Statistics
| Branch: | Revision:

root / hw / fdc.c @ b4e3104b

History | View | Annotate | Download (64.9 kB)

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

    
34
/********************************************************/
35
/* debug Floppy devices */
36
//#define DEBUG_FLOPPY
37

    
38
#ifdef DEBUG_FLOPPY
39
#define FLOPPY_DPRINTF(fmt, args...) \
40
do { printf("FLOPPY: " fmt , ##args); } while (0)
41
#else
42
#define FLOPPY_DPRINTF(fmt, args...)
43
#endif
44

    
45
#define FLOPPY_ERROR(fmt, args...) \
46
do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ##args); } while (0)
47

    
48
/********************************************************/
49
/* Floppy drive emulation                               */
50

    
51
/* Will always be a fixed parameter for us */
52
#define FD_SECTOR_LEN 512
53
#define FD_SECTOR_SC  2   /* Sector size code */
54

    
55
/* Floppy disk drive emulation */
56
typedef enum fdisk_type_t {
57
    FDRIVE_DISK_288   = 0x01, /* 2.88 MB disk           */
58
    FDRIVE_DISK_144   = 0x02, /* 1.44 MB disk           */
59
    FDRIVE_DISK_720   = 0x03, /* 720 kB disk            */
60
    FDRIVE_DISK_USER  = 0x04, /* User defined geometry  */
61
    FDRIVE_DISK_NONE  = 0x05, /* No disk                */
62
} fdisk_type_t;
63

    
64
typedef enum fdrive_type_t {
65
    FDRIVE_DRV_144  = 0x00,   /* 1.44 MB 3"5 drive      */
66
    FDRIVE_DRV_288  = 0x01,   /* 2.88 MB 3"5 drive      */
67
    FDRIVE_DRV_120  = 0x02,   /* 1.2  MB 5"25 drive     */
68
    FDRIVE_DRV_NONE = 0x03,   /* No drive connected     */
69
} fdrive_type_t;
70

    
71
typedef enum fdrive_flags_t {
72
    FDRIVE_MOTOR_ON   = 0x01, /* motor on/off           */
73
} fdrive_flags_t;
74

    
75
typedef enum fdisk_flags_t {
76
    FDISK_DBL_SIDES  = 0x01,
77
} fdisk_flags_t;
78

    
79
typedef struct fdrive_t {
80
    BlockDriverState *bs;
81
    /* Drive status */
82
    fdrive_type_t drive;
83
    fdrive_flags_t drflags;
84
    uint8_t perpendicular;    /* 2.88 MB access mode    */
85
    /* Position */
86
    uint8_t head;
87
    uint8_t track;
88
    uint8_t sect;
89
    /* Last operation status */
90
    uint8_t dir;              /* Direction              */
91
    uint8_t rw;               /* Read/write             */
92
    /* Media */
93
    fdisk_flags_t flags;
94
    uint8_t last_sect;        /* Nb sector per track    */
95
    uint8_t max_track;        /* Nb of tracks           */
96
    uint16_t bps;             /* Bytes per sector       */
97
    uint8_t ro;               /* Is read-only           */
98
} fdrive_t;
99

    
100
static void fd_init (fdrive_t *drv, BlockDriverState *bs)
101
{
102
    /* Drive */
103
    drv->bs = bs;
104
    drv->drive = FDRIVE_DRV_NONE;
105
    drv->drflags = 0;
106
    drv->perpendicular = 0;
107
    /* Disk */
108
    drv->last_sect = 0;
109
    drv->max_track = 0;
110
}
111

    
112
static int _fd_sector (uint8_t head, uint8_t track,
113
                       uint8_t sect, uint8_t last_sect)
114
{
115
    return (((track * 2) + head) * last_sect) + sect - 1;
116
}
117

    
118
/* Returns current position, in sectors, for given drive */
119
static int fd_sector (fdrive_t *drv)
120
{
121
    return _fd_sector(drv->head, drv->track, drv->sect, drv->last_sect);
122
}
123

    
124
static int fd_seek (fdrive_t *drv, uint8_t head, uint8_t track, uint8_t sect,
125
                    int enable_seek)
126
{
127
    uint32_t sector;
128
    int ret;
129

    
130
    if (track > drv->max_track ||
131
        (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) {
132
        FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
133
                       head, track, sect, 1,
134
                       (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
135
                       drv->max_track, drv->last_sect);
136
        return 2;
137
    }
138
    if (sect > drv->last_sect) {
139
        FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n",
140
                       head, track, sect, 1,
141
                       (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1,
142
                       drv->max_track, drv->last_sect);
143
        return 3;
144
    }
145
    sector = _fd_sector(head, track, sect, drv->last_sect);
146
    ret = 0;
147
    if (sector != fd_sector(drv)) {
148
#if 0
149
        if (!enable_seek) {
150
            FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n",
151
                         head, track, sect, 1, drv->max_track, drv->last_sect);
152
            return 4;
153
        }
154
#endif
155
        drv->head = head;
156
        if (drv->track != track)
157
            ret = 1;
158
        drv->track = track;
159
        drv->sect = sect;
160
    }
161

    
162
    return ret;
163
}
164

    
165
/* Set drive back to track 0 */
166
static void fd_recalibrate (fdrive_t *drv)
167
{
168
    FLOPPY_DPRINTF("recalibrate\n");
169
    drv->head = 0;
170
    drv->track = 0;
171
    drv->sect = 1;
172
    drv->dir = 1;
173
    drv->rw = 0;
174
}
175

    
176
/* Recognize floppy formats */
177
typedef struct fd_format_t {
178
    fdrive_type_t drive;
179
    fdisk_type_t  disk;
180
    uint8_t last_sect;
181
    uint8_t max_track;
182
    uint8_t max_head;
183
    const char *str;
184
} fd_format_t;
185

    
186
static const fd_format_t fd_formats[] = {
187
    /* First entry is default format */
188
    /* 1.44 MB 3"1/2 floppy disks */
189
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 18, 80, 1, "1.44 MB 3\"1/2", },
190
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 20, 80, 1,  "1.6 MB 3\"1/2", },
191
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 80, 1, "1.68 MB 3\"1/2", },
192
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 82, 1, "1.72 MB 3\"1/2", },
193
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 21, 83, 1, "1.74 MB 3\"1/2", },
194
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 22, 80, 1, "1.76 MB 3\"1/2", },
195
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 23, 80, 1, "1.84 MB 3\"1/2", },
196
    { FDRIVE_DRV_144, FDRIVE_DISK_144, 24, 80, 1, "1.92 MB 3\"1/2", },
197
    /* 2.88 MB 3"1/2 floppy disks */
198
    { FDRIVE_DRV_288, FDRIVE_DISK_288, 36, 80, 1, "2.88 MB 3\"1/2", },
199
    { FDRIVE_DRV_288, FDRIVE_DISK_288, 39, 80, 1, "3.12 MB 3\"1/2", },
200
    { FDRIVE_DRV_288, FDRIVE_DISK_288, 40, 80, 1,  "3.2 MB 3\"1/2", },
201
    { FDRIVE_DRV_288, FDRIVE_DISK_288, 44, 80, 1, "3.52 MB 3\"1/2", },
202
    { FDRIVE_DRV_288, FDRIVE_DISK_288, 48, 80, 1, "3.84 MB 3\"1/2", },
203
    /* 720 kB 3"1/2 floppy disks */
204
    { FDRIVE_DRV_144, FDRIVE_DISK_720,  9, 80, 1,  "720 kB 3\"1/2", },
205
    { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 80, 1,  "800 kB 3\"1/2", },
206
    { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 82, 1,  "820 kB 3\"1/2", },
207
    { FDRIVE_DRV_144, FDRIVE_DISK_720, 10, 83, 1,  "830 kB 3\"1/2", },
208
    { FDRIVE_DRV_144, FDRIVE_DISK_720, 13, 80, 1, "1.04 MB 3\"1/2", },
209
    { FDRIVE_DRV_144, FDRIVE_DISK_720, 14, 80, 1, "1.12 MB 3\"1/2", },
210
    /* 1.2 MB 5"1/4 floppy disks */
211
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 15, 80, 1,  "1.2 kB 5\"1/4", },
212
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 80, 1, "1.44 MB 5\"1/4", },
213
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 82, 1, "1.48 MB 5\"1/4", },
214
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 18, 83, 1, "1.49 MB 5\"1/4", },
215
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 20, 80, 1,  "1.6 MB 5\"1/4", },
216
    /* 720 kB 5"1/4 floppy disks */
217
    { FDRIVE_DRV_120, FDRIVE_DISK_288,  9, 80, 1,  "720 kB 5\"1/4", },
218
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 11, 80, 1,  "880 kB 5\"1/4", },
219
    /* 360 kB 5"1/4 floppy disks */
220
    { FDRIVE_DRV_120, FDRIVE_DISK_288,  9, 40, 1,  "360 kB 5\"1/4", },
221
    { FDRIVE_DRV_120, FDRIVE_DISK_288,  9, 40, 0,  "180 kB 5\"1/4", },
222
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 10, 41, 1,  "410 kB 5\"1/4", },
223
    { FDRIVE_DRV_120, FDRIVE_DISK_288, 10, 42, 1,  "420 kB 5\"1/4", },
224
    /* 320 kB 5"1/4 floppy disks */
225
    { FDRIVE_DRV_120, FDRIVE_DISK_288,  8, 40, 1,  "320 kB 5\"1/4", },
226
    { FDRIVE_DRV_120, FDRIVE_DISK_288,  8, 40, 0,  "160 kB 5\"1/4", },
227
    /* 360 kB must match 5"1/4 better than 3"1/2... */
228
    { FDRIVE_DRV_144, FDRIVE_DISK_720,  9, 80, 0,  "360 kB 3\"1/2", },
229
    /* end */
230
    { FDRIVE_DRV_NONE, FDRIVE_DISK_NONE, -1, -1, 0, NULL, },
