blob: 7b9628adb96efc5a0ddb35e180a6643635c1ad6f [file] [log] [blame]
Tom Warren85f0ee42011-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 Warrena66f7722016-09-13 10:45:48 -06005 * Portions Copyright 2011-2016 NVIDIA Corporation
Tom Warren85f0ee42011-05-31 10:30:37 +00006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Tom Warren85f0ee42011-05-31 10:30:37 +00008 */
9
Stephen Warrenf227e452012-11-06 11:27:30 +000010#include <bouncebuf.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000011#include <common.h>
Stephen Warrend26e24d2016-08-05 16:10:33 -060012#include <dm/device.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090013#include <errno.h>
Stephen Warrenfba87542011-10-31 06:51:36 +000014#include <asm/gpio.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000015#include <asm/io.h>
Stephen Warrendfbdc122016-05-12 12:11:23 -060016#ifndef CONFIG_TEGRA186
Simon Glassc2ea5e42011-09-21 12:40:04 +000017#include <asm/arch/clock.h>
Tom Warrenab371962012-09-19 15:50:56 -070018#include <asm/arch-tegra/clk_rst.h>
Stephen Warrendfbdc122016-05-12 12:11:23 -060019#endif
Tom Warrenab371962012-09-19 15:50:56 -070020#include <asm/arch-tegra/tegra_mmc.h>
21#include <mmc.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000022
Stephen Warrend26e24d2016-08-05 16:10:33 -060023/*
24 * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that
25 * should not be present. These are needed because newer Tegra SoCs support
26 * only the standard clock/reset APIs, whereas older Tegra SoCs support only
27 * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be
28 * fixed to implement the standard APIs, and all drivers converted to solely
29 * use the new standard APIs, with no ifdefs.
30 */
31
Tom Warren9745cf82013-02-21 12:31:30 +000032DECLARE_GLOBAL_DATA_PTR;
Tom Warren85f0ee42011-05-31 10:30:37 +000033
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060034struct tegra_mmc_priv {
35 struct tegra_mmc *reg;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060036#ifdef CONFIG_TEGRA186
37 struct reset_ctl reset_ctl;
38 struct clk clk;
39#else
40 enum periph_id mmc_id; /* Peripheral ID: PERIPH_ID_... */
41#endif
42 struct gpio_desc cd_gpio; /* Change Detect GPIO */
43 struct gpio_desc pwr_gpio; /* Power GPIO */
44 struct gpio_desc wp_gpio; /* Write Protect GPIO */
45 unsigned int version; /* SDHCI spec. version */
46 unsigned int clock; /* Current clock (MHz) */
47 struct mmc_config cfg; /* mmc configuration */
Tom Warrena66f7722016-09-13 10:45:48 -060048 struct mmc *mmc;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060049};
50
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060051static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
52 unsigned short power)
Tom Warren35ae07b2013-02-26 12:31:26 -070053{
54 u8 pwr = 0;
55 debug("%s: power = %x\n", __func__, power);
56
57 if (power != (unsigned short)-1) {
58 switch (1 << power) {
59 case MMC_VDD_165_195:
60 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
61 break;
62 case MMC_VDD_29_30:
63 case MMC_VDD_30_31:
64 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
65 break;
66 case MMC_VDD_32_33:
67 case MMC_VDD_33_34:
68 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
69 break;
70 }
71 }
72 debug("%s: pwr = %X\n", __func__, pwr);
73
74 /* Set the bus voltage first (if any) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060075 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070076 if (pwr == 0)
77 return;
78
79 /* Now enable bus power */
80 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060081 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070082}
83
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060084static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
85 struct mmc_data *data,
86 struct bounce_buffer *bbstate)
Tom Warren85f0ee42011-05-31 10:30:37 +000087{
88 unsigned char ctrl;
89
Tom Warren85f0ee42011-05-31 10:30:37 +000090
Stephen Warrenf227e452012-11-06 11:27:30 +000091 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
92 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
93 data->blocksize);
94
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060095 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren85f0ee42011-05-31 10:30:37 +000096 /*
97 * DMASEL[4:3]
98 * 00 = Selects SDMA
99 * 01 = Reserved
100 * 10 = Selects 32-bit Address ADMA2
101 * 11 = Selects 64-bit Address ADMA2
102 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600103 ctrl = readb(&priv->reg->hostctl);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000104 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
105 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600106 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000107
108 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600109 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
110 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren85f0ee42011-05-31 10:30:37 +0000111}
112
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600113static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
114 struct mmc_data *data)
Tom Warren85f0ee42011-05-31 10:30:37 +0000115{
116 unsigned short mode;
117 debug(" mmc_set_transfer_mode called\n");
118 /*
119 * TRNMOD
120 * MUL1SIN0[5] : Multi/Single Block Select
121 * RD1WT0[4] : Data Transfer Direction Select
122 * 1 = read
123 * 0 = write
124 * ENACMD12[2] : Auto CMD12 Enable
125 * ENBLKCNT[1] : Block Count Enable
126 * ENDMA[0] : DMA Enable
127 */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000128 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
129 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
130
Tom Warren85f0ee42011-05-31 10:30:37 +0000131 if (data->blocks > 1)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000132 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
133
Tom Warren85f0ee42011-05-31 10:30:37 +0000134 if (data->flags & MMC_DATA_READ)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000135 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren85f0ee42011-05-31 10:30:37 +0000136
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600137 writew(mode, &priv->reg->trnmod);
Tom Warren85f0ee42011-05-31 10:30:37 +0000138}
139
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600140static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
141 struct mmc_cmd *cmd,
142 struct mmc_data *data,
143 unsigned int timeout)
Tom Warren85f0ee42011-05-31 10:30:37 +0000144{
Tom Warren85f0ee42011-05-31 10:30:37 +0000145 /*
146 * PRNSTS
Anton staaf5ab3fba2011-11-10 11:56:52 +0000147 * CMDINHDAT[1] : Command Inhibit (DAT)
148 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren85f0ee42011-05-31 10:30:37 +0000149 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000150 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren85f0ee42011-05-31 10:30:37 +0000151
152 /*
153 * We shouldn't wait for data inhibit for stop commands, even
154 * though they might use busy signaling
155 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000156 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
157 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000158
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600159 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000160 if (timeout == 0) {
161 printf("%s: timeout error\n", __func__);
162 return -1;
163 }
164 timeout--;
165 udelay(1000);
166 }
167
Anton staaf5ab3fba2011-11-10 11:56:52 +0000168 return 0;
169}
170
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600171static int tegra_mmc_send_cmd_bounced(struct mmc *mmc, struct mmc_cmd *cmd,
172 struct mmc_data *data,
173 struct bounce_buffer *bbstate)
Anton staaf5ab3fba2011-11-10 11:56:52 +0000174{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600175 struct tegra_mmc_priv *priv = mmc->priv;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000176 int flags, i;
177 int result;
Anatolij Gustschine1f53412012-03-28 03:40:00 +0000178 unsigned int mask = 0;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000179 unsigned int retry = 0x100000;
180 debug(" mmc_send_cmd called\n");
181
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600182 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000183
184 if (result < 0)
185 return result;
186
Tom Warren85f0ee42011-05-31 10:30:37 +0000187 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600188 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren85f0ee42011-05-31 10:30:37 +0000189
190 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600191 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren85f0ee42011-05-31 10:30:37 +0000192
193 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600194 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren85f0ee42011-05-31 10:30:37 +0000195
196 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
197 return -1;
198
199 /*
200 * CMDREG
201 * CMDIDX[13:8] : Command index
202 * DATAPRNT[5] : Data Present Select
203 * ENCMDIDX[4] : Command Index Check Enable
204 * ENCMDCRC[3] : Command CRC Check Enable
205 * RSPTYP[1:0]
206 * 00 = No Response
207 * 01 = Length 136
208 * 10 = Length 48
209 * 11 = Length 48 Check busy after response
210 */
211 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf0dfb31c2011-11-10 11:56:49 +0000212 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren85f0ee42011-05-31 10:30:37 +0000213 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000214 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren85f0ee42011-05-31 10:30:37 +0000215 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000216 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren85f0ee42011-05-31 10:30:37 +0000217 else
Anton staaf0dfb31c2011-11-10 11:56:49 +0000218 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren85f0ee42011-05-31 10:30:37 +0000219
220 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000221 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000222 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000223 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000224 if (data)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000225 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren85f0ee42011-05-31 10:30:37 +0000226
227 debug("cmd: %d\n", cmd->cmdidx);
228
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600229 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren85f0ee42011-05-31 10:30:37 +0000230
231 for (i = 0; i < retry; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600232 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000233 /* Command Complete */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000234 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000235 if (!data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600236 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000237 break;
238 }
239 }
240
241 if (i == retry) {
242 printf("%s: waiting for status update\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600243 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900244 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000245 }
246
Anton staaf0dfb31c2011-11-10 11:56:49 +0000247 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000248 /* Timeout Error */
249 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600250 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900251 return -ETIMEDOUT;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000252 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000253 /* Error Interrupt */
254 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600255 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000256 return -1;
257 }
258
259 if (cmd->resp_type & MMC_RSP_PRESENT) {
260 if (cmd->resp_type & MMC_RSP_136) {
261 /* CRC is stripped so we need to do some shifting. */
262 for (i = 0; i < 4; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600263 unsigned long offset = (unsigned long)
264 (&priv->reg->rspreg3 - i);
Tom Warren85f0ee42011-05-31 10:30:37 +0000265 cmd->response[i] = readl(offset) << 8;
266
267 if (i != 3) {
268 cmd->response[i] |=
269 readb(offset - 1);
270 }
271 debug("cmd->resp[%d]: %08x\n",
272 i, cmd->response[i]);
273 }
274 } else if (cmd->resp_type & MMC_RSP_BUSY) {
275 for (i = 0; i < retry; i++) {
276 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600277 if (readl(&priv->reg->prnsts)
Tom Warren85f0ee42011-05-31 10:30:37 +0000278 & (1 << 20)) /* DAT[0] */
279 break;
280 }
281
282 if (i == retry) {
283 printf("%s: card is still busy\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600284 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900285 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000286 }
287
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600288 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000289 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
290 } else {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600291 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000292 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
293 }
294 }
295
296 if (data) {
Anton staafbd348422011-11-10 11:56:51 +0000297 unsigned long start = get_timer(0);
298
Tom Warren85f0ee42011-05-31 10:30:37 +0000299 while (1) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600300 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000301
Anton staaf0dfb31c2011-11-10 11:56:49 +0000302 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000303 /* Error Interrupt */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600304 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000305 printf("%s: error during transfer: 0x%08x\n",
306 __func__, mask);
307 return -1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000308 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf3ade2102011-11-10 11:56:50 +0000309 /*
310 * DMA Interrupt, restart the transfer where
311 * it was interrupted.
312 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600313 unsigned int address = readl(&priv->reg->sysad);
Anton staaf3ade2102011-11-10 11:56:50 +0000314
Tom Warren85f0ee42011-05-31 10:30:37 +0000315 debug("DMA end\n");
Anton staaf3ade2102011-11-10 11:56:50 +0000316 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600317 &priv->reg->norintsts);
318 writel(address, &priv->reg->sysad);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000319 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000320 /* Transfer Complete */
321 debug("r/w is done\n");
322 break;
Marcel Ziswilere1207e92014-10-04 01:48:53 +0200323 } else if (get_timer(start) > 8000UL) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600324 writel(mask, &priv->reg->norintsts);
Anton staafbd348422011-11-10 11:56:51 +0000325 printf("%s: MMC Timeout\n"
326 " Interrupt status 0x%08x\n"
327 " Interrupt status enable 0x%08x\n"
328 " Interrupt signal enable 0x%08x\n"
329 " Present status 0x%08x\n",
330 __func__, mask,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600331 readl(&priv->reg->norintstsen),
332 readl(&priv->reg->norintsigen),
333 readl(&priv->reg->prnsts));
Anton staafbd348422011-11-10 11:56:51 +0000334 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000335 }
336 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600337 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000338 }
339
340 udelay(1000);
341 return 0;
342}
343
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200344static int tegra_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600345 struct mmc_data *data)
Stephen Warrenf227e452012-11-06 11:27:30 +0000346{
347 void *buf;
348 unsigned int bbflags;
349 size_t len;
350 struct bounce_buffer bbstate;
351 int ret;
352
353 if (data) {
354 if (data->flags & MMC_DATA_READ) {
355 buf = data->dest;
356 bbflags = GEN_BB_WRITE;
357 } else {
358 buf = (void *)data->src;
359 bbflags = GEN_BB_READ;
360 }
361 len = data->blocks * data->blocksize;
362
363 bounce_buffer_start(&bbstate, buf, len, bbflags);
364 }
365
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600366 ret = tegra_mmc_send_cmd_bounced(mmc, cmd, data, &bbstate);
Stephen Warrenf227e452012-11-06 11:27:30 +0000367
368 if (data)
369 bounce_buffer_stop(&bbstate);
370
371 return ret;
372}
373
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600374static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren85f0ee42011-05-31 10:30:37 +0000375{
Simon Glassc2ea5e42011-09-21 12:40:04 +0000376 int div;
Tom Warren85f0ee42011-05-31 10:30:37 +0000377 unsigned short clk;
378 unsigned long timeout;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000379
Tom Warren85f0ee42011-05-31 10:30:37 +0000380 debug(" mmc_change_clock called\n");
381
Simon Glassc2ea5e42011-09-21 12:40:04 +0000382 /*
Tom Warren35ae07b2013-02-26 12:31:26 -0700383 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glassc2ea5e42011-09-21 12:40:04 +0000384 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000385 if (clock == 0)
386 goto out;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600387#ifdef CONFIG_TEGRA186
388 {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600389 ulong rate = clk_set_rate(&priv->clk, clock);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600390 div = (rate + clock - 1) / clock;
391 }
392#else
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600393 clock_adjust_periph_pll_div(priv->mmc_id, CLOCK_ID_PERIPH, clock,
Simon Glassc2ea5e42011-09-21 12:40:04 +0000394 &div);
Stephen Warrendfbdc122016-05-12 12:11:23 -0600395#endif
Simon Glassc2ea5e42011-09-21 12:40:04 +0000396 debug("div = %d\n", div);
Tom Warren85f0ee42011-05-31 10:30:37 +0000397
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600398 writew(0, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000399
Tom Warren85f0ee42011-05-31 10:30:37 +0000400 /*
401 * CLKCON
402 * SELFREQ[15:8] : base clock divided by value
403 * ENSDCLK[2] : SD Clock Enable
404 * STBLINTCLK[1] : Internal Clock Stable
405 * ENINTCLK[0] : Internal Clock Enable
406 */
Simon Glassc2ea5e42011-09-21 12:40:04 +0000407 div >>= 1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000408 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
409 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600410 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000411
412 /* Wait max 10 ms */
413 timeout = 10;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600414 while (!(readw(&priv->reg->clkcon) &
Anton staaf0dfb31c2011-11-10 11:56:49 +0000415 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000416 if (timeout == 0) {
417 printf("%s: timeout error\n", __func__);
418 return;
419 }
420 timeout--;
421 udelay(1000);
422 }
423
Anton staaf0dfb31c2011-11-10 11:56:49 +0000424 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600425 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000426
427 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren85f0ee42011-05-31 10:30:37 +0000428
429out:
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600430 priv->clock = clock;
Tom Warren85f0ee42011-05-31 10:30:37 +0000431}
432
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200433static void tegra_mmc_set_ios(struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000434{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600435 struct tegra_mmc_priv *priv = mmc->priv;
Tom Warren85f0ee42011-05-31 10:30:37 +0000436 unsigned char ctrl;
437 debug(" mmc_set_ios called\n");
438
439 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
440
441 /* Change clock first */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600442 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren85f0ee42011-05-31 10:30:37 +0000443
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600444 ctrl = readb(&priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000445
446 /*
447 * WIDE8[5]
448 * 0 = Depend on WIDE4
449 * 1 = 8-bit mode
450 * WIDE4[1]
451 * 1 = 4-bit mode
452 * 0 = 1-bit mode
453 */
454 if (mmc->bus_width == 8)
455 ctrl |= (1 << 5);
456 else if (mmc->bus_width == 4)
457 ctrl |= (1 << 1);
458 else
459 ctrl &= ~(1 << 1);
460
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600461 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000462 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
463}
464
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600465static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600466{
467#if defined(CONFIG_TEGRA30)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600468 u32 val;
469
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600470 debug("%s: sdmmc address = %08x\n", __func__, (unsigned int)priv->reg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600471
472 /* Set the pad drive strength for SDMMC1 or 3 only */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600473 if (priv->reg != (void *)0x78000000 &&
474 priv->reg != (void *)0x78000400) {
Stephen Warrenc76e9362016-09-13 10:45:44 -0600475 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
476 __func__);
477 return;
478 }
479
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600480 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600481 val &= 0xFFFFFFF0;
482 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600483 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600484
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600485 val = readl(&priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600486 val &= 0xFFFF0000;
487 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET | AUTO_CAL_ENABLED;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600488 writel(val, &priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600489#endif
490}
491
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600492static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000493{
494 unsigned int timeout;
495 debug(" mmc_reset called\n");
496
497 /*
498 * RSTALL[0] : Software reset for all
499 * 1 = reset
500 * 0 = work
501 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600502 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren85f0ee42011-05-31 10:30:37 +0000503
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600504 priv->clock = 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000505
506 /* Wait max 100 ms */
507 timeout = 100;
508
509 /* hw clears the bit when it's done */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600510 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000511 if (timeout == 0) {
512 printf("%s: timeout error\n", __func__);
513 return;
514 }
515 timeout--;
516 udelay(1000);
517 }
Tom Warren35ae07b2013-02-26 12:31:26 -0700518
519 /* Set SD bus voltage & enable bus power */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600520 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren35ae07b2013-02-26 12:31:26 -0700521 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600522 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren35ae07b2013-02-26 12:31:26 -0700523
524 /* Make sure SDIO pads are set up */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600525 tegra_mmc_pad_init(priv);
Tom Warren85f0ee42011-05-31 10:30:37 +0000526}
527
Tom Warrena66f7722016-09-13 10:45:48 -0600528static int tegra_mmc_init(struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000529{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600530 struct tegra_mmc_priv *priv = mmc->priv;
Tom Warren85f0ee42011-05-31 10:30:37 +0000531 unsigned int mask;
Tom Warrena66f7722016-09-13 10:45:48 -0600532 debug(" tegra_mmc_init called\n");
Tom Warren85f0ee42011-05-31 10:30:37 +0000533
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600534 tegra_mmc_reset(priv, mmc);
Tom Warren85f0ee42011-05-31 10:30:37 +0000535
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600536 priv->version = readw(&priv->reg->hcver);
537 debug("host version = %x\n", priv->version);
Tom Warren85f0ee42011-05-31 10:30:37 +0000538
539 /* mask all */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600540 writel(0xffffffff, &priv->reg->norintstsen);
541 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000542
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600543 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000544 /*
545 * NORMAL Interrupt Status Enable Register init
546 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
547 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf3ade2102011-11-10 11:56:50 +0000548 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren85f0ee42011-05-31 10:30:37 +0000549 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
550 * [0] ENSTACMDCMPLT : Command Complete Status Enable
551 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600552 mask = readl(&priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000553 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000554 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
555 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf3ade2102011-11-10 11:56:50 +0000556 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf0dfb31c2011-11-10 11:56:49 +0000557 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
558 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600559 writel(mask, &priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000560
561 /*
562 * NORMAL Interrupt Signal Enable Register init
563 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
564 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600565 mask = readl(&priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000566 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000567 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600568 writel(mask, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000569
570 return 0;
571}
572
Jeroen Hofstee93dfae72014-10-08 22:57:46 +0200573static int tegra_mmc_getcd(struct mmc *mmc)
Thierry Redingf1494112012-01-02 01:15:39 +0000574{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600575 struct tegra_mmc_priv *priv = mmc->priv;
Thierry Redingf1494112012-01-02 01:15:39 +0000576
Tom Warren22562a42012-09-04 17:00:24 -0700577 debug("tegra_mmc_getcd called\n");
Thierry Redingf1494112012-01-02 01:15:39 +0000578
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600579 if (dm_gpio_is_valid(&priv->cd_gpio))
580 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingf1494112012-01-02 01:15:39 +0000581
582 return 1;
583}
584
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200585static const struct mmc_ops tegra_mmc_ops = {
586 .send_cmd = tegra_mmc_send_cmd,
587 .set_ios = tegra_mmc_set_ios,
Tom Warrena66f7722016-09-13 10:45:48 -0600588 .init = tegra_mmc_init,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200589 .getcd = tegra_mmc_getcd,
590};
591
Tom Warrena66f7722016-09-13 10:45:48 -0600592static int tegra_mmc_probe(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000593{
Tom Warrena66f7722016-09-13 10:45:48 -0600594 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
595 struct tegra_mmc_priv *priv = dev_get_priv(dev);
596 int bus_width;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600597#ifdef CONFIG_TEGRA186
598 int ret;
599#endif
Tom Warren85f0ee42011-05-31 10:30:37 +0000600
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600601 priv->cfg.name = "Tegra SD/MMC";
602 priv->cfg.ops = &tegra_mmc_ops;
Tom Warren85f0ee42011-05-31 10:30:37 +0000603
Tom Warrena66f7722016-09-13 10:45:48 -0600604 bus_width = fdtdec_get_int(gd->fdt_blob, dev->of_offset, "bus-width",
605 1);
606
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600607 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
608 priv->cfg.host_caps = 0;
Tom Warrena66f7722016-09-13 10:45:48 -0600609 if (bus_width == 8)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600610 priv->cfg.host_caps |= MMC_MODE_8BIT;
Tom Warrena66f7722016-09-13 10:45:48 -0600611 if (bus_width >= 4)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600612 priv->cfg.host_caps |= MMC_MODE_4BIT;
613 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren85f0ee42011-05-31 10:30:37 +0000614
615 /*
616 * min freq is for card identification, and is the highest
617 * low-speed SDIO card frequency (actually 400KHz)
618 * max freq is highest HS eMMC clock as per the SD/MMC spec
619 * (actually 52MHz)
Tom Warren85f0ee42011-05-31 10:30:37 +0000620 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600621 priv->cfg.f_min = 375000;
622 priv->cfg.f_max = 48000000;
Tom Warren85f0ee42011-05-31 10:30:37 +0000623
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600624 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200625
Tom Warrena66f7722016-09-13 10:45:48 -0600626 priv->reg = (void *)dev_get_addr(dev);
Tom Warren9745cf82013-02-21 12:31:30 +0000627
Stephen Warrend26e24d2016-08-05 16:10:33 -0600628#ifdef CONFIG_TEGRA186
Tom Warrena66f7722016-09-13 10:45:48 -0600629 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
630 if (ret) {
631 debug("reset_get_by_name() failed: %d\n", ret);
632 return ret;
633 }
634 ret = clk_get_by_index(dev, 0, &priv->clk);
635 if (ret) {
636 debug("clk_get_by_index() failed: %d\n", ret);
637 return ret;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600638 }
Tom Warrena66f7722016-09-13 10:45:48 -0600639
640 ret = reset_assert(&priv->reset_ctl);
641 if (ret)
642 return ret;
643 ret = clk_enable(&priv->clk);
644 if (ret)
645 return ret;
646 ret = clk_set_rate(&priv->clk, 20000000);
647 if (IS_ERR_VALUE(ret))
648 return ret;
649 ret = reset_deassert(&priv->reset_ctl);
650 if (ret)
651 return ret;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600652#else
Tom Warrena66f7722016-09-13 10:45:48 -0600653 priv->mmc_id = clock_decode_periph_id(gd->fdt_blob, dev->of_offset);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600654 if (priv->mmc_id == PERIPH_ID_NONE) {
Tom Warren9745cf82013-02-21 12:31:30 +0000655 debug("%s: could not decode periph id\n", __func__);
656 return -FDT_ERR_NOTFOUND;
657 }
Tom Warren9745cf82013-02-21 12:31:30 +0000658
Tom Warrena66f7722016-09-13 10:45:48 -0600659 clock_start_periph_pll(priv->mmc_id, CLOCK_ID_PERIPH, 20000000);
Stephen Warrendfbdc122016-05-12 12:11:23 -0600660#endif
Tom Warren9745cf82013-02-21 12:31:30 +0000661
Tom Warrena66f7722016-09-13 10:45:48 -0600662 /* These GPIOs are optional */
663 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
664 GPIOD_IS_IN);
665 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
666 GPIOD_IS_IN);
667 gpio_request_by_name(dev, "power-gpios", 0,
668 &priv->pwr_gpio, GPIOD_IS_OUT);
669 if (dm_gpio_is_valid(&priv->pwr_gpio))
670 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warren9745cf82013-02-21 12:31:30 +0000671
Tom Warrena66f7722016-09-13 10:45:48 -0600672 priv->mmc = mmc_create(&priv->cfg, priv);
673 if (priv->mmc == NULL)
674 return -1;
Tom Warren9745cf82013-02-21 12:31:30 +0000675
Tom Warrena66f7722016-09-13 10:45:48 -0600676 priv->mmc->dev = dev;
677 upriv->mmc = priv->mmc;
Tom Warren9745cf82013-02-21 12:31:30 +0000678
Tom Warren9745cf82013-02-21 12:31:30 +0000679 return 0;
680}
681
Tom Warrena66f7722016-09-13 10:45:48 -0600682static const struct udevice_id tegra_mmc_ids[] = {
683 { .compatible = "nvidia,tegra20-sdhci" },
684 { .compatible = "nvidia,tegra30-sdhci" },
685 { .compatible = "nvidia,tegra114-sdhci" },
686 { .compatible = "nvidia,tegra124-sdhci" },
687 { .compatible = "nvidia,tegra210-sdhci" },
688 { .compatible = "nvidia,tegra186-sdhci" },
689 { }
690};
Tom Warren9745cf82013-02-21 12:31:30 +0000691
Tom Warrena66f7722016-09-13 10:45:48 -0600692U_BOOT_DRIVER(tegra_mmc_drv) = {
693 .name = "tegra_mmc",
694 .id = UCLASS_MMC,
695 .of_match = tegra_mmc_ids,
696 .probe = tegra_mmc_probe,
697 .priv_auto_alloc_size = sizeof(struct tegra_mmc_priv),
698};