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