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
18 #define FLOPPY_SIZE_CODE 0x02 // 512 byte sectors
19 #define FLOPPY_DATALEN 0xff // Not used - because size code is 0x02
20 #define FLOPPY_MOTOR_TICKS 37 // ~2 seconds
21 #define FLOPPY_FILLBYTE 0xf6
22 #define FLOPPY_GAPLEN 0x1B
23 #define FLOPPY_FORMAT_GAPLEN 0x6c
25 // New diskette parameter table adding 3 parameters from IBM
26 // Since no provisions are made for multiple drive types, most
27 // values in this table are ignored. I set parameters for 1.44M
29 struct floppy_ext_dbt_s diskette_param_table2 VAR16VISIBLE = {
31 .specify1 = 0xAF, // step rate 12ms, head unload 240ms
32 .specify2 = 0x02, // head load time 4ms, DMA used
33 .shutoff_ticks = FLOPPY_MOTOR_TICKS, // ~2 seconds
34 .bps_code = FLOPPY_SIZE_CODE,
36 .interblock_len = FLOPPY_GAPLEN,
37 .data_len = FLOPPY_DATALEN,
38 .gap_len = FLOPPY_FORMAT_GAPLEN,
39 .fill_byte = FLOPPY_FILLBYTE,
40 .settle_time = 0x0F, // 15ms
41 .startup_time = 0x08, // 1 second
43 .max_track = 79, // maximum track
44 .data_rate = 0, // data transfer rate
45 .drive_type = 4, // drive type in cmos
48 // Since no provisions are made for multiple drive types, most
49 // values in this table are ignored. I set parameters for 1.44M
51 struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7) = {
54 .shutoff_ticks = FLOPPY_MOTOR_TICKS,
55 .bps_code = FLOPPY_SIZE_CODE,
57 .interblock_len = FLOPPY_GAPLEN,
58 .data_len = FLOPPY_DATALEN,
59 .gap_len = FLOPPY_FORMAT_GAPLEN,
60 .fill_byte = FLOPPY_FILLBYTE,
71 struct floppyinfo_s FloppyInfo[] VAR16VISIBLE = {
73 { {0, 0, 0}, 0x00, 0x00},
74 // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors
75 { {2, 40, 9}, 0x00, 0x25},
76 // 2 - 1.2MB, 5.25" - 2 heads, 80 tracks, 15 sectors
77 { {2, 80, 15}, 0x00, 0x25},
78 // 3 - 720KB, 3.5" - 2 heads, 80 tracks, 9 sectors
79 { {2, 80, 9}, 0x00, 0x17},
80 // 4 - 1.44MB, 3.5" - 2 heads, 80 tracks, 18 sectors
81 { {2, 80, 18}, 0x00, 0x17},
82 // 5 - 2.88MB, 3.5" - 2 heads, 80 tracks, 36 sectors
83 { {2, 80, 36}, 0xCC, 0xD7},
84 // 6 - 160k, 5.25" - 1 heads, 40 tracks, 8 sectors
85 { {1, 40, 8}, 0x00, 0x27},
86 // 7 - 180k, 5.25" - 1 heads, 40 tracks, 9 sectors
87 { {1, 40, 9}, 0x00, 0x27},
88 // 8 - 320k, 5.25" - 2 heads, 40 tracks, 8 sectors
89 { {2, 40, 8}, 0x00, 0x27},
93 addFloppy(int floppyid, int ftype, int driver)
95 if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
96 dprintf(1, "Bad floppy type %d\n", ftype);
100 int driveid = Drives.drivecount;
101 if (driveid >= ARRAY_SIZE(Drives.drives))
104 memset(&Drives.drives[driveid], 0, sizeof(Drives.drives[0]));
105 Drives.drives[driveid].cntl_id = floppyid;
106 Drives.drives[driveid].type = driver;
107 Drives.drives[driveid].blksize = FLOPPY_SECTOR_SIZE;
108 Drives.drives[driveid].floppy_type = ftype;
109 Drives.drives[driveid].sectors = (u16)-1;
111 memcpy(&Drives.drives[driveid].lchs, &FloppyInfo[ftype].chs
112 , sizeof(FloppyInfo[ftype].chs));
114 map_floppy_drive(driveid);
122 dprintf(3, "init floppy drives\n");
124 if (CONFIG_COREBOOT) {
125 // XXX - disable floppies on coreboot for now.
127 u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
129 addFloppy(0, type >> 4, DTYPE_FLOPPY);
131 addFloppy(1, type & 0x0f, DTYPE_FLOPPY);
134 outb(0x02, PORT_DMA1_MASK_REG);
136 enable_hwirq(6, entry_0e);
139 // Find a floppy type that matches a given image size.
141 find_floppy_type(u32 size)
144 for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
145 struct chs_s *c = &FloppyInfo[i].chs;
146 if (c->cylinders * c->heads * c->spt * FLOPPY_SECTOR_SIZE == size)
153 /****************************************************************
154 * Low-level floppy IO
155 ****************************************************************/
158 floppy_reset_controller()
161 u8 val8 = inb(PORT_FD_DOR);
162 outb(val8 & ~0x04, PORT_FD_DOR);
163 outb(val8 | 0x04, PORT_FD_DOR);
165 // Wait for controller to come out of reset
166 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
176 if (!GET_BDA(floppy_motor_counter)) {
180 v = GET_BDA(floppy_recalibration_status);
188 SET_BDA(floppy_recalibration_status, v);
193 floppy_prepare_controller(u8 floppyid)
195 CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
197 // turn on motor of selected drive, DMA & int enabled, normal operation
198 u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
204 outb(dor, PORT_FD_DOR);
206 // reset the disk motor timeout value of INT 08
207 SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
209 // wait for drive readiness
210 while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
218 floppy_pio(u8 *cmd, u8 cmdlen)
220 floppy_prepare_controller(cmd[1] & 1);
222 // send command to controller
224 for (i=0; i<cmdlen; i++)
225 outb(cmd[i], PORT_FD_DATA);
227 int ret = wait_floppy_irq();
229 floppy_reset_controller();
237 floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
239 // es:bx = pointer to where to place information from diskette
240 u32 addr = (u32)op->buf_fl;
242 // check for 64K boundary overrun
244 u32 last_addr = addr + end;
245 if ((addr >> 16) != (last_addr >> 16))
246 return DISK_RET_EBOUNDARY;
248 u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
251 mode_register = 0x46;
253 //DEBUGF("floppy dma c2\n");
254 outb(0x06, PORT_DMA1_MASK_REG);
255 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
256 outb(addr, PORT_DMA_ADDR_2);
257 outb(addr>>8, PORT_DMA_ADDR_2);
258 outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
259 outb(end, PORT_DMA_CNT_2);
260 outb(end>>8, PORT_DMA_CNT_2);
262 // port 0b: DMA-1 Mode Register
263 // transfer type=write, channel 2
264 outb(mode_register, PORT_DMA1_MODE_REG);
266 // port 81: DMA-1 Page Register, channel 2
267 outb(addr>>16, PORT_DMA_PAGE_2);
269 outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
271 int ret = floppy_pio(cmd, cmdlen);
273 return DISK_RET_ETIMEOUT;
275 // check port 3f4 for accessibility to status bytes
276 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
277 return DISK_RET_ECONTROLLER;
279 // read 7 return status bytes from controller
281 for (i=0; i<7; i++) {
282 u8 v = inb(PORT_FD_DATA);
284 SET_BDA(floppy_return_status[i], v);
287 return DISK_RET_SUCCESS;
291 /****************************************************************
293 ****************************************************************/
296 set_diskette_current_cyl(u8 floppyid, u8 cyl)
298 SET_BDA(floppy_track[floppyid], cyl);
302 floppy_drive_recal(u8 floppyid)
304 // send Recalibrate command (2 bytes) to controller
306 data[0] = 0x07; // 07: Recalibrate
307 data[1] = floppyid; // 0=drive0, 1=drive1
310 SETBITS_BDA(floppy_recalibration_status, 1<<floppyid);
311 set_diskette_current_cyl(floppyid, 0);
315 floppy_media_sense(u8 driveid)
317 // for now cheat and get drive type from CMOS,
318 // assume media is same as drive type
321 // Bitfields for diskette media control:
322 // Bit(s) Description (Table M0028)
323 // 7-6 last data rate set by controller
324 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
325 // 5-4 last diskette drive step rate selected
326 // 00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
327 // 3-2 {data rate at start of operation}
331 // Bitfields for diskette drive media state:
332 // Bit(s) Description (Table M0030)
334 // 00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
335 // 5 double stepping required (e.g. 360kB in 1.2MB)
336 // 4 media type established
337 // 3 drive capable of supporting 4MB media
338 // 2-0 on exit from BIOS, contains
339 // 000 trying 360kB in 360kB
340 // 001 trying 360kB in 1.2MB
341 // 010 trying 1.2MB in 1.2MB
342 // 011 360kB in 360kB established
343 // 100 360kB in 1.2MB established
344 // 101 1.2MB in 1.2MB established
346 // 111 all other formats/drives
348 u8 ftype = GET_GLOBAL(Drives.drives[driveid].floppy_type);
349 SET_BDA(floppy_last_data_rate, GET_GLOBAL(FloppyInfo[ftype].config_data));
350 u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
351 SET_BDA(floppy_media_state[floppyid]
352 , GET_GLOBAL(FloppyInfo[ftype].media_state));
353 return DISK_RET_SUCCESS;
357 check_recal_drive(u8 driveid)
359 u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
360 if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid))
361 && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED))
363 return DISK_RET_SUCCESS;
365 // Recalibrate drive.
366 floppy_drive_recal(floppyid);
369 return floppy_media_sense(driveid);
373 /****************************************************************
375 ****************************************************************/
378 lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
381 u8 driveid = op->driveid;
384 u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
385 *sector = tmp % nlspt;
388 u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
395 // diskette controller reset
397 floppy_reset(struct disk_op_s *op)
399 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
400 set_diskette_current_cyl(floppyid, 0); // current cylinder
401 return DISK_RET_SUCCESS;
404 // Read Diskette Sectors
406 floppy_read(struct disk_op_s *op)
408 int res = check_recal_drive(op->driveid);
412 u8 track, sector, head;
413 lba2chs(op, &track, §or, &head);
415 // send read-normal-data command (9 bytes) to controller
416 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
418 data[0] = 0xe6; // e6: read normal data
419 data[1] = (head << 2) | floppyid; // HD DR1 DR2
423 data[5] = FLOPPY_SIZE_CODE;
424 data[6] = sector + op->count - 1; // last sector to read on track
425 data[7] = FLOPPY_GAPLEN;
426 data[8] = FLOPPY_DATALEN;
428 res = floppy_cmd(op, op->count * FLOPPY_SECTOR_SIZE, data, 9);
432 if (data[0] & 0xc0) {
433 res = DISK_RET_ECONTROLLER;
437 // ??? should track be new val from return_status[3] ?
438 set_diskette_current_cyl(floppyid, track);
439 return DISK_RET_SUCCESS;
441 op->count = 0; // no sectors read
445 // Write Diskette Sectors
447 floppy_write(struct disk_op_s *op)
449 int res = check_recal_drive(op->driveid);
453 u8 track, sector, head;
454 lba2chs(op, &track, §or, &head);
456 // send write-normal-data command (9 bytes) to controller
457 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
459 data[0] = 0xc5; // c5: write normal data
460 data[1] = (head << 2) | floppyid; // HD DR1 DR2
464 data[5] = FLOPPY_SIZE_CODE;
465 data[6] = sector + op->count - 1; // last sector to write on track
466 data[7] = FLOPPY_GAPLEN;
467 data[8] = FLOPPY_DATALEN;
469 res = floppy_cmd(op, op->count * FLOPPY_SECTOR_SIZE, data, 9);
473 if (data[0] & 0xc0) {
475 res = DISK_RET_EWRITEPROTECT;
477 res = DISK_RET_ECONTROLLER;
481 // ??? should track be new val from return_status[3] ?
482 set_diskette_current_cyl(floppyid, track);
483 return DISK_RET_SUCCESS;
485 op->count = 0; // no sectors read
489 // Verify Diskette Sectors
491 floppy_verify(struct disk_op_s *op)
493 int res = check_recal_drive(op->driveid);
497 u8 track, sector, head;
498 lba2chs(op, &track, §or, &head);
500 // ??? should track be new val from return_status[3] ?
501 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
502 set_diskette_current_cyl(floppyid, track);
503 return DISK_RET_SUCCESS;
505 op->count = 0; // no sectors read
509 // format diskette track
511 floppy_format(struct disk_op_s *op)
513 int ret = check_recal_drive(op->driveid);
519 // send format-track command (6 bytes) to controller
520 u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
522 data[0] = 0x4d; // 4d: format track
523 data[1] = (head << 2) | floppyid; // HD DR1 DR2
524 data[2] = FLOPPY_SIZE_CODE;
525 data[3] = op->count; // number of sectors per track
526 data[4] = FLOPPY_FORMAT_GAPLEN;
527 data[5] = FLOPPY_FILLBYTE;
529 ret = floppy_cmd(op, op->count * 4, data, 6);
533 if (data[0] & 0xc0) {
535 return DISK_RET_EWRITEPROTECT;
536 return DISK_RET_ECONTROLLER;
539 set_diskette_current_cyl(floppyid, 0);
540 return DISK_RET_SUCCESS;
544 process_floppy_op(struct disk_op_s *op)
549 switch (op->command) {
551 return floppy_reset(op);
553 return floppy_read(op);
555 return floppy_write(op);
557 return floppy_verify(op);
559 return floppy_format(op);
562 return DISK_RET_EPARAM;
567 /****************************************************************
569 ****************************************************************/
571 // INT 0Eh Diskette Hardware ISR Entry Point
575 debug_isr(DEBUG_ISR_0e);
579 if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
580 outb(0x08, PORT_FD_DATA); // sense interrupt status
581 while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
585 } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
587 // diskette interrupt has occurred
588 SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
594 // Called from int08 handler.
601 // time to turn off drive(s)?
602 u8 fcount = GET_BDA(floppy_motor_counter);
605 SET_BDA(floppy_motor_counter, fcount);
608 outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);