vserver 1.9.3
[linux-2.6.git] / drivers / ide / legacy / pdc4030.c
1 /*  -*- linux-c -*-
2  *  linux/drivers/ide/legacy/pdc4030.c          Version 0.90  May 27, 1999
3  *
4  *  Copyright (C) 1995-2002  Linus Torvalds & authors (see below)
5  */
6
7 /*
8  *  Principal Author/Maintainer:  Peter Denison <promise@pnd-pc.demon.co.uk>
9  *
10  *  This file provides support for the second port and cache of Promise
11  *  IDE interfaces, e.g. DC4030VL, DC4030VL-1 and DC4030VL-2.
12  *
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.
16  *
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.
38  *
39  *  Version 0.90        Transition to BETA code. No lost/unexpected interrupts
40  */
41
42 /*
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 
45  *
46  *      'linux ide0=dc4030' or 'linux ide1=dc4030'
47  *
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
64  *
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. 
68  *
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
72  * effects.
73  */
74
75 #define DEBUG_READ
76 #define DEBUG_WRITE
77 #define __PROMISE_4030
78
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>
85 #include <linux/mm.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>
91
92 #include <asm/io.h>
93 #include <asm/irq.h>
94
95 #include "pdc4030.h"
96
97 static ide_startstop_t promise_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block);
98
99 /*
100  * promise_selectproc() is invoked by ide.c
101  * in preparation for access to the specified drive.
102  */
103 static void promise_selectproc (ide_drive_t *drive)
104 {
105         unsigned int number;
106
107         number = (HWIF(drive)->channel << 1) + drive->select.b.unit;
108         HWIF(drive)->OUTB(number, IDE_FEATURE_REG);
109 }
110
111 /*
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.
115  */
116 int pdc4030_cmd(ide_drive_t *drive, u8 cmd)
117 {
118         unsigned long timeout;
119         u8 status_val;
120
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);
125         timeout = HZ * 10;
126         timeout += jiffies;
127         do {
128                 if(time_after(jiffies, timeout)) {
129                         return 2; /* device timed out */
130                 }
131                 /* Delays at least 10ms to give interface a chance */
132                 mdelay(10);
133                 status_val = HWIF(drive)->INB(IDE_SECTOR_REG);
134         } while (status_val != 0x50 && status_val != 0x70);
135
136         if(status_val == 0x50)
137                 return 0; /* device returned success */
138         else
139                 return 1; /* device returned failure */
140 }
141
142 /*
143  * pdc4030_identify sends a vendor-specific IDENTIFY command to the drive
144  */
145 int pdc4030_identify(ide_drive_t *drive)
146 {
147         return pdc4030_cmd(drive, PROMISE_IDENTIFY);
148 }
149
150 /*
151  * setup_pdc4030()
152  * Completes the setup of a Promise DC4030 controller card, once found.
153  */
154 int __init setup_pdc4030(ide_hwif_t *hwif)
155 {
156         ide_drive_t *drive;
157         ide_hwif_t *hwif2;
158         struct dc_ident ident;
159         int i;
160         ide_startstop_t startstop;
161         
162         if (!hwif) return 0;
163
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;
168
169         if (hwif->INB(IDE_NSECTOR_REG) == 0xFF ||
170             hwif->INB(IDE_SECTOR_REG) == 0xFF) {
171                 return 0;
172         }
173         if (IDE_CONTROL_REG)
174                 hwif->OUTB(0x08, IDE_CONTROL_REG);
175         if (pdc4030_cmd(drive,PROMISE_GET_CONFIG)) {
176                 return 0;
177         }
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;
182         }
183         hwif->ata_input_data(drive, &ident, SECTOR_WORDS);
184         if (ident.id[1] != 'P' || ident.id[0] != 'T') {
185                 return 0;
186         }
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");
201                         }
202                         return 0;
203         }
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;
209         }
210         printk("on IRQ %d\n",hwif->irq);
211
212         /*
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.
217          *
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.
221          */
222
223         hwif->chipset   = hwif2->chipset = ide_pdc4030;
224         hwif->mate      = hwif2;
225         hwif2->mate     = hwif;
226         hwif2->channel  = 1;
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;
233
234         /* Override the normal ide disk read/write. */
235         hwif->rw_disk = promise_rw_disk;
236         hwif2->rw_disk = promise_rw_disk;
237
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];
241
242 #ifdef DEBUG
243                 printk(KERN_DEBUG "pdc4030: Shifting i/f %d values to i/f %d\n",i-1,i);
244 #endif /* DEBUG */
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;
248         }
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;
262         }
263
264         probe_hwif_init(&ide_hwifs[hwif->index]);
265         probe_hwif_init(&ide_hwifs[hwif2->index]);
266
267         return 1;
268 }
269
270 /*
271  * detect_pdc4030()
272  * Tests for the presence of a DC4030 Promise card on this interface
273  * Returns: 1 if found, 0 if not found
274  */
275 int __init detect_pdc4030(ide_hwif_t *hwif)
276 {
277         ide_drive_t *drive = &hwif->drives[0];
278
279         if (IDE_DATA_REG == 0) { /* Skip test for non-existent interface */
280                 return 0;
281         }
282         hwif->OUTB(0xF3, IDE_SECTOR_REG);
283         hwif->OUTB(0x14, IDE_SELECT_REG);
284         hwif->OUTB(PROMISE_EXTENDED_COMMAND, IDE_COMMAND_REG);
285
286         msleep(50);
287
288         if (hwif->INB(IDE_ERROR_REG) == 'P' &&
289             hwif->INB(IDE_NSECTOR_REG) == 'T' &&
290             hwif->INB(IDE_SECTOR_REG) == 'I') {
291                 return 1;
292         } else {
293                 return 0;
294         }
295 }
296
297 int __init pdc4030_init(void)
298 {
299         unsigned int    index;
300         ide_hwif_t      *hwif;
301
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))
306                                 return -ENODEV;
307                         return 0;
308                 }
309         }
310         return -ENODEV;
311 }
312
313 #ifdef MODULE
314 module_init(pdc4030_init);
315 #endif
316
317 MODULE_AUTHOR("Peter Denison");
318 MODULE_DESCRIPTION("Support of Promise 4030 VLB series IDE chipsets");
319 MODULE_LICENSE("GPL");
320
321 /*
322  * promise_read_intr() is the handler for disk read/multread interrupts
323  */
324 static ide_startstop_t promise_read_intr (ide_drive_t *drive)
325 {
326         unsigned int sectors_left, sectors_avail, nsect;
327         struct request *rq = HWGROUP(drive)->rq;
328         ata_status_t status;
329
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);
333
334 read_again:
335         do {
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;
340         if (!sectors_avail)
341                 goto read_again;
342
343 read_next:
344         nsect = rq->current_nr_sectors;
345         if (nsect > sectors_avail)
346                 nsect = sectors_avail;
347         sectors_avail -= nsect;
348
349 #ifdef DEBUG_READ
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 */
356
357         HWIF(drive)->ata_input_data(drive, rq->buffer, nsect * SECTOR_WORDS);
358         rq->buffer += nsect<<9;
359         rq->sector += nsect;
360         rq->errors = 0;
361         rq->nr_sectors -= nsect;
362         if (!rq->current_nr_sectors)
363                 DRIVER(drive)->end_request(drive, 1, 0);
364
365 /*
366  * Now the data has been read in, do the following:
367  * 
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
375  */
376         if (rq->nr_sectors > 0) {
377                 if (sectors_avail)
378                         goto read_next;
379                 status.all = HWIF(drive)->INB(IDE_STATUS_REG);
380                 if (status.b.drq)
381                         goto read_again;
382                 if (status.b.bsy) {
383                         if (HWGROUP(drive)->handler != NULL)
384                                 BUG();
385                         ide_set_handler(drive,
386                                         &promise_read_intr,
387                                         WAIT_CMD,
388                                         NULL);
389 #ifdef DEBUG_READ
390                         printk(KERN_DEBUG "%s: promise_read: waiting for"
391                                "interrupt\n", drive->name);
392 #endif /* DEBUG_READ */
393                         return ide_started;
394                 }
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);
399         }
400         return ide_stopped;
401 }
402
403 /*
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
407  * has finished.
408  *
409  * Once not busy, the end request is called.
410  */
411 static ide_startstop_t promise_complete_pollfunc(ide_drive_t *drive)
412 {
413         ide_hwgroup_t *hwgroup = HWGROUP(drive);
414         struct request *rq = &hwgroup->wrq;
415         struct bio *bio = rq->bio;
416
417         if ((HWIF(drive)->INB(IDE_STATUS_REG)) & BUSY_STAT) {
418                 if (time_before(jiffies, hwgroup->poll_timeout)) {
419                         if (hwgroup->handler != NULL)
420                                 BUG();
421                         ide_set_handler(drive,
422                                         &promise_complete_pollfunc,
423                                         HZ/100,
424                                         NULL);
425                         return ide_started; /* continue polling... */
426                 }
427                 hwgroup->poll_timeout = 0;
428                 printk(KERN_ERR "%s: completion timeout - still busy!\n",
429                        drive->name);
430                 return DRIVER(drive)->error(drive, "busy timeout",
431                                 HWIF(drive)->INB(IDE_STATUS_REG));
432         }
433
434         hwgroup->poll_timeout = 0;
435 #ifdef DEBUG_WRITE
436         printk(KERN_DEBUG "%s: Write complete - end_request\n", drive->name);
437 #endif /* DEBUG_WRITE */
438
439         bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
440         rq = hwgroup->rq;
441         DRIVER(drive)->end_request(drive, 1, rq->hard_nr_sectors);
442
443         return ide_stopped;
444 }
445
446 /*
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.
449  */
450 static void promise_multwrite (ide_drive_t *drive, unsigned int mcount)
451 {
452         ide_hwgroup_t *hwgroup  = HWGROUP(drive);
453         struct request *rq      = &hwgroup->wrq;
454
455         do {
456                 char *buffer;
457                 int nsect = rq->current_nr_sectors;
458
459                 if (nsect > mcount)
460                         nsect = mcount;
461                 mcount -= nsect;
462                 buffer = rq->buffer;
463
464                 rq->sector += nsect;
465                 rq->buffer += nsect << 9;
466                 rq->nr_sectors -= nsect;
467                 rq->current_nr_sectors -= nsect;
468
469                 /* Do we move to the next bh after this? */
470                 if (!rq->current_nr_sectors) {
471                         struct bio *bio = rq->bio;
472
473                         /*
474                          * only move to next bio, when we have processed
475                          * all bvecs in this one.
476                          */
477                         if (++bio->bi_idx >= bio->bi_vcnt) {
478                                 bio->bi_idx = bio->bi_vcnt - rq->nr_cbio_segments;
479                                 bio = bio->bi_next;
480                         }
481
482                         /* end early early we ran out of requests */
483                         if (!bio) {
484                                 mcount = 0;
485                         } else {
486                                 rq->bio = bio;
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;
490                         }
491                 }
492
493                 /*
494                  * Ok, we're all setup for the interrupt
495                  * re-entering us on the last transfer.
496                  */
497                 taskfile_output_data(drive, buffer, nsect<<7);
498         } while (mcount);
499 }
500
501 /*
502  * promise_write_pollfunc() is the handler for disk write completion polling.
503  */
504 static ide_startstop_t promise_write_pollfunc (ide_drive_t *drive)
505 {
506         ide_hwgroup_t *hwgroup = HWGROUP(drive);
507         struct request *rq = &hwgroup->wrq;
508         struct bio *bio = rq->bio;
509
510         if (HWIF(drive)->INB(IDE_NSECTOR_REG) != 0) {
511                 if (time_before(jiffies, hwgroup->poll_timeout)) {
512                         if (hwgroup->handler != NULL)
513                                 BUG();
514                         ide_set_handler(drive,
515                                         &promise_write_pollfunc,
516                                         HZ/100,
517                                         NULL);
518                         return ide_started; /* continue polling... */
519                 }
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));
525         }
526
527         /*
528          * Now write out last 4 sectors and poll for not BUSY
529          */
530         promise_multwrite(drive, 4);
531         hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
532         if (hwgroup->handler != NULL)
533                 BUG();
534         ide_set_handler(drive, &promise_complete_pollfunc, HZ/100, NULL);
535 #ifdef DEBUG_WRITE
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 */
539         return ide_started;
540 }
541
542 /*
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.
548  */
549 static ide_startstop_t promise_write (ide_drive_t *drive)
550 {
551         ide_hwgroup_t *hwgroup = HWGROUP(drive);
552         struct request *rq = &hwgroup->wrq;
553
554 #ifdef DEBUG_WRITE
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 */
560
561         /*
562          * If there are more than 4 sectors to transfer, do n-4 then go into
563          * the polling strategy as defined above.
564          */
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 */
569                         BUG();
570                 ide_set_handler (drive, &promise_write_pollfunc, HZ/100, NULL);
571                 return ide_started;
572         } else {
573         /*
574          * There are 4 or fewer sectors to transfer, do them all in one go
575          * and wait for NOT BUSY.
576          */
577                 promise_multwrite(drive, rq->nr_sectors);
578                 hwgroup->poll_timeout = jiffies + WAIT_WORSTCASE;
579                 if (hwgroup->handler != NULL)
580                         BUG();
581                 ide_set_handler(drive,
582                                 &promise_complete_pollfunc,
583                                 HZ/100,
584                                 NULL);
585
586 #ifdef DEBUG_WRITE
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 */
591                 return ide_started;
592         }
593 }
594
595 /*
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.
599  */
600 ide_startstop_t do_pdc4030_io (ide_drive_t *drive, struct request *rq)
601 {
602         ide_startstop_t startstop;
603         unsigned long timeout;
604         u8 stat = 0;
605
606         if (rq_data_dir(rq) == READ) {
607                 HWIF(drive)->OUTB(PROMISE_READ, IDE_COMMAND_REG);
608 /*
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.
617  */
618                 timeout = jiffies + HZ/20; /* 50ms wait */
619                 do {
620                         stat = HWIF(drive)->INB(IDE_STATUS_REG);
621                         if (stat & DRQ_STAT) {
622                                 udelay(1);
623                                 return promise_read_intr(drive);
624                         }
625                         if (HWIF(drive)->INB(IDE_SELECT_REG) & 0x01) {
626 #ifdef DEBUG_READ
627                                 printk(KERN_DEBUG "%s: read: waiting for "
628                                                 "interrupt\n", drive->name);
629 #endif /* DEBUG_READ */
630                                 ide_set_handler(drive,
631                                                 &promise_read_intr,
632                                                 WAIT_CMD,
633                                                 NULL);
634                                 return ide_started;
635                         }
636                         udelay(1);
637                 } while (time_before(jiffies, timeout));
638
639                 printk(KERN_ERR "%s: reading: No DRQ and not "
640                                 "waiting - Odd!\n", drive->name);
641                 return ide_stopped;
642         } else {
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);
648                         return startstop;
649                 }
650                 if (!drive->unmask)
651                         local_irq_disable();
652                 HWGROUP(drive)->wrq = *rq; /* scratchpad */
653                 return promise_write(drive);
654         }
655 }
656
657 static ide_startstop_t promise_rw_disk (ide_drive_t *drive, struct request *rq, sector_t block)
658 {
659         /* The four drives on the two logical (one physical) interfaces
660            are distinguished by writing the drive number (0-3) to the
661            Feature register.
662            FIXME: Is promise_selectproc now redundant??
663         */
664         ide_hwif_t *hwif = HWIF(drive);
665         int drive_number = (hwif->channel << 1) + drive->select.b.unit;
666
667         BUG_ON(rq->nr_sectors > 127);
668
669         if (IDE_CONTROL_REG)
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);
677
678         return do_pdc4030_io(drive, rq);
679 }