blob: 98ff95f5ff5c84f2fe82fa7411a8bd6be124bd3e [file] [log] [blame]
Peng Fan43c50872019-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 Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Simon Glass90526e92020-05-10 11:39:56 -060011#include <asm/system.h>
Peng Fan99ac6c72023-04-28 12:08:09 +080012#include <firmware/imx/sci/sci.h>
Peng Fan43c50872019-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 Fanf3a07712023-04-28 12:08:14 +080016#include <imx_thermal.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Peng Fan38e31972023-04-28 12:08:12 +080018#include <linux/clk-provider.h>
Peng Fan43c50872019-08-26 08:12:19 +000019
20DECLARE_GLOBAL_DATA_PTR;
21
Simon Glass8a8d24b2020-12-03 16:55:23 -070022struct cpu_imx_plat {
Peng Fan43c50872019-08-26 08:12:19 +000023 const char *name;
24 const char *rev;
25 const char *type;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020026 u32 cpu_rsrc;
Peng Fan43c50872019-08-26 08:12:19 +000027 u32 cpurev;
28 u32 freq_mhz;
Peng Fan177f9992020-05-03 21:58:52 +080029 u32 mpidr;
Peng Fan43c50872019-08-26 08:12:19 +000030};
31
Peng Fan38e31972023-04-28 12:08:12 +080032static const char *get_imx_type_str(u32 imxtype)
Peng Fan43c50872019-08-26 08:12:19 +000033{
34 switch (imxtype) {
35 case MXC_CPU_IMX8QXP:
36 case MXC_CPU_IMX8QXP_A0:
Peng Fan38e31972023-04-28 12:08:12 +080037 return "8QXP";
Peng Fan43c50872019-08-26 08:12:19 +000038 case MXC_CPU_IMX8QM:
Peng Fan38e31972023-04-28 12:08:12 +080039 return "8QM";
40 case MXC_CPU_IMX93:
41 return "93(52)";/* iMX93 Dual core with NPU */
Peng Fan58da8652023-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 Fan43c50872019-08-26 08:12:19 +000056 default:
57 return "??";
58 }
59}
60
Peng Fan38e31972023-04-28 12:08:12 +080061static const char *get_imx_rev_str(u32 rev)
Peng Fan43c50872019-08-26 08:12:19 +000062{
Peng Fan38e31972023-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 Fan43c50872019-08-26 08:12:19 +000083 }
84}
85
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020086static void set_core_data(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +000087{
Simon Glass8a8d24b2020-12-03 16:55:23 -070088 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin74e8fb02020-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 Fan38e31972023-04-28 12:08:12 +080099 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
100 plat->name = "A55";
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200101 } else {
102 plat->cpu_rsrc = SC_R_A53;
103 plat->name = "?";
104 }
Peng Fan43c50872019-08-26 08:12:19 +0000105}
106
Peng Fan7c5256e2023-04-28 12:08:13 +0800107#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glass8a8d24b2020-12-03 16:55:23 -0700108static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +0000109{
110 struct udevice *thermal_dev;
111 int cpu_tmp, ret;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200112 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan43c50872019-08-26 08:12:19 +0000113
Peng Fan7c5256e2023-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 Fan43c50872019-08-26 08:12:19 +0000120
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200121 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan43c50872019-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 Glass8a8d24b2020-12-03 16:55:23 -0700133static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +0000134{
135 return 0;
136}
137#endif
138
Peng Fanf3a07712023-04-28 12:08:14 +0800139__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
140{
141 return 0;
142}
143
Peng Fan3621efa2023-04-28 12:08:11 +0800144static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000145{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700146 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fanf3a07712023-04-28 12:08:14 +0800147 const char *grade;
Ye Li3ee6ea42020-05-03 21:58:54 +0800148 int ret, temp;
Peng Fanf3a07712023-04-28 12:08:14 +0800149 int minc, maxc;
Peng Fan43c50872019-08-26 08:12:19 +0000150
151 if (size < 100)
152 return -ENOSPC;
153
Peng Fan38e31972023-04-28 12:08:12 +0800154 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan43c50872019-08-26 08:12:19 +0000155 plat->type, plat->rev, plat->name, plat->freq_mhz);
156
Peng Fanf3a07712023-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 Fan7c5256e2023-04-28 12:08:13 +0800178 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Li3ee6ea42020-05-03 21:58:54 +0800179 temp = cpu_imx_get_temp(plat);
Peng Fan43c50872019-08-26 08:12:19 +0000180 buf = buf + ret;
181 size = size - ret;
Ye Li3ee6ea42020-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 Fan43c50872019-08-26 08:12:19 +0000186 }
187
188 snprintf(buf + ret, size - ret, "\n");
189
190 return 0;
191}
192
Simon Glass961420f2020-01-26 22:06:27 -0700193static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan43c50872019-08-26 08:12:19 +0000194{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700195 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-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 Glass961420f2020-01-26 22:06:27 -0700202static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000203{
Peng Fanadb3bd72020-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 Glass89090662022-09-06 20:27:17 -0600210 if (!ofnode_is_enabled(node))
Peng Fanadb3bd72020-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 Fan43c50872019-08-26 08:12:19 +0000222}
223
Simon Glass961420f2020-01-26 22:06:27 -0700224static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000225{
226 snprintf(buf, size, "NXP");
227 return 0;
228}
229
Peng Fan177f9992020-05-03 21:58:52 +0800230static int cpu_imx_is_current(struct udevice *dev)
231{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700232 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan177f9992020-05-03 21:58:52 +0800233
234 if (plat->mpidr == (read_mpidr() & 0xffff))
235 return 1;
236
237 return 0;
238}
239
Peng Fan38e31972023-04-28 12:08:12 +0800240static const struct cpu_ops cpu_imx_ops = {
Peng Fan43c50872019-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 Fan177f9992020-05-03 21:58:52 +0800245 .is_current = cpu_imx_is_current,
Peng Fan43c50872019-08-26 08:12:19 +0000246};
247
Peng Fan38e31972023-04-28 12:08:12 +0800248static const struct udevice_id cpu_imx_ids[] = {
Peng Fan43c50872019-08-26 08:12:19 +0000249 { .compatible = "arm,cortex-a35" },
250 { .compatible = "arm,cortex-a53" },
Peng Fan38e31972023-04-28 12:08:12 +0800251 { .compatible = "arm,cortex-a55" },
Peng Fan177f9992020-05-03 21:58:52 +0800252 { .compatible = "arm,cortex-a72" },
Peng Fan43c50872019-08-26 08:12:19 +0000253 { }
254};
255
Peng Fan38e31972023-04-28 12:08:12 +0800256static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000257{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700258 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan38e31972023-04-28 12:08:12 +0800259 struct clk clk;
Peng Fan43c50872019-08-26 08:12:19 +0000260 ulong rate;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200261 int ret;
Peng Fan55bc96f2020-05-03 21:58:53 +0800262
Peng Fan38e31972023-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 Fan43c50872019-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 Fan38e31972023-04-28 12:08:12 +0800282static int imx_cpu_probe(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000283{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700284 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000285 u32 cpurev;
286
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200287 set_core_data(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000288 cpurev = get_cpu_rev();
289 plat->cpurev = cpurev;
Peng Fan38e31972023-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 Fan177f9992020-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 Fan43c50872019-08-26 08:12:19 +0000299 return 0;
300}
301
Peng Fan38e31972023-04-28 12:08:12 +0800302U_BOOT_DRIVER(cpu_imx_drv) = {
303 .name = "imx_cpu",
Peng Fan43c50872019-08-26 08:12:19 +0000304 .id = UCLASS_CPU,
Peng Fan38e31972023-04-28 12:08:12 +0800305 .of_match = cpu_imx_ids,
306 .ops = &cpu_imx_ops,
307 .probe = imx_cpu_probe,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700308 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan43c50872019-08-26 08:12:19 +0000309 .flags = DM_FLAG_PRE_RELOC,
310};