blob: 51262befaff7683d5831be683f53b7776e23ff02 [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:
38 return "8MM";
39 case MXC_CPU_IMX8MN:
40 return "8MN";
41 case MXC_CPU_IMX8MP:
42 return "8MP";
Peng Fan21981d22019-08-26 08:12:19 +000043 case MXC_CPU_IMX8QXP:
44 case MXC_CPU_IMX8QXP_A0:
Peng Fan146cce92023-04-28 12:08:12 +080045 return "8QXP";
Peng Fan21981d22019-08-26 08:12:19 +000046 case MXC_CPU_IMX8QM:
Peng Fan146cce92023-04-28 12:08:12 +080047 return "8QM";
48 case MXC_CPU_IMX93:
49 return "93(52)";/* iMX93 Dual core with NPU */
Peng Fanc3db3ad2023-04-28 12:08:32 +080050 case MXC_CPU_IMX9351:
51 return "93(51)";/* iMX93 Single core with NPU */
52 case MXC_CPU_IMX9332:
53 return "93(32)";/* iMX93 Dual core without NPU */
54 case MXC_CPU_IMX9331:
55 return "93(31)";/* iMX93 Single core without NPU */
56 case MXC_CPU_IMX9322:
57 return "93(22)";/* iMX93 9x9 Dual core */
58 case MXC_CPU_IMX9321:
59 return "93(21)";/* iMX93 9x9 Single core */
60 case MXC_CPU_IMX9312:
61 return "93(12)";/* iMX93 9x9 Dual core without NPU */
62 case MXC_CPU_IMX9311:
63 return "93(11)";/* iMX93 9x9 Single core without NPU */
Ye Li57b2ac42024-09-19 12:01:33 +080064 case MXC_CPU_IMX9302:
65 return "93(02)";/* iMX93 900Mhz Low performance Dual core without NPU */
66 case MXC_CPU_IMX9301:
67 return "93(01)";/* iMX93 900Mhz Low performance Single core without NPU */
Peng Fan21981d22019-08-26 08:12:19 +000068 default:
69 return "??";
70 }
71}
72
Peng Fan7d5e7aa2024-10-18 15:34:32 +080073static void get_imx_rev_str(struct cpu_imx_plat *plat, u32 rev)
Peng Fan21981d22019-08-26 08:12:19 +000074{
Peng Fan146cce92023-04-28 12:08:12 +080075 if (IS_ENABLED(CONFIG_IMX8)) {
76 switch (rev) {
77 case CHIP_REV_A:
Peng Fan7d5e7aa2024-10-18 15:34:32 +080078 plat->rev[0] = 'A';
79 break;
Peng Fan146cce92023-04-28 12:08:12 +080080 case CHIP_REV_B:
Peng Fan7d5e7aa2024-10-18 15:34:32 +080081 plat->rev[0] = 'B';
82 break;
Peng Fan146cce92023-04-28 12:08:12 +080083 case CHIP_REV_C:
Peng Fan7d5e7aa2024-10-18 15:34:32 +080084 plat->rev[0] = 'C';
85 break;
Peng Fan146cce92023-04-28 12:08:12 +080086 default:
Peng Fan7d5e7aa2024-10-18 15:34:32 +080087 plat->rev[0] = '?';
88 break;
Peng Fan146cce92023-04-28 12:08:12 +080089 }
Peng Fan7d5e7aa2024-10-18 15:34:32 +080090 plat->rev[1] = '\0';
Peng Fan146cce92023-04-28 12:08:12 +080091 } else {
Peng Fan7d5e7aa2024-10-18 15:34:32 +080092 plat->rev[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
93 plat->rev[1] = '.';
94 plat->rev[2] = '0' + (rev & 0xf);
95 plat->rev[3] = '\0';
Peng Fan21981d22019-08-26 08:12:19 +000096 }
97}
98
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020099static void set_core_data(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000100{
Simon Glassb75b15b2020-12-03 16:55:23 -0700101 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200102
103 if (device_is_compatible(dev, "arm,cortex-a35")) {
104 plat->cpu_rsrc = SC_R_A35;
105 plat->name = "A35";
106 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
107 plat->cpu_rsrc = SC_R_A53;
108 plat->name = "A53";
109 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
110 plat->cpu_rsrc = SC_R_A72;
111 plat->name = "A72";
Peng Fan146cce92023-04-28 12:08:12 +0800112 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
113 plat->name = "A55";
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200114 } else {
115 plat->cpu_rsrc = SC_R_A53;
116 plat->name = "?";
117 }
Peng Fan21981d22019-08-26 08:12:19 +0000118}
119
Peng Fan32eaf672023-04-28 12:08:13 +0800120#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700121static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000122{
123 struct udevice *thermal_dev;
124 int cpu_tmp, ret;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200125 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan21981d22019-08-26 08:12:19 +0000126
Peng Fan32eaf672023-04-28 12:08:13 +0800127 if (IS_ENABLED(CONFIG_IMX8)) {
128 if (plat->cpu_rsrc == SC_R_A72)
129 idx = 2; /* use "cpu-thermal1" device */
130 } else {
131 idx = 1;
132 }
Peng Fan21981d22019-08-26 08:12:19 +0000133
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200134 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan21981d22019-08-26 08:12:19 +0000135 if (!ret) {
136 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
137 if (ret)
138 return 0xdeadbeef;
139 } else {
140 return 0xdeadbeef;
141 }
142
143 return cpu_tmp;
144}
145#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700146static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000147{
148 return 0;
149}
150#endif
151
Peng Fan81c694a2023-04-28 12:08:14 +0800152__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
153{
154 return 0;
155}
156
Peng Fand3ee4de2023-04-28 12:08:11 +0800157static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000158{
Simon Glassb75b15b2020-12-03 16:55:23 -0700159 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan81c694a2023-04-28 12:08:14 +0800160 const char *grade;
Ye Licd8d1c52020-05-03 21:58:54 +0800161 int ret, temp;
Peng Fan81c694a2023-04-28 12:08:14 +0800162 int minc, maxc;
Peng Fan21981d22019-08-26 08:12:19 +0000163
164 if (size < 100)
165 return -ENOSPC;
166
Peng Fan146cce92023-04-28 12:08:12 +0800167 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan21981d22019-08-26 08:12:19 +0000168 plat->type, plat->rev, plat->name, plat->freq_mhz);
169
Peng Fan81c694a2023-04-28 12:08:14 +0800170 if (IS_ENABLED(CONFIG_IMX9)) {
171 switch (get_cpu_temp_grade(&minc, &maxc)) {
172 case TEMP_AUTOMOTIVE:
173 grade = "Automotive temperature grade ";
174 break;
175 case TEMP_INDUSTRIAL:
176 grade = "Industrial temperature grade ";
177 break;
178 case TEMP_EXTCOMMERCIAL:
179 grade = "Extended Consumer temperature grade ";
180 break;
181 default:
182 grade = "Consumer temperature grade ";
183 break;
184 }
185
186 buf = buf + ret;
187 size = size - ret;
188 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
189 }
190
Peng Fan32eaf672023-04-28 12:08:13 +0800191 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Licd8d1c52020-05-03 21:58:54 +0800192 temp = cpu_imx_get_temp(plat);
Peng Fan21981d22019-08-26 08:12:19 +0000193 buf = buf + ret;
194 size = size - ret;
Ye Licd8d1c52020-05-03 21:58:54 +0800195 if (temp != 0xdeadbeef)
196 ret = snprintf(buf, size, " at %dC", temp);
197 else
198 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan21981d22019-08-26 08:12:19 +0000199 }
200
Peng Fan21981d22019-08-26 08:12:19 +0000201 return 0;
202}
203
Simon Glass791fa452020-01-26 22:06:27 -0700204static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan21981d22019-08-26 08:12:19 +0000205{
Simon Glassb75b15b2020-12-03 16:55:23 -0700206 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000207
Hou Zhiqianga1124da2024-08-01 11:59:50 +0800208 info->cpu_freq = plat->freq_mhz * 1000000;
Peng Fan21981d22019-08-26 08:12:19 +0000209 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
210 return 0;
211}
212
Simon Glass791fa452020-01-26 22:06:27 -0700213static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000214{
Peng Fan8296b742020-05-03 21:58:51 +0800215 ofnode node;
216 int num = 0;
217
218 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
219 const char *device_type;
220
Simon Glass2e4938b2022-09-06 20:27:17 -0600221 if (!ofnode_is_enabled(node))
Peng Fan8296b742020-05-03 21:58:51 +0800222 continue;
223
224 device_type = ofnode_read_string(node, "device_type");
225 if (!device_type)
226 continue;
227
228 if (!strcmp(device_type, "cpu"))
229 num++;
230 }
231
232 return num;
Peng Fan21981d22019-08-26 08:12:19 +0000233}
234
Simon Glass791fa452020-01-26 22:06:27 -0700235static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000236{
237 snprintf(buf, size, "NXP");
238 return 0;
239}
240
Peng Fane2ded332020-05-03 21:58:52 +0800241static int cpu_imx_is_current(struct udevice *dev)
242{
Simon Glassb75b15b2020-12-03 16:55:23 -0700243 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fane2ded332020-05-03 21:58:52 +0800244
245 if (plat->mpidr == (read_mpidr() & 0xffff))
246 return 1;
247
248 return 0;
249}
250
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800251static int cpu_imx_release_core(const struct udevice *dev, phys_addr_t addr)
252{
253 struct cpu_imx_plat *plat = dev_get_plat(dev);
254 struct pt_regs regs;
255
256 regs.regs[0] = PSCI_0_2_FN64_CPU_ON;
257 regs.regs[1] = plat->mpidr;
258 regs.regs[2] = addr;
259 regs.regs[3] = 0;
260
261 smc_call(&regs);
262 if (regs.regs[0]) {
263 printf("Failed to release CPU core (mpidr: 0x%x)\n", plat->mpidr);
264 return -1;
265 }
266
267 printf("Released CPU core (mpidr: 0x%x) to address 0x%llx\n", plat->mpidr, addr);
268
269 return 0;
270}
271
Peng Fan146cce92023-04-28 12:08:12 +0800272static const struct cpu_ops cpu_imx_ops = {
Peng Fan21981d22019-08-26 08:12:19 +0000273 .get_desc = cpu_imx_get_desc,
274 .get_info = cpu_imx_get_info,
275 .get_count = cpu_imx_get_count,
276 .get_vendor = cpu_imx_get_vendor,
Peng Fane2ded332020-05-03 21:58:52 +0800277 .is_current = cpu_imx_is_current,
Hou Zhiqiang863a1542024-08-01 11:59:54 +0800278 .release_core = cpu_imx_release_core,
Peng Fan21981d22019-08-26 08:12:19 +0000279};
280
Peng Fan146cce92023-04-28 12:08:12 +0800281static const struct udevice_id cpu_imx_ids[] = {
Peng Fan21981d22019-08-26 08:12:19 +0000282 { .compatible = "arm,cortex-a35" },
283 { .compatible = "arm,cortex-a53" },
Peng Fan146cce92023-04-28 12:08:12 +0800284 { .compatible = "arm,cortex-a55" },
Peng Fane2ded332020-05-03 21:58:52 +0800285 { .compatible = "arm,cortex-a72" },
Peng Fan21981d22019-08-26 08:12:19 +0000286 { }
287};
288
Peng Fan146cce92023-04-28 12:08:12 +0800289static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000290{
Simon Glassb75b15b2020-12-03 16:55:23 -0700291 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan146cce92023-04-28 12:08:12 +0800292 struct clk clk;
Peng Fan21981d22019-08-26 08:12:19 +0000293 ulong rate;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200294 int ret;
Peng Fan4b1fbb72020-05-03 21:58:53 +0800295
Peng Fan146cce92023-04-28 12:08:12 +0800296 if (IS_ENABLED(CONFIG_IMX8)) {
297 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
298 (sc_pm_clock_rate_t *)&rate);
299 } else {
300 ret = clk_get_by_index(dev, 0, &clk);
301 if (!ret) {
302 rate = clk_get_rate(&clk);
303 if (!rate)
304 ret = -EOPNOTSUPP;
305 }
306 }
Peng Fan21981d22019-08-26 08:12:19 +0000307 if (ret) {
308 printf("Could not read CPU frequency: %d\n", ret);
309 return 0;
310 }
311
312 return rate;
313}
314
Peng Fan146cce92023-04-28 12:08:12 +0800315static int imx_cpu_probe(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000316{
Simon Glassb75b15b2020-12-03 16:55:23 -0700317 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000318 u32 cpurev;
319
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200320 set_core_data(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000321 cpurev = get_cpu_rev();
322 plat->cpurev = cpurev;
Peng Fan7d5e7aa2024-10-18 15:34:32 +0800323 get_imx_rev_str(plat, cpurev & 0xFFF);
Hou Zhiqiangc5e1a112024-08-01 11:59:51 +0800324 plat->type = get_imx_type_str((cpurev & 0x1FF000) >> 12);
Peng Fan146cce92023-04-28 12:08:12 +0800325 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fane2ded332020-05-03 21:58:52 +0800326 plat->mpidr = dev_read_addr(dev);
327 if (plat->mpidr == FDT_ADDR_T_NONE) {
328 printf("%s: Failed to get CPU reg property\n", __func__);
329 return -EINVAL;
330 }
331
Peng Fan21981d22019-08-26 08:12:19 +0000332 return 0;
333}
334
Peng Fan146cce92023-04-28 12:08:12 +0800335U_BOOT_DRIVER(cpu_imx_drv) = {
336 .name = "imx_cpu",
Peng Fan21981d22019-08-26 08:12:19 +0000337 .id = UCLASS_CPU,
Peng Fan146cce92023-04-28 12:08:12 +0800338 .of_match = cpu_imx_ids,
339 .ops = &cpu_imx_ops,
340 .probe = imx_cpu_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700341 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan21981d22019-08-26 08:12:19 +0000342 .flags = DM_FLAG_PRE_RELOC,
343};