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