Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 1 | // 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 Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 11 | #include <asm/system.h> |
Peng Fan | 2e0644a | 2023-04-28 12:08:09 +0800 | [diff] [blame] | 12 | #include <firmware/imx/sci/sci.h> |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 13 | #include <asm/arch/sys_proto.h> |
| 14 | #include <asm/arch-imx/cpu.h> |
| 15 | #include <asm/armv8/cpu.h> |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 16 | #include <imx_thermal.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 17 | #include <linux/bitops.h> |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 18 | #include <linux/clk-provider.h> |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 19 | |
| 20 | DECLARE_GLOBAL_DATA_PTR; |
| 21 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 22 | struct cpu_imx_plat { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 23 | const char *name; |
| 24 | const char *rev; |
| 25 | const char *type; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 26 | u32 cpu_rsrc; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 27 | u32 cpurev; |
| 28 | u32 freq_mhz; |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 29 | u32 mpidr; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 30 | }; |
| 31 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 32 | static const char *get_imx_type_str(u32 imxtype) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 33 | { |
| 34 | switch (imxtype) { |
| 35 | case MXC_CPU_IMX8QXP: |
| 36 | case MXC_CPU_IMX8QXP_A0: |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 37 | return "8QXP"; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 38 | case MXC_CPU_IMX8QM: |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 39 | return "8QM"; |
| 40 | case MXC_CPU_IMX93: |
| 41 | return "93(52)";/* iMX93 Dual core with NPU */ |
Peng Fan | c3db3ad | 2023-04-28 12:08:32 +0800 | [diff] [blame] | 42 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 56 | default: |
| 57 | return "??"; |
| 58 | } |
| 59 | } |
| 60 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 61 | static const char *get_imx_rev_str(u32 rev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 62 | { |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 63 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 86 | static void set_core_data(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 87 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 88 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 89 | |
| 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 Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 99 | } else if (device_is_compatible(dev, "arm,cortex-a55")) { |
| 100 | plat->name = "A55"; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 101 | } else { |
| 102 | plat->cpu_rsrc = SC_R_A53; |
| 103 | plat->name = "?"; |
| 104 | } |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 105 | } |
| 106 | |
Peng Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 107 | #if IS_ENABLED(CONFIG_DM_THERMAL) |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 108 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 109 | { |
| 110 | struct udevice *thermal_dev; |
| 111 | int cpu_tmp, ret; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 112 | int idx = 1; /* use "cpu-thermal0" device */ |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 113 | |
Peng Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 114 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 120 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 121 | ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 122 | 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 133 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 134 | { |
| 135 | return 0; |
| 136 | } |
| 137 | #endif |
| 138 | |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 139 | __weak u32 get_cpu_temp_grade(int *minc, int *maxc) |
| 140 | { |
| 141 | return 0; |
| 142 | } |
| 143 | |
Peng Fan | d3ee4de | 2023-04-28 12:08:11 +0800 | [diff] [blame] | 144 | static int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 145 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 146 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 147 | const char *grade; |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 148 | int ret, temp; |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 149 | int minc, maxc; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 150 | |
| 151 | if (size < 100) |
| 152 | return -ENOSPC; |
| 153 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 154 | ret = snprintf(buf, size, "NXP i.MX%s Rev%s %s at %u MHz", |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 155 | plat->type, plat->rev, plat->name, plat->freq_mhz); |
| 156 | |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 157 | 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 Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 178 | if (IS_ENABLED(CONFIG_DM_THERMAL)) { |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 179 | temp = cpu_imx_get_temp(plat); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 180 | buf = buf + ret; |
| 181 | size = size - ret; |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 182 | if (temp != 0xdeadbeef) |
| 183 | ret = snprintf(buf, size, " at %dC", temp); |
| 184 | else |
| 185 | ret = snprintf(buf, size, " - invalid sensor data"); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | snprintf(buf + ret, size - ret, "\n"); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 193 | static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 194 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 195 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 196 | |
| 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 Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 202 | static int cpu_imx_get_count(const struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 203 | { |
Peng Fan | 8296b74 | 2020-05-03 21:58:51 +0800 | [diff] [blame] | 204 | ofnode node; |
| 205 | int num = 0; |
| 206 | |
| 207 | ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) { |
| 208 | const char *device_type; |
| 209 | |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 210 | if (!ofnode_is_enabled(node)) |
Peng Fan | 8296b74 | 2020-05-03 21:58:51 +0800 | [diff] [blame] | 211 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 224 | static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 225 | { |
| 226 | snprintf(buf, size, "NXP"); |
| 227 | return 0; |
| 228 | } |
| 229 | |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 230 | static int cpu_imx_is_current(struct udevice *dev) |
| 231 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 232 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 233 | |
| 234 | if (plat->mpidr == (read_mpidr() & 0xffff)) |
| 235 | return 1; |
| 236 | |
| 237 | return 0; |
| 238 | } |
| 239 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 240 | static const struct cpu_ops cpu_imx_ops = { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 241 | .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 Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 245 | .is_current = cpu_imx_is_current, |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 248 | static const struct udevice_id cpu_imx_ids[] = { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 249 | { .compatible = "arm,cortex-a35" }, |
| 250 | { .compatible = "arm,cortex-a53" }, |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 251 | { .compatible = "arm,cortex-a55" }, |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 252 | { .compatible = "arm,cortex-a72" }, |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 253 | { } |
| 254 | }; |
| 255 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 256 | static ulong imx_get_cpu_rate(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 257 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 258 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 259 | struct clk clk; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 260 | ulong rate; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 261 | int ret; |
Peng Fan | 4b1fbb7 | 2020-05-03 21:58:53 +0800 | [diff] [blame] | 262 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 263 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 274 | if (ret) { |
| 275 | printf("Could not read CPU frequency: %d\n", ret); |
| 276 | return 0; |
| 277 | } |
| 278 | |
| 279 | return rate; |
| 280 | } |
| 281 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 282 | static int imx_cpu_probe(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 283 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 284 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 285 | u32 cpurev; |
| 286 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 287 | set_core_data(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 288 | cpurev = get_cpu_rev(); |
| 289 | plat->cpurev = cpurev; |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 290 | 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 Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 293 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
| 301 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 302 | U_BOOT_DRIVER(cpu_imx_drv) = { |
| 303 | .name = "imx_cpu", |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 304 | .id = UCLASS_CPU, |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 305 | .of_match = cpu_imx_ids, |
| 306 | .ops = &cpu_imx_ops, |
| 307 | .probe = imx_cpu_probe, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 308 | .plat_auto = sizeof(struct cpu_imx_plat), |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 309 | .flags = DM_FLAG_PRE_RELOC, |
| 310 | }; |