blob: 78b11900ed12683337e5879f9ac3459cf21de52c [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>
5 * Portions Copyright 2011 NVIDIA Corporation
6 *
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
22#include <common.h>
23#include <mmc.h>
24#include <asm/io.h>
25#include <asm/arch/clk_rst.h>
Simon Glass4ed59e72011-09-21 12:40:04 +000026#include <asm/arch/clock.h>
Tom Warren21ef6a12011-05-31 10:30:37 +000027#include "tegra2_mmc.h"
28
29/* support 4 mmc hosts */
30struct mmc mmc_dev[4];
31struct mmc_host mmc_host[4];
32
Simon Glass4ed59e72011-09-21 12:40:04 +000033
34/**
35 * Get the host address and peripheral ID for a device. Devices are numbered
36 * from 0 to 3.
37 *
38 * @param host Structure to fill in (base, reg, mmc_id)
39 * @param dev_index Device index (0-3)
40 */
41static void tegra2_get_setup(struct mmc_host *host, int dev_index)
Tom Warren21ef6a12011-05-31 10:30:37 +000042{
Tom Warren21ef6a12011-05-31 10:30:37 +000043 debug("tegra2_get_base_mmc: dev_index = %d\n", dev_index);
44
45 switch (dev_index) {
Tom Warren21ef6a12011-05-31 10:30:37 +000046 case 1:
Simon Glass4ed59e72011-09-21 12:40:04 +000047 host->base = TEGRA2_SDMMC3_BASE;
48 host->mmc_id = PERIPH_ID_SDMMC3;
Tom Warren21ef6a12011-05-31 10:30:37 +000049 break;
50 case 2:
Simon Glass4ed59e72011-09-21 12:40:04 +000051 host->base = TEGRA2_SDMMC2_BASE;
52 host->mmc_id = PERIPH_ID_SDMMC2;
Tom Warren21ef6a12011-05-31 10:30:37 +000053 break;
54 case 3:
Simon Glass4ed59e72011-09-21 12:40:04 +000055 host->base = TEGRA2_SDMMC1_BASE;
56 host->mmc_id = PERIPH_ID_SDMMC1;
Tom Warren21ef6a12011-05-31 10:30:37 +000057 break;
Simon Glass4ed59e72011-09-21 12:40:04 +000058 case 0:
Tom Warren21ef6a12011-05-31 10:30:37 +000059 default:
Simon Glass4ed59e72011-09-21 12:40:04 +000060 host->base = TEGRA2_SDMMC4_BASE;
61 host->mmc_id = PERIPH_ID_SDMMC4;
Tom Warren21ef6a12011-05-31 10:30:37 +000062 break;
63 }
64
Simon Glass4ed59e72011-09-21 12:40:04 +000065 host->reg = (struct tegra2_mmc *)host->base;
Tom Warren21ef6a12011-05-31 10:30:37 +000066}
67
68static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
69{
70 unsigned char ctrl;
71
72 debug("data->dest: %08X, data->blocks: %u, data->blocksize: %u\n",
73 (u32)data->dest, data->blocks, data->blocksize);
74
75 writel((u32)data->dest, &host->reg->sysad);
76 /*
77 * DMASEL[4:3]
78 * 00 = Selects SDMA
79 * 01 = Reserved
80 * 10 = Selects 32-bit Address ADMA2
81 * 11 = Selects 64-bit Address ADMA2
82 */
83 ctrl = readb(&host->reg->hostctl);
84 ctrl &= ~(3 << 3); /* SDMA */
85 writeb(ctrl, &host->reg->hostctl);
86
87 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
88 writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
89 writew(data->blocks, &host->reg->blkcnt);
90}
91
92static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
93{
94 unsigned short mode;
95 debug(" mmc_set_transfer_mode called\n");
96 /*
97 * TRNMOD
98 * MUL1SIN0[5] : Multi/Single Block Select
99 * RD1WT0[4] : Data Transfer Direction Select
100 * 1 = read
101 * 0 = write
102 * ENACMD12[2] : Auto CMD12 Enable
103 * ENBLKCNT[1] : Block Count Enable
104 * ENDMA[0] : DMA Enable
105 */
106 mode = (1 << 1) | (1 << 0);
107 if (data->blocks > 1)
108 mode |= (1 << 5);
109 if (data->flags & MMC_DATA_READ)
110 mode |= (1 << 4);
111
112 writew(mode, &host->reg->trnmod);
113}
114
115static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
116 struct mmc_data *data)
117{
118 struct mmc_host *host = (struct mmc_host *)mmc->priv;
119 int flags, i;
120 unsigned int timeout;
121 unsigned int mask;
122 unsigned int retry = 0x100000;
123 debug(" mmc_send_cmd called\n");
124
125 /* Wait max 10 ms */
126 timeout = 10;
127
128 /*
129 * PRNSTS
130 * CMDINHDAT[1] : Command Inhibit (DAT)
131 * CMDINHCMD[0] : Command Inhibit (CMD)
132 */
133 mask = (1 << 0);
134 if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
135 mask |= (1 << 1);
136
137 /*
138 * We shouldn't wait for data inhibit for stop commands, even
139 * though they might use busy signaling
140 */
141 if (data)
142 mask &= ~(1 << 1);
143
144 while (readl(&host->reg->prnsts) & mask) {
145 if (timeout == 0) {
146 printf("%s: timeout error\n", __func__);
147 return -1;
148 }
149 timeout--;
150 udelay(1000);
151 }
152
153 if (data)
154 mmc_prepare_data(host, data);
155
156 debug("cmd->arg: %08x\n", cmd->cmdarg);
157 writel(cmd->cmdarg, &host->reg->argument);
158
159 if (data)
160 mmc_set_transfer_mode(host, data);
161
162 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
163 return -1;
164
165 /*
166 * CMDREG
167 * CMDIDX[13:8] : Command index
168 * DATAPRNT[5] : Data Present Select
169 * ENCMDIDX[4] : Command Index Check Enable
170 * ENCMDCRC[3] : Command CRC Check Enable
171 * RSPTYP[1:0]
172 * 00 = No Response
173 * 01 = Length 136
174 * 10 = Length 48
175 * 11 = Length 48 Check busy after response
176 */
177 if (!(cmd->resp_type & MMC_RSP_PRESENT))
178 flags = 0;
179 else if (cmd->resp_type & MMC_RSP_136)
180 flags = (1 << 0);
181 else if (cmd->resp_type & MMC_RSP_BUSY)
182 flags = (3 << 0);
183 else
184 flags = (2 << 0);
185
186 if (cmd->resp_type & MMC_RSP_CRC)
187 flags |= (1 << 3);
188 if (cmd->resp_type & MMC_RSP_OPCODE)
189 flags |= (1 << 4);
190 if (data)
191 flags |= (1 << 5);
192
193 debug("cmd: %d\n", cmd->cmdidx);
194
195 writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
196
197 for (i = 0; i < retry; i++) {
198 mask = readl(&host->reg->norintsts);
199 /* Command Complete */
200 if (mask & (1 << 0)) {
201 if (!data)
202 writel(mask, &host->reg->norintsts);
203 break;
204 }
205 }
206
207 if (i == retry) {
208 printf("%s: waiting for status update\n", __func__);
209 return TIMEOUT;
210 }
211
212 if (mask & (1 << 16)) {
213 /* Timeout Error */
214 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
215 return TIMEOUT;
216 } else if (mask & (1 << 15)) {
217 /* Error Interrupt */
218 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
219 return -1;
220 }
221
222 if (cmd->resp_type & MMC_RSP_PRESENT) {
223 if (cmd->resp_type & MMC_RSP_136) {
224 /* CRC is stripped so we need to do some shifting. */
225 for (i = 0; i < 4; i++) {
226 unsigned int offset =
227 (unsigned int)(&host->reg->rspreg3 - i);
228 cmd->response[i] = readl(offset) << 8;
229
230 if (i != 3) {
231 cmd->response[i] |=
232 readb(offset - 1);
233 }
234 debug("cmd->resp[%d]: %08x\n",
235 i, cmd->response[i]);
236 }
237 } else if (cmd->resp_type & MMC_RSP_BUSY) {
238 for (i = 0; i < retry; i++) {
239 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
240 if (readl(&host->reg->prnsts)
241 & (1 << 20)) /* DAT[0] */
242 break;
243 }
244
245 if (i == retry) {
246 printf("%s: card is still busy\n", __func__);
247 return TIMEOUT;
248 }
249
250 cmd->response[0] = readl(&host->reg->rspreg0);
251 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
252 } else {
253 cmd->response[0] = readl(&host->reg->rspreg0);
254 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
255 }
256 }
257
258 if (data) {
259 while (1) {
260 mask = readl(&host->reg->norintsts);
261
262 if (mask & (1 << 15)) {
263 /* Error Interrupt */
264 writel(mask, &host->reg->norintsts);
265 printf("%s: error during transfer: 0x%08x\n",
266 __func__, mask);
267 return -1;
268 } else if (mask & (1 << 3)) {
269 /* DMA Interrupt */
270 debug("DMA end\n");
271 break;
272 } else if (mask & (1 << 1)) {
273 /* Transfer Complete */
274 debug("r/w is done\n");
275 break;
276 }
277 }
278 writel(mask, &host->reg->norintsts);
279 }
280
281 udelay(1000);
282 return 0;
283}
284
285static void mmc_change_clock(struct mmc_host *host, uint clock)
286{
Simon Glass4ed59e72011-09-21 12:40:04 +0000287 int div;
Tom Warren21ef6a12011-05-31 10:30:37 +0000288 unsigned short clk;
289 unsigned long timeout;
Simon Glass4ed59e72011-09-21 12:40:04 +0000290
Tom Warren21ef6a12011-05-31 10:30:37 +0000291 debug(" mmc_change_clock called\n");
292
Simon Glass4ed59e72011-09-21 12:40:04 +0000293 /*
294 * Change Tegra2 SDMMCx clock divisor here. Source is 216MHz,
295 * PLLP_OUT0
296 */
Tom Warren21ef6a12011-05-31 10:30:37 +0000297 if (clock == 0)
298 goto out;
Simon Glass4ed59e72011-09-21 12:40:04 +0000299 clock_adjust_periph_pll_div(host->mmc_id, CLOCK_ID_PERIPH, clock,
300 &div);
301 debug("div = %d\n", div);
Tom Warren21ef6a12011-05-31 10:30:37 +0000302
303 writew(0, &host->reg->clkcon);
304
Tom Warren21ef6a12011-05-31 10:30:37 +0000305 /*
306 * CLKCON
307 * SELFREQ[15:8] : base clock divided by value
308 * ENSDCLK[2] : SD Clock Enable
309 * STBLINTCLK[1] : Internal Clock Stable
310 * ENINTCLK[0] : Internal Clock Enable
311 */
Simon Glass4ed59e72011-09-21 12:40:04 +0000312 div >>= 1;
Tom Warren21ef6a12011-05-31 10:30:37 +0000313 clk = (div << 8) | (1 << 0);
314 writew(clk, &host->reg->clkcon);
315
316 /* Wait max 10 ms */
317 timeout = 10;
318 while (!(readw(&host->reg->clkcon) & (1 << 1))) {
319 if (timeout == 0) {
320 printf("%s: timeout error\n", __func__);
321 return;
322 }
323 timeout--;
324 udelay(1000);
325 }
326
327 clk |= (1 << 2);
328 writew(clk, &host->reg->clkcon);
329
330 debug("mmc_change_clock: clkcon = %08X\n", clk);
Tom Warren21ef6a12011-05-31 10:30:37 +0000331
332out:
333 host->clock = clock;
334}
335
336static void mmc_set_ios(struct mmc *mmc)
337{
338 struct mmc_host *host = mmc->priv;
339 unsigned char ctrl;
340 debug(" mmc_set_ios called\n");
341
342 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
343
344 /* Change clock first */
Tom Warren21ef6a12011-05-31 10:30:37 +0000345 mmc_change_clock(host, mmc->clock);
346
347 ctrl = readb(&host->reg->hostctl);
348
349 /*
350 * WIDE8[5]
351 * 0 = Depend on WIDE4
352 * 1 = 8-bit mode
353 * WIDE4[1]
354 * 1 = 4-bit mode
355 * 0 = 1-bit mode
356 */
357 if (mmc->bus_width == 8)
358 ctrl |= (1 << 5);
359 else if (mmc->bus_width == 4)
360 ctrl |= (1 << 1);
361 else
362 ctrl &= ~(1 << 1);
363
364 writeb(ctrl, &host->reg->hostctl);
365 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
366}
367
368static void mmc_reset(struct mmc_host *host)
369{
370 unsigned int timeout;
371 debug(" mmc_reset called\n");
372
373 /*
374 * RSTALL[0] : Software reset for all
375 * 1 = reset
376 * 0 = work
377 */
378 writeb((1 << 0), &host->reg->swrst);
379
380 host->clock = 0;
381
382 /* Wait max 100 ms */
383 timeout = 100;
384
385 /* hw clears the bit when it's done */
386 while (readb(&host->reg->swrst) & (1 << 0)) {
387 if (timeout == 0) {
388 printf("%s: timeout error\n", __func__);
389 return;
390 }
391 timeout--;
392 udelay(1000);
393 }
394}
395
396static int mmc_core_init(struct mmc *mmc)
397{
398 struct mmc_host *host = (struct mmc_host *)mmc->priv;
399 unsigned int mask;
400 debug(" mmc_core_init called\n");
401
402 mmc_reset(host);
403
404 host->version = readw(&host->reg->hcver);
405 debug("host version = %x\n", host->version);
406
407 /* mask all */
408 writel(0xffffffff, &host->reg->norintstsen);
409 writel(0xffffffff, &host->reg->norintsigen);
410
411 writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
412 /*
413 * NORMAL Interrupt Status Enable Register init
414 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
415 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
416 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
417 * [0] ENSTACMDCMPLT : Command Complete Status Enable
418 */
419 mask = readl(&host->reg->norintstsen);
420 mask &= ~(0xffff);
421 mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
422 writel(mask, &host->reg->norintstsen);
423
424 /*
425 * NORMAL Interrupt Signal Enable Register init
426 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
427 */
428 mask = readl(&host->reg->norintsigen);
429 mask &= ~(0xffff);
430 mask |= (1 << 1);
431 writel(mask, &host->reg->norintsigen);
432
433 return 0;
434}
435
436static int tegra2_mmc_initialize(int dev_index, int bus_width)
437{
Stephen Warrende71fbe2011-10-31 06:51:34 +0000438 struct mmc_host *host;
Tom Warren21ef6a12011-05-31 10:30:37 +0000439 struct mmc *mmc;
440
441 debug(" mmc_initialize called\n");
442
Stephen Warrende71fbe2011-10-31 06:51:34 +0000443 host = &mmc_host[dev_index];
444
445 host->clock = 0;
446 tegra2_get_setup(host, dev_index);
447
448 clock_start_periph_pll(host->mmc_id, CLOCK_ID_PERIPH, 20000000);
449
Tom Warren21ef6a12011-05-31 10:30:37 +0000450 mmc = &mmc_dev[dev_index];
451
452 sprintf(mmc->name, "Tegra2 SD/MMC");
Stephen Warrende71fbe2011-10-31 06:51:34 +0000453 mmc->priv = host;
Tom Warren21ef6a12011-05-31 10:30:37 +0000454 mmc->send_cmd = mmc_send_cmd;
455 mmc->set_ios = mmc_set_ios;
456 mmc->init = mmc_core_init;
457
458 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
459 if (bus_width == 8)
460 mmc->host_caps = MMC_MODE_8BIT;
461 else
462 mmc->host_caps = MMC_MODE_4BIT;
Tom Warrenccf79882011-09-21 12:40:07 +0000463 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS | MMC_MODE_HC;
Tom Warren21ef6a12011-05-31 10:30:37 +0000464
465 /*
466 * min freq is for card identification, and is the highest
467 * low-speed SDIO card frequency (actually 400KHz)
468 * max freq is highest HS eMMC clock as per the SD/MMC spec
469 * (actually 52MHz)
470 * Both of these are the closest equivalents w/216MHz source
471 * clock and Tegra2 SDMMC divisors.
472 */
473 mmc->f_min = 375000;
474 mmc->f_max = 48000000;
475
Tom Warren21ef6a12011-05-31 10:30:37 +0000476 mmc_register(mmc);
477
478 return 0;
479}
480
481int tegra2_mmc_init(int dev_index, int bus_width)
482{
483 debug(" tegra2_mmc_init: index %d, bus width %d\n",
484 dev_index, bus_width);
485 return tegra2_mmc_initialize(dev_index, bus_width);
486}