blob: abddbef57b891ee5db0531aa19aee1a065350849 [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 Fan43c50872019-08-26 08:12:19 +000012#include <asm/arch/sci/sci.h>
13#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 Fan43c50872019-08-26 08:12:19 +000017
18DECLARE_GLOBAL_DATA_PTR;
19
Simon Glass8a8d24b2020-12-03 16:55:23 -070020struct cpu_imx_plat {
Peng Fan43c50872019-08-26 08:12:19 +000021 const char *name;
22 const char *rev;
23 const char *type;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020024 u32 cpu_rsrc;
Peng Fan43c50872019-08-26 08:12:19 +000025 u32 cpurev;
26 u32 freq_mhz;
Peng Fan177f9992020-05-03 21:58:52 +080027 u32 mpidr;
Peng Fan43c50872019-08-26 08:12:19 +000028};
29
30const char *get_imx8_type(u32 imxtype)
31{
32 switch (imxtype) {
33 case MXC_CPU_IMX8QXP:
34 case MXC_CPU_IMX8QXP_A0:
35 return "QXP";
36 case MXC_CPU_IMX8QM:
37 return "QM";
38 default:
39 return "??";
40 }
41}
42
43const char *get_imx8_rev(u32 rev)
44{
45 switch (rev) {
46 case CHIP_REV_A:
47 return "A";
48 case CHIP_REV_B:
49 return "B";
Frank Li8142a972020-05-03 21:58:55 +080050 case CHIP_REV_C:
51 return "C";
Peng Fan43c50872019-08-26 08:12:19 +000052 default:
53 return "?";
54 }
55}
56
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020057static void set_core_data(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +000058{
Simon Glass8a8d24b2020-12-03 16:55:23 -070059 struct cpu_imx_plat *plat = dev_get_plat(dev);
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020060
61 if (device_is_compatible(dev, "arm,cortex-a35")) {
62 plat->cpu_rsrc = SC_R_A35;
63 plat->name = "A35";
64 } else if (device_is_compatible(dev, "arm,cortex-a53")) {
65 plat->cpu_rsrc = SC_R_A53;
66 plat->name = "A53";
67 } else if (device_is_compatible(dev, "arm,cortex-a72")) {
68 plat->cpu_rsrc = SC_R_A72;
69 plat->name = "A72";
70 } else {
71 plat->cpu_rsrc = SC_R_A53;
72 plat->name = "?";
73 }
Peng Fan43c50872019-08-26 08:12:19 +000074}
75
76#if IS_ENABLED(CONFIG_IMX_SCU_THERMAL)
Simon Glass8a8d24b2020-12-03 16:55:23 -070077static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +000078{
79 struct udevice *thermal_dev;
80 int cpu_tmp, ret;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020081 int idx = 1; /* use "cpu-thermal0" device */
Peng Fan43c50872019-08-26 08:12:19 +000082
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020083 if (plat->cpu_rsrc == SC_R_A72)
84 idx = 2; /* use "cpu-thermal1" device */
Peng Fan43c50872019-08-26 08:12:19 +000085
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +020086 ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
Peng Fan43c50872019-08-26 08:12:19 +000087 if (!ret) {
88 ret = thermal_get_temp(thermal_dev, &cpu_tmp);
89 if (ret)
90 return 0xdeadbeef;
91 } else {
92 return 0xdeadbeef;
93 }
94
95 return cpu_tmp;
96}
97#else
Simon Glass8a8d24b2020-12-03 16:55:23 -070098static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
Peng Fan43c50872019-08-26 08:12:19 +000099{
100 return 0;
101}
102#endif
103
Simon Glass961420f2020-01-26 22:06:27 -0700104int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000105{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700106 struct cpu_imx_plat *plat = dev_get_plat(dev);
Ye Li3ee6ea42020-05-03 21:58:54 +0800107 int ret, temp;
Peng Fan43c50872019-08-26 08:12:19 +0000108
109 if (size < 100)
110 return -ENOSPC;
111
112 ret = snprintf(buf, size, "NXP i.MX8%s Rev%s %s at %u MHz",
113 plat->type, plat->rev, plat->name, plat->freq_mhz);
114
115 if (IS_ENABLED(CONFIG_IMX_SCU_THERMAL)) {
Ye Li3ee6ea42020-05-03 21:58:54 +0800116 temp = cpu_imx_get_temp(plat);
Peng Fan43c50872019-08-26 08:12:19 +0000117 buf = buf + ret;
118 size = size - ret;
Ye Li3ee6ea42020-05-03 21:58:54 +0800119 if (temp != 0xdeadbeef)
120 ret = snprintf(buf, size, " at %dC", temp);
121 else
122 ret = snprintf(buf, size, " - invalid sensor data");
Peng Fan43c50872019-08-26 08:12:19 +0000123 }
124
125 snprintf(buf + ret, size - ret, "\n");
126
127 return 0;
128}
129
Simon Glass961420f2020-01-26 22:06:27 -0700130static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
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 Fan43c50872019-08-26 08:12:19 +0000133
134 info->cpu_freq = plat->freq_mhz * 1000;
135 info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
136 return 0;
137}
138
Simon Glass961420f2020-01-26 22:06:27 -0700139static int cpu_imx_get_count(const struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000140{
Peng Fanadb3bd72020-05-03 21:58:51 +0800141 ofnode node;
142 int num = 0;
143
144 ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
145 const char *device_type;
146
147 if (!ofnode_is_available(node))
148 continue;
149
150 device_type = ofnode_read_string(node, "device_type");
151 if (!device_type)
152 continue;
153
154 if (!strcmp(device_type, "cpu"))
155 num++;
156 }
157
158 return num;
Peng Fan43c50872019-08-26 08:12:19 +0000159}
160
Simon Glass961420f2020-01-26 22:06:27 -0700161static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
Peng Fan43c50872019-08-26 08:12:19 +0000162{
163 snprintf(buf, size, "NXP");
164 return 0;
165}
166
Peng Fan177f9992020-05-03 21:58:52 +0800167static int cpu_imx_is_current(struct udevice *dev)
168{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700169 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan177f9992020-05-03 21:58:52 +0800170
171 if (plat->mpidr == (read_mpidr() & 0xffff))
172 return 1;
173
174 return 0;
175}
176
Peng Fan43c50872019-08-26 08:12:19 +0000177static const struct cpu_ops cpu_imx8_ops = {
178 .get_desc = cpu_imx_get_desc,
179 .get_info = cpu_imx_get_info,
180 .get_count = cpu_imx_get_count,
181 .get_vendor = cpu_imx_get_vendor,
Peng Fan177f9992020-05-03 21:58:52 +0800182 .is_current = cpu_imx_is_current,
Peng Fan43c50872019-08-26 08:12:19 +0000183};
184
185static const struct udevice_id cpu_imx8_ids[] = {
186 { .compatible = "arm,cortex-a35" },
187 { .compatible = "arm,cortex-a53" },
Peng Fan177f9992020-05-03 21:58:52 +0800188 { .compatible = "arm,cortex-a72" },
Peng Fan43c50872019-08-26 08:12:19 +0000189 { }
190};
191
Peng Fan55bc96f2020-05-03 21:58:53 +0800192static ulong imx8_get_cpu_rate(struct udevice *dev)
Peng Fan43c50872019-08-26 08:12:19 +0000193{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700194 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000195 ulong rate;
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200196 int ret;
Peng Fan55bc96f2020-05-03 21:58:53 +0800197
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200198 ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
Peng Fan43c50872019-08-26 08:12:19 +0000199 (sc_pm_clock_rate_t *)&rate);
200 if (ret) {
201 printf("Could not read CPU frequency: %d\n", ret);
202 return 0;
203 }
204
205 return rate;
206}
207
208static int imx8_cpu_probe(struct udevice *dev)
209{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700210 struct cpu_imx_plat *plat = dev_get_plat(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000211 u32 cpurev;
212
Anatolij Gustschin74e8fb02020-05-20 01:31:44 +0200213 set_core_data(dev);
Peng Fan43c50872019-08-26 08:12:19 +0000214 cpurev = get_cpu_rev();
215 plat->cpurev = cpurev;
Peng Fan43c50872019-08-26 08:12:19 +0000216 plat->rev = get_imx8_rev(cpurev & 0xFFF);
217 plat->type = get_imx8_type((cpurev & 0xFF000) >> 12);
Peng Fan55bc96f2020-05-03 21:58:53 +0800218 plat->freq_mhz = imx8_get_cpu_rate(dev) / 1000000;
Peng Fan177f9992020-05-03 21:58:52 +0800219 plat->mpidr = dev_read_addr(dev);
220 if (plat->mpidr == FDT_ADDR_T_NONE) {
221 printf("%s: Failed to get CPU reg property\n", __func__);
222 return -EINVAL;
223 }
224
Peng Fan43c50872019-08-26 08:12:19 +0000225 return 0;
226}
227
228U_BOOT_DRIVER(cpu_imx8_drv) = {
229 .name = "imx8x_cpu",
230 .id = UCLASS_CPU,
231 .of_match = cpu_imx8_ids,
232 .ops = &cpu_imx8_ops,
233 .probe = imx8_cpu_probe,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700234 .plat_auto = sizeof(struct cpu_imx_plat),
Peng Fan43c50872019-08-26 08:12:19 +0000235 .flags = DM_FLAG_PRE_RELOC,
236};