fdc.c 92.0 KB
Newer Older
B
bellard 已提交
1
/*
2
 * QEMU Floppy disk emulator (Intel 82078)
3
 *
4
 * Copyright (c) 2003, 2007 Jocelyn Mayer
5
 * Copyright (c) 2008 Hervé Poussineau
6
 *
B
bellard 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
B
bellard 已提交
25 26 27 28
/*
 * The controller is used in Sun4m systems in a slightly different
 * way. There are changes in DOR register and DMA is not available.
 */
B
Blue Swirl 已提交
29

P
Peter Maydell 已提交
30
#include "qemu/osdep.h"
P
Paolo Bonzini 已提交
31
#include "hw/block/fdc.h"
32
#include "qapi/error.h"
33 34
#include "qemu/error-report.h"
#include "qemu/timer.h"
35
#include "hw/acpi/aml-build.h"
M
Markus Armbruster 已提交
36
#include "hw/irq.h"
P
Paolo Bonzini 已提交
37
#include "hw/isa/isa.h"
38
#include "hw/qdev-properties.h"
39
#include "hw/sysbus.h"
40
#include "migration/vmstate.h"
41
#include "hw/block/block.h"
42
#include "sysemu/block-backend.h"
43 44
#include "sysemu/blockdev.h"
#include "sysemu/sysemu.h"
45
#include "qemu/log.h"
46
#include "qemu/main-loop.h"
47
#include "qemu/module.h"
48
#include "trace.h"
B
bellard 已提交
49 50 51 52

/********************************************************/
/* debug Floppy devices */

53 54
#define DEBUG_FLOPPY 0

55
#define FLOPPY_DPRINTF(fmt, ...)                                \
56 57 58 59 60
    do {                                                        \
        if (DEBUG_FLOPPY) {                                     \
            fprintf(stderr, "FLOPPY: " fmt , ## __VA_ARGS__);   \
        }                                                       \
    } while (0)
B
bellard 已提交
61

K
Kevin Wolf 已提交
62 63 64 65 66 67 68 69

/********************************************************/
/* qdev floppy bus                                      */

#define TYPE_FLOPPY_BUS "floppy-bus"
#define FLOPPY_BUS(obj) OBJECT_CHECK(FloppyBus, (obj), TYPE_FLOPPY_BUS)

typedef struct FDCtrl FDCtrl;
K
Kevin Wolf 已提交
70 71
typedef struct FDrive FDrive;
static FDrive *get_drv(FDCtrl *fdctrl, int unit);
K
Kevin Wolf 已提交
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90

typedef struct FloppyBus {
    BusState bus;
    FDCtrl *fdc;
} FloppyBus;

static const TypeInfo floppy_bus_info = {
    .name = TYPE_FLOPPY_BUS,
    .parent = TYPE_BUS,
    .instance_size = sizeof(FloppyBus),
};

static void floppy_bus_create(FDCtrl *fdc, FloppyBus *bus, DeviceState *dev)
{
    qbus_create_inplace(bus, sizeof(FloppyBus), TYPE_FLOPPY_BUS, dev, NULL);
    bus->fdc = fdc;
}


B
bellard 已提交
91 92 93
/********************************************************/
/* Floppy drive emulation                               */

94 95 96 97 98 99 100
typedef enum FDriveRate {
    FDRIVE_RATE_500K = 0x00,  /* 500 Kbps */
    FDRIVE_RATE_300K = 0x01,  /* 300 Kbps */
    FDRIVE_RATE_250K = 0x02,  /* 250 Kbps */
    FDRIVE_RATE_1M   = 0x03,  /*   1 Mbps */
} FDriveRate;

J
John Snow 已提交
101 102 103 104 105 106
typedef enum FDriveSize {
    FDRIVE_SIZE_UNKNOWN,
    FDRIVE_SIZE_350,
    FDRIVE_SIZE_525,
} FDriveSize;

107
typedef struct FDFormat {
J
John Snow 已提交
108
    FloppyDriveType drive;
109 110 111 112 113 114
    uint8_t last_sect;
    uint8_t max_track;
    uint8_t max_head;
    FDriveRate rate;
} FDFormat;

J
John Snow 已提交
115 116 117 118
/* In many cases, the total sector size of a format is enough to uniquely
 * identify it. However, there are some total sector collisions between
 * formats of different physical size, and these are noted below by
 * highlighting the total sector size for entries with collisions. */
119 120 121
static const FDFormat fd_formats[] = {
    /* First entry is default format */
    /* 1.44 MB 3"1/2 floppy disks */
J
John Snow 已提交
122 123
    { FLOPPY_DRIVE_TYPE_144, 18, 80, 1, FDRIVE_RATE_500K, }, /* 3.5" 2880 */
    { FLOPPY_DRIVE_TYPE_144, 20, 80, 1, FDRIVE_RATE_500K, }, /* 3.5" 3200 */
J
John Snow 已提交
124 125 126 127 128 129
    { FLOPPY_DRIVE_TYPE_144, 21, 80, 1, FDRIVE_RATE_500K, },
    { FLOPPY_DRIVE_TYPE_144, 21, 82, 1, FDRIVE_RATE_500K, },
    { FLOPPY_DRIVE_TYPE_144, 21, 83, 1, FDRIVE_RATE_500K, },
    { FLOPPY_DRIVE_TYPE_144, 22, 80, 1, FDRIVE_RATE_500K, },
    { FLOPPY_DRIVE_TYPE_144, 23, 80, 1, FDRIVE_RATE_500K, },
    { FLOPPY_DRIVE_TYPE_144, 24, 80, 1, FDRIVE_RATE_500K, },
130
    /* 2.88 MB 3"1/2 floppy disks */
J
John Snow 已提交
131 132 133 134 135
    { FLOPPY_DRIVE_TYPE_288, 36, 80, 1, FDRIVE_RATE_1M, },
    { FLOPPY_DRIVE_TYPE_288, 39, 80, 1, FDRIVE_RATE_1M, },
    { FLOPPY_DRIVE_TYPE_288, 40, 80, 1, FDRIVE_RATE_1M, },
    { FLOPPY_DRIVE_TYPE_288, 44, 80, 1, FDRIVE_RATE_1M, },
    { FLOPPY_DRIVE_TYPE_288, 48, 80, 1, FDRIVE_RATE_1M, },
136
    /* 720 kB 3"1/2 floppy disks */
J
John Snow 已提交
137
    { FLOPPY_DRIVE_TYPE_144,  9, 80, 1, FDRIVE_RATE_250K, }, /* 3.5" 1440 */
J
John Snow 已提交
138 139 140 141 142
    { FLOPPY_DRIVE_TYPE_144, 10, 80, 1, FDRIVE_RATE_250K, },
    { FLOPPY_DRIVE_TYPE_144, 10, 82, 1, FDRIVE_RATE_250K, },
    { FLOPPY_DRIVE_TYPE_144, 10, 83, 1, FDRIVE_RATE_250K, },
    { FLOPPY_DRIVE_TYPE_144, 13, 80, 1, FDRIVE_RATE_250K, },
    { FLOPPY_DRIVE_TYPE_144, 14, 80, 1, FDRIVE_RATE_250K, },
143
    /* 1.2 MB 5"1/4 floppy disks */
J
John Snow 已提交
144
    { FLOPPY_DRIVE_TYPE_120, 15, 80, 1, FDRIVE_RATE_500K, },
J
John Snow 已提交
145
    { FLOPPY_DRIVE_TYPE_120, 18, 80, 1, FDRIVE_RATE_500K, }, /* 5.25" 2880 */
J
John Snow 已提交
146 147
    { FLOPPY_DRIVE_TYPE_120, 18, 82, 1, FDRIVE_RATE_500K, },
    { FLOPPY_DRIVE_TYPE_120, 18, 83, 1, FDRIVE_RATE_500K, },
J
John Snow 已提交
148
    { FLOPPY_DRIVE_TYPE_120, 20, 80, 1, FDRIVE_RATE_500K, }, /* 5.25" 3200 */
149
    /* 720 kB 5"1/4 floppy disks */
J
John Snow 已提交
150
    { FLOPPY_DRIVE_TYPE_120,  9, 80, 1, FDRIVE_RATE_250K, }, /* 5.25" 1440 */
J
John Snow 已提交
151
    { FLOPPY_DRIVE_TYPE_120, 11, 80, 1, FDRIVE_RATE_250K, },
152
    /* 360 kB 5"1/4 floppy disks */
J
John Snow 已提交
153
    { FLOPPY_DRIVE_TYPE_120,  9, 40, 1, FDRIVE_RATE_300K, }, /* 5.25" 720 */
J
John Snow 已提交
154 155 156
    { FLOPPY_DRIVE_TYPE_120,  9, 40, 0, FDRIVE_RATE_300K, },
    { FLOPPY_DRIVE_TYPE_120, 10, 41, 1, FDRIVE_RATE_300K, },
    { FLOPPY_DRIVE_TYPE_120, 10, 42, 1, FDRIVE_RATE_300K, },
157
    /* 320 kB 5"1/4 floppy disks */
J
John Snow 已提交
158 159
    { FLOPPY_DRIVE_TYPE_120,  8, 40, 1, FDRIVE_RATE_250K, },
    { FLOPPY_DRIVE_TYPE_120,  8, 40, 0, FDRIVE_RATE_250K, },
160
    /* 360 kB must match 5"1/4 better than 3"1/2... */
J
John Snow 已提交
161
    { FLOPPY_DRIVE_TYPE_144,  9, 80, 0, FDRIVE_RATE_250K, }, /* 3.5" 720 */
162
    /* end */
J
John Snow 已提交
163
    { FLOPPY_DRIVE_TYPE_NONE, -1, -1, 0, 0, },
164 165
};

J
John Snow 已提交
166 167 168 169 170 171 172 173 174 175 176 177 178
static FDriveSize drive_size(FloppyDriveType drive)
{
    switch (drive) {
    case FLOPPY_DRIVE_TYPE_120:
        return FDRIVE_SIZE_525;
    case FLOPPY_DRIVE_TYPE_144:
    case FLOPPY_DRIVE_TYPE_288:
        return FDRIVE_SIZE_350;
    default:
        return FDRIVE_SIZE_UNKNOWN;
    }
}

B
blueswir1 已提交
179 180 181
#define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv)
#define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive))

B
bellard 已提交
182
/* Will always be a fixed parameter for us */
183 184 185
#define FD_SECTOR_LEN          512
#define FD_SECTOR_SC           2   /* Sector size code */
#define FD_RESET_SENSEI_COUNT  4   /* Number of sense interrupts on RESET */
B
bellard 已提交
186 187

/* Floppy disk drive emulation */
B
Blue Swirl 已提交
188
typedef enum FDiskFlags {
189
    FDISK_DBL_SIDES  = 0x01,
B
Blue Swirl 已提交
190
} FDiskFlags;
191

K
Kevin Wolf 已提交
192
struct FDrive {
193
    FDCtrl *fdctrl;
194
    BlockBackend *blk;
K
Kevin Wolf 已提交
195
    BlockConf *conf;
B
bellard 已提交
196
    /* Drive status */
J
John Snow 已提交
197
    FloppyDriveType drive;    /* CMOS drive type        */
B
bellard 已提交
198 199 200 201 202 203
    uint8_t perpendicular;    /* 2.88 MB access mode    */
    /* Position */
    uint8_t head;
    uint8_t track;
    uint8_t sect;
    /* Media */
J
John Snow 已提交
204
    FloppyDriveType disk;     /* Current disk type      */
B
Blue Swirl 已提交
205
    FDiskFlags flags;
B
bellard 已提交
206 207
    uint8_t last_sect;        /* Nb sector per track    */
    uint8_t max_track;        /* Nb of tracks           */
208
    uint16_t bps;             /* Bytes per sector       */
B
bellard 已提交
209
    uint8_t ro;               /* Is read-only           */
210
    uint8_t media_changed;    /* Is media changed       */
211
    uint8_t media_rate;       /* Data rate of medium    */
M
Max Reitz 已提交
212

J
John Snow 已提交
213
    bool media_validated;     /* Have we validated the media? */
K
Kevin Wolf 已提交
214
};
B
bellard 已提交
215

J
John Snow 已提交
216 217 218

static FloppyDriveType get_fallback_drive_type(FDrive *drv);

J
John Snow 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
/* Hack: FD_SEEK is expected to work on empty drives. However, QEMU
 * currently goes through some pains to keep seeks within the bounds
 * established by last_sect and max_track. Correcting this is difficult,
 * as refactoring FDC code tends to expose nasty bugs in the Linux kernel.
 *
 * For now: allow empty drives to have large bounds so we can seek around,
 * with the understanding that when a diskette is inserted, the bounds will
 * properly tighten to match the geometry of that inserted medium.
 */
static void fd_empty_seek_hack(FDrive *drv)
{
    drv->last_sect = 0xFF;
    drv->max_track = 0xFF;
}

B
Blue Swirl 已提交
234
static void fd_init(FDrive *drv)
B
bellard 已提交
235 236 237 238
{
    /* Drive */
    drv->perpendicular = 0;
    /* Disk */
J
John Snow 已提交
239
    drv->disk = FLOPPY_DRIVE_TYPE_NONE;
240
    drv->last_sect = 0;
B
bellard 已提交
241
    drv->max_track = 0;
J
John Snow 已提交
242 243
    drv->ro = true;
    drv->media_changed = 1;
B
bellard 已提交
244 245
}

246 247
#define NUM_SIDES(drv) ((drv)->flags & FDISK_DBL_SIDES ? 2 : 1)

B
Blue Swirl 已提交
248
static int fd_sector_calc(uint8_t head, uint8_t track, uint8_t sect,
249
                          uint8_t last_sect, uint8_t num_sides)
B
bellard 已提交
250
{
251
    return (((track * num_sides) + head) * last_sect) + sect - 1;
B
bellard 已提交
252 253 254
}

/* Returns current position, in sectors, for given drive */
B
Blue Swirl 已提交
255
static int fd_sector(FDrive *drv)
B
bellard 已提交
256
{
257 258
    return fd_sector_calc(drv->head, drv->track, drv->sect, drv->last_sect,
                          NUM_SIDES(drv));
B
bellard 已提交
259 260
}

261 262 263 264 265 266 267
/* Returns current position, in bytes, for given drive */
static int fd_offset(FDrive *drv)
{
    g_assert(fd_sector(drv) < INT_MAX >> BDRV_SECTOR_BITS);
    return fd_sector(drv) << BDRV_SECTOR_BITS;
}

B
blueswir1 已提交
268 269 270 271 272 273 274
/* Seek to a new position:
 * returns 0 if already on right track
 * returns 1 if track changed
 * returns 2 if track is invalid
 * returns 3 if sector is invalid
 * returns 4 if seek is disabled
 */
B
Blue Swirl 已提交
275 276
static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect,
                   int enable_seek)
