1 /* SPDX-License-Identifier: GPL-2.0-only */
4 * This file is created based on MT8186 Functional Specification
10 #include <soc/rtc_common.h>
11 #include <soc/mt6366.h>
12 #include <soc/pmic_wrap.h>
15 #define MT8186_RTC_DXCO_CAPID 0xE0
17 /* Initialize RTC setting of using DCXO clock */
18 static bool rtc_enable_dcxo(void)
20 u16 bbpu
, con
, osc32con
, sec
;
22 rtc_read(RTC_BBPU
, &bbpu
);
23 rtc_write(RTC_BBPU
, bbpu
| RTC_BBPU_KEY
| RTC_BBPU_RELOAD
);
25 if (!rtc_write_trigger()) {
26 rtc_info("rtc_write_trigger() failed\n");
31 if (!rtc_writeif_unlock()) {
32 rtc_info("rtc_writeif_unlock() failed\n");
36 rtc_read(RTC_OSC32CON
, &osc32con
);
37 osc32con
&= ~(RTC_EMBCK_SRC_SEL
| RTC_EMBCK_SEL_MODE_MASK
39 osc32con
|= RTC_XOSC32_ENB
| RTC_REG_XOSC32_ENB
40 | RTC_EMB_K_EOSC32_MODE
| RTC_EMBCK_SEL_OPTION
;
41 if (!rtc_xosc_write(osc32con
)) {
42 rtc_info("rtc_xosc_write() failed\n");
46 rtc_read(RTC_CON
, &con
);
47 rtc_read(RTC_OSC32CON
, &osc32con
);
48 rtc_read(RTC_AL_SEC
, &sec
);
49 rtc_info("con = %#x, osc32con = %#x, sec = %#x\n", con
, osc32con
, sec
);
54 /* Initialize RTC related gpio */
55 bool rtc_gpio_init(void)
59 /* RTC_32K1V8 clock change from 128k div 4 source to RTC 32k source */
60 pwrap_write_field(PMIC_RG_TOP_CKSEL_CON0_SET
, 0x1, 0x1, 3);
62 /* Export 32K clock RTC_32K1V8_1 */
63 pwrap_write_field(PMIC_RG_TOP_CKPDN_CON1_CLR
, 0x1, 0x1, 1);
65 /* Export 32K clock RTC_32K2V8 */
66 rtc_read(RTC_CON
, &con
);
67 con
&= (RTC_CON_LPSTA_RAW
| RTC_CON_LPRST
| RTC_CON_EOSC32_LPEN
68 | RTC_CON_XOSC32_LPEN
);
69 con
|= (RTC_CON_GPEN
| RTC_CON_GOE
);
70 con
&= ~RTC_CON_F32KOB
;
71 rtc_write(RTC_CON
, con
);
73 return rtc_write_trigger();
76 u16
rtc_get_frequency_meter(u16 val
, u16 measure_src
, u16 window_size
)
79 u16 fqmtr_busy
, fqmtr_data
, fqmtr_rst
, fqmtr_tcksel
;
81 rtc_read(RTC_BBPU
, &bbpu
);
82 rtc_write(RTC_BBPU
, bbpu
| RTC_BBPU_KEY
| RTC_BBPU_RELOAD
);
83 if (!rtc_write_trigger()) {
84 rtc_info("rtc_write_trigger() failed\n");
88 rtc_read(RTC_OSC32CON
, &osc32con
);
89 if (!rtc_xosc_write((osc32con
& ~RTC_XOSCCALI_MASK
) |
90 (val
& RTC_XOSCCALI_MASK
))) {
91 rtc_info("rtc_xosc_write() failed\n");
95 /* Enable FQMTR clock */
96 pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_CLR
, 1, 1,
97 PMIC_RG_FQMTR_32K_CK_PDN_SHIFT
);
98 pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_CLR
, 1, 1,
99 PMIC_RG_FQMTR_CK_PDN_SHIFT
);
102 pwrap_write_field(PMIC_RG_FQMTR_RST
, 1, 1, PMIC_FQMTR_RST_SHIFT
);
104 rtc_read(PMIC_RG_FQMTR_DATA
, &fqmtr_data
);
105 rtc_read(PMIC_RG_FQMTR_CON0
, &fqmtr_busy
);
106 } while (fqmtr_data
&& (fqmtr_busy
& PMIC_FQMTR_CON0_BUSY
));
107 rtc_read(PMIC_RG_FQMTR_RST
, &fqmtr_rst
);
109 pwrap_write_field(PMIC_RG_FQMTR_RST
, 0, 1, PMIC_FQMTR_RST_SHIFT
);
111 /* Set frequency meter window value (0=1X32K(fixed clock)) */
112 rtc_write(PMIC_RG_FQMTR_WINSET
, window_size
);
113 /* Enable 26M and set test clock source */
114 rtc_write(PMIC_RG_FQMTR_CON0
, PMIC_FQMTR_CON0_DCXO26M_EN
| measure_src
);
115 /* Enable 26M -> delay 100us -> enable FQMTR */
117 rtc_read(PMIC_RG_FQMTR_CON0
, &fqmtr_tcksel
);
119 rtc_write(PMIC_RG_FQMTR_CON0
, fqmtr_tcksel
| PMIC_FQMTR_CON0_FQMTR_EN
);
122 /* FQMTR read until ready */
123 if (!wait_us(FQMTR_TIMEOUT_US
,
124 rtc_read(PMIC_RG_FQMTR_CON0
, &fqmtr_busy
) == 0 &&
125 !(fqmtr_busy
& PMIC_FQMTR_CON0_BUSY
))) {
126 rtc_info("get frequency time out: %#x\n", fqmtr_busy
);
130 /* Read data should be closed to 26M/32k = 794 */
131 rtc_read(PMIC_RG_FQMTR_DATA
, &fqmtr_data
);
133 rtc_read(PMIC_RG_FQMTR_CON0
, &fqmtr_tcksel
);
135 rtc_write(PMIC_RG_FQMTR_CON0
, fqmtr_tcksel
& ~PMIC_FQMTR_CON0_FQMTR_EN
);
136 /* Disable FQMTR -> delay 100us -> disable 26M */
139 rtc_read(PMIC_RG_FQMTR_CON0
, &fqmtr_tcksel
);
140 rtc_write(PMIC_RG_FQMTR_CON0
,
141 fqmtr_tcksel
& ~PMIC_FQMTR_CON0_DCXO26M_EN
);
142 rtc_info("input = %#x, output = %#x\n", val
, fqmtr_data
);
144 /* Disable FQMTR clock */
145 pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_SET
, 1, 1,
146 PMIC_RG_FQMTR_32K_CK_PDN_SHIFT
);
147 pwrap_write_field(PMIC_RG_TOP_CKPDN_CON0_SET
, 1, 1,
148 PMIC_RG_FQMTR_CK_PDN_SHIFT
);
153 /* Low power detect setting */
154 static bool rtc_lpd_init(void)
158 /* Set RTC_LPD_OPT */
159 rtc_read(RTC_AL_SEC
, &sec
);
160 sec
|= RTC_LPD_OPT_F32K_CK_ALIVE
;
161 rtc_write(RTC_AL_SEC
, sec
);
162 if (!rtc_write_trigger()) {
163 rtc_info("rtc_write_trigger() failed\n");
167 /* Initialize XOSC32 to detect 32k clock stop */
168 rtc_read(RTC_CON
, &con
);
169 con
|= RTC_CON_XOSC32_LPEN
;
173 /* Initialize EOSC32 to detect RTC low power */
174 rtc_read(RTC_CON
, &con
);
175 con
|= RTC_CON_EOSC32_LPEN
;
179 rtc_read(RTC_CON
, &con
);
180 con
&= ~RTC_CON_XOSC32_LPEN
;
181 rtc_write(RTC_CON
, con
);
183 /* Set RTC_LPD_OPT */
184 rtc_read(RTC_AL_SEC
, &sec
);
185 sec
&= ~RTC_LPD_OPT_MASK
;
186 sec
|= RTC_LPD_OPT_EOSC_LPD
;
187 rtc_write(RTC_AL_SEC
, sec
);
188 if (!rtc_write_trigger()) {
189 rtc_info("rtc_write_trigger() failed\n");
196 static bool rtc_hw_init(void)
200 rtc_read(RTC_BBPU
, &bbpu
);
201 rtc_write(RTC_BBPU
, bbpu
| RTC_BBPU_KEY
| RTC_BBPU_INIT
);
202 if (!rtc_write_trigger()) {
203 rtc_info("rtc_write_trigger() failed\n");
209 rtc_read(RTC_BBPU
, &bbpu
);
210 rtc_write(RTC_BBPU
, bbpu
| RTC_BBPU_KEY
| RTC_BBPU_RELOAD
);
211 if (!rtc_write_trigger()) {
212 rtc_info("rtc_write_trigger() failed\n");
216 rtc_read(RTC_BBPU
, &bbpu
);
217 if (bbpu
& RTC_BBPU_INIT
) {
218 rtc_info("timeout\n");
225 static void mt6366_dcxo_disable_unused(void)
227 /* Disable clock buffer XO_CEL */
228 rtc_write(PMIC_RG_DCXO_CW00_CLR
, 0x0800);
229 /* Mask bblpm request and switch off bblpm mode */
230 rtc_write(PMIC_RG_DCXO_CW23
, 0x0052);
233 static void rtc_set_capid(u16 capid
)
237 rtc_write(PMIC_RG_DCXO_CW03
, 0xFF00 | capid
);
239 rtc_read(PMIC_RG_DCXO_CW03
, &read_capid
);
240 rtc_info("read back capid: %#x\n", read_capid
& 0xFF);
243 /* Check RTC Initialization */
244 int rtc_init(int recover
)
248 rtc_info("recovery: %d\n", recover
);
250 /* Write powerkeys to enable RTC functions */
251 if (!rtc_powerkey_init()) {
252 ret
= -RTC_STATUS_POWERKEY_INIT_FAIL
;
256 /* Write interface unlock need to be set after powerkey match */
257 if (!rtc_writeif_unlock()) {
258 ret
= -RTC_STATUS_WRITEIF_UNLOCK_FAIL
;
264 /* In recovery mode, we need 20ms delay for register setting. */
268 if (!rtc_gpio_init()) {
269 ret
= -RTC_STATUS_GPIO_INIT_FAIL
;
273 if (!rtc_hw_init()) {
274 ret
= -RTC_STATUS_HW_INIT_FAIL
;
278 if (!rtc_reg_init()) {
279 ret
= -RTC_STATUS_REG_INIT_FAIL
;
283 if (!rtc_lpd_init()) {
284 ret
= -RTC_STATUS_LPD_INIT_FAIL
;
289 * After lpd init, powerkeys need to be written again to enable
290 * low power detect function.
292 if (!rtc_powerkey_init()) {
293 ret
= -RTC_STATUS_POWERKEY_INIT_FAIL
;
297 return RTC_STATUS_OK
;
299 rtc_info("init failed: ret = %d\n", ret
);
303 /* Enable RTC bbpu */
304 void rtc_bbpu_power_on(void)
309 /* Pull powerhold high, control by pmic */
310 mt6366_set_power_hold(true);
312 /* Pull PWRBB high */
313 bbpu
= RTC_BBPU_KEY
| RTC_BBPU_AUTO
| RTC_BBPU_RELOAD
| RTC_BBPU_PWREN
;
314 rtc_write(RTC_BBPU
, bbpu
);
315 ret
= rtc_write_trigger();
316 rtc_info("rtc_write_trigger = %d\n", ret
);
318 rtc_read(RTC_BBPU
, &bbpu
);
319 rtc_info("done BBPU = %#x\n", bbpu
);
322 static void dcxo_init(void)
325 rtc_write(PMIC_RG_DCXO_CW15
, 0xA2AA);
326 rtc_write(PMIC_RG_DCXO_CW13
, 0x98E9);
327 rtc_write(PMIC_RG_DCXO_CW16
, 0x9855);
329 /* 26M enable control */
330 /* Enable clock buffer XO_SOC, XO_CEL */
331 rtc_write(PMIC_RG_DCXO_CW00
, 0x4805);
332 rtc_write(PMIC_RG_DCXO_CW11
, 0x8000);
334 /* Load thermal coefficient */
335 rtc_write(PMIC_RG_TOP_TMA_KEY
, 0x9CA7);
336 rtc_write(PMIC_RG_DCXO_CW21
, 0x12A7);
337 rtc_write(PMIC_RG_DCXO_ELR0
, 0xD004);
338 rtc_write(PMIC_RG_TOP_TMA_KEY
, 0x0000);
340 /* Adjust OSC FPM setting */
341 rtc_write(PMIC_RG_DCXO_CW07
, 0x8FFE);
343 /* Re-calibrate OSC current */
344 rtc_write(PMIC_RG_DCXO_CW09
, 0x008F);
346 rtc_write(PMIC_RG_DCXO_CW09
, 0x408F);
349 rtc_set_capid(MT8186_RTC_DXCO_CAPID
);
351 mt6366_dcxo_disable_unused();
354 /* Initialize rtc boot flow */
357 /* DCXO clock initialized settings */
360 /* DCXO 32k initialized settings */
361 pwrap_write_field(PMIC_RG_DCXO_CW02
, 0xF, 0xF, 0);
362 pwrap_write_field(PMIC_RG_SCK_TOP_CON0
, 0x1, 0x1, 0);
364 /* Use DCXO 32K clock */
365 if (!rtc_enable_dcxo())
366 rtc_info("rtc_enable_dcxo() failed\n");