Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2012 SAMSUNG Electronics |
| 4 | * Jaehoon Chung <jh80.chung@samsung.com> |
| 5 | * Rajeshawari Shinde <rajeshwari.s@samsung.com> |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 8 | #include <bouncebuf.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 13 | #include <memalign.h> |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 14 | #include <mmc.h> |
| 15 | #include <dwmmc.h> |
Ley Foon Tan | b98e892 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 16 | #include <wait_bit.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 17 | #include <asm/cache.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Urja Rannikko | 9932a01 | 2019-05-13 13:25:27 +0000 | [diff] [blame] | 19 | #include <power/regulator.h> |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 20 | |
| 21 | #define PAGE_SIZE 4096 |
| 22 | |
Sam Protsenko | 2543c32 | 2024-08-07 22:14:08 -0500 | [diff] [blame] | 23 | struct dwmci_idmac { |
| 24 | u32 flags; |
| 25 | u32 cnt; |
| 26 | u32 addr; |
| 27 | u32 next_addr; |
| 28 | } __aligned(ARCH_DMA_MINALIGN); |
| 29 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 30 | static int dwmci_wait_reset(struct dwmci_host *host, u32 value) |
| 31 | { |
| 32 | unsigned long timeout = 1000; |
| 33 | u32 ctrl; |
| 34 | |
| 35 | dwmci_writel(host, DWMCI_CTRL, value); |
| 36 | |
| 37 | while (timeout--) { |
| 38 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 39 | if (!(ctrl & DWMCI_RESET_ALL)) |
| 40 | return 1; |
| 41 | } |
| 42 | return 0; |
| 43 | } |
| 44 | |
| 45 | static void dwmci_set_idma_desc(struct dwmci_idmac *idmac, |
| 46 | u32 desc0, u32 desc1, u32 desc2) |
| 47 | { |
| 48 | struct dwmci_idmac *desc = idmac; |
| 49 | |
| 50 | desc->flags = desc0; |
| 51 | desc->cnt = desc1; |
| 52 | desc->addr = desc2; |
Prabhakar Kushwaha | fdefb90 | 2015-10-25 13:18:25 +0530 | [diff] [blame] | 53 | desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void dwmci_prepare_data(struct dwmci_host *host, |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 57 | struct mmc_data *data, |
| 58 | struct dwmci_idmac *cur_idmac, |
| 59 | void *bounce_buffer) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 60 | { |
| 61 | unsigned long ctrl; |
| 62 | unsigned int i = 0, flags, cnt, blk_cnt; |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 63 | ulong data_start, data_end; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 64 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 65 | blk_cnt = data->blocks; |
| 66 | |
| 67 | dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); |
| 68 | |
Ley Foon Tan | b98e892 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 69 | /* Clear IDMAC interrupt */ |
| 70 | dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF); |
| 71 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 72 | data_start = (ulong)cur_idmac; |
Prabhakar Kushwaha | fdefb90 | 2015-10-25 13:18:25 +0530 | [diff] [blame] | 73 | dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 74 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 75 | do { |
| 76 | flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ; |
| 77 | flags |= (i == 0) ? DWMCI_IDMAC_FS : 0; |
| 78 | if (blk_cnt <= 8) { |
| 79 | flags |= DWMCI_IDMAC_LD; |
| 80 | cnt = data->blocksize * blk_cnt; |
| 81 | } else |
| 82 | cnt = data->blocksize * 8; |
| 83 | |
| 84 | dwmci_set_idma_desc(cur_idmac, flags, cnt, |
Prabhakar Kushwaha | fdefb90 | 2015-10-25 13:18:25 +0530 | [diff] [blame] | 85 | (ulong)bounce_buffer + (i * PAGE_SIZE)); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 86 | |
Marek Vasut | b6da37b | 2019-02-13 20:16:20 +0100 | [diff] [blame] | 87 | cur_idmac++; |
Mischa Jonker | a7a6091 | 2013-07-26 16:18:40 +0200 | [diff] [blame] | 88 | if (blk_cnt <= 8) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 89 | break; |
| 90 | blk_cnt -= 8; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 91 | i++; |
| 92 | } while(1); |
| 93 | |
| 94 | data_end = (ulong)cur_idmac; |
Marek Vasut | b6da37b | 2019-02-13 20:16:20 +0100 | [diff] [blame] | 95 | flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN)); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 96 | |
| 97 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 98 | ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN; |
| 99 | dwmci_writel(host, DWMCI_CTRL, ctrl); |
| 100 | |
| 101 | ctrl = dwmci_readl(host, DWMCI_BMOD); |
| 102 | ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN; |
| 103 | dwmci_writel(host, DWMCI_BMOD, ctrl); |
| 104 | |
| 105 | dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); |
| 106 | dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks); |
| 107 | } |
| 108 | |
Heiko Stuebner | 46b7a4f | 2018-09-21 10:59:45 +0200 | [diff] [blame] | 109 | static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len) |
| 110 | { |
| 111 | u32 timeout = 20000; |
| 112 | |
| 113 | *len = dwmci_readl(host, DWMCI_STATUS); |
| 114 | while (--timeout && (*len & bit)) { |
| 115 | udelay(200); |
| 116 | *len = dwmci_readl(host, DWMCI_STATUS); |
| 117 | } |
| 118 | |
| 119 | if (!timeout) { |
| 120 | debug("%s: FIFO underflow timeout\n", __func__); |
| 121 | return -ETIMEDOUT; |
| 122 | } |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 127 | static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size) |
| 128 | { |
| 129 | unsigned int timeout; |
| 130 | |
Kever Yang | 4889d83 | 2019-08-29 15:42:41 +0800 | [diff] [blame] | 131 | timeout = size * 8; /* counting in bits */ |
| 132 | timeout *= 10; /* wait 10 times as long */ |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 133 | timeout /= mmc->clock; |
| 134 | timeout /= mmc->bus_width; |
| 135 | timeout /= mmc->ddr_mode ? 2 : 1; |
Kever Yang | 4889d83 | 2019-08-29 15:42:41 +0800 | [diff] [blame] | 136 | timeout *= 1000; /* counting in msec */ |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 137 | timeout = (timeout < 1000) ? 1000 : timeout; |
| 138 | |
| 139 | return timeout; |
| 140 | } |
| 141 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 142 | static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data) |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 143 | { |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 144 | struct mmc *mmc = host->mmc; |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 145 | int ret = 0; |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 146 | u32 timeout, mask, size, i, len = 0; |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 147 | u32 *buf = NULL; |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 148 | ulong start = get_timer(0); |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 149 | u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >> |
| 150 | RX_WMARK_SHIFT) + 1) * 2; |
| 151 | |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 152 | size = data->blocksize * data->blocks; |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 153 | if (data->flags == MMC_DATA_READ) |
| 154 | buf = (unsigned int *)data->dest; |
| 155 | else |
| 156 | buf = (unsigned int *)data->src; |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 157 | |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 158 | timeout = dwmci_get_timeout(mmc, size); |
| 159 | |
| 160 | size /= 4; |
| 161 | |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 162 | for (;;) { |
| 163 | mask = dwmci_readl(host, DWMCI_RINTSTS); |
| 164 | /* Error during data transfer. */ |
| 165 | if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) { |
| 166 | debug("%s: DATA ERROR!\n", __func__); |
| 167 | ret = -EINVAL; |
| 168 | break; |
| 169 | } |
| 170 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 171 | if (host->fifo_mode && size) { |
Xu Ziyuan | 5b8bf12 | 2016-07-28 10:25:48 +0800 | [diff] [blame] | 172 | len = 0; |
Jacob Chen | 953d975 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 173 | if (data->flags == MMC_DATA_READ && |
Ley Foon Tan | 1cead23 | 2021-04-26 11:35:05 +0800 | [diff] [blame] | 174 | (mask & (DWMCI_INTMSK_RXDR | DWMCI_INTMSK_DTO))) { |
| 175 | dwmci_writel(host, DWMCI_RINTSTS, |
John Keeping | a6a7157 | 2022-09-15 18:56:56 +0100 | [diff] [blame] | 176 | mask & (DWMCI_INTMSK_RXDR | |
| 177 | DWMCI_INTMSK_DTO)); |
Jacob Chen | 953d975 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 178 | while (size) { |
Heiko Stuebner | 46b7a4f | 2018-09-21 10:59:45 +0200 | [diff] [blame] | 179 | ret = dwmci_fifo_ready(host, |
| 180 | DWMCI_FIFO_EMPTY, |
| 181 | &len); |
| 182 | if (ret < 0) |
| 183 | break; |
| 184 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 185 | len = (len >> DWMCI_FIFO_SHIFT) & |
| 186 | DWMCI_FIFO_MASK; |
Xu Ziyuan | 6577a2a | 2016-07-28 10:25:47 +0800 | [diff] [blame] | 187 | len = min(size, len); |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 188 | for (i = 0; i < len; i++) |
| 189 | *buf++ = |
| 190 | dwmci_readl(host, DWMCI_DATA); |
Jacob Chen | 953d975 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 191 | size = size > len ? (size - len) : 0; |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 192 | } |
Jacob Chen | 953d975 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 193 | } else if (data->flags == MMC_DATA_WRITE && |
| 194 | (mask & DWMCI_INTMSK_TXDR)) { |
| 195 | while (size) { |
Heiko Stuebner | 46b7a4f | 2018-09-21 10:59:45 +0200 | [diff] [blame] | 196 | ret = dwmci_fifo_ready(host, |
| 197 | DWMCI_FIFO_FULL, |
| 198 | &len); |
| 199 | if (ret < 0) |
| 200 | break; |
| 201 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 202 | len = fifo_depth - ((len >> |
| 203 | DWMCI_FIFO_SHIFT) & |
| 204 | DWMCI_FIFO_MASK); |
Xu Ziyuan | 6577a2a | 2016-07-28 10:25:47 +0800 | [diff] [blame] | 205 | len = min(size, len); |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 206 | for (i = 0; i < len; i++) |
| 207 | dwmci_writel(host, DWMCI_DATA, |
| 208 | *buf++); |
Jacob Chen | 953d975 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 209 | size = size > len ? (size - len) : 0; |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 210 | } |
Jacob Chen | 953d975 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 211 | dwmci_writel(host, DWMCI_RINTSTS, |
| 212 | DWMCI_INTMSK_TXDR); |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 213 | } |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 214 | } |
| 215 | |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 216 | /* Data arrived correctly. */ |
| 217 | if (mask & DWMCI_INTMSK_DTO) { |
| 218 | ret = 0; |
| 219 | break; |
| 220 | } |
| 221 | |
| 222 | /* Check for timeout. */ |
| 223 | if (get_timer(start) > timeout) { |
| 224 | debug("%s: Timeout waiting for data!\n", |
| 225 | __func__); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 226 | ret = -ETIMEDOUT; |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | dwmci_writel(host, DWMCI_RINTSTS, mask); |
| 232 | |
| 233 | return ret; |
| 234 | } |
| 235 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 236 | static int dwmci_set_transfer_mode(struct dwmci_host *host, |
| 237 | struct mmc_data *data) |
| 238 | { |
| 239 | unsigned long mode; |
| 240 | |
| 241 | mode = DWMCI_CMD_DATA_EXP; |
| 242 | if (data->flags & MMC_DATA_WRITE) |
| 243 | mode |= DWMCI_CMD_RW; |
| 244 | |
| 245 | return mode; |
| 246 | } |
| 247 | |
Sam Protsenko | cf81204 | 2024-08-07 22:14:09 -0500 | [diff] [blame] | 248 | static void dwmci_wait_while_busy(struct dwmci_host *host, struct mmc_cmd *cmd) |
| 249 | { |
| 250 | unsigned int timeout = 500; /* msec */ |
| 251 | ulong start; |
| 252 | |
| 253 | start = get_timer(0); |
| 254 | while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) { |
| 255 | if (get_timer(start) > timeout) { |
| 256 | debug("%s: Timeout on data busy, continue anyway\n", |
| 257 | __func__); |
| 258 | break; |
| 259 | } |
| 260 | } |
| 261 | } |
| 262 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 263 | #ifdef CONFIG_DM_MMC |
Jaehoon Chung | ad220ac | 2016-06-28 15:52:21 +0900 | [diff] [blame] | 264 | static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 265 | struct mmc_data *data) |
| 266 | { |
| 267 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 268 | #else |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 269 | static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 270 | struct mmc_data *data) |
| 271 | { |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 272 | #endif |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 273 | struct dwmci_host *host = mmc->priv; |
Mischa Jonker | 7423bed | 2013-07-26 14:08:14 +0200 | [diff] [blame] | 274 | ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac, |
Mischa Jonker | a7a6091 | 2013-07-26 16:18:40 +0200 | [diff] [blame] | 275 | data ? DIV_ROUND_UP(data->blocks, 8) : 0); |
Marek Vasut | 81e093f | 2015-07-27 22:39:38 +0200 | [diff] [blame] | 276 | int ret = 0, flags = 0, i; |
Alexander Graf | 61c2a66 | 2016-03-04 01:09:52 +0100 | [diff] [blame] | 277 | u32 retry = 100000; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 278 | u32 mask, ctrl; |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 279 | struct bounce_buffer bbstate; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 280 | |
Sam Protsenko | cf81204 | 2024-08-07 22:14:09 -0500 | [diff] [blame] | 281 | dwmci_wait_while_busy(host, cmd); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 282 | dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL); |
| 283 | |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 284 | if (data) { |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 285 | if (host->fifo_mode) { |
| 286 | dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); |
| 287 | dwmci_writel(host, DWMCI_BYTCNT, |
| 288 | data->blocksize * data->blocks); |
| 289 | dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 290 | } else { |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 291 | if (data->flags == MMC_DATA_READ) { |
Marek Vasut | 72d37b6 | 2019-03-23 18:45:27 +0100 | [diff] [blame] | 292 | ret = bounce_buffer_start(&bbstate, |
| 293 | (void*)data->dest, |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 294 | data->blocksize * |
| 295 | data->blocks, GEN_BB_WRITE); |
| 296 | } else { |
Marek Vasut | 72d37b6 | 2019-03-23 18:45:27 +0100 | [diff] [blame] | 297 | ret = bounce_buffer_start(&bbstate, |
| 298 | (void*)data->src, |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 299 | data->blocksize * |
| 300 | data->blocks, GEN_BB_READ); |
| 301 | } |
Marek Vasut | 72d37b6 | 2019-03-23 18:45:27 +0100 | [diff] [blame] | 302 | |
| 303 | if (ret) |
| 304 | return ret; |
| 305 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 306 | dwmci_prepare_data(host, data, cur_idmac, |
| 307 | bbstate.bounce_buffer); |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 308 | } |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 309 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 310 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 311 | dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg); |
| 312 | |
| 313 | if (data) |
| 314 | flags = dwmci_set_transfer_mode(host, data); |
| 315 | |
| 316 | if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) |
John Keeping | feb7fa3 | 2021-12-07 16:09:35 +0000 | [diff] [blame] | 317 | return -EBUSY; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 318 | |
| 319 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 320 | flags |= DWMCI_CMD_ABORT_STOP; |
| 321 | else |
| 322 | flags |= DWMCI_CMD_PRV_DAT_WAIT; |
| 323 | |
| 324 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 325 | flags |= DWMCI_CMD_RESP_EXP; |
| 326 | if (cmd->resp_type & MMC_RSP_136) |
| 327 | flags |= DWMCI_CMD_RESP_LENGTH; |
| 328 | } |
| 329 | |
| 330 | if (cmd->resp_type & MMC_RSP_CRC) |
| 331 | flags |= DWMCI_CMD_CHECK_CRC; |
| 332 | |
| 333 | flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG); |
| 334 | |
| 335 | debug("Sending CMD%d\n",cmd->cmdidx); |
| 336 | |
| 337 | dwmci_writel(host, DWMCI_CMD, flags); |
| 338 | |
| 339 | for (i = 0; i < retry; i++) { |
| 340 | mask = dwmci_readl(host, DWMCI_RINTSTS); |
| 341 | if (mask & DWMCI_INTMSK_CDONE) { |
| 342 | if (!data) |
| 343 | dwmci_writel(host, DWMCI_RINTSTS, mask); |
| 344 | break; |
| 345 | } |
| 346 | } |
| 347 | |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 348 | if (i == retry) { |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 349 | debug("%s: Timeout.\n", __func__); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 350 | return -ETIMEDOUT; |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 351 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 352 | |
| 353 | if (mask & DWMCI_INTMSK_RTO) { |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 354 | /* |
| 355 | * Timeout here is not necessarily fatal. (e)MMC cards |
| 356 | * will splat here when they receive CMD55 as they do |
| 357 | * not support this command and that is exactly the way |
| 358 | * to tell them apart from SD cards. Thus, this output |
| 359 | * below shall be debug(). eMMC cards also do not favor |
| 360 | * CMD8, please keep that in mind. |
| 361 | */ |
| 362 | debug("%s: Response Timeout.\n", __func__); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 363 | return -ETIMEDOUT; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 364 | } else if (mask & DWMCI_INTMSK_RE) { |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 365 | debug("%s: Response Error.\n", __func__); |
| 366 | return -EIO; |
Marek Vasut | a6d9199 | 2018-11-06 23:42:11 +0100 | [diff] [blame] | 367 | } else if ((cmd->resp_type & MMC_RSP_CRC) && |
| 368 | (mask & DWMCI_INTMSK_RCRC)) { |
| 369 | debug("%s: Response CRC Error.\n", __func__); |
| 370 | return -EIO; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 371 | } |
| 372 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 373 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 374 | if (cmd->resp_type & MMC_RSP_136) { |
| 375 | cmd->response[0] = dwmci_readl(host, DWMCI_RESP3); |
| 376 | cmd->response[1] = dwmci_readl(host, DWMCI_RESP2); |
| 377 | cmd->response[2] = dwmci_readl(host, DWMCI_RESP1); |
| 378 | cmd->response[3] = dwmci_readl(host, DWMCI_RESP0); |
| 379 | } else { |
| 380 | cmd->response[0] = dwmci_readl(host, DWMCI_RESP0); |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | if (data) { |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 385 | ret = dwmci_data_transfer(host, data); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 386 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 387 | /* only dma mode need it */ |
| 388 | if (!host->fifo_mode) { |
Ley Foon Tan | b98e892 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 389 | if (data->flags == MMC_DATA_READ) |
| 390 | mask = DWMCI_IDINTEN_RI; |
| 391 | else |
| 392 | mask = DWMCI_IDINTEN_TI; |
| 393 | ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS, |
| 394 | mask, true, 1000, false); |
| 395 | if (ret) |
| 396 | debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n", |
| 397 | __func__, mask); |
| 398 | /* clear interrupts */ |
| 399 | dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK); |
| 400 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 401 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 402 | ctrl &= ~(DWMCI_DMA_EN); |
| 403 | dwmci_writel(host, DWMCI_CTRL, ctrl); |
| 404 | bounce_buffer_stop(&bbstate); |
| 405 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 406 | } |
| 407 | |
| 408 | udelay(100); |
| 409 | |
Marek Vasut | 81e093f | 2015-07-27 22:39:38 +0200 | [diff] [blame] | 410 | return ret; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame^] | 413 | static int dwmci_control_clken(struct dwmci_host *host, bool on) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 414 | { |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame^] | 415 | const u32 val = on ? DWMCI_CLKEN_ENABLE | DWMCI_CLKEN_LOW_PWR : 0; |
| 416 | const u32 cmd_only_clk = DWMCI_CMD_PRV_DAT_WAIT | DWMCI_CMD_UPD_CLK; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 417 | int timeout = 10000; |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame^] | 418 | u32 status; |
| 419 | |
| 420 | dwmci_writel(host, DWMCI_CLKENA, val); |
| 421 | |
| 422 | /* Inform CIU */ |
| 423 | dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_START | cmd_only_clk); |
| 424 | do { |
| 425 | status = dwmci_readl(host, DWMCI_CMD); |
| 426 | if (timeout-- < 0) { |
| 427 | debug("%s: Timeout!\n", __func__); |
| 428 | return -ETIMEDOUT; |
| 429 | } |
| 430 | } while (status & DWMCI_CMD_START); |
| 431 | |
| 432 | return 0; |
| 433 | } |
| 434 | |
| 435 | /* |
| 436 | * Update the clock divider. |
| 437 | * |
| 438 | * To prevent a clock glitch keep the clock stopped during the update of |
| 439 | * clock divider and clock source. |
| 440 | */ |
| 441 | static int dwmci_update_div(struct dwmci_host *host, u32 div) |
| 442 | { |
| 443 | int ret; |
| 444 | |
| 445 | /* Disable clock */ |
| 446 | ret = dwmci_control_clken(host, false); |
| 447 | if (ret) |
| 448 | return ret; |
| 449 | |
| 450 | /* Set clock to desired speed */ |
| 451 | dwmci_writel(host, DWMCI_CLKDIV, div); |
| 452 | dwmci_writel(host, DWMCI_CLKSRC, 0); |
| 453 | |
| 454 | /* Enable clock */ |
| 455 | return dwmci_control_clken(host, true); |
| 456 | } |
| 457 | |
| 458 | static int dwmci_setup_bus(struct dwmci_host *host, u32 freq) |
| 459 | { |
| 460 | u32 div; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 461 | unsigned long sclk; |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame^] | 462 | int ret; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 463 | |
Amar | 902664c | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 464 | if ((freq == host->clock) || (freq == 0)) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 465 | return 0; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 466 | /* |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 467 | * If host->get_mmc_clk isn't defined, |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 468 | * then assume that host->bus_hz is source clock value. |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 469 | * host->bus_hz should be set by user. |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 470 | */ |
Jaehoon Chung | d94735b | 2013-10-06 18:59:31 +0900 | [diff] [blame] | 471 | if (host->get_mmc_clk) |
Simon Glass | eff7668 | 2015-08-30 16:55:15 -0600 | [diff] [blame] | 472 | sclk = host->get_mmc_clk(host, freq); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 473 | else if (host->bus_hz) |
| 474 | sclk = host->bus_hz; |
| 475 | else { |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 476 | debug("%s: Didn't get source clock value.\n", __func__); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 477 | return -EINVAL; |
| 478 | } |
| 479 | |
Chin Liang See | 4cfff95 | 2014-06-10 01:26:52 -0500 | [diff] [blame] | 480 | if (sclk == freq) |
| 481 | div = 0; /* bypass mode */ |
| 482 | else |
| 483 | div = DIV_ROUND_UP(sclk, 2 * freq); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 484 | |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame^] | 485 | ret = dwmci_update_div(host, div); |
| 486 | if (ret) |
| 487 | return ret; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 488 | |
| 489 | host->clock = freq; |
| 490 | |
| 491 | return 0; |
| 492 | } |
| 493 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 494 | #ifdef CONFIG_DM_MMC |
Jaehoon Chung | ad220ac | 2016-06-28 15:52:21 +0900 | [diff] [blame] | 495 | static int dwmci_set_ios(struct udevice *dev) |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 496 | { |
| 497 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 498 | #else |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 499 | static int dwmci_set_ios(struct mmc *mmc) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 500 | { |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 501 | #endif |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 502 | struct dwmci_host *host = (struct dwmci_host *)mmc->priv; |
| 503 | u32 ctype, regs; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 504 | |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 505 | debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 506 | |
| 507 | dwmci_setup_bus(host, mmc->clock); |
| 508 | switch (mmc->bus_width) { |
| 509 | case 8: |
| 510 | ctype = DWMCI_CTYPE_8BIT; |
| 511 | break; |
| 512 | case 4: |
| 513 | ctype = DWMCI_CTYPE_4BIT; |
| 514 | break; |
| 515 | default: |
| 516 | ctype = DWMCI_CTYPE_1BIT; |
| 517 | break; |
| 518 | } |
| 519 | |
| 520 | dwmci_writel(host, DWMCI_CTYPE, ctype); |
| 521 | |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 522 | regs = dwmci_readl(host, DWMCI_UHS_REG); |
Andrew Gabbasov | 54c0e22 | 2014-12-01 06:59:12 -0600 | [diff] [blame] | 523 | if (mmc->ddr_mode) |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 524 | regs |= DWMCI_DDR_MODE; |
| 525 | else |
Jaehoon Chung | 401fc50 | 2015-01-14 17:37:53 +0900 | [diff] [blame] | 526 | regs &= ~DWMCI_DDR_MODE; |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 527 | |
| 528 | dwmci_writel(host, DWMCI_UHS_REG, regs); |
| 529 | |
Siew Chin Lim | c51e7e1 | 2020-12-24 18:21:03 +0800 | [diff] [blame] | 530 | if (host->clksel) { |
| 531 | int ret; |
| 532 | |
| 533 | ret = host->clksel(host); |
| 534 | if (ret) |
| 535 | return ret; |
| 536 | } |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 537 | |
Urja Rannikko | 9932a01 | 2019-05-13 13:25:27 +0000 | [diff] [blame] | 538 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 539 | if (mmc->vqmmc_supply) { |
| 540 | int ret; |
| 541 | |
Jonas Karlman | a117d61 | 2023-07-19 21:21:00 +0000 | [diff] [blame] | 542 | ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false); |
| 543 | if (ret) |
| 544 | return ret; |
| 545 | |
Urja Rannikko | 9932a01 | 2019-05-13 13:25:27 +0000 | [diff] [blame] | 546 | if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) |
| 547 | regulator_set_value(mmc->vqmmc_supply, 1800000); |
| 548 | else |
| 549 | regulator_set_value(mmc->vqmmc_supply, 3300000); |
| 550 | |
| 551 | ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true); |
| 552 | if (ret) |
| 553 | return ret; |
| 554 | } |
| 555 | #endif |
| 556 | |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 557 | return 0; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 558 | } |
| 559 | |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 560 | static void dwmci_init_fifo(struct dwmci_host *host) |
| 561 | { |
| 562 | if (!host->fifoth_val) { |
| 563 | u32 fifo_size; |
| 564 | |
| 565 | fifo_size = dwmci_readl(host, DWMCI_FIFOTH); |
| 566 | fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1; |
| 567 | host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) | |
| 568 | TX_WMARK(fifo_size / 2); |
| 569 | } |
| 570 | |
| 571 | dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val); |
| 572 | } |
| 573 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 574 | static int dwmci_init(struct mmc *mmc) |
| 575 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 576 | struct dwmci_host *host = mmc->priv; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 577 | |
Jaehoon Chung | 42f81a8 | 2013-11-29 20:08:57 +0900 | [diff] [blame] | 578 | if (host->board_init) |
| 579 | host->board_init(host); |
Rajeshwari Shinde | 7016309 | 2013-10-29 12:53:13 +0530 | [diff] [blame] | 580 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 581 | dwmci_writel(host, DWMCI_PWREN, 1); |
| 582 | |
| 583 | if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) { |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 584 | debug("%s[%d] Fail-reset!!\n", __func__, __LINE__); |
| 585 | return -EIO; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 586 | } |
| 587 | |
Amar | 902664c | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 588 | /* Enumerate at 400KHz */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 589 | dwmci_setup_bus(host, mmc->cfg->f_min); |
Amar | 902664c | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 590 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 591 | dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF); |
| 592 | dwmci_writel(host, DWMCI_INTMASK, 0); |
| 593 | |
| 594 | dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF); |
| 595 | |
| 596 | dwmci_writel(host, DWMCI_IDINTEN, 0); |
| 597 | dwmci_writel(host, DWMCI_BMOD, 1); |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 598 | dwmci_init_fifo(host); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 599 | |
| 600 | dwmci_writel(host, DWMCI_CLKENA, 0); |
| 601 | dwmci_writel(host, DWMCI_CLKSRC, 0); |
| 602 | |
Ley Foon Tan | b98e892 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 603 | if (!host->fifo_mode) |
| 604 | dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK); |
| 605 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 606 | return 0; |
| 607 | } |
| 608 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 609 | #ifdef CONFIG_DM_MMC |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 610 | int dwmci_probe(struct udevice *dev) |
| 611 | { |
| 612 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 613 | |
| 614 | return dwmci_init(mmc); |
| 615 | } |
| 616 | |
| 617 | const struct dm_mmc_ops dm_dwmci_ops = { |
| 618 | .send_cmd = dwmci_send_cmd, |
| 619 | .set_ios = dwmci_set_ios, |
| 620 | }; |
| 621 | |
| 622 | #else |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 623 | static const struct mmc_ops dwmci_ops = { |
| 624 | .send_cmd = dwmci_send_cmd, |
| 625 | .set_ios = dwmci_set_ios, |
| 626 | .init = dwmci_init, |
| 627 | }; |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 628 | #endif |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 629 | |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 630 | void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host, |
| 631 | u32 max_clk, u32 min_clk) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 632 | { |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 633 | cfg->name = host->name; |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 634 | #ifndef CONFIG_DM_MMC |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 635 | cfg->ops = &dwmci_ops; |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 636 | #endif |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 637 | cfg->f_min = min_clk; |
| 638 | cfg->f_max = max_clk; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 639 | |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 640 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 641 | |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 642 | cfg->host_caps = host->caps; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 643 | |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 644 | if (host->buswidth == 8) { |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 645 | cfg->host_caps |= MMC_MODE_8BIT; |
| 646 | cfg->host_caps &= ~MMC_MODE_4BIT; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 647 | } else { |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 648 | cfg->host_caps |= MMC_MODE_4BIT; |
| 649 | cfg->host_caps &= ~MMC_MODE_8BIT; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 650 | } |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 651 | cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz; |
| 652 | |
| 653 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 654 | } |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 655 | |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 656 | #ifdef CONFIG_BLK |
| 657 | int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg) |
| 658 | { |
| 659 | return mmc_bind(dev, mmc, cfg); |
| 660 | } |
| 661 | #else |
| 662 | int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk) |
| 663 | { |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 664 | dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 665 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 666 | host->mmc = mmc_create(&host->cfg, host); |
| 667 | if (host->mmc == NULL) |
| 668 | return -1; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 669 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 670 | return 0; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 671 | } |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 672 | #endif |