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