2 * linux/drivers/ide/qd65xx.c Version 0.07 Sep 30, 2001
4 * Copyright (C) 1996-2001 Linus Torvalds & author (see below)
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
14 * QDI QD6500/QD6580 EIDE controller fast support
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"
24 * Rewritten from the work of Colten Edwards <pje120@cs.usask.ca> by
25 * Samuel Thibault <samuel.thibault@fnac.net>
28 #undef REALLY_SLOW_IO /* most systems can safely undef this */
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>
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>
45 #ifdef CONFIG_BLK_DEV_QD65XX_MODULE
47 # include "ide_modes.h"
50 # include "ide_modes.h"
51 #endif /* CONFIG_BLK_DEV_QD65XX_MODULE */
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
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
70 * base+0x01: Config (R/O)
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
79 * qd6580: either 1010 or 0101
82 * base+0x02: Timer2 (qd6580 only)
85 * base+0x03: Control (qd6580 only)
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
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 ?)
101 static int timings[4]={-1,-1,-1,-1}; /* stores current timing for each timer */
103 static void qd_write_reg (u8 content, u8 reg)
107 spin_lock_irqsave(&io_request_lock, flags);
109 spin_unlock_irqrestore(&io_request_lock, flags);
112 u8 __init qd_read_reg (u8 reg)
117 spin_lock_irqsave(&io_request_lock, flags);
119 spin_unlock_irqrestore(&io_request_lock, flags);
126 * This routine is invoked from ide.c to prepare for access to a given drive.
129 static void qd_select (ide_drive_t *drive)
131 u8 index = (( (QD_TIMREG(drive)) & 0x80 ) >> 7) |
132 (QD_TIMREG(drive) & 0x02);
134 if (timings[index] != QD_TIMING(drive))
135 qd_write_reg(timings[index] = QD_TIMING(drive), QD_TIMREG(drive));
139 * qd6500_compute_timing
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
146 static u8 qd6500_compute_timing (ide_hwif_t *hwif, int active_time, int recovery_time)
148 u8 active_cycle,recovery_cycle;
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);
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);
158 return((recovery_cycle<<4) | 0x08 | active_cycle);
162 * qd6580_compute_timing
167 static u8 qd6580_compute_timing (int active_time, int recovery_time)
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);
172 return((recovery_cycle<<4) | active_cycle);
178 * tries to find timing from dos driver's table
181 static int qd_find_disk_type (ide_drive_t *drive,
182 int *active_time, int *recovery_time)
184 struct qd65xx_timing_s *p;
187 if (!*drive->id->model) return 0;
189 strncpy(model,drive->id->model,40);
190 ide_fixstring(model,40,1); /* byte-swap */
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;
206 * check whether timings don't conflict
209 static int qd_timing_ok (ide_drive_t drives[])
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 */
220 * records the timing, and enables selectproc as needed
223 static void qd_set_timing (ide_drive_t *drive, u8 timing)
225 ide_hwif_t *hwif = HWIF(drive);
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;
233 hwif->selectproc = &qd_select;
235 printk(KERN_DEBUG "%s: %#x\n", drive->name, timing);
242 static void qd6500_tune_drive (ide_drive_t *drive, u8 pio)
244 int active_time = 175;
245 int recovery_time = 415; /* worst case values from the dos driver */
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) {
251 printk(KERN_INFO "%s: PIO mode%d\n", drive->name,
254 recovery_time = drive->id->eide_pio - 120;
257 qd_set_timing(drive, qd6500_compute_timing(HWIF(drive), active_time, recovery_time));
264 static void qd6580_tune_drive (ide_drive_t *drive, u8 pio)
267 int base = HWIF(drive)->select_data;
268 int active_time = 175;
269 int recovery_time = 415; /* worst case values from the dos driver */
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);
278 if (d.cycle_time >= 110) {
280 recovery_time = d.cycle_time - 102;
282 printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
285 if (d.cycle_time >= 69) {
287 recovery_time = d.cycle_time - 61;
289 printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
292 if (d.cycle_time >= 180) {
294 recovery_time = d.cycle_time - 120;
296 active_time = ide_pio_timings[pio].active_time;
297 recovery_time = d.cycle_time
301 printk(KERN_INFO "%s: PIO mode%d\n", drive->name,pio);
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);
311 qd_set_timing(drive, qd6580_compute_timing(active_time, recovery_time));
317 * tests if the given port is a register
320 static int __init qd_testreg(int port)
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);
331 spin_unlock_irqrestore(&io_request_lock, flags);
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");
340 return (readreg != QD_TESTVAL);
346 * called to setup an ata channel : adjusts attributes & links for tuning
349 void __init qd_setup (int unit, int base, int config, unsigned int data0, unsigned int data1, void (*tuneproc) (ide_drive_t *, u8 pio))
351 ide_hwif_t *hwif = &ide_hwifs[unit];
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 */
370 * called to unsetup an ata channel : back to default values, unlinks tuning
372 void __init qd_unsetup (int unit)
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;
379 if (!(hwif->chipset == ide_qd65xx)) return;
381 printk(KERN_NOTICE "%s: back to defaults\n", hwif->name);
383 hwif->selectproc = NULL;
384 hwif->tuneproc = NULL;
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]));
394 qd_write_reg(unit?QD6580_DEF_DATA2:QD6580_DEF_DATA, QD_TIMREG(&hwif->drives[0]));
397 printk(KERN_WARNING "Unknown qd65xx tuning fonction !\n");
398 printk(KERN_WARNING "keeping settings !\n");
405 * looks at the specified baseport, and if qd found, registers & initialises it
406 * return 1 if another qd may be probed
409 int __init qd_probe (int base)
414 config = qd_read_reg(QD_CONFIG_PORT);
416 if (! ((config & QD_CONFIG_BASEPORT) >> 1 == (base == 0xb0)) )
419 unit = ! (config & QD_CONFIG_IDE_BASEPORT);
421 if ((config & 0xf0) == QD_CONFIG_QD6500) {
423 if (qd_testreg(base)) return 1; /* bad register */
427 printk(KERN_NOTICE "%s: qd6500 at %#x\n",
428 ide_hwifs[unit].name, base);
430 printk(KERN_DEBUG "qd6500: config=%#x, ID3=%u\n",
433 if (config & QD_CONFIG_DISABLED) {
434 printk(KERN_WARNING "qd6500 is disabled !\n");
438 qd_setup(unit, base, config, QD6500_DEF_DATA,
439 QD6500_DEF_DATA, &qd6500_tune_drive);
443 if (((config & 0xf0) == QD_CONFIG_QD6580_A) ||
444 ((config & 0xf0) == QD_CONFIG_QD6580_B)) {
448 if (qd_testreg(base) || qd_testreg(base+0x02)) return 1;
453 control = qd_read_reg(QD_CONTROL_PORT);
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);
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,
466 qd_write_reg(QD_DEF_CONTR,QD_CONTROL_PORT);
470 /* secondary enabled */
471 printk(KERN_INFO "%s&%s: qd6580: dual IDE board\n",
472 ide_hwifs[0].name,ide_hwifs[1].name);
474 qd_setup(0, base, config | (control << 8),
475 QD6580_DEF_DATA, QD6580_DEF_DATA,
477 qd_setup(1, base, config | (control << 8),
478 QD6580_DEF_DATA2, QD6580_DEF_DATA2,
480 qd_write_reg(QD_DEF_CONTR,QD_CONTROL_PORT);
482 return 0; /* no other qd65xx possible */
485 /* no qd65xx found */
493 * called by ide.c when parsing command line
496 void __init init_qd65xx (void)
498 if (qd_probe(0x30)) qd_probe(0xb0);
503 MODULE_AUTHOR("Samuel Thibault");
504 MODULE_DESCRIPTION("support of qd65xx vlb ide chipset");
505 MODULE_LICENSE("GPL");
507 int __init qd65xx_mod_init(void)
509 if (qd_probe(0x30)) qd_probe(0xb0);
510 if (ide_hwifs[0].chipset != ide_qd65xx &&
511 ide_hwifs[1].chipset != ide_qd65xx)
515 module_init(qd65xx_mod_init);
517 void __init qd65xx_mod_exit(void)
522 module_exit(qd65xx_mod_exit);