grml...
[seabios.git] / src / floppy.c
index 90a7fce36bef795c9ae2ece26dfe0eed2c4410ff..383744a22d9d8fa5debc025053d4d2af2463de5c 100644 (file)
@@ -9,32 +9,38 @@
 #include "disk.h" // DISK_RET_SUCCESS
 #include "config.h" // CONFIG_FLOPPY
 #include "biosvar.h" // SET_BDA
-#include "util.h" // irq_disable
+#include "util.h" // wait_irq
 #include "cmos.h" // inb_cmos
 #include "pic.h" // eoi_pic1
 #include "bregs.h" // struct bregs
+#include "boot.h" // boot_add_floppy
+#include "pci.h" // pci_to_bdf
+#include "pci_ids.h" // PCI_CLASS_BRIDGE_ISA
 
-#define FLOPPY_SECTOR_SIZE 512
-
-#define BX_FLOPPY_ON_CNT 37   /* 2 seconds */
+#define FLOPPY_SIZE_CODE 0x02 // 512 byte sectors
+#define FLOPPY_DATALEN 0xff   // Not used - because size code is 0x02
+#define FLOPPY_MOTOR_TICKS 37 // ~2 seconds
+#define FLOPPY_FILLBYTE 0xf6
+#define FLOPPY_GAPLEN 0x1B
+#define FLOPPY_FORMAT_GAPLEN 0x6c
 
 // New diskette parameter table adding 3 parameters from IBM
 // Since no provisions are made for multiple drive types, most
 // values in this table are ignored.  I set parameters for 1.44M
 // floppy here
