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 | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 11 | #include <mmc.h> |
| 12 | #include <asm/test.h> |
| 13 | |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 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 | */ |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 25 | 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] | 26 | struct mmc_data *data) |
| 27 | { |
| 28 | switch (cmd->cmdidx) { |
| 29 | case MMC_CMD_ALL_SEND_CID: |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame^] | 30 | memset(cmd->response, '\0', sizeof(cmd->response)); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 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 */ |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame^] | 47 | cmd->response[2] = 0; |
| 48 | cmd->response[3] = 0; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 49 | break; |
| 50 | case SD_CMD_SWITCH_FUNC: { |
Jean-Jacques Hiblot | 49f8925 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 51 | if (!data) |
| 52 | break; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 53 | u32 *resp = (u32 *)data->dest; |
Simon Glass | 5abd58f | 2019-09-25 08:56:00 -0600 | [diff] [blame^] | 54 | resp[3] = 0; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 55 | resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY); |
Jean-Jacques Hiblot | 49f8925 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 56 | if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED) |
| 57 | resp[4] = (cmd->cmdarg & 0xF) << 24; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 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 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 92 | static int sandbox_mmc_set_ios(struct udevice *dev) |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 93 | { |
| 94 | return 0; |
| 95 | } |
| 96 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 97 | static int sandbox_mmc_get_cd(struct udevice *dev) |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 98 | { |
| 99 | return 1; |
| 100 | } |
| 101 | |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 102 | static const struct dm_mmc_ops sandbox_mmc_ops = { |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 103 | .send_cmd = sandbox_mmc_send_cmd, |
| 104 | .set_ios = sandbox_mmc_set_ios, |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 105 | .get_cd = sandbox_mmc_get_cd, |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 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; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 119 | |
| 120 | cfg->name = dev->name; |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 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 | |
Masahiro Yamada | 24f5aec | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 127 | return mmc_bind(dev, &plat->mmc, cfg); |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | int sandbox_mmc_unbind(struct udevice *dev) |
| 131 | { |
| 132 | mmc_unbind(dev); |
| 133 | |
| 134 | return 0; |
| 135 | } |
| 136 | |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 137 | static const struct udevice_id sandbox_mmc_ids[] = { |
| 138 | { .compatible = "sandbox,mmc" }, |
| 139 | { } |
| 140 | }; |
| 141 | |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 142 | U_BOOT_DRIVER(mmc_sandbox) = { |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 143 | .name = "mmc_sandbox", |
| 144 | .id = UCLASS_MMC, |
| 145 | .of_match = sandbox_mmc_ids, |
Simon Glass | 9a46bd3 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 146 | .ops = &sandbox_mmc_ops, |
Simon Glass | f376a3c | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 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), |
Simon Glass | 8e6cc46 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 151 | }; |