Peng Fan | 43c5087 | 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 | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Simon Glass | 90526e9 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 11 | #include <asm/system.h> |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 12 | #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 Glass | cd93d62 | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 16 | #include <linux/bitops.h> |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 20 | struct cpu_imx_plat { |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 21 | const char *name; |
| 22 | const char *rev; |
| 23 | const char *type; |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 24 | u32 cpu_rsrc; |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 25 | u32 cpurev; |
| 26 | u32 freq_mhz; |
Peng Fan | 177f999 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 27 | u32 mpidr; |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 28 | }; |
| 29 | |
| 30 | const 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 | |
| 43 | const 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 Li | 8142a97 | 2020-05-03 21:58:55 +0800 | [diff] [blame] | 50 | case CHIP_REV_C: |
| 51 | return "C"; |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 52 | default: |
| 53 | return "?"; |
| 54 | } |
| 55 | } |
| 56 | |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 57 | static void set_core_data(struct udevice *dev) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 58 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 59 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 60 | |
| 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 Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | #if IS_ENABLED(CONFIG_IMX_SCU_THERMAL) |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 77 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 78 | { |
| 79 | struct udevice *thermal_dev; |
| 80 | int cpu_tmp, ret; |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 81 | int idx = 1; /* use "cpu-thermal0" device */ |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 82 | |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 83 | if (plat->cpu_rsrc == SC_R_A72) |
| 84 | idx = 2; /* use "cpu-thermal1" device */ |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 85 | |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 86 | ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev); |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 87 | 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 Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 98 | static int cpu_imx_get_temp(struct cpu_imx_plat *plat) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 99 | { |
| 100 | return 0; |
| 101 | } |
| 102 | #endif |
| 103 | |
Simon Glass | 961420f | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 104 | int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 105 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 106 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Ye Li | 3ee6ea4 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 107 | int ret, temp; |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 108 | |
| 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 Li | 3ee6ea4 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 116 | temp = cpu_imx_get_temp(plat); |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 117 | buf = buf + ret; |
| 118 | size = size - ret; |
Ye Li | 3ee6ea4 | 2020-05-03 21:58:54 +0800 | [diff] [blame] | 119 | if (temp != 0xdeadbeef) |
| 120 | ret = snprintf(buf, size, " at %dC", temp); |
| 121 | else |
| 122 | ret = snprintf(buf, size, " - invalid sensor data"); |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | snprintf(buf + ret, size - ret, "\n"); |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
Simon Glass | 961420f | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 130 | static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 131 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 132 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 133 | |
| 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 Glass | 961420f | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 139 | static int cpu_imx_get_count(const struct udevice *dev) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 140 | { |
Peng Fan | adb3bd7 | 2020-05-03 21:58:51 +0800 | [diff] [blame] | 141 | 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 Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Simon Glass | 961420f | 2020-01-26 22:06:27 -0700 | [diff] [blame] | 161 | static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 162 | { |
| 163 | snprintf(buf, size, "NXP"); |
| 164 | return 0; |
| 165 | } |
| 166 | |
Peng Fan | 177f999 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 167 | static int cpu_imx_is_current(struct udevice *dev) |
| 168 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 169 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 177f999 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 170 | |
| 171 | if (plat->mpidr == (read_mpidr() & 0xffff)) |
| 172 | return 1; |
| 173 | |
| 174 | return 0; |
| 175 | } |
| 176 | |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 177 | static 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 Fan | 177f999 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 182 | .is_current = cpu_imx_is_current, |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 183 | }; |
| 184 | |
| 185 | static const struct udevice_id cpu_imx8_ids[] = { |
| 186 | { .compatible = "arm,cortex-a35" }, |
| 187 | { .compatible = "arm,cortex-a53" }, |
Peng Fan | 177f999 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 188 | { .compatible = "arm,cortex-a72" }, |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 189 | { } |
| 190 | }; |
| 191 | |
Peng Fan | 55bc96f | 2020-05-03 21:58:53 +0800 | [diff] [blame] | 192 | static ulong imx8_get_cpu_rate(struct udevice *dev) |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 193 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 194 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 195 | ulong rate; |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 196 | int ret; |
Peng Fan | 55bc96f | 2020-05-03 21:58:53 +0800 | [diff] [blame] | 197 | |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 198 | ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU, |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 199 | (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 | |
| 208 | static int imx8_cpu_probe(struct udevice *dev) |
| 209 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 210 | struct cpu_imx_plat *plat = dev_get_plat(dev); |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 211 | u32 cpurev; |
| 212 | |
Anatolij Gustschin | 74e8fb0 | 2020-05-20 01:31:44 +0200 | [diff] [blame] | 213 | set_core_data(dev); |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 214 | cpurev = get_cpu_rev(); |
| 215 | plat->cpurev = cpurev; |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 216 | plat->rev = get_imx8_rev(cpurev & 0xFFF); |
| 217 | plat->type = get_imx8_type((cpurev & 0xFF000) >> 12); |
Peng Fan | 55bc96f | 2020-05-03 21:58:53 +0800 | [diff] [blame] | 218 | plat->freq_mhz = imx8_get_cpu_rate(dev) / 1000000; |
Peng Fan | 177f999 | 2020-05-03 21:58:52 +0800 | [diff] [blame] | 219 | 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 Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 225 | return 0; |
| 226 | } |
| 227 | |
| 228 | U_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 Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 234 | .plat_auto = sizeof(struct cpu_imx_plat), |
Peng Fan | 43c5087 | 2019-08-26 08:12:19 +0000 | [diff] [blame] | 235 | .flags = DM_FLAG_PRE_RELOC, |
| 236 | }; |