]>
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 | ||
8977f3c1 | 498 | #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI) |
baca51fa | 499 | #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT) |
8977f3c1 | 500 | |
5c02c033 | 501 | struct FDCtrl { |
dc6c1b37 | 502 | MemoryRegion iomem; |
d537cf6c | 503 | qemu_irq irq; |
4b19ec0c | 504 | /* Controller state */ |
ed5fd2cc | 505 | QEMUTimer *result_timer; |
242cca4f BS |
506 | int dma_chann; |
507 | /* Controller's identification */ | |
508 | uint8_t version; | |
509 | /* HW */ | |
8c6a4d77 BS |
510 | uint8_t sra; |
511 | uint8_t srb; | |
368df94d | 512 | uint8_t dor; |
d7a6c270 | 513 | uint8_t dor_vmstate; /* only used as temp during vmstate */ |
46d3233b | 514 | uint8_t tdr; |
b9b3d225 | 515 | uint8_t dsr; |
368df94d | 516 | uint8_t msr; |
8977f3c1 | 517 | uint8_t cur_drv; |
77370520 BS |
518 | uint8_t status0; |
519 | uint8_t status1; | |
520 | uint8_t status2; | |
8977f3c1 | 521 | /* Command FIFO */ |
33f00271 | 522 | uint8_t *fifo; |
d7a6c270 | 523 | int32_t fifo_size; |
8977f3c1 FB |
524 | uint32_t data_pos; |
525 | uint32_t data_len; | |
526 | uint8_t data_state; | |
527 | uint8_t data_dir; | |
890fa6be | 528 | uint8_t eot; /* last wanted sector */ |
8977f3c1 | 529 | /* States kept only to be returned back */ |
8977f3c1 FB |
530 | /* precompensation */ |
531 | uint8_t precomp_trk; | |
532 | uint8_t config; | |
533 | uint8_t lock; | |
534 | /* Power down config (also with status regB access mode */ | |
535 | uint8_t pwrd; | |
536 | /* Floppy drives */ | |
d7a6c270 | 537 | uint8_t num_floppies; |
5c02c033 | 538 | FDrive drives[MAX_FD]; |
f2d81b33 | 539 | int reset_sensei; |
09c6d585 | 540 | uint32_t check_media_rate; |
242cca4f BS |
541 | /* Timers state */ |
542 | uint8_t timer0; | |
543 | uint8_t timer1; | |
baca51fa FB |
544 | }; |
545 | ||
19d46d71 | 546 | #define TYPE_SYSBUS_FDC "base-sysbus-fdc" |
dd3be742 HT |
547 | #define SYSBUS_FDC(obj) OBJECT_CHECK(FDCtrlSysBus, (obj), TYPE_SYSBUS_FDC) |
548 | ||
5c02c033 | 549 | typedef struct FDCtrlSysBus { |
dd3be742 HT |
550 | /*< private >*/ |
551 | SysBusDevice parent_obj; | |
552 | /*< public >*/ | |
553 | ||
5c02c033 BS |
554 | struct FDCtrl state; |
555 | } FDCtrlSysBus; | |
8baf73ad | 556 | |
020c8e76 AF |
557 | #define ISA_FDC(obj) OBJECT_CHECK(FDCtrlISABus, (obj), TYPE_ISA_FDC) |
558 | ||
5c02c033 | 559 | typedef struct FDCtrlISABus { |
020c8e76 AF |
560 | ISADevice parent_obj; |
561 | ||
c9ae703d HP |
562 | uint32_t iobase; |
563 | uint32_t irq; | |
564 | uint32_t dma; | |
5c02c033 | 565 | struct FDCtrl state; |
1ca4d09a GN |
566 | int32_t bootindexA; |
567 | int32_t bootindexB; | |
5c02c033 | 568 | } FDCtrlISABus; |
8baf73ad | 569 | |
baca51fa FB |
570 | static uint32_t fdctrl_read (void *opaque, uint32_t reg) |
571 | { | |
5c02c033 | 572 | FDCtrl *fdctrl = opaque; |
baca51fa FB |
573 | uint32_t retval; |
574 | ||
a18e67f5 | 575 | reg &= 7; |
e64d7d59 | 576 | switch (reg) { |
8c6a4d77 BS |
577 | case FD_REG_SRA: |
578 | retval = fdctrl_read_statusA(fdctrl); | |
4f431960 | 579 | break; |
8c6a4d77 | 580 | case FD_REG_SRB: |
4f431960 JM |
581 | retval = fdctrl_read_statusB(fdctrl); |
582 | break; | |
9fea808a | 583 | case FD_REG_DOR: |
4f431960 JM |
584 | retval = fdctrl_read_dor(fdctrl); |
585 | break; | |
9fea808a | 586 | case FD_REG_TDR: |
baca51fa | 587 | retval = fdctrl_read_tape(fdctrl); |
4f431960 | 588 | break; |
9fea808a | 589 | case FD_REG_MSR: |
baca51fa | 590 | retval = fdctrl_read_main_status(fdctrl); |
4f431960 | 591 | break; |
9fea808a | 592 | case FD_REG_FIFO: |
baca51fa | 593 | retval = fdctrl_read_data(fdctrl); |
4f431960 | 594 | break; |
9fea808a | 595 | case FD_REG_DIR: |
baca51fa | 596 | retval = fdctrl_read_dir(fdctrl); |
4f431960 | 597 | break; |
a541f297 | 598 | default: |
4f431960 JM |
599 | retval = (uint32_t)(-1); |
600 | break; | |
a541f297 | 601 | } |
ed5fd2cc | 602 | FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval); |
baca51fa FB |
603 | |
604 | return retval; | |
605 | } | |
606 | ||
607 | static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value) | |
608 | { | |
5c02c033 | 609 | FDCtrl *fdctrl = opaque; |
baca51fa | 610 | |
ed5fd2cc FB |
611 | FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg & 7, value); |
612 | ||
a18e67f5 | 613 | reg &= 7; |
e64d7d59 | 614 | switch (reg) { |
9fea808a | 615 | case FD_REG_DOR: |
4f431960 JM |
616 | fdctrl_write_dor(fdctrl, value); |
617 | break; | |
9fea808a | 618 | case FD_REG_TDR: |
baca51fa | 619 | fdctrl_write_tape(fdctrl, value); |
4f431960 | 620 | break; |
9fea808a | 621 | case FD_REG_DSR: |
baca51fa | 622 | fdctrl_write_rate(fdctrl, value); |
4f431960 | 623 | break; |
9fea808a | 624 | case FD_REG_FIFO: |
baca51fa | 625 | fdctrl_write_data(fdctrl, value); |
4f431960 | 626 | break; |
a758f8f4 HP |
627 | case FD_REG_CCR: |
628 | fdctrl_write_ccr(fdctrl, value); | |
629 | break; | |
a541f297 | 630 | default: |
4f431960 | 631 | break; |
a541f297 | 632 | } |
baca51fa FB |
633 | } |
634 | ||
a8170e5e | 635 | static uint64_t fdctrl_read_mem (void *opaque, hwaddr reg, |
dc6c1b37 | 636 | unsigned ize) |
62a46c61 | 637 | { |
5dcb6b91 | 638 | return fdctrl_read(opaque, (uint32_t)reg); |
62a46c61 FB |
639 | } |
640 | ||
a8170e5e | 641 | static void fdctrl_write_mem (void *opaque, hwaddr reg, |
dc6c1b37 | 642 | uint64_t value, unsigned size) |
62a46c61 | 643 | { |
5dcb6b91 | 644 | fdctrl_write(opaque, (uint32_t)reg, value); |
62a46c61 FB |
645 | } |
646 | ||
dc6c1b37 AK |
647 | static const MemoryRegionOps fdctrl_mem_ops = { |
648 | .read = fdctrl_read_mem, | |
649 | .write = fdctrl_write_mem, | |
650 | .endianness = DEVICE_NATIVE_ENDIAN, | |
e80cfcfc FB |
651 | }; |
652 | ||
dc6c1b37 AK |
653 | static const MemoryRegionOps fdctrl_mem_strict_ops = { |
654 | .read = fdctrl_read_mem, | |
655 | .write = fdctrl_write_mem, | |
656 | .endianness = DEVICE_NATIVE_ENDIAN, | |
657 | .valid = { | |
658 | .min_access_size = 1, | |
659 | .max_access_size = 1, | |
660 | }, | |
7c560456 BS |
661 | }; |
662 | ||
7d905f71 JW |
663 | static bool fdrive_media_changed_needed(void *opaque) |
664 | { | |
665 | FDrive *drive = opaque; | |
666 | ||
4be74634 | 667 | return (drive->blk != NULL && drive->media_changed != 1); |
7d905f71 JW |
668 | } |
669 | ||
670 | static const VMStateDescription vmstate_fdrive_media_changed = { | |
671 | .name = "fdrive/media_changed", | |
672 | .version_id = 1, | |
673 | .minimum_version_id = 1, | |
d49805ae | 674 | .fields = (VMStateField[]) { |
7d905f71 JW |
675 | VMSTATE_UINT8(media_changed, FDrive), |
676 | VMSTATE_END_OF_LIST() | |
677 | } | |
678 | }; | |
679 | ||
844f65d6 HP |
680 | static bool fdrive_media_rate_needed(void *opaque) |
681 | { | |
682 | FDrive *drive = opaque; | |
683 | ||
684 | return drive->fdctrl->check_media_rate; | |
685 | } | |
686 | ||
687 | static const VMStateDescription vmstate_fdrive_media_rate = { | |
688 | .name = "fdrive/media_rate", | |
689 | .version_id = 1, | |
690 | .minimum_version_id = 1, | |
d49805ae | 691 | .fields = (VMStateField[]) { |
844f65d6 HP |
692 | VMSTATE_UINT8(media_rate, FDrive), |
693 | VMSTATE_END_OF_LIST() | |
694 | } | |
695 | }; | |
696 | ||
c0b92f30 PD |
697 | static bool fdrive_perpendicular_needed(void *opaque) |
698 | { | |
699 | FDrive *drive = opaque; | |
700 | ||
701 | return drive->perpendicular != 0; | |
702 | } | |
703 | ||
704 | static const VMStateDescription vmstate_fdrive_perpendicular = { | |
705 | .name = "fdrive/perpendicular", | |
706 | .version_id = 1, | |
707 | .minimum_version_id = 1, | |
708 | .fields = (VMStateField[]) { | |
709 | VMSTATE_UINT8(perpendicular, FDrive), | |
710 | VMSTATE_END_OF_LIST() | |
711 | } | |
712 | }; | |
713 | ||
714 | static int fdrive_post_load(void *opaque, int version_id) | |
715 | { | |
716 | fd_revalidate(opaque); | |
717 | return 0; | |
718 | } | |
719 | ||
d7a6c270 JQ |
720 | static const VMStateDescription vmstate_fdrive = { |
721 | .name = "fdrive", | |
722 | .version_id = 1, | |
723 | .minimum_version_id = 1, | |
c0b92f30 | 724 | .post_load = fdrive_post_load, |
d49805ae | 725 | .fields = (VMStateField[]) { |
5c02c033 BS |
726 | VMSTATE_UINT8(head, FDrive), |
727 | VMSTATE_UINT8(track, FDrive), | |
728 | VMSTATE_UINT8(sect, FDrive), | |
d7a6c270 | 729 | VMSTATE_END_OF_LIST() |
7d905f71 JW |
730 | }, |
731 | .subsections = (VMStateSubsection[]) { | |
732 | { | |
733 | .vmsd = &vmstate_fdrive_media_changed, | |
734 | .needed = &fdrive_media_changed_needed, | |
844f65d6 HP |
735 | } , { |
736 | .vmsd = &vmstate_fdrive_media_rate, | |
737 | .needed = &fdrive_media_rate_needed, | |
c0b92f30 PD |
738 | } , { |
739 | .vmsd = &vmstate_fdrive_perpendicular, | |
740 | .needed = &fdrive_perpendicular_needed, | |
7d905f71 JW |
741 | } , { |
742 | /* empty */ | |
743 | } | |
d7a6c270 JQ |
744 | } |
745 | }; | |
3ccacc4a | 746 | |
d4bfa4d7 | 747 | static void fdc_pre_save(void *opaque) |
3ccacc4a | 748 | { |
5c02c033 | 749 | FDCtrl *s = opaque; |
3ccacc4a | 750 | |
d7a6c270 | 751 | s->dor_vmstate = s->dor | GET_CUR_DRV(s); |
3ccacc4a BS |
752 | } |
753 | ||
e59fb374 | 754 | static int fdc_post_load(void *opaque, int version_id) |
3ccacc4a | 755 | { |
5c02c033 | 756 | FDCtrl *s = opaque; |
3ccacc4a | 757 | |
d7a6c270 JQ |
758 | SET_CUR_DRV(s, s->dor_vmstate & FD_DOR_SELMASK); |
759 | s->dor = s->dor_vmstate & ~FD_DOR_SELMASK; | |
3ccacc4a BS |
760 | return 0; |
761 | } | |
762 | ||
c0b92f30 PD |
763 | static bool fdc_reset_sensei_needed(void *opaque) |
764 | { | |
765 | FDCtrl *s = opaque; | |
766 | ||
767 | return s->reset_sensei != 0; | |
768 | } | |
769 | ||
770 | static const VMStateDescription vmstate_fdc_reset_sensei = { | |
771 | .name = "fdc/reset_sensei", | |
772 | .version_id = 1, | |
773 | .minimum_version_id = 1, | |
774 | .fields = (VMStateField[]) { | |
775 | VMSTATE_INT32(reset_sensei, FDCtrl), | |
776 | VMSTATE_END_OF_LIST() | |
777 | } | |
778 | }; | |
779 | ||
780 | static bool fdc_result_timer_needed(void *opaque) | |
781 | { | |
782 | FDCtrl *s = opaque; | |
783 | ||
784 | return timer_pending(s->result_timer); | |
785 | } | |
786 | ||
787 | static const VMStateDescription vmstate_fdc_result_timer = { | |
788 | .name = "fdc/result_timer", | |
789 | .version_id = 1, | |
790 | .minimum_version_id = 1, | |
791 | .fields = (VMStateField[]) { | |
e720677e | 792 | VMSTATE_TIMER_PTR(result_timer, FDCtrl), |
c0b92f30 PD |
793 | VMSTATE_END_OF_LIST() |
794 | } | |
795 | }; | |
796 | ||
d7a6c270 | 797 | static const VMStateDescription vmstate_fdc = { |
aef30c3c | 798 | .name = "fdc", |
d7a6c270 JQ |
799 | .version_id = 2, |
800 | .minimum_version_id = 2, | |
d7a6c270 JQ |
801 | .pre_save = fdc_pre_save, |
802 | .post_load = fdc_post_load, | |
d49805ae | 803 | .fields = (VMStateField[]) { |
d7a6c270 | 804 | /* Controller State */ |
5c02c033 BS |
805 | VMSTATE_UINT8(sra, FDCtrl), |
806 | VMSTATE_UINT8(srb, FDCtrl), | |
807 | VMSTATE_UINT8(dor_vmstate, FDCtrl), | |
808 | VMSTATE_UINT8(tdr, FDCtrl), | |
809 | VMSTATE_UINT8(dsr, FDCtrl), | |
810 | VMSTATE_UINT8(msr, FDCtrl), | |
811 | VMSTATE_UINT8(status0, FDCtrl), | |
812 | VMSTATE_UINT8(status1, FDCtrl), | |
813 | VMSTATE_UINT8(status2, FDCtrl), | |
d7a6c270 | 814 | /* Command FIFO */ |
8ec68b06 BS |
815 | VMSTATE_VARRAY_INT32(fifo, FDCtrl, fifo_size, 0, vmstate_info_uint8, |
816 | uint8_t), | |
5c02c033 BS |
817 | VMSTATE_UINT32(data_pos, FDCtrl), |
818 | VMSTATE_UINT32(data_len, FDCtrl), | |
819 | VMSTATE_UINT8(data_state, FDCtrl), | |
820 | VMSTATE_UINT8(data_dir, FDCtrl), | |
821 | VMSTATE_UINT8(eot, FDCtrl), | |
d7a6c270 | 822 | /* States kept only to be returned back */ |
5c02c033 BS |
823 | VMSTATE_UINT8(timer0, FDCtrl), |
824 | VMSTATE_UINT8(timer1, FDCtrl), | |
825 | VMSTATE_UINT8(precomp_trk, FDCtrl), | |
826 | VMSTATE_UINT8(config, FDCtrl), | |
827 | VMSTATE_UINT8(lock, FDCtrl), | |
828 | VMSTATE_UINT8(pwrd, FDCtrl), | |
829 | VMSTATE_UINT8_EQUAL(num_floppies, FDCtrl), | |
830 | VMSTATE_STRUCT_ARRAY(drives, FDCtrl, MAX_FD, 1, | |
831 | vmstate_fdrive, FDrive), | |
d7a6c270 | 832 | VMSTATE_END_OF_LIST() |
c0b92f30 PD |
833 | }, |
834 | .subsections = (VMStateSubsection[]) { | |
835 | { | |
836 | .vmsd = &vmstate_fdc_reset_sensei, | |
837 | .needed = fdc_reset_sensei_needed, | |
838 | } , { | |
839 | .vmsd = &vmstate_fdc_result_timer, | |
840 | .needed = fdc_result_timer_needed, | |
841 | } , { | |
842 | /* empty */ | |
843 | } | |
78ae820c | 844 | } |
d7a6c270 | 845 | }; |
3ccacc4a | 846 | |
2be37833 | 847 | static void fdctrl_external_reset_sysbus(DeviceState *d) |
3ccacc4a | 848 | { |
dd3be742 | 849 | FDCtrlSysBus *sys = SYSBUS_FDC(d); |
5c02c033 | 850 | FDCtrl *s = &sys->state; |
2be37833 BS |
851 | |
852 | fdctrl_reset(s, 0); | |
853 | } | |
854 | ||
855 | static void fdctrl_external_reset_isa(DeviceState *d) | |
856 | { | |
020c8e76 | 857 | FDCtrlISABus *isa = ISA_FDC(d); |
5c02c033 | 858 | FDCtrl *s = &isa->state; |
3ccacc4a BS |
859 | |
860 | fdctrl_reset(s, 0); | |
861 | } | |
862 | ||
2be17ebd BS |
863 | static void fdctrl_handle_tc(void *opaque, int irq, int level) |
864 | { | |
5c02c033 | 865 | //FDCtrl *s = opaque; |
2be17ebd BS |
866 | |
867 | if (level) { | |
868 | // XXX | |
869 | FLOPPY_DPRINTF("TC pulsed\n"); | |
870 | } | |
871 | } | |
872 | ||
8977f3c1 | 873 | /* Change IRQ state */ |
5c02c033 | 874 | static void fdctrl_reset_irq(FDCtrl *fdctrl) |
8977f3c1 | 875 | { |
d497d534 | 876 | fdctrl->status0 = 0; |
8c6a4d77 BS |
877 | if (!(fdctrl->sra & FD_SRA_INTPEND)) |
878 | return; | |
ed5fd2cc | 879 | FLOPPY_DPRINTF("Reset interrupt\n"); |
d537cf6c | 880 | qemu_set_irq(fdctrl->irq, 0); |
8c6a4d77 | 881 | fdctrl->sra &= ~FD_SRA_INTPEND; |
8977f3c1 FB |
882 | } |
883 | ||
d497d534 | 884 | static void fdctrl_raise_irq(FDCtrl *fdctrl) |
8977f3c1 | 885 | { |
8c6a4d77 | 886 | if (!(fdctrl->sra & FD_SRA_INTPEND)) { |
d537cf6c | 887 | qemu_set_irq(fdctrl->irq, 1); |
8c6a4d77 | 888 | fdctrl->sra |= FD_SRA_INTPEND; |
8977f3c1 | 889 | } |
21fcf360 | 890 | |
f2d81b33 | 891 | fdctrl->reset_sensei = 0; |
77370520 | 892 | FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl->status0); |
8977f3c1 FB |
893 | } |
894 | ||
4b19ec0c | 895 | /* Reset controller */ |
5c02c033 | 896 | static void fdctrl_reset(FDCtrl *fdctrl, int do_irq) |
8977f3c1 FB |
897 | { |
898 | int i; | |
899 | ||
4b19ec0c | 900 | FLOPPY_DPRINTF("reset controller\n"); |
baca51fa | 901 | fdctrl_reset_irq(fdctrl); |
4b19ec0c | 902 | /* Initialise controller */ |
8c6a4d77 BS |
903 | fdctrl->sra = 0; |
904 | fdctrl->srb = 0xc0; | |
4be74634 | 905 | if (!fdctrl->drives[1].blk) { |
8c6a4d77 | 906 | fdctrl->sra |= FD_SRA_nDRV2; |
4be74634 | 907 | } |
baca51fa | 908 | fdctrl->cur_drv = 0; |
1c346df2 | 909 | fdctrl->dor = FD_DOR_nRESET; |
368df94d | 910 | fdctrl->dor |= (fdctrl->dma_chann != -1) ? FD_DOR_DMAEN : 0; |
b9b3d225 | 911 | fdctrl->msr = FD_MSR_RQM; |
c0b92f30 PD |
912 | fdctrl->reset_sensei = 0; |
913 | timer_del(fdctrl->result_timer); | |
8977f3c1 | 914 | /* FIFO state */ |
baca51fa FB |
915 | fdctrl->data_pos = 0; |
916 | fdctrl->data_len = 0; | |
b9b3d225 | 917 | fdctrl->data_state = 0; |
baca51fa | 918 | fdctrl->data_dir = FD_DIR_WRITE; |
8977f3c1 | 919 | for (i = 0; i < MAX_FD; i++) |
1c346df2 | 920 | fd_recalibrate(&fdctrl->drives[i]); |
07e415f2 | 921 | fdctrl_to_command_phase(fdctrl); |
77370520 | 922 | if (do_irq) { |
d497d534 HP |
923 | fdctrl->status0 |= FD_SR0_RDYCHG; |
924 | fdctrl_raise_irq(fdctrl); | |
f2d81b33 | 925 | fdctrl->reset_sensei = FD_RESET_SENSEI_COUNT; |
77370520 | 926 | } |
baca51fa FB |
927 | } |
928 | ||
5c02c033 | 929 | static inline FDrive *drv0(FDCtrl *fdctrl) |
baca51fa | 930 | { |
46d3233b | 931 | return &fdctrl->drives[(fdctrl->tdr & FD_TDR_BOOTSEL) >> 2]; |
baca51fa FB |
932 | } |
933 | ||
5c02c033 | 934 | static inline FDrive *drv1(FDCtrl *fdctrl) |
baca51fa | 935 | { |
46d3233b BS |
936 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (1 << 2)) |
937 | return &fdctrl->drives[1]; | |
938 | else | |
939 | return &fdctrl->drives[0]; | |
baca51fa FB |
940 | } |
941 | ||
78ae820c | 942 | #if MAX_FD == 4 |
5c02c033 | 943 | static inline FDrive *drv2(FDCtrl *fdctrl) |
78ae820c BS |
944 | { |
945 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (2 << 2)) | |
946 | return &fdctrl->drives[2]; | |
947 | else | |
948 | return &fdctrl->drives[1]; | |
949 | } | |
950 | ||
5c02c033 | 951 | static inline FDrive *drv3(FDCtrl *fdctrl) |
78ae820c BS |
952 | { |
953 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (3 << 2)) | |
954 | return &fdctrl->drives[3]; | |
955 | else | |
956 | return &fdctrl->drives[2]; | |
957 | } | |
958 | #endif | |
959 | ||
5c02c033 | 960 | static FDrive *get_cur_drv(FDCtrl *fdctrl) |
baca51fa | 961 | { |
78ae820c BS |
962 | switch (fdctrl->cur_drv) { |
963 | case 0: return drv0(fdctrl); | |
964 | case 1: return drv1(fdctrl); | |
965 | #if MAX_FD == 4 | |
966 | case 2: return drv2(fdctrl); | |
967 | case 3: return drv3(fdctrl); | |
968 | #endif | |
969 | default: return NULL; | |
970 | } | |
8977f3c1 FB |
971 | } |
972 | ||
8c6a4d77 | 973 | /* Status A register : 0x00 (read-only) */ |
5c02c033 | 974 | static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl) |
8c6a4d77 BS |
975 | { |
976 | uint32_t retval = fdctrl->sra; | |
977 | ||
978 | FLOPPY_DPRINTF("status register A: 0x%02x\n", retval); | |
979 | ||
980 | return retval; | |
981 | } | |
982 | ||
8977f3c1 | 983 | /* Status B register : 0x01 (read-only) */ |
5c02c033 | 984 | static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl) |
8977f3c1 | 985 | { |
8c6a4d77 BS |
986 | uint32_t retval = fdctrl->srb; |
987 | ||
988 | FLOPPY_DPRINTF("status register B: 0x%02x\n", retval); | |
989 | ||
990 | return retval; | |
8977f3c1 FB |
991 | } |
992 | ||
993 | /* Digital output register : 0x02 */ | |
5c02c033 | 994 | static uint32_t fdctrl_read_dor(FDCtrl *fdctrl) |
8977f3c1 | 995 | { |
1c346df2 | 996 | uint32_t retval = fdctrl->dor; |
8977f3c1 | 997 | |
8977f3c1 | 998 | /* Selected drive */ |
baca51fa | 999 | retval |= fdctrl->cur_drv; |
8977f3c1 FB |
1000 | FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval); |
1001 | ||
1002 | return retval; | |
1003 | } | |
1004 | ||
5c02c033 | 1005 | static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value) |
8977f3c1 | 1006 | { |
8977f3c1 | 1007 | FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value); |
8c6a4d77 BS |
1008 | |
1009 | /* Motors */ | |
1010 | if (value & FD_DOR_MOTEN0) | |
1011 | fdctrl->srb |= FD_SRB_MTR0; | |
1012 | else | |
1013 | fdctrl->srb &= ~FD_SRB_MTR0; | |
1014 | if (value & FD_DOR_MOTEN1) | |
1015 | fdctrl->srb |= FD_SRB_MTR1; | |
1016 | else | |
1017 | fdctrl->srb &= ~FD_SRB_MTR1; | |
1018 | ||
1019 | /* Drive */ | |
1020 | if (value & 1) | |
1021 | fdctrl->srb |= FD_SRB_DR0; | |
1022 | else | |
1023 | fdctrl->srb &= ~FD_SRB_DR0; | |
1024 | ||
8977f3c1 | 1025 | /* Reset */ |
9fea808a | 1026 | if (!(value & FD_DOR_nRESET)) { |
1c346df2 | 1027 | if (fdctrl->dor & FD_DOR_nRESET) { |
4b19ec0c | 1028 | FLOPPY_DPRINTF("controller enter RESET state\n"); |
8977f3c1 FB |
1029 | } |
1030 | } else { | |
1c346df2 | 1031 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4b19ec0c | 1032 | FLOPPY_DPRINTF("controller out of RESET state\n"); |
fb6cf1d0 | 1033 | fdctrl_reset(fdctrl, 1); |
b9b3d225 | 1034 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
8977f3c1 FB |
1035 | } |
1036 | } | |
1037 | /* Selected drive */ | |
9fea808a | 1038 | fdctrl->cur_drv = value & FD_DOR_SELMASK; |
368df94d BS |
1039 | |
1040 | fdctrl->dor = value; | |
8977f3c1 FB |
1041 | } |
1042 | ||
1043 | /* Tape drive register : 0x03 */ | |
5c02c033 | 1044 | static uint32_t fdctrl_read_tape(FDCtrl *fdctrl) |
8977f3c1 | 1045 | { |
46d3233b | 1046 | uint32_t retval = fdctrl->tdr; |
8977f3c1 | 1047 | |
8977f3c1 FB |
1048 | FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval); |
1049 | ||
1050 | return retval; | |
1051 | } | |
1052 | ||
5c02c033 | 1053 | static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value) |
8977f3c1 | 1054 | { |
8977f3c1 | 1055 | /* Reset mode */ |
1c346df2 | 1056 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4b19ec0c | 1057 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
8977f3c1 FB |
1058 | return; |
1059 | } | |
1060 | FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value); | |
1061 | /* Disk boot selection indicator */ | |
46d3233b | 1062 | fdctrl->tdr = value & FD_TDR_BOOTSEL; |
8977f3c1 FB |
1063 | /* Tape indicators: never allow */ |
1064 | } | |
1065 | ||
1066 | /* Main status register : 0x04 (read) */ | |
5c02c033 | 1067 | static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl) |
8977f3c1 | 1068 | { |
b9b3d225 | 1069 | uint32_t retval = fdctrl->msr; |
8977f3c1 | 1070 | |
b9b3d225 | 1071 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
1c346df2 | 1072 | fdctrl->dor |= FD_DOR_nRESET; |
b9b3d225 | 1073 | |
8977f3c1 FB |
1074 | FLOPPY_DPRINTF("main status register: 0x%02x\n", retval); |
1075 | ||
1076 | return retval; | |
1077 | } | |
1078 | ||
1079 | /* Data select rate register : 0x04 (write) */ | |
5c02c033 | 1080 | static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value) |
8977f3c1 | 1081 | { |
8977f3c1 | 1082 | /* Reset mode */ |
1c346df2 | 1083 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4f431960 JM |
1084 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
1085 | return; | |
1086 | } | |
8977f3c1 FB |
1087 | FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value); |
1088 | /* Reset: autoclear */ | |
9fea808a | 1089 | if (value & FD_DSR_SWRESET) { |
1c346df2 | 1090 | fdctrl->dor &= ~FD_DOR_nRESET; |
baca51fa | 1091 | fdctrl_reset(fdctrl, 1); |
1c346df2 | 1092 | fdctrl->dor |= FD_DOR_nRESET; |
8977f3c1 | 1093 | } |
9fea808a | 1094 | if (value & FD_DSR_PWRDOWN) { |
baca51fa | 1095 | fdctrl_reset(fdctrl, 1); |
8977f3c1 | 1096 | } |
b9b3d225 | 1097 | fdctrl->dsr = value; |
8977f3c1 FB |
1098 | } |
1099 | ||
a758f8f4 HP |
1100 | /* Configuration control register: 0x07 (write) */ |
1101 | static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value) | |
1102 | { | |
1103 | /* Reset mode */ | |
1104 | if (!(fdctrl->dor & FD_DOR_nRESET)) { | |
1105 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); | |
1106 | return; | |
1107 | } | |
1108 | FLOPPY_DPRINTF("configuration control register set to 0x%02x\n", value); | |
1109 | ||
1110 | /* Only the rate selection bits used in AT mode, and we | |
1111 | * store those in the DSR. | |
1112 | */ | |
1113 | fdctrl->dsr = (fdctrl->dsr & ~FD_DSR_DRATEMASK) | | |
1114 | (value & FD_DSR_DRATEMASK); | |
1115 | } | |
1116 | ||
5c02c033 | 1117 | static int fdctrl_media_changed(FDrive *drv) |
ea185bbd | 1118 | { |
21fcf360 | 1119 | return drv->media_changed; |
ea185bbd FB |
1120 | } |
1121 | ||
8977f3c1 | 1122 | /* Digital input register : 0x07 (read-only) */ |
5c02c033 | 1123 | static uint32_t fdctrl_read_dir(FDCtrl *fdctrl) |
8977f3c1 | 1124 | { |
8977f3c1 FB |
1125 | uint32_t retval = 0; |
1126 | ||
a2df5fa3 | 1127 | if (fdctrl_media_changed(get_cur_drv(fdctrl))) { |
9fea808a | 1128 | retval |= FD_DIR_DSKCHG; |
a2df5fa3 | 1129 | } |
3c83eb4f | 1130 | if (retval != 0) { |
baca51fa | 1131 | FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval); |
3c83eb4f | 1132 | } |
8977f3c1 FB |
1133 | |
1134 | return retval; | |
1135 | } | |
1136 | ||
07e415f2 KW |
1137 | /* Clear the FIFO and update the state for receiving the next command */ |
1138 | static void fdctrl_to_command_phase(FDCtrl *fdctrl) | |
8977f3c1 | 1139 | { |
baca51fa FB |
1140 | fdctrl->data_dir = FD_DIR_WRITE; |
1141 | fdctrl->data_pos = 0; | |
b9b3d225 | 1142 | fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO); |
8977f3c1 FB |
1143 | } |
1144 | ||
83a26013 KW |
1145 | /* Update the state to allow the guest to read out the command status. |
1146 | * @fifo_len is the number of result bytes to be read out. */ | |
1147 | static void fdctrl_to_result_phase(FDCtrl *fdctrl, int fifo_len) | |
8977f3c1 | 1148 | { |
baca51fa FB |
1149 | fdctrl->data_dir = FD_DIR_READ; |
1150 | fdctrl->data_len = fifo_len; | |
1151 | fdctrl->data_pos = 0; | |
b9b3d225 | 1152 | fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO; |
8977f3c1 FB |
1153 | } |
1154 | ||
1155 | /* Set an error: unimplemented/unknown command */ | |
5c02c033 | 1156 | static void fdctrl_unimplemented(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1157 | { |
cced7a13 BS |
1158 | qemu_log_mask(LOG_UNIMP, "fdc: unimplemented command 0x%02x\n", |
1159 | fdctrl->fifo[0]); | |
9fea808a | 1160 | fdctrl->fifo[0] = FD_SR0_INVCMD; |
83a26013 | 1161 | fdctrl_to_result_phase(fdctrl, 1); |
8977f3c1 FB |
1162 | } |
1163 | ||
6be01b1e PH |
1164 | /* Seek to next sector |
1165 | * returns 0 when end of track reached (for DBL_SIDES on head 1) | |
1166 | * otherwise returns 1 | |
1167 | */ | |
5c02c033 | 1168 | static int fdctrl_seek_to_next_sect(FDCtrl *fdctrl, FDrive *cur_drv) |
746d6de7 BS |
1169 | { |
1170 | FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n", | |
1171 | cur_drv->head, cur_drv->track, cur_drv->sect, | |
1172 | fd_sector(cur_drv)); | |
1173 | /* XXX: cur_drv->sect >= cur_drv->last_sect should be an | |
1174 | error in fact */ | |
6be01b1e PH |
1175 | uint8_t new_head = cur_drv->head; |
1176 | uint8_t new_track = cur_drv->track; | |
1177 | uint8_t new_sect = cur_drv->sect; | |
1178 | ||
1179 | int ret = 1; | |
1180 | ||
1181 | if (new_sect >= cur_drv->last_sect || | |
1182 | new_sect == fdctrl->eot) { | |
1183 | new_sect = 1; | |
746d6de7 | 1184 | if (FD_MULTI_TRACK(fdctrl->data_state)) { |
6be01b1e | 1185 | if (new_head == 0 && |
746d6de7 | 1186 | (cur_drv->flags & FDISK_DBL_SIDES) != 0) { |
6be01b1e | 1187 | new_head = 1; |
746d6de7 | 1188 | } else { |
6be01b1e PH |
1189 | new_head = 0; |
1190 | new_track++; | |
c5139bd9 | 1191 | fdctrl->status0 |= FD_SR0_SEEK; |
6be01b1e PH |
1192 | if ((cur_drv->flags & FDISK_DBL_SIDES) == 0) { |
1193 | ret = 0; | |
1194 | } | |
746d6de7 BS |
1195 | } |
1196 | } else { | |
c5139bd9 | 1197 | fdctrl->status0 |= FD_SR0_SEEK; |
6be01b1e PH |
1198 | new_track++; |
1199 | ret = 0; | |
1200 | } | |
1201 | if (ret == 1) { | |
1202 | FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n", | |
1203 | new_head, new_track, new_sect, fd_sector(cur_drv)); | |
746d6de7 | 1204 | } |
746d6de7 | 1205 | } else { |
6be01b1e | 1206 | new_sect++; |
746d6de7 | 1207 | } |
6be01b1e PH |
1208 | fd_seek(cur_drv, new_head, new_track, new_sect, 1); |
1209 | return ret; | |
746d6de7 BS |
1210 | } |
1211 | ||
8977f3c1 | 1212 | /* Callback for transfer end (stop or abort) */ |
5c02c033 BS |
1213 | static void fdctrl_stop_transfer(FDCtrl *fdctrl, uint8_t status0, |
1214 | uint8_t status1, uint8_t status2) | |
8977f3c1 | 1215 | { |
5c02c033 | 1216 | FDrive *cur_drv; |
baca51fa | 1217 | cur_drv = get_cur_drv(fdctrl); |
075f5532 HP |
1218 | |
1219 | fdctrl->status0 &= ~(FD_SR0_DS0 | FD_SR0_DS1 | FD_SR0_HEAD); | |
1220 | fdctrl->status0 |= GET_CUR_DRV(fdctrl); | |
1221 | if (cur_drv->head) { | |
1222 | fdctrl->status0 |= FD_SR0_HEAD; | |
1223 | } | |
1224 | fdctrl->status0 |= status0; | |
2fee0088 | 1225 | |
8977f3c1 | 1226 | FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n", |
2fee0088 PH |
1227 | status0, status1, status2, fdctrl->status0); |
1228 | fdctrl->fifo[0] = fdctrl->status0; | |
baca51fa FB |
1229 | fdctrl->fifo[1] = status1; |
1230 | fdctrl->fifo[2] = status2; | |
1231 | fdctrl->fifo[3] = cur_drv->track; | |
1232 | fdctrl->fifo[4] = cur_drv->head; | |
1233 | fdctrl->fifo[5] = cur_drv->sect; | |
1234 | fdctrl->fifo[6] = FD_SECTOR_SC; | |
1235 | fdctrl->data_dir = FD_DIR_READ; | |
368df94d | 1236 | if (!(fdctrl->msr & FD_MSR_NONDMA)) { |
baca51fa | 1237 | DMA_release_DREQ(fdctrl->dma_chann); |
ed5fd2cc | 1238 | } |
b9b3d225 | 1239 | fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO; |
368df94d | 1240 | fdctrl->msr &= ~FD_MSR_NONDMA; |
34abf9a7 | 1241 | |
83a26013 | 1242 | fdctrl_to_result_phase(fdctrl, 7); |
d497d534 | 1243 | fdctrl_raise_irq(fdctrl); |
8977f3c1 FB |
1244 | } |
1245 | ||
1246 | /* Prepare a data transfer (either DMA or FIFO) */ | |
5c02c033 | 1247 | static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1248 | { |
5c02c033 | 1249 | FDrive *cur_drv; |
8977f3c1 | 1250 | uint8_t kh, kt, ks; |
8977f3c1 | 1251 | |
cefec4f5 | 1252 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
baca51fa FB |
1253 | cur_drv = get_cur_drv(fdctrl); |
1254 | kt = fdctrl->fifo[2]; | |
1255 | kh = fdctrl->fifo[3]; | |
1256 | ks = fdctrl->fifo[4]; | |
4b19ec0c | 1257 | FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n", |
cefec4f5 | 1258 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
08388273 HP |
1259 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
1260 | NUM_SIDES(cur_drv))); | |
77370520 | 1261 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
8977f3c1 FB |
1262 | case 2: |
1263 | /* sect too big */ | |
9fea808a | 1264 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1265 | fdctrl->fifo[3] = kt; |
1266 | fdctrl->fifo[4] = kh; | |
1267 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1268 | return; |
1269 | case 3: | |
1270 | /* track too big */ | |
77370520 | 1271 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
baca51fa FB |
1272 | fdctrl->fifo[3] = kt; |
1273 | fdctrl->fifo[4] = kh; | |
1274 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1275 | return; |
1276 | case 4: | |
1277 | /* No seek enabled */ | |
9fea808a | 1278 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1279 | fdctrl->fifo[3] = kt; |
1280 | fdctrl->fifo[4] = kh; | |
1281 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1282 | return; |
1283 | case 1: | |
d6ed4e21 | 1284 | fdctrl->status0 |= FD_SR0_SEEK; |
8977f3c1 FB |
1285 | break; |
1286 | default: | |
1287 | break; | |
1288 | } | |
b9b3d225 | 1289 | |
844f65d6 HP |
1290 | /* Check the data rate. If the programmed data rate does not match |
1291 | * the currently inserted medium, the operation has to fail. */ | |
1292 | if (fdctrl->check_media_rate && | |
1293 | (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { | |
1294 | FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n", | |
1295 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); | |
1296 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); | |
1297 | fdctrl->fifo[3] = kt; | |
1298 | fdctrl->fifo[4] = kh; | |
1299 | fdctrl->fifo[5] = ks; | |
1300 | return; | |
1301 | } | |
1302 | ||
8977f3c1 | 1303 | /* Set the FIFO state */ |
baca51fa FB |
1304 | fdctrl->data_dir = direction; |
1305 | fdctrl->data_pos = 0; | |
27c86e24 | 1306 | assert(fdctrl->msr & FD_MSR_CMDBUSY); |
baca51fa FB |
1307 | if (fdctrl->fifo[0] & 0x80) |
1308 | fdctrl->data_state |= FD_STATE_MULTI; | |
1309 | else | |
1310 | fdctrl->data_state &= ~FD_STATE_MULTI; | |
c83f97b5 | 1311 | if (fdctrl->fifo[5] == 0) { |
baca51fa FB |
1312 | fdctrl->data_len = fdctrl->fifo[8]; |
1313 | } else { | |
4f431960 | 1314 | int tmp; |
3bcb80f1 | 1315 | fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]); |
771effeb | 1316 | tmp = (fdctrl->fifo[6] - ks + 1); |
baca51fa | 1317 | if (fdctrl->fifo[0] & 0x80) |
771effeb | 1318 | tmp += fdctrl->fifo[6]; |
4f431960 | 1319 | fdctrl->data_len *= tmp; |
baca51fa | 1320 | } |
890fa6be | 1321 | fdctrl->eot = fdctrl->fifo[6]; |
368df94d | 1322 | if (fdctrl->dor & FD_DOR_DMAEN) { |
8977f3c1 FB |
1323 | int dma_mode; |
1324 | /* DMA transfer are enabled. Check if DMA channel is well programmed */ | |
baca51fa | 1325 | dma_mode = DMA_get_channel_mode(fdctrl->dma_chann); |
8977f3c1 | 1326 | dma_mode = (dma_mode >> 2) & 3; |
baca51fa | 1327 | FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n", |
4f431960 | 1328 | dma_mode, direction, |
baca51fa | 1329 | (128 << fdctrl->fifo[5]) * |
4f431960 | 1330 | (cur_drv->last_sect - ks + 1), fdctrl->data_len); |
8977f3c1 FB |
1331 | if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL || |
1332 | direction == FD_DIR_SCANH) && dma_mode == 0) || | |
1333 | (direction == FD_DIR_WRITE && dma_mode == 2) || | |
7ea004ed HP |
1334 | (direction == FD_DIR_READ && dma_mode == 1) || |
1335 | (direction == FD_DIR_VERIFY)) { | |
8977f3c1 | 1336 | /* No access is allowed until DMA transfer has completed */ |
b9b3d225 | 1337 | fdctrl->msr &= ~FD_MSR_RQM; |
7ea004ed HP |
1338 | if (direction != FD_DIR_VERIFY) { |
1339 | /* Now, we just have to wait for the DMA controller to | |
1340 | * recall us... | |
1341 | */ | |
1342 | DMA_hold_DREQ(fdctrl->dma_chann); | |
1343 | DMA_schedule(fdctrl->dma_chann); | |
1344 | } else { | |
1345 | /* Start transfer */ | |
1346 | fdctrl_transfer_handler(fdctrl, fdctrl->dma_chann, 0, | |
1347 | fdctrl->data_len); | |
1348 | } | |
8977f3c1 | 1349 | return; |
baca51fa | 1350 | } else { |
cced7a13 BS |
1351 | FLOPPY_DPRINTF("bad dma_mode=%d direction=%d\n", dma_mode, |
1352 | direction); | |
8977f3c1 FB |
1353 | } |
1354 | } | |
1355 | FLOPPY_DPRINTF("start non-DMA transfer\n"); | |
368df94d | 1356 | fdctrl->msr |= FD_MSR_NONDMA; |
b9b3d225 BS |
1357 | if (direction != FD_DIR_WRITE) |
1358 | fdctrl->msr |= FD_MSR_DIO; | |
8977f3c1 | 1359 | /* IO based transfer: calculate len */ |
d497d534 | 1360 | fdctrl_raise_irq(fdctrl); |
8977f3c1 FB |
1361 | } |
1362 | ||
1363 | /* Prepare a transfer of deleted data */ | |
5c02c033 | 1364 | static void fdctrl_start_transfer_del(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1365 | { |
cced7a13 | 1366 | qemu_log_mask(LOG_UNIMP, "fdctrl_start_transfer_del() unimplemented\n"); |
77370520 | 1367 | |
8977f3c1 FB |
1368 | /* We don't handle deleted data, |
1369 | * so we don't return *ANYTHING* | |
1370 | */ | |
9fea808a | 1371 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
8977f3c1 FB |
1372 | } |
1373 | ||
1374 | /* handlers for DMA transfers */ | |
85571bc7 FB |
1375 | static int fdctrl_transfer_handler (void *opaque, int nchan, |
1376 | int dma_pos, int dma_len) | |
8977f3c1 | 1377 | { |
5c02c033 BS |
1378 | FDCtrl *fdctrl; |
1379 | FDrive *cur_drv; | |
baca51fa | 1380 | int len, start_pos, rel_pos; |
8977f3c1 FB |
1381 | uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00; |
1382 | ||
baca51fa | 1383 | fdctrl = opaque; |
b9b3d225 | 1384 | if (fdctrl->msr & FD_MSR_RQM) { |
8977f3c1 FB |
1385 | FLOPPY_DPRINTF("Not in DMA transfer mode !\n"); |
1386 | return 0; | |
1387 | } | |
baca51fa FB |
1388 | cur_drv = get_cur_drv(fdctrl); |
1389 | if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL || | |
1390 | fdctrl->data_dir == FD_DIR_SCANH) | |
77370520 | 1391 | status2 = FD_SR2_SNS; |
85571bc7 FB |
1392 | if (dma_len > fdctrl->data_len) |
1393 | dma_len = fdctrl->data_len; | |
4be74634 | 1394 | if (cur_drv->blk == NULL) { |
4f431960 | 1395 | if (fdctrl->data_dir == FD_DIR_WRITE) |
9fea808a | 1396 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
4f431960 | 1397 | else |
9fea808a | 1398 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
4f431960 | 1399 | len = 0; |
890fa6be FB |
1400 | goto transfer_error; |
1401 | } | |
baca51fa | 1402 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; |
85571bc7 FB |
1403 | for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) { |
1404 | len = dma_len - fdctrl->data_pos; | |
baca51fa FB |
1405 | if (len + rel_pos > FD_SECTOR_LEN) |
1406 | len = FD_SECTOR_LEN - rel_pos; | |
6f7e9aec FB |
1407 | FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x " |
1408 | "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos, | |
cefec4f5 | 1409 | fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head, |
baca51fa | 1410 | cur_drv->track, cur_drv->sect, fd_sector(cur_drv), |
9fea808a | 1411 | fd_sector(cur_drv) * FD_SECTOR_LEN); |
baca51fa | 1412 | if (fdctrl->data_dir != FD_DIR_WRITE || |
4f431960 | 1413 | len < FD_SECTOR_LEN || rel_pos != 0) { |
baca51fa | 1414 | /* READ & SCAN commands and realign to a sector for WRITE */ |
4be74634 MA |
1415 | if (blk_read(cur_drv->blk, fd_sector(cur_drv), |
1416 | fdctrl->fifo, 1) < 0) { | |
8977f3c1 FB |
1417 | FLOPPY_DPRINTF("Floppy: error getting sector %d\n", |
1418 | fd_sector(cur_drv)); | |
1419 | /* Sure, image size is too small... */ | |
baca51fa | 1420 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); |
8977f3c1 | 1421 | } |
890fa6be | 1422 | } |
4f431960 JM |
1423 | switch (fdctrl->data_dir) { |
1424 | case FD_DIR_READ: | |
1425 | /* READ commands */ | |
85571bc7 FB |
1426 | DMA_write_memory (nchan, fdctrl->fifo + rel_pos, |
1427 | fdctrl->data_pos, len); | |
4f431960 JM |
1428 | break; |
1429 | case FD_DIR_WRITE: | |
baca51fa | 1430 | /* WRITE commands */ |
8510854e HP |
1431 | if (cur_drv->ro) { |
1432 | /* Handle readonly medium early, no need to do DMA, touch the | |
1433 | * LED or attempt any writes. A real floppy doesn't attempt | |
1434 | * to write to readonly media either. */ | |
1435 | fdctrl_stop_transfer(fdctrl, | |
1436 | FD_SR0_ABNTERM | FD_SR0_SEEK, FD_SR1_NW, | |
1437 | 0x00); | |
1438 | goto transfer_error; | |
1439 | } | |
1440 | ||
85571bc7 FB |
1441 | DMA_read_memory (nchan, fdctrl->fifo + rel_pos, |
1442 | fdctrl->data_pos, len); | |
4be74634 MA |
1443 | if (blk_write(cur_drv->blk, fd_sector(cur_drv), |
1444 | fdctrl->fifo, 1) < 0) { | |
cced7a13 BS |
1445 | FLOPPY_DPRINTF("error writing sector %d\n", |
1446 | fd_sector(cur_drv)); | |
9fea808a | 1447 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
baca51fa | 1448 | goto transfer_error; |
890fa6be | 1449 | } |
4f431960 | 1450 | break; |
7ea004ed HP |
1451 | case FD_DIR_VERIFY: |
1452 | /* VERIFY commands */ | |
1453 | break; | |
4f431960 JM |
1454 | default: |
1455 | /* SCAN commands */ | |
baca51fa | 1456 | { |
4f431960 | 1457 | uint8_t tmpbuf[FD_SECTOR_LEN]; |
baca51fa | 1458 | int ret; |
85571bc7 | 1459 | DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len); |
baca51fa | 1460 | ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len); |
8977f3c1 | 1461 | if (ret == 0) { |
77370520 | 1462 | status2 = FD_SR2_SEH; |
8977f3c1 FB |
1463 | goto end_transfer; |
1464 | } | |
baca51fa FB |
1465 | if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) || |
1466 | (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) { | |
8977f3c1 FB |
1467 | status2 = 0x00; |
1468 | goto end_transfer; | |
1469 | } | |
1470 | } | |
4f431960 | 1471 | break; |
8977f3c1 | 1472 | } |
4f431960 JM |
1473 | fdctrl->data_pos += len; |
1474 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; | |
baca51fa | 1475 | if (rel_pos == 0) { |
8977f3c1 | 1476 | /* Seek to next sector */ |
746d6de7 BS |
1477 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) |
1478 | break; | |
8977f3c1 FB |
1479 | } |
1480 | } | |
4f431960 | 1481 | end_transfer: |
baca51fa FB |
1482 | len = fdctrl->data_pos - start_pos; |
1483 | FLOPPY_DPRINTF("end transfer %d %d %d\n", | |
4f431960 | 1484 | fdctrl->data_pos, len, fdctrl->data_len); |
baca51fa FB |
1485 | if (fdctrl->data_dir == FD_DIR_SCANE || |
1486 | fdctrl->data_dir == FD_DIR_SCANL || | |
1487 | fdctrl->data_dir == FD_DIR_SCANH) | |
77370520 | 1488 | status2 = FD_SR2_SEH; |
baca51fa | 1489 | fdctrl->data_len -= len; |
890fa6be | 1490 | fdctrl_stop_transfer(fdctrl, status0, status1, status2); |
4f431960 | 1491 | transfer_error: |
8977f3c1 | 1492 | |
baca51fa | 1493 | return len; |
8977f3c1 FB |
1494 | } |
1495 | ||
8977f3c1 | 1496 | /* Data register : 0x05 */ |
5c02c033 | 1497 | static uint32_t fdctrl_read_data(FDCtrl *fdctrl) |
8977f3c1 | 1498 | { |
5c02c033 | 1499 | FDrive *cur_drv; |
8977f3c1 | 1500 | uint32_t retval = 0; |
e9077462 | 1501 | uint32_t pos; |
8977f3c1 | 1502 | |
baca51fa | 1503 | cur_drv = get_cur_drv(fdctrl); |
b9b3d225 BS |
1504 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
1505 | if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) { | |
cced7a13 | 1506 | FLOPPY_DPRINTF("error: controller not ready for reading\n"); |
8977f3c1 FB |
1507 | return 0; |
1508 | } | |
baca51fa | 1509 | pos = fdctrl->data_pos; |
e9077462 | 1510 | pos %= FD_SECTOR_LEN; |
368df94d | 1511 | if (fdctrl->msr & FD_MSR_NONDMA) { |
8977f3c1 | 1512 | if (pos == 0) { |
746d6de7 BS |
1513 | if (fdctrl->data_pos != 0) |
1514 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { | |
1515 | FLOPPY_DPRINTF("error seeking to next sector %d\n", | |
1516 | fd_sector(cur_drv)); | |
1517 | return 0; | |
1518 | } | |
4be74634 MA |
1519 | if (blk_read(cur_drv->blk, fd_sector(cur_drv), fdctrl->fifo, 1) |
1520 | < 0) { | |
77370520 BS |
1521 | FLOPPY_DPRINTF("error getting sector %d\n", |
1522 | fd_sector(cur_drv)); | |
1523 | /* Sure, image size is too small... */ | |
1524 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); | |
1525 | } | |
8977f3c1 FB |
1526 | } |
1527 | } | |
baca51fa FB |
1528 | retval = fdctrl->fifo[pos]; |
1529 | if (++fdctrl->data_pos == fdctrl->data_len) { | |
1530 | fdctrl->data_pos = 0; | |
890fa6be | 1531 | /* Switch from transfer mode to status mode |
8977f3c1 FB |
1532 | * then from status mode to command mode |
1533 | */ | |
368df94d | 1534 | if (fdctrl->msr & FD_MSR_NONDMA) { |
c5139bd9 | 1535 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
ed5fd2cc | 1536 | } else { |
07e415f2 | 1537 | fdctrl_to_command_phase(fdctrl); |
ed5fd2cc FB |
1538 | fdctrl_reset_irq(fdctrl); |
1539 | } | |
8977f3c1 FB |
1540 | } |
1541 | FLOPPY_DPRINTF("data register: 0x%02x\n", retval); | |
1542 | ||
1543 | return retval; | |
1544 | } | |
1545 | ||
5c02c033 | 1546 | static void fdctrl_format_sector(FDCtrl *fdctrl) |
8977f3c1 | 1547 | { |
5c02c033 | 1548 | FDrive *cur_drv; |
baca51fa | 1549 | uint8_t kh, kt, ks; |
8977f3c1 | 1550 | |
cefec4f5 | 1551 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
baca51fa FB |
1552 | cur_drv = get_cur_drv(fdctrl); |
1553 | kt = fdctrl->fifo[6]; | |
1554 | kh = fdctrl->fifo[7]; | |
1555 | ks = fdctrl->fifo[8]; | |
1556 | FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n", | |
cefec4f5 | 1557 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
08388273 HP |
1558 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
1559 | NUM_SIDES(cur_drv))); | |
9fea808a | 1560 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
baca51fa FB |
1561 | case 2: |
1562 | /* sect too big */ | |
9fea808a | 1563 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1564 | fdctrl->fifo[3] = kt; |
1565 | fdctrl->fifo[4] = kh; | |
1566 | fdctrl->fifo[5] = ks; | |
1567 | return; | |
1568 | case 3: | |
1569 | /* track too big */ | |
77370520 | 1570 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
baca51fa FB |
1571 | fdctrl->fifo[3] = kt; |
1572 | fdctrl->fifo[4] = kh; | |
1573 | fdctrl->fifo[5] = ks; | |
1574 | return; | |
1575 | case 4: | |
1576 | /* No seek enabled */ | |
9fea808a | 1577 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1578 | fdctrl->fifo[3] = kt; |
1579 | fdctrl->fifo[4] = kh; | |
1580 | fdctrl->fifo[5] = ks; | |
1581 | return; | |
1582 | case 1: | |
cd30b53d | 1583 | fdctrl->status0 |= FD_SR0_SEEK; |
baca51fa FB |
1584 | break; |
1585 | default: | |
1586 | break; | |
1587 | } | |
1588 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); | |
4be74634 MA |
1589 | if (cur_drv->blk == NULL || |
1590 | blk_write(cur_drv->blk, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) { | |
cced7a13 | 1591 | FLOPPY_DPRINTF("error formatting sector %d\n", fd_sector(cur_drv)); |
9fea808a | 1592 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
baca51fa | 1593 | } else { |
4f431960 JM |
1594 | if (cur_drv->sect == cur_drv->last_sect) { |
1595 | fdctrl->data_state &= ~FD_STATE_FORMAT; | |
1596 | /* Last sector done */ | |
cd30b53d | 1597 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
4f431960 JM |
1598 | } else { |
1599 | /* More to do */ | |
1600 | fdctrl->data_pos = 0; | |
1601 | fdctrl->data_len = 4; | |
1602 | } | |
baca51fa FB |
1603 | } |
1604 | } | |
1605 | ||
5c02c033 | 1606 | static void fdctrl_handle_lock(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1607 | { |
1608 | fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0; | |
1609 | fdctrl->fifo[0] = fdctrl->lock << 4; | |
83a26013 | 1610 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1611 | } |
1612 | ||
5c02c033 | 1613 | static void fdctrl_handle_dumpreg(FDCtrl *fdctrl, int direction) |
65cef780 | 1614 | { |
5c02c033 | 1615 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1616 | |
1617 | /* Drives position */ | |
1618 | fdctrl->fifo[0] = drv0(fdctrl)->track; | |
1619 | fdctrl->fifo[1] = drv1(fdctrl)->track; | |
78ae820c BS |
1620 | #if MAX_FD == 4 |
1621 | fdctrl->fifo[2] = drv2(fdctrl)->track; | |
1622 | fdctrl->fifo[3] = drv3(fdctrl)->track; | |
1623 | #else | |
65cef780 BS |
1624 | fdctrl->fifo[2] = 0; |
1625 | fdctrl->fifo[3] = 0; | |
78ae820c | 1626 | #endif |
65cef780 BS |
1627 | /* timers */ |
1628 | fdctrl->fifo[4] = fdctrl->timer0; | |
368df94d | 1629 | fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0); |
65cef780 BS |
1630 | fdctrl->fifo[6] = cur_drv->last_sect; |
1631 | fdctrl->fifo[7] = (fdctrl->lock << 7) | | |
1632 | (cur_drv->perpendicular << 2); | |
1633 | fdctrl->fifo[8] = fdctrl->config; | |
1634 | fdctrl->fifo[9] = fdctrl->precomp_trk; | |
83a26013 | 1635 | fdctrl_to_result_phase(fdctrl, 10); |
65cef780 BS |
1636 | } |
1637 | ||
5c02c033 | 1638 | static void fdctrl_handle_version(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1639 | { |
1640 | /* Controller's version */ | |
1641 | fdctrl->fifo[0] = fdctrl->version; | |
83a26013 | 1642 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1643 | } |
1644 | ||
5c02c033 | 1645 | static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1646 | { |
1647 | fdctrl->fifo[0] = 0x41; /* Stepping 1 */ | |
83a26013 | 1648 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1649 | } |
1650 | ||
5c02c033 | 1651 | static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction) |
65cef780 | 1652 | { |
5c02c033 | 1653 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1654 | |
1655 | /* Drives position */ | |
1656 | drv0(fdctrl)->track = fdctrl->fifo[3]; | |
1657 | drv1(fdctrl)->track = fdctrl->fifo[4]; | |
78ae820c BS |
1658 | #if MAX_FD == 4 |
1659 | drv2(fdctrl)->track = fdctrl->fifo[5]; | |
1660 | drv3(fdctrl)->track = fdctrl->fifo[6]; | |
1661 | #endif | |
65cef780 BS |
1662 | /* timers */ |
1663 | fdctrl->timer0 = fdctrl->fifo[7]; | |
1664 | fdctrl->timer1 = fdctrl->fifo[8]; | |
1665 | cur_drv->last_sect = fdctrl->fifo[9]; | |
1666 | fdctrl->lock = fdctrl->fifo[10] >> 7; | |
1667 | cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF; | |
1668 | fdctrl->config = fdctrl->fifo[11]; | |
1669 | fdctrl->precomp_trk = fdctrl->fifo[12]; | |
1670 | fdctrl->pwrd = fdctrl->fifo[13]; | |
07e415f2 | 1671 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1672 | } |
1673 | ||
5c02c033 | 1674 | static void fdctrl_handle_save(FDCtrl *fdctrl, int direction) |
65cef780 | 1675 | { |
5c02c033 | 1676 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1677 | |
1678 | fdctrl->fifo[0] = 0; | |
1679 | fdctrl->fifo[1] = 0; | |
1680 | /* Drives position */ | |
1681 | fdctrl->fifo[2] = drv0(fdctrl)->track; | |
1682 | fdctrl->fifo[3] = drv1(fdctrl)->track; | |
78ae820c BS |
1683 | #if MAX_FD == 4 |
1684 | fdctrl->fifo[4] = drv2(fdctrl)->track; | |
1685 | fdctrl->fifo[5] = drv3(fdctrl)->track; | |
1686 | #else | |
65cef780 BS |
1687 | fdctrl->fifo[4] = 0; |
1688 | fdctrl->fifo[5] = 0; | |
78ae820c | 1689 | #endif |
65cef780 BS |
1690 | /* timers */ |
1691 | fdctrl->fifo[6] = fdctrl->timer0; | |
1692 | fdctrl->fifo[7] = fdctrl->timer1; | |
1693 | fdctrl->fifo[8] = cur_drv->last_sect; | |
1694 | fdctrl->fifo[9] = (fdctrl->lock << 7) | | |
1695 | (cur_drv->perpendicular << 2); | |
1696 | fdctrl->fifo[10] = fdctrl->config; | |
1697 | fdctrl->fifo[11] = fdctrl->precomp_trk; | |
1698 | fdctrl->fifo[12] = fdctrl->pwrd; | |
1699 | fdctrl->fifo[13] = 0; | |
1700 | fdctrl->fifo[14] = 0; | |
83a26013 | 1701 | fdctrl_to_result_phase(fdctrl, 15); |
65cef780 BS |
1702 | } |
1703 | ||
5c02c033 | 1704 | static void fdctrl_handle_readid(FDCtrl *fdctrl, int direction) |
65cef780 | 1705 | { |
5c02c033 | 1706 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1707 | |
65cef780 | 1708 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; |
bc72ad67 AB |
1709 | timer_mod(fdctrl->result_timer, |
1710 | qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + (get_ticks_per_sec() / 50)); | |
65cef780 BS |
1711 | } |
1712 | ||
5c02c033 | 1713 | static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction) |
65cef780 | 1714 | { |
5c02c033 | 1715 | FDrive *cur_drv; |
65cef780 | 1716 | |
cefec4f5 | 1717 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1718 | cur_drv = get_cur_drv(fdctrl); |
1719 | fdctrl->data_state |= FD_STATE_FORMAT; | |
1720 | if (fdctrl->fifo[0] & 0x80) | |
1721 | fdctrl->data_state |= FD_STATE_MULTI; | |
1722 | else | |
1723 | fdctrl->data_state &= ~FD_STATE_MULTI; | |
65cef780 BS |
1724 | cur_drv->bps = |
1725 | fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2]; | |
1726 | #if 0 | |
1727 | cur_drv->last_sect = | |
1728 | cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] : | |
1729 | fdctrl->fifo[3] / 2; | |
1730 | #else | |
1731 | cur_drv->last_sect = fdctrl->fifo[3]; | |
1732 | #endif | |
1733 | /* TODO: implement format using DMA expected by the Bochs BIOS | |
1734 | * and Linux fdformat (read 3 bytes per sector via DMA and fill | |
1735 | * the sector with the specified fill byte | |
1736 | */ | |
1737 | fdctrl->data_state &= ~FD_STATE_FORMAT; | |
1738 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); | |
1739 | } | |
1740 | ||
5c02c033 | 1741 | static void fdctrl_handle_specify(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1742 | { |
1743 | fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF; | |
1744 | fdctrl->timer1 = fdctrl->fifo[2] >> 1; | |
368df94d BS |
1745 | if (fdctrl->fifo[2] & 1) |
1746 | fdctrl->dor &= ~FD_DOR_DMAEN; | |
1747 | else | |
1748 | fdctrl->dor |= FD_DOR_DMAEN; | |
65cef780 | 1749 | /* No result back */ |
07e415f2 | 1750 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1751 | } |
1752 | ||
5c02c033 | 1753 | static void fdctrl_handle_sense_drive_status(FDCtrl *fdctrl, int direction) |
65cef780 | 1754 | { |
5c02c033 | 1755 | FDrive *cur_drv; |
65cef780 | 1756 | |
cefec4f5 | 1757 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1758 | cur_drv = get_cur_drv(fdctrl); |
1759 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; | |
1760 | /* 1 Byte status back */ | |
1761 | fdctrl->fifo[0] = (cur_drv->ro << 6) | | |
1762 | (cur_drv->track == 0 ? 0x10 : 0x00) | | |
1763 | (cur_drv->head << 2) | | |
cefec4f5 | 1764 | GET_CUR_DRV(fdctrl) | |
65cef780 | 1765 | 0x28; |
83a26013 | 1766 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1767 | } |
1768 | ||
5c02c033 | 1769 | static void fdctrl_handle_recalibrate(FDCtrl *fdctrl, int direction) |
65cef780 | 1770 | { |
5c02c033 | 1771 | FDrive *cur_drv; |
65cef780 | 1772 | |
cefec4f5 | 1773 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1774 | cur_drv = get_cur_drv(fdctrl); |
1775 | fd_recalibrate(cur_drv); | |
07e415f2 | 1776 | fdctrl_to_command_phase(fdctrl); |
65cef780 | 1777 | /* Raise Interrupt */ |
d497d534 HP |
1778 | fdctrl->status0 |= FD_SR0_SEEK; |
1779 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
1780 | } |
1781 | ||
5c02c033 | 1782 | static void fdctrl_handle_sense_interrupt_status(FDCtrl *fdctrl, int direction) |
65cef780 | 1783 | { |
5c02c033 | 1784 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1785 | |
2fee0088 | 1786 | if (fdctrl->reset_sensei > 0) { |
f2d81b33 BS |
1787 | fdctrl->fifo[0] = |
1788 | FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei; | |
1789 | fdctrl->reset_sensei--; | |
2fee0088 PH |
1790 | } else if (!(fdctrl->sra & FD_SRA_INTPEND)) { |
1791 | fdctrl->fifo[0] = FD_SR0_INVCMD; | |
83a26013 | 1792 | fdctrl_to_result_phase(fdctrl, 1); |
2fee0088 | 1793 | return; |
f2d81b33 | 1794 | } else { |
f2d81b33 | 1795 | fdctrl->fifo[0] = |
2fee0088 PH |
1796 | (fdctrl->status0 & ~(FD_SR0_HEAD | FD_SR0_DS1 | FD_SR0_DS0)) |
1797 | | GET_CUR_DRV(fdctrl); | |
f2d81b33 BS |
1798 | } |
1799 | ||
65cef780 | 1800 | fdctrl->fifo[1] = cur_drv->track; |
83a26013 | 1801 | fdctrl_to_result_phase(fdctrl, 2); |
65cef780 | 1802 | fdctrl_reset_irq(fdctrl); |
77370520 | 1803 | fdctrl->status0 = FD_SR0_RDYCHG; |
65cef780 BS |
1804 | } |
1805 | ||
5c02c033 | 1806 | static void fdctrl_handle_seek(FDCtrl *fdctrl, int direction) |
65cef780 | 1807 | { |
5c02c033 | 1808 | FDrive *cur_drv; |
65cef780 | 1809 | |
cefec4f5 | 1810 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1811 | cur_drv = get_cur_drv(fdctrl); |
07e415f2 | 1812 | fdctrl_to_command_phase(fdctrl); |
b072a3c8 HP |
1813 | /* The seek command just sends step pulses to the drive and doesn't care if |
1814 | * there is a medium inserted of if it's banging the head against the drive. | |
1815 | */ | |
6be01b1e | 1816 | fd_seek(cur_drv, cur_drv->head, fdctrl->fifo[2], cur_drv->sect, 1); |
b072a3c8 | 1817 | /* Raise Interrupt */ |
d497d534 HP |
1818 | fdctrl->status0 |= FD_SR0_SEEK; |
1819 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
1820 | } |
1821 | ||
5c02c033 | 1822 | static void fdctrl_handle_perpendicular_mode(FDCtrl *fdctrl, int direction) |
65cef780 | 1823 | { |
5c02c033 | 1824 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1825 | |
1826 | if (fdctrl->fifo[1] & 0x80) | |
1827 | cur_drv->perpendicular = fdctrl->fifo[1] & 0x7; | |
1828 | /* No result back */ | |
07e415f2 | 1829 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1830 | } |
1831 | ||
5c02c033 | 1832 | static void fdctrl_handle_configure(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1833 | { |
1834 | fdctrl->config = fdctrl->fifo[2]; | |
1835 | fdctrl->precomp_trk = fdctrl->fifo[3]; | |
1836 | /* No result back */ | |
07e415f2 | 1837 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1838 | } |
1839 | ||
5c02c033 | 1840 | static void fdctrl_handle_powerdown_mode(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1841 | { |
1842 | fdctrl->pwrd = fdctrl->fifo[1]; | |
1843 | fdctrl->fifo[0] = fdctrl->fifo[1]; | |
83a26013 | 1844 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1845 | } |
1846 | ||
5c02c033 | 1847 | static void fdctrl_handle_option(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1848 | { |
1849 | /* No result back */ | |
07e415f2 | 1850 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1851 | } |
1852 | ||
5c02c033 | 1853 | static void fdctrl_handle_drive_specification_command(FDCtrl *fdctrl, int direction) |
65cef780 | 1854 | { |
5c02c033 | 1855 | FDrive *cur_drv = get_cur_drv(fdctrl); |
e9077462 | 1856 | uint32_t pos; |
65cef780 | 1857 | |
e9077462 PM |
1858 | pos = fdctrl->data_pos - 1; |
1859 | pos %= FD_SECTOR_LEN; | |
1860 | if (fdctrl->fifo[pos] & 0x80) { | |
65cef780 | 1861 | /* Command parameters done */ |
e9077462 | 1862 | if (fdctrl->fifo[pos] & 0x40) { |
65cef780 BS |
1863 | fdctrl->fifo[0] = fdctrl->fifo[1]; |
1864 | fdctrl->fifo[2] = 0; | |
1865 | fdctrl->fifo[3] = 0; | |
83a26013 | 1866 | fdctrl_to_result_phase(fdctrl, 4); |
65cef780 | 1867 | } else { |
07e415f2 | 1868 | fdctrl_to_command_phase(fdctrl); |
65cef780 BS |
1869 | } |
1870 | } else if (fdctrl->data_len > 7) { | |
1871 | /* ERROR */ | |
1872 | fdctrl->fifo[0] = 0x80 | | |
cefec4f5 | 1873 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl); |
83a26013 | 1874 | fdctrl_to_result_phase(fdctrl, 1); |
65cef780 BS |
1875 | } |
1876 | } | |
1877 | ||
6d013772 | 1878 | static void fdctrl_handle_relative_seek_in(FDCtrl *fdctrl, int direction) |
65cef780 | 1879 | { |
5c02c033 | 1880 | FDrive *cur_drv; |
65cef780 | 1881 | |
cefec4f5 | 1882 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1883 | cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1884 | if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) { |
6be01b1e PH |
1885 | fd_seek(cur_drv, cur_drv->head, cur_drv->max_track - 1, |
1886 | cur_drv->sect, 1); | |
65cef780 | 1887 | } else { |
6d013772 PH |
1888 | fd_seek(cur_drv, cur_drv->head, |
1889 | cur_drv->track + fdctrl->fifo[2], cur_drv->sect, 1); | |
65cef780 | 1890 | } |
07e415f2 | 1891 | fdctrl_to_command_phase(fdctrl); |
77370520 | 1892 | /* Raise Interrupt */ |
d497d534 HP |
1893 | fdctrl->status0 |= FD_SR0_SEEK; |
1894 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
1895 | } |
1896 | ||
6d013772 | 1897 | static void fdctrl_handle_relative_seek_out(FDCtrl *fdctrl, int direction) |
65cef780 | 1898 | { |
5c02c033 | 1899 | FDrive *cur_drv; |
65cef780 | 1900 | |
cefec4f5 | 1901 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1902 | cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1903 | if (fdctrl->fifo[2] > cur_drv->track) { |
6be01b1e | 1904 | fd_seek(cur_drv, cur_drv->head, 0, cur_drv->sect, 1); |
65cef780 | 1905 | } else { |
6d013772 PH |
1906 | fd_seek(cur_drv, cur_drv->head, |
1907 | cur_drv->track - fdctrl->fifo[2], cur_drv->sect, 1); | |
65cef780 | 1908 | } |
07e415f2 | 1909 | fdctrl_to_command_phase(fdctrl); |
65cef780 | 1910 | /* Raise Interrupt */ |
d497d534 HP |
1911 | fdctrl->status0 |= FD_SR0_SEEK; |
1912 | fdctrl_raise_irq(fdctrl); | |
65cef780 BS |
1913 | } |
1914 | ||
678803ab BS |
1915 | static const struct { |
1916 | uint8_t value; | |
1917 | uint8_t mask; | |
1918 | const char* name; | |
1919 | int parameters; | |
5c02c033 | 1920 | void (*handler)(FDCtrl *fdctrl, int direction); |
678803ab BS |
1921 | int direction; |
1922 | } handlers[] = { | |
1923 | { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ }, | |
1924 | { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE }, | |
1925 | { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek }, | |
1926 | { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status }, | |
1927 | { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate }, | |
1928 | { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track }, | |
1929 | { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ }, | |
1930 | { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */ | |
1931 | { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */ | |
1932 | { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ }, | |
1933 | { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE }, | |
7ea004ed | 1934 | { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_start_transfer, FD_DIR_VERIFY }, |
678803ab BS |
1935 | { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL }, |
1936 | { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH }, | |
1937 | { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE }, | |
1938 | { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid }, | |
1939 | { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify }, | |
1940 | { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status }, | |
1941 | { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode }, | |
1942 | { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure }, | |
1943 | { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode }, | |
1944 | { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option }, | |
1945 | { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command }, | |
1946 | { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out }, | |
1947 | { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented }, | |
1948 | { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in }, | |
1949 | { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock }, | |
1950 | { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg }, | |
1951 | { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version }, | |
1952 | { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid }, | |
1953 | { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */ | |
1954 | { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */ | |
1955 | }; | |
1956 | /* Associate command to an index in the 'handlers' array */ | |
1957 | static uint8_t command_to_handler[256]; | |
1958 | ||
5c02c033 | 1959 | static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value) |
baca51fa | 1960 | { |
5c02c033 | 1961 | FDrive *cur_drv; |
e9077462 | 1962 | uint32_t pos; |
baca51fa | 1963 | |
8977f3c1 | 1964 | /* Reset mode */ |
1c346df2 | 1965 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4b19ec0c | 1966 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
8977f3c1 FB |
1967 | return; |
1968 | } | |
b9b3d225 | 1969 | if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) { |
cced7a13 | 1970 | FLOPPY_DPRINTF("error: controller not ready for writing\n"); |
8977f3c1 FB |
1971 | return; |
1972 | } | |
b9b3d225 | 1973 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
8977f3c1 | 1974 | /* Is it write command time ? */ |
368df94d | 1975 | if (fdctrl->msr & FD_MSR_NONDMA) { |
8977f3c1 | 1976 | /* FIFO data write */ |
b3bc1540 BS |
1977 | pos = fdctrl->data_pos++; |
1978 | pos %= FD_SECTOR_LEN; | |
1979 | fdctrl->fifo[pos] = value; | |
1980 | if (pos == FD_SECTOR_LEN - 1 || | |
baca51fa | 1981 | fdctrl->data_pos == fdctrl->data_len) { |
77370520 | 1982 | cur_drv = get_cur_drv(fdctrl); |
4be74634 MA |
1983 | if (blk_write(cur_drv->blk, fd_sector(cur_drv), fdctrl->fifo, 1) |
1984 | < 0) { | |
cced7a13 BS |
1985 | FLOPPY_DPRINTF("error writing sector %d\n", |
1986 | fd_sector(cur_drv)); | |
77370520 BS |
1987 | return; |
1988 | } | |
746d6de7 BS |
1989 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { |
1990 | FLOPPY_DPRINTF("error seeking to next sector %d\n", | |
1991 | fd_sector(cur_drv)); | |
1992 | return; | |
1993 | } | |
8977f3c1 | 1994 | } |
890fa6be | 1995 | /* Switch from transfer mode to status mode |
8977f3c1 FB |
1996 | * then from status mode to command mode |
1997 | */ | |
b9b3d225 | 1998 | if (fdctrl->data_pos == fdctrl->data_len) |
c5139bd9 | 1999 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); |
8977f3c1 FB |
2000 | return; |
2001 | } | |
baca51fa | 2002 | if (fdctrl->data_pos == 0) { |
8977f3c1 | 2003 | /* Command */ |
678803ab BS |
2004 | pos = command_to_handler[value & 0xff]; |
2005 | FLOPPY_DPRINTF("%s command\n", handlers[pos].name); | |
2006 | fdctrl->data_len = handlers[pos].parameters + 1; | |
1457a758 | 2007 | fdctrl->msr |= FD_MSR_CMDBUSY; |
8977f3c1 | 2008 | } |
678803ab | 2009 | |
baca51fa | 2010 | FLOPPY_DPRINTF("%s: %02x\n", __func__, value); |
e9077462 PM |
2011 | pos = fdctrl->data_pos++; |
2012 | pos %= FD_SECTOR_LEN; | |
2013 | fdctrl->fifo[pos] = value; | |
77370520 | 2014 | if (fdctrl->data_pos == fdctrl->data_len) { |
8977f3c1 FB |
2015 | /* We now have all parameters |
2016 | * and will be able to treat the command | |
2017 | */ | |
4f431960 JM |
2018 | if (fdctrl->data_state & FD_STATE_FORMAT) { |
2019 | fdctrl_format_sector(fdctrl); | |
8977f3c1 FB |
2020 | return; |
2021 | } | |
65cef780 | 2022 | |
678803ab BS |
2023 | pos = command_to_handler[fdctrl->fifo[0] & 0xff]; |
2024 | FLOPPY_DPRINTF("treat %s command\n", handlers[pos].name); | |
2025 | (*handlers[pos].handler)(fdctrl, handlers[pos].direction); | |
8977f3c1 FB |
2026 | } |
2027 | } | |
ed5fd2cc FB |
2028 | |
2029 | static void fdctrl_result_timer(void *opaque) | |
2030 | { | |
5c02c033 BS |
2031 | FDCtrl *fdctrl = opaque; |
2032 | FDrive *cur_drv = get_cur_drv(fdctrl); | |
4f431960 | 2033 | |
b7ffa3b1 TS |
2034 | /* Pretend we are spinning. |
2035 | * This is needed for Coherent, which uses READ ID to check for | |
2036 | * sector interleaving. | |
2037 | */ | |
2038 | if (cur_drv->last_sect != 0) { | |
2039 | cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1; | |
2040 | } | |
844f65d6 HP |
2041 | /* READ_ID can't automatically succeed! */ |
2042 | if (fdctrl->check_media_rate && | |
2043 | (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { | |
2044 | FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n", | |
2045 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); | |
2046 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); | |
2047 | } else { | |
2048 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); | |
2049 | } | |
ed5fd2cc | 2050 | } |
678803ab | 2051 | |
7d4b4ba5 | 2052 | static void fdctrl_change_cb(void *opaque, bool load) |
8e49ca46 MA |
2053 | { |
2054 | FDrive *drive = opaque; | |
2055 | ||
2056 | drive->media_changed = 1; | |
21fcf360 | 2057 | fd_revalidate(drive); |
8e49ca46 MA |
2058 | } |
2059 | ||
2060 | static const BlockDevOps fdctrl_block_ops = { | |
2061 | .change_media_cb = fdctrl_change_cb, | |
2062 | }; | |
2063 | ||
678803ab | 2064 | /* Init functions */ |
a3ef7a61 | 2065 | static void fdctrl_connect_drives(FDCtrl *fdctrl, Error **errp) |
678803ab | 2066 | { |
12a71a02 | 2067 | unsigned int i; |
7d0d6950 | 2068 | FDrive *drive; |
678803ab | 2069 | |
678803ab | 2070 | for (i = 0; i < MAX_FD; i++) { |
7d0d6950 | 2071 | drive = &fdctrl->drives[i]; |
844f65d6 | 2072 | drive->fdctrl = fdctrl; |
7d0d6950 | 2073 | |
4be74634 MA |
2074 | if (drive->blk) { |
2075 | if (blk_get_on_error(drive->blk, 0) != BLOCKDEV_ON_ERROR_ENOSPC) { | |
a3ef7a61 AF |
2076 | error_setg(errp, "fdc doesn't support drive option werror"); |
2077 | return; | |
b47b3525 | 2078 | } |
4be74634 | 2079 | if (blk_get_on_error(drive->blk, 1) != BLOCKDEV_ON_ERROR_REPORT) { |
a3ef7a61 AF |
2080 | error_setg(errp, "fdc doesn't support drive option rerror"); |
2081 | return; | |
b47b3525 MA |
2082 | } |
2083 | } | |
2084 | ||
7d0d6950 | 2085 | fd_init(drive); |
cfb08fba | 2086 | fdctrl_change_cb(drive, 0); |
4be74634 MA |
2087 | if (drive->blk) { |
2088 | blk_set_dev_ops(drive->blk, &fdctrl_block_ops, drive); | |
7d0d6950 | 2089 | } |
678803ab | 2090 | } |
678803ab BS |
2091 | } |
2092 | ||
dfc65f1f MA |
2093 | ISADevice *fdctrl_init_isa(ISABus *bus, DriveInfo **fds) |
2094 | { | |
4a17cc4f AF |
2095 | DeviceState *dev; |
2096 | ISADevice *isadev; | |
dfc65f1f | 2097 | |
4a17cc4f AF |
2098 | isadev = isa_try_create(bus, TYPE_ISA_FDC); |
2099 | if (!isadev) { | |
dfc65f1f MA |
2100 | return NULL; |
2101 | } | |
4a17cc4f | 2102 | dev = DEVICE(isadev); |
dfc65f1f MA |
2103 | |
2104 | if (fds[0]) { | |
4be74634 | 2105 | qdev_prop_set_drive_nofail(dev, "driveA", blk_by_legacy_dinfo(fds[0])); |
dfc65f1f MA |
2106 | } |
2107 | if (fds[1]) { | |
4be74634 | 2108 | qdev_prop_set_drive_nofail(dev, "driveB", blk_by_legacy_dinfo(fds[1])); |
dfc65f1f | 2109 | } |
4a17cc4f | 2110 | qdev_init_nofail(dev); |
dfc65f1f | 2111 | |
4a17cc4f | 2112 | return isadev; |
dfc65f1f MA |
2113 | } |
2114 | ||
63ffb564 | 2115 | void fdctrl_init_sysbus(qemu_irq irq, int dma_chann, |
a8170e5e | 2116 | hwaddr mmio_base, DriveInfo **fds) |
2091ba23 | 2117 | { |
5c02c033 | 2118 | FDCtrl *fdctrl; |
2091ba23 | 2119 | DeviceState *dev; |
dd3be742 | 2120 | SysBusDevice *sbd; |
5c02c033 | 2121 | FDCtrlSysBus *sys; |
2091ba23 | 2122 | |
19d46d71 | 2123 | dev = qdev_create(NULL, "sysbus-fdc"); |
dd3be742 | 2124 | sys = SYSBUS_FDC(dev); |
99244fa1 GH |
2125 | fdctrl = &sys->state; |
2126 | fdctrl->dma_chann = dma_chann; /* FIXME */ | |
995bf0ca | 2127 | if (fds[0]) { |
4be74634 | 2128 | qdev_prop_set_drive_nofail(dev, "driveA", blk_by_legacy_dinfo(fds[0])); |
995bf0ca GH |
2129 | } |
2130 | if (fds[1]) { | |
4be74634 | 2131 | qdev_prop_set_drive_nofail(dev, "driveB", blk_by_legacy_dinfo(fds[1])); |
995bf0ca | 2132 | } |
e23a1b33 | 2133 | qdev_init_nofail(dev); |
dd3be742 HT |
2134 | sbd = SYS_BUS_DEVICE(dev); |
2135 | sysbus_connect_irq(sbd, 0, irq); | |
2136 | sysbus_mmio_map(sbd, 0, mmio_base); | |
678803ab BS |
2137 | } |
2138 | ||
a8170e5e | 2139 | void sun4m_fdctrl_init(qemu_irq irq, hwaddr io_base, |
63ffb564 | 2140 | DriveInfo **fds, qemu_irq *fdc_tc) |
678803ab | 2141 | { |
f64ab228 | 2142 | DeviceState *dev; |
5c02c033 | 2143 | FDCtrlSysBus *sys; |
678803ab | 2144 | |
12a71a02 | 2145 | dev = qdev_create(NULL, "SUNW,fdtwo"); |
995bf0ca | 2146 | if (fds[0]) { |
4be74634 | 2147 | qdev_prop_set_drive_nofail(dev, "drive", blk_by_legacy_dinfo(fds[0])); |
995bf0ca | 2148 | } |
e23a1b33 | 2149 | qdev_init_nofail(dev); |
dd3be742 HT |
2150 | sys = SYSBUS_FDC(dev); |
2151 | sysbus_connect_irq(SYS_BUS_DEVICE(sys), 0, irq); | |
2152 | sysbus_mmio_map(SYS_BUS_DEVICE(sys), 0, io_base); | |
f64ab228 | 2153 | *fdc_tc = qdev_get_gpio_in(dev, 0); |
678803ab | 2154 | } |
f64ab228 | 2155 | |
a3ef7a61 | 2156 | static void fdctrl_realize_common(FDCtrl *fdctrl, Error **errp) |
f64ab228 | 2157 | { |
12a71a02 BS |
2158 | int i, j; |
2159 | static int command_tables_inited = 0; | |
f64ab228 | 2160 | |
12a71a02 BS |
2161 | /* Fill 'command_to_handler' lookup table */ |
2162 | if (!command_tables_inited) { | |
2163 | command_tables_inited = 1; | |
2164 | for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) { | |
2165 | for (j = 0; j < sizeof(command_to_handler); j++) { | |
2166 | if ((j & handlers[i].mask) == handlers[i].value) { | |
2167 | command_to_handler[j] = i; | |
2168 | } | |
2169 | } | |
2170 | } | |
2171 | } | |
2172 | ||
2173 | FLOPPY_DPRINTF("init controller\n"); | |
2174 | fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN); | |
d7a6c270 | 2175 | fdctrl->fifo_size = 512; |
bc72ad67 | 2176 | fdctrl->result_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL, |
a3ef7a61 | 2177 | fdctrl_result_timer, fdctrl); |
12a71a02 BS |
2178 | |
2179 | fdctrl->version = 0x90; /* Intel 82078 controller */ | |
2180 | fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */ | |
d7a6c270 | 2181 | fdctrl->num_floppies = MAX_FD; |
12a71a02 | 2182 | |
a3ef7a61 | 2183 | if (fdctrl->dma_chann != -1) { |
99244fa1 | 2184 | DMA_register_channel(fdctrl->dma_chann, &fdctrl_transfer_handler, fdctrl); |
a3ef7a61 AF |
2185 | } |
2186 | fdctrl_connect_drives(fdctrl, errp); | |
f64ab228 BS |
2187 | } |
2188 | ||
212ec7ba | 2189 | static const MemoryRegionPortio fdc_portio_list[] = { |
2f290a8c | 2190 | { 1, 5, 1, .read = fdctrl_read, .write = fdctrl_write }, |
212ec7ba RH |
2191 | { 7, 1, 1, .read = fdctrl_read, .write = fdctrl_write }, |
2192 | PORTIO_END_OF_LIST(), | |
2f290a8c RH |
2193 | }; |
2194 | ||
db895a1e | 2195 | static void isabus_fdc_realize(DeviceState *dev, Error **errp) |
8baf73ad | 2196 | { |
db895a1e | 2197 | ISADevice *isadev = ISA_DEVICE(dev); |
020c8e76 | 2198 | FDCtrlISABus *isa = ISA_FDC(dev); |
5c02c033 | 2199 | FDCtrl *fdctrl = &isa->state; |
a3ef7a61 | 2200 | Error *err = NULL; |
8baf73ad | 2201 | |
db895a1e AF |
2202 | isa_register_portio_list(isadev, isa->iobase, fdc_portio_list, fdctrl, |
2203 | "fdc"); | |
dee41d58 | 2204 | |
db895a1e | 2205 | isa_init_irq(isadev, &fdctrl->irq, isa->irq); |
c9ae703d | 2206 | fdctrl->dma_chann = isa->dma; |
8baf73ad | 2207 | |
db895a1e | 2208 | qdev_set_legacy_instance_id(dev, isa->iobase, 2); |
a3ef7a61 AF |
2209 | fdctrl_realize_common(fdctrl, &err); |
2210 | if (err != NULL) { | |
2211 | error_propagate(errp, err); | |
db895a1e AF |
2212 | return; |
2213 | } | |
8baf73ad GH |
2214 | } |
2215 | ||
940194c2 | 2216 | static void sysbus_fdc_initfn(Object *obj) |
12a71a02 | 2217 | { |
19d46d71 | 2218 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
940194c2 | 2219 | FDCtrlSysBus *sys = SYSBUS_FDC(obj); |
5c02c033 | 2220 | FDCtrl *fdctrl = &sys->state; |
12a71a02 | 2221 | |
19d46d71 AF |
2222 | fdctrl->dma_chann = -1; |
2223 | ||
940194c2 | 2224 | memory_region_init_io(&fdctrl->iomem, obj, &fdctrl_mem_ops, fdctrl, |
2d256e6f | 2225 | "fdc", 0x08); |
19d46d71 | 2226 | sysbus_init_mmio(sbd, &fdctrl->iomem); |
940194c2 HT |
2227 | } |
2228 | ||
19d46d71 | 2229 | static void sun4m_fdc_initfn(Object *obj) |
940194c2 | 2230 | { |
19d46d71 AF |
2231 | SysBusDevice *sbd = SYS_BUS_DEVICE(obj); |
2232 | FDCtrlSysBus *sys = SYSBUS_FDC(obj); | |
940194c2 | 2233 | FDCtrl *fdctrl = &sys->state; |
940194c2 | 2234 | |
19d46d71 AF |
2235 | memory_region_init_io(&fdctrl->iomem, obj, &fdctrl_mem_strict_ops, |
2236 | fdctrl, "fdctrl", 0x08); | |
2237 | sysbus_init_mmio(sbd, &fdctrl->iomem); | |
940194c2 | 2238 | } |
2be37833 | 2239 | |
19d46d71 | 2240 | static void sysbus_fdc_common_initfn(Object *obj) |
940194c2 | 2241 | { |
19d46d71 AF |
2242 | DeviceState *dev = DEVICE(obj); |
2243 | SysBusDevice *sbd = SYS_BUS_DEVICE(dev); | |
940194c2 HT |
2244 | FDCtrlSysBus *sys = SYSBUS_FDC(obj); |
2245 | FDCtrl *fdctrl = &sys->state; | |
2246 | ||
19d46d71 AF |
2247 | qdev_set_legacy_instance_id(dev, 0 /* io */, 2); /* FIXME */ |
2248 | ||
2249 | sysbus_init_irq(sbd, &fdctrl->irq); | |
2250 | qdev_init_gpio_in(dev, fdctrl_handle_tc, 1); | |
12a71a02 BS |
2251 | } |
2252 | ||
19d46d71 | 2253 | static void sysbus_fdc_common_realize(DeviceState *dev, Error **errp) |
12a71a02 | 2254 | { |
dd3be742 HT |
2255 | FDCtrlSysBus *sys = SYSBUS_FDC(dev); |
2256 | FDCtrl *fdctrl = &sys->state; | |
12a71a02 | 2257 | |
19d46d71 | 2258 | fdctrl_realize_common(fdctrl, errp); |
12a71a02 | 2259 | } |
f64ab228 | 2260 | |
61a8d649 | 2261 | FDriveType isa_fdc_get_drive_type(ISADevice *fdc, int i) |
34d4260e | 2262 | { |
020c8e76 | 2263 | FDCtrlISABus *isa = ISA_FDC(fdc); |
34d4260e | 2264 | |
61a8d649 | 2265 | return isa->state.drives[i].drive; |
34d4260e KW |
2266 | } |
2267 | ||
a64405d1 JK |
2268 | static const VMStateDescription vmstate_isa_fdc ={ |
2269 | .name = "fdc", | |
2270 | .version_id = 2, | |
2271 | .minimum_version_id = 2, | |
d49805ae | 2272 | .fields = (VMStateField[]) { |
a64405d1 JK |
2273 | VMSTATE_STRUCT(state, FDCtrlISABus, 0, vmstate_fdc, FDCtrl), |
2274 | VMSTATE_END_OF_LIST() | |
2275 | } | |
2276 | }; | |
2277 | ||
39bffca2 | 2278 | static Property isa_fdc_properties[] = { |
c7bcc85d | 2279 | DEFINE_PROP_UINT32("iobase", FDCtrlISABus, iobase, 0x3f0), |
c9ae703d HP |
2280 | DEFINE_PROP_UINT32("irq", FDCtrlISABus, irq, 6), |
2281 | DEFINE_PROP_UINT32("dma", FDCtrlISABus, dma, 2), | |
4be74634 MA |
2282 | DEFINE_PROP_DRIVE("driveA", FDCtrlISABus, state.drives[0].blk), |
2283 | DEFINE_PROP_DRIVE("driveB", FDCtrlISABus, state.drives[1].blk), | |
09c6d585 HP |
2284 | DEFINE_PROP_BIT("check_media_rate", FDCtrlISABus, state.check_media_rate, |
2285 | 0, true), | |
39bffca2 AL |
2286 | DEFINE_PROP_END_OF_LIST(), |
2287 | }; | |
2288 | ||
020c8e76 | 2289 | static void isabus_fdc_class_init(ObjectClass *klass, void *data) |
8f04ee08 | 2290 | { |
39bffca2 | 2291 | DeviceClass *dc = DEVICE_CLASS(klass); |
db895a1e AF |
2292 | |
2293 | dc->realize = isabus_fdc_realize; | |
39bffca2 | 2294 | dc->fw_name = "fdc"; |
39bffca2 AL |
2295 | dc->reset = fdctrl_external_reset_isa; |
2296 | dc->vmsd = &vmstate_isa_fdc; | |
2297 | dc->props = isa_fdc_properties; | |
125ee0ed | 2298 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
39bffca2 AL |
2299 | } |
2300 | ||
81782b6a GA |
2301 | static void isabus_fdc_instance_init(Object *obj) |
2302 | { | |
2303 | FDCtrlISABus *isa = ISA_FDC(obj); | |
2304 | ||
2305 | device_add_bootindex_property(obj, &isa->bootindexA, | |
2306 | "bootindexA", "/floppy@0", | |
2307 | DEVICE(obj), NULL); | |
2308 | device_add_bootindex_property(obj, &isa->bootindexB, | |
2309 | "bootindexB", "/floppy@1", | |
2310 | DEVICE(obj), NULL); | |
2311 | } | |
2312 | ||
8c43a6f0 | 2313 | static const TypeInfo isa_fdc_info = { |
020c8e76 | 2314 | .name = TYPE_ISA_FDC, |
39bffca2 AL |
2315 | .parent = TYPE_ISA_DEVICE, |
2316 | .instance_size = sizeof(FDCtrlISABus), | |
020c8e76 | 2317 | .class_init = isabus_fdc_class_init, |
81782b6a | 2318 | .instance_init = isabus_fdc_instance_init, |
8baf73ad GH |
2319 | }; |
2320 | ||
a64405d1 JK |
2321 | static const VMStateDescription vmstate_sysbus_fdc ={ |
2322 | .name = "fdc", | |
2323 | .version_id = 2, | |
2324 | .minimum_version_id = 2, | |
d49805ae | 2325 | .fields = (VMStateField[]) { |
a64405d1 JK |
2326 | VMSTATE_STRUCT(state, FDCtrlSysBus, 0, vmstate_fdc, FDCtrl), |
2327 | VMSTATE_END_OF_LIST() | |
2328 | } | |
2329 | }; | |
2330 | ||
999e12bb | 2331 | static Property sysbus_fdc_properties[] = { |
4be74634 MA |
2332 | DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus, state.drives[0].blk), |
2333 | DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus, state.drives[1].blk), | |
999e12bb | 2334 | DEFINE_PROP_END_OF_LIST(), |
12a71a02 BS |
2335 | }; |
2336 | ||
999e12bb AL |
2337 | static void sysbus_fdc_class_init(ObjectClass *klass, void *data) |
2338 | { | |
39bffca2 | 2339 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 2340 | |
39bffca2 | 2341 | dc->props = sysbus_fdc_properties; |
125ee0ed | 2342 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
999e12bb AL |
2343 | } |
2344 | ||
8c43a6f0 | 2345 | static const TypeInfo sysbus_fdc_info = { |
19d46d71 AF |
2346 | .name = "sysbus-fdc", |
2347 | .parent = TYPE_SYSBUS_FDC, | |
940194c2 | 2348 | .instance_init = sysbus_fdc_initfn, |
39bffca2 | 2349 | .class_init = sysbus_fdc_class_init, |
999e12bb AL |
2350 | }; |
2351 | ||
2352 | static Property sun4m_fdc_properties[] = { | |
4be74634 | 2353 | DEFINE_PROP_DRIVE("drive", FDCtrlSysBus, state.drives[0].blk), |
999e12bb AL |
2354 | DEFINE_PROP_END_OF_LIST(), |
2355 | }; | |
2356 | ||
2357 | static void sun4m_fdc_class_init(ObjectClass *klass, void *data) | |
2358 | { | |
39bffca2 | 2359 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb | 2360 | |
39bffca2 | 2361 | dc->props = sun4m_fdc_properties; |
125ee0ed | 2362 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
999e12bb AL |
2363 | } |
2364 | ||
8c43a6f0 | 2365 | static const TypeInfo sun4m_fdc_info = { |
39bffca2 | 2366 | .name = "SUNW,fdtwo", |
19d46d71 | 2367 | .parent = TYPE_SYSBUS_FDC, |
940194c2 | 2368 | .instance_init = sun4m_fdc_initfn, |
39bffca2 | 2369 | .class_init = sun4m_fdc_class_init, |
f64ab228 BS |
2370 | }; |
2371 | ||
19d46d71 AF |
2372 | static void sysbus_fdc_common_class_init(ObjectClass *klass, void *data) |
2373 | { | |
2374 | DeviceClass *dc = DEVICE_CLASS(klass); | |
2375 | ||
2376 | dc->realize = sysbus_fdc_common_realize; | |
2377 | dc->reset = fdctrl_external_reset_sysbus; | |
2378 | dc->vmsd = &vmstate_sysbus_fdc; | |
2379 | } | |
2380 | ||
2381 | static const TypeInfo sysbus_fdc_type_info = { | |
2382 | .name = TYPE_SYSBUS_FDC, | |
2383 | .parent = TYPE_SYS_BUS_DEVICE, | |
2384 | .instance_size = sizeof(FDCtrlSysBus), | |
2385 | .instance_init = sysbus_fdc_common_initfn, | |
2386 | .abstract = true, | |
2387 | .class_init = sysbus_fdc_common_class_init, | |
2388 | }; | |
2389 | ||
83f7d43a | 2390 | static void fdc_register_types(void) |
f64ab228 | 2391 | { |
39bffca2 | 2392 | type_register_static(&isa_fdc_info); |
19d46d71 | 2393 | type_register_static(&sysbus_fdc_type_info); |
39bffca2 AL |
2394 | type_register_static(&sysbus_fdc_info); |
2395 | type_register_static(&sun4m_fdc_info); | |
f64ab228 BS |
2396 | } |
2397 | ||
83f7d43a | 2398 | type_init(fdc_register_types) |