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.
32 #include "qemu-error.h"
33 #include "qemu-timer.h"
36 #include "qdev-addr.h"
40 /********************************************************/
41 /* debug Floppy devices */
42 //#define DEBUG_FLOPPY
45 #define FLOPPY_DPRINTF(fmt, ...) \
46 do { printf("FLOPPY: " fmt , ## __VA_ARGS__); } while (0)
48 #define FLOPPY_DPRINTF(fmt, ...)
51 #define FLOPPY_ERROR(fmt, ...) \
52 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
54 /********************************************************/
55 /* Floppy drive emulation */
57 #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
58 #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))
60 /* Will always be a fixed parameter for us */
61 #define FD_SECTOR_LEN 512
62 #define FD_SECTOR_SC 2 /* Sector size code */
63 #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */
65 /* Floppy disk drive emulation */
66 typedef enum FDiskFlags
{
67 FDISK_DBL_SIDES
= 0x01,
70 typedef struct FDrive
{
74 uint8_t perpendicular
; /* 2.88 MB access mode */
81 uint8_t last_sect
; /* Nb sector per track */
82 uint8_t max_track
; /* Nb of tracks */
83 uint16_t bps
; /* Bytes per sector */
84 uint8_t ro
; /* Is read-only */
85 uint8_t media_changed
; /* Is media changed */
88 static void fd_init(FDrive
*drv
)
91 drv
->drive
= FDRIVE_DRV_NONE
;
92 drv
->perpendicular
= 0;
98 static int fd_sector_calc(uint8_t head
, uint8_t track
, uint8_t sect
,
101 return (((track
* 2) + head
) * last_sect
) + sect
- 1;
104 /* Returns current position, in sectors, for given drive */
105 static int fd_sector(FDrive
*drv
)
107 return fd_sector_calc(drv
->head
, drv
->track
, drv
->sect
, drv
->last_sect
);
110 /* Seek to a new position:
111 * returns 0 if already on right track
112 * returns 1 if track changed
113 * returns 2 if track is invalid
114 * returns 3 if sector is invalid
115 * returns 4 if seek is disabled
117 static int fd_seek(FDrive
*drv
, uint8_t head
, uint8_t track
, uint8_t sect
,
123 if (track
> drv
->max_track
||
124 (head
!= 0 && (drv
->flags
& FDISK_DBL_SIDES
) == 0)) {
125 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
126 head
, track
, sect
, 1,
127 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
128 drv
->max_track
, drv
->last_sect
);
131 if (sect
> drv
->last_sect
) {
132 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
133 head
, track
, sect
, 1,
134 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
135 drv
->max_track
, drv
->last_sect
);
138 sector
= fd_sector_calc(head
, track
, sect
, drv
->last_sect
);
140 if (sector
!= fd_sector(drv
)) {
143 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
144 head
, track
, sect
, 1, drv
->max_track
, drv
->last_sect
);
149 if (drv
->track
!= track
)
158 /* Set drive back to track 0 */
159 static void fd_recalibrate(FDrive
*drv
)
161 FLOPPY_DPRINTF("recalibrate\n");
167 /* Revalidate a disk drive after a disk change */
168 static void fd_revalidate(FDrive
*drv
)
170 int nb_heads
, max_track
, last_sect
, ro
;
173 FLOPPY_DPRINTF("revalidate\n");
174 if (drv
->bs
!= NULL
&& bdrv_is_inserted(drv
->bs
)) {
175 ro
= bdrv_is_read_only(drv
->bs
);
176 bdrv_get_floppy_geometry_hint(drv
->bs
, &nb_heads
, &max_track
,
177 &last_sect
, drv
->drive
, &drive
);
178 if (nb_heads
!= 0 && max_track
!= 0 && last_sect
!= 0) {
179 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
180 nb_heads
- 1, max_track
, last_sect
);
182 FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", nb_heads
,
183 max_track
, last_sect
, ro
? "ro" : "rw");
186 drv
->flags
&= ~FDISK_DBL_SIDES
;
188 drv
->flags
|= FDISK_DBL_SIDES
;
190 drv
->max_track
= max_track
;
191 drv
->last_sect
= last_sect
;
195 FLOPPY_DPRINTF("No disk in drive\n");
198 drv
->flags
&= ~FDISK_DBL_SIDES
;
202 /********************************************************/
203 /* Intel 82078 floppy disk controller emulation */
205 typedef struct FDCtrl FDCtrl
;
207 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
);
208 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
);
209 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
210 int dma_pos
, int dma_len
);
211 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
);
213 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
);
214 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
);
215 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
);
216 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
);
217 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
);
218 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
);
219 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
);
220 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
);
221 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
);
222 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
);
223 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
);
234 FD_STATE_MULTI
= 0x01, /* multi track flag */
235 FD_STATE_FORMAT
= 0x02, /* format flag */
236 FD_STATE_SEEK
= 0x04, /* seek flag */
251 FD_CMD_READ_TRACK
= 0x02,
252 FD_CMD_SPECIFY
= 0x03,
253 FD_CMD_SENSE_DRIVE_STATUS
= 0x04,
256 FD_CMD_RECALIBRATE
= 0x07,
257 FD_CMD_SENSE_INTERRUPT_STATUS
= 0x08,
258 FD_CMD_WRITE_DELETED
= 0x09,
259 FD_CMD_READ_ID
= 0x0a,
260 FD_CMD_READ_DELETED
= 0x0c,
261 FD_CMD_FORMAT_TRACK
= 0x0d,
262 FD_CMD_DUMPREG
= 0x0e,
264 FD_CMD_VERSION
= 0x10,
265 FD_CMD_SCAN_EQUAL
= 0x11,
266 FD_CMD_PERPENDICULAR_MODE
= 0x12,
267 FD_CMD_CONFIGURE
= 0x13,
269 FD_CMD_VERIFY
= 0x16,
270 FD_CMD_POWERDOWN_MODE
= 0x17,
271 FD_CMD_PART_ID
= 0x18,
272 FD_CMD_SCAN_LOW_OR_EQUAL
= 0x19,
273 FD_CMD_SCAN_HIGH_OR_EQUAL
= 0x1d,
275 FD_CMD_OPTION
= 0x33,
276 FD_CMD_RESTORE
= 0x4e,
277 FD_CMD_DRIVE_SPECIFICATION_COMMAND
= 0x8e,
278 FD_CMD_RELATIVE_SEEK_OUT
= 0x8f,
279 FD_CMD_FORMAT_AND_WRITE
= 0xcd,
280 FD_CMD_RELATIVE_SEEK_IN
= 0xcf,
284 FD_CONFIG_PRETRK
= 0xff, /* Pre-compensation set to track 0 */
285 FD_CONFIG_FIFOTHR
= 0x0f, /* FIFO threshold set to 1 byte */
286 FD_CONFIG_POLL
= 0x10, /* Poll enabled */
287 FD_CONFIG_EFIFO
= 0x20, /* FIFO disabled */
288 FD_CONFIG_EIS
= 0x40, /* No implied seeks */
294 FD_SR0_ABNTERM
= 0x40,
295 FD_SR0_INVCMD
= 0x80,
296 FD_SR0_RDYCHG
= 0xc0,
300 FD_SR1_EC
= 0x80, /* End of cylinder */
304 FD_SR2_SNS
= 0x04, /* Scan not satisfied */
305 FD_SR2_SEH
= 0x08, /* Scan equal hit */
316 FD_SRA_INTPEND
= 0x80,
330 FD_DOR_SELMASK
= 0x03,
332 FD_DOR_SELMASK
= 0x01,
334 FD_DOR_nRESET
= 0x04,
336 FD_DOR_MOTEN0
= 0x10,
337 FD_DOR_MOTEN1
= 0x20,
338 FD_DOR_MOTEN2
= 0x40,
339 FD_DOR_MOTEN3
= 0x80,
344 FD_TDR_BOOTSEL
= 0x0c,
346 FD_TDR_BOOTSEL
= 0x04,
351 FD_DSR_DRATEMASK
= 0x03,
352 FD_DSR_PWRDOWN
= 0x40,
353 FD_DSR_SWRESET
= 0x80,
357 FD_MSR_DRV0BUSY
= 0x01,
358 FD_MSR_DRV1BUSY
= 0x02,
359 FD_MSR_DRV2BUSY
= 0x04,
360 FD_MSR_DRV3BUSY
= 0x08,
361 FD_MSR_CMDBUSY
= 0x10,
362 FD_MSR_NONDMA
= 0x20,
368 FD_DIR_DSKCHG
= 0x80,
371 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
372 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
373 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
378 /* Controller state */
379 QEMUTimer
*result_timer
;
381 /* Controller's identification */
387 uint8_t dor_vmstate
; /* only used as temp during vmstate */
402 uint8_t eot
; /* last wanted sector */
403 /* States kept only to be returned back */
404 /* precompensation */
408 /* Power down config (also with status regB access mode */
411 uint8_t num_floppies
;
414 FDrive drives
[MAX_FD
];
421 typedef struct FDCtrlSysBus
{
426 typedef struct FDCtrlISABus
{
433 static uint32_t fdctrl_read (void *opaque
, uint32_t reg
)
435 FDCtrl
*fdctrl
= opaque
;
441 retval
= fdctrl_read_statusA(fdctrl
);
444 retval
= fdctrl_read_statusB(fdctrl
);
447 retval
= fdctrl_read_dor(fdctrl
);
450 retval
= fdctrl_read_tape(fdctrl
);
453 retval
= fdctrl_read_main_status(fdctrl
);
456 retval
= fdctrl_read_data(fdctrl
);
459 retval
= fdctrl_read_dir(fdctrl
);
462 retval
= (uint32_t)(-1);
465 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg
& 7, retval
);
470 static void fdctrl_write (void *opaque
, uint32_t reg
, uint32_t value
)
472 FDCtrl
*fdctrl
= opaque
;
474 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg
& 7, value
);
479 fdctrl_write_dor(fdctrl
, value
);
482 fdctrl_write_tape(fdctrl
, value
);
485 fdctrl_write_rate(fdctrl
, value
);
488 fdctrl_write_data(fdctrl
, value
);
495 static uint64_t fdctrl_read_mem (void *opaque
, target_phys_addr_t reg
,
498 return fdctrl_read(opaque
, (uint32_t)reg
);
501 static void fdctrl_write_mem (void *opaque
, target_phys_addr_t reg
,
502 uint64_t value
, unsigned size
)
504 fdctrl_write(opaque
, (uint32_t)reg
, value
);
507 static const MemoryRegionOps fdctrl_mem_ops
= {
508 .read
= fdctrl_read_mem
,
509 .write
= fdctrl_write_mem
,
510 .endianness
= DEVICE_NATIVE_ENDIAN
,
513 static const MemoryRegionOps fdctrl_mem_strict_ops
= {
514 .read
= fdctrl_read_mem
,
515 .write
= fdctrl_write_mem
,
516 .endianness
= DEVICE_NATIVE_ENDIAN
,
518 .min_access_size
= 1,
519 .max_access_size
= 1,
523 static bool fdrive_media_changed_needed(void *opaque
)
525 FDrive
*drive
= opaque
;
527 return (drive
->bs
!= NULL
&& drive
->media_changed
!= 1);
530 static const VMStateDescription vmstate_fdrive_media_changed
= {
531 .name
= "fdrive/media_changed",
533 .minimum_version_id
= 1,
534 .minimum_version_id_old
= 1,
535 .fields
= (VMStateField
[]) {
536 VMSTATE_UINT8(media_changed
, FDrive
),
537 VMSTATE_END_OF_LIST()
541 static const VMStateDescription vmstate_fdrive
= {
544 .minimum_version_id
= 1,
545 .minimum_version_id_old
= 1,
546 .fields
= (VMStateField
[]) {
547 VMSTATE_UINT8(head
, FDrive
),
548 VMSTATE_UINT8(track
, FDrive
),
549 VMSTATE_UINT8(sect
, FDrive
),
550 VMSTATE_END_OF_LIST()
552 .subsections
= (VMStateSubsection
[]) {
554 .vmsd
= &vmstate_fdrive_media_changed
,
555 .needed
= &fdrive_media_changed_needed
,
562 static void fdc_pre_save(void *opaque
)
566 s
->dor_vmstate
= s
->dor
| GET_CUR_DRV(s
);
569 static int fdc_post_load(void *opaque
, int version_id
)
573 SET_CUR_DRV(s
, s
->dor_vmstate
& FD_DOR_SELMASK
);
574 s
->dor
= s
->dor_vmstate
& ~FD_DOR_SELMASK
;
578 static const VMStateDescription vmstate_fdc
= {
581 .minimum_version_id
= 2,
582 .minimum_version_id_old
= 2,
583 .pre_save
= fdc_pre_save
,
584 .post_load
= fdc_post_load
,
585 .fields
= (VMStateField
[]) {
586 /* Controller State */
587 VMSTATE_UINT8(sra
, FDCtrl
),
588 VMSTATE_UINT8(srb
, FDCtrl
),
589 VMSTATE_UINT8(dor_vmstate
, FDCtrl
),
590 VMSTATE_UINT8(tdr
, FDCtrl
),
591 VMSTATE_UINT8(dsr
, FDCtrl
),
592 VMSTATE_UINT8(msr
, FDCtrl
),
593 VMSTATE_UINT8(status0
, FDCtrl
),
594 VMSTATE_UINT8(status1
, FDCtrl
),
595 VMSTATE_UINT8(status2
, FDCtrl
),
597 VMSTATE_VARRAY_INT32(fifo
, FDCtrl
, fifo_size
, 0, vmstate_info_uint8
,
599 VMSTATE_UINT32(data_pos
, FDCtrl
),
600 VMSTATE_UINT32(data_len
, FDCtrl
),
601 VMSTATE_UINT8(data_state
, FDCtrl
),
602 VMSTATE_UINT8(data_dir
, FDCtrl
),
603 VMSTATE_UINT8(eot
, FDCtrl
),
604 /* States kept only to be returned back */
605 VMSTATE_UINT8(timer0
, FDCtrl
),
606 VMSTATE_UINT8(timer1
, FDCtrl
),
607 VMSTATE_UINT8(precomp_trk
, FDCtrl
),
608 VMSTATE_UINT8(config
, FDCtrl
),
609 VMSTATE_UINT8(lock
, FDCtrl
),
610 VMSTATE_UINT8(pwrd
, FDCtrl
),
611 VMSTATE_UINT8_EQUAL(num_floppies
, FDCtrl
),
612 VMSTATE_STRUCT_ARRAY(drives
, FDCtrl
, MAX_FD
, 1,
613 vmstate_fdrive
, FDrive
),
614 VMSTATE_END_OF_LIST()
618 static void fdctrl_external_reset_sysbus(DeviceState
*d
)
620 FDCtrlSysBus
*sys
= container_of(d
, FDCtrlSysBus
, busdev
.qdev
);
621 FDCtrl
*s
= &sys
->state
;
626 static void fdctrl_external_reset_isa(DeviceState
*d
)
628 FDCtrlISABus
*isa
= container_of(d
, FDCtrlISABus
, busdev
.qdev
);
629 FDCtrl
*s
= &isa
->state
;
634 static void fdctrl_handle_tc(void *opaque
, int irq
, int level
)
636 //FDCtrl *s = opaque;
640 FLOPPY_DPRINTF("TC pulsed\n");
644 /* Change IRQ state */
645 static void fdctrl_reset_irq(FDCtrl
*fdctrl
)
647 if (!(fdctrl
->sra
& FD_SRA_INTPEND
))
649 FLOPPY_DPRINTF("Reset interrupt\n");
650 qemu_set_irq(fdctrl
->irq
, 0);
651 fdctrl
->sra
&= ~FD_SRA_INTPEND
;
654 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
)
657 if (fdctrl
->sun4m
&& (fdctrl
->msr
& FD_MSR_CMDBUSY
)) {
659 fdctrl
->msr
&= ~FD_MSR_CMDBUSY
;
660 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
661 fdctrl
->status0
= status0
;
664 if (!(fdctrl
->sra
& FD_SRA_INTPEND
)) {
665 qemu_set_irq(fdctrl
->irq
, 1);
666 fdctrl
->sra
|= FD_SRA_INTPEND
;
668 fdctrl
->reset_sensei
= 0;
669 fdctrl
->status0
= status0
;
670 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl
->status0
);
673 /* Reset controller */
674 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
)
678 FLOPPY_DPRINTF("reset controller\n");
679 fdctrl_reset_irq(fdctrl
);
680 /* Initialise controller */
683 if (!fdctrl
->drives
[1].bs
)
684 fdctrl
->sra
|= FD_SRA_nDRV2
;
686 fdctrl
->dor
= FD_DOR_nRESET
;
687 fdctrl
->dor
|= (fdctrl
->dma_chann
!= -1) ? FD_DOR_DMAEN
: 0;
688 fdctrl
->msr
= FD_MSR_RQM
;
690 fdctrl
->data_pos
= 0;
691 fdctrl
->data_len
= 0;
692 fdctrl
->data_state
= 0;
693 fdctrl
->data_dir
= FD_DIR_WRITE
;
694 for (i
= 0; i
< MAX_FD
; i
++)
695 fd_recalibrate(&fdctrl
->drives
[i
]);
696 fdctrl_reset_fifo(fdctrl
);
698 fdctrl_raise_irq(fdctrl
, FD_SR0_RDYCHG
);
699 fdctrl
->reset_sensei
= FD_RESET_SENSEI_COUNT
;
703 static inline FDrive
*drv0(FDCtrl
*fdctrl
)
705 return &fdctrl
->drives
[(fdctrl
->tdr
& FD_TDR_BOOTSEL
) >> 2];
708 static inline FDrive
*drv1(FDCtrl
*fdctrl
)
710 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (1 << 2))
711 return &fdctrl
->drives
[1];
713 return &fdctrl
->drives
[0];
717 static inline FDrive
*drv2(FDCtrl
*fdctrl
)
719 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (2 << 2))
720 return &fdctrl
->drives
[2];
722 return &fdctrl
->drives
[1];
725 static inline FDrive
*drv3(FDCtrl
*fdctrl
)
727 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (3 << 2))
728 return &fdctrl
->drives
[3];
730 return &fdctrl
->drives
[2];
734 static FDrive
*get_cur_drv(FDCtrl
*fdctrl
)
736 switch (fdctrl
->cur_drv
) {
737 case 0: return drv0(fdctrl
);
738 case 1: return drv1(fdctrl
);
740 case 2: return drv2(fdctrl
);
741 case 3: return drv3(fdctrl
);
743 default: return NULL
;
747 /* Status A register : 0x00 (read-only) */
748 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
)
750 uint32_t retval
= fdctrl
->sra
;
752 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval
);
757 /* Status B register : 0x01 (read-only) */
758 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
)
760 uint32_t retval
= fdctrl
->srb
;
762 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval
);
767 /* Digital output register : 0x02 */
768 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
)
770 uint32_t retval
= fdctrl
->dor
;
773 retval
|= fdctrl
->cur_drv
;
774 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
779 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
)
781 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
784 if (value
& FD_DOR_MOTEN0
)
785 fdctrl
->srb
|= FD_SRB_MTR0
;
787 fdctrl
->srb
&= ~FD_SRB_MTR0
;
788 if (value
& FD_DOR_MOTEN1
)
789 fdctrl
->srb
|= FD_SRB_MTR1
;
791 fdctrl
->srb
&= ~FD_SRB_MTR1
;
795 fdctrl
->srb
|= FD_SRB_DR0
;
797 fdctrl
->srb
&= ~FD_SRB_DR0
;
800 if (!(value
& FD_DOR_nRESET
)) {
801 if (fdctrl
->dor
& FD_DOR_nRESET
) {
802 FLOPPY_DPRINTF("controller enter RESET state\n");
805 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
806 FLOPPY_DPRINTF("controller out of RESET state\n");
807 fdctrl_reset(fdctrl
, 1);
808 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
812 fdctrl
->cur_drv
= value
& FD_DOR_SELMASK
;
817 /* Tape drive register : 0x03 */
818 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
)
820 uint32_t retval
= fdctrl
->tdr
;
822 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
827 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
)
830 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
831 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
834 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
835 /* Disk boot selection indicator */
836 fdctrl
->tdr
= value
& FD_TDR_BOOTSEL
;
837 /* Tape indicators: never allow */
840 /* Main status register : 0x04 (read) */
841 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
)
843 uint32_t retval
= fdctrl
->msr
;
845 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
846 fdctrl
->dor
|= FD_DOR_nRESET
;
850 retval
|= FD_MSR_DIO
;
851 fdctrl_reset_irq(fdctrl
);
854 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
859 /* Data select rate register : 0x04 (write) */
860 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
)
863 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
864 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
867 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
868 /* Reset: autoclear */
869 if (value
& FD_DSR_SWRESET
) {
870 fdctrl
->dor
&= ~FD_DOR_nRESET
;
871 fdctrl_reset(fdctrl
, 1);
872 fdctrl
->dor
|= FD_DOR_nRESET
;
874 if (value
& FD_DSR_PWRDOWN
) {
875 fdctrl_reset(fdctrl
, 1);
880 static int fdctrl_media_changed(FDrive
*drv
)
886 if (drv
->media_changed
) {
887 drv
->media_changed
= 0;
890 ret
= bdrv_media_changed(drv
->bs
);
892 ret
= 0; /* we don't know, assume no */
901 /* Digital input register : 0x07 (read-only) */
902 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
)
906 if (fdctrl_media_changed(drv0(fdctrl
))
907 || fdctrl_media_changed(drv1(fdctrl
))
909 || fdctrl_media_changed(drv2(fdctrl
))
910 || fdctrl_media_changed(drv3(fdctrl
))
913 retval
|= FD_DIR_DSKCHG
;
915 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
921 /* FIFO state control */
922 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
)
924 fdctrl
->data_dir
= FD_DIR_WRITE
;
925 fdctrl
->data_pos
= 0;
926 fdctrl
->msr
&= ~(FD_MSR_CMDBUSY
| FD_MSR_DIO
);
929 /* Set FIFO status for the host to read */
930 static void fdctrl_set_fifo(FDCtrl
*fdctrl
, int fifo_len
, int do_irq
)
932 fdctrl
->data_dir
= FD_DIR_READ
;
933 fdctrl
->data_len
= fifo_len
;
934 fdctrl
->data_pos
= 0;
935 fdctrl
->msr
|= FD_MSR_CMDBUSY
| FD_MSR_RQM
| FD_MSR_DIO
;
937 fdctrl_raise_irq(fdctrl
, 0x00);
940 /* Set an error: unimplemented/unknown command */
941 static void fdctrl_unimplemented(FDCtrl
*fdctrl
, int direction
)
943 FLOPPY_ERROR("unimplemented command 0x%02x\n", fdctrl
->fifo
[0]);
944 fdctrl
->fifo
[0] = FD_SR0_INVCMD
;
945 fdctrl_set_fifo(fdctrl
, 1, 0);
948 /* Seek to next sector */
949 static int fdctrl_seek_to_next_sect(FDCtrl
*fdctrl
, FDrive
*cur_drv
)
951 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
952 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
954 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
956 if (cur_drv
->sect
>= cur_drv
->last_sect
||
957 cur_drv
->sect
== fdctrl
->eot
) {
959 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
960 if (cur_drv
->head
== 0 &&
961 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
966 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0)
973 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
974 cur_drv
->head
, cur_drv
->track
,
975 cur_drv
->sect
, fd_sector(cur_drv
));
982 /* Callback for transfer end (stop or abort) */
983 static void fdctrl_stop_transfer(FDCtrl
*fdctrl
, uint8_t status0
,
984 uint8_t status1
, uint8_t status2
)
988 cur_drv
= get_cur_drv(fdctrl
);
989 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
990 status0
, status1
, status2
,
991 status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
));
992 fdctrl
->fifo
[0] = status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
993 fdctrl
->fifo
[1] = status1
;
994 fdctrl
->fifo
[2] = status2
;
995 fdctrl
->fifo
[3] = cur_drv
->track
;
996 fdctrl
->fifo
[4] = cur_drv
->head
;
997 fdctrl
->fifo
[5] = cur_drv
->sect
;
998 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
999 fdctrl
->data_dir
= FD_DIR_READ
;
1000 if (!(fdctrl
->msr
& FD_MSR_NONDMA
)) {
1001 DMA_release_DREQ(fdctrl
->dma_chann
);
1003 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
1004 fdctrl
->msr
&= ~FD_MSR_NONDMA
;
1005 fdctrl_set_fifo(fdctrl
, 7, 1);
1008 /* Prepare a data transfer (either DMA or FIFO) */
1009 static void fdctrl_start_transfer(FDCtrl
*fdctrl
, int direction
)
1015 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1016 cur_drv
= get_cur_drv(fdctrl
);
1017 kt
= fdctrl
->fifo
[2];
1018 kh
= fdctrl
->fifo
[3];
1019 ks
= fdctrl
->fifo
[4];
1020 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1021 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1022 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
));
1023 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1026 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1027 fdctrl
->fifo
[3] = kt
;
1028 fdctrl
->fifo
[4] = kh
;
1029 fdctrl
->fifo
[5] = ks
;
1033 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1034 fdctrl
->fifo
[3] = kt
;
1035 fdctrl
->fifo
[4] = kh
;
1036 fdctrl
->fifo
[5] = ks
;
1039 /* No seek enabled */
1040 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1041 fdctrl
->fifo
[3] = kt
;
1042 fdctrl
->fifo
[4] = kh
;
1043 fdctrl
->fifo
[5] = ks
;
1052 /* Set the FIFO state */
1053 fdctrl
->data_dir
= direction
;
1054 fdctrl
->data_pos
= 0;
1055 fdctrl
->msr
|= FD_MSR_CMDBUSY
;
1056 if (fdctrl
->fifo
[0] & 0x80)
1057 fdctrl
->data_state
|= FD_STATE_MULTI
;
1059 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1061 fdctrl
->data_state
|= FD_STATE_SEEK
;
1063 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1064 if (fdctrl
->fifo
[5] == 00) {
1065 fdctrl
->data_len
= fdctrl
->fifo
[8];
1068 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
1069 tmp
= (fdctrl
->fifo
[6] - ks
+ 1);
1070 if (fdctrl
->fifo
[0] & 0x80)
1071 tmp
+= fdctrl
->fifo
[6];
1072 fdctrl
->data_len
*= tmp
;
1074 fdctrl
->eot
= fdctrl
->fifo
[6];
1075 if (fdctrl
->dor
& FD_DOR_DMAEN
) {
1077 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1078 dma_mode
= DMA_get_channel_mode(fdctrl
->dma_chann
);
1079 dma_mode
= (dma_mode
>> 2) & 3;
1080 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1081 dma_mode
, direction
,
1082 (128 << fdctrl
->fifo
[5]) *
1083 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
1084 if (((direction
== FD_DIR_SCANE
|| direction
== FD_DIR_SCANL
||
1085 direction
== FD_DIR_SCANH
) && dma_mode
== 0) ||
1086 (direction
== FD_DIR_WRITE
&& dma_mode
== 2) ||
1087 (direction
== FD_DIR_READ
&& dma_mode
== 1)) {
1088 /* No access is allowed until DMA transfer has completed */
1089 fdctrl
->msr
&= ~FD_MSR_RQM
;
1090 /* Now, we just have to wait for the DMA controller to
1093 DMA_hold_DREQ(fdctrl
->dma_chann
);
1094 DMA_schedule(fdctrl
->dma_chann
);
1097 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode
, direction
);
1100 FLOPPY_DPRINTF("start non-DMA transfer\n");
1101 fdctrl
->msr
|= FD_MSR_NONDMA
;
1102 if (direction
!= FD_DIR_WRITE
)
1103 fdctrl
->msr
|= FD_MSR_DIO
;
1104 /* IO based transfer: calculate len */
1105 fdctrl_raise_irq(fdctrl
, 0x00);
1110 /* Prepare a transfer of deleted data */
1111 static void fdctrl_start_transfer_del(FDCtrl
*fdctrl
, int direction
)
1113 FLOPPY_ERROR("fdctrl_start_transfer_del() unimplemented\n");
1115 /* We don't handle deleted data,
1116 * so we don't return *ANYTHING*
1118 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1121 /* handlers for DMA transfers */
1122 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
1123 int dma_pos
, int dma_len
)
1127 int len
, start_pos
, rel_pos
;
1128 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
1131 if (fdctrl
->msr
& FD_MSR_RQM
) {
1132 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1135 cur_drv
= get_cur_drv(fdctrl
);
1136 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
1137 fdctrl
->data_dir
== FD_DIR_SCANH
)
1138 status2
= FD_SR2_SNS
;
1139 if (dma_len
> fdctrl
->data_len
)
1140 dma_len
= fdctrl
->data_len
;
1141 if (cur_drv
->bs
== NULL
) {
1142 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
1143 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1145 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1147 goto transfer_error
;
1149 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1150 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
1151 len
= dma_len
- fdctrl
->data_pos
;
1152 if (len
+ rel_pos
> FD_SECTOR_LEN
)
1153 len
= FD_SECTOR_LEN
- rel_pos
;
1154 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1155 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
1156 fdctrl
->data_len
, GET_CUR_DRV(fdctrl
), cur_drv
->head
,
1157 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
1158 fd_sector(cur_drv
) * FD_SECTOR_LEN
);
1159 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
1160 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
1161 /* READ & SCAN commands and realign to a sector for WRITE */
1162 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1163 fdctrl
->fifo
, 1) < 0) {
1164 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1165 fd_sector(cur_drv
));
1166 /* Sure, image size is too small... */
1167 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1170 switch (fdctrl
->data_dir
) {
1173 DMA_write_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1174 fdctrl
->data_pos
, len
);
1177 /* WRITE commands */
1178 DMA_read_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1179 fdctrl
->data_pos
, len
);
1180 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1181 fdctrl
->fifo
, 1) < 0) {
1182 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv
));
1183 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1184 goto transfer_error
;
1190 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1192 DMA_read_memory (nchan
, tmpbuf
, fdctrl
->data_pos
, len
);
1193 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1195 status2
= FD_SR2_SEH
;
1198 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1199 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1206 fdctrl
->data_pos
+= len
;
1207 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1209 /* Seek to next sector */
1210 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
))
1215 len
= fdctrl
->data_pos
- start_pos
;
1216 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1217 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1218 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1219 fdctrl
->data_dir
== FD_DIR_SCANL
||
1220 fdctrl
->data_dir
== FD_DIR_SCANH
)
1221 status2
= FD_SR2_SEH
;
1222 if (FD_DID_SEEK(fdctrl
->data_state
))
1223 status0
|= FD_SR0_SEEK
;
1224 fdctrl
->data_len
-= len
;
1225 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1231 /* Data register : 0x05 */
1232 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
)
1235 uint32_t retval
= 0;
1238 cur_drv
= get_cur_drv(fdctrl
);
1239 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1240 if (!(fdctrl
->msr
& FD_MSR_RQM
) || !(fdctrl
->msr
& FD_MSR_DIO
)) {
1241 FLOPPY_ERROR("controller not ready for reading\n");
1244 pos
= fdctrl
->data_pos
;
1245 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1246 pos
%= FD_SECTOR_LEN
;
1248 if (fdctrl
->data_pos
!= 0)
1249 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1250 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1251 fd_sector(cur_drv
));
1254 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1255 FLOPPY_DPRINTF("error getting sector %d\n",
1256 fd_sector(cur_drv
));
1257 /* Sure, image size is too small... */
1258 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1262 retval
= fdctrl
->fifo
[pos
];
1263 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1264 fdctrl
->data_pos
= 0;
1265 /* Switch from transfer mode to status mode
1266 * then from status mode to command mode
1268 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1269 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1271 fdctrl_reset_fifo(fdctrl
);
1272 fdctrl_reset_irq(fdctrl
);
1275 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1280 static void fdctrl_format_sector(FDCtrl
*fdctrl
)
1285 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1286 cur_drv
= get_cur_drv(fdctrl
);
1287 kt
= fdctrl
->fifo
[6];
1288 kh
= fdctrl
->fifo
[7];
1289 ks
= fdctrl
->fifo
[8];
1290 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1291 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1292 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
));
1293 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1296 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1297 fdctrl
->fifo
[3] = kt
;
1298 fdctrl
->fifo
[4] = kh
;
1299 fdctrl
->fifo
[5] = ks
;
1303 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1304 fdctrl
->fifo
[3] = kt
;
1305 fdctrl
->fifo
[4] = kh
;
1306 fdctrl
->fifo
[5] = ks
;
1309 /* No seek enabled */
1310 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1311 fdctrl
->fifo
[3] = kt
;
1312 fdctrl
->fifo
[4] = kh
;
1313 fdctrl
->fifo
[5] = ks
;
1316 fdctrl
->data_state
|= FD_STATE_SEEK
;
1321 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1322 if (cur_drv
->bs
== NULL
||
1323 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1324 FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv
));
1325 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1327 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1328 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1329 /* Last sector done */
1330 if (FD_DID_SEEK(fdctrl
->data_state
))
1331 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1333 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1336 fdctrl
->data_pos
= 0;
1337 fdctrl
->data_len
= 4;
1342 static void fdctrl_handle_lock(FDCtrl
*fdctrl
, int direction
)
1344 fdctrl
->lock
= (fdctrl
->fifo
[0] & 0x80) ? 1 : 0;
1345 fdctrl
->fifo
[0] = fdctrl
->lock
<< 4;
1346 fdctrl_set_fifo(fdctrl
, 1, fdctrl
->lock
);
1349 static void fdctrl_handle_dumpreg(FDCtrl
*fdctrl
, int direction
)
1351 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1353 /* Drives position */
1354 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1355 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1357 fdctrl
->fifo
[2] = drv2(fdctrl
)->track
;
1358 fdctrl
->fifo
[3] = drv3(fdctrl
)->track
;
1360 fdctrl
->fifo
[2] = 0;
1361 fdctrl
->fifo
[3] = 0;
1364 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1365 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | (fdctrl
->dor
& FD_DOR_DMAEN
? 1 : 0);
1366 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1367 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1368 (cur_drv
->perpendicular
<< 2);
1369 fdctrl
->fifo
[8] = fdctrl
->config
;
1370 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1371 fdctrl_set_fifo(fdctrl
, 10, 0);
1374 static void fdctrl_handle_version(FDCtrl
*fdctrl
, int direction
)
1376 /* Controller's version */
1377 fdctrl
->fifo
[0] = fdctrl
->version
;
1378 fdctrl_set_fifo(fdctrl
, 1, 1);
1381 static void fdctrl_handle_partid(FDCtrl
*fdctrl
, int direction
)
1383 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1384 fdctrl_set_fifo(fdctrl
, 1, 0);
1387 static void fdctrl_handle_restore(FDCtrl
*fdctrl
, int direction
)
1389 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1391 /* Drives position */
1392 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1393 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1395 drv2(fdctrl
)->track
= fdctrl
->fifo
[5];
1396 drv3(fdctrl
)->track
= fdctrl
->fifo
[6];
1399 fdctrl
->timer0
= fdctrl
->fifo
[7];
1400 fdctrl
->timer1
= fdctrl
->fifo
[8];
1401 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1402 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1403 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1404 fdctrl
->config
= fdctrl
->fifo
[11];
1405 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1406 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1407 fdctrl_reset_fifo(fdctrl
);
1410 static void fdctrl_handle_save(FDCtrl
*fdctrl
, int direction
)
1412 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1414 fdctrl
->fifo
[0] = 0;
1415 fdctrl
->fifo
[1] = 0;
1416 /* Drives position */
1417 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1418 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1420 fdctrl
->fifo
[4] = drv2(fdctrl
)->track
;
1421 fdctrl
->fifo
[5] = drv3(fdctrl
)->track
;
1423 fdctrl
->fifo
[4] = 0;
1424 fdctrl
->fifo
[5] = 0;
1427 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1428 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1429 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1430 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1431 (cur_drv
->perpendicular
<< 2);
1432 fdctrl
->fifo
[10] = fdctrl
->config
;
1433 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1434 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1435 fdctrl
->fifo
[13] = 0;
1436 fdctrl
->fifo
[14] = 0;
1437 fdctrl_set_fifo(fdctrl
, 15, 1);
1440 static void fdctrl_handle_readid(FDCtrl
*fdctrl
, int direction
)
1442 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1444 /* XXX: should set main status register to busy */
1445 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1446 qemu_mod_timer(fdctrl
->result_timer
,
1447 qemu_get_clock_ns(vm_clock
) + (get_ticks_per_sec() / 50));
1450 static void fdctrl_handle_format_track(FDCtrl
*fdctrl
, int direction
)
1454 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1455 cur_drv
= get_cur_drv(fdctrl
);
1456 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1457 if (fdctrl
->fifo
[0] & 0x80)
1458 fdctrl
->data_state
|= FD_STATE_MULTI
;
1460 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1461 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1463 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1465 cur_drv
->last_sect
=
1466 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1467 fdctrl
->fifo
[3] / 2;
1469 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1471 /* TODO: implement format using DMA expected by the Bochs BIOS
1472 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1473 * the sector with the specified fill byte
1475 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1476 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1479 static void fdctrl_handle_specify(FDCtrl
*fdctrl
, int direction
)
1481 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1482 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1483 if (fdctrl
->fifo
[2] & 1)
1484 fdctrl
->dor
&= ~FD_DOR_DMAEN
;
1486 fdctrl
->dor
|= FD_DOR_DMAEN
;
1487 /* No result back */
1488 fdctrl_reset_fifo(fdctrl
);
1491 static void fdctrl_handle_sense_drive_status(FDCtrl
*fdctrl
, int direction
)
1495 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1496 cur_drv
= get_cur_drv(fdctrl
);
1497 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1498 /* 1 Byte status back */
1499 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1500 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1501 (cur_drv
->head
<< 2) |
1502 GET_CUR_DRV(fdctrl
) |
1504 fdctrl_set_fifo(fdctrl
, 1, 0);
1507 static void fdctrl_handle_recalibrate(FDCtrl
*fdctrl
, int direction
)
1511 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1512 cur_drv
= get_cur_drv(fdctrl
);
1513 fd_recalibrate(cur_drv
);
1514 fdctrl_reset_fifo(fdctrl
);
1515 /* Raise Interrupt */
1516 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1519 static void fdctrl_handle_sense_interrupt_status(FDCtrl
*fdctrl
, int direction
)
1521 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1523 if(fdctrl
->reset_sensei
> 0) {
1525 FD_SR0_RDYCHG
+ FD_RESET_SENSEI_COUNT
- fdctrl
->reset_sensei
;
1526 fdctrl
->reset_sensei
--;
1528 /* XXX: status0 handling is broken for read/write
1529 commands, so we do this hack. It should be suppressed
1532 FD_SR0_SEEK
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1535 fdctrl
->fifo
[1] = cur_drv
->track
;
1536 fdctrl_set_fifo(fdctrl
, 2, 0);
1537 fdctrl_reset_irq(fdctrl
);
1538 fdctrl
->status0
= FD_SR0_RDYCHG
;
1541 static void fdctrl_handle_seek(FDCtrl
*fdctrl
, int direction
)
1545 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1546 cur_drv
= get_cur_drv(fdctrl
);
1547 fdctrl_reset_fifo(fdctrl
);
1548 if (fdctrl
->fifo
[2] > cur_drv
->max_track
) {
1549 fdctrl_raise_irq(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
);
1551 cur_drv
->track
= fdctrl
->fifo
[2];
1552 /* Raise Interrupt */
1553 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1557 static void fdctrl_handle_perpendicular_mode(FDCtrl
*fdctrl
, int direction
)
1559 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1561 if (fdctrl
->fifo
[1] & 0x80)
1562 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
1563 /* No result back */
1564 fdctrl_reset_fifo(fdctrl
);
1567 static void fdctrl_handle_configure(FDCtrl
*fdctrl
, int direction
)
1569 fdctrl
->config
= fdctrl
->fifo
[2];
1570 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
1571 /* No result back */
1572 fdctrl_reset_fifo(fdctrl
);
1575 static void fdctrl_handle_powerdown_mode(FDCtrl
*fdctrl
, int direction
)
1577 fdctrl
->pwrd
= fdctrl
->fifo
[1];
1578 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1579 fdctrl_set_fifo(fdctrl
, 1, 1);
1582 static void fdctrl_handle_option(FDCtrl
*fdctrl
, int direction
)
1584 /* No result back */
1585 fdctrl_reset_fifo(fdctrl
);
1588 static void fdctrl_handle_drive_specification_command(FDCtrl
*fdctrl
, int direction
)
1590 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1592 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x80) {
1593 /* Command parameters done */
1594 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x40) {
1595 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1596 fdctrl
->fifo
[2] = 0;
1597 fdctrl
->fifo
[3] = 0;
1598 fdctrl_set_fifo(fdctrl
, 4, 1);
1600 fdctrl_reset_fifo(fdctrl
);
1602 } else if (fdctrl
->data_len
> 7) {
1604 fdctrl
->fifo
[0] = 0x80 |
1605 (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1606 fdctrl_set_fifo(fdctrl
, 1, 1);
1610 static void fdctrl_handle_relative_seek_out(FDCtrl
*fdctrl
, int direction
)
1614 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1615 cur_drv
= get_cur_drv(fdctrl
);
1616 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
1617 cur_drv
->track
= cur_drv
->max_track
- 1;
1619 cur_drv
->track
+= fdctrl
->fifo
[2];
1621 fdctrl_reset_fifo(fdctrl
);
1622 /* Raise Interrupt */
1623 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1626 static void fdctrl_handle_relative_seek_in(FDCtrl
*fdctrl
, int direction
)
1630 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1631 cur_drv
= get_cur_drv(fdctrl
);
1632 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
1635 cur_drv
->track
-= fdctrl
->fifo
[2];
1637 fdctrl_reset_fifo(fdctrl
);
1638 /* Raise Interrupt */
1639 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1642 static const struct {
1647 void (*handler
)(FDCtrl
*fdctrl
, int direction
);
1650 { FD_CMD_READ
, 0x1f, "READ", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1651 { FD_CMD_WRITE
, 0x3f, "WRITE", 8, fdctrl_start_transfer
, FD_DIR_WRITE
},
1652 { FD_CMD_SEEK
, 0xff, "SEEK", 2, fdctrl_handle_seek
},
1653 { FD_CMD_SENSE_INTERRUPT_STATUS
, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status
},
1654 { FD_CMD_RECALIBRATE
, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate
},
1655 { FD_CMD_FORMAT_TRACK
, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track
},
1656 { FD_CMD_READ_TRACK
, 0xbf, "READ TRACK", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1657 { FD_CMD_RESTORE
, 0xff, "RESTORE", 17, fdctrl_handle_restore
}, /* part of READ DELETED DATA */
1658 { FD_CMD_SAVE
, 0xff, "SAVE", 0, fdctrl_handle_save
}, /* part of READ DELETED DATA */
1659 { FD_CMD_READ_DELETED
, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_READ
},
1660 { FD_CMD_SCAN_EQUAL
, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANE
},
1661 { FD_CMD_VERIFY
, 0x1f, "VERIFY", 8, fdctrl_unimplemented
},
1662 { FD_CMD_SCAN_LOW_OR_EQUAL
, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANL
},
1663 { FD_CMD_SCAN_HIGH_OR_EQUAL
, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANH
},
1664 { FD_CMD_WRITE_DELETED
, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_WRITE
},
1665 { FD_CMD_READ_ID
, 0xbf, "READ ID", 1, fdctrl_handle_readid
},
1666 { FD_CMD_SPECIFY
, 0xff, "SPECIFY", 2, fdctrl_handle_specify
},
1667 { FD_CMD_SENSE_DRIVE_STATUS
, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status
},
1668 { FD_CMD_PERPENDICULAR_MODE
, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode
},
1669 { FD_CMD_CONFIGURE
, 0xff, "CONFIGURE", 3, fdctrl_handle_configure
},
1670 { FD_CMD_POWERDOWN_MODE
, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode
},
1671 { FD_CMD_OPTION
, 0xff, "OPTION", 1, fdctrl_handle_option
},
1672 { FD_CMD_DRIVE_SPECIFICATION_COMMAND
, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command
},
1673 { FD_CMD_RELATIVE_SEEK_OUT
, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out
},
1674 { FD_CMD_FORMAT_AND_WRITE
, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented
},
1675 { FD_CMD_RELATIVE_SEEK_IN
, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in
},
1676 { FD_CMD_LOCK
, 0x7f, "LOCK", 0, fdctrl_handle_lock
},
1677 { FD_CMD_DUMPREG
, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg
},
1678 { FD_CMD_VERSION
, 0xff, "VERSION", 0, fdctrl_handle_version
},
1679 { FD_CMD_PART_ID
, 0xff, "PART ID", 0, fdctrl_handle_partid
},
1680 { FD_CMD_WRITE
, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer
, FD_DIR_WRITE
}, /* not in specification ; BeOS 4.5 bug */
1681 { 0, 0, "unknown", 0, fdctrl_unimplemented
}, /* default handler */
1683 /* Associate command to an index in the 'handlers' array */
1684 static uint8_t command_to_handler
[256];
1686 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
)
1692 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1693 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1696 if (!(fdctrl
->msr
& FD_MSR_RQM
) || (fdctrl
->msr
& FD_MSR_DIO
)) {
1697 FLOPPY_ERROR("controller not ready for writing\n");
1700 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1701 /* Is it write command time ? */
1702 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1703 /* FIFO data write */
1704 pos
= fdctrl
->data_pos
++;
1705 pos
%= FD_SECTOR_LEN
;
1706 fdctrl
->fifo
[pos
] = value
;
1707 if (pos
== FD_SECTOR_LEN
- 1 ||
1708 fdctrl
->data_pos
== fdctrl
->data_len
) {
1709 cur_drv
= get_cur_drv(fdctrl
);
1710 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1711 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv
));
1714 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1715 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1716 fd_sector(cur_drv
));
1720 /* Switch from transfer mode to status mode
1721 * then from status mode to command mode
1723 if (fdctrl
->data_pos
== fdctrl
->data_len
)
1724 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1727 if (fdctrl
->data_pos
== 0) {
1729 pos
= command_to_handler
[value
& 0xff];
1730 FLOPPY_DPRINTF("%s command\n", handlers
[pos
].name
);
1731 fdctrl
->data_len
= handlers
[pos
].parameters
+ 1;
1734 FLOPPY_DPRINTF("%s: %02x\n", __func__
, value
);
1735 fdctrl
->fifo
[fdctrl
->data_pos
++] = value
;
1736 if (fdctrl
->data_pos
== fdctrl
->data_len
) {
1737 /* We now have all parameters
1738 * and will be able to treat the command
1740 if (fdctrl
->data_state
& FD_STATE_FORMAT
) {
1741 fdctrl_format_sector(fdctrl
);
1745 pos
= command_to_handler
[fdctrl
->fifo
[0] & 0xff];
1746 FLOPPY_DPRINTF("treat %s command\n", handlers
[pos
].name
);
1747 (*handlers
[pos
].handler
)(fdctrl
, handlers
[pos
].direction
);
1751 static void fdctrl_result_timer(void *opaque
)
1753 FDCtrl
*fdctrl
= opaque
;
1754 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1756 /* Pretend we are spinning.
1757 * This is needed for Coherent, which uses READ ID to check for
1758 * sector interleaving.
1760 if (cur_drv
->last_sect
!= 0) {
1761 cur_drv
->sect
= (cur_drv
->sect
% cur_drv
->last_sect
) + 1;
1763 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1766 static void fdctrl_change_cb(void *opaque
, bool load
)
1768 FDrive
*drive
= opaque
;
1770 drive
->media_changed
= 1;
1773 static const BlockDevOps fdctrl_block_ops
= {
1774 .change_media_cb
= fdctrl_change_cb
,
1777 /* Init functions */
1778 static int fdctrl_connect_drives(FDCtrl
*fdctrl
)
1783 for (i
= 0; i
< MAX_FD
; i
++) {
1784 drive
= &fdctrl
->drives
[i
];
1787 if (bdrv_get_on_error(drive
->bs
, 0) != BLOCK_ERR_STOP_ENOSPC
) {
1788 error_report("fdc doesn't support drive option werror");
1791 if (bdrv_get_on_error(drive
->bs
, 1) != BLOCK_ERR_REPORT
) {
1792 error_report("fdc doesn't support drive option rerror");
1798 fd_revalidate(drive
);
1800 drive
->media_changed
= 1;
1801 bdrv_set_dev_ops(drive
->bs
, &fdctrl_block_ops
, drive
);
1807 void fdctrl_init_sysbus(qemu_irq irq
, int dma_chann
,
1808 target_phys_addr_t mmio_base
, DriveInfo
**fds
)
1814 dev
= qdev_create(NULL
, "sysbus-fdc");
1815 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1816 fdctrl
= &sys
->state
;
1817 fdctrl
->dma_chann
= dma_chann
; /* FIXME */
1819 qdev_prop_set_drive_nofail(dev
, "driveA", fds
[0]->bdrv
);
1822 qdev_prop_set_drive_nofail(dev
, "driveB", fds
[1]->bdrv
);
1824 qdev_init_nofail(dev
);
1825 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1826 sysbus_mmio_map(&sys
->busdev
, 0, mmio_base
);
1829 void sun4m_fdctrl_init(qemu_irq irq
, target_phys_addr_t io_base
,
1830 DriveInfo
**fds
, qemu_irq
*fdc_tc
)
1835 dev
= qdev_create(NULL
, "SUNW,fdtwo");
1837 qdev_prop_set_drive_nofail(dev
, "drive", fds
[0]->bdrv
);
1839 qdev_init_nofail(dev
);
1840 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1841 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1842 sysbus_mmio_map(&sys
->busdev
, 0, io_base
);
1843 *fdc_tc
= qdev_get_gpio_in(dev
, 0);
1846 static int fdctrl_init_common(FDCtrl
*fdctrl
)
1849 static int command_tables_inited
= 0;
1851 /* Fill 'command_to_handler' lookup table */
1852 if (!command_tables_inited
) {
1853 command_tables_inited
= 1;
1854 for (i
= ARRAY_SIZE(handlers
) - 1; i
>= 0; i
--) {
1855 for (j
= 0; j
< sizeof(command_to_handler
); j
++) {
1856 if ((j
& handlers
[i
].mask
) == handlers
[i
].value
) {
1857 command_to_handler
[j
] = i
;
1863 FLOPPY_DPRINTF("init controller\n");
1864 fdctrl
->fifo
= qemu_memalign(512, FD_SECTOR_LEN
);
1865 fdctrl
->fifo_size
= 512;
1866 fdctrl
->result_timer
= qemu_new_timer_ns(vm_clock
,
1867 fdctrl_result_timer
, fdctrl
);
1869 fdctrl
->version
= 0x90; /* Intel 82078 controller */
1870 fdctrl
->config
= FD_CONFIG_EIS
| FD_CONFIG_EFIFO
; /* Implicit seek, polling & FIFO enabled */
1871 fdctrl
->num_floppies
= MAX_FD
;
1873 if (fdctrl
->dma_chann
!= -1)
1874 DMA_register_channel(fdctrl
->dma_chann
, &fdctrl_transfer_handler
, fdctrl
);
1875 return fdctrl_connect_drives(fdctrl
);
1878 static const MemoryRegionPortio fdc_portio_list
[] = {
1879 { 1, 5, 1, .read
= fdctrl_read
, .write
= fdctrl_write
},
1880 { 7, 1, 1, .read
= fdctrl_read
, .write
= fdctrl_write
},
1881 PORTIO_END_OF_LIST(),
1884 static int isabus_fdc_init1(ISADevice
*dev
)
1886 FDCtrlISABus
*isa
= DO_UPCAST(FDCtrlISABus
, busdev
, dev
);
1887 FDCtrl
*fdctrl
= &isa
->state
;
1893 isa_register_portio_list(dev
, iobase
, fdc_portio_list
, fdctrl
, "fdc");
1895 isa_init_irq(&isa
->busdev
, &fdctrl
->irq
, isairq
);
1896 fdctrl
->dma_chann
= dma_chann
;
1898 qdev_set_legacy_instance_id(&dev
->qdev
, iobase
, 2);
1899 ret
= fdctrl_init_common(fdctrl
);
1901 add_boot_device_path(isa
->bootindexA
, &dev
->qdev
, "/floppy@0");
1902 add_boot_device_path(isa
->bootindexB
, &dev
->qdev
, "/floppy@1");
1907 static int sysbus_fdc_init1(SysBusDevice
*dev
)
1909 FDCtrlSysBus
*sys
= DO_UPCAST(FDCtrlSysBus
, busdev
, dev
);
1910 FDCtrl
*fdctrl
= &sys
->state
;
1913 memory_region_init_io(&fdctrl
->iomem
, &fdctrl_mem_ops
, fdctrl
, "fdc", 0x08);
1914 sysbus_init_mmio_region(dev
, &fdctrl
->iomem
);
1915 sysbus_init_irq(dev
, &fdctrl
->irq
);
1916 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
1917 fdctrl
->dma_chann
= -1;
1919 qdev_set_legacy_instance_id(&dev
->qdev
, 0 /* io */, 2); /* FIXME */
1920 ret
= fdctrl_init_common(fdctrl
);
1925 static int sun4m_fdc_init1(SysBusDevice
*dev
)
1927 FDCtrl
*fdctrl
= &(FROM_SYSBUS(FDCtrlSysBus
, dev
)->state
);
1929 memory_region_init_io(&fdctrl
->iomem
, &fdctrl_mem_strict_ops
, fdctrl
,
1931 sysbus_init_mmio_region(dev
, &fdctrl
->iomem
);
1932 sysbus_init_irq(dev
, &fdctrl
->irq
);
1933 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
1936 qdev_set_legacy_instance_id(&dev
->qdev
, 0 /* io */, 2); /* FIXME */
1937 return fdctrl_init_common(fdctrl
);
1940 void fdc_get_bs(BlockDriverState
*bs
[], ISADevice
*dev
)
1942 FDCtrlISABus
*isa
= DO_UPCAST(FDCtrlISABus
, busdev
, dev
);
1943 FDCtrl
*fdctrl
= &isa
->state
;
1946 for (i
= 0; i
< MAX_FD
; i
++) {
1947 bs
[i
] = fdctrl
->drives
[i
].bs
;
1952 static const VMStateDescription vmstate_isa_fdc
={
1955 .minimum_version_id
= 2,
1956 .fields
= (VMStateField
[]) {
1957 VMSTATE_STRUCT(state
, FDCtrlISABus
, 0, vmstate_fdc
, FDCtrl
),
1958 VMSTATE_END_OF_LIST()
1962 static ISADeviceInfo isa_fdc_info
= {
1963 .init
= isabus_fdc_init1
,
1964 .qdev
.name
= "isa-fdc",
1965 .qdev
.fw_name
= "fdc",
1966 .qdev
.size
= sizeof(FDCtrlISABus
),
1968 .qdev
.vmsd
= &vmstate_isa_fdc
,
1969 .qdev
.reset
= fdctrl_external_reset_isa
,
1970 .qdev
.props
= (Property
[]) {
1971 DEFINE_PROP_DRIVE("driveA", FDCtrlISABus
, state
.drives
[0].bs
),
1972 DEFINE_PROP_DRIVE("driveB", FDCtrlISABus
, state
.drives
[1].bs
),
1973 DEFINE_PROP_INT32("bootindexA", FDCtrlISABus
, bootindexA
, -1),
1974 DEFINE_PROP_INT32("bootindexB", FDCtrlISABus
, bootindexB
, -1),
1975 DEFINE_PROP_END_OF_LIST(),
1979 static const VMStateDescription vmstate_sysbus_fdc
={
1982 .minimum_version_id
= 2,
1983 .fields
= (VMStateField
[]) {
1984 VMSTATE_STRUCT(state
, FDCtrlSysBus
, 0, vmstate_fdc
, FDCtrl
),
1985 VMSTATE_END_OF_LIST()
1989 static SysBusDeviceInfo sysbus_fdc_info
= {
1990 .init
= sysbus_fdc_init1
,
1991 .qdev
.name
= "sysbus-fdc",
1992 .qdev
.size
= sizeof(FDCtrlSysBus
),
1993 .qdev
.vmsd
= &vmstate_sysbus_fdc
,
1994 .qdev
.reset
= fdctrl_external_reset_sysbus
,
1995 .qdev
.props
= (Property
[]) {
1996 DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus
, state
.drives
[0].bs
),
1997 DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus
, state
.drives
[1].bs
),
1998 DEFINE_PROP_END_OF_LIST(),
2002 static SysBusDeviceInfo sun4m_fdc_info
= {
2003 .init
= sun4m_fdc_init1
,
2004 .qdev
.name
= "SUNW,fdtwo",
2005 .qdev
.size
= sizeof(FDCtrlSysBus
),
2006 .qdev
.vmsd
= &vmstate_sysbus_fdc
,
2007 .qdev
.reset
= fdctrl_external_reset_sysbus
,
2008 .qdev
.props
= (Property
[]) {
2009 DEFINE_PROP_DRIVE("drive", FDCtrlSysBus
, state
.drives
[0].bs
),
2010 DEFINE_PROP_END_OF_LIST(),
2014 static void fdc_register_devices(void)
2016 isa_qdev_register(&isa_fdc_info
);
2017 sysbus_register_withprop(&sysbus_fdc_info
);
2018 sysbus_register_withprop(&sun4m_fdc_info
);
2021 device_init(fdc_register_devices
)