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