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