blob: 5ed7f01d3f361daa297280a6e468e49aa019ef1b [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>
Simon Glass11c89f32017-05-17 17:18:03 -060010#include <dm.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090011#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.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 Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070019#include <linux/err.h>
Tom Warren2e86e812019-05-29 09:30:01 -070020#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210)
21#include <asm/arch/clock.h>
22#endif
Tom Warren85f0ee42011-05-31 10:30:37 +000023
Simon Glass8c4c5c82017-04-23 20:02:11 -060024struct tegra_mmc_plat {
25 struct mmc_config cfg;
26 struct mmc mmc;
27};
28
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060029struct tegra_mmc_priv {
30 struct tegra_mmc *reg;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060031 struct reset_ctl reset_ctl;
32 struct clk clk;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060033 struct gpio_desc cd_gpio; /* Change Detect GPIO */
34 struct gpio_desc pwr_gpio; /* Power GPIO */
35 struct gpio_desc wp_gpio; /* Write Protect GPIO */
36 unsigned int version; /* SDHCI spec. version */
37 unsigned int clock; /* Current clock (MHz) */
Tom Warren2e86e812019-05-29 09:30:01 -070038 int mmc_id; /* peripheral id */
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +030039
40 int tap_value;
41 int trim_value;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060042};
43
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060044static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
45 unsigned short power)
Tom Warren35ae07b2013-02-26 12:31:26 -070046{
47 u8 pwr = 0;
48 debug("%s: power = %x\n", __func__, power);
49
50 if (power != (unsigned short)-1) {
51 switch (1 << power) {
52 case MMC_VDD_165_195:
53 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
54 break;
55 case MMC_VDD_29_30:
56 case MMC_VDD_30_31:
57 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
58 break;
59 case MMC_VDD_32_33:
60 case MMC_VDD_33_34:
61 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
62 break;
63 }
64 }
65 debug("%s: pwr = %X\n", __func__, pwr);
66
67 /* Set the bus voltage first (if any) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060068 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070069 if (pwr == 0)
70 return;
71
72 /* Now enable bus power */
73 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060074 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070075}
76
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060077static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
78 struct mmc_data *data,
79 struct bounce_buffer *bbstate)
Tom Warren85f0ee42011-05-31 10:30:37 +000080{
81 unsigned char ctrl;
82
Tom Warren85f0ee42011-05-31 10:30:37 +000083
Stephen Warrenf227e452012-11-06 11:27:30 +000084 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
85 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
86 data->blocksize);
87
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060088 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren85f0ee42011-05-31 10:30:37 +000089 /*
90 * DMASEL[4:3]
91 * 00 = Selects SDMA
92 * 01 = Reserved
93 * 10 = Selects 32-bit Address ADMA2
94 * 11 = Selects 64-bit Address ADMA2
95 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060096 ctrl = readb(&priv->reg->hostctl);
Anton staaf0dfb31c2011-11-10 11:56:49 +000097 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
98 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060099 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000100
101 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600102 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
103 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren85f0ee42011-05-31 10:30:37 +0000104}
105
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600106static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
107 struct mmc_data *data)
Tom Warren85f0ee42011-05-31 10:30:37 +0000108{
109 unsigned short mode;
110 debug(" mmc_set_transfer_mode called\n");
111 /*
112 * TRNMOD
113 * MUL1SIN0[5] : Multi/Single Block Select
114 * RD1WT0[4] : Data Transfer Direction Select
115 * 1 = read
116 * 0 = write
117 * ENACMD12[2] : Auto CMD12 Enable
118 * ENBLKCNT[1] : Block Count Enable
119 * ENDMA[0] : DMA Enable
120 */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000121 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
122 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
123
Tom Warren85f0ee42011-05-31 10:30:37 +0000124 if (data->blocks > 1)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000125 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
126
Tom Warren85f0ee42011-05-31 10:30:37 +0000127 if (data->flags & MMC_DATA_READ)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000128 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren85f0ee42011-05-31 10:30:37 +0000129
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600130 writew(mode, &priv->reg->trnmod);
Tom Warren85f0ee42011-05-31 10:30:37 +0000131}
132
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600133static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
134 struct mmc_cmd *cmd,
135 struct mmc_data *data,
136 unsigned int timeout)
Tom Warren85f0ee42011-05-31 10:30:37 +0000137{
Tom Warren85f0ee42011-05-31 10:30:37 +0000138 /*
139 * PRNSTS
Anton staaf5ab3fba2011-11-10 11:56:52 +0000140 * CMDINHDAT[1] : Command Inhibit (DAT)
141 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren85f0ee42011-05-31 10:30:37 +0000142 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000143 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren85f0ee42011-05-31 10:30:37 +0000144
145 /*
146 * We shouldn't wait for data inhibit for stop commands, even
147 * though they might use busy signaling
148 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000149 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
150 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000151
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600152 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000153 if (timeout == 0) {
154 printf("%s: timeout error\n", __func__);
155 return -1;
156 }
157 timeout--;
158 udelay(1000);
159 }
160
Anton staaf5ab3fba2011-11-10 11:56:52 +0000161 return 0;
162}
163
Simon Glass8c4c5c82017-04-23 20:02:11 -0600164static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600165 struct mmc_data *data,
166 struct bounce_buffer *bbstate)
Anton staaf5ab3fba2011-11-10 11:56:52 +0000167{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600168 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000169 int flags, i;
170 int result;
Anatolij Gustschine1f53412012-03-28 03:40:00 +0000171 unsigned int mask = 0;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000172 unsigned int retry = 0x100000;
173 debug(" mmc_send_cmd called\n");
174
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600175 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000176
177 if (result < 0)
178 return result;
179
Tom Warren85f0ee42011-05-31 10:30:37 +0000180 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600181 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren85f0ee42011-05-31 10:30:37 +0000182
183 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600184 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren85f0ee42011-05-31 10:30:37 +0000185
186 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600187 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren85f0ee42011-05-31 10:30:37 +0000188
189 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
190 return -1;
191
192 /*
193 * CMDREG
194 * CMDIDX[13:8] : Command index
195 * DATAPRNT[5] : Data Present Select
196 * ENCMDIDX[4] : Command Index Check Enable
197 * ENCMDCRC[3] : Command CRC Check Enable
198 * RSPTYP[1:0]
199 * 00 = No Response
200 * 01 = Length 136
201 * 10 = Length 48
202 * 11 = Length 48 Check busy after response
203 */
204 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf0dfb31c2011-11-10 11:56:49 +0000205 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren85f0ee42011-05-31 10:30:37 +0000206 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000207 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren85f0ee42011-05-31 10:30:37 +0000208 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000209 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren85f0ee42011-05-31 10:30:37 +0000210 else
Anton staaf0dfb31c2011-11-10 11:56:49 +0000211 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren85f0ee42011-05-31 10:30:37 +0000212
213 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000214 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000215 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000216 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000217 if (data)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000218 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren85f0ee42011-05-31 10:30:37 +0000219
220 debug("cmd: %d\n", cmd->cmdidx);
221
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600222 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren85f0ee42011-05-31 10:30:37 +0000223
224 for (i = 0; i < retry; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600225 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000226 /* Command Complete */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000227 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000228 if (!data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600229 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000230 break;
231 }
232 }
233
234 if (i == retry) {
235 printf("%s: waiting for status update\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600236 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900237 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000238 }
239
Anton staaf0dfb31c2011-11-10 11:56:49 +0000240 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000241 /* Timeout Error */
242 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600243 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900244 return -ETIMEDOUT;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000245 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000246 /* Error Interrupt */
247 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600248 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000249 return -1;
250 }
251
252 if (cmd->resp_type & MMC_RSP_PRESENT) {
253 if (cmd->resp_type & MMC_RSP_136) {
254 /* CRC is stripped so we need to do some shifting. */
255 for (i = 0; i < 4; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600256 unsigned long offset = (unsigned long)
257 (&priv->reg->rspreg3 - i);
Tom Warren85f0ee42011-05-31 10:30:37 +0000258 cmd->response[i] = readl(offset) << 8;
259
260 if (i != 3) {
261 cmd->response[i] |=
262 readb(offset - 1);
263 }
264 debug("cmd->resp[%d]: %08x\n",
265 i, cmd->response[i]);
266 }
267 } else if (cmd->resp_type & MMC_RSP_BUSY) {
268 for (i = 0; i < retry; i++) {
269 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600270 if (readl(&priv->reg->prnsts)
Tom Warren85f0ee42011-05-31 10:30:37 +0000271 & (1 << 20)) /* DAT[0] */
272 break;
273 }
274
275 if (i == retry) {
276 printf("%s: card is still busy\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600277 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900278 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000279 }
280
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600281 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000282 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
283 } else {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600284 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000285 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
286 }
287 }
288
289 if (data) {
Anton staafbd348422011-11-10 11:56:51 +0000290 unsigned long start = get_timer(0);
291
Tom Warren85f0ee42011-05-31 10:30:37 +0000292 while (1) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600293 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000294
Anton staaf0dfb31c2011-11-10 11:56:49 +0000295 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000296 /* Error Interrupt */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600297 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000298 printf("%s: error during transfer: 0x%08x\n",
299 __func__, mask);
300 return -1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000301 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf3ade2102011-11-10 11:56:50 +0000302 /*
303 * DMA Interrupt, restart the transfer where
304 * it was interrupted.
305 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600306 unsigned int address = readl(&priv->reg->sysad);
Anton staaf3ade2102011-11-10 11:56:50 +0000307
Tom Warren85f0ee42011-05-31 10:30:37 +0000308 debug("DMA end\n");
Anton staaf3ade2102011-11-10 11:56:50 +0000309 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600310 &priv->reg->norintsts);
311 writel(address, &priv->reg->sysad);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000312 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000313 /* Transfer Complete */
314 debug("r/w is done\n");
315 break;
Marcel Ziswilere1207e92014-10-04 01:48:53 +0200316 } else if (get_timer(start) > 8000UL) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600317 writel(mask, &priv->reg->norintsts);
Anton staafbd348422011-11-10 11:56:51 +0000318 printf("%s: MMC Timeout\n"
319 " Interrupt status 0x%08x\n"
320 " Interrupt status enable 0x%08x\n"
321 " Interrupt signal enable 0x%08x\n"
322 " Present status 0x%08x\n",
323 __func__, mask,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600324 readl(&priv->reg->norintstsen),
325 readl(&priv->reg->norintsigen),
326 readl(&priv->reg->prnsts));
Anton staafbd348422011-11-10 11:56:51 +0000327 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000328 }
329 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600330 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000331 }
332
333 udelay(1000);
334 return 0;
335}
336
Simon Glass8c4c5c82017-04-23 20:02:11 -0600337static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600338 struct mmc_data *data)
Stephen Warrenf227e452012-11-06 11:27:30 +0000339{
340 void *buf;
341 unsigned int bbflags;
342 size_t len;
343 struct bounce_buffer bbstate;
344 int ret;
345
346 if (data) {
347 if (data->flags & MMC_DATA_READ) {
348 buf = data->dest;
349 bbflags = GEN_BB_WRITE;
350 } else {
351 buf = (void *)data->src;
352 bbflags = GEN_BB_READ;
353 }
354 len = data->blocks * data->blocksize;
355
356 bounce_buffer_start(&bbstate, buf, len, bbflags);
357 }
358
Simon Glass8c4c5c82017-04-23 20:02:11 -0600359 ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate);
Stephen Warrenf227e452012-11-06 11:27:30 +0000360
361 if (data)
362 bounce_buffer_stop(&bbstate);
363
364 return ret;
365}
366
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600367static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren85f0ee42011-05-31 10:30:37 +0000368{
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600369 ulong rate;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000370 int div;
Tom Warren85f0ee42011-05-31 10:30:37 +0000371 unsigned short clk;
372 unsigned long timeout;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000373
Tom Warren85f0ee42011-05-31 10:30:37 +0000374 debug(" mmc_change_clock called\n");
375
Simon Glassc2ea5e42011-09-21 12:40:04 +0000376 /*
Tom Warren35ae07b2013-02-26 12:31:26 -0700377 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glassc2ea5e42011-09-21 12:40:04 +0000378 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000379 if (clock == 0)
380 goto out;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600381
382 rate = clk_set_rate(&priv->clk, clock);
383 div = (rate + clock - 1) / clock;
Tom Warren4ff710a2019-06-03 16:06:34 -0700384
385#if defined(CONFIG_TEGRA210)
386 if (priv->mmc_id == PERIPH_ID_SDMMC1 && clock <= 400000) {
387 /* clock_adjust_periph_pll_div() chooses a 'bad' clock
388 * on SDMMC1 T210, so skip it here and force a clock
389 * that's been spec'd in the table in the TRM for
390 * card-detect (400KHz).
391 */
392 uint effective_rate = clock_adjust_periph_pll_div(priv->mmc_id,
393 CLOCK_ID_PERIPH, 24727273, NULL);
394 div = 62;
395
396 debug("%s: WAR: Using SDMMC1 clock of %u, div %d to achieve %dHz card clock ...\n",
397 __func__, effective_rate, div, clock);
398 } else {
399 clock_adjust_periph_pll_div(priv->mmc_id, CLOCK_ID_PERIPH,
400 clock, &div);
401 }
402#endif
Simon Glassc2ea5e42011-09-21 12:40:04 +0000403 debug("div = %d\n", div);
Tom Warren85f0ee42011-05-31 10:30:37 +0000404
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600405 writew(0, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000406
Tom Warren85f0ee42011-05-31 10:30:37 +0000407 /*
408 * CLKCON
409 * SELFREQ[15:8] : base clock divided by value
410 * ENSDCLK[2] : SD Clock Enable
411 * STBLINTCLK[1] : Internal Clock Stable
412 * ENINTCLK[0] : Internal Clock Enable
413 */
Simon Glassc2ea5e42011-09-21 12:40:04 +0000414 div >>= 1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000415 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
416 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600417 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000418
419 /* Wait max 10 ms */
420 timeout = 10;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600421 while (!(readw(&priv->reg->clkcon) &
Anton staaf0dfb31c2011-11-10 11:56:49 +0000422 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000423 if (timeout == 0) {
424 printf("%s: timeout error\n", __func__);
425 return;
426 }
427 timeout--;
428 udelay(1000);
429 }
430
Anton staaf0dfb31c2011-11-10 11:56:49 +0000431 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600432 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000433
434 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren85f0ee42011-05-31 10:30:37 +0000435
436out:
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600437 priv->clock = clock;
Tom Warren85f0ee42011-05-31 10:30:37 +0000438}
439
Simon Glass8c4c5c82017-04-23 20:02:11 -0600440static int tegra_mmc_set_ios(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000441{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600442 struct tegra_mmc_priv *priv = dev_get_priv(dev);
443 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000444 unsigned char ctrl;
445 debug(" mmc_set_ios called\n");
446
447 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
448
449 /* Change clock first */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600450 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren85f0ee42011-05-31 10:30:37 +0000451
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600452 ctrl = readb(&priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000453
454 /*
455 * WIDE8[5]
456 * 0 = Depend on WIDE4
457 * 1 = 8-bit mode
458 * WIDE4[1]
459 * 1 = 4-bit mode
460 * 0 = 1-bit mode
461 */
462 if (mmc->bus_width == 8)
463 ctrl |= (1 << 5);
464 else if (mmc->bus_width == 4)
465 ctrl |= (1 << 1);
466 else
Simon Glass9d6551a2017-06-07 21:11:48 -0600467 ctrl &= ~(1 << 1 | 1 << 5);
Tom Warren85f0ee42011-05-31 10:30:37 +0000468
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600469 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000470 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900471
472 return 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000473}
474
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600475static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600476{
Tom Warren2e86e812019-05-29 09:30:01 -0700477#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600478 u32 val;
Tom Warren2e86e812019-05-29 09:30:01 -0700479 u16 clk_con;
480 int timeout;
481 int id = priv->mmc_id;
Stephen Warrenc76e9362016-09-13 10:45:44 -0600482
Tom Warren2e86e812019-05-29 09:30:01 -0700483 debug("%s: sdmmc address = %p, id = %d\n", __func__,
484 priv->reg, id);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600485
486 /* Set the pad drive strength for SDMMC1 or 3 only */
Tom Warren2e86e812019-05-29 09:30:01 -0700487 if (id != PERIPH_ID_SDMMC1 && id != PERIPH_ID_SDMMC3) {
Stephen Warrenc76e9362016-09-13 10:45:44 -0600488 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
Tom Warren2e86e812019-05-29 09:30:01 -0700489 __func__);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600490 return;
491 }
492
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600493 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600494 val &= 0xFFFFFFF0;
495 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600496 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600497
Tom Warren2e86e812019-05-29 09:30:01 -0700498 /* Disable SD Clock Enable before running auto-cal as per TRM */
499 clk_con = readw(&priv->reg->clkcon);
500 debug("%s: CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
501 clk_con &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
502 writew(clk_con, &priv->reg->clkcon);
503
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600504 val = readl(&priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600505 val &= 0xFFFF0000;
Tom Warren2e86e812019-05-29 09:30:01 -0700506 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600507 writel(val, &priv->reg->autocalcfg);
Tom Warren2e86e812019-05-29 09:30:01 -0700508 val |= AUTO_CAL_START | AUTO_CAL_ENABLE;
509 writel(val, &priv->reg->autocalcfg);
510 debug("%s: AUTO_CAL_CFG = 0x%08X\n", __func__, val);
511 udelay(1);
512 timeout = 100; /* 10 mSec max (100*100uS) */
513 do {
514 val = readl(&priv->reg->autocalsts);
515 udelay(100);
516 } while ((val & AUTO_CAL_ACTIVE) && --timeout);
517 val = readl(&priv->reg->autocalsts);
518 debug("%s: Final AUTO_CAL_STATUS = 0x%08X, timeout = %d\n",
519 __func__, val, timeout);
520
521 /* Re-enable SD Clock Enable when auto-cal is done */
522 clk_con |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
523 writew(clk_con, &priv->reg->clkcon);
524 clk_con = readw(&priv->reg->clkcon);
525 debug("%s: final CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
526
527 if (timeout == 0) {
528 printf("%s: Warning: Autocal timed out!\n", __func__);
529 /* TBD: Set CFG2TMC_SDMMC1_PAD_CAL_DRV* regs here */
530 }
Tom Warren2e86e812019-05-29 09:30:01 -0700531#endif /* T30/T210 */
Stephen Warrenc76e9362016-09-13 10:45:44 -0600532}
533
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600534static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000535{
536 unsigned int timeout;
537 debug(" mmc_reset called\n");
538
539 /*
540 * RSTALL[0] : Software reset for all
541 * 1 = reset
542 * 0 = work
543 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600544 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren85f0ee42011-05-31 10:30:37 +0000545
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600546 priv->clock = 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000547
548 /* Wait max 100 ms */
549 timeout = 100;
550
551 /* hw clears the bit when it's done */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600552 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000553 if (timeout == 0) {
554 printf("%s: timeout error\n", __func__);
555 return;
556 }
557 timeout--;
558 udelay(1000);
559 }
Tom Warren35ae07b2013-02-26 12:31:26 -0700560
561 /* Set SD bus voltage & enable bus power */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600562 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren35ae07b2013-02-26 12:31:26 -0700563 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600564 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren35ae07b2013-02-26 12:31:26 -0700565
566 /* Make sure SDIO pads are set up */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600567 tegra_mmc_pad_init(priv);
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +0300568
569 if (!IS_ERR_VALUE(priv->tap_value) ||
570 !IS_ERR_VALUE(priv->trim_value)) {
571 u32 val;
572
573 val = readl(&priv->reg->venclkctl);
574
575 val &= ~TRIM_VAL_MASK;
576 val |= (priv->trim_value << TRIM_VAL_SHIFT);
577
578 val &= ~TAP_VAL_MASK;
579 val |= (priv->tap_value << TAP_VAL_SHIFT);
580
581 writel(val, &priv->reg->venclkctl);
582 debug("%s: VENDOR_CLOCK_CNTRL = 0x%08X\n", __func__, val);
583 }
Tom Warren85f0ee42011-05-31 10:30:37 +0000584}
585
Simon Glass8c4c5c82017-04-23 20:02:11 -0600586static int tegra_mmc_init(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000587{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600588 struct tegra_mmc_priv *priv = dev_get_priv(dev);
589 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000590 unsigned int mask;
Tom Warrena66f7722016-09-13 10:45:48 -0600591 debug(" tegra_mmc_init called\n");
Tom Warren85f0ee42011-05-31 10:30:37 +0000592
Tom Warren2e86e812019-05-29 09:30:01 -0700593#if defined(CONFIG_TEGRA210)
594 priv->mmc_id = clock_decode_periph_id(dev);
595 if (priv->mmc_id == PERIPH_ID_NONE) {
596 printf("%s: Missing/invalid peripheral ID\n", __func__);
597 return -EINVAL;
598 }
599#endif
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600600 tegra_mmc_reset(priv, mmc);
Tom Warren85f0ee42011-05-31 10:30:37 +0000601
Marcel Ziswiler86708852017-03-25 01:18:22 +0100602#if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK)
603 /*
604 * Disable the external clock loopback and use the internal one on
605 * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1
606 * bits being set to 0xfffd according to the TRM.
607 *
608 * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled
609 * approach once proper kernel integration made it mainline.
610 */
611 if (priv->reg == (void *)0x700b0400) {
612 mask = readl(&priv->reg->venmiscctl);
613 mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK;
614 writel(mask, &priv->reg->venmiscctl);
615 }
616#endif
617
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600618 priv->version = readw(&priv->reg->hcver);
619 debug("host version = %x\n", priv->version);
Tom Warren85f0ee42011-05-31 10:30:37 +0000620
621 /* mask all */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600622 writel(0xffffffff, &priv->reg->norintstsen);
623 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000624
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600625 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000626 /*
627 * NORMAL Interrupt Status Enable Register init
628 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
629 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf3ade2102011-11-10 11:56:50 +0000630 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren85f0ee42011-05-31 10:30:37 +0000631 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
632 * [0] ENSTACMDCMPLT : Command Complete Status Enable
633 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600634 mask = readl(&priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000635 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000636 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
637 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf3ade2102011-11-10 11:56:50 +0000638 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf0dfb31c2011-11-10 11:56:49 +0000639 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
640 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600641 writel(mask, &priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000642
643 /*
644 * NORMAL Interrupt Signal Enable Register init
645 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
646 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600647 mask = readl(&priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000648 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000649 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600650 writel(mask, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000651
652 return 0;
653}
654
Simon Glass8c4c5c82017-04-23 20:02:11 -0600655static int tegra_mmc_getcd(struct udevice *dev)
Thierry Redingf1494112012-01-02 01:15:39 +0000656{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600657 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Thierry Redingf1494112012-01-02 01:15:39 +0000658
Tom Warren22562a42012-09-04 17:00:24 -0700659 debug("tegra_mmc_getcd called\n");
Thierry Redingf1494112012-01-02 01:15:39 +0000660
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600661 if (dm_gpio_is_valid(&priv->cd_gpio))
662 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingf1494112012-01-02 01:15:39 +0000663
664 return 1;
665}
666
Simon Glass8c4c5c82017-04-23 20:02:11 -0600667static const struct dm_mmc_ops tegra_mmc_ops = {
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200668 .send_cmd = tegra_mmc_send_cmd,
669 .set_ios = tegra_mmc_set_ios,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600670 .get_cd = tegra_mmc_getcd,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200671};
672
Tom Warrena66f7722016-09-13 10:45:48 -0600673static int tegra_mmc_probe(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000674{
Tom Warrena66f7722016-09-13 10:45:48 -0600675 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700676 struct tegra_mmc_plat *plat = dev_get_plat(dev);
Tom Warrena66f7722016-09-13 10:45:48 -0600677 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600678 struct mmc_config *cfg = &plat->cfg;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600679 int bus_width, ret;
Tom Warren85f0ee42011-05-31 10:30:37 +0000680
Simon Glass8c4c5c82017-04-23 20:02:11 -0600681 cfg->name = dev->name;
Tom Warren85f0ee42011-05-31 10:30:37 +0000682
Simon Glass9c3b0e42017-07-25 08:30:08 -0600683 bus_width = dev_read_u32_default(dev, "bus-width", 1);
Tom Warrena66f7722016-09-13 10:45:48 -0600684
Simon Glass8c4c5c82017-04-23 20:02:11 -0600685 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
686 cfg->host_caps = 0;
Tom Warrena66f7722016-09-13 10:45:48 -0600687 if (bus_width == 8)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600688 cfg->host_caps |= MMC_MODE_8BIT;
Tom Warrena66f7722016-09-13 10:45:48 -0600689 if (bus_width >= 4)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600690 cfg->host_caps |= MMC_MODE_4BIT;
691 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren85f0ee42011-05-31 10:30:37 +0000692
693 /*
694 * min freq is for card identification, and is the highest
695 * low-speed SDIO card frequency (actually 400KHz)
696 * max freq is highest HS eMMC clock as per the SD/MMC spec
697 * (actually 52MHz)
Tom Warren85f0ee42011-05-31 10:30:37 +0000698 */
Simon Glass8c4c5c82017-04-23 20:02:11 -0600699 cfg->f_min = 375000;
Peter Geisa0c71062023-12-19 15:35:52 +0200700 cfg->f_max = dev_read_u32_default(dev, "max-frequency", 48000000);
Tom Warren85f0ee42011-05-31 10:30:37 +0000701
Simon Glass8c4c5c82017-04-23 20:02:11 -0600702 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200703
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100704 priv->reg = dev_read_addr_ptr(dev);
Tom Warren9745cf82013-02-21 12:31:30 +0000705
Tom Warrena66f7722016-09-13 10:45:48 -0600706 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
707 if (ret) {
708 debug("reset_get_by_name() failed: %d\n", ret);
709 return ret;
710 }
711 ret = clk_get_by_index(dev, 0, &priv->clk);
712 if (ret) {
713 debug("clk_get_by_index() failed: %d\n", ret);
714 return ret;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600715 }
Tom Warrena66f7722016-09-13 10:45:48 -0600716
717 ret = reset_assert(&priv->reset_ctl);
718 if (ret)
719 return ret;
720 ret = clk_enable(&priv->clk);
721 if (ret)
722 return ret;
723 ret = clk_set_rate(&priv->clk, 20000000);
724 if (IS_ERR_VALUE(ret))
725 return ret;
726 ret = reset_deassert(&priv->reset_ctl);
727 if (ret)
728 return ret;
Tom Warren9745cf82013-02-21 12:31:30 +0000729
Tom Warrena66f7722016-09-13 10:45:48 -0600730 /* These GPIOs are optional */
Simon Glass9c3b0e42017-07-25 08:30:08 -0600731 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
732 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
733 gpio_request_by_name(dev, "power-gpios", 0, &priv->pwr_gpio,
734 GPIOD_IS_OUT);
Tom Warrena66f7722016-09-13 10:45:48 -0600735 if (dm_gpio_is_valid(&priv->pwr_gpio))
736 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warren9745cf82013-02-21 12:31:30 +0000737
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +0300738 ret = dev_read_u32(dev, "nvidia,default-tap", &priv->tap_value);
739 if (ret)
740 priv->tap_value = ret;
741
742 ret = dev_read_u32(dev, "nvidia,default-trim", &priv->trim_value);
743 if (ret)
744 priv->trim_value = ret;
745
Simon Glass8c4c5c82017-04-23 20:02:11 -0600746 upriv->mmc = &plat->mmc;
Tom Warren9745cf82013-02-21 12:31:30 +0000747
Simon Glass8c4c5c82017-04-23 20:02:11 -0600748 return tegra_mmc_init(dev);
749}
Tom Warren9745cf82013-02-21 12:31:30 +0000750
Simon Glass8c4c5c82017-04-23 20:02:11 -0600751static int tegra_mmc_bind(struct udevice *dev)
752{
Simon Glassfa20e932020-12-03 16:55:20 -0700753 struct tegra_mmc_plat *plat = dev_get_plat(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600754
755 return mmc_bind(dev, &plat->mmc, &plat->cfg);
Tom Warren9745cf82013-02-21 12:31:30 +0000756}
757
Tom Warrena66f7722016-09-13 10:45:48 -0600758static const struct udevice_id tegra_mmc_ids[] = {
759 { .compatible = "nvidia,tegra20-sdhci" },
760 { .compatible = "nvidia,tegra30-sdhci" },
761 { .compatible = "nvidia,tegra114-sdhci" },
762 { .compatible = "nvidia,tegra124-sdhci" },
763 { .compatible = "nvidia,tegra210-sdhci" },
764 { .compatible = "nvidia,tegra186-sdhci" },
765 { }
766};
Tom Warren9745cf82013-02-21 12:31:30 +0000767
Tom Warrena66f7722016-09-13 10:45:48 -0600768U_BOOT_DRIVER(tegra_mmc_drv) = {
769 .name = "tegra_mmc",
770 .id = UCLASS_MMC,
771 .of_match = tegra_mmc_ids,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600772 .bind = tegra_mmc_bind,
Tom Warrena66f7722016-09-13 10:45:48 -0600773 .probe = tegra_mmc_probe,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600774 .ops = &tegra_mmc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700775 .plat_auto = sizeof(struct tegra_mmc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700776 .priv_auto = sizeof(struct tegra_mmc_priv),
Tom Warrena66f7722016-09-13 10:45:48 -0600777};