• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Allwinner CPUFreq nvmem based driver
4  *
5  * The sun50i-cpufreq-nvmem driver reads the efuse value from the SoC to
6  * provide the OPP framework with required information.
7  *
8  * Copyright (C) 2019 Yangtao Li <tiny.windzz@gmail.com>
9  */
10 
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/arm-smccc.h>
14 #include <linux/cpu.h>
15 #include <linux/module.h>
16 #include <linux/nvmem-consumer.h>
17 #include <linux/of.h>
18 #include <linux/platform_device.h>
19 #include <linux/pm_opp.h>
20 #include <linux/slab.h>
21 
22 #define NVMEM_MASK	0x7
23 #define NVMEM_SHIFT	5
24 
25 static struct platform_device *cpufreq_dt_pdev, *sun50i_cpufreq_pdev;
26 
27 struct sunxi_cpufreq_data {
28 	u32 (*efuse_xlate)(u32 speedbin);
29 };
30 
sun50i_h6_efuse_xlate(u32 speedbin)31 static u32 sun50i_h6_efuse_xlate(u32 speedbin)
32 {
33 	u32 efuse_value;
34 
35 	efuse_value = (speedbin >> NVMEM_SHIFT) & NVMEM_MASK;
36 
37 	/*
38 	 * We treat unexpected efuse values as if the SoC was from
39 	 * the slowest bin. Expected efuse values are 1-3, slowest
40 	 * to fastest.
41 	 */
42 	if (efuse_value >= 1 && efuse_value <= 3)
43 		return efuse_value - 1;
44 	else
45 		return 0;
46 }
47 
get_soc_id_revision(void)48 static int get_soc_id_revision(void)
49 {
50 #ifdef CONFIG_HAVE_ARM_SMCCC_DISCOVERY
51 	return arm_smccc_get_soc_id_revision();
52 #else
53 	return SMCCC_RET_NOT_SUPPORTED;
54 #endif
55 }
56 
57 /*
58  * Judging by the OPP tables in the vendor BSP, the quality order of the
59  * returned speedbin index is 4 -> 0/2 -> 3 -> 1, from worst to best.
60  * 0 and 2 seem identical from the OPP tables' point of view.
61  */
sun50i_h616_efuse_xlate(u32 speedbin)62 static u32 sun50i_h616_efuse_xlate(u32 speedbin)
63 {
64 	int ver_bits = get_soc_id_revision();
65 	u32 value = 0;
66 
67 	switch (speedbin & 0xffff) {
68 	case 0x2000:
69 		value = 0;
70 		break;
71 	case 0x2400:
72 	case 0x7400:
73 	case 0x2c00:
74 	case 0x7c00:
75 		if (ver_bits != SMCCC_RET_NOT_SUPPORTED && ver_bits <= 1) {
76 			/* ic version A/B */
77 			value = 1;
78 		} else {
79 			/* ic version C and later version */
80 			value = 2;
81 		}
82 		break;
83 	case 0x5000:
84 	case 0x5400:
85 	case 0x6000:
86 		value = 3;
87 		break;
88 	case 0x5c00:
89 		value = 4;
90 		break;
91 	case 0x5d00:
92 		value = 0;
93 		break;
94 	case 0x6c00:
95 		value = 5;
96 		break;
97 	default:
98 		pr_warn("sun50i-cpufreq-nvmem: unknown speed bin 0x%x, using default bin 0\n",
99 			speedbin & 0xffff);
100 		value = 0;
101 		break;
102 	}
103 
104 	return value;
105 }
106 
107 static struct sunxi_cpufreq_data sun50i_h6_cpufreq_data = {
108 	.efuse_xlate = sun50i_h6_efuse_xlate,
109 };
110 
111 static struct sunxi_cpufreq_data sun50i_h616_cpufreq_data = {
112 	.efuse_xlate = sun50i_h616_efuse_xlate,
113 };
114 
115 static const struct of_device_id cpu_opp_match_list[] = {
116 	{ .compatible = "allwinner,sun50i-h6-operating-points",
117 	  .data = &sun50i_h6_cpufreq_data,
118 	},
119 	{ .compatible = "allwinner,sun50i-h616-operating-points",
120 	  .data = &sun50i_h616_cpufreq_data,
121 	},
122 	{}
123 };
124 
125 /**
126  * dt_has_supported_hw() - Check if any OPPs use opp-supported-hw
127  *
128  * If we ask the cpufreq framework to use the opp-supported-hw feature, it
129  * will ignore every OPP node without that DT property. If none of the OPPs
130  * have it, the driver will fail probing, due to the lack of OPPs.
131  *
132  * Returns true if we have at least one OPP with the opp-supported-hw property.
133  */
dt_has_supported_hw(void)134 static bool dt_has_supported_hw(void)
135 {
136 	bool has_opp_supported_hw = false;
137 	struct device *cpu_dev;
138 
139 	cpu_dev = get_cpu_device(0);
140 	if (!cpu_dev)
141 		return false;
142 
143 	struct device_node *np __free(device_node) =
144 		dev_pm_opp_of_get_opp_desc_node(cpu_dev);
145 	if (!np)
146 		return false;
147 
148 	for_each_child_of_node_scoped(np, opp) {
149 		if (of_property_present(opp, "opp-supported-hw")) {
150 			has_opp_supported_hw = true;
151 			break;
152 		}
153 	}
154 
155 	return has_opp_supported_hw;
156 }
157 
158 /**
159  * sun50i_cpufreq_get_efuse() - Determine speed grade from efuse value
160  *
161  * Returns non-negative speed bin index on success, a negative error
162  * value otherwise.
163  */
sun50i_cpufreq_get_efuse(void)164 static int sun50i_cpufreq_get_efuse(void)
165 {
166 	const struct sunxi_cpufreq_data *opp_data;
167 	struct nvmem_cell *speedbin_nvmem;
168 	const struct of_device_id *match;
169 	struct device *cpu_dev;
170 	void *speedbin_ptr;
171 	u32 speedbin = 0;
172 	size_t len;
173 	int ret;
174 
175 	cpu_dev = get_cpu_device(0);
176 	if (!cpu_dev)
177 		return -ENODEV;
178 
179 	struct device_node *np __free(device_node) =
180 		dev_pm_opp_of_get_opp_desc_node(cpu_dev);
181 	if (!np)
182 		return -ENOENT;
183 
184 	match = of_match_node(cpu_opp_match_list, np);
185 	if (!match)
186 		return -ENOENT;
187 
188 	opp_data = match->data;
189 
190 	speedbin_nvmem = of_nvmem_cell_get(np, NULL);
191 	if (IS_ERR(speedbin_nvmem))
192 		return dev_err_probe(cpu_dev, PTR_ERR(speedbin_nvmem),
193 				     "Could not get nvmem cell\n");
194 
195 	speedbin_ptr = nvmem_cell_read(speedbin_nvmem, &len);
196 	nvmem_cell_put(speedbin_nvmem);
197 	if (IS_ERR(speedbin_ptr))
198 		return PTR_ERR(speedbin_ptr);
199 
200 	if (len <= 4)
201 		memcpy(&speedbin, speedbin_ptr, len);
202 	speedbin = le32_to_cpu(speedbin);
203 
204 	ret = opp_data->efuse_xlate(speedbin);
205 
206 	kfree(speedbin_ptr);
207 
208 	return ret;
209 };
210 
sun50i_cpufreq_nvmem_probe(struct platform_device * pdev)211 static int sun50i_cpufreq_nvmem_probe(struct platform_device *pdev)
212 {
213 	int *opp_tokens;
214 	char name[] = "speedXXXXXXXXXXX"; /* Integers can take 11 chars max */
215 	unsigned int cpu, supported_hw;
216 	struct dev_pm_opp_config config = {};
217 	int speed;
218 	int ret;
219 
220 	opp_tokens = kcalloc(num_possible_cpus(), sizeof(*opp_tokens),
221 			     GFP_KERNEL);
222 	if (!opp_tokens)
223 		return -ENOMEM;
224 
225 	speed = sun50i_cpufreq_get_efuse();
226 	if (speed < 0) {
227 		kfree(opp_tokens);
228 		return speed;
229 	}
230 
231 	/*
232 	 * We need at least one OPP with the "opp-supported-hw" property,
233 	 * or else the upper layers will ignore every OPP and will bail out.
234 	 */
235 	if (dt_has_supported_hw()) {
236 		supported_hw = 1U << speed;
237 		config.supported_hw = &supported_hw;
238 		config.supported_hw_count = 1;
239 	}
240 
241 	snprintf(name, sizeof(name), "speed%d", speed);
242 	config.prop_name = name;
243 
244 	for_each_possible_cpu(cpu) {
245 		struct device *cpu_dev = get_cpu_device(cpu);
246 
247 		if (!cpu_dev) {
248 			ret = -ENODEV;
249 			goto free_opp;
250 		}
251 
252 		ret = dev_pm_opp_set_config(cpu_dev, &config);
253 		if (ret < 0)
254 			goto free_opp;
255 
256 		opp_tokens[cpu] = ret;
257 	}
258 
259 	cpufreq_dt_pdev = platform_device_register_simple("cpufreq-dt", -1,
260 							  NULL, 0);
261 	if (!IS_ERR(cpufreq_dt_pdev)) {
262 		platform_set_drvdata(pdev, opp_tokens);
263 		return 0;
264 	}
265 
266 	ret = PTR_ERR(cpufreq_dt_pdev);
267 	pr_err("Failed to register platform device\n");
268 
269 free_opp:
270 	for_each_possible_cpu(cpu)
271 		dev_pm_opp_clear_config(opp_tokens[cpu]);
272 	kfree(opp_tokens);
273 
274 	return ret;
275 }
276 
sun50i_cpufreq_nvmem_remove(struct platform_device * pdev)277 static void sun50i_cpufreq_nvmem_remove(struct platform_device *pdev)
278 {
279 	int *opp_tokens = platform_get_drvdata(pdev);
280 	unsigned int cpu;
281 
282 	platform_device_unregister(cpufreq_dt_pdev);
283 
284 	for_each_possible_cpu(cpu)
285 		dev_pm_opp_clear_config(opp_tokens[cpu]);
286 
287 	kfree(opp_tokens);
288 }
289 
290 static struct platform_driver sun50i_cpufreq_driver = {
291 	.probe = sun50i_cpufreq_nvmem_probe,
292 	.remove_new = sun50i_cpufreq_nvmem_remove,
293 	.driver = {
294 		.name = "sun50i-cpufreq-nvmem",
295 	},
296 };
297 
298 static const struct of_device_id sun50i_cpufreq_match_list[] = {
299 	{ .compatible = "allwinner,sun50i-h6" },
300 	{ .compatible = "allwinner,sun50i-h616" },
301 	{ .compatible = "allwinner,sun50i-h618" },
302 	{ .compatible = "allwinner,sun50i-h700" },
303 	{}
304 };
305 MODULE_DEVICE_TABLE(of, sun50i_cpufreq_match_list);
306 
sun50i_cpufreq_match_node(void)307 static const struct of_device_id *sun50i_cpufreq_match_node(void)
308 {
309 	struct device_node *np __free(device_node) = of_find_node_by_path("/");
310 
311 	return of_match_node(sun50i_cpufreq_match_list, np);
312 }
313 
314 /*
315  * Since the driver depends on nvmem drivers, which may return EPROBE_DEFER,
316  * all the real activity is done in the probe, which may be defered as well.
317  * The init here is only registering the driver and the platform device.
318  */
sun50i_cpufreq_init(void)319 static int __init sun50i_cpufreq_init(void)
320 {
321 	const struct of_device_id *match;
322 	int ret;
323 
324 	match = sun50i_cpufreq_match_node();
325 	if (!match)
326 		return -ENODEV;
327 
328 	ret = platform_driver_register(&sun50i_cpufreq_driver);
329 	if (unlikely(ret < 0))
330 		return ret;
331 
332 	sun50i_cpufreq_pdev =
333 		platform_device_register_simple("sun50i-cpufreq-nvmem",
334 						-1, NULL, 0);
335 	ret = PTR_ERR_OR_ZERO(sun50i_cpufreq_pdev);
336 	if (ret == 0)
337 		return 0;
338 
339 	platform_driver_unregister(&sun50i_cpufreq_driver);
340 	return ret;
341 }
342 module_init(sun50i_cpufreq_init);
343 
sun50i_cpufreq_exit(void)344 static void __exit sun50i_cpufreq_exit(void)
345 {
346 	platform_device_unregister(sun50i_cpufreq_pdev);
347 	platform_driver_unregister(&sun50i_cpufreq_driver);
348 }
349 module_exit(sun50i_cpufreq_exit);
350 
351 MODULE_DESCRIPTION("Sun50i-h6 cpufreq driver");
352 MODULE_LICENSE("GPL v2");
353