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