blob: bb57498d75adab96dbc3c2c7f9e1a10a2261412e [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>
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +08009#include <cache.h>
Simon Glass1d91ba72019-11-14 12:57:37 -070010#include <cpu_func.h>
Rick Chen05a684e2019-08-28 18:46:09 +080011#include <dm.h>
12#include <dm/uclass-internal.h>
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080013#include <asm/arch-andes/csr.h>
Rick Chen883275d2019-11-14 13:52:25 +080014
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080015#ifdef CONFIG_ANDES_L2_CACHE
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080016void enable_caches(void)
Rick Chen883275d2019-11-14 13:52:25 +080017{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080018 struct udevice *dev;
19 int ret;
Rick Chen883275d2019-11-14 13:52:25 +080020
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080021 ret = uclass_get_device_by_driver(UCLASS_CACHE,
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080022 DM_DRIVER_GET(andes_l2_cache),
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080023 &dev);
24 if (ret) {
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080025 log_debug("Cannot enable Andes L2 cache\n");
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080026 } else {
27 ret = cache_enable(dev);
28 if (ret)
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080029 log_debug("Failed to enable Andes L2 cache\n");
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080030 }
Rick Chen883275d2019-11-14 13:52:25 +080031}
32
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080033static void cache_ops(int (*ops)(struct udevice *dev))
Rick Chen883275d2019-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 Linb2ccd1c2023-02-06 16:10:49 +080040 ops(dev);
Rick Chen883275d2019-11-14 13:52:25 +080041}
42#endif
Rick Chen842d5802018-11-07 09:34:06 +080043
Lukas Auer6280e322019-01-04 01:37:29 +010044void flush_dcache_all(void)
45{
Leo Yu-Chi Liangeb422ba2024-05-28 20:57:50 +080046 csr_write(CSR_UCCTLCOMMAND, CCTL_L1D_WBINVAL_ALL);
Lukas Auer6280e322019-01-04 01:37:29 +010047}
48
49void flush_dcache_range(unsigned long start, unsigned long end)
50{
51 flush_dcache_all();
52}
53
54void invalidate_dcache_range(unsigned long start, unsigned long end)
55{
56 flush_dcache_all();
57}
58
Rick Chen842d5802018-11-07 09:34:06 +080059void icache_enable(void)
60{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080061#if CONFIG_IS_ENABLED(RISCV_MMODE)
62 asm volatile("csrsi %0, 0x1" :: "i"(CSR_MCACHE_CTL));
63#endif
Rick Chen842d5802018-11-07 09:34:06 +080064}
65
66void icache_disable(void)
67{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080068#if CONFIG_IS_ENABLED(RISCV_MMODE)
69 asm volatile("csrci %0, 0x1" :: "i"(CSR_MCACHE_CTL));
70#endif
Rick Chen842d5802018-11-07 09:34:06 +080071}
72
73void dcache_enable(void)
74{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080075#if CONFIG_IS_ENABLED(RISCV_MMODE)
76 asm volatile("csrsi %0, 0x2" :: "i"(CSR_MCACHE_CTL));
77#endif
78
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080079#ifdef CONFIG_ANDES_L2_CACHE
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080080 cache_ops(cache_enable);
81#endif
Rick Chen842d5802018-11-07 09:34:06 +080082}
83
84void dcache_disable(void)
85{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080086#if CONFIG_IS_ENABLED(RISCV_MMODE)
87 asm volatile("csrci %0, 0x2" :: "i"(CSR_MCACHE_CTL));
88#endif
89
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080090#ifdef CONFIG_ANDES_L2_CACHE
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080091 cache_ops(cache_disable);
92#endif
Rick Chen842d5802018-11-07 09:34:06 +080093}
94
95int icache_status(void)
96{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +080097 int ret = 0;
98
99#if CONFIG_IS_ENABLED(RISCV_MMODE)
100 asm volatile (
101 "csrr t1, %1\n\t"
102 "andi %0, t1, 0x01\n\t"
103 : "=r" (ret)
104 : "i"(CSR_MCACHE_CTL)
105 : "memory"
106 );
107#endif
108
109 return !!ret;
Rick Chen842d5802018-11-07 09:34:06 +0800110}
111
112int dcache_status(void)
113{
Yu Chien Peter Linb2ccd1c2023-02-06 16:10:49 +0800114 int ret = 0;
115
116#if CONFIG_IS_ENABLED(RISCV_MMODE)
117 asm volatile (
118 "csrr t1, %1\n\t"
119 "andi %0, t1, 0x02\n\t"
120 : "=r" (ret)
121 : "i" (CSR_MCACHE_CTL)
122 : "memory"
123 );
124#endif
125
126 return !!ret;
Rick Chen842d5802018-11-07 09:34:06 +0800127}