cache-cp15.c 3.7 KB

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