pm-rmobile.c 8.1 KB

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