xref: /freebsd-src/sys/arm/freescale/imx/imx6_snvs.c (revision 685dc743dc3b5645e34836464128e1c0558b404b)
13a28d1b6SIan Lepore /*-
23a28d1b6SIan Lepore  * Copyright (c) 2017 Ian Lepore <ian@freebsd.org>
33a28d1b6SIan Lepore  * All rights reserved.
43a28d1b6SIan Lepore  *
53a28d1b6SIan Lepore  * Redistribution and use in source and binary forms, with or without
63a28d1b6SIan Lepore  * modification, are permitted provided that the following conditions
73a28d1b6SIan Lepore  * are met:
83a28d1b6SIan Lepore  * 1. Redistributions of source code must retain the above copyright
93a28d1b6SIan Lepore  *    notice, this list of conditions and the following disclaimer.
103a28d1b6SIan Lepore  * 2. Redistributions in binary form must reproduce the above copyright
113a28d1b6SIan Lepore  *    notice, this list of conditions and the following disclaimer in the
123a28d1b6SIan Lepore  *    documentation and/or other materials provided with the distribution.
133a28d1b6SIan Lepore  *
143a28d1b6SIan Lepore  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
153a28d1b6SIan Lepore  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
163a28d1b6SIan Lepore  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
173a28d1b6SIan Lepore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
183a28d1b6SIan Lepore  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
193a28d1b6SIan Lepore  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
203a28d1b6SIan Lepore  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
213a28d1b6SIan Lepore  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
223a28d1b6SIan Lepore  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
233a28d1b6SIan Lepore  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
243a28d1b6SIan Lepore  * SUCH DAMAGE.
253a28d1b6SIan Lepore  */
263a28d1b6SIan Lepore 
273a28d1b6SIan Lepore #include <sys/cdefs.h>
283a28d1b6SIan Lepore /*
293a28d1b6SIan Lepore  * Driver for imx6 Secure Non-Volatile Storage system, which really means "all
303a28d1b6SIan Lepore  * the stuff that's powered by a battery when main power is off".  This includes
313a28d1b6SIan Lepore  * realtime clock, tamper monitor, and power-management functions.  Currently
323a28d1b6SIan Lepore  * this driver provides only realtime clock support.
333a28d1b6SIan Lepore  */
343a28d1b6SIan Lepore 
353a28d1b6SIan Lepore #include <sys/param.h>
363a28d1b6SIan Lepore #include <sys/systm.h>
373a28d1b6SIan Lepore #include <sys/bus.h>
383a28d1b6SIan Lepore #include <sys/clock.h>
393a28d1b6SIan Lepore #include <sys/kernel.h>
403a28d1b6SIan Lepore #include <sys/module.h>
413a28d1b6SIan Lepore #include <machine/bus.h>
423a28d1b6SIan Lepore 
433a28d1b6SIan Lepore #include <dev/ofw/ofw_bus_subr.h>
443a28d1b6SIan Lepore 
453a28d1b6SIan Lepore #include "clock_if.h"
460ffeeb41SIan Lepore #include "syscon_if.h"
473a28d1b6SIan Lepore 
483a28d1b6SIan Lepore #define	SNVS_LPCR		0x38		/* Control register */
493a28d1b6SIan Lepore #define	  LPCR_LPCALB_VAL_SHIFT	  10		/* Calibration shift */
503a28d1b6SIan Lepore #define	  LPCR_LPCALB_VAL_MASK	  0x1f		/* Calibration mask */
513a28d1b6SIan Lepore #define	  LPCR_LPCALB_EN	  (1u << 8)	/* Calibration enable */
523a28d1b6SIan Lepore #define	  LPCR_SRTC_ENV		  (1u << 0)	/* RTC enabled/valid */
533a28d1b6SIan Lepore 
543a28d1b6SIan Lepore #define	SNVS_LPSRTCMR		0x50		/* Counter MSB */
553a28d1b6SIan Lepore #define	SNVS_LPSRTCLR		0x54		/* Counter LSB */
563a28d1b6SIan Lepore 
573a28d1b6SIan Lepore #define	RTC_RESOLUTION_US	(1000000 / 32768) /* 32khz clock */
583a28d1b6SIan Lepore 
593a28d1b6SIan Lepore /*
603a28d1b6SIan Lepore  * The RTC is a 47-bit counter clocked at 32KHz and organized as a 32.15
613a28d1b6SIan Lepore  * fixed-point binary value.  Shifting by SBT_LSB bits translates between
623a28d1b6SIan Lepore  * counter and sbintime values.
633a28d1b6SIan Lepore  */
643a28d1b6SIan Lepore #define	RTC_BITS	47
653a28d1b6SIan Lepore #define	SBT_BITS	64
663a28d1b6SIan Lepore #define	SBT_LSB		(SBT_BITS - RTC_BITS)
673a28d1b6SIan Lepore 
683a28d1b6SIan Lepore struct snvs_softc {
693a28d1b6SIan Lepore 	device_t 		dev;
700ffeeb41SIan Lepore 	struct syscon		*syscon;
713a28d1b6SIan Lepore 	uint32_t		lpcr;
723a28d1b6SIan Lepore };
733a28d1b6SIan Lepore 
743a28d1b6SIan Lepore static struct ofw_compat_data compat_data[] = {
75256c1bcaSIan Lepore 	{"fsl,sec-v4.0-mon-rtc-lp", true},
763a28d1b6SIan Lepore 	{NULL,               false}
773a28d1b6SIan Lepore };
783a28d1b6SIan Lepore 
793a28d1b6SIan Lepore static inline uint32_t
RD4(struct snvs_softc * sc,bus_size_t offset)803a28d1b6SIan Lepore RD4(struct snvs_softc *sc, bus_size_t offset)
813a28d1b6SIan Lepore {
823a28d1b6SIan Lepore 
830ffeeb41SIan Lepore 	return (SYSCON_READ_4(sc->syscon, offset));
843a28d1b6SIan Lepore }
853a28d1b6SIan Lepore 
863a28d1b6SIan Lepore static inline void
WR4(struct snvs_softc * sc,bus_size_t offset,uint32_t value)873a28d1b6SIan Lepore WR4(struct snvs_softc *sc, bus_size_t offset, uint32_t value)
883a28d1b6SIan Lepore {
893a28d1b6SIan Lepore 
900ffeeb41SIan Lepore 	SYSCON_WRITE_4(sc->syscon, offset, value);
913a28d1b6SIan Lepore }
923a28d1b6SIan Lepore 
933a28d1b6SIan Lepore static void
snvs_rtc_enable(struct snvs_softc * sc,bool enable)943a28d1b6SIan Lepore snvs_rtc_enable(struct snvs_softc *sc, bool enable)
953a28d1b6SIan Lepore {
963a28d1b6SIan Lepore 	uint32_t enbit;
973a28d1b6SIan Lepore 
983a28d1b6SIan Lepore 	if (enable)
993a28d1b6SIan Lepore 		sc->lpcr |= LPCR_SRTC_ENV;
1003a28d1b6SIan Lepore 	else
1013a28d1b6SIan Lepore 		sc->lpcr &= ~LPCR_SRTC_ENV;
1023a28d1b6SIan Lepore 	WR4(sc, SNVS_LPCR, sc->lpcr);
1033a28d1b6SIan Lepore 
1043a28d1b6SIan Lepore 	/* Wait for the hardware to achieve the requested state. */
1053a28d1b6SIan Lepore 	enbit = sc->lpcr & LPCR_SRTC_ENV;
1063a28d1b6SIan Lepore 	while ((RD4(sc, SNVS_LPCR) & LPCR_SRTC_ENV) != enbit)
1073a28d1b6SIan Lepore 		continue;
1083a28d1b6SIan Lepore }
1093a28d1b6SIan Lepore 
1103a28d1b6SIan Lepore static int
snvs_gettime(device_t dev,struct timespec * ts)1113a28d1b6SIan Lepore snvs_gettime(device_t dev, struct timespec *ts)
1123a28d1b6SIan Lepore {
1133a28d1b6SIan Lepore 	struct snvs_softc *sc;
1143a28d1b6SIan Lepore 	sbintime_t counter1, counter2;
1153a28d1b6SIan Lepore 
1163a28d1b6SIan Lepore 	sc = device_get_softc(dev);
1173a28d1b6SIan Lepore 
1183a28d1b6SIan Lepore 	/* If the clock is not enabled and valid, we can't help. */
1193a28d1b6SIan Lepore 	if (!(RD4(sc, SNVS_LPCR) & LPCR_SRTC_ENV)) {
1203a28d1b6SIan Lepore 		return (EINVAL);
1213a28d1b6SIan Lepore 	}
1223a28d1b6SIan Lepore 
1233a28d1b6SIan Lepore 	/*
1243a28d1b6SIan Lepore 	 * The counter is clocked asynchronously to cpu accesses; read and
1253a28d1b6SIan Lepore 	 * assemble the pieces of the counter until we get the same value twice.
1263a28d1b6SIan Lepore 	 * The counter is 47 bits, organized as a 32.15 binary fixed-point
1273a28d1b6SIan Lepore 	 * value. If we shift it up to the high order part of a 64-bit word it
1283a28d1b6SIan Lepore 	 * turns into an sbintime.
1293a28d1b6SIan Lepore 	 */
1303a28d1b6SIan Lepore 	do {
1313a28d1b6SIan Lepore 		counter1  = (uint64_t)RD4(sc, SNVS_LPSRTCMR) << (SBT_LSB + 32);
1323a28d1b6SIan Lepore 		counter1 |= (uint64_t)RD4(sc, SNVS_LPSRTCLR) << (SBT_LSB);
1333a28d1b6SIan Lepore 		counter2  = (uint64_t)RD4(sc, SNVS_LPSRTCMR) << (SBT_LSB + 32);
1343a28d1b6SIan Lepore 		counter2 |= (uint64_t)RD4(sc, SNVS_LPSRTCLR) << (SBT_LSB);
1353a28d1b6SIan Lepore 	} while (counter1 != counter2);
1363a28d1b6SIan Lepore 
1373a28d1b6SIan Lepore 	*ts = sbttots(counter1);
1383a28d1b6SIan Lepore 
139952dd10dSIan Lepore 	clock_dbgprint_ts(sc->dev, CLOCK_DBG_READ, ts);
140952dd10dSIan Lepore 
1413a28d1b6SIan Lepore 	return (0);
1423a28d1b6SIan Lepore }
1433a28d1b6SIan Lepore 
1443a28d1b6SIan Lepore static int
snvs_settime(device_t dev,struct timespec * ts)145952dd10dSIan Lepore snvs_settime(device_t dev, struct timespec *ts)
1463a28d1b6SIan Lepore {
1473a28d1b6SIan Lepore 	struct snvs_softc *sc;
1483a28d1b6SIan Lepore 	sbintime_t sbt;
1493a28d1b6SIan Lepore 
1503a28d1b6SIan Lepore 	sc = device_get_softc(dev);
1513a28d1b6SIan Lepore 
1523a28d1b6SIan Lepore 	/*
153952dd10dSIan Lepore 	 * The hardware format is the same as sbt (with fewer fractional bits),
154952dd10dSIan Lepore 	 * so first convert the time to sbt.  It takes two clock cycles for the
155952dd10dSIan Lepore 	 * counter to start after setting the enable bit, so add two SBT_LSBs to
156952dd10dSIan Lepore 	 * what we're about to set.
1573a28d1b6SIan Lepore 	 */
158952dd10dSIan Lepore 	sbt = tstosbt(*ts);
1593a28d1b6SIan Lepore 	sbt += 2 << SBT_LSB;
1603a28d1b6SIan Lepore 	snvs_rtc_enable(sc, false);
1613a28d1b6SIan Lepore 	WR4(sc, SNVS_LPSRTCMR, (uint32_t)(sbt >> (SBT_LSB + 32)));
1623a28d1b6SIan Lepore 	WR4(sc, SNVS_LPSRTCLR, (uint32_t)(sbt >> (SBT_LSB)));
1633a28d1b6SIan Lepore 	snvs_rtc_enable(sc, true);
1643a28d1b6SIan Lepore 
165952dd10dSIan Lepore 	clock_dbgprint_ts(sc->dev, CLOCK_DBG_WRITE, ts);
166952dd10dSIan Lepore 
1673a28d1b6SIan Lepore 	return (0);
1683a28d1b6SIan Lepore }
1693a28d1b6SIan Lepore 
1703a28d1b6SIan Lepore static int
snvs_probe(device_t dev)1713a28d1b6SIan Lepore snvs_probe(device_t dev)
1723a28d1b6SIan Lepore {
1733a28d1b6SIan Lepore 
1743a28d1b6SIan Lepore 	if (!ofw_bus_status_okay(dev))
1753a28d1b6SIan Lepore 		return (ENXIO);
1763a28d1b6SIan Lepore 
1773a28d1b6SIan Lepore 	if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data)
1783a28d1b6SIan Lepore 		return (ENXIO);
1793a28d1b6SIan Lepore 
1803a28d1b6SIan Lepore 	device_set_desc(dev, "i.MX6 SNVS RTC");
1813a28d1b6SIan Lepore 	return (BUS_PROBE_DEFAULT);
1823a28d1b6SIan Lepore }
1833a28d1b6SIan Lepore 
1843a28d1b6SIan Lepore static int
snvs_attach(device_t dev)1853a28d1b6SIan Lepore snvs_attach(device_t dev)
1863a28d1b6SIan Lepore {
1873a28d1b6SIan Lepore 	struct snvs_softc *sc;
1883a28d1b6SIan Lepore 
1893a28d1b6SIan Lepore 	sc = device_get_softc(dev);
1903a28d1b6SIan Lepore 	sc->dev = dev;
1913a28d1b6SIan Lepore 
1920ffeeb41SIan Lepore 	if (syscon_get_handle_default(sc->dev, &sc->syscon) != 0) {
1930ffeeb41SIan Lepore 		device_printf(sc->dev, "Cannot get syscon handle\n");
1943a28d1b6SIan Lepore 		return (ENXIO);
1953a28d1b6SIan Lepore 	}
1963a28d1b6SIan Lepore 
1973a28d1b6SIan Lepore 	clock_register(sc->dev, RTC_RESOLUTION_US);
1983a28d1b6SIan Lepore 
1993a28d1b6SIan Lepore 	return (0);
2003a28d1b6SIan Lepore }
2013a28d1b6SIan Lepore 
2029b4738cfSIan Lepore static int
snvs_detach(device_t dev)2039b4738cfSIan Lepore snvs_detach(device_t dev)
2049b4738cfSIan Lepore {
2059b4738cfSIan Lepore 	struct snvs_softc *sc;
2069b4738cfSIan Lepore 
2079b4738cfSIan Lepore 	sc = device_get_softc(dev);
2089b4738cfSIan Lepore 	clock_unregister(sc->dev);
2099b4738cfSIan Lepore 	return (0);
2109b4738cfSIan Lepore }
2119b4738cfSIan Lepore 
2123a28d1b6SIan Lepore static device_method_t snvs_methods[] = {
2133a28d1b6SIan Lepore 	DEVMETHOD(device_probe,		snvs_probe),
2143a28d1b6SIan Lepore 	DEVMETHOD(device_attach,	snvs_attach),
2159b4738cfSIan Lepore 	DEVMETHOD(device_detach,	snvs_detach),
2163a28d1b6SIan Lepore 
2173a28d1b6SIan Lepore 	/* clock_if methods */
2183a28d1b6SIan Lepore 	DEVMETHOD(clock_gettime,	snvs_gettime),
2193a28d1b6SIan Lepore 	DEVMETHOD(clock_settime,	snvs_settime),
2203a28d1b6SIan Lepore 
2213a28d1b6SIan Lepore 	DEVMETHOD_END
2223a28d1b6SIan Lepore };
2233a28d1b6SIan Lepore 
2243a28d1b6SIan Lepore static driver_t snvs_driver = {
2253a28d1b6SIan Lepore 	"snvs",
2263a28d1b6SIan Lepore 	snvs_methods,
2273a28d1b6SIan Lepore 	sizeof(struct snvs_softc),
2283a28d1b6SIan Lepore };
2293a28d1b6SIan Lepore 
230*ea538dabSJohn Baldwin DRIVER_MODULE(snvs, simplebus, snvs_driver, 0, 0);
2319b4738cfSIan Lepore SIMPLEBUS_PNP_INFO(compat_data);
232