Change license from GPLv3 to LGPLv3.
[seabios.git] / src / floppy.c
index fb4e2b5e894a78b85cc6304c5e5a0c5faf524e94..6214acba21ea2d083bb646b65fd476e7342915b3 100644 (file)
@@ -3,63 +3,77 @@
 // Copyright (C) 2008  Kevin O'Connor <kevin@koconnor.net>
 // Copyright (C) 2002  MandrakeSoft S.A.
 //
-// This file may be distributed under the terms of the GNU GPLv3 license.
+// This file may be distributed under the terms of the GNU LGPLv3 license.
 
 #include "types.h" // u8
 #include "disk.h" // DISK_RET_SUCCESS
 #include "config.h" // CONFIG_FLOPPY_SUPPORT
-#include "biosvar.h" // struct bregs
+#include "biosvar.h" // SET_BDA
 #include "util.h" // irq_disable
 #include "cmos.h" // inb_cmos
+#include "pic.h" // eoi_pic1
+#include "bregs.h" // struct bregs
 
 #define BX_FLOPPY_ON_CNT 37   /* 2 seconds */
 
-////.org 0xefc7
-// Since no provisions are made for multiple drive types, most
-// values in this table are ignored.  I set parameters for 1.44M
-// floppy here
-char diskette_param_table[11] = {
-    0xAF,
-    0x02, // head load time 0000001, DMA used
-    0x25,
-    0x02,
-    18,
-    0x1B,
-    0xFF,
-    0x6C,
-    0xF6,
-    0x0F,
-    0x08,
-};
-
 // 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
-char diskette_param_table2[14] VISIBLE = {
-    0xAF,
-    0x02, // head load time 0000001, DMA used
-    0x25,
-    0x02,
-    18,
-    0x1B,
-    0xFF,
-    0x6C,
-    0xF6,
-    0x0F,
-    0x08,
-    79,   // maximum track
-    0,    // data transfer rate
-    4,    // drive type in cmos
+struct floppy_ext_dbt_s diskette_param_table2 VAR16_32 = {
+    .dbt = {
+        .specify1       = 0xAF,
+        .specify2       = 0x02, // head load time 0000001, DMA used
+        .shutoff_ticks  = 0x25,
+        .bps_code       = 0x02,
+        .sectors        = 18,
+        .interblock_len = 0x1B,
+        .data_len       = 0xFF,
+        .gap_len        = 0x6C,
+        .fill_byte      = 0xF6,
+        .settle_time    = 0x0F,
+        .startup_time   = 0x08,
+    },
+    .max_track      = 79,   // maximum track
+    .data_rate      = 0,    // data transfer rate
+    .drive_type     = 4,    // drive type in cmos
 };
 
-// Oddities:
-//   Return codes vary greatly - AL not cleared consistenlty, BDA return
-//      status not set consistently, sometimes panics.
-//   Extra outb(0x000a, 0x02) in read?
-//   Does not disable interrupts on failure paths.
-//   numfloppies used before set in int_1308
-//   int_1305 verifies track but doesn't use it?
+void
+floppy_drive_setup()
+{
+    if (! CONFIG_FLOPPY_SUPPORT)
+        return;
+    dprintf(3, "init floppy drives\n");
+    if (CONFIG_COREBOOT)
+        // XXX - disable floppies on coreboot for now.
+        outb_cmos(0, CMOS_FLOPPY_DRIVE_TYPE);
+    u8 type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
+    u8 out = 0;
+    u8 num_floppies = 0;
+
+    if (type & 0xf0) {
+        out |= 0x07;
+        num_floppies++;
+    }
+    if (type & 0x0f) {
+        out |= 0x70;
+        num_floppies++;
+    }
+    SET_BDA(floppy_harddisk_info, out);
+
+    // Update equipment word bits for floppy
+    if (num_floppies == 1)
+        // 1 drive, ready for boot
+        SETBITS_BDA(equipment_list_flags, 0x01);
+    else if (num_floppies == 2)
+        // 2 drives, ready for boot
+        SETBITS_BDA(equipment_list_flags, 0x41);
+
+    outb(0x02, PORT_DMA1_MASK_REG);
+
+    enable_hwirq(6, entry_0e);
+}
 
 static inline void
 set_diskette_current_cyl(u8 drive, u8 cyl)
@@ -130,9 +144,12 @@ floppy_prepare_controller(u8 drive)
     if (prev_reset == 0) {
         irq_enable();
         u8 v;
-        do {
+        for (;;) {
             v = GET_BDA(floppy_recalibration_status);
-        } while ((v & FRS_TIMEOUT) == 0);
+            if (v & FRS_TIMEOUT)
+                break;
+            cpu_relax();
+        }
         irq_disable();
 
         v &= ~FRS_TIMEOUT;
@@ -140,7 +157,7 @@ floppy_prepare_controller(u8 drive)
     }
 }
 
