1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
5 * Copyright (c) 2013 The FreeBSD Foundation
6 * All rights reserved.
7 *
8 * Portions of this software were developed by Oleksandr Rybalko
9 * under sponsorship from the FreeBSD Foundation.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice unmodified, this list of conditions, and the following
16 * disclaimer.
17 * 2. Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in the
19 * documentation and/or other materials provided with the distribution.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 #include <sys/cdefs.h>
34 #include <sys/param.h>
35 #include <sys/bus.h>
36 #include <sys/kernel.h>
37 #include <sys/libkern.h>
38 #include <sys/lock.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
41
42 #include <dev/fdt/fdt_common.h>
43 #include <dev/spibus/spi.h>
44 #include <dev/spibus/spibusvar.h>
45 #include <dev/ofw/ofw_bus.h>
46 #include <dev/ofw/ofw_bus_subr.h>
47 #include <dev/ofw/openfirm.h>
48
49 #include "spibus_if.h"
50
51 struct ofw_spibus_devinfo {
52 struct spibus_ivar opd_dinfo;
53 struct ofw_bus_devinfo opd_obdinfo;
54 };
55
56 /* Methods */
57 static device_probe_t ofw_spibus_probe;
58 static device_attach_t ofw_spibus_attach;
59 static device_t ofw_spibus_add_child(device_t dev, u_int order,
60 const char *name, int unit);
61 static const struct ofw_bus_devinfo *ofw_spibus_get_devinfo(device_t bus,
62 device_t dev);
63
64 static int
ofw_spibus_probe(device_t dev)65 ofw_spibus_probe(device_t dev)
66 {
67
68 if (ofw_bus_get_node(dev) == -1)
69 return (ENXIO);
70 device_set_desc(dev, "OFW SPI bus");
71
72 return (BUS_PROBE_DEFAULT);
73 }
74
75 static int
ofw_spibus_attach(device_t dev)76 ofw_spibus_attach(device_t dev)
77 {
78 struct spibus_softc *sc = device_get_softc(dev);
79 struct ofw_spibus_devinfo *dinfo;
80 phandle_t child;
81 pcell_t clock, paddr;
82 device_t childdev;
83 uint32_t mode = SPIBUS_MODE_NONE;
84
85 sc->dev = dev;
86
87 bus_generic_probe(dev);
88 bus_enumerate_hinted_children(dev);
89
90 /*
91 * Attach those children represented in the device tree.
92 */
93 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
94 child = OF_peer(child)) {
95 /*
96 * Try to get the CS number first from the spi-chipselect
97 * property, then try the reg property.
98 */
99 if (OF_getencprop(child, "spi-chipselect", &paddr,
100 sizeof(paddr)) == -1) {
101 if (OF_getencprop(child, "reg", &paddr,
102 sizeof(paddr)) == -1)
103 continue;
104 }
105
106 /*
107 * Try to get the cpol/cpha mode
108 */
109 if (OF_hasprop(child, "spi-cpol"))
110 mode = SPIBUS_MODE_CPOL;
111 if (OF_hasprop(child, "spi-cpha")) {
112 if (mode == SPIBUS_MODE_CPOL)
113 mode = SPIBUS_MODE_CPOL_CPHA;
114 else
115 mode = SPIBUS_MODE_CPHA;
116 }
117
118 /*
119 * Try to get the CS polarity
120 */
121 if (OF_hasprop(child, "spi-cs-high"))
122 paddr |= SPIBUS_CS_HIGH;
123
124 /*
125 * Get the maximum clock frequency for device, zero means
126 * use the default bus speed.
127 *
128 * XXX Note that the current (2018-04-07) dts bindings say that
129 * spi-max-frequency is a required property (but says nothing of
130 * how to interpret a value of zero).
131 */
132 if (OF_getencprop(child, "spi-max-frequency", &clock,
133 sizeof(clock)) == -1)
134 clock = 0;
135
136 /*
137 * Now set up the SPI and OFW bus layer devinfo and add it
138 * to the bus.
139 */
140 dinfo = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
141 M_NOWAIT | M_ZERO);
142 if (dinfo == NULL)
143 continue;
144 dinfo->opd_dinfo.cs = paddr;
145 dinfo->opd_dinfo.clock = clock;
146 dinfo->opd_dinfo.mode = mode;
147 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
148 0) {
149 free(dinfo, M_DEVBUF);
150 continue;
151 }
152 childdev = device_add_child(dev, NULL, -1);
153
154 resource_list_init(&dinfo->opd_dinfo.rl);
155 ofw_bus_intr_to_rl(childdev, child,
156 &dinfo->opd_dinfo.rl, NULL);
157 device_set_ivars(childdev, dinfo);
158 }
159
160 return (bus_generic_attach(dev));
161 }
162
163 static device_t
ofw_spibus_add_child(device_t dev,u_int order,const char * name,int unit)164 ofw_spibus_add_child(device_t dev, u_int order, const char *name, int unit)
165 {
166 device_t child;
167 struct ofw_spibus_devinfo *devi;
168
169 child = device_add_child_ordered(dev, order, name, unit);
170 if (child == NULL)
171 return (child);
172 devi = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
173 M_NOWAIT | M_ZERO);
174 if (devi == NULL) {
175 device_delete_child(dev, child);
176 return (0);
177 }
178
179 /*
180 * NULL all the OFW-related parts of the ivars for non-OFW
181 * children.
182 */
183 devi->opd_obdinfo.obd_node = -1;
184 devi->opd_obdinfo.obd_name = NULL;
185 devi->opd_obdinfo.obd_compat = NULL;
186 devi->opd_obdinfo.obd_type = NULL;
187 devi->opd_obdinfo.obd_model = NULL;
188
189 device_set_ivars(child, devi);
190
191 return (child);
192 }
193
194 static const struct ofw_bus_devinfo *
ofw_spibus_get_devinfo(device_t bus,device_t dev)195 ofw_spibus_get_devinfo(device_t bus, device_t dev)
196 {
197 struct ofw_spibus_devinfo *dinfo;
198
199 dinfo = device_get_ivars(dev);
200 return (&dinfo->opd_obdinfo);
201 }
202
203 static struct resource_list *
ofw_spibus_get_resource_list(device_t bus __unused,device_t child)204 ofw_spibus_get_resource_list(device_t bus __unused, device_t child)
205 {
206 struct spibus_ivar *devi;
207
208 devi = SPIBUS_IVAR(child);
209 return (&devi->rl);
210 }
211
212 static device_method_t ofw_spibus_methods[] = {
213 /* Device interface */
214 DEVMETHOD(device_probe, ofw_spibus_probe),
215 DEVMETHOD(device_attach, ofw_spibus_attach),
216
217 /* Bus interface */
218 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
219 DEVMETHOD(bus_add_child, ofw_spibus_add_child),
220 DEVMETHOD(bus_get_resource_list, ofw_spibus_get_resource_list),
221
222 /* ofw_bus interface */
223 DEVMETHOD(ofw_bus_get_devinfo, ofw_spibus_get_devinfo),
224 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
225 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
226 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
227 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
228 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
229
230 DEVMETHOD_END
231 };
232
233 devclass_t ofw_spibus_devclass;
234
235 DEFINE_CLASS_1(spibus, ofw_spibus_driver, ofw_spibus_methods,
236 sizeof(struct spibus_softc), spibus_driver);
237 DRIVER_MODULE(ofw_spibus, spi, ofw_spibus_driver, ofw_spibus_devclass, 0, 0);
238 MODULE_VERSION(ofw_spibus, 1);
239 MODULE_DEPEND(ofw_spibus, spibus, 1, 1, 1);
240