2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Jaehoon Chung <jh80.chung@samsung.com>
4 * Rajeshawari Shinde <rajeshwari.s@samsung.com>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (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
26 #include <asm/arch/clk.h>
27 #include <asm-generic/errno.h>
29 #define PAGE_SIZE 4096
31 static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
33 unsigned long timeout = 1000;
36 dwmci_writel(host, DWMCI_CTRL, value);
39 ctrl = dwmci_readl(host, DWMCI_CTRL);
40 if (!(ctrl & DWMCI_RESET_ALL))
46 static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
47 u32 desc0, u32 desc1, u32 desc2)
49 struct dwmci_idmac *desc = idmac;
54 desc->next_addr = (unsigned int)desc + sizeof(struct dwmci_idmac);
57 static void dwmci_prepare_data(struct dwmci_host *host,
58 struct mmc_data *data)
61 unsigned int i = 0, flags, cnt, blk_cnt;
62 ulong data_start, data_end, start_addr;
63 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac, data->blocks);
66 blk_cnt = data->blocks;
68 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
70 data_start = (ulong)cur_idmac;
71 dwmci_writel(host, DWMCI_DBADDR, (unsigned int)cur_idmac);
73 if (data->flags == MMC_DATA_READ)
74 start_addr = (unsigned int)data->dest;
76 start_addr = (unsigned int)data->src;
79 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
80 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
82 flags |= DWMCI_IDMAC_LD;
83 cnt = data->blocksize * blk_cnt;
85 cnt = data->blocksize * 8;
87 dwmci_set_idma_desc(cur_idmac, flags, cnt,
88 start_addr + (i * PAGE_SIZE));
97 data_end = (ulong)cur_idmac;
98 flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
100 ctrl = dwmci_readl(host, DWMCI_CTRL);
101 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
102 dwmci_writel(host, DWMCI_CTRL, ctrl);
104 ctrl = dwmci_readl(host, DWMCI_BMOD);
105 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
106 dwmci_writel(host, DWMCI_BMOD, ctrl);
108 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
109 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
112 static int dwmci_set_transfer_mode(struct dwmci_host *host,
113 struct mmc_data *data)
117 mode = DWMCI_CMD_DATA_EXP;
118 if (data->flags & MMC_DATA_WRITE)
119 mode |= DWMCI_CMD_RW;
124 static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
125 struct mmc_data *data)
127 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
129 unsigned int timeout = 100000;
132 ulong start = get_timer(0);
134 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
135 if (get_timer(start) > timeout) {
136 printf("Timeout on data busy\n");
141 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
144 dwmci_prepare_data(host, data);
146 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
149 flags = dwmci_set_transfer_mode(host, data);
151 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
154 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
155 flags |= DWMCI_CMD_ABORT_STOP;
157 flags |= DWMCI_CMD_PRV_DAT_WAIT;
159 if (cmd->resp_type & MMC_RSP_PRESENT) {
160 flags |= DWMCI_CMD_RESP_EXP;
161 if (cmd->resp_type & MMC_RSP_136)
162 flags |= DWMCI_CMD_RESP_LENGTH;
165 if (cmd->resp_type & MMC_RSP_CRC)
166 flags |= DWMCI_CMD_CHECK_CRC;
168 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
170 debug("Sending CMD%d\n",cmd->cmdidx);
172 dwmci_writel(host, DWMCI_CMD, flags);
174 for (i = 0; i < retry; i++) {
175 mask = dwmci_readl(host, DWMCI_RINTSTS);
176 if (mask & DWMCI_INTMSK_CDONE) {
178 dwmci_writel(host, DWMCI_RINTSTS, mask);
186 if (mask & DWMCI_INTMSK_RTO) {
187 debug("Response Timeout..\n");
189 } else if (mask & DWMCI_INTMSK_RE) {
190 debug("Response Error..\n");
195 if (cmd->resp_type & MMC_RSP_PRESENT) {
196 if (cmd->resp_type & MMC_RSP_136) {
197 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
198 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
199 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
200 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
202 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
208 mask = dwmci_readl(host, DWMCI_RINTSTS);
209 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
210 debug("DATA ERROR!\n");
213 } while (!(mask & DWMCI_INTMSK_DTO));
215 dwmci_writel(host, DWMCI_RINTSTS, mask);
217 ctrl = dwmci_readl(host, DWMCI_CTRL);
218 ctrl &= ~(DWMCI_DMA_EN);
219 dwmci_writel(host, DWMCI_CTRL, ctrl);
227 static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
233 if ((freq == host->clock) || (freq == 0))
236 * If host->mmc_clk didn't define,
237 * then assume that host->bus_hz is source clock value.
238 * host->bus_hz should be set from user.
241 sclk = host->mmc_clk(host->dev_index);
242 else if (host->bus_hz)
245 printf("Didn't get source clock value..\n");
249 div = DIV_ROUND_UP(sclk, 2 * freq);
251 dwmci_writel(host, DWMCI_CLKENA, 0);
252 dwmci_writel(host, DWMCI_CLKSRC, 0);
254 dwmci_writel(host, DWMCI_CLKDIV, div);
255 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
256 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
259 status = dwmci_readl(host, DWMCI_CMD);
261 printf("TIMEOUT error!!\n");
264 } while (status & DWMCI_CMD_START);
266 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
267 DWMCI_CLKEN_LOW_PWR);
269 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
270 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
274 status = dwmci_readl(host, DWMCI_CMD);
276 printf("TIMEOUT error!!\n");
279 } while (status & DWMCI_CMD_START);
286 static void dwmci_set_ios(struct mmc *mmc)
288 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
291 debug("Buswidth = %d, clock: %d\n",mmc->bus_width, mmc->clock);
293 dwmci_setup_bus(host, mmc->clock);
294 switch (mmc->bus_width) {
296 ctype = DWMCI_CTYPE_8BIT;
299 ctype = DWMCI_CTYPE_4BIT;
302 ctype = DWMCI_CTYPE_1BIT;
306 dwmci_writel(host, DWMCI_CTYPE, ctype);
312 static int dwmci_init(struct mmc *mmc)
314 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
315 u32 fifo_size, fifoth_val;
317 dwmci_writel(host, DWMCI_PWREN, 1);
319 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
320 debug("%s[%d] Fail-reset!!\n",__func__,__LINE__);
324 /* Enumerate at 400KHz */
325 dwmci_setup_bus(host, mmc->f_min);
327 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
328 dwmci_writel(host, DWMCI_INTMASK, 0);
330 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
332 dwmci_writel(host, DWMCI_IDINTEN, 0);
333 dwmci_writel(host, DWMCI_BMOD, 1);
335 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
336 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
337 if (host->fifoth_val) {
338 fifoth_val = host->fifoth_val;
340 fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
341 TX_WMARK(fifo_size / 2);
343 dwmci_writel(host, DWMCI_FIFOTH, fifoth_val);
345 dwmci_writel(host, DWMCI_CLKENA, 0);
346 dwmci_writel(host, DWMCI_CLKSRC, 0);
351 int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
356 mmc = malloc(sizeof(struct mmc));
358 printf("mmc malloc fail!\n");
365 sprintf(mmc->name, "%s", host->name);
366 mmc->send_cmd = dwmci_send_cmd;
367 mmc->set_ios = dwmci_set_ios;
368 mmc->init = dwmci_init;
369 mmc->f_min = min_clk;
370 mmc->f_max = max_clk;
372 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
374 mmc->host_caps = host->caps;
376 if (host->buswidth == 8) {
377 mmc->host_caps |= MMC_MODE_8BIT;
378 mmc->host_caps &= ~MMC_MODE_4BIT;
380 mmc->host_caps |= MMC_MODE_4BIT;
381 mmc->host_caps &= ~MMC_MODE_8BIT;
383 mmc->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz | MMC_MODE_HC;
385 err = mmc_register(mmc);