Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 3 | * Copyright (C) 2023 Andes Technology Corporation |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 4 | * Rick Chen, Andes Technology Corporation <rick@andestech.com> |
| 5 | */ |
| 6 | |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 7 | #include <asm/csr.h> |
| 8 | #include <asm/asm.h> |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 9 | #include <cache.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Rick Chen | 05a684e | 2019-08-28 18:46:09 +0800 | [diff] [blame] | 11 | #include <dm.h> |
| 12 | #include <dm/uclass-internal.h> |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 13 | #include <asm/arch-andes/csr.h> |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 14 | |
Leo Yu-Chi Liang | 5d0bbea | 2024-05-14 17:50:11 +0800 | [diff] [blame] | 15 | #ifdef CONFIG_ANDES_L2_CACHE |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 16 | void enable_caches(void) |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 17 | { |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 18 | struct udevice *dev; |
| 19 | int ret; |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 20 | |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 21 | ret = uclass_get_device_by_driver(UCLASS_CACHE, |
Leo Yu-Chi Liang | 5d0bbea | 2024-05-14 17:50:11 +0800 | [diff] [blame] | 22 | DM_DRIVER_GET(andes_l2_cache), |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 23 | &dev); |
| 24 | if (ret) { |
Leo Yu-Chi Liang | 5d0bbea | 2024-05-14 17:50:11 +0800 | [diff] [blame] | 25 | log_debug("Cannot enable Andes L2 cache\n"); |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 26 | } else { |
| 27 | ret = cache_enable(dev); |
| 28 | if (ret) |
Leo Yu-Chi Liang | 5d0bbea | 2024-05-14 17:50:11 +0800 | [diff] [blame] | 29 | log_debug("Failed to enable Andes L2 cache\n"); |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 30 | } |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 31 | } |
| 32 | |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 33 | static void cache_ops(int (*ops)(struct udevice *dev)) |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 34 | { |
| 35 | struct udevice *dev = NULL; |
| 36 | |
| 37 | uclass_find_first_device(UCLASS_CACHE, &dev); |
| 38 | |
| 39 | if (dev) |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 40 | ops(dev); |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 41 | } |
| 42 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 43 | |
Lukas Auer | 6280e32 | 2019-01-04 01:37:29 +0100 | [diff] [blame] | 44 | void flush_dcache_all(void) |
| 45 | { |
Leo Yu-Chi Liang | eb422ba | 2024-05-28 20:57:50 +0800 | [diff] [blame] | 46 | csr_write(CSR_UCCTLCOMMAND, CCTL_L1D_WBINVAL_ALL); |
Lukas Auer | 6280e32 | 2019-01-04 01:37:29 +0100 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | void flush_dcache_range(unsigned long start, unsigned long end) |
| 50 | { |
| 51 | flush_dcache_all(); |
| 52 | } |
| 53 | |
| 54 | void invalidate_dcache_range(unsigned long start, unsigned long end) |
| 55 | { |
| 56 | flush_dcache_all(); |
| 57 | } |
| 58 | |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 59 | void icache_enable(void) |
| 60 | { |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 61 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
| 62 | asm volatile("csrsi %0, 0x1" :: "i"(CSR_MCACHE_CTL)); |
| 63 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | void icache_disable(void) |
| 67 | { |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 68 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
| 69 | asm volatile("csrci %0, 0x1" :: "i"(CSR_MCACHE_CTL)); |
| 70 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | void dcache_enable(void) |
| 74 | { |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 75 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
| 76 | asm volatile("csrsi %0, 0x2" :: "i"(CSR_MCACHE_CTL)); |
| 77 | #endif |
| 78 | |
Leo Yu-Chi Liang | 5d0bbea | 2024-05-14 17:50:11 +0800 | [diff] [blame] | 79 | #ifdef CONFIG_ANDES_L2_CACHE |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 80 | cache_ops(cache_enable); |
| 81 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | void dcache_disable(void) |
| 85 | { |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 86 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
| 87 | asm volatile("csrci %0, 0x2" :: "i"(CSR_MCACHE_CTL)); |
| 88 | #endif |
| 89 | |
Leo Yu-Chi Liang | 5d0bbea | 2024-05-14 17:50:11 +0800 | [diff] [blame] | 90 | #ifdef CONFIG_ANDES_L2_CACHE |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 91 | cache_ops(cache_disable); |
| 92 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | int icache_status(void) |
| 96 | { |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 97 | int ret = 0; |
| 98 | |
| 99 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
| 100 | asm volatile ( |
| 101 | "csrr t1, %1\n\t" |
| 102 | "andi %0, t1, 0x01\n\t" |
| 103 | : "=r" (ret) |
| 104 | : "i"(CSR_MCACHE_CTL) |
| 105 | : "memory" |
| 106 | ); |
| 107 | #endif |
| 108 | |
| 109 | return !!ret; |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int dcache_status(void) |
| 113 | { |
Yu Chien Peter Lin | b2ccd1c | 2023-02-06 16:10:49 +0800 | [diff] [blame] | 114 | int ret = 0; |
| 115 | |
| 116 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
| 117 | asm volatile ( |
| 118 | "csrr t1, %1\n\t" |
| 119 | "andi %0, t1, 0x02\n\t" |
| 120 | : "=r" (ret) |
| 121 | : "i" (CSR_MCACHE_CTL) |
| 122 | : "memory" |
| 123 | ); |
| 124 | #endif |
| 125 | |
| 126 | return !!ret; |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 127 | } |