mvebu-soc-id.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * ID and revision information for mvebu SoCs
  4. *
  5. * Copyright (C) 2014 Marvell
  6. *
  7. * Gregory CLEMENT <[email protected]>
  8. *
  9. * All the mvebu SoCs have information related to their variant and
  10. * revision that can be read from the PCI control register. This is
  11. * done before the PCI initialization to avoid any conflict. Once the
  12. * ID and revision are retrieved, the mapping is freed.
  13. */
  14. #define pr_fmt(fmt) "mvebu-soc-id: " fmt
  15. #include <linux/clk.h>
  16. #include <linux/init.h>
  17. #include <linux/io.h>
  18. #include <linux/kernel.h>
  19. #include <linux/of.h>
  20. #include <linux/of_address.h>
  21. #include <linux/slab.h>
  22. #include <linux/sys_soc.h>
  23. #include "common.h"
  24. #include "mvebu-soc-id.h"
  25. #define PCIE_DEV_ID_OFF 0x0
  26. #define PCIE_DEV_REV_OFF 0x8
  27. #define SOC_ID_MASK 0xFFFF0000
  28. #define SOC_REV_MASK 0xFF
  29. static u32 soc_dev_id;
  30. static u32 soc_rev;
  31. static bool is_id_valid;
  32. static const struct of_device_id mvebu_pcie_of_match_table[] = {
  33. { .compatible = "marvell,armada-xp-pcie", },
  34. { .compatible = "marvell,armada-370-pcie", },
  35. { .compatible = "marvell,kirkwood-pcie" },
  36. {},
  37. };
  38. int mvebu_get_soc_id(u32 *dev, u32 *rev)
  39. {
  40. if (is_id_valid) {
  41. *dev = soc_dev_id;
  42. *rev = soc_rev;
  43. return 0;
  44. } else
  45. return -ENODEV;
  46. }
  47. static int __init get_soc_id_by_pci(void)
  48. {
  49. struct device_node *np;
  50. int ret = 0;
  51. void __iomem *pci_base;
  52. struct clk *clk;
  53. struct device_node *child;
  54. np = of_find_matching_node(NULL, mvebu_pcie_of_match_table);
  55. if (!np)
  56. return ret;
  57. /*
  58. * ID and revision are available from any port, so we
  59. * just pick the first one
  60. */
  61. child = of_get_next_child(np, NULL);
  62. if (child == NULL) {
  63. pr_err("cannot get pci node\n");
  64. ret = -ENOMEM;
  65. goto clk_err;
  66. }
  67. clk = of_clk_get_by_name(child, NULL);
  68. if (IS_ERR(clk)) {
  69. pr_err("cannot get clock\n");
  70. ret = -ENOMEM;
  71. goto clk_err;
  72. }
  73. ret = clk_prepare_enable(clk);
  74. if (ret) {
  75. pr_err("cannot enable clock\n");
  76. goto clk_err;
  77. }
  78. pci_base = of_iomap(child, 0);
  79. if (pci_base == NULL) {
  80. pr_err("cannot map registers\n");
  81. ret = -ENOMEM;
  82. goto res_ioremap;
  83. }
  84. /* SoC ID */
  85. soc_dev_id = readl(pci_base + PCIE_DEV_ID_OFF) >> 16;
  86. /* SoC revision */
  87. soc_rev = readl(pci_base + PCIE_DEV_REV_OFF) & SOC_REV_MASK;
  88. is_id_valid = true;
  89. pr_info("MVEBU SoC ID=0x%X, Rev=0x%X\n", soc_dev_id, soc_rev);
  90. iounmap(pci_base);
  91. res_ioremap:
  92. /*
  93. * If the PCIe unit is actually enabled and we have PCI
  94. * support in the kernel, we intentionally do not release the
  95. * reference to the clock. We want to keep it running since
  96. * the bootloader does some PCIe link configuration that the
  97. * kernel is for now unable to do, and gating the clock would
  98. * make us loose this precious configuration.
  99. */
  100. if (!of_device_is_available(child) || !IS_ENABLED(CONFIG_PCI_MVEBU)) {
  101. clk_disable_unprepare(clk);
  102. clk_put(clk);
  103. }
  104. clk_err:
  105. of_node_put(child);
  106. of_node_put(np);
  107. return ret;
  108. }
  109. static int __init mvebu_soc_id_init(void)
  110. {
  111. /*
  112. * First try to get the ID and the revision by the system
  113. * register and use PCI registers only if it is not possible
  114. */
  115. if (!mvebu_system_controller_get_soc_id(&soc_dev_id, &soc_rev)) {
  116. is_id_valid = true;
  117. pr_info("MVEBU SoC ID=0x%X, Rev=0x%X\n", soc_dev_id, soc_rev);
  118. return 0;
  119. }
  120. return get_soc_id_by_pci();
  121. }
  122. early_initcall(mvebu_soc_id_init);
  123. static int __init mvebu_soc_device(void)
  124. {
  125. struct soc_device_attribute *soc_dev_attr;
  126. struct soc_device *soc_dev;
  127. /* Also protects against running on non-mvebu systems */
  128. if (!is_id_valid)
  129. return 0;
  130. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  131. if (!soc_dev_attr)
  132. return -ENOMEM;
  133. soc_dev_attr->family = kasprintf(GFP_KERNEL, "Marvell");
  134. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%X", soc_rev);
  135. soc_dev_attr->soc_id = kasprintf(GFP_KERNEL, "%X", soc_dev_id);
  136. soc_dev = soc_device_register(soc_dev_attr);
  137. if (IS_ERR(soc_dev)) {
  138. kfree(soc_dev_attr->family);
  139. kfree(soc_dev_attr->revision);
  140. kfree(soc_dev_attr->soc_id);
  141. kfree(soc_dev_attr);
  142. }
  143. return 0;
  144. }
  145. postcore_initcall(mvebu_soc_device);