1 // 16bit code to access floppy drives.
3 // Copyright (C) 2008,2009 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
11 #include "biosvar.h" // SET_BDA
12 #include "util.h" // wait_irq
13 #include "cmos.h" // inb_cmos
14 #include "pic.h" // eoi_pic1
15 #include "bregs.h" // struct bregs
16 #include "boot.h" // boot_add_floppy
17 #include "pci.h" // pci_to_bdf
18 #include "pci_ids.h" // PCI_CLASS_BRIDGE_ISA
20 #define FLOPPY_SIZE_CODE 0x02 // 512 byte sectors
21 #define FLOPPY_DATALEN 0xff // Not used - because size code is 0x02
22 #define FLOPPY_MOTOR_TICKS 37 // ~2 seconds
23 #define FLOPPY_FILLBYTE 0xf6
24 #define FLOPPY_GAPLEN 0x1B
25 #define FLOPPY_FORMAT_GAPLEN 0x6c
27 // New diskette parameter table adding 3 parameters from IBM
28 // Since no provisions are made for multiple drive types, most
29 // values in this table are ignored. I set parameters for 1.44M
31 struct floppy_ext_dbt_s diskette_param_table2 VAR16VISIBLE = {
33 .specify1 = 0xAF, // step rate 12ms, head unload 240ms
34 .specify2 = 0x02, // head load time 4ms, DMA used
35 .shutoff_ticks = FLOPPY_MOTOR_TICKS, // ~2 seconds
36 .bps_code = FLOPPY_SIZE_CODE,
38 .interblock_len = FLOPPY_GAPLEN,
39 .data_len = FLOPPY_DATALEN,
40 .gap_len = FLOPPY_FORMAT_GAPLEN,
41 .fill_byte = FLOPPY_FILLBYTE,
42 .settle_time = 0x0F, // 15ms
43 .startup_time = 0x08, // 1 second
45 .max_track = 79, // maximum track
46 .data_rate = 0, // data transfer rate
47 .drive_type = 4, // drive type in cmos
50 // Since no provisions are made for multiple drive types, most
51 // values in this table are ignored. I set parameters for 1.44M
53 struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7) = {
56 .shutoff_ticks = FLOPPY_MOTOR_TICKS,
57 .bps_code = FLOPPY_SIZE_CODE,
59 .interblock_len = FLOPPY_GAPLEN,
60 .data_len = FLOPPY_DATALEN,
61 .gap_len = FLOPPY_FORMAT_GAPLEN,
62 .fill_byte = FLOPPY_FILLBYTE,
73 struct floppyinfo_s FloppyInfo[] VAR16VISIBLE = {
75 { {0, 0, 0}, 0x00, 0x00},
76 // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors
77 { {2, 40, 9}, 0x00, 0x25},
78 // 2 - 1.2MB, 5.25" - 2 heads, 80 tracks, 15 sectors
79 { {2, 80, 15}, 0x00, 0x25},
80 // 3 - 720KB, 3.5" - 2 heads, 80 tracks, 9 sectors
81 { {2, 80, 9}, 0x00, 0x17},
82 // 4 - 1.44MB, 3.5" - 2 heads, 80 tracks, 18 sectors
83 { {2, 80, 18}, 0x00, 0x17},
84 // 5 - 2.88MB, 3.5" - 2 heads, 80 tracks, 36 sectors
85 { {2, 80, 36}, 0xCC, 0xD7},
86 // 6 - 160k, 5.25" - 1 heads, 40 tracks, 8 sectors
87 { {1, 40, 8}, 0x00, 0x27},
88 // 7 - 180k, 5.25" - 1 heads, 40 tracks, 9 sectors
89 { {1, 40, 9}, 0x00, 0x27},
90 // 8 - 320k, 5.25" - 2 heads, 40 tracks, 8 sectors
91 { {2, 40, 8}, 0x00, 0x27},
95 addFloppy(int floppyid, int ftype, int driver)
97 if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
98 dprintf(1, "Bad floppy type %d\n", ftype);
102 char *desc = malloc_tmp(MAXDESCSIZE);
103 struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
104 if (!drive_g || !desc) {
110 memset(drive_g, 0, sizeof(*drive_g));
111 drive_g->cntl_id = floppyid;
112 drive_g->type = driver;
113 drive_g->blksize = DISK_SECTOR_SIZE;
114 drive_g->floppy_type = ftype;
115 drive_g->sectors = (u64)-1;
116 drive_g->desc = desc;
117 snprintf(desc, MAXDESCSIZE, "Floppy [drive %c]", 'A' + floppyid);
119 memcpy(&drive_g->lchs, &FloppyInfo[ftype].chs
120 , sizeof(FloppyInfo[ftype].chs));
122 int bdf = pci_find_class(PCI_CLASS_BRIDGE_ISA); /* isa-to-pci bridge */
123 int prio = bootprio_find_fdc_device(bdf, PORT_FD_BASE, floppyid);
125 boot_add_floppy(drive_g, prio);
134 dprintf(3, "init floppy drives\n");
136 if (CONFIG_COREBOOT) {
137 // XXX - disable floppies on coreboot for now.
139 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
141 addFloppy(0, type >> 4, DTYPE_FLOPPY);
143 addFloppy(1, type & 0x0f, DTYPE_FLOPPY);
146 outb(0x02, PORT_DMA1_MASK_REG);
148 enable_hwirq(6, FUNC16(entry_0e));
151 // Find a floppy type that matches a given image size.
153 find_floppy_type(u32 size)
156 for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
157 struct chs_s *c = &FloppyInfo[i].chs;
158 if (c->cylinders * c->heads * c->spt * DISK_SECTOR_SIZE == size)
165 /****************************************************************
166 * Low-level floppy IO
167 ****************************************************************/
170 floppy_reset_controller(void)
173 u8 val8 = inb(PORT_FD_DOR);
174 outb(val8 & ~0x04, PORT_FD_DOR);
175 outb(val8 | 0x04, PORT_FD_DOR);
177 // Wait for controller to come out of reset
178 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
183 wait_floppy_irq(void)
188 if (!GET_BDA(floppy_motor_counter))
190 v = GET_BDA(floppy_recalibration_status);
193 // Could use wait_irq() here, but that causes issues on
194 // bochs, so use yield() instead.
199 SET_BDA(floppy_recalibration_status, v);
204 floppy_prepare_controller(u8 floppyid)
206 CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
208 // turn on motor of selected drive, DMA & int enabled, normal operation
209 u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
215 outb(dor, PORT_FD_DOR);
217 // reset the disk motor timeout value of INT 08
218 SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
220 // wait for drive readiness
221 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
229 floppy_pio(u8 *cmd, u8 cmdlen)
231 floppy_prepare_controller(cmd[1] & 1);
233 // send command to controller
235 for (i=0; i<cmdlen; i++)
236 outb(cmd[i], PORT_FD_DATA);
238 int ret = wait_floppy_irq();
240 floppy_reset_controller();
248 floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
250 // es:bx = pointer to where to place information from diskette
251 u32 addr = (u32)op->buf_fl;
253 // check for 64K boundary overrun
255 u32 last_addr = addr + end;
256 if ((addr >> 16) != (last_addr >> 16))
257 return DISK_RET_EBOUNDARY;
259 u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
262 mode_register = 0x46;
264 //DEBUGF("floppy dma c2\n");
265 outb(0x06, PORT_DMA1_MASK_REG);
266 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
267 outb(addr, PORT_DMA_ADDR_2);
268 outb(addr>>8, PORT_DMA_ADDR_2);
269 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
270 outb(end, PORT_DMA_CNT_2);
271 outb(end>>8, PORT_DMA_CNT_2);
273 // port 0b: DMA-1 Mode Register
274 // transfer type=write, channel 2
275 outb(mode_register, PORT_DMA1_MODE_REG);
277 // port 81: DMA-1 Page Register, channel 2
278 outb(addr>>16, PORT_DMA_PAGE_2);
280 outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
282 int ret = floppy_pio(cmd, cmdlen);
284 return DISK_RET_ETIMEOUT;
286 // check port 3f4 for accessibility to status bytes
287 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
288 return DISK_RET_ECONTROLLER;
290 // read 7 return status bytes from controller
292 for (i=0; i<7; i++) {
293 u8 v = inb(PORT_FD_DATA);
295 SET_BDA(floppy_return_status[i], v);
298 return DISK_RET_SUCCESS;
302 /****************************************************************
304 ****************************************************************/
307 set_diskette_current_cyl(u8 floppyid, u8 cyl)
309 SET_BDA(floppy_track[floppyid], cyl);
313 floppy_drive_recal(u8 floppyid)
315 // send Recalibrate command (2 bytes) to controller
317 data[0] = 0x07; // 07: Recalibrate
318 data[1] = floppyid; // 0=drive0, 1=drive1
321 SETBITS_BDA(floppy_recalibration_status, 1<<floppyid);
322 set_diskette_current_cyl(floppyid, 0);
326 floppy_media_sense(struct drive_s *drive_g)
328 // for now cheat and get drive type from CMOS,
329 // assume media is same as drive type
332 // Bitfields for diskette media control:
333 // Bit(s) Description (Table M0028)
334 // 7-6 last data rate set by controller
335 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
336 // 5-4 last diskette drive step rate selected
337 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
338 // 3-2 {data rate at start of operation}
342 // Bitfields for diskette drive media state:
343 // Bit(s) Description (Table M0030)
345 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
346 // 5 double stepping required (e.g. 360kB in 1.2MB)
347 // 4 media type established
348 // 3 drive capable of supporting 4MB media
349 // 2-0 on exit from BIOS, contains
350 // 000 trying 360kB in 360kB
351 // 001 trying 360kB in 1.2MB
352 // 010 trying 1.2MB in 1.2MB
353 // 011 360kB in 360kB established
354 // 100 360kB in 1.2MB established
355 // 101 1.2MB in 1.2MB established
357 // 111 all other formats/drives
359 u8 ftype = GET_GLOBAL(drive_g->floppy_type);
360 SET_BDA(floppy_last_data_rate, GET_GLOBAL(FloppyInfo[ftype].config_data));
361 u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
362 SET_BDA(floppy_media_state[floppyid]
363 , GET_GLOBAL(FloppyInfo[ftype].media_state));
364 return DISK_RET_SUCCESS;
368 check_recal_drive(struct drive_s *drive_g)
370 u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
371 if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid))
372 && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED))
374 return DISK_RET_SUCCESS;
376 // Recalibrate drive.
377 floppy_drive_recal(floppyid);
380 return floppy_media_sense(drive_g);
384 /****************************************************************
386 ****************************************************************/
389 lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
394 u16 nlspt = GET_GLOBAL(op->drive_g->lchs.spt);
395 *sector = tmp % nlspt;
398 u16 nlh = GET_GLOBAL(op->drive_g->lchs.heads);
405 // diskette controller reset
407 floppy_reset(struct disk_op_s *op)
409 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
410 set_diskette_current_cyl(floppyid, 0); // current cylinder
411 return DISK_RET_SUCCESS;
414 // Read Diskette Sectors
416 floppy_read(struct disk_op_s *op)
418 int res = check_recal_drive(op->drive_g);
422 u8 track, sector, head;
423 lba2chs(op, &track, §or, &head);
425 // send read-normal-data command (9 bytes) to controller
426 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
428 data[0] = 0xe6; // e6: read normal data
429 data[1] = (head << 2) | floppyid; // HD DR1 DR2
433 data[5] = FLOPPY_SIZE_CODE;
434 data[6] = sector + op->count - 1; // last sector to read on track
435 data[7] = FLOPPY_GAPLEN;
436 data[8] = FLOPPY_DATALEN;
438 res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
442 if (data[0] & 0xc0) {
443 res = DISK_RET_ECONTROLLER;
447 // ??? should track be new val from return_status[3] ?
448 set_diskette_current_cyl(floppyid, track);
449 return DISK_RET_SUCCESS;
451 op->count = 0; // no sectors read
455 // Write Diskette Sectors
457 floppy_write(struct disk_op_s *op)
459 int res = check_recal_drive(op->drive_g);
463 u8 track, sector, head;
464 lba2chs(op, &track, §or, &head);
466 // send write-normal-data command (9 bytes) to controller
467 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
469 data[0] = 0xc5; // c5: write normal data
470 data[1] = (head << 2) | floppyid; // HD DR1 DR2
474 data[5] = FLOPPY_SIZE_CODE;
475 data[6] = sector + op->count - 1; // last sector to write on track
476 data[7] = FLOPPY_GAPLEN;
477 data[8] = FLOPPY_DATALEN;
479 res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
483 if (data[0] & 0xc0) {
485 res = DISK_RET_EWRITEPROTECT;
487 res = DISK_RET_ECONTROLLER;
491 // ??? should track be new val from return_status[3] ?
492 set_diskette_current_cyl(floppyid, track);
493 return DISK_RET_SUCCESS;
495 op->count = 0; // no sectors read
499 // Verify Diskette Sectors
501 floppy_verify(struct disk_op_s *op)
503 int res = check_recal_drive(op->drive_g);
507 u8 track, sector, head;
508 lba2chs(op, &track, §or, &head);
510 // ??? should track be new val from return_status[3] ?
511 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
512 set_diskette_current_cyl(floppyid, track);
513 return DISK_RET_SUCCESS;
515 op->count = 0; // no sectors read
519 // format diskette track
521 floppy_format(struct disk_op_s *op)
523 int ret = check_recal_drive(op->drive_g);
529 // send format-track command (6 bytes) to controller
530 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
532 data[0] = 0x4d; // 4d: format track
533 data[1] = (head << 2) | floppyid; // HD DR1 DR2
534 data[2] = FLOPPY_SIZE_CODE;
535 data[3] = op->count; // number of sectors per track
536 data[4] = FLOPPY_FORMAT_GAPLEN;
537 data[5] = FLOPPY_FILLBYTE;
539 ret = floppy_cmd(op, op->count * 4, data, 6);
543 if (data[0] & 0xc0) {
545 return DISK_RET_EWRITEPROTECT;
546 return DISK_RET_ECONTROLLER;
549 set_diskette_current_cyl(floppyid, 0);
550 return DISK_RET_SUCCESS;
554 process_floppy_op(struct disk_op_s *op)
559 switch (op->command) {
561 return floppy_reset(op);
563 return floppy_read(op);
565 return floppy_write(op);
567 return floppy_verify(op);
569 return floppy_format(op);
572 return DISK_RET_EPARAM;
577 /****************************************************************
579 ****************************************************************/
581 // INT 0Eh Diskette Hardware ISR Entry Point
585 debug_isr(DEBUG_ISR_0e);
589 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
590 outb(0x08, PORT_FD_DATA); // sense interrupt status
591 while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
595 } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
597 // diskette interrupt has occurred
598 SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
604 // Called from int08 handler.
611 // time to turn off drive(s)?
612 u8 fcount = GET_BDA(floppy_motor_counter);
615 SET_BDA(floppy_motor_counter, fcount);
618 outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);