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