cache-cp15.c 4.8 KB

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