Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2003 |
| 3 | * Wolfgang Denk, DENX Software Engineering, <wd@denx.de> |
| 4 | * |
| 5 | * SPDX-License-Identifier: GPL-2.0+ |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
| 9 | #include <asm/cacheops.h> |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 10 | #include <asm/cm.h> |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 11 | #include <asm/mipsregs.h> |
| 12 | |
Paul Burton | dc2037e | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 13 | DECLARE_GLOBAL_DATA_PTR; |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 14 | |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 15 | static void probe_l2(void) |
| 16 | { |
| 17 | #ifdef CONFIG_MIPS_L2_CACHE |
| 18 | unsigned long conf2, sl; |
| 19 | bool l2c = false; |
| 20 | |
| 21 | if (!(read_c0_config1() & MIPS_CONF_M)) |
| 22 | return; |
| 23 | |
| 24 | conf2 = read_c0_config2(); |
| 25 | |
| 26 | if (__mips_isa_rev >= 6) { |
| 27 | l2c = conf2 & MIPS_CONF_M; |
| 28 | if (l2c) |
| 29 | l2c = read_c0_config3() & MIPS_CONF_M; |
| 30 | if (l2c) |
| 31 | l2c = read_c0_config4() & MIPS_CONF_M; |
| 32 | if (l2c) |
| 33 | l2c = read_c0_config5() & MIPS_CONF5_L2C; |
| 34 | } |
| 35 | |
| 36 | if (l2c && config_enabled(CONFIG_MIPS_CM)) { |
| 37 | gd->arch.l2_line_size = mips_cm_l2_line_size(); |
| 38 | } else if (l2c) { |
| 39 | /* We don't know how to retrieve L2 config on this system */ |
| 40 | BUG(); |
| 41 | } else { |
| 42 | sl = (conf2 & MIPS_CONF2_SL) >> MIPS_CONF2_SL_SHF; |
| 43 | gd->arch.l2_line_size = sl ? (2 << sl) : 0; |
| 44 | } |
| 45 | #endif |
| 46 | } |
| 47 | |
Paul Burton | dc2037e | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 48 | void mips_cache_probe(void) |
| 49 | { |
| 50 | #ifdef CONFIG_SYS_CACHE_SIZE_AUTO |
| 51 | unsigned long conf1, il, dl; |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 52 | |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 53 | conf1 = read_c0_config1(); |
Paul Burton | dc2037e | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 54 | |
Daniel Schwierzeck | a6dae71 | 2016-01-12 21:48:26 +0100 | [diff] [blame] | 55 | il = (conf1 & MIPS_CONF1_IL) >> MIPS_CONF1_IL_SHF; |
Paul Burton | dc2037e | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 56 | dl = (conf1 & MIPS_CONF1_DL) >> MIPS_CONF1_DL_SHF; |
| 57 | |
| 58 | gd->arch.l1i_line_size = il ? (2 << il) : 0; |
| 59 | gd->arch.l1d_line_size = dl ? (2 << dl) : 0; |
| 60 | #endif |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 61 | probe_l2(); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Paul Burton | dc2037e | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 64 | static inline unsigned long icache_line_size(void) |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 65 | { |
Paul Burton | dc2037e | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 66 | #ifdef CONFIG_SYS_CACHE_SIZE_AUTO |
| 67 | return gd->arch.l1i_line_size; |
| 68 | #else |
| 69 | return CONFIG_SYS_ICACHE_LINE_SIZE; |
| 70 | #endif |
| 71 | } |
Paul Burton | 62f1352 | 2016-05-27 14:28:05 +0100 | [diff] [blame] | 72 | |
Paul Burton | dc2037e | 2016-09-21 11:18:48 +0100 | [diff] [blame] | 73 | static inline unsigned long dcache_line_size(void) |
| 74 | { |
| 75 | #ifdef CONFIG_SYS_CACHE_SIZE_AUTO |
| 76 | return gd->arch.l1d_line_size; |
| 77 | #else |
| 78 | return CONFIG_SYS_DCACHE_LINE_SIZE; |
| 79 | #endif |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 82 | static inline unsigned long scache_line_size(void) |
| 83 | { |
| 84 | #ifdef CONFIG_MIPS_L2_CACHE |
| 85 | return gd->arch.l2_line_size; |
| 86 | #else |
| 87 | return 0; |
| 88 | #endif |
| 89 | } |
| 90 | |
Paul Burton | a97d593 | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 91 | #define cache_loop(start, end, lsize, ops...) do { \ |
| 92 | const void *addr = (const void *)(start & ~(lsize - 1)); \ |
| 93 | const void *aend = (const void *)((end - 1) & ~(lsize - 1)); \ |
| 94 | const unsigned int cache_ops[] = { ops }; \ |
| 95 | unsigned int i; \ |
| 96 | \ |
| 97 | for (; addr <= aend; addr += lsize) { \ |
| 98 | for (i = 0; i < ARRAY_SIZE(cache_ops); i++) \ |
| 99 | mips_cache(cache_ops[i], addr); \ |
| 100 | } \ |
| 101 | } while (0) |
| 102 | |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 103 | void flush_cache(ulong start_addr, ulong size) |
| 104 | { |
| 105 | unsigned long ilsize = icache_line_size(); |
| 106 | unsigned long dlsize = dcache_line_size(); |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 107 | unsigned long slsize = scache_line_size(); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 108 | |
| 109 | /* aend will be miscalculated when size is zero, so we return here */ |
| 110 | if (size == 0) |
| 111 | return; |
| 112 | |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 113 | if ((ilsize == dlsize) && !slsize) { |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 114 | /* flush I-cache & D-cache simultaneously */ |
Paul Burton | a97d593 | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 115 | cache_loop(start_addr, start_addr + size, ilsize, |
| 116 | HIT_WRITEBACK_INV_D, HIT_INVALIDATE_I); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
| 120 | /* flush D-cache */ |
Paul Burton | a97d593 | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 121 | cache_loop(start_addr, start_addr + size, dlsize, HIT_WRITEBACK_INV_D); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 122 | |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 123 | /* flush L2 cache */ |
| 124 | if (slsize) |
| 125 | cache_loop(start_addr, start_addr + size, slsize, |
| 126 | HIT_WRITEBACK_INV_SD); |
| 127 | |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 128 | /* flush I-cache */ |
Paul Burton | a97d593 | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 129 | cache_loop(start_addr, start_addr + size, ilsize, HIT_INVALIDATE_I); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | void flush_dcache_range(ulong start_addr, ulong stop) |
| 133 | { |
| 134 | unsigned long lsize = dcache_line_size(); |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 135 | unsigned long slsize = scache_line_size(); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 136 | |
Marek Vasut | 0e50ffc | 2016-01-27 03:13:59 +0100 | [diff] [blame] | 137 | /* aend will be miscalculated when size is zero, so we return here */ |
| 138 | if (start_addr == stop) |
| 139 | return; |
| 140 | |
Paul Burton | a97d593 | 2016-05-27 14:28:06 +0100 | [diff] [blame] | 141 | cache_loop(start_addr, stop, lsize, HIT_WRITEBACK_INV_D); |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 142 | |
| 143 | /* flush L2 cache */ |
| 144 | if (slsize) |
| 145 | cache_loop(start_addr, stop, slsize, HIT_WRITEBACK_INV_SD); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | void invalidate_dcache_range(ulong start_addr, ulong stop) |
| 149 | { |
| 150 | unsigned long lsize = dcache_line_size(); |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 151 | unsigned long slsize = scache_line_size(); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 152 | |
Marek Vasut | 0e50ffc | 2016-01-27 03:13:59 +0100 | [diff] [blame] | 153 | /* aend will be miscalculated when size is zero, so we return here */ |
| 154 | if (start_addr == stop) |
| 155 | return; |
| 156 | |
Paul Burton | 8156078 | 2016-09-21 11:18:54 +0100 | [diff] [blame] | 157 | /* invalidate L2 cache */ |
| 158 | if (slsize) |
| 159 | cache_loop(start_addr, stop, slsize, HIT_INVALIDATE_SD); |
| 160 | |
Paul Burton | 1194f94 | 2016-06-09 13:09:51 +0100 | [diff] [blame] | 161 | cache_loop(start_addr, stop, lsize, HIT_INVALIDATE_D); |
Paul Burton | 4ff6b10 | 2015-01-29 01:27:57 +0000 | [diff] [blame] | 162 | } |