blob: a24520f2e78c91e3e13ef6d5c4413836a7c17aba [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glassd3e58e42015-07-06 12:54:32 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glassd3e58e42015-07-06 12:54:32 -06005 */
6
Simon Glassd3e58e42015-07-06 12:54:32 -06007#include <dm.h>
8#include <errno.h>
Simon Glassf9eebac2016-05-01 13:52:42 -06009#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Simon Glass32b6c8b2021-10-23 17:25:59 -060011#include <malloc.h>
Simon Glassd3e58e42015-07-06 12:54:32 -060012#include <mmc.h>
Simon Glass32b6c8b2021-10-23 17:25:59 -060013#include <os.h>
Simon Glassd3e58e42015-07-06 12:54:32 -060014#include <asm/test.h>
15
Simon Glassf9eebac2016-05-01 13:52:42 -060016struct sandbox_mmc_plat {
17 struct mmc_config cfg;
18 struct mmc mmc;
Simon Glass32b6c8b2021-10-23 17:25:59 -060019 const char *fname;
Simon Glassf9eebac2016-05-01 13:52:42 -060020};
21
Simon Glass32b6c8b2021-10-23 17:25:59 -060022#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)
Simon Glassceeb74f2022-10-20 18:22:58 -060025
26/* Granularity of priv->csize - this is 1MB */
Simon Glass32b6c8b2021-10-23 17:25:59 -060027#define SIZE_MULTIPLE ((1 << (MMC_CMULT + 2)) * MMC_BL_LEN)
Sean Andersonb2f1c0b2021-02-05 09:38:53 -050028
29struct sandbox_mmc_priv {
Simon Glass32b6c8b2021-10-23 17:25:59 -060030 char *buf;
31 int csize; /* CSIZE value to report */
32 int size;
Sean Andersonb2f1c0b2021-02-05 09:38:53 -050033};
34
Simon Glassf9eebac2016-05-01 13:52:42 -060035/**
36 * sandbox_mmc_send_cmd() - Emulate SD commands
37 *
38 * This emulate an SD card version 2. Single-block reads result in zero data.
39 * Multiple-block reads return a test string.
40 */
Simon Glassefcc6692016-06-12 23:30:26 -060041static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassf9eebac2016-05-01 13:52:42 -060042 struct mmc_data *data)
43{
Sean Andersonb2f1c0b2021-02-05 09:38:53 -050044 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
Sean Andersonb2f1c0b2021-02-05 09:38:53 -050045 static ulong erase_start, erase_end;
46
Simon Glassf9eebac2016-05-01 13:52:42 -060047 switch (cmd->cmdidx) {
48 case MMC_CMD_ALL_SEND_CID:
Simon Glassb1970542019-09-25 08:56:00 -060049 memset(cmd->response, '\0', sizeof(cmd->response));
Simon Glassf9eebac2016-05-01 13:52:42 -060050 break;
51 case SD_CMD_SEND_RELATIVE_ADDR:
52 cmd->response[0] = 0 << 16; /* mmc->rca */
53 case MMC_CMD_GO_IDLE_STATE:
54 break;
55 case SD_CMD_SEND_IF_COND:
56 cmd->response[0] = 0xaa;
57 break;
58 case MMC_CMD_SEND_STATUS:
59 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
60 break;
61 case MMC_CMD_SELECT_CARD:
62 break;
63 case MMC_CMD_SEND_CSD:
64 cmd->response[0] = 0;
Sean Andersonb2f1c0b2021-02-05 09:38:53 -050065 cmd->response[1] = (MMC_BL_LEN_SHIFT << 16) |
Simon Glass32b6c8b2021-10-23 17:25:59 -060066 ((priv->csize >> 16) & 0x3f);
67 cmd->response[2] = (priv->csize & 0xffff) << 16;
Simon Glassb1970542019-09-25 08:56:00 -060068 cmd->response[3] = 0;
Simon Glassf9eebac2016-05-01 13:52:42 -060069 break;
70 case SD_CMD_SWITCH_FUNC: {
Jean-Jacques Hiblotef72fd32017-12-14 11:47:14 +010071 if (!data)
72 break;
Simon Glassf9eebac2016-05-01 13:52:42 -060073 u32 *resp = (u32 *)data->dest;
Simon Glassb1970542019-09-25 08:56:00 -060074 resp[3] = 0;
Simon Glassf9eebac2016-05-01 13:52:42 -060075 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
Jean-Jacques Hiblotef72fd32017-12-14 11:47:14 +010076 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
77 resp[4] = (cmd->cmdarg & 0xF) << 24;
Simon Glassf9eebac2016-05-01 13:52:42 -060078 break;
79 }
80 case MMC_CMD_READ_SINGLE_BLOCK:
Simon Glassf9eebac2016-05-01 13:52:42 -060081 case MMC_CMD_READ_MULTIPLE_BLOCK:
Sean Andersonb2f1c0b2021-02-05 09:38:53 -050082 memcpy(data->dest, &priv->buf[cmd->cmdarg * data->blocksize],
83 data->blocks * data->blocksize);
84 break;
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);
Simon Glassf9eebac2016-05-01 13:52:42 -060089 break;
90 case MMC_CMD_STOP_TRANSMISSION:
91 break;
Sean Andersonb2f1c0b2021-02-05 09:38:53 -050092 case SD_CMD_ERASE_WR_BLK_START:
93 erase_start = cmd->cmdarg;
94 break;
95 case SD_CMD_ERASE_WR_BLK_END:
96 erase_end = cmd->cmdarg;
97 break;
Simon Glassceeb74f2022-10-20 18:22:58 -060098#if CONFIG_IS_ENABLED(MMC_WRITE)
99 case MMC_CMD_ERASE: {
100 struct mmc *mmc = mmc_get_mmc_dev(dev);
101
Sean Andersonb2f1c0b2021-02-05 09:38:53 -0500102 memset(&priv->buf[erase_start * mmc->write_bl_len], '\0',
103 (erase_end - erase_start + 1) * mmc->write_bl_len);
104 break;
Simon Glassceeb74f2022-10-20 18:22:58 -0600105 }
106#endif
Simon Glassf9eebac2016-05-01 13:52:42 -0600107 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;
111 break;
112 case MMC_CMD_APP_CMD:
113 break;
114 case MMC_CMD_SET_BLOCKLEN:
115 debug("block len %d\n", cmd->cmdarg);
116 break;
117 case SD_CMD_APP_SEND_SCR: {
118 u32 *scr = (u32 *)data->dest;
119
120 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
121 break;
122 }
123 default:
124 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
125 break;
126 }
127
128 return 0;
129}
130
Simon Glassefcc6692016-06-12 23:30:26 -0600131static int sandbox_mmc_set_ios(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -0600132{
133 return 0;
134}
135
Simon Glassefcc6692016-06-12 23:30:26 -0600136static int sandbox_mmc_get_cd(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -0600137{
138 return 1;
139}
140
Simon Glassefcc6692016-06-12 23:30:26 -0600141static const struct dm_mmc_ops sandbox_mmc_ops = {
Simon Glassf9eebac2016-05-01 13:52:42 -0600142 .send_cmd = sandbox_mmc_send_cmd,
143 .set_ios = sandbox_mmc_set_ios,
Simon Glassefcc6692016-06-12 23:30:26 -0600144 .get_cd = sandbox_mmc_get_cd,
Simon Glassf9eebac2016-05-01 13:52:42 -0600145};
146
Simon Glass965cd402021-07-05 16:32:58 -0600147static int sandbox_mmc_of_to_plat(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -0600148{
Simon Glassfa20e932020-12-03 16:55:20 -0700149 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
Simon Glass965cd402021-07-05 16:32:58 -0600150 struct mmc_config *cfg = &plat->cfg;
151 struct blk_desc *blk;
152 int ret;
153
Simon Glass32b6c8b2021-10-23 17:25:59 -0600154 plat->fname = dev_read_string(dev, "filename");
155
Simon Glass965cd402021-07-05 16:32:58 -0600156 ret = mmc_of_parse(dev, cfg);
157 if (ret)
158 return ret;
159 blk = mmc_get_blk_desc(&plat->mmc);
160 if (blk)
161 blk->removable = !(cfg->host_caps & MMC_CAP_NONREMOVABLE);
162
163 return 0;
164}
165
166static int sandbox_mmc_probe(struct udevice *dev)
167{
168 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
Simon Glass32b6c8b2021-10-23 17:25:59 -0600169 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
170 int ret;
171
172 if (plat->fname) {
173 ret = os_map_file(plat->fname, OS_O_RDWR | OS_O_CREAT,
174 (void **)&priv->buf, &priv->size);
175 if (ret) {
176 log_err("%s: Unable to map file '%s'\n", dev->name,
177 plat->fname);
178 return ret;
179 }
180 priv->csize = priv->size / SIZE_MULTIPLE - 1;
181 } else {
182 priv->csize = 0;
183 priv->size = (priv->csize + 1) * SIZE_MULTIPLE; /* 1 MiB */
184
Simon Glassb9d1ad52023-01-17 10:47:19 -0700185 priv->buf = calloc(1, priv->size);
Simon Glass32b6c8b2021-10-23 17:25:59 -0600186 if (!priv->buf) {
187 log_err("%s: Not enough memory (%x bytes)\n",
188 dev->name, priv->size);
189 return -ENOMEM;
190 }
191 }
Simon Glassf9eebac2016-05-01 13:52:42 -0600192
193 return mmc_init(&plat->mmc);
194}
195
Simon Glass32b6c8b2021-10-23 17:25:59 -0600196static int sandbox_mmc_remove(struct udevice *dev)
197{
198 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
199 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
200
201 if (plat->fname)
202 os_unmap(priv->buf, priv->size);
203 else
204 free(priv->buf);
205
206 return 0;
207}
208
Simon Glass965cd402021-07-05 16:32:58 -0600209static int sandbox_mmc_bind(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -0600210{
Simon Glassfa20e932020-12-03 16:55:20 -0700211 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
Simon Glassf9eebac2016-05-01 13:52:42 -0600212 struct mmc_config *cfg = &plat->cfg;
Simon Glassf9eebac2016-05-01 13:52:42 -0600213
214 cfg->name = dev->name;
Simon Glassf9eebac2016-05-01 13:52:42 -0600215 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;
220
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900221 return mmc_bind(dev, &plat->mmc, cfg);
Simon Glassf9eebac2016-05-01 13:52:42 -0600222}
223
Simon Glass965cd402021-07-05 16:32:58 -0600224static int sandbox_mmc_unbind(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -0600225{
226 mmc_unbind(dev);
227
228 return 0;
229}
230
Simon Glassd3e58e42015-07-06 12:54:32 -0600231static const struct udevice_id sandbox_mmc_ids[] = {
232 { .compatible = "sandbox,mmc" },
233 { }
234};
235
Simon Glassf9eebac2016-05-01 13:52:42 -0600236U_BOOT_DRIVER(mmc_sandbox) = {
Simon Glassd3e58e42015-07-06 12:54:32 -0600237 .name = "mmc_sandbox",
238 .id = UCLASS_MMC,
239 .of_match = sandbox_mmc_ids,
Simon Glassefcc6692016-06-12 23:30:26 -0600240 .ops = &sandbox_mmc_ops,
Simon Glassf9eebac2016-05-01 13:52:42 -0600241 .bind = sandbox_mmc_bind,
242 .unbind = sandbox_mmc_unbind,
Simon Glass965cd402021-07-05 16:32:58 -0600243 .of_to_plat = sandbox_mmc_of_to_plat,
Simon Glassf9eebac2016-05-01 13:52:42 -0600244 .probe = sandbox_mmc_probe,
Simon Glass32b6c8b2021-10-23 17:25:59 -0600245 .remove = sandbox_mmc_remove,
Sean Andersonb2f1c0b2021-02-05 09:38:53 -0500246 .priv_auto = sizeof(struct sandbox_mmc_priv),
247 .plat_auto = sizeof(struct sandbox_mmc_plat),
Simon Glassd3e58e42015-07-06 12:54:32 -0600248};