blob: 6c0a8c0cbe40af8e411599f21ac32d2f7e1fe9d8 [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
Simon Glassb75b15b2020-12-03 16:55:23 -070023struct cpu_imx_plat {
Peng Fan21981d22019-08-26 08:12:19 +000024 const char *name;
25 const char *rev;
26 const char *type;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020027 u32 cpu_rsrc;
Peng Fan21981d22019-08-26 08:12:19 +000028 u32 cpurev;
29 u32 freq_mhz;
Peng Fane2ded332020-05-03 21:58:52 +080030 u32 mpidr;
Peng Fan21981d22019-08-26 08:12:19 +000031};
32
Peng Fan146cce92023-04-28 12:08:12 +080033static const char *get_imx_type_str(u32 imxtype)
Peng Fan21981d22019-08-26 08:12:19 +000034{
35 switch (imxtype) {
Hou Zhiqiang78ff9db2024-08-01 11:59:53 +080036 case MXC_CPU_IMX8MM:
37 return "8MM";
38 case MXC_CPU_IMX8MN:
39 return "8MN";
40 case MXC_CPU_IMX8MP:
41 return "8MP";
Peng Fan21981d22019-08-26 08:12:19 +000042 case MXC_CPU_IMX8QXP:
43 case MXC_CPU_IMX8QXP_A0:
Peng Fan146cce92023-04-28 12:08:12 +080044 return "8QXP";
Peng Fan21981d22019-08-26 08:12:19 +000045 case MXC_CPU_IMX8QM:
Peng Fan146cce92023-04-28 12:08:12 +080046 return "8QM";
47 case MXC_CPU_IMX93:
48 return "93(52)";/* iMX93 Dual core with NPU */
Peng Fanc3db3ad2023-04-28 12:08:32 +080049 case MXC_CPU_IMX9351:
50 return "93(51)";/* iMX93 Single core with NPU */
51 case MXC_CPU_IMX9332:
52 return "93(32)";/* iMX93 Dual core without NPU */
53 case MXC_CPU_IMX9331:
54 return "93(31)";/* iMX93 Single core without NPU */
55 case MXC_CPU_IMX9322:
56 return "93(22)";/* iMX93 9x9 Dual core */
57 case MXC_CPU_IMX9321:
58 return "93(21)";/* iMX93 9x9 Single core */
59 case MXC_CPU_IMX9312:
60 return "93(12)";/* iMX93 9x9 Dual core without NPU */
61 case MXC_CPU_IMX9311:
62 return "93(11)";/* iMX93 9x9 Single core without NPU */
Ye Li57b2ac42024-09-19 12:01:33 +080063 case MXC_CPU_IMX9302:
64 return "93(02)";/* iMX93 900Mhz Low performance Dual core without NPU */
65 case MXC_CPU_IMX9301:
66 return "93(01)";/* iMX93 900Mhz Low performance Single core without NPU */
Peng Fan21981d22019-08-26 08:12:19 +000067 default:
68 return "??";
69 }
70}
71
Peng Fan146cce92023-04-28 12:08:12 +080072static const char *get_imx_rev_str(u32 rev)
Peng Fan21981d22019-08-26 08:12:19 +000073{
Peng Fan146cce92023-04-28 12:08:12 +080074 static char revision[4];
75
76 if (IS_ENABLED(CONFIG_IMX8)) {
77 switch (rev) {
78 case CHIP_REV_A:
79 return "A";
80 case CHIP_REV_B:
81 return "B";
82 case CHIP_REV_C:
83 return "C";
84 default:
85 return "?";
86 }
87 } else {
88 revision[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
89 revision[1] = '.';
90 revision[2] = '0' + (rev & 0xf);
91 revision[3] = '\0';
92
93 return revision;
Peng Fan21981d22019-08-26 08:12:19 +000094 }
95}
96
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020097static void set_core_data(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +000098{
Simon Glassb75b15b2020-12-03 16:55:23 -070099 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200100
101 if (device_is_compatible(dev, "arm,cortex-a35")) {
102 plat->cpu_rsrc = SC_R_A35;
103 plat->name = "A35";
104 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
105 plat->cpu_rsrc = SC_R_A53;
106 plat->name = "A53";
107 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
108 plat->cpu_rsrc = SC_R_A72;
109 plat->name = "A72";
Peng Fan146cce92023-04-28 12:08:12 +0800110 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
111 plat->name = "A55";
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200112 } else {
113 plat->cpu_rsrc = SC_R_A53;
114 plat->name = "?";
115 }
Peng Fan21981d22019-08-26 08:12:19 +0000116}
117
Peng Fan32eaf672023-04-28 12:08:13 +0800118#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700119static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000120{
121 struct udevice *thermal_dev;
122 int cpu_tmp, ret;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200123 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan21981d22019-08-26 08:12:19 +0000124
Peng Fan32eaf672023-04-28 12:08:13 +0800125 if (IS_ENABLED(CONFIG_IMX8)) {
126 if (plat->cpu_rsrc == SC_R_A72)
127 idx = 2; /* use "cpu-thermal1" device */
128 } else {
129 idx = 1;
130 }
Peng Fan21981d22019-08-26 08:12:19 +0000131
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200132 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan21981d22019-08-26 08:12:19 +0000133 if (!ret) {
134 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
135 if (ret)
136 return 0xdeadbeef;
137 } else {
138 return 0xdeadbeef;
139 }
140
141 return cpu_tmp;
142}
143#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700144static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000145{
146 return 0;
147}
148#endif
149
Peng Fan81c694a2023-04-28 12:08:14 +0800150__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
151{
152 return 0;
153}
154
Peng Fand3ee4de2023-04-28 12:08:11 +0800155static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000156{
Simon Glassb75b15b2020-12-03 16:55:23 -0700157 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan81c694a2023-04-28 12:08:14 +0800158 const char *grade;
Ye Licd8d1c52020-05-03 21:58:54 +0800159 int ret, temp;
Peng Fan81c694a2023-04-28 12:08:14 +0800160 int minc, maxc;
Peng Fan21981d22019-08-26 08:12:19 +0000161
162 if (size < 100)
163 return -ENOSPC;
164
Peng Fan146cce92023-04-28 12:08:12 +0800165 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan21981d22019-08-26 08:12:19 +0000166 plat->type, plat->rev, plat->name, plat->freq_mhz);
167
Peng Fan81c694a2023-04-28 12:08:14 +0800168 if (IS_ENABLED(CONFIG_IMX9)) {
169 switch (get_cpu_temp_grade(&minc, &maxc)) {
170 case TEMP_AUTOMOTIVE:
171 grade = "Automotive temperature grade ";
172 break;
173 case TEMP_INDUSTRIAL:
174 grade = "Industrial temperature grade ";
175 break;
176 case TEMP_EXTCOMMERCIAL:
177 grade = "Extended Consumer temperature grade ";
178 break;
179 default:
180 grade = "Consumer temperature grade ";
181 break;
182 }
183
184 buf = buf + ret;
185 size = size - ret;
186 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
187 }
188
Peng Fan32eaf672023-04-28 12:08:13 +0800189 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Licd8d1c52020-05-03 21:58:54 +0800190 temp = cpu_imx_get_temp(plat);
Peng Fan21981d22019-08-26 08:12:19 +0000191 buf = buf + ret;
192 size = size - ret;
Ye Licd8d1c52020-05-03 21:58:54 +0800193 if (temp != 0xdeadbeef)
194 ret = snprintf(buf, size, " at %dC", temp);
195 else
196 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan21981d22019-08-26 08:12:19 +0000197 }
198
Peng Fan21981d22019-08-26 08:12:19 +0000199 return 0;
200}
201
Simon Glass791fa452020-01-26 22:06:27 -0700202static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan21981d22019-08-26 08:12:19 +0000203{
Simon Glassb75b15b2020-12-03 16:55:23 -0700204 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000205
Hou Zhiqianga1124da2024-08-01 11:59:50 +0800206 info->cpu_freq = plat->freq_mhz * 1000000;
Peng Fan21981d22019-08-26 08:12:19 +0000207 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
208 return 0;
209}
210
Simon Glass791fa452020-01-26 22:06:27 -0700211static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000212{
Peng Fan8296b742020-05-03 21:58:51 +0800213 ofnode node;
214 int num = 0;
215
216 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
217 const char *device_type;
218
Simon Glass2e4938b2022-09-06 20:27:17 -0600219 if (!ofnode_is_enabled(node))
Peng Fan8296b742020-05-03 21:58:51 +0800220 continue;
221
222 device_type = ofnode_read_string(node, "device_type");
223 if (!device_type)
224 continue;
225
226 if (!strcmp(device_type, "cpu"))
227 num++;
228 }
229
230 return num;
Peng Fan21981d22019-08-26 08:12:19 +0000231}
232
Simon Glass791fa452020-01-26 22:06:27 -0700233static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000234{
235 snprintf(buf, size, "NXP");
236 return 0;
237}
238
Peng Fane2ded332020-05-03 21:58:52 +0800239static int cpu_imx_is_current(struct udevice *dev)
240{
Simon Glassb75b15b2020-12-03 16:55:23 -0700241 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fane2ded332020-05-03 21:58:52 +0800242
243 if (plat->mpidr == (read_mpidr() & 0xffff))
244 return 1;
245
246 return 0;
247}
248
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800249static int cpu_imx_release_core(const struct udevice *dev, phys_addr_t addr)
250{
251 struct cpu_imx_plat *plat = dev_get_plat(dev);
252 struct pt_regs regs;
253
254 regs.regs[0] = PSCI_0_2_FN64_CPU_ON;
255 regs.regs[1] = plat->mpidr;
256 regs.regs[2] = addr;
257 regs.regs[3] = 0;
258
259 smc_call(&regs);
260 if (regs.regs[0]) {
261 printf("Failed to release CPU core (mpidr: 0x%x)\n", plat->mpidr);
262 return -1;
263 }
264
265 printf("Released CPU core (mpidr: 0x%x) to address 0x%llx\n", plat->mpidr, addr);
266
267 return 0;
268}
269
Peng Fan146cce92023-04-28 12:08:12 +0800270static const struct cpu_ops cpu_imx_ops = {
Peng Fan21981d22019-08-26 08:12:19 +0000271 .get_desc = cpu_imx_get_desc,
272 .get_info = cpu_imx_get_info,
273 .get_count = cpu_imx_get_count,
274 .get_vendor = cpu_imx_get_vendor,
Peng Fane2ded332020-05-03 21:58:52 +0800275 .is_current = cpu_imx_is_current,
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800276 .release_core = cpu_imx_release_core,
Peng Fan21981d22019-08-26 08:12:19 +0000277};
278
Peng Fan146cce92023-04-28 12:08:12 +0800279static const struct udevice_id cpu_imx_ids[] = {
Peng Fan21981d22019-08-26 08:12:19 +0000280 { .compatible = "arm,cortex-a35" },
281 { .compatible = "arm,cortex-a53" },
Peng Fan146cce92023-04-28 12:08:12 +0800282 { .compatible = "arm,cortex-a55" },
Peng Fane2ded332020-05-03 21:58:52 +0800283 { .compatible = "arm,cortex-a72" },
Peng Fan21981d22019-08-26 08:12:19 +0000284 { }
285};
286
Peng Fan146cce92023-04-28 12:08:12 +0800287static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000288{
Simon Glassb75b15b2020-12-03 16:55:23 -0700289 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan146cce92023-04-28 12:08:12 +0800290 struct clk clk;
Peng Fan21981d22019-08-26 08:12:19 +0000291 ulong rate;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200292 int ret;
Peng Fan4b1fbb72020-05-03 21:58:53 +0800293
Peng Fan146cce92023-04-28 12:08:12 +0800294 if (IS_ENABLED(CONFIG_IMX8)) {
295 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
296 (sc_pm_clock_rate_t *)&rate);
297 } else {
298 ret = clk_get_by_index(dev, 0, &clk);
299 if (!ret) {
300 rate = clk_get_rate(&clk);
301 if (!rate)
302 ret = -EOPNOTSUPP;
303 }
304 }
Peng Fan21981d22019-08-26 08:12:19 +0000305 if (ret) {
306 printf("Could not read CPU frequency: %d\n", ret);
307 return 0;
308 }
309
310 return rate;
311}
312
Peng Fan146cce92023-04-28 12:08:12 +0800313static int imx_cpu_probe(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000314{
Simon Glassb75b15b2020-12-03 16:55:23 -0700315 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000316 u32 cpurev;
317
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200318 set_core_data(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000319 cpurev = get_cpu_rev();
320 plat->cpurev = cpurev;
Peng Fan146cce92023-04-28 12:08:12 +0800321 plat->rev = get_imx_rev_str(cpurev & 0xFFF);
Hou Zhiqiangc5e1a112024-08-01 11:59:51 +0800322 plat->type = get_imx_type_str((cpurev & 0x1FF000) >> 12);
Peng Fan146cce92023-04-28 12:08:12 +0800323 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fane2ded332020-05-03 21:58:52 +0800324 plat->mpidr = dev_read_addr(dev);
325 if (plat->mpidr == FDT_ADDR_T_NONE) {
326 printf("%s: Failed to get CPU reg property\n", __func__);
327 return -EINVAL;
328 }
329
Peng Fan21981d22019-08-26 08:12:19 +0000330 return 0;
331}
332
Peng Fan146cce92023-04-28 12:08:12 +0800333U_BOOT_DRIVER(cpu_imx_drv) = {
334 .name = "imx_cpu",
Peng Fan21981d22019-08-26 08:12:19 +0000335 .id = UCLASS_CPU,
Peng Fan146cce92023-04-28 12:08:12 +0800336 .of_match = cpu_imx_ids,
337 .ops = &cpu_imx_ops,
338 .probe = imx_cpu_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700339 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan21981d22019-08-26 08:12:19 +0000340 .flags = DM_FLAG_PRE_RELOC,
341};