-static u8
+static int
 floppy_pio(u8 *cmd, u8 cmdlen)
 {
     floppy_prepare_controller(cmd[1] & 1);
@@ -152,54 +169,62 @@ floppy_pio(u8 *cmd, u8 cmdlen)
 
     irq_enable();
     u8 v;
-    do {
+    for (;;) {
         if (!GET_BDA(floppy_motor_counter)) {
             irq_disable();
             floppy_reset_controller();
-            return DISK_RET_ETIMEOUT;
+            return -1;
         }
         v = GET_BDA(floppy_recalibration_status);
-    } while (!(v & FRS_TIMEOUT));
+        if (v & FRS_TIMEOUT)
+            break;
+        cpu_relax();
+    }
     irq_disable();
 
     v &= ~FRS_TIMEOUT;
     SET_BDA(floppy_recalibration_status, v);
 
-    if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
-        BX_PANIC("int13_diskette: ctrl not ready\n");
-
     return 0;
 }
 
-static u8
+#define floppy_ret(regs, code)                                  \
+    __floppy_ret((regs), (code) | (__LINE__ << 8), __func__)
+
+void
+__floppy_ret(struct bregs *regs, u32 linecode, const char *fname)
+{
+    u8 code = linecode;
+    SET_BDA(floppy_last_status, code);
+    if (code)
+        __set_code_fail(regs, linecode, fname);
+    else
+        set_code_success(regs);
+}
+
+static int
 floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
 {
     // es:bx = pointer to where to place information from diskette
-    // port 04: DMA-1 base and current address, channel 2
-    // port 05: DMA-1 base and current count, channel 2
-    u16 page = regs->es >> 12;   // upper 4 bits
-    u16 base_es = regs->es << 4; // lower 16bits contributed by ES
-    u16 base_address = base_es + regs->bx; // lower 16 bits of address
-    // contributed by ES:BX
-    if (base_address < base_es)
-        // in case of carry, adjust page by 1
-        page++;
+    u32 addr = (u32)MAKE_FARPTR(regs->es, regs->bx);
 
     // check for 64K boundary overrun
-    u16 last_addr = base_address + count;
-    if (last_addr < base_address)
-        return DISK_RET_EBOUNDARY;
+    u32 last_addr = addr + count;
+    if ((addr >> 16) != (last_addr >> 16)) {
+        floppy_ret(regs, DISK_RET_EBOUNDARY);
+        return -1;
+    }
 
     u8 mode_register = 0x4a; // single mode, increment, autoinit disable,
     if (cmd[0] == 0xe6)
         // read
         mode_register = 0x46;
 
-    DEBUGF("floppy dma c2");
+    //DEBUGF("floppy dma c2\n");
     outb(0x06, PORT_DMA1_MASK_REG);
     outb(0x00, PORT_DMA1_CLEAR_FF_REG); // clear flip-flop
-    outb(base_address, PORT_DMA_ADDR_2);
-    outb(base_address>>8, PORT_DMA_ADDR_2);
+    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);
@@ -209,13 +234,19 @@ floppy_cmd(struct bregs *regs, u16 count, u8 *cmd, u8 cmdlen)
     outb(mode_register, PORT_DMA1_MODE_REG);
 
     // port 81: DMA-1 Page Register, channel 2
