cache-cp15.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. /* Copy the page table address to cp15 */
  82. asm volatile("mcr p15, 0, %0, c2, c0, 0"
  83. : : "r" (gd->arch.tlb_addr) : "memory");
  84. /* Set the access control to all-supervisor */
  85. asm volatile("mcr p15, 0, %0, c3, c0, 0"
  86. : : "r" (~0));
  87. arm_init_domains();
  88. /* and enable the mmu */
  89. reg = get_cr(); /* get control reg. */
  90. cp_delay();
  91. set_cr(reg | CR_M);
  92. }
  93. static int mmu_enabled(void)
  94. {
  95. return get_cr() & CR_M;
  96. }
  97. /* cache_bit must be either CR_I or CR_C */
  98. static void cache_enable(uint32_t cache_bit)
  99. {
  100. uint32_t reg;
  101. /* The data cache is not active unless the mmu is enabled too */
  102. if ((cache_bit == CR_C) && !mmu_enabled())
  103. mmu_setup();
  104. reg = get_cr(); /* get control reg. */
  105. cp_delay();
  106. set_cr(reg | cache_bit);
  107. }
  108. /* cache_bit must be either CR_I or CR_C */
  109. static void cache_disable(uint32_t cache_bit)
  110. {
  111. uint32_t reg;
  112. reg = get_cr();
  113. cp_delay();
  114. if (cache_bit == CR_C) {
  115. /* if cache isn;t enabled no need to disable */
  116. if ((reg & CR_C) != CR_C)
  117. return;
  118. /* if disabling data cache, disable mmu too */
  119. cache_bit |= CR_M;
  120. }
  121. reg = get_cr();
  122. cp_delay();
  123. if (cache_bit == (CR_C | CR_M))
  124. flush_dcache_all();
  125. set_cr(reg & ~cache_bit);
  126. }
  127. #endif
  128. #ifdef CONFIG_SYS_ICACHE_OFF
  129. void icache_enable (void)
  130. {
  131. return;
  132. }
  133. void icache_disable (void)
  134. {
  135. return;
  136. }
  137. int icache_status (void)
  138. {
  139. return 0; /* always off */
  140. }
  141. #else
  142. void icache_enable(void)
  143. {
  144. cache_enable(CR_I);
  145. }
  146. void icache_disable(void)
  147. {
  148. cache_disable(CR_I);
  149. }
  150. int icache_status(void)
  151. {
  152. return (get_cr() & CR_I) != 0;
  153. }
  154. #endif
  155. #ifdef CONFIG_SYS_DCACHE_OFF
  156. void dcache_enable (void)
  157. {
  158. return;
  159. }
  160. void dcache_disable (void)
  161. {
  162. return;
  163. }
  164. int dcache_status (void)
  165. {
  166. return 0; /* always off */
  167. }
  168. #else
  169. void dcache_enable(void)
  170. {
  171. cache_enable(CR_C);
  172. }
  173. void dcache_disable(void)
  174. {
  175. cache_disable(CR_C);
  176. }
  177. int dcache_status(void)
  178. {
  179. return (get_cr() & CR_C) != 0;
  180. }
  181. #endif