Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Google, Inc |
| 4 | * Written by Simon Glass <sjg@chromium.org> |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <dm.h> |
| 9 | #include <errno.h> |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 10 | #include <fdtdec.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 13 | #include <mmc.h> |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 14 | #include <os.h> |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 15 | #include <asm/test.h> |
| 16 | |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 17 | struct sandbox_mmc_plat { |
| 18 | struct mmc_config cfg; |
| 19 | struct mmc mmc; |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 20 | const char *fname; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 21 | }; |
| 22 | |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 23 | #define MMC_CMULT 8 /* 8 because the card is high-capacity */ |
| 24 | #define MMC_BL_LEN_SHIFT 10 |
| 25 | #define MMC_BL_LEN BIT(MMC_BL_LEN_SHIFT) |
Simon Glass | ceeb74f | 2022-10-20 18:22:58 -0600 | [diff] [blame] | 26 | |
| 27 | /* Granularity of priv->csize - this is 1MB */ |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 28 | #define SIZE_MULTIPLE ((1 << (MMC_CMULT + 2)) * MMC_BL_LEN) |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 29 | |
| 30 | struct sandbox_mmc_priv { |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 31 | char *buf; |
| 32 | int csize; /* CSIZE value to report */ |
| 33 | int size; |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 34 | }; |
| 35 | |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 36 | /** |
| 37 | * sandbox_mmc_send_cmd() - Emulate SD commands |
| 38 | * |
| 39 | * This emulate an SD card version 2. Single-block reads result in zero data. |
| 40 | * Multiple-block reads return a test string. |
| 41 | */ |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 42 | static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 43 | struct mmc_data *data) |
| 44 | { |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 45 | struct sandbox_mmc_priv *priv = dev_get_priv(dev); |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 46 | static ulong erase_start, erase_end; |
| 47 | |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 48 | switch (cmd->cmdidx) { |
| 49 | case MMC_CMD_ALL_SEND_CID: |
Simon Glass | b197054 | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 50 | memset(cmd->response, '\0', sizeof(cmd->response)); |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 51 | break; |
| 52 | case SD_CMD_SEND_RELATIVE_ADDR: |
| 53 | cmd->response[0] = 0 << 16; /* mmc->rca */ |
| 54 | case MMC_CMD_GO_IDLE_STATE: |
| 55 | break; |
| 56 | case SD_CMD_SEND_IF_COND: |
| 57 | cmd->response[0] = 0xaa; |
| 58 | break; |
| 59 | case MMC_CMD_SEND_STATUS: |
| 60 | cmd->response[0] = MMC_STATUS_RDY_FOR_DATA; |
| 61 | break; |
| 62 | case MMC_CMD_SELECT_CARD: |
| 63 | break; |
| 64 | case MMC_CMD_SEND_CSD: |
| 65 | cmd->response[0] = 0; |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 66 | cmd->response[1] = (MMC_BL_LEN_SHIFT << 16) | |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 67 | ((priv->csize >> 16) & 0x3f); |
| 68 | cmd->response[2] = (priv->csize & 0xffff) << 16; |
Simon Glass | b197054 | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 69 | cmd->response[3] = 0; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 70 | break; |
| 71 | case SD_CMD_SWITCH_FUNC: { |
Jean-Jacques Hiblot | ef72fd3 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 72 | if (!data) |
| 73 | break; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 74 | u32 *resp = (u32 *)data->dest; |
Simon Glass | b197054 | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 75 | resp[3] = 0; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 76 | resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY); |
Jean-Jacques Hiblot | ef72fd3 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 77 | if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED) |
| 78 | resp[4] = (cmd->cmdarg & 0xF) << 24; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 79 | break; |
| 80 | } |
| 81 | case MMC_CMD_READ_SINGLE_BLOCK: |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 82 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 83 | memcpy(data->dest, &priv->buf[cmd->cmdarg * data->blocksize], |
| 84 | data->blocks * data->blocksize); |
| 85 | break; |
| 86 | case MMC_CMD_WRITE_SINGLE_BLOCK: |
| 87 | case MMC_CMD_WRITE_MULTIPLE_BLOCK: |
| 88 | memcpy(&priv->buf[cmd->cmdarg * data->blocksize], data->src, |
| 89 | data->blocks * data->blocksize); |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 90 | break; |
| 91 | case MMC_CMD_STOP_TRANSMISSION: |
| 92 | break; |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 93 | case SD_CMD_ERASE_WR_BLK_START: |
| 94 | erase_start = cmd->cmdarg; |
| 95 | break; |
| 96 | case SD_CMD_ERASE_WR_BLK_END: |
| 97 | erase_end = cmd->cmdarg; |
| 98 | break; |
Simon Glass | ceeb74f | 2022-10-20 18:22:58 -0600 | [diff] [blame] | 99 | #if CONFIG_IS_ENABLED(MMC_WRITE) |
| 100 | case MMC_CMD_ERASE: { |
| 101 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 102 | |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 103 | memset(&priv->buf[erase_start * mmc->write_bl_len], '\0', |
| 104 | (erase_end - erase_start + 1) * mmc->write_bl_len); |
| 105 | break; |
Simon Glass | ceeb74f | 2022-10-20 18:22:58 -0600 | [diff] [blame] | 106 | } |
| 107 | #endif |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 108 | case SD_CMD_APP_SEND_OP_COND: |
| 109 | cmd->response[0] = OCR_BUSY | OCR_HCS; |
| 110 | cmd->response[1] = 0; |
| 111 | cmd->response[2] = 0; |
| 112 | break; |
| 113 | case MMC_CMD_APP_CMD: |
| 114 | break; |
| 115 | case MMC_CMD_SET_BLOCKLEN: |
| 116 | debug("block len %d\n", cmd->cmdarg); |
| 117 | break; |
| 118 | case SD_CMD_APP_SEND_SCR: { |
| 119 | u32 *scr = (u32 *)data->dest; |
| 120 | |
| 121 | scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */ |
| 122 | break; |
| 123 | } |
| 124 | default: |
| 125 | debug("%s: Unknown command %d\n", __func__, cmd->cmdidx); |
| 126 | break; |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 132 | static int sandbox_mmc_set_ios(struct udevice *dev) |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 133 | { |
| 134 | return 0; |
| 135 | } |
| 136 | |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 137 | static int sandbox_mmc_get_cd(struct udevice *dev) |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 138 | { |
| 139 | return 1; |
| 140 | } |
| 141 | |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 142 | static const struct dm_mmc_ops sandbox_mmc_ops = { |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 143 | .send_cmd = sandbox_mmc_send_cmd, |
| 144 | .set_ios = sandbox_mmc_set_ios, |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 145 | .get_cd = sandbox_mmc_get_cd, |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 146 | }; |
| 147 | |
Simon Glass | 965cd40 | 2021-07-05 16:32:58 -0600 | [diff] [blame] | 148 | static int sandbox_mmc_of_to_plat(struct udevice *dev) |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 149 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 150 | struct sandbox_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | 965cd40 | 2021-07-05 16:32:58 -0600 | [diff] [blame] | 151 | struct mmc_config *cfg = &plat->cfg; |
| 152 | struct blk_desc *blk; |
| 153 | int ret; |
| 154 | |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 155 | plat->fname = dev_read_string(dev, "filename"); |
| 156 | |
Simon Glass | 965cd40 | 2021-07-05 16:32:58 -0600 | [diff] [blame] | 157 | ret = mmc_of_parse(dev, cfg); |
| 158 | if (ret) |
| 159 | return ret; |
| 160 | blk = mmc_get_blk_desc(&plat->mmc); |
| 161 | if (blk) |
| 162 | blk->removable = !(cfg->host_caps & MMC_CAP_NONREMOVABLE); |
| 163 | |
| 164 | return 0; |
| 165 | } |
| 166 | |
| 167 | static int sandbox_mmc_probe(struct udevice *dev) |
| 168 | { |
| 169 | struct sandbox_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 170 | struct sandbox_mmc_priv *priv = dev_get_priv(dev); |
| 171 | int ret; |
| 172 | |
| 173 | if (plat->fname) { |
| 174 | ret = os_map_file(plat->fname, OS_O_RDWR | OS_O_CREAT, |
| 175 | (void **)&priv->buf, &priv->size); |
| 176 | if (ret) { |
| 177 | log_err("%s: Unable to map file '%s'\n", dev->name, |
| 178 | plat->fname); |
| 179 | return ret; |
| 180 | } |
| 181 | priv->csize = priv->size / SIZE_MULTIPLE - 1; |
| 182 | } else { |
| 183 | priv->csize = 0; |
| 184 | priv->size = (priv->csize + 1) * SIZE_MULTIPLE; /* 1 MiB */ |
| 185 | |
| 186 | priv->buf = malloc(priv->size); |
| 187 | if (!priv->buf) { |
| 188 | log_err("%s: Not enough memory (%x bytes)\n", |
| 189 | dev->name, priv->size); |
| 190 | return -ENOMEM; |
| 191 | } |
| 192 | } |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 193 | |
| 194 | return mmc_init(&plat->mmc); |
| 195 | } |
| 196 | |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 197 | static int sandbox_mmc_remove(struct udevice *dev) |
| 198 | { |
| 199 | struct sandbox_mmc_plat *plat = dev_get_plat(dev); |
| 200 | struct sandbox_mmc_priv *priv = dev_get_priv(dev); |
| 201 | |
| 202 | if (plat->fname) |
| 203 | os_unmap(priv->buf, priv->size); |
| 204 | else |
| 205 | free(priv->buf); |
| 206 | |
| 207 | return 0; |
| 208 | } |
| 209 | |
Simon Glass | 965cd40 | 2021-07-05 16:32:58 -0600 | [diff] [blame] | 210 | static int sandbox_mmc_bind(struct udevice *dev) |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 211 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 212 | struct sandbox_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 213 | struct mmc_config *cfg = &plat->cfg; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 214 | |
| 215 | cfg->name = dev->name; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 216 | cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT; |
| 217 | cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; |
| 218 | cfg->f_min = 1000000; |
| 219 | cfg->f_max = 52000000; |
| 220 | cfg->b_max = U32_MAX; |
| 221 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 222 | return mmc_bind(dev, &plat->mmc, cfg); |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 223 | } |
| 224 | |
Simon Glass | 965cd40 | 2021-07-05 16:32:58 -0600 | [diff] [blame] | 225 | static int sandbox_mmc_unbind(struct udevice *dev) |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 226 | { |
| 227 | mmc_unbind(dev); |
| 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 232 | static const struct udevice_id sandbox_mmc_ids[] = { |
| 233 | { .compatible = "sandbox,mmc" }, |
| 234 | { } |
| 235 | }; |
| 236 | |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 237 | U_BOOT_DRIVER(mmc_sandbox) = { |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 238 | .name = "mmc_sandbox", |
| 239 | .id = UCLASS_MMC, |
| 240 | .of_match = sandbox_mmc_ids, |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 241 | .ops = &sandbox_mmc_ops, |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 242 | .bind = sandbox_mmc_bind, |
| 243 | .unbind = sandbox_mmc_unbind, |
Simon Glass | 965cd40 | 2021-07-05 16:32:58 -0600 | [diff] [blame] | 244 | .of_to_plat = sandbox_mmc_of_to_plat, |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 245 | .probe = sandbox_mmc_probe, |
Simon Glass | 32b6c8b | 2021-10-23 17:25:59 -0600 | [diff] [blame] | 246 | .remove = sandbox_mmc_remove, |
Sean Anderson | b2f1c0b | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 247 | .priv_auto = sizeof(struct sandbox_mmc_priv), |
| 248 | .plat_auto = sizeof(struct sandbox_mmc_plat), |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 249 | }; |