2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
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.
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.
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
23 #include <asm/arch/mmc.h>
25 #ifdef DEBUG_S3C_HSMMC
26 #define dbg(x...) printf(x)
28 #define dbg(x...) do { } while (0)
31 /* s5pc1xx support 4 mmc hosts */
32 struct mmc mmc_dev[4];
33 struct mmc_host mmc_host[4];
35 static inline struct s5pc1xx_mmc *s5pc1xx_get_base_mmc(int dev_index)
37 unsigned long offset = dev_index * sizeof(struct s5pc1xx_mmc);
40 return (struct s5pc1xx_mmc *)(S5PC100_MMC_BASE + offset);
42 return (struct s5pc1xx_mmc *)(S5PC110_MMC_BASE + offset);
45 static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
49 writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
51 dbg("data->dest: %08x\n", (u32)data->dest);
52 writel((u32)data->dest, &host->reg->sysad);
58 * 10 = Selects 32-bit Address ADMA2
59 * 11 = Selects 64-bit Address ADMA2
61 ctrl = readb(&host->reg->hostctl);
63 writeb(ctrl, &host->reg->hostctl);
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);
70 static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
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
83 mode = (1 << 1) | (1 << 0);
85 mode |= ((1 << 5) | (1 << 2));
86 if (data->flags & MMC_DATA_READ)
89 writew(mode, &host->reg->trnmod);
93 * Sends a command out on the bus. Takes the mmc pointer,
94 * a command pointer, and an optional data pointer.
96 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
97 struct mmc_data *data)
99 struct mmc_host *host = (struct mmc_host *)mmc->priv;
101 unsigned int timeout;
103 unsigned int retry = 0x100000;
110 * CMDINHDAT[1] : Command Inhibit (DAT)
111 * CMDINHCMD[0] : Command Inhibit (CMD)
114 if ((data != NULL) || (cmd->flags & MMC_RSP_BUSY))
118 * We shouldn't wait for data inihibit for stop commands, even
119 * though they might use busy signaling
124 while (readl(&host->reg->prnsts) & mask) {
126 printf("%s: timeout error\n", __func__);
134 mmc_prepare_data(host, data);
136 dbg("cmd->arg: %08x\n", cmd->cmdarg);
137 writel(cmd->cmdarg, &host->reg->argument);
140 mmc_set_transfer_mode(host, data);
142 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
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
155 * 11 = Length 48 Check busy after response
157 if (!(cmd->resp_type & MMC_RSP_PRESENT))
159 else if (cmd->resp_type & MMC_RSP_136)
161 else if (cmd->resp_type & MMC_RSP_BUSY)
166 if (cmd->resp_type & MMC_RSP_CRC)
168 if (cmd->resp_type & MMC_RSP_OPCODE)
173 dbg("cmd: %d\n", cmd->cmdidx);
175 writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
177 for (i = 0; i < retry; i++) {
178 mask = readl(&host->reg->norintsts);
179 /* Command Complete */
180 if (mask & (1 << 0)) {
182 writel(mask, &host->reg->norintsts);
188 printf("%s: waiting for status update\n", __func__);
192 if (mask & (1 << 16)) {
194 dbg("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
196 } else if (mask & (1 << 15)) {
197 /* Error Interrupt */
198 dbg("error: %08x cmd %d\n", mask, cmd->cmdidx);
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;
214 dbg("cmd->resp[%d]: %08x\n",
215 i, cmd->response[i]);
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] */
226 printf("%s: card is still busy\n", __func__);
230 cmd->response[0] = readl(&host->reg->rspreg0);
231 dbg("cmd->resp[0]: %08x\n", cmd->response[0]);
233 cmd->response[0] = readl(&host->reg->rspreg0);
234 dbg("cmd->resp[0]: %08x\n", cmd->response[0]);
240 mask = readl(&host->reg->norintsts);
242 if (mask & (1 << 15)) {
243 /* Error Interrupt */
244 writel(mask, &host->reg->norintsts);
245 printf("%s: error during transfer: 0x%08x\n",
248 } else if (mask & (1 << 3)) {
252 } else if (mask & (1 << 1)) {
253 /* Transfer Complete */
254 dbg("r/w is done\n");
258 writel(mask, &host->reg->norintsts);
265 static void mmc_change_clock(struct mmc_host *host, uint clock)
269 unsigned long timeout;
276 * 11 = XTI or XEXTCLK
278 ctrl2 = readl(&host->reg->control2);
281 writew(ctrl2, &host->reg->control2);
283 writew(0, &host->reg->clkcon);
285 /* XXX: we assume that clock is between 40MHz and 50MHz */
288 else if (clock <= 400000)
290 else if (clock <= 20000000)
292 else if (clock <= 26000000)
296 dbg("div: %d\n", div);
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
305 clk = (div << 8) | (1 << 0);
306 writew(clk, &host->reg->clkcon);
310 while (!(readw(&host->reg->clkcon) & (1 << 1))) {
312 printf("%s: timeout error\n", __func__);
320 writew(clk, &host->reg->clkcon);
327 static void mmc_set_ios(struct mmc *mmc)
329 struct mmc_host *host = mmc->priv;
333 dbg("set_ios: bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
342 writel(0x3 << 16, &host->reg->control4);
344 val = (1 << 31) | /* write status clear async mode enable */
345 (1 << 30) | /* command conflict mask enable */
346 (1 << 8); /* SDCLK hold enable */
348 writel(val, &host->reg->control2);
350 writel(0, &host->reg->control3);
352 mmc_change_clock(host, mmc->clock);
354 ctrl = readb(&host->reg->hostctl);
361 if (mmc->bus_width == 4)
368 * 1 = Riging edge output
369 * 0 = Falling edge output
373 writeb(ctrl, &host->reg->hostctl);
376 static void mmc_reset(struct mmc_host *host)
378 unsigned int timeout;
381 * RSTALL[0] : Software reset for all
385 writeb((1 << 0), &host->reg->swrst);
389 /* Wait max 100 ms */
392 /* hw clears the bit when it's done */
393 while (readb(&host->reg->swrst) & (1 << 0)) {
395 printf("%s: timeout error\n", __func__);
403 static int mmc_core_init(struct mmc *mmc)
405 struct mmc_host *host = (struct mmc_host *)mmc->priv;
409 host->version = readw(&host->reg->hcver);
414 writel(0xffffffff, &host->reg->norintstsen);
415 writel(0xffffffff, &host->reg->norintsigen);
417 mmc_change_clock(host, 400000);
422 static int s5pc1xx_mmc_initialize(int dev_index)
426 mmc = &mmc_dev[dev_index];
428 sprintf(mmc->name, "S5PC1XX SD/MMC");
429 mmc->priv = &mmc_host[dev_index];
430 mmc->send_cmd = mmc_send_cmd;
431 mmc->set_ios = mmc_set_ios;
432 mmc->init = mmc_core_init;
434 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
435 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_HS_52MHz | MMC_MODE_HS;
438 mmc->f_max = 52000000;
440 mmc_host[dev_index].clock = 0;
441 mmc_host[dev_index].reg = s5pc1xx_get_base_mmc(dev_index);
447 int s5pc1xx_mmc_init(int dev_index)
449 return s5pc1xx_mmc_initialize(dev_index);