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