231
};
232

    
233
/* Revalidate a disk drive after a disk change */
234
static void fd_revalidate (fdrive_t *drv)
235
{
236
    const fd_format_t *parse;
237
    uint64_t nb_sectors, size;
238
    int i, first_match, match;
239
    int nb_heads, max_track, last_sect, ro;
240

    
241
    FLOPPY_DPRINTF("revalidate\n");
242
    if (drv->bs != NULL && bdrv_is_inserted(drv->bs)) {
243
        ro = bdrv_is_read_only(drv->bs);
244
        bdrv_get_geometry_hint(drv->bs, &nb_heads, &max_track, &last_sect);
245
        if (nb_heads != 0 && max_track != 0 && last_sect != 0) {
246
            FLOPPY_DPRINTF("User defined disk (%d %d %d)",
247
                           nb_heads - 1, max_track, last_sect);
248
        } else {
249
            bdrv_get_geometry(drv->bs, &nb_sectors);
250
            match = -1;
251
            first_match = -1;
252
            for (i = 0;; i++) {
253
                parse = &fd_formats[i];
254
                if (parse->drive == FDRIVE_DRV_NONE)
255
                    break;
256
                if (drv->drive == parse->drive ||
257
                    drv->drive == FDRIVE_DRV_NONE) {
258
                    size = (parse->max_head + 1) * parse->max_track *
259
                        parse->last_sect;
260
                    if (nb_sectors == size) {
261
                        match = i;
262
                        break;
263
                    }
264
                    if (first_match == -1)
265
                        first_match = i;
266
                }
267
            }
268
            if (match == -1) {
269
                if (first_match == -1)
270
                    match = 1;
271
                else
272
                    match = first_match;
273
                parse = &fd_formats[match];
274
            }
275
            nb_heads = parse->max_head + 1;
276
            max_track = parse->max_track;
277
            last_sect = parse->last_sect;
278
            drv->drive = parse->drive;
279
            FLOPPY_DPRINTF("%s floppy disk (%d h %d t %d s) %s\n", parse->str,
280
                           nb_heads, max_track, last_sect, ro ? "ro" : "rw");
281
        }
282
        if (nb_heads == 1) {
283
            drv->flags &= ~FDISK_DBL_SIDES;
284
        } else {
285
            drv->flags |= FDISK_DBL_SIDES;
286
        }
287
        drv->max_track = max_track;
288
        drv->last_sect = last_sect;
289
        drv->ro = ro;
290
    } else {
291
        FLOPPY_DPRINTF("No disk in drive\n");
292
        drv->last_sect = 0;
293
        drv->max_track = 0;
294
        drv->flags &= ~FDISK_DBL_SIDES;
295
    }
296
}
297

    
298
/* Motor control */
299
static void fd_start (fdrive_t *drv)
300
{
301
    drv->drflags |= FDRIVE_MOTOR_ON;
302
}
303

    
304
static void fd_stop (fdrive_t *drv)
305
{
306
    drv->drflags &= ~FDRIVE_MOTOR_ON;
307
}
308

    
309
/* Re-initialise a drives (motor off, repositioned) */
310
static void fd_reset (fdrive_t *drv)
311
{
312
    fd_stop(drv);
313
    fd_recalibrate(drv);
314
}
315

    
316
/********************************************************/
317
/* Intel 82078 floppy disk controller emulation          */
318

    
319
static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq);
320
static void fdctrl_reset_fifo (fdctrl_t *fdctrl);
321
static int fdctrl_transfer_handler (void *opaque, int nchan,
322
                                    int dma_pos, int dma_len);
323
static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status);
324
static void fdctrl_result_timer(void *opaque);
325

    
326
static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl);
327
static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl);
328
static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value);
329
static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl);
330
static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value);
331
static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl);
332
static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value);
333
static uint32_t fdctrl_read_data (fdctrl_t *fdctrl);
334
static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value);
335
static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl);
336

    
337
enum {
338
    FD_CTRL_ACTIVE = 0x01, /* XXX: suppress that */
339
    FD_CTRL_RESET  = 0x02,
340
    FD_CTRL_SLEEP  = 0x04, /* XXX: suppress that */
341
    FD_CTRL_BUSY   = 0x08, /* dma transfer in progress */
342
    FD_CTRL_INTR   = 0x10,
343
};
344

    
345
enum {
346
    FD_DIR_WRITE   = 0,
347
    FD_DIR_READ    = 1,
348
    FD_DIR_SCANE   = 2,
349
    FD_DIR_SCANL   = 3,
350
    FD_DIR_SCANH   = 4,
351
};
352

    
353
enum {
354
    FD_STATE_CMD    = 0x00,
355
    FD_STATE_STATUS = 0x01,
356
    FD_STATE_DATA   = 0x02,
357
    FD_STATE_STATE  = 0x03,
358
    FD_STATE_MULTI  = 0x10,
359
    FD_STATE_SEEK   = 0x20,
360
    FD_STATE_FORMAT = 0x40,
361
};
362

    
363
enum {
364
    FD_REG_0 = 0x00,
365
    FD_REG_STATUSB = 0x01,
366
    FD_REG_DOR = 0x02,
367
    FD_REG_TDR = 0x03,
368
    FD_REG_MSR = 0x04,
369
    FD_REG_DSR = 0x04,
370
    FD_REG_FIFO = 0x05,
371
    FD_REG_DIR = 0x07,
372
};
373

    
374
enum {
375
    FD_CMD_SPECIFY = 0x03,
376
    FD_CMD_SENSE_DRIVE_STATUS = 0x04,
377
    FD_CMD_RECALIBRATE = 0x07,
378
    FD_CMD_SENSE_INTERRUPT_STATUS = 0x08,
379
    FD_CMD_DUMPREG = 0x0e,
380
    FD_CMD_SEEK = 0x0f,
381
    FD_CMD_VERSION = 0x10,
382
    FD_CMD_PERPENDICULAR_MODE = 0x12,
383
    FD_CMD_CONFIGURE = 0x13,
384
    FD_CMD_UNLOCK = 0x14,
385
    FD_CMD_POWERDOWN_MODE = 0x17,
386
    FD_CMD_PART_ID = 0x18,
387
    FD_CMD_SAVE = 0x2c,
388
    FD_CMD_OPTION = 0x33,
389
    FD_CMD_READ_TRACK = 0x42,
390
    FD_CMD_WRITE = 0x45,
391
    FD_CMD_READ = 0x46,
392
    FD_CMD_WRITE_DELETED = 0x49,
393
    FD_CMD_READ_ID = 0x4a,
394
    FD_CMD_READ_DELETED = 0x4c,
395
    FD_CMD_RESTORE = 0x4c,
396
    FD_CMD_FORMAT_TRACK = 0x4d,
397
    FD_CMD_SCAN_EQUAL = 0x50,
398
    FD_CMD_VERIFY = 0x56,
399
    FD_CMD_SCAN_LOW_OR_EQUAL = 0x59,
400
    FD_CMD_SCAN_HIGH_OR_EQUAL = 0x5d,
401
    FD_CMD_DRIVE_SPECIFICATION_COMMAND = 0x8e,
402
    FD_CMD_RELATIVE_SEEK_OUT = 0x8f,
403
    FD_CMD_LOCK = 0x94,
404
    FD_CMD_FORMAT_AND_WRITE = 0xcd,
405
    FD_CMD_RELATIVE_SEEK_IN = 0xcf,
406
};
407

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

    
416
enum {
417
    FD_SR0_EQPMT    = 0x10,
418
    FD_SR0_SEEK     = 0x20,
419
    FD_SR0_ABNTERM  = 0x40,
420
    FD_SR0_INVCMD   = 0x80,
421
    FD_SR0_RDYCHG   = 0xc0,
422
};
423

    
424
enum {
425
    FD_DOR_SELMASK  = 0x01,
426
    FD_DOR_nRESET   = 0x04,
427
    FD_DOR_DMAEN    = 0x08,
428
    FD_DOR_MOTEN0   = 0x10,
429
    FD_DOR_MOTEN1   = 0x20,
430
    FD_DOR_MOTEN2   = 0x40,
431
    FD_DOR_MOTEN3   = 0x80,
432
};
433

    
434
enum {
435
    FD_TDR_BOOTSEL  = 0x0c,
436
};
437

    
438
enum {
439
    FD_DSR_DRATEMASK= 0x03,
440
    FD_DSR_PWRDOWN  = 0x40,
441
    FD_DSR_SWRESET  = 0x80,
442
};
443

    
444
enum {
445
    FD_MSR_DRV0BUSY = 0x01,
446
    FD_MSR_DRV1BUSY = 0x02,
447
    FD_MSR_DRV2BUSY = 0x04,
448
    FD_MSR_DRV3BUSY = 0x08,
449
    FD_MSR_CMDBUSY  = 0x10,
450
    FD_MSR_NONDMA   = 0x20,
451
    FD_MSR_DIO      = 0x40,
452
    FD_MSR_RQM      = 0x80,
453
};
454

    
455
enum {
456
    FD_DIR_DSKCHG   = 0x80,
457
};
458

    
459
#define FD_STATE(state) ((state) & FD_STATE_STATE)
460
#define FD_SET_STATE(state, new_state) \
461
do { (state) = ((state) & ~FD_STATE_STATE) | (new_state); } while (0)
462
#define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI)
463
#define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK)
464
#define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT)
465

    
466
struct fdctrl_t {
467
    fdctrl_t *fdctrl;
468
    /* Controller's identification */
469
    uint8_t version;
470
    /* HW */
471
    qemu_irq irq;
472
    int dma_chann;
473
    target_phys_addr_t io_base;
474
    /* Controller state */
475
    QEMUTimer *result_timer;
476
    uint8_t state;
477
    uint8_t dma_en;
478
    uint8_t cur_drv;
479
    uint8_t bootsel;
480
    /* Command FIFO */
481
    uint8_t *fifo;
482
    uint32_t data_pos;
483
    uint32_t data_len;
484
    uint8_t data_state;
485
    uint8_t data_dir;
486
    uint8_t int_status;
487
    uint8_t eot; /* last wanted sector */
488
    /* States kept only to be returned back */
489
    /* Timers state */
490
    uint8_t timer0;
491
    uint8_t timer1;
492
    /* precompensation */
493
    uint8_t precomp_trk;
494
    uint8_t config;
495
    uint8_t lock;
496
    /* Power down config (also with status regB access mode */
497
    uint8_t pwrd;
498
    /* Sun4m quirks? */
499
    int sun4m;
500
    /* Floppy drives */
501
    fdrive_t drives[2];
502
};
503

    
504
static uint32_t fdctrl_read (void *opaque, uint32_t reg)
505
{
506
    fdctrl_t *fdctrl = opaque;
507
    uint32_t retval;
508

    
509
    switch (reg & 0x07) {
510
    case FD_REG_0:
511
        if (fdctrl->sun4m) {
512
            // Identify to Linux as S82078B
513
            retval = fdctrl_read_statusB(fdctrl);
514
        } else {
515
            retval = (uint32_t)(-1);
516
        }
517
        break;
518
    case FD_REG_STATUSB:
519
        retval = fdctrl_read_statusB(fdctrl);
520
        break;
521
    case FD_REG_DOR:
522
        retval = fdctrl_read_dor(fdctrl);
523
        break;
524
    case FD_REG_TDR:
525
        retval = fdctrl_read_tape(fdctrl);
526
        break;
527
    case FD_REG_MSR:
528
        retval = fdctrl_read_main_status(fdctrl);
529
        break;
530
    case FD_REG_FIFO:
531
        retval = fdctrl_read_data(fdctrl);
532
        break;
533
    case FD_REG_DIR:
534
        retval = fdctrl_read_dir(fdctrl);
535
        break;
536
    default:
537
        retval = (uint32_t)(-1);
538
        break;
539
    }
540
    FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval);