B
bellard 已提交
277 278
{
    uint32_t sector;
279 280 281
    int ret;

    if (track > drv->max_track ||
282
        (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) {
283 284 285 286
        FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
                       head, track, sect, 1,
                       (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
                       drv->max_track, drv->last_sect);
B
bellard 已提交
287 288 289
        return 2;
    }
    if (sect > drv->last_sect) {
290 291 292 293
        FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
                       head, track, sect, 1,
                       (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
                       drv->max_track, drv->last_sect);
B
bellard 已提交
294 295
        return 3;
    }
296
    sector = fd_sector_calc(head, track, sect, drv->last_sect, NUM_SIDES(drv));
297
    ret = 0;
B
bellard 已提交
298 299 300
    if (sector != fd_sector(drv)) {
#if 0
        if (!enable_seek) {
B
Blue Swirl 已提交
301 302 303 304
            FLOPPY_DPRINTF("error: no implicit seek %d %02x %02x"
                           " (max=%d %02x %02x)\n",
                           head, track, sect, 1, drv->max_track,
                           drv->last_sect);
B
bellard 已提交
305 306 307 308
            return 4;
        }
#endif
        drv->head = head;
309
        if (drv->track != track) {
310
            if (drv->blk != NULL && blk_is_inserted(drv->blk)) {
311 312
                drv->media_changed = 0;
            }
313
            ret = 1;
314
        }
B
bellard 已提交
315 316 317 318
        drv->track = track;
        drv->sect = sect;
    }

319
    if (drv->blk == NULL || !blk_is_inserted(drv->blk)) {
320 321 322
        ret = 2;
    }

323
    return ret;
B
bellard 已提交
324 325 326
}

/* Set drive back to track 0 */
B
Blue Swirl 已提交
327
static void fd_recalibrate(FDrive *drv)
B
bellard 已提交
328 329
{
    FLOPPY_DPRINTF("recalibrate\n");
330
    fd_seek(drv, 0, 0, 1, 1);
B
bellard 已提交
331 332
}

J
John Snow 已提交
333 334 335 336 337 338 339
/**
 * Determine geometry based on inserted diskette.
 * Will not operate on an empty drive.
 *
 * @return: 0 on success, -1 if the drive is empty.
 */
static int pick_geometry(FDrive *drv)
J
John Snow 已提交
340
{
341
    BlockBackend *blk = drv->blk;
J
John Snow 已提交
342 343
    const FDFormat *parse;
    uint64_t nb_sectors, size;
J
John Snow 已提交
344 345 346
    int i;
    int match, size_match, type_match;
    bool magic = drv->drive == FLOPPY_DRIVE_TYPE_AUTO;
J
John Snow 已提交
347

J
John Snow 已提交
348
    /* We can only pick a geometry if we have a diskette. */
349 350 351
    if (!drv->blk || !blk_is_inserted(drv->blk) ||
        drv->drive == FLOPPY_DRIVE_TYPE_NONE)
    {
J
John Snow 已提交
352 353 354
        return -1;
    }

J
John Snow 已提交
355 356 357 358 359 360 361 362 363
    /* We need to determine the likely geometry of the inserted medium.
     * In order of preference, we look for:
     * (1) The same drive type and number of sectors,
     * (2) The same diskette size and number of sectors,
     * (3) The same drive type.
     *
     * In all cases, matches that occur higher in the drive table will take
     * precedence over matches that occur later in the table.
     */
J
John Snow 已提交
364
    blk_get_geometry(blk, &nb_sectors);
J
John Snow 已提交
365
    match = size_match = type_match = -1;
J
John Snow 已提交
366 367
    for (i = 0; ; i++) {
        parse = &fd_formats[i];
J
John Snow 已提交
368
        if (parse->drive == FLOPPY_DRIVE_TYPE_NONE) {
J
John Snow 已提交
369 370
            break;
        }
J
John Snow 已提交
371 372 373 374 375 376 377 378 379 380 381
        size = (parse->max_head + 1) * parse->max_track * parse->last_sect;
        if (nb_sectors == size) {
            if (magic || parse->drive == drv->drive) {
                /* (1) perfect match -- nb_sectors and drive type */
                goto out;
            } else if (drive_size(parse->drive) == drive_size(drv->drive)) {
                /* (2) size match -- nb_sectors and physical medium size */
                match = (match == -1) ? i : match;
            } else {
                /* This is suspicious -- Did the user misconfigure? */
                size_match = (size_match == -1) ? i : size_match;
J
John Snow 已提交
382
            }
J
John Snow 已提交
383 384 385 386 387 388 389
        } else if (type_match == -1) {
            if ((parse->drive == drv->drive) ||
                (magic && (parse->drive == get_fallback_drive_type(drv)))) {
                /* (3) type match -- nb_sectors mismatch, but matches the type
                 *     specified explicitly by the user, or matches the fallback
                 *     default type when using the drive autodetect mechanism */
                type_match = i;
J
John Snow 已提交
390 391 392
            }
        }
    }
J
John Snow 已提交
393 394

    /* No exact match found */
J
John Snow 已提交
395
    if (match == -1) {
J
John Snow 已提交
396 397 398 399
        if (size_match != -1) {
            parse = &fd_formats[size_match];
            FLOPPY_DPRINTF("User requested floppy drive type '%s', "
                           "but inserted medium appears to be a "
400
                           "%"PRId64" sector '%s' type\n",
401
                           FloppyDriveType_str(drv->drive),
J
John Snow 已提交
402
                           nb_sectors,
403
                           FloppyDriveType_str(parse->drive));
J
John Snow 已提交
404
        }
405
        assert(type_match != -1 && "misconfigured fd_format");
J
John Snow 已提交
406
        match = type_match;
J
John Snow 已提交
407
    }
J
John Snow 已提交
408 409 410
    parse = &(fd_formats[match]);

 out:
411 412 413 414 415 416 417
    if (parse->max_head == 0) {
        drv->flags &= ~FDISK_DBL_SIDES;
    } else {
        drv->flags |= FDISK_DBL_SIDES;
    }
    drv->max_track = parse->max_track;
    drv->last_sect = parse->last_sect;
J
John Snow 已提交
418
    drv->disk = parse->drive;
419
    drv->media_rate = parse->rate;
J
John Snow 已提交
420 421 422 423 424
    return 0;
}

static void pick_drive_type(FDrive *drv)
{
J
John Snow 已提交
425 426 427 428
    if (drv->drive != FLOPPY_DRIVE_TYPE_AUTO) {
        return;
    }

J
John Snow 已提交
429 430 431
    if (pick_geometry(drv) == 0) {
        drv->drive = drv->disk;
    } else {
J
John Snow 已提交
432
        drv->drive = get_fallback_drive_type(drv);
J
John Snow 已提交
433
    }
J
John Snow 已提交
434 435

    g_assert(drv->drive != FLOPPY_DRIVE_TYPE_AUTO);
J
John Snow 已提交
436 437
}

B
bellard 已提交
438
/* Revalidate a disk drive after a disk change */
B
Blue Swirl 已提交
439
static void fd_revalidate(FDrive *drv)
B
bellard 已提交
440
{
J
John Snow 已提交
441 442
    int rc;

B
bellard 已提交
443
    FLOPPY_DPRINTF("revalidate\n");
444
    if (drv->blk != NULL) {
445
        drv->ro = blk_is_read_only(drv->blk);
446
        if (!blk_is_inserted(drv->blk)) {
P
Pavel Hrdina 已提交
447
            FLOPPY_DPRINTF("No disk in drive\n");
J
John Snow 已提交
448
            drv->disk = FLOPPY_DRIVE_TYPE_NONE;
J
John Snow 已提交
449
            fd_empty_seek_hack(drv);
J
John Snow 已提交
450 451 452 453 454 455 456 457 458 459 460
        } else if (!drv->media_validated) {
            rc = pick_geometry(drv);
            if (rc) {
                FLOPPY_DPRINTF("Could not validate floppy drive media");
            } else {
                drv->media_validated = true;
                FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n",
                               (drv->flags & FDISK_DBL_SIDES) ? 2 : 1,
                               drv->max_track, drv->last_sect,
                               drv->ro ? "ro" : "rw");
            }
461
        }
B
bellard 已提交
462
    } else {
P
Pavel Hrdina 已提交
463
        FLOPPY_DPRINTF("No drive connected\n");
464
        drv->last_sect = 0;
465 466
        drv->max_track = 0;
        drv->flags &= ~FDISK_DBL_SIDES;
J
John Snow 已提交
467 468
        drv->drive = FLOPPY_DRIVE_TYPE_NONE;
        drv->disk = FLOPPY_DRIVE_TYPE_NONE;
B
bellard 已提交
469
    }
470 471
}

472
static void fd_change_cb(void *opaque, bool load, Error **errp)
K
Kevin Wolf 已提交
473 474
{
    FDrive *drive = opaque;
K
Kevin Wolf 已提交
475 476 477 478

    if (!load) {
        blk_set_perm(drive->blk, 0, BLK_PERM_ALL, &error_abort);
    } else {
479 480 481
        if (!blkconf_apply_backend_options(drive->conf,
                                           blk_is_read_only(drive->blk), false,
                                           errp)) {
K
Kevin Wolf 已提交
482 483 484
            return;
        }
    }
K
Kevin Wolf 已提交
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500

    drive->media_changed = 1;
    drive->media_validated = false;
    fd_revalidate(drive);
}

static const BlockDevOps fd_block_ops = {
    .change_media_cb = fd_change_cb,
};


#define TYPE_FLOPPY_DRIVE "floppy"
#define FLOPPY_DRIVE(obj) \
     OBJECT_CHECK(FloppyDrive, (obj), TYPE_FLOPPY_DRIVE)

typedef struct FloppyDrive {
501 502 503 504
    DeviceState     qdev;
    uint32_t        unit;
    BlockConf       conf;
    FloppyDriveType type;
K
Kevin Wolf 已提交
505 506 507 508
} FloppyDrive;

static Property floppy_drive_properties[] = {
    DEFINE_PROP_UINT32("unit", FloppyDrive, unit, -1),
509
    DEFINE_BLOCK_PROPERTIES(FloppyDrive, conf),
510
    DEFINE_PROP_SIGNED("drive-type", FloppyDrive, type,
511 512
                        FLOPPY_DRIVE_TYPE_AUTO, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
K
Kevin Wolf 已提交
513 514 515
    DEFINE_PROP_END_OF_LIST(),
};

M
Mao Zhongyi 已提交
516
static void floppy_drive_realize(DeviceState *qdev, Error **errp)
K
Kevin Wolf 已提交
517 518 519 520
{
    FloppyDrive *dev = FLOPPY_DRIVE(qdev);
    FloppyBus *bus = FLOPPY_BUS(qdev->parent_bus);
    FDrive *drive;
521
    bool read_only;
522
    int ret;
K
Kevin Wolf 已提交
523 524 525 526 527 528 529 530 531 532 533

    if (dev->unit == -1) {
        for (dev->unit = 0; dev->unit < MAX_FD; dev->unit++) {
            drive = get_drv(bus->fdc, dev->unit);
            if (!drive->blk) {
                break;
            }
        }
    }

    if (dev->unit >= MAX_FD) {
M
Mao Zhongyi 已提交
534 535 536
        error_setg(errp, "Can't create floppy unit %d, bus supports "
                   "only %d units", dev->unit, MAX_FD);
        return;
K
Kevin Wolf 已提交
537 538 539 540
    }

    drive = get_drv(bus->fdc, dev->unit);
    if (drive->blk) {
M
Mao Zhongyi 已提交
541 542
        error_setg(errp, "Floppy unit %d is in use", dev->unit);
        return;
543 544 545
    }

    if (!dev->conf.blk) {
K
Kevin Wolf 已提交
546
        /* Anonymous BlockBackend for an empty drive */
K
Kevin Wolf 已提交
547
        dev->conf.blk = blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL);
548 549
        ret = blk_attach_dev(dev->conf.blk, qdev);
        assert(ret == 0);
550 551 552 553 554 555

        /* Don't take write permissions on an empty drive to allow attaching a
         * read-only node later */
        read_only = true;
    } else {
        read_only = !blk_bs(dev->conf.blk) || blk_is_read_only(dev->conf.blk);
K
Kevin Wolf 已提交
556 557
    }

558 559 560 561
    if (!blkconf_blocksizes(&dev->conf, errp)) {
        return;
    }

562 563 564
    if (dev->conf.logical_block_size != 512 ||
        dev->conf.physical_block_size != 512)
    {
M
Mao Zhongyi 已提交
565 566 567
        error_setg(errp, "Physical and logical block size must "
                   "be 512 for floppy");
        return;
568 569 570 571 572 573 574
    }

    /* rerror/werror aren't supported by fdc and therefore not even registered
     * with qdev. So set the defaults manually before they are used in
     * blkconf_apply_backend_options(). */
    dev->conf.rerror = BLOCKDEV_ON_ERROR_AUTO;
    dev->conf.werror = BLOCKDEV_ON_ERROR_AUTO;
K
Kevin Wolf 已提交
575

576
    if (!blkconf_apply_backend_options(&dev->conf, read_only, false, errp)) {
M
Mao Zhongyi 已提交
577
        return;
K
Kevin Wolf 已提交
578
    }
579 580 581 582 583

    /* 'enospc' is the default for -drive, 'report' is what blk_new() gives us
     * for empty drives. */
    if (blk_get_on_error(dev->conf.blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC &&
        blk_get_on_error(dev->conf.blk, 0) != BLOCKDEV_ON_ERROR_REPORT) {
M
Mao Zhongyi 已提交
584 585
        error_setg(errp, "fdc doesn't support drive option werror");
        return;
K
Kevin Wolf 已提交
586
    }
587
    if (blk_get_on_error(dev->conf.blk, 1) != BLOCKDEV_ON_ERROR_REPORT) {
M
Mao Zhongyi 已提交
588 589
        error_setg(errp, "fdc doesn't support drive option rerror");
        return;
590 591
    }

K
Kevin Wolf 已提交
592
    drive->conf = &dev->conf;
593 594 595 596 597 598 599 600 601 602 603
    drive->blk = dev->conf.blk;
    drive->fdctrl = bus->fdc;

    fd_init(drive);
    blk_set_dev_ops(drive->blk, &fd_block_ops, drive);

    /* Keep 'type' qdev property and FDrive->drive in sync */
    drive->drive = dev->type;
    pick_drive_type(drive);
    dev->type = drive->drive;

K
Kevin Wolf 已提交
604 605 606 607 608 609
    fd_revalidate(drive);
}

static void floppy_drive_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *k = DEVICE_CLASS(klass);
M
Mao Zhongyi 已提交
610
    k->realize = floppy_drive_realize;
K
Kevin Wolf 已提交
611 612
    set_bit(DEVICE_CATEGORY_STORAGE, k->categories);
    k->bus_type = TYPE_FLOPPY_BUS;
613
    device_class_set_props(k, floppy_drive_properties);
K
Kevin Wolf 已提交
614 615 616 617 618 619 620 621 622 623
    k->desc = "virtual floppy drive";
}

static const TypeInfo floppy_drive_info = {
    .name = TYPE_FLOPPY_DRIVE,
    .parent = TYPE_DEVICE,
    .instance_size = sizeof(FloppyDrive),
    .class_init = floppy_drive_class_init,
};

B
bellard 已提交
624
/********************************************************/
B
bellard 已提交
625
/* Intel 82078 floppy disk controller emulation          */
B
bellard 已提交
626

B
Blue Swirl 已提交
627
static void fdctrl_reset(FDCtrl *fdctrl, int do_irq);
628
static void fdctrl_to_command_phase(FDCtrl *fdctrl);
B
bellard 已提交
629
static int fdctrl_transfer_handler (void *opaque, int nchan,
A
Anthony Liguori 已提交
630
                                    int dma_pos, int dma_len);
631
static void fdctrl_raise_irq(FDCtrl *fdctrl);
632
static FDrive *get_cur_drv(FDCtrl *fdctrl);
B
Blue Swirl 已提交
633 634 635 636 637 638 639 640 641 642 643 644

static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl);
static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl);
static uint32_t fdctrl_read_dor(FDCtrl *fdctrl);
static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value);
static uint32_t fdctrl_read_tape(FDCtrl *fdctrl);
static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value);
static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl);
static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value);
static uint32_t fdctrl_read_data(FDCtrl *fdctrl);
static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value);
static uint32_t fdctrl_read_dir(FDCtrl *fdctrl);
645
static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value);
B
bellard 已提交
646 647 648 649 650 651 652

enum {
    FD_DIR_WRITE   = 0,
    FD_DIR_READ    = 1,
    FD_DIR_SCANE   = 2,
    FD_DIR_SCANL   = 3,
    FD_DIR_SCANH   = 4,
H
Hervé Poussineau 已提交
653
    FD_DIR_VERIFY  = 5,
B
bellard 已提交
654 655 656
};

enum {
B
blueswir1 已提交
657 658
    FD_STATE_MULTI  = 0x01,	/* multi track flag */
    FD_STATE_FORMAT = 0x02,	/* format flag */
B
bellard 已提交
659 660
};

661
enum {
B
blueswir1 已提交
662 663
    FD_REG_SRA = 0x00,
    FD_REG_SRB = 0x01,
664 665 666 667 668 669
    FD_REG_DOR = 0x02,
    FD_REG_TDR = 0x03,
    FD_REG_MSR = 0x04,
    FD_REG_DSR = 0x04,
    FD_REG_FIFO = 0x05,
    FD_REG_DIR = 0x07,
670
    FD_REG_CCR = 0x07,
671 672 673
};

enum {
674
    FD_CMD_READ_TRACK = 0x02,
675 676
    FD_CMD_SPECIFY = 0x03,
    FD_CMD_SENSE_DRIVE_STATUS = 0x04,
677 678
    FD_CMD_WRITE = 0x05,
    FD_CMD_READ = 0x06,
679 680
    FD_CMD_RECALIBRATE = 0x07,
    FD_CMD_SENSE_INTERRUPT_STATUS = 0x08,
681 682 683 684
    FD_CMD_WRITE_DELETED = 0x09,
    FD_CMD_READ_ID = 0x0a,
    FD_CMD_READ_DELETED = 0x0c,
    FD_CMD_FORMAT_TRACK = 0x0d,
685 686 687
    FD_CMD_DUMPREG = 0x0e,
    FD_CMD_SEEK = 0x0f,
    FD_CMD_VERSION = 0x10,
688
    FD_CMD_SCAN_EQUAL = 0x11,
689 690
    FD_CMD_PERPENDICULAR_MODE = 0x12,
    FD_CMD_CONFIGURE = 0x13,
691 692
    FD_CMD_LOCK = 0x14,
    FD_CMD_VERIFY = 0x16,
693 694
    FD_CMD_POWERDOWN_MODE = 0x17,
    FD_CMD_PART_ID = 0x18,
695 696
    FD_CMD_SCAN_LOW_OR_EQUAL = 0x19,
    FD_CMD_SCAN_HIGH_OR_EQUAL = 0x1d,
697
    FD_CMD_SAVE = 0x2e,
698
    FD_CMD_OPTION = 0x33,
699
    FD_CMD_RESTORE = 0x4e,
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
    FD_CMD_DRIVE_SPECIFICATION_COMMAND = 0x8e,
    FD_CMD_RELATIVE_SEEK_OUT = 0x8f,
    FD_CMD_FORMAT_AND_WRITE = 0xcd,
    FD_CMD_RELATIVE_SEEK_IN = 0xcf,
};

enum {
    FD_CONFIG_PRETRK = 0xff, /* Pre-compensation set to track 0 */
    FD_CONFIG_FIFOTHR = 0x0f, /* FIFO threshold set to 1 byte */
    FD_CONFIG_POLL  = 0x10, /* Poll enabled */
    FD_CONFIG_EFIFO = 0x20, /* FIFO disabled */
    FD_CONFIG_EIS   = 0x40, /* No implied seeks */
};

enum {
P
Pavel Hrdina 已提交
715 716 717
    FD_SR0_DS0      = 0x01,
    FD_SR0_DS1      = 0x02,
    FD_SR0_HEAD     = 0x04,
718 719 720 721 722 723 724
    FD_SR0_EQPMT    = 0x10,
    FD_SR0_SEEK     = 0x20,
    FD_SR0_ABNTERM  = 0x40,
    FD_SR0_INVCMD   = 0x80,
    FD_SR0_RDYCHG   = 0xc0,
};

