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