blob: 6c0c840bbb101cce5b5bf6b8ab44b89ba80c606a [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>
Simon Glassc05ed002020-05-10 11:40:11 -060016#include <linux/delay.h>
Masahiro Yamada9d86b892020-02-14 16:40:19 +090017#include <linux/dma-mapping.h>
Marek Vasutcb0b6b02018-04-13 23:51:33 +020018#include <linux/io.h>
19#include <linux/sizes.h>
20#include <power/regulator.h>
21#include <asm/unaligned.h>
22
23#include "tmio-common.h"
24
25DECLARE_GLOBAL_DATA_PTR;
26
27static u64 tmio_sd_readq(struct tmio_sd_priv *priv, unsigned int reg)
28{
29 return readq(priv->regbase + (reg << 1));
30}
31
32static void tmio_sd_writeq(struct tmio_sd_priv *priv,
33 u64 val, unsigned int reg)
34{
35 writeq(val, priv->regbase + (reg << 1));
36}
37
38static u16 tmio_sd_readw(struct tmio_sd_priv *priv, unsigned int reg)
39{
40 return readw(priv->regbase + (reg >> 1));
41}
42
43static void tmio_sd_writew(struct tmio_sd_priv *priv,
44 u16 val, unsigned int reg)
45{
46 writew(val, priv->regbase + (reg >> 1));
47}
48
49u32 tmio_sd_readl(struct tmio_sd_priv *priv, unsigned int reg)
50{
51 u32 val;
52
53 if (priv->caps & TMIO_SD_CAP_64BIT)
54 return readl(priv->regbase + (reg << 1));
55 else if (priv->caps & TMIO_SD_CAP_16BIT) {
56 val = readw(priv->regbase + (reg >> 1)) & 0xffff;
57 if ((reg == TMIO_SD_RSP10) || (reg == TMIO_SD_RSP32) ||
58 (reg == TMIO_SD_RSP54) || (reg == TMIO_SD_RSP76)) {
59 val |= readw(priv->regbase + (reg >> 1) + 2) << 16;
60 }
61 return val;
62 } else
63 return readl(priv->regbase + reg);
64}
65
66void tmio_sd_writel(struct tmio_sd_priv *priv,
67 u32 val, unsigned int reg)
68{
69 if (priv->caps & TMIO_SD_CAP_64BIT)
70 writel(val, priv->regbase + (reg << 1));
71 else if (priv->caps & TMIO_SD_CAP_16BIT) {
72 writew(val & 0xffff, priv->regbase + (reg >> 1));
73 if (reg == TMIO_SD_INFO1 || reg == TMIO_SD_INFO1_MASK ||
74 reg == TMIO_SD_INFO2 || reg == TMIO_SD_INFO2_MASK ||
75 reg == TMIO_SD_ARG)
76 writew(val >> 16, priv->regbase + (reg >> 1) + 2);
77 } else
78 writel(val, priv->regbase + reg);
79}
80
Marek Vasut33633eb2018-10-30 22:05:54 +010081static int tmio_sd_check_error(struct udevice *dev, struct mmc_cmd *cmd)
Marek Vasutcb0b6b02018-04-13 23:51:33 +020082{
83 struct tmio_sd_priv *priv = dev_get_priv(dev);
84 u32 info2 = tmio_sd_readl(priv, TMIO_SD_INFO2);
85
86 if (info2 & TMIO_SD_INFO2_ERR_RTO) {
87 /*
88 * TIMEOUT must be returned for unsupported command. Do not
89 * display error log since this might be a part of sequence to
90 * distinguish between SD and MMC.
91 */
92 return -ETIMEDOUT;
93 }
94
95 if (info2 & TMIO_SD_INFO2_ERR_TO) {
96 dev_err(dev, "timeout error\n");
97 return -ETIMEDOUT;
98 }
99
100 if (info2 & (TMIO_SD_INFO2_ERR_END | TMIO_SD_INFO2_ERR_CRC |
101 TMIO_SD_INFO2_ERR_IDX)) {
Marek Vasut33633eb2018-10-30 22:05:54 +0100102 if ((cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK) &&
103 (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200))
104 dev_err(dev, "communication out of sync\n");
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200105 return -EILSEQ;
106 }
107
108 if (info2 & (TMIO_SD_INFO2_ERR_ILA | TMIO_SD_INFO2_ERR_ILR |
109 TMIO_SD_INFO2_ERR_ILW)) {
110 dev_err(dev, "illegal access\n");
111 return -EIO;
112 }
113
114 return 0;
115}
116
Marek Vasut33633eb2018-10-30 22:05:54 +0100117static int tmio_sd_wait_for_irq(struct udevice *dev, struct mmc_cmd *cmd,
118 unsigned int reg, u32 flag)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200119{
120 struct tmio_sd_priv *priv = dev_get_priv(dev);
121 long wait = 1000000;
122 int ret;
123
124 while (!(tmio_sd_readl(priv, reg) & flag)) {
125 if (wait-- < 0) {
126 dev_err(dev, "timeout\n");
127 return -ETIMEDOUT;
128 }
129
Marek Vasut33633eb2018-10-30 22:05:54 +0100130 ret = tmio_sd_check_error(dev, cmd);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200131 if (ret)
132 return ret;
133
134 udelay(1);
135 }
136
137 return 0;
138}
139
140#define tmio_pio_read_fifo(__width, __suffix) \
141static void tmio_pio_read_fifo_##__width(struct tmio_sd_priv *priv, \
142 char *pbuf, uint blksz) \
143{ \
144 u##__width *buf = (u##__width *)pbuf; \
145 int i; \
146 \
147 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
148 for (i = 0; i < blksz / ((__width) / 8); i++) { \
149 *buf++ = tmio_sd_read##__suffix(priv, \
150 TMIO_SD_BUF); \
151 } \
152 } else { \
153 for (i = 0; i < blksz / ((__width) / 8); i++) { \
154 u##__width data; \
155 data = tmio_sd_read##__suffix(priv, \
156 TMIO_SD_BUF); \
157 put_unaligned(data, buf++); \
158 } \
159 } \
160}
161
162tmio_pio_read_fifo(64, q)
163tmio_pio_read_fifo(32, l)
164tmio_pio_read_fifo(16, w)
165
Marek Vasut33633eb2018-10-30 22:05:54 +0100166static int tmio_sd_pio_read_one_block(struct udevice *dev, struct mmc_cmd *cmd,
167 char *pbuf, uint blocksize)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200168{
169 struct tmio_sd_priv *priv = dev_get_priv(dev);
170 int ret;
171
172 /* wait until the buffer is filled with data */
Marek Vasut33633eb2018-10-30 22:05:54 +0100173 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
174 TMIO_SD_INFO2_BRE);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200175 if (ret)
176 return ret;
177
178 /*
179 * Clear the status flag _before_ read the buffer out because
180 * TMIO_SD_INFO2_BRE is edge-triggered, not level-triggered.
181 */
182 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
183
184 if (priv->caps & TMIO_SD_CAP_64BIT)
185 tmio_pio_read_fifo_64(priv, pbuf, blocksize);
186 else if (priv->caps & TMIO_SD_CAP_16BIT)
187 tmio_pio_read_fifo_16(priv, pbuf, blocksize);
188 else
189 tmio_pio_read_fifo_32(priv, pbuf, blocksize);
190
191 return 0;
192}
193
194#define tmio_pio_write_fifo(__width, __suffix) \
195static void tmio_pio_write_fifo_##__width(struct tmio_sd_priv *priv, \
196 const char *pbuf, uint blksz)\
197{ \
198 const u##__width *buf = (const u##__width *)pbuf; \
199 int i; \
200 \
201 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
202 for (i = 0; i < blksz / ((__width) / 8); i++) { \
203 tmio_sd_write##__suffix(priv, *buf++, \
204 TMIO_SD_BUF); \
205 } \
206 } else { \
207 for (i = 0; i < blksz / ((__width) / 8); i++) { \
208 u##__width data = get_unaligned(buf++); \
209 tmio_sd_write##__suffix(priv, data, \
210 TMIO_SD_BUF); \
211 } \
212 } \
213}
214
215tmio_pio_write_fifo(64, q)
216tmio_pio_write_fifo(32, l)
217tmio_pio_write_fifo(16, w)
218
Marek Vasut33633eb2018-10-30 22:05:54 +0100219static int tmio_sd_pio_write_one_block(struct udevice *dev, struct mmc_cmd *cmd,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200220 const char *pbuf, uint blocksize)
221{
222 struct tmio_sd_priv *priv = dev_get_priv(dev);
223 int ret;
224
225 /* wait until the buffer becomes empty */
Marek Vasut33633eb2018-10-30 22:05:54 +0100226 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
227 TMIO_SD_INFO2_BWE);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200228 if (ret)
229 return ret;
230
231 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
232
233 if (priv->caps & TMIO_SD_CAP_64BIT)
234 tmio_pio_write_fifo_64(priv, pbuf, blocksize);
235 else if (priv->caps & TMIO_SD_CAP_16BIT)
236 tmio_pio_write_fifo_16(priv, pbuf, blocksize);
237 else
238 tmio_pio_write_fifo_32(priv, pbuf, blocksize);
239
240 return 0;
241}
242
Marek Vasut33633eb2018-10-30 22:05:54 +0100243static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_cmd *cmd,
244 struct mmc_data *data)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200245{
246 const char *src = data->src;
247 char *dest = data->dest;
248 int i, ret;
249
250 for (i = 0; i < data->blocks; i++) {
251 if (data->flags & MMC_DATA_READ)
Marek Vasut33633eb2018-10-30 22:05:54 +0100252 ret = tmio_sd_pio_read_one_block(dev, cmd, dest,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200253 data->blocksize);
254 else
Marek Vasut33633eb2018-10-30 22:05:54 +0100255 ret = tmio_sd_pio_write_one_block(dev, cmd, src,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200256 data->blocksize);
257 if (ret)
258 return ret;
259
260 if (data->flags & MMC_DATA_READ)
261 dest += data->blocksize;
262 else
263 src += data->blocksize;
264 }
265
266 return 0;
267}
268
269static void tmio_sd_dma_start(struct tmio_sd_priv *priv,
270 dma_addr_t dma_addr)
271{
272 u32 tmp;
273
274 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO1);
275 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO2);
276
277 /* enable DMA */
278 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
279 tmp |= TMIO_SD_EXTMODE_DMA_EN;
280 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
281
282 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_L);
283
284 /* suppress the warning "right shift count >= width of type" */
285 dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
286
287 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_H);
288
289 tmio_sd_writel(priv, TMIO_SD_DMA_CTL_START, TMIO_SD_DMA_CTL);
290}
291
292static int tmio_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
293 unsigned int blocks)
294{
295 struct tmio_sd_priv *priv = dev_get_priv(dev);
296 long wait = 1000000 + 10 * blocks;
297
298 while (!(tmio_sd_readl(priv, TMIO_SD_DMA_INFO1) & flag)) {
299 if (wait-- < 0) {
300 dev_err(dev, "timeout during DMA\n");
301 return -ETIMEDOUT;
302 }
303
304 udelay(10);
305 }
306
307 if (tmio_sd_readl(priv, TMIO_SD_DMA_INFO2)) {
308 dev_err(dev, "error during DMA\n");
309 return -EIO;
310 }
311
312 return 0;
313}
314
315static int tmio_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
316{
317 struct tmio_sd_priv *priv = dev_get_priv(dev);
318 size_t len = data->blocks * data->blocksize;
319 void *buf;
320 enum dma_data_direction dir;
321 dma_addr_t dma_addr;
322 u32 poll_flag, tmp;
323 int ret;
324
325 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
326
Marek Vasut8a73bef2021-01-03 11:38:25 +0100327 tmp |= priv->idma_bus_width;
328
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200329 if (data->flags & MMC_DATA_READ) {
330 buf = data->dest;
331 dir = DMA_FROM_DEVICE;
332 /*
333 * The DMA READ completion flag position differs on Socionext
334 * and Renesas SoCs. It is bit 20 on Socionext SoCs and using
Marek Vasut992bcf42019-01-11 23:45:54 +0100335 * bit 17 is a hardware bug and forbidden. It is either bit 17
336 * or bit 20 on Renesas SoCs, depending on SoC.
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200337 */
Marek Vasut992bcf42019-01-11 23:45:54 +0100338 poll_flag = priv->read_poll_flag;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200339 tmp |= TMIO_SD_DMA_MODE_DIR_RD;
340 } else {
341 buf = (void *)data->src;
342 dir = DMA_TO_DEVICE;
343 poll_flag = TMIO_SD_DMA_INFO1_END_WR;
344 tmp &= ~TMIO_SD_DMA_MODE_DIR_RD;
345 }
346
347 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
348
Vignesh Raghavendraeaa8b042020-01-16 14:23:46 +0530349 dma_addr = dma_map_single(buf, len, dir);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200350
351 tmio_sd_dma_start(priv, dma_addr);
352
353 ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
354
Marek Vasutcbbe6942019-01-11 23:38:07 +0100355 if (poll_flag == TMIO_SD_DMA_INFO1_END_RD)
356 udelay(1);
357
Masahiro Yamada950c5962020-02-14 16:40:18 +0900358 dma_unmap_single(dma_addr, len, dir);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200359
360 return ret;
361}
362
363/* check if the address is DMA'able */
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100364static bool tmio_sd_addr_is_dmaable(struct mmc_data *data)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200365{
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100366 uintptr_t addr = (uintptr_t)data->src;
Marek Vasut92bde152018-10-03 00:44:37 +0200367
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200368 if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN))
369 return false;
370
Marek Vasutbeced532018-10-03 00:46:24 +0200371#if defined(CONFIG_RCAR_GEN3)
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100372 if (!(data->flags & MMC_DATA_READ) && !IS_ALIGNED(addr, 128))
373 return false;
Marek Vasutbeced532018-10-03 00:46:24 +0200374 /* Gen3 DMA has 32bit limit */
375 if (addr >> 32)
376 return false;
377#endif
378
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200379#if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
380 defined(CONFIG_SPL_BUILD)
381 /*
382 * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
383 * of L2, which is unreachable from the DMA engine.
384 */
385 if (addr < CONFIG_SPL_STACK)
386 return false;
387#endif
388
389 return true;
390}
391
392int tmio_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
393 struct mmc_data *data)
394{
395 struct tmio_sd_priv *priv = dev_get_priv(dev);
396 int ret;
397 u32 tmp;
398
399 if (tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_CBSY) {
400 dev_err(dev, "command busy\n");
401 return -EBUSY;
402 }
403
404 /* clear all status flags */
405 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
406 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
407
408 /* disable DMA once */
409 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
410 tmp &= ~TMIO_SD_EXTMODE_DMA_EN;
411 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
412
413 tmio_sd_writel(priv, cmd->cmdarg, TMIO_SD_ARG);
414
415 tmp = cmd->cmdidx;
416
417 if (data) {
418 tmio_sd_writel(priv, data->blocksize, TMIO_SD_SIZE);
419 tmio_sd_writel(priv, data->blocks, TMIO_SD_SECCNT);
420
421 /* Do not send CMD12 automatically */
422 tmp |= TMIO_SD_CMD_NOSTOP | TMIO_SD_CMD_DATA;
423
424 if (data->blocks > 1)
425 tmp |= TMIO_SD_CMD_MULTI;
426
427 if (data->flags & MMC_DATA_READ)
428 tmp |= TMIO_SD_CMD_RD;
429 }
430
431 /*
432 * Do not use the response type auto-detection on this hardware.
433 * CMD8, for example, has different response types on SD and eMMC,
434 * while this controller always assumes the response type for SD.
435 * Set the response type manually.
436 */
437 switch (cmd->resp_type) {
438 case MMC_RSP_NONE:
439 tmp |= TMIO_SD_CMD_RSP_NONE;
440 break;
441 case MMC_RSP_R1:
442 tmp |= TMIO_SD_CMD_RSP_R1;
443 break;
444 case MMC_RSP_R1b:
445 tmp |= TMIO_SD_CMD_RSP_R1B;
446 break;
447 case MMC_RSP_R2:
448 tmp |= TMIO_SD_CMD_RSP_R2;
449 break;
450 case MMC_RSP_R3:
451 tmp |= TMIO_SD_CMD_RSP_R3;
452 break;
453 default:
454 dev_err(dev, "unknown response type\n");
455 return -EINVAL;
456 }
457
458 dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
459 cmd->cmdidx, tmp, cmd->cmdarg);
460 tmio_sd_writel(priv, tmp, TMIO_SD_CMD);
461
Marek Vasut33633eb2018-10-30 22:05:54 +0100462 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
463 TMIO_SD_INFO1_RSP);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200464 if (ret)
465 return ret;
466
467 if (cmd->resp_type & MMC_RSP_136) {
468 u32 rsp_127_104 = tmio_sd_readl(priv, TMIO_SD_RSP76);
469 u32 rsp_103_72 = tmio_sd_readl(priv, TMIO_SD_RSP54);
470 u32 rsp_71_40 = tmio_sd_readl(priv, TMIO_SD_RSP32);
471 u32 rsp_39_8 = tmio_sd_readl(priv, TMIO_SD_RSP10);
472
473 cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
474 ((rsp_103_72 & 0xff000000) >> 24);
475 cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
476 ((rsp_71_40 & 0xff000000) >> 24);
477 cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
478 ((rsp_39_8 & 0xff000000) >> 24);
479 cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
480 } else {
481 /* bit 39-8 */
482 cmd->response[0] = tmio_sd_readl(priv, TMIO_SD_RSP10);
483 }
484
485 if (data) {
486 /* use DMA if the HW supports it and the buffer is aligned */
487 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL &&
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100488 tmio_sd_addr_is_dmaable(data))
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200489 ret = tmio_sd_dma_xfer(dev, data);
490 else
Marek Vasut33633eb2018-10-30 22:05:54 +0100491 ret = tmio_sd_pio_xfer(dev, cmd, data);
Marek Vasutb22c8d02018-10-30 21:53:29 +0100492 if (ret)
493 return ret;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200494
Marek Vasut33633eb2018-10-30 22:05:54 +0100495 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
496 TMIO_SD_INFO1_CMP);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200497 if (ret)
498 return ret;
499 }
500
Marek Vasut33633eb2018-10-30 22:05:54 +0100501 return tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
Marek Vasutb22c8d02018-10-30 21:53:29 +0100502 TMIO_SD_INFO2_SCLKDIVEN);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200503}
504
505static int tmio_sd_set_bus_width(struct tmio_sd_priv *priv,
506 struct mmc *mmc)
507{
508 u32 val, tmp;
509
510 switch (mmc->bus_width) {
511 case 0:
512 case 1:
513 val = TMIO_SD_OPTION_WIDTH_1;
514 break;
515 case 4:
516 val = TMIO_SD_OPTION_WIDTH_4;
517 break;
518 case 8:
519 val = TMIO_SD_OPTION_WIDTH_8;
520 break;
521 default:
522 return -EINVAL;
523 }
524
525 tmp = tmio_sd_readl(priv, TMIO_SD_OPTION);
526 tmp &= ~TMIO_SD_OPTION_WIDTH_MASK;
527 tmp |= val;
528 tmio_sd_writel(priv, tmp, TMIO_SD_OPTION);
529
530 return 0;
531}
532
533static void tmio_sd_set_ddr_mode(struct tmio_sd_priv *priv,
534 struct mmc *mmc)
535{
536 u32 tmp;
537
538 tmp = tmio_sd_readl(priv, TMIO_SD_IF_MODE);
539 if (mmc->ddr_mode)
540 tmp |= TMIO_SD_IF_MODE_DDR;
541 else
542 tmp &= ~TMIO_SD_IF_MODE_DDR;
543 tmio_sd_writel(priv, tmp, TMIO_SD_IF_MODE);
544}
545
Marek Vasut8ec6a042018-06-13 08:02:55 +0200546static ulong tmio_sd_clk_get_rate(struct tmio_sd_priv *priv)
547{
548 return priv->clk_get_rate(priv);
549}
550
Marek Vasuted427da2018-11-15 22:01:33 +0100551static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv, struct mmc *mmc)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200552{
553 unsigned int divisor;
Marek Vasuted427da2018-11-15 22:01:33 +0100554 u32 tmp, val = 0;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200555 ulong mclk;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200556
Marek Vasuted427da2018-11-15 22:01:33 +0100557 if (mmc->clock) {
558 mclk = tmio_sd_clk_get_rate(priv);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200559
Marek Vasuted427da2018-11-15 22:01:33 +0100560 divisor = DIV_ROUND_UP(mclk, mmc->clock);
Marek Vasut8ec6a042018-06-13 08:02:55 +0200561
Marek Vasuted427da2018-11-15 22:01:33 +0100562 /* Do not set divider to 0xff in DDR mode */
563 if (mmc->ddr_mode && (divisor == 1))
564 divisor = 2;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200565
Marek Vasuted427da2018-11-15 22:01:33 +0100566 if (divisor <= 1)
567 val = (priv->caps & TMIO_SD_CAP_RCAR) ?
568 TMIO_SD_CLKCTL_RCAR_DIV1 : TMIO_SD_CLKCTL_DIV1;
569 else if (divisor <= 2)
570 val = TMIO_SD_CLKCTL_DIV2;
571 else if (divisor <= 4)
572 val = TMIO_SD_CLKCTL_DIV4;
573 else if (divisor <= 8)
574 val = TMIO_SD_CLKCTL_DIV8;
575 else if (divisor <= 16)
576 val = TMIO_SD_CLKCTL_DIV16;
577 else if (divisor <= 32)
578 val = TMIO_SD_CLKCTL_DIV32;
579 else if (divisor <= 64)
580 val = TMIO_SD_CLKCTL_DIV64;
581 else if (divisor <= 128)
582 val = TMIO_SD_CLKCTL_DIV128;
583 else if (divisor <= 256)
584 val = TMIO_SD_CLKCTL_DIV256;
585 else if (divisor <= 512 || !(priv->caps & TMIO_SD_CAP_DIV1024))
586 val = TMIO_SD_CLKCTL_DIV512;
587 else
588 val = TMIO_SD_CLKCTL_DIV1024;
589 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200590
591 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
Marek Vasuted427da2018-11-15 22:01:33 +0100592 if (mmc->clock &&
593 !((tmp & TMIO_SD_CLKCTL_SCLKEN) &&
594 ((tmp & TMIO_SD_CLKCTL_DIV_MASK) == val))) {
595 /*
596 * Stop the clock before changing its rate
597 * to avoid a glitch signal
598 */
599 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
600 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200601
Marek Vasuted427da2018-11-15 22:01:33 +0100602 /* Change the clock rate. */
603 tmp &= ~TMIO_SD_CLKCTL_DIV_MASK;
604 tmp |= val;
605 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200606
Marek Vasuted427da2018-11-15 22:01:33 +0100607 /* Enable or Disable the clock */
608 if (mmc->clk_disable) {
Marek Vasut59d529a2018-06-13 08:02:55 +0200609 tmp |= TMIO_SD_CLKCTL_OFFEN;
610 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
Marek Vasuted427da2018-11-15 22:01:33 +0100611 } else {
612 tmp &= ~TMIO_SD_CLKCTL_OFFEN;
613 tmp |= TMIO_SD_CLKCTL_SCLKEN;
Marek Vasut59d529a2018-06-13 08:02:55 +0200614 }
Marek Vasuted427da2018-11-15 22:01:33 +0100615
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200616 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
617
618 udelay(1000);
619}
620
621static void tmio_sd_set_pins(struct udevice *dev)
622{
623 __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev);
624
625#ifdef CONFIG_DM_REGULATOR
626 struct tmio_sd_priv *priv = dev_get_priv(dev);
627
628 if (priv->vqmmc_dev) {
629 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
630 regulator_set_value(priv->vqmmc_dev, 1800000);
631 else
632 regulator_set_value(priv->vqmmc_dev, 3300000);
633 regulator_set_enable(priv->vqmmc_dev, true);
634 }
635#endif
636
637#ifdef CONFIG_PINCTRL
Marek Vasut645a5752018-10-28 13:54:10 +0100638 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200639 pinctrl_select_state(dev, "state_uhs");
Marek Vasut645a5752018-10-28 13:54:10 +0100640 else
641 pinctrl_select_state(dev, "default");
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200642#endif
643}
644
645int tmio_sd_set_ios(struct udevice *dev)
646{
647 struct tmio_sd_priv *priv = dev_get_priv(dev);
648 struct mmc *mmc = mmc_get_mmc_dev(dev);
649 int ret;
650
651 dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
652 mmc->clock, mmc->ddr_mode, mmc->bus_width);
653
Marek Vasut8171f992018-06-13 08:02:55 +0200654 tmio_sd_set_clk_rate(priv, mmc);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200655 ret = tmio_sd_set_bus_width(priv, mmc);
656 if (ret)
657 return ret;
658 tmio_sd_set_ddr_mode(priv, mmc);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200659 tmio_sd_set_pins(dev);
660
661 return 0;
662}
663
664int tmio_sd_get_cd(struct udevice *dev)
665{
666 struct tmio_sd_priv *priv = dev_get_priv(dev);
667
668 if (priv->caps & TMIO_SD_CAP_NONREMOVABLE)
669 return 1;
670
671 return !!(tmio_sd_readl(priv, TMIO_SD_INFO1) &
672 TMIO_SD_INFO1_CD);
673}
674
675static void tmio_sd_host_init(struct tmio_sd_priv *priv)
676{
677 u32 tmp;
678
679 /* soft reset of the host */
680 tmp = tmio_sd_readl(priv, TMIO_SD_SOFT_RST);
681 tmp &= ~TMIO_SD_SOFT_RST_RSTX;
682 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
683 tmp |= TMIO_SD_SOFT_RST_RSTX;
684 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
685
686 /* FIXME: implement eMMC hw_reset */
687
688 tmio_sd_writel(priv, TMIO_SD_STOP_SEC, TMIO_SD_STOP);
689
690 /*
691 * Connected to 32bit AXI.
692 * This register dropped backward compatibility at version 0x10.
693 * Write an appropriate value depending on the IP version.
694 */
Marek Vasut4c80f112019-02-14 15:16:24 +0100695 if (priv->version >= 0x10) {
696 if (priv->caps & TMIO_SD_CAP_64BIT)
Marek Vasut5d688842019-02-19 19:20:14 +0100697 tmio_sd_writel(priv, 0x000, TMIO_SD_HOST_MODE);
Marek Vasut4c80f112019-02-14 15:16:24 +0100698 else
699 tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE);
700 } else {
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200701 tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE);
Marek Vasut4c80f112019-02-14 15:16:24 +0100702 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200703
704 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL) {
705 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
706 tmp |= TMIO_SD_DMA_MODE_ADDR_INC;
Marek Vasut8a73bef2021-01-03 11:38:25 +0100707 tmp |= priv->idma_bus_width;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200708 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
709 }
710}
711
712int tmio_sd_bind(struct udevice *dev)
713{
Simon Glassc69cda22020-12-03 16:55:20 -0700714 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200715
716 return mmc_bind(dev, &plat->mmc, &plat->cfg);
717}
718
719int tmio_sd_probe(struct udevice *dev, u32 quirks)
720{
Simon Glassc69cda22020-12-03 16:55:20 -0700721 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200722 struct tmio_sd_priv *priv = dev_get_priv(dev);
723 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
724 fdt_addr_t base;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200725 ulong mclk;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200726 int ret;
727
Masahiro Yamada25484932020-07-17 14:36:48 +0900728 base = dev_read_addr(dev);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200729 if (base == FDT_ADDR_T_NONE)
730 return -EINVAL;
731
732 priv->regbase = devm_ioremap(dev, base, SZ_2K);
733 if (!priv->regbase)
734 return -ENOMEM;
735
736#ifdef CONFIG_DM_REGULATOR
737 device_get_supply_regulator(dev, "vqmmc-supply", &priv->vqmmc_dev);
Marek Vasutc83da2e2018-06-13 08:02:55 +0200738 if (priv->vqmmc_dev)
739 regulator_set_value(priv->vqmmc_dev, 3300000);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200740#endif
741
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200742 ret = mmc_of_parse(dev, &plat->cfg);
743 if (ret < 0) {
744 dev_err(dev, "failed to parse host caps\n");
745 return ret;
746 }
747
748 plat->cfg.name = dev->name;
749 plat->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
750
751 if (quirks)
752 priv->caps = quirks;
753
754 priv->version = tmio_sd_readl(priv, TMIO_SD_VERSION) &
755 TMIO_SD_VERSION_IP;
756 dev_dbg(dev, "version %x\n", priv->version);
757 if (priv->version >= 0x10) {
758 priv->caps |= TMIO_SD_CAP_DMA_INTERNAL;
759 priv->caps |= TMIO_SD_CAP_DIV1024;
760 }
761
762 if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
763 NULL))
764 priv->caps |= TMIO_SD_CAP_NONREMOVABLE;
765
766 tmio_sd_host_init(priv);
767
Marek Vasut8ec6a042018-06-13 08:02:55 +0200768 mclk = tmio_sd_clk_get_rate(priv);
769
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200770 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200771 plat->cfg.f_min = mclk /
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200772 (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512);
Marek Vasut8ec6a042018-06-13 08:02:55 +0200773 plat->cfg.f_max = mclk;
Marek Vasutc453fe32019-03-18 23:43:10 +0100774 if (quirks & TMIO_SD_CAP_16BIT)
775 plat->cfg.b_max = U16_MAX; /* max value of TMIO_SD_SECCNT */
776 else
777 plat->cfg.b_max = U32_MAX; /* max value of TMIO_SD_SECCNT */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200778
779 upriv->mmc = &plat->mmc;
780
781 return 0;
782}