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