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