cache.S 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifndef CONFIG_SYS_MIPS_CACHE_MODE
  32. #define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
  33. #endif
  34. #define RA t8
  35. /*
  36. * 16kB is the maximum size of instruction and data caches on MIPS 4K,
  37. * 64kB is on 4KE, 24K, 5K, etc. Set bigger size for convenience.
  38. *
  39. * Note that the above size is the maximum size of primary cache. U-Boot
  40. * doesn't have L2 cache support for now.
  41. */
  42. #define MIPS_MAX_CACHE_SIZE 0x10000
  43. #define INDEX_BASE CKSEG0
  44. .macro cache_op op addr
  45. .set push
  46. .set noreorder
  47. .set mips3
  48. cache \op, 0(\addr)
  49. .set pop
  50. .endm
  51. .macro f_fill64 dst, offset, val
  52. LONG_S \val, (\offset + 0 * LONGSIZE)(\dst)
  53. LONG_S \val, (\offset + 1 * LONGSIZE)(\dst)
  54. LONG_S \val, (\offset + 2 * LONGSIZE)(\dst)
  55. LONG_S \val, (\offset + 3 * LONGSIZE)(\dst)
  56. LONG_S \val, (\offset + 4 * LONGSIZE)(\dst)
  57. LONG_S \val, (\offset + 5 * LONGSIZE)(\dst)
  58. LONG_S \val, (\offset + 6 * LONGSIZE)(\dst)
  59. LONG_S \val, (\offset + 7 * LONGSIZE)(\dst)
  60. #if LONGSIZE == 4
  61. LONG_S \val, (\offset + 8 * LONGSIZE)(\dst)
  62. LONG_S \val, (\offset + 9 * LONGSIZE)(\dst)
  63. LONG_S \val, (\offset + 10 * LONGSIZE)(\dst)
  64. LONG_S \val, (\offset + 11 * LONGSIZE)(\dst)
  65. LONG_S \val, (\offset + 12 * LONGSIZE)(\dst)
  66. LONG_S \val, (\offset + 13 * LONGSIZE)(\dst)
  67. LONG_S \val, (\offset + 14 * LONGSIZE)(\dst)
  68. LONG_S \val, (\offset + 15 * LONGSIZE)(\dst)
  69. #endif
  70. .endm
  71. /*
  72. * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz)
  73. */
  74. LEAF(mips_init_icache)
  75. blez a1, 9f
  76. mtc0 zero, CP0_TAGLO
  77. /* clear tag to invalidate */
  78. PTR_LI t0, INDEX_BASE
  79. PTR_ADDU t1, t0, a1
  80. 1: cache_op Index_Store_Tag_I t0
  81. PTR_ADDU t0, a2
  82. bne t0, t1, 1b
  83. /* fill once, so data field parity is correct */
  84. PTR_LI t0, INDEX_BASE
  85. 2: cache_op Fill t0
  86. PTR_ADDU t0, a2
  87. bne t0, t1, 2b
  88. /* invalidate again - prudent but not strictly neccessary */
  89. PTR_LI t0, INDEX_BASE
  90. 1: cache_op Index_Store_Tag_I t0
  91. PTR_ADDU t0, a2
  92. bne t0, t1, 1b
  93. 9: jr ra
  94. END(mips_init_icache)
  95. /*
  96. * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz)
  97. */
  98. LEAF(mips_init_dcache)
  99. blez a1, 9f
  100. mtc0 zero, CP0_TAGLO
  101. /* clear all tags */
  102. PTR_LI t0, INDEX_BASE
  103. PTR_ADDU t1, t0, a1
  104. 1: cache_op Index_Store_Tag_D t0
  105. PTR_ADDU t0, a2
  106. bne t0, t1, 1b
  107. /* load from each line (in cached space) */
  108. PTR_LI t0, INDEX_BASE
  109. 2: LONG_L zero, 0(t0)
  110. PTR_ADDU t0, a2
  111. bne t0, t1, 2b
  112. /* clear all tags */
  113. PTR_LI t0, INDEX_BASE
  114. 1: cache_op Index_Store_Tag_D t0
  115. PTR_ADDU t0, a2
  116. bne t0, t1, 1b
  117. 9: jr ra
  118. END(mips_init_dcache)
  119. /*
  120. * mips_cache_reset - low level initialisation of the primary caches
  121. *
  122. * This routine initialises the primary caches to ensure that they have good
  123. * parity. It must be called by the ROM before any cached locations are used
  124. * to prevent the possibility of data with bad parity being written to memory.
  125. *
  126. * To initialise the instruction cache it is essential that a source of data
  127. * with good parity is available. This routine will initialise an area of
  128. * memory starting at location zero to be used as a source of parity.
  129. *
  130. * RETURNS: N/A
  131. *
  132. */
  133. NESTED(mips_cache_reset, 0, ra)
  134. move RA, ra
  135. li t2, CONFIG_SYS_ICACHE_SIZE
  136. li t3, CONFIG_SYS_DCACHE_SIZE
  137. li t4, CONFIG_SYS_CACHELINE_SIZE
  138. move t5, t4
  139. li v0, MIPS_MAX_CACHE_SIZE
  140. /*
  141. * Now clear that much memory starting from zero.
  142. */
  143. PTR_LI a0, CKSEG1
  144. PTR_ADDU a1, a0, v0
  145. 2: PTR_ADDIU a0, 64
  146. f_fill64 a0, -64, zero
  147. bne a0, a1, 2b
  148. /*
  149. * The caches are probably in an indeterminate state,
  150. * so we force good parity into them by doing an
  151. * invalidate, load/fill, invalidate for each line.
  152. */
  153. /*
  154. * Assume bottom of RAM will generate good parity for the cache.
  155. */
  156. /*
  157. * Initialize the I-cache first,
  158. */
  159. move a1, t2
  160. move a2, t4
  161. PTR_LA t7, mips_init_icache
  162. jalr t7
  163. /*
  164. * then initialize D-cache.
  165. */
  166. move a1, t3
  167. move a2, t5
  168. PTR_LA t7, mips_init_dcache
  169. jalr t7
  170. jr RA
  171. END(mips_cache_reset)
  172. /*
  173. * dcache_status - get cache status
  174. *
  175. * RETURNS: 0 - cache disabled; 1 - cache enabled
  176. *
  177. */
  178. LEAF(dcache_status)
  179. mfc0 t0, CP0_CONFIG
  180. li t1, CONF_CM_UNCACHED
  181. andi t0, t0, CONF_CM_CMASK
  182. move v0, zero
  183. beq t0, t1, 2f
  184. li v0, 1
  185. 2: jr ra
  186. END(dcache_status)
  187. /*
  188. * dcache_disable - disable cache
  189. *
  190. * RETURNS: N/A
  191. *
  192. */
  193. LEAF(dcache_disable)
  194. mfc0 t0, CP0_CONFIG
  195. li t1, -8
  196. and t0, t0, t1
  197. ori t0, t0, CONF_CM_UNCACHED
  198. mtc0 t0, CP0_CONFIG
  199. jr ra
  200. END(dcache_disable)
  201. /*
  202. * dcache_enable - enable cache
  203. *
  204. * RETURNS: N/A
  205. *
  206. */
  207. LEAF(dcache_enable)
  208. mfc0 t0, CP0_CONFIG
  209. ori t0, CONF_CM_CMASK
  210. xori t0, CONF_CM_CMASK
  211. ori t0, CONFIG_SYS_MIPS_CACHE_MODE
  212. mtc0 t0, CP0_CONFIG
  213. jr ra
  214. END(dcache_enable)