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"
39 #include "block_int.h"
41 /********************************************************/
42 /* debug Floppy devices */
43 //#define DEBUG_FLOPPY
46 #define FLOPPY_DPRINTF(fmt, ...) \
47 do { printf("FLOPPY: " fmt , ## __VA_ARGS__); } while (0)
49 #define FLOPPY_DPRINTF(fmt, ...)
52 #define FLOPPY_ERROR(fmt, ...) \
53 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
55 /********************************************************/
56 /* Floppy drive emulation */
58 #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
59 #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))
61 /* Will always be a fixed parameter for us */
62 #define FD_SECTOR_LEN 512
63 #define FD_SECTOR_SC 2 /* Sector size code */
64 #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */
66 /* Floppy disk drive emulation */
67 typedef enum FDiskFlags
{
68 FDISK_DBL_SIDES
= 0x01,
71 typedef struct FDrive
{
75 uint8_t perpendicular
; /* 2.88 MB access mode */
82 uint8_t last_sect
; /* Nb sector per track */
83 uint8_t max_track
; /* Nb of tracks */
84 uint16_t bps
; /* Bytes per sector */
85 uint8_t ro
; /* Is read-only */
86 uint8_t media_changed
; /* Is media changed */
89 static void fd_init(FDrive
*drv
)
92 drv
->drive
= FDRIVE_DRV_NONE
;
93 drv
->perpendicular
= 0;
99 static int fd_sector_calc(uint8_t head
, uint8_t track
, uint8_t sect
,
102 return (((track
* 2) + head
) * last_sect
) + sect
- 1;
105 /* Returns current position, in sectors, for given drive */
106 static int fd_sector(FDrive
*drv
)
108 return fd_sector_calc(drv
->head
, drv
->track
, drv
->sect
, drv
->last_sect
);
111 /* Seek to a new position:
112 * returns 0 if already on right track
113 * returns 1 if track changed
114 * returns 2 if track is invalid
115 * returns 3 if sector is invalid
116 * returns 4 if seek is disabled
118 static int fd_seek(FDrive
*drv
, uint8_t head
, uint8_t track
, uint8_t sect
,
124 if (track
> drv
->max_track
||
125 (head
!= 0 && (drv
->flags
& FDISK_DBL_SIDES
) == 0)) {
126 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
127 head
, track
, sect
, 1,
128 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
129 drv
->max_track
, drv
->last_sect
);
132 if (sect
> drv
->last_sect
) {
133 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
134 head
, track
, sect
, 1,
135 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
136 drv
->max_track
, drv
->last_sect
);
139 sector
= fd_sector_calc(head
, track
, sect
, drv
->last_sect
);
141 if (sector
!= fd_sector(drv
)) {
144 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
145 head
, track
, sect
, 1, drv
->max_track
, drv
->last_sect
);
150 if (drv
->track
!= track
)
159 /* Set drive back to track 0 */
160 static void fd_recalibrate(FDrive
*drv
)
162 FLOPPY_DPRINTF("recalibrate\n");
168 /* Revalidate a disk drive after a disk change */
169 static void fd_revalidate(FDrive
*drv
)
171 int nb_heads
, max_track
, last_sect
, ro
;
174 FLOPPY_DPRINTF("revalidate\n");
175 if (drv
->bs
!= NULL
&& bdrv_is_inserted(drv
->bs
)) {
176 ro
= bdrv_is_read_only(drv
->bs
);
177 bdrv_get_floppy_geometry_hint(drv
->bs
, &nb_heads
, &max_track
,
178 &last_sect
, drv
->drive
, &drive
);
179 if (nb_heads
!= 0 && max_track
!= 0 && last_sect
!= 0) {
180 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
181 nb_heads
- 1, max_track
, last_sect
);
183 FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", nb_heads
,
184 max_track
, last_sect
, ro
? "ro" : "rw");
187 drv
->flags
&= ~FDISK_DBL_SIDES
;
189 drv
->flags
|= FDISK_DBL_SIDES
;
191 drv
->max_track
= max_track
;
192 drv
->last_sect
= last_sect
;
196 FLOPPY_DPRINTF("No disk in drive\n");
199 drv
->flags
&= ~FDISK_DBL_SIDES
;
203 /********************************************************/
204 /* Intel 82078 floppy disk controller emulation */
206 typedef struct FDCtrl FDCtrl
;
208 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
);
209 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
);
210 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
211 int dma_pos
, int dma_len
);
212 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
);
214 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
);
215 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
);
216 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
);
217 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
);
218 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
);
219 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
);
220 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
);
221 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
);
222 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
);
223 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
);
224 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
);
235 FD_STATE_MULTI
= 0x01, /* multi track flag */
236 FD_STATE_FORMAT
= 0x02, /* format flag */
237 FD_STATE_SEEK
= 0x04, /* seek flag */
252 FD_CMD_READ_TRACK
= 0x02,
253 FD_CMD_SPECIFY
= 0x03,
254 FD_CMD_SENSE_DRIVE_STATUS
= 0x04,
257 FD_CMD_RECALIBRATE
= 0x07,
258 FD_CMD_SENSE_INTERRUPT_STATUS
= 0x08,
259 FD_CMD_WRITE_DELETED
= 0x09,
260 FD_CMD_READ_ID
= 0x0a,
261 FD_CMD_READ_DELETED
= 0x0c,
262 FD_CMD_FORMAT_TRACK
= 0x0d,
263 FD_CMD_DUMPREG
= 0x0e,
265 FD_CMD_VERSION
= 0x10,
266 FD_CMD_SCAN_EQUAL
= 0x11,
267 FD_CMD_PERPENDICULAR_MODE
= 0x12,
268 FD_CMD_CONFIGURE
= 0x13,
270 FD_CMD_VERIFY
= 0x16,
271 FD_CMD_POWERDOWN_MODE
= 0x17,
272 FD_CMD_PART_ID
= 0x18,
273 FD_CMD_SCAN_LOW_OR_EQUAL
= 0x19,
274 FD_CMD_SCAN_HIGH_OR_EQUAL
= 0x1d,
276 FD_CMD_OPTION
= 0x33,
277 FD_CMD_RESTORE
= 0x4e,
278 FD_CMD_DRIVE_SPECIFICATION_COMMAND
= 0x8e,
279 FD_CMD_RELATIVE_SEEK_OUT
= 0x8f,
280 FD_CMD_FORMAT_AND_WRITE
= 0xcd,
281 FD_CMD_RELATIVE_SEEK_IN
= 0xcf,
285 FD_CONFIG_PRETRK
= 0xff, /* Pre-compensation set to track 0 */
286 FD_CONFIG_FIFOTHR
= 0x0f, /* FIFO threshold set to 1 byte */
287 FD_CONFIG_POLL
= 0x10, /* Poll enabled */
288 FD_CONFIG_EFIFO
= 0x20, /* FIFO disabled */
289 FD_CONFIG_EIS
= 0x40, /* No implied seeks */
295 FD_SR0_ABNTERM
= 0x40,
296 FD_SR0_INVCMD
= 0x80,
297 FD_SR0_RDYCHG
= 0xc0,
301 FD_SR1_EC
= 0x80, /* End of cylinder */
305 FD_SR2_SNS
= 0x04, /* Scan not satisfied */
306 FD_SR2_SEH
= 0x08, /* Scan equal hit */
317 FD_SRA_INTPEND
= 0x80,
331 FD_DOR_SELMASK
= 0x03,
333 FD_DOR_SELMASK
= 0x01,
335 FD_DOR_nRESET
= 0x04,
337 FD_DOR_MOTEN0
= 0x10,
338 FD_DOR_MOTEN1
= 0x20,
339 FD_DOR_MOTEN2
= 0x40,
340 FD_DOR_MOTEN3
= 0x80,
345 FD_TDR_BOOTSEL
= 0x0c,
347 FD_TDR_BOOTSEL
= 0x04,
352 FD_DSR_DRATEMASK
= 0x03,
353 FD_DSR_PWRDOWN
= 0x40,
354 FD_DSR_SWRESET
= 0x80,
358 FD_MSR_DRV0BUSY
= 0x01,
359 FD_MSR_DRV1BUSY
= 0x02,
360 FD_MSR_DRV2BUSY
= 0x04,
361 FD_MSR_DRV3BUSY
= 0x08,
362 FD_MSR_CMDBUSY
= 0x10,
363 FD_MSR_NONDMA
= 0x20,
369 FD_DIR_DSKCHG
= 0x80,
372 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
373 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
374 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
377 /* Controller's identification */
382 /* Controller state */
383 QEMUTimer
*result_timer
;
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 */
407 /* precompensation */
411 /* Power down config (also with status regB access mode */
416 uint8_t num_floppies
;
417 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
;
440 retval
= fdctrl_read_statusA(fdctrl
);
443 retval
= fdctrl_read_statusB(fdctrl
);
446 retval
= fdctrl_read_dor(fdctrl
);
449 retval
= fdctrl_read_tape(fdctrl
);
452 retval
= fdctrl_read_main_status(fdctrl
);
455 retval
= fdctrl_read_data(fdctrl
);
458 retval
= fdctrl_read_dir(fdctrl
);
461 retval
= (uint32_t)(-1);
464 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg
& 7, retval
);
469 static void fdctrl_write (void *opaque
, uint32_t reg
, uint32_t value
)
471 FDCtrl
*fdctrl
= opaque
;
473 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg
& 7, value
);
477 fdctrl_write_dor(fdctrl
, value
);
480 fdctrl_write_tape(fdctrl
, value
);
483 fdctrl_write_rate(fdctrl
, value
);
486 fdctrl_write_data(fdctrl
, value
);
493 static uint32_t fdctrl_read_port (void *opaque
, uint32_t reg
)
495 return fdctrl_read(opaque
, reg
& 7);
498 static void fdctrl_write_port (void *opaque
, uint32_t reg
, uint32_t value
)
500 fdctrl_write(opaque
, reg
& 7, value
);
503 static uint32_t fdctrl_read_mem (void *opaque
, target_phys_addr_t reg
)
505 return fdctrl_read(opaque
, (uint32_t)reg
);
508 static void fdctrl_write_mem (void *opaque
,
509 target_phys_addr_t reg
, uint32_t value
)
511 fdctrl_write(opaque
, (uint32_t)reg
, value
);
514 static CPUReadMemoryFunc
* const fdctrl_mem_read
[3] = {
520 static CPUWriteMemoryFunc
* const fdctrl_mem_write
[3] = {
526 static CPUReadMemoryFunc
* const fdctrl_mem_read_strict
[3] = {
532 static CPUWriteMemoryFunc
* const fdctrl_mem_write_strict
[3] = {
538 static void fdrive_media_changed_pre_save(void *opaque
)
540 FDrive
*drive
= opaque
;
542 drive
->media_changed
= drive
->bs
->media_changed
;
545 static int fdrive_media_changed_post_load(void *opaque
, int version_id
)
547 FDrive
*drive
= opaque
;
549 if (drive
->bs
!= NULL
) {
550 drive
->bs
->media_changed
= drive
->media_changed
;
553 /* User ejected the floppy when drive->bs == NULL */
557 static bool fdrive_media_changed_needed(void *opaque
)
559 FDrive
*drive
= opaque
;
561 return (drive
->bs
!= NULL
&& drive
->bs
->media_changed
!= 1);
564 static const VMStateDescription vmstate_fdrive_media_changed
= {
565 .name
= "fdrive/media_changed",
567 .minimum_version_id
= 1,
568 .minimum_version_id_old
= 1,
569 .pre_save
= fdrive_media_changed_pre_save
,
570 .post_load
= fdrive_media_changed_post_load
,
571 .fields
= (VMStateField
[]) {
572 VMSTATE_UINT8(media_changed
, FDrive
),
573 VMSTATE_END_OF_LIST()
577 static const VMStateDescription vmstate_fdrive
= {
580 .minimum_version_id
= 1,
581 .minimum_version_id_old
= 1,
582 .fields
= (VMStateField
[]) {
583 VMSTATE_UINT8(head
, FDrive
),
584 VMSTATE_UINT8(track
, FDrive
),
585 VMSTATE_UINT8(sect
, FDrive
),
586 VMSTATE_END_OF_LIST()
588 .subsections
= (VMStateSubsection
[]) {
590 .vmsd
= &vmstate_fdrive_media_changed
,
591 .needed
= &fdrive_media_changed_needed
,
598 static void fdc_pre_save(void *opaque
)
602 s
->dor_vmstate
= s
->dor
| GET_CUR_DRV(s
);
605 static int fdc_post_load(void *opaque
, int version_id
)
609 SET_CUR_DRV(s
, s
->dor_vmstate
& FD_DOR_SELMASK
);
610 s
->dor
= s
->dor_vmstate
& ~FD_DOR_SELMASK
;
614 static const VMStateDescription vmstate_fdc
= {
617 .minimum_version_id
= 2,
618 .minimum_version_id_old
= 2,
619 .pre_save
= fdc_pre_save
,
620 .post_load
= fdc_post_load
,
621 .fields
= (VMStateField
[]) {
622 /* Controller State */
623 VMSTATE_UINT8(sra
, FDCtrl
),
624 VMSTATE_UINT8(srb
, FDCtrl
),
625 VMSTATE_UINT8(dor_vmstate
, FDCtrl
),
626 VMSTATE_UINT8(tdr
, FDCtrl
),
627 VMSTATE_UINT8(dsr
, FDCtrl
),
628 VMSTATE_UINT8(msr
, FDCtrl
),
629 VMSTATE_UINT8(status0
, FDCtrl
),
630 VMSTATE_UINT8(status1
, FDCtrl
),
631 VMSTATE_UINT8(status2
, FDCtrl
),
633 VMSTATE_VARRAY_INT32(fifo
, FDCtrl
, fifo_size
, 0, vmstate_info_uint8
,
635 VMSTATE_UINT32(data_pos
, FDCtrl
),
636 VMSTATE_UINT32(data_len
, FDCtrl
),
637 VMSTATE_UINT8(data_state
, FDCtrl
),
638 VMSTATE_UINT8(data_dir
, FDCtrl
),
639 VMSTATE_UINT8(eot
, FDCtrl
),
640 /* States kept only to be returned back */
641 VMSTATE_UINT8(timer0
, FDCtrl
),
642 VMSTATE_UINT8(timer1
, FDCtrl
),
643 VMSTATE_UINT8(precomp_trk
, FDCtrl
),
644 VMSTATE_UINT8(config
, FDCtrl
),
645 VMSTATE_UINT8(lock
, FDCtrl
),
646 VMSTATE_UINT8(pwrd
, FDCtrl
),
647 VMSTATE_UINT8_EQUAL(num_floppies
, FDCtrl
),
648 VMSTATE_STRUCT_ARRAY(drives
, FDCtrl
, MAX_FD
, 1,
649 vmstate_fdrive
, FDrive
),
650 VMSTATE_END_OF_LIST()
654 static void fdctrl_external_reset_sysbus(DeviceState
*d
)
656 FDCtrlSysBus
*sys
= container_of(d
, FDCtrlSysBus
, busdev
.qdev
);
657 FDCtrl
*s
= &sys
->state
;
662 static void fdctrl_external_reset_isa(DeviceState
*d
)
664 FDCtrlISABus
*isa
= container_of(d
, FDCtrlISABus
, busdev
.qdev
);
665 FDCtrl
*s
= &isa
->state
;
670 static void fdctrl_handle_tc(void *opaque
, int irq
, int level
)
672 //FDCtrl *s = opaque;
676 FLOPPY_DPRINTF("TC pulsed\n");
680 /* Change IRQ state */
681 static void fdctrl_reset_irq(FDCtrl
*fdctrl
)
683 if (!(fdctrl
->sra
& FD_SRA_INTPEND
))
685 FLOPPY_DPRINTF("Reset interrupt\n");
686 qemu_set_irq(fdctrl
->irq
, 0);
687 fdctrl
->sra
&= ~FD_SRA_INTPEND
;
690 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
)
693 if (fdctrl
->sun4m
&& (fdctrl
->msr
& FD_MSR_CMDBUSY
)) {
695 fdctrl
->msr
&= ~FD_MSR_CMDBUSY
;
696 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
697 fdctrl
->status0
= status0
;
700 if (!(fdctrl
->sra
& FD_SRA_INTPEND
)) {
701 qemu_set_irq(fdctrl
->irq
, 1);
702 fdctrl
->sra
|= FD_SRA_INTPEND
;
704 fdctrl
->reset_sensei
= 0;
705 fdctrl
->status0
= status0
;
706 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl
->status0
);
709 /* Reset controller */
710 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
)
714 FLOPPY_DPRINTF("reset controller\n");
715 fdctrl_reset_irq(fdctrl
);
716 /* Initialise controller */
719 if (!fdctrl
->drives
[1].bs
)
720 fdctrl
->sra
|= FD_SRA_nDRV2
;
722 fdctrl
->dor
= FD_DOR_nRESET
;
723 fdctrl
->dor
|= (fdctrl
->dma_chann
!= -1) ? FD_DOR_DMAEN
: 0;
724 fdctrl
->msr
= FD_MSR_RQM
;
726 fdctrl
->data_pos
= 0;
727 fdctrl
->data_len
= 0;
728 fdctrl
->data_state
= 0;
729 fdctrl
->data_dir
= FD_DIR_WRITE
;
730 for (i
= 0; i
< MAX_FD
; i
++)
731 fd_recalibrate(&fdctrl
->drives
[i
]);
732 fdctrl_reset_fifo(fdctrl
);
734 fdctrl_raise_irq(fdctrl
, FD_SR0_RDYCHG
);
735 fdctrl
->reset_sensei
= FD_RESET_SENSEI_COUNT
;
739 static inline FDrive
*drv0(FDCtrl
*fdctrl
)
741 return &fdctrl
->drives
[(fdctrl
->tdr
& FD_TDR_BOOTSEL
) >> 2];
744 static inline FDrive
*drv1(FDCtrl
*fdctrl
)
746 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (1 << 2))
747 return &fdctrl
->drives
[1];
749 return &fdctrl
->drives
[0];
753 static inline FDrive
*drv2(FDCtrl
*fdctrl
)
755 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (2 << 2))
756 return &fdctrl
->drives
[2];
758 return &fdctrl
->drives
[1];
761 static inline FDrive
*drv3(FDCtrl
*fdctrl
)
763 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (3 << 2))
764 return &fdctrl
->drives
[3];
766 return &fdctrl
->drives
[2];
770 static FDrive
*get_cur_drv(FDCtrl
*fdctrl
)
772 switch (fdctrl
->cur_drv
) {
773 case 0: return drv0(fdctrl
);
774 case 1: return drv1(fdctrl
);
776 case 2: return drv2(fdctrl
);
777 case 3: return drv3(fdctrl
);
779 default: return NULL
;
783 /* Status A register : 0x00 (read-only) */
784 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
)
786 uint32_t retval
= fdctrl
->sra
;
788 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval
);
793 /* Status B register : 0x01 (read-only) */
794 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
)
796 uint32_t retval
= fdctrl
->srb
;
798 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval
);
803 /* Digital output register : 0x02 */
804 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
)
806 uint32_t retval
= fdctrl
->dor
;
809 retval
|= fdctrl
->cur_drv
;
810 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
815 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
)
817 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
820 if (value
& FD_DOR_MOTEN0
)
821 fdctrl
->srb
|= FD_SRB_MTR0
;
823 fdctrl
->srb
&= ~FD_SRB_MTR0
;
824 if (value
& FD_DOR_MOTEN1
)
825 fdctrl
->srb
|= FD_SRB_MTR1
;
827 fdctrl
->srb
&= ~FD_SRB_MTR1
;
831 fdctrl
->srb
|= FD_SRB_DR0
;
833 fdctrl
->srb
&= ~FD_SRB_DR0
;
836 if (!(value
& FD_DOR_nRESET
)) {
837 if (fdctrl
->dor
& FD_DOR_nRESET
) {
838 FLOPPY_DPRINTF("controller enter RESET state\n");
841 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
842 FLOPPY_DPRINTF("controller out of RESET state\n");
843 fdctrl_reset(fdctrl
, 1);
844 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
848 fdctrl
->cur_drv
= value
& FD_DOR_SELMASK
;
853 /* Tape drive register : 0x03 */
854 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
)
856 uint32_t retval
= fdctrl
->tdr
;
858 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
863 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
)
866 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
867 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
870 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
871 /* Disk boot selection indicator */
872 fdctrl
->tdr
= value
& FD_TDR_BOOTSEL
;
873 /* Tape indicators: never allow */
876 /* Main status register : 0x04 (read) */
877 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
)
879 uint32_t retval
= fdctrl
->msr
;
881 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
882 fdctrl
->dor
|= FD_DOR_nRESET
;
886 retval
|= FD_MSR_DIO
;
887 fdctrl_reset_irq(fdctrl
);
890 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
895 /* Data select rate register : 0x04 (write) */
896 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
)
899 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
900 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
903 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
904 /* Reset: autoclear */
905 if (value
& FD_DSR_SWRESET
) {
906 fdctrl
->dor
&= ~FD_DOR_nRESET
;
907 fdctrl_reset(fdctrl
, 1);
908 fdctrl
->dor
|= FD_DOR_nRESET
;
910 if (value
& FD_DSR_PWRDOWN
) {
911 fdctrl_reset(fdctrl
, 1);
916 static int fdctrl_media_changed(FDrive
*drv
)
922 ret
= bdrv_media_changed(drv
->bs
);
929 /* Digital input register : 0x07 (read-only) */
930 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
)
934 if (fdctrl_media_changed(drv0(fdctrl
))
935 || fdctrl_media_changed(drv1(fdctrl
))
937 || fdctrl_media_changed(drv2(fdctrl
))
938 || fdctrl_media_changed(drv3(fdctrl
))
941 retval
|= FD_DIR_DSKCHG
;
943 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
949 /* FIFO state control */
950 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
)
952 fdctrl
->data_dir
= FD_DIR_WRITE
;
953 fdctrl
->data_pos
= 0;
954 fdctrl
->msr
&= ~(FD_MSR_CMDBUSY
| FD_MSR_DIO
);
957 /* Set FIFO status for the host to read */
958 static void fdctrl_set_fifo(FDCtrl
*fdctrl
, int fifo_len
, int do_irq
)
960 fdctrl
->data_dir
= FD_DIR_READ
;
961 fdctrl
->data_len
= fifo_len
;
962 fdctrl
->data_pos
= 0;
963 fdctrl
->msr
|= FD_MSR_CMDBUSY
| FD_MSR_RQM
| FD_MSR_DIO
;
965 fdctrl_raise_irq(fdctrl
, 0x00);
968 /* Set an error: unimplemented/unknown command */
969 static void fdctrl_unimplemented(FDCtrl
*fdctrl
, int direction
)
971 FLOPPY_ERROR("unimplemented command 0x%02x\n", fdctrl
->fifo
[0]);
972 fdctrl
->fifo
[0] = FD_SR0_INVCMD
;
973 fdctrl_set_fifo(fdctrl
, 1, 0);
976 /* Seek to next sector */
977 static int fdctrl_seek_to_next_sect(FDCtrl
*fdctrl
, FDrive
*cur_drv
)
979 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
980 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
982 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
984 if (cur_drv
->sect
>= cur_drv
->last_sect
||
985 cur_drv
->sect
== fdctrl
->eot
) {
987 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
988 if (cur_drv
->head
== 0 &&
989 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
994 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0)
1001 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1002 cur_drv
->head
, cur_drv
->track
,
1003 cur_drv
->sect
, fd_sector(cur_drv
));
1010 /* Callback for transfer end (stop or abort) */
1011 static void fdctrl_stop_transfer(FDCtrl
*fdctrl
, uint8_t status0
,
1012 uint8_t status1
, uint8_t status2
)
1016 cur_drv
= get_cur_drv(fdctrl
);
1017 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1018 status0
, status1
, status2
,
1019 status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
));
1020 fdctrl
->fifo
[0] = status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1021 fdctrl
->fifo
[1] = status1
;
1022 fdctrl
->fifo
[2] = status2
;
1023 fdctrl
->fifo
[3] = cur_drv
->track
;
1024 fdctrl
->fifo
[4] = cur_drv
->head
;
1025 fdctrl
->fifo
[5] = cur_drv
->sect
;
1026 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
1027 fdctrl
->data_dir
= FD_DIR_READ
;
1028 if (!(fdctrl
->msr
& FD_MSR_NONDMA
)) {
1029 DMA_release_DREQ(fdctrl
->dma_chann
);
1031 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
1032 fdctrl
->msr
&= ~FD_MSR_NONDMA
;
1033 fdctrl_set_fifo(fdctrl
, 7, 1);
1036 /* Prepare a data transfer (either DMA or FIFO) */
1037 static void fdctrl_start_transfer(FDCtrl
*fdctrl
, int direction
)
1043 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1044 cur_drv
= get_cur_drv(fdctrl
);
1045 kt
= fdctrl
->fifo
[2];
1046 kh
= fdctrl
->fifo
[3];
1047 ks
= fdctrl
->fifo
[4];
1048 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1049 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1050 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
));
1051 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1054 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1055 fdctrl
->fifo
[3] = kt
;
1056 fdctrl
->fifo
[4] = kh
;
1057 fdctrl
->fifo
[5] = ks
;
1061 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1062 fdctrl
->fifo
[3] = kt
;
1063 fdctrl
->fifo
[4] = kh
;
1064 fdctrl
->fifo
[5] = ks
;
1067 /* No seek enabled */
1068 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1069 fdctrl
->fifo
[3] = kt
;
1070 fdctrl
->fifo
[4] = kh
;
1071 fdctrl
->fifo
[5] = ks
;
1080 /* Set the FIFO state */
1081 fdctrl
->data_dir
= direction
;
1082 fdctrl
->data_pos
= 0;
1083 fdctrl
->msr
|= FD_MSR_CMDBUSY
;
1084 if (fdctrl
->fifo
[0] & 0x80)
1085 fdctrl
->data_state
|= FD_STATE_MULTI
;
1087 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1089 fdctrl
->data_state
|= FD_STATE_SEEK
;
1091 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1092 if (fdctrl
->fifo
[5] == 00) {
1093 fdctrl
->data_len
= fdctrl
->fifo
[8];
1096 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
1097 tmp
= (fdctrl
->fifo
[6] - ks
+ 1);
1098 if (fdctrl
->fifo
[0] & 0x80)
1099 tmp
+= fdctrl
->fifo
[6];
1100 fdctrl
->data_len
*= tmp
;
1102 fdctrl
->eot
= fdctrl
->fifo
[6];
1103 if (fdctrl
->dor
& FD_DOR_DMAEN
) {
1105 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1106 dma_mode
= DMA_get_channel_mode(fdctrl
->dma_chann
);
1107 dma_mode
= (dma_mode
>> 2) & 3;
1108 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1109 dma_mode
, direction
,
1110 (128 << fdctrl
->fifo
[5]) *
1111 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
1112 if (((direction
== FD_DIR_SCANE
|| direction
== FD_DIR_SCANL
||
1113 direction
== FD_DIR_SCANH
) && dma_mode
== 0) ||
1114 (direction
== FD_DIR_WRITE
&& dma_mode
== 2) ||
1115 (direction
== FD_DIR_READ
&& dma_mode
== 1)) {
1116 /* No access is allowed until DMA transfer has completed */
1117 fdctrl
->msr
&= ~FD_MSR_RQM
;
1118 /* Now, we just have to wait for the DMA controller to
1121 DMA_hold_DREQ(fdctrl
->dma_chann
);
1122 DMA_schedule(fdctrl
->dma_chann
);
1125 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode
, direction
);
1128 FLOPPY_DPRINTF("start non-DMA transfer\n");
1129 fdctrl
->msr
|= FD_MSR_NONDMA
;
1130 if (direction
!= FD_DIR_WRITE
)
1131 fdctrl
->msr
|= FD_MSR_DIO
;
1132 /* IO based transfer: calculate len */
1133 fdctrl_raise_irq(fdctrl
, 0x00);
1138 /* Prepare a transfer of deleted data */
1139 static void fdctrl_start_transfer_del(FDCtrl
*fdctrl
, int direction
)
1141 FLOPPY_ERROR("fdctrl_start_transfer_del() unimplemented\n");
1143 /* We don't handle deleted data,
1144 * so we don't return *ANYTHING*
1146 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1149 /* handlers for DMA transfers */
1150 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
1151 int dma_pos
, int dma_len
)
1155 int len
, start_pos
, rel_pos
;
1156 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
1159 if (fdctrl
->msr
& FD_MSR_RQM
) {
1160 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1163 cur_drv
= get_cur_drv(fdctrl
);
1164 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
1165 fdctrl
->data_dir
== FD_DIR_SCANH
)
1166 status2
= FD_SR2_SNS
;
1167 if (dma_len
> fdctrl
->data_len
)
1168 dma_len
= fdctrl
->data_len
;
1169 if (cur_drv
->bs
== NULL
) {
1170 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
1171 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1173 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1175 goto transfer_error
;
1177 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1178 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
1179 len
= dma_len
- fdctrl
->data_pos
;
1180 if (len
+ rel_pos
> FD_SECTOR_LEN
)
1181 len
= FD_SECTOR_LEN
- rel_pos
;
1182 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1183 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
1184 fdctrl
->data_len
, GET_CUR_DRV(fdctrl
), cur_drv
->head
,
1185 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
1186 fd_sector(cur_drv
) * FD_SECTOR_LEN
);
1187 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
1188 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
1189 /* READ & SCAN commands and realign to a sector for WRITE */
1190 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1191 fdctrl
->fifo
, 1) < 0) {
1192 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1193 fd_sector(cur_drv
));
1194 /* Sure, image size is too small... */
1195 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1198 switch (fdctrl
->data_dir
) {
1201 DMA_write_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1202 fdctrl
->data_pos
, len
);
1205 /* WRITE commands */
1206 DMA_read_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1207 fdctrl
->data_pos
, len
);
1208 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1209 fdctrl
->fifo
, 1) < 0) {
1210 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv
));
1211 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1212 goto transfer_error
;
1218 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1220 DMA_read_memory (nchan
, tmpbuf
, fdctrl
->data_pos
, len
);
1221 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1223 status2
= FD_SR2_SEH
;
1226 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1227 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1234 fdctrl
->data_pos
+= len
;
1235 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1237 /* Seek to next sector */
1238 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
))
1243 len
= fdctrl
->data_pos
- start_pos
;
1244 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1245 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1246 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1247 fdctrl
->data_dir
== FD_DIR_SCANL
||
1248 fdctrl
->data_dir
== FD_DIR_SCANH
)
1249 status2
= FD_SR2_SEH
;
1250 if (FD_DID_SEEK(fdctrl
->data_state
))
1251 status0
|= FD_SR0_SEEK
;
1252 fdctrl
->data_len
-= len
;
1253 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1259 /* Data register : 0x05 */
1260 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
)
1263 uint32_t retval
= 0;
1266 cur_drv
= get_cur_drv(fdctrl
);
1267 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1268 if (!(fdctrl
->msr
& FD_MSR_RQM
) || !(fdctrl
->msr
& FD_MSR_DIO
)) {
1269 FLOPPY_ERROR("controller not ready for reading\n");
1272 pos
= fdctrl
->data_pos
;
1273 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1274 pos
%= FD_SECTOR_LEN
;
1276 if (fdctrl
->data_pos
!= 0)
1277 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1278 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1279 fd_sector(cur_drv
));
1282 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1283 FLOPPY_DPRINTF("error getting sector %d\n",
1284 fd_sector(cur_drv
));
1285 /* Sure, image size is too small... */
1286 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1290 retval
= fdctrl
->fifo
[pos
];
1291 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1292 fdctrl
->data_pos
= 0;
1293 /* Switch from transfer mode to status mode
1294 * then from status mode to command mode
1296 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1297 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1299 fdctrl_reset_fifo(fdctrl
);
1300 fdctrl_reset_irq(fdctrl
);
1303 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1308 static void fdctrl_format_sector(FDCtrl
*fdctrl
)
1313 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1314 cur_drv
= get_cur_drv(fdctrl
);
1315 kt
= fdctrl
->fifo
[6];
1316 kh
= fdctrl
->fifo
[7];
1317 ks
= fdctrl
->fifo
[8];
1318 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1319 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1320 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
));
1321 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1324 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1325 fdctrl
->fifo
[3] = kt
;
1326 fdctrl
->fifo
[4] = kh
;
1327 fdctrl
->fifo
[5] = ks
;
1331 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1332 fdctrl
->fifo
[3] = kt
;
1333 fdctrl
->fifo
[4] = kh
;
1334 fdctrl
->fifo
[5] = ks
;
1337 /* No seek enabled */
1338 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1339 fdctrl
->fifo
[3] = kt
;
1340 fdctrl
->fifo
[4] = kh
;
1341 fdctrl
->fifo
[5] = ks
;
1344 fdctrl
->data_state
|= FD_STATE_SEEK
;
1349 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1350 if (cur_drv
->bs
== NULL
||
1351 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1352 FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv
));
1353 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1355 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1356 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1357 /* Last sector done */
1358 if (FD_DID_SEEK(fdctrl
->data_state
))
1359 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1361 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1364 fdctrl
->data_pos
= 0;
1365 fdctrl
->data_len
= 4;
1370 static void fdctrl_handle_lock(FDCtrl
*fdctrl
, int direction
)
1372 fdctrl
->lock
= (fdctrl
->fifo
[0] & 0x80) ? 1 : 0;
1373 fdctrl
->fifo
[0] = fdctrl
->lock
<< 4;
1374 fdctrl_set_fifo(fdctrl
, 1, fdctrl
->lock
);
1377 static void fdctrl_handle_dumpreg(FDCtrl
*fdctrl
, int direction
)
1379 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1381 /* Drives position */
1382 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1383 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1385 fdctrl
->fifo
[2] = drv2(fdctrl
)->track
;
1386 fdctrl
->fifo
[3] = drv3(fdctrl
)->track
;
1388 fdctrl
->fifo
[2] = 0;
1389 fdctrl
->fifo
[3] = 0;
1392 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1393 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | (fdctrl
->dor
& FD_DOR_DMAEN
? 1 : 0);
1394 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1395 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1396 (cur_drv
->perpendicular
<< 2);
1397 fdctrl
->fifo
[8] = fdctrl
->config
;
1398 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1399 fdctrl_set_fifo(fdctrl
, 10, 0);
1402 static void fdctrl_handle_version(FDCtrl
*fdctrl
, int direction
)
1404 /* Controller's version */
1405 fdctrl
->fifo
[0] = fdctrl
->version
;
1406 fdctrl_set_fifo(fdctrl
, 1, 1);
1409 static void fdctrl_handle_partid(FDCtrl
*fdctrl
, int direction
)
1411 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1412 fdctrl_set_fifo(fdctrl
, 1, 0);
1415 static void fdctrl_handle_restore(FDCtrl
*fdctrl
, int direction
)
1417 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1419 /* Drives position */
1420 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1421 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1423 drv2(fdctrl
)->track
= fdctrl
->fifo
[5];
1424 drv3(fdctrl
)->track
= fdctrl
->fifo
[6];
1427 fdctrl
->timer0
= fdctrl
->fifo
[7];
1428 fdctrl
->timer1
= fdctrl
->fifo
[8];
1429 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1430 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1431 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1432 fdctrl
->config
= fdctrl
->fifo
[11];
1433 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1434 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1435 fdctrl_reset_fifo(fdctrl
);
1438 static void fdctrl_handle_save(FDCtrl
*fdctrl
, int direction
)
1440 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1442 fdctrl
->fifo
[0] = 0;
1443 fdctrl
->fifo
[1] = 0;
1444 /* Drives position */
1445 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1446 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1448 fdctrl
->fifo
[4] = drv2(fdctrl
)->track
;
1449 fdctrl
->fifo
[5] = drv3(fdctrl
)->track
;
1451 fdctrl
->fifo
[4] = 0;
1452 fdctrl
->fifo
[5] = 0;
1455 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1456 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1457 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1458 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1459 (cur_drv
->perpendicular
<< 2);
1460 fdctrl
->fifo
[10] = fdctrl
->config
;
1461 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1462 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1463 fdctrl
->fifo
[13] = 0;
1464 fdctrl
->fifo
[14] = 0;
1465 fdctrl_set_fifo(fdctrl
, 15, 1);
1468 static void fdctrl_handle_readid(FDCtrl
*fdctrl
, int direction
)
1470 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1472 /* XXX: should set main status register to busy */
1473 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1474 qemu_mod_timer(fdctrl
->result_timer
,
1475 qemu_get_clock_ns(vm_clock
) + (get_ticks_per_sec() / 50));
1478 static void fdctrl_handle_format_track(FDCtrl
*fdctrl
, int direction
)
1482 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1483 cur_drv
= get_cur_drv(fdctrl
);
1484 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1485 if (fdctrl
->fifo
[0] & 0x80)
1486 fdctrl
->data_state
|= FD_STATE_MULTI
;
1488 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1489 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1491 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1493 cur_drv
->last_sect
=
1494 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1495 fdctrl
->fifo
[3] / 2;
1497 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1499 /* TODO: implement format using DMA expected by the Bochs BIOS
1500 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1501 * the sector with the specified fill byte
1503 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1504 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1507 static void fdctrl_handle_specify(FDCtrl
*fdctrl
, int direction
)
1509 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1510 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1511 if (fdctrl
->fifo
[2] & 1)
1512 fdctrl
->dor
&= ~FD_DOR_DMAEN
;
1514 fdctrl
->dor
|= FD_DOR_DMAEN
;
1515 /* No result back */
1516 fdctrl_reset_fifo(fdctrl
);
1519 static void fdctrl_handle_sense_drive_status(FDCtrl
*fdctrl
, int direction
)
1523 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1524 cur_drv
= get_cur_drv(fdctrl
);
1525 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1526 /* 1 Byte status back */
1527 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1528 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1529 (cur_drv
->head
<< 2) |
1530 GET_CUR_DRV(fdctrl
) |
1532 fdctrl_set_fifo(fdctrl
, 1, 0);
1535 static void fdctrl_handle_recalibrate(FDCtrl
*fdctrl
, int direction
)
1539 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1540 cur_drv
= get_cur_drv(fdctrl
);
1541 fd_recalibrate(cur_drv
);
1542 fdctrl_reset_fifo(fdctrl
);
1543 /* Raise Interrupt */
1544 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1547 static void fdctrl_handle_sense_interrupt_status(FDCtrl
*fdctrl
, int direction
)
1549 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1551 if(fdctrl
->reset_sensei
> 0) {
1553 FD_SR0_RDYCHG
+ FD_RESET_SENSEI_COUNT
- fdctrl
->reset_sensei
;
1554 fdctrl
->reset_sensei
--;
1556 /* XXX: status0 handling is broken for read/write
1557 commands, so we do this hack. It should be suppressed
1560 FD_SR0_SEEK
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1563 fdctrl
->fifo
[1] = cur_drv
->track
;
1564 fdctrl_set_fifo(fdctrl
, 2, 0);
1565 fdctrl_reset_irq(fdctrl
);
1566 fdctrl
->status0
= FD_SR0_RDYCHG
;
1569 static void fdctrl_handle_seek(FDCtrl
*fdctrl
, int direction
)
1573 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1574 cur_drv
= get_cur_drv(fdctrl
);
1575 fdctrl_reset_fifo(fdctrl
);
1576 if (fdctrl
->fifo
[2] > cur_drv
->max_track
) {
1577 fdctrl_raise_irq(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
);
1579 cur_drv
->track
= fdctrl
->fifo
[2];
1580 /* Raise Interrupt */
1581 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1585 static void fdctrl_handle_perpendicular_mode(FDCtrl
*fdctrl
, int direction
)
1587 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1589 if (fdctrl
->fifo
[1] & 0x80)
1590 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
1591 /* No result back */
1592 fdctrl_reset_fifo(fdctrl
);
1595 static void fdctrl_handle_configure(FDCtrl
*fdctrl
, int direction
)
1597 fdctrl
->config
= fdctrl
->fifo
[2];
1598 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
1599 /* No result back */
1600 fdctrl_reset_fifo(fdctrl
);
1603 static void fdctrl_handle_powerdown_mode(FDCtrl
*fdctrl
, int direction
)
1605 fdctrl
->pwrd
= fdctrl
->fifo
[1];
1606 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1607 fdctrl_set_fifo(fdctrl
, 1, 1);
1610 static void fdctrl_handle_option(FDCtrl
*fdctrl
, int direction
)
1612 /* No result back */
1613 fdctrl_reset_fifo(fdctrl
);
1616 static void fdctrl_handle_drive_specification_command(FDCtrl
*fdctrl
, int direction
)
1618 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1620 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x80) {
1621 /* Command parameters done */
1622 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x40) {
1623 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1624 fdctrl
->fifo
[2] = 0;
1625 fdctrl
->fifo
[3] = 0;
1626 fdctrl_set_fifo(fdctrl
, 4, 1);
1628 fdctrl_reset_fifo(fdctrl
);
1630 } else if (fdctrl
->data_len
> 7) {
1632 fdctrl
->fifo
[0] = 0x80 |
1633 (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1634 fdctrl_set_fifo(fdctrl
, 1, 1);
1638 static void fdctrl_handle_relative_seek_out(FDCtrl
*fdctrl
, int direction
)
1642 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1643 cur_drv
= get_cur_drv(fdctrl
);
1644 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
1645 cur_drv
->track
= cur_drv
->max_track
- 1;
1647 cur_drv
->track
+= fdctrl
->fifo
[2];
1649 fdctrl_reset_fifo(fdctrl
);
1650 /* Raise Interrupt */
1651 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1654 static void fdctrl_handle_relative_seek_in(FDCtrl
*fdctrl
, int direction
)
1658 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1659 cur_drv
= get_cur_drv(fdctrl
);
1660 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
1663 cur_drv
->track
-= fdctrl
->fifo
[2];
1665 fdctrl_reset_fifo(fdctrl
);
1666 /* Raise Interrupt */
1667 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1670 static const struct {
1675 void (*handler
)(FDCtrl
*fdctrl
, int direction
);
1678 { FD_CMD_READ
, 0x1f, "READ", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1679 { FD_CMD_WRITE
, 0x3f, "WRITE", 8, fdctrl_start_transfer
, FD_DIR_WRITE
},
1680 { FD_CMD_SEEK
, 0xff, "SEEK", 2, fdctrl_handle_seek
},
1681 { FD_CMD_SENSE_INTERRUPT_STATUS
, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status
},
1682 { FD_CMD_RECALIBRATE
, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate
},
1683 { FD_CMD_FORMAT_TRACK
, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track
},
1684 { FD_CMD_READ_TRACK
, 0xbf, "READ TRACK", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1685 { FD_CMD_RESTORE
, 0xff, "RESTORE", 17, fdctrl_handle_restore
}, /* part of READ DELETED DATA */
1686 { FD_CMD_SAVE
, 0xff, "SAVE", 0, fdctrl_handle_save
}, /* part of READ DELETED DATA */
1687 { FD_CMD_READ_DELETED
, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_READ
},
1688 { FD_CMD_SCAN_EQUAL
, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANE
},
1689 { FD_CMD_VERIFY
, 0x1f, "VERIFY", 8, fdctrl_unimplemented
},
1690 { FD_CMD_SCAN_LOW_OR_EQUAL
, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANL
},
1691 { FD_CMD_SCAN_HIGH_OR_EQUAL
, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANH
},
1692 { FD_CMD_WRITE_DELETED
, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_WRITE
},
1693 { FD_CMD_READ_ID
, 0xbf, "READ ID", 1, fdctrl_handle_readid
},
1694 { FD_CMD_SPECIFY
, 0xff, "SPECIFY", 2, fdctrl_handle_specify
},
1695 { FD_CMD_SENSE_DRIVE_STATUS
, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status
},
1696 { FD_CMD_PERPENDICULAR_MODE
, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode
},
1697 { FD_CMD_CONFIGURE
, 0xff, "CONFIGURE", 3, fdctrl_handle_configure
},
1698 { FD_CMD_POWERDOWN_MODE
, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode
},
1699 { FD_CMD_OPTION
, 0xff, "OPTION", 1, fdctrl_handle_option
},
1700 { FD_CMD_DRIVE_SPECIFICATION_COMMAND
, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command
},
1701 { FD_CMD_RELATIVE_SEEK_OUT
, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out
},
1702 { FD_CMD_FORMAT_AND_WRITE
, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented
},
1703 { FD_CMD_RELATIVE_SEEK_IN
, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in
},
1704 { FD_CMD_LOCK
, 0x7f, "LOCK", 0, fdctrl_handle_lock
},
1705 { FD_CMD_DUMPREG
, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg
},
1706 { FD_CMD_VERSION
, 0xff, "VERSION", 0, fdctrl_handle_version
},
1707 { FD_CMD_PART_ID
, 0xff, "PART ID", 0, fdctrl_handle_partid
},
1708 { FD_CMD_WRITE
, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer
, FD_DIR_WRITE
}, /* not in specification ; BeOS 4.5 bug */
1709 { 0, 0, "unknown", 0, fdctrl_unimplemented
}, /* default handler */
1711 /* Associate command to an index in the 'handlers' array */
1712 static uint8_t command_to_handler
[256];
1714 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
)
1720 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1721 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1724 if (!(fdctrl
->msr
& FD_MSR_RQM
) || (fdctrl
->msr
& FD_MSR_DIO
)) {
1725 FLOPPY_ERROR("controller not ready for writing\n");
1728 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1729 /* Is it write command time ? */
1730 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1731 /* FIFO data write */
1732 pos
= fdctrl
->data_pos
++;
1733 pos
%= FD_SECTOR_LEN
;
1734 fdctrl
->fifo
[pos
] = value
;
1735 if (pos
== FD_SECTOR_LEN
- 1 ||
1736 fdctrl
->data_pos
== fdctrl
->data_len
) {
1737 cur_drv
= get_cur_drv(fdctrl
);
1738 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1739 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv
));
1742 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1743 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1744 fd_sector(cur_drv
));
1748 /* Switch from transfer mode to status mode
1749 * then from status mode to command mode
1751 if (fdctrl
->data_pos
== fdctrl
->data_len
)
1752 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1755 if (fdctrl
->data_pos
== 0) {
1757 pos
= command_to_handler
[value
& 0xff];
1758 FLOPPY_DPRINTF("%s command\n", handlers
[pos
].name
);
1759 fdctrl
->data_len
= handlers
[pos
].parameters
+ 1;
1762 FLOPPY_DPRINTF("%s: %02x\n", __func__
, value
);
1763 fdctrl
->fifo
[fdctrl
->data_pos
++] = value
;
1764 if (fdctrl
->data_pos
== fdctrl
->data_len
) {
1765 /* We now have all parameters
1766 * and will be able to treat the command
1768 if (fdctrl
->data_state
& FD_STATE_FORMAT
) {
1769 fdctrl_format_sector(fdctrl
);
1773 pos
= command_to_handler
[fdctrl
->fifo
[0] & 0xff];
1774 FLOPPY_DPRINTF("treat %s command\n", handlers
[pos
].name
);
1775 (*handlers
[pos
].handler
)(fdctrl
, handlers
[pos
].direction
);
1779 static void fdctrl_result_timer(void *opaque
)
1781 FDCtrl
*fdctrl
= opaque
;
1782 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1784 /* Pretend we are spinning.
1785 * This is needed for Coherent, which uses READ ID to check for
1786 * sector interleaving.
1788 if (cur_drv
->last_sect
!= 0) {
1789 cur_drv
->sect
= (cur_drv
->sect
% cur_drv
->last_sect
) + 1;
1791 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1794 /* Init functions */
1795 static int fdctrl_connect_drives(FDCtrl
*fdctrl
)
1800 for (i
= 0; i
< MAX_FD
; i
++) {
1801 drive
= &fdctrl
->drives
[i
];
1804 if (bdrv_get_on_error(drive
->bs
, 0) != BLOCK_ERR_STOP_ENOSPC
) {
1805 error_report("fdc doesn't support drive option werror");
1808 if (bdrv_get_on_error(drive
->bs
, 1) != BLOCK_ERR_REPORT
) {
1809 error_report("fdc doesn't support drive option rerror");
1815 fd_revalidate(drive
);
1817 bdrv_set_removable(drive
->bs
, 1);
1823 void fdctrl_init_sysbus(qemu_irq irq
, int dma_chann
,
1824 target_phys_addr_t mmio_base
, DriveInfo
**fds
)
1830 dev
= qdev_create(NULL
, "sysbus-fdc");
1831 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1832 fdctrl
= &sys
->state
;
1833 fdctrl
->dma_chann
= dma_chann
; /* FIXME */
1835 qdev_prop_set_drive_nofail(dev
, "driveA", fds
[0]->bdrv
);
1838 qdev_prop_set_drive_nofail(dev
, "driveB", fds
[1]->bdrv
);
1840 qdev_init_nofail(dev
);
1841 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1842 sysbus_mmio_map(&sys
->busdev
, 0, mmio_base
);
1845 void sun4m_fdctrl_init(qemu_irq irq
, target_phys_addr_t io_base
,
1846 DriveInfo
**fds
, qemu_irq
*fdc_tc
)
1851 dev
= qdev_create(NULL
, "SUNW,fdtwo");
1853 qdev_prop_set_drive_nofail(dev
, "drive", fds
[0]->bdrv
);
1855 qdev_init_nofail(dev
);
1856 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1857 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1858 sysbus_mmio_map(&sys
->busdev
, 0, io_base
);
1859 *fdc_tc
= qdev_get_gpio_in(dev
, 0);
1862 static int fdctrl_init_common(FDCtrl
*fdctrl
)
1865 static int command_tables_inited
= 0;
1867 /* Fill 'command_to_handler' lookup table */
1868 if (!command_tables_inited
) {
1869 command_tables_inited
= 1;
1870 for (i
= ARRAY_SIZE(handlers
) - 1; i
>= 0; i
--) {
1871 for (j
= 0; j
< sizeof(command_to_handler
); j
++) {
1872 if ((j
& handlers
[i
].mask
) == handlers
[i
].value
) {
1873 command_to_handler
[j
] = i
;
1879 FLOPPY_DPRINTF("init controller\n");
1880 fdctrl
->fifo
= qemu_memalign(512, FD_SECTOR_LEN
);
1881 fdctrl
->fifo_size
= 512;
1882 fdctrl
->result_timer
= qemu_new_timer_ns(vm_clock
,
1883 fdctrl_result_timer
, fdctrl
);
1885 fdctrl
->version
= 0x90; /* Intel 82078 controller */
1886 fdctrl
->config
= FD_CONFIG_EIS
| FD_CONFIG_EFIFO
; /* Implicit seek, polling & FIFO enabled */
1887 fdctrl
->num_floppies
= MAX_FD
;
1889 if (fdctrl
->dma_chann
!= -1)
1890 DMA_register_channel(fdctrl
->dma_chann
, &fdctrl_transfer_handler
, fdctrl
);
1891 return fdctrl_connect_drives(fdctrl
);
1894 static int isabus_fdc_init1(ISADevice
*dev
)
1896 FDCtrlISABus
*isa
= DO_UPCAST(FDCtrlISABus
, busdev
, dev
);
1897 FDCtrl
*fdctrl
= &isa
->state
;
1903 register_ioport_read(iobase
+ 0x01, 5, 1,
1904 &fdctrl_read_port
, fdctrl
);
1905 register_ioport_read(iobase
+ 0x07, 1, 1,
1906 &fdctrl_read_port
, fdctrl
);
1907 register_ioport_write(iobase
+ 0x01, 5, 1,
1908 &fdctrl_write_port
, fdctrl
);
1909 register_ioport_write(iobase
+ 0x07, 1, 1,
1910 &fdctrl_write_port
, fdctrl
);
1911 isa_init_ioport_range(dev
, iobase
, 6);
1912 isa_init_ioport(dev
, iobase
+ 7);
1914 isa_init_irq(&isa
->busdev
, &fdctrl
->irq
, isairq
);
1915 fdctrl
->dma_chann
= dma_chann
;
1917 qdev_set_legacy_instance_id(&dev
->qdev
, iobase
, 2);
1918 ret
= fdctrl_init_common(fdctrl
);
1920 add_boot_device_path(isa
->bootindexA
, &dev
->qdev
, "/floppy@0");
1921 add_boot_device_path(isa
->bootindexB
, &dev
->qdev
, "/floppy@1");
1926 static int sysbus_fdc_init1(SysBusDevice
*dev
)
1928 FDCtrlSysBus
*sys
= DO_UPCAST(FDCtrlSysBus
, busdev
, dev
);
1929 FDCtrl
*fdctrl
= &sys
->state
;
1933 io
= cpu_register_io_memory(fdctrl_mem_read
, fdctrl_mem_write
, fdctrl
,
1934 DEVICE_NATIVE_ENDIAN
);
1935 sysbus_init_mmio(dev
, 0x08, io
);
1936 sysbus_init_irq(dev
, &fdctrl
->irq
);
1937 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
1938 fdctrl
->dma_chann
= -1;
1940 qdev_set_legacy_instance_id(&dev
->qdev
, io
, 2);
1941 ret
= fdctrl_init_common(fdctrl
);
1946 static int sun4m_fdc_init1(SysBusDevice
*dev
)
1948 FDCtrl
*fdctrl
= &(FROM_SYSBUS(FDCtrlSysBus
, dev
)->state
);
1951 io
= cpu_register_io_memory(fdctrl_mem_read_strict
,
1952 fdctrl_mem_write_strict
, fdctrl
,
1953 DEVICE_NATIVE_ENDIAN
);
1954 sysbus_init_mmio(dev
, 0x08, io
);
1955 sysbus_init_irq(dev
, &fdctrl
->irq
);
1956 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
1959 qdev_set_legacy_instance_id(&dev
->qdev
, io
, 2);
1960 return fdctrl_init_common(fdctrl
);
1963 static const VMStateDescription vmstate_isa_fdc
={
1966 .minimum_version_id
= 2,
1967 .fields
= (VMStateField
[]) {
1968 VMSTATE_STRUCT(state
, FDCtrlISABus
, 0, vmstate_fdc
, FDCtrl
),
1969 VMSTATE_END_OF_LIST()
1973 static ISADeviceInfo isa_fdc_info
= {
1974 .init
= isabus_fdc_init1
,
1975 .qdev
.name
= "isa-fdc",
1976 .qdev
.fw_name
= "fdc",
1977 .qdev
.size
= sizeof(FDCtrlISABus
),
1979 .qdev
.vmsd
= &vmstate_isa_fdc
,
1980 .qdev
.reset
= fdctrl_external_reset_isa
,
1981 .qdev
.props
= (Property
[]) {
1982 DEFINE_PROP_DRIVE("driveA", FDCtrlISABus
, state
.drives
[0].bs
),
1983 DEFINE_PROP_DRIVE("driveB", FDCtrlISABus
, state
.drives
[1].bs
),
1984 DEFINE_PROP_INT32("bootindexA", FDCtrlISABus
, bootindexA
, -1),
1985 DEFINE_PROP_INT32("bootindexB", FDCtrlISABus
, bootindexB
, -1),
1986 DEFINE_PROP_END_OF_LIST(),
1990 static const VMStateDescription vmstate_sysbus_fdc
={
1993 .minimum_version_id
= 2,
1994 .fields
= (VMStateField
[]) {
1995 VMSTATE_STRUCT(state
, FDCtrlSysBus
, 0, vmstate_fdc
, FDCtrl
),
1996 VMSTATE_END_OF_LIST()
2000 static SysBusDeviceInfo sysbus_fdc_info
= {
2001 .init
= sysbus_fdc_init1
,
2002 .qdev
.name
= "sysbus-fdc",
2003 .qdev
.size
= sizeof(FDCtrlSysBus
),
2004 .qdev
.vmsd
= &vmstate_sysbus_fdc
,
2005 .qdev
.reset
= fdctrl_external_reset_sysbus
,
2006 .qdev
.props
= (Property
[]) {
2007 DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus
, state
.drives
[0].bs
),
2008 DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus
, state
.drives
[1].bs
),
2009 DEFINE_PROP_END_OF_LIST(),
2013 static SysBusDeviceInfo sun4m_fdc_info
= {
2014 .init
= sun4m_fdc_init1
,
2015 .qdev
.name
= "SUNW,fdtwo",
2016 .qdev
.size
= sizeof(FDCtrlSysBus
),
2017 .qdev
.vmsd
= &vmstate_sysbus_fdc
,
2018 .qdev
.reset
= fdctrl_external_reset_sysbus
,
2019 .qdev
.props
= (Property
[]) {
2020 DEFINE_PROP_DRIVE("drive", FDCtrlSysBus
, state
.drives
[0].bs
),
2021 DEFINE_PROP_END_OF_LIST(),
2025 static void fdc_register_devices(void)
2027 isa_qdev_register(&isa_fdc_info
);
2028 sysbus_register_withprop(&sysbus_fdc_info
);
2029 sysbus_register_withprop(&sun4m_fdc_info
);
2032 device_init(fdc_register_devices
)