blob: 895fbffecfcdb3ad108031f62b04fc02e1e8cf7c [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 Glass8e6cc462015-07-06 12:54:32 -060012#include <mmc.h>
13#include <asm/test.h>
14
Simon Glassf376a3c2016-05-01 13:52:42 -060015struct sandbox_mmc_plat {
16 struct mmc_config cfg;
17 struct mmc mmc;
18};
19
Sean Anderson3f6fb772021-02-05 09:38:53 -050020#define MMC_CSIZE 0
21#define MMC_CMULT 8 /* 8 because the card is high-capacity */
22#define MMC_BL_LEN_SHIFT 10
23#define MMC_BL_LEN BIT(MMC_BL_LEN_SHIFT)
24#define MMC_CAPACITY (((MMC_CSIZE + 1) << (MMC_CMULT + 2)) \
25 * MMC_BL_LEN) /* 1 MiB */
26
27struct sandbox_mmc_priv {
28 u8 buf[MMC_CAPACITY];
29};
30
Simon Glassf376a3c2016-05-01 13:52:42 -060031/**
32 * sandbox_mmc_send_cmd() - Emulate SD commands
33 *
34 * This emulate an SD card version 2. Single-block reads result in zero data.
35 * Multiple-block reads return a test string.
36 */
Simon Glass9a46bd32016-06-12 23:30:26 -060037static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassf376a3c2016-05-01 13:52:42 -060038 struct mmc_data *data)
39{
Sean Anderson3f6fb772021-02-05 09:38:53 -050040 struct sandbox_mmc_priv *priv = dev_get_priv(dev);
41 struct mmc *mmc = mmc_get_mmc_dev(dev);
42 static ulong erase_start, erase_end;
43
Simon Glassf376a3c2016-05-01 13:52:42 -060044 switch (cmd->cmdidx) {
45 case MMC_CMD_ALL_SEND_CID:
Simon Glass5abd58f2019-09-25 08:56:00 -060046 memset(cmd->response, '\0', sizeof(cmd->response));
Simon Glassf376a3c2016-05-01 13:52:42 -060047 break;
48 case SD_CMD_SEND_RELATIVE_ADDR:
49 cmd->response[0] = 0 << 16; /* mmc->rca */
50 case MMC_CMD_GO_IDLE_STATE:
51 break;
52 case SD_CMD_SEND_IF_COND:
53 cmd->response[0] = 0xaa;
54 break;
55 case MMC_CMD_SEND_STATUS:
56 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
57 break;
58 case MMC_CMD_SELECT_CARD:
59 break;
60 case MMC_CMD_SEND_CSD:
61 cmd->response[0] = 0;
Sean Anderson3f6fb772021-02-05 09:38:53 -050062 cmd->response[1] = (MMC_BL_LEN_SHIFT << 16) |
63 ((MMC_CSIZE >> 16) & 0x3f);
64 cmd->response[2] = (MMC_CSIZE & 0xffff) << 16;
Simon Glass5abd58f2019-09-25 08:56:00 -060065 cmd->response[3] = 0;
Simon Glassf376a3c2016-05-01 13:52:42 -060066 break;
67 case SD_CMD_SWITCH_FUNC: {
Jean-Jacques Hiblot49f89252017-12-14 11:47:14 +010068 if (!data)
69 break;
Simon Glassf376a3c2016-05-01 13:52:42 -060070 u32 *resp = (u32 *)data->dest;
Simon Glass5abd58f2019-09-25 08:56:00 -060071 resp[3] = 0;
Simon Glassf376a3c2016-05-01 13:52:42 -060072 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
Jean-Jacques Hiblot49f89252017-12-14 11:47:14 +010073 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
74 resp[4] = (cmd->cmdarg & 0xF) << 24;
Simon Glassf376a3c2016-05-01 13:52:42 -060075 break;
76 }
77 case MMC_CMD_READ_SINGLE_BLOCK:
Simon Glassf376a3c2016-05-01 13:52:42 -060078 case MMC_CMD_READ_MULTIPLE_BLOCK:
Sean Anderson3f6fb772021-02-05 09:38:53 -050079 memcpy(data->dest, &priv->buf[cmd->cmdarg * data->blocksize],
80 data->blocks * data->blocksize);
81 break;
82 case MMC_CMD_WRITE_SINGLE_BLOCK:
83 case MMC_CMD_WRITE_MULTIPLE_BLOCK:
84 memcpy(&priv->buf[cmd->cmdarg * data->blocksize], data->src,
85 data->blocks * data->blocksize);
Simon Glassf376a3c2016-05-01 13:52:42 -060086 break;
87 case MMC_CMD_STOP_TRANSMISSION:
88 break;
Sean Anderson3f6fb772021-02-05 09:38:53 -050089 case SD_CMD_ERASE_WR_BLK_START:
90 erase_start = cmd->cmdarg;
91 break;
92 case SD_CMD_ERASE_WR_BLK_END:
93 erase_end = cmd->cmdarg;
94 break;
95 case MMC_CMD_ERASE:
96 memset(&priv->buf[erase_start * mmc->write_bl_len], '\0',
97 (erase_end - erase_start + 1) * mmc->write_bl_len);
98 break;
Simon Glassf376a3c2016-05-01 13:52:42 -060099 case SD_CMD_APP_SEND_OP_COND:
100 cmd->response[0] = OCR_BUSY | OCR_HCS;
101 cmd->response[1] = 0;
102 cmd->response[2] = 0;
103 break;
104 case MMC_CMD_APP_CMD:
105 break;
106 case MMC_CMD_SET_BLOCKLEN:
107 debug("block len %d\n", cmd->cmdarg);
108 break;
109 case SD_CMD_APP_SEND_SCR: {
110 u32 *scr = (u32 *)data->dest;
111
112 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
113 break;
114 }
115 default:
116 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
117 break;
118 }
119
120 return 0;
121}
122
Simon Glass9a46bd32016-06-12 23:30:26 -0600123static int sandbox_mmc_set_ios(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600124{
125 return 0;
126}
127
Simon Glass9a46bd32016-06-12 23:30:26 -0600128static int sandbox_mmc_get_cd(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600129{
130 return 1;
131}
132
Simon Glass9a46bd32016-06-12 23:30:26 -0600133static const struct dm_mmc_ops sandbox_mmc_ops = {
Simon Glassf376a3c2016-05-01 13:52:42 -0600134 .send_cmd = sandbox_mmc_send_cmd,
135 .set_ios = sandbox_mmc_set_ios,
Simon Glass9a46bd32016-06-12 23:30:26 -0600136 .get_cd = sandbox_mmc_get_cd,
Simon Glassf376a3c2016-05-01 13:52:42 -0600137};
138
Simon Glass6b165ab2021-07-05 16:32:58 -0600139static int sandbox_mmc_of_to_plat(struct udevice *dev)
140{
141 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
142 struct mmc_config *cfg = &plat->cfg;
143 struct blk_desc *blk;
144 int ret;
145
146 ret = mmc_of_parse(dev, cfg);
147 if (ret)
148 return ret;
149 blk = mmc_get_blk_desc(&plat->mmc);
150 if (blk)
151 blk->removable = !(cfg->host_caps & MMC_CAP_NONREMOVABLE);
152
153 return 0;
154}
155
156static int sandbox_mmc_probe(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600157{
Simon Glassc69cda22020-12-03 16:55:20 -0700158 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
Simon Glassf376a3c2016-05-01 13:52:42 -0600159
160 return mmc_init(&plat->mmc);
161}
162
Simon Glass6b165ab2021-07-05 16:32:58 -0600163static int sandbox_mmc_bind(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600164{
Simon Glassc69cda22020-12-03 16:55:20 -0700165 struct sandbox_mmc_plat *plat = dev_get_plat(dev);
Simon Glassf376a3c2016-05-01 13:52:42 -0600166 struct mmc_config *cfg = &plat->cfg;
Simon Glassf376a3c2016-05-01 13:52:42 -0600167
168 cfg->name = dev->name;
Simon Glassf376a3c2016-05-01 13:52:42 -0600169 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
170 cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
171 cfg->f_min = 1000000;
172 cfg->f_max = 52000000;
173 cfg->b_max = U32_MAX;
174
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900175 return mmc_bind(dev, &plat->mmc, cfg);
Simon Glassf376a3c2016-05-01 13:52:42 -0600176}
177
Simon Glass6b165ab2021-07-05 16:32:58 -0600178static int sandbox_mmc_unbind(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -0600179{
180 mmc_unbind(dev);
181
182 return 0;
183}
184
Simon Glass8e6cc462015-07-06 12:54:32 -0600185static const struct udevice_id sandbox_mmc_ids[] = {
186 { .compatible = "sandbox,mmc" },
187 { }
188};
189
Simon Glassf376a3c2016-05-01 13:52:42 -0600190U_BOOT_DRIVER(mmc_sandbox) = {
Simon Glass8e6cc462015-07-06 12:54:32 -0600191 .name = "mmc_sandbox",
192 .id = UCLASS_MMC,
193 .of_match = sandbox_mmc_ids,
Simon Glass9a46bd32016-06-12 23:30:26 -0600194 .ops = &sandbox_mmc_ops,
Simon Glassf376a3c2016-05-01 13:52:42 -0600195 .bind = sandbox_mmc_bind,
196 .unbind = sandbox_mmc_unbind,
Simon Glass6b165ab2021-07-05 16:32:58 -0600197 .of_to_plat = sandbox_mmc_of_to_plat,
Simon Glassf376a3c2016-05-01 13:52:42 -0600198 .probe = sandbox_mmc_probe,
Sean Anderson3f6fb772021-02-05 09:38:53 -0500199 .priv_auto = sizeof(struct sandbox_mmc_priv),
200 .plat_auto = sizeof(struct sandbox_mmc_plat),
Simon Glass8e6cc462015-07-06 12:54:32 -0600201};