mmu.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* ----------------------------------------------------------------------------
  2. * SAM Software Package License
  3. * ----------------------------------------------------------------------------
  4. * Copyright (c) 2013, Atmel Corporation
  5. *
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  11. * - Redistributions of source code must retain the above copyright notice,
  12. * this list of conditions and the disclaimer below.
  13. *
  14. * Atmel's name may not be used to endorse or promote products derived from
  15. * this software without specific prior written permission.
  16. *
  17. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
  18. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  20. * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
  21. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  22. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  23. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  24. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  25. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  26. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. * ----------------------------------------------------------------------------
  28. */
  29. /** \file */
  30. /**
  31. * \addtogroup mmu MMU Initialization
  32. *
  33. * \section Usage
  34. *
  35. * Translation Lookaside Buffers (TLBs) are an implementation technique that caches translations or
  36. * translation table entries. TLBs avoid the requirement for every memory access to perform a translation table
  37. * lookup. The ARM architecture does not specify the exact form of the TLB structures for any design. In a
  38. * similar way to the requirements for caches, the architecture only defines certain principles for TLBs:
  39. *
  40. * The MMU supports memory accesses based on memory sections or pages:
  41. * Supersections Consist of 16MB blocks of memory. Support for Supersections is optional.
  42. * -# Sections Consist of 1MB blocks of memory.
  43. * -# Large pages Consist of 64KB blocks of memory.
  44. * -# Small pages Consist of 4KB blocks of memory.
  45. *
  46. * Access to a memory region is controlled by the access permission bits and the domain field in the TLB entry.
  47. * Memory region attributes
  48. * Each TLB entry has an associated set of memory region attributes. These control accesses to the caches,
  49. * how the write buffer is used, and if the memory region is Shareable and therefore must be kept coherent.
  50. *
  51. * Related files:\n
  52. * \ref mmu.c\n
  53. * \ref mmu.h \n
  54. */
  55. /*------------------------------------------------------------------------------ */
  56. /* Headers */
  57. /*------------------------------------------------------------------------------ */
  58. #include <chip.h>
  59. /*------------------------------------------------------------------------------ */
  60. /* Exported functions */
  61. /*------------------------------------------------------------------------------ */
  62. /**
  63. * \brief Initializes MMU.
  64. * \param pTB Address of the translation table.
  65. */
  66. void MMU_Initialize(uint32_t *pTB)
  67. {
  68. unsigned int index;
  69. unsigned int addr;
  70. /* Reset table entries */
  71. for (index = 0; index < 4096; index++)
  72. pTB[index] = 0;
  73. /* interrupt vector address (after remap) 0x0000_0000 */
  74. pTB[0x000] = (0x600 << 20)| // Physical Address
  75. // ( 1 << 12)| // TEX[0]
  76. ( 3 << 10)| // Access in supervisor mode (AP)
  77. ( 0xF << 5)| // Domain 0xF
  78. ( 1 << 4)| // (XN)
  79. ( 0 << 3)| // C bit : cachable => YES
  80. ( 0 << 2)| // B bit : write-back => YES
  81. ( 2 << 0); // Set as 1 Mbyte section
  82. /* AXI-SRAM(2MB) address (after remap) 0x8000_0000 */
  83. for(addr = 0x001; addr < 0x003; addr++)
  84. {
  85. pTB[addr] = (addr << 20)| // Physical Address
  86. // ( 1 << 12)| // TEX[0]
  87. ( 3 << 10)| // Access in supervisor mode (AP)
  88. ( 0xF << 5)| // Domain 0xF
  89. ( 1 << 4)| // (XN)
  90. ( 0 << 3)| // C bit : cachable => NO
  91. ( 0 << 2)| // B bit : write-back => NO
  92. ( 2 << 0); // Set as 1 Mbyte section
  93. }
  94. // periph address 0x40000000 ~ 0x52000000
  95. for(addr = 0x400; addr < 0x520; addr++)
  96. {
  97. pTB[addr] = (addr << 20)| // Physical Address
  98. ( 3 << 10)| // Access in supervisor mode (AP)
  99. ( 0xF << 5)| // Domain 0xF
  100. ( 1 << 4)| // (XN)
  101. ( 0 << 3)| // C bit : cachable => NO
  102. ( 0 << 2)| // B bit : write-back => NO
  103. ( 2 << 0); // Set as 1 Mbyte section
  104. }
  105. // DDR SDRAM
  106. for(addr = 0x600; addr < 0x700; addr++)
  107. {
  108. pTB[addr] = (addr << 20)| // Physical Address
  109. ( 3 << 10)| // Access in supervisor mode (AP)
  110. ( 1 << 12)| // TEX[0]
  111. ( 0 << 5)| // Domain 0x0,Supersection only support domain 0
  112. ( 0 << 4)| // (XN)
  113. ( 1 << 3)| // C bit : cachable => YES
  114. ( 1 << 2)| // B bit : write-back => YES
  115. ( 2 << 0); // Set as 1 Mbyte section
  116. }
  117. // DDR SDRAM NOCACHE
  118. for(addr = 0x700; addr < 0x800; addr++)
  119. {
  120. pTB[addr] = (addr-0x100 << 20)| // Physical Address
  121. ( 3 << 10)| // Access in supervisor mode (AP)
  122. ( 1 << 12)| // TEX[0]
  123. ( 0 << 5)| // Domain 0x0,Supersection only support domain 0
  124. ( 0 << 4)| // (XN)
  125. ( 0 << 3)| // C bit : cachable => NO
  126. ( 0 << 2)| // B bit : write-back => NO
  127. ( 2 << 0); // Set as 1 Mbyte section
  128. }
  129. // GIC
  130. for(addr = 0xE0B; addr < 0xE10; addr++)
  131. pTB[addr] = (addr << 20)| // Physical Address
  132. ( 3 << 10)| // Access in supervisor mode (AP)
  133. ( 0 << 5)| // Domain 0x0, Supersection only support domain 0
  134. ( 1 << 4)| // (XN)
  135. ( 0 << 3)| // C bit : cachable => YES
  136. ( 0 << 2)| // B bit : write-back => YES
  137. ( 2 << 0); // Set as 1 Mbyte section
  138. CP15_WriteTTB((unsigned int)pTB);
  139. /* Program the domain access register */
  140. CP15_WriteDomainAccessControl(0xC0000003); // only domain 0 & 15: access are not checked
  141. }
  142. void dma_inv_range(UINT32 ulStart, UINT32 ulEnd)
  143. {
  144. CP15_invalidate_dcache_for_dma (ulStart, ulEnd);
  145. }
  146. void dma_clean_range(UINT32 ulStart, UINT32 ulEnd)
  147. {
  148. CP15_clean_dcache_for_dma (ulStart, ulEnd);
  149. }
  150. // flush��clean and invalidate
  151. void dma_flush_range(UINT32 ulStart, UINT32 ulEnd)
  152. {
  153. CP15_flush_dcache_for_dma (ulStart, ulEnd);
  154. }