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