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