-    outb(page, PORT_DMA_PAGE_2);
+    outb(addr>>16, PORT_DMA_PAGE_2);
 
     outb(0x02, PORT_DMA1_MASK_REG); // unmask channel 2
 
-    u8 ret = floppy_pio(cmd, cmdlen);
-    if (ret)
-        return ret;
+    int ret = floppy_pio(cmd, cmdlen);
+    if (ret) {
+        floppy_ret(regs, DISK_RET_ETIMEOUT);
+        return -1;
+    }
+
+    // check port 3f4 for accessibility to status bytes
+    if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
+        BX_PANIC("int13_diskette: ctrl not ready\n");
 
     // read 7 return status bytes from controller
     u8 i;
@@ -343,34 +374,20 @@ floppy_media_sense(u8 drive)
     return rv;
 }
 
-static inline void
-floppy_ret(struct bregs *regs, u8 code)
-{
-    regs->ah = code;
-    SET_BDA(floppy_last_status, code);
-    set_cf(regs, code);
-}
-
-static inline void
-floppy_fail(struct bregs *regs, u8 code)
-{
-    regs->al = 0; // no sectors read
-    floppy_ret(regs, code);
-}
-
-static u16
+static int
 check_drive(struct bregs *regs, u8 drive)
 {
     // see if drive exists
     if (drive > 1 || !get_drive_type(drive)) {
-        floppy_fail(regs, DISK_RET_ETIMEOUT);
-        return 1;
+        // XXX - return type doesn't match
+        floppy_ret(regs, DISK_RET_ETIMEOUT);
+        return -1;
     }
 
     // see if media in drive, and type is known
     if (floppy_media_known(drive) == 0 && floppy_media_sense(drive) == 0) {
-        floppy_fail(regs, DISK_RET_EMEDIA);
-        return 1;
+        floppy_ret(regs, DISK_RET_EMEDIA);
+        return -1;
     }
     return 0;
 }
@@ -405,7 +422,7 @@ static void
 floppy_1302(struct bregs *regs, u8 drive)
 {
     if (check_drive(regs, drive))
-        return;
+        goto fail;
 
     u8 num_sectors = regs->al;
     u8 track       = regs->ch;
@@ -414,9 +431,8 @@ floppy_1302(struct bregs *regs, u8 drive)
 
     if (head > 1 || sector == 0 || num_sectors == 0
         || track > 79 || num_sectors > 72) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
-        return;
+        floppy_ret(regs, DISK_RET_EPARAM);
+        goto fail;
     }
 
     // send read-normal-data command (9 bytes) to controller
@@ -431,21 +447,22 @@ floppy_1302(struct bregs *regs, u8 drive)
     data[7] = 0; // Gap length
     data[8] = 0xff; // Gap length
 
-    u16 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
-    if (ret) {
-        floppy_fail(regs, ret);
-        return;
-    }
+    int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
+    if (ret)
+        goto fail;
 
     if (data[0] & 0xc0) {
-        floppy_fail(regs, DISK_RET_ECONTROLLER);
-        return;
+        floppy_ret(regs, DISK_RET_ECONTROLLER);
+        goto fail;
     }
 
     // ??? should track be new val from return_status[3] ?
     set_diskette_current_cyl(drive, track);
     // AL = number of sectors read (same value as passed)
     floppy_ret(regs, DISK_RET_SUCCESS);
+    return;
+fail:
+    regs->al = 0; // no sectors read
 }
 
 // Write Diskette Sectors
@@ -453,7 +470,7 @@ static void
 floppy_1303(struct bregs *regs, u8 drive)
 {
     if (check_drive(regs, drive))
-        return;
+        goto fail;
 
     u8 num_sectors = regs->al;
     u8 track       = regs->ch;
@@ -462,9 +479,8 @@ floppy_1303(struct bregs *regs, u8 drive)
 
     if (head > 1 || sector == 0 || num_sectors == 0
         || track > 79 || num_sectors > 72) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
-        return;
+        floppy_ret(regs, DISK_RET_EPARAM);
+        goto fail;
     }
 
     // send write-normal-data command (9 bytes) to controller
@@ -479,17 +495,14 @@ floppy_1303(struct bregs *regs, u8 drive)
     data[7] = 0; // Gap length
     data[8] = 0xff; // Gap length
 
