diff mbox

[5/6] prep: add pc87312 Super I/O emulation

Message ID 1331995186-18507-6-git-send-email-hpoussin@reactos.org
State New
Headers show

Commit Message

Hervé Poussineau March 17, 2012, 2:39 p.m. UTC
This provides floppy and IDE controllers as well as serial and parallel ports.
However, dynamic configuration of devices is not yet supported.

Cc: Andreas Färber <andreas.faerber@web.de>
Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
---
 Makefile.objs |    1 +
 hw/pc87312.c  |  425 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 426 insertions(+), 0 deletions(-)
 create mode 100644 hw/pc87312.c

Comments

Andreas Färber March 19, 2012, 1:15 p.m. UTC | #1
Am 17.03.2012 15:39, schrieb Hervé Poussineau:
> This provides floppy and IDE controllers as well as serial and parallel ports.
> However, dynamic configuration of devices is not yet supported.
> 
> Cc: Andreas Färber <andreas.faerber@web.de>
> Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
> ---
>  Makefile.objs |    1 +
>  hw/pc87312.c  |  425 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 426 insertions(+), 0 deletions(-)
>  create mode 100644 hw/pc87312.c

> diff --git a/hw/pc87312.c b/hw/pc87312.c
> new file mode 100644
> index 0000000..1e28dbd
> --- /dev/null
> +++ b/hw/pc87312.c
> @@ -0,0 +1,425 @@
> +/*
> + * QEMU National Semiconductor PC87312 (Super I/O)
> + *
> + * Copyright (c) 2010-2012 Herve Poussineau

FWIW mind to add

Copyright (c) 2011 Andreas Färber

?

> + *
> + * Permission is hereby granted, free of charge, to any person obtaining a copy
> + * of this software and associated documentation files (the "Software"), to deal
> + * in the Software without restriction, including without limitation the rights
> + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
> + * copies of the Software, and to permit persons to whom the Software is
> + * furnished to do so, subject to the following conditions:
> + *
> + * The above copyright notice and this permission notice shall be included in
> + * all copies or substantial portions of the Software.
> + *
> + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
> + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
> + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
> + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
> + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
> + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
> + * THE SOFTWARE.
> + */

