xref: /freebsd/sys/dev/iicbus/pmic/rockchip/rk8xx_rtc.c (revision 8881d206f4e68b564c2c5f50fc717086fc3e827a)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3  *
4  * Copyright (c) 2021 Emmanuel Vadot <manu@FreeBSD.org>
5  * Copyright (c) 2021 Peter Jeremy <peterj@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26  * SUCH DAMAGE.
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/systm.h>
35 #include <sys/clock.h>
36 
37 #include <dev/iicbus/pmic/rockchip/rk8xx.h>
38 
39 int
40 rk8xx_gettime(device_t dev, struct timespec *ts)
41 {
42 	struct rk8xx_softc *sc;
43 	struct bcd_clocktime bct;
44 	uint8_t data[7];
45 	uint8_t ctrl;
46 	int error;
47 
48 	sc = device_get_softc(dev);
49 
50 	/* Latch the RTC value into the shadow registers and set 24hr mode */
51 	error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1);
52 	if (error != 0)
53 		return (error);
54 
55 	ctrl |= sc->rtc_regs.ctrl_readsel_mask;
56 	ctrl &= ~(sc->rtc_regs.ctrl_ampm_mask | sc->rtc_regs.ctrl_gettime_mask);
57 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
58 	if (error != 0)
59 		return (error);
60 	ctrl |= sc->rtc_regs.ctrl_gettime_mask;
61 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
62 	if (error != 0)
63 		return (error);
64 	ctrl &= ~sc->rtc_regs.ctrl_gettime_mask;
65 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
66 	if (error != 0)
67 		return (error);
68 
69 	/* This works as long as sc->rtc_regs.secs = 0 */
70 	error = rk8xx_read(dev, sc->rtc_regs.secs, data, 7);
71 	if (error != 0)
72 		return (error);
73 
74 	/*
75 	 * If the reported year is earlier than 2019, assume the clock is unset.
76 	 * This is both later than the reset value for the RK805 and RK808 as
77 	 * well as being prior to the current time.
78 	 */
79 	if (data[sc->rtc_regs.years] < 0x19)
80 		return (EINVAL);
81 
82 	memset(&bct, 0, sizeof(bct));
83 	bct.year = data[sc->rtc_regs.years];
84 	bct.mon = data[sc->rtc_regs.months] & sc->rtc_regs.months_mask;
85 	bct.day = data[sc->rtc_regs.days] & sc->rtc_regs.days_mask;
86 	bct.hour = data[sc->rtc_regs.hours] & sc->rtc_regs.hours_mask;
87 	bct.min = data[sc->rtc_regs.minutes] & sc->rtc_regs.minutes_mask;
88 	bct.sec = data[sc->rtc_regs.secs] & sc->rtc_regs.secs_mask;
89 	bct.dow = data[sc->rtc_regs.weeks] & sc->rtc_regs.weeks_mask;
90 	/* The day of week is reported as 1-7 with 1 = Monday */
91 	if (bct.dow == 7)
92 		bct.dow = 0;
93 	bct.ispm = 0;
94 
95 	if (bootverbose)
96 		device_printf(dev, "Read RTC: %02x-%02x-%02x %02x:%02x:%02x\n",
97 		    bct.year, bct.mon, bct.day, bct.hour, bct.min, bct.sec);
98 
99 	return (clock_bcd_to_ts(&bct, ts, false));
100 }
101 
102 int
103 rk8xx_settime(device_t dev, struct timespec *ts)
104 {
105 	struct rk8xx_softc *sc;
106 	struct bcd_clocktime bct;
107 	uint8_t data[7];
108 	int error;
109 	uint8_t ctrl;
110 
111 	sc = device_get_softc(dev);
112 
113 	clock_ts_to_bcd(ts, &bct, false);
114 
115 	/* This works as long as RK805_RTC_SECS = 0 */
116 	data[sc->rtc_regs.years] = bct.year;
117 	data[sc->rtc_regs.months] = bct.mon;
118 	data[sc->rtc_regs.days] = bct.day;
119 	data[sc->rtc_regs.hours] = bct.hour;
120 	data[sc->rtc_regs.minutes] = bct.min;
121 	data[sc->rtc_regs.secs] = bct.sec;
122 	data[sc->rtc_regs.weeks] = bct.dow;
123 	/* The day of week is reported as 1-7 with 1 = Monday */
124 	if (data[sc->rtc_regs.weeks] == 0)
125 		data[sc->rtc_regs.weeks] = 7;
126 
127 	error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1);
128 	if (error != 0)
129 		return (error);
130 
131 	ctrl |= sc->rtc_regs.ctrl_stop_mask;
132 	ctrl &= ~sc->rtc_regs.ctrl_ampm_mask;
133 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
134 	if (error != 0)
135 		return (error);
136 
137 	error = rk8xx_write(dev, sc->rtc_regs.secs, data, 7);
138 	ctrl &= ~sc->rtc_regs.ctrl_stop_mask;
139 	rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
140 
141 	return (error);
142 }
143