xref: /openbmc/u-boot/arch/mips/lib/cache_init.S (revision 5ef337a0371e2b2c7905e7e20a38b6bfc80bb708)
183d290c5STom Rini/* SPDX-License-Identifier: GPL-2.0+ */
24a5d8898SPaul Burton/*
34a5d8898SPaul Burton *  Cache-handling routined for MIPS CPUs
44a5d8898SPaul Burton *
54a5d8898SPaul Burton *  Copyright (c) 2003	Wolfgang Denk <wd@denx.de>
64a5d8898SPaul Burton */
74a5d8898SPaul Burton
84a5d8898SPaul Burton#include <asm-offsets.h>
94a5d8898SPaul Burton#include <config.h>
104a5d8898SPaul Burton#include <asm/asm.h>
114a5d8898SPaul Burton#include <asm/regdef.h>
124a5d8898SPaul Burton#include <asm/mipsregs.h>
134a5d8898SPaul Burton#include <asm/addrspace.h>
144a5d8898SPaul Burton#include <asm/cacheops.h>
154baa0ab6SPaul Burton#include <asm/cm.h>
164a5d8898SPaul Burton
174a5d8898SPaul Burton#ifndef CONFIG_SYS_MIPS_CACHE_MODE
184a5d8898SPaul Burton#define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
194a5d8898SPaul Burton#endif
204a5d8898SPaul Burton
214a5d8898SPaul Burton	.macro	f_fill64 dst, offset, val
224a5d8898SPaul Burton	LONG_S	\val, (\offset +  0 * LONGSIZE)(\dst)
234a5d8898SPaul Burton	LONG_S	\val, (\offset +  1 * LONGSIZE)(\dst)
244a5d8898SPaul Burton	LONG_S	\val, (\offset +  2 * LONGSIZE)(\dst)
254a5d8898SPaul Burton	LONG_S	\val, (\offset +  3 * LONGSIZE)(\dst)
264a5d8898SPaul Burton	LONG_S	\val, (\offset +  4 * LONGSIZE)(\dst)
274a5d8898SPaul Burton	LONG_S	\val, (\offset +  5 * LONGSIZE)(\dst)
284a5d8898SPaul Burton	LONG_S	\val, (\offset +  6 * LONGSIZE)(\dst)
294a5d8898SPaul Burton	LONG_S	\val, (\offset +  7 * LONGSIZE)(\dst)
304a5d8898SPaul Burton#if LONGSIZE == 4
314a5d8898SPaul Burton	LONG_S	\val, (\offset +  8 * LONGSIZE)(\dst)
324a5d8898SPaul Burton	LONG_S	\val, (\offset +  9 * LONGSIZE)(\dst)
334a5d8898SPaul Burton	LONG_S	\val, (\offset + 10 * LONGSIZE)(\dst)
344a5d8898SPaul Burton	LONG_S	\val, (\offset + 11 * LONGSIZE)(\dst)
354a5d8898SPaul Burton	LONG_S	\val, (\offset + 12 * LONGSIZE)(\dst)
364a5d8898SPaul Burton	LONG_S	\val, (\offset + 13 * LONGSIZE)(\dst)
374a5d8898SPaul Burton	LONG_S	\val, (\offset + 14 * LONGSIZE)(\dst)
384a5d8898SPaul Burton	LONG_S	\val, (\offset + 15 * LONGSIZE)(\dst)
394a5d8898SPaul Burton#endif
404a5d8898SPaul Burton	.endm
414a5d8898SPaul Burton
42ac22fecaSPaul Burton	.macro cache_loop	curr, end, line_sz, op
43ac22fecaSPaul Burton10:	cache		\op, 0(\curr)
44ac22fecaSPaul Burton	PTR_ADDU	\curr, \curr, \line_sz
45ac22fecaSPaul Burton	bne		\curr, \end, 10b
46ac22fecaSPaul Burton	.endm
47ac22fecaSPaul Burton
48536cb7ceSPaul Burton	.macro	l1_info		sz, line_sz, off
49536cb7ceSPaul Burton	.set	push
50536cb7ceSPaul Burton	.set	noat
51536cb7ceSPaul Burton
52536cb7ceSPaul Burton	mfc0	$1, CP0_CONFIG, 1
53536cb7ceSPaul Burton
54536cb7ceSPaul Burton	/* detect line size */
55a3ab2ae7SDaniel Schwierzeck	srl	\line_sz, $1, \off + MIPS_CONF1_DL_SHF - MIPS_CONF1_DA_SHF
56a3ab2ae7SDaniel Schwierzeck	andi	\line_sz, \line_sz, (MIPS_CONF1_DL >> MIPS_CONF1_DL_SHF)
57536cb7ceSPaul Burton	move	\sz, zero
58536cb7ceSPaul Burton	beqz	\line_sz, 10f
59536cb7ceSPaul Burton	li	\sz, 2
60536cb7ceSPaul Burton	sllv	\line_sz, \sz, \line_sz
61536cb7ceSPaul Burton
62536cb7ceSPaul Burton	/* detect associativity */
63a3ab2ae7SDaniel Schwierzeck	srl	\sz, $1, \off + MIPS_CONF1_DA_SHF - MIPS_CONF1_DA_SHF
64a3ab2ae7SDaniel Schwierzeck	andi	\sz, \sz, (MIPS_CONF1_DA >> MIPS_CONF1_DA_SHF)
659f8ac824SPaul Burton	addiu	\sz, \sz, 1
66536cb7ceSPaul Burton
67536cb7ceSPaul Burton	/* sz *= line_sz */
68536cb7ceSPaul Burton	mul	\sz, \sz, \line_sz
69536cb7ceSPaul Burton
70536cb7ceSPaul Burton	/* detect log32(sets) */
71a3ab2ae7SDaniel Schwierzeck	srl	$1, $1, \off + MIPS_CONF1_DS_SHF - MIPS_CONF1_DA_SHF
72a3ab2ae7SDaniel Schwierzeck	andi	$1, $1, (MIPS_CONF1_DS >> MIPS_CONF1_DS_SHF)
73536cb7ceSPaul Burton	addiu	$1, $1, 1
74536cb7ceSPaul Burton	andi	$1, $1, 0x7
75536cb7ceSPaul Burton
76536cb7ceSPaul Burton	/* sz <<= log32(sets) */
77536cb7ceSPaul Burton	sllv	\sz, \sz, $1
78536cb7ceSPaul Burton
79536cb7ceSPaul Burton	/* sz *= 32 */
80536cb7ceSPaul Burton	li	$1, 32
81536cb7ceSPaul Burton	mul	\sz, \sz, $1
82536cb7ceSPaul Burton10:
83536cb7ceSPaul Burton	.set	pop
84536cb7ceSPaul Burton	.endm
85b8385860SDaniel Schwierzeck
864a5d8898SPaul Burton/*
874a5d8898SPaul Burton * mips_cache_reset - low level initialisation of the primary caches
884a5d8898SPaul Burton *
894a5d8898SPaul Burton * This routine initialises the primary caches to ensure that they have good
904a5d8898SPaul Burton * parity.  It must be called by the ROM before any cached locations are used
914a5d8898SPaul Burton * to prevent the possibility of data with bad parity being written to memory.
924a5d8898SPaul Burton *
934a5d8898SPaul Burton * To initialise the instruction cache it is essential that a source of data
944a5d8898SPaul Burton * with good parity is available. This routine will initialise an area of
954a5d8898SPaul Burton * memory starting at location zero to be used as a source of parity.
964a5d8898SPaul Burton *
974baa0ab6SPaul Burton * Note that this function does not follow the standard calling convention &
984baa0ab6SPaul Burton * may clobber typically callee-saved registers.
994baa0ab6SPaul Burton *
1004a5d8898SPaul Burton * RETURNS: N/A
1014a5d8898SPaul Burton *
1024a5d8898SPaul Burton */
1034baa0ab6SPaul Burton#define R_RETURN	s0
1044baa0ab6SPaul Burton#define R_IC_SIZE	s1
1054baa0ab6SPaul Burton#define R_IC_LINE	s2
1064baa0ab6SPaul Burton#define R_DC_SIZE	s3
1074baa0ab6SPaul Burton#define R_DC_LINE	s4
1084baa0ab6SPaul Burton#define R_L2_SIZE	s5
1094baa0ab6SPaul Burton#define R_L2_LINE	s6
1104baa0ab6SPaul Burton#define R_L2_BYPASSED	s7
1114baa0ab6SPaul Burton#define R_L2_L2C	t8
112ca4e833cSPaul BurtonLEAF(mips_cache_reset)
1134baa0ab6SPaul Burton	move	R_RETURN, ra
1144baa0ab6SPaul Burton
1154baa0ab6SPaul Burton#ifdef CONFIG_MIPS_L2_CACHE
1164baa0ab6SPaul Burton	/*
1174baa0ab6SPaul Burton	 * For there to be an L2 present, Config2 must be present. If it isn't
1184baa0ab6SPaul Burton	 * then we proceed knowing there's no L2 cache.
1194baa0ab6SPaul Burton	 */
1204baa0ab6SPaul Burton	move	R_L2_SIZE, zero
1214baa0ab6SPaul Burton	move	R_L2_LINE, zero
1224baa0ab6SPaul Burton	move	R_L2_BYPASSED, zero
1234baa0ab6SPaul Burton	move	R_L2_L2C, zero
1244baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 1
1254baa0ab6SPaul Burton	bgez	t0, l2_probe_done
1264baa0ab6SPaul Burton
1274baa0ab6SPaul Burton	/*
1284baa0ab6SPaul Burton	 * From MIPSr6 onwards the L2 cache configuration might not be reported
1294baa0ab6SPaul Burton	 * by Config2. The Config5.L2C bit indicates whether this is the case,
1304baa0ab6SPaul Burton	 * and if it is then we need knowledge of where else to look. For cores
1314baa0ab6SPaul Burton	 * from Imagination Technologies this is a CM GCR.
1324baa0ab6SPaul Burton	 */
1334baa0ab6SPaul Burton# if __mips_isa_rev >= 6
1344baa0ab6SPaul Burton	/* Check that Config5 exists */
1354baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 2
1364baa0ab6SPaul Burton	bgez	t0, l2_probe_cop0
1374baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 3
1384baa0ab6SPaul Burton	bgez	t0, l2_probe_cop0
1394baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 4
1404baa0ab6SPaul Burton	bgez	t0, l2_probe_cop0
1414baa0ab6SPaul Burton
1424baa0ab6SPaul Burton	/* Check Config5.L2C is set */
1434baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 5
1444baa0ab6SPaul Burton	and	R_L2_L2C, t0, MIPS_CONF5_L2C
1454baa0ab6SPaul Burton	beqz	R_L2_L2C, l2_probe_cop0
1464baa0ab6SPaul Burton
1474baa0ab6SPaul Burton	/* Config5.L2C is set */
1484baa0ab6SPaul Burton#  ifdef CONFIG_MIPS_CM
1494baa0ab6SPaul Burton	/* The CM will provide L2 configuration */
1504baa0ab6SPaul Burton	PTR_LI	t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
1514baa0ab6SPaul Burton	lw	t1, GCR_L2_CONFIG(t0)
1524baa0ab6SPaul Burton	bgez	t1, l2_probe_done
1534baa0ab6SPaul Burton
1544baa0ab6SPaul Burton	ext	R_L2_LINE, t1, \
1554baa0ab6SPaul Burton		GCR_L2_CONFIG_LINESZ_SHIFT, GCR_L2_CONFIG_LINESZ_BITS
1564baa0ab6SPaul Burton	beqz	R_L2_LINE, l2_probe_done
1574baa0ab6SPaul Burton	li	t2, 2
1584baa0ab6SPaul Burton	sllv	R_L2_LINE, t2, R_L2_LINE
1594baa0ab6SPaul Burton
1604baa0ab6SPaul Burton	ext	t2, t1, GCR_L2_CONFIG_ASSOC_SHIFT, GCR_L2_CONFIG_ASSOC_BITS
1614baa0ab6SPaul Burton	addiu	t2, t2, 1
1624baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_LINE, t2
1634baa0ab6SPaul Burton
1644baa0ab6SPaul Burton	ext	t2, t1, GCR_L2_CONFIG_SETSZ_SHIFT, GCR_L2_CONFIG_SETSZ_BITS
1654baa0ab6SPaul Burton	sllv	R_L2_SIZE, R_L2_SIZE, t2
1664baa0ab6SPaul Burton	li	t2, 64
1674baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_SIZE, t2
1684baa0ab6SPaul Burton
1694baa0ab6SPaul Burton	/* Bypass the L2 cache so that we can init the L1s early */
1704baa0ab6SPaul Burton	or	t1, t1, GCR_L2_CONFIG_BYPASS
1714baa0ab6SPaul Burton	sw	t1, GCR_L2_CONFIG(t0)
1724baa0ab6SPaul Burton	sync
1734baa0ab6SPaul Burton	li	R_L2_BYPASSED, 1
1744baa0ab6SPaul Burton
1754baa0ab6SPaul Burton	/* Zero the L2 tag registers */
1764baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_ADDR(t0)
1774baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_ADDR_UPPER(t0)
1784baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_STATE(t0)
1794baa0ab6SPaul Burton	sw	zero, GCR_L2_TAG_STATE_UPPER(t0)
1804baa0ab6SPaul Burton	sw	zero, GCR_L2_DATA(t0)
1814baa0ab6SPaul Burton	sw	zero, GCR_L2_DATA_UPPER(t0)
1824baa0ab6SPaul Burton	sync
1834baa0ab6SPaul Burton#  else
1844baa0ab6SPaul Burton	/* We don't know how to retrieve L2 configuration on this system */
1854baa0ab6SPaul Burton#  endif
1864baa0ab6SPaul Burton	b	l2_probe_done
1874baa0ab6SPaul Burton# endif
1884baa0ab6SPaul Burton
1894baa0ab6SPaul Burton	/*
1904baa0ab6SPaul Burton	 * For pre-r6 systems, or r6 systems with Config5.L2C==0, probe the L2
1914baa0ab6SPaul Burton	 * cache configuration from the cop0 Config2 register.
1924baa0ab6SPaul Burton	 */
1934baa0ab6SPaul Burtonl2_probe_cop0:
1944baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 2
1954baa0ab6SPaul Burton
1964baa0ab6SPaul Burton	srl	R_L2_LINE, t0, MIPS_CONF2_SL_SHF
1974baa0ab6SPaul Burton	andi	R_L2_LINE, R_L2_LINE, MIPS_CONF2_SL >> MIPS_CONF2_SL_SHF
1984baa0ab6SPaul Burton	beqz	R_L2_LINE, l2_probe_done
1994baa0ab6SPaul Burton	li	t1, 2
2004baa0ab6SPaul Burton	sllv	R_L2_LINE, t1, R_L2_LINE
2014baa0ab6SPaul Burton
2024baa0ab6SPaul Burton	srl	t1, t0, MIPS_CONF2_SA_SHF
2034baa0ab6SPaul Burton	andi	t1, t1, MIPS_CONF2_SA >> MIPS_CONF2_SA_SHF
2044baa0ab6SPaul Burton	addiu	t1, t1, 1
2054baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_LINE, t1
2064baa0ab6SPaul Burton
2074baa0ab6SPaul Burton	srl	t1, t0, MIPS_CONF2_SS_SHF
2084baa0ab6SPaul Burton	andi	t1, t1, MIPS_CONF2_SS >> MIPS_CONF2_SS_SHF
2094baa0ab6SPaul Burton	sllv	R_L2_SIZE, R_L2_SIZE, t1
2104baa0ab6SPaul Burton	li	t1, 64
2114baa0ab6SPaul Burton	mul	R_L2_SIZE, R_L2_SIZE, t1
2124baa0ab6SPaul Burton
2134baa0ab6SPaul Burton	/* Attempt to bypass the L2 so that we can init the L1s early */
2144baa0ab6SPaul Burton	or	t0, t0, MIPS_CONF2_L2B
2154baa0ab6SPaul Burton	mtc0	t0, CP0_CONFIG, 2
2164baa0ab6SPaul Burton	ehb
2174baa0ab6SPaul Burton	mfc0	t0, CP0_CONFIG, 2
2184baa0ab6SPaul Burton	and	R_L2_BYPASSED, t0, MIPS_CONF2_L2B
2194baa0ab6SPaul Burton
2204baa0ab6SPaul Burton	/* Zero the L2 tag registers */
2214baa0ab6SPaul Burton	mtc0	zero, CP0_TAGLO, 4
2224baa0ab6SPaul Burton	ehb
2234baa0ab6SPaul Burtonl2_probe_done:
2244baa0ab6SPaul Burton#endif
2254baa0ab6SPaul Burton
226ace3be4fSPaul Burton#ifndef CONFIG_SYS_CACHE_SIZE_AUTO
2275c72e5a6SPaul Burton	li	R_IC_SIZE, CONFIG_SYS_ICACHE_SIZE
2285c72e5a6SPaul Burton	li	R_IC_LINE, CONFIG_SYS_ICACHE_LINE_SIZE
2294a5d8898SPaul Burton#else
2305c72e5a6SPaul Burton	l1_info	R_IC_SIZE, R_IC_LINE, MIPS_CONF1_IA_SHF
2314a5d8898SPaul Burton#endif
2324a5d8898SPaul Burton
233ace3be4fSPaul Burton#ifndef CONFIG_SYS_CACHE_SIZE_AUTO
2345c72e5a6SPaul Burton	li	R_DC_SIZE, CONFIG_SYS_DCACHE_SIZE
2355c72e5a6SPaul Burton	li	R_DC_LINE, CONFIG_SYS_DCACHE_LINE_SIZE
2364a5d8898SPaul Burton#else
2375c72e5a6SPaul Burton	l1_info	R_DC_SIZE, R_DC_LINE, MIPS_CONF1_DA_SHF
2384a5d8898SPaul Burton#endif
2394a5d8898SPaul Burton
240dd7c7200SPaul Burton#ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
241dd7c7200SPaul Burton
2424a5d8898SPaul Burton	/* Determine the largest L1 cache size */
243ace3be4fSPaul Burton#ifndef CONFIG_SYS_CACHE_SIZE_AUTO
2444a5d8898SPaul Burton#if CONFIG_SYS_ICACHE_SIZE > CONFIG_SYS_DCACHE_SIZE
2454a5d8898SPaul Burton	li	v0, CONFIG_SYS_ICACHE_SIZE
2464a5d8898SPaul Burton#else
2474a5d8898SPaul Burton	li	v0, CONFIG_SYS_DCACHE_SIZE
2484a5d8898SPaul Burton#endif
2494a5d8898SPaul Burton#else
2505c72e5a6SPaul Burton	move	v0, R_IC_SIZE
2515c72e5a6SPaul Burton	sltu	t1, R_IC_SIZE, R_DC_SIZE
2525c72e5a6SPaul Burton	movn	v0, R_DC_SIZE, t1
2534a5d8898SPaul Burton#endif
2544a5d8898SPaul Burton	/*
2554a5d8898SPaul Burton	 * Now clear that much memory starting from zero.
2564a5d8898SPaul Burton	 */
257*5ef337a0SDaniel Schwierzeck	PTR_LI		a0, CKSEG1ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
2584a5d8898SPaul Burton	PTR_ADDU	a1, a0, v0
2594a5d8898SPaul Burton2:	PTR_ADDIU	a0, 64
2604a5d8898SPaul Burton	f_fill64	a0, -64, zero
2614a5d8898SPaul Burton	bne		a0, a1, 2b
2624a5d8898SPaul Burton
263dd7c7200SPaul Burton#endif /* CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD */
2644a5d8898SPaul Burton
2654baa0ab6SPaul Burton#ifdef CONFIG_MIPS_L2_CACHE
2664baa0ab6SPaul Burton	/*
2674baa0ab6SPaul Burton	 * If the L2 is bypassed, init the L1 first so that we can execute the
2684baa0ab6SPaul Burton	 * rest of the cache initialisation using the L1 instruction cache.
2694baa0ab6SPaul Burton	 */
2704baa0ab6SPaul Burton	bnez		R_L2_BYPASSED, l1_init
2714baa0ab6SPaul Burton
2724baa0ab6SPaul Burtonl2_init:
273*5ef337a0SDaniel Schwierzeck	PTR_LI		t0, CKSEG0ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
2744baa0ab6SPaul Burton	PTR_ADDU	t1, t0, R_L2_SIZE
2754baa0ab6SPaul Burton1:	cache		INDEX_STORE_TAG_SD, 0(t0)
2764baa0ab6SPaul Burton	PTR_ADDU	t0, t0, R_L2_LINE
2774baa0ab6SPaul Burton	bne		t0, t1, 1b
2784baa0ab6SPaul Burton
2794baa0ab6SPaul Burton	/*
2804baa0ab6SPaul Burton	 * If the L2 was bypassed then we already initialised the L1s before
2814baa0ab6SPaul Burton	 * the L2, so we are now done.
2824baa0ab6SPaul Burton	 */
2834baa0ab6SPaul Burton	bnez		R_L2_BYPASSED, l2_unbypass
2844baa0ab6SPaul Burton#endif
2854baa0ab6SPaul Burton
2864a5d8898SPaul Burton	/*
2878755d507SPaul Burton	 * The TagLo registers used depend upon the CPU implementation, but the
2888755d507SPaul Burton	 * architecture requires that it is safe for software to write to both
2898755d507SPaul Burton	 * TagLo selects 0 & 2 covering supported cases.
2908755d507SPaul Burton	 */
2914baa0ab6SPaul Burtonl1_init:
2928755d507SPaul Burton	mtc0		zero, CP0_TAGLO
2938755d507SPaul Burton	mtc0		zero, CP0_TAGLO, 2
294d608254bSPaul Burton	ehb
2958755d507SPaul Burton
2968755d507SPaul Burton	/*
297dd7c7200SPaul Burton	 * The caches are probably in an indeterminate state, so we force good
298dd7c7200SPaul Burton	 * parity into them by doing an invalidate for each line. If
299dd7c7200SPaul Burton	 * CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD is set then we'll proceed to
300dd7c7200SPaul Burton	 * perform a load/fill & a further invalidate for each line, assuming
301dd7c7200SPaul Burton	 * that the bottom of RAM (having just been cleared) will generate good
302dd7c7200SPaul Burton	 * parity for the cache.
3034a5d8898SPaul Burton	 */
3044a5d8898SPaul Burton
3054a5d8898SPaul Burton	/*
3064a5d8898SPaul Burton	 * Initialize the I-cache first,
3074a5d8898SPaul Burton	 */
3085c72e5a6SPaul Burton	blez		R_IC_SIZE, 1f
309*5ef337a0SDaniel Schwierzeck	PTR_LI		t0, CKSEG0ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
3105c72e5a6SPaul Burton	PTR_ADDU	t1, t0, R_IC_SIZE
311ca4e833cSPaul Burton	/* clear tag to invalidate */
3125c72e5a6SPaul Burton	cache_loop	t0, t1, R_IC_LINE, INDEX_STORE_TAG_I
313dd7c7200SPaul Burton#ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
314ca4e833cSPaul Burton	/* fill once, so data field parity is correct */
315*5ef337a0SDaniel Schwierzeck	PTR_LI		t0, CKSEG0ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
3165c72e5a6SPaul Burton	cache_loop	t0, t1, R_IC_LINE, FILL
317ca4e833cSPaul Burton	/* invalidate again - prudent but not strictly neccessary */
318*5ef337a0SDaniel Schwierzeck	PTR_LI		t0, CKSEG0ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
3195c72e5a6SPaul Burton	cache_loop	t0, t1, R_IC_LINE, INDEX_STORE_TAG_I
320dd7c7200SPaul Burton#endif
32133b5c9b2SPaul Burton	sync
322b8385860SDaniel Schwierzeck
323b8385860SDaniel Schwierzeck	/*
324b8385860SDaniel Schwierzeck	 * Enable use of the I-cache by setting Config.K0. The code for this
325b8385860SDaniel Schwierzeck	 * must be executed from KSEG1. Jump from KSEG0 to KSEG1 to do this.
326b8385860SDaniel Schwierzeck	 * Jump back to KSEG0 after caches are enabled and insert an
327b8385860SDaniel Schwierzeck	 * instruction hazard barrier.
328b8385860SDaniel Schwierzeck	 */
329b8385860SDaniel Schwierzeck	PTR_LA		t0, change_k0_cca
330b8385860SDaniel Schwierzeck	li		t1, CPHYSADDR(~0)
331b8385860SDaniel Schwierzeck	and		t0, t0, t1
332b8385860SDaniel Schwierzeck	PTR_LI		t1, CKSEG1
33333b5c9b2SPaul Burton	or		t0, t0, t1
334b8385860SDaniel Schwierzeck	li		a0, CONFIG_SYS_MIPS_CACHE_MODE
335b8385860SDaniel Schwierzeck	jalr.hb		t0
33633b5c9b2SPaul Burton
3374a5d8898SPaul Burton	/*
3384a5d8898SPaul Burton	 * then initialize D-cache.
3394a5d8898SPaul Burton	 */
3405c72e5a6SPaul Burton1:	blez		R_DC_SIZE, 3f
341*5ef337a0SDaniel Schwierzeck	PTR_LI		t0, CKSEG0ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
3425c72e5a6SPaul Burton	PTR_ADDU	t1, t0, R_DC_SIZE
343ca4e833cSPaul Burton	/* clear all tags */
3445c72e5a6SPaul Burton	cache_loop	t0, t1, R_DC_LINE, INDEX_STORE_TAG_D
345dd7c7200SPaul Burton#ifdef CONFIG_SYS_MIPS_CACHE_INIT_RAM_LOAD
346ca4e833cSPaul Burton	/* load from each line (in cached space) */
347*5ef337a0SDaniel Schwierzeck	PTR_LI		t0, CKSEG0ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
348ca4e833cSPaul Burton2:	LONG_L		zero, 0(t0)
3495c72e5a6SPaul Burton	PTR_ADDU	t0, R_DC_LINE
350ca4e833cSPaul Burton	bne		t0, t1, 2b
351ca4e833cSPaul Burton	/* clear all tags */
352*5ef337a0SDaniel Schwierzeck	PTR_LI		t0, CKSEG0ADDR(CONFIG_MIPS_CACHE_INDEX_BASE)
3535c72e5a6SPaul Burton	cache_loop	t0, t1, R_DC_LINE, INDEX_STORE_TAG_D
354dd7c7200SPaul Burton#endif
3554baa0ab6SPaul Burton3:
3564a5d8898SPaul Burton
3574baa0ab6SPaul Burton#ifdef CONFIG_MIPS_L2_CACHE
3584baa0ab6SPaul Burton	/* If the L2 isn't bypassed then we're done */
3594baa0ab6SPaul Burton	beqz		R_L2_BYPASSED, return
3604baa0ab6SPaul Burton
3614baa0ab6SPaul Burton	/* The L2 is bypassed - go initialise it */
3624baa0ab6SPaul Burton	b		l2_init
3634baa0ab6SPaul Burton
3644baa0ab6SPaul Burtonl2_unbypass:
3654baa0ab6SPaul Burton# if __mips_isa_rev >= 6
3664baa0ab6SPaul Burton	beqz		R_L2_L2C, 1f
3674baa0ab6SPaul Burton
3684baa0ab6SPaul Burton	li		t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
3694baa0ab6SPaul Burton	lw		t1, GCR_L2_CONFIG(t0)
3704baa0ab6SPaul Burton	xor		t1, t1, GCR_L2_CONFIG_BYPASS
3714baa0ab6SPaul Burton	sw		t1, GCR_L2_CONFIG(t0)
3724baa0ab6SPaul Burton	sync
3734baa0ab6SPaul Burton	ehb
3744baa0ab6SPaul Burton	b		2f
3754baa0ab6SPaul Burton# endif
3764baa0ab6SPaul Burton1:	mfc0		t0, CP0_CONFIG, 2
3774baa0ab6SPaul Burton	xor		t0, t0, MIPS_CONF2_L2B
3784baa0ab6SPaul Burton	mtc0		t0, CP0_CONFIG, 2
3794baa0ab6SPaul Burton	ehb
3804baa0ab6SPaul Burton
3814baa0ab6SPaul Burton2:
3827953354bSPaul Burton# ifdef CONFIG_MIPS_CM
3837953354bSPaul Burton	/* Config3 must exist for a CM to be present */
3847953354bSPaul Burton	mfc0		t0, CP0_CONFIG, 1
3857953354bSPaul Burton	bgez		t0, 2f
3867953354bSPaul Burton	mfc0		t0, CP0_CONFIG, 2
3877953354bSPaul Burton	bgez		t0, 2f
3887953354bSPaul Burton
3897953354bSPaul Burton	/* Check Config3.CMGCR to determine CM presence */
3907953354bSPaul Burton	mfc0		t0, CP0_CONFIG, 3
3917953354bSPaul Burton	and		t0, t0, MIPS_CONF3_CMGCR
3927953354bSPaul Burton	beqz		t0, 2f
3937953354bSPaul Burton
3947953354bSPaul Burton	/* Change Config.K0 to a coherent CCA */
395b8385860SDaniel Schwierzeck	PTR_LA		t0, change_k0_cca
396b8385860SDaniel Schwierzeck	li		a0, CONF_CM_CACHABLE_COW
397b8385860SDaniel Schwierzeck	jalr		t0
3987953354bSPaul Burton
3997953354bSPaul Burton	/*
4007953354bSPaul Burton	 * Join the coherent domain such that the caches of this core are kept
4017953354bSPaul Burton	 * coherent with those of other cores.
4027953354bSPaul Burton	 */
4037953354bSPaul Burton	PTR_LI		t0, CKSEG1ADDR(CONFIG_MIPS_CM_BASE)
4047953354bSPaul Burton	lw		t1, GCR_REV(t0)
4057953354bSPaul Burton	li		t2, GCR_REV_CM3
4067953354bSPaul Burton	li		t3, GCR_Cx_COHERENCE_EN
4077953354bSPaul Burton	bge		t1, t2, 1f
4087953354bSPaul Burton	li		t3, GCR_Cx_COHERENCE_DOM_EN
4097953354bSPaul Burton1:	sw		t3, GCR_Cx_COHERENCE(t0)
4107953354bSPaul Burton	ehb
4117953354bSPaul Burton2:
4127953354bSPaul Burton# endif
4134baa0ab6SPaul Burton#endif
4144baa0ab6SPaul Burton
4154baa0ab6SPaul Burtonreturn:
416639200f6SPaul Burton	/* Ensure all cache operations complete before returning */
417639200f6SPaul Burton	sync
418b8385860SDaniel Schwierzeck	jr	R_RETURN
4194a5d8898SPaul Burton	END(mips_cache_reset)
420b8385860SDaniel Schwierzeck
421b8385860SDaniel SchwierzeckLEAF(change_k0_cca)
422b8385860SDaniel Schwierzeck	mfc0		t0, CP0_CONFIG
423b8385860SDaniel Schwierzeck#if __mips_isa_rev >= 2
424b8385860SDaniel Schwierzeck	ins		t0, a0, 0, 3
425b8385860SDaniel Schwierzeck#else
426b8385860SDaniel Schwierzeck	xor		a0, a0, t0
427b8385860SDaniel Schwierzeck	andi		a0, a0, CONF_CM_CMASK
428b8385860SDaniel Schwierzeck	xor		a0, a0, t0
429b8385860SDaniel Schwierzeck#endif
430b8385860SDaniel Schwierzeck	mtc0		a0, CP0_CONFIG
431b8385860SDaniel Schwierzeck
432b8385860SDaniel Schwierzeck	jr.hb		ra
433b8385860SDaniel Schwierzeck	END(change_k0_cca)
434