Loading...
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
23#include <linux/bitops.h>
24#include <linux/genalloc.h>
25#include <linux/init.h>
26#include <linux/io.h>
27#include <linux/of_platform.h>
28#include <linux/suspend.h>
29#include <asm/suspend.h>
30#include <asm/fncpy.h>
31#include "core.h"
32
33/* Pointer to function copied to ocram */
34static u32 (*socfpga_sdram_self_refresh_in_ocram)(u32 sdr_base);
35
36static int socfpga_setup_ocram_self_refresh(void)
37{
38 struct platform_device *pdev;
39 phys_addr_t ocram_pbase;
40 struct device_node *np;
41 struct gen_pool *ocram_pool;
42 unsigned long ocram_base;
43 void __iomem *suspend_ocram_base;
44 int ret = 0;
45
46 np = of_find_compatible_node(NULL, NULL, "mmio-sram");
47 if (!np) {
48 pr_err("%s: Unable to find mmio-sram in dtb\n", __func__);
49 return -ENODEV;
50 }
51
52 pdev = of_find_device_by_node(np);
53 if (!pdev) {
54 pr_warn("%s: failed to find ocram device!\n", __func__);
55 ret = -ENODEV;
56 goto put_node;
57 }
58
59 ocram_pool = gen_pool_get(&pdev->dev, NULL);
60 if (!ocram_pool) {
61 pr_warn("%s: ocram pool unavailable!\n", __func__);
62 ret = -ENODEV;
63 goto put_node;
64 }
65
66 ocram_base = gen_pool_alloc(ocram_pool, socfpga_sdram_self_refresh_sz);
67 if (!ocram_base) {
68 pr_warn("%s: unable to alloc ocram!\n", __func__);
69 ret = -ENOMEM;
70 goto put_node;
71 }
72
73 ocram_pbase = gen_pool_virt_to_phys(ocram_pool, ocram_base);
74
75 suspend_ocram_base = __arm_ioremap_exec(ocram_pbase,
76 socfpga_sdram_self_refresh_sz,
77 false);
78 if (!suspend_ocram_base) {
79 pr_warn("%s: __arm_ioremap_exec failed!\n", __func__);
80 ret = -ENOMEM;
81 goto put_node;
82 }
83
84 /* Copy the code that puts DDR in self refresh to ocram */
85 socfpga_sdram_self_refresh_in_ocram =
86 (void *)fncpy(suspend_ocram_base,
87 &socfpga_sdram_self_refresh,
88 socfpga_sdram_self_refresh_sz);
89
90 WARN(!socfpga_sdram_self_refresh_in_ocram,
91 "could not copy function to ocram");
92 if (!socfpga_sdram_self_refresh_in_ocram)
93 ret = -EFAULT;
94
95put_node:
96 of_node_put(np);
97
98 return ret;
99}
100
101static int socfpga_pm_suspend(unsigned long arg)
102{
103 u32 ret;
104
105 if (!sdr_ctl_base_addr)
106 return -EFAULT;
107
108 ret = socfpga_sdram_self_refresh_in_ocram((u32)sdr_ctl_base_addr);
109
110 pr_debug("%s self-refresh loops request=%d exit=%d\n", __func__,
111 ret & 0xffff, (ret >> 16) & 0xffff);
112
113 return 0;
114}
115
116static int socfpga_pm_enter(suspend_state_t state)
117{
118 switch (state) {
119 case PM_SUSPEND_MEM:
120 outer_disable();
121 cpu_suspend(0, socfpga_pm_suspend);
122 outer_resume();
123 break;
124 default:
125 return -EINVAL;
126 }
127 return 0;
128}
129
130static const struct platform_suspend_ops socfpga_pm_ops = {
131 .valid = suspend_valid_only_mem,
132 .enter = socfpga_pm_enter,
133};
134
135static int __init socfpga_pm_init(void)
136{
137 int ret;
138
139 ret = socfpga_setup_ocram_self_refresh();
140 if (ret)
141 return ret;
142
143 suspend_set_ops(&socfpga_pm_ops);
144 pr_info("SoCFPGA initialized for DDR self-refresh during suspend.\n");
145
146 return 0;
147}
148arch_initcall(socfpga_pm_init);
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
12#include <linux/bitops.h>
13#include <linux/genalloc.h>
14#include <linux/init.h>
15#include <linux/io.h>
16#include <linux/of_platform.h>
17#include <linux/suspend.h>
18#include <asm/suspend.h>
19#include <asm/fncpy.h>
20#include "core.h"
21
22/* Pointer to function copied to ocram */
23static u32 (*socfpga_sdram_self_refresh_in_ocram)(u32 sdr_base);
24
25static int socfpga_setup_ocram_self_refresh(void)
26{
27 struct platform_device *pdev;
28 phys_addr_t ocram_pbase;
29 struct device_node *np;
30 struct gen_pool *ocram_pool;
31 unsigned long ocram_base;
32 void __iomem *suspend_ocram_base;
33 int ret = 0;
34
35 np = of_find_compatible_node(NULL, NULL, "mmio-sram");
36 if (!np) {
37 pr_err("%s: Unable to find mmio-sram in dtb\n", __func__);
38 return -ENODEV;
39 }
40
41 pdev = of_find_device_by_node(np);
42 if (!pdev) {
43 pr_warn("%s: failed to find ocram device!\n", __func__);
44 ret = -ENODEV;
45 goto put_node;
46 }
47
48 ocram_pool = gen_pool_get(&pdev->dev, NULL);
49 if (!ocram_pool) {
50 pr_warn("%s: ocram pool unavailable!\n", __func__);
51 ret = -ENODEV;
52 goto put_device;
53 }
54
55 ocram_base = gen_pool_alloc(ocram_pool, socfpga_sdram_self_refresh_sz);
56 if (!ocram_base) {
57 pr_warn("%s: unable to alloc ocram!\n", __func__);
58 ret = -ENOMEM;
59 goto put_device;
60 }
61
62 ocram_pbase = gen_pool_virt_to_phys(ocram_pool, ocram_base);
63
64 suspend_ocram_base = __arm_ioremap_exec(ocram_pbase,
65 socfpga_sdram_self_refresh_sz,
66 false);
67 if (!suspend_ocram_base) {
68 pr_warn("%s: __arm_ioremap_exec failed!\n", __func__);
69 ret = -ENOMEM;
70 goto put_device;
71 }
72
73 /* Copy the code that puts DDR in self refresh to ocram */
74 socfpga_sdram_self_refresh_in_ocram =
75 (void *)fncpy(suspend_ocram_base,
76 &socfpga_sdram_self_refresh,
77 socfpga_sdram_self_refresh_sz);
78
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
84put_device:
85 put_device(&pdev->dev);
86put_node:
87 of_node_put(np);
88
89 return ret;
90}
91
92static int socfpga_pm_suspend(unsigned long arg)
93{
94 u32 ret;
95
96 if (!sdr_ctl_base_addr)
97 return -EFAULT;
98
99 ret = socfpga_sdram_self_refresh_in_ocram((u32)sdr_ctl_base_addr);
100
101 pr_debug("%s self-refresh loops request=%d exit=%d\n", __func__,
102 ret & 0xffff, (ret >> 16) & 0xffff);
103
104 return 0;
105}
106
107static int socfpga_pm_enter(suspend_state_t state)
108{
109 switch (state) {
110 case PM_SUSPEND_MEM:
111 outer_disable();
112 cpu_suspend(0, socfpga_pm_suspend);
113 outer_resume();
114 break;
115 default:
116 return -EINVAL;
117 }
118 return 0;
119}
120
121static const struct platform_suspend_ops socfpga_pm_ops = {
122 .valid = suspend_valid_only_mem,
123 .enter = socfpga_pm_enter,
124};
125
126static int __init socfpga_pm_init(void)
127{
128 int ret;
129
130 ret = socfpga_setup_ocram_self_refresh();
131 if (ret)
132 return ret;
133
134 suspend_set_ops(&socfpga_pm_ops);
135 pr_info("SoCFPGA initialized for DDR self-refresh during suspend.\n");
136
137 return 0;
138}
139arch_initcall(socfpga_pm_init);