xref: /openbmc/linux/drivers/cpufreq/sti-cpufreq.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
1aecd8454SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2ab0ea257SLee Jones /*
3ab0ea257SLee Jones  * Match running platform with pre-defined OPP values for CPUFreq
4ab0ea257SLee Jones  *
5ab0ea257SLee Jones  * Author: Ajit Pal Singh <ajitpal.singh@st.com>
6ab0ea257SLee Jones  *         Lee Jones <lee.jones@linaro.org>
7ab0ea257SLee Jones  *
8ab0ea257SLee Jones  * Copyright (C) 2015 STMicroelectronics (R&D) Limited
9ab0ea257SLee Jones  */
10ab0ea257SLee Jones 
11ab0ea257SLee Jones #include <linux/cpu.h>
12ab0ea257SLee Jones #include <linux/io.h>
13ab0ea257SLee Jones #include <linux/mfd/syscon.h>
14ab0ea257SLee Jones #include <linux/module.h>
15ab0ea257SLee Jones #include <linux/of.h>
16*a70eb93aSRob Herring #include <linux/platform_device.h>
17ab0ea257SLee Jones #include <linux/pm_opp.h>
18ab0ea257SLee Jones #include <linux/regmap.h>
19ab0ea257SLee Jones 
20ab0ea257SLee Jones #define VERSION_ELEMENTS	3
21ab0ea257SLee Jones #define MAX_PCODE_NAME_LEN	7
22ab0ea257SLee Jones 
23ab0ea257SLee Jones #define VERSION_SHIFT		28
24ab0ea257SLee Jones #define HW_INFO_INDEX		1
25ab0ea257SLee Jones #define MAJOR_ID_INDEX		1
26ab0ea257SLee Jones #define MINOR_ID_INDEX		2
27ab0ea257SLee Jones 
28ab0ea257SLee Jones /*
29ab0ea257SLee Jones  * Only match on "suitable for ALL versions" entries
30ab0ea257SLee Jones  *
31ab0ea257SLee Jones  * This will be used with the BIT() macro.  It sets the
32ab0ea257SLee Jones  * top bit of a 32bit value and is equal to 0x80000000.
33ab0ea257SLee Jones  */
34ab0ea257SLee Jones #define DEFAULT_VERSION		31
35ab0ea257SLee Jones 
36ab0ea257SLee Jones enum {
37ab0ea257SLee Jones 	PCODE = 0,
38ab0ea257SLee Jones 	SUBSTRATE,
39ab0ea257SLee Jones 	DVFS_MAX_REGFIELDS,
40ab0ea257SLee Jones };
41ab0ea257SLee Jones 
42ab0ea257SLee Jones /**
4334adbcbdSLee Jones  * struct sti_cpufreq_ddata - ST CPUFreq Driver Data
44ab0ea257SLee Jones  *
4534adbcbdSLee Jones  * @cpu:		CPU's OF node
4634adbcbdSLee Jones  * @syscfg_eng:		Engineering Syscon register map
4734adbcbdSLee Jones  * @syscfg:		Syscon register map
48ab0ea257SLee Jones  */
49ab0ea257SLee Jones static struct sti_cpufreq_ddata {
50ab0ea257SLee Jones 	struct device *cpu;
51ab0ea257SLee Jones 	struct regmap *syscfg_eng;
52ab0ea257SLee Jones 	struct regmap *syscfg;
53ab0ea257SLee Jones } ddata;
54ab0ea257SLee Jones 
sti_cpufreq_fetch_major(void)55ab0ea257SLee Jones static int sti_cpufreq_fetch_major(void) {
56ab0ea257SLee Jones 	struct device_node *np = ddata.cpu->of_node;
57ab0ea257SLee Jones 	struct device *dev = ddata.cpu;
58ab0ea257SLee Jones 	unsigned int major_offset;
59ab0ea257SLee Jones 	unsigned int socid;
60ab0ea257SLee Jones 	int ret;
61ab0ea257SLee Jones 
62ab0ea257SLee Jones 	ret = of_property_read_u32_index(np, "st,syscfg",
63ab0ea257SLee Jones 					 MAJOR_ID_INDEX, &major_offset);
64ab0ea257SLee Jones 	if (ret) {
65cc5a7a74SRob Herring 		dev_err(dev, "No major number offset provided in %pOF [%d]\n",
66cc5a7a74SRob Herring 			np, ret);
67ab0ea257SLee Jones 		return ret;
68ab0ea257SLee Jones 	}
69ab0ea257SLee Jones 
70ab0ea257SLee Jones 	ret = regmap_read(ddata.syscfg, major_offset, &socid);
71ab0ea257SLee Jones 	if (ret) {
72ab0ea257SLee Jones 		dev_err(dev, "Failed to read major number from syscon [%d]\n",
73ab0ea257SLee Jones 			ret);
74ab0ea257SLee Jones 		return ret;
75ab0ea257SLee Jones 	}
76ab0ea257SLee Jones 
77ab0ea257SLee Jones 	return ((socid >> VERSION_SHIFT) & 0xf) + 1;
78ab0ea257SLee Jones }
79ab0ea257SLee Jones 
sti_cpufreq_fetch_minor(void)80ab0ea257SLee Jones static int sti_cpufreq_fetch_minor(void)
81ab0ea257SLee Jones {
82ab0ea257SLee Jones 	struct device *dev = ddata.cpu;
83ab0ea257SLee Jones 	struct device_node *np = dev->of_node;
84ab0ea257SLee Jones 	unsigned int minor_offset;
85ab0ea257SLee Jones 	unsigned int minid;
86ab0ea257SLee Jones 	int ret;
87ab0ea257SLee Jones 
88ab0ea257SLee Jones 	ret = of_property_read_u32_index(np, "st,syscfg-eng",
89ab0ea257SLee Jones 					 MINOR_ID_INDEX, &minor_offset);
90ab0ea257SLee Jones 	if (ret) {
91ab0ea257SLee Jones 		dev_err(dev,
92cc5a7a74SRob Herring 			"No minor number offset provided %pOF [%d]\n",
93cc5a7a74SRob Herring 			np, ret);
94ab0ea257SLee Jones 		return ret;
95ab0ea257SLee Jones 	}
96ab0ea257SLee Jones 
97ab0ea257SLee Jones 	ret = regmap_read(ddata.syscfg_eng, minor_offset, &minid);
98ab0ea257SLee Jones 	if (ret) {
99ab0ea257SLee Jones 		dev_err(dev,
100ab0ea257SLee Jones 			"Failed to read the minor number from syscon [%d]\n",
101ab0ea257SLee Jones 			ret);
102ab0ea257SLee Jones 		return ret;
103ab0ea257SLee Jones 	}
104ab0ea257SLee Jones 
105ab0ea257SLee Jones 	return minid & 0xf;
106ab0ea257SLee Jones }
107ab0ea257SLee Jones 
sti_cpufreq_fetch_regmap_field(const struct reg_field * reg_fields,int hw_info_offset,int field)108ab0ea257SLee Jones static int sti_cpufreq_fetch_regmap_field(const struct reg_field *reg_fields,
109ab0ea257SLee Jones 					  int hw_info_offset, int field)
110ab0ea257SLee Jones {
111ab0ea257SLee Jones 	struct regmap_field *regmap_field;
112ab0ea257SLee Jones 	struct reg_field reg_field = reg_fields[field];
113ab0ea257SLee Jones 	struct device *dev = ddata.cpu;
114ab0ea257SLee Jones 	unsigned int value;
115ab0ea257SLee Jones 	int ret;
116ab0ea257SLee Jones 
117ab0ea257SLee Jones 	reg_field.reg = hw_info_offset;
118ab0ea257SLee Jones 	regmap_field = devm_regmap_field_alloc(dev,
119ab0ea257SLee Jones 					       ddata.syscfg_eng,
120ab0ea257SLee Jones 					       reg_field);
121ab0ea257SLee Jones 	if (IS_ERR(regmap_field)) {
122ab0ea257SLee Jones 		dev_err(dev, "Failed to allocate reg field\n");
123ab0ea257SLee Jones 		return PTR_ERR(regmap_field);
124ab0ea257SLee Jones 	}
125ab0ea257SLee Jones 
126ab0ea257SLee Jones 	ret = regmap_field_read(regmap_field, &value);
127ab0ea257SLee Jones 	if (ret) {
128ab0ea257SLee Jones 		dev_err(dev, "Failed to read %s code\n",
129ab0ea257SLee Jones 			field ? "SUBSTRATE" : "PCODE");
130ab0ea257SLee Jones 		return ret;
131ab0ea257SLee Jones 	}
132ab0ea257SLee Jones 
133ab0ea257SLee Jones 	return value;
134ab0ea257SLee Jones }
135ab0ea257SLee Jones 
136ab0ea257SLee Jones static const struct reg_field sti_stih407_dvfs_regfields[DVFS_MAX_REGFIELDS] = {
137ab0ea257SLee Jones 	[PCODE]		= REG_FIELD(0, 16, 19),
138ab0ea257SLee Jones 	[SUBSTRATE]	= REG_FIELD(0, 0, 2),
139ab0ea257SLee Jones };
140ab0ea257SLee Jones 
sti_cpufreq_match(void)141ab0ea257SLee Jones static const struct reg_field *sti_cpufreq_match(void)
142ab0ea257SLee Jones {
143ab0ea257SLee Jones 	if (of_machine_is_compatible("st,stih407") ||
14401a163c5SAlain Volmat 	    of_machine_is_compatible("st,stih410") ||
14501a163c5SAlain Volmat 	    of_machine_is_compatible("st,stih418"))
146ab0ea257SLee Jones 		return sti_stih407_dvfs_regfields;
147ab0ea257SLee Jones 
148ab0ea257SLee Jones 	return NULL;
149ab0ea257SLee Jones }
150ab0ea257SLee Jones 
sti_cpufreq_set_opp_info(void)151ab0ea257SLee Jones static int sti_cpufreq_set_opp_info(void)
152ab0ea257SLee Jones {
153ab0ea257SLee Jones 	struct device *dev = ddata.cpu;
154ab0ea257SLee Jones 	struct device_node *np = dev->of_node;
155ab0ea257SLee Jones 	const struct reg_field *reg_fields;
156ab0ea257SLee Jones 	unsigned int hw_info_offset;
157ab0ea257SLee Jones 	unsigned int version[VERSION_ELEMENTS];
158ab0ea257SLee Jones 	int pcode, substrate, major, minor;
15949df85d0SViresh Kumar 	int opp_token, ret;
160ab0ea257SLee Jones 	char name[MAX_PCODE_NAME_LEN];
16149df85d0SViresh Kumar 	struct dev_pm_opp_config config = {
16249df85d0SViresh Kumar 		.supported_hw = version,
16349df85d0SViresh Kumar 		.supported_hw_count = ARRAY_SIZE(version),
16449df85d0SViresh Kumar 		.prop_name = name,
16549df85d0SViresh Kumar 	};
166ab0ea257SLee Jones 
167ab0ea257SLee Jones 	reg_fields = sti_cpufreq_match();
168ab0ea257SLee Jones 	if (!reg_fields) {
1699ad0a1b6SColin Ian King 		dev_err(dev, "This SoC doesn't support voltage scaling\n");
170ab0ea257SLee Jones 		return -ENODEV;
171ab0ea257SLee Jones 	}
172ab0ea257SLee Jones 
173ab0ea257SLee Jones 	ret = of_property_read_u32_index(np, "st,syscfg-eng",
174ab0ea257SLee Jones 					 HW_INFO_INDEX, &hw_info_offset);
175ab0ea257SLee Jones 	if (ret) {
176ab0ea257SLee Jones 		dev_warn(dev, "Failed to read HW info offset from DT\n");
177ab0ea257SLee Jones 		substrate = DEFAULT_VERSION;
178ab0ea257SLee Jones 		pcode = 0;
179ab0ea257SLee Jones 		goto use_defaults;
180ab0ea257SLee Jones 	}
181ab0ea257SLee Jones 
182ab0ea257SLee Jones 	pcode = sti_cpufreq_fetch_regmap_field(reg_fields,
183ab0ea257SLee Jones 					       hw_info_offset,
184ab0ea257SLee Jones 					       PCODE);
185ab0ea257SLee Jones 	if (pcode < 0) {
186ab0ea257SLee Jones 		dev_warn(dev, "Failed to obtain process code\n");
187ab0ea257SLee Jones 		/* Use default pcode */
188ab0ea257SLee Jones 		pcode = 0;
189ab0ea257SLee Jones 	}
190ab0ea257SLee Jones 
191ab0ea257SLee Jones 	substrate = sti_cpufreq_fetch_regmap_field(reg_fields,
192ab0ea257SLee Jones 						   hw_info_offset,
193ab0ea257SLee Jones 						   SUBSTRATE);
194ab0ea257SLee Jones 	if (substrate) {
195ab0ea257SLee Jones 		dev_warn(dev, "Failed to obtain substrate code\n");
196ab0ea257SLee Jones 		/* Use default substrate */
197ab0ea257SLee Jones 		substrate = DEFAULT_VERSION;
198ab0ea257SLee Jones 	}
199ab0ea257SLee Jones 
200ab0ea257SLee Jones use_defaults:
201ab0ea257SLee Jones 	major = sti_cpufreq_fetch_major();
202ab0ea257SLee Jones 	if (major < 0) {
203ab0ea257SLee Jones 		dev_err(dev, "Failed to obtain major version\n");
204ab0ea257SLee Jones 		/* Use default major number */
205ab0ea257SLee Jones 		major = DEFAULT_VERSION;
206ab0ea257SLee Jones 	}
207ab0ea257SLee Jones 
208ab0ea257SLee Jones 	minor = sti_cpufreq_fetch_minor();
209ab0ea257SLee Jones 	if (minor < 0) {
210ab0ea257SLee Jones 		dev_err(dev, "Failed to obtain minor version\n");
211ab0ea257SLee Jones 		/* Use default minor number */
212ab0ea257SLee Jones 		minor = DEFAULT_VERSION;
213ab0ea257SLee Jones 	}
214ab0ea257SLee Jones 
215ab0ea257SLee Jones 	snprintf(name, MAX_PCODE_NAME_LEN, "pcode%d", pcode);
216ab0ea257SLee Jones 
217ab0ea257SLee Jones 	version[0] = BIT(major);
218ab0ea257SLee Jones 	version[1] = BIT(minor);
219ab0ea257SLee Jones 	version[2] = BIT(substrate);
220ab0ea257SLee Jones 
22149df85d0SViresh Kumar 	opp_token = dev_pm_opp_set_config(dev, &config);
22249df85d0SViresh Kumar 	if (opp_token < 0) {
22349df85d0SViresh Kumar 		dev_err(dev, "Failed to set OPP config\n");
22449df85d0SViresh Kumar 		return opp_token;
225ab0ea257SLee Jones 	}
226ab0ea257SLee Jones 
227ab0ea257SLee Jones 	dev_dbg(dev, "pcode: %d major: %d minor: %d substrate: %d\n",
228ab0ea257SLee Jones 		pcode, major, minor, substrate);
229ab0ea257SLee Jones 	dev_dbg(dev, "version[0]: %x version[1]: %x version[2]: %x\n",
230ab0ea257SLee Jones 		version[0], version[1], version[2]);
231ab0ea257SLee Jones 
232ab0ea257SLee Jones 	return 0;
233ab0ea257SLee Jones }
234ab0ea257SLee Jones 
sti_cpufreq_fetch_syscon_registers(void)235ad61dd30SStephen Boyd static int sti_cpufreq_fetch_syscon_registers(void)
236ab0ea257SLee Jones {
237ab0ea257SLee Jones 	struct device *dev = ddata.cpu;
238ab0ea257SLee Jones 	struct device_node *np = dev->of_node;
239ab0ea257SLee Jones 
240ab0ea257SLee Jones 	ddata.syscfg = syscon_regmap_lookup_by_phandle(np, "st,syscfg");
241ab0ea257SLee Jones 	if (IS_ERR(ddata.syscfg)) {
242ab0ea257SLee Jones 		dev_err(dev,  "\"st,syscfg\" not supplied\n");
243ab0ea257SLee Jones 		return PTR_ERR(ddata.syscfg);
244ab0ea257SLee Jones 	}
245ab0ea257SLee Jones 
246ab0ea257SLee Jones 	ddata.syscfg_eng = syscon_regmap_lookup_by_phandle(np, "st,syscfg-eng");
247ab0ea257SLee Jones 	if (IS_ERR(ddata.syscfg_eng)) {
248ab0ea257SLee Jones 		dev_err(dev, "\"st,syscfg-eng\" not supplied\n");
249ab0ea257SLee Jones 		return PTR_ERR(ddata.syscfg_eng);
250ab0ea257SLee Jones 	}
251ab0ea257SLee Jones 
252ab0ea257SLee Jones 	return 0;
253ab0ea257SLee Jones }
254ab0ea257SLee Jones 
sti_cpufreq_init(void)255f7968c22SXiu Jianfeng static int __init sti_cpufreq_init(void)
256ab0ea257SLee Jones {
257ab0ea257SLee Jones 	int ret;
258ab0ea257SLee Jones 
2592482bc31SSudeep Holla 	if ((!of_machine_is_compatible("st,stih407")) &&
26001a163c5SAlain Volmat 		(!of_machine_is_compatible("st,stih410")) &&
26101a163c5SAlain Volmat 		(!of_machine_is_compatible("st,stih418")))
2622482bc31SSudeep Holla 		return -ENODEV;
2632482bc31SSudeep Holla 
264ab0ea257SLee Jones 	ddata.cpu = get_cpu_device(0);
265ab0ea257SLee Jones 	if (!ddata.cpu) {
266ab0ea257SLee Jones 		dev_err(ddata.cpu, "Failed to get device for CPU0\n");
267ab0ea257SLee Jones 		goto skip_voltage_scaling;
268ab0ea257SLee Jones 	}
269ab0ea257SLee Jones 
270ab0ea257SLee Jones 	if (!of_get_property(ddata.cpu->of_node, "operating-points-v2", NULL)) {
271ab0ea257SLee Jones 		dev_err(ddata.cpu, "OPP-v2 not supported\n");
272ab0ea257SLee Jones 		goto skip_voltage_scaling;
273ab0ea257SLee Jones 	}
274ab0ea257SLee Jones 
275ad61dd30SStephen Boyd 	ret = sti_cpufreq_fetch_syscon_registers();
276ab0ea257SLee Jones 	if (ret)
277ab0ea257SLee Jones 		goto skip_voltage_scaling;
278ab0ea257SLee Jones 
279ab0ea257SLee Jones 	ret = sti_cpufreq_set_opp_info();
280ab0ea257SLee Jones 	if (!ret)
281ab0ea257SLee Jones 		goto register_cpufreq_dt;
282ab0ea257SLee Jones 
283ab0ea257SLee Jones skip_voltage_scaling:
284ab0ea257SLee Jones 	dev_err(ddata.cpu, "Not doing voltage scaling\n");
285ab0ea257SLee Jones 
286ab0ea257SLee Jones register_cpufreq_dt:
287ab0ea257SLee Jones 	platform_device_register_simple("cpufreq-dt", -1, NULL, 0);
288ab0ea257SLee Jones 
289ab0ea257SLee Jones 	return 0;
290ab0ea257SLee Jones }
291ab0ea257SLee Jones module_init(sti_cpufreq_init);
292ab0ea257SLee Jones 
293183747abSPali Rohár static const struct of_device_id __maybe_unused sti_cpufreq_of_match[] = {
294183747abSPali Rohár 	{ .compatible = "st,stih407" },
295183747abSPali Rohár 	{ .compatible = "st,stih410" },
296183747abSPali Rohár 	{ },
297183747abSPali Rohár };
298183747abSPali Rohár MODULE_DEVICE_TABLE(of, sti_cpufreq_of_match);
299183747abSPali Rohár 
300ab0ea257SLee Jones MODULE_DESCRIPTION("STMicroelectronics CPUFreq/OPP driver");
301ab0ea257SLee Jones MODULE_AUTHOR("Ajitpal Singh <ajitpal.singh@st.com>");
302ab0ea257SLee Jones MODULE_AUTHOR("Lee Jones <lee.jones@linaro.org>");
303ab0ea257SLee Jones MODULE_LICENSE("GPL v2");
304