2020-04-04 18:51:11 +02:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0-only */
|
2018-10-26 09:18:09 +02:00
|
|
|
|
|
|
|
#include <soc/rtc_common.h>
|
|
|
|
#include <soc/rtc.h>
|
|
|
|
#include <soc/pmic_wrap.h>
|
2019-03-21 09:55:49 +01:00
|
|
|
#include <timer.h>
|
2018-10-26 09:18:09 +02:00
|
|
|
|
|
|
|
/* ensure rtc write success */
|
|
|
|
int rtc_busy_wait(void)
|
|
|
|
{
|
|
|
|
struct stopwatch sw;
|
|
|
|
u16 bbpu;
|
|
|
|
|
|
|
|
stopwatch_init_usecs_expire(&sw, RTC_CBUSY_TIMEOUT_US);
|
|
|
|
|
|
|
|
do {
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_BBPU, &bbpu);
|
2018-10-26 09:18:09 +02:00
|
|
|
/* Time > 1sec, time out and set recovery mode enable.*/
|
|
|
|
if (stopwatch_expired(&sw)) {
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_info("BBPU CBUSY time out !!\n");
|
2018-10-26 09:18:09 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} while (bbpu & RTC_BBPU_CBUSY);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rtc_write_trigger(void)
|
|
|
|
{
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_WRTGR, 1);
|
2018-10-26 09:18:09 +02:00
|
|
|
return rtc_busy_wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* unlock rtc write interface */
|
|
|
|
int rtc_writeif_unlock(void)
|
|
|
|
{
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_PROT, RTC_PROT_UNLOCK1);
|
2018-10-26 09:18:09 +02:00
|
|
|
if (!rtc_write_trigger())
|
|
|
|
return 0;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_PROT, RTC_PROT_UNLOCK2);
|
2018-10-26 09:18:09 +02:00
|
|
|
if (!rtc_write_trigger())
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set rtc time */
|
|
|
|
int rtc_set(const struct rtc_time *time)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get rtc time */
|
|
|
|
int rtc_get(struct rtc_time *time)
|
|
|
|
{
|
|
|
|
u16 value;
|
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_TC_SEC, &value);
|
2018-10-26 09:18:09 +02:00
|
|
|
time->sec = value;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_TC_MIN, &value);
|
2018-10-26 09:18:09 +02:00
|
|
|
time->min = value;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_TC_HOU, &value);
|
2018-10-26 09:18:09 +02:00
|
|
|
time->hour = value;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_TC_DOM, &value);
|
2018-10-26 09:18:09 +02:00
|
|
|
time->mday = value;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_TC_MTH, &value);
|
2018-10-26 09:18:09 +02:00
|
|
|
time->mon = value;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_TC_YEA, &value);
|
2018-10-26 09:18:09 +02:00
|
|
|
time->year = (value + RTC_MIN_YEAR_OFFSET) % 100;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* set rtc xosc setting */
|
2019-03-19 04:47:21 +01:00
|
|
|
int rtc_xosc_write(u16 val)
|
2018-10-26 09:18:09 +02:00
|
|
|
{
|
|
|
|
u16 bbpu;
|
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_OSC32CON, RTC_OSC32CON_UNLOCK1);
|
2019-04-17 09:43:14 +02:00
|
|
|
if (!rtc_busy_wait())
|
|
|
|
return 0;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_OSC32CON, RTC_OSC32CON_UNLOCK2);
|
2019-04-17 09:43:14 +02:00
|
|
|
if (!rtc_busy_wait())
|
|
|
|
return 0;
|
2018-10-26 09:18:09 +02:00
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_OSC32CON, val);
|
2019-04-17 09:43:14 +02:00
|
|
|
if (!rtc_busy_wait())
|
|
|
|
return 0;
|
2018-10-26 09:18:09 +02:00
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_BBPU, &bbpu);
|
2018-10-26 09:18:09 +02:00
|
|
|
bbpu |= RTC_BBPU_KEY | RTC_BBPU_RELOAD;
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_BBPU, bbpu);
|
|
|
|
|
|
|
|
return rtc_write_trigger();
|
2018-10-26 09:18:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize rtc related registers */
|
|
|
|
int rtc_reg_init(void)
|
|
|
|
{
|
|
|
|
u16 irqsta;
|
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_IRQ_EN, 0);
|
|
|
|
rtc_write(RTC_CII_EN, 0);
|
|
|
|
rtc_write(RTC_AL_MASK, 0);
|
|
|
|
rtc_write(RTC_AL_YEA, 1970 - RTC_MIN_YEAR);
|
|
|
|
rtc_write(RTC_AL_MTH, 1);
|
|
|
|
rtc_write(RTC_AL_DOM, 1);
|
|
|
|
rtc_write(RTC_AL_DOW, 4);
|
|
|
|
rtc_write(RTC_AL_HOU, 0);
|
|
|
|
rtc_write(RTC_AL_MIN, 0);
|
|
|
|
rtc_write(RTC_AL_SEC, 0);
|
|
|
|
|
|
|
|
rtc_write(RTC_DIFF, 0);
|
|
|
|
rtc_write(RTC_CALI, 0);
|
2018-10-26 09:18:09 +02:00
|
|
|
if (!rtc_write_trigger())
|
|
|
|
return 0;
|
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_IRQ_STA, &irqsta); /* read clear */
|
2018-10-26 09:18:09 +02:00
|
|
|
|
|
|
|
/* init time counters after resetting RTC_DIFF and RTC_CALI */
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_write(RTC_TC_YEA, RTC_DEFAULT_YEA - RTC_MIN_YEAR);
|
|
|
|
rtc_write(RTC_TC_MTH, RTC_DEFAULT_MTH);
|
|
|
|
rtc_write(RTC_TC_DOM, RTC_DEFAULT_DOM);
|
|
|
|
rtc_write(RTC_TC_DOW, RTC_DEFAULT_DOW);
|
|
|
|
rtc_write(RTC_TC_HOU, 0);
|
|
|
|
rtc_write(RTC_TC_MIN, 0);
|
|
|
|
rtc_write(RTC_TC_SEC, 0);
|
2018-10-26 09:18:09 +02:00
|
|
|
|
|
|
|
return rtc_write_trigger();
|
|
|
|
}
|
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
static u8 rtc_check_state(void)
|
2018-10-26 09:18:09 +02:00
|
|
|
{
|
|
|
|
u16 con;
|
|
|
|
u16 pwrky1;
|
|
|
|
u16 pwrky2;
|
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_CON, &con);
|
|
|
|
rtc_read(RTC_POWERKEY1, &pwrky1);
|
|
|
|
rtc_read(RTC_POWERKEY2, &pwrky2);
|
|
|
|
|
|
|
|
rtc_info("con=%x, pwrkey1=%x, pwrkey2=%x\n", con, pwrky1, pwrky2);
|
2018-10-26 09:18:09 +02:00
|
|
|
|
|
|
|
if (con & RTC_CON_LPSTA_RAW)
|
|
|
|
return RTC_STATE_INIT;
|
|
|
|
|
|
|
|
if (!rtc_busy_wait())
|
|
|
|
return RTC_STATE_RECOVER;
|
|
|
|
|
|
|
|
if (!rtc_writeif_unlock())
|
|
|
|
return RTC_STATE_RECOVER;
|
|
|
|
|
|
|
|
if (pwrky1 != RTC_POWERKEY1_KEY || pwrky2 != RTC_POWERKEY2_KEY)
|
|
|
|
return RTC_STATE_INIT;
|
|
|
|
else
|
|
|
|
return RTC_STATE_REBOOT;
|
|
|
|
}
|
|
|
|
|
|
|
|
void rtc_boot_common(void)
|
|
|
|
{
|
|
|
|
u16 bbpu;
|
|
|
|
u16 con;
|
|
|
|
u16 irqsta;
|
|
|
|
|
|
|
|
switch (rtc_check_state()) {
|
|
|
|
case RTC_STATE_REBOOT:
|
|
|
|
pwrap_write_field(RTC_BBPU, RTC_BBPU_KEY | RTC_BBPU_RELOAD,
|
|
|
|
0xFFFF, 0);
|
|
|
|
rtc_write_trigger();
|
|
|
|
rtc_osc_init();
|
|
|
|
break;
|
|
|
|
case RTC_STATE_RECOVER:
|
|
|
|
rtc_init(1);
|
|
|
|
break;
|
|
|
|
case RTC_STATE_INIT:
|
|
|
|
default:
|
2019-03-19 04:47:21 +01:00
|
|
|
if (rtc_init(0))
|
2018-10-26 09:18:09 +02:00
|
|
|
rtc_init(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_read(RTC_IRQ_STA, &irqsta); /* Read clear */
|
|
|
|
rtc_read(RTC_BBPU, &bbpu);
|
|
|
|
rtc_read(RTC_CON, &con);
|
2018-10-26 09:18:09 +02:00
|
|
|
|
2019-03-19 04:47:21 +01:00
|
|
|
rtc_info("irqsta=%x, bbpu=%x, con=%x\n", irqsta, bbpu, con);
|
2018-10-26 09:18:09 +02:00
|
|
|
}
|