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 | |
| 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 | */ |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 26 | 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] | 27 | struct mmc_data *data) |
| 28 | { |
| 29 | switch (cmd->cmdidx) { |
| 30 | case MMC_CMD_ALL_SEND_CID: |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 31 | memset(cmd->response, '\0', sizeof(cmd->response)); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 32 | break; |
| 33 | case SD_CMD_SEND_RELATIVE_ADDR: |
| 34 | cmd->response[0] = 0 << 16; /* mmc->rca */ |
| 35 | case MMC_CMD_GO_IDLE_STATE: |
| 36 | break; |
| 37 | case SD_CMD_SEND_IF_COND: |
| 38 | cmd->response[0] = 0xaa; |
| 39 | break; |
| 40 | case MMC_CMD_SEND_STATUS: |
| 41 | cmd->response[0] = MMC_STATUS_RDY_FOR_DATA; |
| 42 | break; |
| 43 | case MMC_CMD_SELECT_CARD: |
| 44 | break; |
| 45 | case MMC_CMD_SEND_CSD: |
| 46 | cmd->response[0] = 0; |
| 47 | cmd->response[1] = 10 << 16; /* 1 << block_len */ |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 48 | cmd->response[2] = 0; |
| 49 | cmd->response[3] = 0; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 50 | break; |
| 51 | case SD_CMD_SWITCH_FUNC: { |
Jean-Jacques Hiblot | 49f8925 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 52 | if (!data) |
| 53 | break; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 54 | u32 *resp = (u32 *)data->dest; |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame] | 55 | resp[3] = 0; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 56 | resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY); |
Jean-Jacques Hiblot | 49f8925 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 57 | if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED) |
| 58 | resp[4] = (cmd->cmdarg & 0xF) << 24; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 59 | break; |
| 60 | } |
| 61 | case MMC_CMD_READ_SINGLE_BLOCK: |
| 62 | memset(data->dest, '\0', data->blocksize); |
| 63 | break; |
| 64 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
| 65 | strcpy(data->dest, "this is a test"); |
| 66 | break; |
| 67 | case MMC_CMD_STOP_TRANSMISSION: |
| 68 | break; |
| 69 | case SD_CMD_APP_SEND_OP_COND: |
| 70 | cmd->response[0] = OCR_BUSY | OCR_HCS; |
| 71 | cmd->response[1] = 0; |
| 72 | cmd->response[2] = 0; |
| 73 | break; |
| 74 | case MMC_CMD_APP_CMD: |
| 75 | break; |
| 76 | case MMC_CMD_SET_BLOCKLEN: |
| 77 | debug("block len %d\n", cmd->cmdarg); |
| 78 | break; |
| 79 | case SD_CMD_APP_SEND_SCR: { |
| 80 | u32 *scr = (u32 *)data->dest; |
| 81 | |
| 82 | scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */ |
| 83 | break; |
| 84 | } |
| 85 | default: |
| 86 | debug("%s: Unknown command %d\n", __func__, cmd->cmdidx); |
| 87 | break; |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 93 | static int sandbox_mmc_set_ios(struct udevice *dev) |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 94 | { |
| 95 | return 0; |
| 96 | } |
| 97 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 98 | static int sandbox_mmc_get_cd(struct udevice *dev) |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 99 | { |
| 100 | return 1; |
| 101 | } |
| 102 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 103 | static const struct dm_mmc_ops sandbox_mmc_ops = { |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 104 | .send_cmd = sandbox_mmc_send_cmd, |
| 105 | .set_ios = sandbox_mmc_set_ios, |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 106 | .get_cd = sandbox_mmc_get_cd, |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | int sandbox_mmc_probe(struct udevice *dev) |
| 110 | { |
| 111 | struct sandbox_mmc_plat *plat = dev_get_platdata(dev); |
| 112 | |
| 113 | return mmc_init(&plat->mmc); |
| 114 | } |
| 115 | |
| 116 | int sandbox_mmc_bind(struct udevice *dev) |
| 117 | { |
| 118 | struct sandbox_mmc_plat *plat = dev_get_platdata(dev); |
| 119 | struct mmc_config *cfg = &plat->cfg; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 120 | |
| 121 | cfg->name = dev->name; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 122 | cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT; |
| 123 | cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; |
| 124 | cfg->f_min = 1000000; |
| 125 | cfg->f_max = 52000000; |
| 126 | cfg->b_max = U32_MAX; |
| 127 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 128 | return mmc_bind(dev, &plat->mmc, cfg); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | int sandbox_mmc_unbind(struct udevice *dev) |
| 132 | { |
| 133 | mmc_unbind(dev); |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 138 | static const struct udevice_id sandbox_mmc_ids[] = { |
| 139 | { .compatible = "sandbox,mmc" }, |
| 140 | { } |
| 141 | }; |
| 142 | |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 143 | U_BOOT_DRIVER(mmc_sandbox) = { |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 144 | .name = "mmc_sandbox", |
| 145 | .id = UCLASS_MMC, |
| 146 | .of_match = sandbox_mmc_ids, |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 147 | .ops = &sandbox_mmc_ops, |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 148 | .bind = sandbox_mmc_bind, |
| 149 | .unbind = sandbox_mmc_unbind, |
| 150 | .probe = sandbox_mmc_probe, |
| 151 | .platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat), |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 152 | }; |