blob: bc6f7ed7c1ea8b7af19080bd73a1d45ff15f5483 [file] [log] [blame]
Rick Chene118f5e2019-08-28 18:46:06 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Copyright (C) 2019 Andes Technology Corporation
4 * Rick Chen, Andes Technology Corporation <rick@andestech.com>
5 */
6
Tom Riniabb9a042024-05-18 20:20:43 -06007#include <common.h>
Rick Chene118f5e2019-08-28 18:46:06 +08008#include <command.h>
9#include <cache.h>
10#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -070011#include <hang.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Rick Chene118f5e2019-08-28 18:46:06 +080013#include <asm/io.h>
14#include <dm/ofnode.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060015#include <linux/bitops.h>
Rick Chene118f5e2019-08-28 18:46:06 +080016
17struct l2cache {
18 volatile u64 configure;
19 volatile u64 control;
20 volatile u64 hpm0;
21 volatile u64 hpm1;
22 volatile u64 hpm2;
23 volatile u64 hpm3;
24 volatile u64 error_status;
25 volatile u64 ecc_error;
26 volatile u64 cctl_command0;
27 volatile u64 cctl_access_line0;
28 volatile u64 cctl_command1;
29 volatile u64 cctl_access_line1;
30 volatile u64 cctl_command2;
31 volatile u64 cctl_access_line2;
32 volatile u64 cctl_command3;
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +080033 volatile u64 cctl_access_line3;
Rick Chene118f5e2019-08-28 18:46:06 +080034 volatile u64 cctl_status;
35};
36
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +080037/* Configuration register */
38#define MEM_MAP_OFF 20
39#define MEM_MAP_MSK BIT(MEM_MAP_OFF)
40/* offset of v0 memory map (Gen1) */
41static u32 cmd_stride = 0x10;
42static u32 status_stride = 0x0;
43static u32 status_bit_offset = 0x4;
44
Rick Chene118f5e2019-08-28 18:46:06 +080045/* Control Register */
46#define L2_ENABLE 0x1
47/* prefetch */
48#define IPREPETCH_OFF 3
49#define DPREPETCH_OFF 5
50#define IPREPETCH_MSK (3 << IPREPETCH_OFF)
51#define DPREPETCH_MSK (3 << DPREPETCH_OFF)
52/* tag ram */
53#define TRAMOCTL_OFF 8
54#define TRAMICTL_OFF 10
55#define TRAMOCTL_MSK (3 << TRAMOCTL_OFF)
56#define TRAMICTL_MSK BIT(TRAMICTL_OFF)
57/* data ram */
58#define DRAMOCTL_OFF 11
59#define DRAMICTL_OFF 13
60#define DRAMOCTL_MSK (3 << DRAMOCTL_OFF)
61#define DRAMICTL_MSK BIT(DRAMICTL_OFF)
62
63/* CCTL Command Register */
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +080064#define CCTL_CMD_REG(base, hart) ((ulong)(base) + 0x40 + (hart) * (cmd_stride))
Rick Chene118f5e2019-08-28 18:46:06 +080065#define L2_WBINVAL_ALL 0x12
66
67/* CCTL Status Register */
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +080068#define CCTL_STATUS_REG(base, hart) ((ulong)(base) + 0x80 + (hart) * (status_stride))
69#define CCTL_STATUS_MSK(hart) (0xf << ((hart) * (status_bit_offset)))
70#define CCTL_STATUS_IDLE(hart) (0 << ((hart) * (status_bit_offset)))
71#define CCTL_STATUS_PROCESS(hart) (1 << ((hart) * (status_bit_offset)))
72#define CCTL_STATUS_ILLEGAL(hart) (2 << ((hart) * (status_bit_offset)))
Rick Chene118f5e2019-08-28 18:46:06 +080073
74DECLARE_GLOBAL_DATA_PTR;
75
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080076struct andes_l2_plat {
Rick Chene118f5e2019-08-28 18:46:06 +080077 struct l2cache *regs;
78 u32 iprefetch;
79 u32 dprefetch;
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020080 u32 tram_ctl[2];
81 u32 dram_ctl[2];
Rick Chene118f5e2019-08-28 18:46:06 +080082};
83
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080084static int andes_l2_enable(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +080085{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080086 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +080087 volatile struct l2cache *regs = plat->regs;
88
89 if (regs)
90 setbits_le32(&regs->control, L2_ENABLE);
91
92 return 0;
93}
94
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080095static int andes_l2_disable(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +080096{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080097 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +080098 volatile struct l2cache *regs = plat->regs;
99 u8 hart = gd->arch.boot_hart;
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +0800100
Rick Chene118f5e2019-08-28 18:46:06 +0800101 void __iomem *cctlcmd = (void __iomem *)CCTL_CMD_REG(regs, hart);
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +0800102 void __iomem *cctlstatus = (void __iomem *)CCTL_STATUS_REG(regs, hart);
Rick Chene118f5e2019-08-28 18:46:06 +0800103
104 if ((regs) && (readl(&regs->control) & L2_ENABLE)) {
105 writel(L2_WBINVAL_ALL, cctlcmd);
106
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +0800107 while ((readl(cctlstatus) & CCTL_STATUS_MSK(hart))) {
108 if ((readl(cctlstatus) & CCTL_STATUS_ILLEGAL(hart))) {
Rick Chene118f5e2019-08-28 18:46:06 +0800109 printf("L2 flush illegal! hanging...");
110 hang();
111 }
112 }
113 clrbits_le32(&regs->control, L2_ENABLE);
114 }
115
116 return 0;
117}
118
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800119static int andes_l2_of_to_plat(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +0800120{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800121 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800122 struct l2cache *regs;
123
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100124 regs = dev_read_addr_ptr(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800125 plat->regs = regs;
126
127 plat->iprefetch = -EINVAL;
128 plat->dprefetch = -EINVAL;
129 plat->tram_ctl[0] = -EINVAL;
130 plat->dram_ctl[0] = -EINVAL;
131
132 /* Instruction and data fetch prefetch depth */
133 dev_read_u32(dev, "andes,inst-prefetch", &plat->iprefetch);
134 dev_read_u32(dev, "andes,data-prefetch", &plat->dprefetch);
135
136 /* Set tag RAM and data RAM setup and output cycle */
137 dev_read_u32_array(dev, "andes,tag-ram-ctl", plat->tram_ctl, 2);
138 dev_read_u32_array(dev, "andes,data-ram-ctl", plat->dram_ctl, 2);
139
140 return 0;
141}
142
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800143static int andes_l2_probe(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +0800144{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800145 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800146 struct l2cache *regs = plat->regs;
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800147 u32 cfg_val, ctl_val;
Rick Chene118f5e2019-08-28 18:46:06 +0800148
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800149 cfg_val = readl(&regs->configure);
Rick Chene118f5e2019-08-28 18:46:06 +0800150 ctl_val = readl(&regs->control);
151
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800152 /* If true, v1 memory map (Gen2) */
153 if (cfg_val & MEM_MAP_MSK) {
154 cmd_stride = 0x1000;
155 status_stride = 0x1000;
156 status_bit_offset = 0x0;
157 }
158
159 ctl_val |= L2_ENABLE;
Rick Chene118f5e2019-08-28 18:46:06 +0800160
161 if (plat->iprefetch != -EINVAL) {
162 ctl_val &= ~(IPREPETCH_MSK);
163 ctl_val |= (plat->iprefetch << IPREPETCH_OFF);
164 }
165
166 if (plat->dprefetch != -EINVAL) {
167 ctl_val &= ~(DPREPETCH_MSK);
168 ctl_val |= (plat->dprefetch << DPREPETCH_OFF);
169 }
170
171 if (plat->tram_ctl[0] != -EINVAL) {
172 ctl_val &= ~(TRAMOCTL_MSK | TRAMICTL_MSK);
173 ctl_val |= plat->tram_ctl[0] << TRAMOCTL_OFF;
174 ctl_val |= plat->tram_ctl[1] << TRAMICTL_OFF;
175 }
176
177 if (plat->dram_ctl[0] != -EINVAL) {
178 ctl_val &= ~(DRAMOCTL_MSK | DRAMICTL_MSK);
179 ctl_val |= plat->dram_ctl[0] << DRAMOCTL_OFF;
180 ctl_val |= plat->dram_ctl[1] << DRAMICTL_OFF;
181 }
182
183 writel(ctl_val, &regs->control);
184
185 return 0;
186}
187
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800188static const struct udevice_id andes_l2_cache_ids[] = {
Yu Chien Peter Lin5cc6b3f2023-02-06 16:10:48 +0800189 { .compatible = "cache" },
Rick Chene118f5e2019-08-28 18:46:06 +0800190 {}
191};
192
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800193static const struct cache_ops andes_l2_cache_ops = {
194 .enable = andes_l2_enable,
195 .disable = andes_l2_disable,
Rick Chene118f5e2019-08-28 18:46:06 +0800196};
197
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800198U_BOOT_DRIVER(andes_l2_cache) = {
199 .name = "andes_l2_cache",
Rick Chene118f5e2019-08-28 18:46:06 +0800200 .id = UCLASS_CACHE,
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800201 .of_match = andes_l2_cache_ids,
202 .of_to_plat = andes_l2_of_to_plat,
203 .probe = andes_l2_probe,
204 .plat_auto = sizeof(struct andes_l2_plat),
205 .ops = &andes_l2_cache_ops,
Rick Chene118f5e2019-08-28 18:46:06 +0800206 .flags = DM_FLAG_PRE_RELOC,
207};