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