blob: 1dc13db9eaaca8f8cd7504e7f4fb9fa0393bfcd0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutcb0b6b02018-04-13 23:51:33 +02002/*
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Marek Vasutcb0b6b02018-04-13 23:51:33 +02005 */
6
7#include <common.h>
8#include <clk.h>
Simon Glass1eb69ae2019-11-14 12:57:39 -07009#include <cpu_func.h>
Marek Vasutcb0b6b02018-04-13 23:51:33 +020010#include <fdtdec.h>
11#include <mmc.h>
12#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <dm/device_compat.h>
Marek Vasutcb0b6b02018-04-13 23:51:33 +020014#include <dm/pinctrl.h>
15#include <linux/compat.h>
Masahiro Yamada9d86b892020-02-14 16:40:19 +090016#include <linux/dma-mapping.h>
Marek Vasutcb0b6b02018-04-13 23:51:33 +020017#include <linux/io.h>
18#include <linux/sizes.h>
19#include <power/regulator.h>
20#include <asm/unaligned.h>
21
22#include "tmio-common.h"
23
24DECLARE_GLOBAL_DATA_PTR;
25
26static u64 tmio_sd_readq(struct tmio_sd_priv *priv, unsigned int reg)
27{
28 return readq(priv->regbase + (reg << 1));
29}
30
31static void tmio_sd_writeq(struct tmio_sd_priv *priv,
32 u64 val, unsigned int reg)
33{
34 writeq(val, priv->regbase + (reg << 1));
35}
36
37static u16 tmio_sd_readw(struct tmio_sd_priv *priv, unsigned int reg)
38{
39 return readw(priv->regbase + (reg >> 1));
40}
41
42static void tmio_sd_writew(struct tmio_sd_priv *priv,
43 u16 val, unsigned int reg)
44{
45 writew(val, priv->regbase + (reg >> 1));
46}
47
48u32 tmio_sd_readl(struct tmio_sd_priv *priv, unsigned int reg)
49{
50 u32 val;
51
52 if (priv->caps & TMIO_SD_CAP_64BIT)
53 return readl(priv->regbase + (reg << 1));
54 else if (priv->caps & TMIO_SD_CAP_16BIT) {
55 val = readw(priv->regbase + (reg >> 1)) & 0xffff;
56 if ((reg == TMIO_SD_RSP10) || (reg == TMIO_SD_RSP32) ||
57 (reg == TMIO_SD_RSP54) || (reg == TMIO_SD_RSP76)) {
58 val |= readw(priv->regbase + (reg >> 1) + 2) << 16;
59 }
60 return val;
61 } else
62 return readl(priv->regbase + reg);
63}
64
65void tmio_sd_writel(struct tmio_sd_priv *priv,
66 u32 val, unsigned int reg)
67{
68 if (priv->caps & TMIO_SD_CAP_64BIT)
69 writel(val, priv->regbase + (reg << 1));
70 else if (priv->caps & TMIO_SD_CAP_16BIT) {
71 writew(val & 0xffff, priv->regbase + (reg >> 1));
72 if (reg == TMIO_SD_INFO1 || reg == TMIO_SD_INFO1_MASK ||
73 reg == TMIO_SD_INFO2 || reg == TMIO_SD_INFO2_MASK ||
74 reg == TMIO_SD_ARG)
75 writew(val >> 16, priv->regbase + (reg >> 1) + 2);
76 } else
77 writel(val, priv->regbase + reg);
78}
79
Marek Vasut33633eb2018-10-30 22:05:54 +010080static int tmio_sd_check_error(struct udevice *dev, struct mmc_cmd *cmd)
Marek Vasutcb0b6b02018-04-13 23:51:33 +020081{
82 struct tmio_sd_priv *priv = dev_get_priv(dev);
83 u32 info2 = tmio_sd_readl(priv, TMIO_SD_INFO2);
84
85 if (info2 & TMIO_SD_INFO2_ERR_RTO) {
86 /*
87 * TIMEOUT must be returned for unsupported command. Do not
88 * display error log since this might be a part of sequence to
89 * distinguish between SD and MMC.
90 */
91 return -ETIMEDOUT;
92 }
93
94 if (info2 & TMIO_SD_INFO2_ERR_TO) {
95 dev_err(dev, "timeout error\n");
96 return -ETIMEDOUT;
97 }
98
99 if (info2 & (TMIO_SD_INFO2_ERR_END | TMIO_SD_INFO2_ERR_CRC |
100 TMIO_SD_INFO2_ERR_IDX)) {
Marek Vasut33633eb2018-10-30 22:05:54 +0100101 if ((cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK) &&
102 (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200))
103 dev_err(dev, "communication out of sync\n");
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200104 return -EILSEQ;
105 }
106
107 if (info2 & (TMIO_SD_INFO2_ERR_ILA | TMIO_SD_INFO2_ERR_ILR |
108 TMIO_SD_INFO2_ERR_ILW)) {
109 dev_err(dev, "illegal access\n");
110 return -EIO;
111 }
112
113 return 0;
114}
115
Marek Vasut33633eb2018-10-30 22:05:54 +0100116static int tmio_sd_wait_for_irq(struct udevice *dev, struct mmc_cmd *cmd,
117 unsigned int reg, u32 flag)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200118{
119 struct tmio_sd_priv *priv = dev_get_priv(dev);
120 long wait = 1000000;
121 int ret;
122
123 while (!(tmio_sd_readl(priv, reg) & flag)) {
124 if (wait-- < 0) {
125 dev_err(dev, "timeout\n");
126 return -ETIMEDOUT;
127 }
128
Marek Vasut33633eb2018-10-30 22:05:54 +0100129 ret = tmio_sd_check_error(dev, cmd);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200130 if (ret)
131 return ret;
132
133 udelay(1);
134 }
135
136 return 0;
137}
138
139#define tmio_pio_read_fifo(__width, __suffix) \
140static void tmio_pio_read_fifo_##__width(struct tmio_sd_priv *priv, \
141 char *pbuf, uint blksz) \
142{ \
143 u##__width *buf = (u##__width *)pbuf; \
144 int i; \
145 \
146 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
147 for (i = 0; i < blksz / ((__width) / 8); i++) { \
148 *buf++ = tmio_sd_read##__suffix(priv, \
149 TMIO_SD_BUF); \
150 } \
151 } else { \
152 for (i = 0; i < blksz / ((__width) / 8); i++) { \
153 u##__width data; \
154 data = tmio_sd_read##__suffix(priv, \
155 TMIO_SD_BUF); \
156 put_unaligned(data, buf++); \
157 } \
158 } \
159}
160
161tmio_pio_read_fifo(64, q)
162tmio_pio_read_fifo(32, l)
163tmio_pio_read_fifo(16, w)
164
Marek Vasut33633eb2018-10-30 22:05:54 +0100165static int tmio_sd_pio_read_one_block(struct udevice *dev, struct mmc_cmd *cmd,
166 char *pbuf, uint blocksize)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200167{
168 struct tmio_sd_priv *priv = dev_get_priv(dev);
169 int ret;
170
171 /* wait until the buffer is filled with data */
Marek Vasut33633eb2018-10-30 22:05:54 +0100172 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
173 TMIO_SD_INFO2_BRE);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200174 if (ret)
175 return ret;
176
177 /*
178 * Clear the status flag _before_ read the buffer out because
179 * TMIO_SD_INFO2_BRE is edge-triggered, not level-triggered.
180 */
181 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
182
183 if (priv->caps & TMIO_SD_CAP_64BIT)
184 tmio_pio_read_fifo_64(priv, pbuf, blocksize);
185 else if (priv->caps & TMIO_SD_CAP_16BIT)
186 tmio_pio_read_fifo_16(priv, pbuf, blocksize);
187 else
188 tmio_pio_read_fifo_32(priv, pbuf, blocksize);
189
190 return 0;
191}
192
193#define tmio_pio_write_fifo(__width, __suffix) \
194static void tmio_pio_write_fifo_##__width(struct tmio_sd_priv *priv, \
195 const char *pbuf, uint blksz)\
196{ \
197 const u##__width *buf = (const u##__width *)pbuf; \
198 int i; \
199 \
200 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
201 for (i = 0; i < blksz / ((__width) / 8); i++) { \
202 tmio_sd_write##__suffix(priv, *buf++, \
203 TMIO_SD_BUF); \
204 } \
205 } else { \
206 for (i = 0; i < blksz / ((__width) / 8); i++) { \
207 u##__width data = get_unaligned(buf++); \
208 tmio_sd_write##__suffix(priv, data, \
209 TMIO_SD_BUF); \
210 } \
211 } \
212}
213
214tmio_pio_write_fifo(64, q)
215tmio_pio_write_fifo(32, l)
216tmio_pio_write_fifo(16, w)
217
Marek Vasut33633eb2018-10-30 22:05:54 +0100218static int tmio_sd_pio_write_one_block(struct udevice *dev, struct mmc_cmd *cmd,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200219 const char *pbuf, uint blocksize)
220{
221 struct tmio_sd_priv *priv = dev_get_priv(dev);
222 int ret;
223
224 /* wait until the buffer becomes empty */
Marek Vasut33633eb2018-10-30 22:05:54 +0100225 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
226 TMIO_SD_INFO2_BWE);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200227 if (ret)
228 return ret;
229
230 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
231
232 if (priv->caps & TMIO_SD_CAP_64BIT)
233 tmio_pio_write_fifo_64(priv, pbuf, blocksize);
234 else if (priv->caps & TMIO_SD_CAP_16BIT)
235 tmio_pio_write_fifo_16(priv, pbuf, blocksize);
236 else
237 tmio_pio_write_fifo_32(priv, pbuf, blocksize);
238
239 return 0;
240}
241
Marek Vasut33633eb2018-10-30 22:05:54 +0100242static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_cmd *cmd,
243 struct mmc_data *data)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200244{
245 const char *src = data->src;
246 char *dest = data->dest;
247 int i, ret;
248
249 for (i = 0; i < data->blocks; i++) {
250 if (data->flags & MMC_DATA_READ)
Marek Vasut33633eb2018-10-30 22:05:54 +0100251 ret = tmio_sd_pio_read_one_block(dev, cmd, dest,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200252 data->blocksize);
253 else
Marek Vasut33633eb2018-10-30 22:05:54 +0100254 ret = tmio_sd_pio_write_one_block(dev, cmd, src,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200255 data->blocksize);
256 if (ret)
257 return ret;
258
259 if (data->flags & MMC_DATA_READ)
260 dest += data->blocksize;
261 else
262 src += data->blocksize;
263 }
264
265 return 0;
266}
267
268static void tmio_sd_dma_start(struct tmio_sd_priv *priv,
269 dma_addr_t dma_addr)
270{
271 u32 tmp;
272
273 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO1);
274 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO2);
275
276 /* enable DMA */
277 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
278 tmp |= TMIO_SD_EXTMODE_DMA_EN;
279 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
280
281 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_L);
282
283 /* suppress the warning "right shift count >= width of type" */
284 dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
285
286 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_H);
287
288 tmio_sd_writel(priv, TMIO_SD_DMA_CTL_START, TMIO_SD_DMA_CTL);
289}
290
291static int tmio_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
292 unsigned int blocks)
293{
294 struct tmio_sd_priv *priv = dev_get_priv(dev);
295 long wait = 1000000 + 10 * blocks;
296
297 while (!(tmio_sd_readl(priv, TMIO_SD_DMA_INFO1) & flag)) {
298 if (wait-- < 0) {
299 dev_err(dev, "timeout during DMA\n");
300 return -ETIMEDOUT;
301 }
302
303 udelay(10);
304 }
305
306 if (tmio_sd_readl(priv, TMIO_SD_DMA_INFO2)) {
307 dev_err(dev, "error during DMA\n");
308 return -EIO;
309 }
310
311 return 0;
312}
313
314static int tmio_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
315{
316 struct tmio_sd_priv *priv = dev_get_priv(dev);
317 size_t len = data->blocks * data->blocksize;
318 void *buf;
319 enum dma_data_direction dir;
320 dma_addr_t dma_addr;
321 u32 poll_flag, tmp;
322 int ret;
323
324 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
325
326 if (data->flags & MMC_DATA_READ) {
327 buf = data->dest;
328 dir = DMA_FROM_DEVICE;
329 /*
330 * The DMA READ completion flag position differs on Socionext
331 * and Renesas SoCs. It is bit 20 on Socionext SoCs and using
Marek Vasut992bcf42019-01-11 23:45:54 +0100332 * bit 17 is a hardware bug and forbidden. It is either bit 17
333 * or bit 20 on Renesas SoCs, depending on SoC.
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200334 */
Marek Vasut992bcf42019-01-11 23:45:54 +0100335 poll_flag = priv->read_poll_flag;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200336 tmp |= TMIO_SD_DMA_MODE_DIR_RD;
337 } else {
338 buf = (void *)data->src;
339 dir = DMA_TO_DEVICE;
340 poll_flag = TMIO_SD_DMA_INFO1_END_WR;
341 tmp &= ~TMIO_SD_DMA_MODE_DIR_RD;
342 }
343
344 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
345
Vignesh Raghavendraeaa8b042020-01-16 14:23:46 +0530346 dma_addr = dma_map_single(buf, len, dir);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200347
348 tmio_sd_dma_start(priv, dma_addr);
349
350 ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
351
Marek Vasutcbbe6942019-01-11 23:38:07 +0100352 if (poll_flag == TMIO_SD_DMA_INFO1_END_RD)
353 udelay(1);
354
Masahiro Yamada950c5962020-02-14 16:40:18 +0900355 dma_unmap_single(dma_addr, len, dir);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200356
357 return ret;
358}
359
360/* check if the address is DMA'able */
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100361static bool tmio_sd_addr_is_dmaable(struct mmc_data *data)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200362{
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100363 uintptr_t addr = (uintptr_t)data->src;
Marek Vasut92bde152018-10-03 00:44:37 +0200364
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200365 if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN))
366 return false;
367
Marek Vasutbeced532018-10-03 00:46:24 +0200368#if defined(CONFIG_RCAR_GEN3)
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100369 if (!(data->flags & MMC_DATA_READ) && !IS_ALIGNED(addr, 128))
370 return false;
Marek Vasutbeced532018-10-03 00:46:24 +0200371 /* Gen3 DMA has 32bit limit */
372 if (addr >> 32)
373 return false;
374#endif
375
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200376#if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
377 defined(CONFIG_SPL_BUILD)
378 /*
379 * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
380 * of L2, which is unreachable from the DMA engine.
381 */
382 if (addr < CONFIG_SPL_STACK)
383 return false;
384#endif
385
386 return true;
387}
388
389int tmio_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
390 struct mmc_data *data)
391{
392 struct tmio_sd_priv *priv = dev_get_priv(dev);
393 int ret;
394 u32 tmp;
395
396 if (tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_CBSY) {
397 dev_err(dev, "command busy\n");
398 return -EBUSY;
399 }
400
401 /* clear all status flags */
402 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
403 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
404
405 /* disable DMA once */
406 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
407 tmp &= ~TMIO_SD_EXTMODE_DMA_EN;
408 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
409
410 tmio_sd_writel(priv, cmd->cmdarg, TMIO_SD_ARG);
411
412 tmp = cmd->cmdidx;
413
414 if (data) {
415 tmio_sd_writel(priv, data->blocksize, TMIO_SD_SIZE);
416 tmio_sd_writel(priv, data->blocks, TMIO_SD_SECCNT);
417
418 /* Do not send CMD12 automatically */
419 tmp |= TMIO_SD_CMD_NOSTOP | TMIO_SD_CMD_DATA;
420
421 if (data->blocks > 1)
422 tmp |= TMIO_SD_CMD_MULTI;
423
424 if (data->flags & MMC_DATA_READ)
425 tmp |= TMIO_SD_CMD_RD;
426 }
427
428 /*
429 * Do not use the response type auto-detection on this hardware.
430 * CMD8, for example, has different response types on SD and eMMC,
431 * while this controller always assumes the response type for SD.
432 * Set the response type manually.
433 */
434 switch (cmd->resp_type) {
435 case MMC_RSP_NONE:
436 tmp |= TMIO_SD_CMD_RSP_NONE;
437 break;
438 case MMC_RSP_R1:
439 tmp |= TMIO_SD_CMD_RSP_R1;
440 break;
441 case MMC_RSP_R1b:
442 tmp |= TMIO_SD_CMD_RSP_R1B;
443 break;
444 case MMC_RSP_R2:
445 tmp |= TMIO_SD_CMD_RSP_R2;
446 break;
447 case MMC_RSP_R3:
448 tmp |= TMIO_SD_CMD_RSP_R3;
449 break;
450 default:
451 dev_err(dev, "unknown response type\n");
452 return -EINVAL;
453 }
454
455 dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
456 cmd->cmdidx, tmp, cmd->cmdarg);
457 tmio_sd_writel(priv, tmp, TMIO_SD_CMD);
458
Marek Vasut33633eb2018-10-30 22:05:54 +0100459 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
460 TMIO_SD_INFO1_RSP);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200461 if (ret)
462 return ret;
463
464 if (cmd->resp_type & MMC_RSP_136) {
465 u32 rsp_127_104 = tmio_sd_readl(priv, TMIO_SD_RSP76);
466 u32 rsp_103_72 = tmio_sd_readl(priv, TMIO_SD_RSP54);
467 u32 rsp_71_40 = tmio_sd_readl(priv, TMIO_SD_RSP32);
468 u32 rsp_39_8 = tmio_sd_readl(priv, TMIO_SD_RSP10);
469
470 cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
471 ((rsp_103_72 & 0xff000000) >> 24);
472 cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
473 ((rsp_71_40 & 0xff000000) >> 24);
474 cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
475 ((rsp_39_8 & 0xff000000) >> 24);
476 cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
477 } else {
478 /* bit 39-8 */
479 cmd->response[0] = tmio_sd_readl(priv, TMIO_SD_RSP10);
480 }
481
482 if (data) {
483 /* use DMA if the HW supports it and the buffer is aligned */
484 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL &&
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100485 tmio_sd_addr_is_dmaable(data))
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200486 ret = tmio_sd_dma_xfer(dev, data);
487 else
Marek Vasut33633eb2018-10-30 22:05:54 +0100488 ret = tmio_sd_pio_xfer(dev, cmd, data);
Marek Vasutb22c8d02018-10-30 21:53:29 +0100489 if (ret)
490 return ret;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200491
Marek Vasut33633eb2018-10-30 22:05:54 +0100492 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
493 TMIO_SD_INFO1_CMP);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200494 if (ret)
495 return ret;
496 }
497
Marek Vasut33633eb2018-10-30 22:05:54 +0100498 return tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
Marek Vasutb22c8d02018-10-30 21:53:29 +0100499 TMIO_SD_INFO2_SCLKDIVEN);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200500}
501
502static int tmio_sd_set_bus_width(struct tmio_sd_priv *priv,
503 struct mmc *mmc)
504{
505 u32 val, tmp;
506
507 switch (mmc->bus_width) {
508 case 0:
509 case 1:
510 val = TMIO_SD_OPTION_WIDTH_1;
511 break;
512 case 4:
513 val = TMIO_SD_OPTION_WIDTH_4;
514 break;
515 case 8:
516 val = TMIO_SD_OPTION_WIDTH_8;
517 break;
518 default:
519 return -EINVAL;
520 }
521
522 tmp = tmio_sd_readl(priv, TMIO_SD_OPTION);
523 tmp &= ~TMIO_SD_OPTION_WIDTH_MASK;
524 tmp |= val;
525 tmio_sd_writel(priv, tmp, TMIO_SD_OPTION);
526
527 return 0;
528}
529
530static void tmio_sd_set_ddr_mode(struct tmio_sd_priv *priv,
531 struct mmc *mmc)
532{
533 u32 tmp;
534
535 tmp = tmio_sd_readl(priv, TMIO_SD_IF_MODE);
536 if (mmc->ddr_mode)
537 tmp |= TMIO_SD_IF_MODE_DDR;
538 else
539 tmp &= ~TMIO_SD_IF_MODE_DDR;
540 tmio_sd_writel(priv, tmp, TMIO_SD_IF_MODE);
541}
542
Marek Vasut8ec6a042018-06-13 08:02:55 +0200543static ulong tmio_sd_clk_get_rate(struct tmio_sd_priv *priv)
544{
545 return priv->clk_get_rate(priv);
546}
547
Marek Vasuted427da2018-11-15 22:01:33 +0100548static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv, struct mmc *mmc)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200549{
550 unsigned int divisor;
Marek Vasuted427da2018-11-15 22:01:33 +0100551 u32 tmp, val = 0;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200552 ulong mclk;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200553
Marek Vasuted427da2018-11-15 22:01:33 +0100554 if (mmc->clock) {
555 mclk = tmio_sd_clk_get_rate(priv);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200556
Marek Vasuted427da2018-11-15 22:01:33 +0100557 divisor = DIV_ROUND_UP(mclk, mmc->clock);
Marek Vasut8ec6a042018-06-13 08:02:55 +0200558
Marek Vasuted427da2018-11-15 22:01:33 +0100559 /* Do not set divider to 0xff in DDR mode */
560 if (mmc->ddr_mode && (divisor == 1))
561 divisor = 2;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200562
Marek Vasuted427da2018-11-15 22:01:33 +0100563 if (divisor <= 1)
564 val = (priv->caps & TMIO_SD_CAP_RCAR) ?
565 TMIO_SD_CLKCTL_RCAR_DIV1 : TMIO_SD_CLKCTL_DIV1;
566 else if (divisor <= 2)
567 val = TMIO_SD_CLKCTL_DIV2;
568 else if (divisor <= 4)
569 val = TMIO_SD_CLKCTL_DIV4;
570 else if (divisor <= 8)
571 val = TMIO_SD_CLKCTL_DIV8;
572 else if (divisor <= 16)
573 val = TMIO_SD_CLKCTL_DIV16;
574 else if (divisor <= 32)
575 val = TMIO_SD_CLKCTL_DIV32;
576 else if (divisor <= 64)
577 val = TMIO_SD_CLKCTL_DIV64;
578 else if (divisor <= 128)
579 val = TMIO_SD_CLKCTL_DIV128;
580 else if (divisor <= 256)
581 val = TMIO_SD_CLKCTL_DIV256;
582 else if (divisor <= 512 || !(priv->caps & TMIO_SD_CAP_DIV1024))
583 val = TMIO_SD_CLKCTL_DIV512;
584 else
585 val = TMIO_SD_CLKCTL_DIV1024;
586 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200587
588 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
Marek Vasuted427da2018-11-15 22:01:33 +0100589 if (mmc->clock &&
590 !((tmp & TMIO_SD_CLKCTL_SCLKEN) &&
591 ((tmp & TMIO_SD_CLKCTL_DIV_MASK) == val))) {
592 /*
593 * Stop the clock before changing its rate
594 * to avoid a glitch signal
595 */
596 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
597 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200598
Marek Vasuted427da2018-11-15 22:01:33 +0100599 /* Change the clock rate. */
600 tmp &= ~TMIO_SD_CLKCTL_DIV_MASK;
601 tmp |= val;
602 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200603
Marek Vasuted427da2018-11-15 22:01:33 +0100604 /* Enable or Disable the clock */
605 if (mmc->clk_disable) {
Marek Vasut59d529a2018-06-13 08:02:55 +0200606 tmp |= TMIO_SD_CLKCTL_OFFEN;
607 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
Marek Vasuted427da2018-11-15 22:01:33 +0100608 } else {
609 tmp &= ~TMIO_SD_CLKCTL_OFFEN;
610 tmp |= TMIO_SD_CLKCTL_SCLKEN;
Marek Vasut59d529a2018-06-13 08:02:55 +0200611 }
Marek Vasuted427da2018-11-15 22:01:33 +0100612
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200613 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
614
615 udelay(1000);
616}
617
618static void tmio_sd_set_pins(struct udevice *dev)
619{
620 __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev);
621
622#ifdef CONFIG_DM_REGULATOR
623 struct tmio_sd_priv *priv = dev_get_priv(dev);
624
625 if (priv->vqmmc_dev) {
626 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
627 regulator_set_value(priv->vqmmc_dev, 1800000);
628 else
629 regulator_set_value(priv->vqmmc_dev, 3300000);
630 regulator_set_enable(priv->vqmmc_dev, true);
631 }
632#endif
633
634#ifdef CONFIG_PINCTRL
Marek Vasut645a5752018-10-28 13:54:10 +0100635 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200636 pinctrl_select_state(dev, "state_uhs");
Marek Vasut645a5752018-10-28 13:54:10 +0100637 else
638 pinctrl_select_state(dev, "default");
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200639#endif
640}
641
642int tmio_sd_set_ios(struct udevice *dev)
643{
644 struct tmio_sd_priv *priv = dev_get_priv(dev);
645 struct mmc *mmc = mmc_get_mmc_dev(dev);
646 int ret;
647
648 dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
649 mmc->clock, mmc->ddr_mode, mmc->bus_width);
650
Marek Vasut8171f992018-06-13 08:02:55 +0200651 tmio_sd_set_clk_rate(priv, mmc);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200652 ret = tmio_sd_set_bus_width(priv, mmc);
653 if (ret)
654 return ret;
655 tmio_sd_set_ddr_mode(priv, mmc);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200656 tmio_sd_set_pins(dev);
657
658 return 0;
659}
660
661int tmio_sd_get_cd(struct udevice *dev)
662{
663 struct tmio_sd_priv *priv = dev_get_priv(dev);
664
665 if (priv->caps & TMIO_SD_CAP_NONREMOVABLE)
666 return 1;
667
668 return !!(tmio_sd_readl(priv, TMIO_SD_INFO1) &
669 TMIO_SD_INFO1_CD);
670}
671
672static void tmio_sd_host_init(struct tmio_sd_priv *priv)
673{
674 u32 tmp;
675
676 /* soft reset of the host */
677 tmp = tmio_sd_readl(priv, TMIO_SD_SOFT_RST);
678 tmp &= ~TMIO_SD_SOFT_RST_RSTX;
679 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
680 tmp |= TMIO_SD_SOFT_RST_RSTX;
681 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
682
683 /* FIXME: implement eMMC hw_reset */
684
685 tmio_sd_writel(priv, TMIO_SD_STOP_SEC, TMIO_SD_STOP);
686
687 /*
688 * Connected to 32bit AXI.
689 * This register dropped backward compatibility at version 0x10.
690 * Write an appropriate value depending on the IP version.
691 */
Marek Vasut4c80f112019-02-14 15:16:24 +0100692 if (priv->version >= 0x10) {
693 if (priv->caps & TMIO_SD_CAP_64BIT)
Marek Vasut5d688842019-02-19 19:20:14 +0100694 tmio_sd_writel(priv, 0x000, TMIO_SD_HOST_MODE);
Marek Vasut4c80f112019-02-14 15:16:24 +0100695 else
696 tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE);
697 } else {
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200698 tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE);
Marek Vasut4c80f112019-02-14 15:16:24 +0100699 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200700
701 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL) {
702 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
703 tmp |= TMIO_SD_DMA_MODE_ADDR_INC;
704 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
705 }
706}
707
708int tmio_sd_bind(struct udevice *dev)
709{
710 struct tmio_sd_plat *plat = dev_get_platdata(dev);
711
712 return mmc_bind(dev, &plat->mmc, &plat->cfg);
713}
714
715int tmio_sd_probe(struct udevice *dev, u32 quirks)
716{
717 struct tmio_sd_plat *plat = dev_get_platdata(dev);
718 struct tmio_sd_priv *priv = dev_get_priv(dev);
719 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
720 fdt_addr_t base;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200721 ulong mclk;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200722 int ret;
723
724 base = devfdt_get_addr(dev);
725 if (base == FDT_ADDR_T_NONE)
726 return -EINVAL;
727
728 priv->regbase = devm_ioremap(dev, base, SZ_2K);
729 if (!priv->regbase)
730 return -ENOMEM;
731
732#ifdef CONFIG_DM_REGULATOR
733 device_get_supply_regulator(dev, "vqmmc-supply", &priv->vqmmc_dev);
Marek Vasutc83da2e2018-06-13 08:02:55 +0200734 if (priv->vqmmc_dev)
735 regulator_set_value(priv->vqmmc_dev, 3300000);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200736#endif
737
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200738 ret = mmc_of_parse(dev, &plat->cfg);
739 if (ret < 0) {
740 dev_err(dev, "failed to parse host caps\n");
741 return ret;
742 }
743
744 plat->cfg.name = dev->name;
745 plat->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
746
747 if (quirks)
748 priv->caps = quirks;
749
750 priv->version = tmio_sd_readl(priv, TMIO_SD_VERSION) &
751 TMIO_SD_VERSION_IP;
752 dev_dbg(dev, "version %x\n", priv->version);
753 if (priv->version >= 0x10) {
754 priv->caps |= TMIO_SD_CAP_DMA_INTERNAL;
755 priv->caps |= TMIO_SD_CAP_DIV1024;
756 }
757
758 if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
759 NULL))
760 priv->caps |= TMIO_SD_CAP_NONREMOVABLE;
761
762 tmio_sd_host_init(priv);
763
Marek Vasut8ec6a042018-06-13 08:02:55 +0200764 mclk = tmio_sd_clk_get_rate(priv);
765
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200766 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200767 plat->cfg.f_min = mclk /
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200768 (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512);
Marek Vasut8ec6a042018-06-13 08:02:55 +0200769 plat->cfg.f_max = mclk;
Marek Vasutc453fe32019-03-18 23:43:10 +0100770 if (quirks & TMIO_SD_CAP_16BIT)
771 plat->cfg.b_max = U16_MAX; /* max value of TMIO_SD_SECCNT */
772 else
773 plat->cfg.b_max = U32_MAX; /* max value of TMIO_SD_SECCNT */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200774
775 upriv->mmc = &plat->mmc;
776
777 return 0;
778}