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