cache-cp15.c 4.2 KB

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