blob: 4781a565547a15e032038fb49060666867ffbe44 [file] [log] [blame]
Peng Fan21981d22019-08-26 08:12:19 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
Peng Fan21981d22019-08-26 08:12:19 +00006#include <cpu.h>
7#include <dm.h>
8#include <thermal.h>
Simon Glass3ba929a2020-10-30 21:38:53 -06009#include <asm/global_data.h>
Simon Glass274e0b02020-05-10 11:39:56 -060010#include <asm/system.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080011#include <firmware/imx/sci/sci.h>
Peng Fan21981d22019-08-26 08:12:19 +000012#include <asm/arch/sys_proto.h>
13#include <asm/arch-imx/cpu.h>
14#include <asm/armv8/cpu.h>
Peng Fan81c694a2023-04-28 12:08:14 +080015#include <imx_thermal.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060016#include <linux/bitops.h>
Peng Fan146cce92023-04-28 12:08:12 +080017#include <linux/clk-provider.h>
Peng Fan21981d22019-08-26 08:12:19 +000018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glassb75b15b2020-12-03 16:55:23 -070021struct cpu_imx_plat {
Peng Fan21981d22019-08-26 08:12:19 +000022 const char *name;
23 const char *rev;
24 const char *type;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020025 u32 cpu_rsrc;
Peng Fan21981d22019-08-26 08:12:19 +000026 u32 cpurev;
27 u32 freq_mhz;
Peng Fane2ded332020-05-03 21:58:52 +080028 u32 mpidr;
Peng Fan21981d22019-08-26 08:12:19 +000029};
30
Peng Fan146cce92023-04-28 12:08:12 +080031static const char *get_imx_type_str(u32 imxtype)
Peng Fan21981d22019-08-26 08:12:19 +000032{
33 switch (imxtype) {
34 case MXC_CPU_IMX8QXP:
35 case MXC_CPU_IMX8QXP_A0:
Peng Fan146cce92023-04-28 12:08:12 +080036 return "8QXP";
Peng Fan21981d22019-08-26 08:12:19 +000037 case MXC_CPU_IMX8QM:
Peng Fan146cce92023-04-28 12:08:12 +080038 return "8QM";
39 case MXC_CPU_IMX93:
40 return "93(52)";/* iMX93 Dual core with NPU */
Peng Fanc3db3ad2023-04-28 12:08:32 +080041 case MXC_CPU_IMX9351:
42 return "93(51)";/* iMX93 Single core with NPU */
43 case MXC_CPU_IMX9332:
44 return "93(32)";/* iMX93 Dual core without NPU */
45 case MXC_CPU_IMX9331:
46 return "93(31)";/* iMX93 Single core without NPU */
47 case MXC_CPU_IMX9322:
48 return "93(22)";/* iMX93 9x9 Dual core */
49 case MXC_CPU_IMX9321:
50 return "93(21)";/* iMX93 9x9 Single core */
51 case MXC_CPU_IMX9312:
52 return "93(12)";/* iMX93 9x9 Dual core without NPU */
53 case MXC_CPU_IMX9311:
54 return "93(11)";/* iMX93 9x9 Single core without NPU */
Peng Fan21981d22019-08-26 08:12:19 +000055 default:
56 return "??";
57 }
58}
59
Peng Fan146cce92023-04-28 12:08:12 +080060static const char *get_imx_rev_str(u32 rev)
Peng Fan21981d22019-08-26 08:12:19 +000061{
Peng Fan146cce92023-04-28 12:08:12 +080062 static char revision[4];
63
64 if (IS_ENABLED(CONFIG_IMX8)) {
65 switch (rev) {
66 case CHIP_REV_A:
67 return "A";
68 case CHIP_REV_B:
69 return "B";
70 case CHIP_REV_C:
71 return "C";
72 default:
73 return "?";
74 }
75 } else {
76 revision[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
77 revision[1] = '.';
78 revision[2] = '0' + (rev & 0xf);
79 revision[3] = '\0';
80
81 return revision;
Peng Fan21981d22019-08-26 08:12:19 +000082 }
83}
84
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020085static void set_core_data(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +000086{
Simon Glassb75b15b2020-12-03 16:55:23 -070087 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020088
89 if (device_is_compatible(dev, "arm,cortex-a35")) {
90 plat->cpu_rsrc = SC_R_A35;
91 plat->name = "A35";
92 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
93 plat->cpu_rsrc = SC_R_A53;
94 plat->name = "A53";
95 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
96 plat->cpu_rsrc = SC_R_A72;
97 plat->name = "A72";
Peng Fan146cce92023-04-28 12:08:12 +080098 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
99 plat->name = "A55";
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200100 } else {
101 plat->cpu_rsrc = SC_R_A53;
102 plat->name = "?";
103 }
Peng Fan21981d22019-08-26 08:12:19 +0000104}
105
Peng Fan32eaf672023-04-28 12:08:13 +0800106#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700107static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000108{
109 struct udevice *thermal_dev;
110 int cpu_tmp, ret;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200111 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan21981d22019-08-26 08:12:19 +0000112
Peng Fan32eaf672023-04-28 12:08:13 +0800113 if (IS_ENABLED(CONFIG_IMX8)) {
114 if (plat->cpu_rsrc == SC_R_A72)
115 idx = 2; /* use "cpu-thermal1" device */
116 } else {
117 idx = 1;
118 }
Peng Fan21981d22019-08-26 08:12:19 +0000119
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200120 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan21981d22019-08-26 08:12:19 +0000121 if (!ret) {
122 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
123 if (ret)
124 return 0xdeadbeef;
125 } else {
126 return 0xdeadbeef;
127 }
128
129 return cpu_tmp;
130}
131#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700132static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000133{
134 return 0;
135}
136#endif
137
Peng Fan81c694a2023-04-28 12:08:14 +0800138__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
139{
140 return 0;
141}
142
Peng Fand3ee4de2023-04-28 12:08:11 +0800143static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000144{
Simon Glassb75b15b2020-12-03 16:55:23 -0700145 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan81c694a2023-04-28 12:08:14 +0800146 const char *grade;
Ye Licd8d1c52020-05-03 21:58:54 +0800147 int ret, temp;
Peng Fan81c694a2023-04-28 12:08:14 +0800148 int minc, maxc;
Peng Fan21981d22019-08-26 08:12:19 +0000149
150 if (size < 100)
151 return -ENOSPC;
152
Peng Fan146cce92023-04-28 12:08:12 +0800153 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan21981d22019-08-26 08:12:19 +0000154 plat->type, plat->rev, plat->name, plat->freq_mhz);
155
Peng Fan81c694a2023-04-28 12:08:14 +0800156 if (IS_ENABLED(CONFIG_IMX9)) {
157 switch (get_cpu_temp_grade(&minc, &maxc)) {
158 case TEMP_AUTOMOTIVE:
159 grade = "Automotive temperature grade ";
160 break;
161 case TEMP_INDUSTRIAL:
162 grade = "Industrial temperature grade ";
163 break;
164 case TEMP_EXTCOMMERCIAL:
165 grade = "Extended Consumer temperature grade ";
166 break;
167 default:
168 grade = "Consumer temperature grade ";
169 break;
170 }
171
172 buf = buf + ret;
173 size = size - ret;
174 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
175 }
176
Peng Fan32eaf672023-04-28 12:08:13 +0800177 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Licd8d1c52020-05-03 21:58:54 +0800178 temp = cpu_imx_get_temp(plat);
Peng Fan21981d22019-08-26 08:12:19 +0000179 buf = buf + ret;
180 size = size - ret;
Ye Licd8d1c52020-05-03 21:58:54 +0800181 if (temp != 0xdeadbeef)
182 ret = snprintf(buf, size, " at %dC", temp);
183 else
184 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan21981d22019-08-26 08:12:19 +0000185 }
186
187 snprintf(buf + ret, size - ret, "\n");
188
189 return 0;
190}
191
Simon Glass791fa452020-01-26 22:06:27 -0700192static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan21981d22019-08-26 08:12:19 +0000193{
Simon Glassb75b15b2020-12-03 16:55:23 -0700194 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000195
196 info->cpu_freq = plat->freq_mhz * 1000;
197 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
198 return 0;
199}
200
Simon Glass791fa452020-01-26 22:06:27 -0700201static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000202{
Peng Fan8296b742020-05-03 21:58:51 +0800203 ofnode node;
204 int num = 0;
205
206 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
207 const char *device_type;
208
Simon Glass2e4938b2022-09-06 20:27:17 -0600209 if (!ofnode_is_enabled(node))
Peng Fan8296b742020-05-03 21:58:51 +0800210 continue;
211
212 device_type = ofnode_read_string(node, "device_type");
213 if (!device_type)
214 continue;
215
216 if (!strcmp(device_type, "cpu"))
217 num++;
218 }
219
220 return num;
Peng Fan21981d22019-08-26 08:12:19 +0000221}
222
Simon Glass791fa452020-01-26 22:06:27 -0700223static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000224{
225 snprintf(buf, size, "NXP");
226 return 0;
227}
228
Peng Fane2ded332020-05-03 21:58:52 +0800229static int cpu_imx_is_current(struct udevice *dev)
230{
Simon Glassb75b15b2020-12-03 16:55:23 -0700231 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fane2ded332020-05-03 21:58:52 +0800232
233 if (plat->mpidr == (read_mpidr() & 0xffff))
234 return 1;
235
236 return 0;
237}
238
Peng Fan146cce92023-04-28 12:08:12 +0800239static const struct cpu_ops cpu_imx_ops = {
Peng Fan21981d22019-08-26 08:12:19 +0000240 .get_desc = cpu_imx_get_desc,
241 .get_info = cpu_imx_get_info,
242 .get_count = cpu_imx_get_count,
243 .get_vendor = cpu_imx_get_vendor,
Peng Fane2ded332020-05-03 21:58:52 +0800244 .is_current = cpu_imx_is_current,
Peng Fan21981d22019-08-26 08:12:19 +0000245};
246
Peng Fan146cce92023-04-28 12:08:12 +0800247static const struct udevice_id cpu_imx_ids[] = {
Peng Fan21981d22019-08-26 08:12:19 +0000248 { .compatible = "arm,cortex-a35" },
249 { .compatible = "arm,cortex-a53" },
Peng Fan146cce92023-04-28 12:08:12 +0800250 { .compatible = "arm,cortex-a55" },
Peng Fane2ded332020-05-03 21:58:52 +0800251 { .compatible = "arm,cortex-a72" },
Peng Fan21981d22019-08-26 08:12:19 +0000252 { }
253};
254
Peng Fan146cce92023-04-28 12:08:12 +0800255static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000256{
Simon Glassb75b15b2020-12-03 16:55:23 -0700257 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan146cce92023-04-28 12:08:12 +0800258 struct clk clk;
Peng Fan21981d22019-08-26 08:12:19 +0000259 ulong rate;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200260 int ret;
Peng Fan4b1fbb72020-05-03 21:58:53 +0800261
Peng Fan146cce92023-04-28 12:08:12 +0800262 if (IS_ENABLED(CONFIG_IMX8)) {
263 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
264 (sc_pm_clock_rate_t *)&rate);
265 } else {
266 ret = clk_get_by_index(dev, 0, &clk);
267 if (!ret) {
268 rate = clk_get_rate(&clk);
269 if (!rate)
270 ret = -EOPNOTSUPP;
271 }
272 }
Peng Fan21981d22019-08-26 08:12:19 +0000273 if (ret) {
274 printf("Could not read CPU frequency: %d\n", ret);
275 return 0;
276 }
277
278 return rate;
279}
280
Peng Fan146cce92023-04-28 12:08:12 +0800281static int imx_cpu_probe(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000282{
Simon Glassb75b15b2020-12-03 16:55:23 -0700283 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000284 u32 cpurev;
285
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200286 set_core_data(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000287 cpurev = get_cpu_rev();
288 plat->cpurev = cpurev;
Peng Fan146cce92023-04-28 12:08:12 +0800289 plat->rev = get_imx_rev_str(cpurev & 0xFFF);
290 plat->type = get_imx_type_str((cpurev & 0xFF000) >> 12);
291 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fane2ded332020-05-03 21:58:52 +0800292 plat->mpidr = dev_read_addr(dev);
293 if (plat->mpidr == FDT_ADDR_T_NONE) {
294 printf("%s: Failed to get CPU reg property\n", __func__);
295 return -EINVAL;
296 }
297
Peng Fan21981d22019-08-26 08:12:19 +0000298 return 0;
299}
300
Peng Fan146cce92023-04-28 12:08:12 +0800301U_BOOT_DRIVER(cpu_imx_drv) = {
302 .name = "imx_cpu",
Peng Fan21981d22019-08-26 08:12:19 +0000303 .id = UCLASS_CPU,
Peng Fan146cce92023-04-28 12:08:12 +0800304 .of_match = cpu_imx_ids,
305 .ops = &cpu_imx_ops,
306 .probe = imx_cpu_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700307 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan21981d22019-08-26 08:12:19 +0000308 .flags = DM_FLAG_PRE_RELOC,
309};