1 // 16bit code to access floppy drives.
3 // Copyright (C) 2008 Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002 MandrakeSoft S.A.
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
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" // eoi_pic1
15 #include "bregs.h" // struct bregs
17 #define BX_FLOPPY_ON_CNT 37 /* 2 seconds */
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
23 struct floppy_ext_dbt_s diskette_param_table2 VAR16_32 = {
26 .specify2 = 0x02, // head load time 0000001, DMA used
27 .shutoff_ticks = 0x25,
30 .interblock_len = 0x1B,
37 .max_track = 79, // maximum track
38 .data_rate = 0, // data transfer rate
39 .drive_type = 4, // drive type in cmos
42 // Since no provisions are made for multiple drive types, most
43 // values in this table are ignored. I set parameters for 1.44M
45 struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7) = {
47 .specify2 = 0x02, // head load time 0000001, DMA used
48 .shutoff_ticks = 0x25,
51 .interblock_len = 0x1B,
62 if (! CONFIG_FLOPPY_SUPPORT)
64 dprintf(3, "init floppy drives\n");
66 // XXX - disable floppies on coreboot for now.
67 outb_cmos(0, CMOS_FLOPPY_DRIVE_TYPE);
68 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
80 SET_BDA(floppy_harddisk_info, out);
82 // Update equipment word bits for floppy
83 if (num_floppies == 1)
84 // 1 drive, ready for boot
85 SETBITS_BDA(equipment_list_flags, 0x01);
86 else if (num_floppies == 2)
87 // 2 drives, ready for boot
88 SETBITS_BDA(equipment_list_flags, 0x41);
90 outb(0x02, PORT_DMA1_MASK_REG);
92 enable_hwirq(6, entry_0e);
96 set_diskette_current_cyl(u8 drive, u8 cyl)
99 SET_BDA(floppy_track1, cyl);
101 SET_BDA(floppy_track0, cyl);
105 get_drive_type(u8 drive)
107 // check CMOS to see if drive exists
108 u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
117 floppy_media_known(u8 drive)
119 if (!(GET_BDA(floppy_recalibration_status) & (1<<drive)))
121 u8 v = GET_BDA(floppy_media_state[drive]);
122 if (!(v & FMS_MEDIA_DRIVE_ESTABLISHED))
128 floppy_reset_controller()
131 u8 val8 = inb(PORT_FD_DOR);
132 outb(val8 & ~0x04, PORT_FD_DOR);
133 outb(val8 | 0x04, PORT_FD_DOR);
135 // Wait for controller to come out of reset
136 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
141 floppy_prepare_controller(u8 drive)
143 CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
145 // turn on motor of selected drive, DMA & int enabled, normal operation
146 u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
152 outb(dor, PORT_FD_DOR);
154 // reset the disk motor timeout value of INT 08
155 SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT);
157 // wait for drive readiness
158 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
161 if (prev_reset == 0) {
165 v = GET_BDA(floppy_recalibration_status);
173 SET_BDA(floppy_recalibration_status, v);
178 floppy_pio(u8 *cmd, u8 cmdlen)
180 floppy_prepare_controller(cmd[1] & 1);
182 // send command to controller
184 for (i=0; i<cmdlen; i++)
185 outb(cmd[i], PORT_FD_DATA);
190 if (!GET_BDA(floppy_motor_counter)) {
192 floppy_reset_controller();
195 v = GET_BDA(floppy_recalibration_status);
203 SET_BDA(floppy_recalibration_status, v);
208 #define floppy_ret(regs, code) \
209 __floppy_ret((regs), (code) | (__LINE__ << 8), __func__)
212 __floppy_ret(struct bregs *regs, u32 linecode, const char *fname)
215 SET_BDA(floppy_last_status, code);
217 __set_code_fail(regs, linecode, fname);
219 set_code_success(regs);
223 floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
225 // es:bx = pointer to where to place information from diskette
226 u32 addr = (u32)MAKE_FARPTR(regs->es, regs->bx);
228 // check for 64K boundary overrun
229 u32 last_addr = addr + count;
230 if ((addr >> 16) != (last_addr >> 16)) {
231 floppy_ret(regs, DISK_RET_EBOUNDARY);
235 u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
238 mode_register = 0x46;
240 //DEBUGF("floppy dma c2\n");
241 outb(0x06, PORT_DMA1_MASK_REG);
242 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
243 outb(addr, PORT_DMA_ADDR_2);
244 outb(addr>>8, PORT_DMA_ADDR_2);
245 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
246 outb(count, PORT_DMA_CNT_2);
247 outb(count>>8, PORT_DMA_CNT_2);
249 // port 0b: DMA-1 Mode Register
250 // transfer type=write, channel 2
251 outb(mode_register, PORT_DMA1_MODE_REG);
253 // port 81: DMA-1 Page Register, channel 2
254 outb(addr>>16, PORT_DMA_PAGE_2);
256 outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
258 int ret = floppy_pio(cmd, cmdlen);
260 floppy_ret(regs, DISK_RET_ETIMEOUT);
264 // check port 3f4 for accessibility to status bytes
265 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
266 BX_PANIC("int13_diskette: ctrl not ready\n");
268 // read 7 return status bytes from controller
270 for (i=0; i<7; i++) {
271 u8 v = inb(PORT_FD_DATA);
273 SET_BDA(floppy_return_status[i], v);
280 floppy_drive_recal(u8 drive)
282 // send Recalibrate command (2 bytes) to controller
284 data[0] = 0x07; // 07: Recalibrate
285 data[1] = drive; // 0=drive0, 1=drive1
288 SETBITS_BDA(floppy_recalibration_status, 1<<drive);
289 set_diskette_current_cyl(drive, 0);
293 floppy_media_sense(u8 drive)
296 u8 config_data, media_state;
298 floppy_drive_recal(drive);
300 // for now cheat and get drive type from CMOS,
301 // assume media is same as drive type
304 // Bitfields for diskette media control:
305 // Bit(s) Description (Table M0028)
306 // 7-6 last data rate set by controller
307 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
308 // 5-4 last diskette drive step rate selected
309 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
310 // 3-2 {data rate at start of operation}
314 // Bitfields for diskette drive media state:
315 // Bit(s) Description (Table M0030)
317 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
318 // 5 double stepping required (e.g. 360kB in 1.2MB)
319 // 4 media type established
320 // 3 drive capable of supporting 4MB media
321 // 2-0 on exit from BIOS, contains
322 // 000 trying 360kB in 360kB
323 // 001 trying 360kB in 1.2MB
324 // 010 trying 1.2MB in 1.2MB
325 // 011 360kB in 360kB established
326 // 100 360kB in 1.2MB established
327 // 101 1.2MB in 1.2MB established
329 // 111 all other formats/drives
331 switch (get_drive_type(drive)) {
334 config_data = 0x00; // 0000 0000
335 media_state = 0x25; // 0010 0101
339 // 1.2 MB 5.25" drive
340 config_data = 0x00; // 0000 0000
341 media_state = 0x25; // 0010 0101 // need double stepping??? (bit 5)
346 config_data = 0x00; // 0000 0000 ???
347 media_state = 0x17; // 0001 0111
351 // 1.44 MB 3.5" drive
352 config_data = 0x00; // 0000 0000
353 media_state = 0x17; // 0001 0111
357 // 2.88 MB 3.5" drive
358 config_data = 0xCC; // 1100 1100
359 media_state = 0xD7; // 1101 0111
363 // Extended floppy size uses special cmos setting
366 config_data = 0x00; // 0000 0000
367 media_state = 0x27; // 0010 0111
372 config_data = 0x00; // 0000 0000
373 media_state = 0x27; // 0010 0111
378 config_data = 0x00; // 0000 0000
379 media_state = 0x27; // 0010 0111
384 config_data = 0x00; // 0000 0000
385 media_state = 0x00; // 0000 0000
389 SET_BDA(floppy_last_data_rate, config_data);
390 SET_BDA(floppy_media_state[drive], media_state);
395 check_drive(struct bregs *regs, u8 drive)
397 // see if drive exists
398 if (drive > 1 || !get_drive_type(drive)) {
399 // XXX - return type doesn't match
400 floppy_ret(regs, DISK_RET_ETIMEOUT);
404 // see if media in drive, and type is known
405 if (floppy_media_known(drive) == 0 && floppy_media_sense(drive) == 0) {
406 floppy_ret(regs, DISK_RET_EMEDIA);
412 // diskette controller reset
414 floppy_1300(struct bregs *regs, u8 drive)
417 floppy_ret(regs, DISK_RET_EPARAM);
420 if (!get_drive_type(drive)) {
421 floppy_ret(regs, DISK_RET_ETIMEOUT);
424 set_diskette_current_cyl(drive, 0); // current cylinder
425 floppy_ret(regs, DISK_RET_SUCCESS);
428 // Read Diskette Status
430 floppy_1301(struct bregs *regs, u8 drive)
432 u8 v = GET_BDA(floppy_last_status);
437 // Read Diskette Sectors
439 floppy_1302(struct bregs *regs, u8 drive)
441 if (check_drive(regs, drive))
444 u8 num_sectors = regs->al;
446 u8 sector = regs->cl;
449 if (head > 1 || sector == 0 || num_sectors == 0
450 || track > 79 || num_sectors > 72) {
451 floppy_ret(regs, DISK_RET_EPARAM);
455 // send read-normal-data command (9 bytes) to controller
457 data[0] = 0xe6; // e6: read normal data
458 data[1] = (head << 2) | drive; // HD DR1 DR2
462 data[5] = 2; // 512 byte sector size
463 data[6] = sector + num_sectors - 1; // last sector to read on track
464 data[7] = 0; // Gap length
465 data[8] = 0xff; // Gap length
467 int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
471 if (data[0] & 0xc0) {
472 floppy_ret(regs, DISK_RET_ECONTROLLER);
476 // ??? should track be new val from return_status[3] ?
477 set_diskette_current_cyl(drive, track);
478 // AL = number of sectors read (same value as passed)
479 floppy_ret(regs, DISK_RET_SUCCESS);
482 regs->al = 0; // no sectors read
485 // Write Diskette Sectors
487 floppy_1303(struct bregs *regs, u8 drive)
489 if (check_drive(regs, drive))
492 u8 num_sectors = regs->al;
494 u8 sector = regs->cl;
497 if (head > 1 || sector == 0 || num_sectors == 0
498 || track > 79 || num_sectors > 72) {
499 floppy_ret(regs, DISK_RET_EPARAM);
503 // send write-normal-data command (9 bytes) to controller
505 data[0] = 0xc5; // c5: write normal data
506 data[1] = (head << 2) | drive; // HD DR1 DR2
510 data[5] = 2; // 512 byte sector size
511 data[6] = sector + num_sectors - 1; // last sector to write on track
512 data[7] = 0; // Gap length
513 data[8] = 0xff; // Gap length
515 int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
519 if (data[0] & 0xc0) {
520 if (data[1] & 0x02) {
521 floppy_ret(regs, DISK_RET_EWRITEPROTECT);
524 BX_PANIC("int13_diskette_function: read error\n");
527 // ??? should track be new val from return_status[3] ?
528 set_diskette_current_cyl(drive, track);
529 // AL = number of sectors read (same value as passed)
530 floppy_ret(regs, DISK_RET_SUCCESS);
533 regs->al = 0; // no sectors read
536 // Verify Diskette Sectors
538 floppy_1304(struct bregs *regs, u8 drive)
540 if (check_drive(regs, drive))
543 u8 num_sectors = regs->al;
545 u8 sector = regs->cl;
548 if (head > 1 || sector == 0 || num_sectors == 0
549 || track > 79 || num_sectors > 72) {
550 floppy_ret(regs, DISK_RET_EPARAM);
554 // ??? should track be new val from return_status[3] ?
555 set_diskette_current_cyl(drive, track);
556 // AL = number of sectors verified (same value as passed)
557 floppy_ret(regs, DISK_RET_SUCCESS);
560 regs->al = 0; // no sectors read
563 // format diskette track
565 floppy_1305(struct bregs *regs, u8 drive)
567 dprintf(3, "floppy f05\n");
569 if (check_drive(regs, drive))
572 u8 num_sectors = regs->al;
575 if (head > 1 || num_sectors == 0 || num_sectors > 18) {
576 floppy_ret(regs, DISK_RET_EPARAM);
580 // send format-track command (6 bytes) to controller
582 data[0] = 0x4d; // 4d: format track
583 data[1] = (head << 2) | drive; // HD DR1 DR2
584 data[2] = 2; // 512 byte sector size
585 data[3] = num_sectors; // number of sectors per track
586 data[4] = 0; // Gap length
587 data[5] = 0xf6; // Fill byte
589 int ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
593 if (data[0] & 0xc0) {
594 if (data[1] & 0x02) {
595 floppy_ret(regs, DISK_RET_EWRITEPROTECT);
598 BX_PANIC("int13_diskette_function: read error\n");
601 set_diskette_current_cyl(drive, 0);
605 // read diskette drive parameters
607 floppy_1308(struct bregs *regs, u8 drive)
609 dprintf(3, "floppy f08\n");
611 u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
613 if (drive_type & 0xf0)
615 if (drive_type & 0x0f)
625 regs->dl = num_floppies;
636 regs->bl = drive_type;
639 regs->dl = num_floppies;
641 switch (drive_type) {
644 regs->dh = 0; // max head #
647 case 1: // 360KB, 5.25"
648 regs->cx = 0x2709; // 40 tracks, 9 sectors
649 regs->dh = 1; // max head #
652 case 2: // 1.2MB, 5.25"
653 regs->cx = 0x4f0f; // 80 tracks, 15 sectors
654 regs->dh = 1; // max head #
657 case 3: // 720KB, 3.5"
658 regs->cx = 0x4f09; // 80 tracks, 9 sectors
659 regs->dh = 1; // max head #
662 case 4: // 1.44MB, 3.5"
663 regs->cx = 0x4f12; // 80 tracks, 18 sectors
664 regs->dh = 1; // max head #
667 case 5: // 2.88MB, 3.5"
668 regs->cx = 0x4f24; // 80 tracks, 36 sectors
669 regs->dh = 1; // max head #
672 case 6: // 160k, 5.25"
673 regs->cx = 0x2708; // 40 tracks, 8 sectors
674 regs->dh = 0; // max head #
677 case 7: // 180k, 5.25"
678 regs->cx = 0x2709; // 40 tracks, 9 sectors
679 regs->dh = 0; // max head #
682 case 8: // 320k, 5.25"
683 regs->cx = 0x2708; // 40 tracks, 8 sectors
684 regs->dh = 1; // max head #
688 BX_PANIC("floppy: int13: bad floppy type\n");
691 /* set es & di to point to 11 byte diskette param table in ROM */
693 regs->di = (u32)&diskette_param_table2;
694 /* disk status not changed upon success */
698 // read diskette drive type
700 floppy_1315(struct bregs *regs, u8 drive)
702 dprintf(6, "floppy f15\n");
705 regs->ah = 0; // only 2 drives supported
706 // set_diskette_ret_status here ???
709 u8 drive_type = get_drive_type(drive);
711 regs->ah = (drive_type != 0);
715 // get diskette change line status
717 floppy_1316(struct bregs *regs, u8 drive)
720 floppy_ret(regs, DISK_RET_EPARAM);
723 floppy_ret(regs, DISK_RET_ECHANGED);
727 floppy_13XX(struct bregs *regs, u8 drive)
729 floppy_ret(regs, DISK_RET_EPARAM);
733 floppy_13(struct bregs *regs, u8 drive)
735 if (! CONFIG_FLOPPY_SUPPORT) {
738 case 0x01: floppy_1301(regs, drive); break;
739 default: floppy_13XX(regs, drive); break;
744 case 0x00: floppy_1300(regs, drive); break;
745 case 0x01: floppy_1301(regs, drive); break;
746 case 0x02: floppy_1302(regs, drive); break;
747 case 0x03: floppy_1303(regs, drive); break;
748 case 0x04: floppy_1304(regs, drive); break;
749 case 0x05: floppy_1305(regs, drive); break;
750 case 0x08: floppy_1308(regs, drive); break;
751 case 0x15: floppy_1315(regs, drive); break;
752 case 0x16: floppy_1316(regs, drive); break;
753 default: floppy_13XX(regs, drive); break;
757 // INT 0Eh Diskette Hardware ISR Entry Point
761 debug_isr(DEBUG_ISR_0e);
762 if (! CONFIG_FLOPPY_SUPPORT)
765 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
766 outb(0x08, PORT_FD_DATA); // sense interrupt status
767 while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
771 } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
773 // diskette interrupt has occurred
774 SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
780 // Called from int08 handler.
784 if (! CONFIG_FLOPPY_SUPPORT)
787 // time to turn off drive(s)?
788 u8 fcount = GET_BDA(floppy_motor_counter);
791 SET_BDA(floppy_motor_counter, fcount);
794 outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);