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" // irq_disable
13 #include "cmos.h" // inb_cmos
14 #include "pic.h" // eoi_pic1
15 #include "bregs.h" // struct bregs
17 #define FLOPPY_SECTOR_SIZE 512
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 VAR16_32 = {
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
44 // Since no provisions are made for multiple drive types, most
45 // values in this table are ignored. I set parameters for 1.44M
47 struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7) = {
49 .specify2 = 0x02, // head load time 0000001, DMA used
50 .shutoff_ticks = 0x25,
53 .interblock_len = 0x1B,
67 struct floppyinfo_s FloppyInfo[] VAR16_32 = {
69 { {0, 0, 0}, 0x00, 0x00},
70 // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors
71 { {2, 40, 9}, 0x00, 0x25},
72 // 2 - 1.2MB, 5.25" - 2 heads, 80 tracks, 15 sectors
73 { {2, 80, 15}, 0x00, 0x25},
74 // 3 - 720KB, 3.5" - 2 heads, 80 tracks, 9 sectors
75 { {2, 80, 9}, 0x00, 0x17},
76 // 4 - 1.44MB, 3.5" - 2 heads, 80 tracks, 18 sectors
77 { {2, 80, 18}, 0x00, 0x17},
78 // 5 - 2.88MB, 3.5" - 2 heads, 80 tracks, 36 sectors
79 { {2, 80, 36}, 0xCC, 0xD7},
80 // 6 - 160k, 5.25" - 1 heads, 40 tracks, 8 sectors
81 { {1, 40, 8}, 0x00, 0x27},
82 // 7 - 180k, 5.25" - 1 heads, 40 tracks, 9 sectors
83 { {1, 40, 9}, 0x00, 0x27},
84 // 8 - 320k, 5.25" - 2 heads, 40 tracks, 8 sectors
85 { {2, 40, 8}, 0x00, 0x27},
89 addFloppy(int floppyid, int ftype, int driver)
91 if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
92 dprintf(1, "Bad floppy type %d\n", ftype);
96 int driveid = Drives.drivecount;
97 if (driveid >= ARRAY_SIZE(Drives.drives))
100 memset(&Drives.drives[driveid], 0, sizeof(Drives.drives[0]));
101 Drives.drives[driveid].cntl_id = floppyid;
102 Drives.drives[driveid].type = driver;
103 Drives.drives[driveid].blksize = FLOPPY_SECTOR_SIZE;
104 Drives.drives[driveid].floppy_type = ftype;
105 Drives.drives[driveid].sectors = (u16)-1;
107 memcpy(&Drives.drives[driveid].lchs, &FloppyInfo[ftype].chs
108 , sizeof(FloppyInfo[ftype].chs));
110 map_floppy_drive(driveid);
118 dprintf(3, "init floppy drives\n");
120 if (CONFIG_COREBOOT) {
121 // XXX - disable floppies on coreboot for now.
123 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
125 addFloppy(0, type >> 4, DTYPE_FLOPPY);
127 addFloppy(1, type & 0x0f, DTYPE_FLOPPY);
130 outb(0x02, PORT_DMA1_MASK_REG);
132 enable_hwirq(6, entry_0e);
135 // Find a floppy type that matches a given image size.
137 find_floppy_type(u32 size)
140 for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
141 struct chs_s *c = &FloppyInfo[i].chs;
142 if (c->cylinders * c->heads * c->spt * FLOPPY_SECTOR_SIZE == size)
149 /****************************************************************
150 * Low-level floppy IO
151 ****************************************************************/
154 floppy_reset_controller()
157 u8 val8 = inb(PORT_FD_DOR);
158 outb(val8 & ~0x04, PORT_FD_DOR);
159 outb(val8 | 0x04, PORT_FD_DOR);
161 // Wait for controller to come out of reset
162 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
172 if (!GET_BDA(floppy_motor_counter)) {
176 v = GET_BDA(floppy_recalibration_status);
184 SET_BDA(floppy_recalibration_status, v);
189 floppy_prepare_controller(u8 floppyid)
191 CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
193 // turn on motor of selected drive, DMA & int enabled, normal operation
194 u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
200 outb(dor, PORT_FD_DOR);
202 // reset the disk motor timeout value of INT 08
203 SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT);
205 // wait for drive readiness
206 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
214 floppy_pio(u8 *cmd, u8 cmdlen)
216 floppy_prepare_controller(cmd[1] & 1);
218 // send command to controller
220 for (i=0; i<cmdlen; i++)
221 outb(cmd[i], PORT_FD_DATA);
223 int ret = wait_floppy_irq();
225 floppy_reset_controller();
233 floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
235 // es:bx = pointer to where to place information from diskette
236 u32 addr = (u32)op->buf_fl;
238 // check for 64K boundary overrun
239 u32 last_addr = addr + count;
240 if ((addr >> 16) != (last_addr >> 16))
241 return DISK_RET_EBOUNDARY;
243 u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
246 mode_register = 0x46;
248 //DEBUGF("floppy dma c2\n");
249 outb(0x06, PORT_DMA1_MASK_REG);
250 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
251 outb(addr, PORT_DMA_ADDR_2);
252 outb(addr>>8, PORT_DMA_ADDR_2);
253 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
254 outb(count, PORT_DMA_CNT_2);
255 outb(count>>8, PORT_DMA_CNT_2);
257 // port 0b: DMA-1 Mode Register
258 // transfer type=write, channel 2
259 outb(mode_register, PORT_DMA1_MODE_REG);
261 // port 81: DMA-1 Page Register, channel 2
262 outb(addr>>16, PORT_DMA_PAGE_2);
264 outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
266 int ret = floppy_pio(cmd, cmdlen);
268 return DISK_RET_ETIMEOUT;
270 // check port 3f4 for accessibility to status bytes
271 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
272 return DISK_RET_ECONTROLLER;
274 // read 7 return status bytes from controller
276 for (i=0; i<7; i++) {
277 u8 v = inb(PORT_FD_DATA);
279 SET_BDA(floppy_return_status[i], v);
282 return DISK_RET_SUCCESS;
286 /****************************************************************
288 ****************************************************************/
291 set_diskette_current_cyl(u8 floppyid, u8 cyl)
293 SET_BDA(floppy_track[floppyid], cyl);
297 floppy_drive_recal(u8 floppyid)
299 // send Recalibrate command (2 bytes) to controller
301 data[0] = 0x07; // 07: Recalibrate
302 data[1] = floppyid; // 0=drive0, 1=drive1
305 SETBITS_BDA(floppy_recalibration_status, 1<<floppyid);
306 set_diskette_current_cyl(floppyid, 0);
310 floppy_media_sense(u8 driveid)
312 // for now cheat and get drive type from CMOS,
313 // assume media is same as drive type
316 // Bitfields for diskette media control:
317 // Bit(s) Description (Table M0028)
318 // 7-6 last data rate set by controller
319 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
320 // 5-4 last diskette drive step rate selected
321 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
322 // 3-2 {data rate at start of operation}
326 // Bitfields for diskette drive media state:
327 // Bit(s) Description (Table M0030)
329 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
330 // 5 double stepping required (e.g. 360kB in 1.2MB)
331 // 4 media type established
332 // 3 drive capable of supporting 4MB media
333 // 2-0 on exit from BIOS, contains
334 // 000 trying 360kB in 360kB
335 // 001 trying 360kB in 1.2MB
336 // 010 trying 1.2MB in 1.2MB
337 // 011 360kB in 360kB established
338 // 100 360kB in 1.2MB established
339 // 101 1.2MB in 1.2MB established
341 // 111 all other formats/drives
343 u8 ftype = GET_GLOBAL(Drives.drives[driveid].floppy_type);
344 SET_BDA(floppy_last_data_rate, GET_GLOBAL(FloppyInfo[ftype].config_data));
345 u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
346 SET_BDA(floppy_media_state[floppyid]
347 , GET_GLOBAL(FloppyInfo[ftype].media_state));
348 return DISK_RET_SUCCESS;
352 check_recal_drive(u8 driveid)
354 u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
355 if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid))
356 && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED))
358 return DISK_RET_SUCCESS;
360 // Recalibrate drive.
361 floppy_drive_recal(floppyid);
364 return floppy_media_sense(driveid);
368 /****************************************************************
370 ****************************************************************/
373 lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
376 u8 driveid = op->driveid;
379 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
380 *sector = tmp % nlspt;
383 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
390 // diskette controller reset
392 floppy_reset(struct disk_op_s *op)
394 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
395 set_diskette_current_cyl(floppyid, 0); // current cylinder
396 return DISK_RET_SUCCESS;
399 // Read Diskette Sectors
401 floppy_read(struct disk_op_s *op)
403 int res = check_recal_drive(op->driveid);
407 u8 track, sector, head;
408 lba2chs(op, &track, §or, &head);
410 // send read-normal-data command (9 bytes) to controller
411 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
413 data[0] = 0xe6; // e6: read normal data
414 data[1] = (head << 2) | floppyid; // HD DR1 DR2
418 data[5] = 2; // 512 byte sector size
419 data[6] = sector + op->count - 1; // last sector to read on track
420 data[7] = 0; // Gap length
421 data[8] = 0xff; // Gap length
423 res = floppy_cmd(op, (op->count * FLOPPY_SECTOR_SIZE) - 1, data, 9);
427 if (data[0] & 0xc0) {
428 res = DISK_RET_ECONTROLLER;
432 // ??? should track be new val from return_status[3] ?
433 set_diskette_current_cyl(floppyid, track);
434 return DISK_RET_SUCCESS;
436 op->count = 0; // no sectors read
440 // Write Diskette Sectors
442 floppy_write(struct disk_op_s *op)
444 int res = check_recal_drive(op->driveid);
448 u8 track, sector, head;
449 lba2chs(op, &track, §or, &head);
451 // send write-normal-data command (9 bytes) to controller
452 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
454 data[0] = 0xc5; // c5: write normal data
455 data[1] = (head << 2) | floppyid; // HD DR1 DR2
459 data[5] = 2; // 512 byte sector size
460 data[6] = sector + op->count - 1; // last sector to write on track
461 data[7] = 0; // Gap length
462 data[8] = 0xff; // Gap length
464 res = floppy_cmd(op, (op->count * FLOPPY_SECTOR_SIZE) - 1, data, 9);
468 if (data[0] & 0xc0) {
470 res = DISK_RET_EWRITEPROTECT;
472 res = DISK_RET_ECONTROLLER;
476 // ??? should track be new val from return_status[3] ?
477 set_diskette_current_cyl(floppyid, track);
478 return DISK_RET_SUCCESS;
480 op->count = 0; // no sectors read
484 // Verify Diskette Sectors
486 floppy_verify(struct disk_op_s *op)
488 int res = check_recal_drive(op->driveid);
492 u8 track, sector, head;
493 lba2chs(op, &track, §or, &head);
495 // ??? should track be new val from return_status[3] ?
496 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
497 set_diskette_current_cyl(floppyid, track);
498 return DISK_RET_SUCCESS;
500 op->count = 0; // no sectors read
504 // format diskette track
506 floppy_format(struct disk_op_s *op)
508 int ret = check_recal_drive(op->driveid);
514 // send format-track command (6 bytes) to controller
515 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
517 data[0] = 0x4d; // 4d: format track
518 data[1] = (head << 2) | floppyid; // HD DR1 DR2
519 data[2] = 2; // 512 byte sector size
520 data[3] = op->count; // number of sectors per track
521 data[4] = 0; // Gap length
522 data[5] = 0xf6; // Fill byte
524 ret = floppy_cmd(op, (op->count * 4) - 1, data, 6);
528 if (data[0] & 0xc0) {
530 return DISK_RET_EWRITEPROTECT;
531 return DISK_RET_ECONTROLLER;
534 set_diskette_current_cyl(floppyid, 0);
535 return DISK_RET_SUCCESS;
539 process_floppy_op(struct disk_op_s *op)
544 switch (op->command) {
546 return floppy_reset(op);
548 return floppy_read(op);
550 return floppy_write(op);
552 return floppy_verify(op);
554 return floppy_format(op);
557 return DISK_RET_EPARAM;
562 /****************************************************************
564 ****************************************************************/
566 // INT 0Eh Diskette Hardware ISR Entry Point
570 debug_isr(DEBUG_ISR_0e);
574 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
575 outb(0x08, PORT_FD_DATA); // sense interrupt status
576 while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
580 } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
582 // diskette interrupt has occurred
583 SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
589 // Called from int08 handler.
596 // time to turn off drive(s)?
597 u8 fcount = GET_BDA(floppy_motor_counter);
600 SET_BDA(floppy_motor_counter, fcount);
603 outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);