gpc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492
  1. /*
  2. * Copyright 2011-2013 Freescale Semiconductor, Inc.
  3. * Copyright 2011 Linaro Ltd.
  4. *
  5. * The code contained herein is licensed under the GNU General Public
  6. * License. You may obtain a copy of the GNU General Public License
  7. * Version 2 or later at the following locations:
  8. *
  9. * http://www.opensource.org/licenses/gpl-license.html
  10. * http://www.gnu.org/copyleft/gpl.html
  11. */
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/io.h>
  15. #include <linux/irq.h>
  16. #include <linux/irqchip.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/pm_domain.h>
  22. #include <linux/regulator/consumer.h>
  23. #include <linux/irqchip/arm-gic.h>
  24. #include "common.h"
  25. #include "hardware.h"
  26. #define GPC_CNTR 0x000
  27. #define GPC_IMR1 0x008
  28. #define GPC_PGC_GPU_PDN 0x260
  29. #define GPC_PGC_GPU_PUPSCR 0x264
  30. #define GPC_PGC_GPU_PDNSCR 0x268
  31. #define GPC_PGC_CPU_PDN 0x2a0
  32. #define GPC_PGC_CPU_PUPSCR 0x2a4
  33. #define GPC_PGC_CPU_PDNSCR 0x2a8
  34. #define GPC_PGC_SW2ISO_SHIFT 0x8
  35. #define GPC_PGC_SW_SHIFT 0x0
  36. #define IMR_NUM 4
  37. #define GPC_MAX_IRQS (IMR_NUM * 32)
  38. #define GPU_VPU_PUP_REQ BIT(1)
  39. #define GPU_VPU_PDN_REQ BIT(0)
  40. #define GPC_CLK_MAX 6
  41. struct pu_domain {
  42. struct generic_pm_domain base;
  43. struct regulator *reg;
  44. struct clk *clk[GPC_CLK_MAX];
  45. int num_clks;
  46. };
  47. static void __iomem *gpc_base;
  48. static u32 gpc_wake_irqs[IMR_NUM];
  49. static u32 gpc_saved_imrs[IMR_NUM];
  50. void imx_gpc_set_arm_power_up_timing(u32 sw2iso, u32 sw)
  51. {
  52. writel_relaxed((sw2iso << GPC_PGC_SW2ISO_SHIFT) |
  53. (sw << GPC_PGC_SW_SHIFT), gpc_base + GPC_PGC_CPU_PUPSCR);
  54. }
  55. void imx_gpc_set_arm_power_down_timing(u32 sw2iso, u32 sw)
  56. {
  57. writel_relaxed((sw2iso << GPC_PGC_SW2ISO_SHIFT) |
  58. (sw << GPC_PGC_SW_SHIFT), gpc_base + GPC_PGC_CPU_PDNSCR);
  59. }
  60. void imx_gpc_set_arm_power_in_lpm(bool power_off)
  61. {
  62. writel_relaxed(power_off, gpc_base + GPC_PGC_CPU_PDN);
  63. }
  64. void imx_gpc_pre_suspend(bool arm_power_off)
  65. {
  66. void __iomem *reg_imr1 = gpc_base + GPC_IMR1;
  67. int i;
  68. /* Tell GPC to power off ARM core when suspend */
  69. if (arm_power_off)
  70. imx_gpc_set_arm_power_in_lpm(arm_power_off);
  71. for (i = 0; i < IMR_NUM; i++) {
  72. gpc_saved_imrs[i] = readl_relaxed(reg_imr1 + i * 4);
  73. writel_relaxed(~gpc_wake_irqs[i], reg_imr1 + i * 4);
  74. }
  75. }
  76. void imx_gpc_post_resume(void)
  77. {
  78. void __iomem *reg_imr1 = gpc_base + GPC_IMR1;
  79. int i;
  80. /* Keep ARM core powered on for other low-power modes */
  81. imx_gpc_set_arm_power_in_lpm(false);
  82. for (i = 0; i < IMR_NUM; i++)
  83. writel_relaxed(gpc_saved_imrs[i], reg_imr1 + i * 4);
  84. }
  85. static int imx_gpc_irq_set_wake(struct irq_data *d, unsigned int on)
  86. {
  87. unsigned int idx = d->hwirq / 32;
  88. u32 mask;
  89. mask = 1 << d->hwirq % 32;
  90. gpc_wake_irqs[idx] = on ? gpc_wake_irqs[idx] | mask :
  91. gpc_wake_irqs[idx] & ~mask;
  92. /*
  93. * Do *not* call into the parent, as the GIC doesn't have any
  94. * wake-up facility...
  95. */
  96. return 0;
  97. }
  98. void imx_gpc_mask_all(void)
  99. {
  100. void __iomem *reg_imr1 = gpc_base + GPC_IMR1;
  101. int i;
  102. for (i = 0; i < IMR_NUM; i++) {
  103. gpc_saved_imrs[i] = readl_relaxed(reg_imr1 + i * 4);
  104. writel_relaxed(~0, reg_imr1 + i * 4);
  105. }
  106. }
  107. void imx_gpc_restore_all(void)
  108. {
  109. void __iomem *reg_imr1 = gpc_base + GPC_IMR1;
  110. int i;
  111. for (i = 0; i < IMR_NUM; i++)
  112. writel_relaxed(gpc_saved_imrs[i], reg_imr1 + i * 4);
  113. }
  114. void imx_gpc_hwirq_unmask(unsigned int hwirq)
  115. {
  116. void __iomem *reg;
  117. u32 val;
  118. reg = gpc_base + GPC_IMR1 + hwirq / 32 * 4;
  119. val = readl_relaxed(reg);
  120. val &= ~(1 << hwirq % 32);
  121. writel_relaxed(val, reg);
  122. }
  123. void imx_gpc_hwirq_mask(unsigned int hwirq)
  124. {
  125. void __iomem *reg;
  126. u32 val;
  127. reg = gpc_base + GPC_IMR1 + hwirq / 32 * 4;
  128. val = readl_relaxed(reg);
  129. val |= 1 << (hwirq % 32);
  130. writel_relaxed(val, reg);
  131. }
  132. static void imx_gpc_irq_unmask(struct irq_data *d)
  133. {
  134. imx_gpc_hwirq_unmask(d->hwirq);
  135. irq_chip_unmask_parent(d);
  136. }
  137. static void imx_gpc_irq_mask(struct irq_data *d)
  138. {
  139. imx_gpc_hwirq_mask(d->hwirq);
  140. irq_chip_mask_parent(d);
  141. }
  142. static struct irq_chip imx_gpc_chip = {
  143. .name = "GPC",
  144. .irq_eoi = irq_chip_eoi_parent,
  145. .irq_mask = imx_gpc_irq_mask,
  146. .irq_unmask = imx_gpc_irq_unmask,
  147. .irq_retrigger = irq_chip_retrigger_hierarchy,
  148. .irq_set_wake = imx_gpc_irq_set_wake,
  149. .irq_set_type = irq_chip_set_type_parent,
  150. #ifdef CONFIG_SMP
  151. .irq_set_affinity = irq_chip_set_affinity_parent,
  152. #endif
  153. };
  154. static int imx_gpc_domain_translate(struct irq_domain *d,
  155. struct irq_fwspec *fwspec,
  156. unsigned long *hwirq,
  157. unsigned int *type)
  158. {
  159. if (is_of_node(fwspec->fwnode)) {
  160. if (fwspec->param_count != 3)
  161. return -EINVAL;
  162. /* No PPI should point to this domain */
  163. if (fwspec->param[0] != 0)
  164. return -EINVAL;
  165. *hwirq = fwspec->param[1];
  166. *type = fwspec->param[2];
  167. return 0;
  168. }
  169. return -EINVAL;
  170. }
  171. static int imx_gpc_domain_alloc(struct irq_domain *domain,
  172. unsigned int irq,
  173. unsigned int nr_irqs, void *data)
  174. {
  175. struct irq_fwspec *fwspec = data;
  176. struct irq_fwspec parent_fwspec;
  177. irq_hw_number_t hwirq;
  178. int i;
  179. if (fwspec->param_count != 3)
  180. return -EINVAL; /* Not GIC compliant */
  181. if (fwspec->param[0] != 0)
  182. return -EINVAL; /* No PPI should point to this domain */
  183. hwirq = fwspec->param[1];
  184. if (hwirq >= GPC_MAX_IRQS)
  185. return -EINVAL; /* Can't deal with this */
  186. for (i = 0; i < nr_irqs; i++)
  187. irq_domain_set_hwirq_and_chip(domain, irq + i, hwirq + i,
  188. &imx_gpc_chip, NULL);
  189. parent_fwspec = *fwspec;
  190. parent_fwspec.fwnode = domain->parent->fwnode;
  191. return irq_domain_alloc_irqs_parent(domain, irq, nr_irqs,
  192. &parent_fwspec);
  193. }
  194. static const struct irq_domain_ops imx_gpc_domain_ops = {
  195. .translate = imx_gpc_domain_translate,
  196. .alloc = imx_gpc_domain_alloc,
  197. .free = irq_domain_free_irqs_common,
  198. };
  199. static int __init imx_gpc_init(struct device_node *node,
  200. struct device_node *parent)
  201. {
  202. struct irq_domain *parent_domain, *domain;
  203. int i;
  204. if (!parent) {
  205. pr_err("%s: no parent, giving up\n", node->full_name);
  206. return -ENODEV;
  207. }
  208. parent_domain = irq_find_host(parent);
  209. if (!parent_domain) {
  210. pr_err("%s: unable to obtain parent domain\n", node->full_name);
  211. return -ENXIO;
  212. }
  213. gpc_base = of_iomap(node, 0);
  214. if (WARN_ON(!gpc_base))
  215. return -ENOMEM;
  216. domain = irq_domain_add_hierarchy(parent_domain, 0, GPC_MAX_IRQS,
  217. node, &imx_gpc_domain_ops,
  218. NULL);
  219. if (!domain) {
  220. iounmap(gpc_base);
  221. return -ENOMEM;
  222. }
  223. /* Initially mask all interrupts */
  224. for (i = 0; i < IMR_NUM; i++)
  225. writel_relaxed(~0, gpc_base + GPC_IMR1 + i * 4);
  226. /*
  227. * Clear the OF_POPULATED flag set in of_irq_init so that
  228. * later the GPC power domain driver will not be skipped.
  229. */
  230. of_node_clear_flag(node, OF_POPULATED);
  231. return 0;
  232. }
  233. IRQCHIP_DECLARE(imx_gpc, "fsl,imx6q-gpc", imx_gpc_init);
  234. void __init imx_gpc_check_dt(void)
  235. {
  236. struct device_node *np;
  237. np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-gpc");
  238. if (WARN_ON(!np))
  239. return;
  240. if (WARN_ON(!of_find_property(np, "interrupt-controller", NULL))) {
  241. pr_warn("Outdated DT detected, suspend/resume will NOT work\n");
  242. /* map GPC, so that at least CPUidle and WARs keep working */
  243. gpc_base = of_iomap(np, 0);
  244. }
  245. }
  246. static void _imx6q_pm_pu_power_off(struct generic_pm_domain *genpd)
  247. {
  248. int iso, iso2sw;
  249. u32 val;
  250. /* Read ISO and ISO2SW power down delays */
  251. val = readl_relaxed(gpc_base + GPC_PGC_GPU_PDNSCR);
  252. iso = val & 0x3f;
  253. iso2sw = (val >> 8) & 0x3f;
  254. /* Gate off PU domain when GPU/VPU when powered down */
  255. writel_relaxed(0x1, gpc_base + GPC_PGC_GPU_PDN);
  256. /* Request GPC to power down GPU/VPU */
  257. val = readl_relaxed(gpc_base + GPC_CNTR);
  258. val |= GPU_VPU_PDN_REQ;
  259. writel_relaxed(val, gpc_base + GPC_CNTR);
  260. /* Wait ISO + ISO2SW IPG clock cycles */
  261. ndelay((iso + iso2sw) * 1000 / 66);
  262. }
  263. static int imx6q_pm_pu_power_off(struct generic_pm_domain *genpd)
  264. {
  265. struct pu_domain *pu = container_of(genpd, struct pu_domain, base);
  266. _imx6q_pm_pu_power_off(genpd);
  267. if (pu->reg)
  268. regulator_disable(pu->reg);
  269. return 0;
  270. }
  271. static int imx6q_pm_pu_power_on(struct generic_pm_domain *genpd)
  272. {
  273. struct pu_domain *pu = container_of(genpd, struct pu_domain, base);
  274. int i, ret, sw, sw2iso;
  275. u32 val;
  276. if (pu->reg)
  277. ret = regulator_enable(pu->reg);
  278. if (pu->reg && ret) {
  279. pr_err("%s: failed to enable regulator: %d\n", __func__, ret);
  280. return ret;
  281. }
  282. /* Enable reset clocks for all devices in the PU domain */
  283. for (i = 0; i < pu->num_clks; i++)
  284. clk_prepare_enable(pu->clk[i]);
  285. /* Gate off PU domain when GPU/VPU when powered down */
  286. writel_relaxed(0x1, gpc_base + GPC_PGC_GPU_PDN);
  287. /* Read ISO and ISO2SW power down delays */
  288. val = readl_relaxed(gpc_base + GPC_PGC_GPU_PUPSCR);
  289. sw = val & 0x3f;
  290. sw2iso = (val >> 8) & 0x3f;
  291. /* Request GPC to power up GPU/VPU */
  292. val = readl_relaxed(gpc_base + GPC_CNTR);
  293. val |= GPU_VPU_PUP_REQ;
  294. writel_relaxed(val, gpc_base + GPC_CNTR);
  295. /* Wait ISO + ISO2SW IPG clock cycles */
  296. ndelay((sw + sw2iso) * 1000 / 66);
  297. /* Disable reset clocks for all devices in the PU domain */
  298. for (i = 0; i < pu->num_clks; i++)
  299. clk_disable_unprepare(pu->clk[i]);
  300. return 0;
  301. }
  302. static struct generic_pm_domain imx6q_arm_domain = {
  303. .name = "ARM",
  304. };
  305. static struct pu_domain imx6q_pu_domain = {
  306. .base = {
  307. .name = "PU",
  308. .power_off = imx6q_pm_pu_power_off,
  309. .power_on = imx6q_pm_pu_power_on,
  310. .states = {
  311. [0] = {
  312. .power_off_latency_ns = 25000,
  313. .power_on_latency_ns = 2000000,
  314. },
  315. },
  316. .state_count = 1,
  317. },
  318. };
  319. static struct generic_pm_domain imx6sl_display_domain = {
  320. .name = "DISPLAY",
  321. };
  322. static struct generic_pm_domain *imx_gpc_domains[] = {
  323. &imx6q_arm_domain,
  324. &imx6q_pu_domain.base,
  325. &imx6sl_display_domain,
  326. };
  327. static struct genpd_onecell_data imx_gpc_onecell_data = {
  328. .domains = imx_gpc_domains,
  329. .num_domains = ARRAY_SIZE(imx_gpc_domains),
  330. };
  331. static int imx_gpc_genpd_init(struct device *dev, struct regulator *pu_reg)
  332. {
  333. struct clk *clk;
  334. int i, ret;
  335. imx6q_pu_domain.reg = pu_reg;
  336. for (i = 0; ; i++) {
  337. clk = of_clk_get(dev->of_node, i);
  338. if (IS_ERR(clk))
  339. break;
  340. if (i >= GPC_CLK_MAX) {
  341. dev_err(dev, "more than %d clocks\n", GPC_CLK_MAX);
  342. goto clk_err;
  343. }
  344. imx6q_pu_domain.clk[i] = clk;
  345. }
  346. imx6q_pu_domain.num_clks = i;
  347. /* Enable power always in case bootloader disabled it. */
  348. imx6q_pm_pu_power_on(&imx6q_pu_domain.base);
  349. if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS))
  350. return 0;
  351. for (i = 0; i < ARRAY_SIZE(imx_gpc_domains); i++)
  352. pm_genpd_init(imx_gpc_domains[i], NULL, false);
  353. ret = of_genpd_add_provider_onecell(dev->of_node,
  354. &imx_gpc_onecell_data);
  355. if (ret)
  356. goto power_off;
  357. return 0;
  358. power_off:
  359. imx6q_pm_pu_power_off(&imx6q_pu_domain.base);
  360. clk_err:
  361. while (i--)
  362. clk_put(imx6q_pu_domain.clk[i]);
  363. imx6q_pu_domain.reg = NULL;
  364. return -EINVAL;
  365. }
  366. static int imx_gpc_probe(struct platform_device *pdev)
  367. {
  368. struct regulator *pu_reg;
  369. int ret;
  370. /* bail out if DT too old and doesn't provide the necessary info */
  371. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells"))
  372. return 0;
  373. pu_reg = devm_regulator_get_optional(&pdev->dev, "pu");
  374. if (PTR_ERR(pu_reg) == -ENODEV)
  375. pu_reg = NULL;
  376. if (IS_ERR(pu_reg)) {
  377. ret = PTR_ERR(pu_reg);
  378. dev_err(&pdev->dev, "failed to get pu regulator: %d\n", ret);
  379. return ret;
  380. }
  381. return imx_gpc_genpd_init(&pdev->dev, pu_reg);
  382. }
  383. static const struct of_device_id imx_gpc_dt_ids[] = {
  384. { .compatible = "fsl,imx6q-gpc" },
  385. { .compatible = "fsl,imx6sl-gpc" },
  386. { }
  387. };
  388. static struct platform_driver imx_gpc_driver = {
  389. .driver = {
  390. .name = "imx-gpc",
  391. .of_match_table = imx_gpc_dt_ids,
  392. },
  393. .probe = imx_gpc_probe,
  394. };
  395. static int __init imx_pgc_init(void)
  396. {
  397. return platform_driver_register(&imx_gpc_driver);
  398. }
  399. subsys_initcall(imx_pgc_init);