blob: 8551eac70185dc414d359cfc67c170043b3f2f9b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Jaehoon Chung7cf73072012-10-15 19:10:29 +00002/*
3 * (C) Copyright 2012 SAMSUNG Electronics
4 * Jaehoon Chung <jh80.chung@samsung.com>
5 * Rajeshawari Shinde <rajeshwari.s@samsung.com>
Jaehoon Chung7cf73072012-10-15 19:10:29 +00006 */
7
Alexey Brodkin55bab5e2013-12-26 15:29:07 +04008#include <bouncebuf.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Simon Glass4c9b9482015-08-06 20:16:27 -060010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000012#include <malloc.h>
Simon Glass2dd337a2015-09-02 17:24:58 -060013#include <memalign.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000014#include <mmc.h>
15#include <dwmmc.h>
Ley Foon Tanb98e8922018-12-20 17:55:41 +080016#include <wait_bit.h>
Simon Glass274e0b02020-05-10 11:39:56 -060017#include <asm/cache.h>
Simon Glassdbd79542020-05-10 11:40:11 -060018#include <linux/delay.h>
Urja Rannikko9932a012019-05-13 13:25:27 +000019#include <power/regulator.h>
Jaehoon Chung7cf73072012-10-15 19:10:29 +000020
21#define PAGE_SIZE 4096
22
Sam Protsenko6384a1f2024-08-07 22:14:15 -050023/* Internal DMA Controller (IDMAC) descriptor for 32-bit addressing mode */
24struct dwmci_idmac32 {
25 u32 des0; /* Control descriptor */
26 u32 des1; /* Buffer size */
27 u32 des2; /* Buffer physical address */
28 u32 des3; /* Next descriptor physical address */
Sam Protsenko2543c322024-08-07 22:14:08 -050029} __aligned(ARCH_DMA_MINALIGN);
30
Sam Protsenko7c991612024-08-07 22:14:16 -050031/* Internal DMA Controller (IDMAC) descriptor for 64-bit addressing mode */
32struct dwmci_idmac64 {
33 u32 des0; /* Control descriptor */
34 u32 des1; /* Reserved */
35 u32 des2; /* Buffer sizes */
36 u32 des3; /* Reserved */
37 u32 des4; /* Lower 32-bits of Buffer Address Pointer 1 */
38 u32 des5; /* Upper 32-bits of Buffer Address Pointer 1 */
39 u32 des6; /* Lower 32-bits of Next Descriptor Address */
40 u32 des7; /* Upper 32-bits of Next Descriptor Address */
41} __aligned(ARCH_DMA_MINALIGN);
42
43/* Register offsets for DW MMC blocks with 32-bit IDMAC */
44static const struct dwmci_idmac_regs dwmci_idmac_regs32 = {
45 .dbaddrl = DWMCI_DBADDR,
46 .idsts = DWMCI_IDSTS,
47 .idinten = DWMCI_IDINTEN,
48 .dscaddrl = DWMCI_DSCADDR,
49 .bufaddrl = DWMCI_BUFADDR,
50};
51
52/* Register offsets for DW MMC blocks with 64-bit IDMAC */
53static const struct dwmci_idmac_regs dwmci_idmac_regs64 = {
54 .dbaddrl = DWMCI_DBADDRL,
55 .dbaddru = DWMCI_DBADDRU,
56 .idsts = DWMCI_IDSTS64,
57 .idinten = DWMCI_IDINTEN64,
58 .dscaddrl = DWMCI_DSCADDRL,
59 .dscaddru = DWMCI_DSCADDRU,
60 .bufaddrl = DWMCI_BUFADDRL,
61 .bufaddru = DWMCI_BUFADDRU,
62};
63
Jaehoon Chung7cf73072012-10-15 19:10:29 +000064static int dwmci_wait_reset(struct dwmci_host *host, u32 value)
65{
66 unsigned long timeout = 1000;
67 u32 ctrl;
68
69 dwmci_writel(host, DWMCI_CTRL, value);
70
71 while (timeout--) {
72 ctrl = dwmci_readl(host, DWMCI_CTRL);
73 if (!(ctrl & DWMCI_RESET_ALL))
74 return 1;
75 }
76 return 0;
77}
78
Sam Protsenko6384a1f2024-08-07 22:14:15 -050079static void dwmci_set_idma_desc32(struct dwmci_idmac32 *desc, u32 control,
80 u32 buf_size, u32 buf_addr)
Jaehoon Chung7cf73072012-10-15 19:10:29 +000081{
Sam Protsenko6384a1f2024-08-07 22:14:15 -050082 phys_addr_t desc_phys = virt_to_phys(desc);
83 u32 next_desc_phys = desc_phys + sizeof(struct dwmci_idmac32);
Jaehoon Chung7cf73072012-10-15 19:10:29 +000084
Sam Protsenko6384a1f2024-08-07 22:14:15 -050085 desc->des0 = control;
86 desc->des1 = buf_size;
87 desc->des2 = buf_addr;
88 desc->des3 = next_desc_phys;
Jaehoon Chung7cf73072012-10-15 19:10:29 +000089}
90
Sam Protsenko7c991612024-08-07 22:14:16 -050091static void dwmci_set_idma_desc64(struct dwmci_idmac64 *desc, u32 control,
92 u32 buf_size, u64 buf_addr)
93{
94 phys_addr_t desc_phys = virt_to_phys(desc);
95 u64 next_desc_phys = desc_phys + sizeof(struct dwmci_idmac64);
96
97 desc->des0 = control;
98 desc->des1 = 0;
99 desc->des2 = buf_size;
100 desc->des3 = 0;
101 desc->des4 = buf_addr & 0xffffffff;
102 desc->des5 = buf_addr >> 32;
103 desc->des6 = next_desc_phys & 0xffffffff;
104 desc->des7 = next_desc_phys >> 32;
105}
106
107static void dwmci_prepare_desc(struct dwmci_host *host, struct mmc_data *data,
108 void *cur_idmac, void *bounce_buffer)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000109{
Sam Protsenko6384a1f2024-08-07 22:14:15 -0500110 struct dwmci_idmac32 *desc32 = cur_idmac;
Sam Protsenko7c991612024-08-07 22:14:16 -0500111 struct dwmci_idmac64 *desc64 = cur_idmac;
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400112 ulong data_start, data_end;
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500113 unsigned int blk_cnt, i;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000114
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500115 data_start = (ulong)cur_idmac;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000116 blk_cnt = data->blocks;
117
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500118 for (i = 0;; i++) {
Sam Protsenko6384a1f2024-08-07 22:14:15 -0500119 phys_addr_t buf_phys = virt_to_phys(bounce_buffer);
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500120 unsigned int flags, cnt;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000121
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500122 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH;
123 if (i == 0)
124 flags |= DWMCI_IDMAC_FS;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000125 if (blk_cnt <= 8) {
126 flags |= DWMCI_IDMAC_LD;
127 cnt = data->blocksize * blk_cnt;
Sam Protsenko286892e2024-08-07 22:14:19 -0500128 } else {
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000129 cnt = data->blocksize * 8;
Sam Protsenko286892e2024-08-07 22:14:19 -0500130 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000131
Sam Protsenko7c991612024-08-07 22:14:16 -0500132 if (host->dma_64bit_address) {
133 dwmci_set_idma_desc64(desc64, flags, cnt,
134 buf_phys + i * PAGE_SIZE);
135 desc64++;
136 } else {
137 dwmci_set_idma_desc32(desc32, flags, cnt,
138 buf_phys + i * PAGE_SIZE);
139 desc32++;
140 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000141
Mischa Jonkera7a60912013-07-26 16:18:40 +0200142 if (blk_cnt <= 8)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000143 break;
144 blk_cnt -= 8;
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500145 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000146
Sam Protsenko7c991612024-08-07 22:14:16 -0500147 if (host->dma_64bit_address)
148 data_end = (ulong)desc64;
149 else
150 data_end = (ulong)desc32;
Marek Vasutb6da37b2019-02-13 20:16:20 +0100151 flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN));
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500152}
153
Sam Protsenko286892e2024-08-07 22:14:19 -0500154static void dwmci_prepare_data(struct dwmci_host *host, struct mmc_data *data,
155 void *cur_idmac, void *bounce_buffer)
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500156{
Sam Protsenko7c991612024-08-07 22:14:16 -0500157 const u32 idmacl = virt_to_phys(cur_idmac) & 0xffffffff;
158 const u32 idmacu = (u64)virt_to_phys(cur_idmac) >> 32;
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500159 unsigned long ctrl;
160
161 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
162
163 /* Clear IDMAC interrupt */
Sam Protsenko7c991612024-08-07 22:14:16 -0500164 dwmci_writel(host, host->regs->idsts, 0xffffffff);
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500165
Sam Protsenko7c991612024-08-07 22:14:16 -0500166 dwmci_writel(host, host->regs->dbaddrl, idmacl);
167 if (host->dma_64bit_address)
168 dwmci_writel(host, host->regs->dbaddru, idmacu);
Sam Protsenko2e7424b2024-08-07 22:14:14 -0500169
Sam Protsenko7c991612024-08-07 22:14:16 -0500170 dwmci_prepare_desc(host, data, cur_idmac, bounce_buffer);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000171
172 ctrl = dwmci_readl(host, DWMCI_CTRL);
173 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN;
174 dwmci_writel(host, DWMCI_CTRL, ctrl);
175
176 ctrl = dwmci_readl(host, DWMCI_BMOD);
177 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN;
178 dwmci_writel(host, DWMCI_BMOD, ctrl);
179
180 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
181 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks);
182}
183
Heiko Stuebner46b7a4f2018-09-21 10:59:45 +0200184static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len)
185{
186 u32 timeout = 20000;
187
188 *len = dwmci_readl(host, DWMCI_STATUS);
189 while (--timeout && (*len & bit)) {
190 udelay(200);
191 *len = dwmci_readl(host, DWMCI_STATUS);
192 }
193
194 if (!timeout) {
195 debug("%s: FIFO underflow timeout\n", __func__);
196 return -ETIMEDOUT;
197 }
198
199 return 0;
200}
201
Marek Vasutffac5122019-03-23 03:32:24 +0100202static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size)
203{
204 unsigned int timeout;
205
Kever Yang4889d832019-08-29 15:42:41 +0800206 timeout = size * 8; /* counting in bits */
207 timeout *= 10; /* wait 10 times as long */
Marek Vasutffac5122019-03-23 03:32:24 +0100208 timeout /= mmc->clock;
209 timeout /= mmc->bus_width;
210 timeout /= mmc->ddr_mode ? 2 : 1;
Kever Yang4889d832019-08-29 15:42:41 +0800211 timeout *= 1000; /* counting in msec */
Marek Vasutffac5122019-03-23 03:32:24 +0100212 timeout = (timeout < 1000) ? 1000 : timeout;
213
214 return timeout;
215}
216
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500217static int dwmci_data_transfer_fifo(struct dwmci_host *host,
218 struct mmc_data *data, u32 mask)
huang linf9836762015-11-17 14:20:21 +0800219{
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500220 const u32 int_rx = mask & (DWMCI_INTMSK_RXDR | DWMCI_INTMSK_DTO);
221 const u32 int_tx = mask & DWMCI_INTMSK_TXDR;
huang linf9836762015-11-17 14:20:21 +0800222 int ret = 0;
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500223 u32 len = 0, size, i;
224 u32 *buf;
huang lin50b73752015-11-17 14:20:22 +0800225
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500226 size = (data->blocksize * data->blocks) / 4;
227 if (!host->fifo_mode || !size)
228 return 0;
229
huang lin50b73752015-11-17 14:20:22 +0800230 if (data->flags == MMC_DATA_READ)
231 buf = (unsigned int *)data->dest;
232 else
233 buf = (unsigned int *)data->src;
huang linf9836762015-11-17 14:20:21 +0800234
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500235 if (data->flags == MMC_DATA_READ && int_rx) {
236 dwmci_writel(host, DWMCI_RINTSTS, int_rx);
237 while (size) {
238 ret = dwmci_fifo_ready(host, DWMCI_FIFO_EMPTY, &len);
239 if (ret < 0)
240 break;
241
242 len = (len >> DWMCI_FIFO_SHIFT) & DWMCI_FIFO_MASK;
243 len = min(size, len);
244 for (i = 0; i < len; i++)
245 *buf++ = dwmci_readl(host, DWMCI_DATA);
246 size = size > len ? (size - len) : 0;
247 }
248 } else if (data->flags == MMC_DATA_WRITE && int_tx) {
249 while (size) {
250 ret = dwmci_fifo_ready(host, DWMCI_FIFO_FULL, &len);
251 if (ret < 0)
252 break;
253
Sam Protsenko751fdf12024-08-07 22:14:17 -0500254 len = host->fifo_depth - ((len >> DWMCI_FIFO_SHIFT) &
255 DWMCI_FIFO_MASK);
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500256 len = min(size, len);
257 for (i = 0; i < len; i++)
258 dwmci_writel(host, DWMCI_DATA, *buf++);
259 size = size > len ? (size - len) : 0;
260 }
261 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_TXDR);
262 }
263
264 return ret;
265}
Marek Vasutffac5122019-03-23 03:32:24 +0100266
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500267static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data)
268{
269 struct mmc *mmc = host->mmc;
270 int ret = 0;
271 u32 timeout, mask, size;
272 ulong start = get_timer(0);
273
274 size = data->blocksize * data->blocks;
275 timeout = dwmci_get_timeout(mmc, size);
Marek Vasutffac5122019-03-23 03:32:24 +0100276
huang linf9836762015-11-17 14:20:21 +0800277 for (;;) {
278 mask = dwmci_readl(host, DWMCI_RINTSTS);
Sam Protsenko286892e2024-08-07 22:14:19 -0500279 /* Error during data transfer */
huang linf9836762015-11-17 14:20:21 +0800280 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) {
281 debug("%s: DATA ERROR!\n", __func__);
282 ret = -EINVAL;
283 break;
284 }
285
Sam Protsenkoc4cffe82024-08-07 22:14:12 -0500286 ret = dwmci_data_transfer_fifo(host, data, mask);
huang lin50b73752015-11-17 14:20:22 +0800287
Sam Protsenko286892e2024-08-07 22:14:19 -0500288 /* Data arrived correctly */
huang linf9836762015-11-17 14:20:21 +0800289 if (mask & DWMCI_INTMSK_DTO) {
290 ret = 0;
291 break;
292 }
293
Sam Protsenko286892e2024-08-07 22:14:19 -0500294 /* Check for timeout */
huang linf9836762015-11-17 14:20:21 +0800295 if (get_timer(start) > timeout) {
Sam Protsenko286892e2024-08-07 22:14:19 -0500296 debug("%s: Timeout waiting for data!\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900297 ret = -ETIMEDOUT;
huang linf9836762015-11-17 14:20:21 +0800298 break;
299 }
300 }
301
302 dwmci_writel(host, DWMCI_RINTSTS, mask);
303
304 return ret;
305}
306
Sam Protsenko3350c202024-08-07 22:14:13 -0500307static int dwmci_dma_transfer(struct dwmci_host *host, uint flags,
308 struct bounce_buffer *bbstate)
309{
310 int ret;
311 u32 mask, ctrl;
312
313 if (flags == MMC_DATA_READ)
314 mask = DWMCI_IDINTEN_RI;
315 else
316 mask = DWMCI_IDINTEN_TI;
317
Sam Protsenko286892e2024-08-07 22:14:19 -0500318 ret = wait_for_bit_le32(host->ioaddr + host->regs->idsts, mask, true,
319 1000, false);
Sam Protsenko3350c202024-08-07 22:14:13 -0500320 if (ret)
321 debug("%s: DWMCI_IDINTEN mask 0x%x timeout\n", __func__, mask);
322
323 /* Clear interrupts */
Sam Protsenko7c991612024-08-07 22:14:16 -0500324 dwmci_writel(host, host->regs->idsts, DWMCI_IDINTEN_MASK);
Sam Protsenko3350c202024-08-07 22:14:13 -0500325
326 ctrl = dwmci_readl(host, DWMCI_CTRL);
327 ctrl &= ~DWMCI_DMA_EN;
328 dwmci_writel(host, DWMCI_CTRL, ctrl);
329
330 bounce_buffer_stop(bbstate);
331 return ret;
332}
333
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000334static int dwmci_set_transfer_mode(struct dwmci_host *host,
Sam Protsenko286892e2024-08-07 22:14:19 -0500335 struct mmc_data *data)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000336{
337 unsigned long mode;
338
339 mode = DWMCI_CMD_DATA_EXP;
340 if (data->flags & MMC_DATA_WRITE)
341 mode |= DWMCI_CMD_RW;
342
343 return mode;
344}
345
Sam Protsenkocf812042024-08-07 22:14:09 -0500346static void dwmci_wait_while_busy(struct dwmci_host *host, struct mmc_cmd *cmd)
347{
348 unsigned int timeout = 500; /* msec */
349 ulong start;
350
351 start = get_timer(0);
352 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) {
353 if (get_timer(start) > timeout) {
354 debug("%s: Timeout on data busy, continue anyway\n",
355 __func__);
356 break;
357 }
358 }
359}
360
Sam Protsenko7c991612024-08-07 22:14:16 -0500361static int dwmci_send_cmd_common(struct dwmci_host *host, struct mmc_cmd *cmd,
362 struct mmc_data *data, void *cur_idmac)
Simon Glassff5c1b72016-06-12 23:30:23 -0600363{
Sam Protsenko7c991612024-08-07 22:14:16 -0500364 int ret, flags = 0, i;
Alexander Graf61c2a662016-03-04 01:09:52 +0100365 u32 retry = 100000;
Sam Protsenko3350c202024-08-07 22:14:13 -0500366 u32 mask;
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400367 struct bounce_buffer bbstate;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000368
Sam Protsenkocf812042024-08-07 22:14:09 -0500369 dwmci_wait_while_busy(host, cmd);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000370 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL);
371
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400372 if (data) {
huang lin50b73752015-11-17 14:20:22 +0800373 if (host->fifo_mode) {
374 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize);
375 dwmci_writel(host, DWMCI_BYTCNT,
376 data->blocksize * data->blocks);
377 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400378 } else {
huang lin50b73752015-11-17 14:20:22 +0800379 if (data->flags == MMC_DATA_READ) {
Marek Vasut72d37b62019-03-23 18:45:27 +0100380 ret = bounce_buffer_start(&bbstate,
Sam Protsenko286892e2024-08-07 22:14:19 -0500381 (void *)data->dest,
huang lin50b73752015-11-17 14:20:22 +0800382 data->blocksize *
383 data->blocks, GEN_BB_WRITE);
384 } else {
Marek Vasut72d37b62019-03-23 18:45:27 +0100385 ret = bounce_buffer_start(&bbstate,
Sam Protsenko286892e2024-08-07 22:14:19 -0500386 (void *)data->src,
huang lin50b73752015-11-17 14:20:22 +0800387 data->blocksize *
388 data->blocks, GEN_BB_READ);
389 }
Marek Vasut72d37b62019-03-23 18:45:27 +0100390
391 if (ret)
392 return ret;
393
huang lin50b73752015-11-17 14:20:22 +0800394 dwmci_prepare_data(host, data, cur_idmac,
395 bbstate.bounce_buffer);
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400396 }
Alexey Brodkin55bab5e2013-12-26 15:29:07 +0400397 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000398
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000399 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg);
400
401 if (data)
402 flags = dwmci_set_transfer_mode(host, data);
403
404 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY))
John Keepingfeb7fa32021-12-07 16:09:35 +0000405 return -EBUSY;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000406
407 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
408 flags |= DWMCI_CMD_ABORT_STOP;
409 else
410 flags |= DWMCI_CMD_PRV_DAT_WAIT;
411
412 if (cmd->resp_type & MMC_RSP_PRESENT) {
413 flags |= DWMCI_CMD_RESP_EXP;
414 if (cmd->resp_type & MMC_RSP_136)
415 flags |= DWMCI_CMD_RESP_LENGTH;
416 }
417
418 if (cmd->resp_type & MMC_RSP_CRC)
419 flags |= DWMCI_CMD_CHECK_CRC;
420
Sam Protsenko286892e2024-08-07 22:14:19 -0500421 flags |= cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000422
Sam Protsenko286892e2024-08-07 22:14:19 -0500423 debug("Sending CMD%d\n", cmd->cmdidx);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000424
425 dwmci_writel(host, DWMCI_CMD, flags);
426
427 for (i = 0; i < retry; i++) {
428 mask = dwmci_readl(host, DWMCI_RINTSTS);
429 if (mask & DWMCI_INTMSK_CDONE) {
430 if (!data)
431 dwmci_writel(host, DWMCI_RINTSTS, mask);
432 break;
433 }
434 }
435
Pavel Macheka425f5d2014-09-05 12:49:48 +0200436 if (i == retry) {
Sam Protsenko286892e2024-08-07 22:14:19 -0500437 debug("%s: Timeout\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900438 return -ETIMEDOUT;
Pavel Macheka425f5d2014-09-05 12:49:48 +0200439 }
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000440
441 if (mask & DWMCI_INTMSK_RTO) {
Pavel Macheka425f5d2014-09-05 12:49:48 +0200442 /*
443 * Timeout here is not necessarily fatal. (e)MMC cards
444 * will splat here when they receive CMD55 as they do
445 * not support this command and that is exactly the way
446 * to tell them apart from SD cards. Thus, this output
447 * below shall be debug(). eMMC cards also do not favor
448 * CMD8, please keep that in mind.
449 */
Sam Protsenko286892e2024-08-07 22:14:19 -0500450 debug("%s: Response Timeout\n", __func__);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900451 return -ETIMEDOUT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000452 } else if (mask & DWMCI_INTMSK_RE) {
Sam Protsenko286892e2024-08-07 22:14:19 -0500453 debug("%s: Response Error\n", __func__);
Simon Glass4c9b9482015-08-06 20:16:27 -0600454 return -EIO;
Marek Vasuta6d91992018-11-06 23:42:11 +0100455 } else if ((cmd->resp_type & MMC_RSP_CRC) &&
456 (mask & DWMCI_INTMSK_RCRC)) {
Sam Protsenko286892e2024-08-07 22:14:19 -0500457 debug("%s: Response CRC Error\n", __func__);
Marek Vasuta6d91992018-11-06 23:42:11 +0100458 return -EIO;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000459 }
460
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000461 if (cmd->resp_type & MMC_RSP_PRESENT) {
462 if (cmd->resp_type & MMC_RSP_136) {
463 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3);
464 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2);
465 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1);
466 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0);
467 } else {
468 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0);
469 }
470 }
471
472 if (data) {
huang lin50b73752015-11-17 14:20:22 +0800473 ret = dwmci_data_transfer(host, data);
Sam Protsenko3350c202024-08-07 22:14:13 -0500474 if (!host->fifo_mode)
475 ret = dwmci_dma_transfer(host, data->flags, &bbstate);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000476 }
477
478 udelay(100);
479
Marek Vasut81e093f2015-07-27 22:39:38 +0200480 return ret;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000481}
482
Sam Protsenko7c991612024-08-07 22:14:16 -0500483#ifdef CONFIG_DM_MMC
484static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
485 struct mmc_data *data)
486{
487 struct mmc *mmc = mmc_get_mmc_dev(dev);
488#else
489static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
490 struct mmc_data *data)
491{
492#endif
493 struct dwmci_host *host = mmc->priv;
494 const size_t buf_size = data ? DIV_ROUND_UP(data->blocks, 8) : 0;
495
496 if (host->dma_64bit_address) {
497 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac64, idmac, buf_size);
498 return dwmci_send_cmd_common(host, cmd, data, idmac);
499 } else {
500 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac32, idmac, buf_size);
501 return dwmci_send_cmd_common(host, cmd, data, idmac);
502 }
503}
504
Sam Protsenkofd5387f2024-08-07 22:14:11 -0500505static int dwmci_control_clken(struct dwmci_host *host, bool on)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000506{
Sam Protsenkofd5387f2024-08-07 22:14:11 -0500507 const u32 val = on ? DWMCI_CLKEN_ENABLE | DWMCI_CLKEN_LOW_PWR : 0;
508 const u32 cmd_only_clk = DWMCI_CMD_PRV_DAT_WAIT | DWMCI_CMD_UPD_CLK;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000509 int timeout = 10000;
Sam Protsenkofd5387f2024-08-07 22:14:11 -0500510 u32 status;
511
512 dwmci_writel(host, DWMCI_CLKENA, val);
513
514 /* Inform CIU */
515 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_START | cmd_only_clk);
516 do {
517 status = dwmci_readl(host, DWMCI_CMD);
518 if (timeout-- < 0) {
519 debug("%s: Timeout!\n", __func__);
520 return -ETIMEDOUT;
521 }
522 } while (status & DWMCI_CMD_START);
523
524 return 0;
525}
526
527/*
528 * Update the clock divider.
529 *
530 * To prevent a clock glitch keep the clock stopped during the update of
531 * clock divider and clock source.
532 */
533static int dwmci_update_div(struct dwmci_host *host, u32 div)
534{
535 int ret;
536
537 /* Disable clock */
538 ret = dwmci_control_clken(host, false);
539 if (ret)
540 return ret;
541
542 /* Set clock to desired speed */
543 dwmci_writel(host, DWMCI_CLKDIV, div);
544 dwmci_writel(host, DWMCI_CLKSRC, 0);
545
546 /* Enable clock */
547 return dwmci_control_clken(host, true);
548}
549
550static int dwmci_setup_bus(struct dwmci_host *host, u32 freq)
551{
552 u32 div;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000553 unsigned long sclk;
Sam Protsenkofd5387f2024-08-07 22:14:11 -0500554 int ret;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000555
Sam Protsenko286892e2024-08-07 22:14:19 -0500556 if (freq == host->clock || freq == 0)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000557 return 0;
Sam Protsenko286892e2024-08-07 22:14:19 -0500558
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000559 /*
Sam Protsenko286892e2024-08-07 22:14:19 -0500560 * If host->get_mmc_clk isn't defined, then assume that host->bus_hz is
561 * source clock value. host->bus_hz should be set by user.
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000562 */
Sam Protsenko286892e2024-08-07 22:14:19 -0500563 if (host->get_mmc_clk) {
Simon Glasseff76682015-08-30 16:55:15 -0600564 sclk = host->get_mmc_clk(host, freq);
Sam Protsenko286892e2024-08-07 22:14:19 -0500565 } else if (host->bus_hz) {
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000566 sclk = host->bus_hz;
Sam Protsenko286892e2024-08-07 22:14:19 -0500567 } else {
568 debug("%s: Didn't get source clock value\n", __func__);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000569 return -EINVAL;
570 }
571
Chin Liang See4cfff952014-06-10 01:26:52 -0500572 if (sclk == freq)
Sam Protsenko286892e2024-08-07 22:14:19 -0500573 div = 0; /* bypass mode */
Chin Liang See4cfff952014-06-10 01:26:52 -0500574 else
575 div = DIV_ROUND_UP(sclk, 2 * freq);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000576
Sam Protsenkofd5387f2024-08-07 22:14:11 -0500577 ret = dwmci_update_div(host, div);
578 if (ret)
579 return ret;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000580
581 host->clock = freq;
582
583 return 0;
584}
585
Simon Glasseba48f92017-07-29 11:35:31 -0600586#ifdef CONFIG_DM_MMC
Jaehoon Chungad220ac2016-06-28 15:52:21 +0900587static int dwmci_set_ios(struct udevice *dev)
Simon Glassff5c1b72016-06-12 23:30:23 -0600588{
589 struct mmc *mmc = mmc_get_mmc_dev(dev);
590#else
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900591static int dwmci_set_ios(struct mmc *mmc)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000592{
Simon Glassff5c1b72016-06-12 23:30:23 -0600593#endif
Jaehoon Chunge8672942014-05-16 13:59:55 +0900594 struct dwmci_host *host = (struct dwmci_host *)mmc->priv;
595 u32 ctype, regs;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000596
Sam Protsenko286892e2024-08-07 22:14:19 -0500597 debug("Bus width = %d, clock: %d\n", mmc->bus_width, mmc->clock);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000598
599 dwmci_setup_bus(host, mmc->clock);
600 switch (mmc->bus_width) {
601 case 8:
602 ctype = DWMCI_CTYPE_8BIT;
603 break;
604 case 4:
605 ctype = DWMCI_CTYPE_4BIT;
606 break;
607 default:
608 ctype = DWMCI_CTYPE_1BIT;
609 break;
610 }
611
612 dwmci_writel(host, DWMCI_CTYPE, ctype);
613
Jaehoon Chunge8672942014-05-16 13:59:55 +0900614 regs = dwmci_readl(host, DWMCI_UHS_REG);
Andrew Gabbasov54c0e222014-12-01 06:59:12 -0600615 if (mmc->ddr_mode)
Jaehoon Chunge8672942014-05-16 13:59:55 +0900616 regs |= DWMCI_DDR_MODE;
617 else
Jaehoon Chung401fc502015-01-14 17:37:53 +0900618 regs &= ~DWMCI_DDR_MODE;
Jaehoon Chunge8672942014-05-16 13:59:55 +0900619
620 dwmci_writel(host, DWMCI_UHS_REG, regs);
621
Siew Chin Limc51e7e12020-12-24 18:21:03 +0800622 if (host->clksel) {
623 int ret;
624
625 ret = host->clksel(host);
626 if (ret)
627 return ret;
628 }
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900629
Urja Rannikko9932a012019-05-13 13:25:27 +0000630#if CONFIG_IS_ENABLED(DM_REGULATOR)
631 if (mmc->vqmmc_supply) {
632 int ret;
633
Jonas Karlmana117d612023-07-19 21:21:00 +0000634 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false);
635 if (ret)
636 return ret;
637
Urja Rannikko9932a012019-05-13 13:25:27 +0000638 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
639 regulator_set_value(mmc->vqmmc_supply, 1800000);
640 else
641 regulator_set_value(mmc->vqmmc_supply, 3300000);
642
643 ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true);
644 if (ret)
645 return ret;
646 }
647#endif
648
Simon Glassff5c1b72016-06-12 23:30:23 -0600649 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000650}
651
Sam Protsenko0845f4f2024-08-07 22:14:10 -0500652static void dwmci_init_fifo(struct dwmci_host *host)
653{
Sam Protsenko751fdf12024-08-07 22:14:17 -0500654 u32 fifo_thr, fifoth_val;
655
656 if (!host->fifo_depth) {
Sam Protsenko0845f4f2024-08-07 22:14:10 -0500657 u32 fifo_size;
658
Sam Protsenko751fdf12024-08-07 22:14:17 -0500659 /*
660 * Automatically detect FIFO depth from FIFOTH register.
661 * Power-on value of RX_WMark is FIFO_DEPTH-1.
662 */
Sam Protsenko0845f4f2024-08-07 22:14:10 -0500663 fifo_size = dwmci_readl(host, DWMCI_FIFOTH);
664 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1;
Sam Protsenko751fdf12024-08-07 22:14:17 -0500665 host->fifo_depth = fifo_size;
Sam Protsenko0845f4f2024-08-07 22:14:10 -0500666 }
667
Sam Protsenko751fdf12024-08-07 22:14:17 -0500668 fifo_thr = host->fifo_depth / 2;
669 fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_thr - 1) | TX_WMARK(fifo_thr);
670 dwmci_writel(host, DWMCI_FIFOTH, fifoth_val);
Sam Protsenko0845f4f2024-08-07 22:14:10 -0500671}
672
Sam Protsenko7c991612024-08-07 22:14:16 -0500673static void dwmci_init_dma(struct dwmci_host *host)
674{
675 int addr_config;
676
677 if (host->fifo_mode)
678 return;
679
680 addr_config = (dwmci_readl(host, DWMCI_HCON) >> 27) & 0x1;
681 if (addr_config == 1) {
682 host->dma_64bit_address = true;
683 host->regs = &dwmci_idmac_regs64;
684 debug("%s: IDMAC supports 64-bit address mode\n", __func__);
685 } else {
686 host->dma_64bit_address = false;
687 host->regs = &dwmci_idmac_regs32;
688 debug("%s: IDMAC supports 32-bit address mode\n", __func__);
689 }
690
691 dwmci_writel(host, host->regs->idinten, DWMCI_IDINTEN_MASK);
692}
693
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000694static int dwmci_init(struct mmc *mmc)
695{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200696 struct dwmci_host *host = mmc->priv;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000697
Jaehoon Chung42f81a82013-11-29 20:08:57 +0900698 if (host->board_init)
699 host->board_init(host);
Rajeshwari Shinde70163092013-10-29 12:53:13 +0530700
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000701 dwmci_writel(host, DWMCI_PWREN, 1);
702
703 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) {
Simon Glass4c9b9482015-08-06 20:16:27 -0600704 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__);
705 return -EIO;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000706 }
707
Amar902664c2013-04-27 11:42:54 +0530708 /* Enumerate at 400KHz */
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200709 dwmci_setup_bus(host, mmc->cfg->f_min);
Amar902664c2013-04-27 11:42:54 +0530710
Sam Protsenko286892e2024-08-07 22:14:19 -0500711 dwmci_writel(host, DWMCI_RINTSTS, 0xffffffff);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000712 dwmci_writel(host, DWMCI_INTMASK, 0);
713
Sam Protsenko286892e2024-08-07 22:14:19 -0500714 dwmci_writel(host, DWMCI_TMOUT, 0xffffffff);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000715
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000716 dwmci_writel(host, DWMCI_BMOD, 1);
Sam Protsenko0845f4f2024-08-07 22:14:10 -0500717 dwmci_init_fifo(host);
Sam Protsenko7c991612024-08-07 22:14:16 -0500718 dwmci_init_dma(host);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000719
720 dwmci_writel(host, DWMCI_CLKENA, 0);
721 dwmci_writel(host, DWMCI_CLKSRC, 0);
722
723 return 0;
724}
725
Simon Glasseba48f92017-07-29 11:35:31 -0600726#ifdef CONFIG_DM_MMC
Simon Glassff5c1b72016-06-12 23:30:23 -0600727int dwmci_probe(struct udevice *dev)
728{
729 struct mmc *mmc = mmc_get_mmc_dev(dev);
730
731 return dwmci_init(mmc);
732}
733
734const struct dm_mmc_ops dm_dwmci_ops = {
735 .send_cmd = dwmci_send_cmd,
736 .set_ios = dwmci_set_ios,
737};
738
739#else
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200740static const struct mmc_ops dwmci_ops = {
741 .send_cmd = dwmci_send_cmd,
742 .set_ios = dwmci_set_ios,
743 .init = dwmci_init,
744};
Simon Glassff5c1b72016-06-12 23:30:23 -0600745#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200746
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900747void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host,
Sam Protsenko286892e2024-08-07 22:14:19 -0500748 u32 max_clk, u32 min_clk)
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000749{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900750 cfg->name = host->name;
Simon Glasseba48f92017-07-29 11:35:31 -0600751#ifndef CONFIG_DM_MMC
Simon Glass82682542016-05-14 14:03:07 -0600752 cfg->ops = &dwmci_ops;
Simon Glassff5c1b72016-06-12 23:30:23 -0600753#endif
Simon Glass82682542016-05-14 14:03:07 -0600754 cfg->f_min = min_clk;
755 cfg->f_max = max_clk;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000756
Simon Glass82682542016-05-14 14:03:07 -0600757 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000758
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900759 cfg->host_caps = host->caps;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000760
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900761 if (host->buswidth == 8) {
Simon Glass82682542016-05-14 14:03:07 -0600762 cfg->host_caps |= MMC_MODE_8BIT;
763 cfg->host_caps &= ~MMC_MODE_4BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000764 } else {
Simon Glass82682542016-05-14 14:03:07 -0600765 cfg->host_caps |= MMC_MODE_4BIT;
766 cfg->host_caps &= ~MMC_MODE_8BIT;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000767 }
Simon Glass82682542016-05-14 14:03:07 -0600768 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
769
770 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
771}
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200772
Simon Glass82682542016-05-14 14:03:07 -0600773#ifdef CONFIG_BLK
774int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg)
775{
776 return mmc_bind(dev, mmc, cfg);
777}
778#else
779int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk)
780{
Jaehoon Chungbf819d02016-09-23 19:13:16 +0900781 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk);
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000782
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200783 host->mmc = mmc_create(&host->cfg, host);
Sam Protsenko286892e2024-08-07 22:14:19 -0500784 if (!host->mmc)
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200785 return -1;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000786
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200787 return 0;
Jaehoon Chung7cf73072012-10-15 19:10:29 +0000788}
Simon Glass82682542016-05-14 14:03:07 -0600789#endif