fdt_spi.c revision 1.11 1 1.11 thorpej /* $NetBSD: fdt_spi.c,v 1.11 2025/09/10 04:17:19 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.11 thorpej __KERNEL_RCSID(0, "$NetBSD: fdt_spi.c,v 1.11 2025/09/10 04:17:19 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.11 thorpej /* XXX Maybe garbage-collect this. */
54 1.1 tnn int
55 1.5 thorpej fdtbus_register_spi_controller(device_t dev,
56 1.7 thorpej const 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.4 thorpej spi->spi_controller = controller;
63 1.1 tnn
64 1.1 tnn LIST_INSERT_HEAD(&fdtbus_spi_controllers, spi, spi_next);
65 1.1 tnn
66 1.1 tnn return 0;
67 1.1 tnn }
68 1.1 tnn
69 1.11 thorpej #if 0
70 1.7 thorpej static const 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.5 thorpej if (phandle ==
77 1.5 thorpej devhandle_to_of(device_handle(spi->spi_dev))) {
78 1.4 thorpej return spi->spi_controller;
79 1.1 tnn }
80 1.1 tnn }
81 1.1 tnn return NULL;
82 1.1 tnn }
83 1.11 thorpej #endif
84