-    u8 ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
-    if (ret) {
-        floppy_fail(regs, ret);
-        return;
-    }
+    int ret = floppy_cmd(regs, (num_sectors * 512) - 1, data, 9);
+    if (ret)
+        goto fail;
 
     if (data[0] & 0xc0) {
         if (data[1] & 0x02) {
-            regs->ax = 0x0300;
-            set_cf(regs, 1);
-            return;
+            floppy_ret(regs, DISK_RET_EWRITEPROTECT);
+            goto fail;
         }
         BX_PANIC("int13_diskette_function: read error\n");
     }
@@ -498,6 +511,9 @@ floppy_1303(struct bregs *regs, u8 drive)
     set_diskette_current_cyl(drive, track);
     // AL = number of sectors read (same value as passed)
     floppy_ret(regs, DISK_RET_SUCCESS);
+    return;
+fail:
+    regs->al = 0; // no sectors read
 }
 
 // Verify Diskette Sectors
@@ -505,7 +521,7 @@ static void
 floppy_1304(struct bregs *regs, u8 drive)
 {
     if (check_drive(regs, drive))
-        return;
+        goto fail;
 
     u8 num_sectors = regs->al;
     u8 track       = regs->ch;
@@ -514,22 +530,24 @@ floppy_1304(struct bregs *regs, u8 drive)
 
     if (head > 1 || sector == 0 || num_sectors == 0
         || track > 79 || num_sectors > 72) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
-        return;
+        floppy_ret(regs, DISK_RET_EPARAM);
+        goto fail;
     }
 
     // ??? should track be new val from return_status[3] ?
     set_diskette_current_cyl(drive, track);
     // AL = number of sectors verified (same value as passed)
     floppy_ret(regs, DISK_RET_SUCCESS);
