soc-imx8.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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/of.h>
  12. #define REV_B1 0x21
  13. #define IMX8MQ_SW_INFO_B1 0x40
  14. #define IMX8MQ_SW_MAGIC_B1 0xff0055aa
  15. #define OCOTP_UID_LOW 0x410
  16. #define OCOTP_UID_HIGH 0x420
  17. /* Same as ANADIG_DIGPROG_IMX7D */
  18. #define ANADIG_DIGPROG_IMX8MM 0x800
  19. struct imx8_soc_data {
  20. char *name;
  21. u32 (*soc_revision)(void);
  22. };
  23. static u64 soc_uid;
  24. static ssize_t soc_uid_show(struct device *dev,
  25. struct device_attribute *attr, char *buf)
  26. {
  27. return sprintf(buf, "%016llX\n", soc_uid);
  28. }
  29. static DEVICE_ATTR_RO(soc_uid);
  30. static u32 __init imx8mq_soc_revision(void)
  31. {
  32. struct device_node *np;
  33. void __iomem *ocotp_base;
  34. u32 magic;
  35. u32 rev = 0;
  36. np = of_find_compatible_node(NULL, NULL, "fsl,imx8mq-ocotp");
  37. if (!np)
  38. goto out;
  39. ocotp_base = of_iomap(np, 0);
  40. WARN_ON(!ocotp_base);
  41. magic = readl_relaxed(ocotp_base + IMX8MQ_SW_INFO_B1);
  42. if (magic == IMX8MQ_SW_MAGIC_B1)
  43. rev = REV_B1;
  44. soc_uid = readl_relaxed(ocotp_base + OCOTP_UID_HIGH);
  45. soc_uid <<= 32;
  46. soc_uid |= readl_relaxed(ocotp_base + OCOTP_UID_LOW);
  47. iounmap(ocotp_base);
  48. out:
  49. of_node_put(np);
  50. return rev;
  51. }
  52. static void __init imx8mm_soc_uid(void)
  53. {
  54. void __iomem *ocotp_base;
  55. struct device_node *np;
  56. np = of_find_compatible_node(NULL, NULL, "fsl,imx8mm-ocotp");
  57. if (!np)
  58. return;
  59. ocotp_base = of_iomap(np, 0);
  60. WARN_ON(!ocotp_base);
  61. soc_uid = readl_relaxed(ocotp_base + OCOTP_UID_HIGH);
  62. soc_uid <<= 32;
  63. soc_uid |= readl_relaxed(ocotp_base + OCOTP_UID_LOW);
  64. iounmap(ocotp_base);
  65. of_node_put(np);
  66. }
  67. static u32 __init imx8mm_soc_revision(void)
  68. {
  69. struct device_node *np;
  70. void __iomem *anatop_base;
  71. u32 rev;
  72. np = of_find_compatible_node(NULL, NULL, "fsl,imx8mm-anatop");
  73. if (!np)
  74. return 0;
  75. anatop_base = of_iomap(np, 0);
  76. WARN_ON(!anatop_base);
  77. rev = readl_relaxed(anatop_base + ANADIG_DIGPROG_IMX8MM);
  78. iounmap(anatop_base);
  79. of_node_put(np);
  80. imx8mm_soc_uid();
  81. return rev;
  82. }
  83. static const struct imx8_soc_data imx8mq_soc_data = {
  84. .name = "i.MX8MQ",
  85. .soc_revision = imx8mq_soc_revision,
  86. };
  87. static const struct imx8_soc_data imx8mm_soc_data = {
  88. .name = "i.MX8MM",
  89. .soc_revision = imx8mm_soc_revision,
  90. };
  91. static const struct imx8_soc_data imx8mn_soc_data = {
  92. .name = "i.MX8MN",
  93. .soc_revision = imx8mm_soc_revision,
  94. };
  95. static const struct of_device_id imx8_soc_match[] = {
  96. { .compatible = "fsl,imx8mq", .data = &imx8mq_soc_data, },
  97. { .compatible = "fsl,imx8mm", .data = &imx8mm_soc_data, },
  98. { .compatible = "fsl,imx8mn", .data = &imx8mn_soc_data, },
  99. { }
  100. };
  101. #define imx8_revision(soc_rev) \
  102. soc_rev ? \
  103. kasprintf(GFP_KERNEL, "%d.%d", (soc_rev >> 4) & 0xf, soc_rev & 0xf) : \
  104. "unknown"
  105. static int __init imx8_soc_init(void)
  106. {
  107. struct soc_device_attribute *soc_dev_attr;
  108. struct soc_device *soc_dev;
  109. const struct of_device_id *id;
  110. u32 soc_rev = 0;
  111. const struct imx8_soc_data *data;
  112. int ret;
  113. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  114. if (!soc_dev_attr)
  115. return -ENOMEM;
  116. soc_dev_attr->family = "Freescale i.MX";
  117. ret = of_property_read_string(of_root, "model", &soc_dev_attr->machine);
  118. if (ret)
  119. goto free_soc;
  120. id = of_match_node(imx8_soc_match, of_root);
  121. if (!id) {
  122. ret = -ENODEV;
  123. goto free_soc;
  124. }
  125. data = id->data;
  126. if (data) {
  127. soc_dev_attr->soc_id = data->name;
  128. if (data->soc_revision)
  129. soc_rev = data->soc_revision();
  130. }
  131. soc_dev_attr->revision = imx8_revision(soc_rev);
  132. if (!soc_dev_attr->revision) {
  133. ret = -ENOMEM;
  134. goto free_soc;
  135. }
  136. soc_dev = soc_device_register(soc_dev_attr);
  137. if (IS_ERR(soc_dev)) {
  138. ret = PTR_ERR(soc_dev);
  139. goto free_rev;
  140. }
  141. ret = device_create_file(soc_device_to_device(soc_dev),
  142. &dev_attr_soc_uid);
  143. if (ret)
  144. goto free_rev;
  145. if (IS_ENABLED(CONFIG_ARM_IMX_CPUFREQ_DT))
  146. platform_device_register_simple("imx-cpufreq-dt", -1, NULL, 0);
  147. return 0;
  148. free_rev:
  149. if (strcmp(soc_dev_attr->revision, "unknown"))
  150. kfree(soc_dev_attr->revision);
  151. free_soc:
  152. kfree(soc_dev_attr);
  153. return ret;
  154. }
  155. device_initcall(imx8_soc_init);