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