rcar-sysc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /*
  2. * R-Car SYSC Power management support
  3. *
  4. * Copyright (C) 2014 Magnus Damm
  5. * Copyright (C) 2015-2016 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. if (pd->flags & PD_NO_CR) {
  147. pr_debug("%s: Cannot control %s\n", __func__, genpd->name);
  148. return -EBUSY;
  149. }
  150. if (pd->flags & PD_BUSY) {
  151. pr_debug("%s: %s busy\n", __func__, genpd->name);
  152. return -EBUSY;
  153. }
  154. return rcar_sysc_power_down(&pd->ch);
  155. }
  156. static int rcar_sysc_pd_power_on(struct generic_pm_domain *genpd)
  157. {
  158. struct rcar_sysc_pd *pd = to_rcar_pd(genpd);
  159. pr_debug("%s: %s\n", __func__, genpd->name);
  160. if (pd->flags & PD_NO_CR) {
  161. pr_debug("%s: Cannot control %s\n", __func__, genpd->name);
  162. return 0;
  163. }
  164. return rcar_sysc_power_up(&pd->ch);
  165. }
  166. static bool has_cpg_mstp;
  167. static void __init rcar_sysc_pd_setup(struct rcar_sysc_pd *pd)
  168. {
  169. struct generic_pm_domain *genpd = &pd->genpd;
  170. const char *name = pd->genpd.name;
  171. struct dev_power_governor *gov = &simple_qos_governor;
  172. if (pd->flags & PD_CPU) {
  173. /*
  174. * This domain contains a CPU core and therefore it should
  175. * only be turned off if the CPU is not in use.
  176. */
  177. pr_debug("PM domain %s contains %s\n", name, "CPU");
  178. pd->flags |= PD_BUSY;
  179. gov = &pm_domain_always_on_gov;
  180. } else if (pd->flags & PD_SCU) {
  181. /*
  182. * This domain contains an SCU and cache-controller, and
  183. * therefore it should only be turned off if the CPU cores are
  184. * not in use.
  185. */
  186. pr_debug("PM domain %s contains %s\n", name, "SCU");
  187. pd->flags |= PD_BUSY;
  188. gov = &pm_domain_always_on_gov;
  189. } else if (pd->flags & PD_NO_CR) {
  190. /*
  191. * This domain cannot be turned off.
  192. */
  193. pd->flags |= PD_BUSY;
  194. gov = &pm_domain_always_on_gov;
  195. }
  196. if (!(pd->flags & (PD_CPU | PD_SCU))) {
  197. /* Enable Clock Domain for I/O devices */
  198. genpd->flags = GENPD_FLAG_PM_CLK;
  199. if (has_cpg_mstp) {
  200. genpd->attach_dev = cpg_mstp_attach_dev;
  201. genpd->detach_dev = cpg_mstp_detach_dev;
  202. } else {
  203. genpd->attach_dev = cpg_mssr_attach_dev;
  204. genpd->detach_dev = cpg_mssr_detach_dev;
  205. }
  206. }
  207. genpd->power_off = rcar_sysc_pd_power_off;
  208. genpd->power_on = rcar_sysc_pd_power_on;
  209. if (pd->flags & (PD_CPU | PD_NO_CR)) {
  210. /* Skip CPUs (handled by SMP code) and areas without control */
  211. pr_debug("%s: Not touching %s\n", __func__, genpd->name);
  212. goto finalize;
  213. }
  214. if (!rcar_sysc_power_is_off(&pd->ch)) {
  215. pr_debug("%s: %s is already powered\n", __func__, genpd->name);
  216. goto finalize;
  217. }
  218. rcar_sysc_power_up(&pd->ch);
  219. finalize:
  220. pm_genpd_init(genpd, gov, false);
  221. }
  222. static const struct of_device_id rcar_sysc_matches[] = {
  223. #ifdef CONFIG_ARCH_R8A7779
  224. { .compatible = "renesas,r8a7779-sysc", .data = &r8a7779_sysc_info },
  225. #endif
  226. #ifdef CONFIG_ARCH_R8A7790
  227. { .compatible = "renesas,r8a7790-sysc", .data = &r8a7790_sysc_info },
  228. #endif
  229. #ifdef CONFIG_ARCH_R8A7791
  230. { .compatible = "renesas,r8a7791-sysc", .data = &r8a7791_sysc_info },
  231. #endif
  232. #ifdef CONFIG_ARCH_R8A7792
  233. { .compatible = "renesas,r8a7792-sysc", .data = &r8a7792_sysc_info },
  234. #endif
  235. #ifdef CONFIG_ARCH_R8A7793
  236. /* R-Car M2-N is identical to R-Car M2-W w.r.t. power domains. */
  237. { .compatible = "renesas,r8a7793-sysc", .data = &r8a7791_sysc_info },
  238. #endif
  239. #ifdef CONFIG_ARCH_R8A7794
  240. { .compatible = "renesas,r8a7794-sysc", .data = &r8a7794_sysc_info },
  241. #endif
  242. #ifdef CONFIG_ARCH_R8A7795
  243. { .compatible = "renesas,r8a7795-sysc", .data = &r8a7795_sysc_info },
  244. #endif
  245. #ifdef CONFIG_ARCH_R8A7796
  246. { .compatible = "renesas,r8a7796-sysc", .data = &r8a7796_sysc_info },
  247. #endif
  248. { /* sentinel */ }
  249. };
  250. struct rcar_pm_domains {
  251. struct genpd_onecell_data onecell_data;
  252. struct generic_pm_domain *domains[RCAR_PD_ALWAYS_ON + 1];
  253. };
  254. static int __init rcar_sysc_pd_init(void)
  255. {
  256. const struct rcar_sysc_info *info;
  257. const struct of_device_id *match;
  258. struct rcar_pm_domains *domains;
  259. struct device_node *np;
  260. u32 syscier, syscimr;
  261. void __iomem *base;
  262. unsigned int i;
  263. int error;
  264. if (rcar_sysc_base)
  265. return 0;
  266. np = of_find_matching_node_and_match(NULL, rcar_sysc_matches, &match);
  267. if (!np)
  268. return -ENODEV;
  269. info = match->data;
  270. has_cpg_mstp = of_find_compatible_node(NULL, NULL,
  271. "renesas,cpg-mstp-clocks");
  272. base = of_iomap(np, 0);
  273. if (!base) {
  274. pr_warn("%s: Cannot map regs\n", np->full_name);
  275. error = -ENOMEM;
  276. goto out_put;
  277. }
  278. rcar_sysc_base = base;
  279. domains = kzalloc(sizeof(*domains), GFP_KERNEL);
  280. if (!domains) {
  281. error = -ENOMEM;
  282. goto out_put;
  283. }
  284. domains->onecell_data.domains = domains->domains;
  285. domains->onecell_data.num_domains = ARRAY_SIZE(domains->domains);
  286. for (i = 0, syscier = 0; i < info->num_areas; i++)
  287. syscier |= BIT(info->areas[i].isr_bit);
  288. /*
  289. * Mask all interrupt sources to prevent the CPU from receiving them.
  290. * Make sure not to clear reserved bits that were set before.
  291. */
  292. syscimr = ioread32(base + SYSCIMR);
  293. syscimr |= syscier;
  294. pr_debug("%s: syscimr = 0x%08x\n", np->full_name, syscimr);
  295. iowrite32(syscimr, base + SYSCIMR);
  296. /*
  297. * SYSC needs all interrupt sources enabled to control power.
  298. */
  299. pr_debug("%s: syscier = 0x%08x\n", np->full_name, syscier);
  300. iowrite32(syscier, base + SYSCIER);
  301. for (i = 0; i < info->num_areas; i++) {
  302. const struct rcar_sysc_area *area = &info->areas[i];
  303. struct rcar_sysc_pd *pd;
  304. pd = kzalloc(sizeof(*pd) + strlen(area->name) + 1, GFP_KERNEL);
  305. if (!pd) {
  306. error = -ENOMEM;
  307. goto out_put;
  308. }
  309. strcpy(pd->name, area->name);
  310. pd->genpd.name = pd->name;
  311. pd->ch.chan_offs = area->chan_offs;
  312. pd->ch.chan_bit = area->chan_bit;
  313. pd->ch.isr_bit = area->isr_bit;
  314. pd->flags = area->flags;
  315. rcar_sysc_pd_setup(pd);
  316. if (area->parent >= 0)
  317. pm_genpd_add_subdomain(domains->domains[area->parent],
  318. &pd->genpd);
  319. domains->domains[area->isr_bit] = &pd->genpd;
  320. }
  321. error = of_genpd_add_provider_onecell(np, &domains->onecell_data);
  322. out_put:
  323. of_node_put(np);
  324. return error;
  325. }
  326. early_initcall(rcar_sysc_pd_init);
  327. void __init rcar_sysc_init(phys_addr_t base, u32 syscier)
  328. {
  329. u32 syscimr;
  330. if (!rcar_sysc_pd_init())
  331. return;
  332. rcar_sysc_base = ioremap_nocache(base, PAGE_SIZE);
  333. /*
  334. * Mask all interrupt sources to prevent the CPU from receiving them.
  335. * Make sure not to clear reserved bits that were set before.
  336. */
  337. syscimr = ioread32(rcar_sysc_base + SYSCIMR);
  338. syscimr |= syscier;
  339. pr_debug("%s: syscimr = 0x%08x\n", __func__, syscimr);
  340. iowrite32(syscimr, rcar_sysc_base + SYSCIMR);
  341. /*
  342. * SYSC needs all interrupt sources enabled to control power.
  343. */
  344. pr_debug("%s: syscier = 0x%08x\n", __func__, syscier);
  345. iowrite32(syscier, rcar_sysc_base + SYSCIER);
  346. }