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 | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 42 | default: |
| 43 | return "??"; |
| 44 | } |
| 45 | } |
| 46 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 47 | static const char *get_imx_rev_str(u32 rev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 48 | { |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 49 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 69 | } |
| 70 | } |
| 71 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 72 | static void set_core_data(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 73 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 74 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 75 | |
| 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 Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 85 | } else if (device_is_compatible(dev, "arm,cortex-a55")) { |
| 86 | plat->name = "A55"; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 87 | } else { |
| 88 | plat->cpu_rsrc = SC_R_A53; |
| 89 | plat->name = "?"; |
| 90 | } |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 91 | } |
| 92 | |
Peng Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 93 | #if IS_ENABLED(CONFIG_DM_THERMAL) |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 94 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 95 | { |
| 96 | struct udevice *thermal_dev; |
| 97 | int cpu_tmp, ret; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 98 | int idx = 1; /* use "cpu-thermal0" device */ |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 99 | |
Peng Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 100 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 106 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 107 | ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 108 | 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 Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 119 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 120 | { |
| 121 | return 0; |
| 122 | } |
| 123 | #endif |
| 124 | |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 125 | __weak u32 get_cpu_temp_grade(int *minc, int *maxc) |
| 126 | { |
| 127 | return 0; |
| 128 | } |
| 129 | |
Peng Fan | d3ee4de | 2023-04-28 12:08:11 +0800 | [diff] [blame] | 130 | 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] | 131 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 132 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 133 | const char *grade; |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 134 | int ret, temp; |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 135 | int minc, maxc; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 136 | |
| 137 | if (size < 100) |
| 138 | return -ENOSPC; |
| 139 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 140 | 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] | 141 | plat->type, plat->rev, plat->name, plat->freq_mhz); |
| 142 | |
Peng Fan | 81c694a | 2023-04-28 12:08:14 +0800 | [diff] [blame] | 143 | 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 Fan | 32eaf67 | 2023-04-28 12:08:13 +0800 | [diff] [blame] | 164 | if (IS_ENABLED(CONFIG_DM_THERMAL)) { |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 165 | temp = cpu_imx_get_temp(plat); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 166 | buf = buf + ret; |
| 167 | size = size - ret; |
Ye Li | cd8d1c5 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 168 | if (temp != 0xdeadbeef) |
| 169 | ret = snprintf(buf, size, " at %dC", temp); |
| 170 | else |
| 171 | ret = snprintf(buf, size, " - invalid sensor data"); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | snprintf(buf + ret, size - ret, "\n"); |
| 175 | |
| 176 | return 0; |
| 177 | } |
| 178 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 179 | 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] | 180 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 181 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 182 | |
| 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 Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 188 | static int cpu_imx_get_count(const struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 189 | { |
Peng Fan | 8296b74 | 2020-05-03 21:58:51 +0800 | [diff] [blame] | 190 | ofnode node; |
| 191 | int num = 0; |
| 192 | |
| 193 | ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) { |
| 194 | const char *device_type; |
| 195 | |
Simon Glass | 2e4938b | 2022-09-06 20:27:17 -0600 | [diff] [blame] | 196 | if (!ofnode_is_enabled(node)) |
Peng Fan | 8296b74 | 2020-05-03 21:58:51 +0800 | [diff] [blame] | 197 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 208 | } |
| 209 | |
Simon Glass | 791fa45 | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 210 | 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] | 211 | { |
| 212 | snprintf(buf, size, "NXP"); |
| 213 | return 0; |
| 214 | } |
| 215 | |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 216 | static int cpu_imx_is_current(struct udevice *dev) |
| 217 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 218 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 219 | |
| 220 | if (plat->mpidr == (read_mpidr() & 0xffff)) |
| 221 | return 1; |
| 222 | |
| 223 | return 0; |
| 224 | } |
| 225 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 226 | static const struct cpu_ops cpu_imx_ops = { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 227 | .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 Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 231 | .is_current = cpu_imx_is_current, |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 232 | }; |
| 233 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 234 | static const struct udevice_id cpu_imx_ids[] = { |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 235 | { .compatible = "arm,cortex-a35" }, |
| 236 | { .compatible = "arm,cortex-a53" }, |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 237 | { .compatible = "arm,cortex-a55" }, |
Peng Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 238 | { .compatible = "arm,cortex-a72" }, |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 239 | { } |
| 240 | }; |
| 241 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 242 | static ulong imx_get_cpu_rate(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 243 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 244 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 245 | struct clk clk; |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 246 | ulong rate; |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 247 | int ret; |
Peng Fan | 4b1fbb7 | 2020-05-03 21:58:53 +0800 | [diff] [blame] | 248 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 249 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 260 | if (ret) { |
| 261 | printf("Could not read CPU frequency: %d\n", ret); |
| 262 | return 0; |
| 263 | } |
| 264 | |
| 265 | return rate; |
| 266 | } |
| 267 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 268 | static int imx_cpu_probe(struct udevice *dev) |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 269 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 270 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 271 | u32 cpurev; |
| 272 | |
Anatolij Gustschin | 50ca4a0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 273 | set_core_data(dev); |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 274 | cpurev = get_cpu_rev(); |
| 275 | plat->cpurev = cpurev; |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 276 | 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 Fan | e2ded33 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 279 | 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 Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 285 | return 0; |
| 286 | } |
| 287 | |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 288 | U_BOOT_DRIVER(cpu_imx_drv) = { |
| 289 | .name = "imx_cpu", |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 290 | .id = UCLASS_CPU, |
Peng Fan | 146cce9 | 2023-04-28 12:08:12 +0800 | [diff] [blame] | 291 | .of_match = cpu_imx_ids, |
| 292 | .ops = &cpu_imx_ops, |
| 293 | .probe = imx_cpu_probe, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 294 | .plat_auto = sizeof(struct cpu_imx_plat), |
Peng Fan | 21981d2 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 295 | .flags = DM_FLAG_PRE_RELOC, |
| 296 | }; |