qd65xx.c 10.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
/*
 *  Copyright (C) 1996-2001  Linus Torvalds & author (see below)
 */

/*
 *  Version 0.03	Cleaned auto-tune, added probe
 *  Version 0.04	Added second channel tuning
 *  Version 0.05	Enhanced tuning ; added qd6500 support
 *  Version 0.06	Added dos driver's list
 *  Version 0.07	Second channel bug fix 
 *
 * QDI QD6500/QD6580 EIDE controller fast support
 *
 * To activate controller support, use "ide0=qd65xx"
 */

/*
 * Rewritten from the work of Colten Edwards <pje120@cs.usask.ca> by
 * Samuel Thibault <samuel.thibault@fnac.net>
 */

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/blkdev.h>
#include <linux/hdreg.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <asm/system.h>
#include <asm/io.h>

36 37
#define DRV_NAME "qd65xx"

L
Linus Torvalds 已提交
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include "qd65xx.h"

/*
 * I/O ports are 0x30-0x31 (and 0x32-0x33 for qd6580)
 *            or 0xb0-0xb1 (and 0xb2-0xb3 for qd6580)
 *	-- qd6500 is a single IDE interface
 *	-- qd6580 is a dual IDE interface
 *
 * More research on qd6580 being done by willmore@cig.mot.com (David)
 * More Information given by Petr Soucek (petr@ryston.cz)
 * http://www.ryston.cz/petr/vlb
 */

/*
 * base: Timer1
 *
 *
 * base+0x01: Config (R/O)
 *
 * bit 0: ide baseport: 1 = 0x1f0 ; 0 = 0x170 (only useful for qd6500)
 * bit 1: qd65xx baseport: 1 = 0xb0 ; 0 = 0x30
 * bit 2: ID3: bus speed: 1 = <=33MHz ; 0 = >33MHz
 * bit 3: qd6500: 1 = disabled, 0 = enabled
 *        qd6580: 1
 * upper nibble:
 *        qd6500: 1100
 *        qd6580: either 1010 or 0101
 *
 *
 * base+0x02: Timer2 (qd6580 only)
 *
 *
 * base+0x03: Control (qd6580 only)
 *
 * bits 0-3 must always be set 1
 * bit 4 must be set 1, but is set 0 by dos driver while measuring vlb clock
 * bit 0 : 1 = Only primary port enabled : channel 0 for hda, channel 1 for hdb
 *         0 = Primary and Secondary ports enabled : channel 0 for hda & hdb
 *                                                   channel 1 for hdc & hdd
 * bit 1 : 1 = only disks on primary port
 *         0 = disks & ATAPI devices on primary port
 * bit 2-4 : always 0
 * bit 5 : status, but of what ?
 * bit 6 : always set 1 by dos driver
 * bit 7 : set 1 for non-ATAPI devices on primary port
 *	(maybe read-ahead and post-write buffer ?)
 */

static int timings[4]={-1,-1,-1,-1}; /* stores current timing for each timer */

/*
89
 * qd65xx_select:
L
Linus Torvalds 已提交
90
 *
91
 * This routine is invoked to prepare for access to a given drive.
L
Linus Torvalds 已提交
92 93
 */

94
static void qd65xx_select(ide_drive_t *drive)
L
Linus Torvalds 已提交
95 96 97 98 99
{
	u8 index = ((	(QD_TIMREG(drive)) & 0x80 ) >> 7) |
			(QD_TIMREG(drive) & 0x02);

	if (timings[index] != QD_TIMING(drive))
100
		outb(timings[index] = QD_TIMING(drive), QD_TIMREG(drive));
L
Linus Torvalds 已提交
101 102 103 104 105 106 107 108 109 110 111 112
}

/*
 * qd6500_compute_timing
 *
 * computes the timing value where
 *	lower nibble represents active time,   in count of VLB clocks
 *	upper nibble represents recovery time, in count of VLB clocks
 */

