2 * linux/drivers/ide/pci/sl82c105.c
4 * SL82C105/Winbond 553 IDE driver
8 * Drive tuning added from Rebel.com's kernel sources
9 * -- Russell King (15/11/98) linux@arm.linux.org.uk
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
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>
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>
35 #define DBG(arg) printk arg
40 * SL82C105 PCI config register 0x40 bits.
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)
51 * Convert a PIO mode and cycle time to the required on/off
52 * times for the interface. This has protection against run-away
55 static unsigned int get_timing_sl82c105(ide_pio_data_t *p)
60 cmd_on = (ide_pio_timings[p->pio_mode].active_time + 29) / 30;
61 cmd_off = (p->cycle_time - 30 * cmd_on + 29) / 30;
73 return (cmd_on - 1) << 8 | (cmd_off - 1) | (p->use_iordy ? 0x40 : 0x00);
77 * Configure the drive and chipset for PIO
79 static void config_for_pio(ide_drive_t *drive, int pio, int report, int chipset_only)
81 ide_hwif_t *hwif = HWIF(drive);
82 struct pci_dev *dev = hwif->pci_dev;
85 unsigned int xfer_mode, reg;
87 DBG(("config_for_pio(drive:%s, pio:%d, report:%d, chipset_only:%d)\n",
88 drive->name, pio, report, chipset_only));
90 reg = (hwif->channel ? 0x4c : 0x44) + (drive->select.b.unit ? 4 : 0);
92 pio = ide_get_best_pio_mode(drive, pio, 5, &p);
94 xfer_mode = XFER_PIO_0 + pio;
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;
100 drive->pio_speed = XFER_PIO_0;
102 if (drive->using_dma == 0) {
104 * If we are actually using MW DMA, then we can not
105 * reprogram the interface drive control register.
107 pci_write_config_word(dev, reg, drv_ctrl);
108 pci_read_config_word(dev, reg, &drv_ctrl);
111 printk("%s: selected %s (%dns) (%04X)\n", drive->name,
112 ide_xfer_verbose(xfer_mode), p.cycle_time, drv_ctrl);
118 * Configure the drive and the chipset for DMA
120 static int config_for_dma (ide_drive_t *drive)
122 ide_hwif_t *hwif = HWIF(drive);
123 struct pci_dev *dev = hwif->pci_dev;
126 DBG(("config_for_dma(drive:%s)\n", drive->name));
128 reg = (hwif->channel ? 0x4c : 0x44) + (drive->select.b.unit ? 4 : 0);
130 if (ide_config_drive_speed(drive, XFER_MW_DMA_2) != 0)
133 pci_write_config_word(dev, reg, 0x0240);
139 * Check to see if the drive and
140 * chipset is capable of DMA mode
143 static int sl82c105_check_drive (ide_drive_t *drive)
145 ide_hwif_t *hwif = HWIF(drive);
147 DBG(("sl82c105_check_drive(drive:%s)\n", drive->name));
150 struct hd_driveid *id = drive->id;
155 if (!id || !(id->capability & 1))
158 /* Consult the list of known "bad" drives */
159 if (__ide_dma_bad_drive(drive))
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);
168 if (__ide_dma_good_drive(drive))
169 return hwif->ide_dma_on(drive);
172 return hwif->ide_dma_off_quietly(drive);
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).
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.
183 static inline void sl82c105_reset_host(struct pci_dev *dev)
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));
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.
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.
199 static int sl82c105_ide_dma_lost_irq(ide_drive_t *drive)
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;
206 printk("sl82c105: lost IRQ: resetting host\n");
209 * Check the raw interrupt from the drive.
211 pci_read_config_dword(dev, 0x40, &val);
213 printk("sl82c105: drive was requesting IRQ, but host lost it\n");
216 * Was DMA enabled? If so, disable it - we're resetting the
217 * host. The IDE layer will be handling the drive for us.
219 val = hwif->INB(dma_base);
221 outb(val & ~1, dma_base);
222 printk("sl82c105: DMA was enabled\n");
225 sl82c105_reset_host(dev);
227 /* ide_dmaproc would return 1, so we do as well */
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.
236 * The generic IDE core will have disabled the BMEN bit before this
237 * function is called.
239 static void sl82c105_ide_dma_start(ide_drive_t *drive)
241 ide_hwif_t *hwif = HWIF(drive);
242 struct pci_dev *dev = hwif->pci_dev;
244 sl82c105_reset_host(dev);
245 ide_dma_start(drive);
248 static int sl82c105_ide_dma_timeout(ide_drive_t *drive)
250 ide_hwif_t *hwif = HWIF(drive);
251 struct pci_dev *dev = hwif->pci_dev;
253 DBG(("sl82c105_ide_dma_timeout(drive:%s)\n", drive->name));
255 sl82c105_reset_host(dev);
256 return __ide_dma_timeout(drive);
259 static int sl82c105_ide_dma_on (ide_drive_t *drive)
261 DBG(("sl82c105_ide_dma_on(drive:%s)\n", drive->name));
263 if (config_for_dma(drive)) {
264 config_for_pio(drive, 4, 0, 0);
265 return HWIF(drive)->ide_dma_off_quietly(drive);
267 printk(KERN_INFO "%s: DMA enabled\n", drive->name);
268 return __ide_dma_on(drive);
271 static int sl82c105_ide_dma_off_quietly (ide_drive_t *drive)
273 u8 speed = XFER_PIO_0;
276 DBG(("sl82c105_ide_dma_off_quietly(drive:%s)\n", drive->name));
278 rc = __ide_dma_off_quietly(drive);
279 if (drive->pio_speed)
280 speed = drive->pio_speed - XFER_PIO_0;
281 config_for_pio(drive, speed, 0, 1);
282 drive->current_speed = drive->pio_speed;
288 * Ok, that is nasty, but we must make sure the DMA timings
289 * won't be used for a PIO access. The solution here is
290 * to make sure the 16 bits mode is diabled on the channel
291 * when DMA is enabled, thus causing the chip to use PIO0
292 * timings for those operations.
294 static void sl82c105_selectproc(ide_drive_t *drive)
296 ide_hwif_t *hwif = HWIF(drive);
297 struct pci_dev *dev = hwif->pci_dev;
300 //DBG(("sl82c105_selectproc(drive:%s)\n", drive->name));
302 mask = hwif->channel ? CTRL_P1F16 : CTRL_P0F16;
303 old = val = *((u32 *)&hwif->hwif_data);
304 if (drive->using_dma)
309 pci_write_config_dword(dev, 0x40, val);
310 *((u32 *)&hwif->hwif_data) = val;
315 * ATA reset will clear the 16 bits mode in the control
316 * register, we need to update our cache
318 static void sl82c105_resetproc(ide_drive_t *drive)
320 ide_hwif_t *hwif = HWIF(drive);
321 struct pci_dev *dev = hwif->pci_dev;
324 DBG(("sl82c105_resetproc(drive:%s)\n", drive->name));
326 pci_read_config_dword(dev, 0x40, &val);
327 *((u32 *)&hwif->hwif_data) = val;
331 * We only deal with PIO mode here - DMA mode 'using_dma' is not
332 * initialised at the point that this function is called.
334 static void tune_sl82c105(ide_drive_t *drive, u8 pio)
336 DBG(("tune_sl82c105(drive:%s)\n", drive->name));
338 config_for_pio(drive, pio, 1, 0);
341 * We support 32-bit I/O on this interface, and it
342 * doesn't have problems with interrupts.
349 * Return the revision of the Winbond bridge
350 * which this function is part of.
352 static unsigned int sl82c105_bridge_revision(struct pci_dev *dev)
354 struct pci_dev *bridge;
358 * The bridge should be part of the same device, but function 0.
360 bridge = pci_find_slot(dev->bus->number,
361 PCI_DEVFN(PCI_SLOT(dev->devfn), 0));
366 * Make sure it is a Winbond 553 and is an ISA bridge.
368 if (bridge->vendor != PCI_VENDOR_ID_WINBOND ||
369 bridge->device != PCI_DEVICE_ID_WINBOND_83C553 ||
370 bridge->class >> 8 != PCI_CLASS_BRIDGE_ISA)
374 * We need to find function 0's revision, not function 1
376 pci_read_config_byte(bridge, PCI_REVISION_ID, &rev);
382 * Enable the PCI device
384 * --BenH: It's arch fixup code that should enable channels that
385 * have not been enabled by firmware. I decided we can still enable
386 * channel 0 here at least, but channel 1 has to be enabled by
387 * firmware or arch code. We still set both to 16 bits mode.
389 static unsigned int __devinit init_chipset_sl82c105(struct pci_dev *dev, const char *msg)
393 DBG(("init_chipset_sl82c105()\n"));
395 pci_read_config_dword(dev, 0x40, &val);
396 val |= CTRL_P0EN | CTRL_P0F16 | CTRL_P1F16;
397 pci_write_config_dword(dev, 0x40, val);
403 * Initialise the chip
406 static void __devinit init_hwif_sl82c105(ide_hwif_t *hwif)
408 struct pci_dev *dev = hwif->pci_dev;
413 DBG(("init_hwif_sl82c105(hwif: ide%d)\n", hwif->index));
415 hwif->tuneproc = tune_sl82c105;
416 hwif->selectproc = sl82c105_selectproc;
417 hwif->resetproc = sl82c105_resetproc;
419 /* Default to PIO 0 for fallback unless tuned otherwise,
420 * we always autotune PIO, this is done before DMA is
421 * checked, so there is no risk of accidentally disabling
424 hwif->drives[0].pio_speed = XFER_PIO_0;
425 hwif->drives[0].autotune = 1;
426 hwif->drives[1].pio_speed = XFER_PIO_1;
427 hwif->drives[1].autotune = 1;
429 pci_read_config_dword(dev, 0x40, &val);
430 *((u32 *)&hwif->hwif_data) = val;
433 hwif->mwdma_mask = 0;
434 hwif->swdma_mask = 0;
440 dma_state = hwif->INB(hwif->dma_base + 2) & ~0x60;
441 rev = sl82c105_bridge_revision(hwif->pci_dev);
444 * Never ever EVER under any circumstances enable
445 * DMA when the bridge is this old.
447 printk(" %s: Winbond 553 bridge revision %d, BM-DMA disabled\n",
450 #ifdef CONFIG_BLK_DEV_IDEDMA
454 hwif->mwdma_mask = 0x07;
455 hwif->swdma_mask = 0x07;
457 hwif->ide_dma_check = &sl82c105_check_drive;
458 hwif->ide_dma_on = &sl82c105_ide_dma_on;
459 hwif->ide_dma_off_quietly = &sl82c105_ide_dma_off_quietly;
460 hwif->ide_dma_lostirq = &sl82c105_ide_dma_lost_irq;
461 hwif->dma_start = &sl82c105_ide_dma_start;
462 hwif->ide_dma_timeout = &sl82c105_ide_dma_timeout;
466 hwif->drives[0].autodma = hwif->autodma;
467 hwif->drives[1].autodma = hwif->autodma;
470 hwif->serialized = hwif->mate->serialized = 1;
471 #endif /* CONFIG_BLK_DEV_IDEDMA */
473 hwif->OUTB(dma_state, hwif->dma_base + 2);
476 static ide_pci_device_t sl82c105_chipset __devinitdata = {
478 .init_chipset = init_chipset_sl82c105,
479 .init_hwif = init_hwif_sl82c105,
481 .autodma = NOAUTODMA,
482 .enablebits = {{0x40,0x01,0x01}, {0x40,0x10,0x10}},
483 .bootable = ON_BOARD,
486 static int __devinit sl82c105_init_one(struct pci_dev *dev, const struct pci_device_id *id)
488 return ide_setup_pci_device(dev, &sl82c105_chipset);
491 static struct pci_device_id sl82c105_pci_tbl[] = {
492 { PCI_VENDOR_ID_WINBOND, PCI_DEVICE_ID_WINBOND_82C105, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
495 MODULE_DEVICE_TABLE(pci, sl82c105_pci_tbl);
497 static struct pci_driver driver = {
498 .name = "W82C105_IDE",
499 .id_table = sl82c105_pci_tbl,
500 .probe = sl82c105_init_one,
503 static int sl82c105_ide_init(void)
505 return ide_pci_register_driver(&driver);
508 module_init(sl82c105_ide_init);
510 MODULE_DESCRIPTION("PCI driver module for W82C105 IDE");
511 MODULE_LICENSE("GPL");