Loading...
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | /* * Copyright (c) 2022 ASPEED Technology Inc. * * SPDX-License-Identifier: Apache-2.0 */ #include <zephyr/kernel.h> #include <zephyr/arch/arm/aarch32/cortex_m/cmsis.h> #include <zephyr/drivers/syscon.h> /* * cache area control: each bit controls 32KB cache area * 1: cacheable * 0: no-cache * * bit[0]: 1st 32KB from 0x0000_0000 to 0x0000_7fff * bit[1]: 2nd 32KB from 0x0000_8000 to 0x0000_ffff * ... * bit[22]: 23th 32KB from 0x000a_8000 to 0x000a_ffff * bit[23]: 24th 32KB from 0x000b_0000 to 0x000b_ffff */ #define CACHE_AREA_CTRL_REG 0xa50 #define CACHE_INVALID_REG 0xa54 #define CACHE_FUNC_CTRL_REG 0xa58 #define CACHED_SRAM_ADDR CONFIG_SRAM_BASE_ADDRESS #define CACHED_SRAM_SIZE KB(CONFIG_SRAM_SIZE) #define CACHED_SRAM_END (CACHED_SRAM_ADDR + CACHED_SRAM_SIZE - 1) #define CACHE_AREA_SIZE_LOG2 15 #define CACHE_AREA_SIZE (1 << CACHE_AREA_SIZE_LOG2) #define DCACHE_INVALID(addr) (BIT(31) | ((addr & GENMASK(10, 0)) << 16)) #define ICACHE_INVALID(addr) (BIT(15) | ((addr & GENMASK(10, 0)) << 0)) #define ICACHE_CLEAN BIT(2) #define DCACHE_CLEAN BIT(1) #define CACHE_ENABLE BIT(0) /* cache size = 32B * 128 = 4KB */ #define CACHE_LINE_SIZE_LOG2 5 #define CACHE_LINE_SIZE (1 << CACHE_LINE_SIZE_LOG2) #define N_CACHE_LINE 128 #define CACHE_ALIGNED_ADDR(addr) \ ((addr >> CACHE_LINE_SIZE_LOG2) << CACHE_LINE_SIZE_LOG2) /* prefetch buffer */ #define PREFETCH_BUF_SIZE CACHE_LINE_SIZE static void aspeed_cache_init(void) { const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); uint32_t start_bit, end_bit, max_bit; /* set all cache areas to no-cache by default */ syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, 0); /* calculate how many areas need to be set */ max_bit = 8 * sizeof(uint32_t) - 1; start_bit = MIN(max_bit, CACHED_SRAM_ADDR >> CACHE_AREA_SIZE_LOG2); end_bit = MIN(max_bit, CACHED_SRAM_END >> CACHE_AREA_SIZE_LOG2); syscon_write_reg(dev, CACHE_AREA_CTRL_REG, GENMASK(end_bit, start_bit)); /* enable cache */ syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, CACHE_ENABLE); } /** * @brief get aligned address and the number of cachline to be invalied * @param [IN] addr - start address to be invalidated * @param [IN] size - size in byte * @param [OUT] p_aligned_addr - pointer to the cacheline aligned address variable * @return number of cacheline to be invalidated * * * addr * |--------size-------------| * |-----|-----|-----|-----|-----| * \ \ * head tail * * example 1: * addr = 0x100 (cacheline aligned), size = 64 * then head = 0x100, number of cache line to be invalidated = 64 / 32 = 2 * which means range [0x100, 0x140) will be invalidated * * example 2: * addr = 0x104 (cacheline unaligned), size = 64 * then head = 0x100, number of cache line to be invalidated = 1 + 64 / 32 = 3 * which means range [0x100, 0x160) will be invalidated */ static uint32_t get_n_cacheline(uint32_t addr, uint32_t size, uint32_t *p_head) { uint32_t n = 0; uint32_t tail; /* head */ *p_head = CACHE_ALIGNED_ADDR(addr); /* roundup the tail address */ tail = addr + size + (CACHE_LINE_SIZE - 1); tail = CACHE_ALIGNED_ADDR(tail); n = (tail - *p_head) >> CACHE_LINE_SIZE_LOG2; return n; } void cache_data_enable(void) { aspeed_cache_init(); } void cache_data_disable(void) { const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, 0); } void cache_instr_enable(void) { aspeed_cache_init(); } void cache_instr_disable(void) { const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, 0); } int cache_data_invd_all(void) { const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); uint32_t ctrl; unsigned int key = 0; syscon_read_reg(dev, CACHE_FUNC_CTRL_REG, &ctrl); /* enter critical section */ if (!k_is_in_isr()) { key = irq_lock(); } ctrl &= ~DCACHE_CLEAN; syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, ctrl); __DSB(); ctrl |= DCACHE_CLEAN; syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, ctrl); __DSB(); /* exit critical section */ if (!k_is_in_isr()) { irq_unlock(key); } return 0; } int cache_data_invd_range(void *addr, size_t size) { uint32_t aligned_addr, i, n; const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); unsigned int key = 0; if (((uint32_t)addr < CACHED_SRAM_ADDR) || ((uint32_t)addr > CACHED_SRAM_END)) { return 0; } /* enter critical section */ if (!k_is_in_isr()) { key = irq_lock(); } n = get_n_cacheline((uint32_t)addr, size, &aligned_addr); for (i = 0; i < n; i++) { syscon_write_reg(dev, CACHE_INVALID_REG, 0); syscon_write_reg(dev, CACHE_INVALID_REG, DCACHE_INVALID(aligned_addr)); aligned_addr += CACHE_LINE_SIZE; } __DSB(); /* exit critical section */ if (!k_is_in_isr()) { irq_unlock(key); } return 0; } int cache_instr_invd_all(void) { const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); uint32_t ctrl; unsigned int key = 0; syscon_read_reg(dev, CACHE_FUNC_CTRL_REG, &ctrl); /* enter critical section */ if (!k_is_in_isr()) { key = irq_lock(); } ctrl &= ~ICACHE_CLEAN; syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, ctrl); __ISB(); ctrl |= ICACHE_CLEAN; syscon_write_reg(dev, CACHE_FUNC_CTRL_REG, ctrl); __ISB(); /* exit critical section */ if (!k_is_in_isr()) { irq_unlock(key); } return 0; } int cache_instr_invd_range(void *addr, size_t size) { uint32_t aligned_addr, i, n; const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); unsigned int key = 0; if (((uint32_t)addr < CACHED_SRAM_ADDR) || ((uint32_t)addr > CACHED_SRAM_END)) { return 0; } n = get_n_cacheline((uint32_t)addr, size, &aligned_addr); /* enter critical section */ if (!k_is_in_isr()) { key = irq_lock(); } for (i = 0; i < n; i++) { syscon_write_reg(dev, CACHE_INVALID_REG, 0); syscon_write_reg(dev, CACHE_INVALID_REG, ICACHE_INVALID(aligned_addr)); aligned_addr += CACHE_LINE_SIZE; } __DSB(); /* exit critical section */ if (!k_is_in_isr()) { irq_unlock(key); } return 0; } int cache_data_flush_all(void) { return -ENOTSUP; } int cache_data_flush_and_invd_all(void) { return -ENOTSUP; } int cache_data_flush_range(void *addr, size_t size) { ARG_UNUSED(addr); ARG_UNUSED(size); return -ENOTSUP; } int cache_data_flush_and_invd_range(void *addr, size_t size) { ARG_UNUSED(addr); ARG_UNUSED(size); return -ENOTSUP; } int cache_instr_flush_all(void) { return -ENOTSUP; } int cache_instr_flush_and_invd_all(void) { return -ENOTSUP; } int cache_instr_flush_range(void *addr, size_t size) { ARG_UNUSED(addr); ARG_UNUSED(size); return -ENOTSUP; } int cache_instr_flush_and_invd_range(void *addr, size_t size) { ARG_UNUSED(addr); ARG_UNUSED(size); return -ENOTSUP; } #ifdef CONFIG_DCACHE_LINE_SIZE_DETECT size_t cache_data_line_size_get(void) { const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); uint32_t ctrl; syscon_read_reg(dev, CACHE_FUNC_CTRL_REG, &ctrl); return (ctrl & CACHE_ENABLE) ? CACHE_LINE_SIZE : 0; } #endif /* CONFIG_DCACHE_LINE_SIZE_DETECT */ #ifdef CONFIG_ICACHE_LINE_SIZE_DETECT size_t cache_instr_line_size_get(void) { const struct device *const dev = DEVICE_DT_GET(DT_NODELABEL(syscon)); uint32_t ctrl; syscon_read_reg(dev, CACHE_FUNC_CTRL_REG, &ctrl); return (ctrl & CCHE_EANABLE) ? CACHE_LINE_SIZE : 0; } #endif /* CONFIG_ICACHE_LINE_SIZE_DETECT */ |