blob: 98ed9bbf40cd36cbb3b83577536a79d71ad501c7 [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
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050037
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050038// 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'Connor89a2f962013-02-18 23:36:03 -050042struct floppy_ext_dbt_s diskette_param_table2 VARFSEG = {
Kevin O'Connor44c631d2008-03-02 11:24:36 -050043 .dbt = {
Kevin O'Connor7b184d82009-08-23 11:56:55 -040044 .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'Connor44c631d2008-03-02 11:24:36 -050048 .sectors = 18,
Kevin O'Connor7b184d82009-08-23 11:56:55 -040049 .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'Connor44c631d2008-03-02 11:24:36 -050055 },
56 .max_track = 79, // maximum track
57 .data_rate = 0, // data transfer rate
58 .drive_type = 4, // drive type in cmos
Kevin O'Connorf076a3e2008-02-25 22:25:15 -050059};
60
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040061struct floppyinfo_s {
62 struct chs_s chs;
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050063 u8 floppy_size;
64 u8 data_rate;
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040065};
66
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050067#define FLOPPY_SIZE_525 0x01
68#define FLOPPY_SIZE_350 0x02
69
70#define FLOPPY_RATE_500K 0x00
71#define FLOPPY_RATE_300K 0x01
72#define FLOPPY_RATE_250K 0x02
73#define FLOPPY_RATE_1M 0x03
74
Kevin O'Connor89a2f962013-02-18 23:36:03 -050075struct floppyinfo_s FloppyInfo[] VARFSEG = {
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040076 // Unknown
77 { {0, 0, 0}, 0x00, 0x00},
78 // 1 - 360KB, 5.25" - 2 heads, 40 tracks, 9 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050079 { {2, 40, 9}, FLOPPY_SIZE_525, FLOPPY_RATE_300K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040080 // 2 - 1.2MB, 5.25" - 2 heads, 80 tracks, 15 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050081 { {2, 80, 15}, FLOPPY_SIZE_525, FLOPPY_RATE_500K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040082 // 3 - 720KB, 3.5" - 2 heads, 80 tracks, 9 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050083 { {2, 80, 9}, FLOPPY_SIZE_350, FLOPPY_RATE_250K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040084 // 4 - 1.44MB, 3.5" - 2 heads, 80 tracks, 18 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050085 { {2, 80, 18}, FLOPPY_SIZE_350, FLOPPY_RATE_500K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040086 // 5 - 2.88MB, 3.5" - 2 heads, 80 tracks, 36 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050087 { {2, 80, 36}, FLOPPY_SIZE_350, FLOPPY_RATE_1M},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040088 // 6 - 160k, 5.25" - 1 heads, 40 tracks, 8 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050089 { {1, 40, 8}, FLOPPY_SIZE_525, FLOPPY_RATE_250K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040090 // 7 - 180k, 5.25" - 1 heads, 40 tracks, 9 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050091 { {1, 40, 9}, FLOPPY_SIZE_525, FLOPPY_RATE_300K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040092 // 8 - 320k, 5.25" - 2 heads, 40 tracks, 8 sectors
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -050093 { {2, 40, 8}, FLOPPY_SIZE_525, FLOPPY_RATE_250K},
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040094};
95
Kevin O'Connor77d227b2009-10-22 21:48:39 -040096struct drive_s *
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -050097init_floppy(int floppyid, int ftype)
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -040098{
99 if (ftype <= 0 || ftype >= ARRAY_SIZE(FloppyInfo)) {
100 dprintf(1, "Bad floppy type %d\n", ftype);
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400101 return NULL;
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400102 }
103
Kevin O'Connor1902c942013-10-26 11:48:06 -0400104 struct drive_s *drive = malloc_fseg(sizeof(*drive));
105 if (!drive) {
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500106 warn_noalloc();
Kevin O'Connor77d227b2009-10-22 21:48:39 -0400107 return NULL;
Kevin O'Connord7e998f2010-02-15 22:48:28 -0500108 }
Kevin O'Connor1902c942013-10-26 11:48:06 -0400109 memset(drive, 0, sizeof(*drive));
110 drive->cntl_id = floppyid;
111 drive->type = DTYPE_FLOPPY;
112 drive->blksize = DISK_SECTOR_SIZE;
113 drive->floppy_type = ftype;
114 drive->sectors = (u64)-1;
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400115
Kevin O'Connor1902c942013-10-26 11:48:06 -0400116 memcpy(&drive->lchs, &FloppyInfo[ftype].chs
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400117 , sizeof(FloppyInfo[ftype].chs));
Kevin O'Connor1902c942013-10-26 11:48:06 -0400118 return drive;
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500119}
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400120
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500121static void
122addFloppy(int floppyid, int ftype)
123{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400124 struct drive_s *drive = init_floppy(floppyid, ftype);
125 if (!drive)
Kevin O'Connorca2bc1c2010-12-29 21:41:19 -0500126 return;
127 char *desc = znprintf(MAXDESCSIZE, "Floppy [drive %c]", 'A' + floppyid);
Kevin O'Connor0cd70052011-07-02 14:04:19 -0400128 struct pci_device *pci = pci_find_class(PCI_CLASS_BRIDGE_ISA); /* isa-to-pci bridge */
Kevin O'Connor03e589c2011-07-09 14:35:37 -0400129 int prio = bootprio_find_fdc_device(pci, PORT_FD_BASE, floppyid);
Kevin O'Connor1902c942013-10-26 11:48:06 -0400130 boot_add_floppy(drive, desc, prio);
Kevin O'Connor51fd0a12009-09-12 13:20:14 -0400131}
132
133void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500134floppy_setup(void)
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400135{
Kevin O'Connoref719892012-11-26 11:14:00 -0500136 memcpy(&diskette_param_table, &diskette_param_table2
137 , sizeof(diskette_param_table));
138 SET_IVT(0x1E, SEGOFF(SEG_BIOS
139 , (u32)&diskette_param_table2 - BUILD_BIOS_ADDR));
140
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400141 if (! CONFIG_FLOPPY)
Kevin O'Connorf54c1502008-06-14 15:56:16 -0400142 return;
Kevin O'Connor35192dd2008-06-08 19:18:33 -0400143 dprintf(3, "init floppy drives\n");
Kevin O'Connor88c00ee2008-05-18 00:14:13 -0400144
Kevin O'Connor897fb112013-02-07 23:32:48 -0500145 if (CONFIG_QEMU) {
Kevin O'Connor8b7861c2013-09-15 02:29:06 -0400146 u8 type = rtc_read(CMOS_FLOPPY_DRIVE_TYPE);
Kevin O'Connor897fb112013-02-07 23:32:48 -0500147 if (type & 0xf0)
148 addFloppy(0, type >> 4);
149 if (type & 0x0f)
150 addFloppy(1, type & 0x0f);
151 } else {
Kevin O'Connor8b73b832012-11-26 11:18:11 -0500152 u8 type = romfile_loadint("etc/floppy0", 0);
153 if (type)
154 addFloppy(0, type);
155 type = romfile_loadint("etc/floppy1", 0);
156 if (type)
157 addFloppy(1, type);
Kevin O'Connor88c00ee2008-05-18 00:14:13 -0400158 }
Kevin O'Connor88c00ee2008-05-18 00:14:13 -0400159
Kevin O'Connorcc9e1bf2010-07-28 21:31:38 -0400160 enable_hwirq(6, FUNC16(entry_0e));
Kevin O'Connor3bbcc142008-04-13 17:07:33 -0400161}
162
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400163// Find a floppy type that matches a given image size.
164int
165find_floppy_type(u32 size)
166{
167 int i;
168 for (i=1; i<ARRAY_SIZE(FloppyInfo); i++) {
169 struct chs_s *c = &FloppyInfo[i].chs;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400170 if (c->cylinder * c->head * c->sector * DISK_SECTOR_SIZE == size)
Kevin O'Connora3855ad2009-08-16 21:59:40 -0400171 return i;
172 }
173 return -1;
174}
175
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500176
177/****************************************************************
178 * Low-level floppy IO
179 ****************************************************************/
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500180
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500181u8 FloppyDOR VARLOW;
182
183static inline void
184floppy_dor_write(u8 val)
185{
186 outb(val, PORT_FD_DOR);
187 SET_LOW(FloppyDOR, val);
188}
189
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500190static void
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500191floppy_disable_controller(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500192{
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500193 dprintf(2, "Floppy_disable_controller\n");
194 floppy_dor_write(0x00);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500195}
196
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500197static int
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500198floppy_wait_irq(void)
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500199{
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500200 u8 frs = GET_BDA(floppy_recalibration_status);
201 SET_BDA(floppy_recalibration_status, frs & ~FRS_IRQ);
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500202 for (;;) {
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500203 if (!GET_BDA(floppy_motor_counter)) {
Kevin O'Connorb66456c2013-12-04 10:31:51 -0500204 warn_timeout();
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500205 floppy_disable_controller();
206 return DISK_RET_ETIMEOUT;
207 }
Kevin O'Connore51316d2012-06-10 09:09:22 -0400208 frs = GET_BDA(floppy_recalibration_status);
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500209 if (frs & FRS_IRQ)
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500210 break;
Kevin O'Connor94c749c2012-05-28 11:44:02 -0400211 // Could use yield_toirq() here, but that causes issues on
Kevin O'Connor7a98fd02010-01-17 13:00:49 -0500212 // bochs, so use yield() instead.
213 yield();
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500214 }
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500215
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500216 SET_BDA(floppy_recalibration_status, frs & ~FRS_IRQ);
217 return DISK_RET_SUCCESS;
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500218}
219
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500220// Floppy commands
221#define FCF_WAITIRQ 0x10000
222#define FC_CHECKIRQ (0x08 | (0<<8) | (2<<12))
Kevin O'Connor8615f622013-12-11 19:38:15 -0500223#define FC_SEEK (0x0f | (2<<8) | (0<<12) | FCF_WAITIRQ)
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500224#define FC_RECALIBRATE (0x07 | (1<<8) | (0<<12) | FCF_WAITIRQ)
225#define FC_READID (0x4a | (1<<8) | (7<<12) | FCF_WAITIRQ)
226#define FC_READ (0xe6 | (8<<8) | (7<<12) | FCF_WAITIRQ)
227#define FC_WRITE (0xc5 | (8<<8) | (7<<12) | FCF_WAITIRQ)
228#define FC_FORMAT (0x4d | (5<<8) | (7<<12) | FCF_WAITIRQ)
Kevin O'Connor9ba374c2013-03-02 18:37:04 -0500229
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500230// Send the specified command and it's parameters to the floppy controller.
Kevin O'Connora68aeaf2008-07-07 21:37:10 -0400231static int
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500232floppy_pio(int command, u8 *param)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500233{
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500234 dprintf(9, "Floppy pio command %x\n", command);
235 // Send command and parameters to controller.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400236 u32 end = timer_calc(FLOPPY_PIO_TIMEOUT);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500237 int send = (command >> 8) & 0xf;
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500238 int i = 0;
239 for (;;) {
240 u8 sts = inb(PORT_FD_STATUS);
241 if (!(sts & 0x80)) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400242 if (timer_check(end)) {
Kevin O'Connorb66456c2013-12-04 10:31:51 -0500243 warn_timeout();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500244 floppy_disable_controller();
245 return DISK_RET_ETIMEOUT;
246 }
Kevin O'Connorca087772014-11-29 13:04:46 -0500247 yield();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500248 continue;
249 }
250 if (sts & 0x40) {
251 floppy_disable_controller();
252 return DISK_RET_ECONTROLLER;
253 }
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500254 if (i == 0)
255 outb(command & 0xff, PORT_FD_DATA);
256 else
257 outb(param[i-1], PORT_FD_DATA);
258 if (i++ >= send)
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500259 break;
260 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500261
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500262 // Wait for command to complete.
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500263 if (command & FCF_WAITIRQ) {
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500264 int ret = floppy_wait_irq();
265 if (ret)
266 return ret;
Kevin O'Connor06ad44e2008-04-05 19:30:02 -0400267 }
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500268
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500269 // Read response from controller.
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400270 end = timer_calc(FLOPPY_PIO_TIMEOUT);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500271 int receive = (command >> 12) & 0xf;
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500272 i = 0;
273 for (;;) {
274 u8 sts = inb(PORT_FD_STATUS);
275 if (!(sts & 0x80)) {
Kevin O'Connor018bdd72013-07-20 18:22:57 -0400276 if (timer_check(end)) {
Kevin O'Connorb66456c2013-12-04 10:31:51 -0500277 warn_timeout();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500278 floppy_disable_controller();
279 return DISK_RET_ETIMEOUT;
280 }
Kevin O'Connorca087772014-11-29 13:04:46 -0500281 yield();
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500282 continue;
283 }
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500284 if (i >= receive) {
285 if (sts & 0x40) {
286 floppy_disable_controller();
287 return DISK_RET_ECONTROLLER;
288 }
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500289 break;
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500290 }
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500291 if (!(sts & 0x40)) {
292 floppy_disable_controller();
293 return DISK_RET_ECONTROLLER;
294 }
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500295 param[i++] = inb(PORT_FD_DATA);
Kevin O'Connor068cb4f2013-03-03 11:05:19 -0500296 }
Kevin O'Connor9ba374c2013-03-02 18:37:04 -0500297
298 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500299}
300
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500301static int
302floppy_enable_controller(void)
303{
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500304 dprintf(2, "Floppy_enable_controller\n");
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500305 SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500306 floppy_dor_write(0x00);
307 floppy_dor_write(0x0c);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500308 int ret = floppy_wait_irq();
309 if (ret)
310 return ret;
311
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500312 u8 param[2];
313 return floppy_pio(FC_CHECKIRQ, param);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500314}
315
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500316// Activate a drive and send a command to it.
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500317static int
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500318floppy_drive_pio(u8 floppyid, int command, u8 *param)
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500319{
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500320 // Enable controller if it isn't running.
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500321 if (!(GET_LOW(FloppyDOR) & 0x04)) {
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500322 int ret = floppy_enable_controller();
323 if (ret)
324 return ret;
325 }
326
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500327 // reset the disk motor timeout value of INT 08
328 SET_BDA(floppy_motor_counter, FLOPPY_MOTOR_TICKS);
329
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500330 // Turn on motor of selected drive, DMA & int enabled, normal operation
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500331 floppy_dor_write((floppyid ? 0x20 : 0x10) | 0x0c | floppyid);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500332
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500333 // Send command.
334 int ret = floppy_pio(command, param);
335 if (ret)
336 return ret;
337
338 // Check IRQ command is needed after irq commands with no results
339 if ((command & FCF_WAITIRQ) && ((command >> 12) & 0xf) == 0)
340 return floppy_pio(FC_CHECKIRQ, param);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500341 return DISK_RET_SUCCESS;
342}
343
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500344
345/****************************************************************
Kevin O'Connor8615f622013-12-11 19:38:15 -0500346 * Floppy media sense and seeking
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500347 ****************************************************************/
348
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500349static int
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400350floppy_drive_recal(u8 floppyid)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500351{
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500352 dprintf(2, "Floppy_drive_recal %d\n", floppyid);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500353 // send Recalibrate command to controller
354 u8 param[2];
355 param[0] = floppyid;
356 int ret = floppy_drive_pio(floppyid, FC_RECALIBRATE, param);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500357 if (ret)
358 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500359
Kevin O'Connore51316d2012-06-10 09:09:22 -0400360 u8 frs = GET_BDA(floppy_recalibration_status);
361 SET_BDA(floppy_recalibration_status, frs | (1<<floppyid));
Kevin O'Connor8615f622013-12-11 19:38:15 -0500362 SET_BDA(floppy_track[floppyid], 0);
Kevin O'Connor1f711d22013-03-02 21:26:54 -0500363 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500364}
365
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500366static int
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500367floppy_drive_readid(u8 floppyid, u8 data_rate, u8 head)
368{
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500369 // Set data rate.
370 outb(data_rate, PORT_FD_DIR);
371
372 // send Read Sector Id command
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500373 u8 param[7];
374 param[0] = (head << 2) | floppyid; // HD DR1 DR2
375 int ret = floppy_drive_pio(floppyid, FC_READID, param);
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500376 if (ret)
377 return ret;
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500378 if (param[0] & 0xc0)
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500379 return -1;
380 return 0;
381}
382
383static int
Kevin O'Connor1902c942013-10-26 11:48:06 -0400384floppy_media_sense(struct drive_s *drive_gf)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500385{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400386 u8 ftype = GET_GLOBALFLAT(drive_gf->floppy_type), stype = ftype;
387 u8 floppyid = GET_GLOBALFLAT(drive_gf->cntl_id);
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500388
389 u8 data_rate = GET_GLOBAL(FloppyInfo[stype].data_rate);
390 int ret = floppy_drive_readid(floppyid, data_rate, 0);
391 if (ret) {
392 // Attempt media sense.
393 for (stype=1; ; stype++) {
394 if (stype >= ARRAY_SIZE(FloppyInfo))
395 return DISK_RET_EMEDIA;
396 if (stype==ftype
397 || (GET_GLOBAL(FloppyInfo[stype].floppy_size)
398 != GET_GLOBAL(FloppyInfo[ftype].floppy_size))
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400399 || (GET_GLOBAL(FloppyInfo[stype].chs.head)
400 > GET_GLOBAL(FloppyInfo[ftype].chs.head))
401 || (GET_GLOBAL(FloppyInfo[stype].chs.cylinder)
402 > GET_GLOBAL(FloppyInfo[ftype].chs.cylinder))
403 || (GET_GLOBAL(FloppyInfo[stype].chs.sector)
404 > GET_GLOBAL(FloppyInfo[ftype].chs.sector)))
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500405 continue;
406 data_rate = GET_GLOBAL(FloppyInfo[stype].data_rate);
407 ret = floppy_drive_readid(floppyid, data_rate, 0);
408 if (!ret)
409 break;
410 }
411 }
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500412 dprintf(2, "Floppy_media_sense on drive %d found rate %d\n"
413 , floppyid, data_rate);
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500414
415 u8 old_data_rate = GET_BDA(floppy_media_state[floppyid]) >> 6;
416 SET_BDA(floppy_last_data_rate, (old_data_rate<<2) | (data_rate<<6));
417 u8 media = (stype == 1 ? 0x04 : (stype == 2 ? 0x05 : 0x07));
418 u8 fms = (data_rate<<6) | FMS_MEDIA_DRIVE_ESTABLISHED | media;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400419 if (GET_GLOBAL(FloppyInfo[stype].chs.cylinder)
420 < GET_GLOBAL(FloppyInfo[ftype].chs.cylinder))
Kevin O'Connore7c5a7e2013-03-03 13:59:58 -0500421 fms |= FMS_DOUBLE_STEPPING;
422 SET_BDA(floppy_media_state[floppyid], fms);
423
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400424 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500425}
426
Kevin O'Connor8615f622013-12-11 19:38:15 -0500427// Prepare a floppy for a data transfer.
Kevin O'Connora68aeaf2008-07-07 21:37:10 -0400428static int
Kevin O'Connor8615f622013-12-11 19:38:15 -0500429floppy_prep(struct drive_s *drive_gf, u8 cylinder)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500430{
Kevin O'Connor1902c942013-10-26 11:48:06 -0400431 u8 floppyid = GET_GLOBALFLAT(drive_gf->cntl_id);
Kevin O'Connor8615f622013-12-11 19:38:15 -0500432 if (!(GET_BDA(floppy_recalibration_status) & (1<<floppyid)) ||
433 !(GET_BDA(floppy_media_state[floppyid]) & FMS_MEDIA_DRIVE_ESTABLISHED)) {
434 // Recalibrate drive.
435 int ret = floppy_drive_recal(floppyid);
436 if (ret)
437 return ret;
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500438
Kevin O'Connor8615f622013-12-11 19:38:15 -0500439 // Sense media.
440 ret = floppy_media_sense(drive_gf);
441 if (ret)
442 return ret;
443 }
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500444
Kevin O'Connor8615f622013-12-11 19:38:15 -0500445 // Seek to cylinder if needed.
446 u8 lastcyl = GET_BDA(floppy_track[floppyid]);
447 if (cylinder != lastcyl) {
448 u8 param[2];
449 param[0] = floppyid;
450 param[1] = cylinder;
451 int ret = floppy_drive_pio(floppyid, FC_SEEK, param);
452 if (ret)
453 return ret;
454 SET_BDA(floppy_track[floppyid], cylinder);
455 }
456
457 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500458}
459
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500460
461/****************************************************************
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400462 * Floppy DMA transfer
Kevin O'Connorc8494052013-03-03 10:27:30 -0500463 ****************************************************************/
464
465// Perform a floppy transfer command (setup DMA and issue PIO).
466static int
Kevin O'Connor8615f622013-12-11 19:38:15 -0500467floppy_dma_cmd(struct disk_op_s *op, int count, int command, u8 *param)
Kevin O'Connorc8494052013-03-03 10:27:30 -0500468{
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400469 // Setup DMA controller
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500470 int isWrite = command != FC_READ;
Kevin O'Connor8615f622013-12-11 19:38:15 -0500471 int ret = dma_floppy((u32)op->buf_fl, count, isWrite);
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400472 if (ret)
Kevin O'Connorc8494052013-03-03 10:27:30 -0500473 return DISK_RET_EBOUNDARY;
474
Kevin O'Connor7b9f2972013-09-18 21:04:03 -0400475 // Invoke floppy controller
Kevin O'Connor8615f622013-12-11 19:38:15 -0500476 u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id);
477 ret = floppy_drive_pio(floppyid, command, param);
Kevin O'Connorc8494052013-03-03 10:27:30 -0500478 if (ret)
479 return ret;
480
481 // Populate floppy_return_status in BDA
482 int i;
483 for (i=0; i<7; i++)
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500484 SET_BDA(floppy_return_status[i], param[i]);
Kevin O'Connorc8494052013-03-03 10:27:30 -0500485
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500486 if (param[0] & 0xc0) {
487 if (param[1] & 0x02)
Kevin O'Connorc8494052013-03-03 10:27:30 -0500488 return DISK_RET_EWRITEPROTECT;
Kevin O'Connor8413b322013-03-09 20:07:38 -0500489 dprintf(1, "floppy error: %02x %02x %02x %02x %02x %02x %02x\n"
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500490 , param[0], param[1], param[2], param[3]
491 , param[4], param[5], param[6]);
Kevin O'Connorc8494052013-03-03 10:27:30 -0500492 return DISK_RET_ECONTROLLER;
493 }
494
Kevin O'Connorc8494052013-03-03 10:27:30 -0500495 return DISK_RET_SUCCESS;
496}
497
498
499/****************************************************************
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400500 * Floppy handlers
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500501 ****************************************************************/
502
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400503static struct chs_s
504lba2chs(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500505{
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400506 struct chs_s res = { };
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400507
Kevin O'Connor302a6e82013-12-07 11:46:37 -0500508 u32 tmp = op->lba;
Kevin O'Connor1902c942013-10-26 11:48:06 -0400509 u16 nls = GET_GLOBALFLAT(op->drive_gf->lchs.sector);
Kevin O'Connor302a6e82013-12-07 11:46:37 -0500510 res.sector = (tmp % nls) + 1;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400511
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400512 tmp /= nls;
Kevin O'Connor1902c942013-10-26 11:48:06 -0400513 u16 nlh = GET_GLOBALFLAT(op->drive_gf->lchs.head);
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400514 res.head = tmp % nlh;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400515
516 tmp /= nlh;
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400517 res.cylinder = tmp;
518
519 return res;
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400520}
521
522// diskette controller reset
523static int
524floppy_reset(struct disk_op_s *op)
525{
Kevin O'Connor5bb75522013-03-03 15:32:31 -0500526 SET_BDA(floppy_recalibration_status, 0);
527 SET_BDA(floppy_media_state[0], 0);
528 SET_BDA(floppy_media_state[1], 0);
529 SET_BDA(floppy_track[0], 0);
530 SET_BDA(floppy_track[1], 0);
531 SET_BDA(floppy_last_data_rate, 0);
532 floppy_disable_controller();
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500533 return floppy_enable_controller();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500534}
535
536// Read Diskette Sectors
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400537static int
538floppy_read(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500539{
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400540 struct chs_s chs = lba2chs(op);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500541 int ret = floppy_prep(op->drive_gf, chs.cylinder);
542 if (ret)
543 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500544
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500545 // send read-normal-data command to controller
Kevin O'Connor1902c942013-10-26 11:48:06 -0400546 u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500547 u8 param[8];
548 param[0] = (chs.head << 2) | floppyid; // HD DR1 DR2
549 param[1] = chs.cylinder;
550 param[2] = chs.head;
551 param[3] = chs.sector;
552 param[4] = FLOPPY_SIZE_CODE;
553 param[5] = chs.sector + op->count - 1; // last sector to read on track
554 param[6] = FLOPPY_GAPLEN;
555 param[7] = FLOPPY_DATALEN;
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500556 return floppy_dma_cmd(op, op->count * DISK_SECTOR_SIZE, FC_READ, param);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500557}
558
559// Write Diskette Sectors
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400560static int
561floppy_write(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500562{
Kevin O'Connor8ab9a342013-09-28 23:34:49 -0400563 struct chs_s chs = lba2chs(op);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500564 int ret = floppy_prep(op->drive_gf, chs.cylinder);
565 if (ret)
566 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500567
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500568 // send write-normal-data command to controller
Kevin O'Connor1902c942013-10-26 11:48:06 -0400569 u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500570 u8 param[8];
571 param[0] = (chs.head << 2) | floppyid; // HD DR1 DR2
572 param[1] = chs.cylinder;
573 param[2] = chs.head;
574 param[3] = chs.sector;
575 param[4] = FLOPPY_SIZE_CODE;
576 param[5] = chs.sector + op->count - 1; // last sector to write on track
577 param[6] = FLOPPY_GAPLEN;
578 param[7] = FLOPPY_DATALEN;
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500579 return floppy_dma_cmd(op, op->count * DISK_SECTOR_SIZE, FC_WRITE, param);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500580}
581
582// Verify Diskette Sectors
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400583static int
584floppy_verify(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500585{
Kevin O'Connor8615f622013-12-11 19:38:15 -0500586 struct chs_s chs = lba2chs(op);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500587 int ret = floppy_prep(op->drive_gf, chs.cylinder);
588 if (ret)
589 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500590
Kevin O'Connor8615f622013-12-11 19:38:15 -0500591 // This command isn't implemented - just return success.
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400592 return DISK_RET_SUCCESS;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500593}
594
595// format diskette track
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400596static int
597floppy_format(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500598{
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500599 struct chs_s chs = lba2chs(op);
Kevin O'Connorb7558a32013-12-17 18:15:46 -0500600 int ret = floppy_prep(op->drive_gf, chs.cylinder);
601 if (ret)
602 return ret;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500603
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500604 // send format-track command to controller
Kevin O'Connor1902c942013-10-26 11:48:06 -0400605 u8 floppyid = GET_GLOBALFLAT(op->drive_gf->cntl_id);
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500606 u8 param[7];
Kevin O'Connorb84899e2013-12-11 19:15:45 -0500607 param[0] = (chs.head << 2) | floppyid; // HD DR1 DR2
Kevin O'Connorcd891f12013-12-11 18:32:12 -0500608 param[1] = FLOPPY_SIZE_CODE;
609 param[2] = op->count; // number of sectors per track
610 param[3] = FLOPPY_FORMAT_GAPLEN;
611 param[4] = FLOPPY_FILLBYTE;
Kevin O'Connor8615f622013-12-11 19:38:15 -0500612 return floppy_dma_cmd(op, op->count * 4, FC_FORMAT, param);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500613}
614
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400615int
Kevin O'Connor17856452015-07-07 14:56:20 -0400616floppy_process_op(struct disk_op_s *op)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500617{
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400618 if (!CONFIG_FLOPPY)
619 return 0;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500620
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400621 switch (op->command) {
622 case CMD_RESET:
623 return floppy_reset(op);
624 case CMD_READ:
625 return floppy_read(op);
626 case CMD_WRITE:
627 return floppy_write(op);
628 case CMD_VERIFY:
629 return floppy_verify(op);
630 case CMD_FORMAT:
631 return floppy_format(op);
632 default:
Kevin O'Connoraf5aabb2009-08-16 18:48:38 -0400633 return DISK_RET_EPARAM;
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500634 }
635}
636
Kevin O'Connora3b612e2009-02-07 11:25:29 -0500637
638/****************************************************************
639 * HW irqs
640 ****************************************************************/
641
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500642// INT 0Eh Diskette Hardware ISR Entry Point
Kevin O'Connor19786762008-03-05 21:09:59 -0500643void VISIBLE16
Kevin O'Connor1297e5d2012-06-02 20:30:58 -0400644handle_0e(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500645{
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400646 if (! CONFIG_FLOPPY)
Kevin O'Connorff5e0052012-12-07 11:25:58 -0500647 return;
648 debug_isr(DEBUG_ISR_0e);
Kevin O'Connor40967022008-07-21 22:23:05 -0400649
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500650 // diskette interrupt has occurred
Kevin O'Connore51316d2012-06-10 09:09:22 -0400651 u8 frs = GET_BDA(floppy_recalibration_status);
Kevin O'Connor6e529bd2013-03-02 20:30:50 -0500652 SET_BDA(floppy_recalibration_status, frs | FRS_IRQ);
Kevin O'Connor40967022008-07-21 22:23:05 -0400653
Kevin O'Connoraa7c2342013-07-14 15:07:21 -0400654 pic_eoi1();
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500655}
656
657// Called from int08 handler.
658void
Kevin O'Connor1ca05b02010-01-03 17:43:37 -0500659floppy_tick(void)
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500660{
Kevin O'Connor0a0e42e2009-08-16 12:09:44 -0400661 if (! CONFIG_FLOPPY)
Kevin O'Connor40967022008-07-21 22:23:05 -0400662 return;
663
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500664 // time to turn off drive(s)?
665 u8 fcount = GET_BDA(floppy_motor_counter);
666 if (fcount) {
667 fcount--;
668 SET_BDA(floppy_motor_counter, fcount);
669 if (fcount == 0)
670 // turn motor(s) off
Kevin O'Connor402f9a42013-12-07 12:48:11 -0500671 floppy_dor_write(GET_LOW(FloppyDOR) & ~0xf0);
Kevin O'Connorf076a3e2008-02-25 22:25:15 -0500672 }
673}