pmu.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. 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(sizeof(*pmu), GFP_KERNEL);
  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(sizeof(*domain), GFP_KERNEL);
  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, *np;
  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("%s: failed to find domains sub-node\n", np_pmu->name);
  321. return 0;
  322. }
  323. pmu = kzalloc(sizeof(*pmu), GFP_KERNEL);
  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("%s: failed to map PMU\n", np_pmu->name);
  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(domains_node, np) {
  339. struct of_phandle_args args;
  340. struct pmu_domain *domain;
  341. domain = kzalloc(sizeof(*domain), GFP_KERNEL);
  342. if (!domain)
  343. break;
  344. domain->pmu = pmu;
  345. domain->base.name = kstrdup(np->name, GFP_KERNEL);
  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("%s: no interrupt specified\n", np_pmu->name);
  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. }