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 | |
Kevin O'Connor | 9521e26 | 2008-07-04 13:04:29 -0400 | [diff] [blame] | 8 | #include "biosvar.h" // SET_BDA |
Kevin O'Connor | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 9 | #include "block.h" // struct drive_s |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 10 | #include "bregs.h" // struct bregs |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 11 | #include "config.h" // CONFIG_FLOPPY |
Kevin O'Connor | 9dea590 | 2013-09-14 20:23:54 -0400 | [diff] [blame] | 12 | #include "malloc.h" // malloc_fseg |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 13 | #include "output.h" // dprintf |
Kevin O'Connor | 031ef55 | 2010-12-27 19:26:57 -0500 | [diff] [blame] | 14 | #include "pci.h" // pci_to_bdf |
| 15 | #include "pci_ids.h" // PCI_CLASS_BRIDGE_ISA |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 16 | #include "pic.h" // pic_eoi1 |
Kevin O'Connor | 41639f8 | 2013-09-14 19:37:36 -0400 | [diff] [blame] | 17 | #include "romfile.h" // romfile_loadint |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 18 | #include "rtc.h" // rtc_read |
Kevin O'Connor | 3df600b | 2013-09-14 19:28:55 -0400 | [diff] [blame] | 19 | #include "stacks.h" // yield |
Kevin O'Connor | 135f3f6 | 2013-09-14 23:57:26 -0400 | [diff] [blame] | 20 | #include "std/disk.h" // DISK_RET_SUCCESS |
Kevin O'Connor | fa9c66a | 2013-09-14 19:10:40 -0400 | [diff] [blame] | 21 | #include "string.h" // memset |
Kevin O'Connor | 2d2fa31 | 2013-09-14 21:55:26 -0400 | [diff] [blame] | 22 | #include "util.h" // timer_calc |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 23 | |
Kevin O'Connor | 4ade523 | 2013-09-18 21:41:48 -0400 | [diff] [blame] | 24 | #define PORT_FD_BASE 0x03f0 |
| 25 | #define PORT_FD_DOR 0x03f2 |
| 26 | #define PORT_FD_STATUS 0x03f4 |
| 27 | #define PORT_FD_DATA 0x03f5 |
| 28 | #define PORT_FD_DIR 0x03f7 |
| 29 | |
Kevin O'Connor | 7b184d8 | 2009-08-23 11:56:55 -0400 | [diff] [blame] | 30 | #define FLOPPY_SIZE_CODE 0x02 // 512 byte sectors |
| 31 | #define FLOPPY_DATALEN 0xff // Not used - because size code is 0x02 |
| 32 | #define FLOPPY_MOTOR_TICKS 37 // ~2 seconds |
| 33 | #define FLOPPY_FILLBYTE 0xf6 |
| 34 | #define FLOPPY_GAPLEN 0x1B |
| 35 | #define FLOPPY_FORMAT_GAPLEN 0x6c |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 36 | #define FLOPPY_PIO_TIMEOUT 1000 |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 37 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 38 | // New diskette parameter table adding 3 parameters from IBM |
| 39 | // Since no provisions are made for multiple drive types, most |
| 40 | // values in this table are ignored. I set parameters for 1.44M |
| 41 | // floppy here |
Kevin O'Connor | 89a2f96 | 2013-02-18 23:36:03 -0500 | [diff] [blame] | 42 | struct floppy_ext_dbt_s diskette_param_table2 VARFSEG = { |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 43 | .dbt = { |
Kevin O'Connor | 7b184d8 | 2009-08-23 11:56:55 -0400 | [diff] [blame] | 44 | .specify1 = 0xAF, // step rate 12ms, head unload 240ms |
| 45 | .specify2 = 0x02, // head load time 4ms, DMA used |
| 46 | .shutoff_ticks = FLOPPY_MOTOR_TICKS, // ~2 seconds |
| 47 | .bps_code = FLOPPY_SIZE_CODE, |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 48 | .sectors = 18, |
Kevin O'Connor | 7b184d8 | 2009-08-23 11:56:55 -0400 | [diff] [blame] | 49 | .interblock_len = FLOPPY_GAPLEN, |
| 50 | .data_len = FLOPPY_DATALEN, |
| 51 | .gap_len = FLOPPY_FORMAT_GAPLEN, |
| 52 | .fill_byte = FLOPPY_FILLBYTE, |
| 53 | .settle_time = 0x0F, // 15ms |
| 54 | .startup_time = 0x08, // 1 second |
Kevin O'Connor | 44c631d | 2008-03-02 11:24:36 -0500 | [diff] [blame] | 55 | }, |
| 56 | .max_track = 79, // maximum track |
| 57 | .data_rate = 0, // data transfer rate |
| 58 | .drive_type = 4, // drive type in cmos |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 59 | }; |
| 60 | |
Kevin O'Connor | ef71989 | 2012-11-26 11:14:00 -0500 | [diff] [blame] | 61 | struct floppy_dbt_s diskette_param_table VAR16FIXED(0xefc7); |
Kevin O'Connor | 3085376 | 2009-01-17 18:49:20 -0500 | [diff] [blame] | 62 | |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 63 | struct floppyinfo_s { |
| 64 | struct chs_s chs; |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 65 | u8 floppy_size; |
| 66 | u8 data_rate; |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 67 | }; |
| 68 | |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 69 | #define FLOPPY_SIZE_525 0x01 |
| 70 | #define FLOPPY_SIZE_350 0x02 |
| 71 | |
| 72 | #define FLOPPY_RATE_500K 0x00 |
| 73 | #define FLOPPY_RATE_300K 0x01 |
| 74 | #define FLOPPY_RATE_250K 0x02 |
| 75 | #define FLOPPY_RATE_1M 0x03 |
| 76 | |
Kevin O'Connor | 89a2f96 | 2013-02-18 23:36:03 -0500 | [diff] [blame] | 77 | struct floppyinfo_s FloppyInfo[] VARFSEG = { |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 78 | // Unknown |
| 79 | { {0, 0, 0}, 0x00, 0x00}, |
| 80 | // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 81 | { {2, 40, 9}, FLOPPY_SIZE_525, FLOPPY_RATE_300K}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 82 | // 2 - 1.2MB, 5.25" - 2 heads, 80 tracks, 15 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 83 | { {2, 80, 15}, FLOPPY_SIZE_525, FLOPPY_RATE_500K}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 84 | // 3 - 720KB, 3.5" - 2 heads, 80 tracks, 9 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 85 | { {2, 80, 9}, FLOPPY_SIZE_350, FLOPPY_RATE_250K}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 86 | // 4 - 1.44MB, 3.5" - 2 heads, 80 tracks, 18 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 87 | { {2, 80, 18}, FLOPPY_SIZE_350, FLOPPY_RATE_500K}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 88 | // 5 - 2.88MB, 3.5" - 2 heads, 80 tracks, 36 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 89 | { {2, 80, 36}, FLOPPY_SIZE_350, FLOPPY_RATE_1M}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 90 | // 6 - 160k, 5.25" - 1 heads, 40 tracks, 8 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 91 | { {1, 40, 8}, FLOPPY_SIZE_525, FLOPPY_RATE_250K}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 92 | // 7 - 180k, 5.25" - 1 heads, 40 tracks, 9 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 93 | { {1, 40, 9}, FLOPPY_SIZE_525, FLOPPY_RATE_300K}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 94 | // 8 - 320k, 5.25" - 2 heads, 40 tracks, 8 sectors |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 95 | { {2, 40, 8}, FLOPPY_SIZE_525, FLOPPY_RATE_250K}, |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 96 | }; |
| 97 | |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 98 | struct drive_s * |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 99 | init_floppy(int floppyid, int ftype) |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 100 | { |
| 101 | if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) { |
| 102 | dprintf(1, "Bad floppy type %d\n", ftype); |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 103 | return NULL; |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 104 | } |
| 105 | |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 106 | struct drive_s *drive = malloc_fseg(sizeof(*drive)); |
| 107 | if (!drive) { |
Kevin O'Connor | d7e998f | 2010-02-15 22:48:28 -0500 | [diff] [blame] | 108 | warn_noalloc(); |
Kevin O'Connor | 77d227b | 2009-10-22 21:48:39 -0400 | [diff] [blame] | 109 | return NULL; |
Kevin O'Connor | d7e998f | 2010-02-15 22:48:28 -0500 | [diff] [blame] | 110 | } |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 111 | memset(drive, 0, sizeof(*drive)); |
| 112 | drive->cntl_id = floppyid; |
| 113 | drive->type = DTYPE_FLOPPY; |
| 114 | drive->blksize = DISK_SECTOR_SIZE; |
| 115 | drive->floppy_type = ftype; |
| 116 | drive->sectors = (u64)-1; |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 117 | |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 118 | memcpy(&drive->lchs, &FloppyInfo[ftype].chs |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 119 | , sizeof(FloppyInfo[ftype].chs)); |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 120 | return drive; |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 121 | } |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 122 | |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 123 | static void |
| 124 | addFloppy(int floppyid, int ftype) |
| 125 | { |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 126 | struct drive_s *drive = init_floppy(floppyid, ftype); |
| 127 | if (!drive) |
Kevin O'Connor | ca2bc1c | 2010-12-29 21:41:19 -0500 | [diff] [blame] | 128 | return; |
| 129 | char *desc = znprintf(MAXDESCSIZE, "Floppy [drive %c]", 'A' + floppyid); |
Kevin O'Connor | 0cd7005 | 2011-07-02 14:04:19 -0400 | [diff] [blame] | 130 | 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] | 131 | int prio = bootprio_find_fdc_device(pci, PORT_FD_BASE, floppyid); |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 132 | boot_add_floppy(drive, desc, prio); |
Kevin O'Connor | 51fd0a1 | 2009-09-12 13:20:14 -0400 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 136 | floppy_setup(void) |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 137 | { |
Kevin O'Connor | ef71989 | 2012-11-26 11:14:00 -0500 | [diff] [blame] | 138 | memcpy(&diskette_param_table, &diskette_param_table2 |
| 139 | , sizeof(diskette_param_table)); |
| 140 | SET_IVT(0x1E, SEGOFF(SEG_BIOS |
| 141 | , (u32)&diskette_param_table2 - BUILD_BIOS_ADDR)); |
| 142 | |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 143 | if (! CONFIG_FLOPPY) |
Kevin O'Connor | f54c150 | 2008-06-14 15:56:16 -0400 | [diff] [blame] | 144 | return; |
Kevin O'Connor | 35192dd | 2008-06-08 19:18:33 -0400 | [diff] [blame] | 145 | dprintf(3, "init floppy drives\n"); |
Kevin O'Connor | 88c00ee | 2008-05-18 00:14:13 -0400 | [diff] [blame] | 146 | |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 147 | if (CONFIG_QEMU) { |
Kevin O'Connor | 8b7861c | 2013-09-15 02:29:06 -0400 | [diff] [blame] | 148 | u8 type = rtc_read(CMOS_FLOPPY_DRIVE_TYPE); |
Kevin O'Connor | 897fb11 | 2013-02-07 23:32:48 -0500 | [diff] [blame] | 149 | if (type & 0xf0) |
| 150 | addFloppy(0, type >> 4); |
| 151 | if (type & 0x0f) |
| 152 | addFloppy(1, type & 0x0f); |
| 153 | } else { |
Kevin O'Connor | 8b73b83 | 2012-11-26 11:18:11 -0500 | [diff] [blame] | 154 | u8 type = romfile_loadint("etc/floppy0", 0); |
| 155 | if (type) |
| 156 | addFloppy(0, type); |
| 157 | type = romfile_loadint("etc/floppy1", 0); |
| 158 | if (type) |
| 159 | addFloppy(1, type); |
Kevin O'Connor | 88c00ee | 2008-05-18 00:14:13 -0400 | [diff] [blame] | 160 | } |
Kevin O'Connor | 88c00ee | 2008-05-18 00:14:13 -0400 | [diff] [blame] | 161 | |
Kevin O'Connor | cc9e1bf | 2010-07-28 21:31:38 -0400 | [diff] [blame] | 162 | enable_hwirq(6, FUNC16(entry_0e)); |
Kevin O'Connor | 3bbcc14 | 2008-04-13 17:07:33 -0400 | [diff] [blame] | 163 | } |
| 164 | |
Kevin O'Connor | a3855ad | 2009-08-16 21:59:40 -0400 | [diff] [blame] | 165 | // Find a floppy type that matches a given image size. |
| 166 | int |
| 167 | find_floppy_type(u32 size) |
| 168 | { |
| 169 | int i; |
| 170 | for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) { |
| 171 | struct chs_s *c = &FloppyInfo[i].chs; |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 172 | if (c->cylinder * c->head * c->sector * DISK_SECTOR_SIZE == size) |
Kevin O'Connor | a3855ad | 2009-08-16 21:59:40 -0400 | [diff] [blame] | 173 | return i; |
| 174 | } |
| 175 | return -1; |
| 176 | } |
| 177 | |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 178 | |
| 179 | /**************************************************************** |
| 180 | * Low-level floppy IO |
| 181 | ****************************************************************/ |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 182 | |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 183 | u8 FloppyDOR VARLOW; |
| 184 | |
| 185 | static inline void |
| 186 | floppy_dor_write(u8 val) |
| 187 | { |
| 188 | outb(val, PORT_FD_DOR); |
| 189 | SET_LOW(FloppyDOR, val); |
| 190 | } |
| 191 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 192 | static void |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 193 | floppy_disable_controller(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 194 | { |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 195 | dprintf(2, "Floppy_disable_controller\n"); |
| 196 | floppy_dor_write(0x00); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 197 | } |
| 198 | |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 199 | static int |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 200 | floppy_wait_irq(void) |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 201 | { |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 202 | u8 frs = GET_BDA(floppy_recalibration_status); |
| 203 | SET_BDA(floppy_recalibration_status, frs & ~FRS_IRQ); |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 204 | for (;;) { |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 205 | if (!GET_BDA(floppy_motor_counter)) { |
Kevin O'Connor | b66456c | 2013-12-04 10:31:51 -0500 | [diff] [blame] | 206 | warn_timeout(); |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 207 | floppy_disable_controller(); |
| 208 | return DISK_RET_ETIMEOUT; |
| 209 | } |
Kevin O'Connor | e51316d | 2012-06-10 09:09:22 -0400 | [diff] [blame] | 210 | frs = GET_BDA(floppy_recalibration_status); |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 211 | if (frs & FRS_IRQ) |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 212 | break; |
Kevin O'Connor | 94c749c | 2012-05-28 11:44:02 -0400 | [diff] [blame] | 213 | // Could use yield_toirq() here, but that causes issues on |
Kevin O'Connor | 7a98fd0 | 2010-01-17 13:00:49 -0500 | [diff] [blame] | 214 | // bochs, so use yield() instead. |
| 215 | yield(); |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 216 | } |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 217 | |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 218 | SET_BDA(floppy_recalibration_status, frs & ~FRS_IRQ); |
| 219 | return DISK_RET_SUCCESS; |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 220 | } |
| 221 | |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 222 | struct floppy_pio_s { |
| 223 | u8 cmdlen; |
| 224 | u8 resplen; |
| 225 | u8 waitirq; |
| 226 | u8 data[9]; |
| 227 | }; |
| 228 | |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 229 | static int |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 230 | floppy_pio(struct floppy_pio_s *pio) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 231 | { |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 232 | // Send command to controller. |
Kevin O'Connor | 018bdd7 | 2013-07-20 18:22:57 -0400 | [diff] [blame] | 233 | u32 end = timer_calc(FLOPPY_PIO_TIMEOUT); |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 234 | int i = 0; |
| 235 | for (;;) { |
| 236 | u8 sts = inb(PORT_FD_STATUS); |
| 237 | if (!(sts & 0x80)) { |
Kevin O'Connor | 018bdd7 | 2013-07-20 18:22:57 -0400 | [diff] [blame] | 238 | if (timer_check(end)) { |
Kevin O'Connor | b66456c | 2013-12-04 10:31:51 -0500 | [diff] [blame] | 239 | warn_timeout(); |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 240 | floppy_disable_controller(); |
| 241 | return DISK_RET_ETIMEOUT; |
| 242 | } |
| 243 | continue; |
| 244 | } |
| 245 | if (sts & 0x40) { |
| 246 | floppy_disable_controller(); |
| 247 | return DISK_RET_ECONTROLLER; |
| 248 | } |
| 249 | outb(pio->data[i++], PORT_FD_DATA); |
| 250 | if (i >= pio->cmdlen) |
| 251 | break; |
| 252 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 253 | |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 254 | // Wait for command to complete. |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 255 | if (pio->waitirq) { |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 256 | int ret = floppy_wait_irq(); |
| 257 | if (ret) |
| 258 | return ret; |
Kevin O'Connor | 06ad44e | 2008-04-05 19:30:02 -0400 | [diff] [blame] | 259 | } |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 260 | |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 261 | // Read response from controller. |
Kevin O'Connor | 018bdd7 | 2013-07-20 18:22:57 -0400 | [diff] [blame] | 262 | end = timer_calc(FLOPPY_PIO_TIMEOUT); |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 263 | i = 0; |
| 264 | for (;;) { |
| 265 | u8 sts = inb(PORT_FD_STATUS); |
| 266 | if (!(sts & 0x80)) { |
Kevin O'Connor | 018bdd7 | 2013-07-20 18:22:57 -0400 | [diff] [blame] | 267 | if (timer_check(end)) { |
Kevin O'Connor | b66456c | 2013-12-04 10:31:51 -0500 | [diff] [blame] | 268 | warn_timeout(); |
Kevin O'Connor | 068cb4f | 2013-03-03 11:05:19 -0500 | [diff] [blame] | 269 | floppy_disable_controller(); |
| 270 | return DISK_RET_ETIMEOUT; |
| 271 | } |
| 272 | continue; |
| 273 | } |
| 274 | if (i >= pio->resplen) |
| 275 | break; |
| 276 | if (!(sts & 0x40)) { |
| 277 | floppy_disable_controller(); |
| 278 | return DISK_RET_ECONTROLLER; |
| 279 | } |
| 280 | pio->data[i++] = inb(PORT_FD_DATA); |
| 281 | } |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 282 | |
| 283 | return DISK_RET_SUCCESS; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 284 | } |
| 285 | |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 286 | static int |
| 287 | floppy_enable_controller(void) |
| 288 | { |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 289 | dprintf(2, "Floppy_enable_controller\n"); |
| 290 | floppy_dor_write(0x00); |
| 291 | floppy_dor_write(0x0c); |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 292 | int ret = floppy_wait_irq(); |
| 293 | if (ret) |
| 294 | return ret; |
| 295 | |
| 296 | struct floppy_pio_s pio; |
| 297 | pio.cmdlen = 1; |
| 298 | pio.resplen = 2; |
| 299 | pio.waitirq = 0; |
| 300 | pio.data[0] = 0x08; // 08: Check Interrupt Status |
| 301 | return floppy_pio(&pio); |
| 302 | } |
| 303 | |
| 304 | static int |
| 305 | floppy_select_drive(u8 floppyid) |
| 306 | { |
| 307 | // reset the disk motor timeout value of INT 08 |
| 308 | SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS); |
| 309 | |
| 310 | // Enable controller if it isn't running. |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 311 | if (!(GET_LOW(FloppyDOR) & 0x04)) { |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 312 | int ret = floppy_enable_controller(); |
| 313 | if (ret) |
| 314 | return ret; |
| 315 | } |
| 316 | |
| 317 | // Turn on motor of selected drive, DMA & int enabled, normal operation |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 318 | floppy_dor_write((floppyid ? 0x20 : 0x10) | 0x0c | floppyid); |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 319 | |
| 320 | return DISK_RET_SUCCESS; |
| 321 | } |
| 322 | |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 323 | |
| 324 | /**************************************************************** |
| 325 | * Floppy media sense |
| 326 | ****************************************************************/ |
| 327 | |
| 328 | static inline void |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 329 | set_diskette_current_cyl(u8 floppyid, u8 cyl) |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 330 | { |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 331 | SET_BDA(floppy_track[floppyid], cyl); |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 332 | } |
| 333 | |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 334 | static int |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 335 | floppy_drive_recal(u8 floppyid) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 336 | { |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 337 | dprintf(2, "Floppy_drive_recal %d\n", floppyid); |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 338 | int ret = floppy_select_drive(floppyid); |
| 339 | if (ret) |
| 340 | return ret; |
| 341 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 342 | // send Recalibrate command (2 bytes) to controller |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 343 | struct floppy_pio_s pio; |
| 344 | pio.cmdlen = 2; |
| 345 | pio.resplen = 0; |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 346 | pio.waitirq = 1; |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 347 | pio.data[0] = 0x07; // 07: Recalibrate |
| 348 | pio.data[1] = floppyid; // 0=drive0, 1=drive1 |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 349 | ret = floppy_pio(&pio); |
| 350 | if (ret) |
| 351 | return ret; |
| 352 | |
| 353 | pio.cmdlen = 1; |
| 354 | pio.resplen = 2; |
| 355 | pio.waitirq = 0; |
| 356 | pio.data[0] = 0x08; // 08: Check Interrupt Status |
| 357 | ret = floppy_pio(&pio); |
| 358 | if (ret) |
| 359 | return ret; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 360 | |
Kevin O'Connor | e51316d | 2012-06-10 09:09:22 -0400 | [diff] [blame] | 361 | u8 frs = GET_BDA(floppy_recalibration_status); |
| 362 | SET_BDA(floppy_recalibration_status, frs | (1<<floppyid)); |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 363 | set_diskette_current_cyl(floppyid, 0); |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 364 | return DISK_RET_SUCCESS; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 365 | } |
| 366 | |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 367 | static int |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 368 | floppy_drive_readid(u8 floppyid, u8 data_rate, u8 head) |
| 369 | { |
| 370 | int ret = floppy_select_drive(floppyid); |
| 371 | if (ret) |
| 372 | return ret; |
| 373 | |
| 374 | // Set data rate. |
| 375 | outb(data_rate, PORT_FD_DIR); |
| 376 | |
| 377 | // send Read Sector Id command |
| 378 | struct floppy_pio_s pio; |
| 379 | pio.cmdlen = 2; |
| 380 | pio.resplen = 7; |
| 381 | pio.waitirq = 1; |
| 382 | pio.data[0] = 0x4a; // 0a: Read Sector Id |
| 383 | pio.data[1] = (head << 2) | floppyid; // HD DR1 DR2 |
| 384 | ret = floppy_pio(&pio); |
| 385 | if (ret) |
| 386 | return ret; |
| 387 | if (pio.data[0] & 0xc0) |
| 388 | return -1; |
| 389 | return 0; |
| 390 | } |
| 391 | |
| 392 | static int |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 393 | floppy_media_sense(struct drive_s *drive_gf) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 394 | { |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 395 | u8 ftype = GET_GLOBALFLAT(drive_gf->floppy_type), stype = ftype; |
| 396 | u8 floppyid = GET_GLOBALFLAT(drive_gf->cntl_id); |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 397 | |
| 398 | u8 data_rate = GET_GLOBAL(FloppyInfo[stype].data_rate); |
| 399 | int ret = floppy_drive_readid(floppyid, data_rate, 0); |
| 400 | if (ret) { |
| 401 | // Attempt media sense. |
| 402 | for (stype=1; ; stype++) { |
| 403 | if (stype >= ARRAY_SIZE(FloppyInfo)) |
| 404 | return DISK_RET_EMEDIA; |
| 405 | if (stype==ftype |
| 406 | || (GET_GLOBAL(FloppyInfo[stype].floppy_size) |
| 407 | != GET_GLOBAL(FloppyInfo[ftype].floppy_size)) |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 408 | || (GET_GLOBAL(FloppyInfo[stype].chs.head) |
| 409 | > GET_GLOBAL(FloppyInfo[ftype].chs.head)) |
| 410 | || (GET_GLOBAL(FloppyInfo[stype].chs.cylinder) |
| 411 | > GET_GLOBAL(FloppyInfo[ftype].chs.cylinder)) |
| 412 | || (GET_GLOBAL(FloppyInfo[stype].chs.sector) |
| 413 | > GET_GLOBAL(FloppyInfo[ftype].chs.sector))) |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 414 | continue; |
| 415 | data_rate = GET_GLOBAL(FloppyInfo[stype].data_rate); |
| 416 | ret = floppy_drive_readid(floppyid, data_rate, 0); |
| 417 | if (!ret) |
| 418 | break; |
| 419 | } |
| 420 | } |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 421 | dprintf(2, "Floppy_media_sense on drive %d found rate %d\n" |
| 422 | , floppyid, data_rate); |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 423 | |
| 424 | u8 old_data_rate = GET_BDA(floppy_media_state[floppyid]) >> 6; |
| 425 | SET_BDA(floppy_last_data_rate, (old_data_rate<<2) | (data_rate<<6)); |
| 426 | u8 media = (stype == 1 ? 0x04 : (stype == 2 ? 0x05 : 0x07)); |
| 427 | u8 fms = (data_rate<<6) | FMS_MEDIA_DRIVE_ESTABLISHED | media; |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 428 | if (GET_GLOBAL(FloppyInfo[stype].chs.cylinder) |
| 429 | < GET_GLOBAL(FloppyInfo[ftype].chs.cylinder)) |
Kevin O'Connor | e7c5a7e | 2013-03-03 13:59:58 -0500 | [diff] [blame] | 430 | fms |= FMS_DOUBLE_STEPPING; |
| 431 | SET_BDA(floppy_media_state[floppyid], fms); |
| 432 | |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 433 | return DISK_RET_SUCCESS; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 434 | } |
| 435 | |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 436 | static int |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 437 | check_recal_drive(struct drive_s *drive_gf) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 438 | { |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 439 | u8 floppyid = GET_GLOBALFLAT(drive_gf->cntl_id); |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 440 | if ((GET_BDA(floppy_recalibration_status) & (1<<floppyid)) |
| 441 | && (GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED)) |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 442 | // Media is known. |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 443 | return DISK_RET_SUCCESS; |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 444 | |
| 445 | // Recalibrate drive. |
Kevin O'Connor | 1f711d2 | 2013-03-02 21:26:54 -0500 | [diff] [blame] | 446 | int ret = floppy_drive_recal(floppyid); |
| 447 | if (ret) |
| 448 | return ret; |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 449 | |
| 450 | // Sense media. |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 451 | return floppy_media_sense(drive_gf); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 452 | } |
| 453 | |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 454 | |
| 455 | /**************************************************************** |
Kevin O'Connor | 7b9f297 | 2013-09-18 21:04:03 -0400 | [diff] [blame] | 456 | * Floppy DMA transfer |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 457 | ****************************************************************/ |
| 458 | |
| 459 | // Perform a floppy transfer command (setup DMA and issue PIO). |
| 460 | static int |
| 461 | floppy_cmd(struct disk_op_s *op, int blocksize, struct floppy_pio_s *pio) |
| 462 | { |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 463 | int ret = check_recal_drive(op->drive_gf); |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 464 | if (ret) |
| 465 | return ret; |
| 466 | |
Kevin O'Connor | 7b9f297 | 2013-09-18 21:04:03 -0400 | [diff] [blame] | 467 | // Setup DMA controller |
| 468 | int isWrite = pio->data[0] != 0xe6; |
| 469 | ret = dma_floppy((u32)op->buf_fl, op->count * blocksize, isWrite); |
| 470 | if (ret) |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 471 | return DISK_RET_EBOUNDARY; |
| 472 | |
Kevin O'Connor | 7b9f297 | 2013-09-18 21:04:03 -0400 | [diff] [blame] | 473 | // Invoke floppy controller |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 474 | ret = floppy_select_drive(pio->data[1] & 1); |
| 475 | if (ret) |
| 476 | return ret; |
| 477 | pio->resplen = 7; |
| 478 | pio->waitirq = 1; |
| 479 | ret = floppy_pio(pio); |
| 480 | if (ret) |
| 481 | return ret; |
| 482 | |
| 483 | // Populate floppy_return_status in BDA |
| 484 | int i; |
| 485 | for (i=0; i<7; i++) |
| 486 | SET_BDA(floppy_return_status[i], pio->data[i]); |
| 487 | |
| 488 | if (pio->data[0] & 0xc0) { |
| 489 | if (pio->data[1] & 0x02) |
| 490 | return DISK_RET_EWRITEPROTECT; |
Kevin O'Connor | 8413b32 | 2013-03-09 20:07:38 -0500 | [diff] [blame] | 491 | dprintf(1, "floppy error: %02x %02x %02x %02x %02x %02x %02x\n" |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 492 | , pio->data[0], pio->data[1], pio->data[2], pio->data[3] |
| 493 | , pio->data[4], pio->data[5], pio->data[6]); |
| 494 | return DISK_RET_ECONTROLLER; |
| 495 | } |
| 496 | |
| 497 | u8 track = (pio->cmdlen == 9 ? pio->data[3] : 0); |
| 498 | set_diskette_current_cyl(pio->data[0] & 1, track); |
| 499 | |
| 500 | return DISK_RET_SUCCESS; |
| 501 | } |
| 502 | |
| 503 | |
| 504 | /**************************************************************** |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 505 | * Floppy handlers |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 506 | ****************************************************************/ |
| 507 | |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 508 | static struct chs_s |
| 509 | lba2chs(struct disk_op_s *op) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 510 | { |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 511 | struct chs_s res = { }; |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 512 | |
Kevin O'Connor | 302a6e8 | 2013-12-07 11:46:37 -0500 | [diff] [blame] | 513 | u32 tmp = op->lba; |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 514 | u16 nls = GET_GLOBALFLAT(op->drive_gf->lchs.sector); |
Kevin O'Connor | 302a6e8 | 2013-12-07 11:46:37 -0500 | [diff] [blame] | 515 | res.sector = (tmp % nls) + 1; |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 516 | |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 517 | tmp /= nls; |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 518 | u16 nlh = GET_GLOBALFLAT(op->drive_gf->lchs.head); |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 519 | res.head = tmp % nlh; |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 520 | |
| 521 | tmp /= nlh; |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 522 | res.cylinder = tmp; |
| 523 | |
| 524 | return res; |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | // diskette controller reset |
| 528 | static int |
| 529 | floppy_reset(struct disk_op_s *op) |
| 530 | { |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 531 | u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id); |
Kevin O'Connor | 5bb7552 | 2013-03-03 15:32:31 -0500 | [diff] [blame] | 532 | SET_BDA(floppy_recalibration_status, 0); |
| 533 | SET_BDA(floppy_media_state[0], 0); |
| 534 | SET_BDA(floppy_media_state[1], 0); |
| 535 | SET_BDA(floppy_track[0], 0); |
| 536 | SET_BDA(floppy_track[1], 0); |
| 537 | SET_BDA(floppy_last_data_rate, 0); |
| 538 | floppy_disable_controller(); |
| 539 | return floppy_select_drive(floppyid); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | // Read Diskette Sectors |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 543 | static int |
| 544 | floppy_read(struct disk_op_s *op) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 545 | { |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 546 | struct chs_s chs = lba2chs(op); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 547 | |
| 548 | // send read-normal-data command (9 bytes) to controller |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 549 | u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id); |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 550 | struct floppy_pio_s pio; |
| 551 | pio.cmdlen = 9; |
| 552 | pio.data[0] = 0xe6; // e6: read normal data |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 553 | pio.data[1] = (chs.head << 2) | floppyid; // HD DR1 DR2 |
| 554 | pio.data[2] = chs.cylinder; |
| 555 | pio.data[3] = chs.head; |
| 556 | pio.data[4] = chs.sector; |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 557 | pio.data[5] = FLOPPY_SIZE_CODE; |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 558 | pio.data[6] = chs.sector + op->count - 1; // last sector to read on track |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 559 | pio.data[7] = FLOPPY_GAPLEN; |
| 560 | pio.data[8] = FLOPPY_DATALEN; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 561 | |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 562 | int res = floppy_cmd(op, DISK_SECTOR_SIZE, &pio); |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 563 | if (res) |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 564 | goto fail; |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 565 | return DISK_RET_SUCCESS; |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 566 | fail: |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 567 | op->count = 0; // no sectors read |
| 568 | return res; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 569 | } |
| 570 | |
| 571 | // Write Diskette Sectors |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 572 | static int |
| 573 | floppy_write(struct disk_op_s *op) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 574 | { |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 575 | struct chs_s chs = lba2chs(op); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 576 | |
| 577 | // send write-normal-data command (9 bytes) to controller |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 578 | u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id); |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 579 | struct floppy_pio_s pio; |
| 580 | pio.cmdlen = 9; |
| 581 | pio.data[0] = 0xc5; // c5: write normal data |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 582 | pio.data[1] = (chs.head << 2) | floppyid; // HD DR1 DR2 |
| 583 | pio.data[2] = chs.cylinder; |
| 584 | pio.data[3] = chs.head; |
| 585 | pio.data[4] = chs.sector; |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 586 | pio.data[5] = FLOPPY_SIZE_CODE; |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 587 | pio.data[6] = chs.sector + op->count - 1; // last sector to write on track |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 588 | pio.data[7] = FLOPPY_GAPLEN; |
| 589 | pio.data[8] = FLOPPY_DATALEN; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 590 | |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 591 | int res = floppy_cmd(op, DISK_SECTOR_SIZE, &pio); |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 592 | if (res) |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 593 | goto fail; |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 594 | return DISK_RET_SUCCESS; |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 595 | fail: |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 596 | op->count = 0; // no sectors read |
| 597 | return res; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | // Verify Diskette Sectors |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 601 | static int |
| 602 | floppy_verify(struct disk_op_s *op) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 603 | { |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 604 | int res = check_recal_drive(op->drive_gf); |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 605 | if (res) |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 606 | goto fail; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 607 | |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 608 | struct chs_s chs = lba2chs(op); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 609 | |
| 610 | // ??? should track be new val from return_status[3] ? |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 611 | u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id); |
Kevin O'Connor | 8ab9a34 | 2013-09-28 23:34:49 -0400 | [diff] [blame] | 612 | set_diskette_current_cyl(floppyid, chs.cylinder); |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 613 | return DISK_RET_SUCCESS; |
Kevin O'Connor | a68aeaf | 2008-07-07 21:37:10 -0400 | [diff] [blame] | 614 | fail: |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 615 | op->count = 0; // no sectors read |
| 616 | return res; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 617 | } |
| 618 | |
| 619 | // format diskette track |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 620 | static int |
| 621 | floppy_format(struct disk_op_s *op) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 622 | { |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 623 | u8 head = op->lba; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 624 | |
| 625 | // send format-track command (6 bytes) to controller |
Kevin O'Connor | 1902c94 | 2013-10-26 11:48:06 -0400 | [diff] [blame] | 626 | u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id); |
Kevin O'Connor | 9ba374c | 2013-03-02 18:37:04 -0500 | [diff] [blame] | 627 | struct floppy_pio_s pio; |
| 628 | pio.cmdlen = 6; |
| 629 | pio.data[0] = 0x4d; // 4d: format track |
| 630 | pio.data[1] = (head << 2) | floppyid; // HD DR1 DR2 |
| 631 | pio.data[2] = FLOPPY_SIZE_CODE; |
| 632 | pio.data[3] = op->count; // number of sectors per track |
| 633 | pio.data[4] = FLOPPY_FORMAT_GAPLEN; |
| 634 | pio.data[5] = FLOPPY_FILLBYTE; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 635 | |
Kevin O'Connor | c849405 | 2013-03-03 10:27:30 -0500 | [diff] [blame] | 636 | return floppy_cmd(op, 4, &pio); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 637 | } |
| 638 | |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 639 | int |
| 640 | process_floppy_op(struct disk_op_s *op) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 641 | { |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 642 | if (!CONFIG_FLOPPY) |
| 643 | return 0; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 644 | |
Kevin O'Connor | af5aabb | 2009-08-16 18:48:38 -0400 | [diff] [blame] | 645 | switch (op->command) { |
| 646 | case CMD_RESET: |
| 647 | return floppy_reset(op); |
| 648 | case CMD_READ: |
| 649 | return floppy_read(op); |
| 650 | case CMD_WRITE: |
| 651 | return floppy_write(op); |
| 652 | case CMD_VERIFY: |
| 653 | return floppy_verify(op); |
| 654 | case CMD_FORMAT: |
| 655 | return floppy_format(op); |
| 656 | default: |
| 657 | op->count = 0; |
| 658 | return DISK_RET_EPARAM; |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 659 | } |
| 660 | } |
| 661 | |
Kevin O'Connor | a3b612e | 2009-02-07 11:25:29 -0500 | [diff] [blame] | 662 | |
| 663 | /**************************************************************** |
| 664 | * HW irqs |
| 665 | ****************************************************************/ |
| 666 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 667 | // INT 0Eh Diskette Hardware ISR Entry Point |
Kevin O'Connor | 1978676 | 2008-03-05 21:09:59 -0500 | [diff] [blame] | 668 | void VISIBLE16 |
Kevin O'Connor | 1297e5d | 2012-06-02 20:30:58 -0400 | [diff] [blame] | 669 | handle_0e(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 670 | { |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 671 | if (! CONFIG_FLOPPY) |
Kevin O'Connor | ff5e005 | 2012-12-07 11:25:58 -0500 | [diff] [blame] | 672 | return; |
| 673 | debug_isr(DEBUG_ISR_0e); |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 674 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 675 | // diskette interrupt has occurred |
Kevin O'Connor | e51316d | 2012-06-10 09:09:22 -0400 | [diff] [blame] | 676 | u8 frs = GET_BDA(floppy_recalibration_status); |
Kevin O'Connor | 6e529bd | 2013-03-02 20:30:50 -0500 | [diff] [blame] | 677 | SET_BDA(floppy_recalibration_status, frs | FRS_IRQ); |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 678 | |
Kevin O'Connor | aa7c234 | 2013-07-14 15:07:21 -0400 | [diff] [blame] | 679 | pic_eoi1(); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 680 | } |
| 681 | |
| 682 | // Called from int08 handler. |
| 683 | void |
Kevin O'Connor | 1ca05b0 | 2010-01-03 17:43:37 -0500 | [diff] [blame] | 684 | floppy_tick(void) |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 685 | { |
Kevin O'Connor | 0a0e42e | 2009-08-16 12:09:44 -0400 | [diff] [blame] | 686 | if (! CONFIG_FLOPPY) |
Kevin O'Connor | 4096702 | 2008-07-21 22:23:05 -0400 | [diff] [blame] | 687 | return; |
| 688 | |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 689 | // time to turn off drive(s)? |
| 690 | u8 fcount = GET_BDA(floppy_motor_counter); |
| 691 | if (fcount) { |
| 692 | fcount--; |
| 693 | SET_BDA(floppy_motor_counter, fcount); |
| 694 | if (fcount == 0) |
| 695 | // turn motor(s) off |
Kevin O'Connor | 402f9a4 | 2013-12-07 12:48:11 -0500 | [diff] [blame] | 696 | floppy_dor_write(GET_LOW(FloppyDOR) & ~0xf0); |
Kevin O'Connor | f076a3e | 2008-02-25 22:25:15 -0500 | [diff] [blame] | 697 | } |
| 698 | } |