soc.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /*
  2. * Copyright (C) 2015 Atmel
  3. *
  4. * Alexandre Belloni <alexandre.belloni@free-electrons.com
  5. * Boris Brezillon <boris.brezillon@free-electrons.com
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. *
  11. */
  12. #define pr_fmt(fmt) "AT91: " fmt
  13. #include <linux/io.h>
  14. #include <linux/of.h>
  15. #include <linux/of_address.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/slab.h>
  18. #include <linux/sys_soc.h>
  19. #include "soc.h"
  20. #define AT91_DBGU_CIDR 0x40
  21. #define AT91_DBGU_EXID 0x44
  22. #define AT91_CHIPID_CIDR 0x00
  23. #define AT91_CHIPID_EXID 0x04
  24. #define AT91_CIDR_VERSION(x) ((x) & 0x1f)
  25. #define AT91_CIDR_EXT BIT(31)
  26. #define AT91_CIDR_MATCH_MASK 0x7fffffe0
  27. static int __init at91_get_cidr_exid_from_dbgu(u32 *cidr, u32 *exid)
  28. {
  29. struct device_node *np;
  30. void __iomem *regs;
  31. np = of_find_compatible_node(NULL, NULL, "atmel,at91rm9200-dbgu");
  32. if (!np)
  33. np = of_find_compatible_node(NULL, NULL,
  34. "atmel,at91sam9260-dbgu");
  35. if (!np)
  36. return -ENODEV;
  37. regs = of_iomap(np, 0);
  38. of_node_put(np);
  39. if (!regs) {
  40. pr_warn("Could not map DBGU iomem range");
  41. return -ENXIO;
  42. }
  43. *cidr = readl(regs + AT91_DBGU_CIDR);
  44. *exid = readl(regs + AT91_DBGU_EXID);
  45. iounmap(regs);
  46. return 0;
  47. }
  48. static int __init at91_get_cidr_exid_from_chipid(u32 *cidr, u32 *exid)
  49. {
  50. struct device_node *np;
  51. void __iomem *regs;
  52. np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-chipid");
  53. if (!np)
  54. return -ENODEV;
  55. regs = of_iomap(np, 0);
  56. of_node_put(np);
  57. if (!regs) {
  58. pr_warn("Could not map DBGU iomem range");
  59. return -ENXIO;
  60. }
  61. *cidr = readl(regs + AT91_CHIPID_CIDR);
  62. *exid = readl(regs + AT91_CHIPID_EXID);
  63. iounmap(regs);
  64. return 0;
  65. }
  66. struct soc_device * __init at91_soc_init(const struct at91_soc *socs)
  67. {
  68. struct soc_device_attribute *soc_dev_attr;
  69. const struct at91_soc *soc;
  70. struct soc_device *soc_dev;
  71. u32 cidr, exid;
  72. int ret;
  73. /*
  74. * With SAMA5D2 and later SoCs, CIDR and EXID registers are no more
  75. * in the dbgu device but in the chipid device whose purpose is only
  76. * to expose these two registers.
  77. */
  78. ret = at91_get_cidr_exid_from_dbgu(&cidr, &exid);
  79. if (ret)
  80. ret = at91_get_cidr_exid_from_chipid(&cidr, &exid);
  81. if (ret) {
  82. if (ret == -ENODEV)
  83. pr_warn("Could not find identification node");
  84. return NULL;
  85. }
  86. for (soc = socs; soc->name; soc++) {
  87. if (soc->cidr_match != (cidr & AT91_CIDR_MATCH_MASK))
  88. continue;
  89. if (!(cidr & AT91_CIDR_EXT) || soc->exid_match == exid)
  90. break;
  91. }
  92. if (!soc->name) {
  93. pr_warn("Could not find matching SoC description\n");
  94. return NULL;
  95. }
  96. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  97. if (!soc_dev_attr)
  98. return NULL;
  99. soc_dev_attr->family = soc->family;
  100. soc_dev_attr->soc_id = soc->name;
  101. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%X",
  102. AT91_CIDR_VERSION(cidr));
  103. soc_dev = soc_device_register(soc_dev_attr);
  104. if (IS_ERR(soc_dev)) {
  105. kfree(soc_dev_attr->revision);
  106. kfree(soc_dev_attr);
  107. pr_warn("Could not register SoC device\n");
  108. return NULL;
  109. }
  110. if (soc->family)
  111. pr_info("Detected SoC family: %s\n", soc->family);
  112. pr_info("Detected SoC: %s, revision %X\n", soc->name,
  113. AT91_CIDR_VERSION(cidr));
  114. return soc_dev;
  115. }