xref: /freebsd/sys/arm/allwinner/aw_rtc.c (revision be82b3a0)
13f3af790SJared McNeill /*-
25f50cbd3SEmmanuel Vadot  * Copyright (c) 2019 Emmanuel Vadot <manu@FreeBSD.Org>
33f3af790SJared McNeill  * Copyright (c) 2016 Vladimir Belian <fate10@gmail.com>
43f3af790SJared McNeill  * All rights reserved.
53f3af790SJared McNeill  *
63f3af790SJared McNeill  * Redistribution and use in source and binary forms, with or without
73f3af790SJared McNeill  * modification, are permitted provided that the following conditions
83f3af790SJared McNeill  * are met:
93f3af790SJared McNeill  * 1. Redistributions of source code must retain the above copyright
103f3af790SJared McNeill  *    notice, this list of conditions and the following disclaimer.
113f3af790SJared McNeill  * 2. Redistributions in binary form must reproduce the above copyright
123f3af790SJared McNeill  *    notice, this list of conditions and the following disclaimer in the
133f3af790SJared McNeill  *    documentation and/or other materials provided with the distribution.
143f3af790SJared McNeill  *
153f3af790SJared McNeill  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
163f3af790SJared McNeill  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
173f3af790SJared McNeill  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
183f3af790SJared McNeill  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
193f3af790SJared McNeill  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
203f3af790SJared McNeill  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
213f3af790SJared McNeill  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
223f3af790SJared McNeill  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
233f3af790SJared McNeill  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
243f3af790SJared McNeill  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
253f3af790SJared McNeill  * SUCH DAMAGE.
263f3af790SJared McNeill  */
273f3af790SJared McNeill 
283f3af790SJared McNeill #include <sys/param.h>
293f3af790SJared McNeill #include <sys/bus.h>
303f3af790SJared McNeill #include <sys/time.h>
313f3af790SJared McNeill #include <sys/rman.h>
323f3af790SJared McNeill #include <sys/clock.h>
333f3af790SJared McNeill #include <sys/systm.h>
343f3af790SJared McNeill #include <sys/kernel.h>
353f3af790SJared McNeill #include <sys/module.h>
363f3af790SJared McNeill #include <sys/resource.h>
373f3af790SJared McNeill 
383f3af790SJared McNeill #include <machine/bus.h>
393f3af790SJared McNeill #include <machine/resource.h>
403f3af790SJared McNeill 
413f3af790SJared McNeill #include <dev/ofw/ofw_bus.h>
423f3af790SJared McNeill #include <dev/ofw/ofw_bus_subr.h>
433f3af790SJared McNeill 
44be82b3a0SEmmanuel Vadot #include <dev/clk/clk_fixed.h>
455f50cbd3SEmmanuel Vadot 
46d00d8ed8SEmmanuel Vadot #include <arm/allwinner/aw_machdep.h>
473f3af790SJared McNeill 
483f3af790SJared McNeill #include "clock_if.h"
493f3af790SJared McNeill 
503f3af790SJared McNeill #define	LOSC_CTRL_REG			0x00
517bf46e12SJared McNeill #define	A10_RTC_DATE_REG		0x04
527bf46e12SJared McNeill #define	A10_RTC_TIME_REG		0x08
537bf46e12SJared McNeill #define	A31_LOSC_AUTO_SWT_STA		0x04
547bf46e12SJared McNeill #define	A31_RTC_DATE_REG		0x10
557bf46e12SJared McNeill #define	A31_RTC_TIME_REG		0x14
563f3af790SJared McNeill 
573f3af790SJared McNeill #define	TIME_MASK			0x001f3f3f
583f3af790SJared McNeill 
597bf46e12SJared McNeill #define	LOSC_OSC_SRC			(1 << 0)
607bf46e12SJared McNeill #define	LOSC_GSM			(1 << 3)
617bf46e12SJared McNeill #define	LOSC_AUTO_SW_EN			(1 << 14)
623f3af790SJared McNeill #define	LOSC_MAGIC			0x16aa0000
633f3af790SJared McNeill #define	LOSC_BUSY_MASK			0x00000380
643f3af790SJared McNeill 
655f50cbd3SEmmanuel Vadot #define	IS_SUN7I			(sc->conf->is_a20 == true)
663f3af790SJared McNeill 
673f3af790SJared McNeill #define	YEAR_MIN			(IS_SUN7I ? 1970 : 2010)
683f3af790SJared McNeill #define	YEAR_MAX			(IS_SUN7I ? 2100 : 2073)
693f3af790SJared McNeill #define	YEAR_OFFSET			(IS_SUN7I ? 1900 : 2010)
703f3af790SJared McNeill #define	YEAR_MASK			(IS_SUN7I ? 0xff : 0x3f)
713f3af790SJared McNeill #define	LEAP_BIT			(IS_SUN7I ? 24 : 22)
723f3af790SJared McNeill 
733f3af790SJared McNeill #define	GET_SEC_VALUE(x)		((x)  & 0x0000003f)
743f3af790SJared McNeill #define	GET_MIN_VALUE(x)		(((x) & 0x00003f00) >> 8)
753f3af790SJared McNeill #define	GET_HOUR_VALUE(x)		(((x) & 0x001f0000) >> 16)
763f3af790SJared McNeill #define	GET_DAY_VALUE(x)		((x)  & 0x0000001f)
773f3af790SJared McNeill #define	GET_MON_VALUE(x)		(((x) & 0x00000f00) >> 8)
783f3af790SJared McNeill #define	GET_YEAR_VALUE(x)		(((x) >> 16) & YEAR_MASK)
793f3af790SJared McNeill 
803f3af790SJared McNeill #define	SET_DAY_VALUE(x)		GET_DAY_VALUE(x)
813f3af790SJared McNeill #define	SET_MON_VALUE(x)		(((x) & 0x0000000f) << 8)
823f3af790SJared McNeill #define	SET_YEAR_VALUE(x)		(((x) & YEAR_MASK)  << 16)
833f3af790SJared McNeill #define	SET_LEAP_VALUE(x)		(((x) & 0x00000001) << LEAP_BIT)
843f3af790SJared McNeill #define	SET_SEC_VALUE(x)		GET_SEC_VALUE(x)
853f3af790SJared McNeill #define	SET_MIN_VALUE(x)		(((x) & 0x0000003f) << 8)
863f3af790SJared McNeill #define	SET_HOUR_VALUE(x)		(((x) & 0x0000001f) << 16)
873f3af790SJared McNeill 
883f3af790SJared McNeill #define	HALF_OF_SEC_NS			500000000
893f3af790SJared McNeill #define	RTC_RES_US			1000000
903f3af790SJared McNeill #define	RTC_TIMEOUT			70
913f3af790SJared McNeill 
923f3af790SJared McNeill #define	RTC_READ(sc, reg) 		bus_read_4((sc)->res, (reg))
933f3af790SJared McNeill #define	RTC_WRITE(sc, reg, val)		bus_write_4((sc)->res, (reg), (val))
943f3af790SJared McNeill 
955f50cbd3SEmmanuel Vadot #define	IS_LEAP_YEAR(y) (((y) % 400) == 0 || (((y) % 100) != 0 && ((y) % 4) == 0))
963f3af790SJared McNeill 
975f50cbd3SEmmanuel Vadot struct aw_rtc_conf {
985f50cbd3SEmmanuel Vadot 	uint64_t	iosc_freq;
995f50cbd3SEmmanuel Vadot 	bus_size_t	rtc_date;
1005f50cbd3SEmmanuel Vadot 	bus_size_t	rtc_time;
1015f50cbd3SEmmanuel Vadot 	bus_size_t	rtc_losc_sta;
1025f50cbd3SEmmanuel Vadot 	bool		is_a20;
1035f50cbd3SEmmanuel Vadot };
1045f50cbd3SEmmanuel Vadot 
1055f50cbd3SEmmanuel Vadot struct aw_rtc_conf a10_conf = {
1065f50cbd3SEmmanuel Vadot 	.rtc_date = A10_RTC_DATE_REG,
1075f50cbd3SEmmanuel Vadot 	.rtc_time = A10_RTC_TIME_REG,
1085f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = LOSC_CTRL_REG,
1095f50cbd3SEmmanuel Vadot };
1105f50cbd3SEmmanuel Vadot 
1115f50cbd3SEmmanuel Vadot struct aw_rtc_conf a20_conf = {
1125f50cbd3SEmmanuel Vadot 	.rtc_date = A10_RTC_DATE_REG,
1135f50cbd3SEmmanuel Vadot 	.rtc_time = A10_RTC_TIME_REG,
1145f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = LOSC_CTRL_REG,
1155f50cbd3SEmmanuel Vadot 	.is_a20 = true,
1165f50cbd3SEmmanuel Vadot };
1175f50cbd3SEmmanuel Vadot 
1185f50cbd3SEmmanuel Vadot struct aw_rtc_conf a31_conf = {
1195f50cbd3SEmmanuel Vadot 	.iosc_freq = 650000,			/* between 600 and 700 Khz */
1205f50cbd3SEmmanuel Vadot 	.rtc_date = A31_RTC_DATE_REG,
1215f50cbd3SEmmanuel Vadot 	.rtc_time = A31_RTC_TIME_REG,
1225f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = A31_LOSC_AUTO_SWT_STA,
1235f50cbd3SEmmanuel Vadot };
1245f50cbd3SEmmanuel Vadot 
1255f50cbd3SEmmanuel Vadot struct aw_rtc_conf h3_conf = {
1265f50cbd3SEmmanuel Vadot 	.iosc_freq = 16000000,
1275f50cbd3SEmmanuel Vadot 	.rtc_date = A31_RTC_DATE_REG,
1285f50cbd3SEmmanuel Vadot 	.rtc_time = A31_RTC_TIME_REG,
1295f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = A31_LOSC_AUTO_SWT_STA,
1305f50cbd3SEmmanuel Vadot };
1313f3af790SJared McNeill 
1323f3af790SJared McNeill static struct ofw_compat_data compat_data[] = {
1335f50cbd3SEmmanuel Vadot 	{ "allwinner,sun4i-a10-rtc", (uintptr_t) &a10_conf },
1345f50cbd3SEmmanuel Vadot 	{ "allwinner,sun7i-a20-rtc", (uintptr_t) &a20_conf },
1355f50cbd3SEmmanuel Vadot 	{ "allwinner,sun6i-a31-rtc", (uintptr_t) &a31_conf },
1365f50cbd3SEmmanuel Vadot 	{ "allwinner,sun8i-h3-rtc", (uintptr_t) &h3_conf },
137bfb92761SEmmanuel Vadot 	{ "allwinner,sun50i-h5-rtc", (uintptr_t) &h3_conf },
138f442e2fcSEmmanuel Vadot 	{ "allwinner,sun50i-h6-rtc", (uintptr_t) &h3_conf },
1397bf46e12SJared McNeill 	{ NULL, 0 }
1403f3af790SJared McNeill };
1413f3af790SJared McNeill 
1423f3af790SJared McNeill struct aw_rtc_softc {
1433f3af790SJared McNeill 	struct resource		*res;
1445f50cbd3SEmmanuel Vadot 	struct aw_rtc_conf	*conf;
145ef61a34aSJared McNeill 	int			type;
1463f3af790SJared McNeill };
1473f3af790SJared McNeill 
1485f50cbd3SEmmanuel Vadot static struct clk_fixed_def aw_rtc_osc32k = {
1495f50cbd3SEmmanuel Vadot 	.clkdef.id = 0,
1505f50cbd3SEmmanuel Vadot 	.freq = 32768,
1515f50cbd3SEmmanuel Vadot };
1525f50cbd3SEmmanuel Vadot 
1535f50cbd3SEmmanuel Vadot static struct clk_fixed_def aw_rtc_iosc = {
1545f50cbd3SEmmanuel Vadot 	.clkdef.id = 2,
1555f50cbd3SEmmanuel Vadot };
1565f50cbd3SEmmanuel Vadot 
1575f50cbd3SEmmanuel Vadot static void	aw_rtc_install_clocks(struct aw_rtc_softc *sc, device_t dev);
1585f50cbd3SEmmanuel Vadot 
1593f3af790SJared McNeill static int aw_rtc_probe(device_t dev);
1603f3af790SJared McNeill static int aw_rtc_attach(device_t dev);
1613f3af790SJared McNeill static int aw_rtc_detach(device_t dev);
1623f3af790SJared McNeill 
1633f3af790SJared McNeill static int aw_rtc_gettime(device_t dev, struct timespec *ts);
1643f3af790SJared McNeill static int aw_rtc_settime(device_t dev, struct timespec *ts);
1653f3af790SJared McNeill 
1663f3af790SJared McNeill static device_method_t aw_rtc_methods[] = {
1673f3af790SJared McNeill 	DEVMETHOD(device_probe,		aw_rtc_probe),
1683f3af790SJared McNeill 	DEVMETHOD(device_attach,	aw_rtc_attach),
1693f3af790SJared McNeill 	DEVMETHOD(device_detach,	aw_rtc_detach),
1703f3af790SJared McNeill 
1713f3af790SJared McNeill 	DEVMETHOD(clock_gettime,	aw_rtc_gettime),
1723f3af790SJared McNeill 	DEVMETHOD(clock_settime,	aw_rtc_settime),
1733f3af790SJared McNeill 
1743f3af790SJared McNeill 	DEVMETHOD_END
1753f3af790SJared McNeill };
1763f3af790SJared McNeill 
1773f3af790SJared McNeill static driver_t aw_rtc_driver = {
1783f3af790SJared McNeill 	"rtc",
1793f3af790SJared McNeill 	aw_rtc_methods,
1803f3af790SJared McNeill 	sizeof(struct aw_rtc_softc),
1813f3af790SJared McNeill };
1823f3af790SJared McNeill 
1837e1e2ba1SJohn Baldwin EARLY_DRIVER_MODULE(aw_rtc, simplebus, aw_rtc_driver, 0, 0,
184cfc8861bSEmmanuel Vadot     BUS_PASS_RESOURCE + BUS_PASS_ORDER_FIRST);
185abc15d70SEmmanuel Vadot MODULE_VERSION(aw_rtc, 1);
186abc15d70SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
1873f3af790SJared McNeill 
1883f3af790SJared McNeill static int
aw_rtc_probe(device_t dev)1893f3af790SJared McNeill aw_rtc_probe(device_t dev)
1903f3af790SJared McNeill {
1913f3af790SJared McNeill 	if (!ofw_bus_status_okay(dev))
1923f3af790SJared McNeill 		return (ENXIO);
1933f3af790SJared McNeill 
1943f3af790SJared McNeill 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
1953f3af790SJared McNeill 		return (ENXIO);
1963f3af790SJared McNeill 
1973f3af790SJared McNeill 	device_set_desc(dev, "Allwinner RTC");
1983f3af790SJared McNeill 
1993f3af790SJared McNeill 	return (BUS_PROBE_DEFAULT);
2003f3af790SJared McNeill }
2013f3af790SJared McNeill 
2023f3af790SJared McNeill static int
aw_rtc_attach(device_t dev)2033f3af790SJared McNeill aw_rtc_attach(device_t dev)
2043f3af790SJared McNeill {
2053f3af790SJared McNeill 	struct aw_rtc_softc *sc  = device_get_softc(dev);
2063f3af790SJared McNeill 	uint32_t val;
2073f3af790SJared McNeill 	int rid = 0;
2083f3af790SJared McNeill 
2093f3af790SJared McNeill 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
2103f3af790SJared McNeill 	if (!sc->res) {
2113f3af790SJared McNeill 		device_printf(dev, "could not allocate resources\n");
2123f3af790SJared McNeill 		return (ENXIO);
2133f3af790SJared McNeill 	}
2143f3af790SJared McNeill 
2155f50cbd3SEmmanuel Vadot 	sc->conf = (struct aw_rtc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
2163f3af790SJared McNeill 	val = RTC_READ(sc, LOSC_CTRL_REG);
2177bf46e12SJared McNeill 	val |= LOSC_AUTO_SW_EN;
2183f3af790SJared McNeill 	val |= LOSC_MAGIC | LOSC_GSM | LOSC_OSC_SRC;
2193f3af790SJared McNeill 	RTC_WRITE(sc, LOSC_CTRL_REG, val);
2203f3af790SJared McNeill 
2213f3af790SJared McNeill 	DELAY(100);
2223f3af790SJared McNeill 
2237bf46e12SJared McNeill 	if (bootverbose) {
2245f50cbd3SEmmanuel Vadot 		val = RTC_READ(sc, sc->conf->rtc_losc_sta);
2257bf46e12SJared McNeill 		if ((val & LOSC_OSC_SRC) == 0)
2267bf46e12SJared McNeill 			device_printf(dev, "Using internal oscillator\n");
2277bf46e12SJared McNeill 		else
2287bf46e12SJared McNeill 			device_printf(dev, "Using external oscillator\n");
2293f3af790SJared McNeill 	}
2303f3af790SJared McNeill 
2315f50cbd3SEmmanuel Vadot 	aw_rtc_install_clocks(sc, dev);
2325f50cbd3SEmmanuel Vadot 
2333f3af790SJared McNeill 	clock_register(dev, RTC_RES_US);
2343f3af790SJared McNeill 
2353f3af790SJared McNeill 	return (0);
2363f3af790SJared McNeill }
2373f3af790SJared McNeill 
2383f3af790SJared McNeill static int
aw_rtc_detach(device_t dev)2393f3af790SJared McNeill aw_rtc_detach(device_t dev)
2403f3af790SJared McNeill {
2413f3af790SJared McNeill 	/* can't support detach, since there's no clock_unregister function */
2423f3af790SJared McNeill 	return (EBUSY);
2433f3af790SJared McNeill }
2443f3af790SJared McNeill 
2455f50cbd3SEmmanuel Vadot static void
aw_rtc_install_clocks(struct aw_rtc_softc * sc,device_t dev)2465f50cbd3SEmmanuel Vadot aw_rtc_install_clocks(struct aw_rtc_softc *sc, device_t dev) {
2475f50cbd3SEmmanuel Vadot 	struct clkdom *clkdom;
2485f50cbd3SEmmanuel Vadot 	const char **clknames;
2495f50cbd3SEmmanuel Vadot 	phandle_t node;
2505f50cbd3SEmmanuel Vadot 	int nclocks;
2515f50cbd3SEmmanuel Vadot 
2525f50cbd3SEmmanuel Vadot 	node = ofw_bus_get_node(dev);
2535f50cbd3SEmmanuel Vadot 	nclocks = ofw_bus_string_list_to_array(node, "clock-output-names", &clknames);
2545f50cbd3SEmmanuel Vadot 	/* No clocks to export */
2555f50cbd3SEmmanuel Vadot 	if (nclocks <= 0)
2565f50cbd3SEmmanuel Vadot 		return;
2575f50cbd3SEmmanuel Vadot 
2585f50cbd3SEmmanuel Vadot 	if (nclocks != 3) {
2595f50cbd3SEmmanuel Vadot 		device_printf(dev, "Having only %d clocks instead of 3, aborting\n", nclocks);
2605f50cbd3SEmmanuel Vadot 		return;
2615f50cbd3SEmmanuel Vadot 	}
2625f50cbd3SEmmanuel Vadot 
2635f50cbd3SEmmanuel Vadot 	clkdom = clkdom_create(dev);
2645f50cbd3SEmmanuel Vadot 
2655f50cbd3SEmmanuel Vadot 	aw_rtc_osc32k.clkdef.name = clknames[0];
2665f50cbd3SEmmanuel Vadot 	if (clknode_fixed_register(clkdom, &aw_rtc_osc32k) != 0)
2675f50cbd3SEmmanuel Vadot 		device_printf(dev, "Cannot register osc32k clock\n");
2685f50cbd3SEmmanuel Vadot 
2695f50cbd3SEmmanuel Vadot 	aw_rtc_iosc.clkdef.name = clknames[2];
2705f50cbd3SEmmanuel Vadot 	aw_rtc_iosc.freq = sc->conf->iosc_freq;
2715f50cbd3SEmmanuel Vadot 	if (clknode_fixed_register(clkdom, &aw_rtc_iosc) != 0)
2725f50cbd3SEmmanuel Vadot 		device_printf(dev, "Cannot register iosc clock\n");
2735f50cbd3SEmmanuel Vadot 
2745f50cbd3SEmmanuel Vadot 	clkdom_finit(clkdom);
2755f50cbd3SEmmanuel Vadot 
2765f50cbd3SEmmanuel Vadot 	if (bootverbose)
2775f50cbd3SEmmanuel Vadot 		clkdom_dump(clkdom);
2785f50cbd3SEmmanuel Vadot }
2795f50cbd3SEmmanuel Vadot 
2803f3af790SJared McNeill static int
aw_rtc_gettime(device_t dev,struct timespec * ts)2813f3af790SJared McNeill aw_rtc_gettime(device_t dev, struct timespec *ts)
2823f3af790SJared McNeill {
2833f3af790SJared McNeill 	struct aw_rtc_softc *sc  = device_get_softc(dev);
2843f3af790SJared McNeill 	struct clocktime ct;
2853f3af790SJared McNeill 	uint32_t rdate, rtime;
2863f3af790SJared McNeill 
2875f50cbd3SEmmanuel Vadot 	rdate = RTC_READ(sc, sc->conf->rtc_date);
2885f50cbd3SEmmanuel Vadot 	rtime = RTC_READ(sc, sc->conf->rtc_time);
2893f3af790SJared McNeill 
2903f3af790SJared McNeill 	if ((rtime & TIME_MASK) == 0)
2915f50cbd3SEmmanuel Vadot 		rdate = RTC_READ(sc, sc->conf->rtc_date);
2923f3af790SJared McNeill 
2933f3af790SJared McNeill 	ct.sec = GET_SEC_VALUE(rtime);
2943f3af790SJared McNeill 	ct.min = GET_MIN_VALUE(rtime);
2953f3af790SJared McNeill 	ct.hour = GET_HOUR_VALUE(rtime);
2963f3af790SJared McNeill 	ct.day = GET_DAY_VALUE(rdate);
2973f3af790SJared McNeill 	ct.mon = GET_MON_VALUE(rdate);
2983f3af790SJared McNeill 	ct.year = GET_YEAR_VALUE(rdate) + YEAR_OFFSET;
2993f3af790SJared McNeill 	ct.dow = -1;
3003f3af790SJared McNeill 	/* RTC resolution is 1 sec */
3013f3af790SJared McNeill 	ct.nsec = 0;
3023f3af790SJared McNeill 
3033f3af790SJared McNeill 	return (clock_ct_to_ts(&ct, ts));
3043f3af790SJared McNeill }
3053f3af790SJared McNeill 
3063f3af790SJared McNeill static int
aw_rtc_settime(device_t dev,struct timespec * ts)3073f3af790SJared McNeill aw_rtc_settime(device_t dev, struct timespec *ts)
3083f3af790SJared McNeill {
3093f3af790SJared McNeill 	struct aw_rtc_softc *sc  = device_get_softc(dev);
3103f3af790SJared McNeill 	struct clocktime ct;
3113f3af790SJared McNeill 	uint32_t clk, rdate, rtime;
3123f3af790SJared McNeill 
3133f3af790SJared McNeill 	/* RTC resolution is 1 sec */
3143f3af790SJared McNeill 	if (ts->tv_nsec >= HALF_OF_SEC_NS)
3153f3af790SJared McNeill 		ts->tv_sec++;
3163f3af790SJared McNeill 	ts->tv_nsec = 0;
3173f3af790SJared McNeill 
3183f3af790SJared McNeill 	clock_ts_to_ct(ts, &ct);
3193f3af790SJared McNeill 
3203f3af790SJared McNeill 	if ((ct.year < YEAR_MIN) || (ct.year > YEAR_MAX)) {
3213f3af790SJared McNeill 		device_printf(dev, "could not set time, year out of range\n");
3223f3af790SJared McNeill 		return (EINVAL);
3233f3af790SJared McNeill 	}
3243f3af790SJared McNeill 
3253f3af790SJared McNeill 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
3263f3af790SJared McNeill 		if (clk > RTC_TIMEOUT) {
3273f3af790SJared McNeill 			device_printf(dev, "could not set time, RTC busy\n");
3283f3af790SJared McNeill 			return (EINVAL);
3293f3af790SJared McNeill 		}
3303f3af790SJared McNeill 		DELAY(1);
3313f3af790SJared McNeill 	}
3323f3af790SJared McNeill 	/* reset time register to avoid unexpected date increment */
3335f50cbd3SEmmanuel Vadot 	RTC_WRITE(sc, sc->conf->rtc_time, 0);
3343f3af790SJared McNeill 
3353f3af790SJared McNeill 	rdate = SET_DAY_VALUE(ct.day) | SET_MON_VALUE(ct.mon) |
3363f3af790SJared McNeill 		SET_YEAR_VALUE(ct.year - YEAR_OFFSET) |
3373f3af790SJared McNeill 		SET_LEAP_VALUE(IS_LEAP_YEAR(ct.year));
3383f3af790SJared McNeill 
3393f3af790SJared McNeill 	rtime = SET_SEC_VALUE(ct.sec) | SET_MIN_VALUE(ct.min) |
3403f3af790SJared McNeill 		SET_HOUR_VALUE(ct.hour);
3413f3af790SJared McNeill 
3423f3af790SJared McNeill 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
3433f3af790SJared McNeill 		if (clk > RTC_TIMEOUT) {
3443f3af790SJared McNeill 			device_printf(dev, "could not set date, RTC busy\n");
3453f3af790SJared McNeill 			return (EINVAL);
3463f3af790SJared McNeill 		}
3473f3af790SJared McNeill 		DELAY(1);
3483f3af790SJared McNeill 	}
3495f50cbd3SEmmanuel Vadot 	RTC_WRITE(sc, sc->conf->rtc_date, rdate);
3503f3af790SJared McNeill 
3513f3af790SJared McNeill 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
3523f3af790SJared McNeill 		if (clk > RTC_TIMEOUT) {
3533f3af790SJared McNeill 			device_printf(dev, "could not set time, RTC busy\n");
3543f3af790SJared McNeill 			return (EINVAL);
3553f3af790SJared McNeill 		}
3563f3af790SJared McNeill 		DELAY(1);
3573f3af790SJared McNeill 	}
3585f50cbd3SEmmanuel Vadot 	RTC_WRITE(sc, sc->conf->rtc_time, rtime);
3593f3af790SJared McNeill 
3603f3af790SJared McNeill 	DELAY(RTC_TIMEOUT);
3613f3af790SJared McNeill 
3623f3af790SJared McNeill 	return (0);
3633f3af790SJared McNeill }
364