xref: /freebsd/sys/dev/spibus/ofw_spibus.c (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
35 
36 #include <sys/param.h>
37 #include <sys/bus.h>
38 #include <sys/kernel.h>
39 #include <sys/libkern.h>
40 #include <sys/lock.h>
41 #include <sys/module.h>
42 #include <sys/mutex.h>
43 
44 #include <dev/fdt/fdt_common.h>
45 #include <dev/spibus/spi.h>
46 #include <dev/spibus/spibusvar.h>
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/ofw_bus_subr.h>
49 #include <dev/ofw/openfirm.h>
50 
51 #include "spibus_if.h"
52 
53 struct ofw_spibus_devinfo {
54 	struct spibus_ivar	opd_dinfo;
55 	struct ofw_bus_devinfo	opd_obdinfo;
56 };
57 
58 /* Methods */
59 static device_probe_t ofw_spibus_probe;
60 static device_attach_t ofw_spibus_attach;
61 static device_t ofw_spibus_add_child(device_t dev, u_int order,
62     const char *name, int unit);
63 static const struct ofw_bus_devinfo *ofw_spibus_get_devinfo(device_t bus,
64     device_t dev);
65 
66 static int
67 ofw_spibus_probe(device_t dev)
68 {
69 
70 	if (ofw_bus_get_node(dev) == -1)
71 		return (ENXIO);
72 	device_set_desc(dev, "OFW SPI bus");
73 
74 	return (BUS_PROBE_DEFAULT);
75 }
76 
77 static int
78 ofw_spibus_attach(device_t dev)
79 {
80 	struct spibus_softc *sc = device_get_softc(dev);
81 	struct ofw_spibus_devinfo *dinfo;
82 	phandle_t child;
83 	pcell_t clock, paddr;
84 	device_t childdev;
85 	uint32_t mode = SPIBUS_MODE_NONE;
86 
87 	sc->dev = dev;
88 
89 	bus_generic_probe(dev);
90 	bus_enumerate_hinted_children(dev);
91 
92 	/*
93 	 * Attach those children represented in the device tree.
94 	 */
95 	for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
96 	    child = OF_peer(child)) {
97 		/*
98 		 * Try to get the CS number first from the spi-chipselect
99 		 * property, then try the reg property.
100 		 */
101 		if (OF_getencprop(child, "spi-chipselect", &paddr,
102 		    sizeof(paddr)) == -1) {
103 			if (OF_getencprop(child, "reg", &paddr,
104 			    sizeof(paddr)) == -1)
105 				continue;
106 		}
107 
108 		/*
109 		 * Try to get the cpol/cpha mode
110 		 */
111 		if (OF_hasprop(child, "spi-cpol"))
112 			mode = SPIBUS_MODE_CPOL;
113 		if (OF_hasprop(child, "spi-cpha")) {
114 			if (mode == SPIBUS_MODE_CPOL)
115 				mode = SPIBUS_MODE_CPOL_CPHA;
116 			else
117 				mode = SPIBUS_MODE_CPHA;
118 		}
119 
120 		/*
121 		 * Try to get the CS polarity
122 		 */
123 		if (OF_hasprop(child, "spi-cs-high"))
124 			paddr |= SPIBUS_CS_HIGH;
125 
126 		/*
127 		 * Get the maximum clock frequency for device, zero means
128 		 * use the default bus speed.
129 		 *
130 		 * XXX Note that the current (2018-04-07) dts bindings say that
131 		 * spi-max-frequency is a required property (but says nothing of
132 		 * how to interpret a value of zero).
133 		 */
134 		if (OF_getencprop(child, "spi-max-frequency", &clock,
135 		    sizeof(clock)) == -1)
136 			clock = 0;
137 
138 		/*
139 		 * Now set up the SPI and OFW bus layer devinfo and add it
140 		 * to the bus.
141 		 */
142 		dinfo = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
143 		    M_NOWAIT | M_ZERO);
144 		if (dinfo == NULL)
145 			continue;
146 		dinfo->opd_dinfo.cs = paddr;
147 		dinfo->opd_dinfo.clock = clock;
148 		dinfo->opd_dinfo.mode = mode;
149 		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
150 		    0) {
151 			free(dinfo, M_DEVBUF);
152 			continue;
153 		}
154 		childdev = device_add_child(dev, NULL, -1);
155 		device_set_ivars(childdev, dinfo);
156 	}
157 
158 	return (bus_generic_attach(dev));
159 }
160 
161 static device_t
162 ofw_spibus_add_child(device_t dev, u_int order, const char *name, int unit)
163 {
164 	device_t child;
165 	struct ofw_spibus_devinfo *devi;
166 
167 	child = device_add_child_ordered(dev, order, name, unit);
168 	if (child == NULL)
169 		return (child);
170 	devi = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
171 	    M_NOWAIT | M_ZERO);
172 	if (devi == NULL) {
173 		device_delete_child(dev, child);
174 		return (0);
175 	}
176 
177 	/*
178 	 * NULL all the OFW-related parts of the ivars for non-OFW
179 	 * children.
180 	 */
181 	devi->opd_obdinfo.obd_node = -1;
182 	devi->opd_obdinfo.obd_name = NULL;
183 	devi->opd_obdinfo.obd_compat = NULL;
184 	devi->opd_obdinfo.obd_type = NULL;
185 	devi->opd_obdinfo.obd_model = NULL;
186 
187 	device_set_ivars(child, devi);
188 
189 	return (child);
190 }
191 
192 static const struct ofw_bus_devinfo *
193 ofw_spibus_get_devinfo(device_t bus, device_t dev)
194 {
195 	struct ofw_spibus_devinfo *dinfo;
196 
197 	dinfo = device_get_ivars(dev);
198 	return (&dinfo->opd_obdinfo);
199 }
200 
201 static device_method_t ofw_spibus_methods[] = {
202 	/* Device interface */
203 	DEVMETHOD(device_probe,		ofw_spibus_probe),
204 	DEVMETHOD(device_attach,	ofw_spibus_attach),
205 
206 	/* Bus interface */
207 	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
208 	DEVMETHOD(bus_add_child,	ofw_spibus_add_child),
209 
210 	/* ofw_bus interface */
211 	DEVMETHOD(ofw_bus_get_devinfo,	ofw_spibus_get_devinfo),
212 	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
213 	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
214 	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
215 	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
216 	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
217 
218 	DEVMETHOD_END
219 };
220 
221 devclass_t ofw_spibus_devclass;
222 
223 DEFINE_CLASS_1(spibus, ofw_spibus_driver, ofw_spibus_methods,
224     sizeof(struct spibus_softc), spibus_driver);
225 DRIVER_MODULE(ofw_spibus, spi, ofw_spibus_driver, ofw_spibus_devclass, 0, 0);
226 MODULE_VERSION(ofw_spibus, 1);
227 MODULE_DEPEND(ofw_spibus, spibus, 1, 1, 1);
228