Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Copyright (C) 2017 Andes Technology Corporation |
| 4 | * Rick Chen, Andes Technology Corporation <rick@andestech.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 8 | #include <cpu_func.h> |
Rick Chen | 05a684e | 2019-08-28 18:46:09 +0800 | [diff] [blame] | 9 | #include <dm.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <asm/cache.h> |
Rick Chen | 05a684e | 2019-08-28 18:46:09 +0800 | [diff] [blame] | 11 | #include <dm/uclass-internal.h> |
| 12 | #include <cache.h> |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 13 | #include <asm/csr.h> |
| 14 | |
| 15 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 16 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 17 | /* mcctlcommand */ |
| 18 | #define CCTL_REG_MCCTLCOMMAND_NUM 0x7cc |
| 19 | |
| 20 | /* D-cache operation */ |
| 21 | #define CCTL_L1D_WBINVAL_ALL 6 |
| 22 | #endif |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 23 | #endif |
| 24 | |
| 25 | #ifdef CONFIG_V5L2_CACHE |
| 26 | static void _cache_enable(void) |
| 27 | { |
| 28 | struct udevice *dev = NULL; |
| 29 | |
| 30 | uclass_find_first_device(UCLASS_CACHE, &dev); |
| 31 | |
| 32 | if (dev) |
| 33 | cache_enable(dev); |
| 34 | } |
| 35 | |
| 36 | static void _cache_disable(void) |
| 37 | { |
| 38 | struct udevice *dev = NULL; |
| 39 | |
| 40 | uclass_find_first_device(UCLASS_CACHE, &dev); |
| 41 | |
| 42 | if (dev) |
| 43 | cache_disable(dev); |
| 44 | } |
| 45 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 46 | |
Lukas Auer | 6280e32 | 2019-01-04 01:37:29 +0100 | [diff] [blame] | 47 | void flush_dcache_all(void) |
| 48 | { |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 49 | #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 50 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 51 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 52 | csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL); |
| 53 | #endif |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 54 | #endif |
| 55 | #endif |
Lukas Auer | 6280e32 | 2019-01-04 01:37:29 +0100 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void flush_dcache_range(unsigned long start, unsigned long end) |
| 59 | { |
| 60 | flush_dcache_all(); |
| 61 | } |
| 62 | |
| 63 | void invalidate_dcache_range(unsigned long start, unsigned long end) |
| 64 | { |
| 65 | flush_dcache_all(); |
| 66 | } |
| 67 | |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 68 | void icache_enable(void) |
| 69 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 70 | #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 71 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 72 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 73 | asm volatile ( |
| 74 | "csrr t1, mcache_ctl\n\t" |
| 75 | "ori t0, t1, 0x1\n\t" |
| 76 | "csrw mcache_ctl, t0\n\t" |
| 77 | ); |
| 78 | #endif |
| 79 | #endif |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 80 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 81 | } |
| 82 | |
| 83 | void icache_disable(void) |
| 84 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 85 | #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 86 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 87 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 88 | asm volatile ( |
| 89 | "fence.i\n\t" |
| 90 | "csrr t1, mcache_ctl\n\t" |
| 91 | "andi t0, t1, ~0x1\n\t" |
| 92 | "csrw mcache_ctl, t0\n\t" |
| 93 | ); |
| 94 | #endif |
| 95 | #endif |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 96 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void dcache_enable(void) |
| 100 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 101 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 102 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 103 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 104 | asm volatile ( |
| 105 | "csrr t1, mcache_ctl\n\t" |
| 106 | "ori t0, t1, 0x2\n\t" |
| 107 | "csrw mcache_ctl, t0\n\t" |
| 108 | ); |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 109 | #endif |
| 110 | #ifdef CONFIG_V5L2_CACHE |
| 111 | _cache_enable(); |
| 112 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 113 | #endif |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | void dcache_disable(void) |
| 118 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 119 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 120 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 121 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 122 | csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL); |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 123 | asm volatile ( |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 124 | "csrr t1, mcache_ctl\n\t" |
| 125 | "andi t0, t1, ~0x2\n\t" |
| 126 | "csrw mcache_ctl, t0\n\t" |
| 127 | ); |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 128 | #endif |
| 129 | #ifdef CONFIG_V5L2_CACHE |
| 130 | _cache_disable(); |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 131 | #endif |
| 132 | #endif |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 133 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | int icache_status(void) |
| 137 | { |
| 138 | int ret = 0; |
| 139 | |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 140 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 141 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 142 | asm volatile ( |
| 143 | "csrr t1, mcache_ctl\n\t" |
| 144 | "andi %0, t1, 0x01\n\t" |
| 145 | : "=r" (ret) |
| 146 | : |
| 147 | : "memory" |
| 148 | ); |
| 149 | #endif |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 150 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 151 | |
| 152 | return ret; |
| 153 | } |
| 154 | |
| 155 | int dcache_status(void) |
| 156 | { |
| 157 | int ret = 0; |
| 158 | |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 159 | #ifdef CONFIG_RISCV_NDS_CACHE |
Pragnesh Patel | d12b55b | 2020-03-14 19:12:28 +0530 | [diff] [blame] | 160 | #if CONFIG_IS_ENABLED(RISCV_MMODE) |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 161 | asm volatile ( |
| 162 | "csrr t1, mcache_ctl\n\t" |
| 163 | "andi %0, t1, 0x02\n\t" |
| 164 | : "=r" (ret) |
| 165 | : |
| 166 | : "memory" |
| 167 | ); |
| 168 | #endif |
Rick Chen | 883275d | 2019-11-14 13:52:25 +0800 | [diff] [blame] | 169 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 170 | |
| 171 | return ret; |
| 172 | } |