mmc: fsl_esdhc: clean up DM and non-DM code
[oweals/u-boot.git] / drivers / mmc / sandbox_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2015 Google, Inc
4  * Written by Simon Glass <sjg@chromium.org>
5  */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <errno.h>
10 #include <fdtdec.h>
11 #include <mmc.h>
12 #include <asm/test.h>
13
14 struct 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  */
25 static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
26                                 struct mmc_data *data)
27 {
28         switch (cmd->cmdidx) {
29         case MMC_CMD_ALL_SEND_CID:
30                 memset(cmd->response, '\0', sizeof(cmd->response));
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                 cmd->response[2] = 0;
48                 cmd->response[3] = 0;
49                 break;
50         case SD_CMD_SWITCH_FUNC: {
51                 if (!data)
52                         break;
53                 u32 *resp = (u32 *)data->dest;
54                 resp[3] = 0;
55                 resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY);
56                 if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED)
57                         resp[4] = (cmd->cmdarg & 0xF) << 24;
58                 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
92 static int sandbox_mmc_set_ios(struct udevice *dev)
93 {
94         return 0;
95 }
96
97 static int sandbox_mmc_get_cd(struct udevice *dev)
98 {
99         return 1;
100 }
101
102 static const struct dm_mmc_ops sandbox_mmc_ops = {
103         .send_cmd = sandbox_mmc_send_cmd,
104         .set_ios = sandbox_mmc_set_ios,
105         .get_cd = sandbox_mmc_get_cd,
106 };
107
108 int 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
115 int sandbox_mmc_bind(struct udevice *dev)
116 {
117         struct sandbox_mmc_plat *plat = dev_get_platdata(dev);
118         struct mmc_config *cfg = &plat->cfg;
119
120         cfg->name = dev->name;
121         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
127         return mmc_bind(dev, &plat->mmc, cfg);
128 }
129
130 int sandbox_mmc_unbind(struct udevice *dev)
131 {
132         mmc_unbind(dev);
133
134         return 0;
135 }
136
137 static const struct udevice_id sandbox_mmc_ids[] = {
138         { .compatible = "sandbox,mmc" },
139         { }
140 };
141
142 U_BOOT_DRIVER(mmc_sandbox) = {
143         .name           = "mmc_sandbox",
144         .id             = UCLASS_MMC,
145         .of_match       = sandbox_mmc_ids,
146         .ops            = &sandbox_mmc_ops,
147         .bind           = sandbox_mmc_bind,
148         .unbind         = sandbox_mmc_unbind,
149         .probe          = sandbox_mmc_probe,
150         .platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat),
151 };