Cleanup handling of interrupt controller (PIC).
[seabios.git] / src / floppy.c
1 // 16bit code to access floppy drives.
2 //
3 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU GPLv3 license.
7
8 #include "types.h" // u8
9 #include "disk.h" // DISK_RET_SUCCESS
10 #include "config.h" // CONFIG_FLOPPY_SUPPORT
11 #include "biosvar.h" // struct bregs
12 #include "util.h" // irq_disable
13 #include "cmos.h" // inb_cmos
14 #include "pic.h" // unmask_pic1
15
16 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
17 #define DEBUGF(fmt, args...)
18
19 #define BX_FLOPPY_ON_CNT 37   /* 2 seconds */
20
21 // New diskette parameter table adding 3 parameters from IBM
22 // Since no provisions are made for multiple drive types, most
23 // values in this table are ignored.  I set parameters for 1.44M
24 // floppy here
25 struct floppy_ext_dbt_s diskette_param_table2 VISIBLE16 = {
26     .dbt = {
27         .specify1       = 0xAF,
28         .specify2       = 0x02, // head load time 0000001, DMA used
29         .shutoff_ticks  = 0x25,
30         .bps_code       = 0x02,
31         .sectors        = 18,
32         .interblock_len = 0x1B,
33         .data_len       = 0xFF,
34         .gap_len        = 0x6C,
35         .fill_byte      = 0xF6,
36         .settle_time    = 0x0F,
37         .startup_time   = 0x08,
38     },
39     .max_track      = 79,   // maximum track
40     .data_rate      = 0,    // data transfer rate
41     .drive_type     = 4,    // drive type in cmos
42 };
43
44 void
45 floppy_drive_setup()
46 {
47     if (! CONFIG_FLOPPY_SUPPORT)
48         return;
49     dprintf(3, "init floppy drives\n");
50     u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
51     u8 out = 0;
52     u8 num_floppies = 0;
53
54     if (type & 0xf0) {
55         out |= 0x07;
56         num_floppies++;
57     }
58     if (type & 0x0f) {
59         out |= 0x70;
60         num_floppies++;
61     }
62     SET_BDA(floppy_harddisk_info, out);
63
64     // Update equipment word bits for floppy
65     if (num_floppies == 1)
66         // 1 drive, ready for boot
67         SETBITS_BDA(equipment_list_flags, 0x01);
68     else if (num_floppies == 2)
69         // 2 drives, ready for boot
70         SETBITS_BDA(equipment_list_flags, 0x41);
71
72     outb(0x02, PORT_DMA1_MASK_REG);
73
74     // Enable IRQ6 (handle_0e)
75     unmask_pic1(PIC1_IRQ6);
76 }
77
78 static inline void
79 set_diskette_current_cyl(u8 drive, u8 cyl)
80 {
81     if (drive)
82         SET_BDA(floppy_track1, cyl);
83     else
84         SET_BDA(floppy_track0, cyl);
85 }
86
87 static u16
88 get_drive_type(u8 drive)
89 {
90     // check CMOS to see if drive exists
91     u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
92     if (drive == 0)
93         drive_type >>= 4;
94     else
95         drive_type &= 0x0f;
96     return drive_type;
97 }
98
99 static u16
100 floppy_media_known(u8 drive)
101 {
102     if (!(GET_BDA(floppy_recalibration_status) & (1<<drive)))
103         return 0;
104     u8 v = GET_BDA(floppy_media_state[drive]);
105     if (!(v & FMS_MEDIA_DRIVE_ESTABLISHED))
106         return 0;
107     return 1;
108 }
109
110 static void
111 floppy_reset_controller()
112 {
113     // Reset controller
114     u8 val8 = inb(PORT_FD_DOR);
115     outb(val8 & ~0x04, PORT_FD_DOR);
116     outb(val8 | 0x04, PORT_FD_DOR);
117
118     // Wait for controller to come out of reset
119     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
120         ;
121 }
122
123 static void
124 floppy_prepare_controller(u8 drive)
125 {
126     CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
127
128     // turn on motor of selected drive, DMA & int enabled, normal operation
129     u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
130     u8 dor = 0x10;
131     if (drive)
132         dor = 0x20;
133     dor |= 0x0c;
134     dor |= drive;
135     outb(dor, PORT_FD_DOR);
136
137     // reset the disk motor timeout value of INT 08
138     SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT);
139
140     // wait for drive readiness
141     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
142         ;
143
144     if (prev_reset == 0) {
145         irq_enable();
146         u8 v;
147         for (;;) {
148             v = GET_BDA(floppy_recalibration_status);
149             if (v & FRS_TIMEOUT)
150                 break;
151             cpu_relax();
152         }
153         irq_disable();
154
155         v &= ~FRS_TIMEOUT;
156         SET_BDA(floppy_recalibration_status, v);
157     }
158 }
159
160 static u8
161 floppy_pio(u8 *cmd, u8 cmdlen)
162 {
163     floppy_prepare_controller(cmd[1] & 1);
164
165     // send command to controller
166     u8 i;
167     for (i=0; i<cmdlen; i++)
168         outb(cmd[i], PORT_FD_DATA);
169
170     irq_enable();
171     u8 v;
172     for (;;) {
173         if (!GET_BDA(floppy_motor_counter)) {
174             irq_disable();
175             floppy_reset_controller();
176             return DISK_RET_ETIMEOUT;
177         }
178         v = GET_BDA(floppy_recalibration_status);
179         if (v & FRS_TIMEOUT)
180             break;
181         cpu_relax();
182     }
183     irq_disable();
184
185     v &= ~FRS_TIMEOUT;
186     SET_BDA(floppy_recalibration_status, v);
187
188     return 0;
189 }
190
191 static u8
192 floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
193 {
194     // es:bx = pointer to where to place information from diskette
195     // port 04: DMA-1 base and current address, channel 2
196     // port 05: DMA-1 base and current count, channel 2
197     u16 page = regs->es >> 12;   // upper 4 bits
198     u16 base_es = regs->es << 4; // lower 16bits contributed by ES
199     u16 base_address = base_es + regs->bx; // lower 16 bits of address
200     // contributed by ES:BX
201     if (base_address < base_es)
202         // in case of carry, adjust page by 1
203         page++;
204
205     // check for 64K boundary overrun
206     u16 last_addr = base_address + count;
207     if (last_addr < base_address)
208         return DISK_RET_EBOUNDARY;
209
210     u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
211     if (cmd[0] == 0xe6)
212         // read
213         mode_register = 0x46;
214
215     //DEBUGF("floppy dma c2\n");
216     outb(0x06, PORT_DMA1_MASK_REG);
217     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
218     outb(base_address, PORT_DMA_ADDR_2);
219     outb(base_address>>8, PORT_DMA_ADDR_2);
220     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
221     outb(count, PORT_DMA_CNT_2);
222     outb(count>>8, PORT_DMA_CNT_2);
223
224     // port 0b: DMA-1 Mode Register
225     // transfer type=write, channel 2
226     outb(mode_register, PORT_DMA1_MODE_REG);
227
228     // port 81: DMA-1 Page Register, channel 2
229     outb(page, PORT_DMA_PAGE_2);
230
231     outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
232
233     u8 ret = floppy_pio(cmd, cmdlen);
234     if (ret)
235         return ret;
236
237     // check port 3f4 for accessibility to status bytes
238     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
239         BX_PANIC("int13_diskette: ctrl not ready\n");
240
241     // read 7 return status bytes from controller
242     u8 i;
243     for (i=0; i<7; i++) {
244         u8 v = inb(PORT_FD_DATA);
245         cmd[i] = v;
246         SET_BDA(floppy_return_status[i], v);
247     }
248
249     return 0;
250 }
251
252 static void
253 floppy_drive_recal(u8 drive)
254 {
255     // send Recalibrate command (2 bytes) to controller
256     u8 data[12];
257     data[0] = 0x07;  // 07: Recalibrate
258     data[1] = drive; // 0=drive0, 1=drive1
259     floppy_pio(data, 2);
260
261     SETBITS_BDA(floppy_recalibration_status, 1<<drive);
262     set_diskette_current_cyl(drive, 0);
263 }
264
265 static u16
266 floppy_media_sense(u8 drive)
267 {
268     u16 rv;
269     u8 config_data, media_state;
270
271     floppy_drive_recal(drive);
272
273     // for now cheat and get drive type from CMOS,
274     // assume media is same as drive type
275
276     // ** config_data **
277     // Bitfields for diskette media control:
278     // Bit(s)  Description (Table M0028)
279     //  7-6  last data rate set by controller
280     //        00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
281     //  5-4  last diskette drive step rate selected
282     //        00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
283     //  3-2  {data rate at start of operation}
284     //  1-0  reserved
285
286     // ** media_state **
287     // Bitfields for diskette drive media state:
288     // Bit(s)  Description (Table M0030)
289     //  7-6  data rate
290     //    00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
291     //  5  double stepping required (e.g. 360kB in 1.2MB)
292     //  4  media type established
293     //  3  drive capable of supporting 4MB media
294     //  2-0  on exit from BIOS, contains
295     //    000 trying 360kB in 360kB
296     //    001 trying 360kB in 1.2MB
297     //    010 trying 1.2MB in 1.2MB
298     //    011 360kB in 360kB established
299     //    100 360kB in 1.2MB established
300     //    101 1.2MB in 1.2MB established
301     //    110 reserved
302     //    111 all other formats/drives
303
304     switch (get_drive_type(drive)) {
305     case 1:
306         // 360K 5.25" drive
307         config_data = 0x00; // 0000 0000
308         media_state = 0x25; // 0010 0101
309         rv = 1;
310         break;
311     case 2:
312         // 1.2 MB 5.25" drive
313         config_data = 0x00; // 0000 0000
314         media_state = 0x25; // 0010 0101   // need double stepping??? (bit 5)
315         rv = 1;
316         break;
317     case 3:
318         // 720K 3.5" drive
319         config_data = 0x00; // 0000 0000 ???
320         media_state = 0x17; // 0001 0111
321         rv = 1;
322         break;
323     case 4:
324         // 1.44 MB 3.5" drive
325         config_data = 0x00; // 0000 0000
326         media_state = 0x17; // 0001 0111
327         rv = 1;
328         break;
329     case 5:
330         // 2.88 MB 3.5" drive
331         config_data = 0xCC; // 1100 1100
332         media_state = 0xD7; // 1101 0111
333         rv = 1;
334         break;
335     //
336     // Extended floppy size uses special cmos setting
337     case 6:
338         // 160k 5.25" drive
339         config_data = 0x00; // 0000 0000
340         media_state = 0x27; // 0010 0111
341         rv = 1;
342         break;
343     case 7:
344         // 180k 5.25" drive
345         config_data = 0x00; // 0000 0000
346         media_state = 0x27; // 0010 0111
347         rv = 1;
348         break;
349     case 8:
350         // 320k 5.25" drive
351         config_data = 0x00; // 0000 0000
352         media_state = 0x27; // 0010 0111
353         rv = 1;
354         break;
355     default:
356         // not recognized
357         config_data = 0x00; // 0000 0000
358         media_state = 0x00; // 0000 0000
359         rv = 0;
360     }
361
362     SET_BDA(floppy_last_data_rate, config_data);
363     SET_BDA(floppy_media_state[drive], media_state);
364     return rv;
365 }
366
367 #define floppy_ret(regs, code) \
368     __floppy_ret(__func__, (regs), (code))
369
370 void
371 __floppy_ret(const char *fname, struct bregs *regs, u8 code)
372 {
373     SET_BDA(floppy_last_status, code);
374     if (code)
375         __set_code_fail(fname, regs, code);
376     else
377         set_code_success(regs);
378 }
379
380 static inline void
381 floppy_fail(struct bregs *regs, u8 code)
382 {
383     floppy_ret(regs, code);
384     regs->al = 0; // no sectors read
385 }
386
387 static u16
388 check_drive(struct bregs *regs, u8 drive)
389 {
390     // see if drive exists
391     if (drive > 1 || !get_drive_type(drive)) {
392         // XXX - return type doesn't match
393         floppy_fail(regs, DISK_RET_ETIMEOUT);
394         return 1;
395     }
396
397     // see if media in drive, and type is known
398     if (floppy_media_known(drive) == 0 && floppy_media_sense(drive) == 0) {
399         floppy_fail(regs, DISK_RET_EMEDIA);
400         return 1;
401     }
402     return 0;
403 }
404
405 // diskette controller reset
406 static void
407 floppy_1300(struct bregs *regs, u8 drive)
408 {
409     if (drive > 1) {
410         floppy_ret(regs, DISK_RET_EPARAM);
411         return;
412     }
413     if (!get_drive_type(drive)) {
414         floppy_ret(regs, DISK_RET_ETIMEOUT);
415         return;
416     }
417     set_diskette_current_cyl(drive, 0); // current cylinder
418     floppy_ret(regs, DISK_RET_SUCCESS);
419 }
420
421 // Read Diskette Status
422 static void
423 floppy_1301(struct bregs *regs, u8 drive)
424 {
425     u8 v = GET_BDA(floppy_last_status);
426     regs->ah = v;
427     set_cf(regs, v);
428 }
429
430 // Read Diskette Sectors
431 static void
432 floppy_1302(struct bregs *regs, u8 drive)
433 {
434     if (check_drive(regs, drive))
435         return;
436
437     u8 num_sectors = regs->al;
438     u8 track       = regs->ch;
439     u8 sector      = regs->cl;
440     u8 head        = regs->dh;
441
442     if (head > 1 || sector == 0 || num_sectors == 0
443         || track > 79 || num_sectors > 72) {
444         floppy_fail(regs, DISK_RET_EPARAM);
445         return;
446     }
447
448     // send read-normal-data command (9 bytes) to controller
449     u8 data[12];
450     data[0] = 0xe6; // e6: read normal data
451     data[1] = (head << 2) | drive; // HD DR1 DR2
452     data[2] = track;
453     data[3] = head;
454     data[4] = sector;
455     data[5] = 2; // 512 byte sector size
456     data[6] = sector + num_sectors - 1; // last sector to read on track
457     data[7] = 0; // Gap length
458     data[8] = 0xff; // Gap length
459
460     u16 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
461     if (ret) {
462         floppy_fail(regs, ret);
463         return;
464     }
465
466     if (data[0] & 0xc0) {
467         floppy_fail(regs, DISK_RET_ECONTROLLER);
468         return;
469     }
470
471     // ??? should track be new val from return_status[3] ?
472     set_diskette_current_cyl(drive, track);
473     // AL = number of sectors read (same value as passed)
474     floppy_ret(regs, DISK_RET_SUCCESS);
475 }
476
477 // Write Diskette Sectors
478 static void
479 floppy_1303(struct bregs *regs, u8 drive)
480 {
481     if (check_drive(regs, drive))
482         return;
483
484     u8 num_sectors = regs->al;
485     u8 track       = regs->ch;
486     u8 sector      = regs->cl;
487     u8 head        = regs->dh;
488
489     if (head > 1 || sector == 0 || num_sectors == 0
490         || track > 79 || num_sectors > 72) {
491         floppy_fail(regs, DISK_RET_EPARAM);
492         return;
493     }
494
495     // send write-normal-data command (9 bytes) to controller
496     u8 data[12];
497     data[0] = 0xc5; // c5: write normal data
498     data[1] = (head << 2) | drive; // HD DR1 DR2
499     data[2] = track;
500     data[3] = head;
501     data[4] = sector;
502     data[5] = 2; // 512 byte sector size
503     data[6] = sector + num_sectors - 1; // last sector to write on track
504     data[7] = 0; // Gap length
505     data[8] = 0xff; // Gap length
506
507     u8 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
508     if (ret) {
509         floppy_fail(regs, ret);
510         return;
511     }
512
513     if (data[0] & 0xc0) {
514         if (data[1] & 0x02) {
515             set_fail(regs);
516             regs->ax = 0x0300;
517             return;
518         }
519         BX_PANIC("int13_diskette_function: read error\n");
520     }
521
522     // ??? should track be new val from return_status[3] ?
523     set_diskette_current_cyl(drive, track);
524     // AL = number of sectors read (same value as passed)
525     floppy_ret(regs, DISK_RET_SUCCESS);
526 }
527
528 // Verify Diskette Sectors
529 static void
530 floppy_1304(struct bregs *regs, u8 drive)
531 {
532     if (check_drive(regs, drive))
533         return;
534
535     u8 num_sectors = regs->al;
536     u8 track       = regs->ch;
537     u8 sector      = regs->cl;
538     u8 head        = regs->dh;
539
540     if (head > 1 || sector == 0 || num_sectors == 0
541         || track > 79 || num_sectors > 72) {
542         floppy_fail(regs, DISK_RET_EPARAM);
543         return;
544     }
545
546     // ??? should track be new val from return_status[3] ?
547     set_diskette_current_cyl(drive, track);
548     // AL = number of sectors verified (same value as passed)
549     floppy_ret(regs, DISK_RET_SUCCESS);
550 }
551
552 // format diskette track
553 static void
554 floppy_1305(struct bregs *regs, u8 drive)
555 {
556     DEBUGF("floppy f05\n");
557
558     if (check_drive(regs, drive))
559         return;
560
561     u8 num_sectors = regs->al;
562     u8 head        = regs->dh;
563
564     if (head > 1 || num_sectors == 0 || num_sectors > 18) {
565         floppy_fail(regs, DISK_RET_EPARAM);
566         return;
567     }
568
569     // send format-track command (6 bytes) to controller
570     u8 data[12];
571     data[0] = 0x4d; // 4d: format track
572     data[1] = (head << 2) | drive; // HD DR1 DR2
573     data[2] = 2; // 512 byte sector size
574     data[3] = num_sectors; // number of sectors per track
575     data[4] = 0; // Gap length
576     data[5] = 0xf6; // Fill byte
577
578     u8 ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
579     if (ret) {
580         floppy_fail(regs, ret);
581         return;
582     }
583
584     if (data[0] & 0xc0) {
585         if (data[1] & 0x02) {
586             set_fail(regs);
587             regs->ax = 0x0300;
588             return;
589         }
590         BX_PANIC("int13_diskette_function: read error\n");
591     }
592
593     set_diskette_current_cyl(drive, 0);
594     floppy_ret(regs, 0);
595 }
596
597 // read diskette drive parameters
598 static void
599 floppy_1308(struct bregs *regs, u8 drive)
600 {
601     DEBUGF("floppy f08\n");
602
603     u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
604     u8 num_floppies = 0;
605     if (drive_type & 0xf0)
606         num_floppies++;
607     if (drive_type & 0x0f)
608         num_floppies++;
609
610     if (drive > 1) {
611         regs->ax = 0;
612         regs->bx = 0;
613         regs->cx = 0;
614         regs->dx = 0;
615         regs->es = 0;
616         regs->di = 0;
617         regs->dl = num_floppies;
618         set_success(regs);
619         return;
620     }
621
622     if (drive == 0)
623         drive_type >>= 4;
624     else
625         drive_type &= 0x0f;
626
627     regs->bh = 0;
628     regs->bl = drive_type;
629     regs->ah = 0;
630     regs->al = 0;
631     regs->dl = num_floppies;
632
633     switch (drive_type) {
634     case 0: // none
635         regs->cx = 0;
636         regs->dh = 0; // max head #
637         break;
638
639     case 1: // 360KB, 5.25"
640         regs->cx = 0x2709; // 40 tracks, 9 sectors
641         regs->dh = 1; // max head #
642         break;
643
644     case 2: // 1.2MB, 5.25"
645         regs->cx = 0x4f0f; // 80 tracks, 15 sectors
646         regs->dh = 1; // max head #
647         break;
648
649     case 3: // 720KB, 3.5"
650         regs->cx = 0x4f09; // 80 tracks, 9 sectors
651         regs->dh = 1; // max head #
652         break;
653
654     case 4: // 1.44MB, 3.5"
655         regs->cx = 0x4f12; // 80 tracks, 18 sectors
656         regs->dh = 1; // max head #
657         break;
658
659     case 5: // 2.88MB, 3.5"
660         regs->cx = 0x4f24; // 80 tracks, 36 sectors
661         regs->dh = 1; // max head #
662         break;
663
664     case 6: // 160k, 5.25"
665         regs->cx = 0x2708; // 40 tracks, 8 sectors
666         regs->dh = 0; // max head #
667         break;
668
669     case 7: // 180k, 5.25"
670         regs->cx = 0x2709; // 40 tracks, 9 sectors
671         regs->dh = 0; // max head #
672         break;
673
674     case 8: // 320k, 5.25"
675         regs->cx = 0x2708; // 40 tracks, 8 sectors
676         regs->dh = 1; // max head #
677         break;
678
679     default: // ?
680         BX_PANIC("floppy: int13: bad floppy type\n");
681     }
682
683     /* set es & di to point to 11 byte diskette param table in ROM */
684     regs->es = SEG_BIOS;
685     regs->di = (u32)&diskette_param_table2;
686     /* disk status not changed upon success */
687 }
688
689 // read diskette drive type
690 static void
691 floppy_1315(struct bregs *regs, u8 drive)
692 {
693     DEBUGF("floppy f15\n");
694     if (drive > 1) {
695         set_fail(regs);
696         regs->ah = 0; // only 2 drives supported
697         // set_diskette_ret_status here ???
698         return;
699     }
700     u8 drive_type = get_drive_type(drive);
701
702     regs->ah = (drive_type != 0);
703     set_success(regs);
704 }
705
706 // get diskette change line status
707 static void
708 floppy_1316(struct bregs *regs, u8 drive)
709 {
710     DEBUGF("floppy f16\n");
711     if (drive > 1) {
712         floppy_ret(regs, DISK_RET_EPARAM);
713         return;
714     }
715     floppy_ret(regs, DISK_RET_ECHANGED);
716 }
717
718 static void
719 floppy_13XX(struct bregs *regs, u8 drive)
720 {
721     floppy_ret(regs, DISK_RET_EPARAM);
722 }
723
724 void
725 floppy_13(struct bregs *regs, u8 drive)
726 {
727     if (! CONFIG_FLOPPY_SUPPORT) {
728         // Minimal stubs
729         switch (regs->ah) {
730         case 0x01: floppy_1301(regs, drive); break;
731         default:   floppy_13XX(regs, drive); break;
732         }
733         return;
734     }
735     switch (regs->ah) {
736     case 0x00: floppy_1300(regs, drive); break;
737     case 0x01: floppy_1301(regs, drive); break;
738     case 0x02: floppy_1302(regs, drive); break;
739     case 0x03: floppy_1303(regs, drive); break;
740     case 0x04: floppy_1304(regs, drive); break;
741     case 0x05: floppy_1305(regs, drive); break;
742     case 0x08: floppy_1308(regs, drive); break;
743     case 0x15: floppy_1315(regs, drive); break;
744     case 0x16: floppy_1316(regs, drive); break;
745     default:   floppy_13XX(regs, drive); break;
746     }
747 }
748
749 // INT 0Eh Diskette Hardware ISR Entry Point
750 void VISIBLE16
751 handle_0e()
752 {
753     debug_isr(DEBUG_ISR_0e);
754     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
755         outb(0x08, PORT_FD_DATA); // sense interrupt status
756         while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
757             ;
758         do {
759             inb(PORT_FD_DATA);
760         } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
761     }
762     eoi_pic1();
763     // diskette interrupt has occurred
764     SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
765 }
766
767 // Called from int08 handler.
768 void
769 floppy_tick()
770 {
771     // time to turn off drive(s)?
772     u8 fcount = GET_BDA(floppy_motor_counter);
773     if (fcount) {
774         fcount--;
775         SET_BDA(floppy_motor_counter, fcount);
776         if (fcount == 0)
777             // turn motor(s) off
778             outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);
779     }
780 }