cache-cp15.c 3.5 KB

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