blob: 117fc56df7944ee2135a5acd7c94735f2cd73e36 [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
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090021#define RA t8
22
Shinya Kuribayashi396aa802008-03-25 21:30:07 +090023/*
24 * 16kB is the maximum size of instruction and data caches on MIPS 4K,
25 * 64kB is on 4KE, 24K, 5K, etc. Set bigger size for convenience.
26 *
27 * Note that the above size is the maximum size of primary cache. U-Boot
28 * doesn't have L2 cache support for now.
29 */
30#define MIPS_MAX_CACHE_SIZE 0x10000
wdenkbb1b8262003-03-27 12:09:35 +000031
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +090032#define INDEX_BASE CKSEG0
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090033
34 .macro cache_op op addr
35 .set push
36 .set noreorder
37 .set mips3
38 cache \op, 0(\addr)
39 .set pop
40 .endm
41
Shinya Kuribayashi52c27e62008-03-25 21:30:06 +090042 .macro f_fill64 dst, offset, val
43 LONG_S \val, (\offset + 0 * LONGSIZE)(\dst)
44 LONG_S \val, (\offset + 1 * LONGSIZE)(\dst)
45 LONG_S \val, (\offset + 2 * LONGSIZE)(\dst)
46 LONG_S \val, (\offset + 3 * LONGSIZE)(\dst)
47 LONG_S \val, (\offset + 4 * LONGSIZE)(\dst)
48 LONG_S \val, (\offset + 5 * LONGSIZE)(\dst)
49 LONG_S \val, (\offset + 6 * LONGSIZE)(\dst)
50 LONG_S \val, (\offset + 7 * LONGSIZE)(\dst)
51#if LONGSIZE == 4
52 LONG_S \val, (\offset + 8 * LONGSIZE)(\dst)
53 LONG_S \val, (\offset + 9 * LONGSIZE)(\dst)
54 LONG_S \val, (\offset + 10 * LONGSIZE)(\dst)
55 LONG_S \val, (\offset + 11 * LONGSIZE)(\dst)
56 LONG_S \val, (\offset + 12 * LONGSIZE)(\dst)
57 LONG_S \val, (\offset + 13 * LONGSIZE)(\dst)
58 LONG_S \val, (\offset + 14 * LONGSIZE)(\dst)
59 LONG_S \val, (\offset + 15 * LONGSIZE)(\dst)
60#endif
61 .endm
62
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090063/*
64 * mips_init_icache(uint PRId, ulong icache_size, unchar icache_linesz)
65 */
66LEAF(mips_init_icache)
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +090067 blez a1, 9f
68 mtc0 zero, CP0_TAGLO
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090069 /* clear tag to invalidate */
70 PTR_LI t0, INDEX_BASE
71 PTR_ADDU t1, t0, a1
Zhi-zhou Zhang724f6182012-10-16 15:02:08 +0200721: cache_op INDEX_STORE_TAG_I t0
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090073 PTR_ADDU t0, a2
74 bne t0, t1, 1b
75 /* fill once, so data field parity is correct */
76 PTR_LI t0, INDEX_BASE
Zhi-zhou Zhang724f6182012-10-16 15:02:08 +0200772: cache_op FILL t0
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090078 PTR_ADDU t0, a2
79 bne t0, t1, 2b
80 /* invalidate again - prudent but not strictly neccessary */
81 PTR_LI t0, INDEX_BASE
Zhi-zhou Zhang724f6182012-10-16 15:02:08 +0200821: cache_op INDEX_STORE_TAG_I t0
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090083 PTR_ADDU t0, a2
84 bne t0, t1, 1b
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900859: jr ra
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090086 END(mips_init_icache)
87
88/*
89 * mips_init_dcache(uint PRId, ulong dcache_size, unchar dcache_linesz)
90 */
91LEAF(mips_init_dcache)
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +090092 blez a1, 9f
93 mtc0 zero, CP0_TAGLO
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090094 /* clear all tags */
95 PTR_LI t0, INDEX_BASE
96 PTR_ADDU t1, t0, a1
Zhi-zhou Zhang724f6182012-10-16 15:02:08 +0200971: cache_op INDEX_STORE_TAG_D t0
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +090098 PTR_ADDU t0, a2
99 bne t0, t1, 1b
100 /* load from each line (in cached space) */
101 PTR_LI t0, INDEX_BASE
1022: LONG_L zero, 0(t0)
103 PTR_ADDU t0, a2
104 bne t0, t1, 2b
105 /* clear all tags */
106 PTR_LI t0, INDEX_BASE
Zhi-zhou Zhang724f6182012-10-16 15:02:08 +02001071: cache_op INDEX_STORE_TAG_D t0
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900108 PTR_ADDU t0, a2
109 bne t0, t1, 1b
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +09001109: jr ra
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900111 END(mips_init_dcache)
112
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900113/*
114 * mips_cache_reset - low level initialisation of the primary caches
115 *
116 * This routine initialises the primary caches to ensure that they have good
117 * parity. It must be called by the ROM before any cached locations are used
118 * to prevent the possibility of data with bad parity being written to memory.
119 *
120 * To initialise the instruction cache it is essential that a source of data
121 * with good parity is available. This routine will initialise an area of
122 * memory starting at location zero to be used as a source of parity.
123 *
124 * RETURNS: N/A
125 *
126 */
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900127NESTED(mips_cache_reset, 0, ra)
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900128 move RA, ra
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200129 li t2, CONFIG_SYS_ICACHE_SIZE
130 li t3, CONFIG_SYS_DCACHE_SIZE
131 li t4, CONFIG_SYS_CACHELINE_SIZE
wdenkbb1b8262003-03-27 12:09:35 +0000132 move t5, t4
133
wdenkbb1b8262003-03-27 12:09:35 +0000134 li v0, MIPS_MAX_CACHE_SIZE
135
Shinya Kuribayashi52c27e62008-03-25 21:30:06 +0900136 /*
137 * Now clear that much memory starting from zero.
wdenkbb1b8262003-03-27 12:09:35 +0000138 */
Shinya Kuribayashi6c6b2612008-06-05 22:29:00 +0900139 PTR_LI a0, CKSEG1
Shinya Kuribayashi52c27e62008-03-25 21:30:06 +0900140 PTR_ADDU a1, a0, v0
1412: PTR_ADDIU a0, 64
142 f_fill64 a0, -64, zero
143 bne a0, a1, 2b
wdenk57b2d802003-06-27 21:31:46 +0000144
Shinya Kuribayashic7faac52007-10-27 15:27:06 +0900145 /*
146 * The caches are probably in an indeterminate state,
147 * so we force good parity into them by doing an
148 * invalidate, load/fill, invalidate for each line.
149 */
wdenkbb1b8262003-03-27 12:09:35 +0000150
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900151 /*
152 * Assume bottom of RAM will generate good parity for the cache.
wdenkbb1b8262003-03-27 12:09:35 +0000153 */
154
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900155 /*
156 * Initialize the I-cache first,
wdenkbb1b8262003-03-27 12:09:35 +0000157 */
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900158 move a1, t2
159 move a2, t4
Shinya Kuribayashib1288822008-05-06 13:22:52 +0900160 PTR_LA t7, mips_init_icache
161 jalr t7
wdenkbb1b8262003-03-27 12:09:35 +0000162
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900163 /*
164 * then initialize D-cache.
wdenkbb1b8262003-03-27 12:09:35 +0000165 */
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900166 move a1, t3
167 move a2, t5
Shinya Kuribayashib1288822008-05-06 13:22:52 +0900168 PTR_LA t7, mips_init_dcache
169 jalr t7
wdenkbb1b8262003-03-27 12:09:35 +0000170
Shinya Kuribayashi5bb51af2008-03-25 21:30:06 +0900171 jr RA
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900172 END(mips_cache_reset)
wdenkbb1b8262003-03-27 12:09:35 +0000173
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900174/*
175 * dcache_status - get cache status
176 *
177 * RETURNS: 0 - cache disabled; 1 - cache enabled
178 *
179 */
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900180LEAF(dcache_status)
Shinya Kuribayashi3bdce4f2008-03-25 21:30:07 +0900181 mfc0 t0, CP0_CONFIG
182 li t1, CONF_CM_UNCACHED
183 andi t0, t0, CONF_CM_CMASK
184 move v0, zero
185 beq t0, t1, 2f
186 li v0, 1
1872: jr ra
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900188 END(dcache_status)
wdenkbb1b8262003-03-27 12:09:35 +0000189
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900190/*
191 * dcache_disable - disable cache
192 *
193 * RETURNS: N/A
194 *
195 */
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900196LEAF(dcache_disable)
wdenkbb1b8262003-03-27 12:09:35 +0000197 mfc0 t0, CP0_CONFIG
198 li t1, -8
199 and t0, t0, t1
200 ori t0, t0, CONF_CM_UNCACHED
Shinya Kuribayashic7faac52007-10-27 15:27:06 +0900201 mtc0 t0, CP0_CONFIG
Shinya Kuribayashi9dabea12008-04-17 23:35:13 +0900202 jr ra
Shinya Kuribayashi06222122008-03-25 21:30:06 +0900203 END(dcache_disable)
wdenkbb1b8262003-03-27 12:09:35 +0000204
Shinya Kuribayashi6911d0a2011-05-07 00:18:13 +0900205/*
206 * dcache_enable - enable cache
207 *
208 * RETURNS: N/A
209 *
210 */
Shinya Kuribayashi4d0e2c92008-05-03 13:51:28 +0900211LEAF(dcache_enable)
212 mfc0 t0, CP0_CONFIG
213 ori t0, CONF_CM_CMASK
214 xori t0, CONF_CM_CMASK
Daniel Schwierzeck9adc1902012-04-02 02:57:55 +0000215 ori t0, CONFIG_SYS_MIPS_CACHE_MODE
Shinya Kuribayashi4d0e2c92008-05-03 13:51:28 +0900216 mtc0 t0, CP0_CONFIG
217 jr ra
218 END(dcache_enable)