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