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