2 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
3 * Copyright (c) 2013 The FreeBSD Foundation
6 * Portions of this software were developed by Oleksandr Rybalko
7 * under sponsorship from the FreeBSD Foundation.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
36 #include <sys/kernel.h>
37 #include <sys/libkern.h>
39 #include <sys/module.h>
40 #include <sys/mutex.h>
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>
49 #include "spibus_if.h"
51 struct ofw_spibus_devinfo {
52 struct spibus_ivar opd_dinfo;
53 struct ofw_bus_devinfo opd_obdinfo;
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,
65 ofw_spibus_probe(device_t dev)
68 if (ofw_bus_get_node(dev) == -1)
70 device_set_desc(dev, "OFW SPI bus");
76 ofw_spibus_attach(device_t dev)
78 struct spibus_softc *sc = device_get_softc(dev);
79 struct ofw_spibus_devinfo *dinfo;
83 uint32_t mode = SPIBUS_MODE_NONE;
87 bus_generic_probe(dev);
88 bus_enumerate_hinted_children(dev);
91 * Attach those children represented in the device tree.
93 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
94 child = OF_peer(child)) {
96 * Try to get the CS number first from the spi-chipselect
97 * property, then try the reg property.
99 if (OF_getencprop(child, "spi-chipselect", &paddr,
100 sizeof(paddr)) == -1) {
101 if (OF_getencprop(child, "reg", &paddr,
102 sizeof(paddr)) == -1)
107 * Try to get the cpol/cpha mode
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;
115 mode = SPIBUS_MODE_CPHA;
119 * Try to get the CS polarity
121 if (OF_hasprop(child, "spi-cs-high"))
122 paddr |= SPIBUS_CS_HIGH;
125 * Get the maximum clock frequency for device, zero means
126 * use the default bus speed.
128 if (OF_getencprop(child, "spi-max-frequency", &clock,
129 sizeof(clock)) == -1)
133 * Now set up the SPI and OFW bus layer devinfo and add it
136 dinfo = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
140 dinfo->opd_dinfo.cs = paddr;
141 dinfo->opd_dinfo.clock = clock;
142 dinfo->opd_dinfo.mode = mode;
143 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
145 free(dinfo, M_DEVBUF);
148 childdev = device_add_child(dev, NULL, -1);
149 device_set_ivars(childdev, dinfo);
152 return (bus_generic_attach(dev));
156 ofw_spibus_add_child(device_t dev, u_int order, const char *name, int unit)
159 struct ofw_spibus_devinfo *devi;
161 child = device_add_child_ordered(dev, order, name, unit);
164 devi = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
167 device_delete_child(dev, child);
172 * NULL all the OFW-related parts of the ivars for non-OFW
175 devi->opd_obdinfo.obd_node = -1;
176 devi->opd_obdinfo.obd_name = NULL;
177 devi->opd_obdinfo.obd_compat = NULL;
178 devi->opd_obdinfo.obd_type = NULL;
179 devi->opd_obdinfo.obd_model = NULL;
181 device_set_ivars(child, devi);
186 static const struct ofw_bus_devinfo *
187 ofw_spibus_get_devinfo(device_t bus, device_t dev)
189 struct ofw_spibus_devinfo *dinfo;
191 dinfo = device_get_ivars(dev);
192 return (&dinfo->opd_obdinfo);
195 static device_method_t ofw_spibus_methods[] = {
196 /* Device interface */
197 DEVMETHOD(device_probe, ofw_spibus_probe),
198 DEVMETHOD(device_attach, ofw_spibus_attach),
201 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
202 DEVMETHOD(bus_add_child, ofw_spibus_add_child),
204 /* ofw_bus interface */
205 DEVMETHOD(ofw_bus_get_devinfo, ofw_spibus_get_devinfo),
206 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
207 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
208 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
209 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
210 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
215 static devclass_t ofwspibus_devclass;
217 DEFINE_CLASS_1(spibus, ofw_spibus_driver, ofw_spibus_methods,
218 sizeof(struct spibus_softc), spibus_driver);
219 DRIVER_MODULE(ofw_spibus, spi, ofw_spibus_driver, ofwspibus_devclass, 0, 0);
220 MODULE_VERSION(ofw_spibus, 1);
221 MODULE_DEPEND(ofw_spibus, spibus, 1, 1, 1);