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> |
| 10 | #include <dm/uclass-internal.h> |
| 11 | #include <cache.h> |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 12 | #include <asm/csr.h> |
| 13 | |
| 14 | #ifdef CONFIG_RISCV_NDS_CACHE |
| 15 | /* mcctlcommand */ |
| 16 | #define CCTL_REG_MCCTLCOMMAND_NUM 0x7cc |
| 17 | |
| 18 | /* D-cache operation */ |
| 19 | #define CCTL_L1D_WBINVAL_ALL 6 |
| 20 | #endif |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 21 | |
Lukas Auer | 6280e32 | 2019-01-04 01:37:29 +0100 | [diff] [blame] | 22 | void flush_dcache_all(void) |
| 23 | { |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 24 | #ifdef CONFIG_RISCV_NDS_CACHE |
| 25 | csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL); |
| 26 | #endif |
Lukas Auer | 6280e32 | 2019-01-04 01:37:29 +0100 | [diff] [blame] | 27 | } |
| 28 | |
| 29 | void flush_dcache_range(unsigned long start, unsigned long end) |
| 30 | { |
| 31 | flush_dcache_all(); |
| 32 | } |
| 33 | |
| 34 | void invalidate_dcache_range(unsigned long start, unsigned long end) |
| 35 | { |
| 36 | flush_dcache_all(); |
| 37 | } |
| 38 | |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 39 | void icache_enable(void) |
| 40 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 41 | #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 42 | #ifdef CONFIG_RISCV_NDS_CACHE |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 43 | asm volatile ( |
| 44 | "csrr t1, mcache_ctl\n\t" |
| 45 | "ori t0, t1, 0x1\n\t" |
| 46 | "csrw mcache_ctl, t0\n\t" |
| 47 | ); |
| 48 | #endif |
| 49 | #endif |
| 50 | } |
| 51 | |
| 52 | void icache_disable(void) |
| 53 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 54 | #if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 55 | #ifdef CONFIG_RISCV_NDS_CACHE |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 56 | asm volatile ( |
| 57 | "fence.i\n\t" |
| 58 | "csrr t1, mcache_ctl\n\t" |
| 59 | "andi t0, t1, ~0x1\n\t" |
| 60 | "csrw mcache_ctl, t0\n\t" |
| 61 | ); |
| 62 | #endif |
| 63 | #endif |
| 64 | } |
| 65 | |
| 66 | void dcache_enable(void) |
| 67 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 68 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 69 | #ifdef CONFIG_RISCV_NDS_CACHE |
Rick Chen | 05a684e | 2019-08-28 18:46:09 +0800 | [diff] [blame] | 70 | struct udevice *dev = NULL; |
| 71 | |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 72 | asm volatile ( |
| 73 | "csrr t1, mcache_ctl\n\t" |
| 74 | "ori t0, t1, 0x2\n\t" |
| 75 | "csrw mcache_ctl, t0\n\t" |
| 76 | ); |
Rick Chen | 05a684e | 2019-08-28 18:46:09 +0800 | [diff] [blame] | 77 | |
| 78 | uclass_find_first_device(UCLASS_CACHE, &dev); |
| 79 | |
| 80 | if (dev) |
| 81 | cache_enable(dev); |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 82 | #endif |
| 83 | #endif |
| 84 | } |
| 85 | |
| 86 | void dcache_disable(void) |
| 87 | { |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 88 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 89 | #ifdef CONFIG_RISCV_NDS_CACHE |
Rick Chen | 05a684e | 2019-08-28 18:46:09 +0800 | [diff] [blame] | 90 | struct udevice *dev = NULL; |
| 91 | |
Rick Chen | 49cb706 | 2019-08-28 18:46:11 +0800 | [diff] [blame] | 92 | csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL); |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 93 | asm volatile ( |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 94 | "csrr t1, mcache_ctl\n\t" |
| 95 | "andi t0, t1, ~0x2\n\t" |
| 96 | "csrw mcache_ctl, t0\n\t" |
| 97 | ); |
Rick Chen | 05a684e | 2019-08-28 18:46:09 +0800 | [diff] [blame] | 98 | |
| 99 | uclass_find_first_device(UCLASS_CACHE, &dev); |
| 100 | |
| 101 | if (dev) |
| 102 | cache_disable(dev); |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 103 | #endif |
| 104 | #endif |
| 105 | } |
| 106 | |
| 107 | int icache_status(void) |
| 108 | { |
| 109 | int ret = 0; |
| 110 | |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 111 | #ifdef CONFIG_RISCV_NDS_CACHE |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 112 | asm volatile ( |
| 113 | "csrr t1, mcache_ctl\n\t" |
| 114 | "andi %0, t1, 0x01\n\t" |
| 115 | : "=r" (ret) |
| 116 | : |
| 117 | : "memory" |
| 118 | ); |
| 119 | #endif |
| 120 | |
| 121 | return ret; |
| 122 | } |
| 123 | |
| 124 | int dcache_status(void) |
| 125 | { |
| 126 | int ret = 0; |
| 127 | |
Bin Meng | 4b284ad | 2018-12-12 06:12:28 -0800 | [diff] [blame] | 128 | #ifdef CONFIG_RISCV_NDS_CACHE |
Rick Chen | 842d580 | 2018-11-07 09:34:06 +0800 | [diff] [blame] | 129 | asm volatile ( |
| 130 | "csrr t1, mcache_ctl\n\t" |
| 131 | "andi %0, t1, 0x02\n\t" |
| 132 | : "=r" (ret) |
| 133 | : |
| 134 | : "memory" |
| 135 | ); |
| 136 | #endif |
| 137 | |
| 138 | return ret; |
| 139 | } |