xref: /freebsd/sys/dev/iicbus/pmic/rockchip/rk8xx_rtc.c (revision fdafd315)
1328077bbSEmmanuel Vadot /*-
24d846d26SWarner Losh  * SPDX-License-Identifier: BSD-2-Clause
3328077bbSEmmanuel Vadot  *
4328077bbSEmmanuel Vadot  * Copyright (c) 2021 Emmanuel Vadot <manu@FreeBSD.org>
5328077bbSEmmanuel Vadot  * Copyright (c) 2021 Peter Jeremy <peterj@FreeBSD.org>
6328077bbSEmmanuel Vadot  *
7328077bbSEmmanuel Vadot  * Redistribution and use in source and binary forms, with or without
8328077bbSEmmanuel Vadot  * modification, are permitted provided that the following conditions
9328077bbSEmmanuel Vadot  * are met:
10328077bbSEmmanuel Vadot  * 1. Redistributions of source code must retain the above copyright
11328077bbSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer.
12328077bbSEmmanuel Vadot  * 2. Redistributions in binary form must reproduce the above copyright
13328077bbSEmmanuel Vadot  *    notice, this list of conditions and the following disclaimer in the
14328077bbSEmmanuel Vadot  *    documentation and/or other materials provided with the distribution.
15328077bbSEmmanuel Vadot  *
16328077bbSEmmanuel Vadot  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17328077bbSEmmanuel Vadot  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18328077bbSEmmanuel Vadot  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19328077bbSEmmanuel Vadot  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20328077bbSEmmanuel Vadot  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21328077bbSEmmanuel Vadot  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22328077bbSEmmanuel Vadot  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23328077bbSEmmanuel Vadot  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24328077bbSEmmanuel Vadot  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25328077bbSEmmanuel Vadot  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26328077bbSEmmanuel Vadot  * SUCH DAMAGE.
27328077bbSEmmanuel Vadot  */
28328077bbSEmmanuel Vadot 
29328077bbSEmmanuel Vadot #include <sys/param.h>
30328077bbSEmmanuel Vadot #include <sys/bus.h>
31328077bbSEmmanuel Vadot #include <sys/systm.h>
32328077bbSEmmanuel Vadot #include <sys/clock.h>
33328077bbSEmmanuel Vadot 
34328077bbSEmmanuel Vadot #include <dev/iicbus/pmic/rockchip/rk8xx.h>
35328077bbSEmmanuel Vadot 
36328077bbSEmmanuel Vadot int
rk8xx_gettime(device_t dev,struct timespec * ts)37328077bbSEmmanuel Vadot rk8xx_gettime(device_t dev, struct timespec *ts)
38328077bbSEmmanuel Vadot {
39328077bbSEmmanuel Vadot 	struct rk8xx_softc *sc;
40328077bbSEmmanuel Vadot 	struct bcd_clocktime bct;
41328077bbSEmmanuel Vadot 	uint8_t data[7];
42328077bbSEmmanuel Vadot 	uint8_t ctrl;
43328077bbSEmmanuel Vadot 	int error;
44328077bbSEmmanuel Vadot 
45328077bbSEmmanuel Vadot 	sc = device_get_softc(dev);
46328077bbSEmmanuel Vadot 
47328077bbSEmmanuel Vadot 	/* Latch the RTC value into the shadow registers and set 24hr mode */
48328077bbSEmmanuel Vadot 	error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1);
49328077bbSEmmanuel Vadot 	if (error != 0)
50328077bbSEmmanuel Vadot 		return (error);
51328077bbSEmmanuel Vadot 
52328077bbSEmmanuel Vadot 	ctrl |= sc->rtc_regs.ctrl_readsel_mask;
53328077bbSEmmanuel Vadot 	ctrl &= ~(sc->rtc_regs.ctrl_ampm_mask | sc->rtc_regs.ctrl_gettime_mask);
54328077bbSEmmanuel Vadot 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
55328077bbSEmmanuel Vadot 	if (error != 0)
56328077bbSEmmanuel Vadot 		return (error);
57328077bbSEmmanuel Vadot 	ctrl |= sc->rtc_regs.ctrl_gettime_mask;
58328077bbSEmmanuel Vadot 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
59328077bbSEmmanuel Vadot 	if (error != 0)
60328077bbSEmmanuel Vadot 		return (error);
6166a3e513SSøren Schmidt 	if (sc->type == RK809 || sc->type == RK817) {
6266a3e513SSøren Schmidt 		/* wait one 32khz cycle for clock shadow registers to latch */
6366a3e513SSøren Schmidt 		DELAY(1000000 / 32000);
6466a3e513SSøren Schmidt 	}
65328077bbSEmmanuel Vadot 	ctrl &= ~sc->rtc_regs.ctrl_gettime_mask;
66328077bbSEmmanuel Vadot 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
67328077bbSEmmanuel Vadot 	if (error != 0)
68328077bbSEmmanuel Vadot 		return (error);
69328077bbSEmmanuel Vadot 
70328077bbSEmmanuel Vadot 	/* This works as long as sc->rtc_regs.secs = 0 */
71328077bbSEmmanuel Vadot 	error = rk8xx_read(dev, sc->rtc_regs.secs, data, 7);
72328077bbSEmmanuel Vadot 	if (error != 0)
73328077bbSEmmanuel Vadot 		return (error);
74328077bbSEmmanuel Vadot 
75328077bbSEmmanuel Vadot 	/*
76328077bbSEmmanuel Vadot 	 * If the reported year is earlier than 2019, assume the clock is unset.
77328077bbSEmmanuel Vadot 	 * This is both later than the reset value for the RK805 and RK808 as
78328077bbSEmmanuel Vadot 	 * well as being prior to the current time.
79328077bbSEmmanuel Vadot 	 */
80328077bbSEmmanuel Vadot 	if (data[sc->rtc_regs.years] < 0x19)
81328077bbSEmmanuel Vadot 		return (EINVAL);
82328077bbSEmmanuel Vadot 
83328077bbSEmmanuel Vadot 	memset(&bct, 0, sizeof(bct));
84328077bbSEmmanuel Vadot 	bct.year = data[sc->rtc_regs.years];
85328077bbSEmmanuel Vadot 	bct.mon = data[sc->rtc_regs.months] & sc->rtc_regs.months_mask;
86328077bbSEmmanuel Vadot 	bct.day = data[sc->rtc_regs.days] & sc->rtc_regs.days_mask;
87328077bbSEmmanuel Vadot 	bct.hour = data[sc->rtc_regs.hours] & sc->rtc_regs.hours_mask;
88328077bbSEmmanuel Vadot 	bct.min = data[sc->rtc_regs.minutes] & sc->rtc_regs.minutes_mask;
89328077bbSEmmanuel Vadot 	bct.sec = data[sc->rtc_regs.secs] & sc->rtc_regs.secs_mask;
90328077bbSEmmanuel Vadot 	bct.dow = data[sc->rtc_regs.weeks] & sc->rtc_regs.weeks_mask;
91328077bbSEmmanuel Vadot 	/* The day of week is reported as 1-7 with 1 = Monday */
92328077bbSEmmanuel Vadot 	if (bct.dow == 7)
93328077bbSEmmanuel Vadot 		bct.dow = 0;
94328077bbSEmmanuel Vadot 	bct.ispm = 0;
9566a3e513SSøren Schmidt 	if (sc->type == RK809 || sc->type == RK817)
9666a3e513SSøren Schmidt 		bct.year += 0x2000;	/* valid for 2000-2099 only */
97328077bbSEmmanuel Vadot 
98328077bbSEmmanuel Vadot 	if (bootverbose)
99328077bbSEmmanuel Vadot 		device_printf(dev, "Read RTC: %02x-%02x-%02x %02x:%02x:%02x\n",
100328077bbSEmmanuel Vadot 		    bct.year, bct.mon, bct.day, bct.hour, bct.min, bct.sec);
101328077bbSEmmanuel Vadot 
102328077bbSEmmanuel Vadot 	return (clock_bcd_to_ts(&bct, ts, false));
103328077bbSEmmanuel Vadot }
104328077bbSEmmanuel Vadot 
105328077bbSEmmanuel Vadot int
rk8xx_settime(device_t dev,struct timespec * ts)106328077bbSEmmanuel Vadot rk8xx_settime(device_t dev, struct timespec *ts)
107328077bbSEmmanuel Vadot {
108328077bbSEmmanuel Vadot 	struct rk8xx_softc *sc;
109328077bbSEmmanuel Vadot 	struct bcd_clocktime bct;
110328077bbSEmmanuel Vadot 	uint8_t data[7];
111328077bbSEmmanuel Vadot 	int error;
112328077bbSEmmanuel Vadot 	uint8_t ctrl;
113328077bbSEmmanuel Vadot 
114328077bbSEmmanuel Vadot 	sc = device_get_softc(dev);
115328077bbSEmmanuel Vadot 
116328077bbSEmmanuel Vadot 	clock_ts_to_bcd(ts, &bct, false);
117328077bbSEmmanuel Vadot 
118328077bbSEmmanuel Vadot 	/* This works as long as RK805_RTC_SECS = 0 */
11966a3e513SSøren Schmidt 	if (sc->type == RK809 || sc->type == RK817) {
12066a3e513SSøren Schmidt 		/* valid for 2000-2099 only */
12166a3e513SSøren Schmidt 		if ((bct.year & 0xff00) != 0x2000) {
12266a3e513SSøren Schmidt 			device_printf(dev, "year out of range\n");
12366a3e513SSøren Schmidt 			return (EINVAL);
12466a3e513SSøren Schmidt 		}
12566a3e513SSøren Schmidt 		bct.year &= 0x00ff;
12666a3e513SSøren Schmidt 	}
127328077bbSEmmanuel Vadot 	data[sc->rtc_regs.years] = bct.year;
128328077bbSEmmanuel Vadot 	data[sc->rtc_regs.months] = bct.mon;
129328077bbSEmmanuel Vadot 	data[sc->rtc_regs.days] = bct.day;
130328077bbSEmmanuel Vadot 	data[sc->rtc_regs.hours] = bct.hour;
131328077bbSEmmanuel Vadot 	data[sc->rtc_regs.minutes] = bct.min;
132328077bbSEmmanuel Vadot 	data[sc->rtc_regs.secs] = bct.sec;
133328077bbSEmmanuel Vadot 	data[sc->rtc_regs.weeks] = bct.dow;
134328077bbSEmmanuel Vadot 	/* The day of week is reported as 1-7 with 1 = Monday */
135328077bbSEmmanuel Vadot 	if (data[sc->rtc_regs.weeks] == 0)
136328077bbSEmmanuel Vadot 		data[sc->rtc_regs.weeks] = 7;
137328077bbSEmmanuel Vadot 
138328077bbSEmmanuel Vadot 	error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1);
139328077bbSEmmanuel Vadot 	if (error != 0)
140328077bbSEmmanuel Vadot 		return (error);
141328077bbSEmmanuel Vadot 
142328077bbSEmmanuel Vadot 	ctrl |= sc->rtc_regs.ctrl_stop_mask;
143328077bbSEmmanuel Vadot 	ctrl &= ~sc->rtc_regs.ctrl_ampm_mask;
144328077bbSEmmanuel Vadot 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
145328077bbSEmmanuel Vadot 	if (error != 0)
146328077bbSEmmanuel Vadot 		return (error);
147328077bbSEmmanuel Vadot 
148328077bbSEmmanuel Vadot 	error = rk8xx_write(dev, sc->rtc_regs.secs, data, 7);
149328077bbSEmmanuel Vadot 	ctrl &= ~sc->rtc_regs.ctrl_stop_mask;
150328077bbSEmmanuel Vadot 	rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
151328077bbSEmmanuel Vadot 
152328077bbSEmmanuel Vadot 	return (error);
153328077bbSEmmanuel Vadot }
154