gpc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. return 0;
  227. }
  228. IRQCHIP_DECLARE(imx_gpc, "fsl,imx6q-gpc", imx_gpc_init);
  229. void __init imx_gpc_check_dt(void)
  230. {
  231. struct device_node *np;
  232. np = of_find_compatible_node(NULL, NULL, "fsl,imx6q-gpc");
  233. if (WARN_ON(!np))
  234. return;
  235. if (WARN_ON(!of_find_property(np, "interrupt-controller", NULL))) {
  236. pr_warn("Outdated DT detected, suspend/resume will NOT work\n");
  237. /* map GPC, so that at least CPUidle and WARs keep working */
  238. gpc_base = of_iomap(np, 0);
  239. }
  240. }
  241. static void _imx6q_pm_pu_power_off(struct generic_pm_domain *genpd)
  242. {
  243. int iso, iso2sw;
  244. u32 val;
  245. /* Read ISO and ISO2SW power down delays */
  246. val = readl_relaxed(gpc_base + GPC_PGC_GPU_PDNSCR);
  247. iso = val & 0x3f;
  248. iso2sw = (val >> 8) & 0x3f;
  249. /* Gate off PU domain when GPU/VPU when powered down */
  250. writel_relaxed(0x1, gpc_base + GPC_PGC_GPU_PDN);
  251. /* Request GPC to power down GPU/VPU */
  252. val = readl_relaxed(gpc_base + GPC_CNTR);
  253. val |= GPU_VPU_PDN_REQ;
  254. writel_relaxed(val, gpc_base + GPC_CNTR);
  255. /* Wait ISO + ISO2SW IPG clock cycles */
  256. ndelay((iso + iso2sw) * 1000 / 66);
  257. }
  258. static int imx6q_pm_pu_power_off(struct generic_pm_domain *genpd)
  259. {
  260. struct pu_domain *pu = container_of(genpd, struct pu_domain, base);
  261. _imx6q_pm_pu_power_off(genpd);
  262. if (pu->reg)
  263. regulator_disable(pu->reg);
  264. return 0;
  265. }
  266. static int imx6q_pm_pu_power_on(struct generic_pm_domain *genpd)
  267. {
  268. struct pu_domain *pu = container_of(genpd, struct pu_domain, base);
  269. int i, ret, sw, sw2iso;
  270. u32 val;
  271. if (pu->reg)
  272. ret = regulator_enable(pu->reg);
  273. if (pu->reg && ret) {
  274. pr_err("%s: failed to enable regulator: %d\n", __func__, ret);
  275. return ret;
  276. }
  277. /* Enable reset clocks for all devices in the PU domain */
  278. for (i = 0; i < pu->num_clks; i++)
  279. clk_prepare_enable(pu->clk[i]);
  280. /* Gate off PU domain when GPU/VPU when powered down */
  281. writel_relaxed(0x1, gpc_base + GPC_PGC_GPU_PDN);
  282. /* Read ISO and ISO2SW power down delays */
  283. val = readl_relaxed(gpc_base + GPC_PGC_GPU_PUPSCR);
  284. sw = val & 0x3f;
  285. sw2iso = (val >> 8) & 0x3f;
  286. /* Request GPC to power up GPU/VPU */
  287. val = readl_relaxed(gpc_base + GPC_CNTR);
  288. val |= GPU_VPU_PUP_REQ;
  289. writel_relaxed(val, gpc_base + GPC_CNTR);
  290. /* Wait ISO + ISO2SW IPG clock cycles */
  291. ndelay((sw + sw2iso) * 1000 / 66);
  292. /* Disable reset clocks for all devices in the PU domain */
  293. for (i = 0; i < pu->num_clks; i++)
  294. clk_disable_unprepare(pu->clk[i]);
  295. return 0;
  296. }
  297. static struct generic_pm_domain imx6q_arm_domain = {
  298. .name = "ARM",
  299. };
  300. static struct pu_domain imx6q_pu_domain = {
  301. .base = {
  302. .name = "PU",
  303. .power_off = imx6q_pm_pu_power_off,
  304. .power_on = imx6q_pm_pu_power_on,
  305. .power_off_latency_ns = 25000,
  306. .power_on_latency_ns = 2000000,
  307. },
  308. };
  309. static struct generic_pm_domain imx6sl_display_domain = {
  310. .name = "DISPLAY",
  311. };
  312. static struct generic_pm_domain *imx_gpc_domains[] = {
  313. &imx6q_arm_domain,
  314. &imx6q_pu_domain.base,
  315. &imx6sl_display_domain,
  316. };
  317. static struct genpd_onecell_data imx_gpc_onecell_data = {
  318. .domains = imx_gpc_domains,
  319. .num_domains = ARRAY_SIZE(imx_gpc_domains),
  320. };
  321. static int imx_gpc_genpd_init(struct device *dev, struct regulator *pu_reg)
  322. {
  323. struct clk *clk;
  324. int i;
  325. imx6q_pu_domain.reg = pu_reg;
  326. for (i = 0; ; i++) {
  327. clk = of_clk_get(dev->of_node, i);
  328. if (IS_ERR(clk))
  329. break;
  330. if (i >= GPC_CLK_MAX) {
  331. dev_err(dev, "more than %d clocks\n", GPC_CLK_MAX);
  332. goto clk_err;
  333. }
  334. imx6q_pu_domain.clk[i] = clk;
  335. }
  336. imx6q_pu_domain.num_clks = i;
  337. /* Enable power always in case bootloader disabled it. */
  338. imx6q_pm_pu_power_on(&imx6q_pu_domain.base);
  339. if (!IS_ENABLED(CONFIG_PM_GENERIC_DOMAINS))
  340. return 0;
  341. pm_genpd_init(&imx6q_pu_domain.base, NULL, false);
  342. return of_genpd_add_provider_onecell(dev->of_node,
  343. &imx_gpc_onecell_data);
  344. clk_err:
  345. while (i--)
  346. clk_put(imx6q_pu_domain.clk[i]);
  347. return -EINVAL;
  348. }
  349. static int imx_gpc_probe(struct platform_device *pdev)
  350. {
  351. struct regulator *pu_reg;
  352. int ret;
  353. /* bail out if DT too old and doesn't provide the necessary info */
  354. if (!of_property_read_bool(pdev->dev.of_node, "#power-domain-cells"))
  355. return 0;
  356. pu_reg = devm_regulator_get_optional(&pdev->dev, "pu");
  357. if (PTR_ERR(pu_reg) == -ENODEV)
  358. pu_reg = NULL;
  359. if (IS_ERR(pu_reg)) {
  360. ret = PTR_ERR(pu_reg);
  361. dev_err(&pdev->dev, "failed to get pu regulator: %d\n", ret);
  362. return ret;
  363. }
  364. return imx_gpc_genpd_init(&pdev->dev, pu_reg);
  365. }
  366. static const struct of_device_id imx_gpc_dt_ids[] = {
  367. { .compatible = "fsl,imx6q-gpc" },
  368. { .compatible = "fsl,imx6sl-gpc" },
  369. { }
  370. };
  371. static struct platform_driver imx_gpc_driver = {
  372. .driver = {
  373. .name = "imx-gpc",
  374. .of_match_table = imx_gpc_dt_ids,
  375. },
  376. .probe = imx_gpc_probe,
  377. };
  378. static int __init imx_pgc_init(void)
  379. {
  380. return platform_driver_register(&imx_gpc_driver);
  381. }
  382. subsys_initcall(imx_pgc_init);