blob: 98ff95f5ff5c84f2fe82fa7411a8bd6be124bd3e [file] [log] [blame]
Peng Fan21981d22019-08-26 08:12:19 +00001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 NXP
4 */
5
6#include <common.h>
7#include <cpu.h>
8#include <dm.h>
9#include <thermal.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.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>
Peng Fan21981d22019-08-26 08:12:19 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glassb75b15b2020-12-03 16:55:23 -070022struct cpu_imx_plat {
Peng Fan21981d22019-08-26 08:12:19 +000023 const char *name;
24 const char *rev;
25 const char *type;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020026 u32 cpu_rsrc;
Peng Fan21981d22019-08-26 08:12:19 +000027 u32 cpurev;
28 u32 freq_mhz;
Peng Fane2ded332020-05-03 21:58:52 +080029 u32 mpidr;
Peng Fan21981d22019-08-26 08:12:19 +000030};
31
Peng Fan146cce92023-04-28 12:08:12 +080032static const char *get_imx_type_str(u32 imxtype)
Peng Fan21981d22019-08-26 08:12:19 +000033{
34 switch (imxtype) {
35 case MXC_CPU_IMX8QXP:
36 case MXC_CPU_IMX8QXP_A0:
Peng Fan146cce92023-04-28 12:08:12 +080037 return "8QXP";
Peng Fan21981d22019-08-26 08:12:19 +000038 case MXC_CPU_IMX8QM:
Peng Fan146cce92023-04-28 12:08:12 +080039 return "8QM";
40 case MXC_CPU_IMX93:
41 return "93(52)";/* iMX93 Dual core with NPU */
Peng Fanc3db3ad2023-04-28 12:08:32 +080042 case MXC_CPU_IMX9351:
43 return "93(51)";/* iMX93 Single core with NPU */
44 case MXC_CPU_IMX9332:
45 return "93(32)";/* iMX93 Dual core without NPU */
46 case MXC_CPU_IMX9331:
47 return "93(31)";/* iMX93 Single core without NPU */
48 case MXC_CPU_IMX9322:
49 return "93(22)";/* iMX93 9x9 Dual core */
50 case MXC_CPU_IMX9321:
51 return "93(21)";/* iMX93 9x9 Single core */
52 case MXC_CPU_IMX9312:
53 return "93(12)";/* iMX93 9x9 Dual core without NPU */
54 case MXC_CPU_IMX9311:
55 return "93(11)";/* iMX93 9x9 Single core without NPU */
Peng Fan21981d22019-08-26 08:12:19 +000056 default:
57 return "??";
58 }
59}
60
Peng Fan146cce92023-04-28 12:08:12 +080061static const char *get_imx_rev_str(u32 rev)
Peng Fan21981d22019-08-26 08:12:19 +000062{
Peng Fan146cce92023-04-28 12:08:12 +080063 static char revision[4];
64
65 if (IS_ENABLED(CONFIG_IMX8)) {
66 switch (rev) {
67 case CHIP_REV_A:
68 return "A";
69 case CHIP_REV_B:
70 return "B";
71 case CHIP_REV_C:
72 return "C";
73 default:
74 return "?";
75 }
76 } else {
77 revision[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
78 revision[1] = '.';
79 revision[2] = '0' + (rev & 0xf);
80 revision[3] = '\0';
81
82 return revision;
Peng Fan21981d22019-08-26 08:12:19 +000083 }
84}
85
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020086static void set_core_data(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +000087{
Simon Glassb75b15b2020-12-03 16:55:23 -070088 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +020089
90 if (device_is_compatible(dev, "arm,cortex-a35")) {
91 plat->cpu_rsrc = SC_R_A35;
92 plat->name = "A35";
93 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
94 plat->cpu_rsrc = SC_R_A53;
95 plat->name = "A53";
96 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
97 plat->cpu_rsrc = SC_R_A72;
98 plat->name = "A72";
Peng Fan146cce92023-04-28 12:08:12 +080099 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
100 plat->name = "A55";
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200101 } else {
102 plat->cpu_rsrc = SC_R_A53;
103 plat->name = "?";
104 }
Peng Fan21981d22019-08-26 08:12:19 +0000105}
106
Peng Fan32eaf672023-04-28 12:08:13 +0800107#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glassb75b15b2020-12-03 16:55:23 -0700108static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000109{
110 struct udevice *thermal_dev;
111 int cpu_tmp, ret;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200112 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan21981d22019-08-26 08:12:19 +0000113
Peng Fan32eaf672023-04-28 12:08:13 +0800114 if (IS_ENABLED(CONFIG_IMX8)) {
115 if (plat->cpu_rsrc == SC_R_A72)
116 idx = 2; /* use "cpu-thermal1" device */
117 } else {
118 idx = 1;
119 }
Peng Fan21981d22019-08-26 08:12:19 +0000120
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200121 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan21981d22019-08-26 08:12:19 +0000122 if (!ret) {
123 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
124 if (ret)
125 return 0xdeadbeef;
126 } else {
127 return 0xdeadbeef;
128 }
129
130 return cpu_tmp;
131}
132#else
Simon Glassb75b15b2020-12-03 16:55:23 -0700133static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan21981d22019-08-26 08:12:19 +0000134{
135 return 0;
136}
137#endif
138
Peng Fan81c694a2023-04-28 12:08:14 +0800139__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
140{
141 return 0;
142}
143
Peng Fand3ee4de2023-04-28 12:08:11 +0800144static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000145{
Simon Glassb75b15b2020-12-03 16:55:23 -0700146 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan81c694a2023-04-28 12:08:14 +0800147 const char *grade;
Ye Licd8d1c52020-05-03 21:58:54 +0800148 int ret, temp;
Peng Fan81c694a2023-04-28 12:08:14 +0800149 int minc, maxc;
Peng Fan21981d22019-08-26 08:12:19 +0000150
151 if (size < 100)
152 return -ENOSPC;
153
Peng Fan146cce92023-04-28 12:08:12 +0800154 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan21981d22019-08-26 08:12:19 +0000155 plat->type, plat->rev, plat->name, plat->freq_mhz);
156
Peng Fan81c694a2023-04-28 12:08:14 +0800157 if (IS_ENABLED(CONFIG_IMX9)) {
158 switch (get_cpu_temp_grade(&minc, &maxc)) {
159 case TEMP_AUTOMOTIVE:
160 grade = "Automotive temperature grade ";
161 break;
162 case TEMP_INDUSTRIAL:
163 grade = "Industrial temperature grade ";
164 break;
165 case TEMP_EXTCOMMERCIAL:
166 grade = "Extended Consumer temperature grade ";
167 break;
168 default:
169 grade = "Consumer temperature grade ";
170 break;
171 }
172
173 buf = buf + ret;
174 size = size - ret;
175 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
176 }
177
Peng Fan32eaf672023-04-28 12:08:13 +0800178 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Licd8d1c52020-05-03 21:58:54 +0800179 temp = cpu_imx_get_temp(plat);
Peng Fan21981d22019-08-26 08:12:19 +0000180 buf = buf + ret;
181 size = size - ret;
Ye Licd8d1c52020-05-03 21:58:54 +0800182 if (temp != 0xdeadbeef)
183 ret = snprintf(buf, size, " at %dC", temp);
184 else
185 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan21981d22019-08-26 08:12:19 +0000186 }
187
188 snprintf(buf + ret, size - ret, "\n");
189
190 return 0;
191}
192
Simon Glass791fa452020-01-26 22:06:27 -0700193static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan21981d22019-08-26 08:12:19 +0000194{
Simon Glassb75b15b2020-12-03 16:55:23 -0700195 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000196
197 info->cpu_freq = plat->freq_mhz * 1000;
198 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
199 return 0;
200}
201
Simon Glass791fa452020-01-26 22:06:27 -0700202static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000203{
Peng Fan8296b742020-05-03 21:58:51 +0800204 ofnode node;
205 int num = 0;
206
207 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
208 const char *device_type;
209
Simon Glass2e4938b2022-09-06 20:27:17 -0600210 if (!ofnode_is_enabled(node))
Peng Fan8296b742020-05-03 21:58:51 +0800211 continue;
212
213 device_type = ofnode_read_string(node, "device_type");
214 if (!device_type)
215 continue;
216
217 if (!strcmp(device_type, "cpu"))
218 num++;
219 }
220
221 return num;
Peng Fan21981d22019-08-26 08:12:19 +0000222}
223
Simon Glass791fa452020-01-26 22:06:27 -0700224static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan21981d22019-08-26 08:12:19 +0000225{
226 snprintf(buf, size, "NXP");
227 return 0;
228}
229
Peng Fane2ded332020-05-03 21:58:52 +0800230static int cpu_imx_is_current(struct udevice *dev)
231{
Simon Glassb75b15b2020-12-03 16:55:23 -0700232 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fane2ded332020-05-03 21:58:52 +0800233
234 if (plat->mpidr == (read_mpidr() & 0xffff))
235 return 1;
236
237 return 0;
238}
239
Peng Fan146cce92023-04-28 12:08:12 +0800240static const struct cpu_ops cpu_imx_ops = {
Peng Fan21981d22019-08-26 08:12:19 +0000241 .get_desc = cpu_imx_get_desc,
242 .get_info = cpu_imx_get_info,
243 .get_count = cpu_imx_get_count,
244 .get_vendor = cpu_imx_get_vendor,
Peng Fane2ded332020-05-03 21:58:52 +0800245 .is_current = cpu_imx_is_current,
Peng Fan21981d22019-08-26 08:12:19 +0000246};
247
Peng Fan146cce92023-04-28 12:08:12 +0800248static const struct udevice_id cpu_imx_ids[] = {
Peng Fan21981d22019-08-26 08:12:19 +0000249 { .compatible = "arm,cortex-a35" },
250 { .compatible = "arm,cortex-a53" },
Peng Fan146cce92023-04-28 12:08:12 +0800251 { .compatible = "arm,cortex-a55" },
Peng Fane2ded332020-05-03 21:58:52 +0800252 { .compatible = "arm,cortex-a72" },
Peng Fan21981d22019-08-26 08:12:19 +0000253 { }
254};
255
Peng Fan146cce92023-04-28 12:08:12 +0800256static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000257{
Simon Glassb75b15b2020-12-03 16:55:23 -0700258 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan146cce92023-04-28 12:08:12 +0800259 struct clk clk;
Peng Fan21981d22019-08-26 08:12:19 +0000260 ulong rate;
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200261 int ret;
Peng Fan4b1fbb72020-05-03 21:58:53 +0800262
Peng Fan146cce92023-04-28 12:08:12 +0800263 if (IS_ENABLED(CONFIG_IMX8)) {
264 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
265 (sc_pm_clock_rate_t *)&rate);
266 } else {
267 ret = clk_get_by_index(dev, 0, &clk);
268 if (!ret) {
269 rate = clk_get_rate(&clk);
270 if (!rate)
271 ret = -EOPNOTSUPP;
272 }
273 }
Peng Fan21981d22019-08-26 08:12:19 +0000274 if (ret) {
275 printf("Could not read CPU frequency: %d\n", ret);
276 return 0;
277 }
278
279 return rate;
280}
281
Peng Fan146cce92023-04-28 12:08:12 +0800282static int imx_cpu_probe(struct udevice *dev)
Peng Fan21981d22019-08-26 08:12:19 +0000283{
Simon Glassb75b15b2020-12-03 16:55:23 -0700284 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000285 u32 cpurev;
286
Anatolij Gustschin50ca4a02020-05-20 01:31:44 +0200287 set_core_data(dev);
Peng Fan21981d22019-08-26 08:12:19 +0000288 cpurev = get_cpu_rev();
289 plat->cpurev = cpurev;
Peng Fan146cce92023-04-28 12:08:12 +0800290 plat->rev = get_imx_rev_str(cpurev & 0xFFF);
291 plat->type = get_imx_type_str((cpurev & 0xFF000) >> 12);
292 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fane2ded332020-05-03 21:58:52 +0800293 plat->mpidr = dev_read_addr(dev);
294 if (plat->mpidr == FDT_ADDR_T_NONE) {
295 printf("%s: Failed to get CPU reg property\n", __func__);
296 return -EINVAL;
297 }
298
Peng Fan21981d22019-08-26 08:12:19 +0000299 return 0;
300}
301
Peng Fan146cce92023-04-28 12:08:12 +0800302U_BOOT_DRIVER(cpu_imx_drv) = {
303 .name = "imx_cpu",
Peng Fan21981d22019-08-26 08:12:19 +0000304 .id = UCLASS_CPU,
Peng Fan146cce92023-04-28 12:08:12 +0800305 .of_match = cpu_imx_ids,
306 .ops = &cpu_imx_ops,
307 .probe = imx_cpu_probe,
Simon Glassb75b15b2020-12-03 16:55:23 -0700308 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan21981d22019-08-26 08:12:19 +0000309 .flags = DM_FLAG_PRE_RELOC,
310};