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