upgrade to linux 2.6.10-1.12_FC2
[linux-2.6.git] / drivers / ide / pci / aec62xx.c
1 /*
2  * linux/drivers/ide/pci/aec62xx.c              Version 0.11    March 27, 2002
3  *
4  * Copyright (C) 1999-2002      Andre Hedrick <andre@linux-ide.org>
5  *
6  */
7
8 #include <linux/module.h>
9 #include <linux/config.h>
10 #include <linux/types.h>
11 #include <linux/pci.h>
12 #include <linux/delay.h>
13 #include <linux/hdreg.h>
14 #include <linux/ide.h>
15 #include <linux/init.h>
16
17 #include <asm/io.h>
18
19 #include "aec62xx.h"
20
21 #if 0
22                 if (dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) {
23                         (void) pci_read_config_byte(dev, 0x54, &art);
24                         p += sprintf(p, "DMA Mode:       %s(%s)",
25                                 (c0&0x20)?((art&0x03)?"UDMA":" DMA"):" PIO",
26                                 (art&0x02)?"2":(art&0x01)?"1":"0");
27                         p += sprintf(p, "          %s(%s)",
28                                 (c0&0x40)?((art&0x0c)?"UDMA":" DMA"):" PIO",
29                                 (art&0x08)?"2":(art&0x04)?"1":"0");
30                         p += sprintf(p, "         %s(%s)",
31                                 (c1&0x20)?((art&0x30)?"UDMA":" DMA"):" PIO",
32                                 (art&0x20)?"2":(art&0x10)?"1":"0");
33                         p += sprintf(p, "           %s(%s)\n",
34                                 (c1&0x40)?((art&0xc0)?"UDMA":" DMA"):" PIO",
35                                 (art&0x80)?"2":(art&0x40)?"1":"0");
36                 } else {
37 #endif
38
39 /*
40  * TO DO: active tuning and correction of cards without a bios.
41  */
42 static u8 pci_bus_clock_list (u8 speed, struct chipset_bus_clock_list_entry * chipset_table)
43 {
44         for ( ; chipset_table->xfer_speed ; chipset_table++)
45                 if (chipset_table->xfer_speed == speed) {
46                         return chipset_table->chipset_settings;
47                 }
48         return chipset_table->chipset_settings;
49 }
50
51 static u8 pci_bus_clock_list_ultra (u8 speed, struct chipset_bus_clock_list_entry * chipset_table)
52 {
53         for ( ; chipset_table->xfer_speed ; chipset_table++)
54                 if (chipset_table->xfer_speed == speed) {
55                         return chipset_table->ultra_settings;
56                 }
57         return chipset_table->ultra_settings;
58 }
59
60 static u8 aec62xx_ratemask (ide_drive_t *drive)
61 {
62         ide_hwif_t *hwif        = HWIF(drive);
63         u8 mode;
64
65         switch(hwif->pci_dev->device) {
66                 case PCI_DEVICE_ID_ARTOP_ATP865:
67                 case PCI_DEVICE_ID_ARTOP_ATP865R:
68 #if 0
69                         mode = (hwif->INB(hwif->dma_master) & 0x10) ? 4 : 3;
70 #else
71                         mode = (hwif->INB(((hwif->channel) ?
72                                         hwif->mate->dma_status :
73                                         hwif->dma_status)) & 0x10) ? 4 : 3;
74 #endif
75                         break;
76                 case PCI_DEVICE_ID_ARTOP_ATP860:
77                 case PCI_DEVICE_ID_ARTOP_ATP860R:
78                         mode = 2;
79                         break;
80                 case PCI_DEVICE_ID_ARTOP_ATP850UF:
81                 default:
82                         return 1;
83         }
84
85         if (!eighty_ninty_three(drive))
86                 mode = min(mode, (u8)1);
87         return mode;
88 }
89
90 static int aec6210_tune_chipset (ide_drive_t *drive, u8 xferspeed)
91 {
92         ide_hwif_t *hwif        = HWIF(drive);
93         struct pci_dev *dev     = hwif->pci_dev;
94         u16 d_conf              = 0;
95         u8 speed        = ide_rate_filter(aec62xx_ratemask(drive), xferspeed);
96         u8 ultra = 0, ultra_conf = 0;
97         u8 tmp0 = 0, tmp1 = 0, tmp2 = 0;
98         unsigned long flags;
99
100         local_irq_save(flags);
101         /* 0x40|(2*drive->dn): Active, 0x41|(2*drive->dn): Recovery */
102         pci_read_config_word(dev, 0x40|(2*drive->dn), &d_conf);
103         tmp0 = pci_bus_clock_list(speed, BUSCLOCK(dev));
104         SPLIT_BYTE(tmp0,tmp1,tmp2);
105         MAKE_WORD(d_conf,tmp1,tmp2);
106         pci_write_config_word(dev, 0x40|(2*drive->dn), d_conf);
107
108         tmp1 = 0x00;
109         tmp2 = 0x00;
110         pci_read_config_byte(dev, 0x54, &ultra);
111         tmp1 = ((0x00 << (2*drive->dn)) | (ultra & ~(3 << (2*drive->dn))));
112         ultra_conf = pci_bus_clock_list_ultra(speed, BUSCLOCK(dev));
113         tmp2 = ((ultra_conf << (2*drive->dn)) | (tmp1 & ~(3 << (2*drive->dn))));
114         pci_write_config_byte(dev, 0x54, tmp2);
115         local_irq_restore(flags);
116         return(ide_config_drive_speed(drive, speed));
117 }
118
119 static int aec6260_tune_chipset (ide_drive_t *drive, u8 xferspeed)
120 {
121         ide_hwif_t *hwif        = HWIF(drive);
122         struct pci_dev *dev     = hwif->pci_dev;
123         u8 speed        = ide_rate_filter(aec62xx_ratemask(drive), xferspeed);
124         u8 unit         = (drive->select.b.unit & 0x01);
125         u8 tmp1 = 0, tmp2 = 0;
126         u8 ultra = 0, drive_conf = 0, ultra_conf = 0;
127         unsigned long flags;
128
129         local_irq_save(flags);
130         /* high 4-bits: Active, low 4-bits: Recovery */
131         pci_read_config_byte(dev, 0x40|drive->dn, &drive_conf);
132         drive_conf = pci_bus_clock_list(speed, BUSCLOCK(dev));
133         pci_write_config_byte(dev, 0x40|drive->dn, drive_conf);
134
135         pci_read_config_byte(dev, (0x44|hwif->channel), &ultra);
136         tmp1 = ((0x00 << (4*unit)) | (ultra & ~(7 << (4*unit))));
137         ultra_conf = pci_bus_clock_list_ultra(speed, BUSCLOCK(dev));
138         tmp2 = ((ultra_conf << (4*unit)) | (tmp1 & ~(7 << (4*unit))));
139         pci_write_config_byte(dev, (0x44|hwif->channel), tmp2);
140         local_irq_restore(flags);
141         return(ide_config_drive_speed(drive, speed));
142 }
143
144 static int aec62xx_tune_chipset (ide_drive_t *drive, u8 speed)
145 {
146         switch (HWIF(drive)->pci_dev->device) {
147                 case PCI_DEVICE_ID_ARTOP_ATP865:
148                 case PCI_DEVICE_ID_ARTOP_ATP865R:
149                 case PCI_DEVICE_ID_ARTOP_ATP860:
150                 case PCI_DEVICE_ID_ARTOP_ATP860R:
151                         return ((int) aec6260_tune_chipset(drive, speed));
152                 case PCI_DEVICE_ID_ARTOP_ATP850UF:
153                         return ((int) aec6210_tune_chipset(drive, speed));
154                 default:
155                         return -1;
156         }
157 }
158
159 static int config_chipset_for_dma (ide_drive_t *drive)
160 {
161         u8 speed = ide_dma_speed(drive, aec62xx_ratemask(drive));       
162
163         if (!(speed))
164                 return 0;
165
166         (void) aec62xx_tune_chipset(drive, speed);
167         return ide_dma_enable(drive);
168 }
169
170 static void aec62xx_tune_drive (ide_drive_t *drive, u8 pio)
171 {
172         u8 speed = 0;
173         u8 new_pio = XFER_PIO_0 + ide_get_best_pio_mode(drive, 255, 5, NULL);
174
175         switch(pio) {
176                 case 5:         speed = new_pio; break;
177                 case 4:         speed = XFER_PIO_4; break;
178                 case 3:         speed = XFER_PIO_3; break;
179                 case 2:         speed = XFER_PIO_2; break;
180                 case 1:         speed = XFER_PIO_1; break;
181                 default:        speed = XFER_PIO_0; break;
182         }
183         (void) aec62xx_tune_chipset(drive, speed);
184 }
185
186 static int aec62xx_config_drive_xfer_rate (ide_drive_t *drive)
187 {
188         ide_hwif_t *hwif        = HWIF(drive);
189         struct hd_driveid *id   = drive->id;
190
191         if ((id->capability & 1) && drive->autodma) {
192
193                 if (ide_use_dma(drive)) {
194                         if (config_chipset_for_dma(drive))
195                                 return hwif->ide_dma_on(drive);
196                 }
197
198                 goto fast_ata_pio;
199
200         } else if ((id->capability & 8) || (id->field_valid & 2)) {
201 fast_ata_pio:
202                 aec62xx_tune_drive(drive, 5);
203                 return hwif->ide_dma_off_quietly(drive);
204         }
205         /* IORDY not supported */
206         return 0;
207 }
208
209 static int aec62xx_irq_timeout (ide_drive_t *drive)
210 {
211         ide_hwif_t *hwif        = HWIF(drive);
212         struct pci_dev *dev     = hwif->pci_dev;
213
214         switch(dev->device) {
215                 case PCI_DEVICE_ID_ARTOP_ATP860:
216                 case PCI_DEVICE_ID_ARTOP_ATP860R:
217                 case PCI_DEVICE_ID_ARTOP_ATP865:
218                 case PCI_DEVICE_ID_ARTOP_ATP865R:
219                         printk(" AEC62XX time out ");
220 #if 0
221                         {
222                                 int i = 0;
223                                 u8 reg49h = 0;
224                                 pci_read_config_byte(HWIF(drive)->pci_dev, 0x49, &reg49h);
225                                 for (i=0;i<256;i++)
226                                         pci_write_config_byte(HWIF(drive)->pci_dev, 0x49, reg49h|0x10);
227                                 pci_write_config_byte(HWIF(drive)->pci_dev, 0x49, reg49h & ~0x10);
228                         }
229                         return 0;
230 #endif
231                 default:
232                         break;
233         }
234 #if 0
235         {
236                 ide_hwif_t *hwif        = HWIF(drive);
237                 struct pci_dev *dev     = hwif->pci_dev;
238                 u8 tmp1 = 0, tmp2 = 0, mode6 = 0;
239
240                 pci_read_config_byte(dev, 0x44, &tmp1);
241                 pci_read_config_byte(dev, 0x45, &tmp2);
242                 printk(" AEC6280 r44=%x r45=%x ",tmp1,tmp2);
243                 mode6 = HWIF(drive)->INB(((hwif->channel) ?
244                                            hwif->mate->dma_status :
245                                            hwif->dma_status));
246                 printk(" AEC6280 133=%x ", (mode6 & 0x10));
247         }
248 #endif
249         return 0;
250 }
251
252 static unsigned int __devinit init_chipset_aec62xx(struct pci_dev *dev, const char *name)
253 {
254         int bus_speed = system_bus_clock();
255
256         if (dev->resource[PCI_ROM_RESOURCE].start) {
257                 pci_write_config_dword(dev, PCI_ROM_ADDRESS, dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
258                 printk(KERN_INFO "%s: ROM enabled at 0x%08lx\n", name, dev->resource[PCI_ROM_RESOURCE].start);
259         }
260
261         if (bus_speed <= 33)
262                 pci_set_drvdata(dev, (void *) aec6xxx_33_base);
263         else
264                 pci_set_drvdata(dev, (void *) aec6xxx_34_base);
265
266         return dev->irq;
267 }
268
269 static void __devinit init_hwif_aec62xx(ide_hwif_t *hwif)
270 {
271         hwif->autodma = 0;
272         hwif->tuneproc = &aec62xx_tune_drive;
273         hwif->speedproc = &aec62xx_tune_chipset;
274
275         if (hwif->pci_dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) {
276                 hwif->serialized = hwif->channel;
277                 hwif->no_dsc = 1;
278         }
279
280         if (hwif->mate)
281                 hwif->mate->serialized = hwif->serialized;
282
283         if (!hwif->dma_base) {
284                 hwif->drives[0].autotune = 1;
285                 hwif->drives[1].autotune = 1;
286                 return;
287         }
288
289         hwif->ultra_mask = 0x7f;
290         hwif->mwdma_mask = 0x07;
291         hwif->swdma_mask = 0x07;
292
293         hwif->ide_dma_check     = &aec62xx_config_drive_xfer_rate;
294         hwif->ide_dma_lostirq   = &aec62xx_irq_timeout;
295         hwif->ide_dma_timeout   = &aec62xx_irq_timeout;
296         if (!noautodma)
297                 hwif->autodma = 1;
298         hwif->drives[0].autodma = hwif->autodma;
299         hwif->drives[1].autodma = hwif->autodma;
300 }
301
302 static void __devinit init_dma_aec62xx(ide_hwif_t *hwif, unsigned long dmabase)
303 {
304         struct pci_dev *dev     = hwif->pci_dev;
305
306         if (dev->device == PCI_DEVICE_ID_ARTOP_ATP850UF) {
307                 u8 reg54h = 0;
308                 unsigned long flags;
309
310                 spin_lock_irqsave(&ide_lock, flags);
311                 pci_read_config_byte(dev, 0x54, &reg54h);
312                 pci_write_config_byte(dev, 0x54, reg54h & ~(hwif->channel ? 0xF0 : 0x0F));
313                 spin_unlock_irqrestore(&ide_lock, flags);
314         } else {
315                 u8 ata66        = 0;
316                 pci_read_config_byte(hwif->pci_dev, 0x49, &ata66);
317                 if (!(hwif->udma_four))
318                         hwif->udma_four = (ata66&(hwif->channel?0x02:0x01))?0:1;
319         }
320
321         ide_setup_dma(hwif, dmabase, 8);
322 }
323
324 static void __devinit init_setup_aec62xx(struct pci_dev *dev, ide_pci_device_t *d)
325 {
326         ide_setup_pci_device(dev, d);
327 }
328
329 static void __devinit init_setup_aec6x80(struct pci_dev *dev, ide_pci_device_t *d)
330 {
331         unsigned long bar4reg = pci_resource_start(dev, 4);
332
333         if (inb(bar4reg+2) & 0x10) {
334                 strcpy(d->name, "AEC6880");
335                 if (dev->device == PCI_DEVICE_ID_ARTOP_ATP865R)
336                         strcpy(d->name, "AEC6880R");
337         } else {
338                 strcpy(d->name, "AEC6280");
339                 if (dev->device == PCI_DEVICE_ID_ARTOP_ATP865R)
340                         strcpy(d->name, "AEC6280R");
341         }
342
343         ide_setup_pci_device(dev, d);
344 }
345
346 /**
347  *      aec62xx_init_one        -       called when a AEC is found
348  *      @dev: the aec62xx device
349  *      @id: the matching pci id
350  *
351  *      Called when the PCI registration layer (or the IDE initialization)
352  *      finds a device matching our IDE device tables.
353  */
354  
355 static int __devinit aec62xx_init_one(struct pci_dev *dev, const struct pci_device_id *id)
356 {
357         ide_pci_device_t *d = &aec62xx_chipsets[id->driver_data];
358
359         d->init_setup(dev, d);
360         return 0;
361 }
362
363 static struct pci_device_id aec62xx_pci_tbl[] = {
364         { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP850UF, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0 },
365         { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP860,   PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1 },
366         { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP860R,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2 },
367         { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP865,   PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3 },
368         { PCI_VENDOR_ID_ARTOP, PCI_DEVICE_ID_ARTOP_ATP865R,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4 },
369         { 0, },
370 };
371 MODULE_DEVICE_TABLE(pci, aec62xx_pci_tbl);
372
373 static struct pci_driver driver = {
374         .name           = "AEC62xx_IDE",
375         .id_table       = aec62xx_pci_tbl,
376         .probe          = aec62xx_init_one,
377 };
378
379 static int aec62xx_ide_init(void)
380 {
381         return ide_pci_register_driver(&driver);
382 }
383
384 module_init(aec62xx_ide_init);
385
386 MODULE_AUTHOR("Andre Hedrick");
387 MODULE_DESCRIPTION("PCI driver module for ARTOP AEC62xx IDE");
388 MODULE_LICENSE("GPL");