pm-rmobile.c 9.8 KB

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