soc-imx8m.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2019 NXP.
  4. */
  5. #include <linux/init.h>
  6. #include <linux/io.h>
  7. #include <linux/of_address.h>
  8. #include <linux/slab.h>
  9. #include <linux/sys_soc.h>
  10. #include <linux/platform_device.h>
  11. #include <linux/arm-smccc.h>
  12. #include <linux/of.h>
  13. #include <linux/clk.h>
  14. #define REV_B1 0x21
  15. #define IMX8MQ_SW_INFO_B1 0x40
  16. #define IMX8MQ_SW_MAGIC_B1 0xff0055aa
  17. #define IMX_SIP_GET_SOC_INFO 0xc2000006
  18. #define OCOTP_UID_LOW 0x410
  19. #define OCOTP_UID_HIGH 0x420
  20. #define IMX8MP_OCOTP_UID_OFFSET 0x10
  21. /* Same as ANADIG_DIGPROG_IMX7D */
  22. #define ANADIG_DIGPROG_IMX8MM 0x800
  23. struct imx8_soc_data {
  24. char *name;
  25. u32 (*soc_revision)(void);
  26. };
  27. static u64 soc_uid;
  28. #ifdef CONFIG_HAVE_ARM_SMCCC
  29. static u32 imx8mq_soc_revision_from_atf(void)
  30. {
  31. struct arm_smccc_res res;
  32. arm_smccc_smc(IMX_SIP_GET_SOC_INFO, 0, 0, 0, 0, 0, 0, 0, &res);
  33. if (res.a0 == SMCCC_RET_NOT_SUPPORTED)
  34. return 0;
  35. else
  36. return res.a0 & 0xff;
  37. }
  38. #else
  39. static inline u32 imx8mq_soc_revision_from_atf(void) { return 0; };
  40. #endif
  41. static u32 __init imx8mq_soc_revision(void)
  42. {
  43. struct device_node *np;
  44. void __iomem *ocotp_base;
  45. u32 magic;
  46. u32 rev;
  47. struct clk *clk;
  48. np = of_find_compatible_node(NULL, NULL, "fsl,imx8mq-ocotp");
  49. if (!np)
  50. return 0;
  51. ocotp_base = of_iomap(np, 0);
  52. WARN_ON(!ocotp_base);
  53. clk = of_clk_get_by_name(np, NULL);
  54. if (IS_ERR(clk)) {
  55. WARN_ON(IS_ERR(clk));
  56. return 0;
  57. }
  58. clk_prepare_enable(clk);
  59. /*
  60. * SOC revision on older imx8mq is not available in fuses so query
  61. * the value from ATF instead.
  62. */
  63. rev = imx8mq_soc_revision_from_atf();
  64. if (!rev) {
  65. magic = readl_relaxed(ocotp_base + IMX8MQ_SW_INFO_B1);
  66. if (magic == IMX8MQ_SW_MAGIC_B1)
  67. rev = REV_B1;
  68. }
  69. soc_uid = readl_relaxed(ocotp_base + OCOTP_UID_HIGH);
  70. soc_uid <<= 32;
  71. soc_uid |= readl_relaxed(ocotp_base + OCOTP_UID_LOW);
  72. clk_disable_unprepare(clk);
  73. clk_put(clk);
  74. iounmap(ocotp_base);
  75. of_node_put(np);
  76. return rev;
  77. }
  78. static void __init imx8mm_soc_uid(void)
  79. {
  80. void __iomem *ocotp_base;
  81. struct device_node *np;
  82. struct clk *clk;
  83. u32 offset = of_machine_is_compatible("fsl,imx8mp") ?
  84. IMX8MP_OCOTP_UID_OFFSET : 0;
  85. np = of_find_compatible_node(NULL, NULL, "fsl,imx8mm-ocotp");
  86. if (!np)
  87. return;
  88. ocotp_base = of_iomap(np, 0);
  89. WARN_ON(!ocotp_base);
  90. clk = of_clk_get_by_name(np, NULL);
  91. if (IS_ERR(clk)) {
  92. WARN_ON(IS_ERR(clk));
  93. return;
  94. }
  95. clk_prepare_enable(clk);
  96. soc_uid = readl_relaxed(ocotp_base + OCOTP_UID_HIGH + offset);
  97. soc_uid <<= 32;
  98. soc_uid |= readl_relaxed(ocotp_base + OCOTP_UID_LOW + offset);
  99. clk_disable_unprepare(clk);
  100. clk_put(clk);
  101. iounmap(ocotp_base);
  102. of_node_put(np);
  103. }
  104. static u32 __init imx8mm_soc_revision(void)
  105. {
  106. struct device_node *np;
  107. void __iomem *anatop_base;
  108. u32 rev;
  109. np = of_find_compatible_node(NULL, NULL, "fsl,imx8mm-anatop");
  110. if (!np)
  111. return 0;
  112. anatop_base = of_iomap(np, 0);
  113. WARN_ON(!anatop_base);
  114. rev = readl_relaxed(anatop_base + ANADIG_DIGPROG_IMX8MM);
  115. iounmap(anatop_base);
  116. of_node_put(np);
  117. imx8mm_soc_uid();
  118. return rev;
  119. }
  120. static const struct imx8_soc_data imx8mq_soc_data = {
  121. .name = "i.MX8MQ",
  122. .soc_revision = imx8mq_soc_revision,
  123. };
  124. static const struct imx8_soc_data imx8mm_soc_data = {
  125. .name = "i.MX8MM",
  126. .soc_revision = imx8mm_soc_revision,
  127. };
  128. static const struct imx8_soc_data imx8mn_soc_data = {
  129. .name = "i.MX8MN",
  130. .soc_revision = imx8mm_soc_revision,
  131. };
  132. static const struct imx8_soc_data imx8mp_soc_data = {
  133. .name = "i.MX8MP",
  134. .soc_revision = imx8mm_soc_revision,
  135. };
  136. static __maybe_unused const struct of_device_id imx8_soc_match[] = {
  137. { .compatible = "fsl,imx8mq", .data = &imx8mq_soc_data, },
  138. { .compatible = "fsl,imx8mm", .data = &imx8mm_soc_data, },
  139. { .compatible = "fsl,imx8mn", .data = &imx8mn_soc_data, },
  140. { .compatible = "fsl,imx8mp", .data = &imx8mp_soc_data, },
  141. { }
  142. };
  143. #define imx8_revision(soc_rev) \
  144. soc_rev ? \
  145. kasprintf(GFP_KERNEL, "%d.%d", (soc_rev >> 4) & 0xf, soc_rev & 0xf) : \
  146. "unknown"
  147. static int __init imx8_soc_init(void)
  148. {
  149. struct soc_device_attribute *soc_dev_attr;
  150. struct soc_device *soc_dev;
  151. const struct of_device_id *id;
  152. u32 soc_rev = 0;
  153. const struct imx8_soc_data *data;
  154. int ret;
  155. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  156. if (!soc_dev_attr)
  157. return -ENOMEM;
  158. soc_dev_attr->family = "Freescale i.MX";
  159. ret = of_property_read_string(of_root, "model", &soc_dev_attr->machine);
  160. if (ret)
  161. goto free_soc;
  162. id = of_match_node(imx8_soc_match, of_root);
  163. if (!id) {
  164. ret = -ENODEV;
  165. goto free_soc;
  166. }
  167. data = id->data;
  168. if (data) {
  169. soc_dev_attr->soc_id = data->name;
  170. if (data->soc_revision)
  171. soc_rev = data->soc_revision();
  172. }
  173. soc_dev_attr->revision = imx8_revision(soc_rev);
  174. if (!soc_dev_attr->revision) {
  175. ret = -ENOMEM;
  176. goto free_soc;
  177. }
  178. soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid);
  179. if (!soc_dev_attr->serial_number) {
  180. ret = -ENOMEM;
  181. goto free_rev;
  182. }
  183. soc_dev = soc_device_register(soc_dev_attr);
  184. if (IS_ERR(soc_dev)) {
  185. ret = PTR_ERR(soc_dev);
  186. goto free_serial_number;
  187. }
  188. pr_info("SoC: %s revision %s\n", soc_dev_attr->soc_id,
  189. soc_dev_attr->revision);
  190. if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
  191. platform_device_register_simple("imx-cpufreq-dt", -1, NULL, 0);
  192. return 0;
  193. free_serial_number:
  194. kfree(soc_dev_attr->serial_number);
  195. free_rev:
  196. if (strcmp(soc_dev_attr->revision, "unknown"))
  197. kfree(soc_dev_attr->revision);
  198. free_soc:
  199. kfree(soc_dev_attr);
  200. return ret;
  201. }
  202. device_initcall(imx8_soc_init);