meson-gx-socinfo.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2017 BayLibre, SAS
  3. * Author: Neil Armstrong <narmstrong@baylibre.com>
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. */
  7. #include <linux/io.h>
  8. #include <linux/of.h>
  9. #include <linux/of_address.h>
  10. #include <linux/of_platform.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/slab.h>
  13. #include <linux/sys_soc.h>
  14. #include <linux/bitfield.h>
  15. #include <linux/regmap.h>
  16. #include <linux/mfd/syscon.h>
  17. #define AO_SEC_SD_CFG8 0xe0
  18. #define AO_SEC_SOCINFO_OFFSET AO_SEC_SD_CFG8
  19. #define SOCINFO_MAJOR GENMASK(31, 24)
  20. #define SOCINFO_PACK GENMASK(23, 16)
  21. #define SOCINFO_MINOR GENMASK(15, 8)
  22. #define SOCINFO_MISC GENMASK(7, 0)
  23. static const struct meson_gx_soc_id {
  24. const char *name;
  25. unsigned int id;
  26. } soc_ids[] = {
  27. { "GXBB", 0x1f },
  28. { "GXTVBB", 0x20 },
  29. { "GXL", 0x21 },
  30. { "GXM", 0x22 },
  31. { "TXL", 0x23 },
  32. };
  33. static const struct meson_gx_package_id {
  34. const char *name;
  35. unsigned int major_id;
  36. unsigned int pack_id;
  37. } soc_packages[] = {
  38. { "S905", 0x1f, 0 },
  39. { "S905M", 0x1f, 0x20 },
  40. { "S905D", 0x21, 0 },
  41. { "S905X", 0x21, 0x80 },
  42. { "S905L", 0x21, 0xc0 },
  43. { "S905M2", 0x21, 0xe0 },
  44. { "S912", 0x22, 0 },
  45. };
  46. static inline unsigned int socinfo_to_major(u32 socinfo)
  47. {
  48. return FIELD_GET(SOCINFO_MAJOR, socinfo);
  49. }
  50. static inline unsigned int socinfo_to_minor(u32 socinfo)
  51. {
  52. return FIELD_GET(SOCINFO_MINOR, socinfo);
  53. }
  54. static inline unsigned int socinfo_to_pack(u32 socinfo)
  55. {
  56. return FIELD_GET(SOCINFO_PACK, socinfo);
  57. }
  58. static inline unsigned int socinfo_to_misc(u32 socinfo)
  59. {
  60. return FIELD_GET(SOCINFO_MISC, socinfo);
  61. }
  62. static const char *socinfo_to_package_id(u32 socinfo)
  63. {
  64. unsigned int pack = socinfo_to_pack(socinfo) & 0xf0;
  65. unsigned int major = socinfo_to_major(socinfo);
  66. int i;
  67. for (i = 0 ; i < ARRAY_SIZE(soc_packages) ; ++i) {
  68. if (soc_packages[i].major_id == major &&
  69. soc_packages[i].pack_id == pack)
  70. return soc_packages[i].name;
  71. }
  72. return "Unknown";
  73. }
  74. static const char *socinfo_to_soc_id(u32 socinfo)
  75. {
  76. unsigned int id = socinfo_to_major(socinfo);
  77. int i;
  78. for (i = 0 ; i < ARRAY_SIZE(soc_ids) ; ++i) {
  79. if (soc_ids[i].id == id)
  80. return soc_ids[i].name;
  81. }
  82. return "Unknown";
  83. }
  84. int __init meson_gx_socinfo_init(void)
  85. {
  86. struct soc_device_attribute *soc_dev_attr;
  87. struct soc_device *soc_dev;
  88. struct device_node *np;
  89. struct regmap *regmap;
  90. unsigned int socinfo;
  91. struct device *dev;
  92. int ret;
  93. /* look up for chipid node */
  94. np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gx-ao-secure");
  95. if (!np)
  96. return -ENODEV;
  97. /* check if interface is enabled */
  98. if (!of_device_is_available(np))
  99. return -ENODEV;
  100. /* check if chip-id is available */
  101. if (!of_property_read_bool(np, "amlogic,has-chip-id"))
  102. return -ENODEV;
  103. /* node should be a syscon */
  104. regmap = syscon_node_to_regmap(np);
  105. of_node_put(np);
  106. if (IS_ERR(regmap)) {
  107. pr_err("%s: failed to get regmap\n", __func__);
  108. return -ENODEV;
  109. }
  110. ret = regmap_read(regmap, AO_SEC_SOCINFO_OFFSET, &socinfo);
  111. if (ret < 0)
  112. return ret;
  113. if (!socinfo) {
  114. pr_err("%s: invalid chipid value\n", __func__);
  115. return -EINVAL;
  116. }
  117. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  118. if (!soc_dev_attr)
  119. return -ENODEV;
  120. soc_dev_attr->family = "Amlogic Meson";
  121. np = of_find_node_by_path("/");
  122. of_property_read_string(np, "model", &soc_dev_attr->machine);
  123. of_node_put(np);
  124. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x:%x - %x:%x",
  125. socinfo_to_major(socinfo),
  126. socinfo_to_minor(socinfo),
  127. socinfo_to_pack(socinfo),
  128. socinfo_to_misc(socinfo));
  129. soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s (%s)",
  130. socinfo_to_soc_id(socinfo),
  131. socinfo_to_package_id(socinfo));
  132. soc_dev = soc_device_register(soc_dev_attr);
  133. if (IS_ERR(soc_dev)) {
  134. kfree(soc_dev_attr->revision);
  135. kfree_const(soc_dev_attr->soc_id);
  136. kfree(soc_dev_attr);
  137. return PTR_ERR(soc_dev);
  138. }
  139. dev = soc_device_to_device(soc_dev);
  140. dev_info(dev, "Amlogic Meson %s Revision %x:%x (%x:%x) Detected\n",
  141. soc_dev_attr->soc_id,
  142. socinfo_to_major(socinfo),
  143. socinfo_to_minor(socinfo),
  144. socinfo_to_pack(socinfo),
  145. socinfo_to_misc(socinfo));
  146. return 0;
  147. }
  148. device_initcall(meson_gx_socinfo_init);