B
blueswir1 已提交
725
enum {
726
    FD_SR1_MA       = 0x01, /* Missing address mark */
727
    FD_SR1_NW       = 0x02, /* Not writable */
B
blueswir1 已提交
728 729 730 731 732 733 734 735
    FD_SR1_EC       = 0x80, /* End of cylinder */
};

enum {
    FD_SR2_SNS      = 0x04, /* Scan not satisfied */
    FD_SR2_SEH      = 0x08, /* Scan equal hit */
};

B
blueswir1 已提交
736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755
enum {
    FD_SRA_DIR      = 0x01,
    FD_SRA_nWP      = 0x02,
    FD_SRA_nINDX    = 0x04,
    FD_SRA_HDSEL    = 0x08,
    FD_SRA_nTRK0    = 0x10,
    FD_SRA_STEP     = 0x20,
    FD_SRA_nDRV2    = 0x40,
    FD_SRA_INTPEND  = 0x80,
};

enum {
    FD_SRB_MTR0     = 0x01,
    FD_SRB_MTR1     = 0x02,
    FD_SRB_WGATE    = 0x04,
    FD_SRB_RDATA    = 0x08,
    FD_SRB_WDATA    = 0x10,
    FD_SRB_DR0      = 0x20,
};

756
enum {
B
blueswir1 已提交
757 758 759
#if MAX_FD == 4
    FD_DOR_SELMASK  = 0x03,
#else
760
    FD_DOR_SELMASK  = 0x01,
B
blueswir1 已提交
761
#endif
762 763 764 765 766 767 768 769 770
    FD_DOR_nRESET   = 0x04,
    FD_DOR_DMAEN    = 0x08,
    FD_DOR_MOTEN0   = 0x10,
    FD_DOR_MOTEN1   = 0x20,
    FD_DOR_MOTEN2   = 0x40,
    FD_DOR_MOTEN3   = 0x80,
};

enum {
B
blueswir1 已提交
771
#if MAX_FD == 4
772
    FD_TDR_BOOTSEL  = 0x0c,
B
blueswir1 已提交
773 774 775
#else
    FD_TDR_BOOTSEL  = 0x04,
#endif
776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798
};

enum {
    FD_DSR_DRATEMASK= 0x03,
    FD_DSR_PWRDOWN  = 0x40,
    FD_DSR_SWRESET  = 0x80,
};

enum {
    FD_MSR_DRV0BUSY = 0x01,
    FD_MSR_DRV1BUSY = 0x02,
    FD_MSR_DRV2BUSY = 0x04,
    FD_MSR_DRV3BUSY = 0x08,
    FD_MSR_CMDBUSY  = 0x10,
    FD_MSR_NONDMA   = 0x20,
    FD_MSR_DIO      = 0x40,
    FD_MSR_RQM      = 0x80,
};

enum {
    FD_DIR_DSKCHG   = 0x80,
};

K
Kevin Wolf 已提交
799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
/*
 * See chapter 5.0 "Controller phases" of the spec:
 *
 * Command phase:
 * The host writes a command and its parameters into the FIFO. The command
 * phase is completed when all parameters for the command have been supplied,
 * and execution phase is entered.
 *
 * Execution phase:
 * Data transfers, either DMA or non-DMA. For non-DMA transfers, the FIFO
 * contains the payload now, otherwise it's unused. When all bytes of the
 * required data have been transferred, the state is switched to either result
 * phase (if the command produces status bytes) or directly back into the
 * command phase for the next command.
 *
 * Result phase:
 * The host reads out the FIFO, which contains one or more result bytes now.
 */
enum {
    /* Only for migration: reconstruct phase from registers like qemu 2.3 */
    FD_PHASE_RECONSTRUCT    = 0,

    FD_PHASE_COMMAND        = 1,
    FD_PHASE_EXECUTION      = 2,
    FD_PHASE_RESULT         = 3,
};

B
bellard 已提交
826
#define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
827
#define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
B
bellard 已提交
828

B
Blue Swirl 已提交
829
struct FDCtrl {
A
Avi Kivity 已提交
830
    MemoryRegion iomem;
P
pbrook 已提交
831
    qemu_irq irq;
B
bellard 已提交
832
    /* Controller state */
833
    QEMUTimer *result_timer;
834
    int dma_chann;
K
Kevin Wolf 已提交
835
    uint8_t phase;
836
    IsaDma *dma;
837 838 839
    /* Controller's identification */
    uint8_t version;
    /* HW */
B
blueswir1 已提交
840 841
    uint8_t sra;
    uint8_t srb;
B
blueswir1 已提交
842
    uint8_t dor;
J
Juan Quintela 已提交
843
    uint8_t dor_vmstate; /* only used as temp during vmstate */
B
blueswir1 已提交
844
    uint8_t tdr;
B
blueswir1 已提交
845
    uint8_t dsr;
B
blueswir1 已提交
846
    uint8_t msr;
B
bellard 已提交
847
    uint8_t cur_drv;
B
blueswir1 已提交
848 849 850
    uint8_t status0;
    uint8_t status1;
    uint8_t status2;
B
bellard 已提交
851
    /* Command FIFO */
852
    uint8_t *fifo;
J
Juan Quintela 已提交
853
    int32_t fifo_size;
B
bellard 已提交
854 855 856 857
    uint32_t data_pos;
    uint32_t data_len;
    uint8_t data_state;
    uint8_t data_dir;
858
    uint8_t eot; /* last wanted sector */
B
bellard 已提交
859 860 861 862 863 864 865 866
    /* States kept only to be returned back */
    /* precompensation */
    uint8_t precomp_trk;
    uint8_t config;
    uint8_t lock;
    /* Power down config (also with status regB access mode */
    uint8_t pwrd;
    /* Floppy drives */
K
Kevin Wolf 已提交
867
    FloppyBus bus;
J
Juan Quintela 已提交
868
    uint8_t num_floppies;
B
Blue Swirl 已提交
869
    FDrive drives[MAX_FD];
870 871 872 873
    struct {
        BlockBackend *blk;
        FloppyDriveType type;
    } qdev_for_drives[MAX_FD];
874
    int reset_sensei;
875
    uint32_t check_media_rate;
J
John Snow 已提交
876
    FloppyDriveType fallback; /* type=auto failure fallback */
877 878 879
    /* Timers state */
    uint8_t timer0;
    uint8_t timer1;
880
    PortioList portio_list;
881 882
};

J
John Snow 已提交
883 884 885 886 887
static FloppyDriveType get_fallback_drive_type(FDrive *drv)
{
    return drv->fdctrl->fallback;
}

888
#define TYPE_SYSBUS_FDC "base-sysbus-fdc"
H
Hu Tao 已提交
889 890
#define SYSBUS_FDC(obj) OBJECT_CHECK(FDCtrlSysBus, (obj), TYPE_SYSBUS_FDC)

B
Blue Swirl 已提交
891
typedef struct FDCtrlSysBus {
H
Hu Tao 已提交
892 893 894 895
    /*< private >*/
    SysBusDevice parent_obj;
    /*< public >*/

B
Blue Swirl 已提交
896 897
    struct FDCtrl state;
} FDCtrlSysBus;
G
Gerd Hoffmann 已提交
898

899 900
#define ISA_FDC(obj) OBJECT_CHECK(FDCtrlISABus, (obj), TYPE_ISA_FDC)

B
Blue Swirl 已提交
901
typedef struct FDCtrlISABus {
902 903
    ISADevice parent_obj;

904 905 906
    uint32_t iobase;
    uint32_t irq;
    uint32_t dma;
B
Blue Swirl 已提交
907
    struct FDCtrl state;
908 909
    int32_t bootindexA;
    int32_t bootindexB;
B
Blue Swirl 已提交
910
} FDCtrlISABus;
G
Gerd Hoffmann 已提交
911

912 913
static uint32_t fdctrl_read (void *opaque, uint32_t reg)
{
B
Blue Swirl 已提交
914
    FDCtrl *fdctrl = opaque;
915 916
    uint32_t retval;

K
Kevin Wolf 已提交
917
    reg &= 7;
B
blueswir1 已提交
918
    switch (reg) {
B
blueswir1 已提交
919 920
    case FD_REG_SRA:
        retval = fdctrl_read_statusA(fdctrl);
921
        break;
B
blueswir1 已提交
922
    case FD_REG_SRB:
923 924
        retval = fdctrl_read_statusB(fdctrl);
        break;
925
    case FD_REG_DOR:
926 927
        retval = fdctrl_read_dor(fdctrl);
        break;
928
    case FD_REG_TDR:
929
        retval = fdctrl_read_tape(fdctrl);
930
        break;
931
    case FD_REG_MSR:
932
        retval = fdctrl_read_main_status(fdctrl);
933
        break;
934
    case FD_REG_FIFO:
935
        retval = fdctrl_read_data(fdctrl);
936
        break;
937
    case FD_REG_DIR:
938
        retval = fdctrl_read_dir(fdctrl);
939
        break;
940
    default:
941 942
        retval = (uint32_t)(-1);
        break;
943
    }
944
    trace_fdc_ioport_read(reg, retval);
945 946 947 948 949 950

    return retval;
}

static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value)
{
B
Blue Swirl 已提交
951
    FDCtrl *fdctrl = opaque;
952

K
Kevin Wolf 已提交
953
    reg &= 7;
954
    trace_fdc_ioport_write(reg, value);
B
blueswir1 已提交
955
    switch (reg) {
956
    case FD_REG_DOR:
957 958
        fdctrl_write_dor(fdctrl, value);
        break;
959
    case FD_REG_TDR:
960
        fdctrl_write_tape(fdctrl, value);
961
        break;
962
    case FD_REG_DSR:
963
        fdctrl_write_rate(fdctrl, value);
964
        break;
965
    case FD_REG_FIFO:
966
        fdctrl_write_data(fdctrl, value);
967
        break;
968 969 970
    case FD_REG_CCR:
        fdctrl_write_ccr(fdctrl, value);
        break;
971
    default:
972
        break;
973
    }
974 975
}

A
Avi Kivity 已提交
976
static uint64_t fdctrl_read_mem (void *opaque, hwaddr reg,
A
Avi Kivity 已提交
977
                                 unsigned ize)
B
bellard 已提交
978
{
979
    return fdctrl_read(opaque, (uint32_t)reg);
B
bellard 已提交
980 981
}

A
Avi Kivity 已提交
982
static void fdctrl_write_mem (void *opaque, hwaddr reg,
A
Avi Kivity 已提交
983
                              uint64_t value, unsigned size)
B
bellard 已提交
984
{
985
    fdctrl_write(opaque, (uint32_t)reg, value);
B
bellard 已提交
986 987
}

A
Avi Kivity 已提交
988 989 990 991
static const MemoryRegionOps fdctrl_mem_ops = {
    .read = fdctrl_read_mem,
    .write = fdctrl_write_mem,
    .endianness = DEVICE_NATIVE_ENDIAN,
B
bellard 已提交
992 993
};

A
Avi Kivity 已提交
994 995 996 997 998 999 1000 1001
static const MemoryRegionOps fdctrl_mem_strict_ops = {
    .read = fdctrl_read_mem,
    .write = fdctrl_write_mem,
    .endianness = DEVICE_NATIVE_ENDIAN,
    .valid = {
        .min_access_size = 1,
        .max_access_size = 1,
    },
1002 1003
};

1004 1005 1006 1007
static bool fdrive_media_changed_needed(void *opaque)
{
    FDrive *drive = opaque;

1008
    return (drive->blk != NULL && drive->media_changed != 1);
1009 1010 1011 1012 1013 1014
}

static const VMStateDescription vmstate_fdrive_media_changed = {
    .name = "fdrive/media_changed",
    .version_id = 1,
    .minimum_version_id = 1,
1015
    .needed = fdrive_media_changed_needed,
1016
    .fields = (VMStateField[]) {
1017 1018 1019 1020 1021
        VMSTATE_UINT8(media_changed, FDrive),
        VMSTATE_END_OF_LIST()
    }
};

1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
static bool fdrive_media_rate_needed(void *opaque)
{
    FDrive *drive = opaque;

    return drive->fdctrl->check_media_rate;
}

static const VMStateDescription vmstate_fdrive_media_rate = {
    .name = "fdrive/media_rate",
    .version_id = 1,
    .minimum_version_id = 1,
1033
    .needed = fdrive_media_rate_needed,
1034
    .fields = (VMStateField[]) {
1035 1036 1037 1038 1039
        VMSTATE_UINT8(media_rate, FDrive),
        VMSTATE_END_OF_LIST()
    }
};

1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
static bool fdrive_perpendicular_needed(void *opaque)
{
    FDrive *drive = opaque;

    return drive->perpendicular != 0;
}

static const VMStateDescription vmstate_fdrive_perpendicular = {
    .name = "fdrive/perpendicular",
    .version_id = 1,
    .minimum_version_id = 1,
1051
    .needed = fdrive_perpendicular_needed,
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063
    .fields = (VMStateField[]) {
        VMSTATE_UINT8(perpendicular, FDrive),
        VMSTATE_END_OF_LIST()
    }
};

static int fdrive_post_load(void *opaque, int version_id)
{
    fd_revalidate(opaque);
    return 0;
}

J
Juan Quintela 已提交
1064 1065 1066 1067
static const VMStateDescription vmstate_fdrive = {
    .name = "fdrive",
    .version_id = 1,
    .minimum_version_id = 1,
1068
    .post_load = fdrive_post_load,
1069
    .fields = (VMStateField[]) {
B
Blue Swirl 已提交
1070 1071 1072
        VMSTATE_UINT8(head, FDrive),
        VMSTATE_UINT8(track, FDrive),
        VMSTATE_UINT8(sect, FDrive),
J
Juan Quintela 已提交
1073
        VMSTATE_END_OF_LIST()
1074
    },
1075 1076 1077 1078 1079
    .subsections = (const VMStateDescription*[]) {
        &vmstate_fdrive_media_changed,
        &vmstate_fdrive_media_rate,
        &vmstate_fdrive_perpendicular,
        NULL
J
Juan Quintela 已提交
1080 1081
    }
};
1082

K
Kevin Wolf 已提交
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104
/*
 * Reconstructs the phase from register values according to the logic that was
 * implemented in qemu 2.3. This is the default value that is used if the phase
 * subsection is not present on migration.
 *
 * Don't change this function to reflect newer qemu versions, it is part of
 * the migration ABI.
 */
static int reconstruct_phase(FDCtrl *fdctrl)
{
    if (fdctrl->msr & FD_MSR_NONDMA) {
        return FD_PHASE_EXECUTION;
    } else if ((fdctrl->msr & FD_MSR_RQM) == 0) {
        /* qemu 2.3 disabled RQM only during DMA transfers */
        return FD_PHASE_EXECUTION;
    } else if (fdctrl->msr & FD_MSR_DIO) {
        return FD_PHASE_RESULT;
    } else {
        return FD_PHASE_COMMAND;
    }
}

1105
static int fdc_pre_save(void *opaque)
1106
{
B
Blue Swirl 已提交
1107
    FDCtrl *s = opaque;
1108

J
Juan Quintela 已提交
1109
    s->dor_vmstate = s->dor | GET_CUR_DRV(s);
1110 1111

    return 0;
1112 1113
}

K
Kevin Wolf 已提交
1114 1115 1116 1117 1118 1119 1120
static int fdc_pre_load(void *opaque)
{
    FDCtrl *s = opaque;
    s->phase = FD_PHASE_RECONSTRUCT;
    return 0;
}

1121
static int fdc_post_load(void *opaque, int version_id)
1122
{
B
Blue Swirl 已提交
1123
    FDCtrl *s = opaque;
1124

J
Juan Quintela 已提交
1125 1126
    SET_CUR_DRV(s, s->dor_vmstate & FD_DOR_SELMASK);
    s->dor = s->dor_vmstate & ~FD_DOR_SELMASK;
K
Kevin Wolf 已提交
1127 1128 1129 1130 1131

    if (s->phase == FD_PHASE_RECONSTRUCT) {
        s->phase = reconstruct_phase(s);
    }

1132 1133 1134
    return 0;
}

1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145
static bool fdc_reset_sensei_needed(void *opaque)
{
    FDCtrl *s = opaque;

    return s->reset_sensei != 0;
}

static const VMStateDescription vmstate_fdc_reset_sensei = {
    .name = "fdc/reset_sensei",
    .version_id = 1,
    .minimum_version_id = 1,
1146
    .needed = fdc_reset_sensei_needed,
1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163
    .fields = (VMStateField[]) {
        VMSTATE_INT32(reset_sensei, FDCtrl),
        VMSTATE_END_OF_LIST()
    }
};

static bool fdc_result_timer_needed(void *opaque)
{
    FDCtrl *s = opaque;

    return timer_pending(s->result_timer);
}

static const VMStateDescription vmstate_fdc_result_timer = {
    .name = "fdc/result_timer",
    .version_id = 1,
    .minimum_version_id = 1,
1164
    .needed = fdc_result_timer_needed,
1165
    .fields = (VMStateField[]) {
1166
        VMSTATE_TIMER_PTR(result_timer, FDCtrl),
1167 1168 1169 1170
        VMSTATE_END_OF_LIST()
    }
};

K
Kevin Wolf 已提交
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
static bool fdc_phase_needed(void *opaque)
{
    FDCtrl *fdctrl = opaque;

    return reconstruct_phase(fdctrl) != fdctrl->phase;
}

static const VMStateDescription vmstate_fdc_phase = {
    .name = "fdc/phase",
    .version_id = 1,
    .minimum_version_id = 1,
1182
    .needed = fdc_phase_needed,
K
Kevin Wolf 已提交
1183 1184 1185 1186 1187 1188
    .fields = (VMStateField[]) {
        VMSTATE_UINT8(phase, FDCtrl),
        VMSTATE_END_OF_LIST()
    }
};

