1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
14 struct sandbox_mmc_plat {
15 struct mmc_config cfg;
20 * sandbox_mmc_send_cmd() - Emulate SD commands
22 * This emulate an SD card version 2. Single-block reads result in zero data.
23 * Multiple-block reads return a test string.
25 static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
26 struct mmc_data *data)
28 switch (cmd->cmdidx) {
29 case MMC_CMD_ALL_SEND_CID:
30 memset(cmd->response, '\0', sizeof(cmd->response));
32 case SD_CMD_SEND_RELATIVE_ADDR:
33 cmd->response[0] = 0 << 16; /* mmc->rca */
34 case MMC_CMD_GO_IDLE_STATE:
36 case SD_CMD_SEND_IF_COND:
37 cmd->response[0] = 0xaa;
39 case MMC_CMD_SEND_STATUS:
40 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
42 case MMC_CMD_SELECT_CARD:
44 case MMC_CMD_SEND_CSD:
46 cmd->response[1] = 10 << 16; /* 1 << block_len */
50 case SD_CMD_SWITCH_FUNC: {
53 u32 *resp = (u32 *)data->dest;
55 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
56 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
57 resp[4] = (cmd->cmdarg & 0xF) << 24;
60 case MMC_CMD_READ_SINGLE_BLOCK:
61 memset(data->dest, '\0', data->blocksize);
63 case MMC_CMD_READ_MULTIPLE_BLOCK:
64 strcpy(data->dest, "this is a test");
66 case MMC_CMD_STOP_TRANSMISSION:
68 case SD_CMD_APP_SEND_OP_COND:
69 cmd->response[0] = OCR_BUSY | OCR_HCS;
75 case MMC_CMD_SET_BLOCKLEN:
76 debug("block len %d\n", cmd->cmdarg);
78 case SD_CMD_APP_SEND_SCR: {
79 u32 *scr = (u32 *)data->dest;
81 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
85 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
92 static int sandbox_mmc_set_ios(struct udevice *dev)
97 static int sandbox_mmc_get_cd(struct udevice *dev)
102 static const struct dm_mmc_ops sandbox_mmc_ops = {
103 .send_cmd = sandbox_mmc_send_cmd,
104 .set_ios = sandbox_mmc_set_ios,
105 .get_cd = sandbox_mmc_get_cd,
108 int sandbox_mmc_probe(struct udevice *dev)
110 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
112 return mmc_init(&plat->mmc);
115 int sandbox_mmc_bind(struct udevice *dev)
117 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
118 struct mmc_config *cfg = &plat->cfg;
120 cfg->name = dev->name;
121 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
122 cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
123 cfg->f_min = 1000000;
124 cfg->f_max = 52000000;
125 cfg->b_max = U32_MAX;
127 return mmc_bind(dev, &plat->mmc, cfg);
130 int sandbox_mmc_unbind(struct udevice *dev)
137 static const struct udevice_id sandbox_mmc_ids[] = {
138 { .compatible = "sandbox,mmc" },
142 U_BOOT_DRIVER(mmc_sandbox) = {
143 .name = "mmc_sandbox",
145 .of_match = sandbox_mmc_ids,
146 .ops = &sandbox_mmc_ops,
147 .bind = sandbox_mmc_bind,
148 .unbind = sandbox_mmc_unbind,
149 .probe = sandbox_mmc_probe,
150 .platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat),