fsl_dma.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * Copyright 2004,2007,2008 Freescale Semiconductor, Inc.
  3. * (C) Copyright 2002, 2003 Motorola Inc.
  4. * Xianghua Xiao (X.Xiao@motorola.com)
  5. *
  6. * (C) Copyright 2000
  7. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  8. *
  9. * SPDX-License-Identifier: GPL-2.0+
  10. */
  11. #include <config.h>
  12. #include <common.h>
  13. #include <asm/io.h>
  14. #include <asm/fsl_dma.h>
  15. /* Controller can only transfer 2^26 - 1 bytes at a time */
  16. #define FSL_DMA_MAX_SIZE (0x3ffffff)
  17. #if defined(CONFIG_MPC83xx)
  18. #define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_CTM_DIRECT | FSL_DMA_MR_DMSEN)
  19. #else
  20. #define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_BWC_DIS | FSL_DMA_MR_CTM_DIRECT)
  21. #endif
  22. #if defined(CONFIG_MPC83xx)
  23. dma83xx_t *dma_base = (void *)(CONFIG_SYS_MPC83xx_DMA_ADDR);
  24. #elif defined(CONFIG_MPC85xx)
  25. ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC85xx_DMA_ADDR);
  26. #elif defined(CONFIG_MPC86xx)
  27. ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC86xx_DMA_ADDR);
  28. #else
  29. #error "Freescale DMA engine not supported on your processor"
  30. #endif
  31. static void dma_sync(void)
  32. {
  33. #if defined(CONFIG_MPC85xx)
  34. asm("sync; isync; msync");
  35. #elif defined(CONFIG_MPC86xx)
  36. asm("sync; isync");
  37. #endif
  38. }
  39. static void out_dma32(volatile unsigned *addr, int val)
  40. {
  41. #if defined(CONFIG_MPC83xx)
  42. out_le32(addr, val);
  43. #else
  44. out_be32(addr, val);
  45. #endif
  46. }
  47. static uint in_dma32(volatile unsigned *addr)
  48. {
  49. #if defined(CONFIG_MPC83xx)
  50. return in_le32(addr);
  51. #else
  52. return in_be32(addr);
  53. #endif
  54. }
  55. static uint dma_check(void) {
  56. volatile fsl_dma_t *dma = &dma_base->dma[0];
  57. uint status;
  58. /* While the channel is busy, spin */
  59. do {
  60. status = in_dma32(&dma->sr);
  61. } while (status & FSL_DMA_SR_CB);
  62. /* clear MR[CS] channel start bit */
  63. out_dma32(&dma->mr, in_dma32(&dma->mr) & ~FSL_DMA_MR_CS);
  64. dma_sync();
  65. if (status != 0)
  66. printf ("DMA Error: status = %x\n", status);
  67. return status;
  68. }
  69. #if !defined(CONFIG_MPC83xx)
  70. void dma_init(void) {
  71. volatile fsl_dma_t *dma = &dma_base->dma[0];
  72. out_dma32(&dma->satr, FSL_DMA_SATR_SREAD_SNOOP);
  73. out_dma32(&dma->datr, FSL_DMA_DATR_DWRITE_SNOOP);
  74. out_dma32(&dma->sr, 0xffffffff); /* clear any errors */
  75. dma_sync();
  76. }
  77. #endif
  78. int dmacpy(phys_addr_t dest, phys_addr_t src, phys_size_t count) {
  79. volatile fsl_dma_t *dma = &dma_base->dma[0];
  80. uint xfer_size;
  81. while (count) {
  82. xfer_size = min(FSL_DMA_MAX_SIZE, count);
  83. out_dma32(&dma->dar, (u32) (dest & 0xFFFFFFFF));
  84. out_dma32(&dma->sar, (u32) (src & 0xFFFFFFFF));
  85. #if !defined(CONFIG_MPC83xx)
  86. out_dma32(&dma->satr,
  87. in_dma32(&dma->satr) | (u32)((u64)src >> 32));
  88. out_dma32(&dma->datr,
  89. in_dma32(&dma->datr) | (u32)((u64)dest >> 32));
  90. #endif
  91. out_dma32(&dma->bcr, xfer_size);
  92. dma_sync();
  93. /* Prepare mode register */
  94. out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT);
  95. dma_sync();
  96. /* Start the transfer */
  97. out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT | FSL_DMA_MR_CS);
  98. count -= xfer_size;
  99. src += xfer_size;
  100. dest += xfer_size;
  101. dma_sync();
  102. if (dma_check())
  103. return -1;
  104. }
  105. return 0;
  106. }
  107. /*
  108. * 85xx/86xx use dma to initialize SDRAM when !CONFIG_ECC_INIT_VIA_DDRCONTROLLER
  109. * while 83xx uses dma to initialize SDRAM when CONFIG_DDR_ECC_INIT_VIA_DMA
  110. */
  111. #if ((!defined CONFIG_MPC83xx && defined(CONFIG_DDR_ECC) && \
  112. !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)) || \
  113. (defined(CONFIG_MPC83xx) && defined(CONFIG_DDR_ECC_INIT_VIA_DMA)))
  114. void dma_meminit(uint val, uint size)
  115. {
  116. uint *p = 0;
  117. uint i = 0;
  118. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  119. if (((uint)p & 0x1f) == 0)
  120. ppcDcbz((ulong)p);
  121. *p = (uint)CONFIG_MEM_INIT_VALUE;
  122. if (((uint)p & 0x1c) == 0x1c)
  123. ppcDcbf((ulong)p);
  124. }
  125. dmacpy(0x002000, 0, 0x002000); /* 8K */
  126. dmacpy(0x004000, 0, 0x004000); /* 16K */
  127. dmacpy(0x008000, 0, 0x008000); /* 32K */
  128. dmacpy(0x010000, 0, 0x010000); /* 64K */
  129. dmacpy(0x020000, 0, 0x020000); /* 128K */
  130. dmacpy(0x040000, 0, 0x040000); /* 256K */
  131. dmacpy(0x080000, 0, 0x080000); /* 512K */
  132. dmacpy(0x100000, 0, 0x100000); /* 1M */
  133. dmacpy(0x200000, 0, 0x200000); /* 2M */
  134. dmacpy(0x400000, 0, 0x400000); /* 4M */
  135. for (i = 1; i < size / 0x800000; i++)
  136. dmacpy((0x800000 * i), 0, 0x800000);
  137. }
  138. #endif