1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <commonlib/bsd/bcd.h>
4 #include <console/console.h>
5 #include <device/i2c_simple.h>
9 enum TPS65913_RTC_REG
{
10 TPS65913_SECONDS_REG
= 0x00,
11 TPS65913_MINUTES_REG
= 0x01,
12 TPS65913_HOURS_REG
= 0x02,
13 TPS65913_DAYS_REG
= 0x03,
14 TPS65913_MONTHS_REG
= 0x04,
15 TPS65913_YEARS_REG
= 0x05,
16 TPS65913_WEEKS_REG
= 0x06,
17 TPS65913_RTC_CTRL_REG
= 0x10,
18 TPS65913_RTC_STATUS_REG
= 0x11,
19 TPS65913_RTC_INTERRUPTS_REG
= 0x12,
23 TPS65913_RTC_CTRL_STOP
= (1 << 0),
24 TPS65913_RTC_CTRL_GET_TIME
= (1 << 6),
26 TPS65913_RTC_STATUS_RUN
= (1 << 1),
27 TPS65913_RTC_RUNNING
= (1 << 1),
28 TPS65913_RTC_FROZEN
= (0 << 1),
31 static inline uint8_t tps65913_read(enum TPS65913_RTC_REG reg
)
34 i2c_readb(CONFIG_DRIVERS_TI_TPS65913_RTC_BUS
,
35 CONFIG_DRIVERS_TI_TPS65913_RTC_ADDR
, reg
, &val
);
39 static inline void tps65913_write(enum TPS65913_RTC_REG reg
, uint8_t val
)
41 i2c_writeb(CONFIG_DRIVERS_TI_TPS65913_RTC_BUS
,
42 CONFIG_DRIVERS_TI_TPS65913_RTC_ADDR
, reg
, val
);
45 static void tps65913_rtc_ctrl_clear(uint8_t bit
)
47 uint8_t control
= tps65913_read(TPS65913_RTC_CTRL_REG
);
50 tps65913_write(TPS65913_RTC_CTRL_REG
, control
);
53 static void tps65913_rtc_ctrl_set(uint8_t bit
)
55 uint8_t control
= tps65913_read(TPS65913_RTC_CTRL_REG
);
57 control
|= TPS65913_RTC_CTRL_GET_TIME
;
58 tps65913_write(TPS65913_RTC_CTRL_REG
, control
);
61 static int tps65913_is_rtc_running(void)
63 uint8_t status
= tps65913_read(TPS65913_RTC_STATUS_REG
);
64 return ((status
& TPS65913_RTC_STATUS_RUN
) == TPS65913_RTC_RUNNING
);
68 * This function ensures that current time is copied to shadow registers. Then a
69 * normal read on TC registers reads from the shadow instead of current TC
70 * registers. This helps prevent the accidental change in counters while
71 * reading. In order to ensure that the current TC registers are copied into
72 * shadow registers, GET_TIME bit needs to be set to 0 and then to 1.
74 static void tps65913_rtc_shadow(void)
76 tps65913_rtc_ctrl_clear(TPS65913_RTC_CTRL_GET_TIME
);
77 tps65913_rtc_ctrl_set(TPS65913_RTC_CTRL_GET_TIME
);
80 static int tps65913_rtc_stop(void)
82 /* Clearing stop bit freezes RTC */
83 tps65913_rtc_ctrl_clear(TPS65913_RTC_CTRL_STOP
);
85 if (tps65913_is_rtc_running()) {
86 printk(BIOS_ERR
, "Could not stop RTC\n");
93 static int tps65913_rtc_start(void)
95 /* Setting stop bit starts RTC */
96 tps65913_rtc_ctrl_set(TPS65913_RTC_CTRL_STOP
);
98 if (!tps65913_is_rtc_running()) {
99 printk(BIOS_ERR
, "Could not start RTC\n");
106 int rtc_set(const struct rtc_time
*time
)
108 /* Before setting the time, ensure that rtc is stopped */
109 if (tps65913_rtc_stop())
112 tps65913_write(TPS65913_SECONDS_REG
, bin2bcd(time
->sec
));
113 tps65913_write(TPS65913_MINUTES_REG
, bin2bcd(time
->min
));
114 tps65913_write(TPS65913_HOURS_REG
, bin2bcd(time
->hour
));
115 tps65913_write(TPS65913_DAYS_REG
, bin2bcd(time
->mday
));
116 tps65913_write(TPS65913_MONTHS_REG
, bin2bcd(time
->mon
));
117 tps65913_write(TPS65913_YEARS_REG
, bin2bcd(time
->year
));
120 if (tps65913_rtc_start())
126 int rtc_get(struct rtc_time
*time
)
128 tps65913_rtc_shadow();
130 time
->sec
= bcd2bin(tps65913_read(TPS65913_SECONDS_REG
) & 0x7f);
131 time
->min
= bcd2bin(tps65913_read(TPS65913_MINUTES_REG
) & 0x7f);
132 time
->hour
= bcd2bin(tps65913_read(TPS65913_HOURS_REG
) & 0x3f);
133 time
->mday
= bcd2bin(tps65913_read(TPS65913_DAYS_REG
) & 0x3f);
134 time
->mon
= bcd2bin(tps65913_read(TPS65913_MONTHS_REG
) & 0x1f);
135 time
->year
= bcd2bin(tps65913_read(TPS65913_YEARS_REG
) & 0xff);