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/cpu.h>
11 #include <linux/err.h>
12 #include <linux/genalloc.h>
13 #include <linux/kernel.h>
14 #include <linux/init.h>
16 #include <linux/module.h>
17 #include <linux/nvmem-consumer.h>
19 #include <linux/platform_data/pm33xx.h>
20 #include <linux/platform_device.h>
21 #include <linux/rtc.h>
22 #include <linux/rtc/rtc-omap.h>
23 #include <linux/sizes.h>
24 #include <linux/sram.h>
25 #include <linux/suspend.h>
26 #include <linux/ti-emif-sram.h>
27 #include <linux/wkup_m3_ipc.h>
29 #include <asm/proc-fns.h>
30 #include <asm/suspend.h>
31 #include <asm/system_misc.h>
33 #define AMX3_PM_SRAM_SYMBOL_OFFSET(sym) ((unsigned long)(sym) - \
34 (unsigned long)pm_sram->do_wfi)
36 #define RTC_SCRATCH_RESUME_REG 0
37 #define RTC_SCRATCH_MAGIC_REG 1
38 #define RTC_REG_BOOT_MAGIC 0x8cd0 /* RTC */
39 #define GIC_INT_SET_PENDING_BASE 0x200
40 #define AM43XX_GIC_DIST_BASE 0x48241000
42 static u32 rtc_magic_val
;
44 static int (*am33xx_do_wfi_sram
)(unsigned long unused
);
45 static phys_addr_t am33xx_do_wfi_sram_phys
;
47 static struct gen_pool
*sram_pool
, *sram_pool_data
;
48 static unsigned long ocmcram_location
, ocmcram_location_data
;
50 static struct rtc_device
*omap_rtc
;
51 static void __iomem
*gic_dist_base
;
53 static struct am33xx_pm_platform_data
*pm_ops
;
54 static struct am33xx_pm_sram_addr
*pm_sram
;
56 static struct device
*pm33xx_dev
;
57 static struct wkup_m3_ipc
*m3_ipc
;
60 static int rtc_only_idle
;
61 static int retrigger_irq
;
62 static unsigned long suspend_wfi_flags
;
64 static struct wkup_m3_wakeup_src wakeup_src
= {.irq_nr
= 0,
68 static struct wkup_m3_wakeup_src rtc_alarm_wakeup
= {
69 .irq_nr
= 108, .src
= "RTC Alarm",
72 static struct wkup_m3_wakeup_src rtc_ext_wakeup
= {
73 .irq_nr
= 0, .src
= "Ext wakeup",
77 static u32
sram_suspend_address(unsigned long addr
)
79 return ((unsigned long)am33xx_do_wfi_sram
+
80 AMX3_PM_SRAM_SYMBOL_OFFSET(addr
));
83 static int am33xx_push_sram_idle(void)
85 struct am33xx_pm_ro_sram_data ro_sram_data
;
87 u32 table_addr
, ro_data_addr
;
90 ro_sram_data
.amx3_pm_sram_data_virt
= ocmcram_location_data
;
91 ro_sram_data
.amx3_pm_sram_data_phys
=
92 gen_pool_virt_to_phys(sram_pool_data
, ocmcram_location_data
);
93 ro_sram_data
.rtc_base_virt
= pm_ops
->get_rtc_base_addr();
95 /* Save physical address to calculate resume offset during pm init */
96 am33xx_do_wfi_sram_phys
= gen_pool_virt_to_phys(sram_pool
,
99 am33xx_do_wfi_sram
= sram_exec_copy(sram_pool
, (void *)ocmcram_location
,
101 *pm_sram
->do_wfi_sz
);
102 if (!am33xx_do_wfi_sram
) {
104 "PM: %s: am33xx_do_wfi copy to sram failed\n",
110 sram_suspend_address((unsigned long)pm_sram
->emif_sram_table
);
111 ret
= ti_emif_copy_pm_function_table(sram_pool
, (void *)table_addr
);
114 "PM: %s: EMIF function copy failed\n", __func__
);
115 return -EPROBE_DEFER
;
119 sram_suspend_address((unsigned long)pm_sram
->ro_sram_data
);
120 copy_addr
= sram_exec_copy(sram_pool
, (void *)ro_data_addr
,
122 sizeof(ro_sram_data
));
125 "PM: %s: ro_sram_data copy to sram failed\n",
133 static int __init
am43xx_map_gic(void)
135 gic_dist_base
= ioremap(AM43XX_GIC_DIST_BASE
, SZ_4K
);
143 #ifdef CONFIG_SUSPEND
144 static struct wkup_m3_wakeup_src
rtc_wake_src(void)
148 i
= __raw_readl(pm_ops
->get_rtc_base_addr() + 0x44) & 0x40;
151 retrigger_irq
= rtc_alarm_wakeup
.irq_nr
;
152 return rtc_alarm_wakeup
;
155 retrigger_irq
= rtc_ext_wakeup
.irq_nr
;
157 return rtc_ext_wakeup
;
160 static int am33xx_rtc_only_idle(unsigned long wfi_flags
)
162 omap_rtc_power_off_program(&omap_rtc
->dev
);
163 am33xx_do_wfi_sram(wfi_flags
);
167 static int am33xx_pm_suspend(suspend_state_t suspend_state
)
171 if (suspend_state
== PM_SUSPEND_MEM
&&
172 pm_ops
->check_off_mode_enable()) {
173 pm_ops
->prepare_rtc_suspend();
174 pm_ops
->save_context();
175 suspend_wfi_flags
|= WFI_FLAG_RTC_ONLY
;
177 ret
= pm_ops
->soc_suspend(suspend_state
, am33xx_rtc_only_idle
,
180 suspend_wfi_flags
&= ~WFI_FLAG_RTC_ONLY
;
181 dev_info(pm33xx_dev
, "Entering RTC Only mode with DDR in self-refresh\n");
184 clk_restore_context();
185 pm_ops
->restore_context();
186 m3_ipc
->ops
->set_rtc_only(m3_ipc
);
187 am33xx_push_sram_idle();
190 ret
= pm_ops
->soc_suspend(suspend_state
, am33xx_do_wfi_sram
,
195 dev_err(pm33xx_dev
, "PM: Kernel suspend failure\n");
197 i
= m3_ipc
->ops
->request_pm_status(m3_ipc
);
202 "PM: Successfully put all powerdomains to target state\n");
206 "PM: Could not transition all powerdomains to target state\n");
211 "PM: CM3 returned unknown result = %d\n", i
);
215 /* print the wakeup reason */
217 wakeup_src
= rtc_wake_src();
218 pr_info("PM: Wakeup source %s\n", wakeup_src
.src
);
220 pr_info("PM: Wakeup source %s\n",
221 m3_ipc
->ops
->request_wake_src(m3_ipc
));
225 if (suspend_state
== PM_SUSPEND_MEM
&& pm_ops
->check_off_mode_enable())
226 pm_ops
->prepare_rtc_resume();
231 static int am33xx_pm_enter(suspend_state_t suspend_state
)
235 switch (suspend_state
) {
237 case PM_SUSPEND_STANDBY
:
238 ret
= am33xx_pm_suspend(suspend_state
);
247 static int am33xx_pm_begin(suspend_state_t state
)
250 struct nvmem_device
*nvmem
;
252 if (state
== PM_SUSPEND_MEM
&& pm_ops
->check_off_mode_enable()) {
253 nvmem
= devm_nvmem_device_get(&omap_rtc
->dev
,
254 "omap_rtc_scratch0");
256 nvmem_device_write(nvmem
, RTC_SCRATCH_MAGIC_REG
* 4, 4,
257 (void *)&rtc_magic_val
);
265 ret
= m3_ipc
->ops
->prepare_low_power(m3_ipc
, WKUP_M3_DEEPSLEEP
);
267 case PM_SUSPEND_STANDBY
:
268 ret
= m3_ipc
->ops
->prepare_low_power(m3_ipc
, WKUP_M3_STANDBY
);
275 static void am33xx_pm_end(void)
278 struct nvmem_device
*nvmem
;
280 nvmem
= devm_nvmem_device_get(&omap_rtc
->dev
, "omap_rtc_scratch0");
284 m3_ipc
->ops
->finish_low_power(m3_ipc
);
288 * 32 bits of Interrupt Set-Pending correspond to 32
289 * 32 interrupts. Compute the bit offset of the
290 * Interrupt and set that particular bit
291 * Compute the register offset by dividing interrupt
292 * number by 32 and mutiplying by 4
294 writel_relaxed(1 << (retrigger_irq
& 31),
295 gic_dist_base
+ GIC_INT_SET_PENDING_BASE
296 + retrigger_irq
/ 32 * 4);
299 nvmem_device_write(nvmem
, RTC_SCRATCH_MAGIC_REG
* 4, 4,
306 static int am33xx_pm_valid(suspend_state_t state
)
309 case PM_SUSPEND_STANDBY
:
317 static const struct platform_suspend_ops am33xx_pm_ops
= {
318 .begin
= am33xx_pm_begin
,
319 .end
= am33xx_pm_end
,
320 .enter
= am33xx_pm_enter
,
321 .valid
= am33xx_pm_valid
,
323 #endif /* CONFIG_SUSPEND */
325 static void am33xx_pm_set_ipc_ops(void)
330 temp
= ti_emif_get_mem_type();
332 dev_err(pm33xx_dev
, "PM: Cannot determine memory type, no PM available\n");
335 m3_ipc
->ops
->set_mem_type(m3_ipc
, temp
);
337 /* Physical resume address to be used by ROM code */
338 resume_address
= am33xx_do_wfi_sram_phys
+
339 *pm_sram
->resume_offset
+ 0x4;
341 m3_ipc
->ops
->set_resume_address(m3_ipc
, (void *)resume_address
);
344 static void am33xx_pm_free_sram(void)
346 gen_pool_free(sram_pool
, ocmcram_location
, *pm_sram
->do_wfi_sz
);
347 gen_pool_free(sram_pool_data
, ocmcram_location_data
,
348 sizeof(struct am33xx_pm_ro_sram_data
));
352 * Push the minimal suspend-resume code to SRAM
354 static int am33xx_pm_alloc_sram(void)
356 struct device_node
*np
;
359 np
= of_find_compatible_node(NULL
, NULL
, "ti,omap3-mpu");
361 np
= of_find_compatible_node(NULL
, NULL
, "ti,omap4-mpu");
363 dev_err(pm33xx_dev
, "PM: %s: Unable to find device node for mpu\n",
369 sram_pool
= of_gen_pool_get(np
, "pm-sram", 0);
371 dev_err(pm33xx_dev
, "PM: %s: Unable to get sram pool for ocmcram\n",
377 sram_pool_data
= of_gen_pool_get(np
, "pm-sram", 1);
378 if (!sram_pool_data
) {
379 dev_err(pm33xx_dev
, "PM: %s: Unable to get sram data pool for ocmcram\n",
385 ocmcram_location
= gen_pool_alloc(sram_pool
, *pm_sram
->do_wfi_sz
);
386 if (!ocmcram_location
) {
387 dev_err(pm33xx_dev
, "PM: %s: Unable to allocate memory from ocmcram\n",
393 ocmcram_location_data
= gen_pool_alloc(sram_pool_data
,
394 sizeof(struct emif_regs_amx3
));
395 if (!ocmcram_location_data
) {
396 dev_err(pm33xx_dev
, "PM: Unable to allocate memory from ocmcram\n");
397 gen_pool_free(sram_pool
, ocmcram_location
, *pm_sram
->do_wfi_sz
);
406 static int am33xx_pm_rtc_setup(void)
408 struct device_node
*np
;
409 unsigned long val
= 0;
410 struct nvmem_device
*nvmem
;
412 np
= of_find_node_by_name(NULL
, "rtc");
414 if (of_device_is_available(np
)) {
415 omap_rtc
= rtc_class_open("rtc0");
417 pr_warn("PM: rtc0 not available");
418 return -EPROBE_DEFER
;
421 nvmem
= devm_nvmem_device_get(&omap_rtc
->dev
,
422 "omap_rtc_scratch0");
423 if (!IS_ERR(nvmem
)) {
424 nvmem_device_read(nvmem
, RTC_SCRATCH_MAGIC_REG
* 4,
425 4, (void *)&rtc_magic_val
);
426 if ((rtc_magic_val
& 0xffff) != RTC_REG_BOOT_MAGIC
)
427 pr_warn("PM: bootloader does not support rtc-only!\n");
429 nvmem_device_write(nvmem
, RTC_SCRATCH_MAGIC_REG
* 4,
431 val
= pm_sram
->resume_address
;
432 nvmem_device_write(nvmem
, RTC_SCRATCH_RESUME_REG
* 4,
436 pr_warn("PM: no-rtc available, rtc-only mode disabled.\n");
442 static int am33xx_pm_probe(struct platform_device
*pdev
)
444 struct device
*dev
= &pdev
->dev
;
447 if (!of_machine_is_compatible("ti,am33xx") &&
448 !of_machine_is_compatible("ti,am43"))
451 pm_ops
= dev
->platform_data
;
453 dev_err(dev
, "PM: Cannot get core PM ops!\n");
457 ret
= am43xx_map_gic();
459 pr_err("PM: Could not ioremap GIC base\n");
463 pm_sram
= pm_ops
->get_sram_addrs();
465 dev_err(dev
, "PM: Cannot get PM asm function addresses!!\n");
469 m3_ipc
= wkup_m3_ipc_get();
471 pr_err("PM: Cannot get wkup_m3_ipc handle\n");
472 return -EPROBE_DEFER
;
477 ret
= am33xx_pm_alloc_sram();
481 ret
= am33xx_pm_rtc_setup();
485 ret
= am33xx_push_sram_idle();
489 am33xx_pm_set_ipc_ops();
491 #ifdef CONFIG_SUSPEND
492 suspend_set_ops(&am33xx_pm_ops
);
495 * For a system suspend we must flush the caches, we want
496 * the DDR in self-refresh, we want to save the context
497 * of the EMIF, and we want the wkup_m3 to handle low-power
500 suspend_wfi_flags
|= WFI_FLAG_FLUSH_CACHE
;
501 suspend_wfi_flags
|= WFI_FLAG_SELF_REFRESH
;
502 suspend_wfi_flags
|= WFI_FLAG_SAVE_EMIF
;
503 suspend_wfi_flags
|= WFI_FLAG_WAKE_M3
;
504 #endif /* CONFIG_SUSPEND */
506 ret
= pm_ops
->init();
508 dev_err(dev
, "Unable to call core pm init!\n");
510 goto err_put_wkup_m3_ipc
;
516 wkup_m3_ipc_put(m3_ipc
);
518 am33xx_pm_free_sram();
523 static int am33xx_pm_remove(struct platform_device
*pdev
)
525 suspend_set_ops(NULL
);
526 wkup_m3_ipc_put(m3_ipc
);
527 am33xx_pm_free_sram();
531 static struct platform_driver am33xx_pm_driver
= {
535 .probe
= am33xx_pm_probe
,
536 .remove
= am33xx_pm_remove
,
538 module_platform_driver(am33xx_pm_driver
);
540 MODULE_ALIAS("platform:pm33xx");
541 MODULE_LICENSE("GPL v2");
542 MODULE_DESCRIPTION("am33xx power management driver");