Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/marvell Clean up gttwsi's register access stuff:



details:   https://anonhg.NetBSD.org/src/rev/5b727c272a87
branches:  trunk
changeset: 968323:5b727c272a87
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun Jan 12 17:48:42 2020 +0000

description:
Clean up gttwsi's register access stuff:
- Garbage-collect the obsolete GTTWSI_ALLWINNER option; it hasn't been
  needed since FDT'ization of the Allwinner support code.
- Redefine thw "TWSI_*" register definitions to clearly call out:
  -> The Marvell flavor of the offsets
  -> The Allwinner flavor of the offsets
  ...and make the regular definitions indices into a register map.
- Pass the appropriate register map from the front-end to the core.
- Remove the customer register read/write callbacks -- they are no longer
  needed now that each front-end passes an appropriate register map to
  the core.

diffstat:

 sys/arch/arm/sunxi/sunxi_twi.c |  56 +++++++++++------------------------------
 sys/dev/i2c/files.i2c          |   3 +-
 sys/dev/i2c/gttwsi_core.c      |  43 ++++++++++---------------------
 sys/dev/i2c/gttwsireg.h        |  50 ++++++++++++++++++++++---------------
 sys/dev/i2c/gttwsivar.h        |  13 ++++++---
 sys/dev/marvell/gttwsi.c       |  16 +++++++++--
 6 files changed, 81 insertions(+), 100 deletions(-)

diffs (truncated from 365 to 300 lines):

diff -r b1a03dae0bf5 -r 5b727c272a87 sys/arch/arm/sunxi/sunxi_twi.c
--- a/sys/arch/arm/sunxi/sunxi_twi.c    Sun Jan 12 17:46:55 2020 +0000
+++ b/sys/arch/arm/sunxi/sunxi_twi.c    Sun Jan 12 17:48:42 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: sunxi_twi.c,v 1.10 2018/07/01 21:16:19 jmcneill Exp $ */
+/* $NetBSD: sunxi_twi.c,v 1.11 2020/01/12 17:48:42 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2017 Jared McNeill <jmcneill%invisible.ca@localhost>
@@ -26,14 +26,9 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
-#include "opt_gttwsi.h"
-#ifdef GTTWSI_ALLWINNER
-# error Do not define GTTWSI_ALLWINNER when using this driver
-#endif
-
 #include <sys/cdefs.h>
 
-__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.10 2018/07/01 21:16:19 jmcneill Exp $");
+__KERNEL_RCSID(0, "$NetBSD: sunxi_twi.c,v 1.11 2020/01/12 17:48:42 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -52,22 +47,14 @@
 #define         TWI_CCR_CLK_M  __BITS(6,3)
 #define         TWI_CCR_CLK_N  __BITS(2,0)
 
-static uint8_t sunxi_twi_regmap_rd[] = {
-       [TWSI_SLAVEADDR/4]              = 0x00,
-       [TWSI_EXTEND_SLAVEADDR/4]       = 0x04,
-       [TWSI_DATA/4]                   = 0x08,
-       [TWSI_CONTROL/4]                = 0x0c,
-       [TWSI_STATUS/4]                 = 0x10,
-       [TWSI_SOFTRESET/4]              = 0x18,
-};
-
-static uint8_t sunxi_twi_regmap_wr[] = {
-       [TWSI_SLAVEADDR/4]              = 0x00,
-       [TWSI_EXTEND_SLAVEADDR/4]       = 0x04,
-       [TWSI_DATA/4]                   = 0x08,
-       [TWSI_CONTROL/4]                = 0x0c,
-       [TWSI_BAUDRATE/4]               = 0x14,
-       [TWSI_SOFTRESET/4]              = 0x18,
+static const bus_size_t sunxi_twi_regmap[] = {
+       [TWSI_SLAVEADDR]        = TWSI_ALLWINNER_SLAVEADDR,
+       [TWSI_EXTEND_SLAVEADDR] = TWSI_ALLWINNER_EXTEND_SLAVEADDR,
+       [TWSI_DATA]             = TWSI_ALLWINNER_DATA,
+       [TWSI_CONTROL]          = TWSI_ALLWINNER_CONTROL,
+       [TWSI_STATUS]           = TWSI_ALLWINNER_STATUS,
+       [TWSI_BAUDRATE]         = TWSI_ALLWINNER_BAUDRATE,
+       [TWSI_SOFTRESET]        = TWSI_ALLWINNER_SOFTRESET,
 };
 
 static int sunxi_twi_match(device_t, cfdata_t, void *);
@@ -106,18 +93,6 @@
        .get_tag = sunxi_twi_get_tag,
 };
 
-static uint32_t
-sunxi_twi_reg_read(struct gttwsi_softc *sc, uint32_t reg)
-{
-       return bus_space_read_4(sc->sc_bust, sc->sc_bush, sunxi_twi_regmap_rd[reg/4]);
-}
-
-static void
-sunxi_twi_reg_write(struct gttwsi_softc *sc, uint32_t reg, uint32_t val)
-{
-       bus_space_write_4(sc->sc_bust, sc->sc_bush, sunxi_twi_regmap_wr[reg/4], val);
-}
-
 static u_int
 sunxi_twi_calc_rate(u_int parent_rate, u_int n, u_int m)
 {
@@ -130,11 +105,12 @@
        uint32_t baud;
        u_int n, m, best_rate;
 
-       baud = sunxi_twi_reg_read(sc, TWSI_BAUDRATE);
+       baud = gttwsi_read_4(sc, TWSI_BAUDRATE);
 
        for (best_rate = 0, n = 0; n < 8; n++) {
                for (m = 0; m < 16; m++) {
-                       const u_int tmp_rate = sunxi_twi_calc_rate(parent_rate, n, m);
+                       const u_int tmp_rate =
+                           sunxi_twi_calc_rate(parent_rate, n, m);
                        if (tmp_rate <= rate && tmp_rate > best_rate) {
                                best_rate = tmp_rate;
                                baud = __SHIFTIN(n, TWI_CCR_CLK_N) |
@@ -143,7 +119,7 @@
                }
        }
 
-       sunxi_twi_reg_write(sc, TWSI_BAUDRATE, baud);
+       gttwsi_write_4(sc, TWSI_BAUDRATE, baud);
        delay(10000);
 }
 
@@ -202,9 +178,7 @@
            conf->iflg_rwc);
 
        /* Attach gttwsi core */