541

    
542
    return retval;
543
}
544

    
545
static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value)
546
{
547
    fdctrl_t *fdctrl = opaque;
548

    
549
    FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg & 7, value);
550

    
551
    switch (reg & 0x07) {
552
    case FD_REG_DOR:
553
        fdctrl_write_dor(fdctrl, value);
554
        break;
555
    case FD_REG_TDR:
556
        fdctrl_write_tape(fdctrl, value);
557
        break;
558
    case FD_REG_DSR:
559
        fdctrl_write_rate(fdctrl, value);
560
        break;
561
    case FD_REG_FIFO:
562
        fdctrl_write_data(fdctrl, value);
563
        break;
564
    default:
565
        break;
566
    }
567
}
568

    
569
static uint32_t fdctrl_read_mem (void *opaque, target_phys_addr_t reg)
570
{
571
    return fdctrl_read(opaque, (uint32_t)reg);
572
}
573

    
574
static void fdctrl_write_mem (void *opaque,
575
                              target_phys_addr_t reg, uint32_t value)
576
{
577
    fdctrl_write(opaque, (uint32_t)reg, value);
578
}
579

    
580
static CPUReadMemoryFunc *fdctrl_mem_read[3] = {
581
    fdctrl_read_mem,
582
    fdctrl_read_mem,
583
    fdctrl_read_mem,
584
};
585

    
586
static CPUWriteMemoryFunc *fdctrl_mem_write[3] = {
587
    fdctrl_write_mem,
588
    fdctrl_write_mem,
589
    fdctrl_write_mem,
590
};
591

    
592
static CPUReadMemoryFunc *fdctrl_mem_read_strict[3] = {
593
    fdctrl_read_mem,
594
    NULL,
595
    NULL,
596
};
597

    
598
static CPUWriteMemoryFunc *fdctrl_mem_write_strict[3] = {
599
    fdctrl_write_mem,
600
    NULL,
601
    NULL,
602
};
603

    
604
static void fd_save (QEMUFile *f, fdrive_t *fd)
605
{
606
    uint8_t tmp;
607

    
608
    tmp = fd->drflags;
609
    qemu_put_8s(f, &tmp);
610
    qemu_put_8s(f, &fd->head);
611
    qemu_put_8s(f, &fd->track);
612
    qemu_put_8s(f, &fd->sect);
613
    qemu_put_8s(f, &fd->dir);
614
    qemu_put_8s(f, &fd->rw);
615
}
616

    
617
static void fdc_save (QEMUFile *f, void *opaque)
618
{
619
    fdctrl_t *s = opaque;
620

    
621
    qemu_put_8s(f, &s->state);
622
    qemu_put_8s(f, &s->dma_en);
623
    qemu_put_8s(f, &s->cur_drv);
624
    qemu_put_8s(f, &s->bootsel);
625
    qemu_put_buffer(f, s->fifo, FD_SECTOR_LEN);
626
    qemu_put_be32s(f, &s->data_pos);
627
    qemu_put_be32s(f, &s->data_len);
628
    qemu_put_8s(f, &s->data_state);
629
    qemu_put_8s(f, &s->data_dir);
630
    qemu_put_8s(f, &s->int_status);
631
    qemu_put_8s(f, &s->eot);
632
    qemu_put_8s(f, &s->timer0);
633
    qemu_put_8s(f, &s->timer1);
634
    qemu_put_8s(f, &s->precomp_trk);
635
    qemu_put_8s(f, &s->config);
636
    qemu_put_8s(f, &s->lock);
637
    qemu_put_8s(f, &s->pwrd);
638
    fd_save(f, &s->drives[0]);
639
    fd_save(f, &s->drives[1]);
640
}
641

    
642
static int fd_load (QEMUFile *f, fdrive_t *fd)
643
{
644
    uint8_t tmp;
645

    
646
    qemu_get_8s(f, &tmp);
647
    fd->drflags = tmp;
648
    qemu_get_8s(f, &fd->head);
649
    qemu_get_8s(f, &fd->track);
650
    qemu_get_8s(f, &fd->sect);
651
    qemu_get_8s(f, &fd->dir);
652
    qemu_get_8s(f, &fd->rw);
653

    
654
    return 0;
655
}
656

    
657
static int fdc_load (QEMUFile *f, void *opaque, int version_id)
658
{
659
    fdctrl_t *s = opaque;
660
    int ret;
661

    
662
    if (version_id != 1)
663
        return -EINVAL;
664

    
665
    qemu_get_8s(f, &s->state);
666
    qemu_get_8s(f, &s->dma_en);
667
    qemu_get_8s(f, &s->cur_drv);
668
    qemu_get_8s(f, &s->bootsel);
669
    qemu_get_buffer(f, s->fifo, FD_SECTOR_LEN);
670
    qemu_get_be32s(f, &s->data_pos);
671
    qemu_get_be32s(f, &s->data_len);
672
    qemu_get_8s(f, &s->data_state);
673
    qemu_get_8s(f, &s->data_dir);
674
    qemu_get_8s(f, &s->int_status);
675
    qemu_get_8s(f, &s->eot);
676
    qemu_get_8s(f, &s->timer0);
677
    qemu_get_8s(f, &s->timer1);
678
    qemu_get_8s(f, &s->precomp_trk);
679
    qemu_get_8s(f, &s->config);
680
    qemu_get_8s(f, &s->lock);
681
    qemu_get_8s(f, &s->pwrd);
682

    
683
    ret = fd_load(f, &s->drives[0]);
684
    if (ret == 0)
685
        ret = fd_load(f, &s->drives[1]);
686

    
687
    return ret;
688
}
689

    
690
static void fdctrl_external_reset(void *opaque)
691
{
692
    fdctrl_t *s = opaque;
693

    
694
    fdctrl_reset(s, 0);
695
}
696

    
697
static fdctrl_t *fdctrl_init_common (qemu_irq irq, int dma_chann,
698
                                     target_phys_addr_t io_base,
699
                                     BlockDriverState **fds)
700
{
701
    fdctrl_t *fdctrl;
702
    int i;
703

    
704
    FLOPPY_DPRINTF("init controller\n");
705
    fdctrl = qemu_mallocz(sizeof(fdctrl_t));
706
    if (!fdctrl)
707
        return NULL;
708
    fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN);
709
    if (fdctrl->fifo == NULL) {
710
        qemu_free(fdctrl);
711
        return NULL;
712
    }
713
    fdctrl->result_timer = qemu_new_timer(vm_clock,
714
                                          fdctrl_result_timer, fdctrl);
715

    
716
    fdctrl->version = 0x90; /* Intel 82078 controller */
717
    fdctrl->irq = irq;
718
    fdctrl->dma_chann = dma_chann;
719
    fdctrl->io_base = io_base;
720
    fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */
721
    if (fdctrl->dma_chann != -1) {
722
        fdctrl->dma_en = 1;
723
        DMA_register_channel(dma_chann, &fdctrl_transfer_handler, fdctrl);
724
    } else {
725
        fdctrl->dma_en = 0;
726
    }
727
    for (i = 0; i < MAX_FD; i++) {
728
        fd_init(&fdctrl->drives[i], fds[i]);
729
    }
730
    fdctrl_reset(fdctrl, 0);
731
    fdctrl->state = FD_CTRL_ACTIVE;
732
    register_savevm("fdc", io_base, 1, fdc_save, fdc_load, fdctrl);
733
    qemu_register_reset(fdctrl_external_reset, fdctrl);
734
    for (i = 0; i < MAX_FD; i++) {
735
        fd_revalidate(&fdctrl->drives[i]);
736
    }
737

    
738
    return fdctrl;
739
}
740

    
741
fdctrl_t *fdctrl_init (qemu_irq irq, int dma_chann, int mem_mapped,
742
                       target_phys_addr_t io_base,
743
                       BlockDriverState **fds)
744
{
745
    fdctrl_t *fdctrl;
746
    int io_mem;
747

    
748
    fdctrl = fdctrl_init_common(irq, dma_chann, io_base, fds);
749

    
750
    fdctrl->sun4m = 0;
751
    if (mem_mapped) {
752
        io_mem = cpu_register_io_memory(0, fdctrl_mem_read, fdctrl_mem_write,
753
                                        fdctrl);
754
        cpu_register_physical_memory(io_base, 0x08, io_mem);
755
    } else {
756
        register_ioport_read((uint32_t)io_base + 0x01, 5, 1, &fdctrl_read,
757
                             fdctrl);
758
        register_ioport_read((uint32_t)io_base + 0x07, 1, 1, &fdctrl_read,
759
                             fdctrl);
760
        register_ioport_write((uint32_t)io_base + 0x01, 5, 1, &fdctrl_write,
761
                              fdctrl);
762
        register_ioport_write((uint32_t)io_base + 0x07, 1, 1, &fdctrl_write,
763
                              fdctrl);
764
    }
765

    
766
    return fdctrl;
767
}
768

    
769
fdctrl_t *sun4m_fdctrl_init (qemu_irq irq, target_phys_addr_t io_base,
770
                             BlockDriverState **fds)
