1*c7fb772bSthorpej /* $NetBSD: fdt_spi.c,v 1.3 2021/08/07 16:19:10 thorpej Exp $ */
23bee3e25Stnn
33bee3e25Stnn /*
43bee3e25Stnn * Copyright (c) 2019 The NetBSD Foundation, Inc.
53bee3e25Stnn * All rights reserved.
63bee3e25Stnn *
73bee3e25Stnn * This code is derived from software contributed to The NetBSD Foundation
83bee3e25Stnn * by Tobias Nygren.
93bee3e25Stnn *
103bee3e25Stnn * Redistribution and use in source and binary forms, with or without
113bee3e25Stnn * modification, are permitted provided that the following conditions
123bee3e25Stnn * are met:
133bee3e25Stnn * 1. Redistributions of source code must retain the above copyright
143bee3e25Stnn * notice, this list of conditions and the following disclaimer.
153bee3e25Stnn * 2. Redistributions in binary form must reproduce the above copyright
163bee3e25Stnn * notice, this list of conditions and the following disclaimer in the
173bee3e25Stnn * documentation and/or other materials provided with the distribution.
183bee3e25Stnn *
193bee3e25Stnn * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
203bee3e25Stnn * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
213bee3e25Stnn * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
223bee3e25Stnn * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
233bee3e25Stnn * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
243bee3e25Stnn * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
253bee3e25Stnn * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
263bee3e25Stnn * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
273bee3e25Stnn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
283bee3e25Stnn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
293bee3e25Stnn * POSSIBILITY OF SUCH DAMAGE.
303bee3e25Stnn */
313bee3e25Stnn
323bee3e25Stnn #include <sys/cdefs.h>
33*c7fb772bSthorpej __KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.3 2021/08/07 16:19:10 thorpej Exp $");
343bee3e25Stnn
353bee3e25Stnn #include <sys/param.h>
363bee3e25Stnn #include <sys/device.h>
373bee3e25Stnn #include <sys/bus.h>
383bee3e25Stnn #include <sys/kmem.h>
393bee3e25Stnn #include <sys/queue.h>
403bee3e25Stnn #include <dev/spi/spivar.h>
413bee3e25Stnn #include <libfdt.h>
423bee3e25Stnn #include <dev/fdt/fdtvar.h>
433bee3e25Stnn
443bee3e25Stnn struct fdtbus_spi_controller {
453bee3e25Stnn device_t spi_dev;
463bee3e25Stnn int spi_phandle;
473bee3e25Stnn const struct fdtbus_spi_controller_func *spi_funcs;
483bee3e25Stnn LIST_ENTRY(fdtbus_spi_controller) spi_next;
493bee3e25Stnn };
503bee3e25Stnn
513bee3e25Stnn static LIST_HEAD(, fdtbus_spi_controller) fdtbus_spi_controllers =
523bee3e25Stnn LIST_HEAD_INITIALIZER(fdtbus_spi_controllers);
533bee3e25Stnn
543bee3e25Stnn int
fdtbus_register_spi_controller(device_t dev,int phandle,const struct fdtbus_spi_controller_func * funcs)553bee3e25Stnn fdtbus_register_spi_controller(device_t dev, int phandle,
563bee3e25Stnn const struct fdtbus_spi_controller_func *funcs)
573bee3e25Stnn {
583bee3e25Stnn struct fdtbus_spi_controller *spi;
593bee3e25Stnn
603bee3e25Stnn spi = kmem_alloc(sizeof(*spi), KM_SLEEP);
613bee3e25Stnn spi->spi_dev = dev;
623bee3e25Stnn spi->spi_phandle = phandle;
633bee3e25Stnn spi->spi_funcs = funcs;
643bee3e25Stnn
653bee3e25Stnn LIST_INSERT_HEAD(&fdtbus_spi_controllers, spi, spi_next);
663bee3e25Stnn
673bee3e25Stnn return 0;
683bee3e25Stnn }
693bee3e25Stnn
703bee3e25Stnn static struct spi_controller *
fdtbus_get_spi_controller(int phandle)713bee3e25Stnn fdtbus_get_spi_controller(int phandle)
723bee3e25Stnn {
733bee3e25Stnn struct fdtbus_spi_controller *spi;
743bee3e25Stnn
753bee3e25Stnn LIST_FOREACH(spi, &fdtbus_spi_controllers, spi_next) {
763bee3e25Stnn if (spi->spi_phandle == phandle) {
773bee3e25Stnn return spi->spi_funcs->get_controller(spi->spi_dev);
783bee3e25Stnn }
793bee3e25Stnn }
803bee3e25Stnn return NULL;
813bee3e25Stnn }
823bee3e25Stnn
833bee3e25Stnn device_t
fdtbus_attach_spibus(device_t dev,int phandle,cfprint_t print)843bee3e25Stnn fdtbus_attach_spibus(device_t dev, int phandle, cfprint_t print)
853bee3e25Stnn {
863bee3e25Stnn struct spi_controller *spi;
873bee3e25Stnn struct spibus_attach_args sba;
883bee3e25Stnn prop_dictionary_t devs;
893bee3e25Stnn device_t ret;
903bee3e25Stnn u_int address_cells;
913bee3e25Stnn
923bee3e25Stnn devs = prop_dictionary_create();
933bee3e25Stnn if (of_getprop_uint32(phandle, "#address-cells", &address_cells))
943bee3e25Stnn address_cells = 1;
953bee3e25Stnn of_enter_spi_devs(devs, phandle, address_cells * 4);
963bee3e25Stnn
973bee3e25Stnn spi = fdtbus_get_spi_controller(phandle);
983bee3e25Stnn KASSERT(spi != NULL);
993bee3e25Stnn memset(&sba, 0, sizeof(sba));
1003bee3e25Stnn sba.sba_controller = spi;
1013bee3e25Stnn
1023bee3e25Stnn sba.sba_child_devices = prop_dictionary_get(devs, "spi-child-devices");
1033bee3e25Stnn if (sba.sba_child_devices)
1043bee3e25Stnn prop_object_retain(sba.sba_child_devices);
1053bee3e25Stnn prop_object_release(devs);
1063bee3e25Stnn
1072685996bSthorpej ret = config_found(dev, &sba, print,
108*c7fb772bSthorpej CFARGS(.iattr = "spibus"));
1093bee3e25Stnn if (sba.sba_child_devices)
1103bee3e25Stnn prop_object_release(sba.sba_child_devices);
1113bee3e25Stnn
1123bee3e25Stnn return ret;
1133bee3e25Stnn }
1143bee3e25Stnn
1153bee3e25Stnn
116