xref: /rk3399_ARM-atf/plat/mediatek/drivers/spm/version/pmic_wrap/v1/mt_spm_pmic_wrap.c (revision cf2df874cd09305ac7282fadb0fef6be597dfffb)
1 /*
2  * Copyright (c) 2025, Mediatek Inc. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <lib/libc/errno.h>
8 
9 #include <lib/mmio.h>
10 
11 #include <pmic_wrap/inc/mt_spm_pmic_wrap.h>
12 
13 #define SPM_DATA_SHIFT	16
14 
15 static struct pmic_wrap_setting *pmic_wrap;
16 
mt_spm_pmic_wrap_set_phase(unsigned int phase)17 int mt_spm_pmic_wrap_set_phase(unsigned int phase)
18 {
19 	int idx;
20 	uint32_t cmd_addr, cmd_data;
21 	struct pmic_wrap_phase_setting *current_phase;
22 
23 	if (!pmic_wrap)
24 		return -ENODEV;
25 
26 	if (phase >= pmic_wrap->phase_nr_idx)
27 		return -EINVAL;
28 
29 	current_phase = &pmic_wrap->phase[phase];
30 
31 	for (idx = 0; idx < current_phase->nr_idx; idx++) {
32 		cmd_addr = current_phase->cmd[idx].cmd_addr;
33 		cmd_data = current_phase->cmd[idx].cmd_data;
34 
35 		mmio_write_32(current_phase->cmd[idx].spm_pwrap_addr,
36 			      (cmd_addr << SPM_DATA_SHIFT) | cmd_data);
37 	}
38 	return 0;
39 }
40 
mt_spm_pmic_wrap_set_cmd(unsigned int phase,unsigned int idx,unsigned int cmd_data)41 int mt_spm_pmic_wrap_set_cmd(unsigned int phase,
42 			     unsigned int idx, unsigned int cmd_data)
43 {
44 	uint32_t cmd_addr;
45 	struct pmic_wrap_phase_setting *current_phase;
46 
47 	if (!pmic_wrap)
48 		return -ENODEV;
49 
50 	if (phase >= pmic_wrap->phase_nr_idx)
51 		return -EINVAL;
52 
53 	if (idx >= pmic_wrap->phase[phase].nr_idx)
54 		return -EINVAL;
55 
56 	current_phase = &pmic_wrap->phase[phase];
57 	current_phase->cmd[idx].cmd_data = cmd_data;
58 	cmd_addr = current_phase->cmd[idx].cmd_addr;
59 
60 	mmio_write_32(current_phase->cmd[idx].spm_pwrap_addr,
61 		      (cmd_addr << SPM_DATA_SHIFT) | cmd_data);
62 	return 0;
63 }
64 
mt_spm_pmic_wrap_get_cmd(unsigned int phase,unsigned int idx)65 unsigned long mt_spm_pmic_wrap_get_cmd(unsigned int phase, unsigned int idx)
66 {
67 	if (!pmic_wrap)
68 		return 0;
69 
70 	if (phase >= pmic_wrap->phase_nr_idx)
71 		return 0;
72 
73 	if (idx >= pmic_wrap->phase[phase].nr_idx)
74 		return 0;
75 
76 	return pmic_wrap->phase[phase].cmd[idx].cmd_data;
77 }
78 
mt_spm_pmic_wrap_set_table(struct pmic_wrap_setting * pw)79 void mt_spm_pmic_wrap_set_table(struct pmic_wrap_setting *pw)
80 {
81 	pmic_wrap = pw;
82 }
83