wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 1 | /* |
Shinya Kuribayashi | 373b16f | 2008-03-25 21:30:07 +0900 | [diff] [blame] | 2 | * Cache-handling routined for MIPS CPUs |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (c) 2003 Wolfgang Denk <wd@denx.de> |
| 5 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: GPL-2.0+ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Wolfgang Denk | 25ddd1f | 2010-10-26 14:34:52 +0200 | [diff] [blame] | 9 | #include <asm-offsets.h> |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 10 | #include <config.h> |
Shinya Kuribayashi | 2f5d414 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 11 | #include <asm/asm.h> |
wdenk | c021880 | 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 | 979cfea | 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 | |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 21 | #define RA t8 |
| 22 | |
Shinya Kuribayashi | 373b16f | 2008-03-25 21:30:07 +0900 | [diff] [blame] | 23 | /* |
| 24 | * 16kB is the maximum size of instruction and data caches on MIPS 4K, |
| 25 | * 64kB is on 4KE, 24K, 5K, etc. Set bigger size for convenience. |
| 26 | * |
| 27 | * Note that the above size is the maximum size of primary cache. U-Boot |
| 28 | * doesn't have L2 cache support for now. |
| 29 | */ |
| 30 | #define MIPS_MAX_CACHE_SIZE 0x10000 |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 31 | |
Shinya Kuribayashi | 7daf2eb | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 32 | #define INDEX_BASE CKSEG0 |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 33 | |
| 34 | .macro cache_op op addr |
| 35 | .set push |
| 36 | .set noreorder |
| 37 | .set mips3 |
| 38 | cache \op, 0(\addr) |
| 39 | .set pop |
| 40 | .endm |
| 41 | |
Shinya Kuribayashi | 1898840 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 42 | .macro f_fill64 dst, offset, val |
| 43 | LONG_S \val, (\offset + 0 * LONGSIZE)(\dst) |
| 44 | LONG_S \val, (\offset + 1 * LONGSIZE)(\dst) |
| 45 | LONG_S \val, (\offset + 2 * LONGSIZE)(\dst) |
| 46 | LONG_S \val, (\offset + 3 * LONGSIZE)(\dst) |
| 47 | LONG_S \val, (\offset + 4 * LONGSIZE)(\dst) |
| 48 | LONG_S \val, (\offset + 5 * LONGSIZE)(\dst) |
| 49 | LONG_S \val, (\offset + 6 * LONGSIZE)(\dst) |
| 50 | LONG_S \val, (\offset + 7 * LONGSIZE)(\dst) |
| 51 | #if LONGSIZE == 4 |
| 52 | LONG_S \val, (\offset + 8 * LONGSIZE)(\dst) |
| 53 | LONG_S \val, (\offset + 9 * LONGSIZE)(\dst) |
| 54 | LONG_S \val, (\offset + 10 * LONGSIZE)(\dst) |
| 55 | LONG_S \val, (\offset + 11 * LONGSIZE)(\dst) |
| 56 | LONG_S \val, (\offset + 12 * LONGSIZE)(\dst) |
| 57 | LONG_S \val, (\offset + 13 * LONGSIZE)(\dst) |
| 58 | LONG_S \val, (\offset + 14 * LONGSIZE)(\dst) |
| 59 | LONG_S \val, (\offset + 15 * LONGSIZE)(\dst) |
| 60 | #endif |
| 61 | .endm |
| 62 | |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 63 | /* |
| 64 | * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz) |
| 65 | */ |
| 66 | LEAF(mips_init_icache) |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 67 | blez a1, 9f |
| 68 | mtc0 zero, CP0_TAGLO |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 69 | /* clear tag to invalidate */ |
| 70 | PTR_LI t0, INDEX_BASE |
| 71 | PTR_ADDU t1, t0, a1 |
Zhi-zhou Zhang | cb0a6a1 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 72 | 1: cache_op INDEX_STORE_TAG_I t0 |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 73 | PTR_ADDU t0, a2 |
| 74 | bne t0, t1, 1b |
| 75 | /* fill once, so data field parity is correct */ |
| 76 | PTR_LI t0, INDEX_BASE |
Zhi-zhou Zhang | cb0a6a1 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 77 | 2: cache_op FILL t0 |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 78 | PTR_ADDU t0, a2 |
| 79 | bne t0, t1, 2b |
| 80 | /* invalidate again - prudent but not strictly neccessary */ |
| 81 | PTR_LI t0, INDEX_BASE |
Zhi-zhou Zhang | cb0a6a1 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 82 | 1: cache_op INDEX_STORE_TAG_I t0 |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 83 | PTR_ADDU t0, a2 |
| 84 | bne t0, t1, 1b |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 85 | 9: jr ra |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 86 | END(mips_init_icache) |
| 87 | |
| 88 | /* |
| 89 | * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz) |
| 90 | */ |
| 91 | LEAF(mips_init_dcache) |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 92 | blez a1, 9f |
| 93 | mtc0 zero, CP0_TAGLO |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 94 | /* clear all tags */ |
| 95 | PTR_LI t0, INDEX_BASE |
| 96 | PTR_ADDU t1, t0, a1 |
Zhi-zhou Zhang | cb0a6a1 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 97 | 1: cache_op INDEX_STORE_TAG_D t0 |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 98 | PTR_ADDU t0, a2 |
| 99 | bne t0, t1, 1b |
| 100 | /* load from each line (in cached space) */ |
| 101 | PTR_LI t0, INDEX_BASE |
| 102 | 2: LONG_L zero, 0(t0) |
| 103 | PTR_ADDU t0, a2 |
| 104 | bne t0, t1, 2b |
| 105 | /* clear all tags */ |
| 106 | PTR_LI t0, INDEX_BASE |
Zhi-zhou Zhang | cb0a6a1 | 2012-10-16 15:02:08 +0200 | [diff] [blame] | 107 | 1: cache_op INDEX_STORE_TAG_D t0 |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 108 | PTR_ADDU t0, a2 |
| 109 | bne t0, t1, 1b |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 110 | 9: jr ra |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 111 | END(mips_init_dcache) |
| 112 | |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 113 | /* |
| 114 | * mips_cache_reset - low level initialisation of the primary caches |
| 115 | * |
| 116 | * This routine initialises the primary caches to ensure that they have good |
| 117 | * parity. It must be called by the ROM before any cached locations are used |
| 118 | * to prevent the possibility of data with bad parity being written to memory. |
| 119 | * |
| 120 | * To initialise the instruction cache it is essential that a source of data |
| 121 | * with good parity is available. This routine will initialise an area of |
| 122 | * memory starting at location zero to be used as a source of parity. |
| 123 | * |
| 124 | * RETURNS: N/A |
| 125 | * |
| 126 | */ |
Shinya Kuribayashi | 2f5d414 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 127 | NESTED(mips_cache_reset, 0, ra) |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 128 | move RA, ra |
Jean-Christophe PLAGNIOL-VILLARD | 6d0f6bc | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 129 | li t2, CONFIG_SYS_ICACHE_SIZE |
| 130 | li t3, CONFIG_SYS_DCACHE_SIZE |
| 131 | li t4, CONFIG_SYS_CACHELINE_SIZE |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 132 | |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 133 | li v0, MIPS_MAX_CACHE_SIZE |
| 134 | |
Shinya Kuribayashi | 1898840 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 135 | /* |
| 136 | * Now clear that much memory starting from zero. |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 137 | */ |
Shinya Kuribayashi | 7daf2eb | 2008-06-05 22:29:00 +0900 | [diff] [blame] | 138 | PTR_LI a0, CKSEG1 |
Shinya Kuribayashi | 1898840 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 139 | PTR_ADDU a1, a0, v0 |
| 140 | 2: PTR_ADDIU a0, 64 |
| 141 | f_fill64 a0, -64, zero |
| 142 | bne a0, a1, 2b |
wdenk | 8bde7f7 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 143 | |
Shinya Kuribayashi | 03c031d | 2007-10-27 15:27:06 +0900 | [diff] [blame] | 144 | /* |
| 145 | * The caches are probably in an indeterminate state, |
| 146 | * so we force good parity into them by doing an |
| 147 | * invalidate, load/fill, invalidate for each line. |
| 148 | */ |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 149 | |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 150 | /* |
| 151 | * Assume bottom of RAM will generate good parity for the cache. |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 152 | */ |
| 153 | |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 154 | /* |
| 155 | * Initialize the I-cache first, |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 156 | */ |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 157 | move a1, t2 |
| 158 | move a2, t4 |
Shinya Kuribayashi | 49387db | 2008-05-06 13:22:52 +0900 | [diff] [blame] | 159 | PTR_LA t7, mips_init_icache |
| 160 | jalr t7 |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 161 | |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 162 | /* |
| 163 | * then initialize D-cache. |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 164 | */ |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 165 | move a1, t3 |
Gabor Juhos | da84f33 | 2013-06-12 18:02:46 +0200 | [diff] [blame^] | 166 | move a2, t4 |
Shinya Kuribayashi | 49387db | 2008-05-06 13:22:52 +0900 | [diff] [blame] | 167 | PTR_LA t7, mips_init_dcache |
| 168 | jalr t7 |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 169 | |
Shinya Kuribayashi | 2e0e527 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 170 | jr RA |
Shinya Kuribayashi | 2f5d414 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 171 | END(mips_cache_reset) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 172 | |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 173 | /* |
| 174 | * dcache_status - get cache status |
| 175 | * |
| 176 | * RETURNS: 0 - cache disabled; 1 - cache enabled |
| 177 | * |
| 178 | */ |
Shinya Kuribayashi | 2f5d414 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 179 | LEAF(dcache_status) |
Shinya Kuribayashi | d98e348 | 2008-03-25 21:30:07 +0900 | [diff] [blame] | 180 | mfc0 t0, CP0_CONFIG |
| 181 | li t1, CONF_CM_UNCACHED |
| 182 | andi t0, t0, CONF_CM_CMASK |
| 183 | move v0, zero |
| 184 | beq t0, t1, 2f |
| 185 | li v0, 1 |
| 186 | 2: jr ra |
Shinya Kuribayashi | 2f5d414 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 187 | END(dcache_status) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 188 | |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 189 | /* |
| 190 | * dcache_disable - disable cache |
| 191 | * |
| 192 | * RETURNS: N/A |
| 193 | * |
| 194 | */ |
Shinya Kuribayashi | 2f5d414 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 195 | LEAF(dcache_disable) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 196 | mfc0 t0, CP0_CONFIG |
| 197 | li t1, -8 |
| 198 | and t0, t0, t1 |
| 199 | ori t0, t0, CONF_CM_UNCACHED |
Shinya Kuribayashi | 03c031d | 2007-10-27 15:27:06 +0900 | [diff] [blame] | 200 | mtc0 t0, CP0_CONFIG |
Shinya Kuribayashi | 43c5092 | 2008-04-17 23:35:13 +0900 | [diff] [blame] | 201 | jr ra |
Shinya Kuribayashi | 2f5d414 | 2008-03-25 21:30:06 +0900 | [diff] [blame] | 202 | END(dcache_disable) |
wdenk | c021880 | 2003-03-27 12:09:35 +0000 | [diff] [blame] | 203 | |
Shinya Kuribayashi | 7aa1f19 | 2011-05-07 00:18:13 +0900 | [diff] [blame] | 204 | /* |
| 205 | * dcache_enable - enable cache |
| 206 | * |
| 207 | * RETURNS: N/A |
| 208 | * |
| 209 | */ |
Shinya Kuribayashi | ea63895 | 2008-05-03 13:51:28 +0900 | [diff] [blame] | 210 | LEAF(dcache_enable) |
| 211 | mfc0 t0, CP0_CONFIG |
| 212 | ori t0, CONF_CM_CMASK |
| 213 | xori t0, CONF_CM_CMASK |
Daniel Schwierzeck | 979cfea | 2012-04-02 02:57:55 +0000 | [diff] [blame] | 214 | ori t0, CONFIG_SYS_MIPS_CACHE_MODE |
Shinya Kuribayashi | ea63895 | 2008-05-03 13:51:28 +0900 | [diff] [blame] | 215 | mtc0 t0, CP0_CONFIG |
| 216 | jr ra |
| 217 | END(dcache_enable) |