blob: 40d77f671c870caea928ac4f78d73169c497447d [file] [log] [blame]
Rick Chen842d5802018-11-07 09:34:06 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +08003 * Copyright (C) 2023 Andes Technology Corporation
Rick Chen842d5802018-11-07 09:34:06 +08004 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
5 */
6
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +08007#include <asm/csr.h>
8#include <asm/asm.h>
Rick Chen842d5802018-11-07 09:34:06 +08009#include <common.h>
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080010#include <cache.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070011#include <cpu_func.h>
Rick Chen05a684e2019-08-28 18:46:09 +080012#include <dm.h>
13#include <dm/uclass-internal.h>
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080014#include <asm/arch-andes/csr.h>
Rick Chen883275d2019-11-14 13:52:25 +080015
16#ifdef CONFIG_V5L2_CACHE
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080017void enable_caches(void)
Rick Chen883275d2019-11-14 13:52:25 +080018{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080019 struct udevice *dev;
20 int ret;
Rick Chen883275d2019-11-14 13:52:25 +080021
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080022 ret = uclass_get_device_by_driver(UCLASS_CACHE,
23 DM_DRIVER_GET(v5l2_cache),
24 &dev);
25 if (ret) {
26 log_debug("Cannot enable v5l2 cache\n");
27 } else {
28 ret = cache_enable(dev);
29 if (ret)
30 log_debug("v5l2 cache enable failed\n");
31 }
Rick Chen883275d2019-11-14 13:52:25 +080032}
33
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080034static void cache_ops(int (*ops)(struct udevice *dev))
Rick Chen883275d2019-11-14 13:52:25 +080035{
36 struct udevice *dev = NULL;
37
38 uclass_find_first_device(UCLASS_CACHE, &dev);
39
40 if (dev)
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080041 ops(dev);
Rick Chen883275d2019-11-14 13:52:25 +080042}
43#endif
Rick Chen842d5802018-11-07 09:34:06 +080044
Lukas Auer6280e322019-01-04 01:37:29 +010045void flush_dcache_all(void)
46{
Pragnesh Pateld12b55b2020-03-14 19:12:28 +053047#if CONFIG_IS_ENABLED(RISCV_MMODE)
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080048 csr_write(CSR_MCCTLCOMMAND, CCTL_L1D_WBINVAL_ALL);
Rick Chen883275d2019-11-14 13:52:25 +080049#endif
Lukas Auer6280e322019-01-04 01:37:29 +010050}
51
52void flush_dcache_range(unsigned long start, unsigned long end)
53{
54 flush_dcache_all();
55}
56
57void invalidate_dcache_range(unsigned long start, unsigned long end)
58{
59 flush_dcache_all();
60}
61
Rick Chen842d5802018-11-07 09:34:06 +080062void icache_enable(void)
63{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080064#if CONFIG_IS_ENABLED(RISCV_MMODE)
65 asm volatile("csrsi %0, 0x1" :: "i"(CSR_MCACHE_CTL));
66#endif
Rick Chen842d5802018-11-07 09:34:06 +080067}
68
69void icache_disable(void)
70{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080071#if CONFIG_IS_ENABLED(RISCV_MMODE)
72 asm volatile("csrci %0, 0x1" :: "i"(CSR_MCACHE_CTL));
73#endif
Rick Chen842d5802018-11-07 09:34:06 +080074}
75
76void dcache_enable(void)
77{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080078#if CONFIG_IS_ENABLED(RISCV_MMODE)
79 asm volatile("csrsi %0, 0x2" :: "i"(CSR_MCACHE_CTL));
80#endif
81
82#ifdef CONFIG_V5L2_CACHE
83 cache_ops(cache_enable);
84#endif
Rick Chen842d5802018-11-07 09:34:06 +080085}
86
87void dcache_disable(void)
88{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080089#if CONFIG_IS_ENABLED(RISCV_MMODE)
90 asm volatile("csrci %0, 0x2" :: "i"(CSR_MCACHE_CTL));
91#endif
92
93#ifdef CONFIG_V5L2_CACHE
94 cache_ops(cache_disable);
95#endif
Rick Chen842d5802018-11-07 09:34:06 +080096}
97
98int icache_status(void)
99{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +0800100 int ret = 0;
101
102#if CONFIG_IS_ENABLED(RISCV_MMODE)
103 asm volatile (
104 "csrr t1, %1\n\t"
105 "andi %0, t1, 0x01\n\t"
106 : "=r" (ret)
107 : "i"(CSR_MCACHE_CTL)
108 : "memory"
109 );
110#endif
111
112 return !!ret;
Rick Chen842d5802018-11-07 09:34:06 +0800113}
114
115int dcache_status(void)
116{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +0800117 int ret = 0;
118
119#if CONFIG_IS_ENABLED(RISCV_MMODE)
120 asm volatile (
121 "csrr t1, %1\n\t"
122 "andi %0, t1, 0x02\n\t"
123 : "=r" (ret)
124 : "i" (CSR_MCACHE_CTL)
125 : "memory"
126 );
127#endif
128
129 return !!ret;
Rick Chen842d5802018-11-07 09:34:06 +0800130}