static u8 qd6500_compute_timing (ide_hwif_t *hwif, int active_time, int recovery_time)
{
113 114
	int clk = ide_vlb_clk ? ide_vlb_clk : system_bus_clock();
	u8 act_cyc, rec_cyc;
L
Linus Torvalds 已提交
115

116 117 118
	if (clk <= 33) {
		act_cyc =  9 - IDE_IN(active_time   * clk / 1000 + 1, 2,  9);
		rec_cyc = 15 - IDE_IN(recovery_time * clk / 1000 + 1, 0, 15);
L
Linus Torvalds 已提交
119
	} else {
120 121
		act_cyc =  8 - IDE_IN(active_time   * clk / 1000 + 1, 1,  8);
		rec_cyc = 18 - IDE_IN(recovery_time * clk / 1000 + 1, 3, 18);
L
Linus Torvalds 已提交
122 123
	}

124
	return (rec_cyc << 4) | 0x08 | act_cyc;
L
Linus Torvalds 已提交
125 126 127 128 129 130 131 132 133 134
}

/*
 * qd6580_compute_timing
 *
 * idem for qd6580
 */

static u8 qd6580_compute_timing (int active_time, int recovery_time)
{
135 136
	int clk = ide_vlb_clk ? ide_vlb_clk : system_bus_clock();
	u8 act_cyc, rec_cyc;
L
Linus Torvalds 已提交
137

138 139 140 141
	act_cyc = 17 - IDE_IN(active_time   * clk / 1000 + 1, 2, 17);
	rec_cyc = 15 - IDE_IN(recovery_time * clk / 1000 + 1, 2, 15);

	return (rec_cyc << 4) | act_cyc;
L
Linus Torvalds 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

/*
 * qd_find_disk_type
 *
 * tries to find timing from dos driver's table
 */

static int qd_find_disk_type (ide_drive_t *drive,
		int *active_time, int *recovery_time)
{
	struct qd65xx_timing_s *p;
	char model[40];

	if (!*drive->id->model) return 0;

	strncpy(model,drive->id->model,40);
	ide_fixstring(model,40,1); /* byte-swap */

	for (p = qd65xx_timing ; p->offset != -1 ; p++) {
		if (!strncmp(p->model, model+p->offset, 4)) {
			printk(KERN_DEBUG "%s: listed !\n", drive->name);
			*active_time = p->active;
			*recovery_time = p->recovery;
			return 1;
		}
	}
	return 0;
}

/*
 * qd_set_timing:
 *
175
 * records the timing
L
Linus Torvalds 已提交
176 177 178 179 180 181 182 183 184 185
 */

static void qd_set_timing (ide_drive_t *drive, u8 timing)
{
	drive->drive_data &= 0xff00;
	drive->drive_data |= timing;

	printk(KERN_DEBUG "%s: %#x\n", drive->name, timing);
}

186
static void qd6500_set_pio_mode(ide_drive_t *drive, const u8 pio)
L
Linus Torvalds 已提交
187 188 189 190
{
	int active_time   = 175;
	int recovery_time = 415; /* worst case values from the dos driver */

191 192 193
	/*
	 * FIXME: use "pio" value
	 */
L
Linus Torvalds 已提交
194 195 196 197 198 199 200 201 202 203 204 205 206
	if (drive->id && !qd_find_disk_type(drive, &active_time, &recovery_time)
		&& drive->id->tPIO && (drive->id->field_valid & 0x02)
		&& drive->id->eide_pio >= 240) {

		printk(KERN_INFO "%s: PIO mode%d\n", drive->name,
				drive->id->tPIO);
		active_time = 110;
		recovery_time = drive->id->eide_pio - 120;
	}

	qd_set_timing(drive, qd6500_compute_timing(HWIF(drive), active_time, recovery_time));
}

207
static void qd6580_set_pio_mode(ide_drive_t *drive, const u8 pio)
L
Linus Torvalds 已提交
208
{
209
	ide_hwif_t *hwif = drive->hwif;
210
	unsigned int cycle_time;
L
Linus Torvalds 已提交
211 212
	int active_time   = 175;
	int recovery_time = 415; /* worst case values from the dos driver */
213
	u8 base = (hwif->config_data & 0xff00) >> 8;
L
Linus Torvalds 已提交
214 215

	if (drive->id && !qd_find_disk_type(drive, &active_time, &recovery_time)) {
216
		cycle_time = ide_pio_cycle_time(drive, pio);
L
Linus Torvalds 已提交
217 218 219 220

		switch (pio) {
			case 0: break;
			case 3:
221
				if (cycle_time >= 110) {
L
Linus Torvalds 已提交
222
					active_time = 86;
223
					recovery_time = cycle_time - 102;
L
Linus Torvalds 已提交
224 225 226 227
				} else
					printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
				break;
			case 4:
228
				if (cycle_time >= 69) {
L
Linus Torvalds 已提交
229
					active_time = 70;
230
					recovery_time = cycle_time - 61;
L
Linus Torvalds 已提交
231 232 233 234
				} else
					printk(KERN_WARNING "%s: Strange recovery time !\n",drive->name);
				break;
			default:
235
				if (cycle_time >= 180) {
L
Linus Torvalds 已提交
236
					active_time = 110;
237
					recovery_time = cycle_time - 120;
L
Linus Torvalds 已提交
238 239
				} else {
					active_time = ide_pio_timings[pio].active_time;
240
					recovery_time = cycle_time - active_time;
L
Linus Torvalds 已提交
241 242 243 244 245 246
				}
		}
		printk(KERN_INFO "%s: PIO mode%d\n", drive->name,pio);
	}

	if (!HWIF(drive)->channel && drive->media != ide_disk) {
247
		outb(0x5f, QD_CONTROL_PORT);
L
Linus Torvalds 已提交
248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
		printk(KERN_WARNING "%s: ATAPI: disabled read-ahead FIFO "
			"and post-write buffer on %s.\n",
			drive->name, HWIF(drive)->name);
	}

	qd_set_timing(drive, qd6580_compute_timing(active_time, recovery_time));
}

/*
 * qd_testreg
 *
 * tests if the given port is a register
 */

static int __init qd_testreg(int port)
{
	unsigned long flags;
265
	u8 savereg, readreg;
L
Linus Torvalds 已提交
266

267
	local_irq_save(flags);
L
Linus Torvalds 已提交
268 269 270 271
	savereg = inb_p(port);
	outb_p(QD_TESTVAL, port);	/* safe value */
	readreg = inb_p(port);
	outb(savereg, port);
272
	local_irq_restore(flags);
L
Linus Torvalds 已提交
273 274 275 276 277 278 279 280 281 282 283

	if (savereg == QD_TESTVAL) {
		printk(KERN_ERR "Outch ! the probe for qd65xx isn't reliable !\n");
		printk(KERN_ERR "Please contact maintainers to tell about your hardware\n");
		printk(KERN_ERR "Assuming qd65xx is not present.\n");
		return 1;
	}

	return (readreg != QD_TESTVAL);
}

284 285
static void __init qd6500_port_init_devs(ide_hwif_t *hwif)
{
286 287
	u8 base = (hwif->config_data & 0xff00) >> 8;
	u8 config = QD_CONFIG(hwif);
288 289 290 291 292 293 294 295

	hwif->drives[0].drive_data = QD6500_DEF_DATA;
	hwif->drives[1].drive_data = QD6500_DEF_DATA;
}

static void __init qd6580_port_init_devs(ide_hwif_t *hwif)
{
	u16 t1, t2;
296 297
	u8 base = (hwif->config_data & 0xff00) >> 8;
	u8 config = QD_CONFIG(hwif);
298

299
	if (hwif->host_flags & IDE_HFLAG_SINGLE) {
300 301 302 303 304 305 306
		t1 = QD6580_DEF_DATA;
		t2 = QD6580_DEF_DATA2;
	} else
		t2 = t1 = hwif->channel ? QD6580_DEF_DATA2 : QD6580_DEF_DATA;

	hwif->drives[0].drive_data = t1;
	hwif->drives[1].drive_data = t2;
L
Linus Torvalds 已提交
307 308
}

309 310 311 312 313 314 315 316 317 318 319 320
static const struct ide_port_ops qd6500_port_ops = {
	.port_init_devs		= qd6500_port_init_devs,
	.set_pio_mode		= qd6500_set_pio_mode,
	.selectproc		= qd65xx_select,
};

static const struct ide_port_ops qd6580_port_ops = {
	.port_init_devs		= qd6580_port_init_devs,
	.set_pio_mode		= qd6580_set_pio_mode,
	.selectproc		= qd65xx_select,
};

321
static const struct ide_port_info qd65xx_port_info __initdata = {
322
	.name			= DRV_NAME,
323 324
	.chipset		= ide_qd65xx,
	.host_flags		= IDE_HFLAG_IO_32BIT |
325
				  IDE_HFLAG_NO_DMA,
326 327 328
	.pio_mask		= ATA_PIO4,
};

L
Linus Torvalds 已提交
329 330 331 332 333 334 335 336 337
/*
 * qd_probe:
 *
 * looks at the specified baseport, and if qd found, registers & initialises it
 * return 1 if another qd may be probed
 */

static int __init qd_probe(int base)
{
338
	int rc;
339
	u8 config, unit, control;
340
	struct ide_port_info d = qd65xx_port_info;
L
Linus Torvalds 已提交
341

342
	config = inb(QD_CONFIG_PORT);
L
Linus Torvalds 已提交
343 344

	if (! ((config & QD_CONFIG_BASEPORT) >> 1 == (base == 0xb0)) )
345
		return -ENODEV;
L
Linus Torvalds 已提交
346 347 348

	unit = ! (config & QD_CONFIG_IDE_BASEPORT);

349 350 351
	if (unit)
		d.host_flags |= IDE_HFLAG_QD_2ND_PORT;

352 353
	switch (config & 0xf0) {
	case QD_CONFIG_QD6500:
354 355
		if (qd_testreg(base))
			 return -ENODEV;	/* bad register */
L
Linus Torvalds 已提交
356 357 358

		if (config & QD_CONFIG_DISABLED) {
			printk(KERN_WARNING "qd6500 is disabled !\n");
359
			return -ENODEV;
L
Linus Torvalds 已提交
360 361
		}

362 363 364 365
		printk(KERN_NOTICE "qd6500 at %#x\n", base);
		printk(KERN_DEBUG "qd6500: config=%#x, ID3=%u\n",
			config, QD_ID3);

366
		d.port_ops = &qd6500_port_ops;
367
		d.host_flags |= IDE_HFLAG_SINGLE;
368 369 370
		break;
	case QD_CONFIG_QD6580_A:
	case QD_CONFIG_QD6580_B:
371 372
		if (qd_testreg(base) || qd_testreg(base + 0x02))
			return -ENODEV;	/* bad registers */
L
Linus Torvalds 已提交
373

374
		control = inb(QD_CONTROL_PORT);
L
Linus Torvalds 已提交
375 376 377 378 379

		printk(KERN_NOTICE "qd6580 at %#x\n", base);
		printk(KERN_DEBUG "qd6580: config=%#x, control=%#x, ID3=%u\n",
			config, control, QD_ID3);

380 381
		outb(QD_DEF_CONTR, QD_CONTROL_PORT);

382
		d.port_ops = &qd6580_port_ops;
383
		if (control & QD_CONTR_SEC_DISABLED)
384 385
			d.host_flags |= IDE_HFLAG_SINGLE;

386 387 388 389 390 391
		printk(KERN_INFO "qd6580: %s IDE board\n",
			(control & QD_CONTR_SEC_DISABLED) ? "single" : "dual");
		break;
	default:
		return -ENODEV;
	}
392

393
	rc = ide_legacy_device_add(&d, (base << 8) | config);
394

395 396
	if (d.host_flags & IDE_HFLAG_SINGLE)
		return (rc == 0) ? 1 : rc;
L
Linus Torvalds 已提交
397

398
	return rc;
L
Linus Torvalds 已提交
399 400
}

401
static int probe_qd65xx;
402 403 404 405

module_param_named(probe, probe_qd65xx, bool, 0);
MODULE_PARM_DESC(probe, "probe for QD65xx chipsets");

406
static int __init qd65xx_init(void)
L
Linus Torvalds 已提交
407
{
408 409
	int rc1, rc2 = -ENODEV;

410 411 412
	if (probe_qd65xx == 0)
		return -ENODEV;

413 414 415 416 417
	rc1 = qd_probe(0x30);
	if (rc1)
		rc2 = qd_probe(0xb0);

	if (rc1 < 0 && rc2 < 0)
L
Linus Torvalds 已提交
418
		return -ENODEV;
419

L
Linus Torvalds 已提交
420 421 422 423 424 425 426 427
	return 0;
}

module_init(qd65xx_init);

MODULE_AUTHOR("Samuel Thibault");
MODULE_DESCRIPTION("support of qd65xx vlb ide chipset");
MODULE_LICENSE("GPL");