ofw_iicbus.c revision 292157
1187261Snwhitehorn/*-
2187261Snwhitehorn * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org>
3187261Snwhitehorn * All rights reserved.
4187261Snwhitehorn *
5187261Snwhitehorn * Redistribution and use in source and binary forms, with or without
6187261Snwhitehorn * modification, are permitted provided that the following conditions
7187261Snwhitehorn * are met:
8187261Snwhitehorn * 1. Redistributions of source code must retain the above copyright
9187261Snwhitehorn *    notice unmodified, this list of conditions, and the following
10187261Snwhitehorn *    disclaimer.
11187261Snwhitehorn * 2. Redistributions in binary form must reproduce the above copyright
12187261Snwhitehorn *    notice, this list of conditions and the following disclaimer in the
13187261Snwhitehorn *    documentation and/or other materials provided with the distribution.
14187261Snwhitehorn *
15187261Snwhitehorn * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16187261Snwhitehorn * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17187261Snwhitehorn * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18187261Snwhitehorn * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19187261Snwhitehorn * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20187261Snwhitehorn * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21187261Snwhitehorn * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22187261Snwhitehorn * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23187261Snwhitehorn * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24187261Snwhitehorn * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25187261Snwhitehorn */
26187261Snwhitehorn
27187261Snwhitehorn#include <sys/cdefs.h>
28187261Snwhitehorn__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_iicbus.c 292157 2015-12-13 08:23:45Z mmel $");
29187261Snwhitehorn
30187261Snwhitehorn#include <sys/param.h>
31187261Snwhitehorn#include <sys/bus.h>
32187261Snwhitehorn#include <sys/kernel.h>
33187261Snwhitehorn#include <sys/libkern.h>
34187261Snwhitehorn#include <sys/lock.h>
35187261Snwhitehorn#include <sys/module.h>
36187261Snwhitehorn#include <sys/mutex.h>
37187261Snwhitehorn
38187261Snwhitehorn#include <dev/iicbus/iicbus.h>
39187261Snwhitehorn#include <dev/iicbus/iiconf.h>
40187261Snwhitehorn#include <dev/ofw/ofw_bus.h>
41187261Snwhitehorn#include <dev/ofw/ofw_bus_subr.h>
42187261Snwhitehorn#include <dev/ofw/openfirm.h>
43187261Snwhitehorn
44187261Snwhitehorn#include "iicbus_if.h"
45187261Snwhitehorn
46187261Snwhitehorn/* Methods */
47187261Snwhitehornstatic device_probe_t ofw_iicbus_probe;
48187261Snwhitehornstatic device_attach_t ofw_iicbus_attach;
49212413Savgstatic device_t ofw_iicbus_add_child(device_t dev, u_int order,
50212413Savg    const char *name, int unit);
51187261Snwhitehornstatic const struct ofw_bus_devinfo *ofw_iicbus_get_devinfo(device_t bus,
52187261Snwhitehorn    device_t dev);
53187261Snwhitehorn
54187261Snwhitehornstatic device_method_t ofw_iicbus_methods[] = {
55187261Snwhitehorn	/* Device interface */
56187261Snwhitehorn	DEVMETHOD(device_probe,		ofw_iicbus_probe),
57187261Snwhitehorn	DEVMETHOD(device_attach,	ofw_iicbus_attach),
58187261Snwhitehorn
59187261Snwhitehorn	/* Bus interface */
60187261Snwhitehorn	DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str),
61187261Snwhitehorn	DEVMETHOD(bus_add_child,	ofw_iicbus_add_child),
62187261Snwhitehorn
63187261Snwhitehorn	/* ofw_bus interface */
64187261Snwhitehorn	DEVMETHOD(ofw_bus_get_devinfo,	ofw_iicbus_get_devinfo),
65187261Snwhitehorn	DEVMETHOD(ofw_bus_get_compat,	ofw_bus_gen_get_compat),
66187261Snwhitehorn	DEVMETHOD(ofw_bus_get_model,	ofw_bus_gen_get_model),
67187261Snwhitehorn	DEVMETHOD(ofw_bus_get_name,	ofw_bus_gen_get_name),
68187261Snwhitehorn	DEVMETHOD(ofw_bus_get_node,	ofw_bus_gen_get_node),
69187261Snwhitehorn	DEVMETHOD(ofw_bus_get_type,	ofw_bus_gen_get_type),
70187261Snwhitehorn
71227848Smarius	DEVMETHOD_END
72187261Snwhitehorn};
73187261Snwhitehorn
74187261Snwhitehornstruct ofw_iicbus_devinfo {
75282674Sloos	struct iicbus_ivar	opd_dinfo;	/* Must be the first. */
76187261Snwhitehorn	struct ofw_bus_devinfo	opd_obdinfo;
77187261Snwhitehorn};
78187261Snwhitehorn
79187261Snwhitehornstatic devclass_t ofwiicbus_devclass;
80187261Snwhitehorn
81187472SnwhitehornDEFINE_CLASS_1(iicbus, ofw_iicbus_driver, ofw_iicbus_methods,
82187472Snwhitehorn    sizeof(struct iicbus_softc), iicbus_driver);
83261844SloosDRIVER_MODULE(ofw_iicbus, iicbb, ofw_iicbus_driver, ofwiicbus_devclass, 0, 0);
84187261SnwhitehornDRIVER_MODULE(ofw_iicbus, iichb, ofw_iicbus_driver, ofwiicbus_devclass, 0, 0);
85187261SnwhitehornMODULE_VERSION(ofw_iicbus, 1);
86187261SnwhitehornMODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1);
87187261Snwhitehorn
88187261Snwhitehornstatic int
89187261Snwhitehornofw_iicbus_probe(device_t dev)
90187261Snwhitehorn{
91187261Snwhitehorn
92233018Snwhitehorn	if (ofw_bus_get_node(dev) == -1)
93187261Snwhitehorn		return (ENXIO);
94187261Snwhitehorn	device_set_desc(dev, "OFW I2C bus");
95187261Snwhitehorn
96187261Snwhitehorn	return (0);
97187261Snwhitehorn}
98187261Snwhitehorn
99187261Snwhitehornstatic int
100187261Snwhitehornofw_iicbus_attach(device_t dev)
101187261Snwhitehorn{
102187261Snwhitehorn	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
103187261Snwhitehorn	struct ofw_iicbus_devinfo *dinfo;
104290557Sandreast	phandle_t child, node, root;
105274641Sian	pcell_t freq, paddr;
106187261Snwhitehorn	device_t childdev;
107290557Sandreast	ssize_t compatlen;
108290557Sandreast	char compat[255];
109290557Sandreast	char *curstr;
110290557Sandreast	u_int iic_addr_8bit = 0;
111187261Snwhitehorn
112187261Snwhitehorn	sc->dev = dev;
113187261Snwhitehorn	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
114274641Sian
115274641Sian	/*
116274641Sian	 * If there is a clock-frequency property for the device node, use it as
117274641Sian	 * the starting value for the bus frequency.  Then call the common
118274641Sian	 * routine that handles the tunable/sysctl which allows the FDT value to
119274641Sian	 * be overridden by the user.
120274641Sian	 */
121274641Sian	node = ofw_bus_get_node(dev);
122274641Sian	freq = 0;
123274641Sian	OF_getencprop(node, "clock-frequency", &freq, sizeof(freq));
124274641Sian	iicbus_init_frequency(dev, freq);
125274641Sian
126187261Snwhitehorn	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
127187261Snwhitehorn
128187261Snwhitehorn	bus_generic_probe(dev);
129187261Snwhitehorn	bus_enumerate_hinted_children(dev);
130187261Snwhitehorn
131187261Snwhitehorn	/*
132290557Sandreast	 * Check if we're running on a PowerMac, needed for the I2C
133290557Sandreast	 * address below.
134290557Sandreast	 */
135290557Sandreast	root = OF_peer(0);
136290557Sandreast	compatlen = OF_getprop(root, "compatible", compat,
137290557Sandreast				sizeof(compat));
138290557Sandreast	if (compatlen != -1) {
139290557Sandreast	    for (curstr = compat; curstr < compat + compatlen;
140290557Sandreast		curstr += strlen(curstr) + 1) {
141290557Sandreast		if (strncmp(curstr, "MacRISC", 7) == 0)
142290557Sandreast		    iic_addr_8bit = 1;
143290557Sandreast	    }
144290557Sandreast	}
145290557Sandreast
146290557Sandreast	/*
147187261Snwhitehorn	 * Attach those children represented in the device tree.
148187261Snwhitehorn	 */
149274641Sian	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
150189280Snwhitehorn		/*
151189280Snwhitehorn		 * Try to get the I2C address first from the i2c-address
152194138Smarius		 * property, then try the reg property.  It moves around
153189280Snwhitehorn		 * on different systems.
154189280Snwhitehorn		 */
155256966Snwhitehorn		if (OF_getencprop(child, "i2c-address", &paddr,
156256966Snwhitehorn		    sizeof(paddr)) == -1)
157256966Snwhitehorn			if (OF_getencprop(child, "reg", &paddr,
158256966Snwhitehorn			    sizeof(paddr)) == -1)
159189280Snwhitehorn				continue;
160189280Snwhitehorn
161187261Snwhitehorn		/*
162187261Snwhitehorn		 * Now set up the I2C and OFW bus layer devinfo and add it
163187261Snwhitehorn		 * to the bus.
164187261Snwhitehorn		 */
165187261Snwhitehorn		dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
166187261Snwhitehorn		    M_NOWAIT | M_ZERO);
167187261Snwhitehorn		if (dinfo == NULL)
168187261Snwhitehorn			continue;
169289704Sian		/*
170290373Sandreast		 * FreeBSD drivers expect I2C addresses to be expressed as
171290373Sandreast		 * 8-bit values.  Apple OFW data contains 8-bit values, but
172290373Sandreast		 * Linux FDT data contains 7-bit values, so shift them up to
173290373Sandreast		 * 8-bit format.
174289704Sian		 */
175290557Sandreast		if (iic_addr_8bit)
176290557Sandreast		    dinfo->opd_dinfo.addr = paddr;
177290557Sandreast		else
178290557Sandreast		    dinfo->opd_dinfo.addr = paddr << 1;
179290557Sandreast
180187261Snwhitehorn		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
181187261Snwhitehorn		    0) {
182187261Snwhitehorn			free(dinfo, M_DEVBUF);
183187261Snwhitehorn			continue;
184187261Snwhitehorn		}
185282674Sloos
186187261Snwhitehorn		childdev = device_add_child(dev, NULL, -1);
187282674Sloos		resource_list_init(&dinfo->opd_dinfo.rl);
188282972Sbr		ofw_bus_intr_to_rl(childdev, child,
189282972Sbr					&dinfo->opd_dinfo.rl, NULL);
190187261Snwhitehorn		device_set_ivars(childdev, dinfo);
191187261Snwhitehorn	}
192187261Snwhitehorn
193292157Smmel	/* Register bus */
194292157Smmel	OF_device_register_xref(OF_xref_from_node(node), dev);
195187261Snwhitehorn	return (bus_generic_attach(dev));
196187261Snwhitehorn}
197187261Snwhitehorn
198187261Snwhitehornstatic device_t
199212413Savgofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
200187261Snwhitehorn{
201187261Snwhitehorn	device_t child;
202187261Snwhitehorn	struct ofw_iicbus_devinfo *devi;
203187261Snwhitehorn
204187261Snwhitehorn	child = device_add_child_ordered(dev, order, name, unit);
205187261Snwhitehorn	if (child == NULL)
206187261Snwhitehorn		return (child);
207194138Smarius	devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
208187261Snwhitehorn	    M_NOWAIT | M_ZERO);
209187261Snwhitehorn	if (devi == NULL) {
210187261Snwhitehorn		device_delete_child(dev, child);
211187261Snwhitehorn		return (0);
212187261Snwhitehorn	}
213187261Snwhitehorn
214194138Smarius	/*
215194138Smarius	 * NULL all the OFW-related parts of the ivars for non-OFW
216194138Smarius	 * children.
217194138Smarius	 */
218187261Snwhitehorn	devi->opd_obdinfo.obd_node = -1;
219187261Snwhitehorn	devi->opd_obdinfo.obd_name = NULL;
220187261Snwhitehorn	devi->opd_obdinfo.obd_compat = NULL;
221187261Snwhitehorn	devi->opd_obdinfo.obd_type = NULL;
222187261Snwhitehorn	devi->opd_obdinfo.obd_model = NULL;
223187261Snwhitehorn
224187261Snwhitehorn	device_set_ivars(child, devi);
225194138Smarius
226187261Snwhitehorn	return (child);
227187261Snwhitehorn}
228187261Snwhitehorn
229187261Snwhitehornstatic const struct ofw_bus_devinfo *
230187261Snwhitehornofw_iicbus_get_devinfo(device_t bus, device_t dev)
231187261Snwhitehorn{
232187261Snwhitehorn	struct ofw_iicbus_devinfo *dinfo;
233187261Snwhitehorn
234187261Snwhitehorn	dinfo = device_get_ivars(dev);
235187261Snwhitehorn	return (&dinfo->opd_obdinfo);
236187261Snwhitehorn}
237