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