Merge tag 'io_uring-5.11-2021-01-16' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / memory / bt1-l2-ctl.c
blob85965fa26e0bbbb50dca26668f4aa25366f0eba1
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020 BAIKAL ELECTRONICS, JSC
5 * Authors:
6 * Serge Semin <Sergey.Semin@baikalelectronics.ru>
8 * Baikal-T1 CM2 L2-cache Control Block driver.
9 */
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>
20 #include <linux/of.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.
42 struct l2_ctl {
43 struct device *dev;
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.
54 enum l2_ctl_stall {
55 L2_WS_STALL,
56 L2_TAG_STALL,
57 L2_DATA_STALL
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;
67 enum l2_ctl_stall id;
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)
79 u32 data = 0;
80 int ret;
82 ret = regmap_read(l2->sys_regs, L2_CTL_REG, &data);
83 if (ret)
84 return ret;
86 switch (id) {
87 case L2_WS_STALL:
88 *val = FIELD_GET(L2_CTL_WS_STALL_MASK, data);
89 break;
90 case L2_TAG_STALL:
91 *val = FIELD_GET(L2_CTL_TAG_STALL_MASK, data);
92 break;
93 case L2_DATA_STALL:
94 *val = FIELD_GET(L2_CTL_DATA_STALL_MASK, data);
95 break;
96 default:
97 return -EINVAL;
100 return 0;
103 static int l2_ctl_set_latency(struct l2_ctl *l2, enum l2_ctl_stall id, u32 val)
105 u32 mask = 0, data = 0;
106 int ret;
108 val = clamp_val(val, L2_CTL_STALL_MIN, L2_CTL_STALL_MAX);
110 switch (id) {
111 case L2_WS_STALL:
112 data = FIELD_PREP(L2_CTL_WS_STALL_MASK, val);
113 mask = L2_CTL_WS_STALL_MASK;
114 break;
115 case L2_TAG_STALL:
116 data = FIELD_PREP(L2_CTL_TAG_STALL_MASK, val);
117 mask = L2_CTL_TAG_STALL_MASK;
118 break;
119 case L2_DATA_STALL:
120 data = FIELD_PREP(L2_CTL_DATA_STALL_MASK, val);
121 mask = L2_CTL_DATA_STALL_MASK;
122 break;
123 default:
124 return -EINVAL;
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);
131 if (ret)
132 return ret;
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;
151 struct l2_ctl *l2;
152 int ret;
154 l2 = devm_kzalloc(dev, sizeof(*l2), GFP_KERNEL);
155 if (!l2)
156 return ERR_PTR(-ENOMEM);
158 ret = devm_add_action(dev, l2_ctl_clear_data, l2);
159 if (ret) {
160 dev_err(dev, "Can't add L2 CTL data clear action\n");
161 return ERR_PTR(ret);
164 l2->dev = dev;
165 platform_set_drvdata(pdev, l2);
167 return 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);
178 return 0;
181 static int l2_ctl_of_parse_property(struct l2_ctl *l2, enum l2_ctl_stall id,
182 const char *propname)
184 int ret = 0;
185 u32 data;
187 if (!of_property_read_u32(l2->dev->of_node, propname, &data)) {
188 ret = l2_ctl_set_latency(l2, id, data);
189 if (ret)
190 dev_err(l2->dev, "Invalid value of '%s'\n", propname);
193 return ret;
196 static int l2_ctl_of_parse(struct l2_ctl *l2)
198 int ret;
200 ret = l2_ctl_of_parse_property(l2, L2_WS_STALL, "baikal,l2-ws-latency");
201 if (ret)
202 return ret;
204 ret = l2_ctl_of_parse_property(l2, L2_TAG_STALL, "baikal,l2-tag-latency");
205 if (ret)
206 return ret;
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,
214 char *buf)
216 struct l2_ctl_device_attribute *devattr = to_l2_ctl_dev_attr(attr);
217 struct l2_ctl *l2 = dev_get_drvdata(dev);
218 u32 data;
219 int ret;
221 ret = l2_ctl_get_latency(l2, devattr->id, &data);
222 if (ret)
223 return ret;
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);
234 u32 data;
235 int ret;
237 if (kstrtouint(buf, 0, &data) < 0)
238 return -EINVAL;
240 ret = l2_ctl_set_latency(l2, devattr->id, data);
241 if (ret)
242 return ret;
244 return count;
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,
255 NULL
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)
268 int ret;
270 ret = device_add_groups(l2->dev, l2_ctl_sysfs_groups);
271 if (ret) {
272 dev_err(l2->dev, "Failed to create L2 CTL sysfs nodes\n");
273 return ret;
276 ret = devm_add_action_or_reset(l2->dev, l2_ctl_remove_sysfs, l2);
277 if (ret)
278 dev_err(l2->dev, "Can't add L2 CTL sysfs remove action\n");
280 return ret;
283 static int l2_ctl_probe(struct platform_device *pdev)
285 struct l2_ctl *l2;
286 int ret;
288 l2 = l2_ctl_create_data(pdev);
289 if (IS_ERR(l2))
290 return PTR_ERR(l2);
292 ret = l2_ctl_find_sys_regs(l2);
293 if (ret)
294 return ret;
296 ret = l2_ctl_of_parse(l2);
297 if (ret)
298 return ret;
300 ret = l2_ctl_init_sysfs(l2);
301 if (ret)
302 return ret;
304 return 0;
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,
315 .driver = {
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");