blob: 4836bddd93b13a2b1e0b75297ae1bfe3ad4e90b3 [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 */
Alice Guo22d6e2f2025-04-28 18:37:35 +0800114 case MXC_CPU_IMX95:
115 return "95";
Peng Fan21981d22019-08-26 08:12:19 +0000116 default:
117 return "??";
118 }
119}
120
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800121static void get_imx_rev_str(struct cpu_imx_plat *plat, u32 rev)
Peng Fan21981d22019-08-26 08:12:19 +0000122{
Peng Fan146cce92023-04-28 12:08:12 +0800123 if (IS_ENABLED(CONFIG_IMX8)) {
124 switch (rev) {
125 case CHIP_REV_A:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800126 plat->rev[0] = 'A';
127 break;
Peng Fan146cce92023-04-28 12:08:12 +0800128 case CHIP_REV_B:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800129 plat->rev[0] = 'B';
130 break;
Peng Fan146cce92023-04-28 12:08:12 +0800131 case CHIP_REV_C:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800132 plat->rev[0] = 'C';
133 break;
Peng Fan146cce92023-04-28 12:08:12 +0800134 default:
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800135 plat->rev[0] = '?';
136 break;
Peng Fan146cce92023-04-28 12:08:12 +0800137 }
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800138 plat->rev[1] = '\0';
Peng Fan146cce92023-04-28 12:08:12 +0800139 } else {
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800140 plat->rev[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
141 plat->rev[1] = '.';
142 plat->rev[2] = '0' + (rev & 0xf);
143 plat->rev[3] = '\0';
Peng Fan21981d22019-08-26 08:12:19 +0000144 }
145}
146
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200147static void set_core_data(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000148{
Simon Glassb75b15b2020-12-03 16:55:23 -0700149 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200150
151 if (device_is_compatible(dev, "arm,cortex-a35")) {
152 plat->cpu_rsrc = SC_R_A35;
153 plat->name = "A35";
154 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
155 plat->cpu_rsrc = SC_R_A53;
156 plat->name = "A53";
157 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
158 plat->cpu_rsrc = SC_R_A72;
159 plat->name = "A72";
Peng Fan146cce92023-04-28 12:08:12 +0800160 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
161 plat->name = "A55";
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200162 } else {
163 plat->cpu_rsrc = SC_R_A53;
164 plat->name = "?";
165 }
Peng Fan21981d22019-08-26 08:12:19 +0000166}
167
Peng Fan32eaf672023-04-28 12:08:13 +0800168#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700169static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000170{
171 struct udevice *thermal_dev;
172 int cpu_tmp, ret;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200173 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan21981d22019-08-26 08:12:19 +0000174
Peng Fan32eaf672023-04-28 12:08:13 +0800175 if (IS_ENABLED(CONFIG_IMX8)) {
176 if (plat->cpu_rsrc == SC_R_A72)
177 idx = 2; /* use "cpu-thermal1" device */
Peng Fan0ce300f2024-12-03 23:42:48 +0800178 } else if (IS_ENABLED(CONFIG_IMX91)) {
179 idx = 0;
Peng Fan32eaf672023-04-28 12:08:13 +0800180 } else {
181 idx = 1;
182 }
Peng Fan21981d22019-08-26 08:12:19 +0000183
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200184 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan21981d22019-08-26 08:12:19 +0000185 if (!ret) {
186 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
187 if (ret)
188 return 0xdeadbeef;
189 } else {
190 return 0xdeadbeef;
191 }
192
193 return cpu_tmp;
194}
195#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700196static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000197{
198 return 0;
199}
200#endif
201
Peng Fan81c694a2023-04-28 12:08:14 +0800202__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
203{
204 return 0;
205}
206
Peng Fand3ee4de2023-04-28 12:08:11 +0800207static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000208{
Simon Glassb75b15b2020-12-03 16:55:23 -0700209 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan81c694a2023-04-28 12:08:14 +0800210 const char *grade;
Ye Licd8d1c52020-05-03 21:58:54 +0800211 int ret, temp;
Peng Fan81c694a2023-04-28 12:08:14 +0800212 int minc, maxc;
Peng Fan21981d22019-08-26 08:12:19 +0000213
214 if (size < 100)
215 return -ENOSPC;
216
Peng Fan146cce92023-04-28 12:08:12 +0800217 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan21981d22019-08-26 08:12:19 +0000218 plat->type, plat->rev, plat->name, plat->freq_mhz);
219
Adam Ford0b69ff22025-03-24 21:54:45 -0500220 if (IS_ENABLED(CONFIG_IMX_TMU)) {
Peng Fan81c694a2023-04-28 12:08:14 +0800221 switch (get_cpu_temp_grade(&minc, &maxc)) {
222 case TEMP_AUTOMOTIVE:
Adam Ford0b69ff22025-03-24 21:54:45 -0500223 grade = "Automotive temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800224 break;
225 case TEMP_INDUSTRIAL:
Adam Ford0b69ff22025-03-24 21:54:45 -0500226 grade = "Industrial temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800227 break;
228 case TEMP_EXTCOMMERCIAL:
Adam Ford0b69ff22025-03-24 21:54:45 -0500229 grade = "Extended Consumer temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800230 break;
231 default:
Adam Ford0b69ff22025-03-24 21:54:45 -0500232 grade = "Consumer temperature grade";
Peng Fan81c694a2023-04-28 12:08:14 +0800233 break;
234 }
235
236 buf = buf + ret;
237 size = size - ret;
238 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
239 }
240
Peng Fan32eaf672023-04-28 12:08:13 +0800241 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Licd8d1c52020-05-03 21:58:54 +0800242 temp = cpu_imx_get_temp(plat);
Peng Fan21981d22019-08-26 08:12:19 +0000243 buf = buf + ret;
244 size = size - ret;
Ye Licd8d1c52020-05-03 21:58:54 +0800245 if (temp != 0xdeadbeef)
246 ret = snprintf(buf, size, " at %dC", temp);
247 else
248 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan21981d22019-08-26 08:12:19 +0000249 }
250
Peng Fan21981d22019-08-26 08:12:19 +0000251 return 0;
252}
253
Simon Glass791fa452020-01-26 22:06:27 -0700254static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan21981d22019-08-26 08:12:19 +0000255{
Simon Glassb75b15b2020-12-03 16:55:23 -0700256 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000257
Hou Zhiqianga1124da2024-08-01 11:59:50 +0800258 info->cpu_freq = plat->freq_mhz * 1000000;
Peng Fan21981d22019-08-26 08:12:19 +0000259 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
260 return 0;
261}
262
Simon Glass791fa452020-01-26 22:06:27 -0700263static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000264{
Peng Fan8296b742020-05-03 21:58:51 +0800265 ofnode node;
266 int num = 0;
267
268 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
269 const char *device_type;
270
Simon Glass2e4938b2022-09-06 20:27:17 -0600271 if (!ofnode_is_enabled(node))
Peng Fan8296b742020-05-03 21:58:51 +0800272 continue;
273
274 device_type = ofnode_read_string(node, "device_type");
275 if (!device_type)
276 continue;
277
278 if (!strcmp(device_type, "cpu"))
279 num++;
280 }
281
282 return num;
Peng Fan21981d22019-08-26 08:12:19 +0000283}
284
Simon Glass791fa452020-01-26 22:06:27 -0700285static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000286{
287 snprintf(buf, size, "NXP");
288 return 0;
289}
290
Peng Fane2ded332020-05-03 21:58:52 +0800291static int cpu_imx_is_current(struct udevice *dev)
292{
Simon Glassb75b15b2020-12-03 16:55:23 -0700293 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fane2ded332020-05-03 21:58:52 +0800294
295 if (plat->mpidr == (read_mpidr() & 0xffff))
296 return 1;
297
298 return 0;
299}
300
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800301static int cpu_imx_release_core(const struct udevice *dev, phys_addr_t addr)
302{
303 struct cpu_imx_plat *plat = dev_get_plat(dev);
304 struct pt_regs regs;
305
306 regs.regs[0] = PSCI_0_2_FN64_CPU_ON;
307 regs.regs[1] = plat->mpidr;
308 regs.regs[2] = addr;
309 regs.regs[3] = 0;
310
311 smc_call(&regs);
312 if (regs.regs[0]) {
313 printf("Failed to release CPU core (mpidr: 0x%x)\n", plat->mpidr);
314 return -1;
315 }
316
317 printf("Released CPU core (mpidr: 0x%x) to address 0x%llx\n", plat->mpidr, addr);
318
319 return 0;
320}
321
Peng Fan146cce92023-04-28 12:08:12 +0800322static const struct cpu_ops cpu_imx_ops = {
Peng Fan21981d22019-08-26 08:12:19 +0000323 .get_desc = cpu_imx_get_desc,
324 .get_info = cpu_imx_get_info,
325 .get_count = cpu_imx_get_count,
326 .get_vendor = cpu_imx_get_vendor,
Peng Fane2ded332020-05-03 21:58:52 +0800327 .is_current = cpu_imx_is_current,
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800328 .release_core = cpu_imx_release_core,
Peng Fan21981d22019-08-26 08:12:19 +0000329};
330
Peng Fan146cce92023-04-28 12:08:12 +0800331static const struct udevice_id cpu_imx_ids[] = {
Peng Fan21981d22019-08-26 08:12:19 +0000332 { .compatible = "arm,cortex-a35" },
333 { .compatible = "arm,cortex-a53" },
Peng Fan146cce92023-04-28 12:08:12 +0800334 { .compatible = "arm,cortex-a55" },
Peng Fane2ded332020-05-03 21:58:52 +0800335 { .compatible = "arm,cortex-a72" },
Peng Fan21981d22019-08-26 08:12:19 +0000336 { }
337};
338
Peng Fan146cce92023-04-28 12:08:12 +0800339static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000340{
Simon Glassb75b15b2020-12-03 16:55:23 -0700341 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan146cce92023-04-28 12:08:12 +0800342 struct clk clk;
Peng Fan21981d22019-08-26 08:12:19 +0000343 ulong rate;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200344 int ret;
Peng Fan4b1fbb72020-05-03 21:58:53 +0800345
Peng Fan146cce92023-04-28 12:08:12 +0800346 if (IS_ENABLED(CONFIG_IMX8)) {
347 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
348 (sc_pm_clock_rate_t *)&rate);
349 } else {
350 ret = clk_get_by_index(dev, 0, &clk);
351 if (!ret) {
352 rate = clk_get_rate(&clk);
353 if (!rate)
354 ret = -EOPNOTSUPP;
355 }
356 }
Peng Fan21981d22019-08-26 08:12:19 +0000357 if (ret) {
358 printf("Could not read CPU frequency: %d\n", ret);
359 return 0;
360 }
361
362 return rate;
363}
364
Peng Fan146cce92023-04-28 12:08:12 +0800365static int imx_cpu_probe(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000366{
Simon Glassb75b15b2020-12-03 16:55:23 -0700367 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000368 u32 cpurev;
369
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200370 set_core_data(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000371 cpurev = get_cpu_rev();
372 plat->cpurev = cpurev;
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800373 get_imx_rev_str(plat, cpurev & 0xFFF);
Hou Zhiqiangc5e1a112024-08-01 11:59:51 +0800374 plat->type = get_imx_type_str((cpurev & 0x1FF000) >> 12);
Peng Fan146cce92023-04-28 12:08:12 +0800375 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fane2ded332020-05-03 21:58:52 +0800376 plat->mpidr = dev_read_addr(dev);
377 if (plat->mpidr == FDT_ADDR_T_NONE) {
378 printf("%s: Failed to get CPU reg property\n", __func__);
379 return -EINVAL;
380 }
381
Peng Fan21981d22019-08-26 08:12:19 +0000382 return 0;
383}
384
Peng Fan146cce92023-04-28 12:08:12 +0800385U_BOOT_DRIVER(cpu_imx_drv) = {
386 .name = "imx_cpu",
Peng Fan21981d22019-08-26 08:12:19 +0000387 .id = UCLASS_CPU,
Peng Fan146cce92023-04-28 12:08:12 +0800388 .of_match = cpu_imx_ids,
389 .ops = &cpu_imx_ops,
390 .probe = imx_cpu_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700391 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan21981d22019-08-26 08:12:19 +0000392 .flags = DM_FLAG_PRE_RELOC,
393};