> +//#define DEBUG_PC87312
> +
> +#ifdef DEBUG_PC87312
> +#define DPRINTF(fmt, ...) \
> +do { fprintf(stderr, "pc87312: " fmt , ## __VA_ARGS__); } while (0)
> +#else
> +#define DPRINTF(fmt, ...) \
> +do {} while (0)
> +#endif

Mid-term we should replace this through proper tracing.

> +static int pc87312_init(ISADevice *dev)
> +{
> +    PC87312State *s;
> +    ISADevice *isa;
> +    ISABus *bus;
> +    CharDriverState *chr;
> +    BlockDriverState *bs;
> +    int i;
> +
> +    s = DO_UPCAST(PC87312State, dev, dev);
> +    bus = isa_bus_from_device(dev);
> +    pc87312_hard_reset(s);
> +
> +    chr = s->parallel.chr;
> +    if (s->parallel.chr != NULL && is_parallel_enabled(s)) {

This logic still seems to be flawed: it should not depend on
s->parallel.chr. If that is NULL we need to create a null char device to
match the device that's present in hardware according to
is_parallel_enabled(s).

> +        qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); /* HACK */

HACK alarm in [PATCH]: What for?

> +        isa = isa_create(bus, "isa-parallel");
> +        qdev_prop_set_uint32(&isa->qdev, "index", 0);
> +        qdev_prop_set_uint32(&isa->qdev, "iobase", get_parallel_iobase(s));
> +        qdev_prop_set_uint32(&isa->qdev, "irq", get_parallel_irq(s));
> +        qdev_prop_set_chr(&isa->qdev, "chardev", chr);
> +        qdev_init_nofail(&isa->qdev);
> +        s->parallel.dev = &isa->qdev;
> +        DPRINTF("parallel: base 0x%x, irq %u\n",
> +                get_parallel_iobase(s), get_parallel_irq(s));
> +    }
> +
> +    for (i = 0; i < 2; i++) {
> +        chr = s->uart[i].chr;

> +        if (chr != NULL && is_uart_enabled(s, i)) {
> +            qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); /* HACK */

2x ditto.

> +            isa = isa_create(bus, "isa-serial");
> +            qdev_prop_set_uint32(&isa->qdev, "index", i);
> +            qdev_prop_set_uint32(&isa->qdev, "iobase", get_uart_iobase(s, i));
> +            qdev_prop_set_uint32(&isa->qdev, "irq", get_uart_irq(s, i));
> +            qdev_prop_set_chr(&isa->qdev, "chardev", chr);
> +            qdev_init_nofail(&isa->qdev);
> +            s->uart[i].dev = &isa->qdev;
> +            DPRINTF("uart%d: base 0x%x, irq %u\n", i,
> +                    get_uart_iobase(s, i),
> +                    get_uart_irq(s, i));
> +        }
> +    }
> +
> +    if (is_fdc_enabled(s)) {
> +        isa = isa_create(bus, "isa-fdc");
> +        qdev_prop_set_uint32(&isa->qdev, "iobase", get_fdc_iobase(s));
> +        qdev_prop_set_uint32(&isa->qdev, "irq", 6);
> +        bs = s->fdc.drive[0];
> +        if (bs != NULL) {
> +            bdrv_detach_dev(bs, bdrv_get_attached_dev(bs)); /* HACK */
> +            qdev_prop_set_drive_nofail(&isa->qdev, "driveA", bs);
> +        }
> +        bs = s->fdc.drive[1];
> +        if (bs != NULL) {
> +            bdrv_detach_dev(bs, bdrv_get_attached_dev(bs)); /* HACK */
> +            qdev_prop_set_drive_nofail(&isa->qdev, "driveB", bs);
> +        }
> +        qdev_init_nofail(&isa->qdev);
> +        s->fdc.dev = &isa->qdev;
> +        DPRINTF("fdc: base 0x%x\n", get_fdc_iobase(s));
> +    }
> +
> +    if (is_ide_enabled(s)) {
> +        isa = isa_create(bus, "isa-ide");
> +        qdev_prop_set_uint32(&isa->qdev, "iobase", get_ide_iobase(s));
> +        qdev_prop_set_uint32(&isa->qdev, "iobase2", get_ide_iobase(s) + 0x206);
> +        qdev_prop_set_uint32(&isa->qdev, "irq", 14);
> +        qdev_init_nofail(&isa->qdev);
> +        s->ide.dev = &isa->qdev;
> +        DPRINTF("ide: base 0x%x\n", get_ide_iobase(s));
> +    }
> +
> +    register_ioport_write(s->iobase, 2, 1, pc87312_ioport_write, s);
> +    register_ioport_read(s->iobase, 2, 1, pc87312_ioport_read, s);
> +    return 0;
> +}
> +
> +static const VMStateDescription vmstate_pc87312 = {
> +    .name = "pc87312",
> +    .version_id = 1,
> +    .minimum_version_id = 1,
> +    .post_load = pc87312_post_load,
> +    .fields = (VMStateField[]) {
> +        VMSTATE_UINT8(read_id_step, PC87312State),
> +        VMSTATE_UINT8(selected_index, PC87312State),
> +        VMSTATE_UINT8_ARRAY(regs, PC87312State, 3),
> +        VMSTATE_END_OF_LIST()
> +    }
> +};
> +
> +static Property pc87312_properties[] = {
> +    DEFINE_PROP_HEX32("iobase", PC87312State, iobase, 0x398),
> +    DEFINE_PROP_UINT8("config", PC87312State, config, 1),
> +    DEFINE_PROP_CHR("parallel", PC87312State, parallel.chr),
> +    DEFINE_PROP_CHR("uart1", PC87312State, uart[0].chr),
> +    DEFINE_PROP_CHR("uart2", PC87312State, uart[1].chr),
> +    DEFINE_PROP_DRIVE("floppyA", PC87312State, fdc.drive[0]),
> +    DEFINE_PROP_DRIVE("floppyB", PC87312State, fdc.drive[1]),
> +    DEFINE_PROP_END_OF_LIST()
> +};
> +
> +static void pc87312_class_initfn(ObjectClass *klass, void *data)

