pm.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /*
  2. * arch/arm/mach-socfpga/pm.c
  3. *
  4. * Copyright (C) 2014-2015 Altera Corporation. All rights reserved.
  5. *
  6. * with code from pm-imx6.c
  7. * Copyright 2011-2014 Freescale Semiconductor, Inc.
  8. * Copyright 2011 Linaro Ltd.
  9. *
  10. * This program is free software; you can redistribute it and/or modify it
  11. * under the terms and conditions of the GNU General Public License,
  12. * version 2, as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. #include <linux/bitops.h>
  23. #include <linux/genalloc.h>
  24. #include <linux/init.h>
  25. #include <linux/io.h>
  26. #include <linux/of_platform.h>
  27. #include <linux/suspend.h>
  28. #include <asm/suspend.h>
  29. #include <asm/fncpy.h>
  30. #include "core.h"
  31. /* Pointer to function copied to ocram */
  32. static u32 (*socfpga_sdram_self_refresh_in_ocram)(u32 sdr_base);
  33. static int socfpga_setup_ocram_self_refresh(void)
  34. {
  35. struct platform_device *pdev;
  36. phys_addr_t ocram_pbase;
  37. struct device_node *np;
  38. struct gen_pool *ocram_pool;
  39. unsigned long ocram_base;
  40. void __iomem *suspend_ocram_base;
  41. int ret = 0;
  42. np = of_find_compatible_node(NULL, NULL, "mmio-sram");
  43. if (!np) {
  44. pr_err("%s: Unable to find mmio-sram in dtb\n", __func__);
  45. return -ENODEV;
  46. }
  47. pdev = of_find_device_by_node(np);
  48. if (!pdev) {
  49. pr_warn("%s: failed to find ocram device!\n", __func__);
  50. ret = -ENODEV;
  51. goto put_node;
  52. }
  53. ocram_pool = gen_pool_get(&pdev->dev, NULL);
  54. if (!ocram_pool) {
  55. pr_warn("%s: ocram pool unavailable!\n", __func__);
  56. ret = -ENODEV;
  57. goto put_node;
  58. }
  59. ocram_base = gen_pool_alloc(ocram_pool, socfpga_sdram_self_refresh_sz);
  60. if (!ocram_base) {
  61. pr_warn("%s: unable to alloc ocram!\n", __func__);
  62. ret = -ENOMEM;
  63. goto put_node;
  64. }
  65. ocram_pbase = gen_pool_virt_to_phys(ocram_pool, ocram_base);
  66. suspend_ocram_base = __arm_ioremap_exec(ocram_pbase,
  67. socfpga_sdram_self_refresh_sz,
  68. false);
  69. if (!suspend_ocram_base) {
  70. pr_warn("%s: __arm_ioremap_exec failed!\n", __func__);
  71. ret = -ENOMEM;
  72. goto put_node;
  73. }
  74. /* Copy the code that puts DDR in self refresh to ocram */
  75. socfpga_sdram_self_refresh_in_ocram =
  76. (void *)fncpy(suspend_ocram_base,
  77. &socfpga_sdram_self_refresh,
  78. socfpga_sdram_self_refresh_sz);
  79. WARN(!socfpga_sdram_self_refresh_in_ocram,
  80. "could not copy function to ocram");
  81. if (!socfpga_sdram_self_refresh_in_ocram)
  82. ret = -EFAULT;
  83. put_node:
  84. of_node_put(np);
  85. return ret;
  86. }
  87. static int socfpga_pm_suspend(unsigned long arg)
  88. {
  89. u32 ret;
  90. if (!sdr_ctl_base_addr)
  91. return -EFAULT;
  92. ret = socfpga_sdram_self_refresh_in_ocram((u32)sdr_ctl_base_addr);
  93. pr_debug("%s self-refresh loops request=%d exit=%d\n", __func__,
  94. ret & 0xffff, (ret >> 16) & 0xffff);
  95. return 0;
  96. }
  97. static int socfpga_pm_enter(suspend_state_t state)
  98. {
  99. switch (state) {
  100. case PM_SUSPEND_STANDBY:
  101. case PM_SUSPEND_MEM:
  102. outer_disable();
  103. cpu_suspend(0, socfpga_pm_suspend);
  104. outer_resume();
  105. break;
  106. default:
  107. return -EINVAL;
  108. }
  109. return 0;
  110. }
  111. static const struct platform_suspend_ops socfpga_pm_ops = {
  112. .valid = suspend_valid_only_mem,
  113. .enter = socfpga_pm_enter,
  114. };
  115. static int __init socfpga_pm_init(void)
  116. {
  117. int ret;
  118. ret = socfpga_setup_ocram_self_refresh();
  119. if (ret)
  120. return ret;
  121. suspend_set_ops(&socfpga_pm_ops);
  122. pr_info("SoCFPGA initialized for DDR self-refresh during suspend.\n");
  123. return 0;
  124. }
  125. arch_initcall(socfpga_pm_init);