Skip to contentSkip to conversation

hw/sd/milkymist: Do not create SD card within the SDHCI controller

PATCH In review Philippe Mathieu-Daudé 4 participants 15 messages Jul 5, 2020 – Jul 7, 2020

lore
Share & tools
lore
#0 lore raw

cover hw/sd/milkymist: Do not create SD card within the SDHCI controller

SDHCI controllers provide a SD Bus to plug SD cards, but don't come with SD card plugged in :)

This series move the SD card creation to the machine/board code.

Regards,

Phil.

Philippe Mathieu-Daudé (4):
hw/lm32/milkymist: Un-inline milkymist_memcard_create() hw/lm32/milkymist: Comment to remember some IRQs lines are left
unwired
hw/sd/milkymist: Create the SDBus at init() hw/sd/milkymist: Do not create SD card within the SDHCI controller

 hw/lm32/milkymist-hw.h    | 11 --------
 hw/lm32/milkymist.c       | 25 +++++++++++++++++
 hw/sd/milkymist-memcard.c | 57 ++++++++++++++++++++++-----------------
 3 files changed, 58 insertions(+), 35 deletions(-)
Signature · 1 line
2.21.3
#1 lore raw

PATCH 1/4 hw/lm32/milkymist: Un-inline milkymist_memcard_create()

As we will modify milkymist_memcard_create(), move it first to the source file where it is used.

---
 hw/lm32/milkymist-hw.h | 11 -----------
 hw/lm32/milkymist.c    | 11 +++++++++++
 2 files changed, 11 insertions(+), 11 deletions(-)
hw/lm32/milkymist-hw.h +0 -11
index 05e2c2a5a7..5dca5d52f5 100644
@@ -31,17 +31,6 @@ static inline DeviceState *milkymist_hpdmc_create(hwaddr base)
     return dev;
 }
 
-static inline DeviceState *milkymist_memcard_create(hwaddr base)
-{
-    DeviceState *dev;
-
-    dev = qdev_new("milkymist-memcard");
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
-    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
-
-    return dev;
-}
-
 static inline DeviceState *milkymist_vgafb_create(hwaddr base,
         uint32_t fb_offset, uint32_t fb_mask)
 {
hw/lm32/milkymist.c +11 -0
index 85913bb68b..469e3c4322 100644
@@ -80,6 +80,17 @@ static void main_cpu_reset(void *opaque)
     env->deba = reset_info->flash_base;
 }
 
+static DeviceState *milkymist_memcard_create(hwaddr base)
+{
+    DeviceState *dev;
+
+    dev = qdev_new("milkymist-memcard");
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+
+    return dev;
+}
+
 static void
 milkymist_init(MachineState *machine)
 {
Signature · 1 line
2.21.3
Reviewed-by on v1 1/4
#2 lore raw

Re: [PATCH 1/4] hw/lm32/milkymist: Un-inline milkymist_memcard_create()

Quoted 5 lines · Philippe Mathieu-Daudé

As we will modify milkymist_memcard_create(), move it first to the source file where it is used.

Alistair

Quoted 53 lines
---
 hw/lm32/milkymist-hw.h | 11 -----------
 hw/lm32/milkymist.c    | 11 +++++++++++
 2 files changed, 11 insertions(+), 11 deletions(-)
hw/lm32/milkymist-hw.h +0 -11
index 05e2c2a5a7..5dca5d52f5 100644
@@ -31,17 +31,6 @@ static inline DeviceState *milkymist_hpdmc_create(hwaddr base)
     return dev;
 }
 
-static inline DeviceState *milkymist_memcard_create(hwaddr base)
-{
-    DeviceState *dev;
-
-    dev = qdev_new("milkymist-memcard");
-    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
-    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
-
-    return dev;
-}
-
 static inline DeviceState *milkymist_vgafb_create(hwaddr base,
         uint32_t fb_offset, uint32_t fb_mask)
 {
hw/lm32/milkymist.c +11 -1
index 85913bb68b..469e3c4322 100644
@@ -80,6 +80,17 @@ static void main_cpu_reset(void *opaque)
     env->deba = reset_info->flash_base;
 }
 
