imx9_cpu.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2019 NXP
  4. */
  5. #include <common.h>
  6. #include <cpu.h>
  7. #include <dm.h>
  8. #include <thermal.h>
  9. #include <asm/global_data.h>
  10. #include <asm/system.h>
  11. #include <firmware/linux/imx/sci/sci.h>
  12. #include <asm/arch/sys_proto.h>
  13. #include <asm/arch-imx/cpu.h>
  14. #include <asm/armv8/cpu.h>
  15. #include <linux/bitops.h>
  16. DECLARE_GLOBAL_DATA_PTR;
  17. struct cpu_imx_plat {
  18. const char *name;
  19. const char *rev;
  20. const char *type;
  21. u32 cpu_rsrc;
  22. u32 cpurev;
  23. u32 freq_mhz;
  24. u32 mpidr;
  25. };
  26. const char *get_imx9_type(u32 imxtype)
  27. {
  28. switch (imxtype) {
  29. case MXC_CPU_IMX93:
  30. return "93";
  31. default:
  32. return "??";
  33. }
  34. }
  35. const char *get_imx9_rev(u32 rev)
  36. {
  37. switch (rev) {
  38. case CHIP_REV_1_0:
  39. return "1.";
  40. case CHIP_REV_B:
  41. return "B";
  42. case CHIP_REV_C:
  43. return "C";
  44. default:
  45. return "?";
  46. }
  47. }
  48. static void set_core_data(struct udevice *dev)
  49. {
  50. struct cpu_imx_plat *plat = dev_get_plat(dev);
  51. if (device_is_compatible(dev, "arm,cortex-a35"))
  52. plat->name = "A35";
  53. else
  54. plat->name = "?";
  55. }
  56. #if IS_ENABLED(CONFIG_IMX_SCU_THERMAL)
  57. static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
  58. {
  59. struct udevice *thermal_dev;
  60. int cpu_tmp, ret;
  61. int idx = 1; /* use "cpu-thermal0" device */
  62. if (plat->cpu_rsrc == SC_R_A72)
  63. idx = 2; /* use "cpu-thermal1" device */
  64. ret = uclass_get_device(UCLASS_THERMAL, idx, &thermal_dev);
  65. if (!ret) {
  66. ret = thermal_get_temp(thermal_dev, &cpu_tmp);
  67. if (ret)
  68. return 0xdeadbeef;
  69. } else {
  70. return 0xdeadbeef;
  71. }
  72. return cpu_tmp;
  73. }
  74. #else
  75. static int cpu_imx_get_temp(struct cpu_imx_plat *plat)
  76. {
  77. return 0;
  78. }
  79. #endif
  80. int cpu_imx_get_desc(const struct udevice *dev, char *buf, int size)
  81. {
  82. struct cpu_imx_plat *plat = dev_get_plat(dev);
  83. int ret, temp;
  84. if (size < 100)
  85. return -ENOSPC;
  86. ret = snprintf(buf, size, "NXP i.MX8%s Rev%s %s at %u MHz",
  87. plat->type, plat->rev, plat->name, plat->freq_mhz);
  88. if (IS_ENABLED(CONFIG_IMX_SCU_THERMAL)) {
  89. temp = cpu_imx_get_temp(plat);
  90. buf = buf + ret;
  91. size = size - ret;
  92. if (temp != 0xdeadbeef)
  93. ret = snprintf(buf, size, " at %dC", temp);
  94. else
  95. ret = snprintf(buf, size, " - invalid sensor data");
  96. }
  97. snprintf(buf + ret, size - ret, "\n");
  98. return 0;
  99. }
  100. static int cpu_imx_get_info(const struct udevice *dev, struct cpu_info *info)
  101. {
  102. struct cpu_imx_plat *plat = dev_get_plat(dev);
  103. info->cpu_freq = plat->freq_mhz * 1000;
  104. info->features = BIT(CPU_FEAT_L1_CACHE) | BIT(CPU_FEAT_MMU);
  105. return 0;
  106. }
  107. static int cpu_imx_get_count(const struct udevice *dev)
  108. {
  109. ofnode node;
  110. int num = 0;
  111. ofnode_for_each_subnode(node, dev_ofnode(dev->parent)) {
  112. const char *device_type;
  113. if (!ofnode_is_enabled(node))
  114. continue;
  115. device_type = ofnode_read_string(node, "device_type");
  116. if (!device_type)
  117. continue;
  118. if (!strcmp(device_type, "cpu"))
  119. num++;
  120. }
  121. return num;
  122. }
  123. static int cpu_imx_get_vendor(const struct udevice *dev, char *buf, int size)
  124. {
  125. snprintf(buf, size, "NXP");
  126. return 0;
  127. }
  128. static int cpu_imx_is_current(struct udevice *dev)
  129. {
  130. struct cpu_imx_plat *plat = dev_get_plat(dev);
  131. if (plat->mpidr == (read_mpidr() & 0xffff))
  132. return 1;
  133. return 0;
  134. }
  135. static const struct cpu_ops cpu_imx9_ops = {
  136. .get_desc = cpu_imx_get_desc,
  137. .get_info = cpu_imx_get_info,
  138. .get_count = cpu_imx_get_count,
  139. .get_vendor = cpu_imx_get_vendor,
  140. .is_current = cpu_imx_is_current,
  141. };
  142. static const struct udevice_id cpu_imx9_ids[] = {
  143. { .compatible = "arm,cortex-a35" },
  144. { .compatible = "arm,cortex-a53" },
  145. { .compatible = "arm,cortex-a72" },
  146. { }
  147. };
  148. static ulong imx9_get_cpu_rate(struct udevice *dev)
  149. {
  150. struct cpu_imx_plat *plat = dev_get_plat(dev);
  151. ulong rate;
  152. int ret;
  153. ret = sc_pm_get_clock_rate(-1, plat->cpu_rsrc, SC_PM_CLK_CPU,
  154. (sc_pm_clock_rate_t *)&rate);
  155. if (ret) {
  156. printf("Could not read CPU frequency: %d\n", ret);
  157. return 0;
  158. }
  159. return rate;
  160. }
  161. static int imx9_cpu_probe(struct udevice *dev)
  162. {
  163. struct cpu_imx_plat *plat = dev_get_plat(dev);
  164. u32 cpurev;
  165. set_core_data(dev);
  166. cpurev = get_cpu_rev();
  167. plat->cpurev = cpurev;
  168. plat->rev = get_imx9_rev(cpurev & 0xFFF);
  169. plat->type = get_imx9_type((cpurev & 0xFF000) >> 12);
  170. plat->freq_mhz = imx9_get_cpu_rate(dev) / 1000000;
  171. plat->mpidr = dev_read_addr(dev);
  172. if (plat->mpidr == FDT_ADDR_T_NONE) {
  173. printf("%s: Failed to get CPU reg property\n", __func__);
  174. return -EINVAL;
  175. }
  176. return 0;
  177. }
  178. U_BOOT_DRIVER(cpu_imx9_drv) = {
  179. .name = "imx9x_cpu",
  180. .id = UCLASS_CPU,
  181. .of_match = cpu_imx9_ids,
  182. .ops = &cpu_imx9_ops,
  183. .probe = imx9_cpu_probe,
  184. .plat_auto = sizeof(struct cpu_imx_plat),
  185. .flags = DM_FLAG_PRE_RELOC,
  186. };