Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Google, Inc |
| 3 | * Written by Simon Glass <sjg@chromium.org> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <dm.h> |
| 10 | #include <errno.h> |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 11 | #include <fdtdec.h> |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 12 | #include <mmc.h> |
| 13 | #include <asm/test.h> |
| 14 | |
Simon Glass | f9eebac | 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 | efcc669 | 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 | f9eebac | 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: |
| 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 */ |
| 47 | break; |
| 48 | case SD_CMD_SWITCH_FUNC: { |
Jean-Jacques Hiblot | ef72fd3 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 49 | if (!data) |
| 50 | break; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 51 | u32 *resp = (u32 *)data->dest; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 52 | resp[7] = cpu_to_be32(SD_HIGHSPEED_BUSY); |
Jean-Jacques Hiblot | ef72fd3 | 2017-12-14 11:47:14 +0100 | [diff] [blame] | 53 | if ((cmd->cmdarg & 0xF) == UHS_SDR12_BUS_SPEED) |
| 54 | resp[4] = (cmd->cmdarg & 0xF) << 24; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 55 | break; |
| 56 | } |
| 57 | case MMC_CMD_READ_SINGLE_BLOCK: |
| 58 | memset(data->dest, '\0', data->blocksize); |
| 59 | break; |
| 60 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
| 61 | strcpy(data->dest, "this is a test"); |
| 62 | break; |
| 63 | case MMC_CMD_STOP_TRANSMISSION: |
| 64 | break; |
| 65 | case SD_CMD_APP_SEND_OP_COND: |
| 66 | cmd->response[0] = OCR_BUSY | OCR_HCS; |
| 67 | cmd->response[1] = 0; |
| 68 | cmd->response[2] = 0; |
| 69 | break; |
| 70 | case MMC_CMD_APP_CMD: |
| 71 | break; |
| 72 | case MMC_CMD_SET_BLOCKLEN: |
| 73 | debug("block len %d\n", cmd->cmdarg); |
| 74 | break; |
| 75 | case SD_CMD_APP_SEND_SCR: { |
| 76 | u32 *scr = (u32 *)data->dest; |
| 77 | |
| 78 | scr[0] = cpu_to_be32(2 << 24 | 1 << 15); /* SD version 3 */ |
| 79 | break; |
| 80 | } |
| 81 | default: |
| 82 | debug("%s: Unknown command %d\n", __func__, cmd->cmdidx); |
| 83 | break; |
| 84 | } |
| 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 89 | static int sandbox_mmc_set_ios(struct udevice *dev) |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 90 | { |
| 91 | return 0; |
| 92 | } |
| 93 | |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 94 | static int sandbox_mmc_get_cd(struct udevice *dev) |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 95 | { |
| 96 | return 1; |
| 97 | } |
| 98 | |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 99 | static const struct dm_mmc_ops sandbox_mmc_ops = { |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 100 | .send_cmd = sandbox_mmc_send_cmd, |
| 101 | .set_ios = sandbox_mmc_set_ios, |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 102 | .get_cd = sandbox_mmc_get_cd, |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | int sandbox_mmc_probe(struct udevice *dev) |
| 106 | { |
| 107 | struct sandbox_mmc_plat *plat = dev_get_platdata(dev); |
| 108 | |
| 109 | return mmc_init(&plat->mmc); |
| 110 | } |
| 111 | |
| 112 | int sandbox_mmc_bind(struct udevice *dev) |
| 113 | { |
| 114 | struct sandbox_mmc_plat *plat = dev_get_platdata(dev); |
| 115 | struct mmc_config *cfg = &plat->cfg; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 116 | |
| 117 | cfg->name = dev->name; |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 118 | cfg->host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_8BIT; |
| 119 | cfg->voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; |
| 120 | cfg->f_min = 1000000; |
| 121 | cfg->f_max = 52000000; |
| 122 | cfg->b_max = U32_MAX; |
| 123 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 124 | return mmc_bind(dev, &plat->mmc, cfg); |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | int sandbox_mmc_unbind(struct udevice *dev) |
| 128 | { |
| 129 | mmc_unbind(dev); |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 134 | static const struct udevice_id sandbox_mmc_ids[] = { |
| 135 | { .compatible = "sandbox,mmc" }, |
| 136 | { } |
| 137 | }; |
| 138 | |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 139 | U_BOOT_DRIVER(mmc_sandbox) = { |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 140 | .name = "mmc_sandbox", |
| 141 | .id = UCLASS_MMC, |
| 142 | .of_match = sandbox_mmc_ids, |
Simon Glass | efcc669 | 2016-06-12 23:30:26 -0600 | [diff] [blame] | 143 | .ops = &sandbox_mmc_ops, |
Simon Glass | f9eebac | 2016-05-01 13:52:42 -0600 | [diff] [blame] | 144 | .bind = sandbox_mmc_bind, |
| 145 | .unbind = sandbox_mmc_unbind, |
| 146 | .probe = sandbox_mmc_probe, |
| 147 | .platdata_auto_alloc_size = sizeof(struct sandbox_mmc_plat), |
Simon Glass | d3e58e4 | 2015-07-06 12:54:32 -0600 | [diff] [blame] | 148 | }; |