J
Juan Quintela 已提交
1189
static const VMStateDescription vmstate_fdc = {
1190
    .name = "fdc",
J
Juan Quintela 已提交
1191 1192 1193
    .version_id = 2,
    .minimum_version_id = 2,
    .pre_save = fdc_pre_save,
K
Kevin Wolf 已提交
1194
    .pre_load = fdc_pre_load,
J
Juan Quintela 已提交
1195
    .post_load = fdc_post_load,
1196
    .fields = (VMStateField[]) {
J
Juan Quintela 已提交
1197
        /* Controller State */
B
Blue Swirl 已提交
1198 1199 1200 1201 1202 1203 1204 1205 1206
        VMSTATE_UINT8(sra, FDCtrl),
        VMSTATE_UINT8(srb, FDCtrl),
        VMSTATE_UINT8(dor_vmstate, FDCtrl),
        VMSTATE_UINT8(tdr, FDCtrl),
        VMSTATE_UINT8(dsr, FDCtrl),
        VMSTATE_UINT8(msr, FDCtrl),
        VMSTATE_UINT8(status0, FDCtrl),
        VMSTATE_UINT8(status1, FDCtrl),
        VMSTATE_UINT8(status2, FDCtrl),
J
Juan Quintela 已提交
1207
        /* Command FIFO */
B
Blue Swirl 已提交
1208 1209
        VMSTATE_VARRAY_INT32(fifo, FDCtrl, fifo_size, 0, vmstate_info_uint8,
                             uint8_t),
B
Blue Swirl 已提交
1210 1211 1212 1213 1214
        VMSTATE_UINT32(data_pos, FDCtrl),
        VMSTATE_UINT32(data_len, FDCtrl),
        VMSTATE_UINT8(data_state, FDCtrl),
        VMSTATE_UINT8(data_dir, FDCtrl),
        VMSTATE_UINT8(eot, FDCtrl),
J
Juan Quintela 已提交
1215
        /* States kept only to be returned back */
B
Blue Swirl 已提交
1216 1217 1218 1219 1220 1221
        VMSTATE_UINT8(timer0, FDCtrl),
        VMSTATE_UINT8(timer1, FDCtrl),
        VMSTATE_UINT8(precomp_trk, FDCtrl),
        VMSTATE_UINT8(config, FDCtrl),
        VMSTATE_UINT8(lock, FDCtrl),
        VMSTATE_UINT8(pwrd, FDCtrl),
1222
        VMSTATE_UINT8_EQUAL(num_floppies, FDCtrl, NULL),
B
Blue Swirl 已提交
1223 1224
        VMSTATE_STRUCT_ARRAY(drives, FDCtrl, MAX_FD, 1,
                             vmstate_fdrive, FDrive),
J
Juan Quintela 已提交
1225
        VMSTATE_END_OF_LIST()
1226
    },
1227 1228 1229 1230 1231
    .subsections = (const VMStateDescription*[]) {
        &vmstate_fdc_reset_sensei,
        &vmstate_fdc_result_timer,
        &vmstate_fdc_phase,
        NULL
B
blueswir1 已提交
1232
    }
J
Juan Quintela 已提交
1233
};
1234

B
Blue Swirl 已提交
1235
static void fdctrl_external_reset_sysbus(DeviceState *d)
1236
{
H
Hu Tao 已提交
1237
    FDCtrlSysBus *sys = SYSBUS_FDC(d);
B
Blue Swirl 已提交
1238
    FDCtrl *s = &sys->state;
B
Blue Swirl 已提交
1239 1240 1241 1242 1243 1244

    fdctrl_reset(s, 0);
}

static void fdctrl_external_reset_isa(DeviceState *d)
{
1245
    FDCtrlISABus *isa = ISA_FDC(d);
B
Blue Swirl 已提交
1246
    FDCtrl *s = &isa->state;
1247 1248 1249 1250

    fdctrl_reset(s, 0);
}

B
blueswir1 已提交
1251 1252
static void fdctrl_handle_tc(void *opaque, int irq, int level)
{
B
Blue Swirl 已提交
1253
    //FDCtrl *s = opaque;
B
blueswir1 已提交
1254 1255 1256 1257 1258 1259 1260

    if (level) {
        // XXX
        FLOPPY_DPRINTF("TC pulsed\n");
    }
}

B
bellard 已提交
1261
/* Change IRQ state */
B
Blue Swirl 已提交
1262
static void fdctrl_reset_irq(FDCtrl *fdctrl)
B
bellard 已提交
1263
{
1264
    fdctrl->status0 = 0;
B
blueswir1 已提交
1265 1266
    if (!(fdctrl->sra & FD_SRA_INTPEND))
        return;
1267
    FLOPPY_DPRINTF("Reset interrupt\n");
P
pbrook 已提交
1268
    qemu_set_irq(fdctrl->irq, 0);
B
blueswir1 已提交
1269
    fdctrl->sra &= ~FD_SRA_INTPEND;
B
bellard 已提交
1270 1271
}

1272
static void fdctrl_raise_irq(FDCtrl *fdctrl)
B
bellard 已提交
1273
{
B
blueswir1 已提交
1274
    if (!(fdctrl->sra & FD_SRA_INTPEND)) {
P
pbrook 已提交
1275
        qemu_set_irq(fdctrl->irq, 1);
B
blueswir1 已提交
1276
        fdctrl->sra |= FD_SRA_INTPEND;
B
bellard 已提交
1277
    }
1278

1279
    fdctrl->reset_sensei = 0;
B
blueswir1 已提交
1280
    FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl->status0);
B
bellard 已提交
1281 1282
}

B
bellard 已提交
1283
/* Reset controller */
B
Blue Swirl 已提交
1284
static void fdctrl_reset(FDCtrl *fdctrl, int do_irq)
B
bellard 已提交
1285 1286 1287
{
    int i;

B
bellard 已提交
1288
    FLOPPY_DPRINTF("reset controller\n");
1289
    fdctrl_reset_irq(fdctrl);
B
bellard 已提交
1290
    /* Initialise controller */
B
blueswir1 已提交
1291 1292
    fdctrl->sra = 0;
    fdctrl->srb = 0xc0;
1293
    if (!fdctrl->drives[1].blk) {
B
blueswir1 已提交
1294
        fdctrl->sra |= FD_SRA_nDRV2;
1295
    }
1296
    fdctrl->cur_drv = 0;
B
blueswir1 已提交
1297
    fdctrl->dor = FD_DOR_nRESET;
B
blueswir1 已提交
1298
    fdctrl->dor |= (fdctrl->dma_chann != -1) ? FD_DOR_DMAEN : 0;
B
blueswir1 已提交
1299
    fdctrl->msr = FD_MSR_RQM;
1300 1301
    fdctrl->reset_sensei = 0;
    timer_del(fdctrl->result_timer);
B
bellard 已提交
1302
    /* FIFO state */
1303 1304
    fdctrl->data_pos = 0;
    fdctrl->data_len = 0;
B
blueswir1 已提交
1305
    fdctrl->data_state = 0;
1306
    fdctrl->data_dir = FD_DIR_WRITE;
B
bellard 已提交
1307
    for (i = 0; i < MAX_FD; i++)
B
blueswir1 已提交
1308
        fd_recalibrate(&fdctrl->drives[i]);
1309
    fdctrl_to_command_phase(fdctrl);
B
blueswir1 已提交
1310
    if (do_irq) {
1311 1312
        fdctrl->status0 |= FD_SR0_RDYCHG;
        fdctrl_raise_irq(fdctrl);
1313
        fdctrl->reset_sensei = FD_RESET_SENSEI_COUNT;
B
blueswir1 已提交
1314
    }
1315 1316
}

B
Blue Swirl 已提交
1317
static inline FDrive *drv0(FDCtrl *fdctrl)
1318
{
B
blueswir1 已提交
1319
    return &fdctrl->drives[(fdctrl->tdr & FD_TDR_BOOTSEL) >> 2];
1320 1321
}

B
Blue Swirl 已提交
1322
static inline FDrive *drv1(FDCtrl *fdctrl)
1323
{
B
blueswir1 已提交
1324 1325 1326 1327
    if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (1 << 2))
        return &fdctrl->drives[1];
    else
        return &fdctrl->drives[0];
1328 1329
}

B
blueswir1 已提交
1330
#if MAX_FD == 4
B
Blue Swirl 已提交
1331
static inline FDrive *drv2(FDCtrl *fdctrl)
B
blueswir1 已提交
1332 1333 1334 1335 1336 1337 1338
{
    if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (2 << 2))
        return &fdctrl->drives[2];
    else
        return &fdctrl->drives[1];
}

B
Blue Swirl 已提交
1339
static inline FDrive *drv3(FDCtrl *fdctrl)
B
blueswir1 已提交
1340 1341 1342 1343 1344 1345 1346 1347
{
    if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (3 << 2))
        return &fdctrl->drives[3];
    else
        return &fdctrl->drives[2];
}
#endif

K
Kevin Wolf 已提交
1348
static FDrive *get_drv(FDCtrl *fdctrl, int unit)
1349
{
K
Kevin Wolf 已提交
1350
    switch (unit) {
B
blueswir1 已提交
1351 1352 1353 1354 1355 1356 1357 1358
        case 0: return drv0(fdctrl);
        case 1: return drv1(fdctrl);
#if MAX_FD == 4
        case 2: return drv2(fdctrl);
        case 3: return drv3(fdctrl);
#endif
        default: return NULL;
    }
B
bellard 已提交
1359 1360
}

K
Kevin Wolf 已提交
1361 1362 1363 1364 1365
static FDrive *get_cur_drv(FDCtrl *fdctrl)
{
    return get_drv(fdctrl, fdctrl->cur_drv);
}

B
blueswir1 已提交
1366
/* Status A register : 0x00 (read-only) */
B
Blue Swirl 已提交
1367
static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl)
B
blueswir1 已提交
1368 1369 1370 1371 1372 1373 1374 1375
{
    uint32_t retval = fdctrl->sra;

    FLOPPY_DPRINTF("status register A: 0x%02x\n", retval);

    return retval;
}

B
bellard 已提交
1376
/* Status B register : 0x01 (read-only) */
B
Blue Swirl 已提交
1377
static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl)
B
bellard 已提交
1378
{
B
blueswir1 已提交
1379 1380 1381 1382 1383
    uint32_t retval = fdctrl->srb;

    FLOPPY_DPRINTF("status register B: 0x%02x\n", retval);

    return retval;
B
bellard 已提交
1384 1385 1386
}

/* Digital output register : 0x02 */
B
Blue Swirl 已提交
1387
static uint32_t fdctrl_read_dor(FDCtrl *fdctrl)
B
bellard 已提交
1388
{
B
blueswir1 已提交
1389
    uint32_t retval = fdctrl->dor;
B
bellard 已提交
1390 1391

    /* Selected drive */
1392
    retval |= fdctrl->cur_drv;
B
bellard 已提交
1393 1394 1395 1396 1397
    FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);

    return retval;
}

B
Blue Swirl 已提交
1398
static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value)
B
bellard 已提交
1399 1400
{
    FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
B
blueswir1 已提交
1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417

    /* Motors */
    if (value & FD_DOR_MOTEN0)
        fdctrl->srb |= FD_SRB_MTR0;
    else
        fdctrl->srb &= ~FD_SRB_MTR0;
    if (value & FD_DOR_MOTEN1)
        fdctrl->srb |= FD_SRB_MTR1;
    else
        fdctrl->srb &= ~FD_SRB_MTR1;

    /* Drive */
    if (value & 1)
        fdctrl->srb |= FD_SRB_DR0;
    else
        fdctrl->srb &= ~FD_SRB_DR0;

B
bellard 已提交
1418
    /* Reset */
1419
    if (!(value & FD_DOR_nRESET)) {
B
blueswir1 已提交
1420
        if (fdctrl->dor & FD_DOR_nRESET) {
B
bellard 已提交
1421
            FLOPPY_DPRINTF("controller enter RESET state\n");
B
bellard 已提交
1422 1423
        }
    } else {
B
blueswir1 已提交
1424
        if (!(fdctrl->dor & FD_DOR_nRESET)) {
B
bellard 已提交
1425
            FLOPPY_DPRINTF("controller out of RESET state\n");
1426
            fdctrl_reset(fdctrl, 1);
B
blueswir1 已提交
1427
            fdctrl->dsr &= ~FD_DSR_PWRDOWN;
B
bellard 已提交
1428 1429 1430
        }
    }
    /* Selected drive */
1431
    fdctrl->cur_drv = value & FD_DOR_SELMASK;
B
blueswir1 已提交
1432 1433

    fdctrl->dor = value;
B
bellard 已提交
1434 1435 1436
}

/* Tape drive register : 0x03 */
B
Blue Swirl 已提交
1437
static uint32_t fdctrl_read_tape(FDCtrl *fdctrl)
B
bellard 已提交
1438
{
B
blueswir1 已提交
1439
    uint32_t retval = fdctrl->tdr;
B
bellard 已提交
1440 1441 1442 1443 1444 1445

    FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);

    return retval;
}

B
Blue Swirl 已提交
1446
static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value)
B
bellard 已提交
1447 1448
{
    /* Reset mode */
B
blueswir1 已提交
1449
    if (!(fdctrl->dor & FD_DOR_nRESET)) {
B
bellard 已提交
1450
        FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
B
bellard 已提交
1451 1452 1453 1454
        return;
    }
    FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
    /* Disk boot selection indicator */
B
blueswir1 已提交
1455
    fdctrl->tdr = value & FD_TDR_BOOTSEL;
B
bellard 已提交
1456 1457 1458 1459
    /* Tape indicators: never allow */
}

/* Main status register : 0x04 (read) */
B
Blue Swirl 已提交
1460
static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl)
B
bellard 已提交
1461
{
B
blueswir1 已提交
1462
    uint32_t retval = fdctrl->msr;
B
bellard 已提交
1463

B
blueswir1 已提交
1464
    fdctrl->dsr &= ~FD_DSR_PWRDOWN;
B
blueswir1 已提交
1465
    fdctrl->dor |= FD_DOR_nRESET;
B
blueswir1 已提交
1466

B
bellard 已提交
1467 1468 1469 1470 1471 1472
    FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);

    return retval;
}

/* Data select rate register : 0x04 (write) */
B
Blue Swirl 已提交
1473
static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value)
B
bellard 已提交
1474 1475
{
    /* Reset mode */
B
blueswir1 已提交
1476
    if (!(fdctrl->dor & FD_DOR_nRESET)) {
1477 1478 1479
        FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
        return;
    }
B
bellard 已提交
1480 1481
    FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
    /* Reset: autoclear */
1482
    if (value & FD_DSR_SWRESET) {
B
blueswir1 已提交
1483
        fdctrl->dor &= ~FD_DOR_nRESET;
1484
        fdctrl_reset(fdctrl, 1);
B
blueswir1 已提交
1485
        fdctrl->dor |= FD_DOR_nRESET;
B
bellard 已提交
1486
    }
1487
    if (value & FD_DSR_PWRDOWN) {
1488
        fdctrl_reset(fdctrl, 1);
B
bellard 已提交
1489
    }
B
blueswir1 已提交
1490
    fdctrl->dsr = value;
B
bellard 已提交
1491 1492
}

1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509
/* Configuration control register: 0x07 (write) */
static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value)
{
    /* Reset mode */
    if (!(fdctrl->dor & FD_DOR_nRESET)) {
        FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
        return;
    }
    FLOPPY_DPRINTF("configuration control register set to 0x%02x\n", value);

    /* Only the rate selection bits used in AT mode, and we
     * store those in the DSR.
     */
    fdctrl->dsr = (fdctrl->dsr & ~FD_DSR_DRATEMASK) |
                  (value & FD_DSR_DRATEMASK);
}

B
Blue Swirl 已提交
1510
static int fdctrl_media_changed(FDrive *drv)
B
bellard 已提交
1511
{
1512
    return drv->media_changed;
B
bellard 已提交
1513 1514
}

B
bellard 已提交
1515
/* Digital input register : 0x07 (read-only) */
B
Blue Swirl 已提交
1516
static uint32_t fdctrl_read_dir(FDCtrl *fdctrl)
B
bellard 已提交
1517 1518 1519
{
    uint32_t retval = 0;

1520
    if (fdctrl_media_changed(get_cur_drv(fdctrl))) {
1521
        retval |= FD_DIR_DSKCHG;
1522
    }
1523
    if (retval != 0) {
1524
        FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
1525
    }
B
bellard 已提交
1526 1527 1528 1529

    return retval;
}

1530 1531
/* Clear the FIFO and update the state for receiving the next command */
static void fdctrl_to_command_phase(FDCtrl *fdctrl)
B
bellard 已提交
1532
{
K
Kevin Wolf 已提交
1533
    fdctrl->phase = FD_PHASE_COMMAND;
1534 1535
    fdctrl->data_dir = FD_DIR_WRITE;
    fdctrl->data_pos = 0;
K
Kevin Wolf 已提交
1536
    fdctrl->data_len = 1; /* Accept command byte, adjust for params later */
B
blueswir1 已提交
1537
    fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO);
K
Kevin Wolf 已提交
1538
    fdctrl->msr |= FD_MSR_RQM;
B
bellard 已提交
1539 1540
}

1541 1542 1543
/* Update the state to allow the guest to read out the command status.
 * @fifo_len is the number of result bytes to be read out. */
static void fdctrl_to_result_phase(FDCtrl *fdctrl, int fifo_len)
B
bellard 已提交
1544
{
K
Kevin Wolf 已提交
1545
    fdctrl->phase = FD_PHASE_RESULT;
1546 1547 1548
    fdctrl->data_dir = FD_DIR_READ;
    fdctrl->data_len = fifo_len;
    fdctrl->data_pos = 0;
B
blueswir1 已提交
1549
    fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO;
B
bellard 已提交
1550 1551 1552
}

