emmc_init.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /*
  2. * Copyright (c) 2015-2021, Renesas Electronics Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stddef.h>
  7. #include <lib/mmio.h>
  8. #include "emmc_config.h"
  9. #include "emmc_hal.h"
  10. #include "emmc_std.h"
  11. #include "emmc_registers.h"
  12. #include "emmc_def.h"
  13. #include "rcar_private.h"
  14. #include "cpg_registers.h"
  15. st_mmc_base mmc_drv_obj;
  16. EMMC_ERROR_CODE rcar_emmc_memcard_power(uint8_t mode)
  17. {
  18. if (mode == TRUE) {
  19. /* power on (Vcc&Vccq is always power on) */
  20. mmc_drv_obj.card_power_enable = TRUE;
  21. } else {
  22. /* power off (Vcc&Vccq is always power on) */
  23. mmc_drv_obj.card_power_enable = FALSE;
  24. mmc_drv_obj.mount = FALSE;
  25. mmc_drv_obj.selected = FALSE;
  26. }
  27. return EMMC_SUCCESS;
  28. }
  29. static inline void emmc_set_retry_count(uint32_t retry)
  30. {
  31. mmc_drv_obj.retries_after_fail = retry;
  32. }
  33. static inline void emmc_set_data_timeout(uint32_t data_timeout)
  34. {
  35. mmc_drv_obj.data_timeout = data_timeout;
  36. }
  37. static void emmc_memset(uint8_t *buff, uint8_t data, uint32_t cnt)
  38. {
  39. if (buff == NULL) {
  40. return;
  41. }
  42. while (cnt > 0) {
  43. *buff++ = data;
  44. cnt--;
  45. }
  46. }
  47. static void emmc_driver_config(void)
  48. {
  49. emmc_set_retry_count(EMMC_RETRY_COUNT);
  50. emmc_set_data_timeout(EMMC_RW_DATA_TIMEOUT);
  51. }
  52. static void emmc_drv_init(void)
  53. {
  54. emmc_memset((uint8_t *) (&mmc_drv_obj), 0, sizeof(st_mmc_base));
  55. mmc_drv_obj.card_present = HAL_MEMCARD_CARD_IS_IN;
  56. mmc_drv_obj.data_timeout = EMMC_RW_DATA_TIMEOUT;
  57. mmc_drv_obj.bus_width = HAL_MEMCARD_DATA_WIDTH_1_BIT;
  58. }
  59. static EMMC_ERROR_CODE emmc_dev_finalize(void)
  60. {
  61. EMMC_ERROR_CODE result;
  62. uint32_t dataL;
  63. /*
  64. * MMC power off
  65. * the power supply of eMMC device is always turning on.
  66. * RST_n : Hi --> Low level.
  67. */
  68. result = rcar_emmc_memcard_power(FALSE);
  69. /* host controller reset */
  70. SETR_32(SD_INFO1, 0x00000000U); /* all interrupt clear */
  71. SETR_32(SD_INFO2, SD_INFO2_CLEAR); /* all interrupt clear */
  72. SETR_32(SD_INFO1_MASK, 0x00000000U); /* all interrupt disable */
  73. SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR); /* all interrupt disable */
  74. SETR_32(SD_CLK_CTRL, 0x00000000U); /* MMC clock stop */
  75. dataL = mmio_read_32(SMSTPCR3);
  76. if ((dataL & CPG_MSTP_MMC) == 0U) {
  77. dataL |= (CPG_MSTP_MMC);
  78. mmio_write_32(CPG_CPGWPR, (~dataL));
  79. mmio_write_32(SMSTPCR3, dataL);
  80. }
  81. return result;
  82. }
  83. static EMMC_ERROR_CODE emmc_dev_init(void)
  84. {
  85. /* Enable clock supply to eMMC. */
  86. mstpcr_write(SMSTPCR3, CPG_MSTPSR3, CPG_MSTP_MMC);
  87. /* Set SD clock */
  88. mmio_write_32(CPG_CPGWPR, ~((uint32_t) (BIT9 | BIT0))); /* SD phy 200MHz */
  89. /* Stop SDnH clock & SDn=200MHz */
  90. mmio_write_32(CPG_SDxCKCR, (BIT9 | BIT0));
  91. /* MMCIF initialize */
  92. SETR_32(SD_INFO1, 0x00000000U); /* all interrupt clear */
  93. SETR_32(SD_INFO2, SD_INFO2_CLEAR); /* all interrupt clear */
  94. SETR_32(SD_INFO1_MASK, 0x00000000U); /* all interrupt disable */
  95. SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR); /* all interrupt disable */
  96. SETR_32(HOST_MODE, 0x00000000U); /* SD_BUF access width = 64-bit */
  97. SETR_32(SD_OPTION, 0x0000C0EEU); /* Bus width = 1bit, timeout=MAX */
  98. SETR_32(SD_CLK_CTRL, 0x00000000U); /* Disable Automatic Control & Clock Output */
  99. return EMMC_SUCCESS;
  100. }
  101. static EMMC_ERROR_CODE emmc_reset_controller(void)
  102. {
  103. EMMC_ERROR_CODE result;
  104. /* initialize mmc driver */
  105. emmc_drv_init();
  106. /* initialize H/W */
  107. result = emmc_dev_init();
  108. if (result == EMMC_SUCCESS) {
  109. mmc_drv_obj.initialize = TRUE;
  110. }
  111. return result;
  112. }
  113. EMMC_ERROR_CODE emmc_terminate(void)
  114. {
  115. EMMC_ERROR_CODE result;
  116. result = emmc_dev_finalize();
  117. emmc_memset((uint8_t *) (&mmc_drv_obj), 0, sizeof(st_mmc_base));
  118. return result;
  119. }
  120. EMMC_ERROR_CODE rcar_emmc_init(void)
  121. {
  122. EMMC_ERROR_CODE result;
  123. result = emmc_reset_controller();
  124. if (result == EMMC_SUCCESS) {
  125. emmc_driver_config();
  126. }
  127. return result;
  128. }