771
{
772
    fdctrl_t *fdctrl;
773
    int io_mem;
774

    
775
    fdctrl = fdctrl_init_common(irq, 0, io_base, fds);
776
    fdctrl->sun4m = 1;
777
    io_mem = cpu_register_io_memory(0, fdctrl_mem_read_strict,
778
                                    fdctrl_mem_write_strict,
779
                                    fdctrl);
780
    cpu_register_physical_memory(io_base, 0x08, io_mem);
781

    
782
    return fdctrl;
783
}
784

    
785
/* XXX: may change if moved to bdrv */
786
int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num)
787
{
788
    return fdctrl->drives[drive_num].drive;
789
}
790

    
791
/* Change IRQ state */
792
static void fdctrl_reset_irq (fdctrl_t *fdctrl)
793
{
794
    FLOPPY_DPRINTF("Reset interrupt\n");
795
    qemu_set_irq(fdctrl->irq, 0);
796
    fdctrl->state &= ~FD_CTRL_INTR;
797
}
798

    
799
static void fdctrl_raise_irq (fdctrl_t *fdctrl, uint8_t status)
800
{
801
    // Sparc mutation
802
    if (fdctrl->sun4m && !fdctrl->dma_en) {
803
        fdctrl->state &= ~FD_CTRL_BUSY;
804
        fdctrl->int_status = status;
805
        return;
806
    }
807
    if (~(fdctrl->state & FD_CTRL_INTR)) {
808
        qemu_set_irq(fdctrl->irq, 1);
809
        fdctrl->state |= FD_CTRL_INTR;
810
    }
811
    FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", status);
812
    fdctrl->int_status = status;
813
}
814

    
815
/* Reset controller */
816
static void fdctrl_reset (fdctrl_t *fdctrl, int do_irq)
817
{
818
    int i;
819

    
820
    FLOPPY_DPRINTF("reset controller\n");
821
    fdctrl_reset_irq(fdctrl);
822
    /* Initialise controller */
823
    fdctrl->cur_drv = 0;
824
    /* FIFO state */
825
    fdctrl->data_pos = 0;
826
    fdctrl->data_len = 0;
827
    fdctrl->data_state = FD_STATE_CMD;
828
    fdctrl->data_dir = FD_DIR_WRITE;
829
    for (i = 0; i < MAX_FD; i++)
830
        fd_reset(&fdctrl->drives[i]);
831
    fdctrl_reset_fifo(fdctrl);
832
    if (do_irq)
833
        fdctrl_raise_irq(fdctrl, FD_SR0_RDYCHG);
834
}
835

    
836
static inline fdrive_t *drv0 (fdctrl_t *fdctrl)
837
{
838
    return &fdctrl->drives[fdctrl->bootsel];
839
}
840

    
841
static inline fdrive_t *drv1 (fdctrl_t *fdctrl)
842
{
843
    return &fdctrl->drives[1 - fdctrl->bootsel];
844
}
845

    
846
static fdrive_t *get_cur_drv (fdctrl_t *fdctrl)
847
{
848
    return fdctrl->cur_drv == 0 ? drv0(fdctrl) : drv1(fdctrl);
849
}
850

    
851
/* Status B register : 0x01 (read-only) */
852
static uint32_t fdctrl_read_statusB (fdctrl_t *fdctrl)
853
{
854
    FLOPPY_DPRINTF("status register: 0x00\n");
855
    return 0;
856
}
857

    
858
/* Digital output register : 0x02 */
859
static uint32_t fdctrl_read_dor (fdctrl_t *fdctrl)
860
{
861
    uint32_t retval = 0;
862

    
863
    /* Drive motors state indicators */
864
    if (drv0(fdctrl)->drflags & FDRIVE_MOTOR_ON)
865
        retval |= FD_DOR_MOTEN0;
866
    if (drv1(fdctrl)->drflags & FDRIVE_MOTOR_ON)
867
        retval |= FD_DOR_MOTEN1;
868
    /* DMA enable */
869
    if (fdctrl->dma_en)
870
        retval |= FD_DOR_DMAEN;
871
    /* Reset indicator */
872
    if (!(fdctrl->state & FD_CTRL_RESET))
873
        retval |= FD_DOR_nRESET;
874
    /* Selected drive */
875
    retval |= fdctrl->cur_drv;
876
    FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval);
877

    
878
    return retval;
879
}
880

    
881
static void fdctrl_write_dor (fdctrl_t *fdctrl, uint32_t value)
882
{
883
    /* Reset mode */
884
    if (fdctrl->state & FD_CTRL_RESET) {
885
        if (!(value & FD_DOR_nRESET)) {
886
            FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
887
            return;
888
        }
889
    }
890
    FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value);
891
    /* Drive motors state indicators */
892
    if (value & FD_DOR_MOTEN1)
893
        fd_start(drv1(fdctrl));
894
    else
895
        fd_stop(drv1(fdctrl));
896
    if (value & FD_DOR_MOTEN0)
897
        fd_start(drv0(fdctrl));
898
    else
899
        fd_stop(drv0(fdctrl));
900
    /* DMA enable */
901
#if 0
902
    if (fdctrl->dma_chann != -1)
903
        fdctrl->dma_en = value & FD_DOR_DMAEN ? 1 : 0;
904
#endif
905
    /* Reset */
906
    if (!(value & FD_DOR_nRESET)) {
907
        if (!(fdctrl->state & FD_CTRL_RESET)) {
908
            FLOPPY_DPRINTF("controller enter RESET state\n");
909
            fdctrl->state |= FD_CTRL_RESET;
910
        }
911
    } else {
912
        if (fdctrl->state & FD_CTRL_RESET) {
913
            FLOPPY_DPRINTF("controller out of RESET state\n");
914
            fdctrl_reset(fdctrl, 1);
915
            fdctrl->state &= ~(FD_CTRL_RESET | FD_CTRL_SLEEP);
916
        }
917
    }
918
    /* Selected drive */
919
    fdctrl->cur_drv = value & FD_DOR_SELMASK;
920
}
921

    
922
/* Tape drive register : 0x03 */
923
static uint32_t fdctrl_read_tape (fdctrl_t *fdctrl)
924
{
925
    uint32_t retval = 0;
926

    
927
    /* Disk boot selection indicator */
928
    retval |= fdctrl->bootsel << 2;
929
    /* Tape indicators: never allowed */
930
    FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval);
931

    
932
    return retval;
933
}
934

    
935
static void fdctrl_write_tape (fdctrl_t *fdctrl, uint32_t value)
936
{
937
    /* Reset mode */
938
    if (fdctrl->state & FD_CTRL_RESET) {
939
        FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
940
        return;
941
    }
942
    FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value);
943
    /* Disk boot selection indicator */
944
    fdctrl->bootsel = (value & FD_TDR_BOOTSEL) >> 2;
945
    /* Tape indicators: never allow */
946
}
947

    
948
/* Main status register : 0x04 (read) */
949
static uint32_t fdctrl_read_main_status (fdctrl_t *fdctrl)
950
{
951
    uint32_t retval = 0;
952

    
953
    fdctrl->state &= ~(FD_CTRL_SLEEP | FD_CTRL_RESET);
954
    if (!(fdctrl->state & FD_CTRL_BUSY)) {
955
        /* Data transfer allowed */
956
        retval |= FD_MSR_RQM;
957
        /* Data transfer direction indicator */
958
        if (fdctrl->data_dir == FD_DIR_READ)
959
            retval |= FD_MSR_DIO;
960
    }
961
    /* Should handle FD_MSR_NONDMA for SPECIFY command */
962
    /* Command busy indicator */
963
    if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA ||
964
        FD_STATE(fdctrl->data_state) == FD_STATE_STATUS)
965
        retval |= FD_MSR_CMDBUSY;
966
    FLOPPY_DPRINTF("main status register: 0x%02x\n", retval);
967

    
968
    return retval;
969
}
970

    
971
/* Data select rate register : 0x04 (write) */
972
static void fdctrl_write_rate (fdctrl_t *fdctrl, uint32_t value)
973
{
974
    /* Reset mode */
975
    if (fdctrl->state & FD_CTRL_RESET) {
976
        FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
977
        return;
978
    }
979
    FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value);
980
    /* Reset: autoclear */
981
    if (value & FD_DSR_SWRESET) {
982
        fdctrl->state |= FD_CTRL_RESET;
983
        fdctrl_reset(fdctrl, 1);
984
        fdctrl->state &= ~FD_CTRL_RESET;
985
    }
986
    if (value & FD_DSR_PWRDOWN) {
987
        fdctrl->state |= FD_CTRL_SLEEP;
988
        fdctrl_reset(fdctrl, 1);
989
    }
990
}
991

    
992
static int fdctrl_media_changed(fdrive_t *drv)
993
{
994
    int ret;
995

    
996
    if (!drv->bs)
997
        return 0;
998
    ret = bdrv_media_changed(drv->bs);
999
    if (ret) {
1000
        fd_revalidate(drv);
1001
    }
1002
    return ret;
1003
}
1004

    
1005
/* Digital input register : 0x07 (read-only) */
1006
static uint32_t fdctrl_read_dir (fdctrl_t *fdctrl)
1007
{
1008
    uint32_t retval = 0;
1009

    
1010
    if (fdctrl_media_changed(drv0(fdctrl)) ||
1011
        fdctrl_media_changed(drv1(fdctrl)))
1012
        retval |= FD_DIR_DSKCHG;
1013
    if (retval != 0)
1014
        FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval);
1015

    
1016
    return retval;
1017
}
1018

    
1019
/* FIFO state control */
1020
static void fdctrl_reset_fifo (fdctrl_t *fdctrl)
1021
{
1022
    fdctrl->data_dir = FD_DIR_WRITE;
1023
    fdctrl->data_pos = 0;
1024
    FD_SET_STATE(fdctrl->data_state, FD_STATE_CMD);
1025
}
1026

    
1027
/* Set FIFO status for the host to read */
1028
static void fdctrl_set_fifo (fdctrl_t *fdctrl, int fifo_len, int do_irq)
1029
{
1030
    fdctrl->data_dir = FD_DIR_READ;
1031
    fdctrl->data_len = fifo_len;
1032
    fdctrl->data_pos = 0;
1033
    FD_SET_STATE(fdctrl->data_state, FD_STATE_STATUS);
1034
    if (do_irq)
1035
        fdctrl_raise_irq(fdctrl, 0x00);
1036
}
1037

    
1038
/* Set an error: unimplemented/unknown command */
1039
static void fdctrl_unimplemented (fdctrl_t *fdctrl)
1040
{
1041
#if 0
1042
    fdrive_t *cur_drv;
1043

1044
    cur_drv = get_cur_drv(fdctrl);
1045
    fdctrl->fifo[0] = FD_SR0_ABNTERM | FD_SR0_SEEK | (cur_drv->head << 2) | fdctrl->cur_drv;
1046
    fdctrl->fifo[1] = 0x00;
1047
    fdctrl->fifo[2] = 0x00;
1048
    fdctrl_set_fifo(fdctrl, 3, 1);
1049
#else
1050
    //    fdctrl_reset_fifo(fdctrl);
1051
    fdctrl->fifo[0] = FD_SR0_INVCMD;
1052
    fdctrl_set_fifo(fdctrl, 1, 0);
1053
#endif
1054
}
1055

    
1056
/* Callback for transfer end (stop or abort) */
1057
static void fdctrl_stop_transfer (fdctrl_t *fdctrl, uint8_t status0,
1058
                                  uint8_t status1, uint8_t status2)
1059
{
1060
    fdrive_t *cur_drv;
1061

    
1062
    cur_drv = get_cur_drv(fdctrl);
1063
    FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n",
1064
                   status0, status1, status2,
1065
                   status0 | (cur_drv->head << 2) | fdctrl->cur_drv);
1066
    fdctrl->fifo[0] = status0 | (cur_drv->head << 2) | fdctrl->cur_drv;
1067
    fdctrl->fifo[1] = status1;
1068
    fdctrl->fifo[2] = status2;
1069
    fdctrl->fifo[3] = cur_drv->track;
1070
    fdctrl->fifo[4] = cur_drv->head;
1071
    fdctrl->fifo[5] = cur_drv->sect;
