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 GPLv3 license.
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
16 #define DEBUGF1(fmt, args...) bprintf(0, fmt , ##args)
17 #define DEBUGF(fmt, args...)
19 #define BX_FLOPPY_ON_CNT 37 /* 2 seconds */
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
25 struct floppy_ext_dbt_s diskette_param_table2 VISIBLE16 = {
28 .specify2 = 0x02, // head load time 0000001, DMA used
29 .shutoff_ticks = 0x25,
32 .interblock_len = 0x1B,
39 .max_track = 79, // maximum track
40 .data_rate = 0, // data transfer rate
41 .drive_type = 4, // drive type in cmos
47 if (! CONFIG_FLOPPY_SUPPORT)
49 dprintf(3, "init floppy drives\n");
50 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
62 SET_BDA(floppy_harddisk_info, out);
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);
72 outb(0x02, PORT_DMA1_MASK_REG);
74 // Enable IRQ6 (handle_0e)
75 unmask_pic1(PIC1_IRQ6);
79 set_diskette_current_cyl(u8 drive, u8 cyl)
82 SET_BDA(floppy_track1, cyl);
84 SET_BDA(floppy_track0, cyl);
88 get_drive_type(u8 drive)
90 // check CMOS to see if drive exists
91 u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
100 floppy_media_known(u8 drive)
102 if (!(GET_BDA(floppy_recalibration_status) & (1<<drive)))
104 u8 v = GET_BDA(floppy_media_state[drive]);
105 if (!(v & FMS_MEDIA_DRIVE_ESTABLISHED))
111 floppy_reset_controller()
114 u8 val8 = inb(PORT_FD_DOR);
115 outb(val8 & ~0x04, PORT_FD_DOR);
116 outb(val8 | 0x04, PORT_FD_DOR);
118 // Wait for controller to come out of reset
119 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
124 floppy_prepare_controller(u8 drive)
126 CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
128 // turn on motor of selected drive, DMA & int enabled, normal operation
129 u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
135 outb(dor, PORT_FD_DOR);
137 // reset the disk motor timeout value of INT 08
138 SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT);
140 // wait for drive readiness
141 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
144 if (prev_reset == 0) {
148 v = GET_BDA(floppy_recalibration_status);
156 SET_BDA(floppy_recalibration_status, v);
161 floppy_pio(u8 *cmd, u8 cmdlen)
163 floppy_prepare_controller(cmd[1] & 1);
165 // send command to controller
167 for (i=0; i<cmdlen; i++)
168 outb(cmd[i], PORT_FD_DATA);
173 if (!GET_BDA(floppy_motor_counter)) {
175 floppy_reset_controller();
176 return DISK_RET_ETIMEOUT;
178 v = GET_BDA(floppy_recalibration_status);
186 SET_BDA(floppy_recalibration_status, v);
192 floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
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
205 // check for 64K boundary overrun
206 u16 last_addr = base_address + count;
207 if (last_addr < base_address)
208 return DISK_RET_EBOUNDARY;
210 u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
213 mode_register = 0x46;
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);
224 // port 0b: DMA-1 Mode Register
225 // transfer type=write, channel 2
226 outb(mode_register, PORT_DMA1_MODE_REG);
228 // port 81: DMA-1 Page Register, channel 2
229 outb(page, PORT_DMA_PAGE_2);
231 outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
233 u8 ret = floppy_pio(cmd, cmdlen);
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");
241 // read 7 return status bytes from controller
243 for (i=0; i<7; i++) {
244 u8 v = inb(PORT_FD_DATA);
246 SET_BDA(floppy_return_status[i], v);
253 floppy_drive_recal(u8 drive)
255 // send Recalibrate command (2 bytes) to controller
257 data[0] = 0x07; // 07: Recalibrate
258 data[1] = drive; // 0=drive0, 1=drive1
261 SETBITS_BDA(floppy_recalibration_status, 1<<drive);
262 set_diskette_current_cyl(drive, 0);
266 floppy_media_sense(u8 drive)
269 u8 config_data, media_state;
271 floppy_drive_recal(drive);
273 // for now cheat and get drive type from CMOS,
274 // assume media is same as drive type
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}
287 // Bitfields for diskette drive media state:
288 // Bit(s) Description (Table M0030)
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
302 // 111 all other formats/drives
304 switch (get_drive_type(drive)) {
307 config_data = 0x00; // 0000 0000
308 media_state = 0x25; // 0010 0101
312 // 1.2 MB 5.25" drive
313 config_data = 0x00; // 0000 0000
314 media_state = 0x25; // 0010 0101 // need double stepping??? (bit 5)
319 config_data = 0x00; // 0000 0000 ???
320 media_state = 0x17; // 0001 0111
324 // 1.44 MB 3.5" drive
325 config_data = 0x00; // 0000 0000
326 media_state = 0x17; // 0001 0111
330 // 2.88 MB 3.5" drive
331 config_data = 0xCC; // 1100 1100
332 media_state = 0xD7; // 1101 0111
336 // Extended floppy size uses special cmos setting
339 config_data = 0x00; // 0000 0000
340 media_state = 0x27; // 0010 0111
345 config_data = 0x00; // 0000 0000
346 media_state = 0x27; // 0010 0111
351 config_data = 0x00; // 0000 0000
352 media_state = 0x27; // 0010 0111
357 config_data = 0x00; // 0000 0000
358 media_state = 0x00; // 0000 0000
362 SET_BDA(floppy_last_data_rate, config_data);
363 SET_BDA(floppy_media_state[drive], media_state);
367 #define floppy_ret(regs, code) \
368 __floppy_ret(__func__, (regs), (code))
371 __floppy_ret(const char *fname, struct bregs *regs, u8 code)
373 SET_BDA(floppy_last_status, code);
375 __set_code_fail(fname, regs, code);
377 set_code_success(regs);
381 floppy_fail(struct bregs *regs, u8 code)
383 floppy_ret(regs, code);
384 regs->al = 0; // no sectors read
388 check_drive(struct bregs *regs, u8 drive)
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);
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);
405 // diskette controller reset
407 floppy_1300(struct bregs *regs, u8 drive)
410 floppy_ret(regs, DISK_RET_EPARAM);
413 if (!get_drive_type(drive)) {
414 floppy_ret(regs, DISK_RET_ETIMEOUT);
417 set_diskette_current_cyl(drive, 0); // current cylinder
418 floppy_ret(regs, DISK_RET_SUCCESS);
421 // Read Diskette Status
423 floppy_1301(struct bregs *regs, u8 drive)
425 u8 v = GET_BDA(floppy_last_status);
430 // Read Diskette Sectors
432 floppy_1302(struct bregs *regs, u8 drive)
434 if (check_drive(regs, drive))
437 u8 num_sectors = regs->al;
439 u8 sector = regs->cl;
442 if (head > 1 || sector == 0 || num_sectors == 0
443 || track > 79 || num_sectors > 72) {
444 floppy_fail(regs, DISK_RET_EPARAM);
448 // send read-normal-data command (9 bytes) to controller
450 data[0] = 0xe6; // e6: read normal data
451 data[1] = (head << 2) | drive; // HD DR1 DR2
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
460 u16 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
462 floppy_fail(regs, ret);
466 if (data[0] & 0xc0) {
467 floppy_fail(regs, DISK_RET_ECONTROLLER);
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);
477 // Write Diskette Sectors
479 floppy_1303(struct bregs *regs, u8 drive)
481 if (check_drive(regs, drive))
484 u8 num_sectors = regs->al;
486 u8 sector = regs->cl;
489 if (head > 1 || sector == 0 || num_sectors == 0
490 || track > 79 || num_sectors > 72) {
491 floppy_fail(regs, DISK_RET_EPARAM);
495 // send write-normal-data command (9 bytes) to controller
497 data[0] = 0xc5; // c5: write normal data
498 data[1] = (head << 2) | drive; // HD DR1 DR2
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
507 u8 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
509 floppy_fail(regs, ret);
513 if (data[0] & 0xc0) {
514 if (data[1] & 0x02) {
519 BX_PANIC("int13_diskette_function: read error\n");
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);
528 // Verify Diskette Sectors
530 floppy_1304(struct bregs *regs, u8 drive)
532 if (check_drive(regs, drive))
535 u8 num_sectors = regs->al;
537 u8 sector = regs->cl;
540 if (head > 1 || sector == 0 || num_sectors == 0
541 || track > 79 || num_sectors > 72) {
542 floppy_fail(regs, DISK_RET_EPARAM);
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);
552 // format diskette track
554 floppy_1305(struct bregs *regs, u8 drive)
556 DEBUGF("floppy f05\n");
558 if (check_drive(regs, drive))
561 u8 num_sectors = regs->al;
564 if (head > 1 || num_sectors == 0 || num_sectors > 18) {
565 floppy_fail(regs, DISK_RET_EPARAM);
569 // send format-track command (6 bytes) to controller
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
578 u8 ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
580 floppy_fail(regs, ret);
584 if (data[0] & 0xc0) {
585 if (data[1] & 0x02) {
590 BX_PANIC("int13_diskette_function: read error\n");
593 set_diskette_current_cyl(drive, 0);
597 // read diskette drive parameters
599 floppy_1308(struct bregs *regs, u8 drive)
601 DEBUGF("floppy f08\n");
603 u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
605 if (drive_type & 0xf0)
607 if (drive_type & 0x0f)
617 regs->dl = num_floppies;
628 regs->bl = drive_type;
631 regs->dl = num_floppies;
633 switch (drive_type) {
636 regs->dh = 0; // max head #
639 case 1: // 360KB, 5.25"
640 regs->cx = 0x2709; // 40 tracks, 9 sectors
641 regs->dh = 1; // max head #
644 case 2: // 1.2MB, 5.25"
645 regs->cx = 0x4f0f; // 80 tracks, 15 sectors
646 regs->dh = 1; // max head #
649 case 3: // 720KB, 3.5"
650 regs->cx = 0x4f09; // 80 tracks, 9 sectors
651 regs->dh = 1; // max head #
654 case 4: // 1.44MB, 3.5"
655 regs->cx = 0x4f12; // 80 tracks, 18 sectors
656 regs->dh = 1; // max head #
659 case 5: // 2.88MB, 3.5"
660 regs->cx = 0x4f24; // 80 tracks, 36 sectors
661 regs->dh = 1; // max head #
664 case 6: // 160k, 5.25"
665 regs->cx = 0x2708; // 40 tracks, 8 sectors
666 regs->dh = 0; // max head #
669 case 7: // 180k, 5.25"
670 regs->cx = 0x2709; // 40 tracks, 9 sectors
671 regs->dh = 0; // max head #
674 case 8: // 320k, 5.25"
675 regs->cx = 0x2708; // 40 tracks, 8 sectors
676 regs->dh = 1; // max head #
680 BX_PANIC("floppy: int13: bad floppy type\n");
683 /* set es & di to point to 11 byte diskette param table in ROM */
685 regs->di = (u32)&diskette_param_table2;
686 /* disk status not changed upon success */
689 // read diskette drive type
691 floppy_1315(struct bregs *regs, u8 drive)
693 DEBUGF("floppy f15\n");
696 regs->ah = 0; // only 2 drives supported
697 // set_diskette_ret_status here ???
700 u8 drive_type = get_drive_type(drive);
702 regs->ah = (drive_type != 0);
706 // get diskette change line status
708 floppy_1316(struct bregs *regs, u8 drive)
710 DEBUGF("floppy f16\n");
712 floppy_ret(regs, DISK_RET_EPARAM);
715 floppy_ret(regs, DISK_RET_ECHANGED);
719 floppy_13XX(struct bregs *regs, u8 drive)
721 floppy_ret(regs, DISK_RET_EPARAM);
725 floppy_13(struct bregs *regs, u8 drive)
727 if (! CONFIG_FLOPPY_SUPPORT) {
730 case 0x01: floppy_1301(regs, drive); break;
731 default: floppy_13XX(regs, drive); break;
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;
749 // INT 0Eh Diskette Hardware ISR Entry Point
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)
760 } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
763 // diskette interrupt has occurred
764 SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
767 // Called from int08 handler.
771 // time to turn off drive(s)?
772 u8 fcount = GET_BDA(floppy_motor_counter);
775 SET_BDA(floppy_motor_counter, fcount);
778 outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);