blob: 7de8f16852dc61676d2d756a6af76db7d9535269 [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;
32 volatile u64 cctl_access_line4;
33 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;
99 void __iomem *cctlcmd = (void __iomem *)CCTL_CMD_REG(regs, hart);
100
101 if ((regs) && (readl(&regs->control) & L2_ENABLE)) {
102 writel(L2_WBINVAL_ALL, cctlcmd);
103
104 while ((readl(&regs->cctl_status) & CCTL_STATUS_MSK(hart))) {
105 if ((readl(&regs->cctl_status) & CCTL_STATUS_ILLEGAL(hart))) {
106 printf("L2 flush illegal! hanging...");
107 hang();
108 }
109 }
110 clrbits_le32(&regs->control, L2_ENABLE);
111 }
112
113 return 0;
114}
115
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800116static int andes_l2_of_to_plat(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +0800117{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800118 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800119 struct l2cache *regs;
120
Johan Jonker8d5d8e02023-03-13 01:32:04 +0100121 regs = dev_read_addr_ptr(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800122 plat->regs = regs;
123
124 plat->iprefetch = -EINVAL;
125 plat->dprefetch = -EINVAL;
126 plat->tram_ctl[0] = -EINVAL;
127 plat->dram_ctl[0] = -EINVAL;
128
129 /* Instruction and data fetch prefetch depth */
130 dev_read_u32(dev, "andes,inst-prefetch", &plat->iprefetch);
131 dev_read_u32(dev, "andes,data-prefetch", &plat->dprefetch);
132
133 /* Set tag RAM and data RAM setup and output cycle */
134 dev_read_u32_array(dev, "andes,tag-ram-ctl", plat->tram_ctl, 2);
135 dev_read_u32_array(dev, "andes,data-ram-ctl", plat->dram_ctl, 2);
136
137 return 0;
138}
139
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800140static int andes_l2_probe(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +0800141{
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800142 struct andes_l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800143 struct l2cache *regs = plat->regs;
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800144 u32 cfg_val, ctl_val;
Rick Chene118f5e2019-08-28 18:46:06 +0800145
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800146 cfg_val = readl(&regs->configure);
Rick Chene118f5e2019-08-28 18:46:06 +0800147 ctl_val = readl(&regs->control);
148
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800149 /* If true, v1 memory map (Gen2) */
150 if (cfg_val & MEM_MAP_MSK) {
151 cmd_stride = 0x1000;
152 status_stride = 0x1000;
153 status_bit_offset = 0x0;
154 }
155
156 ctl_val |= L2_ENABLE;
Rick Chene118f5e2019-08-28 18:46:06 +0800157
158 if (plat->iprefetch != -EINVAL) {
159 ctl_val &= ~(IPREPETCH_MSK);
160 ctl_val |= (plat->iprefetch << IPREPETCH_OFF);
161 }
162
163 if (plat->dprefetch != -EINVAL) {
164 ctl_val &= ~(DPREPETCH_MSK);
165 ctl_val |= (plat->dprefetch << DPREPETCH_OFF);
166 }
167
168 if (plat->tram_ctl[0] != -EINVAL) {
169 ctl_val &= ~(TRAMOCTL_MSK | TRAMICTL_MSK);
170 ctl_val |= plat->tram_ctl[0] << TRAMOCTL_OFF;
171 ctl_val |= plat->tram_ctl[1] << TRAMICTL_OFF;
172 }
173
174 if (plat->dram_ctl[0] != -EINVAL) {
175 ctl_val &= ~(DRAMOCTL_MSK | DRAMICTL_MSK);
176 ctl_val |= plat->dram_ctl[0] << DRAMOCTL_OFF;
177 ctl_val |= plat->dram_ctl[1] << DRAMICTL_OFF;
178 }
179
180 writel(ctl_val, &regs->control);
181
182 return 0;
183}
184
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800185static const struct udevice_id andes_l2_cache_ids[] = {
Yu Chien Peter Lin5cc6b3f2023-02-06 16:10:48 +0800186 { .compatible = "cache" },
Rick Chene118f5e2019-08-28 18:46:06 +0800187 {}
188};
189
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800190static const struct cache_ops andes_l2_cache_ops = {
191 .enable = andes_l2_enable,
192 .disable = andes_l2_disable,
Rick Chene118f5e2019-08-28 18:46:06 +0800193};
194
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800195U_BOOT_DRIVER(andes_l2_cache) = {
196 .name = "andes_l2_cache",
Rick Chene118f5e2019-08-28 18:46:06 +0800197 .id = UCLASS_CACHE,
Leo Yu-Chi Liang5d0bbea2024-05-14 17:50:11 +0800198 .of_match = andes_l2_cache_ids,
199 .of_to_plat = andes_l2_of_to_plat,
200 .probe = andes_l2_probe,
201 .plat_auto = sizeof(struct andes_l2_plat),
202 .ops = &andes_l2_cache_ops,
Rick Chene118f5e2019-08-28 18:46:06 +0800203 .flags = DM_FLAG_PRE_RELOC,
204};