Source-Changes-HG archive

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

[src/trunk]: src/sys/dev/fdt Add support for simple MMC power sequence provid...



details:   https://anonhg.NetBSD.org/src/rev/11c16d95ff16
branches:  trunk
changeset: 827260:11c16d95ff16
user:      jmcneill <jmcneill%NetBSD.org@localhost>
date:      Sun Oct 22 13:56:49 2017 +0000

description:
Add support for simple MMC power sequence provider bindings.

diffstat:

 sys/dev/fdt/fdt_mmc_pwrseq.c    |  120 +++++++++++++++++++++++++++++
 sys/dev/fdt/fdtvar.h            |   19 ++++-
 sys/dev/fdt/files.fdt           |    7 +-
 sys/dev/fdt/mmc_pwrseq_simple.c |  165 ++++++++++++++++++++++++++++++++++++++++
 4 files changed, 309 insertions(+), 2 deletions(-)

diffs (truncated from 367 to 300 lines):

diff -r 72b267aa9e0e -r 11c16d95ff16 sys/dev/fdt/fdt_mmc_pwrseq.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/fdt/fdt_mmc_pwrseq.c      Sun Oct 22 13:56:49 2017 +0000
@@ -0,0 +1,120 @@
+/* $NetBSD: fdt_mmc_pwrseq.c,v 1.1 2017/10/22 13:56:49 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2017 Jared 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 <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: fdt_mmc_pwrseq.c,v 1.1 2017/10/22 13:56:49 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/bus.h>
+#include <sys/kmem.h>
+
+#include <libfdt.h>
+#include <dev/fdt/fdtvar.h>
+
+struct fdtbus_mmc_pwrseq {
+       device_t mps_dev;
+       int mps_phandle;
+       const struct fdtbus_mmc_pwrseq_func *mps_funcs;
+
+       struct fdtbus_mmc_pwrseq *mps_next;
+};
+
+static struct fdtbus_mmc_pwrseq *fdtbus_mps = NULL;
+
+int
+fdtbus_register_mmc_pwrseq(device_t dev, int phandle,
+    const struct fdtbus_mmc_pwrseq_func *funcs)
+{
+       struct fdtbus_mmc_pwrseq *mps;
+
+       mps = kmem_alloc(sizeof(*mps), KM_SLEEP);
+       mps->mps_dev = dev;
+       mps->mps_phandle = phandle;
+       mps->mps_funcs = funcs;
+
+       mps->mps_next = fdtbus_mps;
+       fdtbus_mps = mps;
+
+       return 0;
+}
+
+static struct fdtbus_mmc_pwrseq *
+fdtbus_get_mmc_pwrseq(int phandle)
+{
+       struct fdtbus_mmc_pwrseq *mps;
+
+       for (mps = fdtbus_mps; mps; mps = mps->mps_next) {
+               if (mps->mps_phandle == phandle) {
+                       return mps;
+               }
+       }
+
+       return NULL;
+}
+
+struct fdtbus_mmc_pwrseq *
+fdtbus_mmc_pwrseq_get(int phandle)
+{
+       const u_int *p;
+       int mps_phandle;
+
+       p = fdtbus_get_prop(phandle, "mmc-pwrseq", NULL);
+       if (p == NULL)
+               return NULL;
+
+       mps_phandle = fdtbus_get_phandle_from_native(be32toh(p[0]));
+       return fdtbus_get_mmc_pwrseq(mps_phandle);
+}
+
+void
+fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *mps)
+{
+       if (mps->mps_funcs->pre_power_on)
+               mps->mps_funcs->pre_power_on(mps->mps_dev);
+}
+
+void
+fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *mps)
+{
+       if (mps->mps_funcs->post_power_on)
+               mps->mps_funcs->post_power_on(mps->mps_dev);
+}
+
+void
+fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *mps)
+{
+       if (mps->mps_funcs->power_off)
+               mps->mps_funcs->power_off(mps->mps_dev);
+}
+
+void
+fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *mps)
+{
+       if (mps->mps_funcs->reset)
+               mps->mps_funcs->reset(mps->mps_dev);
+}
diff -r 72b267aa9e0e -r 11c16d95ff16 sys/dev/fdt/fdtvar.h
--- a/sys/dev/fdt/fdtvar.h      Sun Oct 22 13:13:55 2017 +0000
+++ b/sys/dev/fdt/fdtvar.h      Sun Oct 22 13:56:49 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fdtvar.h,v 1.26 2017/08/25 12:28:10 jmcneill Exp $ */
+/* $NetBSD: fdtvar.h,v 1.27 2017/10/22 13:56:49 jmcneill Exp $ */
 
 /*-
  * Copyright (c) 2015 Jared D. McNeill <jmcneill%invisible.ca@localhost>
@@ -185,6 +185,15 @@
        int     (*enable)(device_t, void *, bool);
 };
 
+struct fdtbus_mmc_pwrseq;
+
+struct fdtbus_mmc_pwrseq_func {
+       void    (*pre_power_on)(device_t);
+       void    (*post_power_on)(device_t);
+       void    (*power_off)(device_t);
+       void    (*reset)(device_t);
+};
+
 struct fdt_console {
        int     (*match)(int);
        void    (*consinit)(struct fdt_attach_args *, u_int);
@@ -225,6 +234,8 @@
                    const struct fdtbus_power_controller_func *);
 int            fdtbus_register_phy_controller(device_t, int,
                    const struct fdtbus_phy_controller_func *);
+int            fdtbus_register_mmc_pwrseq(device_t, int,
+                   const struct fdtbus_mmc_pwrseq_func *);
 
 int            fdtbus_get_reg(int, u_int, bus_addr_t *, bus_size_t *);
 int            fdtbus_get_reg_byname(int, const char *, bus_addr_t *,
@@ -278,6 +289,12 @@
 void           fdtbus_phy_put(struct fdtbus_phy *);
 int            fdtbus_phy_enable(struct fdtbus_phy *, bool);
 
+struct fdtbus_mmc_pwrseq *fdtbus_mmc_pwrseq_get(int);
+void           fdtbus_mmc_pwrseq_pre_power_on(struct fdtbus_mmc_pwrseq *);
+void           fdtbus_mmc_pwrseq_post_power_on(struct fdtbus_mmc_pwrseq *);
+void           fdtbus_mmc_pwrseq_power_off(struct fdtbus_mmc_pwrseq *);
+void           fdtbus_mmc_pwrseq_reset(struct fdtbus_mmc_pwrseq *);
+
 int            fdtbus_todr_attach(device_t, int, todr_chip_handle_t);
 
 void           fdtbus_power_reset(void);
diff -r 72b267aa9e0e -r 11c16d95ff16 sys/dev/fdt/files.fdt
--- a/sys/dev/fdt/files.fdt     Sun Oct 22 13:13:55 2017 +0000
+++ b/sys/dev/fdt/files.fdt     Sun Oct 22 13:56:49 2017 +0000
@@ -1,4 +1,4 @@
-# $NetBSD: files.fdt,v 1.21 2017/09/23 23:54:30 jmcneill Exp $
+# $NetBSD: files.fdt,v 1.22 2017/10/22 13:56:49 jmcneill Exp $
 
 include        "external/bsd/libfdt/conf/files.libfdt"
 
@@ -41,6 +41,7 @@
 file   dev/fdt/fdt_gpio.c                      fdtbus
 file   dev/fdt/fdt_i2c.c                       fdtbus
 file   dev/fdt/fdt_intr.c                      fdtbus
+file   dev/fdt/fdt_mmc_pwrseq.c                fdtbus
 file   dev/fdt/fdt_phy.c                       fdtbus
 file   dev/fdt/fdt_power.c                     fdtbus
 file   dev/fdt/fdt_regulator.c                 fdtbus
@@ -52,5 +53,9 @@
 attach cpus at fdt
 file   dev/fdt/cpus.c                          cpus
 
+device mmcpwrseq
+attach mmcpwrseq at fdt
+file   dev/fdt/mmc_pwrseq_simple.c             mmcpwrseq
+
 define fdt_display_timing
 file   dev/fdt/display_timing.c                fdt_display_timing
diff -r 72b267aa9e0e -r 11c16d95ff16 sys/dev/fdt/mmc_pwrseq_simple.c
--- /dev/null   Thu Jan 01 00:00:00 1970 +0000
+++ b/sys/dev/fdt/mmc_pwrseq_simple.c   Sun Oct 22 13:56:49 2017 +0000
@@ -0,0 +1,165 @@
+/* $NetBSD: mmc_pwrseq_simple.c,v 1.1 2017/10/22 13:56:49 jmcneill Exp $ */
+
+/*-
+ * Copyright (c) 2017 Jared 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 <sys/cdefs.h>
+__KERNEL_RCSID(0, "$NetBSD: mmc_pwrseq_simple.c,v 1.1 2017/10/22 13:56:49 jmcneill Exp $");
+
+#include <sys/param.h>
+#include <sys/kernel.h>
+#include <sys/device.h>
+#include <sys/systm.h>
+#include <sys/proc.h>
+#include <sys/gpio.h>
+
+#include <dev/fdt/fdtvar.h>
+
+#define        MMCPWRSEQ_MAX_PINS      32
+
+static const char * const compatible[] = {
+       "mmc-pwrseq-simple",
+       NULL
+};
+
+struct mmcpwrseq_softc {
+       device_t sc_dev;
+       int sc_phandle;
+       struct clk *sc_clk;
+       struct fdtbus_gpio_pin *sc_pins[MMCPWRSEQ_MAX_PINS];
+       u_int sc_npins;
+       u_int sc_post_power_on_delay_ms;
+       u_int sc_power_off_delay_us;
+};
+
+static void
+mmcpwrseq_pre_power_on(device_t dev)
+{
+       struct mmcpwrseq_softc * const sc = device_private(dev);
+       int error;
+
+       if (sc->sc_clk) {
+               error = clk_enable(sc->sc_clk);
+               if (error != 0) {
+                       aprint_error_dev(dev, "failed to enable clock: %d\n",
+                           error);
+               }
+       }
+
+       for (u_int n = 0; n < sc->sc_npins; n++)
+               fdtbus_gpio_write(sc->sc_pins[n], 1);
+}
+
+static void
+mmcpwrseq_post_power_on(device_t dev)
+{
+       struct mmcpwrseq_softc * const sc = device_private(dev);
+
+       for (u_int n = 0; n < sc->sc_npins; n++)
+               fdtbus_gpio_write(sc->sc_pins[n], 0);
+
+       if (sc->sc_post_power_on_delay_ms > 0)
+               kpause("mmcpwrseq", false,
+                   mstohz(sc->sc_post_power_on_delay_ms), NULL);
+}
+
+static void
+mmcpwrseq_power_off(device_t dev)
+{
+       struct mmcpwrseq_softc * const sc = device_private(dev);
+
+       for (u_int n = 0; n < sc->sc_npins; n++)
+               fdtbus_gpio_write(sc->sc_pins[n], 1);
+
+       if (sc->sc_power_off_delay_us > 0)
+               delay(sc->sc_power_off_delay_us);



Home | Main Index | Thread Index | Old Index