1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <soc/rtc_common.h>
6 /* 32k clock calibration */
7 static int rtc_eosc_cali(void)
9 u16 diff_left
, diff_right
, cksel
;
12 u16 left
= RTC_XOSCCALI_START
;
13 u16 right
= RTC_XOSCCALI_END
;
15 rtc_read(PMIC_RG_FQMTR_CKSEL
, &cksel
);
16 cksel
&= ~PMIC_FQMTR_CKSEL_MASK
;
17 /* select EOSC_32 as fixed clock */
18 rtc_write(PMIC_RG_FQMTR_CKSEL
, cksel
| PMIC_FQMTR_FIX_CLK_EOSC_32K
);
19 rtc_read(PMIC_RG_FQMTR_CKSEL
, &cksel
);
20 rtc_info("PMIC_RG_FQMTR_CKSEL=%#x\n", cksel
);
22 while (left
<= right
) {
23 middle
= (right
+ left
) / 2;
27 /* select 26M as target clock */
28 val
= rtc_get_frequency_meter(middle
, PMIC_FQMTR_CON0_FQM26M_CK
, 0);
29 if (val
>= RTC_FQMTR_LOW_BASE
&& val
<= RTC_FQMTR_HIGH_BASE
)
32 if (val
> RTC_FQMTR_HIGH_BASE
)
38 if (val
>= RTC_FQMTR_LOW_BASE
&& val
<= RTC_FQMTR_HIGH_BASE
)
41 val
= rtc_get_frequency_meter(left
, PMIC_FQMTR_CON0_FQM26M_CK
, 0);
42 diff_left
= ABS(val
- RTC_FQMTR_LOW_BASE
);
44 val
= rtc_get_frequency_meter(right
, PMIC_FQMTR_CON0_FQM26M_CK
, 0);
45 diff_right
= ABS(val
- RTC_FQMTR_LOW_BASE
);
47 rtc_info("left: %d, middle: %d, right: %d\n", left
, middle
, right
);
48 if (diff_left
< diff_right
)
54 void rtc_osc_init(void)
58 /* enable 32K export */
60 /* calibrate eosc32 for powerdown clock */
61 rtc_read(RTC_OSC32CON
, &osc32con
);
62 rtc_info("osc32con val = %#x\n", osc32con
);
63 osc32con
&= ~RTC_XOSCCALI_MASK
;
64 osc32con
|= rtc_eosc_cali() & RTC_XOSCCALI_MASK
;
65 rtc_xosc_write(osc32con
);
66 rtc_info("EOSC32 cali val = %#x\n", osc32con
);