+    return;
+fail:
+    regs->al = 0; // no sectors read
 }
 
 // format diskette track
 static void
 floppy_1305(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f05\n");
+    dprintf(3, "floppy f05\n");
 
     if (check_drive(regs, drive))
         return;
@@ -538,8 +556,7 @@ floppy_1305(struct bregs *regs, u8 drive)
     u8 head        = regs->dh;
 
     if (head > 1 || num_sectors == 0 || num_sectors > 18) {
-        BX_INFO("int13_diskette: read/write/verify: parameter out of range\n");
-        floppy_fail(regs, DISK_RET_EPARAM);
+        floppy_ret(regs, DISK_RET_EPARAM);
         return;
     }
 
@@ -552,16 +569,13 @@ floppy_1305(struct bregs *regs, u8 drive)
     data[4] = 0; // Gap length
     data[5] = 0xf6; // Fill byte
 
-    u8 ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
-    if (ret) {
-        floppy_fail(regs, ret);
+    int ret = floppy_cmd(regs, (num_sectors * 4) - 1, data, 6);
+    if (ret)
         return;
-    }
 
     if (data[0] & 0xc0) {
         if (data[1] & 0x02) {
-            regs->ax = 0x0300;
-            set_cf(regs, 1);
+            floppy_ret(regs, DISK_RET_EWRITEPROTECT);
             return;
         }
         BX_PANIC("int13_diskette_function: read error\n");
@@ -575,7 +589,7 @@ floppy_1305(struct bregs *regs, u8 drive)
 static void
 floppy_1308(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f08\n");
+    dprintf(3, "floppy f08\n");
 
     u8 drive_type = inb_cmos(CMOS_FLOPPY_DRIVE_TYPE);
     u8 num_floppies = 0;
@@ -592,7 +606,7 @@ floppy_1308(struct bregs *regs, u8 drive)
         regs->es = 0;
         regs->di = 0;
         regs->dl = num_floppies;
-        set_cf(regs, 0);
+        set_fail(regs);
         return;
     }
 
@@ -659,32 +673,32 @@ floppy_1308(struct bregs *regs, u8 drive)
 
     /* set es & di to point to 11 byte diskette param table in ROM */
     regs->es = SEG_BIOS;
-    regs->di = (u16)diskette_param_table2;
+    regs->di = (u32)&diskette_param_table2;
     /* disk status not changed upon success */
+    set_success(regs);
 }
 
 // read diskette drive type
 static void
 floppy_1315(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f15\n");
+    dprintf(6, "floppy f15\n");
     if (drive > 1) {
+        set_fail(regs);
         regs->ah = 0; // only 2 drives supported
         // set_diskette_ret_status here ???
-        set_cf(regs, 1);
         return;
     }
     u8 drive_type = get_drive_type(drive);
 
     regs->ah = (drive_type != 0);
-    set_cf(regs, 0);
+    set_success(regs);
 }
 
 // get diskette change line status
 static void
 floppy_1316(struct bregs *regs, u8 drive)
 {
-    DEBUGF("floppy f16\n");
     if (drive > 1) {
         floppy_ret(regs, DISK_RET_EPARAM);
         return;
@@ -695,39 +709,42 @@ floppy_1316(struct bregs *regs, u8 drive)
 static void
 floppy_13XX(struct bregs *regs, u8 drive)
 {
-    BX_INFO("int13_diskette: unsupported AH=%02x\n", GET_AH());
     floppy_ret(regs, DISK_RET_EPARAM);
 }
 
 void
 floppy_13(struct bregs *regs, u8 drive)
 {
-    if (CONFIG_FLOPPY_SUPPORT) {
-        switch (regs->ah) {
-        case 0x00: floppy_1300(regs, drive); break;
-        case 0x01: floppy_1301(regs, drive); break;
-        case 0x02: floppy_1302(regs, drive); break;
-        case 0x03: floppy_1303(regs, drive); break;
-        case 0x04: floppy_1304(regs, drive); break;
-        case 0x05: floppy_1305(regs, drive); break;
-        case 0x08: floppy_1308(regs, drive); break;
-        case 0x15: floppy_1315(regs, drive); break;
-        case 0x16: floppy_1316(regs, drive); break;
-        default:   floppy_13XX(regs, drive); break;
-        }
-    } else {
+    if (! CONFIG_FLOPPY_SUPPORT) {
+        // Minimal stubs
         switch (regs->ah) {
         case 0x01: floppy_1301(regs, drive); break;
         default:   floppy_13XX(regs, drive); break;
         }
+        return;
+    }
+    switch (regs->ah) {
+    case 0x00: floppy_1300(regs, drive); break;
+    case 0x01: floppy_1301(regs, drive); break;
+    case 0x02: floppy_1302(regs, drive); break;
+    case 0x03: floppy_1303(regs, drive); break;
+    case 0x04: floppy_1304(regs, drive); break;
+    case 0x05: floppy_1305(regs, drive); break;
+    case 0x08: floppy_1308(regs, drive); break;
+    case 0x15: floppy_1315(regs, drive); break;
+    case 0x16: floppy_1316(regs, drive); break;
+    default:   floppy_13XX(regs, drive); break;
     }
 }
 
 // INT 0Eh Diskette Hardware ISR Entry Point
-void VISIBLE
-handle_0e(struct bregs *regs)
+void VISIBLE16
+handle_0e()
 {
-    //debug_enter(regs);
+    debug_isr(DEBUG_ISR_0e);
+    if (! CONFIG_FLOPPY_SUPPORT)
+        goto done;
+
     if ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0) {
         outb(0x08, PORT_FD_DATA); // sense interrupt status
         while ((inb(PORT_FD_STATUS) & 0xc0) != 0xc0)
@@ -736,15 +753,20 @@ handle_0e(struct bregs *regs)
             inb(PORT_FD_DATA);
         } while ((inb(PORT_FD_STATUS) & 0xc0) == 0xc0);
     }
-    eoi_master_pic();
     // diskette interrupt has occurred
     SETBITS_BDA(floppy_recalibration_status, FRS_TIMEOUT);
+
+done:
+    eoi_pic1();
 }
 
 // Called from int08 handler.
 void
 floppy_tick()
 {
+    if (! CONFIG_FLOPPY_SUPPORT)
+        return;
+
     // time to turn off drive(s)?
     u8 fcount = GET_BDA(floppy_motor_counter);
     if (fcount) {