- patches.fixes/patch-2.6.11-rc1: 2.6.11-rc1.
[linux-flexiantxendom0-3.2.10.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 static u8 pdcnew_ratemask (ide_drive_t *drive)
45 {
46         u8 mode;
47
48         switch(HWIF(drive)->pci_dev->device) {
49                 case PCI_DEVICE_ID_PROMISE_20277:
50                 case PCI_DEVICE_ID_PROMISE_20276:
51                 case PCI_DEVICE_ID_PROMISE_20275:
52                 case PCI_DEVICE_ID_PROMISE_20271:
53                 case PCI_DEVICE_ID_PROMISE_20269:
54                         mode = 4;
55                         break;
56                 case PCI_DEVICE_ID_PROMISE_20270:
57                 case PCI_DEVICE_ID_PROMISE_20268:
58                         mode = 3;
59                         break;
60                 default:
61                         return 0;
62         }
63         if (!eighty_ninty_three(drive))
64                 mode = min(mode, (u8)1);
65         return mode;
66 }
67
68 static int check_in_drive_lists (ide_drive_t *drive, const char **list)
69 {
70         struct hd_driveid *id = drive->id;
71
72         if (pdc_quirk_drives == list) {
73                 while (*list) {
74                         if (strstr(id->model, *list++)) {
75                                 return 2;
76                         }
77                 }
78         } else {
79                 while (*list) {
80                         if (!strcmp(*list++,id->model)) {
81                                 return 1;
82                         }
83                 }
84         }
85         return 0;
86 }
87
88 static int pdcnew_new_tune_chipset (ide_drive_t *drive, u8 xferspeed)
89 {
90         ide_hwif_t *hwif        = HWIF(drive);
91         unsigned long indexreg  = hwif->dma_vendor1;
92         unsigned long datareg   = hwif->dma_vendor3;
93         u8 thold                = 0x10;
94         u8 adj                  = (drive->dn%2) ? 0x08 : 0x00;
95         u8 speed                = ide_rate_filter(pdcnew_ratemask(drive), xferspeed);
96
97         if (speed == XFER_UDMA_2) {
98                 hwif->OUTB((thold + adj), indexreg);
99                 hwif->OUTB((hwif->INB(datareg) & 0x7f), datareg);
100         }
101
102         switch (speed) {
103                 case XFER_UDMA_7:
104                         speed = XFER_UDMA_6;
105                 case XFER_UDMA_6:       set_ultra(0x1a, 0x01, 0xcb); break;
106                 case XFER_UDMA_5:       set_ultra(0x1a, 0x02, 0xcb); break;
107                 case XFER_UDMA_4:       set_ultra(0x1a, 0x03, 0xcd); break;
108                 case XFER_UDMA_3:       set_ultra(0x1a, 0x05, 0xcd); break;
109                 case XFER_UDMA_2:       set_ultra(0x2a, 0x07, 0xcd); break;
110                 case XFER_UDMA_1:       set_ultra(0x3a, 0x0a, 0xd0); break;
111                 case XFER_UDMA_0:       set_ultra(0x4a, 0x0f, 0xd5); break;
112                 case XFER_MW_DMA_2:     set_ata2(0x69, 0x25); break;
113                 case XFER_MW_DMA_1:     set_ata2(0x6b, 0x27); break;
114                 case XFER_MW_DMA_0:     set_ata2(0xdf, 0x5f); break;
115                 case XFER_PIO_4:        set_pio(0x23, 0x09, 0x25); break;
116                 case XFER_PIO_3:        set_pio(0x27, 0x0d, 0x35); break;
117                 case XFER_PIO_2:        set_pio(0x23, 0x26, 0x64); break;
118                 case XFER_PIO_1:        set_pio(0x46, 0x29, 0xa4); break;
119                 case XFER_PIO_0:        set_pio(0xfb, 0x2b, 0xac); break;
120                 default:
121                         ;
122         }
123
124         return (ide_config_drive_speed(drive, speed));
125 }
126
127 /*   0    1    2    3    4    5    6   7   8
128  * 960, 480, 390, 300, 240, 180, 120, 90, 60
129  *           180, 150, 120,  90,  60
130  * DMA_Speed
131  * 180, 120,  90,  90,  90,  60,  30
132  *  11,   5,   4,   3,   2,   1,   0
133  */
134 static void pdcnew_tune_drive(ide_drive_t *drive, u8 pio)
135 {
136         u8 speed;
137
138         if (pio == 5) pio = 4;
139         speed = XFER_PIO_0 + ide_get_best_pio_mode(drive, 255, pio, NULL);
140
141         (void)pdcnew_new_tune_chipset(drive, speed);
142 }
143
144 static u8 pdcnew_new_cable_detect (ide_hwif_t *hwif)
145 {
146         hwif->OUTB(0x0b, hwif->dma_vendor1);
147         return ((u8)((hwif->INB(hwif->dma_vendor3) & 0x04)));
148 }
149 static int config_chipset_for_dma (ide_drive_t *drive)
150 {
151         struct hd_driveid *id   = drive->id;
152         ide_hwif_t *hwif        = HWIF(drive);
153         u8 speed                = -1;
154         u8 cable;
155
156         u8 ultra_66             = ((id->dma_ultra & 0x0010) ||
157                                    (id->dma_ultra & 0x0008)) ? 1 : 0;
158
159         cable = pdcnew_new_cable_detect(hwif);
160
161         if (ultra_66 && cable) {
162                 printk(KERN_WARNING "Warning: %s channel requires an 80-pin cable for operation.\n", hwif->channel ? "Secondary":"Primary");
163                 printk(KERN_WARNING "%s reduced to Ultra33 mode.\n", drive->name);
164         }
165
166         if (drive->media != ide_disk)
167                 return 0;
168         if (id->capability & 4) {       /* IORDY_EN & PREFETCH_EN */
169                 hwif->OUTB((0x13 + ((drive->dn%2) ? 0x08 : 0x00)), hwif->dma_vendor1);
170                 hwif->OUTB((hwif->INB(hwif->dma_vendor3)|0x03), hwif->dma_vendor3);
171         }
172
173         speed = ide_dma_speed(drive, pdcnew_ratemask(drive));
174
175         if (!(speed)) {
176                 hwif->tuneproc(drive, 5);
177                 return 0;
178         }
179
180         (void) hwif->speedproc(drive, speed);
181         return ide_dma_enable(drive);
182 }
183
184 static int pdcnew_config_drive_xfer_rate (ide_drive_t *drive)
185 {
186         ide_hwif_t *hwif        = HWIF(drive);
187         struct hd_driveid *id   = drive->id;
188
189         drive->init_speed = 0;
190
191         if (id && (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                 hwif->tuneproc(drive, 5);
203                 return hwif->ide_dma_off_quietly(drive);
204         }
205         /* IORDY not supported */
206         return 0;
207 }
208
209 static int pdcnew_quirkproc (ide_drive_t *drive)
210 {
211         return ((int) check_in_drive_lists(drive, pdc_quirk_drives));
212 }
213
214 static int pdcnew_ide_dma_lostirq(ide_drive_t *drive)
215 {
216         if (HWIF(drive)->resetproc != NULL)
217                 HWIF(drive)->resetproc(drive);
218         return __ide_dma_lostirq(drive);
219 }
220
221 static int pdcnew_ide_dma_timeout(ide_drive_t *drive)
222 {
223         if (HWIF(drive)->resetproc != NULL)
224                 HWIF(drive)->resetproc(drive);
225         return __ide_dma_timeout(drive);
226 }
227
228 static void pdcnew_new_reset (ide_drive_t *drive)
229 {
230         /*
231          * Deleted this because it is redundant from the caller.
232          */
233         printk(KERN_WARNING "PDC202XX: %s channel reset.\n",
234                 HWIF(drive)->channel ? "Secondary" : "Primary");
235 }
236
237 #ifdef CONFIG_PPC_PMAC
238 static void __devinit apple_kiwi_init(struct pci_dev *pdev)
239 {
240         struct device_node *np = pci_device_to_OF_node(pdev);
241         unsigned int class_rev = 0;
242         void __iomem *mmio;
243         u8 conf;
244
245         if (np == NULL || !device_is_compatible(np, "kiwi-root"))
246                 return;
247
248         pci_read_config_dword(pdev, PCI_CLASS_REVISION, &class_rev);
249         class_rev &= 0xff;
250
251         if (class_rev >= 0x03) {
252                 /* Setup chip magic config stuff (from darwin) */
253                 pci_read_config_byte(pdev, 0x40, &conf);
254                 pci_write_config_byte(pdev, 0x40, conf | 0x01);
255         }
256         mmio = ioremap(pci_resource_start(pdev, 5),
257                                       pci_resource_len(pdev, 5));
258
259         /* Setup some PLL stuffs */
260         switch (pdev->device) {
261         case PCI_DEVICE_ID_PROMISE_20270:
262                 writew(0x0d2b, mmio + 0x1202);
263                 mdelay(30);
264                 break;
265         case PCI_DEVICE_ID_PROMISE_20271:
266                 writew(0x0826, mmio + 0x1202);
267                 mdelay(30);
268                 break;
269         }
270
271         iounmap(mmio);
272 }
273 #endif /* CONFIG_PPC_PMAC */
274
275 static unsigned int __devinit init_chipset_pdcnew(struct pci_dev *dev, const char *name)
276 {
277         if (dev->resource[PCI_ROM_RESOURCE].start) {
278                 pci_write_config_dword(dev, PCI_ROM_ADDRESS,
279                         dev->resource[PCI_ROM_RESOURCE].start | PCI_ROM_ADDRESS_ENABLE);
280                 printk(KERN_INFO "%s: ROM enabled at 0x%08lx\n",
281                         name, dev->resource[PCI_ROM_RESOURCE].start);
282         }
283
284 #ifdef CONFIG_PPC_PMAC
285         apple_kiwi_init(dev);
286 #endif
287
288         return dev->irq;
289 }
290
291 static void __devinit init_hwif_pdc202new(ide_hwif_t *hwif)
292 {
293         hwif->autodma = 0;
294
295         hwif->tuneproc  = &pdcnew_tune_drive;
296         hwif->quirkproc = &pdcnew_quirkproc;
297         hwif->speedproc = &pdcnew_new_tune_chipset;
298         hwif->resetproc = &pdcnew_new_reset;
299
300         hwif->drives[0].autotune = hwif->drives[1].autotune = 1;
301
302         hwif->ultra_mask = 0x7f;
303         hwif->mwdma_mask = 0x07;
304
305         hwif->ide_dma_check = &pdcnew_config_drive_xfer_rate;
306         hwif->ide_dma_lostirq = &pdcnew_ide_dma_lostirq;
307         hwif->ide_dma_timeout = &pdcnew_ide_dma_timeout;
308         if (!(hwif->udma_four))
309                 hwif->udma_four = (pdcnew_new_cable_detect(hwif)) ? 0 : 1;
310         if (!noautodma)
311                 hwif->autodma = 1;
312         hwif->drives[0].autodma = hwif->drives[1].autodma = hwif->autodma;
313 #if PDC202_DEBUG_CABLE
314         printk(KERN_DEBUG "%s: %s-pin cable\n",
315                 hwif->name, hwif->udma_four ? "80" : "40");
316 #endif /* PDC202_DEBUG_CABLE */
317 }
318
319 static int __devinit init_setup_pdcnew(struct pci_dev *dev, ide_pci_device_t *d)
320 {
321         return ide_setup_pci_device(dev, d);
322 }
323
324 static int __devinit init_setup_pdc20270(struct pci_dev *dev,
325                                          ide_pci_device_t *d)
326 {
327         struct pci_dev *findev = NULL;
328
329         if ((dev->bus->self &&
330              dev->bus->self->vendor == PCI_VENDOR_ID_DEC) &&
331             (dev->bus->self->device == PCI_DEVICE_ID_DEC_21150)) {
332                 if (PCI_SLOT(dev->devfn) & 2)
333                         return -ENODEV;
334                 d->extra = 0;
335                 while ((findev = pci_find_device(PCI_ANY_ID, PCI_ANY_ID, findev)) != NULL) {
336                         if ((findev->vendor == dev->vendor) &&
337                             (findev->device == dev->device) &&
338                             (PCI_SLOT(findev->devfn) & 2)) {
339                                 if (findev->irq != dev->irq) {
340                                         findev->irq = dev->irq;
341                                 }
342                                 return ide_setup_pci_devices(dev, findev, d);
343                         }
344                 }
345         }
346         return ide_setup_pci_device(dev, d);
347 }
348
349 static int __devinit init_setup_pdc20276(struct pci_dev *dev,
350                                          ide_pci_device_t *d)
351 {
352         if ((dev->bus->self) &&
353             (dev->bus->self->vendor == PCI_VENDOR_ID_INTEL) &&
354             ((dev->bus->self->device == PCI_DEVICE_ID_INTEL_I960) ||
355              (dev->bus->self->device == PCI_DEVICE_ID_INTEL_I960RM))) {
356                 printk(KERN_INFO "ide: Skipping Promise PDC20276 "
357                         "attached to I2O RAID controller.\n");
358                 return -ENODEV;
359         }
360         return ide_setup_pci_device(dev, d);
361 }
362
363 /**
364  *      pdc202new_init_one      -       called when a pdc202xx is found
365  *      @dev: the pdc202new device
366  *      @id: the matching pci id
367  *
368  *      Called when the PCI registration layer (or the IDE initialization)
369  *      finds a device matching our IDE device tables.
370  */
371  
372 static int __devinit pdc202new_init_one(struct pci_dev *dev, const struct pci_device_id *id)
373 {
374         ide_pci_device_t *d = &pdcnew_chipsets[id->driver_data];
375
376         return d->init_setup(dev, d);
377 }
378
379 static struct pci_device_id pdc202new_pci_tbl[] = {
380         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20268, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
381         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20269, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
382         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20270, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
383         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20271, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 3},
384         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20275, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 4},
385         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20276, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 5},
386         { PCI_VENDOR_ID_PROMISE, PCI_DEVICE_ID_PROMISE_20277, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 6},
387         { 0, },
388 };
389 MODULE_DEVICE_TABLE(pci, pdc202new_pci_tbl);
390
391 static struct pci_driver driver = {
392         .name           = "Promise_IDE",
393         .id_table       = pdc202new_pci_tbl,
394         .probe          = pdc202new_init_one,
395 };
396
397 static int pdc202new_ide_init(void)
398 {
399         return ide_pci_register_driver(&driver);
400 }
401
402 module_init(pdc202new_ide_init);
403
404 MODULE_AUTHOR("Andre Hedrick, Frank Tiernan");
405 MODULE_DESCRIPTION("PCI driver module for Promise PDC20268 and higher");
406 MODULE_LICENSE("GPL");