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