1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2020-2021, The Linux Foundation. All rights reserved.
6 #include <linux/bitfield.h>
8 #include <linux/interconnect-provider.h>
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/of_device.h>
13 #include <linux/platform_device.h>
15 #include <dt-bindings/interconnect/qcom,osm-l3.h>
24 #define LUT_MAX_ENTRIES 40U
25 #define LUT_SRC GENMASK(31, 30)
26 #define LUT_L_VAL GENMASK(7, 0)
29 /* OSM Register offsets */
30 #define REG_ENABLE 0x0
31 #define OSM_LUT_ROW_SIZE 32
32 #define OSM_REG_FREQ_LUT 0x110
33 #define OSM_REG_PERF_STATE 0x920
35 /* EPSS Register offsets */
36 #define EPSS_LUT_ROW_SIZE 4
37 #define EPSS_REG_FREQ_LUT 0x100
38 #define EPSS_REG_PERF_STATE 0x320
40 #define OSM_L3_MAX_LINKS 1
42 #define to_osm_l3_provider(_provider) \
43 container_of(_provider, struct qcom_osm_l3_icc_provider, provider)
45 struct qcom_osm_l3_icc_provider {
47 unsigned int max_state;
48 unsigned int reg_perf_state;
49 unsigned long lut_tables[LUT_MAX_ENTRIES];
50 struct icc_provider provider;
54 * struct qcom_osm_l3_node - Qualcomm specific interconnect nodes
55 * @name: the node name used in debugfs
56 * @links: an array of nodes where we can go next while traversing
57 * @id: a unique node identifier
58 * @num_links: the total number of @links
59 * @buswidth: width of the interconnect between a node and the bus
61 struct qcom_osm_l3_node {
63 u16 links[OSM_L3_MAX_LINKS];
69 struct qcom_osm_l3_desc {
70 const struct qcom_osm_l3_node **nodes;
72 unsigned int lut_row_size;
73 unsigned int reg_freq_lut;
74 unsigned int reg_perf_state;
77 #define DEFINE_QNODE(_name, _id, _buswidth, ...) \
78 static const struct qcom_osm_l3_node _name = { \
81 .buswidth = _buswidth, \
82 .num_links = ARRAY_SIZE(((int[]){ __VA_ARGS__ })), \
83 .links = { __VA_ARGS__ }, \
86 DEFINE_QNODE(sdm845_osm_apps_l3, SDM845_MASTER_OSM_L3_APPS, 16, SDM845_SLAVE_OSM_L3);
87 DEFINE_QNODE(sdm845_osm_l3, SDM845_SLAVE_OSM_L3, 16);
89 static const struct qcom_osm_l3_node *sdm845_osm_l3_nodes[] = {
90 [MASTER_OSM_L3_APPS] = &sdm845_osm_apps_l3,
91 [SLAVE_OSM_L3] = &sdm845_osm_l3,
94 static const struct qcom_osm_l3_desc sdm845_icc_osm_l3 = {
95 .nodes = sdm845_osm_l3_nodes,
96 .num_nodes = ARRAY_SIZE(sdm845_osm_l3_nodes),
97 .lut_row_size = OSM_LUT_ROW_SIZE,
98 .reg_freq_lut = OSM_REG_FREQ_LUT,
99 .reg_perf_state = OSM_REG_PERF_STATE,
102 DEFINE_QNODE(sc7180_osm_apps_l3, SC7180_MASTER_OSM_L3_APPS, 16, SC7180_SLAVE_OSM_L3);
103 DEFINE_QNODE(sc7180_osm_l3, SC7180_SLAVE_OSM_L3, 16);
105 static const struct qcom_osm_l3_node *sc7180_osm_l3_nodes[] = {
106 [MASTER_OSM_L3_APPS] = &sc7180_osm_apps_l3,
107 [SLAVE_OSM_L3] = &sc7180_osm_l3,
110 static const struct qcom_osm_l3_desc sc7180_icc_osm_l3 = {
111 .nodes = sc7180_osm_l3_nodes,
112 .num_nodes = ARRAY_SIZE(sc7180_osm_l3_nodes),
113 .lut_row_size = OSM_LUT_ROW_SIZE,
114 .reg_freq_lut = OSM_REG_FREQ_LUT,
115 .reg_perf_state = OSM_REG_PERF_STATE,
118 DEFINE_QNODE(sc7280_epss_apps_l3, SC7280_MASTER_EPSS_L3_APPS, 32, SC7280_SLAVE_EPSS_L3);
119 DEFINE_QNODE(sc7280_epss_l3, SC7280_SLAVE_EPSS_L3, 32);
121 static const struct qcom_osm_l3_node *sc7280_epss_l3_nodes[] = {
122 [MASTER_EPSS_L3_APPS] = &sc7280_epss_apps_l3,
123 [SLAVE_EPSS_L3_SHARED] = &sc7280_epss_l3,
126 static const struct qcom_osm_l3_desc sc7280_icc_epss_l3 = {
127 .nodes = sc7280_epss_l3_nodes,
128 .num_nodes = ARRAY_SIZE(sc7280_epss_l3_nodes),
129 .lut_row_size = EPSS_LUT_ROW_SIZE,
130 .reg_freq_lut = EPSS_REG_FREQ_LUT,
131 .reg_perf_state = EPSS_REG_PERF_STATE,
134 DEFINE_QNODE(sc8180x_osm_apps_l3, SC8180X_MASTER_OSM_L3_APPS, 32, SC8180X_SLAVE_OSM_L3);
135 DEFINE_QNODE(sc8180x_osm_l3, SC8180X_SLAVE_OSM_L3, 32);
137 static const struct qcom_osm_l3_node *sc8180x_osm_l3_nodes[] = {
138 [MASTER_OSM_L3_APPS] = &sc8180x_osm_apps_l3,
139 [SLAVE_OSM_L3] = &sc8180x_osm_l3,
142 static const struct qcom_osm_l3_desc sc8180x_icc_osm_l3 = {
143 .nodes = sc8180x_osm_l3_nodes,
144 .num_nodes = ARRAY_SIZE(sc8180x_osm_l3_nodes),
145 .lut_row_size = OSM_LUT_ROW_SIZE,
146 .reg_freq_lut = OSM_REG_FREQ_LUT,
147 .reg_perf_state = OSM_REG_PERF_STATE,
150 DEFINE_QNODE(sm8150_osm_apps_l3, SM8150_MASTER_OSM_L3_APPS, 32, SM8150_SLAVE_OSM_L3);
151 DEFINE_QNODE(sm8150_osm_l3, SM8150_SLAVE_OSM_L3, 32);
153 static const struct qcom_osm_l3_node *sm8150_osm_l3_nodes[] = {
154 [MASTER_OSM_L3_APPS] = &sm8150_osm_apps_l3,
155 [SLAVE_OSM_L3] = &sm8150_osm_l3,
158 static const struct qcom_osm_l3_desc sm8150_icc_osm_l3 = {
159 .nodes = sm8150_osm_l3_nodes,
160 .num_nodes = ARRAY_SIZE(sm8150_osm_l3_nodes),
161 .lut_row_size = OSM_LUT_ROW_SIZE,
162 .reg_freq_lut = OSM_REG_FREQ_LUT,
163 .reg_perf_state = OSM_REG_PERF_STATE,
166 DEFINE_QNODE(sm8250_epss_apps_l3, SM8250_MASTER_EPSS_L3_APPS, 32, SM8250_SLAVE_EPSS_L3);
167 DEFINE_QNODE(sm8250_epss_l3, SM8250_SLAVE_EPSS_L3, 32);
169 static const struct qcom_osm_l3_node *sm8250_epss_l3_nodes[] = {
170 [MASTER_EPSS_L3_APPS] = &sm8250_epss_apps_l3,
171 [SLAVE_EPSS_L3_SHARED] = &sm8250_epss_l3,
174 static const struct qcom_osm_l3_desc sm8250_icc_epss_l3 = {
175 .nodes = sm8250_epss_l3_nodes,
176 .num_nodes = ARRAY_SIZE(sm8250_epss_l3_nodes),
177 .lut_row_size = EPSS_LUT_ROW_SIZE,
178 .reg_freq_lut = EPSS_REG_FREQ_LUT,
179 .reg_perf_state = EPSS_REG_PERF_STATE,
182 static int qcom_osm_l3_set(struct icc_node *src, struct icc_node *dst)
184 struct qcom_osm_l3_icc_provider *qp;
185 struct icc_provider *provider;
186 const struct qcom_osm_l3_node *qn;
194 provider = src->provider;
195 qp = to_osm_l3_provider(provider);
197 list_for_each_entry(n, &provider->nodes, node_list)
198 provider->aggregate(n, 0, n->avg_bw, n->peak_bw,
199 &agg_avg, &agg_peak);
201 rate = max(agg_avg, agg_peak);
202 rate = icc_units_to_bps(rate);
203 do_div(rate, qn->buswidth);
205 for (index = 0; index < qp->max_state - 1; index++) {
206 if (qp->lut_tables[index] >= rate)
210 writel_relaxed(index, qp->base + qp->reg_perf_state);
215 static int qcom_osm_l3_remove(struct platform_device *pdev)
217 struct qcom_osm_l3_icc_provider *qp = platform_get_drvdata(pdev);
219 icc_nodes_remove(&qp->provider);
220 return icc_provider_del(&qp->provider);
223 static int qcom_osm_l3_probe(struct platform_device *pdev)
225 u32 info, src, lval, i, prev_freq = 0, freq;
226 static unsigned long hw_rate, xo_rate;
227 struct qcom_osm_l3_icc_provider *qp;
228 const struct qcom_osm_l3_desc *desc;
229 struct icc_onecell_data *data;
230 struct icc_provider *provider;
231 const struct qcom_osm_l3_node **qnodes;
232 struct icc_node *node;
237 clk = clk_get(&pdev->dev, "xo");
241 xo_rate = clk_get_rate(clk);
244 clk = clk_get(&pdev->dev, "alternate");
248 hw_rate = clk_get_rate(clk) / CLK_HW_DIV;
251 qp = devm_kzalloc(&pdev->dev, sizeof(*qp), GFP_KERNEL);
255 qp->base = devm_platform_ioremap_resource(pdev, 0);
256 if (IS_ERR(qp->base))
257 return PTR_ERR(qp->base);
259 /* HW should be in enabled state to proceed */
260 if (!(readl_relaxed(qp->base + REG_ENABLE) & 0x1)) {
261 dev_err(&pdev->dev, "error hardware not enabled\n");
265 desc = device_get_match_data(&pdev->dev);
269 qp->reg_perf_state = desc->reg_perf_state;
271 for (i = 0; i < LUT_MAX_ENTRIES; i++) {
272 info = readl_relaxed(qp->base + desc->reg_freq_lut +
273 i * desc->lut_row_size);
274 src = FIELD_GET(LUT_SRC, info);
275 lval = FIELD_GET(LUT_L_VAL, info);
277 freq = xo_rate * lval;
281 /* Two of the same frequencies signify end of table */
282 if (i > 0 && prev_freq == freq)
285 dev_dbg(&pdev->dev, "index=%d freq=%d\n", i, freq);
287 qp->lut_tables[i] = freq;
292 qnodes = desc->nodes;
293 num_nodes = desc->num_nodes;
295 data = devm_kcalloc(&pdev->dev, num_nodes, sizeof(*node), GFP_KERNEL);
299 provider = &qp->provider;
300 provider->dev = &pdev->dev;
301 provider->set = qcom_osm_l3_set;
302 provider->aggregate = icc_std_aggregate;
303 provider->xlate = of_icc_xlate_onecell;
304 INIT_LIST_HEAD(&provider->nodes);
305 provider->data = data;
307 ret = icc_provider_add(provider);
309 dev_err(&pdev->dev, "error adding interconnect provider\n");
313 for (i = 0; i < num_nodes; i++) {
316 node = icc_node_create(qnodes[i]->id);
322 node->name = qnodes[i]->name;
323 /* Cast away const and add it back in qcom_osm_l3_set() */
324 node->data = (void *)qnodes[i];
325 icc_node_add(node, provider);
327 for (j = 0; j < qnodes[i]->num_links; j++)
328 icc_link_create(node, qnodes[i]->links[j]);
330 data->nodes[i] = node;
332 data->num_nodes = num_nodes;
334 platform_set_drvdata(pdev, qp);
338 icc_nodes_remove(provider);
339 icc_provider_del(provider);
344 static const struct of_device_id osm_l3_of_match[] = {
345 { .compatible = "qcom,sc7180-osm-l3", .data = &sc7180_icc_osm_l3 },
346 { .compatible = "qcom,sc7280-epss-l3", .data = &sc7280_icc_epss_l3 },
347 { .compatible = "qcom,sdm845-osm-l3", .data = &sdm845_icc_osm_l3 },
348 { .compatible = "qcom,sm8150-osm-l3", .data = &sm8150_icc_osm_l3 },
349 { .compatible = "qcom,sc8180x-osm-l3", .data = &sc8180x_icc_osm_l3 },
350 { .compatible = "qcom,sm8250-epss-l3", .data = &sm8250_icc_epss_l3 },
353 MODULE_DEVICE_TABLE(of, osm_l3_of_match);
355 static struct platform_driver osm_l3_driver = {
356 .probe = qcom_osm_l3_probe,
357 .remove = qcom_osm_l3_remove,
360 .of_match_table = osm_l3_of_match,
361 .sync_state = icc_sync_state,
364 module_platform_driver(osm_l3_driver);
366 MODULE_DESCRIPTION("Qualcomm OSM L3 interconnect driver");
367 MODULE_LICENSE("GPL v2");