1 /*-
2 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
3 * Copyright (c) 2013 The FreeBSD Foundation
4 * All rights reserved.
5 *
6 * Portions of this software were developed by Oleksandr Rybalko
7 * under sponsorship from the FreeBSD Foundation.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice unmodified, this list of conditions, and the following
14 * disclaimer.
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.
18 *
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.
29 */
30
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33
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
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 (0);
73 }
74
75 static int
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 paddr;
82 device_t childdev;
83
84 sc->dev = dev;
85
86 bus_generic_probe(dev);
87 bus_enumerate_hinted_children(dev);
88
89 /*
90 * Attach those children represented in the device tree.
91 */
92 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
93 child = OF_peer(child)) {
94 /*
95 * Try to get the CS number first from the spi-chipselect
96 * property, then try the reg property.
97 */
98 if (OF_getencprop(child, "spi-chipselect", &paddr,
99 sizeof(paddr)) == -1) {
100 if (OF_getencprop(child, "reg", &paddr,
101 sizeof(paddr)) == -1)
102 continue;
103 }
104
105 /*
106 * Now set up the SPI and OFW bus layer devinfo and add it
107 * to the bus.
108 */
109 dinfo = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
110 M_NOWAIT | M_ZERO);
111 if (dinfo == NULL)
112 continue;
113 dinfo->opd_dinfo.cs = paddr;
114 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
115 0) {
116 free(dinfo, M_DEVBUF);
117 continue;
118 }
119 childdev = device_add_child(dev, NULL, -1);
120 device_set_ivars(childdev, dinfo);
121 }
122
123 return (bus_generic_attach(dev));
124 }
125
126 static device_t
127 ofw_spibus_add_child(device_t dev, u_int order, const char *name, int unit)
128 {
129 device_t child;
130 struct ofw_spibus_devinfo *devi;
131
132 child = device_add_child_ordered(dev, order, name, unit);
133 if (child == NULL)
134 return (child);
135 devi = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF,
136 M_NOWAIT | M_ZERO);
137 if (devi == NULL) {
138 device_delete_child(dev, child);
139 return (0);
140 }
141
142 /*
143 * NULL all the OFW-related parts of the ivars for non-OFW
144 * children.
145 */
146 devi->opd_obdinfo.obd_node = -1;
147 devi->opd_obdinfo.obd_name = NULL;
148 devi->opd_obdinfo.obd_compat = NULL;
149 devi->opd_obdinfo.obd_type = NULL;
150 devi->opd_obdinfo.obd_model = NULL;
151
152 device_set_ivars(child, devi);
153
154 return (child);
155 }
156
157 static const struct ofw_bus_devinfo *
158 ofw_spibus_get_devinfo(device_t bus, device_t dev)
159 {
160 struct ofw_spibus_devinfo *dinfo;
161
162 dinfo = device_get_ivars(dev);
163 return (&dinfo->opd_obdinfo);
164 }
165
166 static device_method_t ofw_spibus_methods[] = {
167 /* Device interface */
168 DEVMETHOD(device_probe, ofw_spibus_probe),
169 DEVMETHOD(device_attach, ofw_spibus_attach),
170
171 /* Bus interface */
172 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
173 DEVMETHOD(bus_add_child, ofw_spibus_add_child),
174
175 /* ofw_bus interface */
176 DEVMETHOD(ofw_bus_get_devinfo, ofw_spibus_get_devinfo),
177 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
178 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
179 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
180 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
181 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
182
183 DEVMETHOD_END
184 };
185
186 static devclass_t ofwspibus_devclass;
187
188 DEFINE_CLASS_1(spibus, ofw_spibus_driver, ofw_spibus_methods,
189 sizeof(struct spibus_softc), spibus_driver);
190 DRIVER_MODULE(ofw_spibus, spi, ofw_spibus_driver, ofwspibus_devclass, 0, 0);
191 MODULE_VERSION(ofw_spibus, 1);
192 MODULE_DEPEND(ofw_spibus, spibus, 1, 1, 1);
Cache object: 36e85432ee539ef030f311a47c09e279
|