pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Marvell Dove PMU support
  4. */
  5. #include <linux/io.h>
  6. #include <linux/irq.h>
  7. #include <linux/irqdomain.h>
  8. #include <linux/of.h>
  9. #include <linux/of_irq.h>
  10. #include <linux/of_address.h>
  11. #include <linux/platform_device.h>
  12. #include <linux/pm_domain.h>
  13. #include <linux/reset.h>
  14. #include <linux/reset-controller.h>
  15. #include <linux/sched.h>
  16. #include <linux/slab.h>
  17. #include <linux/soc/dove/pmu.h>
  18. #include <linux/spinlock.h>
  19. #define NR_PMU_IRQS 7
  20. #define PMC_SW_RST 0x30
  21. #define PMC_IRQ_CAUSE 0x50
  22. #define PMC_IRQ_MASK 0x54
  23. #define PMU_PWR 0x10
  24. #define PMU_ISO 0x58
  25. struct pmu_data {
  26. spinlock_t lock;
  27. struct device_node *of_node;
  28. void __iomem *pmc_base;
  29. void __iomem *pmu_base;
  30. struct irq_chip_generic *irq_gc;
  31. struct irq_domain *irq_domain;
  32. #ifdef CONFIG_RESET_CONTROLLER
  33. struct reset_controller_dev reset;
  34. #endif
  35. };
  36. /*
  37. * The PMU contains a register to reset various subsystems within the
  38. * SoC. Export this as a reset controller.
  39. */
  40. #ifdef CONFIG_RESET_CONTROLLER
  41. #define rcdev_to_pmu(rcdev) container_of(rcdev, struct pmu_data, reset)
  42. static int pmu_reset_reset(struct reset_controller_dev *rc, unsigned long id)
  43. {
  44. struct pmu_data *pmu = rcdev_to_pmu(rc);
  45. unsigned long flags;
  46. u32 val;
  47. spin_lock_irqsave(&pmu->lock, flags);
  48. val = readl_relaxed(pmu->pmc_base + PMC_SW_RST);
  49. writel_relaxed(val & ~BIT(id), pmu->pmc_base + PMC_SW_RST);
  50. writel_relaxed(val | BIT(id), pmu->pmc_base + PMC_SW_RST);
  51. spin_unlock_irqrestore(&pmu->lock, flags);
  52. return 0;
  53. }
  54. static int pmu_reset_assert(struct reset_controller_dev *rc, unsigned long id)
  55. {
  56. struct pmu_data *pmu = rcdev_to_pmu(rc);
  57. unsigned long flags;
  58. u32 val = ~BIT(id);
  59. spin_lock_irqsave(&pmu->lock, flags);
  60. val &= readl_relaxed(pmu->pmc_base + PMC_SW_RST);
  61. writel_relaxed(val, pmu->pmc_base + PMC_SW_RST);
  62. spin_unlock_irqrestore(&pmu->lock, flags);
  63. return 0;
  64. }
  65. static int pmu_reset_deassert(struct reset_controller_dev *rc, unsigned long id)
  66. {
  67. struct pmu_data *pmu = rcdev_to_pmu(rc);
  68. unsigned long flags;
  69. u32 val = BIT(id);
  70. spin_lock_irqsave(&pmu->lock, flags);
  71. val |= readl_relaxed(pmu->pmc_base + PMC_SW_RST);
  72. writel_relaxed(val, pmu->pmc_base + PMC_SW_RST);
  73. spin_unlock_irqrestore(&pmu->lock, flags);
  74. return 0;
  75. }
  76. static const struct reset_control_ops pmu_reset_ops = {
  77. .reset = pmu_reset_reset,
  78. .assert = pmu_reset_assert,
  79. .deassert = pmu_reset_deassert,
  80. };
  81. static struct reset_controller_dev pmu_reset __initdata = {
  82. .ops = &pmu_reset_ops,
  83. .owner = THIS_MODULE,
  84. .nr_resets = 32,
  85. };
  86. static void __init pmu_reset_init(struct pmu_data *pmu)
  87. {
  88. int ret;
  89. pmu->reset = pmu_reset;
  90. pmu->reset.of_node = pmu->of_node;
  91. ret = reset_controller_register(&pmu->reset);
  92. if (ret)
  93. pr_err("pmu: %s failed: %d\n", "reset_controller_register", ret);
  94. }
  95. #else
  96. static void __init pmu_reset_init(struct pmu_data *pmu)
  97. {
  98. }
  99. #endif
  100. struct pmu_domain {
  101. struct pmu_data *pmu;
  102. u32 pwr_mask;
  103. u32 rst_mask;
  104. u32 iso_mask;
  105. struct generic_pm_domain base;
  106. };
  107. #define to_pmu_domain(dom) container_of(dom, struct pmu_domain, base)
  108. /*
  109. * This deals with the "old" Marvell sequence of bringing a power domain
  110. * down/up, which is: apply power, release reset, disable isolators.
  111. *
  112. * Later devices apparantly use a different sequence: power up, disable
  113. * isolators, assert repair signal, enable SRMA clock, enable AXI clock,
  114. * enable module clock, deassert reset.
  115. *
  116. * Note: reading the assembly, it seems that the IO accessors have an
  117. * unfortunate side-effect - they cause memory already read into registers
  118. * for the if () to be re-read for the bit-set or bit-clear operation.
  119. * The code is written to avoid this.
  120. */
  121. static int pmu_domain_power_off(struct generic_pm_domain *domain)
  122. {
  123. struct pmu_domain *pmu_dom = to_pmu_domain(domain);
  124. struct pmu_data *pmu = pmu_dom->pmu;
  125. unsigned long flags;
  126. unsigned int val;
  127. void __iomem *pmu_base = pmu->pmu_base;
  128. void __iomem *pmc_base = pmu->pmc_base;
  129. spin_lock_irqsave(&pmu->lock, flags);
  130. /* Enable isolators */
  131. if (pmu_dom->iso_mask) {
  132. val = ~pmu_dom->iso_mask;
  133. val &= readl_relaxed(pmu_base + PMU_ISO);
  134. writel_relaxed(val, pmu_base + PMU_ISO);
  135. }
  136. /* Reset unit */
  137. if (pmu_dom->rst_mask) {
  138. val = ~pmu_dom->rst_mask;
  139. val &= readl_relaxed(pmc_base + PMC_SW_RST);
  140. writel_relaxed(val, pmc_base + PMC_SW_RST);
  141. }
  142. /* Power down */
  143. val = readl_relaxed(pmu_base + PMU_PWR) | pmu_dom->pwr_mask;
  144. writel_relaxed(val, pmu_base + PMU_PWR);
  145. spin_unlock_irqrestore(&pmu->lock, flags);
  146. return 0;
  147. }
  148. static int pmu_domain_power_on(struct generic_pm_domain *domain)
  149. {
  150. struct pmu_domain *pmu_dom = to_pmu_domain(domain);
  151. struct pmu_data *pmu = pmu_dom->pmu;
  152. unsigned long flags;
  153. unsigned int val;
  154. void __iomem *pmu_base = pmu->pmu_base;
  155. void __iomem *pmc_base = pmu->pmc_base;
  156. spin_lock_irqsave(&pmu->lock, flags);
  157. /* Power on */
  158. val = ~pmu_dom->pwr_mask & readl_relaxed(pmu_base + PMU_PWR);
  159. writel_relaxed(val, pmu_base + PMU_PWR);
  160. /* Release reset */
  161. if (pmu_dom->rst_mask) {
  162. val = pmu_dom->rst_mask;
  163. val |= readl_relaxed(pmc_base + PMC_SW_RST);
  164. writel_relaxed(val, pmc_base + PMC_SW_RST);
  165. }
  166. /* Disable isolators */
  167. if (pmu_dom->iso_mask) {
  168. val = pmu_dom->iso_mask;
  169. val |= readl_relaxed(pmu_base + PMU_ISO);
  170. writel_relaxed(val, pmu_base + PMU_ISO);
  171. }
  172. spin_unlock_irqrestore(&pmu->lock, flags);
  173. return 0;
  174. }
  175. static void __pmu_domain_register(struct pmu_domain *domain,
  176. struct device_node *np)
  177. {
  178. unsigned int val = readl_relaxed(domain->pmu->pmu_base + PMU_PWR);
  179. domain->base.power_off = pmu_domain_power_off;
  180. domain->base.power_on = pmu_domain_power_on;
  181. pm_genpd_init(&domain->base, NULL, !(val & domain->pwr_mask));
  182. if (np)
  183. of_genpd_add_provider_simple(np, &domain->base);
  184. }
  185. /* PMU IRQ controller */
  186. static void pmu_irq_handler(struct irq_desc *desc)
  187. {
  188. struct pmu_data *pmu = irq_desc_get_handler_data(desc);
  189. struct irq_chip_generic *gc = pmu->irq_gc;
  190. struct irq_domain *domain = pmu->irq_domain;
  191. void __iomem *base = gc->reg_base;
  192. u32 stat = readl_relaxed(base + PMC_IRQ_CAUSE) & gc->mask_cache;
  193. u32 done = ~0;
  194. if (stat == 0) {
  195. handle_bad_irq(desc);
  196. return;
  197. }
  198. while (stat) {
  199. u32 hwirq = fls(stat) - 1;
  200. stat &= ~(1 << hwirq);
  201. done &= ~(1 << hwirq);
  202. generic_handle_irq(irq_find_mapping(domain, hwirq));
  203. }
  204. /*
  205. * The PMU mask register is not RW0C: it is RW. This means that
  206. * the bits take whatever value is written to them; if you write
  207. * a '1', you will set the interrupt.
  208. *
  209. * Unfortunately this means there is NO race free way to clear
  210. * these interrupts.
  211. *
  212. * So, let's structure the code so that the window is as small as
  213. * possible.
  214. */
  215. guard(raw_spinlock)(&gc->lock);
  216. done &= readl_relaxed(base + PMC_IRQ_CAUSE);
  217. writel_relaxed(done, base + PMC_IRQ_CAUSE);
  218. }
  219. static int __init dove_init_pmu_irq(struct pmu_data *pmu, int irq)
  220. {
  221. const char *name = "pmu_irq";
  222. struct irq_chip_generic *gc;
  223. struct irq_domain *domain;
  224. int ret;
  225. /* mask and clear all interrupts */
  226. writel(0, pmu->pmc_base + PMC_IRQ_MASK);
  227. writel(0, pmu->pmc_base + PMC_IRQ_CAUSE);
  228. domain = irq_domain_create_linear(of_fwnode_handle(pmu->of_node), NR_PMU_IRQS,
  229. &irq_generic_chip_ops, NULL);
  230. if (!domain) {
  231. pr_err("%s: unable to add irq domain\n", name);
  232. return -ENOMEM;
  233. }
  234. ret = irq_alloc_domain_generic_chips(domain, NR_PMU_IRQS, 1, name,
  235. handle_level_irq,
  236. IRQ_NOREQUEST | IRQ_NOPROBE, 0,
  237. IRQ_GC_INIT_MASK_CACHE);
  238. if (ret) {
  239. pr_err("%s: unable to alloc irq domain gc: %d\n", name, ret);
  240. irq_domain_remove(domain);
  241. return ret;
  242. }
  243. gc = irq_get_domain_generic_chip(domain, 0);
  244. gc->reg_base = pmu->pmc_base;
  245. gc->chip_types[0].regs.mask = PMC_IRQ_MASK;
  246. gc->chip_types[0].chip.irq_mask = irq_gc_mask_clr_bit;
  247. gc->chip_types[0].chip.irq_unmask = irq_gc_mask_set_bit;
  248. pmu->irq_domain = domain;
  249. pmu->irq_gc = gc;
  250. irq_set_handler_data(irq, pmu);
  251. irq_set_chained_handler(irq, pmu_irq_handler);
  252. return 0;
  253. }
  254. int __init dove_init_pmu_legacy(const struct dove_pmu_initdata *initdata)
  255. {
  256. const struct dove_pmu_domain_initdata *domain_initdata;
  257. struct pmu_data *pmu;
  258. int ret;
  259. pmu = kzalloc_obj(*pmu);
  260. if (!pmu)
  261. return -ENOMEM;
  262. spin_lock_init(&pmu->lock);
  263. pmu->pmc_base = initdata->pmc_base;
  264. pmu->pmu_base = initdata->pmu_base;
  265. pmu_reset_init(pmu);
  266. for (domain_initdata = initdata->domains; domain_initdata->name;
  267. domain_initdata++) {
  268. struct pmu_domain *domain;
  269. domain = kzalloc_obj(*domain);
  270. if (domain) {
  271. domain->pmu = pmu;
  272. domain->pwr_mask = domain_initdata->pwr_mask;
  273. domain->rst_mask = domain_initdata->rst_mask;
  274. domain->iso_mask = domain_initdata->iso_mask;
  275. domain->base.name = domain_initdata->name;
  276. __pmu_domain_register(domain, NULL);
  277. }
  278. }
  279. ret = dove_init_pmu_irq(pmu, initdata->irq);
  280. if (ret)
  281. pr_err("dove_init_pmu_irq() failed: %d\n", ret);
  282. if (pmu->irq_domain)
  283. irq_domain_associate_many(pmu->irq_domain,
  284. initdata->irq_domain_start,
  285. 0, NR_PMU_IRQS);
  286. return 0;
  287. }
  288. /*
  289. * pmu: power-manager@d0000 {
  290. * compatible = "marvell,dove-pmu";
  291. * reg = <0xd0000 0x8000> <0xd8000 0x8000>;
  292. * interrupts = <33>;
  293. * interrupt-controller;
  294. * #reset-cells = 1;
  295. * vpu_domain: vpu-domain {
  296. * #power-domain-cells = <0>;
  297. * marvell,pmu_pwr_mask = <0x00000008>;
  298. * marvell,pmu_iso_mask = <0x00000001>;
  299. * resets = <&pmu 16>;
  300. * };
  301. * gpu_domain: gpu-domain {
  302. * #power-domain-cells = <0>;
  303. * marvell,pmu_pwr_mask = <0x00000004>;
  304. * marvell,pmu_iso_mask = <0x00000002>;
  305. * resets = <&pmu 18>;
  306. * };
  307. * };
  308. */
  309. int __init dove_init_pmu(void)
  310. {
  311. struct device_node *np_pmu, *domains_node;
  312. struct pmu_data *pmu;
  313. int ret, parent_irq;
  314. /* Lookup the PMU node */
  315. np_pmu = of_find_compatible_node(NULL, NULL, "marvell,dove-pmu");
  316. if (!np_pmu)
  317. return 0;
  318. domains_node = of_get_child_by_name(np_pmu, "domains");
  319. if (!domains_node) {
  320. pr_err("%pOFn: failed to find domains sub-node\n", np_pmu);
  321. return 0;
  322. }
  323. pmu = kzalloc_obj(*pmu);
  324. if (!pmu)
  325. return -ENOMEM;
  326. spin_lock_init(&pmu->lock);
  327. pmu->of_node = np_pmu;
  328. pmu->pmc_base = of_iomap(pmu->of_node, 0);
  329. pmu->pmu_base = of_iomap(pmu->of_node, 1);
  330. if (!pmu->pmc_base || !pmu->pmu_base) {
  331. pr_err("%pOFn: failed to map PMU\n", np_pmu);
  332. iounmap(pmu->pmu_base);
  333. iounmap(pmu->pmc_base);
  334. kfree(pmu);
  335. return -ENOMEM;
  336. }
  337. pmu_reset_init(pmu);
  338. for_each_available_child_of_node_scoped(domains_node, np) {
  339. struct of_phandle_args args;
  340. struct pmu_domain *domain;
  341. domain = kzalloc_obj(*domain);
  342. if (!domain)
  343. break;
  344. domain->pmu = pmu;
  345. domain->base.name = kasprintf(GFP_KERNEL, "%pOFn", np);
  346. if (!domain->base.name) {
  347. kfree(domain);
  348. break;
  349. }
  350. of_property_read_u32(np, "marvell,pmu_pwr_mask",
  351. &domain->pwr_mask);
  352. of_property_read_u32(np, "marvell,pmu_iso_mask",
  353. &domain->iso_mask);
  354. /*
  355. * We parse the reset controller property directly here
  356. * to ensure that we can operate when the reset controller
  357. * support is not configured into the kernel.
  358. */
  359. ret = of_parse_phandle_with_args(np, "resets", "#reset-cells",
  360. 0, &args);
  361. if (ret == 0) {
  362. if (args.np == pmu->of_node)
  363. domain->rst_mask = BIT(args.args[0]);
  364. of_node_put(args.np);
  365. }
  366. __pmu_domain_register(domain, np);
  367. }
  368. /* Loss of the interrupt controller is not a fatal error. */
  369. parent_irq = irq_of_parse_and_map(pmu->of_node, 0);
  370. if (!parent_irq) {
  371. pr_err("%pOFn: no interrupt specified\n", np_pmu);
  372. } else {
  373. ret = dove_init_pmu_irq(pmu, parent_irq);
  374. if (ret)
  375. pr_err("dove_init_pmu_irq() failed: %d\n", ret);
  376. }
  377. return 0;
  378. }