blob: 730aadc0178eac7f2516a5b7e22808a5af6dd016 [file] [log] [blame]
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05001// 16bit code to access floppy drives.
2//
Kevin O'Connorc892b132009-08-11 21:59:37 -04003// Copyright (C) 2008,2009 Kevin O'Connor <kevin@koconnor.net>
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05004// Copyright (C) 2002 MandrakeSoft S.A.
5//
Kevin O'Connorb1b7c2a2009-01-15 20:52:58 -05006// This file may be distributed under the terms of the GNU LGPLv3 license.
Kevin O'Connorf076a3e2008-02-25 22:25:15 -05007
Kevin O'Connor9521e262008-07-04 13:04:29 -04008#include "biosvar.h" // SET_BDA
Kevin O'Connor135f3f62013-09-14 23:57:26 -04009#include "block.h" // struct drive_s
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040010#include "bregs.h" // struct bregs
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040011#include "config.h" // CONFIG_FLOPPY
Kevin O'Connor9dea5902013-09-14 20:23:54 -040012#include "malloc.h" // malloc_fseg
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040013#include "output.h" // dprintf
Kevin O'Connor4d8510c2016-02-03 01:28:20 -050014#include "pcidevice.h" // pci_find_class
Kevin O'Connor031ef552010-12-27 19:26:57 -050015#include "pci_ids.h" // PCI_CLASS_BRIDGE_ISA
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040016#include "pic.h" // pic_eoi1
Kevin O'Connor41639f82013-09-14 19:37:36 -040017#include "romfile.h" // romfile_loadint
Kevin O'Connor8b7861c2013-09-15 02:29:06 -040018#include "rtc.h" // rtc_read
Kevin O'Connor3df600b2013-09-14 19:28:55 -040019#include "stacks.h" // yield
Kevin O'Connor135f3f62013-09-14 23:57:26 -040020#include "std/disk.h" // DISK_RET_SUCCESS
Kevin O'Connorfa9c66a2013-09-14 19:10:40 -040021#include "string.h" // memset
Kevin O'Connor2d2fa312013-09-14 21:55:26 -040022#include "util.h" // timer_calc
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050023
Kevin O'Connor4ade5232013-09-18 21:41:48 -040024#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'Connor7b184d82009-08-23 11:56:55 -040030#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'Connor068cb4f2013-03-03 11:05:19 -050036#define FLOPPY_PIO_TIMEOUT 1000
Nikolay Nikolov4a6dbce2018-02-04 17:27:01 +020037#define FLOPPY_IRQ_TIMEOUT 5000
Nikolay Nikolovf9cc9692018-02-10 13:52:14 +020038#define FLOPPY_SPECIFY1 0xAF // step rate 12ms, head unload 240ms
39#define FLOPPY_SPECIFY2 0x02 // head load time 4ms, DMA used
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050040
Nikolay Nikolov53fc6312018-02-04 17:26:58 +020041#define FLOPPY_DOR_MOTOR_D 0x80 // Set to turn drive 3's motor ON
42#define FLOPPY_DOR_MOTOR_C 0x40 // Set to turn drive 2's motor ON
43#define FLOPPY_DOR_MOTOR_B 0x20 // Set to turn drive 1's motor ON
44#define FLOPPY_DOR_MOTOR_A 0x10 // Set to turn drive 0's motor ON
45#define FLOPPY_DOR_MOTOR_MASK 0xf0
46#define FLOPPY_DOR_IRQ 0x08 // Set to enable IRQs and DMA
47#define FLOPPY_DOR_RESET 0x04 // Clear = enter reset mode, Set = normal operation
48#define FLOPPY_DOR_DSEL_MASK 0x03 // "Select" drive number for next access
49
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050050// New diskette parameter table adding 3 parameters from IBM
51// Since no provisions are made for multiple drive types, most
52// values in this table are ignored. I set parameters for 1.44M
53// floppy here
Kevin O'Connor89a2f962013-02-18 23:36:03 -050054struct floppy_ext_dbt_s diskette_param_table2 VARFSEG = {
Kevin O'Connor44c631d2008-03-02 11:24:36 -050055 .dbt = {
Nikolay Nikolovf9cc9692018-02-10 13:52:14 +020056 .specify1 = FLOPPY_SPECIFY1,
57 .specify2 = FLOPPY_SPECIFY2,
Kevin O'Connor7b184d82009-08-23 11:56:55 -040058 .shutoff_ticks = FLOPPY_MOTOR_TICKS, // ~2 seconds
59 .bps_code = FLOPPY_SIZE_CODE,
Kevin O'Connor44c631d2008-03-02 11:24:36 -050060 .sectors = 18,
Kevin O'Connor7b184d82009-08-23 11:56:55 -040061 .interblock_len = FLOPPY_GAPLEN,
62 .data_len = FLOPPY_DATALEN,
63 .gap_len = FLOPPY_FORMAT_GAPLEN,
64 .fill_byte = FLOPPY_FILLBYTE,
65 .settle_time = 0x0F, // 15ms
66 .startup_time = 0x08, // 1 second
Kevin O'Connor44c631d2008-03-02 11:24:36 -050067 },
68 .max_track = 79, // maximum track
69 .data_rate = 0, // data transfer rate
70 .drive_type = 4, // drive type in cmos
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050071};
72
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040073struct floppyinfo_s {
74 struct chs_s chs;
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050075 u8 floppy_size;
76 u8 data_rate;
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040077};
78
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050079#define FLOPPY_SIZE_525 0x01
80#define FLOPPY_SIZE_350 0x02
81
82#define FLOPPY_RATE_500K 0x00
83#define FLOPPY_RATE_300K 0x01
84#define FLOPPY_RATE_250K 0x02
85#define FLOPPY_RATE_1M 0x03
86
Kevin O'Connor89a2f962013-02-18 23:36:03 -050087struct floppyinfo_s FloppyInfo[] VARFSEG = {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040088 // Unknown
89 { {0, 0, 0}, 0x00, 0x00},
90 // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050091 { {2, 40, 9}, FLOPPY_SIZE_525, FLOPPY_RATE_300K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040092 // 2 - 1.2MB, 5.25" - 2 heads, 80 tracks, 15 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050093 { {2, 80, 15}, FLOPPY_SIZE_525, FLOPPY_RATE_500K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040094 // 3 - 720KB, 3.5" - 2 heads, 80 tracks, 9 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050095 { {2, 80, 9}, FLOPPY_SIZE_350, FLOPPY_RATE_250K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040096 // 4 - 1.44MB, 3.5" - 2 heads, 80 tracks, 18 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050097 { {2, 80, 18}, FLOPPY_SIZE_350, FLOPPY_RATE_500K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040098 // 5 - 2.88MB, 3.5" - 2 heads, 80 tracks, 36 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050099 { {2, 80, 36}, FLOPPY_SIZE_350, FLOPPY_RATE_1M},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400100 // 6 - 160k, 5.25" - 1 heads, 40 tracks, 8 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500101 { {1, 40, 8}, FLOPPY_SIZE_525, FLOPPY_RATE_250K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400102 // 7 - 180k, 5.25" - 1 heads, 40 tracks, 9 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500103 { {1, 40, 9}, FLOPPY_SIZE_525, FLOPPY_RATE_300K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400104 // 8 - 320k, 5.25" - 2 heads, 40 tracks, 8 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500105 { {2, 40, 8}, FLOPPY_SIZE_525, FLOPPY_RATE_250K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400106};
107
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400108struct drive_s *
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500109init_floppy(int floppyid, int ftype)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400110{
111 if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
112 dprintf(1, "Bad floppy type %d\n", ftype);
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400113 return NULL;
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400114 }
115
Kevin O'Connor1902c942013-10-26 11:48:06 -0400116 struct drive_s *drive = malloc_fseg(sizeof(*drive));
117 if (!drive) {
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500118 warn_noalloc();
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400119 return NULL;
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500120 }
Kevin O'Connor1902c942013-10-26 11:48:06 -0400121 memset(drive, 0, sizeof(*drive));
122 drive->cntl_id = floppyid;
123 drive->type = DTYPE_FLOPPY;
124 drive->blksize = DISK_SECTOR_SIZE;
125 drive->floppy_type = ftype;
126 drive->sectors = (u64)-1;
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400127
Kevin O'Connor1902c942013-10-26 11:48:06 -0400128 memcpy(&drive->lchs, &FloppyInfo[ftype].chs
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400129 , sizeof(FloppyInfo[ftype].chs));
Kevin O'Connor1902c942013-10-26 11:48:06 -0400130 return drive;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500131}
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400132
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500133static void
134addFloppy(int floppyid, int ftype)
135{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400136 struct drive_s *drive = init_floppy(floppyid, ftype);
137 if (!drive)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500138 return;
139 char *desc = znprintf(MAXDESCSIZE, "Floppy [drive %c]", 'A' + floppyid);
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400140 struct pci_device *pci = pci_find_class(PCI_CLASS_BRIDGE_ISA); /* isa-to-pci bridge */
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400141 int prio = bootprio_find_fdc_device(pci, PORT_FD_BASE, floppyid);
Kevin O'Connor1902c942013-10-26 11:48:06 -0400142 boot_add_floppy(drive, desc, prio);
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400143}
144
145void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500146floppy_setup(void)
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400147{
Kevin O'Connoref719892012-11-26 11:14:00 -0500148 memcpy(&diskette_param_table, &diskette_param_table2
149 , sizeof(diskette_param_table));
150 SET_IVT(0x1E, SEGOFF(SEG_BIOS
151 , (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
152
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400153 if (! CONFIG_FLOPPY)
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400154 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400155 dprintf(3, "init floppy drives\n");
Kevin O'Connor88c00ee2008-05-18 00:14:13 -0400156
Kevin O'Connor897fb112013-02-07 23:32:48 -0500157 if (CONFIG_QEMU) {
Kevin O'Connor8b7861c2013-09-15 02:29:06 -0400158 u8 type = rtc_read(CMOS_FLOPPY_DRIVE_TYPE);
Kevin O'Connor897fb112013-02-07 23:32:48 -0500159 if (type & 0xf0)
160 addFloppy(0, type >> 4);
161 if (type & 0x0f)
162 addFloppy(1, type & 0x0f);
163 } else {
Kevin O'Connor8b73b832012-11-26 11:18:11 -0500164 u8 type = romfile_loadint("etc/floppy0", 0);
165 if (type)
166 addFloppy(0, type);
167 type = romfile_loadint("etc/floppy1", 0);
168 if (type)
169 addFloppy(1, type);
Kevin O'Connor88c00ee2008-05-18 00:14:13 -0400170 }
Kevin O'Connor88c00ee2008-05-18 00:14:13 -0400171
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -0400172 enable_hwirq(6, FUNC16(entry_0e));
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400173}
174
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400175// Find a floppy type that matches a given image size.
176int
177find_floppy_type(u32 size)
178{
179 int i;
180 for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
181 struct chs_s *c = &FloppyInfo[i].chs;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400182 if (c->cylinder * c->head * c->sector * DISK_SECTOR_SIZE == size)
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400183 return i;
184 }
185 return -1;
186}
187
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500188
189/****************************************************************
190 * Low-level floppy IO
191 ****************************************************************/
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500192
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500193u8 FloppyDOR VARLOW;
194
Nikolay Nikolov3ddb42f2018-02-04 17:26:56 +0200195static inline u8
196floppy_dor_read(void)
197{
198 return GET_LOW(FloppyDOR);
199}
200
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500201static inline void
202floppy_dor_write(u8 val)
203{
204 outb(val, PORT_FD_DOR);
205 SET_LOW(FloppyDOR, val);
206}
207
Nikolay Nikolov5157a782018-02-04 17:26:57 +0200208static inline void
209floppy_dor_mask(u8 off, u8 on)
210{
211 floppy_dor_write((floppy_dor_read() & ~off) | on);
212}
213
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500214static void
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500215floppy_disable_controller(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500216{
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500217 dprintf(2, "Floppy_disable_controller\n");
Nikolay Nikolov53d82f02018-02-04 17:26:59 +0200218 // Clear the reset bit (enter reset state) and clear 'enable IRQ and DMA'
219 floppy_dor_mask(FLOPPY_DOR_IRQ | FLOPPY_DOR_RESET, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500220}
221
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500222static int
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500223floppy_wait_irq(void)
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500224{
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500225 u8 frs = GET_BDA(floppy_recalibration_status);
226 SET_BDA(floppy_recalibration_status, frs & ~FRS_IRQ);
Nikolay Nikolov4a6dbce2018-02-04 17:27:01 +0200227 u32 end = timer_calc(FLOPPY_IRQ_TIMEOUT);
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500228 for (;;) {
Nikolay Nikolov4a6dbce2018-02-04 17:27:01 +0200229 if (timer_check(end)) {
Kevin O'Connorb66456c2013-12-04 10:31:51 -0500230 warn_timeout();
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500231 floppy_disable_controller();
232 return DISK_RET_ETIMEOUT;
233 }
Kevin O'Connore51316d2012-06-10 09:09:22 -0400234 frs = GET_BDA(floppy_recalibration_status);
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500235 if (frs & FRS_IRQ)
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500236 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400237 // Could use yield_toirq() here, but that causes issues on
Kevin O'Connor7a98fd02010-01-17 13:00:49 -0500238 // bochs, so use yield() instead.
239 yield();
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500240 }
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500241
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500242 SET_BDA(floppy_recalibration_status, frs & ~FRS_IRQ);
243 return DISK_RET_SUCCESS;
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500244}
245
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500246// Floppy commands
247#define FCF_WAITIRQ 0x10000
248#define FC_CHECKIRQ (0x08 | (0<<8) | (2<<12))
Kevin O'Connor8615f622013-12-11 19:38:15 -0500249#define FC_SEEK (0x0f | (2<<8) | (0<<12) | FCF_WAITIRQ)
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500250#define FC_RECALIBRATE (0x07 | (1<<8) | (0<<12) | FCF_WAITIRQ)
251#define FC_READID (0x4a | (1<<8) | (7<<12) | FCF_WAITIRQ)
252#define FC_READ (0xe6 | (8<<8) | (7<<12) | FCF_WAITIRQ)
253#define FC_WRITE (0xc5 | (8<<8) | (7<<12) | FCF_WAITIRQ)
254#define FC_FORMAT (0x4d | (5<<8) | (7<<12) | FCF_WAITIRQ)
Nikolay Nikolovf9cc9692018-02-10 13:52:14 +0200255#define FC_SPECIFY (0x03 | (2<<8) | (0<<12))
Kevin O'Connor9ba374c2013-03-02 18:37:04 -0500256
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500257// Send the specified command and it's parameters to the floppy controller.
Kevin O'Connora68aeaf2008-07-07 21:37:10 -0400258static int
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500259floppy_pio(int command, u8 *param)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500260{
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500261 dprintf(9, "Floppy pio command %x\n", command);
262 // Send command and parameters to controller.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400263 u32 end = timer_calc(FLOPPY_PIO_TIMEOUT);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500264 int send = (command >> 8) & 0xf;
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500265 int i = 0;
266 for (;;) {
267 u8 sts = inb(PORT_FD_STATUS);
268 if (!(sts & 0x80)) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400269 if (timer_check(end)) {
Kevin O'Connorb66456c2013-12-04 10:31:51 -0500270 warn_timeout();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500271 floppy_disable_controller();
272 return DISK_RET_ETIMEOUT;
273 }
Kevin O'Connorca087772014-11-29 13:04:46 -0500274 yield();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500275 continue;
276 }
277 if (sts & 0x40) {
278 floppy_disable_controller();
279 return DISK_RET_ECONTROLLER;
280 }
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500281 if (i == 0)
282 outb(command & 0xff, PORT_FD_DATA);
283 else
284 outb(param[i-1], PORT_FD_DATA);
285 if (i++ >= send)
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500286 break;
287 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500288
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500289 // Wait for command to complete.
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500290 if (command & FCF_WAITIRQ) {
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500291 int ret = floppy_wait_irq();
292 if (ret)
293 return ret;
Kevin O'Connor06ad44e2008-04-05 19:30:02 -0400294 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500295
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500296 // Read response from controller.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400297 end = timer_calc(FLOPPY_PIO_TIMEOUT);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500298 int receive = (command >> 12) & 0xf;
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500299 i = 0;
300 for (;;) {
301 u8 sts = inb(PORT_FD_STATUS);
302 if (!(sts & 0x80)) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400303 if (timer_check(end)) {
Kevin O'Connorb66456c2013-12-04 10:31:51 -0500304 warn_timeout();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500305 floppy_disable_controller();
306 return DISK_RET_ETIMEOUT;
307 }
Kevin O'Connorca087772014-11-29 13:04:46 -0500308 yield();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500309 continue;
310 }
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500311 if (i >= receive) {
312 if (sts & 0x40) {
313 floppy_disable_controller();
314 return DISK_RET_ECONTROLLER;
315 }
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500316 break;
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500317 }
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500318 if (!(sts & 0x40)) {
319 floppy_disable_controller();
320 return DISK_RET_ECONTROLLER;
321 }
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500322 param[i++] = inb(PORT_FD_DATA);
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500323 }
Kevin O'Connor9ba374c2013-03-02 18:37:04 -0500324
325 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500326}
327
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500328static int
329floppy_enable_controller(void)
330{
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500331 dprintf(2, "Floppy_enable_controller\n");
Nikolay Nikolov53d82f02018-02-04 17:26:59 +0200332 // Clear the reset bit (enter reset state), but set 'enable IRQ and DMA'
333 floppy_dor_mask(FLOPPY_DOR_RESET, FLOPPY_DOR_IRQ);
Nikolay Nikolovb00a03a2018-02-10 13:52:13 +0200334 // Real hardware needs a 4 microsecond delay
335 usleep(4);
Nikolay Nikolov53d82f02018-02-04 17:26:59 +0200336 // Set the reset bit (normal operation) and keep 'enable IRQ and DMA' on
337 floppy_dor_mask(0, FLOPPY_DOR_IRQ | FLOPPY_DOR_RESET);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500338 int ret = floppy_wait_irq();
339 if (ret)
340 return ret;
341
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500342 u8 param[2];
343 return floppy_pio(FC_CHECKIRQ, param);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500344}
345
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500346// Activate a drive and send a command to it.
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500347static int
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500348floppy_drive_pio(u8 floppyid, int command, u8 *param)
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500349{
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500350 // Enable controller if it isn't running.
Nikolay Nikolov53fc6312018-02-04 17:26:58 +0200351 if (!(floppy_dor_read() & FLOPPY_DOR_RESET)) {
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500352 int ret = floppy_enable_controller();
353 if (ret)
354 return ret;
355 }
356
Nikolay Nikolov4a6dbce2018-02-04 17:27:01 +0200357 // set the disk motor timeout value of INT 08 to the highest value
358 SET_BDA(floppy_motor_counter, 255);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500359
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500360 // Turn on motor of selected drive, DMA & int enabled, normal operation
Nikolay Nikolov81f45a62018-02-10 13:52:15 +0200361 floppy_dor_write((FLOPPY_DOR_MOTOR_A << floppyid) | FLOPPY_DOR_IRQ | FLOPPY_DOR_RESET | floppyid);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500362
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500363 // Send command.
364 int ret = floppy_pio(command, param);
Nikolay Nikolov488ede22018-02-04 17:27:00 +0200365 SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS); // reset motor timeout
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500366 if (ret)
367 return ret;
368
369 // Check IRQ command is needed after irq commands with no results
370 if ((command & FCF_WAITIRQ) && ((command >> 12) & 0xf) == 0)
371 return floppy_pio(FC_CHECKIRQ, param);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500372 return DISK_RET_SUCCESS;
373}
374
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500375
376/****************************************************************
Kevin O'Connor8615f622013-12-11 19:38:15 -0500377 * Floppy media sense and seeking
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500378 ****************************************************************/
379
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500380static int
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400381floppy_drive_recal(u8 floppyid)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500382{
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500383 dprintf(2, "Floppy_drive_recal %d\n", floppyid);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500384 // send Recalibrate command to controller
385 u8 param[2];
386 param[0] = floppyid;
387 int ret = floppy_drive_pio(floppyid, FC_RECALIBRATE, param);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500388 if (ret)
389 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500390
Kevin O'Connore51316d2012-06-10 09:09:22 -0400391 u8 frs = GET_BDA(floppy_recalibration_status);
392 SET_BDA(floppy_recalibration_status, frs | (1<<floppyid));
Kevin O'Connor8615f622013-12-11 19:38:15 -0500393 SET_BDA(floppy_track[floppyid], 0);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500394 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500395}
396
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500397static int
Nikolay Nikolovf9cc9692018-02-10 13:52:14 +0200398floppy_drive_specify(void)
399{
400 u8 param[2];
401 param[0] = FLOPPY_SPECIFY1;
402 param[1] = FLOPPY_SPECIFY2;
403 return floppy_pio(FC_SPECIFY, param);
404}
405
406static int
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500407floppy_drive_readid(u8 floppyid, u8 data_rate, u8 head)
408{
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500409 // Set data rate.
410 outb(data_rate, PORT_FD_DIR);
411
412 // send Read Sector Id command
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500413 u8 param[7];
414 param[0] = (head << 2) | floppyid; // HD DR1 DR2
415 int ret = floppy_drive_pio(floppyid, FC_READID, param);
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500416 if (ret)
417 return ret;
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500418 if (param[0] & 0xc0)
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500419 return -1;
420 return 0;
421}
422
423static int
Kevin O'Connor1902c942013-10-26 11:48:06 -0400424floppy_media_sense(struct drive_s *drive_gf)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500425{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400426 u8 ftype = GET_GLOBALFLAT(drive_gf->floppy_type), stype = ftype;
427 u8 floppyid = GET_GLOBALFLAT(drive_gf->cntl_id);
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500428
429 u8 data_rate = GET_GLOBAL(FloppyInfo[stype].data_rate);
430 int ret = floppy_drive_readid(floppyid, data_rate, 0);
431 if (ret) {
432 // Attempt media sense.
433 for (stype=1; ; stype++) {
434 if (stype >= ARRAY_SIZE(FloppyInfo))
435 return DISK_RET_EMEDIA;
436 if (stype==ftype
437 || (GET_GLOBAL(FloppyInfo[stype].floppy_size)
438 != GET_GLOBAL(FloppyInfo[ftype].floppy_size))
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400439 || (GET_GLOBAL(FloppyInfo[stype].chs.head)
440 > GET_GLOBAL(FloppyInfo[ftype].chs.head))
441 || (GET_GLOBAL(FloppyInfo[stype].chs.cylinder)
442 > GET_GLOBAL(FloppyInfo[ftype].chs.cylinder))
443 || (GET_GLOBAL(FloppyInfo[stype].chs.sector)
444 > GET_GLOBAL(FloppyInfo[ftype].chs.sector)))
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500445 continue;
446 data_rate = GET_GLOBAL(FloppyInfo[stype].data_rate);
447 ret = floppy_drive_readid(floppyid, data_rate, 0);
448 if (!ret)
449 break;
450 }
451 }
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500452 dprintf(2, "Floppy_media_sense on drive %d found rate %d\n"
453 , floppyid, data_rate);
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500454
455 u8 old_data_rate = GET_BDA(floppy_media_state[floppyid]) >> 6;
456 SET_BDA(floppy_last_data_rate, (old_data_rate<<2) | (data_rate<<6));
457 u8 media = (stype == 1 ? 0x04 : (stype == 2 ? 0x05 : 0x07));
458 u8 fms = (data_rate<<6) | FMS_MEDIA_DRIVE_ESTABLISHED | media;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400459 if (GET_GLOBAL(FloppyInfo[stype].chs.cylinder)
460 < GET_GLOBAL(FloppyInfo[ftype].chs.cylinder))
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500461 fms |= FMS_DOUBLE_STEPPING;
462 SET_BDA(floppy_media_state[floppyid], fms);
463
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400464 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500465}
466
Kevin O'Connor8615f622013-12-11 19:38:15 -0500467// Prepare a floppy for a data transfer.
Kevin O'Connora68aeaf2008-07-07 21:37:10 -0400468static int
Kevin O'Connor8615f622013-12-11 19:38:15 -0500469floppy_prep(struct drive_s *drive_gf, u8 cylinder)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500470{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400471 u8 floppyid = GET_GLOBALFLAT(drive_gf->cntl_id);
Kevin O'Connor8615f622013-12-11 19:38:15 -0500472 if (!(GET_BDA(floppy_recalibration_status) & (1<<floppyid)) ||
473 !(GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED)) {
474 // Recalibrate drive.
475 int ret = floppy_drive_recal(floppyid);
476 if (ret)
477 return ret;
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500478
Kevin O'Connor8615f622013-12-11 19:38:15 -0500479 // Sense media.
480 ret = floppy_media_sense(drive_gf);
481 if (ret)
482 return ret;
Nikolay Nikolovf9cc9692018-02-10 13:52:14 +0200483
484 // Execute a SPECIFY command (sets the Step Rate Time,
485 // Head Load Time, Head Unload Time and the DMA enable/disable bit).
486 ret = floppy_drive_specify();
487 if (ret)
488 return ret;
Kevin O'Connor8615f622013-12-11 19:38:15 -0500489 }
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500490
Kevin O'Connor8615f622013-12-11 19:38:15 -0500491 // Seek to cylinder if needed.
492 u8 lastcyl = GET_BDA(floppy_track[floppyid]);
493 if (cylinder != lastcyl) {
494 u8 param[2];
495 param[0] = floppyid;
496 param[1] = cylinder;
497 int ret = floppy_drive_pio(floppyid, FC_SEEK, param);
498 if (ret)
499 return ret;
500 SET_BDA(floppy_track[floppyid], cylinder);
501 }
502
503 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500504}
505
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500506
507/****************************************************************
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400508 * Floppy DMA transfer
Kevin O'Connorc8494052013-03-03 10:27:30 -0500509 ****************************************************************/
510
511// Perform a floppy transfer command (setup DMA and issue PIO).
512static int
Kevin O'Connor8615f622013-12-11 19:38:15 -0500513floppy_dma_cmd(struct disk_op_s *op, int count, int command, u8 *param)
Kevin O'Connorc8494052013-03-03 10:27:30 -0500514{
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400515 // Setup DMA controller
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500516 int isWrite = command != FC_READ;
Kevin O'Connor8615f622013-12-11 19:38:15 -0500517 int ret = dma_floppy((u32)op->buf_fl, count, isWrite);
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400518 if (ret)
Kevin O'Connorc8494052013-03-03 10:27:30 -0500519 return DISK_RET_EBOUNDARY;
520
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400521 // Invoke floppy controller
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400522 u8 floppyid = GET_GLOBALFLAT(op->drive_fl->cntl_id);
Kevin O'Connor8615f622013-12-11 19:38:15 -0500523 ret = floppy_drive_pio(floppyid, command, param);
Kevin O'Connorc8494052013-03-03 10:27:30 -0500524 if (ret)
525 return ret;
526
527 // Populate floppy_return_status in BDA
528 int i;
529 for (i=0; i<7; i++)
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500530 SET_BDA(floppy_return_status[i], param[i]);
Kevin O'Connorc8494052013-03-03 10:27:30 -0500531
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500532 if (param[0] & 0xc0) {
533 if (param[1] & 0x02)
Kevin O'Connorc8494052013-03-03 10:27:30 -0500534 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor8413b322013-03-09 20:07:38 -0500535 dprintf(1, "floppy error: %02x %02x %02x %02x %02x %02x %02x\n"
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500536 , param[0], param[1], param[2], param[3]
537 , param[4], param[5], param[6]);
Kevin O'Connorc8494052013-03-03 10:27:30 -0500538 return DISK_RET_ECONTROLLER;
539 }
540
Kevin O'Connorc8494052013-03-03 10:27:30 -0500541 return DISK_RET_SUCCESS;
542}
543
544
545/****************************************************************
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400546 * Floppy handlers
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500547 ****************************************************************/
548
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400549static struct chs_s
550lba2chs(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500551{
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400552 struct chs_s res = { };
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400553
Kevin O'Connor302a6e82013-12-07 11:46:37 -0500554 u32 tmp = op->lba;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400555 u16 nls = GET_GLOBALFLAT(op->drive_fl->lchs.sector);
Kevin O'Connor302a6e82013-12-07 11:46:37 -0500556 res.sector = (tmp % nls) + 1;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400557
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400558 tmp /= nls;
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400559 u16 nlh = GET_GLOBALFLAT(op->drive_fl->lchs.head);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400560 res.head = tmp % nlh;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400561
562 tmp /= nlh;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400563 res.cylinder = tmp;
564
565 return res;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400566}
567
568// diskette controller reset
569static int
570floppy_reset(struct disk_op_s *op)
571{
Kevin O'Connor5bb75522013-03-03 15:32:31 -0500572 SET_BDA(floppy_recalibration_status, 0);
573 SET_BDA(floppy_media_state[0], 0);
574 SET_BDA(floppy_media_state[1], 0);
575 SET_BDA(floppy_track[0], 0);
576 SET_BDA(floppy_track[1], 0);
577 SET_BDA(floppy_last_data_rate, 0);
578 floppy_disable_controller();
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500579 return floppy_enable_controller();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500580}
581
582// Read Diskette Sectors
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400583static int
584floppy_read(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500585{
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400586 struct chs_s chs = lba2chs(op);
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400587 int ret = floppy_prep(op->drive_fl, chs.cylinder);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500588 if (ret)
589 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500590
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500591 // send read-normal-data command to controller
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400592 u8 floppyid = GET_GLOBALFLAT(op->drive_fl->cntl_id);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500593 u8 param[8];
594 param[0] = (chs.head << 2) | floppyid; // HD DR1 DR2
595 param[1] = chs.cylinder;
596 param[2] = chs.head;
597 param[3] = chs.sector;
598 param[4] = FLOPPY_SIZE_CODE;
599 param[5] = chs.sector + op->count - 1; // last sector to read on track
600 param[6] = FLOPPY_GAPLEN;
601 param[7] = FLOPPY_DATALEN;
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500602 return floppy_dma_cmd(op, op->count * DISK_SECTOR_SIZE, FC_READ, param);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500603}
604
605// Write Diskette Sectors
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400606static int
607floppy_write(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500608{
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400609 struct chs_s chs = lba2chs(op);
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400610 int ret = floppy_prep(op->drive_fl, chs.cylinder);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500611 if (ret)
612 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500613
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500614 // send write-normal-data command to controller
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400615 u8 floppyid = GET_GLOBALFLAT(op->drive_fl->cntl_id);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500616 u8 param[8];
617 param[0] = (chs.head << 2) | floppyid; // HD DR1 DR2
618 param[1] = chs.cylinder;
619 param[2] = chs.head;
620 param[3] = chs.sector;
621 param[4] = FLOPPY_SIZE_CODE;
622 param[5] = chs.sector + op->count - 1; // last sector to write on track
623 param[6] = FLOPPY_GAPLEN;
624 param[7] = FLOPPY_DATALEN;
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500625 return floppy_dma_cmd(op, op->count * DISK_SECTOR_SIZE, FC_WRITE, param);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500626}
627
628// Verify Diskette Sectors
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400629static int
630floppy_verify(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500631{
Kevin O'Connor8615f622013-12-11 19:38:15 -0500632 struct chs_s chs = lba2chs(op);
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400633 int ret = floppy_prep(op->drive_fl, chs.cylinder);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500634 if (ret)
635 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500636
Kevin O'Connor8615f622013-12-11 19:38:15 -0500637 // This command isn't implemented - just return success.
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400638 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500639}
640
641// format diskette track
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400642static int
643floppy_format(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500644{
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500645 struct chs_s chs = lba2chs(op);
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400646 int ret = floppy_prep(op->drive_fl, chs.cylinder);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500647 if (ret)
648 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500649
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500650 // send format-track command to controller
Kevin O'Connore5a0b612017-07-11 12:24:50 -0400651 u8 floppyid = GET_GLOBALFLAT(op->drive_fl->cntl_id);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500652 u8 param[7];
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500653 param[0] = (chs.head << 2) | floppyid; // HD DR1 DR2
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500654 param[1] = FLOPPY_SIZE_CODE;
655 param[2] = op->count; // number of sectors per track
656 param[3] = FLOPPY_FORMAT_GAPLEN;
657 param[4] = FLOPPY_FILLBYTE;
Kevin O'Connor8615f622013-12-11 19:38:15 -0500658 return floppy_dma_cmd(op, op->count * 4, FC_FORMAT, param);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500659}
660
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400661int
Kevin O'Connor17856452015-07-07 14:56:20 -0400662floppy_process_op(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500663{
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400664 if (!CONFIG_FLOPPY)
665 return 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500666
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400667 switch (op->command) {
668 case CMD_RESET:
669 return floppy_reset(op);
670 case CMD_READ:
671 return floppy_read(op);
672 case CMD_WRITE:
673 return floppy_write(op);
674 case CMD_VERIFY:
675 return floppy_verify(op);
676 case CMD_FORMAT:
677 return floppy_format(op);
678 default:
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400679 return DISK_RET_EPARAM;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500680 }
681}
682
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500683
684/****************************************************************
685 * HW irqs
686 ****************************************************************/
687
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500688// INT 0Eh Diskette Hardware ISR Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500689void VISIBLE16
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400690handle_0e(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500691{
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400692 if (! CONFIG_FLOPPY)
Kevin O'Connorff5e0052012-12-07 11:25:58 -0500693 return;
694 debug_isr(DEBUG_ISR_0e);
Kevin O'Connor40967022008-07-21 22:23:05 -0400695
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500696 // diskette interrupt has occurred
Kevin O'Connore51316d2012-06-10 09:09:22 -0400697 u8 frs = GET_BDA(floppy_recalibration_status);
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500698 SET_BDA(floppy_recalibration_status, frs | FRS_IRQ);
Kevin O'Connor40967022008-07-21 22:23:05 -0400699
Kevin O'Connoraa7c2342013-07-14 15:07:21 -0400700 pic_eoi1();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500701}
702
703// Called from int08 handler.
704void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500705floppy_tick(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500706{
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400707 if (! CONFIG_FLOPPY)
Kevin O'Connor40967022008-07-21 22:23:05 -0400708 return;
709
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500710 // time to turn off drive(s)?
711 u8 fcount = GET_BDA(floppy_motor_counter);
712 if (fcount) {
713 fcount--;
714 SET_BDA(floppy_motor_counter, fcount);
715 if (fcount == 0)
716 // turn motor(s) off
Nikolay Nikolov53fc6312018-02-04 17:26:58 +0200717 floppy_dor_mask(FLOPPY_DOR_MOTOR_MASK, 0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500718 }
719}