| 1 | /* |
| 2 | * QEMU Floppy disk emulator (Intel 82078) |
| 3 | * |
| 4 | * Copyright (c) 2003, 2007 Jocelyn Mayer |
| 5 | * Copyright (c) 2008 Hervé Poussineau |
| 6 | * |
| 7 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 8 | * of this software and associated documentation files (the "Software"), to deal |
| 9 | * in the Software without restriction, including without limitation the rights |
| 10 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 11 | * copies of the Software, and to permit persons to whom the Software is |
| 12 | * furnished to do so, subject to the following conditions: |
| 13 | * |
| 14 | * The above copyright notice and this permission notice shall be included in |
| 15 | * all copies or substantial portions of the Software. |
| 16 | * |
| 17 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 18 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 19 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 20 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 21 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 22 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 23 | * THE SOFTWARE. |
| 24 | */ |
| 25 | /* |
| 26 | * The controller is used in Sun4m systems in a slightly different |
| 27 | * way. There are changes in DOR register and DMA is not available. |
| 28 | */ |
| 29 | |
| 30 | #include "qemu/osdep.h" |
| 31 | #include "hw/block/fdc.h" |
| 32 | #include "qapi/error.h" |
| 33 | #include "qemu/error-report.h" |
| 34 | #include "qemu/timer.h" |
| 35 | #include "qemu/memalign.h" |
| 36 | #include "hw/core/irq.h" |
| 37 | #include "hw/isa/isa.h" |
| 38 | #include "hw/core/qdev-properties.h" |
| 39 | #include "hw/core/qdev-properties-system.h" |
| 40 | #include "migration/vmstate.h" |
| 41 | #include "hw/block/block.h" |
| 42 | #include "system/block-backend.h" |
| 43 | #include "system/blockdev.h" |
| 44 | #include "system/system.h" |
| 45 | #include "qemu/log.h" |
| 46 | #include "qemu/main-loop.h" |
| 47 | #include "qemu/module.h" |
| 48 | #include "trace.h" |
| 49 | #include "qom/object.h" |
| 50 | #include "fdc-internal.h" |
| 51 | |
| 52 | /********************************************************/ |
| 53 | /* debug Floppy devices */ |
| 54 | |
| 55 | #define DEBUG_FLOPPY 0 |
| 56 | |
| 57 | #define FLOPPY_DPRINTF(fmt, ...) \ |
| 58 | do { \ |
| 59 | if (DEBUG_FLOPPY) { \ |
| 60 | fprintf(stderr, "FLOPPY: " fmt , ## __VA_ARGS__); \ |
| 61 | } \ |
| 62 | } while (0) |
| 63 | |
| 64 | |
| 65 | /* Anonymous BlockBackend for empty drive */ |
| 66 | static BlockBackend *blk_create_empty_drive(void) |
| 67 | { |
| 68 | return blk_new(qemu_get_aio_context(), 0, BLK_PERM_ALL); |
| 69 | } |
| 70 | |
| 71 | /********************************************************/ |
| 72 | /* qdev floppy bus */ |
| 73 | |
| 74 | #define TYPE_FLOPPY_BUS "floppy-bus" |
| 75 | OBJECT_DECLARE_SIMPLE_TYPE(FloppyBus, FLOPPY_BUS) |
| 76 | |
| 77 | static FDrive *get_drv(FDCtrl *fdctrl, int unit); |
| 78 | |
| 79 | static const TypeInfo floppy_bus_info = { |
| 80 | .name = TYPE_FLOPPY_BUS, |
| 81 | .parent = TYPE_BUS, |
| 82 | .instance_size = sizeof(FloppyBus), |
| 83 | }; |
| 84 | |
| 85 | static void floppy_bus_create(FDCtrl *fdc, FloppyBus *bus, DeviceState *dev) |
| 86 | { |
| 87 | qbus_init(bus, sizeof(FloppyBus), TYPE_FLOPPY_BUS, dev, NULL); |
| 88 | bus->fdc = fdc; |
| 89 | } |
| 90 | |
| 91 | |
| 92 | /********************************************************/ |
| 93 | /* Floppy drive emulation */ |
| 94 | |
| 95 | /* In many cases, the total sector size of a format is enough to uniquely |
| 96 | * identify it. However, there are some total sector collisions between |
| 97 | * formats of different physical size, and these are noted below by |
| 98 | * highlighting the total sector size for entries with collisions. */ |
| 99 | const FDFormat fd_formats[] = { |
| 100 | /* First entry is default format */ |
| 101 | /* 1.44 MB 3"1/2 floppy disks */ |
| 102 | { FLOPPY_DRIVE_TYPE_144, 18, 80, 1, FDRIVE_RATE_500K, }, /* 3.5" 2880 */ |
| 103 | { FLOPPY_DRIVE_TYPE_144, 20, 80, 1, FDRIVE_RATE_500K, }, /* 3.5" 3200 */ |
| 104 | { FLOPPY_DRIVE_TYPE_144, 21, 80, 1, FDRIVE_RATE_500K, }, |
| 105 | { FLOPPY_DRIVE_TYPE_144, 21, 82, 1, FDRIVE_RATE_500K, }, |
| 106 | { FLOPPY_DRIVE_TYPE_144, 21, 83, 1, FDRIVE_RATE_500K, }, |
| 107 | { FLOPPY_DRIVE_TYPE_144, 22, 80, 1, FDRIVE_RATE_500K, }, |
| 108 | { FLOPPY_DRIVE_TYPE_144, 23, 80, 1, FDRIVE_RATE_500K, }, |
| 109 | { FLOPPY_DRIVE_TYPE_144, 24, 80, 1, FDRIVE_RATE_500K, }, |
| 110 | /* 2.88 MB 3"1/2 floppy disks */ |
| 111 | { FLOPPY_DRIVE_TYPE_288, 36, 80, 1, FDRIVE_RATE_1M, }, |
| 112 | { FLOPPY_DRIVE_TYPE_288, 39, 80, 1, FDRIVE_RATE_1M, }, |
| 113 | { FLOPPY_DRIVE_TYPE_288, 40, 80, 1, FDRIVE_RATE_1M, }, |
| 114 | { FLOPPY_DRIVE_TYPE_288, 44, 80, 1, FDRIVE_RATE_1M, }, |
| 115 | { FLOPPY_DRIVE_TYPE_288, 48, 80, 1, FDRIVE_RATE_1M, }, |
| 116 | /* 720 kB 3"1/2 floppy disks */ |
| 117 | { FLOPPY_DRIVE_TYPE_144, 9, 80, 1, FDRIVE_RATE_250K, }, /* 3.5" 1440 */ |
| 118 | { FLOPPY_DRIVE_TYPE_144, 10, 80, 1, FDRIVE_RATE_250K, }, |
| 119 | { FLOPPY_DRIVE_TYPE_144, 10, 82, 1, FDRIVE_RATE_250K, }, |
| 120 | { FLOPPY_DRIVE_TYPE_144, 10, 83, 1, FDRIVE_RATE_250K, }, |
| 121 | { FLOPPY_DRIVE_TYPE_144, 13, 80, 1, FDRIVE_RATE_250K, }, |
| 122 | { FLOPPY_DRIVE_TYPE_144, 14, 80, 1, FDRIVE_RATE_250K, }, |
| 123 | /* 1.2 MB 5"1/4 floppy disks */ |
| 124 | { FLOPPY_DRIVE_TYPE_120, 15, 80, 1, FDRIVE_RATE_500K, }, |
| 125 | { FLOPPY_DRIVE_TYPE_120, 18, 80, 1, FDRIVE_RATE_500K, }, /* 5.25" 2880 */ |
| 126 | { FLOPPY_DRIVE_TYPE_120, 18, 82, 1, FDRIVE_RATE_500K, }, |
| 127 | { FLOPPY_DRIVE_TYPE_120, 18, 83, 1, FDRIVE_RATE_500K, }, |
| 128 | { FLOPPY_DRIVE_TYPE_120, 20, 80, 1, FDRIVE_RATE_500K, }, /* 5.25" 3200 */ |
| 129 | /* 720 kB 5"1/4 floppy disks */ |
| 130 | { FLOPPY_DRIVE_TYPE_120, 9, 80, 1, FDRIVE_RATE_250K, }, /* 5.25" 1440 */ |
| 131 | { FLOPPY_DRIVE_TYPE_120, 11, 80, 1, FDRIVE_RATE_250K, }, |
| 132 | /* 360 kB 5"1/4 floppy disks */ |
| 133 | { FLOPPY_DRIVE_TYPE_120, 9, 40, 1, FDRIVE_RATE_300K, }, /* 5.25" 720 */ |
| 134 | { FLOPPY_DRIVE_TYPE_120, 9, 40, 0, FDRIVE_RATE_300K, }, |
| 135 | { FLOPPY_DRIVE_TYPE_120, 10, 41, 1, FDRIVE_RATE_300K, }, |
| 136 | { FLOPPY_DRIVE_TYPE_120, 10, 42, 1, FDRIVE_RATE_300K, }, |
| 137 | /* 320 kB 5"1/4 floppy disks */ |
| 138 | { FLOPPY_DRIVE_TYPE_120, 8, 40, 1, FDRIVE_RATE_250K, }, |
| 139 | { FLOPPY_DRIVE_TYPE_120, 8, 40, 0, FDRIVE_RATE_250K, }, |
| 140 | /* 360 kB must match 5"1/4 better than 3"1/2... */ |
| 141 | { FLOPPY_DRIVE_TYPE_144, 9, 80, 0, FDRIVE_RATE_250K, }, /* 3.5" 720 */ |
| 142 | /* end */ |
| 143 | { FLOPPY_DRIVE_TYPE_NONE, -1, -1, 0, 0, }, |
| 144 | }; |
| 145 | |
| 146 | static FDriveSize drive_size(FloppyDriveType drive) |
| 147 | { |
| 148 | switch (drive) { |
| 149 | case FLOPPY_DRIVE_TYPE_120: |
| 150 | return FDRIVE_SIZE_525; |
| 151 | case FLOPPY_DRIVE_TYPE_144: |
| 152 | case FLOPPY_DRIVE_TYPE_288: |
| 153 | return FDRIVE_SIZE_350; |
| 154 | default: |
| 155 | return FDRIVE_SIZE_UNKNOWN; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv) |
| 160 | #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive)) |
| 161 | |
| 162 | /* Will always be a fixed parameter for us */ |
| 163 | #define FD_SECTOR_LEN 512 |
| 164 | #define FD_SECTOR_SC 2 /* Sector size code */ |
| 165 | #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */ |
| 166 | |
| 167 | |
| 168 | static FloppyDriveType get_fallback_drive_type(FDrive *drv); |
| 169 | |
| 170 | /* Hack: FD_SEEK is expected to work on empty drives. However, QEMU |
| 171 | * currently goes through some pains to keep seeks within the bounds |
| 172 | * established by last_sect and max_track. Correcting this is difficult, |
| 173 | * as refactoring FDC code tends to expose nasty bugs in the Linux kernel. |
| 174 | * |
| 175 | * For now: allow empty drives to have large bounds so we can seek around, |
| 176 | * with the understanding that when a diskette is inserted, the bounds will |
| 177 | * properly tighten to match the geometry of that inserted medium. |
| 178 | */ |
| 179 | static void fd_empty_seek_hack(FDrive *drv) |
| 180 | { |
| 181 | drv->last_sect = 0xFF; |
| 182 | drv->max_track = 0xFF; |
| 183 | } |
| 184 | |
| 185 | static void fd_init(FDrive *drv) |
| 186 | { |
| 187 | /* Drive */ |
| 188 | drv->perpendicular = 0; |
| 189 | /* Disk */ |
| 190 | drv->disk = FLOPPY_DRIVE_TYPE_NONE; |
| 191 | drv->last_sect = 0; |
| 192 | drv->max_track = 0; |
| 193 | drv->ro = true; |
| 194 | drv->media_changed = 1; |
| 195 | } |
| 196 | |
| 197 | #define NUM_SIDES(drv) ((drv)->flags & FDISK_DBL_SIDES ? 2 : 1) |
| 198 | |
| 199 | /* Is a diskette present in the drive? */ |
| 200 | static bool fd_media_present(FDrive *drv) |
| 201 | { |
| 202 | return drv->blk != NULL && blk_is_inserted(drv->blk); |
| 203 | } |
| 204 | |
| 205 | static int fd_sector_calc(uint8_t head, uint8_t track, uint8_t sect, |
| 206 | uint8_t last_sect, uint8_t num_sides) |
| 207 | { |
| 208 | return (((track * num_sides) + head) * last_sect) + sect - 1; |
| 209 | } |
| 210 | |
| 211 | /* Returns current position, in sectors, for given drive */ |
| 212 | static int fd_sector(FDrive *drv) |
| 213 | { |
| 214 | return fd_sector_calc(drv->head, drv->track, drv->sect, drv->last_sect, |
| 215 | NUM_SIDES(drv)); |
| 216 | } |
| 217 | |
| 218 | /* Returns current position, in bytes, for given drive */ |
| 219 | static int fd_offset(FDrive *drv) |
| 220 | { |
| 221 | g_assert(fd_sector(drv) < INT_MAX >> BDRV_SECTOR_BITS); |
| 222 | return fd_sector(drv) << BDRV_SECTOR_BITS; |
| 223 | } |
| 224 | |
| 225 | /* Seek to a new position: |
| 226 | * returns 0 if already on right track |
| 227 | * returns 1 if track changed |
| 228 | * returns 2 if track is invalid |
| 229 | * returns 3 if sector is invalid |
| 230 | * returns 4 if seek is disabled |
| 231 | * returns 5 if no floppy is inserted |
| 232 | */ |
| 233 | static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect, |
| 234 | int enable_seek) |
| 235 | { |
| 236 | uint32_t sector; |
| 237 | int ret; |
| 238 | |
| 239 | if (track > drv->max_track || |
| 240 | (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) { |
| 241 | FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n", |
| 242 | head, track, sect, 1, |
| 243 | (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1, |
| 244 | drv->max_track, drv->last_sect); |
| 245 | return 2; |
| 246 | } |
| 247 | if (sect > drv->last_sect) { |
| 248 | FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n", |
| 249 | head, track, sect, 1, |
| 250 | (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1, |
| 251 | drv->max_track, drv->last_sect); |
| 252 | return 3; |
| 253 | } |
| 254 | sector = fd_sector_calc(head, track, sect, drv->last_sect, NUM_SIDES(drv)); |
| 255 | ret = 0; |
| 256 | if (sector != fd_sector(drv)) { |
| 257 | #if 0 |
| 258 | if (!enable_seek) { |
| 259 | FLOPPY_DPRINTF("error: no implicit seek %d %02x %02x" |
| 260 | " (max=%d %02x %02x)\n", |
| 261 | head, track, sect, 1, drv->max_track, |
| 262 | drv->last_sect); |
| 263 | return 4; |
| 264 | } |
| 265 | #endif |
| 266 | drv->head = head; |
| 267 | if (drv->track != track) { |
| 268 | if (fd_media_present(drv)) { |
| 269 | drv->media_changed = 0; |
| 270 | } |
| 271 | ret = 1; |
| 272 | } |
| 273 | drv->track = track; |
| 274 | drv->sect = sect; |
| 275 | } |
| 276 | |
| 277 | if (!fd_media_present(drv)) { |
| 278 | ret = 5; |
| 279 | } |
| 280 | |
| 281 | return ret; |
| 282 | } |
| 283 | |
| 284 | /* Set drive back to track 0 */ |
| 285 | static void fd_recalibrate(FDrive *drv) |
| 286 | { |
| 287 | FLOPPY_DPRINTF("recalibrate\n"); |
| 288 | fd_seek(drv, 0, 0, 1, 1); |
| 289 | } |
| 290 | |
| 291 | /** |
| 292 | * Determine geometry based on inserted diskette. |
| 293 | * Will not operate on an empty drive. |
| 294 | * |
| 295 | * @return: 0 on success, -1 if the drive is empty. |
| 296 | */ |
| 297 | static int pick_geometry(FDrive *drv) |
| 298 | { |
| 299 | BlockBackend *blk = drv->blk; |
| 300 | const FDFormat *parse; |
| 301 | uint64_t nb_sectors, size; |
| 302 | int i; |
| 303 | int match, size_match, type_match; |
| 304 | bool magic = drv->drive == FLOPPY_DRIVE_TYPE_AUTO; |
| 305 | |
| 306 | /* We can only pick a geometry if we have a diskette. */ |
| 307 | if (!drv->blk || !blk_is_inserted(drv->blk) || |
| 308 | drv->drive == FLOPPY_DRIVE_TYPE_NONE) |
| 309 | { |
| 310 | return -1; |
| 311 | } |
| 312 | |
| 313 | /* We need to determine the likely geometry of the inserted medium. |
| 314 | * In order of preference, we look for: |
| 315 | * (1) The same drive type and number of sectors, |
| 316 | * (2) The same diskette size and number of sectors, |
| 317 | * (3) The same drive type. |
| 318 | * |
| 319 | * In all cases, matches that occur higher in the drive table will take |
| 320 | * precedence over matches that occur later in the table. |
| 321 | */ |
| 322 | blk_get_geometry(blk, &nb_sectors); |
| 323 | match = size_match = type_match = -1; |
| 324 | for (i = 0; ; i++) { |
| 325 | parse = &fd_formats[i]; |
| 326 | if (parse->drive == FLOPPY_DRIVE_TYPE_NONE) { |
| 327 | break; |
| 328 | } |
| 329 | size = (parse->max_head + 1) * parse->max_track * parse->last_sect; |
| 330 | if (nb_sectors == size) { |
| 331 | if (magic || parse->drive == drv->drive) { |
| 332 | /* (1) perfect match -- nb_sectors and drive type */ |
| 333 | goto out; |
| 334 | } else if (drive_size(parse->drive) == drive_size(drv->drive)) { |
| 335 | /* (2) size match -- nb_sectors and physical medium size */ |
| 336 | match = (match == -1) ? i : match; |
| 337 | } else { |
| 338 | /* This is suspicious -- Did the user misconfigure? */ |
| 339 | size_match = (size_match == -1) ? i : size_match; |
| 340 | } |
| 341 | } else if (type_match == -1) { |
| 342 | if ((parse->drive == drv->drive) || |
| 343 | (magic && (parse->drive == get_fallback_drive_type(drv)))) { |
| 344 | /* (3) type match -- nb_sectors mismatch, but matches the type |
| 345 | * specified explicitly by the user, or matches the fallback |
| 346 | * default type when using the drive autodetect mechanism */ |
| 347 | type_match = i; |
| 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | /* No exact match found */ |
| 353 | if (match == -1) { |
| 354 | if (size_match != -1) { |
| 355 | parse = &fd_formats[size_match]; |
| 356 | FLOPPY_DPRINTF("User requested floppy drive type '%s', " |
| 357 | "but inserted medium appears to be a " |
| 358 | "%"PRId64" sector '%s' type\n", |
| 359 | FloppyDriveType_str(drv->drive), |
| 360 | nb_sectors, |
| 361 | FloppyDriveType_str(parse->drive)); |
| 362 | } |
| 363 | assert(type_match != -1 && "misconfigured fd_format"); |
| 364 | match = type_match; |
| 365 | } |
| 366 | parse = &(fd_formats[match]); |
| 367 | |
| 368 | out: |
| 369 | if (parse->max_head == 0) { |
| 370 | drv->flags &= ~FDISK_DBL_SIDES; |
| 371 | } else { |
| 372 | drv->flags |= FDISK_DBL_SIDES; |
| 373 | } |
| 374 | drv->max_track = parse->max_track; |
| 375 | drv->last_sect = parse->last_sect; |
| 376 | drv->disk = parse->drive; |
| 377 | drv->media_rate = parse->rate; |
| 378 | return 0; |
| 379 | } |
| 380 | |
| 381 | static void pick_drive_type(FDrive *drv) |
| 382 | { |
| 383 | if (drv->drive != FLOPPY_DRIVE_TYPE_AUTO) { |
| 384 | return; |
| 385 | } |
| 386 | |
| 387 | if (pick_geometry(drv) == 0) { |
| 388 | drv->drive = drv->disk; |
| 389 | } else { |
| 390 | drv->drive = get_fallback_drive_type(drv); |
| 391 | } |
| 392 | |
| 393 | g_assert(drv->drive != FLOPPY_DRIVE_TYPE_AUTO); |
| 394 | } |
| 395 | |
| 396 | /* Revalidate a disk drive after a disk change */ |
| 397 | static void fd_revalidate(FDrive *drv) |
| 398 | { |
| 399 | int rc; |
| 400 | |
| 401 | FLOPPY_DPRINTF("revalidate\n"); |
| 402 | if (drv->blk != NULL) { |
| 403 | drv->ro = !blk_is_writable(drv->blk); |
| 404 | if (!blk_is_inserted(drv->blk)) { |
| 405 | FLOPPY_DPRINTF("No disk in drive\n"); |
| 406 | drv->disk = FLOPPY_DRIVE_TYPE_NONE; |
| 407 | fd_empty_seek_hack(drv); |
| 408 | } else if (!drv->media_validated) { |
| 409 | rc = pick_geometry(drv); |
| 410 | if (rc) { |
| 411 | FLOPPY_DPRINTF("Could not validate floppy drive media"); |
| 412 | } else { |
| 413 | drv->media_validated = true; |
| 414 | FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", |
| 415 | (drv->flags & FDISK_DBL_SIDES) ? 2 : 1, |
| 416 | drv->max_track, drv->last_sect, |
| 417 | drv->ro ? "ro" : "rw"); |
| 418 | } |
| 419 | } |
| 420 | } else { |
| 421 | FLOPPY_DPRINTF("No drive connected\n"); |
| 422 | drv->last_sect = 0; |
| 423 | drv->max_track = 0; |
| 424 | drv->flags &= ~FDISK_DBL_SIDES; |
| 425 | drv->drive = FLOPPY_DRIVE_TYPE_NONE; |
| 426 | drv->disk = FLOPPY_DRIVE_TYPE_NONE; |
| 427 | } |
| 428 | } |
| 429 | |
| 430 | static void fd_change_cb(void *opaque, bool load, Error **errp) |
| 431 | { |
| 432 | FDrive *drive = opaque; |
| 433 | |
| 434 | if (!load) { |
| 435 | blk_set_perm(drive->blk, 0, BLK_PERM_ALL, &error_abort); |
| 436 | } else { |
| 437 | if (!blkconf_apply_backend_options(drive->conf, |
| 438 | !blk_supports_write_perm(drive->blk), |
| 439 | false, errp)) { |
| 440 | return; |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | drive->media_changed = 1; |
| 445 | drive->media_validated = false; |
| 446 | fd_revalidate(drive); |
| 447 | } |
| 448 | |
| 449 | static const BlockDevOps fd_block_ops = { |
| 450 | .change_media_cb = fd_change_cb, |
| 451 | }; |
| 452 | |
| 453 | |
| 454 | #define TYPE_FLOPPY_DRIVE "floppy" |
| 455 | OBJECT_DECLARE_SIMPLE_TYPE(FloppyDrive, FLOPPY_DRIVE) |
| 456 | |
| 457 | struct FloppyDrive { |
| 458 | DeviceState qdev; |
| 459 | uint32_t unit; |
| 460 | BlockConf conf; |
| 461 | FloppyDriveType type; |
| 462 | }; |
| 463 | |
| 464 | static const Property floppy_drive_properties[] = { |
| 465 | DEFINE_PROP_UINT32("unit", FloppyDrive, unit, -1), |
| 466 | DEFINE_BLOCK_PROPERTIES(FloppyDrive, conf), |
| 467 | DEFINE_PROP_SIGNED("drive-type", FloppyDrive, type, |
| 468 | FLOPPY_DRIVE_TYPE_AUTO, qdev_prop_fdc_drive_type, |
| 469 | FloppyDriveType), |
| 470 | }; |
| 471 | |
| 472 | static void floppy_drive_realize(DeviceState *qdev, Error **errp) |
| 473 | { |
| 474 | FloppyDrive *dev = FLOPPY_DRIVE(qdev); |
| 475 | FloppyBus *bus = FLOPPY_BUS(qdev->parent_bus); |
| 476 | FDrive *drive; |
| 477 | bool read_only; |
| 478 | int ret; |
| 479 | |
| 480 | if (dev->unit == -1) { |
| 481 | for (dev->unit = 0; dev->unit < MAX_FD; dev->unit++) { |
| 482 | drive = get_drv(bus->fdc, dev->unit); |
| 483 | if (!drive->blk) { |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | } |
| 488 | |
| 489 | if (dev->unit >= MAX_FD) { |
| 490 | error_setg(errp, "Can't create floppy unit %d, bus supports " |
| 491 | "only %d units", dev->unit, MAX_FD); |
| 492 | return; |
| 493 | } |
| 494 | |
| 495 | drive = get_drv(bus->fdc, dev->unit); |
| 496 | if (drive->blk) { |
| 497 | error_setg(errp, "Floppy unit %d is in use", dev->unit); |
| 498 | return; |
| 499 | } |
| 500 | |
| 501 | if (!dev->conf.blk) { |
| 502 | dev->conf.blk = blk_create_empty_drive(); |
| 503 | ret = blk_attach_dev(dev->conf.blk, qdev); |
| 504 | assert(ret == 0); |
| 505 | |
| 506 | /* Don't take write permissions on an empty drive to allow attaching a |
| 507 | * read-only node later */ |
| 508 | read_only = true; |
| 509 | } else { |
| 510 | read_only = !blk_bs(dev->conf.blk) || |
| 511 | !blk_supports_write_perm(dev->conf.blk); |
| 512 | } |
| 513 | |
| 514 | if (!blkconf_blocksizes(&dev->conf, errp)) { |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | if (dev->conf.logical_block_size != 512 || |
| 519 | dev->conf.physical_block_size != 512) |
| 520 | { |
| 521 | error_setg(errp, "Physical and logical block size must " |
| 522 | "be 512 for floppy"); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | /* rerror/werror aren't supported by fdc and therefore not even registered |
| 527 | * with qdev. So set the defaults manually before they are used in |
| 528 | * blkconf_apply_backend_options(). */ |
| 529 | dev->conf.rerror = BLOCKDEV_ON_ERROR_AUTO; |
| 530 | dev->conf.werror = BLOCKDEV_ON_ERROR_AUTO; |
| 531 | |
| 532 | if (!blkconf_apply_backend_options(&dev->conf, read_only, false, errp)) { |
| 533 | return; |
| 534 | } |
| 535 | |
| 536 | /* 'enospc' is the default for -drive, 'report' is what blk_new() gives us |
| 537 | * for empty drives. */ |
| 538 | if (blk_get_on_error(dev->conf.blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC && |
| 539 | blk_get_on_error(dev->conf.blk, 0) != BLOCKDEV_ON_ERROR_REPORT) { |
| 540 | error_setg(errp, "fdc doesn't support drive option werror"); |
| 541 | return; |
| 542 | } |
| 543 | if (blk_get_on_error(dev->conf.blk, 1) != BLOCKDEV_ON_ERROR_REPORT) { |
| 544 | error_setg(errp, "fdc doesn't support drive option rerror"); |
| 545 | return; |
| 546 | } |
| 547 | |
| 548 | drive->conf = &dev->conf; |
| 549 | drive->blk = dev->conf.blk; |
| 550 | drive->fdctrl = bus->fdc; |
| 551 | |
| 552 | fd_init(drive); |
| 553 | blk_set_dev_ops(drive->blk, &fd_block_ops, drive); |
| 554 | |
| 555 | /* Keep 'type' qdev property and FDrive->drive in sync */ |
| 556 | drive->drive = dev->type; |
| 557 | pick_drive_type(drive); |
| 558 | dev->type = drive->drive; |
| 559 | |
| 560 | fd_revalidate(drive); |
| 561 | } |
| 562 | |
| 563 | static void floppy_drive_class_init(ObjectClass *klass, const void *data) |
| 564 | { |
| 565 | DeviceClass *k = DEVICE_CLASS(klass); |
| 566 | k->realize = floppy_drive_realize; |
| 567 | set_bit(DEVICE_CATEGORY_STORAGE, k->categories); |
| 568 | k->bus_type = TYPE_FLOPPY_BUS; |
| 569 | device_class_set_props(k, floppy_drive_properties); |
| 570 | k->desc = "virtual floppy drive"; |
| 571 | } |
| 572 | |
| 573 | static const TypeInfo floppy_drive_info = { |
| 574 | .name = TYPE_FLOPPY_DRIVE, |
| 575 | .parent = TYPE_DEVICE, |
| 576 | .instance_size = sizeof(FloppyDrive), |
| 577 | .class_init = floppy_drive_class_init, |
| 578 | }; |
| 579 | |
| 580 | /********************************************************/ |
| 581 | /* Intel 82078 floppy disk controller emulation */ |
| 582 | |
| 583 | static void fdctrl_to_command_phase(FDCtrl *fdctrl); |
| 584 | static void fdctrl_raise_irq(FDCtrl *fdctrl); |
| 585 | static FDrive *get_cur_drv(FDCtrl *fdctrl); |
| 586 | |
| 587 | static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl); |
| 588 | static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl); |
| 589 | static uint32_t fdctrl_read_dor(FDCtrl *fdctrl); |
| 590 | static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value); |
| 591 | static uint32_t fdctrl_read_tape(FDCtrl *fdctrl); |
| 592 | static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value); |
| 593 | static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl); |
| 594 | static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value); |
| 595 | static uint32_t fdctrl_read_data(FDCtrl *fdctrl); |
| 596 | static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value); |
| 597 | static uint32_t fdctrl_read_dir(FDCtrl *fdctrl); |
| 598 | static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value); |
| 599 | |
| 600 | enum { |
| 601 | FD_DIR_WRITE = 0, |
| 602 | FD_DIR_READ = 1, |
| 603 | FD_DIR_SCANE = 2, |
| 604 | FD_DIR_SCANL = 3, |
| 605 | FD_DIR_SCANH = 4, |
| 606 | FD_DIR_VERIFY = 5, |
| 607 | }; |
| 608 | |
| 609 | enum { |
| 610 | FD_STATE_MULTI = 0x01, /* multi track flag */ |
| 611 | FD_STATE_FORMAT = 0x02, /* format flag */ |
| 612 | }; |
| 613 | |
| 614 | enum { |
| 615 | FD_REG_SRA = 0x00, |
| 616 | FD_REG_SRB = 0x01, |
| 617 | FD_REG_DOR = 0x02, |
| 618 | FD_REG_TDR = 0x03, |
| 619 | FD_REG_MSR = 0x04, |
| 620 | FD_REG_DSR = 0x04, |
| 621 | FD_REG_FIFO = 0x05, |
| 622 | FD_REG_DIR = 0x07, |
| 623 | FD_REG_CCR = 0x07, |
| 624 | }; |
| 625 | |
| 626 | enum { |
| 627 | FD_CMD_READ_TRACK = 0x02, |
| 628 | FD_CMD_SPECIFY = 0x03, |
| 629 | FD_CMD_SENSE_DRIVE_STATUS = 0x04, |
| 630 | FD_CMD_WRITE = 0x05, |
| 631 | FD_CMD_READ = 0x06, |
| 632 | FD_CMD_RECALIBRATE = 0x07, |
| 633 | FD_CMD_SENSE_INTERRUPT_STATUS = 0x08, |
| 634 | FD_CMD_WRITE_DELETED = 0x09, |
| 635 | FD_CMD_READ_ID = 0x0a, |
| 636 | FD_CMD_READ_DELETED = 0x0c, |
| 637 | FD_CMD_FORMAT_TRACK = 0x0d, |
| 638 | FD_CMD_DUMPREG = 0x0e, |
| 639 | FD_CMD_SEEK = 0x0f, |
| 640 | FD_CMD_VERSION = 0x10, |
| 641 | FD_CMD_SCAN_EQUAL = 0x11, |
| 642 | FD_CMD_PERPENDICULAR_MODE = 0x12, |
| 643 | FD_CMD_CONFIGURE = 0x13, |
| 644 | FD_CMD_LOCK = 0x14, |
| 645 | FD_CMD_VERIFY = 0x16, |
| 646 | FD_CMD_POWERDOWN_MODE = 0x17, |
| 647 | FD_CMD_PART_ID = 0x18, |
| 648 | FD_CMD_SCAN_LOW_OR_EQUAL = 0x19, |
| 649 | FD_CMD_SCAN_HIGH_OR_EQUAL = 0x1d, |
| 650 | FD_CMD_SAVE = 0x2e, |
| 651 | FD_CMD_OPTION = 0x33, |
| 652 | FD_CMD_RESTORE = 0x4e, |
| 653 | FD_CMD_DRIVE_SPECIFICATION_COMMAND = 0x8e, |
| 654 | FD_CMD_RELATIVE_SEEK_OUT = 0x8f, |
| 655 | FD_CMD_FORMAT_AND_WRITE = 0xcd, |
| 656 | FD_CMD_RELATIVE_SEEK_IN = 0xcf, |
| 657 | }; |
| 658 | |
| 659 | enum { |
| 660 | FD_CONFIG_PRETRK = 0xff, /* Pre-compensation set to track 0 */ |
| 661 | FD_CONFIG_FIFOTHR = 0x0f, /* FIFO threshold set to 1 byte */ |
| 662 | FD_CONFIG_POLL = 0x10, /* Poll enabled */ |
| 663 | FD_CONFIG_EFIFO = 0x20, /* FIFO disabled */ |
| 664 | FD_CONFIG_EIS = 0x40, /* No implied seeks */ |
| 665 | }; |
| 666 | |
| 667 | enum { |
| 668 | FD_SR0_DS0 = 0x01, |
| 669 | FD_SR0_DS1 = 0x02, |
| 670 | FD_SR0_HEAD = 0x04, |
| 671 | FD_SR0_EQPMT = 0x10, |
| 672 | FD_SR0_SEEK = 0x20, |
| 673 | FD_SR0_ABNTERM = 0x40, |
| 674 | FD_SR0_INVCMD = 0x80, |
| 675 | FD_SR0_RDYCHG = 0xc0, |
| 676 | }; |
| 677 | |
| 678 | enum { |
| 679 | FD_SR1_MA = 0x01, /* Missing address mark */ |
| 680 | FD_SR1_NW = 0x02, /* Not writable */ |
| 681 | FD_SR1_EC = 0x80, /* End of cylinder */ |
| 682 | }; |
| 683 | |
| 684 | enum { |
| 685 | FD_SR2_SNS = 0x04, /* Scan not satisfied */ |
| 686 | FD_SR2_SEH = 0x08, /* Scan equal hit */ |
| 687 | }; |
| 688 | |
| 689 | enum { |
| 690 | FD_SRA_DIR = 0x01, |
| 691 | FD_SRA_nWP = 0x02, |
| 692 | FD_SRA_nINDX = 0x04, |
| 693 | FD_SRA_HDSEL = 0x08, |
| 694 | FD_SRA_nTRK0 = 0x10, |
| 695 | FD_SRA_STEP = 0x20, |
| 696 | FD_SRA_nDRV2 = 0x40, |
| 697 | FD_SRA_INTPEND = 0x80, |
| 698 | }; |
| 699 | |
| 700 | enum { |
| 701 | FD_SRB_MTR0 = 0x01, |
| 702 | FD_SRB_MTR1 = 0x02, |
| 703 | FD_SRB_WGATE = 0x04, |
| 704 | FD_SRB_RDATA = 0x08, |
| 705 | FD_SRB_WDATA = 0x10, |
| 706 | FD_SRB_DR0 = 0x20, |
| 707 | }; |
| 708 | |
| 709 | enum { |
| 710 | #if MAX_FD == 4 |
| 711 | FD_DOR_SELMASK = 0x03, |
| 712 | #else |
| 713 | FD_DOR_SELMASK = 0x01, |
| 714 | #endif |
| 715 | FD_DOR_nRESET = 0x04, |
| 716 | FD_DOR_DMAEN = 0x08, |
| 717 | FD_DOR_MOTEN0 = 0x10, |
| 718 | FD_DOR_MOTEN1 = 0x20, |
| 719 | FD_DOR_MOTEN2 = 0x40, |
| 720 | FD_DOR_MOTEN3 = 0x80, |
| 721 | }; |
| 722 | |
| 723 | enum { |
| 724 | #if MAX_FD == 4 |
| 725 | FD_TDR_BOOTSEL = 0x0c, |
| 726 | #else |
| 727 | FD_TDR_BOOTSEL = 0x04, |
| 728 | #endif |
| 729 | }; |
| 730 | |
| 731 | enum { |
| 732 | FD_DSR_DRATEMASK= 0x03, |
| 733 | FD_DSR_PWRDOWN = 0x40, |
| 734 | FD_DSR_SWRESET = 0x80, |
| 735 | }; |
| 736 | |
| 737 | enum { |
| 738 | FD_MSR_DRV0BUSY = 0x01, |
| 739 | FD_MSR_DRV1BUSY = 0x02, |
| 740 | FD_MSR_DRV2BUSY = 0x04, |
| 741 | FD_MSR_DRV3BUSY = 0x08, |
| 742 | FD_MSR_CMDBUSY = 0x10, |
| 743 | FD_MSR_NONDMA = 0x20, |
| 744 | FD_MSR_DIO = 0x40, |
| 745 | FD_MSR_RQM = 0x80, |
| 746 | }; |
| 747 | |
| 748 | enum { |
| 749 | FD_DIR_DSKCHG = 0x80, |
| 750 | }; |
| 751 | |
| 752 | /* |
| 753 | * See chapter 5.0 "Controller phases" of the spec: |
| 754 | * |
| 755 | * Command phase: |
| 756 | * The host writes a command and its parameters into the FIFO. The command |
| 757 | * phase is completed when all parameters for the command have been supplied, |
| 758 | * and execution phase is entered. |
| 759 | * |
| 760 | * Execution phase: |
| 761 | * Data transfers, either DMA or non-DMA. For non-DMA transfers, the FIFO |
| 762 | * contains the payload now, otherwise it's unused. When all bytes of the |
| 763 | * required data have been transferred, the state is switched to either result |
| 764 | * phase (if the command produces status bytes) or directly back into the |
| 765 | * command phase for the next command. |
| 766 | * |
| 767 | * Result phase: |
| 768 | * The host reads out the FIFO, which contains one or more result bytes now. |
| 769 | */ |
| 770 | enum { |
| 771 | /* Only for migration: reconstruct phase from registers like qemu 2.3 */ |
| 772 | FD_PHASE_RECONSTRUCT = 0, |
| 773 | |
| 774 | FD_PHASE_COMMAND = 1, |
| 775 | FD_PHASE_EXECUTION = 2, |
| 776 | FD_PHASE_RESULT = 3, |
| 777 | }; |
| 778 | |
| 779 | #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI) |
| 780 | #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT) |
| 781 | |
| 782 | static FloppyDriveType get_fallback_drive_type(FDrive *drv) |
| 783 | { |
| 784 | return drv->fdctrl->fallback; |
| 785 | } |
| 786 | |
| 787 | uint32_t fdctrl_read(void *opaque, uint32_t reg) |
| 788 | { |
| 789 | FDCtrl *fdctrl = opaque; |
| 790 | uint32_t retval; |
| 791 | |
| 792 | reg &= 7; |
| 793 | switch (reg) { |
| 794 | case FD_REG_SRA: |
| 795 | retval = fdctrl_read_statusA(fdctrl); |
| 796 | break; |
| 797 | case FD_REG_SRB: |
| 798 | retval = fdctrl_read_statusB(fdctrl); |
| 799 | break; |
| 800 | case FD_REG_DOR: |
| 801 | retval = fdctrl_read_dor(fdctrl); |
| 802 | break; |
| 803 | case FD_REG_TDR: |
| 804 | retval = fdctrl_read_tape(fdctrl); |
| 805 | break; |
| 806 | case FD_REG_MSR: |
| 807 | retval = fdctrl_read_main_status(fdctrl); |
| 808 | break; |
| 809 | case FD_REG_FIFO: |
| 810 | retval = fdctrl_read_data(fdctrl); |
| 811 | break; |
| 812 | case FD_REG_DIR: |
| 813 | retval = fdctrl_read_dir(fdctrl); |
| 814 | break; |
| 815 | default: |
| 816 | retval = (uint32_t)(-1); |
| 817 | break; |
| 818 | } |
| 819 | trace_fdc_ioport_read(reg, retval); |
| 820 | |
| 821 | return retval; |
| 822 | } |
| 823 | |
| 824 | void fdctrl_write(void *opaque, uint32_t reg, uint32_t value) |
| 825 | { |
| 826 | FDCtrl *fdctrl = opaque; |
| 827 | |
| 828 | reg &= 7; |
| 829 | trace_fdc_ioport_write(reg, value); |
| 830 | switch (reg) { |
| 831 | case FD_REG_DOR: |
| 832 | fdctrl_write_dor(fdctrl, value); |
| 833 | break; |
| 834 | case FD_REG_TDR: |
| 835 | fdctrl_write_tape(fdctrl, value); |
| 836 | break; |
| 837 | case FD_REG_DSR: |
| 838 | fdctrl_write_rate(fdctrl, value); |
| 839 | break; |
| 840 | case FD_REG_FIFO: |
| 841 | fdctrl_write_data(fdctrl, value); |
| 842 | break; |
| 843 | case FD_REG_CCR: |
| 844 | fdctrl_write_ccr(fdctrl, value); |
| 845 | break; |
| 846 | default: |
| 847 | break; |
| 848 | } |
| 849 | } |
| 850 | |
| 851 | static bool fdrive_media_changed_needed(void *opaque) |
| 852 | { |
| 853 | FDrive *drive = opaque; |
| 854 | |
| 855 | return (drive->blk != NULL && drive->media_changed != 1); |
| 856 | } |
| 857 | |
| 858 | static const VMStateDescription vmstate_fdrive_media_changed = { |
| 859 | .name = "fdrive/media_changed", |
| 860 | .version_id = 1, |
| 861 | .minimum_version_id = 1, |
| 862 | .needed = fdrive_media_changed_needed, |
| 863 | .fields = (const VMStateField[]) { |
| 864 | VMSTATE_UINT8(media_changed, FDrive), |
| 865 | VMSTATE_END_OF_LIST() |
| 866 | } |
| 867 | }; |
| 868 | |
| 869 | static const VMStateDescription vmstate_fdrive_media_rate = { |
| 870 | .name = "fdrive/media_rate", |
| 871 | .version_id = 1, |
| 872 | .minimum_version_id = 1, |
| 873 | .fields = (const VMStateField[]) { |
| 874 | VMSTATE_UINT8(media_rate, FDrive), |
| 875 | VMSTATE_END_OF_LIST() |
| 876 | } |
| 877 | }; |
| 878 | |
| 879 | static bool fdrive_perpendicular_needed(void *opaque) |
| 880 | { |
| 881 | FDrive *drive = opaque; |
| 882 | |
| 883 | return drive->perpendicular != 0; |
| 884 | } |
| 885 | |
| 886 | static const VMStateDescription vmstate_fdrive_perpendicular = { |
| 887 | .name = "fdrive/perpendicular", |
| 888 | .version_id = 1, |
| 889 | .minimum_version_id = 1, |
| 890 | .needed = fdrive_perpendicular_needed, |
| 891 | .fields = (const VMStateField[]) { |
| 892 | VMSTATE_UINT8(perpendicular, FDrive), |
| 893 | VMSTATE_END_OF_LIST() |
| 894 | } |
| 895 | }; |
| 896 | |
| 897 | static int fdrive_post_load(void *opaque, int version_id) |
| 898 | { |
| 899 | fd_revalidate(opaque); |
| 900 | return 0; |
| 901 | } |
| 902 | |
| 903 | static const VMStateDescription vmstate_fdrive = { |
| 904 | .name = "fdrive", |
| 905 | .version_id = 1, |
| 906 | .minimum_version_id = 1, |
| 907 | .post_load = fdrive_post_load, |
| 908 | .fields = (const VMStateField[]) { |
| 909 | VMSTATE_UINT8(head, FDrive), |
| 910 | VMSTATE_UINT8(track, FDrive), |
| 911 | VMSTATE_UINT8(sect, FDrive), |
| 912 | VMSTATE_END_OF_LIST() |
| 913 | }, |
| 914 | .subsections = (const VMStateDescription * const []) { |
| 915 | &vmstate_fdrive_media_changed, |
| 916 | &vmstate_fdrive_media_rate, |
| 917 | &vmstate_fdrive_perpendicular, |
| 918 | NULL |
| 919 | } |
| 920 | }; |
| 921 | |
| 922 | /* |
| 923 | * Reconstructs the phase from register values according to the logic that was |
| 924 | * implemented in qemu 2.3. This is the default value that is used if the phase |
| 925 | * subsection is not present on migration. |
| 926 | * |
| 927 | * Don't change this function to reflect newer qemu versions, it is part of |
| 928 | * the migration ABI. |
| 929 | */ |
| 930 | static int reconstruct_phase(FDCtrl *fdctrl) |
| 931 | { |
| 932 | if (fdctrl->msr & FD_MSR_NONDMA) { |
| 933 | return FD_PHASE_EXECUTION; |
| 934 | } else if ((fdctrl->msr & FD_MSR_RQM) == 0) { |
| 935 | /* qemu 2.3 disabled RQM only during DMA transfers */ |
| 936 | return FD_PHASE_EXECUTION; |
| 937 | } else if (fdctrl->msr & FD_MSR_DIO) { |
| 938 | return FD_PHASE_RESULT; |
| 939 | } else { |
| 940 | return FD_PHASE_COMMAND; |
| 941 | } |
| 942 | } |
| 943 | |
| 944 | static int fdc_pre_save(void *opaque) |
| 945 | { |
| 946 | FDCtrl *s = opaque; |
| 947 | |
| 948 | s->dor_vmstate = s->dor | GET_CUR_DRV(s); |
| 949 | |
| 950 | return 0; |
| 951 | } |
| 952 | |
| 953 | static int fdc_pre_load(void *opaque) |
| 954 | { |
| 955 | FDCtrl *s = opaque; |
| 956 | s->phase = FD_PHASE_RECONSTRUCT; |
| 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | static int fdc_post_load(void *opaque, int version_id) |
| 961 | { |
| 962 | FDCtrl *s = opaque; |
| 963 | |
| 964 | SET_CUR_DRV(s, s->dor_vmstate & FD_DOR_SELMASK); |
| 965 | s->dor = s->dor_vmstate & ~FD_DOR_SELMASK; |
| 966 | |
| 967 | if (s->phase == FD_PHASE_RECONSTRUCT) { |
| 968 | s->phase = reconstruct_phase(s); |
| 969 | } |
| 970 | |
| 971 | return 0; |
| 972 | } |
| 973 | |
| 974 | static bool fdc_reset_sensei_needed(void *opaque) |
| 975 | { |
| 976 | FDCtrl *s = opaque; |
| 977 | |
| 978 | return s->reset_sensei != 0; |
| 979 | } |
| 980 | |
| 981 | static const VMStateDescription vmstate_fdc_reset_sensei = { |
| 982 | .name = "fdc/reset_sensei", |
| 983 | .version_id = 1, |
| 984 | .minimum_version_id = 1, |
| 985 | .needed = fdc_reset_sensei_needed, |
| 986 | .fields = (const VMStateField[]) { |
| 987 | VMSTATE_INT32(reset_sensei, FDCtrl), |
| 988 | VMSTATE_END_OF_LIST() |
| 989 | } |
| 990 | }; |
| 991 | |
| 992 | static bool fdc_result_timer_needed(void *opaque) |
| 993 | { |
| 994 | FDCtrl *s = opaque; |
| 995 | |
| 996 | return timer_pending(s->result_timer); |
| 997 | } |
| 998 | |
| 999 | static const VMStateDescription vmstate_fdc_result_timer = { |
| 1000 | .name = "fdc/result_timer", |
| 1001 | .version_id = 1, |
| 1002 | .minimum_version_id = 1, |
| 1003 | .needed = fdc_result_timer_needed, |
| 1004 | .fields = (const VMStateField[]) { |
| 1005 | VMSTATE_TIMER_PTR(result_timer, FDCtrl), |
| 1006 | VMSTATE_END_OF_LIST() |
| 1007 | } |
| 1008 | }; |
| 1009 | |
| 1010 | static bool fdc_phase_needed(void *opaque) |
| 1011 | { |
| 1012 | FDCtrl *fdctrl = opaque; |
| 1013 | |
| 1014 | return reconstruct_phase(fdctrl) != fdctrl->phase; |
| 1015 | } |
| 1016 | |
| 1017 | static const VMStateDescription vmstate_fdc_phase = { |
| 1018 | .name = "fdc/phase", |
| 1019 | .version_id = 1, |
| 1020 | .minimum_version_id = 1, |
| 1021 | .needed = fdc_phase_needed, |
| 1022 | .fields = (const VMStateField[]) { |
| 1023 | VMSTATE_UINT8(phase, FDCtrl), |
| 1024 | VMSTATE_END_OF_LIST() |
| 1025 | } |
| 1026 | }; |
| 1027 | |
| 1028 | const VMStateDescription vmstate_fdc = { |
| 1029 | .name = "fdc", |
| 1030 | .version_id = 2, |
| 1031 | .minimum_version_id = 2, |
| 1032 | .pre_save = fdc_pre_save, |
| 1033 | .pre_load = fdc_pre_load, |
| 1034 | .post_load = fdc_post_load, |
| 1035 | .fields = (const VMStateField[]) { |
| 1036 | /* Controller State */ |
| 1037 | VMSTATE_UINT8(sra, FDCtrl), |
| 1038 | VMSTATE_UINT8(srb, FDCtrl), |
| 1039 | VMSTATE_UINT8(dor_vmstate, FDCtrl), |
| 1040 | VMSTATE_UINT8(tdr, FDCtrl), |
| 1041 | VMSTATE_UINT8(dsr, FDCtrl), |
| 1042 | VMSTATE_UINT8(msr, FDCtrl), |
| 1043 | VMSTATE_UINT8(status0, FDCtrl), |
| 1044 | VMSTATE_UINT8(status1, FDCtrl), |
| 1045 | VMSTATE_UINT8(status2, FDCtrl), |
| 1046 | /* Command FIFO */ |
| 1047 | VMSTATE_VARRAY_INT32(fifo, FDCtrl, fifo_size, 0, vmstate_info_uint8, |
| 1048 | uint8_t), |
| 1049 | VMSTATE_UINT32(data_pos, FDCtrl), |
| 1050 | VMSTATE_UINT32(data_len, FDCtrl), |
| 1051 | VMSTATE_UINT8(data_state, FDCtrl), |
| 1052 | VMSTATE_UINT8(data_dir, FDCtrl), |
| 1053 | VMSTATE_UINT8(eot, FDCtrl), |
| 1054 | /* States kept only to be returned back */ |
| 1055 | VMSTATE_UINT8(timer0, FDCtrl), |
| 1056 | VMSTATE_UINT8(timer1, FDCtrl), |
| 1057 | VMSTATE_UINT8(precomp_trk, FDCtrl), |
| 1058 | VMSTATE_UINT8(config, FDCtrl), |
| 1059 | VMSTATE_UINT8(lock, FDCtrl), |
| 1060 | VMSTATE_UINT8(pwrd, FDCtrl), |
| 1061 | VMSTATE_UINT8_EQUAL(num_floppies, FDCtrl), |
| 1062 | VMSTATE_STRUCT_ARRAY(drives, FDCtrl, MAX_FD, 1, |
| 1063 | vmstate_fdrive, FDrive), |
| 1064 | VMSTATE_END_OF_LIST() |
| 1065 | }, |
| 1066 | .subsections = (const VMStateDescription * const []) { |
| 1067 | &vmstate_fdc_reset_sensei, |
| 1068 | &vmstate_fdc_result_timer, |
| 1069 | &vmstate_fdc_phase, |
| 1070 | NULL |
| 1071 | } |
| 1072 | }; |
| 1073 | |
| 1074 | /* Change IRQ state */ |
| 1075 | static void fdctrl_reset_irq(FDCtrl *fdctrl) |
| 1076 | { |
| 1077 | fdctrl->status0 = 0; |
| 1078 | if (!(fdctrl->sra & FD_SRA_INTPEND)) |
| 1079 | return; |
| 1080 | FLOPPY_DPRINTF("Reset interrupt\n"); |
| 1081 | qemu_set_irq(fdctrl->irq, 0); |
| 1082 | fdctrl->sra &= ~FD_SRA_INTPEND; |
| 1083 | } |
| 1084 | |
| 1085 | static void fdctrl_raise_irq(FDCtrl *fdctrl) |
| 1086 | { |
| 1087 | if (!(fdctrl->sra & FD_SRA_INTPEND)) { |
| 1088 | qemu_set_irq(fdctrl->irq, 1); |
| 1089 | fdctrl->sra |= FD_SRA_INTPEND; |
| 1090 | } |
| 1091 | |
| 1092 | fdctrl->reset_sensei = 0; |
| 1093 | FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl->status0); |
| 1094 | } |
| 1095 | |
| 1096 | /* Reset controller */ |
| 1097 | void fdctrl_reset(FDCtrl *fdctrl, int do_irq) |
| 1098 | { |
| 1099 | int i; |
| 1100 | |
| 1101 | FLOPPY_DPRINTF("reset controller\n"); |
| 1102 | fdctrl_reset_irq(fdctrl); |
| 1103 | /* Initialise controller */ |
| 1104 | fdctrl->sra = 0; |
| 1105 | fdctrl->srb = 0xc0; |
| 1106 | if (!fdctrl->drives[1].blk) { |
| 1107 | fdctrl->sra |= FD_SRA_nDRV2; |
| 1108 | } |
| 1109 | fdctrl->cur_drv = 0; |
| 1110 | fdctrl->dor = FD_DOR_nRESET; |
| 1111 | fdctrl->dor |= (fdctrl->dma_chann != -1) ? FD_DOR_DMAEN : 0; |
| 1112 | fdctrl->msr = FD_MSR_RQM; |
| 1113 | fdctrl->reset_sensei = 0; |
| 1114 | timer_del(fdctrl->result_timer); |
| 1115 | /* FIFO state */ |
| 1116 | fdctrl->data_pos = 0; |
| 1117 | fdctrl->data_len = 0; |
| 1118 | fdctrl->data_state = 0; |
| 1119 | fdctrl->data_dir = FD_DIR_WRITE; |
| 1120 | for (i = 0; i < MAX_FD; i++) |
| 1121 | fd_recalibrate(&fdctrl->drives[i]); |
| 1122 | fdctrl_to_command_phase(fdctrl); |
| 1123 | if (do_irq) { |
| 1124 | fdctrl->status0 |= FD_SR0_RDYCHG; |
| 1125 | fdctrl_raise_irq(fdctrl); |
| 1126 | fdctrl->reset_sensei = FD_RESET_SENSEI_COUNT; |
| 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | static inline FDrive *drv0(FDCtrl *fdctrl) |
| 1131 | { |
| 1132 | return &fdctrl->drives[(fdctrl->tdr & FD_TDR_BOOTSEL) >> 2]; |
| 1133 | } |
| 1134 | |
| 1135 | static inline FDrive *drv1(FDCtrl *fdctrl) |
| 1136 | { |
| 1137 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (1 << 2)) |
| 1138 | return &fdctrl->drives[1]; |
| 1139 | else |
| 1140 | return &fdctrl->drives[0]; |
| 1141 | } |
| 1142 | |
| 1143 | #if MAX_FD == 4 |
| 1144 | static inline FDrive *drv2(FDCtrl *fdctrl) |
| 1145 | { |
| 1146 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (2 << 2)) |
| 1147 | return &fdctrl->drives[2]; |
| 1148 | else |
| 1149 | return &fdctrl->drives[1]; |
| 1150 | } |
| 1151 | |
| 1152 | static inline FDrive *drv3(FDCtrl *fdctrl) |
| 1153 | { |
| 1154 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (3 << 2)) |
| 1155 | return &fdctrl->drives[3]; |
| 1156 | else |
| 1157 | return &fdctrl->drives[2]; |
| 1158 | } |
| 1159 | #endif |
| 1160 | |
| 1161 | static FDrive *get_drv(FDCtrl *fdctrl, int unit) |
| 1162 | { |
| 1163 | switch (unit) { |
| 1164 | case 0: return drv0(fdctrl); |
| 1165 | case 1: return drv1(fdctrl); |
| 1166 | #if MAX_FD == 4 |
| 1167 | case 2: return drv2(fdctrl); |
| 1168 | case 3: return drv3(fdctrl); |
| 1169 | #endif |
| 1170 | default: return NULL; |
| 1171 | } |
| 1172 | } |
| 1173 | |
| 1174 | static FDrive *get_cur_drv(FDCtrl *fdctrl) |
| 1175 | { |
| 1176 | FDrive *cur_drv = get_drv(fdctrl, fdctrl->cur_drv); |
| 1177 | |
| 1178 | if (!cur_drv->blk) { |
| 1179 | /* |
| 1180 | * Kludge: empty drive line selected. Create an anonymous |
| 1181 | * BlockBackend to avoid NULL deref with various BlockBackend |
| 1182 | * API calls within this model (CVE-2021-20196). |
| 1183 | * Due to the controller QOM model limitations, we don't |
| 1184 | * attach the created to the controller device. |
| 1185 | */ |
| 1186 | cur_drv->blk = blk_create_empty_drive(); |
| 1187 | } |
| 1188 | return cur_drv; |
| 1189 | } |
| 1190 | |
| 1191 | /* Status A register : 0x00 (read-only) */ |
| 1192 | static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl) |
| 1193 | { |
| 1194 | uint32_t retval = fdctrl->sra; |
| 1195 | |
| 1196 | FLOPPY_DPRINTF("status register A: 0x%02x\n", retval); |
| 1197 | |
| 1198 | return retval; |
| 1199 | } |
| 1200 | |
| 1201 | /* Status B register : 0x01 (read-only) */ |
| 1202 | static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl) |
| 1203 | { |
| 1204 | uint32_t retval = fdctrl->srb; |
| 1205 | |
| 1206 | FLOPPY_DPRINTF("status register B: 0x%02x\n", retval); |
| 1207 | |
| 1208 | return retval; |
| 1209 | } |
| 1210 | |
| 1211 | /* Digital output register : 0x02 */ |
| 1212 | static uint32_t fdctrl_read_dor(FDCtrl *fdctrl) |
| 1213 | { |
| 1214 | uint32_t retval = fdctrl->dor; |
| 1215 | |
| 1216 | /* Selected drive */ |
| 1217 | retval |= fdctrl->cur_drv; |
| 1218 | FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval); |
| 1219 | |
| 1220 | return retval; |
| 1221 | } |
| 1222 | |
| 1223 | static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value) |
| 1224 | { |
| 1225 | FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value); |
| 1226 | |
| 1227 | /* Motors */ |
| 1228 | if (value & FD_DOR_MOTEN0) |
| 1229 | fdctrl->srb |= FD_SRB_MTR0; |
| 1230 | else |
| 1231 | fdctrl->srb &= ~FD_SRB_MTR0; |
| 1232 | if (value & FD_DOR_MOTEN1) |
| 1233 | fdctrl->srb |= FD_SRB_MTR1; |
| 1234 | else |
| 1235 | fdctrl->srb &= ~FD_SRB_MTR1; |
| 1236 | |
| 1237 | /* Drive */ |
| 1238 | if (value & 1) |
| 1239 | fdctrl->srb |= FD_SRB_DR0; |
| 1240 | else |
| 1241 | fdctrl->srb &= ~FD_SRB_DR0; |
| 1242 | |
| 1243 | /* Reset */ |
| 1244 | if (!(value & FD_DOR_nRESET)) { |
| 1245 | if (fdctrl->dor & FD_DOR_nRESET) { |
| 1246 | FLOPPY_DPRINTF("controller enter RESET state\n"); |
| 1247 | } |
| 1248 | } else { |
| 1249 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
| 1250 | FLOPPY_DPRINTF("controller out of RESET state\n"); |
| 1251 | fdctrl_reset(fdctrl, 1); |
| 1252 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
| 1253 | } |
| 1254 | } |
| 1255 | /* Selected drive */ |
| 1256 | fdctrl->cur_drv = value & FD_DOR_SELMASK; |
| 1257 | |
| 1258 | fdctrl->dor = value; |
| 1259 | } |
| 1260 | |
| 1261 | /* Tape drive register : 0x03 */ |
| 1262 | static uint32_t fdctrl_read_tape(FDCtrl *fdctrl) |
| 1263 | { |
| 1264 | uint32_t retval = fdctrl->tdr; |
| 1265 | |
| 1266 | FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval); |
| 1267 | |
| 1268 | return retval; |
| 1269 | } |
| 1270 | |
| 1271 | static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value) |
| 1272 | { |
| 1273 | /* Reset mode */ |
| 1274 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
| 1275 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
| 1276 | return; |
| 1277 | } |
| 1278 | FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value); |
| 1279 | /* Disk boot selection indicator */ |
| 1280 | fdctrl->tdr = value & FD_TDR_BOOTSEL; |
| 1281 | /* Tape indicators: never allow */ |
| 1282 | } |
| 1283 | |
| 1284 | /* Main status register : 0x04 (read) */ |
| 1285 | static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl) |
| 1286 | { |
| 1287 | uint32_t retval = fdctrl->msr; |
| 1288 | |
| 1289 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
| 1290 | fdctrl->dor |= FD_DOR_nRESET; |
| 1291 | |
| 1292 | FLOPPY_DPRINTF("main status register: 0x%02x\n", retval); |
| 1293 | |
| 1294 | return retval; |
| 1295 | } |
| 1296 | |
| 1297 | /* Data select rate register : 0x04 (write) */ |
| 1298 | static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value) |
| 1299 | { |
| 1300 | /* Reset mode */ |
| 1301 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
| 1302 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
| 1303 | return; |
| 1304 | } |
| 1305 | FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value); |
| 1306 | /* Reset: autoclear */ |
| 1307 | if (value & FD_DSR_SWRESET) { |
| 1308 | fdctrl->dor &= ~FD_DOR_nRESET; |
| 1309 | fdctrl_reset(fdctrl, 1); |
| 1310 | fdctrl->dor |= FD_DOR_nRESET; |
| 1311 | } |
| 1312 | if (value & FD_DSR_PWRDOWN) { |
| 1313 | fdctrl_reset(fdctrl, 1); |
| 1314 | } |
| 1315 | fdctrl->dsr = value; |
| 1316 | } |
| 1317 | |
| 1318 | /* Configuration control register: 0x07 (write) */ |
| 1319 | static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value) |
| 1320 | { |
| 1321 | /* Reset mode */ |
| 1322 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
| 1323 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
| 1324 | return; |
| 1325 | } |
| 1326 | FLOPPY_DPRINTF("configuration control register set to 0x%02x\n", value); |
| 1327 | |
| 1328 | /* Only the rate selection bits used in AT mode, and we |
| 1329 | * store those in the DSR. |
| 1330 | */ |
| 1331 | fdctrl->dsr = (fdctrl->dsr & ~FD_DSR_DRATEMASK) | |
| 1332 | (value & FD_DSR_DRATEMASK); |
| 1333 | } |
| 1334 | |
| 1335 | static int fdctrl_media_changed(FDrive *drv) |
| 1336 | { |
| 1337 | return drv->media_changed; |
| 1338 | } |
| 1339 | |
| 1340 | /* Digital input register : 0x07 (read-only) */ |
| 1341 | static uint32_t fdctrl_read_dir(FDCtrl *fdctrl) |
| 1342 | { |
| 1343 | uint32_t retval = 0; |
| 1344 | |
| 1345 | if (fdctrl_media_changed(get_cur_drv(fdctrl))) { |
| 1346 | retval |= FD_DIR_DSKCHG; |
| 1347 | } |
| 1348 | if (retval != 0) { |
| 1349 | FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval); |
| 1350 | } |
| 1351 | |
| 1352 | return retval; |
| 1353 | } |
| 1354 | |
| 1355 | /* Clear the FIFO and update the state for receiving the next command */ |
| 1356 | static void fdctrl_to_command_phase(FDCtrl *fdctrl) |
| 1357 | { |
| 1358 | fdctrl->phase = FD_PHASE_COMMAND; |
| 1359 | fdctrl->data_dir = FD_DIR_WRITE; |
| 1360 | fdctrl->data_pos = 0; |
| 1361 | fdctrl->data_len = 1; /* Accept command byte, adjust for params later */ |
| 1362 | fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO); |
| 1363 | fdctrl->msr |= FD_MSR_RQM; |
| 1364 | } |
| 1365 | |
| 1366 | /* Update the state to allow the guest to read out the command status. |
| 1367 | * @fifo_len is the number of result bytes to be read out. */ |
| 1368 | static void fdctrl_to_result_phase(FDCtrl *fdctrl, int fifo_len) |
| 1369 | { |
| 1370 | fdctrl->phase = FD_PHASE_RESULT; |
| 1371 | fdctrl->data_dir = FD_DIR_READ; |
| 1372 | fdctrl->data_len = fifo_len; |
| 1373 | fdctrl->data_pos = 0; |
| 1374 | fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO; |
| 1375 | } |
| 1376 | |
| 1377 | /* Set an error: unimplemented/unknown command */ |
| 1378 | static void fdctrl_unimplemented(FDCtrl *fdctrl, int direction) |
| 1379 | { |
| 1380 | qemu_log_mask(LOG_UNIMP, "fdc: unimplemented command 0x%02x\n", |
| 1381 | fdctrl->fifo[0]); |
| 1382 | fdctrl->fifo[0] = FD_SR0_INVCMD; |
| 1383 | fdctrl_to_result_phase(fdctrl, 1); |
| 1384 | } |
| 1385 | |
| 1386 | /* Seek to next sector |
| 1387 | * returns 0 when end of track reached (for DBL_SIDES on head 1) |
| 1388 | * otherwise returns 1 |
| 1389 | */ |
| 1390 | static int fdctrl_seek_to_next_sect(FDCtrl *fdctrl, FDrive *cur_drv) |
| 1391 | { |
| 1392 | FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n", |
| 1393 | cur_drv->head, cur_drv->track, cur_drv->sect, |
| 1394 | fd_sector(cur_drv)); |
| 1395 | /* XXX: cur_drv->sect >= cur_drv->last_sect should be an |
| 1396 | error in fact */ |
| 1397 | uint8_t new_head = cur_drv->head; |
| 1398 | uint8_t new_track = cur_drv->track; |
| 1399 | uint8_t new_sect = cur_drv->sect; |
| 1400 | |
| 1401 | int ret = 1; |
| 1402 | |
| 1403 | if (new_sect >= cur_drv->last_sect || |
| 1404 | new_sect == fdctrl->eot) { |
| 1405 | new_sect = 1; |
| 1406 | if (FD_MULTI_TRACK(fdctrl->data_state)) { |
| 1407 | if (new_head == 0 && |
| 1408 | (cur_drv->flags & FDISK_DBL_SIDES) != 0) { |
| 1409 | new_head = 1; |
| 1410 | } else { |
| 1411 | new_head = 0; |
| 1412 | new_track++; |
| 1413 | fdctrl->status0 |= FD_SR0_SEEK; |
| 1414 | if ((cur_drv->flags & FDISK_DBL_SIDES) == 0) { |
| 1415 | ret = 0; |
| 1416 | } |
| 1417 | } |
| 1418 | } else { |
| 1419 | fdctrl->status0 |= FD_SR0_SEEK; |
| 1420 | new_track++; |
| 1421 | ret = 0; |
| 1422 | } |
| 1423 | if (ret == 1) { |
| 1424 | FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n", |
| 1425 | new_head, new_track, new_sect, fd_sector(cur_drv)); |
| 1426 | } |
| 1427 | } else { |
| 1428 | new_sect++; |
| 1429 | } |
| 1430 | fd_seek(cur_drv, new_head, new_track, new_sect, 1); |
| 1431 | return ret; |
| 1432 | } |
| 1433 | |
| 1434 | /* Callback for transfer end (stop or abort) */ |
| 1435 | static void fdctrl_stop_transfer(FDCtrl *fdctrl, uint8_t status0, |
| 1436 | uint8_t status1, uint8_t status2) |
| 1437 | { |
| 1438 | FDrive *cur_drv; |
| 1439 | cur_drv = get_cur_drv(fdctrl); |
| 1440 | |
| 1441 | fdctrl->status0 &= ~(FD_SR0_DS0 | FD_SR0_DS1 | FD_SR0_HEAD); |
| 1442 | fdctrl->status0 |= GET_CUR_DRV(fdctrl); |
| 1443 | if (cur_drv->head) { |
| 1444 | fdctrl->status0 |= FD_SR0_HEAD; |
| 1445 | } |
| 1446 | fdctrl->status0 |= status0; |
| 1447 | |
| 1448 | FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n", |
| 1449 | status0, status1, status2, fdctrl->status0); |
| 1450 | fdctrl->fifo[0] = fdctrl->status0; |
| 1451 | fdctrl->fifo[1] = status1; |
| 1452 | fdctrl->fifo[2] = status2; |
| 1453 | fdctrl->fifo[3] = cur_drv->track; |
| 1454 | fdctrl->fifo[4] = cur_drv->head; |
| 1455 | fdctrl->fifo[5] = cur_drv->sect; |
| 1456 | fdctrl->fifo[6] = FD_SECTOR_SC; |
| 1457 | fdctrl->data_dir = FD_DIR_READ; |
| 1458 | if (fdctrl->dma_chann != -1 && !(fdctrl->msr & FD_MSR_NONDMA)) { |
| 1459 | IsaDmaClass *k = ISADMA_GET_CLASS(fdctrl->dma); |
| 1460 | k->release_DREQ(fdctrl->dma, fdctrl->dma_chann); |
| 1461 | } |
| 1462 | fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO; |
| 1463 | fdctrl->msr &= ~FD_MSR_NONDMA; |
| 1464 | |
| 1465 | fdctrl_to_result_phase(fdctrl, 7); |
| 1466 | fdctrl_raise_irq(fdctrl); |
| 1467 | } |
| 1468 | |
| 1469 | /* Prepare a data transfer (either DMA or FIFO) */ |
| 1470 | static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction) |
| 1471 | { |
| 1472 | FDrive *cur_drv; |
| 1473 | uint8_t kh, kt, ks; |
| 1474 | |
| 1475 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 1476 | cur_drv = get_cur_drv(fdctrl); |
| 1477 | kt = fdctrl->fifo[2]; |
| 1478 | kh = fdctrl->fifo[3]; |
| 1479 | ks = fdctrl->fifo[4]; |
| 1480 | FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n", |
| 1481 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
| 1482 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
| 1483 | NUM_SIDES(cur_drv))); |
| 1484 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
| 1485 | case 2: |
| 1486 | /* track/head out of range */ |
| 1487 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
| 1488 | fdctrl->fifo[3] = kt; |
| 1489 | fdctrl->fifo[4] = kh; |
| 1490 | fdctrl->fifo[5] = ks; |
| 1491 | return; |
| 1492 | case 5: |
| 1493 | /* |
| 1494 | * No medium: there is no address mark to be found. Guests that tell |
| 1495 | * an absent diskette from an unreadable one rely on ST1.MA. |
| 1496 | */ |
| 1497 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); |
| 1498 | fdctrl->fifo[3] = kt; |
| 1499 | fdctrl->fifo[4] = kh; |
| 1500 | fdctrl->fifo[5] = ks; |
| 1501 | return; |
| 1502 | case 3: |
| 1503 | /* sector too big */ |
| 1504 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
| 1505 | fdctrl->fifo[3] = kt; |
| 1506 | fdctrl->fifo[4] = kh; |
| 1507 | fdctrl->fifo[5] = ks; |
| 1508 | return; |
| 1509 | case 4: |
| 1510 | /* No seek enabled */ |
| 1511 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
| 1512 | fdctrl->fifo[3] = kt; |
| 1513 | fdctrl->fifo[4] = kh; |
| 1514 | fdctrl->fifo[5] = ks; |
| 1515 | return; |
| 1516 | case 1: |
| 1517 | fdctrl->status0 |= FD_SR0_SEEK; |
| 1518 | break; |
| 1519 | default: |
| 1520 | break; |
| 1521 | } |
| 1522 | |
| 1523 | /* Check the data rate. If the programmed data rate does not match |
| 1524 | * the currently inserted medium, the operation has to fail. */ |
| 1525 | if ((fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { |
| 1526 | FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n", |
| 1527 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); |
| 1528 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); |
| 1529 | fdctrl->fifo[3] = kt; |
| 1530 | fdctrl->fifo[4] = kh; |
| 1531 | fdctrl->fifo[5] = ks; |
| 1532 | return; |
| 1533 | } |
| 1534 | |
| 1535 | /* Set the FIFO state */ |
| 1536 | fdctrl->data_dir = direction; |
| 1537 | fdctrl->data_pos = 0; |
| 1538 | assert(fdctrl->msr & FD_MSR_CMDBUSY); |
| 1539 | if (fdctrl->fifo[0] & 0x80) |
| 1540 | fdctrl->data_state |= FD_STATE_MULTI; |
| 1541 | else |
| 1542 | fdctrl->data_state &= ~FD_STATE_MULTI; |
| 1543 | if (fdctrl->fifo[5] == 0) { |
| 1544 | fdctrl->data_len = fdctrl->fifo[8]; |
| 1545 | } else { |
| 1546 | int tmp; |
| 1547 | fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]); |
| 1548 | tmp = (fdctrl->fifo[6] - ks + 1); |
| 1549 | if (tmp < 0) { |
| 1550 | FLOPPY_DPRINTF("invalid EOT: %d\n", tmp); |
| 1551 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); |
| 1552 | fdctrl->fifo[3] = kt; |
| 1553 | fdctrl->fifo[4] = kh; |
| 1554 | fdctrl->fifo[5] = ks; |
| 1555 | return; |
| 1556 | } |
| 1557 | if (fdctrl->fifo[0] & 0x80) |
| 1558 | tmp += fdctrl->fifo[6]; |
| 1559 | fdctrl->data_len *= tmp; |
| 1560 | } |
| 1561 | fdctrl->eot = fdctrl->fifo[6]; |
| 1562 | if (fdctrl->dor & FD_DOR_DMAEN) { |
| 1563 | /* DMA transfer is enabled. */ |
| 1564 | IsaDmaClass *k = ISADMA_GET_CLASS(fdctrl->dma); |
| 1565 | |
| 1566 | FLOPPY_DPRINTF("direction=%d (%d - %d)\n", |
| 1567 | direction, (128 << fdctrl->fifo[5]) * |
| 1568 | (cur_drv->last_sect - ks + 1), fdctrl->data_len); |
| 1569 | |
| 1570 | /* No access is allowed until DMA transfer has completed */ |
| 1571 | fdctrl->msr &= ~FD_MSR_RQM; |
| 1572 | if (direction != FD_DIR_VERIFY) { |
| 1573 | /* |
| 1574 | * Now, we just have to wait for the DMA controller to |
| 1575 | * recall us... |
| 1576 | */ |
| 1577 | k->hold_DREQ(fdctrl->dma, fdctrl->dma_chann); |
| 1578 | k->schedule(fdctrl->dma); |
| 1579 | } else { |
| 1580 | /* Start transfer */ |
| 1581 | fdctrl_transfer_handler(fdctrl, fdctrl->dma_chann, 0, |
| 1582 | fdctrl->data_len); |
| 1583 | } |
| 1584 | return; |
| 1585 | } |
| 1586 | FLOPPY_DPRINTF("start non-DMA transfer\n"); |
| 1587 | fdctrl->msr |= FD_MSR_NONDMA | FD_MSR_RQM; |
| 1588 | if (direction != FD_DIR_WRITE) |
| 1589 | fdctrl->msr |= FD_MSR_DIO; |
| 1590 | /* IO based transfer: calculate len */ |
| 1591 | fdctrl_raise_irq(fdctrl); |
| 1592 | } |
| 1593 | |
| 1594 | /* Prepare a transfer of deleted data */ |
| 1595 | static void fdctrl_start_transfer_del(FDCtrl *fdctrl, int direction) |
| 1596 | { |
| 1597 | qemu_log_mask(LOG_UNIMP, "fdctrl_start_transfer_del() unimplemented\n"); |
| 1598 | |
| 1599 | /* We don't handle deleted data, |
| 1600 | * so we don't return *ANYTHING* |
| 1601 | */ |
| 1602 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
| 1603 | } |
| 1604 | |
| 1605 | /* handlers for DMA transfers */ |
| 1606 | int fdctrl_transfer_handler(void *opaque, int nchan, int dma_pos, int dma_len) |
| 1607 | { |
| 1608 | FDCtrl *fdctrl; |
| 1609 | FDrive *cur_drv; |
| 1610 | int len, start_pos, rel_pos; |
| 1611 | uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00; |
| 1612 | IsaDmaClass *k; |
| 1613 | |
| 1614 | fdctrl = opaque; |
| 1615 | if (fdctrl->msr & FD_MSR_RQM) { |
| 1616 | FLOPPY_DPRINTF("Not in DMA transfer mode !\n"); |
| 1617 | return 0; |
| 1618 | } |
| 1619 | k = ISADMA_GET_CLASS(fdctrl->dma); |
| 1620 | cur_drv = get_cur_drv(fdctrl); |
| 1621 | if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL || |
| 1622 | fdctrl->data_dir == FD_DIR_SCANH) |
| 1623 | status2 = FD_SR2_SNS; |
| 1624 | if (dma_len > fdctrl->data_len) |
| 1625 | dma_len = fdctrl->data_len; |
| 1626 | if (cur_drv->blk == NULL) { |
| 1627 | if (fdctrl->data_dir == FD_DIR_WRITE) |
| 1628 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
| 1629 | else |
| 1630 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
| 1631 | len = 0; |
| 1632 | goto transfer_error; |
| 1633 | } |
| 1634 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; |
| 1635 | for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) { |
| 1636 | len = dma_len - fdctrl->data_pos; |
| 1637 | if (len + rel_pos > FD_SECTOR_LEN) |
| 1638 | len = FD_SECTOR_LEN - rel_pos; |
| 1639 | FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x " |
| 1640 | "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos, |
| 1641 | fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head, |
| 1642 | cur_drv->track, cur_drv->sect, fd_sector(cur_drv), |
| 1643 | fd_sector(cur_drv) * FD_SECTOR_LEN); |
| 1644 | if (fdctrl->data_dir != FD_DIR_WRITE || |
| 1645 | len < FD_SECTOR_LEN || rel_pos != 0) { |
| 1646 | /* READ & SCAN commands and realign to a sector for WRITE */ |
| 1647 | if (blk_pread(cur_drv->blk, fd_offset(cur_drv), BDRV_SECTOR_SIZE, |
| 1648 | fdctrl->fifo, 0) < 0) { |
| 1649 | FLOPPY_DPRINTF("Floppy: error getting sector %d\n", |
| 1650 | fd_sector(cur_drv)); |
| 1651 | /* Sure, image size is too small... */ |
| 1652 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); |
| 1653 | } |
| 1654 | } |
| 1655 | switch (fdctrl->data_dir) { |
| 1656 | case FD_DIR_READ: |
| 1657 | /* READ commands */ |
| 1658 | k->write_memory(fdctrl->dma, nchan, fdctrl->fifo + rel_pos, |
| 1659 | fdctrl->data_pos, len); |
| 1660 | break; |
| 1661 | case FD_DIR_WRITE: |
| 1662 | /* WRITE commands */ |
| 1663 | if (cur_drv->ro) { |
| 1664 | /* Handle readonly medium early, no need to do DMA, touch the |
| 1665 | * LED or attempt any writes. A real floppy doesn't attempt |
| 1666 | * to write to readonly media either. */ |
| 1667 | fdctrl_stop_transfer(fdctrl, |
| 1668 | FD_SR0_ABNTERM | FD_SR0_SEEK, FD_SR1_NW, |
| 1669 | 0x00); |
| 1670 | goto transfer_error; |
| 1671 | } |
| 1672 | |
| 1673 | k->read_memory(fdctrl->dma, nchan, fdctrl->fifo + rel_pos, |
| 1674 | fdctrl->data_pos, len); |
| 1675 | if (blk_pwrite(cur_drv->blk, fd_offset(cur_drv), BDRV_SECTOR_SIZE, |
| 1676 | fdctrl->fifo, 0) < 0) { |
| 1677 | FLOPPY_DPRINTF("error writing sector %d\n", |
| 1678 | fd_sector(cur_drv)); |
| 1679 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
| 1680 | goto transfer_error; |
| 1681 | } |
| 1682 | break; |
| 1683 | case FD_DIR_VERIFY: |
| 1684 | /* VERIFY commands */ |
| 1685 | break; |
| 1686 | default: |
| 1687 | /* SCAN commands */ |
| 1688 | { |
| 1689 | uint8_t tmpbuf[FD_SECTOR_LEN]; |
| 1690 | int ret; |
| 1691 | k->read_memory(fdctrl->dma, nchan, tmpbuf, fdctrl->data_pos, |
| 1692 | len); |
| 1693 | ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len); |
| 1694 | if (ret == 0) { |
| 1695 | status2 = FD_SR2_SEH; |
| 1696 | goto end_transfer; |
| 1697 | } |
| 1698 | if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) || |
| 1699 | (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) { |
| 1700 | status2 = 0x00; |
| 1701 | goto end_transfer; |
| 1702 | } |
| 1703 | } |
| 1704 | break; |
| 1705 | } |
| 1706 | fdctrl->data_pos += len; |
| 1707 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; |
| 1708 | if (rel_pos == 0) { |
| 1709 | /* Seek to next sector */ |
| 1710 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) |
| 1711 | break; |
| 1712 | } |
| 1713 | } |
| 1714 | end_transfer: |
| 1715 | len = fdctrl->data_pos - start_pos; |
| 1716 | FLOPPY_DPRINTF("end transfer %d %d %d\n", |
| 1717 | fdctrl->data_pos, len, fdctrl->data_len); |
| 1718 | if (fdctrl->data_dir == FD_DIR_SCANE || |
| 1719 | fdctrl->data_dir == FD_DIR_SCANL || |
| 1720 | fdctrl->data_dir == FD_DIR_SCANH) |
| 1721 | status2 = FD_SR2_SEH; |
| 1722 | fdctrl->data_len -= len; |
| 1723 | fdctrl_stop_transfer(fdctrl, status0, status1, status2); |
| 1724 | transfer_error: |
| 1725 | |
| 1726 | return len; |
| 1727 | } |
| 1728 | |
| 1729 | /* Data register : 0x05 */ |
| 1730 | static uint32_t fdctrl_read_data(FDCtrl *fdctrl) |
| 1731 | { |
| 1732 | FDrive *cur_drv; |
| 1733 | uint32_t retval = 0; |
| 1734 | uint32_t pos; |
| 1735 | |
| 1736 | cur_drv = get_cur_drv(fdctrl); |
| 1737 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
| 1738 | if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) { |
| 1739 | FLOPPY_DPRINTF("error: controller not ready for reading\n"); |
| 1740 | return 0; |
| 1741 | } |
| 1742 | |
| 1743 | /* If data_len spans multiple sectors, the current position in the FIFO |
| 1744 | * wraps around while fdctrl->data_pos is the real position in the whole |
| 1745 | * request. */ |
| 1746 | pos = fdctrl->data_pos; |
| 1747 | pos %= FD_SECTOR_LEN; |
| 1748 | |
| 1749 | switch (fdctrl->phase) { |
| 1750 | case FD_PHASE_EXECUTION: |
| 1751 | assert(fdctrl->msr & FD_MSR_NONDMA); |
| 1752 | if (pos == 0) { |
| 1753 | if (fdctrl->data_pos != 0) |
| 1754 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { |
| 1755 | FLOPPY_DPRINTF("error seeking to next sector %d\n", |
| 1756 | fd_sector(cur_drv)); |
| 1757 | return 0; |
| 1758 | } |
| 1759 | if (blk_pread(cur_drv->blk, fd_offset(cur_drv), BDRV_SECTOR_SIZE, |
| 1760 | fdctrl->fifo, 0) |
| 1761 | < 0) { |
| 1762 | FLOPPY_DPRINTF("error getting sector %d\n", |
| 1763 | fd_sector(cur_drv)); |
| 1764 | /* Sure, image size is too small... */ |
| 1765 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); |
| 1766 | } |
| 1767 | } |
| 1768 | |
| 1769 | if (++fdctrl->data_pos == fdctrl->data_len) { |
| 1770 | fdctrl->msr &= ~FD_MSR_RQM; |
| 1771 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
| 1772 | } |
| 1773 | break; |
| 1774 | |
| 1775 | case FD_PHASE_RESULT: |
| 1776 | assert(!(fdctrl->msr & FD_MSR_NONDMA)); |
| 1777 | if (++fdctrl->data_pos == fdctrl->data_len) { |
| 1778 | fdctrl->msr &= ~FD_MSR_RQM; |
| 1779 | fdctrl_to_command_phase(fdctrl); |
| 1780 | fdctrl_reset_irq(fdctrl); |
| 1781 | } |
| 1782 | break; |
| 1783 | |
| 1784 | case FD_PHASE_COMMAND: |
| 1785 | default: |
| 1786 | abort(); |
| 1787 | } |
| 1788 | |
| 1789 | retval = fdctrl->fifo[pos]; |
| 1790 | FLOPPY_DPRINTF("data register: 0x%02x\n", retval); |
| 1791 | |
| 1792 | return retval; |
| 1793 | } |
| 1794 | |
| 1795 | static void fdctrl_format_sector(FDCtrl *fdctrl) |
| 1796 | { |
| 1797 | FDrive *cur_drv; |
| 1798 | uint8_t kh, kt, ks; |
| 1799 | |
| 1800 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 1801 | cur_drv = get_cur_drv(fdctrl); |
| 1802 | kt = fdctrl->fifo[6]; |
| 1803 | kh = fdctrl->fifo[7]; |
| 1804 | ks = fdctrl->fifo[8]; |
| 1805 | FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n", |
| 1806 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
| 1807 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
| 1808 | NUM_SIDES(cur_drv))); |
| 1809 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
| 1810 | case 2: |
| 1811 | /* track/head out of range */ |
| 1812 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
| 1813 | fdctrl->fifo[3] = kt; |
| 1814 | fdctrl->fifo[4] = kh; |
| 1815 | fdctrl->fifo[5] = ks; |
| 1816 | return; |
| 1817 | case 5: |
| 1818 | /* no medium */ |
| 1819 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); |
| 1820 | fdctrl->fifo[3] = kt; |
| 1821 | fdctrl->fifo[4] = kh; |
| 1822 | fdctrl->fifo[5] = ks; |
| 1823 | return; |
| 1824 | case 3: |
| 1825 | /* sector too big */ |
| 1826 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
| 1827 | fdctrl->fifo[3] = kt; |
| 1828 | fdctrl->fifo[4] = kh; |
| 1829 | fdctrl->fifo[5] = ks; |
| 1830 | return; |
| 1831 | case 4: |
| 1832 | /* No seek enabled */ |
| 1833 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
| 1834 | fdctrl->fifo[3] = kt; |
| 1835 | fdctrl->fifo[4] = kh; |
| 1836 | fdctrl->fifo[5] = ks; |
| 1837 | return; |
| 1838 | case 1: |
| 1839 | fdctrl->status0 |= FD_SR0_SEEK; |
| 1840 | break; |
| 1841 | default: |
| 1842 | break; |
| 1843 | } |
| 1844 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); |
| 1845 | if (cur_drv->blk == NULL || |
| 1846 | blk_pwrite(cur_drv->blk, fd_offset(cur_drv), BDRV_SECTOR_SIZE, |
| 1847 | fdctrl->fifo, 0) < 0) { |
| 1848 | FLOPPY_DPRINTF("error formatting sector %d\n", fd_sector(cur_drv)); |
| 1849 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
| 1850 | } else { |
| 1851 | if (cur_drv->sect == cur_drv->last_sect) { |
| 1852 | fdctrl->data_state &= ~FD_STATE_FORMAT; |
| 1853 | /* Last sector done */ |
| 1854 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
| 1855 | } else { |
| 1856 | /* More to do */ |
| 1857 | fdctrl->data_pos = 0; |
| 1858 | fdctrl->data_len = 4; |
| 1859 | } |
| 1860 | } |
| 1861 | } |
| 1862 | |
| 1863 | static void fdctrl_handle_lock(FDCtrl *fdctrl, int direction) |
| 1864 | { |
| 1865 | fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0; |
| 1866 | fdctrl->fifo[0] = fdctrl->lock << 4; |
| 1867 | fdctrl_to_result_phase(fdctrl, 1); |
| 1868 | } |
| 1869 | |
| 1870 | static void fdctrl_handle_dumpreg(FDCtrl *fdctrl, int direction) |
| 1871 | { |
| 1872 | FDrive *cur_drv = get_cur_drv(fdctrl); |
| 1873 | |
| 1874 | /* Drives position */ |
| 1875 | fdctrl->fifo[0] = drv0(fdctrl)->track; |
| 1876 | fdctrl->fifo[1] = drv1(fdctrl)->track; |
| 1877 | #if MAX_FD == 4 |
| 1878 | fdctrl->fifo[2] = drv2(fdctrl)->track; |
| 1879 | fdctrl->fifo[3] = drv3(fdctrl)->track; |
| 1880 | #else |
| 1881 | fdctrl->fifo[2] = 0; |
| 1882 | fdctrl->fifo[3] = 0; |
| 1883 | #endif |
| 1884 | /* timers */ |
| 1885 | fdctrl->fifo[4] = fdctrl->timer0; |
| 1886 | fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0); |
| 1887 | fdctrl->fifo[6] = cur_drv->last_sect; |
| 1888 | fdctrl->fifo[7] = (fdctrl->lock << 7) | |
| 1889 | (cur_drv->perpendicular << 2); |
| 1890 | fdctrl->fifo[8] = fdctrl->config; |
| 1891 | fdctrl->fifo[9] = fdctrl->precomp_trk; |
| 1892 | fdctrl_to_result_phase(fdctrl, 10); |
| 1893 | } |
| 1894 | |
| 1895 | static void fdctrl_handle_version(FDCtrl *fdctrl, int direction) |
| 1896 | { |
| 1897 | /* Controller's version */ |
| 1898 | fdctrl->fifo[0] = fdctrl->version; |
| 1899 | fdctrl_to_result_phase(fdctrl, 1); |
| 1900 | } |
| 1901 | |
| 1902 | static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction) |
| 1903 | { |
| 1904 | fdctrl->fifo[0] = 0x41; /* Stepping 1 */ |
| 1905 | fdctrl_to_result_phase(fdctrl, 1); |
| 1906 | } |
| 1907 | |
| 1908 | static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction) |
| 1909 | { |
| 1910 | FDrive *cur_drv = get_cur_drv(fdctrl); |
| 1911 | |
| 1912 | /* Drives position */ |
| 1913 | drv0(fdctrl)->track = fdctrl->fifo[3]; |
| 1914 | drv1(fdctrl)->track = fdctrl->fifo[4]; |
| 1915 | #if MAX_FD == 4 |
| 1916 | drv2(fdctrl)->track = fdctrl->fifo[5]; |
| 1917 | drv3(fdctrl)->track = fdctrl->fifo[6]; |
| 1918 | #endif |
| 1919 | /* timers */ |
| 1920 | fdctrl->timer0 = fdctrl->fifo[7]; |
| 1921 | fdctrl->timer1 = fdctrl->fifo[8]; |
| 1922 | cur_drv->last_sect = fdctrl->fifo[9]; |
| 1923 | fdctrl->lock = fdctrl->fifo[10] >> 7; |
| 1924 | cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF; |
| 1925 | fdctrl->config = fdctrl->fifo[11]; |
| 1926 | fdctrl->precomp_trk = fdctrl->fifo[12]; |
| 1927 | fdctrl->pwrd = fdctrl->fifo[13]; |
| 1928 | fdctrl_to_command_phase(fdctrl); |
| 1929 | } |
| 1930 | |
| 1931 | static void fdctrl_handle_save(FDCtrl *fdctrl, int direction) |
| 1932 | { |
| 1933 | FDrive *cur_drv = get_cur_drv(fdctrl); |
| 1934 | |
| 1935 | fdctrl->fifo[0] = 0; |
| 1936 | fdctrl->fifo[1] = 0; |
| 1937 | /* Drives position */ |
| 1938 | fdctrl->fifo[2] = drv0(fdctrl)->track; |
| 1939 | fdctrl->fifo[3] = drv1(fdctrl)->track; |
| 1940 | #if MAX_FD == 4 |
| 1941 | fdctrl->fifo[4] = drv2(fdctrl)->track; |
| 1942 | fdctrl->fifo[5] = drv3(fdctrl)->track; |
| 1943 | #else |
| 1944 | fdctrl->fifo[4] = 0; |
| 1945 | fdctrl->fifo[5] = 0; |
| 1946 | #endif |
| 1947 | /* timers */ |
| 1948 | fdctrl->fifo[6] = fdctrl->timer0; |
| 1949 | fdctrl->fifo[7] = fdctrl->timer1; |
| 1950 | fdctrl->fifo[8] = cur_drv->last_sect; |
| 1951 | fdctrl->fifo[9] = (fdctrl->lock << 7) | |
| 1952 | (cur_drv->perpendicular << 2); |
| 1953 | fdctrl->fifo[10] = fdctrl->config; |
| 1954 | fdctrl->fifo[11] = fdctrl->precomp_trk; |
| 1955 | fdctrl->fifo[12] = fdctrl->pwrd; |
| 1956 | fdctrl->fifo[13] = 0; |
| 1957 | fdctrl->fifo[14] = 0; |
| 1958 | fdctrl_to_result_phase(fdctrl, 15); |
| 1959 | } |
| 1960 | |
| 1961 | static void fdctrl_handle_readid(FDCtrl *fdctrl, int direction) |
| 1962 | { |
| 1963 | FDrive *cur_drv; |
| 1964 | |
| 1965 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 1966 | cur_drv = get_cur_drv(fdctrl); |
| 1967 | |
| 1968 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; |
| 1969 | timer_mod(fdctrl->result_timer, qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + |
| 1970 | (NANOSECONDS_PER_SECOND / 50)); |
| 1971 | } |
| 1972 | |
| 1973 | static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction) |
| 1974 | { |
| 1975 | FDrive *cur_drv; |
| 1976 | |
| 1977 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 1978 | cur_drv = get_cur_drv(fdctrl); |
| 1979 | fdctrl->data_state |= FD_STATE_FORMAT; |
| 1980 | if (fdctrl->fifo[0] & 0x80) |
| 1981 | fdctrl->data_state |= FD_STATE_MULTI; |
| 1982 | else |
| 1983 | fdctrl->data_state &= ~FD_STATE_MULTI; |
| 1984 | cur_drv->bps = |
| 1985 | fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2]; |
| 1986 | #if 0 |
| 1987 | cur_drv->last_sect = |
| 1988 | cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] : |
| 1989 | fdctrl->fifo[3] / 2; |
| 1990 | #else |
| 1991 | cur_drv->last_sect = fdctrl->fifo[3]; |
| 1992 | #endif |
| 1993 | /* TODO: implement format using DMA expected by the Bochs BIOS |
| 1994 | * and Linux fdformat (read 3 bytes per sector via DMA and fill |
| 1995 | * the sector with the specified fill byte |
| 1996 | */ |
| 1997 | fdctrl->data_state &= ~FD_STATE_FORMAT; |
| 1998 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
| 1999 | } |
| 2000 | |
| 2001 | static void fdctrl_handle_specify(FDCtrl *fdctrl, int direction) |
| 2002 | { |
| 2003 | fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF; |
| 2004 | fdctrl->timer1 = fdctrl->fifo[2] >> 1; |
| 2005 | if (fdctrl->fifo[2] & 1) |
| 2006 | fdctrl->dor &= ~FD_DOR_DMAEN; |
| 2007 | else |
| 2008 | fdctrl->dor |= FD_DOR_DMAEN; |
| 2009 | /* No result back */ |
| 2010 | fdctrl_to_command_phase(fdctrl); |
| 2011 | } |
| 2012 | |
| 2013 | static void fdctrl_handle_sense_drive_status(FDCtrl *fdctrl, int direction) |
| 2014 | { |
| 2015 | FDrive *cur_drv; |
| 2016 | |
| 2017 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 2018 | cur_drv = get_cur_drv(fdctrl); |
| 2019 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; |
| 2020 | /* 1 Byte status back */ |
| 2021 | fdctrl->fifo[0] = (cur_drv->ro << 6) | |
| 2022 | (cur_drv->track == 0 ? 0x10 : 0x00) | |
| 2023 | (cur_drv->head << 2) | |
| 2024 | GET_CUR_DRV(fdctrl) | |
| 2025 | 0x28; |
| 2026 | fdctrl_to_result_phase(fdctrl, 1); |
| 2027 | } |
| 2028 | |
| 2029 | static void fdctrl_handle_recalibrate(FDCtrl *fdctrl, int direction) |
| 2030 | { |
| 2031 | FDrive *cur_drv; |
| 2032 | |
| 2033 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 2034 | cur_drv = get_cur_drv(fdctrl); |
| 2035 | fd_recalibrate(cur_drv); |
| 2036 | fdctrl_to_command_phase(fdctrl); |
| 2037 | /* Raise Interrupt */ |
| 2038 | fdctrl->status0 |= FD_SR0_SEEK; |
| 2039 | fdctrl_raise_irq(fdctrl); |
| 2040 | } |
| 2041 | |
| 2042 | static void fdctrl_handle_sense_interrupt_status(FDCtrl *fdctrl, int direction) |
| 2043 | { |
| 2044 | FDrive *cur_drv = get_cur_drv(fdctrl); |
| 2045 | |
| 2046 | if (fdctrl->reset_sensei > 0) { |
| 2047 | fdctrl->fifo[0] = |
| 2048 | FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei; |
| 2049 | fdctrl->reset_sensei--; |
| 2050 | } else if (!(fdctrl->sra & FD_SRA_INTPEND)) { |
| 2051 | fdctrl->fifo[0] = FD_SR0_INVCMD; |
| 2052 | fdctrl_to_result_phase(fdctrl, 1); |
| 2053 | return; |
| 2054 | } else { |
| 2055 | fdctrl->fifo[0] = |
| 2056 | (fdctrl->status0 & ~(FD_SR0_HEAD | FD_SR0_DS1 | FD_SR0_DS0)) |
| 2057 | | GET_CUR_DRV(fdctrl); |
| 2058 | } |
| 2059 | |
| 2060 | fdctrl->fifo[1] = cur_drv->track; |
| 2061 | fdctrl_to_result_phase(fdctrl, 2); |
| 2062 | fdctrl_reset_irq(fdctrl); |
| 2063 | fdctrl->status0 = FD_SR0_RDYCHG; |
| 2064 | } |
| 2065 | |
| 2066 | static void fdctrl_handle_seek(FDCtrl *fdctrl, int direction) |
| 2067 | { |
| 2068 | FDrive *cur_drv; |
| 2069 | |
| 2070 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 2071 | cur_drv = get_cur_drv(fdctrl); |
| 2072 | fdctrl_to_command_phase(fdctrl); |
| 2073 | /* The seek command just sends step pulses to the drive and doesn't care if |
| 2074 | * there is a medium inserted of if it's banging the head against the drive. |
| 2075 | */ |
| 2076 | fd_seek(cur_drv, cur_drv->head, fdctrl->fifo[2], cur_drv->sect, 1); |
| 2077 | /* Raise Interrupt */ |
| 2078 | fdctrl->status0 |= FD_SR0_SEEK; |
| 2079 | fdctrl_raise_irq(fdctrl); |
| 2080 | } |
| 2081 | |
| 2082 | static void fdctrl_handle_perpendicular_mode(FDCtrl *fdctrl, int direction) |
| 2083 | { |
| 2084 | FDrive *cur_drv = get_cur_drv(fdctrl); |
| 2085 | |
| 2086 | if (fdctrl->fifo[1] & 0x80) |
| 2087 | cur_drv->perpendicular = fdctrl->fifo[1] & 0x7; |
| 2088 | /* No result back */ |
| 2089 | fdctrl_to_command_phase(fdctrl); |
| 2090 | } |
| 2091 | |
| 2092 | static void fdctrl_handle_configure(FDCtrl *fdctrl, int direction) |
| 2093 | { |
| 2094 | fdctrl->config = fdctrl->fifo[2]; |
| 2095 | fdctrl->precomp_trk = fdctrl->fifo[3]; |
| 2096 | /* No result back */ |
| 2097 | fdctrl_to_command_phase(fdctrl); |
| 2098 | } |
| 2099 | |
| 2100 | static void fdctrl_handle_powerdown_mode(FDCtrl *fdctrl, int direction) |
| 2101 | { |
| 2102 | fdctrl->pwrd = fdctrl->fifo[1]; |
| 2103 | fdctrl->fifo[0] = fdctrl->fifo[1]; |
| 2104 | fdctrl_to_result_phase(fdctrl, 1); |
| 2105 | } |
| 2106 | |
| 2107 | static void fdctrl_handle_option(FDCtrl *fdctrl, int direction) |
| 2108 | { |
| 2109 | /* No result back */ |
| 2110 | fdctrl_to_command_phase(fdctrl); |
| 2111 | } |
| 2112 | |
| 2113 | static void fdctrl_handle_drive_specification_command(FDCtrl *fdctrl, int direction) |
| 2114 | { |
| 2115 | FDrive *cur_drv = get_cur_drv(fdctrl); |
| 2116 | uint32_t pos; |
| 2117 | |
| 2118 | pos = fdctrl->data_pos - 1; |
| 2119 | pos %= FD_SECTOR_LEN; |
| 2120 | if (fdctrl->fifo[pos] & 0x80) { |
| 2121 | /* Command parameters done */ |
| 2122 | if (fdctrl->fifo[pos] & 0x40) { |
| 2123 | fdctrl->fifo[0] = fdctrl->fifo[1]; |
| 2124 | fdctrl->fifo[2] = 0; |
| 2125 | fdctrl->fifo[3] = 0; |
| 2126 | fdctrl_to_result_phase(fdctrl, 4); |
| 2127 | } else { |
| 2128 | fdctrl_to_command_phase(fdctrl); |
| 2129 | } |
| 2130 | } else if (fdctrl->data_len > 7) { |
| 2131 | /* ERROR */ |
| 2132 | fdctrl->fifo[0] = 0x80 | |
| 2133 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl); |
| 2134 | fdctrl_to_result_phase(fdctrl, 1); |
| 2135 | } |
| 2136 | } |
| 2137 | |
| 2138 | static void fdctrl_handle_relative_seek_in(FDCtrl *fdctrl, int direction) |
| 2139 | { |
| 2140 | FDrive *cur_drv; |
| 2141 | |
| 2142 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 2143 | cur_drv = get_cur_drv(fdctrl); |
| 2144 | if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) { |
| 2145 | fd_seek(cur_drv, cur_drv->head, cur_drv->max_track - 1, |
| 2146 | cur_drv->sect, 1); |
| 2147 | } else { |
| 2148 | fd_seek(cur_drv, cur_drv->head, |
| 2149 | cur_drv->track + fdctrl->fifo[2], cur_drv->sect, 1); |
| 2150 | } |
| 2151 | fdctrl_to_command_phase(fdctrl); |
| 2152 | /* Raise Interrupt */ |
| 2153 | fdctrl->status0 |= FD_SR0_SEEK; |
| 2154 | fdctrl_raise_irq(fdctrl); |
| 2155 | } |
| 2156 | |
| 2157 | static void fdctrl_handle_relative_seek_out(FDCtrl *fdctrl, int direction) |
| 2158 | { |
| 2159 | FDrive *cur_drv; |
| 2160 | |
| 2161 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
| 2162 | cur_drv = get_cur_drv(fdctrl); |
| 2163 | if (fdctrl->fifo[2] > cur_drv->track) { |
| 2164 | fd_seek(cur_drv, cur_drv->head, 0, cur_drv->sect, 1); |
| 2165 | } else { |
| 2166 | fd_seek(cur_drv, cur_drv->head, |
| 2167 | cur_drv->track - fdctrl->fifo[2], cur_drv->sect, 1); |
| 2168 | } |
| 2169 | fdctrl_to_command_phase(fdctrl); |
| 2170 | /* Raise Interrupt */ |
| 2171 | fdctrl->status0 |= FD_SR0_SEEK; |
| 2172 | fdctrl_raise_irq(fdctrl); |
| 2173 | } |
| 2174 | |
| 2175 | /* |
| 2176 | * Handlers for the execution phase of each command |
| 2177 | */ |
| 2178 | typedef struct FDCtrlCommand { |
| 2179 | uint8_t value; |
| 2180 | uint8_t mask; |
| 2181 | const char* name; |
| 2182 | int parameters; |
| 2183 | void (*handler)(FDCtrl *fdctrl, int direction); |
| 2184 | int direction; |
| 2185 | } FDCtrlCommand; |
| 2186 | |
| 2187 | static const FDCtrlCommand handlers[] = { |
| 2188 | { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ }, |
| 2189 | { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE }, |
| 2190 | { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek }, |
| 2191 | { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status }, |
| 2192 | { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate }, |
| 2193 | { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track }, |
| 2194 | { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ }, |
| 2195 | { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */ |
| 2196 | { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */ |
| 2197 | { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ }, |
| 2198 | { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE }, |
| 2199 | { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_start_transfer, FD_DIR_VERIFY }, |
| 2200 | { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL }, |
| 2201 | { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH }, |
| 2202 | { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE }, |
| 2203 | { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid }, |
| 2204 | { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify }, |
| 2205 | { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status }, |
| 2206 | { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode }, |
| 2207 | { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure }, |
| 2208 | { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode }, |
| 2209 | { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option }, |
| 2210 | { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command }, |
| 2211 | { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out }, |
| 2212 | { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented }, |
| 2213 | { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in }, |
| 2214 | { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock }, |
| 2215 | { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg }, |
| 2216 | { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version }, |
| 2217 | { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid }, |
| 2218 | { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */ |
| 2219 | { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */ |
| 2220 | }; |
| 2221 | /* Associate command to an index in the 'handlers' array */ |
| 2222 | static uint8_t command_to_handler[256]; |
| 2223 | |
| 2224 | static const FDCtrlCommand *get_command(uint8_t cmd) |
| 2225 | { |
| 2226 | int idx; |
| 2227 | |
| 2228 | idx = command_to_handler[cmd]; |
| 2229 | FLOPPY_DPRINTF("%s command\n", handlers[idx].name); |
| 2230 | return &handlers[idx]; |
| 2231 | } |
| 2232 | |
| 2233 | static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value) |
| 2234 | { |
| 2235 | FDrive *cur_drv; |
| 2236 | const FDCtrlCommand *cmd; |
| 2237 | uint32_t pos; |
| 2238 | |
| 2239 | /* Reset mode */ |
| 2240 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
| 2241 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
| 2242 | return; |
| 2243 | } |
| 2244 | if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) { |
| 2245 | FLOPPY_DPRINTF("error: controller not ready for writing\n"); |
| 2246 | return; |
| 2247 | } |
| 2248 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
| 2249 | |
| 2250 | FLOPPY_DPRINTF("%s: %02x\n", __func__, value); |
| 2251 | |
| 2252 | /* If data_len spans multiple sectors, the current position in the FIFO |
| 2253 | * wraps around while fdctrl->data_pos is the real position in the whole |
| 2254 | * request. */ |
| 2255 | pos = fdctrl->data_pos++; |
| 2256 | pos %= FD_SECTOR_LEN; |
| 2257 | fdctrl->fifo[pos] = value; |
| 2258 | |
| 2259 | if (fdctrl->data_pos == fdctrl->data_len) { |
| 2260 | fdctrl->msr &= ~FD_MSR_RQM; |
| 2261 | } |
| 2262 | |
| 2263 | switch (fdctrl->phase) { |
| 2264 | case FD_PHASE_EXECUTION: |
| 2265 | /* For DMA requests, RQM should be cleared during execution phase, so |
| 2266 | * we would have errored out above. */ |
| 2267 | assert(fdctrl->msr & FD_MSR_NONDMA); |
| 2268 | |
| 2269 | /* FIFO data write */ |
| 2270 | if (pos == FD_SECTOR_LEN - 1 || |
| 2271 | fdctrl->data_pos == fdctrl->data_len) { |
| 2272 | cur_drv = get_cur_drv(fdctrl); |
| 2273 | if (blk_pwrite(cur_drv->blk, fd_offset(cur_drv), BDRV_SECTOR_SIZE, |
| 2274 | fdctrl->fifo, 0) < 0) { |
| 2275 | FLOPPY_DPRINTF("error writing sector %d\n", |
| 2276 | fd_sector(cur_drv)); |
| 2277 | break; |
| 2278 | } |
| 2279 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { |
| 2280 | FLOPPY_DPRINTF("error seeking to next sector %d\n", |
| 2281 | fd_sector(cur_drv)); |
| 2282 | break; |
| 2283 | } |
| 2284 | } |
| 2285 | |
| 2286 | /* Switch to result phase when done with the transfer */ |
| 2287 | if (fdctrl->data_pos == fdctrl->data_len) { |
| 2288 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
| 2289 | } |
| 2290 | break; |
| 2291 | |
| 2292 | case FD_PHASE_COMMAND: |
| 2293 | assert(!(fdctrl->msr & FD_MSR_NONDMA)); |
| 2294 | assert(fdctrl->data_pos < FD_SECTOR_LEN); |
| 2295 | |
| 2296 | if (pos == 0) { |
| 2297 | /* The first byte specifies the command. Now we start reading |
| 2298 | * as many parameters as this command requires. */ |
| 2299 | cmd = get_command(value); |
| 2300 | fdctrl->data_len = cmd->parameters + 1; |
| 2301 | if (cmd->parameters) { |
| 2302 | fdctrl->msr |= FD_MSR_RQM; |
| 2303 | } |
| 2304 | fdctrl->msr |= FD_MSR_CMDBUSY; |
| 2305 | } |
| 2306 | |
| 2307 | if (fdctrl->data_pos == fdctrl->data_len) { |
| 2308 | /* We have all parameters now, execute the command */ |
| 2309 | fdctrl->phase = FD_PHASE_EXECUTION; |
| 2310 | |
| 2311 | if (fdctrl->data_state & FD_STATE_FORMAT) { |
| 2312 | fdctrl_format_sector(fdctrl); |
| 2313 | break; |
| 2314 | } |
| 2315 | |
| 2316 | cmd = get_command(fdctrl->fifo[0]); |
| 2317 | FLOPPY_DPRINTF("Calling handler for '%s'\n", cmd->name); |
| 2318 | cmd->handler(fdctrl, cmd->direction); |
| 2319 | } |
| 2320 | break; |
| 2321 | |
| 2322 | case FD_PHASE_RESULT: |
| 2323 | default: |
| 2324 | abort(); |
| 2325 | } |
| 2326 | } |
| 2327 | |
| 2328 | static void fdctrl_result_timer(void *opaque) |
| 2329 | { |
| 2330 | FDCtrl *fdctrl = opaque; |
| 2331 | FDrive *cur_drv = get_cur_drv(fdctrl); |
| 2332 | |
| 2333 | /* |
| 2334 | * An empty drive has no address marks to read. Completing READ ID |
| 2335 | * successfully, with the made-up sector ID left over from the "spinning" |
| 2336 | * emulation below, tells the guest that a diskette is still present after |
| 2337 | * it has been ejected. The only error path left was a data rate mismatch, |
| 2338 | * and media_rate is never reset when the medium is removed. |
| 2339 | */ |
| 2340 | if (!fd_media_present(cur_drv)) { |
| 2341 | FLOPPY_DPRINTF("read id on empty drive\n"); |
| 2342 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); |
| 2343 | return; |
| 2344 | } |
| 2345 | /* Pretend we are spinning. |
| 2346 | * This is needed for Coherent, which uses READ ID to check for |
| 2347 | * sector interleaving. |
| 2348 | */ |
| 2349 | if (cur_drv->last_sect != 0) { |
| 2350 | cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1; |
| 2351 | } |
| 2352 | /* READ_ID can't automatically succeed! */ |
| 2353 | if ((fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { |
| 2354 | FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n", |
| 2355 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); |
| 2356 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); |
| 2357 | } else { |
| 2358 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
| 2359 | } |
| 2360 | } |
| 2361 | |
| 2362 | /* Init functions */ |
| 2363 | |
| 2364 | void fdctrl_init_drives(FloppyBus *bus, DriveInfo **fds) |
| 2365 | { |
| 2366 | DeviceState *dev; |
| 2367 | int i; |
| 2368 | |
| 2369 | for (i = 0; i < MAX_FD; i++) { |
| 2370 | if (fds[i]) { |
| 2371 | dev = qdev_new("floppy"); |
| 2372 | qdev_prop_set_uint32(dev, "unit", i); |
| 2373 | qdev_prop_set_enum(dev, "drive-type", FLOPPY_DRIVE_TYPE_AUTO); |
| 2374 | qdev_prop_set_drive_err(dev, "drive", blk_by_legacy_dinfo(fds[i]), |
| 2375 | &error_fatal); |
| 2376 | qdev_realize_and_unref(dev, &bus->bus, &error_fatal); |
| 2377 | } |
| 2378 | } |
| 2379 | } |
| 2380 | |
| 2381 | void fdctrl_realize_common(DeviceState *dev, FDCtrl *fdctrl, Error **errp) |
| 2382 | { |
| 2383 | int i, j; |
| 2384 | FDrive *drive; |
| 2385 | static int command_tables_inited = 0; |
| 2386 | |
| 2387 | if (fdctrl->fallback == FLOPPY_DRIVE_TYPE_AUTO) { |
| 2388 | error_setg(errp, "Cannot choose a fallback FDrive type of 'auto'"); |
| 2389 | return; |
| 2390 | } |
| 2391 | |
| 2392 | /* Fill 'command_to_handler' lookup table */ |
| 2393 | if (!command_tables_inited) { |
| 2394 | command_tables_inited = 1; |
| 2395 | for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) { |
| 2396 | for (j = 0; j < sizeof(command_to_handler); j++) { |
| 2397 | if ((j & handlers[i].mask) == handlers[i].value) { |
| 2398 | command_to_handler[j] = i; |
| 2399 | } |
| 2400 | } |
| 2401 | } |
| 2402 | } |
| 2403 | |
| 2404 | FLOPPY_DPRINTF("init controller\n"); |
| 2405 | fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN); |
| 2406 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); |
| 2407 | fdctrl->fifo_size = 512; |
| 2408 | fdctrl->result_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
| 2409 | fdctrl_result_timer, fdctrl); |
| 2410 | |
| 2411 | fdctrl->version = 0x90; /* Intel 82078 controller */ |
| 2412 | fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */ |
| 2413 | fdctrl->num_floppies = MAX_FD; |
| 2414 | |
| 2415 | floppy_bus_create(fdctrl, &fdctrl->bus, dev); |
| 2416 | |
| 2417 | for (i = 0; i < MAX_FD; i++) { |
| 2418 | drive = &fdctrl->drives[i]; |
| 2419 | drive->fdctrl = fdctrl; |
| 2420 | fd_init(drive); |
| 2421 | fd_revalidate(drive); |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | static void fdc_register_types(void) |
| 2426 | { |
| 2427 | type_register_static(&floppy_bus_info); |
| 2428 | type_register_static(&floppy_drive_info); |
| 2429 | } |
| 2430 | |
| 2431 | type_init(fdc_register_types) |