blob: fb1d84b75a6ad7f482dcf41f13c638c1e917bf95 [file] [log] [blame]
wdenkbb1b8262003-03-27 12:09:35 +00001/*
Shinya Kuribayashi396aa802008-03-25 21:30:07 +09002 * Cache-handling routined for MIPS CPUs
wdenkbb1b8262003-03-27 12:09:35 +00003 *
4 * Copyright (c) 2003 Wolfgang Denk <wd@denx.de>
5 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenkbb1b8262003-03-27 12:09:35 +00007 */
8
Wolfgang Denk0191e472010-10-26 14:34:52 +02009#include <asm-offsets.h>
wdenkbb1b8262003-03-27 12:09:35 +000010#include <config.h>
Shinya Kuribayashi06222122008-03-25 21:30:06 +090011#include <asm/asm.h>
wdenkbb1b8262003-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 Schwierzeck9adc1902012-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 Juhos187fe812013-06-13 12:59:34 +020021#define RA t9
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090022
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +090023#define INDEX_BASE CKSEG0
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090024
Shinya Kuribayashi52c27e62008-03-25 21:30:06 +090025 .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)
34#if LONGSIZE == 4
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)
43#endif
44 .endm
45
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090046/*
47 * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz)
48 */
49LEAF(mips_init_icache)
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +090050 blez a1, 9f
51 mtc0 zero, CP0_TAGLO
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090052 /* clear tag to invalidate */
53 PTR_LI t0, INDEX_BASE
54 PTR_ADDU t1, t0, a1
Paul Burton5429af82015-01-29 01:27:56 +0000551: cache INDEX_STORE_TAG_I, 0(t0)
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090056 PTR_ADDU t0, a2
57 bne t0, t1, 1b
58 /* fill once, so data field parity is correct */
59 PTR_LI t0, INDEX_BASE
Paul Burton5429af82015-01-29 01:27:56 +0000602: cache FILL, 0(t0)
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090061 PTR_ADDU t0, a2
62 bne t0, t1, 2b
63 /* invalidate again - prudent but not strictly neccessary */
64 PTR_LI t0, INDEX_BASE
Paul Burton5429af82015-01-29 01:27:56 +0000651: cache INDEX_STORE_TAG_I, 0(t0)
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090066 PTR_ADDU t0, a2
67 bne t0, t1, 1b
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900689: jr ra
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090069 END(mips_init_icache)
70
71/*
72 * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz)
73 */
74LEAF(mips_init_dcache)
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +090075 blez a1, 9f
76 mtc0 zero, CP0_TAGLO
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090077 /* clear all tags */
78 PTR_LI t0, INDEX_BASE
79 PTR_ADDU t1, t0, a1
Paul Burton5429af82015-01-29 01:27:56 +0000801: cache INDEX_STORE_TAG_D, 0(t0)
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090081 PTR_ADDU t0, a2
82 bne t0, t1, 1b
83 /* load from each line (in cached space) */
84 PTR_LI t0, INDEX_BASE
852: LONG_L zero, 0(t0)
86 PTR_ADDU t0, a2
87 bne t0, t1, 2b
88 /* clear all tags */
89 PTR_LI t0, INDEX_BASE
Paul Burton5429af82015-01-29 01:27:56 +0000901: cache INDEX_STORE_TAG_D, 0(t0)
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090091 PTR_ADDU t0, a2
92 bne t0, t1, 1b
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900939: jr ra
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090094 END(mips_init_dcache)
95
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +090096/*
97 * mips_cache_reset - low level initialisation of the primary caches
98 *
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.
102 *
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.
106 *
107 * RETURNS: N/A
108 *
109 */
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900110NESTED(mips_cache_reset, 0, ra)
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900111 move RA, ra
Paul Burtonf122b5a2013-11-08 11:18:42 +0000112
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
117#endif
118
119#ifdef CONFIG_SYS_CACHELINE_SIZE
120 li t7, CONFIG_SYS_CACHELINE_SIZE
Gabor Juhos5b4f4ff2013-06-13 12:59:35 +0200121 li t8, CONFIG_SYS_CACHELINE_SIZE
Paul Burtonf122b5a2013-11-08 11:18:42 +0000122#else
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)
126 beqz t8, 1f
127 li t6, 2
128 sllv t8, t6, t8
wdenkbb1b8262003-03-27 12:09:35 +0000129
Paul Burtonf122b5a2013-11-08 11:18:42 +00001301: /* Detect D-cache line size. */
131 srl t7, t5, MIPS_CONF1_DL_SHIFT
132 andi t7, t7, (MIPS_CONF1_DL >> MIPS_CONF1_DL_SHIFT)
133 beqz t7, 1f
134 li t6, 2
135 sllv t7, t6, t7
1361:
137#endif
wdenkbb1b8262003-03-27 12:09:35 +0000138
Paul Burtonf122b5a2013-11-08 11:18:42 +0000139#ifdef CONFIG_SYS_ICACHE_SIZE
140 li t2, CONFIG_SYS_ICACHE_SIZE
141#else
142 /* Detect I-cache size. */
143 srl t6, t5, MIPS_CONF1_IS_SHIFT
144 andi t6, t6, (MIPS_CONF1_IS >> MIPS_CONF1_IS_SHIFT)
145 li t4, 32
146 xori t2, t6, 0x7
147 beqz t2, 1f
148 addi t6, t6, 1
149 sllv t4, t4, t6
1501: /* At this point t4 == I-cache sets. */
151 mul t2, t4, t8
152 srl t6, t5, MIPS_CONF1_IA_SHIFT
153 andi t6, t6, (MIPS_CONF1_IA >> MIPS_CONF1_IA_SHIFT)
154 addi t6, t6, 1
155 /* At this point t6 == I-cache ways. */
156 mul t2, t2, t6
157#endif
158
159#ifdef CONFIG_SYS_DCACHE_SIZE
160 li t3, CONFIG_SYS_DCACHE_SIZE
161#else
162 /* Detect D-cache size. */
163 srl t6, t5, MIPS_CONF1_DS_SHIFT
164 andi t6, t6, (MIPS_CONF1_DS >> MIPS_CONF1_DS_SHIFT)
165 li t4, 32
166 xori t3, t6, 0x7
167 beqz t3, 1f
168 addi t6, t6, 1
169 sllv t4, t4, t6
1701: /* At this point t4 == I-cache sets. */
171 mul t3, t4, t7
172 srl t6, t5, MIPS_CONF1_DA_SHIFT
173 andi t6, t6, (MIPS_CONF1_DA >> MIPS_CONF1_DA_SHIFT)
174 addi t6, t6, 1
175 /* At this point t6 == I-cache ways. */
176 mul t3, t3, t6
177#endif
178
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
183#else
184 li v0, CONFIG_SYS_DCACHE_SIZE
185#endif
186#else
187 move v0, t2
188 sltu t1, t2, t3
189 movn v0, t3, t1
190#endif
Shinya Kuribayashi52c27e62008-03-25 21:30:06 +0900191 /*
192 * Now clear that much memory starting from zero.
wdenkbb1b8262003-03-27 12:09:35 +0000193 */
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900194 PTR_LI a0, CKSEG1
Shinya Kuribayashi52c27e62008-03-25 21:30:06 +0900195 PTR_ADDU a1, a0, v0
1962: PTR_ADDIU a0, 64
197 f_fill64 a0, -64, zero
198 bne a0, a1, 2b
wdenk57b2d802003-06-27 21:31:46 +0000199
Shinya Kuribayashic7faac52007-10-27 15:27:06 +0900200 /*
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.
204 */
wdenkbb1b8262003-03-27 12:09:35 +0000205
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900206 /*
207 * Assume bottom of RAM will generate good parity for the cache.
wdenkbb1b8262003-03-27 12:09:35 +0000208 */
209
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900210 /*
211 * Initialize the I-cache first,
wdenkbb1b8262003-03-27 12:09:35 +0000212 */
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900213 move a1, t2
Gabor Juhos5b4f4ff2013-06-13 12:59:35 +0200214 move a2, t8
Gabor Juhos02d51a22013-06-13 12:59:36 +0200215 PTR_LA v1, mips_init_icache
216 jalr v1
wdenkbb1b8262003-03-27 12:09:35 +0000217
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900218 /*
219 * then initialize D-cache.
wdenkbb1b8262003-03-27 12:09:35 +0000220 */
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900221 move a1, t3
Paul Burtonf122b5a2013-11-08 11:18:42 +0000222 move a2, t7
Gabor Juhos02d51a22013-06-13 12:59:36 +0200223 PTR_LA v1, mips_init_dcache
224 jalr v1
wdenkbb1b8262003-03-27 12:09:35 +0000225
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900226 jr RA
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900227 END(mips_cache_reset)
wdenkbb1b8262003-03-27 12:09:35 +0000228
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900229/*
230 * dcache_status - get cache status
231 *
232 * RETURNS: 0 - cache disabled; 1 - cache enabled
233 *
234 */
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900235LEAF(dcache_status)
Shinya Kuribayashi3bdce4f2008-03-25 21:30:07 +0900236 mfc0 t0, CP0_CONFIG
237 li t1, CONF_CM_UNCACHED
238 andi t0, t0, CONF_CM_CMASK
239 move v0, zero
240 beq t0, t1, 2f
241 li v0, 1
2422: jr ra
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900243 END(dcache_status)
wdenkbb1b8262003-03-27 12:09:35 +0000244
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900245/*
246 * dcache_disable - disable cache
247 *
248 * RETURNS: N/A
249 *
250 */
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900251LEAF(dcache_disable)
wdenkbb1b8262003-03-27 12:09:35 +0000252 mfc0 t0, CP0_CONFIG
253 li t1, -8
254 and t0, t0, t1
255 ori t0, t0, CONF_CM_UNCACHED
Shinya Kuribayashic7faac52007-10-27 15:27:06 +0900256 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi9dabea12008-04-17 23:35:13 +0900257 jr ra
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900258 END(dcache_disable)
wdenkbb1b8262003-03-27 12:09:35 +0000259
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900260/*
261 * dcache_enable - enable cache
262 *
263 * RETURNS: N/A
264 *
265 */
Shinya Kuribayashi4d0e2c92008-05-03 13:51:28 +0900266LEAF(dcache_enable)
267 mfc0 t0, CP0_CONFIG
268 ori t0, CONF_CM_CMASK
269 xori t0, CONF_CM_CMASK
Daniel Schwierzeck9adc1902012-04-02 02:57:55 +0000270 ori t0, CONFIG_SYS_MIPS_CACHE_MODE
Shinya Kuribayashi4d0e2c92008-05-03 13:51:28 +0900271 mtc0 t0, CP0_CONFIG
272 jr ra
273 END(dcache_enable)