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