2 * (C) Copyright 2009 SAMSUNG Electronics
3 * Minkyu Kang <mk7.kang@samsung.com>
4 * Jaehoon Chung <jh80.chung@samsung.com>
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.
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.
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
24 #include <asm/arch/mmc.h>
26 /* support 4 mmc hosts */
27 struct mmc mmc_dev[4];
28 struct mmc_host mmc_host[4];
30 static inline struct s5p_mmc *s5p_get_base_mmc(int dev_index)
32 unsigned long offset = dev_index * sizeof(struct s5p_mmc);
33 return (struct s5p_mmc *)(samsung_get_base_mmc() + offset);
36 static void mmc_prepare_data(struct mmc_host *host, struct mmc_data *data)
40 debug("data->dest: %08x\n", (u32)data->dest);
41 writel((u32)data->dest, &host->reg->sysad);
46 * 10 = Selects 32-bit Address ADMA2
47 * 11 = Selects 64-bit Address ADMA2
49 ctrl = readb(&host->reg->hostctl);
51 writeb(ctrl, &host->reg->hostctl);
53 /* We do not handle DMA boundaries, so set it to max (512 KiB) */
54 writew((7 << 12) | (data->blocksize & 0xFFF), &host->reg->blksize);
55 writew(data->blocks, &host->reg->blkcnt);
58 static void mmc_set_transfer_mode(struct mmc_host *host, struct mmc_data *data)
64 * MUL1SIN0[5] : Multi/Single Block Select
65 * RD1WT0[4] : Data Transfer Direction Select
68 * ENACMD12[2] : Auto CMD12 Enable
69 * ENBLKCNT[1] : Block Count Enable
70 * ENDMA[0] : DMA Enable
72 mode = (1 << 1) | (1 << 0);
75 if (data->flags & MMC_DATA_READ)
78 writew(mode, &host->reg->trnmod);
81 static int mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
82 struct mmc_data *data)
84 struct mmc_host *host = (struct mmc_host *)mmc->priv;
88 unsigned int retry = 0x100000;
95 * CMDINHDAT[1] : Command Inhibit (DAT)
96 * CMDINHCMD[0] : Command Inhibit (CMD)
99 if ((data != NULL) || (cmd->resp_type & MMC_RSP_BUSY))
103 * We shouldn't wait for data inihibit for stop commands, even
104 * though they might use busy signaling
109 while (readl(&host->reg->prnsts) & mask) {
111 printf("%s: timeout error\n", __func__);
119 mmc_prepare_data(host, data);
121 debug("cmd->arg: %08x\n", cmd->cmdarg);
122 writel(cmd->cmdarg, &host->reg->argument);
125 mmc_set_transfer_mode(host, data);
127 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
132 * CMDIDX[13:8] : Command index
133 * DATAPRNT[5] : Data Present Select
134 * ENCMDIDX[4] : Command Index Check Enable
135 * ENCMDCRC[3] : Command CRC Check Enable
140 * 11 = Length 48 Check busy after response
142 if (!(cmd->resp_type & MMC_RSP_PRESENT))
144 else if (cmd->resp_type & MMC_RSP_136)
146 else if (cmd->resp_type & MMC_RSP_BUSY)
151 if (cmd->resp_type & MMC_RSP_CRC)
153 if (cmd->resp_type & MMC_RSP_OPCODE)
158 debug("cmd: %d\n", cmd->cmdidx);
160 writew((cmd->cmdidx << 8) | flags, &host->reg->cmdreg);
162 for (i = 0; i < retry; i++) {
163 mask = readl(&host->reg->norintsts);
164 /* Command Complete */
165 if (mask & (1 << 0)) {
167 writel(mask, &host->reg->norintsts);
173 printf("%s: waiting for status update\n", __func__);
177 if (mask & (1 << 16)) {
179 debug("timeout: %08x cmd %d\n", mask, cmd->cmdidx);
181 } else if (mask & (1 << 15)) {
182 /* Error Interrupt */
183 debug("error: %08x cmd %d\n", mask, cmd->cmdidx);
187 if (cmd->resp_type & MMC_RSP_PRESENT) {
188 if (cmd->resp_type & MMC_RSP_136) {
189 /* CRC is stripped so we need to do some shifting. */
190 for (i = 0; i < 4; i++) {
191 unsigned int offset =
192 (unsigned int)(&host->reg->rspreg3 - i);
193 cmd->response[i] = readl(offset) << 8;
199 debug("cmd->resp[%d]: %08x\n",
200 i, cmd->response[i]);
202 } else if (cmd->resp_type & MMC_RSP_BUSY) {
203 for (i = 0; i < retry; i++) {
204 /* PRNTDATA[23:20] : DAT[3:0] Line Signal */
205 if (readl(&host->reg->prnsts)
206 & (1 << 20)) /* DAT[0] */
211 printf("%s: card is still busy\n", __func__);
215 cmd->response[0] = readl(&host->reg->rspreg0);
216 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
218 cmd->response[0] = readl(&host->reg->rspreg0);
219 debug("cmd->resp[0]: %08x\n", cmd->response[0]);
225 mask = readl(&host->reg->norintsts);
227 if (mask & (1 << 15)) {
228 /* Error Interrupt */
229 writel(mask, &host->reg->norintsts);
230 printf("%s: error during transfer: 0x%08x\n",
233 } else if (mask & (1 << 3)) {
237 } else if (mask & (1 << 1)) {
238 /* Transfer Complete */
239 debug("r/w is done\n");
243 writel(mask, &host->reg->norintsts);
250 static void mmc_change_clock(struct mmc_host *host, uint clock)
254 unsigned long timeout;
261 * 11 = XTI or XEXTCLK
263 ctrl2 = readl(&host->reg->control2);
266 writel(ctrl2, &host->reg->control2);
268 writew(0, &host->reg->clkcon);
270 /* XXX: we assume that clock is between 40MHz and 50MHz */
273 else if (clock <= 400000)
275 else if (clock <= 20000000)
277 else if (clock <= 26000000)
281 debug("div: %d\n", div);
286 * SELFREQ[15:8] : base clock divied by value
287 * ENSDCLK[2] : SD Clock Enable
288 * STBLINTCLK[1] : Internal Clock Stable
289 * ENINTCLK[0] : Internal Clock Enable
291 clk = (div << 8) | (1 << 0);
292 writew(clk, &host->reg->clkcon);
296 while (!(readw(&host->reg->clkcon) & (1 << 1))) {
298 printf("%s: timeout error\n", __func__);
306 writew(clk, &host->reg->clkcon);
312 static void mmc_set_ios(struct mmc *mmc)
314 struct mmc_host *host = mmc->priv;
318 debug("bus_width: %x, clock: %d\n", mmc->bus_width, mmc->clock);
327 writel(0x3 << 16, &host->reg->control4);
329 val = readl(&host->reg->control2);
332 val |= (1 << 31) | /* write status clear async mode enable */
333 (1 << 30) | /* command conflict mask enable */
334 (1 << 14) | /* Feedback Clock Enable for Rx Clock */
335 (1 << 8); /* SDCLK hold enable */
337 writel(val, &host->reg->control2);
340 * FCSEL1[15] FCSEL0[7]
341 * FCSel[1:0] : Rx Feedback Clock Delay Control
342 * Inverter delay means10ns delay if SDCLK 50MHz setting
343 * 01 = Delay1 (basic delay)
344 * 11 = Delay2 (basic delay + 2ns)
345 * 00 = Delay3 (inverter delay)
346 * 10 = Delay4 (inverter delay + 2ns)
348 writel(0x8080, &host->reg->control3);
350 mmc_change_clock(host, mmc->clock);
352 ctrl = readb(&host->reg->hostctl);
356 * 0 = Depend on WIDE4
362 if (mmc->bus_width == 8)
364 else if (mmc->bus_width == 4)
371 * 1 = Riging edge output
372 * 0 = Falling edge output
376 writeb(ctrl, &host->reg->hostctl);
379 static void mmc_reset(struct mmc_host *host)
381 unsigned int timeout;
384 * RSTALL[0] : Software reset for all
388 writeb((1 << 0), &host->reg->swrst);
392 /* Wait max 100 ms */
395 /* hw clears the bit when it's done */
396 while (readb(&host->reg->swrst) & (1 << 0)) {
398 printf("%s: timeout error\n", __func__);
406 static int mmc_core_init(struct mmc *mmc)
408 struct mmc_host *host = (struct mmc_host *)mmc->priv;
413 host->version = readw(&host->reg->hcver);
416 writel(0xffffffff, &host->reg->norintstsen);
417 writel(0xffffffff, &host->reg->norintsigen);
419 writeb(0xe, &host->reg->timeoutcon); /* TMCLK * 2^27 */
422 * NORMAL Interrupt Status Enable Register init
423 * [5] ENSTABUFRDRDY : Buffer Read Ready Status Enable
424 * [4] ENSTABUFWTRDY : Buffer write Ready Status Enable
425 * [1] ENSTASTANSCMPLT : Transfre Complete Status Enable
426 * [0] ENSTACMDCMPLT : Command Complete Status Enable
428 mask = readl(&host->reg->norintstsen);
430 mask |= (1 << 5) | (1 << 4) | (1 << 1) | (1 << 0);
431 writel(mask, &host->reg->norintstsen);
434 * NORMAL Interrupt Signal Enable Register init
435 * [1] ENSTACMDCMPLT : Transfer Complete Signal Enable
437 mask = readl(&host->reg->norintsigen);
440 writel(mask, &host->reg->norintsigen);
445 static int s5p_mmc_initialize(int dev_index, int bus_width)
449 mmc = &mmc_dev[dev_index];
451 sprintf(mmc->name, "SAMSUNG SD/MMC");
452 mmc->priv = &mmc_host[dev_index];
453 mmc->send_cmd = mmc_send_cmd;
454 mmc->set_ios = mmc_set_ios;
455 mmc->init = mmc_core_init;
457 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
459 mmc->host_caps = MMC_MODE_8BIT;
461 mmc->host_caps = MMC_MODE_4BIT;
462 mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
465 mmc->f_max = 52000000;
467 mmc_host[dev_index].clock = 0;
468 mmc_host[dev_index].reg = s5p_get_base_mmc(dev_index);
475 int s5p_mmc_init(int dev_index, int bus_width)
477 return s5p_mmc_initialize(dev_index, bus_width);