Home | History | Annotate | Line # | Download | only in fdt
fdt_spi.c revision 1.4
      1  1.4  thorpej /* $NetBSD: fdt_spi.c,v 1.4 2025/09/10 02:21:18 thorpej Exp $ */
      2  1.1      tnn 
      3  1.1      tnn /*
      4  1.1      tnn  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  1.1      tnn  * All rights reserved.
      6  1.1      tnn  *
      7  1.1      tnn  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1      tnn  * by Tobias Nygren.
      9  1.1      tnn  *
     10  1.1      tnn  * Redistribution and use in source and binary forms, with or without
     11  1.1      tnn  * modification, are permitted provided that the following conditions
     12  1.1      tnn  * are met:
     13  1.1      tnn  * 1. Redistributions of source code must retain the above copyright
     14  1.1      tnn  *    notice, this list of conditions and the following disclaimer.
     15  1.1      tnn  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      tnn  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      tnn  *    documentation and/or other materials provided with the distribution.
     18  1.1      tnn  *
     19  1.1      tnn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1      tnn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1      tnn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1      tnn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1      tnn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1      tnn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1      tnn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1      tnn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1      tnn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1      tnn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1      tnn  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1      tnn  */
     31  1.1      tnn 
     32  1.1      tnn #include <sys/cdefs.h>
     33  1.4  thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.4 2025/09/10 02:21:18 thorpej Exp $");
     34  1.1      tnn 
     35  1.1      tnn #include <sys/param.h>
     36  1.1      tnn #include <sys/device.h>
     37  1.1      tnn #include <sys/bus.h>
     38  1.1      tnn #include <sys/kmem.h>
     39  1.1      tnn #include <sys/queue.h>
     40  1.1      tnn #include <dev/spi/spivar.h>
     41  1.1      tnn #include <libfdt.h>
     42  1.1      tnn #include <dev/fdt/fdtvar.h>
     43  1.1      tnn 
     44  1.1      tnn struct fdtbus_spi_controller {
     45  1.1      tnn 	device_t spi_dev;
     46  1.1      tnn 	int spi_phandle;
     47  1.4  thorpej 	struct spi_controller *spi_controller;
     48  1.1      tnn 	LIST_ENTRY(fdtbus_spi_controller) spi_next;
     49  1.1      tnn };
     50  1.1      tnn 
     51  1.1      tnn static LIST_HEAD(, fdtbus_spi_controller) fdtbus_spi_controllers =
     52  1.1      tnn     LIST_HEAD_INITIALIZER(fdtbus_spi_controllers);
     53  1.1      tnn 
     54  1.1      tnn int
     55  1.1      tnn fdtbus_register_spi_controller(device_t dev, int phandle,
     56  1.4  thorpej     struct spi_controller *controller)
     57  1.1      tnn {
     58  1.1      tnn 	struct fdtbus_spi_controller *spi;
     59  1.1      tnn 
     60  1.1      tnn 	spi = kmem_alloc(sizeof(*spi), KM_SLEEP);
     61  1.1      tnn 	spi->spi_dev = dev;
     62  1.1      tnn 	spi->spi_phandle = phandle;
     63  1.4  thorpej 	spi->spi_controller = controller;
     64  1.1      tnn 
     65  1.1      tnn 	LIST_INSERT_HEAD(&fdtbus_spi_controllers, spi, spi_next);
     66  1.1      tnn 
     67  1.1      tnn 	return 0;
     68  1.1      tnn }
     69  1.1      tnn 
     70  1.1      tnn static struct spi_controller *
     71  1.1      tnn fdtbus_get_spi_controller(int phandle)
     72  1.1      tnn {
     73  1.1      tnn 	struct fdtbus_spi_controller *spi;
     74  1.1      tnn 
     75  1.1      tnn 	LIST_FOREACH(spi, &fdtbus_spi_controllers, spi_next) {
     76  1.1      tnn 		if (spi->spi_phandle == phandle) {
     77  1.4  thorpej 			return spi->spi_controller;
     78  1.1      tnn 		}
     79  1.1      tnn 	}
     80  1.1      tnn 	return NULL;
     81  1.1      tnn }
     82  1.1      tnn 
     83  1.1      tnn device_t
     84  1.1      tnn fdtbus_attach_spibus(device_t dev, int phandle, cfprint_t print)
     85  1.1      tnn {
     86  1.1      tnn 	struct spi_controller *spi;
     87  1.1      tnn 	struct spibus_attach_args sba;
     88  1.1      tnn 	prop_dictionary_t devs;
     89  1.1      tnn 	device_t ret;
     90  1.1      tnn 	u_int address_cells;
     91  1.1      tnn 
     92  1.1      tnn 	devs = prop_dictionary_create();
     93  1.1      tnn 	if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
     94  1.1      tnn 		address_cells = 1;
     95  1.1      tnn 	of_enter_spi_devs(devs, phandle, address_cells * 4);
     96  1.1      tnn 
     97  1.1      tnn 	spi = fdtbus_get_spi_controller(phandle);
     98  1.1      tnn 	KASSERT(spi != NULL);
     99  1.1      tnn 	memset(&sba, 0, sizeof(sba));
    100  1.1      tnn 	sba.sba_controller = spi;
    101  1.1      tnn 
    102  1.1      tnn 	sba.sba_child_devices = prop_dictionary_get(devs, "spi-child-devices");
    103  1.1      tnn 	if (sba.sba_child_devices)
    104  1.1      tnn 		prop_object_retain(sba.sba_child_devices);
    105  1.1      tnn 	prop_object_release(devs);
    106  1.1      tnn 
    107  1.2  thorpej 	ret = config_found(dev, &sba, print,
    108  1.3  thorpej 	    CFARGS(.iattr = "spibus"));
    109  1.1      tnn 	if (sba.sba_child_devices)
    110  1.1      tnn 		prop_object_release(sba.sba_child_devices);
    111  1.1      tnn 
    112  1.1      tnn 	return ret;
    113  1.1      tnn }
    114  1.1      tnn 
    115  1.1      tnn 
    116