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