xref: /openbmc/u-boot/drivers/mmc/sandbox_mmc.c (revision d024236e5a31a2b4b82cbcc98b31b8170fc88d28)
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 struct 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  */
26 static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
27 				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: {
49 		if (!data)
50 			break;
51 		u32 *resp = (u32 *)data->dest;
52 		resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
53 		if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
54 			resp[4] = (cmd->cmdarg & 0xF) << 24;
55 		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 
89 static int sandbox_mmc_set_ios(struct udevice *dev)
90 {
91 	return 0;
92 }
93 
94 static int sandbox_mmc_get_cd(struct udevice *dev)
95 {
96 	return 1;
97 }
98 
99 static const struct dm_mmc_ops sandbox_mmc_ops = {
100 	.send_cmd = sandbox_mmc_send_cmd,
101 	.set_ios = sandbox_mmc_set_ios,
102 	.get_cd = sandbox_mmc_get_cd,
103 };
104 
105 int 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 
112 int sandbox_mmc_bind(struct udevice *dev)
113 {
114 	struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
115 	struct mmc_config *cfg = &plat->cfg;
116 
117 	cfg->name = dev->name;
118 	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 
124 	return mmc_bind(dev, &plat->mmc, cfg);
125 }
126 
127 int sandbox_mmc_unbind(struct udevice *dev)
128 {
129 	mmc_unbind(dev);
130 
131 	return 0;
132 }
133 
134 static const struct udevice_id sandbox_mmc_ids[] = {
135 	{ .compatible = "sandbox,mmc" },
136 	{ }
137 };
138 
139 U_BOOT_DRIVER(mmc_sandbox) = {
140 	.name		= "mmc_sandbox",
141 	.id		= UCLASS_MMC,
142 	.of_match	= sandbox_mmc_ids,
143 	.ops		= &sandbox_mmc_ops,
144 	.bind		= sandbox_mmc_bind,
145 	.unbind		= sandbox_mmc_unbind,
146 	.probe		= sandbox_mmc_probe,
147 	.platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat),
148 };
149