xref: /freebsd/sys/dev/iicbus/ofw_iicbus.c (revision 1d386b48)
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 #include <sys/param.h>
29 #include <sys/bus.h>
30 #include <sys/kernel.h>
31 #include <sys/libkern.h>
32 #include <sys/lock.h>
33 #include <sys/module.h>
34 #include <sys/mutex.h>
35 
36 #include <dev/iicbus/iicbus.h>
37 #include <dev/iicbus/iiconf.h>
38 #include <dev/ofw/ofw_bus.h>
39 #include <dev/ofw/ofw_bus_subr.h>
40 #include <dev/ofw/openfirm.h>
41 
42 #include "iicbus_if.h"
43 #include "ofw_iicbus_if.h"
44 
45 /* Methods */
46 static device_probe_t ofw_iicbus_probe;
47 static device_attach_t ofw_iicbus_attach;
48 static device_t ofw_iicbus_add_child(device_t dev, u_int order,
49     const char *name, int unit);
50 static const struct ofw_bus_devinfo *ofw_iicbus_get_devinfo(device_t bus,
51     device_t dev);
52 static int ofw_iicbus_set_devinfo(device_t bus, device_t dev,
53     phandle_t ofw_node, char *ofw_name, char *ofw_compat, uint32_t i2c_addr);
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,	ofw_bus_gen_child_pnpinfo),
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 	/* ofw_iicbus interface */
73 	DEVMETHOD(ofw_iicbus_set_devinfo, ofw_iicbus_set_devinfo),
74 
75 	DEVMETHOD_END
76 };
77 
78 struct ofw_iicbus_devinfo {
79 	struct iicbus_ivar	opd_dinfo;	/* Must be the first. */
80 	struct ofw_bus_devinfo	opd_obdinfo;
81 };
82 
83 DEFINE_CLASS_1(iicbus, ofw_iicbus_driver, ofw_iicbus_methods,
84     sizeof(struct iicbus_softc), iicbus_driver);
85 EARLY_DRIVER_MODULE(ofw_iicbus, iicbb, ofw_iicbus_driver, 0, 0, BUS_PASS_BUS);
86 EARLY_DRIVER_MODULE(ofw_iicbus, iichb, ofw_iicbus_driver, 0, 0, BUS_PASS_BUS);
87 EARLY_DRIVER_MODULE(ofw_iicbus, twsi, ofw_iicbus_driver, 0, 0, BUS_PASS_BUS);
88 MODULE_VERSION(ofw_iicbus, 1);
89 MODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1);
90 
91 static int
92 ofw_iicbus_probe(device_t dev)
93 {
94 
95 	if (ofw_bus_get_node(dev) == -1)
96 		return (ENXIO);
97 	device_set_desc(dev, "OFW I2C bus");
98 
99 	return (0);
100 }
101 
102 static int
103 ofw_iicbus_attach(device_t dev)
104 {
105 	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
106 	struct ofw_iicbus_devinfo *dinfo;
107 	phandle_t child, node, root;
108 	pcell_t freq, paddr;
109 	device_t childdev;
110 	ssize_t compatlen;
111 	char compat[255];
112 	char *curstr;
113 	u_int iic_addr_8bit = 0;
114 
115 	sc->dev = dev;
116 	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
117 
118 	/*
119 	 * If there is a clock-frequency property for the device node, use it as
120 	 * the starting value for the bus frequency.  Then call the common
121 	 * routine that handles the tunable/sysctl which allows the FDT value to
122 	 * be overridden by the user.
123 	 */
124 	node = ofw_bus_get_node(dev);
125 	freq = 0;
126 	OF_getencprop(node, "clock-frequency", &freq, sizeof(freq));
127 	iicbus_init_frequency(dev, freq);
128 
129 	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
130 
131 	bus_generic_probe(dev);
132 	bus_enumerate_hinted_children(dev);
133 
134 	/*
135 	 * Check if we're running on a PowerMac, needed for the I2C
136 	 * address below.
137 	 */
138 	root = OF_peer(0);
139 	compatlen = OF_getprop(root, "compatible", compat,
140 				sizeof(compat));
141 	if (compatlen != -1) {
142 	    for (curstr = compat; curstr < compat + compatlen;
143 		curstr += strlen(curstr) + 1) {
144 		if (strncmp(curstr, "MacRISC", 7) == 0)
145 		    iic_addr_8bit = 1;
146 	    }
147 	}
148 
149 	/*
150 	 * Attach those children represented in the device tree.
151 	 */
152 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
153 		/*
154 		 * Try to get the I2C address first from the i2c-address
155 		 * property, then try the reg property.  It moves around
156 		 * on different systems.
157 		 */
158 		if (OF_getencprop(child, "i2c-address", &paddr,
159 		    sizeof(paddr)) == -1)
160 			if (OF_getencprop(child, "reg", &paddr,
161 			    sizeof(paddr)) == -1)
162 				continue;
163 
164 		/*
165 		 * Now set up the I2C and OFW bus layer devinfo and add it
166 		 * to the bus.
167 		 */
168 		dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
169 		    M_NOWAIT | M_ZERO);
170 		if (dinfo == NULL)
171 			continue;
172 		/*
173 		 * FreeBSD drivers expect I2C addresses to be expressed as
174 		 * 8-bit values.  Apple OFW data contains 8-bit values, but
175 		 * Linux FDT data contains 7-bit values, so shift them up to
176 		 * 8-bit format.
177 		 */
178 		if (iic_addr_8bit)
179 		    dinfo->opd_dinfo.addr = paddr;
180 		else
181 		    dinfo->opd_dinfo.addr = paddr << 1;
182 
183 		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
184 		    0) {
185 			free(dinfo, M_DEVBUF);
186 			continue;
187 		}
188 
189 		childdev = device_add_child(dev, NULL, -1);
190 		resource_list_init(&dinfo->opd_dinfo.rl);
191 		ofw_bus_intr_to_rl(childdev, child,
192 					&dinfo->opd_dinfo.rl, NULL);
193 		device_set_ivars(childdev, dinfo);
194 	}
195 
196 	/* Register bus */
197 	OF_device_register_xref(OF_xref_from_node(node), dev);
198 	return (bus_generic_attach(dev));
199 }
200 
201 static device_t
202 ofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
203 {
204 	device_t child;
205 	struct ofw_iicbus_devinfo *devi;
206 
207 	child = device_add_child_ordered(dev, order, name, unit);
208 	if (child == NULL)
209 		return (child);
210 	devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
211 	    M_NOWAIT | M_ZERO);
212 	if (devi == NULL) {
213 		device_delete_child(dev, child);
214 		return (0);
215 	}
216 
217 	/*
218 	 * NULL all the OFW-related parts of the ivars for non-OFW
219 	 * children.
220 	 */
221 	devi->opd_obdinfo.obd_node = -1;
222 	devi->opd_obdinfo.obd_name = NULL;
223 	devi->opd_obdinfo.obd_compat = NULL;
224 	devi->opd_obdinfo.obd_type = NULL;
225 	devi->opd_obdinfo.obd_model = NULL;
226 
227 	device_set_ivars(child, devi);
228 
229 	return (child);
230 }
231 
232 static const struct ofw_bus_devinfo *
233 ofw_iicbus_get_devinfo(device_t bus, device_t dev)
234 {
235 	struct ofw_iicbus_devinfo *dinfo;
236 
237 	dinfo = device_get_ivars(dev);
238 	return (&dinfo->opd_obdinfo);
239 }
240 
241 static int
242 ofw_iicbus_set_devinfo(device_t bus, device_t dev, phandle_t ofw_node,
243     char *ofw_name, char *ofw_compat, uint32_t i2c_addr)
244 {
245 	struct ofw_iicbus_devinfo *devi;
246 
247 	/*
248 	 * Setup OFW-related parts of the ivars for manually
249 	 * created ofw_iicbus childern.
250 	 */
251 	devi = device_get_ivars(dev);
252 	if (devi == NULL)
253 		return (ENXIO);
254 
255 	devi->opd_obdinfo.obd_node = ofw_node;
256 	if (ofw_name != NULL)
257 		devi->opd_obdinfo.obd_name = strdup(ofw_name, M_OFWPROP);
258 	if (ofw_compat != NULL)
259 		devi->opd_obdinfo.obd_compat = strdup(ofw_compat, M_OFWPROP);
260 	devi->opd_dinfo.addr = i2c_addr;
261 	return (0);
262 }
263