xref: /openbmc/u-boot/arch/mips/lib/cache_init.S (revision 4baa0ab67d504f3b4318f999631e3f83d0c52c4a)
14a5d8898SPaul Burton/*
24a5d8898SPaul Burton *  Cache-handling routined for MIPS CPUs
34a5d8898SPaul Burton *
44a5d8898SPaul Burton *  Copyright (c) 2003	Wolfgang Denk <wd@denx.de>
54a5d8898SPaul Burton *
64a5d8898SPaul Burton * SPDX-License-Identifier:	GPL-2.0+
74a5d8898SPaul Burton */
84a5d8898SPaul Burton
94a5d8898SPaul Burton#include <asm-offsets.h>
104a5d8898SPaul Burton#include <config.h>
114a5d8898SPaul Burton#include <asm/asm.h>
124a5d8898SPaul Burton#include <asm/regdef.h>
134a5d8898SPaul Burton#include <asm/mipsregs.h>
144a5d8898SPaul Burton#include <asm/addrspace.h>
154a5d8898SPaul Burton#include <asm/cacheops.h>
16*4baa0ab6SPaul Burton#include <asm/cm.h>
174a5d8898SPaul Burton
184a5d8898SPaul Burton#ifndef CONFIG_SYS_MIPS_CACHE_MODE
194a5d8898SPaul Burton#define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
204a5d8898SPaul Burton#endif
214a5d8898SPaul Burton
224a5d8898SPaul Burton#define INDEX_BASE	CKSEG0
234a5d8898SPaul Burton
244a5d8898SPaul Burton	.macro	f_fill64 dst, offset, val
254a5d8898SPaul Burton	LONG_S	\val, (\offset +  0 * LONGSIZE)(\dst)
264a5d8898SPaul Burton	LONG_S	\val, (\offset +  1 * LONGSIZE)(\dst)
274a5d8898SPaul Burton	LONG_S	\val, (\offset +  2 * LONGSIZE)(\dst)
284a5d8898SPaul Burton	LONG_S	\val, (\offset +  3 * LONGSIZE)(\dst)
294a5d8898SPaul Burton	LONG_S	\val, (\offset +  4 * LONGSIZE)(\dst)
304a5d8898SPaul Burton	LONG_S	\val, (\offset +  5 * LONGSIZE)(\dst)
314a5d8898SPaul Burton	LONG_S	\val, (\offset +  6 * LONGSIZE)(\dst)
324a5d8898SPaul Burton	LONG_S	\val, (\offset +  7 * LONGSIZE)(\dst)
334a5d8898SPaul Burton#if LONGSIZE == 4
344a5d8898SPaul Burton	LONG_S	\val, (\offset +  8 * LONGSIZE)(\dst)
354a5d8898SPaul Burton	LONG_S	\val, (\offset +  9 * LONGSIZE)(\dst)
364a5d8898SPaul Burton	LONG_S	\val, (\offset + 10 * LONGSIZE)(\dst)
374a5d8898SPaul Burton	LONG_S	\val, (\offset + 11 * LONGSIZE)(\dst)
384a5d8898SPaul Burton	LONG_S	\val, (\offset + 12 * LONGSIZE)(\dst)
394a5d8898SPaul Burton	LONG_S	\val, (\offset + 13 * LONGSIZE)(\dst)
404a5d8898SPaul Burton	LONG_S	\val, (\offset + 14 * LONGSIZE)(\dst)
414a5d8898SPaul Burton	LONG_S	\val, (\offset + 15 * LONGSIZE)(\dst)
424a5d8898SPaul Burton#endif
434a5d8898SPaul Burton	.endm
444a5d8898SPaul Burton
45ac22fecaSPaul Burton	.macro cache_loop	curr, end, line_sz, op
46ac22fecaSPaul Burton10:	cache		\op, 0(\curr)
47ac22fecaSPaul Burton	PTR_ADDU	\curr, \curr, \line_sz
48ac22fecaSPaul Burton	bne		\curr, \end, 10b
49ac22fecaSPaul Burton	.endm
50ac22fecaSPaul Burton
51536cb7ceSPaul Burton	.macro	l1_info		sz, line_sz, off
52536cb7ceSPaul Burton	.set	push
53536cb7ceSPaul Burton	.set	noat
54536cb7ceSPaul Burton
55536cb7ceSPaul Burton	mfc0	$1, CP0_CONFIG, 1
56536cb7ceSPaul Burton
57536cb7ceSPaul Burton	/* detect line size */
58a3ab2ae7SDaniel Schwierzeck	srl	\line_sz, $1, \off + MIPS_CONF1_DL_SHF - MIPS_CONF1_DA_SHF
59a3ab2ae7SDaniel Schwierzeck	andi	\line_sz, \line_sz, (MIPS_CONF1_DL >> MIPS_CONF1_DL_SHF)
60536cb7ceSPaul Burton	move	\sz, zero
61536cb7ceSPaul Burton	beqz	\line_sz, 10f
62536cb7ceSPaul Burton	li	\sz, 2
63536cb7ceSPaul Burton	sllv	\line_sz, \sz, \line_sz
64536cb7ceSPaul Burton
65536cb7ceSPaul Burton	/* detect associativity */
66a3ab2ae7SDaniel Schwierzeck	srl	\sz, $1, \off + MIPS_CONF1_DA_SHF - MIPS_CONF1_DA_SHF
67a3ab2ae7SDaniel Schwierzeck	andi	\sz, \sz, (MIPS_CONF1_DA >> MIPS_CONF1_DA_SHF)
689f8ac824SPaul Burton	addiu	\sz, \sz, 1
69536cb7ceSPaul Burton
70536cb7ceSPaul Burton	/* sz *= line_sz */
71536cb7ceSPaul Burton	mul	\sz, \sz, \line_sz
72536cb7ceSPaul Burton
73536cb7ceSPaul Burton	/* detect log32(sets) */
74a3ab2ae7SDaniel Schwierzeck	srl	$1, $1, \off + MIPS_CONF1_DS_SHF - MIPS_CONF1_DA_SHF
75a3ab2ae7SDaniel Schwierzeck	andi	$1, $1, (MIPS_CONF1_DS >> MIPS_CONF1_DS_SHF)
76536cb7ceSPaul Burton	addiu	$1, $1, 1
77536cb7ceSPaul Burton	andi	$1, $1, 0x7
78536cb7ceSPaul Burton
79536cb7ceSPaul Burton	/* sz <<= log32(sets) */
80536cb7ceSPaul Burton	sllv	\sz, \sz, $1
81536cb7ceSPaul Burton
82536cb7ceSPaul Burton	/* sz *= 32 */
83536cb7ceSPaul Burton	li	$1, 32
84536cb7ceSPaul Burton	mul	\sz, \sz, $1
85536cb7ceSPaul Burton10:
86536cb7ceSPaul Burton	.set	pop
87536cb7ceSPaul Burton	.endm
884a5d8898SPaul Burton/*
894a5d8898SPaul Burton * mips_cache_reset - low level initialisation of the primary caches
904a5d8898SPaul Burton *
914a5d8898SPaul Burton * This routine initialises the primary caches to ensure that they have good
924a5d8898SPaul Burton * parity.  It must be called by the ROM before any cached locations are used
934a5d8898SPaul Burton * to prevent the possibility of data with bad parity being written to memory.
944a5d8898SPaul Burton *
954a5d8898SPaul Burton * To initialise the instruction cache it is essential that a source of data
964a5d8898SPaul Burton * with good parity is available. This routine will initialise an area of
974a5d8898SPaul Burton * memory starting at location zero to be used as a source of parity.
984a5d8898SPaul Burton *
99*4baa0ab6SPaul Burton * Note that this function does not follow the standard calling convention &
100*4baa0ab6SPaul Burton * may clobber typically callee-saved registers.
101*4baa0ab6SPaul Burton *
1024a5d8898SPaul Burton * RETURNS: N/A
1034a5d8898SPaul Burton *
1044a5d8898SPaul Burton */
105*4baa0ab6SPaul Burton#define R_RETURN	s0
106*4baa0ab6SPaul Burton#define R_IC_SIZE	s1
107*4baa0ab6SPaul Burton#define R_IC_LINE	s2
108*4baa0ab6SPaul Burton#define R_DC_SIZE	s3
109*4baa0ab6SPaul Burton#define R_DC_LINE	s4
110*4baa0ab6SPaul Burton#define R_L2_SIZE	s5
111*4baa0ab6SPaul Burton#define R_L2_LINE	s6
112*4baa0ab6SPaul Burton#define R_L2_BYPASSED	s7
113*4baa0ab6SPaul Burton#define R_L2_L2C	t8
114ca4e833cSPaul BurtonLEAF(mips_cache_reset)
115*4baa0ab6SPaul Burton	move	R_RETURN, ra
116*4baa0ab6SPaul Burton
117*4baa0ab6SPaul Burton#ifdef CONFIG_MIPS_L2_CACHE
118*4baa0ab6SPaul Burton	/*
119*4baa0ab6SPaul Burton	 * For there to be an L2 present, Config2 must be present. If it isn't
120*4baa0ab6SPaul Burton	 * then we proceed knowing there's no L2 cache.
121*4baa0ab6SPaul Burton	 */
122*4baa0ab6SPaul Burton	move	R_L2_SIZE, zero
123*4baa0ab6SPaul Burton	move	R_L2_LINE, zero
124*4baa0ab6SPaul Burton	move	R_L2_BYPASSED, zero
125*4baa0ab6SPaul Burton	move	R_L2_L2C, zero
126*4baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 1
127*4baa0ab6SPaul Burton	bgez	t0, l2_probe_done
128*4baa0ab6SPaul Burton
129*4baa0ab6SPaul Burton	/*
130*4baa0ab6SPaul Burton	 * From MIPSr6 onwards the L2 cache configuration might not be reported
131*4baa0ab6SPaul Burton	 * by Config2. The Config5.L2C bit indicates whether this is the case,
132*4baa0ab6SPaul Burton	 * and if it is then we need knowledge of where else to look. For cores
133*4baa0ab6SPaul Burton	 * from Imagination Technologies this is a CM GCR.
134*4baa0ab6SPaul Burton	 */
135*4baa0ab6SPaul Burton# if __mips_isa_rev >= 6
136*4baa0ab6SPaul Burton	/* Check that Config5 exists */
137*4baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 2
138*4baa0ab6SPaul Burton	bgez	t0, l2_probe_cop0
139*4baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 3
140*4baa0ab6SPaul Burton	bgez	t0, l2_probe_cop0
141*4baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 4
142*4baa0ab6SPaul Burton	bgez	t0, l2_probe_cop0
143*4baa0ab6SPaul Burton
144*4baa0ab6SPaul Burton	/* Check Config5.L2C is set */
145*4baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 5
146*4baa0ab6SPaul Burton	and	R_L2_L2C, t0, MIPS_CONF5_L2C
147*4baa0ab6SPaul Burton	beqz	R_L2_L2C, l2_probe_cop0
148*4baa0ab6SPaul Burton
149*4baa0ab6SPaul Burton	/* Config5.L2C is set */
150*4baa0ab6SPaul Burton#  ifdef CONFIG_MIPS_CM
151*4baa0ab6SPaul Burton	/* The CM will provide L2 configuration */
152*4baa0ab6SPaul Burton	PTR_LI	t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
153*4baa0ab6SPaul Burton	lw	t1, GCR_L2_CONFIG(t0)
154*4baa0ab6SPaul Burton	bgez	t1, l2_probe_done
155*4baa0ab6SPaul Burton
156*4baa0ab6SPaul Burton	ext	R_L2_LINE, t1, \
157*4baa0ab6SPaul Burton		GCR_L2_CONFIG_LINESZ_SHIFT, GCR_L2_CONFIG_LINESZ_BITS
158*4baa0ab6SPaul Burton	beqz	R_L2_LINE, l2_probe_done
159*4baa0ab6SPaul Burton	li	t2, 2
160*4baa0ab6SPaul Burton	sllv	R_L2_LINE, t2, R_L2_LINE
161*4baa0ab6SPaul Burton
162*4baa0ab6SPaul Burton	ext	t2, t1, GCR_L2_CONFIG_ASSOC_SHIFT, GCR_L2_CONFIG_ASSOC_BITS
163*4baa0ab6SPaul Burton	addiu	t2, t2, 1
164*4baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_LINE, t2
165*4baa0ab6SPaul Burton
166*4baa0ab6SPaul Burton	ext	t2, t1, GCR_L2_CONFIG_SETSZ_SHIFT, GCR_L2_CONFIG_SETSZ_BITS
167*4baa0ab6SPaul Burton	sllv	R_L2_SIZE, R_L2_SIZE, t2
168*4baa0ab6SPaul Burton	li	t2, 64
169*4baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_SIZE, t2
170*4baa0ab6SPaul Burton
171*4baa0ab6SPaul Burton	/* Bypass the L2 cache so that we can init the L1s early */
172*4baa0ab6SPaul Burton	or	t1, t1, GCR_L2_CONFIG_BYPASS
173*4baa0ab6SPaul Burton	sw	t1, GCR_L2_CONFIG(t0)
174*4baa0ab6SPaul Burton	sync
175*4baa0ab6SPaul Burton	li	R_L2_BYPASSED, 1
176*4baa0ab6SPaul Burton
177*4baa0ab6SPaul Burton	/* Zero the L2 tag registers */
178*4baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_ADDR(t0)
179*4baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_ADDR_UPPER(t0)
180*4baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_STATE(t0)
181*4baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_STATE_UPPER(t0)
182*4baa0ab6SPaul Burton	sw	zero, GCR_L2_DATA(t0)
183*4baa0ab6SPaul Burton	sw	zero, GCR_L2_DATA_UPPER(t0)
184*4baa0ab6SPaul Burton	sync
185*4baa0ab6SPaul Burton#  else
186*4baa0ab6SPaul Burton	/* We don't know how to retrieve L2 configuration on this system */
187*4baa0ab6SPaul Burton#  endif
188*4baa0ab6SPaul Burton	b	l2_probe_done
189*4baa0ab6SPaul Burton# endif
190*4baa0ab6SPaul Burton
191*4baa0ab6SPaul Burton	/*
192*4baa0ab6SPaul Burton	 * For pre-r6 systems, or r6 systems with Config5.L2C==0, probe the L2
193*4baa0ab6SPaul Burton	 * cache configuration from the cop0 Config2 register.
194*4baa0ab6SPaul Burton	 */
195*4baa0ab6SPaul Burtonl2_probe_cop0:
196*4baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 2
197*4baa0ab6SPaul Burton
198*4baa0ab6SPaul Burton	srl	R_L2_LINE, t0, MIPS_CONF2_SL_SHF
199*4baa0ab6SPaul Burton	andi	R_L2_LINE, R_L2_LINE, MIPS_CONF2_SL >> MIPS_CONF2_SL_SHF
200*4baa0ab6SPaul Burton	beqz	R_L2_LINE, l2_probe_done
201*4baa0ab6SPaul Burton	li	t1, 2
202*4baa0ab6SPaul Burton	sllv	R_L2_LINE, t1, R_L2_LINE
203*4baa0ab6SPaul Burton
204*4baa0ab6SPaul Burton	srl	t1, t0, MIPS_CONF2_SA_SHF
205*4baa0ab6SPaul Burton	andi	t1, t1, MIPS_CONF2_SA >> MIPS_CONF2_SA_SHF
206*4baa0ab6SPaul Burton	addiu	t1, t1, 1
207*4baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_LINE, t1
208*4baa0ab6SPaul Burton
209*4baa0ab6SPaul Burton	srl	t1, t0, MIPS_CONF2_SS_SHF
210*4baa0ab6SPaul Burton	andi	t1, t1, MIPS_CONF2_SS >> MIPS_CONF2_SS_SHF
211*4baa0ab6SPaul Burton	sllv	R_L2_SIZE, R_L2_SIZE, t1
212*4baa0ab6SPaul Burton	li	t1, 64
213*4baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_SIZE, t1
214*4baa0ab6SPaul Burton
215*4baa0ab6SPaul Burton	/* Attempt to bypass the L2 so that we can init the L1s early */
216*4baa0ab6SPaul Burton	or	t0, t0, MIPS_CONF2_L2B
217*4baa0ab6SPaul Burton	mtc0	t0, CP0_CONFIG, 2
218*4baa0ab6SPaul Burton	ehb
219*4baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 2
220*4baa0ab6SPaul Burton	and	R_L2_BYPASSED, t0, MIPS_CONF2_L2B
221*4baa0ab6SPaul Burton
222*4baa0ab6SPaul Burton	/* Zero the L2 tag registers */
223*4baa0ab6SPaul Burton	mtc0	zero, CP0_TAGLO, 4
224*4baa0ab6SPaul Burton	ehb
225*4baa0ab6SPaul Burtonl2_probe_done:
226*4baa0ab6SPaul Burton#endif
227*4baa0ab6SPaul Burton
228ace3be4fSPaul Burton#ifndef CONFIG_SYS_CACHE_SIZE_AUTO
2295c72e5a6SPaul Burton	li	R_IC_SIZE, CONFIG_SYS_ICACHE_SIZE
2305c72e5a6SPaul Burton	li	R_IC_LINE, CONFIG_SYS_ICACHE_LINE_SIZE
2314a5d8898SPaul Burton#else
2325c72e5a6SPaul Burton	l1_info	R_IC_SIZE, R_IC_LINE, MIPS_CONF1_IA_SHF
2334a5d8898SPaul Burton#endif
2344a5d8898SPaul Burton
235ace3be4fSPaul Burton#ifndef CONFIG_SYS_CACHE_SIZE_AUTO
2365c72e5a6SPaul Burton	li	R_DC_SIZE, CONFIG_SYS_DCACHE_SIZE
2375c72e5a6SPaul Burton	li	R_DC_LINE, CONFIG_SYS_DCACHE_LINE_SIZE
2384a5d8898SPaul Burton#else
2395c72e5a6SPaul Burton	l1_info	R_DC_SIZE, R_DC_LINE, MIPS_CONF1_DA_SHF
2404a5d8898SPaul Burton#endif
2414a5d8898SPaul Burton
242dd7c7200SPaul Burton#ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
243dd7c7200SPaul Burton
2444a5d8898SPaul Burton	/* Determine the largest L1 cache size */
245ace3be4fSPaul Burton#ifndef CONFIG_SYS_CACHE_SIZE_AUTO
2464a5d8898SPaul Burton#if CONFIG_SYS_ICACHE_SIZE > CONFIG_SYS_DCACHE_SIZE
2474a5d8898SPaul Burton	li	v0, CONFIG_SYS_ICACHE_SIZE
2484a5d8898SPaul Burton#else
2494a5d8898SPaul Burton	li	v0, CONFIG_SYS_DCACHE_SIZE
2504a5d8898SPaul Burton#endif
2514a5d8898SPaul Burton#else
2525c72e5a6SPaul Burton	move	v0, R_IC_SIZE
2535c72e5a6SPaul Burton	sltu	t1, R_IC_SIZE, R_DC_SIZE
2545c72e5a6SPaul Burton	movn	v0, R_DC_SIZE, t1
2554a5d8898SPaul Burton#endif
2564a5d8898SPaul Burton	/*
2574a5d8898SPaul Burton	 * Now clear that much memory starting from zero.
2584a5d8898SPaul Burton	 */
2594a5d8898SPaul Burton	PTR_LI		a0, CKSEG1
2604a5d8898SPaul Burton	PTR_ADDU	a1, a0, v0
2614a5d8898SPaul Burton2:	PTR_ADDIU	a0, 64
2624a5d8898SPaul Burton	f_fill64	a0, -64, zero
2634a5d8898SPaul Burton	bne		a0, a1, 2b
2644a5d8898SPaul Burton
265dd7c7200SPaul Burton#endif /* CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD */
2664a5d8898SPaul Burton
267*4baa0ab6SPaul Burton#ifdef CONFIG_MIPS_L2_CACHE
268*4baa0ab6SPaul Burton	/*
269*4baa0ab6SPaul Burton	 * If the L2 is bypassed, init the L1 first so that we can execute the
270*4baa0ab6SPaul Burton	 * rest of the cache initialisation using the L1 instruction cache.
271*4baa0ab6SPaul Burton	 */
272*4baa0ab6SPaul Burton	bnez		R_L2_BYPASSED, l1_init
273*4baa0ab6SPaul Burton
274*4baa0ab6SPaul Burtonl2_init:
275*4baa0ab6SPaul Burton	PTR_LI		t0, INDEX_BASE
276*4baa0ab6SPaul Burton	PTR_ADDU	t1, t0, R_L2_SIZE
277*4baa0ab6SPaul Burton1:	cache		INDEX_STORE_TAG_SD, 0(t0)
278*4baa0ab6SPaul Burton	PTR_ADDU	t0, t0, R_L2_LINE
279*4baa0ab6SPaul Burton	bne		t0, t1, 1b
280*4baa0ab6SPaul Burton
281*4baa0ab6SPaul Burton	/*
282*4baa0ab6SPaul Burton	 * If the L2 was bypassed then we already initialised the L1s before
283*4baa0ab6SPaul Burton	 * the L2, so we are now done.
284*4baa0ab6SPaul Burton	 */
285*4baa0ab6SPaul Burton	bnez		R_L2_BYPASSED, l2_unbypass
286*4baa0ab6SPaul Burton#endif
287*4baa0ab6SPaul Burton
2884a5d8898SPaul Burton	/*
2898755d507SPaul Burton	 * The TagLo registers used depend upon the CPU implementation, but the
2908755d507SPaul Burton	 * architecture requires that it is safe for software to write to both
2918755d507SPaul Burton	 * TagLo selects 0 & 2 covering supported cases.
2928755d507SPaul Burton	 */
293*4baa0ab6SPaul Burtonl1_init:
2948755d507SPaul Burton	mtc0		zero, CP0_TAGLO
2958755d507SPaul Burton	mtc0		zero, CP0_TAGLO, 2
2968755d507SPaul Burton
2978755d507SPaul Burton	/*
298dd7c7200SPaul Burton	 * The caches are probably in an indeterminate state, so we force good
299dd7c7200SPaul Burton	 * parity into them by doing an invalidate for each line. If
300dd7c7200SPaul Burton	 * CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD is set then we'll proceed to
301dd7c7200SPaul Burton	 * perform a load/fill & a further invalidate for each line, assuming
302dd7c7200SPaul Burton	 * that the bottom of RAM (having just been cleared) will generate good
303dd7c7200SPaul Burton	 * parity for the cache.
3044a5d8898SPaul Burton	 */
3054a5d8898SPaul Burton
3064a5d8898SPaul Burton	/*
3074a5d8898SPaul Burton	 * Initialize the I-cache first,
3084a5d8898SPaul Burton	 */
3095c72e5a6SPaul Burton	blez		R_IC_SIZE, 1f
310ca4e833cSPaul Burton	PTR_LI		t0, INDEX_BASE
3115c72e5a6SPaul Burton	PTR_ADDU	t1, t0, R_IC_SIZE
312ca4e833cSPaul Burton	/* clear tag to invalidate */
3135c72e5a6SPaul Burton	cache_loop	t0, t1, R_IC_LINE, INDEX_STORE_TAG_I
314dd7c7200SPaul Burton#ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
315ca4e833cSPaul Burton	/* fill once, so data field parity is correct */
316ca4e833cSPaul Burton	PTR_LI		t0, INDEX_BASE
3175c72e5a6SPaul Burton	cache_loop	t0, t1, R_IC_LINE, FILL
318ca4e833cSPaul Burton	/* invalidate again - prudent but not strictly neccessary */
319ca4e833cSPaul Burton	PTR_LI		t0, INDEX_BASE
3205c72e5a6SPaul Burton	cache_loop	t0, t1, R_IC_LINE, INDEX_STORE_TAG_I
321dd7c7200SPaul Burton#endif
3224a5d8898SPaul Burton
32333b5c9b2SPaul Burton	/* Enable use of the I-cache by setting Config.K0 */
32433b5c9b2SPaul Burton	sync
32533b5c9b2SPaul Burton	mfc0		t0, CP0_CONFIG
32633b5c9b2SPaul Burton	li		t1, CONFIG_SYS_MIPS_CACHE_MODE
32733b5c9b2SPaul Burton#if __mips_isa_rev >= 2
32833b5c9b2SPaul Burton	ins		t0, t1, 0, 3
32933b5c9b2SPaul Burton#else
33033b5c9b2SPaul Burton	ori		t0, t0, CONF_CM_CMASK
33133b5c9b2SPaul Burton	xori		t0, t0, CONF_CM_CMASK
33233b5c9b2SPaul Burton	or		t0, t0, t1
33333b5c9b2SPaul Burton#endif
33433b5c9b2SPaul Burton	mtc0		t0, CP0_CONFIG
33533b5c9b2SPaul Burton
3364a5d8898SPaul Burton	/*
3374a5d8898SPaul Burton	 * then initialize D-cache.
3384a5d8898SPaul Burton	 */
3395c72e5a6SPaul Burton1:	blez		R_DC_SIZE, 3f
340ca4e833cSPaul Burton	PTR_LI		t0, INDEX_BASE
3415c72e5a6SPaul Burton	PTR_ADDU	t1, t0, R_DC_SIZE
342ca4e833cSPaul Burton	/* clear all tags */
3435c72e5a6SPaul Burton	cache_loop	t0, t1, R_DC_LINE, INDEX_STORE_TAG_D
344dd7c7200SPaul Burton#ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
345ca4e833cSPaul Burton	/* load from each line (in cached space) */
346ca4e833cSPaul Burton	PTR_LI		t0, INDEX_BASE
347ca4e833cSPaul Burton2:	LONG_L		zero, 0(t0)
3485c72e5a6SPaul Burton	PTR_ADDU	t0, R_DC_LINE
349ca4e833cSPaul Burton	bne		t0, t1, 2b
350ca4e833cSPaul Burton	/* clear all tags */
351ca4e833cSPaul Burton	PTR_LI		t0, INDEX_BASE
3525c72e5a6SPaul Burton	cache_loop	t0, t1, R_DC_LINE, INDEX_STORE_TAG_D
353dd7c7200SPaul Burton#endif
354*4baa0ab6SPaul Burton3:
3554a5d8898SPaul Burton
356*4baa0ab6SPaul Burton#ifdef CONFIG_MIPS_L2_CACHE
357*4baa0ab6SPaul Burton	/* If the L2 isn't bypassed then we're done */
358*4baa0ab6SPaul Burton	beqz		R_L2_BYPASSED, return
359*4baa0ab6SPaul Burton
360*4baa0ab6SPaul Burton	/* The L2 is bypassed - go initialise it */
361*4baa0ab6SPaul Burton	b		l2_init
362*4baa0ab6SPaul Burton
363*4baa0ab6SPaul Burtonl2_unbypass:
364*4baa0ab6SPaul Burton# if __mips_isa_rev >= 6
365*4baa0ab6SPaul Burton	beqz		R_L2_L2C, 1f
366*4baa0ab6SPaul Burton
367*4baa0ab6SPaul Burton	li		t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
368*4baa0ab6SPaul Burton	lw		t1, GCR_L2_CONFIG(t0)
369*4baa0ab6SPaul Burton	xor		t1, t1, GCR_L2_CONFIG_BYPASS
370*4baa0ab6SPaul Burton	sw		t1, GCR_L2_CONFIG(t0)
371*4baa0ab6SPaul Burton	sync
372*4baa0ab6SPaul Burton	ehb
373*4baa0ab6SPaul Burton	b		2f
374*4baa0ab6SPaul Burton# endif
375*4baa0ab6SPaul Burton1:	mfc0		t0, CP0_CONFIG, 2
376*4baa0ab6SPaul Burton	xor		t0, t0, MIPS_CONF2_L2B
377*4baa0ab6SPaul Burton	mtc0		t0, CP0_CONFIG, 2
378*4baa0ab6SPaul Burton	ehb
379*4baa0ab6SPaul Burton
380*4baa0ab6SPaul Burton2:
381*4baa0ab6SPaul Burton#endif
382*4baa0ab6SPaul Burton
383*4baa0ab6SPaul Burtonreturn:
384*4baa0ab6SPaul Burton	jr	ra
3854a5d8898SPaul Burton	END(mips_cache_reset)
3864a5d8898SPaul Burton
3874a5d8898SPaul Burton/*
3884a5d8898SPaul Burton * dcache_status - get cache status
3894a5d8898SPaul Burton *
3904a5d8898SPaul Burton * RETURNS: 0 - cache disabled; 1 - cache enabled
3914a5d8898SPaul Burton *
3924a5d8898SPaul Burton */
3934a5d8898SPaul BurtonLEAF(dcache_status)
3944a5d8898SPaul Burton	mfc0	t0, CP0_CONFIG
3954a5d8898SPaul Burton	li	t1, CONF_CM_UNCACHED
3964a5d8898SPaul Burton	andi	t0, t0, CONF_CM_CMASK
3974a5d8898SPaul Burton	move	v0, zero
3984a5d8898SPaul Burton	beq	t0, t1, 2f
3994a5d8898SPaul Burton	li	v0, 1
4004a5d8898SPaul Burton2:	jr	ra
4014a5d8898SPaul Burton	END(dcache_status)
4024a5d8898SPaul Burton
4034a5d8898SPaul Burton/*
4044a5d8898SPaul Burton * dcache_disable - disable cache
4054a5d8898SPaul Burton *
4064a5d8898SPaul Burton * RETURNS: N/A
4074a5d8898SPaul Burton *
4084a5d8898SPaul Burton */
4094a5d8898SPaul BurtonLEAF(dcache_disable)
4104a5d8898SPaul Burton	mfc0	t0, CP0_CONFIG
4114a5d8898SPaul Burton	li	t1, -8
4124a5d8898SPaul Burton	and	t0, t0, t1
4134a5d8898SPaul Burton	ori	t0, t0, CONF_CM_UNCACHED
4144a5d8898SPaul Burton	mtc0	t0, CP0_CONFIG
4154a5d8898SPaul Burton	jr	ra
4164a5d8898SPaul Burton	END(dcache_disable)
4174a5d8898SPaul Burton
4184a5d8898SPaul Burton/*
4194a5d8898SPaul Burton * dcache_enable - enable cache
4204a5d8898SPaul Burton *
4214a5d8898SPaul Burton * RETURNS: N/A
4224a5d8898SPaul Burton *
4234a5d8898SPaul Burton */
4244a5d8898SPaul BurtonLEAF(dcache_enable)
4254a5d8898SPaul Burton	mfc0	t0, CP0_CONFIG
4264a5d8898SPaul Burton	ori	t0, CONF_CM_CMASK
4274a5d8898SPaul Burton	xori	t0, CONF_CM_CMASK
4284a5d8898SPaul Burton	ori	t0, CONFIG_SYS_MIPS_CACHE_MODE
4294a5d8898SPaul Burton	mtc0	t0, CP0_CONFIG
4304a5d8898SPaul Burton	jr	ra
4314a5d8898SPaul Burton	END(dcache_enable)
432