blob: 7d945a172e8a641c68bcf386c7ad2ab0a6eb8f21 [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>
Simon Glass11c89f32017-05-17 17:18:03 -060012#include <dm.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>
Tom Warrenab371962012-09-19 15:50:56 -070016#include <asm/arch-tegra/tegra_mmc.h>
17#include <mmc.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000018
Tom Warren9745cf82013-02-21 12:31:30 +000019DECLARE_GLOBAL_DATA_PTR;
Tom Warren85f0ee42011-05-31 10:30:37 +000020
Simon Glass8c4c5c82017-04-23 20:02:11 -060021struct tegra_mmc_plat {
22 struct mmc_config cfg;
23 struct mmc mmc;
24};
25
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060026struct tegra_mmc_priv {
27 struct tegra_mmc *reg;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060028 struct reset_ctl reset_ctl;
29 struct clk clk;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060030 struct gpio_desc cd_gpio; /* Change Detect GPIO */
31 struct gpio_desc pwr_gpio; /* Power GPIO */
32 struct gpio_desc wp_gpio; /* Write Protect GPIO */
33 unsigned int version; /* SDHCI spec. version */
34 unsigned int clock; /* Current clock (MHz) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060035};
36
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060037static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
38 unsigned short power)
Tom Warren35ae07b2013-02-26 12:31:26 -070039{
40 u8 pwr = 0;
41 debug("%s: power = %x\n", __func__, power);
42
43 if (power != (unsigned short)-1) {
44 switch (1 << power) {
45 case MMC_VDD_165_195:
46 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
47 break;
48 case MMC_VDD_29_30:
49 case MMC_VDD_30_31:
50 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
51 break;
52 case MMC_VDD_32_33:
53 case MMC_VDD_33_34:
54 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
55 break;
56 }
57 }
58 debug("%s: pwr = %X\n", __func__, pwr);
59
60 /* Set the bus voltage first (if any) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060061 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070062 if (pwr == 0)
63 return;
64
65 /* Now enable bus power */
66 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060067 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070068}
69
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060070static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
71 struct mmc_data *data,
72 struct bounce_buffer *bbstate)
Tom Warren85f0ee42011-05-31 10:30:37 +000073{
74 unsigned char ctrl;
75
Tom Warren85f0ee42011-05-31 10:30:37 +000076
Stephen Warrenf227e452012-11-06 11:27:30 +000077 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
78 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
79 data->blocksize);
80
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060081 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren85f0ee42011-05-31 10:30:37 +000082 /*
83 * DMASEL[4:3]
84 * 00 = Selects SDMA
85 * 01 = Reserved
86 * 10 = Selects 32-bit Address ADMA2
87 * 11 = Selects 64-bit Address ADMA2
88 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060089 ctrl = readb(&priv->reg->hostctl);
Anton staaf0dfb31c2011-11-10 11:56:49 +000090 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
91 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060092 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +000093
94 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060095 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
96 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren85f0ee42011-05-31 10:30:37 +000097}
98
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060099static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
100 struct mmc_data *data)
Tom Warren85f0ee42011-05-31 10:30:37 +0000101{
102 unsigned short mode;
103 debug(" mmc_set_transfer_mode called\n");
104 /*
105 * TRNMOD
106 * MUL1SIN0[5] : Multi/Single Block Select
107 * RD1WT0[4] : Data Transfer Direction Select
108 * 1 = read
109 * 0 = write
110 * ENACMD12[2] : Auto CMD12 Enable
111 * ENBLKCNT[1] : Block Count Enable
112 * ENDMA[0] : DMA Enable
113 */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000114 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
115 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
116
Tom Warren85f0ee42011-05-31 10:30:37 +0000117 if (data->blocks > 1)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000118 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
119
Tom Warren85f0ee42011-05-31 10:30:37 +0000120 if (data->flags & MMC_DATA_READ)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000121 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren85f0ee42011-05-31 10:30:37 +0000122
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600123 writew(mode, &priv->reg->trnmod);
Tom Warren85f0ee42011-05-31 10:30:37 +0000124}
125
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600126static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
127 struct mmc_cmd *cmd,
128 struct mmc_data *data,
129 unsigned int timeout)
Tom Warren85f0ee42011-05-31 10:30:37 +0000130{
Tom Warren85f0ee42011-05-31 10:30:37 +0000131 /*
132 * PRNSTS
Anton staaf5ab3fba2011-11-10 11:56:52 +0000133 * CMDINHDAT[1] : Command Inhibit (DAT)
134 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren85f0ee42011-05-31 10:30:37 +0000135 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000136 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren85f0ee42011-05-31 10:30:37 +0000137
138 /*
139 * We shouldn't wait for data inhibit for stop commands, even
140 * though they might use busy signaling
141 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000142 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
143 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000144
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600145 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000146 if (timeout == 0) {
147 printf("%s: timeout error\n", __func__);
148 return -1;
149 }
150 timeout--;
151 udelay(1000);
152 }
153
Anton staaf5ab3fba2011-11-10 11:56:52 +0000154 return 0;
155}
156
Simon Glass8c4c5c82017-04-23 20:02:11 -0600157static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600158 struct mmc_data *data,
159 struct bounce_buffer *bbstate)
Anton staaf5ab3fba2011-11-10 11:56:52 +0000160{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600161 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000162 int flags, i;
163 int result;
Anatolij Gustschine1f53412012-03-28 03:40:00 +0000164 unsigned int mask = 0;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000165 unsigned int retry = 0x100000;
166 debug(" mmc_send_cmd called\n");
167
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600168 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000169
170 if (result < 0)
171 return result;
172
Tom Warren85f0ee42011-05-31 10:30:37 +0000173 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600174 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren85f0ee42011-05-31 10:30:37 +0000175
176 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600177 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren85f0ee42011-05-31 10:30:37 +0000178
179 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600180 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren85f0ee42011-05-31 10:30:37 +0000181
182 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
183 return -1;
184
185 /*
186 * CMDREG
187 * CMDIDX[13:8] : Command index
188 * DATAPRNT[5] : Data Present Select
189 * ENCMDIDX[4] : Command Index Check Enable
190 * ENCMDCRC[3] : Command CRC Check Enable
191 * RSPTYP[1:0]
192 * 00 = No Response
193 * 01 = Length 136
194 * 10 = Length 48
195 * 11 = Length 48 Check busy after response
196 */
197 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf0dfb31c2011-11-10 11:56:49 +0000198 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren85f0ee42011-05-31 10:30:37 +0000199 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000200 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren85f0ee42011-05-31 10:30:37 +0000201 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000202 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren85f0ee42011-05-31 10:30:37 +0000203 else
Anton staaf0dfb31c2011-11-10 11:56:49 +0000204 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren85f0ee42011-05-31 10:30:37 +0000205
206 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000207 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000208 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000209 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000210 if (data)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000211 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren85f0ee42011-05-31 10:30:37 +0000212
213 debug("cmd: %d\n", cmd->cmdidx);
214
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600215 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren85f0ee42011-05-31 10:30:37 +0000216
217 for (i = 0; i < retry; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600218 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000219 /* Command Complete */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000220 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000221 if (!data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600222 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000223 break;
224 }
225 }
226
227 if (i == retry) {
228 printf("%s: waiting for status update\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600229 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900230 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000231 }
232
Anton staaf0dfb31c2011-11-10 11:56:49 +0000233 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000234 /* Timeout Error */
235 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600236 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900237 return -ETIMEDOUT;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000238 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000239 /* Error Interrupt */
240 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600241 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000242 return -1;
243 }
244
245 if (cmd->resp_type & MMC_RSP_PRESENT) {
246 if (cmd->resp_type & MMC_RSP_136) {
247 /* CRC is stripped so we need to do some shifting. */
248 for (i = 0; i < 4; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600249 unsigned long offset = (unsigned long)
250 (&priv->reg->rspreg3 - i);
Tom Warren85f0ee42011-05-31 10:30:37 +0000251 cmd->response[i] = readl(offset) << 8;
252
253 if (i != 3) {
254 cmd->response[i] |=
255 readb(offset - 1);
256 }
257 debug("cmd->resp[%d]: %08x\n",
258 i, cmd->response[i]);
259 }
260 } else if (cmd->resp_type & MMC_RSP_BUSY) {
261 for (i = 0; i < retry; i++) {
262 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600263 if (readl(&priv->reg->prnsts)
Tom Warren85f0ee42011-05-31 10:30:37 +0000264 & (1 << 20)) /* DAT[0] */
265 break;
266 }
267
268 if (i == retry) {
269 printf("%s: card is still busy\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600270 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900271 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000272 }
273
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600274 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000275 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
276 } else {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600277 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000278 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
279 }
280 }
281
282 if (data) {
Anton staafbd348422011-11-10 11:56:51 +0000283 unsigned long start = get_timer(0);
284
Tom Warren85f0ee42011-05-31 10:30:37 +0000285 while (1) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600286 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000287
Anton staaf0dfb31c2011-11-10 11:56:49 +0000288 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000289 /* Error Interrupt */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600290 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000291 printf("%s: error during transfer: 0x%08x\n",
292 __func__, mask);
293 return -1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000294 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf3ade2102011-11-10 11:56:50 +0000295 /*
296 * DMA Interrupt, restart the transfer where
297 * it was interrupted.
298 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600299 unsigned int address = readl(&priv->reg->sysad);
Anton staaf3ade2102011-11-10 11:56:50 +0000300
Tom Warren85f0ee42011-05-31 10:30:37 +0000301 debug("DMA end\n");
Anton staaf3ade2102011-11-10 11:56:50 +0000302 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600303 &priv->reg->norintsts);
304 writel(address, &priv->reg->sysad);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000305 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000306 /* Transfer Complete */
307 debug("r/w is done\n");
308 break;
Marcel Ziswilere1207e92014-10-04 01:48:53 +0200309 } else if (get_timer(start) > 8000UL) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600310 writel(mask, &priv->reg->norintsts);
Anton staafbd348422011-11-10 11:56:51 +0000311 printf("%s: MMC Timeout\n"
312 " Interrupt status 0x%08x\n"
313 " Interrupt status enable 0x%08x\n"
314 " Interrupt signal enable 0x%08x\n"
315 " Present status 0x%08x\n",
316 __func__, mask,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600317 readl(&priv->reg->norintstsen),
318 readl(&priv->reg->norintsigen),
319 readl(&priv->reg->prnsts));
Anton staafbd348422011-11-10 11:56:51 +0000320 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000321 }
322 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600323 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000324 }
325
326 udelay(1000);
327 return 0;
328}
329
Simon Glass8c4c5c82017-04-23 20:02:11 -0600330static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600331 struct mmc_data *data)
Stephen Warrenf227e452012-11-06 11:27:30 +0000332{
333 void *buf;
334 unsigned int bbflags;
335 size_t len;
336 struct bounce_buffer bbstate;
337 int ret;
338
339 if (data) {
340 if (data->flags & MMC_DATA_READ) {
341 buf = data->dest;
342 bbflags = GEN_BB_WRITE;
343 } else {
344 buf = (void *)data->src;
345 bbflags = GEN_BB_READ;
346 }
347 len = data->blocks * data->blocksize;
348
349 bounce_buffer_start(&bbstate, buf, len, bbflags);
350 }
351
Simon Glass8c4c5c82017-04-23 20:02:11 -0600352 ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate);
Stephen Warrenf227e452012-11-06 11:27:30 +0000353
354 if (data)
355 bounce_buffer_stop(&bbstate);
356
357 return ret;
358}
359
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600360static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren85f0ee42011-05-31 10:30:37 +0000361{
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600362 ulong rate;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000363 int div;
Tom Warren85f0ee42011-05-31 10:30:37 +0000364 unsigned short clk;
365 unsigned long timeout;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000366
Tom Warren85f0ee42011-05-31 10:30:37 +0000367 debug(" mmc_change_clock called\n");
368
Simon Glassc2ea5e42011-09-21 12:40:04 +0000369 /*
Tom Warren35ae07b2013-02-26 12:31:26 -0700370 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glassc2ea5e42011-09-21 12:40:04 +0000371 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000372 if (clock == 0)
373 goto out;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600374
375 rate = clk_set_rate(&priv->clk, clock);
376 div = (rate + clock - 1) / clock;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000377 debug("div = %d\n", div);
Tom Warren85f0ee42011-05-31 10:30:37 +0000378
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600379 writew(0, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000380
Tom Warren85f0ee42011-05-31 10:30:37 +0000381 /*
382 * CLKCON
383 * SELFREQ[15:8] : base clock divided by value
384 * ENSDCLK[2] : SD Clock Enable
385 * STBLINTCLK[1] : Internal Clock Stable
386 * ENINTCLK[0] : Internal Clock Enable
387 */
Simon Glassc2ea5e42011-09-21 12:40:04 +0000388 div >>= 1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000389 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
390 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600391 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000392
393 /* Wait max 10 ms */
394 timeout = 10;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600395 while (!(readw(&priv->reg->clkcon) &
Anton staaf0dfb31c2011-11-10 11:56:49 +0000396 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000397 if (timeout == 0) {
398 printf("%s: timeout error\n", __func__);
399 return;
400 }
401 timeout--;
402 udelay(1000);
403 }
404
Anton staaf0dfb31c2011-11-10 11:56:49 +0000405 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600406 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000407
408 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren85f0ee42011-05-31 10:30:37 +0000409
410out:
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600411 priv->clock = clock;
Tom Warren85f0ee42011-05-31 10:30:37 +0000412}
413
Simon Glass8c4c5c82017-04-23 20:02:11 -0600414static int tegra_mmc_set_ios(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000415{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600416 struct tegra_mmc_priv *priv = dev_get_priv(dev);
417 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000418 unsigned char ctrl;
419 debug(" mmc_set_ios called\n");
420
421 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
422
423 /* Change clock first */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600424 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren85f0ee42011-05-31 10:30:37 +0000425
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600426 ctrl = readb(&priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000427
428 /*
429 * WIDE8[5]
430 * 0 = Depend on WIDE4
431 * 1 = 8-bit mode
432 * WIDE4[1]
433 * 1 = 4-bit mode
434 * 0 = 1-bit mode
435 */
436 if (mmc->bus_width == 8)
437 ctrl |= (1 << 5);
438 else if (mmc->bus_width == 4)
439 ctrl |= (1 << 1);
440 else
Simon Glass9d6551a2017-06-07 21:11:48 -0600441 ctrl &= ~(1 << 1 | 1 << 5);
Tom Warren85f0ee42011-05-31 10:30:37 +0000442
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600443 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000444 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900445
446 return 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000447}
448
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600449static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600450{
451#if defined(CONFIG_TEGRA30)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600452 u32 val;
453
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600454 debug("%s: sdmmc address = %08x\n", __func__, (unsigned int)priv->reg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600455
456 /* Set the pad drive strength for SDMMC1 or 3 only */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600457 if (priv->reg != (void *)0x78000000 &&
458 priv->reg != (void *)0x78000400) {
Stephen Warrenc76e9362016-09-13 10:45:44 -0600459 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
460 __func__);
461 return;
462 }
463
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600464 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600465 val &= 0xFFFFFFF0;
466 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600467 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600468
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600469 val = readl(&priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600470 val &= 0xFFFF0000;
471 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET | AUTO_CAL_ENABLED;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600472 writel(val, &priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600473#endif
474}
475
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600476static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000477{
478 unsigned int timeout;
479 debug(" mmc_reset called\n");
480
481 /*
482 * RSTALL[0] : Software reset for all
483 * 1 = reset
484 * 0 = work
485 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600486 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren85f0ee42011-05-31 10:30:37 +0000487
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600488 priv->clock = 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000489
490 /* Wait max 100 ms */
491 timeout = 100;
492
493 /* hw clears the bit when it's done */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600494 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000495 if (timeout == 0) {
496 printf("%s: timeout error\n", __func__);
497 return;
498 }
499 timeout--;
500 udelay(1000);
501 }
Tom Warren35ae07b2013-02-26 12:31:26 -0700502
503 /* Set SD bus voltage & enable bus power */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600504 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren35ae07b2013-02-26 12:31:26 -0700505 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600506 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren35ae07b2013-02-26 12:31:26 -0700507
508 /* Make sure SDIO pads are set up */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600509 tegra_mmc_pad_init(priv);
Tom Warren85f0ee42011-05-31 10:30:37 +0000510}
511
Simon Glass8c4c5c82017-04-23 20:02:11 -0600512static int tegra_mmc_init(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000513{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600514 struct tegra_mmc_priv *priv = dev_get_priv(dev);
515 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000516 unsigned int mask;
Tom Warrena66f7722016-09-13 10:45:48 -0600517 debug(" tegra_mmc_init called\n");
Tom Warren85f0ee42011-05-31 10:30:37 +0000518
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600519 tegra_mmc_reset(priv, mmc);
Tom Warren85f0ee42011-05-31 10:30:37 +0000520
Marcel Ziswiler86708852017-03-25 01:18:22 +0100521#if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK)
522 /*
523 * Disable the external clock loopback and use the internal one on
524 * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1
525 * bits being set to 0xfffd according to the TRM.
526 *
527 * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled
528 * approach once proper kernel integration made it mainline.
529 */
530 if (priv->reg == (void *)0x700b0400) {
531 mask = readl(&priv->reg->venmiscctl);
532 mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK;
533 writel(mask, &priv->reg->venmiscctl);
534 }
535#endif
536
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600537 priv->version = readw(&priv->reg->hcver);
538 debug("host version = %x\n", priv->version);
Tom Warren85f0ee42011-05-31 10:30:37 +0000539
540 /* mask all */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600541 writel(0xffffffff, &priv->reg->norintstsen);
542 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000543
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600544 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000545 /*
546 * NORMAL Interrupt Status Enable Register init
547 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
548 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf3ade2102011-11-10 11:56:50 +0000549 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren85f0ee42011-05-31 10:30:37 +0000550 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
551 * [0] ENSTACMDCMPLT : Command Complete Status Enable
552 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600553 mask = readl(&priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000554 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000555 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
556 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf3ade2102011-11-10 11:56:50 +0000557 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf0dfb31c2011-11-10 11:56:49 +0000558 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
559 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600560 writel(mask, &priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000561
562 /*
563 * NORMAL Interrupt Signal Enable Register init
564 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
565 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600566 mask = readl(&priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000567 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000568 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600569 writel(mask, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000570
571 return 0;
572}
573
Simon Glass8c4c5c82017-04-23 20:02:11 -0600574static int tegra_mmc_getcd(struct udevice *dev)
Thierry Redingf1494112012-01-02 01:15:39 +0000575{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600576 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Thierry Redingf1494112012-01-02 01:15:39 +0000577
Tom Warren22562a42012-09-04 17:00:24 -0700578 debug("tegra_mmc_getcd called\n");
Thierry Redingf1494112012-01-02 01:15:39 +0000579
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600580 if (dm_gpio_is_valid(&priv->cd_gpio))
581 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingf1494112012-01-02 01:15:39 +0000582
583 return 1;
584}
585
Simon Glass8c4c5c82017-04-23 20:02:11 -0600586static const struct dm_mmc_ops tegra_mmc_ops = {
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200587 .send_cmd = tegra_mmc_send_cmd,
588 .set_ios = tegra_mmc_set_ios,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600589 .get_cd = tegra_mmc_getcd,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200590};
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);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600595 struct tegra_mmc_plat *plat = dev_get_platdata(dev);
Tom Warrena66f7722016-09-13 10:45:48 -0600596 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600597 struct mmc_config *cfg = &plat->cfg;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600598 int bus_width, ret;
Tom Warren85f0ee42011-05-31 10:30:37 +0000599
Simon Glass8c4c5c82017-04-23 20:02:11 -0600600 cfg->name = dev->name;
Tom Warren85f0ee42011-05-31 10:30:37 +0000601
Simon Glassdd79d6e2017-01-17 16:52:55 -0700602 bus_width = fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev),
603 "bus-width", 1);
Tom Warrena66f7722016-09-13 10:45:48 -0600604
Simon Glass8c4c5c82017-04-23 20:02:11 -0600605 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
606 cfg->host_caps = 0;
Tom Warrena66f7722016-09-13 10:45:48 -0600607 if (bus_width == 8)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600608 cfg->host_caps |= MMC_MODE_8BIT;
Tom Warrena66f7722016-09-13 10:45:48 -0600609 if (bus_width >= 4)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600610 cfg->host_caps |= MMC_MODE_4BIT;
611 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren85f0ee42011-05-31 10:30:37 +0000612
613 /*
614 * min freq is for card identification, and is the highest
615 * low-speed SDIO card frequency (actually 400KHz)
616 * max freq is highest HS eMMC clock as per the SD/MMC spec
617 * (actually 52MHz)
Tom Warren85f0ee42011-05-31 10:30:37 +0000618 */
Simon Glass8c4c5c82017-04-23 20:02:11 -0600619 cfg->f_min = 375000;
620 cfg->f_max = 48000000;
Tom Warren85f0ee42011-05-31 10:30:37 +0000621
Simon Glass8c4c5c82017-04-23 20:02:11 -0600622 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200623
Simon Glassba1dea42017-05-17 17:18:05 -0600624 priv->reg = (void *)devfdt_get_addr(dev);
Tom Warren9745cf82013-02-21 12:31:30 +0000625
Tom Warrena66f7722016-09-13 10:45:48 -0600626 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
627 if (ret) {
628 debug("reset_get_by_name() failed: %d\n", ret);
629 return ret;
630 }
631 ret = clk_get_by_index(dev, 0, &priv->clk);
632 if (ret) {
633 debug("clk_get_by_index() failed: %d\n", ret);
634 return ret;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600635 }
Tom Warrena66f7722016-09-13 10:45:48 -0600636
637 ret = reset_assert(&priv->reset_ctl);
638 if (ret)
639 return ret;
640 ret = clk_enable(&priv->clk);
641 if (ret)
642 return ret;
643 ret = clk_set_rate(&priv->clk, 20000000);
644 if (IS_ERR_VALUE(ret))
645 return ret;
646 ret = reset_deassert(&priv->reset_ctl);
647 if (ret)
648 return ret;
Tom Warren9745cf82013-02-21 12:31:30 +0000649
Tom Warrena66f7722016-09-13 10:45:48 -0600650 /* These GPIOs are optional */
651 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio,
652 GPIOD_IS_IN);
653 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio,
654 GPIOD_IS_IN);
655 gpio_request_by_name(dev, "power-gpios", 0,
656 &priv->pwr_gpio, GPIOD_IS_OUT);
657 if (dm_gpio_is_valid(&priv->pwr_gpio))
658 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warren9745cf82013-02-21 12:31:30 +0000659
Simon Glass8c4c5c82017-04-23 20:02:11 -0600660 upriv->mmc = &plat->mmc;
Tom Warren9745cf82013-02-21 12:31:30 +0000661
Simon Glass8c4c5c82017-04-23 20:02:11 -0600662 return tegra_mmc_init(dev);
663}
Tom Warren9745cf82013-02-21 12:31:30 +0000664
Simon Glass8c4c5c82017-04-23 20:02:11 -0600665static int tegra_mmc_bind(struct udevice *dev)
666{
667 struct tegra_mmc_plat *plat = dev_get_platdata(dev);
668
669 return mmc_bind(dev, &plat->mmc, &plat->cfg);
Tom Warren9745cf82013-02-21 12:31:30 +0000670}
671
Tom Warrena66f7722016-09-13 10:45:48 -0600672static const struct udevice_id tegra_mmc_ids[] = {
673 { .compatible = "nvidia,tegra20-sdhci" },
674 { .compatible = "nvidia,tegra30-sdhci" },
675 { .compatible = "nvidia,tegra114-sdhci" },
676 { .compatible = "nvidia,tegra124-sdhci" },
677 { .compatible = "nvidia,tegra210-sdhci" },
678 { .compatible = "nvidia,tegra186-sdhci" },
679 { }
680};
Tom Warren9745cf82013-02-21 12:31:30 +0000681
Tom Warrena66f7722016-09-13 10:45:48 -0600682U_BOOT_DRIVER(tegra_mmc_drv) = {
683 .name = "tegra_mmc",
684 .id = UCLASS_MMC,
685 .of_match = tegra_mmc_ids,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600686 .bind = tegra_mmc_bind,
Tom Warrena66f7722016-09-13 10:45:48 -0600687 .probe = tegra_mmc_probe,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600688 .ops = &tegra_mmc_ops,
689 .platdata_auto_alloc_size = sizeof(struct tegra_mmc_plat),
Tom Warrena66f7722016-09-13 10:45:48 -0600690 .priv_auto_alloc_size = sizeof(struct tegra_mmc_priv),
691};