pm.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * arch/arm/mach-socfpga/pm.c
  4. *
  5. * Copyright (C) 2014-2015 Altera Corporation. All rights reserved.
  6. *
  7. * with code from pm-imx6.c
  8. * Copyright 2011-2014 Freescale Semiconductor, Inc.
  9. * Copyright 2011 Linaro Ltd.
  10. */
  11. #include <linux/bitops.h>
  12. #include <linux/genalloc.h>
  13. #include <linux/init.h>
  14. #include <linux/io.h>
  15. #include <linux/of.h>
  16. #include <linux/of_platform.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/suspend.h>
  19. #include <asm/suspend.h>
  20. #include <asm/fncpy.h>
  21. #include "core.h"
  22. /* Pointer to function copied to ocram */
  23. static u32 (*socfpga_sdram_self_refresh_in_ocram)(u32 sdr_base);
  24. static int socfpga_setup_ocram_self_refresh(void)
  25. {
  26. struct platform_device *pdev;
  27. phys_addr_t ocram_pbase;
  28. struct device_node *np;
  29. struct gen_pool *ocram_pool;
  30. unsigned long ocram_base;
  31. void __iomem *suspend_ocram_base;
  32. int ret = 0;
  33. np = of_find_compatible_node(NULL, NULL, "mmio-sram");
  34. if (!np) {
  35. pr_err("%s: Unable to find mmio-sram in dtb\n", __func__);
  36. return -ENODEV;
  37. }
  38. pdev = of_find_device_by_node(np);
  39. if (!pdev) {
  40. pr_warn("%s: failed to find ocram device!\n", __func__);
  41. ret = -ENODEV;
  42. goto put_node;
  43. }
  44. ocram_pool = gen_pool_get(&pdev->dev, NULL);
  45. if (!ocram_pool) {
  46. pr_warn("%s: ocram pool unavailable!\n", __func__);
  47. ret = -ENODEV;
  48. goto put_device;
  49. }
  50. ocram_base = gen_pool_alloc(ocram_pool, socfpga_sdram_self_refresh_sz);
  51. if (!ocram_base) {
  52. pr_warn("%s: unable to alloc ocram!\n", __func__);
  53. ret = -ENOMEM;
  54. goto put_device;
  55. }
  56. ocram_pbase = gen_pool_virt_to_phys(ocram_pool, ocram_base);
  57. suspend_ocram_base = __arm_ioremap_exec(ocram_pbase,
  58. socfpga_sdram_self_refresh_sz,
  59. false);
  60. if (!suspend_ocram_base) {
  61. pr_warn("%s: __arm_ioremap_exec failed!\n", __func__);
  62. ret = -ENOMEM;
  63. goto put_device;
  64. }
  65. /* Copy the code that puts DDR in self refresh to ocram */
  66. socfpga_sdram_self_refresh_in_ocram =
  67. (void *)fncpy(suspend_ocram_base,
  68. &socfpga_sdram_self_refresh,
  69. socfpga_sdram_self_refresh_sz);
  70. WARN(!socfpga_sdram_self_refresh_in_ocram,
  71. "could not copy function to ocram");
  72. if (!socfpga_sdram_self_refresh_in_ocram)
  73. ret = -EFAULT;
  74. put_device:
  75. put_device(&pdev->dev);
  76. put_node:
  77. of_node_put(np);
  78. return ret;
  79. }
  80. static int socfpga_pm_suspend(unsigned long arg)
  81. {
  82. u32 ret;
  83. if (!sdr_ctl_base_addr)
  84. return -EFAULT;
  85. ret = socfpga_sdram_self_refresh_in_ocram((u32)sdr_ctl_base_addr);
  86. pr_debug("%s self-refresh loops request=%d exit=%d\n", __func__,
  87. ret & 0xffff, (ret >> 16) & 0xffff);
  88. return 0;
  89. }
  90. static int socfpga_pm_enter(suspend_state_t state)
  91. {
  92. switch (state) {
  93. case PM_SUSPEND_MEM:
  94. outer_disable();
  95. cpu_suspend(0, socfpga_pm_suspend);
  96. outer_resume();
  97. break;
  98. default:
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. static const struct platform_suspend_ops socfpga_pm_ops = {
  104. .valid = suspend_valid_only_mem,
  105. .enter = socfpga_pm_enter,
  106. };
  107. static int __init socfpga_pm_init(void)
  108. {
  109. int ret;
  110. ret = socfpga_setup_ocram_self_refresh();
  111. if (ret)
  112. return ret;
  113. suspend_set_ops(&socfpga_pm_ops);
  114. pr_info("SoCFPGA initialized for DDR self-refresh during suspend.\n");
  115. return 0;
  116. }
  117. arch_initcall(socfpga_pm_init);