live migration: Serialize vmstate saving in stage 2
[qemu/aliguori-queue.git] / hw / fdc.c
blobe875291a3aa2cfd869e3b6697bc029740a7643b8
1 /*
2 * QEMU Floppy disk emulator (Intel 82078)
4 * Copyright (c) 2003, 2007 Jocelyn Mayer
5 * Copyright (c) 2008 Hervé Poussineau
7 * Permission is hereby granted, free of charge, to any person obtaining a copy
8 * of this software and associated documentation files (the "Software"), to deal
9 * in the Software without restriction, including without limitation the rights
10 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the Software is
12 * furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 * THE SOFTWARE.
26 * The controller is used in Sun4m systems in a slightly different
27 * way. There are changes in DOR register and DMA is not available.
30 #include "hw.h"
31 #include "fdc.h"
32 #include "block.h"
33 #include "qemu-timer.h"
34 #include "isa.h"
35 #include "sysbus.h"
36 #include "qdev-addr.h"
38 /********************************************************/
39 /* debug Floppy devices */
40 //#define DEBUG_FLOPPY
42 #ifdef DEBUG_FLOPPY
43 #define FLOPPY_DPRINTF(fmt, ...) \
44 do { printf("FLOPPY: " fmt , ## __VA_ARGS__); } while (0)
45 #else
46 #define FLOPPY_DPRINTF(fmt, ...)
47 #endif
49 #define FLOPPY_ERROR(fmt, ...) \
50 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
52 /********************************************************/
53 /* Floppy drive emulation */
55 #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
56 #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))
58 /* Will always be a fixed parameter for us */
59 #define FD_SECTOR_LEN 512
60 #define FD_SECTOR_SC 2 /* Sector size code */
61 #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */
63 /* Floppy disk drive emulation */
64 typedef enum fdisk_type_t {
65 FDRIVE_DISK_288 = 0x01, /* 2.88 MB disk */
66 FDRIVE_DISK_144 = 0x02, /* 1.44 MB disk */
67 FDRIVE_DISK_720 = 0x03, /* 720 kB disk */
68 FDRIVE_DISK_USER = 0x04, /* User defined geometry */
69 FDRIVE_DISK_NONE = 0x05, /* No disk */
70 } fdisk_type_t;
72 typedef enum fdrive_type_t {
73 FDRIVE_DRV_144 = 0x00, /* 1.44 MB 3"5 drive */
74 FDRIVE_DRV_288 = 0x01, /* 2.88 MB 3"5 drive */
75 FDRIVE_DRV_120 = 0x02, /* 1.2 MB 5"25 drive */
76 FDRIVE_DRV_NONE = 0x03, /* No drive connected */
77 } fdrive_type_t;
79 typedef enum fdisk_flags_t {
80 FDISK_DBL_SIDES = 0x01,
81 } fdisk_flags_t;
83 typedef struct fdrive_t {
84 DriveInfo *dinfo;
85 BlockDriverState *bs;
86 /* Drive status */
87 fdrive_type_t drive;
88 uint8_t perpendicular; /* 2.88 MB access mode */
89 /* Position */
90 uint8_t head;
91 uint8_t track;
92 uint8_t sect;
93 /* Media */
94 fdisk_flags_t flags;
95 uint8_t last_sect; /* Nb sector per track */
96 uint8_t max_track; /* Nb of tracks */
97 uint16_t bps; /* Bytes per sector */
98 uint8_t ro; /* Is read-only */
99 } fdrive_t;
101 static void fd_init (fdrive_t *drv)
103 /* Drive */
104 drv->bs = drv->dinfo ? drv->dinfo->bdrv : NULL;
105 drv->drive = FDRIVE_DRV_NONE;
106 drv->perpendicular = 0;
107 /* Disk */
108 drv->last_sect = 0;
109 drv->max_track = 0;
112 static int _fd_sector (uint8_t head, uint8_t track,
113 uint8_t sect, uint8_t last_sect)
115 return (((track * 2) + head) * last_sect) + sect - 1;
118 /* Returns current position, in sectors, for given drive */
119 static int fd_sector (fdrive_t *drv)
121 return _fd_sector(drv->head, drv->track, drv->sect, drv->last_sect);
124 /* Seek to a new position:
125 * returns 0 if already on right track
126 * returns 1 if track changed
127 * returns 2 if track is invalid
128 * returns 3 if sector is invalid
129 * returns 4 if seek is disabled
131 static int fd_seek (fdrive_t *drv, uint8_t head, uint8_t track, uint8_t sect,
132 int enable_seek)
134 uint32_t sector;
135 int ret;
137 if (track > drv->max_track ||
138 (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) {
139 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
140 head, track, sect, 1,
141 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
142 drv->max_track, drv->last_sect);
143 return 2;
145 if (sect > drv->last_sect) {
146 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
147 head, track, sect, 1,
148 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
149 drv->max_track, drv->last_sect);
150 return 3;
152 sector = _fd_sector(head, track, sect, drv->last_sect);
153 ret = 0;
154 if (sector != fd_sector(drv)) {
155 #if 0
156 if (!enable_seek) {
157 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
158 head, track, sect, 1, drv->max_track, drv->last_sect);
159 return 4;
161 #endif
162 drv->head = head;
163 if (drv->track != track)
164 ret = 1;
165 drv->track = track;
166 drv->sect = sect;
169 return ret;
172 /* Set drive back to track 0 */
173 static void fd_recalibrate (fdrive_t *drv)
175 FLOPPY_DPRINTF("recalibrate\n");
176 drv->head = 0;
177 drv->track = 0;
178 drv->sect = 1;
181 /* Recognize floppy formats */
182 typedef struct fd_format_t {
183 fdrive_type_t drive;
184 fdisk_type_t disk;
185 uint8_t last_sect;
186 uint8_t max_track;
187 uint8_t max_head;
188 const char *str;
189 } fd_format_t;
191 static const fd_format_t fd_formats[] = {
192 /* First entry is default format */
193 /* 1.44 MB 3"1/2 floppy disks */
194 { FDRIVE_DRV_144, FDRIVE_DISK_144, 18, 80, 1, "1.44 MB 3\"1/2", },
195 { FDRIVE_DRV_144, FDRIVE_DISK_144, 20, 80, 1, "1.6 MB 3\"1/2", },
196 { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 80, 1, "1.68 MB 3\"1/2", },
197 { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 82, 1, "1.72 MB 3\"1/2", },
198 { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 83, 1, "1.74 MB 3\"1/2", },
199 { FDRIVE_DRV_144, FDRIVE_DISK_144, 22, 80, 1, "1.76 MB 3\"1/2", },
200 { FDRIVE_DRV_144, FDRIVE_DISK_144, 23, 80, 1, "1.84 MB 3\"1/2", },
201 { FDRIVE_DRV_144, FDRIVE_DISK_144, 24, 80, 1, "1.92 MB 3\"1/2", },
202 /* 2.88 MB 3"1/2 floppy disks */
203 { FDRIVE_DRV_288, FDRIVE_DISK_288, 36, 80, 1, "2.88 MB 3\"1/2", },
204 { FDRIVE_DRV_288, FDRIVE_DISK_288, 39, 80, 1, "3.12 MB 3\"1/2", },
205 { FDRIVE_DRV_288, FDRIVE_DISK_288, 40, 80, 1, "3.2 MB 3\"1/2", },
206 { FDRIVE_DRV_288, FDRIVE_DISK_288, 44, 80, 1, "3.52 MB 3\"1/2", },
207 { FDRIVE_DRV_288, FDRIVE_DISK_288, 48, 80, 1, "3.84 MB 3\"1/2", },
208 /* 720 kB 3"1/2 floppy disks */
209 { FDRIVE_DRV_144, FDRIVE_DISK_720, 9, 80, 1, "720 kB 3\"1/2", },
210 { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 80, 1, "800 kB 3\"1/2", },
211 { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 82, 1, "820 kB 3\"1/2", },
212 { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 83, 1, "830 kB 3\"1/2", },
213 { FDRIVE_DRV_144, FDRIVE_DISK_720, 13, 80, 1, "1.04 MB 3\"1/2", },
214 { FDRIVE_DRV_144, FDRIVE_DISK_720, 14, 80, 1, "1.12 MB 3\"1/2", },
215 /* 1.2 MB 5"1/4 floppy disks */
216 { FDRIVE_DRV_120, FDRIVE_DISK_288, 15, 80, 1, "1.2 kB 5\"1/4", },
217 { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 80, 1, "1.44 MB 5\"1/4", },
218 { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 82, 1, "1.48 MB 5\"1/4", },
219 { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 83, 1, "1.49 MB 5\"1/4", },
220 { FDRIVE_DRV_120, FDRIVE_DISK_288, 20, 80, 1, "1.6 MB 5\"1/4", },
221 /* 720 kB 5"1/4 floppy disks */
222 { FDRIVE_DRV_120, FDRIVE_DISK_288, 9, 80, 1, "720 kB 5\"1/4", },
223 { FDRIVE_DRV_120, FDRIVE_DISK_288, 11, 80, 1, "880 kB 5\"1/4", },
224 /* 360 kB 5"1/4 floppy disks */
225 { FDRIVE_DRV_120, FDRIVE_DISK_288, 9, 40, 1, "360 kB 5\"1/4", },
226 { FDRIVE_DRV_120, FDRIVE_DISK_288, 9, 40, 0, "180 kB 5\"1/4", },
227 { FDRIVE_DRV_120, FDRIVE_DISK_288, 10, 41, 1, "410 kB 5\"1/4", },
228 { FDRIVE_DRV_120, FDRIVE_DISK_288, 10, 42, 1, "420 kB 5\"1/4", },
229 /* 320 kB 5"1/4 floppy disks */
230 { FDRIVE_DRV_120, FDRIVE_DISK_288, 8, 40, 1, "320 kB 5\"1/4", },
231 { FDRIVE_DRV_120, FDRIVE_DISK_288, 8, 40, 0, "160 kB 5\"1/4", },
232 /* 360 kB must match 5"1/4 better than 3"1/2... */
233 { FDRIVE_DRV_144, FDRIVE_DISK_720, 9, 80, 0, "360 kB 3\"1/2", },
234 /* end */
235 { FDRIVE_DRV_NONE, FDRIVE_DISK_NONE, -1, -1, 0, NULL, },
238 /* Revalidate a disk drive after a disk change */
239 static void fd_revalidate (fdrive_t *drv)
241 const fd_format_t *parse;
242 uint64_t nb_sectors, size;
243 int i, first_match, match;
244 int nb_heads, max_track, last_sect, ro;
246 FLOPPY_DPRINTF("revalidate\n");
247 if (drv->bs != NULL && bdrv_is_inserted(drv->bs)) {
248 ro = bdrv_is_read_only(drv->bs);
249 bdrv_get_geometry_hint(drv->bs, &nb_heads, &max_track, &last_sect);
250 if (nb_heads != 0 && max_track != 0 && last_sect != 0) {
251 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
252 nb_heads - 1, max_track, last_sect);
253 } else {
254 bdrv_get_geometry(drv->bs, &nb_sectors);
255 match = -1;
256 first_match = -1;
257 for (i = 0;; i++) {
258 parse = &fd_formats[i];
259 if (parse->drive == FDRIVE_DRV_NONE)
260 break;
261 if (drv->drive == parse->drive ||
262 drv->drive == FDRIVE_DRV_NONE) {
263 size = (parse->max_head + 1) * parse->max_track *
264 parse->last_sect;
265 if (nb_sectors == size) {
266 match = i;
267 break;
269 if (first_match == -1)
270 first_match = i;
273 if (match == -1) {
274 if (first_match == -1)
275 match = 1;
276 else
277 match = first_match;
278 parse = &fd_formats[match];
280 nb_heads = parse->max_head + 1;
281 max_track = parse->max_track;
282 last_sect = parse->last_sect;
283 drv->drive = parse->drive;
284 FLOPPY_DPRINTF("%s floppy disk (%d h %d t %d s) %s\n", parse->str,
285 nb_heads, max_track, last_sect, ro ? "ro" : "rw");
287 if (nb_heads == 1) {
288 drv->flags &= ~FDISK_DBL_SIDES;
289 } else {
290 drv->flags |= FDISK_DBL_SIDES;
292 drv->max_track = max_track;
293 drv->last_sect = last_sect;
294 drv->ro = ro;
295 } else {
296 FLOPPY_DPRINTF("No disk in drive\n");
297 drv->last_sect = 0;
298 drv->max_track = 0;
299 drv->flags &= ~FDISK_DBL_SIDES;
303 /********************************************************/
304 /* Intel 82078 floppy disk controller emulation */
306 static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq);
307 static void fdctrl_reset_fifo (fdctrl_t *fdctrl);
308 static int fdctrl_transfer_handler (void *opaque, int nchan,
309 int dma_pos, int dma_len);
310 static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status0);
312 static uint32_t fdctrl_read_statusA (fdctrl_t *fdctrl);
313 static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl);
314 static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl);
315 static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value);
316 static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl);
317 static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value);
318 static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl);
319 static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value);
320 static uint32_t fdctrl_read_data (fdctrl_t *fdctrl);
321 static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value);
322 static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl);
324 enum {
325 FD_DIR_WRITE = 0,
326 FD_DIR_READ = 1,
327 FD_DIR_SCANE = 2,
328 FD_DIR_SCANL = 3,
329 FD_DIR_SCANH = 4,
332 enum {
333 FD_STATE_MULTI = 0x01, /* multi track flag */
334 FD_STATE_FORMAT = 0x02, /* format flag */
335 FD_STATE_SEEK = 0x04, /* seek flag */
338 enum {
339 FD_REG_SRA = 0x00,
340 FD_REG_SRB = 0x01,
341 FD_REG_DOR = 0x02,
342 FD_REG_TDR = 0x03,
343 FD_REG_MSR = 0x04,
344 FD_REG_DSR = 0x04,
345 FD_REG_FIFO = 0x05,
346 FD_REG_DIR = 0x07,
349 enum {
350 FD_CMD_READ_TRACK = 0x02,
351 FD_CMD_SPECIFY = 0x03,
352 FD_CMD_SENSE_DRIVE_STATUS = 0x04,
353 FD_CMD_WRITE = 0x05,
354 FD_CMD_READ = 0x06,
355 FD_CMD_RECALIBRATE = 0x07,
356 FD_CMD_SENSE_INTERRUPT_STATUS = 0x08,
357 FD_CMD_WRITE_DELETED = 0x09,
358 FD_CMD_READ_ID = 0x0a,
359 FD_CMD_READ_DELETED = 0x0c,
360 FD_CMD_FORMAT_TRACK = 0x0d,
361 FD_CMD_DUMPREG = 0x0e,
362 FD_CMD_SEEK = 0x0f,
363 FD_CMD_VERSION = 0x10,
364 FD_CMD_SCAN_EQUAL = 0x11,
365 FD_CMD_PERPENDICULAR_MODE = 0x12,
366 FD_CMD_CONFIGURE = 0x13,
367 FD_CMD_LOCK = 0x14,
368 FD_CMD_VERIFY = 0x16,
369 FD_CMD_POWERDOWN_MODE = 0x17,
370 FD_CMD_PART_ID = 0x18,
371 FD_CMD_SCAN_LOW_OR_EQUAL = 0x19,
372 FD_CMD_SCAN_HIGH_OR_EQUAL = 0x1d,
373 FD_CMD_SAVE = 0x2c,
374 FD_CMD_OPTION = 0x33,
375 FD_CMD_RESTORE = 0x4c,
376 FD_CMD_DRIVE_SPECIFICATION_COMMAND = 0x8e,
377 FD_CMD_RELATIVE_SEEK_OUT = 0x8f,
378 FD_CMD_FORMAT_AND_WRITE = 0xcd,
379 FD_CMD_RELATIVE_SEEK_IN = 0xcf,
382 enum {
383 FD_CONFIG_PRETRK = 0xff, /* Pre-compensation set to track 0 */
384 FD_CONFIG_FIFOTHR = 0x0f, /* FIFO threshold set to 1 byte */
385 FD_CONFIG_POLL = 0x10, /* Poll enabled */
386 FD_CONFIG_EFIFO = 0x20, /* FIFO disabled */
387 FD_CONFIG_EIS = 0x40, /* No implied seeks */
390 enum {
391 FD_SR0_EQPMT = 0x10,
392 FD_SR0_SEEK = 0x20,
393 FD_SR0_ABNTERM = 0x40,
394 FD_SR0_INVCMD = 0x80,
395 FD_SR0_RDYCHG = 0xc0,
398 enum {
399 FD_SR1_EC = 0x80, /* End of cylinder */
402 enum {
403 FD_SR2_SNS = 0x04, /* Scan not satisfied */
404 FD_SR2_SEH = 0x08, /* Scan equal hit */
407 enum {
408 FD_SRA_DIR = 0x01,
409 FD_SRA_nWP = 0x02,
410 FD_SRA_nINDX = 0x04,
411 FD_SRA_HDSEL = 0x08,
412 FD_SRA_nTRK0 = 0x10,
413 FD_SRA_STEP = 0x20,
414 FD_SRA_nDRV2 = 0x40,
415 FD_SRA_INTPEND = 0x80,
418 enum {
419 FD_SRB_MTR0 = 0x01,
420 FD_SRB_MTR1 = 0x02,
421 FD_SRB_WGATE = 0x04,
422 FD_SRB_RDATA = 0x08,
423 FD_SRB_WDATA = 0x10,
424 FD_SRB_DR0 = 0x20,
427 enum {
428 #if MAX_FD == 4
429 FD_DOR_SELMASK = 0x03,
430 #else
431 FD_DOR_SELMASK = 0x01,
432 #endif
433 FD_DOR_nRESET = 0x04,
434 FD_DOR_DMAEN = 0x08,
435 FD_DOR_MOTEN0 = 0x10,
436 FD_DOR_MOTEN1 = 0x20,
437 FD_DOR_MOTEN2 = 0x40,
438 FD_DOR_MOTEN3 = 0x80,
441 enum {
442 #if MAX_FD == 4
443 FD_TDR_BOOTSEL = 0x0c,
444 #else
445 FD_TDR_BOOTSEL = 0x04,
446 #endif
449 enum {
450 FD_DSR_DRATEMASK= 0x03,
451 FD_DSR_PWRDOWN = 0x40,
452 FD_DSR_SWRESET = 0x80,
455 enum {
456 FD_MSR_DRV0BUSY = 0x01,
457 FD_MSR_DRV1BUSY = 0x02,
458 FD_MSR_DRV2BUSY = 0x04,
459 FD_MSR_DRV3BUSY = 0x08,
460 FD_MSR_CMDBUSY = 0x10,
461 FD_MSR_NONDMA = 0x20,
462 FD_MSR_DIO = 0x40,
463 FD_MSR_RQM = 0x80,
466 enum {
467 FD_DIR_DSKCHG = 0x80,
470 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
471 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
472 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
474 struct fdctrl_t {
475 /* Controller's identification */
476 uint8_t version;
477 /* HW */
478 qemu_irq irq;
479 int dma_chann;
480 /* Controller state */
481 QEMUTimer *result_timer;
482 uint8_t sra;
483 uint8_t srb;
484 uint8_t dor;
485 uint8_t dor_vmstate; /* only used as temp during vmstate */
486 uint8_t tdr;
487 uint8_t dsr;
488 uint8_t msr;
489 uint8_t cur_drv;
490 uint8_t status0;
491 uint8_t status1;
492 uint8_t status2;
493 /* Command FIFO */
494 uint8_t *fifo;
495 int32_t fifo_size;
496 uint32_t data_pos;
497 uint32_t data_len;
498 uint8_t data_state;
499 uint8_t data_dir;
500 uint8_t eot; /* last wanted sector */
501 /* States kept only to be returned back */
502 /* Timers state */
503 uint8_t timer0;
504 uint8_t timer1;
505 /* precompensation */
506 uint8_t precomp_trk;
507 uint8_t config;
508 uint8_t lock;
509 /* Power down config (also with status regB access mode */
510 uint8_t pwrd;
511 /* Sun4m quirks? */
512 int sun4m;
513 /* Floppy drives */
514 uint8_t num_floppies;
515 fdrive_t drives[MAX_FD];
516 int reset_sensei;
519 typedef struct fdctrl_sysbus_t {
520 SysBusDevice busdev;
521 struct fdctrl_t state;
522 } fdctrl_sysbus_t;
524 typedef struct fdctrl_isabus_t {
525 ISADevice busdev;
526 struct fdctrl_t state;
527 } fdctrl_isabus_t;
529 static uint32_t fdctrl_read (void *opaque, uint32_t reg)
531 fdctrl_t *fdctrl = opaque;
532 uint32_t retval;
534 switch (reg) {
535 case FD_REG_SRA:
536 retval = fdctrl_read_statusA(fdctrl);
537 break;
538 case FD_REG_SRB:
539 retval = fdctrl_read_statusB(fdctrl);
540 break;
541 case FD_REG_DOR:
542 retval = fdctrl_read_dor(fdctrl);
543 break;
544 case FD_REG_TDR:
545 retval = fdctrl_read_tape(fdctrl);
546 break;
547 case FD_REG_MSR:
548 retval = fdctrl_read_main_status(fdctrl);
549 break;
550 case FD_REG_FIFO:
551 retval = fdctrl_read_data(fdctrl);
552 break;
553 case FD_REG_DIR:
554 retval = fdctrl_read_dir(fdctrl);
555 break;
556 default:
557 retval = (uint32_t)(-1);
558 break;
560 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval);
562 return retval;
565 static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value)
567 fdctrl_t *fdctrl = opaque;
569 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg & 7, value);
571 switch (reg) {
572 case FD_REG_DOR:
573 fdctrl_write_dor(fdctrl, value);
574 break;
575 case FD_REG_TDR:
576 fdctrl_write_tape(fdctrl, value);
577 break;
578 case FD_REG_DSR:
579 fdctrl_write_rate(fdctrl, value);
580 break;
581 case FD_REG_FIFO:
582 fdctrl_write_data(fdctrl, value);
583 break;
584 default:
585 break;
589 static uint32_t fdctrl_read_port (void *opaque, uint32_t reg)
591 return fdctrl_read(opaque, reg & 7);
594 static void fdctrl_write_port (void *opaque, uint32_t reg, uint32_t value)
596 fdctrl_write(opaque, reg & 7, value);
599 static uint32_t fdctrl_read_mem (void *opaque, target_phys_addr_t reg)
601 return fdctrl_read(opaque, (uint32_t)reg);
604 static void fdctrl_write_mem (void *opaque,
605 target_phys_addr_t reg, uint32_t value)
607 fdctrl_write(opaque, (uint32_t)reg, value);
610 static CPUReadMemoryFunc * const fdctrl_mem_read[3] = {
611 fdctrl_read_mem,
612 fdctrl_read_mem,
613 fdctrl_read_mem,
616 static CPUWriteMemoryFunc * const fdctrl_mem_write[3] = {
617 fdctrl_write_mem,
618 fdctrl_write_mem,
619 fdctrl_write_mem,
622 static CPUReadMemoryFunc * const fdctrl_mem_read_strict[3] = {
623 fdctrl_read_mem,
624 NULL,
625 NULL,
628 static CPUWriteMemoryFunc * const fdctrl_mem_write_strict[3] = {
629 fdctrl_write_mem,
630 NULL,
631 NULL,
634 static const VMStateDescription vmstate_fdrive = {
635 .name = "fdrive",
636 .version_id = 1,
637 .minimum_version_id = 1,
638 .minimum_version_id_old = 1,
639 .fields = (VMStateField []) {
640 VMSTATE_UINT8(head, fdrive_t),
641 VMSTATE_UINT8(track, fdrive_t),
642 VMSTATE_UINT8(sect, fdrive_t),
643 VMSTATE_END_OF_LIST()
647 static void fdc_pre_save(void *opaque)
649 fdctrl_t *s = opaque;
651 s->dor_vmstate = s->dor | GET_CUR_DRV(s);
654 static int fdc_post_load(void *opaque, int version_id)
656 fdctrl_t *s = opaque;
658 SET_CUR_DRV(s, s->dor_vmstate & FD_DOR_SELMASK);
659 s->dor = s->dor_vmstate & ~FD_DOR_SELMASK;
660 return 0;
663 static const VMStateDescription vmstate_fdc = {
664 .name = "fdctrl",
665 .version_id = 2,
666 .minimum_version_id = 2,
667 .minimum_version_id_old = 2,
668 .pre_save = fdc_pre_save,
669 .post_load = fdc_post_load,
670 .fields = (VMStateField []) {
671 /* Controller State */
672 VMSTATE_UINT8(sra, fdctrl_t),
673 VMSTATE_UINT8(srb, fdctrl_t),
674 VMSTATE_UINT8(dor_vmstate, fdctrl_t),
675 VMSTATE_UINT8(tdr, fdctrl_t),
676 VMSTATE_UINT8(dsr, fdctrl_t),
677 VMSTATE_UINT8(msr, fdctrl_t),
678 VMSTATE_UINT8(status0, fdctrl_t),
679 VMSTATE_UINT8(status1, fdctrl_t),
680 VMSTATE_UINT8(status2, fdctrl_t),
681 /* Command FIFO */
682 VMSTATE_VARRAY_INT32(fifo, fdctrl_t, fifo_size, 0, vmstate_info_uint8, uint8),
683 VMSTATE_UINT32(data_pos, fdctrl_t),
684 VMSTATE_UINT32(data_len, fdctrl_t),
685 VMSTATE_UINT8(data_state, fdctrl_t),
686 VMSTATE_UINT8(data_dir, fdctrl_t),
687 VMSTATE_UINT8(eot, fdctrl_t),
688 /* States kept only to be returned back */
689 VMSTATE_UINT8(timer0, fdctrl_t),
690 VMSTATE_UINT8(timer1, fdctrl_t),
691 VMSTATE_UINT8(precomp_trk, fdctrl_t),
692 VMSTATE_UINT8(config, fdctrl_t),
693 VMSTATE_UINT8(lock, fdctrl_t),
694 VMSTATE_UINT8(pwrd, fdctrl_t),
695 VMSTATE_UINT8_EQUAL(num_floppies, fdctrl_t),
696 VMSTATE_STRUCT_ARRAY(drives, fdctrl_t, MAX_FD, 1,
697 vmstate_fdrive, fdrive_t),
698 VMSTATE_END_OF_LIST()
702 static const VMStateDescription vmstate_fdc_isa = {
703 .name = "fdc",
704 .version_id = 2,
705 .minimum_version_id = 2,
706 .minimum_version_id_old = 2,
707 .fields = (VMStateField []) {
708 /* Controller State */
709 VMSTATE_STRUCT(state, fdctrl_isabus_t, 0, vmstate_fdc, fdctrl_t),
710 VMSTATE_END_OF_LIST()
714 static const VMStateDescription vmstate_fdc_sysbus = {
715 .name = "fdc",
716 .version_id = 2,
717 .minimum_version_id = 2,
718 .minimum_version_id_old = 2,
719 .fields = (VMStateField []) {
720 /* Controller State */
721 VMSTATE_STRUCT(state, fdctrl_sysbus_t, 0, vmstate_fdc, fdctrl_t),
722 VMSTATE_END_OF_LIST()
727 static void fdctrl_external_reset_sysbus(DeviceState *d)
729 fdctrl_sysbus_t *sys = container_of(d, fdctrl_sysbus_t, busdev.qdev);
730 fdctrl_t *s = &sys->state;
732 fdctrl_reset(s, 0);
735 static void fdctrl_external_reset_isa(DeviceState *d)
737 fdctrl_isabus_t *isa = container_of(d, fdctrl_isabus_t, busdev.qdev);
738 fdctrl_t *s = &isa->state;
740 fdctrl_reset(s, 0);
743 static void fdctrl_handle_tc(void *opaque, int irq, int level)
745 //fdctrl_t *s = opaque;
747 if (level) {
748 // XXX
749 FLOPPY_DPRINTF("TC pulsed\n");
753 /* XXX: may change if moved to bdrv */
754 int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num)
756 return fdctrl->drives[drive_num].drive;
759 /* Change IRQ state */
760 static void fdctrl_reset_irq (fdctrl_t *fdctrl)
762 if (!(fdctrl->sra & FD_SRA_INTPEND))
763 return;
764 FLOPPY_DPRINTF("Reset interrupt\n");
765 qemu_set_irq(fdctrl->irq, 0);
766 fdctrl->sra &= ~FD_SRA_INTPEND;
769 static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status0)
771 /* Sparc mutation */
772 if (fdctrl->sun4m && (fdctrl->msr & FD_MSR_CMDBUSY)) {
773 /* XXX: not sure */
774 fdctrl->msr &= ~FD_MSR_CMDBUSY;
775 fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO;
776 fdctrl->status0 = status0;
777 return;
779 if (!(fdctrl->sra & FD_SRA_INTPEND)) {
780 qemu_set_irq(fdctrl->irq, 1);
781 fdctrl->sra |= FD_SRA_INTPEND;
783 fdctrl->reset_sensei = 0;
784 fdctrl->status0 = status0;
785 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl->status0);
788 /* Reset controller */
789 static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq)
791 int i;
793 FLOPPY_DPRINTF("reset controller\n");
794 fdctrl_reset_irq(fdctrl);
795 /* Initialise controller */
796 fdctrl->sra = 0;
797 fdctrl->srb = 0xc0;
798 if (!fdctrl->drives[1].bs)
799 fdctrl->sra |= FD_SRA_nDRV2;
800 fdctrl->cur_drv = 0;
801 fdctrl->dor = FD_DOR_nRESET;
802 fdctrl->dor |= (fdctrl->dma_chann != -1) ? FD_DOR_DMAEN : 0;
803 fdctrl->msr = FD_MSR_RQM;
804 /* FIFO state */
805 fdctrl->data_pos = 0;
806 fdctrl->data_len = 0;
807 fdctrl->data_state = 0;
808 fdctrl->data_dir = FD_DIR_WRITE;
809 for (i = 0; i < MAX_FD; i++)
810 fd_recalibrate(&fdctrl->drives[i]);
811 fdctrl_reset_fifo(fdctrl);
812 if (do_irq) {
813 fdctrl_raise_irq(fdctrl, FD_SR0_RDYCHG);
814 fdctrl->reset_sensei = FD_RESET_SENSEI_COUNT;
818 static inline fdrive_t *drv0 (fdctrl_t *fdctrl)
820 return &fdctrl->drives[(fdctrl->tdr & FD_TDR_BOOTSEL) >> 2];
823 static inline fdrive_t *drv1 (fdctrl_t *fdctrl)
825 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (1 << 2))
826 return &fdctrl->drives[1];
827 else
828 return &fdctrl->drives[0];
831 #if MAX_FD == 4
832 static inline fdrive_t *drv2 (fdctrl_t *fdctrl)
834 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (2 << 2))
835 return &fdctrl->drives[2];
836 else
837 return &fdctrl->drives[1];
840 static inline fdrive_t *drv3 (fdctrl_t *fdctrl)
842 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (3 << 2))
843 return &fdctrl->drives[3];
844 else
845 return &fdctrl->drives[2];
847 #endif
849 static fdrive_t *get_cur_drv (fdctrl_t *fdctrl)
851 switch (fdctrl->cur_drv) {
852 case 0: return drv0(fdctrl);
853 case 1: return drv1(fdctrl);
854 #if MAX_FD == 4
855 case 2: return drv2(fdctrl);
856 case 3: return drv3(fdctrl);
857 #endif
858 default: return NULL;
862 /* Status A register : 0x00 (read-only) */
863 static uint32_t fdctrl_read_statusA (fdctrl_t *fdctrl)
865 uint32_t retval = fdctrl->sra;
867 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval);
869 return retval;
872 /* Status B register : 0x01 (read-only) */
873 static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl)
875 uint32_t retval = fdctrl->srb;
877 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval);
879 return retval;
882 /* Digital output register : 0x02 */
883 static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl)
885 uint32_t retval = fdctrl->dor;
887 /* Selected drive */
888 retval |= fdctrl->cur_drv;
889 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
891 return retval;
894 static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value)
896 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
898 /* Motors */
899 if (value & FD_DOR_MOTEN0)
900 fdctrl->srb |= FD_SRB_MTR0;
901 else
902 fdctrl->srb &= ~FD_SRB_MTR0;
903 if (value & FD_DOR_MOTEN1)
904 fdctrl->srb |= FD_SRB_MTR1;
905 else
906 fdctrl->srb &= ~FD_SRB_MTR1;
908 /* Drive */
909 if (value & 1)
910 fdctrl->srb |= FD_SRB_DR0;
911 else
912 fdctrl->srb &= ~FD_SRB_DR0;
914 /* Reset */
915 if (!(value & FD_DOR_nRESET)) {
916 if (fdctrl->dor & FD_DOR_nRESET) {
917 FLOPPY_DPRINTF("controller enter RESET state\n");
919 } else {
920 if (!(fdctrl->dor & FD_DOR_nRESET)) {
921 FLOPPY_DPRINTF("controller out of RESET state\n");
922 fdctrl_reset(fdctrl, 1);
923 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
926 /* Selected drive */
927 fdctrl->cur_drv = value & FD_DOR_SELMASK;
929 fdctrl->dor = value;
932 /* Tape drive register : 0x03 */
933 static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl)
935 uint32_t retval = fdctrl->tdr;
937 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
939 return retval;
942 static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value)
944 /* Reset mode */
945 if (!(fdctrl->dor & FD_DOR_nRESET)) {
946 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
947 return;
949 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
950 /* Disk boot selection indicator */
951 fdctrl->tdr = value & FD_TDR_BOOTSEL;
952 /* Tape indicators: never allow */
955 /* Main status register : 0x04 (read) */
956 static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl)
958 uint32_t retval = fdctrl->msr;
960 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
961 fdctrl->dor |= FD_DOR_nRESET;
963 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
965 return retval;
968 /* Data select rate register : 0x04 (write) */
969 static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value)
971 /* Reset mode */
972 if (!(fdctrl->dor & FD_DOR_nRESET)) {
973 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
974 return;
976 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
977 /* Reset: autoclear */
978 if (value & FD_DSR_SWRESET) {
979 fdctrl->dor &= ~FD_DOR_nRESET;
980 fdctrl_reset(fdctrl, 1);
981 fdctrl->dor |= FD_DOR_nRESET;
983 if (value & FD_DSR_PWRDOWN) {
984 fdctrl_reset(fdctrl, 1);
986 fdctrl->dsr = value;
989 static int fdctrl_media_changed(fdrive_t *drv)
991 int ret;
993 if (!drv->bs)
994 return 0;
995 ret = bdrv_media_changed(drv->bs);
996 if (ret) {
997 fd_revalidate(drv);
999 return ret;
1002 /* Digital input register : 0x07 (read-only) */
1003 static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl)
1005 uint32_t retval = 0;
1007 if (fdctrl_media_changed(drv0(fdctrl))
1008 || fdctrl_media_changed(drv1(fdctrl))
1009 #if MAX_FD == 4
1010 || fdctrl_media_changed(drv2(fdctrl))
1011 || fdctrl_media_changed(drv3(fdctrl))
1012 #endif
1014 retval |= FD_DIR_DSKCHG;
1015 if (retval != 0)
1016 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
1018 return retval;
1021 /* FIFO state control */
1022 static void fdctrl_reset_fifo (fdctrl_t *fdctrl)
1024 fdctrl->data_dir = FD_DIR_WRITE;
1025 fdctrl->data_pos = 0;
1026 fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO);
1029 /* Set FIFO status for the host to read */
1030 static void fdctrl_set_fifo (fdctrl_t *fdctrl, int fifo_len, int do_irq)
1032 fdctrl->data_dir = FD_DIR_READ;
1033 fdctrl->data_len = fifo_len;
1034 fdctrl->data_pos = 0;
1035 fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO;
1036 if (do_irq)
1037 fdctrl_raise_irq(fdctrl, 0x00);
1040 /* Set an error: unimplemented/unknown command */
1041 static void fdctrl_unimplemented (fdctrl_t *fdctrl, int direction)
1043 FLOPPY_ERROR("unimplemented command 0x%02x\n", fdctrl->fifo[0]);
1044 fdctrl->fifo[0] = FD_SR0_INVCMD;
1045 fdctrl_set_fifo(fdctrl, 1, 0);
1048 /* Seek to next sector */
1049 static int fdctrl_seek_to_next_sect (fdctrl_t *fdctrl, fdrive_t *cur_drv)
1051 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
1052 cur_drv->head, cur_drv->track, cur_drv->sect,
1053 fd_sector(cur_drv));
1054 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1055 error in fact */
1056 if (cur_drv->sect >= cur_drv->last_sect ||
1057 cur_drv->sect == fdctrl->eot) {
1058 cur_drv->sect = 1;
1059 if (FD_MULTI_TRACK(fdctrl->data_state)) {
1060 if (cur_drv->head == 0 &&
1061 (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
1062 cur_drv->head = 1;
1063 } else {
1064 cur_drv->head = 0;
1065 cur_drv->track++;
1066 if ((cur_drv->flags & FDISK_DBL_SIDES) == 0)
1067 return 0;
1069 } else {
1070 cur_drv->track++;
1071 return 0;
1073 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1074 cur_drv->head, cur_drv->track,
1075 cur_drv->sect, fd_sector(cur_drv));
1076 } else {
1077 cur_drv->sect++;
1079 return 1;
1082 /* Callback for transfer end (stop or abort) */
1083 static void fdctrl_stop_transfer (fdctrl_t *fdctrl, uint8_t status0,
1084 uint8_t status1, uint8_t status2)
1086 fdrive_t *cur_drv;
1088 cur_drv = get_cur_drv(fdctrl);
1089 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1090 status0, status1, status2,
1091 status0 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl));
1092 fdctrl->fifo[0] = status0 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
1093 fdctrl->fifo[1] = status1;
1094 fdctrl->fifo[2] = status2;
1095 fdctrl->fifo[3] = cur_drv->track;
1096 fdctrl->fifo[4] = cur_drv->head;
1097 fdctrl->fifo[5] = cur_drv->sect;
1098 fdctrl->fifo[6] = FD_SECTOR_SC;
1099 fdctrl->data_dir = FD_DIR_READ;
1100 if (!(fdctrl->msr & FD_MSR_NONDMA)) {
1101 DMA_release_DREQ(fdctrl->dma_chann);
1103 fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO;
1104 fdctrl->msr &= ~FD_MSR_NONDMA;
1105 fdctrl_set_fifo(fdctrl, 7, 1);
1108 /* Prepare a data transfer (either DMA or FIFO) */
1109 static void fdctrl_start_transfer (fdctrl_t *fdctrl, int direction)
1111 fdrive_t *cur_drv;
1112 uint8_t kh, kt, ks;
1113 int did_seek = 0;
1115 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1116 cur_drv = get_cur_drv(fdctrl);
1117 kt = fdctrl->fifo[2];
1118 kh = fdctrl->fifo[3];
1119 ks = fdctrl->fifo[4];
1120 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1121 GET_CUR_DRV(fdctrl), kh, kt, ks,
1122 _fd_sector(kh, kt, ks, cur_drv->last_sect));
1123 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
1124 case 2:
1125 /* sect too big */
1126 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1127 fdctrl->fifo[3] = kt;
1128 fdctrl->fifo[4] = kh;
1129 fdctrl->fifo[5] = ks;
1130 return;
1131 case 3:
1132 /* track too big */
1133 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
1134 fdctrl->fifo[3] = kt;
1135 fdctrl->fifo[4] = kh;
1136 fdctrl->fifo[5] = ks;
1137 return;
1138 case 4:
1139 /* No seek enabled */
1140 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1141 fdctrl->fifo[3] = kt;
1142 fdctrl->fifo[4] = kh;
1143 fdctrl->fifo[5] = ks;
1144 return;
1145 case 1:
1146 did_seek = 1;
1147 break;
1148 default:
1149 break;
1152 /* Set the FIFO state */
1153 fdctrl->data_dir = direction;
1154 fdctrl->data_pos = 0;
1155 fdctrl->msr |= FD_MSR_CMDBUSY;
1156 if (fdctrl->fifo[0] & 0x80)
1157 fdctrl->data_state |= FD_STATE_MULTI;
1158 else
1159 fdctrl->data_state &= ~FD_STATE_MULTI;
1160 if (did_seek)
1161 fdctrl->data_state |= FD_STATE_SEEK;
1162 else
1163 fdctrl->data_state &= ~FD_STATE_SEEK;
1164 if (fdctrl->fifo[5] == 00) {
1165 fdctrl->data_len = fdctrl->fifo[8];
1166 } else {
1167 int tmp;
1168 fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]);
1169 tmp = (fdctrl->fifo[6] - ks + 1);
1170 if (fdctrl->fifo[0] & 0x80)
1171 tmp += fdctrl->fifo[6];
1172 fdctrl->data_len *= tmp;
1174 fdctrl->eot = fdctrl->fifo[6];
1175 if (fdctrl->dor & FD_DOR_DMAEN) {
1176 int dma_mode;
1177 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1178 dma_mode = DMA_get_channel_mode(fdctrl->dma_chann);
1179 dma_mode = (dma_mode >> 2) & 3;
1180 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1181 dma_mode, direction,
1182 (128 << fdctrl->fifo[5]) *
1183 (cur_drv->last_sect - ks + 1), fdctrl->data_len);
1184 if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
1185 direction == FD_DIR_SCANH) && dma_mode == 0) ||
1186 (direction == FD_DIR_WRITE && dma_mode == 2) ||
1187 (direction == FD_DIR_READ && dma_mode == 1)) {
1188 /* No access is allowed until DMA transfer has completed */
1189 fdctrl->msr &= ~FD_MSR_RQM;
1190 /* Now, we just have to wait for the DMA controller to
1191 * recall us...
1193 DMA_hold_DREQ(fdctrl->dma_chann);
1194 DMA_schedule(fdctrl->dma_chann);
1195 return;
1196 } else {
1197 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode, direction);
1200 FLOPPY_DPRINTF("start non-DMA transfer\n");
1201 fdctrl->msr |= FD_MSR_NONDMA;
1202 if (direction != FD_DIR_WRITE)
1203 fdctrl->msr |= FD_MSR_DIO;
1204 /* IO based transfer: calculate len */
1205 fdctrl_raise_irq(fdctrl, 0x00);
1207 return;
1210 /* Prepare a transfer of deleted data */
1211 static void fdctrl_start_transfer_del (fdctrl_t *fdctrl, int direction)
1213 FLOPPY_ERROR("fdctrl_start_transfer_del() unimplemented\n");
1215 /* We don't handle deleted data,
1216 * so we don't return *ANYTHING*
1218 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1221 /* handlers for DMA transfers */
1222 static int fdctrl_transfer_handler (void *opaque, int nchan,
1223 int dma_pos, int dma_len)
1225 fdctrl_t *fdctrl;
1226 fdrive_t *cur_drv;
1227 int len, start_pos, rel_pos;
1228 uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1230 fdctrl = opaque;
1231 if (fdctrl->msr & FD_MSR_RQM) {
1232 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1233 return 0;
1235 cur_drv = get_cur_drv(fdctrl);
1236 if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
1237 fdctrl->data_dir == FD_DIR_SCANH)
1238 status2 = FD_SR2_SNS;
1239 if (dma_len > fdctrl->data_len)
1240 dma_len = fdctrl->data_len;
1241 if (cur_drv->bs == NULL) {
1242 if (fdctrl->data_dir == FD_DIR_WRITE)
1243 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1244 else
1245 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1246 len = 0;
1247 goto transfer_error;
1249 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1250 for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) {
1251 len = dma_len - fdctrl->data_pos;
1252 if (len + rel_pos > FD_SECTOR_LEN)
1253 len = FD_SECTOR_LEN - rel_pos;
1254 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1255 "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos,
1256 fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head,
1257 cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
1258 fd_sector(cur_drv) * FD_SECTOR_LEN);
1259 if (fdctrl->data_dir != FD_DIR_WRITE ||
1260 len < FD_SECTOR_LEN || rel_pos != 0) {
1261 /* READ & SCAN commands and realign to a sector for WRITE */
1262 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv),
1263 fdctrl->fifo, 1) < 0) {
1264 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1265 fd_sector(cur_drv));
1266 /* Sure, image size is too small... */
1267 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1270 switch (fdctrl->data_dir) {
1271 case FD_DIR_READ:
1272 /* READ commands */
1273 DMA_write_memory (nchan, fdctrl->fifo + rel_pos,
1274 fdctrl->data_pos, len);
1275 break;
1276 case FD_DIR_WRITE:
1277 /* WRITE commands */
1278 DMA_read_memory (nchan, fdctrl->fifo + rel_pos,
1279 fdctrl->data_pos, len);
1280 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv),
1281 fdctrl->fifo, 1) < 0) {
1282 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv));
1283 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1284 goto transfer_error;
1286 break;
1287 default:
1288 /* SCAN commands */
1290 uint8_t tmpbuf[FD_SECTOR_LEN];
1291 int ret;
1292 DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len);
1293 ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
1294 if (ret == 0) {
1295 status2 = FD_SR2_SEH;
1296 goto end_transfer;
1298 if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
1299 (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
1300 status2 = 0x00;
1301 goto end_transfer;
1304 break;
1306 fdctrl->data_pos += len;
1307 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1308 if (rel_pos == 0) {
1309 /* Seek to next sector */
1310 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv))
1311 break;
1314 end_transfer:
1315 len = fdctrl->data_pos - start_pos;
1316 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1317 fdctrl->data_pos, len, fdctrl->data_len);
1318 if (fdctrl->data_dir == FD_DIR_SCANE ||
1319 fdctrl->data_dir == FD_DIR_SCANL ||
1320 fdctrl->data_dir == FD_DIR_SCANH)
1321 status2 = FD_SR2_SEH;
1322 if (FD_DID_SEEK(fdctrl->data_state))
1323 status0 |= FD_SR0_SEEK;
1324 fdctrl->data_len -= len;
1325 fdctrl_stop_transfer(fdctrl, status0, status1, status2);
1326 transfer_error:
1328 return len;
1331 /* Data register : 0x05 */
1332 static uint32_t fdctrl_read_data (fdctrl_t *fdctrl)
1334 fdrive_t *cur_drv;
1335 uint32_t retval = 0;
1336 int pos;
1338 cur_drv = get_cur_drv(fdctrl);
1339 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
1340 if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) {
1341 FLOPPY_ERROR("controller not ready for reading\n");
1342 return 0;
1344 pos = fdctrl->data_pos;
1345 if (fdctrl->msr & FD_MSR_NONDMA) {
1346 pos %= FD_SECTOR_LEN;
1347 if (pos == 0) {
1348 if (fdctrl->data_pos != 0)
1349 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
1350 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1351 fd_sector(cur_drv));
1352 return 0;
1354 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1355 FLOPPY_DPRINTF("error getting sector %d\n",
1356 fd_sector(cur_drv));
1357 /* Sure, image size is too small... */
1358 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1362 retval = fdctrl->fifo[pos];
1363 if (++fdctrl->data_pos == fdctrl->data_len) {
1364 fdctrl->data_pos = 0;
1365 /* Switch from transfer mode to status mode
1366 * then from status mode to command mode
1368 if (fdctrl->msr & FD_MSR_NONDMA) {
1369 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1370 } else {
1371 fdctrl_reset_fifo(fdctrl);
1372 fdctrl_reset_irq(fdctrl);
1375 FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
1377 return retval;
1380 static void fdctrl_format_sector (fdctrl_t *fdctrl)
1382 fdrive_t *cur_drv;
1383 uint8_t kh, kt, ks;
1385 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1386 cur_drv = get_cur_drv(fdctrl);
1387 kt = fdctrl->fifo[6];
1388 kh = fdctrl->fifo[7];
1389 ks = fdctrl->fifo[8];
1390 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1391 GET_CUR_DRV(fdctrl), kh, kt, ks,
1392 _fd_sector(kh, kt, ks, cur_drv->last_sect));
1393 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
1394 case 2:
1395 /* sect too big */
1396 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1397 fdctrl->fifo[3] = kt;
1398 fdctrl->fifo[4] = kh;
1399 fdctrl->fifo[5] = ks;
1400 return;
1401 case 3:
1402 /* track too big */
1403 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
1404 fdctrl->fifo[3] = kt;
1405 fdctrl->fifo[4] = kh;
1406 fdctrl->fifo[5] = ks;
1407 return;
1408 case 4:
1409 /* No seek enabled */
1410 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1411 fdctrl->fifo[3] = kt;
1412 fdctrl->fifo[4] = kh;
1413 fdctrl->fifo[5] = ks;
1414 return;
1415 case 1:
1416 fdctrl->data_state |= FD_STATE_SEEK;
1417 break;
1418 default:
1419 break;
1421 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1422 if (cur_drv->bs == NULL ||
1423 bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1424 FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv));
1425 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1426 } else {
1427 if (cur_drv->sect == cur_drv->last_sect) {
1428 fdctrl->data_state &= ~FD_STATE_FORMAT;
1429 /* Last sector done */
1430 if (FD_DID_SEEK(fdctrl->data_state))
1431 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1432 else
1433 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1434 } else {
1435 /* More to do */
1436 fdctrl->data_pos = 0;
1437 fdctrl->data_len = 4;
1442 static void fdctrl_handle_lock (fdctrl_t *fdctrl, int direction)
1444 fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0;
1445 fdctrl->fifo[0] = fdctrl->lock << 4;
1446 fdctrl_set_fifo(fdctrl, 1, fdctrl->lock);
1449 static void fdctrl_handle_dumpreg (fdctrl_t *fdctrl, int direction)
1451 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1453 /* Drives position */
1454 fdctrl->fifo[0] = drv0(fdctrl)->track;
1455 fdctrl->fifo[1] = drv1(fdctrl)->track;
1456 #if MAX_FD == 4
1457 fdctrl->fifo[2] = drv2(fdctrl)->track;
1458 fdctrl->fifo[3] = drv3(fdctrl)->track;
1459 #else
1460 fdctrl->fifo[2] = 0;
1461 fdctrl->fifo[3] = 0;
1462 #endif
1463 /* timers */
1464 fdctrl->fifo[4] = fdctrl->timer0;
1465 fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0);
1466 fdctrl->fifo[6] = cur_drv->last_sect;
1467 fdctrl->fifo[7] = (fdctrl->lock << 7) |
1468 (cur_drv->perpendicular << 2);
1469 fdctrl->fifo[8] = fdctrl->config;
1470 fdctrl->fifo[9] = fdctrl->precomp_trk;
1471 fdctrl_set_fifo(fdctrl, 10, 0);
1474 static void fdctrl_handle_version (fdctrl_t *fdctrl, int direction)
1476 /* Controller's version */
1477 fdctrl->fifo[0] = fdctrl->version;
1478 fdctrl_set_fifo(fdctrl, 1, 1);
1481 static void fdctrl_handle_partid (fdctrl_t *fdctrl, int direction)
1483 fdctrl->fifo[0] = 0x41; /* Stepping 1 */
1484 fdctrl_set_fifo(fdctrl, 1, 0);
1487 static void fdctrl_handle_restore (fdctrl_t *fdctrl, int direction)
1489 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1491 /* Drives position */
1492 drv0(fdctrl)->track = fdctrl->fifo[3];
1493 drv1(fdctrl)->track = fdctrl->fifo[4];
1494 #if MAX_FD == 4
1495 drv2(fdctrl)->track = fdctrl->fifo[5];
1496 drv3(fdctrl)->track = fdctrl->fifo[6];
1497 #endif
1498 /* timers */
1499 fdctrl->timer0 = fdctrl->fifo[7];
1500 fdctrl->timer1 = fdctrl->fifo[8];
1501 cur_drv->last_sect = fdctrl->fifo[9];
1502 fdctrl->lock = fdctrl->fifo[10] >> 7;
1503 cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF;
1504 fdctrl->config = fdctrl->fifo[11];
1505 fdctrl->precomp_trk = fdctrl->fifo[12];
1506 fdctrl->pwrd = fdctrl->fifo[13];
1507 fdctrl_reset_fifo(fdctrl);
1510 static void fdctrl_handle_save (fdctrl_t *fdctrl, int direction)
1512 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1514 fdctrl->fifo[0] = 0;
1515 fdctrl->fifo[1] = 0;
1516 /* Drives position */
1517 fdctrl->fifo[2] = drv0(fdctrl)->track;
1518 fdctrl->fifo[3] = drv1(fdctrl)->track;
1519 #if MAX_FD == 4
1520 fdctrl->fifo[4] = drv2(fdctrl)->track;
1521 fdctrl->fifo[5] = drv3(fdctrl)->track;
1522 #else
1523 fdctrl->fifo[4] = 0;
1524 fdctrl->fifo[5] = 0;
1525 #endif
1526 /* timers */
1527 fdctrl->fifo[6] = fdctrl->timer0;
1528 fdctrl->fifo[7] = fdctrl->timer1;
1529 fdctrl->fifo[8] = cur_drv->last_sect;
1530 fdctrl->fifo[9] = (fdctrl->lock << 7) |
1531 (cur_drv->perpendicular << 2);
1532 fdctrl->fifo[10] = fdctrl->config;
1533 fdctrl->fifo[11] = fdctrl->precomp_trk;
1534 fdctrl->fifo[12] = fdctrl->pwrd;
1535 fdctrl->fifo[13] = 0;
1536 fdctrl->fifo[14] = 0;
1537 fdctrl_set_fifo(fdctrl, 15, 1);
1540 static void fdctrl_handle_readid (fdctrl_t *fdctrl, int direction)
1542 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1544 /* XXX: should set main status register to busy */
1545 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1546 qemu_mod_timer(fdctrl->result_timer,
1547 qemu_get_clock(vm_clock) + (get_ticks_per_sec() / 50));
1550 static void fdctrl_handle_format_track (fdctrl_t *fdctrl, int direction)
1552 fdrive_t *cur_drv;
1554 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1555 cur_drv = get_cur_drv(fdctrl);
1556 fdctrl->data_state |= FD_STATE_FORMAT;
1557 if (fdctrl->fifo[0] & 0x80)
1558 fdctrl->data_state |= FD_STATE_MULTI;
1559 else
1560 fdctrl->data_state &= ~FD_STATE_MULTI;
1561 fdctrl->data_state &= ~FD_STATE_SEEK;
1562 cur_drv->bps =
1563 fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
1564 #if 0
1565 cur_drv->last_sect =
1566 cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
1567 fdctrl->fifo[3] / 2;
1568 #else
1569 cur_drv->last_sect = fdctrl->fifo[3];
1570 #endif
1571 /* TODO: implement format using DMA expected by the Bochs BIOS
1572 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1573 * the sector with the specified fill byte
1575 fdctrl->data_state &= ~FD_STATE_FORMAT;
1576 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1579 static void fdctrl_handle_specify (fdctrl_t *fdctrl, int direction)
1581 fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF;
1582 fdctrl->timer1 = fdctrl->fifo[2] >> 1;
1583 if (fdctrl->fifo[2] & 1)
1584 fdctrl->dor &= ~FD_DOR_DMAEN;
1585 else
1586 fdctrl->dor |= FD_DOR_DMAEN;
1587 /* No result back */
1588 fdctrl_reset_fifo(fdctrl);
1591 static void fdctrl_handle_sense_drive_status (fdctrl_t *fdctrl, int direction)
1593 fdrive_t *cur_drv;
1595 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1596 cur_drv = get_cur_drv(fdctrl);
1597 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1598 /* 1 Byte status back */
1599 fdctrl->fifo[0] = (cur_drv->ro << 6) |
1600 (cur_drv->track == 0 ? 0x10 : 0x00) |
1601 (cur_drv->head << 2) |
1602 GET_CUR_DRV(fdctrl) |
1603 0x28;
1604 fdctrl_set_fifo(fdctrl, 1, 0);
1607 static void fdctrl_handle_recalibrate (fdctrl_t *fdctrl, int direction)
1609 fdrive_t *cur_drv;
1611 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1612 cur_drv = get_cur_drv(fdctrl);
1613 fd_recalibrate(cur_drv);
1614 fdctrl_reset_fifo(fdctrl);
1615 /* Raise Interrupt */
1616 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1619 static void fdctrl_handle_sense_interrupt_status (fdctrl_t *fdctrl, int direction)
1621 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1623 if(fdctrl->reset_sensei > 0) {
1624 fdctrl->fifo[0] =
1625 FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei;
1626 fdctrl->reset_sensei--;
1627 } else {
1628 /* XXX: status0 handling is broken for read/write
1629 commands, so we do this hack. It should be suppressed
1630 ASAP */
1631 fdctrl->fifo[0] =
1632 FD_SR0_SEEK | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
1635 fdctrl->fifo[1] = cur_drv->track;
1636 fdctrl_set_fifo(fdctrl, 2, 0);
1637 fdctrl_reset_irq(fdctrl);
1638 fdctrl->status0 = FD_SR0_RDYCHG;
1641 static void fdctrl_handle_seek (fdctrl_t *fdctrl, int direction)
1643 fdrive_t *cur_drv;
1645 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1646 cur_drv = get_cur_drv(fdctrl);
1647 fdctrl_reset_fifo(fdctrl);
1648 if (fdctrl->fifo[2] > cur_drv->max_track) {
1649 fdctrl_raise_irq(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK);
1650 } else {
1651 cur_drv->track = fdctrl->fifo[2];
1652 /* Raise Interrupt */
1653 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1657 static void fdctrl_handle_perpendicular_mode (fdctrl_t *fdctrl, int direction)
1659 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1661 if (fdctrl->fifo[1] & 0x80)
1662 cur_drv->perpendicular = fdctrl->fifo[1] & 0x7;
1663 /* No result back */
1664 fdctrl_reset_fifo(fdctrl);
1667 static void fdctrl_handle_configure (fdctrl_t *fdctrl, int direction)
1669 fdctrl->config = fdctrl->fifo[2];
1670 fdctrl->precomp_trk = fdctrl->fifo[3];
1671 /* No result back */
1672 fdctrl_reset_fifo(fdctrl);
1675 static void fdctrl_handle_powerdown_mode (fdctrl_t *fdctrl, int direction)
1677 fdctrl->pwrd = fdctrl->fifo[1];
1678 fdctrl->fifo[0] = fdctrl->fifo[1];
1679 fdctrl_set_fifo(fdctrl, 1, 1);
1682 static void fdctrl_handle_option (fdctrl_t *fdctrl, int direction)
1684 /* No result back */
1685 fdctrl_reset_fifo(fdctrl);
1688 static void fdctrl_handle_drive_specification_command (fdctrl_t *fdctrl, int direction)
1690 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1692 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x80) {
1693 /* Command parameters done */
1694 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x40) {
1695 fdctrl->fifo[0] = fdctrl->fifo[1];
1696 fdctrl->fifo[2] = 0;
1697 fdctrl->fifo[3] = 0;
1698 fdctrl_set_fifo(fdctrl, 4, 1);
1699 } else {
1700 fdctrl_reset_fifo(fdctrl);
1702 } else if (fdctrl->data_len > 7) {
1703 /* ERROR */
1704 fdctrl->fifo[0] = 0x80 |
1705 (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
1706 fdctrl_set_fifo(fdctrl, 1, 1);
1710 static void fdctrl_handle_relative_seek_out (fdctrl_t *fdctrl, int direction)
1712 fdrive_t *cur_drv;
1714 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1715 cur_drv = get_cur_drv(fdctrl);
1716 if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) {
1717 cur_drv->track = cur_drv->max_track - 1;
1718 } else {
1719 cur_drv->track += fdctrl->fifo[2];
1721 fdctrl_reset_fifo(fdctrl);
1722 /* Raise Interrupt */
1723 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1726 static void fdctrl_handle_relative_seek_in (fdctrl_t *fdctrl, int direction)
1728 fdrive_t *cur_drv;
1730 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1731 cur_drv = get_cur_drv(fdctrl);
1732 if (fdctrl->fifo[2] > cur_drv->track) {
1733 cur_drv->track = 0;
1734 } else {
1735 cur_drv->track -= fdctrl->fifo[2];
1737 fdctrl_reset_fifo(fdctrl);
1738 /* Raise Interrupt */
1739 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1742 static const struct {
1743 uint8_t value;
1744 uint8_t mask;
1745 const char* name;
1746 int parameters;
1747 void (*handler)(fdctrl_t *fdctrl, int direction);
1748 int direction;
1749 } handlers[] = {
1750 { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ },
1751 { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE },
1752 { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek },
1753 { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status },
1754 { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate },
1755 { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track },
1756 { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ },
1757 { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */
1758 { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */
1759 { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ },
1760 { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE },
1761 { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_unimplemented },
1762 { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL },
1763 { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH },
1764 { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE },
1765 { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid },
1766 { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify },
1767 { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status },
1768 { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode },
1769 { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure },
1770 { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode },
1771 { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option },
1772 { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command },
1773 { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out },
1774 { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented },
1775 { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in },
1776 { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock },
1777 { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg },
1778 { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version },
1779 { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid },
1780 { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */
1781 { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */
1783 /* Associate command to an index in the 'handlers' array */
1784 static uint8_t command_to_handler[256];
1786 static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value)
1788 fdrive_t *cur_drv;
1789 int pos;
1791 /* Reset mode */
1792 if (!(fdctrl->dor & FD_DOR_nRESET)) {
1793 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1794 return;
1796 if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) {
1797 FLOPPY_ERROR("controller not ready for writing\n");
1798 return;
1800 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
1801 /* Is it write command time ? */
1802 if (fdctrl->msr & FD_MSR_NONDMA) {
1803 /* FIFO data write */
1804 pos = fdctrl->data_pos++;
1805 pos %= FD_SECTOR_LEN;
1806 fdctrl->fifo[pos] = value;
1807 if (pos == FD_SECTOR_LEN - 1 ||
1808 fdctrl->data_pos == fdctrl->data_len) {
1809 cur_drv = get_cur_drv(fdctrl);
1810 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1811 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv));
1812 return;
1814 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
1815 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1816 fd_sector(cur_drv));
1817 return;
1820 /* Switch from transfer mode to status mode
1821 * then from status mode to command mode
1823 if (fdctrl->data_pos == fdctrl->data_len)
1824 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1825 return;
1827 if (fdctrl->data_pos == 0) {
1828 /* Command */
1829 pos = command_to_handler[value & 0xff];
1830 FLOPPY_DPRINTF("%s command\n", handlers[pos].name);
1831 fdctrl->data_len = handlers[pos].parameters + 1;
1834 FLOPPY_DPRINTF("%s: %02x\n", __func__, value);
1835 fdctrl->fifo[fdctrl->data_pos++] = value;
1836 if (fdctrl->data_pos == fdctrl->data_len) {
1837 /* We now have all parameters
1838 * and will be able to treat the command
1840 if (fdctrl->data_state & FD_STATE_FORMAT) {
1841 fdctrl_format_sector(fdctrl);
1842 return;
1845 pos = command_to_handler[fdctrl->fifo[0] & 0xff];
1846 FLOPPY_DPRINTF("treat %s command\n", handlers[pos].name);
1847 (*handlers[pos].handler)(fdctrl, handlers[pos].direction);
1851 static void fdctrl_result_timer(void *opaque)
1853 fdctrl_t *fdctrl = opaque;
1854 fdrive_t *cur_drv = get_cur_drv(fdctrl);
1856 /* Pretend we are spinning.
1857 * This is needed for Coherent, which uses READ ID to check for
1858 * sector interleaving.
1860 if (cur_drv->last_sect != 0) {
1861 cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1;
1863 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1866 /* Init functions */
1867 static void fdctrl_connect_drives(fdctrl_t *fdctrl)
1869 unsigned int i;
1871 for (i = 0; i < MAX_FD; i++) {
1872 fd_init(&fdctrl->drives[i]);
1873 fd_revalidate(&fdctrl->drives[i]);
1877 fdctrl_t *fdctrl_init_isa(DriveInfo **fds)
1879 ISADevice *dev;
1881 dev = isa_create("isa-fdc");
1882 qdev_prop_set_drive(&dev->qdev, "driveA", fds[0]);
1883 qdev_prop_set_drive(&dev->qdev, "driveB", fds[1]);
1884 if (qdev_init(&dev->qdev) < 0)
1885 return NULL;
1886 return &(DO_UPCAST(fdctrl_isabus_t, busdev, dev)->state);
1889 fdctrl_t *fdctrl_init_sysbus(qemu_irq irq, int dma_chann,
1890 target_phys_addr_t mmio_base,
1891 DriveInfo **fds)
1893 fdctrl_t *fdctrl;
1894 DeviceState *dev;
1895 fdctrl_sysbus_t *sys;
1897 dev = qdev_create(NULL, "sysbus-fdc");
1898 sys = DO_UPCAST(fdctrl_sysbus_t, busdev.qdev, dev);
1899 fdctrl = &sys->state;
1900 fdctrl->dma_chann = dma_chann; /* FIXME */
1901 qdev_prop_set_drive(dev, "driveA", fds[0]);
1902 qdev_prop_set_drive(dev, "driveB", fds[1]);
1903 qdev_init_nofail(dev);
1904 sysbus_connect_irq(&sys->busdev, 0, irq);
1905 sysbus_mmio_map(&sys->busdev, 0, mmio_base);
1907 return fdctrl;
1910 fdctrl_t *sun4m_fdctrl_init (qemu_irq irq, target_phys_addr_t io_base,
1911 DriveInfo **fds, qemu_irq *fdc_tc)
1913 DeviceState *dev;
1914 fdctrl_sysbus_t *sys;
1915 fdctrl_t *fdctrl;
1917 dev = qdev_create(NULL, "SUNW,fdtwo");
1918 qdev_prop_set_drive(dev, "drive", fds[0]);
1919 qdev_init_nofail(dev);
1920 sys = DO_UPCAST(fdctrl_sysbus_t, busdev.qdev, dev);
1921 fdctrl = &sys->state;
1922 sysbus_connect_irq(&sys->busdev, 0, irq);
1923 sysbus_mmio_map(&sys->busdev, 0, io_base);
1924 *fdc_tc = qdev_get_gpio_in(dev, 0);
1926 return fdctrl;
1929 static int fdctrl_init_common(fdctrl_t *fdctrl)
1931 int i, j;
1932 static int command_tables_inited = 0;
1934 /* Fill 'command_to_handler' lookup table */
1935 if (!command_tables_inited) {
1936 command_tables_inited = 1;
1937 for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) {
1938 for (j = 0; j < sizeof(command_to_handler); j++) {
1939 if ((j & handlers[i].mask) == handlers[i].value) {
1940 command_to_handler[j] = i;
1946 FLOPPY_DPRINTF("init controller\n");
1947 fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN);
1948 fdctrl->fifo_size = 512;
1949 fdctrl->result_timer = qemu_new_timer(vm_clock,
1950 fdctrl_result_timer, fdctrl);
1952 fdctrl->version = 0x90; /* Intel 82078 controller */
1953 fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */
1954 fdctrl->num_floppies = MAX_FD;
1956 if (fdctrl->dma_chann != -1)
1957 DMA_register_channel(fdctrl->dma_chann, &fdctrl_transfer_handler, fdctrl);
1958 fdctrl_connect_drives(fdctrl);
1960 return 0;
1963 static int isabus_fdc_init1(ISADevice *dev)
1965 fdctrl_isabus_t *isa = DO_UPCAST(fdctrl_isabus_t, busdev, dev);
1966 fdctrl_t *fdctrl = &isa->state;
1967 int iobase = 0x3f0;
1968 int isairq = 6;
1969 int dma_chann = 2;
1970 int ret;
1972 register_ioport_read(iobase + 0x01, 5, 1,
1973 &fdctrl_read_port, fdctrl);
1974 register_ioport_read(iobase + 0x07, 1, 1,
1975 &fdctrl_read_port, fdctrl);
1976 register_ioport_write(iobase + 0x01, 5, 1,
1977 &fdctrl_write_port, fdctrl);
1978 register_ioport_write(iobase + 0x07, 1, 1,
1979 &fdctrl_write_port, fdctrl);
1980 isa_init_irq(&isa->busdev, &fdctrl->irq, isairq);
1981 fdctrl->dma_chann = dma_chann;
1983 ret = fdctrl_init_common(fdctrl);
1985 return ret;
1988 static int sysbus_fdc_init1(SysBusDevice *dev)
1990 fdctrl_sysbus_t *sys = DO_UPCAST(fdctrl_sysbus_t, busdev, dev);
1991 fdctrl_t *fdctrl = &sys->state;
1992 int io;
1993 int ret;
1995 io = cpu_register_io_memory(fdctrl_mem_read, fdctrl_mem_write, fdctrl);
1996 sysbus_init_mmio(dev, 0x08, io);
1997 sysbus_init_irq(dev, &fdctrl->irq);
1998 qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1);
1999 fdctrl->dma_chann = -1;
2001 ret = fdctrl_init_common(fdctrl);
2003 return ret;
2006 static int sun4m_fdc_init1(SysBusDevice *dev)
2008 fdctrl_t *fdctrl = &(FROM_SYSBUS(fdctrl_sysbus_t, dev)->state);
2009 int io;
2011 io = cpu_register_io_memory(fdctrl_mem_read_strict,
2012 fdctrl_mem_write_strict, fdctrl);
2013 sysbus_init_mmio(dev, 0x08, io);
2014 sysbus_init_irq(dev, &fdctrl->irq);
2015 qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1);
2017 fdctrl->sun4m = 1;
2018 return fdctrl_init_common(fdctrl);
2021 static ISADeviceInfo isa_fdc_info = {
2022 .init = isabus_fdc_init1,
2023 .qdev.name = "isa-fdc",
2024 .qdev.size = sizeof(fdctrl_isabus_t),
2025 .qdev.no_user = 1,
2026 .qdev.vmsd = &vmstate_fdc_isa,
2027 .qdev.reset = fdctrl_external_reset_isa,
2028 .qdev.props = (Property[]) {
2029 DEFINE_PROP_DRIVE("driveA", fdctrl_isabus_t, state.drives[0].dinfo),
2030 DEFINE_PROP_DRIVE("driveB", fdctrl_isabus_t, state.drives[1].dinfo),
2031 DEFINE_PROP_END_OF_LIST(),
2035 static SysBusDeviceInfo sysbus_fdc_info = {
2036 .init = sysbus_fdc_init1,
2037 .qdev.name = "sysbus-fdc",
2038 .qdev.size = sizeof(fdctrl_sysbus_t),
2039 .qdev.vmsd = &vmstate_fdc_sysbus,
2040 .qdev.reset = fdctrl_external_reset_sysbus,
2041 .qdev.props = (Property[]) {
2042 DEFINE_PROP_DRIVE("driveA", fdctrl_sysbus_t, state.drives[0].dinfo),
2043 DEFINE_PROP_DRIVE("driveB", fdctrl_sysbus_t, state.drives[1].dinfo),
2044 DEFINE_PROP_END_OF_LIST(),
2048 static SysBusDeviceInfo sun4m_fdc_info = {
2049 .init = sun4m_fdc_init1,
2050 .qdev.name = "SUNW,fdtwo",
2051 .qdev.size = sizeof(fdctrl_sysbus_t),
2052 .qdev.vmsd = &vmstate_fdc_sysbus,
2053 .qdev.reset = fdctrl_external_reset_sysbus,
2054 .qdev.props = (Property[]) {
2055 DEFINE_PROP_DRIVE("drive", fdctrl_sysbus_t, state.drives[0].dinfo),
2056 DEFINE_PROP_END_OF_LIST(),
2060 static void fdc_register_devices(void)
2062 isa_qdev_register(&isa_fdc_info);
2063 sysbus_register_withprop(&sysbus_fdc_info);
2064 sysbus_register_withprop(&sun4m_fdc_info);
2067 device_init(fdc_register_devices)