Import 2.3.48
[davej-history.git] / drivers / block / pdc4030.c
blobf42c4946f6dd54063195f41632fb310ee5bc769d
1 /* -*- linux-c -*-
2 * linux/drivers/ide/pdc4030.c Version 0.90 May 27, 1999
4 * Copyright (C) 1995-1999 Linus Torvalds & authors (see below)
5 */
7 /*
8 * Principal Author/Maintainer: peterd@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:
51 * 2496: 81 81
52 * 2497: 3E 3E
53 * 2498: 22 98 *
54 * 2499: 06 05 *
55 * 249A: F0 F0
56 * 249B: 01 01
57 * ...
58 * 24A7: 81 81
59 * 24A8: 3E 3E
60 * 24A9: 22 98 *
61 * 24AA: 06 05 *
62 * 24AB: 70 70
63 * 24AC: 01 01
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 problems with the robustness and efficiency of this driver
70 * because I still don't understand what the card is doing with interrupts.
73 #define DEBUG_READ
74 #define DEBUG_WRITE
76 #include <linux/types.h>
77 #include <linux/kernel.h>
78 #include <linux/delay.h>
79 #include <linux/timer.h>
80 #include <linux/mm.h>
81 #include <linux/ioport.h>
82 #include <linux/blkdev.h>
83 #include <linux/hdreg.h>
84 #include <linux/ide.h>
86 #include <asm/io.h>
87 #include <asm/irq.h>
89 #include "pdc4030.h"
92 * promise_selectproc() is invoked by ide.c
93 * in preparation for access to the specified drive.
95 static void promise_selectproc (ide_drive_t *drive)
97 unsigned int number;
99 number = (HWIF(drive)->channel << 1) + drive->select.b.unit;
100 OUT_BYTE(number,IDE_FEATURE_REG);
104 * pdc4030_cmd handles the set of vendor specific commands that are initiated
105 * by command F0. They all have the same success/failure notification -
106 * 'P' (=0x50) on success, 'p' (=0x70) on failure.
108 int pdc4030_cmd(ide_drive_t *drive, byte cmd)
110 unsigned long timeout, timer;
111 byte status_val;
113 promise_selectproc(drive); /* redundant? */
114 OUT_BYTE(0xF3,IDE_SECTOR_REG);
115 OUT_BYTE(cmd,IDE_SELECT_REG);
116 OUT_BYTE(PROMISE_EXTENDED_COMMAND,IDE_COMMAND_REG);
117 timeout = HZ * 10;
118 timeout += jiffies;
119 do {
120 if(time_after(jiffies, timeout)) {
121 return 2; /* device timed out */
123 /* This is out of delay_10ms() */
124 /* Delays at least 10ms to give interface a chance */
125 timer = jiffies + (HZ + 99)/100 + 1;
126 while (time_after(timer, jiffies));
127 status_val = IN_BYTE(IDE_SECTOR_REG);
128 } while (status_val != 0x50 && status_val != 0x70);
130 if(status_val == 0x50)
131 return 0; /* device returned success */
132 else
133 return 1; /* device returned failure */
137 * pdc4030_identify sends a vendor-specific IDENTIFY command to the drive
139 int pdc4030_identify(ide_drive_t *drive)
141 return pdc4030_cmd(drive, PROMISE_IDENTIFY);
144 int enable_promise_support = 0;
146 void __init init_pdc4030 (void)
148 enable_promise_support = 1;
152 * setup_pdc4030()
153 * Completes the setup of a Promise DC4030 controller card, once found.
155 int __init setup_pdc4030 (ide_hwif_t *hwif)
157 ide_drive_t *drive;
158 ide_hwif_t *hwif2;
159 struct dc_ident ident;
160 int i;
161 ide_startstop_t startstop;
163 if (!hwif) return 0;
165 drive = &hwif->drives[0];
166 hwif2 = &ide_hwifs[hwif->index+1];
167 if (hwif->chipset == ide_pdc4030) /* we've already been found ! */
168 return 1;
170 if (IN_BYTE(IDE_NSECTOR_REG) == 0xFF || IN_BYTE(IDE_SECTOR_REG) == 0xFF) {
171 return 0;
173 if (IDE_CONTROL_REG)
174 OUT_BYTE(0x08,IDE_CONTROL_REG);
175 if (pdc4030_cmd(drive,PROMISE_GET_CONFIG)) {
176 return 0;
178 if (ide_wait_stat(&startstop, drive,DATA_READY,BAD_W_STAT,WAIT_DRQ)) {
179 printk(KERN_INFO
180 "%s: Failed Promise read config!\n",hwif->name);
181 return 0;
183 ide_input_data(drive,&ident,SECTOR_WORDS);
184 if (ident.id[1] != 'P' || ident.id[0] != 'T') {
185 return 0;
187 printk(KERN_INFO "%s: Promise caching controller, ",hwif->name);
188 switch(ident.type) {
189 case 0x43: printk("DC4030VL-2, "); break;
190 case 0x41: printk("DC4030VL-1, "); break;
191 case 0x40: printk("DC4030VL, "); break;
192 default:
193 printk("unknown - type 0x%02x - please report!\n"
194 ,ident.type);
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");
202 return 0;
204 printk("%dKB cache, ",(int)ident.cache_mem);
205 switch(ident.irq) {
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;
224 hwif->mate = hwif2;
225 hwif2->mate = hwif;
226 hwif2->channel = 1;
227 hwif->selectproc = hwif2->selectproc = &promise_selectproc;
228 hwif->serialized = hwif2->serialized = 1;
230 /* Shift the remaining interfaces down by one */
231 for (i=MAX_HWIFS-1 ; i > hwif->index+1 ; i--) {
232 ide_hwif_t *h = &ide_hwifs[i];
234 #ifdef DEBUG
235 printk(KERN_DEBUG "Shifting i/f %d values to i/f %d\n",i-1,i);
236 #endif
237 ide_init_hwif_ports(&h->hw, (h-1)->io_ports[IDE_DATA_OFFSET], 0, NULL);
238 memcpy(h->io_ports, h->hw.io_ports, sizeof(h->io_ports));
239 h->noprobe = (h-1)->noprobe;
241 ide_init_hwif_ports(&hwif2->hw, hwif->io_ports[IDE_DATA_OFFSET], 0, NULL);
242 memcpy(hwif2->io_ports, hwif->hw.io_ports, sizeof(hwif2->io_ports));
243 hwif2->irq = hwif->irq;
244 hwif2->hw.irq = hwif->hw.irq = hwif->irq;
245 for (i=0; i<2 ; i++) {
246 hwif->drives[i].io_32bit = 3;
247 hwif2->drives[i].io_32bit = 3;
248 hwif->drives[i].keep_settings = 1;
249 hwif2->drives[i].keep_settings = 1;
250 if (!ident.current_tm[i].cyl)
251 hwif->drives[i].noprobe = 1;
252 if (!ident.current_tm[i+2].cyl)
253 hwif2->drives[i].noprobe = 1;
255 return 1;
259 * detect_pdc4030()
260 * Tests for the presence of a DC4030 Promise card on this interface
261 * Returns: 1 if found, 0 if not found
263 int __init detect_pdc4030(ide_hwif_t *hwif)
265 ide_drive_t *drive = &hwif->drives[0];
267 if (IDE_DATA_REG == 0) { /* Skip test for non-existent interface */
268 return 0;
270 OUT_BYTE(0xF3, IDE_SECTOR_REG);
271 OUT_BYTE(0x14, IDE_SELECT_REG);
272 OUT_BYTE(PROMISE_EXTENDED_COMMAND, IDE_COMMAND_REG);
274 ide_delay_50ms();
276 if (IN_BYTE(IDE_ERROR_REG) == 'P' &&
277 IN_BYTE(IDE_NSECTOR_REG) == 'T' &&
278 IN_BYTE(IDE_SECTOR_REG) == 'I') {
279 return 1;
280 } else {
281 return 0;
285 void __init ide_probe_for_pdc4030(void)
287 unsigned int index;
288 ide_hwif_t *hwif;
290 if (enable_promise_support == 0)
291 return;
292 for (index = 0; index < MAX_HWIFS; index++) {
293 hwif = &ide_hwifs[index];
294 if (hwif->chipset == ide_unknown && detect_pdc4030(hwif)) {
295 setup_pdc4030(hwif);
303 * promise_read_intr() is the handler for disk read/multread interrupts
305 static ide_startstop_t promise_read_intr (ide_drive_t *drive)
307 byte stat;
308 int total_remaining;
309 unsigned int sectors_left, sectors_avail, nsect;
310 struct request *rq;
312 if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
313 return ide_error(drive, "promise_read_intr", stat);
316 read_again:
317 do {
318 sectors_left = IN_BYTE(IDE_NSECTOR_REG);
319 IN_BYTE(IDE_SECTOR_REG);
320 } while (IN_BYTE(IDE_NSECTOR_REG) != sectors_left);
321 rq = HWGROUP(drive)->rq;
322 sectors_avail = rq->nr_sectors - sectors_left;
323 if (!sectors_avail)
324 goto read_again;
326 read_next:
327 rq = HWGROUP(drive)->rq;
328 nsect = rq->current_nr_sectors;
329 if (nsect > sectors_avail)
330 nsect = sectors_avail;
331 sectors_avail -= nsect;
332 ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
333 #ifdef DEBUG_READ
334 printk(KERN_DEBUG "%s: promise_read: sectors(%ld-%ld), "
335 "buf=0x%08lx, rem=%ld\n", drive->name, rq->sector,
336 rq->sector+nsect-1, (unsigned long) rq->buffer,
337 rq->nr_sectors-nsect);
338 #endif
339 rq->sector += nsect;
340 rq->buffer += nsect<<9;
341 rq->errors = 0;
342 rq->nr_sectors -= nsect;
343 total_remaining = rq->nr_sectors;
344 if ((rq->current_nr_sectors -= nsect) <= 0) {
345 ide_end_request(1, HWGROUP(drive));
348 * Now the data has been read in, do the following:
350 * if there are still sectors left in the request,
351 * if we know there are still sectors available from the interface,
352 * go back and read the next bit of the request.
353 * else if DRQ is asserted, there are more sectors available, so
354 * go back and find out how many, then read them in.
355 * else if BUSY is asserted, we are going to get an interrupt, so
356 * set the handler for the interrupt and just return
358 if (total_remaining > 0) {
359 if (sectors_avail)
360 goto read_next;
361 stat = GET_STAT();
362 if (stat & DRQ_STAT)
363 goto read_again;
364 if (stat & BUSY_STAT) {
365 ide_set_handler (drive, &promise_read_intr, WAIT_CMD, NULL);
366 #ifdef DEBUG_READ
367 printk(KERN_DEBUG "%s: promise_read: waiting for"
368 "interrupt\n", drive->name);
369 #endif
370 return ide_started;
372 printk(KERN_ERR "%s: Eeek! promise_read_intr: sectors left "
373 "!DRQ !BUSY\n", drive->name);
374 return ide_error(drive, "promise read intr", stat);
376 return ide_stopped;
380 * promise_complete_pollfunc()
381 * This is the polling function for waiting (nicely!) until drive stops
382 * being busy. It is invoked at the end of a write, after the previous poll
383 * has finished.
385 * Once not busy, the end request is called.
387 static ide_startstop_t promise_complete_pollfunc(ide_drive_t *drive)
389 ide_hwgroup_t *hwgroup = HWGROUP(drive);
390 struct request *rq = hwgroup->rq;
391 int i;
393 if (GET_STAT() & BUSY_STAT) {
394 if (time_before(jiffies, hwgroup->poll_timeout)) {
395 ide_set_handler(drive, &promise_complete_pollfunc, HZ/100, NULL);
396 return ide_started; /* continue polling... */
398 hwgroup->poll_timeout = 0;
399 printk(KERN_ERR "%s: completion timeout - still busy!\n",
400 drive->name);
401 return ide_error(drive, "busy timeout", GET_STAT());
404 hwgroup->poll_timeout = 0;
405 #ifdef DEBUG_WRITE
406 printk(KERN_DEBUG "%s: Write complete - end_request\n", drive->name);
407 #endif
408 for (i = rq->nr_sectors; i > 0; ) {
409 i -= rq->current_nr_sectors;
410 ide_end_request(1, hwgroup);
412 return ide_stopped;
416 * promise_write_pollfunc() is the handler for disk write completion polling.
418 static ide_startstop_t promise_write_pollfunc (ide_drive_t *drive)
420 ide_hwgroup_t *hwgroup = HWGROUP(drive);
422 if (IN_BYTE(IDE_NSECTOR_REG) != 0) {
423 if (time_before(jiffies, hwgroup->poll_timeout)) {
424 ide_set_handler (drive, &promise_write_pollfunc, HZ/100, NULL);
425 return ide_started; /* continue polling... */
427 hwgroup->poll_timeout = 0;
428 printk(KERN_ERR "%s: write timed-out!\n",drive->name);
429 return ide_error (drive, "write timeout", GET_STAT());
433 * Now write out last 4 sectors and poll for not BUSY
435 ide_multwrite(drive, 4);
436 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
437 ide_set_handler(drive, &promise_complete_pollfunc, HZ/100, NULL);
438 #ifdef DEBUG_WRITE
439 printk(KERN_DEBUG "%s: Done last 4 sectors - status = %02x\n",
440 drive->name, GET_STAT());
441 #endif
442 return ide_started;
446 * promise_write() transfers a block of one or more sectors of data to a
447 * drive as part of a disk write operation. All but 4 sectors are transfered
448 * in the first attempt, then the interface is polled (nicely!) for completion
449 * before the final 4 sectors are transfered. There is no interrupt generated
450 * on writes (at least on the DC4030VL-2), we just have to poll for NOT BUSY.
452 static ide_startstop_t promise_write (ide_drive_t *drive)
454 ide_hwgroup_t *hwgroup = HWGROUP(drive);
455 struct request *rq = &hwgroup->wrq;
457 #ifdef DEBUG_WRITE
458 printk(KERN_DEBUG "%s: promise_write: sectors(%ld-%ld), "
459 "buffer=0x%08x\n", drive->name, rq->sector,
460 rq->sector + rq->nr_sectors - 1, (unsigned int)rq->buffer);
461 #endif
464 * If there are more than 4 sectors to transfer, do n-4 then go into
465 * the polling strategy as defined above.
467 if (rq->nr_sectors > 4) {
468 if (ide_multwrite(drive, rq->nr_sectors - 4))
469 return ide_stopped;
470 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
471 ide_set_handler (drive, &promise_write_pollfunc, HZ/100, NULL);
472 return ide_started;
473 } else {
475 * There are 4 or fewer sectors to transfer, do them all in one go
476 * and wait for NOT BUSY.
478 if (ide_multwrite(drive, rq->nr_sectors))
479 return ide_stopped;
480 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
481 ide_set_handler(drive, &promise_complete_pollfunc, HZ/100, NULL);
482 #ifdef DEBUG_WRITE
483 printk(KERN_DEBUG "%s: promise_write: <= 4 sectors, "
484 "status = %02x\n", drive->name, GET_STAT());
485 #endif
486 return ide_started;
491 * do_pdc4030_io() is called from do_rw_disk, having had the block number
492 * already set up. It issues a READ or WRITE command to the Promise
493 * controller, assuming LBA has been used to set up the block number.
495 ide_startstop_t do_pdc4030_io (ide_drive_t *drive, struct request *rq)
497 unsigned long timeout;
498 byte stat;
500 if (rq->cmd == READ) {
501 OUT_BYTE(PROMISE_READ, IDE_COMMAND_REG);
503 * The card's behaviour is odd at this point. If the data is
504 * available, DRQ will be true, and no interrupt will be
505 * generated by the card. If this is the case, we need to call the
506 * "interrupt" handler (promise_read_intr) directly. Otherwise, if
507 * an interrupt is going to occur, bit0 of the SELECT register will
508 * be high, so we can set the handler the just return and be interrupted.
509 * If neither of these is the case, we wait for up to 50ms (badly I'm
510 * afraid!) until one of them is.
512 timeout = jiffies + HZ/20; /* 50ms wait */
513 do {
514 stat=GET_STAT();
515 if (stat & DRQ_STAT) {
516 udelay(1);
517 return promise_read_intr(drive);
519 if (IN_BYTE(IDE_SELECT_REG) & 0x01) {
520 #ifdef DEBUG_READ
521 printk(KERN_DEBUG "%s: read: waiting for "
522 "interrupt\n", drive->name);
523 #endif
524 ide_set_handler(drive, &promise_read_intr, WAIT_CMD, NULL);
525 return ide_started;
527 udelay(1);
528 } while (time_before(jiffies, timeout));
530 printk(KERN_ERR "%s: reading: No DRQ and not waiting - Odd!\n",
531 drive->name);
532 return ide_stopped;
533 } else if (rq->cmd == WRITE) {
534 ide_startstop_t startstop;
535 OUT_BYTE(PROMISE_WRITE, IDE_COMMAND_REG);
536 if (ide_wait_stat(&startstop, drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
537 printk(KERN_ERR "%s: no DRQ after issuing "
538 "PROMISE_WRITE\n", drive->name);
539 return startstop;
541 if (!drive->unmask)
542 __cli(); /* local CPU only */
543 HWGROUP(drive)->wrq = *rq; /* scratchpad */
544 return promise_write(drive);
546 } else {
547 printk("KERN_WARNING %s: bad command: %d\n",
548 drive->name, rq->cmd);
549 ide_end_request(0, HWGROUP(drive));
550 return ide_stopped;