blob: c01fb3d01657f639819d741c316481b30c0488e4 [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 Glass0f2af882020-05-10 11:40:05 -060013#include <log.h>
Simon Glass9c3b0e42017-07-25 08:30:08 -060014#include <mmc.h>
Stephen Warrenfba87542011-10-31 06:51:36 +000015#include <asm/gpio.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000016#include <asm/io.h>
Tom Warrenab371962012-09-19 15:50:56 -070017#include <asm/arch-tegra/tegra_mmc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060018#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060019#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070020#include <linux/err.h>
Tom Warren2e86e812019-05-29 09:30:01 -070021#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210)
22#include <asm/arch/clock.h>
23#endif
Tom Warren85f0ee42011-05-31 10:30:37 +000024
Simon Glass8c4c5c82017-04-23 20:02:11 -060025struct tegra_mmc_plat {
26 struct mmc_config cfg;
27 struct mmc mmc;
28};
29
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060030struct tegra_mmc_priv {
31 struct tegra_mmc *reg;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060032 struct reset_ctl reset_ctl;
33 struct clk clk;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060034 struct gpio_desc cd_gpio; /* Change Detect GPIO */
35 struct gpio_desc pwr_gpio; /* Power GPIO */
36 struct gpio_desc wp_gpio; /* Write Protect GPIO */
37 unsigned int version; /* SDHCI spec. version */
38 unsigned int clock; /* Current clock (MHz) */
Tom Warren2e86e812019-05-29 09:30:01 -070039 int mmc_id; /* peripheral id */
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +030040
41 int tap_value;
42 int trim_value;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060043};
44
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060045static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
46 unsigned short power)
Tom Warren35ae07b2013-02-26 12:31:26 -070047{
48 u8 pwr = 0;
49 debug("%s: power = %x\n", __func__, power);
50
51 if (power != (unsigned short)-1) {
52 switch (1 << power) {
53 case MMC_VDD_165_195:
54 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
55 break;
56 case MMC_VDD_29_30:
57 case MMC_VDD_30_31:
58 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
59 break;
60 case MMC_VDD_32_33:
61 case MMC_VDD_33_34:
62 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
63 break;
64 }
65 }
66 debug("%s: pwr = %X\n", __func__, pwr);
67
68 /* Set the bus voltage first (if any) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060069 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070070 if (pwr == 0)
71 return;
72
73 /* Now enable bus power */
74 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060075 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070076}
77
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060078static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
79 struct mmc_data *data,
80 struct bounce_buffer *bbstate)
Tom Warren85f0ee42011-05-31 10:30:37 +000081{
82 unsigned char ctrl;
83
Tom Warren85f0ee42011-05-31 10:30:37 +000084
Stephen Warrenf227e452012-11-06 11:27:30 +000085 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
86 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
87 data->blocksize);
88
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060089 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren85f0ee42011-05-31 10:30:37 +000090 /*
91 * DMASEL[4:3]
92 * 00 = Selects SDMA
93 * 01 = Reserved
94 * 10 = Selects 32-bit Address ADMA2
95 * 11 = Selects 64-bit Address ADMA2
96 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060097 ctrl = readb(&priv->reg->hostctl);
Anton staaf0dfb31c2011-11-10 11:56:49 +000098 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
99 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600100 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000101
102 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600103 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
104 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren85f0ee42011-05-31 10:30:37 +0000105}
106
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600107static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
108 struct mmc_data *data)
Tom Warren85f0ee42011-05-31 10:30:37 +0000109{
110 unsigned short mode;
111 debug(" mmc_set_transfer_mode called\n");
112 /*
113 * TRNMOD
114 * MUL1SIN0[5] : Multi/Single Block Select
115 * RD1WT0[4] : Data Transfer Direction Select
116 * 1 = read
117 * 0 = write
118 * ENACMD12[2] : Auto CMD12 Enable
119 * ENBLKCNT[1] : Block Count Enable
120 * ENDMA[0] : DMA Enable
121 */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000122 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
123 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
124
Tom Warren85f0ee42011-05-31 10:30:37 +0000125 if (data->blocks > 1)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000126 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
127
Tom Warren85f0ee42011-05-31 10:30:37 +0000128 if (data->flags & MMC_DATA_READ)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000129 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren85f0ee42011-05-31 10:30:37 +0000130
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600131 writew(mode, &priv->reg->trnmod);
Tom Warren85f0ee42011-05-31 10:30:37 +0000132}
133
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600134static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
135 struct mmc_cmd *cmd,
136 struct mmc_data *data,
137 unsigned int timeout)
Tom Warren85f0ee42011-05-31 10:30:37 +0000138{
Tom Warren85f0ee42011-05-31 10:30:37 +0000139 /*
140 * PRNSTS
Anton staaf5ab3fba2011-11-10 11:56:52 +0000141 * CMDINHDAT[1] : Command Inhibit (DAT)
142 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren85f0ee42011-05-31 10:30:37 +0000143 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000144 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren85f0ee42011-05-31 10:30:37 +0000145
146 /*
147 * We shouldn't wait for data inhibit for stop commands, even
148 * though they might use busy signaling
149 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000150 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
151 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000152
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600153 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000154 if (timeout == 0) {
155 printf("%s: timeout error\n", __func__);
156 return -1;
157 }
158 timeout--;
159 udelay(1000);
160 }
161
Anton staaf5ab3fba2011-11-10 11:56:52 +0000162 return 0;
163}
164
Simon Glass8c4c5c82017-04-23 20:02:11 -0600165static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600166 struct mmc_data *data,
167 struct bounce_buffer *bbstate)
Anton staaf5ab3fba2011-11-10 11:56:52 +0000168{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600169 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000170 int flags, i;
171 int result;
Anatolij Gustschine1f53412012-03-28 03:40:00 +0000172 unsigned int mask = 0;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000173 unsigned int retry = 0x100000;
174 debug(" mmc_send_cmd called\n");
175
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600176 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000177
178 if (result < 0)
179 return result;
180
Tom Warren85f0ee42011-05-31 10:30:37 +0000181 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600182 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren85f0ee42011-05-31 10:30:37 +0000183
184 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600185 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren85f0ee42011-05-31 10:30:37 +0000186
187 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600188 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren85f0ee42011-05-31 10:30:37 +0000189
190 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
191 return -1;
192
193 /*
194 * CMDREG
195 * CMDIDX[13:8] : Command index
196 * DATAPRNT[5] : Data Present Select
197 * ENCMDIDX[4] : Command Index Check Enable
198 * ENCMDCRC[3] : Command CRC Check Enable
199 * RSPTYP[1:0]
200 * 00 = No Response
201 * 01 = Length 136
202 * 10 = Length 48
203 * 11 = Length 48 Check busy after response
204 */
205 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf0dfb31c2011-11-10 11:56:49 +0000206 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren85f0ee42011-05-31 10:30:37 +0000207 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000208 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren85f0ee42011-05-31 10:30:37 +0000209 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000210 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren85f0ee42011-05-31 10:30:37 +0000211 else
Anton staaf0dfb31c2011-11-10 11:56:49 +0000212 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren85f0ee42011-05-31 10:30:37 +0000213
214 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000215 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000216 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000217 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000218 if (data)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000219 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren85f0ee42011-05-31 10:30:37 +0000220
221 debug("cmd: %d\n", cmd->cmdidx);
222
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600223 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren85f0ee42011-05-31 10:30:37 +0000224
225 for (i = 0; i < retry; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600226 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000227 /* Command Complete */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000228 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000229 if (!data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600230 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000231 break;
232 }
233 }
234
235 if (i == retry) {
236 printf("%s: waiting for status update\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600237 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900238 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000239 }
240
Anton staaf0dfb31c2011-11-10 11:56:49 +0000241 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000242 /* Timeout Error */
243 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600244 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900245 return -ETIMEDOUT;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000246 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000247 /* Error Interrupt */
248 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600249 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000250 return -1;
251 }
252
253 if (cmd->resp_type & MMC_RSP_PRESENT) {
254 if (cmd->resp_type & MMC_RSP_136) {
255 /* CRC is stripped so we need to do some shifting. */
256 for (i = 0; i < 4; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600257 unsigned long offset = (unsigned long)
258 (&priv->reg->rspreg3 - i);
Tom Warren85f0ee42011-05-31 10:30:37 +0000259 cmd->response[i] = readl(offset) << 8;
260
261 if (i != 3) {
262 cmd->response[i] |=
263 readb(offset - 1);
264 }
265 debug("cmd->resp[%d]: %08x\n",
266 i, cmd->response[i]);
267 }
268 } else if (cmd->resp_type & MMC_RSP_BUSY) {
269 for (i = 0; i < retry; i++) {
270 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600271 if (readl(&priv->reg->prnsts)
Tom Warren85f0ee42011-05-31 10:30:37 +0000272 & (1 << 20)) /* DAT[0] */
273 break;
274 }
275
276 if (i == retry) {
277 printf("%s: card is still busy\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600278 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900279 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000280 }
281
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600282 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000283 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
284 } else {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600285 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000286 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
287 }
288 }
289
290 if (data) {
Anton staafbd348422011-11-10 11:56:51 +0000291 unsigned long start = get_timer(0);
292
Tom Warren85f0ee42011-05-31 10:30:37 +0000293 while (1) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600294 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000295
Anton staaf0dfb31c2011-11-10 11:56:49 +0000296 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000297 /* Error Interrupt */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600298 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000299 printf("%s: error during transfer: 0x%08x\n",
300 __func__, mask);
301 return -1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000302 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf3ade2102011-11-10 11:56:50 +0000303 /*
304 * DMA Interrupt, restart the transfer where
305 * it was interrupted.
306 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600307 unsigned int address = readl(&priv->reg->sysad);
Anton staaf3ade2102011-11-10 11:56:50 +0000308
Tom Warren85f0ee42011-05-31 10:30:37 +0000309 debug("DMA end\n");
Anton staaf3ade2102011-11-10 11:56:50 +0000310 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600311 &priv->reg->norintsts);
312 writel(address, &priv->reg->sysad);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000313 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000314 /* Transfer Complete */
315 debug("r/w is done\n");
316 break;
Marcel Ziswilere1207e92014-10-04 01:48:53 +0200317 } else if (get_timer(start) > 8000UL) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600318 writel(mask, &priv->reg->norintsts);
Anton staafbd348422011-11-10 11:56:51 +0000319 printf("%s: MMC Timeout\n"
320 " Interrupt status 0x%08x\n"
321 " Interrupt status enable 0x%08x\n"
322 " Interrupt signal enable 0x%08x\n"
323 " Present status 0x%08x\n",
324 __func__, mask,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600325 readl(&priv->reg->norintstsen),
326 readl(&priv->reg->norintsigen),
327 readl(&priv->reg->prnsts));
Anton staafbd348422011-11-10 11:56:51 +0000328 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000329 }
330 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600331 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000332 }
333
334 udelay(1000);
335 return 0;
336}
337
Simon Glass8c4c5c82017-04-23 20:02:11 -0600338static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600339 struct mmc_data *data)
Stephen Warrenf227e452012-11-06 11:27:30 +0000340{
341 void *buf;
342 unsigned int bbflags;
343 size_t len;
344 struct bounce_buffer bbstate;
345 int ret;
346
347 if (data) {
348 if (data->flags & MMC_DATA_READ) {
349 buf = data->dest;
350 bbflags = GEN_BB_WRITE;
351 } else {
352 buf = (void *)data->src;
353 bbflags = GEN_BB_READ;
354 }
355 len = data->blocks * data->blocksize;
356
357 bounce_buffer_start(&bbstate, buf, len, bbflags);
358 }
359
Simon Glass8c4c5c82017-04-23 20:02:11 -0600360 ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate);
Stephen Warrenf227e452012-11-06 11:27:30 +0000361
362 if (data)
363 bounce_buffer_stop(&bbstate);
364
365 return ret;
366}
367
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600368static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren85f0ee42011-05-31 10:30:37 +0000369{
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600370 ulong rate;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000371 int div;
Tom Warren85f0ee42011-05-31 10:30:37 +0000372 unsigned short clk;
373 unsigned long timeout;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000374
Tom Warren85f0ee42011-05-31 10:30:37 +0000375 debug(" mmc_change_clock called\n");
376
Simon Glassc2ea5e42011-09-21 12:40:04 +0000377 /*
Tom Warren35ae07b2013-02-26 12:31:26 -0700378 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glassc2ea5e42011-09-21 12:40:04 +0000379 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000380 if (clock == 0)
381 goto out;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600382
383 rate = clk_set_rate(&priv->clk, clock);
384 div = (rate + clock - 1) / clock;
Tom Warren4ff710a2019-06-03 16:06:34 -0700385
386#if defined(CONFIG_TEGRA210)
387 if (priv->mmc_id == PERIPH_ID_SDMMC1 && clock <= 400000) {
388 /* clock_adjust_periph_pll_div() chooses a 'bad' clock
389 * on SDMMC1 T210, so skip it here and force a clock
390 * that's been spec'd in the table in the TRM for
391 * card-detect (400KHz).
392 */
393 uint effective_rate = clock_adjust_periph_pll_div(priv->mmc_id,
394 CLOCK_ID_PERIPH, 24727273, NULL);
395 div = 62;
396
397 debug("%s: WAR: Using SDMMC1 clock of %u, div %d to achieve %dHz card clock ...\n",
398 __func__, effective_rate, div, clock);
399 } else {
400 clock_adjust_periph_pll_div(priv->mmc_id, CLOCK_ID_PERIPH,
401 clock, &div);
402 }
403#endif
Simon Glassc2ea5e42011-09-21 12:40:04 +0000404 debug("div = %d\n", div);
Tom Warren85f0ee42011-05-31 10:30:37 +0000405
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600406 writew(0, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000407
Tom Warren85f0ee42011-05-31 10:30:37 +0000408 /*
409 * CLKCON
410 * SELFREQ[15:8] : base clock divided by value
411 * ENSDCLK[2] : SD Clock Enable
412 * STBLINTCLK[1] : Internal Clock Stable
413 * ENINTCLK[0] : Internal Clock Enable
414 */
Simon Glassc2ea5e42011-09-21 12:40:04 +0000415 div >>= 1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000416 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
417 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600418 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000419
420 /* Wait max 10 ms */
421 timeout = 10;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600422 while (!(readw(&priv->reg->clkcon) &
Anton staaf0dfb31c2011-11-10 11:56:49 +0000423 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000424 if (timeout == 0) {
425 printf("%s: timeout error\n", __func__);
426 return;
427 }
428 timeout--;
429 udelay(1000);
430 }
431
Anton staaf0dfb31c2011-11-10 11:56:49 +0000432 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600433 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000434
435 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren85f0ee42011-05-31 10:30:37 +0000436
437out:
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600438 priv->clock = clock;
Tom Warren85f0ee42011-05-31 10:30:37 +0000439}
440
Simon Glass8c4c5c82017-04-23 20:02:11 -0600441static int tegra_mmc_set_ios(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000442{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600443 struct tegra_mmc_priv *priv = dev_get_priv(dev);
444 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000445 unsigned char ctrl;
446 debug(" mmc_set_ios called\n");
447
448 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
449
450 /* Change clock first */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600451 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren85f0ee42011-05-31 10:30:37 +0000452
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600453 ctrl = readb(&priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000454
455 /*
456 * WIDE8[5]
457 * 0 = Depend on WIDE4
458 * 1 = 8-bit mode
459 * WIDE4[1]
460 * 1 = 4-bit mode
461 * 0 = 1-bit mode
462 */
463 if (mmc->bus_width == 8)
464 ctrl |= (1 << 5);
465 else if (mmc->bus_width == 4)
466 ctrl |= (1 << 1);
467 else
Simon Glass9d6551a2017-06-07 21:11:48 -0600468 ctrl &= ~(1 << 1 | 1 << 5);
Tom Warren85f0ee42011-05-31 10:30:37 +0000469
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600470 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000471 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900472
473 return 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000474}
475
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600476static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600477{
Tom Warren2e86e812019-05-29 09:30:01 -0700478#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600479 u32 val;
Tom Warren2e86e812019-05-29 09:30:01 -0700480 u16 clk_con;
481 int timeout;
482 int id = priv->mmc_id;
Stephen Warrenc76e9362016-09-13 10:45:44 -0600483
Tom Warren2e86e812019-05-29 09:30:01 -0700484 debug("%s: sdmmc address = %p, id = %d\n", __func__,
485 priv->reg, id);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600486
487 /* Set the pad drive strength for SDMMC1 or 3 only */
Tom Warren2e86e812019-05-29 09:30:01 -0700488 if (id != PERIPH_ID_SDMMC1 && id != PERIPH_ID_SDMMC3) {
Stephen Warrenc76e9362016-09-13 10:45:44 -0600489 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
Tom Warren2e86e812019-05-29 09:30:01 -0700490 __func__);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600491 return;
492 }
493
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600494 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600495 val &= 0xFFFFFFF0;
496 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600497 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600498
Tom Warren2e86e812019-05-29 09:30:01 -0700499 /* Disable SD Clock Enable before running auto-cal as per TRM */
500 clk_con = readw(&priv->reg->clkcon);
501 debug("%s: CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
502 clk_con &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
503 writew(clk_con, &priv->reg->clkcon);
504
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600505 val = readl(&priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600506 val &= 0xFFFF0000;
Tom Warren2e86e812019-05-29 09:30:01 -0700507 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600508 writel(val, &priv->reg->autocalcfg);
Tom Warren2e86e812019-05-29 09:30:01 -0700509 val |= AUTO_CAL_START | AUTO_CAL_ENABLE;
510 writel(val, &priv->reg->autocalcfg);
511 debug("%s: AUTO_CAL_CFG = 0x%08X\n", __func__, val);
512 udelay(1);
513 timeout = 100; /* 10 mSec max (100*100uS) */
514 do {
515 val = readl(&priv->reg->autocalsts);
516 udelay(100);
517 } while ((val & AUTO_CAL_ACTIVE) && --timeout);
518 val = readl(&priv->reg->autocalsts);
519 debug("%s: Final AUTO_CAL_STATUS = 0x%08X, timeout = %d\n",
520 __func__, val, timeout);
521
522 /* Re-enable SD Clock Enable when auto-cal is done */
523 clk_con |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
524 writew(clk_con, &priv->reg->clkcon);
525 clk_con = readw(&priv->reg->clkcon);
526 debug("%s: final CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
527
528 if (timeout == 0) {
529 printf("%s: Warning: Autocal timed out!\n", __func__);
530 /* TBD: Set CFG2TMC_SDMMC1_PAD_CAL_DRV* regs here */
531 }
Tom Warren2e86e812019-05-29 09:30:01 -0700532#endif /* T30/T210 */
Stephen Warrenc76e9362016-09-13 10:45:44 -0600533}
534
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600535static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000536{
537 unsigned int timeout;
538 debug(" mmc_reset called\n");
539
540 /*
541 * RSTALL[0] : Software reset for all
542 * 1 = reset
543 * 0 = work
544 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600545 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren85f0ee42011-05-31 10:30:37 +0000546
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600547 priv->clock = 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000548
549 /* Wait max 100 ms */
550 timeout = 100;
551
552 /* hw clears the bit when it's done */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600553 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000554 if (timeout == 0) {
555 printf("%s: timeout error\n", __func__);
556 return;
557 }
558 timeout--;
559 udelay(1000);
560 }
Tom Warren35ae07b2013-02-26 12:31:26 -0700561
562 /* Set SD bus voltage & enable bus power */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600563 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren35ae07b2013-02-26 12:31:26 -0700564 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600565 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren35ae07b2013-02-26 12:31:26 -0700566
567 /* Make sure SDIO pads are set up */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600568 tegra_mmc_pad_init(priv);
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +0300569
570 if (!IS_ERR_VALUE(priv->tap_value) ||
571 !IS_ERR_VALUE(priv->trim_value)) {
572 u32 val;
573
574 val = readl(&priv->reg->venclkctl);
575
576 val &= ~TRIM_VAL_MASK;
577 val |= (priv->trim_value << TRIM_VAL_SHIFT);
578
579 val &= ~TAP_VAL_MASK;
580 val |= (priv->tap_value << TAP_VAL_SHIFT);
581
582 writel(val, &priv->reg->venclkctl);
583 debug("%s: VENDOR_CLOCK_CNTRL = 0x%08X\n", __func__, val);
584 }
Tom Warren85f0ee42011-05-31 10:30:37 +0000585}
586
Simon Glass8c4c5c82017-04-23 20:02:11 -0600587static int tegra_mmc_init(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000588{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600589 struct tegra_mmc_priv *priv = dev_get_priv(dev);
590 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000591 unsigned int mask;
Tom Warrena66f7722016-09-13 10:45:48 -0600592 debug(" tegra_mmc_init called\n");
Tom Warren85f0ee42011-05-31 10:30:37 +0000593
Tom Warren2e86e812019-05-29 09:30:01 -0700594#if defined(CONFIG_TEGRA210)
595 priv->mmc_id = clock_decode_periph_id(dev);
596 if (priv->mmc_id == PERIPH_ID_NONE) {
597 printf("%s: Missing/invalid peripheral ID\n", __func__);
598 return -EINVAL;
599 }
600#endif
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600601 tegra_mmc_reset(priv, mmc);
Tom Warren85f0ee42011-05-31 10:30:37 +0000602
Marcel Ziswiler86708852017-03-25 01:18:22 +0100603#if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK)
604 /*
605 * Disable the external clock loopback and use the internal one on
606 * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1
607 * bits being set to 0xfffd according to the TRM.
608 *
609 * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled
610 * approach once proper kernel integration made it mainline.
611 */
612 if (priv->reg == (void *)0x700b0400) {
613 mask = readl(&priv->reg->venmiscctl);
614 mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK;
615 writel(mask, &priv->reg->venmiscctl);
616 }
617#endif
618
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600619 priv->version = readw(&priv->reg->hcver);
620 debug("host version = %x\n", priv->version);
Tom Warren85f0ee42011-05-31 10:30:37 +0000621
622 /* mask all */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600623 writel(0xffffffff, &priv->reg->norintstsen);
624 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000625
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600626 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000627 /*
628 * NORMAL Interrupt Status Enable Register init
629 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
630 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf3ade2102011-11-10 11:56:50 +0000631 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren85f0ee42011-05-31 10:30:37 +0000632 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
633 * [0] ENSTACMDCMPLT : Command Complete Status Enable
634 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600635 mask = readl(&priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000636 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000637 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
638 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf3ade2102011-11-10 11:56:50 +0000639 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf0dfb31c2011-11-10 11:56:49 +0000640 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
641 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600642 writel(mask, &priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000643
644 /*
645 * NORMAL Interrupt Signal Enable Register init
646 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
647 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600648 mask = readl(&priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000649 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000650 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600651 writel(mask, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000652
653 return 0;
654}
655
Simon Glass8c4c5c82017-04-23 20:02:11 -0600656static int tegra_mmc_getcd(struct udevice *dev)
Thierry Redingf1494112012-01-02 01:15:39 +0000657{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600658 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Thierry Redingf1494112012-01-02 01:15:39 +0000659
Tom Warren22562a42012-09-04 17:00:24 -0700660 debug("tegra_mmc_getcd called\n");
Thierry Redingf1494112012-01-02 01:15:39 +0000661
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600662 if (dm_gpio_is_valid(&priv->cd_gpio))
663 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingf1494112012-01-02 01:15:39 +0000664
665 return 1;
666}
667
Simon Glass8c4c5c82017-04-23 20:02:11 -0600668static const struct dm_mmc_ops tegra_mmc_ops = {
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200669 .send_cmd = tegra_mmc_send_cmd,
670 .set_ios = tegra_mmc_set_ios,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600671 .get_cd = tegra_mmc_getcd,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200672};
673
Tom Warrena66f7722016-09-13 10:45:48 -0600674static int tegra_mmc_probe(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000675{
Tom Warrena66f7722016-09-13 10:45:48 -0600676 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700677 struct tegra_mmc_plat *plat = dev_get_plat(dev);
Tom Warrena66f7722016-09-13 10:45:48 -0600678 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600679 struct mmc_config *cfg = &plat->cfg;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600680 int bus_width, ret;
Tom Warren85f0ee42011-05-31 10:30:37 +0000681
Simon Glass8c4c5c82017-04-23 20:02:11 -0600682 cfg->name = dev->name;
Tom Warren85f0ee42011-05-31 10:30:37 +0000683
Simon Glass9c3b0e42017-07-25 08:30:08 -0600684 bus_width = dev_read_u32_default(dev, "bus-width", 1);
Tom Warrena66f7722016-09-13 10:45:48 -0600685
Simon Glass8c4c5c82017-04-23 20:02:11 -0600686 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
687 cfg->host_caps = 0;
Tom Warrena66f7722016-09-13 10:45:48 -0600688 if (bus_width == 8)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600689 cfg->host_caps |= MMC_MODE_8BIT;
Tom Warrena66f7722016-09-13 10:45:48 -0600690 if (bus_width >= 4)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600691 cfg->host_caps |= MMC_MODE_4BIT;
692 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren85f0ee42011-05-31 10:30:37 +0000693
694 /*
695 * min freq is for card identification, and is the highest
696 * low-speed SDIO card frequency (actually 400KHz)
697 * max freq is highest HS eMMC clock as per the SD/MMC spec
698 * (actually 52MHz)
Tom Warren85f0ee42011-05-31 10:30:37 +0000699 */
Simon Glass8c4c5c82017-04-23 20:02:11 -0600700 cfg->f_min = 375000;
Peter Geisa0c71062023-12-19 15:35:52 +0200701 cfg->f_max = dev_read_u32_default(dev, "max-frequency", 48000000);
Tom Warren85f0ee42011-05-31 10:30:37 +0000702
Simon Glass8c4c5c82017-04-23 20:02:11 -0600703 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200704
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100705 priv->reg = dev_read_addr_ptr(dev);
Tom Warren9745cf82013-02-21 12:31:30 +0000706
Tom Warrena66f7722016-09-13 10:45:48 -0600707 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
708 if (ret) {
709 debug("reset_get_by_name() failed: %d\n", ret);
710 return ret;
711 }
712 ret = clk_get_by_index(dev, 0, &priv->clk);
713 if (ret) {
714 debug("clk_get_by_index() failed: %d\n", ret);
715 return ret;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600716 }
Tom Warrena66f7722016-09-13 10:45:48 -0600717
718 ret = reset_assert(&priv->reset_ctl);
719 if (ret)
720 return ret;
721 ret = clk_enable(&priv->clk);
722 if (ret)
723 return ret;
724 ret = clk_set_rate(&priv->clk, 20000000);
725 if (IS_ERR_VALUE(ret))
726 return ret;
727 ret = reset_deassert(&priv->reset_ctl);
728 if (ret)
729 return ret;
Tom Warren9745cf82013-02-21 12:31:30 +0000730
Tom Warrena66f7722016-09-13 10:45:48 -0600731 /* These GPIOs are optional */
Simon Glass9c3b0e42017-07-25 08:30:08 -0600732 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
733 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
734 gpio_request_by_name(dev, "power-gpios", 0, &priv->pwr_gpio,
735 GPIOD_IS_OUT);
Tom Warrena66f7722016-09-13 10:45:48 -0600736 if (dm_gpio_is_valid(&priv->pwr_gpio))
737 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warren9745cf82013-02-21 12:31:30 +0000738
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +0300739 ret = dev_read_u32(dev, "nvidia,default-tap", &priv->tap_value);
740 if (ret)
741 priv->tap_value = ret;
742
743 ret = dev_read_u32(dev, "nvidia,default-trim", &priv->trim_value);
744 if (ret)
745 priv->trim_value = ret;
746
Simon Glass8c4c5c82017-04-23 20:02:11 -0600747 upriv->mmc = &plat->mmc;
Tom Warren9745cf82013-02-21 12:31:30 +0000748
Simon Glass8c4c5c82017-04-23 20:02:11 -0600749 return tegra_mmc_init(dev);
750}
Tom Warren9745cf82013-02-21 12:31:30 +0000751
Simon Glass8c4c5c82017-04-23 20:02:11 -0600752static int tegra_mmc_bind(struct udevice *dev)
753{
Simon Glassfa20e932020-12-03 16:55:20 -0700754 struct tegra_mmc_plat *plat = dev_get_plat(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600755
756 return mmc_bind(dev, &plat->mmc, &plat->cfg);
Tom Warren9745cf82013-02-21 12:31:30 +0000757}
758
Tom Warrena66f7722016-09-13 10:45:48 -0600759static const struct udevice_id tegra_mmc_ids[] = {
760 { .compatible = "nvidia,tegra20-sdhci" },
761 { .compatible = "nvidia,tegra30-sdhci" },
762 { .compatible = "nvidia,tegra114-sdhci" },
763 { .compatible = "nvidia,tegra124-sdhci" },
764 { .compatible = "nvidia,tegra210-sdhci" },
765 { .compatible = "nvidia,tegra186-sdhci" },
766 { }
767};
Tom Warren9745cf82013-02-21 12:31:30 +0000768
Tom Warrena66f7722016-09-13 10:45:48 -0600769U_BOOT_DRIVER(tegra_mmc_drv) = {
770 .name = "tegra_mmc",
771 .id = UCLASS_MMC,
772 .of_match = tegra_mmc_ids,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600773 .bind = tegra_mmc_bind,
Tom Warrena66f7722016-09-13 10:45:48 -0600774 .probe = tegra_mmc_probe,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600775 .ops = &tegra_mmc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700776 .plat_auto = sizeof(struct tegra_mmc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700777 .priv_auto = sizeof(struct tegra_mmc_priv),
Tom Warrena66f7722016-09-13 10:45:48 -0600778};