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