xref: /freebsd/sys/dev/iicbus/pmic/rockchip/rk8xx_rtc.c (revision db33c6f3ae9d1231087710068ee4ea5398aacca7)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
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/param.h>
30 #include <sys/bus.h>
31 #include <sys/systm.h>
32 #include <sys/clock.h>
33 
34 #include <dev/iicbus/pmic/rockchip/rk8xx.h>
35 
36 int
37 rk8xx_gettime(device_t dev, struct timespec *ts)
38 {
39 	struct rk8xx_softc *sc;
40 	struct bcd_clocktime bct;
41 	uint8_t data[7];
42 	uint8_t ctrl;
43 	int error;
44 
45 	sc = device_get_softc(dev);
46 
47 	/* Latch the RTC value into the shadow registers and set 24hr mode */
48 	error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1);
49 	if (error != 0)
50 		return (error);
51 
52 	ctrl |= sc->rtc_regs.ctrl_readsel_mask;
53 	ctrl &= ~(sc->rtc_regs.ctrl_ampm_mask | sc->rtc_regs.ctrl_gettime_mask);
54 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
55 	if (error != 0)
56 		return (error);
57 	ctrl |= sc->rtc_regs.ctrl_gettime_mask;
58 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
59 	if (error != 0)
60 		return (error);
61 	if (sc->type == RK809 || sc->type == RK817) {
62 		/* wait one 32khz cycle for clock shadow registers to latch */
63 		DELAY(1000000 / 32000);
64 	}
65 	ctrl &= ~sc->rtc_regs.ctrl_gettime_mask;
66 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
67 	if (error != 0)
68 		return (error);
69 
70 	/* This works as long as sc->rtc_regs.secs = 0 */
71 	error = rk8xx_read(dev, sc->rtc_regs.secs, data, 7);
72 	if (error != 0)
73 		return (error);
74 
75 	/*
76 	 * If the reported year is earlier than 2019, assume the clock is unset.
77 	 * This is both later than the reset value for the RK805 and RK808 as
78 	 * well as being prior to the current time.
79 	 */
80 	if (data[sc->rtc_regs.years] < 0x19)
81 		return (EINVAL);
82 
83 	memset(&bct, 0, sizeof(bct));
84 	bct.year = data[sc->rtc_regs.years];
85 	bct.mon = data[sc->rtc_regs.months] & sc->rtc_regs.months_mask;
86 	bct.day = data[sc->rtc_regs.days] & sc->rtc_regs.days_mask;
87 	bct.hour = data[sc->rtc_regs.hours] & sc->rtc_regs.hours_mask;
88 	bct.min = data[sc->rtc_regs.minutes] & sc->rtc_regs.minutes_mask;
89 	bct.sec = data[sc->rtc_regs.secs] & sc->rtc_regs.secs_mask;
90 	bct.dow = data[sc->rtc_regs.weeks] & sc->rtc_regs.weeks_mask;
91 	/* The day of week is reported as 1-7 with 1 = Monday */
92 	if (bct.dow == 7)
93 		bct.dow = 0;
94 	bct.ispm = 0;
95 	if (sc->type == RK809 || sc->type == RK817)
96 		bct.year += 0x2000;	/* valid for 2000-2099 only */
97 
98 	if (bootverbose)
99 		device_printf(dev, "Read RTC: %02x-%02x-%02x %02x:%02x:%02x\n",
100 		    bct.year, bct.mon, bct.day, bct.hour, bct.min, bct.sec);
101 
102 	return (clock_bcd_to_ts(&bct, ts, false));
103 }
104 
105 int
106 rk8xx_settime(device_t dev, struct timespec *ts)
107 {
108 	struct rk8xx_softc *sc;
109 	struct bcd_clocktime bct;
110 	uint8_t data[7];
111 	int error;
112 	uint8_t ctrl;
113 
114 	sc = device_get_softc(dev);
115 
116 	clock_ts_to_bcd(ts, &bct, false);
117 
118 	/* This works as long as RK805_RTC_SECS = 0 */
119 	if (sc->type == RK809 || sc->type == RK817) {
120 		/* valid for 2000-2099 only */
121 		if ((bct.year & 0xff00) != 0x2000) {
122 			device_printf(dev, "year out of range\n");
123 			return (EINVAL);
124 		}
125 		bct.year &= 0x00ff;
126 	}
127 	data[sc->rtc_regs.years] = bct.year;
128 	data[sc->rtc_regs.months] = bct.mon;
129 	data[sc->rtc_regs.days] = bct.day;
130 	data[sc->rtc_regs.hours] = bct.hour;
131 	data[sc->rtc_regs.minutes] = bct.min;
132 	data[sc->rtc_regs.secs] = bct.sec;
133 	data[sc->rtc_regs.weeks] = bct.dow;
134 	/* The day of week is reported as 1-7 with 1 = Monday */
135 	if (data[sc->rtc_regs.weeks] == 0)
136 		data[sc->rtc_regs.weeks] = 7;
137 
138 	error = rk8xx_read(dev, sc->rtc_regs.ctrl, &ctrl, 1);
139 	if (error != 0)
140 		return (error);
141 
142 	ctrl |= sc->rtc_regs.ctrl_stop_mask;
143 	ctrl &= ~sc->rtc_regs.ctrl_ampm_mask;
144 	error = rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
145 	if (error != 0)
146 		return (error);
147 
148 	error = rk8xx_write(dev, sc->rtc_regs.secs, data, 7);
149 	ctrl &= ~sc->rtc_regs.ctrl_stop_mask;
150 	rk8xx_write(dev, sc->rtc_regs.ctrl, &ctrl, 1);
151 
152 	return (error);
153 }
154