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"
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 /********************************************************/
53 /* Floppy drive emulation */
55 #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
56 #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))
58 /* Will always be a fixed parameter for us */
59 #define FD_SECTOR_LEN 512
60 #define FD_SECTOR_SC 2 /* Sector size code */
61 #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */
63 typedef struct FDCtrl FDCtrl
;
65 /* Floppy disk drive emulation */
66 typedef enum FDiskFlags
{
67 FDISK_DBL_SIDES
= 0x01,
70 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 */
87 uint8_t media_rate
; /* Data rate of medium */
90 static void fd_init(FDrive
*drv
)
93 drv
->drive
= FDRIVE_DRV_NONE
;
94 drv
->perpendicular
= 0;
100 #define NUM_SIDES(drv) ((drv)->flags & FDISK_DBL_SIDES ? 2 : 1)
102 static int fd_sector_calc(uint8_t head
, uint8_t track
, uint8_t sect
,
103 uint8_t last_sect
, uint8_t num_sides
)
105 return (((track
* num_sides
) + head
) * last_sect
) + sect
- 1;
108 /* Returns current position, in sectors, for given drive */
109 static int fd_sector(FDrive
*drv
)
111 return fd_sector_calc(drv
->head
, drv
->track
, drv
->sect
, drv
->last_sect
,
115 /* Seek to a new position:
116 * returns 0 if already on right track
117 * returns 1 if track changed
118 * returns 2 if track is invalid
119 * returns 3 if sector is invalid
120 * returns 4 if seek is disabled
122 static int fd_seek(FDrive
*drv
, uint8_t head
, uint8_t track
, uint8_t sect
,
128 if (track
> drv
->max_track
||
129 (head
!= 0 && (drv
->flags
& FDISK_DBL_SIDES
) == 0)) {
130 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
131 head
, track
, sect
, 1,
132 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
133 drv
->max_track
, drv
->last_sect
);
136 if (sect
> drv
->last_sect
) {
137 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
138 head
, track
, sect
, 1,
139 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
140 drv
->max_track
, drv
->last_sect
);
143 sector
= fd_sector_calc(head
, track
, sect
, drv
->last_sect
, NUM_SIDES(drv
));
145 if (sector
!= fd_sector(drv
)) {
148 FLOPPY_DPRINTF("error: no implicit seek %d %02x %02x"
149 " (max=%d %02x %02x)\n",
150 head
, track
, sect
, 1, drv
->max_track
,
156 if (drv
->track
!= track
)
162 if (drv
->bs
== NULL
|| !bdrv_is_inserted(drv
->bs
)) {
169 /* Set drive back to track 0 */
170 static void fd_recalibrate(FDrive
*drv
)
172 FLOPPY_DPRINTF("recalibrate\n");
178 /* Revalidate a disk drive after a disk change */
179 static void fd_revalidate(FDrive
*drv
)
181 int nb_heads
, max_track
, last_sect
, ro
;
185 FLOPPY_DPRINTF("revalidate\n");
186 if (drv
->bs
!= NULL
) {
187 ro
= bdrv_is_read_only(drv
->bs
);
188 bdrv_get_floppy_geometry_hint(drv
->bs
, &nb_heads
, &max_track
,
189 &last_sect
, drv
->drive
, &drive
, &rate
);
190 if (!bdrv_is_inserted(drv
->bs
)) {
191 FLOPPY_DPRINTF("No disk in drive\n");
192 } else if (nb_heads
!= 0 && max_track
!= 0 && last_sect
!= 0) {
193 FLOPPY_DPRINTF("User defined disk (%d %d %d)\n",
194 nb_heads
- 1, max_track
, last_sect
);
196 FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", nb_heads
,
197 max_track
, last_sect
, ro
? "ro" : "rw");
200 drv
->flags
&= ~FDISK_DBL_SIDES
;
202 drv
->flags
|= FDISK_DBL_SIDES
;
204 drv
->max_track
= max_track
;
205 drv
->last_sect
= last_sect
;
208 drv
->media_rate
= rate
;
210 FLOPPY_DPRINTF("No drive connected\n");
213 drv
->flags
&= ~FDISK_DBL_SIDES
;
217 /********************************************************/
218 /* Intel 82078 floppy disk controller emulation */
220 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
);
221 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
);
222 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
223 int dma_pos
, int dma_len
);
224 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
);
225 static FDrive
*get_cur_drv(FDCtrl
*fdctrl
);
227 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
);
228 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
);
229 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
);
230 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
);
231 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
);
232 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
);
233 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
);
234 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
);
235 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
);
236 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
);
237 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
);
238 static void fdctrl_write_ccr(FDCtrl
*fdctrl
, uint32_t value
);
249 FD_STATE_MULTI
= 0x01, /* multi track flag */
250 FD_STATE_FORMAT
= 0x02, /* format flag */
251 FD_STATE_SEEK
= 0x04, /* seek flag */
267 FD_CMD_READ_TRACK
= 0x02,
268 FD_CMD_SPECIFY
= 0x03,
269 FD_CMD_SENSE_DRIVE_STATUS
= 0x04,
272 FD_CMD_RECALIBRATE
= 0x07,
273 FD_CMD_SENSE_INTERRUPT_STATUS
= 0x08,
274 FD_CMD_WRITE_DELETED
= 0x09,
275 FD_CMD_READ_ID
= 0x0a,
276 FD_CMD_READ_DELETED
= 0x0c,
277 FD_CMD_FORMAT_TRACK
= 0x0d,
278 FD_CMD_DUMPREG
= 0x0e,
280 FD_CMD_VERSION
= 0x10,
281 FD_CMD_SCAN_EQUAL
= 0x11,
282 FD_CMD_PERPENDICULAR_MODE
= 0x12,
283 FD_CMD_CONFIGURE
= 0x13,
285 FD_CMD_VERIFY
= 0x16,
286 FD_CMD_POWERDOWN_MODE
= 0x17,
287 FD_CMD_PART_ID
= 0x18,
288 FD_CMD_SCAN_LOW_OR_EQUAL
= 0x19,
289 FD_CMD_SCAN_HIGH_OR_EQUAL
= 0x1d,
291 FD_CMD_OPTION
= 0x33,
292 FD_CMD_RESTORE
= 0x4e,
293 FD_CMD_DRIVE_SPECIFICATION_COMMAND
= 0x8e,
294 FD_CMD_RELATIVE_SEEK_OUT
= 0x8f,
295 FD_CMD_FORMAT_AND_WRITE
= 0xcd,
296 FD_CMD_RELATIVE_SEEK_IN
= 0xcf,
300 FD_CONFIG_PRETRK
= 0xff, /* Pre-compensation set to track 0 */
301 FD_CONFIG_FIFOTHR
= 0x0f, /* FIFO threshold set to 1 byte */
302 FD_CONFIG_POLL
= 0x10, /* Poll enabled */
303 FD_CONFIG_EFIFO
= 0x20, /* FIFO disabled */
304 FD_CONFIG_EIS
= 0x40, /* No implied seeks */
310 FD_SR0_ABNTERM
= 0x40,
311 FD_SR0_INVCMD
= 0x80,
312 FD_SR0_RDYCHG
= 0xc0,
316 FD_SR1_MA
= 0x01, /* Missing address mark */
317 FD_SR1_NW
= 0x02, /* Not writable */
318 FD_SR1_EC
= 0x80, /* End of cylinder */
322 FD_SR2_SNS
= 0x04, /* Scan not satisfied */
323 FD_SR2_SEH
= 0x08, /* Scan equal hit */
334 FD_SRA_INTPEND
= 0x80,
348 FD_DOR_SELMASK
= 0x03,
350 FD_DOR_SELMASK
= 0x01,
352 FD_DOR_nRESET
= 0x04,
354 FD_DOR_MOTEN0
= 0x10,
355 FD_DOR_MOTEN1
= 0x20,
356 FD_DOR_MOTEN2
= 0x40,
357 FD_DOR_MOTEN3
= 0x80,
362 FD_TDR_BOOTSEL
= 0x0c,
364 FD_TDR_BOOTSEL
= 0x04,
369 FD_DSR_DRATEMASK
= 0x03,
370 FD_DSR_PWRDOWN
= 0x40,
371 FD_DSR_SWRESET
= 0x80,
375 FD_MSR_DRV0BUSY
= 0x01,
376 FD_MSR_DRV1BUSY
= 0x02,
377 FD_MSR_DRV2BUSY
= 0x04,
378 FD_MSR_DRV3BUSY
= 0x08,
379 FD_MSR_CMDBUSY
= 0x10,
380 FD_MSR_NONDMA
= 0x20,
386 FD_DIR_DSKCHG
= 0x80,
389 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
390 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
391 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
396 /* Controller state */
397 QEMUTimer
*result_timer
;
399 /* Controller's identification */
405 uint8_t dor_vmstate
; /* only used as temp during vmstate */
420 uint8_t eot
; /* last wanted sector */
421 /* States kept only to be returned back */
422 /* precompensation */
426 /* Power down config (also with status regB access mode */
429 uint8_t num_floppies
;
432 FDrive drives
[MAX_FD
];
434 uint32_t check_media_rate
;
440 typedef struct FDCtrlSysBus
{
445 typedef struct FDCtrlISABus
{
455 static uint32_t fdctrl_read (void *opaque
, uint32_t reg
)
457 FDCtrl
*fdctrl
= opaque
;
463 retval
= fdctrl_read_statusA(fdctrl
);
466 retval
= fdctrl_read_statusB(fdctrl
);
469 retval
= fdctrl_read_dor(fdctrl
);
472 retval
= fdctrl_read_tape(fdctrl
);
475 retval
= fdctrl_read_main_status(fdctrl
);
478 retval
= fdctrl_read_data(fdctrl
);
481 retval
= fdctrl_read_dir(fdctrl
);
484 retval
= (uint32_t)(-1);
487 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg
& 7, retval
);
492 static void fdctrl_write (void *opaque
, uint32_t reg
, uint32_t value
)
494 FDCtrl
*fdctrl
= opaque
;
496 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg
& 7, value
);
501 fdctrl_write_dor(fdctrl
, value
);
504 fdctrl_write_tape(fdctrl
, value
);
507 fdctrl_write_rate(fdctrl
, value
);
510 fdctrl_write_data(fdctrl
, value
);
513 fdctrl_write_ccr(fdctrl
, value
);
520 static uint64_t fdctrl_read_mem (void *opaque
, target_phys_addr_t reg
,
523 return fdctrl_read(opaque
, (uint32_t)reg
);
526 static void fdctrl_write_mem (void *opaque
, target_phys_addr_t reg
,
527 uint64_t value
, unsigned size
)
529 fdctrl_write(opaque
, (uint32_t)reg
, value
);
532 static const MemoryRegionOps fdctrl_mem_ops
= {
533 .read
= fdctrl_read_mem
,
534 .write
= fdctrl_write_mem
,
535 .endianness
= DEVICE_NATIVE_ENDIAN
,
538 static const MemoryRegionOps fdctrl_mem_strict_ops
= {
539 .read
= fdctrl_read_mem
,
540 .write
= fdctrl_write_mem
,
541 .endianness
= DEVICE_NATIVE_ENDIAN
,
543 .min_access_size
= 1,
544 .max_access_size
= 1,
548 static bool fdrive_media_changed_needed(void *opaque
)
550 FDrive
*drive
= opaque
;
552 return (drive
->bs
!= NULL
&& drive
->media_changed
!= 1);
555 static const VMStateDescription vmstate_fdrive_media_changed
= {
556 .name
= "fdrive/media_changed",
558 .minimum_version_id
= 1,
559 .minimum_version_id_old
= 1,
560 .fields
= (VMStateField
[]) {
561 VMSTATE_UINT8(media_changed
, FDrive
),
562 VMSTATE_END_OF_LIST()
566 static bool fdrive_media_rate_needed(void *opaque
)
568 FDrive
*drive
= opaque
;
570 return drive
->fdctrl
->check_media_rate
;
573 static const VMStateDescription vmstate_fdrive_media_rate
= {
574 .name
= "fdrive/media_rate",
576 .minimum_version_id
= 1,
577 .minimum_version_id_old
= 1,
578 .fields
= (VMStateField
[]) {
579 VMSTATE_UINT8(media_rate
, FDrive
),
580 VMSTATE_END_OF_LIST()
584 static const VMStateDescription vmstate_fdrive
= {
587 .minimum_version_id
= 1,
588 .minimum_version_id_old
= 1,
589 .fields
= (VMStateField
[]) {
590 VMSTATE_UINT8(head
, FDrive
),
591 VMSTATE_UINT8(track
, FDrive
),
592 VMSTATE_UINT8(sect
, FDrive
),
593 VMSTATE_END_OF_LIST()
595 .subsections
= (VMStateSubsection
[]) {
597 .vmsd
= &vmstate_fdrive_media_changed
,
598 .needed
= &fdrive_media_changed_needed
,
600 .vmsd
= &vmstate_fdrive_media_rate
,
601 .needed
= &fdrive_media_rate_needed
,
608 static void fdc_pre_save(void *opaque
)
612 s
->dor_vmstate
= s
->dor
| GET_CUR_DRV(s
);
615 static int fdc_post_load(void *opaque
, int version_id
)
619 SET_CUR_DRV(s
, s
->dor_vmstate
& FD_DOR_SELMASK
);
620 s
->dor
= s
->dor_vmstate
& ~FD_DOR_SELMASK
;
624 static const VMStateDescription vmstate_fdc
= {
627 .minimum_version_id
= 2,
628 .minimum_version_id_old
= 2,
629 .pre_save
= fdc_pre_save
,
630 .post_load
= fdc_post_load
,
631 .fields
= (VMStateField
[]) {
632 /* Controller State */
633 VMSTATE_UINT8(sra
, FDCtrl
),
634 VMSTATE_UINT8(srb
, FDCtrl
),
635 VMSTATE_UINT8(dor_vmstate
, FDCtrl
),
636 VMSTATE_UINT8(tdr
, FDCtrl
),
637 VMSTATE_UINT8(dsr
, FDCtrl
),
638 VMSTATE_UINT8(msr
, FDCtrl
),
639 VMSTATE_UINT8(status0
, FDCtrl
),
640 VMSTATE_UINT8(status1
, FDCtrl
),
641 VMSTATE_UINT8(status2
, FDCtrl
),
643 VMSTATE_VARRAY_INT32(fifo
, FDCtrl
, fifo_size
, 0, vmstate_info_uint8
,
645 VMSTATE_UINT32(data_pos
, FDCtrl
),
646 VMSTATE_UINT32(data_len
, FDCtrl
),
647 VMSTATE_UINT8(data_state
, FDCtrl
),
648 VMSTATE_UINT8(data_dir
, FDCtrl
),
649 VMSTATE_UINT8(eot
, FDCtrl
),
650 /* States kept only to be returned back */
651 VMSTATE_UINT8(timer0
, FDCtrl
),
652 VMSTATE_UINT8(timer1
, FDCtrl
),
653 VMSTATE_UINT8(precomp_trk
, FDCtrl
),
654 VMSTATE_UINT8(config
, FDCtrl
),
655 VMSTATE_UINT8(lock
, FDCtrl
),
656 VMSTATE_UINT8(pwrd
, FDCtrl
),
657 VMSTATE_UINT8_EQUAL(num_floppies
, FDCtrl
),
658 VMSTATE_STRUCT_ARRAY(drives
, FDCtrl
, MAX_FD
, 1,
659 vmstate_fdrive
, FDrive
),
660 VMSTATE_END_OF_LIST()
664 static void fdctrl_external_reset_sysbus(DeviceState
*d
)
666 FDCtrlSysBus
*sys
= container_of(d
, FDCtrlSysBus
, busdev
.qdev
);
667 FDCtrl
*s
= &sys
->state
;
672 static void fdctrl_external_reset_isa(DeviceState
*d
)
674 FDCtrlISABus
*isa
= container_of(d
, FDCtrlISABus
, busdev
.qdev
);
675 FDCtrl
*s
= &isa
->state
;
680 static void fdctrl_handle_tc(void *opaque
, int irq
, int level
)
682 //FDCtrl *s = opaque;
686 FLOPPY_DPRINTF("TC pulsed\n");
690 /* Change IRQ state */
691 static void fdctrl_reset_irq(FDCtrl
*fdctrl
)
693 if (!(fdctrl
->sra
& FD_SRA_INTPEND
))
695 FLOPPY_DPRINTF("Reset interrupt\n");
696 qemu_set_irq(fdctrl
->irq
, 0);
697 fdctrl
->sra
&= ~FD_SRA_INTPEND
;
700 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
)
703 if (fdctrl
->sun4m
&& (fdctrl
->msr
& FD_MSR_CMDBUSY
)) {
705 fdctrl
->msr
&= ~FD_MSR_CMDBUSY
;
706 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
707 fdctrl
->status0
= status0
;
710 if (!(fdctrl
->sra
& FD_SRA_INTPEND
)) {
711 qemu_set_irq(fdctrl
->irq
, 1);
712 fdctrl
->sra
|= FD_SRA_INTPEND
;
714 if (status0
& FD_SR0_SEEK
) {
716 /* A seek clears the disk change line (if a disk is inserted) */
717 cur_drv
= get_cur_drv(fdctrl
);
718 if (cur_drv
->bs
!= NULL
&& bdrv_is_inserted(cur_drv
->bs
)) {
719 cur_drv
->media_changed
= 0;
723 fdctrl
->reset_sensei
= 0;
724 fdctrl
->status0
= status0
;
725 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl
->status0
);
728 /* Reset controller */
729 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
)
733 FLOPPY_DPRINTF("reset controller\n");
734 fdctrl_reset_irq(fdctrl
);
735 /* Initialise controller */
738 if (!fdctrl
->drives
[1].bs
)
739 fdctrl
->sra
|= FD_SRA_nDRV2
;
741 fdctrl
->dor
= FD_DOR_nRESET
;
742 fdctrl
->dor
|= (fdctrl
->dma_chann
!= -1) ? FD_DOR_DMAEN
: 0;
743 fdctrl
->msr
= FD_MSR_RQM
;
745 fdctrl
->data_pos
= 0;
746 fdctrl
->data_len
= 0;
747 fdctrl
->data_state
= 0;
748 fdctrl
->data_dir
= FD_DIR_WRITE
;
749 for (i
= 0; i
< MAX_FD
; i
++)
750 fd_recalibrate(&fdctrl
->drives
[i
]);
751 fdctrl_reset_fifo(fdctrl
);
753 fdctrl_raise_irq(fdctrl
, FD_SR0_RDYCHG
);
754 fdctrl
->reset_sensei
= FD_RESET_SENSEI_COUNT
;
758 static inline FDrive
*drv0(FDCtrl
*fdctrl
)
760 return &fdctrl
->drives
[(fdctrl
->tdr
& FD_TDR_BOOTSEL
) >> 2];
763 static inline FDrive
*drv1(FDCtrl
*fdctrl
)
765 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (1 << 2))
766 return &fdctrl
->drives
[1];
768 return &fdctrl
->drives
[0];
772 static inline FDrive
*drv2(FDCtrl
*fdctrl
)
774 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (2 << 2))
775 return &fdctrl
->drives
[2];
777 return &fdctrl
->drives
[1];
780 static inline FDrive
*drv3(FDCtrl
*fdctrl
)
782 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (3 << 2))
783 return &fdctrl
->drives
[3];
785 return &fdctrl
->drives
[2];
789 static FDrive
*get_cur_drv(FDCtrl
*fdctrl
)
791 switch (fdctrl
->cur_drv
) {
792 case 0: return drv0(fdctrl
);
793 case 1: return drv1(fdctrl
);
795 case 2: return drv2(fdctrl
);
796 case 3: return drv3(fdctrl
);
798 default: return NULL
;
802 /* Status A register : 0x00 (read-only) */
803 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
)
805 uint32_t retval
= fdctrl
->sra
;
807 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval
);
812 /* Status B register : 0x01 (read-only) */
813 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
)
815 uint32_t retval
= fdctrl
->srb
;
817 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval
);
822 /* Digital output register : 0x02 */
823 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
)
825 uint32_t retval
= fdctrl
->dor
;
828 retval
|= fdctrl
->cur_drv
;
829 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
834 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
)
836 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
839 if (value
& FD_DOR_MOTEN0
)
840 fdctrl
->srb
|= FD_SRB_MTR0
;
842 fdctrl
->srb
&= ~FD_SRB_MTR0
;
843 if (value
& FD_DOR_MOTEN1
)
844 fdctrl
->srb
|= FD_SRB_MTR1
;
846 fdctrl
->srb
&= ~FD_SRB_MTR1
;
850 fdctrl
->srb
|= FD_SRB_DR0
;
852 fdctrl
->srb
&= ~FD_SRB_DR0
;
855 if (!(value
& FD_DOR_nRESET
)) {
856 if (fdctrl
->dor
& FD_DOR_nRESET
) {
857 FLOPPY_DPRINTF("controller enter RESET state\n");
860 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
861 FLOPPY_DPRINTF("controller out of RESET state\n");
862 fdctrl_reset(fdctrl
, 1);
863 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
867 fdctrl
->cur_drv
= value
& FD_DOR_SELMASK
;
872 /* Tape drive register : 0x03 */
873 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
)
875 uint32_t retval
= fdctrl
->tdr
;
877 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
882 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
)
885 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
886 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
889 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
890 /* Disk boot selection indicator */
891 fdctrl
->tdr
= value
& FD_TDR_BOOTSEL
;
892 /* Tape indicators: never allow */
895 /* Main status register : 0x04 (read) */
896 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
)
898 uint32_t retval
= fdctrl
->msr
;
900 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
901 fdctrl
->dor
|= FD_DOR_nRESET
;
905 retval
|= FD_MSR_DIO
;
906 fdctrl_reset_irq(fdctrl
);
909 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
914 /* Data select rate register : 0x04 (write) */
915 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
)
918 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
919 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
922 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
923 /* Reset: autoclear */
924 if (value
& FD_DSR_SWRESET
) {
925 fdctrl
->dor
&= ~FD_DOR_nRESET
;
926 fdctrl_reset(fdctrl
, 1);
927 fdctrl
->dor
|= FD_DOR_nRESET
;
929 if (value
& FD_DSR_PWRDOWN
) {
930 fdctrl_reset(fdctrl
, 1);
935 /* Configuration control register: 0x07 (write) */
936 static void fdctrl_write_ccr(FDCtrl
*fdctrl
, uint32_t value
)
939 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
940 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
943 FLOPPY_DPRINTF("configuration control register set to 0x%02x\n", value
);
945 /* Only the rate selection bits used in AT mode, and we
946 * store those in the DSR.
948 fdctrl
->dsr
= (fdctrl
->dsr
& ~FD_DSR_DRATEMASK
) |
949 (value
& FD_DSR_DRATEMASK
);
952 static int fdctrl_media_changed(FDrive
*drv
)
954 return drv
->media_changed
;
957 /* Digital input register : 0x07 (read-only) */
958 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
)
962 if (fdctrl_media_changed(get_cur_drv(fdctrl
))) {
963 retval
|= FD_DIR_DSKCHG
;
966 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
972 /* FIFO state control */
973 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
)
975 fdctrl
->data_dir
= FD_DIR_WRITE
;
976 fdctrl
->data_pos
= 0;
977 fdctrl
->msr
&= ~(FD_MSR_CMDBUSY
| FD_MSR_DIO
);
980 /* Set FIFO status for the host to read */
981 static void fdctrl_set_fifo(FDCtrl
*fdctrl
, int fifo_len
, int do_irq
)
983 fdctrl
->data_dir
= FD_DIR_READ
;
984 fdctrl
->data_len
= fifo_len
;
985 fdctrl
->data_pos
= 0;
986 fdctrl
->msr
|= FD_MSR_CMDBUSY
| FD_MSR_RQM
| FD_MSR_DIO
;
988 fdctrl_raise_irq(fdctrl
, 0x00);
991 /* Set an error: unimplemented/unknown command */
992 static void fdctrl_unimplemented(FDCtrl
*fdctrl
, int direction
)
994 qemu_log_mask(LOG_UNIMP
, "fdc: unimplemented command 0x%02x\n",
996 fdctrl
->fifo
[0] = FD_SR0_INVCMD
;
997 fdctrl_set_fifo(fdctrl
, 1, 0);
1000 /* Seek to next sector */
1001 static int fdctrl_seek_to_next_sect(FDCtrl
*fdctrl
, FDrive
*cur_drv
)
1003 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
1004 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
1005 fd_sector(cur_drv
));
1006 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1008 if (cur_drv
->sect
>= cur_drv
->last_sect
||
1009 cur_drv
->sect
== fdctrl
->eot
) {
1011 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
1012 if (cur_drv
->head
== 0 &&
1013 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
1018 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0)
1025 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1026 cur_drv
->head
, cur_drv
->track
,
1027 cur_drv
->sect
, fd_sector(cur_drv
));
1034 /* Callback for transfer end (stop or abort) */
1035 static void fdctrl_stop_transfer(FDCtrl
*fdctrl
, uint8_t status0
,
1036 uint8_t status1
, uint8_t status2
)
1040 cur_drv
= get_cur_drv(fdctrl
);
1041 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1042 status0
, status1
, status2
,
1043 status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
));
1044 fdctrl
->fifo
[0] = status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1045 fdctrl
->fifo
[1] = status1
;
1046 fdctrl
->fifo
[2] = status2
;
1047 fdctrl
->fifo
[3] = cur_drv
->track
;
1048 fdctrl
->fifo
[4] = cur_drv
->head
;
1049 fdctrl
->fifo
[5] = cur_drv
->sect
;
1050 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
1051 fdctrl
->data_dir
= FD_DIR_READ
;
1052 if (!(fdctrl
->msr
& FD_MSR_NONDMA
)) {
1053 DMA_release_DREQ(fdctrl
->dma_chann
);
1055 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
1056 fdctrl
->msr
&= ~FD_MSR_NONDMA
;
1057 fdctrl_set_fifo(fdctrl
, 7, 1);
1060 /* Prepare a data transfer (either DMA or FIFO) */
1061 static void fdctrl_start_transfer(FDCtrl
*fdctrl
, int direction
)
1067 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1068 cur_drv
= get_cur_drv(fdctrl
);
1069 kt
= fdctrl
->fifo
[2];
1070 kh
= fdctrl
->fifo
[3];
1071 ks
= fdctrl
->fifo
[4];
1072 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1073 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1074 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
,
1075 NUM_SIDES(cur_drv
)));
1076 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1079 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1080 fdctrl
->fifo
[3] = kt
;
1081 fdctrl
->fifo
[4] = kh
;
1082 fdctrl
->fifo
[5] = ks
;
1086 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1087 fdctrl
->fifo
[3] = kt
;
1088 fdctrl
->fifo
[4] = kh
;
1089 fdctrl
->fifo
[5] = ks
;
1092 /* No seek enabled */
1093 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1094 fdctrl
->fifo
[3] = kt
;
1095 fdctrl
->fifo
[4] = kh
;
1096 fdctrl
->fifo
[5] = ks
;
1105 /* Check the data rate. If the programmed data rate does not match
1106 * the currently inserted medium, the operation has to fail. */
1107 if (fdctrl
->check_media_rate
&&
1108 (fdctrl
->dsr
& FD_DSR_DRATEMASK
) != cur_drv
->media_rate
) {
1109 FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n",
1110 fdctrl
->dsr
& FD_DSR_DRATEMASK
, cur_drv
->media_rate
);
1111 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_MA
, 0x00);
1112 fdctrl
->fifo
[3] = kt
;
1113 fdctrl
->fifo
[4] = kh
;
1114 fdctrl
->fifo
[5] = ks
;
1118 /* Set the FIFO state */
1119 fdctrl
->data_dir
= direction
;
1120 fdctrl
->data_pos
= 0;
1121 fdctrl
->msr
|= FD_MSR_CMDBUSY
;
1122 if (fdctrl
->fifo
[0] & 0x80)
1123 fdctrl
->data_state
|= FD_STATE_MULTI
;
1125 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1127 fdctrl
->data_state
|= FD_STATE_SEEK
;
1129 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1130 if (fdctrl
->fifo
[5] == 00) {
1131 fdctrl
->data_len
= fdctrl
->fifo
[8];
1134 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
1135 tmp
= (fdctrl
->fifo
[6] - ks
+ 1);
1136 if (fdctrl
->fifo
[0] & 0x80)
1137 tmp
+= fdctrl
->fifo
[6];
1138 fdctrl
->data_len
*= tmp
;
1140 fdctrl
->eot
= fdctrl
->fifo
[6];
1141 if (fdctrl
->dor
& FD_DOR_DMAEN
) {
1143 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1144 dma_mode
= DMA_get_channel_mode(fdctrl
->dma_chann
);
1145 dma_mode
= (dma_mode
>> 2) & 3;
1146 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1147 dma_mode
, direction
,
1148 (128 << fdctrl
->fifo
[5]) *
1149 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
1150 if (((direction
== FD_DIR_SCANE
|| direction
== FD_DIR_SCANL
||
1151 direction
== FD_DIR_SCANH
) && dma_mode
== 0) ||
1152 (direction
== FD_DIR_WRITE
&& dma_mode
== 2) ||
1153 (direction
== FD_DIR_READ
&& dma_mode
== 1)) {
1154 /* No access is allowed until DMA transfer has completed */
1155 fdctrl
->msr
&= ~FD_MSR_RQM
;
1156 /* Now, we just have to wait for the DMA controller to
1159 DMA_hold_DREQ(fdctrl
->dma_chann
);
1160 DMA_schedule(fdctrl
->dma_chann
);
1163 FLOPPY_DPRINTF("bad dma_mode=%d direction=%d\n", dma_mode
,
1167 FLOPPY_DPRINTF("start non-DMA transfer\n");
1168 fdctrl
->msr
|= FD_MSR_NONDMA
;
1169 if (direction
!= FD_DIR_WRITE
)
1170 fdctrl
->msr
|= FD_MSR_DIO
;
1171 /* IO based transfer: calculate len */
1172 fdctrl_raise_irq(fdctrl
, 0x00);
1177 /* Prepare a transfer of deleted data */
1178 static void fdctrl_start_transfer_del(FDCtrl
*fdctrl
, int direction
)
1180 qemu_log_mask(LOG_UNIMP
, "fdctrl_start_transfer_del() unimplemented\n");
1182 /* We don't handle deleted data,
1183 * so we don't return *ANYTHING*
1185 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1188 /* handlers for DMA transfers */
1189 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
1190 int dma_pos
, int dma_len
)
1194 int len
, start_pos
, rel_pos
;
1195 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
1198 if (fdctrl
->msr
& FD_MSR_RQM
) {
1199 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1202 cur_drv
= get_cur_drv(fdctrl
);
1203 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
1204 fdctrl
->data_dir
== FD_DIR_SCANH
)
1205 status2
= FD_SR2_SNS
;
1206 if (dma_len
> fdctrl
->data_len
)
1207 dma_len
= fdctrl
->data_len
;
1208 if (cur_drv
->bs
== NULL
) {
1209 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
1210 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1212 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1214 goto transfer_error
;
1216 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1217 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
1218 len
= dma_len
- fdctrl
->data_pos
;
1219 if (len
+ rel_pos
> FD_SECTOR_LEN
)
1220 len
= FD_SECTOR_LEN
- rel_pos
;
1221 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1222 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
1223 fdctrl
->data_len
, GET_CUR_DRV(fdctrl
), cur_drv
->head
,
1224 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
1225 fd_sector(cur_drv
) * FD_SECTOR_LEN
);
1226 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
1227 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
1228 /* READ & SCAN commands and realign to a sector for WRITE */
1229 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1230 fdctrl
->fifo
, 1) < 0) {
1231 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1232 fd_sector(cur_drv
));
1233 /* Sure, image size is too small... */
1234 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1237 switch (fdctrl
->data_dir
) {
1240 DMA_write_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1241 fdctrl
->data_pos
, len
);
1244 /* WRITE commands */
1246 /* Handle readonly medium early, no need to do DMA, touch the
1247 * LED or attempt any writes. A real floppy doesn't attempt
1248 * to write to readonly media either. */
1249 fdctrl_stop_transfer(fdctrl
,
1250 FD_SR0_ABNTERM
| FD_SR0_SEEK
, FD_SR1_NW
,
1252 goto transfer_error
;
1255 DMA_read_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1256 fdctrl
->data_pos
, len
);
1257 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1258 fdctrl
->fifo
, 1) < 0) {
1259 FLOPPY_DPRINTF("error writing sector %d\n",
1260 fd_sector(cur_drv
));
1261 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1262 goto transfer_error
;
1268 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1270 DMA_read_memory (nchan
, tmpbuf
, fdctrl
->data_pos
, len
);
1271 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1273 status2
= FD_SR2_SEH
;
1276 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1277 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1284 fdctrl
->data_pos
+= len
;
1285 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1287 /* Seek to next sector */
1288 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
))
1293 len
= fdctrl
->data_pos
- start_pos
;
1294 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1295 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1296 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1297 fdctrl
->data_dir
== FD_DIR_SCANL
||
1298 fdctrl
->data_dir
== FD_DIR_SCANH
)
1299 status2
= FD_SR2_SEH
;
1300 if (FD_DID_SEEK(fdctrl
->data_state
))
1301 status0
|= FD_SR0_SEEK
;
1302 fdctrl
->data_len
-= len
;
1303 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1309 /* Data register : 0x05 */
1310 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
)
1313 uint32_t retval
= 0;
1316 cur_drv
= get_cur_drv(fdctrl
);
1317 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1318 if (!(fdctrl
->msr
& FD_MSR_RQM
) || !(fdctrl
->msr
& FD_MSR_DIO
)) {
1319 FLOPPY_DPRINTF("error: controller not ready for reading\n");
1322 pos
= fdctrl
->data_pos
;
1323 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1324 pos
%= FD_SECTOR_LEN
;
1326 if (fdctrl
->data_pos
!= 0)
1327 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1328 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1329 fd_sector(cur_drv
));
1332 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1333 FLOPPY_DPRINTF("error getting sector %d\n",
1334 fd_sector(cur_drv
));
1335 /* Sure, image size is too small... */
1336 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1340 retval
= fdctrl
->fifo
[pos
];
1341 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1342 fdctrl
->data_pos
= 0;
1343 /* Switch from transfer mode to status mode
1344 * then from status mode to command mode
1346 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1347 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1349 fdctrl_reset_fifo(fdctrl
);
1350 fdctrl_reset_irq(fdctrl
);
1353 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1358 static void fdctrl_format_sector(FDCtrl
*fdctrl
)
1363 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1364 cur_drv
= get_cur_drv(fdctrl
);
1365 kt
= fdctrl
->fifo
[6];
1366 kh
= fdctrl
->fifo
[7];
1367 ks
= fdctrl
->fifo
[8];
1368 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1369 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1370 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
,
1371 NUM_SIDES(cur_drv
)));
1372 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1375 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1376 fdctrl
->fifo
[3] = kt
;
1377 fdctrl
->fifo
[4] = kh
;
1378 fdctrl
->fifo
[5] = ks
;
1382 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1383 fdctrl
->fifo
[3] = kt
;
1384 fdctrl
->fifo
[4] = kh
;
1385 fdctrl
->fifo
[5] = ks
;
1388 /* No seek enabled */
1389 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1390 fdctrl
->fifo
[3] = kt
;
1391 fdctrl
->fifo
[4] = kh
;
1392 fdctrl
->fifo
[5] = ks
;
1395 fdctrl
->data_state
|= FD_STATE_SEEK
;
1400 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1401 if (cur_drv
->bs
== NULL
||
1402 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1403 FLOPPY_DPRINTF("error formatting sector %d\n", fd_sector(cur_drv
));
1404 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1406 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1407 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1408 /* Last sector done */
1409 if (FD_DID_SEEK(fdctrl
->data_state
))
1410 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1412 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1415 fdctrl
->data_pos
= 0;
1416 fdctrl
->data_len
= 4;
1421 static void fdctrl_handle_lock(FDCtrl
*fdctrl
, int direction
)
1423 fdctrl
->lock
= (fdctrl
->fifo
[0] & 0x80) ? 1 : 0;
1424 fdctrl
->fifo
[0] = fdctrl
->lock
<< 4;
1425 fdctrl_set_fifo(fdctrl
, 1, 0);
1428 static void fdctrl_handle_dumpreg(FDCtrl
*fdctrl
, int direction
)
1430 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1432 /* Drives position */
1433 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1434 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1436 fdctrl
->fifo
[2] = drv2(fdctrl
)->track
;
1437 fdctrl
->fifo
[3] = drv3(fdctrl
)->track
;
1439 fdctrl
->fifo
[2] = 0;
1440 fdctrl
->fifo
[3] = 0;
1443 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1444 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | (fdctrl
->dor
& FD_DOR_DMAEN
? 1 : 0);
1445 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1446 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1447 (cur_drv
->perpendicular
<< 2);
1448 fdctrl
->fifo
[8] = fdctrl
->config
;
1449 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1450 fdctrl_set_fifo(fdctrl
, 10, 0);
1453 static void fdctrl_handle_version(FDCtrl
*fdctrl
, int direction
)
1455 /* Controller's version */
1456 fdctrl
->fifo
[0] = fdctrl
->version
;
1457 fdctrl_set_fifo(fdctrl
, 1, 0);
1460 static void fdctrl_handle_partid(FDCtrl
*fdctrl
, int direction
)
1462 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1463 fdctrl_set_fifo(fdctrl
, 1, 0);
1466 static void fdctrl_handle_restore(FDCtrl
*fdctrl
, int direction
)
1468 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1470 /* Drives position */
1471 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1472 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1474 drv2(fdctrl
)->track
= fdctrl
->fifo
[5];
1475 drv3(fdctrl
)->track
= fdctrl
->fifo
[6];
1478 fdctrl
->timer0
= fdctrl
->fifo
[7];
1479 fdctrl
->timer1
= fdctrl
->fifo
[8];
1480 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1481 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1482 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1483 fdctrl
->config
= fdctrl
->fifo
[11];
1484 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1485 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1486 fdctrl_reset_fifo(fdctrl
);
1489 static void fdctrl_handle_save(FDCtrl
*fdctrl
, int direction
)
1491 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1493 fdctrl
->fifo
[0] = 0;
1494 fdctrl
->fifo
[1] = 0;
1495 /* Drives position */
1496 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1497 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1499 fdctrl
->fifo
[4] = drv2(fdctrl
)->track
;
1500 fdctrl
->fifo
[5] = drv3(fdctrl
)->track
;
1502 fdctrl
->fifo
[4] = 0;
1503 fdctrl
->fifo
[5] = 0;
1506 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1507 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1508 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1509 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1510 (cur_drv
->perpendicular
<< 2);
1511 fdctrl
->fifo
[10] = fdctrl
->config
;
1512 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1513 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1514 fdctrl
->fifo
[13] = 0;
1515 fdctrl
->fifo
[14] = 0;
1516 fdctrl_set_fifo(fdctrl
, 15, 0);
1519 static void fdctrl_handle_readid(FDCtrl
*fdctrl
, int direction
)
1521 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1523 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1524 qemu_mod_timer(fdctrl
->result_timer
,
1525 qemu_get_clock_ns(vm_clock
) + (get_ticks_per_sec() / 50));
1528 static void fdctrl_handle_format_track(FDCtrl
*fdctrl
, int direction
)
1532 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1533 cur_drv
= get_cur_drv(fdctrl
);
1534 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1535 if (fdctrl
->fifo
[0] & 0x80)
1536 fdctrl
->data_state
|= FD_STATE_MULTI
;
1538 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1539 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1541 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1543 cur_drv
->last_sect
=
1544 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1545 fdctrl
->fifo
[3] / 2;
1547 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1549 /* TODO: implement format using DMA expected by the Bochs BIOS
1550 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1551 * the sector with the specified fill byte
1553 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1554 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1557 static void fdctrl_handle_specify(FDCtrl
*fdctrl
, int direction
)
1559 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1560 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1561 if (fdctrl
->fifo
[2] & 1)
1562 fdctrl
->dor
&= ~FD_DOR_DMAEN
;
1564 fdctrl
->dor
|= FD_DOR_DMAEN
;
1565 /* No result back */
1566 fdctrl_reset_fifo(fdctrl
);
1569 static void fdctrl_handle_sense_drive_status(FDCtrl
*fdctrl
, int direction
)
1573 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1574 cur_drv
= get_cur_drv(fdctrl
);
1575 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1576 /* 1 Byte status back */
1577 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1578 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1579 (cur_drv
->head
<< 2) |
1580 GET_CUR_DRV(fdctrl
) |
1582 fdctrl_set_fifo(fdctrl
, 1, 0);
1585 static void fdctrl_handle_recalibrate(FDCtrl
*fdctrl
, int direction
)
1589 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1590 cur_drv
= get_cur_drv(fdctrl
);
1591 fd_recalibrate(cur_drv
);
1592 fdctrl_reset_fifo(fdctrl
);
1593 /* Raise Interrupt */
1594 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1597 static void fdctrl_handle_sense_interrupt_status(FDCtrl
*fdctrl
, int direction
)
1599 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1601 if(fdctrl
->reset_sensei
> 0) {
1603 FD_SR0_RDYCHG
+ FD_RESET_SENSEI_COUNT
- fdctrl
->reset_sensei
;
1604 fdctrl
->reset_sensei
--;
1606 /* XXX: status0 handling is broken for read/write
1607 commands, so we do this hack. It should be suppressed
1610 FD_SR0_SEEK
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1613 fdctrl
->fifo
[1] = cur_drv
->track
;
1614 fdctrl_set_fifo(fdctrl
, 2, 0);
1615 fdctrl_reset_irq(fdctrl
);
1616 fdctrl
->status0
= FD_SR0_RDYCHG
;
1619 static void fdctrl_handle_seek(FDCtrl
*fdctrl
, int direction
)
1623 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1624 cur_drv
= get_cur_drv(fdctrl
);
1625 fdctrl_reset_fifo(fdctrl
);
1626 /* The seek command just sends step pulses to the drive and doesn't care if
1627 * there is a medium inserted of if it's banging the head against the drive.
1629 if (fdctrl
->fifo
[2] > cur_drv
->max_track
) {
1630 cur_drv
->track
= cur_drv
->max_track
;
1632 cur_drv
->track
= fdctrl
->fifo
[2];
1634 /* Raise Interrupt */
1635 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1638 static void fdctrl_handle_perpendicular_mode(FDCtrl
*fdctrl
, int direction
)
1640 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1642 if (fdctrl
->fifo
[1] & 0x80)
1643 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
1644 /* No result back */
1645 fdctrl_reset_fifo(fdctrl
);
1648 static void fdctrl_handle_configure(FDCtrl
*fdctrl
, int direction
)
1650 fdctrl
->config
= fdctrl
->fifo
[2];
1651 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
1652 /* No result back */
1653 fdctrl_reset_fifo(fdctrl
);
1656 static void fdctrl_handle_powerdown_mode(FDCtrl
*fdctrl
, int direction
)
1658 fdctrl
->pwrd
= fdctrl
->fifo
[1];
1659 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1660 fdctrl_set_fifo(fdctrl
, 1, 0);
1663 static void fdctrl_handle_option(FDCtrl
*fdctrl
, int direction
)
1665 /* No result back */
1666 fdctrl_reset_fifo(fdctrl
);
1669 static void fdctrl_handle_drive_specification_command(FDCtrl
*fdctrl
, int direction
)
1671 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1673 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x80) {
1674 /* Command parameters done */
1675 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x40) {
1676 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1677 fdctrl
->fifo
[2] = 0;
1678 fdctrl
->fifo
[3] = 0;
1679 fdctrl_set_fifo(fdctrl
, 4, 0);
1681 fdctrl_reset_fifo(fdctrl
);
1683 } else if (fdctrl
->data_len
> 7) {
1685 fdctrl
->fifo
[0] = 0x80 |
1686 (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1687 fdctrl_set_fifo(fdctrl
, 1, 0);
1691 static void fdctrl_handle_relative_seek_out(FDCtrl
*fdctrl
, int direction
)
1695 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1696 cur_drv
= get_cur_drv(fdctrl
);
1697 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
1698 cur_drv
->track
= cur_drv
->max_track
- 1;
1700 cur_drv
->track
+= fdctrl
->fifo
[2];
1702 fdctrl_reset_fifo(fdctrl
);
1703 /* Raise Interrupt */
1704 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1707 static void fdctrl_handle_relative_seek_in(FDCtrl
*fdctrl
, int direction
)
1711 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1712 cur_drv
= get_cur_drv(fdctrl
);
1713 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
1716 cur_drv
->track
-= fdctrl
->fifo
[2];
1718 fdctrl_reset_fifo(fdctrl
);
1719 /* Raise Interrupt */
1720 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1723 static const struct {
1728 void (*handler
)(FDCtrl
*fdctrl
, int direction
);
1731 { FD_CMD_READ
, 0x1f, "READ", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1732 { FD_CMD_WRITE
, 0x3f, "WRITE", 8, fdctrl_start_transfer
, FD_DIR_WRITE
},
1733 { FD_CMD_SEEK
, 0xff, "SEEK", 2, fdctrl_handle_seek
},
1734 { FD_CMD_SENSE_INTERRUPT_STATUS
, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status
},
1735 { FD_CMD_RECALIBRATE
, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate
},
1736 { FD_CMD_FORMAT_TRACK
, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track
},
1737 { FD_CMD_READ_TRACK
, 0xbf, "READ TRACK", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1738 { FD_CMD_RESTORE
, 0xff, "RESTORE", 17, fdctrl_handle_restore
}, /* part of READ DELETED DATA */
1739 { FD_CMD_SAVE
, 0xff, "SAVE", 0, fdctrl_handle_save
}, /* part of READ DELETED DATA */
1740 { FD_CMD_READ_DELETED
, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_READ
},
1741 { FD_CMD_SCAN_EQUAL
, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANE
},
1742 { FD_CMD_VERIFY
, 0x1f, "VERIFY", 8, fdctrl_unimplemented
},
1743 { FD_CMD_SCAN_LOW_OR_EQUAL
, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANL
},
1744 { FD_CMD_SCAN_HIGH_OR_EQUAL
, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANH
},
1745 { FD_CMD_WRITE_DELETED
, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_WRITE
},
1746 { FD_CMD_READ_ID
, 0xbf, "READ ID", 1, fdctrl_handle_readid
},
1747 { FD_CMD_SPECIFY
, 0xff, "SPECIFY", 2, fdctrl_handle_specify
},
1748 { FD_CMD_SENSE_DRIVE_STATUS
, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status
},
1749 { FD_CMD_PERPENDICULAR_MODE
, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode
},
1750 { FD_CMD_CONFIGURE
, 0xff, "CONFIGURE", 3, fdctrl_handle_configure
},
1751 { FD_CMD_POWERDOWN_MODE
, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode
},
1752 { FD_CMD_OPTION
, 0xff, "OPTION", 1, fdctrl_handle_option
},
1753 { FD_CMD_DRIVE_SPECIFICATION_COMMAND
, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command
},
1754 { FD_CMD_RELATIVE_SEEK_OUT
, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out
},
1755 { FD_CMD_FORMAT_AND_WRITE
, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented
},
1756 { FD_CMD_RELATIVE_SEEK_IN
, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in
},
1757 { FD_CMD_LOCK
, 0x7f, "LOCK", 0, fdctrl_handle_lock
},
1758 { FD_CMD_DUMPREG
, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg
},
1759 { FD_CMD_VERSION
, 0xff, "VERSION", 0, fdctrl_handle_version
},
1760 { FD_CMD_PART_ID
, 0xff, "PART ID", 0, fdctrl_handle_partid
},
1761 { FD_CMD_WRITE
, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer
, FD_DIR_WRITE
}, /* not in specification ; BeOS 4.5 bug */
1762 { 0, 0, "unknown", 0, fdctrl_unimplemented
}, /* default handler */
1764 /* Associate command to an index in the 'handlers' array */
1765 static uint8_t command_to_handler
[256];
1767 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
)
1773 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1774 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1777 if (!(fdctrl
->msr
& FD_MSR_RQM
) || (fdctrl
->msr
& FD_MSR_DIO
)) {
1778 FLOPPY_DPRINTF("error: controller not ready for writing\n");
1781 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1782 /* Is it write command time ? */
1783 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1784 /* FIFO data write */
1785 pos
= fdctrl
->data_pos
++;
1786 pos
%= FD_SECTOR_LEN
;
1787 fdctrl
->fifo
[pos
] = value
;
1788 if (pos
== FD_SECTOR_LEN
- 1 ||
1789 fdctrl
->data_pos
== fdctrl
->data_len
) {
1790 cur_drv
= get_cur_drv(fdctrl
);
1791 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1792 FLOPPY_DPRINTF("error writing sector %d\n",
1793 fd_sector(cur_drv
));
1796 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1797 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1798 fd_sector(cur_drv
));
1802 /* Switch from transfer mode to status mode
1803 * then from status mode to command mode
1805 if (fdctrl
->data_pos
== fdctrl
->data_len
)
1806 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1809 if (fdctrl
->data_pos
== 0) {
1811 pos
= command_to_handler
[value
& 0xff];
1812 FLOPPY_DPRINTF("%s command\n", handlers
[pos
].name
);
1813 fdctrl
->data_len
= handlers
[pos
].parameters
+ 1;
1814 fdctrl
->msr
|= FD_MSR_CMDBUSY
;
1817 FLOPPY_DPRINTF("%s: %02x\n", __func__
, value
);
1818 fdctrl
->fifo
[fdctrl
->data_pos
++] = value
;
1819 if (fdctrl
->data_pos
== fdctrl
->data_len
) {
1820 /* We now have all parameters
1821 * and will be able to treat the command
1823 if (fdctrl
->data_state
& FD_STATE_FORMAT
) {
1824 fdctrl_format_sector(fdctrl
);
1828 pos
= command_to_handler
[fdctrl
->fifo
[0] & 0xff];
1829 FLOPPY_DPRINTF("treat %s command\n", handlers
[pos
].name
);
1830 (*handlers
[pos
].handler
)(fdctrl
, handlers
[pos
].direction
);
1834 static void fdctrl_result_timer(void *opaque
)
1836 FDCtrl
*fdctrl
= opaque
;
1837 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1839 /* Pretend we are spinning.
1840 * This is needed for Coherent, which uses READ ID to check for
1841 * sector interleaving.
1843 if (cur_drv
->last_sect
!= 0) {
1844 cur_drv
->sect
= (cur_drv
->sect
% cur_drv
->last_sect
) + 1;
1846 /* READ_ID can't automatically succeed! */
1847 if (fdctrl
->check_media_rate
&&
1848 (fdctrl
->dsr
& FD_DSR_DRATEMASK
) != cur_drv
->media_rate
) {
1849 FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n",
1850 fdctrl
->dsr
& FD_DSR_DRATEMASK
, cur_drv
->media_rate
);
1851 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_MA
, 0x00);
1853 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1857 static void fdctrl_change_cb(void *opaque
, bool load
)
1859 FDrive
*drive
= opaque
;
1861 drive
->media_changed
= 1;
1862 fd_revalidate(drive
);
1865 static const BlockDevOps fdctrl_block_ops
= {
1866 .change_media_cb
= fdctrl_change_cb
,
1869 /* Init functions */
1870 static int fdctrl_connect_drives(FDCtrl
*fdctrl
)
1875 for (i
= 0; i
< MAX_FD
; i
++) {
1876 drive
= &fdctrl
->drives
[i
];
1877 drive
->fdctrl
= fdctrl
;
1880 if (bdrv_get_on_error(drive
->bs
, 0) != BLOCK_ERR_STOP_ENOSPC
) {
1881 error_report("fdc doesn't support drive option werror");
1884 if (bdrv_get_on_error(drive
->bs
, 1) != BLOCK_ERR_REPORT
) {
1885 error_report("fdc doesn't support drive option rerror");
1891 fdctrl_change_cb(drive
, 0);
1893 bdrv_set_dev_ops(drive
->bs
, &fdctrl_block_ops
, drive
);
1899 ISADevice
*fdctrl_init_isa(ISABus
*bus
, DriveInfo
**fds
)
1903 dev
= isa_try_create(bus
, "isa-fdc");
1909 qdev_prop_set_drive_nofail(&dev
->qdev
, "driveA", fds
[0]->bdrv
);
1912 qdev_prop_set_drive_nofail(&dev
->qdev
, "driveB", fds
[1]->bdrv
);
1914 qdev_init_nofail(&dev
->qdev
);
1919 void fdctrl_init_sysbus(qemu_irq irq
, int dma_chann
,
1920 target_phys_addr_t mmio_base
, DriveInfo
**fds
)
1926 dev
= qdev_create(NULL
, "sysbus-fdc");
1927 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1928 fdctrl
= &sys
->state
;
1929 fdctrl
->dma_chann
= dma_chann
; /* FIXME */
1931 qdev_prop_set_drive_nofail(dev
, "driveA", fds
[0]->bdrv
);
1934 qdev_prop_set_drive_nofail(dev
, "driveB", fds
[1]->bdrv
);
1936 qdev_init_nofail(dev
);
1937 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1938 sysbus_mmio_map(&sys
->busdev
, 0, mmio_base
);
1941 void sun4m_fdctrl_init(qemu_irq irq
, target_phys_addr_t io_base
,
1942 DriveInfo
**fds
, qemu_irq
*fdc_tc
)
1947 dev
= qdev_create(NULL
, "SUNW,fdtwo");
1949 qdev_prop_set_drive_nofail(dev
, "drive", fds
[0]->bdrv
);
1951 qdev_init_nofail(dev
);
1952 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1953 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1954 sysbus_mmio_map(&sys
->busdev
, 0, io_base
);
1955 *fdc_tc
= qdev_get_gpio_in(dev
, 0);
1958 static int fdctrl_init_common(FDCtrl
*fdctrl
)
1961 static int command_tables_inited
= 0;
1963 /* Fill 'command_to_handler' lookup table */
1964 if (!command_tables_inited
) {
1965 command_tables_inited
= 1;
1966 for (i
= ARRAY_SIZE(handlers
) - 1; i
>= 0; i
--) {
1967 for (j
= 0; j
< sizeof(command_to_handler
); j
++) {
1968 if ((j
& handlers
[i
].mask
) == handlers
[i
].value
) {
1969 command_to_handler
[j
] = i
;
1975 FLOPPY_DPRINTF("init controller\n");
1976 fdctrl
->fifo
= qemu_memalign(512, FD_SECTOR_LEN
);
1977 fdctrl
->fifo_size
= 512;
1978 fdctrl
->result_timer
= qemu_new_timer_ns(vm_clock
,
1979 fdctrl_result_timer
, fdctrl
);
1981 fdctrl
->version
= 0x90; /* Intel 82078 controller */
1982 fdctrl
->config
= FD_CONFIG_EIS
| FD_CONFIG_EFIFO
; /* Implicit seek, polling & FIFO enabled */
1983 fdctrl
->num_floppies
= MAX_FD
;
1985 if (fdctrl
->dma_chann
!= -1)
1986 DMA_register_channel(fdctrl
->dma_chann
, &fdctrl_transfer_handler
, fdctrl
);
1987 return fdctrl_connect_drives(fdctrl
);
1990 static const MemoryRegionPortio fdc_portio_list
[] = {
1991 { 1, 5, 1, .read
= fdctrl_read
, .write
= fdctrl_write
},
1992 { 7, 1, 1, .read
= fdctrl_read
, .write
= fdctrl_write
},
1993 PORTIO_END_OF_LIST(),
1996 static int isabus_fdc_init1(ISADevice
*dev
)
1998 FDCtrlISABus
*isa
= DO_UPCAST(FDCtrlISABus
, busdev
, dev
);
1999 FDCtrl
*fdctrl
= &isa
->state
;
2002 isa_register_portio_list(dev
, isa
->iobase
, fdc_portio_list
, fdctrl
, "fdc");
2004 isa_init_irq(&isa
->busdev
, &fdctrl
->irq
, isa
->irq
);
2005 fdctrl
->dma_chann
= isa
->dma
;
2007 qdev_set_legacy_instance_id(&dev
->qdev
, isa
->iobase
, 2);
2008 ret
= fdctrl_init_common(fdctrl
);
2010 add_boot_device_path(isa
->bootindexA
, &dev
->qdev
, "/floppy@0");
2011 add_boot_device_path(isa
->bootindexB
, &dev
->qdev
, "/floppy@1");
2016 static int sysbus_fdc_init1(SysBusDevice
*dev
)
2018 FDCtrlSysBus
*sys
= DO_UPCAST(FDCtrlSysBus
, busdev
, dev
);
2019 FDCtrl
*fdctrl
= &sys
->state
;
2022 memory_region_init_io(&fdctrl
->iomem
, &fdctrl_mem_ops
, fdctrl
, "fdc", 0x08);
2023 sysbus_init_mmio(dev
, &fdctrl
->iomem
);
2024 sysbus_init_irq(dev
, &fdctrl
->irq
);
2025 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
2026 fdctrl
->dma_chann
= -1;
2028 qdev_set_legacy_instance_id(&dev
->qdev
, 0 /* io */, 2); /* FIXME */
2029 ret
= fdctrl_init_common(fdctrl
);
2034 static int sun4m_fdc_init1(SysBusDevice
*dev
)
2036 FDCtrl
*fdctrl
= &(FROM_SYSBUS(FDCtrlSysBus
, dev
)->state
);
2038 memory_region_init_io(&fdctrl
->iomem
, &fdctrl_mem_strict_ops
, fdctrl
,
2040 sysbus_init_mmio(dev
, &fdctrl
->iomem
);
2041 sysbus_init_irq(dev
, &fdctrl
->irq
);
2042 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
2045 qdev_set_legacy_instance_id(&dev
->qdev
, 0 /* io */, 2); /* FIXME */
2046 return fdctrl_init_common(fdctrl
);
2049 void fdc_get_bs(BlockDriverState
*bs
[], ISADevice
*dev
)
2051 FDCtrlISABus
*isa
= DO_UPCAST(FDCtrlISABus
, busdev
, dev
);
2052 FDCtrl
*fdctrl
= &isa
->state
;
2055 for (i
= 0; i
< MAX_FD
; i
++) {
2056 bs
[i
] = fdctrl
->drives
[i
].bs
;
2061 static const VMStateDescription vmstate_isa_fdc
={
2064 .minimum_version_id
= 2,
2065 .fields
= (VMStateField
[]) {
2066 VMSTATE_STRUCT(state
, FDCtrlISABus
, 0, vmstate_fdc
, FDCtrl
),
2067 VMSTATE_END_OF_LIST()
2071 static Property isa_fdc_properties
[] = {
2072 DEFINE_PROP_HEX32("iobase", FDCtrlISABus
, iobase
, 0x3f0),
2073 DEFINE_PROP_UINT32("irq", FDCtrlISABus
, irq
, 6),
2074 DEFINE_PROP_UINT32("dma", FDCtrlISABus
, dma
, 2),
2075 DEFINE_PROP_DRIVE("driveA", FDCtrlISABus
, state
.drives
[0].bs
),
2076 DEFINE_PROP_DRIVE("driveB", FDCtrlISABus
, state
.drives
[1].bs
),
2077 DEFINE_PROP_INT32("bootindexA", FDCtrlISABus
, bootindexA
, -1),
2078 DEFINE_PROP_INT32("bootindexB", FDCtrlISABus
, bootindexB
, -1),
2079 DEFINE_PROP_BIT("check_media_rate", FDCtrlISABus
, state
.check_media_rate
,
2081 DEFINE_PROP_END_OF_LIST(),
2084 static void isabus_fdc_class_init1(ObjectClass
*klass
, void *data
)
2086 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2087 ISADeviceClass
*ic
= ISA_DEVICE_CLASS(klass
);
2088 ic
->init
= isabus_fdc_init1
;
2089 dc
->fw_name
= "fdc";
2091 dc
->reset
= fdctrl_external_reset_isa
;
2092 dc
->vmsd
= &vmstate_isa_fdc
;
2093 dc
->props
= isa_fdc_properties
;
2096 static TypeInfo isa_fdc_info
= {
2098 .parent
= TYPE_ISA_DEVICE
,
2099 .instance_size
= sizeof(FDCtrlISABus
),
2100 .class_init
= isabus_fdc_class_init1
,
2103 static const VMStateDescription vmstate_sysbus_fdc
={
2106 .minimum_version_id
= 2,
2107 .fields
= (VMStateField
[]) {
2108 VMSTATE_STRUCT(state
, FDCtrlSysBus
, 0, vmstate_fdc
, FDCtrl
),
2109 VMSTATE_END_OF_LIST()
2113 static Property sysbus_fdc_properties
[] = {
2114 DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus
, state
.drives
[0].bs
),
2115 DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus
, state
.drives
[1].bs
),
2116 DEFINE_PROP_END_OF_LIST(),
2119 static void sysbus_fdc_class_init(ObjectClass
*klass
, void *data
)
2121 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2122 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
2124 k
->init
= sysbus_fdc_init1
;
2125 dc
->reset
= fdctrl_external_reset_sysbus
;
2126 dc
->vmsd
= &vmstate_sysbus_fdc
;
2127 dc
->props
= sysbus_fdc_properties
;
2130 static TypeInfo sysbus_fdc_info
= {
2131 .name
= "sysbus-fdc",
2132 .parent
= TYPE_SYS_BUS_DEVICE
,
2133 .instance_size
= sizeof(FDCtrlSysBus
),
2134 .class_init
= sysbus_fdc_class_init
,
2137 static Property sun4m_fdc_properties
[] = {
2138 DEFINE_PROP_DRIVE("drive", FDCtrlSysBus
, state
.drives
[0].bs
),
2139 DEFINE_PROP_END_OF_LIST(),
2142 static void sun4m_fdc_class_init(ObjectClass
*klass
, void *data
)
2144 DeviceClass
*dc
= DEVICE_CLASS(klass
);
2145 SysBusDeviceClass
*k
= SYS_BUS_DEVICE_CLASS(klass
);
2147 k
->init
= sun4m_fdc_init1
;
2148 dc
->reset
= fdctrl_external_reset_sysbus
;
2149 dc
->vmsd
= &vmstate_sysbus_fdc
;
2150 dc
->props
= sun4m_fdc_properties
;
2153 static TypeInfo sun4m_fdc_info
= {
2154 .name
= "SUNW,fdtwo",
2155 .parent
= TYPE_SYS_BUS_DEVICE
,
2156 .instance_size
= sizeof(FDCtrlSysBus
),
2157 .class_init
= sun4m_fdc_class_init
,
2160 static void fdc_register_types(void)
2162 type_register_static(&isa_fdc_info
);
2163 type_register_static(&sysbus_fdc_info
);
2164 type_register_static(&sun4m_fdc_info
);
2167 type_init(fdc_register_types
)