wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 1 | /* |
Shinya Kuribayashi | 396aa80 | 2008-03-25 21:30:07 +0900 | [diff] [blame] | 2 | * Cache-handling routined for MIPS CPUs |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2003 Wolfgang Denk <wd@denx.de> |
| 5 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Wolfgang Denk | 0191e47 | 2010-10-26 14:34:52 +0200 | [diff] [blame] | 9 | #include <asm-offsets.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 10 | #include <config.h> |
Shinya Kuribayashi | 0622212 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 11 | #include <asm/asm.h> |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 12 | #include <asm/regdef.h> |
| 13 | #include <asm/mipsregs.h> |
| 14 | #include <asm/addrspace.h> |
| 15 | #include <asm/cacheops.h> |
| 16 | |
Daniel Schwierzeck | 9adc190 | 2012-04-02 02:57:55 +0000 | [diff] [blame] | 17 | #ifndef CONFIG_SYS_MIPS_CACHE_MODE |
| 18 | #define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT |
| 19 | #endif |
| 20 | |
Gabor Juhos | 187fe81 | 2013-06-13 12:59:34 +0200 | [diff] [blame] | 21 | #define RA t9 |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 22 | |
Shinya Kuribayashi | 6c6b261 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 23 | #define INDEX_BASE CKSEG0 |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 24 | |
Shinya Kuribayashi | 52c27e6 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 25 | .macro f_fill64 dst, offset, val |
| 26 | LONG_S \val, (\offset + 0 * LONGSIZE)(\dst) |
| 27 | LONG_S \val, (\offset + 1 * LONGSIZE)(\dst) |
| 28 | LONG_S \val, (\offset + 2 * LONGSIZE)(\dst) |
| 29 | LONG_S \val, (\offset + 3 * LONGSIZE)(\dst) |
| 30 | LONG_S \val, (\offset + 4 * LONGSIZE)(\dst) |
| 31 | LONG_S \val, (\offset + 5 * LONGSIZE)(\dst) |
| 32 | LONG_S \val, (\offset + 6 * LONGSIZE)(\dst) |
| 33 | LONG_S \val, (\offset + 7 * LONGSIZE)(\dst) |
| 34 | #if LONGSIZE == 4 |
| 35 | LONG_S \val, (\offset + 8 * LONGSIZE)(\dst) |
| 36 | LONG_S \val, (\offset + 9 * LONGSIZE)(\dst) |
| 37 | LONG_S \val, (\offset + 10 * LONGSIZE)(\dst) |
| 38 | LONG_S \val, (\offset + 11 * LONGSIZE)(\dst) |
| 39 | LONG_S \val, (\offset + 12 * LONGSIZE)(\dst) |
| 40 | LONG_S \val, (\offset + 13 * LONGSIZE)(\dst) |
| 41 | LONG_S \val, (\offset + 14 * LONGSIZE)(\dst) |
| 42 | LONG_S \val, (\offset + 15 * LONGSIZE)(\dst) |
| 43 | #endif |
| 44 | .endm |
| 45 | |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 46 | /* |
| 47 | * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz) |
| 48 | */ |
| 49 | LEAF(mips_init_icache) |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 50 | blez a1, 9f |
| 51 | mtc0 zero, CP0_TAGLO |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 52 | /* clear tag to invalidate */ |
| 53 | PTR_LI t0, INDEX_BASE |
| 54 | PTR_ADDU t1, t0, a1 |
Paul Burton | 5429af8 | 2015-01-29 01:27:56 +0000 | [diff] [blame] | 55 | 1: cache INDEX_STORE_TAG_I, 0(t0) |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 56 | PTR_ADDU t0, a2 |
| 57 | bne t0, t1, 1b |
| 58 | /* fill once, so data field parity is correct */ |
| 59 | PTR_LI t0, INDEX_BASE |
Paul Burton | 5429af8 | 2015-01-29 01:27:56 +0000 | [diff] [blame] | 60 | 2: cache FILL, 0(t0) |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 61 | PTR_ADDU t0, a2 |
| 62 | bne t0, t1, 2b |
| 63 | /* invalidate again - prudent but not strictly neccessary */ |
| 64 | PTR_LI t0, INDEX_BASE |
Paul Burton | 5429af8 | 2015-01-29 01:27:56 +0000 | [diff] [blame] | 65 | 1: cache INDEX_STORE_TAG_I, 0(t0) |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 66 | PTR_ADDU t0, a2 |
| 67 | bne t0, t1, 1b |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 68 | 9: jr ra |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 69 | END(mips_init_icache) |
| 70 | |
| 71 | /* |
| 72 | * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz) |
| 73 | */ |
| 74 | LEAF(mips_init_dcache) |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 75 | blez a1, 9f |
| 76 | mtc0 zero, CP0_TAGLO |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 77 | /* clear all tags */ |
| 78 | PTR_LI t0, INDEX_BASE |
| 79 | PTR_ADDU t1, t0, a1 |
Paul Burton | 5429af8 | 2015-01-29 01:27:56 +0000 | [diff] [blame] | 80 | 1: cache INDEX_STORE_TAG_D, 0(t0) |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 81 | PTR_ADDU t0, a2 |
| 82 | bne t0, t1, 1b |
| 83 | /* load from each line (in cached space) */ |
| 84 | PTR_LI t0, INDEX_BASE |
| 85 | 2: LONG_L zero, 0(t0) |
| 86 | PTR_ADDU t0, a2 |
| 87 | bne t0, t1, 2b |
| 88 | /* clear all tags */ |
| 89 | PTR_LI t0, INDEX_BASE |
Paul Burton | 5429af8 | 2015-01-29 01:27:56 +0000 | [diff] [blame] | 90 | 1: cache INDEX_STORE_TAG_D, 0(t0) |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 91 | PTR_ADDU t0, a2 |
| 92 | bne t0, t1, 1b |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 93 | 9: jr ra |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 94 | END(mips_init_dcache) |
| 95 | |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 96 | /* |
| 97 | * mips_cache_reset - low level initialisation of the primary caches |
| 98 | * |
| 99 | * This routine initialises the primary caches to ensure that they have good |
| 100 | * parity. It must be called by the ROM before any cached locations are used |
| 101 | * to prevent the possibility of data with bad parity being written to memory. |
| 102 | * |
| 103 | * To initialise the instruction cache it is essential that a source of data |
| 104 | * with good parity is available. This routine will initialise an area of |
| 105 | * memory starting at location zero to be used as a source of parity. |
| 106 | * |
| 107 | * RETURNS: N/A |
| 108 | * |
| 109 | */ |
Shinya Kuribayashi | 0622212 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 110 | NESTED(mips_cache_reset, 0, ra) |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 111 | move RA, ra |
Paul Burton | f122b5a | 2013-11-08 11:18:42 +0000 | [diff] [blame] | 112 | |
| 113 | #if !defined(CONFIG_SYS_ICACHE_SIZE) || !defined(CONFIG_SYS_DCACHE_SIZE) || \ |
| 114 | !defined(CONFIG_SYS_CACHELINE_SIZE) |
| 115 | /* read Config1 for use below */ |
| 116 | mfc0 t5, CP0_CONFIG, 1 |
| 117 | #endif |
| 118 | |
| 119 | #ifdef CONFIG_SYS_CACHELINE_SIZE |
| 120 | li t7, CONFIG_SYS_CACHELINE_SIZE |
Gabor Juhos | 5b4f4ff | 2013-06-13 12:59:35 +0200 | [diff] [blame] | 121 | li t8, CONFIG_SYS_CACHELINE_SIZE |
Paul Burton | f122b5a | 2013-11-08 11:18:42 +0000 | [diff] [blame] | 122 | #else |
| 123 | /* Detect I-cache line size. */ |
| 124 | srl t8, t5, MIPS_CONF1_IL_SHIFT |
| 125 | andi t8, t8, (MIPS_CONF1_IL >> MIPS_CONF1_IL_SHIFT) |
| 126 | beqz t8, 1f |
| 127 | li t6, 2 |
| 128 | sllv t8, t6, t8 |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 129 | |
Paul Burton | f122b5a | 2013-11-08 11:18:42 +0000 | [diff] [blame] | 130 | 1: /* Detect D-cache line size. */ |
| 131 | srl t7, t5, MIPS_CONF1_DL_SHIFT |
| 132 | andi t7, t7, (MIPS_CONF1_DL >> MIPS_CONF1_DL_SHIFT) |
| 133 | beqz t7, 1f |
| 134 | li t6, 2 |
| 135 | sllv t7, t6, t7 |
| 136 | 1: |
| 137 | #endif |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 138 | |
Paul Burton | f122b5a | 2013-11-08 11:18:42 +0000 | [diff] [blame] | 139 | #ifdef CONFIG_SYS_ICACHE_SIZE |
| 140 | li t2, CONFIG_SYS_ICACHE_SIZE |
| 141 | #else |
| 142 | /* Detect I-cache size. */ |
| 143 | srl t6, t5, MIPS_CONF1_IS_SHIFT |
| 144 | andi t6, t6, (MIPS_CONF1_IS >> MIPS_CONF1_IS_SHIFT) |
| 145 | li t4, 32 |
| 146 | xori t2, t6, 0x7 |
| 147 | beqz t2, 1f |
| 148 | addi t6, t6, 1 |
| 149 | sllv t4, t4, t6 |
| 150 | 1: /* At this point t4 == I-cache sets. */ |
| 151 | mul t2, t4, t8 |
| 152 | srl t6, t5, MIPS_CONF1_IA_SHIFT |
| 153 | andi t6, t6, (MIPS_CONF1_IA >> MIPS_CONF1_IA_SHIFT) |
| 154 | addi t6, t6, 1 |
| 155 | /* At this point t6 == I-cache ways. */ |
| 156 | mul t2, t2, t6 |
| 157 | #endif |
| 158 | |
| 159 | #ifdef CONFIG_SYS_DCACHE_SIZE |
| 160 | li t3, CONFIG_SYS_DCACHE_SIZE |
| 161 | #else |
| 162 | /* Detect D-cache size. */ |
| 163 | srl t6, t5, MIPS_CONF1_DS_SHIFT |
| 164 | andi t6, t6, (MIPS_CONF1_DS >> MIPS_CONF1_DS_SHIFT) |
| 165 | li t4, 32 |
| 166 | xori t3, t6, 0x7 |
| 167 | beqz t3, 1f |
| 168 | addi t6, t6, 1 |
| 169 | sllv t4, t4, t6 |
| 170 | 1: /* At this point t4 == I-cache sets. */ |
| 171 | mul t3, t4, t7 |
| 172 | srl t6, t5, MIPS_CONF1_DA_SHIFT |
| 173 | andi t6, t6, (MIPS_CONF1_DA >> MIPS_CONF1_DA_SHIFT) |
| 174 | addi t6, t6, 1 |
| 175 | /* At this point t6 == I-cache ways. */ |
| 176 | mul t3, t3, t6 |
| 177 | #endif |
| 178 | |
| 179 | /* Determine the largest L1 cache size */ |
| 180 | #if defined(CONFIG_SYS_ICACHE_SIZE) && defined(CONFIG_SYS_DCACHE_SIZE) |
| 181 | #if CONFIG_SYS_ICACHE_SIZE > CONFIG_SYS_DCACHE_SIZE |
| 182 | li v0, CONFIG_SYS_ICACHE_SIZE |
| 183 | #else |
| 184 | li v0, CONFIG_SYS_DCACHE_SIZE |
| 185 | #endif |
| 186 | #else |
| 187 | move v0, t2 |
| 188 | sltu t1, t2, t3 |
| 189 | movn v0, t3, t1 |
| 190 | #endif |
Shinya Kuribayashi | 52c27e6 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 191 | /* |
| 192 | * Now clear that much memory starting from zero. |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 193 | */ |
Shinya Kuribayashi | 6c6b261 | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 194 | PTR_LI a0, CKSEG1 |
Shinya Kuribayashi | 52c27e6 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 195 | PTR_ADDU a1, a0, v0 |
| 196 | 2: PTR_ADDIU a0, 64 |
| 197 | f_fill64 a0, -64, zero |
| 198 | bne a0, a1, 2b |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 199 | |
Shinya Kuribayashi | c7faac5 | 2007-10-27 15:27:06 +0900 | [diff] [blame] | 200 | /* |
| 201 | * The caches are probably in an indeterminate state, |
| 202 | * so we force good parity into them by doing an |
| 203 | * invalidate, load/fill, invalidate for each line. |
| 204 | */ |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 205 | |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 206 | /* |
| 207 | * Assume bottom of RAM will generate good parity for the cache. |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 208 | */ |
| 209 | |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 210 | /* |
| 211 | * Initialize the I-cache first, |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 212 | */ |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 213 | move a1, t2 |
Gabor Juhos | 5b4f4ff | 2013-06-13 12:59:35 +0200 | [diff] [blame] | 214 | move a2, t8 |
Gabor Juhos | 02d51a2 | 2013-06-13 12:59:36 +0200 | [diff] [blame] | 215 | PTR_LA v1, mips_init_icache |
| 216 | jalr v1 |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 217 | |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 218 | /* |
| 219 | * then initialize D-cache. |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 220 | */ |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 221 | move a1, t3 |
Paul Burton | f122b5a | 2013-11-08 11:18:42 +0000 | [diff] [blame] | 222 | move a2, t7 |
Gabor Juhos | 02d51a2 | 2013-06-13 12:59:36 +0200 | [diff] [blame] | 223 | PTR_LA v1, mips_init_dcache |
| 224 | jalr v1 |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 225 | |
Shinya Kuribayashi | 5bb51af | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 226 | jr RA |
Shinya Kuribayashi | 0622212 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 227 | END(mips_cache_reset) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 228 | |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 229 | /* |
| 230 | * dcache_status - get cache status |
| 231 | * |
| 232 | * RETURNS: 0 - cache disabled; 1 - cache enabled |
| 233 | * |
| 234 | */ |
Shinya Kuribayashi | 0622212 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 235 | LEAF(dcache_status) |
Shinya Kuribayashi | 3bdce4f | 2008-03-25 21:30:07 +0900 | [diff] [blame] | 236 | mfc0 t0, CP0_CONFIG |
| 237 | li t1, CONF_CM_UNCACHED |
| 238 | andi t0, t0, CONF_CM_CMASK |
| 239 | move v0, zero |
| 240 | beq t0, t1, 2f |
| 241 | li v0, 1 |
| 242 | 2: jr ra |
Shinya Kuribayashi | 0622212 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 243 | END(dcache_status) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 244 | |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 245 | /* |
| 246 | * dcache_disable - disable cache |
| 247 | * |
| 248 | * RETURNS: N/A |
| 249 | * |
| 250 | */ |
Shinya Kuribayashi | 0622212 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 251 | LEAF(dcache_disable) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 252 | mfc0 t0, CP0_CONFIG |
| 253 | li t1, -8 |
| 254 | and t0, t0, t1 |
| 255 | ori t0, t0, CONF_CM_UNCACHED |
Shinya Kuribayashi | c7faac5 | 2007-10-27 15:27:06 +0900 | [diff] [blame] | 256 | mtc0 t0, CP0_CONFIG |
Shinya Kuribayashi | 9dabea1 | 2008-04-17 23:35:13 +0900 | [diff] [blame] | 257 | jr ra |
Shinya Kuribayashi | 0622212 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 258 | END(dcache_disable) |
wdenk | bb1b826 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 259 | |
Shinya Kuribayashi | 6911d0a | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 260 | /* |
| 261 | * dcache_enable - enable cache |
| 262 | * |
| 263 | * RETURNS: N/A |
| 264 | * |
| 265 | */ |
Shinya Kuribayashi | 4d0e2c9 | 2008-05-03 13:51:28 +0900 | [diff] [blame] | 266 | LEAF(dcache_enable) |
| 267 | mfc0 t0, CP0_CONFIG |
| 268 | ori t0, CONF_CM_CMASK |
| 269 | xori t0, CONF_CM_CMASK |
Daniel Schwierzeck | 9adc190 | 2012-04-02 02:57:55 +0000 | [diff] [blame] | 270 | ori t0, CONFIG_SYS_MIPS_CACHE_MODE |
Shinya Kuribayashi | 4d0e2c9 | 2008-05-03 13:51:28 +0900 | [diff] [blame] | 271 | mtc0 t0, CP0_CONFIG |
| 272 | jr ra |
| 273 | END(dcache_enable) |