]> CyberLeo.Net >> Repos - FreeBSD/stable/10.git/blob - sys/dev/ofw/ofw_iicbus.c
Copy head (r256279) to stable/10 as part of the 10.0-RELEASE cycle.
[FreeBSD/stable/10.git] / sys / dev / ofw / ofw_iicbus.c
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/fdt/fdt_common.h>
39 #include <dev/iicbus/iicbus.h>
40 #include <dev/iicbus/iiconf.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
43 #include <dev/ofw/openfirm.h>
44
45 #include "iicbus_if.h"
46
47 /* Methods */
48 static device_probe_t ofw_iicbus_probe;
49 static device_attach_t ofw_iicbus_attach;
50 static device_t ofw_iicbus_add_child(device_t dev, u_int order,
51     const char *name, int unit);
52 static const struct ofw_bus_devinfo *ofw_iicbus_get_devinfo(device_t bus,
53     device_t dev);
54
55 static device_method_t ofw_iicbus_methods[] = {
56         /* Device interface */
57         DEVMETHOD(device_probe,         ofw_iicbus_probe),
58         DEVMETHOD(device_attach,        ofw_iicbus_attach),
59
60         /* Bus interface */
61         DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
62         DEVMETHOD(bus_add_child,        ofw_iicbus_add_child),
63
64         /* ofw_bus interface */
65         DEVMETHOD(ofw_bus_get_devinfo,  ofw_iicbus_get_devinfo),
66         DEVMETHOD(ofw_bus_get_compat,   ofw_bus_gen_get_compat),
67         DEVMETHOD(ofw_bus_get_model,    ofw_bus_gen_get_model),
68         DEVMETHOD(ofw_bus_get_name,     ofw_bus_gen_get_name),
69         DEVMETHOD(ofw_bus_get_node,     ofw_bus_gen_get_node),
70         DEVMETHOD(ofw_bus_get_type,     ofw_bus_gen_get_type),
71
72         DEVMETHOD_END
73 };
74
75 struct ofw_iicbus_devinfo {
76         struct iicbus_ivar      opd_dinfo;
77         struct ofw_bus_devinfo  opd_obdinfo;
78 };
79
80 static devclass_t ofwiicbus_devclass;
81
82 DEFINE_CLASS_1(iicbus, ofw_iicbus_driver, ofw_iicbus_methods,
83     sizeof(struct iicbus_softc), iicbus_driver);
84 DRIVER_MODULE(ofw_iicbus, iichb, ofw_iicbus_driver, ofwiicbus_devclass, 0, 0);
85 MODULE_VERSION(ofw_iicbus, 1);
86 MODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1);
87
88 static int
89 ofw_iicbus_probe(device_t dev)
90 {
91
92         if (ofw_bus_get_node(dev) == -1)
93                 return (ENXIO);
94         device_set_desc(dev, "OFW I2C bus");
95
96         return (0);
97 }
98
99 static int
100 ofw_iicbus_attach(device_t dev)
101 {
102         struct iicbus_softc *sc = IICBUS_SOFTC(dev);
103         struct ofw_iicbus_devinfo *dinfo;
104         phandle_t child;
105         pcell_t paddr;
106         device_t childdev;
107         uint32_t addr;
108
109         sc->dev = dev;
110         mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
111         iicbus_reset(dev, IIC_FASTEST, 0, NULL);
112
113         bus_generic_probe(dev);
114         bus_enumerate_hinted_children(dev);
115
116         /*
117          * Attach those children represented in the device tree.
118          */
119         for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
120             child = OF_peer(child)) {
121                 /*
122                  * Try to get the I2C address first from the i2c-address
123                  * property, then try the reg property.  It moves around
124                  * on different systems.
125                  */
126                 if (OF_getprop(child, "i2c-address", &paddr, sizeof(paddr)) == -1)
127                         if (OF_getprop(child, "reg", &paddr, sizeof(paddr)) == -1)
128                                 continue;
129
130                 addr = fdt32_to_cpu(paddr);
131                 /*
132                  * Now set up the I2C and OFW bus layer devinfo and add it
133                  * to the bus.
134                  */
135                 dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
136                     M_NOWAIT | M_ZERO);
137                 if (dinfo == NULL)
138                         continue;
139                 dinfo->opd_dinfo.addr = addr;
140                 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
141                     0) {
142                         free(dinfo, M_DEVBUF);
143                         continue;
144                 }
145                 childdev = device_add_child(dev, NULL, -1);
146                 device_set_ivars(childdev, dinfo);
147         }
148
149         return (bus_generic_attach(dev));
150 }
151
152 static device_t
153 ofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
154 {
155         device_t child;
156         struct ofw_iicbus_devinfo *devi;
157
158         child = device_add_child_ordered(dev, order, name, unit);
159         if (child == NULL)
160                 return (child);
161         devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
162             M_NOWAIT | M_ZERO);
163         if (devi == NULL) {
164                 device_delete_child(dev, child);
165                 return (0);
166         }
167
168         /*
169          * NULL all the OFW-related parts of the ivars for non-OFW
170          * children.
171          */
172         devi->opd_obdinfo.obd_node = -1;
173         devi->opd_obdinfo.obd_name = NULL;
174         devi->opd_obdinfo.obd_compat = NULL;
175         devi->opd_obdinfo.obd_type = NULL;
176         devi->opd_obdinfo.obd_model = NULL;
177
178         device_set_ivars(child, devi);
179
180         return (child);
181 }
182
183 static const struct ofw_bus_devinfo *
184 ofw_iicbus_get_devinfo(device_t bus, device_t dev)
185 {
186         struct ofw_iicbus_devinfo *dinfo;
187
188         dinfo = device_get_ivars(dev);
189         return (&dinfo->opd_obdinfo);
190 }