xref: /openbmc/u-boot/drivers/mmc/sandbox_mmc.c (revision ee7bb5be)
1 /*
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>
11 #include <fdtdec.h>
12 #include <mmc.h>
13 #include <asm/test.h>
14 
15 DECLARE_GLOBAL_DATA_PTR;
16 
17 struct sandbox_mmc_plat {
18 	struct mmc_config cfg;
19 	struct mmc mmc;
20 };
21 
22 /**
23  * sandbox_mmc_send_cmd() - Emulate SD commands
24  *
25  * This emulate an SD card version 2. Single-block reads result in zero data.
26  * Multiple-block reads return a test string.
27  */
28 static int sandbox_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
29 				struct mmc_data *data)
30 {
31 	switch (cmd->cmdidx) {
32 	case MMC_CMD_ALL_SEND_CID:
33 		break;
34 	case SD_CMD_SEND_RELATIVE_ADDR:
35 		cmd->response[0] = 0 << 16; /* mmc->rca */
36 	case MMC_CMD_GO_IDLE_STATE:
37 		break;
38 	case SD_CMD_SEND_IF_COND:
39 		cmd->response[0] = 0xaa;
40 		break;
41 	case MMC_CMD_SEND_STATUS:
42 		cmd->response[0] = MMC_STATUS_RDY_FOR_DATA;
43 		break;
44 	case MMC_CMD_SELECT_CARD:
45 		break;
46 	case MMC_CMD_SEND_CSD:
47 		cmd->response[0] = 0;
48 		cmd->response[1] = 10 << 16;	/* 1 << block_len */
49 		break;
50 	case SD_CMD_SWITCH_FUNC: {
51 		u32 *resp = (u32 *)data->dest;
52 
53 		resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
54 		break;
55 	}
56 	case MMC_CMD_READ_SINGLE_BLOCK:
57 		memset(data->dest, '\0', data->blocksize);
58 		break;
59 	case MMC_CMD_READ_MULTIPLE_BLOCK:
60 		strcpy(data->dest, "this is a test");
61 		break;
62 	case MMC_CMD_STOP_TRANSMISSION:
63 		break;
64 	case SD_CMD_APP_SEND_OP_COND:
65 		cmd->response[0] = OCR_BUSY | OCR_HCS;
66 		cmd->response[1] = 0;
67 		cmd->response[2] = 0;
68 		break;
69 	case MMC_CMD_APP_CMD:
70 		break;
71 	case MMC_CMD_SET_BLOCKLEN:
72 		debug("block len %d\n", cmd->cmdarg);
73 		break;
74 	case SD_CMD_APP_SEND_SCR: {
75 		u32 *scr = (u32 *)data->dest;
76 
77 		scr[0] = cpu_to_be32(2 << 24 | 1 << 15);  /* SD version 3 */
78 		break;
79 	}
80 	default:
81 		debug("%s: Unknown command %d\n", __func__, cmd->cmdidx);
82 		break;
83 	}
84 
85 	return 0;
86 }
87 
88 static void sandbox_mmc_set_ios(struct mmc *mmc)
89 {
90 }
91 
92 static int sandbox_mmc_init(struct mmc *mmc)
93 {
94 	return 0;
95 }
96 
97 static int sandbox_mmc_getcd(struct mmc *mmc)
98 {
99 	return 1;
100 }
101 
102 static const struct mmc_ops sandbox_mmc_ops = {
103 	.send_cmd = sandbox_mmc_send_cmd,
104 	.set_ios = sandbox_mmc_set_ios,
105 	.init = sandbox_mmc_init,
106 	.getcd = sandbox_mmc_getcd,
107 };
108 
109 int 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 
116 int sandbox_mmc_bind(struct udevice *dev)
117 {
118 	struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
119 	struct mmc_config *cfg = &plat->cfg;
120 	int ret;
121 
122 	cfg->name = dev->name;
123 	cfg->ops = &sandbox_mmc_ops;
124 	cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT;
125 	cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
126 	cfg->f_min = 1000000;
127 	cfg->f_max = 52000000;
128 	cfg->b_max = U32_MAX;
129 
130 	ret = mmc_bind(dev, &plat->mmc, cfg);
131 	if (ret)
132 		return ret;
133 
134 	return 0;
135 }
136 
137 int sandbox_mmc_unbind(struct udevice *dev)
138 {
139 	mmc_unbind(dev);
140 
141 	return 0;
142 }
143 
144 static const struct udevice_id sandbox_mmc_ids[] = {
145 	{ .compatible = "sandbox,mmc" },
146 	{ }
147 };
148 
149 U_BOOT_DRIVER(mmc_sandbox) = {
150 	.name		= "mmc_sandbox",
151 	.id		= UCLASS_MMC,
152 	.of_match	= sandbox_mmc_ids,
153 	.bind		= sandbox_mmc_bind,
154 	.unbind		= sandbox_mmc_unbind,
155 	.probe		= sandbox_mmc_probe,
156 	.platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat),
157 };
158