]> git.proxmox.com Git - mirror_qemu.git/blame - hw/fdc.c
isa: always use provided ISA bus when creating an isa device
[mirror_qemu.git] / hw / fdc.c
CommitLineData
8977f3c1 1/*
890fa6be 2 * QEMU Floppy disk emulator (Intel 82078)
5fafdf24 3 *
3ccacc4a 4 * Copyright (c) 2003, 2007 Jocelyn Mayer
bcc4e41f 5 * Copyright (c) 2008 Hervé Poussineau
5fafdf24 6 *
8977f3c1
FB
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:
13 *
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
16 *
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
23 * THE SOFTWARE.
24 */
e80cfcfc
FB
25/*
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.
28 */
f64ab228 29
87ecb68b
PB
30#include "hw.h"
31#include "fdc.h"
b47b3525 32#include "qemu-error.h"
87ecb68b
PB
33#include "qemu-timer.h"
34#include "isa.h"
f64ab228 35#include "sysbus.h"
e8133762 36#include "qdev-addr.h"
2446333c 37#include "blockdev.h"
1ca4d09a 38#include "sysemu.h"
8977f3c1
FB
39
40/********************************************************/
41/* debug Floppy devices */
42//#define DEBUG_FLOPPY
43
44#ifdef DEBUG_FLOPPY
001faf32
BS
45#define FLOPPY_DPRINTF(fmt, ...) \
46 do { printf("FLOPPY: " fmt , ## __VA_ARGS__); } while (0)
8977f3c1 47#else
001faf32 48#define FLOPPY_DPRINTF(fmt, ...)
8977f3c1
FB
49#endif
50
001faf32
BS
51#define FLOPPY_ERROR(fmt, ...) \
52 do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0)
8977f3c1
FB
53
54/********************************************************/
55/* Floppy drive emulation */
56
cefec4f5
BS
57#define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
58#define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))
59
8977f3c1 60/* Will always be a fixed parameter for us */
f2d81b33
BS
61#define FD_SECTOR_LEN 512
62#define FD_SECTOR_SC 2 /* Sector size code */
63#define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */
8977f3c1
FB
64
65/* Floppy disk drive emulation */
5c02c033 66typedef enum FDiskFlags {
baca51fa 67 FDISK_DBL_SIDES = 0x01,
5c02c033 68} FDiskFlags;
baca51fa 69
5c02c033 70typedef struct FDrive {
8977f3c1
FB
71 BlockDriverState *bs;
72 /* Drive status */
5c02c033 73 FDriveType drive;
8977f3c1 74 uint8_t perpendicular; /* 2.88 MB access mode */
8977f3c1
FB
75 /* Position */
76 uint8_t head;
77 uint8_t track;
78 uint8_t sect;
8977f3c1 79 /* Media */
5c02c033 80 FDiskFlags flags;
8977f3c1
FB
81 uint8_t last_sect; /* Nb sector per track */
82 uint8_t max_track; /* Nb of tracks */
baca51fa 83 uint16_t bps; /* Bytes per sector */
8977f3c1 84 uint8_t ro; /* Is read-only */
7d905f71 85 uint8_t media_changed; /* Is media changed */
5c02c033 86} FDrive;
8977f3c1 87
5c02c033 88static void fd_init(FDrive *drv)
8977f3c1
FB
89{
90 /* Drive */
b939777c 91 drv->drive = FDRIVE_DRV_NONE;
8977f3c1 92 drv->perpendicular = 0;
8977f3c1 93 /* Disk */
baca51fa 94 drv->last_sect = 0;
8977f3c1
FB
95 drv->max_track = 0;
96}
97
7859cb98
BS
98static int fd_sector_calc(uint8_t head, uint8_t track, uint8_t sect,
99 uint8_t last_sect)
8977f3c1
FB
100{
101 return (((track * 2) + head) * last_sect) + sect - 1;
102}
103
104/* Returns current position, in sectors, for given drive */
5c02c033 105static int fd_sector(FDrive *drv)
8977f3c1 106{
7859cb98 107 return fd_sector_calc(drv->head, drv->track, drv->sect, drv->last_sect);
8977f3c1
FB
108}
109
77370520
BS
110/* Seek to a new position:
111 * returns 0 if already on right track
112 * returns 1 if track changed
113 * returns 2 if track is invalid
114 * returns 3 if sector is invalid
115 * returns 4 if seek is disabled
116 */
5c02c033
BS
117static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect,
118 int enable_seek)
8977f3c1
FB
119{
120 uint32_t sector;
baca51fa
FB
121 int ret;
122
123 if (track > drv->max_track ||
4f431960 124 (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) {
ed5fd2cc
FB
125 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
126 head, track, sect, 1,
127 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
128 drv->max_track, drv->last_sect);
8977f3c1
FB
129 return 2;
130 }
131 if (sect > drv->last_sect) {
ed5fd2cc
FB
132 FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
133 head, track, sect, 1,
134 (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
135 drv->max_track, drv->last_sect);
8977f3c1
FB
136 return 3;
137 }
7859cb98 138 sector = fd_sector_calc(head, track, sect, drv->last_sect);
baca51fa 139 ret = 0;
8977f3c1
FB
140 if (sector != fd_sector(drv)) {
141#if 0
142 if (!enable_seek) {
143 FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
144 head, track, sect, 1, drv->max_track, drv->last_sect);
145 return 4;
146 }
147#endif
148 drv->head = head;
4f431960
JM
149 if (drv->track != track)
150 ret = 1;
8977f3c1
FB
151 drv->track = track;
152 drv->sect = sect;
8977f3c1
FB
153 }
154
baca51fa 155 return ret;
8977f3c1
FB
156}
157
158/* Set drive back to track 0 */
5c02c033 159static void fd_recalibrate(FDrive *drv)
8977f3c1
FB
160{
161 FLOPPY_DPRINTF("recalibrate\n");
162 drv->head = 0;
163 drv->track = 0;
164 drv->sect = 1;
8977f3c1
FB
165}
166
167/* Revalidate a disk drive after a disk change */
5c02c033 168static void fd_revalidate(FDrive *drv)
8977f3c1 169{
baca51fa 170 int nb_heads, max_track, last_sect, ro;
5bbdbb46 171 FDriveType drive;
8977f3c1
FB
172
173 FLOPPY_DPRINTF("revalidate\n");
a541f297 174 if (drv->bs != NULL && bdrv_is_inserted(drv->bs)) {
4f431960 175 ro = bdrv_is_read_only(drv->bs);
5bbdbb46
BS
176 bdrv_get_floppy_geometry_hint(drv->bs, &nb_heads, &max_track,
177 &last_sect, drv->drive, &drive);
4f431960
JM
178 if (nb_heads != 0 && max_track != 0 && last_sect != 0) {
179 FLOPPY_DPRINTF("User defined disk (%d %d %d)",
ed5fd2cc 180 nb_heads - 1, max_track, last_sect);
4f431960 181 } else {
5bbdbb46
BS
182 FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", nb_heads,
183 max_track, last_sect, ro ? "ro" : "rw");
4f431960
JM
184 }
185 if (nb_heads == 1) {
186 drv->flags &= ~FDISK_DBL_SIDES;
187 } else {
188 drv->flags |= FDISK_DBL_SIDES;
189 }
190 drv->max_track = max_track;
191 drv->last_sect = last_sect;
192 drv->ro = ro;
5bbdbb46 193 drv->drive = drive;
8977f3c1 194 } else {
4f431960 195 FLOPPY_DPRINTF("No disk in drive\n");
baca51fa 196 drv->last_sect = 0;
4f431960
JM
197 drv->max_track = 0;
198 drv->flags &= ~FDISK_DBL_SIDES;
8977f3c1 199 }
caed8802
FB
200}
201
8977f3c1 202/********************************************************/
4b19ec0c 203/* Intel 82078 floppy disk controller emulation */
8977f3c1 204
63ffb564
BS
205typedef struct FDCtrl FDCtrl;
206
5c02c033
BS
207static void fdctrl_reset(FDCtrl *fdctrl, int do_irq);
208static void fdctrl_reset_fifo(FDCtrl *fdctrl);
85571bc7 209static int fdctrl_transfer_handler (void *opaque, int nchan,
c227f099 210 int dma_pos, int dma_len);
5c02c033
BS
211static void fdctrl_raise_irq(FDCtrl *fdctrl, uint8_t status0);
212
213static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl);
214static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl);
215static uint32_t fdctrl_read_dor(FDCtrl *fdctrl);
216static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value);
217static uint32_t fdctrl_read_tape(FDCtrl *fdctrl);
218static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value);
219static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl);
220static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value);
221static uint32_t fdctrl_read_data(FDCtrl *fdctrl);
222static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value);
223static uint32_t fdctrl_read_dir(FDCtrl *fdctrl);
8977f3c1 224
8977f3c1
FB
225enum {
226 FD_DIR_WRITE = 0,
227 FD_DIR_READ = 1,
228 FD_DIR_SCANE = 2,
229 FD_DIR_SCANL = 3,
230 FD_DIR_SCANH = 4,
231};
232
233enum {
b9b3d225
BS
234 FD_STATE_MULTI = 0x01, /* multi track flag */
235 FD_STATE_FORMAT = 0x02, /* format flag */
236 FD_STATE_SEEK = 0x04, /* seek flag */
8977f3c1
FB
237};
238
9fea808a 239enum {
8c6a4d77
BS
240 FD_REG_SRA = 0x00,
241 FD_REG_SRB = 0x01,
9fea808a
BS
242 FD_REG_DOR = 0x02,
243 FD_REG_TDR = 0x03,
244 FD_REG_MSR = 0x04,
245 FD_REG_DSR = 0x04,
246 FD_REG_FIFO = 0x05,
247 FD_REG_DIR = 0x07,
248};
249
250enum {
65cef780 251 FD_CMD_READ_TRACK = 0x02,
9fea808a
BS
252 FD_CMD_SPECIFY = 0x03,
253 FD_CMD_SENSE_DRIVE_STATUS = 0x04,
65cef780
BS
254 FD_CMD_WRITE = 0x05,
255 FD_CMD_READ = 0x06,
9fea808a
BS
256 FD_CMD_RECALIBRATE = 0x07,
257 FD_CMD_SENSE_INTERRUPT_STATUS = 0x08,
65cef780
BS
258 FD_CMD_WRITE_DELETED = 0x09,
259 FD_CMD_READ_ID = 0x0a,
260 FD_CMD_READ_DELETED = 0x0c,
261 FD_CMD_FORMAT_TRACK = 0x0d,
9fea808a
BS
262 FD_CMD_DUMPREG = 0x0e,
263 FD_CMD_SEEK = 0x0f,
264 FD_CMD_VERSION = 0x10,
65cef780 265 FD_CMD_SCAN_EQUAL = 0x11,
9fea808a
BS
266 FD_CMD_PERPENDICULAR_MODE = 0x12,
267 FD_CMD_CONFIGURE = 0x13,
65cef780
BS
268 FD_CMD_LOCK = 0x14,
269 FD_CMD_VERIFY = 0x16,
9fea808a
BS
270 FD_CMD_POWERDOWN_MODE = 0x17,
271 FD_CMD_PART_ID = 0x18,
65cef780
BS
272 FD_CMD_SCAN_LOW_OR_EQUAL = 0x19,
273 FD_CMD_SCAN_HIGH_OR_EQUAL = 0x1d,
bb350a5e 274 FD_CMD_SAVE = 0x2e,
9fea808a 275 FD_CMD_OPTION = 0x33,
bb350a5e 276 FD_CMD_RESTORE = 0x4e,
9fea808a
BS
277 FD_CMD_DRIVE_SPECIFICATION_COMMAND = 0x8e,
278 FD_CMD_RELATIVE_SEEK_OUT = 0x8f,
9fea808a
BS
279 FD_CMD_FORMAT_AND_WRITE = 0xcd,
280 FD_CMD_RELATIVE_SEEK_IN = 0xcf,
281};
282
283enum {
284 FD_CONFIG_PRETRK = 0xff, /* Pre-compensation set to track 0 */
285 FD_CONFIG_FIFOTHR = 0x0f, /* FIFO threshold set to 1 byte */
286 FD_CONFIG_POLL = 0x10, /* Poll enabled */
287 FD_CONFIG_EFIFO = 0x20, /* FIFO disabled */
288 FD_CONFIG_EIS = 0x40, /* No implied seeks */
289};
290
291enum {
292 FD_SR0_EQPMT = 0x10,
293 FD_SR0_SEEK = 0x20,
294 FD_SR0_ABNTERM = 0x40,
295 FD_SR0_INVCMD = 0x80,
296 FD_SR0_RDYCHG = 0xc0,
297};
298
77370520
BS
299enum {
300 FD_SR1_EC = 0x80, /* End of cylinder */
301};
302
303enum {
304 FD_SR2_SNS = 0x04, /* Scan not satisfied */
305 FD_SR2_SEH = 0x08, /* Scan equal hit */
306};
307
8c6a4d77
BS
308enum {
309 FD_SRA_DIR = 0x01,
310 FD_SRA_nWP = 0x02,
311 FD_SRA_nINDX = 0x04,
312 FD_SRA_HDSEL = 0x08,
313 FD_SRA_nTRK0 = 0x10,
314 FD_SRA_STEP = 0x20,
315 FD_SRA_nDRV2 = 0x40,
316 FD_SRA_INTPEND = 0x80,
317};
318
319enum {
320 FD_SRB_MTR0 = 0x01,
321 FD_SRB_MTR1 = 0x02,
322 FD_SRB_WGATE = 0x04,
323 FD_SRB_RDATA = 0x08,
324 FD_SRB_WDATA = 0x10,
325 FD_SRB_DR0 = 0x20,
326};
327
9fea808a 328enum {
78ae820c
BS
329#if MAX_FD == 4
330 FD_DOR_SELMASK = 0x03,
331#else
9fea808a 332 FD_DOR_SELMASK = 0x01,
78ae820c 333#endif
9fea808a
BS
334 FD_DOR_nRESET = 0x04,
335 FD_DOR_DMAEN = 0x08,
336 FD_DOR_MOTEN0 = 0x10,
337 FD_DOR_MOTEN1 = 0x20,
338 FD_DOR_MOTEN2 = 0x40,
339 FD_DOR_MOTEN3 = 0x80,
340};
341
342enum {
78ae820c 343#if MAX_FD == 4
9fea808a 344 FD_TDR_BOOTSEL = 0x0c,
78ae820c
BS
345#else
346 FD_TDR_BOOTSEL = 0x04,
347#endif
9fea808a
BS
348};
349
350enum {
351 FD_DSR_DRATEMASK= 0x03,
352 FD_DSR_PWRDOWN = 0x40,
353 FD_DSR_SWRESET = 0x80,
354};
355
356enum {
357 FD_MSR_DRV0BUSY = 0x01,
358 FD_MSR_DRV1BUSY = 0x02,
359 FD_MSR_DRV2BUSY = 0x04,
360 FD_MSR_DRV3BUSY = 0x08,
361 FD_MSR_CMDBUSY = 0x10,
362 FD_MSR_NONDMA = 0x20,
363 FD_MSR_DIO = 0x40,
364 FD_MSR_RQM = 0x80,
365};
366
367enum {
368 FD_DIR_DSKCHG = 0x80,
369};
370
8977f3c1
FB
371#define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
372#define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
baca51fa 373#define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
8977f3c1 374
5c02c033 375struct FDCtrl {
dc6c1b37 376 MemoryRegion iomem;
d537cf6c 377 qemu_irq irq;
4b19ec0c 378 /* Controller state */
ed5fd2cc 379 QEMUTimer *result_timer;
242cca4f
BS
380 int dma_chann;
381 /* Controller's identification */
382 uint8_t version;
383 /* HW */
8c6a4d77
BS
384 uint8_t sra;
385 uint8_t srb;
368df94d 386 uint8_t dor;
d7a6c270 387 uint8_t dor_vmstate; /* only used as temp during vmstate */
46d3233b 388 uint8_t tdr;
b9b3d225 389 uint8_t dsr;
368df94d 390 uint8_t msr;
8977f3c1 391 uint8_t cur_drv;
77370520
BS
392 uint8_t status0;
393 uint8_t status1;
394 uint8_t status2;
8977f3c1 395 /* Command FIFO */
33f00271 396 uint8_t *fifo;
d7a6c270 397 int32_t fifo_size;
8977f3c1
FB
398 uint32_t data_pos;
399 uint32_t data_len;
400 uint8_t data_state;
401 uint8_t data_dir;
890fa6be 402 uint8_t eot; /* last wanted sector */
8977f3c1 403 /* States kept only to be returned back */
8977f3c1
FB
404 /* precompensation */
405 uint8_t precomp_trk;
406 uint8_t config;
407 uint8_t lock;
408 /* Power down config (also with status regB access mode */
409 uint8_t pwrd;
410 /* Floppy drives */
d7a6c270 411 uint8_t num_floppies;
242cca4f
BS
412 /* Sun4m quirks? */
413 int sun4m;
5c02c033 414 FDrive drives[MAX_FD];
f2d81b33 415 int reset_sensei;
242cca4f
BS
416 /* Timers state */
417 uint8_t timer0;
418 uint8_t timer1;
baca51fa
FB
419};
420
5c02c033 421typedef struct FDCtrlSysBus {
8baf73ad 422 SysBusDevice busdev;
5c02c033
BS
423 struct FDCtrl state;
424} FDCtrlSysBus;
8baf73ad 425
5c02c033 426typedef struct FDCtrlISABus {
8baf73ad 427 ISADevice busdev;
5c02c033 428 struct FDCtrl state;
1ca4d09a
GN
429 int32_t bootindexA;
430 int32_t bootindexB;
5c02c033 431} FDCtrlISABus;
8baf73ad 432
baca51fa
FB
433static uint32_t fdctrl_read (void *opaque, uint32_t reg)
434{
5c02c033 435 FDCtrl *fdctrl = opaque;
baca51fa
FB
436 uint32_t retval;
437
a18e67f5 438 reg &= 7;
e64d7d59 439 switch (reg) {
8c6a4d77
BS
440 case FD_REG_SRA:
441 retval = fdctrl_read_statusA(fdctrl);
4f431960 442 break;
8c6a4d77 443 case FD_REG_SRB:
4f431960
JM
444 retval = fdctrl_read_statusB(fdctrl);
445 break;
9fea808a 446 case FD_REG_DOR:
4f431960
JM
447 retval = fdctrl_read_dor(fdctrl);
448 break;
9fea808a 449 case FD_REG_TDR:
baca51fa 450 retval = fdctrl_read_tape(fdctrl);
4f431960 451 break;
9fea808a 452 case FD_REG_MSR:
baca51fa 453 retval = fdctrl_read_main_status(fdctrl);
4f431960 454 break;
9fea808a 455 case FD_REG_FIFO:
baca51fa 456 retval = fdctrl_read_data(fdctrl);
4f431960 457 break;
9fea808a 458 case FD_REG_DIR:
baca51fa 459 retval = fdctrl_read_dir(fdctrl);
4f431960 460 break;
a541f297 461 default:
4f431960
JM
462 retval = (uint32_t)(-1);
463 break;
a541f297 464 }
ed5fd2cc 465 FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval);
baca51fa
FB
466
467 return retval;
468}
469
470static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value)
471{
5c02c033 472 FDCtrl *fdctrl = opaque;
baca51fa 473
ed5fd2cc
FB
474 FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg & 7, value);
475
a18e67f5 476 reg &= 7;
e64d7d59 477 switch (reg) {
9fea808a 478 case FD_REG_DOR:
4f431960
JM
479 fdctrl_write_dor(fdctrl, value);
480 break;
9fea808a 481 case FD_REG_TDR:
baca51fa 482 fdctrl_write_tape(fdctrl, value);
4f431960 483 break;
9fea808a 484 case FD_REG_DSR:
baca51fa 485 fdctrl_write_rate(fdctrl, value);
4f431960 486 break;
9fea808a 487 case FD_REG_FIFO:
baca51fa 488 fdctrl_write_data(fdctrl, value);
4f431960 489 break;
a541f297 490 default:
4f431960 491 break;
a541f297 492 }
baca51fa
FB
493}
494
dc6c1b37
AK
495static uint64_t fdctrl_read_mem (void *opaque, target_phys_addr_t reg,
496 unsigned ize)
62a46c61 497{
5dcb6b91 498 return fdctrl_read(opaque, (uint32_t)reg);
62a46c61
FB
499}
500
dc6c1b37
AK
501static void fdctrl_write_mem (void *opaque, target_phys_addr_t reg,
502 uint64_t value, unsigned size)
62a46c61 503{
5dcb6b91 504 fdctrl_write(opaque, (uint32_t)reg, value);
62a46c61
FB
505}
506
dc6c1b37
AK
507static const MemoryRegionOps fdctrl_mem_ops = {
508 .read = fdctrl_read_mem,
509 .write = fdctrl_write_mem,
510 .endianness = DEVICE_NATIVE_ENDIAN,
e80cfcfc
FB
511};
512
dc6c1b37
AK
513static const MemoryRegionOps fdctrl_mem_strict_ops = {
514 .read = fdctrl_read_mem,
515 .write = fdctrl_write_mem,
516 .endianness = DEVICE_NATIVE_ENDIAN,
517 .valid = {
518 .min_access_size = 1,
519 .max_access_size = 1,
520 },
7c560456
BS
521};
522
7d905f71
JW
523static bool fdrive_media_changed_needed(void *opaque)
524{
525 FDrive *drive = opaque;
526
8e49ca46 527 return (drive->bs != NULL && drive->media_changed != 1);
7d905f71
JW
528}
529
530static const VMStateDescription vmstate_fdrive_media_changed = {
531 .name = "fdrive/media_changed",
532 .version_id = 1,
533 .minimum_version_id = 1,
534 .minimum_version_id_old = 1,
7d905f71
JW
535 .fields = (VMStateField[]) {
536 VMSTATE_UINT8(media_changed, FDrive),
537 VMSTATE_END_OF_LIST()
538 }
539};
540
d7a6c270
JQ
541static const VMStateDescription vmstate_fdrive = {
542 .name = "fdrive",
543 .version_id = 1,
544 .minimum_version_id = 1,
545 .minimum_version_id_old = 1,
7d905f71 546 .fields = (VMStateField[]) {
5c02c033
BS
547 VMSTATE_UINT8(head, FDrive),
548 VMSTATE_UINT8(track, FDrive),
549 VMSTATE_UINT8(sect, FDrive),
d7a6c270 550 VMSTATE_END_OF_LIST()
7d905f71
JW
551 },
552 .subsections = (VMStateSubsection[]) {
553 {
554 .vmsd = &vmstate_fdrive_media_changed,
555 .needed = &fdrive_media_changed_needed,
556 } , {
557 /* empty */
558 }
d7a6c270
JQ
559 }
560};
3ccacc4a 561
d4bfa4d7 562static void fdc_pre_save(void *opaque)
3ccacc4a 563{
5c02c033 564 FDCtrl *s = opaque;
3ccacc4a 565
d7a6c270 566 s->dor_vmstate = s->dor | GET_CUR_DRV(s);
3ccacc4a
BS
567}
568
e59fb374 569static int fdc_post_load(void *opaque, int version_id)
3ccacc4a 570{
5c02c033 571 FDCtrl *s = opaque;
3ccacc4a 572
d7a6c270
JQ
573 SET_CUR_DRV(s, s->dor_vmstate & FD_DOR_SELMASK);
574 s->dor = s->dor_vmstate & ~FD_DOR_SELMASK;
3ccacc4a
BS
575 return 0;
576}
577
d7a6c270 578static const VMStateDescription vmstate_fdc = {
aef30c3c 579 .name = "fdc",
d7a6c270
JQ
580 .version_id = 2,
581 .minimum_version_id = 2,
582 .minimum_version_id_old = 2,
583 .pre_save = fdc_pre_save,
584 .post_load = fdc_post_load,
585 .fields = (VMStateField []) {
586 /* Controller State */
5c02c033
BS
587 VMSTATE_UINT8(sra, FDCtrl),
588 VMSTATE_UINT8(srb, FDCtrl),
589 VMSTATE_UINT8(dor_vmstate, FDCtrl),
590 VMSTATE_UINT8(tdr, FDCtrl),
591 VMSTATE_UINT8(dsr, FDCtrl),
592 VMSTATE_UINT8(msr, FDCtrl),
593 VMSTATE_UINT8(status0, FDCtrl),
594 VMSTATE_UINT8(status1, FDCtrl),
595 VMSTATE_UINT8(status2, FDCtrl),
d7a6c270 596 /* Command FIFO */
8ec68b06
BS
597 VMSTATE_VARRAY_INT32(fifo, FDCtrl, fifo_size, 0, vmstate_info_uint8,
598 uint8_t),
5c02c033
BS
599 VMSTATE_UINT32(data_pos, FDCtrl),
600 VMSTATE_UINT32(data_len, FDCtrl),
601 VMSTATE_UINT8(data_state, FDCtrl),
602 VMSTATE_UINT8(data_dir, FDCtrl),
603 VMSTATE_UINT8(eot, FDCtrl),
d7a6c270 604 /* States kept only to be returned back */
5c02c033
BS
605 VMSTATE_UINT8(timer0, FDCtrl),
606 VMSTATE_UINT8(timer1, FDCtrl),
607 VMSTATE_UINT8(precomp_trk, FDCtrl),
608 VMSTATE_UINT8(config, FDCtrl),
609 VMSTATE_UINT8(lock, FDCtrl),
610 VMSTATE_UINT8(pwrd, FDCtrl),
611 VMSTATE_UINT8_EQUAL(num_floppies, FDCtrl),
612 VMSTATE_STRUCT_ARRAY(drives, FDCtrl, MAX_FD, 1,
613 vmstate_fdrive, FDrive),
d7a6c270 614 VMSTATE_END_OF_LIST()
78ae820c 615 }
d7a6c270 616};
3ccacc4a 617
2be37833 618static void fdctrl_external_reset_sysbus(DeviceState *d)
3ccacc4a 619{
5c02c033
BS
620 FDCtrlSysBus *sys = container_of(d, FDCtrlSysBus, busdev.qdev);
621 FDCtrl *s = &sys->state;
2be37833
BS
622
623 fdctrl_reset(s, 0);
624}
625
626static void fdctrl_external_reset_isa(DeviceState *d)
627{
5c02c033
BS
628 FDCtrlISABus *isa = container_of(d, FDCtrlISABus, busdev.qdev);
629 FDCtrl *s = &isa->state;
3ccacc4a
BS
630
631 fdctrl_reset(s, 0);
632}
633
2be17ebd
BS
634static void fdctrl_handle_tc(void *opaque, int irq, int level)
635{
5c02c033 636 //FDCtrl *s = opaque;
2be17ebd
BS
637
638 if (level) {
639 // XXX
640 FLOPPY_DPRINTF("TC pulsed\n");
641 }
642}
643
8977f3c1 644/* Change IRQ state */
5c02c033 645static void fdctrl_reset_irq(FDCtrl *fdctrl)
8977f3c1 646{
8c6a4d77
BS
647 if (!(fdctrl->sra & FD_SRA_INTPEND))
648 return;
ed5fd2cc 649 FLOPPY_DPRINTF("Reset interrupt\n");
d537cf6c 650 qemu_set_irq(fdctrl->irq, 0);
8c6a4d77 651 fdctrl->sra &= ~FD_SRA_INTPEND;
8977f3c1
FB
652}
653
5c02c033 654static void fdctrl_raise_irq(FDCtrl *fdctrl, uint8_t status0)
8977f3c1 655{
b9b3d225
BS
656 /* Sparc mutation */
657 if (fdctrl->sun4m && (fdctrl->msr & FD_MSR_CMDBUSY)) {
658 /* XXX: not sure */
659 fdctrl->msr &= ~FD_MSR_CMDBUSY;
660 fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO;
77370520 661 fdctrl->status0 = status0;
4f431960 662 return;
6f7e9aec 663 }
8c6a4d77 664 if (!(fdctrl->sra & FD_SRA_INTPEND)) {
d537cf6c 665 qemu_set_irq(fdctrl->irq, 1);
8c6a4d77 666 fdctrl->sra |= FD_SRA_INTPEND;
8977f3c1 667 }
f2d81b33 668 fdctrl->reset_sensei = 0;
77370520
BS
669 fdctrl->status0 = status0;
670 FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl->status0);
8977f3c1
FB
671}
672
4b19ec0c 673/* Reset controller */
5c02c033 674static void fdctrl_reset(FDCtrl *fdctrl, int do_irq)
8977f3c1
FB
675{
676 int i;
677
4b19ec0c 678 FLOPPY_DPRINTF("reset controller\n");
baca51fa 679 fdctrl_reset_irq(fdctrl);
4b19ec0c 680 /* Initialise controller */
8c6a4d77
BS
681 fdctrl->sra = 0;
682 fdctrl->srb = 0xc0;
683 if (!fdctrl->drives[1].bs)
684 fdctrl->sra |= FD_SRA_nDRV2;
baca51fa 685 fdctrl->cur_drv = 0;
1c346df2 686 fdctrl->dor = FD_DOR_nRESET;
368df94d 687 fdctrl->dor |= (fdctrl->dma_chann != -1) ? FD_DOR_DMAEN : 0;
b9b3d225 688 fdctrl->msr = FD_MSR_RQM;
8977f3c1 689 /* FIFO state */
baca51fa
FB
690 fdctrl->data_pos = 0;
691 fdctrl->data_len = 0;
b9b3d225 692 fdctrl->data_state = 0;
baca51fa 693 fdctrl->data_dir = FD_DIR_WRITE;
8977f3c1 694 for (i = 0; i < MAX_FD; i++)
1c346df2 695 fd_recalibrate(&fdctrl->drives[i]);
baca51fa 696 fdctrl_reset_fifo(fdctrl);
77370520 697 if (do_irq) {
9fea808a 698 fdctrl_raise_irq(fdctrl, FD_SR0_RDYCHG);
f2d81b33 699 fdctrl->reset_sensei = FD_RESET_SENSEI_COUNT;
77370520 700 }
baca51fa
FB
701}
702
5c02c033 703static inline FDrive *drv0(FDCtrl *fdctrl)
baca51fa 704{
46d3233b 705 return &fdctrl->drives[(fdctrl->tdr & FD_TDR_BOOTSEL) >> 2];
baca51fa
FB
706}
707
5c02c033 708static inline FDrive *drv1(FDCtrl *fdctrl)
baca51fa 709{
46d3233b
BS
710 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (1 << 2))
711 return &fdctrl->drives[1];
712 else
713 return &fdctrl->drives[0];
baca51fa
FB
714}
715
78ae820c 716#if MAX_FD == 4
5c02c033 717static inline FDrive *drv2(FDCtrl *fdctrl)
78ae820c
BS
718{
719 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (2 << 2))
720 return &fdctrl->drives[2];
721 else
722 return &fdctrl->drives[1];
723}
724
5c02c033 725static inline FDrive *drv3(FDCtrl *fdctrl)
78ae820c
BS
726{
727 if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (3 << 2))
728 return &fdctrl->drives[3];
729 else
730 return &fdctrl->drives[2];
731}
732#endif
733
5c02c033 734static FDrive *get_cur_drv(FDCtrl *fdctrl)
baca51fa 735{
78ae820c
BS
736 switch (fdctrl->cur_drv) {
737 case 0: return drv0(fdctrl);
738 case 1: return drv1(fdctrl);
739#if MAX_FD == 4
740 case 2: return drv2(fdctrl);
741 case 3: return drv3(fdctrl);
742#endif
743 default: return NULL;
744 }
8977f3c1
FB
745}
746
8c6a4d77 747/* Status A register : 0x00 (read-only) */
5c02c033 748static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl)
8c6a4d77
BS
749{
750 uint32_t retval = fdctrl->sra;
751
752 FLOPPY_DPRINTF("status register A: 0x%02x\n", retval);
753
754 return retval;
755}
756
8977f3c1 757/* Status B register : 0x01 (read-only) */
5c02c033 758static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl)
8977f3c1 759{
8c6a4d77
BS
760 uint32_t retval = fdctrl->srb;
761
762 FLOPPY_DPRINTF("status register B: 0x%02x\n", retval);
763
764 return retval;
8977f3c1
FB
765}
766
767/* Digital output register : 0x02 */
5c02c033 768static uint32_t fdctrl_read_dor(FDCtrl *fdctrl)
8977f3c1 769{
1c346df2 770 uint32_t retval = fdctrl->dor;
8977f3c1 771
8977f3c1 772 /* Selected drive */
baca51fa 773 retval |= fdctrl->cur_drv;
8977f3c1
FB
774 FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
775
776 return retval;
777}
778
5c02c033 779static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value)
8977f3c1 780{
8977f3c1 781 FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
8c6a4d77
BS
782
783 /* Motors */
784 if (value & FD_DOR_MOTEN0)
785 fdctrl->srb |= FD_SRB_MTR0;
786 else
787 fdctrl->srb &= ~FD_SRB_MTR0;
788 if (value & FD_DOR_MOTEN1)
789 fdctrl->srb |= FD_SRB_MTR1;
790 else
791 fdctrl->srb &= ~FD_SRB_MTR1;
792
793 /* Drive */
794 if (value & 1)
795 fdctrl->srb |= FD_SRB_DR0;
796 else
797 fdctrl->srb &= ~FD_SRB_DR0;
798
8977f3c1 799 /* Reset */
9fea808a 800 if (!(value & FD_DOR_nRESET)) {
1c346df2 801 if (fdctrl->dor & FD_DOR_nRESET) {
4b19ec0c 802 FLOPPY_DPRINTF("controller enter RESET state\n");
8977f3c1
FB
803 }
804 } else {
1c346df2 805 if (!(fdctrl->dor & FD_DOR_nRESET)) {
4b19ec0c 806 FLOPPY_DPRINTF("controller out of RESET state\n");
fb6cf1d0 807 fdctrl_reset(fdctrl, 1);
b9b3d225 808 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
8977f3c1
FB
809 }
810 }
811 /* Selected drive */
9fea808a 812 fdctrl->cur_drv = value & FD_DOR_SELMASK;
368df94d
BS
813
814 fdctrl->dor = value;
8977f3c1
FB
815}
816
817/* Tape drive register : 0x03 */
5c02c033 818static uint32_t fdctrl_read_tape(FDCtrl *fdctrl)
8977f3c1 819{
46d3233b 820 uint32_t retval = fdctrl->tdr;
8977f3c1 821
8977f3c1
FB
822 FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
823
824 return retval;
825}
826
5c02c033 827static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value)
8977f3c1 828{
8977f3c1 829 /* Reset mode */
1c346df2 830 if (!(fdctrl->dor & FD_DOR_nRESET)) {
4b19ec0c 831 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
8977f3c1
FB
832 return;
833 }
834 FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
835 /* Disk boot selection indicator */
46d3233b 836 fdctrl->tdr = value & FD_TDR_BOOTSEL;
8977f3c1
FB
837 /* Tape indicators: never allow */
838}
839
840/* Main status register : 0x04 (read) */
5c02c033 841static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl)
8977f3c1 842{
b9b3d225 843 uint32_t retval = fdctrl->msr;
8977f3c1 844
b9b3d225 845 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
1c346df2 846 fdctrl->dor |= FD_DOR_nRESET;
b9b3d225 847
82407d1a
AT
848 /* Sparc mutation */
849 if (fdctrl->sun4m) {
850 retval |= FD_MSR_DIO;
851 fdctrl_reset_irq(fdctrl);
852 };
853
8977f3c1
FB
854 FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
855
856 return retval;
857}
858
859/* Data select rate register : 0x04 (write) */
5c02c033 860static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value)
8977f3c1 861{
8977f3c1 862 /* Reset mode */
1c346df2 863 if (!(fdctrl->dor & FD_DOR_nRESET)) {
4f431960
JM
864 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
865 return;
866 }
8977f3c1
FB
867 FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
868 /* Reset: autoclear */
9fea808a 869 if (value & FD_DSR_SWRESET) {
1c346df2 870 fdctrl->dor &= ~FD_DOR_nRESET;
baca51fa 871 fdctrl_reset(fdctrl, 1);
1c346df2 872 fdctrl->dor |= FD_DOR_nRESET;
8977f3c1 873 }
9fea808a 874 if (value & FD_DSR_PWRDOWN) {
baca51fa 875 fdctrl_reset(fdctrl, 1);
8977f3c1 876 }
b9b3d225 877 fdctrl->dsr = value;
8977f3c1
FB
878}
879
5c02c033 880static int fdctrl_media_changed(FDrive *drv)
ea185bbd
FB
881{
882 int ret;
4f431960 883
5fafdf24 884 if (!drv->bs)
ea185bbd 885 return 0;
18d90055
MA
886 if (drv->media_changed) {
887 drv->media_changed = 0;
888 ret = 1;
889 } else {
890 ret = bdrv_media_changed(drv->bs);
891 if (ret < 0) {
892 ret = 0; /* we don't know, assume no */
893 }
8e49ca46 894 }
ea185bbd
FB
895 if (ret) {
896 fd_revalidate(drv);
897 }
898 return ret;
899}
900
8977f3c1 901/* Digital input register : 0x07 (read-only) */
5c02c033 902static uint32_t fdctrl_read_dir(FDCtrl *fdctrl)
8977f3c1 903{
8977f3c1
FB
904 uint32_t retval = 0;
905
78ae820c
BS
906 if (fdctrl_media_changed(drv0(fdctrl))
907 || fdctrl_media_changed(drv1(fdctrl))
908#if MAX_FD == 4
909 || fdctrl_media_changed(drv2(fdctrl))
910 || fdctrl_media_changed(drv3(fdctrl))
911#endif
912 )
9fea808a 913 retval |= FD_DIR_DSKCHG;
3c83eb4f 914 if (retval != 0) {
baca51fa 915 FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
3c83eb4f 916 }
8977f3c1
FB
917
918 return retval;
919}
920
921/* FIFO state control */
5c02c033 922static void fdctrl_reset_fifo(FDCtrl *fdctrl)
8977f3c1 923{
baca51fa
FB
924 fdctrl->data_dir = FD_DIR_WRITE;
925 fdctrl->data_pos = 0;
b9b3d225 926 fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO);
8977f3c1
FB
927}
928
929/* Set FIFO status for the host to read */
5c02c033 930static void fdctrl_set_fifo(FDCtrl *fdctrl, int fifo_len, int do_irq)
8977f3c1 931{
baca51fa
FB
932 fdctrl->data_dir = FD_DIR_READ;
933 fdctrl->data_len = fifo_len;
934 fdctrl->data_pos = 0;
b9b3d225 935 fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO;
8977f3c1 936 if (do_irq)
baca51fa 937 fdctrl_raise_irq(fdctrl, 0x00);
8977f3c1
FB
938}
939
940/* Set an error: unimplemented/unknown command */
5c02c033 941static void fdctrl_unimplemented(FDCtrl *fdctrl, int direction)
8977f3c1 942{
77370520 943 FLOPPY_ERROR("unimplemented command 0x%02x\n", fdctrl->fifo[0]);
9fea808a 944 fdctrl->fifo[0] = FD_SR0_INVCMD;
baca51fa 945 fdctrl_set_fifo(fdctrl, 1, 0);
8977f3c1
FB
946}
947
746d6de7 948/* Seek to next sector */
5c02c033 949static int fdctrl_seek_to_next_sect(FDCtrl *fdctrl, FDrive *cur_drv)
746d6de7
BS
950{
951 FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
952 cur_drv->head, cur_drv->track, cur_drv->sect,
953 fd_sector(cur_drv));
954 /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
955 error in fact */
956 if (cur_drv->sect >= cur_drv->last_sect ||
957 cur_drv->sect == fdctrl->eot) {
958 cur_drv->sect = 1;
959 if (FD_MULTI_TRACK(fdctrl->data_state)) {
960 if (cur_drv->head == 0 &&
961 (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
962 cur_drv->head = 1;
963 } else {
964 cur_drv->head = 0;
965 cur_drv->track++;
966 if ((cur_drv->flags & FDISK_DBL_SIDES) == 0)
967 return 0;
968 }
969 } else {
970 cur_drv->track++;
971 return 0;
972 }
973 FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
974 cur_drv->head, cur_drv->track,
975 cur_drv->sect, fd_sector(cur_drv));
976 } else {
977 cur_drv->sect++;
978 }
979 return 1;
980}
981
8977f3c1 982/* Callback for transfer end (stop or abort) */
5c02c033
BS
983static void fdctrl_stop_transfer(FDCtrl *fdctrl, uint8_t status0,
984 uint8_t status1, uint8_t status2)
8977f3c1 985{
5c02c033 986 FDrive *cur_drv;
8977f3c1 987
baca51fa 988 cur_drv = get_cur_drv(fdctrl);
8977f3c1
FB
989 FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
990 status0, status1, status2,
cefec4f5
BS
991 status0 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl));
992 fdctrl->fifo[0] = status0 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
baca51fa
FB
993 fdctrl->fifo[1] = status1;
994 fdctrl->fifo[2] = status2;
995 fdctrl->fifo[3] = cur_drv->track;
996 fdctrl->fifo[4] = cur_drv->head;
997 fdctrl->fifo[5] = cur_drv->sect;
998 fdctrl->fifo[6] = FD_SECTOR_SC;
999 fdctrl->data_dir = FD_DIR_READ;
368df94d 1000 if (!(fdctrl->msr & FD_MSR_NONDMA)) {
baca51fa 1001 DMA_release_DREQ(fdctrl->dma_chann);
ed5fd2cc 1002 }
b9b3d225 1003 fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO;
368df94d 1004 fdctrl->msr &= ~FD_MSR_NONDMA;
baca51fa 1005 fdctrl_set_fifo(fdctrl, 7, 1);
8977f3c1
FB
1006}
1007
1008/* Prepare a data transfer (either DMA or FIFO) */
5c02c033 1009static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction)
8977f3c1 1010{
5c02c033 1011 FDrive *cur_drv;
8977f3c1 1012 uint8_t kh, kt, ks;
77370520 1013 int did_seek = 0;
8977f3c1 1014
cefec4f5 1015 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
baca51fa
FB
1016 cur_drv = get_cur_drv(fdctrl);
1017 kt = fdctrl->fifo[2];
1018 kh = fdctrl->fifo[3];
1019 ks = fdctrl->fifo[4];
4b19ec0c 1020 FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
cefec4f5 1021 GET_CUR_DRV(fdctrl), kh, kt, ks,
7859cb98 1022 fd_sector_calc(kh, kt, ks, cur_drv->last_sect));
77370520 1023 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
8977f3c1
FB
1024 case 2:
1025 /* sect too big */
9fea808a 1026 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
baca51fa
FB
1027 fdctrl->fifo[3] = kt;
1028 fdctrl->fifo[4] = kh;
1029 fdctrl->fifo[5] = ks;
8977f3c1
FB
1030 return;
1031 case 3:
1032 /* track too big */
77370520 1033 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
baca51fa
FB
1034 fdctrl->fifo[3] = kt;
1035 fdctrl->fifo[4] = kh;
1036 fdctrl->fifo[5] = ks;
8977f3c1
FB
1037 return;
1038 case 4:
1039 /* No seek enabled */
9fea808a 1040 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
baca51fa
FB
1041 fdctrl->fifo[3] = kt;
1042 fdctrl->fifo[4] = kh;
1043 fdctrl->fifo[5] = ks;
8977f3c1
FB
1044 return;
1045 case 1:
1046 did_seek = 1;
1047 break;
1048 default:
1049 break;
1050 }
b9b3d225 1051
8977f3c1 1052 /* Set the FIFO state */
baca51fa
FB
1053 fdctrl->data_dir = direction;
1054 fdctrl->data_pos = 0;
b9b3d225 1055 fdctrl->msr |= FD_MSR_CMDBUSY;
baca51fa
FB
1056 if (fdctrl->fifo[0] & 0x80)
1057 fdctrl->data_state |= FD_STATE_MULTI;
1058 else
1059 fdctrl->data_state &= ~FD_STATE_MULTI;
8977f3c1 1060 if (did_seek)
baca51fa
FB
1061 fdctrl->data_state |= FD_STATE_SEEK;
1062 else
1063 fdctrl->data_state &= ~FD_STATE_SEEK;
1064 if (fdctrl->fifo[5] == 00) {
1065 fdctrl->data_len = fdctrl->fifo[8];
1066 } else {
4f431960 1067 int tmp;
3bcb80f1 1068 fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]);
771effeb 1069 tmp = (fdctrl->fifo[6] - ks + 1);
baca51fa 1070 if (fdctrl->fifo[0] & 0x80)
771effeb 1071 tmp += fdctrl->fifo[6];
4f431960 1072 fdctrl->data_len *= tmp;
baca51fa 1073 }
890fa6be 1074 fdctrl->eot = fdctrl->fifo[6];
368df94d 1075 if (fdctrl->dor & FD_DOR_DMAEN) {
8977f3c1
FB
1076 int dma_mode;
1077 /* DMA transfer are enabled. Check if DMA channel is well programmed */
baca51fa 1078 dma_mode = DMA_get_channel_mode(fdctrl->dma_chann);
8977f3c1 1079 dma_mode = (dma_mode >> 2) & 3;
baca51fa 1080 FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
4f431960 1081 dma_mode, direction,
baca51fa 1082 (128 << fdctrl->fifo[5]) *
4f431960 1083 (cur_drv->last_sect - ks + 1), fdctrl->data_len);
8977f3c1
FB
1084 if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
1085 direction == FD_DIR_SCANH) && dma_mode == 0) ||
1086 (direction == FD_DIR_WRITE && dma_mode == 2) ||
1087 (direction == FD_DIR_READ && dma_mode == 1)) {
1088 /* No access is allowed until DMA transfer has completed */
b9b3d225 1089 fdctrl->msr &= ~FD_MSR_RQM;
4b19ec0c 1090 /* Now, we just have to wait for the DMA controller to
8977f3c1
FB
1091 * recall us...
1092 */
baca51fa
FB
1093 DMA_hold_DREQ(fdctrl->dma_chann);
1094 DMA_schedule(fdctrl->dma_chann);
8977f3c1 1095 return;
baca51fa 1096 } else {
4f431960 1097 FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode, direction);
8977f3c1
FB
1098 }
1099 }
1100 FLOPPY_DPRINTF("start non-DMA transfer\n");
368df94d 1101 fdctrl->msr |= FD_MSR_NONDMA;
b9b3d225
BS
1102 if (direction != FD_DIR_WRITE)
1103 fdctrl->msr |= FD_MSR_DIO;
8977f3c1 1104 /* IO based transfer: calculate len */
baca51fa 1105 fdctrl_raise_irq(fdctrl, 0x00);
8977f3c1
FB
1106
1107 return;
1108}
1109
1110/* Prepare a transfer of deleted data */
5c02c033 1111static void fdctrl_start_transfer_del(FDCtrl *fdctrl, int direction)
8977f3c1 1112{
77370520
BS
1113 FLOPPY_ERROR("fdctrl_start_transfer_del() unimplemented\n");
1114
8977f3c1
FB
1115 /* We don't handle deleted data,
1116 * so we don't return *ANYTHING*
1117 */
9fea808a 1118 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
8977f3c1
FB
1119}
1120
1121/* handlers for DMA transfers */
85571bc7
FB
1122static int fdctrl_transfer_handler (void *opaque, int nchan,
1123 int dma_pos, int dma_len)
8977f3c1 1124{
5c02c033
BS
1125 FDCtrl *fdctrl;
1126 FDrive *cur_drv;
baca51fa 1127 int len, start_pos, rel_pos;
8977f3c1
FB
1128 uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1129
baca51fa 1130 fdctrl = opaque;
b9b3d225 1131 if (fdctrl->msr & FD_MSR_RQM) {
8977f3c1
FB
1132 FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1133 return 0;
1134 }
baca51fa
FB
1135 cur_drv = get_cur_drv(fdctrl);
1136 if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
1137 fdctrl->data_dir == FD_DIR_SCANH)
77370520 1138 status2 = FD_SR2_SNS;
85571bc7
FB
1139 if (dma_len > fdctrl->data_len)
1140 dma_len = fdctrl->data_len;
890fa6be 1141 if (cur_drv->bs == NULL) {
4f431960 1142 if (fdctrl->data_dir == FD_DIR_WRITE)
9fea808a 1143 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
4f431960 1144 else
9fea808a 1145 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
4f431960 1146 len = 0;
890fa6be
FB
1147 goto transfer_error;
1148 }
baca51fa 1149 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
85571bc7
FB
1150 for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) {
1151 len = dma_len - fdctrl->data_pos;
baca51fa
FB
1152 if (len + rel_pos > FD_SECTOR_LEN)
1153 len = FD_SECTOR_LEN - rel_pos;
6f7e9aec
FB
1154 FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1155 "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos,
cefec4f5 1156 fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head,
baca51fa 1157 cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
9fea808a 1158 fd_sector(cur_drv) * FD_SECTOR_LEN);
baca51fa 1159 if (fdctrl->data_dir != FD_DIR_WRITE ||
4f431960 1160 len < FD_SECTOR_LEN || rel_pos != 0) {
baca51fa
FB
1161 /* READ & SCAN commands and realign to a sector for WRITE */
1162 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv),
4f431960 1163 fdctrl->fifo, 1) < 0) {
8977f3c1
FB
1164 FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1165 fd_sector(cur_drv));
1166 /* Sure, image size is too small... */
baca51fa 1167 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
8977f3c1 1168 }
890fa6be 1169 }
4f431960
JM
1170 switch (fdctrl->data_dir) {
1171 case FD_DIR_READ:
1172 /* READ commands */
85571bc7
FB
1173 DMA_write_memory (nchan, fdctrl->fifo + rel_pos,
1174 fdctrl->data_pos, len);
4f431960
JM
1175 break;
1176 case FD_DIR_WRITE:
baca51fa 1177 /* WRITE commands */
85571bc7
FB
1178 DMA_read_memory (nchan, fdctrl->fifo + rel_pos,
1179 fdctrl->data_pos, len);
baca51fa 1180 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv),
4f431960 1181 fdctrl->fifo, 1) < 0) {
77370520 1182 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv));
9fea808a 1183 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
baca51fa 1184 goto transfer_error;
890fa6be 1185 }
4f431960
JM
1186 break;
1187 default:
1188 /* SCAN commands */
baca51fa 1189 {
4f431960 1190 uint8_t tmpbuf[FD_SECTOR_LEN];
baca51fa 1191 int ret;
85571bc7 1192 DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len);
baca51fa 1193 ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
8977f3c1 1194 if (ret == 0) {
77370520 1195 status2 = FD_SR2_SEH;
8977f3c1
FB
1196 goto end_transfer;
1197 }
baca51fa
FB
1198 if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
1199 (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
8977f3c1
FB
1200 status2 = 0x00;
1201 goto end_transfer;
1202 }
1203 }
4f431960 1204 break;
8977f3c1 1205 }
4f431960
JM
1206 fdctrl->data_pos += len;
1207 rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
baca51fa 1208 if (rel_pos == 0) {
8977f3c1 1209 /* Seek to next sector */
746d6de7
BS
1210 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv))
1211 break;
8977f3c1
FB
1212 }
1213 }
4f431960 1214 end_transfer:
baca51fa
FB
1215 len = fdctrl->data_pos - start_pos;
1216 FLOPPY_DPRINTF("end transfer %d %d %d\n",
4f431960 1217 fdctrl->data_pos, len, fdctrl->data_len);
baca51fa
FB
1218 if (fdctrl->data_dir == FD_DIR_SCANE ||
1219 fdctrl->data_dir == FD_DIR_SCANL ||
1220 fdctrl->data_dir == FD_DIR_SCANH)
77370520 1221 status2 = FD_SR2_SEH;
baca51fa 1222 if (FD_DID_SEEK(fdctrl->data_state))
9fea808a 1223 status0 |= FD_SR0_SEEK;
baca51fa 1224 fdctrl->data_len -= len;
890fa6be 1225 fdctrl_stop_transfer(fdctrl, status0, status1, status2);
4f431960 1226 transfer_error:
8977f3c1 1227
baca51fa 1228 return len;
8977f3c1
FB
1229}
1230
8977f3c1 1231/* Data register : 0x05 */
5c02c033 1232static uint32_t fdctrl_read_data(FDCtrl *fdctrl)
8977f3c1 1233{
5c02c033 1234 FDrive *cur_drv;
8977f3c1 1235 uint32_t retval = 0;
746d6de7 1236 int pos;
8977f3c1 1237
baca51fa 1238 cur_drv = get_cur_drv(fdctrl);
b9b3d225
BS
1239 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
1240 if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) {
1241 FLOPPY_ERROR("controller not ready for reading\n");
8977f3c1
FB
1242 return 0;
1243 }
baca51fa 1244 pos = fdctrl->data_pos;
368df94d 1245 if (fdctrl->msr & FD_MSR_NONDMA) {
8977f3c1
FB
1246 pos %= FD_SECTOR_LEN;
1247 if (pos == 0) {
746d6de7
BS
1248 if (fdctrl->data_pos != 0)
1249 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
1250 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1251 fd_sector(cur_drv));
1252 return 0;
1253 }
77370520
BS
1254 if (bdrv_read(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1255 FLOPPY_DPRINTF("error getting sector %d\n",
1256 fd_sector(cur_drv));
1257 /* Sure, image size is too small... */
1258 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1259 }
8977f3c1
FB
1260 }
1261 }
baca51fa
FB
1262 retval = fdctrl->fifo[pos];
1263 if (++fdctrl->data_pos == fdctrl->data_len) {
1264 fdctrl->data_pos = 0;
890fa6be 1265 /* Switch from transfer mode to status mode
8977f3c1
FB
1266 * then from status mode to command mode
1267 */
368df94d 1268 if (fdctrl->msr & FD_MSR_NONDMA) {
9fea808a 1269 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
ed5fd2cc 1270 } else {
baca51fa 1271 fdctrl_reset_fifo(fdctrl);
ed5fd2cc
FB
1272 fdctrl_reset_irq(fdctrl);
1273 }
8977f3c1
FB
1274 }
1275 FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
1276
1277 return retval;
1278}
1279
5c02c033 1280static void fdctrl_format_sector(FDCtrl *fdctrl)
8977f3c1 1281{
5c02c033 1282 FDrive *cur_drv;
baca51fa 1283 uint8_t kh, kt, ks;
8977f3c1 1284
cefec4f5 1285 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
baca51fa
FB
1286 cur_drv = get_cur_drv(fdctrl);
1287 kt = fdctrl->fifo[6];
1288 kh = fdctrl->fifo[7];
1289 ks = fdctrl->fifo[8];
1290 FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
cefec4f5 1291 GET_CUR_DRV(fdctrl), kh, kt, ks,
7859cb98 1292 fd_sector_calc(kh, kt, ks, cur_drv->last_sect));
9fea808a 1293 switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
baca51fa
FB
1294 case 2:
1295 /* sect too big */
9fea808a 1296 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
baca51fa
FB
1297 fdctrl->fifo[3] = kt;
1298 fdctrl->fifo[4] = kh;
1299 fdctrl->fifo[5] = ks;
1300 return;
1301 case 3:
1302 /* track too big */
77370520 1303 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
baca51fa
FB
1304 fdctrl->fifo[3] = kt;
1305 fdctrl->fifo[4] = kh;
1306 fdctrl->fifo[5] = ks;
1307 return;
1308 case 4:
1309 /* No seek enabled */
9fea808a 1310 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
baca51fa
FB
1311 fdctrl->fifo[3] = kt;
1312 fdctrl->fifo[4] = kh;
1313 fdctrl->fifo[5] = ks;
1314 return;
1315 case 1:
baca51fa
FB
1316 fdctrl->data_state |= FD_STATE_SEEK;
1317 break;
1318 default:
1319 break;
1320 }
1321 memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1322 if (cur_drv->bs == NULL ||
1323 bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
37a4c539 1324 FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv));
9fea808a 1325 fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
baca51fa 1326 } else {
4f431960
JM
1327 if (cur_drv->sect == cur_drv->last_sect) {
1328 fdctrl->data_state &= ~FD_STATE_FORMAT;
1329 /* Last sector done */
1330 if (FD_DID_SEEK(fdctrl->data_state))
9fea808a 1331 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
4f431960
JM
1332 else
1333 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1334 } else {
1335 /* More to do */
1336 fdctrl->data_pos = 0;
1337 fdctrl->data_len = 4;
1338 }
baca51fa
FB
1339 }
1340}
1341
5c02c033 1342static void fdctrl_handle_lock(FDCtrl *fdctrl, int direction)
65cef780
BS
1343{
1344 fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0;
1345 fdctrl->fifo[0] = fdctrl->lock << 4;
1346 fdctrl_set_fifo(fdctrl, 1, fdctrl->lock);
1347}
1348
5c02c033 1349static void fdctrl_handle_dumpreg(FDCtrl *fdctrl, int direction)
65cef780 1350{
5c02c033 1351 FDrive *cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1352
1353 /* Drives position */
1354 fdctrl->fifo[0] = drv0(fdctrl)->track;
1355 fdctrl->fifo[1] = drv1(fdctrl)->track;
78ae820c
BS
1356#if MAX_FD == 4
1357 fdctrl->fifo[2] = drv2(fdctrl)->track;
1358 fdctrl->fifo[3] = drv3(fdctrl)->track;
1359#else
65cef780
BS
1360 fdctrl->fifo[2] = 0;
1361 fdctrl->fifo[3] = 0;
78ae820c 1362#endif
65cef780
BS
1363 /* timers */
1364 fdctrl->fifo[4] = fdctrl->timer0;
368df94d 1365 fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0);
65cef780
BS
1366 fdctrl->fifo[6] = cur_drv->last_sect;
1367 fdctrl->fifo[7] = (fdctrl->lock << 7) |
1368 (cur_drv->perpendicular << 2);
1369 fdctrl->fifo[8] = fdctrl->config;
1370 fdctrl->fifo[9] = fdctrl->precomp_trk;
1371 fdctrl_set_fifo(fdctrl, 10, 0);
1372}
1373
5c02c033 1374static void fdctrl_handle_version(FDCtrl *fdctrl, int direction)
65cef780
BS
1375{
1376 /* Controller's version */
1377 fdctrl->fifo[0] = fdctrl->version;
1378 fdctrl_set_fifo(fdctrl, 1, 1);
1379}
1380
5c02c033 1381static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction)
65cef780
BS
1382{
1383 fdctrl->fifo[0] = 0x41; /* Stepping 1 */
1384 fdctrl_set_fifo(fdctrl, 1, 0);
1385}
1386
5c02c033 1387static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction)
65cef780 1388{
5c02c033 1389 FDrive *cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1390
1391 /* Drives position */
1392 drv0(fdctrl)->track = fdctrl->fifo[3];
1393 drv1(fdctrl)->track = fdctrl->fifo[4];
78ae820c
BS
1394#if MAX_FD == 4
1395 drv2(fdctrl)->track = fdctrl->fifo[5];
1396 drv3(fdctrl)->track = fdctrl->fifo[6];
1397#endif
65cef780
BS
1398 /* timers */
1399 fdctrl->timer0 = fdctrl->fifo[7];
1400 fdctrl->timer1 = fdctrl->fifo[8];
1401 cur_drv->last_sect = fdctrl->fifo[9];
1402 fdctrl->lock = fdctrl->fifo[10] >> 7;
1403 cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF;
1404 fdctrl->config = fdctrl->fifo[11];
1405 fdctrl->precomp_trk = fdctrl->fifo[12];
1406 fdctrl->pwrd = fdctrl->fifo[13];
1407 fdctrl_reset_fifo(fdctrl);
1408}
1409
5c02c033 1410static void fdctrl_handle_save(FDCtrl *fdctrl, int direction)
65cef780 1411{
5c02c033 1412 FDrive *cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1413
1414 fdctrl->fifo[0] = 0;
1415 fdctrl->fifo[1] = 0;
1416 /* Drives position */
1417 fdctrl->fifo[2] = drv0(fdctrl)->track;
1418 fdctrl->fifo[3] = drv1(fdctrl)->track;
78ae820c
BS
1419#if MAX_FD == 4
1420 fdctrl->fifo[4] = drv2(fdctrl)->track;
1421 fdctrl->fifo[5] = drv3(fdctrl)->track;
1422#else
65cef780
BS
1423 fdctrl->fifo[4] = 0;
1424 fdctrl->fifo[5] = 0;
78ae820c 1425#endif
65cef780
BS
1426 /* timers */
1427 fdctrl->fifo[6] = fdctrl->timer0;
1428 fdctrl->fifo[7] = fdctrl->timer1;
1429 fdctrl->fifo[8] = cur_drv->last_sect;
1430 fdctrl->fifo[9] = (fdctrl->lock << 7) |
1431 (cur_drv->perpendicular << 2);
1432 fdctrl->fifo[10] = fdctrl->config;
1433 fdctrl->fifo[11] = fdctrl->precomp_trk;
1434 fdctrl->fifo[12] = fdctrl->pwrd;
1435 fdctrl->fifo[13] = 0;
1436 fdctrl->fifo[14] = 0;
1437 fdctrl_set_fifo(fdctrl, 15, 1);
1438}
1439
5c02c033 1440static void fdctrl_handle_readid(FDCtrl *fdctrl, int direction)
65cef780 1441{
5c02c033 1442 FDrive *cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1443
1444 /* XXX: should set main status register to busy */
1445 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1446 qemu_mod_timer(fdctrl->result_timer,
74475455 1447 qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() / 50));
65cef780
BS
1448}
1449
5c02c033 1450static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction)
65cef780 1451{
5c02c033 1452 FDrive *cur_drv;
65cef780 1453
cefec4f5 1454 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
65cef780
BS
1455 cur_drv = get_cur_drv(fdctrl);
1456 fdctrl->data_state |= FD_STATE_FORMAT;
1457 if (fdctrl->fifo[0] & 0x80)
1458 fdctrl->data_state |= FD_STATE_MULTI;
1459 else
1460 fdctrl->data_state &= ~FD_STATE_MULTI;
1461 fdctrl->data_state &= ~FD_STATE_SEEK;
1462 cur_drv->bps =
1463 fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
1464#if 0
1465 cur_drv->last_sect =
1466 cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
1467 fdctrl->fifo[3] / 2;
1468#else
1469 cur_drv->last_sect = fdctrl->fifo[3];
1470#endif
1471 /* TODO: implement format using DMA expected by the Bochs BIOS
1472 * and Linux fdformat (read 3 bytes per sector via DMA and fill
1473 * the sector with the specified fill byte
1474 */
1475 fdctrl->data_state &= ~FD_STATE_FORMAT;
1476 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1477}
1478
5c02c033 1479static void fdctrl_handle_specify(FDCtrl *fdctrl, int direction)
65cef780
BS
1480{
1481 fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF;
1482 fdctrl->timer1 = fdctrl->fifo[2] >> 1;
368df94d
BS
1483 if (fdctrl->fifo[2] & 1)
1484 fdctrl->dor &= ~FD_DOR_DMAEN;
1485 else
1486 fdctrl->dor |= FD_DOR_DMAEN;
65cef780
BS
1487 /* No result back */
1488 fdctrl_reset_fifo(fdctrl);
1489}
1490
5c02c033 1491static void fdctrl_handle_sense_drive_status(FDCtrl *fdctrl, int direction)
65cef780 1492{
5c02c033 1493 FDrive *cur_drv;
65cef780 1494
cefec4f5 1495 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
65cef780
BS
1496 cur_drv = get_cur_drv(fdctrl);
1497 cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1498 /* 1 Byte status back */
1499 fdctrl->fifo[0] = (cur_drv->ro << 6) |
1500 (cur_drv->track == 0 ? 0x10 : 0x00) |
1501 (cur_drv->head << 2) |
cefec4f5 1502 GET_CUR_DRV(fdctrl) |
65cef780
BS
1503 0x28;
1504 fdctrl_set_fifo(fdctrl, 1, 0);
1505}
1506
5c02c033 1507static void fdctrl_handle_recalibrate(FDCtrl *fdctrl, int direction)
65cef780 1508{
5c02c033 1509 FDrive *cur_drv;
65cef780 1510
cefec4f5 1511 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
65cef780
BS
1512 cur_drv = get_cur_drv(fdctrl);
1513 fd_recalibrate(cur_drv);
1514 fdctrl_reset_fifo(fdctrl);
1515 /* Raise Interrupt */
1516 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1517}
1518
5c02c033 1519static void fdctrl_handle_sense_interrupt_status(FDCtrl *fdctrl, int direction)
65cef780 1520{
5c02c033 1521 FDrive *cur_drv = get_cur_drv(fdctrl);
65cef780 1522
f2d81b33
BS
1523 if(fdctrl->reset_sensei > 0) {
1524 fdctrl->fifo[0] =
1525 FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei;
1526 fdctrl->reset_sensei--;
1527 } else {
1528 /* XXX: status0 handling is broken for read/write
1529 commands, so we do this hack. It should be suppressed
1530 ASAP */
1531 fdctrl->fifo[0] =
1532 FD_SR0_SEEK | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
1533 }
1534
65cef780
BS
1535 fdctrl->fifo[1] = cur_drv->track;
1536 fdctrl_set_fifo(fdctrl, 2, 0);
1537 fdctrl_reset_irq(fdctrl);
77370520 1538 fdctrl->status0 = FD_SR0_RDYCHG;
65cef780
BS
1539}
1540
5c02c033 1541static void fdctrl_handle_seek(FDCtrl *fdctrl, int direction)
65cef780 1542{
5c02c033 1543 FDrive *cur_drv;
65cef780 1544
cefec4f5 1545 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
65cef780 1546 cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1547 fdctrl_reset_fifo(fdctrl);
1548 if (fdctrl->fifo[2] > cur_drv->max_track) {
1549 fdctrl_raise_irq(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK);
1550 } else {
1551 cur_drv->track = fdctrl->fifo[2];
1552 /* Raise Interrupt */
1553 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1554 }
1555}
1556
5c02c033 1557static void fdctrl_handle_perpendicular_mode(FDCtrl *fdctrl, int direction)
65cef780 1558{
5c02c033 1559 FDrive *cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1560
1561 if (fdctrl->fifo[1] & 0x80)
1562 cur_drv->perpendicular = fdctrl->fifo[1] & 0x7;
1563 /* No result back */
1c346df2 1564 fdctrl_reset_fifo(fdctrl);
65cef780
BS
1565}
1566
5c02c033 1567static void fdctrl_handle_configure(FDCtrl *fdctrl, int direction)
65cef780
BS
1568{
1569 fdctrl->config = fdctrl->fifo[2];
1570 fdctrl->precomp_trk = fdctrl->fifo[3];
1571 /* No result back */
1572 fdctrl_reset_fifo(fdctrl);
1573}
1574
5c02c033 1575static void fdctrl_handle_powerdown_mode(FDCtrl *fdctrl, int direction)
65cef780
BS
1576{
1577 fdctrl->pwrd = fdctrl->fifo[1];
1578 fdctrl->fifo[0] = fdctrl->fifo[1];
1579 fdctrl_set_fifo(fdctrl, 1, 1);
1580}
1581
5c02c033 1582static void fdctrl_handle_option(FDCtrl *fdctrl, int direction)
65cef780
BS
1583{
1584 /* No result back */
1585 fdctrl_reset_fifo(fdctrl);
1586}
1587
5c02c033 1588static void fdctrl_handle_drive_specification_command(FDCtrl *fdctrl, int direction)
65cef780 1589{
5c02c033 1590 FDrive *cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1591
1592 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x80) {
1593 /* Command parameters done */
1594 if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x40) {
1595 fdctrl->fifo[0] = fdctrl->fifo[1];
1596 fdctrl->fifo[2] = 0;
1597 fdctrl->fifo[3] = 0;
1598 fdctrl_set_fifo(fdctrl, 4, 1);
1599 } else {
1600 fdctrl_reset_fifo(fdctrl);
1601 }
1602 } else if (fdctrl->data_len > 7) {
1603 /* ERROR */
1604 fdctrl->fifo[0] = 0x80 |
cefec4f5 1605 (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
65cef780
BS
1606 fdctrl_set_fifo(fdctrl, 1, 1);
1607 }
1608}
1609
5c02c033 1610static void fdctrl_handle_relative_seek_out(FDCtrl *fdctrl, int direction)
65cef780 1611{
5c02c033 1612 FDrive *cur_drv;
65cef780 1613
cefec4f5 1614 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
65cef780 1615 cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1616 if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) {
1617 cur_drv->track = cur_drv->max_track - 1;
1618 } else {
1619 cur_drv->track += fdctrl->fifo[2];
1620 }
1621 fdctrl_reset_fifo(fdctrl);
77370520 1622 /* Raise Interrupt */
65cef780
BS
1623 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1624}
1625
5c02c033 1626static void fdctrl_handle_relative_seek_in(FDCtrl *fdctrl, int direction)
65cef780 1627{
5c02c033 1628 FDrive *cur_drv;
65cef780 1629
cefec4f5 1630 SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
65cef780 1631 cur_drv = get_cur_drv(fdctrl);
65cef780
BS
1632 if (fdctrl->fifo[2] > cur_drv->track) {
1633 cur_drv->track = 0;
1634 } else {
1635 cur_drv->track -= fdctrl->fifo[2];
1636 }
1637 fdctrl_reset_fifo(fdctrl);
1638 /* Raise Interrupt */
1639 fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1640}
1641
678803ab
BS
1642static const struct {
1643 uint8_t value;
1644 uint8_t mask;
1645 const char* name;
1646 int parameters;
5c02c033 1647 void (*handler)(FDCtrl *fdctrl, int direction);
678803ab
BS
1648 int direction;
1649} handlers[] = {
1650 { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ },
1651 { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE },
1652 { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek },
1653 { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status },
1654 { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate },
1655 { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track },
1656 { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ },
1657 { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */
1658 { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */
1659 { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ },
1660 { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE },
1661 { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_unimplemented },
1662 { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL },
1663 { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH },
1664 { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE },
1665 { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid },
1666 { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify },
1667 { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status },
1668 { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode },
1669 { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure },
1670 { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode },
1671 { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option },
1672 { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command },
1673 { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out },
1674 { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented },
1675 { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in },
1676 { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock },
1677 { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg },
1678 { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version },
1679 { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid },
1680 { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */
1681 { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */
1682};
1683/* Associate command to an index in the 'handlers' array */
1684static uint8_t command_to_handler[256];
1685
5c02c033 1686static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value)
baca51fa 1687{
5c02c033 1688 FDrive *cur_drv;
65cef780 1689 int pos;
baca51fa 1690
8977f3c1 1691 /* Reset mode */
1c346df2 1692 if (!(fdctrl->dor & FD_DOR_nRESET)) {
4b19ec0c 1693 FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
8977f3c1
FB
1694 return;
1695 }
b9b3d225
BS
1696 if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) {
1697 FLOPPY_ERROR("controller not ready for writing\n");
8977f3c1
FB
1698 return;
1699 }
b9b3d225 1700 fdctrl->dsr &= ~FD_DSR_PWRDOWN;
8977f3c1 1701 /* Is it write command time ? */
368df94d 1702 if (fdctrl->msr & FD_MSR_NONDMA) {
8977f3c1 1703 /* FIFO data write */
b3bc1540
BS
1704 pos = fdctrl->data_pos++;
1705 pos %= FD_SECTOR_LEN;
1706 fdctrl->fifo[pos] = value;
1707 if (pos == FD_SECTOR_LEN - 1 ||
baca51fa 1708 fdctrl->data_pos == fdctrl->data_len) {
77370520
BS
1709 cur_drv = get_cur_drv(fdctrl);
1710 if (bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1711 FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv));
1712 return;
1713 }
746d6de7
BS
1714 if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
1715 FLOPPY_DPRINTF("error seeking to next sector %d\n",
1716 fd_sector(cur_drv));
1717 return;
1718 }
8977f3c1 1719 }
890fa6be 1720 /* Switch from transfer mode to status mode
8977f3c1
FB
1721 * then from status mode to command mode
1722 */
b9b3d225 1723 if (fdctrl->data_pos == fdctrl->data_len)
9fea808a 1724 fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
8977f3c1
FB
1725 return;
1726 }
baca51fa 1727 if (fdctrl->data_pos == 0) {
8977f3c1 1728 /* Command */
678803ab
BS
1729 pos = command_to_handler[value & 0xff];
1730 FLOPPY_DPRINTF("%s command\n", handlers[pos].name);
1731 fdctrl->data_len = handlers[pos].parameters + 1;
8977f3c1 1732 }
678803ab 1733
baca51fa 1734 FLOPPY_DPRINTF("%s: %02x\n", __func__, value);
77370520
BS
1735 fdctrl->fifo[fdctrl->data_pos++] = value;
1736 if (fdctrl->data_pos == fdctrl->data_len) {
8977f3c1
FB
1737 /* We now have all parameters
1738 * and will be able to treat the command
1739 */
4f431960
JM
1740 if (fdctrl->data_state & FD_STATE_FORMAT) {
1741 fdctrl_format_sector(fdctrl);
8977f3c1
FB
1742 return;
1743 }
65cef780 1744
678803ab
BS
1745 pos = command_to_handler[fdctrl->fifo[0] & 0xff];
1746 FLOPPY_DPRINTF("treat %s command\n", handlers[pos].name);
1747 (*handlers[pos].handler)(fdctrl, handlers[pos].direction);
8977f3c1
FB
1748 }
1749}
ed5fd2cc
FB
1750
1751static void fdctrl_result_timer(void *opaque)
1752{
5c02c033
BS
1753 FDCtrl *fdctrl = opaque;
1754 FDrive *cur_drv = get_cur_drv(fdctrl);
4f431960 1755
b7ffa3b1
TS
1756 /* Pretend we are spinning.
1757 * This is needed for Coherent, which uses READ ID to check for
1758 * sector interleaving.
1759 */
1760 if (cur_drv->last_sect != 0) {
1761 cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1;
1762 }
ed5fd2cc
FB
1763 fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1764}
678803ab 1765
7d4b4ba5 1766static void fdctrl_change_cb(void *opaque, bool load)
8e49ca46
MA
1767{
1768 FDrive *drive = opaque;
1769
1770 drive->media_changed = 1;
1771}
1772
1773static const BlockDevOps fdctrl_block_ops = {
1774 .change_media_cb = fdctrl_change_cb,
1775};
1776
678803ab 1777/* Init functions */
b47b3525 1778static int fdctrl_connect_drives(FDCtrl *fdctrl)
678803ab 1779{
12a71a02 1780 unsigned int i;
7d0d6950 1781 FDrive *drive;
678803ab 1782
678803ab 1783 for (i = 0; i < MAX_FD; i++) {
7d0d6950
MA
1784 drive = &fdctrl->drives[i];
1785
b47b3525
MA
1786 if (drive->bs) {
1787 if (bdrv_get_on_error(drive->bs, 0) != BLOCK_ERR_STOP_ENOSPC) {
1788 error_report("fdc doesn't support drive option werror");
1789 return -1;
1790 }
1791 if (bdrv_get_on_error(drive->bs, 1) != BLOCK_ERR_REPORT) {
1792 error_report("fdc doesn't support drive option rerror");
1793 return -1;
1794 }
1795 }
1796
7d0d6950
MA
1797 fd_init(drive);
1798 fd_revalidate(drive);
1799 if (drive->bs) {
8e49ca46 1800 drive->media_changed = 1;
8e49ca46 1801 bdrv_set_dev_ops(drive->bs, &fdctrl_block_ops, drive);
7d0d6950 1802 }
678803ab 1803 }
b47b3525 1804 return 0;
678803ab
BS
1805}
1806
63ffb564
BS
1807void fdctrl_init_sysbus(qemu_irq irq, int dma_chann,
1808 target_phys_addr_t mmio_base, DriveInfo **fds)
2091ba23 1809{
5c02c033 1810 FDCtrl *fdctrl;
2091ba23 1811 DeviceState *dev;
5c02c033 1812 FDCtrlSysBus *sys;
2091ba23
GH
1813
1814 dev = qdev_create(NULL, "sysbus-fdc");
5c02c033 1815 sys = DO_UPCAST(FDCtrlSysBus, busdev.qdev, dev);
99244fa1
GH
1816 fdctrl = &sys->state;
1817 fdctrl->dma_chann = dma_chann; /* FIXME */
995bf0ca 1818 if (fds[0]) {
18846dee 1819 qdev_prop_set_drive_nofail(dev, "driveA", fds[0]->bdrv);
995bf0ca
GH
1820 }
1821 if (fds[1]) {
18846dee 1822 qdev_prop_set_drive_nofail(dev, "driveB", fds[1]->bdrv);
995bf0ca 1823 }
e23a1b33 1824 qdev_init_nofail(dev);
2091ba23
GH
1825 sysbus_connect_irq(&sys->busdev, 0, irq);
1826 sysbus_mmio_map(&sys->busdev, 0, mmio_base);
678803ab
BS
1827}
1828
63ffb564
BS
1829void sun4m_fdctrl_init(qemu_irq irq, target_phys_addr_t io_base,
1830 DriveInfo **fds, qemu_irq *fdc_tc)
678803ab 1831{
f64ab228 1832 DeviceState *dev;
5c02c033 1833 FDCtrlSysBus *sys;
678803ab 1834
12a71a02 1835 dev = qdev_create(NULL, "SUNW,fdtwo");
995bf0ca 1836 if (fds[0]) {
18846dee 1837 qdev_prop_set_drive_nofail(dev, "drive", fds[0]->bdrv);
995bf0ca 1838 }
e23a1b33 1839 qdev_init_nofail(dev);
5c02c033 1840 sys = DO_UPCAST(FDCtrlSysBus, busdev.qdev, dev);
8baf73ad
GH
1841 sysbus_connect_irq(&sys->busdev, 0, irq);
1842 sysbus_mmio_map(&sys->busdev, 0, io_base);
f64ab228 1843 *fdc_tc = qdev_get_gpio_in(dev, 0);
678803ab 1844}
f64ab228 1845
a64405d1 1846static int fdctrl_init_common(FDCtrl *fdctrl)
f64ab228 1847{
12a71a02
BS
1848 int i, j;
1849 static int command_tables_inited = 0;
f64ab228 1850
12a71a02
BS
1851 /* Fill 'command_to_handler' lookup table */
1852 if (!command_tables_inited) {
1853 command_tables_inited = 1;
1854 for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) {
1855 for (j = 0; j < sizeof(command_to_handler); j++) {
1856 if ((j & handlers[i].mask) == handlers[i].value) {
1857 command_to_handler[j] = i;
1858 }
1859 }
1860 }
1861 }
1862
1863 FLOPPY_DPRINTF("init controller\n");
1864 fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN);
d7a6c270 1865 fdctrl->fifo_size = 512;
74475455 1866 fdctrl->result_timer = qemu_new_timer_ns(vm_clock,
12a71a02
BS
1867 fdctrl_result_timer, fdctrl);
1868
1869 fdctrl->version = 0x90; /* Intel 82078 controller */
1870 fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */
d7a6c270 1871 fdctrl->num_floppies = MAX_FD;
12a71a02 1872
99244fa1
GH
1873 if (fdctrl->dma_chann != -1)
1874 DMA_register_channel(fdctrl->dma_chann, &fdctrl_transfer_handler, fdctrl);
b47b3525 1875 return fdctrl_connect_drives(fdctrl);
f64ab228
BS
1876}
1877
212ec7ba 1878static const MemoryRegionPortio fdc_portio_list[] = {
2f290a8c 1879 { 1, 5, 1, .read = fdctrl_read, .write = fdctrl_write },
212ec7ba
RH
1880 { 7, 1, 1, .read = fdctrl_read, .write = fdctrl_write },
1881 PORTIO_END_OF_LIST(),
2f290a8c
RH
1882};
1883
81a322d4 1884static int isabus_fdc_init1(ISADevice *dev)
8baf73ad 1885{
5c02c033
BS
1886 FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev);
1887 FDCtrl *fdctrl = &isa->state;
86c86157 1888 int iobase = 0x3f0;
2e15e23b 1889 int isairq = 6;
99244fa1 1890 int dma_chann = 2;
2be37833 1891 int ret;
8baf73ad 1892
212ec7ba 1893 isa_register_portio_list(dev, iobase, fdc_portio_list, fdctrl, "fdc");
dee41d58 1894
2e15e23b 1895 isa_init_irq(&isa->busdev, &fdctrl->irq, isairq);
99244fa1 1896 fdctrl->dma_chann = dma_chann;
8baf73ad 1897
a64405d1
JK
1898 qdev_set_legacy_instance_id(&dev->qdev, iobase, 2);
1899 ret = fdctrl_init_common(fdctrl);
2be37833 1900
1ca4d09a
GN
1901 add_boot_device_path(isa->bootindexA, &dev->qdev, "/floppy@0");
1902 add_boot_device_path(isa->bootindexB, &dev->qdev, "/floppy@1");
1903
2be37833 1904 return ret;
8baf73ad
GH
1905}
1906
81a322d4 1907static int sysbus_fdc_init1(SysBusDevice *dev)
12a71a02 1908{
5c02c033
BS
1909 FDCtrlSysBus *sys = DO_UPCAST(FDCtrlSysBus, busdev, dev);
1910 FDCtrl *fdctrl = &sys->state;
2be37833 1911 int ret;
12a71a02 1912
dc6c1b37 1913 memory_region_init_io(&fdctrl->iomem, &fdctrl_mem_ops, fdctrl, "fdc", 0x08);
750ecd44 1914 sysbus_init_mmio(dev, &fdctrl->iomem);
8baf73ad
GH
1915 sysbus_init_irq(dev, &fdctrl->irq);
1916 qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1);
99244fa1 1917 fdctrl->dma_chann = -1;
8baf73ad 1918
dc6c1b37 1919 qdev_set_legacy_instance_id(&dev->qdev, 0 /* io */, 2); /* FIXME */
a64405d1 1920 ret = fdctrl_init_common(fdctrl);
2be37833
BS
1921
1922 return ret;
12a71a02
BS
1923}
1924
81a322d4 1925static int sun4m_fdc_init1(SysBusDevice *dev)
12a71a02 1926{
5c02c033 1927 FDCtrl *fdctrl = &(FROM_SYSBUS(FDCtrlSysBus, dev)->state);
12a71a02 1928
dc6c1b37
AK
1929 memory_region_init_io(&fdctrl->iomem, &fdctrl_mem_strict_ops, fdctrl,
1930 "fdctrl", 0x08);
750ecd44 1931 sysbus_init_mmio(dev, &fdctrl->iomem);
8baf73ad
GH
1932 sysbus_init_irq(dev, &fdctrl->irq);
1933 qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1);
1934
1935 fdctrl->sun4m = 1;
dc6c1b37 1936 qdev_set_legacy_instance_id(&dev->qdev, 0 /* io */, 2); /* FIXME */
a64405d1 1937 return fdctrl_init_common(fdctrl);
12a71a02 1938}
f64ab228 1939
34d4260e
KW
1940void fdc_get_bs(BlockDriverState *bs[], ISADevice *dev)
1941{
1942 FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev);
1943 FDCtrl *fdctrl = &isa->state;
1944 int i;
1945
1946 for (i = 0; i < MAX_FD; i++) {
1947 bs[i] = fdctrl->drives[i].bs;
1948 }
1949}
1950
1951
a64405d1
JK
1952static const VMStateDescription vmstate_isa_fdc ={
1953 .name = "fdc",
1954 .version_id = 2,
1955 .minimum_version_id = 2,
1956 .fields = (VMStateField []) {
1957 VMSTATE_STRUCT(state, FDCtrlISABus, 0, vmstate_fdc, FDCtrl),
1958 VMSTATE_END_OF_LIST()
1959 }
1960};
1961
8baf73ad
GH
1962static ISADeviceInfo isa_fdc_info = {
1963 .init = isabus_fdc_init1,
1964 .qdev.name = "isa-fdc",
779206de 1965 .qdev.fw_name = "fdc",
5c02c033 1966 .qdev.size = sizeof(FDCtrlISABus),
39a51dfd 1967 .qdev.no_user = 1,
a64405d1 1968 .qdev.vmsd = &vmstate_isa_fdc,
2be37833 1969 .qdev.reset = fdctrl_external_reset_isa,
fd8014e1 1970 .qdev.props = (Property[]) {
f8b6cc00
MA
1971 DEFINE_PROP_DRIVE("driveA", FDCtrlISABus, state.drives[0].bs),
1972 DEFINE_PROP_DRIVE("driveB", FDCtrlISABus, state.drives[1].bs),
1ca4d09a
GN
1973 DEFINE_PROP_INT32("bootindexA", FDCtrlISABus, bootindexA, -1),
1974 DEFINE_PROP_INT32("bootindexB", FDCtrlISABus, bootindexB, -1),
fd8014e1
GH
1975 DEFINE_PROP_END_OF_LIST(),
1976 },
8baf73ad
GH
1977};
1978
a64405d1
JK
1979static const VMStateDescription vmstate_sysbus_fdc ={
1980 .name = "fdc",
1981 .version_id = 2,
1982 .minimum_version_id = 2,
1983 .fields = (VMStateField []) {
1984 VMSTATE_STRUCT(state, FDCtrlSysBus, 0, vmstate_fdc, FDCtrl),
1985 VMSTATE_END_OF_LIST()
1986 }
1987};
1988
8baf73ad
GH
1989static SysBusDeviceInfo sysbus_fdc_info = {
1990 .init = sysbus_fdc_init1,
1991 .qdev.name = "sysbus-fdc",
5c02c033 1992 .qdev.size = sizeof(FDCtrlSysBus),
a64405d1 1993 .qdev.vmsd = &vmstate_sysbus_fdc,
2be37833 1994 .qdev.reset = fdctrl_external_reset_sysbus,
fd8014e1 1995 .qdev.props = (Property[]) {
f8b6cc00
MA
1996 DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus, state.drives[0].bs),
1997 DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus, state.drives[1].bs),
fd8014e1
GH
1998 DEFINE_PROP_END_OF_LIST(),
1999 },
12a71a02
BS
2000};
2001
2002static SysBusDeviceInfo sun4m_fdc_info = {
2003 .init = sun4m_fdc_init1,
2004 .qdev.name = "SUNW,fdtwo",
5c02c033 2005 .qdev.size = sizeof(FDCtrlSysBus),
a64405d1 2006 .qdev.vmsd = &vmstate_sysbus_fdc,
2be37833 2007 .qdev.reset = fdctrl_external_reset_sysbus,
fd8014e1 2008 .qdev.props = (Property[]) {
f8b6cc00 2009 DEFINE_PROP_DRIVE("drive", FDCtrlSysBus, state.drives[0].bs),
fd8014e1
GH
2010 DEFINE_PROP_END_OF_LIST(),
2011 },
f64ab228
BS
2012};
2013
2014static void fdc_register_devices(void)
2015{
8baf73ad
GH
2016 isa_qdev_register(&isa_fdc_info);
2017 sysbus_register_withprop(&sysbus_fdc_info);
12a71a02 2018 sysbus_register_withprop(&sun4m_fdc_info);
f64ab228
BS
2019}
2020
2021device_init(fdc_register_devices)