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