/* Set an error: unimplemented/unknown command */
B
Blue Swirl 已提交
1553
static void fdctrl_unimplemented(FDCtrl *fdctrl, int direction)
B
bellard 已提交
1554
{
B
Blue Swirl 已提交
1555 1556
    qemu_log_mask(LOG_UNIMP, "fdc: unimplemented command 0x%02x\n",
                  fdctrl->fifo[0]);
1557
    fdctrl->fifo[0] = FD_SR0_INVCMD;
1558
    fdctrl_to_result_phase(fdctrl, 1);
B
bellard 已提交
1559 1560
}

1561 1562 1563 1564
/* Seek to next sector
 * returns 0 when end of track reached (for DBL_SIDES on head 1)
 * otherwise returns 1
 */
B
Blue Swirl 已提交
1565
static int fdctrl_seek_to_next_sect(FDCtrl *fdctrl, FDrive *cur_drv)
B
blueswir1 已提交
1566 1567 1568 1569 1570 1571
{
    FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n",
                   cur_drv->head, cur_drv->track, cur_drv->sect,
                   fd_sector(cur_drv));
    /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
       error in fact */
1572 1573 1574 1575 1576 1577 1578 1579 1580
    uint8_t new_head = cur_drv->head;
    uint8_t new_track = cur_drv->track;
    uint8_t new_sect = cur_drv->sect;

    int ret = 1;

    if (new_sect >= cur_drv->last_sect ||
        new_sect == fdctrl->eot) {
        new_sect = 1;
B
blueswir1 已提交
1581
        if (FD_MULTI_TRACK(fdctrl->data_state)) {
1582
            if (new_head == 0 &&
B
blueswir1 已提交
1583
                (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
1584
                new_head = 1;
B
blueswir1 已提交
1585
            } else {
1586 1587
                new_head = 0;
                new_track++;
1588
                fdctrl->status0 |= FD_SR0_SEEK;
1589 1590 1591
                if ((cur_drv->flags & FDISK_DBL_SIDES) == 0) {
                    ret = 0;
                }
B
blueswir1 已提交
1592 1593
            }
        } else {
1594
            fdctrl->status0 |= FD_SR0_SEEK;
1595 1596 1597 1598 1599 1600
            new_track++;
            ret = 0;
        }
        if (ret == 1) {
            FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
                    new_head, new_track, new_sect, fd_sector(cur_drv));
B
blueswir1 已提交
1601 1602
        }
    } else {
1603
        new_sect++;
B
blueswir1 已提交
1604
    }
1605 1606
    fd_seek(cur_drv, new_head, new_track, new_sect, 1);
    return ret;
B
blueswir1 已提交
1607 1608
}

B
bellard 已提交
1609
/* Callback for transfer end (stop or abort) */
B
Blue Swirl 已提交
1610 1611
static void fdctrl_stop_transfer(FDCtrl *fdctrl, uint8_t status0,
                                 uint8_t status1, uint8_t status2)
B
bellard 已提交
1612
{
B
Blue Swirl 已提交
1613
    FDrive *cur_drv;
1614
    cur_drv = get_cur_drv(fdctrl);
H
Hervé Poussineau 已提交
1615 1616 1617 1618 1619 1620 1621

    fdctrl->status0 &= ~(FD_SR0_DS0 | FD_SR0_DS1 | FD_SR0_HEAD);
    fdctrl->status0 |= GET_CUR_DRV(fdctrl);
    if (cur_drv->head) {
        fdctrl->status0 |= FD_SR0_HEAD;
    }
    fdctrl->status0 |= status0;
P
Pavel Hrdina 已提交
1622

B
bellard 已提交
1623
    FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
P
Pavel Hrdina 已提交
1624 1625
                   status0, status1, status2, fdctrl->status0);
    fdctrl->fifo[0] = fdctrl->status0;
1626 1627 1628 1629 1630 1631 1632
    fdctrl->fifo[1] = status1;
    fdctrl->fifo[2] = status2;
    fdctrl->fifo[3] = cur_drv->track;
    fdctrl->fifo[4] = cur_drv->head;
    fdctrl->fifo[5] = cur_drv->sect;
    fdctrl->fifo[6] = FD_SECTOR_SC;
    fdctrl->data_dir = FD_DIR_READ;
1633
    if (fdctrl->dma_chann != -1 && !(fdctrl->msr & FD_MSR_NONDMA)) {
1634 1635
        IsaDmaClass *k = ISADMA_GET_CLASS(fdctrl->dma);
        k->release_DREQ(fdctrl->dma, fdctrl->dma_chann);
1636
    }
B
blueswir1 已提交
1637
    fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO;
B
blueswir1 已提交
1638
    fdctrl->msr &= ~FD_MSR_NONDMA;
1639

1640
    fdctrl_to_result_phase(fdctrl, 7);
1641
    fdctrl_raise_irq(fdctrl);
B
bellard 已提交
1642 1643 1644
}

/* Prepare a data transfer (either DMA or FIFO) */
B
Blue Swirl 已提交
1645
static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction)
B
bellard 已提交
1646
{
B
Blue Swirl 已提交
1647
    FDrive *cur_drv;
B
bellard 已提交
1648 1649
    uint8_t kh, kt, ks;

B
blueswir1 已提交
1650
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1651 1652 1653 1654
    cur_drv = get_cur_drv(fdctrl);
    kt = fdctrl->fifo[2];
    kh = fdctrl->fifo[3];
    ks = fdctrl->fifo[4];
B
bellard 已提交
1655
    FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
B
blueswir1 已提交
1656
                   GET_CUR_DRV(fdctrl), kh, kt, ks,
1657 1658
                   fd_sector_calc(kh, kt, ks, cur_drv->last_sect,
                                  NUM_SIDES(cur_drv)));
B
blueswir1 已提交
1659
    switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
B
bellard 已提交
1660 1661
    case 2:
        /* sect too big */
1662
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1663 1664 1665
        fdctrl->fifo[3] = kt;
        fdctrl->fifo[4] = kh;
        fdctrl->fifo[5] = ks;
B
bellard 已提交
1666 1667 1668
        return;
    case 3:
        /* track too big */
B
blueswir1 已提交
1669
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
1670 1671 1672
        fdctrl->fifo[3] = kt;
        fdctrl->fifo[4] = kh;
        fdctrl->fifo[5] = ks;
B
bellard 已提交
1673 1674 1675
        return;
    case 4:
        /* No seek enabled */
1676
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1677 1678 1679
        fdctrl->fifo[3] = kt;
        fdctrl->fifo[4] = kh;
        fdctrl->fifo[5] = ks;
B
bellard 已提交
1680 1681
        return;
    case 1:
1682
        fdctrl->status0 |= FD_SR0_SEEK;
B
bellard 已提交
1683 1684 1685 1686
        break;
    default:
        break;
    }
B
blueswir1 已提交
1687

1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700
    /* Check the data rate. If the programmed data rate does not match
     * the currently inserted medium, the operation has to fail. */
    if (fdctrl->check_media_rate &&
        (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) {
        FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n",
                       fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate);
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00);
        fdctrl->fifo[3] = kt;
        fdctrl->fifo[4] = kh;
        fdctrl->fifo[5] = ks;
        return;
    }

B
bellard 已提交
1701
    /* Set the FIFO state */
1702 1703
    fdctrl->data_dir = direction;
    fdctrl->data_pos = 0;
1704
    assert(fdctrl->msr & FD_MSR_CMDBUSY);
1705 1706 1707 1708
    if (fdctrl->fifo[0] & 0x80)
        fdctrl->data_state |= FD_STATE_MULTI;
    else
        fdctrl->data_state &= ~FD_STATE_MULTI;
H
Hervé Poussineau 已提交
1709
    if (fdctrl->fifo[5] == 0) {
1710 1711
        fdctrl->data_len = fdctrl->fifo[8];
    } else {
1712
        int tmp;
T
ths 已提交
1713
        fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]);
1714
        tmp = (fdctrl->fifo[6] - ks + 1);
1715
        if (fdctrl->fifo[0] & 0x80)
1716
            tmp += fdctrl->fifo[6];
1717
        fdctrl->data_len *= tmp;
1718
    }
1719
    fdctrl->eot = fdctrl->fifo[6];
B
blueswir1 已提交
1720
    if (fdctrl->dor & FD_DOR_DMAEN) {
1721
        /* DMA transfer is enabled. */
1722
        IsaDmaClass *k = ISADMA_GET_CLASS(fdctrl->dma);
1723 1724 1725

        FLOPPY_DPRINTF("direction=%d (%d - %d)\n",
                       direction, (128 << fdctrl->fifo[5]) *
1726
                       (cur_drv->last_sect - ks + 1), fdctrl->data_len);
1727 1728 1729 1730 1731 1732 1733 1734 1735 1736

        /* No access is allowed until DMA transfer has completed */
        fdctrl->msr &= ~FD_MSR_RQM;
        if (direction != FD_DIR_VERIFY) {
            /*
             * Now, we just have to wait for the DMA controller to
             * recall us...
             */
            k->hold_DREQ(fdctrl->dma, fdctrl->dma_chann);
            k->schedule(fdctrl->dma);
1737
        } else {
1738 1739 1740
            /* Start transfer */
            fdctrl_transfer_handler(fdctrl, fdctrl->dma_chann, 0,
                    fdctrl->data_len);
B
bellard 已提交
1741
        }
1742
        return;
B
bellard 已提交
1743 1744
    }
    FLOPPY_DPRINTF("start non-DMA transfer\n");
K
Kevin Wolf 已提交
1745
    fdctrl->msr |= FD_MSR_NONDMA | FD_MSR_RQM;
B
blueswir1 已提交
1746 1747
    if (direction != FD_DIR_WRITE)
        fdctrl->msr |= FD_MSR_DIO;
B
bellard 已提交
1748
    /* IO based transfer: calculate len */
1749
    fdctrl_raise_irq(fdctrl);
B
bellard 已提交
1750 1751 1752
}

/* Prepare a transfer of deleted data */
B
Blue Swirl 已提交
1753
static void fdctrl_start_transfer_del(FDCtrl *fdctrl, int direction)
B
bellard 已提交
1754
{
B
Blue Swirl 已提交
1755
    qemu_log_mask(LOG_UNIMP, "fdctrl_start_transfer_del() unimplemented\n");
B
blueswir1 已提交
1756

B
bellard 已提交
1757 1758 1759
    /* We don't handle deleted data,
     * so we don't return *ANYTHING*
     */
1760
    fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
B
bellard 已提交
1761 1762 1763
}

/* handlers for DMA transfers */
B
bellard 已提交
1764 1765
static int fdctrl_transfer_handler (void *opaque, int nchan,
                                    int dma_pos, int dma_len)
B
bellard 已提交
1766
{
B
Blue Swirl 已提交
1767 1768
    FDCtrl *fdctrl;
    FDrive *cur_drv;
1769
    int len, start_pos, rel_pos;
B
bellard 已提交
1770
    uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1771
    IsaDmaClass *k;
B
bellard 已提交
1772

1773
    fdctrl = opaque;
B
blueswir1 已提交
1774
    if (fdctrl->msr & FD_MSR_RQM) {
B
bellard 已提交
1775 1776 1777
        FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
        return 0;
    }
1778
    k = ISADMA_GET_CLASS(fdctrl->dma);
1779 1780 1781
    cur_drv = get_cur_drv(fdctrl);
    if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
        fdctrl->data_dir == FD_DIR_SCANH)
B
blueswir1 已提交
1782
        status2 = FD_SR2_SNS;
B
bellard 已提交
1783 1784
    if (dma_len > fdctrl->data_len)
        dma_len = fdctrl->data_len;
1785
    if (cur_drv->blk == NULL) {
1786
        if (fdctrl->data_dir == FD_DIR_WRITE)
1787
            fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1788
        else
1789
            fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1790
        len = 0;
1791 1792
        goto transfer_error;
    }
1793
    rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
B
bellard 已提交
1794 1795
    for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) {
        len = dma_len - fdctrl->data_pos;
1796 1797
        if (len + rel_pos > FD_SECTOR_LEN)
            len = FD_SECTOR_LEN - rel_pos;
B
bellard 已提交
1798 1799
        FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
                       "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos,
B
blueswir1 已提交
1800
                       fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head,
1801
                       cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
1802
                       fd_sector(cur_drv) * FD_SECTOR_LEN);
1803
        if (fdctrl->data_dir != FD_DIR_WRITE ||
1804
            len < FD_SECTOR_LEN || rel_pos != 0) {
1805
            /* READ & SCAN commands and realign to a sector for WRITE */
1806 1807
            if (blk_pread(cur_drv->blk, fd_offset(cur_drv),
                          fdctrl->fifo, BDRV_SECTOR_SIZE) < 0) {
B
bellard 已提交
1808 1809 1810
                FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
                               fd_sector(cur_drv));
                /* Sure, image size is too small... */
1811
                memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
B
bellard 已提交
1812
            }
1813
        }
1814 1815 1816
        switch (fdctrl->data_dir) {
        case FD_DIR_READ:
            /* READ commands */
1817 1818
            k->write_memory(fdctrl->dma, nchan, fdctrl->fifo + rel_pos,
                            fdctrl->data_pos, len);
1819 1820
            break;
        case FD_DIR_WRITE:
1821
            /* WRITE commands */
1822 1823 1824 1825 1826 1827 1828 1829 1830 1831
            if (cur_drv->ro) {
                /* Handle readonly medium early, no need to do DMA, touch the
                 * LED or attempt any writes. A real floppy doesn't attempt
                 * to write to readonly media either. */
                fdctrl_stop_transfer(fdctrl,
                                     FD_SR0_ABNTERM | FD_SR0_SEEK, FD_SR1_NW,
                                     0x00);
                goto transfer_error;
            }

1832 1833
            k->read_memory(fdctrl->dma, nchan, fdctrl->fifo + rel_pos,
                           fdctrl->data_pos, len);
1834 1835
            if (blk_pwrite(cur_drv->blk, fd_offset(cur_drv),
                           fdctrl->fifo, BDRV_SECTOR_SIZE, 0) < 0) {
B
Blue Swirl 已提交
1836 1837
                FLOPPY_DPRINTF("error writing sector %d\n",
                               fd_sector(cur_drv));
1838
                fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1839
                goto transfer_error;
1840
            }
1841
            break;
H
Hervé Poussineau 已提交
1842 1843 1844
        case FD_DIR_VERIFY:
            /* VERIFY commands */
            break;
1845 1846
        default:
            /* SCAN commands */
1847
            {
1848
                uint8_t tmpbuf[FD_SECTOR_LEN];
1849
                int ret;
1850 1851
                k->read_memory(fdctrl->dma, nchan, tmpbuf, fdctrl->data_pos,
                               len);
1852
                ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
B
bellard 已提交
1853
                if (ret == 0) {
B
blueswir1 已提交
1854
                    status2 = FD_SR2_SEH;
B
bellard 已提交
1855 1856
                    goto end_transfer;
                }
1857 1858
                if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
                    (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
B
bellard 已提交
1859 1860 1861 1862
                    status2 = 0x00;
                    goto end_transfer;
                }
            }
1863
            break;
B
bellard 已提交
1864
        }
1865 1866
        fdctrl->data_pos += len;
        rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1867
        if (rel_pos == 0) {
B
bellard 已提交
1868
            /* Seek to next sector */
B
blueswir1 已提交
1869 1870
            if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv))
                break;
B
bellard 已提交
1871 1872
        }
    }
1873
 end_transfer:
1874 1875
    len = fdctrl->data_pos - start_pos;
    FLOPPY_DPRINTF("end transfer %d %d %d\n",
1876
                   fdctrl->data_pos, len, fdctrl->data_len);
1877 1878 1879
    if (fdctrl->data_dir == FD_DIR_SCANE ||
        fdctrl->data_dir == FD_DIR_SCANL ||
        fdctrl->data_dir == FD_DIR_SCANH)
B
blueswir1 已提交
1880
        status2 = FD_SR2_SEH;
1881
    fdctrl->data_len -= len;
1882
    fdctrl_stop_transfer(fdctrl, status0, status1, status2);
1883
 transfer_error:
B
bellard 已提交
1884

1885
    return len;
B
bellard 已提交
1886 1887 1888
}

/* Data register : 0x05 */
B
Blue Swirl 已提交
1889
static uint32_t fdctrl_read_data(FDCtrl *fdctrl)
B
bellard 已提交
1890
{
B
Blue Swirl 已提交
1891
    FDrive *cur_drv;
B
bellard 已提交
1892
    uint32_t retval = 0;
1893
    uint32_t pos;
B
bellard 已提交
1894

1895
    cur_drv = get_cur_drv(fdctrl);
B
blueswir1 已提交
1896 1897
    fdctrl->dsr &= ~FD_DSR_PWRDOWN;
    if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) {
B
Blue Swirl 已提交
1898
        FLOPPY_DPRINTF("error: controller not ready for reading\n");
B
bellard 已提交
1899 1900
        return 0;
    }
1901 1902 1903 1904

    /* If data_len spans multiple sectors, the current position in the FIFO
     * wraps around while fdctrl->data_pos is the real position in the whole
     * request. */
1905
    pos = fdctrl->data_pos;
1906
    pos %= FD_SECTOR_LEN;
