Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2016 Socionext Inc. |
| 4 | * Author: Masahiro Yamada <yamada.masahiro@socionext.com> |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <clk.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 10 | #include <fdtdec.h> |
| 11 | #include <mmc.h> |
| 12 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 13 | #include <asm/global_data.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 14 | #include <dm/device_compat.h> |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 15 | #include <dm/pinctrl.h> |
| 16 | #include <linux/compat.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Masahiro Yamada | 6373a17 | 2020-02-14 16:40:19 +0900 | [diff] [blame] | 18 | #include <linux/dma-mapping.h> |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 19 | #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 | |
| 26 | DECLARE_GLOBAL_DATA_PTR; |
| 27 | |
| 28 | static u64 tmio_sd_readq(struct tmio_sd_priv *priv, unsigned int reg) |
| 29 | { |
| 30 | return readq(priv->regbase + (reg << 1)); |
| 31 | } |
| 32 | |
| 33 | static 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 | |
| 39 | static u16 tmio_sd_readw(struct tmio_sd_priv *priv, unsigned int reg) |
| 40 | { |
| 41 | return readw(priv->regbase + (reg >> 1)); |
| 42 | } |
| 43 | |
| 44 | static 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 | |
| 50 | u32 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 | |
| 67 | void 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 Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 82 | static int tmio_sd_check_error(struct udevice *dev, struct mmc_cmd *cmd) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 83 | { |
| 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 Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 103 | 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 106 | 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 Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 118 | static int tmio_sd_wait_for_irq(struct udevice *dev, struct mmc_cmd *cmd, |
| 119 | unsigned int reg, u32 flag) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 120 | { |
| 121 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 122 | long wait = 1000000; |
| 123 | int ret; |
| 124 | |
Marek Vasut | f0ea8a7 | 2023-10-14 23:56:03 +0200 | [diff] [blame] | 125 | while (true) { |
| 126 | if (tmio_sd_readl(priv, reg) & flag) |
| 127 | return tmio_sd_check_error(dev, cmd); |
| 128 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 129 | if (wait-- < 0) { |
| 130 | dev_err(dev, "timeout\n"); |
| 131 | return -ETIMEDOUT; |
| 132 | } |
| 133 | |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 134 | ret = tmio_sd_check_error(dev, cmd); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 135 | if (ret) |
| 136 | return ret; |
| 137 | |
| 138 | udelay(1); |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
| 144 | #define tmio_pio_read_fifo(__width, __suffix) \ |
| 145 | static 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 | |
| 166 | tmio_pio_read_fifo(64, q) |
| 167 | tmio_pio_read_fifo(32, l) |
| 168 | tmio_pio_read_fifo(16, w) |
| 169 | |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 170 | static int tmio_sd_pio_read_one_block(struct udevice *dev, struct mmc_cmd *cmd, |
| 171 | char *pbuf, uint blocksize) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 172 | { |
| 173 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 174 | int ret; |
| 175 | |
| 176 | /* wait until the buffer is filled with data */ |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 177 | ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2, |
| 178 | TMIO_SD_INFO2_BRE); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 179 | 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) \ |
| 199 | static 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 | |
| 219 | tmio_pio_write_fifo(64, q) |
| 220 | tmio_pio_write_fifo(32, l) |
| 221 | tmio_pio_write_fifo(16, w) |
| 222 | |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 223 | static int tmio_sd_pio_write_one_block(struct udevice *dev, struct mmc_cmd *cmd, |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 224 | 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 Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 230 | ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2, |
| 231 | TMIO_SD_INFO2_BWE); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 232 | 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 Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 247 | static int tmio_sd_pio_xfer(struct udevice *dev, struct mmc_cmd *cmd, |
| 248 | struct mmc_data *data) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 249 | { |
| 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 Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 256 | ret = tmio_sd_pio_read_one_block(dev, cmd, dest, |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 257 | data->blocksize); |
| 258 | else |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 259 | ret = tmio_sd_pio_write_one_block(dev, cmd, src, |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 260 | 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 | |
| 273 | static 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 | |
| 296 | static 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 | |
| 319 | static 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 Vasut | d6e2f87 | 2021-01-03 11:38:25 +0100 | [diff] [blame] | 331 | tmp |= priv->idma_bus_width; |
| 332 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 333 | 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 Vasut | e9a2822 | 2019-01-11 23:45:54 +0100 | [diff] [blame] | 339 | * bit 17 is a hardware bug and forbidden. It is either bit 17 |
| 340 | * or bit 20 on Renesas SoCs, depending on SoC. |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 341 | */ |
Marek Vasut | e9a2822 | 2019-01-11 23:45:54 +0100 | [diff] [blame] | 342 | poll_flag = priv->read_poll_flag; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 343 | 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 Raghavendra | 0892e71 | 2020-01-16 14:23:46 +0530 | [diff] [blame] | 353 | dma_addr = dma_map_single(buf, len, dir); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 354 | |
| 355 | tmio_sd_dma_start(priv, dma_addr); |
| 356 | |
| 357 | ret = tmio_sd_dma_wait_for_irq(dev, poll_flag, data->blocks); |
| 358 | |
Marek Vasut | 314b9ca | 2019-01-11 23:38:07 +0100 | [diff] [blame] | 359 | if (poll_flag == TMIO_SD_DMA_INFO1_END_RD) |
| 360 | udelay(1); |
| 361 | |
Masahiro Yamada | 05a5dba | 2020-02-14 16:40:18 +0900 | [diff] [blame] | 362 | dma_unmap_single(dma_addr, len, dir); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 363 | |
| 364 | return ret; |
| 365 | } |
| 366 | |
| 367 | /* check if the address is DMA'able */ |
Hiroyuki Yokoyama | ed5aa49 | 2020-03-07 17:32:59 +0100 | [diff] [blame] | 368 | static bool tmio_sd_addr_is_dmaable(struct mmc_data *data) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 369 | { |
Hiroyuki Yokoyama | ed5aa49 | 2020-03-07 17:32:59 +0100 | [diff] [blame] | 370 | uintptr_t addr = (uintptr_t)data->src; |
Marek Vasut | c7da6e34 | 2018-10-03 00:44:37 +0200 | [diff] [blame] | 371 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 372 | if (!IS_ALIGNED(addr, TMIO_SD_DMA_MINALIGN)) |
| 373 | return false; |
| 374 | |
Hai Pham | 206dc91 | 2023-02-28 22:24:06 +0100 | [diff] [blame] | 375 | if (IS_ENABLED(CONFIG_RCAR_64)) { |
Marek Vasut | d4be38e | 2023-02-28 22:18:13 +0100 | [diff] [blame] | 376 | 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 Vasut | 967db0e | 2018-10-03 00:46:24 +0200 | [diff] [blame] | 382 | |
Marek Vasut | d4be38e | 2023-02-28 22:18:13 +0100 | [diff] [blame] | 383 | #ifdef CONFIG_SPL_BUILD |
Marek Vasut | c738e0c | 2023-04-08 19:35:37 +0200 | [diff] [blame] | 384 | if (IS_ENABLED(CONFIG_ARCH_UNIPHIER) && !IS_ENABLED(CONFIG_ARM64)) { |
Marek Vasut | d4be38e | 2023-02-28 22:18:13 +0100 | [diff] [blame] | 385 | /* |
| 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 392 | #endif |
| 393 | |
| 394 | return true; |
| 395 | } |
| 396 | |
| 397 | int 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 Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 467 | ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1, |
| 468 | TMIO_SD_INFO1_RSP); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 469 | 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 Yokoyama | ed5aa49 | 2020-03-07 17:32:59 +0100 | [diff] [blame] | 493 | tmio_sd_addr_is_dmaable(data)) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 494 | ret = tmio_sd_dma_xfer(dev, data); |
| 495 | else |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 496 | ret = tmio_sd_pio_xfer(dev, cmd, data); |
Marek Vasut | 6b4a8ba | 2018-10-30 21:53:29 +0100 | [diff] [blame] | 497 | if (ret) |
| 498 | return ret; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 499 | |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 500 | ret = tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO1, |
| 501 | TMIO_SD_INFO1_CMP); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 502 | if (ret) |
| 503 | return ret; |
| 504 | } |
| 505 | |
Marek Vasut | dc86e91 | 2018-10-30 22:05:54 +0100 | [diff] [blame] | 506 | return tmio_sd_wait_for_irq(dev, cmd, TMIO_SD_INFO2, |
Marek Vasut | 6b4a8ba | 2018-10-30 21:53:29 +0100 | [diff] [blame] | 507 | TMIO_SD_INFO2_SCLKDIVEN); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 508 | } |
| 509 | |
| 510 | static 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 | |
| 538 | static 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 Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 551 | static ulong tmio_sd_clk_get_rate(struct tmio_sd_priv *priv) |
| 552 | { |
| 553 | return priv->clk_get_rate(priv); |
| 554 | } |
| 555 | |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 556 | static void tmio_sd_set_clk_rate(struct tmio_sd_priv *priv, struct mmc *mmc) |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 557 | { |
| 558 | unsigned int divisor; |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 559 | u32 tmp, val = 0; |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 560 | ulong mclk; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 561 | |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 562 | if (mmc->clock) { |
| 563 | mclk = tmio_sd_clk_get_rate(priv); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 564 | |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 565 | divisor = DIV_ROUND_UP(mclk, mmc->clock); |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 566 | |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 567 | /* Do not set divider to 0xff in DDR mode */ |
| 568 | if (mmc->ddr_mode && (divisor == 1)) |
| 569 | divisor = 2; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 570 | |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 571 | 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 595 | |
| 596 | tmp = tmio_sd_readl(priv, TMIO_SD_CLKCTL); |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 597 | 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 606 | |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 607 | /* Change the clock rate. */ |
| 608 | tmp &= ~TMIO_SD_CLKCTL_DIV_MASK; |
| 609 | tmp |= val; |
| 610 | } |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 611 | |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 612 | /* Enable or Disable the clock */ |
| 613 | if (mmc->clk_disable) { |
Marek Vasut | 5abfb13 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 614 | tmp |= TMIO_SD_CLKCTL_OFFEN; |
| 615 | tmp &= ~TMIO_SD_CLKCTL_SCLKEN; |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 616 | } else { |
| 617 | tmp &= ~TMIO_SD_CLKCTL_OFFEN; |
| 618 | tmp |= TMIO_SD_CLKCTL_SCLKEN; |
Marek Vasut | 5abfb13 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 619 | } |
Marek Vasut | 9763b18 | 2018-11-15 22:01:33 +0100 | [diff] [blame] | 620 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 621 | tmio_sd_writel(priv, tmp, TMIO_SD_CLKCTL); |
| 622 | |
| 623 | udelay(1000); |
| 624 | } |
| 625 | |
| 626 | static void tmio_sd_set_pins(struct udevice *dev) |
| 627 | { |
| 628 | __maybe_unused struct mmc *mmc = mmc_get_mmc_dev(dev); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 629 | struct tmio_sd_priv *priv = dev_get_priv(dev); |
| 630 | |
Marek Vasut | d4be38e | 2023-02-28 22:18:13 +0100 | [diff] [blame] | 631 | if (CONFIG_IS_ENABLED(DM_REGULATOR) && priv->vqmmc_dev) { |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 632 | 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 638 | |
Marek Vasut | d4be38e | 2023-02-28 22:18:13 +0100 | [diff] [blame] | 639 | 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 645 | } |
| 646 | |
| 647 | int 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 Vasut | bfe04e0 | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 656 | tmio_sd_set_clk_rate(priv, mmc); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 657 | ret = tmio_sd_set_bus_width(priv, mmc); |
| 658 | if (ret) |
| 659 | return ret; |
| 660 | tmio_sd_set_ddr_mode(priv, mmc); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 661 | tmio_sd_set_pins(dev); |
| 662 | |
| 663 | return 0; |
| 664 | } |
| 665 | |
| 666 | int 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 | |
| 677 | static 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 Vasut | 2a43f88 | 2019-02-14 15:16:24 +0100 | [diff] [blame] | 697 | if (priv->version >= 0x10) { |
| 698 | if (priv->caps & TMIO_SD_CAP_64BIT) |
Marek Vasut | 1a4abf5 | 2019-02-19 19:20:14 +0100 | [diff] [blame] | 699 | tmio_sd_writel(priv, 0x000, TMIO_SD_HOST_MODE); |
Marek Vasut | 2a43f88 | 2019-02-14 15:16:24 +0100 | [diff] [blame] | 700 | else |
| 701 | tmio_sd_writel(priv, 0x101, TMIO_SD_HOST_MODE); |
| 702 | } else { |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 703 | tmio_sd_writel(priv, 0x0, TMIO_SD_HOST_MODE); |
Marek Vasut | 2a43f88 | 2019-02-14 15:16:24 +0100 | [diff] [blame] | 704 | } |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 705 | |
| 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 Vasut | d6e2f87 | 2021-01-03 11:38:25 +0100 | [diff] [blame] | 709 | tmp |= priv->idma_bus_width; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 710 | tmio_sd_writel(priv, tmp, TMIO_SD_DMA_MODE); |
| 711 | } |
| 712 | } |
| 713 | |
| 714 | int tmio_sd_bind(struct udevice *dev) |
| 715 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 716 | struct tmio_sd_plat *plat = dev_get_plat(dev); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 717 | |
| 718 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 719 | } |
| 720 | |
| 721 | int tmio_sd_probe(struct udevice *dev, u32 quirks) |
| 722 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 723 | struct tmio_sd_plat *plat = dev_get_plat(dev); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 724 | 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 Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 727 | ulong mclk; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 728 | int ret; |
| 729 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 730 | base = dev_read_addr(dev); |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 731 | 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 Vasut | d4be38e | 2023-02-28 22:18:13 +0100 | [diff] [blame] | 738 | 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 744 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 745 | 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 Vasut | d61f32e | 2023-10-22 23:40:43 +0200 | [diff] [blame] | 762 | if (!(priv->caps & TMIO_SD_CAP_RCAR)) |
| 763 | priv->caps |= TMIO_SD_CAP_DIV1024; |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 764 | } |
| 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 Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 772 | mclk = tmio_sd_clk_get_rate(priv); |
| 773 | |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 774 | plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 775 | plat->cfg.f_min = mclk / |
Marek Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 776 | (priv->caps & TMIO_SD_CAP_DIV1024 ? 1024 : 512); |
Marek Vasut | da90a1b | 2018-06-13 08:02:55 +0200 | [diff] [blame] | 777 | plat->cfg.f_max = mclk; |
Marek Vasut | 6d6e6fe | 2019-03-18 23:43:10 +0100 | [diff] [blame] | 778 | 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 Vasut | fd83e76 | 2018-04-13 23:51:33 +0200 | [diff] [blame] | 782 | |
| 783 | upriv->mmc = &plat->mmc; |
| 784 | |
| 785 | return 0; |
| 786 | } |