blob: eda07d3f29e873ad036ccfc751051e75e6937aa3 [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
7#include <common.h>
8#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;
33 volatile u64 cctl_access_line4;
34 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
76struct v5l2_plat {
77 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
84static int v5l2_enable(struct udevice *dev)
85{
Simon Glassfa20e932020-12-03 16:55:20 -070086 struct v5l2_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
95static int v5l2_disable(struct udevice *dev)
96{
Simon Glassfa20e932020-12-03 16:55:20 -070097 struct v5l2_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;
100 void __iomem *cctlcmd = (void __iomem *)CCTL_CMD_REG(regs, hart);
101
102 if ((regs) && (readl(&regs->control) & L2_ENABLE)) {
103 writel(L2_WBINVAL_ALL, cctlcmd);
104
105 while ((readl(&regs->cctl_status) & CCTL_STATUS_MSK(hart))) {
106 if ((readl(&regs->cctl_status) & CCTL_STATUS_ILLEGAL(hart))) {
107 printf("L2 flush illegal! hanging...");
108 hang();
109 }
110 }
111 clrbits_le32(&regs->control, L2_ENABLE);
112 }
113
114 return 0;
115}
116
Simon Glassaad29ae2020-12-03 16:55:21 -0700117static int v5l2_of_to_plat(struct udevice *dev)
Rick Chene118f5e2019-08-28 18:46:06 +0800118{
Simon Glassfa20e932020-12-03 16:55:20 -0700119 struct v5l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800120 struct l2cache *regs;
121
Yu Chien Peter Lin7b170662023-02-06 16:10:53 +0800122 regs = (struct l2cache *)(uintptr_t)dev_read_addr(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800123 plat->regs = regs;
124
125 plat->iprefetch = -EINVAL;
126 plat->dprefetch = -EINVAL;
127 plat->tram_ctl[0] = -EINVAL;
128 plat->dram_ctl[0] = -EINVAL;
129
130 /* Instruction and data fetch prefetch depth */
131 dev_read_u32(dev, "andes,inst-prefetch", &plat->iprefetch);
132 dev_read_u32(dev, "andes,data-prefetch", &plat->dprefetch);
133
134 /* Set tag RAM and data RAM setup and output cycle */
135 dev_read_u32_array(dev, "andes,tag-ram-ctl", plat->tram_ctl, 2);
136 dev_read_u32_array(dev, "andes,data-ram-ctl", plat->dram_ctl, 2);
137
138 return 0;
139}
140
141static int v5l2_probe(struct udevice *dev)
142{
Simon Glassfa20e932020-12-03 16:55:20 -0700143 struct v5l2_plat *plat = dev_get_plat(dev);
Rick Chene118f5e2019-08-28 18:46:06 +0800144 struct l2cache *regs = plat->regs;
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800145 u32 cfg_val, ctl_val;
Rick Chene118f5e2019-08-28 18:46:06 +0800146
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800147 cfg_val = readl(&regs->configure);
Rick Chene118f5e2019-08-28 18:46:06 +0800148 ctl_val = readl(&regs->control);
149
Yu Chien Peter Lin31e1bd02023-02-06 16:10:46 +0800150 /* If true, v1 memory map (Gen2) */
151 if (cfg_val & MEM_MAP_MSK) {
152 cmd_stride = 0x1000;
153 status_stride = 0x1000;
154 status_bit_offset = 0x0;
155 }
156
157 ctl_val |= L2_ENABLE;
Rick Chene118f5e2019-08-28 18:46:06 +0800158
159 if (plat->iprefetch != -EINVAL) {
160 ctl_val &= ~(IPREPETCH_MSK);
161 ctl_val |= (plat->iprefetch << IPREPETCH_OFF);
162 }
163
164 if (plat->dprefetch != -EINVAL) {
165 ctl_val &= ~(DPREPETCH_MSK);
166 ctl_val |= (plat->dprefetch << DPREPETCH_OFF);
167 }
168
169 if (plat->tram_ctl[0] != -EINVAL) {
170 ctl_val &= ~(TRAMOCTL_MSK | TRAMICTL_MSK);
171 ctl_val |= plat->tram_ctl[0] << TRAMOCTL_OFF;
172 ctl_val |= plat->tram_ctl[1] << TRAMICTL_OFF;
173 }
174
175 if (plat->dram_ctl[0] != -EINVAL) {
176 ctl_val &= ~(DRAMOCTL_MSK | DRAMICTL_MSK);
177 ctl_val |= plat->dram_ctl[0] << DRAMOCTL_OFF;
178 ctl_val |= plat->dram_ctl[1] << DRAMICTL_OFF;
179 }
180
181 writel(ctl_val, &regs->control);
182
183 return 0;
184}
185
186static const struct udevice_id v5l2_cache_ids[] = {
Yu Chien Peter Lin5cc6b3f2023-02-06 16:10:48 +0800187 { .compatible = "cache" },
Rick Chene118f5e2019-08-28 18:46:06 +0800188 {}
189};
190
191static const struct cache_ops v5l2_cache_ops = {
192 .enable = v5l2_enable,
193 .disable = v5l2_disable,
194};
195
196U_BOOT_DRIVER(v5l2_cache) = {
197 .name = "v5l2_cache",
198 .id = UCLASS_CACHE,
199 .of_match = v5l2_cache_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700200 .of_to_plat = v5l2_of_to_plat,
Rick Chene118f5e2019-08-28 18:46:06 +0800201 .probe = v5l2_probe,
Simon Glass71fa5b42020-12-03 16:55:18 -0700202 .plat_auto = sizeof(struct v5l2_plat),
Rick Chene118f5e2019-08-28 18:46:06 +0800203 .ops = &v5l2_cache_ops,
204 .flags = DM_FLAG_PRE_RELOC,
205};