1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
6 * Serge Semin <Sergey.Semin@baikalelectronics.ru>
8 * Baikal-T1 CM2 L2-cache Control Block driver.
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/bitfield.h>
14 #include <linux/types.h>
15 #include <linux/device.h>
16 #include <linux/platform_device.h>
17 #include <linux/regmap.h>
18 #include <linux/mfd/syscon.h>
19 #include <linux/sysfs.h>
22 #define L2_CTL_REG 0x028
23 #define L2_CTL_DATA_STALL_FLD 0
24 #define L2_CTL_DATA_STALL_MASK GENMASK(1, L2_CTL_DATA_STALL_FLD)
25 #define L2_CTL_TAG_STALL_FLD 2
26 #define L2_CTL_TAG_STALL_MASK GENMASK(3, L2_CTL_TAG_STALL_FLD)
27 #define L2_CTL_WS_STALL_FLD 4
28 #define L2_CTL_WS_STALL_MASK GENMASK(5, L2_CTL_WS_STALL_FLD)
29 #define L2_CTL_SET_CLKRATIO BIT(13)
30 #define L2_CTL_CLKRATIO_LOCK BIT(31)
32 #define L2_CTL_STALL_MIN 0
33 #define L2_CTL_STALL_MAX 3
34 #define L2_CTL_STALL_SET_DELAY_US 1
35 #define L2_CTL_STALL_SET_TOUT_US 1000
38 * struct l2_ctl - Baikal-T1 L2 Control block private data.
39 * @dev: Pointer to the device structure.
40 * @sys_regs: Baikal-T1 System Controller registers map.
45 struct regmap
*sys_regs
;
49 * enum l2_ctl_stall - Baikal-T1 L2-cache-RAM stall identifier.
50 * @L2_WSSTALL: Way-select latency.
51 * @L2_TAGSTALL: Tag latency.
52 * @L2_DATASTALL: Data latency.
61 * struct l2_ctl_device_attribute - Baikal-T1 L2-cache device attribute.
62 * @dev_attr: Actual sysfs device attribute.
63 * @id: L2-cache stall field identifier.
65 struct l2_ctl_device_attribute
{
66 struct device_attribute dev_attr
;
70 #define to_l2_ctl_dev_attr(_dev_attr) \
71 container_of(_dev_attr, struct l2_ctl_device_attribute, dev_attr)
73 #define L2_CTL_ATTR_RW(_name, _prefix, _id) \
74 struct l2_ctl_device_attribute l2_ctl_attr_##_name = \
75 { __ATTR(_name, 0644, _prefix##_show, _prefix##_store), _id }
77 static int l2_ctl_get_latency(struct l2_ctl
*l2
, enum l2_ctl_stall id
, u32
*val
)
82 ret
= regmap_read(l2
->sys_regs
, L2_CTL_REG
, &data
);
88 *val
= FIELD_GET(L2_CTL_WS_STALL_MASK
, data
);
91 *val
= FIELD_GET(L2_CTL_TAG_STALL_MASK
, data
);
94 *val
= FIELD_GET(L2_CTL_DATA_STALL_MASK
, data
);
103 static int l2_ctl_set_latency(struct l2_ctl
*l2
, enum l2_ctl_stall id
, u32 val
)
105 u32 mask
= 0, data
= 0;
108 val
= clamp_val(val
, L2_CTL_STALL_MIN
, L2_CTL_STALL_MAX
);
112 data
= FIELD_PREP(L2_CTL_WS_STALL_MASK
, val
);
113 mask
= L2_CTL_WS_STALL_MASK
;
116 data
= FIELD_PREP(L2_CTL_TAG_STALL_MASK
, val
);
117 mask
= L2_CTL_TAG_STALL_MASK
;
120 data
= FIELD_PREP(L2_CTL_DATA_STALL_MASK
, val
);
121 mask
= L2_CTL_DATA_STALL_MASK
;
127 data
|= L2_CTL_SET_CLKRATIO
;
128 mask
|= L2_CTL_SET_CLKRATIO
;
130 ret
= regmap_update_bits(l2
->sys_regs
, L2_CTL_REG
, mask
, data
);
134 return regmap_read_poll_timeout(l2
->sys_regs
, L2_CTL_REG
, data
,
135 data
& L2_CTL_CLKRATIO_LOCK
,
136 L2_CTL_STALL_SET_DELAY_US
,
137 L2_CTL_STALL_SET_TOUT_US
);
140 static void l2_ctl_clear_data(void *data
)
142 struct l2_ctl
*l2
= data
;
143 struct platform_device
*pdev
= to_platform_device(l2
->dev
);
145 platform_set_drvdata(pdev
, NULL
);
148 static struct l2_ctl
*l2_ctl_create_data(struct platform_device
*pdev
)
150 struct device
*dev
= &pdev
->dev
;
154 l2
= devm_kzalloc(dev
, sizeof(*l2
), GFP_KERNEL
);
156 return ERR_PTR(-ENOMEM
);
158 ret
= devm_add_action(dev
, l2_ctl_clear_data
, l2
);
160 dev_err(dev
, "Can't add L2 CTL data clear action\n");
165 platform_set_drvdata(pdev
, l2
);
170 static int l2_ctl_find_sys_regs(struct l2_ctl
*l2
)
172 l2
->sys_regs
= syscon_node_to_regmap(l2
->dev
->of_node
->parent
);
173 if (IS_ERR(l2
->sys_regs
)) {
174 dev_err(l2
->dev
, "Couldn't get L2 CTL register map\n");
175 return PTR_ERR(l2
->sys_regs
);
181 static int l2_ctl_of_parse_property(struct l2_ctl
*l2
, enum l2_ctl_stall id
,
182 const char *propname
)
187 if (!of_property_read_u32(l2
->dev
->of_node
, propname
, &data
)) {
188 ret
= l2_ctl_set_latency(l2
, id
, data
);
190 dev_err(l2
->dev
, "Invalid value of '%s'\n", propname
);
196 static int l2_ctl_of_parse(struct l2_ctl
*l2
)
200 ret
= l2_ctl_of_parse_property(l2
, L2_WS_STALL
, "baikal,l2-ws-latency");
204 ret
= l2_ctl_of_parse_property(l2
, L2_TAG_STALL
, "baikal,l2-tag-latency");
208 return l2_ctl_of_parse_property(l2
, L2_DATA_STALL
,
209 "baikal,l2-data-latency");
212 static ssize_t
l2_ctl_latency_show(struct device
*dev
,
213 struct device_attribute
*attr
,
216 struct l2_ctl_device_attribute
*devattr
= to_l2_ctl_dev_attr(attr
);
217 struct l2_ctl
*l2
= dev_get_drvdata(dev
);
221 ret
= l2_ctl_get_latency(l2
, devattr
->id
, &data
);
225 return scnprintf(buf
, PAGE_SIZE
, "%u\n", data
);
228 static ssize_t
l2_ctl_latency_store(struct device
*dev
,
229 struct device_attribute
*attr
,
230 const char *buf
, size_t count
)
232 struct l2_ctl_device_attribute
*devattr
= to_l2_ctl_dev_attr(attr
);
233 struct l2_ctl
*l2
= dev_get_drvdata(dev
);
237 if (kstrtouint(buf
, 0, &data
) < 0)
240 ret
= l2_ctl_set_latency(l2
, devattr
->id
, data
);
247 static L2_CTL_ATTR_RW(l2_ws_latency
, l2_ctl_latency
, L2_WS_STALL
);
248 static L2_CTL_ATTR_RW(l2_tag_latency
, l2_ctl_latency
, L2_TAG_STALL
);
249 static L2_CTL_ATTR_RW(l2_data_latency
, l2_ctl_latency
, L2_DATA_STALL
);
251 static struct attribute
*l2_ctl_sysfs_attrs
[] = {
252 &l2_ctl_attr_l2_ws_latency
.dev_attr
.attr
,
253 &l2_ctl_attr_l2_tag_latency
.dev_attr
.attr
,
254 &l2_ctl_attr_l2_data_latency
.dev_attr
.attr
,
257 ATTRIBUTE_GROUPS(l2_ctl_sysfs
);
259 static void l2_ctl_remove_sysfs(void *data
)
261 struct l2_ctl
*l2
= data
;
263 device_remove_groups(l2
->dev
, l2_ctl_sysfs_groups
);
266 static int l2_ctl_init_sysfs(struct l2_ctl
*l2
)
270 ret
= device_add_groups(l2
->dev
, l2_ctl_sysfs_groups
);
272 dev_err(l2
->dev
, "Failed to create L2 CTL sysfs nodes\n");
276 ret
= devm_add_action_or_reset(l2
->dev
, l2_ctl_remove_sysfs
, l2
);
278 dev_err(l2
->dev
, "Can't add L2 CTL sysfs remove action\n");
283 static int l2_ctl_probe(struct platform_device
*pdev
)
288 l2
= l2_ctl_create_data(pdev
);
292 ret
= l2_ctl_find_sys_regs(l2
);
296 ret
= l2_ctl_of_parse(l2
);
300 ret
= l2_ctl_init_sysfs(l2
);
307 static const struct of_device_id l2_ctl_of_match
[] = {
308 { .compatible
= "baikal,bt1-l2-ctl" },
311 MODULE_DEVICE_TABLE(of
, l2_ctl_of_match
);
313 static struct platform_driver l2_ctl_driver
= {
314 .probe
= l2_ctl_probe
,
316 .name
= "bt1-l2-ctl",
317 .of_match_table
= l2_ctl_of_match
320 module_platform_driver(l2_ctl_driver
);
322 MODULE_AUTHOR("Serge Semin <Sergey.Semin@baikalelectronics.ru>");
323 MODULE_DESCRIPTION("Baikal-T1 L2-cache driver");
324 MODULE_LICENSE("GPL v2");