cache-cp15.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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. static void cp_delay (void)
  35. {
  36. volatile int i;
  37. /* copro seems to need some delay between reading and writing */
  38. for (i = 0; i < 100; i++)
  39. nop();
  40. asm volatile("" : : : "memory");
  41. }
  42. void set_section_dcache(int section, enum dcache_option option)
  43. {
  44. u32 *page_table = (u32 *)gd->arch.tlb_addr;
  45. u32 value;
  46. value = (section << MMU_SECTION_SHIFT) | (3 << 10);
  47. value |= option;
  48. page_table[section] = value;
  49. }
  50. void __mmu_page_table_flush(unsigned long start, unsigned long stop)
  51. {
  52. debug("%s: Warning: not implemented\n", __func__);
  53. }
  54. void mmu_page_table_flush(unsigned long start, unsigned long stop)
  55. __attribute__((weak, alias("__mmu_page_table_flush")));
  56. void mmu_set_region_dcache_behaviour(u32 start, int size,
  57. enum dcache_option option)
  58. {
  59. u32 *page_table = (u32 *)gd->arch.tlb_addr;
  60. u32 upto, end;
  61. end = ALIGN(start + size, MMU_SECTION_SIZE) >> MMU_SECTION_SHIFT;
  62. start = start >> MMU_SECTION_SHIFT;
  63. debug("%s: start=%x, size=%x, option=%d\n", __func__, start, size,
  64. option);
  65. for (upto = start; upto < end; upto++)
  66. set_section_dcache(upto, option);
  67. mmu_page_table_flush((u32)&page_table[start], (u32)&page_table[end]);
  68. }
  69. __weak void dram_bank_mmu_setup(int bank)
  70. {
  71. bd_t *bd = gd->bd;
  72. int i;
  73. debug("%s: bank: %d\n", __func__, bank);
  74. for (i = bd->bi_dram[bank].start >> 20;
  75. i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
  76. i++) {
  77. #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
  78. set_section_dcache(i, DCACHE_WRITETHROUGH);
  79. #else
  80. set_section_dcache(i, DCACHE_WRITEBACK);
  81. #endif
  82. }
  83. }
  84. /* to activate the MMU we need to set up virtual memory: use 1M areas */
  85. static inline void mmu_setup(void)
  86. {
  87. int i;
  88. u32 reg;
  89. arm_init_before_mmu();
  90. /* Set up an identity-mapping for all 4GB, rw for everyone */
  91. for (i = 0; i < 4096; i++)
  92. set_section_dcache(i, DCACHE_OFF);
  93. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  94. dram_bank_mmu_setup(i);
  95. }
  96. /* Copy the page table address to cp15 */
  97. asm volatile("mcr p15, 0, %0, c2, c0, 0"
  98. : : "r" (gd->arch.tlb_addr) : "memory");
  99. /* Set the access control to all-supervisor */
  100. asm volatile("mcr p15, 0, %0, c3, c0, 0"
  101. : : "r" (~0));
  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