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> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 9 | #include <cpu_func.h> |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 10 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.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> |
Ley Foon Tan | b98e892 | 2018-12-20 17:55:41 +0800 | [diff] [blame] | 16 | #include <wait_bit.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 17 | #include <asm/cache.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 18 | #include <linux/delay.h> |
Urja Rannikko | 9932a01 | 2019-05-13 13:25:27 +0000 | [diff] [blame] | 19 | #include <power/regulator.h> |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 20 | |
| 21 | #define PAGE_SIZE 4096 |
| 22 | |
Sam Protsenko | 6384a1f | 2024-08-07 22:14:15 -0500 | [diff] [blame] | 23 | /* Internal DMA Controller (IDMAC) descriptor for 32-bit addressing mode */ |
| 24 | struct dwmci_idmac32 { |
| 25 | u32 des0; /* Control descriptor */ |
| 26 | u32 des1; /* Buffer size */ |
| 27 | u32 des2; /* Buffer physical address */ |
| 28 | u32 des3; /* Next descriptor physical address */ |
Sam Protsenko | 2543c32 | 2024-08-07 22:14:08 -0500 | [diff] [blame] | 29 | } __aligned(ARCH_DMA_MINALIGN); |
| 30 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 31 | /* Internal DMA Controller (IDMAC) descriptor for 64-bit addressing mode */ |
| 32 | struct dwmci_idmac64 { |
| 33 | u32 des0; /* Control descriptor */ |
| 34 | u32 des1; /* Reserved */ |
| 35 | u32 des2; /* Buffer sizes */ |
| 36 | u32 des3; /* Reserved */ |
| 37 | u32 des4; /* Lower 32-bits of Buffer Address Pointer 1 */ |
| 38 | u32 des5; /* Upper 32-bits of Buffer Address Pointer 1 */ |
| 39 | u32 des6; /* Lower 32-bits of Next Descriptor Address */ |
| 40 | u32 des7; /* Upper 32-bits of Next Descriptor Address */ |
| 41 | } __aligned(ARCH_DMA_MINALIGN); |
| 42 | |
| 43 | /* Register offsets for DW MMC blocks with 32-bit IDMAC */ |
| 44 | static const struct dwmci_idmac_regs dwmci_idmac_regs32 = { |
| 45 | .dbaddrl = DWMCI_DBADDR, |
| 46 | .idsts = DWMCI_IDSTS, |
| 47 | .idinten = DWMCI_IDINTEN, |
| 48 | .dscaddrl = DWMCI_DSCADDR, |
| 49 | .bufaddrl = DWMCI_BUFADDR, |
| 50 | }; |
| 51 | |
| 52 | /* Register offsets for DW MMC blocks with 64-bit IDMAC */ |
| 53 | static const struct dwmci_idmac_regs dwmci_idmac_regs64 = { |
| 54 | .dbaddrl = DWMCI_DBADDRL, |
| 55 | .dbaddru = DWMCI_DBADDRU, |
| 56 | .idsts = DWMCI_IDSTS64, |
| 57 | .idinten = DWMCI_IDINTEN64, |
| 58 | .dscaddrl = DWMCI_DSCADDRL, |
| 59 | .dscaddru = DWMCI_DSCADDRU, |
| 60 | .bufaddrl = DWMCI_BUFADDRL, |
| 61 | .bufaddru = DWMCI_BUFADDRU, |
| 62 | }; |
| 63 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 64 | static int dwmci_wait_reset(struct dwmci_host *host, u32 value) |
| 65 | { |
| 66 | unsigned long timeout = 1000; |
| 67 | u32 ctrl; |
| 68 | |
| 69 | dwmci_writel(host, DWMCI_CTRL, value); |
| 70 | |
| 71 | while (timeout--) { |
| 72 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 73 | if (!(ctrl & DWMCI_RESET_ALL)) |
| 74 | return 1; |
| 75 | } |
| 76 | return 0; |
| 77 | } |
| 78 | |
Sam Protsenko | 6384a1f | 2024-08-07 22:14:15 -0500 | [diff] [blame] | 79 | static void dwmci_set_idma_desc32(struct dwmci_idmac32 *desc, u32 control, |
| 80 | u32 buf_size, u32 buf_addr) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 81 | { |
Sam Protsenko | 6384a1f | 2024-08-07 22:14:15 -0500 | [diff] [blame] | 82 | phys_addr_t desc_phys = virt_to_phys(desc); |
| 83 | u32 next_desc_phys = desc_phys + sizeof(struct dwmci_idmac32); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 84 | |
Sam Protsenko | 6384a1f | 2024-08-07 22:14:15 -0500 | [diff] [blame] | 85 | desc->des0 = control; |
| 86 | desc->des1 = buf_size; |
| 87 | desc->des2 = buf_addr; |
| 88 | desc->des3 = next_desc_phys; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 89 | } |
| 90 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 91 | static void dwmci_set_idma_desc64(struct dwmci_idmac64 *desc, u32 control, |
| 92 | u32 buf_size, u64 buf_addr) |
| 93 | { |
| 94 | phys_addr_t desc_phys = virt_to_phys(desc); |
| 95 | u64 next_desc_phys = desc_phys + sizeof(struct dwmci_idmac64); |
| 96 | |
| 97 | desc->des0 = control; |
| 98 | desc->des1 = 0; |
| 99 | desc->des2 = buf_size; |
| 100 | desc->des3 = 0; |
| 101 | desc->des4 = buf_addr & 0xffffffff; |
| 102 | desc->des5 = buf_addr >> 32; |
| 103 | desc->des6 = next_desc_phys & 0xffffffff; |
| 104 | desc->des7 = next_desc_phys >> 32; |
| 105 | } |
| 106 | |
| 107 | static void dwmci_prepare_desc(struct dwmci_host *host, struct mmc_data *data, |
| 108 | void *cur_idmac, void *bounce_buffer) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 109 | { |
Sam Protsenko | 6384a1f | 2024-08-07 22:14:15 -0500 | [diff] [blame] | 110 | struct dwmci_idmac32 *desc32 = cur_idmac; |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 111 | struct dwmci_idmac64 *desc64 = cur_idmac; |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 112 | ulong data_start, data_end; |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 113 | unsigned int blk_cnt, i; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 114 | |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 115 | data_start = (ulong)cur_idmac; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 116 | blk_cnt = data->blocks; |
| 117 | |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 118 | for (i = 0;; i++) { |
Sam Protsenko | 6384a1f | 2024-08-07 22:14:15 -0500 | [diff] [blame] | 119 | phys_addr_t buf_phys = virt_to_phys(bounce_buffer); |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 120 | unsigned int flags, cnt; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 121 | |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 122 | flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH; |
| 123 | if (i == 0) |
| 124 | flags |= DWMCI_IDMAC_FS; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 125 | if (blk_cnt <= 8) { |
| 126 | flags |= DWMCI_IDMAC_LD; |
| 127 | cnt = data->blocksize * blk_cnt; |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 128 | } else { |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 129 | cnt = data->blocksize * 8; |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 130 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 131 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 132 | if (host->dma_64bit_address) { |
| 133 | dwmci_set_idma_desc64(desc64, flags, cnt, |
| 134 | buf_phys + i * PAGE_SIZE); |
| 135 | desc64++; |
| 136 | } else { |
| 137 | dwmci_set_idma_desc32(desc32, flags, cnt, |
| 138 | buf_phys + i * PAGE_SIZE); |
| 139 | desc32++; |
| 140 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 141 | |
Mischa Jonker | a7a6091 | 2013-07-26 16:18:40 +0200 | [diff] [blame] | 142 | if (blk_cnt <= 8) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 143 | break; |
| 144 | blk_cnt -= 8; |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 145 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 146 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 147 | if (host->dma_64bit_address) |
| 148 | data_end = (ulong)desc64; |
| 149 | else |
| 150 | data_end = (ulong)desc32; |
Marek Vasut | b6da37b | 2019-02-13 20:16:20 +0100 | [diff] [blame] | 151 | flush_dcache_range(data_start, roundup(data_end, ARCH_DMA_MINALIGN)); |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 152 | } |
| 153 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 154 | static void dwmci_prepare_data(struct dwmci_host *host, struct mmc_data *data, |
| 155 | void *cur_idmac, void *bounce_buffer) |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 156 | { |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 157 | const u32 idmacl = virt_to_phys(cur_idmac) & 0xffffffff; |
| 158 | const u32 idmacu = (u64)virt_to_phys(cur_idmac) >> 32; |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 159 | unsigned long ctrl; |
| 160 | |
| 161 | dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); |
| 162 | |
| 163 | /* Clear IDMAC interrupt */ |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 164 | dwmci_writel(host, host->regs->idsts, 0xffffffff); |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 165 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 166 | dwmci_writel(host, host->regs->dbaddrl, idmacl); |
| 167 | if (host->dma_64bit_address) |
| 168 | dwmci_writel(host, host->regs->dbaddru, idmacu); |
Sam Protsenko | 2e7424b | 2024-08-07 22:14:14 -0500 | [diff] [blame] | 169 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 170 | dwmci_prepare_desc(host, data, cur_idmac, bounce_buffer); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 171 | |
| 172 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 173 | ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN; |
| 174 | dwmci_writel(host, DWMCI_CTRL, ctrl); |
| 175 | |
| 176 | ctrl = dwmci_readl(host, DWMCI_BMOD); |
| 177 | ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN; |
| 178 | dwmci_writel(host, DWMCI_BMOD, ctrl); |
| 179 | |
| 180 | dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); |
| 181 | dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks); |
| 182 | } |
| 183 | |
Heiko Stuebner | 46b7a4f | 2018-09-21 10:59:45 +0200 | [diff] [blame] | 184 | static int dwmci_fifo_ready(struct dwmci_host *host, u32 bit, u32 *len) |
| 185 | { |
| 186 | u32 timeout = 20000; |
| 187 | |
| 188 | *len = dwmci_readl(host, DWMCI_STATUS); |
| 189 | while (--timeout && (*len & bit)) { |
| 190 | udelay(200); |
| 191 | *len = dwmci_readl(host, DWMCI_STATUS); |
| 192 | } |
| 193 | |
| 194 | if (!timeout) { |
| 195 | debug("%s: FIFO underflow timeout\n", __func__); |
| 196 | return -ETIMEDOUT; |
| 197 | } |
| 198 | |
| 199 | return 0; |
| 200 | } |
| 201 | |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 202 | static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size) |
| 203 | { |
| 204 | unsigned int timeout; |
| 205 | |
Kever Yang | 4889d83 | 2019-08-29 15:42:41 +0800 | [diff] [blame] | 206 | timeout = size * 8; /* counting in bits */ |
| 207 | timeout *= 10; /* wait 10 times as long */ |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 208 | timeout /= mmc->clock; |
| 209 | timeout /= mmc->bus_width; |
| 210 | timeout /= mmc->ddr_mode ? 2 : 1; |
Kever Yang | 4889d83 | 2019-08-29 15:42:41 +0800 | [diff] [blame] | 211 | timeout *= 1000; /* counting in msec */ |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 212 | timeout = (timeout < 1000) ? 1000 : timeout; |
| 213 | |
| 214 | return timeout; |
| 215 | } |
| 216 | |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 217 | static int dwmci_data_transfer_fifo(struct dwmci_host *host, |
| 218 | struct mmc_data *data, u32 mask) |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 219 | { |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 220 | const u32 int_rx = mask & (DWMCI_INTMSK_RXDR | DWMCI_INTMSK_DTO); |
| 221 | const u32 int_tx = mask & DWMCI_INTMSK_TXDR; |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 222 | int ret = 0; |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 223 | u32 len = 0, size, i; |
| 224 | u32 *buf; |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 225 | |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 226 | size = (data->blocksize * data->blocks) / 4; |
| 227 | if (!host->fifo_mode || !size) |
| 228 | return 0; |
| 229 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 230 | if (data->flags == MMC_DATA_READ) |
| 231 | buf = (unsigned int *)data->dest; |
| 232 | else |
| 233 | buf = (unsigned int *)data->src; |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 234 | |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 235 | if (data->flags == MMC_DATA_READ && int_rx) { |
| 236 | dwmci_writel(host, DWMCI_RINTSTS, int_rx); |
| 237 | while (size) { |
| 238 | ret = dwmci_fifo_ready(host, DWMCI_FIFO_EMPTY, &len); |
| 239 | if (ret < 0) |
| 240 | break; |
| 241 | |
| 242 | len = (len >> DWMCI_FIFO_SHIFT) & DWMCI_FIFO_MASK; |
| 243 | len = min(size, len); |
| 244 | for (i = 0; i < len; i++) |
| 245 | *buf++ = dwmci_readl(host, DWMCI_DATA); |
| 246 | size = size > len ? (size - len) : 0; |
| 247 | } |
| 248 | } else if (data->flags == MMC_DATA_WRITE && int_tx) { |
| 249 | while (size) { |
| 250 | ret = dwmci_fifo_ready(host, DWMCI_FIFO_FULL, &len); |
| 251 | if (ret < 0) |
| 252 | break; |
| 253 | |
Sam Protsenko | 751fdf1 | 2024-08-07 22:14:17 -0500 | [diff] [blame] | 254 | len = host->fifo_depth - ((len >> DWMCI_FIFO_SHIFT) & |
| 255 | DWMCI_FIFO_MASK); |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 256 | len = min(size, len); |
| 257 | for (i = 0; i < len; i++) |
| 258 | dwmci_writel(host, DWMCI_DATA, *buf++); |
| 259 | size = size > len ? (size - len) : 0; |
| 260 | } |
| 261 | dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_TXDR); |
| 262 | } |
| 263 | |
| 264 | return ret; |
| 265 | } |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 266 | |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 267 | static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data) |
| 268 | { |
| 269 | struct mmc *mmc = host->mmc; |
| 270 | int ret = 0; |
| 271 | u32 timeout, mask, size; |
| 272 | ulong start = get_timer(0); |
| 273 | |
| 274 | size = data->blocksize * data->blocks; |
| 275 | timeout = dwmci_get_timeout(mmc, size); |
Marek Vasut | ffac512 | 2019-03-23 03:32:24 +0100 | [diff] [blame] | 276 | |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 277 | for (;;) { |
| 278 | mask = dwmci_readl(host, DWMCI_RINTSTS); |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 279 | /* Error during data transfer */ |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 280 | if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) { |
| 281 | debug("%s: DATA ERROR!\n", __func__); |
| 282 | ret = -EINVAL; |
| 283 | break; |
| 284 | } |
| 285 | |
Sam Protsenko | c4cffe8 | 2024-08-07 22:14:12 -0500 | [diff] [blame] | 286 | ret = dwmci_data_transfer_fifo(host, data, mask); |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 287 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 288 | /* Data arrived correctly */ |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 289 | if (mask & DWMCI_INTMSK_DTO) { |
| 290 | ret = 0; |
| 291 | break; |
| 292 | } |
| 293 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 294 | /* Check for timeout */ |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 295 | if (get_timer(start) > timeout) { |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 296 | debug("%s: Timeout waiting for data!\n", __func__); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 297 | ret = -ETIMEDOUT; |
huang lin | f983676 | 2015-11-17 14:20:21 +0800 | [diff] [blame] | 298 | break; |
| 299 | } |
| 300 | } |
| 301 | |
| 302 | dwmci_writel(host, DWMCI_RINTSTS, mask); |
| 303 | |
| 304 | return ret; |
| 305 | } |
| 306 | |
Sam Protsenko | 3350c20 | 2024-08-07 22:14:13 -0500 | [diff] [blame] | 307 | static int dwmci_dma_transfer(struct dwmci_host *host, uint flags, |
| 308 | struct bounce_buffer *bbstate) |
| 309 | { |
| 310 | int ret; |
| 311 | u32 mask, ctrl; |
| 312 | |
| 313 | if (flags == MMC_DATA_READ) |
| 314 | mask = DWMCI_IDINTEN_RI; |
| 315 | else |
| 316 | mask = DWMCI_IDINTEN_TI; |
| 317 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 318 | ret = wait_for_bit_le32(host->ioaddr + host->regs->idsts, mask, true, |
| 319 | 1000, false); |
Sam Protsenko | 3350c20 | 2024-08-07 22:14:13 -0500 | [diff] [blame] | 320 | if (ret) |
| 321 | debug("%s: DWMCI_IDINTEN mask 0x%x timeout\n", __func__, mask); |
| 322 | |
| 323 | /* Clear interrupts */ |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 324 | dwmci_writel(host, host->regs->idsts, DWMCI_IDINTEN_MASK); |
Sam Protsenko | 3350c20 | 2024-08-07 22:14:13 -0500 | [diff] [blame] | 325 | |
| 326 | ctrl = dwmci_readl(host, DWMCI_CTRL); |
| 327 | ctrl &= ~DWMCI_DMA_EN; |
| 328 | dwmci_writel(host, DWMCI_CTRL, ctrl); |
| 329 | |
| 330 | bounce_buffer_stop(bbstate); |
| 331 | return ret; |
| 332 | } |
| 333 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 334 | static int dwmci_set_transfer_mode(struct dwmci_host *host, |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 335 | struct mmc_data *data) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 336 | { |
| 337 | unsigned long mode; |
| 338 | |
| 339 | mode = DWMCI_CMD_DATA_EXP; |
| 340 | if (data->flags & MMC_DATA_WRITE) |
| 341 | mode |= DWMCI_CMD_RW; |
| 342 | |
| 343 | return mode; |
| 344 | } |
| 345 | |
Sam Protsenko | cf81204 | 2024-08-07 22:14:09 -0500 | [diff] [blame] | 346 | static void dwmci_wait_while_busy(struct dwmci_host *host, struct mmc_cmd *cmd) |
| 347 | { |
| 348 | unsigned int timeout = 500; /* msec */ |
| 349 | ulong start; |
| 350 | |
| 351 | start = get_timer(0); |
| 352 | while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) { |
| 353 | if (get_timer(start) > timeout) { |
| 354 | debug("%s: Timeout on data busy, continue anyway\n", |
| 355 | __func__); |
| 356 | break; |
| 357 | } |
| 358 | } |
| 359 | } |
| 360 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 361 | static int dwmci_send_cmd_common(struct dwmci_host *host, struct mmc_cmd *cmd, |
| 362 | struct mmc_data *data, void *cur_idmac) |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 363 | { |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 364 | int ret, flags = 0, i; |
Alexander Graf | 61c2a66 | 2016-03-04 01:09:52 +0100 | [diff] [blame] | 365 | u32 retry = 100000; |
Sam Protsenko | 3350c20 | 2024-08-07 22:14:13 -0500 | [diff] [blame] | 366 | u32 mask; |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 367 | struct bounce_buffer bbstate; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 368 | |
Sam Protsenko | cf81204 | 2024-08-07 22:14:09 -0500 | [diff] [blame] | 369 | dwmci_wait_while_busy(host, cmd); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 370 | dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL); |
| 371 | |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 372 | if (data) { |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 373 | if (host->fifo_mode) { |
| 374 | dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); |
| 375 | dwmci_writel(host, DWMCI_BYTCNT, |
| 376 | data->blocksize * data->blocks); |
| 377 | dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 378 | } else { |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 379 | if (data->flags == MMC_DATA_READ) { |
Marek Vasut | 72d37b6 | 2019-03-23 18:45:27 +0100 | [diff] [blame] | 380 | ret = bounce_buffer_start(&bbstate, |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 381 | (void *)data->dest, |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 382 | data->blocksize * |
| 383 | data->blocks, GEN_BB_WRITE); |
| 384 | } else { |
Marek Vasut | 72d37b6 | 2019-03-23 18:45:27 +0100 | [diff] [blame] | 385 | ret = bounce_buffer_start(&bbstate, |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 386 | (void *)data->src, |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 387 | data->blocksize * |
| 388 | data->blocks, GEN_BB_READ); |
| 389 | } |
Marek Vasut | 72d37b6 | 2019-03-23 18:45:27 +0100 | [diff] [blame] | 390 | |
| 391 | if (ret) |
| 392 | return ret; |
| 393 | |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 394 | dwmci_prepare_data(host, data, cur_idmac, |
| 395 | bbstate.bounce_buffer); |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 396 | } |
Alexey Brodkin | 55bab5e | 2013-12-26 15:29:07 +0400 | [diff] [blame] | 397 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 398 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 399 | dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg); |
| 400 | |
| 401 | if (data) |
| 402 | flags = dwmci_set_transfer_mode(host, data); |
| 403 | |
| 404 | if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) |
John Keeping | feb7fa3 | 2021-12-07 16:09:35 +0000 | [diff] [blame] | 405 | return -EBUSY; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 406 | |
| 407 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 408 | flags |= DWMCI_CMD_ABORT_STOP; |
| 409 | else |
| 410 | flags |= DWMCI_CMD_PRV_DAT_WAIT; |
| 411 | |
| 412 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 413 | flags |= DWMCI_CMD_RESP_EXP; |
| 414 | if (cmd->resp_type & MMC_RSP_136) |
| 415 | flags |= DWMCI_CMD_RESP_LENGTH; |
| 416 | } |
| 417 | |
| 418 | if (cmd->resp_type & MMC_RSP_CRC) |
| 419 | flags |= DWMCI_CMD_CHECK_CRC; |
| 420 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 421 | flags |= cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 422 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 423 | debug("Sending CMD%d\n", cmd->cmdidx); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 424 | |
| 425 | dwmci_writel(host, DWMCI_CMD, flags); |
| 426 | |
| 427 | for (i = 0; i < retry; i++) { |
| 428 | mask = dwmci_readl(host, DWMCI_RINTSTS); |
| 429 | if (mask & DWMCI_INTMSK_CDONE) { |
| 430 | if (!data) |
| 431 | dwmci_writel(host, DWMCI_RINTSTS, mask); |
| 432 | break; |
| 433 | } |
| 434 | } |
| 435 | |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 436 | if (i == retry) { |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 437 | debug("%s: Timeout\n", __func__); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 438 | return -ETIMEDOUT; |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 439 | } |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 440 | |
| 441 | if (mask & DWMCI_INTMSK_RTO) { |
Pavel Machek | a425f5d | 2014-09-05 12:49:48 +0200 | [diff] [blame] | 442 | /* |
| 443 | * Timeout here is not necessarily fatal. (e)MMC cards |
| 444 | * will splat here when they receive CMD55 as they do |
| 445 | * not support this command and that is exactly the way |
| 446 | * to tell them apart from SD cards. Thus, this output |
| 447 | * below shall be debug(). eMMC cards also do not favor |
| 448 | * CMD8, please keep that in mind. |
| 449 | */ |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 450 | debug("%s: Response Timeout\n", __func__); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 451 | return -ETIMEDOUT; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 452 | } else if (mask & DWMCI_INTMSK_RE) { |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 453 | debug("%s: Response Error\n", __func__); |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 454 | return -EIO; |
Marek Vasut | a6d9199 | 2018-11-06 23:42:11 +0100 | [diff] [blame] | 455 | } else if ((cmd->resp_type & MMC_RSP_CRC) && |
| 456 | (mask & DWMCI_INTMSK_RCRC)) { |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 457 | debug("%s: Response CRC Error\n", __func__); |
Marek Vasut | a6d9199 | 2018-11-06 23:42:11 +0100 | [diff] [blame] | 458 | return -EIO; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 459 | } |
| 460 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 461 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 462 | if (cmd->resp_type & MMC_RSP_136) { |
| 463 | cmd->response[0] = dwmci_readl(host, DWMCI_RESP3); |
| 464 | cmd->response[1] = dwmci_readl(host, DWMCI_RESP2); |
| 465 | cmd->response[2] = dwmci_readl(host, DWMCI_RESP1); |
| 466 | cmd->response[3] = dwmci_readl(host, DWMCI_RESP0); |
| 467 | } else { |
| 468 | cmd->response[0] = dwmci_readl(host, DWMCI_RESP0); |
| 469 | } |
| 470 | } |
| 471 | |
| 472 | if (data) { |
huang lin | 50b7375 | 2015-11-17 14:20:22 +0800 | [diff] [blame] | 473 | ret = dwmci_data_transfer(host, data); |
Sam Protsenko | 3350c20 | 2024-08-07 22:14:13 -0500 | [diff] [blame] | 474 | if (!host->fifo_mode) |
| 475 | ret = dwmci_dma_transfer(host, data->flags, &bbstate); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | udelay(100); |
| 479 | |
Marek Vasut | 81e093f | 2015-07-27 22:39:38 +0200 | [diff] [blame] | 480 | return ret; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 481 | } |
| 482 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 483 | #ifdef CONFIG_DM_MMC |
| 484 | static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 485 | struct mmc_data *data) |
| 486 | { |
| 487 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 488 | #else |
| 489 | static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 490 | struct mmc_data *data) |
| 491 | { |
| 492 | #endif |
| 493 | struct dwmci_host *host = mmc->priv; |
| 494 | const size_t buf_size = data ? DIV_ROUND_UP(data->blocks, 8) : 0; |
| 495 | |
| 496 | if (host->dma_64bit_address) { |
| 497 | ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac64, idmac, buf_size); |
| 498 | return dwmci_send_cmd_common(host, cmd, data, idmac); |
| 499 | } else { |
| 500 | ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac32, idmac, buf_size); |
| 501 | return dwmci_send_cmd_common(host, cmd, data, idmac); |
| 502 | } |
| 503 | } |
| 504 | |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame] | 505 | static int dwmci_control_clken(struct dwmci_host *host, bool on) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 506 | { |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame] | 507 | const u32 val = on ? DWMCI_CLKEN_ENABLE | DWMCI_CLKEN_LOW_PWR : 0; |
| 508 | const u32 cmd_only_clk = DWMCI_CMD_PRV_DAT_WAIT | DWMCI_CMD_UPD_CLK; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 509 | int timeout = 10000; |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame] | 510 | u32 status; |
| 511 | |
| 512 | dwmci_writel(host, DWMCI_CLKENA, val); |
| 513 | |
| 514 | /* Inform CIU */ |
| 515 | dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_START | cmd_only_clk); |
| 516 | do { |
| 517 | status = dwmci_readl(host, DWMCI_CMD); |
| 518 | if (timeout-- < 0) { |
| 519 | debug("%s: Timeout!\n", __func__); |
| 520 | return -ETIMEDOUT; |
| 521 | } |
| 522 | } while (status & DWMCI_CMD_START); |
| 523 | |
| 524 | return 0; |
| 525 | } |
| 526 | |
| 527 | /* |
| 528 | * Update the clock divider. |
| 529 | * |
| 530 | * To prevent a clock glitch keep the clock stopped during the update of |
| 531 | * clock divider and clock source. |
| 532 | */ |
| 533 | static int dwmci_update_div(struct dwmci_host *host, u32 div) |
| 534 | { |
| 535 | int ret; |
| 536 | |
| 537 | /* Disable clock */ |
| 538 | ret = dwmci_control_clken(host, false); |
| 539 | if (ret) |
| 540 | return ret; |
| 541 | |
| 542 | /* Set clock to desired speed */ |
| 543 | dwmci_writel(host, DWMCI_CLKDIV, div); |
| 544 | dwmci_writel(host, DWMCI_CLKSRC, 0); |
| 545 | |
| 546 | /* Enable clock */ |
| 547 | return dwmci_control_clken(host, true); |
| 548 | } |
| 549 | |
| 550 | static int dwmci_setup_bus(struct dwmci_host *host, u32 freq) |
| 551 | { |
| 552 | u32 div; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 553 | unsigned long sclk; |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame] | 554 | int ret; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 555 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 556 | if (freq == host->clock || freq == 0) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 557 | return 0; |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 558 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 559 | /* |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 560 | * If host->get_mmc_clk isn't defined, then assume that host->bus_hz is |
| 561 | * source clock value. host->bus_hz should be set by user. |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 562 | */ |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 563 | if (host->get_mmc_clk) { |
Simon Glass | eff7668 | 2015-08-30 16:55:15 -0600 | [diff] [blame] | 564 | sclk = host->get_mmc_clk(host, freq); |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 565 | } else if (host->bus_hz) { |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 566 | sclk = host->bus_hz; |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 567 | } else { |
| 568 | debug("%s: Didn't get source clock value\n", __func__); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 569 | return -EINVAL; |
| 570 | } |
| 571 | |
Chin Liang See | 4cfff95 | 2014-06-10 01:26:52 -0500 | [diff] [blame] | 572 | if (sclk == freq) |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 573 | div = 0; /* bypass mode */ |
Chin Liang See | 4cfff95 | 2014-06-10 01:26:52 -0500 | [diff] [blame] | 574 | else |
| 575 | div = DIV_ROUND_UP(sclk, 2 * freq); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 576 | |
Sam Protsenko | fd5387f | 2024-08-07 22:14:11 -0500 | [diff] [blame] | 577 | ret = dwmci_update_div(host, div); |
| 578 | if (ret) |
| 579 | return ret; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 580 | |
| 581 | host->clock = freq; |
| 582 | |
| 583 | return 0; |
| 584 | } |
| 585 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 586 | #ifdef CONFIG_DM_MMC |
Jaehoon Chung | ad220ac | 2016-06-28 15:52:21 +0900 | [diff] [blame] | 587 | static int dwmci_set_ios(struct udevice *dev) |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 588 | { |
| 589 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 590 | #else |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 591 | static int dwmci_set_ios(struct mmc *mmc) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 592 | { |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 593 | #endif |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 594 | struct dwmci_host *host = (struct dwmci_host *)mmc->priv; |
| 595 | u32 ctype, regs; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 596 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 597 | debug("Bus width = %d, clock: %d\n", mmc->bus_width, mmc->clock); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 598 | |
| 599 | dwmci_setup_bus(host, mmc->clock); |
| 600 | switch (mmc->bus_width) { |
| 601 | case 8: |
| 602 | ctype = DWMCI_CTYPE_8BIT; |
| 603 | break; |
| 604 | case 4: |
| 605 | ctype = DWMCI_CTYPE_4BIT; |
| 606 | break; |
| 607 | default: |
| 608 | ctype = DWMCI_CTYPE_1BIT; |
| 609 | break; |
| 610 | } |
| 611 | |
| 612 | dwmci_writel(host, DWMCI_CTYPE, ctype); |
| 613 | |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 614 | regs = dwmci_readl(host, DWMCI_UHS_REG); |
Andrew Gabbasov | 54c0e22 | 2014-12-01 06:59:12 -0600 | [diff] [blame] | 615 | if (mmc->ddr_mode) |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 616 | regs |= DWMCI_DDR_MODE; |
| 617 | else |
Jaehoon Chung | 401fc50 | 2015-01-14 17:37:53 +0900 | [diff] [blame] | 618 | regs &= ~DWMCI_DDR_MODE; |
Jaehoon Chung | e867294 | 2014-05-16 13:59:55 +0900 | [diff] [blame] | 619 | |
| 620 | dwmci_writel(host, DWMCI_UHS_REG, regs); |
| 621 | |
Siew Chin Lim | c51e7e1 | 2020-12-24 18:21:03 +0800 | [diff] [blame] | 622 | if (host->clksel) { |
| 623 | int ret; |
| 624 | |
| 625 | ret = host->clksel(host); |
| 626 | if (ret) |
| 627 | return ret; |
| 628 | } |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 629 | |
Urja Rannikko | 9932a01 | 2019-05-13 13:25:27 +0000 | [diff] [blame] | 630 | #if CONFIG_IS_ENABLED(DM_REGULATOR) |
| 631 | if (mmc->vqmmc_supply) { |
| 632 | int ret; |
| 633 | |
Jonas Karlman | a117d61 | 2023-07-19 21:21:00 +0000 | [diff] [blame] | 634 | ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, false); |
| 635 | if (ret) |
| 636 | return ret; |
| 637 | |
Urja Rannikko | 9932a01 | 2019-05-13 13:25:27 +0000 | [diff] [blame] | 638 | if (mmc->signal_voltage == MMC_SIGNAL_VOLTAGE_180) |
| 639 | regulator_set_value(mmc->vqmmc_supply, 1800000); |
| 640 | else |
| 641 | regulator_set_value(mmc->vqmmc_supply, 3300000); |
| 642 | |
| 643 | ret = regulator_set_enable_if_allowed(mmc->vqmmc_supply, true); |
| 644 | if (ret) |
| 645 | return ret; |
| 646 | } |
| 647 | #endif |
| 648 | |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 649 | return 0; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 650 | } |
| 651 | |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 652 | static void dwmci_init_fifo(struct dwmci_host *host) |
| 653 | { |
Sam Protsenko | 751fdf1 | 2024-08-07 22:14:17 -0500 | [diff] [blame] | 654 | u32 fifo_thr, fifoth_val; |
| 655 | |
| 656 | if (!host->fifo_depth) { |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 657 | u32 fifo_size; |
| 658 | |
Sam Protsenko | 751fdf1 | 2024-08-07 22:14:17 -0500 | [diff] [blame] | 659 | /* |
| 660 | * Automatically detect FIFO depth from FIFOTH register. |
| 661 | * Power-on value of RX_WMark is FIFO_DEPTH-1. |
| 662 | */ |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 663 | fifo_size = dwmci_readl(host, DWMCI_FIFOTH); |
| 664 | fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1; |
Sam Protsenko | 751fdf1 | 2024-08-07 22:14:17 -0500 | [diff] [blame] | 665 | host->fifo_depth = fifo_size; |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 666 | } |
| 667 | |
Sam Protsenko | 751fdf1 | 2024-08-07 22:14:17 -0500 | [diff] [blame] | 668 | fifo_thr = host->fifo_depth / 2; |
| 669 | fifoth_val = MSIZE(0x2) | RX_WMARK(fifo_thr - 1) | TX_WMARK(fifo_thr); |
| 670 | dwmci_writel(host, DWMCI_FIFOTH, fifoth_val); |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 671 | } |
| 672 | |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 673 | static void dwmci_init_dma(struct dwmci_host *host) |
| 674 | { |
| 675 | int addr_config; |
| 676 | |
| 677 | if (host->fifo_mode) |
| 678 | return; |
| 679 | |
| 680 | addr_config = (dwmci_readl(host, DWMCI_HCON) >> 27) & 0x1; |
| 681 | if (addr_config == 1) { |
| 682 | host->dma_64bit_address = true; |
| 683 | host->regs = &dwmci_idmac_regs64; |
| 684 | debug("%s: IDMAC supports 64-bit address mode\n", __func__); |
| 685 | } else { |
| 686 | host->dma_64bit_address = false; |
| 687 | host->regs = &dwmci_idmac_regs32; |
| 688 | debug("%s: IDMAC supports 32-bit address mode\n", __func__); |
| 689 | } |
| 690 | |
| 691 | dwmci_writel(host, host->regs->idinten, DWMCI_IDINTEN_MASK); |
| 692 | } |
| 693 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 694 | static int dwmci_init(struct mmc *mmc) |
| 695 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 696 | struct dwmci_host *host = mmc->priv; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 697 | |
Jaehoon Chung | 42f81a8 | 2013-11-29 20:08:57 +0900 | [diff] [blame] | 698 | if (host->board_init) |
| 699 | host->board_init(host); |
Rajeshwari Shinde | 7016309 | 2013-10-29 12:53:13 +0530 | [diff] [blame] | 700 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 701 | dwmci_writel(host, DWMCI_PWREN, 1); |
| 702 | |
| 703 | if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) { |
Simon Glass | 4c9b948 | 2015-08-06 20:16:27 -0600 | [diff] [blame] | 704 | debug("%s[%d] Fail-reset!!\n", __func__, __LINE__); |
| 705 | return -EIO; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Amar | 902664c | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 708 | /* Enumerate at 400KHz */ |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 709 | dwmci_setup_bus(host, mmc->cfg->f_min); |
Amar | 902664c | 2013-04-27 11:42:54 +0530 | [diff] [blame] | 710 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 711 | dwmci_writel(host, DWMCI_RINTSTS, 0xffffffff); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 712 | dwmci_writel(host, DWMCI_INTMASK, 0); |
| 713 | |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 714 | dwmci_writel(host, DWMCI_TMOUT, 0xffffffff); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 715 | |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 716 | dwmci_writel(host, DWMCI_BMOD, 1); |
Sam Protsenko | 0845f4f | 2024-08-07 22:14:10 -0500 | [diff] [blame] | 717 | dwmci_init_fifo(host); |
Sam Protsenko | 7c99161 | 2024-08-07 22:14:16 -0500 | [diff] [blame] | 718 | dwmci_init_dma(host); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 719 | |
| 720 | dwmci_writel(host, DWMCI_CLKENA, 0); |
| 721 | dwmci_writel(host, DWMCI_CLKSRC, 0); |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 726 | #ifdef CONFIG_DM_MMC |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 727 | int dwmci_probe(struct udevice *dev) |
| 728 | { |
| 729 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 730 | |
| 731 | return dwmci_init(mmc); |
| 732 | } |
| 733 | |
| 734 | const struct dm_mmc_ops dm_dwmci_ops = { |
| 735 | .send_cmd = dwmci_send_cmd, |
| 736 | .set_ios = dwmci_set_ios, |
| 737 | }; |
| 738 | |
| 739 | #else |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 740 | static const struct mmc_ops dwmci_ops = { |
| 741 | .send_cmd = dwmci_send_cmd, |
| 742 | .set_ios = dwmci_set_ios, |
| 743 | .init = dwmci_init, |
| 744 | }; |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 745 | #endif |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 746 | |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 747 | void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host, |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 748 | u32 max_clk, u32 min_clk) |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 749 | { |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 750 | cfg->name = host->name; |
Simon Glass | eba48f9 | 2017-07-29 11:35:31 -0600 | [diff] [blame] | 751 | #ifndef CONFIG_DM_MMC |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 752 | cfg->ops = &dwmci_ops; |
Simon Glass | ff5c1b7 | 2016-06-12 23:30:23 -0600 | [diff] [blame] | 753 | #endif |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 754 | cfg->f_min = min_clk; |
| 755 | cfg->f_max = max_clk; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 756 | |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 757 | 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] | 758 | |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 759 | cfg->host_caps = host->caps; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 760 | |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 761 | if (host->buswidth == 8) { |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 762 | cfg->host_caps |= MMC_MODE_8BIT; |
| 763 | cfg->host_caps &= ~MMC_MODE_4BIT; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 764 | } else { |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 765 | cfg->host_caps |= MMC_MODE_4BIT; |
| 766 | cfg->host_caps &= ~MMC_MODE_8BIT; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 767 | } |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 768 | cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz; |
| 769 | |
| 770 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 771 | } |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 772 | |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 773 | #ifdef CONFIG_BLK |
| 774 | int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg) |
| 775 | { |
| 776 | return mmc_bind(dev, mmc, cfg); |
| 777 | } |
| 778 | #else |
| 779 | int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk) |
| 780 | { |
Jaehoon Chung | bf819d0 | 2016-09-23 19:13:16 +0900 | [diff] [blame] | 781 | dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk); |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 782 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 783 | host->mmc = mmc_create(&host->cfg, host); |
Sam Protsenko | 286892e | 2024-08-07 22:14:19 -0500 | [diff] [blame] | 784 | if (!host->mmc) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 785 | return -1; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 786 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 787 | return 0; |
Jaehoon Chung | 7cf7307 | 2012-10-15 19:10:29 +0000 | [diff] [blame] | 788 | } |
Simon Glass | 8268254 | 2016-05-14 14:03:07 -0600 | [diff] [blame] | 789 | #endif |