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.
33 #include "qemu-timer.h"
36 #include "qdev-addr.h"
38 /********************************************************/
39 /* debug Floppy devices */
40 //#define DEBUG_FLOPPY
43 #define FLOPPY_DPRINTF(fmt, ...) \
44 do { printf("FLOPPY: " fmt , ## __VA_ARGS__); } while (0)
46 #define FLOPPY_DPRINTF(fmt, ...)
49 #define FLOPPY_ERROR(fmt, ...) \
50 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
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 /* Floppy disk drive emulation */
64 typedef enum FDiskType
{
65 FDRIVE_DISK_288
= 0x01, /* 2.88 MB disk */
66 FDRIVE_DISK_144
= 0x02, /* 1.44 MB disk */
67 FDRIVE_DISK_720
= 0x03, /* 720 kB disk */
68 FDRIVE_DISK_USER
= 0x04, /* User defined geometry */
69 FDRIVE_DISK_NONE
= 0x05, /* No disk */
72 typedef enum FDriveType
{
73 FDRIVE_DRV_144
= 0x00, /* 1.44 MB 3"5 drive */
74 FDRIVE_DRV_288
= 0x01, /* 2.88 MB 3"5 drive */
75 FDRIVE_DRV_120
= 0x02, /* 1.2 MB 5"25 drive */
76 FDRIVE_DRV_NONE
= 0x03, /* No drive connected */
79 typedef enum FDiskFlags
{
80 FDISK_DBL_SIDES
= 0x01,
83 typedef struct FDrive
{
88 uint8_t perpendicular
; /* 2.88 MB access mode */
95 uint8_t last_sect
; /* Nb sector per track */
96 uint8_t max_track
; /* Nb of tracks */
97 uint16_t bps
; /* Bytes per sector */
98 uint8_t ro
; /* Is read-only */
101 static void fd_init(FDrive
*drv
)
104 drv
->bs
= drv
->dinfo
? drv
->dinfo
->bdrv
: NULL
;
105 drv
->drive
= FDRIVE_DRV_NONE
;
106 drv
->perpendicular
= 0;
112 static int fd_sector_calc(uint8_t head
, uint8_t track
, uint8_t sect
,
115 return (((track
* 2) + head
) * last_sect
) + sect
- 1;
118 /* Returns current position, in sectors, for given drive */
119 static int fd_sector(FDrive
*drv
)
121 return fd_sector_calc(drv
->head
, drv
->track
, drv
->sect
, drv
->last_sect
);
124 /* Seek to a new position:
125 * returns 0 if already on right track
126 * returns 1 if track changed
127 * returns 2 if track is invalid
128 * returns 3 if sector is invalid
129 * returns 4 if seek is disabled
131 static int fd_seek(FDrive
*drv
, uint8_t head
, uint8_t track
, uint8_t sect
,
137 if (track
> drv
->max_track
||
138 (head
!= 0 && (drv
->flags
& FDISK_DBL_SIDES
) == 0)) {
139 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
140 head
, track
, sect
, 1,
141 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
142 drv
->max_track
, drv
->last_sect
);
145 if (sect
> drv
->last_sect
) {
146 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
147 head
, track
, sect
, 1,
148 (drv
->flags
& FDISK_DBL_SIDES
) == 0 ? 0 : 1,
149 drv
->max_track
, drv
->last_sect
);
152 sector
= fd_sector_calc(head
, track
, sect
, drv
->last_sect
);
154 if (sector
!= fd_sector(drv
)) {
157 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
158 head
, track
, sect
, 1, drv
->max_track
, drv
->last_sect
);
163 if (drv
->track
!= track
)
172 /* Set drive back to track 0 */
173 static void fd_recalibrate(FDrive
*drv
)
175 FLOPPY_DPRINTF("recalibrate\n");
181 /* Recognize floppy formats */
182 typedef struct FDFormat
{
191 static const FDFormat fd_formats
[] = {
192 /* First entry is default format */
193 /* 1.44 MB 3"1/2 floppy disks */
194 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 18, 80, 1, "1.44 MB 3\"1/2", },
195 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 20, 80, 1, "1.6 MB 3\"1/2", },
196 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 80, 1, "1.68 MB 3\"1/2", },
197 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 82, 1, "1.72 MB 3\"1/2", },
198 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 21, 83, 1, "1.74 MB 3\"1/2", },
199 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 22, 80, 1, "1.76 MB 3\"1/2", },
200 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 23, 80, 1, "1.84 MB 3\"1/2", },
201 { FDRIVE_DRV_144
, FDRIVE_DISK_144
, 24, 80, 1, "1.92 MB 3\"1/2", },
202 /* 2.88 MB 3"1/2 floppy disks */
203 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 36, 80, 1, "2.88 MB 3\"1/2", },
204 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 39, 80, 1, "3.12 MB 3\"1/2", },
205 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 40, 80, 1, "3.2 MB 3\"1/2", },
206 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 44, 80, 1, "3.52 MB 3\"1/2", },
207 { FDRIVE_DRV_288
, FDRIVE_DISK_288
, 48, 80, 1, "3.84 MB 3\"1/2", },
208 /* 720 kB 3"1/2 floppy disks */
209 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 9, 80, 1, "720 kB 3\"1/2", },
210 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 80, 1, "800 kB 3\"1/2", },
211 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 82, 1, "820 kB 3\"1/2", },
212 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 10, 83, 1, "830 kB 3\"1/2", },
213 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 13, 80, 1, "1.04 MB 3\"1/2", },
214 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 14, 80, 1, "1.12 MB 3\"1/2", },
215 /* 1.2 MB 5"1/4 floppy disks */
216 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 15, 80, 1, "1.2 kB 5\"1/4", },
217 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 80, 1, "1.44 MB 5\"1/4", },
218 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 82, 1, "1.48 MB 5\"1/4", },
219 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 18, 83, 1, "1.49 MB 5\"1/4", },
220 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 20, 80, 1, "1.6 MB 5\"1/4", },
221 /* 720 kB 5"1/4 floppy disks */
222 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 80, 1, "720 kB 5\"1/4", },
223 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 11, 80, 1, "880 kB 5\"1/4", },
224 /* 360 kB 5"1/4 floppy disks */
225 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 40, 1, "360 kB 5\"1/4", },
226 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 9, 40, 0, "180 kB 5\"1/4", },
227 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 10, 41, 1, "410 kB 5\"1/4", },
228 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 10, 42, 1, "420 kB 5\"1/4", },
229 /* 320 kB 5"1/4 floppy disks */
230 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 8, 40, 1, "320 kB 5\"1/4", },
231 { FDRIVE_DRV_120
, FDRIVE_DISK_288
, 8, 40, 0, "160 kB 5\"1/4", },
232 /* 360 kB must match 5"1/4 better than 3"1/2... */
233 { FDRIVE_DRV_144
, FDRIVE_DISK_720
, 9, 80, 0, "360 kB 3\"1/2", },
235 { FDRIVE_DRV_NONE
, FDRIVE_DISK_NONE
, -1, -1, 0, NULL
, },
238 /* Revalidate a disk drive after a disk change */
239 static void fd_revalidate(FDrive
*drv
)
241 const FDFormat
*parse
;
242 uint64_t nb_sectors
, size
;
243 int i
, first_match
, match
;
244 int nb_heads
, max_track
, last_sect
, ro
;
246 FLOPPY_DPRINTF("revalidate\n");
247 if (drv
->bs
!= NULL
&& bdrv_is_inserted(drv
->bs
)) {
248 ro
= bdrv_is_read_only(drv
->bs
);
249 bdrv_get_geometry_hint(drv
->bs
, &nb_heads
, &max_track
, &last_sect
);
250 if (nb_heads
!= 0 && max_track
!= 0 && last_sect
!= 0) {
251 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
252 nb_heads
- 1, max_track
, last_sect
);
254 bdrv_get_geometry(drv
->bs
, &nb_sectors
);
258 parse
= &fd_formats
[i
];
259 if (parse
->drive
== FDRIVE_DRV_NONE
)
261 if (drv
->drive
== parse
->drive
||
262 drv
->drive
== FDRIVE_DRV_NONE
) {
263 size
= (parse
->max_head
+ 1) * parse
->max_track
*
265 if (nb_sectors
== size
) {
269 if (first_match
== -1)
274 if (first_match
== -1)
278 parse
= &fd_formats
[match
];
280 nb_heads
= parse
->max_head
+ 1;
281 max_track
= parse
->max_track
;
282 last_sect
= parse
->last_sect
;
283 drv
->drive
= parse
->drive
;
284 FLOPPY_DPRINTF("%s floppy disk (%d h %d t %d s) %s\n", parse
->str
,
285 nb_heads
, max_track
, last_sect
, ro
? "ro" : "rw");
288 drv
->flags
&= ~FDISK_DBL_SIDES
;
290 drv
->flags
|= FDISK_DBL_SIDES
;
292 drv
->max_track
= max_track
;
293 drv
->last_sect
= last_sect
;
296 FLOPPY_DPRINTF("No disk in drive\n");
299 drv
->flags
&= ~FDISK_DBL_SIDES
;
303 /********************************************************/
304 /* Intel 82078 floppy disk controller emulation */
306 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
);
307 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
);
308 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
309 int dma_pos
, int dma_len
);
310 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
);
312 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
);
313 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
);
314 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
);
315 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
);
316 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
);
317 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
);
318 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
);
319 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
);
320 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
);
321 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
);
322 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
);
333 FD_STATE_MULTI
= 0x01, /* multi track flag */
334 FD_STATE_FORMAT
= 0x02, /* format flag */
335 FD_STATE_SEEK
= 0x04, /* seek flag */
350 FD_CMD_READ_TRACK
= 0x02,
351 FD_CMD_SPECIFY
= 0x03,
352 FD_CMD_SENSE_DRIVE_STATUS
= 0x04,
355 FD_CMD_RECALIBRATE
= 0x07,
356 FD_CMD_SENSE_INTERRUPT_STATUS
= 0x08,
357 FD_CMD_WRITE_DELETED
= 0x09,
358 FD_CMD_READ_ID
= 0x0a,
359 FD_CMD_READ_DELETED
= 0x0c,
360 FD_CMD_FORMAT_TRACK
= 0x0d,
361 FD_CMD_DUMPREG
= 0x0e,
363 FD_CMD_VERSION
= 0x10,
364 FD_CMD_SCAN_EQUAL
= 0x11,
365 FD_CMD_PERPENDICULAR_MODE
= 0x12,
366 FD_CMD_CONFIGURE
= 0x13,
368 FD_CMD_VERIFY
= 0x16,
369 FD_CMD_POWERDOWN_MODE
= 0x17,
370 FD_CMD_PART_ID
= 0x18,
371 FD_CMD_SCAN_LOW_OR_EQUAL
= 0x19,
372 FD_CMD_SCAN_HIGH_OR_EQUAL
= 0x1d,
374 FD_CMD_OPTION
= 0x33,
375 FD_CMD_RESTORE
= 0x4c,
376 FD_CMD_DRIVE_SPECIFICATION_COMMAND
= 0x8e,
377 FD_CMD_RELATIVE_SEEK_OUT
= 0x8f,
378 FD_CMD_FORMAT_AND_WRITE
= 0xcd,
379 FD_CMD_RELATIVE_SEEK_IN
= 0xcf,
383 FD_CONFIG_PRETRK
= 0xff, /* Pre-compensation set to track 0 */
384 FD_CONFIG_FIFOTHR
= 0x0f, /* FIFO threshold set to 1 byte */
385 FD_CONFIG_POLL
= 0x10, /* Poll enabled */
386 FD_CONFIG_EFIFO
= 0x20, /* FIFO disabled */
387 FD_CONFIG_EIS
= 0x40, /* No implied seeks */
393 FD_SR0_ABNTERM
= 0x40,
394 FD_SR0_INVCMD
= 0x80,
395 FD_SR0_RDYCHG
= 0xc0,
399 FD_SR1_EC
= 0x80, /* End of cylinder */
403 FD_SR2_SNS
= 0x04, /* Scan not satisfied */
404 FD_SR2_SEH
= 0x08, /* Scan equal hit */
415 FD_SRA_INTPEND
= 0x80,
429 FD_DOR_SELMASK
= 0x03,
431 FD_DOR_SELMASK
= 0x01,
433 FD_DOR_nRESET
= 0x04,
435 FD_DOR_MOTEN0
= 0x10,
436 FD_DOR_MOTEN1
= 0x20,
437 FD_DOR_MOTEN2
= 0x40,
438 FD_DOR_MOTEN3
= 0x80,
443 FD_TDR_BOOTSEL
= 0x0c,
445 FD_TDR_BOOTSEL
= 0x04,
450 FD_DSR_DRATEMASK
= 0x03,
451 FD_DSR_PWRDOWN
= 0x40,
452 FD_DSR_SWRESET
= 0x80,
456 FD_MSR_DRV0BUSY
= 0x01,
457 FD_MSR_DRV1BUSY
= 0x02,
458 FD_MSR_DRV2BUSY
= 0x04,
459 FD_MSR_DRV3BUSY
= 0x08,
460 FD_MSR_CMDBUSY
= 0x10,
461 FD_MSR_NONDMA
= 0x20,
467 FD_DIR_DSKCHG
= 0x80,
470 #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
471 #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
472 #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
475 /* Controller's identification */
480 /* Controller state */
481 QEMUTimer
*result_timer
;
485 uint8_t dor_vmstate
; /* only used as temp during vmstate */
500 uint8_t eot
; /* last wanted sector */
501 /* States kept only to be returned back */
505 /* precompensation */
509 /* Power down config (also with status regB access mode */
514 uint8_t num_floppies
;
515 FDrive drives
[MAX_FD
];
519 typedef struct FDCtrlSysBus
{
524 typedef struct FDCtrlISABus
{
529 static uint32_t fdctrl_read (void *opaque
, uint32_t reg
)
531 FDCtrl
*fdctrl
= opaque
;
536 retval
= fdctrl_read_statusA(fdctrl
);
539 retval
= fdctrl_read_statusB(fdctrl
);
542 retval
= fdctrl_read_dor(fdctrl
);
545 retval
= fdctrl_read_tape(fdctrl
);
548 retval
= fdctrl_read_main_status(fdctrl
);
551 retval
= fdctrl_read_data(fdctrl
);
554 retval
= fdctrl_read_dir(fdctrl
);
557 retval
= (uint32_t)(-1);
560 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg
& 7, retval
);
565 static void fdctrl_write (void *opaque
, uint32_t reg
, uint32_t value
)
567 FDCtrl
*fdctrl
= opaque
;
569 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg
& 7, value
);
573 fdctrl_write_dor(fdctrl
, value
);
576 fdctrl_write_tape(fdctrl
, value
);
579 fdctrl_write_rate(fdctrl
, value
);
582 fdctrl_write_data(fdctrl
, value
);
589 static uint32_t fdctrl_read_port (void *opaque
, uint32_t reg
)
591 return fdctrl_read(opaque
, reg
& 7);
594 static void fdctrl_write_port (void *opaque
, uint32_t reg
, uint32_t value
)
596 fdctrl_write(opaque
, reg
& 7, value
);
599 static uint32_t fdctrl_read_mem (void *opaque
, target_phys_addr_t reg
)
601 return fdctrl_read(opaque
, (uint32_t)reg
);
604 static void fdctrl_write_mem (void *opaque
,
605 target_phys_addr_t reg
, uint32_t value
)
607 fdctrl_write(opaque
, (uint32_t)reg
, value
);
610 static CPUReadMemoryFunc
* const fdctrl_mem_read
[3] = {
616 static CPUWriteMemoryFunc
* const fdctrl_mem_write
[3] = {
622 static CPUReadMemoryFunc
* const fdctrl_mem_read_strict
[3] = {
628 static CPUWriteMemoryFunc
* const fdctrl_mem_write_strict
[3] = {
634 static const VMStateDescription vmstate_fdrive
= {
637 .minimum_version_id
= 1,
638 .minimum_version_id_old
= 1,
639 .fields
= (VMStateField
[]) {
640 VMSTATE_UINT8(head
, FDrive
),
641 VMSTATE_UINT8(track
, FDrive
),
642 VMSTATE_UINT8(sect
, FDrive
),
643 VMSTATE_END_OF_LIST()
647 static void fdc_pre_save(void *opaque
)
651 s
->dor_vmstate
= s
->dor
| GET_CUR_DRV(s
);
654 static int fdc_post_load(void *opaque
, int version_id
)
658 SET_CUR_DRV(s
, s
->dor_vmstate
& FD_DOR_SELMASK
);
659 s
->dor
= s
->dor_vmstate
& ~FD_DOR_SELMASK
;
663 static const VMStateDescription vmstate_fdc
= {
666 .minimum_version_id
= 2,
667 .minimum_version_id_old
= 2,
668 .pre_save
= fdc_pre_save
,
669 .post_load
= fdc_post_load
,
670 .fields
= (VMStateField
[]) {
671 /* Controller State */
672 VMSTATE_UINT8(sra
, FDCtrl
),
673 VMSTATE_UINT8(srb
, FDCtrl
),
674 VMSTATE_UINT8(dor_vmstate
, FDCtrl
),
675 VMSTATE_UINT8(tdr
, FDCtrl
),
676 VMSTATE_UINT8(dsr
, FDCtrl
),
677 VMSTATE_UINT8(msr
, FDCtrl
),
678 VMSTATE_UINT8(status0
, FDCtrl
),
679 VMSTATE_UINT8(status1
, FDCtrl
),
680 VMSTATE_UINT8(status2
, FDCtrl
),
682 VMSTATE_VARRAY_INT32(fifo
, FDCtrl
, fifo_size
, 0, vmstate_info_uint8
,
684 VMSTATE_UINT32(data_pos
, FDCtrl
),
685 VMSTATE_UINT32(data_len
, FDCtrl
),
686 VMSTATE_UINT8(data_state
, FDCtrl
),
687 VMSTATE_UINT8(data_dir
, FDCtrl
),
688 VMSTATE_UINT8(eot
, FDCtrl
),
689 /* States kept only to be returned back */
690 VMSTATE_UINT8(timer0
, FDCtrl
),
691 VMSTATE_UINT8(timer1
, FDCtrl
),
692 VMSTATE_UINT8(precomp_trk
, FDCtrl
),
693 VMSTATE_UINT8(config
, FDCtrl
),
694 VMSTATE_UINT8(lock
, FDCtrl
),
695 VMSTATE_UINT8(pwrd
, FDCtrl
),
696 VMSTATE_UINT8_EQUAL(num_floppies
, FDCtrl
),
697 VMSTATE_STRUCT_ARRAY(drives
, FDCtrl
, MAX_FD
, 1,
698 vmstate_fdrive
, FDrive
),
699 VMSTATE_END_OF_LIST()
703 static void fdctrl_external_reset_sysbus(DeviceState
*d
)
705 FDCtrlSysBus
*sys
= container_of(d
, FDCtrlSysBus
, busdev
.qdev
);
706 FDCtrl
*s
= &sys
->state
;
711 static void fdctrl_external_reset_isa(DeviceState
*d
)
713 FDCtrlISABus
*isa
= container_of(d
, FDCtrlISABus
, busdev
.qdev
);
714 FDCtrl
*s
= &isa
->state
;
719 static void fdctrl_handle_tc(void *opaque
, int irq
, int level
)
721 //FDCtrl *s = opaque;
725 FLOPPY_DPRINTF("TC pulsed\n");
729 /* XXX: may change if moved to bdrv */
730 int fdctrl_get_drive_type(FDCtrl
*fdctrl
, int drive_num
)
732 return fdctrl
->drives
[drive_num
].drive
;
735 /* Change IRQ state */
736 static void fdctrl_reset_irq(FDCtrl
*fdctrl
)
738 if (!(fdctrl
->sra
& FD_SRA_INTPEND
))
740 FLOPPY_DPRINTF("Reset interrupt\n");
741 qemu_set_irq(fdctrl
->irq
, 0);
742 fdctrl
->sra
&= ~FD_SRA_INTPEND
;
745 static void fdctrl_raise_irq(FDCtrl
*fdctrl
, uint8_t status0
)
748 if (fdctrl
->sun4m
&& (fdctrl
->msr
& FD_MSR_CMDBUSY
)) {
750 fdctrl
->msr
&= ~FD_MSR_CMDBUSY
;
751 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
752 fdctrl
->status0
= status0
;
755 if (!(fdctrl
->sra
& FD_SRA_INTPEND
)) {
756 qemu_set_irq(fdctrl
->irq
, 1);
757 fdctrl
->sra
|= FD_SRA_INTPEND
;
759 fdctrl
->reset_sensei
= 0;
760 fdctrl
->status0
= status0
;
761 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl
->status0
);
764 /* Reset controller */
765 static void fdctrl_reset(FDCtrl
*fdctrl
, int do_irq
)
769 FLOPPY_DPRINTF("reset controller\n");
770 fdctrl_reset_irq(fdctrl
);
771 /* Initialise controller */
774 if (!fdctrl
->drives
[1].bs
)
775 fdctrl
->sra
|= FD_SRA_nDRV2
;
777 fdctrl
->dor
= FD_DOR_nRESET
;
778 fdctrl
->dor
|= (fdctrl
->dma_chann
!= -1) ? FD_DOR_DMAEN
: 0;
779 fdctrl
->msr
= FD_MSR_RQM
;
781 fdctrl
->data_pos
= 0;
782 fdctrl
->data_len
= 0;
783 fdctrl
->data_state
= 0;
784 fdctrl
->data_dir
= FD_DIR_WRITE
;
785 for (i
= 0; i
< MAX_FD
; i
++)
786 fd_recalibrate(&fdctrl
->drives
[i
]);
787 fdctrl_reset_fifo(fdctrl
);
789 fdctrl_raise_irq(fdctrl
, FD_SR0_RDYCHG
);
790 fdctrl
->reset_sensei
= FD_RESET_SENSEI_COUNT
;
794 static inline FDrive
*drv0(FDCtrl
*fdctrl
)
796 return &fdctrl
->drives
[(fdctrl
->tdr
& FD_TDR_BOOTSEL
) >> 2];
799 static inline FDrive
*drv1(FDCtrl
*fdctrl
)
801 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (1 << 2))
802 return &fdctrl
->drives
[1];
804 return &fdctrl
->drives
[0];
808 static inline FDrive
*drv2(FDCtrl
*fdctrl
)
810 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (2 << 2))
811 return &fdctrl
->drives
[2];
813 return &fdctrl
->drives
[1];
816 static inline FDrive
*drv3(FDCtrl
*fdctrl
)
818 if ((fdctrl
->tdr
& FD_TDR_BOOTSEL
) < (3 << 2))
819 return &fdctrl
->drives
[3];
821 return &fdctrl
->drives
[2];
825 static FDrive
*get_cur_drv(FDCtrl
*fdctrl
)
827 switch (fdctrl
->cur_drv
) {
828 case 0: return drv0(fdctrl
);
829 case 1: return drv1(fdctrl
);
831 case 2: return drv2(fdctrl
);
832 case 3: return drv3(fdctrl
);
834 default: return NULL
;
838 /* Status A register : 0x00 (read-only) */
839 static uint32_t fdctrl_read_statusA(FDCtrl
*fdctrl
)
841 uint32_t retval
= fdctrl
->sra
;
843 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval
);
848 /* Status B register : 0x01 (read-only) */
849 static uint32_t fdctrl_read_statusB(FDCtrl
*fdctrl
)
851 uint32_t retval
= fdctrl
->srb
;
853 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval
);
858 /* Digital output register : 0x02 */
859 static uint32_t fdctrl_read_dor(FDCtrl
*fdctrl
)
861 uint32_t retval
= fdctrl
->dor
;
864 retval
|= fdctrl
->cur_drv
;
865 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval
);
870 static void fdctrl_write_dor(FDCtrl
*fdctrl
, uint32_t value
)
872 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value
);
875 if (value
& FD_DOR_MOTEN0
)
876 fdctrl
->srb
|= FD_SRB_MTR0
;
878 fdctrl
->srb
&= ~FD_SRB_MTR0
;
879 if (value
& FD_DOR_MOTEN1
)
880 fdctrl
->srb
|= FD_SRB_MTR1
;
882 fdctrl
->srb
&= ~FD_SRB_MTR1
;
886 fdctrl
->srb
|= FD_SRB_DR0
;
888 fdctrl
->srb
&= ~FD_SRB_DR0
;
891 if (!(value
& FD_DOR_nRESET
)) {
892 if (fdctrl
->dor
& FD_DOR_nRESET
) {
893 FLOPPY_DPRINTF("controller enter RESET state\n");
896 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
897 FLOPPY_DPRINTF("controller out of RESET state\n");
898 fdctrl_reset(fdctrl
, 1);
899 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
903 fdctrl
->cur_drv
= value
& FD_DOR_SELMASK
;
908 /* Tape drive register : 0x03 */
909 static uint32_t fdctrl_read_tape(FDCtrl
*fdctrl
)
911 uint32_t retval
= fdctrl
->tdr
;
913 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval
);
918 static void fdctrl_write_tape(FDCtrl
*fdctrl
, uint32_t value
)
921 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
922 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
925 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value
);
926 /* Disk boot selection indicator */
927 fdctrl
->tdr
= value
& FD_TDR_BOOTSEL
;
928 /* Tape indicators: never allow */
931 /* Main status register : 0x04 (read) */
932 static uint32_t fdctrl_read_main_status(FDCtrl
*fdctrl
)
934 uint32_t retval
= fdctrl
->msr
;
936 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
937 fdctrl
->dor
|= FD_DOR_nRESET
;
941 retval
|= FD_MSR_DIO
;
942 fdctrl_reset_irq(fdctrl
);
945 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval
);
950 /* Data select rate register : 0x04 (write) */
951 static void fdctrl_write_rate(FDCtrl
*fdctrl
, uint32_t value
)
954 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
955 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
958 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value
);
959 /* Reset: autoclear */
960 if (value
& FD_DSR_SWRESET
) {
961 fdctrl
->dor
&= ~FD_DOR_nRESET
;
962 fdctrl_reset(fdctrl
, 1);
963 fdctrl
->dor
|= FD_DOR_nRESET
;
965 if (value
& FD_DSR_PWRDOWN
) {
966 fdctrl_reset(fdctrl
, 1);
971 static int fdctrl_media_changed(FDrive
*drv
)
977 ret
= bdrv_media_changed(drv
->bs
);
984 /* Digital input register : 0x07 (read-only) */
985 static uint32_t fdctrl_read_dir(FDCtrl
*fdctrl
)
989 if (fdctrl_media_changed(drv0(fdctrl
))
990 || fdctrl_media_changed(drv1(fdctrl
))
992 || fdctrl_media_changed(drv2(fdctrl
))
993 || fdctrl_media_changed(drv3(fdctrl
))
996 retval
|= FD_DIR_DSKCHG
;
998 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval
);
1004 /* FIFO state control */
1005 static void fdctrl_reset_fifo(FDCtrl
*fdctrl
)
1007 fdctrl
->data_dir
= FD_DIR_WRITE
;
1008 fdctrl
->data_pos
= 0;
1009 fdctrl
->msr
&= ~(FD_MSR_CMDBUSY
| FD_MSR_DIO
);
1012 /* Set FIFO status for the host to read */
1013 static void fdctrl_set_fifo(FDCtrl
*fdctrl
, int fifo_len
, int do_irq
)
1015 fdctrl
->data_dir
= FD_DIR_READ
;
1016 fdctrl
->data_len
= fifo_len
;
1017 fdctrl
->data_pos
= 0;
1018 fdctrl
->msr
|= FD_MSR_CMDBUSY
| FD_MSR_RQM
| FD_MSR_DIO
;
1020 fdctrl_raise_irq(fdctrl
, 0x00);
1023 /* Set an error: unimplemented/unknown command */
1024 static void fdctrl_unimplemented(FDCtrl
*fdctrl
, int direction
)
1026 FLOPPY_ERROR("unimplemented command 0x%02x\n", fdctrl
->fifo
[0]);
1027 fdctrl
->fifo
[0] = FD_SR0_INVCMD
;
1028 fdctrl_set_fifo(fdctrl
, 1, 0);
1031 /* Seek to next sector */
1032 static int fdctrl_seek_to_next_sect(FDCtrl
*fdctrl
, FDrive
*cur_drv
)
1034 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
1035 cur_drv
->head
, cur_drv
->track
, cur_drv
->sect
,
1036 fd_sector(cur_drv
));
1037 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1039 if (cur_drv
->sect
>= cur_drv
->last_sect
||
1040 cur_drv
->sect
== fdctrl
->eot
) {
1042 if (FD_MULTI_TRACK(fdctrl
->data_state
)) {
1043 if (cur_drv
->head
== 0 &&
1044 (cur_drv
->flags
& FDISK_DBL_SIDES
) != 0) {
1049 if ((cur_drv
->flags
& FDISK_DBL_SIDES
) == 0)
1056 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1057 cur_drv
->head
, cur_drv
->track
,
1058 cur_drv
->sect
, fd_sector(cur_drv
));
1065 /* Callback for transfer end (stop or abort) */
1066 static void fdctrl_stop_transfer(FDCtrl
*fdctrl
, uint8_t status0
,
1067 uint8_t status1
, uint8_t status2
)
1071 cur_drv
= get_cur_drv(fdctrl
);
1072 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1073 status0
, status1
, status2
,
1074 status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
));
1075 fdctrl
->fifo
[0] = status0
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1076 fdctrl
->fifo
[1] = status1
;
1077 fdctrl
->fifo
[2] = status2
;
1078 fdctrl
->fifo
[3] = cur_drv
->track
;
1079 fdctrl
->fifo
[4] = cur_drv
->head
;
1080 fdctrl
->fifo
[5] = cur_drv
->sect
;
1081 fdctrl
->fifo
[6] = FD_SECTOR_SC
;
1082 fdctrl
->data_dir
= FD_DIR_READ
;
1083 if (!(fdctrl
->msr
& FD_MSR_NONDMA
)) {
1084 DMA_release_DREQ(fdctrl
->dma_chann
);
1086 fdctrl
->msr
|= FD_MSR_RQM
| FD_MSR_DIO
;
1087 fdctrl
->msr
&= ~FD_MSR_NONDMA
;
1088 fdctrl_set_fifo(fdctrl
, 7, 1);
1091 /* Prepare a data transfer (either DMA or FIFO) */
1092 static void fdctrl_start_transfer(FDCtrl
*fdctrl
, int direction
)
1098 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1099 cur_drv
= get_cur_drv(fdctrl
);
1100 kt
= fdctrl
->fifo
[2];
1101 kh
= fdctrl
->fifo
[3];
1102 ks
= fdctrl
->fifo
[4];
1103 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1104 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1105 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
));
1106 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1109 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1110 fdctrl
->fifo
[3] = kt
;
1111 fdctrl
->fifo
[4] = kh
;
1112 fdctrl
->fifo
[5] = ks
;
1116 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1117 fdctrl
->fifo
[3] = kt
;
1118 fdctrl
->fifo
[4] = kh
;
1119 fdctrl
->fifo
[5] = ks
;
1122 /* No seek enabled */
1123 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1124 fdctrl
->fifo
[3] = kt
;
1125 fdctrl
->fifo
[4] = kh
;
1126 fdctrl
->fifo
[5] = ks
;
1135 /* Set the FIFO state */
1136 fdctrl
->data_dir
= direction
;
1137 fdctrl
->data_pos
= 0;
1138 fdctrl
->msr
|= FD_MSR_CMDBUSY
;
1139 if (fdctrl
->fifo
[0] & 0x80)
1140 fdctrl
->data_state
|= FD_STATE_MULTI
;
1142 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1144 fdctrl
->data_state
|= FD_STATE_SEEK
;
1146 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1147 if (fdctrl
->fifo
[5] == 00) {
1148 fdctrl
->data_len
= fdctrl
->fifo
[8];
1151 fdctrl
->data_len
= 128 << (fdctrl
->fifo
[5] > 7 ? 7 : fdctrl
->fifo
[5]);
1152 tmp
= (fdctrl
->fifo
[6] - ks
+ 1);
1153 if (fdctrl
->fifo
[0] & 0x80)
1154 tmp
+= fdctrl
->fifo
[6];
1155 fdctrl
->data_len
*= tmp
;
1157 fdctrl
->eot
= fdctrl
->fifo
[6];
1158 if (fdctrl
->dor
& FD_DOR_DMAEN
) {
1160 /* DMA transfer are enabled. Check if DMA channel is well programmed */
1161 dma_mode
= DMA_get_channel_mode(fdctrl
->dma_chann
);
1162 dma_mode
= (dma_mode
>> 2) & 3;
1163 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1164 dma_mode
, direction
,
1165 (128 << fdctrl
->fifo
[5]) *
1166 (cur_drv
->last_sect
- ks
+ 1), fdctrl
->data_len
);
1167 if (((direction
== FD_DIR_SCANE
|| direction
== FD_DIR_SCANL
||
1168 direction
== FD_DIR_SCANH
) && dma_mode
== 0) ||
1169 (direction
== FD_DIR_WRITE
&& dma_mode
== 2) ||
1170 (direction
== FD_DIR_READ
&& dma_mode
== 1)) {
1171 /* No access is allowed until DMA transfer has completed */
1172 fdctrl
->msr
&= ~FD_MSR_RQM
;
1173 /* Now, we just have to wait for the DMA controller to
1176 DMA_hold_DREQ(fdctrl
->dma_chann
);
1177 DMA_schedule(fdctrl
->dma_chann
);
1180 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode
, direction
);
1183 FLOPPY_DPRINTF("start non-DMA transfer\n");
1184 fdctrl
->msr
|= FD_MSR_NONDMA
;
1185 if (direction
!= FD_DIR_WRITE
)
1186 fdctrl
->msr
|= FD_MSR_DIO
;
1187 /* IO based transfer: calculate len */
1188 fdctrl_raise_irq(fdctrl
, 0x00);
1193 /* Prepare a transfer of deleted data */
1194 static void fdctrl_start_transfer_del(FDCtrl
*fdctrl
, int direction
)
1196 FLOPPY_ERROR("fdctrl_start_transfer_del() unimplemented\n");
1198 /* We don't handle deleted data,
1199 * so we don't return *ANYTHING*
1201 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1204 /* handlers for DMA transfers */
1205 static int fdctrl_transfer_handler (void *opaque
, int nchan
,
1206 int dma_pos
, int dma_len
)
1210 int len
, start_pos
, rel_pos
;
1211 uint8_t status0
= 0x00, status1
= 0x00, status2
= 0x00;
1214 if (fdctrl
->msr
& FD_MSR_RQM
) {
1215 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1218 cur_drv
= get_cur_drv(fdctrl
);
1219 if (fdctrl
->data_dir
== FD_DIR_SCANE
|| fdctrl
->data_dir
== FD_DIR_SCANL
||
1220 fdctrl
->data_dir
== FD_DIR_SCANH
)
1221 status2
= FD_SR2_SNS
;
1222 if (dma_len
> fdctrl
->data_len
)
1223 dma_len
= fdctrl
->data_len
;
1224 if (cur_drv
->bs
== NULL
) {
1225 if (fdctrl
->data_dir
== FD_DIR_WRITE
)
1226 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1228 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1230 goto transfer_error
;
1232 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1233 for (start_pos
= fdctrl
->data_pos
; fdctrl
->data_pos
< dma_len
;) {
1234 len
= dma_len
- fdctrl
->data_pos
;
1235 if (len
+ rel_pos
> FD_SECTOR_LEN
)
1236 len
= FD_SECTOR_LEN
- rel_pos
;
1237 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1238 "(%d-0x%08x 0x%08x)\n", len
, dma_len
, fdctrl
->data_pos
,
1239 fdctrl
->data_len
, GET_CUR_DRV(fdctrl
), cur_drv
->head
,
1240 cur_drv
->track
, cur_drv
->sect
, fd_sector(cur_drv
),
1241 fd_sector(cur_drv
) * FD_SECTOR_LEN
);
1242 if (fdctrl
->data_dir
!= FD_DIR_WRITE
||
1243 len
< FD_SECTOR_LEN
|| rel_pos
!= 0) {
1244 /* READ & SCAN commands and realign to a sector for WRITE */
1245 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
),
1246 fdctrl
->fifo
, 1) < 0) {
1247 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1248 fd_sector(cur_drv
));
1249 /* Sure, image size is too small... */
1250 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1253 switch (fdctrl
->data_dir
) {
1256 DMA_write_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1257 fdctrl
->data_pos
, len
);
1260 /* WRITE commands */
1261 DMA_read_memory (nchan
, fdctrl
->fifo
+ rel_pos
,
1262 fdctrl
->data_pos
, len
);
1263 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
),
1264 fdctrl
->fifo
, 1) < 0) {
1265 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv
));
1266 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1267 goto transfer_error
;
1273 uint8_t tmpbuf
[FD_SECTOR_LEN
];
1275 DMA_read_memory (nchan
, tmpbuf
, fdctrl
->data_pos
, len
);
1276 ret
= memcmp(tmpbuf
, fdctrl
->fifo
+ rel_pos
, len
);
1278 status2
= FD_SR2_SEH
;
1281 if ((ret
< 0 && fdctrl
->data_dir
== FD_DIR_SCANL
) ||
1282 (ret
> 0 && fdctrl
->data_dir
== FD_DIR_SCANH
)) {
1289 fdctrl
->data_pos
+= len
;
1290 rel_pos
= fdctrl
->data_pos
% FD_SECTOR_LEN
;
1292 /* Seek to next sector */
1293 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
))
1298 len
= fdctrl
->data_pos
- start_pos
;
1299 FLOPPY_DPRINTF("end transfer %d %d %d\n",
1300 fdctrl
->data_pos
, len
, fdctrl
->data_len
);
1301 if (fdctrl
->data_dir
== FD_DIR_SCANE
||
1302 fdctrl
->data_dir
== FD_DIR_SCANL
||
1303 fdctrl
->data_dir
== FD_DIR_SCANH
)
1304 status2
= FD_SR2_SEH
;
1305 if (FD_DID_SEEK(fdctrl
->data_state
))
1306 status0
|= FD_SR0_SEEK
;
1307 fdctrl
->data_len
-= len
;
1308 fdctrl_stop_transfer(fdctrl
, status0
, status1
, status2
);
1314 /* Data register : 0x05 */
1315 static uint32_t fdctrl_read_data(FDCtrl
*fdctrl
)
1318 uint32_t retval
= 0;
1321 cur_drv
= get_cur_drv(fdctrl
);
1322 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1323 if (!(fdctrl
->msr
& FD_MSR_RQM
) || !(fdctrl
->msr
& FD_MSR_DIO
)) {
1324 FLOPPY_ERROR("controller not ready for reading\n");
1327 pos
= fdctrl
->data_pos
;
1328 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1329 pos
%= FD_SECTOR_LEN
;
1331 if (fdctrl
->data_pos
!= 0)
1332 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1333 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1334 fd_sector(cur_drv
));
1337 if (bdrv_read(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1338 FLOPPY_DPRINTF("error getting sector %d\n",
1339 fd_sector(cur_drv
));
1340 /* Sure, image size is too small... */
1341 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1345 retval
= fdctrl
->fifo
[pos
];
1346 if (++fdctrl
->data_pos
== fdctrl
->data_len
) {
1347 fdctrl
->data_pos
= 0;
1348 /* Switch from transfer mode to status mode
1349 * then from status mode to command mode
1351 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1352 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1354 fdctrl_reset_fifo(fdctrl
);
1355 fdctrl_reset_irq(fdctrl
);
1358 FLOPPY_DPRINTF("data register: 0x%02x\n", retval
);
1363 static void fdctrl_format_sector(FDCtrl
*fdctrl
)
1368 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1369 cur_drv
= get_cur_drv(fdctrl
);
1370 kt
= fdctrl
->fifo
[6];
1371 kh
= fdctrl
->fifo
[7];
1372 ks
= fdctrl
->fifo
[8];
1373 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1374 GET_CUR_DRV(fdctrl
), kh
, kt
, ks
,
1375 fd_sector_calc(kh
, kt
, ks
, cur_drv
->last_sect
));
1376 switch (fd_seek(cur_drv
, kh
, kt
, ks
, fdctrl
->config
& FD_CONFIG_EIS
)) {
1379 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1380 fdctrl
->fifo
[3] = kt
;
1381 fdctrl
->fifo
[4] = kh
;
1382 fdctrl
->fifo
[5] = ks
;
1386 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, FD_SR1_EC
, 0x00);
1387 fdctrl
->fifo
[3] = kt
;
1388 fdctrl
->fifo
[4] = kh
;
1389 fdctrl
->fifo
[5] = ks
;
1392 /* No seek enabled */
1393 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
, 0x00, 0x00);
1394 fdctrl
->fifo
[3] = kt
;
1395 fdctrl
->fifo
[4] = kh
;
1396 fdctrl
->fifo
[5] = ks
;
1399 fdctrl
->data_state
|= FD_STATE_SEEK
;
1404 memset(fdctrl
->fifo
, 0, FD_SECTOR_LEN
);
1405 if (cur_drv
->bs
== NULL
||
1406 bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1407 FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv
));
1408 fdctrl_stop_transfer(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
, 0x00, 0x00);
1410 if (cur_drv
->sect
== cur_drv
->last_sect
) {
1411 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1412 /* Last sector done */
1413 if (FD_DID_SEEK(fdctrl
->data_state
))
1414 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1416 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1419 fdctrl
->data_pos
= 0;
1420 fdctrl
->data_len
= 4;
1425 static void fdctrl_handle_lock(FDCtrl
*fdctrl
, int direction
)
1427 fdctrl
->lock
= (fdctrl
->fifo
[0] & 0x80) ? 1 : 0;
1428 fdctrl
->fifo
[0] = fdctrl
->lock
<< 4;
1429 fdctrl_set_fifo(fdctrl
, 1, fdctrl
->lock
);
1432 static void fdctrl_handle_dumpreg(FDCtrl
*fdctrl
, int direction
)
1434 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1436 /* Drives position */
1437 fdctrl
->fifo
[0] = drv0(fdctrl
)->track
;
1438 fdctrl
->fifo
[1] = drv1(fdctrl
)->track
;
1440 fdctrl
->fifo
[2] = drv2(fdctrl
)->track
;
1441 fdctrl
->fifo
[3] = drv3(fdctrl
)->track
;
1443 fdctrl
->fifo
[2] = 0;
1444 fdctrl
->fifo
[3] = 0;
1447 fdctrl
->fifo
[4] = fdctrl
->timer0
;
1448 fdctrl
->fifo
[5] = (fdctrl
->timer1
<< 1) | (fdctrl
->dor
& FD_DOR_DMAEN
? 1 : 0);
1449 fdctrl
->fifo
[6] = cur_drv
->last_sect
;
1450 fdctrl
->fifo
[7] = (fdctrl
->lock
<< 7) |
1451 (cur_drv
->perpendicular
<< 2);
1452 fdctrl
->fifo
[8] = fdctrl
->config
;
1453 fdctrl
->fifo
[9] = fdctrl
->precomp_trk
;
1454 fdctrl_set_fifo(fdctrl
, 10, 0);
1457 static void fdctrl_handle_version(FDCtrl
*fdctrl
, int direction
)
1459 /* Controller's version */
1460 fdctrl
->fifo
[0] = fdctrl
->version
;
1461 fdctrl_set_fifo(fdctrl
, 1, 1);
1464 static void fdctrl_handle_partid(FDCtrl
*fdctrl
, int direction
)
1466 fdctrl
->fifo
[0] = 0x41; /* Stepping 1 */
1467 fdctrl_set_fifo(fdctrl
, 1, 0);
1470 static void fdctrl_handle_restore(FDCtrl
*fdctrl
, int direction
)
1472 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1474 /* Drives position */
1475 drv0(fdctrl
)->track
= fdctrl
->fifo
[3];
1476 drv1(fdctrl
)->track
= fdctrl
->fifo
[4];
1478 drv2(fdctrl
)->track
= fdctrl
->fifo
[5];
1479 drv3(fdctrl
)->track
= fdctrl
->fifo
[6];
1482 fdctrl
->timer0
= fdctrl
->fifo
[7];
1483 fdctrl
->timer1
= fdctrl
->fifo
[8];
1484 cur_drv
->last_sect
= fdctrl
->fifo
[9];
1485 fdctrl
->lock
= fdctrl
->fifo
[10] >> 7;
1486 cur_drv
->perpendicular
= (fdctrl
->fifo
[10] >> 2) & 0xF;
1487 fdctrl
->config
= fdctrl
->fifo
[11];
1488 fdctrl
->precomp_trk
= fdctrl
->fifo
[12];
1489 fdctrl
->pwrd
= fdctrl
->fifo
[13];
1490 fdctrl_reset_fifo(fdctrl
);
1493 static void fdctrl_handle_save(FDCtrl
*fdctrl
, int direction
)
1495 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1497 fdctrl
->fifo
[0] = 0;
1498 fdctrl
->fifo
[1] = 0;
1499 /* Drives position */
1500 fdctrl
->fifo
[2] = drv0(fdctrl
)->track
;
1501 fdctrl
->fifo
[3] = drv1(fdctrl
)->track
;
1503 fdctrl
->fifo
[4] = drv2(fdctrl
)->track
;
1504 fdctrl
->fifo
[5] = drv3(fdctrl
)->track
;
1506 fdctrl
->fifo
[4] = 0;
1507 fdctrl
->fifo
[5] = 0;
1510 fdctrl
->fifo
[6] = fdctrl
->timer0
;
1511 fdctrl
->fifo
[7] = fdctrl
->timer1
;
1512 fdctrl
->fifo
[8] = cur_drv
->last_sect
;
1513 fdctrl
->fifo
[9] = (fdctrl
->lock
<< 7) |
1514 (cur_drv
->perpendicular
<< 2);
1515 fdctrl
->fifo
[10] = fdctrl
->config
;
1516 fdctrl
->fifo
[11] = fdctrl
->precomp_trk
;
1517 fdctrl
->fifo
[12] = fdctrl
->pwrd
;
1518 fdctrl
->fifo
[13] = 0;
1519 fdctrl
->fifo
[14] = 0;
1520 fdctrl_set_fifo(fdctrl
, 15, 1);
1523 static void fdctrl_handle_readid(FDCtrl
*fdctrl
, int direction
)
1525 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1527 /* XXX: should set main status register to busy */
1528 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1529 qemu_mod_timer(fdctrl
->result_timer
,
1530 qemu_get_clock(vm_clock
) + (get_ticks_per_sec() / 50));
1533 static void fdctrl_handle_format_track(FDCtrl
*fdctrl
, int direction
)
1537 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1538 cur_drv
= get_cur_drv(fdctrl
);
1539 fdctrl
->data_state
|= FD_STATE_FORMAT
;
1540 if (fdctrl
->fifo
[0] & 0x80)
1541 fdctrl
->data_state
|= FD_STATE_MULTI
;
1543 fdctrl
->data_state
&= ~FD_STATE_MULTI
;
1544 fdctrl
->data_state
&= ~FD_STATE_SEEK
;
1546 fdctrl
->fifo
[2] > 7 ? 16384 : 128 << fdctrl
->fifo
[2];
1548 cur_drv
->last_sect
=
1549 cur_drv
->flags
& FDISK_DBL_SIDES
? fdctrl
->fifo
[3] :
1550 fdctrl
->fifo
[3] / 2;
1552 cur_drv
->last_sect
= fdctrl
->fifo
[3];
1554 /* TODO: implement format using DMA expected by the Bochs BIOS
1555 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1556 * the sector with the specified fill byte
1558 fdctrl
->data_state
&= ~FD_STATE_FORMAT
;
1559 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1562 static void fdctrl_handle_specify(FDCtrl
*fdctrl
, int direction
)
1564 fdctrl
->timer0
= (fdctrl
->fifo
[1] >> 4) & 0xF;
1565 fdctrl
->timer1
= fdctrl
->fifo
[2] >> 1;
1566 if (fdctrl
->fifo
[2] & 1)
1567 fdctrl
->dor
&= ~FD_DOR_DMAEN
;
1569 fdctrl
->dor
|= FD_DOR_DMAEN
;
1570 /* No result back */
1571 fdctrl_reset_fifo(fdctrl
);
1574 static void fdctrl_handle_sense_drive_status(FDCtrl
*fdctrl
, int direction
)
1578 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1579 cur_drv
= get_cur_drv(fdctrl
);
1580 cur_drv
->head
= (fdctrl
->fifo
[1] >> 2) & 1;
1581 /* 1 Byte status back */
1582 fdctrl
->fifo
[0] = (cur_drv
->ro
<< 6) |
1583 (cur_drv
->track
== 0 ? 0x10 : 0x00) |
1584 (cur_drv
->head
<< 2) |
1585 GET_CUR_DRV(fdctrl
) |
1587 fdctrl_set_fifo(fdctrl
, 1, 0);
1590 static void fdctrl_handle_recalibrate(FDCtrl
*fdctrl
, int direction
)
1594 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1595 cur_drv
= get_cur_drv(fdctrl
);
1596 fd_recalibrate(cur_drv
);
1597 fdctrl_reset_fifo(fdctrl
);
1598 /* Raise Interrupt */
1599 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1602 static void fdctrl_handle_sense_interrupt_status(FDCtrl
*fdctrl
, int direction
)
1604 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1606 if(fdctrl
->reset_sensei
> 0) {
1608 FD_SR0_RDYCHG
+ FD_RESET_SENSEI_COUNT
- fdctrl
->reset_sensei
;
1609 fdctrl
->reset_sensei
--;
1611 /* XXX: status0 handling is broken for read/write
1612 commands, so we do this hack. It should be suppressed
1615 FD_SR0_SEEK
| (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1618 fdctrl
->fifo
[1] = cur_drv
->track
;
1619 fdctrl_set_fifo(fdctrl
, 2, 0);
1620 fdctrl_reset_irq(fdctrl
);
1621 fdctrl
->status0
= FD_SR0_RDYCHG
;
1624 static void fdctrl_handle_seek(FDCtrl
*fdctrl
, int direction
)
1628 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1629 cur_drv
= get_cur_drv(fdctrl
);
1630 fdctrl_reset_fifo(fdctrl
);
1631 if (fdctrl
->fifo
[2] > cur_drv
->max_track
) {
1632 fdctrl_raise_irq(fdctrl
, FD_SR0_ABNTERM
| FD_SR0_SEEK
);
1634 cur_drv
->track
= fdctrl
->fifo
[2];
1635 /* Raise Interrupt */
1636 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1640 static void fdctrl_handle_perpendicular_mode(FDCtrl
*fdctrl
, int direction
)
1642 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1644 if (fdctrl
->fifo
[1] & 0x80)
1645 cur_drv
->perpendicular
= fdctrl
->fifo
[1] & 0x7;
1646 /* No result back */
1647 fdctrl_reset_fifo(fdctrl
);
1650 static void fdctrl_handle_configure(FDCtrl
*fdctrl
, int direction
)
1652 fdctrl
->config
= fdctrl
->fifo
[2];
1653 fdctrl
->precomp_trk
= fdctrl
->fifo
[3];
1654 /* No result back */
1655 fdctrl_reset_fifo(fdctrl
);
1658 static void fdctrl_handle_powerdown_mode(FDCtrl
*fdctrl
, int direction
)
1660 fdctrl
->pwrd
= fdctrl
->fifo
[1];
1661 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1662 fdctrl_set_fifo(fdctrl
, 1, 1);
1665 static void fdctrl_handle_option(FDCtrl
*fdctrl
, int direction
)
1667 /* No result back */
1668 fdctrl_reset_fifo(fdctrl
);
1671 static void fdctrl_handle_drive_specification_command(FDCtrl
*fdctrl
, int direction
)
1673 FDrive
*cur_drv
= get_cur_drv(fdctrl
);
1675 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x80) {
1676 /* Command parameters done */
1677 if (fdctrl
->fifo
[fdctrl
->data_pos
- 1] & 0x40) {
1678 fdctrl
->fifo
[0] = fdctrl
->fifo
[1];
1679 fdctrl
->fifo
[2] = 0;
1680 fdctrl
->fifo
[3] = 0;
1681 fdctrl_set_fifo(fdctrl
, 4, 1);
1683 fdctrl_reset_fifo(fdctrl
);
1685 } else if (fdctrl
->data_len
> 7) {
1687 fdctrl
->fifo
[0] = 0x80 |
1688 (cur_drv
->head
<< 2) | GET_CUR_DRV(fdctrl
);
1689 fdctrl_set_fifo(fdctrl
, 1, 1);
1693 static void fdctrl_handle_relative_seek_out(FDCtrl
*fdctrl
, int direction
)
1697 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1698 cur_drv
= get_cur_drv(fdctrl
);
1699 if (fdctrl
->fifo
[2] + cur_drv
->track
>= cur_drv
->max_track
) {
1700 cur_drv
->track
= cur_drv
->max_track
- 1;
1702 cur_drv
->track
+= fdctrl
->fifo
[2];
1704 fdctrl_reset_fifo(fdctrl
);
1705 /* Raise Interrupt */
1706 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1709 static void fdctrl_handle_relative_seek_in(FDCtrl
*fdctrl
, int direction
)
1713 SET_CUR_DRV(fdctrl
, fdctrl
->fifo
[1] & FD_DOR_SELMASK
);
1714 cur_drv
= get_cur_drv(fdctrl
);
1715 if (fdctrl
->fifo
[2] > cur_drv
->track
) {
1718 cur_drv
->track
-= fdctrl
->fifo
[2];
1720 fdctrl_reset_fifo(fdctrl
);
1721 /* Raise Interrupt */
1722 fdctrl_raise_irq(fdctrl
, FD_SR0_SEEK
);
1725 static const struct {
1730 void (*handler
)(FDCtrl
*fdctrl
, int direction
);
1733 { FD_CMD_READ
, 0x1f, "READ", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1734 { FD_CMD_WRITE
, 0x3f, "WRITE", 8, fdctrl_start_transfer
, FD_DIR_WRITE
},
1735 { FD_CMD_SEEK
, 0xff, "SEEK", 2, fdctrl_handle_seek
},
1736 { FD_CMD_SENSE_INTERRUPT_STATUS
, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status
},
1737 { FD_CMD_RECALIBRATE
, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate
},
1738 { FD_CMD_FORMAT_TRACK
, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track
},
1739 { FD_CMD_READ_TRACK
, 0xbf, "READ TRACK", 8, fdctrl_start_transfer
, FD_DIR_READ
},
1740 { FD_CMD_RESTORE
, 0xff, "RESTORE", 17, fdctrl_handle_restore
}, /* part of READ DELETED DATA */
1741 { FD_CMD_SAVE
, 0xff, "SAVE", 0, fdctrl_handle_save
}, /* part of READ DELETED DATA */
1742 { FD_CMD_READ_DELETED
, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_READ
},
1743 { FD_CMD_SCAN_EQUAL
, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANE
},
1744 { FD_CMD_VERIFY
, 0x1f, "VERIFY", 8, fdctrl_unimplemented
},
1745 { FD_CMD_SCAN_LOW_OR_EQUAL
, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANL
},
1746 { FD_CMD_SCAN_HIGH_OR_EQUAL
, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer
, FD_DIR_SCANH
},
1747 { FD_CMD_WRITE_DELETED
, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del
, FD_DIR_WRITE
},
1748 { FD_CMD_READ_ID
, 0xbf, "READ ID", 1, fdctrl_handle_readid
},
1749 { FD_CMD_SPECIFY
, 0xff, "SPECIFY", 2, fdctrl_handle_specify
},
1750 { FD_CMD_SENSE_DRIVE_STATUS
, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status
},
1751 { FD_CMD_PERPENDICULAR_MODE
, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode
},
1752 { FD_CMD_CONFIGURE
, 0xff, "CONFIGURE", 3, fdctrl_handle_configure
},
1753 { FD_CMD_POWERDOWN_MODE
, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode
},
1754 { FD_CMD_OPTION
, 0xff, "OPTION", 1, fdctrl_handle_option
},
1755 { FD_CMD_DRIVE_SPECIFICATION_COMMAND
, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command
},
1756 { FD_CMD_RELATIVE_SEEK_OUT
, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out
},
1757 { FD_CMD_FORMAT_AND_WRITE
, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented
},
1758 { FD_CMD_RELATIVE_SEEK_IN
, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in
},
1759 { FD_CMD_LOCK
, 0x7f, "LOCK", 0, fdctrl_handle_lock
},
1760 { FD_CMD_DUMPREG
, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg
},
1761 { FD_CMD_VERSION
, 0xff, "VERSION", 0, fdctrl_handle_version
},
1762 { FD_CMD_PART_ID
, 0xff, "PART ID", 0, fdctrl_handle_partid
},
1763 { FD_CMD_WRITE
, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer
, FD_DIR_WRITE
}, /* not in specification ; BeOS 4.5 bug */
1764 { 0, 0, "unknown", 0, fdctrl_unimplemented
}, /* default handler */
1766 /* Associate command to an index in the 'handlers' array */
1767 static uint8_t command_to_handler
[256];
1769 static void fdctrl_write_data(FDCtrl
*fdctrl
, uint32_t value
)
1775 if (!(fdctrl
->dor
& FD_DOR_nRESET
)) {
1776 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1779 if (!(fdctrl
->msr
& FD_MSR_RQM
) || (fdctrl
->msr
& FD_MSR_DIO
)) {
1780 FLOPPY_ERROR("controller not ready for writing\n");
1783 fdctrl
->dsr
&= ~FD_DSR_PWRDOWN
;
1784 /* Is it write command time ? */
1785 if (fdctrl
->msr
& FD_MSR_NONDMA
) {
1786 /* FIFO data write */
1787 pos
= fdctrl
->data_pos
++;
1788 pos
%= FD_SECTOR_LEN
;
1789 fdctrl
->fifo
[pos
] = value
;
1790 if (pos
== FD_SECTOR_LEN
- 1 ||
1791 fdctrl
->data_pos
== fdctrl
->data_len
) {
1792 cur_drv
= get_cur_drv(fdctrl
);
1793 if (bdrv_write(cur_drv
->bs
, fd_sector(cur_drv
), fdctrl
->fifo
, 1) < 0) {
1794 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv
));
1797 if (!fdctrl_seek_to_next_sect(fdctrl
, cur_drv
)) {
1798 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1799 fd_sector(cur_drv
));
1803 /* Switch from transfer mode to status mode
1804 * then from status mode to command mode
1806 if (fdctrl
->data_pos
== fdctrl
->data_len
)
1807 fdctrl_stop_transfer(fdctrl
, FD_SR0_SEEK
, 0x00, 0x00);
1810 if (fdctrl
->data_pos
== 0) {
1812 pos
= command_to_handler
[value
& 0xff];
1813 FLOPPY_DPRINTF("%s command\n", handlers
[pos
].name
);
1814 fdctrl
->data_len
= handlers
[pos
].parameters
+ 1;
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 fdctrl_stop_transfer(fdctrl
, 0x00, 0x00, 0x00);
1849 /* Init functions */
1850 static void fdctrl_connect_drives(FDCtrl
*fdctrl
)
1854 for (i
= 0; i
< MAX_FD
; i
++) {
1855 fd_init(&fdctrl
->drives
[i
]);
1856 fd_revalidate(&fdctrl
->drives
[i
]);
1860 FDCtrl
*fdctrl_init_isa(DriveInfo
**fds
)
1864 dev
= isa_create("isa-fdc");
1866 qdev_prop_set_drive(&dev
->qdev
, "driveA", fds
[0]);
1869 qdev_prop_set_drive(&dev
->qdev
, "driveB", fds
[1]);
1871 if (qdev_init(&dev
->qdev
) < 0)
1873 return &(DO_UPCAST(FDCtrlISABus
, busdev
, dev
)->state
);
1876 FDCtrl
*fdctrl_init_sysbus(qemu_irq irq
, int dma_chann
,
1877 target_phys_addr_t mmio_base
, DriveInfo
**fds
)
1883 dev
= qdev_create(NULL
, "sysbus-fdc");
1884 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1885 fdctrl
= &sys
->state
;
1886 fdctrl
->dma_chann
= dma_chann
; /* FIXME */
1888 qdev_prop_set_drive(dev
, "driveA", fds
[0]);
1891 qdev_prop_set_drive(dev
, "driveB", fds
[1]);
1893 qdev_init_nofail(dev
);
1894 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1895 sysbus_mmio_map(&sys
->busdev
, 0, mmio_base
);
1900 FDCtrl
*sun4m_fdctrl_init(qemu_irq irq
, target_phys_addr_t io_base
,
1901 DriveInfo
**fds
, qemu_irq
*fdc_tc
)
1907 dev
= qdev_create(NULL
, "SUNW,fdtwo");
1909 qdev_prop_set_drive(dev
, "drive", fds
[0]);
1911 qdev_init_nofail(dev
);
1912 sys
= DO_UPCAST(FDCtrlSysBus
, busdev
.qdev
, dev
);
1913 fdctrl
= &sys
->state
;
1914 sysbus_connect_irq(&sys
->busdev
, 0, irq
);
1915 sysbus_mmio_map(&sys
->busdev
, 0, io_base
);
1916 *fdc_tc
= qdev_get_gpio_in(dev
, 0);
1921 static int fdctrl_init_common(FDCtrl
*fdctrl
, target_phys_addr_t io_base
)
1924 static int command_tables_inited
= 0;
1926 /* Fill 'command_to_handler' lookup table */
1927 if (!command_tables_inited
) {
1928 command_tables_inited
= 1;
1929 for (i
= ARRAY_SIZE(handlers
) - 1; i
>= 0; i
--) {
1930 for (j
= 0; j
< sizeof(command_to_handler
); j
++) {
1931 if ((j
& handlers
[i
].mask
) == handlers
[i
].value
) {
1932 command_to_handler
[j
] = i
;
1938 FLOPPY_DPRINTF("init controller\n");
1939 fdctrl
->fifo
= qemu_memalign(512, FD_SECTOR_LEN
);
1940 fdctrl
->fifo_size
= 512;
1941 fdctrl
->result_timer
= qemu_new_timer(vm_clock
,
1942 fdctrl_result_timer
, fdctrl
);
1944 fdctrl
->version
= 0x90; /* Intel 82078 controller */
1945 fdctrl
->config
= FD_CONFIG_EIS
| FD_CONFIG_EFIFO
; /* Implicit seek, polling & FIFO enabled */
1946 fdctrl
->num_floppies
= MAX_FD
;
1948 if (fdctrl
->dma_chann
!= -1)
1949 DMA_register_channel(fdctrl
->dma_chann
, &fdctrl_transfer_handler
, fdctrl
);
1950 fdctrl_connect_drives(fdctrl
);
1952 vmstate_register(io_base
, &vmstate_fdc
, fdctrl
);
1956 static int isabus_fdc_init1(ISADevice
*dev
)
1958 FDCtrlISABus
*isa
= DO_UPCAST(FDCtrlISABus
, busdev
, dev
);
1959 FDCtrl
*fdctrl
= &isa
->state
;
1965 register_ioport_read(iobase
+ 0x01, 5, 1,
1966 &fdctrl_read_port
, fdctrl
);
1967 register_ioport_read(iobase
+ 0x07, 1, 1,
1968 &fdctrl_read_port
, fdctrl
);
1969 register_ioport_write(iobase
+ 0x01, 5, 1,
1970 &fdctrl_write_port
, fdctrl
);
1971 register_ioport_write(iobase
+ 0x07, 1, 1,
1972 &fdctrl_write_port
, fdctrl
);
1973 isa_init_irq(&isa
->busdev
, &fdctrl
->irq
, isairq
);
1974 fdctrl
->dma_chann
= dma_chann
;
1976 ret
= fdctrl_init_common(fdctrl
, iobase
);
1981 static int sysbus_fdc_init1(SysBusDevice
*dev
)
1983 FDCtrlSysBus
*sys
= DO_UPCAST(FDCtrlSysBus
, busdev
, dev
);
1984 FDCtrl
*fdctrl
= &sys
->state
;
1988 io
= cpu_register_io_memory(fdctrl_mem_read
, fdctrl_mem_write
, fdctrl
);
1989 sysbus_init_mmio(dev
, 0x08, io
);
1990 sysbus_init_irq(dev
, &fdctrl
->irq
);
1991 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
1992 fdctrl
->dma_chann
= -1;
1994 ret
= fdctrl_init_common(fdctrl
, io
);
1999 static int sun4m_fdc_init1(SysBusDevice
*dev
)
2001 FDCtrl
*fdctrl
= &(FROM_SYSBUS(FDCtrlSysBus
, dev
)->state
);
2004 io
= cpu_register_io_memory(fdctrl_mem_read_strict
,
2005 fdctrl_mem_write_strict
, fdctrl
);
2006 sysbus_init_mmio(dev
, 0x08, io
);
2007 sysbus_init_irq(dev
, &fdctrl
->irq
);
2008 qdev_init_gpio_in(&dev
->qdev
, fdctrl_handle_tc
, 1);
2011 return fdctrl_init_common(fdctrl
, io
);
2014 static ISADeviceInfo isa_fdc_info
= {
2015 .init
= isabus_fdc_init1
,
2016 .qdev
.name
= "isa-fdc",
2017 .qdev
.size
= sizeof(FDCtrlISABus
),
2019 .qdev
.reset
= fdctrl_external_reset_isa
,
2020 .qdev
.props
= (Property
[]) {
2021 DEFINE_PROP_DRIVE("driveA", FDCtrlISABus
, state
.drives
[0].dinfo
),
2022 DEFINE_PROP_DRIVE("driveB", FDCtrlISABus
, state
.drives
[1].dinfo
),
2023 DEFINE_PROP_END_OF_LIST(),
2027 static SysBusDeviceInfo sysbus_fdc_info
= {
2028 .init
= sysbus_fdc_init1
,
2029 .qdev
.name
= "sysbus-fdc",
2030 .qdev
.size
= sizeof(FDCtrlSysBus
),
2031 .qdev
.reset
= fdctrl_external_reset_sysbus
,
2032 .qdev
.props
= (Property
[]) {
2033 DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus
, state
.drives
[0].dinfo
),
2034 DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus
, state
.drives
[1].dinfo
),
2035 DEFINE_PROP_END_OF_LIST(),
2039 static SysBusDeviceInfo sun4m_fdc_info
= {
2040 .init
= sun4m_fdc_init1
,
2041 .qdev
.name
= "SUNW,fdtwo",
2042 .qdev
.size
= sizeof(FDCtrlSysBus
),
2043 .qdev
.reset
= fdctrl_external_reset_sysbus
,
2044 .qdev
.props
= (Property
[]) {
2045 DEFINE_PROP_DRIVE("drive", FDCtrlSysBus
, state
.drives
[0].dinfo
),
2046 DEFINE_PROP_END_OF_LIST(),
2050 static void fdc_register_devices(void)
2052 isa_qdev_register(&isa_fdc_info
);
2053 sysbus_register_withprop(&sysbus_fdc_info
);
2054 sysbus_register_withprop(&sun4m_fdc_info
);
2057 device_init(fdc_register_devices
)