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
27 #include <hw/pcmcia.h>
29 #include "block_int.h"
32 #include <hw/ide/internal.h>
34 /***********************************************************/
35 /* CF-ATA Microdrive */
37 #define METADATA_SIZE 0x20
39 /* DSCM-1XXXX Microdrive hard disk with CF+ II / PCMCIA interface. */
56 /* Register bitfields */
83 static inline void md_interrupt_update(MicroDriveState *s)
88 qemu_set_irq(s->card.slot->irq,
89 !(s->stat & STAT_INT) && /* Inverted */
90 !(s->ctrl & (CTRL_IEN | CTRL_SRST)) &&
91 !(s->opt & OPT_SRESET));
94 static void md_set_irq(void *opaque, int irq, int level)
96 MicroDriveState *s = opaque;
100 s->stat &= ~STAT_INT;
102 md_interrupt_update(s);
105 static void md_reset(MicroDriveState *s)
107 s->opt = OPT_MODE_MMAP;
112 ide_bus_reset(&s->bus);
115 static uint8_t md_attr_read(void *opaque, uint32_t at)
117 MicroDriveState *s = opaque;
118 if (at < s->attr_base) {
119 if (at < s->card.cis_len)
120 return s->card.cis[at];
128 case 0x00: /* Configuration Option Register */
130 case 0x02: /* Card Configuration Status Register */
131 if (s->ctrl & CTRL_IEN)
132 return s->stat & ~STAT_INT;
135 case 0x04: /* Pin Replacement Register */
136 return (s->pins & PINS_CRDY) | 0x0c;
137 case 0x06: /* Socket and Copy Register */
141 printf("%s: Bad attribute space register %02x\n", __FUNCTION__, at);
148 static void md_attr_write(void *opaque, uint32_t at, uint8_t value)
150 MicroDriveState *s = opaque;
154 case 0x00: /* Configuration Option Register */
155 s->opt = value & 0xcf;
156 if (value & OPT_SRESET)
158 md_interrupt_update(s);
160 case 0x02: /* Card Configuration Status Register */
161 if ((s->stat ^ value) & STAT_PWRDWN)
162 s->pins |= PINS_CRDY;
164 s->stat |= value & 0x74;
165 md_interrupt_update(s);
166 /* Word 170 in Identify Device must be equal to STAT_XE */
168 case 0x04: /* Pin Replacement Register */
169 s->pins &= PINS_CRDY;
170 s->pins |= value & PINS_MRDY;
172 case 0x06: /* Socket and Copy Register */
175 printf("%s: Bad attribute space register %02x\n", __FUNCTION__, at);
179 static uint16_t md_common_read(void *opaque, uint32_t at)
181 MicroDriveState *s = opaque;
186 switch (s->opt & OPT_MODE) {
188 if ((at & ~0x3ff) == 0x400)
191 case OPT_MODE_IOMAP16:
194 case OPT_MODE_IOMAP1:
195 if ((at & ~0xf) == 0x3f0)
197 else if ((at & ~0xf) == 0x1f0)
200 case OPT_MODE_IOMAP2:
201 if ((at & ~0xf) == 0x370)
203 else if ((at & ~0xf) == 0x170)
208 case 0x0: /* Even RD Data */
210 return ide_data_readw(&s->bus, 0);
212 /* TODO: 8-bit accesses */
216 s->io = ide_data_readw(&s->bus, 0);
219 s->cycle = !s->cycle;
221 case 0x9: /* Odd RD Data */
223 case 0xd: /* Error */
224 return ide_ioport_read(&s->bus, 0x1);
225 case 0xe: /* Alternate Status */
226 ifs = idebus_active_if(&s->bus);
231 case 0xf: /* Device Address */
232 ifs = idebus_active_if(&s->bus);
233 return 0xc2 | ((~ifs->select << 2) & 0x3c);
235 return ide_ioport_read(&s->bus, at);
241 static void md_common_write(void *opaque, uint32_t at, uint16_t value)
243 MicroDriveState *s = opaque;
246 switch (s->opt & OPT_MODE) {
248 if ((at & ~0x3ff) == 0x400)
251 case OPT_MODE_IOMAP16:
254 case OPT_MODE_IOMAP1:
255 if ((at & ~0xf) == 0x3f0)
257 else if ((at & ~0xf) == 0x1f0)
260 case OPT_MODE_IOMAP2:
261 if ((at & ~0xf) == 0x370)
263 else if ((at & ~0xf) == 0x170)
268 case 0x0: /* Even WR Data */
270 ide_data_writew(&s->bus, 0, value);
273 /* TODO: 8-bit accesses */
275 ide_data_writew(&s->bus, 0, s->io | (value << 8));
277 s->io = value & 0xff;
278 s->cycle = !s->cycle;
281 s->io = value & 0xff;
282 s->cycle = !s->cycle;
284 case 0xd: /* Features */
285 ide_ioport_write(&s->bus, 0x1, value);
287 case 0xe: /* Device Control */
289 if (value & CTRL_SRST)
291 md_interrupt_update(s);
294 if (s->stat & STAT_PWRDWN) {
295 s->pins |= PINS_CRDY;
296 s->stat &= ~STAT_PWRDWN;
298 ide_ioport_write(&s->bus, at, value);
302 static const VMStateDescription vmstate_microdrive = {
303 .name = "microdrive",
305 .minimum_version_id = 0,
306 .minimum_version_id_old = 0,
307 .fields = (VMStateField []) {
308 VMSTATE_UINT8(opt, MicroDriveState),
309 VMSTATE_UINT8(stat, MicroDriveState),
310 VMSTATE_UINT8(pins, MicroDriveState),
311 VMSTATE_UINT8(ctrl, MicroDriveState),
312 VMSTATE_UINT16(io, MicroDriveState),
313 VMSTATE_UINT8(cycle, MicroDriveState),
314 VMSTATE_IDE_BUS(bus, MicroDriveState),
315 VMSTATE_IDE_DRIVES(bus.ifs, MicroDriveState),
316 VMSTATE_END_OF_LIST()
320 static const uint8_t dscm1xxxx_cis[0x14a] = {
321 [0x000] = CISTPL_DEVICE, /* 5V Device Information */
322 [0x002] = 0x03, /* Tuple length = 4 bytes */
323 [0x004] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
324 [0x006] = 0x01, /* Size = 2K bytes */
325 [0x008] = CISTPL_ENDMARK,
327 [0x00a] = CISTPL_DEVICE_OC, /* Additional Device Information */
328 [0x00c] = 0x04, /* Tuple length = 4 byest */
329 [0x00e] = 0x03, /* Conditions: Ext = 0, Vcc 3.3V, MWAIT = 1 */
330 [0x010] = 0xdb, /* ID: DTYPE_FUNCSPEC, non WP, DSPEED_150NS */
331 [0x012] = 0x01, /* Size = 2K bytes */
332 [0x014] = CISTPL_ENDMARK,
334 [0x016] = CISTPL_JEDEC_C, /* JEDEC ID */
335 [0x018] = 0x02, /* Tuple length = 2 bytes */
336 [0x01a] = 0xdf, /* PC Card ATA with no Vpp required */
339 [0x01e] = CISTPL_MANFID, /* Manufacture ID */
340 [0x020] = 0x04, /* Tuple length = 4 bytes */
341 [0x022] = 0xa4, /* TPLMID_MANF = 00a4 (IBM) */
343 [0x026] = 0x00, /* PLMID_CARD = 0000 */
346 [0x02a] = CISTPL_VERS_1, /* Level 1 Version */
347 [0x02c] = 0x12, /* Tuple length = 23 bytes */
348 [0x02e] = 0x04, /* Major Version = JEIDA 4.2 / PCMCIA 2.1 */
349 [0x030] = 0x01, /* Minor Version = 1 */
365 [0x050] = CISTPL_ENDMARK,
367 [0x052] = CISTPL_FUNCID, /* Function ID */
368 [0x054] = 0x02, /* Tuple length = 2 bytes */
369 [0x056] = 0x04, /* TPLFID_FUNCTION = Fixed Disk */
370 [0x058] = 0x01, /* TPLFID_SYSINIT: POST = 1, ROM = 0 */
372 [0x05a] = CISTPL_FUNCE, /* Function Extension */
373 [0x05c] = 0x02, /* Tuple length = 2 bytes */
374 [0x05e] = 0x01, /* TPLFE_TYPE = Disk Device Interface */
375 [0x060] = 0x01, /* TPLFE_DATA = PC Card ATA Interface */
377 [0x062] = CISTPL_FUNCE, /* Function Extension */
378 [0x064] = 0x03, /* Tuple length = 3 bytes */
379 [0x066] = 0x02, /* TPLFE_TYPE = Basic PC Card ATA Interface */
380 [0x068] = 0x08, /* TPLFE_DATA: Rotating, Unique, Single */
381 [0x06a] = 0x0f, /* TPLFE_DATA: Sleep, Standby, Idle, Auto */
383 [0x06c] = CISTPL_CONFIG, /* Configuration */
384 [0x06e] = 0x05, /* Tuple length = 5 bytes */
385 [0x070] = 0x01, /* TPCC_RASZ = 2 bytes, TPCC_RMSZ = 1 byte */
386 [0x072] = 0x07, /* TPCC_LAST = 7 */
387 [0x074] = 0x00, /* TPCC_RADR = 0200 */
389 [0x078] = 0x0f, /* TPCC_RMSK = 200, 202, 204, 206 */
391 [0x07a] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
392 [0x07c] = 0x0b, /* Tuple length = 11 bytes */
393 [0x07e] = 0xc0, /* TPCE_INDX = Memory Mode, Default, Iface */
394 [0x080] = 0xc0, /* TPCE_IF = Memory, no BVDs, no WP, READY */
395 [0x082] = 0xa1, /* TPCE_FS = Vcc only, no I/O, Memory, Misc */
396 [0x084] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
397 [0x086] = 0x55, /* NomV: 5.0 V */
398 [0x088] = 0x4d, /* MinV: 4.5 V */
399 [0x08a] = 0x5d, /* MaxV: 5.5 V */
400 [0x08c] = 0x4e, /* Peakl: 450 mA */
401 [0x08e] = 0x08, /* TPCE_MS = 1 window, 1 byte, Host address */
402 [0x090] = 0x00, /* Window descriptor: Window length = 0 */
403 [0x092] = 0x20, /* TPCE_MI: support power down mode, RW */
405 [0x094] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
406 [0x096] = 0x06, /* Tuple length = 6 bytes */
407 [0x098] = 0x00, /* TPCE_INDX = Memory Mode, no Default */
408 [0x09a] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
409 [0x09c] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
410 [0x09e] = 0xb5, /* NomV: 3.3 V */
412 [0x0a2] = 0x3e, /* Peakl: 350 mA */
414 [0x0a4] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
415 [0x0a6] = 0x0d, /* Tuple length = 13 bytes */
416 [0x0a8] = 0xc1, /* TPCE_INDX = I/O and Memory Mode, Default */
417 [0x0aa] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
418 [0x0ac] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
419 [0x0ae] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
420 [0x0b0] = 0x55, /* NomV: 5.0 V */
421 [0x0b2] = 0x4d, /* MinV: 4.5 V */
422 [0x0b4] = 0x5d, /* MaxV: 5.5 V */
423 [0x0b6] = 0x4e, /* Peakl: 450 mA */
424 [0x0b8] = 0x64, /* TPCE_IO = 16-byte boundary, 16/8 accesses */
425 [0x0ba] = 0xf0, /* TPCE_IR = MASK, Level, Pulse, Share */
426 [0x0bc] = 0xff, /* IRQ0..IRQ7 supported */
427 [0x0be] = 0xff, /* IRQ8..IRQ15 supported */
428 [0x0c0] = 0x20, /* TPCE_MI = support power down mode */
430 [0x0c2] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
431 [0x0c4] = 0x06, /* Tuple length = 6 bytes */
432 [0x0c6] = 0x01, /* TPCE_INDX = I/O and Memory Mode */
433 [0x0c8] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
434 [0x0ca] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
435 [0x0cc] = 0xb5, /* NomV: 3.3 V */
437 [0x0d0] = 0x3e, /* Peakl: 350 mA */
439 [0x0d2] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
440 [0x0d4] = 0x12, /* Tuple length = 18 bytes */
441 [0x0d6] = 0xc2, /* TPCE_INDX = I/O Primary Mode */
442 [0x0d8] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
443 [0x0da] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
444 [0x0dc] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
445 [0x0de] = 0x55, /* NomV: 5.0 V */
446 [0x0e0] = 0x4d, /* MinV: 4.5 V */
447 [0x0e2] = 0x5d, /* MaxV: 5.5 V */
448 [0x0e4] = 0x4e, /* Peakl: 450 mA */
449 [0x0e6] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
450 [0x0e8] = 0x61, /* Range: 2 fields, 2 bytes addr, 1 byte len */
451 [0x0ea] = 0xf0, /* Field 1 address = 0x01f0 */
453 [0x0ee] = 0x07, /* Address block length = 8 */
454 [0x0f0] = 0xf6, /* Field 2 address = 0x03f6 */
456 [0x0f4] = 0x01, /* Address block length = 2 */
457 [0x0f6] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
458 [0x0f8] = 0x20, /* TPCE_MI = support power down mode */
460 [0x0fa] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
461 [0x0fc] = 0x06, /* Tuple length = 6 bytes */
462 [0x0fe] = 0x02, /* TPCE_INDX = I/O Primary Mode, no Default */
463 [0x100] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
464 [0x102] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
465 [0x104] = 0xb5, /* NomV: 3.3 V */
467 [0x108] = 0x3e, /* Peakl: 350 mA */
469 [0x10a] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
470 [0x10c] = 0x12, /* Tuple length = 18 bytes */
471 [0x10e] = 0xc3, /* TPCE_INDX = I/O Secondary Mode, Default */
472 [0x110] = 0x41, /* TPCE_IF = I/O and Memory, no BVD, no WP */
473 [0x112] = 0x99, /* TPCE_FS = Vcc only, I/O, Interrupt, Misc */
474 [0x114] = 0x27, /* NomV = 1, MinV = 1, MaxV = 1, Peakl = 1 */
475 [0x116] = 0x55, /* NomV: 5.0 V */
476 [0x118] = 0x4d, /* MinV: 4.5 V */
477 [0x11a] = 0x5d, /* MaxV: 5.5 V */
478 [0x11c] = 0x4e, /* Peakl: 450 mA */
479 [0x11e] = 0xea, /* TPCE_IO = 1K boundary, 16/8 access, Range */
480 [0x120] = 0x61, /* Range: 2 fields, 2 byte addr, 1 byte len */
481 [0x122] = 0x70, /* Field 1 address = 0x0170 */
483 [0x126] = 0x07, /* Address block length = 8 */
484 [0x128] = 0x76, /* Field 2 address = 0x0376 */
486 [0x12c] = 0x01, /* Address block length = 2 */
487 [0x12e] = 0xee, /* TPCE_IR = IRQ E, Level, Pulse, Share */
488 [0x130] = 0x20, /* TPCE_MI = support power down mode */
490 [0x132] = CISTPL_CFTABLE_ENTRY, /* 16-bit PC Card Configuration */
491 [0x134] = 0x06, /* Tuple length = 6 bytes */
492 [0x136] = 0x03, /* TPCE_INDX = I/O Secondary Mode */
493 [0x138] = 0x01, /* TPCE_FS = Vcc only, no I/O, no Memory */
494 [0x13a] = 0x21, /* NomV = 1, MinV = 0, MaxV = 0, Peakl = 1 */
495 [0x13c] = 0xb5, /* NomV: 3.3 V */
497 [0x140] = 0x3e, /* Peakl: 350 mA */
499 [0x142] = CISTPL_NO_LINK, /* No Link */
500 [0x144] = 0x00, /* Tuple length = 0 bytes */
502 [0x146] = CISTPL_END, /* Tuple End */
505 static int dscm1xxxx_attach(void *opaque)
507 MicroDriveState *md = opaque;
508 md->card.attr_read = md_attr_read;
509 md->card.attr_write = md_attr_write;
510 md->card.common_read = md_common_read;
511 md->card.common_write = md_common_write;
512 md->card.io_read = md_common_read;
513 md->card.io_write = md_common_write;
515 md->attr_base = md->card.cis[0x74] | (md->card.cis[0x76] << 8);
519 md_interrupt_update(md);
521 md->card.slot->card_string = "DSCM-1xxxx Hitachi Microdrive";
525 static int dscm1xxxx_detach(void *opaque)
527 MicroDriveState *md = opaque;
532 PCMCIACardState *dscm1xxxx_init(DriveInfo *bdrv)
534 MicroDriveState *md = (MicroDriveState *) qemu_mallocz(sizeof(MicroDriveState));
536 md->card.attach = dscm1xxxx_attach;
537 md->card.detach = dscm1xxxx_detach;
538 md->card.cis = dscm1xxxx_cis;
539 md->card.cis_len = sizeof(dscm1xxxx_cis);
541 ide_init2_with_non_qdev_drives(&md->bus, bdrv, NULL,
542 qemu_allocate_irqs(md_set_irq, md, 1)[0]);
543 md->bus.ifs[0].drive_kind = IDE_CFATA;
544 md->bus.ifs[0].mdata_size = METADATA_SIZE;
545 md->bus.ifs[0].mdata_storage = (uint8_t *) qemu_mallocz(METADATA_SIZE);
547 vmstate_register(NULL, -1, &vmstate_microdrive, md);