blob: f022e93552521f8d414fbfcae363d7f3373b2bce [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warren21ef6a12011-05-31 10:30:37 +00002/*
3 * (C) Copyright 2009 SAMSUNG Electronics
4 * Minkyu Kang <mk7.kang@samsung.com>
5 * Jaehoon Chung <jh80.chung@samsung.com>
Tom Warren6a474db2016-09-13 10:45:48 -06006 * Portions Copyright 2011-2016 NVIDIA Corporation
Tom Warren21ef6a12011-05-31 10:30:37 +00007 */
8
Stephen Warren19815392012-11-06 11:27:30 +00009#include <bouncebuf.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000010#include <common.h>
Simon Glass9d922452017-05-17 17:18:03 -060011#include <dm.h>
Jaehoon Chung915ffa52016-07-19 16:33:36 +090012#include <errno.h>
Simon Glass49cb9302017-07-25 08:30:08 -060013#include <mmc.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>
Simon Glass61b29b82020-02-03 07:36:15 -070017#include <linux/err.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000018
Simon Glass0e513e72017-04-23 20:02:11 -060019struct tegra_mmc_plat {
20 struct mmc_config cfg;
21 struct mmc mmc;
22};
23
Stephen Warrenf53c4e42016-09-13 10:45:46 -060024struct tegra_mmc_priv {
25 struct tegra_mmc *reg;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060026 struct reset_ctl reset_ctl;
27 struct clk clk;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060028 struct gpio_desc cd_gpio; /* Change Detect GPIO */
29 struct gpio_desc pwr_gpio; /* Power GPIO */
30 struct gpio_desc wp_gpio; /* Write Protect GPIO */
31 unsigned int version; /* SDHCI spec. version */
32 unsigned int clock; /* Current clock (MHz) */
Stephen Warrenf53c4e42016-09-13 10:45:46 -060033};
34
Stephen Warrenf53c4e42016-09-13 10:45:46 -060035static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
36 unsigned short power)
Tom Warren2d348a12013-02-26 12:31:26 -070037{
38 u8 pwr = 0;
39 debug("%s: power = %x\n", __func__, power);
40
41 if (power != (unsigned short)-1) {
42 switch (1 << power) {
43 case MMC_VDD_165_195:
44 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
45 break;
46 case MMC_VDD_29_30:
47 case MMC_VDD_30_31:
48 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
49 break;
50 case MMC_VDD_32_33:
51 case MMC_VDD_33_34:
52 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
53 break;
54 }
55 }
56 debug("%s: pwr = %X\n", __func__, pwr);
57
58 /* Set the bus voltage first (if any) */
Stephen Warrenf53c4e42016-09-13 10:45:46 -060059 writeb(pwr, &priv->reg->pwrcon);
Tom Warren2d348a12013-02-26 12:31:26 -070060 if (pwr == 0)
61 return;
62
63 /* Now enable bus power */
64 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060065 writeb(pwr, &priv->reg->pwrcon);
Tom Warren2d348a12013-02-26 12:31:26 -070066}
67
Stephen Warrenf53c4e42016-09-13 10:45:46 -060068static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
69 struct mmc_data *data,
70 struct bounce_buffer *bbstate)
Tom Warren21ef6a12011-05-31 10:30:37 +000071{
72 unsigned char ctrl;
73
Tom Warren21ef6a12011-05-31 10:30:37 +000074
Stephen Warren19815392012-11-06 11:27:30 +000075 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
76 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
77 data->blocksize);
78
Stephen Warrenf53c4e42016-09-13 10:45:46 -060079 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren21ef6a12011-05-31 10:30:37 +000080 /*
81 * DMASEL[4:3]
82 * 00 = Selects SDMA
83 * 01 = Reserved
84 * 10 = Selects 32-bit Address ADMA2
85 * 11 = Selects 64-bit Address ADMA2
86 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -060087 ctrl = readb(&priv->reg->hostctl);
Anton staaf8e42f0d2011-11-10 11:56:49 +000088 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
89 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warrenf53c4e42016-09-13 10:45:46 -060090 writeb(ctrl, &priv->reg->hostctl);
Tom Warren21ef6a12011-05-31 10:30:37 +000091
92 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warrenf53c4e42016-09-13 10:45:46 -060093 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
94 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren21ef6a12011-05-31 10:30:37 +000095}
96
Stephen Warrenf53c4e42016-09-13 10:45:46 -060097static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
98 struct mmc_data *data)
Tom Warren21ef6a12011-05-31 10:30:37 +000099{
100 unsigned short mode;
101 debug(" mmc_set_transfer_mode called\n");
102 /*
103 * TRNMOD
104 * MUL1SIN0[5] : Multi/Single Block Select
105 * RD1WT0[4] : Data Transfer Direction Select
106 * 1 = read
107 * 0 = write
108 * ENACMD12[2] : Auto CMD12 Enable
109 * ENBLKCNT[1] : Block Count Enable
110 * ENDMA[0] : DMA Enable
111 */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000112 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
113 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
114
Tom Warren21ef6a12011-05-31 10:30:37 +0000115 if (data->blocks > 1)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000116 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
117
Tom Warren21ef6a12011-05-31 10:30:37 +0000118 if (data->flags & MMC_DATA_READ)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000119 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren21ef6a12011-05-31 10:30:37 +0000120
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600121 writew(mode, &priv->reg->trnmod);
Tom Warren21ef6a12011-05-31 10:30:37 +0000122}
123
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600124static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
125 struct mmc_cmd *cmd,
126 struct mmc_data *data,
127 unsigned int timeout)
Tom Warren21ef6a12011-05-31 10:30:37 +0000128{
Tom Warren21ef6a12011-05-31 10:30:37 +0000129 /*
130 * PRNSTS
Anton staaf0963ff32011-11-10 11:56:52 +0000131 * CMDINHDAT[1] : Command Inhibit (DAT)
132 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren21ef6a12011-05-31 10:30:37 +0000133 */
Anton staaf0963ff32011-11-10 11:56:52 +0000134 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren21ef6a12011-05-31 10:30:37 +0000135
136 /*
137 * We shouldn't wait for data inhibit for stop commands, even
138 * though they might use busy signaling
139 */
Anton staaf0963ff32011-11-10 11:56:52 +0000140 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
141 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren21ef6a12011-05-31 10:30:37 +0000142
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600143 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000144 if (timeout == 0) {
145 printf("%s: timeout error\n", __func__);
146 return -1;
147 }
148 timeout--;
149 udelay(1000);
150 }
151
Anton staaf0963ff32011-11-10 11:56:52 +0000152 return 0;
153}
154
Simon Glass0e513e72017-04-23 20:02:11 -0600155static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600156 struct mmc_data *data,
157 struct bounce_buffer *bbstate)
Anton staaf0963ff32011-11-10 11:56:52 +0000158{
Simon Glass0e513e72017-04-23 20:02:11 -0600159 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Anton staaf0963ff32011-11-10 11:56:52 +0000160 int flags, i;
161 int result;
Anatolij Gustschin60e242e2012-03-28 03:40:00 +0000162 unsigned int mask = 0;
Anton staaf0963ff32011-11-10 11:56:52 +0000163 unsigned int retry = 0x100000;
164 debug(" mmc_send_cmd called\n");
165
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600166 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf0963ff32011-11-10 11:56:52 +0000167
168 if (result < 0)
169 return result;
170
Tom Warren21ef6a12011-05-31 10:30:37 +0000171 if (data)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600172 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren21ef6a12011-05-31 10:30:37 +0000173
174 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600175 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren21ef6a12011-05-31 10:30:37 +0000176
177 if (data)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600178 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren21ef6a12011-05-31 10:30:37 +0000179
180 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
181 return -1;
182
183 /*
184 * CMDREG
185 * CMDIDX[13:8] : Command index
186 * DATAPRNT[5] : Data Present Select
187 * ENCMDIDX[4] : Command Index Check Enable
188 * ENCMDCRC[3] : Command CRC Check Enable
189 * RSPTYP[1:0]
190 * 00 = No Response
191 * 01 = Length 136
192 * 10 = Length 48
193 * 11 = Length 48 Check busy after response
194 */
195 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf8e42f0d2011-11-10 11:56:49 +0000196 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren21ef6a12011-05-31 10:30:37 +0000197 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000198 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren21ef6a12011-05-31 10:30:37 +0000199 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000200 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren21ef6a12011-05-31 10:30:37 +0000201 else
Anton staaf8e42f0d2011-11-10 11:56:49 +0000202 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren21ef6a12011-05-31 10:30:37 +0000203
204 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000205 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren21ef6a12011-05-31 10:30:37 +0000206 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000207 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren21ef6a12011-05-31 10:30:37 +0000208 if (data)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000209 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren21ef6a12011-05-31 10:30:37 +0000210
211 debug("cmd: %d\n", cmd->cmdidx);
212
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600213 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren21ef6a12011-05-31 10:30:37 +0000214
215 for (i = 0; i < retry; i++) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600216 mask = readl(&priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000217 /* Command Complete */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000218 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000219 if (!data)
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600220 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000221 break;
222 }
223 }
224
225 if (i == retry) {
226 printf("%s: waiting for status update\n", __func__);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600227 writel(mask, &priv->reg->norintsts);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900228 return -ETIMEDOUT;
Tom Warren21ef6a12011-05-31 10:30:37 +0000229 }
230
Anton staaf8e42f0d2011-11-10 11:56:49 +0000231 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000232 /* Timeout Error */
233 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600234 writel(mask, &priv->reg->norintsts);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900235 return -ETIMEDOUT;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000236 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000237 /* Error Interrupt */
238 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600239 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000240 return -1;
241 }
242
243 if (cmd->resp_type & MMC_RSP_PRESENT) {
244 if (cmd->resp_type & MMC_RSP_136) {
245 /* CRC is stripped so we need to do some shifting. */
246 for (i = 0; i < 4; i++) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600247 unsigned long offset = (unsigned long)
248 (&priv->reg->rspreg3 - i);
Tom Warren21ef6a12011-05-31 10:30:37 +0000249 cmd->response[i] = readl(offset) << 8;
250
251 if (i != 3) {
252 cmd->response[i] |=
253 readb(offset - 1);
254 }
255 debug("cmd->resp[%d]: %08x\n",
256 i, cmd->response[i]);
257 }
258 } else if (cmd->resp_type & MMC_RSP_BUSY) {
259 for (i = 0; i < retry; i++) {
260 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600261 if (readl(&priv->reg->prnsts)
Tom Warren21ef6a12011-05-31 10:30:37 +0000262 & (1 << 20)) /* DAT[0] */
263 break;
264 }
265
266 if (i == retry) {
267 printf("%s: card is still busy\n", __func__);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600268 writel(mask, &priv->reg->norintsts);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900269 return -ETIMEDOUT;
Tom Warren21ef6a12011-05-31 10:30:37 +0000270 }
271
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600272 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren21ef6a12011-05-31 10:30:37 +0000273 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
274 } else {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600275 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren21ef6a12011-05-31 10:30:37 +0000276 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
277 }
278 }
279
280 if (data) {
Anton staaf9b3d1872011-11-10 11:56:51 +0000281 unsigned long start = get_timer(0);
282
Tom Warren21ef6a12011-05-31 10:30:37 +0000283 while (1) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600284 mask = readl(&priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000285
Anton staaf8e42f0d2011-11-10 11:56:49 +0000286 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000287 /* Error Interrupt */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600288 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000289 printf("%s: error during transfer: 0x%08x\n",
290 __func__, mask);
291 return -1;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000292 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf5a762e22011-11-10 11:56:50 +0000293 /*
294 * DMA Interrupt, restart the transfer where
295 * it was interrupted.
296 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600297 unsigned int address = readl(&priv->reg->sysad);
Anton staaf5a762e22011-11-10 11:56:50 +0000298
Tom Warren21ef6a12011-05-31 10:30:37 +0000299 debug("DMA end\n");
Anton staaf5a762e22011-11-10 11:56:50 +0000300 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600301 &priv->reg->norintsts);
302 writel(address, &priv->reg->sysad);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000303 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000304 /* Transfer Complete */
305 debug("r/w is done\n");
306 break;
Marcel Ziswiler09fb7362014-10-04 01:48:53 +0200307 } else if (get_timer(start) > 8000UL) {
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600308 writel(mask, &priv->reg->norintsts);
Anton staaf9b3d1872011-11-10 11:56:51 +0000309 printf("%s: MMC Timeout\n"
310 " Interrupt status 0x%08x\n"
311 " Interrupt status enable 0x%08x\n"
312 " Interrupt signal enable 0x%08x\n"
313 " Present status 0x%08x\n",
314 __func__, mask,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600315 readl(&priv->reg->norintstsen),
316 readl(&priv->reg->norintsigen),
317 readl(&priv->reg->prnsts));
Anton staaf9b3d1872011-11-10 11:56:51 +0000318 return -1;
Tom Warren21ef6a12011-05-31 10:30:37 +0000319 }
320 }
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600321 writel(mask, &priv->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000322 }
323
324 udelay(1000);
325 return 0;
326}
327
Simon Glass0e513e72017-04-23 20:02:11 -0600328static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600329 struct mmc_data *data)
Stephen Warren19815392012-11-06 11:27:30 +0000330{
331 void *buf;
332 unsigned int bbflags;
333 size_t len;
334 struct bounce_buffer bbstate;
335 int ret;
336
337 if (data) {
338 if (data->flags & MMC_DATA_READ) {
339 buf = data->dest;
340 bbflags = GEN_BB_WRITE;
341 } else {
342 buf = (void *)data->src;
343 bbflags = GEN_BB_READ;
344 }
345 len = data->blocks * data->blocksize;
346
347 bounce_buffer_start(&bbstate, buf, len, bbflags);
348 }
349
Simon Glass0e513e72017-04-23 20:02:11 -0600350 ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate);
Stephen Warren19815392012-11-06 11:27:30 +0000351
352 if (data)
353 bounce_buffer_stop(&bbstate);
354
355 return ret;
356}
357
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600358static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren21ef6a12011-05-31 10:30:37 +0000359{
Stephen Warrene8adca92016-09-13 10:46:01 -0600360 ulong rate;
Simon Glass4ed59e72011-09-21 12:40:04 +0000361 int div;
Tom Warren21ef6a12011-05-31 10:30:37 +0000362 unsigned short clk;
363 unsigned long timeout;
Simon Glass4ed59e72011-09-21 12:40:04 +0000364
Tom Warren21ef6a12011-05-31 10:30:37 +0000365 debug(" mmc_change_clock called\n");
366
Simon Glass4ed59e72011-09-21 12:40:04 +0000367 /*
Tom Warren2d348a12013-02-26 12:31:26 -0700368 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glass4ed59e72011-09-21 12:40:04 +0000369 */
Tom Warren21ef6a12011-05-31 10:30:37 +0000370 if (clock == 0)
371 goto out;
Stephen Warrene8adca92016-09-13 10:46:01 -0600372
373 rate = clk_set_rate(&priv->clk, clock);
374 div = (rate + clock - 1) / clock;
Simon Glass4ed59e72011-09-21 12:40:04 +0000375 debug("div = %d\n", div);
Tom Warren21ef6a12011-05-31 10:30:37 +0000376
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600377 writew(0, &priv->reg->clkcon);
Tom Warren21ef6a12011-05-31 10:30:37 +0000378
Tom Warren21ef6a12011-05-31 10:30:37 +0000379 /*
380 * CLKCON
381 * SELFREQ[15:8] : base clock divided by value
382 * ENSDCLK[2] : SD Clock Enable
383 * STBLINTCLK[1] : Internal Clock Stable
384 * ENINTCLK[0] : Internal Clock Enable
385 */
Simon Glass4ed59e72011-09-21 12:40:04 +0000386 div >>= 1;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000387 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
388 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600389 writew(clk, &priv->reg->clkcon);
Tom Warren21ef6a12011-05-31 10:30:37 +0000390
391 /* Wait max 10 ms */
392 timeout = 10;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600393 while (!(readw(&priv->reg->clkcon) &
Anton staaf8e42f0d2011-11-10 11:56:49 +0000394 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000395 if (timeout == 0) {
396 printf("%s: timeout error\n", __func__);
397 return;
398 }
399 timeout--;
400 udelay(1000);
401 }
402
Anton staaf8e42f0d2011-11-10 11:56:49 +0000403 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600404 writew(clk, &priv->reg->clkcon);
Tom Warren21ef6a12011-05-31 10:30:37 +0000405
406 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren21ef6a12011-05-31 10:30:37 +0000407
408out:
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600409 priv->clock = clock;
Tom Warren21ef6a12011-05-31 10:30:37 +0000410}
411
Simon Glass0e513e72017-04-23 20:02:11 -0600412static int tegra_mmc_set_ios(struct udevice *dev)
Tom Warren21ef6a12011-05-31 10:30:37 +0000413{
Simon Glass0e513e72017-04-23 20:02:11 -0600414 struct tegra_mmc_priv *priv = dev_get_priv(dev);
415 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren21ef6a12011-05-31 10:30:37 +0000416 unsigned char ctrl;
417 debug(" mmc_set_ios called\n");
418
419 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
420
421 /* Change clock first */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600422 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren21ef6a12011-05-31 10:30:37 +0000423
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600424 ctrl = readb(&priv->reg->hostctl);
Tom Warren21ef6a12011-05-31 10:30:37 +0000425
426 /*
427 * WIDE8[5]
428 * 0 = Depend on WIDE4
429 * 1 = 8-bit mode
430 * WIDE4[1]
431 * 1 = 4-bit mode
432 * 0 = 1-bit mode
433 */
434 if (mmc->bus_width == 8)
435 ctrl |= (1 << 5);
436 else if (mmc->bus_width == 4)
437 ctrl |= (1 << 1);
438 else
Simon Glass542b5f82017-06-07 21:11:48 -0600439 ctrl &= ~(1 << 1 | 1 << 5);
Tom Warren21ef6a12011-05-31 10:30:37 +0000440
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600441 writeb(ctrl, &priv->reg->hostctl);
Tom Warren21ef6a12011-05-31 10:30:37 +0000442 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900443
444 return 0;
Tom Warren21ef6a12011-05-31 10:30:37 +0000445}
446
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600447static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warren6b835882016-09-13 10:45:44 -0600448{
449#if defined(CONFIG_TEGRA30)
Stephen Warren6b835882016-09-13 10:45:44 -0600450 u32 val;
451
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600452 debug("%s: sdmmc address = %08x\n", __func__, (unsigned int)priv->reg);
Stephen Warren6b835882016-09-13 10:45:44 -0600453
454 /* Set the pad drive strength for SDMMC1 or 3 only */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600455 if (priv->reg != (void *)0x78000000 &&
456 priv->reg != (void *)0x78000400) {
Stephen Warren6b835882016-09-13 10:45:44 -0600457 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
458 __func__);
459 return;
460 }
461
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600462 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warren6b835882016-09-13 10:45:44 -0600463 val &= 0xFFFFFFF0;
464 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600465 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warren6b835882016-09-13 10:45:44 -0600466
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600467 val = readl(&priv->reg->autocalcfg);
Stephen Warren6b835882016-09-13 10:45:44 -0600468 val &= 0xFFFF0000;
469 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET | AUTO_CAL_ENABLED;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600470 writel(val, &priv->reg->autocalcfg);
Stephen Warren6b835882016-09-13 10:45:44 -0600471#endif
472}
473
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600474static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren21ef6a12011-05-31 10:30:37 +0000475{
476 unsigned int timeout;
477 debug(" mmc_reset called\n");
478
479 /*
480 * RSTALL[0] : Software reset for all
481 * 1 = reset
482 * 0 = work
483 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600484 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren21ef6a12011-05-31 10:30:37 +0000485
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600486 priv->clock = 0;
Tom Warren21ef6a12011-05-31 10:30:37 +0000487
488 /* Wait max 100 ms */
489 timeout = 100;
490
491 /* hw clears the bit when it's done */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600492 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000493 if (timeout == 0) {
494 printf("%s: timeout error\n", __func__);
495 return;
496 }
497 timeout--;
498 udelay(1000);
499 }
Tom Warren2d348a12013-02-26 12:31:26 -0700500
501 /* Set SD bus voltage & enable bus power */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600502 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren2d348a12013-02-26 12:31:26 -0700503 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600504 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren2d348a12013-02-26 12:31:26 -0700505
506 /* Make sure SDIO pads are set up */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600507 tegra_mmc_pad_init(priv);
Tom Warren21ef6a12011-05-31 10:30:37 +0000508}
509
Simon Glass0e513e72017-04-23 20:02:11 -0600510static int tegra_mmc_init(struct udevice *dev)
Tom Warren21ef6a12011-05-31 10:30:37 +0000511{
Simon Glass0e513e72017-04-23 20:02:11 -0600512 struct tegra_mmc_priv *priv = dev_get_priv(dev);
513 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren21ef6a12011-05-31 10:30:37 +0000514 unsigned int mask;
Tom Warren6a474db2016-09-13 10:45:48 -0600515 debug(" tegra_mmc_init called\n");
Tom Warren21ef6a12011-05-31 10:30:37 +0000516
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600517 tegra_mmc_reset(priv, mmc);
Tom Warren21ef6a12011-05-31 10:30:37 +0000518
Marcel Ziswiler4119b702017-03-25 01:18:22 +0100519#if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK)
520 /*
521 * Disable the external clock loopback and use the internal one on
522 * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1
523 * bits being set to 0xfffd according to the TRM.
524 *
525 * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled
526 * approach once proper kernel integration made it mainline.
527 */
528 if (priv->reg == (void *)0x700b0400) {
529 mask = readl(&priv->reg->venmiscctl);
530 mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK;
531 writel(mask, &priv->reg->venmiscctl);
532 }
533#endif
534
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600535 priv->version = readw(&priv->reg->hcver);
536 debug("host version = %x\n", priv->version);
Tom Warren21ef6a12011-05-31 10:30:37 +0000537
538 /* mask all */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600539 writel(0xffffffff, &priv->reg->norintstsen);
540 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000541
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600542 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren21ef6a12011-05-31 10:30:37 +0000543 /*
544 * NORMAL Interrupt Status Enable Register init
545 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
546 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf5a762e22011-11-10 11:56:50 +0000547 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren21ef6a12011-05-31 10:30:37 +0000548 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
549 * [0] ENSTACMDCMPLT : Command Complete Status Enable
550 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600551 mask = readl(&priv->reg->norintstsen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000552 mask &= ~(0xffff);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000553 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
554 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf5a762e22011-11-10 11:56:50 +0000555 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf8e42f0d2011-11-10 11:56:49 +0000556 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
557 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600558 writel(mask, &priv->reg->norintstsen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000559
560 /*
561 * NORMAL Interrupt Signal Enable Register init
562 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
563 */
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600564 mask = readl(&priv->reg->norintsigen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000565 mask &= ~(0xffff);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000566 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600567 writel(mask, &priv->reg->norintsigen);
Tom Warren21ef6a12011-05-31 10:30:37 +0000568
569 return 0;
570}
571
Simon Glass0e513e72017-04-23 20:02:11 -0600572static int tegra_mmc_getcd(struct udevice *dev)
Thierry Redingbf836622012-01-02 01:15:39 +0000573{
Simon Glass0e513e72017-04-23 20:02:11 -0600574 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Thierry Redingbf836622012-01-02 01:15:39 +0000575
Tom Warren29f3e3f2012-09-04 17:00:24 -0700576 debug("tegra_mmc_getcd called\n");
Thierry Redingbf836622012-01-02 01:15:39 +0000577
Stephen Warrenf53c4e42016-09-13 10:45:46 -0600578 if (dm_gpio_is_valid(&priv->cd_gpio))
579 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingbf836622012-01-02 01:15:39 +0000580
581 return 1;
582}
583
Simon Glass0e513e72017-04-23 20:02:11 -0600584static const struct dm_mmc_ops tegra_mmc_ops = {
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200585 .send_cmd = tegra_mmc_send_cmd,
586 .set_ios = tegra_mmc_set_ios,
Simon Glass0e513e72017-04-23 20:02:11 -0600587 .get_cd = tegra_mmc_getcd,
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200588};
589
Tom Warren6a474db2016-09-13 10:45:48 -0600590static int tegra_mmc_probe(struct udevice *dev)
Tom Warren21ef6a12011-05-31 10:30:37 +0000591{
Tom Warren6a474db2016-09-13 10:45:48 -0600592 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass0e513e72017-04-23 20:02:11 -0600593 struct tegra_mmc_plat *plat = dev_get_platdata(dev);
Tom Warren6a474db2016-09-13 10:45:48 -0600594 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Simon Glass0e513e72017-04-23 20:02:11 -0600595 struct mmc_config *cfg = &plat->cfg;
Stephen Warrene8adca92016-09-13 10:46:01 -0600596 int bus_width, ret;
Tom Warren21ef6a12011-05-31 10:30:37 +0000597
Simon Glass0e513e72017-04-23 20:02:11 -0600598 cfg->name = dev->name;
Tom Warren21ef6a12011-05-31 10:30:37 +0000599
Simon Glass49cb9302017-07-25 08:30:08 -0600600 bus_width = dev_read_u32_default(dev, "bus-width", 1);
Tom Warren6a474db2016-09-13 10:45:48 -0600601
Simon Glass0e513e72017-04-23 20:02:11 -0600602 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
603 cfg->host_caps = 0;
Tom Warren6a474db2016-09-13 10:45:48 -0600604 if (bus_width == 8)
Simon Glass0e513e72017-04-23 20:02:11 -0600605 cfg->host_caps |= MMC_MODE_8BIT;
Tom Warren6a474db2016-09-13 10:45:48 -0600606 if (bus_width >= 4)
Simon Glass0e513e72017-04-23 20:02:11 -0600607 cfg->host_caps |= MMC_MODE_4BIT;
608 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren21ef6a12011-05-31 10:30:37 +0000609
610 /*
611 * min freq is for card identification, and is the highest
612 * low-speed SDIO card frequency (actually 400KHz)
613 * max freq is highest HS eMMC clock as per the SD/MMC spec
614 * (actually 52MHz)
Tom Warren21ef6a12011-05-31 10:30:37 +0000615 */
Simon Glass0e513e72017-04-23 20:02:11 -0600616 cfg->f_min = 375000;
617 cfg->f_max = 48000000;
Tom Warren21ef6a12011-05-31 10:30:37 +0000618
Simon Glass0e513e72017-04-23 20:02:11 -0600619 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200620
Simon Glass49cb9302017-07-25 08:30:08 -0600621 priv->reg = (void *)dev_read_addr(dev);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000622
Tom Warren6a474db2016-09-13 10:45:48 -0600623 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
624 if (ret) {
625 debug("reset_get_by_name() failed: %d\n", ret);
626 return ret;
Stephen Warrenc0493072016-08-05 16:10:33 -0600627 }
Tom Warren6a474db2016-09-13 10:45:48 -0600628 ret = clk_get_by_index(dev, 0, &priv->clk);
629 if (ret) {
630 debug("clk_get_by_index() failed: %d\n", ret);
631 return ret;
632 }
633
634 ret = reset_assert(&priv->reset_ctl);
635 if (ret)
636 return ret;
637 ret = clk_enable(&priv->clk);
638 if (ret)
639 return ret;
640 ret = clk_set_rate(&priv->clk, 20000000);
641 if (IS_ERR_VALUE(ret))
642 return ret;
643 ret = reset_deassert(&priv->reset_ctl);
644 if (ret)
645 return ret;
Tom Warrenc9aa8312013-02-21 12:31:30 +0000646
647 /* These GPIOs are optional */
Simon Glass49cb9302017-07-25 08:30:08 -0600648 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
649 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
650 gpio_request_by_name(dev, "power-gpios", 0, &priv->pwr_gpio,
651 GPIOD_IS_OUT);
Tom Warren6a474db2016-09-13 10:45:48 -0600652 if (dm_gpio_is_valid(&priv->pwr_gpio))
653 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000654
Simon Glass0e513e72017-04-23 20:02:11 -0600655 upriv->mmc = &plat->mmc;
Tom Warren6a474db2016-09-13 10:45:48 -0600656
Simon Glass0e513e72017-04-23 20:02:11 -0600657 return tegra_mmc_init(dev);
658}
Tom Warren6a474db2016-09-13 10:45:48 -0600659
Simon Glass0e513e72017-04-23 20:02:11 -0600660static int tegra_mmc_bind(struct udevice *dev)
661{
662 struct tegra_mmc_plat *plat = dev_get_platdata(dev);
663
664 return mmc_bind(dev, &plat->mmc, &plat->cfg);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000665}
666
Tom Warren6a474db2016-09-13 10:45:48 -0600667static const struct udevice_id tegra_mmc_ids[] = {
668 { .compatible = "nvidia,tegra20-sdhci" },
669 { .compatible = "nvidia,tegra30-sdhci" },
670 { .compatible = "nvidia,tegra114-sdhci" },
671 { .compatible = "nvidia,tegra124-sdhci" },
672 { .compatible = "nvidia,tegra210-sdhci" },
673 { .compatible = "nvidia,tegra186-sdhci" },
674 { }
675};
Tom Warrenc9aa8312013-02-21 12:31:30 +0000676
Tom Warren6a474db2016-09-13 10:45:48 -0600677U_BOOT_DRIVER(tegra_mmc_drv) = {
678 .name = "tegra_mmc",
679 .id = UCLASS_MMC,
680 .of_match = tegra_mmc_ids,
Simon Glass0e513e72017-04-23 20:02:11 -0600681 .bind = tegra_mmc_bind,
Tom Warren6a474db2016-09-13 10:45:48 -0600682 .probe = tegra_mmc_probe,
Simon Glass0e513e72017-04-23 20:02:11 -0600683 .ops = &tegra_mmc_ops,
684 .platdata_auto_alloc_size = sizeof(struct tegra_mmc_plat),
Tom Warren6a474db2016-09-13 10:45:48 -0600685 .priv_auto_alloc_size = sizeof(struct tegra_mmc_priv),
686};