fdt_spi.c revision 1.12 1 1.12 thorpej /* $NetBSD: fdt_spi.c,v 1.12 2025/09/14 00:28:43 thorpej Exp $ */
2 1.12 thorpej
3 1.12 thorpej /*
4 1.12 thorpej * Copyright (c) 2021, 2022 The NetBSD Foundation, Inc.
5 1.12 thorpej * All rights reserved.
6 1.12 thorpej *
7 1.12 thorpej * Redistribution and use in source and binary forms, with or without
8 1.12 thorpej * modification, are permitted provided that the following conditions
9 1.12 thorpej * are met:
10 1.12 thorpej * 1. Redistributions of source code must retain the above copyright
11 1.12 thorpej * notice, this list of conditions and the following disclaimer.
12 1.12 thorpej * 2. Redistributions in binary form must reproduce the above copyright
13 1.12 thorpej * notice, this list of conditions and the following disclaimer in the
14 1.12 thorpej * documentation and/or other materials provided with the distribution.
15 1.12 thorpej *
16 1.12 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
17 1.12 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
18 1.12 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 1.12 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
20 1.12 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 1.12 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 1.12 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 1.12 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 1.12 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 1.12 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 1.12 thorpej * POSSIBILITY OF SUCH DAMAGE.
27 1.12 thorpej */
28 1.1 tnn
29 1.1 tnn /*
30 1.1 tnn * Copyright (c) 2019 The NetBSD Foundation, Inc.
31 1.1 tnn * All rights reserved.
32 1.1 tnn *
33 1.1 tnn * This code is derived from software contributed to The NetBSD Foundation
34 1.1 tnn * by Tobias Nygren.
35 1.1 tnn *
36 1.1 tnn * Redistribution and use in source and binary forms, with or without
37 1.1 tnn * modification, are permitted provided that the following conditions
38 1.1 tnn * are met:
39 1.1 tnn * 1. Redistributions of source code must retain the above copyright
40 1.1 tnn * notice, this list of conditions and the following disclaimer.
41 1.1 tnn * 2. Redistributions in binary form must reproduce the above copyright
42 1.1 tnn * notice, this list of conditions and the following disclaimer in the
43 1.1 tnn * documentation and/or other materials provided with the distribution.
44 1.1 tnn *
45 1.1 tnn * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
46 1.1 tnn * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
47 1.1 tnn * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
48 1.1 tnn * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
49 1.1 tnn * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
50 1.1 tnn * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
51 1.1 tnn * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
52 1.1 tnn * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
53 1.1 tnn * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54 1.1 tnn * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
55 1.1 tnn * POSSIBILITY OF SUCH DAMAGE.
56 1.1 tnn */
57 1.1 tnn
58 1.1 tnn #include <sys/cdefs.h>
59 1.12 thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.12 2025/09/14 00:28:43 thorpej Exp $");
60 1.1 tnn
61 1.1 tnn #include <sys/param.h>
62 1.1 tnn #include <sys/device.h>
63 1.1 tnn #include <sys/kmem.h>
64 1.1 tnn #include <sys/queue.h>
65 1.12 thorpej
66 1.1 tnn #include <dev/fdt/fdtvar.h>
67 1.1 tnn
68 1.12 thorpej #include <dev/ofw/openfirm.h>
69 1.12 thorpej
70 1.12 thorpej #include <dev/spi/spi_calls.h>
71 1.12 thorpej
72 1.1 tnn struct fdtbus_spi_controller {
73 1.1 tnn device_t spi_dev;
74 1.7 thorpej const struct spi_controller *spi_controller;
75 1.1 tnn LIST_ENTRY(fdtbus_spi_controller) spi_next;
76 1.1 tnn };
77 1.1 tnn
78 1.1 tnn static LIST_HEAD(, fdtbus_spi_controller) fdtbus_spi_controllers =
79 1.1 tnn LIST_HEAD_INITIALIZER(fdtbus_spi_controllers);
80 1.1 tnn
81 1.11 thorpej /* XXX Maybe garbage-collect this. */
82 1.1 tnn int
83 1.5 thorpej fdtbus_register_spi_controller(device_t dev,
84 1.7 thorpej const struct spi_controller *controller)
85 1.1 tnn {
86 1.1 tnn struct fdtbus_spi_controller *spi;
87 1.1 tnn
88 1.1 tnn spi = kmem_alloc(sizeof(*spi), KM_SLEEP);
89 1.1 tnn spi->spi_dev = dev;
90 1.4 thorpej spi->spi_controller = controller;
91 1.1 tnn
92 1.1 tnn LIST_INSERT_HEAD(&fdtbus_spi_controllers, spi, spi_next);
93 1.1 tnn
94 1.1 tnn return 0;
95 1.1 tnn }
96 1.1 tnn
97 1.11 thorpej #if 0
98 1.7 thorpej static const struct spi_controller *
99 1.1 tnn fdtbus_get_spi_controller(int phandle)
100 1.1 tnn {
101 1.1 tnn struct fdtbus_spi_controller *spi;
102 1.1 tnn
103 1.1 tnn LIST_FOREACH(spi, &fdtbus_spi_controllers, spi_next) {
104 1.5 thorpej if (phandle ==
105 1.5 thorpej devhandle_to_of(device_handle(spi->spi_dev))) {
106 1.4 thorpej return spi->spi_controller;
107 1.1 tnn }
108 1.1 tnn }
109 1.1 tnn return NULL;
110 1.1 tnn }
111 1.11 thorpej #endif
112 1.12 thorpej
113 1.12 thorpej static int
114 1.12 thorpej fdtbus_spi_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
115 1.12 thorpej {
116 1.12 thorpej struct spi_enumerate_devices_args *args = v;
117 1.12 thorpej int spi_node, node;
118 1.12 thorpej char name[32], compat_buf[32];
119 1.12 thorpej bus_addr_t chip_select;
120 1.12 thorpej char *clist;
121 1.12 thorpej int clist_size;
122 1.12 thorpej bool cbrv;
123 1.12 thorpej
124 1.12 thorpej spi_node = devhandle_to_of(call_handle);
125 1.12 thorpej
126 1.12 thorpej for (node = OF_child(spi_node); node != 0; node = OF_peer(node)) {
127 1.12 thorpej if (OF_getprop(node, "name", name, sizeof(name)) <= 0) {
128 1.12 thorpej continue;
129 1.12 thorpej }
130 1.12 thorpej
131 1.12 thorpej if (fdtbus_get_reg(node, 0, &chip_select, NULL) != 0) {
132 1.12 thorpej continue;
133 1.12 thorpej }
134 1.12 thorpej
135 1.12 thorpej /* Device Tree bindings specify a max chip select of 256. */
136 1.12 thorpej if (chip_select > 256) {
137 1.12 thorpej continue;
138 1.12 thorpej }
139 1.12 thorpej
140 1.12 thorpej clist_size = OF_getproplen(node, "compatible");
141 1.12 thorpej if (clist_size <= 0) {
142 1.12 thorpej continue;
143 1.12 thorpej }
144 1.12 thorpej
145 1.12 thorpej clist = kmem_tmpbuf_alloc(clist_size,
146 1.12 thorpej compat_buf, sizeof(compat_buf), KM_SLEEP);
147 1.12 thorpej if (OF_getprop(node, "compatible", clist, clist_size) <
148 1.12 thorpej clist_size) {
149 1.12 thorpej kmem_tmpbuf_free(clist, clist_size, compat_buf);
150 1.12 thorpej continue;
151 1.12 thorpej }
152 1.12 thorpej
153 1.12 thorpej args->chip_select = (int)chip_select;
154 1.12 thorpej args->sa->sa_name = name;
155 1.12 thorpej args->sa->sa_clist = clist;
156 1.12 thorpej args->sa->sa_clist_size = clist_size;
157 1.12 thorpej args->sa->sa_devhandle = devhandle_from_of(call_handle, node);
158 1.12 thorpej
159 1.12 thorpej cbrv = args->callback(dev, args);
160 1.12 thorpej
161 1.12 thorpej kmem_tmpbuf_free(clist, clist_size, compat_buf);
162 1.12 thorpej
163 1.12 thorpej if (!cbrv) {
164 1.12 thorpej break;
165 1.12 thorpej }
166 1.12 thorpej }
167 1.12 thorpej
168 1.12 thorpej return 0;
169 1.12 thorpej }
170 1.12 thorpej OF_DEVICE_CALL_REGISTER(SPI_ENUMERATE_DEVICES_STR,
171 1.12 thorpej fdtbus_spi_enumerate_devices);
172