]>
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 | |
87ecb68b PB |
30 | #include "hw.h" |
31 | #include "fdc.h" | |
b47b3525 | 32 | #include "qemu-error.h" |
87ecb68b PB |
33 | #include "qemu-timer.h" |
34 | #include "isa.h" | |
f64ab228 | 35 | #include "sysbus.h" |
e8133762 | 36 | #include "qdev-addr.h" |
2446333c | 37 | #include "blockdev.h" |
1ca4d09a | 38 | #include "sysemu.h" |
8977f3c1 FB |
39 | |
40 | /********************************************************/ | |
41 | /* debug Floppy devices */ | |
42 | //#define DEBUG_FLOPPY | |
43 | ||
44 | #ifdef DEBUG_FLOPPY | |
001faf32 BS |
45 | #define FLOPPY_DPRINTF(fmt, ...) \ |
46 | do { printf("FLOPPY: " fmt , ## __VA_ARGS__); } while (0) | |
8977f3c1 | 47 | #else |
001faf32 | 48 | #define FLOPPY_DPRINTF(fmt, ...) |
8977f3c1 FB |
49 | #endif |
50 | ||
001faf32 BS |
51 | #define FLOPPY_ERROR(fmt, ...) \ |
52 | do { printf("FLOPPY ERROR: %s: " fmt, __func__ , ## __VA_ARGS__); } while (0) | |
8977f3c1 FB |
53 | |
54 | /********************************************************/ | |
55 | /* Floppy drive emulation */ | |
56 | ||
cefec4f5 BS |
57 | #define GET_CUR_DRV(fdctrl) ((fdctrl)->cur_drv) |
58 | #define SET_CUR_DRV(fdctrl, drive) ((fdctrl)->cur_drv = (drive)) | |
59 | ||
8977f3c1 | 60 | /* Will always be a fixed parameter for us */ |
f2d81b33 BS |
61 | #define FD_SECTOR_LEN 512 |
62 | #define FD_SECTOR_SC 2 /* Sector size code */ | |
63 | #define FD_RESET_SENSEI_COUNT 4 /* Number of sense interrupts on RESET */ | |
8977f3c1 | 64 | |
844f65d6 HP |
65 | typedef struct FDCtrl FDCtrl; |
66 | ||
8977f3c1 | 67 | /* Floppy disk drive emulation */ |
5c02c033 | 68 | typedef enum FDiskFlags { |
baca51fa | 69 | FDISK_DBL_SIDES = 0x01, |
5c02c033 | 70 | } FDiskFlags; |
baca51fa | 71 | |
5c02c033 | 72 | typedef struct FDrive { |
844f65d6 | 73 | FDCtrl *fdctrl; |
8977f3c1 FB |
74 | BlockDriverState *bs; |
75 | /* Drive status */ | |
5c02c033 | 76 | FDriveType drive; |
8977f3c1 | 77 | uint8_t perpendicular; /* 2.88 MB access mode */ |
8977f3c1 FB |
78 | /* Position */ |
79 | uint8_t head; | |
80 | uint8_t track; | |
81 | uint8_t sect; | |
8977f3c1 | 82 | /* Media */ |
5c02c033 | 83 | FDiskFlags flags; |
8977f3c1 FB |
84 | uint8_t last_sect; /* Nb sector per track */ |
85 | uint8_t max_track; /* Nb of tracks */ | |
baca51fa | 86 | uint16_t bps; /* Bytes per sector */ |
8977f3c1 | 87 | uint8_t ro; /* Is read-only */ |
7d905f71 | 88 | uint8_t media_changed; /* Is media changed */ |
844f65d6 | 89 | uint8_t media_rate; /* Data rate of medium */ |
5c02c033 | 90 | } FDrive; |
8977f3c1 | 91 | |
5c02c033 | 92 | static void fd_init(FDrive *drv) |
8977f3c1 FB |
93 | { |
94 | /* Drive */ | |
b939777c | 95 | drv->drive = FDRIVE_DRV_NONE; |
8977f3c1 | 96 | drv->perpendicular = 0; |
8977f3c1 | 97 | /* Disk */ |
baca51fa | 98 | drv->last_sect = 0; |
8977f3c1 FB |
99 | drv->max_track = 0; |
100 | } | |
101 | ||
08388273 HP |
102 | #define NUM_SIDES(drv) ((drv)->flags & FDISK_DBL_SIDES ? 2 : 1) |
103 | ||
7859cb98 | 104 | static int fd_sector_calc(uint8_t head, uint8_t track, uint8_t sect, |
08388273 | 105 | uint8_t last_sect, uint8_t num_sides) |
8977f3c1 | 106 | { |
08388273 | 107 | return (((track * num_sides) + head) * last_sect) + sect - 1; |
8977f3c1 FB |
108 | } |
109 | ||
110 | /* Returns current position, in sectors, for given drive */ | |
5c02c033 | 111 | static int fd_sector(FDrive *drv) |
8977f3c1 | 112 | { |
08388273 HP |
113 | return fd_sector_calc(drv->head, drv->track, drv->sect, drv->last_sect, |
114 | NUM_SIDES(drv)); | |
8977f3c1 FB |
115 | } |
116 | ||
77370520 BS |
117 | /* Seek to a new position: |
118 | * returns 0 if already on right track | |
119 | * returns 1 if track changed | |
120 | * returns 2 if track is invalid | |
121 | * returns 3 if sector is invalid | |
122 | * returns 4 if seek is disabled | |
123 | */ | |
5c02c033 BS |
124 | static int fd_seek(FDrive *drv, uint8_t head, uint8_t track, uint8_t sect, |
125 | int enable_seek) | |
8977f3c1 FB |
126 | { |
127 | uint32_t sector; | |
baca51fa FB |
128 | int ret; |
129 | ||
130 | if (track > drv->max_track || | |
4f431960 | 131 | (head != 0 && (drv->flags & FDISK_DBL_SIDES) == 0)) { |
ed5fd2cc FB |
132 | FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n", |
133 | head, track, sect, 1, | |
134 | (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1, | |
135 | drv->max_track, drv->last_sect); | |
8977f3c1 FB |
136 | return 2; |
137 | } | |
138 | if (sect > drv->last_sect) { | |
ed5fd2cc FB |
139 | FLOPPY_DPRINTF("try to read %d %02x %02x (max=%d %d %02x %02x)\n", |
140 | head, track, sect, 1, | |
141 | (drv->flags & FDISK_DBL_SIDES) == 0 ? 0 : 1, | |
142 | drv->max_track, drv->last_sect); | |
8977f3c1 FB |
143 | return 3; |
144 | } | |
08388273 | 145 | sector = fd_sector_calc(head, track, sect, drv->last_sect, NUM_SIDES(drv)); |
baca51fa | 146 | ret = 0; |
8977f3c1 FB |
147 | if (sector != fd_sector(drv)) { |
148 | #if 0 | |
149 | if (!enable_seek) { | |
150 | FLOPPY_ERROR("no implicit seek %d %02x %02x (max=%d %02x %02x)\n", | |
151 | head, track, sect, 1, drv->max_track, drv->last_sect); | |
152 | return 4; | |
153 | } | |
154 | #endif | |
155 | drv->head = head; | |
4f431960 JM |
156 | if (drv->track != track) |
157 | ret = 1; | |
8977f3c1 FB |
158 | drv->track = track; |
159 | drv->sect = sect; | |
8977f3c1 FB |
160 | } |
161 | ||
baca51fa | 162 | return ret; |
8977f3c1 FB |
163 | } |
164 | ||
165 | /* Set drive back to track 0 */ | |
5c02c033 | 166 | static void fd_recalibrate(FDrive *drv) |
8977f3c1 FB |
167 | { |
168 | FLOPPY_DPRINTF("recalibrate\n"); | |
169 | drv->head = 0; | |
170 | drv->track = 0; | |
171 | drv->sect = 1; | |
8977f3c1 FB |
172 | } |
173 | ||
174 | /* Revalidate a disk drive after a disk change */ | |
5c02c033 | 175 | static void fd_revalidate(FDrive *drv) |
8977f3c1 | 176 | { |
baca51fa | 177 | int nb_heads, max_track, last_sect, ro; |
5bbdbb46 | 178 | FDriveType drive; |
f8d3d128 | 179 | FDriveRate rate; |
8977f3c1 FB |
180 | |
181 | FLOPPY_DPRINTF("revalidate\n"); | |
a541f297 | 182 | if (drv->bs != NULL && bdrv_is_inserted(drv->bs)) { |
4f431960 | 183 | ro = bdrv_is_read_only(drv->bs); |
5bbdbb46 | 184 | bdrv_get_floppy_geometry_hint(drv->bs, &nb_heads, &max_track, |
f8d3d128 | 185 | &last_sect, drv->drive, &drive, &rate); |
4f431960 JM |
186 | if (nb_heads != 0 && max_track != 0 && last_sect != 0) { |
187 | FLOPPY_DPRINTF("User defined disk (%d %d %d)", | |
ed5fd2cc | 188 | nb_heads - 1, max_track, last_sect); |
4f431960 | 189 | } else { |
5bbdbb46 BS |
190 | FLOPPY_DPRINTF("Floppy disk (%d h %d t %d s) %s\n", nb_heads, |
191 | max_track, last_sect, ro ? "ro" : "rw"); | |
4f431960 JM |
192 | } |
193 | if (nb_heads == 1) { | |
194 | drv->flags &= ~FDISK_DBL_SIDES; | |
195 | } else { | |
196 | drv->flags |= FDISK_DBL_SIDES; | |
197 | } | |
198 | drv->max_track = max_track; | |
199 | drv->last_sect = last_sect; | |
200 | drv->ro = ro; | |
5bbdbb46 | 201 | drv->drive = drive; |
844f65d6 | 202 | drv->media_rate = rate; |
8977f3c1 | 203 | } else { |
4f431960 | 204 | FLOPPY_DPRINTF("No disk in drive\n"); |
baca51fa | 205 | drv->last_sect = 0; |
4f431960 JM |
206 | drv->max_track = 0; |
207 | drv->flags &= ~FDISK_DBL_SIDES; | |
8977f3c1 | 208 | } |
caed8802 FB |
209 | } |
210 | ||
8977f3c1 | 211 | /********************************************************/ |
4b19ec0c | 212 | /* Intel 82078 floppy disk controller emulation */ |
8977f3c1 | 213 | |
5c02c033 BS |
214 | static void fdctrl_reset(FDCtrl *fdctrl, int do_irq); |
215 | static void fdctrl_reset_fifo(FDCtrl *fdctrl); | |
85571bc7 | 216 | static int fdctrl_transfer_handler (void *opaque, int nchan, |
c227f099 | 217 | int dma_pos, int dma_len); |
5c02c033 | 218 | static void fdctrl_raise_irq(FDCtrl *fdctrl, uint8_t status0); |
a2df5fa3 | 219 | static FDrive *get_cur_drv(FDCtrl *fdctrl); |
5c02c033 BS |
220 | |
221 | static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl); | |
222 | static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl); | |
223 | static uint32_t fdctrl_read_dor(FDCtrl *fdctrl); | |
224 | static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value); | |
225 | static uint32_t fdctrl_read_tape(FDCtrl *fdctrl); | |
226 | static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value); | |
227 | static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl); | |
228 | static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value); | |
229 | static uint32_t fdctrl_read_data(FDCtrl *fdctrl); | |
230 | static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value); | |
231 | static uint32_t fdctrl_read_dir(FDCtrl *fdctrl); | |
a758f8f4 | 232 | static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value); |
8977f3c1 | 233 | |
8977f3c1 FB |
234 | enum { |
235 | FD_DIR_WRITE = 0, | |
236 | FD_DIR_READ = 1, | |
237 | FD_DIR_SCANE = 2, | |
238 | FD_DIR_SCANL = 3, | |
239 | FD_DIR_SCANH = 4, | |
240 | }; | |
241 | ||
242 | enum { | |
b9b3d225 BS |
243 | FD_STATE_MULTI = 0x01, /* multi track flag */ |
244 | FD_STATE_FORMAT = 0x02, /* format flag */ | |
245 | FD_STATE_SEEK = 0x04, /* seek flag */ | |
8977f3c1 FB |
246 | }; |
247 | ||
9fea808a | 248 | enum { |
8c6a4d77 BS |
249 | FD_REG_SRA = 0x00, |
250 | FD_REG_SRB = 0x01, | |
9fea808a BS |
251 | FD_REG_DOR = 0x02, |
252 | FD_REG_TDR = 0x03, | |
253 | FD_REG_MSR = 0x04, | |
254 | FD_REG_DSR = 0x04, | |
255 | FD_REG_FIFO = 0x05, | |
256 | FD_REG_DIR = 0x07, | |
a758f8f4 | 257 | FD_REG_CCR = 0x07, |
9fea808a BS |
258 | }; |
259 | ||
260 | enum { | |
65cef780 | 261 | FD_CMD_READ_TRACK = 0x02, |
9fea808a BS |
262 | FD_CMD_SPECIFY = 0x03, |
263 | FD_CMD_SENSE_DRIVE_STATUS = 0x04, | |
65cef780 BS |
264 | FD_CMD_WRITE = 0x05, |
265 | FD_CMD_READ = 0x06, | |
9fea808a BS |
266 | FD_CMD_RECALIBRATE = 0x07, |
267 | FD_CMD_SENSE_INTERRUPT_STATUS = 0x08, | |
65cef780 BS |
268 | FD_CMD_WRITE_DELETED = 0x09, |
269 | FD_CMD_READ_ID = 0x0a, | |
270 | FD_CMD_READ_DELETED = 0x0c, | |
271 | FD_CMD_FORMAT_TRACK = 0x0d, | |
9fea808a BS |
272 | FD_CMD_DUMPREG = 0x0e, |
273 | FD_CMD_SEEK = 0x0f, | |
274 | FD_CMD_VERSION = 0x10, | |
65cef780 | 275 | FD_CMD_SCAN_EQUAL = 0x11, |
9fea808a BS |
276 | FD_CMD_PERPENDICULAR_MODE = 0x12, |
277 | FD_CMD_CONFIGURE = 0x13, | |
65cef780 BS |
278 | FD_CMD_LOCK = 0x14, |
279 | FD_CMD_VERIFY = 0x16, | |
9fea808a BS |
280 | FD_CMD_POWERDOWN_MODE = 0x17, |
281 | FD_CMD_PART_ID = 0x18, | |
65cef780 BS |
282 | FD_CMD_SCAN_LOW_OR_EQUAL = 0x19, |
283 | FD_CMD_SCAN_HIGH_OR_EQUAL = 0x1d, | |
bb350a5e | 284 | FD_CMD_SAVE = 0x2e, |
9fea808a | 285 | FD_CMD_OPTION = 0x33, |
bb350a5e | 286 | FD_CMD_RESTORE = 0x4e, |
9fea808a BS |
287 | FD_CMD_DRIVE_SPECIFICATION_COMMAND = 0x8e, |
288 | FD_CMD_RELATIVE_SEEK_OUT = 0x8f, | |
9fea808a BS |
289 | FD_CMD_FORMAT_AND_WRITE = 0xcd, |
290 | FD_CMD_RELATIVE_SEEK_IN = 0xcf, | |
291 | }; | |
292 | ||
293 | enum { | |
294 | FD_CONFIG_PRETRK = 0xff, /* Pre-compensation set to track 0 */ | |
295 | FD_CONFIG_FIFOTHR = 0x0f, /* FIFO threshold set to 1 byte */ | |
296 | FD_CONFIG_POLL = 0x10, /* Poll enabled */ | |
297 | FD_CONFIG_EFIFO = 0x20, /* FIFO disabled */ | |
298 | FD_CONFIG_EIS = 0x40, /* No implied seeks */ | |
299 | }; | |
300 | ||
301 | enum { | |
302 | FD_SR0_EQPMT = 0x10, | |
303 | FD_SR0_SEEK = 0x20, | |
304 | FD_SR0_ABNTERM = 0x40, | |
305 | FD_SR0_INVCMD = 0x80, | |
306 | FD_SR0_RDYCHG = 0xc0, | |
307 | }; | |
308 | ||
77370520 | 309 | enum { |
844f65d6 | 310 | FD_SR1_MA = 0x01, /* Missing address mark */ |
8510854e | 311 | FD_SR1_NW = 0x02, /* Not writable */ |
77370520 BS |
312 | FD_SR1_EC = 0x80, /* End of cylinder */ |
313 | }; | |
314 | ||
315 | enum { | |
316 | FD_SR2_SNS = 0x04, /* Scan not satisfied */ | |
317 | FD_SR2_SEH = 0x08, /* Scan equal hit */ | |
318 | }; | |
319 | ||
8c6a4d77 BS |
320 | enum { |
321 | FD_SRA_DIR = 0x01, | |
322 | FD_SRA_nWP = 0x02, | |
323 | FD_SRA_nINDX = 0x04, | |
324 | FD_SRA_HDSEL = 0x08, | |
325 | FD_SRA_nTRK0 = 0x10, | |
326 | FD_SRA_STEP = 0x20, | |
327 | FD_SRA_nDRV2 = 0x40, | |
328 | FD_SRA_INTPEND = 0x80, | |
329 | }; | |
330 | ||
331 | enum { | |
332 | FD_SRB_MTR0 = 0x01, | |
333 | FD_SRB_MTR1 = 0x02, | |
334 | FD_SRB_WGATE = 0x04, | |
335 | FD_SRB_RDATA = 0x08, | |
336 | FD_SRB_WDATA = 0x10, | |
337 | FD_SRB_DR0 = 0x20, | |
338 | }; | |
339 | ||
9fea808a | 340 | enum { |
78ae820c BS |
341 | #if MAX_FD == 4 |
342 | FD_DOR_SELMASK = 0x03, | |
343 | #else | |
9fea808a | 344 | FD_DOR_SELMASK = 0x01, |
78ae820c | 345 | #endif |
9fea808a BS |
346 | FD_DOR_nRESET = 0x04, |
347 | FD_DOR_DMAEN = 0x08, | |
348 | FD_DOR_MOTEN0 = 0x10, | |
349 | FD_DOR_MOTEN1 = 0x20, | |
350 | FD_DOR_MOTEN2 = 0x40, | |
351 | FD_DOR_MOTEN3 = 0x80, | |
352 | }; | |
353 | ||
354 | enum { | |
78ae820c | 355 | #if MAX_FD == 4 |
9fea808a | 356 | FD_TDR_BOOTSEL = 0x0c, |
78ae820c BS |
357 | #else |
358 | FD_TDR_BOOTSEL = 0x04, | |
359 | #endif | |
9fea808a BS |
360 | }; |
361 | ||
362 | enum { | |
363 | FD_DSR_DRATEMASK= 0x03, | |
364 | FD_DSR_PWRDOWN = 0x40, | |
365 | FD_DSR_SWRESET = 0x80, | |
366 | }; | |
367 | ||
368 | enum { | |
369 | FD_MSR_DRV0BUSY = 0x01, | |
370 | FD_MSR_DRV1BUSY = 0x02, | |
371 | FD_MSR_DRV2BUSY = 0x04, | |
372 | FD_MSR_DRV3BUSY = 0x08, | |
373 | FD_MSR_CMDBUSY = 0x10, | |
374 | FD_MSR_NONDMA = 0x20, | |
375 | FD_MSR_DIO = 0x40, | |
376 | FD_MSR_RQM = 0x80, | |
377 | }; | |
378 | ||
379 | enum { | |
380 | FD_DIR_DSKCHG = 0x80, | |
381 | }; | |
382 | ||
8977f3c1 FB |
383 | #define FD_MULTI_TRACK(state) ((state) & FD_STATE_MULTI) |
384 | #define FD_DID_SEEK(state) ((state) & FD_STATE_SEEK) | |
baca51fa | 385 | #define FD_FORMAT_CMD(state) ((state) & FD_STATE_FORMAT) |
8977f3c1 | 386 | |
5c02c033 | 387 | struct FDCtrl { |
dc6c1b37 | 388 | MemoryRegion iomem; |
d537cf6c | 389 | qemu_irq irq; |
4b19ec0c | 390 | /* Controller state */ |
ed5fd2cc | 391 | QEMUTimer *result_timer; |
242cca4f BS |
392 | int dma_chann; |
393 | /* Controller's identification */ | |
394 | uint8_t version; | |
395 | /* HW */ | |
8c6a4d77 BS |
396 | uint8_t sra; |
397 | uint8_t srb; | |
368df94d | 398 | uint8_t dor; |
d7a6c270 | 399 | uint8_t dor_vmstate; /* only used as temp during vmstate */ |
46d3233b | 400 | uint8_t tdr; |
b9b3d225 | 401 | uint8_t dsr; |
368df94d | 402 | uint8_t msr; |
8977f3c1 | 403 | uint8_t cur_drv; |
77370520 BS |
404 | uint8_t status0; |
405 | uint8_t status1; | |
406 | uint8_t status2; | |
8977f3c1 | 407 | /* Command FIFO */ |
33f00271 | 408 | uint8_t *fifo; |
d7a6c270 | 409 | int32_t fifo_size; |
8977f3c1 FB |
410 | uint32_t data_pos; |
411 | uint32_t data_len; | |
412 | uint8_t data_state; | |
413 | uint8_t data_dir; | |
890fa6be | 414 | uint8_t eot; /* last wanted sector */ |
8977f3c1 | 415 | /* States kept only to be returned back */ |
8977f3c1 FB |
416 | /* precompensation */ |
417 | uint8_t precomp_trk; | |
418 | uint8_t config; | |
419 | uint8_t lock; | |
420 | /* Power down config (also with status regB access mode */ | |
421 | uint8_t pwrd; | |
422 | /* Floppy drives */ | |
d7a6c270 | 423 | uint8_t num_floppies; |
242cca4f BS |
424 | /* Sun4m quirks? */ |
425 | int sun4m; | |
5c02c033 | 426 | FDrive drives[MAX_FD]; |
f2d81b33 | 427 | int reset_sensei; |
09c6d585 | 428 | uint32_t check_media_rate; |
242cca4f BS |
429 | /* Timers state */ |
430 | uint8_t timer0; | |
431 | uint8_t timer1; | |
baca51fa FB |
432 | }; |
433 | ||
5c02c033 | 434 | typedef struct FDCtrlSysBus { |
8baf73ad | 435 | SysBusDevice busdev; |
5c02c033 BS |
436 | struct FDCtrl state; |
437 | } FDCtrlSysBus; | |
8baf73ad | 438 | |
5c02c033 | 439 | typedef struct FDCtrlISABus { |
8baf73ad | 440 | ISADevice busdev; |
c9ae703d HP |
441 | uint32_t iobase; |
442 | uint32_t irq; | |
443 | uint32_t dma; | |
5c02c033 | 444 | struct FDCtrl state; |
1ca4d09a GN |
445 | int32_t bootindexA; |
446 | int32_t bootindexB; | |
5c02c033 | 447 | } FDCtrlISABus; |
8baf73ad | 448 | |
baca51fa FB |
449 | static uint32_t fdctrl_read (void *opaque, uint32_t reg) |
450 | { | |
5c02c033 | 451 | FDCtrl *fdctrl = opaque; |
baca51fa FB |
452 | uint32_t retval; |
453 | ||
a18e67f5 | 454 | reg &= 7; |
e64d7d59 | 455 | switch (reg) { |
8c6a4d77 BS |
456 | case FD_REG_SRA: |
457 | retval = fdctrl_read_statusA(fdctrl); | |
4f431960 | 458 | break; |
8c6a4d77 | 459 | case FD_REG_SRB: |
4f431960 JM |
460 | retval = fdctrl_read_statusB(fdctrl); |
461 | break; | |
9fea808a | 462 | case FD_REG_DOR: |
4f431960 JM |
463 | retval = fdctrl_read_dor(fdctrl); |
464 | break; | |
9fea808a | 465 | case FD_REG_TDR: |
baca51fa | 466 | retval = fdctrl_read_tape(fdctrl); |
4f431960 | 467 | break; |
9fea808a | 468 | case FD_REG_MSR: |
baca51fa | 469 | retval = fdctrl_read_main_status(fdctrl); |
4f431960 | 470 | break; |
9fea808a | 471 | case FD_REG_FIFO: |
baca51fa | 472 | retval = fdctrl_read_data(fdctrl); |
4f431960 | 473 | break; |
9fea808a | 474 | case FD_REG_DIR: |
baca51fa | 475 | retval = fdctrl_read_dir(fdctrl); |
4f431960 | 476 | break; |
a541f297 | 477 | default: |
4f431960 JM |
478 | retval = (uint32_t)(-1); |
479 | break; | |
a541f297 | 480 | } |
ed5fd2cc | 481 | FLOPPY_DPRINTF("read reg%d: 0x%02x\n", reg & 7, retval); |
baca51fa FB |
482 | |
483 | return retval; | |
484 | } | |
485 | ||
486 | static void fdctrl_write (void *opaque, uint32_t reg, uint32_t value) | |
487 | { | |
5c02c033 | 488 | FDCtrl *fdctrl = opaque; |
baca51fa | 489 | |
ed5fd2cc FB |
490 | FLOPPY_DPRINTF("write reg%d: 0x%02x\n", reg & 7, value); |
491 | ||
a18e67f5 | 492 | reg &= 7; |
e64d7d59 | 493 | switch (reg) { |
9fea808a | 494 | case FD_REG_DOR: |
4f431960 JM |
495 | fdctrl_write_dor(fdctrl, value); |
496 | break; | |
9fea808a | 497 | case FD_REG_TDR: |
baca51fa | 498 | fdctrl_write_tape(fdctrl, value); |
4f431960 | 499 | break; |
9fea808a | 500 | case FD_REG_DSR: |
baca51fa | 501 | fdctrl_write_rate(fdctrl, value); |
4f431960 | 502 | break; |
9fea808a | 503 | case FD_REG_FIFO: |
baca51fa | 504 | fdctrl_write_data(fdctrl, value); |
4f431960 | 505 | break; |
a758f8f4 HP |
506 | case FD_REG_CCR: |
507 | fdctrl_write_ccr(fdctrl, value); | |
508 | break; | |
a541f297 | 509 | default: |
4f431960 | 510 | break; |
a541f297 | 511 | } |
baca51fa FB |
512 | } |
513 | ||
dc6c1b37 AK |
514 | static uint64_t fdctrl_read_mem (void *opaque, target_phys_addr_t reg, |
515 | unsigned ize) | |
62a46c61 | 516 | { |
5dcb6b91 | 517 | return fdctrl_read(opaque, (uint32_t)reg); |
62a46c61 FB |
518 | } |
519 | ||
dc6c1b37 AK |
520 | static void fdctrl_write_mem (void *opaque, target_phys_addr_t reg, |
521 | uint64_t value, unsigned size) | |
62a46c61 | 522 | { |
5dcb6b91 | 523 | fdctrl_write(opaque, (uint32_t)reg, value); |
62a46c61 FB |
524 | } |
525 | ||
dc6c1b37 AK |
526 | static const MemoryRegionOps fdctrl_mem_ops = { |
527 | .read = fdctrl_read_mem, | |
528 | .write = fdctrl_write_mem, | |
529 | .endianness = DEVICE_NATIVE_ENDIAN, | |
e80cfcfc FB |
530 | }; |
531 | ||
dc6c1b37 AK |
532 | static const MemoryRegionOps fdctrl_mem_strict_ops = { |
533 | .read = fdctrl_read_mem, | |
534 | .write = fdctrl_write_mem, | |
535 | .endianness = DEVICE_NATIVE_ENDIAN, | |
536 | .valid = { | |
537 | .min_access_size = 1, | |
538 | .max_access_size = 1, | |
539 | }, | |
7c560456 BS |
540 | }; |
541 | ||
7d905f71 JW |
542 | static bool fdrive_media_changed_needed(void *opaque) |
543 | { | |
544 | FDrive *drive = opaque; | |
545 | ||
8e49ca46 | 546 | return (drive->bs != NULL && drive->media_changed != 1); |
7d905f71 JW |
547 | } |
548 | ||
549 | static const VMStateDescription vmstate_fdrive_media_changed = { | |
550 | .name = "fdrive/media_changed", | |
551 | .version_id = 1, | |
552 | .minimum_version_id = 1, | |
553 | .minimum_version_id_old = 1, | |
7d905f71 JW |
554 | .fields = (VMStateField[]) { |
555 | VMSTATE_UINT8(media_changed, FDrive), | |
556 | VMSTATE_END_OF_LIST() | |
557 | } | |
558 | }; | |
559 | ||
844f65d6 HP |
560 | static bool fdrive_media_rate_needed(void *opaque) |
561 | { | |
562 | FDrive *drive = opaque; | |
563 | ||
564 | return drive->fdctrl->check_media_rate; | |
565 | } | |
566 | ||
567 | static const VMStateDescription vmstate_fdrive_media_rate = { | |
568 | .name = "fdrive/media_rate", | |
569 | .version_id = 1, | |
570 | .minimum_version_id = 1, | |
571 | .minimum_version_id_old = 1, | |
572 | .fields = (VMStateField[]) { | |
573 | VMSTATE_UINT8(media_rate, FDrive), | |
574 | VMSTATE_END_OF_LIST() | |
575 | } | |
576 | }; | |
577 | ||
d7a6c270 JQ |
578 | static const VMStateDescription vmstate_fdrive = { |
579 | .name = "fdrive", | |
580 | .version_id = 1, | |
581 | .minimum_version_id = 1, | |
582 | .minimum_version_id_old = 1, | |
7d905f71 | 583 | .fields = (VMStateField[]) { |
5c02c033 BS |
584 | VMSTATE_UINT8(head, FDrive), |
585 | VMSTATE_UINT8(track, FDrive), | |
586 | VMSTATE_UINT8(sect, FDrive), | |
d7a6c270 | 587 | VMSTATE_END_OF_LIST() |
7d905f71 JW |
588 | }, |
589 | .subsections = (VMStateSubsection[]) { | |
590 | { | |
591 | .vmsd = &vmstate_fdrive_media_changed, | |
592 | .needed = &fdrive_media_changed_needed, | |
844f65d6 HP |
593 | } , { |
594 | .vmsd = &vmstate_fdrive_media_rate, | |
595 | .needed = &fdrive_media_rate_needed, | |
7d905f71 JW |
596 | } , { |
597 | /* empty */ | |
598 | } | |
d7a6c270 JQ |
599 | } |
600 | }; | |
3ccacc4a | 601 | |
d4bfa4d7 | 602 | static void fdc_pre_save(void *opaque) |
3ccacc4a | 603 | { |
5c02c033 | 604 | FDCtrl *s = opaque; |
3ccacc4a | 605 | |
d7a6c270 | 606 | s->dor_vmstate = s->dor | GET_CUR_DRV(s); |
3ccacc4a BS |
607 | } |
608 | ||
e59fb374 | 609 | static int fdc_post_load(void *opaque, int version_id) |
3ccacc4a | 610 | { |
5c02c033 | 611 | FDCtrl *s = opaque; |
3ccacc4a | 612 | |
d7a6c270 JQ |
613 | SET_CUR_DRV(s, s->dor_vmstate & FD_DOR_SELMASK); |
614 | s->dor = s->dor_vmstate & ~FD_DOR_SELMASK; | |
3ccacc4a BS |
615 | return 0; |
616 | } | |
617 | ||
d7a6c270 | 618 | static const VMStateDescription vmstate_fdc = { |
aef30c3c | 619 | .name = "fdc", |
d7a6c270 JQ |
620 | .version_id = 2, |
621 | .minimum_version_id = 2, | |
622 | .minimum_version_id_old = 2, | |
623 | .pre_save = fdc_pre_save, | |
624 | .post_load = fdc_post_load, | |
625 | .fields = (VMStateField []) { | |
626 | /* Controller State */ | |
5c02c033 BS |
627 | VMSTATE_UINT8(sra, FDCtrl), |
628 | VMSTATE_UINT8(srb, FDCtrl), | |
629 | VMSTATE_UINT8(dor_vmstate, FDCtrl), | |
630 | VMSTATE_UINT8(tdr, FDCtrl), | |
631 | VMSTATE_UINT8(dsr, FDCtrl), | |
632 | VMSTATE_UINT8(msr, FDCtrl), | |
633 | VMSTATE_UINT8(status0, FDCtrl), | |
634 | VMSTATE_UINT8(status1, FDCtrl), | |
635 | VMSTATE_UINT8(status2, FDCtrl), | |
d7a6c270 | 636 | /* Command FIFO */ |
8ec68b06 BS |
637 | VMSTATE_VARRAY_INT32(fifo, FDCtrl, fifo_size, 0, vmstate_info_uint8, |
638 | uint8_t), | |
5c02c033 BS |
639 | VMSTATE_UINT32(data_pos, FDCtrl), |
640 | VMSTATE_UINT32(data_len, FDCtrl), | |
641 | VMSTATE_UINT8(data_state, FDCtrl), | |
642 | VMSTATE_UINT8(data_dir, FDCtrl), | |
643 | VMSTATE_UINT8(eot, FDCtrl), | |
d7a6c270 | 644 | /* States kept only to be returned back */ |
5c02c033 BS |
645 | VMSTATE_UINT8(timer0, FDCtrl), |
646 | VMSTATE_UINT8(timer1, FDCtrl), | |
647 | VMSTATE_UINT8(precomp_trk, FDCtrl), | |
648 | VMSTATE_UINT8(config, FDCtrl), | |
649 | VMSTATE_UINT8(lock, FDCtrl), | |
650 | VMSTATE_UINT8(pwrd, FDCtrl), | |
651 | VMSTATE_UINT8_EQUAL(num_floppies, FDCtrl), | |
652 | VMSTATE_STRUCT_ARRAY(drives, FDCtrl, MAX_FD, 1, | |
653 | vmstate_fdrive, FDrive), | |
d7a6c270 | 654 | VMSTATE_END_OF_LIST() |
78ae820c | 655 | } |
d7a6c270 | 656 | }; |
3ccacc4a | 657 | |
2be37833 | 658 | static void fdctrl_external_reset_sysbus(DeviceState *d) |
3ccacc4a | 659 | { |
5c02c033 BS |
660 | FDCtrlSysBus *sys = container_of(d, FDCtrlSysBus, busdev.qdev); |
661 | FDCtrl *s = &sys->state; | |
2be37833 BS |
662 | |
663 | fdctrl_reset(s, 0); | |
664 | } | |
665 | ||
666 | static void fdctrl_external_reset_isa(DeviceState *d) | |
667 | { | |
5c02c033 BS |
668 | FDCtrlISABus *isa = container_of(d, FDCtrlISABus, busdev.qdev); |
669 | FDCtrl *s = &isa->state; | |
3ccacc4a BS |
670 | |
671 | fdctrl_reset(s, 0); | |
672 | } | |
673 | ||
2be17ebd BS |
674 | static void fdctrl_handle_tc(void *opaque, int irq, int level) |
675 | { | |
5c02c033 | 676 | //FDCtrl *s = opaque; |
2be17ebd BS |
677 | |
678 | if (level) { | |
679 | // XXX | |
680 | FLOPPY_DPRINTF("TC pulsed\n"); | |
681 | } | |
682 | } | |
683 | ||
8977f3c1 | 684 | /* Change IRQ state */ |
5c02c033 | 685 | static void fdctrl_reset_irq(FDCtrl *fdctrl) |
8977f3c1 | 686 | { |
8c6a4d77 BS |
687 | if (!(fdctrl->sra & FD_SRA_INTPEND)) |
688 | return; | |
ed5fd2cc | 689 | FLOPPY_DPRINTF("Reset interrupt\n"); |
d537cf6c | 690 | qemu_set_irq(fdctrl->irq, 0); |
8c6a4d77 | 691 | fdctrl->sra &= ~FD_SRA_INTPEND; |
8977f3c1 FB |
692 | } |
693 | ||
5c02c033 | 694 | static void fdctrl_raise_irq(FDCtrl *fdctrl, uint8_t status0) |
8977f3c1 | 695 | { |
b9b3d225 BS |
696 | /* Sparc mutation */ |
697 | if (fdctrl->sun4m && (fdctrl->msr & FD_MSR_CMDBUSY)) { | |
698 | /* XXX: not sure */ | |
699 | fdctrl->msr &= ~FD_MSR_CMDBUSY; | |
700 | fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO; | |
77370520 | 701 | fdctrl->status0 = status0; |
4f431960 | 702 | return; |
6f7e9aec | 703 | } |
8c6a4d77 | 704 | if (!(fdctrl->sra & FD_SRA_INTPEND)) { |
d537cf6c | 705 | qemu_set_irq(fdctrl->irq, 1); |
8c6a4d77 | 706 | fdctrl->sra |= FD_SRA_INTPEND; |
8977f3c1 | 707 | } |
f2d81b33 | 708 | fdctrl->reset_sensei = 0; |
77370520 BS |
709 | fdctrl->status0 = status0; |
710 | FLOPPY_DPRINTF("Set interrupt status to 0x%02x\n", fdctrl->status0); | |
8977f3c1 FB |
711 | } |
712 | ||
4b19ec0c | 713 | /* Reset controller */ |
5c02c033 | 714 | static void fdctrl_reset(FDCtrl *fdctrl, int do_irq) |
8977f3c1 FB |
715 | { |
716 | int i; | |
717 | ||
4b19ec0c | 718 | FLOPPY_DPRINTF("reset controller\n"); |
baca51fa | 719 | fdctrl_reset_irq(fdctrl); |
4b19ec0c | 720 | /* Initialise controller */ |
8c6a4d77 BS |
721 | fdctrl->sra = 0; |
722 | fdctrl->srb = 0xc0; | |
723 | if (!fdctrl->drives[1].bs) | |
724 | fdctrl->sra |= FD_SRA_nDRV2; | |
baca51fa | 725 | fdctrl->cur_drv = 0; |
1c346df2 | 726 | fdctrl->dor = FD_DOR_nRESET; |
368df94d | 727 | fdctrl->dor |= (fdctrl->dma_chann != -1) ? FD_DOR_DMAEN : 0; |
b9b3d225 | 728 | fdctrl->msr = FD_MSR_RQM; |
8977f3c1 | 729 | /* FIFO state */ |
baca51fa FB |
730 | fdctrl->data_pos = 0; |
731 | fdctrl->data_len = 0; | |
b9b3d225 | 732 | fdctrl->data_state = 0; |
baca51fa | 733 | fdctrl->data_dir = FD_DIR_WRITE; |
8977f3c1 | 734 | for (i = 0; i < MAX_FD; i++) |
1c346df2 | 735 | fd_recalibrate(&fdctrl->drives[i]); |
baca51fa | 736 | fdctrl_reset_fifo(fdctrl); |
77370520 | 737 | if (do_irq) { |
9fea808a | 738 | fdctrl_raise_irq(fdctrl, FD_SR0_RDYCHG); |
f2d81b33 | 739 | fdctrl->reset_sensei = FD_RESET_SENSEI_COUNT; |
77370520 | 740 | } |
baca51fa FB |
741 | } |
742 | ||
5c02c033 | 743 | static inline FDrive *drv0(FDCtrl *fdctrl) |
baca51fa | 744 | { |
46d3233b | 745 | return &fdctrl->drives[(fdctrl->tdr & FD_TDR_BOOTSEL) >> 2]; |
baca51fa FB |
746 | } |
747 | ||
5c02c033 | 748 | static inline FDrive *drv1(FDCtrl *fdctrl) |
baca51fa | 749 | { |
46d3233b BS |
750 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (1 << 2)) |
751 | return &fdctrl->drives[1]; | |
752 | else | |
753 | return &fdctrl->drives[0]; | |
baca51fa FB |
754 | } |
755 | ||
78ae820c | 756 | #if MAX_FD == 4 |
5c02c033 | 757 | static inline FDrive *drv2(FDCtrl *fdctrl) |
78ae820c BS |
758 | { |
759 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (2 << 2)) | |
760 | return &fdctrl->drives[2]; | |
761 | else | |
762 | return &fdctrl->drives[1]; | |
763 | } | |
764 | ||
5c02c033 | 765 | static inline FDrive *drv3(FDCtrl *fdctrl) |
78ae820c BS |
766 | { |
767 | if ((fdctrl->tdr & FD_TDR_BOOTSEL) < (3 << 2)) | |
768 | return &fdctrl->drives[3]; | |
769 | else | |
770 | return &fdctrl->drives[2]; | |
771 | } | |
772 | #endif | |
773 | ||
5c02c033 | 774 | static FDrive *get_cur_drv(FDCtrl *fdctrl) |
baca51fa | 775 | { |
78ae820c BS |
776 | switch (fdctrl->cur_drv) { |
777 | case 0: return drv0(fdctrl); | |
778 | case 1: return drv1(fdctrl); | |
779 | #if MAX_FD == 4 | |
780 | case 2: return drv2(fdctrl); | |
781 | case 3: return drv3(fdctrl); | |
782 | #endif | |
783 | default: return NULL; | |
784 | } | |
8977f3c1 FB |
785 | } |
786 | ||
8c6a4d77 | 787 | /* Status A register : 0x00 (read-only) */ |
5c02c033 | 788 | static uint32_t fdctrl_read_statusA(FDCtrl *fdctrl) |
8c6a4d77 BS |
789 | { |
790 | uint32_t retval = fdctrl->sra; | |
791 | ||
792 | FLOPPY_DPRINTF("status register A: 0x%02x\n", retval); | |
793 | ||
794 | return retval; | |
795 | } | |
796 | ||
8977f3c1 | 797 | /* Status B register : 0x01 (read-only) */ |
5c02c033 | 798 | static uint32_t fdctrl_read_statusB(FDCtrl *fdctrl) |
8977f3c1 | 799 | { |
8c6a4d77 BS |
800 | uint32_t retval = fdctrl->srb; |
801 | ||
802 | FLOPPY_DPRINTF("status register B: 0x%02x\n", retval); | |
803 | ||
804 | return retval; | |
8977f3c1 FB |
805 | } |
806 | ||
807 | /* Digital output register : 0x02 */ | |
5c02c033 | 808 | static uint32_t fdctrl_read_dor(FDCtrl *fdctrl) |
8977f3c1 | 809 | { |
1c346df2 | 810 | uint32_t retval = fdctrl->dor; |
8977f3c1 | 811 | |
8977f3c1 | 812 | /* Selected drive */ |
baca51fa | 813 | retval |= fdctrl->cur_drv; |
8977f3c1 FB |
814 | FLOPPY_DPRINTF("digital output register: 0x%02x\n", retval); |
815 | ||
816 | return retval; | |
817 | } | |
818 | ||
5c02c033 | 819 | static void fdctrl_write_dor(FDCtrl *fdctrl, uint32_t value) |
8977f3c1 | 820 | { |
8977f3c1 | 821 | FLOPPY_DPRINTF("digital output register set to 0x%02x\n", value); |
8c6a4d77 BS |
822 | |
823 | /* Motors */ | |
824 | if (value & FD_DOR_MOTEN0) | |
825 | fdctrl->srb |= FD_SRB_MTR0; | |
826 | else | |
827 | fdctrl->srb &= ~FD_SRB_MTR0; | |
828 | if (value & FD_DOR_MOTEN1) | |
829 | fdctrl->srb |= FD_SRB_MTR1; | |
830 | else | |
831 | fdctrl->srb &= ~FD_SRB_MTR1; | |
832 | ||
833 | /* Drive */ | |
834 | if (value & 1) | |
835 | fdctrl->srb |= FD_SRB_DR0; | |
836 | else | |
837 | fdctrl->srb &= ~FD_SRB_DR0; | |
838 | ||
8977f3c1 | 839 | /* Reset */ |
9fea808a | 840 | if (!(value & FD_DOR_nRESET)) { |
1c346df2 | 841 | if (fdctrl->dor & FD_DOR_nRESET) { |
4b19ec0c | 842 | FLOPPY_DPRINTF("controller enter RESET state\n"); |
8977f3c1 FB |
843 | } |
844 | } else { | |
1c346df2 | 845 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4b19ec0c | 846 | FLOPPY_DPRINTF("controller out of RESET state\n"); |
fb6cf1d0 | 847 | fdctrl_reset(fdctrl, 1); |
b9b3d225 | 848 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
8977f3c1 FB |
849 | } |
850 | } | |
851 | /* Selected drive */ | |
9fea808a | 852 | fdctrl->cur_drv = value & FD_DOR_SELMASK; |
368df94d BS |
853 | |
854 | fdctrl->dor = value; | |
8977f3c1 FB |
855 | } |
856 | ||
857 | /* Tape drive register : 0x03 */ | |
5c02c033 | 858 | static uint32_t fdctrl_read_tape(FDCtrl *fdctrl) |
8977f3c1 | 859 | { |
46d3233b | 860 | uint32_t retval = fdctrl->tdr; |
8977f3c1 | 861 | |
8977f3c1 FB |
862 | FLOPPY_DPRINTF("tape drive register: 0x%02x\n", retval); |
863 | ||
864 | return retval; | |
865 | } | |
866 | ||
5c02c033 | 867 | static void fdctrl_write_tape(FDCtrl *fdctrl, uint32_t value) |
8977f3c1 | 868 | { |
8977f3c1 | 869 | /* Reset mode */ |
1c346df2 | 870 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4b19ec0c | 871 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
8977f3c1 FB |
872 | return; |
873 | } | |
874 | FLOPPY_DPRINTF("tape drive register set to 0x%02x\n", value); | |
875 | /* Disk boot selection indicator */ | |
46d3233b | 876 | fdctrl->tdr = value & FD_TDR_BOOTSEL; |
8977f3c1 FB |
877 | /* Tape indicators: never allow */ |
878 | } | |
879 | ||
880 | /* Main status register : 0x04 (read) */ | |
5c02c033 | 881 | static uint32_t fdctrl_read_main_status(FDCtrl *fdctrl) |
8977f3c1 | 882 | { |
b9b3d225 | 883 | uint32_t retval = fdctrl->msr; |
8977f3c1 | 884 | |
b9b3d225 | 885 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
1c346df2 | 886 | fdctrl->dor |= FD_DOR_nRESET; |
b9b3d225 | 887 | |
82407d1a AT |
888 | /* Sparc mutation */ |
889 | if (fdctrl->sun4m) { | |
890 | retval |= FD_MSR_DIO; | |
891 | fdctrl_reset_irq(fdctrl); | |
892 | }; | |
893 | ||
8977f3c1 FB |
894 | FLOPPY_DPRINTF("main status register: 0x%02x\n", retval); |
895 | ||
896 | return retval; | |
897 | } | |
898 | ||
899 | /* Data select rate register : 0x04 (write) */ | |
5c02c033 | 900 | static void fdctrl_write_rate(FDCtrl *fdctrl, uint32_t value) |
8977f3c1 | 901 | { |
8977f3c1 | 902 | /* Reset mode */ |
1c346df2 | 903 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4f431960 JM |
904 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
905 | return; | |
906 | } | |
8977f3c1 FB |
907 | FLOPPY_DPRINTF("select rate register set to 0x%02x\n", value); |
908 | /* Reset: autoclear */ | |
9fea808a | 909 | if (value & FD_DSR_SWRESET) { |
1c346df2 | 910 | fdctrl->dor &= ~FD_DOR_nRESET; |
baca51fa | 911 | fdctrl_reset(fdctrl, 1); |
1c346df2 | 912 | fdctrl->dor |= FD_DOR_nRESET; |
8977f3c1 | 913 | } |
9fea808a | 914 | if (value & FD_DSR_PWRDOWN) { |
baca51fa | 915 | fdctrl_reset(fdctrl, 1); |
8977f3c1 | 916 | } |
b9b3d225 | 917 | fdctrl->dsr = value; |
8977f3c1 FB |
918 | } |
919 | ||
a758f8f4 HP |
920 | /* Configuration control register: 0x07 (write) */ |
921 | static void fdctrl_write_ccr(FDCtrl *fdctrl, uint32_t value) | |
922 | { | |
923 | /* Reset mode */ | |
924 | if (!(fdctrl->dor & FD_DOR_nRESET)) { | |
925 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); | |
926 | return; | |
927 | } | |
928 | FLOPPY_DPRINTF("configuration control register set to 0x%02x\n", value); | |
929 | ||
930 | /* Only the rate selection bits used in AT mode, and we | |
931 | * store those in the DSR. | |
932 | */ | |
933 | fdctrl->dsr = (fdctrl->dsr & ~FD_DSR_DRATEMASK) | | |
934 | (value & FD_DSR_DRATEMASK); | |
935 | } | |
936 | ||
5c02c033 | 937 | static int fdctrl_media_changed(FDrive *drv) |
ea185bbd FB |
938 | { |
939 | int ret; | |
4f431960 | 940 | |
5fafdf24 | 941 | if (!drv->bs) |
ea185bbd | 942 | return 0; |
18d90055 MA |
943 | if (drv->media_changed) { |
944 | drv->media_changed = 0; | |
945 | ret = 1; | |
946 | } else { | |
947 | ret = bdrv_media_changed(drv->bs); | |
948 | if (ret < 0) { | |
949 | ret = 0; /* we don't know, assume no */ | |
950 | } | |
8e49ca46 | 951 | } |
ea185bbd FB |
952 | if (ret) { |
953 | fd_revalidate(drv); | |
954 | } | |
955 | return ret; | |
956 | } | |
957 | ||
8977f3c1 | 958 | /* Digital input register : 0x07 (read-only) */ |
5c02c033 | 959 | static uint32_t fdctrl_read_dir(FDCtrl *fdctrl) |
8977f3c1 | 960 | { |
8977f3c1 FB |
961 | uint32_t retval = 0; |
962 | ||
a2df5fa3 | 963 | if (fdctrl_media_changed(get_cur_drv(fdctrl))) { |
9fea808a | 964 | retval |= FD_DIR_DSKCHG; |
a2df5fa3 | 965 | } |
3c83eb4f | 966 | if (retval != 0) { |
baca51fa | 967 | FLOPPY_DPRINTF("Floppy digital input register: 0x%02x\n", retval); |
3c83eb4f | 968 | } |
8977f3c1 FB |
969 | |
970 | return retval; | |
971 | } | |
972 | ||
973 | /* FIFO state control */ | |
5c02c033 | 974 | static void fdctrl_reset_fifo(FDCtrl *fdctrl) |
8977f3c1 | 975 | { |
baca51fa FB |
976 | fdctrl->data_dir = FD_DIR_WRITE; |
977 | fdctrl->data_pos = 0; | |
b9b3d225 | 978 | fdctrl->msr &= ~(FD_MSR_CMDBUSY | FD_MSR_DIO); |
8977f3c1 FB |
979 | } |
980 | ||
981 | /* Set FIFO status for the host to read */ | |
5c02c033 | 982 | static void fdctrl_set_fifo(FDCtrl *fdctrl, int fifo_len, int do_irq) |
8977f3c1 | 983 | { |
baca51fa FB |
984 | fdctrl->data_dir = FD_DIR_READ; |
985 | fdctrl->data_len = fifo_len; | |
986 | fdctrl->data_pos = 0; | |
b9b3d225 | 987 | fdctrl->msr |= FD_MSR_CMDBUSY | FD_MSR_RQM | FD_MSR_DIO; |
8977f3c1 | 988 | if (do_irq) |
baca51fa | 989 | fdctrl_raise_irq(fdctrl, 0x00); |
8977f3c1 FB |
990 | } |
991 | ||
992 | /* Set an error: unimplemented/unknown command */ | |
5c02c033 | 993 | static void fdctrl_unimplemented(FDCtrl *fdctrl, int direction) |
8977f3c1 | 994 | { |
77370520 | 995 | FLOPPY_ERROR("unimplemented command 0x%02x\n", fdctrl->fifo[0]); |
9fea808a | 996 | fdctrl->fifo[0] = FD_SR0_INVCMD; |
baca51fa | 997 | fdctrl_set_fifo(fdctrl, 1, 0); |
8977f3c1 FB |
998 | } |
999 | ||
746d6de7 | 1000 | /* Seek to next sector */ |
5c02c033 | 1001 | static int fdctrl_seek_to_next_sect(FDCtrl *fdctrl, FDrive *cur_drv) |
746d6de7 BS |
1002 | { |
1003 | FLOPPY_DPRINTF("seek to next sector (%d %02x %02x => %d)\n", | |
1004 | cur_drv->head, cur_drv->track, cur_drv->sect, | |
1005 | fd_sector(cur_drv)); | |
1006 | /* XXX: cur_drv->sect >= cur_drv->last_sect should be an | |
1007 | error in fact */ | |
1008 | if (cur_drv->sect >= cur_drv->last_sect || | |
1009 | cur_drv->sect == fdctrl->eot) { | |
1010 | cur_drv->sect = 1; | |
1011 | if (FD_MULTI_TRACK(fdctrl->data_state)) { | |
1012 | if (cur_drv->head == 0 && | |
1013 | (cur_drv->flags & FDISK_DBL_SIDES) != 0) { | |
1014 | cur_drv->head = 1; | |
1015 | } else { | |
1016 | cur_drv->head = 0; | |
1017 | cur_drv->track++; | |
1018 | if ((cur_drv->flags & FDISK_DBL_SIDES) == 0) | |
1019 | return 0; | |
1020 | } | |
1021 | } else { | |
1022 | cur_drv->track++; | |
1023 | return 0; | |
1024 | } | |
1025 | FLOPPY_DPRINTF("seek to next track (%d %02x %02x => %d)\n", | |
1026 | cur_drv->head, cur_drv->track, | |
1027 | cur_drv->sect, fd_sector(cur_drv)); | |
1028 | } else { | |
1029 | cur_drv->sect++; | |
1030 | } | |
1031 | return 1; | |
1032 | } | |
1033 | ||
8977f3c1 | 1034 | /* Callback for transfer end (stop or abort) */ |
5c02c033 BS |
1035 | static void fdctrl_stop_transfer(FDCtrl *fdctrl, uint8_t status0, |
1036 | uint8_t status1, uint8_t status2) | |
8977f3c1 | 1037 | { |
5c02c033 | 1038 | FDrive *cur_drv; |
8977f3c1 | 1039 | |
baca51fa | 1040 | cur_drv = get_cur_drv(fdctrl); |
8977f3c1 FB |
1041 | FLOPPY_DPRINTF("transfer status: %02x %02x %02x (%02x)\n", |
1042 | status0, status1, status2, | |
cefec4f5 BS |
1043 | status0 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl)); |
1044 | fdctrl->fifo[0] = status0 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl); | |
baca51fa FB |
1045 | fdctrl->fifo[1] = status1; |
1046 | fdctrl->fifo[2] = status2; | |
1047 | fdctrl->fifo[3] = cur_drv->track; | |
1048 | fdctrl->fifo[4] = cur_drv->head; | |
1049 | fdctrl->fifo[5] = cur_drv->sect; | |
1050 | fdctrl->fifo[6] = FD_SECTOR_SC; | |
1051 | fdctrl->data_dir = FD_DIR_READ; | |
368df94d | 1052 | if (!(fdctrl->msr & FD_MSR_NONDMA)) { |
baca51fa | 1053 | DMA_release_DREQ(fdctrl->dma_chann); |
ed5fd2cc | 1054 | } |
b9b3d225 | 1055 | fdctrl->msr |= FD_MSR_RQM | FD_MSR_DIO; |
368df94d | 1056 | fdctrl->msr &= ~FD_MSR_NONDMA; |
baca51fa | 1057 | fdctrl_set_fifo(fdctrl, 7, 1); |
8977f3c1 FB |
1058 | } |
1059 | ||
1060 | /* Prepare a data transfer (either DMA or FIFO) */ | |
5c02c033 | 1061 | static void fdctrl_start_transfer(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1062 | { |
5c02c033 | 1063 | FDrive *cur_drv; |
8977f3c1 | 1064 | uint8_t kh, kt, ks; |
77370520 | 1065 | int did_seek = 0; |
8977f3c1 | 1066 | |
cefec4f5 | 1067 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
baca51fa FB |
1068 | cur_drv = get_cur_drv(fdctrl); |
1069 | kt = fdctrl->fifo[2]; | |
1070 | kh = fdctrl->fifo[3]; | |
1071 | ks = fdctrl->fifo[4]; | |
4b19ec0c | 1072 | FLOPPY_DPRINTF("Start transfer at %d %d %02x %02x (%d)\n", |
cefec4f5 | 1073 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
08388273 HP |
1074 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
1075 | NUM_SIDES(cur_drv))); | |
77370520 | 1076 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
8977f3c1 FB |
1077 | case 2: |
1078 | /* sect too big */ | |
9fea808a | 1079 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1080 | fdctrl->fifo[3] = kt; |
1081 | fdctrl->fifo[4] = kh; | |
1082 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1083 | return; |
1084 | case 3: | |
1085 | /* track too big */ | |
77370520 | 1086 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
baca51fa FB |
1087 | fdctrl->fifo[3] = kt; |
1088 | fdctrl->fifo[4] = kh; | |
1089 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1090 | return; |
1091 | case 4: | |
1092 | /* No seek enabled */ | |
9fea808a | 1093 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1094 | fdctrl->fifo[3] = kt; |
1095 | fdctrl->fifo[4] = kh; | |
1096 | fdctrl->fifo[5] = ks; | |
8977f3c1 FB |
1097 | return; |
1098 | case 1: | |
1099 | did_seek = 1; | |
1100 | break; | |
1101 | default: | |
1102 | break; | |
1103 | } | |
b9b3d225 | 1104 | |
844f65d6 HP |
1105 | /* Check the data rate. If the programmed data rate does not match |
1106 | * the currently inserted medium, the operation has to fail. */ | |
1107 | if (fdctrl->check_media_rate && | |
1108 | (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { | |
1109 | FLOPPY_DPRINTF("data rate mismatch (fdc=%d, media=%d)\n", | |
1110 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); | |
1111 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); | |
1112 | fdctrl->fifo[3] = kt; | |
1113 | fdctrl->fifo[4] = kh; | |
1114 | fdctrl->fifo[5] = ks; | |
1115 | return; | |
1116 | } | |
1117 | ||
8977f3c1 | 1118 | /* Set the FIFO state */ |
baca51fa FB |
1119 | fdctrl->data_dir = direction; |
1120 | fdctrl->data_pos = 0; | |
b9b3d225 | 1121 | fdctrl->msr |= FD_MSR_CMDBUSY; |
baca51fa FB |
1122 | if (fdctrl->fifo[0] & 0x80) |
1123 | fdctrl->data_state |= FD_STATE_MULTI; | |
1124 | else | |
1125 | fdctrl->data_state &= ~FD_STATE_MULTI; | |
8977f3c1 | 1126 | if (did_seek) |
baca51fa FB |
1127 | fdctrl->data_state |= FD_STATE_SEEK; |
1128 | else | |
1129 | fdctrl->data_state &= ~FD_STATE_SEEK; | |
1130 | if (fdctrl->fifo[5] == 00) { | |
1131 | fdctrl->data_len = fdctrl->fifo[8]; | |
1132 | } else { | |
4f431960 | 1133 | int tmp; |
3bcb80f1 | 1134 | fdctrl->data_len = 128 << (fdctrl->fifo[5] > 7 ? 7 : fdctrl->fifo[5]); |
771effeb | 1135 | tmp = (fdctrl->fifo[6] - ks + 1); |
baca51fa | 1136 | if (fdctrl->fifo[0] & 0x80) |
771effeb | 1137 | tmp += fdctrl->fifo[6]; |
4f431960 | 1138 | fdctrl->data_len *= tmp; |
baca51fa | 1139 | } |
890fa6be | 1140 | fdctrl->eot = fdctrl->fifo[6]; |
368df94d | 1141 | if (fdctrl->dor & FD_DOR_DMAEN) { |
8977f3c1 FB |
1142 | int dma_mode; |
1143 | /* DMA transfer are enabled. Check if DMA channel is well programmed */ | |
baca51fa | 1144 | dma_mode = DMA_get_channel_mode(fdctrl->dma_chann); |
8977f3c1 | 1145 | dma_mode = (dma_mode >> 2) & 3; |
baca51fa | 1146 | FLOPPY_DPRINTF("dma_mode=%d direction=%d (%d - %d)\n", |
4f431960 | 1147 | dma_mode, direction, |
baca51fa | 1148 | (128 << fdctrl->fifo[5]) * |
4f431960 | 1149 | (cur_drv->last_sect - ks + 1), fdctrl->data_len); |
8977f3c1 FB |
1150 | if (((direction == FD_DIR_SCANE || direction == FD_DIR_SCANL || |
1151 | direction == FD_DIR_SCANH) && dma_mode == 0) || | |
1152 | (direction == FD_DIR_WRITE && dma_mode == 2) || | |
1153 | (direction == FD_DIR_READ && dma_mode == 1)) { | |
1154 | /* No access is allowed until DMA transfer has completed */ | |
b9b3d225 | 1155 | fdctrl->msr &= ~FD_MSR_RQM; |
4b19ec0c | 1156 | /* Now, we just have to wait for the DMA controller to |
8977f3c1 FB |
1157 | * recall us... |
1158 | */ | |
baca51fa FB |
1159 | DMA_hold_DREQ(fdctrl->dma_chann); |
1160 | DMA_schedule(fdctrl->dma_chann); | |
8977f3c1 | 1161 | return; |
baca51fa | 1162 | } else { |
4f431960 | 1163 | FLOPPY_ERROR("dma_mode=%d direction=%d\n", dma_mode, direction); |
8977f3c1 FB |
1164 | } |
1165 | } | |
1166 | FLOPPY_DPRINTF("start non-DMA transfer\n"); | |
368df94d | 1167 | fdctrl->msr |= FD_MSR_NONDMA; |
b9b3d225 BS |
1168 | if (direction != FD_DIR_WRITE) |
1169 | fdctrl->msr |= FD_MSR_DIO; | |
8977f3c1 | 1170 | /* IO based transfer: calculate len */ |
baca51fa | 1171 | fdctrl_raise_irq(fdctrl, 0x00); |
8977f3c1 FB |
1172 | |
1173 | return; | |
1174 | } | |
1175 | ||
1176 | /* Prepare a transfer of deleted data */ | |
5c02c033 | 1177 | static void fdctrl_start_transfer_del(FDCtrl *fdctrl, int direction) |
8977f3c1 | 1178 | { |
77370520 BS |
1179 | FLOPPY_ERROR("fdctrl_start_transfer_del() unimplemented\n"); |
1180 | ||
8977f3c1 FB |
1181 | /* We don't handle deleted data, |
1182 | * so we don't return *ANYTHING* | |
1183 | */ | |
9fea808a | 1184 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
8977f3c1 FB |
1185 | } |
1186 | ||
1187 | /* handlers for DMA transfers */ | |
85571bc7 FB |
1188 | static int fdctrl_transfer_handler (void *opaque, int nchan, |
1189 | int dma_pos, int dma_len) | |
8977f3c1 | 1190 | { |
5c02c033 BS |
1191 | FDCtrl *fdctrl; |
1192 | FDrive *cur_drv; | |
baca51fa | 1193 | int len, start_pos, rel_pos; |
8977f3c1 FB |
1194 | uint8_t status0 = 0x00, status1 = 0x00, status2 = 0x00; |
1195 | ||
baca51fa | 1196 | fdctrl = opaque; |
b9b3d225 | 1197 | if (fdctrl->msr & FD_MSR_RQM) { |
8977f3c1 FB |
1198 | FLOPPY_DPRINTF("Not in DMA transfer mode !\n"); |
1199 | return 0; | |
1200 | } | |
baca51fa FB |
1201 | cur_drv = get_cur_drv(fdctrl); |
1202 | if (fdctrl->data_dir == FD_DIR_SCANE || fdctrl->data_dir == FD_DIR_SCANL || | |
1203 | fdctrl->data_dir == FD_DIR_SCANH) | |
77370520 | 1204 | status2 = FD_SR2_SNS; |
85571bc7 FB |
1205 | if (dma_len > fdctrl->data_len) |
1206 | dma_len = fdctrl->data_len; | |
890fa6be | 1207 | if (cur_drv->bs == NULL) { |
4f431960 | 1208 | if (fdctrl->data_dir == FD_DIR_WRITE) |
9fea808a | 1209 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
4f431960 | 1210 | else |
9fea808a | 1211 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
4f431960 | 1212 | len = 0; |
890fa6be FB |
1213 | goto transfer_error; |
1214 | } | |
baca51fa | 1215 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; |
85571bc7 FB |
1216 | for (start_pos = fdctrl->data_pos; fdctrl->data_pos < dma_len;) { |
1217 | len = dma_len - fdctrl->data_pos; | |
baca51fa FB |
1218 | if (len + rel_pos > FD_SECTOR_LEN) |
1219 | len = FD_SECTOR_LEN - rel_pos; | |
6f7e9aec FB |
1220 | FLOPPY_DPRINTF("copy %d bytes (%d %d %d) %d pos %d %02x " |
1221 | "(%d-0x%08x 0x%08x)\n", len, dma_len, fdctrl->data_pos, | |
cefec4f5 | 1222 | fdctrl->data_len, GET_CUR_DRV(fdctrl), cur_drv->head, |
baca51fa | 1223 | cur_drv->track, cur_drv->sect, fd_sector(cur_drv), |
9fea808a | 1224 | fd_sector(cur_drv) * FD_SECTOR_LEN); |
baca51fa | 1225 | if (fdctrl->data_dir != FD_DIR_WRITE || |
4f431960 | 1226 | len < FD_SECTOR_LEN || rel_pos != 0) { |
baca51fa FB |
1227 | /* READ & SCAN commands and realign to a sector for WRITE */ |
1228 | if (bdrv_read(cur_drv->bs, fd_sector(cur_drv), | |
4f431960 | 1229 | fdctrl->fifo, 1) < 0) { |
8977f3c1 FB |
1230 | FLOPPY_DPRINTF("Floppy: error getting sector %d\n", |
1231 | fd_sector(cur_drv)); | |
1232 | /* Sure, image size is too small... */ | |
baca51fa | 1233 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); |
8977f3c1 | 1234 | } |
890fa6be | 1235 | } |
4f431960 JM |
1236 | switch (fdctrl->data_dir) { |
1237 | case FD_DIR_READ: | |
1238 | /* READ commands */ | |
85571bc7 FB |
1239 | DMA_write_memory (nchan, fdctrl->fifo + rel_pos, |
1240 | fdctrl->data_pos, len); | |
4f431960 JM |
1241 | break; |
1242 | case FD_DIR_WRITE: | |
baca51fa | 1243 | /* WRITE commands */ |
8510854e HP |
1244 | if (cur_drv->ro) { |
1245 | /* Handle readonly medium early, no need to do DMA, touch the | |
1246 | * LED or attempt any writes. A real floppy doesn't attempt | |
1247 | * to write to readonly media either. */ | |
1248 | fdctrl_stop_transfer(fdctrl, | |
1249 | FD_SR0_ABNTERM | FD_SR0_SEEK, FD_SR1_NW, | |
1250 | 0x00); | |
1251 | goto transfer_error; | |
1252 | } | |
1253 | ||
85571bc7 FB |
1254 | DMA_read_memory (nchan, fdctrl->fifo + rel_pos, |
1255 | fdctrl->data_pos, len); | |
baca51fa | 1256 | if (bdrv_write(cur_drv->bs, fd_sector(cur_drv), |
4f431960 | 1257 | fdctrl->fifo, 1) < 0) { |
77370520 | 1258 | FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv)); |
9fea808a | 1259 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
baca51fa | 1260 | goto transfer_error; |
890fa6be | 1261 | } |
4f431960 JM |
1262 | break; |
1263 | default: | |
1264 | /* SCAN commands */ | |
baca51fa | 1265 | { |
4f431960 | 1266 | uint8_t tmpbuf[FD_SECTOR_LEN]; |
baca51fa | 1267 | int ret; |
85571bc7 | 1268 | DMA_read_memory (nchan, tmpbuf, fdctrl->data_pos, len); |
baca51fa | 1269 | ret = memcmp(tmpbuf, fdctrl->fifo + rel_pos, len); |
8977f3c1 | 1270 | if (ret == 0) { |
77370520 | 1271 | status2 = FD_SR2_SEH; |
8977f3c1 FB |
1272 | goto end_transfer; |
1273 | } | |
baca51fa FB |
1274 | if ((ret < 0 && fdctrl->data_dir == FD_DIR_SCANL) || |
1275 | (ret > 0 && fdctrl->data_dir == FD_DIR_SCANH)) { | |
8977f3c1 FB |
1276 | status2 = 0x00; |
1277 | goto end_transfer; | |
1278 | } | |
1279 | } | |
4f431960 | 1280 | break; |
8977f3c1 | 1281 | } |
4f431960 JM |
1282 | fdctrl->data_pos += len; |
1283 | rel_pos = fdctrl->data_pos % FD_SECTOR_LEN; | |
baca51fa | 1284 | if (rel_pos == 0) { |
8977f3c1 | 1285 | /* Seek to next sector */ |
746d6de7 BS |
1286 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) |
1287 | break; | |
8977f3c1 FB |
1288 | } |
1289 | } | |
4f431960 | 1290 | end_transfer: |
baca51fa FB |
1291 | len = fdctrl->data_pos - start_pos; |
1292 | FLOPPY_DPRINTF("end transfer %d %d %d\n", | |
4f431960 | 1293 | fdctrl->data_pos, len, fdctrl->data_len); |
baca51fa FB |
1294 | if (fdctrl->data_dir == FD_DIR_SCANE || |
1295 | fdctrl->data_dir == FD_DIR_SCANL || | |
1296 | fdctrl->data_dir == FD_DIR_SCANH) | |
77370520 | 1297 | status2 = FD_SR2_SEH; |
baca51fa | 1298 | if (FD_DID_SEEK(fdctrl->data_state)) |
9fea808a | 1299 | status0 |= FD_SR0_SEEK; |
baca51fa | 1300 | fdctrl->data_len -= len; |
890fa6be | 1301 | fdctrl_stop_transfer(fdctrl, status0, status1, status2); |
4f431960 | 1302 | transfer_error: |
8977f3c1 | 1303 | |
baca51fa | 1304 | return len; |
8977f3c1 FB |
1305 | } |
1306 | ||
8977f3c1 | 1307 | /* Data register : 0x05 */ |
5c02c033 | 1308 | static uint32_t fdctrl_read_data(FDCtrl *fdctrl) |
8977f3c1 | 1309 | { |
5c02c033 | 1310 | FDrive *cur_drv; |
8977f3c1 | 1311 | uint32_t retval = 0; |
746d6de7 | 1312 | int pos; |
8977f3c1 | 1313 | |
baca51fa | 1314 | cur_drv = get_cur_drv(fdctrl); |
b9b3d225 BS |
1315 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
1316 | if (!(fdctrl->msr & FD_MSR_RQM) || !(fdctrl->msr & FD_MSR_DIO)) { | |
1317 | FLOPPY_ERROR("controller not ready for reading\n"); | |
8977f3c1 FB |
1318 | return 0; |
1319 | } | |
baca51fa | 1320 | pos = fdctrl->data_pos; |
368df94d | 1321 | if (fdctrl->msr & FD_MSR_NONDMA) { |
8977f3c1 FB |
1322 | pos %= FD_SECTOR_LEN; |
1323 | if (pos == 0) { | |
746d6de7 BS |
1324 | if (fdctrl->data_pos != 0) |
1325 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { | |
1326 | FLOPPY_DPRINTF("error seeking to next sector %d\n", | |
1327 | fd_sector(cur_drv)); | |
1328 | return 0; | |
1329 | } | |
77370520 BS |
1330 | if (bdrv_read(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) { |
1331 | FLOPPY_DPRINTF("error getting sector %d\n", | |
1332 | fd_sector(cur_drv)); | |
1333 | /* Sure, image size is too small... */ | |
1334 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); | |
1335 | } | |
8977f3c1 FB |
1336 | } |
1337 | } | |
baca51fa FB |
1338 | retval = fdctrl->fifo[pos]; |
1339 | if (++fdctrl->data_pos == fdctrl->data_len) { | |
1340 | fdctrl->data_pos = 0; | |
890fa6be | 1341 | /* Switch from transfer mode to status mode |
8977f3c1 FB |
1342 | * then from status mode to command mode |
1343 | */ | |
368df94d | 1344 | if (fdctrl->msr & FD_MSR_NONDMA) { |
9fea808a | 1345 | fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00); |
ed5fd2cc | 1346 | } else { |
baca51fa | 1347 | fdctrl_reset_fifo(fdctrl); |
ed5fd2cc FB |
1348 | fdctrl_reset_irq(fdctrl); |
1349 | } | |
8977f3c1 FB |
1350 | } |
1351 | FLOPPY_DPRINTF("data register: 0x%02x\n", retval); | |
1352 | ||
1353 | return retval; | |
1354 | } | |
1355 | ||
5c02c033 | 1356 | static void fdctrl_format_sector(FDCtrl *fdctrl) |
8977f3c1 | 1357 | { |
5c02c033 | 1358 | FDrive *cur_drv; |
baca51fa | 1359 | uint8_t kh, kt, ks; |
8977f3c1 | 1360 | |
cefec4f5 | 1361 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
baca51fa FB |
1362 | cur_drv = get_cur_drv(fdctrl); |
1363 | kt = fdctrl->fifo[6]; | |
1364 | kh = fdctrl->fifo[7]; | |
1365 | ks = fdctrl->fifo[8]; | |
1366 | FLOPPY_DPRINTF("format sector at %d %d %02x %02x (%d)\n", | |
cefec4f5 | 1367 | GET_CUR_DRV(fdctrl), kh, kt, ks, |
08388273 HP |
1368 | fd_sector_calc(kh, kt, ks, cur_drv->last_sect, |
1369 | NUM_SIDES(cur_drv))); | |
9fea808a | 1370 | switch (fd_seek(cur_drv, kh, kt, ks, fdctrl->config & FD_CONFIG_EIS)) { |
baca51fa FB |
1371 | case 2: |
1372 | /* sect too big */ | |
9fea808a | 1373 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1374 | fdctrl->fifo[3] = kt; |
1375 | fdctrl->fifo[4] = kh; | |
1376 | fdctrl->fifo[5] = ks; | |
1377 | return; | |
1378 | case 3: | |
1379 | /* track too big */ | |
77370520 | 1380 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_EC, 0x00); |
baca51fa FB |
1381 | fdctrl->fifo[3] = kt; |
1382 | fdctrl->fifo[4] = kh; | |
1383 | fdctrl->fifo[5] = ks; | |
1384 | return; | |
1385 | case 4: | |
1386 | /* No seek enabled */ | |
9fea808a | 1387 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, 0x00, 0x00); |
baca51fa FB |
1388 | fdctrl->fifo[3] = kt; |
1389 | fdctrl->fifo[4] = kh; | |
1390 | fdctrl->fifo[5] = ks; | |
1391 | return; | |
1392 | case 1: | |
baca51fa FB |
1393 | fdctrl->data_state |= FD_STATE_SEEK; |
1394 | break; | |
1395 | default: | |
1396 | break; | |
1397 | } | |
1398 | memset(fdctrl->fifo, 0, FD_SECTOR_LEN); | |
1399 | if (cur_drv->bs == NULL || | |
1400 | bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) { | |
37a4c539 | 1401 | FLOPPY_ERROR("formatting sector %d\n", fd_sector(cur_drv)); |
9fea808a | 1402 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM | FD_SR0_SEEK, 0x00, 0x00); |
baca51fa | 1403 | } else { |
4f431960 JM |
1404 | if (cur_drv->sect == cur_drv->last_sect) { |
1405 | fdctrl->data_state &= ~FD_STATE_FORMAT; | |
1406 | /* Last sector done */ | |
1407 | if (FD_DID_SEEK(fdctrl->data_state)) | |
9fea808a | 1408 | fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00); |
4f431960 JM |
1409 | else |
1410 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); | |
1411 | } else { | |
1412 | /* More to do */ | |
1413 | fdctrl->data_pos = 0; | |
1414 | fdctrl->data_len = 4; | |
1415 | } | |
baca51fa FB |
1416 | } |
1417 | } | |
1418 | ||
5c02c033 | 1419 | static void fdctrl_handle_lock(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1420 | { |
1421 | fdctrl->lock = (fdctrl->fifo[0] & 0x80) ? 1 : 0; | |
1422 | fdctrl->fifo[0] = fdctrl->lock << 4; | |
a005186c | 1423 | fdctrl_set_fifo(fdctrl, 1, 0); |
65cef780 BS |
1424 | } |
1425 | ||
5c02c033 | 1426 | static void fdctrl_handle_dumpreg(FDCtrl *fdctrl, int direction) |
65cef780 | 1427 | { |
5c02c033 | 1428 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1429 | |
1430 | /* Drives position */ | |
1431 | fdctrl->fifo[0] = drv0(fdctrl)->track; | |
1432 | fdctrl->fifo[1] = drv1(fdctrl)->track; | |
78ae820c BS |
1433 | #if MAX_FD == 4 |
1434 | fdctrl->fifo[2] = drv2(fdctrl)->track; | |
1435 | fdctrl->fifo[3] = drv3(fdctrl)->track; | |
1436 | #else | |
65cef780 BS |
1437 | fdctrl->fifo[2] = 0; |
1438 | fdctrl->fifo[3] = 0; | |
78ae820c | 1439 | #endif |
65cef780 BS |
1440 | /* timers */ |
1441 | fdctrl->fifo[4] = fdctrl->timer0; | |
368df94d | 1442 | fdctrl->fifo[5] = (fdctrl->timer1 << 1) | (fdctrl->dor & FD_DOR_DMAEN ? 1 : 0); |
65cef780 BS |
1443 | fdctrl->fifo[6] = cur_drv->last_sect; |
1444 | fdctrl->fifo[7] = (fdctrl->lock << 7) | | |
1445 | (cur_drv->perpendicular << 2); | |
1446 | fdctrl->fifo[8] = fdctrl->config; | |
1447 | fdctrl->fifo[9] = fdctrl->precomp_trk; | |
1448 | fdctrl_set_fifo(fdctrl, 10, 0); | |
1449 | } | |
1450 | ||
5c02c033 | 1451 | static void fdctrl_handle_version(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1452 | { |
1453 | /* Controller's version */ | |
1454 | fdctrl->fifo[0] = fdctrl->version; | |
a005186c | 1455 | fdctrl_set_fifo(fdctrl, 1, 0); |
65cef780 BS |
1456 | } |
1457 | ||
5c02c033 | 1458 | static void fdctrl_handle_partid(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1459 | { |
1460 | fdctrl->fifo[0] = 0x41; /* Stepping 1 */ | |
1461 | fdctrl_set_fifo(fdctrl, 1, 0); | |
1462 | } | |
1463 | ||
5c02c033 | 1464 | static void fdctrl_handle_restore(FDCtrl *fdctrl, int direction) |
65cef780 | 1465 | { |
5c02c033 | 1466 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1467 | |
1468 | /* Drives position */ | |
1469 | drv0(fdctrl)->track = fdctrl->fifo[3]; | |
1470 | drv1(fdctrl)->track = fdctrl->fifo[4]; | |
78ae820c BS |
1471 | #if MAX_FD == 4 |
1472 | drv2(fdctrl)->track = fdctrl->fifo[5]; | |
1473 | drv3(fdctrl)->track = fdctrl->fifo[6]; | |
1474 | #endif | |
65cef780 BS |
1475 | /* timers */ |
1476 | fdctrl->timer0 = fdctrl->fifo[7]; | |
1477 | fdctrl->timer1 = fdctrl->fifo[8]; | |
1478 | cur_drv->last_sect = fdctrl->fifo[9]; | |
1479 | fdctrl->lock = fdctrl->fifo[10] >> 7; | |
1480 | cur_drv->perpendicular = (fdctrl->fifo[10] >> 2) & 0xF; | |
1481 | fdctrl->config = fdctrl->fifo[11]; | |
1482 | fdctrl->precomp_trk = fdctrl->fifo[12]; | |
1483 | fdctrl->pwrd = fdctrl->fifo[13]; | |
1484 | fdctrl_reset_fifo(fdctrl); | |
1485 | } | |
1486 | ||
5c02c033 | 1487 | static void fdctrl_handle_save(FDCtrl *fdctrl, int direction) |
65cef780 | 1488 | { |
5c02c033 | 1489 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1490 | |
1491 | fdctrl->fifo[0] = 0; | |
1492 | fdctrl->fifo[1] = 0; | |
1493 | /* Drives position */ | |
1494 | fdctrl->fifo[2] = drv0(fdctrl)->track; | |
1495 | fdctrl->fifo[3] = drv1(fdctrl)->track; | |
78ae820c BS |
1496 | #if MAX_FD == 4 |
1497 | fdctrl->fifo[4] = drv2(fdctrl)->track; | |
1498 | fdctrl->fifo[5] = drv3(fdctrl)->track; | |
1499 | #else | |
65cef780 BS |
1500 | fdctrl->fifo[4] = 0; |
1501 | fdctrl->fifo[5] = 0; | |
78ae820c | 1502 | #endif |
65cef780 BS |
1503 | /* timers */ |
1504 | fdctrl->fifo[6] = fdctrl->timer0; | |
1505 | fdctrl->fifo[7] = fdctrl->timer1; | |
1506 | fdctrl->fifo[8] = cur_drv->last_sect; | |
1507 | fdctrl->fifo[9] = (fdctrl->lock << 7) | | |
1508 | (cur_drv->perpendicular << 2); | |
1509 | fdctrl->fifo[10] = fdctrl->config; | |
1510 | fdctrl->fifo[11] = fdctrl->precomp_trk; | |
1511 | fdctrl->fifo[12] = fdctrl->pwrd; | |
1512 | fdctrl->fifo[13] = 0; | |
1513 | fdctrl->fifo[14] = 0; | |
a005186c | 1514 | fdctrl_set_fifo(fdctrl, 15, 0); |
65cef780 BS |
1515 | } |
1516 | ||
5c02c033 | 1517 | static void fdctrl_handle_readid(FDCtrl *fdctrl, int direction) |
65cef780 | 1518 | { |
5c02c033 | 1519 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1520 | |
65cef780 BS |
1521 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; |
1522 | qemu_mod_timer(fdctrl->result_timer, | |
74475455 | 1523 | qemu_get_clock_ns(vm_clock) + (get_ticks_per_sec() / 50)); |
65cef780 BS |
1524 | } |
1525 | ||
5c02c033 | 1526 | static void fdctrl_handle_format_track(FDCtrl *fdctrl, int direction) |
65cef780 | 1527 | { |
5c02c033 | 1528 | FDrive *cur_drv; |
65cef780 | 1529 | |
cefec4f5 | 1530 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1531 | cur_drv = get_cur_drv(fdctrl); |
1532 | fdctrl->data_state |= FD_STATE_FORMAT; | |
1533 | if (fdctrl->fifo[0] & 0x80) | |
1534 | fdctrl->data_state |= FD_STATE_MULTI; | |
1535 | else | |
1536 | fdctrl->data_state &= ~FD_STATE_MULTI; | |
1537 | fdctrl->data_state &= ~FD_STATE_SEEK; | |
1538 | cur_drv->bps = | |
1539 | fdctrl->fifo[2] > 7 ? 16384 : 128 << fdctrl->fifo[2]; | |
1540 | #if 0 | |
1541 | cur_drv->last_sect = | |
1542 | cur_drv->flags & FDISK_DBL_SIDES ? fdctrl->fifo[3] : | |
1543 | fdctrl->fifo[3] / 2; | |
1544 | #else | |
1545 | cur_drv->last_sect = fdctrl->fifo[3]; | |
1546 | #endif | |
1547 | /* TODO: implement format using DMA expected by the Bochs BIOS | |
1548 | * and Linux fdformat (read 3 bytes per sector via DMA and fill | |
1549 | * the sector with the specified fill byte | |
1550 | */ | |
1551 | fdctrl->data_state &= ~FD_STATE_FORMAT; | |
1552 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); | |
1553 | } | |
1554 | ||
5c02c033 | 1555 | static void fdctrl_handle_specify(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1556 | { |
1557 | fdctrl->timer0 = (fdctrl->fifo[1] >> 4) & 0xF; | |
1558 | fdctrl->timer1 = fdctrl->fifo[2] >> 1; | |
368df94d BS |
1559 | if (fdctrl->fifo[2] & 1) |
1560 | fdctrl->dor &= ~FD_DOR_DMAEN; | |
1561 | else | |
1562 | fdctrl->dor |= FD_DOR_DMAEN; | |
65cef780 BS |
1563 | /* No result back */ |
1564 | fdctrl_reset_fifo(fdctrl); | |
1565 | } | |
1566 | ||
5c02c033 | 1567 | static void fdctrl_handle_sense_drive_status(FDCtrl *fdctrl, int direction) |
65cef780 | 1568 | { |
5c02c033 | 1569 | FDrive *cur_drv; |
65cef780 | 1570 | |
cefec4f5 | 1571 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1572 | cur_drv = get_cur_drv(fdctrl); |
1573 | cur_drv->head = (fdctrl->fifo[1] >> 2) & 1; | |
1574 | /* 1 Byte status back */ | |
1575 | fdctrl->fifo[0] = (cur_drv->ro << 6) | | |
1576 | (cur_drv->track == 0 ? 0x10 : 0x00) | | |
1577 | (cur_drv->head << 2) | | |
cefec4f5 | 1578 | GET_CUR_DRV(fdctrl) | |
65cef780 BS |
1579 | 0x28; |
1580 | fdctrl_set_fifo(fdctrl, 1, 0); | |
1581 | } | |
1582 | ||
5c02c033 | 1583 | static void fdctrl_handle_recalibrate(FDCtrl *fdctrl, int direction) |
65cef780 | 1584 | { |
5c02c033 | 1585 | FDrive *cur_drv; |
65cef780 | 1586 | |
cefec4f5 | 1587 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 BS |
1588 | cur_drv = get_cur_drv(fdctrl); |
1589 | fd_recalibrate(cur_drv); | |
1590 | fdctrl_reset_fifo(fdctrl); | |
1591 | /* Raise Interrupt */ | |
1592 | fdctrl_raise_irq(fdctrl, FD_SR0_SEEK); | |
1593 | } | |
1594 | ||
5c02c033 | 1595 | static void fdctrl_handle_sense_interrupt_status(FDCtrl *fdctrl, int direction) |
65cef780 | 1596 | { |
5c02c033 | 1597 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1598 | |
f2d81b33 BS |
1599 | if(fdctrl->reset_sensei > 0) { |
1600 | fdctrl->fifo[0] = | |
1601 | FD_SR0_RDYCHG + FD_RESET_SENSEI_COUNT - fdctrl->reset_sensei; | |
1602 | fdctrl->reset_sensei--; | |
1603 | } else { | |
1604 | /* XXX: status0 handling is broken for read/write | |
1605 | commands, so we do this hack. It should be suppressed | |
1606 | ASAP */ | |
1607 | fdctrl->fifo[0] = | |
1608 | FD_SR0_SEEK | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl); | |
1609 | } | |
1610 | ||
65cef780 BS |
1611 | fdctrl->fifo[1] = cur_drv->track; |
1612 | fdctrl_set_fifo(fdctrl, 2, 0); | |
1613 | fdctrl_reset_irq(fdctrl); | |
77370520 | 1614 | fdctrl->status0 = FD_SR0_RDYCHG; |
65cef780 BS |
1615 | } |
1616 | ||
5c02c033 | 1617 | static void fdctrl_handle_seek(FDCtrl *fdctrl, int direction) |
65cef780 | 1618 | { |
5c02c033 | 1619 | FDrive *cur_drv; |
65cef780 | 1620 | |
cefec4f5 | 1621 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1622 | cur_drv = get_cur_drv(fdctrl); |
65cef780 | 1623 | fdctrl_reset_fifo(fdctrl); |
b072a3c8 HP |
1624 | /* The seek command just sends step pulses to the drive and doesn't care if |
1625 | * there is a medium inserted of if it's banging the head against the drive. | |
1626 | */ | |
65cef780 | 1627 | if (fdctrl->fifo[2] > cur_drv->max_track) { |
b072a3c8 | 1628 | cur_drv->track = cur_drv->max_track; |
65cef780 BS |
1629 | } else { |
1630 | cur_drv->track = fdctrl->fifo[2]; | |
65cef780 | 1631 | } |
b072a3c8 HP |
1632 | /* Raise Interrupt */ |
1633 | fdctrl_raise_irq(fdctrl, FD_SR0_SEEK); | |
65cef780 BS |
1634 | } |
1635 | ||
5c02c033 | 1636 | static void fdctrl_handle_perpendicular_mode(FDCtrl *fdctrl, int direction) |
65cef780 | 1637 | { |
5c02c033 | 1638 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1639 | |
1640 | if (fdctrl->fifo[1] & 0x80) | |
1641 | cur_drv->perpendicular = fdctrl->fifo[1] & 0x7; | |
1642 | /* No result back */ | |
1c346df2 | 1643 | fdctrl_reset_fifo(fdctrl); |
65cef780 BS |
1644 | } |
1645 | ||
5c02c033 | 1646 | static void fdctrl_handle_configure(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1647 | { |
1648 | fdctrl->config = fdctrl->fifo[2]; | |
1649 | fdctrl->precomp_trk = fdctrl->fifo[3]; | |
1650 | /* No result back */ | |
1651 | fdctrl_reset_fifo(fdctrl); | |
1652 | } | |
1653 | ||
5c02c033 | 1654 | static void fdctrl_handle_powerdown_mode(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1655 | { |
1656 | fdctrl->pwrd = fdctrl->fifo[1]; | |
1657 | fdctrl->fifo[0] = fdctrl->fifo[1]; | |
a005186c | 1658 | fdctrl_set_fifo(fdctrl, 1, 0); |
65cef780 BS |
1659 | } |
1660 | ||
5c02c033 | 1661 | static void fdctrl_handle_option(FDCtrl *fdctrl, int direction) |
65cef780 BS |
1662 | { |
1663 | /* No result back */ | |
1664 | fdctrl_reset_fifo(fdctrl); | |
1665 | } | |
1666 | ||
5c02c033 | 1667 | static void fdctrl_handle_drive_specification_command(FDCtrl *fdctrl, int direction) |
65cef780 | 1668 | { |
5c02c033 | 1669 | FDrive *cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1670 | |
1671 | if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x80) { | |
1672 | /* Command parameters done */ | |
1673 | if (fdctrl->fifo[fdctrl->data_pos - 1] & 0x40) { | |
1674 | fdctrl->fifo[0] = fdctrl->fifo[1]; | |
1675 | fdctrl->fifo[2] = 0; | |
1676 | fdctrl->fifo[3] = 0; | |
a005186c | 1677 | fdctrl_set_fifo(fdctrl, 4, 0); |
65cef780 BS |
1678 | } else { |
1679 | fdctrl_reset_fifo(fdctrl); | |
1680 | } | |
1681 | } else if (fdctrl->data_len > 7) { | |
1682 | /* ERROR */ | |
1683 | fdctrl->fifo[0] = 0x80 | | |
cefec4f5 | 1684 | (cur_drv->head << 2) | GET_CUR_DRV(fdctrl); |
a005186c | 1685 | fdctrl_set_fifo(fdctrl, 1, 0); |
65cef780 BS |
1686 | } |
1687 | } | |
1688 | ||
5c02c033 | 1689 | static void fdctrl_handle_relative_seek_out(FDCtrl *fdctrl, int direction) |
65cef780 | 1690 | { |
5c02c033 | 1691 | FDrive *cur_drv; |
65cef780 | 1692 | |
cefec4f5 | 1693 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1694 | cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1695 | if (fdctrl->fifo[2] + cur_drv->track >= cur_drv->max_track) { |
1696 | cur_drv->track = cur_drv->max_track - 1; | |
1697 | } else { | |
1698 | cur_drv->track += fdctrl->fifo[2]; | |
1699 | } | |
1700 | fdctrl_reset_fifo(fdctrl); | |
77370520 | 1701 | /* Raise Interrupt */ |
65cef780 BS |
1702 | fdctrl_raise_irq(fdctrl, FD_SR0_SEEK); |
1703 | } | |
1704 | ||
5c02c033 | 1705 | static void fdctrl_handle_relative_seek_in(FDCtrl *fdctrl, int direction) |
65cef780 | 1706 | { |
5c02c033 | 1707 | FDrive *cur_drv; |
65cef780 | 1708 | |
cefec4f5 | 1709 | SET_CUR_DRV(fdctrl, fdctrl->fifo[1] & FD_DOR_SELMASK); |
65cef780 | 1710 | cur_drv = get_cur_drv(fdctrl); |
65cef780 BS |
1711 | if (fdctrl->fifo[2] > cur_drv->track) { |
1712 | cur_drv->track = 0; | |
1713 | } else { | |
1714 | cur_drv->track -= fdctrl->fifo[2]; | |
1715 | } | |
1716 | fdctrl_reset_fifo(fdctrl); | |
1717 | /* Raise Interrupt */ | |
1718 | fdctrl_raise_irq(fdctrl, FD_SR0_SEEK); | |
1719 | } | |
1720 | ||
678803ab BS |
1721 | static const struct { |
1722 | uint8_t value; | |
1723 | uint8_t mask; | |
1724 | const char* name; | |
1725 | int parameters; | |
5c02c033 | 1726 | void (*handler)(FDCtrl *fdctrl, int direction); |
678803ab BS |
1727 | int direction; |
1728 | } handlers[] = { | |
1729 | { FD_CMD_READ, 0x1f, "READ", 8, fdctrl_start_transfer, FD_DIR_READ }, | |
1730 | { FD_CMD_WRITE, 0x3f, "WRITE", 8, fdctrl_start_transfer, FD_DIR_WRITE }, | |
1731 | { FD_CMD_SEEK, 0xff, "SEEK", 2, fdctrl_handle_seek }, | |
1732 | { FD_CMD_SENSE_INTERRUPT_STATUS, 0xff, "SENSE INTERRUPT STATUS", 0, fdctrl_handle_sense_interrupt_status }, | |
1733 | { FD_CMD_RECALIBRATE, 0xff, "RECALIBRATE", 1, fdctrl_handle_recalibrate }, | |
1734 | { FD_CMD_FORMAT_TRACK, 0xbf, "FORMAT TRACK", 5, fdctrl_handle_format_track }, | |
1735 | { FD_CMD_READ_TRACK, 0xbf, "READ TRACK", 8, fdctrl_start_transfer, FD_DIR_READ }, | |
1736 | { FD_CMD_RESTORE, 0xff, "RESTORE", 17, fdctrl_handle_restore }, /* part of READ DELETED DATA */ | |
1737 | { FD_CMD_SAVE, 0xff, "SAVE", 0, fdctrl_handle_save }, /* part of READ DELETED DATA */ | |
1738 | { FD_CMD_READ_DELETED, 0x1f, "READ DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_READ }, | |
1739 | { FD_CMD_SCAN_EQUAL, 0x1f, "SCAN EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANE }, | |
1740 | { FD_CMD_VERIFY, 0x1f, "VERIFY", 8, fdctrl_unimplemented }, | |
1741 | { FD_CMD_SCAN_LOW_OR_EQUAL, 0x1f, "SCAN LOW OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANL }, | |
1742 | { FD_CMD_SCAN_HIGH_OR_EQUAL, 0x1f, "SCAN HIGH OR EQUAL", 8, fdctrl_start_transfer, FD_DIR_SCANH }, | |
1743 | { FD_CMD_WRITE_DELETED, 0x3f, "WRITE DELETED DATA", 8, fdctrl_start_transfer_del, FD_DIR_WRITE }, | |
1744 | { FD_CMD_READ_ID, 0xbf, "READ ID", 1, fdctrl_handle_readid }, | |
1745 | { FD_CMD_SPECIFY, 0xff, "SPECIFY", 2, fdctrl_handle_specify }, | |
1746 | { FD_CMD_SENSE_DRIVE_STATUS, 0xff, "SENSE DRIVE STATUS", 1, fdctrl_handle_sense_drive_status }, | |
1747 | { FD_CMD_PERPENDICULAR_MODE, 0xff, "PERPENDICULAR MODE", 1, fdctrl_handle_perpendicular_mode }, | |
1748 | { FD_CMD_CONFIGURE, 0xff, "CONFIGURE", 3, fdctrl_handle_configure }, | |
1749 | { FD_CMD_POWERDOWN_MODE, 0xff, "POWERDOWN MODE", 2, fdctrl_handle_powerdown_mode }, | |
1750 | { FD_CMD_OPTION, 0xff, "OPTION", 1, fdctrl_handle_option }, | |
1751 | { FD_CMD_DRIVE_SPECIFICATION_COMMAND, 0xff, "DRIVE SPECIFICATION COMMAND", 5, fdctrl_handle_drive_specification_command }, | |
1752 | { FD_CMD_RELATIVE_SEEK_OUT, 0xff, "RELATIVE SEEK OUT", 2, fdctrl_handle_relative_seek_out }, | |
1753 | { FD_CMD_FORMAT_AND_WRITE, 0xff, "FORMAT AND WRITE", 10, fdctrl_unimplemented }, | |
1754 | { FD_CMD_RELATIVE_SEEK_IN, 0xff, "RELATIVE SEEK IN", 2, fdctrl_handle_relative_seek_in }, | |
1755 | { FD_CMD_LOCK, 0x7f, "LOCK", 0, fdctrl_handle_lock }, | |
1756 | { FD_CMD_DUMPREG, 0xff, "DUMPREG", 0, fdctrl_handle_dumpreg }, | |
1757 | { FD_CMD_VERSION, 0xff, "VERSION", 0, fdctrl_handle_version }, | |
1758 | { FD_CMD_PART_ID, 0xff, "PART ID", 0, fdctrl_handle_partid }, | |
1759 | { FD_CMD_WRITE, 0x1f, "WRITE (BeOS)", 8, fdctrl_start_transfer, FD_DIR_WRITE }, /* not in specification ; BeOS 4.5 bug */ | |
1760 | { 0, 0, "unknown", 0, fdctrl_unimplemented }, /* default handler */ | |
1761 | }; | |
1762 | /* Associate command to an index in the 'handlers' array */ | |
1763 | static uint8_t command_to_handler[256]; | |
1764 | ||
5c02c033 | 1765 | static void fdctrl_write_data(FDCtrl *fdctrl, uint32_t value) |
baca51fa | 1766 | { |
5c02c033 | 1767 | FDrive *cur_drv; |
65cef780 | 1768 | int pos; |
baca51fa | 1769 | |
8977f3c1 | 1770 | /* Reset mode */ |
1c346df2 | 1771 | if (!(fdctrl->dor & FD_DOR_nRESET)) { |
4b19ec0c | 1772 | FLOPPY_DPRINTF("Floppy controller in RESET state !\n"); |
8977f3c1 FB |
1773 | return; |
1774 | } | |
b9b3d225 BS |
1775 | if (!(fdctrl->msr & FD_MSR_RQM) || (fdctrl->msr & FD_MSR_DIO)) { |
1776 | FLOPPY_ERROR("controller not ready for writing\n"); | |
8977f3c1 FB |
1777 | return; |
1778 | } | |
b9b3d225 | 1779 | fdctrl->dsr &= ~FD_DSR_PWRDOWN; |
8977f3c1 | 1780 | /* Is it write command time ? */ |
368df94d | 1781 | if (fdctrl->msr & FD_MSR_NONDMA) { |
8977f3c1 | 1782 | /* FIFO data write */ |
b3bc1540 BS |
1783 | pos = fdctrl->data_pos++; |
1784 | pos %= FD_SECTOR_LEN; | |
1785 | fdctrl->fifo[pos] = value; | |
1786 | if (pos == FD_SECTOR_LEN - 1 || | |
baca51fa | 1787 | fdctrl->data_pos == fdctrl->data_len) { |
77370520 BS |
1788 | cur_drv = get_cur_drv(fdctrl); |
1789 | if (bdrv_write(cur_drv->bs, fd_sector(cur_drv), fdctrl->fifo, 1) < 0) { | |
1790 | FLOPPY_ERROR("writing sector %d\n", fd_sector(cur_drv)); | |
1791 | return; | |
1792 | } | |
746d6de7 BS |
1793 | if (!fdctrl_seek_to_next_sect(fdctrl, cur_drv)) { |
1794 | FLOPPY_DPRINTF("error seeking to next sector %d\n", | |
1795 | fd_sector(cur_drv)); | |
1796 | return; | |
1797 | } | |
8977f3c1 | 1798 | } |
890fa6be | 1799 | /* Switch from transfer mode to status mode |
8977f3c1 FB |
1800 | * then from status mode to command mode |
1801 | */ | |
b9b3d225 | 1802 | if (fdctrl->data_pos == fdctrl->data_len) |
9fea808a | 1803 | fdctrl_stop_transfer(fdctrl, FD_SR0_SEEK, 0x00, 0x00); |
8977f3c1 FB |
1804 | return; |
1805 | } | |
baca51fa | 1806 | if (fdctrl->data_pos == 0) { |
8977f3c1 | 1807 | /* Command */ |
678803ab BS |
1808 | pos = command_to_handler[value & 0xff]; |
1809 | FLOPPY_DPRINTF("%s command\n", handlers[pos].name); | |
1810 | fdctrl->data_len = handlers[pos].parameters + 1; | |
1457a758 | 1811 | fdctrl->msr |= FD_MSR_CMDBUSY; |
8977f3c1 | 1812 | } |
678803ab | 1813 | |
baca51fa | 1814 | FLOPPY_DPRINTF("%s: %02x\n", __func__, value); |
77370520 BS |
1815 | fdctrl->fifo[fdctrl->data_pos++] = value; |
1816 | if (fdctrl->data_pos == fdctrl->data_len) { | |
8977f3c1 FB |
1817 | /* We now have all parameters |
1818 | * and will be able to treat the command | |
1819 | */ | |
4f431960 JM |
1820 | if (fdctrl->data_state & FD_STATE_FORMAT) { |
1821 | fdctrl_format_sector(fdctrl); | |
8977f3c1 FB |
1822 | return; |
1823 | } | |
65cef780 | 1824 | |
678803ab BS |
1825 | pos = command_to_handler[fdctrl->fifo[0] & 0xff]; |
1826 | FLOPPY_DPRINTF("treat %s command\n", handlers[pos].name); | |
1827 | (*handlers[pos].handler)(fdctrl, handlers[pos].direction); | |
8977f3c1 FB |
1828 | } |
1829 | } | |
ed5fd2cc FB |
1830 | |
1831 | static void fdctrl_result_timer(void *opaque) | |
1832 | { | |
5c02c033 BS |
1833 | FDCtrl *fdctrl = opaque; |
1834 | FDrive *cur_drv = get_cur_drv(fdctrl); | |
4f431960 | 1835 | |
b7ffa3b1 TS |
1836 | /* Pretend we are spinning. |
1837 | * This is needed for Coherent, which uses READ ID to check for | |
1838 | * sector interleaving. | |
1839 | */ | |
1840 | if (cur_drv->last_sect != 0) { | |
1841 | cur_drv->sect = (cur_drv->sect % cur_drv->last_sect) + 1; | |
1842 | } | |
844f65d6 HP |
1843 | /* READ_ID can't automatically succeed! */ |
1844 | if (fdctrl->check_media_rate && | |
1845 | (fdctrl->dsr & FD_DSR_DRATEMASK) != cur_drv->media_rate) { | |
1846 | FLOPPY_DPRINTF("read id rate mismatch (fdc=%d, media=%d)\n", | |
1847 | fdctrl->dsr & FD_DSR_DRATEMASK, cur_drv->media_rate); | |
1848 | fdctrl_stop_transfer(fdctrl, FD_SR0_ABNTERM, FD_SR1_MA, 0x00); | |
1849 | } else { | |
1850 | fdctrl_stop_transfer(fdctrl, 0x00, 0x00, 0x00); | |
1851 | } | |
ed5fd2cc | 1852 | } |
678803ab | 1853 | |
7d4b4ba5 | 1854 | static void fdctrl_change_cb(void *opaque, bool load) |
8e49ca46 MA |
1855 | { |
1856 | FDrive *drive = opaque; | |
1857 | ||
1858 | drive->media_changed = 1; | |
1859 | } | |
1860 | ||
1861 | static const BlockDevOps fdctrl_block_ops = { | |
1862 | .change_media_cb = fdctrl_change_cb, | |
1863 | }; | |
1864 | ||
678803ab | 1865 | /* Init functions */ |
b47b3525 | 1866 | static int fdctrl_connect_drives(FDCtrl *fdctrl) |
678803ab | 1867 | { |
12a71a02 | 1868 | unsigned int i; |
7d0d6950 | 1869 | FDrive *drive; |
678803ab | 1870 | |
678803ab | 1871 | for (i = 0; i < MAX_FD; i++) { |
7d0d6950 | 1872 | drive = &fdctrl->drives[i]; |
844f65d6 | 1873 | drive->fdctrl = fdctrl; |
7d0d6950 | 1874 | |
b47b3525 MA |
1875 | if (drive->bs) { |
1876 | if (bdrv_get_on_error(drive->bs, 0) != BLOCK_ERR_STOP_ENOSPC) { | |
1877 | error_report("fdc doesn't support drive option werror"); | |
1878 | return -1; | |
1879 | } | |
1880 | if (bdrv_get_on_error(drive->bs, 1) != BLOCK_ERR_REPORT) { | |
1881 | error_report("fdc doesn't support drive option rerror"); | |
1882 | return -1; | |
1883 | } | |
1884 | } | |
1885 | ||
7d0d6950 MA |
1886 | fd_init(drive); |
1887 | fd_revalidate(drive); | |
1888 | if (drive->bs) { | |
8e49ca46 | 1889 | drive->media_changed = 1; |
8e49ca46 | 1890 | bdrv_set_dev_ops(drive->bs, &fdctrl_block_ops, drive); |
7d0d6950 | 1891 | } |
678803ab | 1892 | } |
b47b3525 | 1893 | return 0; |
678803ab BS |
1894 | } |
1895 | ||
63ffb564 BS |
1896 | void fdctrl_init_sysbus(qemu_irq irq, int dma_chann, |
1897 | target_phys_addr_t mmio_base, DriveInfo **fds) | |
2091ba23 | 1898 | { |
5c02c033 | 1899 | FDCtrl *fdctrl; |
2091ba23 | 1900 | DeviceState *dev; |
5c02c033 | 1901 | FDCtrlSysBus *sys; |
2091ba23 GH |
1902 | |
1903 | dev = qdev_create(NULL, "sysbus-fdc"); | |
5c02c033 | 1904 | sys = DO_UPCAST(FDCtrlSysBus, busdev.qdev, dev); |
99244fa1 GH |
1905 | fdctrl = &sys->state; |
1906 | fdctrl->dma_chann = dma_chann; /* FIXME */ | |
995bf0ca | 1907 | if (fds[0]) { |
18846dee | 1908 | qdev_prop_set_drive_nofail(dev, "driveA", fds[0]->bdrv); |
995bf0ca GH |
1909 | } |
1910 | if (fds[1]) { | |
18846dee | 1911 | qdev_prop_set_drive_nofail(dev, "driveB", fds[1]->bdrv); |
995bf0ca | 1912 | } |
e23a1b33 | 1913 | qdev_init_nofail(dev); |
2091ba23 GH |
1914 | sysbus_connect_irq(&sys->busdev, 0, irq); |
1915 | sysbus_mmio_map(&sys->busdev, 0, mmio_base); | |
678803ab BS |
1916 | } |
1917 | ||
63ffb564 BS |
1918 | void sun4m_fdctrl_init(qemu_irq irq, target_phys_addr_t io_base, |
1919 | DriveInfo **fds, qemu_irq *fdc_tc) | |
678803ab | 1920 | { |
f64ab228 | 1921 | DeviceState *dev; |
5c02c033 | 1922 | FDCtrlSysBus *sys; |
678803ab | 1923 | |
12a71a02 | 1924 | dev = qdev_create(NULL, "SUNW,fdtwo"); |
995bf0ca | 1925 | if (fds[0]) { |
18846dee | 1926 | qdev_prop_set_drive_nofail(dev, "drive", fds[0]->bdrv); |
995bf0ca | 1927 | } |
e23a1b33 | 1928 | qdev_init_nofail(dev); |
5c02c033 | 1929 | sys = DO_UPCAST(FDCtrlSysBus, busdev.qdev, dev); |
8baf73ad GH |
1930 | sysbus_connect_irq(&sys->busdev, 0, irq); |
1931 | sysbus_mmio_map(&sys->busdev, 0, io_base); | |
f64ab228 | 1932 | *fdc_tc = qdev_get_gpio_in(dev, 0); |
678803ab | 1933 | } |
f64ab228 | 1934 | |
a64405d1 | 1935 | static int fdctrl_init_common(FDCtrl *fdctrl) |
f64ab228 | 1936 | { |
12a71a02 BS |
1937 | int i, j; |
1938 | static int command_tables_inited = 0; | |
f64ab228 | 1939 | |
12a71a02 BS |
1940 | /* Fill 'command_to_handler' lookup table */ |
1941 | if (!command_tables_inited) { | |
1942 | command_tables_inited = 1; | |
1943 | for (i = ARRAY_SIZE(handlers) - 1; i >= 0; i--) { | |
1944 | for (j = 0; j < sizeof(command_to_handler); j++) { | |
1945 | if ((j & handlers[i].mask) == handlers[i].value) { | |
1946 | command_to_handler[j] = i; | |
1947 | } | |
1948 | } | |
1949 | } | |
1950 | } | |
1951 | ||
1952 | FLOPPY_DPRINTF("init controller\n"); | |
1953 | fdctrl->fifo = qemu_memalign(512, FD_SECTOR_LEN); | |
d7a6c270 | 1954 | fdctrl->fifo_size = 512; |
74475455 | 1955 | fdctrl->result_timer = qemu_new_timer_ns(vm_clock, |
12a71a02 BS |
1956 | fdctrl_result_timer, fdctrl); |
1957 | ||
1958 | fdctrl->version = 0x90; /* Intel 82078 controller */ | |
1959 | fdctrl->config = FD_CONFIG_EIS | FD_CONFIG_EFIFO; /* Implicit seek, polling & FIFO enabled */ | |
d7a6c270 | 1960 | fdctrl->num_floppies = MAX_FD; |
12a71a02 | 1961 | |
99244fa1 GH |
1962 | if (fdctrl->dma_chann != -1) |
1963 | DMA_register_channel(fdctrl->dma_chann, &fdctrl_transfer_handler, fdctrl); | |
b47b3525 | 1964 | return fdctrl_connect_drives(fdctrl); |
f64ab228 BS |
1965 | } |
1966 | ||
212ec7ba | 1967 | static const MemoryRegionPortio fdc_portio_list[] = { |
2f290a8c | 1968 | { 1, 5, 1, .read = fdctrl_read, .write = fdctrl_write }, |
212ec7ba RH |
1969 | { 7, 1, 1, .read = fdctrl_read, .write = fdctrl_write }, |
1970 | PORTIO_END_OF_LIST(), | |
2f290a8c RH |
1971 | }; |
1972 | ||
81a322d4 | 1973 | static int isabus_fdc_init1(ISADevice *dev) |
8baf73ad | 1974 | { |
5c02c033 BS |
1975 | FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev); |
1976 | FDCtrl *fdctrl = &isa->state; | |
2be37833 | 1977 | int ret; |
8baf73ad | 1978 | |
c9ae703d | 1979 | isa_register_portio_list(dev, isa->iobase, fdc_portio_list, fdctrl, "fdc"); |
dee41d58 | 1980 | |
c9ae703d HP |
1981 | isa_init_irq(&isa->busdev, &fdctrl->irq, isa->irq); |
1982 | fdctrl->dma_chann = isa->dma; | |
8baf73ad | 1983 | |
c9ae703d | 1984 | qdev_set_legacy_instance_id(&dev->qdev, isa->iobase, 2); |
a64405d1 | 1985 | ret = fdctrl_init_common(fdctrl); |
2be37833 | 1986 | |
1ca4d09a GN |
1987 | add_boot_device_path(isa->bootindexA, &dev->qdev, "/floppy@0"); |
1988 | add_boot_device_path(isa->bootindexB, &dev->qdev, "/floppy@1"); | |
1989 | ||
2be37833 | 1990 | return ret; |
8baf73ad GH |
1991 | } |
1992 | ||
81a322d4 | 1993 | static int sysbus_fdc_init1(SysBusDevice *dev) |
12a71a02 | 1994 | { |
5c02c033 BS |
1995 | FDCtrlSysBus *sys = DO_UPCAST(FDCtrlSysBus, busdev, dev); |
1996 | FDCtrl *fdctrl = &sys->state; | |
2be37833 | 1997 | int ret; |
12a71a02 | 1998 | |
dc6c1b37 | 1999 | memory_region_init_io(&fdctrl->iomem, &fdctrl_mem_ops, fdctrl, "fdc", 0x08); |
750ecd44 | 2000 | sysbus_init_mmio(dev, &fdctrl->iomem); |
8baf73ad GH |
2001 | sysbus_init_irq(dev, &fdctrl->irq); |
2002 | qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1); | |
99244fa1 | 2003 | fdctrl->dma_chann = -1; |
8baf73ad | 2004 | |
dc6c1b37 | 2005 | qdev_set_legacy_instance_id(&dev->qdev, 0 /* io */, 2); /* FIXME */ |
a64405d1 | 2006 | ret = fdctrl_init_common(fdctrl); |
2be37833 BS |
2007 | |
2008 | return ret; | |
12a71a02 BS |
2009 | } |
2010 | ||
81a322d4 | 2011 | static int sun4m_fdc_init1(SysBusDevice *dev) |
12a71a02 | 2012 | { |
5c02c033 | 2013 | FDCtrl *fdctrl = &(FROM_SYSBUS(FDCtrlSysBus, dev)->state); |
12a71a02 | 2014 | |
dc6c1b37 AK |
2015 | memory_region_init_io(&fdctrl->iomem, &fdctrl_mem_strict_ops, fdctrl, |
2016 | "fdctrl", 0x08); | |
750ecd44 | 2017 | sysbus_init_mmio(dev, &fdctrl->iomem); |
8baf73ad GH |
2018 | sysbus_init_irq(dev, &fdctrl->irq); |
2019 | qdev_init_gpio_in(&dev->qdev, fdctrl_handle_tc, 1); | |
2020 | ||
2021 | fdctrl->sun4m = 1; | |
dc6c1b37 | 2022 | qdev_set_legacy_instance_id(&dev->qdev, 0 /* io */, 2); /* FIXME */ |
a64405d1 | 2023 | return fdctrl_init_common(fdctrl); |
12a71a02 | 2024 | } |
f64ab228 | 2025 | |
34d4260e KW |
2026 | void fdc_get_bs(BlockDriverState *bs[], ISADevice *dev) |
2027 | { | |
2028 | FDCtrlISABus *isa = DO_UPCAST(FDCtrlISABus, busdev, dev); | |
2029 | FDCtrl *fdctrl = &isa->state; | |
2030 | int i; | |
2031 | ||
2032 | for (i = 0; i < MAX_FD; i++) { | |
2033 | bs[i] = fdctrl->drives[i].bs; | |
2034 | } | |
2035 | } | |
2036 | ||
2037 | ||
a64405d1 JK |
2038 | static const VMStateDescription vmstate_isa_fdc ={ |
2039 | .name = "fdc", | |
2040 | .version_id = 2, | |
2041 | .minimum_version_id = 2, | |
2042 | .fields = (VMStateField []) { | |
2043 | VMSTATE_STRUCT(state, FDCtrlISABus, 0, vmstate_fdc, FDCtrl), | |
2044 | VMSTATE_END_OF_LIST() | |
2045 | } | |
2046 | }; | |
2047 | ||
39bffca2 | 2048 | static Property isa_fdc_properties[] = { |
c9ae703d HP |
2049 | DEFINE_PROP_HEX32("iobase", FDCtrlISABus, iobase, 0x3f0), |
2050 | DEFINE_PROP_UINT32("irq", FDCtrlISABus, irq, 6), | |
2051 | DEFINE_PROP_UINT32("dma", FDCtrlISABus, dma, 2), | |
39bffca2 AL |
2052 | DEFINE_PROP_DRIVE("driveA", FDCtrlISABus, state.drives[0].bs), |
2053 | DEFINE_PROP_DRIVE("driveB", FDCtrlISABus, state.drives[1].bs), | |
2054 | DEFINE_PROP_INT32("bootindexA", FDCtrlISABus, bootindexA, -1), | |
2055 | DEFINE_PROP_INT32("bootindexB", FDCtrlISABus, bootindexB, -1), | |
09c6d585 HP |
2056 | DEFINE_PROP_BIT("check_media_rate", FDCtrlISABus, state.check_media_rate, |
2057 | 0, true), | |
39bffca2 AL |
2058 | DEFINE_PROP_END_OF_LIST(), |
2059 | }; | |
2060 | ||
8f04ee08 AL |
2061 | static void isabus_fdc_class_init1(ObjectClass *klass, void *data) |
2062 | { | |
39bffca2 | 2063 | DeviceClass *dc = DEVICE_CLASS(klass); |
8f04ee08 AL |
2064 | ISADeviceClass *ic = ISA_DEVICE_CLASS(klass); |
2065 | ic->init = isabus_fdc_init1; | |
39bffca2 AL |
2066 | dc->fw_name = "fdc"; |
2067 | dc->no_user = 1; | |
2068 | dc->reset = fdctrl_external_reset_isa; | |
2069 | dc->vmsd = &vmstate_isa_fdc; | |
2070 | dc->props = isa_fdc_properties; | |
2071 | } | |
2072 | ||
2073 | static TypeInfo isa_fdc_info = { | |
2074 | .name = "isa-fdc", | |
2075 | .parent = TYPE_ISA_DEVICE, | |
2076 | .instance_size = sizeof(FDCtrlISABus), | |
2077 | .class_init = isabus_fdc_class_init1, | |
8baf73ad GH |
2078 | }; |
2079 | ||
a64405d1 JK |
2080 | static const VMStateDescription vmstate_sysbus_fdc ={ |
2081 | .name = "fdc", | |
2082 | .version_id = 2, | |
2083 | .minimum_version_id = 2, | |
2084 | .fields = (VMStateField []) { | |
2085 | VMSTATE_STRUCT(state, FDCtrlSysBus, 0, vmstate_fdc, FDCtrl), | |
2086 | VMSTATE_END_OF_LIST() | |
2087 | } | |
2088 | }; | |
2089 | ||
999e12bb AL |
2090 | static Property sysbus_fdc_properties[] = { |
2091 | DEFINE_PROP_DRIVE("driveA", FDCtrlSysBus, state.drives[0].bs), | |
2092 | DEFINE_PROP_DRIVE("driveB", FDCtrlSysBus, state.drives[1].bs), | |
2093 | DEFINE_PROP_END_OF_LIST(), | |
12a71a02 BS |
2094 | }; |
2095 | ||
999e12bb AL |
2096 | static void sysbus_fdc_class_init(ObjectClass *klass, void *data) |
2097 | { | |
39bffca2 | 2098 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb AL |
2099 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
2100 | ||
2101 | k->init = sysbus_fdc_init1; | |
39bffca2 AL |
2102 | dc->reset = fdctrl_external_reset_sysbus; |
2103 | dc->vmsd = &vmstate_sysbus_fdc; | |
2104 | dc->props = sysbus_fdc_properties; | |
999e12bb AL |
2105 | } |
2106 | ||
39bffca2 AL |
2107 | static TypeInfo sysbus_fdc_info = { |
2108 | .name = "sysbus-fdc", | |
2109 | .parent = TYPE_SYS_BUS_DEVICE, | |
2110 | .instance_size = sizeof(FDCtrlSysBus), | |
2111 | .class_init = sysbus_fdc_class_init, | |
999e12bb AL |
2112 | }; |
2113 | ||
2114 | static Property sun4m_fdc_properties[] = { | |
2115 | DEFINE_PROP_DRIVE("drive", FDCtrlSysBus, state.drives[0].bs), | |
2116 | DEFINE_PROP_END_OF_LIST(), | |
2117 | }; | |
2118 | ||
2119 | static void sun4m_fdc_class_init(ObjectClass *klass, void *data) | |
2120 | { | |
39bffca2 | 2121 | DeviceClass *dc = DEVICE_CLASS(klass); |
999e12bb AL |
2122 | SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); |
2123 | ||
2124 | k->init = sun4m_fdc_init1; | |
39bffca2 AL |
2125 | dc->reset = fdctrl_external_reset_sysbus; |
2126 | dc->vmsd = &vmstate_sysbus_fdc; | |
2127 | dc->props = sun4m_fdc_properties; | |
999e12bb AL |
2128 | } |
2129 | ||
39bffca2 AL |
2130 | static TypeInfo sun4m_fdc_info = { |
2131 | .name = "SUNW,fdtwo", | |
2132 | .parent = TYPE_SYS_BUS_DEVICE, | |
2133 | .instance_size = sizeof(FDCtrlSysBus), | |
2134 | .class_init = sun4m_fdc_class_init, | |
f64ab228 BS |
2135 | }; |
2136 | ||
83f7d43a | 2137 | static void fdc_register_types(void) |
f64ab228 | 2138 | { |
39bffca2 AL |
2139 | type_register_static(&isa_fdc_info); |
2140 | type_register_static(&sysbus_fdc_info); | |
2141 | type_register_static(&sun4m_fdc_info); | |
f64ab228 BS |
2142 | } |
2143 | ||
83f7d43a | 2144 | type_init(fdc_register_types) |