blob: 280738fbf4eb191e2fe4ae29239c053214bda451 [file] [log] [blame]
Minkyu Kang50002842010-07-06 20:26:06 +09001/*
2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Jaehoon Chung <jh80.chung@samsung.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21#include <common.h>
22#include <mmc.h>
23#include <asm/io.h>
24#include <asm/arch/mmc.h>
Jaehoon Chung68a8cbf2011-05-17 21:19:17 +000025#include <asm/arch/clk.h>
Minkyu Kang50002842010-07-06 20:26:06 +090026
Minkyu Kang50002842010-07-06 20:26:06 +090027/* support 4 mmc hosts */
28struct mmc mmc_dev[4];
29struct mmc_host mmc_host[4];
30
31static inline struct s5p_mmc *s5p_get_base_mmc(int dev_index)
32{
33 unsigned long offset = dev_index * sizeof(struct s5p_mmc);
Minkyu Kangd93d0f02010-08-13 16:07:35 +090034 return (struct s5p_mmc *)(samsung_get_base_mmc() + offset);
Minkyu Kang50002842010-07-06 20:26:06 +090035}
36
37static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
38{
39 unsigned char ctrl;
40
Minkyu Kang6a69e112010-08-05 09:55:14 +090041 debug("data->dest: %08x\n", (u32)data->dest);
Minkyu Kang50002842010-07-06 20:26:06 +090042 writel((u32)data->dest, &host->reg->sysad);
43 /*
44 * DMASEL[4:3]
45 * 00 = Selects SDMA
46 * 01 = Reserved
47 * 10 = Selects 32-bit Address ADMA2
48 * 11 = Selects 64-bit Address ADMA2
49 */
50 ctrl = readb(&host->reg->hostctl);
51 ctrl &= ~(3 << 3);
52 writeb(ctrl, &host->reg->hostctl);
53
54 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
Chander Kashyapf69bb512011-03-22 01:40:50 +000055 writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
Minkyu Kang50002842010-07-06 20:26:06 +090056 writew(data->blocks, &host->reg->blkcnt);
57}
58
59static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
60{
61 unsigned short mode;
62
63 /*
64 * TRNMOD
65 * MUL1SIN0[5] : Multi/Single Block Select
66 * RD1WT0[4] : Data Transfer Direction Select
67 * 1 = read
68 * 0 = write
69 * ENACMD12[2] : Auto CMD12 Enable
70 * ENBLKCNT[1] : Block Count Enable
71 * ENDMA[0] : DMA Enable
72 */
73 mode = (1 << 1) | (1 << 0);
74 if (data->blocks > 1)
75 mode |= (1 << 5);
76 if (data->flags & MMC_DATA_READ)
77 mode |= (1 << 4);
78
79 writew(mode, &host->reg->trnmod);
80}
81
82static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
83 struct mmc_data *data)
84{
85 struct mmc_host *host = (struct mmc_host *)mmc->priv;
86 int flags, i;
87 unsigned int timeout;
88 unsigned int mask;
89 unsigned int retry = 0x100000;
90
91 /* Wait max 10 ms */
92 timeout = 10;
93
94 /*
95 * PRNSTS
96 * CMDINHDAT[1] : Command Inhibit (DAT)
97 * CMDINHCMD[0] : Command Inhibit (CMD)
98 */
99 mask = (1 << 0);
100 if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
101 mask |= (1 << 1);
102
103 /*
104 * We shouldn't wait for data inihibit for stop commands, even
105 * though they might use busy signaling
106 */
107 if (data)
108 mask &= ~(1 << 1);
109
110 while (readl(&host->reg->prnsts) & mask) {
111 if (timeout == 0) {
112 printf("%s: timeout error\n", __func__);
113 return -1;
114 }
115 timeout--;
116 udelay(1000);
117 }
118
119 if (data)
120 mmc_prepare_data(host, data);
121
Minkyu Kang6a69e112010-08-05 09:55:14 +0900122 debug("cmd->arg: %08x\n", cmd->cmdarg);
Minkyu Kang50002842010-07-06 20:26:06 +0900123 writel(cmd->cmdarg, &host->reg->argument);
124
125 if (data)
126 mmc_set_transfer_mode(host, data);
127
128 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
129 return -1;
130
131 /*
132 * CMDREG
133 * CMDIDX[13:8] : Command index
134 * DATAPRNT[5] : Data Present Select
135 * ENCMDIDX[4] : Command Index Check Enable
136 * ENCMDCRC[3] : Command CRC Check Enable
137 * RSPTYP[1:0]
138 * 00 = No Response
139 * 01 = Length 136
140 * 10 = Length 48
141 * 11 = Length 48 Check busy after response
142 */
143 if (!(cmd->resp_type & MMC_RSP_PRESENT))
144 flags = 0;
145 else if (cmd->resp_type & MMC_RSP_136)
146 flags = (1 << 0);
147 else if (cmd->resp_type & MMC_RSP_BUSY)
148 flags = (3 << 0);
149 else
150 flags = (2 << 0);
151
152 if (cmd->resp_type & MMC_RSP_CRC)
153 flags |= (1 << 3);
154 if (cmd->resp_type & MMC_RSP_OPCODE)
155 flags |= (1 << 4);
156 if (data)
157 flags |= (1 << 5);
158
Minkyu Kang6a69e112010-08-05 09:55:14 +0900159 debug("cmd: %d\n", cmd->cmdidx);
Minkyu Kang50002842010-07-06 20:26:06 +0900160
161 writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
162
163 for (i = 0; i < retry; i++) {
164 mask = readl(&host->reg->norintsts);
165 /* Command Complete */
166 if (mask & (1 << 0)) {
167 if (!data)
168 writel(mask, &host->reg->norintsts);
169 break;
170 }
171 }
172
173 if (i == retry) {
174 printf("%s: waiting for status update\n", __func__);
175 return TIMEOUT;
176 }
177
178 if (mask & (1 << 16)) {
179 /* Timeout Error */
Minkyu Kang6a69e112010-08-05 09:55:14 +0900180 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
Minkyu Kang50002842010-07-06 20:26:06 +0900181 return TIMEOUT;
182 } else if (mask & (1 << 15)) {
183 /* Error Interrupt */
Minkyu Kang6a69e112010-08-05 09:55:14 +0900184 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
Minkyu Kang50002842010-07-06 20:26:06 +0900185 return -1;
186 }
187
188 if (cmd->resp_type & MMC_RSP_PRESENT) {
189 if (cmd->resp_type & MMC_RSP_136) {
190 /* CRC is stripped so we need to do some shifting. */
191 for (i = 0; i < 4; i++) {
192 unsigned int offset =
193 (unsigned int)(&host->reg->rspreg3 - i);
194 cmd->response[i] = readl(offset) << 8;
195
196 if (i != 3) {
197 cmd->response[i] |=
198 readb(offset - 1);
199 }
Minkyu Kang6a69e112010-08-05 09:55:14 +0900200 debug("cmd->resp[%d]: %08x\n",
Minkyu Kang50002842010-07-06 20:26:06 +0900201 i, cmd->response[i]);
202 }
203 } else if (cmd->resp_type & MMC_RSP_BUSY) {
204 for (i = 0; i < retry; i++) {
205 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
206 if (readl(&host->reg->prnsts)
207 & (1 << 20)) /* DAT[0] */
208 break;
209 }
210
211 if (i == retry) {
212 printf("%s: card is still busy\n", __func__);
213 return TIMEOUT;
214 }
215
216 cmd->response[0] = readl(&host->reg->rspreg0);
Minkyu Kang6a69e112010-08-05 09:55:14 +0900217 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
Minkyu Kang50002842010-07-06 20:26:06 +0900218 } else {
219 cmd->response[0] = readl(&host->reg->rspreg0);
Minkyu Kang6a69e112010-08-05 09:55:14 +0900220 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
Minkyu Kang50002842010-07-06 20:26:06 +0900221 }
222 }
223
224 if (data) {
225 while (1) {
226 mask = readl(&host->reg->norintsts);
227
228 if (mask & (1 << 15)) {
229 /* Error Interrupt */
230 writel(mask, &host->reg->norintsts);
231 printf("%s: error during transfer: 0x%08x\n",
232 __func__, mask);
233 return -1;
234 } else if (mask & (1 << 3)) {
235 /* DMA Interrupt */
Minkyu Kang6a69e112010-08-05 09:55:14 +0900236 debug("DMA end\n");
Minkyu Kang50002842010-07-06 20:26:06 +0900237 break;
238 } else if (mask & (1 << 1)) {
239 /* Transfer Complete */
Minkyu Kang6a69e112010-08-05 09:55:14 +0900240 debug("r/w is done\n");
Minkyu Kang50002842010-07-06 20:26:06 +0900241 break;
242 }
243 }
244 writel(mask, &host->reg->norintsts);
245 }
246
247 udelay(1000);
248 return 0;
249}
250
251static void mmc_change_clock(struct mmc_host *host, uint clock)
252{
253 int div;
254 unsigned short clk;
255 unsigned long timeout;
256 unsigned long ctrl2;
257
258 /*
259 * SELBASECLK[5:4]
260 * 00/01 = HCLK
261 * 10 = EPLL
262 * 11 = XTI or XEXTCLK
263 */
264 ctrl2 = readl(&host->reg->control2);
265 ctrl2 &= ~(3 << 4);
266 ctrl2 |= (2 << 4);
267 writel(ctrl2, &host->reg->control2);
268
269 writew(0, &host->reg->clkcon);
270
271 /* XXX: we assume that clock is between 40MHz and 50MHz */
272 if (clock == 0)
273 goto out;
274 else if (clock <= 400000)
275 div = 0x100;
276 else if (clock <= 20000000)
277 div = 4;
278 else if (clock <= 26000000)
279 div = 2;
280 else
281 div = 1;
Minkyu Kang6a69e112010-08-05 09:55:14 +0900282 debug("div: %d\n", div);
Minkyu Kang50002842010-07-06 20:26:06 +0900283
284 div >>= 1;
285 /*
286 * CLKCON
287 * SELFREQ[15:8] : base clock divied by value
288 * ENSDCLK[2] : SD Clock Enable
289 * STBLINTCLK[1] : Internal Clock Stable
290 * ENINTCLK[0] : Internal Clock Enable
291 */
292 clk = (div << 8) | (1 << 0);
293 writew(clk, &host->reg->clkcon);
294
Jaehoon Chung68a8cbf2011-05-17 21:19:17 +0000295 set_mmc_clk(host->dev_index, div);
296
Minkyu Kang50002842010-07-06 20:26:06 +0900297 /* Wait max 10 ms */
298 timeout = 10;
299 while (!(readw(&host->reg->clkcon) & (1 << 1))) {
300 if (timeout == 0) {
301 printf("%s: timeout error\n", __func__);
302 return;
303 }
304 timeout--;
305 udelay(1000);
306 }
307
308 clk |= (1 << 2);
309 writew(clk, &host->reg->clkcon);
310
311out:
312 host->clock = clock;
313}
314
315static void mmc_set_ios(struct mmc *mmc)
316{
317 struct mmc_host *host = mmc->priv;
318 unsigned char ctrl;
319 unsigned long val;
320
Minkyu Kang6a69e112010-08-05 09:55:14 +0900321 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
Minkyu Kang50002842010-07-06 20:26:06 +0900322
323 /*
324 * SELCLKPADDS[17:16]
325 * 00 = 2mA
326 * 01 = 4mA
327 * 10 = 7mA
328 * 11 = 9mA
329 */
330 writel(0x3 << 16, &host->reg->control4);
331
332 val = readl(&host->reg->control2);
333 val &= (0x3 << 4);
334
335 val |= (1 << 31) | /* write status clear async mode enable */
336 (1 << 30) | /* command conflict mask enable */
337 (1 << 14) | /* Feedback Clock Enable for Rx Clock */
338 (1 << 8); /* SDCLK hold enable */
339
340 writel(val, &host->reg->control2);
341
342 /*
343 * FCSEL1[15] FCSEL0[7]
344 * FCSel[1:0] : Rx Feedback Clock Delay Control
345 * Inverter delay means10ns delay if SDCLK 50MHz setting
346 * 01 = Delay1 (basic delay)
347 * 11 = Delay2 (basic delay + 2ns)
348 * 00 = Delay3 (inverter delay)
349 * 10 = Delay4 (inverter delay + 2ns)
350 */
351 writel(0x8080, &host->reg->control3);
352
353 mmc_change_clock(host, mmc->clock);
354
355 ctrl = readb(&host->reg->hostctl);
356
357 /*
Jaehoon Chung1727e212010-09-27 15:43:52 +0900358 * WIDE8[5]
359 * 0 = Depend on WIDE4
360 * 1 = 8-bit mode
Minkyu Kang50002842010-07-06 20:26:06 +0900361 * WIDE4[1]
362 * 1 = 4-bit mode
363 * 0 = 1-bit mode
364 */
Jaehoon Chung1727e212010-09-27 15:43:52 +0900365 if (mmc->bus_width == 8)
366 ctrl |= (1 << 5);
367 else if (mmc->bus_width == 4)
Minkyu Kang50002842010-07-06 20:26:06 +0900368 ctrl |= (1 << 1);
369 else
370 ctrl &= ~(1 << 1);
371
372 /*
373 * OUTEDGEINV[2]
374 * 1 = Riging edge output
375 * 0 = Falling edge output
376 */
377 ctrl &= ~(1 << 2);
378
379 writeb(ctrl, &host->reg->hostctl);
380}
381
382static void mmc_reset(struct mmc_host *host)
383{
384 unsigned int timeout;
385
386 /*
387 * RSTALL[0] : Software reset for all
388 * 1 = reset
389 * 0 = work
390 */
391 writeb((1 << 0), &host->reg->swrst);
392
393 host->clock = 0;
394
395 /* Wait max 100 ms */
396 timeout = 100;
397
398 /* hw clears the bit when it's done */
399 while (readb(&host->reg->swrst) & (1 << 0)) {
400 if (timeout == 0) {
401 printf("%s: timeout error\n", __func__);
402 return;
403 }
404 timeout--;
405 udelay(1000);
406 }
407}
408
409static int mmc_core_init(struct mmc *mmc)
410{
411 struct mmc_host *host = (struct mmc_host *)mmc->priv;
412 unsigned int mask;
413
414 mmc_reset(host);
415
416 host->version = readw(&host->reg->hcver);
417
418 /* mask all */
419 writel(0xffffffff, &host->reg->norintstsen);
420 writel(0xffffffff, &host->reg->norintsigen);
421
422 writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
423
424 /*
425 * NORMAL Interrupt Status Enable Register init
426 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
427 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
428 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
429 * [0] ENSTACMDCMPLT : Command Complete Status Enable
430 */
431 mask = readl(&host->reg->norintstsen);
432 mask &= ~(0xffff);
433 mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
434 writel(mask, &host->reg->norintstsen);
435
436 /*
437 * NORMAL Interrupt Signal Enable Register init
438 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
439 */
440 mask = readl(&host->reg->norintsigen);
441 mask &= ~(0xffff);
442 mask |= (1 << 1);
443 writel(mask, &host->reg->norintsigen);
444
445 return 0;
446}
447
Jaehoon Chung1727e212010-09-27 15:43:52 +0900448static int s5p_mmc_initialize(int dev_index, int bus_width)
Minkyu Kang50002842010-07-06 20:26:06 +0900449{
450 struct mmc *mmc;
451
452 mmc = &mmc_dev[dev_index];
453
454 sprintf(mmc->name, "SAMSUNG SD/MMC");
455 mmc->priv = &mmc_host[dev_index];
456 mmc->send_cmd = mmc_send_cmd;
457 mmc->set_ios = mmc_set_ios;
458 mmc->init = mmc_core_init;
459
460 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung1727e212010-09-27 15:43:52 +0900461 if (bus_width == 8)
462 mmc->host_caps = MMC_MODE_8BIT;
463 else
464 mmc->host_caps = MMC_MODE_4BIT;
465 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
Minkyu Kang50002842010-07-06 20:26:06 +0900466
467 mmc->f_min = 400000;
468 mmc->f_max = 52000000;
469
Jaehoon Chung68a8cbf2011-05-17 21:19:17 +0000470 mmc_host[dev_index].dev_index = dev_index;
Minkyu Kang50002842010-07-06 20:26:06 +0900471 mmc_host[dev_index].clock = 0;
472 mmc_host[dev_index].reg = s5p_get_base_mmc(dev_index);
Dirk Behme810423f2011-05-15 05:39:28 +0000473 mmc->b_max = 0;
Minkyu Kang50002842010-07-06 20:26:06 +0900474 mmc_register(mmc);
475
476 return 0;
477}
478
Jaehoon Chung1727e212010-09-27 15:43:52 +0900479int s5p_mmc_init(int dev_index, int bus_width)
Minkyu Kang50002842010-07-06 20:26:06 +0900480{
Jaehoon Chung1727e212010-09-27 15:43:52 +0900481 return s5p_mmc_initialize(dev_index, bus_width);
Minkyu Kang50002842010-07-06 20:26:06 +0900482}