1907 1908 1909 1910

    switch (fdctrl->phase) {
    case FD_PHASE_EXECUTION:
        assert(fdctrl->msr & FD_MSR_NONDMA);
B
bellard 已提交
1911
        if (pos == 0) {
B
blueswir1 已提交
1912 1913 1914 1915 1916 1917
            if (fdctrl->data_pos != 0)
                if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
                    FLOPPY_DPRINTF("error seeking to next sector %d\n",
                                   fd_sector(cur_drv));
                    return 0;
                }
1918 1919
            if (blk_pread(cur_drv->blk, fd_offset(cur_drv), fdctrl->fifo,
                          BDRV_SECTOR_SIZE)
1920
                < 0) {
B
blueswir1 已提交
1921 1922 1923 1924 1925
                FLOPPY_DPRINTF("error getting sector %d\n",
                               fd_sector(cur_drv));
                /* Sure, image size is too small... */
                memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
            }
B
bellard 已提交
1926
        }
1927 1928

        if (++fdctrl->data_pos == fdctrl->data_len) {
K
Kevin Wolf 已提交
1929
            fdctrl->msr &= ~FD_MSR_RQM;
1930
            fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1931 1932 1933 1934 1935 1936
        }
        break;

    case FD_PHASE_RESULT:
        assert(!(fdctrl->msr & FD_MSR_NONDMA));
        if (++fdctrl->data_pos == fdctrl->data_len) {
K
Kevin Wolf 已提交
1937
            fdctrl->msr &= ~FD_MSR_RQM;
1938
            fdctrl_to_command_phase(fdctrl);
1939 1940
            fdctrl_reset_irq(fdctrl);
        }
1941 1942 1943 1944 1945
        break;

    case FD_PHASE_COMMAND:
    default:
        abort();
B
bellard 已提交
1946
    }
1947 1948

    retval = fdctrl->fifo[pos];
B
bellard 已提交
1949 1950 1951 1952 1953
    FLOPPY_DPRINTF("data register: 0x%02x\n", retval);

    return retval;
}

B
Blue Swirl 已提交
1954
static void fdctrl_format_sector(FDCtrl *fdctrl)
B
bellard 已提交
1955
{
B
Blue Swirl 已提交
1956
    FDrive *cur_drv;
1957
    uint8_t kh, kt, ks;
B
bellard 已提交
1958

B
blueswir1 已提交
1959
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
1960 1961 1962 1963 1964
    cur_drv = get_cur_drv(fdctrl);
    kt = fdctrl->fifo[6];
    kh = fdctrl->fifo[7];
    ks = fdctrl->fifo[8];
    FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
B
blueswir1 已提交
1965
                   GET_CUR_DRV(fdctrl), kh, kt, ks,
1966 1967
                   fd_sector_calc(kh, kt, ks, cur_drv->last_sect,
                                  NUM_SIDES(cur_drv)));
1968
    switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
1969 1970
    case 2:
        /* sect too big */
1971
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1972 1973 1974 1975 1976 1977
        fdctrl->fifo[3] = kt;
        fdctrl->fifo[4] = kh;
        fdctrl->fifo[5] = ks;
        return;
    case 3:
        /* track too big */
B
blueswir1 已提交
1978
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00);
1979 1980 1981 1982 1983 1984
        fdctrl->fifo[3] = kt;
        fdctrl->fifo[4] = kh;
        fdctrl->fifo[5] = ks;
        return;
    case 4:
        /* No seek enabled */
1985
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1986 1987 1988 1989 1990
        fdctrl->fifo[3] = kt;
        fdctrl->fifo[4] = kh;
        fdctrl->fifo[5] = ks;
        return;
    case 1:
1991
        fdctrl->status0 |= FD_SR0_SEEK;
1992 1993 1994 1995 1996
        break;
    default:
        break;
    }
    memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1997
    if (cur_drv->blk == NULL ||
1998 1999
        blk_pwrite(cur_drv->blk, fd_offset(cur_drv), fdctrl->fifo,
                   BDRV_SECTOR_SIZE, 0) < 0) {
B
Blue Swirl 已提交
2000
        FLOPPY_DPRINTF("error formatting sector %d\n", fd_sector(cur_drv));
2001
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
2002
    } else {
2003 2004 2005
        if (cur_drv->sect == cur_drv->last_sect) {
            fdctrl->data_state &= ~FD_STATE_FORMAT;
            /* Last sector done */
2006
            fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
2007 2008 2009 2010 2011
        } else {
            /* More to do */
            fdctrl->data_pos = 0;
            fdctrl->data_len = 4;
        }
2012 2013 2014
    }
}

B
Blue Swirl 已提交
2015
static void fdctrl_handle_lock(FDCtrl *fdctrl, int direction)
2016 2017 2018
{
    fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0;
    fdctrl->fifo[0] = fdctrl->lock << 4;
2019
    fdctrl_to_result_phase(fdctrl, 1);
2020 2021
}

B
Blue Swirl 已提交
2022
static void fdctrl_handle_dumpreg(FDCtrl *fdctrl, int direction)
2023
{
B
Blue Swirl 已提交
2024
    FDrive *cur_drv = get_cur_drv(fdctrl);
2025 2026 2027 2028

    /* Drives position */
    fdctrl->fifo[0] = drv0(fdctrl)->track;
    fdctrl->fifo[1] = drv1(fdctrl)->track;
B
blueswir1 已提交
2029 2030 2031 2032
#if MAX_FD == 4
    fdctrl->fifo[2] = drv2(fdctrl)->track;
    fdctrl->fifo[3] = drv3(fdctrl)->track;
#else
2033 2034
    fdctrl->fifo[2] = 0;
    fdctrl->fifo[3] = 0;
B
blueswir1 已提交
2035
#endif
2036 2037
    /* timers */
    fdctrl->fifo[4] = fdctrl->timer0;
B
blueswir1 已提交
2038
    fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0);
2039 2040 2041 2042 2043
    fdctrl->fifo[6] = cur_drv->last_sect;
    fdctrl->fifo[7] = (fdctrl->lock << 7) |
        (cur_drv->perpendicular << 2);
    fdctrl->fifo[8] = fdctrl->config;
    fdctrl->fifo[9] = fdctrl->precomp_trk;
2044
    fdctrl_to_result_phase(fdctrl, 10);
2045 2046
}

B
Blue Swirl 已提交
2047
static void fdctrl_handle_version(FDCtrl *fdctrl, int direction)
2048 2049 2050
{
    /* Controller's version */
    fdctrl->fifo[0] = fdctrl->version;
2051
    fdctrl_to_result_phase(fdctrl, 1);
2052 2053
}

B
Blue Swirl 已提交
2054
static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction)
2055 2056
{
    fdctrl->fifo[0] = 0x41; /* Stepping 1 */
2057
    fdctrl_to_result_phase(fdctrl, 1);
2058 2059
}

B
Blue Swirl 已提交
2060
static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction)
2061
{
B
Blue Swirl 已提交
2062
    FDrive *cur_drv = get_cur_drv(fdctrl);
2063 2064 2065 2066

    /* Drives position */
    drv0(fdctrl)->track = fdctrl->fifo[3];
    drv1(fdctrl)->track = fdctrl->fifo[4];
B
blueswir1 已提交
2067 2068 2069 2070
#if MAX_FD == 4
    drv2(fdctrl)->track = fdctrl->fifo[5];
    drv3(fdctrl)->track = fdctrl->fifo[6];
#endif
2071 2072 2073 2074 2075 2076 2077 2078 2079
    /* timers */
    fdctrl->timer0 = fdctrl->fifo[7];
    fdctrl->timer1 = fdctrl->fifo[8];
    cur_drv->last_sect = fdctrl->fifo[9];
    fdctrl->lock = fdctrl->fifo[10] >> 7;
    cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF;
    fdctrl->config = fdctrl->fifo[11];
    fdctrl->precomp_trk = fdctrl->fifo[12];
    fdctrl->pwrd = fdctrl->fifo[13];
2080
    fdctrl_to_command_phase(fdctrl);
2081 2082
}

B
Blue Swirl 已提交
2083
static void fdctrl_handle_save(FDCtrl *fdctrl, int direction)
2084
{
B
Blue Swirl 已提交
2085
    FDrive *cur_drv = get_cur_drv(fdctrl);
2086 2087 2088 2089 2090 2091

    fdctrl->fifo[0] = 0;
    fdctrl->fifo[1] = 0;
    /* Drives position */
    fdctrl->fifo[2] = drv0(fdctrl)->track;
    fdctrl->fifo[3] = drv1(fdctrl)->track;
B
blueswir1 已提交
2092 2093 2094 2095
#if MAX_FD == 4
    fdctrl->fifo[4] = drv2(fdctrl)->track;
    fdctrl->fifo[5] = drv3(fdctrl)->track;
#else
2096 2097
    fdctrl->fifo[4] = 0;
    fdctrl->fifo[5] = 0;
B
blueswir1 已提交
2098
#endif
2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109
    /* timers */
    fdctrl->fifo[6] = fdctrl->timer0;
    fdctrl->fifo[7] = fdctrl->timer1;
    fdctrl->fifo[8] = cur_drv->last_sect;
    fdctrl->fifo[9] = (fdctrl->lock << 7) |
        (cur_drv->perpendicular << 2);
    fdctrl->fifo[10] = fdctrl->config;
    fdctrl->fifo[11] = fdctrl->precomp_trk;
    fdctrl->fifo[12] = fdctrl->pwrd;
    fdctrl->fifo[13] = 0;
    fdctrl->fifo[14] = 0;
2110
    fdctrl_to_result_phase(fdctrl, 15);
2111 2112
}

B
Blue Swirl 已提交
2113
static void fdctrl_handle_readid(FDCtrl *fdctrl, int direction)
2114
{
B
Blue Swirl 已提交
2115
    FDrive *cur_drv = get_cur_drv(fdctrl);
2116 2117

    cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
2118 2119
    timer_mod(fdctrl->result_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) +
             (NANOSECONDS_PER_SECOND / 50));
2120 2121
}

B
Blue Swirl 已提交
2122
static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction)
2123
{
B
Blue Swirl 已提交
2124
    FDrive *cur_drv;
2125

B
blueswir1 已提交
2126
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149
    cur_drv = get_cur_drv(fdctrl);
    fdctrl->data_state |= FD_STATE_FORMAT;
    if (fdctrl->fifo[0] & 0x80)
        fdctrl->data_state |= FD_STATE_MULTI;
    else
        fdctrl->data_state &= ~FD_STATE_MULTI;
    cur_drv->bps =
        fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
#if 0
    cur_drv->last_sect =
        cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
        fdctrl->fifo[3] / 2;
#else
    cur_drv->last_sect = fdctrl->fifo[3];
#endif
    /* TODO: implement format using DMA expected by the Bochs BIOS
     * and Linux fdformat (read 3 bytes per sector via DMA and fill
     * the sector with the specified fill byte
     */
    fdctrl->data_state &= ~FD_STATE_FORMAT;
    fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
}

B
Blue Swirl 已提交
2150
static void fdctrl_handle_specify(FDCtrl *fdctrl, int direction)
2151 2152 2153
{
    fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF;
    fdctrl->timer1 = fdctrl->fifo[2] >> 1;
B
blueswir1 已提交
2154 2155 2156 2157
    if (fdctrl->fifo[2] & 1)
        fdctrl->dor &= ~FD_DOR_DMAEN;
    else
        fdctrl->dor |= FD_DOR_DMAEN;
2158
    /* No result back */
2159
    fdctrl_to_command_phase(fdctrl);
2160 2161
}

B
Blue Swirl 已提交
2162
static void fdctrl_handle_sense_drive_status(FDCtrl *fdctrl, int direction)
2163
{
B
Blue Swirl 已提交
2164
    FDrive *cur_drv;
2165

B
blueswir1 已提交
2166
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
2167 2168 2169 2170 2171 2172
    cur_drv = get_cur_drv(fdctrl);
    cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
    /* 1 Byte status back */
    fdctrl->fifo[0] = (cur_drv->ro << 6) |
        (cur_drv->track == 0 ? 0x10 : 0x00) |
        (cur_drv->head << 2) |
B
blueswir1 已提交
2173
        GET_CUR_DRV(fdctrl) |
2174
        0x28;
2175
    fdctrl_to_result_phase(fdctrl, 1);
2176 2177
}

B
Blue Swirl 已提交
2178
static void fdctrl_handle_recalibrate(FDCtrl *fdctrl, int direction)
2179
{
B
Blue Swirl 已提交
2180
    FDrive *cur_drv;
2181

B
blueswir1 已提交
2182
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
2183 2184
    cur_drv = get_cur_drv(fdctrl);
    fd_recalibrate(cur_drv);
2185
    fdctrl_to_command_phase(fdctrl);
2186
    /* Raise Interrupt */
2187 2188
    fdctrl->status0 |= FD_SR0_SEEK;
    fdctrl_raise_irq(fdctrl);
2189 2190
}

B
Blue Swirl 已提交
2191
static void fdctrl_handle_sense_interrupt_status(FDCtrl *fdctrl, int direction)
2192
{
B
Blue Swirl 已提交
2193
    FDrive *cur_drv = get_cur_drv(fdctrl);
2194

P
Pavel Hrdina 已提交
2195
    if (fdctrl->reset_sensei > 0) {
2196 2197 2198
        fdctrl->fifo[0] =
            FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei;
        fdctrl->reset_sensei--;
P
Pavel Hrdina 已提交
2199 2200
    } else if (!(fdctrl->sra & FD_SRA_INTPEND)) {
        fdctrl->fifo[0] = FD_SR0_INVCMD;
2201
        fdctrl_to_result_phase(fdctrl, 1);
P
Pavel Hrdina 已提交
2202
        return;
2203 2204
    } else {
        fdctrl->fifo[0] =
P
Pavel Hrdina 已提交
2205 2206
                (fdctrl->status0 & ~(FD_SR0_HEAD | FD_SR0_DS1 | FD_SR0_DS0))
                | GET_CUR_DRV(fdctrl);
2207 2208
    }

2209
    fdctrl->fifo[1] = cur_drv->track;
2210
    fdctrl_to_result_phase(fdctrl, 2);
2211
    fdctrl_reset_irq(fdctrl);
B
blueswir1 已提交
2212
    fdctrl->status0 = FD_SR0_RDYCHG;
2213 2214
}

B
Blue Swirl 已提交
2215
static void fdctrl_handle_seek(FDCtrl *fdctrl, int direction)
2216
{
B
Blue Swirl 已提交
2217
    FDrive *cur_drv;
2218

B
blueswir1 已提交
2219
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
2220
    cur_drv = get_cur_drv(fdctrl);
2221
    fdctrl_to_command_phase(fdctrl);
2222 2223 2224
    /* The seek command just sends step pulses to the drive and doesn't care if
     * there is a medium inserted of if it's banging the head against the drive.
     */
2225
    fd_seek(cur_drv, cur_drv->head, fdctrl->fifo[2], cur_drv->sect, 1);
2226
    /* Raise Interrupt */
2227 2228
    fdctrl->status0 |= FD_SR0_SEEK;
    fdctrl_raise_irq(fdctrl);
2229 2230
}

B
Blue Swirl 已提交
2231
static void fdctrl_handle_perpendicular_mode(FDCtrl *fdctrl, int direction)
2232
{
B
Blue Swirl 已提交
2233
    FDrive *cur_drv = get_cur_drv(fdctrl);
2234 2235 2236 2237

    if (fdctrl->fifo[1] & 0x80)
        cur_drv->perpendicular = fdctrl->fifo[1] & 0x7;
    /* No result back */
2238
    fdctrl_to_command_phase(fdctrl);
2239 2240
}

B
Blue Swirl 已提交
2241
static void fdctrl_handle_configure(FDCtrl *fdctrl, int direction)
2242 2243 2244 2245
{
    fdctrl->config = fdctrl->fifo[2];
    fdctrl->precomp_trk =  fdctrl->fifo[3];
    /* No result back */
2246
    fdctrl_to_command_phase(fdctrl);
2247 2248
}

B
Blue Swirl 已提交
2249
static void fdctrl_handle_powerdown_mode(FDCtrl *fdctrl, int direction)
2250 2251 2252
{
    fdctrl->pwrd = fdctrl->fifo[1];
    fdctrl->fifo[0] = fdctrl->fifo[1];
2253
    fdctrl_to_result_phase(fdctrl, 1);
2254 2255
}

B
Blue Swirl 已提交
2256
static void fdctrl_handle_option(FDCtrl *fdctrl, int direction)
2257 2258
{
    /* No result back */
2259
    fdctrl_to_command_phase(fdctrl);
2260 2261
}

B
Blue Swirl 已提交
2262
static void fdctrl_handle_drive_specification_command(FDCtrl *fdctrl, int direction)
2263
{
B
Blue Swirl 已提交
2264
    FDrive *cur_drv = get_cur_drv(fdctrl);
2265
    uint32_t pos;
2266

2267 2268 2269
    pos = fdctrl->data_pos - 1;
    pos %= FD_SECTOR_LEN;
    if (fdctrl->fifo[pos] & 0x80) {
2270
        /* Command parameters done */
2271
        if (fdctrl->fifo[pos] & 0x40) {
2272 2273 2274
            fdctrl->fifo[0] = fdctrl->fifo[1];
            fdctrl->fifo[2] = 0;
            fdctrl->fifo[3] = 0;
2275
            fdctrl_to_result_phase(fdctrl, 4);
2276
        } else {
2277
            fdctrl_to_command_phase(fdctrl);
2278 2279 2280 2281
        }
    } else if (fdctrl->data_len > 7) {
        /* ERROR */
        fdctrl->fifo[0] = 0x80 |
B
blueswir1 已提交
2282
            (cur_drv->head << 2) | GET_CUR_DRV(fdctrl);
2283
        fdctrl_to_result_phase(fdctrl, 1);
2284 2285 2286
    }
}

