blob: 9287f1c3424f02ad38e96ddbd93f3cbf470498ad [file] [log] [blame]
Tom Warren85f0ee42011-05-31 10:30:37 +00001/*
2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Jaehoon Chung <jh80.chung@samsung.com>
Tom Warrenab0cc6b2015-03-04 16:36:00 -07005 * Portions Copyright 2011-2015 NVIDIA Corporation
Tom Warren85f0ee42011-05-31 10:30:37 +00006 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Tom Warren85f0ee42011-05-31 10:30:37 +00008 */
9
Stephen Warrenf227e452012-11-06 11:27:30 +000010#include <bouncebuf.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000011#include <common.h>
Stephen Warrend26e24d2016-08-05 16:10:33 -060012#include <dm/device.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090013#include <errno.h>
Stephen Warrenfba87542011-10-31 06:51:36 +000014#include <asm/gpio.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000015#include <asm/io.h>
Stephen Warrendfbdc122016-05-12 12:11:23 -060016#ifndef CONFIG_TEGRA186
Simon Glassc2ea5e42011-09-21 12:40:04 +000017#include <asm/arch/clock.h>
Tom Warrenab371962012-09-19 15:50:56 -070018#include <asm/arch-tegra/clk_rst.h>
Stephen Warrendfbdc122016-05-12 12:11:23 -060019#endif
Jeroen Hofstee93dfae72014-10-08 22:57:46 +020020#include <asm/arch-tegra/mmc.h>
Tom Warrenab371962012-09-19 15:50:56 -070021#include <asm/arch-tegra/tegra_mmc.h>
22#include <mmc.h>
Tom Warren85f0ee42011-05-31 10:30:37 +000023
Stephen Warrend26e24d2016-08-05 16:10:33 -060024/*
25 * FIXME: TODO: This driver contains a number of ifdef CONFIG_TEGRA186 that
26 * should not be present. These are needed because newer Tegra SoCs support
27 * only the standard clock/reset APIs, whereas older Tegra SoCs support only
28 * a custom Tegra-specific API. ASAP the older Tegra SoCs' code should be
29 * fixed to implement the standard APIs, and all drivers converted to solely
30 * use the new standard APIs, with no ifdefs.
31 */
32
Tom Warren9745cf82013-02-21 12:31:30 +000033DECLARE_GLOBAL_DATA_PTR;
Tom Warren85f0ee42011-05-31 10:30:37 +000034
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060035struct tegra_mmc_priv {
36 struct tegra_mmc *reg;
37 int id; /* device id/number, 0-3 */
38 int enabled; /* 1 to enable, 0 to disable */
39 int width; /* Bus Width, 1, 4 or 8 */
40#ifdef CONFIG_TEGRA186
41 struct reset_ctl reset_ctl;
42 struct clk clk;
43#else
44 enum periph_id mmc_id; /* Peripheral ID: PERIPH_ID_... */
45#endif
46 struct gpio_desc cd_gpio; /* Change Detect GPIO */
47 struct gpio_desc pwr_gpio; /* Power GPIO */
48 struct gpio_desc wp_gpio; /* Write Protect GPIO */
49 unsigned int version; /* SDHCI spec. version */
50 unsigned int clock; /* Current clock (MHz) */
51 struct mmc_config cfg; /* mmc configuration */
52};
53
54struct tegra_mmc_priv mmc_host[CONFIG_SYS_MMC_MAX_DEVICE];
Simon Glassc2ea5e42011-09-21 12:40:04 +000055
Masahiro Yamada366b24f2015-08-12 07:31:55 +090056#if !CONFIG_IS_ENABLED(OF_CONTROL)
Tom Warren9745cf82013-02-21 12:31:30 +000057#error "Please enable device tree support to use this driver"
58#endif
Tom Warren85f0ee42011-05-31 10:30:37 +000059
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060060static void tegra_mmc_set_power(struct tegra_mmc_priv *priv,
61 unsigned short power)
Tom Warren35ae07b2013-02-26 12:31:26 -070062{
63 u8 pwr = 0;
64 debug("%s: power = %x\n", __func__, power);
65
66 if (power != (unsigned short)-1) {
67 switch (1 << power) {
68 case MMC_VDD_165_195:
69 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
70 break;
71 case MMC_VDD_29_30:
72 case MMC_VDD_30_31:
73 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
74 break;
75 case MMC_VDD_32_33:
76 case MMC_VDD_33_34:
77 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
78 break;
79 }
80 }
81 debug("%s: pwr = %X\n", __func__, pwr);
82
83 /* Set the bus voltage first (if any) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060084 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070085 if (pwr == 0)
86 return;
87
88 /* Now enable bus power */
89 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060090 writeb(pwr, &priv->reg->pwrcon);
Tom Warren35ae07b2013-02-26 12:31:26 -070091}
92
Stephen Warren6d9ccbb2016-09-13 10:45:46 -060093static void tegra_mmc_prepare_data(struct tegra_mmc_priv *priv,
94 struct mmc_data *data,
95 struct bounce_buffer *bbstate)
Tom Warren85f0ee42011-05-31 10:30:37 +000096{
97 unsigned char ctrl;
98
Tom Warren85f0ee42011-05-31 10:30:37 +000099
Stephen Warrenf227e452012-11-06 11:27:30 +0000100 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
101 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
102 data->blocksize);
103
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600104 writel((u32)(unsigned long)bbstate->bounce_buffer, &priv->reg->sysad);
Tom Warren85f0ee42011-05-31 10:30:37 +0000105 /*
106 * DMASEL[4:3]
107 * 00 = Selects SDMA
108 * 01 = Reserved
109 * 10 = Selects 32-bit Address ADMA2
110 * 11 = Selects 64-bit Address ADMA2
111 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600112 ctrl = readb(&priv->reg->hostctl);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000113 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
114 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600115 writeb(ctrl, &priv->reg->hostctl);
Tom Warren85f0ee42011-05-31 10:30:37 +0000116
117 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600118 writew((7 << 12) | (data->blocksize & 0xFFF), &priv->reg->blksize);
119 writew(data->blocks, &priv->reg->blkcnt);
Tom Warren85f0ee42011-05-31 10:30:37 +0000120}
121
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600122static void tegra_mmc_set_transfer_mode(struct tegra_mmc_priv *priv,
123 struct mmc_data *data)
Tom Warren85f0ee42011-05-31 10:30:37 +0000124{
125 unsigned short mode;
126 debug(" mmc_set_transfer_mode called\n");
127 /*
128 * TRNMOD
129 * MUL1SIN0[5] : Multi/Single Block Select
130 * RD1WT0[4] : Data Transfer Direction Select
131 * 1 = read
132 * 0 = write
133 * ENACMD12[2] : Auto CMD12 Enable
134 * ENBLKCNT[1] : Block Count Enable
135 * ENDMA[0] : DMA Enable
136 */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000137 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
138 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
139
Tom Warren85f0ee42011-05-31 10:30:37 +0000140 if (data->blocks > 1)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000141 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
142
Tom Warren85f0ee42011-05-31 10:30:37 +0000143 if (data->flags & MMC_DATA_READ)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000144 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren85f0ee42011-05-31 10:30:37 +0000145
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600146 writew(mode, &priv->reg->trnmod);
Tom Warren85f0ee42011-05-31 10:30:37 +0000147}
148
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600149static int tegra_mmc_wait_inhibit(struct tegra_mmc_priv *priv,
150 struct mmc_cmd *cmd,
151 struct mmc_data *data,
152 unsigned int timeout)
Tom Warren85f0ee42011-05-31 10:30:37 +0000153{
Tom Warren85f0ee42011-05-31 10:30:37 +0000154 /*
155 * PRNSTS
Anton staaf5ab3fba2011-11-10 11:56:52 +0000156 * CMDINHDAT[1] : Command Inhibit (DAT)
157 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren85f0ee42011-05-31 10:30:37 +0000158 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000159 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren85f0ee42011-05-31 10:30:37 +0000160
161 /*
162 * We shouldn't wait for data inhibit for stop commands, even
163 * though they might use busy signaling
164 */
Anton staaf5ab3fba2011-11-10 11:56:52 +0000165 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
166 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000167
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600168 while (readl(&priv->reg->prnsts) & mask) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000169 if (timeout == 0) {
170 printf("%s: timeout error\n", __func__);
171 return -1;
172 }
173 timeout--;
174 udelay(1000);
175 }
176
Anton staaf5ab3fba2011-11-10 11:56:52 +0000177 return 0;
178}
179
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600180static int tegra_mmc_send_cmd_bounced(struct mmc *mmc, struct mmc_cmd *cmd,
181 struct mmc_data *data,
182 struct bounce_buffer *bbstate)
Anton staaf5ab3fba2011-11-10 11:56:52 +0000183{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600184 struct tegra_mmc_priv *priv = mmc->priv;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000185 int flags, i;
186 int result;
Anatolij Gustschine1f53412012-03-28 03:40:00 +0000187 unsigned int mask = 0;
Anton staaf5ab3fba2011-11-10 11:56:52 +0000188 unsigned int retry = 0x100000;
189 debug(" mmc_send_cmd called\n");
190
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600191 result = tegra_mmc_wait_inhibit(priv, cmd, data, 10 /* ms */);
Anton staaf5ab3fba2011-11-10 11:56:52 +0000192
193 if (result < 0)
194 return result;
195
Tom Warren85f0ee42011-05-31 10:30:37 +0000196 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600197 tegra_mmc_prepare_data(priv, data, bbstate);
Tom Warren85f0ee42011-05-31 10:30:37 +0000198
199 debug("cmd->arg: %08x\n", cmd->cmdarg);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600200 writel(cmd->cmdarg, &priv->reg->argument);
Tom Warren85f0ee42011-05-31 10:30:37 +0000201
202 if (data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600203 tegra_mmc_set_transfer_mode(priv, data);
Tom Warren85f0ee42011-05-31 10:30:37 +0000204
205 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
206 return -1;
207
208 /*
209 * CMDREG
210 * CMDIDX[13:8] : Command index
211 * DATAPRNT[5] : Data Present Select
212 * ENCMDIDX[4] : Command Index Check Enable
213 * ENCMDCRC[3] : Command CRC Check Enable
214 * RSPTYP[1:0]
215 * 00 = No Response
216 * 01 = Length 136
217 * 10 = Length 48
218 * 11 = Length 48 Check busy after response
219 */
220 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf0dfb31c2011-11-10 11:56:49 +0000221 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren85f0ee42011-05-31 10:30:37 +0000222 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000223 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren85f0ee42011-05-31 10:30:37 +0000224 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000225 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren85f0ee42011-05-31 10:30:37 +0000226 else
Anton staaf0dfb31c2011-11-10 11:56:49 +0000227 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren85f0ee42011-05-31 10:30:37 +0000228
229 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000230 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000231 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000232 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren85f0ee42011-05-31 10:30:37 +0000233 if (data)
Anton staaf0dfb31c2011-11-10 11:56:49 +0000234 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren85f0ee42011-05-31 10:30:37 +0000235
236 debug("cmd: %d\n", cmd->cmdidx);
237
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600238 writew((cmd->cmdidx << 8) | flags, &priv->reg->cmdreg);
Tom Warren85f0ee42011-05-31 10:30:37 +0000239
240 for (i = 0; i < retry; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600241 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000242 /* Command Complete */
Anton staaf0dfb31c2011-11-10 11:56:49 +0000243 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000244 if (!data)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600245 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000246 break;
247 }
248 }
249
250 if (i == retry) {
251 printf("%s: waiting for status update\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600252 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900253 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000254 }
255
Anton staaf0dfb31c2011-11-10 11:56:49 +0000256 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000257 /* Timeout Error */
258 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600259 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900260 return -ETIMEDOUT;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000261 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000262 /* Error Interrupt */
263 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600264 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000265 return -1;
266 }
267
268 if (cmd->resp_type & MMC_RSP_PRESENT) {
269 if (cmd->resp_type & MMC_RSP_136) {
270 /* CRC is stripped so we need to do some shifting. */
271 for (i = 0; i < 4; i++) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600272 unsigned long offset = (unsigned long)
273 (&priv->reg->rspreg3 - i);
Tom Warren85f0ee42011-05-31 10:30:37 +0000274 cmd->response[i] = readl(offset) << 8;
275
276 if (i != 3) {
277 cmd->response[i] |=
278 readb(offset - 1);
279 }
280 debug("cmd->resp[%d]: %08x\n",
281 i, cmd->response[i]);
282 }
283 } else if (cmd->resp_type & MMC_RSP_BUSY) {
284 for (i = 0; i < retry; i++) {
285 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600286 if (readl(&priv->reg->prnsts)
Tom Warren85f0ee42011-05-31 10:30:37 +0000287 & (1 << 20)) /* DAT[0] */
288 break;
289 }
290
291 if (i == retry) {
292 printf("%s: card is still busy\n", __func__);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600293 writel(mask, &priv->reg->norintsts);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900294 return -ETIMEDOUT;
Tom Warren85f0ee42011-05-31 10:30:37 +0000295 }
296
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600297 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000298 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
299 } else {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600300 cmd->response[0] = readl(&priv->reg->rspreg0);
Tom Warren85f0ee42011-05-31 10:30:37 +0000301 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
302 }
303 }
304
305 if (data) {
Anton staafbd348422011-11-10 11:56:51 +0000306 unsigned long start = get_timer(0);
307
Tom Warren85f0ee42011-05-31 10:30:37 +0000308 while (1) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600309 mask = readl(&priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000310
Anton staaf0dfb31c2011-11-10 11:56:49 +0000311 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000312 /* Error Interrupt */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600313 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000314 printf("%s: error during transfer: 0x%08x\n",
315 __func__, mask);
316 return -1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000317 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf3ade2102011-11-10 11:56:50 +0000318 /*
319 * DMA Interrupt, restart the transfer where
320 * it was interrupted.
321 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600322 unsigned int address = readl(&priv->reg->sysad);
Anton staaf3ade2102011-11-10 11:56:50 +0000323
Tom Warren85f0ee42011-05-31 10:30:37 +0000324 debug("DMA end\n");
Anton staaf3ade2102011-11-10 11:56:50 +0000325 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600326 &priv->reg->norintsts);
327 writel(address, &priv->reg->sysad);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000328 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000329 /* Transfer Complete */
330 debug("r/w is done\n");
331 break;
Marcel Ziswilere1207e92014-10-04 01:48:53 +0200332 } else if (get_timer(start) > 8000UL) {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600333 writel(mask, &priv->reg->norintsts);
Anton staafbd348422011-11-10 11:56:51 +0000334 printf("%s: MMC Timeout\n"
335 " Interrupt status 0x%08x\n"
336 " Interrupt status enable 0x%08x\n"
337 " Interrupt signal enable 0x%08x\n"
338 " Present status 0x%08x\n",
339 __func__, mask,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600340 readl(&priv->reg->norintstsen),
341 readl(&priv->reg->norintsigen),
342 readl(&priv->reg->prnsts));
Anton staafbd348422011-11-10 11:56:51 +0000343 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000344 }
345 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600346 writel(mask, &priv->reg->norintsts);
Tom Warren85f0ee42011-05-31 10:30:37 +0000347 }
348
349 udelay(1000);
350 return 0;
351}
352
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200353static int tegra_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600354 struct mmc_data *data)
Stephen Warrenf227e452012-11-06 11:27:30 +0000355{
356 void *buf;
357 unsigned int bbflags;
358 size_t len;
359 struct bounce_buffer bbstate;
360 int ret;
361
362 if (data) {
363 if (data->flags & MMC_DATA_READ) {
364 buf = data->dest;
365 bbflags = GEN_BB_WRITE;
366 } else {
367 buf = (void *)data->src;
368 bbflags = GEN_BB_READ;
369 }
370 len = data->blocks * data->blocksize;
371
372 bounce_buffer_start(&bbstate, buf, len, bbflags);
373 }
374
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600375 ret = tegra_mmc_send_cmd_bounced(mmc, cmd, data, &bbstate);
Stephen Warrenf227e452012-11-06 11:27:30 +0000376
377 if (data)
378 bounce_buffer_stop(&bbstate);
379
380 return ret;
381}
382
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600383static void tegra_mmc_change_clock(struct tegra_mmc_priv *priv, uint clock)
Tom Warren85f0ee42011-05-31 10:30:37 +0000384{
Simon Glassc2ea5e42011-09-21 12:40:04 +0000385 int div;
Tom Warren85f0ee42011-05-31 10:30:37 +0000386 unsigned short clk;
387 unsigned long timeout;
Simon Glassc2ea5e42011-09-21 12:40:04 +0000388
Tom Warren85f0ee42011-05-31 10:30:37 +0000389 debug(" mmc_change_clock called\n");
390
Simon Glassc2ea5e42011-09-21 12:40:04 +0000391 /*
Tom Warren35ae07b2013-02-26 12:31:26 -0700392 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glassc2ea5e42011-09-21 12:40:04 +0000393 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000394 if (clock == 0)
395 goto out;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600396#ifdef CONFIG_TEGRA186
397 {
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600398 ulong rate = clk_set_rate(&priv->clk, clock);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600399 div = (rate + clock - 1) / clock;
400 }
401#else
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600402 clock_adjust_periph_pll_div(priv->mmc_id, CLOCK_ID_PERIPH, clock,
Simon Glassc2ea5e42011-09-21 12:40:04 +0000403 &div);
Stephen Warrendfbdc122016-05-12 12:11:23 -0600404#endif
Simon Glassc2ea5e42011-09-21 12:40:04 +0000405 debug("div = %d\n", div);
Tom Warren85f0ee42011-05-31 10:30:37 +0000406
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600407 writew(0, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000408
Tom Warren85f0ee42011-05-31 10:30:37 +0000409 /*
410 * CLKCON
411 * SELFREQ[15:8] : base clock divided by value
412 * ENSDCLK[2] : SD Clock Enable
413 * STBLINTCLK[1] : Internal Clock Stable
414 * ENINTCLK[0] : Internal Clock Enable
415 */
Simon Glassc2ea5e42011-09-21 12:40:04 +0000416 div >>= 1;
Anton staaf0dfb31c2011-11-10 11:56:49 +0000417 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
418 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600419 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000420
421 /* Wait max 10 ms */
422 timeout = 10;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600423 while (!(readw(&priv->reg->clkcon) &
Anton staaf0dfb31c2011-11-10 11:56:49 +0000424 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000425 if (timeout == 0) {
426 printf("%s: timeout error\n", __func__);
427 return;
428 }
429 timeout--;
430 udelay(1000);
431 }
432
Anton staaf0dfb31c2011-11-10 11:56:49 +0000433 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600434 writew(clk, &priv->reg->clkcon);
Tom Warren85f0ee42011-05-31 10:30:37 +0000435
436 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren85f0ee42011-05-31 10:30:37 +0000437
438out:
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600439 priv->clock = clock;
Tom Warren85f0ee42011-05-31 10:30:37 +0000440}
441
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200442static void tegra_mmc_set_ios(struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000443{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600444 struct tegra_mmc_priv *priv = mmc->priv;
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
468 ctrl &= ~(1 << 1);
469
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);
472}
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{
476#if defined(CONFIG_TEGRA30)
Stephen Warrenc76e9362016-09-13 10:45:44 -0600477 u32 val;
478
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600479 debug("%s: sdmmc address = %08x\n", __func__, (unsigned int)priv->reg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600480
481 /* Set the pad drive strength for SDMMC1 or 3 only */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600482 if (priv->reg != (void *)0x78000000 &&
483 priv->reg != (void *)0x78000400) {
Stephen Warrenc76e9362016-09-13 10:45:44 -0600484 debug("%s: settings are only valid for SDMMC1/SDMMC3!\n",
485 __func__);
486 return;
487 }
488
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600489 val = readl(&priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600490 val &= 0xFFFFFFF0;
491 val |= MEMCOMP_PADCTRL_VREF;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600492 writel(val, &priv->reg->sdmemcmppadctl);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600493
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600494 val = readl(&priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600495 val &= 0xFFFF0000;
496 val |= AUTO_CAL_PU_OFFSET | AUTO_CAL_PD_OFFSET | AUTO_CAL_ENABLED;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600497 writel(val, &priv->reg->autocalcfg);
Stephen Warrenc76e9362016-09-13 10:45:44 -0600498#endif
499}
500
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600501static void tegra_mmc_reset(struct tegra_mmc_priv *priv, struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000502{
503 unsigned int timeout;
504 debug(" mmc_reset called\n");
505
506 /*
507 * RSTALL[0] : Software reset for all
508 * 1 = reset
509 * 0 = work
510 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600511 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &priv->reg->swrst);
Tom Warren85f0ee42011-05-31 10:30:37 +0000512
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600513 priv->clock = 0;
Tom Warren85f0ee42011-05-31 10:30:37 +0000514
515 /* Wait max 100 ms */
516 timeout = 100;
517
518 /* hw clears the bit when it's done */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600519 while (readb(&priv->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren85f0ee42011-05-31 10:30:37 +0000520 if (timeout == 0) {
521 printf("%s: timeout error\n", __func__);
522 return;
523 }
524 timeout--;
525 udelay(1000);
526 }
Tom Warren35ae07b2013-02-26 12:31:26 -0700527
528 /* Set SD bus voltage & enable bus power */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600529 tegra_mmc_set_power(priv, fls(mmc->cfg->voltages) - 1);
Tom Warren35ae07b2013-02-26 12:31:26 -0700530 debug("%s: power control = %02X, host control = %02X\n", __func__,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600531 readb(&priv->reg->pwrcon), readb(&priv->reg->hostctl));
Tom Warren35ae07b2013-02-26 12:31:26 -0700532
533 /* Make sure SDIO pads are set up */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600534 tegra_mmc_pad_init(priv);
Tom Warren85f0ee42011-05-31 10:30:37 +0000535}
536
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200537static int tegra_mmc_core_init(struct mmc *mmc)
Tom Warren85f0ee42011-05-31 10:30:37 +0000538{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600539 struct tegra_mmc_priv *priv = mmc->priv;
Tom Warren85f0ee42011-05-31 10:30:37 +0000540 unsigned int mask;
541 debug(" mmc_core_init called\n");
542
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600543 tegra_mmc_reset(priv, mmc);
Tom Warren85f0ee42011-05-31 10:30:37 +0000544
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600545 priv->version = readw(&priv->reg->hcver);
546 debug("host version = %x\n", priv->version);
Tom Warren85f0ee42011-05-31 10:30:37 +0000547
548 /* mask all */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600549 writel(0xffffffff, &priv->reg->norintstsen);
550 writel(0xffffffff, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000551
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600552 writeb(0xe, &priv->reg->timeoutcon); /* TMCLK * 2^27 */
Tom Warren85f0ee42011-05-31 10:30:37 +0000553 /*
554 * NORMAL Interrupt Status Enable Register init
555 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
556 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf3ade2102011-11-10 11:56:50 +0000557 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren85f0ee42011-05-31 10:30:37 +0000558 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
559 * [0] ENSTACMDCMPLT : Command Complete Status Enable
560 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600561 mask = readl(&priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000562 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000563 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
564 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf3ade2102011-11-10 11:56:50 +0000565 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf0dfb31c2011-11-10 11:56:49 +0000566 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
567 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600568 writel(mask, &priv->reg->norintstsen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000569
570 /*
571 * NORMAL Interrupt Signal Enable Register init
572 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
573 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600574 mask = readl(&priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000575 mask &= ~(0xffff);
Anton staaf0dfb31c2011-11-10 11:56:49 +0000576 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600577 writel(mask, &priv->reg->norintsigen);
Tom Warren85f0ee42011-05-31 10:30:37 +0000578
579 return 0;
580}
581
Jeroen Hofstee93dfae72014-10-08 22:57:46 +0200582static int tegra_mmc_getcd(struct mmc *mmc)
Thierry Redingf1494112012-01-02 01:15:39 +0000583{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600584 struct tegra_mmc_priv *priv = mmc->priv;
Thierry Redingf1494112012-01-02 01:15:39 +0000585
Tom Warren22562a42012-09-04 17:00:24 -0700586 debug("tegra_mmc_getcd called\n");
Thierry Redingf1494112012-01-02 01:15:39 +0000587
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600588 if (dm_gpio_is_valid(&priv->cd_gpio))
589 return dm_gpio_get_value(&priv->cd_gpio);
Thierry Redingf1494112012-01-02 01:15:39 +0000590
591 return 1;
592}
593
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200594static const struct mmc_ops tegra_mmc_ops = {
595 .send_cmd = tegra_mmc_send_cmd,
596 .set_ios = tegra_mmc_set_ios,
597 .init = tegra_mmc_core_init,
598 .getcd = tegra_mmc_getcd,
599};
600
Simon Glass7bcaf2a2015-05-04 11:31:16 -0600601static int do_mmc_init(int dev_index, bool removable)
Tom Warren85f0ee42011-05-31 10:30:37 +0000602{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600603 struct tegra_mmc_priv *priv;
Tom Warren85f0ee42011-05-31 10:30:37 +0000604 struct mmc *mmc;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600605#ifdef CONFIG_TEGRA186
606 int ret;
607#endif
Tom Warren85f0ee42011-05-31 10:30:37 +0000608
Tom Warren9745cf82013-02-21 12:31:30 +0000609 /* DT should have been read & host config filled in */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600610 priv = &mmc_host[dev_index];
611 if (!priv->enabled)
Tom Warren9745cf82013-02-21 12:31:30 +0000612 return -1;
Stephen Warren85a6c072011-10-31 06:51:34 +0000613
Simon Glassa30d4ba2015-01-05 20:05:38 -0700614 debug(" do_mmc_init: index %d, bus width %d pwr_gpio %d cd_gpio %d\n",
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600615 dev_index, priv->width, gpio_get_number(&priv->pwr_gpio),
616 gpio_get_number(&priv->cd_gpio));
Stephen Warren85a6c072011-10-31 06:51:34 +0000617
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600618 priv->clock = 0;
Stephen Warrend26e24d2016-08-05 16:10:33 -0600619
620#ifdef CONFIG_TEGRA186
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600621 ret = reset_assert(&priv->reset_ctl);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600622 if (ret)
623 return ret;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600624 ret = clk_enable(&priv->clk);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600625 if (ret)
626 return ret;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600627 ret = clk_set_rate(&priv->clk, 20000000);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600628 if (IS_ERR_VALUE(ret))
629 return ret;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600630 ret = reset_deassert(&priv->reset_ctl);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600631 if (ret)
632 return ret;
633#else
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600634 clock_start_periph_pll(priv->mmc_id, CLOCK_ID_PERIPH, 20000000);
Stephen Warrendfbdc122016-05-12 12:11:23 -0600635#endif
Stephen Warren85a6c072011-10-31 06:51:34 +0000636
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600637 if (dm_gpio_is_valid(&priv->pwr_gpio))
638 dm_gpio_set_value(&priv->pwr_gpio, 1);
Stephen Warrenfba87542011-10-31 06:51:36 +0000639
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600640 memset(&priv->cfg, 0, sizeof(priv->cfg));
Tom Warren85f0ee42011-05-31 10:30:37 +0000641
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600642 priv->cfg.name = "Tegra SD/MMC";
643 priv->cfg.ops = &tegra_mmc_ops;
Tom Warren85f0ee42011-05-31 10:30:37 +0000644
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600645 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
646 priv->cfg.host_caps = 0;
647 if (priv->width == 8)
648 priv->cfg.host_caps |= MMC_MODE_8BIT;
649 if (priv->width >= 4)
650 priv->cfg.host_caps |= MMC_MODE_4BIT;
651 priv->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Tom Warren85f0ee42011-05-31 10:30:37 +0000652
653 /*
654 * min freq is for card identification, and is the highest
655 * low-speed SDIO card frequency (actually 400KHz)
656 * max freq is highest HS eMMC clock as per the SD/MMC spec
657 * (actually 52MHz)
Tom Warren85f0ee42011-05-31 10:30:37 +0000658 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600659 priv->cfg.f_min = 375000;
660 priv->cfg.f_max = 48000000;
Tom Warren85f0ee42011-05-31 10:30:37 +0000661
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600662 priv->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200663
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600664 mmc = mmc_create(&priv->cfg, priv);
Simon Glass7bcaf2a2015-05-04 11:31:16 -0600665 mmc->block_dev.removable = removable;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200666 if (mmc == NULL)
667 return -1;
Tom Warren85f0ee42011-05-31 10:30:37 +0000668
669 return 0;
670}
Tom Warren9745cf82013-02-21 12:31:30 +0000671
672/**
673 * Get the host address and peripheral ID for a node.
674 *
675 * @param blob fdt blob
676 * @param node Device index (0-3)
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600677 * @param priv Structure to fill in (reg, width, mmc_id)
Tom Warren9745cf82013-02-21 12:31:30 +0000678 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600679static int mmc_get_config(const void *blob, int node,
680 struct tegra_mmc_priv *priv, bool *removablep)
Tom Warren9745cf82013-02-21 12:31:30 +0000681{
682 debug("%s: node = %d\n", __func__, node);
683
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600684 priv->enabled = fdtdec_get_is_enabled(blob, node);
Tom Warren9745cf82013-02-21 12:31:30 +0000685
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600686 priv->reg = (struct tegra_mmc *)fdtdec_get_addr(blob, node, "reg");
687 if ((fdt_addr_t)priv->reg == FDT_ADDR_T_NONE) {
Tom Warren9745cf82013-02-21 12:31:30 +0000688 debug("%s: no sdmmc base reg info found\n", __func__);
689 return -FDT_ERR_NOTFOUND;
690 }
691
Stephen Warrend26e24d2016-08-05 16:10:33 -0600692#ifdef CONFIG_TEGRA186
693 {
694 /*
695 * FIXME: This variable should go away when the MMC device
696 * actually is a udevice.
697 */
698 struct udevice dev;
699 int ret;
700 dev.of_offset = node;
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600701 ret = reset_get_by_name(&dev, "sdhci", &priv->reset_ctl);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600702 if (ret) {
Stephen Warren0a81e582016-08-18 11:08:44 -0600703 debug("reset_get_by_name() failed: %d\n", ret);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600704 return ret;
705 }
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600706 ret = clk_get_by_index(&dev, 0, &priv->clk);
Stephen Warrend26e24d2016-08-05 16:10:33 -0600707 if (ret) {
708 debug("clk_get_by_index() failed: %d\n", ret);
709 return ret;
710 }
711 }
712#else
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600713 priv->mmc_id = clock_decode_periph_id(blob, node);
714 if (priv->mmc_id == PERIPH_ID_NONE) {
Tom Warren9745cf82013-02-21 12:31:30 +0000715 debug("%s: could not decode periph id\n", __func__);
716 return -FDT_ERR_NOTFOUND;
717 }
Stephen Warrendfbdc122016-05-12 12:11:23 -0600718#endif
Tom Warren9745cf82013-02-21 12:31:30 +0000719
720 /*
721 * NOTE: mmc->bus_width is determined by mmc.c dynamically.
722 * TBD: Override it with this value?
723 */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600724 priv->width = fdtdec_get_int(blob, node, "bus-width", 0);
725 if (!priv->width)
Tom Warren9745cf82013-02-21 12:31:30 +0000726 debug("%s: no sdmmc width found\n", __func__);
727
728 /* These GPIOs are optional */
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600729 gpio_request_by_name_nodev(blob, node, "cd-gpios", 0, &priv->cd_gpio,
Simon Glassa30d4ba2015-01-05 20:05:38 -0700730 GPIOD_IS_IN);
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600731 gpio_request_by_name_nodev(blob, node, "wp-gpios", 0, &priv->wp_gpio,
Simon Glassa30d4ba2015-01-05 20:05:38 -0700732 GPIOD_IS_IN);
733 gpio_request_by_name_nodev(blob, node, "power-gpios", 0,
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600734 &priv->pwr_gpio, GPIOD_IS_OUT);
Simon Glass7bcaf2a2015-05-04 11:31:16 -0600735 *removablep = !fdtdec_get_bool(blob, node, "non-removable");
Tom Warren9745cf82013-02-21 12:31:30 +0000736
737 debug("%s: found controller at %p, width = %d, periph_id = %d\n",
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600738 __func__, priv->reg, priv->width,
Stephen Warrendfbdc122016-05-12 12:11:23 -0600739#ifndef CONFIG_TEGRA186
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600740 priv->mmc_id
Stephen Warrendfbdc122016-05-12 12:11:23 -0600741#else
742 -1
743#endif
744 );
Tom Warren9745cf82013-02-21 12:31:30 +0000745 return 0;
746}
747
748/*
749 * Process a list of nodes, adding them to our list of SDMMC ports.
750 *
751 * @param blob fdt blob
752 * @param node_list list of nodes to process (any <=0 are ignored)
753 * @param count number of nodes to process
754 * @return 0 if ok, -1 on error
755 */
756static int process_nodes(const void *blob, int node_list[], int count)
757{
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600758 struct tegra_mmc_priv *priv;
Simon Glass7bcaf2a2015-05-04 11:31:16 -0600759 bool removable;
Tom Warren9745cf82013-02-21 12:31:30 +0000760 int i, node;
761
762 debug("%s: count = %d\n", __func__, count);
763
764 /* build mmc_host[] for each controller */
765 for (i = 0; i < count; i++) {
766 node = node_list[i];
767 if (node <= 0)
768 continue;
769
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600770 priv = &mmc_host[i];
771 priv->id = i;
Tom Warren9745cf82013-02-21 12:31:30 +0000772
Stephen Warren6d9ccbb2016-09-13 10:45:46 -0600773 if (mmc_get_config(blob, node, priv, &removable)) {
Tom Warren9745cf82013-02-21 12:31:30 +0000774 printf("%s: failed to decode dev %d\n", __func__, i);
775 return -1;
776 }
Simon Glass7bcaf2a2015-05-04 11:31:16 -0600777 do_mmc_init(i, removable);
Tom Warren9745cf82013-02-21 12:31:30 +0000778 }
779 return 0;
780}
781
782void tegra_mmc_init(void)
783{
Stephen Warren2c0ea602014-04-18 10:56:11 -0600784 int node_list[CONFIG_SYS_MMC_MAX_DEVICE], count;
Tom Warren9745cf82013-02-21 12:31:30 +0000785 const void *blob = gd->fdt_blob;
786 debug("%s entry\n", __func__);
787
Stephen Warrendfbdc122016-05-12 12:11:23 -0600788 /* See if any Tegra186 MMC controllers are present */
Stephen Warrend55aadc2016-09-13 10:45:43 -0600789 count = fdtdec_find_aliases_for_id(blob, "mmc",
Stephen Warrendfbdc122016-05-12 12:11:23 -0600790 COMPAT_NVIDIA_TEGRA186_SDMMC, node_list,
791 CONFIG_SYS_MMC_MAX_DEVICE);
792 debug("%s: count of Tegra186 sdhci nodes is %d\n", __func__, count);
793 if (process_nodes(blob, node_list, count)) {
794 printf("%s: Error processing T186 mmc node(s)!\n", __func__);
795 return;
796 }
797
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700798 /* See if any Tegra210 MMC controllers are present */
Stephen Warrend55aadc2016-09-13 10:45:43 -0600799 count = fdtdec_find_aliases_for_id(blob, "mmc",
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700800 COMPAT_NVIDIA_TEGRA210_SDMMC, node_list,
801 CONFIG_SYS_MMC_MAX_DEVICE);
802 debug("%s: count of Tegra210 sdhci nodes is %d\n", __func__, count);
803 if (process_nodes(blob, node_list, count)) {
Simon Glassa40f7a22016-01-30 16:37:42 -0700804 printf("%s: Error processing T210 mmc node(s)!\n", __func__);
Tom Warrenab0cc6b2015-03-04 16:36:00 -0700805 return;
806 }
807
Stephen Warren65e0eb12014-01-24 12:46:06 -0700808 /* See if any Tegra124 MMC controllers are present */
Stephen Warrend55aadc2016-09-13 10:45:43 -0600809 count = fdtdec_find_aliases_for_id(blob, "mmc",
Stephen Warren2c0ea602014-04-18 10:56:11 -0600810 COMPAT_NVIDIA_TEGRA124_SDMMC, node_list,
811 CONFIG_SYS_MMC_MAX_DEVICE);
Stephen Warren65e0eb12014-01-24 12:46:06 -0700812 debug("%s: count of Tegra124 sdhci nodes is %d\n", __func__, count);
813 if (process_nodes(blob, node_list, count)) {
Simon Glassa40f7a22016-01-30 16:37:42 -0700814 printf("%s: Error processing T124 mmc node(s)!\n", __func__);
Stephen Warren65e0eb12014-01-24 12:46:06 -0700815 return;
816 }
817
Tom Warren35ae07b2013-02-26 12:31:26 -0700818 /* See if any Tegra30 MMC controllers are present */
Stephen Warrend55aadc2016-09-13 10:45:43 -0600819 count = fdtdec_find_aliases_for_id(blob, "mmc",
Stephen Warren2c0ea602014-04-18 10:56:11 -0600820 COMPAT_NVIDIA_TEGRA30_SDMMC, node_list,
821 CONFIG_SYS_MMC_MAX_DEVICE);
Tom Warren35ae07b2013-02-26 12:31:26 -0700822 debug("%s: count of T30 sdhci nodes is %d\n", __func__, count);
823 if (process_nodes(blob, node_list, count)) {
824 printf("%s: Error processing T30 mmc node(s)!\n", __func__);
825 return;
826 }
Tom Warren9745cf82013-02-21 12:31:30 +0000827
Tom Warren35ae07b2013-02-26 12:31:26 -0700828 /* Now look for any Tegra20 MMC controllers */
Stephen Warrend55aadc2016-09-13 10:45:43 -0600829 count = fdtdec_find_aliases_for_id(blob, "mmc",
Stephen Warren2c0ea602014-04-18 10:56:11 -0600830 COMPAT_NVIDIA_TEGRA20_SDMMC, node_list,
831 CONFIG_SYS_MMC_MAX_DEVICE);
Tom Warren35ae07b2013-02-26 12:31:26 -0700832 debug("%s: count of T20 sdhci nodes is %d\n", __func__, count);
Tom Warren9745cf82013-02-21 12:31:30 +0000833 if (process_nodes(blob, node_list, count)) {
Tom Warren35ae07b2013-02-26 12:31:26 -0700834 printf("%s: Error processing T20 mmc node(s)!\n", __func__);
Tom Warren9745cf82013-02-21 12:31:30 +0000835 return;
836 }
837}