FreeBSD kernel IICBUS device code
ofw_iicbus.c
Go to the documentation of this file.
1/*-
2 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice unmodified, this list of conditions, and the following
10 * disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include <sys/cdefs.h>
28__FBSDID("$FreeBSD$");
29
30#include <sys/param.h>
31#include <sys/bus.h>
32#include <sys/kernel.h>
33#include <sys/libkern.h>
34#include <sys/lock.h>
35#include <sys/module.h>
36#include <sys/mutex.h>
37
38#include <dev/iicbus/iicbus.h>
39#include <dev/iicbus/iiconf.h>
40#include <dev/ofw/ofw_bus.h>
41#include <dev/ofw/ofw_bus_subr.h>
42#include <dev/ofw/openfirm.h>
43
44#include "iicbus_if.h"
45#include "ofw_iicbus_if.h"
46
47/* Methods */
48static device_probe_t ofw_iicbus_probe;
49static device_attach_t ofw_iicbus_attach;
50static device_t ofw_iicbus_add_child(device_t dev, u_int order,
51 const char *name, int unit);
52static const struct ofw_bus_devinfo *ofw_iicbus_get_devinfo(device_t bus,
53 device_t dev);
54static int ofw_iicbus_set_devinfo(device_t bus, device_t dev,
55 phandle_t ofw_node, char *ofw_name, char *ofw_compat, uint32_t i2c_addr);
56
57static device_method_t ofw_iicbus_methods[] = {
58 /* Device interface */
59 DEVMETHOD(device_probe, ofw_iicbus_probe),
60 DEVMETHOD(device_attach, ofw_iicbus_attach),
61
62 /* Bus interface */
63 DEVMETHOD(bus_child_pnpinfo, ofw_bus_gen_child_pnpinfo),
64 DEVMETHOD(bus_add_child, ofw_iicbus_add_child),
65
66 /* ofw_bus interface */
67 DEVMETHOD(ofw_bus_get_devinfo, ofw_iicbus_get_devinfo),
68 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
69 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
70 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
71 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
72 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
73
74 /* ofw_iicbus interface */
76
77 DEVMETHOD_END
78};
79
81 struct iicbus_ivar opd_dinfo; /* Must be the first. */
82 struct ofw_bus_devinfo opd_obdinfo;
83};
84
86
88 sizeof(struct iicbus_softc), iicbus_driver);
90 0, 0, BUS_PASS_BUS);
92 0, 0, BUS_PASS_BUS);
94 0, 0, BUS_PASS_BUS);
97
98static int
100{
101
102 if (ofw_bus_get_node(dev) == -1)
103 return (ENXIO);
104 device_set_desc(dev, "OFW I2C bus");
105
106 return (0);
107}
108
109static int
111{
112 struct iicbus_softc *sc = IICBUS_SOFTC(dev);
113 struct ofw_iicbus_devinfo *dinfo;
114 phandle_t child, node, root;
115 pcell_t freq, paddr;
116 device_t childdev;
117 ssize_t compatlen;
118 char compat[255];
119 char *curstr;
120 u_int iic_addr_8bit = 0;
121
122 sc->dev = dev;
123 mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
124
125 /*
126 * If there is a clock-frequency property for the device node, use it as
127 * the starting value for the bus frequency. Then call the common
128 * routine that handles the tunable/sysctl which allows the FDT value to
129 * be overridden by the user.
130 */
131 node = ofw_bus_get_node(dev);
132 freq = 0;
133 OF_getencprop(node, "clock-frequency", &freq, sizeof(freq));
135
136 iicbus_reset(dev, IIC_FASTEST, 0, NULL);
137
138 bus_generic_probe(dev);
139 bus_enumerate_hinted_children(dev);
140
141 /*
142 * Check if we're running on a PowerMac, needed for the I2C
143 * address below.
144 */
145 root = OF_peer(0);
146 compatlen = OF_getprop(root, "compatible", compat,
147 sizeof(compat));
148 if (compatlen != -1) {
149 for (curstr = compat; curstr < compat + compatlen;
150 curstr += strlen(curstr) + 1) {
151 if (strncmp(curstr, "MacRISC", 7) == 0)
152 iic_addr_8bit = 1;
153 }
154 }
155
156 /*
157 * Attach those children represented in the device tree.
158 */
159 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
160 /*
161 * Try to get the I2C address first from the i2c-address
162 * property, then try the reg property. It moves around
163 * on different systems.
164 */
165 if (OF_getencprop(child, "i2c-address", &paddr,
166 sizeof(paddr)) == -1)
167 if (OF_getencprop(child, "reg", &paddr,
168 sizeof(paddr)) == -1)
169 continue;
170
171 /*
172 * Now set up the I2C and OFW bus layer devinfo and add it
173 * to the bus.
174 */
175 dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
176 M_NOWAIT | M_ZERO);
177 if (dinfo == NULL)
178 continue;
179 /*
180 * FreeBSD drivers expect I2C addresses to be expressed as
181 * 8-bit values. Apple OFW data contains 8-bit values, but
182 * Linux FDT data contains 7-bit values, so shift them up to
183 * 8-bit format.
184 */
185 if (iic_addr_8bit)
186 dinfo->opd_dinfo.addr = paddr;
187 else
188 dinfo->opd_dinfo.addr = paddr << 1;
189
190 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
191 0) {
192 free(dinfo, M_DEVBUF);
193 continue;
194 }
195
196 childdev = device_add_child(dev, NULL, -1);
197 resource_list_init(&dinfo->opd_dinfo.rl);
198 ofw_bus_intr_to_rl(childdev, child,
199 &dinfo->opd_dinfo.rl, NULL);
200 device_set_ivars(childdev, dinfo);
201 }
202
203 /* Register bus */
204 OF_device_register_xref(OF_xref_from_node(node), dev);
205 return (bus_generic_attach(dev));
206}
207
208static device_t
209ofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
210{
211 device_t child;
212 struct ofw_iicbus_devinfo *devi;
213
214 child = device_add_child_ordered(dev, order, name, unit);
215 if (child == NULL)
216 return (child);
217 devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
218 M_NOWAIT | M_ZERO);
219 if (devi == NULL) {
220 device_delete_child(dev, child);
221 return (0);
222 }
223
224 /*
225 * NULL all the OFW-related parts of the ivars for non-OFW
226 * children.
227 */
228 devi->opd_obdinfo.obd_node = -1;
229 devi->opd_obdinfo.obd_name = NULL;
230 devi->opd_obdinfo.obd_compat = NULL;
231 devi->opd_obdinfo.obd_type = NULL;
232 devi->opd_obdinfo.obd_model = NULL;
233
234 device_set_ivars(child, devi);
235
236 return (child);
237}
238
239static const struct ofw_bus_devinfo *
240ofw_iicbus_get_devinfo(device_t bus, device_t dev)
241{
242 struct ofw_iicbus_devinfo *dinfo;
243
244 dinfo = device_get_ivars(dev);
245 return (&dinfo->opd_obdinfo);
246}
247
248static int
249ofw_iicbus_set_devinfo(device_t bus, device_t dev, phandle_t ofw_node,
250 char *ofw_name, char *ofw_compat, uint32_t i2c_addr)
251{
252 struct ofw_iicbus_devinfo *devi;
253
254 /*
255 * Setup OFW-related parts of the ivars for manually
256 * created ofw_iicbus childern.
257 */
258 devi = device_get_ivars(dev);
259 if (devi == NULL)
260 return (ENXIO);
261
262 devi->opd_obdinfo.obd_node = ofw_node;
263 if (ofw_name != NULL)
264 devi->opd_obdinfo.obd_name = strdup(ofw_name, M_OFWPROP);
265 if (ofw_compat != NULL)
266 devi->opd_obdinfo.obd_compat = strdup(ofw_compat, M_OFWPROP);
267 devi->opd_dinfo.addr = i2c_addr;
268 return (0);
269}
INTERFACE iicbb
Definition: iicbb_if.m:31
void iicbus_init_frequency(device_t dev, u_int bus_freq)
Definition: iicbus.c:301
driver_t iicbus_driver
Definition: iicbus.c:380
#define IICBUS_SOFTC(d)
Definition: iicbus.h:38
driver_t ofw_iicbus_driver
INTERFACE iicbus
Definition: iicbus_if.m:32
#define iicbus_reset(bus, speed, addr, oldaddr)
Definition: iiconf.h:135
#define IIC_FASTEST
Definition: iiconf.h:83
devclass_t ofw_iicbus_devclass
Definition: ofw_iicbus.c:85
static device_method_t ofw_iicbus_methods[]
Definition: ofw_iicbus.c:57
EARLY_DRIVER_MODULE(ofw_iicbus, iicbb, ofw_iicbus_driver, ofw_iicbus_devclass, 0, 0, BUS_PASS_BUS)
__FBSDID("$FreeBSD$")
DEFINE_CLASS_1(iicbus, ofw_iicbus_driver, ofw_iicbus_methods, sizeof(struct iicbus_softc), iicbus_driver)
MODULE_VERSION(ofw_iicbus, 1)
MODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1)
static device_probe_t ofw_iicbus_probe
Definition: ofw_iicbus.c:48
static device_t ofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
Definition: ofw_iicbus.c:209
static const struct ofw_bus_devinfo * ofw_iicbus_get_devinfo(device_t bus, device_t dev)
Definition: ofw_iicbus.c:240
static device_attach_t ofw_iicbus_attach
Definition: ofw_iicbus.c:49
static int ofw_iicbus_set_devinfo(device_t bus, device_t dev, phandle_t ofw_node, char *ofw_name, char *ofw_compat, uint32_t i2c_addr)
Definition: ofw_iicbus.c:249
char * ofw_compat
Definition: ofw_iicbus_if.m:41
device_t dev
Definition: ofw_iicbus_if.m:38
uint32_t i2c_addr
Definition: ofw_iicbus_if.m:42
INTERFACE ofw_iicbus
Definition: ofw_iicbus_if.m:31
char * ofw_name
Definition: ofw_iicbus_if.m:40
phandle_t ofw_node
Definition: ofw_iicbus_if.m:39
struct resource_list rl
Definition: iicbus.h:57
uint32_t addr
Definition: iicbus.h:56
struct mtx lock
Definition: iicbus.h:50
device_t dev
Definition: iicbus.h:42
struct ofw_bus_devinfo opd_obdinfo
Definition: ofw_iicbus.c:82
struct iicbus_ivar opd_dinfo
Definition: ofw_iicbus.c:81