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