-       sc->sc_reg_read = sunxi_twi_reg_read;
-       sc->sc_reg_write = sunxi_twi_reg_write;
-       gttwsi_attach_subr(self, bst, bsh);
+       gttwsi_attach_subr(self, bst, bsh, sunxi_twi_regmap);
 
        /*
         * Set clock rate to 100kHz.
diff -r b1a03dae0bf5 -r 5b727c272a87 sys/dev/i2c/files.i2c
--- a/sys/dev/i2c/files.i2c     Sun Jan 12 17:46:55 2020 +0000
+++ b/sys/dev/i2c/files.i2c     Sun Jan 12 17:48:42 2020 +0000
@@ -1,4 +1,4 @@
-#      $NetBSD: files.i2c,v 1.107 2020/01/03 18:00:05 jmcneill Exp $
+#      $NetBSD: files.i2c,v 1.108 2020/01/12 17:48:42 thorpej Exp $
 
 obsolete defflag       opt_i2cbus.h            I2C_SCAN
 define i2cbus { }
@@ -55,7 +55,6 @@
 file   dev/i2c/motoi2c.c                       motoi2c
 define mvi2c
 file   dev/i2c/gttwsi_core.c                   mvi2c
-defflag opt_gttwsi.h                           GTTWSI_ALLWINNER
 
 #
 # I2C client devices
diff -r b1a03dae0bf5 -r 5b727c272a87 sys/dev/i2c/gttwsi_core.c
--- a/sys/dev/i2c/gttwsi_core.c Sun Jan 12 17:46:55 2020 +0000
+++ b/sys/dev/i2c/gttwsi_core.c Sun Jan 12 17:48:42 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gttwsi_core.c,v 1.11 2020/01/11 22:21:25 thorpej Exp $ */
+/*     $NetBSD: gttwsi_core.c,v 1.12 2020/01/12 17:48:42 thorpej Exp $ */
 /*
  * Copyright (c) 2008 Eiji Kawauchi.
  * All rights reserved.
@@ -66,7 +66,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: gttwsi_core.c,v 1.11 2020/01/11 22:21:25 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: gttwsi_core.c,v 1.12 2020/01/12 17:48:42 thorpej Exp $");
 #include "locators.h"
 
 #include <sys/param.h>
@@ -92,45 +92,35 @@
 static int     gttwsi_wait(struct gttwsi_softc *, uint32_t, uint32_t,
                            uint32_t, int);
 
-static inline uint32_t
-gttwsi_default_read_4(struct gttwsi_softc *sc, uint32_t reg)
+uint32_t
+gttwsi_read_4(struct gttwsi_softc *sc, uint32_t reg)
 {
-       uint32_t val = bus_space_read_4(sc->sc_bust, sc->sc_bush, reg);
+       const uint32_t val = bus_space_read_4(sc->sc_bust, sc->sc_bush,
+                                             sc->sc_regmap[reg]);
 #ifdef TWSI_DEBUG
-       printf("I2C:R:%02x:%02x\n", reg, val);
+       printf("I2C:R:[%u]%02x:%02x\n", reg, sc->sc_regmap[reg], val);
 #else
        DELAY(TWSI_READ_DELAY);
 #endif
        return val;
 }
 
-static inline void
-gttwsi_default_write_4(struct gttwsi_softc *sc, uint32_t reg, uint32_t val)
+void
+gttwsi_write_4(struct gttwsi_softc *sc, uint32_t reg, uint32_t val)
 {
+
        bus_space_write_4(sc->sc_bust, sc->sc_bush, reg, val);
 #ifdef TWSI_DEBUG
-       printf("I2C:W:%02x:%02x\n", reg, val);
+       printf("I2C:W:[%u]%02x:%02x\n", reg, sc->sc_regmap[reg], val);
 #else
        DELAY(TWSI_WRITE_DELAY);
 #endif
-       return;
-}
-
-static inline uint32_t
-gttwsi_read_4(struct gttwsi_softc *sc, uint32_t reg)
-{
-       return sc->sc_reg_read(sc, reg);
-}
-
-static inline void
-gttwsi_write_4(struct gttwsi_softc *sc, uint32_t reg, uint32_t val)
-{
-       return sc->sc_reg_write(sc, reg, val);
 }
 
 /* ARGSUSED */
 void
