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