ftp://ftp.kernel.org/pub/linux/kernel/v2.6/linux-2.6.6.tar.bz2
[linux-2.6.git] / drivers / ide / pci / sl82c105.c
1 /*
2  * linux/drivers/ide/pci/sl82c105.c
3  *
4  * SL82C105/Winbond 553 IDE driver
5  *
6  * Maintainer unknown.
7  *
8  * Drive tuning added from Rebel.com's kernel sources
9  *  -- Russell King (15/11/98) linux@arm.linux.org.uk
10  * 
11  * Merge in Russell's HW workarounds, fix various problems
12  * with the timing registers setup.
13  *  -- Benjamin Herrenschmidt (01/11/03) benh@kernel.crashing.org
14  */
15
16 #include <linux/config.h>
17 #include <linux/types.h>
18 #include <linux/module.h>
19 #include <linux/kernel.h>
20 #include <linux/timer.h>
21 #include <linux/mm.h>
22 #include <linux/ioport.h>
23 #include <linux/interrupt.h>
24 #include <linux/blkdev.h>
25 #include <linux/hdreg.h>
26 #include <linux/pci.h>
27 #include <linux/ide.h>
28
29 #include <asm/io.h>
30 #include <asm/dma.h>
31
32 #include "sl82c105.h"
33
34 #undef DEBUG
35
36 #ifdef DEBUG
37 #define DBG(arg) printk arg
38 #else
39 #define DBG(fmt,...)
40 #endif
41 /*
42  * SL82C105 PCI config register 0x40 bits.
43  */
44 #define CTRL_IDE_IRQB   (1 << 30)
45 #define CTRL_IDE_IRQA   (1 << 28)
46 #define CTRL_LEGIRQ     (1 << 11)
47 #define CTRL_P1F16      (1 << 5)
48 #define CTRL_P1EN       (1 << 4)
49 #define CTRL_P0F16      (1 << 1)
50 #define CTRL_P0EN       (1 << 0)
51
52 /*
53  * Convert a PIO mode and cycle time to the required on/off
54  * times for the interface.  This has protection against run-away
55  * timings.
56  */
57 static unsigned int get_timing_sl82c105(ide_pio_data_t *p)
58 {
59         unsigned int cmd_on;
60         unsigned int cmd_off;
61
62         cmd_on = (ide_pio_timings[p->pio_mode].active_time + 29) / 30;
63         cmd_off = (p->cycle_time - 30 * cmd_on + 29) / 30;
64
65         if (cmd_on > 32)
66                 cmd_on = 32;
67         if (cmd_on == 0)
68                 cmd_on = 1;
69
70         if (cmd_off > 32)
71                 cmd_off = 32;
72         if (cmd_off == 0)
73                 cmd_off = 1;
74
75         return (cmd_on - 1) << 8 | (cmd_off - 1) | (p->use_iordy ? 0x40 : 0x00);
76 }
77
78 /*
79  * Configure the drive and chipset for PIO
80  */
81 static void config_for_pio(ide_drive_t *drive, int pio, int report, int chipset_only)
82 {
83         ide_hwif_t *hwif = HWIF(drive);
84         struct pci_dev *dev = hwif->pci_dev;
85         ide_pio_data_t p;
86         u16 drv_ctrl = 0x909;
87         unsigned int xfer_mode, reg;
88
89         DBG(("config_for_pio(drive:%s, pio:%d, report:%d, chipset_only:%d)\n",
90                 drive->name, pio, report, chipset_only));
91                 
92         reg = (hwif->channel ? 0x4c : 0x44) + (drive->select.b.unit ? 4 : 0);
93
94         pio = ide_get_best_pio_mode(drive, pio, 5, &p);
95
96         xfer_mode = XFER_PIO_0 + pio;
97
98         if (chipset_only || ide_config_drive_speed(drive, xfer_mode) == 0) {
99                 drv_ctrl = get_timing_sl82c105(&p);
100                 drive->pio_speed = xfer_mode;
101         } else
102                 drive->pio_speed = XFER_PIO_0;
103
104         if (drive->using_dma == 0) {
105                 /*
106                  * If we are actually using MW DMA, then we can not
107                  * reprogram the interface drive control register.
108                  */
109                 pci_write_config_word(dev, reg, drv_ctrl);
110                 pci_read_config_word(dev, reg, &drv_ctrl);
111
112                 if (report) {
113                         printk("%s: selected %s (%dns) (%04X)\n", drive->name,
114                                ide_xfer_verbose(xfer_mode), p.cycle_time, drv_ctrl);
115                 }
116         }
117 }
118
119 /*
120  * Configure the drive and the chipset for DMA
121  */
122 static int config_for_dma (ide_drive_t *drive)
123 {
124         ide_hwif_t *hwif = HWIF(drive);
125         struct pci_dev *dev = hwif->pci_dev;
126         unsigned int reg;
127
128         DBG(("config_for_dma(drive:%s)\n", drive->name));
129
130         reg = (hwif->channel ? 0x4c : 0x44) + (drive->select.b.unit ? 4 : 0);
131
132         if (ide_config_drive_speed(drive, XFER_MW_DMA_2) != 0)
133                 return 1;
134
135         pci_write_config_word(dev, reg, 0x0240);
136
137         return 0;
138 }
139
140 /*
141  * Check to see if the drive and
142  * chipset is capable of DMA mode
143  */
144
145 static int sl82c105_check_drive (ide_drive_t *drive)
146 {
147         ide_hwif_t *hwif        = HWIF(drive);
148
149         DBG(("sl82c105_check_drive(drive:%s)\n", drive->name));
150
151         do {
152                 struct hd_driveid *id = drive->id;
153
154                 if (!drive->autodma)
155                         break;
156
157                 if (!id || !(id->capability & 1))
158                         break;
159
160                 /* Consult the list of known "bad" drives */
161                 if (__ide_dma_bad_drive(drive))
162                         break;
163
164                 if (id->field_valid & 2) {
165                         if ((id->dma_mword & hwif->mwdma_mask) ||
166                             (id->dma_1word & hwif->swdma_mask))
167                                 return hwif->ide_dma_on(drive);
168                 }
169
170                 if (__ide_dma_good_drive(drive))
171                         return hwif->ide_dma_on(drive);
172         } while (0);
173
174         return hwif->ide_dma_off_quietly(drive);
175 }
176
177 /*
178  * The SL82C105 holds off all IDE interrupts while in DMA mode until
179  * all DMA activity is completed.  Sometimes this causes problems (eg,
180  * when the drive wants to report an error condition).
181  *
182  * 0x7e is a "chip testing" register.  Bit 2 resets the DMA controller
183  * state machine.  We need to kick this to work around various bugs.
184  */
185 static inline void sl82c105_reset_host(struct pci_dev *dev)
186 {
187         u16 val;
188
189         pci_read_config_word(dev, 0x7e, &val);
190         pci_write_config_word(dev, 0x7e, val | (1 << 2));
191         pci_write_config_word(dev, 0x7e, val & ~(1 << 2));
192 }
193
194 /*
195  * If we get an IRQ timeout, it might be that the DMA state machine
196  * got confused.  Fix from Todd Inglett.  Details from Winbond.
197  *
198  * This function is called when the IDE timer expires, the drive
199  * indicates that it is READY, and we were waiting for DMA to complete.
200  */
201 static int sl82c105_ide_dma_lost_irq(ide_drive_t *drive)
202 {
203         ide_hwif_t *hwif = HWIF(drive);
204         struct pci_dev *dev = hwif->pci_dev;
205         u32 val, mask = hwif->channel ? CTRL_IDE_IRQB : CTRL_IDE_IRQA;
206         unsigned long dma_base = hwif->dma_base;
207
208         printk("sl82c105: lost IRQ: resetting host\n");
209
210         /*
211          * Check the raw interrupt from the drive.
212          */
213         pci_read_config_dword(dev, 0x40, &val);
214         if (val & mask)
215                 printk("sl82c105: drive was requesting IRQ, but host lost it\n");
216
217         /*
218          * Was DMA enabled?  If so, disable it - we're resetting the
219          * host.  The IDE layer will be handling the drive for us.
220          */
221         val = hwif->INB(dma_base);
222         if (val & 1) {
223                 outb(val & ~1, dma_base);
224                 printk("sl82c105: DMA was enabled\n");
225         }
226
227         sl82c105_reset_host(dev);
228
229         /* ide_dmaproc would return 1, so we do as well */
230         return 1;
231 }
232
233 /*
234  * ATAPI devices can cause the SL82C105 DMA state machine to go gaga.
235  * Winbond recommend that the DMA state machine is reset prior to
236  * setting the bus master DMA enable bit.
237  *
238  * The generic IDE core will have disabled the BMEN bit before this
239  * function is called.
240  */
241 static int sl82c105_ide_dma_begin(ide_drive_t *drive)
242 {
243         ide_hwif_t *hwif = HWIF(drive);
244         struct pci_dev *dev = hwif->pci_dev;
245
246 //      DBG(("sl82c105_ide_dma_begin(drive:%s)\n", drive->name));
247
248         sl82c105_reset_host(dev);
249         return __ide_dma_begin(drive);
250 }
251
252 static int sl82c105_ide_dma_timeout(ide_drive_t *drive)
253 {
254         ide_hwif_t *hwif = HWIF(drive);
255         struct pci_dev *dev = hwif->pci_dev;
256
257         DBG(("sl82c105_ide_dma_timeout(drive:%s)\n", drive->name));
258
259         sl82c105_reset_host(dev);
260         return __ide_dma_timeout(drive);
261 }
262
263 static int sl82c105_ide_dma_on (ide_drive_t *drive)
264 {
265         DBG(("sl82c105_ide_dma_on(drive:%s)\n", drive->name));
266
267         if (config_for_dma(drive)) {
268                 config_for_pio(drive, 4, 0, 0);
269                 return HWIF(drive)->ide_dma_off_quietly(drive);
270         }
271         printk(KERN_INFO "%s: DMA enabled\n", drive->name);
272         return __ide_dma_on(drive);
273 }
274
275 static int sl82c105_ide_dma_off_quietly (ide_drive_t *drive)
276 {
277         u8 speed = XFER_PIO_0;
278         int rc;
279         
280         DBG(("sl82c105_ide_dma_off_quietly(drive:%s)\n", drive->name));
281
282         rc = __ide_dma_off_quietly(drive);
283         if (drive->pio_speed)
284                 speed = drive->pio_speed - XFER_PIO_0;
285         config_for_pio(drive, speed, 0, 1);
286         drive->current_speed = drive->pio_speed;
287
288         return rc;
289 }
290
291 /*
292  * Ok, that is nasty, but we must make sure the DMA timings
293  * won't be used for a PIO access. The solution here is
294  * to make sure the 16 bits mode is diabled on the channel
295  * when DMA is enabled, thus causing the chip to use PIO0
296  * timings for those operations.
297  */
298 static void sl82c105_selectproc(ide_drive_t *drive)
299 {
300         ide_hwif_t *hwif = HWIF(drive);
301         struct pci_dev *dev = hwif->pci_dev;
302         u32 val, old, mask;
303
304         //DBG(("sl82c105_selectproc(drive:%s)\n", drive->name));
305
306         mask = hwif->channel ? CTRL_P1F16 : CTRL_P0F16;
307         old = val = *((u32 *)&hwif->hwif_data);
308         if (drive->using_dma)
309                 val &= ~mask;
310         else
311                 val |= mask;
312         if (old != val) {
313                 pci_write_config_dword(dev, 0x40, val); 
314                 *((u32 *)&hwif->hwif_data) = val;
315         }
316 }
317
318 /*
319  * ATA reset will clear the 16 bits mode in the control
320  * register, we need to update our cache
321  */
322 static void sl82c105_resetproc(ide_drive_t *drive)
323 {
324         ide_hwif_t *hwif = HWIF(drive);
325         struct pci_dev *dev = hwif->pci_dev;
326         u32 val;
327
328         DBG(("sl82c105_resetproc(drive:%s)\n", drive->name));
329
330         pci_read_config_dword(dev, 0x40, &val);
331         *((u32 *)&hwif->hwif_data) = val;
332 }
333         
334 /*
335  * We only deal with PIO mode here - DMA mode 'using_dma' is not
336  * initialised at the point that this function is called.
337  */
338 static void tune_sl82c105(ide_drive_t *drive, u8 pio)
339 {
340         DBG(("tune_sl82c105(drive:%s)\n", drive->name));
341
342         config_for_pio(drive, pio, 1, 0);
343
344         /*
345          * We support 32-bit I/O on this interface, and it
346          * doesn't have problems with interrupts.
347          */
348         drive->io_32bit = 1;
349         drive->unmask = 1;
350 }
351
352 /*
353  * Return the revision of the Winbond bridge
354  * which this function is part of.
355  */
356 static unsigned int sl82c105_bridge_revision(struct pci_dev *dev)
357 {
358         struct pci_dev *bridge;
359         u8 rev;
360
361         /*
362          * The bridge should be part of the same device, but function 0.
363          */
364         bridge = pci_find_slot(dev->bus->number,
365                                PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
366         if (!bridge)
367                 return -1;
368
369         /*
370          * Make sure it is a Winbond 553 and is an ISA bridge.
371          */
372         if (bridge->vendor != PCI_VENDOR_ID_WINBOND ||
373             bridge->device != PCI_DEVICE_ID_WINBOND_83C553 ||
374             bridge->class >> 8 != PCI_CLASS_BRIDGE_ISA)
375                 return -1;
376
377         /*
378          * We need to find function 0's revision, not function 1
379          */
380         pci_read_config_byte(bridge, PCI_REVISION_ID, &rev);
381
382         return rev;
383 }
384
385 /*
386  * Enable the PCI device
387  * 
388  * --BenH: It's arch fixup code that should enable channels that
389  * have not been enabled by firmware. I decided we can still enable
390  * channel 0 here at least, but channel 1 has to be enabled by
391  * firmware or arch code. We still set both to 16 bits mode.
392  */
393 static unsigned int __init init_chipset_sl82c105(struct pci_dev *dev, const char *msg)
394 {
395         u32 val;
396
397         DBG(("init_chipset_sl82c105()\n"));
398
399         pci_read_config_dword(dev, 0x40, &val);
400         val |= CTRL_P0EN | CTRL_P0F16 | CTRL_P1F16;
401         pci_write_config_dword(dev, 0x40, val);
402
403         return dev->irq;
404 }
405
406 static void __init init_dma_sl82c105(ide_hwif_t *hwif, unsigned long dma_base)
407 {
408         unsigned int rev;
409         u8 dma_state;
410
411         DBG(("init_dma_sl82c105(hwif: ide%d, dma_base: 0x%08x)\n", hwif->index, dma_base));
412
413         hwif->autodma = 0;
414
415         if (!dma_base)
416                 return;
417
418         dma_state = hwif->INB(dma_base + 2);
419         rev = sl82c105_bridge_revision(hwif->pci_dev);
420         if (rev <= 5) {
421                 printk("    %s: Winbond 553 bridge revision %d, BM-DMA disabled\n",
422                        hwif->name, rev);
423                 dma_state &= ~0x60;
424         } else {
425                 dma_state |= 0x60;
426                 if (!noautodma)
427                         hwif->autodma = 1;
428         }
429         hwif->OUTB(dma_state, dma_base + 2);
430
431         ide_setup_dma(hwif, dma_base, 8);
432 }
433
434 /*
435  * Initialise the chip
436  */
437
438 static void __init init_hwif_sl82c105(ide_hwif_t *hwif)
439 {
440         struct pci_dev *dev = hwif->pci_dev;
441         u32 val;
442         
443         DBG(("init_hwif_sl82c105(hwif: ide%d)\n", hwif->index));
444
445         hwif->tuneproc = tune_sl82c105;
446         hwif->selectproc = sl82c105_selectproc;
447         hwif->resetproc = sl82c105_resetproc;
448         
449         /* Default to PIO 0 for fallback unless tuned otherwise,
450          * we always autotune PIO, this is done before DMA is
451          * checked, so there is no risk of accidentally disabling
452          * DMA
453           */
454         hwif->drives[0].pio_speed = XFER_PIO_0;
455         hwif->drives[0].autotune = 1;
456         hwif->drives[1].pio_speed = XFER_PIO_1;
457         hwif->drives[1].autotune = 1;
458
459         pci_read_config_dword(dev, 0x40, &val);
460         *((u32 *)&hwif->hwif_data) = val;
461         
462         if (!hwif->dma_base)
463                 return;
464
465         hwif->atapi_dma = 1;
466         hwif->mwdma_mask = 0x07;
467         hwif->swdma_mask = 0x07;
468
469 #ifdef CONFIG_BLK_DEV_IDEDMA
470         hwif->ide_dma_check = &sl82c105_check_drive;
471         hwif->ide_dma_on = &sl82c105_ide_dma_on;
472         hwif->ide_dma_off_quietly = &sl82c105_ide_dma_off_quietly;
473         hwif->ide_dma_lostirq = &sl82c105_ide_dma_lost_irq;
474         hwif->ide_dma_begin = &sl82c105_ide_dma_begin;
475         hwif->ide_dma_timeout = &sl82c105_ide_dma_timeout;
476
477         if (!noautodma)
478                 hwif->autodma = 1;
479         hwif->drives[0].autodma = hwif->autodma;
480         hwif->drives[1].autodma = hwif->autodma;
481 #endif /* CONFIG_BLK_DEV_IDEDMA */
482 }
483
484 static int __devinit sl82c105_init_one(struct pci_dev *dev, const struct pci_device_id *id)
485 {
486         ide_pci_device_t *d = &sl82c105_chipsets[id->driver_data];
487         if (dev->device != d->device)
488                 BUG();
489         ide_setup_pci_device(dev, d);
490         return 0;
491 }
492
493 static struct pci_device_id sl82c105_pci_tbl[] = {
494         { PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
495         { 0, },
496 };
497 MODULE_DEVICE_TABLE(pci, sl82c105_pci_tbl);
498
499 static struct pci_driver driver = {
500         .name           = "W82C105 IDE",
501         .id_table       = sl82c105_pci_tbl,
502         .probe          = sl82c105_init_one,
503 };
504
505 static int sl82c105_ide_init(void)
506 {
507         return ide_pci_register_driver(&driver);
508 }
509
510 module_init(sl82c105_ide_init);
511
512 MODULE_DESCRIPTION("PCI driver module for W82C105 IDE");
513 MODULE_LICENSE("GPL");