soc.c 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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_CIDR_VERSION(x) ((x) & 0x1f)
  22. #define AT91_DBGU_CIDR_EXT BIT(31)
  23. #define AT91_DBGU_CIDR_MATCH_MASK 0x7fffffe0
  24. #define AT91_DBGU_EXID 0x44
  25. struct soc_device * __init at91_soc_init(const struct at91_soc *socs)
  26. {
  27. struct soc_device_attribute *soc_dev_attr;
  28. const struct at91_soc *soc;
  29. struct soc_device *soc_dev;
  30. struct device_node *np;
  31. void __iomem *regs;
  32. u32 cidr, exid;
  33. np = of_find_compatible_node(NULL, NULL, "atmel,at91rm9200-dbgu");
  34. if (!np)
  35. np = of_find_compatible_node(NULL, NULL,
  36. "atmel,at91sam9260-dbgu");
  37. if (!np) {
  38. pr_warn("Could not find DBGU node");
  39. return NULL;
  40. }
  41. regs = of_iomap(np, 0);
  42. of_node_put(np);
  43. if (!regs) {
  44. pr_warn("Could not map DBGU iomem range");
  45. return NULL;
  46. }
  47. cidr = readl(regs + AT91_DBGU_CIDR);
  48. exid = readl(regs + AT91_DBGU_EXID);
  49. iounmap(regs);
  50. for (soc = socs; soc->name; soc++) {
  51. if (soc->cidr_match != (cidr & AT91_DBGU_CIDR_MATCH_MASK))
  52. continue;
  53. if (!(cidr & AT91_DBGU_CIDR_EXT) || soc->exid_match == exid)
  54. break;
  55. }
  56. if (!soc->name) {
  57. pr_warn("Could not find matching SoC description\n");
  58. return NULL;
  59. }
  60. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  61. if (!soc_dev_attr)
  62. return NULL;
  63. soc_dev_attr->family = soc->family;
  64. soc_dev_attr->soc_id = soc->name;
  65. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%X",
  66. AT91_DBGU_CIDR_VERSION(cidr));
  67. soc_dev = soc_device_register(soc_dev_attr);
  68. if (IS_ERR(soc_dev)) {
  69. kfree(soc_dev_attr->revision);
  70. kfree(soc_dev_attr);
  71. pr_warn("Could not register SoC device\n");
  72. return NULL;
  73. }
  74. if (soc->family)
  75. pr_info("Detected SoC family: %s\n", soc->family);
  76. pr_info("Detected SoC: %s, revision %X\n", soc->name,
  77. AT91_DBGU_CIDR_VERSION(cidr));
  78. return soc_dev;
  79. }