cache-cp15.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * (C) Copyright 2002
  3. * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <asm/system.h>
  25. #include <asm/cache.h>
  26. #include <linux/compiler.h>
  27. #if !(defined(CONFIG_SYS_ICACHE_OFF) && defined(CONFIG_SYS_DCACHE_OFF))
  28. DECLARE_GLOBAL_DATA_PTR;
  29. void __arm_init_before_mmu(void)
  30. {
  31. }
  32. void arm_init_before_mmu(void)
  33. __attribute__((weak, alias("__arm_init_before_mmu")));
  34. __weak void arm_init_domains(void)
  35. {
  36. }
  37. static void cp_delay (void)
  38. {
  39. volatile int i;
  40. /* copro seems to need some delay between reading and writing */
  41. for (i = 0; i < 100; i++)
  42. nop();
  43. asm volatile("" : : : "memory");
  44. }
  45. void set_section_dcache(int section, enum dcache_option option)
  46. {
  47. u32 *page_table = (u32 *)gd->arch.tlb_addr;
  48. u32 value;
  49. value = (section << MMU_SECTION_SHIFT) | (3 << 10);
  50. value |= option;
  51. page_table[section] = value;
  52. }
  53. void __mmu_page_table_flush(unsigned long start, unsigned long stop)
  54. {
  55. debug("%s: Warning: not implemented\n", __func__);
  56. }
  57. void mmu_page_table_flush(unsigned long start, unsigned long stop)
  58. __attribute__((weak, alias("__mmu_page_table_flush")));
  59. void mmu_set_region_dcache_behaviour(u32 start, int size,
  60. enum dcache_option option)
  61. {
  62. u32 *page_table = (u32 *)gd->arch.tlb_addr;
  63. u32 upto, end;
  64. end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
  65. start = start >> MMU_SECTION_SHIFT;
  66. debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size,
  67. option);
  68. for (upto = start; upto < end; upto++)
  69. set_section_dcache(upto, option);
  70. mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
  71. }
  72. __weak void dram_bank_mmu_setup(int bank)
  73. {
  74. bd_t *bd = gd->bd;
  75. int i;
  76. debug("%s: bank: %d\n", __func__, bank);
  77. for (i = bd->bi_dram[bank].start >> 20;
  78. i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
  79. i++) {
  80. #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
  81. set_section_dcache(i, DCACHE_WRITETHROUGH);
  82. #else
  83. set_section_dcache(i, DCACHE_WRITEBACK);
  84. #endif
  85. }
  86. }
  87. /* to activate the MMU we need to set up virtual memory: use 1M areas */
  88. static inline void mmu_setup(void)
  89. {
  90. int i;
  91. u32 reg;
  92. arm_init_before_mmu();
  93. /* Set up an identity-mapping for all 4GB, rw for everyone */
  94. for (i = 0; i < 4096; i++)
  95. set_section_dcache(i, DCACHE_OFF);
  96. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  97. dram_bank_mmu_setup(i);
  98. }
  99. /* Copy the page table address to cp15 */
  100. asm volatile("mcr p15, 0, %0, c2, c0, 0"
  101. : : "r" (gd->arch.tlb_addr) : "memory");
  102. /* Set the access control to all-supervisor */
  103. asm volatile("mcr p15, 0, %0, c3, c0, 0"
  104. : : "r" (~0));
  105. arm_init_domains();
  106. /* and enable the mmu */
  107. reg = get_cr(); /* get control reg. */
  108. cp_delay();
  109. set_cr(reg | CR_M);
  110. }
  111. static int mmu_enabled(void)
  112. {
  113. return get_cr() & CR_M;
  114. }
  115. /* cache_bit must be either CR_I or CR_C */
  116. static void cache_enable(uint32_t cache_bit)
  117. {
  118. uint32_t reg;
  119. /* The data cache is not active unless the mmu is enabled too */
  120. if ((cache_bit == CR_C) && !mmu_enabled())
  121. mmu_setup();
  122. reg = get_cr(); /* get control reg. */
  123. cp_delay();
  124. set_cr(reg | cache_bit);
  125. }
  126. /* cache_bit must be either CR_I or CR_C */
  127. static void cache_disable(uint32_t cache_bit)
  128. {
  129. uint32_t reg;
  130. reg = get_cr();
  131. cp_delay();
  132. if (cache_bit == CR_C) {
  133. /* if cache isn;t enabled no need to disable */
  134. if ((reg & CR_C) != CR_C)
  135. return;
  136. /* if disabling data cache, disable mmu too */
  137. cache_bit |= CR_M;
  138. }
  139. reg = get_cr();
  140. cp_delay();
  141. if (cache_bit == (CR_C | CR_M))
  142. flush_dcache_all();
  143. set_cr(reg & ~cache_bit);
  144. }
  145. #endif
  146. #ifdef CONFIG_SYS_ICACHE_OFF
  147. void icache_enable (void)
  148. {
  149. return;
  150. }
  151. void icache_disable (void)
  152. {
  153. return;
  154. }
  155. int icache_status (void)
  156. {
  157. return 0; /* always off */
  158. }
  159. #else
  160. void icache_enable(void)
  161. {
  162. cache_enable(CR_I);
  163. }
  164. void icache_disable(void)
  165. {
  166. cache_disable(CR_I);
  167. }
  168. int icache_status(void)
  169. {
  170. return (get_cr() & CR_I) != 0;
  171. }
  172. #endif
  173. #ifdef CONFIG_SYS_DCACHE_OFF
  174. void dcache_enable (void)
  175. {
  176. return;
  177. }
  178. void dcache_disable (void)
  179. {
  180. return;
  181. }
  182. int dcache_status (void)
  183. {
  184. return 0; /* always off */
  185. }
  186. #else
  187. void dcache_enable(void)
  188. {
  189. cache_enable(CR_C);
  190. }
  191. void dcache_disable(void)
  192. {
  193. cache_disable(CR_C);
  194. }
  195. int dcache_status(void)
  196. {
  197. return (get_cr() & CR_C) != 0;
  198. }
  199. #endif