]>
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; | |
6cc8a11c | 1226 | fdctrl->data_len = 1; /* Accept command byte, adjust for params later */ |
b9b3d225 | 1227 | fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO); |
6cc8a11c | 1228 | fdctrl->msr |= FD_MSR_RQM; |
8977f3c1 FB |
1229 | } |
1230 | ||
83a26013 KW |
1231 | /* Update the state to allow the guest to read out the command status. |
1232 | * @fifo_len is the number of result bytes to be read out. */ | |
1233 | static void fdctrl_to_result_phase(FDCtrl *fdctrl, int fifo_len) | |
8977f3c1 | 1234 | { |
85d291a0 | 1235 | fdctrl->phase = FD_PHASE_RESULT; |
baca51fa FB |
1236 | fdctrl->data_dir = FD_DIR_READ; |
1237 | fdctrl->data_len = fifo_len; | |
1238 | fdctrl->data_pos = 0; | |
b9b3d225 | 1239 | fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO; |
8977f3c1 FB |
1240 | } |
1241 | ||
1242 | /* Set an error: unimplemented/unknown command */ | |
5c02c033 | 1243 | static void fdctrl_unimplemented(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1244 | { |
cced7a13 BS |
1245 | qemu_log_mask(LOG_UNIMP, "fdc: unimplemented command 0x%02x\n", |
1246 | fdctrl->fifo[0]); | |
9fea808a | 1247 | fdctrl->fifo[0] = FD_SR0_INVCMD; |
83a26013 | 1248 | fdctrl_to_result_phase(fdctrl, 1); |
8977f3c1 FB |
1249 | } |
1250 | ||
6be01b1e PH |
1251 | /* Seek to next sector |
1252 | * returns 0 when end of track reached (for DBL_SIDES on head 1) | |
1253 | * otherwise returns 1 | |
1254 | */ | |
5c02c033 | 1255 | static int fdctrl_seek_to_next_sect(FDCtrl *fdctrl, FDrive *cur_drv) |
746d6de7 BS |
1256 | { |
1257 | FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n", | |
1258 | cur_drv->head, cur_drv->track, cur_drv->sect, | |
1259 | fd_sector(cur_drv)); | |
1260 | /* XXX: cur_drv->sect >= cur_drv->last_sect should be an | |
1261 | error in fact */ | |
6be01b1e PH |
1262 | uint8_t new_head = cur_drv->head; |
1263 | uint8_t new_track = cur_drv->track; | |
1264 | uint8_t new_sect = cur_drv->sect; | |
1265 | ||
1266 | int ret = 1; | |
1267 | ||
1268 | if (new_sect >= cur_drv->last_sect || | |
1269 | new_sect == fdctrl->eot) { | |
1270 | new_sect = 1; | |
746d6de7 | 1271 | if (FD_MULTI_TRACK(fdctrl->data_state)) { |
6be01b1e | 1272 | if (new_head == 0 && |
746d6de7 | 1273 | (cur_drv->flags & FDISK_DBL_SIDES) != 0) { |
6be01b1e | 1274 | new_head = 1; |
746d6de7 | 1275 | } else { |
6be01b1e PH |
1276 | new_head = 0; |
1277 | new_track++; | |
c5139bd9 | 1278 | fdctrl->status0 |= FD_SR0_SEEK; |
6be01b1e PH |
1279 | if ((cur_drv->flags & FDISK_DBL_SIDES) == 0) { |
1280 | ret = 0; | |
1281 | } | |
746d6de7 BS |
1282 | } |
1283 | } else { | |
c5139bd9 | 1284 | fdctrl->status0 |= FD_SR0_SEEK; |
6be01b1e PH |
1285 | new_track++; |
1286 | ret = 0; | |
1287 | } | |
1288 | if (ret == 1) { | |
1289 | FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n", | |
1290 | new_head, new_track, new_sect, fd_sector(cur_drv)); | |
746d6de7 | 1291 | } |
746d6de7 | 1292 | } else { |
6be01b1e | 1293 | new_sect++; |
746d6de7 | 1294 | } |
6be01b1e PH |
1295 | fd_seek(cur_drv, new_head, new_track, new_sect, 1); |
1296 | return ret; | |
746d6de7 BS |
1297 | } |
1298 | ||
8977f3c1 | 1299 | /* Callback for transfer end (stop or abort) */ |
5c02c033 BS |
1300 | static void fdctrl_stop_transfer(FDCtrl *fdctrl, uint8_t status0, |
1301 | uint8_t status1, uint8_t status2) | |
8977f3c1 | 1302 | { |
5c02c033 | 1303 | FDrive *cur_drv; |
baca51fa | 1304 | cur_drv = get_cur_drv(fdctrl); |
075f5532 HP |
1305 | |
1306 | fdctrl->status0 &= ~(FD_SR0_DS0 | FD_SR0_DS1 | FD_SR0_HEAD); | |
1307 | fdctrl->status0 |= GET_CUR_DRV(fdctrl); | |
1308 | if (cur_drv->head) { | |
1309 | fdctrl->status0 |= FD_SR0_HEAD; | |
1310 | } | |
1311 | fdctrl->status0 |= status0; | |
2fee0088 | 1312 | |
8977f3c1 | 1313 | FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n", |
2fee0088 PH |
1314 | status0, status1, status2, fdctrl->status0); |
1315 | fdctrl->fifo[0] = fdctrl->status0; | |
baca51fa FB |
1316 | fdctrl->fifo[1] = status1; |
1317 | fdctrl->fifo[2] = status2; | |
1318 | fdctrl->fifo[3] = cur_drv->track; | |
1319 | fdctrl->fifo[4] = cur_drv->head; | |
1320 | fdctrl->fifo[5] = cur_drv->sect; | |
1321 | fdctrl->fifo[6] = FD_SECTOR_SC; | |
1322 | fdctrl->data_dir = FD_DIR_READ; | |
368df94d | 1323 | if (!(fdctrl->msr & FD_MSR_NONDMA)) { |
baca51fa | 1324 | DMA_release_DREQ(fdctrl->dma_chann); |
ed5fd2cc | 1325 | } |
b9b3d225 | 1326 | fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO; |
368df94d | 1327 | fdctrl->msr &= ~FD_MSR_NONDMA; |
34abf9a7 | 1328 | |
83a26013 | 1329 | fdctrl_to_result_phase(fdctrl, 7); |
d497d534 | 1330 | fdctrl_raise_irq(fdctrl); |
8977f3c1 FB |
1331 | } |
1332 | ||
1333 | /* Prepare a data transfer (either DMA or FIFO) */ | |
5c02c033 | 1334 | static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1335 | { |
5c02c033 | 1336 | FDrive *cur_drv; |
8977f3c1 | 1337 | uint8_t kh, kt, ks; |
8977f3c1 | 1338 | |
cefec4f5 | 1339 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
baca51fa FB |
1340 | cur_drv = get_cur_drv(fdctrl); |
1341 | kt = fdctrl->fifo[2]; | |
1342 | kh = fdctrl->fifo[3]; | |
1343 | ks = fdctrl->fifo[4]; | |
4b19ec0c | 1344 | FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n", |
cefec4f5 | 1345 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
08388273 HP |
1346 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
1347 | NUM_SIDES(cur_drv))); | |
77370520 | 1348 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
8977f3c1 FB |
1349 | case 2: |
1350 | /* sect too big */ | |
9fea808a | 1351 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1352 | fdctrl->fifo[3] = kt; |
1353 | fdctrl->fifo[4] = kh; | |
1354 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1355 | return; |
1356 | case 3: | |
1357 | /* track too big */ | |
77370520 | 1358 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
baca51fa FB |
1359 | fdctrl->fifo[3] = kt; |
1360 | fdctrl->fifo[4] = kh; | |
1361 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1362 | return; |
1363 | case 4: | |
1364 | /* No seek enabled */ | |
9fea808a | 1365 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1366 | fdctrl->fifo[3] = kt; |
1367 | fdctrl->fifo[4] = kh; | |
1368 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1369 | return; |
1370 | case 1: | |
d6ed4e21 | 1371 | fdctrl->status0 |= FD_SR0_SEEK; |
8977f3c1 FB |
1372 | break; |
1373 | default: | |
1374 | break; | |
1375 | } | |
b9b3d225 | 1376 | |
844f65d6 HP |
1377 | /* Check the data rate. If the programmed data rate does not match |
1378 | * the currently inserted medium, the operation has to fail. */ | |
1379 | if (fdctrl->check_media_rate && | |
1380 | (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { | |
1381 | FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n", | |
1382 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); | |
1383 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); | |
1384 | fdctrl->fifo[3] = kt; | |
1385 | fdctrl->fifo[4] = kh; | |
1386 | fdctrl->fifo[5] = ks; | |
1387 | return; | |
1388 | } | |
1389 | ||
8977f3c1 | 1390 | /* Set the FIFO state */ |
baca51fa FB |
1391 | fdctrl->data_dir = direction; |
1392 | fdctrl->data_pos = 0; | |
27c86e24 | 1393 | assert(fdctrl->msr & FD_MSR_CMDBUSY); |
baca51fa FB |
1394 | if (fdctrl->fifo[0] & 0x80) |
1395 | fdctrl->data_state |= FD_STATE_MULTI; | |
1396 | else | |
1397 | fdctrl->data_state &= ~FD_STATE_MULTI; | |
c83f97b5 | 1398 | if (fdctrl->fifo[5] == 0) { |
baca51fa FB |
1399 | fdctrl->data_len = fdctrl->fifo[8]; |
1400 | } else { | |
4f431960 | 1401 | int tmp; |
3bcb80f1 | 1402 | fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]); |
771effeb | 1403 | tmp = (fdctrl->fifo[6] - ks + 1); |
baca51fa | 1404 | if (fdctrl->fifo[0] & 0x80) |
771effeb | 1405 | tmp += fdctrl->fifo[6]; |
4f431960 | 1406 | fdctrl->data_len *= tmp; |
baca51fa | 1407 | } |
890fa6be | 1408 | fdctrl->eot = fdctrl->fifo[6]; |
368df94d | 1409 | if (fdctrl->dor & FD_DOR_DMAEN) { |
8977f3c1 FB |
1410 | int dma_mode; |
1411 | /* DMA transfer are enabled. Check if DMA channel is well programmed */ | |
baca51fa | 1412 | dma_mode = DMA_get_channel_mode(fdctrl->dma_chann); |
8977f3c1 | 1413 | dma_mode = (dma_mode >> 2) & 3; |
baca51fa | 1414 | FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n", |
4f431960 | 1415 | dma_mode, direction, |
baca51fa | 1416 | (128 << fdctrl->fifo[5]) * |
4f431960 | 1417 | (cur_drv->last_sect - ks + 1), fdctrl->data_len); |
8977f3c1 FB |
1418 | if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL || |
1419 | direction == FD_DIR_SCANH) && dma_mode == 0) || | |
1420 | (direction == FD_DIR_WRITE && dma_mode == 2) || | |
7ea004ed HP |
1421 | (direction == FD_DIR_READ && dma_mode == 1) || |
1422 | (direction == FD_DIR_VERIFY)) { | |
8977f3c1 | 1423 | /* No access is allowed until DMA transfer has completed */ |
b9b3d225 | 1424 | fdctrl->msr &= ~FD_MSR_RQM; |
7ea004ed HP |
1425 | if (direction != FD_DIR_VERIFY) { |
1426 | /* Now, we just have to wait for the DMA controller to | |
1427 | * recall us... | |
1428 | */ | |
1429 | DMA_hold_DREQ(fdctrl->dma_chann); | |
1430 | DMA_schedule(fdctrl->dma_chann); | |
1431 | } else { | |
1432 | /* Start transfer */ | |
1433 | fdctrl_transfer_handler(fdctrl, fdctrl->dma_chann, 0, | |
1434 | fdctrl->data_len); | |
1435 | } | |
8977f3c1 | 1436 | return; |
baca51fa | 1437 | } else { |
cced7a13 BS |
1438 | FLOPPY_DPRINTF("bad dma_mode=%d direction=%d\n", dma_mode, |
1439 | direction); | |
8977f3c1 FB |
1440 | } |
1441 | } | |
1442 | FLOPPY_DPRINTF("start non-DMA transfer\n"); | |
6cc8a11c | 1443 | fdctrl->msr |= FD_MSR_NONDMA | FD_MSR_RQM; |
b9b3d225 BS |
1444 | if (direction != FD_DIR_WRITE) |
1445 | fdctrl->msr |= FD_MSR_DIO; | |
8977f3c1 | 1446 | /* IO based transfer: calculate len */ |
d497d534 | 1447 | fdctrl_raise_irq(fdctrl); |
8977f3c1 FB |
1448 | } |
1449 | ||
1450 | /* Prepare a transfer of deleted data */ | |
5c02c033 | 1451 | static void fdctrl_start_transfer_del(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1452 | { |
cced7a13 | 1453 | qemu_log_mask(LOG_UNIMP, "fdctrl_start_transfer_del() unimplemented\n"); |
77370520 | 1454 | |
8977f3c1 FB |
1455 | /* We don't handle deleted data, |
1456 | * so we don't return *ANYTHING* | |
1457 | */ | |
9fea808a | 1458 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
8977f3c1 FB |
1459 | } |
1460 | ||
1461 | /* handlers for DMA transfers */ | |
85571bc7 FB |
1462 | static int fdctrl_transfer_handler (void *opaque, int nchan, |
1463 | int dma_pos, int dma_len) | |
8977f3c1 | 1464 | { |
5c02c033 BS |
1465 | FDCtrl *fdctrl; |
1466 | FDrive *cur_drv; | |
baca51fa | 1467 | int len, start_pos, rel_pos; |
8977f3c1 FB |
1468 | uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00; |
1469 | ||
baca51fa | 1470 | fdctrl = opaque; |
b9b3d225 | 1471 | if (fdctrl->msr & FD_MSR_RQM) { |
8977f3c1 FB |
1472 | FLOPPY_DPRINTF("Not in DMA transfer mode !\n"); |
1473 | return 0; | |
1474 | } | |
baca51fa FB |
1475 | cur_drv = get_cur_drv(fdctrl); |
1476 | if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL || | |
1477 | fdctrl->data_dir == FD_DIR_SCANH) | |
77370520 | 1478 | status2 = FD_SR2_SNS; |
85571bc7 FB |
1479 | if (dma_len > fdctrl->data_len) |
1480 | dma_len = fdctrl->data_len; | |
4be74634 | 1481 | if (cur_drv->blk == NULL) { |
4f431960 | 1482 | if (fdctrl->data_dir == FD_DIR_WRITE) |
9fea808a | 1483 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
4f431960 | 1484 | else |
9fea808a | 1485 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
4f431960 | 1486 | len = 0; |
890fa6be FB |
1487 | goto transfer_error; |
1488 | } | |
baca51fa | 1489 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; |
85571bc7 FB |
1490 | for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) { |
1491 | len = dma_len - fdctrl->data_pos; | |
baca51fa FB |
1492 | if (len + rel_pos > FD_SECTOR_LEN) |
1493 | len = FD_SECTOR_LEN - rel_pos; | |
6f7e9aec FB |
1494 | FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x " |
1495 | "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos, | |
cefec4f5 | 1496 | fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head, |
baca51fa | 1497 | cur_drv->track, cur_drv->sect, fd_sector(cur_drv), |
9fea808a | 1498 | fd_sector(cur_drv) * FD_SECTOR_LEN); |
baca51fa | 1499 | if (fdctrl->data_dir != FD_DIR_WRITE || |
4f431960 | 1500 | len < FD_SECTOR_LEN || rel_pos != 0) { |
baca51fa | 1501 | /* READ & SCAN commands and realign to a sector for WRITE */ |
4be74634 MA |
1502 | if (blk_read(cur_drv->blk, fd_sector(cur_drv), |
1503 | fdctrl->fifo, 1) < 0) { | |
8977f3c1 FB |
1504 | FLOPPY_DPRINTF("Floppy: error getting sector %d\n", |
1505 | fd_sector(cur_drv)); | |
1506 | /* Sure, image size is too small... */ | |
baca51fa | 1507 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); |
8977f3c1 | 1508 | } |
890fa6be | 1509 | } |
4f431960 JM |
1510 | switch (fdctrl->data_dir) { |
1511 | case FD_DIR_READ: | |
1512 | /* READ commands */ | |
85571bc7 FB |
1513 | DMA_write_memory (nchan, fdctrl->fifo + rel_pos, |
1514 | fdctrl->data_pos, len); | |
4f431960 JM |
1515 | break; |
1516 | case FD_DIR_WRITE: | |
baca51fa | 1517 | /* WRITE commands */ |
8510854e HP |
1518 | if (cur_drv->ro) { |
1519 | /* Handle readonly medium early, no need to do DMA, touch the | |
1520 | * LED or attempt any writes. A real floppy doesn't attempt | |
1521 | * to write to readonly media either. */ | |
1522 | fdctrl_stop_transfer(fdctrl, | |
1523 | FD_SR0_ABNTERM | FD_SR0_SEEK, FD_SR1_NW, | |
1524 | 0x00); | |
1525 | goto transfer_error; | |
1526 | } | |
1527 | ||
85571bc7 FB |
1528 | DMA_read_memory (nchan, fdctrl->fifo + rel_pos, |
1529 | fdctrl->data_pos, len); | |
4be74634 MA |
1530 | if (blk_write(cur_drv->blk, fd_sector(cur_drv), |
1531 | fdctrl->fifo, 1) < 0) { | |
cced7a13 BS |
1532 | FLOPPY_DPRINTF("error writing sector %d\n", |
1533 | fd_sector(cur_drv)); | |
9fea808a | 1534 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
baca51fa | 1535 | goto transfer_error; |
890fa6be | 1536 | } |
4f431960 | 1537 | break; |
7ea004ed HP |
1538 | case FD_DIR_VERIFY: |
1539 | /* VERIFY commands */ | |
1540 | break; | |
4f431960 JM |
1541 | default: |
1542 | /* SCAN commands */ | |
baca51fa | 1543 | { |
4f431960 | 1544 | uint8_t tmpbuf[FD_SECTOR_LEN]; |
baca51fa | 1545 | int ret; |
85571bc7 | 1546 | DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len); |
baca51fa | 1547 | ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len); |
8977f3c1 | 1548 | if (ret == 0) { |
77370520 | 1549 | status2 = FD_SR2_SEH; |
8977f3c1 FB |
1550 | goto end_transfer; |
1551 | } | |
baca51fa FB |
1552 | if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) || |
1553 | (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) { | |
8977f3c1 FB |
1554 | status2 = 0x00; |
1555 | goto end_transfer; | |
1556 | } | |
1557 | } | |
4f431960 | 1558 | break; |
8977f3c1 | 1559 | } |
4f431960 JM |
1560 | fdctrl->data_pos += len; |
1561 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; | |
baca51fa | 1562 | if (rel_pos == 0) { |
8977f3c1 | 1563 | /* Seek to next sector */ |
746d6de7 BS |
1564 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) |
1565 | break; | |
8977f3c1 FB |
1566 | } |
1567 | } | |
4f431960 | 1568 | end_transfer: |
baca51fa FB |
1569 | len = fdctrl->data_pos - start_pos; |
1570 | FLOPPY_DPRINTF("end transfer %d %d %d\n", | |
4f431960 | 1571 | fdctrl->data_pos, len, fdctrl->data_len); |
baca51fa FB |
1572 | if (fdctrl->data_dir == FD_DIR_SCANE || |
1573 | fdctrl->data_dir == FD_DIR_SCANL || | |
1574 | fdctrl->data_dir == FD_DIR_SCANH) | |
77370520 | 1575 | status2 = FD_SR2_SEH; |
baca51fa | 1576 | fdctrl->data_len -= len; |
890fa6be | 1577 | fdctrl_stop_transfer(fdctrl, status0, status1, status2); |
4f431960 | 1578 | transfer_error: |
8977f3c1 | 1579 | |
baca51fa | 1580 | return len; |
8977f3c1 FB |
1581 | } |
1582 | ||
8977f3c1 | 1583 | /* Data register : 0x05 */ |
5c02c033 | 1584 | static uint32_t fdctrl_read_data(FDCtrl *fdctrl) |
8977f3c1 | 1585 | { |
5c02c033 | 1586 | FDrive *cur_drv; |
8977f3c1 | 1587 | uint32_t retval = 0; |
e9077462 | 1588 | uint32_t pos; |
8977f3c1 | 1589 | |
baca51fa | 1590 | cur_drv = get_cur_drv(fdctrl); |
b9b3d225 BS |
1591 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
1592 | if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) { | |
cced7a13 | 1593 | FLOPPY_DPRINTF("error: controller not ready for reading\n"); |
8977f3c1 FB |
1594 | return 0; |
1595 | } | |
f6c2d1d8 KW |
1596 | |
1597 | /* If data_len spans multiple sectors, the current position in the FIFO | |
1598 | * wraps around while fdctrl->data_pos is the real position in the whole | |
1599 | * request. */ | |
baca51fa | 1600 | pos = fdctrl->data_pos; |
e9077462 | 1601 | pos %= FD_SECTOR_LEN; |
f6c2d1d8 KW |
1602 | |
1603 | switch (fdctrl->phase) { | |
1604 | case FD_PHASE_EXECUTION: | |
1605 | assert(fdctrl->msr & FD_MSR_NONDMA); | |
8977f3c1 | 1606 | if (pos == 0) { |
746d6de7 BS |
1607 | if (fdctrl->data_pos != 0) |
1608 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { | |
1609 | FLOPPY_DPRINTF("error seeking to next sector %d\n", | |
1610 | fd_sector(cur_drv)); | |
1611 | return 0; | |
1612 | } | |
4be74634 MA |
1613 | if (blk_read(cur_drv->blk, fd_sector(cur_drv), fdctrl->fifo, 1) |
1614 | < 0) { | |
77370520 BS |
1615 | FLOPPY_DPRINTF("error getting sector %d\n", |
1616 | fd_sector(cur_drv)); | |
1617 | /* Sure, image size is too small... */ | |
1618 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); | |
1619 | } | |
8977f3c1 | 1620 | } |
f6c2d1d8 KW |
1621 | |
1622 | if (++fdctrl->data_pos == fdctrl->data_len) { | |
6cc8a11c | 1623 | fdctrl->msr &= ~FD_MSR_RQM; |
c5139bd9 | 1624 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
f6c2d1d8 KW |
1625 | } |
1626 | break; | |
1627 | ||
1628 | case FD_PHASE_RESULT: | |
1629 | assert(!(fdctrl->msr & FD_MSR_NONDMA)); | |
1630 | if (++fdctrl->data_pos == fdctrl->data_len) { | |
6cc8a11c | 1631 | fdctrl->msr &= ~FD_MSR_RQM; |
07e415f2 | 1632 | fdctrl_to_command_phase(fdctrl); |
ed5fd2cc FB |
1633 | fdctrl_reset_irq(fdctrl); |
1634 | } | |
f6c2d1d8 KW |
1635 | break; |
1636 | ||
1637 | case FD_PHASE_COMMAND: | |
1638 | default: | |
1639 | abort(); | |
8977f3c1 | 1640 | } |
f6c2d1d8 KW |
1641 | |
1642 | retval = fdctrl->fifo[pos]; | |
8977f3c1 FB |
1643 | FLOPPY_DPRINTF("data register: 0x%02x\n", retval); |
1644 | ||
1645 | return retval; | |
1646 | } | |
1647 | ||
5c02c033 | 1648 | static void fdctrl_format_sector(FDCtrl *fdctrl) |
8977f3c1 | 1649 | { |
5c02c033 | 1650 | FDrive *cur_drv; |
baca51fa | 1651 | uint8_t kh, kt, ks; |
8977f3c1 | 1652 | |
cefec4f5 | 1653 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
baca51fa FB |
1654 | cur_drv = get_cur_drv(fdctrl); |
1655 | kt = fdctrl->fifo[6]; | |
1656 | kh = fdctrl->fifo[7]; | |
1657 | ks = fdctrl->fifo[8]; | |
1658 | FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n", | |
cefec4f5 | 1659 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
08388273 HP |
1660 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
1661 | NUM_SIDES(cur_drv))); | |
9fea808a | 1662 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
baca51fa FB |
1663 | case 2: |
1664 | /* sect too big */ | |
9fea808a | 1665 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1666 | fdctrl->fifo[3] = kt; |
1667 | fdctrl->fifo[4] = kh; | |
1668 | fdctrl->fifo[5] = ks; | |
1669 | return; | |
1670 | case 3: | |
1671 | /* track too big */ | |
77370520 | 1672 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
baca51fa FB |
1673 | fdctrl->fifo[3] = kt; |
1674 | fdctrl->fifo[4] = kh; | |
1675 | fdctrl->fifo[5] = ks; | |
1676 | return; | |
1677 | case 4: | |
1678 | /* No seek enabled */ | |
9fea808a | 1679 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1680 | fdctrl->fifo[3] = kt; |
1681 | fdctrl->fifo[4] = kh; | |
1682 | fdctrl->fifo[5] = ks; | |
1683 | return; | |
1684 | case 1: | |
cd30b53d | 1685 | fdctrl->status0 |= FD_SR0_SEEK; |
baca51fa FB |
1686 | break; |
1687 | default: | |
1688 | break; | |
1689 | } | |
1690 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); | |
4be74634 MA |
1691 | if (cur_drv->blk == NULL || |
1692 | blk_write(cur_drv->blk, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) { | |
cced7a13 | 1693 | FLOPPY_DPRINTF("error formatting sector %d\n", fd_sector(cur_drv)); |
9fea808a | 1694 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
baca51fa | 1695 | } else { |
4f431960 JM |
1696 | if (cur_drv->sect == cur_drv->last_sect) { |
1697 | fdctrl->data_state &= ~FD_STATE_FORMAT; | |
1698 | /* Last sector done */ | |
cd30b53d | 1699 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
4f431960 JM |
1700 | } else { |
1701 | /* More to do */ | |
1702 | fdctrl->data_pos = 0; | |
1703 | fdctrl->data_len = 4; | |
1704 | } | |
baca51fa FB |
1705 | } |
1706 | } | |
1707 | ||
5c02c033 | 1708 | static void fdctrl_handle_lock(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1709 | { |
1710 | fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0; | |
1711 | fdctrl->fifo[0] = fdctrl->lock << 4; | |
83a26013 | 1712 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1713 | } |
1714 | ||
5c02c033 | 1715 | static void fdctrl_handle_dumpreg(FDCtrl *fdctrl, int direction) |
65cef780 | 1716 | { |
5c02c033 | 1717 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1718 | |
1719 | /* Drives position */ | |
1720 | fdctrl->fifo[0] = drv0(fdctrl)->track; | |
1721 | fdctrl->fifo[1] = drv1(fdctrl)->track; | |
78ae820c BS |
1722 | #if MAX_FD == 4 |
1723 | fdctrl->fifo[2] = drv2(fdctrl)->track; | |
1724 | fdctrl->fifo[3] = drv3(fdctrl)->track; | |
1725 | #else | |
65cef780 BS |
1726 | fdctrl->fifo[2] = 0; |
1727 | fdctrl->fifo[3] = 0; | |
78ae820c | 1728 | #endif |
65cef780 BS |
1729 | /* timers */ |
1730 | fdctrl->fifo[4] = fdctrl->timer0; | |
368df94d | 1731 | fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0); |
65cef780 BS |
1732 | fdctrl->fifo[6] = cur_drv->last_sect; |
1733 | fdctrl->fifo[7] = (fdctrl->lock << 7) | | |
1734 | (cur_drv->perpendicular << 2); | |
1735 | fdctrl->fifo[8] = fdctrl->config; | |
1736 | fdctrl->fifo[9] = fdctrl->precomp_trk; | |
83a26013 | 1737 | fdctrl_to_result_phase(fdctrl, 10); |
65cef780 BS |
1738 | } |
1739 | ||
5c02c033 | 1740 | static void fdctrl_handle_version(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1741 | { |
1742 | /* Controller's version */ | |
1743 | fdctrl->fifo[0] = fdctrl->version; | |
83a26013 | 1744 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1745 | } |
1746 | ||
5c02c033 | 1747 | static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1748 | { |
1749 | fdctrl->fifo[0] = 0x41; /* Stepping 1 */ | |
83a26013 | 1750 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1751 | } |
1752 | ||
5c02c033 | 1753 | static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction) |
65cef780 | 1754 | { |
5c02c033 | 1755 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1756 | |
1757 | /* Drives position */ | |
1758 | drv0(fdctrl)->track = fdctrl->fifo[3]; | |
1759 | drv1(fdctrl)->track = fdctrl->fifo[4]; | |
78ae820c BS |
1760 | #if MAX_FD == 4 |
1761 | drv2(fdctrl)->track = fdctrl->fifo[5]; | |
1762 | drv3(fdctrl)->track = fdctrl->fifo[6]; | |
1763 | #endif | |
65cef780 BS |
1764 | /* timers */ |
1765 | fdctrl->timer0 = fdctrl->fifo[7]; | |
1766 | fdctrl->timer1 = fdctrl->fifo[8]; | |
1767 | cur_drv->last_sect = fdctrl->fifo[9]; | |
1768 | fdctrl->lock = fdctrl->fifo[10] >> 7; | |
1769 | cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF; | |
1770 | fdctrl->config = fdctrl->fifo[11]; | |
1771 | fdctrl->precomp_trk = fdctrl->fifo[12]; | |
1772 | fdctrl->pwrd = fdctrl->fifo[13]; | |
07e415f2 | 1773 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1774 | } |
1775 | ||
5c02c033 | 1776 | static void fdctrl_handle_save(FDCtrl *fdctrl, int direction) |
65cef780 | 1777 | { |
5c02c033 | 1778 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1779 | |
1780 | fdctrl->fifo[0] = 0; | |
1781 | fdctrl->fifo[1] = 0; | |
1782 | /* Drives position */ | |
1783 | fdctrl->fifo[2] = drv0(fdctrl)->track; | |
1784 | fdctrl->fifo[3] = drv1(fdctrl)->track; | |
78ae820c BS |
1785 | #if MAX_FD == 4 |
1786 | fdctrl->fifo[4] = drv2(fdctrl)->track; | |
1787 | fdctrl->fifo[5] = drv3(fdctrl)->track; | |
1788 | #else | |
65cef780 BS |
1789 | fdctrl->fifo[4] = 0; |
1790 | fdctrl->fifo[5] = 0; | |
78ae820c | 1791 | #endif |
65cef780 BS |
1792 | /* timers */ |
1793 | fdctrl->fifo[6] = fdctrl->timer0; | |
1794 | fdctrl->fifo[7] = fdctrl->timer1; | |
1795 | fdctrl->fifo[8] = cur_drv->last_sect; | |
1796 | fdctrl->fifo[9] = (fdctrl->lock << 7) | | |
1797 | (cur_drv->perpendicular << 2); | |
1798 | fdctrl->fifo[10] = fdctrl->config; | |
1799 | fdctrl->fifo[11] = fdctrl->precomp_trk; | |
1800 | fdctrl->fifo[12] = fdctrl->pwrd; | |
1801 | fdctrl->fifo[13] = 0; | |
1802 | fdctrl->fifo[14] = 0; | |
83a26013 | 1803 | fdctrl_to_result_phase(fdctrl, 15); |
65cef780 BS |
1804 | } |
1805 | ||
5c02c033 | 1806 | static void fdctrl_handle_readid(FDCtrl *fdctrl, int direction) |
65cef780 | 1807 | { |
5c02c033 | 1808 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1809 | |
65cef780 | 1810 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; |
bc72ad67 AB |
1811 | timer_mod(fdctrl->result_timer, |
1812 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (get_ticks_per_sec() / 50)); | |
65cef780 BS |
1813 | } |
1814 | ||
5c02c033 | 1815 | static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction) |
65cef780 | 1816 | { |
5c02c033 | 1817 | FDrive *cur_drv; |
65cef780 | 1818 | |
cefec4f5 | 1819 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1820 | cur_drv = get_cur_drv(fdctrl); |
1821 | fdctrl->data_state |= FD_STATE_FORMAT; | |
1822 | if (fdctrl->fifo[0] & 0x80) | |
1823 | fdctrl->data_state |= FD_STATE_MULTI; | |
1824 | else | |
1825 | fdctrl->data_state &= ~FD_STATE_MULTI; | |
65cef780 BS |
1826 | cur_drv->bps = |
1827 | fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2]; | |
1828 | #if 0 | |
1829 | cur_drv->last_sect = | |
1830 | cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] : | |
1831 | fdctrl->fifo[3] / 2; | |
1832 | #else | |
1833 | cur_drv->last_sect = fdctrl->fifo[3]; | |
1834 | #endif | |
1835 | /* TODO: implement format using DMA expected by the Bochs BIOS | |
1836 | * and Linux fdformat (read 3 bytes per sector via DMA and fill | |
1837 | * the sector with the specified fill byte | |
1838 | */ | |
1839 | fdctrl->data_state &= ~FD_STATE_FORMAT; | |
1840 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); | |
1841 | } | |
1842 | ||
5c02c033 | 1843 | static void fdctrl_handle_specify(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1844 | { |
1845 | fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF; | |
1846 | fdctrl->timer1 = fdctrl->fifo[2] >> 1; | |
368df94d BS |
1847 | if (fdctrl->fifo[2] & 1) |
1848 | fdctrl->dor &= ~FD_DOR_DMAEN; | |
1849 | else | |
1850 | fdctrl->dor |= FD_DOR_DMAEN; | |
65cef780 | 1851 | /* No result back */ |
07e415f2 | 1852 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1853 | } |
1854 | ||
5c02c033 | 1855 | static void fdctrl_handle_sense_drive_status(FDCtrl *fdctrl, int direction) |
65cef780 | 1856 | { |
5c02c033 | 1857 | FDrive *cur_drv; |
65cef780 | 1858 | |
cefec4f5 | 1859 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1860 | cur_drv = get_cur_drv(fdctrl); |
1861 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; | |
1862 | /* 1 Byte status back */ | |
1863 | fdctrl->fifo[0] = (cur_drv->ro << 6) | | |
1864 | (cur_drv->track == 0 ? 0x10 : 0x00) | | |
1865 | (cur_drv->head << 2) | | |
cefec4f5 | 1866 | GET_CUR_DRV(fdctrl) | |
65cef780 | 1867 | 0x28; |
83a26013 | 1868 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1869 | } |
1870 | ||
5c02c033 | 1871 | static void fdctrl_handle_recalibrate(FDCtrl *fdctrl, int direction) |
65cef780 | 1872 | { |
5c02c033 | 1873 | FDrive *cur_drv; |
65cef780 | 1874 | |
cefec4f5 | 1875 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1876 | cur_drv = get_cur_drv(fdctrl); |
1877 | fd_recalibrate(cur_drv); | |
07e415f2 | 1878 | fdctrl_to_command_phase(fdctrl); |
65cef780 | 1879 | /* Raise Interrupt */ |
d497d534 HP |
1880 | fdctrl->status0 |= FD_SR0_SEEK; |
1881 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
1882 | } |
1883 | ||
5c02c033 | 1884 | static void fdctrl_handle_sense_interrupt_status(FDCtrl *fdctrl, int direction) |
65cef780 | 1885 | { |
5c02c033 | 1886 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1887 | |
2fee0088 | 1888 | if (fdctrl->reset_sensei > 0) { |
f2d81b33 BS |
1889 | fdctrl->fifo[0] = |
1890 | FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei; | |
1891 | fdctrl->reset_sensei--; | |
2fee0088 PH |
1892 | } else if (!(fdctrl->sra & FD_SRA_INTPEND)) { |
1893 | fdctrl->fifo[0] = FD_SR0_INVCMD; | |
83a26013 | 1894 | fdctrl_to_result_phase(fdctrl, 1); |
2fee0088 | 1895 | return; |
f2d81b33 | 1896 | } else { |
f2d81b33 | 1897 | fdctrl->fifo[0] = |
2fee0088 PH |
1898 | (fdctrl->status0 & ~(FD_SR0_HEAD | FD_SR0_DS1 | FD_SR0_DS0)) |
1899 | | GET_CUR_DRV(fdctrl); | |
f2d81b33 BS |
1900 | } |
1901 | ||
65cef780 | 1902 | fdctrl->fifo[1] = cur_drv->track; |
83a26013 | 1903 | fdctrl_to_result_phase(fdctrl, 2); |
65cef780 | 1904 | fdctrl_reset_irq(fdctrl); |
77370520 | 1905 | fdctrl->status0 = FD_SR0_RDYCHG; |
65cef780 BS |
1906 | } |
1907 | ||
5c02c033 | 1908 | static void fdctrl_handle_seek(FDCtrl *fdctrl, int direction) |
65cef780 | 1909 | { |
5c02c033 | 1910 | FDrive *cur_drv; |
65cef780 | 1911 | |
cefec4f5 | 1912 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1913 | cur_drv = get_cur_drv(fdctrl); |
07e415f2 | 1914 | fdctrl_to_command_phase(fdctrl); |
b072a3c8 HP |
1915 | /* The seek command just sends step pulses to the drive and doesn't care if |
1916 | * there is a medium inserted of if it's banging the head against the drive. | |
1917 | */ | |
6be01b1e | 1918 | fd_seek(cur_drv, cur_drv->head, fdctrl->fifo[2], cur_drv->sect, 1); |
b072a3c8 | 1919 | /* Raise Interrupt */ |
d497d534 HP |
1920 | fdctrl->status0 |= FD_SR0_SEEK; |
1921 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
1922 | } |
1923 | ||
5c02c033 | 1924 | static void fdctrl_handle_perpendicular_mode(FDCtrl *fdctrl, int direction) |
65cef780 | 1925 | { |
5c02c033 | 1926 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1927 | |
1928 | if (fdctrl->fifo[1] & 0x80) | |
1929 | cur_drv->perpendicular = fdctrl->fifo[1] & 0x7; | |
1930 | /* No result back */ | |
07e415f2 | 1931 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1932 | } |
1933 | ||
5c02c033 | 1934 | static void fdctrl_handle_configure(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1935 | { |
1936 | fdctrl->config = fdctrl->fifo[2]; | |
1937 | fdctrl->precomp_trk = fdctrl->fifo[3]; | |
1938 | /* No result back */ | |
07e415f2 | 1939 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1940 | } |
1941 | ||
5c02c033 | 1942 | static void fdctrl_handle_powerdown_mode(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1943 | { |
1944 | fdctrl->pwrd = fdctrl->fifo[1]; | |
1945 | fdctrl->fifo[0] = fdctrl->fifo[1]; | |
83a26013 | 1946 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1947 | } |
1948 | ||
5c02c033 | 1949 | static void fdctrl_handle_option(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1950 | { |
1951 | /* No result back */ | |
07e415f2 | 1952 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1953 | } |
1954 | ||
5c02c033 | 1955 | static void fdctrl_handle_drive_specification_command(FDCtrl *fdctrl, int direction) |
65cef780 | 1956 | { |
5c02c033 | 1957 | FDrive *cur_drv = get_cur_drv(fdctrl); |
e9077462 | 1958 | uint32_t pos; |
65cef780 | 1959 | |
e9077462 PM |
1960 | pos = fdctrl->data_pos - 1; |
1961 | pos %= FD_SECTOR_LEN; | |
1962 | if (fdctrl->fifo[pos] & 0x80) { | |
65cef780 | 1963 | /* Command parameters done */ |
e9077462 | 1964 | if (fdctrl->fifo[pos] & 0x40) { |
65cef780 BS |
1965 | fdctrl->fifo[0] = fdctrl->fifo[1]; |
1966 | fdctrl->fifo[2] = 0; | |
1967 | fdctrl->fifo[3] = 0; | |
83a26013 | 1968 | fdctrl_to_result_phase(fdctrl, 4); |
65cef780 | 1969 | } else { |
07e415f2 | 1970 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1971 | } |
1972 | } else if (fdctrl->data_len > 7) { | |
1973 | /* ERROR */ | |
1974 | fdctrl->fifo[0] = 0x80 | | |
cefec4f5 | 1975 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl); |
83a26013 | 1976 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1977 | } |
1978 | } | |
1979 | ||
6d013772 | 1980 | static void fdctrl_handle_relative_seek_in(FDCtrl *fdctrl, int direction) |
65cef780 | 1981 | { |
5c02c033 | 1982 | FDrive *cur_drv; |
65cef780 | 1983 | |
cefec4f5 | 1984 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1985 | cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1986 | if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) { |
6be01b1e PH |
1987 | fd_seek(cur_drv, cur_drv->head, cur_drv->max_track - 1, |
1988 | cur_drv->sect, 1); | |
65cef780 | 1989 | } else { |
6d013772 PH |
1990 | fd_seek(cur_drv, cur_drv->head, |
1991 | cur_drv->track + fdctrl->fifo[2], cur_drv->sect, 1); | |
65cef780 | 1992 | } |
07e415f2 | 1993 | fdctrl_to_command_phase(fdctrl); |
77370520 | 1994 | /* Raise Interrupt */ |
d497d534 HP |
1995 | fdctrl->status0 |= FD_SR0_SEEK; |
1996 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
1997 | } |
1998 | ||
6d013772 | 1999 | static void fdctrl_handle_relative_seek_out(FDCtrl *fdctrl, int direction) |
65cef780 | 2000 | { |
5c02c033 | 2001 | FDrive *cur_drv; |
65cef780 | 2002 | |
cefec4f5 | 2003 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 2004 | cur_drv = get_cur_drv(fdctrl); |
65cef780 | 2005 | if (fdctrl->fifo[2] > cur_drv->track) { |
6be01b1e | 2006 | fd_seek(cur_drv, cur_drv->head, 0, cur_drv->sect, 1); |
65cef780 | 2007 | } else { |
6d013772 PH |
2008 | fd_seek(cur_drv, cur_drv->head, |
2009 | cur_drv->track - fdctrl->fifo[2], cur_drv->sect, 1); | |
65cef780 | 2010 | } |
07e415f2 | 2011 | fdctrl_to_command_phase(fdctrl); |
65cef780 | 2012 | /* Raise Interrupt */ |
d497d534 HP |
2013 | fdctrl->status0 |= FD_SR0_SEEK; |
2014 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
2015 | } |
2016 | ||
85d291a0 KW |
2017 | /* |
2018 | * Handlers for the execution phase of each command | |
2019 | */ | |
d275b33d | 2020 | typedef struct FDCtrlCommand { |
678803ab BS |
2021 | uint8_t value; |
2022 | uint8_t mask; | |
2023 | const char* name; | |
2024 | int parameters; | |
5c02c033 | 2025 | void (*handler)(FDCtrl *fdctrl, int direction); |
678803ab | 2026 | int direction; |
d275b33d KW |
2027 | } FDCtrlCommand; |
2028 | ||
2029 | static const FDCtrlCommand handlers[] = { | |
678803ab BS |
2030 | { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ }, |
2031 | { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE }, | |
2032 | { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek }, | |
2033 | { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status }, | |
2034 | { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate }, | |
2035 | { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track }, | |
2036 | { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ }, | |
2037 | { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */ | |
2038 | { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */ | |
2039 | { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ }, | |
2040 | { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE }, | |
7ea004ed | 2041 | { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_start_transfer, FD_DIR_VERIFY }, |
678803ab BS |
2042 | { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL }, |
2043 | { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH }, | |
2044 | { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE }, | |
2045 | { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid }, | |
2046 | { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify }, | |
2047 | { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status }, | |
2048 | { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode }, | |
2049 | { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure }, | |
2050 | { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode }, | |
2051 | { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option }, | |
2052 | { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command }, | |
2053 | { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out }, | |
2054 | { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented }, | |
2055 | { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in }, | |
2056 | { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock }, | |
2057 | { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg }, | |
2058 | { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version }, | |
2059 | { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid }, | |
2060 | { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */ | |
2061 | { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */ | |
2062 | }; | |
2063 | /* Associate command to an index in the 'handlers' array */ | |
2064 | static uint8_t command_to_handler[256]; | |
2065 | ||
d275b33d KW |
2066 | static const FDCtrlCommand *get_command(uint8_t cmd) |
2067 | { | |
2068 | int idx; | |
2069 | ||
2070 | idx = command_to_handler[cmd]; | |
2071 | FLOPPY_DPRINTF("%s command\n", handlers[idx].name); | |
2072 | return &handlers[idx]; | |
2073 | } | |
2074 | ||
5c02c033 | 2075 | static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value) |
baca51fa | 2076 | { |
5c02c033 | 2077 | FDrive *cur_drv; |
d275b33d | 2078 | const FDCtrlCommand *cmd; |
e9077462 | 2079 | uint32_t pos; |
baca51fa | 2080 | |
8977f3c1 | 2081 | /* Reset mode */ |
1c346df2 | 2082 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4b19ec0c | 2083 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
8977f3c1 FB |
2084 | return; |
2085 | } | |
b9b3d225 | 2086 | if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) { |
cced7a13 | 2087 | FLOPPY_DPRINTF("error: controller not ready for writing\n"); |
8977f3c1 FB |
2088 | return; |
2089 | } | |
b9b3d225 | 2090 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
5b0a25e8 | 2091 | |
d275b33d KW |
2092 | FLOPPY_DPRINTF("%s: %02x\n", __func__, value); |
2093 | ||
2094 | /* If data_len spans multiple sectors, the current position in the FIFO | |
2095 | * wraps around while fdctrl->data_pos is the real position in the whole | |
2096 | * request. */ | |
2097 | pos = fdctrl->data_pos++; | |
2098 | pos %= FD_SECTOR_LEN; | |
2099 | fdctrl->fifo[pos] = value; | |
2100 | ||
6cc8a11c KW |
2101 | if (fdctrl->data_pos == fdctrl->data_len) { |
2102 | fdctrl->msr &= ~FD_MSR_RQM; | |
2103 | } | |
2104 | ||
5b0a25e8 KW |
2105 | switch (fdctrl->phase) { |
2106 | case FD_PHASE_EXECUTION: | |
2107 | /* For DMA requests, RQM should be cleared during execution phase, so | |
2108 | * we would have errored out above. */ | |
2109 | assert(fdctrl->msr & FD_MSR_NONDMA); | |
d275b33d | 2110 | |
8977f3c1 | 2111 | /* FIFO data write */ |
b3bc1540 | 2112 | if (pos == FD_SECTOR_LEN - 1 || |
baca51fa | 2113 | fdctrl->data_pos == fdctrl->data_len) { |
77370520 | 2114 | cur_drv = get_cur_drv(fdctrl); |
4be74634 MA |
2115 | if (blk_write(cur_drv->blk, fd_sector(cur_drv), fdctrl->fifo, 1) |
2116 | < 0) { | |
cced7a13 BS |
2117 | FLOPPY_DPRINTF("error writing sector %d\n", |
2118 | fd_sector(cur_drv)); | |
5b0a25e8 | 2119 | break; |
77370520 | 2120 | } |
746d6de7 BS |
2121 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { |
2122 | FLOPPY_DPRINTF("error seeking to next sector %d\n", | |
2123 | fd_sector(cur_drv)); | |
5b0a25e8 | 2124 | break; |
746d6de7 | 2125 | } |
8977f3c1 | 2126 | } |
d275b33d KW |
2127 | |
2128 | /* Switch to result phase when done with the transfer */ | |
2129 | if (fdctrl->data_pos == fdctrl->data_len) { | |
c5139bd9 | 2130 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
d275b33d | 2131 | } |
5b0a25e8 | 2132 | break; |
678803ab | 2133 | |
5b0a25e8 KW |
2134 | case FD_PHASE_COMMAND: |
2135 | assert(!(fdctrl->msr & FD_MSR_NONDMA)); | |
d275b33d | 2136 | assert(fdctrl->data_pos < FD_SECTOR_LEN); |
5b0a25e8 | 2137 | |
d275b33d KW |
2138 | if (pos == 0) { |
2139 | /* The first byte specifies the command. Now we start reading | |
2140 | * as many parameters as this command requires. */ | |
2141 | cmd = get_command(value); | |
2142 | fdctrl->data_len = cmd->parameters + 1; | |
6cc8a11c KW |
2143 | if (cmd->parameters) { |
2144 | fdctrl->msr |= FD_MSR_RQM; | |
2145 | } | |
5b0a25e8 | 2146 | fdctrl->msr |= FD_MSR_CMDBUSY; |
8977f3c1 | 2147 | } |
65cef780 | 2148 | |
5b0a25e8 | 2149 | if (fdctrl->data_pos == fdctrl->data_len) { |
d275b33d | 2150 | /* We have all parameters now, execute the command */ |
5b0a25e8 | 2151 | fdctrl->phase = FD_PHASE_EXECUTION; |
d275b33d | 2152 | |
5b0a25e8 KW |
2153 | if (fdctrl->data_state & FD_STATE_FORMAT) { |
2154 | fdctrl_format_sector(fdctrl); | |
2155 | break; | |
2156 | } | |
2157 | ||
d275b33d KW |
2158 | cmd = get_command(fdctrl->fifo[0]); |
2159 | FLOPPY_DPRINTF("Calling handler for '%s'\n", cmd->name); | |
2160 | cmd->handler(fdctrl, cmd->direction); | |
5b0a25e8 KW |
2161 | } |
2162 | break; | |
2163 | ||
2164 | case FD_PHASE_RESULT: | |
2165 | default: | |
2166 | abort(); | |
8977f3c1 FB |
2167 | } |
2168 | } | |
ed5fd2cc FB |
2169 | |
2170 | static void fdctrl_result_timer(void *opaque) | |
2171 | { | |
5c02c033 BS |
2172 | FDCtrl *fdctrl = opaque; |
2173 | FDrive *cur_drv = get_cur_drv(fdctrl); | |
4f431960 | 2174 | |
b7ffa3b1 TS |
2175 | /* Pretend we are spinning. |
2176 | * This is needed for Coherent, which uses READ ID to check for | |
2177 | * sector interleaving. | |
2178 | */ | |
2179 | if (cur_drv->last_sect != 0) { | |
2180 | cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1; | |
2181 | } | |
844f65d6 HP |
2182 | /* READ_ID can't automatically succeed! */ |
2183 | if (fdctrl->check_media_rate && | |
2184 | (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { | |
2185 | FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n", | |
2186 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); | |
2187 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); | |
2188 | } else { | |
2189 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); | |
2190 | } | |
ed5fd2cc | 2191 | } |
678803ab | 2192 | |
7d4b4ba5 | 2193 | static void fdctrl_change_cb(void *opaque, bool load) |
8e49ca46 MA |
2194 | { |
2195 | FDrive *drive = opaque; | |
2196 | ||
2197 | drive->media_changed = 1; | |
21fcf360 | 2198 | fd_revalidate(drive); |
8e49ca46 MA |
2199 | } |
2200 | ||
2201 | static const BlockDevOps fdctrl_block_ops = { | |
2202 | .change_media_cb = fdctrl_change_cb, | |
2203 | }; | |
2204 | ||
678803ab | 2205 | /* Init functions */ |
a3ef7a61 | 2206 | static void fdctrl_connect_drives(FDCtrl *fdctrl, Error **errp) |
678803ab | 2207 | { |
12a71a02 | 2208 | unsigned int i; |
7d0d6950 | 2209 | FDrive *drive; |
678803ab | 2210 | |
678803ab | 2211 | for (i = 0; i < MAX_FD; i++) { |
7d0d6950 | 2212 | drive = &fdctrl->drives[i]; |
844f65d6 | 2213 | drive->fdctrl = fdctrl; |
7d0d6950 | 2214 | |
4be74634 MA |
2215 | if (drive->blk) { |
2216 | if (blk_get_on_error(drive->blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC) { | |
a3ef7a61 AF |
2217 | error_setg(errp, "fdc doesn't support drive option werror"); |
2218 | return; | |
b47b3525 | 2219 | } |
4be74634 | 2220 | if (blk_get_on_error(drive->blk, 1) != BLOCKDEV_ON_ERROR_REPORT) { |
a3ef7a61 AF |
2221 | error_setg(errp, "fdc doesn't support drive option rerror"); |
2222 | return; | |
b47b3525 MA |
2223 | } |
2224 | } | |
2225 | ||
7d0d6950 | 2226 | fd_init(drive); |
cfb08fba | 2227 | fdctrl_change_cb(drive, 0); |
4be74634 MA |
2228 | if (drive->blk) { |
2229 | blk_set_dev_ops(drive->blk, &fdctrl_block_ops, drive); | |
7d0d6950 | 2230 | } |
678803ab | 2231 | } |
678803ab BS |
2232 | } |
2233 | ||
dfc65f1f MA |
2234 | ISADevice *fdctrl_init_isa(ISABus *bus, DriveInfo **fds) |
2235 | { | |
4a17cc4f AF |
2236 | DeviceState *dev; |
2237 | ISADevice *isadev; | |
dfc65f1f | 2238 | |
4a17cc4f AF |
2239 | isadev = isa_try_create(bus, TYPE_ISA_FDC); |
2240 | if (!isadev) { | |
dfc65f1f MA |
2241 | return NULL; |
2242 | } | |
4a17cc4f | 2243 | dev = DEVICE(isadev); |
dfc65f1f MA |
2244 | |
2245 | if (fds[0]) { | |
4be74634 | 2246 | qdev_prop_set_drive_nofail(dev, "driveA", blk_by_legacy_dinfo(fds[0])); |
dfc65f1f MA |
2247 | } |
2248 | if (fds[1]) { | |
4be74634 | 2249 | qdev_prop_set_drive_nofail(dev, "driveB", blk_by_legacy_dinfo(fds[1])); |
dfc65f1f | 2250 | } |
4a17cc4f | 2251 | qdev_init_nofail(dev); |
dfc65f1f | 2252 | |
4a17cc4f | 2253 | return isadev; |
dfc65f1f MA |
2254 | } |
2255 | ||
63ffb564 | 2256 | void fdctrl_init_sysbus(qemu_irq irq, int dma_chann, |
a8170e5e | 2257 | hwaddr mmio_base, DriveInfo **fds) |
2091ba23 | 2258 | { |
5c02c033 | 2259 | FDCtrl *fdctrl; |
2091ba23 | 2260 | DeviceState *dev; |
dd3be742 | 2261 | SysBusDevice *sbd; |
5c02c033 | 2262 | FDCtrlSysBus *sys; |
2091ba23 | 2263 | |
19d46d71 | 2264 | dev = qdev_create(NULL, "sysbus-fdc"); |
dd3be742 | 2265 | sys = SYSBUS_FDC(dev); |
99244fa1 GH |
2266 | fdctrl = &sys->state; |
2267 | fdctrl->dma_chann = dma_chann; /* FIXME */ | |
995bf0ca | 2268 | if (fds[0]) { |
4be74634 | 2269 | qdev_prop_set_drive_nofail(dev, "driveA", blk_by_legacy_dinfo(fds[0])); |
995bf0ca GH |
2270 | } |
2271 | if (fds[1]) { | |
4be74634 | 2272 | qdev_prop_set_drive_nofail(dev, "driveB", blk_by_legacy_dinfo(fds[1])); |
995bf0ca | 2273 | } |
e23a1b33 | 2274 | qdev_init_nofail(dev); |
dd3be742 HT |
2275 | sbd = SYS_BUS_DEVICE(dev); |
2276 | sysbus_connect_irq(sbd, 0, irq); | |
2277 | sysbus_mmio_map(sbd, 0, mmio_base); | |
678803ab BS |
2278 | } |
2279 | ||
a8170e5e | 2280 | void sun4m_fdctrl_init(qemu_irq irq, hwaddr io_base, |
63ffb564 | 2281 | DriveInfo **fds, qemu_irq *fdc_tc) |
678803ab | 2282 | { |
f64ab228 | 2283 | DeviceState *dev; |
5c02c033 | 2284 | FDCtrlSysBus *sys; |
678803ab | 2285 | |
12a71a02 | 2286 | dev = qdev_create(NULL, "SUNW,fdtwo"); |
995bf0ca | 2287 | if (fds[0]) { |
4be74634 | 2288 | qdev_prop_set_drive_nofail(dev, "drive", blk_by_legacy_dinfo(fds[0])); |
995bf0ca | 2289 | } |
e23a1b33 | 2290 | qdev_init_nofail(dev); |
dd3be742 HT |
2291 | sys = SYSBUS_FDC(dev); |
2292 | sysbus_connect_irq(SYS_BUS_DEVICE(sys), 0, irq); | |
2293 | sysbus_mmio_map(SYS_BUS_DEVICE(sys), 0, io_base); | |
f64ab228 | 2294 | *fdc_tc = qdev_get_gpio_in(dev, 0); |
678803ab | 2295 | } |
f64ab228 | 2296 | |
a3ef7a61 | 2297 | static void fdctrl_realize_common(FDCtrl *fdctrl, Error **errp) |
f64ab228 | 2298 | { |
12a71a02 BS |
2299 | int i, j; |
2300 | static int command_tables_inited = 0; | |
f64ab228 | 2301 | |
12a71a02 BS |
2302 | /* Fill 'command_to_handler' lookup table */ |
2303 | if (!command_tables_inited) { | |
2304 | command_tables_inited = 1; | |
2305 | for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) { | |
2306 | for (j = 0; j < sizeof(command_to_handler); j++) { | |
2307 | if ((j & handlers[i].mask) == handlers[i].value) { | |
2308 | command_to_handler[j] = i; | |
2309 | } | |
2310 | } | |
2311 | } | |
2312 | } | |
2313 | ||
2314 | FLOPPY_DPRINTF("init controller\n"); | |
2315 | fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN); | |
d7a6c270 | 2316 | fdctrl->fifo_size = 512; |
bc72ad67 | 2317 | fdctrl->result_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
a3ef7a61 | 2318 | fdctrl_result_timer, fdctrl); |
12a71a02 BS |
2319 | |
2320 | fdctrl->version = 0x90; /* Intel 82078 controller */ | |
2321 | fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */ | |
d7a6c270 | 2322 | fdctrl->num_floppies = MAX_FD; |
12a71a02 | 2323 | |
a3ef7a61 | 2324 | if (fdctrl->dma_chann != -1) { |
99244fa1 | 2325 | DMA_register_channel(fdctrl->dma_chann, &fdctrl_transfer_handler, fdctrl); |
a3ef7a61 AF |
2326 | } |
2327 | fdctrl_connect_drives(fdctrl, errp); | |
f64ab228 BS |
2328 | } |
2329 | ||
212ec7ba | 2330 | static const MemoryRegionPortio fdc_portio_list[] = { |
2f290a8c | 2331 | { 1, 5, 1, .read = fdctrl_read, .write = fdctrl_write }, |
212ec7ba RH |
2332 | { 7, 1, 1, .read = fdctrl_read, .write = fdctrl_write }, |
2333 | PORTIO_END_OF_LIST(), | |
2f290a8c RH |
2334 | }; |
2335 | ||
db895a1e | 2336 | static void isabus_fdc_realize(DeviceState *dev, Error **errp) |
8baf73ad | 2337 | { |
db895a1e | 2338 | ISADevice *isadev = ISA_DEVICE(dev); |
020c8e76 | 2339 | FDCtrlISABus *isa = ISA_FDC(dev); |
5c02c033 | 2340 | FDCtrl *fdctrl = &isa->state; |
a3ef7a61 | 2341 | Error *err = NULL; |
8baf73ad | 2342 | |
db895a1e AF |
2343 | isa_register_portio_list(isadev, isa->iobase, fdc_portio_list, fdctrl, |
2344 | "fdc"); | |
dee41d58 | 2345 | |
db895a1e | 2346 | isa_init_irq(isadev, &fdctrl->irq, isa->irq); |
c9ae703d | 2347 | fdctrl->dma_chann = isa->dma; |
8baf73ad | 2348 | |
db895a1e | 2349 | qdev_set_legacy_instance_id(dev, isa->iobase, 2); |
a3ef7a61 AF |
2350 | fdctrl_realize_common(fdctrl, &err); |
2351 | if (err != NULL) { | |
2352 | error_propagate(errp, err); | |
db895a1e AF |
2353 | return; |
2354 | } | |
8baf73ad GH |
2355 | } |
2356 | ||
940194c2 | 2357 | static void sysbus_fdc_initfn(Object *obj) |
12a71a02 | 2358 | { |
19d46d71 | 2359 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
940194c2 | 2360 | FDCtrlSysBus *sys = SYSBUS_FDC(obj); |
5c02c033 | 2361 | FDCtrl *fdctrl = &sys->state; |
12a71a02 | 2362 | |
19d46d71 AF |
2363 | fdctrl->dma_chann = -1; |
2364 | ||
940194c2 | 2365 | memory_region_init_io(&fdctrl->iomem, obj, &fdctrl_mem_ops, fdctrl, |
2d256e6f | 2366 | "fdc", 0x08); |
19d46d71 | 2367 | sysbus_init_mmio(sbd, &fdctrl->iomem); |
940194c2 HT |
2368 | } |
2369 | ||
19d46d71 | 2370 | static void sun4m_fdc_initfn(Object *obj) |
940194c2 | 2371 | { |
19d46d71 AF |
2372 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
2373 | FDCtrlSysBus *sys = SYSBUS_FDC(obj); | |
940194c2 | 2374 | FDCtrl *fdctrl = &sys->state; |
940194c2 | 2375 | |
19d46d71 AF |
2376 | memory_region_init_io(&fdctrl->iomem, obj, &fdctrl_mem_strict_ops, |
2377 | fdctrl, "fdctrl", 0x08); | |
2378 | sysbus_init_mmio(sbd, &fdctrl->iomem); | |
940194c2 | 2379 | } |
2be37833 | 2380 | |
19d46d71 | 2381 | static void sysbus_fdc_common_initfn(Object *obj) |
940194c2 | 2382 | { |
19d46d71 AF |
2383 | DeviceState *dev = DEVICE(obj); |
2384 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); | |
940194c2 HT |
2385 | FDCtrlSysBus *sys = SYSBUS_FDC(obj); |
2386 | FDCtrl *fdctrl = &sys->state; | |
2387 | ||
19d46d71 AF |
2388 | qdev_set_legacy_instance_id(dev, 0 /* io */, 2); /* FIXME */ |
2389 | ||
2390 | sysbus_init_irq(sbd, &fdctrl->irq); | |
2391 | qdev_init_gpio_in(dev, fdctrl_handle_tc, 1); | |
12a71a02 BS |
2392 | } |
2393 | ||
19d46d71 | 2394 | static void sysbus_fdc_common_realize(DeviceState *dev, Error **errp) |
12a71a02 | 2395 | { |
dd3be742 HT |
2396 | FDCtrlSysBus *sys = SYSBUS_FDC(dev); |
2397 | FDCtrl *fdctrl = &sys->state; | |
12a71a02 | 2398 | |
19d46d71 | 2399 | fdctrl_realize_common(fdctrl, errp); |
12a71a02 | 2400 | } |
f64ab228 | 2401 | |
61a8d649 | 2402 | FDriveType isa_fdc_get_drive_type(ISADevice *fdc, int i) |
34d4260e | 2403 | { |
020c8e76 | 2404 | FDCtrlISABus *isa = ISA_FDC(fdc); |
34d4260e | 2405 | |
61a8d649 | 2406 | return isa->state.drives[i].drive; |
34d4260e KW |
2407 | } |
2408 | ||
a64405d1 JK |
2409 | static const VMStateDescription vmstate_isa_fdc ={ |
2410 | .name = "fdc", | |
2411 | .version_id = 2, | |
2412 | .minimum_version_id = 2, | |
d49805ae | 2413 | .fields = (VMStateField[]) { |
a64405d1 JK |
2414 | VMSTATE_STRUCT(state, FDCtrlISABus, 0, vmstate_fdc, FDCtrl), |
2415 | VMSTATE_END_OF_LIST() | |
2416 | } | |
2417 | }; | |
2418 | ||
39bffca2 | 2419 | static Property isa_fdc_properties[] = { |
c7bcc85d | 2420 | DEFINE_PROP_UINT32("iobase", FDCtrlISABus, iobase, 0x3f0), |
c9ae703d HP |
2421 | DEFINE_PROP_UINT32("irq", FDCtrlISABus, irq, 6), |
2422 | DEFINE_PROP_UINT32("dma", FDCtrlISABus, dma, 2), | |
4be74634 MA |
2423 | DEFINE_PROP_DRIVE("driveA", FDCtrlISABus, state.drives[0].blk), |
2424 | DEFINE_PROP_DRIVE("driveB", FDCtrlISABus, state.drives[1].blk), | |
09c6d585 HP |
2425 | DEFINE_PROP_BIT("check_media_rate", FDCtrlISABus, state.check_media_rate, |
2426 | 0, true), | |
39bffca2 AL |
2427 | DEFINE_PROP_END_OF_LIST(), |
2428 | }; | |
2429 | ||
020c8e76 | 2430 | static void isabus_fdc_class_init(ObjectClass *klass, void *data) |
8f04ee08 | 2431 | { |
39bffca2 | 2432 | DeviceClass *dc = DEVICE_CLASS(klass); |
db895a1e AF |
2433 | |
2434 | dc->realize = isabus_fdc_realize; | |
39bffca2 | 2435 | dc->fw_name = "fdc"; |
39bffca2 AL |
2436 | dc->reset = fdctrl_external_reset_isa; |
2437 | dc->vmsd = &vmstate_isa_fdc; | |
2438 | dc->props = isa_fdc_properties; | |
125ee0ed | 2439 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
39bffca2 AL |
2440 | } |
2441 | ||
81782b6a GA |
2442 | static void isabus_fdc_instance_init(Object *obj) |
2443 | { | |
2444 | FDCtrlISABus *isa = ISA_FDC(obj); | |
2445 | ||
2446 | device_add_bootindex_property(obj, &isa->bootindexA, | |
2447 | "bootindexA", "/floppy@0", | |
2448 | DEVICE(obj), NULL); | |
2449 | device_add_bootindex_property(obj, &isa->bootindexB, | |
2450 | "bootindexB", "/floppy@1", | |
2451 | DEVICE(obj), NULL); | |
2452 | } | |
2453 | ||
8c43a6f0 | 2454 | static const TypeInfo isa_fdc_info = { |
020c8e76 | 2455 | .name = TYPE_ISA_FDC, |
39bffca2 AL |
2456 | .parent = TYPE_ISA_DEVICE, |
2457 | .instance_size = sizeof(FDCtrlISABus), | |
020c8e76 | 2458 | .class_init = isabus_fdc_class_init, |
81782b6a | 2459 | .instance_init = isabus_fdc_instance_init, |
8baf73ad GH |
2460 | }; |
2461 | ||
a64405d1 JK |
2462 | static const VMStateDescription vmstate_sysbus_fdc ={ |
2463 | .name = "fdc", | |
2464 | .version_id = 2, | |
2465 | .minimum_version_id = 2, | |
d49805ae | 2466 | .fields = (VMStateField[]) { |
a64405d1 JK |
2467 | VMSTATE_STRUCT(state, FDCtrlSysBus, 0, vmstate_fdc, FDCtrl), |
2468 | VMSTATE_END_OF_LIST() | |
2469 | } | |
2470 | }; | |
2471 | ||
999e12bb | 2472 | static Property sysbus_fdc_properties[] = { |
4be74634 MA |
2473 | DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus, state.drives[0].blk), |
2474 | DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus, state.drives[1].blk), | |
999e12bb | 2475 | DEFINE_PROP_END_OF_LIST(), |
12a71a02 BS |
2476 | }; |
2477 | ||
999e12bb AL |
2478 | static void sysbus_fdc_class_init(ObjectClass *klass, void *data) |
2479 | { | |
39bffca2 | 2480 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 2481 | |
39bffca2 | 2482 | dc->props = sysbus_fdc_properties; |
125ee0ed | 2483 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
999e12bb AL |
2484 | } |
2485 | ||
8c43a6f0 | 2486 | static const TypeInfo sysbus_fdc_info = { |
19d46d71 AF |
2487 | .name = "sysbus-fdc", |
2488 | .parent = TYPE_SYSBUS_FDC, | |
940194c2 | 2489 | .instance_init = sysbus_fdc_initfn, |
39bffca2 | 2490 | .class_init = sysbus_fdc_class_init, |
999e12bb AL |
2491 | }; |
2492 | ||
2493 | static Property sun4m_fdc_properties[] = { | |
4be74634 | 2494 | DEFINE_PROP_DRIVE("drive", FDCtrlSysBus, state.drives[0].blk), |
999e12bb AL |
2495 | DEFINE_PROP_END_OF_LIST(), |
2496 | }; | |
2497 | ||
2498 | static void sun4m_fdc_class_init(ObjectClass *klass, void *data) | |
2499 | { | |
39bffca2 | 2500 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 2501 | |
39bffca2 | 2502 | dc->props = sun4m_fdc_properties; |
125ee0ed | 2503 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
999e12bb AL |
2504 | } |
2505 | ||
8c43a6f0 | 2506 | static const TypeInfo sun4m_fdc_info = { |
39bffca2 | 2507 | .name = "SUNW,fdtwo", |
19d46d71 | 2508 | .parent = TYPE_SYSBUS_FDC, |
940194c2 | 2509 | .instance_init = sun4m_fdc_initfn, |
39bffca2 | 2510 | .class_init = sun4m_fdc_class_init, |
f64ab228 BS |
2511 | }; |
2512 | ||
19d46d71 AF |
2513 | static void sysbus_fdc_common_class_init(ObjectClass *klass, void *data) |
2514 | { | |
2515 | DeviceClass *dc = DEVICE_CLASS(klass); | |
2516 | ||
2517 | dc->realize = sysbus_fdc_common_realize; | |
2518 | dc->reset = fdctrl_external_reset_sysbus; | |
2519 | dc->vmsd = &vmstate_sysbus_fdc; | |
2520 | } | |
2521 | ||
2522 | static const TypeInfo sysbus_fdc_type_info = { | |
2523 | .name = TYPE_SYSBUS_FDC, | |
2524 | .parent = TYPE_SYS_BUS_DEVICE, | |
2525 | .instance_size = sizeof(FDCtrlSysBus), | |
2526 | .instance_init = sysbus_fdc_common_initfn, | |
2527 | .abstract = true, | |
2528 | .class_init = sysbus_fdc_common_class_init, | |
2529 | }; | |
2530 | ||
83f7d43a | 2531 | static void fdc_register_types(void) |
f64ab228 | 2532 | { |
39bffca2 | 2533 | type_register_static(&isa_fdc_info); |
19d46d71 | 2534 | type_register_static(&sysbus_fdc_type_info); |
39bffca2 AL |
2535 | type_register_static(&sysbus_fdc_info); |
2536 | type_register_static(&sun4m_fdc_info); | |
f64ab228 BS |
2537 | } |
2538 | ||
83f7d43a | 2539 | type_init(fdc_register_types) |