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