blob: 890c496b5357c3a2258ab1fe0091c51e52d6ba30 [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 Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass336d4612020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Marek Vasutcb0b6b02018-04-13 23:51:33 +020015#include <dm/pinctrl.h>
16#include <linux/compat.h>
Simon Glassc05ed002020-05-10 11:40:11 -060017#include <linux/delay.h>
Masahiro Yamada9d86b892020-02-14 16:40:19 +090018#include <linux/dma-mapping.h>
Marek Vasutcb0b6b02018-04-13 23:51:33 +020019#include <linux/io.h>
20#include <linux/sizes.h>
21#include <power/regulator.h>
22#include <asm/unaligned.h>
23
24#include "tmio-common.h"
25
26DECLARE_GLOBAL_DATA_PTR;
27
28static u64 tmio_sd_readq(struct tmio_sd_priv *priv, unsigned int reg)
29{
30 return readq(priv->regbase + (reg << 1));
31}
32
33static void tmio_sd_writeq(struct tmio_sd_priv *priv,
34 u64 val, unsigned int reg)
35{
36 writeq(val, priv->regbase + (reg << 1));
37}
38
39static u16 tmio_sd_readw(struct tmio_sd_priv *priv, unsigned int reg)
40{
41 return readw(priv->regbase + (reg >> 1));
42}
43
44static void tmio_sd_writew(struct tmio_sd_priv *priv,
45 u16 val, unsigned int reg)
46{
47 writew(val, priv->regbase + (reg >> 1));
48}
49
50u32 tmio_sd_readl(struct tmio_sd_priv *priv, unsigned int reg)
51{
52 u32 val;
53
54 if (priv->caps & TMIO_SD_CAP_64BIT)
55 return readl(priv->regbase + (reg << 1));
56 else if (priv->caps & TMIO_SD_CAP_16BIT) {
57 val = readw(priv->regbase + (reg >> 1)) & 0xffff;
58 if ((reg == TMIO_SD_RSP10) || (reg == TMIO_SD_RSP32) ||
59 (reg == TMIO_SD_RSP54) || (reg == TMIO_SD_RSP76)) {
60 val |= readw(priv->regbase + (reg >> 1) + 2) << 16;
61 }
62 return val;
63 } else
64 return readl(priv->regbase + reg);
65}
66
67void tmio_sd_writel(struct tmio_sd_priv *priv,
68 u32 val, unsigned int reg)
69{
70 if (priv->caps & TMIO_SD_CAP_64BIT)
71 writel(val, priv->regbase + (reg << 1));
72 else if (priv->caps & TMIO_SD_CAP_16BIT) {
73 writew(val & 0xffff, priv->regbase + (reg >> 1));
74 if (reg == TMIO_SD_INFO1 || reg == TMIO_SD_INFO1_MASK ||
75 reg == TMIO_SD_INFO2 || reg == TMIO_SD_INFO2_MASK ||
76 reg == TMIO_SD_ARG)
77 writew(val >> 16, priv->regbase + (reg >> 1) + 2);
78 } else
79 writel(val, priv->regbase + reg);
80}
81
Marek Vasut33633eb2018-10-30 22:05:54 +010082static int tmio_sd_check_error(struct udevice *dev, struct mmc_cmd *cmd)
Marek Vasutcb0b6b02018-04-13 23:51:33 +020083{
84 struct tmio_sd_priv *priv = dev_get_priv(dev);
85 u32 info2 = tmio_sd_readl(priv, TMIO_SD_INFO2);
86
87 if (info2 & TMIO_SD_INFO2_ERR_RTO) {
88 /*
89 * TIMEOUT must be returned for unsupported command. Do not
90 * display error log since this might be a part of sequence to
91 * distinguish between SD and MMC.
92 */
93 return -ETIMEDOUT;
94 }
95
96 if (info2 & TMIO_SD_INFO2_ERR_TO) {
97 dev_err(dev, "timeout error\n");
98 return -ETIMEDOUT;
99 }
100
101 if (info2 & (TMIO_SD_INFO2_ERR_END | TMIO_SD_INFO2_ERR_CRC |
102 TMIO_SD_INFO2_ERR_IDX)) {
Marek Vasut33633eb2018-10-30 22:05:54 +0100103 if ((cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK) &&
104 (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200))
105 dev_err(dev, "communication out of sync\n");
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200106 return -EILSEQ;
107 }
108
109 if (info2 & (TMIO_SD_INFO2_ERR_ILA | TMIO_SD_INFO2_ERR_ILR |
110 TMIO_SD_INFO2_ERR_ILW)) {
111 dev_err(dev, "illegal access\n");
112 return -EIO;
113 }
114
115 return 0;
116}
117
Marek Vasut33633eb2018-10-30 22:05:54 +0100118static int tmio_sd_wait_for_irq(struct udevice *dev, struct mmc_cmd *cmd,
119 unsigned int reg, u32 flag)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200120{
121 struct tmio_sd_priv *priv = dev_get_priv(dev);
122 long wait = 1000000;
123 int ret;
124
Marek Vasut631dbe02023-10-14 23:56:03 +0200125 while (true) {
126 if (tmio_sd_readl(priv, reg) & flag)
127 return tmio_sd_check_error(dev, cmd);
128
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200129 if (wait-- < 0) {
130 dev_err(dev, "timeout\n");
131 return -ETIMEDOUT;
132 }
133
Marek Vasut33633eb2018-10-30 22:05:54 +0100134 ret = tmio_sd_check_error(dev, cmd);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200135 if (ret)
136 return ret;
137
138 udelay(1);
139 }
140
141 return 0;
142}
143
144#define tmio_pio_read_fifo(__width, __suffix) \
145static void tmio_pio_read_fifo_##__width(struct tmio_sd_priv *priv, \
146 char *pbuf, uint blksz) \
147{ \
148 u##__width *buf = (u##__width *)pbuf; \
149 int i; \
150 \
151 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
152 for (i = 0; i < blksz / ((__width) / 8); i++) { \
153 *buf++ = tmio_sd_read##__suffix(priv, \
154 TMIO_SD_BUF); \
155 } \
156 } else { \
157 for (i = 0; i < blksz / ((__width) / 8); i++) { \
158 u##__width data; \
159 data = tmio_sd_read##__suffix(priv, \
160 TMIO_SD_BUF); \
161 put_unaligned(data, buf++); \
162 } \
163 } \
164}
165
166tmio_pio_read_fifo(64, q)
167tmio_pio_read_fifo(32, l)
168tmio_pio_read_fifo(16, w)
169
Marek Vasut33633eb2018-10-30 22:05:54 +0100170static int tmio_sd_pio_read_one_block(struct udevice *dev, struct mmc_cmd *cmd,
171 char *pbuf, uint blocksize)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200172{
173 struct tmio_sd_priv *priv = dev_get_priv(dev);
174 int ret;
175
176 /* wait until the buffer is filled with data */
Marek Vasut33633eb2018-10-30 22:05:54 +0100177 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
178 TMIO_SD_INFO2_BRE);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200179 if (ret)
180 return ret;
181
182 /*
183 * Clear the status flag _before_ read the buffer out because
184 * TMIO_SD_INFO2_BRE is edge-triggered, not level-triggered.
185 */
186 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
187
188 if (priv->caps & TMIO_SD_CAP_64BIT)
189 tmio_pio_read_fifo_64(priv, pbuf, blocksize);
190 else if (priv->caps & TMIO_SD_CAP_16BIT)
191 tmio_pio_read_fifo_16(priv, pbuf, blocksize);
192 else
193 tmio_pio_read_fifo_32(priv, pbuf, blocksize);
194
195 return 0;
196}
197
198#define tmio_pio_write_fifo(__width, __suffix) \
199static void tmio_pio_write_fifo_##__width(struct tmio_sd_priv *priv, \
200 const char *pbuf, uint blksz)\
201{ \
202 const u##__width *buf = (const u##__width *)pbuf; \
203 int i; \
204 \
205 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
206 for (i = 0; i < blksz / ((__width) / 8); i++) { \
207 tmio_sd_write##__suffix(priv, *buf++, \
208 TMIO_SD_BUF); \
209 } \
210 } else { \
211 for (i = 0; i < blksz / ((__width) / 8); i++) { \
212 u##__width data = get_unaligned(buf++); \
213 tmio_sd_write##__suffix(priv, data, \
214 TMIO_SD_BUF); \
215 } \
216 } \
217}
218
219tmio_pio_write_fifo(64, q)
220tmio_pio_write_fifo(32, l)
221tmio_pio_write_fifo(16, w)
222
Marek Vasut33633eb2018-10-30 22:05:54 +0100223static int tmio_sd_pio_write_one_block(struct udevice *dev, struct mmc_cmd *cmd,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200224 const char *pbuf, uint blocksize)
225{
226 struct tmio_sd_priv *priv = dev_get_priv(dev);
227 int ret;
228
229 /* wait until the buffer becomes empty */
Marek Vasut33633eb2018-10-30 22:05:54 +0100230 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
231 TMIO_SD_INFO2_BWE);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200232 if (ret)
233 return ret;
234
235 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
236
237 if (priv->caps & TMIO_SD_CAP_64BIT)
238 tmio_pio_write_fifo_64(priv, pbuf, blocksize);
239 else if (priv->caps & TMIO_SD_CAP_16BIT)
240 tmio_pio_write_fifo_16(priv, pbuf, blocksize);
241 else
242 tmio_pio_write_fifo_32(priv, pbuf, blocksize);
243
244 return 0;
245}
246
Marek Vasut33633eb2018-10-30 22:05:54 +0100247static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_cmd *cmd,
248 struct mmc_data *data)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200249{
250 const char *src = data->src;
251 char *dest = data->dest;
252 int i, ret;
253
254 for (i = 0; i < data->blocks; i++) {
255 if (data->flags & MMC_DATA_READ)
Marek Vasut33633eb2018-10-30 22:05:54 +0100256 ret = tmio_sd_pio_read_one_block(dev, cmd, dest,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200257 data->blocksize);
258 else
Marek Vasut33633eb2018-10-30 22:05:54 +0100259 ret = tmio_sd_pio_write_one_block(dev, cmd, src,
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200260 data->blocksize);
261 if (ret)
262 return ret;
263
264 if (data->flags & MMC_DATA_READ)
265 dest += data->blocksize;
266 else
267 src += data->blocksize;
268 }
269
270 return 0;
271}
272
273static void tmio_sd_dma_start(struct tmio_sd_priv *priv,
274 dma_addr_t dma_addr)
275{
276 u32 tmp;
277
278 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO1);
279 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO2);
280
281 /* enable DMA */
282 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
283 tmp |= TMIO_SD_EXTMODE_DMA_EN;
284 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
285
286 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_L);
287
288 /* suppress the warning "right shift count >= width of type" */
289 dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
290
291 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_H);
292
293 tmio_sd_writel(priv, TMIO_SD_DMA_CTL_START, TMIO_SD_DMA_CTL);
294}
295
296static int tmio_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
297 unsigned int blocks)
298{
299 struct tmio_sd_priv *priv = dev_get_priv(dev);
300 long wait = 1000000 + 10 * blocks;
301
302 while (!(tmio_sd_readl(priv, TMIO_SD_DMA_INFO1) & flag)) {
303 if (wait-- < 0) {
304 dev_err(dev, "timeout during DMA\n");
305 return -ETIMEDOUT;
306 }
307
308 udelay(10);
309 }
310
311 if (tmio_sd_readl(priv, TMIO_SD_DMA_INFO2)) {
312 dev_err(dev, "error during DMA\n");
313 return -EIO;
314 }
315
316 return 0;
317}
318
319static int tmio_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
320{
321 struct tmio_sd_priv *priv = dev_get_priv(dev);
322 size_t len = data->blocks * data->blocksize;
323 void *buf;
324 enum dma_data_direction dir;
325 dma_addr_t dma_addr;
326 u32 poll_flag, tmp;
327 int ret;
328
329 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
330
Marek Vasut8a73bef2021-01-03 11:38:25 +0100331 tmp |= priv->idma_bus_width;
332
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200333 if (data->flags & MMC_DATA_READ) {
334 buf = data->dest;
335 dir = DMA_FROM_DEVICE;
336 /*
337 * The DMA READ completion flag position differs on Socionext
338 * and Renesas SoCs. It is bit 20 on Socionext SoCs and using
Marek Vasut992bcf42019-01-11 23:45:54 +0100339 * bit 17 is a hardware bug and forbidden. It is either bit 17
340 * or bit 20 on Renesas SoCs, depending on SoC.
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200341 */
Marek Vasut992bcf42019-01-11 23:45:54 +0100342 poll_flag = priv->read_poll_flag;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200343 tmp |= TMIO_SD_DMA_MODE_DIR_RD;
344 } else {
345 buf = (void *)data->src;
346 dir = DMA_TO_DEVICE;
347 poll_flag = TMIO_SD_DMA_INFO1_END_WR;
348 tmp &= ~TMIO_SD_DMA_MODE_DIR_RD;
349 }
350
351 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
352
Vignesh Raghavendraeaa8b042020-01-16 14:23:46 +0530353 dma_addr = dma_map_single(buf, len, dir);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200354
355 tmio_sd_dma_start(priv, dma_addr);
356
357 ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
358
Marek Vasutcbbe6942019-01-11 23:38:07 +0100359 if (poll_flag == TMIO_SD_DMA_INFO1_END_RD)
360 udelay(1);
361
Masahiro Yamada950c5962020-02-14 16:40:18 +0900362 dma_unmap_single(dma_addr, len, dir);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200363
364 return ret;
365}
366
367/* check if the address is DMA'able */
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100368static bool tmio_sd_addr_is_dmaable(struct mmc_data *data)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200369{
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100370 uintptr_t addr = (uintptr_t)data->src;
Marek Vasut92bde152018-10-03 00:44:37 +0200371
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200372 if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN))
373 return false;
374
Hai Pham632a7b12023-02-28 22:24:06 +0100375 if (IS_ENABLED(CONFIG_RCAR_64)) {
Marek Vasut2769ddc2023-02-28 22:18:13 +0100376 if (!(data->flags & MMC_DATA_READ) && !IS_ALIGNED(addr, 128))
377 return false;
378 /* Gen3 DMA has 32bit limit */
379 if (sizeof(addr) > 4 && addr >> 32)
380 return false;
381 }
Marek Vasutbeced532018-10-03 00:46:24 +0200382
Marek Vasut2769ddc2023-02-28 22:18:13 +0100383#ifdef CONFIG_SPL_BUILD
Marek Vasut53614bc2023-04-08 19:35:37 +0200384 if (IS_ENABLED(CONFIG_ARCH_UNIPHIER) && !IS_ENABLED(CONFIG_ARM64)) {
Marek Vasut2769ddc2023-02-28 22:18:13 +0100385 /*
386 * For UniPhier ARMv7 SoCs, the stack is allocated in locked
387 * ways of L2, which is unreachable from the DMA engine.
388 */
389 if (addr < CONFIG_SPL_STACK)
390 return false;
391 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200392#endif
393
394 return true;
395}
396
397int tmio_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
398 struct mmc_data *data)
399{
400 struct tmio_sd_priv *priv = dev_get_priv(dev);
401 int ret;
402 u32 tmp;
403
404 if (tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_CBSY) {
405 dev_err(dev, "command busy\n");
406 return -EBUSY;
407 }
408
409 /* clear all status flags */
410 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
411 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
412
413 /* disable DMA once */
414 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
415 tmp &= ~TMIO_SD_EXTMODE_DMA_EN;
416 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
417
418 tmio_sd_writel(priv, cmd->cmdarg, TMIO_SD_ARG);
419
420 tmp = cmd->cmdidx;
421
422 if (data) {
423 tmio_sd_writel(priv, data->blocksize, TMIO_SD_SIZE);
424 tmio_sd_writel(priv, data->blocks, TMIO_SD_SECCNT);
425
426 /* Do not send CMD12 automatically */
427 tmp |= TMIO_SD_CMD_NOSTOP | TMIO_SD_CMD_DATA;
428
429 if (data->blocks > 1)
430 tmp |= TMIO_SD_CMD_MULTI;
431
432 if (data->flags & MMC_DATA_READ)
433 tmp |= TMIO_SD_CMD_RD;
434 }
435
436 /*
437 * Do not use the response type auto-detection on this hardware.
438 * CMD8, for example, has different response types on SD and eMMC,
439 * while this controller always assumes the response type for SD.
440 * Set the response type manually.
441 */
442 switch (cmd->resp_type) {
443 case MMC_RSP_NONE:
444 tmp |= TMIO_SD_CMD_RSP_NONE;
445 break;
446 case MMC_RSP_R1:
447 tmp |= TMIO_SD_CMD_RSP_R1;
448 break;
449 case MMC_RSP_R1b:
450 tmp |= TMIO_SD_CMD_RSP_R1B;
451 break;
452 case MMC_RSP_R2:
453 tmp |= TMIO_SD_CMD_RSP_R2;
454 break;
455 case MMC_RSP_R3:
456 tmp |= TMIO_SD_CMD_RSP_R3;
457 break;
458 default:
459 dev_err(dev, "unknown response type\n");
460 return -EINVAL;
461 }
462
463 dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
464 cmd->cmdidx, tmp, cmd->cmdarg);
465 tmio_sd_writel(priv, tmp, TMIO_SD_CMD);
466
Marek Vasut33633eb2018-10-30 22:05:54 +0100467 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
468 TMIO_SD_INFO1_RSP);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200469 if (ret)
470 return ret;
471
472 if (cmd->resp_type & MMC_RSP_136) {
473 u32 rsp_127_104 = tmio_sd_readl(priv, TMIO_SD_RSP76);
474 u32 rsp_103_72 = tmio_sd_readl(priv, TMIO_SD_RSP54);
475 u32 rsp_71_40 = tmio_sd_readl(priv, TMIO_SD_RSP32);
476 u32 rsp_39_8 = tmio_sd_readl(priv, TMIO_SD_RSP10);
477
478 cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
479 ((rsp_103_72 & 0xff000000) >> 24);
480 cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
481 ((rsp_71_40 & 0xff000000) >> 24);
482 cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
483 ((rsp_39_8 & 0xff000000) >> 24);
484 cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
485 } else {
486 /* bit 39-8 */
487 cmd->response[0] = tmio_sd_readl(priv, TMIO_SD_RSP10);
488 }
489
490 if (data) {
491 /* use DMA if the HW supports it and the buffer is aligned */
492 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL &&
Hiroyuki Yokoyama902af102020-03-07 17:32:59 +0100493 tmio_sd_addr_is_dmaable(data))
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200494 ret = tmio_sd_dma_xfer(dev, data);
495 else
Marek Vasut33633eb2018-10-30 22:05:54 +0100496 ret = tmio_sd_pio_xfer(dev, cmd, data);
Marek Vasutb22c8d02018-10-30 21:53:29 +0100497 if (ret)
498 return ret;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200499
Marek Vasut33633eb2018-10-30 22:05:54 +0100500 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
501 TMIO_SD_INFO1_CMP);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200502 if (ret)
503 return ret;
504 }
505
Marek Vasut33633eb2018-10-30 22:05:54 +0100506 return tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
Marek Vasutb22c8d02018-10-30 21:53:29 +0100507 TMIO_SD_INFO2_SCLKDIVEN);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200508}
509
510static int tmio_sd_set_bus_width(struct tmio_sd_priv *priv,
511 struct mmc *mmc)
512{
513 u32 val, tmp;
514
515 switch (mmc->bus_width) {
516 case 0:
517 case 1:
518 val = TMIO_SD_OPTION_WIDTH_1;
519 break;
520 case 4:
521 val = TMIO_SD_OPTION_WIDTH_4;
522 break;
523 case 8:
524 val = TMIO_SD_OPTION_WIDTH_8;
525 break;
526 default:
527 return -EINVAL;
528 }
529
530 tmp = tmio_sd_readl(priv, TMIO_SD_OPTION);
531 tmp &= ~TMIO_SD_OPTION_WIDTH_MASK;
532 tmp |= val;
533 tmio_sd_writel(priv, tmp, TMIO_SD_OPTION);
534
535 return 0;
536}
537
538static void tmio_sd_set_ddr_mode(struct tmio_sd_priv *priv,
539 struct mmc *mmc)
540{
541 u32 tmp;
542
543 tmp = tmio_sd_readl(priv, TMIO_SD_IF_MODE);
544 if (mmc->ddr_mode)
545 tmp |= TMIO_SD_IF_MODE_DDR;
546 else
547 tmp &= ~TMIO_SD_IF_MODE_DDR;
548 tmio_sd_writel(priv, tmp, TMIO_SD_IF_MODE);
549}
550
Marek Vasut8ec6a042018-06-13 08:02:55 +0200551static ulong tmio_sd_clk_get_rate(struct tmio_sd_priv *priv)
552{
553 return priv->clk_get_rate(priv);
554}
555
Marek Vasuted427da2018-11-15 22:01:33 +0100556static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv, struct mmc *mmc)
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200557{
558 unsigned int divisor;
Marek Vasuted427da2018-11-15 22:01:33 +0100559 u32 tmp, val = 0;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200560 ulong mclk;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200561
Marek Vasuted427da2018-11-15 22:01:33 +0100562 if (mmc->clock) {
563 mclk = tmio_sd_clk_get_rate(priv);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200564
Marek Vasuted427da2018-11-15 22:01:33 +0100565 divisor = DIV_ROUND_UP(mclk, mmc->clock);
Marek Vasut8ec6a042018-06-13 08:02:55 +0200566
Marek Vasuted427da2018-11-15 22:01:33 +0100567 /* Do not set divider to 0xff in DDR mode */
568 if (mmc->ddr_mode && (divisor == 1))
569 divisor = 2;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200570
Marek Vasuted427da2018-11-15 22:01:33 +0100571 if (divisor <= 1)
572 val = (priv->caps & TMIO_SD_CAP_RCAR) ?
573 TMIO_SD_CLKCTL_RCAR_DIV1 : TMIO_SD_CLKCTL_DIV1;
574 else if (divisor <= 2)
575 val = TMIO_SD_CLKCTL_DIV2;
576 else if (divisor <= 4)
577 val = TMIO_SD_CLKCTL_DIV4;
578 else if (divisor <= 8)
579 val = TMIO_SD_CLKCTL_DIV8;
580 else if (divisor <= 16)
581 val = TMIO_SD_CLKCTL_DIV16;
582 else if (divisor <= 32)
583 val = TMIO_SD_CLKCTL_DIV32;
584 else if (divisor <= 64)
585 val = TMIO_SD_CLKCTL_DIV64;
586 else if (divisor <= 128)
587 val = TMIO_SD_CLKCTL_DIV128;
588 else if (divisor <= 256)
589 val = TMIO_SD_CLKCTL_DIV256;
590 else if (divisor <= 512 || !(priv->caps & TMIO_SD_CAP_DIV1024))
591 val = TMIO_SD_CLKCTL_DIV512;
592 else
593 val = TMIO_SD_CLKCTL_DIV1024;
594 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200595
596 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
Marek Vasuted427da2018-11-15 22:01:33 +0100597 if (mmc->clock &&
598 !((tmp & TMIO_SD_CLKCTL_SCLKEN) &&
599 ((tmp & TMIO_SD_CLKCTL_DIV_MASK) == val))) {
600 /*
601 * Stop the clock before changing its rate
602 * to avoid a glitch signal
603 */
604 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
605 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200606
Marek Vasuted427da2018-11-15 22:01:33 +0100607 /* Change the clock rate. */
608 tmp &= ~TMIO_SD_CLKCTL_DIV_MASK;
609 tmp |= val;
610 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200611
Marek Vasuted427da2018-11-15 22:01:33 +0100612 /* Enable or Disable the clock */
613 if (mmc->clk_disable) {
Marek Vasut59d529a2018-06-13 08:02:55 +0200614 tmp |= TMIO_SD_CLKCTL_OFFEN;
615 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
Marek Vasuted427da2018-11-15 22:01:33 +0100616 } else {
617 tmp &= ~TMIO_SD_CLKCTL_OFFEN;
618 tmp |= TMIO_SD_CLKCTL_SCLKEN;
Marek Vasut59d529a2018-06-13 08:02:55 +0200619 }
Marek Vasuted427da2018-11-15 22:01:33 +0100620
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200621 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
622
623 udelay(1000);
624}
625
626static void tmio_sd_set_pins(struct udevice *dev)
627{
628 __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200629 struct tmio_sd_priv *priv = dev_get_priv(dev);
630
Marek Vasut2769ddc2023-02-28 22:18:13 +0100631 if (CONFIG_IS_ENABLED(DM_REGULATOR) && priv->vqmmc_dev) {
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200632 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
633 regulator_set_value(priv->vqmmc_dev, 1800000);
634 else
635 regulator_set_value(priv->vqmmc_dev, 3300000);
636 regulator_set_enable(priv->vqmmc_dev, true);
637 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200638
Marek Vasut2769ddc2023-02-28 22:18:13 +0100639 if (CONFIG_IS_ENABLED(PINCTRL)) {
640 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
641 pinctrl_select_state(dev, "state_uhs");
642 else
643 pinctrl_select_state(dev, "default");
644 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200645}
646
647int tmio_sd_set_ios(struct udevice *dev)
648{
649 struct tmio_sd_priv *priv = dev_get_priv(dev);
650 struct mmc *mmc = mmc_get_mmc_dev(dev);
651 int ret;
652
653 dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
654 mmc->clock, mmc->ddr_mode, mmc->bus_width);
655
Marek Vasut8171f992018-06-13 08:02:55 +0200656 tmio_sd_set_clk_rate(priv, mmc);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200657 ret = tmio_sd_set_bus_width(priv, mmc);
658 if (ret)
659 return ret;
660 tmio_sd_set_ddr_mode(priv, mmc);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200661 tmio_sd_set_pins(dev);
662
663 return 0;
664}
665
666int tmio_sd_get_cd(struct udevice *dev)
667{
668 struct tmio_sd_priv *priv = dev_get_priv(dev);
669
670 if (priv->caps & TMIO_SD_CAP_NONREMOVABLE)
671 return 1;
672
673 return !!(tmio_sd_readl(priv, TMIO_SD_INFO1) &
674 TMIO_SD_INFO1_CD);
675}
676
677static void tmio_sd_host_init(struct tmio_sd_priv *priv)
678{
679 u32 tmp;
680
681 /* soft reset of the host */
682 tmp = tmio_sd_readl(priv, TMIO_SD_SOFT_RST);
683 tmp &= ~TMIO_SD_SOFT_RST_RSTX;
684 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
685 tmp |= TMIO_SD_SOFT_RST_RSTX;
686 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
687
688 /* FIXME: implement eMMC hw_reset */
689
690 tmio_sd_writel(priv, TMIO_SD_STOP_SEC, TMIO_SD_STOP);
691
692 /*
693 * Connected to 32bit AXI.
694 * This register dropped backward compatibility at version 0x10.
695 * Write an appropriate value depending on the IP version.
696 */
Marek Vasut4c80f112019-02-14 15:16:24 +0100697 if (priv->version >= 0x10) {
698 if (priv->caps & TMIO_SD_CAP_64BIT)
Marek Vasut5d688842019-02-19 19:20:14 +0100699 tmio_sd_writel(priv, 0x000, TMIO_SD_HOST_MODE);
Marek Vasut4c80f112019-02-14 15:16:24 +0100700 else
701 tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE);
702 } else {
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200703 tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE);
Marek Vasut4c80f112019-02-14 15:16:24 +0100704 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200705
706 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL) {
707 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
708 tmp |= TMIO_SD_DMA_MODE_ADDR_INC;
Marek Vasut8a73bef2021-01-03 11:38:25 +0100709 tmp |= priv->idma_bus_width;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200710 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
711 }
712}
713
714int tmio_sd_bind(struct udevice *dev)
715{
Simon Glassc69cda22020-12-03 16:55:20 -0700716 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200717
718 return mmc_bind(dev, &plat->mmc, &plat->cfg);
719}
720
721int tmio_sd_probe(struct udevice *dev, u32 quirks)
722{
Simon Glassc69cda22020-12-03 16:55:20 -0700723 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200724 struct tmio_sd_priv *priv = dev_get_priv(dev);
725 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
726 fdt_addr_t base;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200727 ulong mclk;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200728 int ret;
729
Masahiro Yamada25484932020-07-17 14:36:48 +0900730 base = dev_read_addr(dev);
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200731 if (base == FDT_ADDR_T_NONE)
732 return -EINVAL;
733
734 priv->regbase = devm_ioremap(dev, base, SZ_2K);
735 if (!priv->regbase)
736 return -ENOMEM;
737
Marek Vasut2769ddc2023-02-28 22:18:13 +0100738 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
739 device_get_supply_regulator(dev, "vqmmc-supply",
740 &priv->vqmmc_dev);
741 if (priv->vqmmc_dev)
742 regulator_set_value(priv->vqmmc_dev, 3300000);
743 }
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200744
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200745 ret = mmc_of_parse(dev, &plat->cfg);
746 if (ret < 0) {
747 dev_err(dev, "failed to parse host caps\n");
748 return ret;
749 }
750
751 plat->cfg.name = dev->name;
752 plat->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
753
754 if (quirks)
755 priv->caps = quirks;
756
757 priv->version = tmio_sd_readl(priv, TMIO_SD_VERSION) &
758 TMIO_SD_VERSION_IP;
759 dev_dbg(dev, "version %x\n", priv->version);
760 if (priv->version >= 0x10) {
761 priv->caps |= TMIO_SD_CAP_DMA_INTERNAL;
Marek Vasut1332bdc2023-10-22 23:40:43 +0200762 if (!(priv->caps & TMIO_SD_CAP_RCAR))
763 priv->caps |= TMIO_SD_CAP_DIV1024;
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200764 }
765
766 if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
767 NULL))
768 priv->caps |= TMIO_SD_CAP_NONREMOVABLE;
769
770 tmio_sd_host_init(priv);
771
Marek Vasut8ec6a042018-06-13 08:02:55 +0200772 mclk = tmio_sd_clk_get_rate(priv);
773
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200774 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasut8ec6a042018-06-13 08:02:55 +0200775 plat->cfg.f_min = mclk /
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200776 (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512);
Marek Vasut8ec6a042018-06-13 08:02:55 +0200777 plat->cfg.f_max = mclk;
Marek Vasutc453fe32019-03-18 23:43:10 +0100778 if (quirks & TMIO_SD_CAP_16BIT)
779 plat->cfg.b_max = U16_MAX; /* max value of TMIO_SD_SECCNT */
780 else
781 plat->cfg.b_max = U32_MAX; /* max value of TMIO_SD_SECCNT */
Marek Vasutcb0b6b02018-04-13 23:51:33 +0200782
783 upriv->mmc = &plat->mmc;
784
785 return 0;
786}