fdt_spi.c revision 1.7 1 1.7 thorpej /* $NetBSD: fdt_spi.c,v 1.7 2025/09/10 03:23:27 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.7 thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.7 2025/09/10 03:23:27 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.7 thorpej const struct spi_controller *spi_controller;
47 1.1 tnn LIST_ENTRY(fdtbus_spi_controller) spi_next;
48 1.1 tnn };
49 1.1 tnn
50 1.1 tnn static LIST_HEAD(, fdtbus_spi_controller) fdtbus_spi_controllers =
51 1.1 tnn LIST_HEAD_INITIALIZER(fdtbus_spi_controllers);
52 1.1 tnn
53 1.1 tnn int
54 1.5 thorpej fdtbus_register_spi_controller(device_t dev,
55 1.7 thorpej const struct spi_controller *controller)
56 1.1 tnn {
57 1.1 tnn struct fdtbus_spi_controller *spi;
58 1.1 tnn
59 1.1 tnn spi = kmem_alloc(sizeof(*spi), KM_SLEEP);
60 1.1 tnn spi->spi_dev = dev;
61 1.4 thorpej spi->spi_controller = controller;
62 1.1 tnn
63 1.1 tnn LIST_INSERT_HEAD(&fdtbus_spi_controllers, spi, spi_next);
64 1.1 tnn
65 1.1 tnn return 0;
66 1.1 tnn }
67 1.1 tnn
68 1.7 thorpej static const struct spi_controller *
69 1.1 tnn fdtbus_get_spi_controller(int phandle)
70 1.1 tnn {
71 1.1 tnn struct fdtbus_spi_controller *spi;
72 1.1 tnn
73 1.1 tnn LIST_FOREACH(spi, &fdtbus_spi_controllers, spi_next) {
74 1.5 thorpej if (phandle ==
75 1.5 thorpej devhandle_to_of(device_handle(spi->spi_dev))) {
76 1.4 thorpej return spi->spi_controller;
77 1.1 tnn }
78 1.1 tnn }
79 1.1 tnn return NULL;
80 1.1 tnn }
81 1.1 tnn
82 1.1 tnn device_t
83 1.5 thorpej fdtbus_attach_spibus(device_t dev, cfprint_t print)
84 1.1 tnn {
85 1.7 thorpej const struct spi_controller *spi;
86 1.5 thorpej int phandle = devhandle_to_of(device_handle(dev));
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.6 thorpej CFARGS(.iattr = "spibus",
109 1.6 thorpej .devhandle = device_handle(dev)));
110 1.1 tnn if (sba.sba_child_devices)
111 1.1 tnn prop_object_release(sba.sba_child_devices);
112 1.1 tnn
113 1.1 tnn return ret;
114 1.1 tnn }
115