rcar-sysc.c 11 KB

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