mmc: tmio: Make DMA transfer end bit configurable
[platform/kernel/u-boot.git] / drivers / mmc / tmio-common.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2016 Socionext Inc.
4  *   Author: Masahiro Yamada <yamada.masahiro@socionext.com>
5  */
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
22 DECLARE_GLOBAL_DATA_PTR;
23
24 static u64 tmio_sd_readq(struct tmio_sd_priv *priv, unsigned int reg)
25 {
26         return readq(priv->regbase + (reg << 1));
27 }
28
29 static 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
35 static u16 tmio_sd_readw(struct tmio_sd_priv *priv, unsigned int reg)
36 {
37         return readw(priv->regbase + (reg >> 1));
38 }
39
40 static 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
46 u32 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
63 void 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
78 static 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
91 static 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
98 static int tmio_sd_check_error(struct udevice *dev, struct mmc_cmd *cmd)
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                 if ((cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK) &&
120                     (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200))
121                         dev_err(dev, "communication out of sync\n");
122                 return -EILSEQ;
123         }
124
125         if (info2 & (TMIO_SD_INFO2_ERR_ILA | TMIO_SD_INFO2_ERR_ILR |
126                      TMIO_SD_INFO2_ERR_ILW)) {
127                 dev_err(dev, "illegal access\n");
128                 return -EIO;
129         }
130
131         return 0;
132 }
133
134 static int tmio_sd_wait_for_irq(struct udevice *dev, struct mmc_cmd *cmd,
135                                 unsigned int reg, u32 flag)
136 {
137         struct tmio_sd_priv *priv = dev_get_priv(dev);
138         long wait = 1000000;
139         int ret;
140
141         while (!(tmio_sd_readl(priv, reg) & flag)) {
142                 if (wait-- < 0) {
143                         dev_err(dev, "timeout\n");
144                         return -ETIMEDOUT;
145                 }
146
147                 ret = tmio_sd_check_error(dev, cmd);
148                 if (ret)
149                         return ret;
150
151                 udelay(1);
152         }
153
154         return 0;
155 }
156
157 #define tmio_pio_read_fifo(__width, __suffix)                           \
158 static void tmio_pio_read_fifo_##__width(struct tmio_sd_priv *priv,     \
159                                           char *pbuf, uint blksz)       \
160 {                                                                       \
161         u##__width *buf = (u##__width *)pbuf;                           \
162         int i;                                                          \
163                                                                         \
164         if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) {      \
165                 for (i = 0; i < blksz / ((__width) / 8); i++) {         \
166                         *buf++ = tmio_sd_read##__suffix(priv,           \
167                                                          TMIO_SD_BUF);  \
168                 }                                                       \
169         } else {                                                        \
170                 for (i = 0; i < blksz / ((__width) / 8); i++) {         \
171                         u##__width data;                                \
172                         data = tmio_sd_read##__suffix(priv,             \
173                                                        TMIO_SD_BUF);    \
174                         put_unaligned(data, buf++);                     \
175                 }                                                       \
176         }                                                               \
177 }
178
179 tmio_pio_read_fifo(64, q)
180 tmio_pio_read_fifo(32, l)
181 tmio_pio_read_fifo(16, w)
182
183 static int tmio_sd_pio_read_one_block(struct udevice *dev, struct mmc_cmd *cmd,
184                                       char *pbuf, uint blocksize)
185 {
186         struct tmio_sd_priv *priv = dev_get_priv(dev);
187         int ret;
188
189         /* wait until the buffer is filled with data */
190         ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
191                                    TMIO_SD_INFO2_BRE);
192         if (ret)
193                 return ret;
194
195         /*
196          * Clear the status flag _before_ read the buffer out because
197          * TMIO_SD_INFO2_BRE is edge-triggered, not level-triggered.
198          */
199         tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
200
201         if (priv->caps & TMIO_SD_CAP_64BIT)
202                 tmio_pio_read_fifo_64(priv, pbuf, blocksize);
203         else if (priv->caps & TMIO_SD_CAP_16BIT)
204                 tmio_pio_read_fifo_16(priv, pbuf, blocksize);
205         else
206                 tmio_pio_read_fifo_32(priv, pbuf, blocksize);
207
208         return 0;
209 }
210
211 #define tmio_pio_write_fifo(__width, __suffix)                          \
212 static void tmio_pio_write_fifo_##__width(struct tmio_sd_priv *priv,    \
213                                            const char *pbuf, uint blksz)\
214 {                                                                       \
215         const u##__width *buf = (const u##__width *)pbuf;               \
216         int i;                                                          \
217                                                                         \
218         if (likely(IS_ALIGNED((uintptr_t)buf, ((__width) / 8)))) {      \
219                 for (i = 0; i < blksz / ((__width) / 8); i++) {         \
220                         tmio_sd_write##__suffix(priv, *buf++,           \
221                                                  TMIO_SD_BUF);          \
222                 }                                                       \
223         } else {                                                        \
224                 for (i = 0; i < blksz / ((__width) / 8); i++) {         \
225                         u##__width data = get_unaligned(buf++);         \
226                         tmio_sd_write##__suffix(priv, data,             \
227                                                  TMIO_SD_BUF);          \
228                 }                                                       \
229         }                                                               \
230 }
231
232 tmio_pio_write_fifo(64, q)
233 tmio_pio_write_fifo(32, l)
234 tmio_pio_write_fifo(16, w)
235
236 static int tmio_sd_pio_write_one_block(struct udevice *dev, struct mmc_cmd *cmd,
237                                            const char *pbuf, uint blocksize)
238 {
239         struct tmio_sd_priv *priv = dev_get_priv(dev);
240         int ret;
241
242         /* wait until the buffer becomes empty */
243         ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
244                                    TMIO_SD_INFO2_BWE);
245         if (ret)
246                 return ret;
247
248         tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
249
250         if (priv->caps & TMIO_SD_CAP_64BIT)
251                 tmio_pio_write_fifo_64(priv, pbuf, blocksize);
252         else if (priv->caps & TMIO_SD_CAP_16BIT)
253                 tmio_pio_write_fifo_16(priv, pbuf, blocksize);
254         else
255                 tmio_pio_write_fifo_32(priv, pbuf, blocksize);
256
257         return 0;
258 }
259
260 static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_cmd *cmd,
261                             struct mmc_data *data)
262 {
263         const char *src = data->src;
264         char *dest = data->dest;
265         int i, ret;
266
267         for (i = 0; i < data->blocks; i++) {
268                 if (data->flags & MMC_DATA_READ)
269                         ret = tmio_sd_pio_read_one_block(dev, cmd, dest,
270                                                              data->blocksize);
271                 else
272                         ret = tmio_sd_pio_write_one_block(dev, cmd, src,
273                                                               data->blocksize);
274                 if (ret)
275                         return ret;
276
277                 if (data->flags & MMC_DATA_READ)
278                         dest += data->blocksize;
279                 else
280                         src += data->blocksize;
281         }
282
283         return 0;
284 }
285
286 static void tmio_sd_dma_start(struct tmio_sd_priv *priv,
287                                   dma_addr_t dma_addr)
288 {
289         u32 tmp;
290
291         tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO1);
292         tmio_sd_writel(priv, 0, TMIO_SD_DMA_INFO2);
293
294         /* enable DMA */
295         tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
296         tmp |= TMIO_SD_EXTMODE_DMA_EN;
297         tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
298
299         tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_L);
300
301         /* suppress the warning "right shift count >= width of type" */
302         dma_addr >>= min_t(int, 32, 8 * sizeof(dma_addr));
303
304         tmio_sd_writel(priv, dma_addr & U32_MAX, TMIO_SD_DMA_ADDR_H);
305
306         tmio_sd_writel(priv, TMIO_SD_DMA_CTL_START, TMIO_SD_DMA_CTL);
307 }
308
309 static int tmio_sd_dma_wait_for_irq(struct udevice *dev, u32 flag,
310                                         unsigned int blocks)
311 {
312         struct tmio_sd_priv *priv = dev_get_priv(dev);
313         long wait = 1000000 + 10 * blocks;
314
315         while (!(tmio_sd_readl(priv, TMIO_SD_DMA_INFO1) & flag)) {
316                 if (wait-- < 0) {
317                         dev_err(dev, "timeout during DMA\n");
318                         return -ETIMEDOUT;
319                 }
320
321                 udelay(10);
322         }
323
324         if (tmio_sd_readl(priv, TMIO_SD_DMA_INFO2)) {
325                 dev_err(dev, "error during DMA\n");
326                 return -EIO;
327         }
328
329         return 0;
330 }
331
332 static int tmio_sd_dma_xfer(struct udevice *dev, struct mmc_data *data)
333 {
334         struct tmio_sd_priv *priv = dev_get_priv(dev);
335         size_t len = data->blocks * data->blocksize;
336         void *buf;
337         enum dma_data_direction dir;
338         dma_addr_t dma_addr;
339         u32 poll_flag, tmp;
340         int ret;
341
342         tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
343
344         if (data->flags & MMC_DATA_READ) {
345                 buf = data->dest;
346                 dir = DMA_FROM_DEVICE;
347                 /*
348                  * The DMA READ completion flag position differs on Socionext
349                  * and Renesas SoCs. It is bit 20 on Socionext SoCs and using
350                  * bit 17 is a hardware bug and forbidden. It is either bit 17
351                  * or bit 20 on Renesas SoCs, depending on SoC.
352                  */
353                 poll_flag = priv->read_poll_flag;
354                 tmp |= TMIO_SD_DMA_MODE_DIR_RD;
355         } else {
356                 buf = (void *)data->src;
357                 dir = DMA_TO_DEVICE;
358                 poll_flag = TMIO_SD_DMA_INFO1_END_WR;
359                 tmp &= ~TMIO_SD_DMA_MODE_DIR_RD;
360         }
361
362         tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
363
364         dma_addr = __dma_map_single(buf, len, dir);
365
366         tmio_sd_dma_start(priv, dma_addr);
367
368         ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks);
369
370         __dma_unmap_single(dma_addr, len, dir);
371
372         return ret;
373 }
374
375 /* check if the address is DMA'able */
376 static bool tmio_sd_addr_is_dmaable(const char *src)
377 {
378         uintptr_t addr = (uintptr_t)src;
379
380         if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN))
381                 return false;
382
383 #if defined(CONFIG_RCAR_GEN3)
384         /* Gen3 DMA has 32bit limit */
385         if (addr >> 32)
386                 return false;
387 #endif
388
389 #if defined(CONFIG_ARCH_UNIPHIER) && !defined(CONFIG_ARM64) && \
390         defined(CONFIG_SPL_BUILD)
391         /*
392          * For UniPhier ARMv7 SoCs, the stack is allocated in the locked ways
393          * of L2, which is unreachable from the DMA engine.
394          */
395         if (addr < CONFIG_SPL_STACK)
396                 return false;
397 #endif
398
399         return true;
400 }
401
402 int tmio_sd_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
403                       struct mmc_data *data)
404 {
405         struct tmio_sd_priv *priv = dev_get_priv(dev);
406         int ret;
407         u32 tmp;
408
409         if (tmio_sd_readl(priv, TMIO_SD_INFO2) & TMIO_SD_INFO2_CBSY) {
410                 dev_err(dev, "command busy\n");
411                 return -EBUSY;
412         }
413
414         /* clear all status flags */
415         tmio_sd_writel(priv, 0, TMIO_SD_INFO1);
416         tmio_sd_writel(priv, 0, TMIO_SD_INFO2);
417
418         /* disable DMA once */
419         tmp = tmio_sd_readl(priv, TMIO_SD_EXTMODE);
420         tmp &= ~TMIO_SD_EXTMODE_DMA_EN;
421         tmio_sd_writel(priv, tmp, TMIO_SD_EXTMODE);
422
423         tmio_sd_writel(priv, cmd->cmdarg, TMIO_SD_ARG);
424
425         tmp = cmd->cmdidx;
426
427         if (data) {
428                 tmio_sd_writel(priv, data->blocksize, TMIO_SD_SIZE);
429                 tmio_sd_writel(priv, data->blocks, TMIO_SD_SECCNT);
430
431                 /* Do not send CMD12 automatically */
432                 tmp |= TMIO_SD_CMD_NOSTOP | TMIO_SD_CMD_DATA;
433
434                 if (data->blocks > 1)
435                         tmp |= TMIO_SD_CMD_MULTI;
436
437                 if (data->flags & MMC_DATA_READ)
438                         tmp |= TMIO_SD_CMD_RD;
439         }
440
441         /*
442          * Do not use the response type auto-detection on this hardware.
443          * CMD8, for example, has different response types on SD and eMMC,
444          * while this controller always assumes the response type for SD.
445          * Set the response type manually.
446          */
447         switch (cmd->resp_type) {
448         case MMC_RSP_NONE:
449                 tmp |= TMIO_SD_CMD_RSP_NONE;
450                 break;
451         case MMC_RSP_R1:
452                 tmp |= TMIO_SD_CMD_RSP_R1;
453                 break;
454         case MMC_RSP_R1b:
455                 tmp |= TMIO_SD_CMD_RSP_R1B;
456                 break;
457         case MMC_RSP_R2:
458                 tmp |= TMIO_SD_CMD_RSP_R2;
459                 break;
460         case MMC_RSP_R3:
461                 tmp |= TMIO_SD_CMD_RSP_R3;
462                 break;
463         default:
464                 dev_err(dev, "unknown response type\n");
465                 return -EINVAL;
466         }
467
468         dev_dbg(dev, "sending CMD%d (SD_CMD=%08x, SD_ARG=%08x)\n",
469                 cmd->cmdidx, tmp, cmd->cmdarg);
470         tmio_sd_writel(priv, tmp, TMIO_SD_CMD);
471
472         ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
473                                    TMIO_SD_INFO1_RSP);
474         if (ret)
475                 return ret;
476
477         if (cmd->resp_type & MMC_RSP_136) {
478                 u32 rsp_127_104 = tmio_sd_readl(priv, TMIO_SD_RSP76);
479                 u32 rsp_103_72 = tmio_sd_readl(priv, TMIO_SD_RSP54);
480                 u32 rsp_71_40 = tmio_sd_readl(priv, TMIO_SD_RSP32);
481                 u32 rsp_39_8 = tmio_sd_readl(priv, TMIO_SD_RSP10);
482
483                 cmd->response[0] = ((rsp_127_104 & 0x00ffffff) << 8) |
484                                    ((rsp_103_72  & 0xff000000) >> 24);
485                 cmd->response[1] = ((rsp_103_72  & 0x00ffffff) << 8) |
486                                    ((rsp_71_40   & 0xff000000) >> 24);
487                 cmd->response[2] = ((rsp_71_40   & 0x00ffffff) << 8) |
488                                    ((rsp_39_8    & 0xff000000) >> 24);
489                 cmd->response[3] = (rsp_39_8     & 0xffffff)   << 8;
490         } else {
491                 /* bit 39-8 */
492                 cmd->response[0] = tmio_sd_readl(priv, TMIO_SD_RSP10);
493         }
494
495         if (data) {
496                 /* use DMA if the HW supports it and the buffer is aligned */
497                 if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL &&
498                     tmio_sd_addr_is_dmaable(data->src))
499                         ret = tmio_sd_dma_xfer(dev, data);
500                 else
501                         ret = tmio_sd_pio_xfer(dev, cmd, data);
502                 if (ret)
503                         return ret;
504
505                 ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1,
506                                            TMIO_SD_INFO1_CMP);
507                 if (ret)
508                         return ret;
509         }
510
511         return tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2,
512                                     TMIO_SD_INFO2_SCLKDIVEN);
513 }
514
515 static int tmio_sd_set_bus_width(struct tmio_sd_priv *priv,
516                                      struct mmc *mmc)
517 {
518         u32 val, tmp;
519
520         switch (mmc->bus_width) {
521         case 0:
522         case 1:
523                 val = TMIO_SD_OPTION_WIDTH_1;
524                 break;
525         case 4:
526                 val = TMIO_SD_OPTION_WIDTH_4;
527                 break;
528         case 8:
529                 val = TMIO_SD_OPTION_WIDTH_8;
530                 break;
531         default:
532                 return -EINVAL;
533         }
534
535         tmp = tmio_sd_readl(priv, TMIO_SD_OPTION);
536         tmp &= ~TMIO_SD_OPTION_WIDTH_MASK;
537         tmp |= val;
538         tmio_sd_writel(priv, tmp, TMIO_SD_OPTION);
539
540         return 0;
541 }
542
543 static void tmio_sd_set_ddr_mode(struct tmio_sd_priv *priv,
544                                      struct mmc *mmc)
545 {
546         u32 tmp;
547
548         tmp = tmio_sd_readl(priv, TMIO_SD_IF_MODE);
549         if (mmc->ddr_mode)
550                 tmp |= TMIO_SD_IF_MODE_DDR;
551         else
552                 tmp &= ~TMIO_SD_IF_MODE_DDR;
553         tmio_sd_writel(priv, tmp, TMIO_SD_IF_MODE);
554 }
555
556 static ulong tmio_sd_clk_get_rate(struct tmio_sd_priv *priv)
557 {
558         return priv->clk_get_rate(priv);
559 }
560
561 static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv, struct mmc *mmc)
562 {
563         unsigned int divisor;
564         u32 tmp, val = 0;
565         ulong mclk;
566
567         if (mmc->clock) {
568                 mclk = tmio_sd_clk_get_rate(priv);
569
570                 divisor = DIV_ROUND_UP(mclk, mmc->clock);
571
572                 /* Do not set divider to 0xff in DDR mode */
573                 if (mmc->ddr_mode && (divisor == 1))
574                         divisor = 2;
575
576                 if (divisor <= 1)
577                         val = (priv->caps & TMIO_SD_CAP_RCAR) ?
578                               TMIO_SD_CLKCTL_RCAR_DIV1 : TMIO_SD_CLKCTL_DIV1;
579                 else if (divisor <= 2)
580                         val = TMIO_SD_CLKCTL_DIV2;
581                 else if (divisor <= 4)
582                         val = TMIO_SD_CLKCTL_DIV4;
583                 else if (divisor <= 8)
584                         val = TMIO_SD_CLKCTL_DIV8;
585                 else if (divisor <= 16)
586                         val = TMIO_SD_CLKCTL_DIV16;
587                 else if (divisor <= 32)
588                         val = TMIO_SD_CLKCTL_DIV32;
589                 else if (divisor <= 64)
590                         val = TMIO_SD_CLKCTL_DIV64;
591                 else if (divisor <= 128)
592                         val = TMIO_SD_CLKCTL_DIV128;
593                 else if (divisor <= 256)
594                         val = TMIO_SD_CLKCTL_DIV256;
595                 else if (divisor <= 512 || !(priv->caps & TMIO_SD_CAP_DIV1024))
596                         val = TMIO_SD_CLKCTL_DIV512;
597                 else
598                         val = TMIO_SD_CLKCTL_DIV1024;
599         }
600
601         tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL);
602         if (mmc->clock &&
603             !((tmp & TMIO_SD_CLKCTL_SCLKEN) &&
604               ((tmp & TMIO_SD_CLKCTL_DIV_MASK) == val))) {
605                 /*
606                  * Stop the clock before changing its rate
607                  * to avoid a glitch signal
608                  */
609                 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
610                 tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
611
612                 /* Change the clock rate. */
613                 tmp &= ~TMIO_SD_CLKCTL_DIV_MASK;
614                 tmp |= val;
615         }
616
617         /* Enable or Disable the clock */
618         if (mmc->clk_disable) {
619                 tmp |= TMIO_SD_CLKCTL_OFFEN;
620                 tmp &= ~TMIO_SD_CLKCTL_SCLKEN;
621         } else {
622                 tmp &= ~TMIO_SD_CLKCTL_OFFEN;
623                 tmp |= TMIO_SD_CLKCTL_SCLKEN;
624         }
625
626         tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL);
627
628         udelay(1000);
629 }
630
631 static void tmio_sd_set_pins(struct udevice *dev)
632 {
633         __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev);
634
635 #ifdef CONFIG_DM_REGULATOR
636         struct tmio_sd_priv *priv = dev_get_priv(dev);
637
638         if (priv->vqmmc_dev) {
639                 if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
640                         regulator_set_value(priv->vqmmc_dev, 1800000);
641                 else
642                         regulator_set_value(priv->vqmmc_dev, 3300000);
643                 regulator_set_enable(priv->vqmmc_dev, true);
644         }
645 #endif
646
647 #ifdef CONFIG_PINCTRL
648         if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180)
649                 pinctrl_select_state(dev, "state_uhs");
650         else
651                 pinctrl_select_state(dev, "default");
652 #endif
653 }
654
655 int tmio_sd_set_ios(struct udevice *dev)
656 {
657         struct tmio_sd_priv *priv = dev_get_priv(dev);
658         struct mmc *mmc = mmc_get_mmc_dev(dev);
659         int ret;
660
661         dev_dbg(dev, "clock %uHz, DDRmode %d, width %u\n",
662                 mmc->clock, mmc->ddr_mode, mmc->bus_width);
663
664         tmio_sd_set_clk_rate(priv, mmc);
665         ret = tmio_sd_set_bus_width(priv, mmc);
666         if (ret)
667                 return ret;
668         tmio_sd_set_ddr_mode(priv, mmc);
669         tmio_sd_set_pins(dev);
670
671         return 0;
672 }
673
674 int tmio_sd_get_cd(struct udevice *dev)
675 {
676         struct tmio_sd_priv *priv = dev_get_priv(dev);
677
678         if (priv->caps & TMIO_SD_CAP_NONREMOVABLE)
679                 return 1;
680
681         return !!(tmio_sd_readl(priv, TMIO_SD_INFO1) &
682                   TMIO_SD_INFO1_CD);
683 }
684
685 static void tmio_sd_host_init(struct tmio_sd_priv *priv)
686 {
687         u32 tmp;
688
689         /* soft reset of the host */
690         tmp = tmio_sd_readl(priv, TMIO_SD_SOFT_RST);
691         tmp &= ~TMIO_SD_SOFT_RST_RSTX;
692         tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
693         tmp |= TMIO_SD_SOFT_RST_RSTX;
694         tmio_sd_writel(priv, tmp, TMIO_SD_SOFT_RST);
695
696         /* FIXME: implement eMMC hw_reset */
697
698         tmio_sd_writel(priv, TMIO_SD_STOP_SEC, TMIO_SD_STOP);
699
700         /*
701          * Connected to 32bit AXI.
702          * This register dropped backward compatibility at version 0x10.
703          * Write an appropriate value depending on the IP version.
704          */
705         if (priv->version >= 0x10)
706                 tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE);
707         else
708                 tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE);
709
710         if (priv->caps & TMIO_SD_CAP_DMA_INTERNAL) {
711                 tmp = tmio_sd_readl(priv, TMIO_SD_DMA_MODE);
712                 tmp |= TMIO_SD_DMA_MODE_ADDR_INC;
713                 tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE);
714         }
715 }
716
717 int tmio_sd_bind(struct udevice *dev)
718 {
719         struct tmio_sd_plat *plat = dev_get_platdata(dev);
720
721         return mmc_bind(dev, &plat->mmc, &plat->cfg);
722 }
723
724 int tmio_sd_probe(struct udevice *dev, u32 quirks)
725 {
726         struct tmio_sd_plat *plat = dev_get_platdata(dev);
727         struct tmio_sd_priv *priv = dev_get_priv(dev);
728         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
729         fdt_addr_t base;
730         ulong mclk;
731         int ret;
732
733         base = devfdt_get_addr(dev);
734         if (base == FDT_ADDR_T_NONE)
735                 return -EINVAL;
736
737         priv->regbase = devm_ioremap(dev, base, SZ_2K);
738         if (!priv->regbase)
739                 return -ENOMEM;
740
741 #ifdef CONFIG_DM_REGULATOR
742         device_get_supply_regulator(dev, "vqmmc-supply", &priv->vqmmc_dev);
743         if (priv->vqmmc_dev)
744                 regulator_set_value(priv->vqmmc_dev, 3300000);
745 #endif
746
747         ret = mmc_of_parse(dev, &plat->cfg);
748         if (ret < 0) {
749                 dev_err(dev, "failed to parse host caps\n");
750                 return ret;
751         }
752
753         plat->cfg.name = dev->name;
754         plat->cfg.host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS;
755
756         if (quirks)
757                 priv->caps = quirks;
758
759         priv->version = tmio_sd_readl(priv, TMIO_SD_VERSION) &
760                                                 TMIO_SD_VERSION_IP;
761         dev_dbg(dev, "version %x\n", priv->version);
762         if (priv->version >= 0x10) {
763                 priv->caps |= TMIO_SD_CAP_DMA_INTERNAL;
764                 priv->caps |= TMIO_SD_CAP_DIV1024;
765         }
766
767         if (fdt_get_property(gd->fdt_blob, dev_of_offset(dev), "non-removable",
768                              NULL))
769                 priv->caps |= TMIO_SD_CAP_NONREMOVABLE;
770
771         tmio_sd_host_init(priv);
772
773         mclk = tmio_sd_clk_get_rate(priv);
774
775         plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34;
776         plat->cfg.f_min = mclk /
777                         (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512);
778         plat->cfg.f_max = mclk;
779         plat->cfg.b_max = U32_MAX; /* max value of TMIO_SD_SECCNT */
780
781         upriv->mmc = &plat->mmc;
782
783         return 0;
784 }