blob: 269bb27f75a642bb14f3f54f0c56d245f4ee1f09 [file] [log] [blame]
Rick Chen52923c62018-11-07 09:34:06 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +08003 * Copyright (C) 2023 Andes Technology Corporation
Rick Chen52923c62018-11-07 09:34:06 +08004 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
5 */
6
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +08007#include <asm/csr.h>
8#include <asm/asm.h>
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +08009#include <cache.h>
Simon Glass9edefc22019-11-14 12:57:37 -070010#include <cpu_func.h>
Rick Chen7045ed92019-08-28 18:46:09 +080011#include <dm.h>
12#include <dm/uclass-internal.h>
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080013#include <asm/arch-andes/csr.h>
Rick Chen8ba595b2019-11-14 13:52:25 +080014
15#ifdef CONFIG_V5L2_CACHE
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080016void enable_caches(void)
Rick Chen8ba595b2019-11-14 13:52:25 +080017{
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080018 struct udevice *dev;
19 int ret;
Rick Chen8ba595b2019-11-14 13:52:25 +080020
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080021 ret = uclass_get_device_by_driver(UCLASS_CACHE,
22 DM_DRIVER_GET(v5l2_cache),
23 &dev);
24 if (ret) {
25 log_debug("Cannot enable v5l2 cache\n");
26 } else {
27 ret = cache_enable(dev);
28 if (ret)
29 log_debug("v5l2 cache enable failed\n");
30 }
Rick Chen8ba595b2019-11-14 13:52:25 +080031}
32
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080033static void cache_ops(int (*ops)(struct udevice *dev))
Rick Chen8ba595b2019-11-14 13:52:25 +080034{
35 struct udevice *dev = NULL;
36
37 uclass_find_first_device(UCLASS_CACHE, &dev);
38
39 if (dev)
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080040 ops(dev);
Rick Chen8ba595b2019-11-14 13:52:25 +080041}
42#endif
Rick Chen52923c62018-11-07 09:34:06 +080043
Lukas Auerc9056652019-01-04 01:37:29 +010044void flush_dcache_all(void)
45{
Pragnesh Patel5988bb92020-03-14 19:12:28 +053046#if CONFIG_IS_ENABLED(RISCV_MMODE)
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080047 csr_write(CSR_MCCTLCOMMAND, CCTL_L1D_WBINVAL_ALL);
Rick Chen8ba595b2019-11-14 13:52:25 +080048#endif
Lukas Auerc9056652019-01-04 01:37:29 +010049}
50
51void flush_dcache_range(unsigned long start, unsigned long end)
52{
53 flush_dcache_all();
54}
55
56void invalidate_dcache_range(unsigned long start, unsigned long end)
57{
58 flush_dcache_all();
59}
60
Rick Chen52923c62018-11-07 09:34:06 +080061void icache_enable(void)
62{
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080063#if CONFIG_IS_ENABLED(RISCV_MMODE)
64 asm volatile("csrsi %0, 0x1" :: "i"(CSR_MCACHE_CTL));
65#endif
Rick Chen52923c62018-11-07 09:34:06 +080066}
67
68void icache_disable(void)
69{
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080070#if CONFIG_IS_ENABLED(RISCV_MMODE)
71 asm volatile("csrci %0, 0x1" :: "i"(CSR_MCACHE_CTL));
72#endif
Rick Chen52923c62018-11-07 09:34:06 +080073}
74
75void dcache_enable(void)
76{
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080077#if CONFIG_IS_ENABLED(RISCV_MMODE)
78 asm volatile("csrsi %0, 0x2" :: "i"(CSR_MCACHE_CTL));
79#endif
80
81#ifdef CONFIG_V5L2_CACHE
82 cache_ops(cache_enable);
83#endif
Rick Chen52923c62018-11-07 09:34:06 +080084}
85
86void dcache_disable(void)
87{
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080088#if CONFIG_IS_ENABLED(RISCV_MMODE)
89 asm volatile("csrci %0, 0x2" :: "i"(CSR_MCACHE_CTL));
90#endif
91
92#ifdef CONFIG_V5L2_CACHE
93 cache_ops(cache_disable);
94#endif
Rick Chen52923c62018-11-07 09:34:06 +080095}
96
97int icache_status(void)
98{
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +080099 int ret = 0;
100
101#if CONFIG_IS_ENABLED(RISCV_MMODE)
102 asm volatile (
103 "csrr t1, %1\n\t"
104 "andi %0, t1, 0x01\n\t"
105 : "=r" (ret)
106 : "i"(CSR_MCACHE_CTL)
107 : "memory"
108 );
109#endif
110
111 return !!ret;
Rick Chen52923c62018-11-07 09:34:06 +0800112}
113
114int dcache_status(void)
115{
Yu Chien Peter Lin600a7082023-02-06 16:10:49 +0800116 int ret = 0;
117
118#if CONFIG_IS_ENABLED(RISCV_MMODE)
119 asm volatile (
120 "csrr t1, %1\n\t"
121 "andi %0, t1, 0x02\n\t"
122 : "=r" (ret)
123 : "i" (CSR_MCACHE_CTL)
124 : "memory"
125 );
126#endif
127
128 return !!ret;
Rick Chen52923c62018-11-07 09:34:06 +0800129}