rmobile-sysc.c 8.2 KB

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