qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Cédric Le Goater" <clg@kaod.org>
To: qemu-arm@nongnu.org, qemu-devel@nongnu.org
Cc: "Cédric Le Goater" <clg@kaod.org>,
	"Philippe Mathieu-Daudé" <philmd@linaro.org>
Subject: [PULL 26/26] hw/sd: Introduce a "sd-card" SPI variant model
Date: Fri,  1 Sep 2023 11:42:14 +0200	[thread overview]
Message-ID: <20230901094214.296918-27-clg@kaod.org> (raw)
In-Reply-To: <20230901094214.296918-1-clg@kaod.org>

and replace the SDState::spi attribute with a test checking the
SDProto array of commands.

Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org>
Signed-off-by: Cédric Le Goater <clg@kaod.org>
---
 include/hw/sd/sd.h  |  3 +++
 hw/arm/stellaris.c  |  3 +--
 hw/riscv/sifive_u.c |  3 +--
 hw/sd/sd.c          | 54 +++++++++++++++++++++++++++++++++------------
 4 files changed, 45 insertions(+), 18 deletions(-)

diff --git a/include/hw/sd/sd.h b/include/hw/sd/sd.h
index b322d8f19b17..2c8748fb9b97 100644
--- a/include/hw/sd/sd.h
+++ b/include/hw/sd/sd.h
@@ -93,6 +93,9 @@ typedef struct {
 #define TYPE_SD_CARD "sd-card"
 OBJECT_DECLARE_TYPE(SDState, SDCardClass, SD_CARD)
 
+#define TYPE_SD_CARD_SPI "sd-card-spi"
+DECLARE_INSTANCE_CHECKER(SDState, SD_CARD_SPI, TYPE_SD_CARD_SPI)
+
 struct SDCardClass {
     /*< private >*/
     DeviceClass parent_class;
diff --git a/hw/arm/stellaris.c b/hw/arm/stellaris.c
index 5a3106e00939..aa5b0ddfaa5a 100644
--- a/hw/arm/stellaris.c
+++ b/hw/arm/stellaris.c
@@ -1235,9 +1235,8 @@ static void stellaris_init(MachineState *ms, stellaris_board_info *board)
 
             dinfo = drive_get(IF_SD, 0, 0);
             blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
-            carddev = qdev_new(TYPE_SD_CARD);
+            carddev = qdev_new(TYPE_SD_CARD_SPI);
             qdev_prop_set_drive_err(carddev, "drive", blk, &error_fatal);
-            qdev_prop_set_bit(carddev, "spi", true);
             qdev_realize_and_unref(carddev,
                                    qdev_get_child_bus(sddev, "sd-bus"),
                                    &error_fatal);
diff --git a/hw/riscv/sifive_u.c b/hw/riscv/sifive_u.c
index 35a335b8d0ba..ec76dce6c952 100644
--- a/hw/riscv/sifive_u.c
+++ b/hw/riscv/sifive_u.c
@@ -674,9 +674,8 @@ static void sifive_u_machine_init(MachineState *machine)
 
     dinfo = drive_get(IF_SD, 0, 0);
     blk = dinfo ? blk_by_legacy_dinfo(dinfo) : NULL;
-    card_dev = qdev_new(TYPE_SD_CARD);
+    card_dev = qdev_new(TYPE_SD_CARD_SPI);
     qdev_prop_set_drive_err(card_dev, "drive", blk, &error_fatal);
-    qdev_prop_set_bit(card_dev, "spi", true);
     qdev_realize_and_unref(card_dev,
                            qdev_get_child_bus(sd_dev, "sd-bus"),
                            &error_fatal);
diff --git a/hw/sd/sd.c b/hw/sd/sd.c
index d1c0b132c227..132daba4bd13 100644
--- a/hw/sd/sd.c
+++ b/hw/sd/sd.c
@@ -115,7 +115,6 @@ struct SDState {
 
     uint8_t spec_version;
     BlockBackend *blk;
-    bool spi;
 
     /* Runtime changeables */
 
@@ -159,6 +158,13 @@ static const struct SDProto *sd_proto(SDState *sd)
     return sc->proto;
 }
 
+static const SDProto sd_proto_spi;
+
+static bool sd_is_spi(SDState *sd)
+{
+    return sd_proto(sd) == &sd_proto_spi;
+}
+
 static const char *sd_version_str(enum SDPhySpecificationVersion version)
 {
     static const char *sdphy_version[] = {
@@ -336,7 +342,7 @@ static void sd_set_ocr(SDState *sd)
     /* All voltages OK */
     sd->ocr = R_OCR_VDD_VOLTAGE_WIN_HI_MASK;
 
-    if (sd->spi) {
+    if (sd_is_spi(sd)) {
         /*
          * We don't need to emulate power up sequence in SPI-mode.
          * Thus, the card's power up status bit should be set to 1 when reset.
@@ -741,13 +747,12 @@ SDState *sd_init(BlockBackend *blk, bool is_spi)
     SDState *sd;
     Error *err = NULL;
 
-    obj = object_new(TYPE_SD_CARD);
+    obj = object_new(is_spi ? TYPE_SD_CARD_SPI : TYPE_SD_CARD);
     dev = DEVICE(obj);
     if (!qdev_prop_set_drive_err(dev, "drive", blk, &err)) {
         error_reportf_err(err, "sd_init failed: ");
         return NULL;
     }
-    qdev_prop_set_bit(dev, "spi", is_spi);
 
     /*
      * Realizing the device properly would put it into the QOM
@@ -1027,7 +1032,7 @@ static sd_rsp_type_t sd_cmd_GO_IDLE_STATE(SDState *sd, SDRequest req)
         sd_reset(DEVICE(sd));
     }
 
-    return sd->spi ? sd_r1 : sd_r0;
+    return sd_is_spi(sd) ? sd_r1 : sd_r0;
 }
 
 static sd_rsp_type_t sd_cmd_SEND_OP_CMD(SDState *sd, SDRequest req)
@@ -1203,7 +1208,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
 
         /* No response if not exactly one VHS bit is set.  */
         if (!(req.arg >> 8) || (req.arg >> (ctz32(req.arg & ~0xff) + 1))) {
-            return sd->spi ? sd_r7 : sd_r0;
+            return sd_is_spi(sd) ? sd_r7 : sd_r0;
         }
 
         /* Accept.  */
@@ -1219,8 +1224,9 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
             return sd_r2_s;
 
         case sd_transfer_state:
-            if (!sd->spi)
+            if (!sd_is_spi(sd)) {
                 break;
+            }
             sd->state = sd_sendingdata_state;
             memcpy(sd->data, sd->csd, 16);
             sd->data_start = addr;
@@ -1241,8 +1247,9 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
             return sd_r2_i;
 
         case sd_transfer_state:
-            if (!sd->spi)
+            if (!sd_is_spi(sd)) {
                 break;
+            }
             sd->state = sd_sendingdata_state;
             memcpy(sd->data, sd->cid, 16);
             sd->data_start = addr;
@@ -1274,7 +1281,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
     case 13:  /* CMD13:  SEND_STATUS */
         switch (sd->mode) {
         case sd_data_transfer_mode:
-            if (!sd->spi && sd->rca != rca) {
+            if (!sd_is_spi(sd) && sd->rca != rca) {
                 return sd_r0;
             }
 
@@ -1531,7 +1538,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, SDRequest req)
         default:
             break;
         }
-        if (!sd->spi) {
+        if (!sd_is_spi(sd)) {
             if (sd->rca != rca) {
                 return sd_r0;
             }
@@ -2184,11 +2191,8 @@ static void sd_instance_finalize(Object *obj)
 static void sd_realize(DeviceState *dev, Error **errp)
 {
     SDState *sd = SD_CARD(dev);
-    SDCardClass *sc = SD_CARD_GET_CLASS(sd);
     int ret;
 
-    sc->proto = sd->spi ? &sd_proto_spi : &sd_proto_sd;
-
     switch (sd->spec_version) {
     case SD_PHY_SPECv1_10_VERS
      ... SD_PHY_SPECv3_01_VERS:
@@ -2245,7 +2249,6 @@ static Property sd_properties[] = {
      * whether card should be in SSI or MMC/SD mode.  It is also up to the
      * board to ensure that ssi transfers only occur when the chip select
      * is asserted.  */
-    DEFINE_PROP_BOOL("spi", SDState, spi, false),
     DEFINE_PROP_END_OF_LIST()
 };
 
@@ -2272,6 +2275,7 @@ static void sd_class_init(ObjectClass *klass, void *data)
     sc->enable = sd_enable;
     sc->get_inserted = sd_get_inserted;
     sc->get_readonly = sd_get_readonly;
+    sc->proto = &sd_proto_sd;
 }
 
 static const TypeInfo sd_info = {
@@ -2284,9 +2288,31 @@ static const TypeInfo sd_info = {
     .instance_finalize = sd_instance_finalize,
 };
 
+/*
+ * We do not model the chip select pin, so allow the board to select
+ * whether card should be in SSI or MMC/SD mode.  It is also up to the
+ * board to ensure that ssi transfers only occur when the chip select
+ * is asserted.
+ */
+static void sd_spi_class_init(ObjectClass *klass, void *data)
+{
+    DeviceClass *dc = DEVICE_CLASS(klass);
+    SDCardClass *sc = SD_CARD_CLASS(klass);
+
+    dc->desc = "SD SPI";
+    sc->proto = &sd_proto_spi;
+}
+
+static const TypeInfo sd_spi_info = {
+    .name = TYPE_SD_CARD_SPI,
+    .parent = TYPE_SD_CARD,
+    .class_init = sd_spi_class_init,
+};
+
 static void sd_register_types(void)
 {
     type_register_static(&sd_info);
+    type_register_static(&sd_spi_info);
 }
 
 type_init(sd_register_types)
-- 
2.41.0



  parent reply	other threads:[~2023-09-01  9:46 UTC|newest]

Thread overview: 31+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-09-01  9:41 [PULL 00/26] aspeed queue Cédric Le Goater
2023-09-01  9:41 ` [PULL 01/26] aspeed: Introduce helper for 32-bit hosts limitation Cédric Le Goater
2023-09-01  9:41 ` [PULL 02/26] hw/i2c/aspeed: Fix Tx count and Rx size error in buffer pool mode Cédric Le Goater
2023-09-01  9:41 ` [PULL 03/26] hw/i2c/aspeed: Fix TXBUF transmission start position error Cédric Le Goater
2023-09-01  9:41 ` [PULL 04/26] hw/i2c/aspeed: Add support for buffer organization Cédric Le Goater
2023-09-01  9:41 ` [PULL 05/26] tests/avocado/machine_aspeed.py: Update SDK images Cédric Le Goater
2023-09-01  9:41 ` [PULL 06/26] hw/ssi: Add a "cs" property to SSIPeripheral Cédric Le Goater
2023-09-01  9:41 ` [PULL 07/26] hw/ssi: Introduce a ssi_get_cs() helper Cédric Le Goater
2023-09-01  9:41 ` [PULL 08/26] aspeed/smc: Wire CS lines at reset Cédric Le Goater
2023-09-01  9:41 ` [PULL 09/26] hw/ssi: Check for duplicate CS indexes Cédric Le Goater
2023-09-01  9:41 ` [PULL 10/26] aspeed: Create flash devices only when defaults are enabled Cédric Le Goater
2023-09-01  9:41 ` [PULL 11/26] m25p80: Introduce an helper to retrieve the BlockBackend of a device Cédric Le Goater
2023-09-01  9:42 ` [PULL 12/26] aspeed: Get the BlockBackend of FMC0 from the flash device Cédric Le Goater
2023-09-01  9:42 ` [PULL 13/26] hw/sd/sdcard: Return ILLEGAL for CMD19/CMD23 prior SD spec v3.01 Cédric Le Goater
2023-09-01  9:42 ` [PULL 14/26] hw/sd: When card is in wrong state, log which state it is Cédric Le Goater
2023-09-01  9:42 ` [PULL 15/26] hw/sd: When card is in wrong state, log which spec version is used Cédric Le Goater
2023-09-01  9:42 ` [PULL 16/26] hw/sd: Move proto_name to SDProto structure Cédric Le Goater
2023-09-01  9:42 ` [PULL 17/26] hw/sd: Introduce sd_cmd_handler type Cédric Le Goater
2023-09-01  9:42 ` [PULL 18/26] hw/sd: Add sd_cmd_illegal() handler Cédric Le Goater
2023-09-01  9:42 ` [PULL 19/26] hw/sd: Add sd_cmd_unimplemented() handler Cédric Le Goater
2023-09-01  9:42 ` [PULL 20/26] hw/sd: Add sd_cmd_GO_IDLE_STATE() handler Cédric Le Goater
2023-09-01  9:42 ` [PULL 21/26] hw/sd: Add sd_cmd_SEND_OP_CMD() handler Cédric Le Goater
2023-09-01  9:42 ` [PULL 22/26] hw/sd: Add sd_cmd_ALL_SEND_CID() handler Cédric Le Goater
2023-09-01  9:42 ` [PULL 23/26] hw/sd: Add sd_cmd_SEND_RELATIVE_ADDR() handler Cédric Le Goater
2023-09-01  9:42 ` [PULL 24/26] hw/sd: Add sd_cmd_SEND_TUNING_BLOCK() handler Cédric Le Goater
2023-09-01  9:42 ` [PULL 25/26] hw/sd: Add sd_cmd_SET_BLOCK_COUNT() handler Cédric Le Goater
2023-09-01  9:42 ` Cédric Le Goater [this message]
2023-09-06 18:24 ` [PULL 00/26] aspeed queue Stefan Hajnoczi
2023-09-08  7:09 ` Cédric Le Goater
2023-09-08  7:52   ` Michael Tokarev
2023-09-08  8:25     ` Cédric Le Goater

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230901094214.296918-27-clg@kaod.org \
    --to=clg@kaod.org \
    --cc=philmd@linaro.org \
    --cc=qemu-arm@nongnu.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).