blob: 4e1eccaa5b0b54a5ddddaa6c17e05b6c8f102652 [file] [log] [blame]
Peng Fan21981d22019-08-26 08:12:19 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
Hou Zhiqianga1124da2024-08-01 11:59:50 +08003 * Copyright 2019, 2024 NXP
Peng Fan21981d22019-08-26 08:12:19 +00004 */
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>
Hou Zhiqiang863a1542024-08-01 11:59:54 +080010#include <asm/ptrace.h>
Simon Glass274e0b02020-05-10 11:39:56 -060011#include <asm/system.h>
Peng Fan2e0644a2023-04-28 12:08:09 +080012#include <firmware/imx/sci/sci.h>
Peng Fan21981d22019-08-26 08:12:19 +000013#include <asm/arch/sys_proto.h>
14#include <asm/arch-imx/cpu.h>
15#include <asm/armv8/cpu.h>
Peng Fan81c694a2023-04-28 12:08:14 +080016#include <imx_thermal.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Peng Fan146cce92023-04-28 12:08:12 +080018#include <linux/clk-provider.h>
Hou Zhiqiang863a1542024-08-01 11:59:54 +080019#include <linux/psci.h>
Peng Fan21981d22019-08-26 08:12:19 +000020
21DECLARE_GLOBAL_DATA_PTR;
22
Peng Fan7d5e7aa2024-10-18 15:34:32 +080023#define IMX_REV_LEN 4
Simon Glassb75b15b2020-12-03 16:55:23 -070024struct cpu_imx_plat {
Peng Fan21981d22019-08-26 08:12:19 +000025 const char *name;
Peng Fan21981d22019-08-26 08:12:19 +000026 const char *type;
Peng Fan7d5e7aa2024-10-18 15:34:32 +080027 char rev[IMX_REV_LEN];
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020028 u32 cpu_rsrc;
Peng Fan21981d22019-08-26 08:12:19 +000029 u32 cpurev;
30 u32 freq_mhz;
Peng Fane2ded332020-05-03 21:58:52 +080031 u32 mpidr;
Peng Fan21981d22019-08-26 08:12:19 +000032};
33
Peng Fan146cce92023-04-28 12:08:12 +080034static const char *get_imx_type_str(u32 imxtype)
Peng Fan21981d22019-08-26 08:12:19 +000035{
36 switch (imxtype) {
Hou Zhiqiang78ff9db2024-08-01 11:59:53 +080037 case MXC_CPU_IMX8MM:
Adam Ford8b453112025-03-24 21:54:43 -050038 return "8MMQ"; /* Quad-core version of the imx8mm */
39 case MXC_CPU_IMX8MML:
40 return "8MMQL"; /* Quad-core Lite version of the imx8mm */
41 case MXC_CPU_IMX8MMD:
42 return "8MMD"; /* Dual-core version of the imx8mm */
43 case MXC_CPU_IMX8MMDL:
44 return "8MMDL"; /* Dual-core Lite version of the imx8mm */
45 case MXC_CPU_IMX8MMS:
46 return "8MMS"; /* Single-core version of the imx8mm */
47 case MXC_CPU_IMX8MMSL:
48 return "8MMSL"; /* Single-core Lite version of the imx8mm */
Hou Zhiqiang78ff9db2024-08-01 11:59:53 +080049 case MXC_CPU_IMX8MN:
Adam Ford8b453112025-03-24 21:54:43 -050050 return "8MNano Quad"; /* Quad-core version */
51 case MXC_CPU_IMX8MND:
52 return "8MNano Dual"; /* Dual-core version */
53 case MXC_CPU_IMX8MNS:
54 return "8MNano Solo"; /* Single-core version */
55 case MXC_CPU_IMX8MNL:
56 return "8MNano QuadLite"; /* Quad-core Lite version */
57 case MXC_CPU_IMX8MNDL:
58 return "8MNano DualLite"; /* Dual-core Lite version */
59 case MXC_CPU_IMX8MNSL:
60 return "8MNano SoloLite";/* Single-core Lite version of the imx8mn */
61 case MXC_CPU_IMX8MNUQ:
62 return "8MNano UltraLite Quad";/* Quad-core UltraLite version of the imx8mn */
63 case MXC_CPU_IMX8MNUD:
64 return "8MNano UltraLite Dual";/* Dual-core UltraLite version of the imx8mn */
65 case MXC_CPU_IMX8MNUS:
66 return "8MNano UltraLite Solo";/* Single-core UltraLite version of the imx8mn */
Hou Zhiqiang78ff9db2024-08-01 11:59:53 +080067 case MXC_CPU_IMX8MP:
Adam Ford8b453112025-03-24 21:54:43 -050068 return "8MP[8]"; /* Quad-core version of the imx8mp */
69 case MXC_CPU_IMX8MPD:
70 return "8MP Dual[3]"; /* Dual-core version of the imx8mp */
71 case MXC_CPU_IMX8MPL:
72 return "8MP Lite[4]"; /* Quad-core Lite version of the imx8mp */
73 case MXC_CPU_IMX8MP6:
74 return "8MP[6]"; /* Quad-core version of the imx8mp, NPU fused */
75 case MXC_CPU_IMX8MQ:
76 return "8MQ"; /* Quad-core version of the imx8mq */
77 case MXC_CPU_IMX8MQL:
78 return "8MQLite"; /* Quad-core Lite version of the imx8mq */
79 case MXC_CPU_IMX8MD:
80 return "8MD"; /* Dual-core version of the imx8mq */
Peng Fan21981d22019-08-26 08:12:19 +000081 case MXC_CPU_IMX8QXP:
82 case MXC_CPU_IMX8QXP_A0:
Peng Fan146cce92023-04-28 12:08:12 +080083 return "8QXP";
Peng Fan21981d22019-08-26 08:12:19 +000084 case MXC_CPU_IMX8QM:
Peng Fan146cce92023-04-28 12:08:12 +080085 return "8QM";
86 case MXC_CPU_IMX93:
87 return "93(52)";/* iMX93 Dual core with NPU */
Peng Fanc3db3ad2023-04-28 12:08:32 +080088 case MXC_CPU_IMX9351:
89 return "93(51)";/* iMX93 Single core with NPU */
90 case MXC_CPU_IMX9332:
91 return "93(32)";/* iMX93 Dual core without NPU */
92 case MXC_CPU_IMX9331:
93 return "93(31)";/* iMX93 Single core without NPU */
94 case MXC_CPU_IMX9322:
95 return "93(22)";/* iMX93 9x9 Dual core */
96 case MXC_CPU_IMX9321:
97 return "93(21)";/* iMX93 9x9 Single core */
98 case MXC_CPU_IMX9312:
99 return "93(12)";/* iMX93 9x9 Dual core without NPU */
100 case MXC_CPU_IMX9311:
101 return "93(11)";/* iMX93 9x9 Single core without NPU */
Ye Li57b2ac42024-09-19 12:01:33 +0800102 case MXC_CPU_IMX9302:
103 return "93(02)";/* iMX93 900Mhz Low performance Dual core without NPU */
104 case MXC_CPU_IMX9301:
105 return "93(01)";/* iMX93 900Mhz Low performance Single core without NPU */
Peng Fan0ce300f2024-12-03 23:42:48 +0800106 case MXC_CPU_IMX91:
107 return "91(31)";/* iMX91 11x11 Full feature */
108 case MXC_CPU_IMX9121:
109 return "91(21)";/* iMX91 11x11 Low drive mode */
110 case MXC_CPU_IMX9111:
111 return "91(11)";/* iMX91 9x9 Reduced feature */
112 case MXC_CPU_IMX9101:
113 return "91(01)";/* iMX91 9x9 Specific feature */
Peng Fan21981d22019-08-26 08:12:19 +0000114 default:
115 return "??";
116 }
117}
118
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800119static void get_imx_rev_str(struct cpu_imx_plat *plat, u32 rev)
Peng Fan21981d22019-08-26 08:12:19 +0000120{
Peng Fan146cce92023-04-28 12:08:12 +0800121 if (IS_ENABLED(CONFIG_IMX8)) {
122 switch (rev) {
123 case CHIP_REV_A:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800124 plat->rev[0] = 'A';
125 break;
Peng Fan146cce92023-04-28 12:08:12 +0800126 case CHIP_REV_B:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800127 plat->rev[0] = 'B';
128 break;
Peng Fan146cce92023-04-28 12:08:12 +0800129 case CHIP_REV_C:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800130 plat->rev[0] = 'C';
131 break;
Peng Fan146cce92023-04-28 12:08:12 +0800132 default:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800133 plat->rev[0] = '?';
134 break;
Peng Fan146cce92023-04-28 12:08:12 +0800135 }
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800136 plat->rev[1] = '\0';
Peng Fan146cce92023-04-28 12:08:12 +0800137 } else {
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800138 plat->rev[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
139 plat->rev[1] = '.';
140 plat->rev[2] = '0' + (rev & 0xf);
141 plat->rev[3] = '\0';
Peng Fan21981d22019-08-26 08:12:19 +0000142 }
143}
144
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200145static void set_core_data(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000146{
Simon Glassb75b15b2020-12-03 16:55:23 -0700147 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200148
149 if (device_is_compatible(dev, "arm,cortex-a35")) {
150 plat->cpu_rsrc = SC_R_A35;
151 plat->name = "A35";
152 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
153 plat->cpu_rsrc = SC_R_A53;
154 plat->name = "A53";
155 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
156 plat->cpu_rsrc = SC_R_A72;
157 plat->name = "A72";
Peng Fan146cce92023-04-28 12:08:12 +0800158 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
159 plat->name = "A55";
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200160 } else {
161 plat->cpu_rsrc = SC_R_A53;
162 plat->name = "?";
163 }
Peng Fan21981d22019-08-26 08:12:19 +0000164}
165
Peng Fan32eaf672023-04-28 12:08:13 +0800166#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700167static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000168{
169 struct udevice *thermal_dev;
170 int cpu_tmp, ret;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200171 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan21981d22019-08-26 08:12:19 +0000172
Peng Fan32eaf672023-04-28 12:08:13 +0800173 if (IS_ENABLED(CONFIG_IMX8)) {
174 if (plat->cpu_rsrc == SC_R_A72)
175 idx = 2; /* use "cpu-thermal1" device */
Peng Fan0ce300f2024-12-03 23:42:48 +0800176 } else if (IS_ENABLED(CONFIG_IMX91)) {
177 idx = 0;
Peng Fan32eaf672023-04-28 12:08:13 +0800178 } else {
179 idx = 1;
180 }
Peng Fan21981d22019-08-26 08:12:19 +0000181
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200182 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan21981d22019-08-26 08:12:19 +0000183 if (!ret) {
184 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
185 if (ret)
186 return 0xdeadbeef;
187 } else {
188 return 0xdeadbeef;
189 }
190
191 return cpu_tmp;
192}
193#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700194static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000195{
196 return 0;
197}
198#endif
199
Peng Fan81c694a2023-04-28 12:08:14 +0800200__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
201{
202 return 0;
203}
204
Peng Fand3ee4de2023-04-28 12:08:11 +0800205static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000206{
Simon Glassb75b15b2020-12-03 16:55:23 -0700207 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan81c694a2023-04-28 12:08:14 +0800208 const char *grade;
Ye Licd8d1c52020-05-03 21:58:54 +0800209 int ret, temp;
Peng Fan81c694a2023-04-28 12:08:14 +0800210 int minc, maxc;
Peng Fan21981d22019-08-26 08:12:19 +0000211
212 if (size < 100)
213 return -ENOSPC;
214
Peng Fan146cce92023-04-28 12:08:12 +0800215 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan21981d22019-08-26 08:12:19 +0000216 plat->type, plat->rev, plat->name, plat->freq_mhz);
217
Adam Ford0b69ff22025-03-24 21:54:45 -0500218 if (IS_ENABLED(CONFIG_IMX_TMU)) {
Peng Fan81c694a2023-04-28 12:08:14 +0800219 switch (get_cpu_temp_grade(&minc, &maxc)) {
220 case TEMP_AUTOMOTIVE:
Adam Ford0b69ff22025-03-24 21:54:45 -0500221 grade = "Automotive temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800222 break;
223 case TEMP_INDUSTRIAL:
Adam Ford0b69ff22025-03-24 21:54:45 -0500224 grade = "Industrial temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800225 break;
226 case TEMP_EXTCOMMERCIAL:
Adam Ford0b69ff22025-03-24 21:54:45 -0500227 grade = "Extended Consumer temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800228 break;
229 default:
Adam Ford0b69ff22025-03-24 21:54:45 -0500230 grade = "Consumer temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800231 break;
232 }
233
234 buf = buf + ret;
235 size = size - ret;
236 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
237 }
238
Peng Fan32eaf672023-04-28 12:08:13 +0800239 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Licd8d1c52020-05-03 21:58:54 +0800240 temp = cpu_imx_get_temp(plat);
Peng Fan21981d22019-08-26 08:12:19 +0000241 buf = buf + ret;
242 size = size - ret;
Ye Licd8d1c52020-05-03 21:58:54 +0800243 if (temp != 0xdeadbeef)
244 ret = snprintf(buf, size, " at %dC", temp);
245 else
246 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan21981d22019-08-26 08:12:19 +0000247 }
248
Peng Fan21981d22019-08-26 08:12:19 +0000249 return 0;
250}
251
Simon Glass791fa452020-01-26 22:06:27 -0700252static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan21981d22019-08-26 08:12:19 +0000253{
Simon Glassb75b15b2020-12-03 16:55:23 -0700254 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000255
Hou Zhiqianga1124da2024-08-01 11:59:50 +0800256 info->cpu_freq = plat->freq_mhz * 1000000;
Peng Fan21981d22019-08-26 08:12:19 +0000257 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
258 return 0;
259}
260
Simon Glass791fa452020-01-26 22:06:27 -0700261static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000262{
Peng Fan8296b742020-05-03 21:58:51 +0800263 ofnode node;
264 int num = 0;
265
266 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
267 const char *device_type;
268
Simon Glass2e4938b2022-09-06 20:27:17 -0600269 if (!ofnode_is_enabled(node))
Peng Fan8296b742020-05-03 21:58:51 +0800270 continue;
271
272 device_type = ofnode_read_string(node, "device_type");
273 if (!device_type)
274 continue;
275
276 if (!strcmp(device_type, "cpu"))
277 num++;
278 }
279
280 return num;
Peng Fan21981d22019-08-26 08:12:19 +0000281}
282
Simon Glass791fa452020-01-26 22:06:27 -0700283static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000284{
285 snprintf(buf, size, "NXP");
286 return 0;
287}
288
Peng Fane2ded332020-05-03 21:58:52 +0800289static int cpu_imx_is_current(struct udevice *dev)
290{
Simon Glassb75b15b2020-12-03 16:55:23 -0700291 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fane2ded332020-05-03 21:58:52 +0800292
293 if (plat->mpidr == (read_mpidr() & 0xffff))
294 return 1;
295
296 return 0;
297}
298
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800299static int cpu_imx_release_core(const struct udevice *dev, phys_addr_t addr)
300{
301 struct cpu_imx_plat *plat = dev_get_plat(dev);
302 struct pt_regs regs;
303
304 regs.regs[0] = PSCI_0_2_FN64_CPU_ON;
305 regs.regs[1] = plat->mpidr;
306 regs.regs[2] = addr;
307 regs.regs[3] = 0;
308
309 smc_call(&regs);
310 if (regs.regs[0]) {
311 printf("Failed to release CPU core (mpidr: 0x%x)\n", plat->mpidr);
312 return -1;
313 }
314
315 printf("Released CPU core (mpidr: 0x%x) to address 0x%llx\n", plat->mpidr, addr);
316
317 return 0;
318}
319
Peng Fan146cce92023-04-28 12:08:12 +0800320static const struct cpu_ops cpu_imx_ops = {
Peng Fan21981d22019-08-26 08:12:19 +0000321 .get_desc = cpu_imx_get_desc,
322 .get_info = cpu_imx_get_info,
323 .get_count = cpu_imx_get_count,
324 .get_vendor = cpu_imx_get_vendor,
Peng Fane2ded332020-05-03 21:58:52 +0800325 .is_current = cpu_imx_is_current,
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800326 .release_core = cpu_imx_release_core,
Peng Fan21981d22019-08-26 08:12:19 +0000327};
328
Peng Fan146cce92023-04-28 12:08:12 +0800329static const struct udevice_id cpu_imx_ids[] = {
Peng Fan21981d22019-08-26 08:12:19 +0000330 { .compatible = "arm,cortex-a35" },
331 { .compatible = "arm,cortex-a53" },
Peng Fan146cce92023-04-28 12:08:12 +0800332 { .compatible = "arm,cortex-a55" },
Peng Fane2ded332020-05-03 21:58:52 +0800333 { .compatible = "arm,cortex-a72" },
Peng Fan21981d22019-08-26 08:12:19 +0000334 { }
335};
336
Peng Fan146cce92023-04-28 12:08:12 +0800337static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000338{
Simon Glassb75b15b2020-12-03 16:55:23 -0700339 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan146cce92023-04-28 12:08:12 +0800340 struct clk clk;
Peng Fan21981d22019-08-26 08:12:19 +0000341 ulong rate;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200342 int ret;
Peng Fan4b1fbb72020-05-03 21:58:53 +0800343
Peng Fan146cce92023-04-28 12:08:12 +0800344 if (IS_ENABLED(CONFIG_IMX8)) {
345 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
346 (sc_pm_clock_rate_t *)&rate);
347 } else {
348 ret = clk_get_by_index(dev, 0, &clk);
349 if (!ret) {
350 rate = clk_get_rate(&clk);
351 if (!rate)
352 ret = -EOPNOTSUPP;
353 }
354 }
Peng Fan21981d22019-08-26 08:12:19 +0000355 if (ret) {
356 printf("Could not read CPU frequency: %d\n", ret);
357 return 0;
358 }
359
360 return rate;
361}
362
Peng Fan146cce92023-04-28 12:08:12 +0800363static int imx_cpu_probe(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000364{
Simon Glassb75b15b2020-12-03 16:55:23 -0700365 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000366 u32 cpurev;
367
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200368 set_core_data(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000369 cpurev = get_cpu_rev();
370 plat->cpurev = cpurev;
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800371 get_imx_rev_str(plat, cpurev & 0xFFF);
Hou Zhiqiangc5e1a112024-08-01 11:59:51 +0800372 plat->type = get_imx_type_str((cpurev & 0x1FF000) >> 12);
Peng Fan146cce92023-04-28 12:08:12 +0800373 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fane2ded332020-05-03 21:58:52 +0800374 plat->mpidr = dev_read_addr(dev);
375 if (plat->mpidr == FDT_ADDR_T_NONE) {
376 printf("%s: Failed to get CPU reg property\n", __func__);
377 return -EINVAL;
378 }
379
Peng Fan21981d22019-08-26 08:12:19 +0000380 return 0;
381}
382
Peng Fan146cce92023-04-28 12:08:12 +0800383U_BOOT_DRIVER(cpu_imx_drv) = {
384 .name = "imx_cpu",
Peng Fan21981d22019-08-26 08:12:19 +0000385 .id = UCLASS_CPU,
Peng Fan146cce92023-04-28 12:08:12 +0800386 .of_match = cpu_imx_ids,
387 .ops = &cpu_imx_ops,
388 .probe = imx_cpu_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700389 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan21981d22019-08-26 08:12:19 +0000390 .flags = DM_FLAG_PRE_RELOC,
391};