1 /*
2  * Copyright (c) 2020, MediaTek Inc. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <stddef.h>
8 
9 #include <lib/mmio.h>
10 
11 #include <mt_spm_notifier.h>
12 #include <mt_spm_sspm_intc.h>
13 
14 #define MT_SPM_SSPM_MBOX_OFF(x)		(SSPM_MBOX_BASE + x)
15 #define MT_SPM_MBOX(slot)		MT_SPM_SSPM_MBOX_OFF((slot << 2UL))
16 
17 #define SSPM_MBOX_SPM_LP_LOOKUP1	MT_SPM_MBOX(0)
18 #define SSPM_MBOX_SPM_LP_LOOKUP2	MT_SPM_MBOX(1)
19 #define SSPM_MBOX_SPM_LP1		MT_SPM_MBOX(2)
20 #define SSPM_MBOX_SPM_LP2		MT_SPM_MBOX(3)
21 
22 #define MCUPM_MBOX_OFFSET_LP		0x0C55FDA4
23 #define MCUPM_MBOX_ENTER_LP		0x454e0000
24 #define MCUPM_MBOX_LEAVE_LP		0x4c450000
25 #define MCUPM_MBOX_SLEEP_MASK		0x0000FFFF
26 
mt_spm_sspm_notify(int type,unsigned int lp_mode)27 int mt_spm_sspm_notify(int type, unsigned int lp_mode)
28 {
29 	switch (type) {
30 	case MT_SPM_NOTIFY_LP_ENTER:
31 		mmio_write_32(SSPM_MBOX_SPM_LP1, lp_mode);
32 		mmio_write_32(MCUPM_MBOX_OFFSET_LP, MCUPM_MBOX_ENTER_LP |
33 			      (lp_mode & MCUPM_MBOX_SLEEP_MASK));
34 		DO_SPM_SSPM_LP_SUSPEND();
35 		break;
36 	case MT_SPM_NOTIFY_LP_LEAVE:
37 		mmio_write_32(SSPM_MBOX_SPM_LP1, lp_mode);
38 		mmio_write_32(MCUPM_MBOX_OFFSET_LP, MCUPM_MBOX_LEAVE_LP |
39 			      (lp_mode & MCUPM_MBOX_SLEEP_MASK));
40 		DO_SPM_SSPM_LP_RESUME();
41 		break;
42 	default:
43 		break;
44 	}
45 
46 	return 0;
47 }
48