+static DeviceState *milkymist_memcard_create(hwaddr base)
+{
+    DeviceState *dev;
+
+    dev = qdev_new("milkymist-memcard");
+    sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
+    sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+
+    return dev;
+}
+
 static void
 milkymist_init(MachineState *machine)
 {
--

2.21.3

#3 lore raw

PATCH 2/4 hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

In reply to Philippe Mathieu-Daudé

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
hw/lm32/milkymist.c +1 -0
index 469e3c4322..117973c967 100644
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
     return dev;
 }
Signature · 1 line
2.21.3
#4 lore raw

Re: [PATCH 2/4] hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

Quoted 5 lines · Philippe Mathieu-Daudé

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

I'm not convinced adding fixmes or todos in the code is the right direction. It would be better to file bugs or use some other more official tracking mechanism.

Alistair

Quoted 20 lines
---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
hw/lm32/milkymist.c +1 -1
index 469e3c4322..117973c967 100644
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
     return dev;
 }
--

2.21.3

#5 lore raw

Re: [PATCH 2/4] hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

Quoted 10 lines · Alistair Francis
Quoted 5 lines · Philippe Mathieu-Daudé

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

I'm not convinced adding fixmes or todos in the code is the right direction. It would be better to file bugs or use some other more official tracking mechanism.

This code is orphan :S

I'll fill a launchpad bug ticket.

OTOH we could also log UNIMP for lost IRQs (triggered but no handler registered).

Quoted 24 lines

Alistair

Quoted 20 lines
---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
hw/lm32/milkymist.c +1 -1
index 469e3c4322..117973c967 100644
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
     return dev;
 }
--

2.21.3

#6 lore raw

Re: [PATCH 2/4] hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 7 lines · Alistair Francis
Quoted 5 lines · Philippe Mathieu-Daudé

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

Quoted 7 lines
Quoted 3 lines

I'm not convinced adding fixmes or todos in the code is the right direction. It would be better to file bugs or use some other more official tracking mechanism.

This code is orphan :S

I'll fill a launchpad bug ticket.

I also mean in general (you have some other patches that add TODOs or FIXMEs).

Quoted 3 lines

OTOH we could also log UNIMP for lost IRQs (triggered but no handler registered).

That would also work.

Alistair

Quoted 25 lines
Quoted 25 lines

Alistair

Quoted 20 lines
---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
hw/lm32/milkymist.c +1 -1
index 469e3c4322..117973c967 100644
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
     return dev;
 }
--

2.21.3

#7 lore raw

Re: [PATCH 2/4] hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

Quoted 14 lines · Alistair Francis
Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Alistair Francis
Quoted 5 lines · Philippe Mathieu-Daudé

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

I'm not convinced adding fixmes or todos in the code is the right direction. It would be better to file bugs or use some other more official tracking mechanism.

Quoted 5 lines
Quoted 3 lines

This code is orphan :S

I'll fill a launchpad bug ticket.

I also mean in general (you have some other patches that add TODOs or FIXMEs).

Not all developers look at launchpad, while all of them read the code ;)

$ git grep -E '(TODO|FIXME)' | wc -l
1899

For orphan code, a comment in the code might be better to remember the technical debt to the next developers interested to rework this piece of code (I'd rather not trust they'll dig in the mailing list archive and launchpad tickets while staring at the code).

Quoted 35 lines
Quoted 4 lines

OTOH we could also log UNIMP for lost IRQs (triggered but no handler registered).

That would also work.

Alistair

Quoted 25 lines
Quoted 25 lines

Alistair

Quoted 20 lines
---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
hw/lm32/milkymist.c +1 -1
index 469e3c4322..117973c967 100644
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
     return dev;
 }
--

2.21.3

#8 lore raw

Re: [PATCH 2/4] hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

Quoted 16 lines · Philippe Mathieu-Daudé
Quoted 14 lines · Alistair Francis
Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 10 lines · Alistair Francis
Quoted 5 lines · Philippe Mathieu-Daudé

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

I'm not convinced adding fixmes or todos in the code is the right direction. It would be better to file bugs or use some other more official tracking mechanism.

Quoted 7 lines
Quoted 5 lines
Quoted 3 lines

This code is orphan :S

I'll fill a launchpad bug ticket.

I also mean in general (you have some other patches that add TODOs or FIXMEs).

Not all developers look at launchpad, while all of them read the code ;)

Good point.

