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
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 "qemu/osdep.h"
31 #include "hw/block/fdc.h"
32 #include "qapi/error.h"
33 #include "qemu/error-report.h"
34 #include "qemu/timer.h"
35 #include "qemu/memalign.h"
37 #include "hw/isa/isa.h"
38 #include "hw/qdev-properties.h"
39 #include "hw/qdev-properties-system.h"
40 #include "migration/vmstate.h"
41 #include "hw/block/block.h"
42 #include "sysemu/block-backend.h"
43 #include "sysemu/blockdev.h"
44 #include "sysemu/sysemu.h"
46 #include "qemu/main-loop.h"
47 #include "qemu/module.h"
49 #include "qom/object.h"
50 #include "fdc-internal.h"
52 /********************************************************/
53 /* debug Floppy devices */
55 #define DEBUG_FLOPPY 0
57 #define FLOPPY_DPRINTF(fmt, ...) \
60 fprintf(stderr, "FLOPPY: " fmt , ## __VA_ARGS__); \
65 /* Anonymous BlockBackend for empty drive */
66 static BlockBackend
*blk_create_empty_drive(void)
68 return blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL
);
71 /********************************************************/
74 #define TYPE_FLOPPY_BUS "floppy-bus"
75 OBJECT_DECLARE_SIMPLE_TYPE(FloppyBus
, FLOPPY_BUS
)
77 static FDrive
*get_drv(FDCtrl
*fdctrl
, int unit
);
79 static const TypeInfo floppy_bus_info
= {
80 .name
= TYPE_FLOPPY_BUS
,
82 .instance_size
= sizeof(FloppyBus
),
85 static void floppy_bus_create(FDCtrl
*fdc
, FloppyBus
*bus
, DeviceState
*dev
)
87 qbus_init(bus
, sizeof(FloppyBus
), TYPE_FLOPPY_BUS
, dev
, NULL
);
92 /********************************************************/
93 /* Floppy drive emulation */
95 /* In many cases, the total sector size of a format is enough to uniquely
96 * identify it. However, there are some total sector collisions between
97 * formats of different physical size, and these are noted below by
98 * highlighting the total sector size for entries with collisions. */
99 const FDFormat fd_formats
[] = {
100 /* First entry is default format */
101 /* 1.44 MB 3"1/2 floppy disks */
102 { FLOPPY_DRIVE_TYPE_144
, 18, 80, 1, FDRIVE_RATE_500K
, }, /* 3.5" 2880 */
103 { FLOPPY_DRIVE_TYPE_144
, 20, 80, 1, FDRIVE_RATE_500K
, }, /* 3.5" 3200 */
104 { FLOPPY_DRIVE_TYPE_144
, 21, 80, 1, FDRIVE_RATE_500K
, },
105 { FLOPPY_DRIVE_TYPE_144
, 21, 82, 1, FDRIVE_RATE_500K
, },
106 { FLOPPY_DRIVE_TYPE_144
, 21, 83, 1, FDRIVE_RATE_500K
, },
107 { FLOPPY_DRIVE_TYPE_144
, 22, 80, 1, FDRIVE_RATE_500K
, },
108 { FLOPPY_DRIVE_TYPE_144
, 23, 80, 1, FDRIVE_RATE_500K
, },
109 { FLOPPY_DRIVE_TYPE_144
, 24, 80, 1, FDRIVE_RATE_500K
, },
110 /* 2.88 MB 3"1/2 floppy disks */
111 { FLOPPY_DRIVE_TYPE_288
, 36, 80, 1, FDRIVE_RATE_1M
, },
112 { FLOPPY_DRIVE_TYPE_288
, 39, 80, 1, FDRIVE_RATE_1M
, },
113 { FLOPPY_DRIVE_TYPE_288
, 40, 80, 1, FDRIVE_RATE_1M
, },
114 { FLOPPY_DRIVE_TYPE_288
, 44, 80, 1, FDRIVE_RATE_1M
, },
115 { FLOPPY_DRIVE_TYPE_288
, 48, 80, 1, FDRIVE_RATE_1M
, },
116 /* 720 kB 3"1/2 floppy disks */
117 { FLOPPY_DRIVE_TYPE_144
, 9, 80, 1, FDRIVE_RATE_250K
, }, /* 3.5" 1440 */
118 { FLOPPY_DRIVE_TYPE_144
, 10, 80, 1, FDRIVE_RATE_250K
, },
119 { FLOPPY_DRIVE_TYPE_144
, 10, 82, 1, FDRIVE_RATE_250K
, },
120 { FLOPPY_DRIVE_TYPE_144
, 10, 83, 1, FDRIVE_RATE_250K
, },
121 { FLOPPY_DRIVE_TYPE_144
, 13, 80, 1, FDRIVE_RATE_250K
, },
122 { FLOPPY_DRIVE_TYPE_144
, 14, 80, 1, FDRIVE_RATE_250K
, },
123 /* 1.2 MB 5"1/4 floppy disks */
124 { FLOPPY_DRIVE_TYPE_120
, 15, 80, 1, FDRIVE_RATE_500K
, },
125 { FLOPPY_DRIVE_TYPE_120
, 18, 80, 1, FDRIVE_RATE_500K
, }, /* 5.25" 2880 */
126 { FLOPPY_DRIVE_TYPE_120
, 18, 82, 1, FDRIVE_RATE_500K
, },
127 { FLOPPY_DRIVE_TYPE_120
, 18, 83, 1, FDRIVE_RATE_500K
, },
128 { FLOPPY_DRIVE_TYPE_120
, 20, 80, 1, FDRIVE_RATE_500K
, }, /* 5.25" 3200 */
129 /* 720 kB 5"1/4 floppy disks */
130 { FLOPPY_DRIVE_TYPE_120
, 9, 80, 1, FDRIVE_RATE_250K
, }, /* 5.25" 1440 */
131 { FLOPPY_DRIVE_TYPE_120
, 11, 80, 1, FDRIVE_RATE_250K
, },
132 /* 360 kB 5"1/4 floppy disks */
133 { FLOPPY_DRIVE_TYPE_120
, 9, 40, 1, FDRIVE_RATE_300K
, }, /* 5.25" 720 */
134 { FLOPPY_DRIVE_TYPE_120
, 9, 40, 0, FDRIVE_RATE_300K
, },
135 { FLOPPY_DRIVE_TYPE_120
, 10, 41, 1, FDRIVE_RATE_300K
, },
136 { FLOPPY_DRIVE_TYPE_120
, 10, 42, 1, FDRIVE_RATE_300K
, },
137 /* 320 kB 5"1/4 floppy disks */
138 { FLOPPY_DRIVE_TYPE_120
, 8, 40, 1, FDRIVE_RATE_250K
, },
139 { FLOPPY_DRIVE_TYPE_120
, 8, 40, 0, FDRIVE_RATE_250K
, },
140 /* 360 kB must match 5"1/4 better than 3"1/2... */
141 { FLOPPY_DRIVE_TYPE_144
, 9, 80, 0, FDRIVE_RATE_250K
, }, /* 3.5" 720 */
143 { FLOPPY_DRIVE_TYPE_NONE
, -1, -1, 0, 0, },
146 static FDriveSize
drive_size(FloppyDriveType drive
)
149 case FLOPPY_DRIVE_TYPE_120
:
150 return FDRIVE_SIZE_525
;
151 case FLOPPY_DRIVE_TYPE_144
:
152 case FLOPPY_DRIVE_TYPE_288
:
153 return FDRIVE_SIZE_350
;
155 return FDRIVE_SIZE_UNKNOWN
;
159 #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
160 #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))
162 /* Will always be a fixed parameter for us */
163 #define FD_SECTOR_LEN 512
164 #define FD_SECTOR_SC 2 /* Sector size code */
165 #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */
168 static FloppyDriveType
get_fallback_drive_type(FDrive
*drv
);
170 /* Hack: FD_SEEK is expected to work on empty drives. However, QEMU
171 * currently goes through some pains to keep seeks within the bounds
172 * established by last_sect and max_track. Correcting this is difficult,
173 * as refactoring FDC code tends to expose nasty bugs in the Linux kernel.
175 * For now: allow empty drives to have large bounds so we can seek around,
176 * with the understanding that when a diskette is inserted, the bounds will
177 * properly tighten to match the geometry of that inserted medium.
179 static void fd_empty_seek_hack(FDrive
*drv
)
181 drv
->last_sect
= 0xFF;
182 drv
->max_track
= 0xFF;
185 static void fd_init(FDrive
*drv
)
188 drv
->perpendicular
= 0;
190 drv
->disk
= FLOPPY_DRIVE_TYPE_NONE
;
194 drv
->media_changed
= 1;
197 #define NUM_SIDES(drv) ((drv)->flags & FDISK_DBL_SIDES ? 2 : 1)
199 static int fd_sector_calc(uint8_t head
, uint8_t track
, uint8_t sect
,
200 uint8_t last_sect
, uint8_t num_sides
)
202 return (((track
* num_sides
) + head
) * last_sect
) + sect
- 1;
205 /* Returns current position, in sectors, for given drive */
206 static int fd_sector(FDrive
*drv
)
208 return fd_sector_calc(drv
->head
, drv
->track
, drv
->sect
, drv
->last_sect
,
212 /* Returns current position, in bytes, for given drive */
213 static int fd_offset(FDrive
*drv
)
215 g_assert(fd_sector(drv
) < INT_MAX
>> BDRV_SECTOR_BITS
);
216 return fd_sector(drv
) << BDRV_SECTOR_BITS
;
219 /* Seek to a new position:
220 * returns 0 if already on right track
221 * returns 1 if track changed
222 * returns 2 if track is invalid
223 * returns 3 if sector is invalid
224 * returns 4 if seek is disabled
226 static int fd_seek(FDrive
*drv
, uint8_t head
, uint8_t track
, uint8_t sect
,
232 if (track
> drv
->max_track
||
233 (head
!= 0 && (drv
->flags
& FDISK_DBL_SIDES
) == 0)) {
234 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
235 head
, track
, sect
, 1,
236 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
237 drv
->max_track
, drv
->last_sect
);
240 if (sect
> drv
->last_sect
) {
241 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
242 head
, track
, sect
, 1,
243 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
244 drv
->max_track
, drv
->last_sect
);
247 sector
= fd_sector_calc(head
, track
, sect
, drv
->last_sect
, NUM_SIDES(drv
));
249 if (sector
!= fd_sector(drv
)) {
252 FLOPPY_DPRINTF("error: no implicit seek %d %02x %02x"
253 " (max=%d %02x %02x)\n",
254 head
, track
, sect
, 1, drv
->max_track
,
260 if (drv
->track
!= track
) {
261 if (drv
->blk
!= NULL
&& blk_is_inserted(drv
->blk
)) {
262 drv
->media_changed
= 0;
270 if (drv
->blk
== NULL
|| !blk_is_inserted(drv
->blk
)) {
277 /* Set drive back to track 0 */
278 static void fd_recalibrate(FDrive
*drv
)
280 FLOPPY_DPRINTF("recalibrate\n");
281 fd_seek(drv
, 0, 0, 1, 1);
285 * Determine geometry based on inserted diskette.
286 * Will not operate on an empty drive.
288 * @return: 0 on success, -1 if the drive is empty.
290 static int pick_geometry(FDrive
*drv
)
292 BlockBackend
*blk
= drv
->blk
;
293 const FDFormat
*parse
;
294 uint64_t nb_sectors
, size
;
296 int match
, size_match
, type_match
;
297 bool magic
= drv
->drive
== FLOPPY_DRIVE_TYPE_AUTO
;
299 /* We can only pick a geometry if we have a diskette. */
300 if (!drv
->blk
|| !blk_is_inserted(drv
->blk
) ||
301 drv
->drive
== FLOPPY_DRIVE_TYPE_NONE
)
306 /* We need to determine the likely geometry of the inserted medium.
307 * In order of preference, we look for:
308 * (1) The same drive type and number of sectors,
309 * (2) The same diskette size and number of sectors,
310 * (3) The same drive type.
312 * In all cases, matches that occur higher in the drive table will take
313 * precedence over matches that occur later in the table.
315 blk_get_geometry(blk
, &nb_sectors
);
316 match
= size_match
= type_match
= -1;
318 parse
= &fd_formats
[i
];
319 if (parse
->drive
== FLOPPY_DRIVE_TYPE_NONE
) {
322 size
= (parse
->max_head
+ 1) * parse
->max_track
* parse
->last_sect
;
323 if (nb_sectors
== size
) {
324 if (magic
|| parse
->drive
== drv
->drive
) {
325 /* (1) perfect match -- nb_sectors and drive type */
327 } else if (drive_size(parse
->drive
) == drive_size(drv
->drive
)) {
328 /* (2) size match -- nb_sectors and physical medium size */
329 match
= (match
== -1) ? i
: match
;
331 /* This is suspicious -- Did the user misconfigure? */
332 size_match
= (size_match
== -1) ? i
: size_match
;
334 } else if (type_match
== -1) {
335 if ((parse
->drive
== drv
->drive
) ||
336 (magic
&& (parse
->drive
== get_fallback_drive_type(drv
)))) {
337 /* (3) type match -- nb_sectors mismatch, but matches the type
338 * specified explicitly by the user, or matches the fallback
339 * default type when using the drive autodetect mechanism */
345 /* No exact match found */
347 if (size_match
!= -1) {
348 parse
= &fd_formats
[size_match
];
349 FLOPPY_DPRINTF("User requested floppy drive type '%s', "
350 "but inserted medium appears to be a "
351 "%"PRId64
" sector '%s' type\n",
352 FloppyDriveType_str(drv
->drive
),
354 FloppyDriveType_str(parse
->drive
));
356 assert(type_match
!= -1 && "misconfigured fd_format");
359 parse
= &(fd_formats
[match
]);
362 if (parse
->max_head
== 0) {
363 drv
->flags
&= ~FDISK_DBL_SIDES
;
365 drv
->flags
|= FDISK_DBL_SIDES
;
367 drv
->max_track
= parse
->max_track
;
368 drv
->last_sect
= parse
->last_sect
;
369 drv
->disk
= parse
->drive
;
370 drv
->media_rate
= parse
->rate
;
374 static void pick_drive_type(FDrive
*drv
)
376 if (drv
->drive
!= FLOPPY_DRIVE_TYPE_AUTO
) {
380 if (pick_geometry(drv
) == 0) {
381 drv
->drive
= drv
->disk
;
383 drv
->drive
= get_fallback_drive_type(drv
);
386 g_assert(drv
->drive
!= FLOPPY_DRIVE_TYPE_AUTO
);
389 /* Revalidate a disk drive after a disk change */
390 static void fd_revalidate(FDrive
*drv
)
394 FLOPPY_DPRINTF("revalidate\n");
395 if (drv
->blk
!= NULL
) {
396 drv
->ro
= !blk_is_writable(drv
->blk
);
397 if (!blk_is_inserted(drv
->blk
)) {
398 FLOPPY_DPRINTF("No disk in drive\n");
399 drv
->disk
= FLOPPY_DRIVE_TYPE_NONE
;
400 fd_empty_seek_hack(drv
);
401 } else if (!drv
->media_validated
) {
402 rc
= pick_geometry(drv
);
404 FLOPPY_DPRINTF("Could not validate floppy drive media");
406 drv
->media_validated
= true;
407 FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n",
408 (drv
->flags
& FDISK_DBL_SIDES
) ? 2 : 1,
409 drv
->max_track
, drv
->last_sect
,
410 drv
->ro
? "ro" : "rw");
414 FLOPPY_DPRINTF("No drive connected\n");
417 drv
->flags
&= ~FDISK_DBL_SIDES
;
418 drv
->drive
= FLOPPY_DRIVE_TYPE_NONE
;
419 drv
->disk
= FLOPPY_DRIVE_TYPE_NONE
;
423 static void fd_change_cb(void *opaque
, bool load
, Error
**errp
)
425 FDrive
*drive
= opaque
;
428 blk_set_perm(drive
->blk
, 0, BLK_PERM_ALL
, &error_abort
);
430 if (!blkconf_apply_backend_options(drive
->conf
,
431 !blk_supports_write_perm(drive
->blk
),
437 drive
->media_changed
= 1;
438 drive
->media_validated
= false;
439 fd_revalidate(drive
);
442 static const BlockDevOps fd_block_ops
= {
443 .change_media_cb
= fd_change_cb
,
447 #define TYPE_FLOPPY_DRIVE "floppy"
448 OBJECT_DECLARE_SIMPLE_TYPE(FloppyDrive
, FLOPPY_DRIVE
)
454 FloppyDriveType type
;
457 static Property floppy_drive_properties
[] = {
458 DEFINE_PROP_UINT32("unit", FloppyDrive
, unit
, -1),
459 DEFINE_BLOCK_PROPERTIES(FloppyDrive
, conf
),
460 DEFINE_PROP_SIGNED("drive-type", FloppyDrive
, type
,
461 FLOPPY_DRIVE_TYPE_AUTO
, qdev_prop_fdc_drive_type
,
463 DEFINE_PROP_END_OF_LIST(),
466 static void floppy_drive_realize(DeviceState
*qdev
, Error
**errp
)
468 FloppyDrive
*dev
= FLOPPY_DRIVE(qdev
);
469 FloppyBus
*bus
= FLOPPY_BUS(qdev
->parent_bus
);
474 if (dev
->unit
== -1) {
475 for (dev
->unit
= 0; dev
->unit
< MAX_FD
; dev
->unit
++) {
476 drive
= get_drv(bus
->fdc
, dev
->unit
);
483 if (dev
->unit
>= MAX_FD
) {
484 error_setg(errp
, "Can't create floppy unit %d, bus supports "
485 "only %d units", dev
->unit
, MAX_FD
);
489 drive
= get_drv(bus
->fdc
, dev
->unit
);
491 error_setg(errp
, "Floppy unit %d is in use", dev
->unit
);
495 if (!dev
->conf
.blk
) {
496 dev
->conf
.blk
= blk_create_empty_drive();
497 ret
= blk_attach_dev(dev
->conf
.blk
, qdev
);
500 /* Don't take write permissions on an empty drive to allow attaching a
501 * read-only node later */
504 read_only
= !blk_bs(dev
->conf
.blk
) ||
505 !blk_supports_write_perm(dev
->conf
.blk
);
508 if (!blkconf_blocksizes(&dev
->conf
, errp
)) {
512 if (dev
->conf
.logical_block_size
!= 512 ||
513 dev
->conf
.physical_block_size
!= 512)
515 error_setg(errp
, "Physical and logical block size must "
516 "be 512 for floppy");
520 /* rerror/werror aren't supported by fdc and therefore not even registered
521 * with qdev. So set the defaults manually before they are used in
522 * blkconf_apply_backend_options(). */
523 dev
->conf
.rerror
= BLOCKDEV_ON_ERROR_AUTO
;
524 dev
->conf
.werror
= BLOCKDEV_ON_ERROR_AUTO
;
526 if (!blkconf_apply_backend_options(&dev
->conf
, read_only
, false, errp
)) {
530 /* 'enospc' is the default for -drive, 'report' is what blk_new() gives us
531 * for empty drives. */
532 if (blk_get_on_error(dev
->conf
.blk
, 0) != BLOCKDEV_ON_ERROR_ENOSPC
&&
533 blk_get_on_error(dev
->conf
.blk
, 0) != BLOCKDEV_ON_ERROR_REPORT
) {
534 error_setg(errp
, "fdc doesn't support drive option werror");
537 if (blk_get_on_error(dev
->conf
.blk
, 1) != BLOCKDEV_ON_ERROR_REPORT
) {
538 error_setg(errp
, "fdc doesn't support drive option rerror");
542 drive
->conf
= &dev
->conf
;
543 drive
->blk
= dev
->conf
.blk
;
544 drive
->fdctrl
= bus
->fdc
;
547 blk_set_dev_ops(drive
->blk
, &fd_block_ops
, drive
);
549 /* Keep 'type' qdev property and FDrive->drive in sync */
550 drive
->drive
= dev
->type
;
551 pick_drive_type(drive
);
552 dev
->type
= drive
->drive
;
554 fd_revalidate(drive
);
557 static void floppy_drive_class_init(ObjectClass
*klass
, void *data
)
559 DeviceClass
*k
= DEVICE_CLASS(klass
);
560 k
->realize
= floppy_drive_realize
;
561 set_bit(DEVICE_CATEGORY_STORAGE
, k
->categories
);
562 k
->bus_type
= TYPE_FLOPPY_BUS
;
563 device_class_set_props(k
, floppy_drive_properties
);
564 k
->desc
= "virtual floppy drive";
567 static const TypeInfo floppy_drive_info
= {
568 .name
= TYPE_FLOPPY_DRIVE
,
569 .parent
= TYPE_DEVICE
,
570 .instance_size
= sizeof(FloppyDrive
),
571 .class_init
= floppy_drive_class_init
,
574 /********************************************************/
575 /* Intel 82078 floppy disk controller emulation */
577 static void fdctrl_to_command_phase(FDCtrl
*fdctrl
);
578 static void fdctrl_raise_irq(FDCtrl
*fdctrl
);
579 static FDrive
*get_cur_drv(FDCtrl
*fdctrl
);
581 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
);
582 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
);
583 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
);
584 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
);
585 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
);
586 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
);
587 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
);
588 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
);
589 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
);
590 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
);
591 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
);
592 static void fdctrl_write_ccr(FDCtrl
*fdctrl
, uint32_t value
);
604 FD_STATE_MULTI
= 0x01, /* multi track flag */
605 FD_STATE_FORMAT
= 0x02, /* format flag */
621 FD_CMD_READ_TRACK
= 0x02,
622 FD_CMD_SPECIFY
= 0x03,
623 FD_CMD_SENSE_DRIVE_STATUS
= 0x04,
626 FD_CMD_RECALIBRATE
= 0x07,
627 FD_CMD_SENSE_INTERRUPT_STATUS
= 0x08,
628 FD_CMD_WRITE_DELETED
= 0x09,
629 FD_CMD_READ_ID
= 0x0a,
630 FD_CMD_READ_DELETED
= 0x0c,
631 FD_CMD_FORMAT_TRACK
= 0x0d,
632 FD_CMD_DUMPREG
= 0x0e,
634 FD_CMD_VERSION
= 0x10,
635 FD_CMD_SCAN_EQUAL
= 0x11,
636 FD_CMD_PERPENDICULAR_MODE
= 0x12,
637 FD_CMD_CONFIGURE
= 0x13,
639 FD_CMD_VERIFY
= 0x16,
640 FD_CMD_POWERDOWN_MODE
= 0x17,
641 FD_CMD_PART_ID
= 0x18,
642 FD_CMD_SCAN_LOW_OR_EQUAL
= 0x19,
643 FD_CMD_SCAN_HIGH_OR_EQUAL
= 0x1d,
645 FD_CMD_OPTION
= 0x33,
646 FD_CMD_RESTORE
= 0x4e,
647 FD_CMD_DRIVE_SPECIFICATION_COMMAND
= 0x8e,
648 FD_CMD_RELATIVE_SEEK_OUT
= 0x8f,
649 FD_CMD_FORMAT_AND_WRITE
= 0xcd,
650 FD_CMD_RELATIVE_SEEK_IN
= 0xcf,
654 FD_CONFIG_PRETRK
= 0xff, /* Pre-compensation set to track 0 */
655 FD_CONFIG_FIFOTHR
= 0x0f, /* FIFO threshold set to 1 byte */
656 FD_CONFIG_POLL
= 0x10, /* Poll enabled */
657 FD_CONFIG_EFIFO
= 0x20, /* FIFO disabled */
658 FD_CONFIG_EIS
= 0x40, /* No implied seeks */
667 FD_SR0_ABNTERM
= 0x40,
668 FD_SR0_INVCMD
= 0x80,
669 FD_SR0_RDYCHG
= 0xc0,
673 FD_SR1_MA
= 0x01, /* Missing address mark */
674 FD_SR1_NW
= 0x02, /* Not writable */
675 FD_SR1_EC
= 0x80, /* End of cylinder */
679 FD_SR2_SNS
= 0x04, /* Scan not satisfied */
680 FD_SR2_SEH
= 0x08, /* Scan equal hit */
691 FD_SRA_INTPEND
= 0x80,
705 FD_DOR_SELMASK
= 0x03,
707 FD_DOR_SELMASK
= 0x01,
709 FD_DOR_nRESET
= 0x04,
711 FD_DOR_MOTEN0
= 0x10,
712 FD_DOR_MOTEN1
= 0x20,
713 FD_DOR_MOTEN2
= 0x40,
714 FD_DOR_MOTEN3
= 0x80,
719 FD_TDR_BOOTSEL
= 0x0c,
721 FD_TDR_BOOTSEL
= 0x04,
726 FD_DSR_DRATEMASK
= 0x03,
727 FD_DSR_PWRDOWN
= 0x40,
728 FD_DSR_SWRESET
= 0x80,
732 FD_MSR_DRV0BUSY
= 0x01,
733 FD_MSR_DRV1BUSY
= 0x02,
734 FD_MSR_DRV2BUSY
= 0x04,
735 FD_MSR_DRV3BUSY
= 0x08,
736 FD_MSR_CMDBUSY
= 0x10,
737 FD_MSR_NONDMA
= 0x20,
743 FD_DIR_DSKCHG
= 0x80,
747 * See chapter 5.0 "Controller phases" of the spec:
750 * The host writes a command and its parameters into the FIFO. The command
751 * phase is completed when all parameters for the command have been supplied,
752 * and execution phase is entered.
755 * Data transfers, either DMA or non-DMA. For non-DMA transfers, the FIFO
756 * contains the payload now, otherwise it's unused. When all bytes of the
757 * required data have been transferred, the state is switched to either result
758 * phase (if the command produces status bytes) or directly back into the
759 * command phase for the next command.
762 * The host reads out the FIFO, which contains one or more result bytes now.
765 /* Only for migration: reconstruct phase from registers like qemu 2.3 */
766 FD_PHASE_RECONSTRUCT
= 0,
768 FD_PHASE_COMMAND
= 1,
769 FD_PHASE_EXECUTION
= 2,
773 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
774 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
776 static FloppyDriveType
get_fallback_drive_type(FDrive
*drv
)
778 return drv
->fdctrl
->fallback
;
781 uint32_t fdctrl_read(void *opaque
, uint32_t reg
)
783 FDCtrl
*fdctrl
= opaque
;
789 retval
= fdctrl_read_statusA(fdctrl
);
792 retval
= fdctrl_read_statusB(fdctrl
);
795 retval
= fdctrl_read_dor(fdctrl
);
798 retval
= fdctrl_read_tape(fdctrl
);
801 retval
= fdctrl_read_main_status(fdctrl
);
804 retval
= fdctrl_read_data(fdctrl
);
807 retval
= fdctrl_read_dir(fdctrl
);
810 retval
= (uint32_t)(-1);
813 trace_fdc_ioport_read(reg
, retval
);
818 void fdctrl_write(void *opaque
, uint32_t reg
, uint32_t value
)
820 FDCtrl
*fdctrl
= opaque
;
823 trace_fdc_ioport_write(reg
, value
);
826 fdctrl_write_dor(fdctrl
, value
);
829 fdctrl_write_tape(fdctrl
, value
);
832 fdctrl_write_rate(fdctrl
, value
);
835 fdctrl_write_data(fdctrl
, value
);
838 fdctrl_write_ccr(fdctrl
, value
);
845 static bool fdrive_media_changed_needed(void *opaque
)
847 FDrive
*drive
= opaque
;
849 return (drive
->blk
!= NULL
&& drive
->media_changed
!= 1);
852 static const VMStateDescription vmstate_fdrive_media_changed
= {
853 .name
= "fdrive/media_changed",
855 .minimum_version_id
= 1,
856 .needed
= fdrive_media_changed_needed
,
857 .fields
= (const VMStateField
[]) {
858 VMSTATE_UINT8(media_changed
, FDrive
),
859 VMSTATE_END_OF_LIST()
863 static const VMStateDescription vmstate_fdrive_media_rate
= {
864 .name
= "fdrive/media_rate",
866 .minimum_version_id
= 1,
867 .fields
= (const VMStateField
[]) {
868 VMSTATE_UINT8(media_rate
, FDrive
),
869 VMSTATE_END_OF_LIST()
873 static bool fdrive_perpendicular_needed(void *opaque
)
875 FDrive
*drive
= opaque
;
877 return drive
->perpendicular
!= 0;
880 static const VMStateDescription vmstate_fdrive_perpendicular
= {
881 .name
= "fdrive/perpendicular",
883 .minimum_version_id
= 1,
884 .needed
= fdrive_perpendicular_needed
,
885 .fields
= (const VMStateField
[]) {
886 VMSTATE_UINT8(perpendicular
, FDrive
),
887 VMSTATE_END_OF_LIST()
891 static int fdrive_post_load(void *opaque
, int version_id
)
893 fd_revalidate(opaque
);
897 static const VMStateDescription vmstate_fdrive
= {
900 .minimum_version_id
= 1,
901 .post_load
= fdrive_post_load
,
902 .fields
= (const VMStateField
[]) {
903 VMSTATE_UINT8(head
, FDrive
),
904 VMSTATE_UINT8(track
, FDrive
),
905 VMSTATE_UINT8(sect
, FDrive
),
906 VMSTATE_END_OF_LIST()
908 .subsections
= (const VMStateDescription
* const []) {
909 &vmstate_fdrive_media_changed
,
910 &vmstate_fdrive_media_rate
,
911 &vmstate_fdrive_perpendicular
,
917 * Reconstructs the phase from register values according to the logic that was
918 * implemented in qemu 2.3. This is the default value that is used if the phase
919 * subsection is not present on migration.
921 * Don't change this function to reflect newer qemu versions, it is part of
924 static int reconstruct_phase(FDCtrl
*fdctrl
)
926 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
927 return FD_PHASE_EXECUTION
;
928 } else if ((fdctrl
->msr
& FD_MSR_RQM
) == 0) {
929 /* qemu 2.3 disabled RQM only during DMA transfers */
930 return FD_PHASE_EXECUTION
;
931 } else if (fdctrl
->msr
& FD_MSR_DIO
) {
932 return FD_PHASE_RESULT
;
934 return FD_PHASE_COMMAND
;
938 static int fdc_pre_save(void *opaque
)
942 s
->dor_vmstate
= s
->dor
| GET_CUR_DRV(s
);
947 static int fdc_pre_load(void *opaque
)
950 s
->phase
= FD_PHASE_RECONSTRUCT
;
954 static int fdc_post_load(void *opaque
, int version_id
)
958 SET_CUR_DRV(s
, s
->dor_vmstate
& FD_DOR_SELMASK
);
959 s
->dor
= s
->dor_vmstate
& ~FD_DOR_SELMASK
;
961 if (s
->phase
== FD_PHASE_RECONSTRUCT
) {
962 s
->phase
= reconstruct_phase(s
);
968 static bool fdc_reset_sensei_needed(void *opaque
)
972 return s
->reset_sensei
!= 0;
975 static const VMStateDescription vmstate_fdc_reset_sensei
= {
976 .name
= "fdc/reset_sensei",
978 .minimum_version_id
= 1,
979 .needed
= fdc_reset_sensei_needed
,
980 .fields
= (const VMStateField
[]) {
981 VMSTATE_INT32(reset_sensei
, FDCtrl
),
982 VMSTATE_END_OF_LIST()
986 static bool fdc_result_timer_needed(void *opaque
)
990 return timer_pending(s
->result_timer
);
993 static const VMStateDescription vmstate_fdc_result_timer
= {
994 .name
= "fdc/result_timer",
996 .minimum_version_id
= 1,
997 .needed
= fdc_result_timer_needed
,
998 .fields
= (const VMStateField
[]) {
999 VMSTATE_TIMER_PTR(result_timer
, FDCtrl
),
1000 VMSTATE_END_OF_LIST()
1004 static bool fdc_phase_needed(void *opaque
)
1006 FDCtrl
*fdctrl
= opaque
;
1008 return reconstruct_phase(fdctrl
) != fdctrl
->phase
;
1011 static const VMStateDescription vmstate_fdc_phase
= {
1012 .name
= "fdc/phase",
1014 .minimum_version_id
= 1,
1015 .needed
= fdc_phase_needed
,
1016 .fields
= (const VMStateField
[]) {
1017 VMSTATE_UINT8(phase
, FDCtrl
),
1018 VMSTATE_END_OF_LIST()
1022 const VMStateDescription vmstate_fdc
= {
1025 .minimum_version_id
= 2,
1026 .pre_save
= fdc_pre_save
,
1027 .pre_load
= fdc_pre_load
,
1028 .post_load
= fdc_post_load
,
1029 .fields
= (const VMStateField
[]) {
1030 /* Controller State */
1031 VMSTATE_UINT8(sra
, FDCtrl
),
1032 VMSTATE_UINT8(srb
, FDCtrl
),
1033 VMSTATE_UINT8(dor_vmstate
, FDCtrl
),
1034 VMSTATE_UINT8(tdr
, FDCtrl
),
1035 VMSTATE_UINT8(dsr
, FDCtrl
),
1036 VMSTATE_UINT8(msr
, FDCtrl
),
1037 VMSTATE_UINT8(status0
, FDCtrl
),
1038 VMSTATE_UINT8(status1
, FDCtrl
),
1039 VMSTATE_UINT8(status2
, FDCtrl
),
1041 VMSTATE_VARRAY_INT32(fifo
, FDCtrl
, fifo_size
, 0, vmstate_info_uint8
,
1043 VMSTATE_UINT32(data_pos
, FDCtrl
),
1044 VMSTATE_UINT32(data_len
, FDCtrl
),
1045 VMSTATE_UINT8(data_state
, FDCtrl
),
1046 VMSTATE_UINT8(data_dir
, FDCtrl
),
1047 VMSTATE_UINT8(eot
, FDCtrl
),
1048 /* States kept only to be returned back */
1049 VMSTATE_UINT8(timer0
, FDCtrl
),
1050 VMSTATE_UINT8(timer1
, FDCtrl
),
1051 VMSTATE_UINT8(precomp_trk
, FDCtrl
),
1052 VMSTATE_UINT8(config
, FDCtrl
),
1053 VMSTATE_UINT8(lock
, FDCtrl
),
1054 VMSTATE_UINT8(pwrd
, FDCtrl
),
1055 VMSTATE_UINT8_EQUAL(num_floppies
, FDCtrl
, NULL
),
1056 VMSTATE_STRUCT_ARRAY(drives
, FDCtrl
, MAX_FD
, 1,
1057 vmstate_fdrive
, FDrive
),
1058 VMSTATE_END_OF_LIST()
1060 .subsections
= (const VMStateDescription
* const []) {
1061 &vmstate_fdc_reset_sensei
,
1062 &vmstate_fdc_result_timer
,
1068 /* Change IRQ state */
1069 static void fdctrl_reset_irq(FDCtrl
*fdctrl
)
1071 fdctrl
->status0
= 0;
1072 if (!(fdctrl
->sra
& FD_SRA_INTPEND
))
1074 FLOPPY_DPRINTF("Reset interrupt\n");
1075 qemu_set_irq(fdctrl
->irq
, 0);
1076 fdctrl
->sra
&= ~FD_SRA_INTPEND
;
1079 static void fdctrl_raise_irq(FDCtrl
*fdctrl
)
1081 if (!(fdctrl
->sra
& FD_SRA_INTPEND
)) {
1082 qemu_set_irq(fdctrl
->irq
, 1);
1083 fdctrl
->sra
|= FD_SRA_INTPEND
;
1086 fdctrl
->reset_sensei
= 0;
1087 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl
->status0
);
1090 /* Reset controller */
1091 void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
)
1095 FLOPPY_DPRINTF("reset controller\n");
1096 fdctrl_reset_irq(fdctrl
);
1097 /* Initialise controller */
1100 if (!fdctrl
->drives
[1].blk
) {
1101 fdctrl
->sra
|= FD_SRA_nDRV2
;
1103 fdctrl
->cur_drv
= 0;
1104 fdctrl
->dor
= FD_DOR_nRESET
;
1105 fdctrl
->dor
|= (fdctrl
->dma_chann
!= -1) ? FD_DOR_DMAEN
: 0;
1106 fdctrl
->msr
= FD_MSR_RQM
;
1107 fdctrl
->reset_sensei
= 0;
1108 timer_del(fdctrl
->result_timer
);
1110 fdctrl
->data_pos
= 0;
1111 fdctrl
->data_len
= 0;
1112 fdctrl
->data_state
= 0;
1113 fdctrl
->data_dir
= FD_DIR_WRITE
;
1114 for (i
= 0; i
< MAX_FD
; i
++)
1115 fd_recalibrate(&fdctrl
->drives
[i
]);
1116 fdctrl_to_command_phase(fdctrl
);
1118 fdctrl
->status0
|= FD_SR0_RDYCHG
;
1119 fdctrl_raise_irq(fdctrl
);
1120 fdctrl
->reset_sensei
= FD_RESET_SENSEI_COUNT
;
1124 static inline FDrive
*drv0(FDCtrl
*fdctrl
)
1126 return &fdctrl
->drives
[(fdctrl
->tdr
& FD_TDR_BOOTSEL
) >> 2];
1129 static inline FDrive
*drv1(FDCtrl
*fdctrl
)
1131 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (1 << 2))
1132 return &fdctrl
->drives
[1];
1134 return &fdctrl
->drives
[0];
1138 static inline FDrive
*drv2(FDCtrl
*fdctrl
)
1140 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (2 << 2))
1141 return &fdctrl
->drives
[2];
1143 return &fdctrl
->drives
[1];
1146 static inline FDrive
*drv3(FDCtrl
*fdctrl
)
1148 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (3 << 2))
1149 return &fdctrl
->drives
[3];
1151 return &fdctrl
->drives
[2];
1155 static FDrive
*get_drv(FDCtrl
*fdctrl
, int unit
)
1158 case 0: return drv0(fdctrl
);
1159 case 1: return drv1(fdctrl
);
1161 case 2: return drv2(fdctrl
);
1162 case 3: return drv3(fdctrl
);
1164 default: return NULL
;
1168 static FDrive
*get_cur_drv(FDCtrl
*fdctrl
)
1170 FDrive
*cur_drv
= get_drv(fdctrl
, fdctrl
->cur_drv
);
1172 if (!cur_drv
->blk
) {
1174 * Kludge: empty drive line selected. Create an anonymous
1175 * BlockBackend to avoid NULL deref with various BlockBackend
1176 * API calls within this model (CVE-2021-20196).
1177 * Due to the controller QOM model limitations, we don't
1178 * attach the created to the controller device.
1180 cur_drv
->blk
= blk_create_empty_drive();
1185 /* Status A register : 0x00 (read-only) */
1186 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
)
1188 uint32_t retval
= fdctrl
->sra
;
1190 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval
);
1195 /* Status B register : 0x01 (read-only) */
1196 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
)
1198 uint32_t retval
= fdctrl
->srb
;
1200 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval
);
1205 /* Digital output register : 0x02 */
1206 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
)
1208 uint32_t retval
= fdctrl
->dor
;
1210 /* Selected drive */
1211 retval
|= fdctrl
->cur_drv
;
1212 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
1217 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
)
1219 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
1222 if (value
& FD_DOR_MOTEN0
)
1223 fdctrl
->srb
|= FD_SRB_MTR0
;
1225 fdctrl
->srb
&= ~FD_SRB_MTR0
;
1226 if (value
& FD_DOR_MOTEN1
)
1227 fdctrl
->srb
|= FD_SRB_MTR1
;
1229 fdctrl
->srb
&= ~FD_SRB_MTR1
;
1233 fdctrl
->srb
|= FD_SRB_DR0
;
1235 fdctrl
->srb
&= ~FD_SRB_DR0
;
1238 if (!(value
& FD_DOR_nRESET
)) {
1239 if (fdctrl
->dor
& FD_DOR_nRESET
) {
1240 FLOPPY_DPRINTF("controller enter RESET state\n");
1243 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1244 FLOPPY_DPRINTF("controller out of RESET state\n");
1245 fdctrl_reset(fdctrl
, 1);
1246 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1249 /* Selected drive */
1250 fdctrl
->cur_drv
= value
& FD_DOR_SELMASK
;
1252 fdctrl
->dor
= value
;
1255 /* Tape drive register : 0x03 */
1256 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
)
1258 uint32_t retval
= fdctrl
->tdr
;
1260 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
1265 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
)
1268 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1269 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1272 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
1273 /* Disk boot selection indicator */
1274 fdctrl
->tdr
= value
& FD_TDR_BOOTSEL
;
1275 /* Tape indicators: never allow */
1278 /* Main status register : 0x04 (read) */
1279 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
)
1281 uint32_t retval
= fdctrl
->msr
;
1283 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1284 fdctrl
->dor
|= FD_DOR_nRESET
;
1286 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
1291 /* Data select rate register : 0x04 (write) */
1292 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
)
1295 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1296 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1299 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
1300 /* Reset: autoclear */
1301 if (value
& FD_DSR_SWRESET
) {
1302 fdctrl
->dor
&= ~FD_DOR_nRESET
;
1303 fdctrl_reset(fdctrl
, 1);
1304 fdctrl
->dor
|= FD_DOR_nRESET
;
1306 if (value
& FD_DSR_PWRDOWN
) {
1307 fdctrl_reset(fdctrl
, 1);
1309 fdctrl
->dsr
= value
;
1312 /* Configuration control register: 0x07 (write) */
1313 static void fdctrl_write_ccr(FDCtrl
*fdctrl
, uint32_t value
)
1316 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1317 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1320 FLOPPY_DPRINTF("configuration control register set to 0x%02x\n", value
);
1322 /* Only the rate selection bits used in AT mode, and we
1323 * store those in the DSR.
1325 fdctrl
->dsr
= (fdctrl
->dsr
& ~FD_DSR_DRATEMASK
) |
1326 (value
& FD_DSR_DRATEMASK
);
1329 static int fdctrl_media_changed(FDrive
*drv
)
1331 return drv
->media_changed
;
1334 /* Digital input register : 0x07 (read-only) */
1335 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
)
1337 uint32_t retval
= 0;
1339 if (fdctrl_media_changed(get_cur_drv(fdctrl
))) {
1340 retval
|= FD_DIR_DSKCHG
;
1343 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
1349 /* Clear the FIFO and update the state for receiving the next command */
1350 static void fdctrl_to_command_phase(FDCtrl
*fdctrl
)
1352 fdctrl
->phase
= FD_PHASE_COMMAND
;
1353 fdctrl
->data_dir
= FD_DIR_WRITE
;
1354 fdctrl
->data_pos
= 0;
1355 fdctrl
->data_len
= 1; /* Accept command byte, adjust for params later */
1356 fdctrl
->msr
&= ~(FD_MSR_CMDBUSY
| FD_MSR_DIO
);
1357 fdctrl
->msr
|= FD_MSR_RQM
;
1360 /* Update the state to allow the guest to read out the command status.
1361 * @fifo_len is the number of result bytes to be read out. */
1362 static void fdctrl_to_result_phase(FDCtrl
*fdctrl
, int fifo_len
)
1364 fdctrl
->phase
= FD_PHASE_RESULT
;
1365 fdctrl
->data_dir
= FD_DIR_READ
;
1366 fdctrl
->data_len
= fifo_len
;
1367 fdctrl
->data_pos
= 0;
1368 fdctrl
->msr
|= FD_MSR_CMDBUSY
| FD_MSR_RQM
| FD_MSR_DIO
;
1371 /* Set an error: unimplemented/unknown command */
1372 static void fdctrl_unimplemented(FDCtrl
*fdctrl
, int direction
)
1374 qemu_log_mask(LOG_UNIMP
, "fdc: unimplemented command 0x%02x\n",
1376 fdctrl
->fifo
[0] = FD_SR0_INVCMD
;
1377 fdctrl_to_result_phase(fdctrl
, 1);
1380 /* Seek to next sector
1381 * returns 0 when end of track reached (for DBL_SIDES on head 1)
1382 * otherwise returns 1
1384 static int fdctrl_seek_to_next_sect(FDCtrl
*fdctrl
, FDrive
*cur_drv
)
1386 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
1387 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
1388 fd_sector(cur_drv
));
1389 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1391 uint8_t new_head
= cur_drv
->head
;
1392 uint8_t new_track
= cur_drv
->track
;
1393 uint8_t new_sect
= cur_drv
->sect
;
1397 if (new_sect
>= cur_drv
->last_sect
||
1398 new_sect
== fdctrl
->eot
) {
1400 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
1401 if (new_head
== 0 &&
1402 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
1407 fdctrl
->status0
|= FD_SR0_SEEK
;
1408 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0) {
1413 fdctrl
->status0
|= FD_SR0_SEEK
;
1418 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1419 new_head
, new_track
, new_sect
, fd_sector(cur_drv
));
1424 fd_seek(cur_drv
, new_head
, new_track
, new_sect
, 1);
1428 /* Callback for transfer end (stop or abort) */
1429 static void fdctrl_stop_transfer(FDCtrl
*fdctrl
, uint8_t status0
,
1430 uint8_t status1
, uint8_t status2
)
1433 cur_drv
= get_cur_drv(fdctrl
);
1435 fdctrl
->status0
&= ~(FD_SR0_DS0
| FD_SR0_DS1
| FD_SR0_HEAD
);
1436 fdctrl
->status0
|= GET_CUR_DRV(fdctrl
);
1437 if (cur_drv
->head
) {
1438 fdctrl
->status0
|= FD_SR0_HEAD
;
1440 fdctrl
->status0
|= status0
;
1442 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1443 status0
, status1
, status2
, fdctrl
->status0
);
1444 fdctrl
->fifo
[0] = fdctrl
->status0
;
1445 fdctrl
->fifo
[1] = status1
;
1446 fdctrl
->fifo
[2] = status2
;
1447 fdctrl
->fifo
[3] = cur_drv
->track
;
1448 fdctrl
->fifo
[4] = cur_drv
->head
;
1449 fdctrl
->fifo
[5] = cur_drv
->sect
;
1450 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
1451 fdctrl
->data_dir
= FD_DIR_READ
;
1452 if (fdctrl
->dma_chann
!= -1 && !(fdctrl
->msr
& FD_MSR_NONDMA
)) {
1453 IsaDmaClass
*k
= ISADMA_GET_CLASS(fdctrl
->dma
);
1454 k
->release_DREQ(fdctrl
->dma
, fdctrl
->dma_chann
);
1456 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
1457 fdctrl
->msr
&= ~FD_MSR_NONDMA
;
1459 fdctrl_to_result_phase(fdctrl
, 7);
1460 fdctrl_raise_irq(fdctrl
);
1463 /* Prepare a data transfer (either DMA or FIFO) */
1464 static void fdctrl_start_transfer(FDCtrl
*fdctrl
, int direction
)
1469 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1470 cur_drv
= get_cur_drv(fdctrl
);
1471 kt
= fdctrl
->fifo
[2];
1472 kh
= fdctrl
->fifo
[3];
1473 ks
= fdctrl
->fifo
[4];
1474 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1475 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1476 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
,
1477 NUM_SIDES(cur_drv
)));
1478 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1481 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1482 fdctrl
->fifo
[3] = kt
;
1483 fdctrl
->fifo
[4] = kh
;
1484 fdctrl
->fifo
[5] = ks
;
1488 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1489 fdctrl
->fifo
[3] = kt
;
1490 fdctrl
->fifo
[4] = kh
;
1491 fdctrl
->fifo
[5] = ks
;
1494 /* No seek enabled */
1495 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1496 fdctrl
->fifo
[3] = kt
;
1497 fdctrl
->fifo
[4] = kh
;
1498 fdctrl
->fifo
[5] = ks
;
1501 fdctrl
->status0
|= FD_SR0_SEEK
;
1507 /* Check the data rate. If the programmed data rate does not match
1508 * the currently inserted medium, the operation has to fail. */
1509 if ((fdctrl
->dsr
& FD_DSR_DRATEMASK
) != cur_drv
->media_rate
) {
1510 FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n",
1511 fdctrl
->dsr
& FD_DSR_DRATEMASK
, cur_drv
->media_rate
);
1512 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_MA
, 0x00);
1513 fdctrl
->fifo
[3] = kt
;
1514 fdctrl
->fifo
[4] = kh
;
1515 fdctrl
->fifo
[5] = ks
;
1519 /* Set the FIFO state */
1520 fdctrl
->data_dir
= direction
;
1521 fdctrl
->data_pos
= 0;
1522 assert(fdctrl
->msr
& FD_MSR_CMDBUSY
);
1523 if (fdctrl
->fifo
[0] & 0x80)
1524 fdctrl
->data_state
|= FD_STATE_MULTI
;
1526 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1527 if (fdctrl
->fifo
[5] == 0) {
1528 fdctrl
->data_len
= fdctrl
->fifo
[8];
1531 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
1532 tmp
= (fdctrl
->fifo
[6] - ks
+ 1);
1534 FLOPPY_DPRINTF("invalid EOT: %d\n", tmp
);
1535 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_MA
, 0x00);
1536 fdctrl
->fifo
[3] = kt
;
1537 fdctrl
->fifo
[4] = kh
;
1538 fdctrl
->fifo
[5] = ks
;
1541 if (fdctrl
->fifo
[0] & 0x80)
1542 tmp
+= fdctrl
->fifo
[6];
1543 fdctrl
->data_len
*= tmp
;
1545 fdctrl
->eot
= fdctrl
->fifo
[6];
1546 if (fdctrl
->dor
& FD_DOR_DMAEN
) {
1547 /* DMA transfer is enabled. */
1548 IsaDmaClass
*k
= ISADMA_GET_CLASS(fdctrl
->dma
);
1550 FLOPPY_DPRINTF("direction=%d (%d - %d)\n",
1551 direction
, (128 << fdctrl
->fifo
[5]) *
1552 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
1554 /* No access is allowed until DMA transfer has completed */
1555 fdctrl
->msr
&= ~FD_MSR_RQM
;
1556 if (direction
!= FD_DIR_VERIFY
) {
1558 * Now, we just have to wait for the DMA controller to
1561 k
->hold_DREQ(fdctrl
->dma
, fdctrl
->dma_chann
);
1562 k
->schedule(fdctrl
->dma
);
1564 /* Start transfer */
1565 fdctrl_transfer_handler(fdctrl
, fdctrl
->dma_chann
, 0,
1570 FLOPPY_DPRINTF("start non-DMA transfer\n");
1571 fdctrl
->msr
|= FD_MSR_NONDMA
| FD_MSR_RQM
;
1572 if (direction
!= FD_DIR_WRITE
)
1573 fdctrl
->msr
|= FD_MSR_DIO
;
1574 /* IO based transfer: calculate len */
1575 fdctrl_raise_irq(fdctrl
);
1578 /* Prepare a transfer of deleted data */
1579 static void fdctrl_start_transfer_del(FDCtrl
*fdctrl
, int direction
)
1581 qemu_log_mask(LOG_UNIMP
, "fdctrl_start_transfer_del() unimplemented\n");
1583 /* We don't handle deleted data,
1584 * so we don't return *ANYTHING*
1586 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1589 /* handlers for DMA transfers */
1590 int fdctrl_transfer_handler(void *opaque
, int nchan
, int dma_pos
, int dma_len
)
1594 int len
, start_pos
, rel_pos
;
1595 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
1599 if (fdctrl
->msr
& FD_MSR_RQM
) {
1600 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1603 k
= ISADMA_GET_CLASS(fdctrl
->dma
);
1604 cur_drv
= get_cur_drv(fdctrl
);
1605 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
1606 fdctrl
->data_dir
== FD_DIR_SCANH
)
1607 status2
= FD_SR2_SNS
;
1608 if (dma_len
> fdctrl
->data_len
)
1609 dma_len
= fdctrl
->data_len
;
1610 if (cur_drv
->blk
== NULL
) {
1611 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
1612 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1614 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1616 goto transfer_error
;
1618 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1619 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
1620 len
= dma_len
- fdctrl
->data_pos
;
1621 if (len
+ rel_pos
> FD_SECTOR_LEN
)
1622 len
= FD_SECTOR_LEN
- rel_pos
;
1623 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1624 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
1625 fdctrl
->data_len
, GET_CUR_DRV(fdctrl
), cur_drv
->head
,
1626 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
1627 fd_sector(cur_drv
) * FD_SECTOR_LEN
);
1628 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
1629 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
1630 /* READ & SCAN commands and realign to a sector for WRITE */
1631 if (blk_pread(cur_drv
->blk
, fd_offset(cur_drv
), BDRV_SECTOR_SIZE
,
1632 fdctrl
->fifo
, 0) < 0) {
1633 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1634 fd_sector(cur_drv
));
1635 /* Sure, image size is too small... */
1636 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1639 switch (fdctrl
->data_dir
) {
1642 k
->write_memory(fdctrl
->dma
, nchan
, fdctrl
->fifo
+ rel_pos
,
1643 fdctrl
->data_pos
, len
);
1646 /* WRITE commands */
1648 /* Handle readonly medium early, no need to do DMA, touch the
1649 * LED or attempt any writes. A real floppy doesn't attempt
1650 * to write to readonly media either. */
1651 fdctrl_stop_transfer(fdctrl
,
1652 FD_SR0_ABNTERM
| FD_SR0_SEEK
, FD_SR1_NW
,
1654 goto transfer_error
;
1657 k
->read_memory(fdctrl
->dma
, nchan
, fdctrl
->fifo
+ rel_pos
,
1658 fdctrl
->data_pos
, len
);
1659 if (blk_pwrite(cur_drv
->blk
, fd_offset(cur_drv
), BDRV_SECTOR_SIZE
,
1660 fdctrl
->fifo
, 0) < 0) {
1661 FLOPPY_DPRINTF("error writing sector %d\n",
1662 fd_sector(cur_drv
));
1663 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1664 goto transfer_error
;
1668 /* VERIFY commands */
1673 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1675 k
->read_memory(fdctrl
->dma
, nchan
, tmpbuf
, fdctrl
->data_pos
,
1677 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1679 status2
= FD_SR2_SEH
;
1682 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1683 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1690 fdctrl
->data_pos
+= len
;
1691 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1693 /* Seek to next sector */
1694 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
))
1699 len
= fdctrl
->data_pos
- start_pos
;
1700 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1701 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1702 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1703 fdctrl
->data_dir
== FD_DIR_SCANL
||
1704 fdctrl
->data_dir
== FD_DIR_SCANH
)
1705 status2
= FD_SR2_SEH
;
1706 fdctrl
->data_len
-= len
;
1707 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1713 /* Data register : 0x05 */
1714 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
)
1717 uint32_t retval
= 0;
1720 cur_drv
= get_cur_drv(fdctrl
);
1721 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1722 if (!(fdctrl
->msr
& FD_MSR_RQM
) || !(fdctrl
->msr
& FD_MSR_DIO
)) {
1723 FLOPPY_DPRINTF("error: controller not ready for reading\n");
1727 /* If data_len spans multiple sectors, the current position in the FIFO
1728 * wraps around while fdctrl->data_pos is the real position in the whole
1730 pos
= fdctrl
->data_pos
;
1731 pos
%= FD_SECTOR_LEN
;
1733 switch (fdctrl
->phase
) {
1734 case FD_PHASE_EXECUTION
:
1735 assert(fdctrl
->msr
& FD_MSR_NONDMA
);
1737 if (fdctrl
->data_pos
!= 0)
1738 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1739 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1740 fd_sector(cur_drv
));
1743 if (blk_pread(cur_drv
->blk
, fd_offset(cur_drv
), BDRV_SECTOR_SIZE
,
1746 FLOPPY_DPRINTF("error getting sector %d\n",
1747 fd_sector(cur_drv
));
1748 /* Sure, image size is too small... */
1749 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1753 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1754 fdctrl
->msr
&= ~FD_MSR_RQM
;
1755 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1759 case FD_PHASE_RESULT
:
1760 assert(!(fdctrl
->msr
& FD_MSR_NONDMA
));
1761 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1762 fdctrl
->msr
&= ~FD_MSR_RQM
;
1763 fdctrl_to_command_phase(fdctrl
);
1764 fdctrl_reset_irq(fdctrl
);
1768 case FD_PHASE_COMMAND
:
1773 retval
= fdctrl
->fifo
[pos
];
1774 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1779 static void fdctrl_format_sector(FDCtrl
*fdctrl
)
1784 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1785 cur_drv
= get_cur_drv(fdctrl
);
1786 kt
= fdctrl
->fifo
[6];
1787 kh
= fdctrl
->fifo
[7];
1788 ks
= fdctrl
->fifo
[8];
1789 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1790 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1791 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
,
1792 NUM_SIDES(cur_drv
)));
1793 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1796 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1797 fdctrl
->fifo
[3] = kt
;
1798 fdctrl
->fifo
[4] = kh
;
1799 fdctrl
->fifo
[5] = ks
;
1803 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1804 fdctrl
->fifo
[3] = kt
;
1805 fdctrl
->fifo
[4] = kh
;
1806 fdctrl
->fifo
[5] = ks
;
1809 /* No seek enabled */
1810 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1811 fdctrl
->fifo
[3] = kt
;
1812 fdctrl
->fifo
[4] = kh
;
1813 fdctrl
->fifo
[5] = ks
;
1816 fdctrl
->status0
|= FD_SR0_SEEK
;
1821 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1822 if (cur_drv
->blk
== NULL
||
1823 blk_pwrite(cur_drv
->blk
, fd_offset(cur_drv
), BDRV_SECTOR_SIZE
,
1824 fdctrl
->fifo
, 0) < 0) {
1825 FLOPPY_DPRINTF("error formatting sector %d\n", fd_sector(cur_drv
));
1826 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1828 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1829 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1830 /* Last sector done */
1831 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1834 fdctrl
->data_pos
= 0;
1835 fdctrl
->data_len
= 4;
1840 static void fdctrl_handle_lock(FDCtrl
*fdctrl
, int direction
)
1842 fdctrl
->lock
= (fdctrl
->fifo
[0] & 0x80) ? 1 : 0;
1843 fdctrl
->fifo
[0] = fdctrl
->lock
<< 4;
1844 fdctrl_to_result_phase(fdctrl
, 1);
1847 static void fdctrl_handle_dumpreg(FDCtrl
*fdctrl
, int direction
)
1849 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1851 /* Drives position */
1852 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1853 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1855 fdctrl
->fifo
[2] = drv2(fdctrl
)->track
;
1856 fdctrl
->fifo
[3] = drv3(fdctrl
)->track
;
1858 fdctrl
->fifo
[2] = 0;
1859 fdctrl
->fifo
[3] = 0;
1862 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1863 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | (fdctrl
->dor
& FD_DOR_DMAEN
? 1 : 0);
1864 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1865 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1866 (cur_drv
->perpendicular
<< 2);
1867 fdctrl
->fifo
[8] = fdctrl
->config
;
1868 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1869 fdctrl_to_result_phase(fdctrl
, 10);
1872 static void fdctrl_handle_version(FDCtrl
*fdctrl
, int direction
)
1874 /* Controller's version */
1875 fdctrl
->fifo
[0] = fdctrl
->version
;
1876 fdctrl_to_result_phase(fdctrl
, 1);
1879 static void fdctrl_handle_partid(FDCtrl
*fdctrl
, int direction
)
1881 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1882 fdctrl_to_result_phase(fdctrl
, 1);
1885 static void fdctrl_handle_restore(FDCtrl
*fdctrl
, int direction
)
1887 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1889 /* Drives position */
1890 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1891 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1893 drv2(fdctrl
)->track
= fdctrl
->fifo
[5];
1894 drv3(fdctrl
)->track
= fdctrl
->fifo
[6];
1897 fdctrl
->timer0
= fdctrl
->fifo
[7];
1898 fdctrl
->timer1
= fdctrl
->fifo
[8];
1899 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1900 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1901 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1902 fdctrl
->config
= fdctrl
->fifo
[11];
1903 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1904 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1905 fdctrl_to_command_phase(fdctrl
);
1908 static void fdctrl_handle_save(FDCtrl
*fdctrl
, int direction
)
1910 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1912 fdctrl
->fifo
[0] = 0;
1913 fdctrl
->fifo
[1] = 0;
1914 /* Drives position */
1915 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1916 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1918 fdctrl
->fifo
[4] = drv2(fdctrl
)->track
;
1919 fdctrl
->fifo
[5] = drv3(fdctrl
)->track
;
1921 fdctrl
->fifo
[4] = 0;
1922 fdctrl
->fifo
[5] = 0;
1925 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1926 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1927 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1928 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1929 (cur_drv
->perpendicular
<< 2);
1930 fdctrl
->fifo
[10] = fdctrl
->config
;
1931 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1932 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1933 fdctrl
->fifo
[13] = 0;
1934 fdctrl
->fifo
[14] = 0;
1935 fdctrl_to_result_phase(fdctrl
, 15);
1938 static void fdctrl_handle_readid(FDCtrl
*fdctrl
, int direction
)
1940 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1942 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1943 timer_mod(fdctrl
->result_timer
, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL
) +
1944 (NANOSECONDS_PER_SECOND
/ 50));
1947 static void fdctrl_handle_format_track(FDCtrl
*fdctrl
, int direction
)
1951 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1952 cur_drv
= get_cur_drv(fdctrl
);
1953 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1954 if (fdctrl
->fifo
[0] & 0x80)
1955 fdctrl
->data_state
|= FD_STATE_MULTI
;
1957 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1959 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1961 cur_drv
->last_sect
=
1962 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1963 fdctrl
->fifo
[3] / 2;
1965 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1967 /* TODO: implement format using DMA expected by the Bochs BIOS
1968 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1969 * the sector with the specified fill byte
1971 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1972 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1975 static void fdctrl_handle_specify(FDCtrl
*fdctrl
, int direction
)
1977 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1978 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1979 if (fdctrl
->fifo
[2] & 1)
1980 fdctrl
->dor
&= ~FD_DOR_DMAEN
;
1982 fdctrl
->dor
|= FD_DOR_DMAEN
;
1983 /* No result back */
1984 fdctrl_to_command_phase(fdctrl
);
1987 static void fdctrl_handle_sense_drive_status(FDCtrl
*fdctrl
, int direction
)
1991 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1992 cur_drv
= get_cur_drv(fdctrl
);
1993 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1994 /* 1 Byte status back */
1995 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1996 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1997 (cur_drv
->head
<< 2) |
1998 GET_CUR_DRV(fdctrl
) |
2000 fdctrl_to_result_phase(fdctrl
, 1);
2003 static void fdctrl_handle_recalibrate(FDCtrl
*fdctrl
, int direction
)
2007 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
2008 cur_drv
= get_cur_drv(fdctrl
);
2009 fd_recalibrate(cur_drv
);
2010 fdctrl_to_command_phase(fdctrl
);
2011 /* Raise Interrupt */
2012 fdctrl
->status0
|= FD_SR0_SEEK
;
2013 fdctrl_raise_irq(fdctrl
);
2016 static void fdctrl_handle_sense_interrupt_status(FDCtrl
*fdctrl
, int direction
)
2018 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
2020 if (fdctrl
->reset_sensei
> 0) {
2022 FD_SR0_RDYCHG
+ FD_RESET_SENSEI_COUNT
- fdctrl
->reset_sensei
;
2023 fdctrl
->reset_sensei
--;
2024 } else if (!(fdctrl
->sra
& FD_SRA_INTPEND
)) {
2025 fdctrl
->fifo
[0] = FD_SR0_INVCMD
;
2026 fdctrl_to_result_phase(fdctrl
, 1);
2030 (fdctrl
->status0
& ~(FD_SR0_HEAD
| FD_SR0_DS1
| FD_SR0_DS0
))
2031 | GET_CUR_DRV(fdctrl
);
2034 fdctrl
->fifo
[1] = cur_drv
->track
;
2035 fdctrl_to_result_phase(fdctrl
, 2);
2036 fdctrl_reset_irq(fdctrl
);
2037 fdctrl
->status0
= FD_SR0_RDYCHG
;
2040 static void fdctrl_handle_seek(FDCtrl
*fdctrl
, int direction
)
2044 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
2045 cur_drv
= get_cur_drv(fdctrl
);
2046 fdctrl_to_command_phase(fdctrl
);
2047 /* The seek command just sends step pulses to the drive and doesn't care if
2048 * there is a medium inserted of if it's banging the head against the drive.
2050 fd_seek(cur_drv
, cur_drv
->head
, fdctrl
->fifo
[2], cur_drv
->sect
, 1);
2051 /* Raise Interrupt */
2052 fdctrl
->status0
|= FD_SR0_SEEK
;
2053 fdctrl_raise_irq(fdctrl
);
2056 static void fdctrl_handle_perpendicular_mode(FDCtrl
*fdctrl
, int direction
)
2058 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
2060 if (fdctrl
->fifo
[1] & 0x80)
2061 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
2062 /* No result back */
2063 fdctrl_to_command_phase(fdctrl
);
2066 static void fdctrl_handle_configure(FDCtrl
*fdctrl
, int direction
)
2068 fdctrl
->config
= fdctrl
->fifo
[2];
2069 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
2070 /* No result back */
2071 fdctrl_to_command_phase(fdctrl
);
2074 static void fdctrl_handle_powerdown_mode(FDCtrl
*fdctrl
, int direction
)
2076 fdctrl
->pwrd
= fdctrl
->fifo
[1];
2077 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
2078 fdctrl_to_result_phase(fdctrl
, 1);
2081 static void fdctrl_handle_option(FDCtrl
*fdctrl
, int direction
)
2083 /* No result back */
2084 fdctrl_to_command_phase(fdctrl
);
2087 static void fdctrl_handle_drive_specification_command(FDCtrl
*fdctrl
, int direction
)
2089 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
2092 pos
= fdctrl
->data_pos
- 1;
2093 pos
%= FD_SECTOR_LEN
;
2094 if (fdctrl
->fifo
[pos
] & 0x80) {
2095 /* Command parameters done */
2096 if (fdctrl
->fifo
[pos
] & 0x40) {
2097 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
2098 fdctrl
->fifo
[2] = 0;
2099 fdctrl
->fifo
[3] = 0;
2100 fdctrl_to_result_phase(fdctrl
, 4);
2102 fdctrl_to_command_phase(fdctrl
);
2104 } else if (fdctrl
->data_len
> 7) {
2106 fdctrl
->fifo
[0] = 0x80 |
2107 (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
2108 fdctrl_to_result_phase(fdctrl
, 1);
2112 static void fdctrl_handle_relative_seek_in(FDCtrl
*fdctrl
, int direction
)
2116 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
2117 cur_drv
= get_cur_drv(fdctrl
);
2118 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
2119 fd_seek(cur_drv
, cur_drv
->head
, cur_drv
->max_track
- 1,
2122 fd_seek(cur_drv
, cur_drv
->head
,
2123 cur_drv
->track
+ fdctrl
->fifo
[2], cur_drv
->sect
, 1);
2125 fdctrl_to_command_phase(fdctrl
);
2126 /* Raise Interrupt */
2127 fdctrl
->status0
|= FD_SR0_SEEK
;
2128 fdctrl_raise_irq(fdctrl
);
2131 static void fdctrl_handle_relative_seek_out(FDCtrl
*fdctrl
, int direction
)
2135 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
2136 cur_drv
= get_cur_drv(fdctrl
);
2137 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
2138 fd_seek(cur_drv
, cur_drv
->head
, 0, cur_drv
->sect
, 1);
2140 fd_seek(cur_drv
, cur_drv
->head
,
2141 cur_drv
->track
- fdctrl
->fifo
[2], cur_drv
->sect
, 1);
2143 fdctrl_to_command_phase(fdctrl
);
2144 /* Raise Interrupt */
2145 fdctrl
->status0
|= FD_SR0_SEEK
;
2146 fdctrl_raise_irq(fdctrl
);
2150 * Handlers for the execution phase of each command
2152 typedef struct FDCtrlCommand
{
2157 void (*handler
)(FDCtrl
*fdctrl
, int direction
);
2161 static const FDCtrlCommand handlers
[] = {
2162 { FD_CMD_READ
, 0x1f, "READ", 8, fdctrl_start_transfer
, FD_DIR_READ
},
2163 { FD_CMD_WRITE
, 0x3f, "WRITE", 8, fdctrl_start_transfer
, FD_DIR_WRITE
},
2164 { FD_CMD_SEEK
, 0xff, "SEEK", 2, fdctrl_handle_seek
},
2165 { FD_CMD_SENSE_INTERRUPT_STATUS
, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status
},
2166 { FD_CMD_RECALIBRATE
, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate
},
2167 { FD_CMD_FORMAT_TRACK
, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track
},
2168 { FD_CMD_READ_TRACK
, 0xbf, "READ TRACK", 8, fdctrl_start_transfer
, FD_DIR_READ
},
2169 { FD_CMD_RESTORE
, 0xff, "RESTORE", 17, fdctrl_handle_restore
}, /* part of READ DELETED DATA */
2170 { FD_CMD_SAVE
, 0xff, "SAVE", 0, fdctrl_handle_save
}, /* part of READ DELETED DATA */
2171 { FD_CMD_READ_DELETED
, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_READ
},
2172 { FD_CMD_SCAN_EQUAL
, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANE
},
2173 { FD_CMD_VERIFY
, 0x1f, "VERIFY", 8, fdctrl_start_transfer
, FD_DIR_VERIFY
},
2174 { FD_CMD_SCAN_LOW_OR_EQUAL
, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANL
},
2175 { FD_CMD_SCAN_HIGH_OR_EQUAL
, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANH
},
2176 { FD_CMD_WRITE_DELETED
, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_WRITE
},
2177 { FD_CMD_READ_ID
, 0xbf, "READ ID", 1, fdctrl_handle_readid
},
2178 { FD_CMD_SPECIFY
, 0xff, "SPECIFY", 2, fdctrl_handle_specify
},
2179 { FD_CMD_SENSE_DRIVE_STATUS
, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status
},
2180 { FD_CMD_PERPENDICULAR_MODE
, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode
},
2181 { FD_CMD_CONFIGURE
, 0xff, "CONFIGURE", 3, fdctrl_handle_configure
},
2182 { FD_CMD_POWERDOWN_MODE
, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode
},
2183 { FD_CMD_OPTION
, 0xff, "OPTION", 1, fdctrl_handle_option
},
2184 { FD_CMD_DRIVE_SPECIFICATION_COMMAND
, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command
},
2185 { FD_CMD_RELATIVE_SEEK_OUT
, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out
},
2186 { FD_CMD_FORMAT_AND_WRITE
, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented
},
2187 { FD_CMD_RELATIVE_SEEK_IN
, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in
},
2188 { FD_CMD_LOCK
, 0x7f, "LOCK", 0, fdctrl_handle_lock
},
2189 { FD_CMD_DUMPREG
, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg
},
2190 { FD_CMD_VERSION
, 0xff, "VERSION", 0, fdctrl_handle_version
},
2191 { FD_CMD_PART_ID
, 0xff, "PART ID", 0, fdctrl_handle_partid
},
2192 { FD_CMD_WRITE
, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer
, FD_DIR_WRITE
}, /* not in specification ; BeOS 4.5 bug */
2193 { 0, 0, "unknown", 0, fdctrl_unimplemented
}, /* default handler */
2195 /* Associate command to an index in the 'handlers' array */
2196 static uint8_t command_to_handler
[256];
2198 static const FDCtrlCommand
*get_command(uint8_t cmd
)
2202 idx
= command_to_handler
[cmd
];
2203 FLOPPY_DPRINTF("%s command\n", handlers
[idx
].name
);
2204 return &handlers
[idx
];
2207 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
)
2210 const FDCtrlCommand
*cmd
;
2214 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
2215 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
2218 if (!(fdctrl
->msr
& FD_MSR_RQM
) || (fdctrl
->msr
& FD_MSR_DIO
)) {
2219 FLOPPY_DPRINTF("error: controller not ready for writing\n");
2222 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
2224 FLOPPY_DPRINTF("%s: %02x\n", __func__
, value
);
2226 /* If data_len spans multiple sectors, the current position in the FIFO
2227 * wraps around while fdctrl->data_pos is the real position in the whole
2229 pos
= fdctrl
->data_pos
++;
2230 pos
%= FD_SECTOR_LEN
;
2231 fdctrl
->fifo
[pos
] = value
;
2233 if (fdctrl
->data_pos
== fdctrl
->data_len
) {
2234 fdctrl
->msr
&= ~FD_MSR_RQM
;
2237 switch (fdctrl
->phase
) {
2238 case FD_PHASE_EXECUTION
:
2239 /* For DMA requests, RQM should be cleared during execution phase, so
2240 * we would have errored out above. */
2241 assert(fdctrl
->msr
& FD_MSR_NONDMA
);
2243 /* FIFO data write */
2244 if (pos
== FD_SECTOR_LEN
- 1 ||
2245 fdctrl
->data_pos
== fdctrl
->data_len
) {
2246 cur_drv
= get_cur_drv(fdctrl
);
2247 if (blk_pwrite(cur_drv
->blk
, fd_offset(cur_drv
), BDRV_SECTOR_SIZE
,
2248 fdctrl
->fifo
, 0) < 0) {
2249 FLOPPY_DPRINTF("error writing sector %d\n",
2250 fd_sector(cur_drv
));
2253 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
2254 FLOPPY_DPRINTF("error seeking to next sector %d\n",
2255 fd_sector(cur_drv
));
2260 /* Switch to result phase when done with the transfer */
2261 if (fdctrl
->data_pos
== fdctrl
->data_len
) {
2262 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
2266 case FD_PHASE_COMMAND
:
2267 assert(!(fdctrl
->msr
& FD_MSR_NONDMA
));
2268 assert(fdctrl
->data_pos
< FD_SECTOR_LEN
);
2271 /* The first byte specifies the command. Now we start reading
2272 * as many parameters as this command requires. */
2273 cmd
= get_command(value
);
2274 fdctrl
->data_len
= cmd
->parameters
+ 1;
2275 if (cmd
->parameters
) {
2276 fdctrl
->msr
|= FD_MSR_RQM
;
2278 fdctrl
->msr
|= FD_MSR_CMDBUSY
;
2281 if (fdctrl
->data_pos
== fdctrl
->data_len
) {
2282 /* We have all parameters now, execute the command */
2283 fdctrl
->phase
= FD_PHASE_EXECUTION
;
2285 if (fdctrl
->data_state
& FD_STATE_FORMAT
) {
2286 fdctrl_format_sector(fdctrl
);
2290 cmd
= get_command(fdctrl
->fifo
[0]);
2291 FLOPPY_DPRINTF("Calling handler for '%s'\n", cmd
->name
);
2292 cmd
->handler(fdctrl
, cmd
->direction
);
2296 case FD_PHASE_RESULT
:
2302 static void fdctrl_result_timer(void *opaque
)
2304 FDCtrl
*fdctrl
= opaque
;
2305 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
2307 /* Pretend we are spinning.
2308 * This is needed for Coherent, which uses READ ID to check for
2309 * sector interleaving.
2311 if (cur_drv
->last_sect
!= 0) {
2312 cur_drv
->sect
= (cur_drv
->sect
% cur_drv
->last_sect
) + 1;
2314 /* READ_ID can't automatically succeed! */
2315 if ((fdctrl
->dsr
& FD_DSR_DRATEMASK
) != cur_drv
->media_rate
) {
2316 FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n",
2317 fdctrl
->dsr
& FD_DSR_DRATEMASK
, cur_drv
->media_rate
);
2318 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_MA
, 0x00);
2320 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
2324 /* Init functions */
2326 void fdctrl_init_drives(FloppyBus
*bus
, DriveInfo
**fds
)
2331 for (i
= 0; i
< MAX_FD
; i
++) {
2333 dev
= qdev_new("floppy");
2334 qdev_prop_set_uint32(dev
, "unit", i
);
2335 qdev_prop_set_enum(dev
, "drive-type", FLOPPY_DRIVE_TYPE_AUTO
);
2336 qdev_prop_set_drive_err(dev
, "drive", blk_by_legacy_dinfo(fds
[i
]),
2338 qdev_realize_and_unref(dev
, &bus
->bus
, &error_fatal
);
2343 void fdctrl_realize_common(DeviceState
*dev
, FDCtrl
*fdctrl
, Error
**errp
)
2347 static int command_tables_inited
= 0;
2349 if (fdctrl
->fallback
== FLOPPY_DRIVE_TYPE_AUTO
) {
2350 error_setg(errp
, "Cannot choose a fallback FDrive type of 'auto'");
2354 /* Fill 'command_to_handler' lookup table */
2355 if (!command_tables_inited
) {
2356 command_tables_inited
= 1;
2357 for (i
= ARRAY_SIZE(handlers
) - 1; i
>= 0; i
--) {
2358 for (j
= 0; j
< sizeof(command_to_handler
); j
++) {
2359 if ((j
& handlers
[i
].mask
) == handlers
[i
].value
) {
2360 command_to_handler
[j
] = i
;
2366 FLOPPY_DPRINTF("init controller\n");
2367 fdctrl
->fifo
= qemu_memalign(512, FD_SECTOR_LEN
);
2368 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
2369 fdctrl
->fifo_size
= 512;
2370 fdctrl
->result_timer
= timer_new_ns(QEMU_CLOCK_VIRTUAL
,
2371 fdctrl_result_timer
, fdctrl
);
2373 fdctrl
->version
= 0x90; /* Intel 82078 controller */
2374 fdctrl
->config
= FD_CONFIG_EIS
| FD_CONFIG_EFIFO
; /* Implicit seek, polling & FIFO enabled */
2375 fdctrl
->num_floppies
= MAX_FD
;
2377 floppy_bus_create(fdctrl
, &fdctrl
->bus
, dev
);
2379 for (i
= 0; i
< MAX_FD
; i
++) {
2380 drive
= &fdctrl
->drives
[i
];
2381 drive
->fdctrl
= fdctrl
;
2383 fd_revalidate(drive
);
2387 static void fdc_register_types(void)
2389 type_register_static(&floppy_bus_info
);
2390 type_register_static(&floppy_drive_info
);
2393 type_init(fdc_register_types
)