blob: 899952d773cdd063122cbb37382f5e8bf82c6231 [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 Glass8e6cc462015-07-06 12:54:32 -060011#include <mmc.h>
12#include <asm/test.h>
13
Simon Glassf376a3c2016-05-01 13:52:42 -060014struct sandbox_mmc_plat {
15 struct mmc_config cfg;
16 struct mmc mmc;
17};
18
19/**
20 * sandbox_mmc_send_cmd() - Emulate SD commands
21 *
22 * This emulate an SD card version 2. Single-block reads result in zero data.
23 * Multiple-block reads return a test string.
24 */
Simon Glass9a46bd32016-06-12 23:30:26 -060025static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassf376a3c2016-05-01 13:52:42 -060026 struct mmc_data *data)
27{
28 switch (cmd->cmdidx) {
29 case MMC_CMD_ALL_SEND_CID:
Simon Glass5abd58f2019-09-25 08:56:00 -060030 memset(cmd->response, '\0', sizeof(cmd->response));
Simon Glassf376a3c2016-05-01 13:52:42 -060031 break;
32 case SD_CMD_SEND_RELATIVE_ADDR:
33 cmd->response[0] = 0 << 16; /* mmc->rca */
34 case MMC_CMD_GO_IDLE_STATE:
35 break;
36 case SD_CMD_SEND_IF_COND:
37 cmd->response[0] = 0xaa;
38 break;
39 case MMC_CMD_SEND_STATUS:
40 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
41 break;
42 case MMC_CMD_SELECT_CARD:
43 break;
44 case MMC_CMD_SEND_CSD:
45 cmd->response[0] = 0;
46 cmd->response[1] = 10 << 16; /* 1 << block_len */
Simon Glass5abd58f2019-09-25 08:56:00 -060047 cmd->response[2] = 0;
48 cmd->response[3] = 0;
Simon Glassf376a3c2016-05-01 13:52:42 -060049 break;
50 case SD_CMD_SWITCH_FUNC: {
Jean-Jacques Hiblot49f89252017-12-14 11:47:14 +010051 if (!data)
52 break;
Simon Glassf376a3c2016-05-01 13:52:42 -060053 u32 *resp = (u32 *)data->dest;
Simon Glass5abd58f2019-09-25 08:56:00 -060054 resp[3] = 0;
Simon Glassf376a3c2016-05-01 13:52:42 -060055 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
Jean-Jacques Hiblot49f89252017-12-14 11:47:14 +010056 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
57 resp[4] = (cmd->cmdarg & 0xF) << 24;
Simon Glassf376a3c2016-05-01 13:52:42 -060058 break;
59 }
60 case MMC_CMD_READ_SINGLE_BLOCK:
61 memset(data->dest, '\0', data->blocksize);
62 break;
63 case MMC_CMD_READ_MULTIPLE_BLOCK:
64 strcpy(data->dest, "this is a test");
65 break;
66 case MMC_CMD_STOP_TRANSMISSION:
67 break;
68 case SD_CMD_APP_SEND_OP_COND:
69 cmd->response[0] = OCR_BUSY | OCR_HCS;
70 cmd->response[1] = 0;
71 cmd->response[2] = 0;
72 break;
73 case MMC_CMD_APP_CMD:
74 break;
75 case MMC_CMD_SET_BLOCKLEN:
76 debug("block len %d\n", cmd->cmdarg);
77 break;
78 case SD_CMD_APP_SEND_SCR: {
79 u32 *scr = (u32 *)data->dest;
80
81 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
82 break;
83 }
84 default:
85 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
86 break;
87 }
88
89 return 0;
90}
91
Simon Glass9a46bd32016-06-12 23:30:26 -060092static int sandbox_mmc_set_ios(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -060093{
94 return 0;
95}
96
Simon Glass9a46bd32016-06-12 23:30:26 -060097static int sandbox_mmc_get_cd(struct udevice *dev)
Simon Glassf376a3c2016-05-01 13:52:42 -060098{
99 return 1;
100}
101
Simon Glass9a46bd32016-06-12 23:30:26 -0600102static const struct dm_mmc_ops sandbox_mmc_ops = {
Simon Glassf376a3c2016-05-01 13:52:42 -0600103 .send_cmd = sandbox_mmc_send_cmd,
104 .set_ios = sandbox_mmc_set_ios,
Simon Glass9a46bd32016-06-12 23:30:26 -0600105 .get_cd = sandbox_mmc_get_cd,
Simon Glassf376a3c2016-05-01 13:52:42 -0600106};
107
108int sandbox_mmc_probe(struct udevice *dev)
109{
110 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
111
112 return mmc_init(&plat->mmc);
113}
114
115int sandbox_mmc_bind(struct udevice *dev)
116{
117 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
118 struct mmc_config *cfg = &plat->cfg;
Simon Glassf376a3c2016-05-01 13:52:42 -0600119
120 cfg->name = dev->name;
Simon Glassf376a3c2016-05-01 13:52:42 -0600121 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;
126
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900127 return mmc_bind(dev, &plat->mmc, cfg);
Simon Glassf376a3c2016-05-01 13:52:42 -0600128}
129
130int sandbox_mmc_unbind(struct udevice *dev)
131{
132 mmc_unbind(dev);
133
134 return 0;
135}
136
Simon Glass8e6cc462015-07-06 12:54:32 -0600137static const struct udevice_id sandbox_mmc_ids[] = {
138 { .compatible = "sandbox,mmc" },
139 { }
140};
141
Simon Glassf376a3c2016-05-01 13:52:42 -0600142U_BOOT_DRIVER(mmc_sandbox) = {
Simon Glass8e6cc462015-07-06 12:54:32 -0600143 .name = "mmc_sandbox",
144 .id = UCLASS_MMC,
145 .of_match = sandbox_mmc_ids,
Simon Glass9a46bd32016-06-12 23:30:26 -0600146 .ops = &sandbox_mmc_ops,
Simon Glassf376a3c2016-05-01 13:52:42 -0600147 .bind = sandbox_mmc_bind,
148 .unbind = sandbox_mmc_unbind,
149 .probe = sandbox_mmc_probe,
150 .platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat),
Simon Glass8e6cc462015-07-06 12:54:32 -0600151};