P
Pavel Hrdina 已提交
2287
static void fdctrl_handle_relative_seek_in(FDCtrl *fdctrl, int direction)
2288
{
B
Blue Swirl 已提交
2289
    FDrive *cur_drv;
2290

B
blueswir1 已提交
2291
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
2292 2293
    cur_drv = get_cur_drv(fdctrl);
    if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) {
2294 2295
        fd_seek(cur_drv, cur_drv->head, cur_drv->max_track - 1,
                cur_drv->sect, 1);
2296
    } else {
P
Pavel Hrdina 已提交
2297 2298
        fd_seek(cur_drv, cur_drv->head,
                cur_drv->track + fdctrl->fifo[2], cur_drv->sect, 1);
2299
    }
2300
    fdctrl_to_command_phase(fdctrl);
B
blueswir1 已提交
2301
    /* Raise Interrupt */
2302 2303
    fdctrl->status0 |= FD_SR0_SEEK;
    fdctrl_raise_irq(fdctrl);
2304 2305
}

P
Pavel Hrdina 已提交
2306
static void fdctrl_handle_relative_seek_out(FDCtrl *fdctrl, int direction)
2307
{
B
Blue Swirl 已提交
2308
    FDrive *cur_drv;
2309

B
blueswir1 已提交
2310
    SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK);
2311 2312
    cur_drv = get_cur_drv(fdctrl);
    if (fdctrl->fifo[2] > cur_drv->track) {
2313
        fd_seek(cur_drv, cur_drv->head, 0, cur_drv->sect, 1);
2314
    } else {
P
Pavel Hrdina 已提交
2315 2316
        fd_seek(cur_drv, cur_drv->head,
                cur_drv->track - fdctrl->fifo[2], cur_drv->sect, 1);
2317
    }
2318
    fdctrl_to_command_phase(fdctrl);
2319
    /* Raise Interrupt */
2320 2321
    fdctrl->status0 |= FD_SR0_SEEK;
    fdctrl_raise_irq(fdctrl);
2322 2323
}

K
Kevin Wolf 已提交
2324 2325 2326
/*
 * Handlers for the execution phase of each command
 */
2327
typedef struct FDCtrlCommand {
B
blueswir1 已提交
2328 2329 2330 2331
    uint8_t value;
    uint8_t mask;
    const char* name;
    int parameters;
B
Blue Swirl 已提交
2332
    void (*handler)(FDCtrl *fdctrl, int direction);
B
blueswir1 已提交
2333
    int direction;
2334 2335 2336
} FDCtrlCommand;

static const FDCtrlCommand handlers[] = {
B
blueswir1 已提交
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347
    { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ },
    { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE },
    { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek },
    { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status },
    { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate },
    { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track },
    { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ },
    { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */
    { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */
    { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ },
    { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE },
H
Hervé Poussineau 已提交
2348
    { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_start_transfer, FD_DIR_VERIFY },
B
blueswir1 已提交
2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372
    { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL },
    { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH },
    { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE },
    { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid },
    { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify },
    { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status },
    { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode },
    { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure },
    { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode },
    { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option },
    { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command },
    { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out },
    { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented },
    { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in },
    { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock },
    { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg },
    { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version },
    { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid },
    { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */
    { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */
};
/* Associate command to an index in the 'handlers' array */
static uint8_t command_to_handler[256];

2373 2374 2375 2376 2377 2378 2379 2380 2381
static const FDCtrlCommand *get_command(uint8_t cmd)
{
    int idx;

    idx = command_to_handler[cmd];
    FLOPPY_DPRINTF("%s command\n", handlers[idx].name);
    return &handlers[idx];
}

B
Blue Swirl 已提交
2382
static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value)
2383
{
B
Blue Swirl 已提交
2384
    FDrive *cur_drv;
2385
    const FDCtrlCommand *cmd;
2386
    uint32_t pos;
2387

B
bellard 已提交
2388
    /* Reset mode */
B
blueswir1 已提交
2389
    if (!(fdctrl->dor & FD_DOR_nRESET)) {
B
bellard 已提交
2390
        FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
B
bellard 已提交
2391 2392
        return;
    }
B
blueswir1 已提交
2393
    if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) {
B
Blue Swirl 已提交
2394
        FLOPPY_DPRINTF("error: controller not ready for writing\n");
B
bellard 已提交
2395 2396
        return;
    }
B
blueswir1 已提交
2397
    fdctrl->dsr &= ~FD_DSR_PWRDOWN;
2398

2399 2400 2401 2402 2403 2404 2405 2406 2407
    FLOPPY_DPRINTF("%s: %02x\n", __func__, value);

    /* If data_len spans multiple sectors, the current position in the FIFO
     * wraps around while fdctrl->data_pos is the real position in the whole
     * request. */
    pos = fdctrl->data_pos++;
    pos %= FD_SECTOR_LEN;
    fdctrl->fifo[pos] = value;

K
Kevin Wolf 已提交
2408 2409 2410 2411
    if (fdctrl->data_pos == fdctrl->data_len) {
        fdctrl->msr &= ~FD_MSR_RQM;
    }

2412 2413 2414 2415 2416
    switch (fdctrl->phase) {
    case FD_PHASE_EXECUTION:
        /* For DMA requests, RQM should be cleared during execution phase, so
         * we would have errored out above. */
        assert(fdctrl->msr & FD_MSR_NONDMA);
2417

B
bellard 已提交
2418
        /* FIFO data write */
2419
        if (pos == FD_SECTOR_LEN - 1 ||
2420
            fdctrl->data_pos == fdctrl->data_len) {
B
blueswir1 已提交
2421
            cur_drv = get_cur_drv(fdctrl);
2422 2423
            if (blk_pwrite(cur_drv->blk, fd_offset(cur_drv), fdctrl->fifo,
                           BDRV_SECTOR_SIZE, 0) < 0) {
B
Blue Swirl 已提交
2424 2425
                FLOPPY_DPRINTF("error writing sector %d\n",
                               fd_sector(cur_drv));
2426
                break;
B
blueswir1 已提交
2427
            }
B
blueswir1 已提交
2428 2429 2430
            if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) {
                FLOPPY_DPRINTF("error seeking to next sector %d\n",
                               fd_sector(cur_drv));
2431
                break;
B
blueswir1 已提交
2432
            }
B
bellard 已提交
2433
        }
2434 2435 2436

        /* Switch to result phase when done with the transfer */
        if (fdctrl->data_pos == fdctrl->data_len) {
2437
            fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
2438
        }
2439
        break;
B
blueswir1 已提交
2440

2441 2442
    case FD_PHASE_COMMAND:
        assert(!(fdctrl->msr & FD_MSR_NONDMA));
2443
        assert(fdctrl->data_pos < FD_SECTOR_LEN);
2444

2445 2446 2447 2448 2449
        if (pos == 0) {
            /* The first byte specifies the command. Now we start reading
             * as many parameters as this command requires. */
            cmd = get_command(value);
            fdctrl->data_len = cmd->parameters + 1;
K
Kevin Wolf 已提交
2450 2451 2452
            if (cmd->parameters) {
                fdctrl->msr |= FD_MSR_RQM;
            }
2453
            fdctrl->msr |= FD_MSR_CMDBUSY;
B
bellard 已提交
2454
        }
2455

2456
        if (fdctrl->data_pos == fdctrl->data_len) {
2457
            /* We have all parameters now, execute the command */
2458
            fdctrl->phase = FD_PHASE_EXECUTION;
2459

2460 2461 2462 2463 2464
            if (fdctrl->data_state & FD_STATE_FORMAT) {
                fdctrl_format_sector(fdctrl);
                break;
            }

2465 2466 2467
            cmd = get_command(fdctrl->fifo[0]);
            FLOPPY_DPRINTF("Calling handler for '%s'\n", cmd->name);
            cmd->handler(fdctrl, cmd->direction);
2468 2469 2470 2471 2472 2473
        }
        break;

    case FD_PHASE_RESULT:
    default:
        abort();
B
bellard 已提交
2474 2475
    }
}
2476 2477 2478

static void fdctrl_result_timer(void *opaque)
{
B
Blue Swirl 已提交
2479 2480
    FDCtrl *fdctrl = opaque;
    FDrive *cur_drv = get_cur_drv(fdctrl);
2481

2482 2483 2484 2485 2486 2487 2488
    /* Pretend we are spinning.
     * This is needed for Coherent, which uses READ ID to check for
     * sector interleaving.
     */
    if (cur_drv->last_sect != 0) {
        cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1;
    }
2489 2490 2491 2492 2493 2494 2495 2496 2497
    /* READ_ID can't automatically succeed! */
    if (fdctrl->check_media_rate &&
        (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) {
        FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n",
                       fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate);
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00);
    } else {
        fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
    }
2498
}
B
blueswir1 已提交
2499 2500

/* Init functions */
2501 2502
static void fdctrl_connect_drives(FDCtrl *fdctrl, DeviceState *fdc_dev,
                                  Error **errp)
B
blueswir1 已提交
2503
{
B
Blue Swirl 已提交
2504
    unsigned int i;
2505
    FDrive *drive;
K
Kevin Wolf 已提交
2506
    DeviceState *dev;
2507
    BlockBackend *blk;
K
Kevin Wolf 已提交
2508
    Error *local_err = NULL;
B
blueswir1 已提交
2509 2510

    for (i = 0; i < MAX_FD; i++) {
2511
        drive = &fdctrl->drives[i];
2512
        drive->fdctrl = fdctrl;
2513

K
Kevin Wolf 已提交
2514 2515
        /* If the drive is not present, we skip creating the qdev device, but
         * still have to initialise the controller. */
2516 2517
        blk = fdctrl->qdev_for_drives[i].blk;
        if (!blk) {
K
Kevin Wolf 已提交
2518 2519 2520
            fd_init(drive);
            fd_revalidate(drive);
            continue;
2521 2522
        }

2523
        dev = qdev_new("floppy");
K
Kevin Wolf 已提交
2524
        qdev_prop_set_uint32(dev, "unit", i);
2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537
        qdev_prop_set_enum(dev, "drive-type", fdctrl->qdev_for_drives[i].type);

        blk_ref(blk);
        blk_detach_dev(blk, fdc_dev);
        fdctrl->qdev_for_drives[i].blk = NULL;
        qdev_prop_set_drive(dev, "drive", blk, &local_err);
        blk_unref(blk);

        if (local_err) {
            error_propagate(errp, local_err);
            return;
        }

2538
        qdev_realize_and_unref(dev, &fdctrl->bus.bus, &local_err);
K
Kevin Wolf 已提交
2539 2540 2541
        if (local_err) {
            error_propagate(errp, local_err);
            return;
2542
        }
B
blueswir1 已提交
2543 2544 2545
    }
}

M
Markus Armbruster 已提交
2546 2547
ISADevice *fdctrl_init_isa(ISABus *bus, DriveInfo **fds)
{
A
Andreas Färber 已提交
2548 2549
    DeviceState *dev;
    ISADevice *isadev;
M
Markus Armbruster 已提交
2550

2551
    isadev = isa_try_new(TYPE_ISA_FDC);
A
Andreas Färber 已提交
2552
    if (!isadev) {
M
Markus Armbruster 已提交
2553 2554
        return NULL;
    }
A
Andreas Färber 已提交
2555
    dev = DEVICE(isadev);
M
Markus Armbruster 已提交
2556 2557

    if (fds[0]) {
2558 2559
        qdev_prop_set_drive(dev, "driveA", blk_by_legacy_dinfo(fds[0]),
                            &error_fatal);
M
Markus Armbruster 已提交
2560 2561
    }
    if (fds[1]) {
2562 2563
        qdev_prop_set_drive(dev, "driveB", blk_by_legacy_dinfo(fds[1]),
                            &error_fatal);
M
Markus Armbruster 已提交
2564
    }
2565
    isa_realize_and_unref(isadev, bus, &error_fatal);
M
Markus Armbruster 已提交
2566

A
Andreas Färber 已提交
2567
    return isadev;
M
Markus Armbruster 已提交
2568 2569
}

B
Blue Swirl 已提交
2570
void fdctrl_init_sysbus(qemu_irq irq, int dma_chann,
A
Avi Kivity 已提交
2571
                        hwaddr mmio_base, DriveInfo **fds)
G
Gerd Hoffmann 已提交
2572
{
B
Blue Swirl 已提交
2573
    FDCtrl *fdctrl;
G
Gerd Hoffmann 已提交
2574
    DeviceState *dev;
H
Hu Tao 已提交
2575
    SysBusDevice *sbd;
B
Blue Swirl 已提交
2576
    FDCtrlSysBus *sys;
G
Gerd Hoffmann 已提交
2577

2578
    dev = qdev_new("sysbus-fdc");
H
Hu Tao 已提交
2579
    sys = SYSBUS_FDC(dev);
2580 2581
    fdctrl = &sys->state;
    fdctrl->dma_chann = dma_chann; /* FIXME */
2582
    if (fds[0]) {
2583 2584
        qdev_prop_set_drive(dev, "driveA", blk_by_legacy_dinfo(fds[0]),
                            &error_fatal);
2585 2586
    }
    if (fds[1]) {
2587 2588
        qdev_prop_set_drive(dev, "driveB", blk_by_legacy_dinfo(fds[1]),
                            &error_fatal);
2589
    }
H
Hu Tao 已提交
2590
    sbd = SYS_BUS_DEVICE(dev);
2591
    sysbus_realize_and_unref(sbd, &error_fatal);
H
Hu Tao 已提交
2592 2593
    sysbus_connect_irq(sbd, 0, irq);
    sysbus_mmio_map(sbd, 0, mmio_base);
B
blueswir1 已提交
2594 2595
}

A
Avi Kivity 已提交
2596
void sun4m_fdctrl_init(qemu_irq irq, hwaddr io_base,
B
Blue Swirl 已提交
2597
                       DriveInfo **fds, qemu_irq *fdc_tc)
B
blueswir1 已提交
2598
{
B
Blue Swirl 已提交
2599
    DeviceState *dev;
B
Blue Swirl 已提交
2600
    FDCtrlSysBus *sys;
B
blueswir1 已提交
2601

2602
    dev = qdev_new("SUNW,fdtwo");
2603
    if (fds[0]) {
2604 2605
        qdev_prop_set_drive(dev, "drive", blk_by_legacy_dinfo(fds[0]),
                            &error_fatal);
2606
    }
2607
    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
H
Hu Tao 已提交
2608 2609 2610
    sys = SYSBUS_FDC(dev);
    sysbus_connect_irq(SYS_BUS_DEVICE(sys), 0, irq);
    sysbus_mmio_map(SYS_BUS_DEVICE(sys), 0, io_base);
B
Blue Swirl 已提交
2611
    *fdc_tc = qdev_get_gpio_in(dev, 0);
B
blueswir1 已提交
2612
}
B
Blue Swirl 已提交
2613

K
Kevin Wolf 已提交
2614 2615
static void fdctrl_realize_common(DeviceState *dev, FDCtrl *fdctrl,
                                  Error **errp)
B
Blue Swirl 已提交
2616
{
B
Blue Swirl 已提交
2617 2618
    int i, j;
    static int command_tables_inited = 0;
B
Blue Swirl 已提交
2619

J
John Snow 已提交
2620 2621
    if (fdctrl->fallback == FLOPPY_DRIVE_TYPE_AUTO) {
        error_setg(errp, "Cannot choose a fallback FDrive type of 'auto'");
2622
        return;
J
John Snow 已提交
2623 2624
    }

B
Blue Swirl 已提交
2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638
    /* Fill 'command_to_handler' lookup table */
    if (!command_tables_inited) {
        command_tables_inited = 1;
        for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) {
            for (j = 0; j < sizeof(command_to_handler); j++) {
                if ((j & handlers[i].mask) == handlers[i].value) {
                    command_to_handler[j] = i;
                }
            }
        }
    }

    FLOPPY_DPRINTF("init controller\n");
    fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN);
2639
    memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
J
Juan Quintela 已提交
2640
    fdctrl->fifo_size = 512;
2641
    fdctrl->result_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
2642
                                             fdctrl_result_timer, fdctrl);
B
Blue Swirl 已提交
2643 2644 2645

    fdctrl->version = 0x90; /* Intel 82078 controller */
    fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */
J
Juan Quintela 已提交
2646
    fdctrl->num_floppies = MAX_FD;
B
Blue Swirl 已提交
2647

2648
    if (fdctrl->dma_chann != -1) {
2649 2650 2651 2652 2653
        IsaDmaClass *k;
        assert(fdctrl->dma);
        k = ISADMA_GET_CLASS(fdctrl->dma);
        k->register_channel(fdctrl->dma, fdctrl->dma_chann,
                            &fdctrl_transfer_handler, fdctrl);
2654
    }
K
Kevin Wolf 已提交
2655 2656

    floppy_bus_create(fdctrl, &fdctrl->bus, dev);
2657
    fdctrl_connect_drives(fdctrl, dev, errp);
B
Blue Swirl 已提交
2658 2659
}

2660
static const MemoryRegionPortio fdc_portio_list[] = {
2661
    { 1, 5, 1, .read = fdctrl_read, .write = fdctrl_write },
2662 2663
    { 7, 1, 1, .read = fdctrl_read, .write = fdctrl_write },
    PORTIO_END_OF_LIST(),
2664 2665
};

