blob: e86ea8fe096878376c43ccc47ab06ba44075e960 [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
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
Simon Glassf9eebac2016-05-01 13:52:42 -060010#include <fdtdec.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.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:
Simon Glassb1970542019-09-25 08:56:00 -060031 memset(cmd->response, '\0', sizeof(cmd->response));
Simon Glassf9eebac2016-05-01 13:52:42 -060032 break;
33 case SD_CMD_SEND_RELATIVE_ADDR:
34 cmd->response[0] = 0 << 16; /* mmc->rca */
35 case MMC_CMD_GO_IDLE_STATE:
36 break;
37 case SD_CMD_SEND_IF_COND:
38 cmd->response[0] = 0xaa;
39 break;
40 case MMC_CMD_SEND_STATUS:
41 cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
42 break;
43 case MMC_CMD_SELECT_CARD:
44 break;
45 case MMC_CMD_SEND_CSD:
46 cmd->response[0] = 0;
47 cmd->response[1] = 10 << 16; /* 1 << block_len */
Simon Glassb1970542019-09-25 08:56:00 -060048 cmd->response[2] = 0;
49 cmd->response[3] = 0;
Simon Glassf9eebac2016-05-01 13:52:42 -060050 break;
51 case SD_CMD_SWITCH_FUNC: {
Jean-Jacques Hiblotef72fd32017-12-14 11:47:14 +010052 if (!data)
53 break;
Simon Glassf9eebac2016-05-01 13:52:42 -060054 u32 *resp = (u32 *)data->dest;
Simon Glassb1970542019-09-25 08:56:00 -060055 resp[3] = 0;
Simon Glassf9eebac2016-05-01 13:52:42 -060056 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
Jean-Jacques Hiblotef72fd32017-12-14 11:47:14 +010057 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
58 resp[4] = (cmd->cmdarg & 0xF) << 24;
Simon Glassf9eebac2016-05-01 13:52:42 -060059 break;
60 }
61 case MMC_CMD_READ_SINGLE_BLOCK:
62 memset(data->dest, '\0', data->blocksize);
63 break;
64 case MMC_CMD_READ_MULTIPLE_BLOCK:
65 strcpy(data->dest, "this is a test");
66 break;
67 case MMC_CMD_STOP_TRANSMISSION:
68 break;
69 case SD_CMD_APP_SEND_OP_COND:
70 cmd->response[0] = OCR_BUSY | OCR_HCS;
71 cmd->response[1] = 0;
72 cmd->response[2] = 0;
73 break;
74 case MMC_CMD_APP_CMD:
75 break;
76 case MMC_CMD_SET_BLOCKLEN:
77 debug("block len %d\n", cmd->cmdarg);
78 break;
79 case SD_CMD_APP_SEND_SCR: {
80 u32 *scr = (u32 *)data->dest;
81
82 scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */
83 break;
84 }
85 default:
86 debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
87 break;
88 }
89
90 return 0;
91}
92
Simon Glassefcc6692016-06-12 23:30:26 -060093static int sandbox_mmc_set_ios(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -060094{
95 return 0;
96}
97
Simon Glassefcc6692016-06-12 23:30:26 -060098static int sandbox_mmc_get_cd(struct udevice *dev)
Simon Glassf9eebac2016-05-01 13:52:42 -060099{
100 return 1;
101}
102
Simon Glassefcc6692016-06-12 23:30:26 -0600103static const struct dm_mmc_ops sandbox_mmc_ops = {
Simon Glassf9eebac2016-05-01 13:52:42 -0600104 .send_cmd = sandbox_mmc_send_cmd,
105 .set_ios = sandbox_mmc_set_ios,
Simon Glassefcc6692016-06-12 23:30:26 -0600106 .get_cd = sandbox_mmc_get_cd,
Simon Glassf9eebac2016-05-01 13:52:42 -0600107};
108
109int sandbox_mmc_probe(struct udevice *dev)
110{
111 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
112
113 return mmc_init(&plat->mmc);
114}
115
116int sandbox_mmc_bind(struct udevice *dev)
117{
118 struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
119 struct mmc_config *cfg = &plat->cfg;
Simon Glassf9eebac2016-05-01 13:52:42 -0600120
121 cfg->name = dev->name;
Simon Glassf9eebac2016-05-01 13:52:42 -0600122 cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
123 cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
124 cfg->f_min = 1000000;
125 cfg->f_max = 52000000;
126 cfg->b_max = U32_MAX;
127
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900128 return mmc_bind(dev, &plat->mmc, cfg);
Simon Glassf9eebac2016-05-01 13:52:42 -0600129}
130
131int sandbox_mmc_unbind(struct udevice *dev)
132{
133 mmc_unbind(dev);
134
135 return 0;
136}
137
Simon Glassd3e58e42015-07-06 12:54:32 -0600138static const struct udevice_id sandbox_mmc_ids[] = {
139 { .compatible = "sandbox,mmc" },
140 { }
141};
142
Simon Glassf9eebac2016-05-01 13:52:42 -0600143U_BOOT_DRIVER(mmc_sandbox) = {
Simon Glassd3e58e42015-07-06 12:54:32 -0600144 .name = "mmc_sandbox",
145 .id = UCLASS_MMC,
146 .of_match = sandbox_mmc_ids,
Simon Glassefcc6692016-06-12 23:30:26 -0600147 .ops = &sandbox_mmc_ops,
Simon Glassf9eebac2016-05-01 13:52:42 -0600148 .bind = sandbox_mmc_bind,
149 .unbind = sandbox_mmc_unbind,
150 .probe = sandbox_mmc_probe,
151 .platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat),
Simon Glassd3e58e42015-07-06 12:54:32 -0600152};