blob: d8b6a4a8821d26ae950411f7ff404658ef2326b7 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutfd83e762018-04-13 23:51:33 +02002/*
3 * Copyright (C) 2016 Socionext Inc.
4 * Author: Masahiro Yamada <yamada.masahiro@socionext.com>
Marek Vasutfd83e762018-04-13 23:51:33 +02005 */
6
7#include <common.h>
8#include <clk.h>
Simon Glass63334482019-11-14 12:57:39 -07009#include <cpu_func.h>
Marek Vasutfd83e762018-04-13 23:51:33 +020010#include <fdtdec.h>
11#include <mmc.h>
12#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Simon Glass9bc15642020-02-03 07:36:16 -070014#include <dm/device_compat.h>
Marek Vasutfd83e762018-04-13 23:51:33 +020015#include <dm/pinctrl.h>
16#include <linux/compat.h>
Simon Glassdbd79542020-05-10 11:40:11 -060017#include <linux/delay.h>
Masahiro Yamada6373a172020-02-14 16:40:19 +090018#include <linux/dma-mapping.h>
Marek Vasutfd83e762018-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 Vasutdc86e912018-10-30 22:05:54 +010082static int tmio_sd_check_error(struct udevice *dev, struct mmc_cmd *cmd)
Marek Vasutfd83e762018-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 Vasutdc86e912018-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 Vasutfd83e762018-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 Vasutdc86e912018-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 Vasutfd83e762018-04-13 23:51:33 +0200120{
121 struct tmio_sd_priv *priv = dev_get_priv(dev);
122 long wait = 1000000;
123 int ret;
124
125 while (!(tmio_sd_readl(priv, reg) & flag)) {
126 if (wait-- < 0) {
127 dev_err(dev, "timeout\n");
128 return -ETIMEDOUT;
129 }
130
Marek Vasutdc86e912018-10-30 22:05:54 +0100131 ret = tmio_sd_check_error(dev, cmd);
Marek Vasutfd83e762018-04-13 23:51:33 +0200132 if (ret)
133 return ret;
134
135 udelay(1);
136 }
137
138 return 0;
139}
140
141#define tmio_pio_read_fifo(__width, __suffix) \
142static void tmio_pio_read_fifo_##__width(struct tmio_sd_priv *priv, \
143 char *pbuf, uint blksz) \
144{ \
145 u##__width *buf = (u##__width *)pbuf; \
146 int i; \
147 \
148 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
149 for (i = 0; i < blksz / ((__width) / 8); i++) { \
150 *buf++ = tmio_sd_read##__suffix(priv, \
151 TMIO_SD_BUF); \
152 } \
153 } else { \
154 for (i = 0; i < blksz / ((__width) / 8); i++) { \
155 u##__width data; \
156 data = tmio_sd_read##__suffix(priv, \
157 TMIO_SD_BUF); \
158 put_unaligned(data, buf++); \
159 } \
160 } \
161}
162
163tmio_pio_read_fifo(64, q)
164tmio_pio_read_fifo(32, l)
165tmio_pio_read_fifo(16, w)
166
Marek Vasutdc86e912018-10-30 22:05:54 +0100167static int tmio_sd_pio_read_one_block(struct udevice *dev, struct mmc_cmd *cmd,
168 char *pbuf, uint blocksize)
Marek Vasutfd83e762018-04-13 23:51:33 +0200169{
170 struct tmio_sd_priv *priv = dev_get_priv(dev);
171 int ret;
172
173 /* wait until the buffer is filled with data */
Marek Vasutdc86e912018-10-30 22:05:54 +0100174 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
175 TMIO_SD_INFO2_BRE);
Marek Vasutfd83e762018-04-13 23:51:33 +0200176 if (ret)
177 return ret;
178
179 /*
180 * Clear the status flag _before_ read the buffer out because
181 * TMIO_SD_INFO2_BRE is edge-triggered, not level-triggered.
182 */
183 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
184
185 if (priv->caps & TMIO_SD_CAP_64BIT)
186 tmio_pio_read_fifo_64(priv, pbuf, blocksize);
187 else if (priv->caps & TMIO_SD_CAP_16BIT)
188 tmio_pio_read_fifo_16(priv, pbuf, blocksize);
189 else
190 tmio_pio_read_fifo_32(priv, pbuf, blocksize);
191
192 return 0;
193}
194
195#define tmio_pio_write_fifo(__width, __suffix) \
196static void tmio_pio_write_fifo_##__width(struct tmio_sd_priv *priv, \
197 const char *pbuf, uint blksz)\
198{ \
199 const u##__width *buf = (const u##__width *)pbuf; \
200 int i; \
201 \
202 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
203 for (i = 0; i < blksz / ((__width) / 8); i++) { \
204 tmio_sd_write##__suffix(priv, *buf++, \
205 TMIO_SD_BUF); \
206 } \
207 } else { \
208 for (i = 0; i < blksz / ((__width) / 8); i++) { \
209 u##__width data = get_unaligned(buf++); \
210 tmio_sd_write##__suffix(priv, data, \
211 TMIO_SD_BUF); \
212 } \
213 } \
214}
215
216tmio_pio_write_fifo(64, q)
217tmio_pio_write_fifo(32, l)
218tmio_pio_write_fifo(16, w)
219
Marek Vasutdc86e912018-10-30 22:05:54 +0100220static int tmio_sd_pio_write_one_block(struct udevice *dev, struct mmc_cmd *cmd,
Marek Vasutfd83e762018-04-13 23:51:33 +0200221 const char *pbuf, uint blocksize)
222{
223 struct tmio_sd_priv *priv = dev_get_priv(dev);
224 int ret;
225
226 /* wait until the buffer becomes empty */
Marek Vasutdc86e912018-10-30 22:05:54 +0100227 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
228 TMIO_SD_INFO2_BWE);
Marek Vasutfd83e762018-04-13 23:51:33 +0200229 if (ret)
230 return ret;
231
232 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
233
234 if (priv->caps & TMIO_SD_CAP_64BIT)
235 tmio_pio_write_fifo_64(priv, pbuf, blocksize);
236 else if (priv->caps & TMIO_SD_CAP_16BIT)
237 tmio_pio_write_fifo_16(priv, pbuf, blocksize);
238 else
239 tmio_pio_write_fifo_32(priv, pbuf, blocksize);
240
241 return 0;
242}
243
Marek Vasutdc86e912018-10-30 22:05:54 +0100244static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_cmd *cmd,
245 struct mmc_data *data)
Marek Vasutfd83e762018-04-13 23:51:33 +0200246{
247 const char *src = data->src;
248 char *dest = data->dest;
249 int i, ret;
250
251 for (i = 0; i < data->blocks; i++) {
252 if (data->flags & MMC_DATA_READ)
Marek Vasutdc86e912018-10-30 22:05:54 +0100253 ret = tmio_sd_pio_read_one_block(dev, cmd, dest,
Marek Vasutfd83e762018-04-13 23:51:33 +0200254 data->blocksize);
255 else
Marek Vasutdc86e912018-10-30 22:05:54 +0100256 ret = tmio_sd_pio_write_one_block(dev, cmd, src,
Marek Vasutfd83e762018-04-13 23:51:33 +0200257 data->blocksize);
258 if (ret)
259 return ret;
260
261 if (data->flags & MMC_DATA_READ)
262 dest += data->blocksize;
263 else
264 src += data->blocksize;
265 }
266
267 return 0;
268}
269
270static void tmio_sd_dma_start(struct tmio_sd_priv *priv,
271 dma_addr_t dma_addr)
272{
273 u32 tmp;
274
275 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO1);
276 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO2);
277
278 /* enable DMA */
279 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
280 tmp |= TMIO_SD_EXTMODE_DMA_EN;
281 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
282
283 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_L);
284
285 /* suppress the warning "right shift count >= width of type" */
286 dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
287
288 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_H);
289
290 tmio_sd_writel(priv, TMIO_SD_DMA_CTL_START, TMIO_SD_DMA_CTL);
291}
292
293static int tmio_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
294 unsigned int blocks)
295{
296 struct tmio_sd_priv *priv = dev_get_priv(dev);
297 long wait = 1000000 + 10 * blocks;
298
299 while (!(tmio_sd_readl(priv, TMIO_SD_DMA_INFO1) & flag)) {
300 if (wait-- < 0) {
301 dev_err(dev, "timeout during DMA\n");
302 return -ETIMEDOUT;
303 }
304
305 udelay(10);
306 }
307
308 if (tmio_sd_readl(priv, TMIO_SD_DMA_INFO2)) {
309 dev_err(dev, "error during DMA\n");
310 return -EIO;
311 }
312
313 return 0;
314}
315
316static int tmio_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
317{
318 struct tmio_sd_priv *priv = dev_get_priv(dev);
319 size_t len = data->blocks * data->blocksize;
320 void *buf;
321 enum dma_data_direction dir;
322 dma_addr_t dma_addr;
323 u32 poll_flag, tmp;
324 int ret;
325
326 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
327
Marek Vasutd6e2f872021-01-03 11:38:25 +0100328 tmp |= priv->idma_bus_width;
329
Marek Vasutfd83e762018-04-13 23:51:33 +0200330 if (data->flags & MMC_DATA_READ) {
331 buf = data->dest;
332 dir = DMA_FROM_DEVICE;
333 /*
334 * The DMA READ completion flag position differs on Socionext
335 * and Renesas SoCs. It is bit 20 on Socionext SoCs and using
Marek Vasute9a28222019-01-11 23:45:54 +0100336 * bit 17 is a hardware bug and forbidden. It is either bit 17
337 * or bit 20 on Renesas SoCs, depending on SoC.
Marek Vasutfd83e762018-04-13 23:51:33 +0200338 */
Marek Vasute9a28222019-01-11 23:45:54 +0100339 poll_flag = priv->read_poll_flag;
Marek Vasutfd83e762018-04-13 23:51:33 +0200340 tmp |= TMIO_SD_DMA_MODE_DIR_RD;
341 } else {
342 buf = (void *)data->src;
343 dir = DMA_TO_DEVICE;
344 poll_flag = TMIO_SD_DMA_INFO1_END_WR;
345 tmp &= ~TMIO_SD_DMA_MODE_DIR_RD;
346 }
347
348 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
349
Vignesh Raghavendra0892e712020-01-16 14:23:46 +0530350 dma_addr = dma_map_single(buf, len, dir);
Marek Vasutfd83e762018-04-13 23:51:33 +0200351
352 tmio_sd_dma_start(priv, dma_addr);
353
354 ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
355
Marek Vasut314b9ca2019-01-11 23:38:07 +0100356 if (poll_flag == TMIO_SD_DMA_INFO1_END_RD)
357 udelay(1);
358
Masahiro Yamada05a5dba2020-02-14 16:40:18 +0900359 dma_unmap_single(dma_addr, len, dir);
Marek Vasutfd83e762018-04-13 23:51:33 +0200360
361 return ret;
362}
363
364/* check if the address is DMA'able */
Hiroyuki Yokoyamaed5aa492020-03-07 17:32:59 +0100365static bool tmio_sd_addr_is_dmaable(struct mmc_data *data)
Marek Vasutfd83e762018-04-13 23:51:33 +0200366{
Hiroyuki Yokoyamaed5aa492020-03-07 17:32:59 +0100367 uintptr_t addr = (uintptr_t)data->src;
Marek Vasutc7da6e342018-10-03 00:44:37 +0200368
Marek Vasutfd83e762018-04-13 23:51:33 +0200369 if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN))
370 return false;
371
Marek Vasutd4be38e2023-02-28 22:18:13 +0100372 if (IS_ENABLED(CONFIG_RCAR_GEN3)) {
373 if (!(data->flags & MMC_DATA_READ) && !IS_ALIGNED(addr, 128))
374 return false;
375 /* Gen3 DMA has 32bit limit */
376 if (sizeof(addr) > 4 && addr >> 32)
377 return false;
378 }
Marek Vasut967db0e2018-10-03 00:46:24 +0200379
Marek Vasutd4be38e2023-02-28 22:18:13 +0100380#ifdef CONFIG_SPL_BUILD
381 if (IS_ENABLED(CONFIG_ARCH_UNIPHIER) && !CONFIG_IS_ENABLED(CONFIG_ARM64)) {
382 /*
383 * For UniPhier ARMv7 SoCs, the stack is allocated in locked
384 * ways of L2, which is unreachable from the DMA engine.
385 */
386 if (addr < CONFIG_SPL_STACK)
387 return false;
388 }
Marek Vasutfd83e762018-04-13 23:51:33 +0200389#endif
390
391 return true;
392}
393
394int tmio_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
395 struct mmc_data *data)
396{
397 struct tmio_sd_priv *priv = dev_get_priv(dev);
398 int ret;
399 u32 tmp;
400
401 if (tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_CBSY) {
402 dev_err(dev, "command busy\n");
403 return -EBUSY;
404 }
405
406 /* clear all status flags */
407 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
408 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
409
410 /* disable DMA once */
411 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
412 tmp &= ~TMIO_SD_EXTMODE_DMA_EN;
413 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
414
415 tmio_sd_writel(priv, cmd->cmdarg, TMIO_SD_ARG);
416
417 tmp = cmd->cmdidx;
418
419 if (data) {
420 tmio_sd_writel(priv, data->blocksize, TMIO_SD_SIZE);
421 tmio_sd_writel(priv, data->blocks, TMIO_SD_SECCNT);
422
423 /* Do not send CMD12 automatically */
424 tmp |= TMIO_SD_CMD_NOSTOP | TMIO_SD_CMD_DATA;
425
426 if (data->blocks > 1)
427 tmp |= TMIO_SD_CMD_MULTI;
428
429 if (data->flags & MMC_DATA_READ)
430 tmp |= TMIO_SD_CMD_RD;
431 }
432
433 /*
434 * Do not use the response type auto-detection on this hardware.
435 * CMD8, for example, has different response types on SD and eMMC,
436 * while this controller always assumes the response type for SD.
437 * Set the response type manually.
438 */
439 switch (cmd->resp_type) {
440 case MMC_RSP_NONE:
441 tmp |= TMIO_SD_CMD_RSP_NONE;
442 break;
443 case MMC_RSP_R1:
444 tmp |= TMIO_SD_CMD_RSP_R1;
445 break;
446 case MMC_RSP_R1b:
447 tmp |= TMIO_SD_CMD_RSP_R1B;
448 break;
449 case MMC_RSP_R2:
450 tmp |= TMIO_SD_CMD_RSP_R2;
451 break;
452 case MMC_RSP_R3:
453 tmp |= TMIO_SD_CMD_RSP_R3;
454 break;
455 default:
456 dev_err(dev, "unknown response type\n");
457 return -EINVAL;
458 }
459
460 dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
461 cmd->cmdidx, tmp, cmd->cmdarg);
462 tmio_sd_writel(priv, tmp, TMIO_SD_CMD);
463
Marek Vasutdc86e912018-10-30 22:05:54 +0100464 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
465 TMIO_SD_INFO1_RSP);
Marek Vasutfd83e762018-04-13 23:51:33 +0200466 if (ret)
467 return ret;
468
469 if (cmd->resp_type & MMC_RSP_136) {
470 u32 rsp_127_104 = tmio_sd_readl(priv, TMIO_SD_RSP76);
471 u32 rsp_103_72 = tmio_sd_readl(priv, TMIO_SD_RSP54);
472 u32 rsp_71_40 = tmio_sd_readl(priv, TMIO_SD_RSP32);
473 u32 rsp_39_8 = tmio_sd_readl(priv, TMIO_SD_RSP10);
474
475 cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
476 ((rsp_103_72 & 0xff000000) >> 24);
477 cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
478 ((rsp_71_40 & 0xff000000) >> 24);
479 cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
480 ((rsp_39_8 & 0xff000000) >> 24);
481 cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
482 } else {
483 /* bit 39-8 */
484 cmd->response[0] = tmio_sd_readl(priv, TMIO_SD_RSP10);
485 }
486
487 if (data) {
488 /* use DMA if the HW supports it and the buffer is aligned */
489 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL &&
Hiroyuki Yokoyamaed5aa492020-03-07 17:32:59 +0100490 tmio_sd_addr_is_dmaable(data))
Marek Vasutfd83e762018-04-13 23:51:33 +0200491 ret = tmio_sd_dma_xfer(dev, data);
492 else
Marek Vasutdc86e912018-10-30 22:05:54 +0100493 ret = tmio_sd_pio_xfer(dev, cmd, data);
Marek Vasut6b4a8ba2018-10-30 21:53:29 +0100494 if (ret)
495 return ret;
Marek Vasutfd83e762018-04-13 23:51:33 +0200496
Marek Vasutdc86e912018-10-30 22:05:54 +0100497 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
498 TMIO_SD_INFO1_CMP);
Marek Vasutfd83e762018-04-13 23:51:33 +0200499 if (ret)
500 return ret;
501 }
502
Marek Vasutdc86e912018-10-30 22:05:54 +0100503 return tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
Marek Vasut6b4a8ba2018-10-30 21:53:29 +0100504 TMIO_SD_INFO2_SCLKDIVEN);
Marek Vasutfd83e762018-04-13 23:51:33 +0200505}
506
507static int tmio_sd_set_bus_width(struct tmio_sd_priv *priv,
508 struct mmc *mmc)
509{
510 u32 val, tmp;
511
512 switch (mmc->bus_width) {
513 case 0:
514 case 1:
515 val = TMIO_SD_OPTION_WIDTH_1;
516 break;
517 case 4:
518 val = TMIO_SD_OPTION_WIDTH_4;
519 break;
520 case 8:
521 val = TMIO_SD_OPTION_WIDTH_8;
522 break;
523 default:
524 return -EINVAL;
525 }
526
527 tmp = tmio_sd_readl(priv, TMIO_SD_OPTION);
528 tmp &= ~TMIO_SD_OPTION_WIDTH_MASK;
529 tmp |= val;
530 tmio_sd_writel(priv, tmp, TMIO_SD_OPTION);
531
532 return 0;
533}
534
535static void tmio_sd_set_ddr_mode(struct tmio_sd_priv *priv,
536 struct mmc *mmc)
537{
538 u32 tmp;
539
540 tmp = tmio_sd_readl(priv, TMIO_SD_IF_MODE);
541 if (mmc->ddr_mode)
542 tmp |= TMIO_SD_IF_MODE_DDR;
543 else
544 tmp &= ~TMIO_SD_IF_MODE_DDR;
545 tmio_sd_writel(priv, tmp, TMIO_SD_IF_MODE);
546}
547
Marek Vasutda90a1b2018-06-13 08:02:55 +0200548static ulong tmio_sd_clk_get_rate(struct tmio_sd_priv *priv)
549{
550 return priv->clk_get_rate(priv);
551}
552
Marek Vasut9763b182018-11-15 22:01:33 +0100553static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv, struct mmc *mmc)
Marek Vasutfd83e762018-04-13 23:51:33 +0200554{
555 unsigned int divisor;
Marek Vasut9763b182018-11-15 22:01:33 +0100556 u32 tmp, val = 0;
Marek Vasutda90a1b2018-06-13 08:02:55 +0200557 ulong mclk;
Marek Vasutfd83e762018-04-13 23:51:33 +0200558
Marek Vasut9763b182018-11-15 22:01:33 +0100559 if (mmc->clock) {
560 mclk = tmio_sd_clk_get_rate(priv);
Marek Vasutfd83e762018-04-13 23:51:33 +0200561
Marek Vasut9763b182018-11-15 22:01:33 +0100562 divisor = DIV_ROUND_UP(mclk, mmc->clock);
Marek Vasutda90a1b2018-06-13 08:02:55 +0200563
Marek Vasut9763b182018-11-15 22:01:33 +0100564 /* Do not set divider to 0xff in DDR mode */
565 if (mmc->ddr_mode && (divisor == 1))
566 divisor = 2;
Marek Vasutfd83e762018-04-13 23:51:33 +0200567
Marek Vasut9763b182018-11-15 22:01:33 +0100568 if (divisor <= 1)
569 val = (priv->caps & TMIO_SD_CAP_RCAR) ?
570 TMIO_SD_CLKCTL_RCAR_DIV1 : TMIO_SD_CLKCTL_DIV1;
571 else if (divisor <= 2)
572 val = TMIO_SD_CLKCTL_DIV2;
573 else if (divisor <= 4)
574 val = TMIO_SD_CLKCTL_DIV4;
575 else if (divisor <= 8)
576 val = TMIO_SD_CLKCTL_DIV8;
577 else if (divisor <= 16)
578 val = TMIO_SD_CLKCTL_DIV16;
579 else if (divisor <= 32)
580 val = TMIO_SD_CLKCTL_DIV32;
581 else if (divisor <= 64)
582 val = TMIO_SD_CLKCTL_DIV64;
583 else if (divisor <= 128)
584 val = TMIO_SD_CLKCTL_DIV128;
585 else if (divisor <= 256)
586 val = TMIO_SD_CLKCTL_DIV256;
587 else if (divisor <= 512 || !(priv->caps & TMIO_SD_CAP_DIV1024))
588 val = TMIO_SD_CLKCTL_DIV512;
589 else
590 val = TMIO_SD_CLKCTL_DIV1024;
591 }
Marek Vasutfd83e762018-04-13 23:51:33 +0200592
593 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
Marek Vasut9763b182018-11-15 22:01:33 +0100594 if (mmc->clock &&
595 !((tmp & TMIO_SD_CLKCTL_SCLKEN) &&
596 ((tmp & TMIO_SD_CLKCTL_DIV_MASK) == val))) {
597 /*
598 * Stop the clock before changing its rate
599 * to avoid a glitch signal
600 */
601 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
602 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
Marek Vasutfd83e762018-04-13 23:51:33 +0200603
Marek Vasut9763b182018-11-15 22:01:33 +0100604 /* Change the clock rate. */
605 tmp &= ~TMIO_SD_CLKCTL_DIV_MASK;
606 tmp |= val;
607 }
Marek Vasutfd83e762018-04-13 23:51:33 +0200608
Marek Vasut9763b182018-11-15 22:01:33 +0100609 /* Enable or Disable the clock */
610 if (mmc->clk_disable) {
Marek Vasut5abfb132018-06-13 08:02:55 +0200611 tmp |= TMIO_SD_CLKCTL_OFFEN;
612 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
Marek Vasut9763b182018-11-15 22:01:33 +0100613 } else {
614 tmp &= ~TMIO_SD_CLKCTL_OFFEN;
615 tmp |= TMIO_SD_CLKCTL_SCLKEN;
Marek Vasut5abfb132018-06-13 08:02:55 +0200616 }
Marek Vasut9763b182018-11-15 22:01:33 +0100617
Marek Vasutfd83e762018-04-13 23:51:33 +0200618 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
619
620 udelay(1000);
621}
622
623static void tmio_sd_set_pins(struct udevice *dev)
624{
625 __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev);
Marek Vasutfd83e762018-04-13 23:51:33 +0200626 struct tmio_sd_priv *priv = dev_get_priv(dev);
627
Marek Vasutd4be38e2023-02-28 22:18:13 +0100628 if (CONFIG_IS_ENABLED(DM_REGULATOR) && priv->vqmmc_dev) {
Marek Vasutfd83e762018-04-13 23:51:33 +0200629 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 }
Marek Vasutfd83e762018-04-13 23:51:33 +0200635
Marek Vasutd4be38e2023-02-28 22:18:13 +0100636 if (CONFIG_IS_ENABLED(PINCTRL)) {
637 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
638 pinctrl_select_state(dev, "state_uhs");
639 else
640 pinctrl_select_state(dev, "default");
641 }
Marek Vasutfd83e762018-04-13 23:51:33 +0200642}
643
644int tmio_sd_set_ios(struct udevice *dev)
645{
646 struct tmio_sd_priv *priv = dev_get_priv(dev);
647 struct mmc *mmc = mmc_get_mmc_dev(dev);
648 int ret;
649
650 dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
651 mmc->clock, mmc->ddr_mode, mmc->bus_width);
652
Marek Vasutbfe04e02018-06-13 08:02:55 +0200653 tmio_sd_set_clk_rate(priv, mmc);
Marek Vasutfd83e762018-04-13 23:51:33 +0200654 ret = tmio_sd_set_bus_width(priv, mmc);
655 if (ret)
656 return ret;
657 tmio_sd_set_ddr_mode(priv, mmc);
Marek Vasutfd83e762018-04-13 23:51:33 +0200658 tmio_sd_set_pins(dev);
659
660 return 0;
661}
662
663int tmio_sd_get_cd(struct udevice *dev)
664{
665 struct tmio_sd_priv *priv = dev_get_priv(dev);
666
667 if (priv->caps & TMIO_SD_CAP_NONREMOVABLE)
668 return 1;
669
670 return !!(tmio_sd_readl(priv, TMIO_SD_INFO1) &
671 TMIO_SD_INFO1_CD);
672}
673
674static void tmio_sd_host_init(struct tmio_sd_priv *priv)
675{
676 u32 tmp;
677
678 /* soft reset of the host */
679 tmp = tmio_sd_readl(priv, TMIO_SD_SOFT_RST);
680 tmp &= ~TMIO_SD_SOFT_RST_RSTX;
681 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
682 tmp |= TMIO_SD_SOFT_RST_RSTX;
683 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
684
685 /* FIXME: implement eMMC hw_reset */
686
687 tmio_sd_writel(priv, TMIO_SD_STOP_SEC, TMIO_SD_STOP);
688
689 /*
690 * Connected to 32bit AXI.
691 * This register dropped backward compatibility at version 0x10.
692 * Write an appropriate value depending on the IP version.
693 */
Marek Vasut2a43f882019-02-14 15:16:24 +0100694 if (priv->version >= 0x10) {
695 if (priv->caps & TMIO_SD_CAP_64BIT)
Marek Vasut1a4abf52019-02-19 19:20:14 +0100696 tmio_sd_writel(priv, 0x000, TMIO_SD_HOST_MODE);
Marek Vasut2a43f882019-02-14 15:16:24 +0100697 else
698 tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE);
699 } else {
Marek Vasutfd83e762018-04-13 23:51:33 +0200700 tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE);
Marek Vasut2a43f882019-02-14 15:16:24 +0100701 }
Marek Vasutfd83e762018-04-13 23:51:33 +0200702
703 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL) {
704 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
705 tmp |= TMIO_SD_DMA_MODE_ADDR_INC;
Marek Vasutd6e2f872021-01-03 11:38:25 +0100706 tmp |= priv->idma_bus_width;
Marek Vasutfd83e762018-04-13 23:51:33 +0200707 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
708 }
709}
710
711int tmio_sd_bind(struct udevice *dev)
712{
Simon Glassfa20e932020-12-03 16:55:20 -0700713 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasutfd83e762018-04-13 23:51:33 +0200714
715 return mmc_bind(dev, &plat->mmc, &plat->cfg);
716}
717
718int tmio_sd_probe(struct udevice *dev, u32 quirks)
719{
Simon Glassfa20e932020-12-03 16:55:20 -0700720 struct tmio_sd_plat *plat = dev_get_plat(dev);
Marek Vasutfd83e762018-04-13 23:51:33 +0200721 struct tmio_sd_priv *priv = dev_get_priv(dev);
722 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
723 fdt_addr_t base;
Marek Vasutda90a1b2018-06-13 08:02:55 +0200724 ulong mclk;
Marek Vasutfd83e762018-04-13 23:51:33 +0200725 int ret;
726
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900727 base = dev_read_addr(dev);
Marek Vasutfd83e762018-04-13 23:51:33 +0200728 if (base == FDT_ADDR_T_NONE)
729 return -EINVAL;
730
731 priv->regbase = devm_ioremap(dev, base, SZ_2K);
732 if (!priv->regbase)
733 return -ENOMEM;
734
Marek Vasutd4be38e2023-02-28 22:18:13 +0100735 if (CONFIG_IS_ENABLED(DM_REGULATOR)) {
736 device_get_supply_regulator(dev, "vqmmc-supply",
737 &priv->vqmmc_dev);
738 if (priv->vqmmc_dev)
739 regulator_set_value(priv->vqmmc_dev, 3300000);
740 }
Marek Vasutfd83e762018-04-13 23:51:33 +0200741
Marek Vasutfd83e762018-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 Vasutda90a1b2018-06-13 08:02:55 +0200768 mclk = tmio_sd_clk_get_rate(priv);
769
Marek Vasutfd83e762018-04-13 23:51:33 +0200770 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasutda90a1b2018-06-13 08:02:55 +0200771 plat->cfg.f_min = mclk /
Marek Vasutfd83e762018-04-13 23:51:33 +0200772 (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512);
Marek Vasutda90a1b2018-06-13 08:02:55 +0200773 plat->cfg.f_max = mclk;
Marek Vasut6d6e6fe2019-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 Vasutfd83e762018-04-13 23:51:33 +0200778
779 upriv->mmc = &plat->mmc;
780
781 return 0;
782}