Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/netbsd-7]: src/sys/arch Pull up following revision(s) (requested by jmcn...



details:   https://anonhg.NetBSD.org/src/rev/a558444a03e6
branches:  netbsd-7
changeset: 799139:a558444a03e6
user:      snj <snj%NetBSD.org@localhost>
date:      Wed Mar 25 17:13:25 2015 +0000

description:
Pull up following revision(s) (requested by jmcneill in ticket #639):
        sys/arch/arm/amlogic/amlogic_intr.h: revision 1.6
        sys/arch/arm/amlogic/amlogic_io.c: revision 1.8
        sys/arch/arm/amlogic/amlogic_reg.h: revision 1.10
        sys/arch/arm/amlogic/amlogic_rtc.c: revision 1.1
        sys/arch/arm/amlogic/amlogic_rtcreg.h: revision 1.1
        sys/arch/arm/amlogic/files.amlogic: revision 1.9
        sys/arch/evbarm/conf/ODROID-C1: revision 1.13
Add Amlogic RTC driver, from Anon Ymous

diffstat:

 sys/arch/arm/amlogic/amlogic_intr.h   |    3 +-
 sys/arch/arm/amlogic/amlogic_io.c     |    6 +-
 sys/arch/arm/amlogic/amlogic_reg.h    |    5 +-
 sys/arch/arm/amlogic/amlogic_rtc.c    |  370 ++++++++++++++++++++++++++++++++++
 sys/arch/arm/amlogic/amlogic_rtcreg.h |  101 +++++++++
 sys/arch/arm/amlogic/files.amlogic    |    7 +-
 sys/arch/evbarm/conf/ODROID-C1        |    5 +-
 7 files changed, 491 insertions(+), 6 deletions(-)

diffs (truncated from 583 to 300 lines):

diff -r 2162072add3d -r a558444a03e6 sys/arch/arm/amlogic/amlogic_intr.h
--- a/sys/arch/arm/amlogic/amlogic_intr.h       Wed Mar 25 17:09:59 2015 +0000
+++ b/sys/arch/arm/amlogic/amlogic_intr.h       Wed Mar 25 17:13:25 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: amlogic_intr.h,v 1.5.2.2 2015/03/21 08:51:17 snj Exp $ */
+/* $NetBSD: amlogic_intr.h,v 1.5.2.3 2015/03/25 17:13:25 snj Exp $ */
 
 /*-
  * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -40,6 +40,7 @@
 #define AMLOGIC_INTR_USB0      62
 #define AMLOGIC_INTR_USB1      63
 #define AMLOGIC_INTR_UART2AO   70
+#define AMLOGIC_INTR_RTC       104
 #define AMLOGIC_INTR_SDHC      110
 #define AMLOGIC_INTR_UART0AO   122
 
diff -r 2162072add3d -r a558444a03e6 sys/arch/arm/amlogic/amlogic_io.c
--- a/sys/arch/arm/amlogic/amlogic_io.c Wed Mar 25 17:09:59 2015 +0000
+++ b/sys/arch/arm/amlogic/amlogic_io.c Wed Mar 25 17:13:25 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: amlogic_io.c,v 1.7.2.2 2015/03/21 08:51:17 snj Exp $ */
+/* $NetBSD: amlogic_io.c,v 1.7.2.3 2015/03/25 17:13:25 snj Exp $ */
 
 /*-
  * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -29,7 +29,7 @@
 #include "opt_amlogic.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: amlogic_io.c,v 1.7.2.2 2015/03/21 08:51:17 snj Exp $");
+__KERNEL_RCSID(0, "$NetBSD: amlogic_io.c,v 1.7.2.3 2015/03/25 17:13:25 snj Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -77,6 +77,8 @@
     AMLOGIC_SDHC_OFFSET, AMLOGIC_SDHC_SIZE, 1, AMLOGIC_INTR_SDHC },
   { "amlogicsdhc",
     AMLOGIC_SDHC_OFFSET, AMLOGIC_SDHC_SIZE, 2, AMLOGIC_INTR_SDHC },
+  { "amlogicrtc",
+    AMLOGIC_RTC_OFFSET, AMLOGIC_RTC_SIZE, NOPORT, AMLOGIC_INTR_RTC },
 };
 
 int
diff -r 2162072add3d -r a558444a03e6 sys/arch/arm/amlogic/amlogic_reg.h
--- a/sys/arch/arm/amlogic/amlogic_reg.h        Wed Mar 25 17:09:59 2015 +0000
+++ b/sys/arch/arm/amlogic/amlogic_reg.h        Wed Mar 25 17:13:25 2015 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: amlogic_reg.h,v 1.9.2.2 2015/03/21 08:51:17 snj Exp $ */
+/* $NetBSD: amlogic_reg.h,v 1.9.2.3 2015/03/25 17:13:25 snj Exp $ */
 
 /*-
  * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -63,6 +63,9 @@
 #define AMLOGIC_UART_SIZE      0x20
 #define AMLOGIC_UART_FREQ      AMLOGIC_REF_FREQ
 
+#define AMLOGIC_RTC_OFFSET     0x08100740
+#define AMLOGIC_RTC_SIZE       0x14
+
 #define AMLOGIC_USB0_OFFSET    0x09040000
 #define AMLOGIC_USB1_OFFSET    0x090c0000
 #define AMLOGIC_USB_SIZE       0x40000
diff -r 2162072add3d -r a558444a03e6 sys/arch/arm/amlogic/amlogic_rtc.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/amlogic/amlogic_rtc.c        Wed Mar 25 17:13:25 2015 +0000
@@ -0,0 +1,370 @@
+/* $NetBSD: amlogic_rtc.c,v 1.1.2.2 2015/03/25 17:13:25 snj Exp $ */
+
+/*-
+ * Copyright (c) 2015 The NetBSD Foundation, Inc.
+ * All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
+ * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
+ * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
+ * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
+ * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: amlogic_rtc.c,v 1.1.2.2 2015/03/25 17:13:25 snj Exp $");
+
+#include <sys/param.h>
+#include <sys/atomic.h>
+#include <sys/device.h>
+#include <sys/kernel.h>
+#include <sys/systm.h>
+
+#include <dev/clock_subr.h>
+
+#include <arm/amlogic/amlogic_reg.h>
+#include <arm/amlogic/amlogic_rtcreg.h>
+#include <arm/amlogic/amlogic_var.h>
+
+#define RESET_RETRY_TIMES      3
+#define RTC_COMM_DELAY         5
+#define RTC_RESET_DELAY                100
+#define RTC_STATIC_VALUE_INIT  0x180a  /* XXX: MAGIC? */
+
+struct amlogic_rtc_softc {
+       device_t                sc_dev;
+       bus_space_tag_t         sc_bst;
+       bus_space_handle_t      sc_bsh;
+       struct todr_chip_handle sc_todr;
+       int                     sc_osc_failed;
+       unsigned int            sc_busy;
+};
+
+static int amlogic_rtc_match(device_t, cfdata_t, void *);
+static void amlogic_rtc_attach(device_t, device_t, void *);
+static int amlogic_rtc_todr_gettime(todr_chip_handle_t, struct timeval *);
+static int amlogic_rtc_todr_settime(todr_chip_handle_t, struct timeval *);
+
+CFATTACH_DECL_NEW(amlogic_rtc, sizeof(struct amlogic_rtc_softc),
+       amlogic_rtc_match, amlogic_rtc_attach, NULL, NULL);
+
+#define RTC_WRITE(sc, reg, val) \
+       bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
+#define RTC_READ(sc, reg) \
+       bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
+
+static inline void
+setbits(struct amlogic_rtc_softc *sc, uint32_t reg, uint32_t bits)
+{
+
+       RTC_WRITE(sc, reg, RTC_READ(sc, reg) | bits);
+}
+
+static inline void
+clrbits(struct amlogic_rtc_softc *sc, uint32_t reg, uint32_t bits)
+{
+
+       RTC_WRITE(sc, reg, RTC_READ(sc, reg) & ~bits);
+}
+
+static int
+amlogic_rtc_check_osc_clk(struct amlogic_rtc_softc *sc)
+{
+       uint32_t cnt1, cnt2;
+
+       setbits(sc, AO_RTC_REG3, AO_RTC_REG3_COUNT_ALWAYS);
+
+       /*
+        * Wait for 50uS.  32.768khz is 30.5uS.  This should be long
+        * enough for one full cycle of 32.768 khz.
+        */
+       cnt1 = RTC_READ(sc, AO_RTC_REG2);
+       delay(50);
+       cnt2 = RTC_READ(sc, AO_RTC_REG2);
+
+       clrbits(sc, AO_RTC_REG3, AO_RTC_REG3_COUNT_ALWAYS);
+
+       return cnt1 == cnt2;
+}
+
+static int
+amlogic_rtc_match(device_t parent, cfdata_t cf, void *aux)
+{
+       return 1;
+}
+
+static void
+amlogic_rtc_attach(device_t parent, device_t self, void *aux)
+{
+       struct amlogic_rtc_softc * const sc = device_private(self);
+       struct amlogicio_attach_args * const aio = aux;
+       const struct amlogic_locators * const loc = &aio->aio_loc;
+
+       sc->sc_dev = self;
+       sc->sc_bst = aio->aio_core_bst;
+       bus_space_subregion(aio->aio_core_bst, aio->aio_bsh,
+           loc->loc_offset, loc->loc_size, &sc->sc_bsh);
+
+       sc->sc_osc_failed = amlogic_rtc_check_osc_clk(sc);
+
+       memset(&sc->sc_todr, 0, sizeof(sc->sc_todr));
+       sc->sc_todr.cookie = sc;
+       sc->sc_todr.todr_gettime = amlogic_rtc_todr_gettime;
+       sc->sc_todr.todr_settime = amlogic_rtc_todr_settime;
+
+       aprint_naive("\n");
+       aprint_normal(": RTC");
+       if (sc->sc_osc_failed) {
+               aprint_error(" battery not present or discharged\n");
+       } else {
+               aprint_normal("\n");
+               todr_attach(&sc->sc_todr);
+       }
+}
+
+static void
+amlogic_rtc_sclk_pulse(struct amlogic_rtc_softc *sc)
+{
+
+       delay(RTC_COMM_DELAY);
+       setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SCLK);
+
+       delay(RTC_COMM_DELAY);
+       clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SCLK);
+}
+
+static void
+amlogic_rtc_send_bit(struct amlogic_rtc_softc *sc, uint32_t bitset)
+{
+
+       if (bitset)
+               setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
+       else
+               clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
+
+       amlogic_rtc_sclk_pulse(sc);
+}
+
+#define SERIAL_ADDR_BITS       3
+#define SERIAL_DATA_BITS       32
+#define        SERIAL_TYPE_ADDR        (1 << (SERIAL_ADDR_BITS - 1))
+#define        SERIAL_TYPE_DATA        (1 << (SERIAL_DATA_BITS - 1))
+
+static void
+amlogic_rtc_send_data(struct amlogic_rtc_softc *sc,
+    uint32_t nextbit, uint32_t data)
+{
+
+       KASSERT(nextbit == SERIAL_TYPE_ADDR || nextbit == SERIAL_TYPE_DATA);
+
+       while (nextbit) {
+               amlogic_rtc_send_bit(sc, data & nextbit);
+               nextbit >>= 1;
+       }
+}
+
+static uint32_t
+amlogic_rtc_get_data(struct amlogic_rtc_softc *sc)
+{
+       uint32_t data;
+       size_t i;
+
+       data = 0;
+       for (i = 0; i < SERIAL_DATA_BITS; i++) {
+               amlogic_rtc_sclk_pulse(sc);
+               data <<= 1;
+               data |= __SHIFTOUT(RTC_READ(sc, AO_RTC_REG1), AO_RTC_REG1_SDO);
+       }
+       return data;
+}
+
+enum serial_mode {
+       SERIAL_MODE_READ,
+       SERIAL_MODE_WRITE,
+};
+
+static void
+amlogic_rtc_set_mode(struct amlogic_rtc_softc *sc, enum serial_mode mode)
+{
+
+       clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SEN);
+
+       switch(mode) {
+       case SERIAL_MODE_READ:
+               clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
+               break;
+       case SERIAL_MODE_WRITE:
+               setbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
+               break;
+       default:
+               KASSERT(1);
+               return;
+       }
+       amlogic_rtc_sclk_pulse(sc);
+
+       clrbits(sc, AO_RTC_REG0, AO_RTC_REG0_SDI);
+}
+
+static int
+amlogic_rtc_wait_s_ready(struct amlogic_rtc_softc *sc)
+{
+       size_t s_nrdy_cnt, retry_cnt;
+
+       s_nrdy_cnt = 40000;
+       retry_cnt = 0;
+       while (!(RTC_READ(sc, AO_RTC_REG1) & AO_RTC_REG1_S_READY)) {
+               if (s_nrdy_cnt-- == 0) {
+                       s_nrdy_cnt = 40000;
+                       if (retry_cnt++ == RESET_RETRY_TIMES)



Home | Main Index | Thread Index | Old Index