blob: 45a4f216b079d14c650c538198aca048f8892c8c [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
Rick Chene118f5e2019-08-28 18:46:06 +08007#include <command.h>
8#include <cache.h>
9#include <dm.h>
Simon Glassf11478f2019-12-28 10:45:07 -070010#include <hang.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Rick Chene118f5e2019-08-28 18:46:06 +080012#include <asm/io.h>
13#include <dm/ofnode.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060014#include <linux/bitops.h>
Rick Chene118f5e2019-08-28 18:46:06 +080015
16struct l2cache {
17 volatile u64 configure;
18 volatile u64 control;
19 volatile u64 hpm0;
20 volatile u64 hpm1;
21 volatile u64 hpm2;
22 volatile u64 hpm3;
23 volatile u64 error_status;
24 volatile u64 ecc_error;
25 volatile u64 cctl_command0;
26 volatile u64 cctl_access_line0;
27 volatile u64 cctl_command1;
28 volatile u64 cctl_access_line1;
29 volatile u64 cctl_command2;
30 volatile u64 cctl_access_line2;
31 volatile u64 cctl_command3;
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +080032 volatile u64 cctl_access_line3;
Rick Chene118f5e2019-08-28 18:46:06 +080033 volatile u64 cctl_status;
34};
35
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +080036/* Configuration register */
37#define MEM_MAP_OFF 20
38#define MEM_MAP_MSK BIT(MEM_MAP_OFF)
39/* offset of v0 memory map (Gen1) */
40static u32 cmd_stride = 0x10;
41static u32 status_stride = 0x0;
42static u32 status_bit_offset = 0x4;
43
Rick Chene118f5e2019-08-28 18:46:06 +080044/* Control Register */
45#define L2_ENABLE 0x1
46/* prefetch */
47#define IPREPETCH_OFF 3
48#define DPREPETCH_OFF 5
49#define IPREPETCH_MSK (3 << IPREPETCH_OFF)
50#define DPREPETCH_MSK (3 << DPREPETCH_OFF)
51/* tag ram */
52#define TRAMOCTL_OFF 8
53#define TRAMICTL_OFF 10
54#define TRAMOCTL_MSK (3 << TRAMOCTL_OFF)
55#define TRAMICTL_MSK BIT(TRAMICTL_OFF)
56/* data ram */
57#define DRAMOCTL_OFF 11
58#define DRAMICTL_OFF 13
59#define DRAMOCTL_MSK (3 << DRAMOCTL_OFF)
60#define DRAMICTL_MSK BIT(DRAMICTL_OFF)
61
62/* CCTL Command Register */
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +080063#define CCTL_CMD_REG(base, hart) ((ulong)(base) + 0x40 + (hart) * (cmd_stride))
Rick Chene118f5e2019-08-28 18:46:06 +080064#define L2_WBINVAL_ALL 0x12
65
66/* CCTL Status Register */
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +080067#define CCTL_STATUS_REG(base, hart) ((ulong)(base) + 0x80 + (hart) * (status_stride))
68#define CCTL_STATUS_MSK(hart) (0xf << ((hart) * (status_bit_offset)))
69#define CCTL_STATUS_IDLE(hart) (0 << ((hart) * (status_bit_offset)))
70#define CCTL_STATUS_PROCESS(hart) (1 << ((hart) * (status_bit_offset)))
71#define CCTL_STATUS_ILLEGAL(hart) (2 << ((hart) * (status_bit_offset)))
Rick Chene118f5e2019-08-28 18:46:06 +080072
73DECLARE_GLOBAL_DATA_PTR;
74
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080075struct andes_l2_plat {
Rick Chene118f5e2019-08-28 18:46:06 +080076 struct l2cache *regs;
77 u32 iprefetch;
78 u32 dprefetch;
Wolfgang Denk62fb2b42021-09-27 17:42:39 +020079 u32 tram_ctl[2];
80 u32 dram_ctl[2];
Rick Chene118f5e2019-08-28 18:46:06 +080081};
82
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080083static int andes_l2_enable(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +080084{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080085 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +080086 volatile struct l2cache *regs = plat->regs;
87
88 if (regs)
89 setbits_le32(&regs->control, L2_ENABLE);
90
91 return 0;
92}
93
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080094static int andes_l2_disable(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +080095{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +080096 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +080097 volatile struct l2cache *regs = plat->regs;
98 u8 hart = gd->arch.boot_hart;
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +080099
Rick Chene118f5e2019-08-28 18:46:06 +0800100 void __iomem *cctlcmd = (void __iomem *)CCTL_CMD_REG(regs, hart);
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +0800101 void __iomem *cctlstatus = (void __iomem *)CCTL_STATUS_REG(regs, hart);
Rick Chene118f5e2019-08-28 18:46:06 +0800102
103 if ((regs) && (readl(&regs->control) & L2_ENABLE)) {
104 writel(L2_WBINVAL_ALL, cctlcmd);
105
Leo Yu-Chi Liangf98a80a2024-05-28 20:49:42 +0800106 while ((readl(cctlstatus) & CCTL_STATUS_MSK(hart))) {
107 if ((readl(cctlstatus) & CCTL_STATUS_ILLEGAL(hart))) {
Rick Chene118f5e2019-08-28 18:46:06 +0800108 printf("L2 flush illegal! hanging...");
109 hang();
110 }
111 }
112 clrbits_le32(&regs->control, L2_ENABLE);
113 }
114
115 return 0;
116}
117
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800118static int andes_l2_of_to_plat(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +0800119{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800120 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800121 struct l2cache *regs;
122
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100123 regs = dev_read_addr_ptr(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800124 plat->regs = regs;
125
126 plat->iprefetch = -EINVAL;
127 plat->dprefetch = -EINVAL;
128 plat->tram_ctl[0] = -EINVAL;
129 plat->dram_ctl[0] = -EINVAL;
130
131 /* Instruction and data fetch prefetch depth */
132 dev_read_u32(dev, "andes,inst-prefetch", &plat->iprefetch);
133 dev_read_u32(dev, "andes,data-prefetch", &plat->dprefetch);
134
135 /* Set tag RAM and data RAM setup and output cycle */
136 dev_read_u32_array(dev, "andes,tag-ram-ctl", plat->tram_ctl, 2);
137 dev_read_u32_array(dev, "andes,data-ram-ctl", plat->dram_ctl, 2);
138
139 return 0;
140}
141
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800142static int andes_l2_probe(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +0800143{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800144 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800145 struct l2cache *regs = plat->regs;
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800146 u32 cfg_val, ctl_val;
Rick Chene118f5e2019-08-28 18:46:06 +0800147
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800148 cfg_val = readl(&regs->configure);
Rick Chene118f5e2019-08-28 18:46:06 +0800149 ctl_val = readl(&regs->control);
150
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800151 /* If true, v1 memory map (Gen2) */
152 if (cfg_val & MEM_MAP_MSK) {
153 cmd_stride = 0x1000;
154 status_stride = 0x1000;
155 status_bit_offset = 0x0;
156 }
157
158 ctl_val |= L2_ENABLE;
Rick Chene118f5e2019-08-28 18:46:06 +0800159
160 if (plat->iprefetch != -EINVAL) {
161 ctl_val &= ~(IPREPETCH_MSK);
162 ctl_val |= (plat->iprefetch << IPREPETCH_OFF);
163 }
164
165 if (plat->dprefetch != -EINVAL) {
166 ctl_val &= ~(DPREPETCH_MSK);
167 ctl_val |= (plat->dprefetch << DPREPETCH_OFF);
168 }
169
170 if (plat->tram_ctl[0] != -EINVAL) {
171 ctl_val &= ~(TRAMOCTL_MSK | TRAMICTL_MSK);
172 ctl_val |= plat->tram_ctl[0] << TRAMOCTL_OFF;
173 ctl_val |= plat->tram_ctl[1] << TRAMICTL_OFF;
174 }
175
176 if (plat->dram_ctl[0] != -EINVAL) {
177 ctl_val &= ~(DRAMOCTL_MSK | DRAMICTL_MSK);
178 ctl_val |= plat->dram_ctl[0] << DRAMOCTL_OFF;
179 ctl_val |= plat->dram_ctl[1] << DRAMICTL_OFF;
180 }
181
182 writel(ctl_val, &regs->control);
183
184 return 0;
185}
186
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800187static const struct udevice_id andes_l2_cache_ids[] = {
Yu Chien Peter Lin5cc6b3f2023-02-06 16:10:48 +0800188 { .compatible = "cache" },
Rick Chene118f5e2019-08-28 18:46:06 +0800189 {}
190};
191
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800192static const struct cache_ops andes_l2_cache_ops = {
193 .enable = andes_l2_enable,
194 .disable = andes_l2_disable,
Rick Chene118f5e2019-08-28 18:46:06 +0800195};
196
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800197U_BOOT_DRIVER(andes_l2_cache) = {
198 .name = "andes_l2_cache",
Rick Chene118f5e2019-08-28 18:46:06 +0800199 .id = UCLASS_CACHE,
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800200 .of_match = andes_l2_cache_ids,
201 .of_to_plat = andes_l2_of_to_plat,
202 .probe = andes_l2_probe,
203 .plat_auto = sizeof(struct andes_l2_plat),
204 .ops = &andes_l2_cache_ops,
Rick Chene118f5e2019-08-28 18:46:06 +0800205 .flags = DM_FLAG_PRE_RELOC,
206};