flowctrl.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * drivers/soc/tegra/flowctrl.c
  4. *
  5. * Functions and macros to control the flowcontroller
  6. *
  7. * Copyright (c) 2010-2012, NVIDIA Corporation. All rights reserved.
  8. */
  9. #include <linux/cpumask.h>
  10. #include <linux/init.h>
  11. #include <linux/io.h>
  12. #include <linux/kernel.h>
  13. #include <linux/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/platform_device.h>
  16. #include <soc/tegra/common.h>
  17. #include <soc/tegra/flowctrl.h>
  18. #include <soc/tegra/fuse.h>
  19. static u8 flowctrl_offset_halt_cpu[] = {
  20. FLOW_CTRL_HALT_CPU0_EVENTS,
  21. FLOW_CTRL_HALT_CPU1_EVENTS,
  22. FLOW_CTRL_HALT_CPU1_EVENTS + 8,
  23. FLOW_CTRL_HALT_CPU1_EVENTS + 16,
  24. };
  25. static u8 flowctrl_offset_cpu_csr[] = {
  26. FLOW_CTRL_CPU0_CSR,
  27. FLOW_CTRL_CPU1_CSR,
  28. FLOW_CTRL_CPU1_CSR + 8,
  29. FLOW_CTRL_CPU1_CSR + 16,
  30. };
  31. static void __iomem *tegra_flowctrl_base;
  32. static void flowctrl_update(u8 offset, u32 value)
  33. {
  34. if (WARN_ONCE(IS_ERR_OR_NULL(tegra_flowctrl_base),
  35. "Tegra flowctrl not initialised!\n"))
  36. return;
  37. writel(value, tegra_flowctrl_base + offset);
  38. /* ensure the update has reached the flow controller */
  39. wmb();
  40. readl_relaxed(tegra_flowctrl_base + offset);
  41. }
  42. u32 flowctrl_read_cpu_csr(unsigned int cpuid)
  43. {
  44. u8 offset = flowctrl_offset_cpu_csr[cpuid];
  45. if (WARN_ONCE(IS_ERR_OR_NULL(tegra_flowctrl_base),
  46. "Tegra flowctrl not initialised!\n"))
  47. return 0;
  48. return readl(tegra_flowctrl_base + offset);
  49. }
  50. void flowctrl_write_cpu_csr(unsigned int cpuid, u32 value)
  51. {
  52. return flowctrl_update(flowctrl_offset_cpu_csr[cpuid], value);
  53. }
  54. void flowctrl_write_cpu_halt(unsigned int cpuid, u32 value)
  55. {
  56. return flowctrl_update(flowctrl_offset_halt_cpu[cpuid], value);
  57. }
  58. void flowctrl_cpu_suspend_enter(unsigned int cpuid)
  59. {
  60. unsigned int reg;
  61. int i;
  62. reg = flowctrl_read_cpu_csr(cpuid);
  63. switch (tegra_get_chip_id()) {
  64. case TEGRA20:
  65. /* clear wfe bitmap */
  66. reg &= ~TEGRA20_FLOW_CTRL_CSR_WFE_BITMAP;
  67. /* clear wfi bitmap */
  68. reg &= ~TEGRA20_FLOW_CTRL_CSR_WFI_BITMAP;
  69. /* pwr gating on wfe */
  70. reg |= TEGRA20_FLOW_CTRL_CSR_WFE_CPU0 << cpuid;
  71. break;
  72. case TEGRA30:
  73. case TEGRA114:
  74. case TEGRA124:
  75. /* clear wfe bitmap */
  76. reg &= ~TEGRA30_FLOW_CTRL_CSR_WFE_BITMAP;
  77. /* clear wfi bitmap */
  78. reg &= ~TEGRA30_FLOW_CTRL_CSR_WFI_BITMAP;
  79. if (tegra_get_chip_id() == TEGRA30) {
  80. /*
  81. * The wfi doesn't work well on Tegra30 because
  82. * CPU hangs under some odd circumstances after
  83. * power-gating (like memory running off PLLP),
  84. * hence use wfe that is working perfectly fine.
  85. * Note that Tegra30 TRM doc clearly stands that
  86. * wfi should be used for the "Cluster Switching",
  87. * while wfe for the power-gating, just like it
  88. * is done on Tegra20.
  89. */
  90. reg |= TEGRA20_FLOW_CTRL_CSR_WFE_CPU0 << cpuid;
  91. } else {
  92. /* pwr gating on wfi */
  93. reg |= TEGRA30_FLOW_CTRL_CSR_WFI_CPU0 << cpuid;
  94. }
  95. break;
  96. }
  97. reg |= FLOW_CTRL_CSR_INTR_FLAG; /* clear intr flag */
  98. reg |= FLOW_CTRL_CSR_EVENT_FLAG; /* clear event flag */
  99. reg |= FLOW_CTRL_CSR_ENABLE; /* pwr gating */
  100. flowctrl_write_cpu_csr(cpuid, reg);
  101. for (i = 0; i < num_possible_cpus(); i++) {
  102. if (i == cpuid)
  103. continue;
  104. reg = flowctrl_read_cpu_csr(i);
  105. reg |= FLOW_CTRL_CSR_EVENT_FLAG;
  106. reg |= FLOW_CTRL_CSR_INTR_FLAG;
  107. flowctrl_write_cpu_csr(i, reg);
  108. }
  109. }
  110. void flowctrl_cpu_suspend_exit(unsigned int cpuid)
  111. {
  112. unsigned int reg;
  113. /* Disable powergating via flow controller for CPU0 */
  114. reg = flowctrl_read_cpu_csr(cpuid);
  115. switch (tegra_get_chip_id()) {
  116. case TEGRA20:
  117. /* clear wfe bitmap */
  118. reg &= ~TEGRA20_FLOW_CTRL_CSR_WFE_BITMAP;
  119. /* clear wfi bitmap */
  120. reg &= ~TEGRA20_FLOW_CTRL_CSR_WFI_BITMAP;
  121. break;
  122. case TEGRA30:
  123. case TEGRA114:
  124. case TEGRA124:
  125. /* clear wfe bitmap */
  126. reg &= ~TEGRA30_FLOW_CTRL_CSR_WFE_BITMAP;
  127. /* clear wfi bitmap */
  128. reg &= ~TEGRA30_FLOW_CTRL_CSR_WFI_BITMAP;
  129. break;
  130. }
  131. reg &= ~FLOW_CTRL_CSR_ENABLE; /* clear enable */
  132. reg |= FLOW_CTRL_CSR_INTR_FLAG; /* clear intr */
  133. reg |= FLOW_CTRL_CSR_EVENT_FLAG; /* clear event */
  134. flowctrl_write_cpu_csr(cpuid, reg);
  135. }
  136. static int tegra_flowctrl_probe(struct platform_device *pdev)
  137. {
  138. void __iomem *base = tegra_flowctrl_base;
  139. struct resource *res;
  140. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  141. tegra_flowctrl_base = devm_ioremap_resource(&pdev->dev, res);
  142. if (IS_ERR(tegra_flowctrl_base))
  143. return PTR_ERR(tegra_flowctrl_base);
  144. iounmap(base);
  145. return 0;
  146. }
  147. static const struct of_device_id tegra_flowctrl_match[] = {
  148. { .compatible = "nvidia,tegra210-flowctrl" },
  149. { .compatible = "nvidia,tegra124-flowctrl" },
  150. { .compatible = "nvidia,tegra114-flowctrl" },
  151. { .compatible = "nvidia,tegra30-flowctrl" },
  152. { .compatible = "nvidia,tegra20-flowctrl" },
  153. { }
  154. };
  155. static struct platform_driver tegra_flowctrl_driver = {
  156. .driver = {
  157. .name = "tegra-flowctrl",
  158. .suppress_bind_attrs = true,
  159. .of_match_table = tegra_flowctrl_match,
  160. },
  161. .probe = tegra_flowctrl_probe,
  162. };
  163. builtin_platform_driver(tegra_flowctrl_driver);
  164. static int __init tegra_flowctrl_init(void)
  165. {
  166. struct resource res;
  167. struct device_node *np;
  168. if (!soc_is_tegra())
  169. return 0;
  170. np = of_find_matching_node(NULL, tegra_flowctrl_match);
  171. if (np) {
  172. if (of_address_to_resource(np, 0, &res) < 0) {
  173. pr_err("failed to get flowctrl register\n");
  174. return -ENXIO;
  175. }
  176. of_node_put(np);
  177. } else if (IS_ENABLED(CONFIG_ARM)) {
  178. /*
  179. * Hardcoded fallback for 32-bit Tegra
  180. * devices if device tree node is missing.
  181. */
  182. res.start = 0x60007000;
  183. res.end = 0x60007fff;
  184. res.flags = IORESOURCE_MEM;
  185. } else {
  186. /*
  187. * At this point we're running on a Tegra,
  188. * that doesn't support the flow controller
  189. * (eg. Tegra186), so just return.
  190. */
  191. return 0;
  192. }
  193. tegra_flowctrl_base = ioremap(res.start, resource_size(&res));
  194. if (!tegra_flowctrl_base)
  195. return -ENXIO;
  196. return 0;
  197. }
  198. early_initcall(tegra_flowctrl_init);