1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
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;
69 #define to_l2_ctl_dev_attr(_dev_attr) \
70 container_of(_dev_attr, struct l2_ctl_device_attribute, dev_attr)
72 #define L2_CTL_ATTR_RW(_name, _prefix, _id) \
73 struct l2_ctl_device_attribute l2_ctl_attr_##_name = \
74 { __ATTR(_name, 0644, _prefix##_show, _prefix##_store), _id }
76 static int l2_ctl_get_latency(struct l2_ctl *l2, enum l2_ctl_stall id, u32 *val)
81 ret = regmap_read(l2->sys_regs, L2_CTL_REG, &data);
87 *val = FIELD_GET(L2_CTL_WS_STALL_MASK, data);
90 *val = FIELD_GET(L2_CTL_TAG_STALL_MASK, data);
93 *val = FIELD_GET(L2_CTL_DATA_STALL_MASK, data);
102 static int l2_ctl_set_latency(struct l2_ctl *l2, enum l2_ctl_stall id, u32 val)
104 u32 mask = 0, data = 0;
107 val = clamp_val(val, L2_CTL_STALL_MIN, L2_CTL_STALL_MAX);
111 data = FIELD_PREP(L2_CTL_WS_STALL_MASK, val);
112 mask = L2_CTL_WS_STALL_MASK;
115 data = FIELD_PREP(L2_CTL_TAG_STALL_MASK, val);
116 mask = L2_CTL_TAG_STALL_MASK;
119 data = FIELD_PREP(L2_CTL_DATA_STALL_MASK, val);
120 mask = L2_CTL_DATA_STALL_MASK;
126 data |= L2_CTL_SET_CLKRATIO;
127 mask |= L2_CTL_SET_CLKRATIO;
129 ret = regmap_update_bits(l2->sys_regs, L2_CTL_REG, mask, data);
133 return regmap_read_poll_timeout(l2->sys_regs, L2_CTL_REG, data,
134 data & L2_CTL_CLKRATIO_LOCK,
135 L2_CTL_STALL_SET_DELAY_US,
136 L2_CTL_STALL_SET_TOUT_US);
139 static void l2_ctl_clear_data(void *data)
141 struct l2_ctl *l2 = data;
142 struct platform_device *pdev = to_platform_device(l2->dev);
144 platform_set_drvdata(pdev, NULL);
147 static struct l2_ctl *l2_ctl_create_data(struct platform_device *pdev)
149 struct device *dev = &pdev->dev;
153 l2 = devm_kzalloc(dev, sizeof(*l2), GFP_KERNEL);
155 return ERR_PTR(-ENOMEM);
157 ret = devm_add_action(dev, l2_ctl_clear_data, l2);
159 dev_err(dev, "Can't add L2 CTL data clear action\n");
164 platform_set_drvdata(pdev, l2);
169 static int l2_ctl_find_sys_regs(struct l2_ctl *l2)
171 l2->sys_regs = syscon_node_to_regmap(l2->dev->of_node->parent);
172 if (IS_ERR(l2->sys_regs)) {
173 dev_err(l2->dev, "Couldn't get L2 CTL register map\n");
174 return PTR_ERR(l2->sys_regs);
180 static int l2_ctl_of_parse_property(struct l2_ctl *l2, enum l2_ctl_stall id,
181 const char *propname)
186 if (!of_property_read_u32(l2->dev->of_node, propname, &data)) {
187 ret = l2_ctl_set_latency(l2, id, data);
189 dev_err(l2->dev, "Invalid value of '%s'\n", propname);
195 static int l2_ctl_of_parse(struct l2_ctl *l2)
199 ret = l2_ctl_of_parse_property(l2, L2_WS_STALL, "baikal,l2-ws-latency");
203 ret = l2_ctl_of_parse_property(l2, L2_TAG_STALL, "baikal,l2-tag-latency");
207 return l2_ctl_of_parse_property(l2, L2_DATA_STALL,
208 "baikal,l2-data-latency");
211 static ssize_t l2_ctl_latency_show(struct device *dev,
212 struct device_attribute *attr,
215 struct l2_ctl_device_attribute *devattr = to_l2_ctl_dev_attr(attr);
216 struct l2_ctl *l2 = dev_get_drvdata(dev);
220 ret = l2_ctl_get_latency(l2, devattr->id, &data);
224 return scnprintf(buf, PAGE_SIZE, "%u\n", data);
227 static ssize_t l2_ctl_latency_store(struct device *dev,
228 struct device_attribute *attr,
229 const char *buf, size_t count)
231 struct l2_ctl_device_attribute *devattr = to_l2_ctl_dev_attr(attr);
232 struct l2_ctl *l2 = dev_get_drvdata(dev);
236 if (kstrtouint(buf, 0, &data) < 0)
239 ret = l2_ctl_set_latency(l2, devattr->id, data);
245 static L2_CTL_ATTR_RW(l2_ws_latency, l2_ctl_latency, L2_WS_STALL);
246 static L2_CTL_ATTR_RW(l2_tag_latency, l2_ctl_latency, L2_TAG_STALL);
247 static L2_CTL_ATTR_RW(l2_data_latency, l2_ctl_latency, L2_DATA_STALL);
249 static struct attribute *l2_ctl_sysfs_attrs[] = {
250 &l2_ctl_attr_l2_ws_latency.dev_attr.attr,
251 &l2_ctl_attr_l2_tag_latency.dev_attr.attr,
252 &l2_ctl_attr_l2_data_latency.dev_attr.attr,
255 ATTRIBUTE_GROUPS(l2_ctl_sysfs);
257 static void l2_ctl_remove_sysfs(void *data)
259 struct l2_ctl *l2 = data;
261 device_remove_groups(l2->dev, l2_ctl_sysfs_groups);
264 static int l2_ctl_init_sysfs(struct l2_ctl *l2)
268 ret = device_add_groups(l2->dev, l2_ctl_sysfs_groups);
270 dev_err(l2->dev, "Failed to create L2 CTL sysfs nodes\n");
274 ret = devm_add_action_or_reset(l2->dev, l2_ctl_remove_sysfs, l2);
276 dev_err(l2->dev, "Can't add L2 CTL sysfs remove action\n");
281 static int l2_ctl_probe(struct platform_device *pdev)
286 l2 = l2_ctl_create_data(pdev);
290 ret = l2_ctl_find_sys_regs(l2);
294 ret = l2_ctl_of_parse(l2);
298 ret = l2_ctl_init_sysfs(l2);
305 static const struct of_device_id l2_ctl_of_match[] = {
306 { .compatible = "baikal,bt1-l2-ctl" },
309 MODULE_DEVICE_TABLE(of, l2_ctl_of_match);
311 static struct platform_driver l2_ctl_driver = {
312 .probe = l2_ctl_probe,
314 .name = "bt1-l2-ctl",
315 .of_match_table = l2_ctl_of_match
318 module_platform_driver(l2_ctl_driver);
321 MODULE_DESCRIPTION("Baikal-T1 L2-cache driver");
322 MODULE_LICENSE("GPL v2");