blob: 0945beb368ae9c3682f4e7a41201fcadc4180632 [file] [log] [blame]
Simon Glassd3e58e42015-07-06 12:54:32 -06001/*
2 * Copyright (c) 2015 Google, Inc
3 * Written by Simon Glass <sjg@chromium.org>
4 *
5 * SPDX-License-Identifier: GPL-2.0+
6 */
7
8#include <common.h>
9#include <dm.h>
10#include <errno.h>
Simon Glassf9eebac2016-05-01 13:52:42 -060011#include <fdtdec.h>
Simon Glassd3e58e42015-07-06 12:54:32 -060012#include <mmc.h>
13#include <asm/test.h>
14
Simon Glassf9eebac2016-05-01 13:52:42 -060015struct sandbox_mmc_plat {
16 struct mmc_config cfg;
17 struct mmc mmc;
18};
19
20/**
21 * sandbox_mmc_send_cmd() - Emulate SD commands
22 *
23 * This emulate an SD card version 2. Single-block reads result in zero data.
24 * Multiple-block reads return a test string.
25 */
Simon Glassefcc6692016-06-12 23:30:26 -060026static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glassf9eebac2016-05-01 13:52:42 -060027 struct mmc_data *data)
28{
29 switch (cmd->cmdidx) {
30 case MMC_CMD_ALL_SEND_CID:
31 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 */
47 break;
48 case SD_CMD_SWITCH_FUNC: {
Jean-Jacques Hiblotef72fd32017-12-14 11:47:14 +010049 if (!data)
50 break;
Simon Glassf9eebac2016-05-01 13:52:42 -060051 u32 *resp = (u32 *)data->dest;
Simon Glassf9eebac2016-05-01 13:52:42 -060052 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
Jean-Jacques Hiblotef72fd32017-12-14 11:47:14 +010053 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
54 resp[4] = (cmd->cmdarg & 0xF) << 24;
Simon Glassf9eebac2016-05-01 13:52:42 -060055 break;
56 }
57 case MMC_CMD_READ_SINGLE_BLOCK:
58 memset(data->dest, '\0', data->blocksize);
59 break;
60 case MMC_CMD_READ_MULTIPLE_BLOCK:
61 strcpy(data->dest, "this is a test");
62 break;
63 case MMC_CMD_STOP_TRANSMISSION:
64 break;
65 case SD_CMD_APP_SEND_OP_COND:
66 cmd->response[0] = OCR_BUSY | OCR_HCS;
67 cmd->response[1] = 0;
68 cmd->response[2] = 0;
69 break;
70 case MMC_CMD_APP_CMD:
71 break;
72 case MMC_CMD_SET_BLOCKLEN:
73 debug("block len %d\n", cmd->cmdarg);
74 break;
75 case SD_CMD_APP_SEND_SCR: {
76 u32 *scr = (u32 *)data->dest;
77
78 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
79 break;
80 }
81 default:
82 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
83 break;
84 }
85
86 return 0;
87}
88
Simon Glassefcc6692016-06-12 23:30:26 -060089static int sandbox_mmc_set_ios(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -060090{
91 return 0;
92}
93
Simon Glassefcc6692016-06-12 23:30:26 -060094static int sandbox_mmc_get_cd(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -060095{
96 return 1;
97}
98
Simon Glassefcc6692016-06-12 23:30:26 -060099static const struct dm_mmc_ops sandbox_mmc_ops = {
Simon Glassf9eebac2016-05-01 13:52:42 -0600100 .send_cmd = sandbox_mmc_send_cmd,
101 .set_ios = sandbox_mmc_set_ios,
Simon Glassefcc6692016-06-12 23:30:26 -0600102 .get_cd = sandbox_mmc_get_cd,
Simon Glassf9eebac2016-05-01 13:52:42 -0600103};
104
105int sandbox_mmc_probe(struct udevice *dev)
106{
107 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
108
109 return mmc_init(&plat->mmc);
110}
111
112int sandbox_mmc_bind(struct udevice *dev)
113{
114 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
115 struct mmc_config *cfg = &plat->cfg;
Simon Glassf9eebac2016-05-01 13:52:42 -0600116
117 cfg->name = dev->name;
Simon Glassf9eebac2016-05-01 13:52:42 -0600118 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
119 cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
120 cfg->f_min = 1000000;
121 cfg->f_max = 52000000;
122 cfg->b_max = U32_MAX;
123
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900124 return mmc_bind(dev, &plat->mmc, cfg);
Simon Glassf9eebac2016-05-01 13:52:42 -0600125}
126
127int sandbox_mmc_unbind(struct udevice *dev)
128{
129 mmc_unbind(dev);
130
131 return 0;
132}
133
Simon Glassd3e58e42015-07-06 12:54:32 -0600134static const struct udevice_id sandbox_mmc_ids[] = {
135 { .compatible = "sandbox,mmc" },
136 { }
137};
138
Simon Glassf9eebac2016-05-01 13:52:42 -0600139U_BOOT_DRIVER(mmc_sandbox) = {
Simon Glassd3e58e42015-07-06 12:54:32 -0600140 .name = "mmc_sandbox",
141 .id = UCLASS_MMC,
142 .of_match = sandbox_mmc_ids,
Simon Glassefcc6692016-06-12 23:30:26 -0600143 .ops = &sandbox_mmc_ops,
Simon Glassf9eebac2016-05-01 13:52:42 -0600144 .bind = sandbox_mmc_bind,
145 .unbind = sandbox_mmc_unbind,
146 .probe = sandbox_mmc_probe,
147 .platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat),
Simon Glassd3e58e42015-07-06 12:54:32 -0600148};