Source-Changes-HG archive

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

[src/thorpej-i2c-spi-conf]: src/sys/dev Define a "spi-enumerate-devices" devi...



details:   https://anonhg.NetBSD.org/src/rev/7b04d86f164d
branches:  thorpej-i2c-spi-conf
changeset: 378795:7b04d86f164d
user:      thorpej <thorpej%NetBSD.org@localhost>
date:      Tue May 18 23:48:16 2021 +0000

description:
Define a "spi-enumerate-devices" device call and use it for direct
configuration of SPI devices, rather than slinging arrays of dictionaries
around.  Implement this device call for OpenFirmware / FDT, following
the SPI bindings for Device Tree.

diffstat:

 sys/dev/fdt/fdt_spi.c      |   30 +---
 sys/dev/ofw/ofw_spi_subr.c |  146 +++++++++++-----------
 sys/dev/spi/spi.c          |  285 +++++++++++++++++++++++---------------------
 sys/dev/spi/spivar.h       |   26 +++-
 4 files changed, 245 insertions(+), 242 deletions(-)

diffs (truncated from 702 to 300 lines):

diff -r 87423d810603 -r 7b04d86f164d sys/dev/fdt/fdt_spi.c
--- a/sys/dev/fdt/fdt_spi.c     Tue May 18 23:30:55 2021 +0000
+++ b/sys/dev/fdt/fdt_spi.c     Tue May 18 23:48:16 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: fdt_spi.c,v 1.2 2021/04/24 23:36:53 thorpej Exp $ */
+/* $NetBSD: fdt_spi.c,v 1.2.2.1 2021/05/18 23:48:16 thorpej Exp $ */
 
 /*
  * Copyright (c) 2019 The NetBSD Foundation, Inc.
@@ -30,7 +30,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.2 2021/04/24 23:36:53 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.2.2.1 2021/05/18 23:48:16 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
@@ -84,33 +84,17 @@ device_t
 fdtbus_attach_spibus(device_t dev, int phandle, cfprint_t print)
 {
        struct spi_controller *spi;
-       struct spibus_attach_args sba;
-       prop_dictionary_t devs;
-       device_t ret;
-       u_int address_cells;
-
-       devs = prop_dictionary_create();
-       if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
-               address_cells = 1;
-       of_enter_spi_devs(devs, phandle, address_cells * 4);
 
        spi = fdtbus_get_spi_controller(phandle);
        KASSERT(spi != NULL);
-       memset(&sba, 0, sizeof(sba));
-       sba.sba_controller = spi;
 
-       sba.sba_child_devices = prop_dictionary_get(devs, "spi-child-devices");
-       if (sba.sba_child_devices)
-               prop_object_retain(sba.sba_child_devices);
-       prop_object_release(devs);
-
-       ret = config_found(dev, &sba, print,
+       struct spibus_attach_args sba = {
+               .sba_controller = spi,
+       };
+       return config_found(dev, &sba, print,
            CFARG_IATTR, "spibus",
+           CFARG_DEVHANDLE, device_handle(dev),
            CFARG_EOL);
-       if (sba.sba_child_devices)
-               prop_object_release(sba.sba_child_devices);
-
-       return ret;
 }
 
 
diff -r 87423d810603 -r 7b04d86f164d sys/dev/ofw/ofw_spi_subr.c
--- a/sys/dev/ofw/ofw_spi_subr.c        Tue May 18 23:30:55 2021 +0000
+++ b/sys/dev/ofw/ofw_spi_subr.c        Tue May 18 23:48:16 2021 +0000
@@ -1,100 +1,96 @@
-/*     $NetBSD: ofw_spi_subr.c,v 1.1 2021/02/04 20:19:09 thorpej Exp $ */
+/*     $NetBSD: ofw_spi_subr.c,v 1.1.6.1 2021/05/18 23:48:16 thorpej Exp $     */
 
 /*
- * Copyright 1998
- * Digital Equipment Corporation. All rights reserved.
+ * Copyright (c) 2021 The NetBSD Foundation, Inc.
+ * All rights reserved.
  *
- * This software is furnished under license and may be used and
- * copied only in accordance with the following terms and conditions.
- * Subject to these conditions, you may download, copy, install,
- * use, modify and distribute this software in source and/or binary
- * form. No title or ownership is transferred hereby.
- *
- * 1) Any source code used, modified or distributed must reproduce
- *    and retain this copyright notice and list of conditions as
- *    they appear in the source file.
+ * 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.
  *
- * 2) No right is granted to use any trade name, trademark, or logo of
- *    Digital Equipment Corporation. Neither the "Digital Equipment
- *    Corporation" name nor any trademark or logo of Digital Equipment
- *    Corporation may be used to endorse or promote products derived
- *    from this software without the prior written permission of
- *    Digital Equipment Corporation.
- *
- * 3) This software is provided "AS-IS" and any express or implied
- *    warranties, including but not limited to, any implied warranties
- *    of merchantability, fitness for a particular purpose, or
- *    non-infringement are disclaimed. In no event shall DIGITAL be
- *    liable for any damages whatsoever, and in particular, DIGITAL
- *    shall not be liable for special, indirect, consequential, or
- *    incidental damages or damages for lost profits, loss of
- *    revenue or loss of use, whether such damages arise in contract,
- *    negligence, tort, under statute, in equity, at law or otherwise,
- *    even if advised of the possibility of such damage.
+ * 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: ofw_spi_subr.c,v 1.1 2021/02/04 20:19:09 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: ofw_spi_subr.c,v 1.1.6.1 2021/05/18 23:48:16 thorpej Exp $");
 
 #include <sys/param.h>
 #include <sys/device.h>
 #include <sys/kmem.h>
 #include <sys/systm.h>
 #include <dev/ofw/openfirm.h>
+#include <dev/spi/spivar.h>
 
-void
-of_enter_spi_devs(prop_dictionary_t props, int ofnode, size_t cell_size)
+static int
+of_spi_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
 {
-       int node, len;
-       char name[32];
-       uint64_t reg64;
-       uint32_t reg32;
-       uint32_t slave;
-       u_int32_t maxfreq;
-       prop_array_t array = NULL;
-       prop_dictionary_t dev;
-       int mode;
+       struct spi_enumerate_devices_args *args = v;
+       int spi_node, node;
+       char name[32], compat_buf[32];
+       uint32_t chip_select;
+       char *clist;
+       int clist_size;
+       bool cbrv;
 
-       for (node = OF_child(ofnode); node; node = OF_peer(node)) {
-               if (OF_getprop(node, "name", name, sizeof(name)) <= 0)
+       spi_node = devhandle_to_of(call_handle);
+
+       for (node = OF_child(spi_node); node != 0; node = OF_peer(node)) {
+               if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
                        continue;
-               len = OF_getproplen(node, "reg");
-               slave = 0;
-               if (cell_size == 8 && len >= sizeof(reg64)) {
-                       if (OF_getprop(node, "reg", &reg64, sizeof(reg64))
-                           < sizeof(reg64))
-                               continue;
-                       slave = be64toh(reg64);
-               } else if (cell_size == 4 && len >= sizeof(reg32)) {
-                       if (OF_getprop(node, "reg", &reg32, sizeof(reg32))
-                           < sizeof(reg32))
-                               continue;
-                       slave = be32toh(reg32);
-               } else {
+               }
+
+               if (of_getprop_uint32(node, "reg", &chip_select) != 0) {
+                       continue;
+               }
+
+               /* Device Tree bindings specify a max chip select of 256. */
+               if (chip_select > 256) {
                        continue;
                }
