1 // SPDX-License-Identifier: GPL-2.0
3 * AM33XX Power Management Routines
5 * Copyright (C) 2012-2018 Texas Instruments Incorporated - http://www.ti.com/
6 * Vaibhav Bedia, Dave Gerlach
10 #include <linux/err.h>
11 #include <linux/genalloc.h>
12 #include <linux/kernel.h>
13 #include <linux/init.h>
15 #include <linux/module.h>
17 #include <linux/platform_data/pm33xx.h>
18 #include <linux/platform_device.h>
19 #include <linux/sizes.h>
20 #include <linux/sram.h>
21 #include <linux/suspend.h>
22 #include <linux/ti-emif-sram.h>
23 #include <linux/wkup_m3_ipc.h>
25 #include <asm/proc-fns.h>
26 #include <asm/suspend.h>
27 #include <asm/system_misc.h>
29 #define AMX3_PM_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
30 (unsigned long)pm_sram->do_wfi)
32 static int (*am33xx_do_wfi_sram
)(unsigned long unused
);
33 static phys_addr_t am33xx_do_wfi_sram_phys
;
35 static struct gen_pool
*sram_pool
, *sram_pool_data
;
36 static unsigned long ocmcram_location
, ocmcram_location_data
;
38 static struct am33xx_pm_platform_data
*pm_ops
;
39 static struct am33xx_pm_sram_addr
*pm_sram
;
41 static struct device
*pm33xx_dev
;
42 static struct wkup_m3_ipc
*m3_ipc
;
44 static unsigned long suspend_wfi_flags
;
46 static u32
sram_suspend_address(unsigned long addr
)
48 return ((unsigned long)am33xx_do_wfi_sram
+
49 AMX3_PM_SRAM_SYMBOL_OFFSET(addr
));
53 static int am33xx_pm_suspend(suspend_state_t suspend_state
)
57 ret
= pm_ops
->soc_suspend((unsigned long)suspend_state
,
58 am33xx_do_wfi_sram
, suspend_wfi_flags
);
61 dev_err(pm33xx_dev
, "PM: Kernel suspend failure\n");
63 i
= m3_ipc
->ops
->request_pm_status(m3_ipc
);
68 "PM: Successfully put all powerdomains to target state\n");
72 "PM: Could not transition all powerdomains to target state\n");
77 "PM: CM3 returned unknown result = %d\n", i
);
85 static int am33xx_pm_enter(suspend_state_t suspend_state
)
89 switch (suspend_state
) {
91 case PM_SUSPEND_STANDBY
:
92 ret
= am33xx_pm_suspend(suspend_state
);
101 static int am33xx_pm_begin(suspend_state_t state
)
107 ret
= m3_ipc
->ops
->prepare_low_power(m3_ipc
, WKUP_M3_DEEPSLEEP
);
109 case PM_SUSPEND_STANDBY
:
110 ret
= m3_ipc
->ops
->prepare_low_power(m3_ipc
, WKUP_M3_STANDBY
);
117 static void am33xx_pm_end(void)
119 m3_ipc
->ops
->finish_low_power(m3_ipc
);
122 static int am33xx_pm_valid(suspend_state_t state
)
125 case PM_SUSPEND_STANDBY
:
133 static const struct platform_suspend_ops am33xx_pm_ops
= {
134 .begin
= am33xx_pm_begin
,
135 .end
= am33xx_pm_end
,
136 .enter
= am33xx_pm_enter
,
137 .valid
= am33xx_pm_valid
,
139 #endif /* CONFIG_SUSPEND */
141 static void am33xx_pm_set_ipc_ops(void)
146 temp
= ti_emif_get_mem_type();
148 dev_err(pm33xx_dev
, "PM: Cannot determine memory type, no PM available\n");
151 m3_ipc
->ops
->set_mem_type(m3_ipc
, temp
);
153 /* Physical resume address to be used by ROM code */
154 resume_address
= am33xx_do_wfi_sram_phys
+
155 *pm_sram
->resume_offset
+ 0x4;
157 m3_ipc
->ops
->set_resume_address(m3_ipc
, (void *)resume_address
);
160 static void am33xx_pm_free_sram(void)
162 gen_pool_free(sram_pool
, ocmcram_location
, *pm_sram
->do_wfi_sz
);
163 gen_pool_free(sram_pool_data
, ocmcram_location_data
,
164 sizeof(struct am33xx_pm_ro_sram_data
));
168 * Push the minimal suspend-resume code to SRAM
170 static int am33xx_pm_alloc_sram(void)
172 struct device_node
*np
;
175 np
= of_find_compatible_node(NULL
, NULL
, "ti,omap3-mpu");
177 np
= of_find_compatible_node(NULL
, NULL
, "ti,omap4-mpu");
179 dev_err(pm33xx_dev
, "PM: %s: Unable to find device node for mpu\n",
185 sram_pool
= of_gen_pool_get(np
, "pm-sram", 0);
187 dev_err(pm33xx_dev
, "PM: %s: Unable to get sram pool for ocmcram\n",
193 sram_pool_data
= of_gen_pool_get(np
, "pm-sram", 1);
194 if (!sram_pool_data
) {
195 dev_err(pm33xx_dev
, "PM: %s: Unable to get sram data pool for ocmcram\n",
201 ocmcram_location
= gen_pool_alloc(sram_pool
, *pm_sram
->do_wfi_sz
);
202 if (!ocmcram_location
) {
203 dev_err(pm33xx_dev
, "PM: %s: Unable to allocate memory from ocmcram\n",
209 ocmcram_location_data
= gen_pool_alloc(sram_pool_data
,
210 sizeof(struct emif_regs_amx3
));
211 if (!ocmcram_location_data
) {
212 dev_err(pm33xx_dev
, "PM: Unable to allocate memory from ocmcram\n");
213 gen_pool_free(sram_pool
, ocmcram_location
, *pm_sram
->do_wfi_sz
);
222 static int am33xx_push_sram_idle(void)
224 struct am33xx_pm_ro_sram_data ro_sram_data
;
226 u32 table_addr
, ro_data_addr
;
229 ro_sram_data
.amx3_pm_sram_data_virt
= ocmcram_location_data
;
230 ro_sram_data
.amx3_pm_sram_data_phys
=
231 gen_pool_virt_to_phys(sram_pool_data
, ocmcram_location_data
);
232 ro_sram_data
.rtc_base_virt
= pm_ops
->get_rtc_base_addr();
234 /* Save physical address to calculate resume offset during pm init */
235 am33xx_do_wfi_sram_phys
= gen_pool_virt_to_phys(sram_pool
,
238 am33xx_do_wfi_sram
= sram_exec_copy(sram_pool
, (void *)ocmcram_location
,
240 *pm_sram
->do_wfi_sz
);
241 if (!am33xx_do_wfi_sram
) {
243 "PM: %s: am33xx_do_wfi copy to sram failed\n",
249 sram_suspend_address((unsigned long)pm_sram
->emif_sram_table
);
250 ret
= ti_emif_copy_pm_function_table(sram_pool
, (void *)table_addr
);
253 "PM: %s: EMIF function copy failed\n", __func__
);
254 return -EPROBE_DEFER
;
258 sram_suspend_address((unsigned long)pm_sram
->ro_sram_data
);
259 copy_addr
= sram_exec_copy(sram_pool
, (void *)ro_data_addr
,
261 sizeof(ro_sram_data
));
264 "PM: %s: ro_sram_data copy to sram failed\n",
272 static int am33xx_pm_probe(struct platform_device
*pdev
)
274 struct device
*dev
= &pdev
->dev
;
277 if (!of_machine_is_compatible("ti,am33xx") &&
278 !of_machine_is_compatible("ti,am43"))
281 pm_ops
= dev
->platform_data
;
283 dev_err(dev
, "PM: Cannot get core PM ops!\n");
287 pm_sram
= pm_ops
->get_sram_addrs();
289 dev_err(dev
, "PM: Cannot get PM asm function addresses!!\n");
295 ret
= am33xx_pm_alloc_sram();
299 ret
= am33xx_push_sram_idle();
303 m3_ipc
= wkup_m3_ipc_get();
305 dev_dbg(dev
, "PM: Cannot get wkup_m3_ipc handle\n");
310 am33xx_pm_set_ipc_ops();
312 #ifdef CONFIG_SUSPEND
313 suspend_set_ops(&am33xx_pm_ops
);
314 #endif /* CONFIG_SUSPEND */
317 * For a system suspend we must flush the caches, we want
318 * the DDR in self-refresh, we want to save the context
319 * of the EMIF, and we want the wkup_m3 to handle low-power
322 suspend_wfi_flags
|= WFI_FLAG_FLUSH_CACHE
;
323 suspend_wfi_flags
|= WFI_FLAG_SELF_REFRESH
;
324 suspend_wfi_flags
|= WFI_FLAG_SAVE_EMIF
;
325 suspend_wfi_flags
|= WFI_FLAG_WAKE_M3
;
327 ret
= pm_ops
->init();
329 dev_err(dev
, "Unable to call core pm init!\n");
331 goto err_put_wkup_m3_ipc
;
337 wkup_m3_ipc_put(m3_ipc
);
339 am33xx_pm_free_sram();
344 static int am33xx_pm_remove(struct platform_device
*pdev
)
346 suspend_set_ops(NULL
);
347 wkup_m3_ipc_put(m3_ipc
);
348 am33xx_pm_free_sram();
352 static struct platform_driver am33xx_pm_driver
= {
356 .probe
= am33xx_pm_probe
,
357 .remove
= am33xx_pm_remove
,
359 module_platform_driver(am33xx_pm_driver
);
361 MODULE_ALIAS("platform:pm33xx");
362 MODULE_LICENSE("GPL v2");
363 MODULE_DESCRIPTION("am33xx power management driver");