dma_driver.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2015-2020, Renesas Electronics Corporation. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <stdint.h>
  7. #include <string.h>
  8. #include <arch_helpers.h>
  9. #include <common/debug.h>
  10. #include <lib/mmio.h>
  11. #include "cpg_registers.h"
  12. #include "rcar_def.h"
  13. #include "rcar_private.h"
  14. /* DMA CHANNEL setting (0/16/32) */
  15. #if RCAR_LSI == RCAR_V3M
  16. #define DMA_CH 16
  17. #else
  18. #define DMA_CH 0
  19. #endif
  20. #if (DMA_CH == 0)
  21. #define SYS_DMAC_BIT ((uint32_t)1U << 19U)
  22. #define DMA_BASE (0xE6700000U)
  23. #elif (DMA_CH == 16)
  24. #define SYS_DMAC_BIT ((uint32_t)1U << 18U)
  25. #define DMA_BASE (0xE7300000U)
  26. #elif (DMA_CH == 32)
  27. #define SYS_DMAC_BIT ((uint32_t)1U << 17U)
  28. #define DMA_BASE (0xE7320000U)
  29. #else
  30. #define SYS_DMAC_BIT ((uint32_t)1U << 19U)
  31. #define DMA_BASE (0xE6700000U)
  32. #endif
  33. /* DMA operation */
  34. #define DMA_DMAOR (DMA_BASE + 0x0060U)
  35. /* DMA secure control */
  36. #define DMA_DMASEC (DMA_BASE + 0x0030U)
  37. /* DMA channel clear */
  38. #define DMA_DMACHCLR (DMA_BASE + 0x0080U)
  39. /* DMA source address */
  40. #define DMA_DMASAR (DMA_BASE + 0x8000U)
  41. /* DMA destination address */
  42. #define DMA_DMADAR (DMA_BASE + 0x8004U)
  43. /* DMA transfer count */
  44. #define DMA_DMATCR (DMA_BASE + 0x8008U)
  45. /* DMA channel control */
  46. #define DMA_DMACHCR (DMA_BASE + 0x800CU)
  47. /* DMA fixed destination address */
  48. #define DMA_DMAFIXDAR (DMA_BASE + 0x8014U)
  49. #define DMA_USE_CHANNEL (0x00000001U)
  50. #define DMAOR_INITIAL (0x0301U)
  51. #define DMACHCLR_CH_ALL (0x0000FFFFU)
  52. #define DMAFIXDAR_32BIT_SHIFT (32U)
  53. #define DMAFIXDAR_DAR_MASK (0x000000FFU)
  54. #define DMADAR_BOUNDARY_ADDR (0x100000000ULL)
  55. #define DMATCR_CNT_SHIFT (6U)
  56. #define DMATCR_MAX (0x00FFFFFFU)
  57. #define DMACHCR_TRN_MODE (0x00105409U)
  58. #define DMACHCR_DE_BIT (0x00000001U)
  59. #define DMACHCR_TE_BIT (0x00000002U)
  60. #define DMACHCR_CHE_BIT (0x80000000U)
  61. #define DMA_SIZE_UNIT FLASH_TRANS_SIZE_UNIT
  62. #define DMA_FRACTION_MASK (0xFFU)
  63. #define DMA_DST_LIMIT (0x10000000000ULL)
  64. /* transfer length limit */
  65. #define DMA_LENGTH_LIMIT ((DMATCR_MAX * (1U << DMATCR_CNT_SHIFT)) \
  66. & ~DMA_FRACTION_MASK)
  67. static void dma_enable(void)
  68. {
  69. mstpcr_write(CPG_SMSTPCR2, CPG_MSTPSR2, SYS_DMAC_BIT);
  70. }
  71. static void dma_setup(void)
  72. {
  73. mmio_write_16(DMA_DMAOR, 0);
  74. mmio_write_32(DMA_DMACHCLR, DMACHCLR_CH_ALL);
  75. }
  76. static void dma_start(uintptr_t dst, uint32_t src, uint32_t len)
  77. {
  78. mmio_write_16(DMA_DMAOR, DMAOR_INITIAL);
  79. mmio_write_32(DMA_DMAFIXDAR, (dst >> DMAFIXDAR_32BIT_SHIFT) &
  80. DMAFIXDAR_DAR_MASK);
  81. mmio_write_32(DMA_DMADAR, dst & UINT32_MAX);
  82. mmio_write_32(DMA_DMASAR, src);
  83. mmio_write_32(DMA_DMATCR, len >> DMATCR_CNT_SHIFT);
  84. mmio_write_32(DMA_DMASEC, DMA_USE_CHANNEL);
  85. mmio_write_32(DMA_DMACHCR, DMACHCR_TRN_MODE);
  86. }
  87. static void dma_end(void)
  88. {
  89. while ((mmio_read_32(DMA_DMACHCR) & DMACHCR_TE_BIT) == 0) {
  90. if ((mmio_read_32(DMA_DMACHCR) & DMACHCR_CHE_BIT) != 0U) {
  91. ERROR("BL2: DMA - Channel Address Error\n");
  92. panic();
  93. break;
  94. }
  95. }
  96. /* DMA transfer Disable */
  97. mmio_clrbits_32(DMA_DMACHCR, DMACHCR_DE_BIT);
  98. while ((mmio_read_32(DMA_DMACHCR) & DMACHCR_DE_BIT) != 0)
  99. ;
  100. mmio_write_32(DMA_DMASEC, 0);
  101. mmio_write_16(DMA_DMAOR, 0);
  102. mmio_write_32(DMA_DMACHCLR, DMA_USE_CHANNEL);
  103. }
  104. void rcar_dma_exec(uintptr_t dst, uint32_t src, uint32_t len)
  105. {
  106. uint32_t dma_len = len;
  107. if (len & DMA_FRACTION_MASK)
  108. dma_len = (len + DMA_SIZE_UNIT) & ~DMA_FRACTION_MASK;
  109. if (!dma_len || dma_len > DMA_LENGTH_LIMIT) {
  110. ERROR("BL2: DMA - size invalid, length (0x%x)\n", dma_len);
  111. panic();
  112. }
  113. if (src & DMA_FRACTION_MASK) {
  114. ERROR("BL2: DMA - src address invalid (0x%x), len=(0x%x)\n",
  115. src, dma_len);
  116. panic();
  117. }
  118. if ((dst & UINT32_MAX) + dma_len > DMADAR_BOUNDARY_ADDR ||
  119. (dst + dma_len > DMA_DST_LIMIT) ||
  120. (dst & DMA_FRACTION_MASK)) {
  121. ERROR("BL2: DMA - dest address invalid (0x%lx), len=(0x%x)\n",
  122. dst, dma_len);
  123. panic();
  124. }
  125. dma_start(dst, src, dma_len);
  126. dma_end();
  127. }
  128. void rcar_dma_init(void)
  129. {
  130. dma_enable();
  131. dma_setup();
  132. }