* added 0.99 linux version
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / block / pdc4030.c
blob84fbb48561da9e5090641142a6a49e48d00534fd
1 /* -*- linux-c -*-
2 * linux/drivers/block/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;
162 if (!hwif) return 0;
164 drive = &hwif->drives[0];
165 hwif2 = &ide_hwifs[hwif->index+1];
166 if (hwif->chipset == ide_pdc4030) /* we've already been found ! */
167 return 1;
169 if (IN_BYTE(IDE_NSECTOR_REG) == 0xFF || IN_BYTE(IDE_SECTOR_REG) == 0xFF) {
170 return 0;
172 OUT_BYTE(0x08,IDE_CONTROL_REG);
173 if (pdc4030_cmd(drive,PROMISE_GET_CONFIG)) {
174 return 0;
176 if (ide_wait_stat(drive,DATA_READY,BAD_W_STAT,WAIT_DRQ)) {
177 printk(KERN_INFO
178 "%s: Failed Promise read config!\n",hwif->name);
179 return 0;
181 ide_input_data(drive,&ident,SECTOR_WORDS);
182 if (ident.id[1] != 'P' || ident.id[0] != 'T') {
183 return 0;
185 printk(KERN_INFO "%s: Promise caching controller, ",hwif->name);
186 switch(ident.type) {
187 case 0x43: printk("DC4030VL-2, "); break;
188 case 0x41: printk("DC4030VL-1, "); break;
189 case 0x40: printk("DC4030VL, "); break;
190 default:
191 printk("unknown - type 0x%02x - please report!\n"
192 ,ident.type);
193 printk("Please e-mail the following data to "
194 "promise@pnd-pc.demon.co.uk along with\n"
195 "a description of your card and drives:\n");
196 for (i=0; i < 0x90; i++) {
197 printk("%02x ", ((unsigned char *)&ident)[i]);
198 if ((i & 0x0f) == 0x0f) printk("\n");
200 return 0;
202 printk("%dKB cache, ",(int)ident.cache_mem);
203 switch(ident.irq) {
204 case 0x00: hwif->irq = 14; break;
205 case 0x01: hwif->irq = 12; break;
206 default: hwif->irq = 15; break;
208 printk("on IRQ %d\n",hwif->irq);
211 * Once found and identified, we set up the next hwif in the array
212 * (hwif2 = ide_hwifs[hwif->index+1]) with the same io ports, irq
213 * and other settings as the main hwif. This gives us two "mated"
214 * hwifs pointing to the Promise card.
216 * We also have to shift the default values for the remaining
217 * interfaces "up by one" to make room for the second interface on the
218 * same set of values.
221 hwif->chipset = hwif2->chipset = ide_pdc4030;
222 hwif->mate = hwif2;
223 hwif2->mate = hwif;
224 hwif2->channel = 1;
225 hwif->selectproc = hwif2->selectproc = &promise_selectproc;
226 hwif->serialized = hwif2->serialized = 1;
228 /* Shift the remaining interfaces down by one */
229 for (i=MAX_HWIFS-1 ; i > hwif->index+1 ; i--) {
230 ide_hwif_t *h = &ide_hwifs[i];
232 #ifdef DEBUG
233 printk(KERN_DEBUG "Shifting i/f %d values to i/f %d\n",i-1,i);
234 #endif
235 ide_init_hwif_ports(&h->hw, (h-1)->io_ports[IDE_DATA_OFFSET], 0, NULL);
236 memcpy(h->io_ports, h->hw.io_ports, sizeof(h->io_ports));
237 h->noprobe = (h-1)->noprobe;
239 ide_init_hwif_ports(&hwif2->hw, hwif->io_ports[IDE_DATA_OFFSET], 0, NULL);
240 memcpy(hwif2->io_ports, hwif->hw.io_ports, sizeof(hwif2->io_ports));
241 hwif2->irq = hwif->irq;
242 hwif2->hw.irq = hwif->hw.irq = hwif->irq;
243 for (i=0; i<2 ; i++) {
244 hwif->drives[i].io_32bit = 3;
245 hwif2->drives[i].io_32bit = 3;
246 hwif->drives[i].keep_settings = 1;
247 hwif2->drives[i].keep_settings = 1;
248 if (!ident.current_tm[i].cyl)
249 hwif->drives[i].noprobe = 1;
250 if (!ident.current_tm[i+2].cyl)
251 hwif2->drives[i].noprobe = 1;
253 return 1;
257 * detect_pdc4030()
258 * Tests for the presence of a DC4030 Promise card on this interface
259 * Returns: 1 if found, 0 if not found
261 int __init detect_pdc4030(ide_hwif_t *hwif)
263 ide_drive_t *drive = &hwif->drives[0];
265 if (IDE_DATA_REG == 0) { /* Skip test for non-existent interface */
266 return 0;
268 OUT_BYTE(0xF3, IDE_SECTOR_REG);
269 OUT_BYTE(0x14, IDE_SELECT_REG);
270 OUT_BYTE(PROMISE_EXTENDED_COMMAND, IDE_COMMAND_REG);
272 ide_delay_50ms();
274 if (IN_BYTE(IDE_ERROR_REG) == 'P' &&
275 IN_BYTE(IDE_NSECTOR_REG) == 'T' &&
276 IN_BYTE(IDE_SECTOR_REG) == 'I') {
277 return 1;
278 } else {
279 return 0;
283 void __init ide_probe_for_pdc4030(void)
285 unsigned int index;
286 ide_hwif_t *hwif;
288 if (enable_promise_support == 0)
289 return;
290 for (index = 0; index < MAX_HWIFS; index++) {
291 hwif = &ide_hwifs[index];
292 if (hwif->chipset == ide_unknown && detect_pdc4030(hwif)) {
293 setup_pdc4030(hwif);
301 * promise_read_intr() is the handler for disk read/multread interrupts
303 static void promise_read_intr (ide_drive_t *drive)
305 byte stat;
306 int total_remaining;
307 unsigned int sectors_left, sectors_avail, nsect;
308 struct request *rq;
310 if (!OK_STAT(stat=GET_STAT(),DATA_READY,BAD_R_STAT)) {
311 ide_error(drive, "promise_read_intr", stat);
312 return;
315 read_again:
316 do {
317 sectors_left = IN_BYTE(IDE_NSECTOR_REG);
318 IN_BYTE(IDE_SECTOR_REG);
319 } while (IN_BYTE(IDE_NSECTOR_REG) != sectors_left);
320 rq = HWGROUP(drive)->rq;
321 sectors_avail = rq->nr_sectors - sectors_left;
322 if (!sectors_avail)
323 goto read_again;
325 read_next:
326 rq = HWGROUP(drive)->rq;
327 nsect = rq->current_nr_sectors;
328 if (nsect > sectors_avail)
329 nsect = sectors_avail;
330 sectors_avail -= nsect;
331 ide_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
332 #ifdef DEBUG_READ
333 printk(KERN_DEBUG "%s: promise_read: sectors(%ld-%ld), "
334 "buf=0x%08lx, rem=%ld\n", drive->name, rq->sector,
335 rq->sector+nsect-1, (unsigned long) rq->buffer,
336 rq->nr_sectors-nsect);
337 #endif
338 rq->sector += nsect;
339 rq->buffer += nsect<<9;
340 rq->errors = 0;
341 rq->nr_sectors -= nsect;
342 total_remaining = rq->nr_sectors;
343 if ((rq->current_nr_sectors -= nsect) <= 0) {
344 ide_end_request(1, HWGROUP(drive));
347 * Now the data has been read in, do the following:
349 * if there are still sectors left in the request,
350 * if we know there are still sectors available from the interface,
351 * go back and read the next bit of the request.
352 * else if DRQ is asserted, there are more sectors available, so
353 * go back and find out how many, then read them in.
354 * else if BUSY is asserted, we are going to get an interrupt, so
355 * set the handler for the interrupt and just return
357 if (total_remaining > 0) {
358 if (sectors_avail)
359 goto read_next;
360 stat = GET_STAT();
361 if (stat & DRQ_STAT)
362 goto read_again;
363 if (stat & BUSY_STAT) {
364 ide_set_handler (drive, &promise_read_intr, WAIT_CMD);
365 #ifdef DEBUG_READ
366 printk(KERN_DEBUG "%s: promise_read: waiting for"
367 "interrupt\n", drive->name);
368 #endif
369 return;
371 printk(KERN_ERR "%s: Eeek! promise_read_intr: sectors left "
372 "!DRQ !BUSY\n", drive->name);
373 ide_error(drive, "promise read intr", stat);
378 * promise_complete_pollfunc()
379 * This is the polling function for waiting (nicely!) until drive stops
380 * being busy. It is invoked at the end of a write, after the previous poll
381 * has finished.
383 * Once not busy, the end request is called.
385 static void promise_complete_pollfunc(ide_drive_t *drive)
387 ide_hwgroup_t *hwgroup = HWGROUP(drive);
388 struct request *rq = hwgroup->rq;
389 int i;
391 if (GET_STAT() & BUSY_STAT) {
392 if (time_before(jiffies, hwgroup->poll_timeout)) {
393 ide_set_handler(drive, &promise_complete_pollfunc, 1);
394 return; /* continue polling... */
396 hwgroup->poll_timeout = 0;
397 printk(KERN_ERR "%s: completion timeout - still busy!\n",
398 drive->name);
399 ide_error(drive, "busy timeout", GET_STAT());
400 return;
403 hwgroup->poll_timeout = 0;
404 #ifdef DEBUG_WRITE
405 printk(KERN_DEBUG "%s: Write complete - end_request\n", drive->name);
406 #endif
407 for (i = rq->nr_sectors; i > 0; ) {
408 i -= rq->current_nr_sectors;
409 ide_end_request(1, hwgroup);
414 * promise_write_pollfunc() is the handler for disk write completion polling.
416 static void promise_write_pollfunc (ide_drive_t *drive)
418 ide_hwgroup_t *hwgroup = HWGROUP(drive);
420 if (IN_BYTE(IDE_NSECTOR_REG) != 0) {
421 if (time_before(jiffies, hwgroup->poll_timeout)) {
422 ide_set_handler (drive, &promise_write_pollfunc, 1);
423 return; /* continue polling... */
425 hwgroup->poll_timeout = 0;
426 printk(KERN_ERR "%s: write timed-out!\n",drive->name);
427 ide_error (drive, "write timeout", GET_STAT());
428 return;
432 * Now write out last 4 sectors and poll for not BUSY
434 ide_multwrite(drive, 4);
435 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
436 ide_set_handler(drive, &promise_complete_pollfunc, 1);
437 #ifdef DEBUG_WRITE
438 printk(KERN_DEBUG "%s: Done last 4 sectors - status = %02x\n",
439 drive->name, GET_STAT());
440 #endif
441 return;
445 * promise_write() transfers a block of one or more sectors of data to a
446 * drive as part of a disk write operation. All but 4 sectors are transfered
447 * in the first attempt, then the interface is polled (nicely!) for completion
448 * before the final 4 sectors are transfered. There is no interrupt generated
449 * on writes (at least on the DC4030VL-2), we just have to poll for NOT BUSY.
451 static void promise_write (ide_drive_t *drive)
453 ide_hwgroup_t *hwgroup = HWGROUP(drive);
454 struct request *rq = &hwgroup->wrq;
456 #ifdef DEBUG_WRITE
457 printk(KERN_DEBUG "%s: promise_write: sectors(%ld-%ld), "
458 "buffer=0x%08x\n", drive->name, rq->sector,
459 rq->sector + rq->nr_sectors - 1, (unsigned int)rq->buffer);
460 #endif
463 * If there are more than 4 sectors to transfer, do n-4 then go into
464 * the polling strategy as defined above.
466 if (rq->nr_sectors > 4) {
467 ide_multwrite(drive, rq->nr_sectors - 4);
468 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
469 ide_set_handler (drive, &promise_write_pollfunc, 1);
470 } else {
472 * There are 4 or fewer sectors to transfer, do them all in one go
473 * and wait for NOT BUSY.
475 ide_multwrite(drive, rq->nr_sectors);
476 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
477 ide_set_handler(drive, &promise_complete_pollfunc, 1);
478 #ifdef DEBUG_WRITE
479 printk(KERN_DEBUG "%s: promise_write: <= 4 sectors, "
480 "status = %02x\n", drive->name, GET_STAT());
481 #endif
486 * do_pdc4030_io() is called from do_rw_disk, having had the block number
487 * already set up. It issues a READ or WRITE command to the Promise
488 * controller, assuming LBA has been used to set up the block number.
490 void do_pdc4030_io (ide_drive_t *drive, struct request *rq)
492 unsigned long timeout;
493 byte stat;
495 if (rq->cmd == READ) {
496 OUT_BYTE(PROMISE_READ, IDE_COMMAND_REG);
498 * The card's behaviour is odd at this point. If the data is
499 * available, DRQ will be true, and no interrupt will be
500 * generated by the card. If this is the case, we need to call the
501 * "interrupt" handler (promise_read_intr) directly. Otherwise, if
502 * an interrupt is going to occur, bit0 of the SELECT register will
503 * be high, so we can set the handler the just return and be interrupted.
504 * If neither of these is the case, we wait for up to 50ms (badly I'm
505 * afraid!) until one of them is.
507 timeout = jiffies + HZ/20; /* 50ms wait */
508 do {
509 stat=GET_STAT();
510 if (stat & DRQ_STAT) {
511 udelay(1);
512 promise_read_intr(drive);
513 return;
515 if (IN_BYTE(IDE_SELECT_REG) & 0x01) {
516 #ifdef DEBUG_READ
517 printk(KERN_DEBUG "%s: read: waiting for "
518 "interrupt\n", drive->name);
519 #endif
520 ide_set_handler(drive, &promise_read_intr, WAIT_CMD);
521 return;
523 udelay(1);
524 } while (time_before(jiffies, timeout));
526 printk(KERN_ERR "%s: reading: No DRQ and not waiting - Odd!\n",
527 drive->name);
529 } else if (rq->cmd == WRITE) {
530 OUT_BYTE(PROMISE_WRITE, IDE_COMMAND_REG);
531 if (ide_wait_stat(drive, DATA_READY, drive->bad_wstat, WAIT_DRQ)) {
532 printk(KERN_ERR "%s: no DRQ after issuing "
533 "PROMISE_WRITE\n", drive->name);
534 return;
536 if (!drive->unmask)
537 __cli(); /* local CPU only */
538 HWGROUP(drive)->wrq = *rq; /* scratchpad */
539 promise_write(drive);
541 } else {
542 printk("KERN_WARNING %s: bad command: %d\n",
543 drive->name, rq->cmd);
544 ide_end_request(0, HWGROUP(drive));