tegra-apbmisc.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2014-2023, NVIDIA CORPORATION. All rights reserved.
  4. */
  5. #include <linux/acpi.h>
  6. #include <linux/export.h>
  7. #include <linux/io.h>
  8. #include <linux/kernel.h>
  9. #include <linux/mod_devicetable.h>
  10. #include <linux/of.h>
  11. #include <linux/of_address.h>
  12. #include <soc/tegra/common.h>
  13. #include <soc/tegra/fuse.h>
  14. #include "fuse.h"
  15. #define FUSE_SKU_INFO 0x10
  16. #define ERD_ERR_CONFIG 0x120c
  17. #define ERD_MASK_INBAND_ERR 0x1
  18. #define PMC_STRAPPING_OPT_A_RAM_CODE_SHIFT 4
  19. #define PMC_STRAPPING_OPT_A_RAM_CODE_MASK_LONG \
  20. (0xf << PMC_STRAPPING_OPT_A_RAM_CODE_SHIFT)
  21. #define PMC_STRAPPING_OPT_A_RAM_CODE_MASK_SHORT \
  22. (0x3 << PMC_STRAPPING_OPT_A_RAM_CODE_SHIFT)
  23. static void __iomem *apbmisc_base;
  24. static bool long_ram_code;
  25. static u32 strapping;
  26. static u32 chipid;
  27. u32 tegra_read_chipid(void)
  28. {
  29. WARN(!chipid, "Tegra APB MISC not yet available\n");
  30. return chipid;
  31. }
  32. u8 tegra_get_chip_id(void)
  33. {
  34. return (tegra_read_chipid() >> 8) & 0xff;
  35. }
  36. u8 tegra_get_major_rev(void)
  37. {
  38. return (tegra_read_chipid() >> 4) & 0xf;
  39. }
  40. u8 tegra_get_minor_rev(void)
  41. {
  42. return (tegra_read_chipid() >> 16) & 0xf;
  43. }
  44. u8 tegra_get_platform(void)
  45. {
  46. return (tegra_read_chipid() >> 20) & 0xf;
  47. }
  48. bool tegra_is_silicon(void)
  49. {
  50. switch (tegra_get_chip_id()) {
  51. case TEGRA194:
  52. case TEGRA234:
  53. case TEGRA241:
  54. case TEGRA264:
  55. if (tegra_get_platform() == 0)
  56. return true;
  57. return false;
  58. }
  59. /*
  60. * Chips prior to Tegra194 have a different way of determining whether
  61. * they are silicon or not. Since we never supported simulation on the
  62. * older Tegra chips, don't bother extracting the information and just
  63. * report that we're running on silicon.
  64. */
  65. return true;
  66. }
  67. u32 tegra_read_straps(void)
  68. {
  69. WARN(!chipid, "Tegra ABP MISC not yet available\n");
  70. return strapping;
  71. }
  72. u32 tegra_read_ram_code(void)
  73. {
  74. u32 straps = tegra_read_straps();
  75. if (long_ram_code)
  76. straps &= PMC_STRAPPING_OPT_A_RAM_CODE_MASK_LONG;
  77. else
  78. straps &= PMC_STRAPPING_OPT_A_RAM_CODE_MASK_SHORT;
  79. return straps >> PMC_STRAPPING_OPT_A_RAM_CODE_SHIFT;
  80. }
  81. EXPORT_SYMBOL_GPL(tegra_read_ram_code);
  82. /*
  83. * The function sets ERD(Error Response Disable) bit.
  84. * This allows to mask inband errors and always send an
  85. * OKAY response from CBB to the master which caused error.
  86. */
  87. int tegra194_miscreg_mask_serror(void)
  88. {
  89. if (!apbmisc_base)
  90. return -EPROBE_DEFER;
  91. if (!of_machine_is_compatible("nvidia,tegra194")) {
  92. WARN(1, "Only supported for Tegra194 devices!\n");
  93. return -EOPNOTSUPP;
  94. }
  95. writel_relaxed(ERD_MASK_INBAND_ERR,
  96. apbmisc_base + ERD_ERR_CONFIG);
  97. return 0;
  98. }
  99. EXPORT_SYMBOL(tegra194_miscreg_mask_serror);
  100. static const struct of_device_id apbmisc_match[] __initconst = {
  101. { .compatible = "nvidia,tegra20-apbmisc", },
  102. { .compatible = "nvidia,tegra186-misc", },
  103. { .compatible = "nvidia,tegra194-misc", },
  104. { .compatible = "nvidia,tegra234-misc", },
  105. { .compatible = "nvidia,tegra264-misc", },
  106. {},
  107. };
  108. void __init tegra_init_revision(void)
  109. {
  110. u8 chip_id, minor_rev;
  111. chip_id = tegra_get_chip_id();
  112. minor_rev = tegra_get_minor_rev();
  113. switch (minor_rev) {
  114. case 1:
  115. tegra_sku_info.revision = TEGRA_REVISION_A01;
  116. break;
  117. case 2:
  118. tegra_sku_info.revision = TEGRA_REVISION_A02;
  119. break;
  120. case 3:
  121. if (chip_id == TEGRA20 && (tegra_fuse_read_spare(18) ||
  122. tegra_fuse_read_spare(19)))
  123. tegra_sku_info.revision = TEGRA_REVISION_A03p;
  124. else
  125. tegra_sku_info.revision = TEGRA_REVISION_A03;
  126. break;
  127. case 4:
  128. tegra_sku_info.revision = TEGRA_REVISION_A04;
  129. break;
  130. default:
  131. tegra_sku_info.revision = TEGRA_REVISION_UNKNOWN;
  132. }
  133. tegra_sku_info.sku_id = tegra_fuse_read_early(FUSE_SKU_INFO);
  134. tegra_sku_info.platform = tegra_get_platform();
  135. }
  136. static void tegra_init_apbmisc_resources(struct resource *apbmisc,
  137. struct resource *straps)
  138. {
  139. void __iomem *strapping_base;
  140. apbmisc_base = ioremap(apbmisc->start, resource_size(apbmisc));
  141. if (apbmisc_base)
  142. chipid = readl_relaxed(apbmisc_base + 4);
  143. else
  144. pr_err("failed to map APBMISC registers\n");
  145. strapping_base = ioremap(straps->start, resource_size(straps));
  146. if (strapping_base) {
  147. strapping = readl_relaxed(strapping_base);
  148. iounmap(strapping_base);
  149. } else {
  150. pr_err("failed to map strapping options registers\n");
  151. }
  152. }
  153. /**
  154. * tegra_init_apbmisc - Initializes Tegra APBMISC and Strapping registers.
  155. *
  156. * This is called during early init as some of the old 32-bit ARM code needs
  157. * information from the APBMISC registers very early during boot.
  158. */
  159. void __init tegra_init_apbmisc(void)
  160. {
  161. struct resource apbmisc, straps;
  162. struct device_node *np;
  163. np = of_find_matching_node(NULL, apbmisc_match);
  164. if (!np) {
  165. /*
  166. * Fall back to legacy initialization for 32-bit ARM only. All
  167. * 64-bit ARM device tree files for Tegra are required to have
  168. * an APBMISC node.
  169. *
  170. * This is for backwards-compatibility with old device trees
  171. * that didn't contain an APBMISC node.
  172. */
  173. if (IS_ENABLED(CONFIG_ARM) && soc_is_tegra()) {
  174. /* APBMISC registers (chip revision, ...) */
  175. apbmisc.start = 0x70000800;
  176. apbmisc.end = 0x70000863;
  177. apbmisc.flags = IORESOURCE_MEM;
  178. /* strapping options */
  179. if (of_machine_is_compatible("nvidia,tegra124")) {
  180. straps.start = 0x7000e864;
  181. straps.end = 0x7000e867;
  182. } else {
  183. straps.start = 0x70000008;
  184. straps.end = 0x7000000b;
  185. }
  186. straps.flags = IORESOURCE_MEM;
  187. pr_warn("Using APBMISC region %pR\n", &apbmisc);
  188. pr_warn("Using strapping options registers %pR\n",
  189. &straps);
  190. } else {
  191. /*
  192. * At this point we're not running on Tegra, so play
  193. * nice with multi-platform kernels.
  194. */
  195. return;
  196. }
  197. } else {
  198. /*
  199. * Extract information from the device tree if we've found a
  200. * matching node.
  201. */
  202. if (of_address_to_resource(np, 0, &apbmisc) < 0) {
  203. pr_err("failed to get APBMISC registers\n");
  204. goto put;
  205. }
  206. if (of_address_to_resource(np, 1, &straps) < 0) {
  207. pr_err("failed to get strapping options registers\n");
  208. goto put;
  209. }
  210. }
  211. tegra_init_apbmisc_resources(&apbmisc, &straps);
  212. long_ram_code = of_property_read_bool(np, "nvidia,long-ram-code");
  213. put:
  214. of_node_put(np);
  215. }
  216. #ifdef CONFIG_ACPI
  217. static const struct acpi_device_id apbmisc_acpi_match[] = {
  218. { "NVDA2010" },
  219. { /* sentinel */ }
  220. };
  221. void tegra_acpi_init_apbmisc(void)
  222. {
  223. struct resource *resources[2] = { NULL };
  224. struct resource_entry *rentry;
  225. struct acpi_device *adev = NULL;
  226. struct list_head resource_list;
  227. int rcount = 0;
  228. int ret;
  229. adev = acpi_dev_get_first_match_dev(apbmisc_acpi_match[0].id, NULL, -1);
  230. if (!adev)
  231. return;
  232. INIT_LIST_HEAD(&resource_list);
  233. ret = acpi_dev_get_memory_resources(adev, &resource_list);
  234. if (ret < 0) {
  235. pr_err("failed to get APBMISC memory resources");
  236. goto out_put_acpi_dev;
  237. }
  238. /*
  239. * Get required memory resources.
  240. *
  241. * resources[0]: apbmisc.
  242. * resources[1]: straps.
  243. */
  244. resource_list_for_each_entry(rentry, &resource_list) {
  245. if (rcount >= ARRAY_SIZE(resources))
  246. break;
  247. resources[rcount++] = rentry->res;
  248. }
  249. if (!resources[0]) {
  250. pr_err("failed to get APBMISC registers\n");
  251. goto out_free_resource_list;
  252. }
  253. if (!resources[1]) {
  254. pr_err("failed to get strapping options registers\n");
  255. goto out_free_resource_list;
  256. }
  257. tegra_init_apbmisc_resources(resources[0], resources[1]);
  258. out_free_resource_list:
  259. acpi_dev_free_resource_list(&resource_list);
  260. out_put_acpi_dev:
  261. acpi_dev_put(adev);
  262. }
  263. #else
  264. void tegra_acpi_init_apbmisc(void)
  265. {
  266. }
  267. #endif