1072
    fdctrl->fifo[6] = FD_SECTOR_SC;
1073
    fdctrl->data_dir = FD_DIR_READ;
1074
    if (fdctrl->state & FD_CTRL_BUSY) {
1075
        DMA_release_DREQ(fdctrl->dma_chann);
1076
        fdctrl->state &= ~FD_CTRL_BUSY;
1077
    }
1078
    fdctrl_set_fifo(fdctrl, 7, 1);
1079
}
1080

    
1081
/* Prepare a data transfer (either DMA or FIFO) */
1082
static void fdctrl_start_transfer (fdctrl_t *fdctrl, int direction)
1083
{
1084
    fdrive_t *cur_drv;
1085
    uint8_t kh, kt, ks;
1086
    int did_seek;
1087

    
1088
    fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1089
    cur_drv = get_cur_drv(fdctrl);
1090
    kt = fdctrl->fifo[2];
1091
    kh = fdctrl->fifo[3];
1092
    ks = fdctrl->fifo[4];
1093
    FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n",
1094
                   fdctrl->cur_drv, kh, kt, ks,
1095
                   _fd_sector(kh, kt, ks, cur_drv->last_sect));
1096
    did_seek = 0;
1097
    switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & 0x40)) {
1098
    case 2:
1099
        /* sect too big */
1100
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1101
        fdctrl->fifo[3] = kt;
1102
        fdctrl->fifo[4] = kh;
1103
        fdctrl->fifo[5] = ks;
1104
        return;
1105
    case 3:
1106
        /* track too big */
1107
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x80, 0x00);
1108
        fdctrl->fifo[3] = kt;
1109
        fdctrl->fifo[4] = kh;
1110
        fdctrl->fifo[5] = ks;
1111
        return;
1112
    case 4:
1113
        /* No seek enabled */
1114
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1115
        fdctrl->fifo[3] = kt;
1116
        fdctrl->fifo[4] = kh;
1117
        fdctrl->fifo[5] = ks;
1118
        return;
1119
    case 1:
1120
        did_seek = 1;
1121
        break;
1122
    default:
1123
        break;
1124
    }
1125
    /* Set the FIFO state */
1126
    fdctrl->data_dir = direction;
1127
    fdctrl->data_pos = 0;
1128
    FD_SET_STATE(fdctrl->data_state, FD_STATE_DATA); /* FIFO ready for data */
1129
    if (fdctrl->fifo[0] & 0x80)
1130
        fdctrl->data_state |= FD_STATE_MULTI;
1131
    else
1132
        fdctrl->data_state &= ~FD_STATE_MULTI;
1133
    if (did_seek)
1134
        fdctrl->data_state |= FD_STATE_SEEK;
1135
    else
1136
        fdctrl->data_state &= ~FD_STATE_SEEK;
1137
    if (fdctrl->fifo[5] == 00) {
1138
        fdctrl->data_len = fdctrl->fifo[8];
1139
    } else {
1140
        int tmp;
1141
        fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]);
1142
        tmp = (cur_drv->last_sect - ks + 1);
1143
        if (fdctrl->fifo[0] & 0x80)
1144
            tmp += cur_drv->last_sect;
1145
        fdctrl->data_len *= tmp;
1146
    }
1147
    fdctrl->eot = fdctrl->fifo[6];
1148
    if (fdctrl->dma_en) {
1149
        int dma_mode;
1150
        /* DMA transfer are enabled. Check if DMA channel is well programmed */
1151
        dma_mode = DMA_get_channel_mode(fdctrl->dma_chann);
1152
        dma_mode = (dma_mode >> 2) & 3;
1153
        FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n",
1154
                       dma_mode, direction,
1155
                       (128 << fdctrl->fifo[5]) *
1156
                       (cur_drv->last_sect - ks + 1), fdctrl->data_len);
1157
        if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL ||
1158
              direction == FD_DIR_SCANH) && dma_mode == 0) ||
1159
            (direction == FD_DIR_WRITE && dma_mode == 2) ||
1160
            (direction == FD_DIR_READ && dma_mode == 1)) {
1161
            /* No access is allowed until DMA transfer has completed */
1162
            fdctrl->state |= FD_CTRL_BUSY;
1163
            /* Now, we just have to wait for the DMA controller to
1164
             * recall us...
1165
             */
1166
            DMA_hold_DREQ(fdctrl->dma_chann);
1167
            DMA_schedule(fdctrl->dma_chann);
1168
            return;
1169
        } else {
1170
            FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode, direction);
1171
        }
1172
    }
1173
    FLOPPY_DPRINTF("start non-DMA transfer\n");
1174
    /* IO based transfer: calculate len */
1175
    fdctrl_raise_irq(fdctrl, 0x00);
1176

    
1177
    return;
1178
}
1179

    
1180
/* Prepare a transfer of deleted data */
1181
static void fdctrl_start_transfer_del (fdctrl_t *fdctrl, int direction)
1182
{
1183
    /* We don't handle deleted data,
1184
     * so we don't return *ANYTHING*
1185
     */
1186
    fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1187
}
1188

    
1189
/* handlers for DMA transfers */
1190
static int fdctrl_transfer_handler (void *opaque, int nchan,
1191
                                    int dma_pos, int dma_len)
1192
{
1193
    fdctrl_t *fdctrl;
1194
    fdrive_t *cur_drv;
1195
    int len, start_pos, rel_pos;
1196
    uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00;
1197

    
1198
    fdctrl = opaque;
1199
    if (!(fdctrl->state & FD_CTRL_BUSY)) {
1200
        FLOPPY_DPRINTF("Not in DMA transfer mode !\n");
1201
        return 0;
1202
    }
1203
    cur_drv = get_cur_drv(fdctrl);
1204
    if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL ||
1205
        fdctrl->data_dir == FD_DIR_SCANH)
1206
        status2 = 0x04;
1207
    if (dma_len > fdctrl->data_len)
1208
        dma_len = fdctrl->data_len;
1209
    if (cur_drv->bs == NULL) {
1210
        if (fdctrl->data_dir == FD_DIR_WRITE)
1211
            fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1212
        else
1213
            fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1214
        len = 0;
1215
        goto transfer_error;
1216
    }
1217
    rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1218
    for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) {
1219
        len = dma_len - fdctrl->data_pos;
1220
        if (len + rel_pos > FD_SECTOR_LEN)
1221
            len = FD_SECTOR_LEN - rel_pos;
1222
        FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x "
1223
                       "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos,
1224
                       fdctrl->data_len, fdctrl->cur_drv, cur_drv->head,
1225
                       cur_drv->track, cur_drv->sect, fd_sector(cur_drv),
1226
                       fd_sector(cur_drv) * FD_SECTOR_LEN);
1227
        if (fdctrl->data_dir != FD_DIR_WRITE ||
1228
            len < FD_SECTOR_LEN || rel_pos != 0) {
1229
            /* READ & SCAN commands and realign to a sector for WRITE */
1230
            if (bdrv_read(cur_drv->bs, fd_sector(cur_drv),
1231
                          fdctrl->fifo, 1) < 0) {
1232
                FLOPPY_DPRINTF("Floppy: error getting sector %d\n",
1233
                               fd_sector(cur_drv));
1234
                /* Sure, image size is too small... */
1235
                memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1236
            }
1237
        }
1238
        switch (fdctrl->data_dir) {
1239
        case FD_DIR_READ:
1240
            /* READ commands */
1241
            DMA_write_memory (nchan, fdctrl->fifo + rel_pos,
1242
                              fdctrl->data_pos, len);
1243
            break;
1244
        case FD_DIR_WRITE:
1245
            /* WRITE commands */
1246
            DMA_read_memory (nchan, fdctrl->fifo + rel_pos,
1247
                             fdctrl->data_pos, len);
1248
            if (bdrv_write(cur_drv->bs, fd_sector(cur_drv),
1249
                           fdctrl->fifo, 1) < 0) {
1250
                FLOPPY_ERROR("writting sector %d\n", fd_sector(cur_drv));
1251
                fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1252
                goto transfer_error;
1253
            }
1254
            break;
1255
        default:
1256
            /* SCAN commands */
1257
            {
1258
                uint8_t tmpbuf[FD_SECTOR_LEN];
1259
                int ret;
1260
                DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len);
1261
                ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len);
1262
                if (ret == 0) {
1263
                    status2 = 0x08;
1264
                    goto end_transfer;
1265
                }
1266
                if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) ||
1267
                    (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) {
1268
                    status2 = 0x00;
1269
                    goto end_transfer;
1270
                }
1271
            }
1272
            break;
1273
        }
1274
        fdctrl->data_pos += len;
1275
        rel_pos = fdctrl->data_pos % FD_SECTOR_LEN;
1276
        if (rel_pos == 0) {
1277
            /* Seek to next sector */
1278
            FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d) (%d)\n",
1279
                           cur_drv->head, cur_drv->track, cur_drv->sect,
1280
                           fd_sector(cur_drv),
1281
                           fdctrl->data_pos - len);
1282
            /* XXX: cur_drv->sect >= cur_drv->last_sect should be an
1283
               error in fact */
1284
            if (cur_drv->sect >= cur_drv->last_sect ||
1285
                cur_drv->sect == fdctrl->eot) {
1286
                cur_drv->sect = 1;
1287
                if (FD_MULTI_TRACK(fdctrl->data_state)) {
1288
                    if (cur_drv->head == 0 &&
1289
                        (cur_drv->flags & FDISK_DBL_SIDES) != 0) {
1290
                        cur_drv->head = 1;
1291
                    } else {
1292
                        cur_drv->head = 0;
1293
                        cur_drv->track++;
1294
                        if ((cur_drv->flags & FDISK_DBL_SIDES) == 0)
1295
                            break;
1296
                    }
1297
                } else {
1298
                    cur_drv->track++;
1299
                    break;
1300
                }
1301
                FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n",
1302
                               cur_drv->head, cur_drv->track,
1303
                               cur_drv->sect, fd_sector(cur_drv));
1304
            } else {
1305
                cur_drv->sect++;
1306
            }
1307
        }
1308
    }
1309
 end_transfer:
1310
    len = fdctrl->data_pos - start_pos;
1311
    FLOPPY_DPRINTF("end transfer %d %d %d\n",
1312
                   fdctrl->data_pos, len, fdctrl->data_len);
1313
    if (fdctrl->data_dir == FD_DIR_SCANE ||
1314
        fdctrl->data_dir == FD_DIR_SCANL ||
1315
        fdctrl->data_dir == FD_DIR_SCANH)
1316
        status2 = 0x08;
1317
    if (FD_DID_SEEK(fdctrl->data_state))
1318
        status0 |= FD_SR0_SEEK;
1319
    fdctrl->data_len -= len;
1320
    //    if (fdctrl->data_len == 0)