I always thought initfn was used for instances...

> +{
> +    DeviceClass *dc = DEVICE_CLASS(klass);
> +    ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
> +
> +    ic->init = pc87312_init;
> +    dc->reset = pc87312_reset;
> +    dc->vmsd = &vmstate_pc87312;
> +    dc->props = pc87312_properties;
> +}
> +
> +static TypeInfo pc87312_info = {
> +    .name          = "pc87312",
> +    .parent        = TYPE_ISA_DEVICE,
> +    .instance_size = sizeof(PC87312State),
> +    .class_init    = pc87312_class_initfn,
> +};
> +
> +static void pc87312_register_types(void)
> +{
> +    type_register_static(&pc87312_info);
> +}
> +
> +type_init(pc87312_register_types)
> +

Trailing empty line.

So what about the ugly ISA hot-plug issue that originally stalled our
two series? I'm missing a Change Log about that. You changed the initial
configuration to the one used by 40P firmware IIRC and stopped
supporting the chipset's reconfiguration? Either way any limitation of
this implementation should be prominently documented please.

Thanks for your work on this,

Andreas
Hervé Poussineau March 19, 2012, 6:26 p.m. UTC | #2
Andreas Färber a écrit :
> Am 17.03.2012 15:39, schrieb Hervé Poussineau:
>> This provides floppy and IDE controllers as well as serial and parallel ports.
>> However, dynamic configuration of devices is not yet supported.
>>
>> Cc: Andreas Färber <andreas.faerber@web.de>
>> Signed-off-by: Hervé Poussineau <hpoussin@reactos.org>
>> ---
>>  Makefile.objs |    1 +
>>  hw/pc87312.c  |  425 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++
>>  2 files changed, 426 insertions(+), 0 deletions(-)
>>  create mode 100644 hw/pc87312.c
> 
>> diff --git a/hw/pc87312.c b/hw/pc87312.c
>> new file mode 100644
>> index 0000000..1e28dbd
>> --- /dev/null
>> +++ b/hw/pc87312.c
>> @@ -0,0 +1,425 @@
>> +/*
>> + * QEMU National Semiconductor PC87312 (Super I/O)
>> + *
>> + * Copyright (c) 2010-2012 Herve Poussineau
> 
> FWIW mind to add
> 
> Copyright (c) 2011 Andreas Färber
> 
> ?

Yes, of course. Sorry about that.

>> +
>> +    chr = s->parallel.chr;
>> +    if (s->parallel.chr != NULL && is_parallel_enabled(s)) {
> 
> This logic still seems to be flawed: it should not depend on
> s->parallel.chr. If that is NULL we need to create a null char device to
> match the device that's present in hardware according to
> is_parallel_enabled(s).

Ok, will remove the 'chr != NULL' check.

> 
>> +        qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); /* HACK */
> 
> HACK alarm in [PATCH]: What for?

The problem is composition. Main board contains a pc87312, which 
contains a isa-parallel. Main board doesn't know the isa-parallel device.

isa-parallel device must have a chardev (set by a property). pc87312 
creates the isa-parallel device, so it must know the chardev. I did it 
also with a chardev property.
Unfortunately, a chardev can only be used once, and I have not found a 
better way to give chardev from main board to superI/O to parallel.
Do you have any better idea?


>> +    for (i = 0; i < 2; i++) {
>> +        chr = s->uart[i].chr;
> 
>> +        if (chr != NULL && is_uart_enabled(s, i)) {
>> +            qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); /* HACK */
> 
> 2x ditto.

Same answer :)

>> +
>> +static void pc87312_class_initfn(ObjectClass *klass, void *data)
> 
> I always thought initfn was used for instances...

Ok, will change to pc87312_class_init

>> +
>> +type_init(pc87312_register_types)
>> +
> 
> Trailing empty line.

Ok, will remove.

> 
> So what about the ugly ISA hot-plug issue that originally stalled our
> two series? I'm missing a Change Log about that. You changed the initial
> configuration to the one used by 40P firmware IIRC and stopped
> supporting the chipset's reconfiguration? Either way any limitation of
> this implementation should be prominently documented please.

