blob: 97b115459546ec8606895df4ef9da104f7d82366 [file] [log] [blame]
Tom Warren21ef6a12011-05-31 10:30:37 +00001/*
2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Jaehoon Chung <jh80.chung@samsung.com>
Tom Warren6a474db2016-09-13 10:45:48 -06005 * Portions Copyright 2011-2016 NVIDIA Corporation
Tom Warren21ef6a12011-05-31 10:30:37 +00006 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Tom Warren21ef6a12011-05-31 10:30:37 +00008 */
9
Stephen Warren19815392012-11-06 11:27:30 +000010#include <bouncebuf.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000011#include <common.h>
Stephen Warrenc0493072016-08-05 16:10:33 -060012#include <dm/device.h>
Jaehoon Chung915ffa52016-07-19 16:33:36 +090013#include <errno.h>
Stephen Warren98778412011-10-31 06:51:36 +000014#include <asm/gpio.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000015#include <asm/io.h>
Tom Warren150c2492012-09-19 15:50:56 -070016#include <asm/arch-tegra/tegra_mmc.h>
17#include <mmc.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000018
Tom Warrenc9aa8312013-02-21 12:31:30 +000019DECLARE_GLOBAL_DATA_PTR;
Tom Warren21ef6a12011-05-31 10:30:37 +000020
Stephen Warrenf53c4e42016-09-13 10:45:46 -060021struct tegra_mmc_priv {
22 struct tegra_mmc *reg;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060023 struct reset_ctl reset_ctl;
24 struct clk clk;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060025 struct gpio_desc cd_gpio; /* Change Detect GPIO */
26 struct gpio_desc pwr_gpio; /* Power GPIO */
27 struct gpio_desc wp_gpio; /* Write Protect GPIO */
28 unsigned int version; /* SDHCI spec. version */
29 unsigned int clock; /* Current clock (MHz) */
30 struct mmc_config cfg; /* mmc configuration */
Tom Warren6a474db2016-09-13 10:45:48 -060031 struct mmc *mmc;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060032};
33
Stephen Warrenf53c4e42016-09-13 10:45:46 -060034static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
35 unsigned short power)
Tom Warren2d348a12013-02-26 12:31:26 -070036{
37 u8 pwr = 0;
38 debug("%s: power = %x\n", __func__, power);
39
40 if (power != (unsigned short)-1) {
41 switch (1 << power) {
42 case MMC_VDD_165_195:
43 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
44 break;
45 case MMC_VDD_29_30:
46 case MMC_VDD_30_31:
47 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
48 break;
49 case MMC_VDD_32_33:
50 case MMC_VDD_33_34:
51 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
52 break;
53 }
54 }
55 debug("%s: pwr = %X\n", __func__, pwr);
56
57 /* Set the bus voltage first (if any) */
Stephen Warrenf53c4e42016-09-13 10:45:46 -060058 writeb(pwr, &priv->reg->pwrcon);
Tom Warren2d348a12013-02-26 12:31:26 -070059 if (pwr == 0)
60 return;
61
62 /* Now enable bus power */
63 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060064 writeb(pwr, &priv->reg->pwrcon);
Tom Warren2d348a12013-02-26 12:31:26 -070065}
66
Stephen Warrenf53c4e42016-09-13 10:45:46 -060067static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
68 struct mmc_data *data,
69 struct bounce_buffer *bbstate)
Tom Warren21ef6a12011-05-31 10:30:37 +000070{
71 unsigned char ctrl;
72
Tom Warren21ef6a12011-05-31 10:30:37 +000073
Stephen Warren19815392012-11-06 11:27:30 +000074 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
75 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
76 data->blocksize);
77
Stephen Warrenf53c4e42016-09-13 10:45:46 -060078 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren21ef6a12011-05-31 10:30:37 +000079 /*
80 * DMASEL[4:3]
81 * 00 = Selects SDMA
82 * 01 = Reserved
83 * 10 = Selects 32-bit Address ADMA2
84 * 11 = Selects 64-bit Address ADMA2
85 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -060086 ctrl = readb(&priv->reg->hostctl);
Anton staaf8e42f0d2011-11-10 11:56:49 +000087 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
88 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060089 writeb(ctrl, &priv->reg->hostctl);
Tom Warren21ef6a12011-05-31 10:30:37 +000090
91 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warrenf53c4e42016-09-13 10:45:46 -060092 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
93 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren21ef6a12011-05-31 10:30:37 +000094}
95
Stephen Warrenf53c4e42016-09-13 10:45:46 -060096static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
97 struct mmc_data *data)
Tom Warren21ef6a12011-05-31 10:30:37 +000098{
99 unsigned short mode;
100 debug(" mmc_set_transfer_mode called\n");
101 /*
102 * TRNMOD
103 * MUL1SIN0[5] : Multi/Single Block Select
104 * RD1WT0[4] : Data Transfer Direction Select
105 * 1 = read
106 * 0 = write
107 * ENACMD12[2] : Auto CMD12 Enable
108 * ENBLKCNT[1] : Block Count Enable
109 * ENDMA[0] : DMA Enable
110 */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000111 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
112 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
113
Tom Warren21ef6a12011-05-31 10:30:37 +0000114 if (data->blocks > 1)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000115 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
116
Tom Warren21ef6a12011-05-31 10:30:37 +0000117 if (data->flags & MMC_DATA_READ)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000118 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren21ef6a12011-05-31 10:30:37 +0000119
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600120 writew(mode, &priv->reg->trnmod);
Tom Warren21ef6a12011-05-31 10:30:37 +0000121}
122
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600123static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
124 struct mmc_cmd *cmd,
125 struct mmc_data *data,
126 unsigned int timeout)
Tom Warren21ef6a12011-05-31 10:30:37 +0000127{
Tom Warren21ef6a12011-05-31 10:30:37 +0000128 /*
129 * PRNSTS
Anton staaf0963ff32011-11-10 11:56:52 +0000130 * CMDINHDAT[1] : Command Inhibit (DAT)
131 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren21ef6a12011-05-31 10:30:37 +0000132 */
Anton staaf0963ff32011-11-10 11:56:52 +0000133 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren21ef6a12011-05-31 10:30:37 +0000134
135 /*
136 * We shouldn't wait for data inhibit for stop commands, even
137 * though they might use busy signaling
138 */
Anton staaf0963ff32011-11-10 11:56:52 +0000139 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
140 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren21ef6a12011-05-31 10:30:37 +0000141
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600142 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000143 if (timeout == 0) {
144 printf("%s: timeout error\n", __func__);
145 return -1;
146 }
147 timeout--;
148 udelay(1000);
149 }
150
Anton staaf0963ff32011-11-10 11:56:52 +0000151 return 0;
152}
153
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600154static int tegra_mmc_send_cmd_bounced(struct mmc *mmc, struct mmc_cmd *cmd,
155 struct mmc_data *data,
156 struct bounce_buffer *bbstate)
Anton staaf0963ff32011-11-10 11:56:52 +0000157{
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600158 struct tegra_mmc_priv *priv = mmc->priv;
Anton staaf0963ff32011-11-10 11:56:52 +0000159 int flags, i;
160 int result;
Anatolij Gustschin60e242e2012-03-28 03:40:00 +0000161 unsigned int mask = 0;
Anton staaf0963ff32011-11-10 11:56:52 +0000162 unsigned int retry = 0x100000;
163 debug(" mmc_send_cmd called\n");
164
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600165 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf0963ff32011-11-10 11:56:52 +0000166
167 if (result < 0)
168 return result;
169
Tom Warren21ef6a12011-05-31 10:30:37 +0000170 if (data)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600171 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren21ef6a12011-05-31 10:30:37 +0000172
173 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600174 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren21ef6a12011-05-31 10:30:37 +0000175
176 if (data)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600177 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren21ef6a12011-05-31 10:30:37 +0000178
179 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
180 return -1;
181
182 /*
183 * CMDREG
184 * CMDIDX[13:8] : Command index
185 * DATAPRNT[5] : Data Present Select
186 * ENCMDIDX[4] : Command Index Check Enable
187 * ENCMDCRC[3] : Command CRC Check Enable
188 * RSPTYP[1:0]
189 * 00 = No Response
190 * 01 = Length 136
191 * 10 = Length 48
192 * 11 = Length 48 Check busy after response
193 */
194 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf8e42f0d2011-11-10 11:56:49 +0000195 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren21ef6a12011-05-31 10:30:37 +0000196 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000197 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren21ef6a12011-05-31 10:30:37 +0000198 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000199 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren21ef6a12011-05-31 10:30:37 +0000200 else
Anton staaf8e42f0d2011-11-10 11:56:49 +0000201 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren21ef6a12011-05-31 10:30:37 +0000202
203 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000204 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren21ef6a12011-05-31 10:30:37 +0000205 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000206 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren21ef6a12011-05-31 10:30:37 +0000207 if (data)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000208 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren21ef6a12011-05-31 10:30:37 +0000209
210 debug("cmd: %d\n", cmd->cmdidx);
211
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600212 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren21ef6a12011-05-31 10:30:37 +0000213
214 for (i = 0; i < retry; i++) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600215 mask = readl(&priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000216 /* Command Complete */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000217 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000218 if (!data)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600219 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000220 break;
221 }
222 }
223
224 if (i == retry) {
225 printf("%s: waiting for status update\n", __func__);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600226 writel(mask, &priv->reg->norintsts);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900227 return -ETIMEDOUT;
Tom Warren21ef6a12011-05-31 10:30:37 +0000228 }
229
Anton staaf8e42f0d2011-11-10 11:56:49 +0000230 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000231 /* Timeout Error */
232 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600233 writel(mask, &priv->reg->norintsts);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900234 return -ETIMEDOUT;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000235 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000236 /* Error Interrupt */
237 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600238 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000239 return -1;
240 }
241
242 if (cmd->resp_type & MMC_RSP_PRESENT) {
243 if (cmd->resp_type & MMC_RSP_136) {
244 /* CRC is stripped so we need to do some shifting. */
245 for (i = 0; i < 4; i++) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600246 unsigned long offset = (unsigned long)
247 (&priv->reg->rspreg3 - i);
Tom Warren21ef6a12011-05-31 10:30:37 +0000248 cmd->response[i] = readl(offset) << 8;
249
250 if (i != 3) {
251 cmd->response[i] |=
252 readb(offset - 1);
253 }
254 debug("cmd->resp[%d]: %08x\n",
255 i, cmd->response[i]);
256 }
257 } else if (cmd->resp_type & MMC_RSP_BUSY) {
258 for (i = 0; i < retry; i++) {
259 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600260 if (readl(&priv->reg->prnsts)
Tom Warren21ef6a12011-05-31 10:30:37 +0000261 & (1 << 20)) /* DAT[0] */
262 break;
263 }
264
265 if (i == retry) {
266 printf("%s: card is still busy\n", __func__);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600267 writel(mask, &priv->reg->norintsts);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900268 return -ETIMEDOUT;
Tom Warren21ef6a12011-05-31 10:30:37 +0000269 }
270
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600271 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren21ef6a12011-05-31 10:30:37 +0000272 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
273 } else {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600274 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren21ef6a12011-05-31 10:30:37 +0000275 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
276 }
277 }
278
279 if (data) {
Anton staaf9b3d1872011-11-10 11:56:51 +0000280 unsigned long start = get_timer(0);
281
Tom Warren21ef6a12011-05-31 10:30:37 +0000282 while (1) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600283 mask = readl(&priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000284
Anton staaf8e42f0d2011-11-10 11:56:49 +0000285 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000286 /* Error Interrupt */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600287 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000288 printf("%s: error during transfer: 0x%08x\n",
289 __func__, mask);
290 return -1;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000291 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf5a762e22011-11-10 11:56:50 +0000292 /*
293 * DMA Interrupt, restart the transfer where
294 * it was interrupted.
295 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600296 unsigned int address = readl(&priv->reg->sysad);
Anton staaf5a762e22011-11-10 11:56:50 +0000297
Tom Warren21ef6a12011-05-31 10:30:37 +0000298 debug("DMA end\n");
Anton staaf5a762e22011-11-10 11:56:50 +0000299 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600300 &priv->reg->norintsts);
301 writel(address, &priv->reg->sysad);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000302 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000303 /* Transfer Complete */
304 debug("r/w is done\n");
305 break;
Marcel Ziswiler09fb7362014-10-04 01:48:53 +0200306 } else if (get_timer(start) > 8000UL) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600307 writel(mask, &priv->reg->norintsts);
Anton staaf9b3d1872011-11-10 11:56:51 +0000308 printf("%s: MMC Timeout\n"
309 " Interrupt status 0x%08x\n"
310 " Interrupt status enable 0x%08x\n"
311 " Interrupt signal enable 0x%08x\n"
312 " Present status 0x%08x\n",
313 __func__, mask,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600314 readl(&priv->reg->norintstsen),
315 readl(&priv->reg->norintsigen),
316 readl(&priv->reg->prnsts));
Anton staaf9b3d1872011-11-10 11:56:51 +0000317 return -1;
Tom Warren21ef6a12011-05-31 10:30:37 +0000318 }
319 }
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600320 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000321 }
322
323 udelay(1000);
324 return 0;
325}
326
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200327static int tegra_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600328 struct mmc_data *data)
Stephen Warren19815392012-11-06 11:27:30 +0000329{
330 void *buf;
331 unsigned int bbflags;
332 size_t len;
333 struct bounce_buffer bbstate;
334 int ret;
335
336 if (data) {
337 if (data->flags & MMC_DATA_READ) {
338 buf = data->dest;
339 bbflags = GEN_BB_WRITE;
340 } else {
341 buf = (void *)data->src;
342 bbflags = GEN_BB_READ;
343 }
344 len = data->blocks * data->blocksize;
345
346 bounce_buffer_start(&bbstate, buf, len, bbflags);
347 }
348
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600349 ret = tegra_mmc_send_cmd_bounced(mmc, cmd, data, &bbstate);
Stephen Warren19815392012-11-06 11:27:30 +0000350
351 if (data)
352 bounce_buffer_stop(&bbstate);
353
354 return ret;
355}
356
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600357static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren21ef6a12011-05-31 10:30:37 +0000358{
Stephen Warrene8adca92016-09-13 10:46:01 -0600359 ulong rate;
Simon Glass4ed59e72011-09-21 12:40:04 +0000360 int div;
Tom Warren21ef6a12011-05-31 10:30:37 +0000361 unsigned short clk;
362 unsigned long timeout;
Simon Glass4ed59e72011-09-21 12:40:04 +0000363
Tom Warren21ef6a12011-05-31 10:30:37 +0000364 debug(" mmc_change_clock called\n");
365
Simon Glass4ed59e72011-09-21 12:40:04 +0000366 /*
Tom Warren2d348a12013-02-26 12:31:26 -0700367 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glass4ed59e72011-09-21 12:40:04 +0000368 */
Tom Warren21ef6a12011-05-31 10:30:37 +0000369 if (clock == 0)
370 goto out;
Stephen Warrene8adca92016-09-13 10:46:01 -0600371
372 rate = clk_set_rate(&priv->clk, clock);
373 div = (rate + clock - 1) / clock;
Simon Glass4ed59e72011-09-21 12:40:04 +0000374 debug("div = %d\n", div);
Tom Warren21ef6a12011-05-31 10:30:37 +0000375
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600376 writew(0, &priv->reg->clkcon);
Tom Warren21ef6a12011-05-31 10:30:37 +0000377
Tom Warren21ef6a12011-05-31 10:30:37 +0000378 /*
379 * CLKCON
380 * SELFREQ[15:8] : base clock divided by value
381 * ENSDCLK[2] : SD Clock Enable
382 * STBLINTCLK[1] : Internal Clock Stable
383 * ENINTCLK[0] : Internal Clock Enable
384 */
Simon Glass4ed59e72011-09-21 12:40:04 +0000385 div >>= 1;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000386 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
387 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600388 writew(clk, &priv->reg->clkcon);
Tom Warren21ef6a12011-05-31 10:30:37 +0000389
390 /* Wait max 10 ms */
391 timeout = 10;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600392 while (!(readw(&priv->reg->clkcon) &
Anton staaf8e42f0d2011-11-10 11:56:49 +0000393 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000394 if (timeout == 0) {
395 printf("%s: timeout error\n", __func__);
396 return;
397 }
398 timeout--;
399 udelay(1000);
400 }
401
Anton staaf8e42f0d2011-11-10 11:56:49 +0000402 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600403 writew(clk, &priv->reg->clkcon);
Tom Warren21ef6a12011-05-31 10:30:37 +0000404
405 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren21ef6a12011-05-31 10:30:37 +0000406
407out:
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600408 priv->clock = clock;
Tom Warren21ef6a12011-05-31 10:30:37 +0000409}
410
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200411static void tegra_mmc_set_ios(struct mmc *mmc)
Tom Warren21ef6a12011-05-31 10:30:37 +0000412{
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600413 struct tegra_mmc_priv *priv = mmc->priv;
Tom Warren21ef6a12011-05-31 10:30:37 +0000414 unsigned char ctrl;
415 debug(" mmc_set_ios called\n");
416
417 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
418
419 /* Change clock first */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600420 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren21ef6a12011-05-31 10:30:37 +0000421
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600422 ctrl = readb(&priv->reg->hostctl);
Tom Warren21ef6a12011-05-31 10:30:37 +0000423
424 /*
425 * WIDE8[5]
426 * 0 = Depend on WIDE4
427 * 1 = 8-bit mode
428 * WIDE4[1]
429 * 1 = 4-bit mode
430 * 0 = 1-bit mode
431 */
432 if (mmc->bus_width == 8)
433 ctrl |= (1 << 5);
434 else if (mmc->bus_width == 4)
435 ctrl |= (1 << 1);
436 else
437 ctrl &= ~(1 << 1);
438
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600439 writeb(ctrl, &priv->reg->hostctl);
Tom Warren21ef6a12011-05-31 10:30:37 +0000440 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
441}
442
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600443static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warren6b835882016-09-13 10:45:44 -0600444{
445#if defined(CONFIG_TEGRA30)
Stephen Warren6b835882016-09-13 10:45:44 -0600446 u32 val;
447
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600448 debug("%s: sdmmc address = %08x\n", __func__, (unsigned int)priv->reg);
Stephen Warren6b835882016-09-13 10:45:44 -0600449
450 /* Set the pad drive strength for SDMMC1 or 3 only */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600451 if (priv->reg != (void *)0x78000000 &&
452 priv->reg != (void *)0x78000400) {
Stephen Warren6b835882016-09-13 10:45:44 -0600453 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
454 __func__);
455 return;
456 }
457
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600458 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warren6b835882016-09-13 10:45:44 -0600459 val &= 0xFFFFFFF0;
460 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600461 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warren6b835882016-09-13 10:45:44 -0600462
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600463 val = readl(&priv->reg->autocalcfg);
Stephen Warren6b835882016-09-13 10:45:44 -0600464 val &= 0xFFFF0000;
465 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET | AUTO_CAL_ENABLED;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600466 writel(val, &priv->reg->autocalcfg);
Stephen Warren6b835882016-09-13 10:45:44 -0600467#endif
468}
469
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600470static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren21ef6a12011-05-31 10:30:37 +0000471{
472 unsigned int timeout;
473 debug(" mmc_reset called\n");
474
475 /*
476 * RSTALL[0] : Software reset for all
477 * 1 = reset
478 * 0 = work
479 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600480 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren21ef6a12011-05-31 10:30:37 +0000481
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600482 priv->clock = 0;
Tom Warren21ef6a12011-05-31 10:30:37 +0000483
484 /* Wait max 100 ms */
485 timeout = 100;
486
487 /* hw clears the bit when it's done */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600488 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000489 if (timeout == 0) {
490 printf("%s: timeout error\n", __func__);
491 return;
492 }
493 timeout--;
494 udelay(1000);
495 }
Tom Warren2d348a12013-02-26 12:31:26 -0700496
497 /* Set SD bus voltage & enable bus power */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600498 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren2d348a12013-02-26 12:31:26 -0700499 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600500 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren2d348a12013-02-26 12:31:26 -0700501
502 /* Make sure SDIO pads are set up */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600503 tegra_mmc_pad_init(priv);
Tom Warren21ef6a12011-05-31 10:30:37 +0000504}
505
Tom Warren6a474db2016-09-13 10:45:48 -0600506static int tegra_mmc_init(struct mmc *mmc)
Tom Warren21ef6a12011-05-31 10:30:37 +0000507{
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600508 struct tegra_mmc_priv *priv = mmc->priv;
Tom Warren21ef6a12011-05-31 10:30:37 +0000509 unsigned int mask;
Tom Warren6a474db2016-09-13 10:45:48 -0600510 debug(" tegra_mmc_init called\n");
Tom Warren21ef6a12011-05-31 10:30:37 +0000511
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600512 tegra_mmc_reset(priv, mmc);
Tom Warren21ef6a12011-05-31 10:30:37 +0000513
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600514 priv->version = readw(&priv->reg->hcver);
515 debug("host version = %x\n", priv->version);
Tom Warren21ef6a12011-05-31 10:30:37 +0000516
517 /* mask all */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600518 writel(0xffffffff, &priv->reg->norintstsen);
519 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000520
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600521 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren21ef6a12011-05-31 10:30:37 +0000522 /*
523 * NORMAL Interrupt Status Enable Register init
524 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
525 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf5a762e22011-11-10 11:56:50 +0000526 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren21ef6a12011-05-31 10:30:37 +0000527 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
528 * [0] ENSTACMDCMPLT : Command Complete Status Enable
529 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600530 mask = readl(&priv->reg->norintstsen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000531 mask &= ~(0xffff);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000532 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
533 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf5a762e22011-11-10 11:56:50 +0000534 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf8e42f0d2011-11-10 11:56:49 +0000535 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
536 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600537 writel(mask, &priv->reg->norintstsen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000538
539 /*
540 * NORMAL Interrupt Signal Enable Register init
541 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
542 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600543 mask = readl(&priv->reg->norintsigen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000544 mask &= ~(0xffff);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000545 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600546 writel(mask, &priv->reg->norintsigen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000547
548 return 0;
549}
550
Jeroen Hofstee19d7bf32014-10-08 22:57:46 +0200551static int tegra_mmc_getcd(struct mmc *mmc)
Thierry Redingbf836622012-01-02 01:15:39 +0000552{
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600553 struct tegra_mmc_priv *priv = mmc->priv;
Thierry Redingbf836622012-01-02 01:15:39 +0000554
Tom Warren29f3e3f2012-09-04 17:00:24 -0700555 debug("tegra_mmc_getcd called\n");
Thierry Redingbf836622012-01-02 01:15:39 +0000556
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600557 if (dm_gpio_is_valid(&priv->cd_gpio))
558 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingbf836622012-01-02 01:15:39 +0000559
560 return 1;
561}
562
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200563static const struct mmc_ops tegra_mmc_ops = {
564 .send_cmd = tegra_mmc_send_cmd,
565 .set_ios = tegra_mmc_set_ios,
Tom Warren6a474db2016-09-13 10:45:48 -0600566 .init = tegra_mmc_init,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200567 .getcd = tegra_mmc_getcd,
568};
569
Tom Warren6a474db2016-09-13 10:45:48 -0600570static int tegra_mmc_probe(struct udevice *dev)
Tom Warren21ef6a12011-05-31 10:30:37 +0000571{
Tom Warren6a474db2016-09-13 10:45:48 -0600572 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
573 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Stephen Warrene8adca92016-09-13 10:46:01 -0600574 int bus_width, ret;
Tom Warren21ef6a12011-05-31 10:30:37 +0000575
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600576 priv->cfg.name = "Tegra SD/MMC";
577 priv->cfg.ops = &tegra_mmc_ops;
Tom Warren21ef6a12011-05-31 10:30:37 +0000578
Tom Warren6a474db2016-09-13 10:45:48 -0600579 bus_width = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "bus-width",
580 1);
581
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600582 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
583 priv->cfg.host_caps = 0;
Tom Warren6a474db2016-09-13 10:45:48 -0600584 if (bus_width == 8)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600585 priv->cfg.host_caps |= MMC_MODE_8BIT;
Tom Warren6a474db2016-09-13 10:45:48 -0600586 if (bus_width >= 4)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600587 priv->cfg.host_caps |= MMC_MODE_4BIT;
588 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren21ef6a12011-05-31 10:30:37 +0000589
590 /*
591 * min freq is for card identification, and is the highest
592 * low-speed SDIO card frequency (actually 400KHz)
593 * max freq is highest HS eMMC clock as per the SD/MMC spec
594 * (actually 52MHz)
Tom Warren21ef6a12011-05-31 10:30:37 +0000595 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600596 priv->cfg.f_min = 375000;
597 priv->cfg.f_max = 48000000;
Tom Warren21ef6a12011-05-31 10:30:37 +0000598
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600599 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200600
Tom Warren6a474db2016-09-13 10:45:48 -0600601 priv->reg = (void *)dev_get_addr(dev);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000602
Tom Warren6a474db2016-09-13 10:45:48 -0600603 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
604 if (ret) {
605 debug("reset_get_by_name() failed: %d\n", ret);
606 return ret;
Stephen Warrenc0493072016-08-05 16:10:33 -0600607 }
Tom Warren6a474db2016-09-13 10:45:48 -0600608 ret = clk_get_by_index(dev, 0, &priv->clk);
609 if (ret) {
610 debug("clk_get_by_index() failed: %d\n", ret);
611 return ret;
612 }
613
614 ret = reset_assert(&priv->reset_ctl);
615 if (ret)
616 return ret;
617 ret = clk_enable(&priv->clk);
618 if (ret)
619 return ret;
620 ret = clk_set_rate(&priv->clk, 20000000);
621 if (IS_ERR_VALUE(ret))
622 return ret;
623 ret = reset_deassert(&priv->reset_ctl);
624 if (ret)
625 return ret;
Tom Warrenc9aa8312013-02-21 12:31:30 +0000626
627 /* These GPIOs are optional */
Tom Warren6a474db2016-09-13 10:45:48 -0600628 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
629 GPIOD_IS_IN);
630 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
631 GPIOD_IS_IN);
632 gpio_request_by_name(dev, "power-gpios", 0,
633 &priv->pwr_gpio, GPIOD_IS_OUT);
634 if (dm_gpio_is_valid(&priv->pwr_gpio))
635 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000636
Tom Warren6a474db2016-09-13 10:45:48 -0600637 priv->mmc = mmc_create(&priv->cfg, priv);
638 if (priv->mmc == NULL)
639 return -1;
640
641 priv->mmc->dev = dev;
642 upriv->mmc = priv->mmc;
643
Tom Warrenc9aa8312013-02-21 12:31:30 +0000644 return 0;
645}
646
Tom Warren6a474db2016-09-13 10:45:48 -0600647static const struct udevice_id tegra_mmc_ids[] = {
648 { .compatible = "nvidia,tegra20-sdhci" },
649 { .compatible = "nvidia,tegra30-sdhci" },
650 { .compatible = "nvidia,tegra114-sdhci" },
651 { .compatible = "nvidia,tegra124-sdhci" },
652 { .compatible = "nvidia,tegra210-sdhci" },
653 { .compatible = "nvidia,tegra186-sdhci" },
654 { }
655};
Tom Warrenc9aa8312013-02-21 12:31:30 +0000656
Tom Warren6a474db2016-09-13 10:45:48 -0600657U_BOOT_DRIVER(tegra_mmc_drv) = {
658 .name = "tegra_mmc",
659 .id = UCLASS_MMC,
660 .of_match = tegra_mmc_ids,
661 .probe = tegra_mmc_probe,
662 .priv_auto_alloc_size = sizeof(struct tegra_mmc_priv),
663};