From nobody Wed Apr 16 12:47:13 2025 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1693561584003956.4752114629921; Fri, 1 Sep 2023 02:46:24 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qc0gZ-0003XX-CP; Fri, 01 Sep 2023 05:43:27 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qc0gX-0003Iq-Da; Fri, 01 Sep 2023 05:43:25 -0400 Received: from gandalf.ozlabs.org ([150.107.74.76]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qc0gT-0003Bb-EC; Fri, 01 Sep 2023 05:43:25 -0400 Received: from gandalf.ozlabs.org (mail.ozlabs.org [IPv6:2404:9400:2221:ea00::3]) by gandalf.ozlabs.org (Postfix) with ESMTP id 4RcY4R3Sjbz4wxW; Fri, 1 Sep 2023 19:43:19 +1000 (AEST) Received: from authenticated.ozlabs.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by mail.ozlabs.org (Postfix) with ESMTPSA id 4RcY4P6wPHz4x2b; Fri, 1 Sep 2023 19:43:17 +1000 (AEST) From: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= To: qemu-arm@nongnu.org, qemu-devel@nongnu.org Cc: =?UTF-8?q?C=C3=A9dric=20Le=20Goater?= , =?UTF-8?q?Philippe=20Mathieu-Daud=C3=A9?= Subject: [PULL 26/26] hw/sd: Introduce a "sd-card" SPI variant model Date: Fri, 1 Sep 2023 11:42:14 +0200 Message-ID: <20230901094214.296918-27-clg@kaod.org> X-Mailer: git-send-email 2.41.0 In-Reply-To: <20230901094214.296918-1-clg@kaod.org> References: <20230901094214.296918-1-clg@kaod.org> MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=150.107.74.76; envelope-from=SRS0=SnXb=ER=kaod.org=clg@ozlabs.org; helo=gandalf.ozlabs.org X-Spam_score_int: -16 X-Spam_score: -1.7 X-Spam_bar: - X-Spam_report: (-1.7 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.25, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1693561584425100001 and replace the SDState::spi attribute with a test checking the SDProto array of commands. Reviewed-by: Philippe Mathieu-Daud=C3=A9 Signed-off-by: C=C3=A9dric Le Goater --- 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) =20 +#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, stellari= s_board_info *board) =20 dinfo =3D drive_get(IF_SD, 0, 0); blk =3D dinfo ? blk_by_legacy_dinfo(dinfo) : NULL; - carddev =3D qdev_new(TYPE_SD_CARD); + carddev =3D 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) =20 dinfo =3D drive_get(IF_SD, 0, 0); blk =3D dinfo ? blk_by_legacy_dinfo(dinfo) : NULL; - card_dev =3D qdev_new(TYPE_SD_CARD); + card_dev =3D 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 { =20 uint8_t spec_version; BlockBackend *blk; - bool spi; =20 /* Runtime changeables */ =20 @@ -159,6 +158,13 @@ static const struct SDProto *sd_proto(SDState *sd) return sc->proto; } =20 +static const SDProto sd_proto_spi; + +static bool sd_is_spi(SDState *sd) +{ + return sd_proto(sd) =3D=3D &sd_proto_spi; +} + static const char *sd_version_str(enum SDPhySpecificationVersion version) { static const char *sdphy_version[] =3D { @@ -336,7 +342,7 @@ static void sd_set_ocr(SDState *sd) /* All voltages OK */ sd->ocr =3D R_OCR_VDD_VOLTAGE_WIN_HI_MASK; =20 - 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 re= set. @@ -741,13 +747,12 @@ SDState *sd_init(BlockBackend *blk, bool is_spi) SDState *sd; Error *err =3D NULL; =20 - obj =3D object_new(TYPE_SD_CARD); + obj =3D object_new(is_spi ? TYPE_SD_CARD_SPI : TYPE_SD_CARD); dev =3D 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); =20 /* * 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)); } =20 - return sd->spi ? sd_r1 : sd_r0; + return sd_is_spi(sd) ? sd_r1 : sd_r0; } =20 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, S= DRequest req) =20 /* 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; } =20 /* Accept. */ @@ -1219,8 +1224,9 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, S= DRequest req) return sd_r2_s; =20 case sd_transfer_state: - if (!sd->spi) + if (!sd_is_spi(sd)) { break; + } sd->state =3D sd_sendingdata_state; memcpy(sd->data, sd->csd, 16); sd->data_start =3D addr; @@ -1241,8 +1247,9 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, S= DRequest req) return sd_r2_i; =20 case sd_transfer_state: - if (!sd->spi) + if (!sd_is_spi(sd)) { break; + } sd->state =3D sd_sendingdata_state; memcpy(sd->data, sd->cid, 16); sd->data_start =3D addr; @@ -1274,7 +1281,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, S= DRequest req) case 13: /* CMD13: SEND_STATUS */ switch (sd->mode) { case sd_data_transfer_mode: - if (!sd->spi && sd->rca !=3D rca) { + if (!sd_is_spi(sd) && sd->rca !=3D rca) { return sd_r0; } =20 @@ -1531,7 +1538,7 @@ static sd_rsp_type_t sd_normal_command(SDState *sd, S= DRequest req) default: break; } - if (!sd->spi) { + if (!sd_is_spi(sd)) { if (sd->rca !=3D 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 =3D SD_CARD(dev); - SDCardClass *sc =3D SD_CARD_GET_CLASS(sd); int ret; =20 - sc->proto =3D 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[] =3D { * 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() }; =20 @@ -2272,6 +2275,7 @@ static void sd_class_init(ObjectClass *klass, void *d= ata) sc->enable =3D sd_enable; sc->get_inserted =3D sd_get_inserted; sc->get_readonly =3D sd_get_readonly; + sc->proto =3D &sd_proto_sd; } =20 static const TypeInfo sd_info =3D { @@ -2284,9 +2288,31 @@ static const TypeInfo sd_info =3D { .instance_finalize =3D sd_instance_finalize, }; =20 +/* + * 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 =3D DEVICE_CLASS(klass); + SDCardClass *sc =3D SD_CARD_CLASS(klass); + + dc->desc =3D "SD SPI"; + sc->proto =3D &sd_proto_spi; +} + +static const TypeInfo sd_spi_info =3D { + .name =3D TYPE_SD_CARD_SPI, + .parent =3D TYPE_SD_CARD, + .class_init =3D sd_spi_class_init, +}; + static void sd_register_types(void) { type_register_static(&sd_info); + type_register_static(&sd_spi_info); } =20 type_init(sd_register_types) --=20 2.41.0