blob: 8f5455e5195741a50d31a7db4369445f03f38d9d [file] [log] [blame]
Rick Chen842d5802018-11-07 09:34:06 +08001// 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 Chen05a684e2019-08-28 18:46:09 +08008#include <dm.h>
9#include <dm/uclass-internal.h>
10#include <cache.h>
Rick Chen842d5802018-11-07 09:34:06 +080011
Lukas Auer6280e322019-01-04 01:37:29 +010012void 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
24void flush_dcache_range(unsigned long start, unsigned long end)
25{
26 flush_dcache_all();
27}
28
29void invalidate_dcache_range(unsigned long start, unsigned long end)
30{
31 flush_dcache_all();
32}
33
Rick Chen842d5802018-11-07 09:34:06 +080034void icache_enable(void)
35{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040036#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080037#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +080038 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
47void icache_disable(void)
48{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040049#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080050#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +080051 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
61void dcache_enable(void)
62{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040063#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080064#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen05a684e2019-08-28 18:46:09 +080065 struct udevice *dev = NULL;
66
Rick Chen842d5802018-11-07 09:34:06 +080067 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 Chen05a684e2019-08-28 18:46:09 +080072
73 uclass_find_first_device(UCLASS_CACHE, &dev);
74
75 if (dev)
76 cache_enable(dev);
Rick Chen842d5802018-11-07 09:34:06 +080077#endif
78#endif
79}
80
81void dcache_disable(void)
82{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040083#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080084#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen05a684e2019-08-28 18:46:09 +080085 struct udevice *dev = NULL;
86
Rick Chen842d5802018-11-07 09:34:06 +080087 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 Chen05a684e2019-08-28 18:46:09 +080093
94 uclass_find_first_device(UCLASS_CACHE, &dev);
95
96 if (dev)
97 cache_disable(dev);
Rick Chen842d5802018-11-07 09:34:06 +080098#endif
99#endif
100}
101
102int icache_status(void)
103{
104 int ret = 0;
105
Bin Meng4b284ad2018-12-12 06:12:28 -0800106#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +0800107 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
119int dcache_status(void)
120{
121 int ret = 0;
122
Bin Meng4b284ad2018-12-12 06:12:28 -0800123#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +0800124 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}