blob: 2b041562a63974e6e9c8dc156aac4fad52fb6baa [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Tom Warren85f0ee42011-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 Warren2e86e812019-05-29 09:30:01 -07006 * Portions Copyright 2011-2019 NVIDIA Corporation
Tom Warren85f0ee42011-05-31 10:30:37 +00007 */
8
Stephen Warrenf227e452012-11-06 11:27:30 +00009#include <bouncebuf.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000010#include <common.h>
Simon Glass11c89f32017-05-17 17:18:03 -060011#include <dm.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090012#include <errno.h>
Simon Glass9c3b0e42017-07-25 08:30:08 -060013#include <mmc.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>
Simon Glassd66c5f72020-02-03 07:36:15 -070017#include <linux/err.h>
Tom Warren2e86e812019-05-29 09:30:01 -070018#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210)
19#include <asm/arch/clock.h>
20#endif
Tom Warren85f0ee42011-05-31 10:30:37 +000021
Simon Glass8c4c5c82017-04-23 20:02:11 -060022struct tegra_mmc_plat {
23 struct mmc_config cfg;
24 struct mmc mmc;
25};
26
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060027struct tegra_mmc_priv {
28 struct tegra_mmc *reg;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060029 struct reset_ctl reset_ctl;
30 struct clk clk;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060031 struct gpio_desc cd_gpio; /* Change Detect GPIO */
32 struct gpio_desc pwr_gpio; /* Power GPIO */
33 struct gpio_desc wp_gpio; /* Write Protect GPIO */
34 unsigned int version; /* SDHCI spec. version */
35 unsigned int clock; /* Current clock (MHz) */
Tom Warren2e86e812019-05-29 09:30:01 -070036 int mmc_id; /* peripheral id */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060037};
38
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060039static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
40 unsigned short power)
Tom Warren35ae07b2013-02-26 12:31:26 -070041{
42 u8 pwr = 0;
43 debug("%s: power = %x\n", __func__, power);
44
45 if (power != (unsigned short)-1) {
46 switch (1 << power) {
47 case MMC_VDD_165_195:
48 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
49 break;
50 case MMC_VDD_29_30:
51 case MMC_VDD_30_31:
52 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
53 break;
54 case MMC_VDD_32_33:
55 case MMC_VDD_33_34:
56 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
57 break;
58 }
59 }
60 debug("%s: pwr = %X\n", __func__, pwr);
61
62 /* Set the bus voltage first (if any) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060063 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070064 if (pwr == 0)
65 return;
66
67 /* Now enable bus power */
68 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060069 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070070}
71
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060072static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
73 struct mmc_data *data,
74 struct bounce_buffer *bbstate)
Tom Warren85f0ee42011-05-31 10:30:37 +000075{
76 unsigned char ctrl;
77
Tom Warren85f0ee42011-05-31 10:30:37 +000078
Stephen Warrenf227e452012-11-06 11:27:30 +000079 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
80 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
81 data->blocksize);
82
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060083 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren85f0ee42011-05-31 10:30:37 +000084 /*
85 * DMASEL[4:3]
86 * 00 = Selects SDMA
87 * 01 = Reserved
88 * 10 = Selects 32-bit Address ADMA2
89 * 11 = Selects 64-bit Address ADMA2
90 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060091 ctrl = readb(&priv->reg->hostctl);
Anton staaf0dfb31c2011-11-10 11:56:49 +000092 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
93 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060094 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +000095
96 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060097 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
98 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren85f0ee42011-05-31 10:30:37 +000099}
100
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600101static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
102 struct mmc_data *data)
Tom Warren85f0ee42011-05-31 10:30:37 +0000103{
104 unsigned short mode;
105 debug(" mmc_set_transfer_mode called\n");
106 /*
107 * TRNMOD
108 * MUL1SIN0[5] : Multi/Single Block Select
109 * RD1WT0[4] : Data Transfer Direction Select
110 * 1 = read
111 * 0 = write
112 * ENACMD12[2] : Auto CMD12 Enable
113 * ENBLKCNT[1] : Block Count Enable
114 * ENDMA[0] : DMA Enable
115 */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000116 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
117 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
118
Tom Warren85f0ee42011-05-31 10:30:37 +0000119 if (data->blocks > 1)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000120 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
121
Tom Warren85f0ee42011-05-31 10:30:37 +0000122 if (data->flags & MMC_DATA_READ)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000123 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren85f0ee42011-05-31 10:30:37 +0000124
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600125 writew(mode, &priv->reg->trnmod);
Tom Warren85f0ee42011-05-31 10:30:37 +0000126}
127
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600128static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
129 struct mmc_cmd *cmd,
130 struct mmc_data *data,
131 unsigned int timeout)
Tom Warren85f0ee42011-05-31 10:30:37 +0000132{
Tom Warren85f0ee42011-05-31 10:30:37 +0000133 /*
134 * PRNSTS
Anton staaf5ab3fba2011-11-10 11:56:52 +0000135 * CMDINHDAT[1] : Command Inhibit (DAT)
136 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren85f0ee42011-05-31 10:30:37 +0000137 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000138 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren85f0ee42011-05-31 10:30:37 +0000139
140 /*
141 * We shouldn't wait for data inhibit for stop commands, even
142 * though they might use busy signaling
143 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000144 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
145 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000146
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600147 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000148 if (timeout == 0) {
149 printf("%s: timeout error\n", __func__);
150 return -1;
151 }
152 timeout--;
153 udelay(1000);
154 }
155
Anton staaf5ab3fba2011-11-10 11:56:52 +0000156 return 0;
157}
158
Simon Glass8c4c5c82017-04-23 20:02:11 -0600159static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600160 struct mmc_data *data,
161 struct bounce_buffer *bbstate)
Anton staaf5ab3fba2011-11-10 11:56:52 +0000162{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600163 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000164 int flags, i;
165 int result;
Anatolij Gustschine1f53412012-03-28 03:40:00 +0000166 unsigned int mask = 0;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000167 unsigned int retry = 0x100000;
168 debug(" mmc_send_cmd called\n");
169
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600170 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000171
172 if (result < 0)
173 return result;
174
Tom Warren85f0ee42011-05-31 10:30:37 +0000175 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600176 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren85f0ee42011-05-31 10:30:37 +0000177
178 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600179 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren85f0ee42011-05-31 10:30:37 +0000180
181 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600182 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren85f0ee42011-05-31 10:30:37 +0000183
184 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
185 return -1;
186
187 /*
188 * CMDREG
189 * CMDIDX[13:8] : Command index
190 * DATAPRNT[5] : Data Present Select
191 * ENCMDIDX[4] : Command Index Check Enable
192 * ENCMDCRC[3] : Command CRC Check Enable
193 * RSPTYP[1:0]
194 * 00 = No Response
195 * 01 = Length 136
196 * 10 = Length 48
197 * 11 = Length 48 Check busy after response
198 */
199 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf0dfb31c2011-11-10 11:56:49 +0000200 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren85f0ee42011-05-31 10:30:37 +0000201 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000202 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren85f0ee42011-05-31 10:30:37 +0000203 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000204 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren85f0ee42011-05-31 10:30:37 +0000205 else
Anton staaf0dfb31c2011-11-10 11:56:49 +0000206 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren85f0ee42011-05-31 10:30:37 +0000207
208 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000209 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000210 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000211 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000212 if (data)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000213 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren85f0ee42011-05-31 10:30:37 +0000214
215 debug("cmd: %d\n", cmd->cmdidx);
216
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600217 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren85f0ee42011-05-31 10:30:37 +0000218
219 for (i = 0; i < retry; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600220 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000221 /* Command Complete */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000222 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000223 if (!data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600224 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000225 break;
226 }
227 }
228
229 if (i == retry) {
230 printf("%s: waiting for status update\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600231 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900232 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000233 }
234
Anton staaf0dfb31c2011-11-10 11:56:49 +0000235 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000236 /* Timeout Error */
237 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600238 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900239 return -ETIMEDOUT;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000240 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000241 /* Error Interrupt */
242 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600243 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000244 return -1;
245 }
246
247 if (cmd->resp_type & MMC_RSP_PRESENT) {
248 if (cmd->resp_type & MMC_RSP_136) {
249 /* CRC is stripped so we need to do some shifting. */
250 for (i = 0; i < 4; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600251 unsigned long offset = (unsigned long)
252 (&priv->reg->rspreg3 - i);
Tom Warren85f0ee42011-05-31 10:30:37 +0000253 cmd->response[i] = readl(offset) << 8;
254
255 if (i != 3) {
256 cmd->response[i] |=
257 readb(offset - 1);
258 }
259 debug("cmd->resp[%d]: %08x\n",
260 i, cmd->response[i]);
261 }
262 } else if (cmd->resp_type & MMC_RSP_BUSY) {
263 for (i = 0; i < retry; i++) {
264 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600265 if (readl(&priv->reg->prnsts)
Tom Warren85f0ee42011-05-31 10:30:37 +0000266 & (1 << 20)) /* DAT[0] */
267 break;
268 }
269
270 if (i == retry) {
271 printf("%s: card is still busy\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600272 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900273 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000274 }
275
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600276 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000277 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
278 } else {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600279 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000280 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
281 }
282 }
283
284 if (data) {
Anton staafbd348422011-11-10 11:56:51 +0000285 unsigned long start = get_timer(0);
286
Tom Warren85f0ee42011-05-31 10:30:37 +0000287 while (1) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600288 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000289
Anton staaf0dfb31c2011-11-10 11:56:49 +0000290 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000291 /* Error Interrupt */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600292 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000293 printf("%s: error during transfer: 0x%08x\n",
294 __func__, mask);
295 return -1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000296 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf3ade2102011-11-10 11:56:50 +0000297 /*
298 * DMA Interrupt, restart the transfer where
299 * it was interrupted.
300 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600301 unsigned int address = readl(&priv->reg->sysad);
Anton staaf3ade2102011-11-10 11:56:50 +0000302
Tom Warren85f0ee42011-05-31 10:30:37 +0000303 debug("DMA end\n");
Anton staaf3ade2102011-11-10 11:56:50 +0000304 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600305 &priv->reg->norintsts);
306 writel(address, &priv->reg->sysad);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000307 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000308 /* Transfer Complete */
309 debug("r/w is done\n");
310 break;
Marcel Ziswilere1207e92014-10-04 01:48:53 +0200311 } else if (get_timer(start) > 8000UL) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600312 writel(mask, &priv->reg->norintsts);
Anton staafbd348422011-11-10 11:56:51 +0000313 printf("%s: MMC Timeout\n"
314 " Interrupt status 0x%08x\n"
315 " Interrupt status enable 0x%08x\n"
316 " Interrupt signal enable 0x%08x\n"
317 " Present status 0x%08x\n",
318 __func__, mask,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600319 readl(&priv->reg->norintstsen),
320 readl(&priv->reg->norintsigen),
321 readl(&priv->reg->prnsts));
Anton staafbd348422011-11-10 11:56:51 +0000322 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000323 }
324 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600325 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000326 }
327
328 udelay(1000);
329 return 0;
330}
331
Simon Glass8c4c5c82017-04-23 20:02:11 -0600332static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600333 struct mmc_data *data)
Stephen Warrenf227e452012-11-06 11:27:30 +0000334{
335 void *buf;
336 unsigned int bbflags;
337 size_t len;
338 struct bounce_buffer bbstate;
339 int ret;
340
341 if (data) {
342 if (data->flags & MMC_DATA_READ) {
343 buf = data->dest;
344 bbflags = GEN_BB_WRITE;
345 } else {
346 buf = (void *)data->src;
347 bbflags = GEN_BB_READ;
348 }
349 len = data->blocks * data->blocksize;
350
351 bounce_buffer_start(&bbstate, buf, len, bbflags);
352 }
353
Simon Glass8c4c5c82017-04-23 20:02:11 -0600354 ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate);
Stephen Warrenf227e452012-11-06 11:27:30 +0000355
356 if (data)
357 bounce_buffer_stop(&bbstate);
358
359 return ret;
360}
361
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600362static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren85f0ee42011-05-31 10:30:37 +0000363{
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600364 ulong rate;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000365 int div;
Tom Warren85f0ee42011-05-31 10:30:37 +0000366 unsigned short clk;
367 unsigned long timeout;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000368
Tom Warren85f0ee42011-05-31 10:30:37 +0000369 debug(" mmc_change_clock called\n");
370
Simon Glassc2ea5e42011-09-21 12:40:04 +0000371 /*
Tom Warren35ae07b2013-02-26 12:31:26 -0700372 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glassc2ea5e42011-09-21 12:40:04 +0000373 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000374 if (clock == 0)
375 goto out;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600376
377 rate = clk_set_rate(&priv->clk, clock);
378 div = (rate + clock - 1) / clock;
Tom Warren4ff710a2019-06-03 16:06:34 -0700379
380#if defined(CONFIG_TEGRA210)
381 if (priv->mmc_id == PERIPH_ID_SDMMC1 && clock <= 400000) {
382 /* clock_adjust_periph_pll_div() chooses a 'bad' clock
383 * on SDMMC1 T210, so skip it here and force a clock
384 * that's been spec'd in the table in the TRM for
385 * card-detect (400KHz).
386 */
387 uint effective_rate = clock_adjust_periph_pll_div(priv->mmc_id,
388 CLOCK_ID_PERIPH, 24727273, NULL);
389 div = 62;
390
391 debug("%s: WAR: Using SDMMC1 clock of %u, div %d to achieve %dHz card clock ...\n",
392 __func__, effective_rate, div, clock);
393 } else {
394 clock_adjust_periph_pll_div(priv->mmc_id, CLOCK_ID_PERIPH,
395 clock, &div);
396 }
397#endif
Simon Glassc2ea5e42011-09-21 12:40:04 +0000398 debug("div = %d\n", div);
Tom Warren85f0ee42011-05-31 10:30:37 +0000399
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600400 writew(0, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000401
Tom Warren85f0ee42011-05-31 10:30:37 +0000402 /*
403 * CLKCON
404 * SELFREQ[15:8] : base clock divided by value
405 * ENSDCLK[2] : SD Clock Enable
406 * STBLINTCLK[1] : Internal Clock Stable
407 * ENINTCLK[0] : Internal Clock Enable
408 */
Simon Glassc2ea5e42011-09-21 12:40:04 +0000409 div >>= 1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000410 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
411 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600412 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000413
414 /* Wait max 10 ms */
415 timeout = 10;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600416 while (!(readw(&priv->reg->clkcon) &
Anton staaf0dfb31c2011-11-10 11:56:49 +0000417 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000418 if (timeout == 0) {
419 printf("%s: timeout error\n", __func__);
420 return;
421 }
422 timeout--;
423 udelay(1000);
424 }
425
Anton staaf0dfb31c2011-11-10 11:56:49 +0000426 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600427 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000428
429 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren85f0ee42011-05-31 10:30:37 +0000430
431out:
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600432 priv->clock = clock;
Tom Warren85f0ee42011-05-31 10:30:37 +0000433}
434
Simon Glass8c4c5c82017-04-23 20:02:11 -0600435static int tegra_mmc_set_ios(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000436{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600437 struct tegra_mmc_priv *priv = dev_get_priv(dev);
438 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000439 unsigned char ctrl;
440 debug(" mmc_set_ios called\n");
441
442 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
443
444 /* Change clock first */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600445 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren85f0ee42011-05-31 10:30:37 +0000446
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600447 ctrl = readb(&priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000448
449 /*
450 * WIDE8[5]
451 * 0 = Depend on WIDE4
452 * 1 = 8-bit mode
453 * WIDE4[1]
454 * 1 = 4-bit mode
455 * 0 = 1-bit mode
456 */
457 if (mmc->bus_width == 8)
458 ctrl |= (1 << 5);
459 else if (mmc->bus_width == 4)
460 ctrl |= (1 << 1);
461 else
Simon Glass9d6551a2017-06-07 21:11:48 -0600462 ctrl &= ~(1 << 1 | 1 << 5);
Tom Warren85f0ee42011-05-31 10:30:37 +0000463
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600464 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000465 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900466
467 return 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000468}
469
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600470static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600471{
Tom Warren2e86e812019-05-29 09:30:01 -0700472#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600473 u32 val;
Tom Warren2e86e812019-05-29 09:30:01 -0700474 u16 clk_con;
475 int timeout;
476 int id = priv->mmc_id;
Stephen Warrenc76e9362016-09-13 10:45:44 -0600477
Tom Warren2e86e812019-05-29 09:30:01 -0700478 debug("%s: sdmmc address = %p, id = %d\n", __func__,
479 priv->reg, id);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600480
481 /* Set the pad drive strength for SDMMC1 or 3 only */
Tom Warren2e86e812019-05-29 09:30:01 -0700482 if (id != PERIPH_ID_SDMMC1 && id != PERIPH_ID_SDMMC3) {
Stephen Warrenc76e9362016-09-13 10:45:44 -0600483 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
Tom Warren2e86e812019-05-29 09:30:01 -0700484 __func__);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600485 return;
486 }
487
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600488 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600489 val &= 0xFFFFFFF0;
490 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600491 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600492
Tom Warren2e86e812019-05-29 09:30:01 -0700493 /* Disable SD Clock Enable before running auto-cal as per TRM */
494 clk_con = readw(&priv->reg->clkcon);
495 debug("%s: CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
496 clk_con &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
497 writew(clk_con, &priv->reg->clkcon);
498
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600499 val = readl(&priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600500 val &= 0xFFFF0000;
Tom Warren2e86e812019-05-29 09:30:01 -0700501 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600502 writel(val, &priv->reg->autocalcfg);
Tom Warren2e86e812019-05-29 09:30:01 -0700503 val |= AUTO_CAL_START | AUTO_CAL_ENABLE;
504 writel(val, &priv->reg->autocalcfg);
505 debug("%s: AUTO_CAL_CFG = 0x%08X\n", __func__, val);
506 udelay(1);
507 timeout = 100; /* 10 mSec max (100*100uS) */
508 do {
509 val = readl(&priv->reg->autocalsts);
510 udelay(100);
511 } while ((val & AUTO_CAL_ACTIVE) && --timeout);
512 val = readl(&priv->reg->autocalsts);
513 debug("%s: Final AUTO_CAL_STATUS = 0x%08X, timeout = %d\n",
514 __func__, val, timeout);
515
516 /* Re-enable SD Clock Enable when auto-cal is done */
517 clk_con |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
518 writew(clk_con, &priv->reg->clkcon);
519 clk_con = readw(&priv->reg->clkcon);
520 debug("%s: final CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
521
522 if (timeout == 0) {
523 printf("%s: Warning: Autocal timed out!\n", __func__);
524 /* TBD: Set CFG2TMC_SDMMC1_PAD_CAL_DRV* regs here */
525 }
526
527#if defined(CONFIG_TEGRA210)
528 u32 tap_value, trim_value;
529
530 /* Set tap/trim values for SDMMC1/3 @ <48MHz here */
531 val = readl(&priv->reg->venspictl); /* aka VENDOR_SYS_SW_CNTL */
532 val &= IO_TRIM_BYPASS_MASK;
533 if (id == PERIPH_ID_SDMMC1) {
534 tap_value = 4; /* default */
535 if (val)
536 tap_value = 3;
537 trim_value = 2;
538 } else { /* SDMMC3 */
539 tap_value = 3;
540 trim_value = 3;
541 }
542
543 val = readl(&priv->reg->venclkctl);
544 val &= ~TRIM_VAL_MASK;
545 val |= (trim_value << TRIM_VAL_SHIFT);
546 val &= ~TAP_VAL_MASK;
547 val |= (tap_value << TAP_VAL_SHIFT);
548 writel(val, &priv->reg->venclkctl);
549 debug("%s: VENDOR_CLOCK_CNTRL = 0x%08X\n", __func__, val);
550#endif /* T210 */
551#endif /* T30/T210 */
Stephen Warrenc76e9362016-09-13 10:45:44 -0600552}
553
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600554static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000555{
556 unsigned int timeout;
557 debug(" mmc_reset called\n");
558
559 /*
560 * RSTALL[0] : Software reset for all
561 * 1 = reset
562 * 0 = work
563 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600564 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren85f0ee42011-05-31 10:30:37 +0000565
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600566 priv->clock = 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000567
568 /* Wait max 100 ms */
569 timeout = 100;
570
571 /* hw clears the bit when it's done */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600572 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000573 if (timeout == 0) {
574 printf("%s: timeout error\n", __func__);
575 return;
576 }
577 timeout--;
578 udelay(1000);
579 }
Tom Warren35ae07b2013-02-26 12:31:26 -0700580
581 /* Set SD bus voltage & enable bus power */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600582 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren35ae07b2013-02-26 12:31:26 -0700583 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600584 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren35ae07b2013-02-26 12:31:26 -0700585
586 /* Make sure SDIO pads are set up */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600587 tegra_mmc_pad_init(priv);
Tom Warren85f0ee42011-05-31 10:30:37 +0000588}
589
Simon Glass8c4c5c82017-04-23 20:02:11 -0600590static int tegra_mmc_init(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000591{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600592 struct tegra_mmc_priv *priv = dev_get_priv(dev);
593 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000594 unsigned int mask;
Tom Warrena66f7722016-09-13 10:45:48 -0600595 debug(" tegra_mmc_init called\n");
Tom Warren85f0ee42011-05-31 10:30:37 +0000596
Tom Warren2e86e812019-05-29 09:30:01 -0700597#if defined(CONFIG_TEGRA210)
598 priv->mmc_id = clock_decode_periph_id(dev);
599 if (priv->mmc_id == PERIPH_ID_NONE) {
600 printf("%s: Missing/invalid peripheral ID\n", __func__);
601 return -EINVAL;
602 }
603#endif
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600604 tegra_mmc_reset(priv, mmc);
Tom Warren85f0ee42011-05-31 10:30:37 +0000605
Marcel Ziswiler86708852017-03-25 01:18:22 +0100606#if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK)
607 /*
608 * Disable the external clock loopback and use the internal one on
609 * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1
610 * bits being set to 0xfffd according to the TRM.
611 *
612 * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled
613 * approach once proper kernel integration made it mainline.
614 */
615 if (priv->reg == (void *)0x700b0400) {
616 mask = readl(&priv->reg->venmiscctl);
617 mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK;
618 writel(mask, &priv->reg->venmiscctl);
619 }
620#endif
621
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600622 priv->version = readw(&priv->reg->hcver);
623 debug("host version = %x\n", priv->version);
Tom Warren85f0ee42011-05-31 10:30:37 +0000624
625 /* mask all */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600626 writel(0xffffffff, &priv->reg->norintstsen);
627 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000628
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600629 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000630 /*
631 * NORMAL Interrupt Status Enable Register init
632 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
633 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf3ade2102011-11-10 11:56:50 +0000634 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren85f0ee42011-05-31 10:30:37 +0000635 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
636 * [0] ENSTACMDCMPLT : Command Complete Status Enable
637 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600638 mask = readl(&priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000639 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000640 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
641 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf3ade2102011-11-10 11:56:50 +0000642 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf0dfb31c2011-11-10 11:56:49 +0000643 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
644 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600645 writel(mask, &priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000646
647 /*
648 * NORMAL Interrupt Signal Enable Register init
649 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
650 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600651 mask = readl(&priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000652 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000653 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600654 writel(mask, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000655
656 return 0;
657}
658
Simon Glass8c4c5c82017-04-23 20:02:11 -0600659static int tegra_mmc_getcd(struct udevice *dev)
Thierry Redingf1494112012-01-02 01:15:39 +0000660{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600661 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Thierry Redingf1494112012-01-02 01:15:39 +0000662
Tom Warren22562a42012-09-04 17:00:24 -0700663 debug("tegra_mmc_getcd called\n");
Thierry Redingf1494112012-01-02 01:15:39 +0000664
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600665 if (dm_gpio_is_valid(&priv->cd_gpio))
666 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingf1494112012-01-02 01:15:39 +0000667
668 return 1;
669}
670
Simon Glass8c4c5c82017-04-23 20:02:11 -0600671static const struct dm_mmc_ops tegra_mmc_ops = {
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200672 .send_cmd = tegra_mmc_send_cmd,
673 .set_ios = tegra_mmc_set_ios,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600674 .get_cd = tegra_mmc_getcd,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200675};
676
Tom Warrena66f7722016-09-13 10:45:48 -0600677static int tegra_mmc_probe(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000678{
Tom Warrena66f7722016-09-13 10:45:48 -0600679 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600680 struct tegra_mmc_plat *plat = dev_get_platdata(dev);
Tom Warrena66f7722016-09-13 10:45:48 -0600681 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600682 struct mmc_config *cfg = &plat->cfg;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600683 int bus_width, ret;
Tom Warren85f0ee42011-05-31 10:30:37 +0000684
Simon Glass8c4c5c82017-04-23 20:02:11 -0600685 cfg->name = dev->name;
Tom Warren85f0ee42011-05-31 10:30:37 +0000686
Simon Glass9c3b0e42017-07-25 08:30:08 -0600687 bus_width = dev_read_u32_default(dev, "bus-width", 1);
Tom Warrena66f7722016-09-13 10:45:48 -0600688
Simon Glass8c4c5c82017-04-23 20:02:11 -0600689 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
690 cfg->host_caps = 0;
Tom Warrena66f7722016-09-13 10:45:48 -0600691 if (bus_width == 8)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600692 cfg->host_caps |= MMC_MODE_8BIT;
Tom Warrena66f7722016-09-13 10:45:48 -0600693 if (bus_width >= 4)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600694 cfg->host_caps |= MMC_MODE_4BIT;
695 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren85f0ee42011-05-31 10:30:37 +0000696
697 /*
698 * min freq is for card identification, and is the highest
699 * low-speed SDIO card frequency (actually 400KHz)
700 * max freq is highest HS eMMC clock as per the SD/MMC spec
701 * (actually 52MHz)
Tom Warren85f0ee42011-05-31 10:30:37 +0000702 */
Simon Glass8c4c5c82017-04-23 20:02:11 -0600703 cfg->f_min = 375000;
704 cfg->f_max = 48000000;
Tom Warren85f0ee42011-05-31 10:30:37 +0000705
Simon Glass8c4c5c82017-04-23 20:02:11 -0600706 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200707
Simon Glass9c3b0e42017-07-25 08:30:08 -0600708 priv->reg = (void *)dev_read_addr(dev);
Tom Warren9745cf82013-02-21 12:31:30 +0000709
Tom Warrena66f7722016-09-13 10:45:48 -0600710 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
711 if (ret) {
712 debug("reset_get_by_name() failed: %d\n", ret);
713 return ret;
714 }
715 ret = clk_get_by_index(dev, 0, &priv->clk);
716 if (ret) {
717 debug("clk_get_by_index() failed: %d\n", ret);
718 return ret;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600719 }
Tom Warrena66f7722016-09-13 10:45:48 -0600720
721 ret = reset_assert(&priv->reset_ctl);
722 if (ret)
723 return ret;
724 ret = clk_enable(&priv->clk);
725 if (ret)
726 return ret;
727 ret = clk_set_rate(&priv->clk, 20000000);
728 if (IS_ERR_VALUE(ret))
729 return ret;
730 ret = reset_deassert(&priv->reset_ctl);
731 if (ret)
732 return ret;
Tom Warren9745cf82013-02-21 12:31:30 +0000733
Tom Warrena66f7722016-09-13 10:45:48 -0600734 /* These GPIOs are optional */
Simon Glass9c3b0e42017-07-25 08:30:08 -0600735 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
736 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
737 gpio_request_by_name(dev, "power-gpios", 0, &priv->pwr_gpio,
738 GPIOD_IS_OUT);
Tom Warrena66f7722016-09-13 10:45:48 -0600739 if (dm_gpio_is_valid(&priv->pwr_gpio))
740 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warren9745cf82013-02-21 12:31:30 +0000741
Simon Glass8c4c5c82017-04-23 20:02:11 -0600742 upriv->mmc = &plat->mmc;
Tom Warren9745cf82013-02-21 12:31:30 +0000743
Simon Glass8c4c5c82017-04-23 20:02:11 -0600744 return tegra_mmc_init(dev);
745}
Tom Warren9745cf82013-02-21 12:31:30 +0000746
Simon Glass8c4c5c82017-04-23 20:02:11 -0600747static int tegra_mmc_bind(struct udevice *dev)
748{
749 struct tegra_mmc_plat *plat = dev_get_platdata(dev);
750
751 return mmc_bind(dev, &plat->mmc, &plat->cfg);
Tom Warren9745cf82013-02-21 12:31:30 +0000752}
753
Tom Warrena66f7722016-09-13 10:45:48 -0600754static const struct udevice_id tegra_mmc_ids[] = {
755 { .compatible = "nvidia,tegra20-sdhci" },
756 { .compatible = "nvidia,tegra30-sdhci" },
757 { .compatible = "nvidia,tegra114-sdhci" },
758 { .compatible = "nvidia,tegra124-sdhci" },
759 { .compatible = "nvidia,tegra210-sdhci" },
760 { .compatible = "nvidia,tegra186-sdhci" },
761 { }
762};
Tom Warren9745cf82013-02-21 12:31:30 +0000763
Tom Warrena66f7722016-09-13 10:45:48 -0600764U_BOOT_DRIVER(tegra_mmc_drv) = {
765 .name = "tegra_mmc",
766 .id = UCLASS_MMC,
767 .of_match = tegra_mmc_ids,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600768 .bind = tegra_mmc_bind,
Tom Warrena66f7722016-09-13 10:45:48 -0600769 .probe = tegra_mmc_probe,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600770 .ops = &tegra_mmc_ops,
771 .platdata_auto_alloc_size = sizeof(struct tegra_mmc_plat),
Tom Warrena66f7722016-09-13 10:45:48 -0600772 .priv_auto_alloc_size = sizeof(struct tegra_mmc_priv),
773};