fsl_dma.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. * See file CREDITS for list of people who contributed to this
  10. * project.
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License as
  14. * published by the Free Software Foundation; either version 2 of
  15. * the License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  25. * MA 02111-1307 USA
  26. */
  27. #include <config.h>
  28. #include <common.h>
  29. #include <asm/io.h>
  30. #include <asm/fsl_dma.h>
  31. /* Controller can only transfer 2^26 - 1 bytes at a time */
  32. #define FSL_DMA_MAX_SIZE (0x3ffffff)
  33. #if defined(CONFIG_MPC83xx)
  34. #define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_CTM_DIRECT | FSL_DMA_MR_DMSEN)
  35. #else
  36. #define FSL_DMA_MR_DEFAULT (FSL_DMA_MR_BWC_DIS | FSL_DMA_MR_CTM_DIRECT)
  37. #endif
  38. #if defined(CONFIG_MPC83xx)
  39. dma83xx_t *dma_base = (void *)(CONFIG_SYS_MPC83xx_DMA_ADDR);
  40. #elif defined(CONFIG_MPC85xx)
  41. ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC85xx_DMA_ADDR);
  42. #elif defined(CONFIG_MPC86xx)
  43. ccsr_dma_t *dma_base = (void *)(CONFIG_SYS_MPC86xx_DMA_ADDR);
  44. #else
  45. #error "Freescale DMA engine not supported on your processor"
  46. #endif
  47. static void dma_sync(void)
  48. {
  49. #if defined(CONFIG_MPC85xx)
  50. asm("sync; isync; msync");
  51. #elif defined(CONFIG_MPC86xx)
  52. asm("sync; isync");
  53. #endif
  54. }
  55. static void out_dma32(volatile unsigned *addr, int val)
  56. {
  57. #if defined(CONFIG_MPC83xx)
  58. out_le32(addr, val);
  59. #else
  60. out_be32(addr, val);
  61. #endif
  62. }
  63. static uint in_dma32(volatile unsigned *addr)
  64. {
  65. #if defined(CONFIG_MPC83xx)
  66. return in_le32(addr);
  67. #else
  68. return in_be32(addr);
  69. #endif
  70. }
  71. static uint dma_check(void) {
  72. volatile fsl_dma_t *dma = &dma_base->dma[0];
  73. uint status;
  74. /* While the channel is busy, spin */
  75. do {
  76. status = in_dma32(&dma->sr);
  77. } while (status & FSL_DMA_SR_CB);
  78. /* clear MR[CS] channel start bit */
  79. out_dma32(&dma->mr, in_dma32(&dma->mr) & ~FSL_DMA_MR_CS);
  80. dma_sync();
  81. if (status != 0)
  82. printf ("DMA Error: status = %x\n", status);
  83. return status;
  84. }
  85. #if !defined(CONFIG_MPC83xx)
  86. void dma_init(void) {
  87. volatile fsl_dma_t *dma = &dma_base->dma[0];
  88. out_dma32(&dma->satr, FSL_DMA_SATR_SREAD_SNOOP);
  89. out_dma32(&dma->datr, FSL_DMA_DATR_DWRITE_SNOOP);
  90. out_dma32(&dma->sr, 0xffffffff); /* clear any errors */
  91. dma_sync();
  92. }
  93. #endif
  94. int dmacpy(phys_addr_t dest, phys_addr_t src, phys_size_t count) {
  95. volatile fsl_dma_t *dma = &dma_base->dma[0];
  96. uint xfer_size;
  97. while (count) {
  98. xfer_size = MIN(FSL_DMA_MAX_SIZE, count);
  99. out_dma32(&dma->dar, (uint) dest);
  100. out_dma32(&dma->sar, (uint) src);
  101. out_dma32(&dma->bcr, xfer_size);
  102. dma_sync();
  103. /* Prepare mode register */
  104. out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT);
  105. dma_sync();
  106. /* Start the transfer */
  107. out_dma32(&dma->mr, FSL_DMA_MR_DEFAULT | FSL_DMA_MR_CS);
  108. count -= xfer_size;
  109. src += xfer_size;
  110. dest += xfer_size;
  111. dma_sync();
  112. if (dma_check())
  113. return -1;
  114. }
  115. return 0;
  116. }
  117. /*
  118. * 85xx/86xx use dma to initialize SDRAM when !CONFIG_ECC_INIT_VIA_DDRCONTROLLER
  119. * while 83xx uses dma to initialize SDRAM when CONFIG_DDR_ECC_INIT_VIA_DMA
  120. */
  121. #if ((!defined CONFIG_MPC83xx && defined(CONFIG_DDR_ECC) && \
  122. !defined(CONFIG_ECC_INIT_VIA_DDRCONTROLLER)) || \
  123. (defined(CONFIG_MPC83xx) && defined(CONFIG_DDR_ECC_INIT_VIA_DMA)))
  124. void dma_meminit(uint val, uint size)
  125. {
  126. uint *p = 0;
  127. uint i = 0;
  128. for (*p = 0; p < (uint *)(8 * 1024); p++) {
  129. if (((uint)p & 0x1f) == 0)
  130. ppcDcbz((ulong)p);
  131. *p = (uint)CONFIG_MEM_INIT_VALUE;
  132. if (((uint)p & 0x1c) == 0x1c)
  133. ppcDcbf((ulong)p);
  134. }
  135. dmacpy(0x002000, 0, 0x002000); /* 8K */
  136. dmacpy(0x004000, 0, 0x004000); /* 16K */
  137. dmacpy(0x008000, 0, 0x008000); /* 32K */
  138. dmacpy(0x010000, 0, 0x010000); /* 64K */
  139. dmacpy(0x020000, 0, 0x020000); /* 128K */
  140. dmacpy(0x040000, 0, 0x040000); /* 256K */
  141. dmacpy(0x080000, 0, 0x080000); /* 512K */
  142. dmacpy(0x100000, 0, 0x100000); /* 1M */
  143. dmacpy(0x200000, 0, 0x200000); /* 2M */
  144. dmacpy(0x400000, 0, 0x400000); /* 4M */
  145. for (i = 1; i < size / 0x800000; i++)
  146. dmacpy((0x800000 * i), 0, 0x800000);
  147. }
  148. #endif