blob: 355991beb0fd4d9b0f99751a42a236485abcdaac [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
Stephen Warrenf227e452012-11-06 11:27:30 +000083 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
84 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
85 data->blocksize);
86
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060087 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren85f0ee42011-05-31 10:30:37 +000088 /*
89 * DMASEL[4:3]
90 * 00 = Selects SDMA
91 * 01 = Reserved
92 * 10 = Selects 32-bit Address ADMA2
93 * 11 = Selects 64-bit Address ADMA2
94 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060095 ctrl = readb(&priv->reg->hostctl);
Anton staaf0dfb31c2011-11-10 11:56:49 +000096 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
97 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060098 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +000099
100 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600101 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
102 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren85f0ee42011-05-31 10:30:37 +0000103}
104
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600105static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
106 struct mmc_data *data)
Tom Warren85f0ee42011-05-31 10:30:37 +0000107{
108 unsigned short mode;
109 debug(" mmc_set_transfer_mode called\n");
110 /*
111 * TRNMOD
112 * MUL1SIN0[5] : Multi/Single Block Select
113 * RD1WT0[4] : Data Transfer Direction Select
114 * 1 = read
115 * 0 = write
116 * ENACMD12[2] : Auto CMD12 Enable
117 * ENBLKCNT[1] : Block Count Enable
118 * ENDMA[0] : DMA Enable
119 */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000120 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
121 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
122
Tom Warren85f0ee42011-05-31 10:30:37 +0000123 if (data->blocks > 1)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000124 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
125
Tom Warren85f0ee42011-05-31 10:30:37 +0000126 if (data->flags & MMC_DATA_READ)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000127 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren85f0ee42011-05-31 10:30:37 +0000128
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600129 writew(mode, &priv->reg->trnmod);
Tom Warren85f0ee42011-05-31 10:30:37 +0000130}
131
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600132static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
133 struct mmc_cmd *cmd,
134 struct mmc_data *data,
135 unsigned int timeout)
Tom Warren85f0ee42011-05-31 10:30:37 +0000136{
Tom Warren85f0ee42011-05-31 10:30:37 +0000137 /*
138 * PRNSTS
Anton staaf5ab3fba2011-11-10 11:56:52 +0000139 * CMDINHDAT[1] : Command Inhibit (DAT)
140 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren85f0ee42011-05-31 10:30:37 +0000141 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000142 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren85f0ee42011-05-31 10:30:37 +0000143
144 /*
145 * We shouldn't wait for data inhibit for stop commands, even
146 * though they might use busy signaling
147 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000148 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
149 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000150
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600151 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000152 if (timeout == 0) {
153 printf("%s: timeout error\n", __func__);
154 return -1;
155 }
156 timeout--;
157 udelay(1000);
158 }
159
Anton staaf5ab3fba2011-11-10 11:56:52 +0000160 return 0;
161}
162
Simon Glass8c4c5c82017-04-23 20:02:11 -0600163static int tegra_mmc_send_cmd_bounced(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600164 struct mmc_data *data,
165 struct bounce_buffer *bbstate)
Anton staaf5ab3fba2011-11-10 11:56:52 +0000166{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600167 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000168 int flags, i;
169 int result;
Anatolij Gustschine1f53412012-03-28 03:40:00 +0000170 unsigned int mask = 0;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000171 unsigned int retry = 0x100000;
172 debug(" mmc_send_cmd called\n");
173
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600174 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000175
176 if (result < 0)
177 return result;
178
Tom Warren85f0ee42011-05-31 10:30:37 +0000179 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600180 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren85f0ee42011-05-31 10:30:37 +0000181
182 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600183 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren85f0ee42011-05-31 10:30:37 +0000184
185 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600186 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren85f0ee42011-05-31 10:30:37 +0000187
188 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
189 return -1;
190
191 /*
192 * CMDREG
193 * CMDIDX[13:8] : Command index
194 * DATAPRNT[5] : Data Present Select
195 * ENCMDIDX[4] : Command Index Check Enable
196 * ENCMDCRC[3] : Command CRC Check Enable
197 * RSPTYP[1:0]
198 * 00 = No Response
199 * 01 = Length 136
200 * 10 = Length 48
201 * 11 = Length 48 Check busy after response
202 */
203 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf0dfb31c2011-11-10 11:56:49 +0000204 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren85f0ee42011-05-31 10:30:37 +0000205 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000206 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren85f0ee42011-05-31 10:30:37 +0000207 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000208 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren85f0ee42011-05-31 10:30:37 +0000209 else
Anton staaf0dfb31c2011-11-10 11:56:49 +0000210 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren85f0ee42011-05-31 10:30:37 +0000211
212 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000213 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000214 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000215 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000216 if (data)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000217 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren85f0ee42011-05-31 10:30:37 +0000218
219 debug("cmd: %d\n", cmd->cmdidx);
220
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600221 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren85f0ee42011-05-31 10:30:37 +0000222
223 for (i = 0; i < retry; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600224 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000225 /* Command Complete */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000226 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000227 if (!data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600228 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000229 break;
230 }
231 }
232
233 if (i == retry) {
234 printf("%s: waiting for status update\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600235 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900236 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000237 }
238
Anton staaf0dfb31c2011-11-10 11:56:49 +0000239 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000240 /* Timeout Error */
241 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600242 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900243 return -ETIMEDOUT;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000244 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000245 /* Error Interrupt */
246 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600247 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000248 return -1;
249 }
250
251 if (cmd->resp_type & MMC_RSP_PRESENT) {
252 if (cmd->resp_type & MMC_RSP_136) {
253 /* CRC is stripped so we need to do some shifting. */
254 for (i = 0; i < 4; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600255 unsigned long offset = (unsigned long)
256 (&priv->reg->rspreg3 - i);
Tom Warren85f0ee42011-05-31 10:30:37 +0000257 cmd->response[i] = readl(offset) << 8;
258
259 if (i != 3) {
260 cmd->response[i] |=
261 readb(offset - 1);
262 }
263 debug("cmd->resp[%d]: %08x\n",
264 i, cmd->response[i]);
265 }
266 } else if (cmd->resp_type & MMC_RSP_BUSY) {
267 for (i = 0; i < retry; i++) {
268 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600269 if (readl(&priv->reg->prnsts)
Tom Warren85f0ee42011-05-31 10:30:37 +0000270 & (1 << 20)) /* DAT[0] */
271 break;
272 }
273
274 if (i == retry) {
275 printf("%s: card is still busy\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600276 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900277 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000278 }
279
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600280 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000281 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
282 } else {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600283 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000284 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
285 }
286 }
287
288 if (data) {
Anton staafbd348422011-11-10 11:56:51 +0000289 unsigned long start = get_timer(0);
290
Tom Warren85f0ee42011-05-31 10:30:37 +0000291 while (1) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600292 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000293
Anton staaf0dfb31c2011-11-10 11:56:49 +0000294 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000295 /* Error Interrupt */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600296 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000297 printf("%s: error during transfer: 0x%08x\n",
298 __func__, mask);
299 return -1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000300 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf3ade2102011-11-10 11:56:50 +0000301 /*
302 * DMA Interrupt, restart the transfer where
303 * it was interrupted.
304 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600305 unsigned int address = readl(&priv->reg->sysad);
Anton staaf3ade2102011-11-10 11:56:50 +0000306
Tom Warren85f0ee42011-05-31 10:30:37 +0000307 debug("DMA end\n");
Anton staaf3ade2102011-11-10 11:56:50 +0000308 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600309 &priv->reg->norintsts);
310 writel(address, &priv->reg->sysad);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000311 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000312 /* Transfer Complete */
313 debug("r/w is done\n");
314 break;
Marcel Ziswilere1207e92014-10-04 01:48:53 +0200315 } else if (get_timer(start) > 8000UL) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600316 writel(mask, &priv->reg->norintsts);
Anton staafbd348422011-11-10 11:56:51 +0000317 printf("%s: MMC Timeout\n"
318 " Interrupt status 0x%08x\n"
319 " Interrupt status enable 0x%08x\n"
320 " Interrupt signal enable 0x%08x\n"
321 " Present status 0x%08x\n",
322 __func__, mask,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600323 readl(&priv->reg->norintstsen),
324 readl(&priv->reg->norintsigen),
325 readl(&priv->reg->prnsts));
Anton staafbd348422011-11-10 11:56:51 +0000326 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000327 }
328 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600329 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000330 }
331
332 udelay(1000);
333 return 0;
334}
335
Simon Glass8c4c5c82017-04-23 20:02:11 -0600336static int tegra_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600337 struct mmc_data *data)
Stephen Warrenf227e452012-11-06 11:27:30 +0000338{
339 void *buf;
340 unsigned int bbflags;
341 size_t len;
342 struct bounce_buffer bbstate;
343 int ret;
344
345 if (data) {
346 if (data->flags & MMC_DATA_READ) {
347 buf = data->dest;
348 bbflags = GEN_BB_WRITE;
349 } else {
350 buf = (void *)data->src;
351 bbflags = GEN_BB_READ;
352 }
353 len = data->blocks * data->blocksize;
354
355 bounce_buffer_start(&bbstate, buf, len, bbflags);
356 }
357
Simon Glass8c4c5c82017-04-23 20:02:11 -0600358 ret = tegra_mmc_send_cmd_bounced(dev, cmd, data, &bbstate);
Stephen Warrenf227e452012-11-06 11:27:30 +0000359
360 if (data)
361 bounce_buffer_stop(&bbstate);
362
363 return ret;
364}
365
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600366static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren85f0ee42011-05-31 10:30:37 +0000367{
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600368 ulong rate;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000369 int div;
Tom Warren85f0ee42011-05-31 10:30:37 +0000370 unsigned short clk;
371 unsigned long timeout;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000372
Tom Warren85f0ee42011-05-31 10:30:37 +0000373 debug(" mmc_change_clock called\n");
374
Simon Glassc2ea5e42011-09-21 12:40:04 +0000375 /*
Tom Warren35ae07b2013-02-26 12:31:26 -0700376 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glassc2ea5e42011-09-21 12:40:04 +0000377 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000378 if (clock == 0)
379 goto out;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600380
381 rate = clk_set_rate(&priv->clk, clock);
382 div = (rate + clock - 1) / clock;
Tom Warren4ff710a2019-06-03 16:06:34 -0700383
384#if defined(CONFIG_TEGRA210)
385 if (priv->mmc_id == PERIPH_ID_SDMMC1 && clock <= 400000) {
386 /* clock_adjust_periph_pll_div() chooses a 'bad' clock
387 * on SDMMC1 T210, so skip it here and force a clock
388 * that's been spec'd in the table in the TRM for
389 * card-detect (400KHz).
390 */
391 uint effective_rate = clock_adjust_periph_pll_div(priv->mmc_id,
392 CLOCK_ID_PERIPH, 24727273, NULL);
393 div = 62;
394
395 debug("%s: WAR: Using SDMMC1 clock of %u, div %d to achieve %dHz card clock ...\n",
396 __func__, effective_rate, div, clock);
397 } else {
398 clock_adjust_periph_pll_div(priv->mmc_id, CLOCK_ID_PERIPH,
399 clock, &div);
400 }
401#endif
Simon Glassc2ea5e42011-09-21 12:40:04 +0000402 debug("div = %d\n", div);
Tom Warren85f0ee42011-05-31 10:30:37 +0000403
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600404 writew(0, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000405
Tom Warren85f0ee42011-05-31 10:30:37 +0000406 /*
407 * CLKCON
408 * SELFREQ[15:8] : base clock divided by value
409 * ENSDCLK[2] : SD Clock Enable
410 * STBLINTCLK[1] : Internal Clock Stable
411 * ENINTCLK[0] : Internal Clock Enable
412 */
Simon Glassc2ea5e42011-09-21 12:40:04 +0000413 div >>= 1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000414 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
415 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600416 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000417
418 /* Wait max 10 ms */
419 timeout = 10;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600420 while (!(readw(&priv->reg->clkcon) &
Anton staaf0dfb31c2011-11-10 11:56:49 +0000421 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000422 if (timeout == 0) {
423 printf("%s: timeout error\n", __func__);
424 return;
425 }
426 timeout--;
427 udelay(1000);
428 }
429
Anton staaf0dfb31c2011-11-10 11:56:49 +0000430 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600431 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000432
433 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren85f0ee42011-05-31 10:30:37 +0000434
435out:
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600436 priv->clock = clock;
Tom Warren85f0ee42011-05-31 10:30:37 +0000437}
438
Simon Glass8c4c5c82017-04-23 20:02:11 -0600439static int tegra_mmc_set_ios(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000440{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600441 struct tegra_mmc_priv *priv = dev_get_priv(dev);
442 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000443 unsigned char ctrl;
444 debug(" mmc_set_ios called\n");
445
446 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
447
448 /* Change clock first */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600449 tegra_mmc_change_clock(priv, mmc->clock);
Tom Warren85f0ee42011-05-31 10:30:37 +0000450
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600451 ctrl = readb(&priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000452
453 /*
454 * WIDE8[5]
455 * 0 = Depend on WIDE4
456 * 1 = 8-bit mode
457 * WIDE4[1]
458 * 1 = 4-bit mode
459 * 0 = 1-bit mode
460 */
461 if (mmc->bus_width == 8)
462 ctrl |= (1 << 5);
463 else if (mmc->bus_width == 4)
464 ctrl |= (1 << 1);
465 else
Simon Glass9d6551a2017-06-07 21:11:48 -0600466 ctrl &= ~(1 << 1 | 1 << 5);
Tom Warren85f0ee42011-05-31 10:30:37 +0000467
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600468 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000469 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900470
471 return 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000472}
473
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600474static void tegra_mmc_pad_init(struct tegra_mmc_priv *priv)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600475{
Tom Warren2e86e812019-05-29 09:30:01 -0700476#if defined(CONFIG_TEGRA30) || defined(CONFIG_TEGRA210)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600477 u32 val;
Tom Warren2e86e812019-05-29 09:30:01 -0700478 u16 clk_con;
479 int timeout;
480 int id = priv->mmc_id;
Stephen Warrenc76e9362016-09-13 10:45:44 -0600481
Tom Warren2e86e812019-05-29 09:30:01 -0700482 debug("%s: sdmmc address = %p, id = %d\n", __func__,
483 priv->reg, id);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600484
485 /* Set the pad drive strength for SDMMC1 or 3 only */
Tom Warren2e86e812019-05-29 09:30:01 -0700486 if (id != PERIPH_ID_SDMMC1 && id != PERIPH_ID_SDMMC3) {
Stephen Warrenc76e9362016-09-13 10:45:44 -0600487 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
Tom Warren2e86e812019-05-29 09:30:01 -0700488 __func__);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600489 return;
490 }
491
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600492 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600493 val &= 0xFFFFFFF0;
494 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600495 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600496
Tom Warren2e86e812019-05-29 09:30:01 -0700497 /* Disable SD Clock Enable before running auto-cal as per TRM */
498 clk_con = readw(&priv->reg->clkcon);
499 debug("%s: CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
500 clk_con &= ~TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
501 writew(clk_con, &priv->reg->clkcon);
502
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600503 val = readl(&priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600504 val &= 0xFFFF0000;
Tom Warren2e86e812019-05-29 09:30:01 -0700505 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600506 writel(val, &priv->reg->autocalcfg);
Tom Warren2e86e812019-05-29 09:30:01 -0700507 val |= AUTO_CAL_START | AUTO_CAL_ENABLE;
508 writel(val, &priv->reg->autocalcfg);
509 debug("%s: AUTO_CAL_CFG = 0x%08X\n", __func__, val);
510 udelay(1);
511 timeout = 100; /* 10 mSec max (100*100uS) */
512 do {
513 val = readl(&priv->reg->autocalsts);
514 udelay(100);
515 } while ((val & AUTO_CAL_ACTIVE) && --timeout);
516 val = readl(&priv->reg->autocalsts);
517 debug("%s: Final AUTO_CAL_STATUS = 0x%08X, timeout = %d\n",
518 __func__, val, timeout);
519
520 /* Re-enable SD Clock Enable when auto-cal is done */
521 clk_con |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
522 writew(clk_con, &priv->reg->clkcon);
523 clk_con = readw(&priv->reg->clkcon);
524 debug("%s: final CLOCK_CONTROL = 0x%04X\n", __func__, clk_con);
525
526 if (timeout == 0) {
527 printf("%s: Warning: Autocal timed out!\n", __func__);
528 /* TBD: Set CFG2TMC_SDMMC1_PAD_CAL_DRV* regs here */
529 }
Tom Warren2e86e812019-05-29 09:30:01 -0700530#endif /* T30/T210 */
Stephen Warrenc76e9362016-09-13 10:45:44 -0600531}
532
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600533static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000534{
535 unsigned int timeout;
536 debug(" mmc_reset called\n");
537
538 /*
539 * RSTALL[0] : Software reset for all
540 * 1 = reset
541 * 0 = work
542 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600543 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren85f0ee42011-05-31 10:30:37 +0000544
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600545 priv->clock = 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000546
547 /* Wait max 100 ms */
548 timeout = 100;
549
550 /* hw clears the bit when it's done */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600551 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000552 if (timeout == 0) {
553 printf("%s: timeout error\n", __func__);
554 return;
555 }
556 timeout--;
557 udelay(1000);
558 }
Tom Warren35ae07b2013-02-26 12:31:26 -0700559
560 /* Set SD bus voltage & enable bus power */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600561 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren35ae07b2013-02-26 12:31:26 -0700562 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600563 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren35ae07b2013-02-26 12:31:26 -0700564
565 /* Make sure SDIO pads are set up */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600566 tegra_mmc_pad_init(priv);
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +0300567
568 if (!IS_ERR_VALUE(priv->tap_value) ||
569 !IS_ERR_VALUE(priv->trim_value)) {
570 u32 val;
571
572 val = readl(&priv->reg->venclkctl);
573
574 val &= ~TRIM_VAL_MASK;
575 val |= (priv->trim_value << TRIM_VAL_SHIFT);
576
577 val &= ~TAP_VAL_MASK;
578 val |= (priv->tap_value << TAP_VAL_SHIFT);
579
580 writel(val, &priv->reg->venclkctl);
581 debug("%s: VENDOR_CLOCK_CNTRL = 0x%08X\n", __func__, val);
582 }
Tom Warren85f0ee42011-05-31 10:30:37 +0000583}
584
Simon Glass8c4c5c82017-04-23 20:02:11 -0600585static int tegra_mmc_init(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000586{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600587 struct tegra_mmc_priv *priv = dev_get_priv(dev);
588 struct mmc *mmc = mmc_get_mmc_dev(dev);
Tom Warren85f0ee42011-05-31 10:30:37 +0000589 unsigned int mask;
Tom Warrena66f7722016-09-13 10:45:48 -0600590 debug(" tegra_mmc_init called\n");
Tom Warren85f0ee42011-05-31 10:30:37 +0000591
Tom Warren2e86e812019-05-29 09:30:01 -0700592#if defined(CONFIG_TEGRA210)
593 priv->mmc_id = clock_decode_periph_id(dev);
594 if (priv->mmc_id == PERIPH_ID_NONE) {
595 printf("%s: Missing/invalid peripheral ID\n", __func__);
596 return -EINVAL;
597 }
598#endif
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600599 tegra_mmc_reset(priv, mmc);
Tom Warren85f0ee42011-05-31 10:30:37 +0000600
Marcel Ziswiler86708852017-03-25 01:18:22 +0100601#if defined(CONFIG_TEGRA124_MMC_DISABLE_EXT_LOOPBACK)
602 /*
603 * Disable the external clock loopback and use the internal one on
604 * SDMMC3 as per the SDMMC_VENDOR_MISC_CNTRL_0 register's SDMMC_SPARE1
605 * bits being set to 0xfffd according to the TRM.
606 *
607 * TODO(marcel.ziswiler@toradex.com): Move to device tree controlled
608 * approach once proper kernel integration made it mainline.
609 */
610 if (priv->reg == (void *)0x700b0400) {
611 mask = readl(&priv->reg->venmiscctl);
612 mask &= ~TEGRA_MMC_MISCON_ENABLE_EXT_LOOPBACK;
613 writel(mask, &priv->reg->venmiscctl);
614 }
615#endif
616
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600617 priv->version = readw(&priv->reg->hcver);
618 debug("host version = %x\n", priv->version);
Tom Warren85f0ee42011-05-31 10:30:37 +0000619
620 /* mask all */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600621 writel(0xffffffff, &priv->reg->norintstsen);
622 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000623
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600624 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000625 /*
626 * NORMAL Interrupt Status Enable Register init
627 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
628 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf3ade2102011-11-10 11:56:50 +0000629 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren85f0ee42011-05-31 10:30:37 +0000630 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
631 * [0] ENSTACMDCMPLT : Command Complete Status Enable
632 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600633 mask = readl(&priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000634 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000635 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
636 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf3ade2102011-11-10 11:56:50 +0000637 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf0dfb31c2011-11-10 11:56:49 +0000638 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
639 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600640 writel(mask, &priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000641
642 /*
643 * NORMAL Interrupt Signal Enable Register init
644 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
645 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600646 mask = readl(&priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000647 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000648 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600649 writel(mask, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000650
651 return 0;
652}
653
Simon Glass8c4c5c82017-04-23 20:02:11 -0600654static int tegra_mmc_getcd(struct udevice *dev)
Thierry Redingf1494112012-01-02 01:15:39 +0000655{
Simon Glass8c4c5c82017-04-23 20:02:11 -0600656 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Thierry Redingf1494112012-01-02 01:15:39 +0000657
Tom Warren22562a42012-09-04 17:00:24 -0700658 debug("tegra_mmc_getcd called\n");
Thierry Redingf1494112012-01-02 01:15:39 +0000659
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600660 if (dm_gpio_is_valid(&priv->cd_gpio))
661 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingf1494112012-01-02 01:15:39 +0000662
663 return 1;
664}
665
Simon Glass8c4c5c82017-04-23 20:02:11 -0600666static const struct dm_mmc_ops tegra_mmc_ops = {
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200667 .send_cmd = tegra_mmc_send_cmd,
668 .set_ios = tegra_mmc_set_ios,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600669 .get_cd = tegra_mmc_getcd,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200670};
671
Tom Warrena66f7722016-09-13 10:45:48 -0600672static int tegra_mmc_probe(struct udevice *dev)
Tom Warren85f0ee42011-05-31 10:30:37 +0000673{
Tom Warrena66f7722016-09-13 10:45:48 -0600674 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700675 struct tegra_mmc_plat *plat = dev_get_plat(dev);
Tom Warrena66f7722016-09-13 10:45:48 -0600676 struct tegra_mmc_priv *priv = dev_get_priv(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600677 struct mmc_config *cfg = &plat->cfg;
Stephen Warrenf79df4b2016-09-13 10:46:01 -0600678 int bus_width, ret;
Tom Warren85f0ee42011-05-31 10:30:37 +0000679
Simon Glass8c4c5c82017-04-23 20:02:11 -0600680 cfg->name = dev->name;
Tom Warren85f0ee42011-05-31 10:30:37 +0000681
Simon Glass9c3b0e42017-07-25 08:30:08 -0600682 bus_width = dev_read_u32_default(dev, "bus-width", 1);
Tom Warrena66f7722016-09-13 10:45:48 -0600683
Simon Glass8c4c5c82017-04-23 20:02:11 -0600684 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
685 cfg->host_caps = 0;
Tom Warrena66f7722016-09-13 10:45:48 -0600686 if (bus_width == 8)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600687 cfg->host_caps |= MMC_MODE_8BIT;
Tom Warrena66f7722016-09-13 10:45:48 -0600688 if (bus_width >= 4)
Simon Glass8c4c5c82017-04-23 20:02:11 -0600689 cfg->host_caps |= MMC_MODE_4BIT;
690 cfg->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren85f0ee42011-05-31 10:30:37 +0000691
692 /*
693 * min freq is for card identification, and is the highest
694 * low-speed SDIO card frequency (actually 400KHz)
695 * max freq is highest HS eMMC clock as per the SD/MMC spec
696 * (actually 52MHz)
Tom Warren85f0ee42011-05-31 10:30:37 +0000697 */
Simon Glass8c4c5c82017-04-23 20:02:11 -0600698 cfg->f_min = 375000;
Peter Geisa0c71062023-12-19 15:35:52 +0200699 cfg->f_max = dev_read_u32_default(dev, "max-frequency", 48000000);
Tom Warren85f0ee42011-05-31 10:30:37 +0000700
Simon Glass8c4c5c82017-04-23 20:02:11 -0600701 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200702
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100703 priv->reg = dev_read_addr_ptr(dev);
Tom Warren9745cf82013-02-21 12:31:30 +0000704
Tom Warrena66f7722016-09-13 10:45:48 -0600705 ret = reset_get_by_name(dev, "sdhci", &priv->reset_ctl);
706 if (ret) {
707 debug("reset_get_by_name() failed: %d\n", ret);
708 return ret;
709 }
710 ret = clk_get_by_index(dev, 0, &priv->clk);
711 if (ret) {
712 debug("clk_get_by_index() failed: %d\n", ret);
713 return ret;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600714 }
Tom Warrena66f7722016-09-13 10:45:48 -0600715
716 ret = reset_assert(&priv->reset_ctl);
717 if (ret)
718 return ret;
719 ret = clk_enable(&priv->clk);
720 if (ret)
721 return ret;
722 ret = clk_set_rate(&priv->clk, 20000000);
723 if (IS_ERR_VALUE(ret))
724 return ret;
725 ret = reset_deassert(&priv->reset_ctl);
726 if (ret)
727 return ret;
Tom Warren9745cf82013-02-21 12:31:30 +0000728
Tom Warrena66f7722016-09-13 10:45:48 -0600729 /* These GPIOs are optional */
Simon Glass9c3b0e42017-07-25 08:30:08 -0600730 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
731 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
732 gpio_request_by_name(dev, "power-gpios", 0, &priv->pwr_gpio,
733 GPIOD_IS_OUT);
Tom Warrena66f7722016-09-13 10:45:48 -0600734 if (dm_gpio_is_valid(&priv->pwr_gpio))
735 dm_gpio_set_value(&priv->pwr_gpio, 1);
Tom Warren9745cf82013-02-21 12:31:30 +0000736
Svyatoslav Ryhelfdeb13a2023-10-03 09:33:52 +0300737 ret = dev_read_u32(dev, "nvidia,default-tap", &priv->tap_value);
738 if (ret)
739 priv->tap_value = ret;
740
741 ret = dev_read_u32(dev, "nvidia,default-trim", &priv->trim_value);
742 if (ret)
743 priv->trim_value = ret;
744
Simon Glass8c4c5c82017-04-23 20:02:11 -0600745 upriv->mmc = &plat->mmc;
Tom Warren9745cf82013-02-21 12:31:30 +0000746
Simon Glass8c4c5c82017-04-23 20:02:11 -0600747 return tegra_mmc_init(dev);
748}
Tom Warren9745cf82013-02-21 12:31:30 +0000749
Simon Glass8c4c5c82017-04-23 20:02:11 -0600750static int tegra_mmc_bind(struct udevice *dev)
751{
Simon Glassfa20e932020-12-03 16:55:20 -0700752 struct tegra_mmc_plat *plat = dev_get_plat(dev);
Simon Glass8c4c5c82017-04-23 20:02:11 -0600753
754 return mmc_bind(dev, &plat->mmc, &plat->cfg);
Tom Warren9745cf82013-02-21 12:31:30 +0000755}
756
Tom Warrena66f7722016-09-13 10:45:48 -0600757static const struct udevice_id tegra_mmc_ids[] = {
758 { .compatible = "nvidia,tegra20-sdhci" },
759 { .compatible = "nvidia,tegra30-sdhci" },
760 { .compatible = "nvidia,tegra114-sdhci" },
761 { .compatible = "nvidia,tegra124-sdhci" },
762 { .compatible = "nvidia,tegra210-sdhci" },
763 { .compatible = "nvidia,tegra186-sdhci" },
764 { }
765};
Tom Warren9745cf82013-02-21 12:31:30 +0000766
Tom Warrena66f7722016-09-13 10:45:48 -0600767U_BOOT_DRIVER(tegra_mmc_drv) = {
768 .name = "tegra_mmc",
769 .id = UCLASS_MMC,
770 .of_match = tegra_mmc_ids,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600771 .bind = tegra_mmc_bind,
Tom Warrena66f7722016-09-13 10:45:48 -0600772 .probe = tegra_mmc_probe,
Simon Glass8c4c5c82017-04-23 20:02:11 -0600773 .ops = &tegra_mmc_ops,
Simon Glass71fa5b42020-12-03 16:55:18 -0700774 .plat_auto = sizeof(struct tegra_mmc_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700775 .priv_auto = sizeof(struct tegra_mmc_priv),
Tom Warrena66f7722016-09-13 10:45:48 -0600776};