2 * QEMU IDE Emulation: microdrive (CF / PCMCIA)
4 * Copyright (c) 2003 Fabrice Bellard
5 * Copyright (c) 2006 Openedhand Ltd.
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:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
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
26 #include <hw/i386/pc.h>
27 #include <hw/pcmcia.h>
28 #include "block/block.h"
29 #include "sysemu/dma.h"
31 #include <hw/ide/internal.h>
33 /***********************************************************/
34 /* CF-ATA Microdrive */
36 #define METADATA_SIZE 0x20
38 /* DSCM-1XXXX Microdrive hard disk with CF+ II / PCMCIA interface. */
55 /* Register bitfields */
82 static inline void md_interrupt_update(MicroDriveState
*s
)
87 qemu_set_irq(s
->card
.slot
->irq
,
88 !(s
->stat
& STAT_INT
) && /* Inverted */
89 !(s
->ctrl
& (CTRL_IEN
| CTRL_SRST
)) &&
90 !(s
->opt
& OPT_SRESET
));
93 static void md_set_irq(void *opaque
, int irq
, int level
)
95 MicroDriveState
*s
= opaque
;
101 md_interrupt_update(s
);
104 static void md_reset(MicroDriveState
*s
)
106 s
->opt
= OPT_MODE_MMAP
;
111 ide_bus_reset(&s
->bus
);
114 static uint8_t md_attr_read(void *opaque
, uint32_t at
)
116 MicroDriveState
*s
= opaque
;
117 if (at
< s
->attr_base
) {
118 if (at
< s
->card
.cis_len
)
119 return s
->card
.cis
[at
];
127 case 0x00: /* Configuration Option Register */
129 case 0x02: /* Card Configuration Status Register */
130 if (s
->ctrl
& CTRL_IEN
)
131 return s
->stat
& ~STAT_INT
;
134 case 0x04: /* Pin Replacement Register */
135 return (s
->pins
& PINS_CRDY
) | 0x0c;
136 case 0x06: /* Socket and Copy Register */
140 printf("%s: Bad attribute space register %02x\n", __FUNCTION__
, at
);
147 static void md_attr_write(void *opaque
, uint32_t at
, uint8_t value
)
149 MicroDriveState
*s
= opaque
;
153 case 0x00: /* Configuration Option Register */
154 s
->opt
= value
& 0xcf;
155 if (value
& OPT_SRESET
)
157 md_interrupt_update(s
);
159 case 0x02: /* Card Configuration Status Register */
160 if ((s
->stat
^ value
) & STAT_PWRDWN
)
161 s
->pins
|= PINS_CRDY
;
163 s
->stat
|= value
& 0x74;
164 md_interrupt_update(s
);
165 /* Word 170 in Identify Device must be equal to STAT_XE */
167 case 0x04: /* Pin Replacement Register */
168 s
->pins
&= PINS_CRDY
;
169 s
->pins
|= value
& PINS_MRDY
;
171 case 0x06: /* Socket and Copy Register */
174 printf("%s: Bad attribute space register %02x\n", __FUNCTION__
, at
);
178 static uint16_t md_common_read(void *opaque
, uint32_t at
)
180 MicroDriveState
*s
= opaque
;
185 switch (s
->opt
& OPT_MODE
) {
187 if ((at
& ~0x3ff) == 0x400)
190 case OPT_MODE_IOMAP16
:
193 case OPT_MODE_IOMAP1
:
194 if ((at
& ~0xf) == 0x3f0)
196 else if ((at
& ~0xf) == 0x1f0)
199 case OPT_MODE_IOMAP2
:
200 if ((at
& ~0xf) == 0x370)
202 else if ((at
& ~0xf) == 0x170)
207 case 0x0: /* Even RD Data */
209 return ide_data_readw(&s
->bus
, 0);
211 /* TODO: 8-bit accesses */
215 s
->io
= ide_data_readw(&s
->bus
, 0);
218 s
->cycle
= !s
->cycle
;
220 case 0x9: /* Odd RD Data */
222 case 0xd: /* Error */
223 return ide_ioport_read(&s
->bus
, 0x1);
224 case 0xe: /* Alternate Status */
225 ifs
= idebus_active_if(&s
->bus
);
230 case 0xf: /* Device Address */
231 ifs
= idebus_active_if(&s
->bus
);
232 return 0xc2 | ((~ifs
->select
<< 2) & 0x3c);
234 return ide_ioport_read(&s
->bus
, at
);
240 static void md_common_write(void *opaque
, uint32_t at
, uint16_t value
)
242 MicroDriveState
*s
= opaque
;
245 switch (s
->opt
& OPT_MODE
) {
247 if ((at
& ~0x3ff) == 0x400)
250 case OPT_MODE_IOMAP16
:
253 case OPT_MODE_IOMAP1
:
254 if ((at
& ~0xf) == 0x3f0)
256 else if ((at
& ~0xf) == 0x1f0)
259 case OPT_MODE_IOMAP2
:
260 if ((at
& ~0xf) == 0x370)
262 else if ((at
& ~0xf) == 0x170)
267 case 0x0: /* Even WR Data */
269 ide_data_writew(&s
->bus
, 0, value
);
272 /* TODO: 8-bit accesses */
274 ide_data_writew(&s
->bus
, 0, s
->io
| (value
<< 8));
276 s
->io
= value
& 0xff;
277 s
->cycle
= !s
->cycle
;
280 s
->io
= value
& 0xff;
281 s
->cycle
= !s
->cycle
;
283 case 0xd: /* Features */
284 ide_ioport_write(&s
->bus
, 0x1, value
);
286 case 0xe: /* Device Control */
288 if (value
& CTRL_SRST
)
290 md_interrupt_update(s
);
293 if (s
->stat
& STAT_PWRDWN
) {
294 s
->pins
|= PINS_CRDY
;
295 s
->stat
&= ~STAT_PWRDWN
;
297 ide_ioport_write(&s
->bus
, at
, value
);
301 static const VMStateDescription vmstate_microdrive
= {
302 .name
= "microdrive",
304 .minimum_version_id
= 0,
305 .minimum_version_id_old
= 0,
306 .fields
= (VMStateField
[]) {
307 VMSTATE_UINT8(opt
, MicroDriveState
),
308 VMSTATE_UINT8(stat
, MicroDriveState
),
309 VMSTATE_UINT8(pins
, MicroDriveState
),
310 VMSTATE_UINT8(ctrl
, MicroDriveState
),
311 VMSTATE_UINT16(io
, MicroDriveState
),
312 VMSTATE_UINT8(cycle
, MicroDriveState
),
313 VMSTATE_IDE_BUS(bus
, MicroDriveState
),
314 VMSTATE_IDE_DRIVES(bus
.ifs
, MicroDriveState
),
315 VMSTATE_END_OF_LIST()
319 static const uint8_t dscm1xxxx_cis
[0x14a] = {
320 [0x000] = CISTPL_DEVICE
, /* 5V Device Information */
321 [0x002] = 0x03, /* Tuple length = 4 bytes */
322 [0x004] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
323 [0x006] = 0x01, /* Size = 2K bytes */
324 [0x008] = CISTPL_ENDMARK
,
326 [0x00a] = CISTPL_DEVICE_OC
, /* Additional Device Information */
327 [0x00c] = 0x04, /* Tuple length = 4 byest */
328 [0x00e] = 0x03, /* Conditions: Ext = 0, Vcc 3.3V, MWAIT = 1 */
329 [0x010] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
330 [0x012] = 0x01, /* Size = 2K bytes */
331 [0x014] = CISTPL_ENDMARK
,
333 [0x016] = CISTPL_JEDEC_C
, /* JEDEC ID */
334 [0x018] = 0x02, /* Tuple length = 2 bytes */
335 [0x01a] = 0xdf, /* PC Card ATA with no Vpp required */
338 [0x01e] = CISTPL_MANFID
, /* Manufacture ID */
339 [0x020] = 0x04, /* Tuple length = 4 bytes */
340 [0x022] = 0xa4, /* TPLMID_MANF = 00a4 (IBM) */
342 [0x026] = 0x00, /* PLMID_CARD = 0000 */
345 [0x02a] = CISTPL_VERS_1
, /* Level 1 Version */
346 [0x02c] = 0x12, /* Tuple length = 23 bytes */
347 [0x02e] = 0x04, /* Major Version = JEIDA 4.2 / PCMCIA 2.1 */
348 [0x030] = 0x01, /* Minor Version = 1 */
364 [0x050] = CISTPL_ENDMARK
,
366 [0x052] = CISTPL_FUNCID
, /* Function ID */
367 [0x054] = 0x02, /* Tuple length = 2 bytes */
368 [0x056] = 0x04, /* TPLFID_FUNCTION = Fixed Disk */
369 [0x058] = 0x01, /* TPLFID_SYSINIT: POST = 1, ROM = 0 */
371 [0x05a] = CISTPL_FUNCE
, /* Function Extension */
372 [0x05c] = 0x02, /* Tuple length = 2 bytes */
373 [0x05e] = 0x01, /* TPLFE_TYPE = Disk Device Interface */
374 [0x060] = 0x01, /* TPLFE_DATA = PC Card ATA Interface */
376 [0x062] = CISTPL_FUNCE
, /* Function Extension */
377 [0x064] = 0x03, /* Tuple length = 3 bytes */
378 [0x066] = 0x02, /* TPLFE_TYPE = Basic PC Card ATA Interface */
379 [0x068] = 0x08, /* TPLFE_DATA: Rotating, Unique, Single */
380 [0x06a] = 0x0f, /* TPLFE_DATA: Sleep, Standby, Idle, Auto */
382 [0x06c] = CISTPL_CONFIG
, /* Configuration */
383 [0x06e] = 0x05, /* Tuple length = 5 bytes */
384 [0x070] = 0x01, /* TPCC_RASZ = 2 bytes, TPCC_RMSZ = 1 byte */
385 [0x072] = 0x07, /* TPCC_LAST = 7 */
386 [0x074] = 0x00, /* TPCC_RADR = 0200 */
388 [0x078] = 0x0f, /* TPCC_RMSK = 200, 202, 204, 206 */
390 [0x07a] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
391 [0x07c] = 0x0b, /* Tuple length = 11 bytes */
392 [0x07e] = 0xc0, /* TPCE_INDX = Memory Mode, Default, Iface */
393 [0x080] = 0xc0, /* TPCE_IF = Memory, no BVDs, no WP, READY */
394 [0x082] = 0xa1, /* TPCE_FS = Vcc only, no I/O, Memory, Misc */
395 [0x084] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
396 [0x086] = 0x55, /* NomV: 5.0 V */
397 [0x088] = 0x4d, /* MinV: 4.5 V */
398 [0x08a] = 0x5d, /* MaxV: 5.5 V */
399 [0x08c] = 0x4e, /* Peakl: 450 mA */
400 [0x08e] = 0x08, /* TPCE_MS = 1 window, 1 byte, Host address */
401 [0x090] = 0x00, /* Window descriptor: Window length = 0 */
402 [0x092] = 0x20, /* TPCE_MI: support power down mode, RW */
404 [0x094] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
405 [0x096] = 0x06, /* Tuple length = 6 bytes */
406 [0x098] = 0x00, /* TPCE_INDX = Memory Mode, no Default */
407 [0x09a] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
408 [0x09c] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
409 [0x09e] = 0xb5, /* NomV: 3.3 V */
411 [0x0a2] = 0x3e, /* Peakl: 350 mA */
413 [0x0a4] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
414 [0x0a6] = 0x0d, /* Tuple length = 13 bytes */
415 [0x0a8] = 0xc1, /* TPCE_INDX = I/O and Memory Mode, Default */
416 [0x0aa] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
417 [0x0ac] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
418 [0x0ae] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
419 [0x0b0] = 0x55, /* NomV: 5.0 V */
420 [0x0b2] = 0x4d, /* MinV: 4.5 V */
421 [0x0b4] = 0x5d, /* MaxV: 5.5 V */
422 [0x0b6] = 0x4e, /* Peakl: 450 mA */
423 [0x0b8] = 0x64, /* TPCE_IO = 16-byte boundary, 16/8 accesses */
424 [0x0ba] = 0xf0, /* TPCE_IR = MASK, Level, Pulse, Share */
425 [0x0bc] = 0xff, /* IRQ0..IRQ7 supported */
426 [0x0be] = 0xff, /* IRQ8..IRQ15 supported */
427 [0x0c0] = 0x20, /* TPCE_MI = support power down mode */
429 [0x0c2] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
430 [0x0c4] = 0x06, /* Tuple length = 6 bytes */
431 [0x0c6] = 0x01, /* TPCE_INDX = I/O and Memory Mode */
432 [0x0c8] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
433 [0x0ca] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
434 [0x0cc] = 0xb5, /* NomV: 3.3 V */
436 [0x0d0] = 0x3e, /* Peakl: 350 mA */
438 [0x0d2] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
439 [0x0d4] = 0x12, /* Tuple length = 18 bytes */
440 [0x0d6] = 0xc2, /* TPCE_INDX = I/O Primary Mode */
441 [0x0d8] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
442 [0x0da] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
443 [0x0dc] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
444 [0x0de] = 0x55, /* NomV: 5.0 V */
445 [0x0e0] = 0x4d, /* MinV: 4.5 V */
446 [0x0e2] = 0x5d, /* MaxV: 5.5 V */
447 [0x0e4] = 0x4e, /* Peakl: 450 mA */
448 [0x0e6] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
449 [0x0e8] = 0x61, /* Range: 2 fields, 2 bytes addr, 1 byte len */
450 [0x0ea] = 0xf0, /* Field 1 address = 0x01f0 */
452 [0x0ee] = 0x07, /* Address block length = 8 */
453 [0x0f0] = 0xf6, /* Field 2 address = 0x03f6 */
455 [0x0f4] = 0x01, /* Address block length = 2 */
456 [0x0f6] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
457 [0x0f8] = 0x20, /* TPCE_MI = support power down mode */
459 [0x0fa] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
460 [0x0fc] = 0x06, /* Tuple length = 6 bytes */
461 [0x0fe] = 0x02, /* TPCE_INDX = I/O Primary Mode, no Default */
462 [0x100] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
463 [0x102] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
464 [0x104] = 0xb5, /* NomV: 3.3 V */
466 [0x108] = 0x3e, /* Peakl: 350 mA */
468 [0x10a] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
469 [0x10c] = 0x12, /* Tuple length = 18 bytes */
470 [0x10e] = 0xc3, /* TPCE_INDX = I/O Secondary Mode, Default */
471 [0x110] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
472 [0x112] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
473 [0x114] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
474 [0x116] = 0x55, /* NomV: 5.0 V */
475 [0x118] = 0x4d, /* MinV: 4.5 V */
476 [0x11a] = 0x5d, /* MaxV: 5.5 V */
477 [0x11c] = 0x4e, /* Peakl: 450 mA */
478 [0x11e] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
479 [0x120] = 0x61, /* Range: 2 fields, 2 byte addr, 1 byte len */
480 [0x122] = 0x70, /* Field 1 address = 0x0170 */
482 [0x126] = 0x07, /* Address block length = 8 */
483 [0x128] = 0x76, /* Field 2 address = 0x0376 */
485 [0x12c] = 0x01, /* Address block length = 2 */
486 [0x12e] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
487 [0x130] = 0x20, /* TPCE_MI = support power down mode */
489 [0x132] = CISTPL_CFTABLE_ENTRY
, /* 16-bit PC Card Configuration */
490 [0x134] = 0x06, /* Tuple length = 6 bytes */
491 [0x136] = 0x03, /* TPCE_INDX = I/O Secondary Mode */
492 [0x138] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
493 [0x13a] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
494 [0x13c] = 0xb5, /* NomV: 3.3 V */
496 [0x140] = 0x3e, /* Peakl: 350 mA */
498 [0x142] = CISTPL_NO_LINK
, /* No Link */
499 [0x144] = 0x00, /* Tuple length = 0 bytes */
501 [0x146] = CISTPL_END
, /* Tuple End */
504 static int dscm1xxxx_attach(void *opaque
)
506 MicroDriveState
*md
= opaque
;
507 md
->card
.attr_read
= md_attr_read
;
508 md
->card
.attr_write
= md_attr_write
;
509 md
->card
.common_read
= md_common_read
;
510 md
->card
.common_write
= md_common_write
;
511 md
->card
.io_read
= md_common_read
;
512 md
->card
.io_write
= md_common_write
;
514 md
->attr_base
= md
->card
.cis
[0x74] | (md
->card
.cis
[0x76] << 8);
518 md_interrupt_update(md
);
520 md
->card
.slot
->card_string
= "DSCM-1xxxx Hitachi Microdrive";
524 static int dscm1xxxx_detach(void *opaque
)
526 MicroDriveState
*md
= opaque
;
531 PCMCIACardState
*dscm1xxxx_init(DriveInfo
*bdrv
)
533 MicroDriveState
*md
= (MicroDriveState
*) g_malloc0(sizeof(MicroDriveState
));
535 md
->card
.attach
= dscm1xxxx_attach
;
536 md
->card
.detach
= dscm1xxxx_detach
;
537 md
->card
.cis
= dscm1xxxx_cis
;
538 md
->card
.cis_len
= sizeof(dscm1xxxx_cis
);
540 ide_init2_with_non_qdev_drives(&md
->bus
, bdrv
, NULL
,
541 qemu_allocate_irqs(md_set_irq
, md
, 1)[0]);
542 md
->bus
.ifs
[0].drive_kind
= IDE_CFATA
;
543 md
->bus
.ifs
[0].mdata_size
= METADATA_SIZE
;
544 md
->bus
.ifs
[0].mdata_storage
= (uint8_t *) g_malloc0(METADATA_SIZE
);
546 vmstate_register(NULL
, -1, &vmstate_microdrive
, md
);