xref: /aosp_15_r20/external/coreboot/src/drivers/ti/tps65913/tps65913rtc.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <commonlib/bsd/bcd.h>
4 #include <console/console.h>
5 #include <device/i2c_simple.h>
6 #include <rtc.h>
7 #include <stdint.h>
8 
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,
20 };
21 
22 enum {
23 	TPS65913_RTC_CTRL_STOP		= (1 << 0),
24 	TPS65913_RTC_CTRL_GET_TIME	= (1 << 6),
25 
26 	TPS65913_RTC_STATUS_RUN	= (1 << 1),
27 	TPS65913_RTC_RUNNING		= (1 << 1),
28 	TPS65913_RTC_FROZEN		= (0 << 1),
29 };
30 
tps65913_read(enum TPS65913_RTC_REG reg)31 static inline uint8_t tps65913_read(enum TPS65913_RTC_REG reg)
32 {
33 	uint8_t val;
34 	i2c_readb(CONFIG_DRIVERS_TI_TPS65913_RTC_BUS,
35 		  CONFIG_DRIVERS_TI_TPS65913_RTC_ADDR, reg, &val);
36 	return val;
37 }
38 
tps65913_write(enum TPS65913_RTC_REG reg,uint8_t val)39 static inline void tps65913_write(enum TPS65913_RTC_REG reg, uint8_t val)
40 {
41 	i2c_writeb(CONFIG_DRIVERS_TI_TPS65913_RTC_BUS,
42 		   CONFIG_DRIVERS_TI_TPS65913_RTC_ADDR, reg, val);
43 }
44 
tps65913_rtc_ctrl_clear(uint8_t bit)45 static void tps65913_rtc_ctrl_clear(uint8_t bit)
46 {
47 	uint8_t control = tps65913_read(TPS65913_RTC_CTRL_REG);
48 
49 	control &= ~bit;
50 	tps65913_write(TPS65913_RTC_CTRL_REG, control);
51 }
52 
tps65913_rtc_ctrl_set(uint8_t bit)53 static void tps65913_rtc_ctrl_set(uint8_t bit)
54 {
55 	uint8_t control = tps65913_read(TPS65913_RTC_CTRL_REG);
56 
57 	control |= TPS65913_RTC_CTRL_GET_TIME;
58 	tps65913_write(TPS65913_RTC_CTRL_REG, control);
59 }
60 
tps65913_is_rtc_running(void)61 static int tps65913_is_rtc_running(void)
62 {
63 	uint8_t status = tps65913_read(TPS65913_RTC_STATUS_REG);
64 	return ((status & TPS65913_RTC_STATUS_RUN) == TPS65913_RTC_RUNNING);
65 }
66 
67 /*
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.
73  */
tps65913_rtc_shadow(void)74 static void tps65913_rtc_shadow(void)
75 {
76 	tps65913_rtc_ctrl_clear(TPS65913_RTC_CTRL_GET_TIME);
77 	tps65913_rtc_ctrl_set(TPS65913_RTC_CTRL_GET_TIME);
78 }
79 
tps65913_rtc_stop(void)80 static int tps65913_rtc_stop(void)
81 {
82 	/* Clearing stop bit freezes RTC */
83 	tps65913_rtc_ctrl_clear(TPS65913_RTC_CTRL_STOP);
84 
85 	if (tps65913_is_rtc_running()) {
86 		printk(BIOS_ERR, "Could not stop RTC\n");
87 		return 1;
88 	}
89 
90 	return 0;
91 }
92 
tps65913_rtc_start(void)93 static int tps65913_rtc_start(void)
94 {
95 	/* Setting stop bit starts RTC */
96 	tps65913_rtc_ctrl_set(TPS65913_RTC_CTRL_STOP);
97 
98 	if (!tps65913_is_rtc_running()) {
99 		printk(BIOS_ERR, "Could not start RTC\n");
100 		return 1;
101 	}
102 
103 	return 0;
104 }
105 
rtc_set(const struct rtc_time * time)106 int rtc_set(const struct rtc_time *time)
107 {
108 	/* Before setting the time, ensure that rtc is stopped */
109 	if (tps65913_rtc_stop())
110 		return 1;
111 
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));
118 
119 	/* Re-start rtc */
120 	if (tps65913_rtc_start())
121 		return 1;
122 
123 	return 0;
124 }
125 
rtc_get(struct rtc_time * time)126 int rtc_get(struct rtc_time *time)
127 {
128 	tps65913_rtc_shadow();
129 
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);
136 
137 	return 0;
138 }
139