pm-rmobile.c 9.0 KB

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