scsi-disk: adjust offsets in MODE SENSE by 2
[qemu-kvm.git] / hw / fdc.c
blob08830c1ba2c44efd8840bc60cbcd6a7ec4544024
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 "qemu-error.h"
33 #include "qemu-timer.h"
34 #include "isa.h"
35 #include "sysbus.h"
36 #include "qdev-addr.h"
37 #include "blockdev.h"
38 #include "sysemu.h"
39 #include "qemu-log.h"
41 /********************************************************/
42 /* debug Floppy devices */
43 //#define DEBUG_FLOPPY
45 #ifdef DEBUG_FLOPPY
46 #define FLOPPY_DPRINTF(fmt, ...) \
47 do { printf("FLOPPY: " fmt , ## __VA_ARGS__); } while (0)
48 #else
49 #define FLOPPY_DPRINTF(fmt, ...)
50 #endif
52 /********************************************************/
53 /* Floppy drive emulation */
55 typedef enum FDriveRate {
56 FDRIVE_RATE_500K = 0x00, /* 500 Kbps */
57 FDRIVE_RATE_300K = 0x01, /* 300 Kbps */
58 FDRIVE_RATE_250K = 0x02, /* 250 Kbps */
59 FDRIVE_RATE_1M = 0x03, /* 1 Mbps */
60 } FDriveRate;
62 typedef struct FDFormat {
63 FDriveType drive;
64 uint8_t last_sect;
65 uint8_t max_track;
66 uint8_t max_head;
67 FDriveRate rate;
68 } FDFormat;
70 static const FDFormat fd_formats[] = {
71 /* First entry is default format */
72 /* 1.44 MB 3"1/2 floppy disks */
73 { FDRIVE_DRV_144, 18, 80, 1, FDRIVE_RATE_500K, },
74 { FDRIVE_DRV_144, 20, 80, 1, FDRIVE_RATE_500K, },
75 { FDRIVE_DRV_144, 21, 80, 1, FDRIVE_RATE_500K, },
76 { FDRIVE_DRV_144, 21, 82, 1, FDRIVE_RATE_500K, },
77 { FDRIVE_DRV_144, 21, 83, 1, FDRIVE_RATE_500K, },
78 { FDRIVE_DRV_144, 22, 80, 1, FDRIVE_RATE_500K, },
79 { FDRIVE_DRV_144, 23, 80, 1, FDRIVE_RATE_500K, },
80 { FDRIVE_DRV_144, 24, 80, 1, FDRIVE_RATE_500K, },
81 /* 2.88 MB 3"1/2 floppy disks */
82 { FDRIVE_DRV_288, 36, 80, 1, FDRIVE_RATE_1M, },
83 { FDRIVE_DRV_288, 39, 80, 1, FDRIVE_RATE_1M, },
84 { FDRIVE_DRV_288, 40, 80, 1, FDRIVE_RATE_1M, },
85 { FDRIVE_DRV_288, 44, 80, 1, FDRIVE_RATE_1M, },
86 { FDRIVE_DRV_288, 48, 80, 1, FDRIVE_RATE_1M, },
87 /* 720 kB 3"1/2 floppy disks */
88 { FDRIVE_DRV_144, 9, 80, 1, FDRIVE_RATE_250K, },
89 { FDRIVE_DRV_144, 10, 80, 1, FDRIVE_RATE_250K, },
90 { FDRIVE_DRV_144, 10, 82, 1, FDRIVE_RATE_250K, },
91 { FDRIVE_DRV_144, 10, 83, 1, FDRIVE_RATE_250K, },
92 { FDRIVE_DRV_144, 13, 80, 1, FDRIVE_RATE_250K, },
93 { FDRIVE_DRV_144, 14, 80, 1, FDRIVE_RATE_250K, },
94 /* 1.2 MB 5"1/4 floppy disks */
95 { FDRIVE_DRV_120, 15, 80, 1, FDRIVE_RATE_500K, },
96 { FDRIVE_DRV_120, 18, 80, 1, FDRIVE_RATE_500K, },
97 { FDRIVE_DRV_120, 18, 82, 1, FDRIVE_RATE_500K, },
98 { FDRIVE_DRV_120, 18, 83, 1, FDRIVE_RATE_500K, },
99 { FDRIVE_DRV_120, 20, 80, 1, FDRIVE_RATE_500K, },
100 /* 720 kB 5"1/4 floppy disks */
101 { FDRIVE_DRV_120, 9, 80, 1, FDRIVE_RATE_250K, },
102 { FDRIVE_DRV_120, 11, 80, 1, FDRIVE_RATE_250K, },
103 /* 360 kB 5"1/4 floppy disks */
104 { FDRIVE_DRV_120, 9, 40, 1, FDRIVE_RATE_300K, },
105 { FDRIVE_DRV_120, 9, 40, 0, FDRIVE_RATE_300K, },
106 { FDRIVE_DRV_120, 10, 41, 1, FDRIVE_RATE_300K, },
107 { FDRIVE_DRV_120, 10, 42, 1, FDRIVE_RATE_300K, },
108 /* 320 kB 5"1/4 floppy disks */
109 { FDRIVE_DRV_120, 8, 40, 1, FDRIVE_RATE_250K, },
110 { FDRIVE_DRV_120, 8, 40, 0, FDRIVE_RATE_250K, },
111 /* 360 kB must match 5"1/4 better than 3"1/2... */
112 { FDRIVE_DRV_144, 9, 80, 0, FDRIVE_RATE_250K, },
113 /* end */
114 { FDRIVE_DRV_NONE, -1, -1, 0, 0, },
117 static void pick_geometry(BlockDriverState *bs, int *nb_heads,
118 int *max_track, int *last_sect,
119 FDriveType drive_in, FDriveType *drive,
120 FDriveRate *rate)
122 const FDFormat *parse;
123 uint64_t nb_sectors, size;
124 int i, first_match, match;
126 bdrv_get_geometry(bs, &nb_sectors);
127 match = -1;
128 first_match = -1;
129 for (i = 0; ; i++) {
130 parse = &fd_formats[i];
131 if (parse->drive == FDRIVE_DRV_NONE) {
132 break;
134 if (drive_in == parse->drive ||
135 drive_in == FDRIVE_DRV_NONE) {
136 size = (parse->max_head + 1) * parse->max_track *
137 parse->last_sect;
138 if (nb_sectors == size) {
139 match = i;
140 break;
142 if (first_match == -1) {
143 first_match = i;
147 if (match == -1) {
148 if (first_match == -1) {
149 match = 1;
150 } else {
151 match = first_match;
153 parse = &fd_formats[match];
155 *nb_heads = parse->max_head + 1;
156 *max_track = parse->max_track;
157 *last_sect = parse->last_sect;
158 *drive = parse->drive;
159 *rate = parse->rate;
162 #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
163 #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))
165 /* Will always be a fixed parameter for us */
166 #define FD_SECTOR_LEN 512
167 #define FD_SECTOR_SC 2 /* Sector size code */
168 #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */
170 typedef struct FDCtrl FDCtrl;
172 /* Floppy disk drive emulation */
173 typedef enum FDiskFlags {
174 FDISK_DBL_SIDES = 0x01,
175 } FDiskFlags;
177 typedef struct FDrive {
178 FDCtrl *fdctrl;
179 BlockDriverState *bs;
180 /* Drive status */
181 FDriveType drive;
182 uint8_t perpendicular; /* 2.88 MB access mode */
183 /* Position */
184 uint8_t head;
185 uint8_t track;
186 uint8_t sect;
187 /* Media */
188 FDiskFlags flags;
189 uint8_t last_sect; /* Nb sector per track */
190 uint8_t max_track; /* Nb of tracks */
191 uint16_t bps; /* Bytes per sector */
192 uint8_t ro; /* Is read-only */
193 uint8_t media_changed; /* Is media changed */
194 uint8_t media_rate; /* Data rate of medium */
195 } FDrive;
197 static void fd_init(FDrive *drv)
199 /* Drive */
200 drv->drive = FDRIVE_DRV_NONE;
201 drv->perpendicular = 0;
202 /* Disk */
203 drv->last_sect = 0;
204 drv->max_track = 0;
207 #define NUM_SIDES(drv) ((drv)->flags & FDISK_DBL_SIDES ? 2 : 1)
209 static int fd_sector_calc(uint8_t head, uint8_t track, uint8_t sect,
210 uint8_t last_sect, uint8_t num_sides)
212 return (((track * num_sides) + head) * last_sect) + sect - 1;
215 /* Returns current position, in sectors, for given drive */
216 static int fd_sector(FDrive *drv)
218 return fd_sector_calc(drv->head, drv->track, drv->sect, drv->last_sect,
219 NUM_SIDES(drv));
222 /* Seek to a new position:
223 * returns 0 if already on right track
224 * returns 1 if track changed
225 * returns 2 if track is invalid
226 * returns 3 if sector is invalid
227 * returns 4 if seek is disabled
229 static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect,
230 int enable_seek)
232 uint32_t sector;
233 int ret;
235 if (track > drv->max_track ||
236 (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) {
237 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
238 head, track, sect, 1,
239 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
240 drv->max_track, drv->last_sect);
241 return 2;
243 if (sect > drv->last_sect) {
244 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
245 head, track, sect, 1,
246 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
247 drv->max_track, drv->last_sect);
248 return 3;
250 sector = fd_sector_calc(head, track, sect, drv->last_sect, NUM_SIDES(drv));
251 ret = 0;
252 if (sector != fd_sector(drv)) {
253 #if 0
254 if (!enable_seek) {
255 FLOPPY_DPRINTF("error: no implicit seek %d %02x %02x"
256 " (max=%d %02x %02x)\n",
257 head, track, sect, 1, drv->max_track,
258 drv->last_sect);
259 return 4;
261 #endif
262 drv->head = head;
263 if (drv->track != track) {
264 if (drv->bs != NULL && bdrv_is_inserted(drv->bs)) {
265 drv->media_changed = 0;
267 ret = 1;
269 drv->track = track;
270 drv->sect = sect;
273 if (drv->bs == NULL || !bdrv_is_inserted(drv->bs)) {
274 ret = 2;
277 return ret;
280 /* Set drive back to track 0 */
281 static void fd_recalibrate(FDrive *drv)
283 FLOPPY_DPRINTF("recalibrate\n");
284 fd_seek(drv, 0, 0, 1, 1);
287 /* Revalidate a disk drive after a disk change */
288 static void fd_revalidate(FDrive *drv)
290 int nb_heads, max_track, last_sect, ro;
291 FDriveType drive;
292 FDriveRate rate;
294 FLOPPY_DPRINTF("revalidate\n");
295 if (drv->bs != NULL) {
296 ro = bdrv_is_read_only(drv->bs);
297 pick_geometry(drv->bs, &nb_heads, &max_track,
298 &last_sect, drv->drive, &drive, &rate);
299 if (!bdrv_is_inserted(drv->bs)) {
300 FLOPPY_DPRINTF("No disk in drive\n");
301 } else {
302 FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", nb_heads,
303 max_track, last_sect, ro ? "ro" : "rw");
305 if (nb_heads == 1) {
306 drv->flags &= ~FDISK_DBL_SIDES;
307 } else {
308 drv->flags |= FDISK_DBL_SIDES;
310 drv->max_track = max_track;
311 drv->last_sect = last_sect;
312 drv->ro = ro;
313 drv->drive = drive;
314 drv->media_rate = rate;
315 } else {
316 FLOPPY_DPRINTF("No drive connected\n");
317 drv->last_sect = 0;
318 drv->max_track = 0;
319 drv->flags &= ~FDISK_DBL_SIDES;
323 /********************************************************/
324 /* Intel 82078 floppy disk controller emulation */
326 static void fdctrl_reset(FDCtrl *fdctrl, int do_irq);
327 static void fdctrl_reset_fifo(FDCtrl *fdctrl);
328 static int fdctrl_transfer_handler (void *opaque, int nchan,
329 int dma_pos, int dma_len);
330 static void fdctrl_raise_irq(FDCtrl *fdctrl, uint8_t status0);
331 static FDrive *get_cur_drv(FDCtrl *fdctrl);
333 static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl);
334 static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl);
335 static uint32_t fdctrl_read_dor(FDCtrl *fdctrl);
336 static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value);
337 static uint32_t fdctrl_read_tape(FDCtrl *fdctrl);
338 static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value);
339 static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl);
340 static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value);
341 static uint32_t fdctrl_read_data(FDCtrl *fdctrl);
342 static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value);
343 static uint32_t fdctrl_read_dir(FDCtrl *fdctrl);
344 static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value);
346 enum {
347 FD_DIR_WRITE = 0,
348 FD_DIR_READ = 1,
349 FD_DIR_SCANE = 2,
350 FD_DIR_SCANL = 3,
351 FD_DIR_SCANH = 4,
354 enum {
355 FD_STATE_MULTI = 0x01, /* multi track flag */
356 FD_STATE_FORMAT = 0x02, /* format flag */
357 FD_STATE_SEEK = 0x04, /* seek flag */
360 enum {
361 FD_REG_SRA = 0x00,
362 FD_REG_SRB = 0x01,
363 FD_REG_DOR = 0x02,
364 FD_REG_TDR = 0x03,
365 FD_REG_MSR = 0x04,
366 FD_REG_DSR = 0x04,
367 FD_REG_FIFO = 0x05,
368 FD_REG_DIR = 0x07,
369 FD_REG_CCR = 0x07,
372 enum {
373 FD_CMD_READ_TRACK = 0x02,
374 FD_CMD_SPECIFY = 0x03,
375 FD_CMD_SENSE_DRIVE_STATUS = 0x04,
376 FD_CMD_WRITE = 0x05,
377 FD_CMD_READ = 0x06,
378 FD_CMD_RECALIBRATE = 0x07,
379 FD_CMD_SENSE_INTERRUPT_STATUS = 0x08,
380 FD_CMD_WRITE_DELETED = 0x09,
381 FD_CMD_READ_ID = 0x0a,
382 FD_CMD_READ_DELETED = 0x0c,
383 FD_CMD_FORMAT_TRACK = 0x0d,
384 FD_CMD_DUMPREG = 0x0e,
385 FD_CMD_SEEK = 0x0f,
386 FD_CMD_VERSION = 0x10,
387 FD_CMD_SCAN_EQUAL = 0x11,
388 FD_CMD_PERPENDICULAR_MODE = 0x12,
389 FD_CMD_CONFIGURE = 0x13,
390 FD_CMD_LOCK = 0x14,
391 FD_CMD_VERIFY = 0x16,
392 FD_CMD_POWERDOWN_MODE = 0x17,
393 FD_CMD_PART_ID = 0x18,
394 FD_CMD_SCAN_LOW_OR_EQUAL = 0x19,
395 FD_CMD_SCAN_HIGH_OR_EQUAL = 0x1d,
396 FD_CMD_SAVE = 0x2e,
397 FD_CMD_OPTION = 0x33,
398 FD_CMD_RESTORE = 0x4e,
399 FD_CMD_DRIVE_SPECIFICATION_COMMAND = 0x8e,
400 FD_CMD_RELATIVE_SEEK_OUT = 0x8f,
401 FD_CMD_FORMAT_AND_WRITE = 0xcd,
402 FD_CMD_RELATIVE_SEEK_IN = 0xcf,
405 enum {
406 FD_CONFIG_PRETRK = 0xff, /* Pre-compensation set to track 0 */
407 FD_CONFIG_FIFOTHR = 0x0f, /* FIFO threshold set to 1 byte */
408 FD_CONFIG_POLL = 0x10, /* Poll enabled */
409 FD_CONFIG_EFIFO = 0x20, /* FIFO disabled */
410 FD_CONFIG_EIS = 0x40, /* No implied seeks */
413 enum {
414 FD_SR0_DS0 = 0x01,
415 FD_SR0_DS1 = 0x02,
416 FD_SR0_HEAD = 0x04,
417 FD_SR0_EQPMT = 0x10,
418 FD_SR0_SEEK = 0x20,
419 FD_SR0_ABNTERM = 0x40,
420 FD_SR0_INVCMD = 0x80,
421 FD_SR0_RDYCHG = 0xc0,
424 enum {
425 FD_SR1_MA = 0x01, /* Missing address mark */
426 FD_SR1_NW = 0x02, /* Not writable */
427 FD_SR1_EC = 0x80, /* End of cylinder */
430 enum {
431 FD_SR2_SNS = 0x04, /* Scan not satisfied */
432 FD_SR2_SEH = 0x08, /* Scan equal hit */
435 enum {
436 FD_SRA_DIR = 0x01,
437 FD_SRA_nWP = 0x02,
438 FD_SRA_nINDX = 0x04,
439 FD_SRA_HDSEL = 0x08,
440 FD_SRA_nTRK0 = 0x10,
441 FD_SRA_STEP = 0x20,
442 FD_SRA_nDRV2 = 0x40,
443 FD_SRA_INTPEND = 0x80,
446 enum {
447 FD_SRB_MTR0 = 0x01,
448 FD_SRB_MTR1 = 0x02,
449 FD_SRB_WGATE = 0x04,
450 FD_SRB_RDATA = 0x08,
451 FD_SRB_WDATA = 0x10,
452 FD_SRB_DR0 = 0x20,
455 enum {
456 #if MAX_FD == 4
457 FD_DOR_SELMASK = 0x03,
458 #else
459 FD_DOR_SELMASK = 0x01,
460 #endif
461 FD_DOR_nRESET = 0x04,
462 FD_DOR_DMAEN = 0x08,
463 FD_DOR_MOTEN0 = 0x10,
464 FD_DOR_MOTEN1 = 0x20,
465 FD_DOR_MOTEN2 = 0x40,
466 FD_DOR_MOTEN3 = 0x80,
469 enum {
470 #if MAX_FD == 4
471 FD_TDR_BOOTSEL = 0x0c,
472 #else
473 FD_TDR_BOOTSEL = 0x04,
474 #endif
477 enum {
478 FD_DSR_DRATEMASK= 0x03,
479 FD_DSR_PWRDOWN = 0x40,
480 FD_DSR_SWRESET = 0x80,
483 enum {
484 FD_MSR_DRV0BUSY = 0x01,
485 FD_MSR_DRV1BUSY = 0x02,
486 FD_MSR_DRV2BUSY = 0x04,
487 FD_MSR_DRV3BUSY = 0x08,
488 FD_MSR_CMDBUSY = 0x10,
489 FD_MSR_NONDMA = 0x20,
490 FD_MSR_DIO = 0x40,
491 FD_MSR_RQM = 0x80,
494 enum {
495 FD_DIR_DSKCHG = 0x80,
498 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
499 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
500 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
502 struct FDCtrl {
503 MemoryRegion iomem;
504 qemu_irq irq;
505 /* Controller state */
506 QEMUTimer *result_timer;
507 int dma_chann;
508 /* Controller's identification */
509 uint8_t version;
510 /* HW */
511 uint8_t sra;
512 uint8_t srb;
513 uint8_t dor;
514 uint8_t dor_vmstate; /* only used as temp during vmstate */
515 uint8_t tdr;
516 uint8_t dsr;
517 uint8_t msr;
518 uint8_t cur_drv;
519 uint8_t status0;
520 uint8_t status1;
521 uint8_t status2;
522 /* Command FIFO */
523 uint8_t *fifo;
524 int32_t fifo_size;
525 uint32_t data_pos;
526 uint32_t data_len;
527 uint8_t data_state;
528 uint8_t data_dir;
529 uint8_t eot; /* last wanted sector */
530 /* States kept only to be returned back */
531 /* precompensation */
532 uint8_t precomp_trk;
533 uint8_t config;
534 uint8_t lock;
535 /* Power down config (also with status regB access mode */
536 uint8_t pwrd;
537 /* Floppy drives */
538 uint8_t num_floppies;
539 /* Sun4m quirks? */
540 int sun4m;
541 FDrive drives[MAX_FD];
542 int reset_sensei;
543 uint32_t check_media_rate;
544 /* Timers state */
545 uint8_t timer0;
546 uint8_t timer1;
549 typedef struct FDCtrlSysBus {
550 SysBusDevice busdev;
551 struct FDCtrl state;
552 } FDCtrlSysBus;
554 typedef struct FDCtrlISABus {
555 ISADevice busdev;
556 uint32_t iobase;
557 uint32_t irq;
558 uint32_t dma;
559 struct FDCtrl state;
560 int32_t bootindexA;
561 int32_t bootindexB;
562 } FDCtrlISABus;
564 static uint32_t fdctrl_read (void *opaque, uint32_t reg)
566 FDCtrl *fdctrl = opaque;
567 uint32_t retval;
569 reg &= 7;
570 switch (reg) {
571 case FD_REG_SRA:
572 retval = fdctrl_read_statusA(fdctrl);
573 break;
574 case FD_REG_SRB:
575 retval = fdctrl_read_statusB(fdctrl);
576 break;
577 case FD_REG_DOR:
578 retval = fdctrl_read_dor(fdctrl);
579 break;
580 case FD_REG_TDR:
581 retval = fdctrl_read_tape(fdctrl);
582 break;
583 case FD_REG_MSR:
584 retval = fdctrl_read_main_status(fdctrl);
585 break;
586 case FD_REG_FIFO:
587 retval = fdctrl_read_data(fdctrl);
588 break;
589 case FD_REG_DIR:
590 retval = fdctrl_read_dir(fdctrl);
591 break;
592 default:
593 retval = (uint32_t)(-1);
594 break;
596 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval);
598 return retval;
601 static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value)
603 FDCtrl *fdctrl = opaque;
605 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg & 7, value);
607 reg &= 7;
608 switch (reg) {
609 case FD_REG_DOR:
610 fdctrl_write_dor(fdctrl, value);
611 break;
612 case FD_REG_TDR:
613 fdctrl_write_tape(fdctrl, value);
614 break;
615 case FD_REG_DSR:
616 fdctrl_write_rate(fdctrl, value);
617 break;
618 case FD_REG_FIFO:
619 fdctrl_write_data(fdctrl, value);
620 break;
621 case FD_REG_CCR:
622 fdctrl_write_ccr(fdctrl, value);
623 break;
624 default:
625 break;
629 static uint64_t fdctrl_read_mem (void *opaque, target_phys_addr_t reg,
630 unsigned ize)
632 return fdctrl_read(opaque, (uint32_t)reg);
635 static void fdctrl_write_mem (void *opaque, target_phys_addr_t reg,
636 uint64_t value, unsigned size)
638 fdctrl_write(opaque, (uint32_t)reg, value);
641 static const MemoryRegionOps fdctrl_mem_ops = {
642 .read = fdctrl_read_mem,
643 .write = fdctrl_write_mem,
644 .endianness = DEVICE_NATIVE_ENDIAN,
647 static const MemoryRegionOps fdctrl_mem_strict_ops = {
648 .read = fdctrl_read_mem,
649 .write = fdctrl_write_mem,
650 .endianness = DEVICE_NATIVE_ENDIAN,
651 .valid = {
652 .min_access_size = 1,
653 .max_access_size = 1,
657 static bool fdrive_media_changed_needed(void *opaque)
659 FDrive *drive = opaque;
661 return (drive->bs != NULL && drive->media_changed != 1);
664 static const VMStateDescription vmstate_fdrive_media_changed = {
665 .name = "fdrive/media_changed",
666 .version_id = 1,
667 .minimum_version_id = 1,
668 .minimum_version_id_old = 1,
669 .fields = (VMStateField[]) {
670 VMSTATE_UINT8(media_changed, FDrive),
671 VMSTATE_END_OF_LIST()
675 static bool fdrive_media_rate_needed(void *opaque)
677 FDrive *drive = opaque;
679 return drive->fdctrl->check_media_rate;
682 static const VMStateDescription vmstate_fdrive_media_rate = {
683 .name = "fdrive/media_rate",
684 .version_id = 1,
685 .minimum_version_id = 1,
686 .minimum_version_id_old = 1,
687 .fields = (VMStateField[]) {
688 VMSTATE_UINT8(media_rate, FDrive),
689 VMSTATE_END_OF_LIST()
693 static const VMStateDescription vmstate_fdrive = {
694 .name = "fdrive",
695 .version_id = 1,
696 .minimum_version_id = 1,
697 .minimum_version_id_old = 1,
698 .fields = (VMStateField[]) {
699 VMSTATE_UINT8(head, FDrive),
700 VMSTATE_UINT8(track, FDrive),
701 VMSTATE_UINT8(sect, FDrive),
702 VMSTATE_END_OF_LIST()
704 .subsections = (VMStateSubsection[]) {
706 .vmsd = &vmstate_fdrive_media_changed,
707 .needed = &fdrive_media_changed_needed,
708 } , {
709 .vmsd = &vmstate_fdrive_media_rate,
710 .needed = &fdrive_media_rate_needed,
711 } , {
712 /* empty */
717 static void fdc_pre_save(void *opaque)
719 FDCtrl *s = opaque;
721 s->dor_vmstate = s->dor | GET_CUR_DRV(s);
724 static int fdc_post_load(void *opaque, int version_id)
726 FDCtrl *s = opaque;
728 SET_CUR_DRV(s, s->dor_vmstate & FD_DOR_SELMASK);
729 s->dor = s->dor_vmstate & ~FD_DOR_SELMASK;
730 return 0;
733 static const VMStateDescription vmstate_fdc = {
734 .name = "fdc",
735 .version_id = 2,
736 .minimum_version_id = 2,
737 .minimum_version_id_old = 2,
738 .pre_save = fdc_pre_save,
739 .post_load = fdc_post_load,
740 .fields = (VMStateField []) {
741 /* Controller State */
742 VMSTATE_UINT8(sra, FDCtrl),
743 VMSTATE_UINT8(srb, FDCtrl),
744 VMSTATE_UINT8(dor_vmstate, FDCtrl),
745 VMSTATE_UINT8(tdr, FDCtrl),
746 VMSTATE_UINT8(dsr, FDCtrl),
747 VMSTATE_UINT8(msr, FDCtrl),
748 VMSTATE_UINT8(status0, FDCtrl),
749 VMSTATE_UINT8(status1, FDCtrl),
750 VMSTATE_UINT8(status2, FDCtrl),
751 /* Command FIFO */
752 VMSTATE_VARRAY_INT32(fifo, FDCtrl, fifo_size, 0, vmstate_info_uint8,
753 uint8_t),
754 VMSTATE_UINT32(data_pos, FDCtrl),
755 VMSTATE_UINT32(data_len, FDCtrl),
756 VMSTATE_UINT8(data_state, FDCtrl),
757 VMSTATE_UINT8(data_dir, FDCtrl),
758 VMSTATE_UINT8(eot, FDCtrl),
759 /* States kept only to be returned back */
760 VMSTATE_UINT8(timer0, FDCtrl),
761 VMSTATE_UINT8(timer1, FDCtrl),
762 VMSTATE_UINT8(precomp_trk, FDCtrl),
763 VMSTATE_UINT8(config, FDCtrl),
764 VMSTATE_UINT8(lock, FDCtrl),
765 VMSTATE_UINT8(pwrd, FDCtrl),
766 VMSTATE_UINT8_EQUAL(num_floppies, FDCtrl),
767 VMSTATE_STRUCT_ARRAY(drives, FDCtrl, MAX_FD, 1,
768 vmstate_fdrive, FDrive),
769 VMSTATE_END_OF_LIST()
773 static void fdctrl_external_reset_sysbus(DeviceState *d)
775 FDCtrlSysBus *sys = container_of(d, FDCtrlSysBus, busdev.qdev);
776 FDCtrl *s = &sys->state;
778 fdctrl_reset(s, 0);
781 static void fdctrl_external_reset_isa(DeviceState *d)
783 FDCtrlISABus *isa = container_of(d, FDCtrlISABus, busdev.qdev);
784 FDCtrl *s = &isa->state;
786 fdctrl_reset(s, 0);
789 static void fdctrl_handle_tc(void *opaque, int irq, int level)
791 //FDCtrl *s = opaque;
793 if (level) {
794 // XXX
795 FLOPPY_DPRINTF("TC pulsed\n");
799 /* Change IRQ state */
800 static void fdctrl_reset_irq(FDCtrl *fdctrl)
802 if (!(fdctrl->sra & FD_SRA_INTPEND))
803 return;
804 FLOPPY_DPRINTF("Reset interrupt\n");
805 qemu_set_irq(fdctrl->irq, 0);
806 fdctrl->sra &= ~FD_SRA_INTPEND;
809 static void fdctrl_raise_irq(FDCtrl *fdctrl, uint8_t status0)
811 /* Sparc mutation */
812 if (fdctrl->sun4m && (fdctrl->msr & FD_MSR_CMDBUSY)) {
813 /* XXX: not sure */
814 fdctrl->msr &= ~FD_MSR_CMDBUSY;
815 fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO;
816 fdctrl->status0 = status0;
817 return;
819 if (!(fdctrl->sra & FD_SRA_INTPEND)) {
820 qemu_set_irq(fdctrl->irq, 1);
821 fdctrl->sra |= FD_SRA_INTPEND;
824 fdctrl->reset_sensei = 0;
825 fdctrl->status0 = status0;
826 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl->status0);
829 /* Reset controller */
830 static void fdctrl_reset(FDCtrl *fdctrl, int do_irq)
832 int i;
834 FLOPPY_DPRINTF("reset controller\n");
835 fdctrl_reset_irq(fdctrl);
836 /* Initialise controller */
837 fdctrl->sra = 0;
838 fdctrl->srb = 0xc0;
839 if (!fdctrl->drives[1].bs)
840 fdctrl->sra |= FD_SRA_nDRV2;
841 fdctrl->cur_drv = 0;
842 fdctrl->dor = FD_DOR_nRESET;
843 fdctrl->dor |= (fdctrl->dma_chann != -1) ? FD_DOR_DMAEN : 0;
844 fdctrl->msr = FD_MSR_RQM;
845 /* FIFO state */
846 fdctrl->data_pos = 0;
847 fdctrl->data_len = 0;
848 fdctrl->data_state = 0;
849 fdctrl->data_dir = FD_DIR_WRITE;
850 for (i = 0; i < MAX_FD; i++)
851 fd_recalibrate(&fdctrl->drives[i]);
852 fdctrl_reset_fifo(fdctrl);
853 if (do_irq) {
854 fdctrl_raise_irq(fdctrl, FD_SR0_RDYCHG);
855 fdctrl->reset_sensei = FD_RESET_SENSEI_COUNT;
859 static inline FDrive *drv0(FDCtrl *fdctrl)
861 return &fdctrl->drives[(fdctrl->tdr & FD_TDR_BOOTSEL) >> 2];
864 static inline FDrive *drv1(FDCtrl *fdctrl)
866 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (1 << 2))
867 return &fdctrl->drives[1];
868 else
869 return &fdctrl->drives[0];
872 #if MAX_FD == 4
873 static inline FDrive *drv2(FDCtrl *fdctrl)
875 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (2 << 2))
876 return &fdctrl->drives[2];
877 else
878 return &fdctrl->drives[1];
881 static inline FDrive *drv3(FDCtrl *fdctrl)
883 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (3 << 2))
884 return &fdctrl->drives[3];
885 else
886 return &fdctrl->drives[2];
888 #endif
890 static FDrive *get_cur_drv(FDCtrl *fdctrl)
892 switch (fdctrl->cur_drv) {
893 case 0: return drv0(fdctrl);
894 case 1: return drv1(fdctrl);
895 #if MAX_FD == 4
896 case 2: return drv2(fdctrl);
897 case 3: return drv3(fdctrl);
898 #endif
899 default: return NULL;
903 /* Status A register : 0x00 (read-only) */
904 static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl)
906 uint32_t retval = fdctrl->sra;
908 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval);
910 return retval;
913 /* Status B register : 0x01 (read-only) */
914 static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl)
916 uint32_t retval = fdctrl->srb;
918 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval);
920 return retval;
923 /* Digital output register : 0x02 */
924 static uint32_t fdctrl_read_dor(FDCtrl *fdctrl)
926 uint32_t retval = fdctrl->dor;
928 /* Selected drive */
929 retval |= fdctrl->cur_drv;
930 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
932 return retval;
935 static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value)
937 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
939 /* Motors */
940 if (value & FD_DOR_MOTEN0)
941 fdctrl->srb |= FD_SRB_MTR0;
942 else
943 fdctrl->srb &= ~FD_SRB_MTR0;
944 if (value & FD_DOR_MOTEN1)
945 fdctrl->srb |= FD_SRB_MTR1;
946 else
947 fdctrl->srb &= ~FD_SRB_MTR1;
949 /* Drive */
950 if (value & 1)
951 fdctrl->srb |= FD_SRB_DR0;
952 else
953 fdctrl->srb &= ~FD_SRB_DR0;
955 /* Reset */
956 if (!(value & FD_DOR_nRESET)) {
957 if (fdctrl->dor & FD_DOR_nRESET) {
958 FLOPPY_DPRINTF("controller enter RESET state\n");
960 } else {
961 if (!(fdctrl->dor & FD_DOR_nRESET)) {
962 FLOPPY_DPRINTF("controller out of RESET state\n");
963 fdctrl_reset(fdctrl, 1);
964 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
967 /* Selected drive */
968 fdctrl->cur_drv = value & FD_DOR_SELMASK;
970 fdctrl->dor = value;
973 /* Tape drive register : 0x03 */
974 static uint32_t fdctrl_read_tape(FDCtrl *fdctrl)
976 uint32_t retval = fdctrl->tdr;
978 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
980 return retval;
983 static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value)
985 /* Reset mode */
986 if (!(fdctrl->dor & FD_DOR_nRESET)) {
987 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
988 return;
990 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
991 /* Disk boot selection indicator */
992 fdctrl->tdr = value & FD_TDR_BOOTSEL;
993 /* Tape indicators: never allow */
996 /* Main status register : 0x04 (read) */
997 static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl)
999 uint32_t retval = fdctrl->msr;
1001 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
1002 fdctrl->dor |= FD_DOR_nRESET;
1004 /* Sparc mutation */
1005 if (fdctrl->sun4m) {
1006 retval |= FD_MSR_DIO;
1007 fdctrl_reset_irq(fdctrl);
1010 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
1012 return retval;
1015 /* Data select rate register : 0x04 (write) */
1016 static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value)
1018 /* Reset mode */
1019 if (!(fdctrl->dor & FD_DOR_nRESET)) {
1020 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1021 return;
1023 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
1024 /* Reset: autoclear */
1025 if (value & FD_DSR_SWRESET) {
1026 fdctrl->dor &= ~FD_DOR_nRESET;
1027 fdctrl_reset(fdctrl, 1);
1028 fdctrl->dor |= FD_DOR_nRESET;
1030 if (value & FD_DSR_PWRDOWN) {
1031 fdctrl_reset(fdctrl, 1);
1033 fdctrl->dsr = value;
1036 /* Configuration control register: 0x07 (write) */
1037 static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value)
1039 /* Reset mode */
1040 if (!(fdctrl->dor & FD_DOR_nRESET)) {
1041 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1042 return;
1044 FLOPPY_DPRINTF("configuration control register set to 0x%02x\n", value);
1046 /* Only the rate selection bits used in AT mode, and we
1047 * store those in the DSR.
1049 fdctrl->dsr = (fdctrl->dsr & ~FD_DSR_DRATEMASK) |
1050 (value & FD_DSR_DRATEMASK);
1053 static int fdctrl_media_changed(FDrive *drv)
1055 return drv->media_changed;
1058 /* Digital input register : 0x07 (read-only) */
1059 static uint32_t fdctrl_read_dir(FDCtrl *fdctrl)
1061 uint32_t retval = 0;
1063 if (fdctrl_media_changed(get_cur_drv(fdctrl))) {
1064 retval |= FD_DIR_DSKCHG;
1066 if (retval != 0) {
1067 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
1070 return retval;
1073 /* FIFO state control */
1074 static void fdctrl_reset_fifo(FDCtrl *fdctrl)
1076 fdctrl->data_dir = FD_DIR_WRITE;
1077 fdctrl->data_pos = 0;
1078 fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO);
1081 /* Set FIFO status for the host to read */
1082 static void fdctrl_set_fifo(FDCtrl *fdctrl, int fifo_len, uint8_t status0)
1084 fdctrl->data_dir = FD_DIR_READ;
1085 fdctrl->data_len = fifo_len;
1086 fdctrl->data_pos = 0;
1087 fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO;
1088 if (status0) {
1089 fdctrl_raise_irq(fdctrl, status0);
1093 /* Set an error: unimplemented/unknown command */
1094 static void fdctrl_unimplemented(FDCtrl *fdctrl, int direction)
1096 qemu_log_mask(LOG_UNIMP, "fdc: unimplemented command 0x%02x\n",
1097 fdctrl->fifo[0]);
1098 fdctrl->fifo[0] = FD_SR0_INVCMD;
1099 fdctrl_set_fifo(fdctrl, 1, 0);
1102 /* Seek to next sector
1103 * returns 0 when end of track reached (for DBL_SIDES on head 1)
1104 * otherwise returns 1
1106 static int fdctrl_seek_to_next_sect(FDCtrl *fdctrl, FDrive *cur_drv)
1108 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
1109 cur_drv->head, cur_drv->track, cur_drv->sect,
1110 fd_sector(cur_drv));
1111 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1112 error in fact */
1113 uint8_t new_head = cur_drv->head;
1114 uint8_t new_track = cur_drv->track;
1115 uint8_t new_sect = cur_drv->sect;
1117 int ret = 1;
1119 if (new_sect >= cur_drv->last_sect ||
1120 new_sect == fdctrl->eot) {
1121 new_sect = 1;
1122 if (FD_MULTI_TRACK(fdctrl->data_state)) {
1123 if (new_head == 0 &&
1124 (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
1125 new_head = 1;
1126 } else {
1127 new_head = 0;
1128 new_track++;
1129 if ((cur_drv->flags & FDISK_DBL_SIDES) == 0) {
1130 ret = 0;
1133 } else {
1134 new_track++;
1135 ret = 0;
1137 if (ret == 1) {
1138 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1139 new_head, new_track, new_sect, fd_sector(cur_drv));
1141 } else {
1142 new_sect++;
1144 fd_seek(cur_drv, new_head, new_track, new_sect, 1);
1145 return ret;
1148 /* Callback for transfer end (stop or abort) */
1149 static void fdctrl_stop_transfer(FDCtrl *fdctrl, uint8_t status0,
1150 uint8_t status1, uint8_t status2)
1152 FDrive *cur_drv;
1154 cur_drv = get_cur_drv(fdctrl);
1155 fdctrl->status0 = status0 | FD_SR0_SEEK | (cur_drv->head << 2) |
1156 GET_CUR_DRV(fdctrl);
1158 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1159 status0, status1, status2, fdctrl->status0);
1160 fdctrl->fifo[0] = fdctrl->status0;
1161 fdctrl->fifo[1] = status1;
1162 fdctrl->fifo[2] = status2;
1163 fdctrl->fifo[3] = cur_drv->track;
1164 fdctrl->fifo[4] = cur_drv->head;
1165 fdctrl->fifo[5] = cur_drv->sect;
1166 fdctrl->fifo[6] = FD_SECTOR_SC;
1167 fdctrl->data_dir = FD_DIR_READ;
1168 if (!(fdctrl->msr & FD_MSR_NONDMA)) {
1169 DMA_release_DREQ(fdctrl->dma_chann);
1171 fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO;
1172 fdctrl->msr &= ~FD_MSR_NONDMA;
1173 fdctrl_set_fifo(fdctrl, 7, fdctrl->status0);
1176 /* Prepare a data transfer (either DMA or FIFO) */
1177 static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction)
1179 FDrive *cur_drv;
1180 uint8_t kh, kt, ks;
1181 int did_seek = 0;
1183 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1184 cur_drv = get_cur_drv(fdctrl);
1185 kt = fdctrl->fifo[2];
1186 kh = fdctrl->fifo[3];
1187 ks = fdctrl->fifo[4];
1188 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1189 GET_CUR_DRV(fdctrl), kh, kt, ks,
1190 fd_sector_calc(kh, kt, ks, cur_drv->last_sect,
1191 NUM_SIDES(cur_drv)));
1192 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
1193 case 2:
1194 /* sect too big */
1195 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1196 fdctrl->fifo[3] = kt;
1197 fdctrl->fifo[4] = kh;
1198 fdctrl->fifo[5] = ks;
1199 return;
1200 case 3:
1201 /* track too big */
1202 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
1203 fdctrl->fifo[3] = kt;
1204 fdctrl->fifo[4] = kh;
1205 fdctrl->fifo[5] = ks;
1206 return;
1207 case 4:
1208 /* No seek enabled */
1209 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1210 fdctrl->fifo[3] = kt;
1211 fdctrl->fifo[4] = kh;
1212 fdctrl->fifo[5] = ks;
1213 return;
1214 case 1:
1215 did_seek = 1;
1216 break;
1217 default:
1218 break;
1221 /* Check the data rate. If the programmed data rate does not match
1222 * the currently inserted medium, the operation has to fail. */
1223 if (fdctrl->check_media_rate &&
1224 (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) {
1225 FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n",
1226 fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate);
1227 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00);
1228 fdctrl->fifo[3] = kt;
1229 fdctrl->fifo[4] = kh;
1230 fdctrl->fifo[5] = ks;
1231 return;
1234 /* Set the FIFO state */
1235 fdctrl->data_dir = direction;
1236 fdctrl->data_pos = 0;
1237 fdctrl->msr |= FD_MSR_CMDBUSY;
1238 if (fdctrl->fifo[0] & 0x80)
1239 fdctrl->data_state |= FD_STATE_MULTI;
1240 else
1241 fdctrl->data_state &= ~FD_STATE_MULTI;
1242 if (did_seek)
1243 fdctrl->data_state |= FD_STATE_SEEK;
1244 else
1245 fdctrl->data_state &= ~FD_STATE_SEEK;
1246 if (fdctrl->fifo[5] == 00) {
1247 fdctrl->data_len = fdctrl->fifo[8];
1248 } else {
1249 int tmp;
1250 fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]);
1251 tmp = (fdctrl->fifo[6] - ks + 1);
1252 if (fdctrl->fifo[0] & 0x80)
1253 tmp += fdctrl->fifo[6];
1254 fdctrl->data_len *= tmp;
1256 fdctrl->eot = fdctrl->fifo[6];
1257 if (fdctrl->dor & FD_DOR_DMAEN) {
1258 int dma_mode;
1259 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1260 dma_mode = DMA_get_channel_mode(fdctrl->dma_chann);
1261 dma_mode = (dma_mode >> 2) & 3;
1262 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1263 dma_mode, direction,
1264 (128 << fdctrl->fifo[5]) *
1265 (cur_drv->last_sect - ks + 1), fdctrl->data_len);
1266 if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
1267 direction == FD_DIR_SCANH) && dma_mode == 0) ||
1268 (direction == FD_DIR_WRITE && dma_mode == 2) ||
1269 (direction == FD_DIR_READ && dma_mode == 1)) {
1270 /* No access is allowed until DMA transfer has completed */
1271 fdctrl->msr &= ~FD_MSR_RQM;
1272 /* Now, we just have to wait for the DMA controller to
1273 * recall us...
1275 DMA_hold_DREQ(fdctrl->dma_chann);
1276 DMA_schedule(fdctrl->dma_chann);
1277 return;
1278 } else {
1279 FLOPPY_DPRINTF("bad dma_mode=%d direction=%d\n", dma_mode,
1280 direction);
1283 FLOPPY_DPRINTF("start non-DMA transfer\n");
1284 fdctrl->msr |= FD_MSR_NONDMA;
1285 if (direction != FD_DIR_WRITE)
1286 fdctrl->msr |= FD_MSR_DIO;
1287 /* IO based transfer: calculate len */
1288 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1290 return;
1293 /* Prepare a transfer of deleted data */
1294 static void fdctrl_start_transfer_del(FDCtrl *fdctrl, int direction)
1296 qemu_log_mask(LOG_UNIMP, "fdctrl_start_transfer_del() unimplemented\n");
1298 /* We don't handle deleted data,
1299 * so we don't return *ANYTHING*
1301 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1304 /* handlers for DMA transfers */
1305 static int fdctrl_transfer_handler (void *opaque, int nchan,
1306 int dma_pos, int dma_len)
1308 FDCtrl *fdctrl;
1309 FDrive *cur_drv;
1310 int len, start_pos, rel_pos;
1311 uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1313 fdctrl = opaque;
1314 if (fdctrl->msr & FD_MSR_RQM) {
1315 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1316 return 0;
1318 cur_drv = get_cur_drv(fdctrl);
1319 if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
1320 fdctrl->data_dir == FD_DIR_SCANH)
1321 status2 = FD_SR2_SNS;
1322 if (dma_len > fdctrl->data_len)
1323 dma_len = fdctrl->data_len;
1324 if (cur_drv->bs == NULL) {
1325 if (fdctrl->data_dir == FD_DIR_WRITE)
1326 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1327 else
1328 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1329 len = 0;
1330 goto transfer_error;
1332 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1333 for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) {
1334 len = dma_len - fdctrl->data_pos;
1335 if (len + rel_pos > FD_SECTOR_LEN)
1336 len = FD_SECTOR_LEN - rel_pos;
1337 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1338 "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos,
1339 fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head,
1340 cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
1341 fd_sector(cur_drv) * FD_SECTOR_LEN);
1342 if (fdctrl->data_dir != FD_DIR_WRITE ||
1343 len < FD_SECTOR_LEN || rel_pos != 0) {
1344 /* READ & SCAN commands and realign to a sector for WRITE */
1345 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv),
1346 fdctrl->fifo, 1) < 0) {
1347 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1348 fd_sector(cur_drv));
1349 /* Sure, image size is too small... */
1350 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1353 switch (fdctrl->data_dir) {
1354 case FD_DIR_READ:
1355 /* READ commands */
1356 DMA_write_memory (nchan, fdctrl->fifo + rel_pos,
1357 fdctrl->data_pos, len);
1358 break;
1359 case FD_DIR_WRITE:
1360 /* WRITE commands */
1361 if (cur_drv->ro) {
1362 /* Handle readonly medium early, no need to do DMA, touch the
1363 * LED or attempt any writes. A real floppy doesn't attempt
1364 * to write to readonly media either. */
1365 fdctrl_stop_transfer(fdctrl,
1366 FD_SR0_ABNTERM | FD_SR0_SEEK, FD_SR1_NW,
1367 0x00);
1368 goto transfer_error;
1371 DMA_read_memory (nchan, fdctrl->fifo + rel_pos,
1372 fdctrl->data_pos, len);
1373 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv),
1374 fdctrl->fifo, 1) < 0) {
1375 FLOPPY_DPRINTF("error writing sector %d\n",
1376 fd_sector(cur_drv));
1377 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1378 goto transfer_error;
1380 break;
1381 default:
1382 /* SCAN commands */
1384 uint8_t tmpbuf[FD_SECTOR_LEN];
1385 int ret;
1386 DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len);
1387 ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
1388 if (ret == 0) {
1389 status2 = FD_SR2_SEH;
1390 goto end_transfer;
1392 if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
1393 (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
1394 status2 = 0x00;
1395 goto end_transfer;
1398 break;
1400 fdctrl->data_pos += len;
1401 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1402 if (rel_pos == 0) {
1403 /* Seek to next sector */
1404 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv))
1405 break;
1408 end_transfer:
1409 len = fdctrl->data_pos - start_pos;
1410 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1411 fdctrl->data_pos, len, fdctrl->data_len);
1412 if (fdctrl->data_dir == FD_DIR_SCANE ||
1413 fdctrl->data_dir == FD_DIR_SCANL ||
1414 fdctrl->data_dir == FD_DIR_SCANH)
1415 status2 = FD_SR2_SEH;
1416 if (FD_DID_SEEK(fdctrl->data_state))
1417 status0 |= FD_SR0_SEEK;
1418 fdctrl->data_len -= len;
1419 fdctrl_stop_transfer(fdctrl, status0, status1, status2);
1420 transfer_error:
1422 return len;
1425 /* Data register : 0x05 */
1426 static uint32_t fdctrl_read_data(FDCtrl *fdctrl)
1428 FDrive *cur_drv;
1429 uint32_t retval = 0;
1430 int pos;
1432 cur_drv = get_cur_drv(fdctrl);
1433 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
1434 if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) {
1435 FLOPPY_DPRINTF("error: controller not ready for reading\n");
1436 return 0;
1438 pos = fdctrl->data_pos;
1439 if (fdctrl->msr & FD_MSR_NONDMA) {
1440 pos %= FD_SECTOR_LEN;
1441 if (pos == 0) {
1442 if (fdctrl->data_pos != 0)
1443 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
1444 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1445 fd_sector(cur_drv));
1446 return 0;
1448 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1449 FLOPPY_DPRINTF("error getting sector %d\n",
1450 fd_sector(cur_drv));
1451 /* Sure, image size is too small... */
1452 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1456 retval = fdctrl->fifo[pos];
1457 if (++fdctrl->data_pos == fdctrl->data_len) {
1458 fdctrl->data_pos = 0;
1459 /* Switch from transfer mode to status mode
1460 * then from status mode to command mode
1462 if (fdctrl->msr & FD_MSR_NONDMA) {
1463 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1464 } else {
1465 fdctrl_reset_fifo(fdctrl);
1466 fdctrl_reset_irq(fdctrl);
1469 FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
1471 return retval;
1474 static void fdctrl_format_sector(FDCtrl *fdctrl)
1476 FDrive *cur_drv;
1477 uint8_t kh, kt, ks;
1479 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1480 cur_drv = get_cur_drv(fdctrl);
1481 kt = fdctrl->fifo[6];
1482 kh = fdctrl->fifo[7];
1483 ks = fdctrl->fifo[8];
1484 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1485 GET_CUR_DRV(fdctrl), kh, kt, ks,
1486 fd_sector_calc(kh, kt, ks, cur_drv->last_sect,
1487 NUM_SIDES(cur_drv)));
1488 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
1489 case 2:
1490 /* sect too big */
1491 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1492 fdctrl->fifo[3] = kt;
1493 fdctrl->fifo[4] = kh;
1494 fdctrl->fifo[5] = ks;
1495 return;
1496 case 3:
1497 /* track too big */
1498 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
1499 fdctrl->fifo[3] = kt;
1500 fdctrl->fifo[4] = kh;
1501 fdctrl->fifo[5] = ks;
1502 return;
1503 case 4:
1504 /* No seek enabled */
1505 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1506 fdctrl->fifo[3] = kt;
1507 fdctrl->fifo[4] = kh;
1508 fdctrl->fifo[5] = ks;
1509 return;
1510 case 1:
1511 fdctrl->data_state |= FD_STATE_SEEK;
1512 break;
1513 default:
1514 break;
1516 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1517 if (cur_drv->bs == NULL ||
1518 bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1519 FLOPPY_DPRINTF("error formatting sector %d\n", fd_sector(cur_drv));
1520 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1521 } else {
1522 if (cur_drv->sect == cur_drv->last_sect) {
1523 fdctrl->data_state &= ~FD_STATE_FORMAT;
1524 /* Last sector done */
1525 if (FD_DID_SEEK(fdctrl->data_state))
1526 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1527 else
1528 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1529 } else {
1530 /* More to do */
1531 fdctrl->data_pos = 0;
1532 fdctrl->data_len = 4;
1537 static void fdctrl_handle_lock(FDCtrl *fdctrl, int direction)
1539 fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0;
1540 fdctrl->fifo[0] = fdctrl->lock << 4;
1541 fdctrl_set_fifo(fdctrl, 1, 0);
1544 static void fdctrl_handle_dumpreg(FDCtrl *fdctrl, int direction)
1546 FDrive *cur_drv = get_cur_drv(fdctrl);
1548 /* Drives position */
1549 fdctrl->fifo[0] = drv0(fdctrl)->track;
1550 fdctrl->fifo[1] = drv1(fdctrl)->track;
1551 #if MAX_FD == 4
1552 fdctrl->fifo[2] = drv2(fdctrl)->track;
1553 fdctrl->fifo[3] = drv3(fdctrl)->track;
1554 #else
1555 fdctrl->fifo[2] = 0;
1556 fdctrl->fifo[3] = 0;
1557 #endif
1558 /* timers */
1559 fdctrl->fifo[4] = fdctrl->timer0;
1560 fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0);
1561 fdctrl->fifo[6] = cur_drv->last_sect;
1562 fdctrl->fifo[7] = (fdctrl->lock << 7) |
1563 (cur_drv->perpendicular << 2);
1564 fdctrl->fifo[8] = fdctrl->config;
1565 fdctrl->fifo[9] = fdctrl->precomp_trk;
1566 fdctrl_set_fifo(fdctrl, 10, 0);
1569 static void fdctrl_handle_version(FDCtrl *fdctrl, int direction)
1571 /* Controller's version */
1572 fdctrl->fifo[0] = fdctrl->version;
1573 fdctrl_set_fifo(fdctrl, 1, 0);
1576 static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction)
1578 fdctrl->fifo[0] = 0x41; /* Stepping 1 */
1579 fdctrl_set_fifo(fdctrl, 1, 0);
1582 static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction)
1584 FDrive *cur_drv = get_cur_drv(fdctrl);
1586 /* Drives position */
1587 drv0(fdctrl)->track = fdctrl->fifo[3];
1588 drv1(fdctrl)->track = fdctrl->fifo[4];
1589 #if MAX_FD == 4
1590 drv2(fdctrl)->track = fdctrl->fifo[5];
1591 drv3(fdctrl)->track = fdctrl->fifo[6];
1592 #endif
1593 /* timers */
1594 fdctrl->timer0 = fdctrl->fifo[7];
1595 fdctrl->timer1 = fdctrl->fifo[8];
1596 cur_drv->last_sect = fdctrl->fifo[9];
1597 fdctrl->lock = fdctrl->fifo[10] >> 7;
1598 cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF;
1599 fdctrl->config = fdctrl->fifo[11];
1600 fdctrl->precomp_trk = fdctrl->fifo[12];
1601 fdctrl->pwrd = fdctrl->fifo[13];
1602 fdctrl_reset_fifo(fdctrl);
1605 static void fdctrl_handle_save(FDCtrl *fdctrl, int direction)
1607 FDrive *cur_drv = get_cur_drv(fdctrl);
1609 fdctrl->fifo[0] = 0;
1610 fdctrl->fifo[1] = 0;
1611 /* Drives position */
1612 fdctrl->fifo[2] = drv0(fdctrl)->track;
1613 fdctrl->fifo[3] = drv1(fdctrl)->track;
1614 #if MAX_FD == 4
1615 fdctrl->fifo[4] = drv2(fdctrl)->track;
1616 fdctrl->fifo[5] = drv3(fdctrl)->track;
1617 #else
1618 fdctrl->fifo[4] = 0;
1619 fdctrl->fifo[5] = 0;
1620 #endif
1621 /* timers */
1622 fdctrl->fifo[6] = fdctrl->timer0;
1623 fdctrl->fifo[7] = fdctrl->timer1;
1624 fdctrl->fifo[8] = cur_drv->last_sect;
1625 fdctrl->fifo[9] = (fdctrl->lock << 7) |
1626 (cur_drv->perpendicular << 2);
1627 fdctrl->fifo[10] = fdctrl->config;
1628 fdctrl->fifo[11] = fdctrl->precomp_trk;
1629 fdctrl->fifo[12] = fdctrl->pwrd;
1630 fdctrl->fifo[13] = 0;
1631 fdctrl->fifo[14] = 0;
1632 fdctrl_set_fifo(fdctrl, 15, 0);
1635 static void fdctrl_handle_readid(FDCtrl *fdctrl, int direction)
1637 FDrive *cur_drv = get_cur_drv(fdctrl);
1639 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1640 qemu_mod_timer(fdctrl->result_timer,
1641 qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() / 50));
1644 static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction)
1646 FDrive *cur_drv;
1648 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1649 cur_drv = get_cur_drv(fdctrl);
1650 fdctrl->data_state |= FD_STATE_FORMAT;
1651 if (fdctrl->fifo[0] & 0x80)
1652 fdctrl->data_state |= FD_STATE_MULTI;
1653 else
1654 fdctrl->data_state &= ~FD_STATE_MULTI;
1655 fdctrl->data_state &= ~FD_STATE_SEEK;
1656 cur_drv->bps =
1657 fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
1658 #if 0
1659 cur_drv->last_sect =
1660 cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
1661 fdctrl->fifo[3] / 2;
1662 #else
1663 cur_drv->last_sect = fdctrl->fifo[3];
1664 #endif
1665 /* TODO: implement format using DMA expected by the Bochs BIOS
1666 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1667 * the sector with the specified fill byte
1669 fdctrl->data_state &= ~FD_STATE_FORMAT;
1670 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1673 static void fdctrl_handle_specify(FDCtrl *fdctrl, int direction)
1675 fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF;
1676 fdctrl->timer1 = fdctrl->fifo[2] >> 1;
1677 if (fdctrl->fifo[2] & 1)
1678 fdctrl->dor &= ~FD_DOR_DMAEN;
1679 else
1680 fdctrl->dor |= FD_DOR_DMAEN;
1681 /* No result back */
1682 fdctrl_reset_fifo(fdctrl);
1685 static void fdctrl_handle_sense_drive_status(FDCtrl *fdctrl, int direction)
1687 FDrive *cur_drv;
1689 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1690 cur_drv = get_cur_drv(fdctrl);
1691 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1692 /* 1 Byte status back */
1693 fdctrl->fifo[0] = (cur_drv->ro << 6) |
1694 (cur_drv->track == 0 ? 0x10 : 0x00) |
1695 (cur_drv->head << 2) |
1696 GET_CUR_DRV(fdctrl) |
1697 0x28;
1698 fdctrl_set_fifo(fdctrl, 1, 0);
1701 static void fdctrl_handle_recalibrate(FDCtrl *fdctrl, int direction)
1703 FDrive *cur_drv;
1705 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1706 cur_drv = get_cur_drv(fdctrl);
1707 fd_recalibrate(cur_drv);
1708 fdctrl_reset_fifo(fdctrl);
1709 /* Raise Interrupt */
1710 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1713 static void fdctrl_handle_sense_interrupt_status(FDCtrl *fdctrl, int direction)
1715 FDrive *cur_drv = get_cur_drv(fdctrl);
1717 if (fdctrl->reset_sensei > 0) {
1718 fdctrl->fifo[0] =
1719 FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei;
1720 fdctrl->reset_sensei--;
1721 } else if (!(fdctrl->sra & FD_SRA_INTPEND)) {
1722 fdctrl->fifo[0] = FD_SR0_INVCMD;
1723 fdctrl_set_fifo(fdctrl, 1, 0);
1724 return;
1725 } else {
1726 fdctrl->fifo[0] =
1727 (fdctrl->status0 & ~(FD_SR0_HEAD | FD_SR0_DS1 | FD_SR0_DS0))
1728 | GET_CUR_DRV(fdctrl);
1731 fdctrl->fifo[1] = cur_drv->track;
1732 fdctrl_set_fifo(fdctrl, 2, 0);
1733 fdctrl_reset_irq(fdctrl);
1734 fdctrl->status0 = FD_SR0_RDYCHG;
1737 static void fdctrl_handle_seek(FDCtrl *fdctrl, int direction)
1739 FDrive *cur_drv;
1741 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1742 cur_drv = get_cur_drv(fdctrl);
1743 fdctrl_reset_fifo(fdctrl);
1744 /* The seek command just sends step pulses to the drive and doesn't care if
1745 * there is a medium inserted of if it's banging the head against the drive.
1747 fd_seek(cur_drv, cur_drv->head, fdctrl->fifo[2], cur_drv->sect, 1);
1748 /* Raise Interrupt */
1749 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1752 static void fdctrl_handle_perpendicular_mode(FDCtrl *fdctrl, int direction)
1754 FDrive *cur_drv = get_cur_drv(fdctrl);
1756 if (fdctrl->fifo[1] & 0x80)
1757 cur_drv->perpendicular = fdctrl->fifo[1] & 0x7;
1758 /* No result back */
1759 fdctrl_reset_fifo(fdctrl);
1762 static void fdctrl_handle_configure(FDCtrl *fdctrl, int direction)
1764 fdctrl->config = fdctrl->fifo[2];
1765 fdctrl->precomp_trk = fdctrl->fifo[3];
1766 /* No result back */
1767 fdctrl_reset_fifo(fdctrl);
1770 static void fdctrl_handle_powerdown_mode(FDCtrl *fdctrl, int direction)
1772 fdctrl->pwrd = fdctrl->fifo[1];
1773 fdctrl->fifo[0] = fdctrl->fifo[1];
1774 fdctrl_set_fifo(fdctrl, 1, 0);
1777 static void fdctrl_handle_option(FDCtrl *fdctrl, int direction)
1779 /* No result back */
1780 fdctrl_reset_fifo(fdctrl);
1783 static void fdctrl_handle_drive_specification_command(FDCtrl *fdctrl, int direction)
1785 FDrive *cur_drv = get_cur_drv(fdctrl);
1787 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x80) {
1788 /* Command parameters done */
1789 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x40) {
1790 fdctrl->fifo[0] = fdctrl->fifo[1];
1791 fdctrl->fifo[2] = 0;
1792 fdctrl->fifo[3] = 0;
1793 fdctrl_set_fifo(fdctrl, 4, 0);
1794 } else {
1795 fdctrl_reset_fifo(fdctrl);
1797 } else if (fdctrl->data_len > 7) {
1798 /* ERROR */
1799 fdctrl->fifo[0] = 0x80 |
1800 (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
1801 fdctrl_set_fifo(fdctrl, 1, 0);
1805 static void fdctrl_handle_relative_seek_in(FDCtrl *fdctrl, int direction)
1807 FDrive *cur_drv;
1809 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1810 cur_drv = get_cur_drv(fdctrl);
1811 if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) {
1812 fd_seek(cur_drv, cur_drv->head, cur_drv->max_track - 1,
1813 cur_drv->sect, 1);
1814 } else {
1815 fd_seek(cur_drv, cur_drv->head,
1816 cur_drv->track + fdctrl->fifo[2], cur_drv->sect, 1);
1818 fdctrl_reset_fifo(fdctrl);
1819 /* Raise Interrupt */
1820 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1823 static void fdctrl_handle_relative_seek_out(FDCtrl *fdctrl, int direction)
1825 FDrive *cur_drv;
1827 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1828 cur_drv = get_cur_drv(fdctrl);
1829 if (fdctrl->fifo[2] > cur_drv->track) {
1830 fd_seek(cur_drv, cur_drv->head, 0, cur_drv->sect, 1);
1831 } else {
1832 fd_seek(cur_drv, cur_drv->head,
1833 cur_drv->track - fdctrl->fifo[2], cur_drv->sect, 1);
1835 fdctrl_reset_fifo(fdctrl);
1836 /* Raise Interrupt */
1837 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1840 static const struct {
1841 uint8_t value;
1842 uint8_t mask;
1843 const char* name;
1844 int parameters;
1845 void (*handler)(FDCtrl *fdctrl, int direction);
1846 int direction;
1847 } handlers[] = {
1848 { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ },
1849 { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE },
1850 { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek },
1851 { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status },
1852 { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate },
1853 { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track },
1854 { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ },
1855 { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */
1856 { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */
1857 { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ },
1858 { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE },
1859 { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_unimplemented },
1860 { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL },
1861 { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH },
1862 { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE },
1863 { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid },
1864 { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify },
1865 { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status },
1866 { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode },
1867 { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure },
1868 { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode },
1869 { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option },
1870 { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command },
1871 { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out },
1872 { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented },
1873 { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in },
1874 { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock },
1875 { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg },
1876 { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version },
1877 { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid },
1878 { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */
1879 { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */
1881 /* Associate command to an index in the 'handlers' array */
1882 static uint8_t command_to_handler[256];
1884 static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value)
1886 FDrive *cur_drv;
1887 int pos;
1889 /* Reset mode */
1890 if (!(fdctrl->dor & FD_DOR_nRESET)) {
1891 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1892 return;
1894 if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) {
1895 FLOPPY_DPRINTF("error: controller not ready for writing\n");
1896 return;
1898 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
1899 /* Is it write command time ? */
1900 if (fdctrl->msr & FD_MSR_NONDMA) {
1901 /* FIFO data write */
1902 pos = fdctrl->data_pos++;
1903 pos %= FD_SECTOR_LEN;
1904 fdctrl->fifo[pos] = value;
1905 if (pos == FD_SECTOR_LEN - 1 ||
1906 fdctrl->data_pos == fdctrl->data_len) {
1907 cur_drv = get_cur_drv(fdctrl);
1908 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1909 FLOPPY_DPRINTF("error writing sector %d\n",
1910 fd_sector(cur_drv));
1911 return;
1913 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
1914 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1915 fd_sector(cur_drv));
1916 return;
1919 /* Switch from transfer mode to status mode
1920 * then from status mode to command mode
1922 if (fdctrl->data_pos == fdctrl->data_len)
1923 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1924 return;
1926 if (fdctrl->data_pos == 0) {
1927 /* Command */
1928 pos = command_to_handler[value & 0xff];
1929 FLOPPY_DPRINTF("%s command\n", handlers[pos].name);
1930 fdctrl->data_len = handlers[pos].parameters + 1;
1931 fdctrl->msr |= FD_MSR_CMDBUSY;
1934 FLOPPY_DPRINTF("%s: %02x\n", __func__, value);
1935 fdctrl->fifo[fdctrl->data_pos++] = value;
1936 if (fdctrl->data_pos == fdctrl->data_len) {
1937 /* We now have all parameters
1938 * and will be able to treat the command
1940 if (fdctrl->data_state & FD_STATE_FORMAT) {
1941 fdctrl_format_sector(fdctrl);
1942 return;
1945 pos = command_to_handler[fdctrl->fifo[0] & 0xff];
1946 FLOPPY_DPRINTF("treat %s command\n", handlers[pos].name);
1947 (*handlers[pos].handler)(fdctrl, handlers[pos].direction);
1951 static void fdctrl_result_timer(void *opaque)
1953 FDCtrl *fdctrl = opaque;
1954 FDrive *cur_drv = get_cur_drv(fdctrl);
1956 /* Pretend we are spinning.
1957 * This is needed for Coherent, which uses READ ID to check for
1958 * sector interleaving.
1960 if (cur_drv->last_sect != 0) {
1961 cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1;
1963 /* READ_ID can't automatically succeed! */
1964 if (fdctrl->check_media_rate &&
1965 (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) {
1966 FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n",
1967 fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate);
1968 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00);
1969 } else {
1970 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1974 static void fdctrl_change_cb(void *opaque, bool load)
1976 FDrive *drive = opaque;
1978 drive->media_changed = 1;
1979 fd_revalidate(drive);
1982 static const BlockDevOps fdctrl_block_ops = {
1983 .change_media_cb = fdctrl_change_cb,
1986 /* Init functions */
1987 static int fdctrl_connect_drives(FDCtrl *fdctrl)
1989 unsigned int i;
1990 FDrive *drive;
1992 for (i = 0; i < MAX_FD; i++) {
1993 drive = &fdctrl->drives[i];
1994 drive->fdctrl = fdctrl;
1996 if (drive->bs) {
1997 if (bdrv_get_on_error(drive->bs, 0) != BLOCK_ERR_STOP_ENOSPC) {
1998 error_report("fdc doesn't support drive option werror");
1999 return -1;
2001 if (bdrv_get_on_error(drive->bs, 1) != BLOCK_ERR_REPORT) {
2002 error_report("fdc doesn't support drive option rerror");
2003 return -1;
2007 fd_init(drive);
2008 fdctrl_change_cb(drive, 0);
2009 if (drive->bs) {
2010 bdrv_set_dev_ops(drive->bs, &fdctrl_block_ops, drive);
2013 return 0;
2016 ISADevice *fdctrl_init_isa(ISABus *bus, DriveInfo **fds)
2018 ISADevice *dev;
2020 dev = isa_try_create(bus, "isa-fdc");
2021 if (!dev) {
2022 return NULL;
2025 if (fds[0]) {
2026 qdev_prop_set_drive_nofail(&dev->qdev, "driveA", fds[0]->bdrv);
2028 if (fds[1]) {
2029 qdev_prop_set_drive_nofail(&dev->qdev, "driveB", fds[1]->bdrv);
2031 qdev_init_nofail(&dev->qdev);
2033 return dev;
2036 void fdctrl_init_sysbus(qemu_irq irq, int dma_chann,
2037 target_phys_addr_t mmio_base, DriveInfo **fds)
2039 FDCtrl *fdctrl;
2040 DeviceState *dev;
2041 FDCtrlSysBus *sys;
2043 dev = qdev_create(NULL, "sysbus-fdc");
2044 sys = DO_UPCAST(FDCtrlSysBus, busdev.qdev, dev);
2045 fdctrl = &sys->state;
2046 fdctrl->dma_chann = dma_chann; /* FIXME */
2047 if (fds[0]) {
2048 qdev_prop_set_drive_nofail(dev, "driveA", fds[0]->bdrv);
2050 if (fds[1]) {
2051 qdev_prop_set_drive_nofail(dev, "driveB", fds[1]->bdrv);
2053 qdev_init_nofail(dev);
2054 sysbus_connect_irq(&sys->busdev, 0, irq);
2055 sysbus_mmio_map(&sys->busdev, 0, mmio_base);
2058 void sun4m_fdctrl_init(qemu_irq irq, target_phys_addr_t io_base,
2059 DriveInfo **fds, qemu_irq *fdc_tc)
2061 DeviceState *dev;
2062 FDCtrlSysBus *sys;
2064 dev = qdev_create(NULL, "SUNW,fdtwo");
2065 if (fds[0]) {
2066 qdev_prop_set_drive_nofail(dev, "drive", fds[0]->bdrv);
2068 qdev_init_nofail(dev);
2069 sys = DO_UPCAST(FDCtrlSysBus, busdev.qdev, dev);
2070 sysbus_connect_irq(&sys->busdev, 0, irq);
2071 sysbus_mmio_map(&sys->busdev, 0, io_base);
2072 *fdc_tc = qdev_get_gpio_in(dev, 0);
2075 static int fdctrl_init_common(FDCtrl *fdctrl)
2077 int i, j;
2078 static int command_tables_inited = 0;
2080 /* Fill 'command_to_handler' lookup table */
2081 if (!command_tables_inited) {
2082 command_tables_inited = 1;
2083 for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) {
2084 for (j = 0; j < sizeof(command_to_handler); j++) {
2085 if ((j & handlers[i].mask) == handlers[i].value) {
2086 command_to_handler[j] = i;
2092 FLOPPY_DPRINTF("init controller\n");
2093 fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN);
2094 fdctrl->fifo_size = 512;
2095 fdctrl->result_timer = qemu_new_timer_ns(vm_clock,
2096 fdctrl_result_timer, fdctrl);
2098 fdctrl->version = 0x90; /* Intel 82078 controller */
2099 fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */
2100 fdctrl->num_floppies = MAX_FD;
2102 if (fdctrl->dma_chann != -1)
2103 DMA_register_channel(fdctrl->dma_chann, &fdctrl_transfer_handler, fdctrl);
2104 return fdctrl_connect_drives(fdctrl);
2107 static const MemoryRegionPortio fdc_portio_list[] = {
2108 { 1, 5, 1, .read = fdctrl_read, .write = fdctrl_write },
2109 { 7, 1, 1, .read = fdctrl_read, .write = fdctrl_write },
2110 PORTIO_END_OF_LIST(),
2113 static int isabus_fdc_init1(ISADevice *dev)
2115 FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev);
2116 FDCtrl *fdctrl = &isa->state;
2117 int ret;
2119 isa_register_portio_list(dev, isa->iobase, fdc_portio_list, fdctrl, "fdc");
2121 isa_init_irq(&isa->busdev, &fdctrl->irq, isa->irq);
2122 fdctrl->dma_chann = isa->dma;
2124 qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 2);
2125 ret = fdctrl_init_common(fdctrl);
2127 add_boot_device_path(isa->bootindexA, &dev->qdev, "/floppy@0");
2128 add_boot_device_path(isa->bootindexB, &dev->qdev, "/floppy@1");
2130 return ret;
2133 static int sysbus_fdc_init1(SysBusDevice *dev)
2135 FDCtrlSysBus *sys = DO_UPCAST(FDCtrlSysBus, busdev, dev);
2136 FDCtrl *fdctrl = &sys->state;
2137 int ret;
2139 memory_region_init_io(&fdctrl->iomem, &fdctrl_mem_ops, fdctrl, "fdc", 0x08);
2140 sysbus_init_mmio(dev, &fdctrl->iomem);
2141 sysbus_init_irq(dev, &fdctrl->irq);
2142 qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1);
2143 fdctrl->dma_chann = -1;
2145 qdev_set_legacy_instance_id(&dev->qdev, 0 /* io */, 2); /* FIXME */
2146 ret = fdctrl_init_common(fdctrl);
2148 return ret;
2151 static int sun4m_fdc_init1(SysBusDevice *dev)
2153 FDCtrl *fdctrl = &(FROM_SYSBUS(FDCtrlSysBus, dev)->state);
2155 memory_region_init_io(&fdctrl->iomem, &fdctrl_mem_strict_ops, fdctrl,
2156 "fdctrl", 0x08);
2157 sysbus_init_mmio(dev, &fdctrl->iomem);
2158 sysbus_init_irq(dev, &fdctrl->irq);
2159 qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1);
2161 fdctrl->sun4m = 1;
2162 qdev_set_legacy_instance_id(&dev->qdev, 0 /* io */, 2); /* FIXME */
2163 return fdctrl_init_common(fdctrl);
2166 FDriveType isa_fdc_get_drive_type(ISADevice *fdc, int i)
2168 FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, fdc);
2170 return isa->state.drives[i].drive;
2173 static const VMStateDescription vmstate_isa_fdc ={
2174 .name = "fdc",
2175 .version_id = 2,
2176 .minimum_version_id = 2,
2177 .fields = (VMStateField []) {
2178 VMSTATE_STRUCT(state, FDCtrlISABus, 0, vmstate_fdc, FDCtrl),
2179 VMSTATE_END_OF_LIST()
2183 static Property isa_fdc_properties[] = {
2184 DEFINE_PROP_HEX32("iobase", FDCtrlISABus, iobase, 0x3f0),
2185 DEFINE_PROP_UINT32("irq", FDCtrlISABus, irq, 6),
2186 DEFINE_PROP_UINT32("dma", FDCtrlISABus, dma, 2),
2187 DEFINE_PROP_DRIVE("driveA", FDCtrlISABus, state.drives[0].bs),
2188 DEFINE_PROP_DRIVE("driveB", FDCtrlISABus, state.drives[1].bs),
2189 DEFINE_PROP_INT32("bootindexA", FDCtrlISABus, bootindexA, -1),
2190 DEFINE_PROP_INT32("bootindexB", FDCtrlISABus, bootindexB, -1),
2191 DEFINE_PROP_BIT("check_media_rate", FDCtrlISABus, state.check_media_rate,
2192 0, true),
2193 DEFINE_PROP_END_OF_LIST(),
2196 static void isabus_fdc_class_init1(ObjectClass *klass, void *data)
2198 DeviceClass *dc = DEVICE_CLASS(klass);
2199 ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
2200 ic->init = isabus_fdc_init1;
2201 dc->fw_name = "fdc";
2202 dc->no_user = 1;
2203 dc->reset = fdctrl_external_reset_isa;
2204 dc->vmsd = &vmstate_isa_fdc;
2205 dc->props = isa_fdc_properties;
2208 static TypeInfo isa_fdc_info = {
2209 .name = "isa-fdc",
2210 .parent = TYPE_ISA_DEVICE,
2211 .instance_size = sizeof(FDCtrlISABus),
2212 .class_init = isabus_fdc_class_init1,
2215 static const VMStateDescription vmstate_sysbus_fdc ={
2216 .name = "fdc",
2217 .version_id = 2,
2218 .minimum_version_id = 2,
2219 .fields = (VMStateField []) {
2220 VMSTATE_STRUCT(state, FDCtrlSysBus, 0, vmstate_fdc, FDCtrl),
2221 VMSTATE_END_OF_LIST()
2225 static Property sysbus_fdc_properties[] = {
2226 DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus, state.drives[0].bs),
2227 DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus, state.drives[1].bs),
2228 DEFINE_PROP_END_OF_LIST(),
2231 static void sysbus_fdc_class_init(ObjectClass *klass, void *data)
2233 DeviceClass *dc = DEVICE_CLASS(klass);
2234 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
2236 k->init = sysbus_fdc_init1;
2237 dc->reset = fdctrl_external_reset_sysbus;
2238 dc->vmsd = &vmstate_sysbus_fdc;
2239 dc->props = sysbus_fdc_properties;
2242 static TypeInfo sysbus_fdc_info = {
2243 .name = "sysbus-fdc",
2244 .parent = TYPE_SYS_BUS_DEVICE,
2245 .instance_size = sizeof(FDCtrlSysBus),
2246 .class_init = sysbus_fdc_class_init,
2249 static Property sun4m_fdc_properties[] = {
2250 DEFINE_PROP_DRIVE("drive", FDCtrlSysBus, state.drives[0].bs),
2251 DEFINE_PROP_END_OF_LIST(),
2254 static void sun4m_fdc_class_init(ObjectClass *klass, void *data)
2256 DeviceClass *dc = DEVICE_CLASS(klass);
2257 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass);
2259 k->init = sun4m_fdc_init1;
2260 dc->reset = fdctrl_external_reset_sysbus;
2261 dc->vmsd = &vmstate_sysbus_fdc;
2262 dc->props = sun4m_fdc_properties;
2265 static TypeInfo sun4m_fdc_info = {
2266 .name = "SUNW,fdtwo",
2267 .parent = TYPE_SYS_BUS_DEVICE,
2268 .instance_size = sizeof(FDCtrlSysBus),
2269 .class_init = sun4m_fdc_class_init,
2272 static void fdc_register_types(void)
2274 type_register_static(&isa_fdc_info);
2275 type_register_static(&sysbus_fdc_info);
2276 type_register_static(&sun4m_fdc_info);
2279 type_init(fdc_register_types)