Yes, chipset reconfiguration required ISA hot-plug issue, so I didn't 
want to wait for this serie. In commit message, I wrote "However, 
dynamic configuration of devices is not yet supported.", and you get an 
error message at each reconfiguration:

+static void reconfigure_devices(PC87312State *s)
+{
+    error_report("pc87312: unsupported device reconfiguration (%02x 
%02x %02x)",
+                 s->FER, s->FAR, s->PTR);
+}

What else should I add?
Anyway, if/when dynamic reconfiguration will be implemented, only this 
method will be changed.


About the initial configuration, it is controlled by "config" property.
In patch 6/6, when I use the Super I/O chip in 'prep' machine, I set 
this property to 13, which means fdc + serial0 + serial1 + parallel0.
For IBM 40p, the initial configuration should be 15 (fdc + ide + serial0 
+ parallel0).
So I don't think the Super I/O is exclusively tied to IBM 40p.


> 
> Thanks for your work on this,

Thanks

Hervé
diff mbox

Patch

diff --git a/Makefile.objs b/Makefile.objs
index 226b01d..232eed0 100644
--- a/Makefile.objs
+++ b/Makefile.objs
@@ -252,6 +252,7 @@  hw-obj-$(CONFIG_I8259) += i8259_common.o i8259.o
 # PPC devices
 hw-obj-$(CONFIG_PREP_PCI) += prep_pci.o
 hw-obj-$(CONFIG_I82378) += i82378.o
+hw-obj-$(CONFIG_PC87312) += pc87312.o
 # Mac shared devices
 hw-obj-$(CONFIG_MACIO) += macio.o
 hw-obj-$(CONFIG_CUDA) += cuda.o
