blob: 22bd844eae750610e2b1367e6bb581db9e17902a [file] [log] [blame]
wdenkc0218802003-03-27 12:09:35 +00001/*
Shinya Kuribayashi373b16f2008-03-25 21:30:07 +09002 * Cache-handling routined for MIPS CPUs
wdenkc0218802003-03-27 12:09:35 +00003 *
4 * Copyright (c) 2003 Wolfgang Denk <wd@denx.de>
5 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkc0218802003-03-27 12:09:35 +00007 */
8
Wolfgang Denk25ddd1f2010-10-26 14:34:52 +02009#include <asm-offsets.h>
wdenkc0218802003-03-27 12:09:35 +000010#include <config.h>
Shinya Kuribayashi2f5d4142008-03-25 21:30:06 +090011#include <asm/asm.h>
wdenkc0218802003-03-27 12:09:35 +000012#include <asm/regdef.h>
13#include <asm/mipsregs.h>
14#include <asm/addrspace.h>
15#include <asm/cacheops.h>
16
Daniel Schwierzeck979cfea2012-04-02 02:57:55 +000017#ifndef CONFIG_SYS_MIPS_CACHE_MODE
18#define CONFIG_SYS_MIPS_CACHE_MODE CONF_CM_CACHABLE_NONCOHERENT
19#endif
20
Gabor Juhosc3259162013-06-13 12:59:34 +020021#define RA t9
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090022
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +090023#define INDEX_BASE CKSEG0
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090024
25 .macro cache_op op addr
26 .set push
27 .set noreorder
28 .set mips3
29 cache \op, 0(\addr)
30 .set pop
31 .endm
32
Shinya Kuribayashi18988402008-03-25 21:30:06 +090033 .macro f_fill64 dst, offset, val
34 LONG_S \val, (\offset + 0 * LONGSIZE)(\dst)
35 LONG_S \val, (\offset + 1 * LONGSIZE)(\dst)
36 LONG_S \val, (\offset + 2 * LONGSIZE)(\dst)
37 LONG_S \val, (\offset + 3 * LONGSIZE)(\dst)
38 LONG_S \val, (\offset + 4 * LONGSIZE)(\dst)
39 LONG_S \val, (\offset + 5 * LONGSIZE)(\dst)
40 LONG_S \val, (\offset + 6 * LONGSIZE)(\dst)
41 LONG_S \val, (\offset + 7 * LONGSIZE)(\dst)
42#if LONGSIZE == 4
43 LONG_S \val, (\offset + 8 * LONGSIZE)(\dst)
44 LONG_S \val, (\offset + 9 * LONGSIZE)(\dst)
45 LONG_S \val, (\offset + 10 * LONGSIZE)(\dst)
46 LONG_S \val, (\offset + 11 * LONGSIZE)(\dst)
47 LONG_S \val, (\offset + 12 * LONGSIZE)(\dst)
48 LONG_S \val, (\offset + 13 * LONGSIZE)(\dst)
49 LONG_S \val, (\offset + 14 * LONGSIZE)(\dst)
50 LONG_S \val, (\offset + 15 * LONGSIZE)(\dst)
51#endif
52 .endm
53
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090054/*
55 * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz)
56 */
57LEAF(mips_init_icache)
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +090058 blez a1, 9f
59 mtc0 zero, CP0_TAGLO
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090060 /* clear tag to invalidate */
61 PTR_LI t0, INDEX_BASE
62 PTR_ADDU t1, t0, a1
Zhi-zhou Zhangcb0a6a12012-10-16 15:02:08 +0200631: cache_op INDEX_STORE_TAG_I t0
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090064 PTR_ADDU t0, a2
65 bne t0, t1, 1b
66 /* fill once, so data field parity is correct */
67 PTR_LI t0, INDEX_BASE
Zhi-zhou Zhangcb0a6a12012-10-16 15:02:08 +0200682: cache_op FILL t0
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090069 PTR_ADDU t0, a2
70 bne t0, t1, 2b
71 /* invalidate again - prudent but not strictly neccessary */
72 PTR_LI t0, INDEX_BASE
Zhi-zhou Zhangcb0a6a12012-10-16 15:02:08 +0200731: cache_op INDEX_STORE_TAG_I t0
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090074 PTR_ADDU t0, a2
75 bne t0, t1, 1b
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900769: jr ra
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090077 END(mips_init_icache)
78
79/*
80 * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz)
81 */
82LEAF(mips_init_dcache)
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +090083 blez a1, 9f
84 mtc0 zero, CP0_TAGLO
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090085 /* clear all tags */
86 PTR_LI t0, INDEX_BASE
87 PTR_ADDU t1, t0, a1
Zhi-zhou Zhangcb0a6a12012-10-16 15:02:08 +0200881: cache_op INDEX_STORE_TAG_D t0
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090089 PTR_ADDU t0, a2
90 bne t0, t1, 1b
91 /* load from each line (in cached space) */
92 PTR_LI t0, INDEX_BASE
932: LONG_L zero, 0(t0)
94 PTR_ADDU t0, a2
95 bne t0, t1, 2b
96 /* clear all tags */
97 PTR_LI t0, INDEX_BASE
Zhi-zhou Zhangcb0a6a12012-10-16 15:02:08 +0200981: cache_op INDEX_STORE_TAG_D t0
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +090099 PTR_ADDU t0, a2
100 bne t0, t1, 1b
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +09001019: jr ra
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900102 END(mips_init_dcache)
103
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900104/*
105 * mips_cache_reset - low level initialisation of the primary caches
106 *
107 * This routine initialises the primary caches to ensure that they have good
108 * parity. It must be called by the ROM before any cached locations are used
109 * to prevent the possibility of data with bad parity being written to memory.
110 *
111 * To initialise the instruction cache it is essential that a source of data
112 * with good parity is available. This routine will initialise an area of
113 * memory starting at location zero to be used as a source of parity.
114 *
115 * RETURNS: N/A
116 *
117 */
Shinya Kuribayashi2f5d4142008-03-25 21:30:06 +0900118NESTED(mips_cache_reset, 0, ra)
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900119 move RA, ra
Paul Burtonfa476f72013-11-08 11:18:42 +0000120
121#if !defined(CONFIG_SYS_ICACHE_SIZE) || !defined(CONFIG_SYS_DCACHE_SIZE) || \
122 !defined(CONFIG_SYS_CACHELINE_SIZE)
123 /* read Config1 for use below */
124 mfc0 t5, CP0_CONFIG, 1
125#endif
126
127#ifdef CONFIG_SYS_CACHELINE_SIZE
128 li t7, CONFIG_SYS_CACHELINE_SIZE
Gabor Juhosee8b1e22013-06-13 12:59:35 +0200129 li t8, CONFIG_SYS_CACHELINE_SIZE
Paul Burtonfa476f72013-11-08 11:18:42 +0000130#else
131 /* Detect I-cache line size. */
132 srl t8, t5, MIPS_CONF1_IL_SHIFT
133 andi t8, t8, (MIPS_CONF1_IL >> MIPS_CONF1_IL_SHIFT)
134 beqz t8, 1f
135 li t6, 2
136 sllv t8, t6, t8
wdenkc0218802003-03-27 12:09:35 +0000137
Paul Burtonfa476f72013-11-08 11:18:42 +00001381: /* Detect D-cache line size. */
139 srl t7, t5, MIPS_CONF1_DL_SHIFT
140 andi t7, t7, (MIPS_CONF1_DL >> MIPS_CONF1_DL_SHIFT)
141 beqz t7, 1f
142 li t6, 2
143 sllv t7, t6, t7
1441:
145#endif
wdenkc0218802003-03-27 12:09:35 +0000146
Paul Burtonfa476f72013-11-08 11:18:42 +0000147#ifdef CONFIG_SYS_ICACHE_SIZE
148 li t2, CONFIG_SYS_ICACHE_SIZE
149#else
150 /* Detect I-cache size. */
151 srl t6, t5, MIPS_CONF1_IS_SHIFT
152 andi t6, t6, (MIPS_CONF1_IS >> MIPS_CONF1_IS_SHIFT)
153 li t4, 32
154 xori t2, t6, 0x7
155 beqz t2, 1f
156 addi t6, t6, 1
157 sllv t4, t4, t6
1581: /* At this point t4 == I-cache sets. */
159 mul t2, t4, t8
160 srl t6, t5, MIPS_CONF1_IA_SHIFT
161 andi t6, t6, (MIPS_CONF1_IA >> MIPS_CONF1_IA_SHIFT)
162 addi t6, t6, 1
163 /* At this point t6 == I-cache ways. */
164 mul t2, t2, t6
165#endif
166
167#ifdef CONFIG_SYS_DCACHE_SIZE
168 li t3, CONFIG_SYS_DCACHE_SIZE
169#else
170 /* Detect D-cache size. */
171 srl t6, t5, MIPS_CONF1_DS_SHIFT
172 andi t6, t6, (MIPS_CONF1_DS >> MIPS_CONF1_DS_SHIFT)
173 li t4, 32
174 xori t3, t6, 0x7
175 beqz t3, 1f
176 addi t6, t6, 1
177 sllv t4, t4, t6
1781: /* At this point t4 == I-cache sets. */
179 mul t3, t4, t7
180 srl t6, t5, MIPS_CONF1_DA_SHIFT
181 andi t6, t6, (MIPS_CONF1_DA >> MIPS_CONF1_DA_SHIFT)
182 addi t6, t6, 1
183 /* At this point t6 == I-cache ways. */
184 mul t3, t3, t6
185#endif
186
187 /* Determine the largest L1 cache size */
188#if defined(CONFIG_SYS_ICACHE_SIZE) && defined(CONFIG_SYS_DCACHE_SIZE)
189#if CONFIG_SYS_ICACHE_SIZE > CONFIG_SYS_DCACHE_SIZE
190 li v0, CONFIG_SYS_ICACHE_SIZE
191#else
192 li v0, CONFIG_SYS_DCACHE_SIZE
193#endif
194#else
195 move v0, t2
196 sltu t1, t2, t3
197 movn v0, t3, t1
198#endif
Shinya Kuribayashi18988402008-03-25 21:30:06 +0900199 /*
200 * Now clear that much memory starting from zero.
wdenkc0218802003-03-27 12:09:35 +0000201 */
Shinya Kuribayashi7daf2eb2008-06-05 22:29:00 +0900202 PTR_LI a0, CKSEG1
Shinya Kuribayashi18988402008-03-25 21:30:06 +0900203 PTR_ADDU a1, a0, v0
2042: PTR_ADDIU a0, 64
205 f_fill64 a0, -64, zero
206 bne a0, a1, 2b
wdenk8bde7f72003-06-27 21:31:46 +0000207
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900208 /*
209 * The caches are probably in an indeterminate state,
210 * so we force good parity into them by doing an
211 * invalidate, load/fill, invalidate for each line.
212 */
wdenkc0218802003-03-27 12:09:35 +0000213
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900214 /*
215 * Assume bottom of RAM will generate good parity for the cache.
wdenkc0218802003-03-27 12:09:35 +0000216 */
217
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900218 /*
219 * Initialize the I-cache first,
wdenkc0218802003-03-27 12:09:35 +0000220 */
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900221 move a1, t2
Gabor Juhosee8b1e22013-06-13 12:59:35 +0200222 move a2, t8
Gabor Juhosdb2c86d2013-06-13 12:59:36 +0200223 PTR_LA v1, mips_init_icache
224 jalr v1
wdenkc0218802003-03-27 12:09:35 +0000225
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900226 /*
227 * then initialize D-cache.
wdenkc0218802003-03-27 12:09:35 +0000228 */
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900229 move a1, t3
Paul Burtonfa476f72013-11-08 11:18:42 +0000230 move a2, t7
Gabor Juhosdb2c86d2013-06-13 12:59:36 +0200231 PTR_LA v1, mips_init_dcache
232 jalr v1
wdenkc0218802003-03-27 12:09:35 +0000233
Shinya Kuribayashi2e0e5272008-03-25 21:30:06 +0900234 jr RA
Shinya Kuribayashi2f5d4142008-03-25 21:30:06 +0900235 END(mips_cache_reset)
wdenkc0218802003-03-27 12:09:35 +0000236
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900237/*
238 * dcache_status - get cache status
239 *
240 * RETURNS: 0 - cache disabled; 1 - cache enabled
241 *
242 */
Shinya Kuribayashi2f5d4142008-03-25 21:30:06 +0900243LEAF(dcache_status)
Shinya Kuribayashid98e3482008-03-25 21:30:07 +0900244 mfc0 t0, CP0_CONFIG
245 li t1, CONF_CM_UNCACHED
246 andi t0, t0, CONF_CM_CMASK
247 move v0, zero
248 beq t0, t1, 2f
249 li v0, 1
2502: jr ra
Shinya Kuribayashi2f5d4142008-03-25 21:30:06 +0900251 END(dcache_status)
wdenkc0218802003-03-27 12:09:35 +0000252
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900253/*
254 * dcache_disable - disable cache
255 *
256 * RETURNS: N/A
257 *
258 */
Shinya Kuribayashi2f5d4142008-03-25 21:30:06 +0900259LEAF(dcache_disable)
wdenkc0218802003-03-27 12:09:35 +0000260 mfc0 t0, CP0_CONFIG
261 li t1, -8
262 and t0, t0, t1
263 ori t0, t0, CONF_CM_UNCACHED
Shinya Kuribayashi03c031d2007-10-27 15:27:06 +0900264 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi43c50922008-04-17 23:35:13 +0900265 jr ra
Shinya Kuribayashi2f5d4142008-03-25 21:30:06 +0900266 END(dcache_disable)
wdenkc0218802003-03-27 12:09:35 +0000267
Shinya Kuribayashi7aa1f192011-05-07 00:18:13 +0900268/*
269 * dcache_enable - enable cache
270 *
271 * RETURNS: N/A
272 *
273 */
Shinya Kuribayashiea638952008-05-03 13:51:28 +0900274LEAF(dcache_enable)
275 mfc0 t0, CP0_CONFIG
276 ori t0, CONF_CM_CMASK
277 xori t0, CONF_CM_CMASK
Daniel Schwierzeck979cfea2012-04-02 02:57:55 +0000278 ori t0, CONFIG_SYS_MIPS_CACHE_MODE
Shinya Kuribayashiea638952008-05-03 13:51:28 +0900279 mtc0 t0, CP0_CONFIG
280 jr ra
281 END(dcache_enable)