Source-Changes-HG archive

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

[src/trunk]: src/sys/arch/arm/rockchip add I2C driver



details:   https://anonhg.NetBSD.org/src/rev/41ddb7ca5828
branches:  trunk
changeset: 335225:41ddb7ca5828
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Tue Dec 30 17:15:31 2014 +0000

description:
add I2C driver

diffstat:

 sys/arch/arm/rockchip/files.rockchip    |    9 +-
 sys/arch/arm/rockchip/obio.c            |   10 +-
 sys/arch/arm/rockchip/rockchip_board.c  |   40 +++-
 sys/arch/arm/rockchip/rockchip_i2c.c    |  369 ++++++++++++++++++++++++++++++++
 sys/arch/arm/rockchip/rockchip_i2creg.h |   82 +++++++
 sys/arch/arm/rockchip/rockchip_var.h    |    6 +-
 6 files changed, 508 insertions(+), 8 deletions(-)

diffs (truncated from 641 to 300 lines):

diff -r c6263f58ef71 -r 41ddb7ca5828 sys/arch/arm/rockchip/files.rockchip
--- a/sys/arch/arm/rockchip/files.rockchip      Tue Dec 30 12:42:16 2014 +0000
+++ b/sys/arch/arm/rockchip/files.rockchip      Tue Dec 30 17:15:31 2014 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.rockchip,v 1.3 2014/12/27 16:18:50 jmcneill Exp $
+#      $NetBSD: files.rockchip,v 1.4 2014/12/30 17:15:31 jmcneill Exp $
 #
 # Configuration info for Rockchip ARM Peripherals
 #
@@ -18,7 +18,7 @@
 file   arch/arm/arm/bus_space_a4x.S            obio
 
 # On-board I/O
-device obio {[addr = -1], [size = -1], [width = -1], [intr = -1], [mult = 1]}: bus_space_generic
+device obio {[addr = -1], [size = -1], [width = -1], [intr = -1], [mult = 1], [port = -1]}: bus_space_generic
 attach obio at mainbus
 file arch/arm/rockchip/obio.c          obio
 
@@ -26,6 +26,11 @@
 attach com at obio with obiouart
 file   arch/arm/rockchip/obio_com.c    obiouart
 
+# I2C controller
+device rkiic: i2cbus, i2cexec
+attach rkiic at obio
+file   arch/arm/rockchip/rockchip_i2c.c        rkiic
+
 # SD/MMC (Synopsys DesignWare)
 attach dwcmmc at obio with rkdwcmmc
 file   arch/arm/rockchip/rockchip_dwcmmc.c     rkdwcmmc
