xref: /aosp_15_r20/external/coreboot/src/soc/rockchip/common/rk808.c (revision b9411a12aaaa7e1e6a6fb7c5e057f44ee179a49c)
1 /* SPDX-License-Identifier: GPL-2.0-only */
2 
3 #include <assert.h>
4 #include <commonlib/bsd/bcd.h>
5 #include <console/console.h>
6 #include <delay.h>
7 #include <device/i2c_simple.h>
8 #include <rtc.h>
9 #include <soc/rk808.h>
10 #include <stdint.h>
11 
12 #if CONFIG_PMIC_BUS < 0
13 #error "PMIC_BUS must be set in mainboard's Kconfig."
14 #endif
15 
16 #define RK808_ADDR		0x1b
17 
18 #define DCDC_EN			0x23
19 #define LDO_EN			0x24
20 #define BUCK1SEL		0x2f
21 #define BUCK4SEL		0x38
22 #define LDO_ONSEL(i)		(0x39 + 2 * i)
23 #define LDO_SLPSEL(i)		(0x3a + 2 * i)
24 
25 #define RTC_SECOND		0x00
26 #define RTC_MINUTE		0x01
27 #define RTC_HOUR		0x02
28 #define RTC_DAY			0x03
29 #define RTC_MONTH		0x04
30 #define RTC_YEAR		0x05
31 #define RTC_WEEKS		0x06
32 #define RTC_CTRL		0x10
33 #define RTC_STATUS		0x11
34 
35 #define RTC_CTRL_STOP_RTC	(1 << 0)
36 #define RTC_CTRL_GET_TIME	(1 << 6)
37 #define RTC_CTRL_RTC_READSEL	(1 << 7)
38 
39 #define DCDC_UV_ACT		0x28
40 #define DCDC_ILMAX		0x90
41 
rk808_read(uint8_t reg,uint8_t * value)42 static int rk808_read(uint8_t reg, uint8_t *value)
43 {
44 	return i2c_readb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value);
45 }
46 
rk808_write(uint8_t reg,uint8_t value)47 static int rk808_write(uint8_t reg, uint8_t value)
48 {
49 	return i2c_writeb(CONFIG_PMIC_BUS, RK808_ADDR, reg, value);
50 }
51 
rk808_clrsetbits(uint8_t reg,uint8_t clr,uint8_t set)52 static void rk808_clrsetbits(uint8_t reg, uint8_t clr, uint8_t set)
53 {
54 	uint8_t value;
55 
56 	if (rk808_read(reg, &value) || rk808_write(reg, (value & ~clr) | set))
57 		printk(BIOS_ERR, "Cannot set Rk808[%#x]!\n", reg);
58 }
59 
rk808_configure_switch(int sw,int enabled)60 void rk808_configure_switch(int sw, int enabled)
61 {
62 	assert(sw == 1 || sw == 2);
63 	rk808_clrsetbits(DCDC_EN, 1 << (sw + 4), !!enabled << (sw + 4));
64 }
65 
rk808_configure_ldo(int ldo,int millivolts)66 void rk808_configure_ldo(int ldo, int millivolts)
67 {
68 	uint8_t vsel;
69 
70 	if (!millivolts) {
71 		rk808_clrsetbits(LDO_EN, 1 << (ldo - 1), 0);
72 		return;
73 	}
74 
75 	switch (ldo) {
76 	case 1:
77 	case 2:
78 	case 4:
79 	case 5:
80 	case 8:
81 		vsel = DIV_ROUND_UP(millivolts, 100) - 18;
82 		assert(vsel <= 0x10);
83 		break;
84 	case 3:
85 	case 6:
86 	case 7:
87 		vsel = DIV_ROUND_UP(millivolts, 100) - 8;
88 		assert(vsel <= 0x11);
89 		break;
90 	default:
91 		die("Unknown LDO index!");
92 	}
93 
94 	rk808_clrsetbits(LDO_ONSEL(ldo), 0x1f, vsel);
95 	rk808_clrsetbits(LDO_EN, 0, 1 << (ldo - 1));
96 }
97 
rk808_configure_buck(int buck,int millivolts)98 void rk808_configure_buck(int buck, int millivolts)
99 {
100 	uint8_t vsel;
101 	uint8_t buck_reg;
102 
103 	switch (buck) {
104 	case 1:
105 	case 2:
106 		/* 25mV steps. base = 29 * 25mV = 725 */
107 		vsel = (DIV_ROUND_UP(millivolts, 25) - 29) * 2 + 1;
108 		assert(vsel <= 0x3f);
109 		buck_reg = BUCK1SEL + 4 * (buck - 1);
110 		break;
111 	case 4:
112 		vsel = DIV_ROUND_UP(millivolts, 100) - 18;
113 		assert(vsel <= 0xf);
114 		buck_reg = BUCK4SEL;
115 		break;
116 	default:
117 		die("Unknown buck index!");
118 	}
119 	rk808_clrsetbits(DCDC_ILMAX, 0, 3 << ((buck - 1) * 2));
120 
121 	/* undervoltage detection may be wrong, disable it */
122 	rk808_clrsetbits(DCDC_UV_ACT, 1 << (buck - 1), 0);
123 
124 	rk808_clrsetbits(buck_reg, 0x3f, vsel);
125 	rk808_clrsetbits(DCDC_EN, 0, 1 << (buck - 1));
126 }
127 
rk808rtc_stop(void)128 static void rk808rtc_stop(void)
129 {
130 	rk808_clrsetbits(RTC_CTRL, RTC_CTRL_STOP_RTC, 0);
131 }
132 
rk808rtc_start(void)133 static void rk808rtc_start(void)
134 {
135 	rk808_clrsetbits(RTC_CTRL, 0, RTC_CTRL_STOP_RTC);
136 }
137 
rtc_set(const struct rtc_time * time)138 int rtc_set(const struct rtc_time *time)
139 {
140 	int ret = 0;
141 
142 	/* RTC time can only be set when RTC is frozen */
143 	rk808rtc_stop();
144 
145 	ret |= rk808_write(RTC_SECOND, bin2bcd(time->sec));
146 	ret |= rk808_write(RTC_MINUTE, bin2bcd(time->min));
147 	ret |= rk808_write(RTC_HOUR, bin2bcd(time->hour));
148 	ret |= rk808_write(RTC_DAY, bin2bcd(time->mday));
149 	ret |= rk808_write(RTC_MONTH, bin2bcd(time->mon));
150 	ret |= rk808_write(RTC_YEAR, bin2bcd(time->year));
151 
152 	rk808rtc_start();
153 	return ret;
154 }
155 
rtc_get(struct rtc_time * time)156 int rtc_get(struct rtc_time *time)
157 {
158 	uint8_t value;
159 	int ret = 0;
160 
161 	/*
162 	 * Set RTC_READSEL to cause reads to access shadow registers and
163 	 * transition GET_TIME from 0 to 1 to cause dynamic register content
164 	 * to be copied into shadow registers. This ensures a coherent reading
165 	 * of time values as we access each register using slow I2C transfers.
166 	 */
167 	rk808_clrsetbits(RTC_CTRL, RTC_CTRL_GET_TIME, 0);
168 	rk808_clrsetbits(RTC_CTRL, 0, RTC_CTRL_GET_TIME | RTC_CTRL_RTC_READSEL);
169 
170 	/*
171 	 * After we set the GET_TIME bit, the rtc time can't be read
172 	 * immediately. So we should wait up to 31.25 us.
173 	 */
174 	udelay(32);
175 
176 	ret |= rk808_read(RTC_SECOND, &value);
177 	time->sec = bcd2bin(value & 0x7f);
178 
179 	ret |= rk808_read(RTC_MINUTE, &value);
180 	time->min = bcd2bin(value & 0x7f);
181 
182 	ret |= rk808_read(RTC_HOUR, &value);
183 	time->hour = bcd2bin(value & 0x3f);
184 
185 	ret |= rk808_read(RTC_DAY, &value);
186 	time->mday = bcd2bin(value & 0x3f);
187 
188 	ret |= rk808_read(RTC_MONTH, &value);
189 	time->mon = bcd2bin(value & 0x1f);
190 
191 	ret |= rk808_read(RTC_YEAR, &value);
192 	time->year = bcd2bin(value);
193 
194 	time->wday = -1; /* unknown */
195 
196 	return ret;
197 }
198