Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/arch/arm/sunxi Add sun7i RTC support.
details: https://anonhg.NetBSD.org/src/rev/498fc261e112
branches: trunk
changeset: 826955:498fc261e112
user: jmcneill <jmcneill%NetBSD.org@localhost>
date: Sat Oct 07 17:03:49 2017 +0000
description:
Add sun7i RTC support.
diffstat:
sys/arch/arm/sunxi/sunxi_rtc.c | 168 ++++++++++++++++++++++++++++++----------
1 files changed, 127 insertions(+), 41 deletions(-)
diffs (250 lines):
diff -r 72d69397b5c6 -r 498fc261e112 sys/arch/arm/sunxi/sunxi_rtc.c
--- a/sys/arch/arm/sunxi/sunxi_rtc.c Sat Oct 07 16:44:24 2017 +0000
+++ b/sys/arch/arm/sunxi/sunxi_rtc.c Sat Oct 07 17:03:49 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sunxi_rtc.c,v 1.1 2017/06/29 19:38:24 jmcneill Exp $ */
+/* $NetBSD: sunxi_rtc.c,v 1.2 2017/10/07 17:03:49 jmcneill Exp $ */
/*-
* Copyright (c) 2014-2017 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -27,7 +27,7 @@
*/
#include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: sunxi_rtc.c,v 1.1 2017/06/29 19:38:24 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunxi_rtc.c,v 1.2 2017/10/07 17:03:49 jmcneill Exp $");
#include <sys/param.h>
#include <sys/bus.h>
@@ -40,22 +40,39 @@
#include <dev/fdt/fdtvar.h>
-#define RTC_YY_MM_DD_REG 0x10
-#define SUNXI_RTC_LEAP __BIT(22)
-#define SUNXI_RTC_YEAR __BITS(21,16)
-#define SUNXI_RTC_MONTH __BITS(11,8)
-#define SUNXI_RTC_DAY __BITS(4,0)
-#define RTC_HH_MM_SS_REG 0x14
-#define SUNXI_RTC_WK_NO __BITS(31,29)
-#define SUNXI_RTC_HOUR __BITS(20,16)
-#define SUNXI_RTC_MINUTE __BITS(13,8)
-#define SUNXI_RTC_SECOND __BITS(5,0)
+#define SUN7I_RTC_YY_MM_DD_REG 0x04
+#define SUN7I_RTC_LEAP __BIT(24)
+#define SUN7I_RTC_YEAR __BITS(23,16)
+#define SUN7I_RTC_MONTH __BITS(11,8)
+#define SUN7I_RTC_DAY __BITS(4,0)
+#define SUN7I_RTC_HH_MM_SS_REG 0x08
+#define SUN7I_RTC_WK_NO __BITS(31,29)
+#define SUN7I_RTC_HOUR __BITS(20,16)
+#define SUN7I_RTC_MINUTE __BITS(13,8)
+#define SUN7I_RTC_SECOND __BITS(5,0)
+
+#define SUN6I_RTC_YY_MM_DD_REG 0x10
+#define SUN6I_RTC_LEAP __BIT(22)
+#define SUN6I_RTC_YEAR __BITS(21,16)
+#define SUN6I_RTC_MONTH __BITS(11,8)
+#define SUN6I_RTC_DAY __BITS(4,0)
+#define SUN6I_RTC_HH_MM_SS_REG 0x14
+#define SUN6I_RTC_WK_NO __BITS(31,29)
+#define SUN6I_RTC_HOUR __BITS(20,16)
+#define SUN6I_RTC_MINUTE __BITS(13,8)
+#define SUN6I_RTC_SECOND __BITS(5,0)
#define SUNXI_RTC_BASE_YEAR 2000
-static const char * const compatible[] = {
- "allwinner,sun6i-a31-rtc",
- NULL
+enum sunxi_rtc_type {
+ RTC_SUN6I = 1,
+ RTC_SUN7I,
+};
+
+static const struct of_compat_data compat_data[] = {
+ { "allwinner,sun6i-a31-rtc", RTC_SUN6I },
+ { "allwinner,sun7i-a20-rtc", RTC_SUN7I },
+ { NULL }
};
struct sunxi_rtc_softc {
@@ -73,8 +90,11 @@
static int sunxi_rtc_match(device_t, cfdata_t, void *);
static void sunxi_rtc_attach(device_t, device_t, void *);
-static int sunxi_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
-static int sunxi_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
+static int sun6i_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
+static int sun6i_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
+
+static int sun7i_rtc_gettime(todr_chip_handle_t, struct clock_ymdhms *);
+static int sun7i_rtc_settime(todr_chip_handle_t, struct clock_ymdhms *);
CFATTACH_DECL_NEW(sunxi_rtc, sizeof(struct sunxi_rtc_softc),
sunxi_rtc_match, sunxi_rtc_attach, NULL, NULL);
@@ -84,7 +104,7 @@
{
struct fdt_attach_args * const faa = aux;
- return of_match_compatible(faa->faa_phandle, compatible);
+ return of_match_compat_data(faa->faa_phandle, compat_data);
}
static void
@@ -93,6 +113,7 @@
struct sunxi_rtc_softc * const sc = device_private(self);
struct fdt_attach_args * const faa = aux;
const int phandle = faa->faa_phandle;
+ enum sunxi_rtc_type type;
bus_addr_t addr;
bus_size_t size;
@@ -111,34 +132,44 @@
aprint_naive("\n");
aprint_normal(": RTC\n");
- sc->sc_todr.todr_gettime_ymdhms = sunxi_rtc_gettime;
- sc->sc_todr.todr_settime_ymdhms = sunxi_rtc_settime;
+ type = of_search_compatible(phandle, compat_data)->data;
+
sc->sc_todr.cookie = sc;
+ switch (type) {
+ case RTC_SUN6I:
+ sc->sc_todr.todr_gettime_ymdhms = sun6i_rtc_gettime;
+ sc->sc_todr.todr_settime_ymdhms = sun6i_rtc_settime;
+ break;
+ case RTC_SUN7I:
+ sc->sc_todr.todr_gettime_ymdhms = sun7i_rtc_gettime;
+ sc->sc_todr.todr_settime_ymdhms = sun7i_rtc_settime;
+ break;
+ }
fdtbus_todr_attach(self, phandle, &sc->sc_todr);
}
static int
-sunxi_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
+sun6i_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
{
struct sunxi_rtc_softc *sc = tch->cookie;
- const uint32_t yymmdd = RTC_READ(sc, RTC_YY_MM_DD_REG);
- const uint32_t hhmmss = RTC_READ(sc, RTC_HH_MM_SS_REG);
+ const uint32_t yymmdd = RTC_READ(sc, SUN6I_RTC_YY_MM_DD_REG);
+ const uint32_t hhmmss = RTC_READ(sc, SUN6I_RTC_HH_MM_SS_REG);
- dt->dt_year = __SHIFTOUT(yymmdd, SUNXI_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
- dt->dt_mon = __SHIFTOUT(yymmdd, SUNXI_RTC_MONTH);
- dt->dt_day = __SHIFTOUT(yymmdd, SUNXI_RTC_DAY);
- dt->dt_wday = __SHIFTOUT(hhmmss, SUNXI_RTC_WK_NO);
- dt->dt_hour = __SHIFTOUT(hhmmss, SUNXI_RTC_HOUR);
- dt->dt_min = __SHIFTOUT(hhmmss, SUNXI_RTC_MINUTE);
- dt->dt_sec = __SHIFTOUT(hhmmss, SUNXI_RTC_SECOND);
+ dt->dt_year = __SHIFTOUT(yymmdd, SUN6I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
+ dt->dt_mon = __SHIFTOUT(yymmdd, SUN6I_RTC_MONTH);
+ dt->dt_day = __SHIFTOUT(yymmdd, SUN6I_RTC_DAY);
+ dt->dt_wday = __SHIFTOUT(hhmmss, SUN6I_RTC_WK_NO);
+ dt->dt_hour = __SHIFTOUT(hhmmss, SUN6I_RTC_HOUR);
+ dt->dt_min = __SHIFTOUT(hhmmss, SUN6I_RTC_MINUTE);
+ dt->dt_sec = __SHIFTOUT(hhmmss, SUN6I_RTC_SECOND);
return 0;
}
static int
-sunxi_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
+sun6i_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
{
struct sunxi_rtc_softc *sc = tch->cookie;
uint32_t yymmdd, hhmmss, maxyear;
@@ -151,24 +182,79 @@
"not writing back time\n", dt->dt_year);
return EIO;
}
- maxyear = __SHIFTOUT(0xffffffff, SUNXI_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
+ maxyear = __SHIFTOUT(0xffffffff, SUN6I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
if (dt->dt_year > maxyear) {
aprint_normal_dev(sc->sc_dev, "year exceeds available field:"
" %llu, not writing back time\n", dt->dt_year);
return EIO;
}
- yymmdd = __SHIFTIN(dt->dt_year - SUNXI_RTC_BASE_YEAR, SUNXI_RTC_YEAR);
- yymmdd |= __SHIFTIN(dt->dt_mon, SUNXI_RTC_MONTH);
- yymmdd |= __SHIFTIN(dt->dt_day, SUNXI_RTC_DAY);
+ yymmdd = __SHIFTIN(dt->dt_year - SUNXI_RTC_BASE_YEAR, SUN6I_RTC_YEAR);
+ yymmdd |= __SHIFTIN(dt->dt_mon, SUN6I_RTC_MONTH);
+ yymmdd |= __SHIFTIN(dt->dt_day, SUN6I_RTC_DAY);
+
+ hhmmss = __SHIFTIN(dt->dt_wday, SUN6I_RTC_WK_NO);
+ hhmmss |= __SHIFTIN(dt->dt_hour, SUN6I_RTC_HOUR);
+ hhmmss |= __SHIFTIN(dt->dt_min, SUN6I_RTC_MINUTE);
+ hhmmss |= __SHIFTIN(dt->dt_sec, SUN6I_RTC_SECOND);
+
+ RTC_WRITE(sc, SUN6I_RTC_YY_MM_DD_REG, yymmdd);
+ RTC_WRITE(sc, SUN6I_RTC_HH_MM_SS_REG, hhmmss);
+
+ return 0;
+}
- hhmmss = __SHIFTIN(dt->dt_wday, SUNXI_RTC_WK_NO);
- hhmmss |= __SHIFTIN(dt->dt_hour, SUNXI_RTC_HOUR);
- hhmmss |= __SHIFTIN(dt->dt_min, SUNXI_RTC_MINUTE);
- hhmmss |= __SHIFTIN(dt->dt_sec, SUNXI_RTC_SECOND);
+static int
+sun7i_rtc_gettime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
+{
+ struct sunxi_rtc_softc *sc = tch->cookie;
+
+ const uint32_t yymmdd = RTC_READ(sc, SUN7I_RTC_YY_MM_DD_REG);
+ const uint32_t hhmmss = RTC_READ(sc, SUN7I_RTC_HH_MM_SS_REG);
- RTC_WRITE(sc, RTC_YY_MM_DD_REG, yymmdd);
- RTC_WRITE(sc, RTC_HH_MM_SS_REG, hhmmss);
+ dt->dt_year = __SHIFTOUT(yymmdd, SUN7I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
+ dt->dt_mon = __SHIFTOUT(yymmdd, SUN7I_RTC_MONTH);
+ dt->dt_day = __SHIFTOUT(yymmdd, SUN7I_RTC_DAY);
+ dt->dt_wday = __SHIFTOUT(hhmmss, SUN7I_RTC_WK_NO);
+ dt->dt_hour = __SHIFTOUT(hhmmss, SUN7I_RTC_HOUR);
+ dt->dt_min = __SHIFTOUT(hhmmss, SUN7I_RTC_MINUTE);
+ dt->dt_sec = __SHIFTOUT(hhmmss, SUN7I_RTC_SECOND);
return 0;
}
+
+static int
+sun7i_rtc_settime(todr_chip_handle_t tch, struct clock_ymdhms *dt)
+{
+ struct sunxi_rtc_softc *sc = tch->cookie;
+ uint32_t yymmdd, hhmmss, maxyear;
+
+ /*
+ * Sanity check the date before writing it back
+ */
+ if (dt->dt_year < SUNXI_RTC_BASE_YEAR) {
+ aprint_normal_dev(sc->sc_dev, "year pre the epoch: %llu, "
+ "not writing back time\n", dt->dt_year);
+ return EIO;
+ }
+ maxyear = __SHIFTOUT(0xffffffff, SUN7I_RTC_YEAR) + SUNXI_RTC_BASE_YEAR;
+ if (dt->dt_year > maxyear) {
+ aprint_normal_dev(sc->sc_dev, "year exceeds available field:"
+ " %llu, not writing back time\n", dt->dt_year);
+ return EIO;
+ }
+
+ yymmdd = __SHIFTIN(dt->dt_year - SUNXI_RTC_BASE_YEAR, SUN7I_RTC_YEAR);
+ yymmdd |= __SHIFTIN(dt->dt_mon, SUN7I_RTC_MONTH);
+ yymmdd |= __SHIFTIN(dt->dt_day, SUN7I_RTC_DAY);
+
+ hhmmss = __SHIFTIN(dt->dt_wday, SUN7I_RTC_WK_NO);
+ hhmmss |= __SHIFTIN(dt->dt_hour, SUN7I_RTC_HOUR);
+ hhmmss |= __SHIFTIN(dt->dt_min, SUN7I_RTC_MINUTE);
+ hhmmss |= __SHIFTIN(dt->dt_sec, SUN7I_RTC_SECOND);
+
+ RTC_WRITE(sc, SUN7I_RTC_YY_MM_DD_REG, yymmdd);
+ RTC_WRITE(sc, SUN7I_RTC_HH_MM_SS_REG, hhmmss);
+
+ return 0;
+}
Home |
Main Index |
Thread Index |
Old Index