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