Skip to content

Commit

Permalink
hw/sd: Introduce a "sd-card" SPI variant model
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
legoater committed Sep 1, 2023
1 parent 6380cd2 commit c3287c0
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 18 deletions.
3 changes: 1 addition & 2 deletions hw/arm/stellaris.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 1 addition & 2 deletions hw/riscv/sifive_u.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
54 changes: 40 additions & 14 deletions hw/sd/sd.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ struct SDState {

uint8_t spec_version;
BlockBackend *blk;
bool spi;

/* Runtime changeables */

Expand Down Expand Up @@ -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[] = {
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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. */
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()
};

Expand All @@ -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 = {
Expand All @@ -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)
3 changes: 3 additions & 0 deletions include/hw/sd/sd.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit c3287c0

Please sign in to comment.