src.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright 2011 Freescale Semiconductor, Inc.
  4. * Copyright 2011 Linaro Ltd.
  5. */
  6. #include <linux/init.h>
  7. #include <linux/io.h>
  8. #include <linux/iopoll.h>
  9. #include <linux/of.h>
  10. #include <linux/of_address.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/reset-controller.h>
  13. #include <linux/smp.h>
  14. #include <asm/smp_plat.h>
  15. #include "common.h"
  16. #include "hardware.h"
  17. #define SRC_SCR 0x000
  18. #define SRC_GPR1_V1 0x020
  19. #define SRC_GPR1_V2 0x074
  20. #define SRC_GPR1(gpr_v2) ((gpr_v2) ? SRC_GPR1_V2 : SRC_GPR1_V1)
  21. #define BP_SRC_SCR_WARM_RESET_ENABLE 0
  22. #define BP_SRC_SCR_SW_GPU_RST 1
  23. #define BP_SRC_SCR_SW_VPU_RST 2
  24. #define BP_SRC_SCR_SW_IPU1_RST 3
  25. #define BP_SRC_SCR_SW_OPEN_VG_RST 4
  26. #define BP_SRC_SCR_SW_IPU2_RST 12
  27. #define BP_SRC_SCR_CORE1_RST 14
  28. #define BP_SRC_SCR_CORE1_ENABLE 22
  29. /* below is for i.MX7D */
  30. #define SRC_A7RCR1 0x008
  31. #define BP_SRC_A7RCR1_A7_CORE1_ENABLE 1
  32. #define GPC_CPU_PGC_SW_PUP_REQ 0xf0
  33. #define GPC_CPU_PGC_SW_PDN_REQ 0xfc
  34. #define GPC_PGC_C1 0x840
  35. #define BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7 0x2
  36. static void __iomem *src_base;
  37. static DEFINE_SPINLOCK(scr_lock);
  38. static bool gpr_v2;
  39. static void __iomem *gpc_base;
  40. static const int sw_reset_bits[5] = {
  41. BP_SRC_SCR_SW_GPU_RST,
  42. BP_SRC_SCR_SW_VPU_RST,
  43. BP_SRC_SCR_SW_IPU1_RST,
  44. BP_SRC_SCR_SW_OPEN_VG_RST,
  45. BP_SRC_SCR_SW_IPU2_RST
  46. };
  47. static int imx_src_reset_module(struct reset_controller_dev *rcdev,
  48. unsigned long sw_reset_idx)
  49. {
  50. unsigned long timeout;
  51. unsigned long flags;
  52. int bit;
  53. u32 val;
  54. if (sw_reset_idx >= ARRAY_SIZE(sw_reset_bits))
  55. return -EINVAL;
  56. bit = 1 << sw_reset_bits[sw_reset_idx];
  57. spin_lock_irqsave(&scr_lock, flags);
  58. val = readl_relaxed(src_base + SRC_SCR);
  59. val |= bit;
  60. writel_relaxed(val, src_base + SRC_SCR);
  61. spin_unlock_irqrestore(&scr_lock, flags);
  62. timeout = jiffies + msecs_to_jiffies(1000);
  63. while (readl(src_base + SRC_SCR) & bit) {
  64. if (time_after(jiffies, timeout))
  65. return -ETIME;
  66. cpu_relax();
  67. }
  68. return 0;
  69. }
  70. static const struct reset_control_ops imx_src_ops = {
  71. .reset = imx_src_reset_module,
  72. };
  73. static void imx_gpcv2_set_m_core_pgc(bool enable, u32 offset)
  74. {
  75. writel_relaxed(enable, gpc_base + offset);
  76. }
  77. /*
  78. * The motivation for bringing up the second i.MX7D core inside the kernel
  79. * is that legacy vendor bootloaders usually do not implement PSCI support.
  80. * This is a significant blocker for systems in the field that are running old
  81. * bootloader versions to upgrade to a modern mainline kernel version, as only
  82. * one CPU of the i.MX7D would be brought up.
  83. * Bring up the second i.MX7D core inside the kernel to make the migration
  84. * path to mainline kernel easier for the existing iMX7D users.
  85. */
  86. void imx_gpcv2_set_core1_pdn_pup_by_software(bool pdn)
  87. {
  88. u32 reg = pdn ? GPC_CPU_PGC_SW_PDN_REQ : GPC_CPU_PGC_SW_PUP_REQ;
  89. u32 val, pup;
  90. int ret;
  91. imx_gpcv2_set_m_core_pgc(true, GPC_PGC_C1);
  92. val = readl_relaxed(gpc_base + reg);
  93. val |= BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7;
  94. writel_relaxed(val, gpc_base + reg);
  95. ret = readl_relaxed_poll_timeout_atomic(gpc_base + reg, pup,
  96. !(pup & BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7),
  97. 5, 1000000);
  98. if (ret < 0) {
  99. pr_err("i.MX7D: CORE1_A7 power up timeout\n");
  100. val &= ~BM_CPU_PGC_SW_PDN_PUP_REQ_CORE1_A7;
  101. writel_relaxed(val, gpc_base + reg);
  102. }
  103. imx_gpcv2_set_m_core_pgc(false, GPC_PGC_C1);
  104. }
  105. void imx_enable_cpu(int cpu, bool enable)
  106. {
  107. u32 mask, val;
  108. cpu = cpu_logical_map(cpu);
  109. spin_lock(&scr_lock);
  110. if (gpr_v2) {
  111. if (enable)
  112. imx_gpcv2_set_core1_pdn_pup_by_software(false);
  113. mask = 1 << (BP_SRC_A7RCR1_A7_CORE1_ENABLE + cpu - 1);
  114. val = readl_relaxed(src_base + SRC_A7RCR1);
  115. val = enable ? val | mask : val & ~mask;
  116. writel_relaxed(val, src_base + SRC_A7RCR1);
  117. } else {
  118. mask = 1 << (BP_SRC_SCR_CORE1_ENABLE + cpu - 1);
  119. val = readl_relaxed(src_base + SRC_SCR);
  120. val = enable ? val | mask : val & ~mask;
  121. val |= 1 << (BP_SRC_SCR_CORE1_RST + cpu - 1);
  122. writel_relaxed(val, src_base + SRC_SCR);
  123. }
  124. spin_unlock(&scr_lock);
  125. }
  126. void imx_set_cpu_jump(int cpu, void *jump_addr)
  127. {
  128. cpu = cpu_logical_map(cpu);
  129. writel_relaxed(__pa_symbol(jump_addr),
  130. src_base + SRC_GPR1(gpr_v2) + cpu * 8);
  131. }
  132. u32 imx_get_cpu_arg(int cpu)
  133. {
  134. cpu = cpu_logical_map(cpu);
  135. return readl_relaxed(src_base + SRC_GPR1(gpr_v2) + cpu * 8 + 4);
  136. }
  137. void imx_set_cpu_arg(int cpu, u32 arg)
  138. {
  139. cpu = cpu_logical_map(cpu);
  140. writel_relaxed(arg, src_base + SRC_GPR1(gpr_v2) + cpu * 8 + 4);
  141. }
  142. void __init imx_src_init(void)
  143. {
  144. struct device_node *np;
  145. u32 val;
  146. np = of_find_compatible_node(NULL, NULL, "fsl,imx51-src");
  147. if (!np)
  148. return;
  149. src_base = of_iomap(np, 0);
  150. WARN_ON(!src_base);
  151. /*
  152. * force warm reset sources to generate cold reset
  153. * for a more reliable restart
  154. */
  155. spin_lock(&scr_lock);
  156. val = readl_relaxed(src_base + SRC_SCR);
  157. val &= ~(1 << BP_SRC_SCR_WARM_RESET_ENABLE);
  158. writel_relaxed(val, src_base + SRC_SCR);
  159. spin_unlock(&scr_lock);
  160. }
  161. void __init imx7_src_init(void)
  162. {
  163. struct device_node *np;
  164. gpr_v2 = true;
  165. np = of_find_compatible_node(NULL, NULL, "fsl,imx7d-src");
  166. if (!np)
  167. return;
  168. src_base = of_iomap(np, 0);
  169. if (!src_base)
  170. return;
  171. np = of_find_compatible_node(NULL, NULL, "fsl,imx7d-gpc");
  172. if (!np)
  173. return;
  174. gpc_base = of_iomap(np, 0);
  175. if (!gpc_base)
  176. return;
  177. }
  178. static const struct of_device_id imx_src_dt_ids[] = {
  179. { .compatible = "fsl,imx51-src" },
  180. { /* sentinel */ }
  181. };
  182. static int imx_src_probe(struct platform_device *pdev)
  183. {
  184. struct reset_controller_dev *rcdev;
  185. rcdev = devm_kzalloc(&pdev->dev, sizeof(*rcdev), GFP_KERNEL);
  186. if (!rcdev)
  187. return -ENOMEM;
  188. rcdev->ops = &imx_src_ops;
  189. rcdev->dev = &pdev->dev;
  190. rcdev->of_node = pdev->dev.of_node;
  191. rcdev->nr_resets = ARRAY_SIZE(sw_reset_bits);
  192. return devm_reset_controller_register(&pdev->dev, rcdev);
  193. }
  194. static struct platform_driver imx_src_driver = {
  195. .driver = {
  196. .name = "imx-src",
  197. .of_match_table = imx_src_dt_ids,
  198. },
  199. .probe = imx_src_probe,
  200. };
  201. builtin_platform_driver(imx_src_driver);