ofw_iicbus.c revision 308333
129759Swollman/*-
229759Swollman * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
329759Swollman * All rights reserved.
429759Swollman *
529759Swollman * Redistribution and use in source and binary forms, with or without
629759Swollman * modification, are permitted provided that the following conditions
729759Swollman * are met:
829759Swollman * 1. Redistributions of source code must retain the above copyright
929759Swollman *    notice unmodified, this list of conditions, and the following
1029759Swollman *    disclaimer.
1129759Swollman * 2. Redistributions in binary form must reproduce the above copyright
1229759Swollman *    notice, this list of conditions and the following disclaimer in the
1329759Swollman *    documentation and/or other materials provided with the distribution.
1429759Swollman *
15226396Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
1629759Swollman * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
1729759Swollman * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
1829759Swollman * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
1929759Swollman * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2029759Swollman * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2129759Swollman * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2229759Swollman * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2329759Swollman * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2429759Swollman * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2529759Swollman */
2629759Swollman
2729759Swollman#include <sys/cdefs.h>
2829759Swollman__FBSDID("$FreeBSD: stable/11/sys/dev/iicbus/ofw_iicbus.c 308333 2016-11-05 10:23:02Z mmel $");
2929759Swollman
3029759Swollman#include <sys/param.h>
3129759Swollman#include <sys/bus.h>
3229759Swollman#include <sys/kernel.h>
3329759Swollman#include <sys/libkern.h>
3429759Swollman#include <sys/lock.h>
3529759Swollman#include <sys/module.h>
3629759Swollman#include <sys/mutex.h>
3729759Swollman
3829759Swollman#include <dev/iicbus/iicbus.h>
3929759Swollman#include <dev/iicbus/iiconf.h>
4029759Swollman#include <dev/ofw/ofw_bus.h>
4129759Swollman#include <dev/ofw/ofw_bus_subr.h>
4229759Swollman#include <dev/ofw/openfirm.h>
4329759Swollman
4429759Swollman#include "iicbus_if.h"
4529759Swollman
4629759Swollman/* Methods */
4729759Swollmanstatic device_probe_t ofw_iicbus_probe;
4829759Swollmanstatic device_attach_t ofw_iicbus_attach;
4929759Swollmanstatic device_t ofw_iicbus_add_child(device_t dev, u_int order,
5029759Swollman    const char *name, int unit);
5129759Swollmanstatic const struct ofw_bus_devinfo *ofw_iicbus_get_devinfo(device_t bus,
5229759Swollman    device_t dev);
5329759Swollman
5429759Swollmanstatic device_method_t ofw_iicbus_methods[] = {
5529759Swollman	/* Device interface */
5629759Swollman	DEVMETHOD(device_probe,		ofw_iicbus_probe),
5729759Swollman	DEVMETHOD(device_attach,	ofw_iicbus_attach),
5829759Swollman
5987715Smarkm	/* Bus interface */
6087715Smarkm	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
6187715Smarkm	DEVMETHOD(bus_add_child,	ofw_iicbus_add_child),
6287715Smarkm
63200462Sdelphij	/* ofw_bus interface */
64200462Sdelphij	DEVMETHOD(ofw_bus_get_devinfo,	ofw_iicbus_get_devinfo),
6529759Swollman	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
6629759Swollman	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
6729759Swollman	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
6829759Swollman	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
6929759Swollman	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
7029759Swollman
7129759Swollman	DEVMETHOD_END
7229759Swollman};
7329759Swollman
7487715Smarkmstruct ofw_iicbus_devinfo {
7529759Swollman	struct iicbus_ivar	opd_dinfo;	/* Must be the first. */
7629759Swollman	struct ofw_bus_devinfo	opd_obdinfo;
7729759Swollman};
7829759Swollman
7929759Swollmandevclass_t ofwiicbus_devclass;
8029759Swollman
8129759SwollmanDEFINE_CLASS_1(iicbus, ofw_iicbus_driver, ofw_iicbus_methods,
8229759Swollman    sizeof(struct iicbus_softc), iicbus_driver);
8329759SwollmanEARLY_DRIVER_MODULE(ofw_iicbus, iicbb, ofw_iicbus_driver, ofwiicbus_devclass,
8429759Swollman    0, 0, BUS_PASS_BUS);
8529759SwollmanEARLY_DRIVER_MODULE(ofw_iicbus, iichb, ofw_iicbus_driver, ofwiicbus_devclass,
8629759Swollman    0, 0, BUS_PASS_BUS);
8729759SwollmanMODULE_VERSION(ofw_iicbus, 1);
8829759SwollmanMODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1);
8929759Swollman
9029759Swollmanstatic int
9129759Swollmanofw_iicbus_probe(device_t dev)
9229759Swollman{
9329759Swollman
9429759Swollman	if (ofw_bus_get_node(dev) == -1)
9529759Swollman		return (ENXIO);
9629759Swollman	device_set_desc(dev, "OFW I2C bus");
9729759Swollman
9829759Swollman	return (0);
9929759Swollman}
100
101static int
102ofw_iicbus_attach(device_t dev)
103{
104	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
105	struct ofw_iicbus_devinfo *dinfo;
106	phandle_t child, node, root;
107	pcell_t freq, paddr;
108	device_t childdev;
109	ssize_t compatlen;
110	char compat[255];
111	char *curstr;
112	u_int iic_addr_8bit = 0;
113
114	sc->dev = dev;
115	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
116
117	/*
118	 * If there is a clock-frequency property for the device node, use it as
119	 * the starting value for the bus frequency.  Then call the common
120	 * routine that handles the tunable/sysctl which allows the FDT value to
121	 * be overridden by the user.
122	 */
123	node = ofw_bus_get_node(dev);
124	freq = 0;
125	OF_getencprop(node, "clock-frequency", &freq, sizeof(freq));
126	iicbus_init_frequency(dev, freq);
127
128	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
129
130	bus_generic_probe(dev);
131	bus_enumerate_hinted_children(dev);
132
133	/*
134	 * Check if we're running on a PowerMac, needed for the I2C
135	 * address below.
136	 */
137	root = OF_peer(0);
138	compatlen = OF_getprop(root, "compatible", compat,
139				sizeof(compat));
140	if (compatlen != -1) {
141	    for (curstr = compat; curstr < compat + compatlen;
142		curstr += strlen(curstr) + 1) {
143		if (strncmp(curstr, "MacRISC", 7) == 0)
144		    iic_addr_8bit = 1;
145	    }
146	}
147
148	/*
149	 * Attach those children represented in the device tree.
150	 */
151	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
152		/*
153		 * Try to get the I2C address first from the i2c-address
154		 * property, then try the reg property.  It moves around
155		 * on different systems.
156		 */
157		if (OF_getencprop(child, "i2c-address", &paddr,
158		    sizeof(paddr)) == -1)
159			if (OF_getencprop(child, "reg", &paddr,
160			    sizeof(paddr)) == -1)
161				continue;
162
163		/*
164		 * Now set up the I2C and OFW bus layer devinfo and add it
165		 * to the bus.
166		 */
167		dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
168		    M_NOWAIT | M_ZERO);
169		if (dinfo == NULL)
170			continue;
171		/*
172		 * FreeBSD drivers expect I2C addresses to be expressed as
173		 * 8-bit values.  Apple OFW data contains 8-bit values, but
174		 * Linux FDT data contains 7-bit values, so shift them up to
175		 * 8-bit format.
176		 */
177		if (iic_addr_8bit)
178		    dinfo->opd_dinfo.addr = paddr;
179		else
180		    dinfo->opd_dinfo.addr = paddr << 1;
181
182		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
183		    0) {
184			free(dinfo, M_DEVBUF);
185			continue;
186		}
187
188		childdev = device_add_child(dev, NULL, -1);
189		resource_list_init(&dinfo->opd_dinfo.rl);
190		ofw_bus_intr_to_rl(childdev, child,
191					&dinfo->opd_dinfo.rl, NULL);
192		device_set_ivars(childdev, dinfo);
193	}
194
195	/* Register bus */
196	OF_device_register_xref(OF_xref_from_node(node), dev);
197	return (bus_generic_attach(dev));
198}
199
200static device_t
201ofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
202{
203	device_t child;
204	struct ofw_iicbus_devinfo *devi;
205
206	child = device_add_child_ordered(dev, order, name, unit);
207	if (child == NULL)
208		return (child);
209	devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
210	    M_NOWAIT | M_ZERO);
211	if (devi == NULL) {
212		device_delete_child(dev, child);
213		return (0);
214	}
215
216	/*
217	 * NULL all the OFW-related parts of the ivars for non-OFW
218	 * children.
219	 */
220	devi->opd_obdinfo.obd_node = -1;
221	devi->opd_obdinfo.obd_name = NULL;
222	devi->opd_obdinfo.obd_compat = NULL;
223	devi->opd_obdinfo.obd_type = NULL;
224	devi->opd_obdinfo.obd_model = NULL;
225
226	device_set_ivars(child, devi);
227
228	return (child);
229}
230
231static const struct ofw_bus_devinfo *
232ofw_iicbus_get_devinfo(device_t bus, device_t dev)
233{
234	struct ofw_iicbus_devinfo *dinfo;
235
236	dinfo = device_get_ivars(dev);
237	return (&dinfo->opd_obdinfo);
238}
239