2 * Cache-handling routined for MIPS CPUs
4 * Copyright (c) 2003 Wolfgang Denk <wd@denx.de>
6 * SPDX-License-Identifier: GPL-2.0+
9 #include <asm-offsets.h>
12 #include <asm/regdef.h>
13 #include <asm/mipsregs.h>
14 #include <asm/addrspace.h>
15 #include <asm/cacheops.h>
17 #ifndef CONFIG_SYS_MIPS_CACHE_MODE
18 #define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
23 #define INDEX_BASE CKSEG0
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)
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)
47 * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz)
49 LEAF(mips_init_icache)
52 /* clear tag to invalidate */
55 1: cache INDEX_STORE_TAG_I, 0(t0)
58 /* fill once, so data field parity is correct */
63 /* invalidate again - prudent but not strictly neccessary */
65 1: cache INDEX_STORE_TAG_I, 0(t0)
72 * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz)
74 LEAF(mips_init_dcache)
80 1: cache INDEX_STORE_TAG_D, 0(t0)
83 /* load from each line (in cached space) */
90 1: cache INDEX_STORE_TAG_D, 0(t0)
97 * mips_cache_reset - low level initialisation of the primary caches
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.
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.
110 NESTED(mips_cache_reset, 0, ra)
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
119 #ifdef CONFIG_SYS_CACHELINE_SIZE
120 li t7, CONFIG_SYS_CACHELINE_SIZE
121 li t8, CONFIG_SYS_CACHELINE_SIZE
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)
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)
139 #ifdef CONFIG_SYS_ICACHE_SIZE
140 li t2, CONFIG_SYS_ICACHE_SIZE
142 /* Detect I-cache size. */
143 srl t6, t5, MIPS_CONF1_IS_SHIFT
144 andi t6, t6, (MIPS_CONF1_IS >> MIPS_CONF1_IS_SHIFT)
150 1: /* At this point t4 == I-cache sets. */
152 srl t6, t5, MIPS_CONF1_IA_SHIFT
153 andi t6, t6, (MIPS_CONF1_IA >> MIPS_CONF1_IA_SHIFT)
155 /* At this point t6 == I-cache ways. */
159 #ifdef CONFIG_SYS_DCACHE_SIZE
160 li t3, CONFIG_SYS_DCACHE_SIZE
162 /* Detect D-cache size. */
163 srl t6, t5, MIPS_CONF1_DS_SHIFT
164 andi t6, t6, (MIPS_CONF1_DS >> MIPS_CONF1_DS_SHIFT)
170 1: /* At this point t4 == I-cache sets. */
172 srl t6, t5, MIPS_CONF1_DA_SHIFT
173 andi t6, t6, (MIPS_CONF1_DA >> MIPS_CONF1_DA_SHIFT)
175 /* At this point t6 == I-cache ways. */
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
184 li v0, CONFIG_SYS_DCACHE_SIZE
192 * Now clear that much memory starting from zero.
197 f_fill64 a0, -64, zero
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.
207 * Assume bottom of RAM will generate good parity for the cache.
211 * Initialize the I-cache first,
215 PTR_LA v1, mips_init_icache
219 * then initialize D-cache.
223 PTR_LA v1, mips_init_dcache
227 END(mips_cache_reset)
230 * dcache_status - get cache status
232 * RETURNS: 0 - cache disabled; 1 - cache enabled
237 li t1, CONF_CM_UNCACHED
238 andi t0, t0, CONF_CM_CMASK
246 * dcache_disable - disable cache
255 ori t0, t0, CONF_CM_UNCACHED
261 * dcache_enable - enable cache
268 ori t0, CONF_CM_CMASK
269 xori t0, CONF_CM_CMASK
270 ori t0, CONFIG_SYS_MIPS_CACHE_MODE