1 /*
2  *  linux/drivers/ide/legacy/qd65xx.c		Version 0.07	Sep 30, 2001
3  *
4  *  Copyright (C) 1996-2001  Linus Torvalds & author (see below)
5  */
6 
7 /*
8  *  Version 0.03	Cleaned auto-tune, added probe
9  *  Version 0.04	Added second channel tuning
10  *  Version 0.05	Enhanced tuning ; added qd6500 support
11  *  Version 0.06	Added dos driver's list
12  *  Version 0.07	Second channel bug fix
13  *
14  * QDI QD6500/QD6580 EIDE controller fast support
15  *
16  * Please set local bus speed using kernel parameter idebus
17  * 	for example, "idebus=33" stands for 33Mhz VLbus
18  * To activate controller support, use "ide0=qd65xx"
19  * To enable tuning, use "ide0=autotune"
20  * To enable second channel tuning (qd6580 only), use "ide1=autotune"
21  */
22 
23 /*
24  * Rewritten from the work of Colten Edwards <pje120@cs.usask.ca> by
25  * Samuel Thibault <samuel.thibault@fnac.net>
26  */
27 
28 #undef REALLY_SLOW_IO		/* most systems can safely undef this */
29 
30 #include <linux/module.h>
31 #include <linux/config.h>
32 #include <linux/types.h>
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/timer.h>
36 #include <linux/mm.h>
37 #include <linux/ioport.h>
38 #include <linux/blkdev.h>
39 #include <linux/hdreg.h>
40 #include <linux/ide.h>
41 #include <linux/init.h>
42 #include <asm/system.h>
43 #include <asm/io.h>
44 
45 #ifdef CONFIG_BLK_DEV_QD65XX_MODULE
46 # define _IDE_C
47 # include "ide_modes.h"
48 # undef _IDE_C
49 #else
50 # include "ide_modes.h"
51 #endif /* CONFIG_BLK_DEV_QD65XX_MODULE */
52 
53 #include "qd65xx.h"
54 
55 /*
56  * I/O ports are 0x30-0x31 (and 0x32-0x33 for qd6580)
57  *            or 0xb0-0xb1 (and 0xb2-0xb3 for qd6580)
58  *	-- qd6500 is a single IDE interface
59  *	-- qd6580 is a dual IDE interface
60  *
61  * More research on qd6580 being done by willmore@cig.mot.com (David)
62  * More Information given by Petr Soucek (petr@ryston.cz)
63  * http://www.ryston.cz/petr/vlb
64  */
65 
66 /*
67  * base: Timer1
68  *
69  *
70  * base+0x01: Config (R/O)
71  *
72  * bit 0: ide baseport: 1 = 0x1f0 ; 0 = 0x170 (only useful for qd6500)
73  * bit 1: qd65xx baseport: 1 = 0xb0 ; 0 = 0x30
74  * bit 2: ID3: bus speed: 1 = <=33MHz ; 0 = >33MHz
75  * bit 3: qd6500: 1 = disabled, 0 = enabled
76  *        qd6580: 1
77  * upper nibble:
78  *        qd6500: 1100
79  *        qd6580: either 1010 or 0101
80  *
81  *
82  * base+0x02: Timer2 (qd6580 only)
83  *
84  *
85  * base+0x03: Control (qd6580 only)
86  *
87  * bits 0-3 must always be set 1
88  * bit 4 must be set 1, but is set 0 by dos driver while measuring vlb clock
89  * bit 0 : 1 = Only primary port enabled : channel 0 for hda, channel 1 for hdb
90  *         0 = Primary and Secondary ports enabled : channel 0 for hda & hdb
91  *                                                   channel 1 for hdc & hdd
92  * bit 1 : 1 = only disks on primary port
93  *         0 = disks & ATAPI devices on primary port
94  * bit 2-4 : always 0
95  * bit 5 : status, but of what ?
96  * bit 6 : always set 1 by dos driver
97  * bit 7 : set 1 for non-ATAPI devices on primary port
98  *	(maybe read-ahead and post-write buffer ?)
99  */
100 
101 static int timings[4]={-1,-1,-1,-1}; /* stores current timing for each timer */
102 
qd_write_reg(u8 content,u8 reg)103 static void qd_write_reg (u8 content, u8 reg)
104 {
105 	unsigned long flags;
106 
107 	spin_lock_irqsave(&io_request_lock, flags);
108 	outb(content,reg);
109 	spin_unlock_irqrestore(&io_request_lock, flags);
110 }
111 
qd_read_reg(u8 reg)112 u8 __init qd_read_reg (u8 reg)
113 {
114 	unsigned long flags;
115 	u8 read;
116 
117 	spin_lock_irqsave(&io_request_lock, flags);
118 	read = inb(reg);
119 	spin_unlock_irqrestore(&io_request_lock, flags);
120 	return read;
121 }
122 
123 /*
124  * qd_select:
125  *
126  * This routine is invoked from ide.c to prepare for access to a given drive.
127  */
128 
qd_select(ide_drive_t * drive)129 static void qd_select (ide_drive_t *drive)
130 {
131 	u8 index = ((	(QD_TIMREG(drive)) & 0x80 ) >> 7) |
132 			(QD_TIMREG(drive) & 0x02);
133 
134 	if (timings[index] != QD_TIMING(drive))
135 		qd_write_reg(timings[index] = QD_TIMING(drive), QD_TIMREG(drive));
136 }
137 
138 /*
139  * qd6500_compute_timing
140  *
141  * computes the timing value where
142  *	lower nibble represents active time,   in count of VLB clocks
143  *	upper nibble represents recovery time, in count of VLB clocks
144  */
145 
qd6500_compute_timing(ide_hwif_t * hwif,int active_time,int recovery_time)146 static u8 qd6500_compute_timing (ide_hwif_t *hwif, int active_time, int recovery_time)
147 {
148 	u8 active_cycle,recovery_cycle;
149 
150 	if (system_bus_clock()<=33) {
151 		active_cycle =   9  - IDE_IN(active_time   * system_bus_clock() / 1000 + 1, 2, 9);
152 		recovery_cycle = 15 - IDE_IN(recovery_time * system_bus_clock() / 1000 + 1, 0, 15);
153 	} else {
154 		active_cycle =   8  - IDE_IN(active_time   * system_bus_clock() / 1000 + 1, 1, 8);
155 		recovery_cycle = 18 - IDE_IN(recovery_time * system_bus_clock() / 1000 + 1, 3, 18);
156 	}
157 
158 	return((recovery_cycle<<4) | 0x08 | active_cycle);
159 }
160 
161 /*
162  * qd6580_compute_timing
163  *
164  * idem for qd6580
165  */
166 
qd6580_compute_timing(int active_time,int recovery_time)167 static u8 qd6580_compute_timing (int active_time, int recovery_time)
168 {
169 	u8 active_cycle   = 17 - IDE_IN(active_time   * system_bus_clock() / 1000 + 1, 2, 17);
170 	u8 recovery_cycle = 15 - IDE_IN(recovery_time * system_bus_clock() / 1000 + 1, 2, 15);
171 
172 	return((recovery_cycle<<4) | active_cycle);
173 }
174 
175 /*
176  * qd_find_disk_type
177  *
178  * tries to find timing from dos driver's table
179  */
180 
qd_find_disk_type(ide_drive_t * drive,int * active_time,int * recovery_time)181 static int qd_find_disk_type (ide_drive_t *drive,
182 		int *active_time, int *recovery_time)
183 {
184 	struct qd65xx_timing_s *p;
185 	char model[40];
186 
187 	if (!*drive->id->model) return 0;
188 
189 	strncpy(model,drive->id->model,40);
190 	ide_fixstring(model,40,1); /* byte-swap */
191 
192 	for (p = qd65xx_timing ; p->offset != -1 ; p++) {
193 		if (!strncmp(p->model, model+p->offset, 4)) {
194 			printk(KERN_DEBUG "%s: listed !\n", drive->name);
195 			*active_time = p->active;
196 			*recovery_time = p->recovery;
197 			return 1;
198 		}
199 	}
200 	return 0;
201 }
202 
203 /*
204  * qd_timing_ok:
205  *
206  * check whether timings don't conflict
207  */
208 
qd_timing_ok(ide_drive_t drives[])209 static int qd_timing_ok (ide_drive_t drives[])
210 {
211 	return (IDE_IMPLY(drives[0].present && drives[1].present,
212 			IDE_IMPLY(QD_TIMREG(drives) == QD_TIMREG(drives+1),
213 			          QD_TIMING(drives) == QD_TIMING(drives+1))));
214 	/* if same timing register, must be same timing */
215 }
216 
217 /*
218  * qd_set_timing:
219  *
220  * records the timing, and enables selectproc as needed
221  */
222 
qd_set_timing(ide_drive_t * drive,u8 timing)223 static void qd_set_timing (ide_drive_t *drive, u8 timing)
224 {
225 	ide_hwif_t *hwif = HWIF(drive);
226 
227 	drive->drive_data &= 0xff00;
228 	drive->drive_data |= timing;
229 	if (qd_timing_ok(hwif->drives)) {
230 		qd_select(drive); /* selects once */
231 		hwif->selectproc = NULL;
232 	} else
233 		hwif->selectproc = &qd_select;
234 
235 	printk(KERN_DEBUG "%s: %#x\n", drive->name, timing);
236 }
237 
238 /*
239  * qd6500_tune_drive
240  */
241 
qd6500_tune_drive(ide_drive_t * drive,u8 pio)242 static void qd6500_tune_drive (ide_drive_t *drive, u8 pio)
243 {
244 	int active_time   = 175;
245 	int recovery_time = 415; /* worst case values from the dos driver */
246 
247 	if (drive->id && !qd_find_disk_type(drive, &active_time, &recovery_time)
248 		&& drive->id->tPIO && (drive->id->field_valid & 0x02)
249 		&& drive->id->eide_pio >= 240) {
250 
251 		printk(KERN_INFO "%s: PIO mode%d\n", drive->name,
252 				drive->id->tPIO);
253 		active_time = 110;
254 		recovery_time = drive->id->eide_pio - 120;
255 	}
256 
257 	qd_set_timing(drive, qd6500_compute_timing(HWIF(drive), active_time, recovery_time));
258 }
259 
260 /*
261  * qd6580_tune_drive
262  */
263 
qd6580_tune_drive(ide_drive_t * drive,u8 pio)264 static void qd6580_tune_drive (ide_drive_t *drive, u8 pio)
265 {
266 	ide_pio_data_t d;
267 	int base = HWIF(drive)->select_data;
268 	int active_time   = 175;
269 	int recovery_time = 415; /* worst case values from the dos driver */
270 
271 	if (drive->id && !qd_find_disk_type(drive, &active_time, &recovery_time)) {
272 		pio = ide_get_best_pio_mode(drive, pio, 255, &d);
273 		pio = IDE_MIN(pio,4);
274 
275 		switch (pio) {
276 			case 0: break;
277 			case 3:
278 				if (d.cycle_time >= 110) {
279 					active_time = 86;
280 					recovery_time = d.cycle_time - 102;
281 				} else
282 					printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
283 				break;
284 			case 4:
285 				if (d.cycle_time >= 69) {
286 					active_time = 70;
287 					recovery_time = d.cycle_time - 61;
288 				} else
289 					printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
290 				break;
291 			default:
292 				if (d.cycle_time >= 180) {
293 					active_time = 110;
294 					recovery_time = d.cycle_time - 120;
295 				} else {
296 					active_time = ide_pio_timings[pio].active_time;
297 					recovery_time = d.cycle_time
298 							-active_time;
299 				}
300 		}
301 		printk(KERN_INFO "%s: PIO mode%d\n", drive->name,pio);
302 	}
303 
304 	if (!HWIF(drive)->channel && drive->media != ide_disk) {
305 		qd_write_reg(0x5f, QD_CONTROL_PORT);
306 		printk(KERN_WARNING "%s: ATAPI: disabled read-ahead FIFO "
307 			"and post-write buffer on %s.\n",
308 			drive->name, HWIF(drive)->name);
309 	}
310 
311 	qd_set_timing(drive, qd6580_compute_timing(active_time, recovery_time));
312 }
313 
314 /*
315  * qd_testreg
316  *
317  * tests if the given port is a register
318  */
319 
qd_testreg(int port)320 static int __init qd_testreg(int port)
321 {
322 	u8 savereg;
323 	u8 readreg;
324 	unsigned long flags;
325 
326 	spin_lock_irqsave(&io_request_lock, flags);
327 	savereg = inb_p(port);
328 	outb_p(QD_TESTVAL, port);	/* safe value */
329 	readreg = inb_p(port);
330 	outb(savereg, port);
331 	spin_unlock_irqrestore(&io_request_lock, flags);
332 
333 	if (savereg == QD_TESTVAL) {
334 		printk(KERN_ERR "Outch ! the probe for qd65xx isn't reliable !\n");
335 		printk(KERN_ERR "Please contact maintainers to tell about your hardware\n");
336 		printk(KERN_ERR "Assuming qd65xx is not present.\n");
337 		return 1;
338 	}
339 
340 	return (readreg != QD_TESTVAL);
341 }
342 
343 /*
344  * qd_setup:
345  *
346  * called to setup an ata channel : adjusts attributes & links for tuning
347  */
348 
qd_setup(int unit,int base,int config,unsigned int data0,unsigned int data1,void (* tuneproc)(ide_drive_t *,u8 pio))349 void __init qd_setup (int unit, int base, int config, unsigned int data0, unsigned int data1, void (*tuneproc) (ide_drive_t *, u8 pio))
350 {
351 	ide_hwif_t *hwif = &ide_hwifs[unit];
352 
353 	hwif->chipset = ide_qd65xx;
354 	hwif->channel = unit;
355 	hwif->select_data = base;
356 	hwif->config_data = config;
357 	hwif->drives[0].drive_data = data0;
358 	hwif->drives[1].drive_data = data1;
359 	hwif->drives[0].io_32bit =
360 	hwif->drives[1].io_32bit = 1;
361 	hwif->tuneproc = tuneproc;
362 #ifndef HWIF_PROBE_CLASSIC_METHOD
363 	probe_hwif_init(hwif);
364 #endif /* HWIF_PROBE_CLASSIC_METHOD */
365 }
366 
367 /*
368  * qd_unsetup:
369  *
370  * called to unsetup an ata channel : back to default values, unlinks tuning
371  */
qd_unsetup(int unit)372 void __init qd_unsetup (int unit)
373 {
374 	ide_hwif_t *hwif = &ide_hwifs[unit];
375 	u8 config = hwif->config_data;
376 	int base = hwif->select_data;
377 	void *tuneproc = (void *) hwif->tuneproc;
378 
379 	if (!(hwif->chipset == ide_qd65xx)) return;
380 
381 	printk(KERN_NOTICE "%s: back to defaults\n", hwif->name);
382 
383 	hwif->selectproc = NULL;
384 	hwif->tuneproc = NULL;
385 
386 	if (tuneproc == (void *) qd6500_tune_drive) {
387 		// will do it for both
388 		qd_write_reg(QD6500_DEF_DATA, QD_TIMREG(&hwif->drives[0]));
389 	} else if (tuneproc == (void *) qd6580_tune_drive) {
390 		if (QD_CONTROL(hwif) & QD_CONTR_SEC_DISABLED) {
391 			qd_write_reg(QD6580_DEF_DATA, QD_TIMREG(&hwif->drives[0]));
392 			qd_write_reg(QD6580_DEF_DATA2, QD_TIMREG(&hwif->drives[1]));
393 		} else {
394 			qd_write_reg(unit?QD6580_DEF_DATA2:QD6580_DEF_DATA, QD_TIMREG(&hwif->drives[0]));
395 		}
396 	} else {
397 		printk(KERN_WARNING "Unknown qd65xx tuning fonction !\n");
398 		printk(KERN_WARNING "keeping settings !\n");
399 	}
400 }
401 
402 /*
403  * qd_probe:
404  *
405  * looks at the specified baseport, and if qd found, registers & initialises it
406  * return 1 if another qd may be probed
407  */
408 
qd_probe(int base)409 int __init qd_probe (int base)
410 {
411 	u8 config;
412 	u8 unit;
413 
414 	config = qd_read_reg(QD_CONFIG_PORT);
415 
416 	if (! ((config & QD_CONFIG_BASEPORT) >> 1 == (base == 0xb0)) )
417 		return 1;
418 
419 	unit = ! (config & QD_CONFIG_IDE_BASEPORT);
420 
421 	if ((config & 0xf0) == QD_CONFIG_QD6500) {
422 
423 		if (qd_testreg(base)) return 1;		/* bad register */
424 
425 		/* qd6500 found */
426 
427 		printk(KERN_NOTICE "%s: qd6500 at %#x\n",
428 			ide_hwifs[unit].name, base);
429 
430 		printk(KERN_DEBUG "qd6500: config=%#x, ID3=%u\n",
431 			config, QD_ID3);
432 
433 		if (config & QD_CONFIG_DISABLED) {
434 			printk(KERN_WARNING "qd6500 is disabled !\n");
435 			return 1;
436 		}
437 
438 		qd_setup(unit, base, config, QD6500_DEF_DATA,
439 			QD6500_DEF_DATA, &qd6500_tune_drive);
440 		return 1;
441 	}
442 
443 	if (((config & 0xf0) == QD_CONFIG_QD6580_A) ||
444 	    ((config & 0xf0) == QD_CONFIG_QD6580_B)) {
445 
446 		u8 control;
447 
448 		if (qd_testreg(base) || qd_testreg(base+0x02)) return 1;
449 			/* bad registers */
450 
451 		/* qd6580 found */
452 
453 		control = qd_read_reg(QD_CONTROL_PORT);
454 
455 		printk(KERN_NOTICE "qd6580 at %#x\n", base);
456 		printk(KERN_DEBUG "qd6580: config=%#x, control=%#x, ID3=%u\n",
457 			config, control, QD_ID3);
458 
459 		if (control & QD_CONTR_SEC_DISABLED) {
460 			/* secondary disabled */
461 			printk(KERN_INFO "%s: qd6580: single IDE board\n",
462 					ide_hwifs[unit].name);
463 			qd_setup(unit, base, config | (control << 8),
464 				QD6580_DEF_DATA, QD6580_DEF_DATA2,
465 				&qd6580_tune_drive);
466 			qd_write_reg(QD_DEF_CONTR,QD_CONTROL_PORT);
467 
468 			return 1;
469 		} else {
470 			/* secondary enabled */
471 			printk(KERN_INFO "%s&%s: qd6580: dual IDE board\n",
472 					ide_hwifs[0].name,ide_hwifs[1].name);
473 
474 			qd_setup(0, base, config | (control << 8),
475 				QD6580_DEF_DATA, QD6580_DEF_DATA,
476 				&qd6580_tune_drive);
477 			qd_setup(1, base, config | (control << 8),
478 				QD6580_DEF_DATA2, QD6580_DEF_DATA2,
479 				&qd6580_tune_drive);
480 			qd_write_reg(QD_DEF_CONTR,QD_CONTROL_PORT);
481 
482 			return 0; /* no other qd65xx possible */
483 		}
484 	}
485 	/* no qd65xx found */
486 	return 1;
487 }
488 
489 #ifndef MODULE
490 /*
491  * init_qd65xx:
492  *
493  * called by ide.c when parsing command line
494  */
495 
init_qd65xx(void)496 void __init init_qd65xx (void)
497 {
498 	if (qd_probe(0x30)) qd_probe(0xb0);
499 }
500 
501 #else
502 
503 MODULE_AUTHOR("Samuel Thibault");
504 MODULE_DESCRIPTION("support of qd65xx vlb ide chipset");
505 MODULE_LICENSE("GPL");
506 
qd65xx_mod_init(void)507 int __init qd65xx_mod_init(void)
508 {
509 	if (qd_probe(0x30)) qd_probe(0xb0);
510 	if (ide_hwifs[0].chipset != ide_qd65xx &&
511 	    ide_hwifs[1].chipset != ide_qd65xx)
512 		return -ENODEV;
513 	return 0;
514 }
515 module_init(qd65xx_mod_init);
516 
qd65xx_mod_exit(void)517 void __init qd65xx_mod_exit(void)
518 {
519 	qd_unsetup(0);
520 	qd_unsetup(1);
521 }
522 module_exit(qd65xx_mod_exit);
523 #endif
524