common.c 3.5 KB

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