blob: fcd5784899da68181d1d9f7a370451ab8f131bb1 [file] [log] [blame]
Jaehoon Chung7cf73072012-10-15 19:10:29 +00001/*
2 * (C) Copyright 2012 SAMSUNG Electronics
3 * Jaehoon Chung <jh80.chung@samsung.com>
4 * Rajeshawari Shinde <rajeshwari.s@samsung.com>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Jaehoon Chung7cf73072012-10-15 19:10:29 +00007 */
8
Alexey Brodkin55bab5e2013-12-26 15:29:07 +04009#include <bouncebuf.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000010#include <common.h>
11#include <malloc.h>
12#include <mmc.h>
13#include <dwmmc.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000014#include <asm-generic/errno.h>
15
16#define PAGE_SIZE 4096
17
18static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
19{
20 unsigned long timeout = 1000;
21 u32 ctrl;
22
23 dwmci_writel(host, DWMCI_CTRL, value);
24
25 while (timeout--) {
26 ctrl = dwmci_readl(host, DWMCI_CTRL);
27 if (!(ctrl & DWMCI_RESET_ALL))
28 return 1;
29 }
30 return 0;
31}
32
33static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
34 u32 desc0, u32 desc1, u32 desc2)
35{
36 struct dwmci_idmac *desc = idmac;
37
38 desc->flags = desc0;
39 desc->cnt = desc1;
40 desc->addr = desc2;
41 desc->next_addr = (unsigned int)desc + sizeof(struct dwmci_idmac);
42}
43
44static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040045 struct mmc_data *data,
46 struct dwmci_idmac *cur_idmac,
47 void *bounce_buffer)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000048{
49 unsigned long ctrl;
50 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040051 ulong data_start, data_end;
Jaehoon Chung7cf73072012-10-15 19:10:29 +000052
53
54 blk_cnt = data->blocks;
55
56 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
57
58 data_start = (ulong)cur_idmac;
59 dwmci_writel(host, DWMCI_DBADDR, (unsigned int)cur_idmac);
60
Jaehoon Chung7cf73072012-10-15 19:10:29 +000061 do {
62 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
63 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
64 if (blk_cnt <= 8) {
65 flags |= DWMCI_IDMAC_LD;
66 cnt = data->blocksize * blk_cnt;
67 } else
68 cnt = data->blocksize * 8;
69
70 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Alexey Brodkin55bab5e2013-12-26 15:29:07 +040071 (u32)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung7cf73072012-10-15 19:10:29 +000072
Mischa Jonkera7a60912013-07-26 16:18:40 +020073 if (blk_cnt <= 8)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000074 break;
75 blk_cnt -= 8;
76 cur_idmac++;
77 i++;
78 } while(1);
79
80 data_end = (ulong)cur_idmac;
81 flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
82
83 ctrl = dwmci_readl(host, DWMCI_CTRL);
84 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
85 dwmci_writel(host, DWMCI_CTRL, ctrl);
86
87 ctrl = dwmci_readl(host, DWMCI_BMOD);
88 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
89 dwmci_writel(host, DWMCI_BMOD, ctrl);
90
91 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
92 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
93}
94
95static int dwmci_set_transfer_mode(struct dwmci_host *host,
96 struct mmc_data *data)
97{
98 unsigned long mode;
99
100 mode = DWMCI_CMD_DATA_EXP;
101 if (data->flags & MMC_DATA_WRITE)
102 mode |= DWMCI_CMD_RW;
103
104 return mode;
105}
106
107static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
108 struct mmc_data *data)
109{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200110 struct dwmci_host *host = mmc->priv;
Mischa Jonker7423bed2013-07-26 14:08:14 +0200111 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonkera7a60912013-07-26 16:18:40 +0200112 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut81e093f2015-07-27 22:39:38 +0200113 int ret = 0, flags = 0, i;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000114 unsigned int timeout = 100000;
115 u32 retry = 10000;
116 u32 mask, ctrl;
Amar902664c2013-04-27 11:42:54 +0530117 ulong start = get_timer(0);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400118 struct bounce_buffer bbstate;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000119
120 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar902664c2013-04-27 11:42:54 +0530121 if (get_timer(start) > timeout) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200122 printf("%s: Timeout on data busy\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000123 return TIMEOUT;
124 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000125 }
126
127 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
128
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400129 if (data) {
130 if (data->flags == MMC_DATA_READ) {
131 bounce_buffer_start(&bbstate, (void*)data->dest,
132 data->blocksize *
133 data->blocks, GEN_BB_WRITE);
134 } else {
135 bounce_buffer_start(&bbstate, (void*)data->src,
136 data->blocksize *
137 data->blocks, GEN_BB_READ);
138 }
139 dwmci_prepare_data(host, data, cur_idmac,
140 bbstate.bounce_buffer);
141 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000142
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000143 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
144
145 if (data)
146 flags = dwmci_set_transfer_mode(host, data);
147
148 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
149 return -1;
150
151 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
152 flags |= DWMCI_CMD_ABORT_STOP;
153 else
154 flags |= DWMCI_CMD_PRV_DAT_WAIT;
155
156 if (cmd->resp_type & MMC_RSP_PRESENT) {
157 flags |= DWMCI_CMD_RESP_EXP;
158 if (cmd->resp_type & MMC_RSP_136)
159 flags |= DWMCI_CMD_RESP_LENGTH;
160 }
161
162 if (cmd->resp_type & MMC_RSP_CRC)
163 flags |= DWMCI_CMD_CHECK_CRC;
164
165 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
166
167 debug("Sending CMD%d\n",cmd->cmdidx);
168
169 dwmci_writel(host, DWMCI_CMD, flags);
170
171 for (i = 0; i < retry; i++) {
172 mask = dwmci_readl(host, DWMCI_RINTSTS);
173 if (mask & DWMCI_INTMSK_CDONE) {
174 if (!data)
175 dwmci_writel(host, DWMCI_RINTSTS, mask);
176 break;
177 }
178 }
179
Pavel Macheka425f5d2014-09-05 12:49:48 +0200180 if (i == retry) {
181 printf("%s: Timeout.\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000182 return TIMEOUT;
Pavel Macheka425f5d2014-09-05 12:49:48 +0200183 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000184
185 if (mask & DWMCI_INTMSK_RTO) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200186 /*
187 * Timeout here is not necessarily fatal. (e)MMC cards
188 * will splat here when they receive CMD55 as they do
189 * not support this command and that is exactly the way
190 * to tell them apart from SD cards. Thus, this output
191 * below shall be debug(). eMMC cards also do not favor
192 * CMD8, please keep that in mind.
193 */
194 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000195 return TIMEOUT;
196 } else if (mask & DWMCI_INTMSK_RE) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200197 printf("%s: Response Error.\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000198 return -1;
199 }
200
201
202 if (cmd->resp_type & MMC_RSP_PRESENT) {
203 if (cmd->resp_type & MMC_RSP_136) {
204 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
205 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
206 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
207 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
208 } else {
209 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
210 }
211 }
212
213 if (data) {
Marek Vasut795de7b2015-07-27 22:39:37 +0200214 start = get_timer(0);
215 timeout = 1000;
216 for (;;) {
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000217 mask = dwmci_readl(host, DWMCI_RINTSTS);
Marek Vasut795de7b2015-07-27 22:39:37 +0200218 /* Error during data transfer. */
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000219 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200220 printf("%s: DATA ERROR!\n", __func__);
Marek Vasut81e093f2015-07-27 22:39:38 +0200221 ret = -EINVAL;
222 break;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000223 }
Marek Vasut795de7b2015-07-27 22:39:37 +0200224
225 /* Data arrived correctly. */
Marek Vasut81e093f2015-07-27 22:39:38 +0200226 if (mask & DWMCI_INTMSK_DTO) {
227 ret = 0;
Marek Vasut795de7b2015-07-27 22:39:37 +0200228 break;
Marek Vasut81e093f2015-07-27 22:39:38 +0200229 }
Marek Vasut795de7b2015-07-27 22:39:37 +0200230
231 /* Check for timeout. */
232 if (get_timer(start) > timeout) {
233 printf("%s: Timeout waiting for data!\n",
234 __func__);
Marek Vasut81e093f2015-07-27 22:39:38 +0200235 ret = TIMEOUT;
236 break;
Marek Vasut795de7b2015-07-27 22:39:37 +0200237 }
238 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000239
240 dwmci_writel(host, DWMCI_RINTSTS, mask);
241
242 ctrl = dwmci_readl(host, DWMCI_CTRL);
243 ctrl &= ~(DWMCI_DMA_EN);
244 dwmci_writel(host, DWMCI_CTRL, ctrl);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400245
246 bounce_buffer_stop(&bbstate);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000247 }
248
249 udelay(100);
250
Marek Vasut81e093f2015-07-27 22:39:38 +0200251 return ret;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000252}
253
254static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
255{
256 u32 div, status;
257 int timeout = 10000;
258 unsigned long sclk;
259
Amar902664c2013-04-27 11:42:54 +0530260 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000261 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000262 /*
Pavel Macheka425f5d2014-09-05 12:49:48 +0200263 * If host->get_mmc_clk isn't defined,
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000264 * then assume that host->bus_hz is source clock value.
Pavel Macheka425f5d2014-09-05 12:49:48 +0200265 * host->bus_hz should be set by user.
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000266 */
Jaehoon Chungd94735b2013-10-06 18:59:31 +0900267 if (host->get_mmc_clk)
Rajeshwari S Shindeccfa20b2014-02-05 10:48:15 +0530268 sclk = host->get_mmc_clk(host);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000269 else if (host->bus_hz)
270 sclk = host->bus_hz;
271 else {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200272 printf("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000273 return -EINVAL;
274 }
275
Chin Liang See4cfff952014-06-10 01:26:52 -0500276 if (sclk == freq)
277 div = 0; /* bypass mode */
278 else
279 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000280
281 dwmci_writel(host, DWMCI_CLKENA, 0);
282 dwmci_writel(host, DWMCI_CLKSRC, 0);
283
284 dwmci_writel(host, DWMCI_CLKDIV, div);
285 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
286 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
287
288 do {
289 status = dwmci_readl(host, DWMCI_CMD);
290 if (timeout-- < 0) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200291 printf("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000292 return -ETIMEDOUT;
293 }
294 } while (status & DWMCI_CMD_START);
295
296 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
297 DWMCI_CLKEN_LOW_PWR);
298
299 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
300 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
301
302 timeout = 10000;
303 do {
304 status = dwmci_readl(host, DWMCI_CMD);
305 if (timeout-- < 0) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200306 printf("%s: Timeout!\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000307 return -ETIMEDOUT;
308 }
309 } while (status & DWMCI_CMD_START);
310
311 host->clock = freq;
312
313 return 0;
314}
315
316static void dwmci_set_ios(struct mmc *mmc)
317{
Jaehoon Chunge8672942014-05-16 13:59:55 +0900318 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
319 u32 ctype, regs;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000320
Pavel Macheka425f5d2014-09-05 12:49:48 +0200321 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000322
323 dwmci_setup_bus(host, mmc->clock);
324 switch (mmc->bus_width) {
325 case 8:
326 ctype = DWMCI_CTYPE_8BIT;
327 break;
328 case 4:
329 ctype = DWMCI_CTYPE_4BIT;
330 break;
331 default:
332 ctype = DWMCI_CTYPE_1BIT;
333 break;
334 }
335
336 dwmci_writel(host, DWMCI_CTYPE, ctype);
337
Jaehoon Chunge8672942014-05-16 13:59:55 +0900338 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov54c0e222014-12-01 06:59:12 -0600339 if (mmc->ddr_mode)
Jaehoon Chunge8672942014-05-16 13:59:55 +0900340 regs |= DWMCI_DDR_MODE;
341 else
Jaehoon Chung401fc502015-01-14 17:37:53 +0900342 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chunge8672942014-05-16 13:59:55 +0900343
344 dwmci_writel(host, DWMCI_UHS_REG, regs);
345
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000346 if (host->clksel)
347 host->clksel(host);
348}
349
350static int dwmci_init(struct mmc *mmc)
351{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200352 struct dwmci_host *host = mmc->priv;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000353
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900354 if (host->board_init)
355 host->board_init(host);
Rajeshwari Shinde70163092013-10-29 12:53:13 +0530356
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000357 dwmci_writel(host, DWMCI_PWREN, 1);
358
359 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200360 printf("%s[%d] Fail-reset!!\n", __func__, __LINE__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000361 return -1;
362 }
363
Amar902664c2013-04-27 11:42:54 +0530364 /* Enumerate at 400KHz */
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200365 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar902664c2013-04-27 11:42:54 +0530366
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000367 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
368 dwmci_writel(host, DWMCI_INTMASK, 0);
369
370 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
371
372 dwmci_writel(host, DWMCI_IDINTEN, 0);
373 dwmci_writel(host, DWMCI_BMOD, 1);
374
Alexey Brodkindb8f8692013-11-27 17:00:52 +0400375 if (host->fifoth_val) {
376 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Amar902664c2013-04-27 11:42:54 +0530377 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000378
379 dwmci_writel(host, DWMCI_CLKENA, 0);
380 dwmci_writel(host, DWMCI_CLKSRC, 0);
381
382 return 0;
383}
384
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200385static const struct mmc_ops dwmci_ops = {
386 .send_cmd = dwmci_send_cmd,
387 .set_ios = dwmci_set_ios,
388 .init = dwmci_init,
389};
390
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000391int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
392{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200393 host->cfg.name = host->name;
394 host->cfg.ops = &dwmci_ops;
395 host->cfg.f_min = min_clk;
396 host->cfg.f_max = max_clk;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000397
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200398 host->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000399
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200400 host->cfg.host_caps = host->caps;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000401
402 if (host->buswidth == 8) {
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200403 host->cfg.host_caps |= MMC_MODE_8BIT;
404 host->cfg.host_caps &= ~MMC_MODE_4BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000405 } else {
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200406 host->cfg.host_caps |= MMC_MODE_4BIT;
407 host->cfg.host_caps &= ~MMC_MODE_8BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000408 }
Rob Herring5fd3edd2015-03-23 17:56:59 -0500409 host->cfg.host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200410
411 host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000412
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200413 host->mmc = mmc_create(&host->cfg, host);
414 if (host->mmc == NULL)
415 return -1;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000416
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200417 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000418}