cache-cp15.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <common.h>
  8. #include <asm/system.h>
  9. #include <asm/cache.h>
  10. #include <linux/compiler.h>
  11. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
  12. DECLARE_GLOBAL_DATA_PTR;
  13. __weak void arm_init_before_mmu(void)
  14. {
  15. }
  16. __weak void arm_init_domains(void)
  17. {
  18. }
  19. static void cp_delay (void)
  20. {
  21. volatile int i;
  22. /* copro seems to need some delay between reading and writing */
  23. for (i = 0; i < 100; i++)
  24. nop();
  25. asm volatile("" : : : "memory");
  26. }
  27. void set_section_dcache(int section, enum dcache_option option)
  28. {
  29. u32 *page_table = (u32 *)gd->arch.tlb_addr;
  30. u32 value;
  31. value = (section << MMU_SECTION_SHIFT) | (3 << 10);
  32. value |= option;
  33. page_table[section] = value;
  34. }
  35. __weak void mmu_page_table_flush(unsigned long start, unsigned long stop)
  36. {
  37. debug("%s: Warning: not implemented\n", __func__);
  38. }
  39. void mmu_set_region_dcache_behaviour(phys_addr_t start, size_t size,
  40. enum dcache_option option)
  41. {
  42. u32 *page_table = (u32 *)gd->arch.tlb_addr;
  43. unsigned long upto, end;
  44. end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
  45. start = start >> MMU_SECTION_SHIFT;
  46. debug("%s: start=%pa, size=%zu, option=%d\n", __func__, &start, size,
  47. option);
  48. for (upto = start; upto < end; upto++)
  49. set_section_dcache(upto, option);
  50. mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
  51. }
  52. __weak void dram_bank_mmu_setup(int bank)
  53. {
  54. bd_t *bd = gd->bd;
  55. int i;
  56. debug("%s: bank: %d\n", __func__, bank);
  57. for (i = bd->bi_dram[bank].start >> 20;
  58. i < (bd->bi_dram[bank].start >> 20) + (bd->bi_dram[bank].size >> 20);
  59. i++) {
  60. #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
  61. set_section_dcache(i, DCACHE_WRITETHROUGH);
  62. #elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
  63. set_section_dcache(i, DCACHE_WRITEALLOC);
  64. #else
  65. set_section_dcache(i, DCACHE_WRITEBACK);
  66. #endif
  67. }
  68. }
  69. /* to activate the MMU we need to set up virtual memory: use 1M areas */
  70. static inline void mmu_setup(void)
  71. {
  72. int i;
  73. u32 reg;
  74. arm_init_before_mmu();
  75. /* Set up an identity-mapping for all 4GB, rw for everyone */
  76. for (i = 0; i < 4096; i++)
  77. set_section_dcache(i, DCACHE_OFF);
  78. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  79. dram_bank_mmu_setup(i);
  80. }
  81. #ifdef CONFIG_CPU_V7
  82. /* Set TTBR0 */
  83. reg = gd->arch.tlb_addr & TTBR0_BASE_ADDR_MASK;
  84. #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
  85. reg |= TTBR0_RGN_WT | TTBR0_IRGN_WT;
  86. #elif defined(CONFIG_SYS_ARM_CACHE_WRITEALLOC)
  87. reg |= TTBR0_RGN_WBWA | TTBR0_IRGN_WBWA;
  88. #else
  89. reg |= TTBR0_RGN_WB | TTBR0_IRGN_WB;
  90. #endif
  91. asm volatile("mcr p15, 0, %0, c2, c0, 0"
  92. : : "r" (reg) : "memory");
  93. #else
  94. /* Copy the page table address to cp15 */
  95. asm volatile("mcr p15, 0, %0, c2, c0, 0"
  96. : : "r" (gd->arch.tlb_addr) : "memory");
  97. #endif
  98. /* Set the access control to all-supervisor */
  99. asm volatile("mcr p15, 0, %0, c3, c0, 0"
  100. : : "r" (~0));
  101. arm_init_domains();
  102. /* and enable the mmu */
  103. reg = get_cr(); /* get control reg. */
  104. cp_delay();
  105. set_cr(reg | CR_M);
  106. }
  107. static int mmu_enabled(void)
  108. {
  109. return get_cr() & CR_M;
  110. }
  111. /* cache_bit must be either CR_I or CR_C */
  112. static void cache_enable(uint32_t cache_bit)
  113. {
  114. uint32_t reg;
  115. /* The data cache is not active unless the mmu is enabled too */
  116. if ((cache_bit == CR_C) && !mmu_enabled())
  117. mmu_setup();
  118. reg = get_cr(); /* get control reg. */
  119. cp_delay();
  120. set_cr(reg | cache_bit);
  121. }
  122. /* cache_bit must be either CR_I or CR_C */
  123. static void cache_disable(uint32_t cache_bit)
  124. {
  125. uint32_t reg;
  126. reg = get_cr();
  127. cp_delay();
  128. if (cache_bit == CR_C) {
  129. /* if cache isn;t enabled no need to disable */
  130. if ((reg & CR_C) != CR_C)
  131. return;
  132. /* if disabling data cache, disable mmu too */
  133. cache_bit |= CR_M;
  134. }
  135. reg = get_cr();
  136. cp_delay();
  137. if (cache_bit == (CR_C | CR_M))
  138. flush_dcache_all();
  139. set_cr(reg & ~cache_bit);
  140. }
  141. #endif
  142. #ifdef CONFIG_SYS_ICACHE_OFF
  143. void icache_enable (void)
  144. {
  145. return;
  146. }
  147. void icache_disable (void)
  148. {
  149. return;
  150. }
  151. int icache_status (void)
  152. {
  153. return 0; /* always off */
  154. }
  155. #else
  156. void icache_enable(void)
  157. {
  158. cache_enable(CR_I);
  159. }
  160. void icache_disable(void)
  161. {
  162. cache_disable(CR_I);
  163. }
  164. int icache_status(void)
  165. {
  166. return (get_cr() & CR_I) != 0;
  167. }
  168. #endif
  169. #ifdef CONFIG_SYS_DCACHE_OFF
  170. void dcache_enable (void)
  171. {
  172. return;
  173. }
  174. void dcache_disable (void)
  175. {
  176. return;
  177. }
  178. int dcache_status (void)
  179. {
  180. return 0; /* always off */
  181. }
  182. #else
  183. void dcache_enable(void)
  184. {
  185. cache_enable(CR_C);
  186. }
  187. void dcache_disable(void)
  188. {
  189. cache_disable(CR_C);
  190. }
  191. int dcache_status(void)
  192. {
  193. return (get_cr() & CR_C) != 0;
  194. }
  195. #endif