Quoted 8 lines
$ git grep -E '(TODO|FIXME)' | wc -l
1899

For orphan code, a comment in the code might be better to remember the technical debt to the next developers interested to rework this piece of code (I'd rather not trust they'll dig in the mailing list archive and launchpad tickets while staring at the code).

Agreed. I guess this is fine then. If possible/applicable a log message would be more helpful.

Alistair

Quoted 36 lines
Quoted 36 lines
Quoted 5 lines

OTOH we could also log UNIMP for lost IRQs (triggered but no handler registered).

That would also work.

Alistair

Quoted 25 lines
Quoted 25 lines

Alistair

Quoted 20 lines
---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
hw/lm32/milkymist.c +1 -1
index 469e3c4322..117973c967 100644
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
     return dev;
 }
--

2.21.3

#9 lore raw

Re: [PATCH 2/4] hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

In reply to Philippe Mathieu-Daudé
Quoted 14 lines · Philippe Mathieu-Daudé

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
hw/lm32/milkymist.c +0 -0
index 469e3c4322..117973c967 100644

hw/lm32/milkymist.c static DeviceState *milkymist_memcard_create(hwaddr base) · line 90

Quoted 4 lines
hw/lm32/milkymist.c +1 -0
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */

It's possible that these lines are correctly not wired up (ie that the hardware does not provide any kind of indication of the r/o or insertion events). The milkymist mmc device is a very simple one. AIUI the RTL for the board is on github if anybody wants to go check.

thanks
-- PMM

#10 lore raw

Re: [PATCH 2/4] hw/lm32/milkymist: Comment to remember some IRQs lines are left unwired

Am 2020-07-07 18:30, schrieb Peter Maydell:

Quoted 10 lines · Philippe Mathieu-Daudé
Quoted 9 lines

The 'card is readonly' and 'card inserted' IRQs are not wired. Add a comment in case someone know where to wire them.

---
 hw/lm32/milkymist.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 16 lines

hw/lm32/milkymist.c static DeviceState *milkymist_memcard_create(hwaddr · line 91

Quoted 10 lines
hw/lm32/milkymist.c +1 -0
index 469e3c4322..117973c967 100644
@@ -87,6 +87,7 @@ static DeviceState *milkymist_memcard_create(hwaddr 
 base)
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
+    /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */

It's possible that these lines are correctly not wired up (ie that the hardware does not provide any kind of indication of the r/o or insertion events). The milkymist mmc device is a very simple one. AIUI the RTL for the board is on github if anybody wants to go check.

That is correct, there are not indications nor are there any interrupts.

-michael

#11 lore raw

PATCH 3/4 hw/sd/milkymist: Create the SDBus at init()

In reply to Philippe Mathieu-Daudé

We don't need to wait until realize() to create the SDBus, create it in init() directly.

---
 hw/sd/milkymist-memcard.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/milkymist-memcard.c +3 -3
index afdb8aa0c0..fb02309f07 100644
@@ -261,6 +261,9 @@ static void milkymist_memcard_init(Object *obj)
     memory_region_init_io(&s->regs_region, OBJECT(s), &memcard_mmio_ops, s,
             "milkymist-memcard", R_MAX * 4);
     sysbus_init_mmio(dev, &s->regs_region);
+
+    qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
+                        DEVICE(obj), "sd-bus");
 }
 
 static void milkymist_memcard_realize(DeviceState *dev, Error **errp)
@@ -271,9 +274,6 @@ static void milkymist_memcard_realize(DeviceState *dev, Error **errp)
     DriveInfo *dinfo;
     Error *err = NULL;
 
-    qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
-                        dev, "sd-bus");
-
     /* Create and plug in the sd card */
     /* FIXME use a qdev drive property instead of drive_get_next() */
     dinfo = drive_get_next(IF_SD);
Signature · 1 line
2.21.3
Reviewed-by on v1 3/4
#12 lore raw

Re: [PATCH 3/4] hw/sd/milkymist: Create the SDBus at init()

Quoted 5 lines · Philippe Mathieu-Daudé

We don't need to wait until realize() to create the SDBus, create it in init() directly.

Alistair

Quoted 32 lines
---
 hw/sd/milkymist-memcard.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