-               if (of_getprop_uint32(node, "spi-max-frequency", &maxfreq)) {
-                       maxfreq = 0;
+
+               clist_size = OF_getproplen(node, "compatible");
+               if (clist_size <= 0) {
+                       continue;
                }
-               mode = ((int)of_hasprop(node, "cpol") << 1) | (int)of_hasprop(node, "cpha");
 
-               if (array == NULL)
-                       array = prop_array_create();
+               clist = kmem_tmpbuf_alloc(clist_size,
+                   compat_buf, sizeof(compat_buf), KM_SLEEP);
+               if (OF_getprop(node, "compatible", clist, clist_size) <
+                   clist_size) {
+                       kmem_tmpbuf_free(clist, clist_size, compat_buf);
+                       continue;
+               }
 
-               dev = prop_dictionary_create();
-               prop_dictionary_set_string(dev, "name", name);
-               prop_dictionary_set_uint32(dev, "slave", slave);
-               prop_dictionary_set_uint32(dev, "mode", mode);
-               if (maxfreq > 0)
-                       prop_dictionary_set_uint32(dev, "spi-max-frequency", maxfreq);
-               prop_dictionary_set_uint64(dev, "cookie", node);
-               of_to_dataprop(dev, node, "compatible", "compatible");
-               prop_array_add(array, dev);
-               prop_object_release(dev);
+               args->chip_select = (int)chip_select;
+               args->sa->sa_name = name;
+               args->sa->sa_clist = clist;
+               args->sa->sa_clist_size = clist_size;
+               args->sa->sa_devhandle = devhandle_from_of(node);
+
+               cbrv = args->callback(dev, args);
+
+               kmem_tmpbuf_free(clist, clist_size, compat_buf);
+
+               if (!cbrv) {
+                       break;
+               }
        }
 
