1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2015 Google, Inc
16 struct sandbox_mmc_plat {
17 struct mmc_config cfg;
22 #define MMC_CMULT 8 /* 8 because the card is high-capacity */
23 #define MMC_BL_LEN_SHIFT 10
24 #define MMC_BL_LEN BIT(MMC_BL_LEN_SHIFT)
26 /* Granularity of priv->csize - this is 1MB */
27 #define SIZE_MULTIPLE ((1 << (MMC_CMULT + 2)) * MMC_BL_LEN)
29 struct sandbox_mmc_priv {
31 int csize; /* CSIZE value to report */
36 * sandbox_mmc_send_cmd() - Emulate SD commands
38 * This emulate an SD card version 2. Single-block reads result in zero data.
39 * Multiple-block reads return a test string.
41 static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
42 struct mmc_data *data)
44 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
45 static ulong erase_start, erase_end;
47 switch (cmd->cmdidx) {
48 case MMC_CMD_ALL_SEND_CID:
49 memset(cmd->response, '\0', sizeof(cmd->response));
51 case SD_CMD_SEND_RELATIVE_ADDR:
52 cmd->response[0] = 0 << 16; /* mmc->rca */
53 case MMC_CMD_GO_IDLE_STATE:
55 case SD_CMD_SEND_IF_COND:
56 cmd->response[0] = 0xaa;
58 case MMC_CMD_SEND_STATUS:
59 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
61 case MMC_CMD_SELECT_CARD:
63 case MMC_CMD_SEND_CSD:
65 cmd->response[1] = (MMC_BL_LEN_SHIFT << 16) |
66 ((priv->csize >> 16) & 0x3f);
67 cmd->response[2] = (priv->csize & 0xffff) << 16;
70 case SD_CMD_SWITCH_FUNC: {
73 u32 *resp = (u32 *)data->dest;
75 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
76 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
77 resp[4] = (cmd->cmdarg & 0xF) << 24;
80 case MMC_CMD_READ_SINGLE_BLOCK:
81 case MMC_CMD_READ_MULTIPLE_BLOCK:
82 memcpy(data->dest, &priv->buf[cmd->cmdarg * data->blocksize],
83 data->blocks * data->blocksize);
85 case MMC_CMD_WRITE_SINGLE_BLOCK:
86 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
87 memcpy(&priv->buf[cmd->cmdarg * data->blocksize], data->src,
88 data->blocks * data->blocksize);
90 case MMC_CMD_STOP_TRANSMISSION:
92 case SD_CMD_ERASE_WR_BLK_START:
93 erase_start = cmd->cmdarg;
95 case SD_CMD_ERASE_WR_BLK_END:
96 erase_end = cmd->cmdarg;
98 #if CONFIG_IS_ENABLED(MMC_WRITE)
100 struct mmc *mmc = mmc_get_mmc_dev(dev);
102 memset(&priv->buf[erase_start * mmc->write_bl_len], '\0',
103 (erase_end - erase_start + 1) * mmc->write_bl_len);
107 case SD_CMD_APP_SEND_OP_COND:
108 cmd->response[0] = OCR_BUSY | OCR_HCS;
109 cmd->response[1] = 0;
110 cmd->response[2] = 0;
112 case MMC_CMD_APP_CMD:
114 case MMC_CMD_SET_BLOCKLEN:
115 debug("block len %d\n", cmd->cmdarg);
117 case SD_CMD_APP_SEND_SCR: {
118 u32 *scr = (u32 *)data->dest;
120 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
124 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
131 static int sandbox_mmc_set_ios(struct udevice *dev)
136 static int sandbox_mmc_get_cd(struct udevice *dev)
141 static const struct dm_mmc_ops sandbox_mmc_ops = {
142 .send_cmd = sandbox_mmc_send_cmd,
143 .set_ios = sandbox_mmc_set_ios,
144 .get_cd = sandbox_mmc_get_cd,
147 static int sandbox_mmc_of_to_plat(struct udevice *dev)
149 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
150 struct mmc_config *cfg = &plat->cfg;
151 struct blk_desc *blk;
154 plat->fname = dev_read_string(dev, "filename");
156 ret = mmc_of_parse(dev, cfg);
159 blk = mmc_get_blk_desc(&plat->mmc);
161 blk->removable = !(cfg->host_caps & MMC_CAP_NONREMOVABLE);
166 static int sandbox_mmc_probe(struct udevice *dev)
168 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
169 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
173 ret = os_map_file(plat->fname, OS_O_RDWR | OS_O_CREAT,
174 (void **)&priv->buf, &priv->size);
176 log_err("%s: Unable to map file '%s'\n", dev->name,
180 priv->csize = priv->size / SIZE_MULTIPLE - 1;
183 priv->size = (priv->csize + 1) * SIZE_MULTIPLE; /* 1 MiB */
185 priv->buf = calloc(1, priv->size);
187 log_err("%s: Not enough memory (%x bytes)\n",
188 dev->name, priv->size);
193 return mmc_init(&plat->mmc);
196 static int sandbox_mmc_remove(struct udevice *dev)
198 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
199 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
202 os_unmap(priv->buf, priv->size);
209 static int sandbox_mmc_bind(struct udevice *dev)
211 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
212 struct mmc_config *cfg = &plat->cfg;
214 cfg->name = dev->name;
215 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
216 cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
217 cfg->f_min = 1000000;
218 cfg->f_max = 52000000;
219 cfg->b_max = U32_MAX;
221 return mmc_bind(dev, &plat->mmc, cfg);
224 static int sandbox_mmc_unbind(struct udevice *dev)
231 static const struct udevice_id sandbox_mmc_ids[] = {
232 { .compatible = "sandbox,mmc" },
236 U_BOOT_DRIVER(mmc_sandbox) = {
237 .name = "mmc_sandbox",
239 .of_match = sandbox_mmc_ids,
240 .ops = &sandbox_mmc_ops,
241 .bind = sandbox_mmc_bind,
242 .unbind = sandbox_mmc_unbind,
243 .of_to_plat = sandbox_mmc_of_to_plat,
244 .probe = sandbox_mmc_probe,
245 .remove = sandbox_mmc_remove,
246 .priv_auto = sizeof(struct sandbox_mmc_priv),
247 .plat_auto = sizeof(struct sandbox_mmc_plat),