hw/sd/milkymist-memcard.c +3 -4
index afdb8aa0c0..fb02309f07 100644
@@ -261,6 +261,9 @@ static void milkymist_memcard_init(Object *obj)
     memory_region_init_io(&s->regs_region, OBJECT(s), &memcard_mmio_ops, s,
             "milkymist-memcard", R_MAX * 4);
     sysbus_init_mmio(dev, &s->regs_region);
+
+    qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
+                        DEVICE(obj), "sd-bus");
 }
 
 static void milkymist_memcard_realize(DeviceState *dev, Error **errp)
@@ -271,9 +274,6 @@ static void milkymist_memcard_realize(DeviceState *dev, Error **errp)
     DriveInfo *dinfo;
     Error *err = NULL;
 
-    qbus_create_inplace(&s->sdbus, sizeof(s->sdbus), TYPE_SD_BUS,
-                        dev, "sd-bus");
-
     /* Create and plug in the sd card */
     /* FIXME use a qdev drive property instead of drive_get_next() */
     dinfo = drive_get_next(IF_SD);
--

2.21.3

#13 lore raw

PATCH 4/4 hw/sd/milkymist: Do not create SD card within the SDHCI controller

In reply to Philippe Mathieu-Daudé

SDHCI controllers provide a SD Bus to plug SD cards, but don't come with SD card plugged in :) Let the machine/board object create and plug the SD cards when required.

---
 hw/lm32/milkymist.c       | 13 +++++++++
 hw/sd/milkymist-memcard.c | 55 +++++++++++++++++++++++----------------
 2 files changed, 45 insertions(+), 23 deletions(-)
hw/lm32/milkymist.c +13 -0
index 117973c967..8e2c68da5a 100644
@@ -34,6 +34,7 @@
 #include "elf.h"
 #include "milkymist-hw.h"
 #include "hw/display/milkymist_tmu2.h"
+#include "hw/sd/sd.h"
 #include "lm32.h"
 #include "exec/address-spaces.h"
 #include "qemu/cutils.h"
@@ -83,12 +84,24 @@ static void main_cpu_reset(void *opaque)
 static DeviceState *milkymist_memcard_create(hwaddr base)
 {
     DeviceState *dev;
+    DriveInfo *dinfo;
 
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
     /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
+    dinfo = drive_get_next(IF_SD);
+    if (dinfo) {
+        DeviceState *card;
+
+        card = qdev_new(TYPE_SD_CARD);
+        qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
+                                &error_fatal);
+        qdev_realize_and_unref(card, qdev_get_child_bus(dev, "sd-bus"),
+                               &error_fatal);
+    }
+
     return dev;
 }
 
hw/sd/milkymist-memcard.c +32 -23

90 lines not on this page · raw message

Signature · 1 line
2.21.3
Reviewed-by on v1 4/4
#14 lore raw
Quoted 6 lines · Philippe Mathieu-Daudé

SDHCI controllers provide a SD Bus to plug SD cards, but don't come with SD card plugged in :) Let the machine/board object create and plug the SD cards when required.

Alistair

Quoted 145 lines
---
 hw/lm32/milkymist.c       | 13 +++++++++
 hw/sd/milkymist-memcard.c | 55 +++++++++++++++++++++++----------------
 2 files changed, 45 insertions(+), 23 deletions(-)
hw/lm32/milkymist.c +13 -0
index 117973c967..8e2c68da5a 100644
@@ -34,6 +34,7 @@
 #include "elf.h"
 #include "milkymist-hw.h"
 #include "hw/display/milkymist_tmu2.h"
+#include "hw/sd/sd.h"
 #include "lm32.h"
 #include "exec/address-spaces.h"
 #include "qemu/cutils.h"
@@ -83,12 +84,24 @@ static void main_cpu_reset(void *opaque)
 static DeviceState *milkymist_memcard_create(hwaddr base)
 {
     DeviceState *dev;
+    DriveInfo *dinfo;
 
     dev = qdev_new("milkymist-memcard");
     sysbus_realize_and_unref(SYS_BUS_DEVICE(dev), &error_fatal);
     sysbus_mmio_map(SYS_BUS_DEVICE(dev), 0, base);
     /* FIXME wire 'card is readonly' and 'card inserted' IRQs? */
 
+    dinfo = drive_get_next(IF_SD);
+    if (dinfo) {
+        DeviceState *card;
+
+        card = qdev_new(TYPE_SD_CARD);
+        qdev_prop_set_drive_err(card, "drive", blk_by_legacy_dinfo(dinfo),
+                                &error_fatal);
+        qdev_realize_and_unref(card, qdev_get_child_bus(dev, "sd-bus"),
+                               &error_fatal);
+    }
+
     return dev;
 }
 
hw/sd/milkymist-memcard.c +32 -24
index fb02309f07..e9f5db5e22 100644
@@ -66,6 +66,8 @@ enum {
 #define MILKYMIST_MEMCARD(obj) \
     OBJECT_CHECK(MilkymistMemcardState, (obj), TYPE_MILKYMIST_MEMCARD)
 
+#define TYPE_MILKYMIST_SDBUS "milkymist-sdbus"
+
 struct MilkymistMemcardState {
     SysBusDevice parent_obj;
 
@@ -253,6 +255,19 @@ static void milkymist_memcard_reset(DeviceState *d)
     }
 }
 
+static void milkymist_memcard_set_readonly(DeviceState *dev, bool level)
+{
+    qemu_log_mask(LOG_UNIMP,
+                  "milkymist_memcard: read-only mode not supported\n");
+}
+
+static void milkymist_memcard_set_inserted(DeviceState *dev, bool level)
+{
+    MilkymistMemcardState *s = MILKYMIST_MEMCARD(dev);
+
+    s->enabled = !!level;
+}
+
 static void milkymist_memcard_init(Object *obj)
 {
     MilkymistMemcardState *s = MILKYMIST_MEMCARD(obj);
@@ -266,27 +281,6 @@ static void milkymist_memcard_init(Object *obj)
                         DEVICE(obj), "sd-bus");
 }
 
-static void milkymist_memcard_realize(DeviceState *dev, Error **errp)
-{
-    MilkymistMemcardState *s = MILKYMIST_MEMCARD(dev);
-    DeviceState *carddev;
-    BlockBackend *blk;
-    DriveInfo *dinfo;
-    Error *err = NULL;
-
-    /* Create and plug in the sd card */
-    /* FIXME use a qdev drive property instead of drive_get_next() */
-    dinfo = drive_get_next(IF_SD);
-    blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
-    carddev = qdev_new(TYPE_SD_CARD);
-    qdev_prop_set_drive(carddev, "drive", blk);
-    if (!qdev_realize_and_unref(carddev, BUS(&s->sdbus), &err)) {
-        error_propagate_prepend(errp, err, "failed to init SD card: %s");
-        return;
-    }
-    s->enabled = blk && blk_is_inserted(blk);
-}
-
 static const VMStateDescription vmstate_milkymist_memcard = {
     .name = "milkymist-memcard",
     .version_id = 1,
@@ -308,10 +302,9 @@ static void milkymist_memcard_class_init(ObjectClass *klass, void *data)
 {
     DeviceClass *dc = DEVICE_CLASS(klass);
 
-    dc->realize = milkymist_memcard_realize;
     dc->reset = milkymist_memcard_reset;
     dc->vmsd = &vmstate_milkymist_memcard;
-    /* Reason: init() method uses drive_get_next() */
+    /* Reason: output IRQs should be wired up */
     dc->user_creatable = false;
 }
 
@@ -323,9 +316,25 @@ static const TypeInfo milkymist_memcard_info = {
     .class_init    = milkymist_memcard_class_init,
 };
 
+static void milkymist_sdbus_class_init(ObjectClass *klass, void *data)
+{
+    SDBusClass *sbc = SD_BUS_CLASS(klass);
+
+    sbc->set_inserted = milkymist_memcard_set_inserted;
+    sbc->set_readonly = milkymist_memcard_set_readonly;
+}
+
+static const TypeInfo milkymist_sdbus_info = {
+    .name = TYPE_MILKYMIST_SDBUS,
+    .parent = TYPE_SD_BUS,
+    .instance_size = sizeof(SDBus),
+    .class_init = milkymist_sdbus_class_init,
+};
+
 static void milkymist_memcard_register_types(void)
 {
     type_register_static(&milkymist_memcard_info);
+    type_register_static(&milkymist_sdbus_info);
 }
 
 type_init(milkymist_memcard_register_types)
--

2.21.3