rcar-sysc.c 13 KB

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