Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Jaehoon Chung | 757bff4 | 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 | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 8 | #include <bouncebuf.h> |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | cf92e05 | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 12 | #include <memalign.h> |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 13 | #include <mmc.h> |
| 14 | #include <dwmmc.h> |
Ley Foon Tan | 7997599 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 15 | #include <wait_bit.h> |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 16 | |
| 17 | #define PAGE_SIZE 4096 |
| 18 | |
| 19 | static int dwmci_wait_reset(struct dwmci_host *host, u32 value) |
| 20 | { |
| 21 | unsigned long timeout = 1000; |
| 22 | u32 ctrl; |
| 23 | |
| 24 | dwmci_writel(host, DWMCI_CTRL, value); |
| 25 | |
| 26 | while (timeout--) { |
| 27 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 28 | if (!(ctrl & DWMCI_RESET_ALL)) |
| 29 | return 1; |
| 30 | } |
| 31 | return 0; |
| 32 | } |
| 33 | |
| 34 | static void dwmci_set_idma_desc(struct dwmci_idmac *idmac, |
| 35 | u32 desc0, u32 desc1, u32 desc2) |
| 36 | { |
| 37 | struct dwmci_idmac *desc = idmac; |
| 38 | |
| 39 | desc->flags = desc0; |
| 40 | desc->cnt = desc1; |
| 41 | desc->addr = desc2; |
Prabhakar Kushwaha | 41f7be3 | 2015-10-25 13:18:25 +0530 | [diff] [blame] | 42 | desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | static void dwmci_prepare_data(struct dwmci_host *host, |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 46 | struct mmc_data *data, |
| 47 | struct dwmci_idmac *cur_idmac, |
| 48 | void *bounce_buffer) |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 49 | { |
| 50 | unsigned long ctrl; |
| 51 | unsigned int i = 0, flags, cnt, blk_cnt; |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 52 | ulong data_start, data_end; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 53 | |
| 54 | |
| 55 | blk_cnt = data->blocks; |
| 56 | |
| 57 | dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); |
| 58 | |
Ley Foon Tan | 7997599 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 59 | /* Clear IDMAC interrupt */ |
| 60 | dwmci_writel(host, DWMCI_IDSTS, 0xFFFFFFFF); |
| 61 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 62 | data_start = (ulong)cur_idmac; |
Prabhakar Kushwaha | 41f7be3 | 2015-10-25 13:18:25 +0530 | [diff] [blame] | 63 | dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 64 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 65 | do { |
| 66 | flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ; |
| 67 | flags |= (i == 0) ? DWMCI_IDMAC_FS : 0; |
| 68 | if (blk_cnt <= 8) { |
| 69 | flags |= DWMCI_IDMAC_LD; |
| 70 | cnt = data->blocksize * blk_cnt; |
| 71 | } else |
| 72 | cnt = data->blocksize * 8; |
| 73 | |
| 74 | dwmci_set_idma_desc(cur_idmac, flags, cnt, |
Prabhakar Kushwaha | 41f7be3 | 2015-10-25 13:18:25 +0530 | [diff] [blame] | 75 | (ulong)bounce_buffer + (i * PAGE_SIZE)); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 76 | |
Mischa Jonker | 21bd576 | 2013-07-26 16:18:40 +0200 | [diff] [blame] | 77 | if (blk_cnt <= 8) |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 78 | break; |
| 79 | blk_cnt -= 8; |
| 80 | cur_idmac++; |
| 81 | i++; |
| 82 | } while(1); |
| 83 | |
| 84 | data_end = (ulong)cur_idmac; |
| 85 | flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN); |
| 86 | |
| 87 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 88 | ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN; |
| 89 | dwmci_writel(host, DWMCI_CTRL, ctrl); |
| 90 | |
| 91 | ctrl = dwmci_readl(host, DWMCI_BMOD); |
| 92 | ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN; |
| 93 | dwmci_writel(host, DWMCI_BMOD, ctrl); |
| 94 | |
| 95 | dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); |
| 96 | dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks); |
| 97 | } |
| 98 | |
Heiko Stuebner | 05fa06b | 2018-09-21 10:59:45 +0200 | [diff] [blame] | 99 | static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len) |
| 100 | { |
| 101 | u32 timeout = 20000; |
| 102 | |
| 103 | *len = dwmci_readl(host, DWMCI_STATUS); |
| 104 | while (--timeout && (*len & bit)) { |
| 105 | udelay(200); |
| 106 | *len = dwmci_readl(host, DWMCI_STATUS); |
| 107 | } |
| 108 | |
| 109 | if (!timeout) { |
| 110 | debug("%s: FIFO underflow timeout\n", __func__); |
| 111 | return -ETIMEDOUT; |
| 112 | } |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 117 | static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data) |
huang lin | f382eb8 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 118 | { |
| 119 | int ret = 0; |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 120 | u32 timeout = 240000; |
| 121 | u32 mask, size, i, len = 0; |
| 122 | u32 *buf = NULL; |
huang lin | f382eb8 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 123 | ulong start = get_timer(0); |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 124 | u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >> |
| 125 | RX_WMARK_SHIFT) + 1) * 2; |
| 126 | |
| 127 | size = data->blocksize * data->blocks / 4; |
| 128 | if (data->flags == MMC_DATA_READ) |
| 129 | buf = (unsigned int *)data->dest; |
| 130 | else |
| 131 | buf = (unsigned int *)data->src; |
huang lin | f382eb8 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 132 | |
| 133 | for (;;) { |
| 134 | mask = dwmci_readl(host, DWMCI_RINTSTS); |
| 135 | /* Error during data transfer. */ |
| 136 | if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) { |
| 137 | debug("%s: DATA ERROR!\n", __func__); |
| 138 | ret = -EINVAL; |
| 139 | break; |
| 140 | } |
| 141 | |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 142 | if (host->fifo_mode && size) { |
Xu Ziyuan | 720724d | 2016-07-28 10:25:48 +0800 | [diff] [blame] | 143 | len = 0; |
Jacob Chen | 2b42903 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 144 | if (data->flags == MMC_DATA_READ && |
| 145 | (mask & DWMCI_INTMSK_RXDR)) { |
| 146 | while (size) { |
Heiko Stuebner | 05fa06b | 2018-09-21 10:59:45 +0200 | [diff] [blame] | 147 | ret = dwmci_fifo_ready(host, |
| 148 | DWMCI_FIFO_EMPTY, |
| 149 | &len); |
| 150 | if (ret < 0) |
| 151 | break; |
| 152 | |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 153 | len = (len >> DWMCI_FIFO_SHIFT) & |
| 154 | DWMCI_FIFO_MASK; |
Xu Ziyuan | 2990e07 | 2016-07-28 10:25:47 +0800 | [diff] [blame] | 155 | len = min(size, len); |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 156 | for (i = 0; i < len; i++) |
| 157 | *buf++ = |
| 158 | dwmci_readl(host, DWMCI_DATA); |
Jacob Chen | 2b42903 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 159 | size = size > len ? (size - len) : 0; |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 160 | } |
Jacob Chen | 2b42903 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 161 | dwmci_writel(host, DWMCI_RINTSTS, |
| 162 | DWMCI_INTMSK_RXDR); |
| 163 | } else if (data->flags == MMC_DATA_WRITE && |
| 164 | (mask & DWMCI_INTMSK_TXDR)) { |
| 165 | while (size) { |
Heiko Stuebner | 05fa06b | 2018-09-21 10:59:45 +0200 | [diff] [blame] | 166 | ret = dwmci_fifo_ready(host, |
| 167 | DWMCI_FIFO_FULL, |
| 168 | &len); |
| 169 | if (ret < 0) |
| 170 | break; |
| 171 | |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 172 | len = fifo_depth - ((len >> |
| 173 | DWMCI_FIFO_SHIFT) & |
| 174 | DWMCI_FIFO_MASK); |
Xu Ziyuan | 2990e07 | 2016-07-28 10:25:47 +0800 | [diff] [blame] | 175 | len = min(size, len); |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 176 | for (i = 0; i < len; i++) |
| 177 | dwmci_writel(host, DWMCI_DATA, |
| 178 | *buf++); |
Jacob Chen | 2b42903 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 179 | size = size > len ? (size - len) : 0; |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 180 | } |
Jacob Chen | 2b42903 | 2016-09-19 10:16:50 +0800 | [diff] [blame] | 181 | dwmci_writel(host, DWMCI_RINTSTS, |
| 182 | DWMCI_INTMSK_TXDR); |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 183 | } |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 184 | } |
| 185 | |
huang lin | f382eb8 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 186 | /* Data arrived correctly. */ |
| 187 | if (mask & DWMCI_INTMSK_DTO) { |
| 188 | ret = 0; |
| 189 | break; |
| 190 | } |
| 191 | |
| 192 | /* Check for timeout. */ |
| 193 | if (get_timer(start) > timeout) { |
| 194 | debug("%s: Timeout waiting for data!\n", |
| 195 | __func__); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 196 | ret = -ETIMEDOUT; |
huang lin | f382eb8 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 197 | break; |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | dwmci_writel(host, DWMCI_RINTSTS, mask); |
| 202 | |
| 203 | return ret; |
| 204 | } |
| 205 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 206 | static int dwmci_set_transfer_mode(struct dwmci_host *host, |
| 207 | struct mmc_data *data) |
| 208 | { |
| 209 | unsigned long mode; |
| 210 | |
| 211 | mode = DWMCI_CMD_DATA_EXP; |
| 212 | if (data->flags & MMC_DATA_WRITE) |
| 213 | mode |= DWMCI_CMD_RW; |
| 214 | |
| 215 | return mode; |
| 216 | } |
| 217 | |
Simon Glass | e7881d8 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 218 | #ifdef CONFIG_DM_MMC |
Jaehoon Chung | 5628347 | 2016-06-28 15:52:21 +0900 | [diff] [blame] | 219 | static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 220 | struct mmc_data *data) |
| 221 | { |
| 222 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 223 | #else |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 224 | static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 225 | struct mmc_data *data) |
| 226 | { |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 227 | #endif |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 228 | struct dwmci_host *host = mmc->priv; |
Mischa Jonker | 2136d22 | 2013-07-26 14:08:14 +0200 | [diff] [blame] | 229 | ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac, |
Mischa Jonker | 21bd576 | 2013-07-26 16:18:40 +0200 | [diff] [blame] | 230 | data ? DIV_ROUND_UP(data->blocks, 8) : 0); |
Marek Vasut | 9042d97 | 2015-07-27 22:39:38 +0200 | [diff] [blame] | 231 | int ret = 0, flags = 0, i; |
Xu Ziyuan | 02ebd42 | 2016-07-19 09:38:22 +0800 | [diff] [blame] | 232 | unsigned int timeout = 500; |
Alexander Graf | 9b5b8b6 | 2016-03-04 01:09:52 +0100 | [diff] [blame] | 233 | u32 retry = 100000; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 234 | u32 mask, ctrl; |
Amar | 9c50e35 | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 235 | ulong start = get_timer(0); |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 236 | struct bounce_buffer bbstate; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 237 | |
| 238 | while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) { |
Amar | 9c50e35 | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 239 | if (get_timer(start) > timeout) { |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 240 | debug("%s: Timeout on data busy\n", __func__); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 241 | return -ETIMEDOUT; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 242 | } |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL); |
| 246 | |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 247 | if (data) { |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 248 | if (host->fifo_mode) { |
| 249 | dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); |
| 250 | dwmci_writel(host, DWMCI_BYTCNT, |
| 251 | data->blocksize * data->blocks); |
| 252 | dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 253 | } else { |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 254 | if (data->flags == MMC_DATA_READ) { |
| 255 | bounce_buffer_start(&bbstate, (void*)data->dest, |
| 256 | data->blocksize * |
| 257 | data->blocks, GEN_BB_WRITE); |
| 258 | } else { |
| 259 | bounce_buffer_start(&bbstate, (void*)data->src, |
| 260 | data->blocksize * |
| 261 | data->blocks, GEN_BB_READ); |
| 262 | } |
| 263 | dwmci_prepare_data(host, data, cur_idmac, |
| 264 | bbstate.bounce_buffer); |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 265 | } |
Alexey Brodkin | 2a7a210 | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 266 | } |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 267 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 268 | dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg); |
| 269 | |
| 270 | if (data) |
| 271 | flags = dwmci_set_transfer_mode(host, data); |
| 272 | |
| 273 | if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) |
| 274 | return -1; |
| 275 | |
| 276 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 277 | flags |= DWMCI_CMD_ABORT_STOP; |
| 278 | else |
| 279 | flags |= DWMCI_CMD_PRV_DAT_WAIT; |
| 280 | |
| 281 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 282 | flags |= DWMCI_CMD_RESP_EXP; |
| 283 | if (cmd->resp_type & MMC_RSP_136) |
| 284 | flags |= DWMCI_CMD_RESP_LENGTH; |
| 285 | } |
| 286 | |
| 287 | if (cmd->resp_type & MMC_RSP_CRC) |
| 288 | flags |= DWMCI_CMD_CHECK_CRC; |
| 289 | |
| 290 | flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG); |
| 291 | |
| 292 | debug("Sending CMD%d\n",cmd->cmdidx); |
| 293 | |
| 294 | dwmci_writel(host, DWMCI_CMD, flags); |
| 295 | |
| 296 | for (i = 0; i < retry; i++) { |
| 297 | mask = dwmci_readl(host, DWMCI_RINTSTS); |
| 298 | if (mask & DWMCI_INTMSK_CDONE) { |
| 299 | if (!data) |
| 300 | dwmci_writel(host, DWMCI_RINTSTS, mask); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | |
Pavel Machek | f33c930 | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 305 | if (i == retry) { |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 306 | debug("%s: Timeout.\n", __func__); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 307 | return -ETIMEDOUT; |
Pavel Machek | f33c930 | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 308 | } |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 309 | |
| 310 | if (mask & DWMCI_INTMSK_RTO) { |
Pavel Machek | f33c930 | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 311 | /* |
| 312 | * Timeout here is not necessarily fatal. (e)MMC cards |
| 313 | * will splat here when they receive CMD55 as they do |
| 314 | * not support this command and that is exactly the way |
| 315 | * to tell them apart from SD cards. Thus, this output |
| 316 | * below shall be debug(). eMMC cards also do not favor |
| 317 | * CMD8, please keep that in mind. |
| 318 | */ |
| 319 | debug("%s: Response Timeout.\n", __func__); |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 320 | return -ETIMEDOUT; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 321 | } else if (mask & DWMCI_INTMSK_RE) { |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 322 | debug("%s: Response Error.\n", __func__); |
| 323 | return -EIO; |
Marek Vasut | 26cc40d | 2018-11-06 23:42:11 +0100 | [diff] [blame] | 324 | } else if ((cmd->resp_type & MMC_RSP_CRC) && |
| 325 | (mask & DWMCI_INTMSK_RCRC)) { |
| 326 | debug("%s: Response CRC Error.\n", __func__); |
| 327 | return -EIO; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | |
| 331 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 332 | if (cmd->resp_type & MMC_RSP_136) { |
| 333 | cmd->response[0] = dwmci_readl(host, DWMCI_RESP3); |
| 334 | cmd->response[1] = dwmci_readl(host, DWMCI_RESP2); |
| 335 | cmd->response[2] = dwmci_readl(host, DWMCI_RESP1); |
| 336 | cmd->response[3] = dwmci_readl(host, DWMCI_RESP0); |
| 337 | } else { |
| 338 | cmd->response[0] = dwmci_readl(host, DWMCI_RESP0); |
| 339 | } |
| 340 | } |
| 341 | |
| 342 | if (data) { |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 343 | ret = dwmci_data_transfer(host, data); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 344 | |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 345 | /* only dma mode need it */ |
| 346 | if (!host->fifo_mode) { |
Ley Foon Tan | 7997599 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 347 | if (data->flags == MMC_DATA_READ) |
| 348 | mask = DWMCI_IDINTEN_RI; |
| 349 | else |
| 350 | mask = DWMCI_IDINTEN_TI; |
| 351 | ret = wait_for_bit_le32(host->ioaddr + DWMCI_IDSTS, |
| 352 | mask, true, 1000, false); |
| 353 | if (ret) |
| 354 | debug("%s: DWMCI_IDINTEN mask 0x%x timeout.\n", |
| 355 | __func__, mask); |
| 356 | /* clear interrupts */ |
| 357 | dwmci_writel(host, DWMCI_IDSTS, DWMCI_IDINTEN_MASK); |
| 358 | |
huang lin | a65f51b | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 359 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 360 | ctrl &= ~(DWMCI_DMA_EN); |
| 361 | dwmci_writel(host, DWMCI_CTRL, ctrl); |
| 362 | bounce_buffer_stop(&bbstate); |
| 363 | } |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | udelay(100); |
| 367 | |
Marek Vasut | 9042d97 | 2015-07-27 22:39:38 +0200 | [diff] [blame] | 368 | return ret; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | static int dwmci_setup_bus(struct dwmci_host *host, u32 freq) |
| 372 | { |
| 373 | u32 div, status; |
| 374 | int timeout = 10000; |
| 375 | unsigned long sclk; |
| 376 | |
Amar | 9c50e35 | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 377 | if ((freq == host->clock) || (freq == 0)) |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 378 | return 0; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 379 | /* |
Pavel Machek | f33c930 | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 380 | * If host->get_mmc_clk isn't defined, |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 381 | * then assume that host->bus_hz is source clock value. |
Pavel Machek | f33c930 | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 382 | * host->bus_hz should be set by user. |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 383 | */ |
Jaehoon Chung | b44fe83 | 2013-10-06 18:59:31 +0900 | [diff] [blame] | 384 | if (host->get_mmc_clk) |
Simon Glass | e3563f2 | 2015-08-30 16:55:15 -0600 | [diff] [blame] | 385 | sclk = host->get_mmc_clk(host, freq); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 386 | else if (host->bus_hz) |
| 387 | sclk = host->bus_hz; |
| 388 | else { |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 389 | debug("%s: Didn't get source clock value.\n", __func__); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 390 | return -EINVAL; |
| 391 | } |
| 392 | |
Chin Liang See | 6ace153 | 2014-06-10 01:26:52 -0500 | [diff] [blame] | 393 | if (sclk == freq) |
| 394 | div = 0; /* bypass mode */ |
| 395 | else |
| 396 | div = DIV_ROUND_UP(sclk, 2 * freq); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 397 | |
| 398 | dwmci_writel(host, DWMCI_CLKENA, 0); |
| 399 | dwmci_writel(host, DWMCI_CLKSRC, 0); |
| 400 | |
| 401 | dwmci_writel(host, DWMCI_CLKDIV, div); |
| 402 | dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT | |
| 403 | DWMCI_CMD_UPD_CLK | DWMCI_CMD_START); |
| 404 | |
| 405 | do { |
| 406 | status = dwmci_readl(host, DWMCI_CMD); |
| 407 | if (timeout-- < 0) { |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 408 | debug("%s: Timeout!\n", __func__); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 409 | return -ETIMEDOUT; |
| 410 | } |
| 411 | } while (status & DWMCI_CMD_START); |
| 412 | |
| 413 | dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE | |
| 414 | DWMCI_CLKEN_LOW_PWR); |
| 415 | |
| 416 | dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT | |
| 417 | DWMCI_CMD_UPD_CLK | DWMCI_CMD_START); |
| 418 | |
| 419 | timeout = 10000; |
| 420 | do { |
| 421 | status = dwmci_readl(host, DWMCI_CMD); |
| 422 | if (timeout-- < 0) { |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 423 | debug("%s: Timeout!\n", __func__); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 424 | return -ETIMEDOUT; |
| 425 | } |
| 426 | } while (status & DWMCI_CMD_START); |
| 427 | |
| 428 | host->clock = freq; |
| 429 | |
| 430 | return 0; |
| 431 | } |
| 432 | |
Simon Glass | e7881d8 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 433 | #ifdef CONFIG_DM_MMC |
Jaehoon Chung | 5628347 | 2016-06-28 15:52:21 +0900 | [diff] [blame] | 434 | static int dwmci_set_ios(struct udevice *dev) |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 435 | { |
| 436 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 437 | #else |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 438 | static int dwmci_set_ios(struct mmc *mmc) |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 439 | { |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 440 | #endif |
Jaehoon Chung | 045bdcd | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 441 | struct dwmci_host *host = (struct dwmci_host *)mmc->priv; |
| 442 | u32 ctype, regs; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 443 | |
Pavel Machek | f33c930 | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 444 | debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 445 | |
| 446 | dwmci_setup_bus(host, mmc->clock); |
| 447 | switch (mmc->bus_width) { |
| 448 | case 8: |
| 449 | ctype = DWMCI_CTYPE_8BIT; |
| 450 | break; |
| 451 | case 4: |
| 452 | ctype = DWMCI_CTYPE_4BIT; |
| 453 | break; |
| 454 | default: |
| 455 | ctype = DWMCI_CTYPE_1BIT; |
| 456 | break; |
| 457 | } |
| 458 | |
| 459 | dwmci_writel(host, DWMCI_CTYPE, ctype); |
| 460 | |
Jaehoon Chung | 045bdcd | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 461 | regs = dwmci_readl(host, DWMCI_UHS_REG); |
Andrew Gabbasov | 2b8a969 | 2014-12-01 06:59:12 -0600 | [diff] [blame] | 462 | if (mmc->ddr_mode) |
Jaehoon Chung | 045bdcd | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 463 | regs |= DWMCI_DDR_MODE; |
| 464 | else |
Jaehoon Chung | afc9e2b | 2015-01-14 17:37:53 +0900 | [diff] [blame] | 465 | regs &= ~DWMCI_DDR_MODE; |
Jaehoon Chung | 045bdcd | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 466 | |
| 467 | dwmci_writel(host, DWMCI_UHS_REG, regs); |
| 468 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 469 | if (host->clksel) |
| 470 | host->clksel(host); |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 471 | |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 472 | return 0; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static int dwmci_init(struct mmc *mmc) |
| 476 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 477 | struct dwmci_host *host = mmc->priv; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 478 | |
Jaehoon Chung | 18ab675 | 2013-11-29 20:08:57 +0900 | [diff] [blame] | 479 | if (host->board_init) |
| 480 | host->board_init(host); |
Rajeshwari Shinde | 6f0b7ca | 2013-10-29 12:53:13 +0530 | [diff] [blame] | 481 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 482 | dwmci_writel(host, DWMCI_PWREN, 1); |
| 483 | |
| 484 | if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) { |
Simon Glass | 1c87ffe | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 485 | debug("%s[%d] Fail-reset!!\n", __func__, __LINE__); |
| 486 | return -EIO; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 487 | } |
| 488 | |
Amar | 9c50e35 | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 489 | /* Enumerate at 400KHz */ |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 490 | dwmci_setup_bus(host, mmc->cfg->f_min); |
Amar | 9c50e35 | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 491 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 492 | dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF); |
| 493 | dwmci_writel(host, DWMCI_INTMASK, 0); |
| 494 | |
| 495 | dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF); |
| 496 | |
| 497 | dwmci_writel(host, DWMCI_IDINTEN, 0); |
| 498 | dwmci_writel(host, DWMCI_BMOD, 1); |
| 499 | |
Simon Glass | 760177d | 2015-08-06 20:16:29 -0600 | [diff] [blame] | 500 | if (!host->fifoth_val) { |
| 501 | uint32_t fifo_size; |
| 502 | |
| 503 | fifo_size = dwmci_readl(host, DWMCI_FIFOTH); |
| 504 | fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1; |
| 505 | host->fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_size / 2 - 1) | |
| 506 | TX_WMARK(fifo_size / 2); |
Amar | 9c50e35 | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 507 | } |
Simon Glass | 760177d | 2015-08-06 20:16:29 -0600 | [diff] [blame] | 508 | dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 509 | |
| 510 | dwmci_writel(host, DWMCI_CLKENA, 0); |
| 511 | dwmci_writel(host, DWMCI_CLKSRC, 0); |
| 512 | |
Ley Foon Tan | 7997599 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 513 | if (!host->fifo_mode) |
| 514 | dwmci_writel(host, DWMCI_IDINTEN, DWMCI_IDINTEN_MASK); |
| 515 | |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 516 | return 0; |
| 517 | } |
| 518 | |
Simon Glass | e7881d8 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 519 | #ifdef CONFIG_DM_MMC |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 520 | int dwmci_probe(struct udevice *dev) |
| 521 | { |
| 522 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 523 | |
| 524 | return dwmci_init(mmc); |
| 525 | } |
| 526 | |
| 527 | const struct dm_mmc_ops dm_dwmci_ops = { |
| 528 | .send_cmd = dwmci_send_cmd, |
| 529 | .set_ios = dwmci_set_ios, |
| 530 | }; |
| 531 | |
| 532 | #else |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 533 | static const struct mmc_ops dwmci_ops = { |
| 534 | .send_cmd = dwmci_send_cmd, |
| 535 | .set_ios = dwmci_set_ios, |
| 536 | .init = dwmci_init, |
| 537 | }; |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 538 | #endif |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 539 | |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 540 | void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host, |
| 541 | u32 max_clk, u32 min_clk) |
Simon Glass | 5e6ff81 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 542 | { |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 543 | cfg->name = host->name; |
Simon Glass | e7881d8 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 544 | #ifndef CONFIG_DM_MMC |
Simon Glass | 5e6ff81 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 545 | cfg->ops = &dwmci_ops; |
Simon Glass | 691272f | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 546 | #endif |
Simon Glass | 5e6ff81 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 547 | cfg->f_min = min_clk; |
| 548 | cfg->f_max = max_clk; |
| 549 | |
| 550 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; |
| 551 | |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 552 | cfg->host_caps = host->caps; |
Simon Glass | 5e6ff81 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 553 | |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 554 | if (host->buswidth == 8) { |
Simon Glass | 5e6ff81 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 555 | cfg->host_caps |= MMC_MODE_8BIT; |
| 556 | cfg->host_caps &= ~MMC_MODE_4BIT; |
| 557 | } else { |
| 558 | cfg->host_caps |= MMC_MODE_4BIT; |
| 559 | cfg->host_caps &= ~MMC_MODE_8BIT; |
| 560 | } |
| 561 | cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz; |
| 562 | |
| 563 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 564 | } |
| 565 | |
| 566 | #ifdef CONFIG_BLK |
| 567 | int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg) |
| 568 | { |
| 569 | return mmc_bind(dev, mmc, cfg); |
| 570 | } |
| 571 | #else |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 572 | int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk) |
| 573 | { |
Jaehoon Chung | e5113c3 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 574 | dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk); |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 575 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 576 | host->mmc = mmc_create(&host->cfg, host); |
| 577 | if (host->mmc == NULL) |
| 578 | return -1; |
| 579 | |
| 580 | return 0; |
Jaehoon Chung | 757bff4 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 581 | } |
Simon Glass | 5e6ff81 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 582 | #endif |