Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009 SAMSUNG Electronics |
| 4 | * Minkyu Kang <mk7.kang@samsung.com> |
| 5 | * Jaehoon Chung <jh80.chung@samsung.com> |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 6 | * Portions Copyright 2011-2019 NVIDIA Corporation |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Stephen Warren | f227e45 | 2012-11-06 11:27:30 +0000 | [diff] [blame] | 9 | #include <bouncebuf.h> |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 10 | #include <common.h> |
Simon Glass | 11c89f3 | 2017-05-17 17:18:03 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 9c3b0e4 | 2017-07-25 08:30:08 -0600 | [diff] [blame] | 13 | #include <mmc.h> |
Stephen Warren | fba8754 | 2011-10-31 06:51:36 +0000 | [diff] [blame] | 14 | #include <asm/gpio.h> |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 15 | #include <asm/io.h> |
Tom Warren | ab37196 | 2012-09-19 15:50:56 -0700 | [diff] [blame] | 16 | #include <asm/arch-tegra/tegra_mmc.h> |
Simon Glass | d66c5f7 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <linux/err.h> |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 18 | #if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210) |
| 19 | #include <asm/arch/clock.h> |
| 20 | #endif |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 21 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 22 | struct tegra_mmc_plat { |
| 23 | struct mmc_config cfg; |
| 24 | struct mmc mmc; |
| 25 | }; |
| 26 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 27 | struct tegra_mmc_priv { |
| 28 | struct tegra_mmc *reg; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 29 | struct reset_ctl reset_ctl; |
| 30 | struct clk clk; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 31 | struct gpio_desc cd_gpio; /* Change Detect GPIO */ |
| 32 | struct gpio_desc pwr_gpio; /* Power GPIO */ |
| 33 | struct gpio_desc wp_gpio; /* Write Protect GPIO */ |
| 34 | unsigned int version; /* SDHCI spec. version */ |
| 35 | unsigned int clock; /* Current clock (MHz) */ |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 36 | int mmc_id; /* peripheral id */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 37 | }; |
| 38 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 39 | static void tegra_mmc_set_power(struct tegra_mmc_priv *priv, |
| 40 | unsigned short power) |
Tom Warren | 35ae07b | 2013-02-26 12:31:26 -0700 | [diff] [blame] | 41 | { |
| 42 | u8 pwr = 0; |
| 43 | debug("%s: power = %x\n", __func__, power); |
| 44 | |
| 45 | if (power != (unsigned short)-1) { |
| 46 | switch (1 << power) { |
| 47 | case MMC_VDD_165_195: |
| 48 | pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8; |
| 49 | break; |
| 50 | case MMC_VDD_29_30: |
| 51 | case MMC_VDD_30_31: |
| 52 | pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0; |
| 53 | break; |
| 54 | case MMC_VDD_32_33: |
| 55 | case MMC_VDD_33_34: |
| 56 | pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | debug("%s: pwr = %X\n", __func__, pwr); |
| 61 | |
| 62 | /* Set the bus voltage first (if any) */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 63 | writeb(pwr, &priv->reg->pwrcon); |
Tom Warren | 35ae07b | 2013-02-26 12:31:26 -0700 | [diff] [blame] | 64 | if (pwr == 0) |
| 65 | return; |
| 66 | |
| 67 | /* Now enable bus power */ |
| 68 | pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 69 | writeb(pwr, &priv->reg->pwrcon); |
Tom Warren | 35ae07b | 2013-02-26 12:31:26 -0700 | [diff] [blame] | 70 | } |
| 71 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 72 | static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv, |
| 73 | struct mmc_data *data, |
| 74 | struct bounce_buffer *bbstate) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 75 | { |
| 76 | unsigned char ctrl; |
| 77 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 78 | |
Stephen Warren | f227e45 | 2012-11-06 11:27:30 +0000 | [diff] [blame] | 79 | debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n", |
| 80 | bbstate->bounce_buffer, bbstate->user_buffer, data->blocks, |
| 81 | data->blocksize); |
| 82 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 83 | writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 84 | /* |
| 85 | * DMASEL[4:3] |
| 86 | * 00 = Selects SDMA |
| 87 | * 01 = Reserved |
| 88 | * 10 = Selects 32-bit Address ADMA2 |
| 89 | * 11 = Selects 64-bit Address ADMA2 |
| 90 | */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 91 | ctrl = readb(&priv->reg->hostctl); |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 92 | ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK; |
| 93 | ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 94 | writeb(ctrl, &priv->reg->hostctl); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 95 | |
| 96 | /* We do not handle DMA boundaries, so set it to max (512 KiB) */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 97 | writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize); |
| 98 | writew(data->blocks, &priv->reg->blkcnt); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 101 | static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv, |
| 102 | struct mmc_data *data) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 103 | { |
| 104 | unsigned short mode; |
| 105 | debug(" mmc_set_transfer_mode called\n"); |
| 106 | /* |
| 107 | * TRNMOD |
| 108 | * MUL1SIN0[5] : Multi/Single Block Select |
| 109 | * RD1WT0[4] : Data Transfer Direction Select |
| 110 | * 1 = read |
| 111 | * 0 = write |
| 112 | * ENACMD12[2] : Auto CMD12 Enable |
| 113 | * ENBLKCNT[1] : Block Count Enable |
| 114 | * ENDMA[0] : DMA Enable |
| 115 | */ |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 116 | mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE | |
| 117 | TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE); |
| 118 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 119 | if (data->blocks > 1) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 120 | mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT; |
| 121 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 122 | if (data->flags & MMC_DATA_READ) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 123 | mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 124 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 125 | writew(mode, &priv->reg->trnmod); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 126 | } |
| 127 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 128 | static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv, |
| 129 | struct mmc_cmd *cmd, |
| 130 | struct mmc_data *data, |
| 131 | unsigned int timeout) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 132 | { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 133 | /* |
| 134 | * PRNSTS |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 135 | * CMDINHDAT[1] : Command Inhibit (DAT) |
| 136 | * CMDINHCMD[0] : Command Inhibit (CMD) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 137 | */ |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 138 | unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 139 | |
| 140 | /* |
| 141 | * We shouldn't wait for data inhibit for stop commands, even |
| 142 | * though they might use busy signaling |
| 143 | */ |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 144 | if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY)) |
| 145 | mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 146 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 147 | while (readl(&priv->reg->prnsts) & mask) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 148 | if (timeout == 0) { |
| 149 | printf("%s: timeout error\n", __func__); |
| 150 | return -1; |
| 151 | } |
| 152 | timeout--; |
| 153 | udelay(1000); |
| 154 | } |
| 155 | |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 156 | return 0; |
| 157 | } |
| 158 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 159 | static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd, |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 160 | struct mmc_data *data, |
| 161 | struct bounce_buffer *bbstate) |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 162 | { |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 163 | struct tegra_mmc_priv *priv = dev_get_priv(dev); |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 164 | int flags, i; |
| 165 | int result; |
Anatolij Gustschin | e1f5341 | 2012-03-28 03:40:00 +0000 | [diff] [blame] | 166 | unsigned int mask = 0; |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 167 | unsigned int retry = 0x100000; |
| 168 | debug(" mmc_send_cmd called\n"); |
| 169 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 170 | result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */); |
Anton staaf | 5ab3fba | 2011-11-10 11:56:52 +0000 | [diff] [blame] | 171 | |
| 172 | if (result < 0) |
| 173 | return result; |
| 174 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 175 | if (data) |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 176 | tegra_mmc_prepare_data(priv, data, bbstate); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 177 | |
| 178 | debug("cmd->arg: %08x\n", cmd->cmdarg); |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 179 | writel(cmd->cmdarg, &priv->reg->argument); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 180 | |
| 181 | if (data) |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 182 | tegra_mmc_set_transfer_mode(priv, data); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 183 | |
| 184 | if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) |
| 185 | return -1; |
| 186 | |
| 187 | /* |
| 188 | * CMDREG |
| 189 | * CMDIDX[13:8] : Command index |
| 190 | * DATAPRNT[5] : Data Present Select |
| 191 | * ENCMDIDX[4] : Command Index Check Enable |
| 192 | * ENCMDCRC[3] : Command CRC Check Enable |
| 193 | * RSPTYP[1:0] |
| 194 | * 00 = No Response |
| 195 | * 01 = Length 136 |
| 196 | * 10 = Length 48 |
| 197 | * 11 = Length 48 Check busy after response |
| 198 | */ |
| 199 | if (!(cmd->resp_type & MMC_RSP_PRESENT)) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 200 | flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 201 | else if (cmd->resp_type & MMC_RSP_136) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 202 | flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 203 | else if (cmd->resp_type & MMC_RSP_BUSY) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 204 | flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 205 | else |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 206 | flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 207 | |
| 208 | if (cmd->resp_type & MMC_RSP_CRC) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 209 | flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 210 | if (cmd->resp_type & MMC_RSP_OPCODE) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 211 | flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 212 | if (data) |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 213 | flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 214 | |
| 215 | debug("cmd: %d\n", cmd->cmdidx); |
| 216 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 217 | writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 218 | |
| 219 | for (i = 0; i < retry; i++) { |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 220 | mask = readl(&priv->reg->norintsts); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 221 | /* Command Complete */ |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 222 | if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 223 | if (!data) |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 224 | writel(mask, &priv->reg->norintsts); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 225 | break; |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | if (i == retry) { |
| 230 | printf("%s: waiting for status update\n", __func__); |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 231 | writel(mask, &priv->reg->norintsts); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 232 | return -ETIMEDOUT; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 233 | } |
| 234 | |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 235 | if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 236 | /* Timeout Error */ |
| 237 | debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx); |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 238 | writel(mask, &priv->reg->norintsts); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 239 | return -ETIMEDOUT; |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 240 | } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 241 | /* Error Interrupt */ |
| 242 | debug("error: %08x cmd %d\n", mask, cmd->cmdidx); |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 243 | writel(mask, &priv->reg->norintsts); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 244 | return -1; |
| 245 | } |
| 246 | |
| 247 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 248 | if (cmd->resp_type & MMC_RSP_136) { |
| 249 | /* CRC is stripped so we need to do some shifting. */ |
| 250 | for (i = 0; i < 4; i++) { |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 251 | unsigned long offset = (unsigned long) |
| 252 | (&priv->reg->rspreg3 - i); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 253 | cmd->response[i] = readl(offset) << 8; |
| 254 | |
| 255 | if (i != 3) { |
| 256 | cmd->response[i] |= |
| 257 | readb(offset - 1); |
| 258 | } |
| 259 | debug("cmd->resp[%d]: %08x\n", |
| 260 | i, cmd->response[i]); |
| 261 | } |
| 262 | } else if (cmd->resp_type & MMC_RSP_BUSY) { |
| 263 | for (i = 0; i < retry; i++) { |
| 264 | /* PRNTDATA[23:20] : DAT[3:0] Line Signal */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 265 | if (readl(&priv->reg->prnsts) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 266 | & (1 << 20)) /* DAT[0] */ |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | if (i == retry) { |
| 271 | printf("%s: card is still busy\n", __func__); |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 272 | writel(mask, &priv->reg->norintsts); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 273 | return -ETIMEDOUT; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 276 | cmd->response[0] = readl(&priv->reg->rspreg0); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 277 | debug("cmd->resp[0]: %08x\n", cmd->response[0]); |
| 278 | } else { |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 279 | cmd->response[0] = readl(&priv->reg->rspreg0); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 280 | debug("cmd->resp[0]: %08x\n", cmd->response[0]); |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (data) { |
Anton staaf | bd34842 | 2011-11-10 11:56:51 +0000 | [diff] [blame] | 285 | unsigned long start = get_timer(0); |
| 286 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 287 | while (1) { |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 288 | mask = readl(&priv->reg->norintsts); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 289 | |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 290 | if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 291 | /* Error Interrupt */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 292 | writel(mask, &priv->reg->norintsts); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 293 | printf("%s: error during transfer: 0x%08x\n", |
| 294 | __func__, mask); |
| 295 | return -1; |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 296 | } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) { |
Anton staaf | 3ade210 | 2011-11-10 11:56:50 +0000 | [diff] [blame] | 297 | /* |
| 298 | * DMA Interrupt, restart the transfer where |
| 299 | * it was interrupted. |
| 300 | */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 301 | unsigned int address = readl(&priv->reg->sysad); |
Anton staaf | 3ade210 | 2011-11-10 11:56:50 +0000 | [diff] [blame] | 302 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 303 | debug("DMA end\n"); |
Anton staaf | 3ade210 | 2011-11-10 11:56:50 +0000 | [diff] [blame] | 304 | writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT, |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 305 | &priv->reg->norintsts); |
| 306 | writel(address, &priv->reg->sysad); |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 307 | } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 308 | /* Transfer Complete */ |
| 309 | debug("r/w is done\n"); |
| 310 | break; |
Marcel Ziswiler | e1207e9 | 2014-10-04 01:48:53 +0200 | [diff] [blame] | 311 | } else if (get_timer(start) > 8000UL) { |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 312 | writel(mask, &priv->reg->norintsts); |
Anton staaf | bd34842 | 2011-11-10 11:56:51 +0000 | [diff] [blame] | 313 | printf("%s: MMC Timeout\n" |
| 314 | " Interrupt status 0x%08x\n" |
| 315 | " Interrupt status enable 0x%08x\n" |
| 316 | " Interrupt signal enable 0x%08x\n" |
| 317 | " Present status 0x%08x\n", |
| 318 | __func__, mask, |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 319 | readl(&priv->reg->norintstsen), |
| 320 | readl(&priv->reg->norintsigen), |
| 321 | readl(&priv->reg->prnsts)); |
Anton staaf | bd34842 | 2011-11-10 11:56:51 +0000 | [diff] [blame] | 322 | return -1; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 323 | } |
| 324 | } |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 325 | writel(mask, &priv->reg->norintsts); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | udelay(1000); |
| 329 | return 0; |
| 330 | } |
| 331 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 332 | static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 333 | struct mmc_data *data) |
Stephen Warren | f227e45 | 2012-11-06 11:27:30 +0000 | [diff] [blame] | 334 | { |
| 335 | void *buf; |
| 336 | unsigned int bbflags; |
| 337 | size_t len; |
| 338 | struct bounce_buffer bbstate; |
| 339 | int ret; |
| 340 | |
| 341 | if (data) { |
| 342 | if (data->flags & MMC_DATA_READ) { |
| 343 | buf = data->dest; |
| 344 | bbflags = GEN_BB_WRITE; |
| 345 | } else { |
| 346 | buf = (void *)data->src; |
| 347 | bbflags = GEN_BB_READ; |
| 348 | } |
| 349 | len = data->blocks * data->blocksize; |
| 350 | |
| 351 | bounce_buffer_start(&bbstate, buf, len, bbflags); |
| 352 | } |
| 353 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 354 | ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate); |
Stephen Warren | f227e45 | 2012-11-06 11:27:30 +0000 | [diff] [blame] | 355 | |
| 356 | if (data) |
| 357 | bounce_buffer_stop(&bbstate); |
| 358 | |
| 359 | return ret; |
| 360 | } |
| 361 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 362 | static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 363 | { |
Stephen Warren | f79df4b | 2016-09-13 10:46:01 -0600 | [diff] [blame] | 364 | ulong rate; |
Simon Glass | c2ea5e4 | 2011-09-21 12:40:04 +0000 | [diff] [blame] | 365 | int div; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 366 | unsigned short clk; |
| 367 | unsigned long timeout; |
Simon Glass | c2ea5e4 | 2011-09-21 12:40:04 +0000 | [diff] [blame] | 368 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 369 | debug(" mmc_change_clock called\n"); |
| 370 | |
Simon Glass | c2ea5e4 | 2011-09-21 12:40:04 +0000 | [diff] [blame] | 371 | /* |
Tom Warren | 35ae07b | 2013-02-26 12:31:26 -0700 | [diff] [blame] | 372 | * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0 |
Simon Glass | c2ea5e4 | 2011-09-21 12:40:04 +0000 | [diff] [blame] | 373 | */ |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 374 | if (clock == 0) |
| 375 | goto out; |
Stephen Warren | f79df4b | 2016-09-13 10:46:01 -0600 | [diff] [blame] | 376 | |
| 377 | rate = clk_set_rate(&priv->clk, clock); |
| 378 | div = (rate + clock - 1) / clock; |
Simon Glass | c2ea5e4 | 2011-09-21 12:40:04 +0000 | [diff] [blame] | 379 | debug("div = %d\n", div); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 380 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 381 | writew(0, &priv->reg->clkcon); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 382 | |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 383 | /* |
| 384 | * CLKCON |
| 385 | * SELFREQ[15:8] : base clock divided by value |
| 386 | * ENSDCLK[2] : SD Clock Enable |
| 387 | * STBLINTCLK[1] : Internal Clock Stable |
| 388 | * ENINTCLK[0] : Internal Clock Enable |
| 389 | */ |
Simon Glass | c2ea5e4 | 2011-09-21 12:40:04 +0000 | [diff] [blame] | 390 | div >>= 1; |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 391 | clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) | |
| 392 | TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE); |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 393 | writew(clk, &priv->reg->clkcon); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 394 | |
| 395 | /* Wait max 10 ms */ |
| 396 | timeout = 10; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 397 | while (!(readw(&priv->reg->clkcon) & |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 398 | TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 399 | if (timeout == 0) { |
| 400 | printf("%s: timeout error\n", __func__); |
| 401 | return; |
| 402 | } |
| 403 | timeout--; |
| 404 | udelay(1000); |
| 405 | } |
| 406 | |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 407 | clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 408 | writew(clk, &priv->reg->clkcon); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 409 | |
| 410 | debug("mmc_change_clock: clkcon = %08X\n", clk); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 411 | |
| 412 | out: |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 413 | priv->clock = clock; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 416 | static int tegra_mmc_set_ios(struct udevice *dev) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 417 | { |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 418 | struct tegra_mmc_priv *priv = dev_get_priv(dev); |
| 419 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 420 | unsigned char ctrl; |
| 421 | debug(" mmc_set_ios called\n"); |
| 422 | |
| 423 | debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock); |
| 424 | |
| 425 | /* Change clock first */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 426 | tegra_mmc_change_clock(priv, mmc->clock); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 427 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 428 | ctrl = readb(&priv->reg->hostctl); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * WIDE8[5] |
| 432 | * 0 = Depend on WIDE4 |
| 433 | * 1 = 8-bit mode |
| 434 | * WIDE4[1] |
| 435 | * 1 = 4-bit mode |
| 436 | * 0 = 1-bit mode |
| 437 | */ |
| 438 | if (mmc->bus_width == 8) |
| 439 | ctrl |= (1 << 5); |
| 440 | else if (mmc->bus_width == 4) |
| 441 | ctrl |= (1 << 1); |
| 442 | else |
Simon Glass | 9d6551a | 2017-06-07 21:11:48 -0600 | [diff] [blame] | 443 | ctrl &= ~(1 << 1 | 1 << 5); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 444 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 445 | writeb(ctrl, &priv->reg->hostctl); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 446 | debug("mmc_set_ios: hostctl = %08X\n", ctrl); |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 447 | |
| 448 | return 0; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 451 | static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv) |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 452 | { |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 453 | #if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210) |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 454 | u32 val; |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 455 | u16 clk_con; |
| 456 | int timeout; |
| 457 | int id = priv->mmc_id; |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 458 | |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 459 | debug("%s: sdmmc address = %p, id = %d\n", __func__, |
| 460 | priv->reg, id); |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 461 | |
| 462 | /* Set the pad drive strength for SDMMC1 or 3 only */ |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 463 | if (id != PERIPH_ID_SDMMC1 && id != PERIPH_ID_SDMMC3) { |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 464 | debug("%s: settings are only valid for SDMMC1/SDMMC3!\n", |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 465 | __func__); |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 466 | return; |
| 467 | } |
| 468 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 469 | val = readl(&priv->reg->sdmemcmppadctl); |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 470 | val &= 0xFFFFFFF0; |
| 471 | val |= MEMCOMP_PADCTRL_VREF; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 472 | writel(val, &priv->reg->sdmemcmppadctl); |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 473 | |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 474 | /* Disable SD Clock Enable before running auto-cal as per TRM */ |
| 475 | clk_con = readw(&priv->reg->clkcon); |
| 476 | debug("%s: CLOCK_CONTROL = 0x%04X\n", __func__, clk_con); |
| 477 | clk_con &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; |
| 478 | writew(clk_con, &priv->reg->clkcon); |
| 479 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 480 | val = readl(&priv->reg->autocalcfg); |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 481 | val &= 0xFFFF0000; |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 482 | val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 483 | writel(val, &priv->reg->autocalcfg); |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 484 | val |= AUTO_CAL_START | AUTO_CAL_ENABLE; |
| 485 | writel(val, &priv->reg->autocalcfg); |
| 486 | debug("%s: AUTO_CAL_CFG = 0x%08X\n", __func__, val); |
| 487 | udelay(1); |
| 488 | timeout = 100; /* 10 mSec max (100*100uS) */ |
| 489 | do { |
| 490 | val = readl(&priv->reg->autocalsts); |
| 491 | udelay(100); |
| 492 | } while ((val & AUTO_CAL_ACTIVE) && --timeout); |
| 493 | val = readl(&priv->reg->autocalsts); |
| 494 | debug("%s: Final AUTO_CAL_STATUS = 0x%08X, timeout = %d\n", |
| 495 | __func__, val, timeout); |
| 496 | |
| 497 | /* Re-enable SD Clock Enable when auto-cal is done */ |
| 498 | clk_con |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE; |
| 499 | writew(clk_con, &priv->reg->clkcon); |
| 500 | clk_con = readw(&priv->reg->clkcon); |
| 501 | debug("%s: final CLOCK_CONTROL = 0x%04X\n", __func__, clk_con); |
| 502 | |
| 503 | if (timeout == 0) { |
| 504 | printf("%s: Warning: Autocal timed out!\n", __func__); |
| 505 | /* TBD: Set CFG2TMC_SDMMC1_PAD_CAL_DRV* regs here */ |
| 506 | } |
| 507 | |
| 508 | #if defined(CONFIG_TEGRA210) |
| 509 | u32 tap_value, trim_value; |
| 510 | |
| 511 | /* Set tap/trim values for SDMMC1/3 @ <48MHz here */ |
| 512 | val = readl(&priv->reg->venspictl); /* aka VENDOR_SYS_SW_CNTL */ |
| 513 | val &= IO_TRIM_BYPASS_MASK; |
| 514 | if (id == PERIPH_ID_SDMMC1) { |
| 515 | tap_value = 4; /* default */ |
| 516 | if (val) |
| 517 | tap_value = 3; |
| 518 | trim_value = 2; |
| 519 | } else { /* SDMMC3 */ |
| 520 | tap_value = 3; |
| 521 | trim_value = 3; |
| 522 | } |
| 523 | |
| 524 | val = readl(&priv->reg->venclkctl); |
| 525 | val &= ~TRIM_VAL_MASK; |
| 526 | val |= (trim_value << TRIM_VAL_SHIFT); |
| 527 | val &= ~TAP_VAL_MASK; |
| 528 | val |= (tap_value << TAP_VAL_SHIFT); |
| 529 | writel(val, &priv->reg->venclkctl); |
| 530 | debug("%s: VENDOR_CLOCK_CNTRL = 0x%08X\n", __func__, val); |
| 531 | #endif /* T210 */ |
| 532 | #endif /* T30/T210 */ |
Stephen Warren | c76e936 | 2016-09-13 10:45:44 -0600 | [diff] [blame] | 533 | } |
| 534 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 535 | static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 536 | { |
| 537 | unsigned int timeout; |
| 538 | debug(" mmc_reset called\n"); |
| 539 | |
| 540 | /* |
| 541 | * RSTALL[0] : Software reset for all |
| 542 | * 1 = reset |
| 543 | * 0 = work |
| 544 | */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 545 | writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 546 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 547 | priv->clock = 0; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 548 | |
| 549 | /* Wait max 100 ms */ |
| 550 | timeout = 100; |
| 551 | |
| 552 | /* hw clears the bit when it's done */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 553 | while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) { |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 554 | if (timeout == 0) { |
| 555 | printf("%s: timeout error\n", __func__); |
| 556 | return; |
| 557 | } |
| 558 | timeout--; |
| 559 | udelay(1000); |
| 560 | } |
Tom Warren | 35ae07b | 2013-02-26 12:31:26 -0700 | [diff] [blame] | 561 | |
| 562 | /* Set SD bus voltage & enable bus power */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 563 | tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1); |
Tom Warren | 35ae07b | 2013-02-26 12:31:26 -0700 | [diff] [blame] | 564 | debug("%s: power control = %02X, host control = %02X\n", __func__, |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 565 | readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl)); |
Tom Warren | 35ae07b | 2013-02-26 12:31:26 -0700 | [diff] [blame] | 566 | |
| 567 | /* Make sure SDIO pads are set up */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 568 | tegra_mmc_pad_init(priv); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 569 | } |
| 570 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 571 | static int tegra_mmc_init(struct udevice *dev) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 572 | { |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 573 | struct tegra_mmc_priv *priv = dev_get_priv(dev); |
| 574 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 575 | unsigned int mask; |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 576 | debug(" tegra_mmc_init called\n"); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 577 | |
Tom Warren | 2e86e81 | 2019-05-29 09:30:01 -0700 | [diff] [blame^] | 578 | #if defined(CONFIG_TEGRA210) |
| 579 | priv->mmc_id = clock_decode_periph_id(dev); |
| 580 | if (priv->mmc_id == PERIPH_ID_NONE) { |
| 581 | printf("%s: Missing/invalid peripheral ID\n", __func__); |
| 582 | return -EINVAL; |
| 583 | } |
| 584 | #endif |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 585 | tegra_mmc_reset(priv, mmc); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 586 | |
Marcel Ziswiler | 8670885 | 2017-03-25 01:18:22 +0100 | [diff] [blame] | 587 | #if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK) |
| 588 | /* |
| 589 | * Disable the external clock loopback and use the internal one on |
| 590 | * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1 |
| 591 | * bits being set to 0xfffd according to the TRM. |
| 592 | * |
| 593 | * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled |
| 594 | * approach once proper kernel integration made it mainline. |
| 595 | */ |
| 596 | if (priv->reg == (void *)0x700b0400) { |
| 597 | mask = readl(&priv->reg->venmiscctl); |
| 598 | mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK; |
| 599 | writel(mask, &priv->reg->venmiscctl); |
| 600 | } |
| 601 | #endif |
| 602 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 603 | priv->version = readw(&priv->reg->hcver); |
| 604 | debug("host version = %x\n", priv->version); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 605 | |
| 606 | /* mask all */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 607 | writel(0xffffffff, &priv->reg->norintstsen); |
| 608 | writel(0xffffffff, &priv->reg->norintsigen); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 609 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 610 | writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */ |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 611 | /* |
| 612 | * NORMAL Interrupt Status Enable Register init |
| 613 | * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable |
| 614 | * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable |
Anton staaf | 3ade210 | 2011-11-10 11:56:50 +0000 | [diff] [blame] | 615 | * [3] ENSTADMAINT : DMA boundary interrupt |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 616 | * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable |
| 617 | * [0] ENSTACMDCMPLT : Command Complete Status Enable |
| 618 | */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 619 | mask = readl(&priv->reg->norintstsen); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 620 | mask &= ~(0xffff); |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 621 | mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE | |
| 622 | TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE | |
Anton staaf | 3ade210 | 2011-11-10 11:56:50 +0000 | [diff] [blame] | 623 | TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT | |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 624 | TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY | |
| 625 | TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY); |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 626 | writel(mask, &priv->reg->norintstsen); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 627 | |
| 628 | /* |
| 629 | * NORMAL Interrupt Signal Enable Register init |
| 630 | * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable |
| 631 | */ |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 632 | mask = readl(&priv->reg->norintsigen); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 633 | mask &= ~(0xffff); |
Anton staaf | 0dfb31c | 2011-11-10 11:56:49 +0000 | [diff] [blame] | 634 | mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE; |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 635 | writel(mask, &priv->reg->norintsigen); |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 636 | |
| 637 | return 0; |
| 638 | } |
| 639 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 640 | static int tegra_mmc_getcd(struct udevice *dev) |
Thierry Reding | f149411 | 2012-01-02 01:15:39 +0000 | [diff] [blame] | 641 | { |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 642 | struct tegra_mmc_priv *priv = dev_get_priv(dev); |
Thierry Reding | f149411 | 2012-01-02 01:15:39 +0000 | [diff] [blame] | 643 | |
Tom Warren | 22562a4 | 2012-09-04 17:00:24 -0700 | [diff] [blame] | 644 | debug("tegra_mmc_getcd called\n"); |
Thierry Reding | f149411 | 2012-01-02 01:15:39 +0000 | [diff] [blame] | 645 | |
Stephen Warren | 6d9ccbb | 2016-09-13 10:45:46 -0600 | [diff] [blame] | 646 | if (dm_gpio_is_valid(&priv->cd_gpio)) |
| 647 | return dm_gpio_get_value(&priv->cd_gpio); |
Thierry Reding | f149411 | 2012-01-02 01:15:39 +0000 | [diff] [blame] | 648 | |
| 649 | return 1; |
| 650 | } |
| 651 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 652 | static const struct dm_mmc_ops tegra_mmc_ops = { |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 653 | .send_cmd = tegra_mmc_send_cmd, |
| 654 | .set_ios = tegra_mmc_set_ios, |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 655 | .get_cd = tegra_mmc_getcd, |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 656 | }; |
| 657 | |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 658 | static int tegra_mmc_probe(struct udevice *dev) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 659 | { |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 660 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 661 | struct tegra_mmc_plat *plat = dev_get_platdata(dev); |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 662 | struct tegra_mmc_priv *priv = dev_get_priv(dev); |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 663 | struct mmc_config *cfg = &plat->cfg; |
Stephen Warren | f79df4b | 2016-09-13 10:46:01 -0600 | [diff] [blame] | 664 | int bus_width, ret; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 665 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 666 | cfg->name = dev->name; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 667 | |
Simon Glass | 9c3b0e4 | 2017-07-25 08:30:08 -0600 | [diff] [blame] | 668 | bus_width = dev_read_u32_default(dev, "bus-width", 1); |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 669 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 670 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; |
| 671 | cfg->host_caps = 0; |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 672 | if (bus_width == 8) |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 673 | cfg->host_caps |= MMC_MODE_8BIT; |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 674 | if (bus_width >= 4) |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 675 | cfg->host_caps |= MMC_MODE_4BIT; |
| 676 | cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 677 | |
| 678 | /* |
| 679 | * min freq is for card identification, and is the highest |
| 680 | * low-speed SDIO card frequency (actually 400KHz) |
| 681 | * max freq is highest HS eMMC clock as per the SD/MMC spec |
| 682 | * (actually 52MHz) |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 683 | */ |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 684 | cfg->f_min = 375000; |
| 685 | cfg->f_max = 48000000; |
Tom Warren | 85f0ee4 | 2011-05-31 10:30:37 +0000 | [diff] [blame] | 686 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 687 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 688 | |
Simon Glass | 9c3b0e4 | 2017-07-25 08:30:08 -0600 | [diff] [blame] | 689 | priv->reg = (void *)dev_read_addr(dev); |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 690 | |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 691 | ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl); |
| 692 | if (ret) { |
| 693 | debug("reset_get_by_name() failed: %d\n", ret); |
| 694 | return ret; |
| 695 | } |
| 696 | ret = clk_get_by_index(dev, 0, &priv->clk); |
| 697 | if (ret) { |
| 698 | debug("clk_get_by_index() failed: %d\n", ret); |
| 699 | return ret; |
Stephen Warren | d26e24d | 2016-08-05 16:10:33 -0600 | [diff] [blame] | 700 | } |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 701 | |
| 702 | ret = reset_assert(&priv->reset_ctl); |
| 703 | if (ret) |
| 704 | return ret; |
| 705 | ret = clk_enable(&priv->clk); |
| 706 | if (ret) |
| 707 | return ret; |
| 708 | ret = clk_set_rate(&priv->clk, 20000000); |
| 709 | if (IS_ERR_VALUE(ret)) |
| 710 | return ret; |
| 711 | ret = reset_deassert(&priv->reset_ctl); |
| 712 | if (ret) |
| 713 | return ret; |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 714 | |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 715 | /* These GPIOs are optional */ |
Simon Glass | 9c3b0e4 | 2017-07-25 08:30:08 -0600 | [diff] [blame] | 716 | gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN); |
| 717 | gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN); |
| 718 | gpio_request_by_name(dev, "power-gpios", 0, &priv->pwr_gpio, |
| 719 | GPIOD_IS_OUT); |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 720 | if (dm_gpio_is_valid(&priv->pwr_gpio)) |
| 721 | dm_gpio_set_value(&priv->pwr_gpio, 1); |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 722 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 723 | upriv->mmc = &plat->mmc; |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 724 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 725 | return tegra_mmc_init(dev); |
| 726 | } |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 727 | |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 728 | static int tegra_mmc_bind(struct udevice *dev) |
| 729 | { |
| 730 | struct tegra_mmc_plat *plat = dev_get_platdata(dev); |
| 731 | |
| 732 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 733 | } |
| 734 | |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 735 | static const struct udevice_id tegra_mmc_ids[] = { |
| 736 | { .compatible = "nvidia,tegra20-sdhci" }, |
| 737 | { .compatible = "nvidia,tegra30-sdhci" }, |
| 738 | { .compatible = "nvidia,tegra114-sdhci" }, |
| 739 | { .compatible = "nvidia,tegra124-sdhci" }, |
| 740 | { .compatible = "nvidia,tegra210-sdhci" }, |
| 741 | { .compatible = "nvidia,tegra186-sdhci" }, |
| 742 | { } |
| 743 | }; |
Tom Warren | 9745cf8 | 2013-02-21 12:31:30 +0000 | [diff] [blame] | 744 | |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 745 | U_BOOT_DRIVER(tegra_mmc_drv) = { |
| 746 | .name = "tegra_mmc", |
| 747 | .id = UCLASS_MMC, |
| 748 | .of_match = tegra_mmc_ids, |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 749 | .bind = tegra_mmc_bind, |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 750 | .probe = tegra_mmc_probe, |
Simon Glass | 8c4c5c8 | 2017-04-23 20:02:11 -0600 | [diff] [blame] | 751 | .ops = &tegra_mmc_ops, |
| 752 | .platdata_auto_alloc_size = sizeof(struct tegra_mmc_plat), |
Tom Warren | a66f772 | 2016-09-13 10:45:48 -0600 | [diff] [blame] | 753 | .priv_auto_alloc_size = sizeof(struct tegra_mmc_priv), |
| 754 | }; |