blob: 60deca963a6733f4e9ad4404cc891619cab601dd [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 */
Peng Fan21981d22019-08-26 08:12:19 +000063 default:
64 return "??";
65 }
66}
67
Peng Fan146cce92023-04-28 12:08:12 +080068static const char *get_imx_rev_str(u32 rev)
Peng Fan21981d22019-08-26 08:12:19 +000069{
Peng Fan146cce92023-04-28 12:08:12 +080070 static char revision[4];
71
72 if (IS_ENABLED(CONFIG_IMX8)) {
73 switch (rev) {
74 case CHIP_REV_A:
75 return "A";
76 case CHIP_REV_B:
77 return "B";
78 case CHIP_REV_C:
79 return "C";
80 default:
81 return "?";
82 }
83 } else {
84 revision[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
85 revision[1] = '.';
86 revision[2] = '0' + (rev & 0xf);
87 revision[3] = '\0';
88
89 return revision;
Peng Fan21981d22019-08-26 08:12:19 +000090 }
91}
92
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020093static void set_core_data(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +000094{
Simon Glassb75b15b2020-12-03 16:55:23 -070095 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020096
97 if (device_is_compatible(dev, "arm,cortex-a35")) {
98 plat->cpu_rsrc = SC_R_A35;
99 plat->name = "A35";
100 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
101 plat->cpu_rsrc = SC_R_A53;
102 plat->name = "A53";
103 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
104 plat->cpu_rsrc = SC_R_A72;
105 plat->name = "A72";
Peng Fan146cce92023-04-28 12:08:12 +0800106 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
107 plat->name = "A55";
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200108 } else {
109 plat->cpu_rsrc = SC_R_A53;
110 plat->name = "?";
111 }
Peng Fan21981d22019-08-26 08:12:19 +0000112}
113
Peng Fan32eaf672023-04-28 12:08:13 +0800114#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700115static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000116{
117 struct udevice *thermal_dev;
118 int cpu_tmp, ret;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200119 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan21981d22019-08-26 08:12:19 +0000120
Peng Fan32eaf672023-04-28 12:08:13 +0800121 if (IS_ENABLED(CONFIG_IMX8)) {
122 if (plat->cpu_rsrc == SC_R_A72)
123 idx = 2; /* use "cpu-thermal1" device */
124 } else {
125 idx = 1;
126 }
Peng Fan21981d22019-08-26 08:12:19 +0000127
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200128 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan21981d22019-08-26 08:12:19 +0000129 if (!ret) {
130 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
131 if (ret)
132 return 0xdeadbeef;
133 } else {
134 return 0xdeadbeef;
135 }
136
137 return cpu_tmp;
138}
139#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700140static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000141{
142 return 0;
143}
144#endif
145
Peng Fan81c694a2023-04-28 12:08:14 +0800146__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
147{
148 return 0;
149}
150
Peng Fand3ee4de2023-04-28 12:08:11 +0800151static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000152{
Simon Glassb75b15b2020-12-03 16:55:23 -0700153 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan81c694a2023-04-28 12:08:14 +0800154 const char *grade;
Ye Licd8d1c52020-05-03 21:58:54 +0800155 int ret, temp;
Peng Fan81c694a2023-04-28 12:08:14 +0800156 int minc, maxc;
Peng Fan21981d22019-08-26 08:12:19 +0000157
158 if (size < 100)
159 return -ENOSPC;
160
Peng Fan146cce92023-04-28 12:08:12 +0800161 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan21981d22019-08-26 08:12:19 +0000162 plat->type, plat->rev, plat->name, plat->freq_mhz);
163
Peng Fan81c694a2023-04-28 12:08:14 +0800164 if (IS_ENABLED(CONFIG_IMX9)) {
165 switch (get_cpu_temp_grade(&minc, &maxc)) {
166 case TEMP_AUTOMOTIVE:
167 grade = "Automotive temperature grade ";
168 break;
169 case TEMP_INDUSTRIAL:
170 grade = "Industrial temperature grade ";
171 break;
172 case TEMP_EXTCOMMERCIAL:
173 grade = "Extended Consumer temperature grade ";
174 break;
175 default:
176 grade = "Consumer temperature grade ";
177 break;
178 }
179
180 buf = buf + ret;
181 size = size - ret;
182 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
183 }
184
Peng Fan32eaf672023-04-28 12:08:13 +0800185 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Licd8d1c52020-05-03 21:58:54 +0800186 temp = cpu_imx_get_temp(plat);
Peng Fan21981d22019-08-26 08:12:19 +0000187 buf = buf + ret;
188 size = size - ret;
Ye Licd8d1c52020-05-03 21:58:54 +0800189 if (temp != 0xdeadbeef)
190 ret = snprintf(buf, size, " at %dC", temp);
191 else
192 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan21981d22019-08-26 08:12:19 +0000193 }
194
Peng Fan21981d22019-08-26 08:12:19 +0000195 return 0;
196}
197
Simon Glass791fa452020-01-26 22:06:27 -0700198static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan21981d22019-08-26 08:12:19 +0000199{
Simon Glassb75b15b2020-12-03 16:55:23 -0700200 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000201
Hou Zhiqianga1124da2024-08-01 11:59:50 +0800202 info->cpu_freq = plat->freq_mhz * 1000000;
Peng Fan21981d22019-08-26 08:12:19 +0000203 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
204 return 0;
205}
206
Simon Glass791fa452020-01-26 22:06:27 -0700207static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000208{
Peng Fan8296b742020-05-03 21:58:51 +0800209 ofnode node;
210 int num = 0;
211
212 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
213 const char *device_type;
214
Simon Glass2e4938b2022-09-06 20:27:17 -0600215 if (!ofnode_is_enabled(node))
Peng Fan8296b742020-05-03 21:58:51 +0800216 continue;
217
218 device_type = ofnode_read_string(node, "device_type");
219 if (!device_type)
220 continue;
221
222 if (!strcmp(device_type, "cpu"))
223 num++;
224 }
225
226 return num;
Peng Fan21981d22019-08-26 08:12:19 +0000227}
228
Simon Glass791fa452020-01-26 22:06:27 -0700229static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000230{
231 snprintf(buf, size, "NXP");
232 return 0;
233}
234
Peng Fane2ded332020-05-03 21:58:52 +0800235static int cpu_imx_is_current(struct udevice *dev)
236{
Simon Glassb75b15b2020-12-03 16:55:23 -0700237 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fane2ded332020-05-03 21:58:52 +0800238
239 if (plat->mpidr == (read_mpidr() & 0xffff))
240 return 1;
241
242 return 0;
243}
244
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800245static int cpu_imx_release_core(const struct udevice *dev, phys_addr_t addr)
246{
247 struct cpu_imx_plat *plat = dev_get_plat(dev);
248 struct pt_regs regs;
249
250 regs.regs[0] = PSCI_0_2_FN64_CPU_ON;
251 regs.regs[1] = plat->mpidr;
252 regs.regs[2] = addr;
253 regs.regs[3] = 0;
254
255 smc_call(&regs);
256 if (regs.regs[0]) {
257 printf("Failed to release CPU core (mpidr: 0x%x)\n", plat->mpidr);
258 return -1;
259 }
260
261 printf("Released CPU core (mpidr: 0x%x) to address 0x%llx\n", plat->mpidr, addr);
262
263 return 0;
264}
265
Peng Fan146cce92023-04-28 12:08:12 +0800266static const struct cpu_ops cpu_imx_ops = {
Peng Fan21981d22019-08-26 08:12:19 +0000267 .get_desc = cpu_imx_get_desc,
268 .get_info = cpu_imx_get_info,
269 .get_count = cpu_imx_get_count,
270 .get_vendor = cpu_imx_get_vendor,
Peng Fane2ded332020-05-03 21:58:52 +0800271 .is_current = cpu_imx_is_current,
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800272 .release_core = cpu_imx_release_core,
Peng Fan21981d22019-08-26 08:12:19 +0000273};
274
Peng Fan146cce92023-04-28 12:08:12 +0800275static const struct udevice_id cpu_imx_ids[] = {
Peng Fan21981d22019-08-26 08:12:19 +0000276 { .compatible = "arm,cortex-a35" },
277 { .compatible = "arm,cortex-a53" },
Peng Fan146cce92023-04-28 12:08:12 +0800278 { .compatible = "arm,cortex-a55" },
Peng Fane2ded332020-05-03 21:58:52 +0800279 { .compatible = "arm,cortex-a72" },
Peng Fan21981d22019-08-26 08:12:19 +0000280 { }
281};
282
Peng Fan146cce92023-04-28 12:08:12 +0800283static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000284{
Simon Glassb75b15b2020-12-03 16:55:23 -0700285 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan146cce92023-04-28 12:08:12 +0800286 struct clk clk;
Peng Fan21981d22019-08-26 08:12:19 +0000287 ulong rate;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200288 int ret;
Peng Fan4b1fbb72020-05-03 21:58:53 +0800289
Peng Fan146cce92023-04-28 12:08:12 +0800290 if (IS_ENABLED(CONFIG_IMX8)) {
291 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
292 (sc_pm_clock_rate_t *)&rate);
293 } else {
294 ret = clk_get_by_index(dev, 0, &clk);
295 if (!ret) {
296 rate = clk_get_rate(&clk);
297 if (!rate)
298 ret = -EOPNOTSUPP;
299 }
300 }
Peng Fan21981d22019-08-26 08:12:19 +0000301 if (ret) {
302 printf("Could not read CPU frequency: %d\n", ret);
303 return 0;
304 }
305
306 return rate;
307}
308
Peng Fan146cce92023-04-28 12:08:12 +0800309static int imx_cpu_probe(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000310{
Simon Glassb75b15b2020-12-03 16:55:23 -0700311 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000312 u32 cpurev;
313
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200314 set_core_data(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000315 cpurev = get_cpu_rev();
316 plat->cpurev = cpurev;
Peng Fan146cce92023-04-28 12:08:12 +0800317 plat->rev = get_imx_rev_str(cpurev & 0xFFF);
Hou Zhiqiangc5e1a112024-08-01 11:59:51 +0800318 plat->type = get_imx_type_str((cpurev & 0x1FF000) >> 12);
Peng Fan146cce92023-04-28 12:08:12 +0800319 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fane2ded332020-05-03 21:58:52 +0800320 plat->mpidr = dev_read_addr(dev);
321 if (plat->mpidr == FDT_ADDR_T_NONE) {
322 printf("%s: Failed to get CPU reg property\n", __func__);
323 return -EINVAL;
324 }
325
Peng Fan21981d22019-08-26 08:12:19 +0000326 return 0;
327}
328
Peng Fan146cce92023-04-28 12:08:12 +0800329U_BOOT_DRIVER(cpu_imx_drv) = {
330 .name = "imx_cpu",
Peng Fan21981d22019-08-26 08:12:19 +0000331 .id = UCLASS_CPU,
Peng Fan146cce92023-04-28 12:08:12 +0800332 .of_match = cpu_imx_ids,
333 .ops = &cpu_imx_ops,
334 .probe = imx_cpu_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700335 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan21981d22019-08-26 08:12:19 +0000336 .flags = DM_FLAG_PRE_RELOC,
337};