-       if (array != NULL) {
-               prop_dictionary_set(props, "spi-child-devices", array);
-               prop_object_release(array);
-       }
+       return 0;
 }
+OF_DEVICE_CALL_REGISTER("spi-enumerate-devices", of_spi_enumerate_devices);
diff -r 87423d810603 -r 7b04d86f164d sys/dev/spi/spi.c
--- a/sys/dev/spi/spi.c Tue May 18 23:30:55 2021 +0000
+++ b/sys/dev/spi/spi.c Tue May 18 23:48:16 2021 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: spi.c,v 1.17 2021/04/24 23:36:59 thorpej Exp $ */
+/* $NetBSD: spi.c,v 1.17.2.1 2021/05/18 23:48:16 thorpej Exp $ */
 
 /*-
  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
@@ -42,7 +42,7 @@
  */
 
 #include <sys/cdefs.h>
-__KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.17 2021/04/24 23:36:59 thorpej Exp $");
+__KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.17.2.1 2021/05/18 23:48:16 thorpej Exp $");
 
 #include "locators.h"
 
@@ -50,7 +50,7 @@
 #include <sys/systm.h>
 #include <sys/device.h>
 #include <sys/conf.h>
-#include <sys/malloc.h>
+#include <sys/kmem.h>
 #include <sys/mutex.h>
 #include <sys/condvar.h>
 #include <sys/errno.h>
@@ -62,12 +62,14 @@
 #include "locators.h"
 
 struct spi_softc {
+       device_t                sc_dev;
        struct spi_controller   sc_controller;
        int                     sc_mode;
        int                     sc_speed;
        int                     sc_slave;
        int                     sc_nslaves;
        struct spi_handle       *sc_slaves;
+       kmutex_t                sc_slave_state_lock;
        kmutex_t                sc_lock;
        kcondvar_t              sc_cv;
        int                     sc_flags;
@@ -97,13 +99,14 @@ const struct cdevsw spi_cdevsw = {
  * SPI slave device.  We have one of these per slave.
  */
 struct spi_handle {
-       struct spi_softc        *sh_sc;
-       struct spi_controller   *sh_controller;
-       int                     sh_slave;
-       int                     sh_mode;
-       int                     sh_speed;
-       int                     sh_flags;
-#define SPIH_ATTACHED          1
+       struct spi_softc        *sh_sc;         /* static */
+       struct spi_controller   *sh_controller; /* static */
+       int                     sh_slave;       /* static */
+       int                     sh_mode;        /* locked by owning child */
+       int                     sh_speed;       /* locked by owning child */
+       int                     sh_flags;       /* ^^ slave_state_lock ^^ */
+#define SPIH_ATTACHED          __BIT(0)
+#define SPIH_DIRECT            __BIT(1)
 };
 
 #define SPI_MAXDATA 4096
@@ -131,14 +134,95 @@ spi_match(device_t parent, cfdata_t cf, 
 }
 
 static int



Home | Main Index | Thread Index | Old Index