blob: 304d5e59013ec00fca31cfe88327068330b60f37 [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 Fan43c50872019-08-26 08:12:19 +000042 default:
43 return "??";
44 }
45}
46
Peng Fan38e31972023-04-28 12:08:12 +080047static const char *get_imx_rev_str(u32 rev)
Peng Fan43c50872019-08-26 08:12:19 +000048{
Peng Fan38e31972023-04-28 12:08:12 +080049 static char revision[4];
50
51 if (IS_ENABLED(CONFIG_IMX8)) {
52 switch (rev) {
53 case CHIP_REV_A:
54 return "A";
55 case CHIP_REV_B:
56 return "B";
57 case CHIP_REV_C:
58 return "C";
59 default:
60 return "?";
61 }
62 } else {
63 revision[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
64 revision[1] = '.';
65 revision[2] = '0' + (rev & 0xf);
66 revision[3] = '\0';
67
68 return revision;
Peng Fan43c50872019-08-26 08:12:19 +000069 }
70}
71
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020072static void set_core_data(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +000073{
Simon Glass8a8d24b2020-12-03 16:55:23 -070074 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020075
76 if (device_is_compatible(dev, "arm,cortex-a35")) {
77 plat->cpu_rsrc = SC_R_A35;
78 plat->name = "A35";
79 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
80 plat->cpu_rsrc = SC_R_A53;
81 plat->name = "A53";
82 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
83 plat->cpu_rsrc = SC_R_A72;
84 plat->name = "A72";
Peng Fan38e31972023-04-28 12:08:12 +080085 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
86 plat->name = "A55";
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020087 } else {
88 plat->cpu_rsrc = SC_R_A53;
89 plat->name = "?";
90 }
Peng Fan43c50872019-08-26 08:12:19 +000091}
92
Peng Fan7c5256e2023-04-28 12:08:13 +080093#if IS_ENABLED(CONFIG_DM_THERMAL)
Simon Glass8a8d24b2020-12-03 16:55:23 -070094static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +000095{
96 struct udevice *thermal_dev;
97 int cpu_tmp, ret;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020098 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan43c50872019-08-26 08:12:19 +000099
Peng Fan7c5256e2023-04-28 12:08:13 +0800100 if (IS_ENABLED(CONFIG_IMX8)) {
101 if (plat->cpu_rsrc == SC_R_A72)
102 idx = 2; /* use "cpu-thermal1" device */
103 } else {
104 idx = 1;
105 }
Peng Fan43c50872019-08-26 08:12:19 +0000106
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200107 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan43c50872019-08-26 08:12:19 +0000108 if (!ret) {
109 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
110 if (ret)
111 return 0xdeadbeef;
112 } else {
113 return 0xdeadbeef;
114 }
115
116 return cpu_tmp;
117}
118#else
Simon Glass8a8d24b2020-12-03 16:55:23 -0700119static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +0000120{
121 return 0;
122}
123#endif
124
Peng Fanf3a07712023-04-28 12:08:14 +0800125__weak u32 get_cpu_temp_grade(int *minc, int *maxc)
126{
127 return 0;
128}
129
Peng Fan3621efa2023-04-28 12:08:11 +0800130static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000131{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700132 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fanf3a07712023-04-28 12:08:14 +0800133 const char *grade;
Ye Li3ee6ea42020-05-03 21:58:54 +0800134 int ret, temp;
Peng Fanf3a07712023-04-28 12:08:14 +0800135 int minc, maxc;
Peng Fan43c50872019-08-26 08:12:19 +0000136
137 if (size < 100)
138 return -ENOSPC;
139
Peng Fan38e31972023-04-28 12:08:12 +0800140 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan43c50872019-08-26 08:12:19 +0000141 plat->type, plat->rev, plat->name, plat->freq_mhz);
142
Peng Fanf3a07712023-04-28 12:08:14 +0800143 if (IS_ENABLED(CONFIG_IMX9)) {
144 switch (get_cpu_temp_grade(&minc, &maxc)) {
145 case TEMP_AUTOMOTIVE:
146 grade = "Automotive temperature grade ";
147 break;
148 case TEMP_INDUSTRIAL:
149 grade = "Industrial temperature grade ";
150 break;
151 case TEMP_EXTCOMMERCIAL:
152 grade = "Extended Consumer temperature grade ";
153 break;
154 default:
155 grade = "Consumer temperature grade ";
156 break;
157 }
158
159 buf = buf + ret;
160 size = size - ret;
161 ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc);
162 }
163
Peng Fan7c5256e2023-04-28 12:08:13 +0800164 if (IS_ENABLED(CONFIG_DM_THERMAL)) {
Ye Li3ee6ea42020-05-03 21:58:54 +0800165 temp = cpu_imx_get_temp(plat);
Peng Fan43c50872019-08-26 08:12:19 +0000166 buf = buf + ret;
167 size = size - ret;
Ye Li3ee6ea42020-05-03 21:58:54 +0800168 if (temp != 0xdeadbeef)
169 ret = snprintf(buf, size, " at %dC", temp);
170 else
171 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan43c50872019-08-26 08:12:19 +0000172 }
173
174 snprintf(buf + ret, size - ret, "\n");
175
176 return 0;
177}
178
Simon Glass961420f2020-01-26 22:06:27 -0700179static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan43c50872019-08-26 08:12:19 +0000180{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700181 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000182
183 info->cpu_freq = plat->freq_mhz * 1000;
184 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
185 return 0;
186}
187
Simon Glass961420f2020-01-26 22:06:27 -0700188static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000189{
Peng Fanadb3bd72020-05-03 21:58:51 +0800190 ofnode node;
191 int num = 0;
192
193 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
194 const char *device_type;
195
Simon Glass89090662022-09-06 20:27:17 -0600196 if (!ofnode_is_enabled(node))
Peng Fanadb3bd72020-05-03 21:58:51 +0800197 continue;
198
199 device_type = ofnode_read_string(node, "device_type");
200 if (!device_type)
201 continue;
202
203 if (!strcmp(device_type, "cpu"))
204 num++;
205 }
206
207 return num;
Peng Fan43c50872019-08-26 08:12:19 +0000208}
209
Simon Glass961420f2020-01-26 22:06:27 -0700210static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000211{
212 snprintf(buf, size, "NXP");
213 return 0;
214}
215
Peng Fan177f9992020-05-03 21:58:52 +0800216static int cpu_imx_is_current(struct udevice *dev)
217{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700218 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan177f9992020-05-03 21:58:52 +0800219
220 if (plat->mpidr == (read_mpidr() & 0xffff))
221 return 1;
222
223 return 0;
224}
225
Peng Fan38e31972023-04-28 12:08:12 +0800226static const struct cpu_ops cpu_imx_ops = {
Peng Fan43c50872019-08-26 08:12:19 +0000227 .get_desc = cpu_imx_get_desc,
228 .get_info = cpu_imx_get_info,
229 .get_count = cpu_imx_get_count,
230 .get_vendor = cpu_imx_get_vendor,
Peng Fan177f9992020-05-03 21:58:52 +0800231 .is_current = cpu_imx_is_current,
Peng Fan43c50872019-08-26 08:12:19 +0000232};
233
Peng Fan38e31972023-04-28 12:08:12 +0800234static const struct udevice_id cpu_imx_ids[] = {
Peng Fan43c50872019-08-26 08:12:19 +0000235 { .compatible = "arm,cortex-a35" },
236 { .compatible = "arm,cortex-a53" },
Peng Fan38e31972023-04-28 12:08:12 +0800237 { .compatible = "arm,cortex-a55" },
Peng Fan177f9992020-05-03 21:58:52 +0800238 { .compatible = "arm,cortex-a72" },
Peng Fan43c50872019-08-26 08:12:19 +0000239 { }
240};
241
Peng Fan38e31972023-04-28 12:08:12 +0800242static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000243{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700244 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan38e31972023-04-28 12:08:12 +0800245 struct clk clk;
Peng Fan43c50872019-08-26 08:12:19 +0000246 ulong rate;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200247 int ret;
Peng Fan55bc96f2020-05-03 21:58:53 +0800248
Peng Fan38e31972023-04-28 12:08:12 +0800249 if (IS_ENABLED(CONFIG_IMX8)) {
250 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
251 (sc_pm_clock_rate_t *)&rate);
252 } else {
253 ret = clk_get_by_index(dev, 0, &clk);
254 if (!ret) {
255 rate = clk_get_rate(&clk);
256 if (!rate)
257 ret = -EOPNOTSUPP;
258 }
259 }
Peng Fan43c50872019-08-26 08:12:19 +0000260 if (ret) {
261 printf("Could not read CPU frequency: %d\n", ret);
262 return 0;
263 }
264
265 return rate;
266}
267
Peng Fan38e31972023-04-28 12:08:12 +0800268static int imx_cpu_probe(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000269{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700270 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000271 u32 cpurev;
272
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200273 set_core_data(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000274 cpurev = get_cpu_rev();
275 plat->cpurev = cpurev;
Peng Fan38e31972023-04-28 12:08:12 +0800276 plat->rev = get_imx_rev_str(cpurev & 0xFFF);
277 plat->type = get_imx_type_str((cpurev & 0xFF000) >> 12);
278 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fan177f9992020-05-03 21:58:52 +0800279 plat->mpidr = dev_read_addr(dev);
280 if (plat->mpidr == FDT_ADDR_T_NONE) {
281 printf("%s: Failed to get CPU reg property\n", __func__);
282 return -EINVAL;
283 }
284
Peng Fan43c50872019-08-26 08:12:19 +0000285 return 0;
286}
287
Peng Fan38e31972023-04-28 12:08:12 +0800288U_BOOT_DRIVER(cpu_imx_drv) = {
289 .name = "imx_cpu",
Peng Fan43c50872019-08-26 08:12:19 +0000290 .id = UCLASS_CPU,
Peng Fan38e31972023-04-28 12:08:12 +0800291 .of_match = cpu_imx_ids,
292 .ops = &cpu_imx_ops,
293 .probe = imx_cpu_probe,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700294 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan43c50872019-08-26 08:12:19 +0000295 .flags = DM_FLAG_PRE_RELOC,
296};