soc-imx.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright 2020 NXP
  4. */
  5. #include <linux/mfd/syscon.h>
  6. #include <linux/of.h>
  7. #include <linux/of_address.h>
  8. #include <linux/regmap.h>
  9. #include <linux/slab.h>
  10. #include <linux/sys_soc.h>
  11. #include <soc/imx/cpu.h>
  12. #include <soc/imx/revision.h>
  13. #define OCOTP_UID_H 0x420
  14. #define OCOTP_UID_L 0x410
  15. #define OCOTP_ULP_UID_1 0x4b0
  16. #define OCOTP_ULP_UID_2 0x4c0
  17. #define OCOTP_ULP_UID_3 0x4d0
  18. #define OCOTP_ULP_UID_4 0x4e0
  19. static int __init imx_soc_device_init(void)
  20. {
  21. struct soc_device_attribute *soc_dev_attr;
  22. const char *ocotp_compat = NULL;
  23. struct soc_device *soc_dev;
  24. struct device_node *root;
  25. struct regmap *ocotp = NULL;
  26. const char *soc_id;
  27. u64 soc_uid = 0;
  28. u32 val;
  29. int ret;
  30. /* Return early if this is running on devices with different SoCs */
  31. if (!__mxc_cpu_type)
  32. return 0;
  33. if (of_machine_is_compatible("fsl,ls1021a"))
  34. return 0;
  35. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  36. if (!soc_dev_attr)
  37. return -ENOMEM;
  38. soc_dev_attr->family = "Freescale i.MX";
  39. root = of_find_node_by_path("/");
  40. ret = of_property_read_string(root, "model", &soc_dev_attr->machine);
  41. of_node_put(root);
  42. if (ret)
  43. goto free_soc;
  44. switch (__mxc_cpu_type) {
  45. case MXC_CPU_MX1:
  46. soc_id = "i.MX1";
  47. break;
  48. case MXC_CPU_MX21:
  49. soc_id = "i.MX21";
  50. break;
  51. case MXC_CPU_MX25:
  52. soc_id = "i.MX25";
  53. break;
  54. case MXC_CPU_MX27:
  55. soc_id = "i.MX27";
  56. break;
  57. case MXC_CPU_MX31:
  58. soc_id = "i.MX31";
  59. break;
  60. case MXC_CPU_MX35:
  61. soc_id = "i.MX35";
  62. break;
  63. case MXC_CPU_MX51:
  64. soc_id = "i.MX51";
  65. break;
  66. case MXC_CPU_MX53:
  67. soc_id = "i.MX53";
  68. break;
  69. case MXC_CPU_IMX6SL:
  70. ocotp_compat = "fsl,imx6sl-ocotp";
  71. soc_id = "i.MX6SL";
  72. break;
  73. case MXC_CPU_IMX6DL:
  74. ocotp_compat = "fsl,imx6q-ocotp";
  75. soc_id = "i.MX6DL";
  76. break;
  77. case MXC_CPU_IMX6SX:
  78. ocotp_compat = "fsl,imx6sx-ocotp";
  79. soc_id = "i.MX6SX";
  80. break;
  81. case MXC_CPU_IMX6Q:
  82. ocotp_compat = "fsl,imx6q-ocotp";
  83. soc_id = "i.MX6Q";
  84. break;
  85. case MXC_CPU_IMX6UL:
  86. ocotp_compat = "fsl,imx6ul-ocotp";
  87. soc_id = "i.MX6UL";
  88. break;
  89. case MXC_CPU_IMX6ULL:
  90. ocotp_compat = "fsl,imx6ull-ocotp";
  91. soc_id = "i.MX6ULL";
  92. break;
  93. case MXC_CPU_IMX6ULZ:
  94. ocotp_compat = "fsl,imx6ull-ocotp";
  95. soc_id = "i.MX6ULZ";
  96. break;
  97. case MXC_CPU_IMX6SLL:
  98. ocotp_compat = "fsl,imx6sll-ocotp";
  99. soc_id = "i.MX6SLL";
  100. break;
  101. case MXC_CPU_IMX7D:
  102. ocotp_compat = "fsl,imx7d-ocotp";
  103. soc_id = "i.MX7D";
  104. break;
  105. case MXC_CPU_IMX7ULP:
  106. ocotp_compat = "fsl,imx7ulp-ocotp";
  107. soc_id = "i.MX7ULP";
  108. break;
  109. case MXC_CPU_VF500:
  110. ocotp_compat = "fsl,vf610-ocotp";
  111. soc_id = "VF500";
  112. break;
  113. case MXC_CPU_VF510:
  114. ocotp_compat = "fsl,vf610-ocotp";
  115. soc_id = "VF510";
  116. break;
  117. case MXC_CPU_VF600:
  118. ocotp_compat = "fsl,vf610-ocotp";
  119. soc_id = "VF600";
  120. break;
  121. case MXC_CPU_VF610:
  122. ocotp_compat = "fsl,vf610-ocotp";
  123. soc_id = "VF610";
  124. break;
  125. default:
  126. soc_id = "Unknown";
  127. }
  128. soc_dev_attr->soc_id = soc_id;
  129. if (ocotp_compat) {
  130. ocotp = syscon_regmap_lookup_by_compatible(ocotp_compat);
  131. if (IS_ERR(ocotp))
  132. pr_err("%s: failed to find %s regmap!\n", __func__, ocotp_compat);
  133. }
  134. if (!IS_ERR_OR_NULL(ocotp)) {
  135. if (__mxc_cpu_type == MXC_CPU_IMX7ULP) {
  136. regmap_read(ocotp, OCOTP_ULP_UID_4, &val);
  137. soc_uid = val & 0xffff;
  138. regmap_read(ocotp, OCOTP_ULP_UID_3, &val);
  139. soc_uid <<= 16;
  140. soc_uid |= val & 0xffff;
  141. regmap_read(ocotp, OCOTP_ULP_UID_2, &val);
  142. soc_uid <<= 16;
  143. soc_uid |= val & 0xffff;
  144. regmap_read(ocotp, OCOTP_ULP_UID_1, &val);
  145. soc_uid <<= 16;
  146. soc_uid |= val & 0xffff;
  147. } else {
  148. regmap_read(ocotp, OCOTP_UID_H, &val);
  149. soc_uid = val;
  150. regmap_read(ocotp, OCOTP_UID_L, &val);
  151. soc_uid <<= 32;
  152. soc_uid |= val;
  153. }
  154. }
  155. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%d.%d",
  156. (imx_get_soc_revision() >> 4) & 0xf,
  157. imx_get_soc_revision() & 0xf);
  158. if (!soc_dev_attr->revision) {
  159. ret = -ENOMEM;
  160. goto free_soc;
  161. }
  162. soc_dev_attr->serial_number = kasprintf(GFP_KERNEL, "%016llX", soc_uid);
  163. if (!soc_dev_attr->serial_number) {
  164. ret = -ENOMEM;
  165. goto free_rev;
  166. }
  167. soc_dev = soc_device_register(soc_dev_attr);
  168. if (IS_ERR(soc_dev)) {
  169. ret = PTR_ERR(soc_dev);
  170. goto free_serial_number;
  171. }
  172. return 0;
  173. free_serial_number:
  174. kfree(soc_dev_attr->serial_number);
  175. free_rev:
  176. kfree(soc_dev_attr->revision);
  177. free_soc:
  178. kfree(soc_dev_attr);
  179. return ret;
  180. }
  181. device_initcall(imx_soc_device_init);