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