cache-cp15.c 4.2 KB

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