1321
    fdctrl_stop_transfer(fdctrl, status0, status1, status2);
1322
 transfer_error:
1323

    
1324
    return len;
1325
}
1326

    
1327
/* Data register : 0x05 */
1328
static uint32_t fdctrl_read_data (fdctrl_t *fdctrl)
1329
{
1330
    fdrive_t *cur_drv;
1331
    uint32_t retval = 0;
1332
    int pos, len;
1333

    
1334
    cur_drv = get_cur_drv(fdctrl);
1335
    fdctrl->state &= ~FD_CTRL_SLEEP;
1336
    if (FD_STATE(fdctrl->data_state) == FD_STATE_CMD) {
1337
        FLOPPY_ERROR("can't read data in CMD state\n");
1338
        return 0;
1339
    }
1340
    pos = fdctrl->data_pos;
1341
    if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
1342
        pos %= FD_SECTOR_LEN;
1343
        if (pos == 0) {
1344
            len = fdctrl->data_len - fdctrl->data_pos;
1345
            if (len > FD_SECTOR_LEN)
1346
                len = FD_SECTOR_LEN;
1347
            bdrv_read(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1);
1348
        }
1349
    }
1350
    retval = fdctrl->fifo[pos];
1351
    if (++fdctrl->data_pos == fdctrl->data_len) {
1352
        fdctrl->data_pos = 0;
1353
        /* Switch from transfer mode to status mode
1354
         * then from status mode to command mode
1355
         */
1356
        if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
1357
            fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1358
        } else {
1359
            fdctrl_reset_fifo(fdctrl);
1360
            fdctrl_reset_irq(fdctrl);
1361
        }
1362
    }
1363
    FLOPPY_DPRINTF("data register: 0x%02x\n", retval);
1364

    
1365
    return retval;
1366
}
1367

    
1368
static void fdctrl_format_sector (fdctrl_t *fdctrl)
1369
{
1370
    fdrive_t *cur_drv;
1371
    uint8_t kh, kt, ks;
1372
    int did_seek;
1373

    
1374
    fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1375
    cur_drv = get_cur_drv(fdctrl);
1376
    kt = fdctrl->fifo[6];
1377
    kh = fdctrl->fifo[7];
1378
    ks = fdctrl->fifo[8];
1379
    FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n",
1380
                   fdctrl->cur_drv, kh, kt, ks,
1381
                   _fd_sector(kh, kt, ks, cur_drv->last_sect));
1382
    did_seek = 0;
1383
    switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) {
1384
    case 2:
1385
        /* sect too big */
1386
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1387
        fdctrl->fifo[3] = kt;
1388
        fdctrl->fifo[4] = kh;
1389
        fdctrl->fifo[5] = ks;
1390
        return;
1391
    case 3:
1392
        /* track too big */
1393
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x80, 0x00);
1394
        fdctrl->fifo[3] = kt;
1395
        fdctrl->fifo[4] = kh;
1396
        fdctrl->fifo[5] = ks;
1397
        return;
1398
    case 4:
1399
        /* No seek enabled */
1400
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00);
1401
        fdctrl->fifo[3] = kt;
1402
        fdctrl->fifo[4] = kh;
1403
        fdctrl->fifo[5] = ks;
1404
        return;
1405
    case 1:
1406
        did_seek = 1;
1407
        fdctrl->data_state |= FD_STATE_SEEK;
1408
        break;
1409
    default:
1410
        break;
1411
    }
1412
    memset(fdctrl->fifo, 0, FD_SECTOR_LEN);
1413
    if (cur_drv->bs == NULL ||
1414
        bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) {
1415
        FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv));
1416
        fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00);
1417
    } else {
1418
        if (cur_drv->sect == cur_drv->last_sect) {
1419
            fdctrl->data_state &= ~FD_STATE_FORMAT;
1420
            /* Last sector done */
1421
            if (FD_DID_SEEK(fdctrl->data_state))
1422
                fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1423
            else
1424
                fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1425
        } else {
1426
            /* More to do */
1427
            fdctrl->data_pos = 0;
1428
            fdctrl->data_len = 4;
1429
        }
1430
    }
1431
}
1432

    
1433
static void fdctrl_write_data (fdctrl_t *fdctrl, uint32_t value)
1434
{
1435
    fdrive_t *cur_drv;
1436

    
1437
    cur_drv = get_cur_drv(fdctrl);
1438
    /* Reset mode */
1439
    if (fdctrl->state & FD_CTRL_RESET) {
1440
        FLOPPY_DPRINTF("Floppy controller in RESET state !\n");
1441
        return;
1442
    }
1443
    fdctrl->state &= ~FD_CTRL_SLEEP;
1444
    if (FD_STATE(fdctrl->data_state) == FD_STATE_STATUS) {
1445
        FLOPPY_ERROR("can't write data in status mode\n");
1446
        return;
1447
    }
1448
    /* Is it write command time ? */
1449
    if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA) {
1450
        /* FIFO data write */
1451
        fdctrl->fifo[fdctrl->data_pos++] = value;
1452
        if (fdctrl->data_pos % FD_SECTOR_LEN == (FD_SECTOR_LEN - 1) ||
1453
            fdctrl->data_pos == fdctrl->data_len) {
1454
            bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1);
1455
        }
1456
        /* Switch from transfer mode to status mode
1457
         * then from status mode to command mode
1458
         */
1459
        if (FD_STATE(fdctrl->data_state) == FD_STATE_DATA)
1460
            fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1461
        return;
1462
    }
