blob: e86bc680ff2806ef3b4d9f1169cf81c72f5956b1 [file] [log] [blame]
Tom Warren21ef6a12011-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 Warrenc9aa8312013-02-21 12:31:30 +00005 * Portions Copyright 2011-2013 NVIDIA Corporation
Tom Warren21ef6a12011-05-31 10:30:37 +00006 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
Stephen Warren19815392012-11-06 11:27:30 +000022#include <bouncebuf.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000023#include <common.h>
Stephen Warren98778412011-10-31 06:51:36 +000024#include <asm/gpio.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000025#include <asm/io.h>
Simon Glass4ed59e72011-09-21 12:40:04 +000026#include <asm/arch/clock.h>
Tom Warren150c2492012-09-19 15:50:56 -070027#include <asm/arch-tegra/clk_rst.h>
28#include <asm/arch-tegra/tegra_mmc.h>
29#include <mmc.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000030
Tom Warrenc9aa8312013-02-21 12:31:30 +000031DECLARE_GLOBAL_DATA_PTR;
Tom Warren21ef6a12011-05-31 10:30:37 +000032
Tom Warrenc9aa8312013-02-21 12:31:30 +000033struct mmc mmc_dev[MAX_HOSTS];
34struct mmc_host mmc_host[MAX_HOSTS];
Simon Glass4ed59e72011-09-21 12:40:04 +000035
Tom Warrenc9aa8312013-02-21 12:31:30 +000036#ifndef CONFIG_OF_CONTROL
37#error "Please enable device tree support to use this driver"
38#endif
Tom Warren21ef6a12011-05-31 10:30:37 +000039
Tom Warren2d348a12013-02-26 12:31:26 -070040static void mmc_set_power(struct mmc_host *host, unsigned short power)
41{
42 u8 pwr = 0;
43 debug("%s: power = %x\n", __func__, power);
44
45 if (power != (unsigned short)-1) {
46 switch (1 << power) {
47 case MMC_VDD_165_195:
48 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V1_8;
49 break;
50 case MMC_VDD_29_30:
51 case MMC_VDD_30_31:
52 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_0;
53 break;
54 case MMC_VDD_32_33:
55 case MMC_VDD_33_34:
56 pwr = TEGRA_MMC_PWRCTL_SD_BUS_VOLTAGE_V3_3;
57 break;
58 }
59 }
60 debug("%s: pwr = %X\n", __func__, pwr);
61
62 /* Set the bus voltage first (if any) */
63 writeb(pwr, &host->reg->pwrcon);
64 if (pwr == 0)
65 return;
66
67 /* Now enable bus power */
68 pwr |= TEGRA_MMC_PWRCTL_SD_BUS_POWER;
69 writeb(pwr, &host->reg->pwrcon);
70}
71
Stephen Warren19815392012-11-06 11:27:30 +000072static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data,
73 struct bounce_buffer *bbstate)
Tom Warren21ef6a12011-05-31 10:30:37 +000074{
75 unsigned char ctrl;
76
Tom Warren21ef6a12011-05-31 10:30:37 +000077
Stephen Warren19815392012-11-06 11:27:30 +000078 debug("buf: %p (%p), data->blocks: %u, data->blocksize: %u\n",
79 bbstate->bounce_buffer, bbstate->user_buffer, data->blocks,
80 data->blocksize);
81
82 writel((u32)bbstate->bounce_buffer, &host->reg->sysad);
Tom Warren21ef6a12011-05-31 10:30:37 +000083 /*
84 * DMASEL[4:3]
85 * 00 = Selects SDMA
86 * 01 = Reserved
87 * 10 = Selects 32-bit Address ADMA2
88 * 11 = Selects 64-bit Address ADMA2
89 */
90 ctrl = readb(&host->reg->hostctl);
Anton staaf8e42f0d2011-11-10 11:56:49 +000091 ctrl &= ~TEGRA_MMC_HOSTCTL_DMASEL_MASK;
92 ctrl |= TEGRA_MMC_HOSTCTL_DMASEL_SDMA;
Tom Warren21ef6a12011-05-31 10:30:37 +000093 writeb(ctrl, &host->reg->hostctl);
94
95 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
96 writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
97 writew(data->blocks, &host->reg->blkcnt);
98}
99
100static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
101{
102 unsigned short mode;
103 debug(" mmc_set_transfer_mode called\n");
104 /*
105 * TRNMOD
106 * MUL1SIN0[5] : Multi/Single Block Select
107 * RD1WT0[4] : Data Transfer Direction Select
108 * 1 = read
109 * 0 = write
110 * ENACMD12[2] : Auto CMD12 Enable
111 * ENBLKCNT[1] : Block Count Enable
112 * ENDMA[0] : DMA Enable
113 */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000114 mode = (TEGRA_MMC_TRNMOD_DMA_ENABLE |
115 TEGRA_MMC_TRNMOD_BLOCK_COUNT_ENABLE);
116
Tom Warren21ef6a12011-05-31 10:30:37 +0000117 if (data->blocks > 1)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000118 mode |= TEGRA_MMC_TRNMOD_MULTI_BLOCK_SELECT;
119
Tom Warren21ef6a12011-05-31 10:30:37 +0000120 if (data->flags & MMC_DATA_READ)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000121 mode |= TEGRA_MMC_TRNMOD_DATA_XFER_DIR_SEL_READ;
Tom Warren21ef6a12011-05-31 10:30:37 +0000122
123 writew(mode, &host->reg->trnmod);
124}
125
Anton staaf0963ff32011-11-10 11:56:52 +0000126static int mmc_wait_inhibit(struct mmc_host *host,
127 struct mmc_cmd *cmd,
128 struct mmc_data *data,
129 unsigned int timeout)
Tom Warren21ef6a12011-05-31 10:30:37 +0000130{
Tom Warren21ef6a12011-05-31 10:30:37 +0000131 /*
132 * PRNSTS
Anton staaf0963ff32011-11-10 11:56:52 +0000133 * CMDINHDAT[1] : Command Inhibit (DAT)
134 * CMDINHCMD[0] : Command Inhibit (CMD)
Tom Warren21ef6a12011-05-31 10:30:37 +0000135 */
Anton staaf0963ff32011-11-10 11:56:52 +0000136 unsigned int mask = TEGRA_MMC_PRNSTS_CMD_INHIBIT_CMD;
Tom Warren21ef6a12011-05-31 10:30:37 +0000137
138 /*
139 * We shouldn't wait for data inhibit for stop commands, even
140 * though they might use busy signaling
141 */
Anton staaf0963ff32011-11-10 11:56:52 +0000142 if ((data == NULL) && (cmd->resp_type & MMC_RSP_BUSY))
143 mask |= TEGRA_MMC_PRNSTS_CMD_INHIBIT_DAT;
Tom Warren21ef6a12011-05-31 10:30:37 +0000144
145 while (readl(&host->reg->prnsts) & mask) {
146 if (timeout == 0) {
147 printf("%s: timeout error\n", __func__);
148 return -1;
149 }
150 timeout--;
151 udelay(1000);
152 }
153
Anton staaf0963ff32011-11-10 11:56:52 +0000154 return 0;
155}
156
Stephen Warren19815392012-11-06 11:27:30 +0000157static int mmc_send_cmd_bounced(struct mmc *mmc, struct mmc_cmd *cmd,
158 struct mmc_data *data, struct bounce_buffer *bbstate)
Anton staaf0963ff32011-11-10 11:56:52 +0000159{
160 struct mmc_host *host = (struct mmc_host *)mmc->priv;
161 int flags, i;
162 int result;
Anatolij Gustschin60e242e2012-03-28 03:40:00 +0000163 unsigned int mask = 0;
Anton staaf0963ff32011-11-10 11:56:52 +0000164 unsigned int retry = 0x100000;
165 debug(" mmc_send_cmd called\n");
166
167 result = mmc_wait_inhibit(host, cmd, data, 10 /* ms */);
168
169 if (result < 0)
170 return result;
171
Tom Warren21ef6a12011-05-31 10:30:37 +0000172 if (data)
Stephen Warren19815392012-11-06 11:27:30 +0000173 mmc_prepare_data(host, data, bbstate);
Tom Warren21ef6a12011-05-31 10:30:37 +0000174
175 debug("cmd->arg: %08x\n", cmd->cmdarg);
176 writel(cmd->cmdarg, &host->reg->argument);
177
178 if (data)
179 mmc_set_transfer_mode(host, data);
180
181 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
182 return -1;
183
184 /*
185 * CMDREG
186 * CMDIDX[13:8] : Command index
187 * DATAPRNT[5] : Data Present Select
188 * ENCMDIDX[4] : Command Index Check Enable
189 * ENCMDCRC[3] : Command CRC Check Enable
190 * RSPTYP[1:0]
191 * 00 = No Response
192 * 01 = Length 136
193 * 10 = Length 48
194 * 11 = Length 48 Check busy after response
195 */
196 if (!(cmd->resp_type & MMC_RSP_PRESENT))
Anton staaf8e42f0d2011-11-10 11:56:49 +0000197 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_NO_RESPONSE;
Tom Warren21ef6a12011-05-31 10:30:37 +0000198 else if (cmd->resp_type & MMC_RSP_136)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000199 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_136;
Tom Warren21ef6a12011-05-31 10:30:37 +0000200 else if (cmd->resp_type & MMC_RSP_BUSY)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000201 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48_BUSY;
Tom Warren21ef6a12011-05-31 10:30:37 +0000202 else
Anton staaf8e42f0d2011-11-10 11:56:49 +0000203 flags = TEGRA_MMC_CMDREG_RESP_TYPE_SELECT_LENGTH_48;
Tom Warren21ef6a12011-05-31 10:30:37 +0000204
205 if (cmd->resp_type & MMC_RSP_CRC)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000206 flags |= TEGRA_MMC_TRNMOD_CMD_CRC_CHECK;
Tom Warren21ef6a12011-05-31 10:30:37 +0000207 if (cmd->resp_type & MMC_RSP_OPCODE)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000208 flags |= TEGRA_MMC_TRNMOD_CMD_INDEX_CHECK;
Tom Warren21ef6a12011-05-31 10:30:37 +0000209 if (data)
Anton staaf8e42f0d2011-11-10 11:56:49 +0000210 flags |= TEGRA_MMC_TRNMOD_DATA_PRESENT_SELECT_DATA_TRANSFER;
Tom Warren21ef6a12011-05-31 10:30:37 +0000211
212 debug("cmd: %d\n", cmd->cmdidx);
213
214 writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
215
216 for (i = 0; i < retry; i++) {
217 mask = readl(&host->reg->norintsts);
218 /* Command Complete */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000219 if (mask & TEGRA_MMC_NORINTSTS_CMD_COMPLETE) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000220 if (!data)
221 writel(mask, &host->reg->norintsts);
222 break;
223 }
224 }
225
226 if (i == retry) {
227 printf("%s: waiting for status update\n", __func__);
Tom Warrencf39cf52012-02-07 06:17:16 +0000228 writel(mask, &host->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000229 return TIMEOUT;
230 }
231
Anton staaf8e42f0d2011-11-10 11:56:49 +0000232 if (mask & TEGRA_MMC_NORINTSTS_CMD_TIMEOUT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000233 /* Timeout Error */
234 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Tom Warrencf39cf52012-02-07 06:17:16 +0000235 writel(mask, &host->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000236 return TIMEOUT;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000237 } else if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000238 /* Error Interrupt */
239 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Tom Warrencf39cf52012-02-07 06:17:16 +0000240 writel(mask, &host->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000241 return -1;
242 }
243
244 if (cmd->resp_type & MMC_RSP_PRESENT) {
245 if (cmd->resp_type & MMC_RSP_136) {
246 /* CRC is stripped so we need to do some shifting. */
247 for (i = 0; i < 4; i++) {
248 unsigned int offset =
249 (unsigned int)(&host->reg->rspreg3 - i);
250 cmd->response[i] = readl(offset) << 8;
251
252 if (i != 3) {
253 cmd->response[i] |=
254 readb(offset - 1);
255 }
256 debug("cmd->resp[%d]: %08x\n",
257 i, cmd->response[i]);
258 }
259 } else if (cmd->resp_type & MMC_RSP_BUSY) {
260 for (i = 0; i < retry; i++) {
261 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
262 if (readl(&host->reg->prnsts)
263 & (1 << 20)) /* DAT[0] */
264 break;
265 }
266
267 if (i == retry) {
268 printf("%s: card is still busy\n", __func__);
Tom Warrencf39cf52012-02-07 06:17:16 +0000269 writel(mask, &host->reg->norintsts);
Tom Warren21ef6a12011-05-31 10:30:37 +0000270 return TIMEOUT;
271 }
272
273 cmd->response[0] = readl(&host->reg->rspreg0);
274 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
275 } else {
276 cmd->response[0] = readl(&host->reg->rspreg0);
277 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
278 }
279 }
280
281 if (data) {
Anton staaf9b3d1872011-11-10 11:56:51 +0000282 unsigned long start = get_timer(0);
283
Tom Warren21ef6a12011-05-31 10:30:37 +0000284 while (1) {
285 mask = readl(&host->reg->norintsts);
286
Anton staaf8e42f0d2011-11-10 11:56:49 +0000287 if (mask & TEGRA_MMC_NORINTSTS_ERR_INTERRUPT) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000288 /* Error Interrupt */
289 writel(mask, &host->reg->norintsts);
290 printf("%s: error during transfer: 0x%08x\n",
291 __func__, mask);
292 return -1;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000293 } else if (mask & TEGRA_MMC_NORINTSTS_DMA_INTERRUPT) {
Anton staaf5a762e22011-11-10 11:56:50 +0000294 /*
295 * DMA Interrupt, restart the transfer where
296 * it was interrupted.
297 */
298 unsigned int address = readl(&host->reg->sysad);
299
Tom Warren21ef6a12011-05-31 10:30:37 +0000300 debug("DMA end\n");
Anton staaf5a762e22011-11-10 11:56:50 +0000301 writel(TEGRA_MMC_NORINTSTS_DMA_INTERRUPT,
302 &host->reg->norintsts);
303 writel(address, &host->reg->sysad);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000304 } else if (mask & TEGRA_MMC_NORINTSTS_XFER_COMPLETE) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000305 /* Transfer Complete */
306 debug("r/w is done\n");
307 break;
Anton staaf9b3d1872011-11-10 11:56:51 +0000308 } else if (get_timer(start) > 2000UL) {
309 writel(mask, &host->reg->norintsts);
310 printf("%s: MMC Timeout\n"
311 " Interrupt status 0x%08x\n"
312 " Interrupt status enable 0x%08x\n"
313 " Interrupt signal enable 0x%08x\n"
314 " Present status 0x%08x\n",
315 __func__, mask,
316 readl(&host->reg->norintstsen),
317 readl(&host->reg->norintsigen),
318 readl(&host->reg->prnsts));
319 return -1;
Tom Warren21ef6a12011-05-31 10:30:37 +0000320 }
321 }
322 writel(mask, &host->reg->norintsts);
323 }
324
325 udelay(1000);
326 return 0;
327}
328
Stephen Warren19815392012-11-06 11:27:30 +0000329static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
330 struct mmc_data *data)
331{
332 void *buf;
333 unsigned int bbflags;
334 size_t len;
335 struct bounce_buffer bbstate;
336 int ret;
337
338 if (data) {
339 if (data->flags & MMC_DATA_READ) {
340 buf = data->dest;
341 bbflags = GEN_BB_WRITE;
342 } else {
343 buf = (void *)data->src;
344 bbflags = GEN_BB_READ;
345 }
346 len = data->blocks * data->blocksize;
347
348 bounce_buffer_start(&bbstate, buf, len, bbflags);
349 }
350
351 ret = mmc_send_cmd_bounced(mmc, cmd, data, &bbstate);
352
353 if (data)
354 bounce_buffer_stop(&bbstate);
355
356 return ret;
357}
358
Tom Warren21ef6a12011-05-31 10:30:37 +0000359static void mmc_change_clock(struct mmc_host *host, uint clock)
360{
Simon Glass4ed59e72011-09-21 12:40:04 +0000361 int div;
Tom Warren21ef6a12011-05-31 10:30:37 +0000362 unsigned short clk;
363 unsigned long timeout;
Simon Glass4ed59e72011-09-21 12:40:04 +0000364
Tom Warren21ef6a12011-05-31 10:30:37 +0000365 debug(" mmc_change_clock called\n");
366
Simon Glass4ed59e72011-09-21 12:40:04 +0000367 /*
Tom Warren2d348a12013-02-26 12:31:26 -0700368 * Change Tegra SDMMCx clock divisor here. Source is PLLP_OUT0
Simon Glass4ed59e72011-09-21 12:40:04 +0000369 */
Tom Warren21ef6a12011-05-31 10:30:37 +0000370 if (clock == 0)
371 goto out;
Simon Glass4ed59e72011-09-21 12:40:04 +0000372 clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
373 &div);
374 debug("div = %d\n", div);
Tom Warren21ef6a12011-05-31 10:30:37 +0000375
376 writew(0, &host->reg->clkcon);
377
Tom Warren21ef6a12011-05-31 10:30:37 +0000378 /*
379 * CLKCON
380 * SELFREQ[15:8] : base clock divided by value
381 * ENSDCLK[2] : SD Clock Enable
382 * STBLINTCLK[1] : Internal Clock Stable
383 * ENINTCLK[0] : Internal Clock Enable
384 */
Simon Glass4ed59e72011-09-21 12:40:04 +0000385 div >>= 1;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000386 clk = ((div << TEGRA_MMC_CLKCON_SDCLK_FREQ_SEL_SHIFT) |
387 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_ENABLE);
Tom Warren21ef6a12011-05-31 10:30:37 +0000388 writew(clk, &host->reg->clkcon);
389
390 /* Wait max 10 ms */
391 timeout = 10;
Anton staaf8e42f0d2011-11-10 11:56:49 +0000392 while (!(readw(&host->reg->clkcon) &
393 TEGRA_MMC_CLKCON_INTERNAL_CLOCK_STABLE)) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000394 if (timeout == 0) {
395 printf("%s: timeout error\n", __func__);
396 return;
397 }
398 timeout--;
399 udelay(1000);
400 }
401
Anton staaf8e42f0d2011-11-10 11:56:49 +0000402 clk |= TEGRA_MMC_CLKCON_SD_CLOCK_ENABLE;
Tom Warren21ef6a12011-05-31 10:30:37 +0000403 writew(clk, &host->reg->clkcon);
404
405 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren21ef6a12011-05-31 10:30:37 +0000406
407out:
408 host->clock = clock;
409}
410
411static void mmc_set_ios(struct mmc *mmc)
412{
413 struct mmc_host *host = mmc->priv;
414 unsigned char ctrl;
415 debug(" mmc_set_ios called\n");
416
417 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
418
419 /* Change clock first */
Tom Warren21ef6a12011-05-31 10:30:37 +0000420 mmc_change_clock(host, mmc->clock);
421
422 ctrl = readb(&host->reg->hostctl);
423
424 /*
425 * WIDE8[5]
426 * 0 = Depend on WIDE4
427 * 1 = 8-bit mode
428 * WIDE4[1]
429 * 1 = 4-bit mode
430 * 0 = 1-bit mode
431 */
432 if (mmc->bus_width == 8)
433 ctrl |= (1 << 5);
434 else if (mmc->bus_width == 4)
435 ctrl |= (1 << 1);
436 else
437 ctrl &= ~(1 << 1);
438
439 writeb(ctrl, &host->reg->hostctl);
440 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
441}
442
Tom Warren2d348a12013-02-26 12:31:26 -0700443static void mmc_reset(struct mmc_host *host, struct mmc *mmc)
Tom Warren21ef6a12011-05-31 10:30:37 +0000444{
445 unsigned int timeout;
446 debug(" mmc_reset called\n");
447
448 /*
449 * RSTALL[0] : Software reset for all
450 * 1 = reset
451 * 0 = work
452 */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000453 writeb(TEGRA_MMC_SWRST_SW_RESET_FOR_ALL, &host->reg->swrst);
Tom Warren21ef6a12011-05-31 10:30:37 +0000454
455 host->clock = 0;
456
457 /* Wait max 100 ms */
458 timeout = 100;
459
460 /* hw clears the bit when it's done */
Anton staaf8e42f0d2011-11-10 11:56:49 +0000461 while (readb(&host->reg->swrst) & TEGRA_MMC_SWRST_SW_RESET_FOR_ALL) {
Tom Warren21ef6a12011-05-31 10:30:37 +0000462 if (timeout == 0) {
463 printf("%s: timeout error\n", __func__);
464 return;
465 }
466 timeout--;
467 udelay(1000);
468 }
Tom Warren2d348a12013-02-26 12:31:26 -0700469
470 /* Set SD bus voltage & enable bus power */
471 mmc_set_power(host, fls(mmc->voltages) - 1);
472 debug("%s: power control = %02X, host control = %02X\n", __func__,
473 readb(&host->reg->pwrcon), readb(&host->reg->hostctl));
474
475 /* Make sure SDIO pads are set up */
476 pad_init_mmc(host);
Tom Warren21ef6a12011-05-31 10:30:37 +0000477}
478
479static int mmc_core_init(struct mmc *mmc)
480{
481 struct mmc_host *host = (struct mmc_host *)mmc->priv;
482 unsigned int mask;
483 debug(" mmc_core_init called\n");
484
Tom Warren2d348a12013-02-26 12:31:26 -0700485 mmc_reset(host, mmc);
Tom Warren21ef6a12011-05-31 10:30:37 +0000486
487 host->version = readw(&host->reg->hcver);
488 debug("host version = %x\n", host->version);
489
490 /* mask all */
491 writel(0xffffffff, &host->reg->norintstsen);
492 writel(0xffffffff, &host->reg->norintsigen);
493
494 writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
495 /*
496 * NORMAL Interrupt Status Enable Register init
497 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
498 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
Anton staaf5a762e22011-11-10 11:56:50 +0000499 * [3] ENSTADMAINT : DMA boundary interrupt
Tom Warren21ef6a12011-05-31 10:30:37 +0000500 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
501 * [0] ENSTACMDCMPLT : Command Complete Status Enable
502 */
503 mask = readl(&host->reg->norintstsen);
504 mask &= ~(0xffff);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000505 mask |= (TEGRA_MMC_NORINTSTSEN_CMD_COMPLETE |
506 TEGRA_MMC_NORINTSTSEN_XFER_COMPLETE |
Anton staaf5a762e22011-11-10 11:56:50 +0000507 TEGRA_MMC_NORINTSTSEN_DMA_INTERRUPT |
Anton staaf8e42f0d2011-11-10 11:56:49 +0000508 TEGRA_MMC_NORINTSTSEN_BUFFER_WRITE_READY |
509 TEGRA_MMC_NORINTSTSEN_BUFFER_READ_READY);
Tom Warren21ef6a12011-05-31 10:30:37 +0000510 writel(mask, &host->reg->norintstsen);
511
512 /*
513 * NORMAL Interrupt Signal Enable Register init
514 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
515 */
516 mask = readl(&host->reg->norintsigen);
517 mask &= ~(0xffff);
Anton staaf8e42f0d2011-11-10 11:56:49 +0000518 mask |= TEGRA_MMC_NORINTSIGEN_XFER_COMPLETE;
Tom Warren21ef6a12011-05-31 10:30:37 +0000519 writel(mask, &host->reg->norintsigen);
520
521 return 0;
522}
523
Tom Warren29f3e3f2012-09-04 17:00:24 -0700524int tegra_mmc_getcd(struct mmc *mmc)
Thierry Redingbf836622012-01-02 01:15:39 +0000525{
526 struct mmc_host *host = (struct mmc_host *)mmc->priv;
527
Tom Warren29f3e3f2012-09-04 17:00:24 -0700528 debug("tegra_mmc_getcd called\n");
Thierry Redingbf836622012-01-02 01:15:39 +0000529
Tom Warrenc9aa8312013-02-21 12:31:30 +0000530 if (fdt_gpio_isvalid(&host->cd_gpio))
531 return fdtdec_get_gpio(&host->cd_gpio);
Thierry Redingbf836622012-01-02 01:15:39 +0000532
533 return 1;
534}
535
Tom Warrenc9aa8312013-02-21 12:31:30 +0000536static int do_mmc_init(int dev_index)
Tom Warren21ef6a12011-05-31 10:30:37 +0000537{
Stephen Warrende71fbe2011-10-31 06:51:34 +0000538 struct mmc_host *host;
Stephen Warren98778412011-10-31 06:51:36 +0000539 char gpusage[12]; /* "SD/MMCn PWR" or "SD/MMCn CD" */
Tom Warren21ef6a12011-05-31 10:30:37 +0000540 struct mmc *mmc;
541
Tom Warrenc9aa8312013-02-21 12:31:30 +0000542 /* DT should have been read & host config filled in */
Stephen Warrende71fbe2011-10-31 06:51:34 +0000543 host = &mmc_host[dev_index];
Tom Warrenc9aa8312013-02-21 12:31:30 +0000544 if (!host->enabled)
545 return -1;
546
547 debug(" do_mmc_init: index %d, bus width %d "
548 "pwr_gpio %d cd_gpio %d\n",
549 dev_index, host->width,
550 host->pwr_gpio.gpio, host->cd_gpio.gpio);
Stephen Warrende71fbe2011-10-31 06:51:34 +0000551
552 host->clock = 0;
Stephen Warrende71fbe2011-10-31 06:51:34 +0000553 clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
554
Tom Warrenc9aa8312013-02-21 12:31:30 +0000555 if (fdt_gpio_isvalid(&host->pwr_gpio)) {
Stephen Warren98778412011-10-31 06:51:36 +0000556 sprintf(gpusage, "SD/MMC%d PWR", dev_index);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000557 gpio_request(host->pwr_gpio.gpio, gpusage);
558 gpio_direction_output(host->pwr_gpio.gpio, 1);
559 debug(" Power GPIO name = %s\n", host->pwr_gpio.name);
Stephen Warren98778412011-10-31 06:51:36 +0000560 }
561
Tom Warrenc9aa8312013-02-21 12:31:30 +0000562 if (fdt_gpio_isvalid(&host->cd_gpio)) {
Stephen Warren98778412011-10-31 06:51:36 +0000563 sprintf(gpusage, "SD/MMC%d CD", dev_index);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000564 gpio_request(host->cd_gpio.gpio, gpusage);
565 gpio_direction_input(host->cd_gpio.gpio);
566 debug(" CD GPIO name = %s\n", host->cd_gpio.name);
Stephen Warren98778412011-10-31 06:51:36 +0000567 }
568
Tom Warren21ef6a12011-05-31 10:30:37 +0000569 mmc = &mmc_dev[dev_index];
570
Tom Warren29f3e3f2012-09-04 17:00:24 -0700571 sprintf(mmc->name, "Tegra SD/MMC");
Stephen Warrende71fbe2011-10-31 06:51:34 +0000572 mmc->priv = host;
Tom Warren21ef6a12011-05-31 10:30:37 +0000573 mmc->send_cmd = mmc_send_cmd;
574 mmc->set_ios = mmc_set_ios;
575 mmc->init = mmc_core_init;
Tom Warren29f3e3f2012-09-04 17:00:24 -0700576 mmc->getcd = tegra_mmc_getcd;
Nikita Kiryanovd23d8d72012-12-03 02:19:46 +0000577 mmc->getwp = NULL;
Tom Warren21ef6a12011-05-31 10:30:37 +0000578
579 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Stephen Warren8c0ec0d2012-11-02 06:56:12 +0000580 mmc->host_caps = 0;
Tom Warrenc9aa8312013-02-21 12:31:30 +0000581 if (host->width == 8)
Stephen Warren8c0ec0d2012-11-02 06:56:12 +0000582 mmc->host_caps |= MMC_MODE_8BIT;
Tom Warrenc9aa8312013-02-21 12:31:30 +0000583 if (host->width >= 4)
Stephen Warren8c0ec0d2012-11-02 06:56:12 +0000584 mmc->host_caps |= MMC_MODE_4BIT;
Tom Warrenccf79882011-09-21 12:40:07 +0000585 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
Tom Warren21ef6a12011-05-31 10:30:37 +0000586
587 /*
588 * min freq is for card identification, and is the highest
589 * low-speed SDIO card frequency (actually 400KHz)
590 * max freq is highest HS eMMC clock as per the SD/MMC spec
591 * (actually 52MHz)
Tom Warren21ef6a12011-05-31 10:30:37 +0000592 */
593 mmc->f_min = 375000;
594 mmc->f_max = 48000000;
595
Tom Warren21ef6a12011-05-31 10:30:37 +0000596 mmc_register(mmc);
597
598 return 0;
599}
Tom Warrenc9aa8312013-02-21 12:31:30 +0000600
601/**
602 * Get the host address and peripheral ID for a node.
603 *
604 * @param blob fdt blob
605 * @param node Device index (0-3)
606 * @param host Structure to fill in (reg, width, mmc_id)
607 */
608static int mmc_get_config(const void *blob, int node, struct mmc_host *host)
609{
610 debug("%s: node = %d\n", __func__, node);
611
612 host->enabled = fdtdec_get_is_enabled(blob, node);
613
614 host->reg = (struct tegra_mmc *)fdtdec_get_addr(blob, node, "reg");
615 if ((fdt_addr_t)host->reg == FDT_ADDR_T_NONE) {
616 debug("%s: no sdmmc base reg info found\n", __func__);
617 return -FDT_ERR_NOTFOUND;
618 }
619
620 host->mmc_id = clock_decode_periph_id(blob, node);
621 if (host->mmc_id == PERIPH_ID_NONE) {
622 debug("%s: could not decode periph id\n", __func__);
623 return -FDT_ERR_NOTFOUND;
624 }
625
626 /*
627 * NOTE: mmc->bus_width is determined by mmc.c dynamically.
628 * TBD: Override it with this value?
629 */
630 host->width = fdtdec_get_int(blob, node, "bus-width", 0);
631 if (!host->width)
632 debug("%s: no sdmmc width found\n", __func__);
633
634 /* These GPIOs are optional */
635 fdtdec_decode_gpio(blob, node, "cd-gpios", &host->cd_gpio);
636 fdtdec_decode_gpio(blob, node, "wp-gpios", &host->wp_gpio);
637 fdtdec_decode_gpio(blob, node, "power-gpios", &host->pwr_gpio);
638
639 debug("%s: found controller at %p, width = %d, periph_id = %d\n",
640 __func__, host->reg, host->width, host->mmc_id);
641 return 0;
642}
643
644/*
645 * Process a list of nodes, adding them to our list of SDMMC ports.
646 *
647 * @param blob fdt blob
648 * @param node_list list of nodes to process (any <=0 are ignored)
649 * @param count number of nodes to process
650 * @return 0 if ok, -1 on error
651 */
652static int process_nodes(const void *blob, int node_list[], int count)
653{
654 struct mmc_host *host;
655 int i, node;
656
657 debug("%s: count = %d\n", __func__, count);
658
659 /* build mmc_host[] for each controller */
660 for (i = 0; i < count; i++) {
661 node = node_list[i];
662 if (node <= 0)
663 continue;
664
665 host = &mmc_host[i];
666 host->id = i;
667
668 if (mmc_get_config(blob, node, host)) {
669 printf("%s: failed to decode dev %d\n", __func__, i);
670 return -1;
671 }
672 do_mmc_init(i);
673 }
674 return 0;
675}
676
677void tegra_mmc_init(void)
678{
679 int node_list[MAX_HOSTS], count;
680 const void *blob = gd->fdt_blob;
681 debug("%s entry\n", __func__);
682
Tom Warren2d348a12013-02-26 12:31:26 -0700683 /* See if any Tegra30 MMC controllers are present */
684 count = fdtdec_find_aliases_for_id(blob, "sdhci",
685 COMPAT_NVIDIA_TEGRA30_SDMMC, node_list, MAX_HOSTS);
686 debug("%s: count of T30 sdhci nodes is %d\n", __func__, count);
687 if (process_nodes(blob, node_list, count)) {
688 printf("%s: Error processing T30 mmc node(s)!\n", __func__);
689 return;
690 }
691
692 /* Now look for any Tegra20 MMC controllers */
Tom Warrenc9aa8312013-02-21 12:31:30 +0000693 count = fdtdec_find_aliases_for_id(blob, "sdhci",
694 COMPAT_NVIDIA_TEGRA20_SDMMC, node_list, MAX_HOSTS);
Tom Warren2d348a12013-02-26 12:31:26 -0700695 debug("%s: count of T20 sdhci nodes is %d\n", __func__, count);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000696 if (process_nodes(blob, node_list, count)) {
Tom Warren2d348a12013-02-26 12:31:26 -0700697 printf("%s: Error processing T20 mmc node(s)!\n", __func__);
Tom Warrenc9aa8312013-02-21 12:31:30 +0000698 return;
699 }
700}