blob: f1ce019db5a6cbd7da762cfdf8ebf7a1bcb9ad4c [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>
9#include <fdtdec.h>
10#include <mmc.h>
11#include <dm.h>
12#include <dm/pinctrl.h>
13#include <linux/compat.h>
14#include <linux/dma-direction.h>
15#include <linux/io.h>
16#include <linux/sizes.h>
17#include <power/regulator.h>
18#include <asm/unaligned.h>
19
20#include "tmio-common.h"
21
22DECLARE_GLOBAL_DATA_PTR;
23
24static u64 tmio_sd_readq(struct tmio_sd_priv *priv, unsigned int reg)
25{
26 return readq(priv->regbase + (reg << 1));
27}
28
29static void tmio_sd_writeq(struct tmio_sd_priv *priv,
30 u64 val, unsigned int reg)
31{
32 writeq(val, priv->regbase + (reg << 1));
33}
34
35static u16 tmio_sd_readw(struct tmio_sd_priv *priv, unsigned int reg)
36{
37 return readw(priv->regbase + (reg >> 1));
38}
39
40static void tmio_sd_writew(struct tmio_sd_priv *priv,
41 u16 val, unsigned int reg)
42{
43 writew(val, priv->regbase + (reg >> 1));
44}
45
46u32 tmio_sd_readl(struct tmio_sd_priv *priv, unsigned int reg)
47{
48 u32 val;
49
50 if (priv->caps & TMIO_SD_CAP_64BIT)
51 return readl(priv->regbase + (reg << 1));
52 else if (priv->caps & TMIO_SD_CAP_16BIT) {
53 val = readw(priv->regbase + (reg >> 1)) & 0xffff;
54 if ((reg == TMIO_SD_RSP10) || (reg == TMIO_SD_RSP32) ||
55 (reg == TMIO_SD_RSP54) || (reg == TMIO_SD_RSP76)) {
56 val |= readw(priv->regbase + (reg >> 1) + 2) << 16;
57 }
58 return val;
59 } else
60 return readl(priv->regbase + reg);
61}
62
63void tmio_sd_writel(struct tmio_sd_priv *priv,
64 u32 val, unsigned int reg)
65{
66 if (priv->caps & TMIO_SD_CAP_64BIT)
67 writel(val, priv->regbase + (reg << 1));
68 else if (priv->caps & TMIO_SD_CAP_16BIT) {
69 writew(val & 0xffff, priv->regbase + (reg >> 1));
70 if (reg == TMIO_SD_INFO1 || reg == TMIO_SD_INFO1_MASK ||
71 reg == TMIO_SD_INFO2 || reg == TMIO_SD_INFO2_MASK ||
72 reg == TMIO_SD_ARG)
73 writew(val >> 16, priv->regbase + (reg >> 1) + 2);
74 } else
75 writel(val, priv->regbase + reg);
76}
77
78static dma_addr_t __dma_map_single(void *ptr, size_t size,
79 enum dma_data_direction dir)
80{
81 unsigned long addr = (unsigned long)ptr;
82
83 if (dir == DMA_FROM_DEVICE)
84 invalidate_dcache_range(addr, addr + size);
85 else
86 flush_dcache_range(addr, addr + size);
87
88 return addr;
89}
90
91static void __dma_unmap_single(dma_addr_t addr, size_t size,
92 enum dma_data_direction dir)
93{
94 if (dir != DMA_TO_DEVICE)
95 invalidate_dcache_range(addr, addr + size);
96}
97
98static int tmio_sd_check_error(struct udevice *dev)
99{
100 struct tmio_sd_priv *priv = dev_get_priv(dev);
101 u32 info2 = tmio_sd_readl(priv, TMIO_SD_INFO2);
102
103 if (info2 & TMIO_SD_INFO2_ERR_RTO) {
104 /*
105 * TIMEOUT must be returned for unsupported command. Do not
106 * display error log since this might be a part of sequence to
107 * distinguish between SD and MMC.
108 */
109 return -ETIMEDOUT;
110 }
111
112 if (info2 & TMIO_SD_INFO2_ERR_TO) {
113 dev_err(dev, "timeout error\n");
114 return -ETIMEDOUT;
115 }
116
117 if (info2 & (TMIO_SD_INFO2_ERR_END | TMIO_SD_INFO2_ERR_CRC |
118 TMIO_SD_INFO2_ERR_IDX)) {
119 dev_err(dev, "communication out of sync\n");
120 return -EILSEQ;
121 }
122
123 if (info2 & (TMIO_SD_INFO2_ERR_ILA | TMIO_SD_INFO2_ERR_ILR |
124 TMIO_SD_INFO2_ERR_ILW)) {
125 dev_err(dev, "illegal access\n");
126 return -EIO;
127 }
128
129 return 0;
130}
131
132static int tmio_sd_wait_for_irq(struct udevice *dev, unsigned int reg,
133 u32 flag)
134{
135 struct tmio_sd_priv *priv = dev_get_priv(dev);
136 long wait = 1000000;
137 int ret;
138
139 while (!(tmio_sd_readl(priv, reg) & flag)) {
140 if (wait-- < 0) {
141 dev_err(dev, "timeout\n");
142 return -ETIMEDOUT;
143 }
144
145 ret = tmio_sd_check_error(dev);
146 if (ret)
147 return ret;
148
149 udelay(1);
150 }
151
152 return 0;
153}
154
155#define tmio_pio_read_fifo(__width, __suffix) \
156static void tmio_pio_read_fifo_##__width(struct tmio_sd_priv *priv, \
157 char *pbuf, uint blksz) \
158{ \
159 u##__width *buf = (u##__width *)pbuf; \
160 int i; \
161 \
162 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
163 for (i = 0; i < blksz / ((__width) / 8); i++) { \
164 *buf++ = tmio_sd_read##__suffix(priv, \
165 TMIO_SD_BUF); \
166 } \
167 } else { \
168 for (i = 0; i < blksz / ((__width) / 8); i++) { \
169 u##__width data; \
170 data = tmio_sd_read##__suffix(priv, \
171 TMIO_SD_BUF); \
172 put_unaligned(data, buf++); \
173 } \
174 } \
175}
176
177tmio_pio_read_fifo(64, q)
178tmio_pio_read_fifo(32, l)
179tmio_pio_read_fifo(16, w)
180
181static int tmio_sd_pio_read_one_block(struct udevice *dev, char *pbuf,
182 uint blocksize)
183{
184 struct tmio_sd_priv *priv = dev_get_priv(dev);
185 int ret;
186
187 /* wait until the buffer is filled with data */
188 ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO2,
189 TMIO_SD_INFO2_BRE);
190 if (ret)
191 return ret;
192
193 /*
194 * Clear the status flag _before_ read the buffer out because
195 * TMIO_SD_INFO2_BRE is edge-triggered, not level-triggered.
196 */
197 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
198
199 if (priv->caps & TMIO_SD_CAP_64BIT)
200 tmio_pio_read_fifo_64(priv, pbuf, blocksize);
201 else if (priv->caps & TMIO_SD_CAP_16BIT)
202 tmio_pio_read_fifo_16(priv, pbuf, blocksize);
203 else
204 tmio_pio_read_fifo_32(priv, pbuf, blocksize);
205
206 return 0;
207}
208
209#define tmio_pio_write_fifo(__width, __suffix) \
210static void tmio_pio_write_fifo_##__width(struct tmio_sd_priv *priv, \
211 const char *pbuf, uint blksz)\
212{ \
213 const u##__width *buf = (const u##__width *)pbuf; \
214 int i; \
215 \
216 if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) { \
217 for (i = 0; i < blksz / ((__width) / 8); i++) { \
218 tmio_sd_write##__suffix(priv, *buf++, \
219 TMIO_SD_BUF); \
220 } \
221 } else { \
222 for (i = 0; i < blksz / ((__width) / 8); i++) { \
223 u##__width data = get_unaligned(buf++); \
224 tmio_sd_write##__suffix(priv, data, \
225 TMIO_SD_BUF); \
226 } \
227 } \
228}
229
230tmio_pio_write_fifo(64, q)
231tmio_pio_write_fifo(32, l)
232tmio_pio_write_fifo(16, w)
233
234static int tmio_sd_pio_write_one_block(struct udevice *dev,
235 const char *pbuf, uint blocksize)
236{
237 struct tmio_sd_priv *priv = dev_get_priv(dev);
238 int ret;
239
240 /* wait until the buffer becomes empty */
241 ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO2,
242 TMIO_SD_INFO2_BWE);
243 if (ret)
244 return ret;
245
246 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
247
248 if (priv->caps & TMIO_SD_CAP_64BIT)
249 tmio_pio_write_fifo_64(priv, pbuf, blocksize);
250 else if (priv->caps & TMIO_SD_CAP_16BIT)
251 tmio_pio_write_fifo_16(priv, pbuf, blocksize);
252 else
253 tmio_pio_write_fifo_32(priv, pbuf, blocksize);
254
255 return 0;
256}
257
258static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_data *data)
259{
260 const char *src = data->src;
261 char *dest = data->dest;
262 int i, ret;
263
264 for (i = 0; i < data->blocks; i++) {
265 if (data->flags & MMC_DATA_READ)
266 ret = tmio_sd_pio_read_one_block(dev, dest,
267 data->blocksize);
268 else
269 ret = tmio_sd_pio_write_one_block(dev, src,
270 data->blocksize);
271 if (ret)
272 return ret;
273
274 if (data->flags & MMC_DATA_READ)
275 dest += data->blocksize;
276 else
277 src += data->blocksize;
278 }
279
280 return 0;
281}
282
283static void tmio_sd_dma_start(struct tmio_sd_priv *priv,
284 dma_addr_t dma_addr)
285{
286 u32 tmp;
287
288 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO1);
289 tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO2);
290
291 /* enable DMA */
292 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
293 tmp |= TMIO_SD_EXTMODE_DMA_EN;
294 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
295
296 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_L);
297
298 /* suppress the warning "right shift count >= width of type" */
299 dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
300
301 tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_H);
302
303 tmio_sd_writel(priv, TMIO_SD_DMA_CTL_START, TMIO_SD_DMA_CTL);
304}
305
306static int tmio_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
307 unsigned int blocks)
308{
309 struct tmio_sd_priv *priv = dev_get_priv(dev);
310 long wait = 1000000 + 10 * blocks;
311
312 while (!(tmio_sd_readl(priv, TMIO_SD_DMA_INFO1) & flag)) {
313 if (wait-- < 0) {
314 dev_err(dev, "timeout during DMA\n");
315 return -ETIMEDOUT;
316 }
317
318 udelay(10);
319 }
320
321 if (tmio_sd_readl(priv, TMIO_SD_DMA_INFO2)) {
322 dev_err(dev, "error during DMA\n");
323 return -EIO;
324 }
325
326 return 0;
327}
328
329static int tmio_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
330{
331 struct tmio_sd_priv *priv = dev_get_priv(dev);
332 size_t len = data->blocks * data->blocksize;
333 void *buf;
334 enum dma_data_direction dir;
335 dma_addr_t dma_addr;
336 u32 poll_flag, tmp;
337 int ret;
338
339 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
340
341 if (data->flags & MMC_DATA_READ) {
342 buf = data->dest;
343 dir = DMA_FROM_DEVICE;
344 /*
345 * The DMA READ completion flag position differs on Socionext
346 * and Renesas SoCs. It is bit 20 on Socionext SoCs and using
347 * bit 17 is a hardware bug and forbidden. It is bit 17 on
348 * Renesas SoCs and bit 20 does not work on them.
349 */
350 poll_flag = (priv->caps & TMIO_SD_CAP_RCAR) ?
351 TMIO_SD_DMA_INFO1_END_RD :
352 TMIO_SD_DMA_INFO1_END_RD2;
353 tmp |= TMIO_SD_DMA_MODE_DIR_RD;
354 } else {
355 buf = (void *)data->src;
356 dir = DMA_TO_DEVICE;
357 poll_flag = TMIO_SD_DMA_INFO1_END_WR;
358 tmp &= ~TMIO_SD_DMA_MODE_DIR_RD;
359 }
360
361 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
362
363 dma_addr = __dma_map_single(buf, len, dir);
364
365 tmio_sd_dma_start(priv, dma_addr);
366
367 ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
368
369 __dma_unmap_single(dma_addr, len, dir);
370
371 return ret;
372}
373
374/* check if the address is DMA'able */
Marek Vasutc7da6e342018-10-03 00:44:37 +0200375static bool tmio_sd_addr_is_dmaable(const char *src)
Marek Vasutfd83e762018-04-13 23:51:33 +0200376{
Marek Vasutc7da6e342018-10-03 00:44:37 +0200377 uintptr_t addr = (uintptr_t)src;
378
Marek Vasutfd83e762018-04-13 23:51:33 +0200379 if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN))
380 return false;
381
Marek Vasut967db0e2018-10-03 00:46:24 +0200382#if defined(CONFIG_RCAR_GEN3)
383 /* Gen3 DMA has 32bit limit */
384 if (addr >> 32)
385 return false;
386#endif
387
Marek Vasutfd83e762018-04-13 23:51:33 +0200388#if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
389 defined(CONFIG_SPL_BUILD)
390 /*
391 * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
392 * of L2, which is unreachable from the DMA engine.
393 */
394 if (addr < CONFIG_SPL_STACK)
395 return false;
396#endif
397
398 return true;
399}
400
401int tmio_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
402 struct mmc_data *data)
403{
404 struct tmio_sd_priv *priv = dev_get_priv(dev);
405 int ret;
406 u32 tmp;
407
408 if (tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_CBSY) {
409 dev_err(dev, "command busy\n");
410 return -EBUSY;
411 }
412
413 /* clear all status flags */
414 tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
415 tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
416
417 /* disable DMA once */
418 tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
419 tmp &= ~TMIO_SD_EXTMODE_DMA_EN;
420 tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
421
422 tmio_sd_writel(priv, cmd->cmdarg, TMIO_SD_ARG);
423
424 tmp = cmd->cmdidx;
425
426 if (data) {
427 tmio_sd_writel(priv, data->blocksize, TMIO_SD_SIZE);
428 tmio_sd_writel(priv, data->blocks, TMIO_SD_SECCNT);
429
430 /* Do not send CMD12 automatically */
431 tmp |= TMIO_SD_CMD_NOSTOP | TMIO_SD_CMD_DATA;
432
433 if (data->blocks > 1)
434 tmp |= TMIO_SD_CMD_MULTI;
435
436 if (data->flags & MMC_DATA_READ)
437 tmp |= TMIO_SD_CMD_RD;
438 }
439
440 /*
441 * Do not use the response type auto-detection on this hardware.
442 * CMD8, for example, has different response types on SD and eMMC,
443 * while this controller always assumes the response type for SD.
444 * Set the response type manually.
445 */
446 switch (cmd->resp_type) {
447 case MMC_RSP_NONE:
448 tmp |= TMIO_SD_CMD_RSP_NONE;
449 break;
450 case MMC_RSP_R1:
451 tmp |= TMIO_SD_CMD_RSP_R1;
452 break;
453 case MMC_RSP_R1b:
454 tmp |= TMIO_SD_CMD_RSP_R1B;
455 break;
456 case MMC_RSP_R2:
457 tmp |= TMIO_SD_CMD_RSP_R2;
458 break;
459 case MMC_RSP_R3:
460 tmp |= TMIO_SD_CMD_RSP_R3;
461 break;
462 default:
463 dev_err(dev, "unknown response type\n");
464 return -EINVAL;
465 }
466
467 dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
468 cmd->cmdidx, tmp, cmd->cmdarg);
469 tmio_sd_writel(priv, tmp, TMIO_SD_CMD);
470
471 ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO1,
472 TMIO_SD_INFO1_RSP);
473 if (ret)
474 return ret;
475
476 if (cmd->resp_type & MMC_RSP_136) {
477 u32 rsp_127_104 = tmio_sd_readl(priv, TMIO_SD_RSP76);
478 u32 rsp_103_72 = tmio_sd_readl(priv, TMIO_SD_RSP54);
479 u32 rsp_71_40 = tmio_sd_readl(priv, TMIO_SD_RSP32);
480 u32 rsp_39_8 = tmio_sd_readl(priv, TMIO_SD_RSP10);
481
482 cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
483 ((rsp_103_72 & 0xff000000) >> 24);
484 cmd->response[1] = ((rsp_103_72 & 0x00ffffff) << 8) |
485 ((rsp_71_40 & 0xff000000) >> 24);
486 cmd->response[2] = ((rsp_71_40 & 0x00ffffff) << 8) |
487 ((rsp_39_8 & 0xff000000) >> 24);
488 cmd->response[3] = (rsp_39_8 & 0xffffff) << 8;
489 } else {
490 /* bit 39-8 */
491 cmd->response[0] = tmio_sd_readl(priv, TMIO_SD_RSP10);
492 }
493
494 if (data) {
495 /* use DMA if the HW supports it and the buffer is aligned */
496 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL &&
Marek Vasutc7da6e342018-10-03 00:44:37 +0200497 tmio_sd_addr_is_dmaable(data->src))
Marek Vasutfd83e762018-04-13 23:51:33 +0200498 ret = tmio_sd_dma_xfer(dev, data);
499 else
500 ret = tmio_sd_pio_xfer(dev, data);
Marek Vasut6b4a8ba2018-10-30 21:53:29 +0100501 if (ret)
502 return ret;
Marek Vasutfd83e762018-04-13 23:51:33 +0200503
504 ret = tmio_sd_wait_for_irq(dev, TMIO_SD_INFO1,
505 TMIO_SD_INFO1_CMP);
506 if (ret)
507 return ret;
508 }
509
Marek Vasut6b4a8ba2018-10-30 21:53:29 +0100510 return tmio_sd_wait_for_irq(dev, TMIO_SD_INFO2,
511 TMIO_SD_INFO2_SCLKDIVEN);
Marek Vasutfd83e762018-04-13 23:51:33 +0200512}
513
514static int tmio_sd_set_bus_width(struct tmio_sd_priv *priv,
515 struct mmc *mmc)
516{
517 u32 val, tmp;
518
519 switch (mmc->bus_width) {
520 case 0:
521 case 1:
522 val = TMIO_SD_OPTION_WIDTH_1;
523 break;
524 case 4:
525 val = TMIO_SD_OPTION_WIDTH_4;
526 break;
527 case 8:
528 val = TMIO_SD_OPTION_WIDTH_8;
529 break;
530 default:
531 return -EINVAL;
532 }
533
534 tmp = tmio_sd_readl(priv, TMIO_SD_OPTION);
535 tmp &= ~TMIO_SD_OPTION_WIDTH_MASK;
536 tmp |= val;
537 tmio_sd_writel(priv, tmp, TMIO_SD_OPTION);
538
539 return 0;
540}
541
542static void tmio_sd_set_ddr_mode(struct tmio_sd_priv *priv,
543 struct mmc *mmc)
544{
545 u32 tmp;
546
547 tmp = tmio_sd_readl(priv, TMIO_SD_IF_MODE);
548 if (mmc->ddr_mode)
549 tmp |= TMIO_SD_IF_MODE_DDR;
550 else
551 tmp &= ~TMIO_SD_IF_MODE_DDR;
552 tmio_sd_writel(priv, tmp, TMIO_SD_IF_MODE);
553}
554
555static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv,
556 struct mmc *mmc)
557{
558 unsigned int divisor;
559 u32 val, tmp;
560
561 if (!mmc->clock)
562 return;
563
564 divisor = DIV_ROUND_UP(priv->mclk, mmc->clock);
565
566 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
590 tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
591 if (tmp & TMIO_SD_CLKCTL_SCLKEN &&
592 (tmp & TMIO_SD_CLKCTL_DIV_MASK) == val)
593 return;
594
595 /* stop the clock before changing its rate to avoid a glitch signal */
596 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
597 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
598
599 tmp &= ~TMIO_SD_CLKCTL_DIV_MASK;
600 tmp |= val | TMIO_SD_CLKCTL_OFFEN;
601 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
602
603 tmp |= TMIO_SD_CLKCTL_SCLKEN;
604 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
605
606 udelay(1000);
607}
608
609static void tmio_sd_set_pins(struct udevice *dev)
610{
611 __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev);
612
613#ifdef CONFIG_DM_REGULATOR
614 struct tmio_sd_priv *priv = dev_get_priv(dev);
615
616 if (priv->vqmmc_dev) {
617 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
618 regulator_set_value(priv->vqmmc_dev, 1800000);
619 else
620 regulator_set_value(priv->vqmmc_dev, 3300000);
621 regulator_set_enable(priv->vqmmc_dev, true);
622 }
623#endif
624
625#ifdef CONFIG_PINCTRL
Marek Vasut073e6582018-10-28 13:54:10 +0100626 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
Marek Vasutfd83e762018-04-13 23:51:33 +0200627 pinctrl_select_state(dev, "state_uhs");
Marek Vasut073e6582018-10-28 13:54:10 +0100628 else
629 pinctrl_select_state(dev, "default");
Marek Vasutfd83e762018-04-13 23:51:33 +0200630#endif
631}
632
633int tmio_sd_set_ios(struct udevice *dev)
634{
635 struct tmio_sd_priv *priv = dev_get_priv(dev);
636 struct mmc *mmc = mmc_get_mmc_dev(dev);
637 int ret;
638
639 dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
640 mmc->clock, mmc->ddr_mode, mmc->bus_width);
641
642 ret = tmio_sd_set_bus_width(priv, mmc);
643 if (ret)
644 return ret;
645 tmio_sd_set_ddr_mode(priv, mmc);
646 tmio_sd_set_clk_rate(priv, mmc);
647 tmio_sd_set_pins(dev);
648
649 return 0;
650}
651
652int tmio_sd_get_cd(struct udevice *dev)
653{
654 struct tmio_sd_priv *priv = dev_get_priv(dev);
655
656 if (priv->caps & TMIO_SD_CAP_NONREMOVABLE)
657 return 1;
658
659 return !!(tmio_sd_readl(priv, TMIO_SD_INFO1) &
660 TMIO_SD_INFO1_CD);
661}
662
663static void tmio_sd_host_init(struct tmio_sd_priv *priv)
664{
665 u32 tmp;
666
667 /* soft reset of the host */
668 tmp = tmio_sd_readl(priv, TMIO_SD_SOFT_RST);
669 tmp &= ~TMIO_SD_SOFT_RST_RSTX;
670 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
671 tmp |= TMIO_SD_SOFT_RST_RSTX;
672 tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
673
674 /* FIXME: implement eMMC hw_reset */
675
676 tmio_sd_writel(priv, TMIO_SD_STOP_SEC, TMIO_SD_STOP);
677
678 /*
679 * Connected to 32bit AXI.
680 * This register dropped backward compatibility at version 0x10.
681 * Write an appropriate value depending on the IP version.
682 */
683 if (priv->version >= 0x10)
684 tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE);
685 else
686 tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE);
687
688 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL) {
689 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
690 tmp |= TMIO_SD_DMA_MODE_ADDR_INC;
691 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
692 }
693}
694
695int tmio_sd_bind(struct udevice *dev)
696{
697 struct tmio_sd_plat *plat = dev_get_platdata(dev);
698
699 return mmc_bind(dev, &plat->mmc, &plat->cfg);
700}
701
702int tmio_sd_probe(struct udevice *dev, u32 quirks)
703{
704 struct tmio_sd_plat *plat = dev_get_platdata(dev);
705 struct tmio_sd_priv *priv = dev_get_priv(dev);
706 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
707 fdt_addr_t base;
Marek Vasutfd83e762018-04-13 23:51:33 +0200708 int ret;
709
710 base = devfdt_get_addr(dev);
711 if (base == FDT_ADDR_T_NONE)
712 return -EINVAL;
713
714 priv->regbase = devm_ioremap(dev, base, SZ_2K);
715 if (!priv->regbase)
716 return -ENOMEM;
717
718#ifdef CONFIG_DM_REGULATOR
719 device_get_supply_regulator(dev, "vqmmc-supply", &priv->vqmmc_dev);
720#endif
721
Marek Vasutfd83e762018-04-13 23:51:33 +0200722 ret = mmc_of_parse(dev, &plat->cfg);
723 if (ret < 0) {
724 dev_err(dev, "failed to parse host caps\n");
725 return ret;
726 }
727
728 plat->cfg.name = dev->name;
729 plat->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
730
731 if (quirks)
732 priv->caps = quirks;
733
734 priv->version = tmio_sd_readl(priv, TMIO_SD_VERSION) &
735 TMIO_SD_VERSION_IP;
736 dev_dbg(dev, "version %x\n", priv->version);
737 if (priv->version >= 0x10) {
738 priv->caps |= TMIO_SD_CAP_DMA_INTERNAL;
739 priv->caps |= TMIO_SD_CAP_DIV1024;
740 }
741
742 if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
743 NULL))
744 priv->caps |= TMIO_SD_CAP_NONREMOVABLE;
745
746 tmio_sd_host_init(priv);
747
748 plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
749 plat->cfg.f_min = priv->mclk /
750 (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512);
751 plat->cfg.f_max = priv->mclk;
752 plat->cfg.b_max = U32_MAX; /* max value of TMIO_SD_SECCNT */
753
754 upriv->mmc = &plat->mmc;
755
756 return 0;
757}