xref: /freebsd/sys/arm/allwinner/aw_rtc.c (revision 5dae51da3da0cc94d17bd67b308fad304ebec7e0)
1 /*-
2  * Copyright (c) 2016 Vladimir Belian <fate10@gmail.com>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include <sys/param.h>
31 #include <sys/bus.h>
32 #include <sys/time.h>
33 #include <sys/rman.h>
34 #include <sys/clock.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
38 #include <sys/resource.h>
39 
40 #include <machine/bus.h>
41 #include <machine/resource.h>
42 
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 
46 #include <arm/allwinner/aw_machdep.h>
47 
48 #include "clock_if.h"
49 
50 #define	LOSC_CTRL_REG			0x00
51 #define	A10_RTC_DATE_REG		0x04
52 #define	A10_RTC_TIME_REG		0x08
53 #define	A31_LOSC_AUTO_SWT_STA		0x04
54 #define	A31_RTC_DATE_REG		0x10
55 #define	A31_RTC_TIME_REG		0x14
56 
57 #define	TIME_MASK			0x001f3f3f
58 
59 #define	LOSC_OSC_SRC			(1 << 0)
60 #define	LOSC_GSM			(1 << 3)
61 #define	LOSC_AUTO_SW_EN			(1 << 14)
62 #define	LOSC_MAGIC			0x16aa0000
63 #define	LOSC_BUSY_MASK			0x00000380
64 
65 #define	IS_SUN7I			(sc->type == A20_RTC)
66 
67 #define	YEAR_MIN			(IS_SUN7I ? 1970 : 2010)
68 #define	YEAR_MAX			(IS_SUN7I ? 2100 : 2073)
69 #define	YEAR_OFFSET			(IS_SUN7I ? 1900 : 2010)
70 #define	YEAR_MASK			(IS_SUN7I ? 0xff : 0x3f)
71 #define	LEAP_BIT			(IS_SUN7I ? 24 : 22)
72 
73 #define	GET_SEC_VALUE(x)		((x)  & 0x0000003f)
74 #define	GET_MIN_VALUE(x)		(((x) & 0x00003f00) >> 8)
75 #define	GET_HOUR_VALUE(x)		(((x) & 0x001f0000) >> 16)
76 #define	GET_DAY_VALUE(x)		((x)  & 0x0000001f)
77 #define	GET_MON_VALUE(x)		(((x) & 0x00000f00) >> 8)
78 #define	GET_YEAR_VALUE(x)		(((x) >> 16) & YEAR_MASK)
79 
80 #define	SET_DAY_VALUE(x)		GET_DAY_VALUE(x)
81 #define	SET_MON_VALUE(x)		(((x) & 0x0000000f) << 8)
82 #define	SET_YEAR_VALUE(x)		(((x) & YEAR_MASK)  << 16)
83 #define	SET_LEAP_VALUE(x)		(((x) & 0x00000001) << LEAP_BIT)
84 #define	SET_SEC_VALUE(x)		GET_SEC_VALUE(x)
85 #define	SET_MIN_VALUE(x)		(((x) & 0x0000003f) << 8)
86 #define	SET_HOUR_VALUE(x)		(((x) & 0x0000001f) << 16)
87 
88 #define	HALF_OF_SEC_NS			500000000
89 #define	RTC_RES_US			1000000
90 #define	RTC_TIMEOUT			70
91 
92 #define	RTC_READ(sc, reg) 		bus_read_4((sc)->res, (reg))
93 #define	RTC_WRITE(sc, reg, val)		bus_write_4((sc)->res, (reg), (val))
94 
95 #define	IS_LEAP_YEAR(y) \
96 	(((y) % 400) == 0 || (((y) % 100) != 0 && ((y) % 4) == 0))
97 
98 #define	A10_RTC	1
99 #define	A20_RTC	2
100 #define	A31_RTC	3
101 
102 static struct ofw_compat_data compat_data[] = {
103 	{ "allwinner,sun4i-a10-rtc", A10_RTC },
104 	{ "allwinner,sun7i-a20-rtc", A20_RTC },
105 	{ "allwinner,sun6i-a31-rtc", A31_RTC },
106 	{ NULL, 0 }
107 };
108 
109 struct aw_rtc_softc {
110 	struct resource		*res;
111 	int			type;
112 	bus_size_t		rtc_date;
113 	bus_size_t		rtc_time;
114 };
115 
116 static int aw_rtc_probe(device_t dev);
117 static int aw_rtc_attach(device_t dev);
118 static int aw_rtc_detach(device_t dev);
119 
120 static int aw_rtc_gettime(device_t dev, struct timespec *ts);
121 static int aw_rtc_settime(device_t dev, struct timespec *ts);
122 
123 static device_method_t aw_rtc_methods[] = {
124 	DEVMETHOD(device_probe,		aw_rtc_probe),
125 	DEVMETHOD(device_attach,	aw_rtc_attach),
126 	DEVMETHOD(device_detach,	aw_rtc_detach),
127 
128 	DEVMETHOD(clock_gettime,	aw_rtc_gettime),
129 	DEVMETHOD(clock_settime,	aw_rtc_settime),
130 
131 	DEVMETHOD_END
132 };
133 
134 static driver_t aw_rtc_driver = {
135 	"rtc",
136 	aw_rtc_methods,
137 	sizeof(struct aw_rtc_softc),
138 };
139 
140 static devclass_t aw_rtc_devclass;
141 
142 EARLY_DRIVER_MODULE(aw_rtc, simplebus, aw_rtc_driver, aw_rtc_devclass, 0, 0,
143     BUS_PASS_TIMER + BUS_PASS_ORDER_MIDDLE);
144 
145 
146 static int
147 aw_rtc_probe(device_t dev)
148 {
149 	if (!ofw_bus_status_okay(dev))
150 		return (ENXIO);
151 
152 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
153 		return (ENXIO);
154 
155 	device_set_desc(dev, "Allwinner RTC");
156 
157 	return (BUS_PROBE_DEFAULT);
158 }
159 
160 static int
161 aw_rtc_attach(device_t dev)
162 {
163 	struct aw_rtc_softc *sc  = device_get_softc(dev);
164 	bus_size_t rtc_losc_sta;
165 	uint32_t val;
166 	int rid = 0;
167 
168 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
169 	if (!sc->res) {
170 		device_printf(dev, "could not allocate resources\n");
171 		return (ENXIO);
172 	}
173 
174 	sc->type = ofw_bus_search_compatible(dev, compat_data)->ocd_data;
175 	switch (sc->type) {
176 	case A10_RTC:
177 	case A20_RTC:
178 		sc->rtc_date = A10_RTC_DATE_REG;
179 		sc->rtc_time = A10_RTC_TIME_REG;
180 		rtc_losc_sta = LOSC_CTRL_REG;
181 		break;
182 	case A31_RTC:
183 		sc->rtc_date = A31_RTC_DATE_REG;
184 		sc->rtc_time = A31_RTC_TIME_REG;
185 		rtc_losc_sta = A31_LOSC_AUTO_SWT_STA;
186 		break;
187 	}
188 	val = RTC_READ(sc, LOSC_CTRL_REG);
189 	val |= LOSC_AUTO_SW_EN;
190 	val |= LOSC_MAGIC | LOSC_GSM | LOSC_OSC_SRC;
191 	RTC_WRITE(sc, LOSC_CTRL_REG, val);
192 
193 	DELAY(100);
194 
195 	if (bootverbose) {
196 		val = RTC_READ(sc, rtc_losc_sta);
197 		if ((val & LOSC_OSC_SRC) == 0)
198 			device_printf(dev, "Using internal oscillator\n");
199 		else
200 			device_printf(dev, "Using external oscillator\n");
201 	}
202 
203 	clock_register(dev, RTC_RES_US);
204 
205 	return (0);
206 }
207 
208 static int
209 aw_rtc_detach(device_t dev)
210 {
211 	/* can't support detach, since there's no clock_unregister function */
212 	return (EBUSY);
213 }
214 
215 static int
216 aw_rtc_gettime(device_t dev, struct timespec *ts)
217 {
218 	struct aw_rtc_softc *sc  = device_get_softc(dev);
219 	struct clocktime ct;
220 	uint32_t rdate, rtime;
221 
222 	rdate = RTC_READ(sc, sc->rtc_date);
223 	rtime = RTC_READ(sc, sc->rtc_time);
224 
225 	if ((rtime & TIME_MASK) == 0)
226 		rdate = RTC_READ(sc, sc->rtc_date);
227 
228 	ct.sec = GET_SEC_VALUE(rtime);
229 	ct.min = GET_MIN_VALUE(rtime);
230 	ct.hour = GET_HOUR_VALUE(rtime);
231 	ct.day = GET_DAY_VALUE(rdate);
232 	ct.mon = GET_MON_VALUE(rdate);
233 	ct.year = GET_YEAR_VALUE(rdate) + YEAR_OFFSET;
234 	ct.dow = -1;
235 	/* RTC resolution is 1 sec */
236 	ct.nsec = 0;
237 
238 	return (clock_ct_to_ts(&ct, ts));
239 }
240 
241 static int
242 aw_rtc_settime(device_t dev, struct timespec *ts)
243 {
244 	struct aw_rtc_softc *sc  = device_get_softc(dev);
245 	struct clocktime ct;
246 	uint32_t clk, rdate, rtime;
247 
248 	/* RTC resolution is 1 sec */
249 	if (ts->tv_nsec >= HALF_OF_SEC_NS)
250 		ts->tv_sec++;
251 	ts->tv_nsec = 0;
252 
253 	clock_ts_to_ct(ts, &ct);
254 
255 	if ((ct.year < YEAR_MIN) || (ct.year > YEAR_MAX)) {
256 		device_printf(dev, "could not set time, year out of range\n");
257 		return (EINVAL);
258 	}
259 
260 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
261 		if (clk > RTC_TIMEOUT) {
262 			device_printf(dev, "could not set time, RTC busy\n");
263 			return (EINVAL);
264 		}
265 		DELAY(1);
266 	}
267 	/* reset time register to avoid unexpected date increment */
268 	RTC_WRITE(sc, sc->rtc_time, 0);
269 
270 	rdate = SET_DAY_VALUE(ct.day) | SET_MON_VALUE(ct.mon) |
271 		SET_YEAR_VALUE(ct.year - YEAR_OFFSET) |
272 		SET_LEAP_VALUE(IS_LEAP_YEAR(ct.year));
273 
274 	rtime = SET_SEC_VALUE(ct.sec) | SET_MIN_VALUE(ct.min) |
275 		SET_HOUR_VALUE(ct.hour);
276 
277 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
278 		if (clk > RTC_TIMEOUT) {
279 			device_printf(dev, "could not set date, RTC busy\n");
280 			return (EINVAL);
281 		}
282 		DELAY(1);
283 	}
284 	RTC_WRITE(sc, sc->rtc_date, rdate);
285 
286 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
287 		if (clk > RTC_TIMEOUT) {
288 			device_printf(dev, "could not set time, RTC busy\n");
289 			return (EINVAL);
290 		}
291 		DELAY(1);
292 	}
293 	RTC_WRITE(sc, sc->rtc_time, rtime);
294 
295 	DELAY(RTC_TIMEOUT);
296 
297 	return (0);
298 }
299