blob: a6a5fc65790a18db6f52d6cdeb0b11bf7a0377fa [file] [log] [blame]
Jaehoon Chung757bff42012-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 Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
Jaehoon Chung757bff42012-10-15 19:10:29 +00007 */
8
Alexey Brodkin2a7a2102013-12-26 15:29:07 +04009#include <bouncebuf.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000010#include <common.h>
Simon Glass1c87ffe2015-08-06 20:16:27 -060011#include <errno.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000012#include <malloc.h>
Simon Glasscf92e052015-09-02 17:24:58 -060013#include <memalign.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000014#include <mmc.h>
15#include <dwmmc.h>
Jaehoon Chung757bff42012-10-15 19:10:29 +000016
17#define PAGE_SIZE 4096
18
19static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
20{
21 unsigned long timeout = 1000;
22 u32 ctrl;
23
24 dwmci_writel(host, DWMCI_CTRL, value);
25
26 while (timeout--) {
27 ctrl = dwmci_readl(host, DWMCI_CTRL);
28 if (!(ctrl & DWMCI_RESET_ALL))
29 return 1;
30 }
31 return 0;
32}
33
34static void dwmci_set_idma_desc(struct dwmci_idmac *idmac,
35 u32 desc0, u32 desc1, u32 desc2)
36{
37 struct dwmci_idmac *desc = idmac;
38
39 desc->flags = desc0;
40 desc->cnt = desc1;
41 desc->addr = desc2;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053042 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000043}
44
45static void dwmci_prepare_data(struct dwmci_host *host,
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040046 struct mmc_data *data,
47 struct dwmci_idmac *cur_idmac,
48 void *bounce_buffer)
Jaehoon Chung757bff42012-10-15 19:10:29 +000049{
50 unsigned long ctrl;
51 unsigned int i = 0, flags, cnt, blk_cnt;
Alexey Brodkin2a7a2102013-12-26 15:29:07 +040052 ulong data_start, data_end;
Jaehoon Chung757bff42012-10-15 19:10:29 +000053
54
55 blk_cnt = data->blocks;
56
57 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
58
59 data_start = (ulong)cur_idmac;
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053060 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac);
Jaehoon Chung757bff42012-10-15 19:10:29 +000061
Jaehoon Chung757bff42012-10-15 19:10:29 +000062 do {
63 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ;
64 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0;
65 if (blk_cnt <= 8) {
66 flags |= DWMCI_IDMAC_LD;
67 cnt = data->blocksize * blk_cnt;
68 } else
69 cnt = data->blocksize * 8;
70
71 dwmci_set_idma_desc(cur_idmac, flags, cnt,
Prabhakar Kushwaha41f7be32015-10-25 13:18:25 +053072 (ulong)bounce_buffer + (i * PAGE_SIZE));
Jaehoon Chung757bff42012-10-15 19:10:29 +000073
Mischa Jonker21bd5762013-07-26 16:18:40 +020074 if (blk_cnt <= 8)
Jaehoon Chung757bff42012-10-15 19:10:29 +000075 break;
76 blk_cnt -= 8;
77 cur_idmac++;
78 i++;
79 } while(1);
80
81 data_end = (ulong)cur_idmac;
82 flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN);
83
84 ctrl = dwmci_readl(host, DWMCI_CTRL);
85 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
86 dwmci_writel(host, DWMCI_CTRL, ctrl);
87
88 ctrl = dwmci_readl(host, DWMCI_BMOD);
89 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
90 dwmci_writel(host, DWMCI_BMOD, ctrl);
91
92 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
93 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
94}
95
huang lina65f51b2015-11-17 14:20:22 +080096static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
huang linf382eb82015-11-17 14:20:21 +080097{
98 int ret = 0;
huang lina65f51b2015-11-17 14:20:22 +080099 u32 timeout = 240000;
100 u32 mask, size, i, len = 0;
101 u32 *buf = NULL;
huang linf382eb82015-11-17 14:20:21 +0800102 ulong start = get_timer(0);
huang lina65f51b2015-11-17 14:20:22 +0800103 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >>
104 RX_WMARK_SHIFT) + 1) * 2;
105
106 size = data->blocksize * data->blocks / 4;
107 if (data->flags == MMC_DATA_READ)
108 buf = (unsigned int *)data->dest;
109 else
110 buf = (unsigned int *)data->src;
huang linf382eb82015-11-17 14:20:21 +0800111
112 for (;;) {
113 mask = dwmci_readl(host, DWMCI_RINTSTS);
114 /* Error during data transfer. */
115 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
116 debug("%s: DATA ERROR!\n", __func__);
117 ret = -EINVAL;
118 break;
119 }
120
huang lina65f51b2015-11-17 14:20:22 +0800121 if (host->fifo_mode && size) {
122 if (data->flags == MMC_DATA_READ) {
Jaehoon Chungca2ec9a2016-05-13 23:37:44 +0900123 if ((dwmci_readl(host, DWMCI_RINTSTS) &
huang lina65f51b2015-11-17 14:20:22 +0800124 DWMCI_INTMSK_RXDR)) {
125 len = dwmci_readl(host, DWMCI_STATUS);
126 len = (len >> DWMCI_FIFO_SHIFT) &
127 DWMCI_FIFO_MASK;
128 for (i = 0; i < len; i++)
129 *buf++ =
130 dwmci_readl(host, DWMCI_DATA);
131 dwmci_writel(host, DWMCI_RINTSTS,
132 DWMCI_INTMSK_RXDR);
133 }
134 } else {
Jaehoon Chungca2ec9a2016-05-13 23:37:44 +0900135 if ((dwmci_readl(host, DWMCI_RINTSTS) &
huang lina65f51b2015-11-17 14:20:22 +0800136 DWMCI_INTMSK_TXDR)) {
137 len = dwmci_readl(host, DWMCI_STATUS);
138 len = fifo_depth - ((len >>
139 DWMCI_FIFO_SHIFT) &
140 DWMCI_FIFO_MASK);
141 for (i = 0; i < len; i++)
142 dwmci_writel(host, DWMCI_DATA,
143 *buf++);
144 dwmci_writel(host, DWMCI_RINTSTS,
145 DWMCI_INTMSK_TXDR);
146 }
147 }
148 size = size > len ? (size - len) : 0;
149 }
150
huang linf382eb82015-11-17 14:20:21 +0800151 /* Data arrived correctly. */
152 if (mask & DWMCI_INTMSK_DTO) {
153 ret = 0;
154 break;
155 }
156
157 /* Check for timeout. */
158 if (get_timer(start) > timeout) {
159 debug("%s: Timeout waiting for data!\n",
160 __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900161 ret = -ETIMEDOUT;
huang linf382eb82015-11-17 14:20:21 +0800162 break;
163 }
164 }
165
166 dwmci_writel(host, DWMCI_RINTSTS, mask);
167
168 return ret;
169}
170
Jaehoon Chung757bff42012-10-15 19:10:29 +0000171static int dwmci_set_transfer_mode(struct dwmci_host *host,
172 struct mmc_data *data)
173{
174 unsigned long mode;
175
176 mode = DWMCI_CMD_DATA_EXP;
177 if (data->flags & MMC_DATA_WRITE)
178 mode |= DWMCI_CMD_RW;
179
180 return mode;
181}
182
Simon Glass691272f2016-06-12 23:30:23 -0600183#ifdef CONFIG_DM_MMC_OPS
Jaehoon Chung56283472016-06-28 15:52:21 +0900184static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
Simon Glass691272f2016-06-12 23:30:23 -0600185 struct mmc_data *data)
186{
187 struct mmc *mmc = mmc_get_mmc_dev(dev);
188#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000189static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
190 struct mmc_data *data)
191{
Simon Glass691272f2016-06-12 23:30:23 -0600192#endif
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200193 struct dwmci_host *host = mmc->priv;
Mischa Jonker2136d222013-07-26 14:08:14 +0200194 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac,
Mischa Jonker21bd5762013-07-26 16:18:40 +0200195 data ? DIV_ROUND_UP(data->blocks, 8) : 0);
Marek Vasut9042d972015-07-27 22:39:38 +0200196 int ret = 0, flags = 0, i;
Xu Ziyuan02ebd422016-07-19 09:38:22 +0800197 unsigned int timeout = 500;
Alexander Graf9b5b8b62016-03-04 01:09:52 +0100198 u32 retry = 100000;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000199 u32 mask, ctrl;
Amar9c50e352013-04-27 11:42:54 +0530200 ulong start = get_timer(0);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400201 struct bounce_buffer bbstate;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000202
203 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
Amar9c50e352013-04-27 11:42:54 +0530204 if (get_timer(start) > timeout) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600205 debug("%s: Timeout on data busy\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900206 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000207 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000208 }
209
210 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
211
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400212 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800213 if (host->fifo_mode) {
214 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
215 dwmci_writel(host, DWMCI_BYTCNT,
216 data->blocksize * data->blocks);
217 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400218 } else {
huang lina65f51b2015-11-17 14:20:22 +0800219 if (data->flags == MMC_DATA_READ) {
220 bounce_buffer_start(&bbstate, (void*)data->dest,
221 data->blocksize *
222 data->blocks, GEN_BB_WRITE);
223 } else {
224 bounce_buffer_start(&bbstate, (void*)data->src,
225 data->blocksize *
226 data->blocks, GEN_BB_READ);
227 }
228 dwmci_prepare_data(host, data, cur_idmac,
229 bbstate.bounce_buffer);
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400230 }
Alexey Brodkin2a7a2102013-12-26 15:29:07 +0400231 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000232
Jaehoon Chung757bff42012-10-15 19:10:29 +0000233 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
234
235 if (data)
236 flags = dwmci_set_transfer_mode(host, data);
237
238 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
239 return -1;
240
241 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
242 flags |= DWMCI_CMD_ABORT_STOP;
243 else
244 flags |= DWMCI_CMD_PRV_DAT_WAIT;
245
246 if (cmd->resp_type & MMC_RSP_PRESENT) {
247 flags |= DWMCI_CMD_RESP_EXP;
248 if (cmd->resp_type & MMC_RSP_136)
249 flags |= DWMCI_CMD_RESP_LENGTH;
250 }
251
252 if (cmd->resp_type & MMC_RSP_CRC)
253 flags |= DWMCI_CMD_CHECK_CRC;
254
255 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG);
256
257 debug("Sending CMD%d\n",cmd->cmdidx);
258
259 dwmci_writel(host, DWMCI_CMD, flags);
260
261 for (i = 0; i < retry; i++) {
262 mask = dwmci_readl(host, DWMCI_RINTSTS);
263 if (mask & DWMCI_INTMSK_CDONE) {
264 if (!data)
265 dwmci_writel(host, DWMCI_RINTSTS, mask);
266 break;
267 }
268 }
269
Pavel Machekf33c9302014-09-05 12:49:48 +0200270 if (i == retry) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600271 debug("%s: Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900272 return -ETIMEDOUT;
Pavel Machekf33c9302014-09-05 12:49:48 +0200273 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000274
275 if (mask & DWMCI_INTMSK_RTO) {
Pavel Machekf33c9302014-09-05 12:49:48 +0200276 /*
277 * Timeout here is not necessarily fatal. (e)MMC cards
278 * will splat here when they receive CMD55 as they do
279 * not support this command and that is exactly the way
280 * to tell them apart from SD cards. Thus, this output
281 * below shall be debug(). eMMC cards also do not favor
282 * CMD8, please keep that in mind.
283 */
284 debug("%s: Response Timeout.\n", __func__);
Jaehoon Chung915ffa52016-07-19 16:33:36 +0900285 return -ETIMEDOUT;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000286 } else if (mask & DWMCI_INTMSK_RE) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600287 debug("%s: Response Error.\n", __func__);
288 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000289 }
290
291
292 if (cmd->resp_type & MMC_RSP_PRESENT) {
293 if (cmd->resp_type & MMC_RSP_136) {
294 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
295 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
296 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
297 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
298 } else {
299 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
300 }
301 }
302
303 if (data) {
huang lina65f51b2015-11-17 14:20:22 +0800304 ret = dwmci_data_transfer(host, data);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000305
huang lina65f51b2015-11-17 14:20:22 +0800306 /* only dma mode need it */
307 if (!host->fifo_mode) {
308 ctrl = dwmci_readl(host, DWMCI_CTRL);
309 ctrl &= ~(DWMCI_DMA_EN);
310 dwmci_writel(host, DWMCI_CTRL, ctrl);
311 bounce_buffer_stop(&bbstate);
312 }
Jaehoon Chung757bff42012-10-15 19:10:29 +0000313 }
314
315 udelay(100);
316
Marek Vasut9042d972015-07-27 22:39:38 +0200317 return ret;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000318}
319
320static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
321{
322 u32 div, status;
323 int timeout = 10000;
324 unsigned long sclk;
325
Amar9c50e352013-04-27 11:42:54 +0530326 if ((freq == host->clock) || (freq == 0))
Jaehoon Chung757bff42012-10-15 19:10:29 +0000327 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000328 /*
Pavel Machekf33c9302014-09-05 12:49:48 +0200329 * If host->get_mmc_clk isn't defined,
Jaehoon Chung757bff42012-10-15 19:10:29 +0000330 * then assume that host->bus_hz is source clock value.
Pavel Machekf33c9302014-09-05 12:49:48 +0200331 * host->bus_hz should be set by user.
Jaehoon Chung757bff42012-10-15 19:10:29 +0000332 */
Jaehoon Chungb44fe832013-10-06 18:59:31 +0900333 if (host->get_mmc_clk)
Simon Glasse3563f22015-08-30 16:55:15 -0600334 sclk = host->get_mmc_clk(host, freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000335 else if (host->bus_hz)
336 sclk = host->bus_hz;
337 else {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600338 debug("%s: Didn't get source clock value.\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000339 return -EINVAL;
340 }
341
Chin Liang See6ace1532014-06-10 01:26:52 -0500342 if (sclk == freq)
343 div = 0; /* bypass mode */
344 else
345 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000346
347 dwmci_writel(host, DWMCI_CLKENA, 0);
348 dwmci_writel(host, DWMCI_CLKSRC, 0);
349
350 dwmci_writel(host, DWMCI_CLKDIV, div);
351 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
352 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
353
354 do {
355 status = dwmci_readl(host, DWMCI_CMD);
356 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600357 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000358 return -ETIMEDOUT;
359 }
360 } while (status & DWMCI_CMD_START);
361
362 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE |
363 DWMCI_CLKEN_LOW_PWR);
364
365 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT |
366 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START);
367
368 timeout = 10000;
369 do {
370 status = dwmci_readl(host, DWMCI_CMD);
371 if (timeout-- < 0) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600372 debug("%s: Timeout!\n", __func__);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000373 return -ETIMEDOUT;
374 }
375 } while (status & DWMCI_CMD_START);
376
377 host->clock = freq;
378
379 return 0;
380}
381
Simon Glass691272f2016-06-12 23:30:23 -0600382#ifdef CONFIG_DM_MMC_OPS
Jaehoon Chung56283472016-06-28 15:52:21 +0900383static int dwmci_set_ios(struct udevice *dev)
Simon Glass691272f2016-06-12 23:30:23 -0600384{
385 struct mmc *mmc = mmc_get_mmc_dev(dev);
386#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000387static void dwmci_set_ios(struct mmc *mmc)
388{
Simon Glass691272f2016-06-12 23:30:23 -0600389#endif
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900390 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
391 u32 ctype, regs;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000392
Pavel Machekf33c9302014-09-05 12:49:48 +0200393 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000394
395 dwmci_setup_bus(host, mmc->clock);
396 switch (mmc->bus_width) {
397 case 8:
398 ctype = DWMCI_CTYPE_8BIT;
399 break;
400 case 4:
401 ctype = DWMCI_CTYPE_4BIT;
402 break;
403 default:
404 ctype = DWMCI_CTYPE_1BIT;
405 break;
406 }
407
408 dwmci_writel(host, DWMCI_CTYPE, ctype);
409
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900410 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov2b8a9692014-12-01 06:59:12 -0600411 if (mmc->ddr_mode)
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900412 regs |= DWMCI_DDR_MODE;
413 else
Jaehoon Chungafc9e2b2015-01-14 17:37:53 +0900414 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chung045bdcd2014-05-16 13:59:55 +0900415
416 dwmci_writel(host, DWMCI_UHS_REG, regs);
417
Jaehoon Chung757bff42012-10-15 19:10:29 +0000418 if (host->clksel)
419 host->clksel(host);
Simon Glass691272f2016-06-12 23:30:23 -0600420#ifdef CONFIG_DM_MMC_OPS
421 return 0;
422#endif
Jaehoon Chung757bff42012-10-15 19:10:29 +0000423}
424
425static int dwmci_init(struct mmc *mmc)
426{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200427 struct dwmci_host *host = mmc->priv;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000428
Jaehoon Chung18ab6752013-11-29 20:08:57 +0900429 if (host->board_init)
430 host->board_init(host);
Rajeshwari Shinde6f0b7ca2013-10-29 12:53:13 +0530431
Jaehoon Chung757bff42012-10-15 19:10:29 +0000432 dwmci_writel(host, DWMCI_PWREN, 1);
433
434 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass1c87ffe2015-08-06 20:16:27 -0600435 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
436 return -EIO;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000437 }
438
Amar9c50e352013-04-27 11:42:54 +0530439 /* Enumerate at 400KHz */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200440 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar9c50e352013-04-27 11:42:54 +0530441
Jaehoon Chung757bff42012-10-15 19:10:29 +0000442 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF);
443 dwmci_writel(host, DWMCI_INTMASK, 0);
444
445 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF);
446
447 dwmci_writel(host, DWMCI_IDINTEN, 0);
448 dwmci_writel(host, DWMCI_BMOD, 1);
449
Simon Glass760177d2015-08-06 20:16:29 -0600450 if (!host->fifoth_val) {
451 uint32_t fifo_size;
452
453 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
454 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
455 host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) |
456 TX_WMARK(fifo_size / 2);
Amar9c50e352013-04-27 11:42:54 +0530457 }
Simon Glass760177d2015-08-06 20:16:29 -0600458 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000459
460 dwmci_writel(host, DWMCI_CLKENA, 0);
461 dwmci_writel(host, DWMCI_CLKSRC, 0);
462
463 return 0;
464}
465
Simon Glass691272f2016-06-12 23:30:23 -0600466#ifdef CONFIG_DM_MMC_OPS
467int dwmci_probe(struct udevice *dev)
468{
469 struct mmc *mmc = mmc_get_mmc_dev(dev);
470
471 return dwmci_init(mmc);
472}
473
474const struct dm_mmc_ops dm_dwmci_ops = {
475 .send_cmd = dwmci_send_cmd,
476 .set_ios = dwmci_set_ios,
477};
478
479#else
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200480static const struct mmc_ops dwmci_ops = {
481 .send_cmd = dwmci_send_cmd,
482 .set_ios = dwmci_set_ios,
483 .init = dwmci_init,
484};
Simon Glass691272f2016-06-12 23:30:23 -0600485#endif
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200486
Simon Glass5e6ff812016-05-14 14:03:07 -0600487void dwmci_setup_cfg(struct mmc_config *cfg, const char *name, int buswidth,
488 uint caps, u32 max_clk, u32 min_clk)
489{
490 cfg->name = name;
Simon Glass691272f2016-06-12 23:30:23 -0600491#ifndef CONFIG_DM_MMC_OPS
Simon Glass5e6ff812016-05-14 14:03:07 -0600492 cfg->ops = &dwmci_ops;
Simon Glass691272f2016-06-12 23:30:23 -0600493#endif
Simon Glass5e6ff812016-05-14 14:03:07 -0600494 cfg->f_min = min_clk;
495 cfg->f_max = max_clk;
496
497 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
498
499 cfg->host_caps = caps;
500
501 if (buswidth == 8) {
502 cfg->host_caps |= MMC_MODE_8BIT;
503 cfg->host_caps &= ~MMC_MODE_4BIT;
504 } else {
505 cfg->host_caps |= MMC_MODE_4BIT;
506 cfg->host_caps &= ~MMC_MODE_8BIT;
507 }
508 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
509
510 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
511}
512
513#ifdef CONFIG_BLK
514int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
515{
516 return mmc_bind(dev, mmc, cfg);
517}
518#else
Jaehoon Chung757bff42012-10-15 19:10:29 +0000519int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
520{
Simon Glass5e6ff812016-05-14 14:03:07 -0600521 dwmci_setup_cfg(&host->cfg, host->name, host->buswidth, host->caps,
522 max_clk, min_clk);
Jaehoon Chung757bff42012-10-15 19:10:29 +0000523
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200524 host->mmc = mmc_create(&host->cfg, host);
525 if (host->mmc == NULL)
526 return -1;
527
528 return 0;
Jaehoon Chung757bff42012-10-15 19:10:29 +0000529}
Simon Glass5e6ff812016-05-14 14:03:07 -0600530#endif