2666
static void isabus_fdc_realize(DeviceState *dev, Error **errp)
G
Gerd Hoffmann 已提交
2667
{
2668
    ISADevice *isadev = ISA_DEVICE(dev);
2669
    FDCtrlISABus *isa = ISA_FDC(dev);
B
Blue Swirl 已提交
2670
    FDCtrl *fdctrl = &isa->state;
2671
    Error *err = NULL;
G
Gerd Hoffmann 已提交
2672

2673 2674
    isa_register_portio_list(isadev, &fdctrl->portio_list,
                             isa->iobase, fdc_portio_list, fdctrl,
2675
                             "fdc");
2676

2677
    isa_init_irq(isadev, &fdctrl->irq, isa->irq);
2678
    fdctrl->dma_chann = isa->dma;
2679 2680
    if (fdctrl->dma_chann != -1) {
        fdctrl->dma = isa_get_dma(isa_bus_from_device(isadev), isa->dma);
2681 2682 2683 2684
        if (!fdctrl->dma) {
            error_setg(errp, "ISA controller does not support DMA");
            return;
        }
2685
    }
G
Gerd Hoffmann 已提交
2686

2687
    qdev_set_legacy_instance_id(dev, isa->iobase, 2);
K
Kevin Wolf 已提交
2688
    fdctrl_realize_common(dev, fdctrl, &err);
2689 2690
    if (err != NULL) {
        error_propagate(errp, err);
2691 2692
        return;
    }
G
Gerd Hoffmann 已提交
2693 2694
}

H
Hu Tao 已提交
2695
static void sysbus_fdc_initfn(Object *obj)
B
Blue Swirl 已提交
2696
{
2697
    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
H
Hu Tao 已提交
2698
    FDCtrlSysBus *sys = SYSBUS_FDC(obj);
B
Blue Swirl 已提交
2699
    FDCtrl *fdctrl = &sys->state;
B
Blue Swirl 已提交
2700

2701 2702
    fdctrl->dma_chann = -1;

H
Hu Tao 已提交
2703
    memory_region_init_io(&fdctrl->iomem, obj, &fdctrl_mem_ops, fdctrl,
2704
                          "fdc", 0x08);
2705
    sysbus_init_mmio(sbd, &fdctrl->iomem);
H
Hu Tao 已提交
2706 2707
}

2708
static void sun4m_fdc_initfn(Object *obj)
H
Hu Tao 已提交
2709
{
2710 2711
    SysBusDevice *sbd = SYS_BUS_DEVICE(obj);
    FDCtrlSysBus *sys = SYSBUS_FDC(obj);
H
Hu Tao 已提交
2712 2713
    FDCtrl *fdctrl = &sys->state;

H
Hervé Poussineau 已提交
2714 2715
    fdctrl->dma_chann = -1;

2716 2717 2718
    memory_region_init_io(&fdctrl->iomem, obj, &fdctrl_mem_strict_ops,
                          fdctrl, "fdctrl", 0x08);
    sysbus_init_mmio(sbd, &fdctrl->iomem);
H
Hu Tao 已提交
2719
}
B
Blue Swirl 已提交
2720

2721
static void sysbus_fdc_common_initfn(Object *obj)
H
Hu Tao 已提交
2722
{
2723 2724
    DeviceState *dev = DEVICE(obj);
    SysBusDevice *sbd = SYS_BUS_DEVICE(dev);
H
Hu Tao 已提交
2725 2726 2727
    FDCtrlSysBus *sys = SYSBUS_FDC(obj);
    FDCtrl *fdctrl = &sys->state;

2728 2729 2730 2731
    qdev_set_legacy_instance_id(dev, 0 /* io */, 2); /* FIXME */

    sysbus_init_irq(sbd, &fdctrl->irq);
    qdev_init_gpio_in(dev, fdctrl_handle_tc, 1);
B
Blue Swirl 已提交
2732 2733
}

2734
static void sysbus_fdc_common_realize(DeviceState *dev, Error **errp)
B
Blue Swirl 已提交
2735
{
H
Hu Tao 已提交
2736 2737
    FDCtrlSysBus *sys = SYSBUS_FDC(dev);
    FDCtrl *fdctrl = &sys->state;
B
Blue Swirl 已提交
2738

K
Kevin Wolf 已提交
2739
    fdctrl_realize_common(dev, fdctrl, errp);
B
Blue Swirl 已提交
2740
}
B
Blue Swirl 已提交
2741

J
John Snow 已提交
2742
FloppyDriveType isa_fdc_get_drive_type(ISADevice *fdc, int i)
K
Kevin Wolf 已提交
2743
{
2744
    FDCtrlISABus *isa = ISA_FDC(fdc);
K
Kevin Wolf 已提交
2745

2746
    return isa->state.drives[i].drive;
K
Kevin Wolf 已提交
2747 2748
}

2749 2750
static void isa_fdc_get_drive_max_chs(FloppyDriveType type, uint8_t *maxc,
                                      uint8_t *maxh, uint8_t *maxs)
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771
{
    const FDFormat *fdf;

    *maxc = *maxh = *maxs = 0;
    for (fdf = fd_formats; fdf->drive != FLOPPY_DRIVE_TYPE_NONE; fdf++) {
        if (fdf->drive != type) {
            continue;
        }
        if (*maxc < fdf->max_track) {
            *maxc = fdf->max_track;
        }
        if (*maxh < fdf->max_head) {
            *maxh = fdf->max_head;
        }
        if (*maxs < fdf->last_sect) {
            *maxs = fdf->last_sect;
        }
    }
    (*maxc)--;
}

2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813
static Aml *build_fdinfo_aml(int idx, FloppyDriveType type)
{
    Aml *dev, *fdi;
    uint8_t maxc, maxh, maxs;

    isa_fdc_get_drive_max_chs(type, &maxc, &maxh, &maxs);

    dev = aml_device("FLP%c", 'A' + idx);

    aml_append(dev, aml_name_decl("_ADR", aml_int(idx)));

    fdi = aml_package(16);
    aml_append(fdi, aml_int(idx));  /* Drive Number */
    aml_append(fdi,
        aml_int(cmos_get_fd_drive_type(type)));  /* Device Type */
    /*
     * the values below are the limits of the drive, and are thus independent
     * of the inserted media
     */
    aml_append(fdi, aml_int(maxc));  /* Maximum Cylinder Number */
    aml_append(fdi, aml_int(maxs));  /* Maximum Sector Number */
    aml_append(fdi, aml_int(maxh));  /* Maximum Head Number */
    /*
     * SeaBIOS returns the below values for int 0x13 func 0x08 regardless of
     * the drive type, so shall we
     */
    aml_append(fdi, aml_int(0xAF));  /* disk_specify_1 */
    aml_append(fdi, aml_int(0x02));  /* disk_specify_2 */
    aml_append(fdi, aml_int(0x25));  /* disk_motor_wait */
    aml_append(fdi, aml_int(0x02));  /* disk_sector_siz */
    aml_append(fdi, aml_int(0x12));  /* disk_eot */
    aml_append(fdi, aml_int(0x1B));  /* disk_rw_gap */
    aml_append(fdi, aml_int(0xFF));  /* disk_dtl */
    aml_append(fdi, aml_int(0x6C));  /* disk_formt_gap */
    aml_append(fdi, aml_int(0xF6));  /* disk_fill */
    aml_append(fdi, aml_int(0x0F));  /* disk_head_sttl */
    aml_append(fdi, aml_int(0x08));  /* disk_motor_strt */

    aml_append(dev, aml_name_decl("_FDI", fdi));
    return dev;
}

2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838
int cmos_get_fd_drive_type(FloppyDriveType fd0)
{
    int val;

    switch (fd0) {
    case FLOPPY_DRIVE_TYPE_144:
        /* 1.44 Mb 3"5 drive */
        val = 4;
        break;
    case FLOPPY_DRIVE_TYPE_288:
        /* 2.88 Mb 3"5 drive */
        val = 5;
        break;
    case FLOPPY_DRIVE_TYPE_120:
        /* 1.2 Mb 5"5 drive */
        val = 2;
        break;
    case FLOPPY_DRIVE_TYPE_NONE:
    default:
        val = 0;
        break;
    }
    return val;
}

2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875
static void fdc_isa_build_aml(ISADevice *isadev, Aml *scope)
{
    Aml *dev;
    Aml *crs;
    int i;

#define ACPI_FDE_MAX_FD 4
    uint32_t fde_buf[5] = {
        0, 0, 0, 0,     /* presence of floppy drives #0 - #3 */
        cpu_to_le32(2)  /* tape presence (2 == never present) */
    };

    crs = aml_resource_template();
    aml_append(crs, aml_io(AML_DECODE16, 0x03F2, 0x03F2, 0x00, 0x04));
    aml_append(crs, aml_io(AML_DECODE16, 0x03F7, 0x03F7, 0x00, 0x01));
    aml_append(crs, aml_irq_no_flags(6));
    aml_append(crs,
        aml_dma(AML_COMPATIBILITY, AML_NOTBUSMASTER, AML_TRANSFER8, 2));

    dev = aml_device("FDC0");
    aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0700")));
    aml_append(dev, aml_name_decl("_CRS", crs));

    for (i = 0; i < MIN(MAX_FD, ACPI_FDE_MAX_FD); i++) {
        FloppyDriveType type = isa_fdc_get_drive_type(isadev, i);

        if (type < FLOPPY_DRIVE_TYPE_NONE) {
            fde_buf[i] = cpu_to_le32(1);  /* drive present */
            aml_append(dev, build_fdinfo_aml(i, type));
        }
    }
    aml_append(dev, aml_name_decl("_FDE",
               aml_buffer(sizeof(fde_buf), (uint8_t *)fde_buf)));

    aml_append(scope, dev);
}

J
Jan Kiszka 已提交
2876 2877 2878 2879
static const VMStateDescription vmstate_isa_fdc ={
    .name = "fdc",
    .version_id = 2,
    .minimum_version_id = 2,
2880
    .fields = (VMStateField[]) {
J
Jan Kiszka 已提交
2881 2882 2883 2884 2885
        VMSTATE_STRUCT(state, FDCtrlISABus, 0, vmstate_fdc, FDCtrl),
        VMSTATE_END_OF_LIST()
    }
};

2886
static Property isa_fdc_properties[] = {
2887
    DEFINE_PROP_UINT32("iobase", FDCtrlISABus, iobase, 0x3f0),
2888 2889
    DEFINE_PROP_UINT32("irq", FDCtrlISABus, irq, 6),
    DEFINE_PROP_UINT32("dma", FDCtrlISABus, dma, 2),
2890 2891
    DEFINE_PROP_DRIVE("driveA", FDCtrlISABus, state.qdev_for_drives[0].blk),
    DEFINE_PROP_DRIVE("driveB", FDCtrlISABus, state.qdev_for_drives[1].blk),
2892 2893
    DEFINE_PROP_BIT("check_media_rate", FDCtrlISABus, state.check_media_rate,
                    0, true),
2894
    DEFINE_PROP_SIGNED("fdtypeA", FDCtrlISABus, state.qdev_for_drives[0].type,
J
John Snow 已提交
2895 2896
                        FLOPPY_DRIVE_TYPE_AUTO, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
2897
    DEFINE_PROP_SIGNED("fdtypeB", FDCtrlISABus, state.qdev_for_drives[1].type,
J
John Snow 已提交
2898 2899
                        FLOPPY_DRIVE_TYPE_AUTO, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
2900
    DEFINE_PROP_SIGNED("fallback", FDCtrlISABus, state.fallback,
2901
                        FLOPPY_DRIVE_TYPE_288, qdev_prop_fdc_drive_type,
J
John Snow 已提交
2902
                        FloppyDriveType),
2903 2904 2905
    DEFINE_PROP_END_OF_LIST(),
};

2906
static void isabus_fdc_class_init(ObjectClass *klass, void *data)
2907
{
2908
    DeviceClass *dc = DEVICE_CLASS(klass);
2909
    ISADeviceClass *isa = ISA_DEVICE_CLASS(klass);
2910 2911

    dc->realize = isabus_fdc_realize;
2912 2913 2914
    dc->fw_name = "fdc";
    dc->reset = fdctrl_external_reset_isa;
    dc->vmsd = &vmstate_isa_fdc;
2915
    isa->build_aml = fdc_isa_build_aml;
2916
    device_class_set_props(dc, isa_fdc_properties);
2917
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2918 2919
}

2920 2921 2922 2923 2924 2925
static void isabus_fdc_instance_init(Object *obj)
{
    FDCtrlISABus *isa = ISA_FDC(obj);

    device_add_bootindex_property(obj, &isa->bootindexA,
                                  "bootindexA", "/floppy@0",
2926
                                  DEVICE(obj));
2927 2928
    device_add_bootindex_property(obj, &isa->bootindexB,
                                  "bootindexB", "/floppy@1",
2929
                                  DEVICE(obj));
2930 2931
}

2932
static const TypeInfo isa_fdc_info = {
2933
    .name          = TYPE_ISA_FDC,
2934 2935
    .parent        = TYPE_ISA_DEVICE,
    .instance_size = sizeof(FDCtrlISABus),
2936
    .class_init    = isabus_fdc_class_init,
2937
    .instance_init = isabus_fdc_instance_init,
G
Gerd Hoffmann 已提交
2938 2939
};

J
Jan Kiszka 已提交
2940 2941 2942 2943
static const VMStateDescription vmstate_sysbus_fdc ={
    .name = "fdc",
    .version_id = 2,
    .minimum_version_id = 2,
2944
    .fields = (VMStateField[]) {
J
Jan Kiszka 已提交
2945 2946 2947 2948 2949
        VMSTATE_STRUCT(state, FDCtrlSysBus, 0, vmstate_fdc, FDCtrl),
        VMSTATE_END_OF_LIST()
    }
};

2950
static Property sysbus_fdc_properties[] = {
2951 2952
    DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus, state.qdev_for_drives[0].blk),
    DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus, state.qdev_for_drives[1].blk),
2953
    DEFINE_PROP_SIGNED("fdtypeA", FDCtrlSysBus, state.qdev_for_drives[0].type,
J
John Snow 已提交
2954 2955
                        FLOPPY_DRIVE_TYPE_AUTO, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
2956
    DEFINE_PROP_SIGNED("fdtypeB", FDCtrlSysBus, state.qdev_for_drives[1].type,
J
John Snow 已提交
2957 2958
                        FLOPPY_DRIVE_TYPE_AUTO, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
2959
    DEFINE_PROP_SIGNED("fallback", FDCtrlISABus, state.fallback,
J
John Snow 已提交
2960 2961
                        FLOPPY_DRIVE_TYPE_144, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
2962
    DEFINE_PROP_END_OF_LIST(),
B
Blue Swirl 已提交
2963 2964
};

2965 2966
static void sysbus_fdc_class_init(ObjectClass *klass, void *data)
{
2967
    DeviceClass *dc = DEVICE_CLASS(klass);
2968

2969
    device_class_set_props(dc, sysbus_fdc_properties);
2970
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2971 2972
}

2973
static const TypeInfo sysbus_fdc_info = {
2974 2975
    .name          = "sysbus-fdc",
    .parent        = TYPE_SYSBUS_FDC,
H
Hu Tao 已提交
2976
    .instance_init = sysbus_fdc_initfn,
2977
    .class_init    = sysbus_fdc_class_init,
2978 2979 2980
};

static Property sun4m_fdc_properties[] = {
2981
    DEFINE_PROP_DRIVE("drive", FDCtrlSysBus, state.qdev_for_drives[0].blk),
2982
    DEFINE_PROP_SIGNED("fdtype", FDCtrlSysBus, state.qdev_for_drives[0].type,
J
John Snow 已提交
2983 2984
                        FLOPPY_DRIVE_TYPE_AUTO, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
2985
    DEFINE_PROP_SIGNED("fallback", FDCtrlISABus, state.fallback,
J
John Snow 已提交
2986 2987
                        FLOPPY_DRIVE_TYPE_144, qdev_prop_fdc_drive_type,
                        FloppyDriveType),
2988 2989 2990 2991 2992
    DEFINE_PROP_END_OF_LIST(),
};

static void sun4m_fdc_class_init(ObjectClass *klass, void *data)
{
2993
    DeviceClass *dc = DEVICE_CLASS(klass);
2994

2995
    device_class_set_props(dc, sun4m_fdc_properties);
2996
    set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
2997 2998
}

2999
static const TypeInfo sun4m_fdc_info = {
3000
    .name          = "SUNW,fdtwo",
3001
    .parent        = TYPE_SYSBUS_FDC,
H
Hu Tao 已提交
3002
    .instance_init = sun4m_fdc_initfn,
3003
    .class_init    = sun4m_fdc_class_init,
B
Blue Swirl 已提交
3004 3005
};

3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023
static void sysbus_fdc_common_class_init(ObjectClass *klass, void *data)
{
    DeviceClass *dc = DEVICE_CLASS(klass);

    dc->realize = sysbus_fdc_common_realize;
    dc->reset = fdctrl_external_reset_sysbus;
    dc->vmsd = &vmstate_sysbus_fdc;
}

static const TypeInfo sysbus_fdc_type_info = {
    .name          = TYPE_SYSBUS_FDC,
    .parent        = TYPE_SYS_BUS_DEVICE,
    .instance_size = sizeof(FDCtrlSysBus),
    .instance_init = sysbus_fdc_common_initfn,
    .abstract      = true,
    .class_init    = sysbus_fdc_common_class_init,
};

A
Andreas Färber 已提交
3024
static void fdc_register_types(void)
B
Blue Swirl 已提交
3025
{
3026
    type_register_static(&isa_fdc_info);
3027
    type_register_static(&sysbus_fdc_type_info);
3028 3029
    type_register_static(&sysbus_fdc_info);
    type_register_static(&sun4m_fdc_info);
K
Kevin Wolf 已提交
3030
    type_register_static(&floppy_bus_info);
K
Kevin Wolf 已提交
3031
    type_register_static(&floppy_drive_info);
B
Blue Swirl 已提交
3032 3033
}

A
Andreas Färber 已提交
3034
type_init(fdc_register_types)