diff --git a/hw/pc87312.c b/hw/pc87312.c
new file mode 100644
index 0000000..1e28dbd
--- /dev/null
+++ b/hw/pc87312.c
@@ -0,0 +1,425 @@ 
+/*
+ * QEMU National Semiconductor PC87312 (Super I/O)
+ *
+ * Copyright (c) 2010-2012 Herve Poussineau
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "isa.h"
+#include "fdc.h"
+#include "ide.h"
+
+//#define DEBUG_PC87312
+
+#ifdef DEBUG_PC87312
+#define DPRINTF(fmt, ...) \
+do { fprintf(stderr, "pc87312: " fmt , ## __VA_ARGS__); } while (0)
+#else
+#define DPRINTF(fmt, ...) \
+do {} while (0)
+#endif
+
+#define BADF(fmt, ...) \
+do { fprintf(stderr, "pc87312 ERROR: " fmt , ## __VA_ARGS__); } while (0)
+
+#define REG_FER 0
+#define REG_FAR 1
+#define REG_PTR 2
+
+#define FER regs[REG_FER]
+#define FAR regs[REG_FAR]
+#define PTR regs[REG_PTR]
+
+#define FER_PARALLEL_EN   0x01
+#define FER_UART1_EN      0x02
+#define FER_UART2_EN      0x04
+#define FER_FDC_EN        0x08
+#define FER_FDC_4         0x10
+#define FER_FDC_ADDR      0x20
+#define FER_IDE_EN        0x40
+#define FER_IDE_ADDR      0x80
+
+#define FAR_PARALLEL_ADDR 0x03
+#define FAR_UART1_ADDR    0x0C
+#define FAR_UART2_ADDR    0x30
+#define FAR_UART_3_4      0xC0
+
+#define PTR_POWER_DOWN    0x01
+#define PTR_CLOCK_DOWN    0x02
+#define PTR_PWDN          0x04
+#define PTR_IRQ_5_7       0x08
+#define PTR_UART1_TEST    0x10
+#define PTR_UART2_TEST    0x20
+#define PTR_LOCK_CONF     0x40
+#define PTR_EPP_MODE      0x80
+
+typedef struct PC87312State {
+    ISADevice dev;
+    uint32_t iobase;
+
+    uint8_t config; /* initial configuration */
+
+    struct {
+        DeviceState *dev;
+        CharDriverState *chr;
+    } parallel;
+
+    struct {
+        DeviceState *dev;
+        CharDriverState *chr;
+    } uart[2];
+
+    struct {
+        DeviceState *dev;
+        BlockDriverState *drive[2];
+        uint32_t base;
+    } fdc;
+
+    struct {
+        DeviceState *dev;
+        uint32_t base;
+    } ide;
+
+    uint8_t read_id_step;
+    uint8_t selected_index;
+
+    uint8_t regs[3];
+} PC87312State;
+
+
+/* Parallel port */
+
+static inline bool is_parallel_enabled(PC87312State *s)
+{
+    return s->FER & FER_PARALLEL_EN;
+}
+
+static const uint32_t parallel_base[] = { 0x378, 0x3bc, 0x278, 0x00 };
+
+static inline uint32_t get_parallel_iobase(PC87312State *s)
+{
+    return parallel_base[s->FAR & FAR_PARALLEL_ADDR];
+}
+
+static const uint32_t parallel_irq[] = { 5, 7, 5, 0 };
+
+static inline uint32_t get_parallel_irq(PC87312State *s)
+{
+    int idx;
+    idx = (s->FAR & FAR_PARALLEL_ADDR);
+    if (idx == 0) {
+        return (s->PTR & PTR_IRQ_5_7) ? 7 : 5;
+    } else {
+        return parallel_irq[idx];
+    }
+}
+
+static inline bool is_parallel_epp(PC87312State *s)
+{
+    return s->PTR & PTR_EPP_MODE;
+}
+
+
+/* UARTs */
+
+static const uint32_t uart_base[2][4] = {
+    { 0x3e8, 0x338, 0x2e8, 0x220 },
+    { 0x2e8, 0x238, 0x2e0, 0x228 }
+};
+
+static inline uint32_t get_uart_iobase(PC87312State *s, int i)
+{
+    int idx;
+    idx = (s->FAR >> (2 * i + 2)) & 0x3;
+    if (idx == 0) {
+        return 0x3f8;
+    } else if (idx == 1) {
+        return 0x2f8;
+    } else {
+        return uart_base[idx & 1][(s->FAR & FAR_UART_3_4) >> 6];
+    }
+}
+
+static inline uint32_t get_uart_irq(PC87312State *s, int i)
+{
+    int idx;
+    idx = (s->FAR >> (2 * i + 2)) & 0x3;
+    return (idx & 1) ? 3 : 4;
+}
+
+static inline bool is_uart_enabled(PC87312State *s, int i)
+{
+    return s->FER & (FER_UART1_EN << i);
+}
+
+
+/* Floppy controller */
+
+static inline bool is_fdc_enabled(PC87312State *s)
+{
+    return s->FER & FER_FDC_EN;
+}
+
+static inline uint32_t get_fdc_iobase(PC87312State *s)
+{
+    return (s->FER & FER_FDC_ADDR) ? 0x370 : 0x3f0;
+}
+
+
+/* IDE controller */
+
+static inline bool is_ide_enabled(PC87312State *s)
+{
+    return s->FER & FER_IDE_EN;
+}
+
+static inline uint32_t get_ide_iobase(PC87312State *s)
+{
+    return (s->FER & FER_IDE_ADDR) ? 0x170 : 0x1f0;
+}
+
+
+static void reconfigure_devices(PC87312State *s)
+{
+    error_report("pc87312: unsupported device reconfiguration (%02x %02x %02x)",
+                 s->FER, s->FAR, s->PTR);
+}
+
+static void pc87312_soft_reset(PC87312State *s)
+{
+    static const uint8_t fer_init[] = {
+        0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4f, 0x4b, 0x4b,
+        0x4b, 0x4b, 0x4b, 0x4b, 0x0f, 0x0f, 0x0f, 0x0f,
+        0x49, 0x49, 0x49, 0x49, 0x07, 0x07, 0x07, 0x07,
+        0x47, 0x47, 0x47, 0x47, 0x47, 0x47, 0x08, 0x00,
+    };
+    static const uint8_t far_init[] = {
+        0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x00, 0x01,
+        0x01, 0x09, 0x08, 0x08, 0x10, 0x11, 0x39, 0x24,
+        0x00, 0x01, 0x01, 0x00, 0x10, 0x11, 0x39, 0x24,
+        0x10, 0x11, 0x11, 0x39, 0x24, 0x38, 0x10, 0x10,
+    };
+    static const uint8_t ptr_init[] = {
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
+    };
+
+    s->read_id_step = 0;
+    s->selected_index = REG_FER;
+
+    s->FER = fer_init[s->config & 0x1f];
+    s->FAR = far_init[s->config & 0x1f];
+    s->PTR = ptr_init[s->config & 0x1f];
+}
+
+static void pc87312_hard_reset(PC87312State *s)
+{
+    pc87312_soft_reset(s);
+}
+
+static void pc87312_ioport_write(void *opaque, uint32_t addr, uint32_t val)
+{
+    PC87312State *s = opaque;
+
+    DPRINTF("%s: write %x at %x\n", __func__, val, addr);
+
+    if ((addr & 1) == 0) {
+        /* Index register */
+        s->read_id_step = 2;
+        s->selected_index = val;
+    } else {
+        /* Data register */
+        if (s->selected_index < 3) {
+            s->regs[s->selected_index] = val;
+            reconfigure_devices(s);
+        }
+    }
+}
+
+static uint32_t pc87312_ioport_read(void *opaque, uint32_t addr)
+{
+    PC87312State *s = opaque;
+    uint32_t val;
+
+    if ((addr & 1) == 0) {
+        /* Index register */
+        if (s->read_id_step++ == 0) {
+            val = 0x88;
+        } else if (s->read_id_step++ == 1) {
+            val = 0;
+        } else {
+            val = s->selected_index;
+        }
+    } else {
+        /* Data register */
+        if (s->selected_index < 3) {
+            val = s->regs[s->selected_index];
+        } else {
+            /* Invalid selected index */
+            val = 0;
+        }
+    }
+
+    DPRINTF("%s: read %x at %x\n", __func__, val, addr);
+    return val;
+}
+
+static int pc87312_post_load(void *opaque, int version_id)
+{
+    PC87312State *s = opaque;
+    reconfigure_devices(s);
+    return 0;
+}
+
+static void pc87312_reset(DeviceState *d)
+{
+    PC87312State *s = container_of(d, PC87312State, dev.qdev);
+    pc87312_soft_reset(s);
+}
+
+static int pc87312_init(ISADevice *dev)
+{
+    PC87312State *s;
+    ISADevice *isa;
+    ISABus *bus;
+    CharDriverState *chr;
+    BlockDriverState *bs;
+    int i;
+
+    s = DO_UPCAST(PC87312State, dev, dev);
+    bus = isa_bus_from_device(dev);
+    pc87312_hard_reset(s);
+
+    chr = s->parallel.chr;
+    if (s->parallel.chr != NULL && is_parallel_enabled(s)) {
+        qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); /* HACK */
+        isa = isa_create(bus, "isa-parallel");
+        qdev_prop_set_uint32(&isa->qdev, "index", 0);
+        qdev_prop_set_uint32(&isa->qdev, "iobase", get_parallel_iobase(s));
+        qdev_prop_set_uint32(&isa->qdev, "irq", get_parallel_irq(s));
+        qdev_prop_set_chr(&isa->qdev, "chardev", chr);
+        qdev_init_nofail(&isa->qdev);
+        s->parallel.dev = &isa->qdev;
+        DPRINTF("parallel: base 0x%x, irq %u\n",
+                get_parallel_iobase(s), get_parallel_irq(s));
+    }
+
+    for (i = 0; i < 2; i++) {
+        chr = s->uart[i].chr;
+        if (chr != NULL && is_uart_enabled(s, i)) {
+            qemu_chr_add_handlers(chr, NULL, NULL, NULL, NULL); /* HACK */
+            isa = isa_create(bus, "isa-serial");
+            qdev_prop_set_uint32(&isa->qdev, "index", i);
+            qdev_prop_set_uint32(&isa->qdev, "iobase", get_uart_iobase(s, i));
+            qdev_prop_set_uint32(&isa->qdev, "irq", get_uart_irq(s, i));
+            qdev_prop_set_chr(&isa->qdev, "chardev", chr);
+            qdev_init_nofail(&isa->qdev);
+            s->uart[i].dev = &isa->qdev;
+            DPRINTF("uart%d: base 0x%x, irq %u\n", i,
+                    get_uart_iobase(s, i),
+                    get_uart_irq(s, i));
+        }
+    }
+
+    if (is_fdc_enabled(s)) {
+        isa = isa_create(bus, "isa-fdc");
+        qdev_prop_set_uint32(&isa->qdev, "iobase", get_fdc_iobase(s));
+        qdev_prop_set_uint32(&isa->qdev, "irq", 6);
+        bs = s->fdc.drive[0];
+        if (bs != NULL) {
+            bdrv_detach_dev(bs, bdrv_get_attached_dev(bs)); /* HACK */
+            qdev_prop_set_drive_nofail(&isa->qdev, "driveA", bs);
+        }
+        bs = s->fdc.drive[1];
+        if (bs != NULL) {
+            bdrv_detach_dev(bs, bdrv_get_attached_dev(bs)); /* HACK */
+            qdev_prop_set_drive_nofail(&isa->qdev, "driveB", bs);
+        }
+        qdev_init_nofail(&isa->qdev);
+        s->fdc.dev = &isa->qdev;
+        DPRINTF("fdc: base 0x%x\n", get_fdc_iobase(s));
+    }
+
+    if (is_ide_enabled(s)) {
+        isa = isa_create(bus, "isa-ide");
+        qdev_prop_set_uint32(&isa->qdev, "iobase", get_ide_iobase(s));
+        qdev_prop_set_uint32(&isa->qdev, "iobase2", get_ide_iobase(s) + 0x206);
+        qdev_prop_set_uint32(&isa->qdev, "irq", 14);
+        qdev_init_nofail(&isa->qdev);
+        s->ide.dev = &isa->qdev;
+        DPRINTF("ide: base 0x%x\n", get_ide_iobase(s));
+    }
+
+    register_ioport_write(s->iobase, 2, 1, pc87312_ioport_write, s);
+    register_ioport_read(s->iobase, 2, 1, pc87312_ioport_read, s);
+    return 0;
+}
+
+static const VMStateDescription vmstate_pc87312 = {
+    .name = "pc87312",
+    .version_id = 1,
+    .minimum_version_id = 1,
+    .post_load = pc87312_post_load,
+    .fields = (VMStateField[]) {
+        VMSTATE_UINT8(read_id_step, PC87312State),
+        VMSTATE_UINT8(selected_index, PC87312State),
+        VMSTATE_UINT8_ARRAY(regs, PC87312State, 3),
+        VMSTATE_END_OF_LIST()
+    }
+};
+
+static Property pc87312_properties[] = {
+    DEFINE_PROP_HEX32("iobase", PC87312State, iobase, 0x398),
+    DEFINE_PROP_UINT8("config", PC87312State, config, 1),
+    DEFINE_PROP_CHR("parallel", PC87312State, parallel.chr),
+    DEFINE_PROP_CHR("uart1", PC87312State, uart[0].chr),
+    DEFINE_PROP_CHR("uart2", PC87312State, uart[1].chr),
+    DEFINE_PROP_DRIVE("floppyA", PC87312State, fdc.drive[0]),
+    DEFINE_PROP_DRIVE("floppyB", PC87312State, fdc.drive[1]),
+    DEFINE_PROP_END_OF_LIST()
+};
+
+static void pc87312_class_initfn(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    ISADeviceClass *ic = ISA_DEVICE_CLASS(klass);
+
+    ic->init = pc87312_init;
+    dc->reset = pc87312_reset;
+    dc->vmsd = &vmstate_pc87312;
+    dc->props = pc87312_properties;
+}
+
+static TypeInfo pc87312_info = {
+    .name          = "pc87312",
+    .parent        = TYPE_ISA_DEVICE,
+    .instance_size = sizeof(PC87312State),
+    .class_init    = pc87312_class_initfn,
+};
+
+static void pc87312_register_types(void)
+{
+    type_register_static(&pc87312_info);
+}
+
+type_init(pc87312_register_types)
+