blob: 0ba7940a4dbfc41308182ad2023e1c9b82f748a7 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Simon Glass8e6cc462015-07-06 12:54:32 -06002/*
3 * Copyright (c) 2015 Google, Inc
4 * Written by Simon Glass <sjg@chromium.org>
Simon Glass8e6cc462015-07-06 12:54:32 -06005 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glassf376a3c2016-05-01 13:52:42 -060010#include <fdtdec.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060011#include <log.h>
Simon Glass0bf61ac2021-10-23 17:25:59 -060012#include <malloc.h>
Simon Glass8e6cc462015-07-06 12:54:32 -060013#include <mmc.h>
Simon Glass0bf61ac2021-10-23 17:25:59 -060014#include <os.h>
Simon Glass8e6cc462015-07-06 12:54:32 -060015#include <asm/test.h>
16
Simon Glassf376a3c2016-05-01 13:52:42 -060017struct sandbox_mmc_plat {
18 struct mmc_config cfg;
19 struct mmc mmc;
Simon Glass0bf61ac2021-10-23 17:25:59 -060020 const char *fname;
Simon Glassf376a3c2016-05-01 13:52:42 -060021};
22
Simon Glass0bf61ac2021-10-23 17:25:59 -060023#define MMC_CMULT 8 /* 8 because the card is high-capacity */
24#define MMC_BL_LEN_SHIFT 10
25#define MMC_BL_LEN BIT(MMC_BL_LEN_SHIFT)
Simon Glass372a7d92022-10-20 18:22:58 -060026
27/* Granularity of priv->csize - this is 1MB */
Simon Glass0bf61ac2021-10-23 17:25:59 -060028#define SIZE_MULTIPLE ((1 << (MMC_CMULT + 2)) * MMC_BL_LEN)
Sean Anderson3f6fb772021-02-05 09:38:53 -050029
30struct sandbox_mmc_priv {
Simon Glass0bf61ac2021-10-23 17:25:59 -060031 char *buf;
32 int csize; /* CSIZE value to report */
33 int size;
Sean Anderson3f6fb772021-02-05 09:38:53 -050034};
35
Simon Glassf376a3c2016-05-01 13:52:42 -060036/**
37 * sandbox_mmc_send_cmd() - Emulate SD commands
38 *
39 * This emulate an SD card version 2. Single-block reads result in zero data.
40 * Multiple-block reads return a test string.
41 */
Simon Glass9a46bd32016-06-12 23:30:26 -060042static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassf376a3c2016-05-01 13:52:42 -060043 struct mmc_data *data)
44{
Sean Anderson3f6fb772021-02-05 09:38:53 -050045 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
Sean Anderson3f6fb772021-02-05 09:38:53 -050046 static ulong erase_start, erase_end;
47
Simon Glassf376a3c2016-05-01 13:52:42 -060048 switch (cmd->cmdidx) {
49 case MMC_CMD_ALL_SEND_CID:
Simon Glass5abd58f2019-09-25 08:56:00 -060050 memset(cmd->response, '\0', sizeof(cmd->response));
Simon Glassf376a3c2016-05-01 13:52:42 -060051 break;
52 case SD_CMD_SEND_RELATIVE_ADDR:
53 cmd->response[0] = 0 << 16; /* mmc->rca */
54 case MMC_CMD_GO_IDLE_STATE:
55 break;
56 case SD_CMD_SEND_IF_COND:
57 cmd->response[0] = 0xaa;
58 break;
59 case MMC_CMD_SEND_STATUS:
60 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
61 break;
62 case MMC_CMD_SELECT_CARD:
63 break;
64 case MMC_CMD_SEND_CSD:
65 cmd->response[0] = 0;
Sean Anderson3f6fb772021-02-05 09:38:53 -050066 cmd->response[1] = (MMC_BL_LEN_SHIFT << 16) |
Simon Glass0bf61ac2021-10-23 17:25:59 -060067 ((priv->csize >> 16) & 0x3f);
68 cmd->response[2] = (priv->csize & 0xffff) << 16;
Simon Glass5abd58f2019-09-25 08:56:00 -060069 cmd->response[3] = 0;
Simon Glassf376a3c2016-05-01 13:52:42 -060070 break;
71 case SD_CMD_SWITCH_FUNC: {
Jean-Jacques Hiblot49f89252017-12-14 11:47:14 +010072 if (!data)
73 break;
Simon Glassf376a3c2016-05-01 13:52:42 -060074 u32 *resp = (u32 *)data->dest;
Simon Glass5abd58f2019-09-25 08:56:00 -060075 resp[3] = 0;
Simon Glassf376a3c2016-05-01 13:52:42 -060076 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
Jean-Jacques Hiblot49f89252017-12-14 11:47:14 +010077 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
78 resp[4] = (cmd->cmdarg & 0xF) << 24;
Simon Glassf376a3c2016-05-01 13:52:42 -060079 break;
80 }
81 case MMC_CMD_READ_SINGLE_BLOCK:
Simon Glassf376a3c2016-05-01 13:52:42 -060082 case MMC_CMD_READ_MULTIPLE_BLOCK:
Sean Anderson3f6fb772021-02-05 09:38:53 -050083 memcpy(data->dest, &priv->buf[cmd->cmdarg * data->blocksize],
84 data->blocks * data->blocksize);
85 break;
86 case MMC_CMD_WRITE_SINGLE_BLOCK:
87 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
88 memcpy(&priv->buf[cmd->cmdarg * data->blocksize], data->src,
89 data->blocks * data->blocksize);
Simon Glassf376a3c2016-05-01 13:52:42 -060090 break;
91 case MMC_CMD_STOP_TRANSMISSION:
92 break;
Sean Anderson3f6fb772021-02-05 09:38:53 -050093 case SD_CMD_ERASE_WR_BLK_START:
94 erase_start = cmd->cmdarg;
95 break;
96 case SD_CMD_ERASE_WR_BLK_END:
97 erase_end = cmd->cmdarg;
98 break;
Simon Glass372a7d92022-10-20 18:22:58 -060099#if CONFIG_IS_ENABLED(MMC_WRITE)
100 case MMC_CMD_ERASE: {
101 struct mmc *mmc = mmc_get_mmc_dev(dev);
102
Sean Anderson3f6fb772021-02-05 09:38:53 -0500103 memset(&priv->buf[erase_start * mmc->write_bl_len], '\0',
104 (erase_end - erase_start + 1) * mmc->write_bl_len);
105 break;
Simon Glass372a7d92022-10-20 18:22:58 -0600106 }
107#endif
Simon Glassf376a3c2016-05-01 13:52:42 -0600108 case SD_CMD_APP_SEND_OP_COND:
109 cmd->response[0] = OCR_BUSY | OCR_HCS;
110 cmd->response[1] = 0;
111 cmd->response[2] = 0;
112 break;
113 case MMC_CMD_APP_CMD:
114 break;
115 case MMC_CMD_SET_BLOCKLEN:
116 debug("block len %d\n", cmd->cmdarg);
117 break;
118 case SD_CMD_APP_SEND_SCR: {
119 u32 *scr = (u32 *)data->dest;
120
121 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
122 break;
123 }
124 default:
125 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
126 break;
127 }
128
129 return 0;
130}
131
Simon Glass9a46bd32016-06-12 23:30:26 -0600132static int sandbox_mmc_set_ios(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600133{
134 return 0;
135}
136
Simon Glass9a46bd32016-06-12 23:30:26 -0600137static int sandbox_mmc_get_cd(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600138{
139 return 1;
140}
141
Simon Glass9a46bd32016-06-12 23:30:26 -0600142static const struct dm_mmc_ops sandbox_mmc_ops = {
Simon Glassf376a3c2016-05-01 13:52:42 -0600143 .send_cmd = sandbox_mmc_send_cmd,
144 .set_ios = sandbox_mmc_set_ios,
Simon Glass9a46bd32016-06-12 23:30:26 -0600145 .get_cd = sandbox_mmc_get_cd,
Simon Glassf376a3c2016-05-01 13:52:42 -0600146};
147
Simon Glass6b165ab2021-07-05 16:32:58 -0600148static int sandbox_mmc_of_to_plat(struct udevice *dev)
149{
150 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
151 struct mmc_config *cfg = &plat->cfg;
152 struct blk_desc *blk;
153 int ret;
154
Simon Glass0bf61ac2021-10-23 17:25:59 -0600155 plat->fname = dev_read_string(dev, "filename");
156
Simon Glass6b165ab2021-07-05 16:32:58 -0600157 ret = mmc_of_parse(dev, cfg);
158 if (ret)
159 return ret;
160 blk = mmc_get_blk_desc(&plat->mmc);
161 if (blk)
162 blk->removable = !(cfg->host_caps & MMC_CAP_NONREMOVABLE);
163
164 return 0;
165}
166
167static int sandbox_mmc_probe(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600168{
Simon Glassc69cda22020-12-03 16:55:20 -0700169 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
Simon Glass0bf61ac2021-10-23 17:25:59 -0600170 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
171 int ret;
172
173 if (plat->fname) {
174 ret = os_map_file(plat->fname, OS_O_RDWR | OS_O_CREAT,
175 (void **)&priv->buf, &priv->size);
176 if (ret) {
177 log_err("%s: Unable to map file '%s'\n", dev->name,
178 plat->fname);
179 return ret;
180 }
181 priv->csize = priv->size / SIZE_MULTIPLE - 1;
182 } else {
183 priv->csize = 0;
184 priv->size = (priv->csize + 1) * SIZE_MULTIPLE; /* 1 MiB */
185
Simon Glass67bb9732023-01-17 10:47:19 -0700186 priv->buf = calloc(1, priv->size);
Simon Glass0bf61ac2021-10-23 17:25:59 -0600187 if (!priv->buf) {
188 log_err("%s: Not enough memory (%x bytes)\n",
189 dev->name, priv->size);
190 return -ENOMEM;
191 }
192 }
Simon Glassf376a3c2016-05-01 13:52:42 -0600193
194 return mmc_init(&plat->mmc);
195}
196
Simon Glass0bf61ac2021-10-23 17:25:59 -0600197static int sandbox_mmc_remove(struct udevice *dev)
198{
199 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
200 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
201
202 if (plat->fname)
203 os_unmap(priv->buf, priv->size);
204 else
205 free(priv->buf);
206
207 return 0;
208}
209
Simon Glass6b165ab2021-07-05 16:32:58 -0600210static int sandbox_mmc_bind(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600211{
Simon Glassc69cda22020-12-03 16:55:20 -0700212 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
Simon Glassf376a3c2016-05-01 13:52:42 -0600213 struct mmc_config *cfg = &plat->cfg;
Simon Glassf376a3c2016-05-01 13:52:42 -0600214
215 cfg->name = dev->name;
Simon Glassf376a3c2016-05-01 13:52:42 -0600216 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
217 cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
218 cfg->f_min = 1000000;
219 cfg->f_max = 52000000;
220 cfg->b_max = U32_MAX;
221
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900222 return mmc_bind(dev, &plat->mmc, cfg);
Simon Glassf376a3c2016-05-01 13:52:42 -0600223}
224
Simon Glass6b165ab2021-07-05 16:32:58 -0600225static int sandbox_mmc_unbind(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600226{
227 mmc_unbind(dev);
228
229 return 0;
230}
231
Simon Glass8e6cc462015-07-06 12:54:32 -0600232static const struct udevice_id sandbox_mmc_ids[] = {
233 { .compatible = "sandbox,mmc" },
234 { }
235};
236
Simon Glassf376a3c2016-05-01 13:52:42 -0600237U_BOOT_DRIVER(mmc_sandbox) = {
Simon Glass8e6cc462015-07-06 12:54:32 -0600238 .name = "mmc_sandbox",
239 .id = UCLASS_MMC,
240 .of_match = sandbox_mmc_ids,
Simon Glass9a46bd32016-06-12 23:30:26 -0600241 .ops = &sandbox_mmc_ops,
Simon Glassf376a3c2016-05-01 13:52:42 -0600242 .bind = sandbox_mmc_bind,
243 .unbind = sandbox_mmc_unbind,
Simon Glass6b165ab2021-07-05 16:32:58 -0600244 .of_to_plat = sandbox_mmc_of_to_plat,
Simon Glassf376a3c2016-05-01 13:52:42 -0600245 .probe = sandbox_mmc_probe,
Simon Glass0bf61ac2021-10-23 17:25:59 -0600246 .remove = sandbox_mmc_remove,
Sean Anderson3f6fb772021-02-05 09:38:53 -0500247 .priv_auto = sizeof(struct sandbox_mmc_priv),
248 .plat_auto = sizeof(struct sandbox_mmc_plat),
Simon Glass8e6cc462015-07-06 12:54:32 -0600249};