pm-rmobile.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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 bool rmobile_pd_active_wakeup(struct device *dev)
  101. {
  102. return true;
  103. }
  104. static void rmobile_init_pm_domain(struct rmobile_pm_domain *rmobile_pd)
  105. {
  106. struct generic_pm_domain *genpd = &rmobile_pd->genpd;
  107. struct dev_power_governor *gov = rmobile_pd->gov;
  108. genpd->flags = GENPD_FLAG_PM_CLK;
  109. genpd->dev_ops.active_wakeup = rmobile_pd_active_wakeup;
  110. genpd->power_off = rmobile_pd_power_down;
  111. genpd->power_on = rmobile_pd_power_up;
  112. genpd->attach_dev = cpg_mstp_attach_dev;
  113. genpd->detach_dev = cpg_mstp_detach_dev;
  114. __rmobile_pd_power_up(rmobile_pd, false);
  115. pm_genpd_init(genpd, gov ? : &simple_qos_governor, false);
  116. }
  117. static int rmobile_pd_suspend_busy(void)
  118. {
  119. /*
  120. * This domain should not be turned off.
  121. */
  122. return -EBUSY;
  123. }
  124. static int rmobile_pd_suspend_console(void)
  125. {
  126. /*
  127. * Serial consoles make use of SCIF hardware located in this domain,
  128. * hence keep the power domain on if "no_console_suspend" is set.
  129. */
  130. return console_suspend_enabled ? 0 : -EBUSY;
  131. }
  132. enum pd_types {
  133. PD_NORMAL,
  134. PD_CPU,
  135. PD_CONSOLE,
  136. PD_DEBUG,
  137. PD_MEMCTL,
  138. };
  139. #define MAX_NUM_SPECIAL_PDS 16
  140. static struct special_pd {
  141. struct device_node *pd;
  142. enum pd_types type;
  143. } special_pds[MAX_NUM_SPECIAL_PDS] __initdata;
  144. static unsigned int num_special_pds __initdata;
  145. static const struct of_device_id special_ids[] __initconst = {
  146. { .compatible = "arm,coresight-etm3x", .data = (void *)PD_DEBUG },
  147. { .compatible = "renesas,dbsc-r8a73a4", .data = (void *)PD_MEMCTL, },
  148. { .compatible = "renesas,dbsc3-r8a7740", .data = (void *)PD_MEMCTL, },
  149. { .compatible = "renesas,sbsc-sh73a0", .data = (void *)PD_MEMCTL, },
  150. { /* sentinel */ },
  151. };
  152. static void __init add_special_pd(struct device_node *np, enum pd_types type)
  153. {
  154. unsigned int i;
  155. struct device_node *pd;
  156. pd = of_parse_phandle(np, "power-domains", 0);
  157. if (!pd)
  158. return;
  159. for (i = 0; i < num_special_pds; i++)
  160. if (pd == special_pds[i].pd && type == special_pds[i].type) {
  161. of_node_put(pd);
  162. return;
  163. }
  164. if (num_special_pds == ARRAY_SIZE(special_pds)) {
  165. pr_warn("Too many special PM domains\n");
  166. of_node_put(pd);
  167. return;
  168. }
  169. pr_debug("Special PM domain %s type %d for %s\n", pd->name, type,
  170. np->full_name);
  171. special_pds[num_special_pds].pd = pd;
  172. special_pds[num_special_pds].type = type;
  173. num_special_pds++;
  174. }
  175. static void __init get_special_pds(void)
  176. {
  177. struct device_node *np;
  178. const struct of_device_id *id;
  179. /* PM domains containing CPUs */
  180. for_each_node_by_type(np, "cpu")
  181. add_special_pd(np, PD_CPU);
  182. /* PM domain containing console */
  183. if (of_stdout)
  184. add_special_pd(of_stdout, PD_CONSOLE);
  185. /* PM domains containing other special devices */
  186. for_each_matching_node_and_match(np, special_ids, &id)
  187. add_special_pd(np, (enum pd_types)id->data);
  188. }
  189. static void __init put_special_pds(void)
  190. {
  191. unsigned int i;
  192. for (i = 0; i < num_special_pds; i++)
  193. of_node_put(special_pds[i].pd);
  194. }
  195. static enum pd_types __init pd_type(const struct device_node *pd)
  196. {
  197. unsigned int i;
  198. for (i = 0; i < num_special_pds; i++)
  199. if (pd == special_pds[i].pd)
  200. return special_pds[i].type;
  201. return PD_NORMAL;
  202. }
  203. static void __init rmobile_setup_pm_domain(struct device_node *np,
  204. struct rmobile_pm_domain *pd)
  205. {
  206. const char *name = pd->genpd.name;
  207. switch (pd_type(np)) {
  208. case PD_CPU:
  209. /*
  210. * This domain contains the CPU core and therefore it should
  211. * only be turned off if the CPU is not in use.
  212. */
  213. pr_debug("PM domain %s contains CPU\n", name);
  214. pd->gov = &pm_domain_always_on_gov;
  215. pd->suspend = rmobile_pd_suspend_busy;
  216. break;
  217. case PD_CONSOLE:
  218. pr_debug("PM domain %s contains serial console\n", name);
  219. pd->gov = &pm_domain_always_on_gov;
  220. pd->suspend = rmobile_pd_suspend_console;
  221. break;
  222. case PD_DEBUG:
  223. /*
  224. * This domain contains the Coresight-ETM hardware block and
  225. * therefore it should only be turned off if the debug module
  226. * is not in use.
  227. */
  228. pr_debug("PM domain %s contains Coresight-ETM\n", name);
  229. pd->gov = &pm_domain_always_on_gov;
  230. pd->suspend = rmobile_pd_suspend_busy;
  231. break;
  232. case PD_MEMCTL:
  233. /*
  234. * This domain contains a memory-controller and therefore it
  235. * should only be turned off if memory is not in use.
  236. */
  237. pr_debug("PM domain %s contains MEMCTL\n", name);
  238. pd->gov = &pm_domain_always_on_gov;
  239. pd->suspend = rmobile_pd_suspend_busy;
  240. break;
  241. case PD_NORMAL:
  242. break;
  243. }
  244. rmobile_init_pm_domain(pd);
  245. }
  246. static int __init rmobile_add_pm_domains(void __iomem *base,
  247. struct device_node *parent,
  248. struct generic_pm_domain *genpd_parent)
  249. {
  250. struct device_node *np;
  251. for_each_child_of_node(parent, np) {
  252. struct rmobile_pm_domain *pd;
  253. u32 idx = ~0;
  254. if (of_property_read_u32(np, "reg", &idx)) {
  255. /* always-on domain */
  256. }
  257. pd = kzalloc(sizeof(*pd), GFP_KERNEL);
  258. if (!pd) {
  259. of_node_put(np);
  260. return -ENOMEM;
  261. }
  262. pd->genpd.name = np->name;
  263. pd->base = base;
  264. pd->bit_shift = idx;
  265. rmobile_setup_pm_domain(np, pd);
  266. if (genpd_parent)
  267. pm_genpd_add_subdomain(genpd_parent, &pd->genpd);
  268. of_genpd_add_provider_simple(np, &pd->genpd);
  269. rmobile_add_pm_domains(base, np, &pd->genpd);
  270. }
  271. return 0;
  272. }
  273. static int __init rmobile_init_pm_domains(void)
  274. {
  275. struct device_node *np, *pmd;
  276. bool scanned = false;
  277. void __iomem *base;
  278. int ret = 0;
  279. for_each_compatible_node(np, NULL, "renesas,sysc-rmobile") {
  280. base = of_iomap(np, 0);
  281. if (!base) {
  282. pr_warn("%s cannot map reg 0\n", np->full_name);
  283. continue;
  284. }
  285. pmd = of_get_child_by_name(np, "pm-domains");
  286. if (!pmd) {
  287. pr_warn("%s lacks pm-domains node\n", np->full_name);
  288. continue;
  289. }
  290. if (!scanned) {
  291. /* Find PM domains containing special blocks */
  292. get_special_pds();
  293. scanned = true;
  294. }
  295. ret = rmobile_add_pm_domains(base, pmd, NULL);
  296. of_node_put(pmd);
  297. if (ret) {
  298. of_node_put(np);
  299. break;
  300. }
  301. }
  302. put_special_pds();
  303. return ret;
  304. }
  305. core_initcall(rmobile_init_pm_domains);