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