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