mmu_ARMCA5.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. /**************************************************************************//**
  2. * @file mmu_ARMCA5.c
  3. * @brief MMU Configuration for ARM Cortex-A5 Device Series
  4. * @version V1.2.0
  5. * @date 15. May 2019
  6. *
  7. * @note
  8. *
  9. ******************************************************************************/
  10. /*
  11. * Copyright (c) 2009-2019 Arm Limited. All rights reserved.
  12. *
  13. * SPDX-License-Identifier: Apache-2.0
  14. *
  15. * Licensed under the Apache License, Version 2.0 (the License); you may
  16. * not use this file except in compliance with the License.
  17. * You may obtain a copy of the License at
  18. *
  19. * www.apache.org/licenses/LICENSE-2.0
  20. *
  21. * Unless required by applicable law or agreed to in writing, software
  22. * distributed under the License is distributed on an AS IS BASIS, WITHOUT
  23. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  24. * See the License for the specific language governing permissions and
  25. * limitations under the License.
  26. */
  27. /* Memory map description from: DUI0447G_v2m_p1_trm.pdf 4.2.2 Arm Cortex-A Series memory map
  28. Memory Type
  29. 0xffffffff |--------------------------| ------------
  30. | FLAG SYNC | Device Memory
  31. 0xfffff000 |--------------------------| ------------
  32. | Fault | Fault
  33. 0xfff00000 |--------------------------| ------------
  34. | | Normal
  35. | |
  36. | Daughterboard |
  37. | memory |
  38. | |
  39. 0x80505000 |--------------------------| ------------
  40. |TTB (L2 Sync Flags ) 4k | Normal
  41. 0x80504C00 |--------------------------| ------------
  42. |TTB (L2 Peripherals-B) 16k| Normal
  43. 0x80504800 |--------------------------| ------------
  44. |TTB (L2 Peripherals-A) 16k| Normal
  45. 0x80504400 |--------------------------| ------------
  46. |TTB (L2 Priv Periphs) 4k | Normal
  47. 0x80504000 |--------------------------| ------------
  48. | TTB (L1 Descriptors) | Normal
  49. 0x80500000 |--------------------------| ------------
  50. | Stack | Normal
  51. |--------------------------| ------------
  52. | Heap | Normal
  53. 0x80400000 |--------------------------| ------------
  54. | ZI Data | Normal
  55. 0x80300000 |--------------------------| ------------
  56. | RW Data | Normal
  57. 0x80200000 |--------------------------| ------------
  58. | RO Data | Normal
  59. |--------------------------| ------------
  60. | RO Code | USH Normal
  61. 0x80000000 |--------------------------| ------------
  62. | Daughterboard | Fault
  63. | HSB AXI buses |
  64. 0x40000000 |--------------------------| ------------
  65. | Daughterboard | Fault
  66. | test chips peripherals |
  67. 0x2c002000 |--------------------------| ------------
  68. | Private Address | Device Memory
  69. 0x2c000000 |--------------------------| ------------
  70. | Daughterboard | Fault
  71. | test chips peripherals |
  72. 0x20000000 |--------------------------| ------------
  73. | Peripherals | Device Memory RW/RO
  74. | | & Fault
  75. 0x00000000 |--------------------------|
  76. */
  77. // L1 Cache info and restrictions about architecture of the caches (CCSIR register):
  78. // Write-Through support *not* available
  79. // Write-Back support available.
  80. // Read allocation support available.
  81. // Write allocation support available.
  82. //Note: You should use the Shareable attribute carefully.
  83. //For cores without coherency logic (such as SCU) marking a region as shareable forces the processor to not cache that region regardless of the inner cache settings.
  84. //Cortex-A versions of RTX use LDREX/STREX instructions relying on Local monitors. Local monitors will be used only when the region gets cached, regions that are not cached will use the Global Monitor.
  85. //Some Cortex-A implementations do not include Global Monitors, so wrongly setting the attribute Shareable may cause STREX to fail.
  86. //Recall: When the Shareable attribute is applied to a memory region that is not Write-Back, Normal memory, data held in this region is treated as Non-cacheable.
  87. //When SMP bit = 0, Inner WB/WA Cacheable Shareable attributes are treated as Non-cacheable.
  88. //When SMP bit = 1, Inner WB/WA Cacheable Shareable attributes are treated as Cacheable.
  89. //Following MMU configuration is expected
  90. //SCTLR.AFE == 1 (Simplified access permissions model - AP[2:1] define access permissions, AP[0] is an access flag)
  91. //SCTLR.TRE == 0 (TEX remap disabled, so memory type and attributes are described directly by bits in the descriptor)
  92. //Domain 0 is always the Client domain
  93. //Descriptors should place all memory in domain 0
  94. #include "ARMCA5.h"
  95. #include "mem_ARMCA5.h"
  96. // TTB base address
  97. #define TTB_BASE ((uint32_t*)__TTB_BASE)
  98. // L2 table pointers
  99. //----------------------------------------
  100. #define TTB_L1_SIZE (0x00004000) // The L1 translation table divides the full 4GB address space of a 32-bit core
  101. // into 4096 equally sized sections, each of which describes 1MB of virtual memory space.
  102. // The L1 translation table therefore contains 4096 32-bit (word-sized) entries.
  103. #define PRIVATE_TABLE_L2_BASE_4k (__TTB_BASE + TTB_L1_SIZE) // Map 4k Private Address space
  104. #define PERIPHERAL_A_TABLE_L2_BASE_64k (__TTB_BASE + TTB_L1_SIZE + 0x400) // Map 64k Peripheral #1 0x1C000000 - 0x1C00FFFFF
  105. #define PERIPHERAL_B_TABLE_L2_BASE_64k (__TTB_BASE + TTB_L1_SIZE + 0x800) // Map 64k Peripheral #2 0x1C100000 - 0x1C1FFFFFF
  106. #define SYNC_FLAGS_TABLE_L2_BASE_4k (__TTB_BASE + TTB_L1_SIZE + 0xC00) // Map 4k Flag synchronization
  107. //--------------------- PERIPHERALS -------------------
  108. #define PERIPHERAL_A_FAULT (0x00000000 + 0x1c000000) //0x1C000000-0x1C00FFFF (1M)
  109. #define PERIPHERAL_B_FAULT (0x00100000 + 0x1c000000) //0x1C100000-0x1C10FFFF (1M)
  110. //--------------------- SYNC FLAGS --------------------
  111. #define FLAG_SYNC 0xFFFFF000
  112. #define F_SYNC_BASE 0xFFF00000 //1M aligned
  113. static uint32_t Sect_Normal; //outer & inner wb/wa, non-shareable, executable, rw, domain 0, base addr 0
  114. static uint32_t Sect_Normal_Cod; //outer & inner wb/wa, non-shareable, executable, ro, domain 0, base addr 0
  115. static uint32_t Sect_Normal_RO; //as Sect_Normal_Cod, but not executable
  116. static uint32_t Sect_Normal_RW; //as Sect_Normal_Cod, but writeable and not executable
  117. static uint32_t Sect_Device_RO; //device, non-shareable, non-executable, ro, domain 0, base addr 0
  118. static uint32_t Sect_Device_RW; //as Sect_Device_RO, but writeable
  119. /* Define global descriptors */
  120. static uint32_t Page_L1_4k = 0x0; //generic
  121. static uint32_t Page_L1_64k = 0x0; //generic
  122. static uint32_t Page_4k_Device_RW; //Shared device, not executable, rw, domain 0
  123. static uint32_t Page_64k_Device_RW; //Shared device, not executable, rw, domain 0
  124. void MMU_CreateTranslationTable(void)
  125. {
  126. mmu_region_attributes_Type region;
  127. //Create 4GB of faulting entries
  128. MMU_TTSection (TTB_BASE, 0, 4096, DESCRIPTOR_FAULT);
  129. /*
  130. * Generate descriptors. Refer to core_ca.h to get information about attributes
  131. *
  132. */
  133. //Create descriptors for Vectors, RO, RW, ZI sections
  134. section_normal(Sect_Normal, region);
  135. section_normal_cod(Sect_Normal_Cod, region);
  136. section_normal_ro(Sect_Normal_RO, region);
  137. section_normal_rw(Sect_Normal_RW, region);
  138. //Create descriptors for peripherals
  139. section_device_ro(Sect_Device_RO, region);
  140. section_device_rw(Sect_Device_RW, region);
  141. //Create descriptors for 64k pages
  142. page64k_device_rw(Page_L1_64k, Page_64k_Device_RW, region);
  143. //Create descriptors for 4k pages
  144. page4k_device_rw(Page_L1_4k, Page_4k_Device_RW, region);
  145. /*
  146. * Define MMU flat-map regions and attributes
  147. *
  148. */
  149. //Define Image
  150. MMU_TTSection (TTB_BASE, __ROM_BASE, __ROM_SIZE/0x100000, Sect_Normal_Cod); // multiple of 1MB sections
  151. MMU_TTSection (TTB_BASE, __RAM_BASE, __RAM_SIZE/0x100000, Sect_Normal_RW); // multiple of 1MB sections
  152. //--------------------- PERIPHERALS -------------------
  153. MMU_TTSection (TTB_BASE, VE_A5_MP_FLASH_BASE0 , 64, Sect_Device_RO); // 64MB NOR
  154. MMU_TTSection (TTB_BASE, VE_A5_MP_FLASH_BASE1 , 64, Sect_Device_RO); // 64MB NOR
  155. MMU_TTSection (TTB_BASE, VE_A5_MP_SRAM_BASE , 32, Sect_Device_RW); // 32MB RAM
  156. MMU_TTSection (TTB_BASE, VE_A5_MP_VRAM_BASE , 32, Sect_Device_RW); // 32MB RAM
  157. MMU_TTSection (TTB_BASE, VE_A5_MP_ETHERNET_BASE , 16, Sect_Device_RW);
  158. MMU_TTSection (TTB_BASE, VE_A5_MP_USB_BASE , 16, Sect_Device_RW);
  159. // Create (16 * 64k)=1MB faulting entries to cover peripheral range 0x1C000000-0x1C00FFFF
  160. MMU_TTPage64k(TTB_BASE, PERIPHERAL_A_FAULT , 16, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, DESCRIPTOR_FAULT);
  161. // Define peripheral range 0x1C000000-0x1C00FFFF
  162. MMU_TTPage64k(TTB_BASE, VE_A5_MP_DAP_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  163. MMU_TTPage64k(TTB_BASE, VE_A5_MP_SYSTEM_REG_BASE, 1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  164. MMU_TTPage64k(TTB_BASE, VE_A5_MP_SERIAL_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  165. MMU_TTPage64k(TTB_BASE, VE_A5_MP_AACI_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  166. MMU_TTPage64k(TTB_BASE, VE_A5_MP_MMCI_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  167. MMU_TTPage64k(TTB_BASE, VE_A5_MP_KMI0_BASE , 2, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  168. MMU_TTPage64k(TTB_BASE, VE_A5_MP_UART_BASE , 4, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  169. MMU_TTPage64k(TTB_BASE, VE_A5_MP_WDT_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_A_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  170. // Create (16 * 64k)=1MB faulting entries to cover peripheral range 0x1C100000-0x1C10FFFF
  171. MMU_TTPage64k(TTB_BASE, PERIPHERAL_B_FAULT , 16, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, DESCRIPTOR_FAULT);
  172. // Define peripheral range 0x1C100000-0x1C10FFFF
  173. MMU_TTPage64k(TTB_BASE, VE_A5_MP_TIMER_BASE , 2, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  174. MMU_TTPage64k(TTB_BASE, VE_A5_MP_DVI_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  175. MMU_TTPage64k(TTB_BASE, VE_A5_MP_RTC_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  176. MMU_TTPage64k(TTB_BASE, VE_A5_MP_UART4_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  177. MMU_TTPage64k(TTB_BASE, VE_A5_MP_CLCD_BASE , 1, Page_L1_64k, (uint32_t *)PERIPHERAL_B_TABLE_L2_BASE_64k, Page_64k_Device_RW);
  178. // Create (256 * 4k)=1MB faulting entries to cover private address space. Needs to be marked as Device memory
  179. MMU_TTPage4k (TTB_BASE, __get_CBAR() ,256, Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, DESCRIPTOR_FAULT);
  180. // Define private address space entry.
  181. MMU_TTPage4k (TTB_BASE, __get_CBAR() , 3, Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, Page_4k_Device_RW);
  182. // Define L2CC entry. Uncomment if PL310 is present
  183. // MMU_TTPage4k (TTB_BASE, VE_A5_MP_PL310_BASE , 1, Page_L1_4k, (uint32_t *)PRIVATE_TABLE_L2_BASE_4k, Page_4k_Device_RW);
  184. // Create (256 * 4k)=1MB faulting entries to synchronization space (Useful if some non-cacheable DMA agent is present in the SoC)
  185. MMU_TTPage4k (TTB_BASE, F_SYNC_BASE , 256, Page_L1_4k, (uint32_t *)SYNC_FLAGS_TABLE_L2_BASE_4k, DESCRIPTOR_FAULT);
  186. // Define synchronization space entry.
  187. MMU_TTPage4k (TTB_BASE, FLAG_SYNC , 1, Page_L1_4k, (uint32_t *)SYNC_FLAGS_TABLE_L2_BASE_4k, Page_4k_Device_RW);
  188. /* Set location of level 1 page table
  189. ; 31:14 - Translation table base addr (31:14-TTBCR.N, TTBCR.N is 0 out of reset)
  190. ; 13:7 - 0x0
  191. ; 6 - IRGN[0] 0x1 (Inner WB WA)
  192. ; 5 - NOS 0x0 (Non-shared)
  193. ; 4:3 - RGN 0x01 (Outer WB WA)
  194. ; 2 - IMP 0x0 (Implementation Defined)
  195. ; 1 - S 0x0 (Non-shared)
  196. ; 0 - IRGN[1] 0x0 (Inner WB WA) */
  197. __set_TTBR0(__TTB_BASE | 0x48);
  198. __ISB();
  199. /* Set up domain access control register
  200. ; We set domain 0 to Client and all other domains to No Access.
  201. ; All translation table entries specify domain 0 */
  202. __set_DACR(1);
  203. __ISB();
  204. }