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 | |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 6 | #include <cpu.h> |
| 7 | #include <dm.h> |
| 8 | #include <thermal.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 9 | #include <asm/global_data.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 10 | #include <asm/system.h> |
Peng Fan | 2e0644a | 2023-04-28 12:08:09 +0800 | [diff] [blame] | 11 | #include <firmware/imx/sci/sci.h> |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 12 | #include <asm/arch/sys_proto.h> |
| 13 | #include <asm/arch-imx/cpu.h> |
| 14 | #include <asm/armv8/cpu.h> |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 15 | #include <imx_thermal.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 17 | #include <linux/clk-provider.h> |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 18 | |
| 19 | DECLARE_GLOBAL_DATA_PTR; |
| 20 | |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 21 | struct cpu_imx_plat { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 22 | const char *name; |
| 23 | const char *rev; |
| 24 | const char *type; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 25 | u32 cpu_rsrc; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 26 | u32 cpurev; |
| 27 | u32 freq_mhz; |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 28 | u32 mpidr; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 29 | }; |
| 30 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 31 | static const char *get_imx_type_str(u32 imxtype) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 32 | { |
| 33 | switch (imxtype) { |
| 34 | case MXC_CPU_IMX8QXP: |
| 35 | case MXC_CPU_IMX8QXP_A0: |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 36 | return "8QXP"; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 37 | case MXC_CPU_IMX8QM: |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 38 | return "8QM"; |
| 39 | case MXC_CPU_IMX93: |
| 40 | return "93(52)";/* iMX93 Dual core with NPU */ |
Peng Fan | c3db3ad | 2023-04-28 12:08:32 +0800 | [diff] [blame] | 41 | case MXC_CPU_IMX9351: |
| 42 | return "93(51)";/* iMX93 Single core with NPU */ |
| 43 | case MXC_CPU_IMX9332: |
| 44 | return "93(32)";/* iMX93 Dual core without NPU */ |
| 45 | case MXC_CPU_IMX9331: |
| 46 | return "93(31)";/* iMX93 Single core without NPU */ |
| 47 | case MXC_CPU_IMX9322: |
| 48 | return "93(22)";/* iMX93 9x9 Dual core */ |
| 49 | case MXC_CPU_IMX9321: |
| 50 | return "93(21)";/* iMX93 9x9 Single core */ |
| 51 | case MXC_CPU_IMX9312: |
| 52 | return "93(12)";/* iMX93 9x9 Dual core without NPU */ |
| 53 | case MXC_CPU_IMX9311: |
| 54 | return "93(11)";/* iMX93 9x9 Single core without NPU */ |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 55 | default: |
| 56 | return "??"; |
| 57 | } |
| 58 | } |
| 59 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 60 | static const char *get_imx_rev_str(u32 rev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 61 | { |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 62 | static char revision[4]; |
| 63 | |
| 64 | if (IS_ENABLED(CONFIG_IMX8)) { |
| 65 | switch (rev) { |
| 66 | case CHIP_REV_A: |
| 67 | return "A"; |
| 68 | case CHIP_REV_B: |
| 69 | return "B"; |
| 70 | case CHIP_REV_C: |
| 71 | return "C"; |
| 72 | default: |
| 73 | return "?"; |
| 74 | } |
| 75 | } else { |
| 76 | revision[0] = '1' + (((rev & 0xf0) - CHIP_REV_1_0) >> 4); |
| 77 | revision[1] = '.'; |
| 78 | revision[2] = '0' + (rev & 0xf); |
| 79 | revision[3] = '\0'; |
| 80 | |
| 81 | return revision; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 85 | static void set_core_data(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 86 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 87 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 88 | |
| 89 | if (device_is_compatible(dev, "arm,cortex-a35")) { |
| 90 | plat->cpu_rsrc = SC_R_A35; |
| 91 | plat->name = "A35"; |
| 92 | } else if (device_is_compatible(dev, "arm,cortex-a53")) { |
| 93 | plat->cpu_rsrc = SC_R_A53; |
| 94 | plat->name = "A53"; |
| 95 | } else if (device_is_compatible(dev, "arm,cortex-a72")) { |
| 96 | plat->cpu_rsrc = SC_R_A72; |
| 97 | plat->name = "A72"; |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 98 | } else if (device_is_compatible(dev, "arm,cortex-a55")) { |
| 99 | plat->name = "A55"; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 100 | } else { |
| 101 | plat->cpu_rsrc = SC_R_A53; |
| 102 | plat->name = "?"; |
| 103 | } |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 104 | } |
| 105 | |
Peng Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 106 | #if IS_ENABLED(CONFIG_DM_THERMAL) |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 107 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 108 | { |
| 109 | struct udevice *thermal_dev; |
| 110 | int cpu_tmp, ret; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 111 | int idx = 1; /* use "cpu-thermal0" device */ |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 112 | |
Peng Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 113 | if (IS_ENABLED(CONFIG_IMX8)) { |
| 114 | if (plat->cpu_rsrc == SC_R_A72) |
| 115 | idx = 2; /* use "cpu-thermal1" device */ |
| 116 | } else { |
| 117 | idx = 1; |
| 118 | } |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 119 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 120 | ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 121 | if (!ret) { |
| 122 | ret = thermal_get_temp(thermal_dev, &cpu_tmp); |
| 123 | if (ret) |
| 124 | return 0xdeadbeef; |
| 125 | } else { |
| 126 | return 0xdeadbeef; |
| 127 | } |
| 128 | |
| 129 | return cpu_tmp; |
| 130 | } |
| 131 | #else |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 132 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 133 | { |
| 134 | return 0; |
| 135 | } |
| 136 | #endif |
| 137 | |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 138 | __weak u32 get_cpu_temp_grade(int *minc, int *maxc) |
| 139 | { |
| 140 | return 0; |
| 141 | } |
| 142 | |
Peng Fan | d3ee4de | 2023-04-28 12:08:11 +0800 | [diff] [blame] | 143 | 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] | 144 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 145 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 146 | const char *grade; |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 147 | int ret, temp; |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 148 | int minc, maxc; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 149 | |
| 150 | if (size < 100) |
| 151 | return -ENOSPC; |
| 152 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 153 | 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] | 154 | plat->type, plat->rev, plat->name, plat->freq_mhz); |
| 155 | |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 156 | if (IS_ENABLED(CONFIG_IMX9)) { |
| 157 | switch (get_cpu_temp_grade(&minc, &maxc)) { |
| 158 | case TEMP_AUTOMOTIVE: |
| 159 | grade = "Automotive temperature grade "; |
| 160 | break; |
| 161 | case TEMP_INDUSTRIAL: |
| 162 | grade = "Industrial temperature grade "; |
| 163 | break; |
| 164 | case TEMP_EXTCOMMERCIAL: |
| 165 | grade = "Extended Consumer temperature grade "; |
| 166 | break; |
| 167 | default: |
| 168 | grade = "Consumer temperature grade "; |
| 169 | break; |
| 170 | } |
| 171 | |
| 172 | buf = buf + ret; |
| 173 | size = size - ret; |
| 174 | ret = snprintf(buf, size, "\nCPU: %s (%dC to %dC)", grade, minc, maxc); |
| 175 | } |
| 176 | |
Peng Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 177 | if (IS_ENABLED(CONFIG_DM_THERMAL)) { |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 178 | temp = cpu_imx_get_temp(plat); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 179 | buf = buf + ret; |
| 180 | size = size - ret; |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 181 | if (temp != 0xdeadbeef) |
| 182 | ret = snprintf(buf, size, " at %dC", temp); |
| 183 | else |
| 184 | ret = snprintf(buf, size, " - invalid sensor data"); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | snprintf(buf + ret, size - ret, "\n"); |
| 188 | |
| 189 | return 0; |
| 190 | } |
| 191 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 192 | 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] | 193 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 194 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 195 | |
| 196 | info->cpu_freq = plat->freq_mhz * 1000; |
| 197 | info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU); |
| 198 | return 0; |
| 199 | } |
| 200 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 201 | static int cpu_imx_get_count(const struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 202 | { |
Peng Fan | 8296b74 | 2020-05-03 21:58:51 +0800 | [diff] [blame] | 203 | ofnode node; |
| 204 | int num = 0; |
| 205 | |
| 206 | ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) { |
| 207 | const char *device_type; |
| 208 | |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 209 | if (!ofnode_is_enabled(node)) |
Peng Fan | 8296b74 | 2020-05-03 21:58:51 +0800 | [diff] [blame] | 210 | continue; |
| 211 | |
| 212 | device_type = ofnode_read_string(node, "device_type"); |
| 213 | if (!device_type) |
| 214 | continue; |
| 215 | |
| 216 | if (!strcmp(device_type, "cpu")) |
| 217 | num++; |
| 218 | } |
| 219 | |
| 220 | return num; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 221 | } |
| 222 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 223 | 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] | 224 | { |
| 225 | snprintf(buf, size, "NXP"); |
| 226 | return 0; |
| 227 | } |
| 228 | |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 229 | static int cpu_imx_is_current(struct udevice *dev) |
| 230 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 231 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 232 | |
| 233 | if (plat->mpidr == (read_mpidr() & 0xffff)) |
| 234 | return 1; |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 239 | static const struct cpu_ops cpu_imx_ops = { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 240 | .get_desc = cpu_imx_get_desc, |
| 241 | .get_info = cpu_imx_get_info, |
| 242 | .get_count = cpu_imx_get_count, |
| 243 | .get_vendor = cpu_imx_get_vendor, |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 244 | .is_current = cpu_imx_is_current, |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 245 | }; |
| 246 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 247 | static const struct udevice_id cpu_imx_ids[] = { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 248 | { .compatible = "arm,cortex-a35" }, |
| 249 | { .compatible = "arm,cortex-a53" }, |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 250 | { .compatible = "arm,cortex-a55" }, |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 251 | { .compatible = "arm,cortex-a72" }, |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 252 | { } |
| 253 | }; |
| 254 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 255 | static ulong imx_get_cpu_rate(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 256 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 257 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 258 | struct clk clk; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 259 | ulong rate; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 260 | int ret; |
Peng Fan | 4b1fbb7 | 2020-05-03 21:58:53 +0800 | [diff] [blame] | 261 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 262 | if (IS_ENABLED(CONFIG_IMX8)) { |
| 263 | ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU, |
| 264 | (sc_pm_clock_rate_t *)&rate); |
| 265 | } else { |
| 266 | ret = clk_get_by_index(dev, 0, &clk); |
| 267 | if (!ret) { |
| 268 | rate = clk_get_rate(&clk); |
| 269 | if (!rate) |
| 270 | ret = -EOPNOTSUPP; |
| 271 | } |
| 272 | } |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 273 | if (ret) { |
| 274 | printf("Could not read CPU frequency: %d\n", ret); |
| 275 | return 0; |
| 276 | } |
| 277 | |
| 278 | return rate; |
| 279 | } |
| 280 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 281 | static int imx_cpu_probe(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 282 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 283 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 284 | u32 cpurev; |
| 285 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 286 | set_core_data(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 287 | cpurev = get_cpu_rev(); |
| 288 | plat->cpurev = cpurev; |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 289 | plat->rev = get_imx_rev_str(cpurev & 0xFFF); |
| 290 | plat->type = get_imx_type_str((cpurev & 0xFF000) >> 12); |
| 291 | plat->freq_mhz = imx_get_cpu_rate(dev) / 1000000; |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 292 | plat->mpidr = dev_read_addr(dev); |
| 293 | if (plat->mpidr == FDT_ADDR_T_NONE) { |
| 294 | printf("%s: Failed to get CPU reg property\n", __func__); |
| 295 | return -EINVAL; |
| 296 | } |
| 297 | |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 298 | return 0; |
| 299 | } |
| 300 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 301 | U_BOOT_DRIVER(cpu_imx_drv) = { |
| 302 | .name = "imx_cpu", |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 303 | .id = UCLASS_CPU, |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 304 | .of_match = cpu_imx_ids, |
| 305 | .ops = &cpu_imx_ops, |
| 306 | .probe = imx_cpu_probe, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 307 | .plat_auto = sizeof(struct cpu_imx_plat), |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 308 | .flags = DM_FLAG_PRE_RELOC, |
| 309 | }; |