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