rmobile-sysc.c 8.1 KB

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