1463
    if (fdctrl->data_pos == 0) {
1464
        /* Command */
1465
        switch (value & 0x5F) {
1466
        case FD_CMD_READ:
1467
            /* READ variants */
1468
            FLOPPY_DPRINTF("READ command\n");
1469
            /* 8 parameters cmd */
1470
            fdctrl->data_len = 9;
1471
            goto enqueue;
1472
        case FD_CMD_READ_DELETED:
1473
            /* READ_DELETED variants */
1474
            FLOPPY_DPRINTF("READ_DELETED command\n");
1475
            /* 8 parameters cmd */
1476
            fdctrl->data_len = 9;
1477
            goto enqueue;
1478
        case FD_CMD_SCAN_EQUAL:
1479
            /* SCAN_EQUAL variants */
1480
            FLOPPY_DPRINTF("SCAN_EQUAL command\n");
1481
            /* 8 parameters cmd */
1482
            fdctrl->data_len = 9;
1483
            goto enqueue;
1484
        case FD_CMD_VERIFY:
1485
            /* VERIFY variants */
1486
            FLOPPY_DPRINTF("VERIFY command\n");
1487
            /* 8 parameters cmd */
1488
            fdctrl->data_len = 9;
1489
            goto enqueue;
1490
        case FD_CMD_SCAN_LOW_OR_EQUAL:
1491
            /* SCAN_LOW_OR_EQUAL variants */
1492
            FLOPPY_DPRINTF("SCAN_LOW_OR_EQUAL command\n");
1493
            /* 8 parameters cmd */
1494
            fdctrl->data_len = 9;
1495
            goto enqueue;
1496
        case FD_CMD_SCAN_HIGH_OR_EQUAL:
1497
            /* SCAN_HIGH_OR_EQUAL variants */
1498
            FLOPPY_DPRINTF("SCAN_HIGH_OR_EQUAL command\n");
1499
            /* 8 parameters cmd */
1500
            fdctrl->data_len = 9;
1501
            goto enqueue;
1502
        default:
1503
            break;
1504
        }
1505
        switch (value & 0x7F) {
1506
        case FD_CMD_WRITE:
1507
            /* WRITE variants */
1508
            FLOPPY_DPRINTF("WRITE command\n");
1509
            /* 8 parameters cmd */
1510
            fdctrl->data_len = 9;
1511
            goto enqueue;
1512
        case FD_CMD_WRITE_DELETED:
1513
            /* WRITE_DELETED variants */
1514
            FLOPPY_DPRINTF("WRITE_DELETED command\n");
1515
            /* 8 parameters cmd */
1516
            fdctrl->data_len = 9;
1517
            goto enqueue;
1518
        default:
1519
            break;
1520
        }
1521
        switch (value) {
1522
        case FD_CMD_SPECIFY:
1523
            /* SPECIFY */
1524
            FLOPPY_DPRINTF("SPECIFY command\n");
1525
            /* 1 parameter cmd */
1526
            fdctrl->data_len = 3;
1527
            goto enqueue;
1528
        case FD_CMD_SENSE_DRIVE_STATUS:
1529
            /* SENSE_DRIVE_STATUS */
1530
            FLOPPY_DPRINTF("SENSE_DRIVE_STATUS command\n");
1531
            /* 1 parameter cmd */
1532
            fdctrl->data_len = 2;
1533
            goto enqueue;
1534
        case FD_CMD_RECALIBRATE:
1535
            /* RECALIBRATE */
1536
            FLOPPY_DPRINTF("RECALIBRATE command\n");
1537
            /* 1 parameter cmd */
1538
            fdctrl->data_len = 2;
1539
            goto enqueue;
1540
        case FD_CMD_SENSE_INTERRUPT_STATUS:
1541
            /* SENSE_INTERRUPT_STATUS */
1542
            FLOPPY_DPRINTF("SENSE_INTERRUPT_STATUS command (%02x)\n",
1543
                           fdctrl->int_status);
1544
            /* No parameters cmd: returns status if no interrupt */
1545
#if 0
1546
            fdctrl->fifo[0] =
1547
                fdctrl->int_status | (cur_drv->head << 2) | fdctrl->cur_drv;
1548
#else
1549
            /* XXX: int_status handling is broken for read/write
1550
               commands, so we do this hack. It should be suppressed
1551
               ASAP */
1552
            fdctrl->fifo[0] =
1553
                0x20 | (cur_drv->head << 2) | fdctrl->cur_drv;
1554
#endif
1555
            fdctrl->fifo[1] = cur_drv->track;
1556
            fdctrl_set_fifo(fdctrl, 2, 0);
1557
            fdctrl_reset_irq(fdctrl);
1558
            fdctrl->int_status = FD_SR0_RDYCHG;
1559
            return;
1560
        case FD_CMD_DUMPREG:
1561
            /* DUMPREG */
1562
            FLOPPY_DPRINTF("DUMPREG command\n");
1563
            /* Drives position */
1564
            fdctrl->fifo[0] = drv0(fdctrl)->track;
1565
            fdctrl->fifo[1] = drv1(fdctrl)->track;
1566
            fdctrl->fifo[2] = 0;
1567
            fdctrl->fifo[3] = 0;
1568
            /* timers */
1569
            fdctrl->fifo[4] = fdctrl->timer0;
1570
            fdctrl->fifo[5] = (fdctrl->timer1 << 1) | fdctrl->dma_en;
1571
            fdctrl->fifo[6] = cur_drv->last_sect;
1572
            fdctrl->fifo[7] = (fdctrl->lock << 7) |
1573
                (cur_drv->perpendicular << 2);
1574
            fdctrl->fifo[8] = fdctrl->config;
1575
            fdctrl->fifo[9] = fdctrl->precomp_trk;
1576
            fdctrl_set_fifo(fdctrl, 10, 0);
1577
            return;
1578
        case FD_CMD_SEEK:
1579
            /* SEEK */
1580
            FLOPPY_DPRINTF("SEEK command\n");
1581
            /* 2 parameters cmd */
1582
            fdctrl->data_len = 3;
1583
            goto enqueue;
1584
        case FD_CMD_VERSION:
1585
            /* VERSION */
1586
            FLOPPY_DPRINTF("VERSION command\n");
1587
            /* No parameters cmd */
1588
            /* Controller's version */
1589
            fdctrl->fifo[0] = fdctrl->version;
1590
            fdctrl_set_fifo(fdctrl, 1, 1);
1591
            return;
1592
        case FD_CMD_PERPENDICULAR_MODE:
1593
            /* PERPENDICULAR_MODE */
1594
            FLOPPY_DPRINTF("PERPENDICULAR_MODE command\n");
1595
            /* 1 parameter cmd */
1596
            fdctrl->data_len = 2;
1597
            goto enqueue;
1598
        case FD_CMD_CONFIGURE:
1599
            /* CONFIGURE */
1600
            FLOPPY_DPRINTF("CONFIGURE command\n");
1601
            /* 3 parameters cmd */
1602
            fdctrl->data_len = 4;
1603
            goto enqueue;
1604
        case FD_CMD_UNLOCK:
1605
            /* UNLOCK */
1606
            FLOPPY_DPRINTF("UNLOCK command\n");
1607
            /* No parameters cmd */
1608
            fdctrl->lock = 0;
1609
            fdctrl->fifo[0] = 0;
1610
            fdctrl_set_fifo(fdctrl, 1, 0);
1611
            return;
1612
        case FD_CMD_POWERDOWN_MODE:
1613
            /* POWERDOWN_MODE */
1614
            FLOPPY_DPRINTF("POWERDOWN_MODE command\n");
1615
            /* 2 parameters cmd */
1616
            fdctrl->data_len = 3;
1617
            goto enqueue;
1618
        case FD_CMD_PART_ID:
1619
            /* PART_ID */
1620
            FLOPPY_DPRINTF("PART_ID command\n");
1621
            /* No parameters cmd */
1622
            fdctrl->fifo[0] = 0x41; /* Stepping 1 */
1623
            fdctrl_set_fifo(fdctrl, 1, 0);
1624
            return;
1625
        case FD_CMD_SAVE:
1626
            /* SAVE */
1627
            FLOPPY_DPRINTF("SAVE command\n");
1628
            /* No parameters cmd */
1629
            fdctrl->fifo[0] = 0;
1630
            fdctrl->fifo[1] = 0;
1631
            /* Drives position */
1632
            fdctrl->fifo[2] = drv0(fdctrl)->track;
1633
            fdctrl->fifo[3] = drv1(fdctrl)->track;
1634
            fdctrl->fifo[4] = 0;
1635
            fdctrl->fifo[5] = 0;
1636
            /* timers */
1637
            fdctrl->fifo[6] = fdctrl->timer0;
1638
            fdctrl->fifo[7] = fdctrl->timer1;
1639
            fdctrl->fifo[8] = cur_drv->last_sect;
1640
            fdctrl->fifo[9] = (fdctrl->lock << 7) |
1641
                (cur_drv->perpendicular << 2);
1642
            fdctrl->fifo[10] = fdctrl->config;
1643
            fdctrl->fifo[11] = fdctrl->precomp_trk;
1644
            fdctrl->fifo[12] = fdctrl->pwrd;
1645
            fdctrl->fifo[13] = 0;
1646
            fdctrl->fifo[14] = 0;
1647
            fdctrl_set_fifo(fdctrl, 15, 1);
1648
            return;
1649
        case FD_CMD_OPTION:
1650
            /* OPTION */
1651
            FLOPPY_DPRINTF("OPTION command\n");
1652
            /* 1 parameter cmd */
1653
            fdctrl->data_len = 2;
1654
            goto enqueue;
1655
        case FD_CMD_READ_TRACK:
1656
            /* READ_TRACK */
1657
            FLOPPY_DPRINTF("READ_TRACK command\n");
1658
            /* 8 parameters cmd */
1659
            fdctrl->data_len = 9;
1660
            goto enqueue;
1661
        case FD_CMD_READ_ID:
1662
            /* READ_ID */
1663
            FLOPPY_DPRINTF("READ_ID command\n");
1664
            /* 1 parameter cmd */
1665
            fdctrl->data_len = 2;
1666
            goto enqueue;
1667
        case FD_CMD_RESTORE:
1668
            /* RESTORE */
1669
            FLOPPY_DPRINTF("RESTORE command\n");
1670
            /* 17 parameters cmd */
1671
            fdctrl->data_len = 18;
1672
            goto enqueue;
1673
        case FD_CMD_FORMAT_TRACK:
1674
            /* FORMAT_TRACK */
1675
            FLOPPY_DPRINTF("FORMAT_TRACK command\n");
1676
            /* 5 parameters cmd */
1677
            fdctrl->data_len = 6;
1678
            goto enqueue;
1679
        case FD_CMD_DRIVE_SPECIFICATION_COMMAND:
1680
            /* DRIVE_SPECIFICATION_COMMAND */
1681
            FLOPPY_DPRINTF("DRIVE_SPECIFICATION_COMMAND command\n");
1682
            /* 5 parameters cmd */
1683
            fdctrl->data_len = 6;
1684
            goto enqueue;
1685
        case FD_CMD_RELATIVE_SEEK_OUT:
1686
            /* RELATIVE_SEEK_OUT */
1687
            FLOPPY_DPRINTF("RELATIVE_SEEK_OUT command\n");
1688
            /* 2 parameters cmd */
1689
            fdctrl->data_len = 3;
1690
            goto enqueue;
1691
        case FD_CMD_LOCK:
1692
            /* LOCK */
1693
            FLOPPY_DPRINTF("LOCK command\n");
1694
            /* No parameters cmd */
1695
            fdctrl->lock = 1;
1696
            fdctrl->fifo[0] = 0x10;
1697
            fdctrl_set_fifo(fdctrl, 1, 1);
1698
            return;
1699
        case FD_CMD_FORMAT_AND_WRITE:
1700
            /* FORMAT_AND_WRITE */
1701
            FLOPPY_DPRINTF("FORMAT_AND_WRITE command\n");
1702
            /* 10 parameters cmd */
1703
            fdctrl->data_len = 11;
1704
            goto enqueue;
1705
        case FD_CMD_RELATIVE_SEEK_IN:
1706
            /* RELATIVE_SEEK_IN */
1707
            FLOPPY_DPRINTF("RELATIVE_SEEK_IN command\n");
1708
            /* 2 parameters cmd */
1709
            fdctrl->data_len = 3;
1710
            goto enqueue;
1711
        default:
1712
            /* Unknown command */
1713
            FLOPPY_ERROR("unknown command: 0x%02x\n", value);
1714
            fdctrl_unimplemented(fdctrl);
1715
            return;
1716
        }
1717
    }
1718
 enqueue:
1719
    FLOPPY_DPRINTF("%s: %02x\n", __func__, value);
1720
    fdctrl->fifo[fdctrl->data_pos] = value;
1721
    if (++fdctrl->data_pos == fdctrl->data_len) {
1722
        /* We now have all parameters
1723
         * and will be able to treat the command
1724
         */
1725
        if (fdctrl->data_state & FD_STATE_FORMAT) {
1726
            fdctrl_format_sector(fdctrl);
1727
            return;
1728
        }
1729
        switch (fdctrl->fifo[0] & 0x1F) {
1730
        case FD_CMD_READ & 0x1F:
1731
            {
1732
                /* READ variants */
1733
                FLOPPY_DPRINTF("treat READ command\n");
1734
                fdctrl_start_transfer(fdctrl, FD_DIR_READ);
1735
                return;
1736
            }
1737
        case FD_CMD_READ_DELETED & 0x1F:
1738
            /* READ_DELETED variants */
1739
//            FLOPPY_DPRINTF("treat READ_DELETED command\n");
1740
            FLOPPY_ERROR("treat READ_DELETED command\n");
1741
            fdctrl_start_transfer_del(fdctrl, FD_DIR_READ);
1742
            return;
1743
        case FD_CMD_VERIFY & 0x1F:
1744
            /* VERIFY variants */
1745
//            FLOPPY_DPRINTF("treat VERIFY command\n");
1746
            FLOPPY_ERROR("treat VERIFY command\n");
1747
            fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00);
1748
            return;
1749
        case FD_CMD_SCAN_EQUAL & 0x1F:
1750
            /* SCAN_EQUAL variants */
1751
//            FLOPPY_DPRINTF("treat SCAN_EQUAL command\n");
1752
            FLOPPY_ERROR("treat SCAN_EQUAL command\n");
1753
            fdctrl_start_transfer(fdctrl, FD_DIR_SCANE);
1754
            return;
1755
        case FD_CMD_SCAN_LOW_OR_EQUAL & 0x1F:
1756
            /* SCAN_LOW_OR_EQUAL variants */
1757
//            FLOPPY_DPRINTF("treat SCAN_LOW_OR_EQUAL command\n");
1758
            FLOPPY_ERROR("treat SCAN_LOW_OR_EQUAL command\n");
1759
            fdctrl_start_transfer(fdctrl, FD_DIR_SCANL);
1760
            return;
1761
        case FD_CMD_SCAN_HIGH_OR_EQUAL & 0x1F:
1762
            /* SCAN_HIGH_OR_EQUAL variants */
1763
//            FLOPPY_DPRINTF("treat SCAN_HIGH_OR_EQUAL command\n");
1764
            FLOPPY_ERROR("treat SCAN_HIGH_OR_EQUAL command\n");
1765
            fdctrl_start_transfer(fdctrl, FD_DIR_SCANH);
1766
            return;
1767
        default:
1768
            break;
1769
        }
1770
        switch (fdctrl->fifo[0] & 0x3F) {
1771
        case FD_CMD_WRITE & 0x3F:
1772
            /* WRITE variants */
1773
            FLOPPY_DPRINTF("treat WRITE command (%02x)\n", fdctrl->fifo[0]);
1774
            fdctrl_start_transfer(fdctrl, FD_DIR_WRITE);
1775
            return;
1776
        case FD_CMD_WRITE_DELETED & 0x3F:
1777
            /* WRITE_DELETED variants */
1778
//            FLOPPY_DPRINTF("treat WRITE_DELETED command\n");
1779
            FLOPPY_ERROR("treat WRITE_DELETED command\n");
1780
            fdctrl_start_transfer_del(fdctrl, FD_DIR_WRITE);
1781
            return;
1782
        default:
1783
            break;
1784
        }
