emmc_interrupt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /*
  2. * Copyright (c) 2015-2020, Renesas Electronics Corporation. All rights
  3. * reserved.
  4. *
  5. * SPDX-License-Identifier: BSD-3-Clause
  6. */
  7. #include <stddef.h>
  8. #include <lib/mmio.h>
  9. #include "emmc_config.h"
  10. #include "emmc_def.h"
  11. #include "emmc_hal.h"
  12. #include "emmc_registers.h"
  13. #include "emmc_std.h"
  14. #include "rcar_def.h"
  15. static EMMC_ERROR_CODE emmc_trans_sector(uint32_t *buff_address_virtual);
  16. uint32_t emmc_interrupt(void)
  17. {
  18. EMMC_ERROR_CODE result;
  19. uint32_t prr_data;
  20. uint32_t cut_ver;
  21. uint32_t end_bit;
  22. prr_data = mmio_read_32((uintptr_t) RCAR_PRR);
  23. cut_ver = prr_data & PRR_CUT_MASK;
  24. if ((prr_data & PRR_PRODUCT_MASK) == PRR_PRODUCT_H3) {
  25. if (cut_ver == PRR_PRODUCT_10) {
  26. end_bit = BIT17;
  27. } else if (cut_ver == PRR_PRODUCT_11) {
  28. end_bit = BIT17;
  29. } else {
  30. end_bit = BIT20;
  31. }
  32. } else if ((prr_data & PRR_PRODUCT_MASK) == PRR_PRODUCT_M3) {
  33. if (cut_ver == PRR_PRODUCT_10) {
  34. end_bit = BIT17;
  35. } else {
  36. end_bit = BIT20;
  37. }
  38. } else {
  39. end_bit = BIT20;
  40. }
  41. /* SD_INFO */
  42. mmc_drv_obj.error_info.info1 = GETR_32(SD_INFO1);
  43. mmc_drv_obj.error_info.info2 = GETR_32(SD_INFO2);
  44. /* SD_INFO EVENT */
  45. mmc_drv_obj.int_event1 =
  46. mmc_drv_obj.error_info.info1 & GETR_32(SD_INFO1_MASK);
  47. mmc_drv_obj.int_event2 =
  48. mmc_drv_obj.error_info.info2 & GETR_32(SD_INFO2_MASK);
  49. /* ERR_STS */
  50. mmc_drv_obj.error_info.status1 = GETR_32(SD_ERR_STS1);
  51. mmc_drv_obj.error_info.status2 = GETR_32(SD_ERR_STS2);
  52. /* DM_CM_INFO */
  53. mmc_drv_obj.error_info.dm_info1 = GETR_32(DM_CM_INFO1);
  54. mmc_drv_obj.error_info.dm_info2 = GETR_32(DM_CM_INFO2);
  55. /* DM_CM_INFO EVENT */
  56. mmc_drv_obj.dm_event1 =
  57. mmc_drv_obj.error_info.dm_info1 & GETR_32(DM_CM_INFO1_MASK);
  58. mmc_drv_obj.dm_event2 =
  59. mmc_drv_obj.error_info.dm_info2 & GETR_32(DM_CM_INFO2_MASK);
  60. /* ERR SD_INFO2 */
  61. if ((SD_INFO2_ALL_ERR & mmc_drv_obj.int_event2) != 0) {
  62. SETR_32(SD_INFO1_MASK, 0x00000000U); /* interrupt disable */
  63. SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR); /* interrupt disable */
  64. SETR_32(SD_INFO1, 0x00000000U); /* interrupt clear */
  65. SETR_32(SD_INFO2, SD_INFO2_CLEAR); /* interrupt clear */
  66. mmc_drv_obj.state_machine_blocking = FALSE;
  67. }
  68. /* PIO Transfer */
  69. /* BWE/BRE */
  70. else if (((SD_INFO2_BWE | SD_INFO2_BRE) & mmc_drv_obj.int_event2)) {
  71. /* BWE */
  72. if (SD_INFO2_BWE & mmc_drv_obj.int_event2) {
  73. SETR_32(SD_INFO2, (GETR_32(SD_INFO2) & ~SD_INFO2_BWE));
  74. }
  75. /* BRE */
  76. else {
  77. SETR_32(SD_INFO2, (GETR_32(SD_INFO2) & ~SD_INFO2_BRE));
  78. }
  79. result = emmc_trans_sector(mmc_drv_obj.buff_address_virtual);
  80. mmc_drv_obj.buff_address_virtual += EMMC_BLOCK_LENGTH;
  81. mmc_drv_obj.remain_size -= EMMC_BLOCK_LENGTH;
  82. if (result != EMMC_SUCCESS) {
  83. /* data transfer error */
  84. emmc_write_error_info(EMMC_FUNCNO_NONE, result);
  85. /* Panic */
  86. SETR_32(SD_INFO1_MASK, 0x00000000U);
  87. SETR_32(SD_INFO2_MASK, SD_INFO2_CLEAR);
  88. SETR_32(SD_INFO1, 0x00000000U);
  89. /* interrupt clear */
  90. SETR_32(SD_INFO2, SD_INFO2_CLEAR);
  91. mmc_drv_obj.force_terminate = TRUE;
  92. } else {
  93. mmc_drv_obj.during_transfer = FALSE;
  94. }
  95. mmc_drv_obj.state_machine_blocking = FALSE;
  96. }
  97. /* DMA_TRANSFER */
  98. /* DM_CM_INFO1: DMA-ch0 transfer complete or error occurred */
  99. else if ((BIT16 & mmc_drv_obj.dm_event1) != 0) {
  100. SETR_32(DM_CM_INFO1, 0x00000000U);
  101. SETR_32(DM_CM_INFO2, 0x00000000U);
  102. /* interrupt clear */
  103. SETR_32(SD_INFO2, (GETR_32(SD_INFO2) & ~SD_INFO2_BWE));
  104. /* DM_CM_INFO2: DMA-ch0 error occurred */
  105. if ((BIT16 & mmc_drv_obj.dm_event2) != 0) {
  106. mmc_drv_obj.dma_error_flag = TRUE;
  107. } else {
  108. mmc_drv_obj.during_dma_transfer = FALSE;
  109. mmc_drv_obj.during_transfer = FALSE;
  110. }
  111. /* wait next interrupt */
  112. mmc_drv_obj.state_machine_blocking = FALSE;
  113. }
  114. /* DM_CM_INFO1: DMA-ch1 transfer complete or error occurred */
  115. else if ((end_bit & mmc_drv_obj.dm_event1) != 0U) {
  116. SETR_32(DM_CM_INFO1, 0x00000000U);
  117. SETR_32(DM_CM_INFO2, 0x00000000U);
  118. /* interrupt clear */
  119. SETR_32(SD_INFO2, (GETR_32(SD_INFO2) & ~SD_INFO2_BRE));
  120. /* DM_CM_INFO2: DMA-ch1 error occurred */
  121. if ((BIT17 & mmc_drv_obj.dm_event2) != 0) {
  122. mmc_drv_obj.dma_error_flag = TRUE;
  123. } else {
  124. mmc_drv_obj.during_dma_transfer = FALSE;
  125. mmc_drv_obj.during_transfer = FALSE;
  126. }
  127. /* wait next interrupt */
  128. mmc_drv_obj.state_machine_blocking = FALSE;
  129. }
  130. /* Response end */
  131. else if ((SD_INFO1_INFO0 & mmc_drv_obj.int_event1) != 0) {
  132. /* interrupt clear */
  133. SETR_32(SD_INFO1, (GETR_32(SD_INFO1) & ~SD_INFO1_INFO0));
  134. mmc_drv_obj.state_machine_blocking = FALSE;
  135. }
  136. /* Access end */
  137. else if ((SD_INFO1_INFO2 & mmc_drv_obj.int_event1) != 0) {
  138. /* interrupt clear */
  139. SETR_32(SD_INFO1, (GETR_32(SD_INFO1) & ~SD_INFO1_INFO2));
  140. mmc_drv_obj.state_machine_blocking = FALSE;
  141. } else {
  142. /* nothing to do. */
  143. }
  144. return (uint32_t) 0;
  145. }
  146. static EMMC_ERROR_CODE emmc_trans_sector(uint32_t *buff_address_virtual)
  147. {
  148. uint32_t length, i;
  149. uint64_t *bufPtrLL;
  150. if (buff_address_virtual == NULL) {
  151. return EMMC_ERR_PARAM;
  152. }
  153. if ((mmc_drv_obj.during_transfer != TRUE)
  154. || (mmc_drv_obj.remain_size == 0)) {
  155. return EMMC_ERR_STATE;
  156. }
  157. bufPtrLL = (uint64_t *) buff_address_virtual;
  158. length = mmc_drv_obj.remain_size;
  159. /* data transefer */
  160. for (i = 0; i < (length >> 3); i++) {
  161. /* Write */
  162. if (mmc_drv_obj.cmd_info.dir == HAL_MEMCARD_WRITE) {
  163. SETR_64(SD_BUF0, *bufPtrLL); /* buffer --> FIFO */
  164. }
  165. /* Read */
  166. else {
  167. /* Checks when the read data reaches SD_SIZE. */
  168. /* The BRE bit is cleared at emmc_interrupt function. */
  169. if (((i %
  170. (uint32_t) (EMMC_BLOCK_LENGTH >>
  171. EMMC_BUF_SIZE_SHIFT)) == 0U)
  172. && (i != 0U)) {
  173. /* BRE check */
  174. while (((GETR_32(SD_INFO2)) & SD_INFO2_BRE) ==
  175. 0U) {
  176. /* ERROR check */
  177. if (((GETR_32(SD_INFO2)) &
  178. SD_INFO2_ALL_ERR) != 0U) {
  179. return EMMC_ERR_TRANSFER;
  180. }
  181. }
  182. /* BRE clear */
  183. SETR_32(SD_INFO2,
  184. (uint32_t) (GETR_32(SD_INFO2) &
  185. ~SD_INFO2_BRE));
  186. }
  187. *bufPtrLL = GETR_64(SD_BUF0); /* FIFO --> buffer */
  188. }
  189. bufPtrLL++;
  190. }
  191. return EMMC_SUCCESS;
  192. }