S5P: mmc: use the standard debug macro
[oweals/u-boot.git] / drivers / mmc / s5p_mmc.c
1 /*
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>
25
26 /* support 4 mmc hosts */
27 struct mmc mmc_dev[4];
28 struct mmc_host mmc_host[4];
29
30 static inline struct s5p_mmc *s5p_get_base_mmc(int dev_index)
31 {
32         unsigned long offset = dev_index * sizeof(struct s5p_mmc);
33
34         if (cpu_is_s5pc100())
35                 return (struct s5p_mmc *)(S5PC100_MMC_BASE + offset);
36         else
37                 return (struct s5p_mmc *)(S5PC110_MMC_BASE + offset);
38 }
39
40 static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
41 {
42         unsigned char ctrl;
43
44         debug("data->dest: %08x\n", (u32)data->dest);
45         writel((u32)data->dest, &host->reg->sysad);
46         /*
47          * DMASEL[4:3]
48          * 00 = Selects SDMA
49          * 01 = Reserved
50          * 10 = Selects 32-bit Address ADMA2
51          * 11 = Selects 64-bit Address ADMA2
52          */
53         ctrl = readb(&host->reg->hostctl);
54         ctrl &= ~(3 << 3);
55         writeb(ctrl, &host->reg->hostctl);
56
57         /* We do not handle DMA boundaries, so set it to max (512 KiB) */
58         writew((7 << 12) | (512 << 0), &host->reg->blksize);
59         writew(data->blocks, &host->reg->blkcnt);
60 }
61
62 static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
63 {
64         unsigned short mode;
65
66         /*
67          * TRNMOD
68          * MUL1SIN0[5]  : Multi/Single Block Select
69          * RD1WT0[4]    : Data Transfer Direction Select
70          *      1 = read
71          *      0 = write
72          * ENACMD12[2]  : Auto CMD12 Enable
73          * ENBLKCNT[1]  : Block Count Enable
74          * ENDMA[0]     : DMA Enable
75          */
76         mode = (1 << 1) | (1 << 0);
77         if (data->blocks > 1)
78                 mode |= (1 << 5);
79         if (data->flags & MMC_DATA_READ)
80                 mode |= (1 << 4);
81
82         writew(mode, &host->reg->trnmod);
83 }
84
85 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
86                         struct mmc_data *data)
87 {
88         struct mmc_host *host = (struct mmc_host *)mmc->priv;
89         int flags, i;
90         unsigned int timeout;
91         unsigned int mask;
92         unsigned int retry = 0x100000;
93
94         /* Wait max 10 ms */
95         timeout = 10;
96
97         /*
98          * PRNSTS
99          * CMDINHDAT[1] : Command Inhibit (DAT)
100          * CMDINHCMD[0] : Command Inhibit (CMD)
101          */
102         mask = (1 << 0);
103         if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
104                 mask |= (1 << 1);
105
106         /*
107          * We shouldn't wait for data inihibit for stop commands, even
108          * though they might use busy signaling
109          */
110         if (data)
111                 mask &= ~(1 << 1);
112
113         while (readl(&host->reg->prnsts) & mask) {
114                 if (timeout == 0) {
115                         printf("%s: timeout error\n", __func__);
116                         return -1;
117                 }
118                 timeout--;
119                 udelay(1000);
120         }
121
122         if (data)
123                 mmc_prepare_data(host, data);
124
125         debug("cmd->arg: %08x\n", cmd->cmdarg);
126         writel(cmd->cmdarg, &host->reg->argument);
127
128         if (data)
129                 mmc_set_transfer_mode(host, data);
130
131         if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
132                 return -1;
133
134         /*
135          * CMDREG
136          * CMDIDX[13:8] : Command index
137          * DATAPRNT[5]  : Data Present Select
138          * ENCMDIDX[4]  : Command Index Check Enable
139          * ENCMDCRC[3]  : Command CRC Check Enable
140          * RSPTYP[1:0]
141          *      00 = No Response
142          *      01 = Length 136
143          *      10 = Length 48
144          *      11 = Length 48 Check busy after response
145          */
146         if (!(cmd->resp_type & MMC_RSP_PRESENT))
147                 flags = 0;
148         else if (cmd->resp_type & MMC_RSP_136)
149                 flags = (1 << 0);
150         else if (cmd->resp_type & MMC_RSP_BUSY)
151                 flags = (3 << 0);
152         else
153                 flags = (2 << 0);
154
155         if (cmd->resp_type & MMC_RSP_CRC)
156                 flags |= (1 << 3);
157         if (cmd->resp_type & MMC_RSP_OPCODE)
158                 flags |= (1 << 4);
159         if (data)
160                 flags |= (1 << 5);
161
162         debug("cmd: %d\n", cmd->cmdidx);
163
164         writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
165
166         for (i = 0; i < retry; i++) {
167                 mask = readl(&host->reg->norintsts);
168                 /* Command Complete */
169                 if (mask & (1 << 0)) {
170                         if (!data)
171                                 writel(mask, &host->reg->norintsts);
172                         break;
173                 }
174         }
175
176         if (i == retry) {
177                 printf("%s: waiting for status update\n", __func__);
178                 return TIMEOUT;
179         }
180
181         if (mask & (1 << 16)) {
182                 /* Timeout Error */
183                 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
184                 return TIMEOUT;
185         } else if (mask & (1 << 15)) {
186                 /* Error Interrupt */
187                 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
188                 return -1;
189         }
190
191         if (cmd->resp_type & MMC_RSP_PRESENT) {
192                 if (cmd->resp_type & MMC_RSP_136) {
193                         /* CRC is stripped so we need to do some shifting. */
194                         for (i = 0; i < 4; i++) {
195                                 unsigned int offset =
196                                         (unsigned int)(&host->reg->rspreg3 - i);
197                                 cmd->response[i] = readl(offset) << 8;
198
199                                 if (i != 3) {
200                                         cmd->response[i] |=
201                                                 readb(offset - 1);
202                                 }
203                                 debug("cmd->resp[%d]: %08x\n",
204                                                 i, cmd->response[i]);
205                         }
206                 } else if (cmd->resp_type & MMC_RSP_BUSY) {
207                         for (i = 0; i < retry; i++) {
208                                 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
209                                 if (readl(&host->reg->prnsts)
210                                         & (1 << 20))    /* DAT[0] */
211                                         break;
212                         }
213
214                         if (i == retry) {
215                                 printf("%s: card is still busy\n", __func__);
216                                 return TIMEOUT;
217                         }
218
219                         cmd->response[0] = readl(&host->reg->rspreg0);
220                         debug("cmd->resp[0]: %08x\n", cmd->response[0]);
221                 } else {
222                         cmd->response[0] = readl(&host->reg->rspreg0);
223                         debug("cmd->resp[0]: %08x\n", cmd->response[0]);
224                 }
225         }
226
227         if (data) {
228                 while (1) {
229                         mask = readl(&host->reg->norintsts);
230
231                         if (mask & (1 << 15)) {
232                                 /* Error Interrupt */
233                                 writel(mask, &host->reg->norintsts);
234                                 printf("%s: error during transfer: 0x%08x\n",
235                                                 __func__, mask);
236                                 return -1;
237                         } else if (mask & (1 << 3)) {
238                                 /* DMA Interrupt */
239                                 debug("DMA end\n");
240                                 break;
241                         } else if (mask & (1 << 1)) {
242                                 /* Transfer Complete */
243                                 debug("r/w is done\n");
244                                 break;
245                         }
246                 }
247                 writel(mask, &host->reg->norintsts);
248         }
249
250         udelay(1000);
251         return 0;
252 }
253
254 static void mmc_change_clock(struct mmc_host *host, uint clock)
255 {
256         int div;
257         unsigned short clk;
258         unsigned long timeout;
259         unsigned long ctrl2;
260
261         /*
262          * SELBASECLK[5:4]
263          * 00/01 = HCLK
264          * 10 = EPLL
265          * 11 = XTI or XEXTCLK
266          */
267         ctrl2 = readl(&host->reg->control2);
268         ctrl2 &= ~(3 << 4);
269         ctrl2 |= (2 << 4);
270         writel(ctrl2, &host->reg->control2);
271
272         writew(0, &host->reg->clkcon);
273
274         /* XXX: we assume that clock is between 40MHz and 50MHz */
275         if (clock == 0)
276                 goto out;
277         else if (clock <= 400000)
278                 div = 0x100;
279         else if (clock <= 20000000)
280                 div = 4;
281         else if (clock <= 26000000)
282                 div = 2;
283         else
284                 div = 1;
285         debug("div: %d\n", div);
286
287         div >>= 1;
288         /*
289          * CLKCON
290          * SELFREQ[15:8]        : base clock divied by value
291          * ENSDCLK[2]           : SD Clock Enable
292          * STBLINTCLK[1]        : Internal Clock Stable
293          * ENINTCLK[0]          : Internal Clock Enable
294          */
295         clk = (div << 8) | (1 << 0);
296         writew(clk, &host->reg->clkcon);
297
298         /* Wait max 10 ms */
299         timeout = 10;
300         while (!(readw(&host->reg->clkcon) & (1 << 1))) {
301                 if (timeout == 0) {
302                         printf("%s: timeout error\n", __func__);
303                         return;
304                 }
305                 timeout--;
306                 udelay(1000);
307         }
308
309         clk |= (1 << 2);
310         writew(clk, &host->reg->clkcon);
311
312 out:
313         host->clock = clock;
314 }
315
316 static void mmc_set_ios(struct mmc *mmc)
317 {
318         struct mmc_host *host = mmc->priv;
319         unsigned char ctrl;
320         unsigned long val;
321
322         debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
323
324         /*
325          * SELCLKPADDS[17:16]
326          * 00 = 2mA
327          * 01 = 4mA
328          * 10 = 7mA
329          * 11 = 9mA
330          */
331         writel(0x3 << 16, &host->reg->control4);
332
333         val = readl(&host->reg->control2);
334         val &= (0x3 << 4);
335
336         val |=  (1 << 31) |     /* write status clear async mode enable */
337                 (1 << 30) |     /* command conflict mask enable */
338                 (1 << 14) |     /* Feedback Clock Enable for Rx Clock */
339                 (1 << 8);       /* SDCLK hold enable */
340
341         writel(val, &host->reg->control2);
342
343         /*
344          * FCSEL1[15] FCSEL0[7]
345          * FCSel[1:0] : Rx Feedback Clock Delay Control
346          *      Inverter delay means10ns delay if SDCLK 50MHz setting
347          *      01 = Delay1 (basic delay)
348          *      11 = Delay2 (basic delay + 2ns)
349          *      00 = Delay3 (inverter delay)
350          *      10 = Delay4 (inverter delay + 2ns)
351          */
352         writel(0x8080, &host->reg->control3);
353
354         mmc_change_clock(host, mmc->clock);
355
356         ctrl = readb(&host->reg->hostctl);
357
358         /*
359          * WIDE4[1]
360          * 1 = 4-bit mode
361          * 0 = 1-bit mode
362          */
363         if (mmc->bus_width == 4)
364                 ctrl |= (1 << 1);
365         else
366                 ctrl &= ~(1 << 1);
367
368         /*
369          * OUTEDGEINV[2]
370          * 1 = Riging edge output
371          * 0 = Falling edge output
372          */
373         ctrl &= ~(1 << 2);
374
375         writeb(ctrl, &host->reg->hostctl);
376 }
377
378 static void mmc_reset(struct mmc_host *host)
379 {
380         unsigned int timeout;
381
382         /*
383          * RSTALL[0] : Software reset for all
384          * 1 = reset
385          * 0 = work
386          */
387         writeb((1 << 0), &host->reg->swrst);
388
389         host->clock = 0;
390
391         /* Wait max 100 ms */
392         timeout = 100;
393
394         /* hw clears the bit when it's done */
395         while (readb(&host->reg->swrst) & (1 << 0)) {
396                 if (timeout == 0) {
397                         printf("%s: timeout error\n", __func__);
398                         return;
399                 }
400                 timeout--;
401                 udelay(1000);
402         }
403 }
404
405 static int mmc_core_init(struct mmc *mmc)
406 {
407         struct mmc_host *host = (struct mmc_host *)mmc->priv;
408         unsigned int mask;
409
410         mmc_reset(host);
411
412         host->version = readw(&host->reg->hcver);
413
414         /* mask all */
415         writel(0xffffffff, &host->reg->norintstsen);
416         writel(0xffffffff, &host->reg->norintsigen);
417
418         writeb(0xe, &host->reg->timeoutcon);    /* TMCLK * 2^27 */
419
420         /*
421          * NORMAL Interrupt Status Enable Register init
422          * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
423          * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
424          * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
425          * [0] ENSTACMDCMPLT : Command Complete Status Enable
426         */
427         mask = readl(&host->reg->norintstsen);
428         mask &= ~(0xffff);
429         mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
430         writel(mask, &host->reg->norintstsen);
431
432         /*
433          * NORMAL Interrupt Signal Enable Register init
434          * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
435          */
436         mask = readl(&host->reg->norintsigen);
437         mask &= ~(0xffff);
438         mask |= (1 << 1);
439         writel(mask, &host->reg->norintsigen);
440
441         return 0;
442 }
443
444 static int s5p_mmc_initialize(int dev_index)
445 {
446         struct mmc *mmc;
447
448         mmc = &mmc_dev[dev_index];
449
450         sprintf(mmc->name, "SAMSUNG SD/MMC");
451         mmc->priv = &mmc_host[dev_index];
452         mmc->send_cmd = mmc_send_cmd;
453         mmc->set_ios = mmc_set_ios;
454         mmc->init = mmc_core_init;
455
456         mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
457         mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
458
459         mmc->f_min = 400000;
460         mmc->f_max = 52000000;
461
462         mmc_host[dev_index].clock = 0;
463         mmc_host[dev_index].reg = s5p_get_base_mmc(dev_index);
464         mmc_register(mmc);
465
466         return 0;
467 }
468
469 int s5p_mmc_init(int dev_index)
470 {
471         return s5p_mmc_initialize(dev_index);
472 }