1785
        switch (fdctrl->fifo[0]) {
1786
        case FD_CMD_SPECIFY:
1787
            /* SPECIFY */
1788
            FLOPPY_DPRINTF("treat SPECIFY command\n");
1789
            fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF;
1790
            fdctrl->timer1 = fdctrl->fifo[2] >> 1;
1791
            fdctrl->dma_en = 1 - (fdctrl->fifo[2] & 1) ;
1792
            /* No result back */
1793
            fdctrl_reset_fifo(fdctrl);
1794
            break;
1795
        case FD_CMD_SENSE_DRIVE_STATUS:
1796
            /* SENSE_DRIVE_STATUS */
1797
            FLOPPY_DPRINTF("treat SENSE_DRIVE_STATUS command\n");
1798
            fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1799
            cur_drv = get_cur_drv(fdctrl);
1800
            cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1801
            /* 1 Byte status back */
1802
            fdctrl->fifo[0] = (cur_drv->ro << 6) |
1803
                (cur_drv->track == 0 ? 0x10 : 0x00) |
1804
                (cur_drv->head << 2) |
1805
                fdctrl->cur_drv |
1806
                0x28;
1807
            fdctrl_set_fifo(fdctrl, 1, 0);
1808
            break;
1809
        case FD_CMD_RECALIBRATE:
1810
            /* RECALIBRATE */
1811
            FLOPPY_DPRINTF("treat RECALIBRATE command\n");
1812
            fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1813
            cur_drv = get_cur_drv(fdctrl);
1814
            fd_recalibrate(cur_drv);
1815
            fdctrl_reset_fifo(fdctrl);
1816
            /* Raise Interrupt */
1817
            fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1818
            break;
1819
        case FD_CMD_SEEK:
1820
            /* SEEK */
1821
            FLOPPY_DPRINTF("treat SEEK command\n");
1822
            fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1823
            cur_drv = get_cur_drv(fdctrl);
1824
            fd_start(cur_drv);
1825
            if (fdctrl->fifo[2] <= cur_drv->track)
1826
                cur_drv->dir = 1;
1827
            else
1828
                cur_drv->dir = 0;
1829
            fdctrl_reset_fifo(fdctrl);
1830
            if (fdctrl->fifo[2] > cur_drv->max_track) {
1831
                fdctrl_raise_irq(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK);
1832
            } else {
1833
                cur_drv->track = fdctrl->fifo[2];
1834
                /* Raise Interrupt */
1835
                fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1836
            }
1837
            break;
1838
        case FD_CMD_PERPENDICULAR_MODE:
1839
            /* PERPENDICULAR_MODE */
1840
            FLOPPY_DPRINTF("treat PERPENDICULAR_MODE command\n");
1841
            if (fdctrl->fifo[1] & 0x80)
1842
                cur_drv->perpendicular = fdctrl->fifo[1] & 0x7;
1843
            /* No result back */
1844
            fdctrl_reset_fifo(fdctrl);
1845
            break;
1846
        case FD_CMD_CONFIGURE:
1847
            /* CONFIGURE */
1848
            FLOPPY_DPRINTF("treat CONFIGURE command\n");
1849
            fdctrl->config = fdctrl->fifo[2];
1850
            fdctrl->precomp_trk =  fdctrl->fifo[3];
1851
            /* No result back */
1852
            fdctrl_reset_fifo(fdctrl);
1853
            break;
1854
        case FD_CMD_POWERDOWN_MODE:
1855
            /* POWERDOWN_MODE */
1856
            FLOPPY_DPRINTF("treat POWERDOWN_MODE command\n");
1857
            fdctrl->pwrd = fdctrl->fifo[1];
1858
            fdctrl->fifo[0] = fdctrl->fifo[1];
1859
            fdctrl_set_fifo(fdctrl, 1, 1);
1860
            break;
1861
        case FD_CMD_OPTION:
1862
            /* OPTION */
1863
            FLOPPY_DPRINTF("treat OPTION command\n");
1864
            /* No result back */
1865
            fdctrl_reset_fifo(fdctrl);
1866
            break;
1867
        case FD_CMD_READ_TRACK:
1868
            /* READ_TRACK */
1869
            FLOPPY_DPRINTF("treat READ_TRACK command\n");
1870
            FLOPPY_ERROR("treat READ_TRACK command\n");
1871
            fdctrl_start_transfer(fdctrl, FD_DIR_READ);
1872
            break;
1873
        case FD_CMD_READ_ID:
1874
            /* READ_ID */
1875
            FLOPPY_DPRINTF("treat READ_ID command\n");
1876
            /* XXX: should set main status register to busy */
1877
            cur_drv->head = (fdctrl->fifo[1] >> 2) & 1;
1878
            qemu_mod_timer(fdctrl->result_timer,
1879
                           qemu_get_clock(vm_clock) + (ticks_per_sec / 50));
1880
            break;
1881
        case FD_CMD_RESTORE:
1882
            /* RESTORE */
1883
            FLOPPY_DPRINTF("treat RESTORE command\n");
1884
            /* Drives position */
1885
            drv0(fdctrl)->track = fdctrl->fifo[3];
1886
            drv1(fdctrl)->track = fdctrl->fifo[4];
1887
            /* timers */
1888
            fdctrl->timer0 = fdctrl->fifo[7];
1889
            fdctrl->timer1 = fdctrl->fifo[8];
1890
            cur_drv->last_sect = fdctrl->fifo[9];
1891
            fdctrl->lock = fdctrl->fifo[10] >> 7;
1892
            cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF;
1893
            fdctrl->config = fdctrl->fifo[11];
1894
            fdctrl->precomp_trk = fdctrl->fifo[12];
1895
            fdctrl->pwrd = fdctrl->fifo[13];
1896
            fdctrl_reset_fifo(fdctrl);
1897
            break;
1898
        case FD_CMD_FORMAT_TRACK:
1899
            /* FORMAT_TRACK */
1900
            FLOPPY_DPRINTF("treat FORMAT_TRACK command\n");
1901
            fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1902
            cur_drv = get_cur_drv(fdctrl);
1903
            fdctrl->data_state |= FD_STATE_FORMAT;
1904
            if (fdctrl->fifo[0] & 0x80)
1905
                fdctrl->data_state |= FD_STATE_MULTI;
1906
            else
1907
                fdctrl->data_state &= ~FD_STATE_MULTI;
1908
            fdctrl->data_state &= ~FD_STATE_SEEK;
1909
            cur_drv->bps =
1910
                fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2];
1911
#if 0
1912
            cur_drv->last_sect =
1913
                cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] :
1914
                fdctrl->fifo[3] / 2;
1915
#else
1916
            cur_drv->last_sect = fdctrl->fifo[3];
1917
#endif
1918
            /* TODO: implement format using DMA expected by the Bochs BIOS
1919
             * and Linux fdformat (read 3 bytes per sector via DMA and fill
1920
             * the sector with the specified fill byte
1921
             */
1922
            fdctrl->data_state &= ~FD_STATE_FORMAT;
1923
            fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1924
            break;
1925
        case FD_CMD_DRIVE_SPECIFICATION_COMMAND:
1926
            /* DRIVE_SPECIFICATION_COMMAND */
1927
            FLOPPY_DPRINTF("treat DRIVE_SPECIFICATION_COMMAND command\n");
1928
            if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x80) {
1929
                /* Command parameters done */
1930
                if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x40) {
1931
                    fdctrl->fifo[0] = fdctrl->fifo[1];
1932
                    fdctrl->fifo[2] = 0;
1933
                    fdctrl->fifo[3] = 0;
1934
                    fdctrl_set_fifo(fdctrl, 4, 1);
1935
                } else {
1936
                    fdctrl_reset_fifo(fdctrl);
1937
                }
1938
            } else if (fdctrl->data_len > 7) {
1939
                /* ERROR */
1940
                fdctrl->fifo[0] = 0x80 |
1941
                    (cur_drv->head << 2) | fdctrl->cur_drv;
1942
                fdctrl_set_fifo(fdctrl, 1, 1);
1943
            }
1944
            break;
1945
        case FD_CMD_RELATIVE_SEEK_OUT:
1946
            /* RELATIVE_SEEK_OUT */
1947
            FLOPPY_DPRINTF("treat RELATIVE_SEEK_OUT command\n");
1948
            fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1949
            cur_drv = get_cur_drv(fdctrl);
1950
            fd_start(cur_drv);
1951
            cur_drv->dir = 0;
1952
            if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) {
1953
                cur_drv->track = cur_drv->max_track - 1;
1954
            } else {
1955
                cur_drv->track += fdctrl->fifo[2];
1956
            }
1957
            fdctrl_reset_fifo(fdctrl);
1958
            fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1959
            break;
1960
        case FD_CMD_FORMAT_AND_WRITE:
1961
            /* FORMAT_AND_WRITE */
1962
            FLOPPY_DPRINTF("treat FORMAT_AND_WRITE command\n");
1963
            FLOPPY_ERROR("treat FORMAT_AND_WRITE command\n");
1964
            fdctrl_unimplemented(fdctrl);
1965
            break;
1966
        case FD_CMD_RELATIVE_SEEK_IN:
1967
            /* RELATIVE_SEEK_IN */
1968
            FLOPPY_DPRINTF("treat RELATIVE_SEEK_IN command\n");
1969
            fdctrl->cur_drv = fdctrl->fifo[1] & FD_DOR_SELMASK;
1970
            cur_drv = get_cur_drv(fdctrl);
1971
            fd_start(cur_drv);
1972
            cur_drv->dir = 1;
1973
            if (fdctrl->fifo[2] > cur_drv->track) {
1974
                cur_drv->track = 0;
1975
            } else {
1976
                cur_drv->track -= fdctrl->fifo[2];
1977
            }
1978
            fdctrl_reset_fifo(fdctrl);
1979
            /* Raise Interrupt */
1980
            fdctrl_raise_irq(fdctrl, FD_SR0_SEEK);
1981
            break;
1982
        }
1983
    }
1984
}
1985

    
1986
static void fdctrl_result_timer(void *opaque)
1987
{
1988
    fdctrl_t *fdctrl = opaque;
1989
    fdrive_t *cur_drv = get_cur_drv(fdctrl);
1990

    
1991
    /* Pretend we are spinning.
1992
     * This is needed for Coherent, which uses READ ID to check for
1993
     * sector interleaving.
1994
     */
1995
    if (cur_drv->last_sect != 0) {
1996
        cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1;
1997
    }
1998
    fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00);
1999
}