-gttwsi_attach_subr(device_t self, bus_space_tag_t iot, bus_space_handle_t ioh)
+gttwsi_attach_subr(device_t self, bus_space_tag_t iot, bus_space_handle_t ioh,
+                  const bus_size_t *regmap)
 {
        struct gttwsi_softc * const sc = device_private(self);
        prop_dictionary_t cfg = device_properties(self);
@@ -141,11 +131,7 @@
        sc->sc_dev = self;
        sc->sc_bust = iot;
        sc->sc_bush = ioh;
-
-       if (sc->sc_reg_read == NULL)
-               sc->sc_reg_read = gttwsi_default_read_4;
-       if (sc->sc_reg_write == NULL)
-               sc->sc_reg_write = gttwsi_default_write_4;
+       sc->sc_regmap = regmap;
 
        mutex_init(&sc->sc_mtx, MUTEX_DEFAULT, IPL_BIO);
        cv_init(&sc->sc_cv, device_xname(self));
@@ -166,7 +152,6 @@
         */
        /* reset */
        gttwsi_write_4(sc, TWSI_SOFTRESET, SOFTRESET_VAL);
-
 }
 
 void
diff -r b1a03dae0bf5 -r 5b727c272a87 sys/dev/i2c/gttwsireg.h
--- a/sys/dev/i2c/gttwsireg.h   Sun Jan 12 17:46:55 2020 +0000
+++ b/sys/dev/i2c/gttwsireg.h   Sun Jan 12 17:48:42 2020 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: gttwsireg.h,v 1.3 2014/09/11 11:14:44 jmcneill Exp $ */
+/* $NetBSD: gttwsireg.h,v 1.4 2020/01/12 17:48:42 thorpej Exp $ */
 
 /*
  * Copyright (c) 2008 Eiji Kawauchi.
@@ -27,27 +27,37 @@
 #ifndef _GTTWSIREG_H_
 #define _GTTWSIREG_H_
 
-#include "opt_gttwsi.h"
+#define GTTWSI_SIZE            0x100
+#define        GTTWSI_NREGS            7
 
-#define GTTWSI_SIZE            0x100
+       /* reg map indices */
+#define        TWSI_SLAVEADDR                  0
+#define        TWSI_EXTEND_SLAVEADDR           1
+#define        TWSI_DATA                       2
+#define        TWSI_CONTROL                    3
+#define        TWSI_STATUS                     4
+#define        TWSI_BAUDRATE                   5
+#define        TWSI_SOFTRESET                  6
 
