1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22#include <common.h>
23#include <mmc.h>
24#include <asm/io.h>
25#include <asm/arch/clk_rst.h>
26#include "tegra2_mmc.h"
27
28
29struct mmc mmc_dev[4];
30struct mmc_host mmc_host[4];
31
32static inline struct tegra2_mmc *tegra2_get_base_mmc(int dev_index)
33{
34 unsigned long offset;
35 debug("tegra2_get_base_mmc: dev_index = %d\n", dev_index);
36
37 switch (dev_index) {
38 case 0:
39 offset = TEGRA2_SDMMC4_BASE;
40 break;
41 case 1:
42 offset = TEGRA2_SDMMC3_BASE;
43 break;
44 case 2:
45 offset = TEGRA2_SDMMC2_BASE;
46 break;
47 case 3:
48 offset = TEGRA2_SDMMC1_BASE;
49 break;
50 default:
51 offset = TEGRA2_SDMMC4_BASE;
52 break;
53 }
54
55 return (struct tegra2_mmc *)(offset);
56}
57
58static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
59{
60 unsigned char ctrl;
61
62 debug("data->dest: %08X, data->blocks: %u, data->blocksize: %u\n",
63 (u32)data->dest, data->blocks, data->blocksize);
64
65 writel((u32)data->dest, &host->reg->sysad);
66
67
68
69
70
71
72
73 ctrl = readb(&host->reg->hostctl);
74 ctrl &= ~(3 << 3);
75 writeb(ctrl, &host->reg->hostctl);
76
77
78 writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
79 writew(data->blocks, &host->reg->blkcnt);
80}
81
82static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
83{
84 unsigned short mode;
85 debug(" mmc_set_transfer_mode called\n");
86
87
88
89
90
91
92
93
94
95
96 mode = (1 << 1) | (1 << 0);
97 if (data->blocks > 1)
98 mode |= (1 << 5);
99 if (data->flags & MMC_DATA_READ)
100 mode |= (1 << 4);
101
102 writew(mode, &host->reg->trnmod);
103}
104
105static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
106 struct mmc_data *data)
107{
108 struct mmc_host *host = (struct mmc_host *)mmc->priv;
109 int flags, i;
110 unsigned int timeout;
111 unsigned int mask;
112 unsigned int retry = 0x100000;
113 debug(" mmc_send_cmd called\n");
114
115
116 timeout = 10;
117
118
119
120
121
122
123 mask = (1 << 0);
124 if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
125 mask |= (1 << 1);
126
127
128
129
130
131 if (data)
132 mask &= ~(1 << 1);
133
134 while (readl(&host->reg->prnsts) & mask) {
135 if (timeout == 0) {
136 printf("%s: timeout error\n", __func__);
137 return -1;
138 }
139 timeout--;
140 udelay(1000);
141 }
142
143 if (data)
144 mmc_prepare_data(host, data);
145
146 debug("cmd->arg: %08x\n", cmd->cmdarg);
147 writel(cmd->cmdarg, &host->reg->argument);
148
149 if (data)
150 mmc_set_transfer_mode(host, data);
151
152 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
153 return -1;
154
155
156
157
158
159
160
161
162
163
164
165
166
167 if (!(cmd->resp_type & MMC_RSP_PRESENT))
168 flags = 0;
169 else if (cmd->resp_type & MMC_RSP_136)
170 flags = (1 << 0);
171 else if (cmd->resp_type & MMC_RSP_BUSY)
172 flags = (3 << 0);
173 else
174 flags = (2 << 0);
175
176 if (cmd->resp_type & MMC_RSP_CRC)
177 flags |= (1 << 3);
178 if (cmd->resp_type & MMC_RSP_OPCODE)
179 flags |= (1 << 4);
180 if (data)
181 flags |= (1 << 5);
182
183 debug("cmd: %d\n", cmd->cmdidx);
184
185 writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
186
187 for (i = 0; i < retry; i++) {
188 mask = readl(&host->reg->norintsts);
189
190 if (mask & (1 << 0)) {
191 if (!data)
192 writel(mask, &host->reg->norintsts);
193 break;
194 }
195 }
196
197 if (i == retry) {
198 printf("%s: waiting for status update\n", __func__);
199 return TIMEOUT;
200 }
201
202 if (mask & (1 << 16)) {
203
204 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
205 return TIMEOUT;
206 } else if (mask & (1 << 15)) {
207
208 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
209 return -1;
210 }
211
212 if (cmd->resp_type & MMC_RSP_PRESENT) {
213 if (cmd->resp_type & MMC_RSP_136) {
214
215 for (i = 0; i < 4; i++) {
216 unsigned int offset =
217 (unsigned int)(&host->reg->rspreg3 - i);
218 cmd->response[i] = readl(offset) << 8;
219
220 if (i != 3) {
221 cmd->response[i] |=
222 readb(offset - 1);
223 }
224 debug("cmd->resp[%d]: %08x\n",
225 i, cmd->response[i]);
226 }
227 } else if (cmd->resp_type & MMC_RSP_BUSY) {
228 for (i = 0; i < retry; i++) {
229
230 if (readl(&host->reg->prnsts)
231 & (1 << 20))
232 break;
233 }
234
235 if (i == retry) {
236 printf("%s: card is still busy\n", __func__);
237 return TIMEOUT;
238 }
239
240 cmd->response[0] = readl(&host->reg->rspreg0);
241 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
242 } else {
243 cmd->response[0] = readl(&host->reg->rspreg0);
244 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
245 }
246 }
247
248 if (data) {
249 while (1) {
250 mask = readl(&host->reg->norintsts);
251
252 if (mask & (1 << 15)) {
253
254 writel(mask, &host->reg->norintsts);
255 printf("%s: error during transfer: 0x%08x\n",
256 __func__, mask);
257 return -1;
258 } else if (mask & (1 << 3)) {
259
260 debug("DMA end\n");
261 break;
262 } else if (mask & (1 << 1)) {
263
264 debug("r/w is done\n");
265 break;
266 }
267 }
268 writel(mask, &host->reg->norintsts);
269 }
270
271 udelay(1000);
272 return 0;
273}
274
275static void mmc_change_clock(struct mmc_host *host, uint clock)
276{
277 int div, hw_div;
278 unsigned short clk;
279 unsigned long timeout;
280 unsigned int reg, hostbase;
281 struct clk_rst_ctlr *clkrst = (struct clk_rst_ctlr *)NV_PA_CLK_RST_BASE;
282 debug(" mmc_change_clock called\n");
283
284
285
286 if (clock == 0)
287 goto out;
288
289 div = 1;
290 if (clock <= 400000) {
291 hw_div = ((9-1)<<1);
292 div = 64;
293 } else if (clock <= 20000000)
294 hw_div = ((11-1)<<1);
295 else if (clock <= 26000000)
296 hw_div = ((9-1)<<1);
297 else
298 hw_div = ((4-1)<<1) + 1;
299
300 debug("mmc_change_clock: hw_div = %d, card clock div = %d\n",
301 hw_div, div);
302
303
304
305 hostbase = readl(&host->base);
306 debug("mmc_change_clock: hostbase = %08X\n", hostbase);
307
308 if (hostbase == TEGRA2_SDMMC1_BASE) {
309 reg = readl(&clkrst->crc_clk_src_sdmmc1);
310 reg &= 0xFFFFFF00;
311 reg |= hw_div;
312 writel(reg, &clkrst->crc_clk_src_sdmmc1);
313 } else if (hostbase == TEGRA2_SDMMC2_BASE) {
314 reg = readl(&clkrst->crc_clk_src_sdmmc2);
315 reg &= 0xFFFFFF00;
316 reg |= hw_div;
317 writel(reg, &clkrst->crc_clk_src_sdmmc2);
318 } else if (hostbase == TEGRA2_SDMMC3_BASE) {
319 reg = readl(&clkrst->crc_clk_src_sdmmc3);
320 reg &= 0xFFFFFF00;
321 reg |= hw_div;
322 writel(reg, &clkrst->crc_clk_src_sdmmc3);
323 } else {
324 reg = readl(&clkrst->crc_clk_src_sdmmc4);
325 reg &= 0xFFFFFF00;
326 reg |= hw_div;
327 writel(reg, &clkrst->crc_clk_src_sdmmc4);
328 }
329
330 writew(0, &host->reg->clkcon);
331
332 div >>= 1;
333
334
335
336
337
338
339
340 clk = (div << 8) | (1 << 0);
341 writew(clk, &host->reg->clkcon);
342
343
344 timeout = 10;
345 while (!(readw(&host->reg->clkcon) & (1 << 1))) {
346 if (timeout == 0) {
347 printf("%s: timeout error\n", __func__);
348 return;
349 }
350 timeout--;
351 udelay(1000);
352 }
353
354 clk |= (1 << 2);
355 writew(clk, &host->reg->clkcon);
356
357 debug("mmc_change_clock: clkcon = %08X\n", clk);
358 debug("mmc_change_clock: CLK_SOURCE_SDMMCx = %08X\n", reg);
359
360out:
361 host->clock = clock;
362}
363
364static void mmc_set_ios(struct mmc *mmc)
365{
366 struct mmc_host *host = mmc->priv;
367 unsigned char ctrl;
368 debug(" mmc_set_ios called\n");
369
370 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
371
372
373
374 mmc_change_clock(host, mmc->clock);
375
376 ctrl = readb(&host->reg->hostctl);
377
378
379
380
381
382
383
384
385
386 if (mmc->bus_width == 8)
387 ctrl |= (1 << 5);
388 else if (mmc->bus_width == 4)
389 ctrl |= (1 << 1);
390 else
391 ctrl &= ~(1 << 1);
392
393 writeb(ctrl, &host->reg->hostctl);
394 debug("mmc_set_ios: hostctl = %08X\n", ctrl);
395}
396
397static void mmc_reset(struct mmc_host *host)
398{
399 unsigned int timeout;
400 debug(" mmc_reset called\n");
401
402
403
404
405
406
407 writeb((1 << 0), &host->reg->swrst);
408
409 host->clock = 0;
410
411
412 timeout = 100;
413
414
415 while (readb(&host->reg->swrst) & (1 << 0)) {
416 if (timeout == 0) {
417 printf("%s: timeout error\n", __func__);
418 return;
419 }
420 timeout--;
421 udelay(1000);
422 }
423}
424
425static int mmc_core_init(struct mmc *mmc)
426{
427 struct mmc_host *host = (struct mmc_host *)mmc->priv;
428 unsigned int mask;
429 debug(" mmc_core_init called\n");
430
431 mmc_reset(host);
432
433 host->version = readw(&host->reg->hcver);
434 debug("host version = %x\n", host->version);
435
436
437 writel(0xffffffff, &host->reg->norintstsen);
438 writel(0xffffffff, &host->reg->norintsigen);
439
440 writeb(0xe, &host->reg->timeoutcon);
441
442
443
444
445
446
447
448 mask = readl(&host->reg->norintstsen);
449 mask &= ~(0xffff);
450 mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
451 writel(mask, &host->reg->norintstsen);
452
453
454
455
456
457 mask = readl(&host->reg->norintsigen);
458 mask &= ~(0xffff);
459 mask |= (1 << 1);
460 writel(mask, &host->reg->norintsigen);
461
462 return 0;
463}
464
465static int tegra2_mmc_initialize(int dev_index, int bus_width)
466{
467 struct mmc *mmc;
468
469 debug(" mmc_initialize called\n");
470
471 mmc = &mmc_dev[dev_index];
472
473 sprintf(mmc->name, "Tegra2 SD/MMC");
474 mmc->priv = &mmc_host[dev_index];
475 mmc->send_cmd = mmc_send_cmd;
476 mmc->set_ios = mmc_set_ios;
477 mmc->init = mmc_core_init;
478
479 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
480 if (bus_width == 8)
481 mmc->host_caps = MMC_MODE_8BIT;
482 else
483 mmc->host_caps = MMC_MODE_4BIT;
484 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
485
486
487
488
489
490
491
492
493
494 mmc->f_min = 375000;
495 mmc->f_max = 48000000;
496
497 mmc_host[dev_index].clock = 0;
498 mmc_host[dev_index].reg = tegra2_get_base_mmc(dev_index);
499 mmc_host[dev_index].base = (unsigned int)mmc_host[dev_index].reg;
500 mmc_register(mmc);
501
502 return 0;
503}
504
505int tegra2_mmc_init(int dev_index, int bus_width)
506{
507 debug(" tegra2_mmc_init: index %d, bus width %d\n",
508 dev_index, bus_width);
509 return tegra2_mmc_initialize(dev_index, bus_width);
510}
511