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