common.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright © 2014 NVIDIA Corporation
  4. * Copyright © 2015 Broadcom Corporation
  5. */
  6. #include <linux/io.h>
  7. #include <linux/of.h>
  8. #include <linux/of_address.h>
  9. #include <linux/slab.h>
  10. #include <linux/soc/brcmstb/brcmstb.h>
  11. #include <linux/sys_soc.h>
  12. #include <soc/brcmstb/common.h>
  13. static u32 family_id;
  14. static u32 product_id;
  15. static const struct of_device_id brcmstb_machine_match[] = {
  16. { .compatible = "brcm,brcmstb", },
  17. { }
  18. };
  19. bool soc_is_brcmstb(void)
  20. {
  21. const struct of_device_id *match;
  22. struct device_node *root;
  23. root = of_find_node_by_path("/");
  24. if (!root)
  25. return false;
  26. match = of_match_node(brcmstb_machine_match, root);
  27. of_node_put(root);
  28. return match != NULL;
  29. }
  30. u32 brcmstb_get_family_id(void)
  31. {
  32. return family_id;
  33. }
  34. EXPORT_SYMBOL(brcmstb_get_family_id);
  35. u32 brcmstb_get_product_id(void)
  36. {
  37. return product_id;
  38. }
  39. EXPORT_SYMBOL(brcmstb_get_product_id);
  40. static const struct of_device_id sun_top_ctrl_match[] = {
  41. { .compatible = "brcm,bcm7125-sun-top-ctrl", },
  42. { .compatible = "brcm,bcm7346-sun-top-ctrl", },
  43. { .compatible = "brcm,bcm7358-sun-top-ctrl", },
  44. { .compatible = "brcm,bcm7360-sun-top-ctrl", },
  45. { .compatible = "brcm,bcm7362-sun-top-ctrl", },
  46. { .compatible = "brcm,bcm7420-sun-top-ctrl", },
  47. { .compatible = "brcm,bcm7425-sun-top-ctrl", },
  48. { .compatible = "brcm,bcm7429-sun-top-ctrl", },
  49. { .compatible = "brcm,bcm7435-sun-top-ctrl", },
  50. { .compatible = "brcm,brcmstb-sun-top-ctrl", },
  51. { }
  52. };
  53. static int __init brcmstb_soc_device_early_init(void)
  54. {
  55. struct device_node *sun_top_ctrl;
  56. void __iomem *sun_top_ctrl_base;
  57. int ret = 0;
  58. /* We could be on a multi-platform kernel, don't make this fatal but
  59. * bail out early
  60. */
  61. sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
  62. if (!sun_top_ctrl)
  63. return ret;
  64. sun_top_ctrl_base = of_iomap(sun_top_ctrl, 0);
  65. if (!sun_top_ctrl_base) {
  66. ret = -ENODEV;
  67. goto out;
  68. }
  69. family_id = readl(sun_top_ctrl_base);
  70. product_id = readl(sun_top_ctrl_base + 0x4);
  71. iounmap(sun_top_ctrl_base);
  72. out:
  73. of_node_put(sun_top_ctrl);
  74. return ret;
  75. }
  76. early_initcall(brcmstb_soc_device_early_init);
  77. static int __init brcmstb_soc_device_init(void)
  78. {
  79. struct soc_device_attribute *soc_dev_attr;
  80. struct device_node *sun_top_ctrl;
  81. struct soc_device *soc_dev;
  82. int ret = 0;
  83. /* We could be on a multi-platform kernel, don't make this fatal but
  84. * bail out early
  85. */
  86. sun_top_ctrl = of_find_matching_node(NULL, sun_top_ctrl_match);
  87. if (!sun_top_ctrl)
  88. return ret;
  89. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  90. if (!soc_dev_attr) {
  91. ret = -ENOMEM;
  92. goto out;
  93. }
  94. soc_dev_attr->family = kasprintf(GFP_KERNEL, "%x",
  95. family_id >> 28 ?
  96. family_id >> 16 : family_id >> 8);
  97. soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%x",
  98. product_id >> 28 ?
  99. product_id >> 16 : product_id >> 8);
  100. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%c%d",
  101. ((product_id & 0xf0) >> 4) + 'A',
  102. product_id & 0xf);
  103. soc_dev = soc_device_register(soc_dev_attr);
  104. if (IS_ERR(soc_dev)) {
  105. kfree(soc_dev_attr->family);
  106. kfree(soc_dev_attr->soc_id);
  107. kfree(soc_dev_attr->revision);
  108. kfree(soc_dev_attr);
  109. ret = -ENOMEM;
  110. }
  111. out:
  112. of_node_put(sun_top_ctrl);
  113. return ret;
  114. }
  115. arch_initcall(brcmstb_soc_device_init);