blob: 2d3df419f1ca582e24c8801114416c8aed807610 [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>
Simon Glasscd93d622020-05-10 11:40:13 -060016#include <linux/bitops.h>
Peng Fan38e31972023-04-28 12:08:12 +080017#include <linux/clk-provider.h>
Peng Fan43c50872019-08-26 08:12:19 +000018
19DECLARE_GLOBAL_DATA_PTR;
20
Simon Glass8a8d24b2020-12-03 16:55:23 -070021struct cpu_imx_plat {
Peng Fan43c50872019-08-26 08:12:19 +000022 const char *name;
23 const char *rev;
24 const char *type;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020025 u32 cpu_rsrc;
Peng Fan43c50872019-08-26 08:12:19 +000026 u32 cpurev;
27 u32 freq_mhz;
Peng Fan177f9992020-05-03 21:58:52 +080028 u32 mpidr;
Peng Fan43c50872019-08-26 08:12:19 +000029};
30
Peng Fan38e31972023-04-28 12:08:12 +080031static const char *get_imx_type_str(u32 imxtype)
Peng Fan43c50872019-08-26 08:12:19 +000032{
33 switch (imxtype) {
34 case MXC_CPU_IMX8QXP:
35 case MXC_CPU_IMX8QXP_A0:
Peng Fan38e31972023-04-28 12:08:12 +080036 return "8QXP";
Peng Fan43c50872019-08-26 08:12:19 +000037 case MXC_CPU_IMX8QM:
Peng Fan38e31972023-04-28 12:08:12 +080038 return "8QM";
39 case MXC_CPU_IMX93:
40 return "93(52)";/* iMX93 Dual core with NPU */
Peng Fan43c50872019-08-26 08:12:19 +000041 default:
42 return "??";
43 }
44}
45
Peng Fan38e31972023-04-28 12:08:12 +080046static const char *get_imx_rev_str(u32 rev)
Peng Fan43c50872019-08-26 08:12:19 +000047{
Peng Fan38e31972023-04-28 12:08:12 +080048 static char revision[4];
49
50 if (IS_ENABLED(CONFIG_IMX8)) {
51 switch (rev) {
52 case CHIP_REV_A:
53 return "A";
54 case CHIP_REV_B:
55 return "B";
56 case CHIP_REV_C:
57 return "C";
58 default:
59 return "?";
60 }
61 } else {
62 revision[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4);
63 revision[1] = '.';
64 revision[2] = '0' + (rev & 0xf);
65 revision[3] = '\0';
66
67 return revision;
Peng Fan43c50872019-08-26 08:12:19 +000068 }
69}
70
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020071static void set_core_data(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +000072{
Simon Glass8a8d24b2020-12-03 16:55:23 -070073 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020074
75 if (device_is_compatible(dev, "arm,cortex-a35")) {
76 plat->cpu_rsrc = SC_R_A35;
77 plat->name = "A35";
78 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
79 plat->cpu_rsrc = SC_R_A53;
80 plat->name = "A53";
81 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
82 plat->cpu_rsrc = SC_R_A72;
83 plat->name = "A72";
Peng Fan38e31972023-04-28 12:08:12 +080084 } else if (device_is_compatible(dev, "arm,cortex-a55")) {
85 plat->name = "A55";
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020086 } else {
87 plat->cpu_rsrc = SC_R_A53;
88 plat->name = "?";
89 }
Peng Fan43c50872019-08-26 08:12:19 +000090}
91
92#if IS_ENABLED(CONFIG_IMX_SCU_THERMAL)
Simon Glass8a8d24b2020-12-03 16:55:23 -070093static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +000094{
95 struct udevice *thermal_dev;
96 int cpu_tmp, ret;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020097 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan43c50872019-08-26 08:12:19 +000098
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020099 if (plat->cpu_rsrc == SC_R_A72)
100 idx = 2; /* use "cpu-thermal1" device */
Peng Fan43c50872019-08-26 08:12:19 +0000101
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200102 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan43c50872019-08-26 08:12:19 +0000103 if (!ret) {
104 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
105 if (ret)
106 return 0xdeadbeef;
107 } else {
108 return 0xdeadbeef;
109 }
110
111 return cpu_tmp;
112}
113#else
Simon Glass8a8d24b2020-12-03 16:55:23 -0700114static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +0000115{
116 return 0;
117}
118#endif
119
Peng Fan3621efa2023-04-28 12:08:11 +0800120static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000121{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700122 struct cpu_imx_plat *plat = dev_get_plat(dev);
Ye Li3ee6ea42020-05-03 21:58:54 +0800123 int ret, temp;
Peng Fan43c50872019-08-26 08:12:19 +0000124
125 if (size < 100)
126 return -ENOSPC;
127
Peng Fan38e31972023-04-28 12:08:12 +0800128 ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz",
Peng Fan43c50872019-08-26 08:12:19 +0000129 plat->type, plat->rev, plat->name, plat->freq_mhz);
130
131 if (IS_ENABLED(CONFIG_IMX_SCU_THERMAL)) {
Ye Li3ee6ea42020-05-03 21:58:54 +0800132 temp = cpu_imx_get_temp(plat);
Peng Fan43c50872019-08-26 08:12:19 +0000133 buf = buf + ret;
134 size = size - ret;
Ye Li3ee6ea42020-05-03 21:58:54 +0800135 if (temp != 0xdeadbeef)
136 ret = snprintf(buf, size, " at %dC", temp);
137 else
138 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan43c50872019-08-26 08:12:19 +0000139 }
140
141 snprintf(buf + ret, size - ret, "\n");
142
143 return 0;
144}
145
Simon Glass961420f2020-01-26 22:06:27 -0700146static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
Peng Fan43c50872019-08-26 08:12:19 +0000147{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700148 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000149
150 info->cpu_freq = plat->freq_mhz * 1000;
151 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
152 return 0;
153}
154
Simon Glass961420f2020-01-26 22:06:27 -0700155static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000156{
Peng Fanadb3bd72020-05-03 21:58:51 +0800157 ofnode node;
158 int num = 0;
159
160 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
161 const char *device_type;
162
Simon Glass89090662022-09-06 20:27:17 -0600163 if (!ofnode_is_enabled(node))
Peng Fanadb3bd72020-05-03 21:58:51 +0800164 continue;
165
166 device_type = ofnode_read_string(node, "device_type");
167 if (!device_type)
168 continue;
169
170 if (!strcmp(device_type, "cpu"))
171 num++;
172 }
173
174 return num;
Peng Fan43c50872019-08-26 08:12:19 +0000175}
176
Simon Glass961420f2020-01-26 22:06:27 -0700177static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000178{
179 snprintf(buf, size, "NXP");
180 return 0;
181}
182
Peng Fan177f9992020-05-03 21:58:52 +0800183static int cpu_imx_is_current(struct udevice *dev)
184{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700185 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan177f9992020-05-03 21:58:52 +0800186
187 if (plat->mpidr == (read_mpidr() & 0xffff))
188 return 1;
189
190 return 0;
191}
192
Peng Fan38e31972023-04-28 12:08:12 +0800193static const struct cpu_ops cpu_imx_ops = {
Peng Fan43c50872019-08-26 08:12:19 +0000194 .get_desc = cpu_imx_get_desc,
195 .get_info = cpu_imx_get_info,
196 .get_count = cpu_imx_get_count,
197 .get_vendor = cpu_imx_get_vendor,
Peng Fan177f9992020-05-03 21:58:52 +0800198 .is_current = cpu_imx_is_current,
Peng Fan43c50872019-08-26 08:12:19 +0000199};
200
Peng Fan38e31972023-04-28 12:08:12 +0800201static const struct udevice_id cpu_imx_ids[] = {
Peng Fan43c50872019-08-26 08:12:19 +0000202 { .compatible = "arm,cortex-a35" },
203 { .compatible = "arm,cortex-a53" },
Peng Fan38e31972023-04-28 12:08:12 +0800204 { .compatible = "arm,cortex-a55" },
Peng Fan177f9992020-05-03 21:58:52 +0800205 { .compatible = "arm,cortex-a72" },
Peng Fan43c50872019-08-26 08:12:19 +0000206 { }
207};
208
Peng Fan38e31972023-04-28 12:08:12 +0800209static ulong imx_get_cpu_rate(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000210{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700211 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan38e31972023-04-28 12:08:12 +0800212 struct clk clk;
Peng Fan43c50872019-08-26 08:12:19 +0000213 ulong rate;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200214 int ret;
Peng Fan55bc96f2020-05-03 21:58:53 +0800215
Peng Fan38e31972023-04-28 12:08:12 +0800216 if (IS_ENABLED(CONFIG_IMX8)) {
217 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
218 (sc_pm_clock_rate_t *)&rate);
219 } else {
220 ret = clk_get_by_index(dev, 0, &clk);
221 if (!ret) {
222 rate = clk_get_rate(&clk);
223 if (!rate)
224 ret = -EOPNOTSUPP;
225 }
226 }
Peng Fan43c50872019-08-26 08:12:19 +0000227 if (ret) {
228 printf("Could not read CPU frequency: %d\n", ret);
229 return 0;
230 }
231
232 return rate;
233}
234
Peng Fan38e31972023-04-28 12:08:12 +0800235static int imx_cpu_probe(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000236{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700237 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000238 u32 cpurev;
239
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200240 set_core_data(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000241 cpurev = get_cpu_rev();
242 plat->cpurev = cpurev;
Peng Fan38e31972023-04-28 12:08:12 +0800243 plat->rev = get_imx_rev_str(cpurev & 0xFFF);
244 plat->type = get_imx_type_str((cpurev & 0xFF000) >> 12);
245 plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000;
Peng Fan177f9992020-05-03 21:58:52 +0800246 plat->mpidr = dev_read_addr(dev);
247 if (plat->mpidr == FDT_ADDR_T_NONE) {
248 printf("%s: Failed to get CPU reg property\n", __func__);
249 return -EINVAL;
250 }
251
Peng Fan43c50872019-08-26 08:12:19 +0000252 return 0;
253}
254
Peng Fan38e31972023-04-28 12:08:12 +0800255U_BOOT_DRIVER(cpu_imx_drv) = {
256 .name = "imx_cpu",
Peng Fan43c50872019-08-26 08:12:19 +0000257 .id = UCLASS_CPU,
Peng Fan38e31972023-04-28 12:08:12 +0800258 .of_match = cpu_imx_ids,
259 .ops = &cpu_imx_ops,
260 .probe = imx_cpu_probe,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700261 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan43c50872019-08-26 08:12:19 +0000262 .flags = DM_FLAG_PRE_RELOC,
263};