blob: 1455f2298f74db9276c0469e7fe3ba6bd196e457 [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>
Simon Glass1d91ba72019-11-14 12:57:37 -07008#include <cpu_func.h>
Rick Chen05a684e2019-08-28 18:46:09 +08009#include <dm.h>
10#include <dm/uclass-internal.h>
11#include <cache.h>
Rick Chen49cb7062019-08-28 18:46:11 +080012#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 Chen842d5802018-11-07 09:34:06 +080021
Lukas Auer6280e322019-01-04 01:37:29 +010022void flush_dcache_all(void)
23{
Rick Chen49cb7062019-08-28 18:46:11 +080024#ifdef CONFIG_RISCV_NDS_CACHE
25 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
26#endif
Lukas Auer6280e322019-01-04 01:37:29 +010027}
28
29void flush_dcache_range(unsigned long start, unsigned long end)
30{
31 flush_dcache_all();
32}
33
34void invalidate_dcache_range(unsigned long start, unsigned long end)
35{
36 flush_dcache_all();
37}
38
Rick Chen842d5802018-11-07 09:34:06 +080039void icache_enable(void)
40{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040041#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080042#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +080043 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
52void icache_disable(void)
53{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040054#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080055#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +080056 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
66void dcache_enable(void)
67{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040068#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080069#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen05a684e2019-08-28 18:46:09 +080070 struct udevice *dev = NULL;
71
Rick Chen842d5802018-11-07 09:34:06 +080072 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 Chen05a684e2019-08-28 18:46:09 +080077
78 uclass_find_first_device(UCLASS_CACHE, &dev);
79
80 if (dev)
81 cache_enable(dev);
Rick Chen842d5802018-11-07 09:34:06 +080082#endif
83#endif
84}
85
86void dcache_disable(void)
87{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040088#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080089#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen05a684e2019-08-28 18:46:09 +080090 struct udevice *dev = NULL;
91
Rick Chen49cb7062019-08-28 18:46:11 +080092 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
Rick Chen842d5802018-11-07 09:34:06 +080093 asm volatile (
Rick Chen842d5802018-11-07 09:34:06 +080094 "csrr t1, mcache_ctl\n\t"
95 "andi t0, t1, ~0x2\n\t"
96 "csrw mcache_ctl, t0\n\t"
97 );
Rick Chen05a684e2019-08-28 18:46:09 +080098
99 uclass_find_first_device(UCLASS_CACHE, &dev);
100
101 if (dev)
102 cache_disable(dev);
Rick Chen842d5802018-11-07 09:34:06 +0800103#endif
104#endif
105}
106
107int icache_status(void)
108{
109 int ret = 0;
110
Bin Meng4b284ad2018-12-12 06:12:28 -0800111#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +0800112 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
124int dcache_status(void)
125{
126 int ret = 0;
127
Bin Meng4b284ad2018-12-12 06:12:28 -0800128#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +0800129 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}