-struct floppy_ext_dbt_s diskette_param_table2 VAR16_32 = {
+struct floppy_ext_dbt_s diskette_param_table2 VAR16VISIBLE = {
     .dbt = {
-        .specify1       = 0xAF,
-        .specify2       = 0x02, // head load time 0000001, DMA used
-        .shutoff_ticks  = 0x25,
-        .bps_code       = 0x02,
+        .specify1       = 0xAF, // step rate 12ms, head unload 240ms
+        .specify2       = 0x02, // head load time 4ms, DMA used
+        .shutoff_ticks  = FLOPPY_MOTOR_TICKS, // ~2 seconds
+        .bps_code       = FLOPPY_SIZE_CODE,
         .sectors        = 18,
-        .interblock_len = 0x1B,
-        .data_len       = 0xFF,
-        .gap_len        = 0x6C,
-        .fill_byte      = 0xF6,
-        .settle_time    = 0x0F,
-        .startup_time   = 0x08,
+        .interblock_len = FLOPPY_GAPLEN,
+        .data_len       = FLOPPY_DATALEN,
+        .gap_len        = FLOPPY_FORMAT_GAPLEN,
+        .fill_byte      = FLOPPY_FILLBYTE,
+        .settle_time    = 0x0F, // 15ms
+        .startup_time   = 0x08, // 1 second
     },
     .max_track      = 79,   // maximum track
     .data_rate      = 0,    // data transfer rate
@@ -46,14 +52,14 @@ struct floppy_ext_dbt_s diskette_param_table2 VAR16_32 = {
 // floppy here
 struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7) = {
     .specify1       = 0xAF,
-    .specify2       = 0x02, // head load time 0000001, DMA used
-    .shutoff_ticks  = 0x25,
-    .bps_code       = 0x02,
+    .specify2       = 0x02,
+    .shutoff_ticks  = FLOPPY_MOTOR_TICKS,
+    .bps_code       = FLOPPY_SIZE_CODE,
     .sectors        = 18,
-    .interblock_len = 0x1B,
-    .data_len       = 0xFF,
-    .gap_len        = 0x6C,
-    .fill_byte      = 0xF6,
+    .interblock_len = FLOPPY_GAPLEN,
+    .data_len       = FLOPPY_DATALEN,
+    .gap_len        = FLOPPY_FORMAT_GAPLEN,
+    .fill_byte      = FLOPPY_FILLBYTE,
     .settle_time    = 0x0F,
     .startup_time   = 0x08,
 };
@@ -64,7 +70,7 @@ struct floppyinfo_s {
     u8 media_state;
 };
 
-struct floppyinfo_s FloppyInfo[] VAR16_32 = {
+struct floppyinfo_s FloppyInfo[] VAR16VISIBLE = {
     // Unknown
     { {0, 0, 0}, 0x00, 0x00},
     // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors
@@ -85,33 +91,45 @@ struct floppyinfo_s FloppyInfo[] VAR16_32 = {
     { {2, 40, 8}, 0x00, 0x27},
 };
 
-void
-addFloppy(int floppyid, int ftype, int driver)
+struct drive_s *
+init_floppy(int floppyid, int ftype)
 {
     if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
         dprintf(1, "Bad floppy type %d\n", ftype);
-        return;
+        return NULL;
     }
 
-    int driveid = Drives.drivecount;
-    if (driveid >= ARRAY_SIZE(Drives.drives))
-        return;
-    Drives.drivecount++;
-    memset(&Drives.drives[driveid], 0, sizeof(Drives.drives[0]));
-    Drives.drives[driveid].cntl_id = floppyid;
-    Drives.drives[driveid].type = driver;
-    Drives.drives[driveid].blksize = FLOPPY_SECTOR_SIZE;
-    Drives.drives[driveid].floppy_type = ftype;
-    Drives.drives[driveid].sectors = (u16)-1;
-
-    memcpy(&Drives.drives[driveid].lchs, &FloppyInfo[ftype].chs
+    struct drive_s *drive_g = malloc_fseg(sizeof(*drive_g));
+    if (!drive_g) {
+        warn_noalloc();
+        return NULL;
+    }
+    memset(drive_g, 0, sizeof(*drive_g));
+    drive_g->cntl_id = floppyid;
+    drive_g->type = DTYPE_FLOPPY;
+    drive_g->blksize = DISK_SECTOR_SIZE;
+    drive_g->floppy_type = ftype;
+    drive_g->sectors = (u64)-1;
+
+    memcpy(&drive_g->lchs, &FloppyInfo[ftype].chs
            , sizeof(FloppyInfo[ftype].chs));
+    return drive_g;
+}
 
-    map_floppy_drive(driveid);
+static void
+addFloppy(int floppyid, int ftype)
+{
+    struct drive_s *drive_g = init_floppy(floppyid, ftype);
+    if (!drive_g)
+        return;
+    char *desc = znprintf(MAXDESCSIZE, "Floppy [drive %c]", 'A' + floppyid);
+    struct pci_device *pci = pci_find_class(PCI_CLASS_BRIDGE_ISA); /* isa-to-pci bridge */
+    int prio = bootprio_find_fdc_device(pci, PORT_FD_BASE, floppyid);
+    boot_add_floppy(drive_g, desc, prio);
 }
 
 void
-floppy_setup()
+floppy_setup(void)
 {
     if (! CONFIG_FLOPPY)
         return;
@@ -122,14 +140,14 @@ floppy_setup()
     } else {
         u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
         if (type & 0xf0)
-            addFloppy(0, type >> 4, DTYPE_FLOPPY);
+            addFloppy(0, type >> 4);
         if (type & 0x0f)
-            addFloppy(1, type & 0x0f, DTYPE_FLOPPY);
+            addFloppy(1, type & 0x0f);
     }
 
     outb(0x02, PORT_DMA1_MASK_REG);
 
-    enable_hwirq(6, entry_0e);
+    enable_hwirq(6, FUNC16(entry_0e));
 }
 
 // Find a floppy type that matches a given image size.
@@ -139,7 +157,7 @@ find_floppy_type(u32 size)
     int i;
     for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
         struct chs_s *c = &FloppyInfo[i].chs;
-        if (c->cylinders * c->heads * c->spt * FLOPPY_SECTOR_SIZE == size)
+        if (c->cylinders * c->heads * c->spt * DISK_SECTOR_SIZE == size)
             return i;
     }
     return -1;
@@ -151,7 +169,7 @@ find_floppy_type(u32 size)
  ****************************************************************/
 
 static void
-floppy_reset_controller()
+floppy_reset_controller(void)
 {
     // Reset controller
     u8 val8 = inb(PORT_FD_DOR);
@@ -164,21 +182,20 @@ floppy_reset_controller()
 }
 
 static int
-wait_floppy_irq()
+wait_floppy_irq(void)
 {
-    irq_enable();
+    ASSERT16();
     u8 v;
     for (;;) {
-        if (!GET_BDA(floppy_motor_counter)) {
-            irq_disable();
+        if (!GET_BDA(floppy_motor_counter))
             return -1;
-        }
         v = GET_BDA(floppy_recalibration_status);
         if (v & FRS_TIMEOUT)
             break;
-        cpu_relax();
+        // Could use wait_irq() here, but that causes issues on
+        // bochs, so use yield() instead.
+        yield();
     }
-    irq_disable();
 
     v &= ~FRS_TIMEOUT;
     SET_BDA(floppy_recalibration_status, v);
@@ -200,7 +217,7 @@ floppy_prepare_controller(u8 floppyid)
     outb(dor, PORT_FD_DOR);
 
     // reset the disk motor timeout value of INT 08
-    SET_BDA(floppy_motor_counter, BX_FLOPPY_ON_CNT);
+    SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
 
     // wait for drive readiness
     while ((inb(PORT_FD_STATUS) & 0xc0) != 0x80)
@@ -236,7 +253,8 @@ floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
     u32 addr = (u32)op->buf_fl;
 
     // check for 64K boundary overrun
-    u32 last_addr = addr + count;
+    u16 end = count - 1;
+    u32 last_addr = addr + end;
     if ((addr >> 16) != (last_addr >> 16))
         return DISK_RET_EBOUNDARY;
 
@@ -251,8 +269,8 @@ floppy_cmd(struct disk_op_s *op, u16 count, u8 *cmd, u8 cmdlen)
     outb(addr, PORT_DMA_ADDR_2);
     outb(addr>>8, PORT_DMA_ADDR_2);
     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
-    outb(count, PORT_DMA_CNT_2);
-    outb(count>>8, PORT_DMA_CNT_2);
+    outb(end, PORT_DMA_CNT_2);
+    outb(end>>8, PORT_DMA_CNT_2);
 
     // port 0b: DMA-1 Mode Register
     // transfer type=write, channel 2
@@ -307,7 +325,7 @@ floppy_drive_recal(u8 floppyid)
 }
 
 static int
-floppy_media_sense(u8 driveid)
+floppy_media_sense(struct drive_s *drive_g)
 {
     // for now cheat and get drive type from CMOS,
     // assume media is same as drive type
@@ -340,18 +358,18 @@ floppy_media_sense(u8 driveid)
     //    110 reserved
     //    111 all other formats/drives
 
-    u8 ftype = GET_GLOBAL(Drives.drives[driveid].floppy_type);
+    u8 ftype = GET_GLOBAL(drive_g->floppy_type);
     SET_BDA(floppy_last_data_rate, GET_GLOBAL(FloppyInfo[ftype].config_data));
-    u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
+    u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
     SET_BDA(floppy_media_state[floppyid]
             , GET_GLOBAL(FloppyInfo[ftype].media_state));
     return DISK_RET_SUCCESS;
 }
 
 static int
-check_recal_drive(u8 driveid)
+check_recal_drive(struct drive_s *drive_g)
 {
-    u8 floppyid = GET_GLOBAL(Drives.drives[driveid].cntl_id);
+    u8 floppyid = GET_GLOBAL(drive_g->cntl_id);
     if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid))
         && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED))
         // Media is known.
