1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <console/console.h>
6 #include <soc/mt6359p.h>
10 static const struct pmic_setting key_protect_setting
[] = {
11 {0x3A8, 0x9CA6, 0xFFFF, 0},
12 {0x44A, 0xBADE, 0xFFFF, 0},
13 {0xA3A, 0x4729, 0xFFFF, 0},
14 {0xC58, 0x1605, 0xFFFF, 0},
15 {0xC5A, 0x1706, 0xFFFF, 0},
16 {0xC5C, 0x1807, 0xFFFF, 0},
17 {0xFB4, 0x6359, 0xFFFF, 0},
18 {0x1432, 0x5543, 0xFFFF, 0},
21 static const struct pmic_efuse efuse_setting
[] = {
22 {79, 0xa0e, 0x1, 0xf},
23 {886, 0x198c, 0xf, 0x8},
24 {890, 0x198e, 0xf, 0x0},
25 {902, 0x1998, 0xf, 0x8},
26 {906, 0x1998, 0xf, 0xc},
27 {918, 0x19a2, 0xf, 0x8},
28 {922, 0x19a2, 0xf, 0xc},
29 {1014, 0x19ae, 0xf, 0x7},
30 {1018, 0x19ae, 0xf, 0xb},
31 {1158, 0x1a0a, 0xf, 0x7},
32 {1162, 0x1a0a, 0xf, 0xb},
33 {1206, 0x1a16, 0xf, 0x7},
34 {1210, 0x1a16, 0xf, 0xb},
35 {1254, 0x1a22, 0xf, 0x7},
36 {1258, 0x1a22, 0xf, 0xb},
37 {1304, 0x1a2c, 0x7, 0x4},
38 {1307, 0x1a32, 0x7, 0x8},
39 {1336, 0x1a34, 0x7, 0x4},
40 {1339, 0x1a3a, 0x7, 0x8},
41 {1683, 0x79c, 0xf, 0x4},
42 {1688, 0xc8a, 0x1, 0x3},
43 {1689, 0xc88, 0x1, 0x3},
44 {1690, 0xc88, 0x7, 0x0},
47 static struct pmif
*pmif_arb
= NULL
;
48 static void mt6359p_write(u32 reg
, u32 data
)
50 pmif_arb
->write(pmif_arb
, 0, reg
, data
);
53 static u32
mt6359p_read_field(u32 reg
, u32 mask
, u32 shift
)
55 return pmif_arb
->read_field(pmif_arb
, 0, reg
, mask
, shift
);
58 void mt6359p_write_field(u32 reg
, u32 val
, u32 mask
, u32 shift
)
60 pmif_arb
->write_field(pmif_arb
, 0, reg
, val
, mask
, shift
);
63 static void pmic_set_power_hold(void)
65 mt6359p_write_field(PMIC_PWRHOLD
, 0x1, 0x1, 0);
68 static void pmic_wdt_set(void)
70 /* [5]=1, RG_WDTRSTB_DEB */
71 mt6359p_write_field(PMIC_TOP_RST_MISC_SET
, 0x20, 0xFFFF, 0);
72 /* [1]=0, RG_WDTRSTB_MODE */
73 mt6359p_write_field(PMIC_TOP_RST_MISC_CLR
, 0x02, 0xFFFF, 0);
74 /* [0]=1, RG_WDTRSTB_EN */
75 mt6359p_write_field(PMIC_TOP_RST_MISC_SET
, 0x01, 0xFFFF, 0);
78 static void pmic_protect_key_setting(bool lock
)
80 for (int i
= 0; i
< ARRAY_SIZE(key_protect_setting
); i
++)
81 mt6359p_write(key_protect_setting
[i
].addr
,
82 lock
? 0 : key_protect_setting
[i
].val
);
85 static int check_idle(u32 timeout
, u32 addr
, u32 mask
)
87 if (!wait_us(timeout
, !mt6359p_read_field(addr
, mask
, 0)))
93 static u32
pmic_read_efuse(u32 efuse_bit
, u32 mask
)
98 index
= efuse_bit
/ 16;
99 shift
= efuse_bit
% 16;
100 mt6359p_write_field(PMIC_TOP_CKHWEN_CON0
, 0, 0x1, 2);
101 mt6359p_write_field(PMIC_TOP_CKPDN_CON0
, 0, 0x1, 4);
102 mt6359p_write_field(PMIC_OTP_CON11
, 1, 0x1, 0);
103 mt6359p_write_field(PMIC_OTP_CON0
, index
* 2, 0xFF, 0);
104 if (mt6359p_read_field(PMIC_OTP_CON8
, 1, 0))
105 mt6359p_write_field(PMIC_OTP_CON8
, 0, 1, 0);
107 mt6359p_write_field(PMIC_OTP_CON8
, 1, 1, 0);
110 if (check_idle(EFUSE_WAIT_US
, PMIC_OTP_CON13
, EFUSE_BUSY
))
111 die("[%s] timeout after %d usecs\n", __func__
, EFUSE_WAIT_US
);
115 efuse_data
= mt6359p_read_field(PMIC_OTP_CON12
, 0xFFFF, 0);
116 efuse_data
= (efuse_data
>> shift
) & mask
;
118 mt6359p_write_field(PMIC_TOP_CKHWEN_CON0
, 1, 0x1, 2);
119 mt6359p_write_field(PMIC_TOP_CKPDN_CON0
, 1, 0x1, 4);
124 static void pmic_efuse_setting(void)
131 for (int i
= 0; i
< ARRAY_SIZE(efuse_setting
); i
++) {
132 efuse_data
= pmic_read_efuse(efuse_setting
[i
].efuse_bit
, efuse_setting
[i
].mask
);
133 mt6359p_write_field(efuse_setting
[i
].addr
, efuse_data
,
134 efuse_setting
[i
].mask
, efuse_setting
[i
].shift
);
137 efuse_data
= pmic_read_efuse(EFUSE_RG_VPA_OC_FT
, 0x1);
139 /* restore VPA_DLC initial setting */
140 mt6359p_write(PMIC_BUCK_VPA_DLC_CON0
, 0x2810);
141 mt6359p_write(PMIC_BUCK_VPA_DLC_CON1
, 0x800);
144 printk(BIOS_DEBUG
, "%s: Set efuses in %lld msecs\n",
145 __func__
, stopwatch_duration_msecs(&sw
));
148 static void pmic_wk_vs2_voter_setting(void)
151 * 1. Set VS2_VOTER_VOSEL = 1.35V
153 * 3. Set VS2_VOSEL = 1.4V
155 mt6359p_write_field(PMIC_VS2_VOTER_CFG
, 0x2C, 0x7F, 0);
156 mt6359p_write_field(PMIC_VS2_VOTER
, 0, 0xFFF, 0);
157 mt6359p_write_field(PMIC_VS2_ELR0
, 0x30, 0x7F, 0);
160 void mt6359p_buck_set_voltage(u32 buck_id
, u32 buck_uv
)
162 u32 vol_offset
, vol_reg
, vol
, vol_step
;
165 die("ERROR: pmif_arb not initialized");
170 vol_reg
= PMIC_VGPU11_ELR0
;
173 case MT6359P_SRAM_PROC1
:
175 vol_reg
= PMIC_VSRAM_PROC1_ELR
;
178 case MT6359P_SRAM_PROC2
:
180 vol_reg
= PMIC_VSRAM_PROC2_ELR
;
185 vol_reg
= PMIC_VCORE_ELR0
;
190 vol_reg
= PMIC_VPA_CON1
;
194 die("ERROR: Unknown buck_id %u", buck_id
);
198 vol
= (buck_uv
- vol_offset
) / vol_step
;
199 mt6359p_write_field(vol_reg
, vol
, 0x7F, 0);
202 u32
mt6359p_buck_get_voltage(u32 buck_id
)
204 u32 vol_shift
, vol_offset
, vol_reg
, vol
, vol_step
;
207 die("ERROR: pmif_arb not initialized");
213 vol_reg
= PMIC_VGPU11_DBG0
;
216 case MT6359P_SRAM_PROC1
:
219 vol_reg
= PMIC_VSRAM_PROC1_VOSEL1
;
222 case MT6359P_SRAM_PROC2
:
225 vol_reg
= PMIC_VSRAM_PROC2_VOSEL1
;
231 vol_reg
= PMIC_VCORE_DBG0
;
237 vol_reg
= PMIC_VPA_DBG0
;
241 die("ERROR: Unknown buck_id %u", buck_id
);
245 vol
= mt6359p_read_field(vol_reg
, 0x7F, vol_shift
);
246 return vol_offset
+ vol
* vol_step
;
249 void mt6359p_set_vm18_voltage(u32 vm18_uv
)
251 u32 reg_vol
, reg_cali
;
254 die("ERROR: pmif_arb not initialized");
256 assert(vm18_uv
>= 1700000);
257 assert(vm18_uv
< 2000000);
259 reg_vol
= (vm18_uv
/ 1000 - VM18_VOL_OFFSET
) / 100;
260 reg_cali
= ((vm18_uv
/ 1000) % 100) / 10;
261 mt6359p_write(PMIC_VM18_ANA_CON0
, (reg_vol
<< VM18_VOL_REG_SHIFT
) | reg_cali
);
264 u32
mt6359p_get_vm18_voltage(void)
266 u32 reg_vol
, reg_cali
;
269 die("ERROR: pmif_arb not initialized");
271 reg_vol
= 100 * mt6359p_read_field(PMIC_VM18_ANA_CON0
, 0xF, VM18_VOL_REG_SHIFT
);
272 reg_cali
= 10 * mt6359p_read_field(PMIC_VM18_ANA_CON0
, 0xF, 0);
273 return 1000 * (VM18_VOL_OFFSET
+ reg_vol
+ reg_cali
);
276 void mt6359p_set_vsim1_voltage(u32 vsim1_uv
)
278 u32 reg_vol
, reg_cali
;
281 die("ERROR: pmif_arb not initialized");
283 if ((vsim1_uv
>= 1700000) && (vsim1_uv
<= 1900000))
284 reg_vol
= (vsim1_uv
/ 1000 - VSIM1_VOL_OFFSET_1
) / 100;
285 else if ((vsim1_uv
>= 2700000) && (vsim1_uv
<= 2800000))
286 reg_vol
= (vsim1_uv
/ 1000 - VSIM1_VOL_OFFSET_2
) / 100;
287 else if ((vsim1_uv
>= 3000000) && (vsim1_uv
<= 3200000))
288 reg_vol
= (vsim1_uv
/ 1000 - VSIM1_VOL_OFFSET_2
) / 100;
290 die("ERROR: Unknown vsim1 voltage %u", vsim1_uv
);
292 reg_cali
= ((vsim1_uv
/ 1000) % 100) / 10;
293 mt6359p_write(PMIC_VSIM1_ANA_CON0
, (reg_vol
<< VSIM1_VOL_REG_SHIFT
) | reg_cali
);
296 u32
mt6359p_get_vsim1_voltage(void)
298 u32 reg_vol
, reg_cali
, reg_offset
;
301 die("ERROR: pmif_arb not initialized");
303 reg_vol
= 100 * mt6359p_read_field(PMIC_VSIM1_ANA_CON0
, 0xF,
304 VSIM1_VOL_REG_SHIFT
);
305 reg_cali
= 10 * mt6359p_read_field(PMIC_VSIM1_ANA_CON0
, 0xF, 0);
307 if ((reg_vol
== 300) || (reg_vol
== 400))
308 reg_offset
= VSIM1_VOL_OFFSET_1
;
309 else if ((reg_vol
== 800) || (reg_vol
== 1100) || (reg_vol
== 1200))
310 reg_offset
= VSIM1_VOL_OFFSET_2
;
312 die("ERROR: Unknown vsim1 reg_vol %x", reg_vol
);
314 return 1000 * (reg_offset
+ reg_vol
+ reg_cali
);
317 void mt6359p_enable_vpa(bool enable
)
319 mt6359p_write_field(PMIC_VPA_CON0
, enable
, 0x1, 0);
322 void mt6359p_enable_vsim1(bool enable
)
324 mt6359p_write_field(PMIC_VSIM1_CON0
, enable
, 0x1, 0);
327 void mt6359p_enable_vm18(bool enable
)
329 mt6359p_write_field(PMIC_VM18_CON0
, enable
, 0x1, 0);
332 void mt6359p_init_pmif_arb(void)
335 pmif_arb
= get_pmif_controller(PMIF_SPI
, 0);
337 die("ERROR: No spi device");
340 if (pmif_arb
->is_pmif_init_done(pmif_arb
))
341 die("ERROR - Failed to initialize pmif spi");
344 void mt6359p_init(void)
346 mt6359p_init_pmif_arb();
347 pmic_set_power_hold();
349 pmic_protect_key_setting(false);
352 pmic_efuse_setting();
353 pmic_protect_key_setting(true);
354 pmic_wk_vs2_voter_setting();