diff -r c6263f58ef71 -r 41ddb7ca5828 sys/arch/arm/rockchip/obio.c
--- a/sys/arch/arm/rockchip/obio.c      Tue Dec 30 12:42:16 2014 +0000
+++ b/sys/arch/arm/rockchip/obio.c      Tue Dec 30 17:15:31 2014 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: obio.c,v 1.4 2014/12/30 12:38:20 jmcneill Exp $        */
+/*     $NetBSD: obio.c,v 1.5 2014/12/30 17:15:31 jmcneill Exp $        */
 
 /*
  * Copyright (c) 2001, 2002, 2003 Wasabi Systems, Inc.
@@ -38,7 +38,7 @@
 #include "opt_rockchip.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.4 2014/12/30 12:38:20 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: obio.c,v 1.5 2014/12/30 17:15:31 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/systm.h>
@@ -120,6 +120,8 @@
                aprint_normal(" intr %d", obio->obio_intr);
        if (obio->obio_mult != OBIOCF_MULT_DEFAULT)
                aprint_normal(" mult %d", obio->obio_mult);
+       if (obio->obio_port != OBIOCF_PORT_DEFAULT)
+               aprint_normal(" port %d", obio->obio_port);
 
        return UNCONF;
 }
@@ -147,6 +149,7 @@
        obio.obio_width = cf->cf_loc[OBIOCF_WIDTH];
        obio.obio_intr = cf->cf_loc[OBIOCF_INTR];
        obio.obio_mult = cf->cf_loc[OBIOCF_MULT];
+       obio.obio_port = cf->cf_loc[OBIOCF_PORT];
        obio.obio_dmat = &rockchip_bus_dma_tag;
 
        switch (cf->cf_loc[OBIOCF_MULT]) {
@@ -249,10 +252,11 @@
 obio_dump_clocks(void)
 {
        printf("APLL: %u Hz\n", rockchip_apll_get_rate());
+       printf("CPLL: %u Hz\n", rockchip_cpll_get_rate());
        printf("GPLL: %u Hz\n", rockchip_gpll_get_rate());
        printf("CPU: %u Hz\n", rockchip_cpu_get_rate());
        printf("AHB: %u Hz\n", rockchip_ahb_get_rate());
+       printf("APB: %u Hz\n", rockchip_apb_get_rate());
        printf("A9PERIPH: %u Hz\n", rockchip_a9periph_get_rate());
-       printf("MMC0: %u Hz\n", rockchip_mmc0_get_rate());
 }
 #endif
diff -r c6263f58ef71 -r 41ddb7ca5828 sys/arch/arm/rockchip/rockchip_board.c
--- a/sys/arch/arm/rockchip/rockchip_board.c    Tue Dec 30 12:42:16 2014 +0000
+++ b/sys/arch/arm/rockchip/rockchip_board.c    Tue Dec 30 17:15:31 2014 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: rockchip_board.c,v 1.6 2014/12/30 03:53:52 jmcneill Exp $ */
+/* $NetBSD: rockchip_board.c,v 1.7 2014/12/30 17:15:31 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2014 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -29,7 +29,7 @@
 #include "opt_rockchip.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: rockchip_board.c,v 1.6 2014/12/30 03:53:52 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: rockchip_board.c,v 1.7 2014/12/30 17:15:31 jmcneill Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -115,6 +115,12 @@
 }
 
 u_int
+rockchip_cpll_get_rate(void)
+{
+       return rockchip_pll_get_rate(CRU_CPLL_CON0_REG, CRU_CPLL_CON1_REG);
+}
+
+u_int
 rockchip_apll_get_rate(void)
 {
        return rockchip_pll_get_rate(CRU_APLL_CON0_REG, CRU_APLL_CON1_REG);
@@ -364,6 +370,26 @@
 }
 
 u_int
+rockchip_apb_get_rate(void)
+{
+       bus_space_tag_t bst = &rockchip_bs_tag;
+       bus_space_handle_t bsh;
+       uint32_t clksel_con10;
+       uint32_t pclk_div, aclk_div;
+
+       rockchip_get_cru_bsh(&bsh);
+
+       clksel_con10 = bus_space_read_4(bst, bsh, CRU_CLKSEL_CON_REG(10));
+
+       pclk_div = __SHIFTOUT(clksel_con10,
+                             CRU_CLKSEL_CON10_PERI_PCLK_DIV_CON) + 1;
+       aclk_div = 1 << __SHIFTOUT(clksel_con10,
+                                  CRU_CLKSEL_CON10_PERI_ACLK_DIV_CON);
+
+       return rockchip_gpll_get_rate() / (pclk_div * aclk_div);
+}
+
+u_int
 rockchip_mmc0_get_rate(void)
 {
        bus_space_tag_t bst = &rockchip_bs_tag;
@@ -416,3 +442,13 @@
 
        return 0;
 }
+
+u_int
+rockchip_i2c_get_rate(u_int port)
+{
+       if (port == 0 || port == 1) {
+               return rockchip_a9periph_get_rate();
+       } else {
+               return rockchip_apb_get_rate();
+       }
+}
diff -r c6263f58ef71 -r 41ddb7ca5828 sys/arch/arm/rockchip/rockchip_i2c.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/arch/arm/rockchip/rockchip_i2c.c      Tue Dec 30 17:15:31 2014 +0000
@@ -0,0 +1,369 @@
+/* $NetBSD: rockchip_i2c.c,v 1.1 2014/12/30 17:15:31 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2014 Jared D. McNeill <jmcneill%invisible.ca@localhost>
+ * 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 AUTHOR ``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 AUTHOR 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 "locators.h"
+
+#include <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: rockchip_i2c.c,v 1.1 2014/12/30 17:15:31 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/device.h>
+#include <sys/intr.h>
+#include <sys/systm.h>
+#include <sys/kernel.h>
+#include <sys/mutex.h>
+#include <sys/condvar.h>
+
+#include <arm/rockchip/rockchip_reg.h>
+#include <arm/rockchip/rockchip_var.h>
+
+#include <arm/rockchip/rockchip_i2creg.h>
+
+#include <dev/i2c/i2cvar.h>
+
+#define RKIIC_CLOCK_RATE       1000000
+
+struct rkiic_softc {
+       device_t sc_dev;
+       bus_space_tag_t sc_bst;
+       bus_space_handle_t sc_bsh;
+       void *sc_ih;
+
+       struct i2c_controller sc_ic;
+       kmutex_t sc_lock;
+       kcondvar_t sc_cv;
+       device_t sc_i2cdev;
+       u_int sc_port;
+
+       uint32_t sc_intr_ipd;
+};
+
+static int     rkiic_match(device_t, cfdata_t, void *);
+static void    rkiic_attach(device_t, device_t, void *);
+
+static int     rkiic_intr(void *);
+
+static int     rkiic_acquire_bus(void *, int);
+static void    rkiic_release_bus(void *, int);
+static int     rkiic_exec(void *, i2c_op_t, i2c_addr_t, const void *,
+                          size_t, void *, size_t, int);
+
+static int     rkiic_wait(struct rkiic_softc *, uint32_t, int, int);
+static int     rkiic_read(struct rkiic_softc *, i2c_addr_t,
+                          uint8_t *, size_t, int);
+static int     rkiic_write(struct rkiic_softc *, i2c_addr_t,
+                           const uint8_t *, size_t, int);
+static int     rkiic_set_rate(struct rkiic_softc *, u_int);
+
+CFATTACH_DECL_NEW(rkiic, sizeof(struct rkiic_softc),
+       rkiic_match, rkiic_attach, NULL, NULL);
+
+#define I2C_WRITE(sc, reg, val) \
+    bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
+#define I2C_READ(sc, reg) \
+    bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
+
+static int
+rkiic_match(device_t parent, cfdata_t cf, void *aux)
+{
+       struct obio_attach_args * const obio = aux;
+
+       if (obio->obio_port == OBIOCF_PORT_DEFAULT)
+               return 0;
+
+       return 1;
+}
+
+static void
+rkiic_attach(device_t parent, device_t self, void *aux)
+{
+       struct rkiic_softc *sc = device_private(self);
+       struct obio_attach_args * const obio = aux;
+       struct i2cbus_attach_args iba;
+
+       sc->sc_dev = self;
+       sc->sc_bst = obio->obio_bst;
+       bus_space_subregion(obio->obio_bst, obio->obio_bsh, obio->obio_offset,
+           obio->obio_size, &sc->sc_bsh);
+       mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_SCHED);
+       cv_init(&sc->sc_cv, device_xname(self));
+       sc->sc_port = obio->obio_port;
+
+       aprint_naive("\n");
+       aprint_normal(": I2C%u\n", sc->sc_port);
+
+       if (obio->obio_intr != OBIOCF_INTR_DEFAULT) {
+               sc->sc_ih = intr_establish(obio->obio_intr, IPL_SCHED,
+                   IST_LEVEL, rkiic_intr, sc);
+               if (sc->sc_ih == NULL) {
+                       aprint_error_dev(self,
+                           "couldn't establish interrupt\n");
+                       /* Not fatal; will use polling mode */
+               }
+       }
+
+       sc->sc_ic.ic_cookie = sc;
+       sc->sc_ic.ic_acquire_bus = rkiic_acquire_bus;
+       sc->sc_ic.ic_release_bus = rkiic_release_bus;
+       sc->sc_ic.ic_exec = rkiic_exec;
+
+       iba.iba_tag = &sc->sc_ic;
+       sc->sc_i2cdev = config_found_ia(self, "i2cbus",
+           &iba, iicbus_print);
+}
+
+static int
+rkiic_intr(void *priv)
+{
+       struct rkiic_softc *sc = priv;
+       uint32_t ipd;
+



Home | Main Index | Thread Index | Old Index