xref: /freebsd/sys/arm/allwinner/aw_rtc.c (revision bfb92761dcba2d7427f0eb0ea777751e4a7db06c)
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/cdefs.h>
293f3af790SJared McNeill __FBSDID("$FreeBSD$");
303f3af790SJared McNeill 
313f3af790SJared McNeill #include <sys/param.h>
323f3af790SJared McNeill #include <sys/bus.h>
333f3af790SJared McNeill #include <sys/time.h>
343f3af790SJared McNeill #include <sys/rman.h>
353f3af790SJared McNeill #include <sys/clock.h>
363f3af790SJared McNeill #include <sys/systm.h>
373f3af790SJared McNeill #include <sys/kernel.h>
383f3af790SJared McNeill #include <sys/module.h>
393f3af790SJared McNeill #include <sys/resource.h>
403f3af790SJared McNeill 
413f3af790SJared McNeill #include <machine/bus.h>
423f3af790SJared McNeill #include <machine/resource.h>
433f3af790SJared McNeill 
443f3af790SJared McNeill #include <dev/ofw/ofw_bus.h>
453f3af790SJared McNeill #include <dev/ofw/ofw_bus_subr.h>
463f3af790SJared McNeill 
475f50cbd3SEmmanuel Vadot #include <dev/extres/clk/clk_fixed.h>
485f50cbd3SEmmanuel Vadot 
49d00d8ed8SEmmanuel Vadot #include <arm/allwinner/aw_machdep.h>
503f3af790SJared McNeill 
513f3af790SJared McNeill #include "clock_if.h"
523f3af790SJared McNeill 
533f3af790SJared McNeill #define	LOSC_CTRL_REG			0x00
547bf46e12SJared McNeill #define	A10_RTC_DATE_REG		0x04
557bf46e12SJared McNeill #define	A10_RTC_TIME_REG		0x08
567bf46e12SJared McNeill #define	A31_LOSC_AUTO_SWT_STA		0x04
577bf46e12SJared McNeill #define	A31_RTC_DATE_REG		0x10
587bf46e12SJared McNeill #define	A31_RTC_TIME_REG		0x14
593f3af790SJared McNeill 
603f3af790SJared McNeill #define	TIME_MASK			0x001f3f3f
613f3af790SJared McNeill 
627bf46e12SJared McNeill #define	LOSC_OSC_SRC			(1 << 0)
637bf46e12SJared McNeill #define	LOSC_GSM			(1 << 3)
647bf46e12SJared McNeill #define	LOSC_AUTO_SW_EN			(1 << 14)
653f3af790SJared McNeill #define	LOSC_MAGIC			0x16aa0000
663f3af790SJared McNeill #define	LOSC_BUSY_MASK			0x00000380
673f3af790SJared McNeill 
685f50cbd3SEmmanuel Vadot #define	IS_SUN7I			(sc->conf->is_a20 == true)
693f3af790SJared McNeill 
703f3af790SJared McNeill #define	YEAR_MIN			(IS_SUN7I ? 1970 : 2010)
713f3af790SJared McNeill #define	YEAR_MAX			(IS_SUN7I ? 2100 : 2073)
723f3af790SJared McNeill #define	YEAR_OFFSET			(IS_SUN7I ? 1900 : 2010)
733f3af790SJared McNeill #define	YEAR_MASK			(IS_SUN7I ? 0xff : 0x3f)
743f3af790SJared McNeill #define	LEAP_BIT			(IS_SUN7I ? 24 : 22)
753f3af790SJared McNeill 
763f3af790SJared McNeill #define	GET_SEC_VALUE(x)		((x)  & 0x0000003f)
773f3af790SJared McNeill #define	GET_MIN_VALUE(x)		(((x) & 0x00003f00) >> 8)
783f3af790SJared McNeill #define	GET_HOUR_VALUE(x)		(((x) & 0x001f0000) >> 16)
793f3af790SJared McNeill #define	GET_DAY_VALUE(x)		((x)  & 0x0000001f)
803f3af790SJared McNeill #define	GET_MON_VALUE(x)		(((x) & 0x00000f00) >> 8)
813f3af790SJared McNeill #define	GET_YEAR_VALUE(x)		(((x) >> 16) & YEAR_MASK)
823f3af790SJared McNeill 
833f3af790SJared McNeill #define	SET_DAY_VALUE(x)		GET_DAY_VALUE(x)
843f3af790SJared McNeill #define	SET_MON_VALUE(x)		(((x) & 0x0000000f) << 8)
853f3af790SJared McNeill #define	SET_YEAR_VALUE(x)		(((x) & YEAR_MASK)  << 16)
863f3af790SJared McNeill #define	SET_LEAP_VALUE(x)		(((x) & 0x00000001) << LEAP_BIT)
873f3af790SJared McNeill #define	SET_SEC_VALUE(x)		GET_SEC_VALUE(x)
883f3af790SJared McNeill #define	SET_MIN_VALUE(x)		(((x) & 0x0000003f) << 8)
893f3af790SJared McNeill #define	SET_HOUR_VALUE(x)		(((x) & 0x0000001f) << 16)
903f3af790SJared McNeill 
913f3af790SJared McNeill #define	HALF_OF_SEC_NS			500000000
923f3af790SJared McNeill #define	RTC_RES_US			1000000
933f3af790SJared McNeill #define	RTC_TIMEOUT			70
943f3af790SJared McNeill 
953f3af790SJared McNeill #define	RTC_READ(sc, reg) 		bus_read_4((sc)->res, (reg))
963f3af790SJared McNeill #define	RTC_WRITE(sc, reg, val)		bus_write_4((sc)->res, (reg), (val))
973f3af790SJared McNeill 
985f50cbd3SEmmanuel Vadot #define	IS_LEAP_YEAR(y) (((y) % 400) == 0 || (((y) % 100) != 0 && ((y) % 4) == 0))
993f3af790SJared McNeill 
1005f50cbd3SEmmanuel Vadot struct aw_rtc_conf {
1015f50cbd3SEmmanuel Vadot 	uint64_t	iosc_freq;
1025f50cbd3SEmmanuel Vadot 	bus_size_t	rtc_date;
1035f50cbd3SEmmanuel Vadot 	bus_size_t	rtc_time;
1045f50cbd3SEmmanuel Vadot 	bus_size_t	rtc_losc_sta;
1055f50cbd3SEmmanuel Vadot 	bool		is_a20;
1065f50cbd3SEmmanuel Vadot };
1075f50cbd3SEmmanuel Vadot 
1085f50cbd3SEmmanuel Vadot struct aw_rtc_conf a10_conf = {
1095f50cbd3SEmmanuel Vadot 	.rtc_date = A10_RTC_DATE_REG,
1105f50cbd3SEmmanuel Vadot 	.rtc_time = A10_RTC_TIME_REG,
1115f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = LOSC_CTRL_REG,
1125f50cbd3SEmmanuel Vadot };
1135f50cbd3SEmmanuel Vadot 
1145f50cbd3SEmmanuel Vadot struct aw_rtc_conf a20_conf = {
1155f50cbd3SEmmanuel Vadot 	.rtc_date = A10_RTC_DATE_REG,
1165f50cbd3SEmmanuel Vadot 	.rtc_time = A10_RTC_TIME_REG,
1175f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = LOSC_CTRL_REG,
1185f50cbd3SEmmanuel Vadot 	.is_a20 = true,
1195f50cbd3SEmmanuel Vadot };
1205f50cbd3SEmmanuel Vadot 
1215f50cbd3SEmmanuel Vadot struct aw_rtc_conf a31_conf = {
1225f50cbd3SEmmanuel Vadot 	.iosc_freq = 650000,			/* between 600 and 700 Khz */
1235f50cbd3SEmmanuel Vadot 	.rtc_date = A31_RTC_DATE_REG,
1245f50cbd3SEmmanuel Vadot 	.rtc_time = A31_RTC_TIME_REG,
1255f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = A31_LOSC_AUTO_SWT_STA,
1265f50cbd3SEmmanuel Vadot };
1275f50cbd3SEmmanuel Vadot 
1285f50cbd3SEmmanuel Vadot struct aw_rtc_conf h3_conf = {
1295f50cbd3SEmmanuel Vadot 	.iosc_freq = 16000000,
1305f50cbd3SEmmanuel Vadot 	.rtc_date = A31_RTC_DATE_REG,
1315f50cbd3SEmmanuel Vadot 	.rtc_time = A31_RTC_TIME_REG,
1325f50cbd3SEmmanuel Vadot 	.rtc_losc_sta = A31_LOSC_AUTO_SWT_STA,
1335f50cbd3SEmmanuel Vadot };
1343f3af790SJared McNeill 
1353f3af790SJared McNeill static struct ofw_compat_data compat_data[] = {
1365f50cbd3SEmmanuel Vadot 	{ "allwinner,sun4i-a10-rtc", (uintptr_t) &a10_conf },
1375f50cbd3SEmmanuel Vadot 	{ "allwinner,sun7i-a20-rtc", (uintptr_t) &a20_conf },
1385f50cbd3SEmmanuel Vadot 	{ "allwinner,sun6i-a31-rtc", (uintptr_t) &a31_conf },
1395f50cbd3SEmmanuel Vadot 	{ "allwinner,sun8i-h3-rtc", (uintptr_t) &h3_conf },
140*bfb92761SEmmanuel Vadot 	{ "allwinner,sun50i-h5-rtc", (uintptr_t) &h3_conf },
1417bf46e12SJared McNeill 	{ NULL, 0 }
1423f3af790SJared McNeill };
1433f3af790SJared McNeill 
1443f3af790SJared McNeill struct aw_rtc_softc {
1453f3af790SJared McNeill 	struct resource		*res;
1465f50cbd3SEmmanuel Vadot 	struct aw_rtc_conf	*conf;
147ef61a34aSJared McNeill 	int			type;
1483f3af790SJared McNeill };
1493f3af790SJared McNeill 
1505f50cbd3SEmmanuel Vadot static struct clk_fixed_def aw_rtc_osc32k = {
1515f50cbd3SEmmanuel Vadot 	.clkdef.id = 0,
1525f50cbd3SEmmanuel Vadot 	.freq = 32768,
1535f50cbd3SEmmanuel Vadot };
1545f50cbd3SEmmanuel Vadot 
1555f50cbd3SEmmanuel Vadot static struct clk_fixed_def aw_rtc_iosc = {
1565f50cbd3SEmmanuel Vadot 	.clkdef.id = 2,
1575f50cbd3SEmmanuel Vadot };
1585f50cbd3SEmmanuel Vadot 
1595f50cbd3SEmmanuel Vadot static void	aw_rtc_install_clocks(struct aw_rtc_softc *sc, device_t dev);
1605f50cbd3SEmmanuel Vadot 
1613f3af790SJared McNeill static int aw_rtc_probe(device_t dev);
1623f3af790SJared McNeill static int aw_rtc_attach(device_t dev);
1633f3af790SJared McNeill static int aw_rtc_detach(device_t dev);
1643f3af790SJared McNeill 
1653f3af790SJared McNeill static int aw_rtc_gettime(device_t dev, struct timespec *ts);
1663f3af790SJared McNeill static int aw_rtc_settime(device_t dev, struct timespec *ts);
1673f3af790SJared McNeill 
1683f3af790SJared McNeill static device_method_t aw_rtc_methods[] = {
1693f3af790SJared McNeill 	DEVMETHOD(device_probe,		aw_rtc_probe),
1703f3af790SJared McNeill 	DEVMETHOD(device_attach,	aw_rtc_attach),
1713f3af790SJared McNeill 	DEVMETHOD(device_detach,	aw_rtc_detach),
1723f3af790SJared McNeill 
1733f3af790SJared McNeill 	DEVMETHOD(clock_gettime,	aw_rtc_gettime),
1743f3af790SJared McNeill 	DEVMETHOD(clock_settime,	aw_rtc_settime),
1753f3af790SJared McNeill 
1763f3af790SJared McNeill 	DEVMETHOD_END
1773f3af790SJared McNeill };
1783f3af790SJared McNeill 
1793f3af790SJared McNeill static driver_t aw_rtc_driver = {
1803f3af790SJared McNeill 	"rtc",
1813f3af790SJared McNeill 	aw_rtc_methods,
1823f3af790SJared McNeill 	sizeof(struct aw_rtc_softc),
1833f3af790SJared McNeill };
1843f3af790SJared McNeill 
1853f3af790SJared McNeill static devclass_t aw_rtc_devclass;
1863f3af790SJared McNeill 
1873f3af790SJared McNeill EARLY_DRIVER_MODULE(aw_rtc, simplebus, aw_rtc_driver, aw_rtc_devclass, 0, 0,
1885f50cbd3SEmmanuel Vadot     BUS_PASS_BUS + BUS_PASS_ORDER_MIDDLE);
189abc15d70SEmmanuel Vadot MODULE_VERSION(aw_rtc, 1);
190abc15d70SEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data);
1913f3af790SJared McNeill 
1923f3af790SJared McNeill static int
1933f3af790SJared McNeill aw_rtc_probe(device_t dev)
1943f3af790SJared McNeill {
1953f3af790SJared McNeill 	if (!ofw_bus_status_okay(dev))
1963f3af790SJared McNeill 		return (ENXIO);
1973f3af790SJared McNeill 
1983f3af790SJared McNeill 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
1993f3af790SJared McNeill 		return (ENXIO);
2003f3af790SJared McNeill 
2013f3af790SJared McNeill 	device_set_desc(dev, "Allwinner RTC");
2023f3af790SJared McNeill 
2033f3af790SJared McNeill 	return (BUS_PROBE_DEFAULT);
2043f3af790SJared McNeill }
2053f3af790SJared McNeill 
2063f3af790SJared McNeill static int
2073f3af790SJared McNeill aw_rtc_attach(device_t dev)
2083f3af790SJared McNeill {
2093f3af790SJared McNeill 	struct aw_rtc_softc *sc  = device_get_softc(dev);
2103f3af790SJared McNeill 	uint32_t val;
2113f3af790SJared McNeill 	int rid = 0;
2123f3af790SJared McNeill 
2133f3af790SJared McNeill 	sc->res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, RF_ACTIVE);
2143f3af790SJared McNeill 	if (!sc->res) {
2153f3af790SJared McNeill 		device_printf(dev, "could not allocate resources\n");
2163f3af790SJared McNeill 		return (ENXIO);
2173f3af790SJared McNeill 	}
2183f3af790SJared McNeill 
2195f50cbd3SEmmanuel Vadot 	sc->conf = (struct aw_rtc_conf *)ofw_bus_search_compatible(dev, compat_data)->ocd_data;
2203f3af790SJared McNeill 	val = RTC_READ(sc, LOSC_CTRL_REG);
2217bf46e12SJared McNeill 	val |= LOSC_AUTO_SW_EN;
2223f3af790SJared McNeill 	val |= LOSC_MAGIC | LOSC_GSM | LOSC_OSC_SRC;
2233f3af790SJared McNeill 	RTC_WRITE(sc, LOSC_CTRL_REG, val);
2243f3af790SJared McNeill 
2253f3af790SJared McNeill 	DELAY(100);
2263f3af790SJared McNeill 
2277bf46e12SJared McNeill 	if (bootverbose) {
2285f50cbd3SEmmanuel Vadot 		val = RTC_READ(sc, sc->conf->rtc_losc_sta);
2297bf46e12SJared McNeill 		if ((val & LOSC_OSC_SRC) == 0)
2307bf46e12SJared McNeill 			device_printf(dev, "Using internal oscillator\n");
2317bf46e12SJared McNeill 		else
2327bf46e12SJared McNeill 			device_printf(dev, "Using external oscillator\n");
2333f3af790SJared McNeill 	}
2343f3af790SJared McNeill 
2355f50cbd3SEmmanuel Vadot 	aw_rtc_install_clocks(sc, dev);
2365f50cbd3SEmmanuel Vadot 
2373f3af790SJared McNeill 	clock_register(dev, RTC_RES_US);
2383f3af790SJared McNeill 
2393f3af790SJared McNeill 	return (0);
2403f3af790SJared McNeill }
2413f3af790SJared McNeill 
2423f3af790SJared McNeill static int
2433f3af790SJared McNeill aw_rtc_detach(device_t dev)
2443f3af790SJared McNeill {
2453f3af790SJared McNeill 	/* can't support detach, since there's no clock_unregister function */
2463f3af790SJared McNeill 	return (EBUSY);
2473f3af790SJared McNeill }
2483f3af790SJared McNeill 
2495f50cbd3SEmmanuel Vadot static void
2505f50cbd3SEmmanuel Vadot aw_rtc_install_clocks(struct aw_rtc_softc *sc, device_t dev) {
2515f50cbd3SEmmanuel Vadot 	struct clkdom *clkdom;
2525f50cbd3SEmmanuel Vadot 	const char **clknames;
2535f50cbd3SEmmanuel Vadot 	phandle_t node;
2545f50cbd3SEmmanuel Vadot 	int nclocks;
2555f50cbd3SEmmanuel Vadot 
2565f50cbd3SEmmanuel Vadot 	node = ofw_bus_get_node(dev);
2575f50cbd3SEmmanuel Vadot 	nclocks = ofw_bus_string_list_to_array(node, "clock-output-names", &clknames);
2585f50cbd3SEmmanuel Vadot 	/* No clocks to export */
2595f50cbd3SEmmanuel Vadot 	if (nclocks <= 0)
2605f50cbd3SEmmanuel Vadot 		return;
2615f50cbd3SEmmanuel Vadot 
2625f50cbd3SEmmanuel Vadot 	if (nclocks != 3) {
2635f50cbd3SEmmanuel Vadot 		device_printf(dev, "Having only %d clocks instead of 3, aborting\n", nclocks);
2645f50cbd3SEmmanuel Vadot 		return;
2655f50cbd3SEmmanuel Vadot 	}
2665f50cbd3SEmmanuel Vadot 
2675f50cbd3SEmmanuel Vadot 	clkdom = clkdom_create(dev);
2685f50cbd3SEmmanuel Vadot 
2695f50cbd3SEmmanuel Vadot 	aw_rtc_osc32k.clkdef.name = clknames[0];
2705f50cbd3SEmmanuel Vadot 	if (clknode_fixed_register(clkdom, &aw_rtc_osc32k) != 0)
2715f50cbd3SEmmanuel Vadot 		device_printf(dev, "Cannot register osc32k clock\n");
2725f50cbd3SEmmanuel Vadot 
2735f50cbd3SEmmanuel Vadot 	aw_rtc_iosc.clkdef.name = clknames[2];
2745f50cbd3SEmmanuel Vadot 	aw_rtc_iosc.freq = sc->conf->iosc_freq;
2755f50cbd3SEmmanuel Vadot 	if (clknode_fixed_register(clkdom, &aw_rtc_iosc) != 0)
2765f50cbd3SEmmanuel Vadot 		device_printf(dev, "Cannot register iosc clock\n");
2775f50cbd3SEmmanuel Vadot 
2785f50cbd3SEmmanuel Vadot 	clkdom_finit(clkdom);
2795f50cbd3SEmmanuel Vadot 
2805f50cbd3SEmmanuel Vadot 	if (bootverbose)
2815f50cbd3SEmmanuel Vadot 		clkdom_dump(clkdom);
2825f50cbd3SEmmanuel Vadot }
2835f50cbd3SEmmanuel Vadot 
2843f3af790SJared McNeill static int
2853f3af790SJared McNeill aw_rtc_gettime(device_t dev, struct timespec *ts)
2863f3af790SJared McNeill {
2873f3af790SJared McNeill 	struct aw_rtc_softc *sc  = device_get_softc(dev);
2883f3af790SJared McNeill 	struct clocktime ct;
2893f3af790SJared McNeill 	uint32_t rdate, rtime;
2903f3af790SJared McNeill 
2915f50cbd3SEmmanuel Vadot 	rdate = RTC_READ(sc, sc->conf->rtc_date);
2925f50cbd3SEmmanuel Vadot 	rtime = RTC_READ(sc, sc->conf->rtc_time);
2933f3af790SJared McNeill 
2943f3af790SJared McNeill 	if ((rtime & TIME_MASK) == 0)
2955f50cbd3SEmmanuel Vadot 		rdate = RTC_READ(sc, sc->conf->rtc_date);
2963f3af790SJared McNeill 
2973f3af790SJared McNeill 	ct.sec = GET_SEC_VALUE(rtime);
2983f3af790SJared McNeill 	ct.min = GET_MIN_VALUE(rtime);
2993f3af790SJared McNeill 	ct.hour = GET_HOUR_VALUE(rtime);
3003f3af790SJared McNeill 	ct.day = GET_DAY_VALUE(rdate);
3013f3af790SJared McNeill 	ct.mon = GET_MON_VALUE(rdate);
3023f3af790SJared McNeill 	ct.year = GET_YEAR_VALUE(rdate) + YEAR_OFFSET;
3033f3af790SJared McNeill 	ct.dow = -1;
3043f3af790SJared McNeill 	/* RTC resolution is 1 sec */
3053f3af790SJared McNeill 	ct.nsec = 0;
3063f3af790SJared McNeill 
3073f3af790SJared McNeill 	return (clock_ct_to_ts(&ct, ts));
3083f3af790SJared McNeill }
3093f3af790SJared McNeill 
3103f3af790SJared McNeill static int
3113f3af790SJared McNeill aw_rtc_settime(device_t dev, struct timespec *ts)
3123f3af790SJared McNeill {
3133f3af790SJared McNeill 	struct aw_rtc_softc *sc  = device_get_softc(dev);
3143f3af790SJared McNeill 	struct clocktime ct;
3153f3af790SJared McNeill 	uint32_t clk, rdate, rtime;
3163f3af790SJared McNeill 
3173f3af790SJared McNeill 	/* RTC resolution is 1 sec */
3183f3af790SJared McNeill 	if (ts->tv_nsec >= HALF_OF_SEC_NS)
3193f3af790SJared McNeill 		ts->tv_sec++;
3203f3af790SJared McNeill 	ts->tv_nsec = 0;
3213f3af790SJared McNeill 
3223f3af790SJared McNeill 	clock_ts_to_ct(ts, &ct);
3233f3af790SJared McNeill 
3243f3af790SJared McNeill 	if ((ct.year < YEAR_MIN) || (ct.year > YEAR_MAX)) {
3253f3af790SJared McNeill 		device_printf(dev, "could not set time, year out of range\n");
3263f3af790SJared McNeill 		return (EINVAL);
3273f3af790SJared McNeill 	}
3283f3af790SJared McNeill 
3293f3af790SJared McNeill 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
3303f3af790SJared McNeill 		if (clk > RTC_TIMEOUT) {
3313f3af790SJared McNeill 			device_printf(dev, "could not set time, RTC busy\n");
3323f3af790SJared McNeill 			return (EINVAL);
3333f3af790SJared McNeill 		}
3343f3af790SJared McNeill 		DELAY(1);
3353f3af790SJared McNeill 	}
3363f3af790SJared McNeill 	/* reset time register to avoid unexpected date increment */
3375f50cbd3SEmmanuel Vadot 	RTC_WRITE(sc, sc->conf->rtc_time, 0);
3383f3af790SJared McNeill 
3393f3af790SJared McNeill 	rdate = SET_DAY_VALUE(ct.day) | SET_MON_VALUE(ct.mon) |
3403f3af790SJared McNeill 		SET_YEAR_VALUE(ct.year - YEAR_OFFSET) |
3413f3af790SJared McNeill 		SET_LEAP_VALUE(IS_LEAP_YEAR(ct.year));
3423f3af790SJared McNeill 
3433f3af790SJared McNeill 	rtime = SET_SEC_VALUE(ct.sec) | SET_MIN_VALUE(ct.min) |
3443f3af790SJared McNeill 		SET_HOUR_VALUE(ct.hour);
3453f3af790SJared McNeill 
3463f3af790SJared McNeill 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
3473f3af790SJared McNeill 		if (clk > RTC_TIMEOUT) {
3483f3af790SJared McNeill 			device_printf(dev, "could not set date, RTC busy\n");
3493f3af790SJared McNeill 			return (EINVAL);
3503f3af790SJared McNeill 		}
3513f3af790SJared McNeill 		DELAY(1);
3523f3af790SJared McNeill 	}
3535f50cbd3SEmmanuel Vadot 	RTC_WRITE(sc, sc->conf->rtc_date, rdate);
3543f3af790SJared McNeill 
3553f3af790SJared McNeill 	for (clk = 0; RTC_READ(sc, LOSC_CTRL_REG) & LOSC_BUSY_MASK; clk++) {
3563f3af790SJared McNeill 		if (clk > RTC_TIMEOUT) {
3573f3af790SJared McNeill 			device_printf(dev, "could not set time, RTC busy\n");
3583f3af790SJared McNeill 			return (EINVAL);
3593f3af790SJared McNeill 		}
3603f3af790SJared McNeill 		DELAY(1);
3613f3af790SJared McNeill 	}
3625f50cbd3SEmmanuel Vadot 	RTC_WRITE(sc, sc->conf->rtc_time, rtime);
3633f3af790SJared McNeill 
3643f3af790SJared McNeill 	DELAY(RTC_TIMEOUT);
3653f3af790SJared McNeill 
3663f3af790SJared McNeill 	return (0);
3673f3af790SJared McNeill }
368