Unify cd emulation access and main disk access code.
[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_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 int
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 -1;
97     }
98
99     int driveid = Drives.drivecount;
100     if (driveid >= ARRAY_SIZE(Drives.drives))
101         return -1;
102     Drives.drivecount++;
103     memset(&Drives.drives[driveid], 0, sizeof(Drives.drives[0]));
104     Drives.drives[driveid].cntl_id = floppyid;
105     Drives.drives[driveid].type = driver;
106     Drives.drives[driveid].blksize = DISK_SECTOR_SIZE;
107     Drives.drives[driveid].floppy_type = ftype;
108     Drives.drives[driveid].sectors = (u64)-1;
109
110     memcpy(&Drives.drives[driveid].lchs, &FloppyInfo[ftype].chs
111            , sizeof(FloppyInfo[ftype].chs));
112
113     map_floppy_drive(driveid);
114     return driveid;
115 }
116
117 void
118 describe_floppy(int driveid)
119 {
120     printf("drive %c", 'A' + Drives.drives[driveid].cntl_id);
121 }
122
123 void
124 floppy_setup()
125 {
126     if (! CONFIG_FLOPPY)
127         return;
128     dprintf(3, "init floppy drives\n");
129
130     if (CONFIG_COREBOOT) {
131         // XXX - disable floppies on coreboot for now.
132     } else {
133         u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
134         if (type & 0xf0)
135             addFloppy(0, type >> 4, DTYPE_FLOPPY);
136         if (type & 0x0f)
137             addFloppy(1, type & 0x0f, DTYPE_FLOPPY);
138     }
139
140     outb(0x02, PORT_DMA1_MASK_REG);
141
142     enable_hwirq(6, entry_0e);
143 }
144
145 // Find a floppy type that matches a given image size.
146 int
147 find_floppy_type(u32 size)
148 {
149     int i;
150     for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
151         struct chs_s *c = &FloppyInfo[i].chs;
152         if (c->cylinders * c->heads * c->spt * DISK_SECTOR_SIZE == size)
153             return i;
154     }
155     return -1;
156 }
157
158
159 /****************************************************************
160  * Low-level floppy IO
161  ****************************************************************/
162
163 static void
164 floppy_reset_controller()
165 {
166     // Reset controller
167     u8 val8 = inb(PORT_FD_DOR);
168     outb(val8 & ~0x04, PORT_FD_DOR);
169     outb(val8 | 0x04, PORT_FD_DOR);
170
171     // Wait for controller to come out of reset
172     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
173         ;
174 }
175
176 static int
177 wait_floppy_irq()
178 {
179     irq_enable();
180     u8 v;
181     for (;;) {
182         if (!GET_BDA(floppy_motor_counter)) {
183             irq_disable();
184             return -1;
185         }
186         v = GET_BDA(floppy_recalibration_status);
187         if (v & FRS_TIMEOUT)
188             break;
189         cpu_relax();
190     }
191     irq_disable();
192
193     v &= ~FRS_TIMEOUT;
194     SET_BDA(floppy_recalibration_status, v);
195     return 0;
196 }
197
198 static void
199 floppy_prepare_controller(u8 floppyid)
200 {
201     CLEARBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
202
203     // turn on motor of selected drive, DMA & int enabled, normal operation
204     u8 prev_reset = inb(PORT_FD_DOR) & 0x04;
205     u8 dor = 0x10;
206     if (floppyid)
207         dor = 0x20;
208     dor |= 0x0c;
209     dor |= floppyid;
210     outb(dor, PORT_FD_DOR);
211
212     // reset the disk motor timeout value of INT 08
213     SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
214
215     // wait for drive readiness
216     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
217         ;
218
219     if (!prev_reset)
220         wait_floppy_irq();
221 }
222
223 static int
224 floppy_pio(u8 *cmd, u8 cmdlen)
225 {
226     floppy_prepare_controller(cmd[1] & 1);
227
228     // send command to controller
229     u8 i;
230     for (i=0; i<cmdlen; i++)
231         outb(cmd[i], PORT_FD_DATA);
232
233     int ret = wait_floppy_irq();
234     if (ret) {
235         floppy_reset_controller();
236         return -1;
237     }
238
239     return 0;
240 }
241
242 static int
243 floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
244 {
245     // es:bx = pointer to where to place information from diskette
246     u32 addr = (u32)op->buf_fl;
247
248     // check for 64K boundary overrun
249     u16 end = count - 1;
250     u32 last_addr = addr + end;
251     if ((addr >> 16) != (last_addr >> 16))
252         return DISK_RET_EBOUNDARY;
253
254     u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
255     if (cmd[0] == 0xe6)
256         // read
257         mode_register = 0x46;
258
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);
267
268     // port 0b: DMA-1 Mode Register
269     // transfer type=write, channel 2
270     outb(mode_register, PORT_DMA1_MODE_REG);
271
272     // port 81: DMA-1 Page Register, channel 2
273     outb(addr>>16, PORT_DMA_PAGE_2);
274
275     outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
276
277     int ret = floppy_pio(cmd, cmdlen);
278     if (ret)
279         return DISK_RET_ETIMEOUT;
280
281     // check port 3f4 for accessibility to status bytes
282     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
283         return DISK_RET_ECONTROLLER;
284
285     // read 7 return status bytes from controller
286     u8 i;
287     for (i=0; i<7; i++) {
288         u8 v = inb(PORT_FD_DATA);
289         cmd[i] = v;
290         SET_BDA(floppy_return_status[i], v);
291     }
292
293     return DISK_RET_SUCCESS;
294 }
295
296
297 /****************************************************************
298  * Floppy media sense
299  ****************************************************************/
300
301 static inline void
302 set_diskette_current_cyl(u8 floppyid, u8 cyl)
303 {
304     SET_BDA(floppy_track[floppyid], cyl);
305 }
306
307 static void
308 floppy_drive_recal(u8 floppyid)
309 {
310     // send Recalibrate command (2 bytes) to controller
311     u8 data[12];
312     data[0] = 0x07;  // 07: Recalibrate
313     data[1] = floppyid; // 0=drive0, 1=drive1
314     floppy_pio(data, 2);
315
316     SETBITS_BDA(floppy_recalibration_status, 1<<floppyid);
317     set_diskette_current_cyl(floppyid, 0);
318 }
319
320 static int
321 floppy_media_sense(u8 driveid)
322 {
323     // for now cheat and get drive type from CMOS,
324     // assume media is same as drive type
325
326     // ** config_data **
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}
334     //  1-0  reserved
335
336     // ** media_state **
337     // Bitfields for diskette drive media state:
338     // Bit(s)  Description (Table M0030)
339     //  7-6  data rate
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
351     //    110 reserved
352     //    111 all other formats/drives
353
354     u8 ftype = GET_GLOBAL(Drives.drives[driveid].floppy_type);
355     SET_BDA(floppy_last_data_rate, GET_GLOBAL(FloppyInfo[ftype].config_data));
356     u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
357     SET_BDA(floppy_media_state[floppyid]
358             , GET_GLOBAL(FloppyInfo[ftype].media_state));
359     return DISK_RET_SUCCESS;
360 }
361
362 static int
363 check_recal_drive(u8 driveid)
364 {
365     u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
366     if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid))
367         && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED))
368         // Media is known.
369         return DISK_RET_SUCCESS;
370
371     // Recalibrate drive.
372     floppy_drive_recal(floppyid);
373
374     // Sense media.
375     return floppy_media_sense(driveid);
376 }
377
378
379 /****************************************************************
380  * Floppy handlers
381  ****************************************************************/
382
383 static void
384 lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
385 {
386     u32 lba = op->lba;
387     u8 driveid = op->driveid;
388
389     u32 tmp = lba + 1;
390     u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
391     *sector = tmp % nlspt;
392
393     tmp /= nlspt;
394     u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
395     *head = tmp % nlh;
396
397     tmp /= nlh;
398     *track = tmp;
399 }
400
401 // diskette controller reset
402 static int
403 floppy_reset(struct disk_op_s *op)
404 {
405     u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
406     set_diskette_current_cyl(floppyid, 0); // current cylinder
407     return DISK_RET_SUCCESS;
408 }
409
410 // Read Diskette Sectors
411 static int
412 floppy_read(struct disk_op_s *op)
413 {
414     int res = check_recal_drive(op->driveid);
415     if (res)
416         goto fail;
417
418     u8 track, sector, head;
419     lba2chs(op, &track, &sector, &head);
420
421     // send read-normal-data command (9 bytes) to controller
422     u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
423     u8 data[12];
424     data[0] = 0xe6; // e6: read normal data
425     data[1] = (head << 2) | floppyid; // HD DR1 DR2
426     data[2] = track;
427     data[3] = head;
428     data[4] = sector;
429     data[5] = FLOPPY_SIZE_CODE;
430     data[6] = sector + op->count - 1; // last sector to read on track
431     data[7] = FLOPPY_GAPLEN;
432     data[8] = FLOPPY_DATALEN;
433
434     res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
435     if (res)
436         goto fail;
437
438     if (data[0] & 0xc0) {
439         res = DISK_RET_ECONTROLLER;
440         goto fail;
441     }
442
443     // ??? should track be new val from return_status[3] ?
444     set_diskette_current_cyl(floppyid, track);
445     return DISK_RET_SUCCESS;
446 fail:
447     op->count = 0; // no sectors read
448     return res;
449 }
450
451 // Write Diskette Sectors
452 static int
453 floppy_write(struct disk_op_s *op)
454 {
455     int res = check_recal_drive(op->driveid);
456     if (res)
457         goto fail;
458
459     u8 track, sector, head;
460     lba2chs(op, &track, &sector, &head);
461
462     // send write-normal-data command (9 bytes) to controller
463     u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
464     u8 data[12];
465     data[0] = 0xc5; // c5: write normal data
466     data[1] = (head << 2) | floppyid; // HD DR1 DR2
467     data[2] = track;
468     data[3] = head;
469     data[4] = sector;
470     data[5] = FLOPPY_SIZE_CODE;
471     data[6] = sector + op->count - 1; // last sector to write on track
472     data[7] = FLOPPY_GAPLEN;
473     data[8] = FLOPPY_DATALEN;
474
475     res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
476     if (res)
477         goto fail;
478
479     if (data[0] & 0xc0) {
480         if (data[1] & 0x02)
481             res = DISK_RET_EWRITEPROTECT;
482         else
483             res = DISK_RET_ECONTROLLER;
484         goto fail;
485     }
486
487     // ??? should track be new val from return_status[3] ?
488     set_diskette_current_cyl(floppyid, track);
489     return DISK_RET_SUCCESS;
490 fail:
491     op->count = 0; // no sectors read
492     return res;
493 }
494
495 // Verify Diskette Sectors
496 static int
497 floppy_verify(struct disk_op_s *op)
498 {
499     int res = check_recal_drive(op->driveid);
500     if (res)
501         goto fail;
502
503     u8 track, sector, head;
504     lba2chs(op, &track, &sector, &head);
505
506     // ??? should track be new val from return_status[3] ?
507     u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
508     set_diskette_current_cyl(floppyid, track);
509     return DISK_RET_SUCCESS;
510 fail:
511     op->count = 0; // no sectors read
512     return res;
513 }
514
515 // format diskette track
516 static int
517 floppy_format(struct disk_op_s *op)
518 {
519     int ret = check_recal_drive(op->driveid);
520     if (ret)
521         return ret;
522
523     u8 head = op->lba;
524
525     // send format-track command (6 bytes) to controller
526     u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
527     u8 data[12];
528     data[0] = 0x4d; // 4d: format track
529     data[1] = (head << 2) | floppyid; // HD DR1 DR2
530     data[2] = FLOPPY_SIZE_CODE;
531     data[3] = op->count; // number of sectors per track
532     data[4] = FLOPPY_FORMAT_GAPLEN;
533     data[5] = FLOPPY_FILLBYTE;
534
535     ret = floppy_cmd(op, op->count * 4, data, 6);
536     if (ret)
537         return ret;
538
539     if (data[0] & 0xc0) {
540         if (data[1] & 0x02)
541             return DISK_RET_EWRITEPROTECT;
542         return DISK_RET_ECONTROLLER;
543     }
544
545     set_diskette_current_cyl(floppyid, 0);
546     return DISK_RET_SUCCESS;
547 }
548
549 int
550 process_floppy_op(struct disk_op_s *op)
551 {
552     if (!CONFIG_FLOPPY)
553         return 0;
554
555     switch (op->command) {
556     case CMD_RESET:
557         return floppy_reset(op);
558     case CMD_READ:
559         return floppy_read(op);
560     case CMD_WRITE:
561         return floppy_write(op);
562     case CMD_VERIFY:
563         return floppy_verify(op);
564     case CMD_FORMAT:
565         return floppy_format(op);
566     default:
567         op->count = 0;
568         return DISK_RET_EPARAM;
569     }
570 }
571
572
573 /****************************************************************
574  * HW irqs
575  ****************************************************************/
576
577 // INT 0Eh Diskette Hardware ISR Entry Point
578 void VISIBLE16
579 handle_0e()
580 {
581     debug_isr(DEBUG_ISR_0e);
582     if (! CONFIG_FLOPPY)
583         goto done;
584
585     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
586         outb(0x08, PORT_FD_DATA); // sense interrupt status
587         while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
588             ;
589         do {
590             inb(PORT_FD_DATA);
591         } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
592     }
593     // diskette interrupt has occurred
594     SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
595
596 done:
597     eoi_pic1();
598 }
599
600 // Called from int08 handler.
601 void
602 floppy_tick()
603 {
604     if (! CONFIG_FLOPPY)
605         return;
606
607     // time to turn off drive(s)?
608     u8 fcount = GET_BDA(floppy_motor_counter);
609     if (fcount) {
610         fcount--;
611         SET_BDA(floppy_motor_counter, fcount);
612         if (fcount == 0)
613             // turn motor(s) off
614             outb(inb(PORT_FD_DOR) & 0xcf, PORT_FD_DOR);
615     }
616 }