blob: 35f23c748d1b9c939519c8518db7d8eb5851fd87 [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>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <asm/cache.h>
Rick Chen05a684e2019-08-28 18:46:09 +080011#include <dm/uclass-internal.h>
12#include <cache.h>
Rick Chen49cb7062019-08-28 18:46:11 +080013#include <asm/csr.h>
14
15#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +053016#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen49cb7062019-08-28 18:46:11 +080017/* mcctlcommand */
18#define CCTL_REG_MCCTLCOMMAND_NUM 0x7cc
19
20/* D-cache operation */
21#define CCTL_L1D_WBINVAL_ALL 6
22#endif
Rick Chen883275d2019-11-14 13:52:25 +080023#endif
24
25#ifdef CONFIG_V5L2_CACHE
26static void _cache_enable(void)
27{
28 struct udevice *dev = NULL;
29
30 uclass_find_first_device(UCLASS_CACHE, &dev);
31
32 if (dev)
33 cache_enable(dev);
34}
35
36static void _cache_disable(void)
37{
38 struct udevice *dev = NULL;
39
40 uclass_find_first_device(UCLASS_CACHE, &dev);
41
42 if (dev)
43 cache_disable(dev);
44}
45#endif
Rick Chen842d5802018-11-07 09:34:06 +080046
Lukas Auer6280e322019-01-04 01:37:29 +010047void flush_dcache_all(void)
48{
Rick Chen883275d2019-11-14 13:52:25 +080049#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Rick Chen49cb7062019-08-28 18:46:11 +080050#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +053051#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen49cb7062019-08-28 18:46:11 +080052 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
53#endif
Rick Chen883275d2019-11-14 13:52:25 +080054#endif
55#endif
Lukas Auer6280e322019-01-04 01:37:29 +010056}
57
58void flush_dcache_range(unsigned long start, unsigned long end)
59{
60 flush_dcache_all();
61}
62
63void invalidate_dcache_range(unsigned long start, unsigned long end)
64{
65 flush_dcache_all();
66}
67
Rick Chen842d5802018-11-07 09:34:06 +080068void icache_enable(void)
69{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040070#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080071#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +053072#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen842d5802018-11-07 09:34:06 +080073 asm volatile (
74 "csrr t1, mcache_ctl\n\t"
75 "ori t0, t1, 0x1\n\t"
76 "csrw mcache_ctl, t0\n\t"
77 );
78#endif
79#endif
Rick Chen883275d2019-11-14 13:52:25 +080080#endif
Rick Chen842d5802018-11-07 09:34:06 +080081}
82
83void icache_disable(void)
84{
Trevor Woerner43ec7e02019-05-03 09:41:00 -040085#if !CONFIG_IS_ENABLED(SYS_ICACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -080086#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +053087#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen842d5802018-11-07 09:34:06 +080088 asm volatile (
89 "fence.i\n\t"
90 "csrr t1, mcache_ctl\n\t"
91 "andi t0, t1, ~0x1\n\t"
92 "csrw mcache_ctl, t0\n\t"
93 );
94#endif
95#endif
Rick Chen883275d2019-11-14 13:52:25 +080096#endif
Rick Chen842d5802018-11-07 09:34:06 +080097}
98
99void dcache_enable(void)
100{
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400101#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -0800102#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +0530103#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen842d5802018-11-07 09:34:06 +0800104 asm volatile (
105 "csrr t1, mcache_ctl\n\t"
106 "ori t0, t1, 0x2\n\t"
107 "csrw mcache_ctl, t0\n\t"
108 );
Rick Chen883275d2019-11-14 13:52:25 +0800109#endif
110#ifdef CONFIG_V5L2_CACHE
111 _cache_enable();
112#endif
Rick Chen842d5802018-11-07 09:34:06 +0800113#endif
114#endif
115}
116
117void dcache_disable(void)
118{
Trevor Woerner43ec7e02019-05-03 09:41:00 -0400119#if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF)
Bin Meng4b284ad2018-12-12 06:12:28 -0800120#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +0530121#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen49cb7062019-08-28 18:46:11 +0800122 csr_write(CCTL_REG_MCCTLCOMMAND_NUM, CCTL_L1D_WBINVAL_ALL);
Rick Chen842d5802018-11-07 09:34:06 +0800123 asm volatile (
Rick Chen842d5802018-11-07 09:34:06 +0800124 "csrr t1, mcache_ctl\n\t"
125 "andi t0, t1, ~0x2\n\t"
126 "csrw mcache_ctl, t0\n\t"
127 );
Rick Chen883275d2019-11-14 13:52:25 +0800128#endif
129#ifdef CONFIG_V5L2_CACHE
130 _cache_disable();
Rick Chen842d5802018-11-07 09:34:06 +0800131#endif
132#endif
Rick Chen883275d2019-11-14 13:52:25 +0800133#endif
Rick Chen842d5802018-11-07 09:34:06 +0800134}
135
136int icache_status(void)
137{
138 int ret = 0;
139
Bin Meng4b284ad2018-12-12 06:12:28 -0800140#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +0530141#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen842d5802018-11-07 09:34:06 +0800142 asm volatile (
143 "csrr t1, mcache_ctl\n\t"
144 "andi %0, t1, 0x01\n\t"
145 : "=r" (ret)
146 :
147 : "memory"
148 );
149#endif
Rick Chen883275d2019-11-14 13:52:25 +0800150#endif
Rick Chen842d5802018-11-07 09:34:06 +0800151
152 return ret;
153}
154
155int dcache_status(void)
156{
157 int ret = 0;
158
Bin Meng4b284ad2018-12-12 06:12:28 -0800159#ifdef CONFIG_RISCV_NDS_CACHE
Pragnesh Pateld12b55b2020-03-14 19:12:28 +0530160#if CONFIG_IS_ENABLED(RISCV_MMODE)
Rick Chen842d5802018-11-07 09:34:06 +0800161 asm volatile (
162 "csrr t1, mcache_ctl\n\t"
163 "andi %0, t1, 0x02\n\t"
164 : "=r" (ret)
165 :
166 : "memory"
167 );
168#endif
Rick Chen883275d2019-11-14 13:52:25 +0800169#endif
Rick Chen842d5802018-11-07 09:34:06 +0800170
171 return ret;
172}