soc.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 const struct at91_soc __initconst socs[] = {
  28. #ifdef CONFIG_SOC_AT91RM9200
  29. AT91_SOC(AT91RM9200_CIDR_MATCH, 0, "at91rm9200 BGA", "at91rm9200"),
  30. #endif
  31. #ifdef CONFIG_SOC_AT91SAM9
  32. AT91_SOC(AT91SAM9260_CIDR_MATCH, 0, "at91sam9260", NULL),
  33. AT91_SOC(AT91SAM9261_CIDR_MATCH, 0, "at91sam9261", NULL),
  34. AT91_SOC(AT91SAM9263_CIDR_MATCH, 0, "at91sam9263", NULL),
  35. AT91_SOC(AT91SAM9G20_CIDR_MATCH, 0, "at91sam9g20", NULL),
  36. AT91_SOC(AT91SAM9RL64_CIDR_MATCH, 0, "at91sam9rl64", NULL),
  37. AT91_SOC(AT91SAM9G45_CIDR_MATCH, AT91SAM9M11_EXID_MATCH,
  38. "at91sam9m11", "at91sam9g45"),
  39. AT91_SOC(AT91SAM9G45_CIDR_MATCH, AT91SAM9M10_EXID_MATCH,
  40. "at91sam9m10", "at91sam9g45"),
  41. AT91_SOC(AT91SAM9G45_CIDR_MATCH, AT91SAM9G46_EXID_MATCH,
  42. "at91sam9g46", "at91sam9g45"),
  43. AT91_SOC(AT91SAM9G45_CIDR_MATCH, AT91SAM9G45_EXID_MATCH,
  44. "at91sam9g45", "at91sam9g45"),
  45. AT91_SOC(AT91SAM9X5_CIDR_MATCH, AT91SAM9G15_EXID_MATCH,
  46. "at91sam9g15", "at91sam9x5"),
  47. AT91_SOC(AT91SAM9X5_CIDR_MATCH, AT91SAM9G35_EXID_MATCH,
  48. "at91sam9g35", "at91sam9x5"),
  49. AT91_SOC(AT91SAM9X5_CIDR_MATCH, AT91SAM9X35_EXID_MATCH,
  50. "at91sam9x35", "at91sam9x5"),
  51. AT91_SOC(AT91SAM9X5_CIDR_MATCH, AT91SAM9G25_EXID_MATCH,
  52. "at91sam9g25", "at91sam9x5"),
  53. AT91_SOC(AT91SAM9X5_CIDR_MATCH, AT91SAM9X25_EXID_MATCH,
  54. "at91sam9x25", "at91sam9x5"),
  55. AT91_SOC(AT91SAM9N12_CIDR_MATCH, AT91SAM9CN12_EXID_MATCH,
  56. "at91sam9cn12", "at91sam9n12"),
  57. AT91_SOC(AT91SAM9N12_CIDR_MATCH, AT91SAM9N12_EXID_MATCH,
  58. "at91sam9n12", "at91sam9n12"),
  59. AT91_SOC(AT91SAM9N12_CIDR_MATCH, AT91SAM9CN11_EXID_MATCH,
  60. "at91sam9cn11", "at91sam9n12"),
  61. AT91_SOC(AT91SAM9XE128_CIDR_MATCH, 0, "at91sam9xe128", "at91sam9xe128"),
  62. AT91_SOC(AT91SAM9XE256_CIDR_MATCH, 0, "at91sam9xe256", "at91sam9xe256"),
  63. AT91_SOC(AT91SAM9XE512_CIDR_MATCH, 0, "at91sam9xe512", "at91sam9xe512"),
  64. #endif
  65. #ifdef CONFIG_SOC_SAMA5
  66. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D21CU_EXID_MATCH,
  67. "sama5d21", "sama5d2"),
  68. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D22CU_EXID_MATCH,
  69. "sama5d22", "sama5d2"),
  70. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D23CU_EXID_MATCH,
  71. "sama5d23", "sama5d2"),
  72. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D24CX_EXID_MATCH,
  73. "sama5d24", "sama5d2"),
  74. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D24CU_EXID_MATCH,
  75. "sama5d24", "sama5d2"),
  76. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D26CU_EXID_MATCH,
  77. "sama5d26", "sama5d2"),
  78. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D27CU_EXID_MATCH,
  79. "sama5d27", "sama5d2"),
  80. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D27CN_EXID_MATCH,
  81. "sama5d27", "sama5d2"),
  82. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D28CU_EXID_MATCH,
  83. "sama5d28", "sama5d2"),
  84. AT91_SOC(SAMA5D2_CIDR_MATCH, SAMA5D28CN_EXID_MATCH,
  85. "sama5d28", "sama5d2"),
  86. AT91_SOC(SAMA5D3_CIDR_MATCH, SAMA5D31_EXID_MATCH,
  87. "sama5d31", "sama5d3"),
  88. AT91_SOC(SAMA5D3_CIDR_MATCH, SAMA5D33_EXID_MATCH,
  89. "sama5d33", "sama5d3"),
  90. AT91_SOC(SAMA5D3_CIDR_MATCH, SAMA5D34_EXID_MATCH,
  91. "sama5d34", "sama5d3"),
  92. AT91_SOC(SAMA5D3_CIDR_MATCH, SAMA5D35_EXID_MATCH,
  93. "sama5d35", "sama5d3"),
  94. AT91_SOC(SAMA5D3_CIDR_MATCH, SAMA5D36_EXID_MATCH,
  95. "sama5d36", "sama5d3"),
  96. AT91_SOC(SAMA5D4_CIDR_MATCH, SAMA5D41_EXID_MATCH,
  97. "sama5d41", "sama5d4"),
  98. AT91_SOC(SAMA5D4_CIDR_MATCH, SAMA5D42_EXID_MATCH,
  99. "sama5d42", "sama5d4"),
  100. AT91_SOC(SAMA5D4_CIDR_MATCH, SAMA5D43_EXID_MATCH,
  101. "sama5d43", "sama5d4"),
  102. AT91_SOC(SAMA5D4_CIDR_MATCH, SAMA5D44_EXID_MATCH,
  103. "sama5d44", "sama5d4"),
  104. #endif
  105. #ifdef CONFIG_SOC_SAMV7
  106. AT91_SOC(SAME70Q21_CIDR_MATCH, SAME70Q21_EXID_MATCH,
  107. "same70q21", "same7"),
  108. AT91_SOC(SAME70Q20_CIDR_MATCH, SAME70Q20_EXID_MATCH,
  109. "same70q20", "same7"),
  110. AT91_SOC(SAME70Q19_CIDR_MATCH, SAME70Q19_EXID_MATCH,
  111. "same70q19", "same7"),
  112. AT91_SOC(SAMS70Q21_CIDR_MATCH, SAMS70Q21_EXID_MATCH,
  113. "sams70q21", "sams7"),
  114. AT91_SOC(SAMS70Q20_CIDR_MATCH, SAMS70Q20_EXID_MATCH,
  115. "sams70q20", "sams7"),
  116. AT91_SOC(SAMS70Q19_CIDR_MATCH, SAMS70Q19_EXID_MATCH,
  117. "sams70q19", "sams7"),
  118. AT91_SOC(SAMV71Q21_CIDR_MATCH, SAMV71Q21_EXID_MATCH,
  119. "samv71q21", "samv7"),
  120. AT91_SOC(SAMV71Q20_CIDR_MATCH, SAMV71Q20_EXID_MATCH,
  121. "samv71q20", "samv7"),
  122. AT91_SOC(SAMV71Q19_CIDR_MATCH, SAMV71Q19_EXID_MATCH,
  123. "samv71q19", "samv7"),
  124. AT91_SOC(SAMV70Q20_CIDR_MATCH, SAMV70Q20_EXID_MATCH,
  125. "samv70q20", "samv7"),
  126. AT91_SOC(SAMV70Q19_CIDR_MATCH, SAMV70Q19_EXID_MATCH,
  127. "samv70q19", "samv7"),
  128. #endif
  129. { /* sentinel */ },
  130. };
  131. static int __init at91_get_cidr_exid_from_dbgu(u32 *cidr, u32 *exid)
  132. {
  133. struct device_node *np;
  134. void __iomem *regs;
  135. np = of_find_compatible_node(NULL, NULL, "atmel,at91rm9200-dbgu");
  136. if (!np)
  137. np = of_find_compatible_node(NULL, NULL,
  138. "atmel,at91sam9260-dbgu");
  139. if (!np)
  140. return -ENODEV;
  141. regs = of_iomap(np, 0);
  142. of_node_put(np);
  143. if (!regs) {
  144. pr_warn("Could not map DBGU iomem range");
  145. return -ENXIO;
  146. }
  147. *cidr = readl(regs + AT91_DBGU_CIDR);
  148. *exid = readl(regs + AT91_DBGU_EXID);
  149. iounmap(regs);
  150. return 0;
  151. }
  152. static int __init at91_get_cidr_exid_from_chipid(u32 *cidr, u32 *exid)
  153. {
  154. struct device_node *np;
  155. void __iomem *regs;
  156. np = of_find_compatible_node(NULL, NULL, "atmel,sama5d2-chipid");
  157. if (!np)
  158. return -ENODEV;
  159. regs = of_iomap(np, 0);
  160. of_node_put(np);
  161. if (!regs) {
  162. pr_warn("Could not map DBGU iomem range");
  163. return -ENXIO;
  164. }
  165. *cidr = readl(regs + AT91_CHIPID_CIDR);
  166. *exid = readl(regs + AT91_CHIPID_EXID);
  167. iounmap(regs);
  168. return 0;
  169. }
  170. struct soc_device * __init at91_soc_init(const struct at91_soc *socs)
  171. {
  172. struct soc_device_attribute *soc_dev_attr;
  173. const struct at91_soc *soc;
  174. struct soc_device *soc_dev;
  175. u32 cidr, exid;
  176. int ret;
  177. /*
  178. * With SAMA5D2 and later SoCs, CIDR and EXID registers are no more
  179. * in the dbgu device but in the chipid device whose purpose is only
  180. * to expose these two registers.
  181. */
  182. ret = at91_get_cidr_exid_from_dbgu(&cidr, &exid);
  183. if (ret)
  184. ret = at91_get_cidr_exid_from_chipid(&cidr, &exid);
  185. if (ret) {
  186. if (ret == -ENODEV)
  187. pr_warn("Could not find identification node");
  188. return NULL;
  189. }
  190. for (soc = socs; soc->name; soc++) {
  191. if (soc->cidr_match != (cidr & AT91_CIDR_MATCH_MASK))
  192. continue;
  193. if (!(cidr & AT91_CIDR_EXT) || soc->exid_match == exid)
  194. break;
  195. }
  196. if (!soc->name) {
  197. pr_warn("Could not find matching SoC description\n");
  198. return NULL;
  199. }
  200. soc_dev_attr = kzalloc(sizeof(*soc_dev_attr), GFP_KERNEL);
  201. if (!soc_dev_attr)
  202. return NULL;
  203. soc_dev_attr->family = soc->family;
  204. soc_dev_attr->soc_id = soc->name;
  205. soc_dev_attr->revision = kasprintf(GFP_KERNEL, "%X",
  206. AT91_CIDR_VERSION(cidr));
  207. soc_dev = soc_device_register(soc_dev_attr);
  208. if (IS_ERR(soc_dev)) {
  209. kfree(soc_dev_attr->revision);
  210. kfree(soc_dev_attr);
  211. pr_warn("Could not register SoC device\n");
  212. return NULL;
  213. }
  214. if (soc->family)
  215. pr_info("Detected SoC family: %s\n", soc->family);
  216. pr_info("Detected SoC: %s, revision %X\n", soc->name,
  217. AT91_CIDR_VERSION(cidr));
  218. return soc_dev;
  219. }
  220. static const struct of_device_id at91_soc_allowed_list[] __initconst = {
  221. { .compatible = "atmel,at91rm9200", },
  222. { .compatible = "atmel,at91sam9", },
  223. { .compatible = "atmel,sama5", },
  224. { .compatible = "atmel,samv7", },
  225. { }
  226. };
  227. static int __init atmel_soc_device_init(void)
  228. {
  229. struct device_node *np = of_find_node_by_path("/");
  230. if (!of_match_node(at91_soc_allowed_list, np))
  231. return 0;
  232. at91_soc_init(socs);
  233. return 0;
  234. }
  235. subsys_initcall(atmel_soc_device_init);