rcar-sysc.c 12 KB

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