Work around bochs floppy issue with wait_irq().
[seabios.git] / src / floppy.c
1 // 16bit code to access floppy drives.
2 //
3 // Copyright (C) 2008,2009  Kevin O'Connor <kevin@koconnor.net>
4 // Copyright (C) 2002  MandrakeSoft S.A.
5 //
6 // This file may be distributed under the terms of the GNU LGPLv3 license.
7
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
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
23
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
27 // floppy here
28 struct floppy_ext_dbt_s diskette_param_table2 VAR16VISIBLE = {
29     .dbt = {
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,
34         .sectors        = 18,
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
41     },
42     .max_track      = 79,   // maximum track
43     .data_rate      = 0,    // data transfer rate
44     .drive_type     = 4,    // drive type in cmos
45 };
46
47 // Since no provisions are made for multiple drive types, most
48 // values in this table are ignored.  I set parameters for 1.44M
49 // floppy here
50 struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7) = {
51     .specify1       = 0xAF,
52     .specify2       = 0x02,
53     .shutoff_ticks  = FLOPPY_MOTOR_TICKS,
54     .bps_code       = FLOPPY_SIZE_CODE,
55     .sectors        = 18,
56     .interblock_len = FLOPPY_GAPLEN,
57     .data_len       = FLOPPY_DATALEN,
58     .gap_len        = FLOPPY_FORMAT_GAPLEN,
59     .fill_byte      = FLOPPY_FILLBYTE,
60     .settle_time    = 0x0F,
61     .startup_time   = 0x08,
62 };
63
64 struct floppyinfo_s {
65     struct chs_s chs;
66     u8 config_data;
67     u8 media_state;
68 };
69
70 struct floppyinfo_s FloppyInfo[] VAR16VISIBLE = {
71     // Unknown
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},
89 };
90
91 struct drive_s *
92 addFloppy(int floppyid, int ftype, int driver)
93 {
94     if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
95         dprintf(1, "Bad floppy type %d\n", ftype);
96         return NULL;
97     }
98
99     struct drive_s *drive_g = allocDrive();
100     if (!drive_g)
101         return NULL;
102     drive_g->cntl_id = floppyid;
103     drive_g->type = driver;
104     drive_g->blksize = DISK_SECTOR_SIZE;
105     drive_g->floppy_type = ftype;
106     drive_g->sectors = (u64)-1;
107
108     memcpy(&drive_g->lchs, &FloppyInfo[ftype].chs
109            , sizeof(FloppyInfo[ftype].chs));
110
111     map_floppy_drive(drive_g);
112     return drive_g;
113 }
114
115 void
116 describe_floppy(struct drive_s *drive_g)
117 {
118     printf("drive %c", 'A' + drive_g->cntl_id);
119 }
120
121 void
122 floppy_setup(void)
123 {
124     if (! CONFIG_FLOPPY)
125         return;
126     dprintf(3, "init floppy drives\n");
127
128     if (CONFIG_COREBOOT) {
129         // XXX - disable floppies on coreboot for now.
130     } else {
131         u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
132         if (type & 0xf0)
133             addFloppy(0, type >> 4, DTYPE_FLOPPY);
134         if (type & 0x0f)
135             addFloppy(1, type & 0x0f, DTYPE_FLOPPY);
136     }
137
138     outb(0x02, PORT_DMA1_MASK_REG);
139
140     enable_hwirq(6, entry_0e);
141 }
142
143 // Find a floppy type that matches a given image size.
144 int
145 find_floppy_type(u32 size)
146 {
147     int i;
148     for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
149         struct chs_s *c = &FloppyInfo[i].chs;
150         if (c->cylinders * c->heads * c->spt * DISK_SECTOR_SIZE == size)
151             return i;
152     }
153     return -1;
154 }
155
156
157 /****************************************************************
158  * Low-level floppy IO
159  ****************************************************************/
160
161 static void
162 floppy_reset_controller(void)
163 {
164     // Reset controller
165     u8 val8 = inb(PORT_FD_DOR);
166     outb(val8 & ~0x04, PORT_FD_DOR);
167     outb(val8 | 0x04, PORT_FD_DOR);
168
169     // Wait for controller to come out of reset
170     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
171         ;
172 }
173
174 static int
175 wait_floppy_irq(void)
176 {
177     ASSERT16();
178     u8 v;
179     for (;;) {
180         if (!GET_BDA(floppy_motor_counter))
181             return -1;
182         v = GET_BDA(floppy_recalibration_status);
183         if (v & FRS_TIMEOUT)
184             break;
185         // Could use wait_irq() here, but that causes issues on
186         // bochs, so use yield() instead.
187         yield();
188     }
189
190     v &= ~FRS_TIMEOUT;
191     SET_BDA(floppy_recalibration_status, v);
192     return 0;
193 }
194
195 static void
196 floppy_prepare_controller(u8 floppyid)
197 {
198     CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
199
200     // turn on motor of selected drive, DMA & int enabled, normal operation
201     u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
202     u8 dor = 0x10;
203     if (floppyid)
204         dor = 0x20;
205     dor |= 0x0c;
206     dor |= floppyid;
207     outb(dor, PORT_FD_DOR);
208
209     // reset the disk motor timeout value of INT 08
210     SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
211
212     // wait for drive readiness
213     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
214         ;
215
216     if (!prev_reset)
217         wait_floppy_irq();
218 }
219
220 static int
221 floppy_pio(u8 *cmd, u8 cmdlen)
222 {
223     floppy_prepare_controller(cmd[1] & 1);
224
225     // send command to controller
226     u8 i;
227     for (i=0; i<cmdlen; i++)
228         outb(cmd[i], PORT_FD_DATA);
229
230     int ret = wait_floppy_irq();
231     if (ret) {
232         floppy_reset_controller();
233         return -1;
234     }
235
236     return 0;
237 }
238
239 static int
240 floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
241 {
242     // es:bx = pointer to where to place information from diskette
243     u32 addr = (u32)op->buf_fl;
244
245     // check for 64K boundary overrun
246     u16 end = count - 1;
247     u32 last_addr = addr + end;
248     if ((addr >> 16) != (last_addr >> 16))
249         return DISK_RET_EBOUNDARY;
250
251     u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
252     if (cmd[0] == 0xe6)
253         // read
254         mode_register = 0x46;
255
256     //DEBUGF("floppy dma c2\n");
257     outb(0x06, PORT_DMA1_MASK_REG);
258     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
259     outb(addr, PORT_DMA_ADDR_2);
260     outb(addr>>8, PORT_DMA_ADDR_2);
261     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
262     outb(end, PORT_DMA_CNT_2);
263     outb(end>>8, PORT_DMA_CNT_2);
264
265     // port 0b: DMA-1 Mode Register
266     // transfer type=write, channel 2
267     outb(mode_register, PORT_DMA1_MODE_REG);
268
269     // port 81: DMA-1 Page Register, channel 2
270     outb(addr>>16, PORT_DMA_PAGE_2);
271
272     outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
273
274     int ret = floppy_pio(cmd, cmdlen);
275     if (ret)
276         return DISK_RET_ETIMEOUT;
277
278     // check port 3f4 for accessibility to status bytes
279     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
280         return DISK_RET_ECONTROLLER;
281
282     // read 7 return status bytes from controller
283     u8 i;
284     for (i=0; i<7; i++) {
285         u8 v = inb(PORT_FD_DATA);
286         cmd[i] = v;
287         SET_BDA(floppy_return_status[i], v);
288     }
289
290     return DISK_RET_SUCCESS;
291 }
292
293
294 /****************************************************************
295  * Floppy media sense
296  ****************************************************************/
297
298 static inline void
299 set_diskette_current_cyl(u8 floppyid, u8 cyl)
300 {
301     SET_BDA(floppy_track[floppyid], cyl);
302 }
303
304 static void
305 floppy_drive_recal(u8 floppyid)
306 {
307     // send Recalibrate command (2 bytes) to controller
308     u8 data[12];
309     data[0] = 0x07;  // 07: Recalibrate
310     data[1] = floppyid; // 0=drive0, 1=drive1
311     floppy_pio(data, 2);
312
313     SETBITS_BDA(floppy_recalibration_status, 1<<floppyid);
314     set_diskette_current_cyl(floppyid, 0);
315 }
316
317 static int
318 floppy_media_sense(struct drive_s *drive_g)
319 {
320     // for now cheat and get drive type from CMOS,
321     // assume media is same as drive type
322
323     // ** config_data **
324     // Bitfields for diskette media control:
325     // Bit(s)  Description (Table M0028)
326     //  7-6  last data rate set by controller
327     //        00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
328     //  5-4  last diskette drive step rate selected
329     //        00=0Ch, 01=0Dh, 10=0Eh, 11=0Ah
330     //  3-2  {data rate at start of operation}
331     //  1-0  reserved
332
333     // ** media_state **
334     // Bitfields for diskette drive media state:
335     // Bit(s)  Description (Table M0030)
336     //  7-6  data rate
337     //    00=500kbps, 01=300kbps, 10=250kbps, 11=1Mbps
338     //  5  double stepping required (e.g. 360kB in 1.2MB)
339     //  4  media type established
340     //  3  drive capable of supporting 4MB media
341     //  2-0  on exit from BIOS, contains
342     //    000 trying 360kB in 360kB
343     //    001 trying 360kB in 1.2MB
344     //    010 trying 1.2MB in 1.2MB
345     //    011 360kB in 360kB established
346     //    100 360kB in 1.2MB established
347     //    101 1.2MB in 1.2MB established
348     //    110 reserved
349     //    111 all other formats/drives
350
351     u8 ftype = GET_GLOBAL(drive_g->floppy_type);
352     SET_BDA(floppy_last_data_rate, GET_GLOBAL(FloppyInfo[ftype].config_data));
353     u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
354     SET_BDA(floppy_media_state[floppyid]
355             , GET_GLOBAL(FloppyInfo[ftype].media_state));
356     return DISK_RET_SUCCESS;
357 }
358
359 static int
360 check_recal_drive(struct drive_s *drive_g)
361 {
362     u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
363     if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid))
364         && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED))
365         // Media is known.
366         return DISK_RET_SUCCESS;
367
368     // Recalibrate drive.
369     floppy_drive_recal(floppyid);
370
371     // Sense media.
372     return floppy_media_sense(drive_g);
373 }
374
375
376 /****************************************************************
377  * Floppy handlers
378  ****************************************************************/
379
380 static void
381 lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
382 {
383     u32 lba = op->lba;
384
385     u32 tmp = lba + 1;
386     u16 nlspt = GET_GLOBAL(op->drive_g->lchs.spt);
387     *sector = tmp % nlspt;
388
389     tmp /= nlspt;
390     u16 nlh = GET_GLOBAL(op->drive_g->lchs.heads);
391     *head = tmp % nlh;
392
393     tmp /= nlh;
394     *track = tmp;
395 }
396
397 // diskette controller reset
398 static int
399 floppy_reset(struct disk_op_s *op)
400 {
401     u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
402     set_diskette_current_cyl(floppyid, 0); // current cylinder
403     return DISK_RET_SUCCESS;
404 }
405
406 // Read Diskette Sectors
407 static int
408 floppy_read(struct disk_op_s *op)
409 {
410     int res = check_recal_drive(op->drive_g);
411     if (res)
412         goto fail;
413
414     u8 track, sector, head;
415     lba2chs(op, &track, &sector, &head);
416
417     // send read-normal-data command (9 bytes) to controller
418     u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
419     u8 data[12];
420     data[0] = 0xe6; // e6: read normal data
421     data[1] = (head << 2) | floppyid; // HD DR1 DR2
422     data[2] = track;
423     data[3] = head;
424     data[4] = sector;
425     data[5] = FLOPPY_SIZE_CODE;
426     data[6] = sector + op->count - 1; // last sector to read on track
427     data[7] = FLOPPY_GAPLEN;
428     data[8] = FLOPPY_DATALEN;
429
430     res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
431     if (res)
432         goto fail;
433
434     if (data[0] & 0xc0) {
435         res = DISK_RET_ECONTROLLER;
436         goto fail;
437     }
438
439     // ??? should track be new val from return_status[3] ?
440     set_diskette_current_cyl(floppyid, track);
441     return DISK_RET_SUCCESS;
442 fail:
443     op->count = 0; // no sectors read
444     return res;
445 }
446
447 // Write Diskette Sectors
448 static int
449 floppy_write(struct disk_op_s *op)
450 {
451     int res = check_recal_drive(op->drive_g);
452     if (res)
453         goto fail;
454
455     u8 track, sector, head;
456     lba2chs(op, &track, &sector, &head);
457
458     // send write-normal-data command (9 bytes) to controller
459     u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
460     u8 data[12];
461     data[0] = 0xc5; // c5: write normal data
462     data[1] = (head << 2) | floppyid; // HD DR1 DR2
463     data[2] = track;
464     data[3] = head;
465     data[4] = sector;
466     data[5] = FLOPPY_SIZE_CODE;
467     data[6] = sector + op->count - 1; // last sector to write on track
468     data[7] = FLOPPY_GAPLEN;
469     data[8] = FLOPPY_DATALEN;
470
471     res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
472     if (res)
473         goto fail;
474
475     if (data[0] & 0xc0) {
476         if (data[1] & 0x02)
477             res = DISK_RET_EWRITEPROTECT;
478         else
479             res = DISK_RET_ECONTROLLER;
480         goto fail;
481     }
482
483     // ??? should track be new val from return_status[3] ?
484     set_diskette_current_cyl(floppyid, track);
485     return DISK_RET_SUCCESS;
486 fail:
487     op->count = 0; // no sectors read
488     return res;
489 }
490
491 // Verify Diskette Sectors
492 static int
493 floppy_verify(struct disk_op_s *op)
494 {
495     int res = check_recal_drive(op->drive_g);
496     if (res)
497         goto fail;
498
499     u8 track, sector, head;
500     lba2chs(op, &track, &sector, &head);
501
502     // ??? should track be new val from return_status[3] ?
503     u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
504     set_diskette_current_cyl(floppyid, track);
505     return DISK_RET_SUCCESS;
506 fail:
507     op->count = 0; // no sectors read
508     return res;
509 }
510
511 // format diskette track
512 static int
513 floppy_format(struct disk_op_s *op)
514 {
515     int ret = check_recal_drive(op->drive_g);
516     if (ret)
517         return ret;
518
519     u8 head = op->lba;
520
521     // send format-track command (6 bytes) to controller
522     u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
523     u8 data[12];
524     data[0] = 0x4d; // 4d: format track
525     data[1] = (head << 2) | floppyid; // HD DR1 DR2
526     data[2] = FLOPPY_SIZE_CODE;
527     data[3] = op->count; // number of sectors per track
528     data[4] = FLOPPY_FORMAT_GAPLEN;
529     data[5] = FLOPPY_FILLBYTE;
530
531     ret = floppy_cmd(op, op->count * 4, data, 6);
532     if (ret)
533         return ret;
534
535     if (data[0] & 0xc0) {
536         if (data[1] & 0x02)
537             return DISK_RET_EWRITEPROTECT;
538         return DISK_RET_ECONTROLLER;
539     }
540
541     set_diskette_current_cyl(floppyid, 0);
542     return DISK_RET_SUCCESS;
543 }
544
545 int
546 process_floppy_op(struct disk_op_s *op)
547 {
548     if (!CONFIG_FLOPPY)
549         return 0;
550
551     switch (op->command) {
552     case CMD_RESET:
553         return floppy_reset(op);
554     case CMD_READ:
555         return floppy_read(op);
556     case CMD_WRITE:
557         return floppy_write(op);
558     case CMD_VERIFY:
559         return floppy_verify(op);
560     case CMD_FORMAT:
561         return floppy_format(op);
562     default:
563         op->count = 0;
564         return DISK_RET_EPARAM;
565     }
566 }
567
568
569 /****************************************************************
570  * HW irqs
571  ****************************************************************/
572
573 // INT 0Eh Diskette Hardware ISR Entry Point
574 void VISIBLE16
575 handle_0e(void)
576 {
577     debug_isr(DEBUG_ISR_0e);
578     if (! CONFIG_FLOPPY)
579         goto done;
580
581     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
582         outb(0x08, PORT_FD_DATA); // sense interrupt status
583         while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
584             ;
585         do {
586             inb(PORT_FD_DATA);
587         } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
588     }
589     // diskette interrupt has occurred
590     SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
591
592 done:
593     eoi_pic1();
594 }
595
596 // Called from int08 handler.
597 void
598 floppy_tick(void)
599 {
600     if (! CONFIG_FLOPPY)
601         return;
602
603     // time to turn off drive(s)?
604     u8 fcount = GET_BDA(floppy_motor_counter);
605     if (fcount) {
606         fcount--;
607         SET_BDA(floppy_motor_counter, fcount);
608         if (fcount == 0)
609             // turn motor(s) off
610             outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);
611     }
612 }