rmobile-sysc.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * rmobile power management support
  4. *
  5. * Copyright (C) 2012 Renesas Solutions Corp.
  6. * Copyright (C) 2012 Kuninori Morimoto <kuninori.morimoto.gx@renesas.com>
  7. * Copyright (C) 2014 Glider bvba
  8. *
  9. * based on pm-sh7372.c
  10. * Copyright (C) 2011 Magnus Damm
  11. */
  12. #include <linux/clk/renesas.h>
  13. #include <linux/console.h>
  14. #include <linux/delay.h>
  15. #include <linux/io.h>
  16. #include <linux/iopoll.h>
  17. #include <linux/of.h>
  18. #include <linux/of_address.h>
  19. #include <linux/pm.h>
  20. #include <linux/pm_clock.h>
  21. #include <linux/pm_domain.h>
  22. #include <linux/slab.h>
  23. /* SYSC */
  24. #define SPDCR 0x08 /* SYS Power Down Control Register */
  25. #define SWUCR 0x14 /* SYS Wakeup Control Register */
  26. #define PSTR 0x80 /* Power Status Register */
  27. #define PSTR_RETRIES 100
  28. #define PSTR_DELAY_US 10
  29. struct rmobile_pm_domain {
  30. struct generic_pm_domain genpd;
  31. struct dev_power_governor *gov;
  32. int (*suspend)(void);
  33. void __iomem *base;
  34. unsigned int bit_shift;
  35. };
  36. static inline
  37. struct rmobile_pm_domain *to_rmobile_pd(struct generic_pm_domain *d)
  38. {
  39. return container_of(d, struct rmobile_pm_domain, genpd);
  40. }
  41. static int rmobile_pd_power_down(struct generic_pm_domain *genpd)
  42. {
  43. struct rmobile_pm_domain *rmobile_pd = to_rmobile_pd(genpd);
  44. unsigned int mask = BIT(rmobile_pd->bit_shift);
  45. u32 val;
  46. if (rmobile_pd->suspend) {
  47. int ret = rmobile_pd->suspend();
  48. if (ret)
  49. return ret;
  50. }
  51. if (readl(rmobile_pd->base + PSTR) & mask) {
  52. writel(mask, rmobile_pd->base + SPDCR);
  53. readl_poll_timeout_atomic(rmobile_pd->base + SPDCR, val,
  54. !(val & mask), 0, PSTR_RETRIES);
  55. }
  56. pr_debug("%s: Power off, 0x%08x -> PSTR = 0x%08x\n", genpd->name, mask,
  57. readl(rmobile_pd->base + PSTR));
  58. return 0;
  59. }
  60. static int __rmobile_pd_power_up(struct rmobile_pm_domain *rmobile_pd)
  61. {
  62. unsigned int val, mask = BIT(rmobile_pd->bit_shift);
  63. int ret = 0;
  64. if (readl(rmobile_pd->base + PSTR) & mask)
  65. return ret;
  66. writel(mask, rmobile_pd->base + SWUCR);
  67. ret = readl_poll_timeout_atomic(rmobile_pd->base + SWUCR, val,
  68. (val & mask), PSTR_DELAY_US,
  69. PSTR_RETRIES * PSTR_DELAY_US);
  70. pr_debug("%s: Power on, 0x%08x -> PSTR = 0x%08x\n",
  71. rmobile_pd->genpd.name, mask,
  72. readl(rmobile_pd->base + PSTR));
  73. return ret;
  74. }
  75. static int rmobile_pd_power_up(struct generic_pm_domain *genpd)
  76. {
  77. return __rmobile_pd_power_up(to_rmobile_pd(genpd));
  78. }
  79. static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
  80. {
  81. struct generic_pm_domain *genpd = &rmobile_pd->genpd;
  82. struct dev_power_governor *gov = rmobile_pd->gov;
  83. genpd->flags |= GENPD_FLAG_PM_CLK | GENPD_FLAG_ACTIVE_WAKEUP;
  84. genpd->attach_dev = cpg_mstp_attach_dev;
  85. genpd->detach_dev = cpg_mstp_detach_dev;
  86. if (!(genpd->flags & GENPD_FLAG_ALWAYS_ON)) {
  87. genpd->power_off = rmobile_pd_power_down;
  88. genpd->power_on = rmobile_pd_power_up;
  89. __rmobile_pd_power_up(rmobile_pd);
  90. }
  91. pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
  92. }
  93. static int rmobile_pd_suspend_console(void)
  94. {
  95. /*
  96. * Serial consoles make use of SCIF hardware located in this domain,
  97. * hence keep the power domain on if "no_console_suspend" is set.
  98. */
  99. return console_suspend_enabled ? 0 : -EBUSY;
  100. }
  101. enum pd_types {
  102. PD_NORMAL,
  103. PD_CPU,
  104. PD_CONSOLE,
  105. PD_DEBUG,
  106. PD_MEMCTL,
  107. };
  108. #define MAX_NUM_SPECIAL_PDS 16
  109. static struct special_pd {
  110. struct device_node *pd;
  111. enum pd_types type;
  112. } special_pds[MAX_NUM_SPECIAL_PDS] __initdata;
  113. static unsigned int num_special_pds __initdata;
  114. static const struct of_device_id special_ids[] __initconst = {
  115. { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG },
  116. { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, },
  117. { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, },
  118. { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, },
  119. { /* sentinel */ },
  120. };
  121. static void __init add_special_pd(struct device_node *np, enum pd_types type)
  122. {
  123. unsigned int i;
  124. struct device_node *pd;
  125. pd = of_parse_phandle(np, "power-domains", 0);
  126. if (!pd)
  127. return;
  128. for (i = 0; i < num_special_pds; i++)
  129. if (pd == special_pds[i].pd && type == special_pds[i].type) {
  130. of_node_put(pd);
  131. return;
  132. }
  133. if (num_special_pds == ARRAY_SIZE(special_pds)) {
  134. pr_warn("Too many special PM domains\n");
  135. of_node_put(pd);
  136. return;
  137. }
  138. pr_debug("Special PM domain %pOFn type %d for %pOF\n", pd, type, np);
  139. special_pds[num_special_pds].pd = pd;
  140. special_pds[num_special_pds].type = type;
  141. num_special_pds++;
  142. }
  143. static void __init get_special_pds(void)
  144. {
  145. struct device_node *np;
  146. const struct of_device_id *id;
  147. /* PM domains containing CPUs */
  148. for_each_of_cpu_node(np)
  149. add_special_pd(np, PD_CPU);
  150. /* PM domain containing console */
  151. if (of_stdout)
  152. add_special_pd(of_stdout, PD_CONSOLE);
  153. /* PM domains containing other special devices */
  154. for_each_matching_node_and_match(np, special_ids, &id)
  155. add_special_pd(np, (uintptr_t)id->data);
  156. }
  157. static void __init put_special_pds(void)
  158. {
  159. unsigned int i;
  160. for (i = 0; i < num_special_pds; i++)
  161. of_node_put(special_pds[i].pd);
  162. }
  163. static enum pd_types __init pd_type(const struct device_node *pd)
  164. {
  165. unsigned int i;
  166. for (i = 0; i < num_special_pds; i++)
  167. if (pd == special_pds[i].pd)
  168. return special_pds[i].type;
  169. return PD_NORMAL;
  170. }
  171. static void __init rmobile_setup_pm_domain(struct device_node *np,
  172. struct rmobile_pm_domain *pd)
  173. {
  174. const char *name = pd->genpd.name;
  175. switch (pd_type(np)) {
  176. case PD_CPU:
  177. /*
  178. * This domain contains the CPU core and therefore it should
  179. * only be turned off if the CPU is not in use.
  180. */
  181. pr_debug("PM domain %s contains CPU\n", name);
  182. pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
  183. break;
  184. case PD_CONSOLE:
  185. pr_debug("PM domain %s contains serial console\n", name);
  186. pd->gov = &pm_domain_always_on_gov;
  187. pd->suspend = rmobile_pd_suspend_console;
  188. break;
  189. case PD_DEBUG:
  190. /*
  191. * This domain contains the Coresight-ETM hardware block and
  192. * therefore it should only be turned off if the debug module
  193. * is not in use.
  194. */
  195. pr_debug("PM domain %s contains Coresight-ETM\n", name);
  196. pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
  197. break;
  198. case PD_MEMCTL:
  199. /*
  200. * This domain contains a memory-controller and therefore it
  201. * should only be turned off if memory is not in use.
  202. */
  203. pr_debug("PM domain %s contains MEMCTL\n", name);
  204. pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
  205. break;
  206. case PD_NORMAL:
  207. if (pd->bit_shift == ~0) {
  208. /* Top-level always-on domain */
  209. pr_debug("PM domain %s is always-on domain\n", name);
  210. pd->genpd.flags |= GENPD_FLAG_ALWAYS_ON;
  211. }
  212. break;
  213. }
  214. rmobile_init_pm_domain(pd);
  215. }
  216. static int __init rmobile_add_pm_domains(void __iomem *base,
  217. struct device_node *parent,
  218. struct generic_pm_domain *genpd_parent)
  219. {
  220. for_each_child_of_node_scoped(parent, np) {
  221. struct rmobile_pm_domain *pd;
  222. u32 idx = ~0;
  223. if (of_property_read_u32(np, "reg", &idx)) {
  224. /* always-on domain */
  225. }
  226. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  227. if (!pd)
  228. return -ENOMEM;
  229. pd->genpd.name = np->name;
  230. pd->base = base;
  231. pd->bit_shift = idx;
  232. rmobile_setup_pm_domain(np, pd);
  233. if (genpd_parent)
  234. pm_genpd_add_subdomain(genpd_parent, &pd->genpd);
  235. of_genpd_add_provider_simple(np, &pd->genpd);
  236. rmobile_add_pm_domains(base, np, &pd->genpd);
  237. }
  238. return 0;
  239. }
  240. static int __init rmobile_init_pm_domains(void)
  241. {
  242. struct device_node *np, *pmd;
  243. bool scanned = false;
  244. void __iomem *base;
  245. int ret = 0;
  246. for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") {
  247. base = of_iomap(np, 0);
  248. if (!base) {
  249. pr_warn("%pOF cannot map reg 0\n", np);
  250. continue;
  251. }
  252. pmd = of_get_child_by_name(np, "pm-domains");
  253. if (!pmd) {
  254. iounmap(base);
  255. pr_warn("%pOF lacks pm-domains node\n", np);
  256. continue;
  257. }
  258. if (!scanned) {
  259. /* Find PM domains containing special blocks */
  260. get_special_pds();
  261. scanned = true;
  262. }
  263. ret = rmobile_add_pm_domains(base, pmd, NULL);
  264. of_node_put(pmd);
  265. if (ret) {
  266. of_node_put(np);
  267. break;
  268. }
  269. fwnode_dev_initialized(of_fwnode_handle(np), true);
  270. }
  271. put_special_pds();
  272. return ret;
  273. }
  274. core_initcall(rmobile_init_pm_domains);