@@ -361,7 +379,7 @@ check_recal_drive(u8 driveid)
     floppy_drive_recal(floppyid);
 
     // Sense media.
-    return floppy_media_sense(driveid);
+    return floppy_media_sense(drive_g);
 }
 
 
@@ -373,14 +391,13 @@ static void
 lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
 {
     u32 lba = op->lba;
-    u8 driveid = op->driveid;
 
     u32 tmp = lba + 1;
-    u16 nlspt = GET_GLOBAL(Drives.drives[driveid].lchs.spt);
+    u16 nlspt = GET_GLOBAL(op->drive_g->lchs.spt);
     *sector = tmp % nlspt;
 
     tmp /= nlspt;
-    u16 nlh = GET_GLOBAL(Drives.drives[driveid].lchs.heads);
+    u16 nlh = GET_GLOBAL(op->drive_g->lchs.heads);
     *head = tmp % nlh;
 
     tmp /= nlh;
@@ -391,7 +408,7 @@ lba2chs(struct disk_op_s *op, u8 *track, u8 *sector, u8 *head)
 static int
 floppy_reset(struct disk_op_s *op)
 {
-    u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
+    u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
     set_diskette_current_cyl(floppyid, 0); // current cylinder
     return DISK_RET_SUCCESS;
 }
@@ -400,7 +417,7 @@ floppy_reset(struct disk_op_s *op)
 static int
 floppy_read(struct disk_op_s *op)
 {
-    int res = check_recal_drive(op->driveid);
+    int res = check_recal_drive(op->drive_g);
     if (res)
         goto fail;
 
@@ -408,19 +425,19 @@ floppy_read(struct disk_op_s *op)
     lba2chs(op, &track, &sector, &head);
 
     // send read-normal-data command (9 bytes) to controller
-    u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
+    u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
     u8 data[12];
     data[0] = 0xe6; // e6: read normal data
     data[1] = (head << 2) | floppyid; // HD DR1 DR2
     data[2] = track;
     data[3] = head;
     data[4] = sector;
-    data[5] = 2; // 512 byte sector size
+    data[5] = FLOPPY_SIZE_CODE;
     data[6] = sector + op->count - 1; // last sector to read on track
-    data[7] = 0; // Gap length
-    data[8] = 0xff; // Gap length
+    data[7] = FLOPPY_GAPLEN;
+    data[8] = FLOPPY_DATALEN;
 
-    res = floppy_cmd(op, (op->count * FLOPPY_SECTOR_SIZE) - 1, data, 9);
+    res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
     if (res)
         goto fail;
 
@@ -441,7 +458,7 @@ fail:
 static int
 floppy_write(struct disk_op_s *op)
 {
-    int res = check_recal_drive(op->driveid);
+    int res = check_recal_drive(op->drive_g);
     if (res)
         goto fail;
 
@@ -449,19 +466,19 @@ floppy_write(struct disk_op_s *op)
     lba2chs(op, &track, &sector, &head);
 
     // send write-normal-data command (9 bytes) to controller
-    u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
+    u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
     u8 data[12];
     data[0] = 0xc5; // c5: write normal data
     data[1] = (head << 2) | floppyid; // HD DR1 DR2
     data[2] = track;
     data[3] = head;
     data[4] = sector;
-    data[5] = 2; // 512 byte sector size
+    data[5] = FLOPPY_SIZE_CODE;
     data[6] = sector + op->count - 1; // last sector to write on track
-    data[7] = 0; // Gap length
-    data[8] = 0xff; // Gap length
+    data[7] = FLOPPY_GAPLEN;
+    data[8] = FLOPPY_DATALEN;
 
-    res = floppy_cmd(op, (op->count * FLOPPY_SECTOR_SIZE) - 1, data, 9);
+    res = floppy_cmd(op, op->count * DISK_SECTOR_SIZE, data, 9);
     if (res)
         goto fail;
 
@@ -485,7 +502,7 @@ fail:
 static int
 floppy_verify(struct disk_op_s *op)
 {
-    int res = check_recal_drive(op->driveid);
+    int res = check_recal_drive(op->drive_g);
     if (res)
         goto fail;
 
@@ -493,7 +510,7 @@ floppy_verify(struct disk_op_s *op)
     lba2chs(op, &track, &sector, &head);
 
     // ??? should track be new val from return_status[3] ?
-    u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
+    u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
     set_diskette_current_cyl(floppyid, track);
     return DISK_RET_SUCCESS;
 fail:
@@ -505,23 +522,23 @@ fail:
 static int
 floppy_format(struct disk_op_s *op)
 {
-    int ret = check_recal_drive(op->driveid);
+    int ret = check_recal_drive(op->drive_g);
     if (ret)
         return ret;
 
     u8 head = op->lba;
 
     // send format-track command (6 bytes) to controller
-    u8 floppyid = GET_GLOBAL(Drives.drives[op->driveid].cntl_id);
+    u8 floppyid = GET_GLOBAL(op->drive_g->cntl_id);
     u8 data[12];
     data[0] = 0x4d; // 4d: format track
     data[1] = (head << 2) | floppyid; // HD DR1 DR2
-    data[2] = 2; // 512 byte sector size
+    data[2] = FLOPPY_SIZE_CODE;
     data[3] = op->count; // number of sectors per track
-    data[4] = 0; // Gap length
-    data[5] = 0xf6; // Fill byte
+    data[4] = FLOPPY_FORMAT_GAPLEN;
+    data[5] = FLOPPY_FILLBYTE;
 
-    ret = floppy_cmd(op, (op->count * 4) - 1, data, 6);
+    ret = floppy_cmd(op, op->count * 4, data, 6);
     if (ret)
         return ret;
 
@@ -565,7 +582,7 @@ process_floppy_op(struct disk_op_s *op)
 
 // INT 0Eh Diskette Hardware ISR Entry Point
 void VISIBLE16
-handle_0e()
+handle_0e(void)
 {
     debug_isr(DEBUG_ISR_0e);
     if (! CONFIG_FLOPPY)
@@ -588,7 +605,7 @@ done:
 
 // Called from int08 handler.
 void
-floppy_tick()
+floppy_tick(void)
 {
     if (! CONFIG_FLOPPY)
         return;