pmu.c 10 KB

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