pm-rmobile.c 8.2 KB

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