blob: 41de30cc02439f1b1c38930dce5576958ed7218e [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 Chen49cb7062019-08-28 18:46:11 +080011#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 Chen842d5802018-11-07 09:34:06 +080020
Lukas Auer6280e322019-01-04 01:37:29 +010021void flush_dcache_all(void)
22{
Rick Chen49cb7062019-08-28 18:46:11 +080023#ifdef CONFIG_RISCV_NDS_CACHE
24 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
25#endif
Lukas Auer6280e322019-01-04 01:37:29 +010026}
27
28void flush_dcache_range(unsigned long start, unsigned long end)
29{
30 flush_dcache_all();
31}
32
33void invalidate_dcache_range(unsigned long start, unsigned long end)
34{
35 flush_dcache_all();
36}
37
Rick Chen842d5802018-11-07 09:34:06 +080038void icache_enable(void)
39{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040040#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080041#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +080042 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
51void icache_disable(void)
52{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040053#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080054#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +080055 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
65void dcache_enable(void)
66{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040067#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080068#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen05a684e2019-08-28 18:46:09 +080069 struct udevice *dev = NULL;
70
Rick Chen842d5802018-11-07 09:34:06 +080071 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 Chen05a684e2019-08-28 18:46:09 +080076
77 uclass_find_first_device(UCLASS_CACHE, &dev);
78
79 if (dev)
80 cache_enable(dev);
Rick Chen842d5802018-11-07 09:34:06 +080081#endif
82#endif
83}
84
85void dcache_disable(void)
86{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040087#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080088#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen05a684e2019-08-28 18:46:09 +080089 struct udevice *dev = NULL;
90
Rick Chen49cb7062019-08-28 18:46:11 +080091 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
Rick Chen842d5802018-11-07 09:34:06 +080092 asm volatile (
Rick Chen842d5802018-11-07 09:34:06 +080093 "csrr t1, mcache_ctl\n\t"
94 "andi t0, t1, ~0x2\n\t"
95 "csrw mcache_ctl, t0\n\t"
96 );
Rick Chen05a684e2019-08-28 18:46:09 +080097
98 uclass_find_first_device(UCLASS_CACHE, &dev);
99
100 if (dev)
101 cache_disable(dev);
Rick Chen842d5802018-11-07 09:34:06 +0800102#endif
103#endif
104}
105
106int icache_status(void)
107{
108 int ret = 0;
109
Bin Meng4b284ad2018-12-12 06:12:28 -0800110#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +0800111 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
123int dcache_status(void)
124{
125 int ret = 0;
126
Bin Meng4b284ad2018-12-12 06:12:28 -0800127#ifdef CONFIG_RISCV_NDS_CACHE
Rick Chen842d5802018-11-07 09:34:06 +0800128 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}