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