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