blob: 9df629d23ceeb55e552ea48c033572f07d149410 [file] [log] [blame]
Rick Chen52923c62018-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 Glass9edefc22019-11-14 12:57:37 -07008#include <cpu_func.h>
Rick Chen7045ed92019-08-28 18:46:09 +08009#include <dm.h>
10#include <dm/uclass-internal.h>
11#include <cache.h>
Rick Chen61ce84b2019-08-28 18:46:11 +080012#include <asm/csr.h>
13
14#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +053015#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen61ce84b2019-08-28 18:46:11 +080016/* mcctlcommand */
17#define CCTL_REG_MCCTLCOMMAND_NUM 0x7cc
18
19/* D-cache operation */
20#define CCTL_L1D_WBINVAL_ALL 6
21#endif
Rick Chen8ba595b2019-11-14 13:52:25 +080022#endif
23
24#ifdef CONFIG_V5L2_CACHE
25static void _cache_enable(void)
26{
27 struct udevice *dev = NULL;
28
29 uclass_find_first_device(UCLASS_CACHE, &dev);
30
31 if (dev)
32 cache_enable(dev);
33}
34
35static void _cache_disable(void)
36{
37 struct udevice *dev = NULL;
38
39 uclass_find_first_device(UCLASS_CACHE, &dev);
40
41 if (dev)
42 cache_disable(dev);
43}
44#endif
Rick Chen52923c62018-11-07 09:34:06 +080045
Lukas Auerc9056652019-01-04 01:37:29 +010046void flush_dcache_all(void)
47{
Rick Chen8ba595b2019-11-14 13:52:25 +080048#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Rick Chen61ce84b2019-08-28 18:46:11 +080049#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +053050#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen61ce84b2019-08-28 18:46:11 +080051 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
52#endif
Rick Chen8ba595b2019-11-14 13:52:25 +080053#endif
54#endif
Lukas Auerc9056652019-01-04 01:37:29 +010055}
56
57void flush_dcache_range(unsigned long start, unsigned long end)
58{
59 flush_dcache_all();
60}
61
62void invalidate_dcache_range(unsigned long start, unsigned long end)
63{
64 flush_dcache_all();
65}
66
Rick Chen52923c62018-11-07 09:34:06 +080067void icache_enable(void)
68{
Trevor Woerner10015022019-05-03 09:41:00 -040069#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng44fe7952018-12-12 06:12:28 -080070#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +053071#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen52923c62018-11-07 09:34:06 +080072 asm volatile (
73 "csrr t1, mcache_ctl\n\t"
74 "ori t0, t1, 0x1\n\t"
75 "csrw mcache_ctl, t0\n\t"
76 );
77#endif
78#endif
Rick Chen8ba595b2019-11-14 13:52:25 +080079#endif
Rick Chen52923c62018-11-07 09:34:06 +080080}
81
82void icache_disable(void)
83{
Trevor Woerner10015022019-05-03 09:41:00 -040084#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng44fe7952018-12-12 06:12:28 -080085#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +053086#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen52923c62018-11-07 09:34:06 +080087 asm volatile (
88 "fence.i\n\t"
89 "csrr t1, mcache_ctl\n\t"
90 "andi t0, t1, ~0x1\n\t"
91 "csrw mcache_ctl, t0\n\t"
92 );
93#endif
94#endif
Rick Chen8ba595b2019-11-14 13:52:25 +080095#endif
Rick Chen52923c62018-11-07 09:34:06 +080096}
97
98void dcache_enable(void)
99{
Trevor Woerner10015022019-05-03 09:41:00 -0400100#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng44fe7952018-12-12 06:12:28 -0800101#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +0530102#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen52923c62018-11-07 09:34:06 +0800103 asm volatile (
104 "csrr t1, mcache_ctl\n\t"
105 "ori t0, t1, 0x2\n\t"
106 "csrw mcache_ctl, t0\n\t"
107 );
Rick Chen8ba595b2019-11-14 13:52:25 +0800108#endif
109#ifdef CONFIG_V5L2_CACHE
110 _cache_enable();
111#endif
Rick Chen52923c62018-11-07 09:34:06 +0800112#endif
113#endif
114}
115
116void dcache_disable(void)
117{
Trevor Woerner10015022019-05-03 09:41:00 -0400118#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng44fe7952018-12-12 06:12:28 -0800119#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +0530120#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen61ce84b2019-08-28 18:46:11 +0800121 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
Rick Chen52923c62018-11-07 09:34:06 +0800122 asm volatile (
Rick Chen52923c62018-11-07 09:34:06 +0800123 "csrr t1, mcache_ctl\n\t"
124 "andi t0, t1, ~0x2\n\t"
125 "csrw mcache_ctl, t0\n\t"
126 );
Rick Chen8ba595b2019-11-14 13:52:25 +0800127#endif
128#ifdef CONFIG_V5L2_CACHE
129 _cache_disable();
130#endif
Rick Chen52923c62018-11-07 09:34:06 +0800131#endif
132#endif
133}
134
135int icache_status(void)
136{
137 int ret = 0;
138
Bin Meng44fe7952018-12-12 06:12:28 -0800139#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +0530140#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen52923c62018-11-07 09:34:06 +0800141 asm volatile (
142 "csrr t1, mcache_ctl\n\t"
143 "andi %0, t1, 0x01\n\t"
144 : "=r" (ret)
145 :
146 : "memory"
147 );
148#endif
Rick Chen8ba595b2019-11-14 13:52:25 +0800149#endif
Rick Chen52923c62018-11-07 09:34:06 +0800150
151 return ret;
152}
153
154int dcache_status(void)
155{
156 int ret = 0;
157
Bin Meng44fe7952018-12-12 06:12:28 -0800158#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Patel5988bb92020-03-14 19:12:28 +0530159#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen52923c62018-11-07 09:34:06 +0800160 asm volatile (
161 "csrr t1, mcache_ctl\n\t"
162 "andi %0, t1, 0x02\n\t"
163 : "=r" (ret)
164 :
165 : "memory"
166 );
167#endif
Rick Chen8ba595b2019-11-14 13:52:25 +0800168#endif
Rick Chen52923c62018-11-07 09:34:06 +0800169
170 return ret;
171}