2 * linux/drivers/ide/legacy/pdc4030.c Version 0.90 May 27, 1999
4 * Copyright (C) 1995-2002 Linus Torvalds & authors (see below)
8 * Principal Author/Maintainer: Peter Denison <promise@pnd-pc.demon.co.uk>
10 * This file provides support for the second port and cache of Promise
11 * IDE interfaces, e.g. DC4030VL, DC4030VL-1 and DC4030VL-2.
13 * Thanks are due to Mark Lord for advice and patiently answering stupid
14 * questions, and all those mugs^H^H^H^Hbrave souls who've tested this,
15 * especially Andre Hedrick.
17 * Version 0.01 Initial version, #include'd in ide.c rather than
18 * compiled separately.
19 * Reads use Promise commands, writes as before. Drives
20 * on second channel are read-only.
21 * Version 0.02 Writes working on second channel, reads on both
22 * channels. Writes fail under high load. Suspect
23 * transfers of >127 sectors don't work.
24 * Version 0.03 Brought into line with ide.c version 5.27.
25 * Other minor changes.
26 * Version 0.04 Updated for ide.c version 5.30
27 * Changed initialization strategy
28 * Version 0.05 Kernel integration. -ml
29 * Version 0.06 Ooops. Add hwgroup to direct call of ide_intr() -ml
30 * Version 0.07 Added support for DC4030 variants
31 * Secondary interface autodetection
32 * Version 0.08 Renamed to pdc4030.c
33 * Version 0.09 Obsolete - never released - did manual write request
34 * splitting before max_sectors[major][minor] available.
35 * Version 0.10 Updated for 2.1 series of kernels
36 * Version 0.11 Updated for 2.3 series of kernels
37 * Autodetection code added.
39 * Version 0.90 Transition to BETA code. No lost/unexpected interrupts
43 * Once you've compiled it in, you'll have to also enable the interface
44 * setup routine from the kernel command line, as in
46 * 'linux ide0=dc4030' or 'linux ide1=dc4030'
48 * It should now work as a second controller also ('ide1=dc4030') but only
49 * if you DON'T have BIOS V4.44, which has a bug. If you have this version
50 * and EPROM programming facilities, you need to fix 4 bytes:
65 * As of January 1999, Promise Technology Inc. have finally supplied me with
66 * some technical information which has shed a glimmer of light on some of the
67 * problems I was having, especially with writes.
69 * There are still potential problems with the robustness and efficiency of
70 * this driver because I still don't understand what the card is doing with
71 * interrupts, however, it has been stable for a while with no reports of ill
77 #define __PROMISE_4030
79 #include <linux/module.h>
80 #include <linux/config.h>
81 #include <linux/types.h>
82 #include <linux/kernel.h>
83 #include <linux/delay.h>
84 #include <linux/timer.h>
86 #include <linux/ioport.h>
87 #include <linux/blkdev.h>
88 #include <linux/hdreg.h>
89 #include <linux/ide.h>
90 #include <linux/init.h>
97 static ide_startstop_t promise_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block);
100 * promise_selectproc() is invoked by ide.c
101 * in preparation for access to the specified drive.
103 static void promise_selectproc (ide_drive_t *drive)
107 number = (HWIF(drive)->channel << 1) + drive->select.b.unit;
108 HWIF(drive)->OUTB(number, IDE_FEATURE_REG);
112 * pdc4030_cmd handles the set of vendor specific commands that are initiated
113 * by command F0. They all have the same success/failure notification -
114 * 'P' (=0x50) on success, 'p' (=0x70) on failure.
116 int pdc4030_cmd(ide_drive_t *drive, u8 cmd)
118 unsigned long timeout;
121 promise_selectproc(drive); /* redundant? */
122 HWIF(drive)->OUTB(0xF3, IDE_SECTOR_REG);
123 HWIF(drive)->OUTB(cmd, IDE_SELECT_REG);
124 HWIF(drive)->OUTB(PROMISE_EXTENDED_COMMAND, IDE_COMMAND_REG);
128 if(time_after(jiffies, timeout)) {
129 return 2; /* device timed out */
131 /* Delays at least 10ms to give interface a chance */
133 status_val = HWIF(drive)->INB(IDE_SECTOR_REG);
134 } while (status_val != 0x50 && status_val != 0x70);
136 if(status_val == 0x50)
137 return 0; /* device returned success */
139 return 1; /* device returned failure */
143 * pdc4030_identify sends a vendor-specific IDENTIFY command to the drive
145 int pdc4030_identify(ide_drive_t *drive)
147 return pdc4030_cmd(drive, PROMISE_IDENTIFY);
152 * Completes the setup of a Promise DC4030 controller card, once found.
154 int __init setup_pdc4030(ide_hwif_t *hwif)
158 struct dc_ident ident;
160 ide_startstop_t startstop;
164 drive = &hwif->drives[0];
165 hwif2 = &ide_hwifs[hwif->index+1];
166 if (hwif->chipset == ide_pdc4030) /* we've already been found ! */
169 if (hwif->INB(IDE_NSECTOR_REG) == 0xFF ||
170 hwif->INB(IDE_SECTOR_REG) == 0xFF) {
174 hwif->OUTB(0x08, IDE_CONTROL_REG);
175 if (pdc4030_cmd(drive,PROMISE_GET_CONFIG)) {
178 if (ide_wait_stat(&startstop, drive,DATA_READY,BAD_W_STAT,WAIT_DRQ)) {
180 "%s: Failed Promise read config!\n",hwif->name);
183 hwif->ata_input_data(drive, &ident, SECTOR_WORDS);
184 if (ident.id[1] != 'P' || ident.id[0] != 'T') {
187 printk(KERN_INFO "%s: Promise caching controller, ",hwif->name);
189 case 0x43: printk("DC4030VL-2, "); break;
190 case 0x41: printk("DC4030VL-1, "); break;
191 case 0x40: printk("DC4030VL, "); break;
193 printk("unknown - type 0x%02x - please report!\n"
195 printk("Please e-mail the following data to "
196 "promise@pnd-pc.demon.co.uk along with\n"
197 "a description of your card and drives:\n");
198 for (i=0; i < 0x90; i++) {
199 printk("%02x ", ((unsigned char *)&ident)[i]);
200 if ((i & 0x0f) == 0x0f) printk("\n");
204 printk("%dKB cache, ",(int)ident.cache_mem);
206 case 0x00: hwif->irq = 14; break;
207 case 0x01: hwif->irq = 12; break;
208 default: hwif->irq = 15; break;
210 printk("on IRQ %d\n",hwif->irq);
213 * Once found and identified, we set up the next hwif in the array
214 * (hwif2 = ide_hwifs[hwif->index+1]) with the same io ports, irq
215 * and other settings as the main hwif. This gives us two "mated"
216 * hwifs pointing to the Promise card.
218 * We also have to shift the default values for the remaining
219 * interfaces "up by one" to make room for the second interface on the
220 * same set of values.
223 hwif->chipset = hwif2->chipset = ide_pdc4030;
227 hwif->rqsize = hwif2->rqsize = 127;
228 hwif->no_lba48 = hwif2->no_lba48 = 1;
229 hwif->selectproc = hwif2->selectproc = &promise_selectproc;
230 hwif->serialized = hwif2->serialized = 1;
231 /* DC4030 hosted drives need their own identify... */
232 hwif->identify = hwif2->identify = &pdc4030_identify;
234 /* Override the normal ide disk read/write. */
235 hwif->rw_disk = promise_rw_disk;
236 hwif2->rw_disk = promise_rw_disk;
238 /* Shift the remaining interfaces up by one */
239 for (i=MAX_HWIFS-1 ; i > hwif->index+1 ; i--) {
240 ide_hwif_t *h = &ide_hwifs[i];
243 printk(KERN_DEBUG "pdc4030: Shifting i/f %d values to i/f %d\n",i-1,i);
245 ide_init_hwif_ports(&h->hw, (h-1)->io_ports[IDE_DATA_OFFSET], 0, NULL);
246 memcpy(h->io_ports, h->hw.io_ports, sizeof(h->io_ports));
247 h->noprobe = (h-1)->noprobe;
249 ide_init_hwif_ports(&hwif2->hw, hwif->io_ports[IDE_DATA_OFFSET], 0, NULL);
250 memcpy(hwif2->io_ports, hwif->hw.io_ports, sizeof(hwif2->io_ports));
251 hwif2->irq = hwif->irq;
252 hwif2->hw.irq = hwif->hw.irq = hwif->irq;
253 for (i=0; i<2 ; i++) {
254 hwif->drives[i].io_32bit = 3;
255 hwif2->drives[i].io_32bit = 3;
256 hwif->drives[i].keep_settings = 1;
257 hwif2->drives[i].keep_settings = 1;
258 if (!ident.current_tm[i].cyl)
259 hwif->drives[i].noprobe = 1;
260 if (!ident.current_tm[i+2].cyl)
261 hwif2->drives[i].noprobe = 1;
264 probe_hwif_init(&ide_hwifs[hwif->index]);
265 probe_hwif_init(&ide_hwifs[hwif2->index]);
272 * Tests for the presence of a DC4030 Promise card on this interface
273 * Returns: 1 if found, 0 if not found
275 int __init detect_pdc4030(ide_hwif_t *hwif)
277 ide_drive_t *drive = &hwif->drives[0];
279 if (IDE_DATA_REG == 0) { /* Skip test for non-existent interface */
282 hwif->OUTB(0xF3, IDE_SECTOR_REG);
283 hwif->OUTB(0x14, IDE_SELECT_REG);
284 hwif->OUTB(PROMISE_EXTENDED_COMMAND, IDE_COMMAND_REG);
288 if (hwif->INB(IDE_ERROR_REG) == 'P' &&
289 hwif->INB(IDE_NSECTOR_REG) == 'T' &&
290 hwif->INB(IDE_SECTOR_REG) == 'I') {
297 int __init pdc4030_init(void)
302 for (index = 0; index < MAX_HWIFS; index++) {
303 hwif = &ide_hwifs[index];
304 if (hwif->chipset == ide_unknown && detect_pdc4030(hwif)) {
305 if (!setup_pdc4030(hwif))
314 module_init(pdc4030_init);
317 MODULE_AUTHOR("Peter Denison");
318 MODULE_DESCRIPTION("Support of Promise 4030 VLB series IDE chipsets");
319 MODULE_LICENSE("GPL");
322 * promise_read_intr() is the handler for disk read/multread interrupts
324 static ide_startstop_t promise_read_intr (ide_drive_t *drive)
326 unsigned int sectors_left, sectors_avail, nsect;
327 struct request *rq = HWGROUP(drive)->rq;
330 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
331 if (!OK_STAT(status.all, DATA_READY, BAD_R_STAT))
332 return DRIVER(drive)->error(drive, __FUNCTION__, status.all);
336 sectors_left = HWIF(drive)->INB(IDE_NSECTOR_REG);
337 HWIF(drive)->INB(IDE_SECTOR_REG);
338 } while (HWIF(drive)->INB(IDE_NSECTOR_REG) != sectors_left);
339 sectors_avail = rq->nr_sectors - sectors_left;
344 nsect = rq->current_nr_sectors;
345 if (nsect > sectors_avail)
346 nsect = sectors_avail;
347 sectors_avail -= nsect;
350 printk(KERN_DEBUG "%s: %s: sectors(%lu-%lu), rem=%lu\n",
351 drive->name, __FUNCTION__,
352 (unsigned long)rq->sector,
353 (unsigned long)rq->sector + nsect - 1,
354 (unsigned long)rq->nr_sectors - nsect);
355 #endif /* DEBUG_READ */
357 HWIF(drive)->ata_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
358 rq->buffer += nsect<<9;
361 rq->nr_sectors -= nsect;
362 if (!rq->current_nr_sectors)
363 DRIVER(drive)->end_request(drive, 1, 0);
366 * Now the data has been read in, do the following:
368 * if there are still sectors left in the request,
369 * if we know there are still sectors available from the interface,
370 * go back and read the next bit of the request.
371 * else if DRQ is asserted, there are more sectors available, so
372 * go back and find out how many, then read them in.
373 * else if BUSY is asserted, we are going to get an interrupt, so
374 * set the handler for the interrupt and just return
376 if (rq->nr_sectors > 0) {
379 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
383 if (HWGROUP(drive)->handler != NULL)
385 ide_set_handler(drive,
390 printk(KERN_DEBUG "%s: promise_read: waiting for"
391 "interrupt\n", drive->name);
392 #endif /* DEBUG_READ */
395 printk(KERN_ERR "%s: Eeek! promise_read_intr: sectors left "
396 "!DRQ !BUSY\n", drive->name);
397 return DRIVER(drive)->error(drive,
398 "promise read intr", status.all);
404 * promise_complete_pollfunc()
405 * This is the polling function for waiting (nicely!) until drive stops
406 * being busy. It is invoked at the end of a write, after the previous poll
409 * Once not busy, the end request is called.
411 static ide_startstop_t promise_complete_pollfunc(ide_drive_t *drive)
413 ide_hwgroup_t *hwgroup = HWGROUP(drive);
414 struct request *rq = &hwgroup->wrq;
415 struct bio *bio = rq->bio;
417 if ((HWIF(drive)->INB(IDE_STATUS_REG)) & BUSY_STAT) {
418 if (time_before(jiffies, hwgroup->poll_timeout)) {
419 if (hwgroup->handler != NULL)
421 ide_set_handler(drive,
422 &promise_complete_pollfunc,
425 return ide_started; /* continue polling... */
427 hwgroup->poll_timeout = 0;
428 printk(KERN_ERR "%s: completion timeout - still busy!\n",
430 return DRIVER(drive)->error(drive, "busy timeout",
431 HWIF(drive)->INB(IDE_STATUS_REG));
434 hwgroup->poll_timeout = 0;
436 printk(KERN_DEBUG "%s: Write complete - end_request\n", drive->name);
437 #endif /* DEBUG_WRITE */
439 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
441 DRIVER(drive)->end_request(drive, 1, rq->hard_nr_sectors);
447 * promise_multwrite() transfers a block of up to mcount sectors of data
448 * to a drive as part of a disk multiple-sector write operation.
450 static void promise_multwrite (ide_drive_t *drive, unsigned int mcount)
452 ide_hwgroup_t *hwgroup = HWGROUP(drive);
453 struct request *rq = &hwgroup->wrq;
457 int nsect = rq->current_nr_sectors;
465 rq->buffer += nsect << 9;
466 rq->nr_sectors -= nsect;
467 rq->current_nr_sectors -= nsect;
469 /* Do we move to the next bh after this? */
470 if (!rq->current_nr_sectors) {
471 struct bio *bio = rq->bio;
474 * only move to next bio, when we have processed
475 * all bvecs in this one.
477 if (++bio->bi_idx >= bio->bi_vcnt) {
478 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
482 /* end early early we ran out of requests */
487 rq->nr_cbio_segments = bio_segments(bio);
488 rq->current_nr_sectors = bio_cur_sectors(bio);
489 rq->hard_cur_sectors = rq->current_nr_sectors;
494 * Ok, we're all setup for the interrupt
495 * re-entering us on the last transfer.
497 taskfile_output_data(drive, buffer, nsect<<7);
502 * promise_write_pollfunc() is the handler for disk write completion polling.
504 static ide_startstop_t promise_write_pollfunc (ide_drive_t *drive)
506 ide_hwgroup_t *hwgroup = HWGROUP(drive);
507 struct request *rq = &hwgroup->wrq;
508 struct bio *bio = rq->bio;
510 if (HWIF(drive)->INB(IDE_NSECTOR_REG) != 0) {
511 if (time_before(jiffies, hwgroup->poll_timeout)) {
512 if (hwgroup->handler != NULL)
514 ide_set_handler(drive,
515 &promise_write_pollfunc,
518 return ide_started; /* continue polling... */
520 hwgroup->poll_timeout = 0;
521 printk(KERN_ERR "%s: write timed-out!\n",drive->name);
522 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
523 return DRIVER(drive)->error(drive, "write timeout",
524 HWIF(drive)->INB(IDE_STATUS_REG));
528 * Now write out last 4 sectors and poll for not BUSY
530 promise_multwrite(drive, 4);
531 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
532 if (hwgroup->handler != NULL)
534 ide_set_handler(drive, &promise_complete_pollfunc, HZ/100, NULL);
536 printk(KERN_DEBUG "%s: Done last 4 sectors - status = %02x\n",
537 drive->name, HWIF(drive)->INB(IDE_STATUS_REG));
538 #endif /* DEBUG_WRITE */
543 * promise_write() transfers a block of one or more sectors of data to a
544 * drive as part of a disk write operation. All but 4 sectors are transferred
545 * in the first attempt, then the interface is polled (nicely!) for completion
546 * before the final 4 sectors are transferred. There is no interrupt generated
547 * on writes (at least on the DC4030VL-2), we just have to poll for NOT BUSY.
549 static ide_startstop_t promise_write (ide_drive_t *drive)
551 ide_hwgroup_t *hwgroup = HWGROUP(drive);
552 struct request *rq = &hwgroup->wrq;
555 printk(KERN_DEBUG "%s: %s: sectors(%lu-%lu)\n",
556 drive->name, __FUNCTION__,
557 (unsigned long)rq->sector,
558 (unsigned long)rq->sector + rq->nr_sectors - 1);
559 #endif /* DEBUG_WRITE */
562 * If there are more than 4 sectors to transfer, do n-4 then go into
563 * the polling strategy as defined above.
565 if (rq->nr_sectors > 4) {
566 promise_multwrite(drive, rq->nr_sectors - 4);
567 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
568 if (hwgroup->handler != NULL) /* paranoia check */
570 ide_set_handler (drive, &promise_write_pollfunc, HZ/100, NULL);
574 * There are 4 or fewer sectors to transfer, do them all in one go
575 * and wait for NOT BUSY.
577 promise_multwrite(drive, rq->nr_sectors);
578 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
579 if (hwgroup->handler != NULL)
581 ide_set_handler(drive,
582 &promise_complete_pollfunc,
587 printk(KERN_DEBUG "%s: promise_write: <= 4 sectors, "
588 "status = %02x\n", drive->name,
589 HWIF(drive)->INB(IDE_STATUS_REG));
590 #endif /* DEBUG_WRITE */
596 * do_pdc4030_io() is called from promise_rw_disk, having had the block number
597 * already set up. It issues a READ or WRITE command to the Promise
598 * controller, assuming LBA has been used to set up the block number.
600 ide_startstop_t do_pdc4030_io (ide_drive_t *drive, struct request *rq)
602 ide_startstop_t startstop;
603 unsigned long timeout;
606 if (rq_data_dir(rq) == READ) {
607 HWIF(drive)->OUTB(PROMISE_READ, IDE_COMMAND_REG);
609 * The card's behaviour is odd at this point. If the data is
610 * available, DRQ will be true, and no interrupt will be
611 * generated by the card. If this is the case, we need to call the
612 * "interrupt" handler (promise_read_intr) directly. Otherwise, if
613 * an interrupt is going to occur, bit0 of the SELECT register will
614 * be high, so we can set the handler the just return and be interrupted.
615 * If neither of these is the case, we wait for up to 50ms (badly I'm
616 * afraid!) until one of them is.
618 timeout = jiffies + HZ/20; /* 50ms wait */
620 stat = HWIF(drive)->INB(IDE_STATUS_REG);
621 if (stat & DRQ_STAT) {
623 return promise_read_intr(drive);
625 if (HWIF(drive)->INB(IDE_SELECT_REG) & 0x01) {
627 printk(KERN_DEBUG "%s: read: waiting for "
628 "interrupt\n", drive->name);
629 #endif /* DEBUG_READ */
630 ide_set_handler(drive,
637 } while (time_before(jiffies, timeout));
639 printk(KERN_ERR "%s: reading: No DRQ and not "
640 "waiting - Odd!\n", drive->name);
643 HWIF(drive)->OUTB(PROMISE_WRITE, IDE_COMMAND_REG);
644 if (ide_wait_stat(&startstop, drive, DATA_READY,
645 drive->bad_wstat, WAIT_DRQ)) {
646 printk(KERN_ERR "%s: no DRQ after issuing "
647 "PROMISE_WRITE\n", drive->name);
652 HWGROUP(drive)->wrq = *rq; /* scratchpad */
653 return promise_write(drive);
657 static ide_startstop_t promise_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
659 /* The four drives on the two logical (one physical) interfaces
660 are distinguished by writing the drive number (0-3) to the
662 FIXME: Is promise_selectproc now redundant??
664 ide_hwif_t *hwif = HWIF(drive);
665 int drive_number = (hwif->channel << 1) + drive->select.b.unit;
667 BUG_ON(rq->nr_sectors > 127);
670 hwif->OUTB(drive->ctl, IDE_CONTROL_REG);
671 hwif->OUTB(drive_number, IDE_FEATURE_REG);
672 hwif->OUTB(rq->nr_sectors, IDE_NSECTOR_REG);
673 hwif->OUTB(block,IDE_SECTOR_REG);
674 hwif->OUTB(block>>=8,IDE_LCYL_REG);
675 hwif->OUTB(block>>=8,IDE_HCYL_REG);
676 hwif->OUTB(((block>>8)&0x0f)|drive->select.all,IDE_SELECT_REG);
678 return do_pdc4030_io(drive, rq);