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
17 #define FLOPPY_SIZE_CODE 0x02 // 512 byte sectors
18 #define FLOPPY_DATALEN 0xff // Not used - because size code is 0x02
19 #define FLOPPY_MOTOR_TICKS 37 // ~2 seconds
20 #define FLOPPY_FILLBYTE 0xf6
21 #define FLOPPY_GAPLEN 0x1B
22 #define FLOPPY_FORMAT_GAPLEN 0x6c
24 // New diskette parameter table adding 3 parameters from IBM
25 // Since no provisions are made for multiple drive types, most
26 // values in this table are ignored. I set parameters for 1.44M
28 struct floppy_ext_dbt_s diskette_param_table2 VAR16VISIBLE = {
30 .specify1 = 0xAF, // step rate 12ms, head unload 240ms
31 .specify2 = 0x02, // head load time 4ms, DMA used
32 .shutoff_ticks = FLOPPY_MOTOR_TICKS, // ~2 seconds
33 .bps_code = FLOPPY_SIZE_CODE,
35 .interblock_len = FLOPPY_GAPLEN,
36 .data_len = FLOPPY_DATALEN,
37 .gap_len = FLOPPY_FORMAT_GAPLEN,
38 .fill_byte = FLOPPY_FILLBYTE,
39 .settle_time = 0x0F, // 15ms
40 .startup_time = 0x08, // 1 second
42 .max_track = 79, // maximum track
43 .data_rate = 0, // data transfer rate
44 .drive_type = 4, // drive type in cmos
47 // Since no provisions are made for multiple drive types, most
48 // values in this table are ignored. I set parameters for 1.44M
50 struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7) = {
53 .shutoff_ticks = FLOPPY_MOTOR_TICKS,
54 .bps_code = FLOPPY_SIZE_CODE,
56 .interblock_len = FLOPPY_GAPLEN,
57 .data_len = FLOPPY_DATALEN,
58 .gap_len = FLOPPY_FORMAT_GAPLEN,
59 .fill_byte = FLOPPY_FILLBYTE,
70 struct floppyinfo_s FloppyInfo[] VAR16VISIBLE = {
72 { {0, 0, 0}, 0x00, 0x00},
73 // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors
74 { {2, 40, 9}, 0x00, 0x25},
75 // 2 - 1.2MB, 5.25" - 2 heads, 80 tracks, 15 sectors
76 { {2, 80, 15}, 0x00, 0x25},
77 // 3 - 720KB, 3.5" - 2 heads, 80 tracks, 9 sectors
78 { {2, 80, 9}, 0x00, 0x17},
79 // 4 - 1.44MB, 3.5" - 2 heads, 80 tracks, 18 sectors
80 { {2, 80, 18}, 0x00, 0x17},
81 // 5 - 2.88MB, 3.5" - 2 heads, 80 tracks, 36 sectors
82 { {2, 80, 36}, 0xCC, 0xD7},
83 // 6 - 160k, 5.25" - 1 heads, 40 tracks, 8 sectors
84 { {1, 40, 8}, 0x00, 0x27},
85 // 7 - 180k, 5.25" - 1 heads, 40 tracks, 9 sectors
86 { {1, 40, 9}, 0x00, 0x27},
87 // 8 - 320k, 5.25" - 2 heads, 40 tracks, 8 sectors
88 { {2, 40, 8}, 0x00, 0x27},
92 addFloppy(int floppyid, int ftype, int driver)
94 if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
95 dprintf(1, "Bad floppy type %d\n", ftype);
99 struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
104 memset(drive_g, 0, sizeof(*drive_g));
105 drive_g->cntl_id = floppyid;
106 drive_g->type = driver;
107 drive_g->blksize = DISK_SECTOR_SIZE;
108 drive_g->floppy_type = ftype;
109 drive_g->sectors = (u64)-1;
111 memcpy(&drive_g->lchs, &FloppyInfo[ftype].chs
112 , sizeof(FloppyInfo[ftype].chs));
114 map_floppy_drive(drive_g);
119 describe_floppy(struct drive_s *drive_g)
121 printf("drive %c", 'A' + drive_g->cntl_id);
129 dprintf(3, "init floppy drives\n");
131 if (CONFIG_COREBOOT) {
132 // XXX - disable floppies on coreboot for now.
134 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
136 addFloppy(0, type >> 4, DTYPE_FLOPPY);
138 addFloppy(1, type & 0x0f, DTYPE_FLOPPY);
141 outb(0x02, PORT_DMA1_MASK_REG);
143 enable_hwirq(6, entry_0e);
146 // Find a floppy type that matches a given image size.
148 find_floppy_type(u32 size)
151 for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
152 struct chs_s *c = &FloppyInfo[i].chs;
153 if (c->cylinders * c->heads * c->spt * DISK_SECTOR_SIZE == size)
160 /****************************************************************
161 * Low-level floppy IO
162 ****************************************************************/
165 floppy_reset_controller(void)
168 u8 val8 = inb(PORT_FD_DOR);
169 outb(val8 & ~0x04, PORT_FD_DOR);
170 outb(val8 | 0x04, PORT_FD_DOR);
172 // Wait for controller to come out of reset
173 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
178 wait_floppy_irq(void)
183 if (!GET_BDA(floppy_motor_counter))
185 v = GET_BDA(floppy_recalibration_status);
188 // Could use wait_irq() here, but that causes issues on
189 // bochs, so use yield() instead.
194 SET_BDA(floppy_recalibration_status, v);
199 floppy_prepare_controller(u8 floppyid)
201 CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
203 // turn on motor of selected drive, DMA & int enabled, normal operation
204 u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
210 outb(dor, PORT_FD_DOR);
212 // reset the disk motor timeout value of INT 08
213 SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
215 // wait for drive readiness
216 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
224 floppy_pio(u8 *cmd, u8 cmdlen)
226 floppy_prepare_controller(cmd[1] & 1);
228 // send command to controller
230 for (i=0; i<cmdlen; i++)
231 outb(cmd[i], PORT_FD_DATA);
233 int ret = wait_floppy_irq();
235 floppy_reset_controller();
243 floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
245 // es:bx = pointer to where to place information from diskette
246 u32 addr = (u32)op->buf_fl;
248 // check for 64K boundary overrun
250 u32 last_addr = addr + end;
251 if ((addr >> 16) != (last_addr >> 16))
252 return DISK_RET_EBOUNDARY;
254 u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
257 mode_register = 0x46;
259 //DEBUGF("floppy dma c2\n");
260 outb(0x06, PORT_DMA1_MASK_REG);
261 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
262 outb(addr, PORT_DMA_ADDR_2);
263 outb(addr>>8, PORT_DMA_ADDR_2);
264 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
265 outb(end, PORT_DMA_CNT_2);
266 outb(end>>8, PORT_DMA_CNT_2);
268 // port 0b: DMA-1 Mode Register
269 // transfer type=write, channel 2
270 outb(mode_register, PORT_DMA1_MODE_REG);
272 // port 81: DMA-1 Page Register, channel 2
273 outb(addr>>16, PORT_DMA_PAGE_2);
275 outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
277 int ret = floppy_pio(cmd, cmdlen);
279 return DISK_RET_ETIMEOUT;
281 // check port 3f4 for accessibility to status bytes
282 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
283 return DISK_RET_ECONTROLLER;
285 // read 7 return status bytes from controller
287 for (i=0; i<7; i++) {
288 u8 v = inb(PORT_FD_DATA);
290 SET_BDA(floppy_return_status[i], v);
293 return DISK_RET_SUCCESS;
297 /****************************************************************
299 ****************************************************************/
302 set_diskette_current_cyl(u8 floppyid, u8 cyl)
304 SET_BDA(floppy_track[floppyid], cyl);
308 floppy_drive_recal(u8 floppyid)
310 // send Recalibrate command (2 bytes) to controller
312 data[0] = 0x07; // 07: Recalibrate
313 data[1] = floppyid; // 0=drive0, 1=drive1
316 SETBITS_BDA(floppy_recalibration_status, 1<<floppyid);
317 set_diskette_current_cyl(floppyid, 0);
321 floppy_media_sense(struct drive_s *drive_g)
323 // for now cheat and get drive type from CMOS,
324 // assume media is same as drive type
327 // Bitfields for diskette media control:
328 // Bit(s) Description (Table M0028)
329 // 7-6 last data rate set by controller
330 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
331 // 5-4 last diskette drive step rate selected
332 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
333 // 3-2 {data rate at start of operation}
337 // Bitfields for diskette drive media state:
338 // Bit(s) Description (Table M0030)
340 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
341 // 5 double stepping required (e.g. 360kB in 1.2MB)
342 // 4 media type established
343 // 3 drive capable of supporting 4MB media
344 // 2-0 on exit from BIOS, contains
345 // 000 trying 360kB in 360kB
346 // 001 trying 360kB in 1.2MB
347 // 010 trying 1.2MB in 1.2MB
348 // 011 360kB in 360kB established
349 // 100 360kB in 1.2MB established
350 // 101 1.2MB in 1.2MB established
352 // 111 all other formats/drives
354 u8 ftype = GET_GLOBAL(drive_g->floppy_type);
355 SET_BDA(floppy_last_data_rate, GET_GLOBAL(FloppyInfo[ftype].config_data));
356 u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
357 SET_BDA(floppy_media_state[floppyid]
358 , GET_GLOBAL(FloppyInfo[ftype].media_state));
359 return DISK_RET_SUCCESS;
363 check_recal_drive(struct drive_s *drive_g)
365 u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
366 if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid))
367 && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED))
369 return DISK_RET_SUCCESS;
371 // Recalibrate drive.
372 floppy_drive_recal(floppyid);
375 return floppy_media_sense(drive_g);
379 /****************************************************************
381 ****************************************************************/
384 lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
389 u16 nlspt = GET_GLOBAL(op->drive_g->lchs.spt);
390 *sector = tmp % nlspt;
393 u16 nlh = GET_GLOBAL(op->drive_g->lchs.heads);
400 // diskette controller reset
402 floppy_reset(struct disk_op_s *op)
404 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
405 set_diskette_current_cyl(floppyid, 0); // current cylinder
406 return DISK_RET_SUCCESS;
409 // Read Diskette Sectors
411 floppy_read(struct disk_op_s *op)
413 int res = check_recal_drive(op->drive_g);
417 u8 track, sector, head;
418 lba2chs(op, &track, §or, &head);
420 // send read-normal-data command (9 bytes) to controller
421 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
423 data[0] = 0xe6; // e6: read normal data
424 data[1] = (head << 2) | floppyid; // HD DR1 DR2
428 data[5] = FLOPPY_SIZE_CODE;
429 data[6] = sector + op->count - 1; // last sector to read on track
430 data[7] = FLOPPY_GAPLEN;
431 data[8] = FLOPPY_DATALEN;
433 res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
437 if (data[0] & 0xc0) {
438 res = DISK_RET_ECONTROLLER;
442 // ??? should track be new val from return_status[3] ?
443 set_diskette_current_cyl(floppyid, track);
444 return DISK_RET_SUCCESS;
446 op->count = 0; // no sectors read
450 // Write Diskette Sectors
452 floppy_write(struct disk_op_s *op)
454 int res = check_recal_drive(op->drive_g);
458 u8 track, sector, head;
459 lba2chs(op, &track, §or, &head);
461 // send write-normal-data command (9 bytes) to controller
462 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
464 data[0] = 0xc5; // c5: write normal data
465 data[1] = (head << 2) | floppyid; // HD DR1 DR2
469 data[5] = FLOPPY_SIZE_CODE;
470 data[6] = sector + op->count - 1; // last sector to write on track
471 data[7] = FLOPPY_GAPLEN;
472 data[8] = FLOPPY_DATALEN;
474 res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
478 if (data[0] & 0xc0) {
480 res = DISK_RET_EWRITEPROTECT;
482 res = DISK_RET_ECONTROLLER;
486 // ??? should track be new val from return_status[3] ?
487 set_diskette_current_cyl(floppyid, track);
488 return DISK_RET_SUCCESS;
490 op->count = 0; // no sectors read
494 // Verify Diskette Sectors
496 floppy_verify(struct disk_op_s *op)
498 int res = check_recal_drive(op->drive_g);
502 u8 track, sector, head;
503 lba2chs(op, &track, §or, &head);
505 // ??? should track be new val from return_status[3] ?
506 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
507 set_diskette_current_cyl(floppyid, track);
508 return DISK_RET_SUCCESS;
510 op->count = 0; // no sectors read
514 // format diskette track
516 floppy_format(struct disk_op_s *op)
518 int ret = check_recal_drive(op->drive_g);
524 // send format-track command (6 bytes) to controller
525 u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
527 data[0] = 0x4d; // 4d: format track
528 data[1] = (head << 2) | floppyid; // HD DR1 DR2
529 data[2] = FLOPPY_SIZE_CODE;
530 data[3] = op->count; // number of sectors per track
531 data[4] = FLOPPY_FORMAT_GAPLEN;
532 data[5] = FLOPPY_FILLBYTE;
534 ret = floppy_cmd(op, op->count * 4, data, 6);
538 if (data[0] & 0xc0) {
540 return DISK_RET_EWRITEPROTECT;
541 return DISK_RET_ECONTROLLER;
544 set_diskette_current_cyl(floppyid, 0);
545 return DISK_RET_SUCCESS;
549 process_floppy_op(struct disk_op_s *op)
554 switch (op->command) {
556 return floppy_reset(op);
558 return floppy_read(op);
560 return floppy_write(op);
562 return floppy_verify(op);
564 return floppy_format(op);
567 return DISK_RET_EPARAM;
572 /****************************************************************
574 ****************************************************************/
576 // INT 0Eh Diskette Hardware ISR Entry Point
580 debug_isr(DEBUG_ISR_0e);
584 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
585 outb(0x08, PORT_FD_DATA); // sense interrupt status
586 while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
590 } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
592 // diskette interrupt has occurred
593 SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
599 // Called from int08 handler.
606 // time to turn off drive(s)?
607 u8 fcount = GET_BDA(floppy_motor_counter);
610 SET_BDA(floppy_motor_counter, fcount);
613 outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);