Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2003 |
| 4 | * Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/cacheops.h> |
Paul Burton | 939a255 | 2017-05-12 13:26:11 +0200 | [diff] [blame] | 9 | #ifdef CONFIG_MIPS_L2_CACHE |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 10 | #include <asm/cm.h> |
Paul Burton | 939a255 | 2017-05-12 13:26:11 +0200 | [diff] [blame] | 11 | #endif |
Paul Burton | 219c2db | 2017-11-21 11:18:37 -0800 | [diff] [blame] | 12 | #include <asm/io.h> |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 13 | #include <asm/mipsregs.h> |
Paul Burton | d8b3269 | 2017-11-21 11:18:38 -0800 | [diff] [blame] | 14 | #include <asm/system.h> |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 15 | |
Paul Burton | 8cb4817 | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 16 | DECLARE_GLOBAL_DATA_PTR; |
Paul Burton | 3722862 | 2016-05-27 14:28:05 +0100 | [diff] [blame] | 17 | |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 18 | static void probe_l2(void) |
| 19 | { |
| 20 | #ifdef CONFIG_MIPS_L2_CACHE |
| 21 | unsigned long conf2, sl; |
| 22 | bool l2c = false; |
| 23 | |
| 24 | if (!(read_c0_config1() & MIPS_CONF_M)) |
| 25 | return; |
| 26 | |
| 27 | conf2 = read_c0_config2(); |
| 28 | |
| 29 | if (__mips_isa_rev >= 6) { |
| 30 | l2c = conf2 & MIPS_CONF_M; |
| 31 | if (l2c) |
| 32 | l2c = read_c0_config3() & MIPS_CONF_M; |
| 33 | if (l2c) |
| 34 | l2c = read_c0_config4() & MIPS_CONF_M; |
| 35 | if (l2c) |
| 36 | l2c = read_c0_config5() & MIPS_CONF5_L2C; |
| 37 | } |
| 38 | |
| 39 | if (l2c && config_enabled(CONFIG_MIPS_CM)) { |
| 40 | gd->arch.l2_line_size = mips_cm_l2_line_size(); |
| 41 | } else if (l2c) { |
| 42 | /* We don't know how to retrieve L2 config on this system */ |
| 43 | BUG(); |
| 44 | } else { |
| 45 | sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF; |
| 46 | gd->arch.l2_line_size = sl ? (2 << sl) : 0; |
| 47 | } |
| 48 | #endif |
| 49 | } |
| 50 | |
Paul Burton | 8cb4817 | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 51 | void mips_cache_probe(void) |
| 52 | { |
| 53 | #ifdef CONFIG_SYS_CACHE_SIZE_AUTO |
| 54 | unsigned long conf1, il, dl; |
Paul Burton | 3722862 | 2016-05-27 14:28:05 +0100 | [diff] [blame] | 55 | |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 56 | conf1 = read_c0_config1(); |
Paul Burton | 8cb4817 | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 57 | |
Daniel Schwierzeck | a3ab2ae | 2016-01-12 21:48:26 +0100 | [diff] [blame] | 58 | il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF; |
Paul Burton | 8cb4817 | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 59 | dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF; |
| 60 | |
| 61 | gd->arch.l1i_line_size = il ? (2 << il) : 0; |
| 62 | gd->arch.l1d_line_size = dl ? (2 << dl) : 0; |
| 63 | #endif |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 64 | probe_l2(); |
Paul Burton | 8cb4817 | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | static inline unsigned long icache_line_size(void) |
| 68 | { |
| 69 | #ifdef CONFIG_SYS_CACHE_SIZE_AUTO |
| 70 | return gd->arch.l1i_line_size; |
| 71 | #else |
| 72 | return CONFIG_SYS_ICACHE_LINE_SIZE; |
| 73 | #endif |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | static inline unsigned long dcache_line_size(void) |
| 77 | { |
Paul Burton | 8cb4817 | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 78 | #ifdef CONFIG_SYS_CACHE_SIZE_AUTO |
| 79 | return gd->arch.l1d_line_size; |
| 80 | #else |
| 81 | return CONFIG_SYS_DCACHE_LINE_SIZE; |
| 82 | #endif |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 83 | } |
| 84 | |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 85 | static inline unsigned long scache_line_size(void) |
| 86 | { |
| 87 | #ifdef CONFIG_MIPS_L2_CACHE |
| 88 | return gd->arch.l2_line_size; |
| 89 | #else |
| 90 | return 0; |
| 91 | #endif |
| 92 | } |
| 93 | |
Paul Burton | fb64cda | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 94 | #define cache_loop(start, end, lsize, ops...) do { \ |
| 95 | const void *addr = (const void *)(start & ~(lsize - 1)); \ |
| 96 | const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \ |
| 97 | const unsigned int cache_ops[] = { ops }; \ |
| 98 | unsigned int i; \ |
| 99 | \ |
Paul Burton | cc4f364 | 2017-11-21 11:18:39 -0800 | [diff] [blame] | 100 | if (!lsize) \ |
| 101 | break; \ |
| 102 | \ |
Paul Burton | fb64cda | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 103 | for (; addr <= aend; addr += lsize) { \ |
| 104 | for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \ |
| 105 | mips_cache(cache_ops[i], addr); \ |
| 106 | } \ |
| 107 | } while (0) |
| 108 | |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 109 | void flush_cache(ulong start_addr, ulong size) |
| 110 | { |
| 111 | unsigned long ilsize = icache_line_size(); |
| 112 | unsigned long dlsize = dcache_line_size(); |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 113 | unsigned long slsize = scache_line_size(); |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 114 | |
| 115 | /* aend will be miscalculated when size is zero, so we return here */ |
| 116 | if (size == 0) |
| 117 | return; |
| 118 | |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 119 | if ((ilsize == dlsize) && !slsize) { |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 120 | /* flush I-cache & D-cache simultaneously */ |
Paul Burton | fb64cda | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 121 | cache_loop(start_addr, start_addr + size, ilsize, |
| 122 | HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I); |
Paul Burton | 219c2db | 2017-11-21 11:18:37 -0800 | [diff] [blame] | 123 | goto ops_done; |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /* flush D-cache */ |
Paul Burton | fb64cda | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 127 | cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D); |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 128 | |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 129 | /* flush L2 cache */ |
Paul Burton | cc4f364 | 2017-11-21 11:18:39 -0800 | [diff] [blame] | 130 | cache_loop(start_addr, start_addr + size, slsize, HIT_WRITEBACK_INV_SD); |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 131 | |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 132 | /* flush I-cache */ |
Paul Burton | fb64cda | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 133 | cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I); |
Paul Burton | 219c2db | 2017-11-21 11:18:37 -0800 | [diff] [blame] | 134 | |
| 135 | ops_done: |
| 136 | /* ensure cache ops complete before any further memory accesses */ |
| 137 | sync(); |
Paul Burton | d8b3269 | 2017-11-21 11:18:38 -0800 | [diff] [blame] | 138 | |
| 139 | /* ensure the pipeline doesn't contain now-invalid instructions */ |
| 140 | instruction_hazard_barrier(); |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | void flush_dcache_range(ulong start_addr, ulong stop) |
| 144 | { |
| 145 | unsigned long lsize = dcache_line_size(); |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 146 | unsigned long slsize = scache_line_size(); |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 147 | |
Marek Vasut | fbb0de0 | 2016-01-27 03:13:59 +0100 | [diff] [blame] | 148 | /* aend will be miscalculated when size is zero, so we return here */ |
| 149 | if (start_addr == stop) |
| 150 | return; |
| 151 | |
Paul Burton | fb64cda | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 152 | cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D); |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 153 | |
| 154 | /* flush L2 cache */ |
Paul Burton | cc4f364 | 2017-11-21 11:18:39 -0800 | [diff] [blame] | 155 | cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD); |
Paul Burton | 219c2db | 2017-11-21 11:18:37 -0800 | [diff] [blame] | 156 | |
| 157 | /* ensure cache ops complete before any further memory accesses */ |
| 158 | sync(); |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 159 | } |
| 160 | |
| 161 | void invalidate_dcache_range(ulong start_addr, ulong stop) |
| 162 | { |
| 163 | unsigned long lsize = dcache_line_size(); |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 164 | unsigned long slsize = scache_line_size(); |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 165 | |
Marek Vasut | fbb0de0 | 2016-01-27 03:13:59 +0100 | [diff] [blame] | 166 | /* aend will be miscalculated when size is zero, so we return here */ |
| 167 | if (start_addr == stop) |
| 168 | return; |
| 169 | |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 170 | /* invalidate L2 cache */ |
Paul Burton | cc4f364 | 2017-11-21 11:18:39 -0800 | [diff] [blame] | 171 | cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD); |
Paul Burton | 4baa0ab | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 172 | |
Paul Burton | a95800e | 2016-06-09 13:09:51 +0100 | [diff] [blame] | 173 | cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D); |
Paul Burton | 219c2db | 2017-11-21 11:18:37 -0800 | [diff] [blame] | 174 | |
| 175 | /* ensure cache ops complete before any further memory accesses */ |
| 176 | sync(); |
Paul Burton | 30374f9 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 177 | } |
Daniel Schwierzeck | 2f85c2b | 2018-09-07 19:02:03 +0200 | [diff] [blame] | 178 | |
| 179 | int dcache_status(void) |
| 180 | { |
| 181 | unsigned int cca = read_c0_config() & CONF_CM_CMASK; |
| 182 | return cca != CONF_CM_UNCACHED; |
| 183 | } |
| 184 | |
| 185 | void dcache_enable(void) |
| 186 | { |
| 187 | puts("Not supported!\n"); |
| 188 | } |
| 189 | |
| 190 | void dcache_disable(void) |
| 191 | { |
| 192 | /* change CCA to uncached */ |
| 193 | change_c0_config(CONF_CM_CMASK, CONF_CM_UNCACHED); |
| 194 | |
| 195 | /* ensure the pipeline doesn't contain now-invalid instructions */ |
| 196 | instruction_hazard_barrier(); |
| 197 | } |