meson-gx-socinfo.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /*
  2. * Copyright (c) 2017 BayLibre, SAS
  3. * Author: Neil Armstrong <[email protected]>
  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. { "TXLX", 0x24 },
  33. { "AXG", 0x25 },
  34. { "GXLX", 0x26 },
  35. { "TXHD", 0x27 },
  36. { "G12A", 0x28 },
  37. { "G12B", 0x29 },
  38. { "SM1", 0x2b },
  39. { "A1", 0x2c },
  40. };
  41. static const struct meson_gx_package_id {
  42. const char *name;
  43. unsigned int major_id;
  44. unsigned int pack_id;
  45. unsigned int pack_mask;
  46. } soc_packages[] = {
  47. { "S905", 0x1f, 0, 0x20 }, /* pack_id != 0x20 */
  48. { "S905H", 0x1f, 0x3, 0xf }, /* pack_id & 0xf == 0x3 */
  49. { "S905M", 0x1f, 0x20, 0xf0 }, /* pack_id == 0x20 */
  50. { "S905D", 0x21, 0, 0xf0 },
  51. { "S905X", 0x21, 0x80, 0xf0 },
  52. { "S905W", 0x21, 0xa0, 0xf0 },
  53. { "S905L", 0x21, 0xc0, 0xf0 },
  54. { "S905M2", 0x21, 0xe0, 0xf0 },
  55. { "S805X", 0x21, 0x30, 0xf0 },
  56. { "S805Y", 0x21, 0xb0, 0xf0 },
  57. { "S912", 0x22, 0, 0x0 }, /* Only S912 is known for GXM */
  58. { "962X", 0x24, 0x10, 0xf0 },
  59. { "962E", 0x24, 0x20, 0xf0 },
  60. { "A113X", 0x25, 0x37, 0xff },
  61. { "A113D", 0x25, 0x22, 0xff },
  62. { "S905D2", 0x28, 0x10, 0xf0 },
  63. { "S905Y2", 0x28, 0x30, 0xf0 },
  64. { "S905X2", 0x28, 0x40, 0xf0 },
  65. { "A311D", 0x29, 0x10, 0xf0 },
  66. { "S922X", 0x29, 0x40, 0xf0 },
  67. { "S905D3", 0x2b, 0x4, 0xf5 },
  68. { "S905X3", 0x2b, 0x5, 0xf5 },
  69. { "S905X3", 0x2b, 0x10, 0x3f },
  70. { "S905D3", 0x2b, 0x30, 0x3f },
  71. { "A113L", 0x2c, 0x0, 0xf8 },
  72. };
  73. static inline unsigned int socinfo_to_major(u32 socinfo)
  74. {
  75. return FIELD_GET(SOCINFO_MAJOR, socinfo);
  76. }
  77. static inline unsigned int socinfo_to_minor(u32 socinfo)
  78. {
  79. return FIELD_GET(SOCINFO_MINOR, socinfo);
  80. }
  81. static inline unsigned int socinfo_to_pack(u32 socinfo)
  82. {
  83. return FIELD_GET(SOCINFO_PACK, socinfo);
  84. }
  85. static inline unsigned int socinfo_to_misc(u32 socinfo)
  86. {
  87. return FIELD_GET(SOCINFO_MISC, socinfo);
  88. }
  89. static const char *socinfo_to_package_id(u32 socinfo)
  90. {
  91. unsigned int pack = socinfo_to_pack(socinfo);
  92. unsigned int major = socinfo_to_major(socinfo);
  93. int i;
  94. for (i = 0 ; i < ARRAY_SIZE(soc_packages) ; ++i) {
  95. if (soc_packages[i].major_id == major &&
  96. soc_packages[i].pack_id ==
  97. (pack & soc_packages[i].pack_mask))
  98. return soc_packages[i].name;
  99. }
  100. return "Unknown";
  101. }
  102. static const char *socinfo_to_soc_id(u32 socinfo)
  103. {
  104. unsigned int id = socinfo_to_major(socinfo);
  105. int i;
  106. for (i = 0 ; i < ARRAY_SIZE(soc_ids) ; ++i) {
  107. if (soc_ids[i].id == id)
  108. return soc_ids[i].name;
  109. }
  110. return "Unknown";
  111. }
  112. static int __init meson_gx_socinfo_init(void)
  113. {
  114. struct soc_device_attribute *soc_dev_attr;
  115. struct soc_device *soc_dev;
  116. struct device_node *np;
  117. struct regmap *regmap;
  118. unsigned int socinfo;
  119. struct device *dev;
  120. int ret;
  121. /* look up for chipid node */
  122. np = of_find_compatible_node(NULL, NULL, "amlogic,meson-gx-ao-secure");
  123. if (!np)
  124. return -ENODEV;
  125. /* check if interface is enabled */
  126. if (!of_device_is_available(np)) {
  127. of_node_put(np);
  128. return -ENODEV;
  129. }
  130. /* check if chip-id is available */
  131. if (!of_property_read_bool(np, "amlogic,has-chip-id")) {
  132. of_node_put(np);
  133. return -ENODEV;
  134. }
  135. /* node should be a syscon */
  136. regmap = syscon_node_to_regmap(np);
  137. of_node_put(np);
  138. if (IS_ERR(regmap)) {
  139. pr_err("%s: failed to get regmap\n", __func__);
  140. return -ENODEV;
  141. }
  142. ret = regmap_read(regmap, AO_SEC_SOCINFO_OFFSET, &socinfo);
  143. if (ret < 0)
  144. return ret;
  145. if (!socinfo) {
  146. pr_err("%s: invalid chipid value\n", __func__);
  147. return -EINVAL;
  148. }
  149. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  150. if (!soc_dev_attr)
  151. return -ENODEV;
  152. soc_dev_attr->family = "Amlogic Meson";
  153. np = of_find_node_by_path("/");
  154. of_property_read_string(np, "model", &soc_dev_attr->machine);
  155. of_node_put(np);
  156. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%x:%x - %x:%x",
  157. socinfo_to_major(socinfo),
  158. socinfo_to_minor(socinfo),
  159. socinfo_to_pack(socinfo),
  160. socinfo_to_misc(socinfo));
  161. soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%s (%s)",
  162. socinfo_to_soc_id(socinfo),
  163. socinfo_to_package_id(socinfo));
  164. soc_dev = soc_device_register(soc_dev_attr);
  165. if (IS_ERR(soc_dev)) {
  166. kfree(soc_dev_attr->revision);
  167. kfree_const(soc_dev_attr->soc_id);
  168. kfree(soc_dev_attr);
  169. return PTR_ERR(soc_dev);
  170. }
  171. dev = soc_device_to_device(soc_dev);
  172. dev_info(dev, "Amlogic Meson %s Revision %x:%x (%x:%x) Detected\n",
  173. soc_dev_attr->soc_id,
  174. socinfo_to_major(socinfo),
  175. socinfo_to_minor(socinfo),
  176. socinfo_to_pack(socinfo),
  177. socinfo_to_misc(socinfo));
  178. return 0;
  179. }
  180. device_initcall(meson_gx_socinfo_init);