Source-Changes-HG archive

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

[src/trunk]: src/sys/arch/arm/samsung Use a separate lock (not the i2c bus lo...



details:   https://anonhg.NetBSD.org/src/rev/321522b8fffa
branches:  trunk
changeset: 466467:321522b8fffa
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Sun Dec 22 23:50:43 2019 +0000

description:
Use a separate lock (not the i2c bus lock) to synchronize with the
interrupt handler.  Refactor the code slightly to make the lock use
consistent.

Also includes the changes for:

Cleanup i2c bus acquire / release, centralizing all of the logic into
iic_acquire_bus() / iic_release_bus().  "acquire" and "release" hooks
no longer need to be provided by back-end controller drivers (only if
they need special handling, e.g. powering on the i2c controller).
This results in the removal of a bunch of rendundant code from each
back-end controller driver.

Assert that we are not in hard interrupt context in iic_acquire_bus(),
iic_exec(), and iic_release_bus().

diffstat:

 sys/arch/arm/samsung/exynos_i2c.c |  118 ++++++++++++++++++++++---------------
 1 files changed, 71 insertions(+), 47 deletions(-)

diffs (220 lines):

diff -r 6ad1a5820dbf -r 321522b8fffa sys/arch/arm/samsung/exynos_i2c.c
--- a/sys/arch/arm/samsung/exynos_i2c.c Sun Dec 22 23:40:49 2019 +0000
+++ b/sys/arch/arm/samsung/exynos_i2c.c Sun Dec 22 23:50:43 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: exynos_i2c.c,v 1.17 2019/10/18 06:13:38 skrll Exp $ */
+/*     $NetBSD: exynos_i2c.c,v 1.18 2019/12/22 23:50:43 thorpej Exp $ */
 
 /*
  * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -31,7 +31,7 @@
 #include "opt_arm_debug.h"
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: exynos_i2c.c,v 1.17 2019/10/18 06:13:38 skrll Exp $");
+__KERNEL_RCSID(0, "$NetBSD: exynos_i2c.c,v 1.18 2019/12/22 23:50:43 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/bus.h>
@@ -65,15 +65,12 @@
        bool                    sc_sda_is_output;
 
        struct i2c_controller   sc_ic;
-       kmutex_t                sc_lock;
-       kcondvar_t              sc_cv;
+       kmutex_t                sc_intr_lock;
+       kcondvar_t              sc_intr_wait;
 };
 
 static int     exynos_i2c_intr(void *);
 
-static int     exynos_i2c_acquire_bus(void *, int);
-static void    exynos_i2c_release_bus(void *, int);
-
 static int     exynos_i2c_send_start(void *, int);
 static int     exynos_i2c_send_stop(void *, int);
 static int     exynos_i2c_initiate_xfer(void *, i2c_addr_t, int);
@@ -155,8 +152,8 @@
                return;
        }
 
-       mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
-       cv_init(&sc->sc_cv, device_xname(self));
+       mutex_init(&sc->sc_intr_lock, MUTEX_DEFAULT, IPL_VM);
+       cv_init(&sc->sc_intr_wait, device_xname(self));
        aprint_normal(" @ 0x%08x\n", (uint)addr);
 
        if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
@@ -173,9 +170,8 @@
        }
        aprint_normal_dev(self, "interrupting on %s\n", intrstr);
        
+       iic_tag_init(&sc->sc_ic);
        sc->sc_ic.ic_cookie = sc;
-       sc->sc_ic.ic_acquire_bus = exynos_i2c_acquire_bus;
-       sc->sc_ic.ic_release_bus = exynos_i2c_release_bus;
        sc->sc_ic.ic_send_start  = exynos_i2c_send_start;
        sc->sc_ic.ic_send_stop   = exynos_i2c_send_stop;
        sc->sc_ic.ic_initiate_xfer = exynos_i2c_initiate_xfer;
@@ -206,31 +202,14 @@
        istatus &= ~IRQPEND;
        I2C_WRITE(sc, IICCON, istatus);
 
-       mutex_enter(&sc->sc_lock);
-       cv_broadcast(&sc->sc_cv);
-       mutex_exit(&sc->sc_lock);
+       mutex_enter(&sc->sc_intr_lock);
+       cv_broadcast(&sc->sc_intr_wait);
+       mutex_exit(&sc->sc_intr_lock);
 
        return 1;
 }
 
 static int
-exynos_i2c_acquire_bus(void *cookie, int flags)
-{
-       struct exynos_i2c_softc *i2c_sc = cookie;
-
-       mutex_enter(&i2c_sc->sc_lock);
-       return 0;
-}
-
-static void
-exynos_i2c_release_bus(void *cookie, int flags)
-{
-       struct exynos_i2c_softc *i2c_sc = cookie;
-
-       mutex_exit(&i2c_sc->sc_lock);
-}
-
-static int
 exynos_i2c_wait(struct exynos_i2c_softc *sc, int flags)
 {
        int error, retry;
@@ -240,8 +219,9 @@
 
        while (--retry > 0) {
                if ((flags & I2C_F_POLL) == 0) {
-                       error = cv_timedwait_sig(&sc->sc_cv, &sc->sc_lock,
-                           uimax(mstohz(10), 1));
+                       error = cv_timedwait_sig(&sc->sc_intr_wait,
+                                                &sc->sc_intr_lock,
+                                                uimax(mstohz(10), 1));
                        if (error) {
                                return error;
                        }
@@ -265,19 +245,51 @@
 
 
 static int
+exynos_i2c_send_start_locked(struct exynos_i2c_softc *sc, int flags)
+{
+       I2C_WRITE(sc, IICSTAT, 0xF0);
+       return 0;
+}
+
+static int
+exynos_i2c_send_stop_locked(struct exynos_i2c_softc *sc, int flags)
+{
+       I2C_WRITE(sc, IICSTAT, 0xD0);
+       return 0;
+}
+
+static int
+exynos_i2c_write_byte_locked(struct exynos_i2c_softc *sc, uint8_t byte,
+    int flags)
+{
+       int error = exynos_i2c_wait(sc, flags);
+       if (error) {
+               return error;
+       }
+       I2C_WRITE(sc, IICDS, byte);
+       return 0;
+}
+
+static int
 exynos_i2c_send_start(void *cookie, int flags)
 {
        struct exynos_i2c_softc *sc = cookie;
-       I2C_WRITE(sc, IICSTAT, 0xF0);
-       return 0;
+
+       mutex_enter(&sc->sc_intr_lock);
+       int error = exynos_i2c_send_start_locked(sc, flags);
+       mutex_exit(&sc->sc_intr_lock);
+       return error;
 }
 
 static int
 exynos_i2c_send_stop(void *cookie, int flags)
 {
        struct exynos_i2c_softc *sc = cookie;
-       I2C_WRITE(sc, IICSTAT, 0xD0);
-       return 0;
+
+       mutex_enter(&sc->sc_intr_lock);
+       int error = exynos_i2c_send_stop_locked(sc, flags);
+       mutex_exit(&sc->sc_intr_lock);
+       return error;
 }
 
 static int
@@ -285,26 +297,38 @@
 {
        struct exynos_i2c_softc *sc = cookie;
        uint8_t byte = addr & 0x7f;
+       int error;
+
        if (flags & I2C_F_READ)
                byte |= READBIT;
        else
                byte &= ~READBIT;
+
+       mutex_enter(&sc->sc_intr_lock);
        I2C_WRITE(sc, IICADD, addr);
-       exynos_i2c_send_start(cookie, flags);
-       exynos_i2c_write_byte(cookie, byte, flags);
-       return exynos_i2c_wait(cookie, flags);
+       exynos_i2c_send_start_locked(sc, flags);
+       exynos_i2c_write_byte_locked(sc, byte, flags);
+       error = exynos_i2c_wait(cookie, flags);
+       mutex_exit(&sc->sc_intr_lock);
+
+       return error;
 }
 
 static int
 exynos_i2c_read_byte(void *cookie, uint8_t *bytep, int flags)
 {
        struct exynos_i2c_softc *sc = cookie;
+
+       mutex_enter(&sc->sc_intr_lock);
        int error = exynos_i2c_wait(sc, flags);
-       if (error)
+       if (error) {
+               mutex_exit(&sc->sc_intr_lock);
                return error;
+       }
        *bytep = I2C_READ(sc, IICDS) & 0xff;
        if (flags & I2C_F_STOP)
-               exynos_i2c_send_stop(cookie, flags);
+               exynos_i2c_send_stop_locked(sc, flags);
+       mutex_exit(&sc->sc_intr_lock);
        return 0;
 }
 
@@ -312,9 +336,9 @@
 exynos_i2c_write_byte(void *cookie, uint8_t byte, int flags)
 {
        struct exynos_i2c_softc *sc = cookie;
-       int error = exynos_i2c_wait(sc, flags);
-       if (error)
-               return error;
-       I2C_WRITE(sc, IICDS, byte);
-       return 0;
+
+       mutex_enter(&sc->sc_intr_lock);
+       int error = exynos_i2c_write_byte_locked(sc, byte, flags);
+       mutex_exit(&sc->sc_intr_lock);
+       return error;
 }



Home | Main Index | Thread Index | Old Index