rcar-sysc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * R-Car SYSC Power management support
  4. *
  5. * Copyright (C) 2014 Magnus Damm
  6. * Copyright (C) 2015-2017 Glider bvba
  7. */
  8. #include <linux/clk/renesas.h>
  9. #include <linux/delay.h>
  10. #include <linux/err.h>
  11. #include <linux/mm.h>
  12. #include <linux/of_address.h>
  13. #include <linux/pm_domain.h>
  14. #include <linux/slab.h>
  15. #include <linux/spinlock.h>
  16. #include <linux/io.h>
  17. #include <linux/iopoll.h>
  18. #include <linux/soc/renesas/rcar-sysc.h>
  19. #include "rcar-sysc.h"
  20. /* SYSC Common */
  21. #define SYSCSR 0x00 /* SYSC Status Register */
  22. #define SYSCISR 0x04 /* Interrupt Status Register */
  23. #define SYSCISCR 0x08 /* Interrupt Status Clear Register */
  24. #define SYSCIER 0x0c /* Interrupt Enable Register */
  25. #define SYSCIMR 0x10 /* Interrupt Mask Register */
  26. /* SYSC Status Register */
  27. #define SYSCSR_PONENB 1 /* Ready for power resume requests */
  28. #define SYSCSR_POFFENB 0 /* Ready for power shutoff requests */
  29. /*
  30. * Power Control Register Offsets inside the register block for each domain
  31. * Note: The "CR" registers for ARM cores exist on H1 only
  32. * Use WFI to power off, CPG/APMU to resume ARM cores on R-Car Gen2
  33. * Use PSCI on R-Car Gen3
  34. */
  35. #define PWRSR_OFFS 0x00 /* Power Status Register */
  36. #define PWROFFCR_OFFS 0x04 /* Power Shutoff Control Register */
  37. #define PWROFFSR_OFFS 0x08 /* Power Shutoff Status Register */
  38. #define PWRONCR_OFFS 0x0c /* Power Resume Control Register */
  39. #define PWRONSR_OFFS 0x10 /* Power Resume Status Register */
  40. #define PWRER_OFFS 0x14 /* Power Shutoff/Resume Error */
  41. #define SYSCSR_TIMEOUT 1000
  42. #define SYSCSR_DELAY_US 1
  43. #define PWRER_RETRIES 1000
  44. #define PWRER_DELAY_US 1
  45. #define SYSCISR_TIMEOUT 1000
  46. #define SYSCISR_DELAY_US 1
  47. #define RCAR_PD_ALWAYS_ON 32 /* Always-on power area */
  48. struct rcar_sysc_pd {
  49. struct generic_pm_domain genpd;
  50. u16 chan_offs;
  51. u8 chan_bit;
  52. u8 isr_bit;
  53. unsigned int flags;
  54. char name[];
  55. };
  56. static void __iomem *rcar_sysc_base;
  57. static DEFINE_SPINLOCK(rcar_sysc_lock); /* SMP CPUs + I/O devices */
  58. static u32 rcar_sysc_extmask_offs, rcar_sysc_extmask_val;
  59. static int rcar_sysc_pwr_on_off(const struct rcar_sysc_pd *pd, bool on)
  60. {
  61. unsigned int sr_bit, reg_offs;
  62. u32 val;
  63. int ret;
  64. if (on) {
  65. sr_bit = SYSCSR_PONENB;
  66. reg_offs = PWRONCR_OFFS;
  67. } else {
  68. sr_bit = SYSCSR_POFFENB;
  69. reg_offs = PWROFFCR_OFFS;
  70. }
  71. /* Wait until SYSC is ready to accept a power request */
  72. ret = readl_poll_timeout_atomic(rcar_sysc_base + SYSCSR, val,
  73. val & BIT(sr_bit), SYSCSR_DELAY_US,
  74. SYSCSR_TIMEOUT);
  75. if (ret)
  76. return -EAGAIN;
  77. /* Power-off delay quirk */
  78. if (!on && (pd->flags & PD_OFF_DELAY))
  79. udelay(1);
  80. /* Submit power shutoff or power resume request */
  81. iowrite32(BIT(pd->chan_bit), rcar_sysc_base + pd->chan_offs + reg_offs);
  82. return 0;
  83. }
  84. static int rcar_sysc_power(const struct rcar_sysc_pd *pd, bool on)
  85. {
  86. unsigned int isr_mask = BIT(pd->isr_bit);
  87. unsigned int chan_mask = BIT(pd->chan_bit);
  88. unsigned int status, k;
  89. unsigned long flags;
  90. int ret;
  91. spin_lock_irqsave(&rcar_sysc_lock, flags);
  92. /*
  93. * Mask external power requests for CPU or 3DG domains
  94. */
  95. if (rcar_sysc_extmask_val) {
  96. iowrite32(rcar_sysc_extmask_val,
  97. rcar_sysc_base + rcar_sysc_extmask_offs);
  98. }
  99. /*
  100. * The interrupt source needs to be enabled, but masked, to prevent the
  101. * CPU from receiving it.
  102. */
  103. iowrite32(ioread32(rcar_sysc_base + SYSCIMR) | isr_mask,
  104. rcar_sysc_base + SYSCIMR);
  105. iowrite32(ioread32(rcar_sysc_base + SYSCIER) | isr_mask,
  106. rcar_sysc_base + SYSCIER);
  107. iowrite32(isr_mask, rcar_sysc_base + SYSCISCR);
  108. /* Submit power shutoff or resume request until it was accepted */
  109. for (k = 0; k < PWRER_RETRIES; k++) {
  110. ret = rcar_sysc_pwr_on_off(pd, on);
  111. if (ret)
  112. goto out;
  113. status = ioread32(rcar_sysc_base + pd->chan_offs + PWRER_OFFS);
  114. if (!(status & chan_mask))
  115. break;
  116. udelay(PWRER_DELAY_US);
  117. }
  118. if (k == PWRER_RETRIES) {
  119. ret = -EIO;
  120. goto out;
  121. }
  122. /* Wait until the power shutoff or resume request has completed * */
  123. ret = readl_poll_timeout_atomic(rcar_sysc_base + SYSCISR, status,
  124. status & isr_mask, SYSCISR_DELAY_US,
  125. SYSCISR_TIMEOUT);
  126. if (ret)
  127. ret = -EIO;
  128. iowrite32(isr_mask, rcar_sysc_base + SYSCISCR);
  129. out:
  130. if (rcar_sysc_extmask_val)
  131. iowrite32(0, rcar_sysc_base + rcar_sysc_extmask_offs);
  132. spin_unlock_irqrestore(&rcar_sysc_lock, flags);
  133. pr_debug("sysc power %s domain %d: %08x -> %d\n", on ? "on" : "off",
  134. pd->isr_bit, ioread32(rcar_sysc_base + SYSCISR), ret);
  135. return ret;
  136. }
  137. static bool rcar_sysc_power_is_off(const struct rcar_sysc_pd *pd)
  138. {
  139. unsigned int st;
  140. st = ioread32(rcar_sysc_base + pd->chan_offs + PWRSR_OFFS);
  141. if (st & BIT(pd->chan_bit))
  142. return true;
  143. return false;
  144. }
  145. static inline struct rcar_sysc_pd *to_rcar_pd(struct generic_pm_domain *d)
  146. {
  147. return container_of(d, struct rcar_sysc_pd, genpd);
  148. }
  149. static int rcar_sysc_pd_power_off(struct generic_pm_domain *genpd)
  150. {
  151. struct rcar_sysc_pd *pd = to_rcar_pd(genpd);
  152. pr_debug("%s: %s\n", __func__, genpd->name);
  153. return rcar_sysc_power(pd, false);
  154. }
  155. static int rcar_sysc_pd_power_on(struct generic_pm_domain *genpd)
  156. {
  157. struct rcar_sysc_pd *pd = to_rcar_pd(genpd);
  158. pr_debug("%s: %s\n", __func__, genpd->name);
  159. return rcar_sysc_power(pd, true);
  160. }
  161. static bool has_cpg_mstp;
  162. static int __init rcar_sysc_pd_setup(struct rcar_sysc_pd *pd)
  163. {
  164. struct generic_pm_domain *genpd = &pd->genpd;
  165. const char *name = pd->genpd.name;
  166. int error;
  167. if (pd->flags & PD_CPU) {
  168. /*
  169. * This domain contains a CPU core and therefore it should
  170. * only be turned off if the CPU is not in use.
  171. */
  172. pr_debug("PM domain %s contains %s\n", name, "CPU");
  173. genpd->flags |= GENPD_FLAG_ALWAYS_ON;
  174. } else if (pd->flags & PD_SCU) {
  175. /*
  176. * This domain contains an SCU and cache-controller, and
  177. * therefore it should only be turned off if the CPU cores are
  178. * not in use.
  179. */
  180. pr_debug("PM domain %s contains %s\n", name, "SCU");
  181. genpd->flags |= GENPD_FLAG_ALWAYS_ON;
  182. } else if (pd->flags & PD_NO_CR) {
  183. /*
  184. * This domain cannot be turned off.
  185. */
  186. genpd->flags |= GENPD_FLAG_ALWAYS_ON;
  187. }
  188. if (!(pd->flags & (PD_CPU | PD_SCU))) {
  189. /* Enable Clock Domain for I/O devices */
  190. genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
  191. if (has_cpg_mstp) {
  192. genpd->attach_dev = cpg_mstp_attach_dev;
  193. genpd->detach_dev = cpg_mstp_detach_dev;
  194. } else {
  195. genpd->attach_dev = cpg_mssr_attach_dev;
  196. genpd->detach_dev = cpg_mssr_detach_dev;
  197. }
  198. }
  199. genpd->power_off = rcar_sysc_pd_power_off;
  200. genpd->power_on = rcar_sysc_pd_power_on;
  201. if (pd->flags & (PD_CPU | PD_NO_CR)) {
  202. /* Skip CPUs (handled by SMP code) and areas without control */
  203. pr_debug("%s: Not touching %s\n", __func__, genpd->name);
  204. goto finalize;
  205. }
  206. if (!rcar_sysc_power_is_off(pd)) {
  207. pr_debug("%s: %s is already powered\n", __func__, genpd->name);
  208. goto finalize;
  209. }
  210. rcar_sysc_power(pd, true);
  211. finalize:
  212. error = pm_genpd_init(genpd, &simple_qos_governor, false);
  213. if (error)
  214. pr_err("Failed to init PM domain %s: %d\n", name, error);
  215. return error;
  216. }
  217. static const struct of_device_id rcar_sysc_matches[] __initconst = {
  218. #ifdef CONFIG_SYSC_R8A7742
  219. { .compatible = "renesas,r8a7742-sysc", .data = &r8a7742_sysc_info },
  220. #endif
  221. #ifdef CONFIG_SYSC_R8A7743
  222. { .compatible = "renesas,r8a7743-sysc", .data = &r8a7743_sysc_info },
  223. /* RZ/G1N is identical to RZ/G2M w.r.t. power domains. */
  224. { .compatible = "renesas,r8a7744-sysc", .data = &r8a7743_sysc_info },
  225. #endif
  226. #ifdef CONFIG_SYSC_R8A7745
  227. { .compatible = "renesas,r8a7745-sysc", .data = &r8a7745_sysc_info },
  228. #endif
  229. #ifdef CONFIG_SYSC_R8A77470
  230. { .compatible = "renesas,r8a77470-sysc", .data = &r8a77470_sysc_info },
  231. #endif
  232. #ifdef CONFIG_SYSC_R8A774A1
  233. { .compatible = "renesas,r8a774a1-sysc", .data = &r8a774a1_sysc_info },
  234. #endif
  235. #ifdef CONFIG_SYSC_R8A774B1
  236. { .compatible = "renesas,r8a774b1-sysc", .data = &r8a774b1_sysc_info },
  237. #endif
  238. #ifdef CONFIG_SYSC_R8A774C0
  239. { .compatible = "renesas,r8a774c0-sysc", .data = &r8a774c0_sysc_info },
  240. #endif
  241. #ifdef CONFIG_SYSC_R8A774E1
  242. { .compatible = "renesas,r8a774e1-sysc", .data = &r8a774e1_sysc_info },
  243. #endif
  244. #ifdef CONFIG_SYSC_R8A7779
  245. { .compatible = "renesas,r8a7779-sysc", .data = &r8a7779_sysc_info },
  246. #endif
  247. #ifdef CONFIG_SYSC_R8A7790
  248. { .compatible = "renesas,r8a7790-sysc", .data = &r8a7790_sysc_info },
  249. #endif
  250. #ifdef CONFIG_SYSC_R8A7791
  251. { .compatible = "renesas,r8a7791-sysc", .data = &r8a7791_sysc_info },
  252. /* R-Car M2-N is identical to R-Car M2-W w.r.t. power domains. */
  253. { .compatible = "renesas,r8a7793-sysc", .data = &r8a7791_sysc_info },
  254. #endif
  255. #ifdef CONFIG_SYSC_R8A7792
  256. { .compatible = "renesas,r8a7792-sysc", .data = &r8a7792_sysc_info },
  257. #endif
  258. #ifdef CONFIG_SYSC_R8A7794
  259. { .compatible = "renesas,r8a7794-sysc", .data = &r8a7794_sysc_info },
  260. #endif
  261. #ifdef CONFIG_SYSC_R8A7795
  262. { .compatible = "renesas,r8a7795-sysc", .data = &r8a7795_sysc_info },
  263. #endif
  264. #ifdef CONFIG_SYSC_R8A77960
  265. { .compatible = "renesas,r8a7796-sysc", .data = &r8a77960_sysc_info },
  266. #endif
  267. #ifdef CONFIG_SYSC_R8A77961
  268. { .compatible = "renesas,r8a77961-sysc", .data = &r8a77961_sysc_info },
  269. #endif
  270. #ifdef CONFIG_SYSC_R8A77965
  271. { .compatible = "renesas,r8a77965-sysc", .data = &r8a77965_sysc_info },
  272. #endif
  273. #ifdef CONFIG_SYSC_R8A77970
  274. { .compatible = "renesas,r8a77970-sysc", .data = &r8a77970_sysc_info },
  275. #endif
  276. #ifdef CONFIG_SYSC_R8A77980
  277. { .compatible = "renesas,r8a77980-sysc", .data = &r8a77980_sysc_info },
  278. #endif
  279. #ifdef CONFIG_SYSC_R8A77990
  280. { .compatible = "renesas,r8a77990-sysc", .data = &r8a77990_sysc_info },
  281. #endif
  282. #ifdef CONFIG_SYSC_R8A77995
  283. { .compatible = "renesas,r8a77995-sysc", .data = &r8a77995_sysc_info },
  284. #endif
  285. { /* sentinel */ }
  286. };
  287. struct rcar_pm_domains {
  288. struct genpd_onecell_data onecell_data;
  289. struct generic_pm_domain *domains[RCAR_PD_ALWAYS_ON + 1];
  290. };
  291. static struct genpd_onecell_data *rcar_sysc_onecell_data;
  292. static int __init rcar_sysc_pd_init(void)
  293. {
  294. const struct rcar_sysc_info *info;
  295. const struct of_device_id *match;
  296. struct rcar_pm_domains *domains;
  297. struct device_node *np;
  298. void __iomem *base;
  299. unsigned int i;
  300. int error;
  301. np = of_find_matching_node_and_match(NULL, rcar_sysc_matches, &match);
  302. if (!np)
  303. return -ENODEV;
  304. info = match->data;
  305. if (info->init) {
  306. error = info->init();
  307. if (error)
  308. goto out_put;
  309. }
  310. has_cpg_mstp = of_find_compatible_node(NULL, NULL,
  311. "renesas,cpg-mstp-clocks");
  312. base = of_iomap(np, 0);
  313. if (!base) {
  314. pr_warn("%pOF: Cannot map regs\n", np);
  315. error = -ENOMEM;
  316. goto out_put;
  317. }
  318. rcar_sysc_base = base;
  319. /* Optional External Request Mask Register */
  320. rcar_sysc_extmask_offs = info->extmask_offs;
  321. rcar_sysc_extmask_val = info->extmask_val;
  322. domains = kzalloc(sizeof(*domains), GFP_KERNEL);
  323. if (!domains) {
  324. error = -ENOMEM;
  325. goto out_put;
  326. }
  327. domains->onecell_data.domains = domains->domains;
  328. domains->onecell_data.num_domains = ARRAY_SIZE(domains->domains);
  329. rcar_sysc_onecell_data = &domains->onecell_data;
  330. for (i = 0; i < info->num_areas; i++) {
  331. const struct rcar_sysc_area *area = &info->areas[i];
  332. struct rcar_sysc_pd *pd;
  333. size_t n;
  334. n = strlen(area->name) + 1;
  335. pd = kzalloc(sizeof(*pd) + n, GFP_KERNEL);
  336. if (!pd) {
  337. error = -ENOMEM;
  338. goto out_put;
  339. }
  340. memcpy(pd->name, area->name, n);
  341. pd->genpd.name = pd->name;
  342. pd->chan_offs = area->chan_offs;
  343. pd->chan_bit = area->chan_bit;
  344. pd->isr_bit = area->isr_bit;
  345. pd->flags = area->flags;
  346. error = rcar_sysc_pd_setup(pd);
  347. if (error)
  348. goto out_put;
  349. domains->domains[area->isr_bit] = &pd->genpd;
  350. if (area->parent < 0)
  351. continue;
  352. error = pm_genpd_add_subdomain(domains->domains[area->parent],
  353. &pd->genpd);
  354. if (error) {
  355. pr_warn("Failed to add PM subdomain %s to parent %u\n",
  356. area->name, area->parent);
  357. goto out_put;
  358. }
  359. }
  360. error = of_genpd_add_provider_onecell(np, &domains->onecell_data);
  361. if (!error)
  362. fwnode_dev_initialized(of_fwnode_handle(np), true);
  363. out_put:
  364. of_node_put(np);
  365. return error;
  366. }
  367. early_initcall(rcar_sysc_pd_init);
  368. #ifdef CONFIG_ARCH_R8A7779
  369. static int rcar_sysc_power_cpu(unsigned int idx, bool on)
  370. {
  371. struct generic_pm_domain *genpd;
  372. struct rcar_sysc_pd *pd;
  373. unsigned int i;
  374. if (!rcar_sysc_onecell_data)
  375. return -ENODEV;
  376. for (i = 0; i < rcar_sysc_onecell_data->num_domains; i++) {
  377. genpd = rcar_sysc_onecell_data->domains[i];
  378. if (!genpd)
  379. continue;
  380. pd = to_rcar_pd(genpd);
  381. if (!(pd->flags & PD_CPU) || pd->chan_bit != idx)
  382. continue;
  383. return rcar_sysc_power(pd, on);
  384. }
  385. return -ENOENT;
  386. }
  387. int rcar_sysc_power_down_cpu(unsigned int cpu)
  388. {
  389. return rcar_sysc_power_cpu(cpu, false);
  390. }
  391. int rcar_sysc_power_up_cpu(unsigned int cpu)
  392. {
  393. return rcar_sysc_power_cpu(cpu, true);
  394. }
  395. #endif /* CONFIG_ARCH_R8A7779 */