coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
tps65913rtc.c
Go to the documentation of this file.
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 
20 };
21 
22 enum {
25 
28  TPS65913_RTC_FROZEN = (0 << 1),
29 };
30 
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 
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 
46 {
48 
49  control &= ~bit;
51 }
52 
54 {
56 
57  control |= TPS65913_RTC_CTRL_GET_TIME;
59 }
60 
61 static int tps65913_is_rtc_running(void)
62 {
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  */
74 static void tps65913_rtc_shadow(void)
75 {
78 }
79 
80 static int tps65913_rtc_stop(void)
81 {
82  /* Clearing stop bit freezes RTC */
84 
86  printk(BIOS_ERR, "Could not stop RTC\n");
87  return 1;
88  }
89 
90  return 0;
91 }
92 
93 static int tps65913_rtc_start(void)
94 {
95  /* Setting stop bit starts RTC */
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 
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 
118 
119  /* Re-start rtc */
120  if (tps65913_rtc_start())
121  return 1;
122 
123  return 0;
124 }
125 
126 int rtc_get(struct rtc_time *time)
127 {
129 
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 }
static uint8_t bin2bcd(uint8_t val)
Definition: bcd.h:13
static uint8_t bcd2bin(uint8_t val)
Definition: bcd.h:8
#define printk(level,...)
Definition: stdlib.h:16
static int i2c_writeb(unsigned int bus, uint8_t slave, uint8_t reg, uint8_t data)
Write a byte with one segment in one frame.
Definition: i2c_simple.h:131
static int i2c_readb(unsigned int bus, uint8_t slave, uint8_t reg, uint8_t *data)
Read a byte with two segments in one frame.
Definition: i2c_simple.h:109
#define BIOS_ERR
BIOS_ERR - System in incomplete state.
Definition: loglevel.h:72
unsigned char uint8_t
Definition: stdint.h:8
Definition: rtc.h:6
int year
Definition: rtc.h:12
int hour
Definition: rtc.h:9
int mon
Definition: rtc.h:11
int min
Definition: rtc.h:8
int sec
Definition: rtc.h:7
int mday
Definition: rtc.h:10
u8 val
Definition: sys.c:300
static void tps65913_rtc_ctrl_clear(uint8_t bit)
Definition: tps65913rtc.c:45
static int tps65913_rtc_start(void)
Definition: tps65913rtc.c:93
static void tps65913_rtc_shadow(void)
Definition: tps65913rtc.c:74
static uint8_t tps65913_read(enum TPS65913_RTC_REG reg)
Definition: tps65913rtc.c:31
static void tps65913_write(enum TPS65913_RTC_REG reg, uint8_t val)
Definition: tps65913rtc.c:39
int rtc_set(const struct rtc_time *time)
Definition: tps65913rtc.c:106
TPS65913_RTC_REG
Definition: tps65913rtc.c:9
@ TPS65913_DAYS_REG
Definition: tps65913rtc.c:13
@ TPS65913_WEEKS_REG
Definition: tps65913rtc.c:16
@ TPS65913_MINUTES_REG
Definition: tps65913rtc.c:11
@ TPS65913_RTC_STATUS_REG
Definition: tps65913rtc.c:18
@ TPS65913_YEARS_REG
Definition: tps65913rtc.c:15
@ TPS65913_RTC_INTERRUPTS_REG
Definition: tps65913rtc.c:19
@ TPS65913_RTC_CTRL_REG
Definition: tps65913rtc.c:17
@ TPS65913_MONTHS_REG
Definition: tps65913rtc.c:14
@ TPS65913_SECONDS_REG
Definition: tps65913rtc.c:10
@ TPS65913_HOURS_REG
Definition: tps65913rtc.c:12
int rtc_get(struct rtc_time *time)
Definition: tps65913rtc.c:126
static int tps65913_rtc_stop(void)
Definition: tps65913rtc.c:80
@ TPS65913_RTC_RUNNING
Definition: tps65913rtc.c:27
@ TPS65913_RTC_CTRL_STOP
Definition: tps65913rtc.c:23
@ TPS65913_RTC_CTRL_GET_TIME
Definition: tps65913rtc.c:24
@ TPS65913_RTC_STATUS_RUN
Definition: tps65913rtc.c:26
@ TPS65913_RTC_FROZEN
Definition: tps65913rtc.c:28
static int tps65913_is_rtc_running(void)
Definition: tps65913rtc.c:61
static void tps65913_rtc_ctrl_set(uint8_t bit)
Definition: tps65913rtc.c:53