-#if defined(GTTWSI_ALLWINNER)
-#define TWSI_SLAVEADDR         0x00
-#define TWSI_EXTEND_SLAVEADDR  0x04
-#define TWSI_DATA              0x08
-#define TWSI_CONTROL           0x0c
-#define TWSI_STATUS            0x10
-#define TWSI_BAUDRATE          0x14
-#define TWSI_SOFTRESET         0x18
-#else
-#define        TWSI_SLAVEADDR          0x00
-#define        TWSI_EXTEND_SLAVEADDR   0x10
-#define        TWSI_DATA               0x04
-#define        TWSI_CONTROL            0x08
-#define        TWSI_STATUS             0x0c    /* for read */
-#define        TWSI_BAUDRATE           0x0c    /* for write */
-#define        TWSI_SOFTRESET          0x1c
-#endif
+       /* register offsets for Allwinner implementations */
+#define        TWSI_ALLWINNER_SLAVEADDR        0x00
+#define        TWSI_ALLWINNER_EXTEND_SLAVEADDR 0x04
+#define        TWSI_ALLWINNER_DATA             0x08
+#define        TWSI_ALLWINNER_CONTROL          0x0c
+#define        TWSI_ALLWINNER_STATUS           0x10
+#define        TWSI_ALLWINNER_BAUDRATE         0x14
+#define        TWSI_ALLWINNER_SOFTRESET        0x18
+#define        TWSI_ALLWINNER_ENH_FEAT         0x1c
+#define        TWSI_ALLWINNER_LINE_CTRL        0x20
+
+       /* register offsets for Marvell implementations */
+#define        TWSI_MARVELL_SLAVEADDR          0x00
+#define        TWSI_MARVELL_EXTEND_SLAVEADDR   0x10
+#define        TWSI_MARVELL_DATA               0x04
+#define        TWSI_MARVELL_CONTROL            0x08
+#define        TWSI_MARVELL_STATUS             0x0c    /* for read */
+#define        TWSI_MARVELL_BAUDRATE           0x0c    /* for write */
+#define        TWSI_MARVELL_SOFTRESET          0x1c
 
 #define        SLAVEADDR_GCE_MASK      0x01
 #define        SLAVEADDR_SADDR_MASK    0xfe
diff -r b1a03dae0bf5 -r 5b727c272a87 sys/dev/i2c/gttwsivar.h
--- a/sys/dev/i2c/gttwsivar.h   Sun Jan 12 17:46:55 2020 +0000
+++ b/sys/dev/i2c/gttwsivar.h   Sun Jan 12 17:48:42 2020 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: gttwsivar.h,v 1.5 2019/12/25 14:08:47 thorpej Exp $    */
+/*     $NetBSD: gttwsivar.h,v 1.6 2020/01/12 17:48:42 thorpej Exp $    */
 /*
  * Copyright (c) 2008 Eiji Kawauchi.
  * All rights reserved.
@@ -88,15 +88,18 @@
        kmutex_t sc_mtx;



Home | Main Index | Thread Index | Old Index