patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / ide / pci / pdc202xx_new.c
1 /*
2  *  Promise TX2/TX4/TX2000/133 IDE driver
3  *
4  *  This program is free software; you can redistribute it and/or
5  *  modify it under the terms of the GNU General Public License
6  *  as published by the Free Software Foundation; either version
7  *  2 of the License, or (at your option) any later version.
8  *
9  *  Split from:
10  *  linux/drivers/ide/pdc202xx.c        Version 0.35    Mar. 30, 2002
11  *  Copyright (C) 1998-2002             Andre Hedrick <andre@linux-ide.org>
12  *  Portions Copyright (C) 1999 Promise Technology, Inc.
13  *  Author: Frank Tiernan (frankt@promise.com)
14  *  Released under terms of General Public License
15  */
16
17 #include <linux/config.h>
18 #include <linux/module.h>
19 #include <linux/types.h>
20 #include <linux/kernel.h>
21 #include <linux/delay.h>
22 #include <linux/timer.h>
23 #include <linux/mm.h>
24 #include <linux/ioport.h>
25 #include <linux/blkdev.h>
26 #include <linux/hdreg.h>
27 #include <linux/interrupt.h>
28 #include <linux/pci.h>
29 #include <linux/init.h>
30 #include <linux/ide.h>
31
32 #include <asm/io.h>
33 #include <asm/irq.h>
34
35 #ifdef CONFIG_PPC_PMAC
36 #include <asm/prom.h>
37 #include <asm/pci-bridge.h>
38 #endif
39
40 #include "pdc202xx_new.h"
41
42 #define PDC202_DEBUG_CABLE      0
43
44 #if defined(DISPLAY_PDC202XX_TIMINGS) && defined(CONFIG_PROC_FS)
45 #include <linux/stat.h>
46 #include <linux/proc_fs.h>
47
48 static u8 pdcnew_proc = 0;
49 #define PDC202_MAX_DEVS         5
50 static struct pci_dev *pdc202_devs[PDC202_MAX_DEVS];
51 static int n_pdc202_devs;
52
53 static char * pdcnew_info(char *buf, struct pci_dev *dev)
54 {
55         char *p = buf;
56
57         p += sprintf(p, "\n                                ");
58         switch(dev->device) {
59                 case PCI_DEVICE_ID_PROMISE_20277:
60                         p += sprintf(p, "SBFastTrak 133 Lite"); break;
61                 case PCI_DEVICE_ID_PROMISE_20276:
62                         p += sprintf(p, "MBFastTrak 133 Lite"); break;
63                 case PCI_DEVICE_ID_PROMISE_20275:
64                         p += sprintf(p, "MBUltra133"); break;
65                 case PCI_DEVICE_ID_PROMISE_20271:
66                         p += sprintf(p, "FastTrak TX2000"); break;
67                 case PCI_DEVICE_ID_PROMISE_20270:
68                         p += sprintf(p, "FastTrak LP/TX2/TX4"); break;
69                 case PCI_DEVICE_ID_PROMISE_20269:
70                         p += sprintf(p, "Ultra133 TX2"); break;
71                 case PCI_DEVICE_ID_PROMISE_20268:
72                         p += sprintf(p, "Ultra100 TX2"); break;
73                 default:
74                         p += sprintf(p, "Ultra series"); break;
75                         break;
76         }
77         p += sprintf(p, " Chipset.\n");
78         return (char *)p;
79 }
80
81 static int pdcnew_get_info (char *buffer, char **addr, off_t offset, int count)
82 {
83         char *p = buffer;
84         int i, len;
85
86         for (i = 0; i < n_pdc202_devs; i++) {
87                 struct pci_dev *dev     = pdc202_devs[i];
88                 p = pdcnew_info(buffer, dev);
89         }
90         /* p - buffer must be less than 4k! */
91         len = (p - buffer) - offset;
92         *addr = buffer + offset;
93         
94         return len > count ? count : len;
95 }
96 #endif  /* defined(DISPLAY_PDC202XX_TIMINGS) && defined(CONFIG_PROC_FS) */
97
98
99 static u8 pdcnew_ratemask (ide_drive_t *drive)
100 {
101         u8 mode;
102
103         switch(HWIF(drive)->pci_dev->device) {
104                 case PCI_DEVICE_ID_PROMISE_20277:
105                 case PCI_DEVICE_ID_PROMISE_20276:
106                 case PCI_DEVICE_ID_PROMISE_20275:
107                 case PCI_DEVICE_ID_PROMISE_20271:
108                 case PCI_DEVICE_ID_PROMISE_20269:
109                         mode = 4;
110                         break;
111                 case PCI_DEVICE_ID_PROMISE_20270:
112                 case PCI_DEVICE_ID_PROMISE_20268:
113                         mode = 3;
114                         break;
115                 default:
116                         return 0;
117         }
118         if (!eighty_ninty_three(drive))
119                 mode = min(mode, (u8)1);
120         return mode;
121 }
122
123 static int check_in_drive_lists (ide_drive_t *drive, const char **list)
124 {
125         struct hd_driveid *id = drive->id;
126
127         if (pdc_quirk_drives == list) {
128                 while (*list) {
129                         if (strstr(id->model, *list++)) {
130                                 return 2;
131                         }
132                 }
133         } else {
134                 while (*list) {
135                         if (!strcmp(*list++,id->model)) {
136                                 return 1;
137                         }
138                 }
139         }
140         return 0;
141 }
142
143 static int pdcnew_new_tune_chipset (ide_drive_t *drive, u8 xferspeed)
144 {
145         ide_hwif_t *hwif        = HWIF(drive);
146         unsigned long indexreg  = hwif->dma_vendor1;
147         unsigned long datareg   = hwif->dma_vendor3;
148         u8 thold                = 0x10;
149         u8 adj                  = (drive->dn%2) ? 0x08 : 0x00;
150         u8 speed                = ide_rate_filter(pdcnew_ratemask(drive), xferspeed);
151
152         if (speed == XFER_UDMA_2) {
153                 hwif->OUTB((thold + adj), indexreg);
154                 hwif->OUTB((hwif->INB(datareg) & 0x7f), datareg);
155         }
156
157         switch (speed) {
158                 case XFER_UDMA_7:
159                         speed = XFER_UDMA_6;
160                 case XFER_UDMA_6:       set_ultra(0x1a, 0x01, 0xcb); break;
161                 case XFER_UDMA_5:       set_ultra(0x1a, 0x02, 0xcb); break;
162                 case XFER_UDMA_4:       set_ultra(0x1a, 0x03, 0xcd); break;
163                 case XFER_UDMA_3:       set_ultra(0x1a, 0x05, 0xcd); break;
164                 case XFER_UDMA_2:       set_ultra(0x2a, 0x07, 0xcd); break;
165                 case XFER_UDMA_1:       set_ultra(0x3a, 0x0a, 0xd0); break;
166                 case XFER_UDMA_0:       set_ultra(0x4a, 0x0f, 0xd5); break;
167                 case XFER_MW_DMA_2:     set_ata2(0x69, 0x25); break;
168                 case XFER_MW_DMA_1:     set_ata2(0x6b, 0x27); break;
169                 case XFER_MW_DMA_0:     set_ata2(0xdf, 0x5f); break;
170                 case XFER_PIO_4:        set_pio(0x23, 0x09, 0x25); break;
171                 case XFER_PIO_3:        set_pio(0x27, 0x0d, 0x35); break;
172                 case XFER_PIO_2:        set_pio(0x23, 0x26, 0x64); break;
173                 case XFER_PIO_1:        set_pio(0x46, 0x29, 0xa4); break;
174                 case XFER_PIO_0:        set_pio(0xfb, 0x2b, 0xac); break;
175                 default:
176                         ;
177         }
178
179         return (ide_config_drive_speed(drive, speed));
180 }
181
182 /*   0    1    2    3    4    5    6   7   8
183  * 960, 480, 390, 300, 240, 180, 120, 90, 60
184  *           180, 150, 120,  90,  60
185  * DMA_Speed
186  * 180, 120,  90,  90,  90,  60,  30
187  *  11,   5,   4,   3,   2,   1,   0
188  */
189 static void pdcnew_tune_drive(ide_drive_t *drive, u8 pio)
190 {
191         u8 speed;
192
193         if (pio == 5) pio = 4;
194         speed = XFER_PIO_0 + ide_get_best_pio_mode(drive, 255, pio, NULL);
195
196         (void)pdcnew_new_tune_chipset(drive, speed);
197 }
198
199 static u8 pdcnew_new_cable_detect (ide_hwif_t *hwif)
200 {
201         hwif->OUTB(0x0b, hwif->dma_vendor1);
202         return ((u8)((hwif->INB(hwif->dma_vendor3) & 0x04)));
203 }
204 static int config_chipset_for_dma (ide_drive_t *drive)
205 {
206         struct hd_driveid *id   = drive->id;
207         ide_hwif_t *hwif        = HWIF(drive);
208         u8 speed                = -1;
209         u8 cable;
210
211         u8 ultra_66             = ((id->dma_ultra & 0x0010) ||
212                                    (id->dma_ultra & 0x0008)) ? 1 : 0;
213
214         cable = pdcnew_new_cable_detect(hwif);
215
216         if (ultra_66 && cable) {
217                 printk(KERN_WARNING "Warning: %s channel requires an 80-pin cable for operation.\n", hwif->channel ? "Secondary":"Primary");
218                 printk(KERN_WARNING "%s reduced to Ultra33 mode.\n", drive->name);
219         }
220
221         if (drive->media != ide_disk)
222                 return 0;
223         if (id->capability & 4) {       /* IORDY_EN & PREFETCH_EN */
224                 hwif->OUTB((0x13 + ((drive->dn%2) ? 0x08 : 0x00)), hwif->dma_vendor1);
225                 hwif->OUTB((hwif->INB(hwif->dma_vendor3)|0x03), hwif->dma_vendor3);
226         }
227
228         speed = ide_dma_speed(drive, pdcnew_ratemask(drive));
229
230         if (!(speed)) {
231                 hwif->tuneproc(drive, 5);
232                 return 0;
233         }
234
235         (void) hwif->speedproc(drive, speed);
236         return ide_dma_enable(drive);
237 }
238
239 static int pdcnew_config_drive_xfer_rate (ide_drive_t *drive)
240 {
241         ide_hwif_t *hwif        = HWIF(drive);
242         struct hd_driveid *id   = drive->id;
243
244         drive->init_speed = 0;
245
246         if (id && (id->capability & 1) && drive->autodma) {
247                 /* Consult the list of known "bad" drives */
248                 if (__ide_dma_bad_drive(drive))
249                         goto fast_ata_pio;
250                 if (id->field_valid & 4) {
251                         if (id->dma_ultra & hwif->ultra_mask) {
252                                 /* Force if Capable UltraDMA */
253                                 int dma = config_chipset_for_dma(drive);
254                                 if ((id->field_valid & 2) && !dma)
255                                         goto try_dma_modes;
256                         }
257                 } else if (id->field_valid & 2) {
258 try_dma_modes:
259                         if ((id->dma_mword & hwif->mwdma_mask) ||
260                             (id->dma_1word & hwif->swdma_mask)) {
261                                 /* Force if Capable regular DMA modes */
262                                 if (!config_chipset_for_dma(drive))
263                                         goto no_dma_set;
264                         }
265                 } else if (__ide_dma_good_drive(drive) &&
266                             (id->eide_dma_time < 150)) {
267                                 goto no_dma_set;
268                         /* Consult the list of known "good" drives */
269                         if (!config_chipset_for_dma(drive))
270                                 goto no_dma_set;
271                 } else {
272                         goto fast_ata_pio;
273                 }
274                 return hwif->ide_dma_on(drive);
275         } else if ((id->capability & 8) || (id->field_valid & 2)) {
276 fast_ata_pio:
277 no_dma_set:
278                 hwif->tuneproc(drive, 5);
279                 return hwif->ide_dma_off_quietly(drive);
280         }
281         /* IORDY not supported */
282         return 0;
283 }
284
285 static int pdcnew_quirkproc (ide_drive_t *drive)
286 {
287         return ((int) check_in_drive_lists(drive, pdc_quirk_drives));
288 }
289
290 static int pdcnew_ide_dma_lostirq(ide_drive_t *drive)
291 {
292         if (HWIF(drive)->resetproc != NULL)
293                 HWIF(drive)->resetproc(drive);
294         return __ide_dma_lostirq(drive);
295 }
296
297 static int pdcnew_ide_dma_timeout(ide_drive_t *drive)
298 {
299         if (HWIF(drive)->resetproc != NULL)
300                 HWIF(drive)->resetproc(drive);
301         return __ide_dma_timeout(drive);
302 }
303
304 static void pdcnew_new_reset (ide_drive_t *drive)
305 {
306         /*
307          * Deleted this because it is redundant from the caller.
308          */
309         printk(KERN_WARNING "PDC202XX: %s channel reset.\n",
310                 HWIF(drive)->channel ? "Secondary" : "Primary");
311 }
312
313 static void pdcnew_reset_host (ide_hwif_t *hwif)
314 {
315 //      unsigned long high_16   = hwif->dma_base - (8*(hwif->channel));
316         unsigned long high_16   = hwif->dma_master;
317         u8 udma_speed_flag      = hwif->INB(high_16|0x001f);
318
319         hwif->OUTB((udma_speed_flag | 0x10), (high_16|0x001f));
320         mdelay(100);
321         hwif->OUTB((udma_speed_flag & ~0x10), (high_16|0x001f));
322         mdelay(2000);   /* 2 seconds ?! */
323
324         printk(KERN_WARNING "PDC202XX: %s channel reset.\n",
325                 hwif->channel ? "Secondary" : "Primary");
326 }
327
328 void pdcnew_reset (ide_drive_t *drive)
329 {
330         ide_hwif_t *hwif        = HWIF(drive);
331         ide_hwif_t *mate        = hwif->mate;
332         
333         pdcnew_reset_host(hwif);
334         pdcnew_reset_host(mate);
335 #if 0
336         /*
337          * FIXME: Have to kick all the drives again :-/
338          * What a pain in the ACE!
339          */
340         if (hwif->present) {
341                 u16 hunit = 0;
342                 for (hunit = 0; hunit < MAX_DRIVES; ++hunit) {
343                         ide_drive_t *hdrive = &hwif->drives[hunit];
344                         if (hdrive->present) {
345                                 if (hwif->ide_dma_check)
346                                         hwif->ide_dma_check(hdrive);
347                                 else
348                                         hwif->tuneproc(hdrive, 5);
349                         }
350                 }
351         }
352         if (mate->present) {
353                 u16 munit = 0;
354                 for (munit = 0; munit < MAX_DRIVES; ++munit) {
355                         ide_drive_t *mdrive = &mate->drives[munit];
356                         if (mdrive->present) {
357                                 if (mate->ide_dma_check) 
358                                         mate->ide_dma_check(mdrive);
359                                 else
360                                         mate->tuneproc(mdrive, 5);
361                         }
362                 }
363         }
364 #else
365         hwif->tuneproc(drive, 5);
366 #endif
367 }
368
369 #ifdef CONFIG_PPC_PMAC
370 static void __devinit apple_kiwi_init(struct pci_dev *pdev)
371 {
372         struct device_node *np = pci_device_to_OF_node(pdev);
373         unsigned int class_rev = 0;
374         unsigned long mmio;
375         u8 conf;
376
377         if (np == NULL || !device_is_compatible(np, "kiwi-root"))
378                 return;
379
380         pci_read_config_dword(pdev, PCI_CLASS_REVISION, &class_rev);
381         class_rev &= 0xff;
382
383         if (class_rev >= 0x03) {
384                 /* Setup chip magic config stuff (from darwin) */
385                 pci_read_config_byte(pdev, 0x40, &conf);
386                 pci_write_config_byte(pdev, 0x40, conf | 0x01);
387         }
388         mmio = (unsigned long)ioremap(pci_resource_start(pdev, 5),
389                                       pci_resource_len(pdev, 5));
390
391         /* Setup some PLL stuffs */
392         switch (pdev->device) {
393         case PCI_DEVICE_ID_PROMISE_20270:
394                 writew(0x0d2b, mmio + 0x1202);
395                 mdelay(30);
396                 break;
397         case PCI_DEVICE_ID_PROMISE_20271:
398                 writew(0x0826, mmio + 0x1202);
399                 mdelay(30);
400                 break;
401         }
402
403         iounmap((void *)mmio);
404 }
405 #endif /* CONFIG_PPC_PMAC */
406
407 static unsigned int __devinit init_chipset_pdcnew(struct pci_dev *dev, const char *name)
408 {
409         if (dev->resource[PCI_ROM_RESOURCE].start) {
410                 pci_write_config_dword(dev, PCI_ROM_ADDRESS,
411                         dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
412                 printk(KERN_INFO "%s: ROM enabled at 0x%08lx\n",
413                         name, dev->resource[PCI_ROM_RESOURCE].start);
414         }
415
416 #ifdef CONFIG_PPC_PMAC
417         apple_kiwi_init(dev);
418 #endif
419
420 #if defined(DISPLAY_PDC202XX_TIMINGS) && defined(CONFIG_PROC_FS)
421         pdc202_devs[n_pdc202_devs++] = dev;
422
423         if (!pdcnew_proc) {
424                 pdcnew_proc = 1;
425                 ide_pci_create_host_proc("pdcnew", pdcnew_get_info);
426         }
427 #endif /* DISPLAY_PDC202XX_TIMINGS && CONFIG_PROC_FS */
428
429         return dev->irq;
430 }
431
432 static void __devinit init_hwif_pdc202new(ide_hwif_t *hwif)
433 {
434         hwif->autodma = 0;
435
436         hwif->tuneproc  = &pdcnew_tune_drive;
437         hwif->quirkproc = &pdcnew_quirkproc;
438         hwif->speedproc = &pdcnew_new_tune_chipset;
439         hwif->resetproc = &pdcnew_new_reset;
440
441         hwif->drives[0].autotune = hwif->drives[1].autotune = 1;
442
443         hwif->ultra_mask = 0x7f;
444         hwif->mwdma_mask = 0x07;
445
446         hwif->ide_dma_check = &pdcnew_config_drive_xfer_rate;
447         hwif->ide_dma_lostirq = &pdcnew_ide_dma_lostirq;
448         hwif->ide_dma_timeout = &pdcnew_ide_dma_timeout;
449         if (!(hwif->udma_four))
450                 hwif->udma_four = (pdcnew_new_cable_detect(hwif)) ? 0 : 1;
451         if (!noautodma)
452                 hwif->autodma = 1;
453         hwif->drives[0].autodma = hwif->drives[1].autodma = hwif->autodma;
454 #if PDC202_DEBUG_CABLE
455         printk(KERN_DEBUG "%s: %s-pin cable\n",
456                 hwif->name, hwif->udma_four ? "80" : "40");
457 #endif /* PDC202_DEBUG_CABLE */
458 }
459
460 static void __devinit init_setup_pdcnew(struct pci_dev *dev, ide_pci_device_t *d)
461 {
462         ide_setup_pci_device(dev, d);
463 }
464
465 static void __devinit init_setup_pdc20270(struct pci_dev *dev, ide_pci_device_t *d)
466 {
467         struct pci_dev *findev = NULL;
468
469         if ((dev->bus->self &&
470              dev->bus->self->vendor == PCI_VENDOR_ID_DEC) &&
471             (dev->bus->self->device == PCI_DEVICE_ID_DEC_21150)) {
472                 if (PCI_SLOT(dev->devfn) & 2) {
473                         return;
474                 }
475                 d->extra = 0;
476                 while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
477                         if ((findev->vendor == dev->vendor) &&
478                             (findev->device == dev->device) &&
479                             (PCI_SLOT(findev->devfn) & 2)) {
480                                 if (findev->irq != dev->irq) {
481                                         findev->irq = dev->irq;
482                                 }
483                                 ide_setup_pci_devices(dev, findev, d);
484                                 return;
485                         }
486                 }
487         }
488         ide_setup_pci_device(dev, d);
489 }
490
491 static void __devinit init_setup_pdc20276(struct pci_dev *dev, ide_pci_device_t *d)
492 {
493         if ((dev->bus->self) &&
494             (dev->bus->self->vendor == PCI_VENDOR_ID_INTEL) &&
495             ((dev->bus->self->device == PCI_DEVICE_ID_INTEL_I960) ||
496              (dev->bus->self->device == PCI_DEVICE_ID_INTEL_I960RM))) {
497                 printk(KERN_INFO "ide: Skipping Promise PDC20276 "
498                         "attached to I2O RAID controller.\n");
499                 return;
500         }
501         ide_setup_pci_device(dev, d);
502 }
503
504 /**
505  *      pdc202new_init_one      -       called when a pdc202xx is found
506  *      @dev: the pdc202new device
507  *      @id: the matching pci id
508  *
509  *      Called when the PCI registration layer (or the IDE initialization)
510  *      finds a device matching our IDE device tables.
511  */
512  
513 static int __devinit pdc202new_init_one(struct pci_dev *dev, const struct pci_device_id *id)
514 {
515         ide_pci_device_t *d = &pdcnew_chipsets[id->driver_data];
516
517         d->init_setup(dev, d);
518         return 0;
519 }
520
521 static struct pci_device_id pdc202new_pci_tbl[] = {
522         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20268, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
523         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20269, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
524         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20270, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
525         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20271, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
526         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20275, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
527         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20276, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
528         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20277, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6},
529         { 0, },
530 };
531 MODULE_DEVICE_TABLE(pci, pdc202new_pci_tbl);
532
533 static struct pci_driver driver = {
534         .name           = "Promise IDE",
535         .id_table       = pdc202new_pci_tbl,
536         .probe          = pdc202new_init_one,
537 };
538
539 static int pdc202new_ide_init(void)
540 {
541         return ide_pci_register_driver(&driver);
542 }
543
544 module_init(pdc202new_ide_init);
545
546 MODULE_AUTHOR("Andre Hedrick, Frank Tiernan");
547 MODULE_DESCRIPTION("PCI driver module for Promise PDC20268 and higher");
548 MODULE_LICENSE("GPL");