rcar-sysc.c 12 KB

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