1 /* 2 * (C) Copyright 2012 SAMSUNG Electronics 3 * Jaehoon Chung <jh80.chung@samsung.com> 4 * Rajeshawari Shinde <rajeshwari.s@samsung.com> 5 * 6 * SPDX-License-Identifier: GPL-2.0+ 7 */ 8 9 #include <common.h> 10 #include <bouncebuf.h> 11 #include <errno.h> 12 #include <malloc.h> 13 #include <memalign.h> 14 #include <mmc.h> 15 #include <dwmmc.h> 16 #ifdef CONFIG_DM_GPIO 17 #include <asm/gpio.h> 18 #include <asm-generic/gpio.h> 19 #endif 20 21 #define PAGE_SIZE 4096 22 23 /* 24 * Currently it supports read/write up to 8*8*4 Bytes per 25 * stride as a burst mode. Please note that if you change 26 * MAX_STRIDE, you should also update dwmci_memcpy_fromio 27 * to augment the groups of {ldm, stm}. 28 */ 29 #define MAX_STRIDE 64 30 #if CONFIG_ARM && CONFIG_CPU_V7 31 void noinline dwmci_memcpy_fromio(void *buffer, void *fifo_addr) 32 { 33 __asm__ __volatile__ ( 34 "push {r2, r3, r4, r5, r6, r7, r8, r9}\n" 35 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 36 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 37 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 38 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 39 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 40 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 41 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 42 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 43 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 44 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 45 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 46 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 47 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 48 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 49 "ldm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 50 "stm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 51 "pop {r2, r3, r4, r5, r6,r7,r8,r9}\n" 52 :::"memory" 53 ); 54 } 55 56 void noinline dwmci_memcpy_toio(void *buffer, void *fifo_addr) 57 { 58 __asm__ __volatile__ ( 59 "push {r2, r3, r4, r5, r6, r7, r8, r9}\n" 60 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 61 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 62 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 63 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 64 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 65 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 66 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 67 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 68 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 69 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 70 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 71 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 72 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 73 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 74 "ldm r0!, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 75 "stm r1, {r2,r3,r4,r5,r6,r7,r8,r9}\n" 76 "pop {r2, r3, r4, r5, r6,r7,r8,r9}\n" 77 :::"memory" 78 ); 79 } 80 #else 81 void dwmci_memcpy_fromio(void *buffer, void *fifo_addr) {}; 82 void dwmci_memcpy_toio(void *buffer, void *fifo_addr) {}; 83 #endif 84 static int dwmci_wait_reset(struct dwmci_host *host, u32 value) 85 { 86 unsigned long timeout = 1000; 87 u32 ctrl; 88 89 dwmci_writel(host, DWMCI_CTRL, value); 90 91 while (timeout--) { 92 ctrl = dwmci_readl(host, DWMCI_CTRL); 93 if (!(ctrl & DWMCI_RESET_ALL)) 94 return 1; 95 } 96 return 0; 97 } 98 99 static void dwmci_set_idma_desc(struct dwmci_idmac *idmac, 100 u32 desc0, u32 desc1, u32 desc2) 101 { 102 struct dwmci_idmac *desc = idmac; 103 104 desc->flags = desc0; 105 desc->cnt = desc1; 106 desc->addr = desc2; 107 desc->next_addr = (ulong)desc + sizeof(struct dwmci_idmac); 108 } 109 110 static void dwmci_prepare_data(struct dwmci_host *host, 111 struct mmc_data *data, 112 struct dwmci_idmac *cur_idmac, 113 void *bounce_buffer) 114 { 115 unsigned long ctrl; 116 unsigned int i = 0, flags, cnt, blk_cnt; 117 ulong data_start, data_end; 118 119 120 blk_cnt = data->blocks; 121 122 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); 123 124 data_start = (ulong)cur_idmac; 125 dwmci_writel(host, DWMCI_DBADDR, (ulong)cur_idmac); 126 127 do { 128 flags = DWMCI_IDMAC_OWN | DWMCI_IDMAC_CH ; 129 flags |= (i == 0) ? DWMCI_IDMAC_FS : 0; 130 if (blk_cnt <= 8) { 131 flags |= DWMCI_IDMAC_LD; 132 cnt = data->blocksize * blk_cnt; 133 } else 134 cnt = data->blocksize * 8; 135 136 dwmci_set_idma_desc(cur_idmac, flags, cnt, 137 (ulong)bounce_buffer + (i * PAGE_SIZE)); 138 139 if (blk_cnt <= 8) 140 break; 141 blk_cnt -= 8; 142 cur_idmac++; 143 i++; 144 } while(1); 145 146 data_end = (ulong)cur_idmac; 147 flush_dcache_range(data_start, data_end + ARCH_DMA_MINALIGN); 148 149 ctrl = dwmci_readl(host, DWMCI_CTRL); 150 ctrl |= DWMCI_IDMAC_EN | DWMCI_DMA_EN; 151 dwmci_writel(host, DWMCI_CTRL, ctrl); 152 153 ctrl = dwmci_readl(host, DWMCI_BMOD); 154 ctrl |= DWMCI_BMOD_IDMAC_FB | DWMCI_BMOD_IDMAC_EN; 155 dwmci_writel(host, DWMCI_BMOD, ctrl); 156 157 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); 158 dwmci_writel(host, DWMCI_BYTCNT, data->blocksize * data->blocks); 159 } 160 161 static unsigned int dwmci_get_timeout(struct mmc *mmc, const unsigned int size) 162 { 163 unsigned int timeout; 164 165 timeout = size * 8; /* counting in bits */ 166 timeout *= 10; /* wait 10 times as long */ 167 timeout /= mmc->clock; 168 timeout /= mmc->bus_width; 169 timeout *= 1000; /* counting in msec */ 170 timeout = (timeout < 10000) ? 10000 : timeout; 171 172 return timeout; 173 } 174 175 static int dwmci_data_transfer(struct dwmci_host *host, struct mmc_data *data) 176 { 177 int ret = 0; 178 int reset_timeout = 100; 179 u32 timeout, status, ctrl, mask, size, i, len = 0; 180 u32 *buf = NULL; 181 ulong start = get_timer(0); 182 u32 fifo_depth = (((host->fifoth_val & RX_WMARK_MASK) >> 183 RX_WMARK_SHIFT) + 1) * 2; 184 bool stride; 185 186 size = data->blocksize * data->blocks; 187 /* Still use legacy PIO mode if size < 512(128 * 4) Bytes */ 188 stride = host->stride_pio && size > 128; 189 if (data->flags == MMC_DATA_READ) 190 buf = (unsigned int *)data->dest; 191 else 192 buf = (unsigned int *)data->src; 193 194 timeout = dwmci_get_timeout(host->mmc, size); 195 size /= 4; 196 197 for (;;) { 198 mask = dwmci_readl(host, DWMCI_RINTSTS); 199 /* Error during data transfer. */ 200 if (mask & (DWMCI_DATA_ERR | DWMCI_DATA_TOUT)) { 201 debug("%s: DATA ERROR!\n", __func__); 202 dwmci_wait_reset(host, DWMCI_RESET_ALL); 203 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT | 204 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START); 205 206 do { 207 status = dwmci_readl(host, DWMCI_CMD); 208 if (reset_timeout-- < 0) 209 break; 210 udelay(100); 211 } while (status & DWMCI_CMD_START); 212 213 if (!host->fifo_mode) { 214 ctrl = dwmci_readl(host, DWMCI_BMOD); 215 ctrl |= DWMCI_BMOD_IDMAC_RESET; 216 dwmci_writel(host, DWMCI_BMOD, ctrl); 217 } 218 219 ret = -EINVAL; 220 break; 221 } 222 223 if (host->fifo_mode && size) { 224 len = 0; 225 if (data->flags == MMC_DATA_READ && 226 (mask & DWMCI_INTMSK_RXDR)) { 227 while (size) { 228 len = dwmci_readl(host, DWMCI_STATUS); 229 len = (len >> DWMCI_FIFO_SHIFT) & 230 DWMCI_FIFO_MASK; 231 len = min(size, len); 232 if (!stride) { 233 /* Legacy pio mode */ 234 for (i = 0; i < len; i++) 235 *buf++ = dwmci_readl(host, DWMCI_DATA); 236 goto read_again; 237 } 238 239 /* dwmci_memcpy_fromio now bursts 256 Bytes once */ 240 if (len < MAX_STRIDE) 241 continue; 242 243 for (i = 0; i < len / MAX_STRIDE; i++) { 244 dwmci_memcpy_fromio(buf, host->ioaddr + DWMCI_DATA); 245 buf += MAX_STRIDE; 246 } 247 248 len = i * MAX_STRIDE; 249 read_again: 250 size = size > len ? (size - len) : 0; 251 } 252 dwmci_writel(host, DWMCI_RINTSTS, 253 DWMCI_INTMSK_RXDR); 254 } else if (data->flags == MMC_DATA_WRITE && 255 (mask & DWMCI_INTMSK_TXDR)) { 256 while (size) { 257 len = dwmci_readl(host, DWMCI_STATUS); 258 len = fifo_depth - ((len >> 259 DWMCI_FIFO_SHIFT) & 260 DWMCI_FIFO_MASK); 261 len = min(size, len); 262 if (!stride) { 263 for (i = 0; i < len; i++) 264 dwmci_writel(host, DWMCI_DATA, 265 *buf++); 266 goto write_again; 267 } 268 /* dwmci_memcpy_toio now bursts 256 Bytes once */ 269 if (len < MAX_STRIDE) 270 continue; 271 272 for (i = 0; i < len / MAX_STRIDE; i++) { 273 dwmci_memcpy_toio(buf, host->ioaddr + DWMCI_DATA); 274 buf += MAX_STRIDE; 275 } 276 277 len = i * MAX_STRIDE; 278 write_again: 279 size = size > len ? (size - len) : 0; 280 } 281 dwmci_writel(host, DWMCI_RINTSTS, 282 DWMCI_INTMSK_TXDR); 283 } 284 } 285 286 /* Data arrived correctly. */ 287 if (mask & DWMCI_INTMSK_DTO) { 288 ret = 0; 289 break; 290 } 291 292 /* Check for timeout. */ 293 if (get_timer(start) > timeout) { 294 debug("%s: Timeout waiting for data!\n", 295 __func__); 296 ret = -ETIMEDOUT; 297 break; 298 } 299 } 300 301 dwmci_writel(host, DWMCI_RINTSTS, mask); 302 303 return ret; 304 } 305 306 static int dwmci_set_transfer_mode(struct dwmci_host *host, 307 struct mmc_data *data) 308 { 309 unsigned long mode; 310 311 mode = DWMCI_CMD_DATA_EXP; 312 if (data->flags & MMC_DATA_WRITE) 313 mode |= DWMCI_CMD_RW; 314 315 return mode; 316 } 317 318 #ifdef CONFIG_DM_MMC 319 static int dwmci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, 320 struct mmc_data *data) 321 { 322 struct mmc *mmc = mmc_get_mmc_dev(dev); 323 #else 324 static int dwmci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, 325 struct mmc_data *data) 326 { 327 #endif 328 struct dwmci_host *host = mmc->priv; 329 ALLOC_CACHE_ALIGN_BUFFER(struct dwmci_idmac, cur_idmac, 330 data ? DIV_ROUND_UP(data->blocks, 8) : 0); 331 int ret = 0, flags = 0, i; 332 unsigned int timeout = 500; 333 u32 retry = 100000; 334 u32 mask, ctrl; 335 ulong start = get_timer(0); 336 struct bounce_buffer bbstate; 337 338 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) { 339 if (get_timer(start) > timeout) { 340 debug("%s: Timeout on data busy\n", __func__); 341 return -ETIMEDOUT; 342 } 343 } 344 345 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL); 346 347 if (data) { 348 if (host->fifo_mode) { 349 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); 350 dwmci_writel(host, DWMCI_BYTCNT, 351 data->blocksize * data->blocks); 352 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); 353 } else { 354 if (data->flags == MMC_DATA_READ) { 355 bounce_buffer_start(&bbstate, (void*)data->dest, 356 data->blocksize * 357 data->blocks, GEN_BB_WRITE); 358 } else { 359 bounce_buffer_start(&bbstate, (void*)data->src, 360 data->blocksize * 361 data->blocks, GEN_BB_READ); 362 } 363 dwmci_prepare_data(host, data, cur_idmac, 364 bbstate.bounce_buffer); 365 } 366 } 367 368 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg); 369 370 if (data) 371 flags = dwmci_set_transfer_mode(host, data); 372 373 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) 374 return -1; 375 376 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) 377 flags |= DWMCI_CMD_ABORT_STOP; 378 else 379 flags |= DWMCI_CMD_PRV_DAT_WAIT; 380 381 if (cmd->resp_type & MMC_RSP_PRESENT) { 382 flags |= DWMCI_CMD_RESP_EXP; 383 if (cmd->resp_type & MMC_RSP_136) 384 flags |= DWMCI_CMD_RESP_LENGTH; 385 } 386 387 if (cmd->resp_type & MMC_RSP_CRC) 388 flags |= DWMCI_CMD_CHECK_CRC; 389 390 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG); 391 392 debug("Sending CMD%d\n",cmd->cmdidx); 393 394 dwmci_writel(host, DWMCI_CMD, flags); 395 396 for (i = 0; i < retry; i++) { 397 mask = dwmci_readl(host, DWMCI_RINTSTS); 398 if (mask & DWMCI_INTMSK_CDONE) { 399 if (!data) 400 dwmci_writel(host, DWMCI_RINTSTS, mask); 401 break; 402 } 403 } 404 405 if (i == retry) { 406 debug("%s: Timeout.\n", __func__); 407 return -ETIMEDOUT; 408 } 409 410 if (mask & DWMCI_INTMSK_RTO) { 411 /* 412 * Timeout here is not necessarily fatal. (e)MMC cards 413 * will splat here when they receive CMD55 as they do 414 * not support this command and that is exactly the way 415 * to tell them apart from SD cards. Thus, this output 416 * below shall be debug(). eMMC cards also do not favor 417 * CMD8, please keep that in mind. 418 */ 419 debug("%s: Response Timeout.\n", __func__); 420 return -ETIMEDOUT; 421 } else if (mask & DWMCI_INTMSK_RE) { 422 debug("%s: Response Error.\n", __func__); 423 return -EIO; 424 } 425 426 427 if (cmd->resp_type & MMC_RSP_PRESENT) { 428 if (cmd->resp_type & MMC_RSP_136) { 429 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3); 430 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2); 431 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1); 432 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0); 433 } else { 434 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0); 435 } 436 } 437 438 if (data) { 439 ret = dwmci_data_transfer(host, data); 440 441 /* only dma mode need it */ 442 if (!host->fifo_mode) { 443 ctrl = dwmci_readl(host, DWMCI_CTRL); 444 ctrl &= ~(DWMCI_DMA_EN); 445 dwmci_writel(host, DWMCI_CTRL, ctrl); 446 bounce_buffer_stop(&bbstate); 447 } 448 } 449 450 return ret; 451 } 452 453 #ifdef CONFIG_SPL_BLK_READ_PREPARE 454 #ifdef CONFIG_DM_MMC 455 static int dwmci_send_cmd_prepare(struct udevice *dev, struct mmc_cmd *cmd, 456 struct mmc_data *data) 457 { 458 struct mmc *mmc = mmc_get_mmc_dev(dev); 459 #else 460 static int dwmci_send_cmd_prepare(struct mmc *mmc, struct mmc_cmd *cmd, 461 struct mmc_data *data) 462 { 463 #endif 464 struct dwmci_host *host = mmc->priv; 465 struct dwmci_idmac *cur_idmac; 466 int ret = 0, flags = 0, i; 467 unsigned int timeout = 500; 468 u32 retry = 100000; 469 u32 mask; 470 ulong start = get_timer(0); 471 struct bounce_buffer bbstate; 472 473 cur_idmac = malloc(ROUND(DIV_ROUND_UP(data->blocks, 8) * 474 sizeof(struct dwmci_idmac), 475 ARCH_DMA_MINALIGN) + ARCH_DMA_MINALIGN - 1); 476 if (!cur_idmac) 477 return -ENODATA; 478 479 while (dwmci_readl(host, DWMCI_STATUS) & DWMCI_BUSY) { 480 if (get_timer(start) > timeout) { 481 debug("%s: Timeout on data busy\n", __func__); 482 return -ETIMEDOUT; 483 } 484 } 485 486 dwmci_writel(host, DWMCI_RINTSTS, DWMCI_INTMSK_ALL); 487 488 if (data) { 489 if (host->fifo_mode) { 490 dwmci_writel(host, DWMCI_BLKSIZ, data->blocksize); 491 dwmci_writel(host, DWMCI_BYTCNT, 492 data->blocksize * data->blocks); 493 dwmci_wait_reset(host, DWMCI_CTRL_FIFO_RESET); 494 } else { 495 if (data->flags == MMC_DATA_READ) { 496 bounce_buffer_start(&bbstate, (void *)data->dest, 497 data->blocksize * 498 data->blocks, GEN_BB_WRITE); 499 } else { 500 bounce_buffer_start(&bbstate, (void *)data->src, 501 data->blocksize * 502 data->blocks, GEN_BB_READ); 503 } 504 dwmci_prepare_data(host, data, cur_idmac, 505 bbstate.bounce_buffer); 506 } 507 } 508 509 dwmci_writel(host, DWMCI_CMDARG, cmd->cmdarg); 510 511 if (data) 512 flags = dwmci_set_transfer_mode(host, data); 513 514 if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) 515 return -1; 516 517 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) 518 flags |= DWMCI_CMD_ABORT_STOP; 519 else 520 flags |= DWMCI_CMD_PRV_DAT_WAIT; 521 522 if (cmd->resp_type & MMC_RSP_PRESENT) { 523 flags |= DWMCI_CMD_RESP_EXP; 524 if (cmd->resp_type & MMC_RSP_136) 525 flags |= DWMCI_CMD_RESP_LENGTH; 526 } 527 528 if (cmd->resp_type & MMC_RSP_CRC) 529 flags |= DWMCI_CMD_CHECK_CRC; 530 531 flags |= (cmd->cmdidx | DWMCI_CMD_START | DWMCI_CMD_USE_HOLD_REG); 532 533 debug("Sending CMD%d\n", cmd->cmdidx); 534 535 dwmci_writel(host, DWMCI_CMD, flags); 536 537 for (i = 0; i < retry; i++) { 538 mask = dwmci_readl(host, DWMCI_RINTSTS); 539 if (mask & DWMCI_INTMSK_CDONE) { 540 if (!data) 541 dwmci_writel(host, DWMCI_RINTSTS, mask); 542 break; 543 } 544 } 545 546 if (i == retry) { 547 debug("%s: Timeout.\n", __func__); 548 return -ETIMEDOUT; 549 } 550 551 if (mask & DWMCI_INTMSK_RTO) { 552 /* 553 * Timeout here is not necessarily fatal. (e)MMC cards 554 * will splat here when they receive CMD55 as they do 555 * not support this command and that is exactly the way 556 * to tell them apart from SD cards. Thus, this output 557 * below shall be debug(). eMMC cards also do not favor 558 * CMD8, please keep that in mind. 559 */ 560 debug("%s: Response Timeout.\n", __func__); 561 return -ETIMEDOUT; 562 } else if (mask & DWMCI_INTMSK_RE) { 563 debug("%s: Response Error.\n", __func__); 564 return -EIO; 565 } 566 567 if (cmd->resp_type & MMC_RSP_PRESENT) { 568 if (cmd->resp_type & MMC_RSP_136) { 569 cmd->response[0] = dwmci_readl(host, DWMCI_RESP3); 570 cmd->response[1] = dwmci_readl(host, DWMCI_RESP2); 571 cmd->response[2] = dwmci_readl(host, DWMCI_RESP1); 572 cmd->response[3] = dwmci_readl(host, DWMCI_RESP0); 573 } else { 574 cmd->response[0] = dwmci_readl(host, DWMCI_RESP0); 575 } 576 } 577 578 return ret; 579 } 580 #endif 581 582 static int dwmci_setup_bus(struct dwmci_host *host, u32 freq) 583 { 584 u32 div, status; 585 int timeout = 10000; 586 unsigned long sclk; 587 588 if (freq == 0) 589 return 0; 590 /* 591 * If host->get_mmc_clk isn't defined, 592 * then assume that host->bus_hz is source clock value. 593 * host->bus_hz should be set by user. 594 */ 595 if (host->get_mmc_clk) 596 sclk = host->get_mmc_clk(host, freq); 597 else if (host->bus_hz) 598 sclk = host->bus_hz; 599 else { 600 debug("%s: Didn't get source clock value.\n", __func__); 601 return -EINVAL; 602 } 603 604 if (sclk == 0) 605 return -EINVAL; 606 607 if (sclk == freq) 608 div = 0; /* bypass mode */ 609 else 610 div = DIV_ROUND_UP(sclk, 2 * freq); 611 612 dwmci_writel(host, DWMCI_CLKENA, 0); 613 dwmci_writel(host, DWMCI_CLKSRC, 0); 614 615 dwmci_writel(host, DWMCI_CLKDIV, div); 616 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT | 617 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START); 618 619 do { 620 status = dwmci_readl(host, DWMCI_CMD); 621 if (timeout-- < 0) { 622 debug("%s: Timeout!\n", __func__); 623 return -ETIMEDOUT; 624 } 625 } while (status & DWMCI_CMD_START); 626 627 dwmci_writel(host, DWMCI_CLKENA, DWMCI_CLKEN_ENABLE | 628 DWMCI_CLKEN_LOW_PWR); 629 630 dwmci_writel(host, DWMCI_CMD, DWMCI_CMD_PRV_DAT_WAIT | 631 DWMCI_CMD_UPD_CLK | DWMCI_CMD_START); 632 633 timeout = 10000; 634 do { 635 status = dwmci_readl(host, DWMCI_CMD); 636 if (timeout-- < 0) { 637 debug("%s: Timeout!\n", __func__); 638 return -ETIMEDOUT; 639 } 640 } while (status & DWMCI_CMD_START); 641 642 host->clock = freq; 643 644 return 0; 645 } 646 647 #ifdef CONFIG_DM_MMC 648 static bool dwmci_card_busy(struct udevice *dev) 649 { 650 struct mmc *mmc = mmc_get_mmc_dev(dev); 651 #else 652 static bool dwmci_card_busy(struct mmc *mmc) 653 { 654 #endif 655 u32 status; 656 struct dwmci_host *host = (struct dwmci_host *)mmc->priv; 657 658 /* 659 * Check the busy bit which is low when DAT[3:0] 660 * (the data lines) are 0000 661 */ 662 status = dwmci_readl(host, DWMCI_STATUS); 663 664 return !!(status & DWMCI_BUSY); 665 } 666 667 #ifdef CONFIG_DM_MMC 668 static int dwmci_execute_tuning(struct udevice *dev, u32 opcode) 669 { 670 struct mmc *mmc = mmc_get_mmc_dev(dev); 671 #else 672 static int dwmci_execute_tuning(struct mmc *mmc, u32 opcode) 673 { 674 #endif 675 struct dwmci_host *host = (struct dwmci_host *)mmc->priv; 676 677 if (!host->execute_tuning) 678 return -EIO; 679 680 return host->execute_tuning(host, opcode); 681 } 682 683 #ifdef CONFIG_DM_MMC 684 static int dwmci_set_ios(struct udevice *dev) 685 { 686 struct mmc *mmc = mmc_get_mmc_dev(dev); 687 #else 688 static int dwmci_set_ios(struct mmc *mmc) 689 { 690 #endif 691 struct dwmci_host *host = (struct dwmci_host *)mmc->priv; 692 u32 ctype, regs; 693 694 debug("Buswidth = %d, clock: %d\n", mmc->bus_width, mmc->clock); 695 696 dwmci_setup_bus(host, mmc->clock); 697 switch (mmc->bus_width) { 698 case 8: 699 ctype = DWMCI_CTYPE_8BIT; 700 break; 701 case 4: 702 ctype = DWMCI_CTYPE_4BIT; 703 break; 704 default: 705 ctype = DWMCI_CTYPE_1BIT; 706 break; 707 } 708 709 dwmci_writel(host, DWMCI_CTYPE, ctype); 710 711 regs = dwmci_readl(host, DWMCI_UHS_REG); 712 if (mmc_card_ddr(mmc)) 713 regs |= DWMCI_DDR_MODE; 714 else 715 regs &= ~DWMCI_DDR_MODE; 716 717 dwmci_writel(host, DWMCI_UHS_REG, regs); 718 719 if (host->clksel) 720 host->clksel(host); 721 722 return 0; 723 } 724 725 static int dwmci_init(struct mmc *mmc) 726 { 727 struct dwmci_host *host = mmc->priv; 728 uint32_t use_dma; 729 uint32_t verid; 730 731 if (host->board_init) 732 host->board_init(host); 733 #ifdef CONFIG_ARCH_ROCKCHIP 734 if (host->dev_index == 0) 735 dwmci_writel(host, DWMCI_PWREN, 1); 736 else if (host->dev_index == 1) 737 dwmci_writel(host, DWMCI_PWREN, 0); 738 else 739 dwmci_writel(host, DWMCI_PWREN, 1); 740 #else 741 dwmci_writel(host, DWMCI_PWREN, 1); 742 #endif 743 744 verid = dwmci_readl(host, DWMCI_VERID) & 0x0000ffff; 745 if (verid >= DW_MMC_240A) 746 dwmci_writel(host, DWMCI_CARDTHRCTL, DWMCI_CDTHRCTRL_CONFIG); 747 748 if (!dwmci_wait_reset(host, DWMCI_RESET_ALL)) { 749 debug("%s[%d] Fail-reset!!\n", __func__, __LINE__); 750 return -EIO; 751 } 752 753 use_dma = SDMMC_GET_TRANS_MODE(dwmci_readl(host, DWMCI_HCON)); 754 if (use_dma == DMA_INTERFACE_IDMA) { 755 host->fifo_mode = 0; 756 } else { 757 host->fifo_mode = 1; 758 } 759 760 /* Enumerate at 400KHz */ 761 dwmci_setup_bus(host, mmc->cfg->f_min); 762 763 dwmci_writel(host, DWMCI_RINTSTS, 0xFFFFFFFF); 764 dwmci_writel(host, DWMCI_INTMASK, 0); 765 766 dwmci_writel(host, DWMCI_TMOUT, 0xFFFFFFFF); 767 768 dwmci_writel(host, DWMCI_IDINTEN, 0); 769 dwmci_writel(host, DWMCI_BMOD, 1); 770 771 if (!host->fifoth_val) { 772 uint32_t fifo_size; 773 774 fifo_size = dwmci_readl(host, DWMCI_FIFOTH); 775 fifo_size = ((fifo_size & RX_WMARK_MASK) >> RX_WMARK_SHIFT) + 1; 776 host->fifoth_val = MSIZE(DWMCI_MSIZE) | 777 RX_WMARK(fifo_size / 2 - 1) | 778 TX_WMARK(fifo_size / 2); 779 } 780 dwmci_writel(host, DWMCI_FIFOTH, host->fifoth_val); 781 782 dwmci_writel(host, DWMCI_CLKENA, 0); 783 dwmci_writel(host, DWMCI_CLKSRC, 0); 784 785 return 0; 786 } 787 788 static int dwmci_get_cd(struct udevice *dev) 789 { 790 int ret = -1; 791 792 #if defined(CONFIG_DM_GPIO) && (defined(CONFIG_SPL_GPIO_SUPPORT) || !defined(CONFIG_SPL_BUILD)) 793 struct gpio_desc detect; 794 795 ret = gpio_request_by_name(dev, "cd-gpios", 0, &detect, GPIOD_IS_IN); 796 if (ret) { 797 return ret; 798 } 799 800 ret = !dm_gpio_get_value(&detect); 801 #endif 802 return ret; 803 } 804 805 #ifdef CONFIG_DM_MMC 806 int dwmci_probe(struct udevice *dev) 807 { 808 struct mmc *mmc = mmc_get_mmc_dev(dev); 809 810 return dwmci_init(mmc); 811 } 812 813 const struct dm_mmc_ops dm_dwmci_ops = { 814 .card_busy = dwmci_card_busy, 815 .send_cmd = dwmci_send_cmd, 816 #ifdef CONFIG_SPL_BLK_READ_PREPARE 817 .send_cmd_prepare = dwmci_send_cmd_prepare, 818 #endif 819 .set_ios = dwmci_set_ios, 820 .get_cd = dwmci_get_cd, 821 .execute_tuning = dwmci_execute_tuning, 822 }; 823 824 #else 825 static const struct mmc_ops dwmci_ops = { 826 .card_busy = dwmci_card_busy, 827 .send_cmd = dwmci_send_cmd, 828 .set_ios = dwmci_set_ios, 829 .get_cd = dwmci_get_cd, 830 .init = dwmci_init, 831 .execute_tuning = dwmci_execute_tuning, 832 }; 833 #endif 834 835 void dwmci_setup_cfg(struct mmc_config *cfg, struct dwmci_host *host, 836 u32 max_clk, u32 min_clk) 837 { 838 cfg->name = host->name; 839 #ifndef CONFIG_DM_MMC 840 cfg->ops = &dwmci_ops; 841 #endif 842 cfg->f_min = min_clk; 843 cfg->f_max = max_clk; 844 845 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195; 846 847 cfg->host_caps = host->caps; 848 849 switch (host->buswidth) { 850 case 8: 851 cfg->host_caps |= MMC_MODE_8BIT | MMC_MODE_4BIT; 852 break; 853 case 4: 854 cfg->host_caps |= MMC_MODE_4BIT; 855 cfg->host_caps &= ~MMC_MODE_8BIT; 856 break; 857 case 1: 858 cfg->host_caps &= ~MMC_MODE_4BIT; 859 cfg->host_caps &= ~MMC_MODE_8BIT; 860 break; 861 default: 862 printf("Unsupported bus width: %d\n", host->buswidth); 863 break; 864 } 865 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz; 866 867 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; 868 } 869 870 #ifdef CONFIG_BLK 871 int dwmci_bind(struct udevice *dev, struct mmc *mmc, struct mmc_config *cfg) 872 { 873 return mmc_bind(dev, mmc, cfg); 874 } 875 #else 876 int add_dwmci(struct dwmci_host *host, u32 max_clk, u32 min_clk) 877 { 878 dwmci_setup_cfg(&host->cfg, host, max_clk, min_clk); 879 880 host->mmc = mmc_create(&host->cfg, host); 881 if (host->mmc == NULL) 882 return -1; 883 884 return 0; 885 } 886 #endif 887