gpcdma.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. /*
  2. * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
  3. *
  4. * SPDX-License-Identifier: BSD-3-Clause
  5. */
  6. #include <arch_helpers.h>
  7. #include <common/debug.h>
  8. #include <drivers/delay_timer.h>
  9. #include <errno.h>
  10. #include <gpcdma.h>
  11. #include <lib/mmio.h>
  12. #include <lib/utils_def.h>
  13. #include <platform_def.h>
  14. #include <stdbool.h>
  15. #include <tegra_def.h>
  16. /* DMA channel registers */
  17. #define DMA_CH_CSR U(0x0)
  18. #define DMA_CH_CSR_WEIGHT_SHIFT U(10)
  19. #define DMA_CH_CSR_XFER_MODE_SHIFT U(21)
  20. #define DMA_CH_CSR_DMA_MODE_MEM2MEM U(4)
  21. #define DMA_CH_CSR_DMA_MODE_FIXEDPATTERN U(6)
  22. #define DMA_CH_CSR_IRQ_MASK_ENABLE (U(1) << 15)
  23. #define DMA_CH_CSR_RUN_ONCE (U(1) << 27)
  24. #define DMA_CH_CSR_ENABLE (U(1) << 31)
  25. #define DMA_CH_STAT U(0x4)
  26. #define DMA_CH_STAT_BUSY (U(1) << 31)
  27. #define DMA_CH_SRC_PTR U(0xC)
  28. #define DMA_CH_DST_PTR U(0x10)
  29. #define DMA_CH_HI_ADR_PTR U(0x14)
  30. #define DMA_CH_HI_ADR_PTR_SRC_MASK U(0xFF)
  31. #define DMA_CH_HI_ADR_PTR_DST_SHIFT U(16)
  32. #define DMA_CH_HI_ADR_PTR_DST_MASK U(0xFF)
  33. #define DMA_CH_MC_SEQ U(0x18)
  34. #define DMA_CH_MC_SEQ_REQ_CNT_SHIFT U(25)
  35. #define DMA_CH_MC_SEQ_REQ_CNT_VAL U(0x10)
  36. #define DMA_CH_MC_SEQ_BURST_SHIFT U(23)
  37. #define DMA_CH_MC_SEQ_BURST_16_WORDS U(0x3)
  38. #define DMA_CH_WORD_COUNT U(0x20)
  39. #define DMA_CH_FIXED_PATTERN U(0x34)
  40. #define DMA_CH_TZ U(0x38)
  41. #define DMA_CH_TZ_ACCESS_ENABLE U(0)
  42. #define DMA_CH_TZ_ACCESS_DISABLE U(3)
  43. #define MAX_TRANSFER_SIZE (1U*1024U*1024U*1024U) /* 1GB */
  44. #define GPCDMA_TIMEOUT_MS U(100)
  45. #define GPCDMA_RESET_BIT (U(1) << 1)
  46. static bool init_done;
  47. static void tegra_gpcdma_write32(uint32_t offset, uint32_t val)
  48. {
  49. mmio_write_32(TEGRA_GPCDMA_BASE + offset, val);
  50. }
  51. static uint32_t tegra_gpcdma_read32(uint32_t offset)
  52. {
  53. return mmio_read_32(TEGRA_GPCDMA_BASE + offset);
  54. }
  55. static void tegra_gpcdma_init(void)
  56. {
  57. /* assert reset for DMA engine */
  58. mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_GPCDMA_RST_SET_REG_OFFSET,
  59. GPCDMA_RESET_BIT);
  60. udelay(2);
  61. /* de-assert reset for DMA engine */
  62. mmio_write_32(TEGRA_CAR_RESET_BASE + TEGRA_GPCDMA_RST_CLR_REG_OFFSET,
  63. GPCDMA_RESET_BIT);
  64. }
  65. static void tegra_gpcdma_memcpy_priv(uint64_t dst_addr, uint64_t src_addr,
  66. uint32_t num_bytes, uint32_t mode)
  67. {
  68. uint32_t val, timeout = 0;
  69. int32_t ret = 0;
  70. /* sanity check byte count */
  71. if ((num_bytes > MAX_TRANSFER_SIZE) || ((num_bytes & 0x3U) != U(0))) {
  72. ret = -EINVAL;
  73. }
  74. /* initialise GPCDMA block */
  75. if (!init_done) {
  76. tegra_gpcdma_init();
  77. init_done = true;
  78. }
  79. /* make sure channel isn't busy */
  80. val = tegra_gpcdma_read32(DMA_CH_STAT);
  81. if ((val & DMA_CH_STAT_BUSY) == DMA_CH_STAT_BUSY) {
  82. ERROR("DMA channel is busy\n");
  83. ret = -EBUSY;
  84. }
  85. if (ret == 0) {
  86. /* disable any DMA transfers */
  87. tegra_gpcdma_write32(DMA_CH_CSR, 0);
  88. /* enable DMA access to TZDRAM */
  89. tegra_gpcdma_write32(DMA_CH_TZ, DMA_CH_TZ_ACCESS_ENABLE);
  90. /* configure MC sequencer */
  91. val = (DMA_CH_MC_SEQ_REQ_CNT_VAL << DMA_CH_MC_SEQ_REQ_CNT_SHIFT) |
  92. (DMA_CH_MC_SEQ_BURST_16_WORDS << DMA_CH_MC_SEQ_BURST_SHIFT);
  93. tegra_gpcdma_write32(DMA_CH_MC_SEQ, val);
  94. /* reset fixed pattern */
  95. tegra_gpcdma_write32(DMA_CH_FIXED_PATTERN, 0);
  96. /* populate src and dst address registers */
  97. tegra_gpcdma_write32(DMA_CH_SRC_PTR, (uint32_t)src_addr);
  98. tegra_gpcdma_write32(DMA_CH_DST_PTR, (uint32_t)dst_addr);
  99. val = (uint32_t)((src_addr >> 32) & DMA_CH_HI_ADR_PTR_SRC_MASK);
  100. val |= (uint32_t)(((dst_addr >> 32) & DMA_CH_HI_ADR_PTR_DST_MASK) <<
  101. DMA_CH_HI_ADR_PTR_DST_SHIFT);
  102. tegra_gpcdma_write32(DMA_CH_HI_ADR_PTR, val);
  103. /* transfer size (in words) */
  104. tegra_gpcdma_write32(DMA_CH_WORD_COUNT, ((num_bytes >> 2) - 1U));
  105. /* populate value for CSR */
  106. val = (mode << DMA_CH_CSR_XFER_MODE_SHIFT) |
  107. DMA_CH_CSR_RUN_ONCE | (U(1) << DMA_CH_CSR_WEIGHT_SHIFT) |
  108. DMA_CH_CSR_IRQ_MASK_ENABLE;
  109. tegra_gpcdma_write32(DMA_CH_CSR, val);
  110. /* enable transfer */
  111. val = tegra_gpcdma_read32(DMA_CH_CSR);
  112. val |= DMA_CH_CSR_ENABLE;
  113. tegra_gpcdma_write32(DMA_CH_CSR, val);
  114. /* wait till transfer completes */
  115. do {
  116. /* read the status */
  117. val = tegra_gpcdma_read32(DMA_CH_STAT);
  118. if ((val & DMA_CH_STAT_BUSY) != DMA_CH_STAT_BUSY) {
  119. break;
  120. }
  121. mdelay(1);
  122. timeout++;
  123. } while (timeout < GPCDMA_TIMEOUT_MS);
  124. /* flag timeout error */
  125. if (timeout == GPCDMA_TIMEOUT_MS) {
  126. ERROR("DMA transfer timed out\n");
  127. }
  128. dsbsy();
  129. /* disable DMA access to TZDRAM */
  130. tegra_gpcdma_write32(DMA_CH_TZ, DMA_CH_TZ_ACCESS_DISABLE);
  131. isb();
  132. }
  133. }
  134. /*******************************************************************************
  135. * Memcpy using GPCDMA block (Mem2Mem copy)
  136. ******************************************************************************/
  137. void tegra_gpcdma_memcpy(uint64_t dst_addr, uint64_t src_addr,
  138. uint32_t num_bytes)
  139. {
  140. tegra_gpcdma_memcpy_priv(dst_addr, src_addr, num_bytes,
  141. DMA_CH_CSR_DMA_MODE_MEM2MEM);
  142. }
  143. /*******************************************************************************
  144. * Memset using GPCDMA block (Fixed pattern write)
  145. ******************************************************************************/
  146. void tegra_gpcdma_zeromem(uint64_t dst_addr, uint32_t num_bytes)
  147. {
  148. tegra_gpcdma_memcpy_priv(dst_addr, 0, num_bytes,
  149. DMA_CH_CSR_DMA_MODE_FIXEDPATTERN);
  150. }