cache.S 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Cache-handling routined for MIPS CPUs
  3. *
  4. * Copyright (c) 2003 Wolfgang Denk <wd@denx.de>
  5. *
  6. * See file CREDITS for list of people who contributed to this
  7. * project.
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License as
  11. * published by the Free Software Foundation; either version 2 of
  12. * the License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  22. * MA 02111-1307 USA
  23. */
  24. #include <asm-offsets.h>
  25. #include <config.h>
  26. #include <asm/asm.h>
  27. #include <asm/regdef.h>
  28. #include <asm/mipsregs.h>
  29. #include <asm/addrspace.h>
  30. #include <asm/cacheops.h>
  31. #define RA t8
  32. /*
  33. * 16kB is the maximum size of instruction and data caches on MIPS 4K,
  34. * 64kB is on 4KE, 24K, 5K, etc. Set bigger size for convenience.
  35. *
  36. * Note that the above size is the maximum size of primary cache. U-Boot
  37. * doesn't have L2 cache support for now.
  38. */
  39. #define MIPS_MAX_CACHE_SIZE 0x10000
  40. #define INDEX_BASE CKSEG0
  41. .macro cache_op op addr
  42. .set push
  43. .set noreorder
  44. .set mips3
  45. cache \op, 0(\addr)
  46. .set pop
  47. .endm
  48. .macro f_fill64 dst, offset, val
  49. LONG_S \val, (\offset + 0 * LONGSIZE)(\dst)
  50. LONG_S \val, (\offset + 1 * LONGSIZE)(\dst)
  51. LONG_S \val, (\offset + 2 * LONGSIZE)(\dst)
  52. LONG_S \val, (\offset + 3 * LONGSIZE)(\dst)
  53. LONG_S \val, (\offset + 4 * LONGSIZE)(\dst)
  54. LONG_S \val, (\offset + 5 * LONGSIZE)(\dst)
  55. LONG_S \val, (\offset + 6 * LONGSIZE)(\dst)
  56. LONG_S \val, (\offset + 7 * LONGSIZE)(\dst)
  57. #if LONGSIZE == 4
  58. LONG_S \val, (\offset + 8 * LONGSIZE)(\dst)
  59. LONG_S \val, (\offset + 9 * LONGSIZE)(\dst)
  60. LONG_S \val, (\offset + 10 * LONGSIZE)(\dst)
  61. LONG_S \val, (\offset + 11 * LONGSIZE)(\dst)
  62. LONG_S \val, (\offset + 12 * LONGSIZE)(\dst)
  63. LONG_S \val, (\offset + 13 * LONGSIZE)(\dst)
  64. LONG_S \val, (\offset + 14 * LONGSIZE)(\dst)
  65. LONG_S \val, (\offset + 15 * LONGSIZE)(\dst)
  66. #endif
  67. .endm
  68. /*
  69. * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz)
  70. */
  71. LEAF(mips_init_icache)
  72. blez a1, 9f
  73. mtc0 zero, CP0_TAGLO
  74. /* clear tag to invalidate */
  75. PTR_LI t0, INDEX_BASE
  76. PTR_ADDU t1, t0, a1
  77. 1: cache_op Index_Store_Tag_I t0
  78. PTR_ADDU t0, a2
  79. bne t0, t1, 1b
  80. /* fill once, so data field parity is correct */
  81. PTR_LI t0, INDEX_BASE
  82. 2: cache_op Fill t0
  83. PTR_ADDU t0, a2
  84. bne t0, t1, 2b
  85. /* invalidate again - prudent but not strictly neccessary */
  86. PTR_LI t0, INDEX_BASE
  87. 1: cache_op Index_Store_Tag_I t0
  88. PTR_ADDU t0, a2
  89. bne t0, t1, 1b
  90. 9: jr ra
  91. END(mips_init_icache)
  92. /*
  93. * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz)
  94. */
  95. LEAF(mips_init_dcache)
  96. blez a1, 9f
  97. mtc0 zero, CP0_TAGLO
  98. /* clear all tags */
  99. PTR_LI t0, INDEX_BASE
  100. PTR_ADDU t1, t0, a1
  101. 1: cache_op Index_Store_Tag_D t0
  102. PTR_ADDU t0, a2
  103. bne t0, t1, 1b
  104. /* load from each line (in cached space) */
  105. PTR_LI t0, INDEX_BASE
  106. 2: LONG_L zero, 0(t0)
  107. PTR_ADDU t0, a2
  108. bne t0, t1, 2b
  109. /* clear all tags */
  110. PTR_LI t0, INDEX_BASE
  111. 1: cache_op Index_Store_Tag_D t0
  112. PTR_ADDU t0, a2
  113. bne t0, t1, 1b
  114. 9: jr ra
  115. END(mips_init_dcache)
  116. /*
  117. * mips_cache_reset - low level initialisation of the primary caches
  118. *
  119. * This routine initialises the primary caches to ensure that they have good
  120. * parity. It must be called by the ROM before any cached locations are used
  121. * to prevent the possibility of data with bad parity being written to memory.
  122. *
  123. * To initialise the instruction cache it is essential that a source of data
  124. * with good parity is available. This routine will initialise an area of
  125. * memory starting at location zero to be used as a source of parity.
  126. *
  127. * RETURNS: N/A
  128. *
  129. */
  130. NESTED(mips_cache_reset, 0, ra)
  131. move RA, ra
  132. li t2, CONFIG_SYS_ICACHE_SIZE
  133. li t3, CONFIG_SYS_DCACHE_SIZE
  134. li t4, CONFIG_SYS_CACHELINE_SIZE
  135. move t5, t4
  136. li v0, MIPS_MAX_CACHE_SIZE
  137. /*
  138. * Now clear that much memory starting from zero.
  139. */
  140. PTR_LI a0, CKSEG1
  141. PTR_ADDU a1, a0, v0
  142. 2: PTR_ADDIU a0, 64
  143. f_fill64 a0, -64, zero
  144. bne a0, a1, 2b
  145. /*
  146. * The caches are probably in an indeterminate state,
  147. * so we force good parity into them by doing an
  148. * invalidate, load/fill, invalidate for each line.
  149. */
  150. /*
  151. * Assume bottom of RAM will generate good parity for the cache.
  152. */
  153. /*
  154. * Initialize the I-cache first,
  155. */
  156. move a1, t2
  157. move a2, t4
  158. PTR_LA t7, mips_init_icache
  159. jalr t7
  160. /*
  161. * then initialize D-cache.
  162. */
  163. move a1, t3
  164. move a2, t5
  165. PTR_LA t7, mips_init_dcache
  166. jalr t7
  167. jr RA
  168. END(mips_cache_reset)
  169. /*
  170. * dcache_status - get cache status
  171. *
  172. * RETURNS: 0 - cache disabled; 1 - cache enabled
  173. *
  174. */
  175. LEAF(dcache_status)
  176. mfc0 t0, CP0_CONFIG
  177. li t1, CONF_CM_UNCACHED
  178. andi t0, t0, CONF_CM_CMASK
  179. move v0, zero
  180. beq t0, t1, 2f
  181. li v0, 1
  182. 2: jr ra
  183. END(dcache_status)
  184. /*
  185. * dcache_disable - disable cache
  186. *
  187. * RETURNS: N/A
  188. *
  189. */
  190. LEAF(dcache_disable)
  191. mfc0 t0, CP0_CONFIG
  192. li t1, -8
  193. and t0, t0, t1
  194. ori t0, t0, CONF_CM_UNCACHED
  195. mtc0 t0, CP0_CONFIG
  196. jr ra
  197. END(dcache_disable)
  198. /*
  199. * dcache_enable - enable cache
  200. *
  201. * RETURNS: N/A
  202. *
  203. */
  204. LEAF(dcache_enable)
  205. mfc0 t0, CP0_CONFIG
  206. ori t0, CONF_CM_CMASK
  207. xori t0, CONF_CM_CMASK
  208. ori t0, CONF_CM_CACHABLE_NONCOHERENT
  209. mtc0 t0, CP0_CONFIG
  210. jr ra
  211. END(dcache_enable)