cache-cp15.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. #if defined(CONFIG_SYS_ARM_CACHE_WRITETHROUGH)
  27. #define CACHE_SETUP 0x1a
  28. #else
  29. #define CACHE_SETUP 0x1e
  30. #endif
  31. DECLARE_GLOBAL_DATA_PTR;
  32. void __arm_init_before_mmu(void)
  33. {
  34. }
  35. void arm_init_before_mmu(void)
  36. __attribute__((weak, alias("__arm_init_before_mmu")));
  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. static inline void dram_bank_mmu_setup(int bank)
  46. {
  47. u32 *page_table = (u32 *)gd->tlb_addr;
  48. bd_t *bd = gd->bd;
  49. int i;
  50. debug("%s: bank: %d\n", __func__, bank);
  51. for (i = bd->bi_dram[bank].start >> 20;
  52. i < (bd->bi_dram[bank].start + bd->bi_dram[bank].size) >> 20;
  53. i++) {
  54. page_table[i] = i << 20 | (3 << 10) | CACHE_SETUP;
  55. }
  56. }
  57. /* to activate the MMU we need to set up virtual memory: use 1M areas */
  58. static inline void mmu_setup(void)
  59. {
  60. u32 *page_table = (u32 *)gd->tlb_addr;
  61. int i;
  62. u32 reg;
  63. arm_init_before_mmu();
  64. /* Set up an identity-mapping for all 4GB, rw for everyone */
  65. for (i = 0; i < 4096; i++)
  66. page_table[i] = i << 20 | (3 << 10) | 0x12;
  67. for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
  68. dram_bank_mmu_setup(i);
  69. }
  70. /* Copy the page table address to cp15 */
  71. asm volatile("mcr p15, 0, %0, c2, c0, 0"
  72. : : "r" (page_table) : "memory");
  73. /* Set the access control to all-supervisor */
  74. asm volatile("mcr p15, 0, %0, c3, c0, 0"
  75. : : "r" (~0));
  76. /* and enable the mmu */
  77. reg = get_cr(); /* get control reg. */
  78. cp_delay();
  79. set_cr(reg | CR_M);
  80. }
  81. static int mmu_enabled(void)
  82. {
  83. return get_cr() & CR_M;
  84. }
  85. /* cache_bit must be either CR_I or CR_C */
  86. static void cache_enable(uint32_t cache_bit)
  87. {
  88. uint32_t reg;
  89. /* The data cache is not active unless the mmu is enabled too */
  90. if ((cache_bit == CR_C) && !mmu_enabled())
  91. mmu_setup();
  92. reg = get_cr(); /* get control reg. */
  93. cp_delay();
  94. set_cr(reg | cache_bit);
  95. }
  96. /* cache_bit must be either CR_I or CR_C */
  97. static void cache_disable(uint32_t cache_bit)
  98. {
  99. uint32_t reg;
  100. reg = get_cr();
  101. cp_delay();
  102. if (cache_bit == CR_C) {
  103. /* if cache isn;t enabled no need to disable */
  104. if ((reg & CR_C) != CR_C)
  105. return;
  106. /* if disabling data cache, disable mmu too */
  107. cache_bit |= CR_M;
  108. flush_dcache_all();
  109. }
  110. set_cr(reg & ~cache_bit);
  111. }
  112. #endif
  113. #ifdef CONFIG_SYS_ICACHE_OFF
  114. void icache_enable (void)
  115. {
  116. return;
  117. }
  118. void icache_disable (void)
  119. {
  120. return;
  121. }
  122. int icache_status (void)
  123. {
  124. return 0; /* always off */
  125. }
  126. #else
  127. void icache_enable(void)
  128. {
  129. cache_enable(CR_I);
  130. }
  131. void icache_disable(void)
  132. {
  133. cache_disable(CR_I);
  134. }
  135. int icache_status(void)
  136. {
  137. return (get_cr() & CR_I) != 0;
  138. }
  139. #endif
  140. #ifdef CONFIG_SYS_DCACHE_OFF
  141. void dcache_enable (void)
  142. {
  143. return;
  144. }
  145. void dcache_disable (void)
  146. {
  147. return;
  148. }
  149. int dcache_status (void)
  150. {
  151. return 0; /* always off */
  152. }
  153. #else
  154. void dcache_enable(void)
  155. {
  156. cache_enable(CR_C);
  157. }
  158. void dcache_disable(void)
  159. {
  160. cache_disable(CR_C);
  161. }
  162. int dcache_status(void)
  163. {
  164. return (get_cr() & CR_C) != 0;
  165. }
  166. #endif