Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Simon Glass | 8e6cc46 | 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 | 8e6cc46 | 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 | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 10 | #include <fdtdec.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 12 | #include <mmc.h> |
| 13 | #include <asm/test.h> |
| 14 | |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 15 | struct sandbox_mmc_plat { |
| 16 | struct mmc_config cfg; |
| 17 | struct mmc mmc; |
| 18 | }; |
| 19 | |
Sean Anderson | 3f6fb77 | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 20 | #define MMC_CSIZE 0 |
| 21 | #define MMC_CMULT 8 /* 8 because the card is high-capacity */ |
| 22 | #define MMC_BL_LEN_SHIFT 10 |
| 23 | #define MMC_BL_LEN BIT(MMC_BL_LEN_SHIFT) |
| 24 | #define MMC_CAPACITY (((MMC_CSIZE + 1) << (MMC_CMULT + 2)) \ |
| 25 | * MMC_BL_LEN) /* 1 MiB */ |
| 26 | |
| 27 | struct sandbox_mmc_priv { |
| 28 | u8 buf[MMC_CAPACITY]; |
| 29 | }; |
| 30 | |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 31 | /** |
| 32 | * sandbox_mmc_send_cmd() - Emulate SD commands |
| 33 | * |
| 34 | * This emulate an SD card version 2. Single-block reads result in zero data. |
| 35 | * Multiple-block reads return a test string. |
| 36 | */ |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 37 | static int sandbox_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 38 | struct mmc_data *data) |
| 39 | { |
Sean Anderson | 3f6fb77 | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 40 | struct sandbox_mmc_priv *priv = dev_get_priv(dev); |
| 41 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 42 | static ulong erase_start, erase_end; |
| 43 | |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 44 | switch (cmd->cmdidx) { |
| 45 | case MMC_CMD_ALL_SEND_CID: |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 46 | memset(cmd->response, '\0', sizeof(cmd->response)); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 47 | break; |
| 48 | case SD_CMD_SEND_RELATIVE_ADDR: |
| 49 | cmd->response[0] = 0 << 16; /* mmc->rca */ |
| 50 | case MMC_CMD_GO_IDLE_STATE: |
| 51 | break; |
| 52 | case SD_CMD_SEND_IF_COND: |
| 53 | cmd->response[0] = 0xaa; |
| 54 | break; |
| 55 | case MMC_CMD_SEND_STATUS: |
| 56 | cmd->response[0] = MMC_STATUS_RDY_FOR_DATA; |
| 57 | break; |
| 58 | case MMC_CMD_SELECT_CARD: |
| 59 | break; |
| 60 | case MMC_CMD_SEND_CSD: |
| 61 | cmd->response[0] = 0; |
Sean Anderson | 3f6fb77 | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 62 | cmd->response[1] = (MMC_BL_LEN_SHIFT << 16) | |
| 63 | ((MMC_CSIZE >> 16) & 0x3f); |
| 64 | cmd->response[2] = (MMC_CSIZE & 0xffff) << 16; |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 65 | cmd->response[3] = 0; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 66 | break; |
| 67 | case SD_CMD_SWITCH_FUNC: { |
Jean-Jacques Hiblot | 49f8925 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 68 | if (!data) |
| 69 | break; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 70 | u32 *resp = (u32 *)data->dest; |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 71 | resp[3] = 0; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 72 | resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY); |
Jean-Jacques Hiblot | 49f8925 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 73 | if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED) |
| 74 | resp[4] = (cmd->cmdarg & 0xF) << 24; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 75 | break; |
| 76 | } |
| 77 | case MMC_CMD_READ_SINGLE_BLOCK: |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 78 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
Sean Anderson | 3f6fb77 | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 79 | memcpy(data->dest, &priv->buf[cmd->cmdarg * data->blocksize], |
| 80 | data->blocks * data->blocksize); |
| 81 | break; |
| 82 | case MMC_CMD_WRITE_SINGLE_BLOCK: |
| 83 | case MMC_CMD_WRITE_MULTIPLE_BLOCK: |
| 84 | memcpy(&priv->buf[cmd->cmdarg * data->blocksize], data->src, |
| 85 | data->blocks * data->blocksize); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 86 | break; |
| 87 | case MMC_CMD_STOP_TRANSMISSION: |
| 88 | break; |
Sean Anderson | 3f6fb77 | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 89 | case SD_CMD_ERASE_WR_BLK_START: |
| 90 | erase_start = cmd->cmdarg; |
| 91 | break; |
| 92 | case SD_CMD_ERASE_WR_BLK_END: |
| 93 | erase_end = cmd->cmdarg; |
| 94 | break; |
| 95 | case MMC_CMD_ERASE: |
| 96 | memset(&priv->buf[erase_start * mmc->write_bl_len], '\0', |
| 97 | (erase_end - erase_start + 1) * mmc->write_bl_len); |
| 98 | break; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 99 | case SD_CMD_APP_SEND_OP_COND: |
| 100 | cmd->response[0] = OCR_BUSY | OCR_HCS; |
| 101 | cmd->response[1] = 0; |
| 102 | cmd->response[2] = 0; |
| 103 | break; |
| 104 | case MMC_CMD_APP_CMD: |
| 105 | break; |
| 106 | case MMC_CMD_SET_BLOCKLEN: |
| 107 | debug("block len %d\n", cmd->cmdarg); |
| 108 | break; |
| 109 | case SD_CMD_APP_SEND_SCR: { |
| 110 | u32 *scr = (u32 *)data->dest; |
| 111 | |
| 112 | scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */ |
| 113 | break; |
| 114 | } |
| 115 | default: |
| 116 | debug("%s: Unknown command %d\n", __func__, cmd->cmdidx); |
| 117 | break; |
| 118 | } |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 123 | static int sandbox_mmc_set_ios(struct udevice *dev) |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 124 | { |
| 125 | return 0; |
| 126 | } |
| 127 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 128 | static int sandbox_mmc_get_cd(struct udevice *dev) |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 129 | { |
| 130 | return 1; |
| 131 | } |
| 132 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 133 | static const struct dm_mmc_ops sandbox_mmc_ops = { |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 134 | .send_cmd = sandbox_mmc_send_cmd, |
| 135 | .set_ios = sandbox_mmc_set_ios, |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 136 | .get_cd = sandbox_mmc_get_cd, |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | int sandbox_mmc_probe(struct udevice *dev) |
| 140 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 141 | struct sandbox_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 142 | |
| 143 | return mmc_init(&plat->mmc); |
| 144 | } |
| 145 | |
| 146 | int sandbox_mmc_bind(struct udevice *dev) |
| 147 | { |
Simon Glass | c69cda2 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 148 | struct sandbox_mmc_plat *plat = dev_get_plat(dev); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 149 | struct mmc_config *cfg = &plat->cfg; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 150 | |
| 151 | cfg->name = dev->name; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 152 | cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT; |
| 153 | cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; |
| 154 | cfg->f_min = 1000000; |
| 155 | cfg->f_max = 52000000; |
| 156 | cfg->b_max = U32_MAX; |
| 157 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 158 | return mmc_bind(dev, &plat->mmc, cfg); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | int sandbox_mmc_unbind(struct udevice *dev) |
| 162 | { |
| 163 | mmc_unbind(dev); |
| 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 168 | static const struct udevice_id sandbox_mmc_ids[] = { |
| 169 | { .compatible = "sandbox,mmc" }, |
| 170 | { } |
| 171 | }; |
| 172 | |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 173 | U_BOOT_DRIVER(mmc_sandbox) = { |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 174 | .name = "mmc_sandbox", |
| 175 | .id = UCLASS_MMC, |
| 176 | .of_match = sandbox_mmc_ids, |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 177 | .ops = &sandbox_mmc_ops, |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 178 | .bind = sandbox_mmc_bind, |
| 179 | .unbind = sandbox_mmc_unbind, |
| 180 | .probe = sandbox_mmc_probe, |
Sean Anderson | 3f6fb77 | 2021-02-05 09:38:53 -0500 | [diff] [blame] | 181 | .priv_auto = sizeof(struct sandbox_mmc_priv), |
| 182 | .plat_auto = sizeof(struct sandbox_mmc_plat), |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 183 | }; |