ofw_iicbus.c revision 187472
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 187472 2009-01-20 14:06:30Z nwhitehorn $");
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;
49187261Snwhitehornstatic device_t ofw_iicbus_add_child(device_t dev, int order, const char *name,
50187261Snwhitehorn    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
71187261Snwhitehorn	{ 0, 0 }
72187261Snwhitehorn};
73187261Snwhitehorn
74187261Snwhitehornstruct ofw_iicbus_devinfo {
75187261Snwhitehorn	struct iicbus_ivar	opd_dinfo;
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);
83187261SnwhitehornDRIVER_MODULE(ofw_iicbus, iichb, ofw_iicbus_driver, ofwiicbus_devclass, 0, 0);
84187261SnwhitehornMODULE_VERSION(ofw_iicbus, 1);
85187261SnwhitehornMODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1);
86187261Snwhitehorn
87187261Snwhitehornstatic int
88187261Snwhitehornofw_iicbus_probe(device_t dev)
89187261Snwhitehorn{
90187261Snwhitehorn
91187261Snwhitehorn	if (ofw_bus_get_node(dev) == 0)
92187261Snwhitehorn		return (ENXIO);
93187261Snwhitehorn	device_set_desc(dev, "OFW I2C bus");
94187261Snwhitehorn
95187261Snwhitehorn	return (0);
96187261Snwhitehorn}
97187261Snwhitehorn
98187261Snwhitehornstatic int
99187261Snwhitehornofw_iicbus_attach(device_t dev)
100187261Snwhitehorn{
101187261Snwhitehorn	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
102187261Snwhitehorn	struct ofw_iicbus_devinfo *dinfo;
103187261Snwhitehorn	phandle_t node, child;
104187261Snwhitehorn	device_t childdev;
105187261Snwhitehorn	uint32_t addr;
106187261Snwhitehorn
107187261Snwhitehorn	sc->dev = dev;
108187261Snwhitehorn	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
109187261Snwhitehorn	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
110187261Snwhitehorn
111187261Snwhitehorn	bus_generic_probe(dev);
112187261Snwhitehorn	bus_enumerate_hinted_children(dev);
113187261Snwhitehorn
114187261Snwhitehorn	/*
115187261Snwhitehorn	 * Attach those children represented in the device tree.
116187261Snwhitehorn	 */
117187261Snwhitehorn
118187261Snwhitehorn	node = ofw_bus_get_node(dev);
119187261Snwhitehorn
120187261Snwhitehorn	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
121187261Snwhitehorn		if (OF_getprop(child, "reg", &addr, sizeof(addr)) == -1)
122187261Snwhitehorn			continue;
123187261Snwhitehorn
124187261Snwhitehorn		/*
125187261Snwhitehorn		 * Now set up the I2C and OFW bus layer devinfo and add it
126187261Snwhitehorn		 * to the bus.
127187261Snwhitehorn		 */
128187261Snwhitehorn
129187261Snwhitehorn		dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
130187261Snwhitehorn		    M_NOWAIT | M_ZERO);
131187261Snwhitehorn		if (dinfo == NULL)
132187261Snwhitehorn			continue;
133187261Snwhitehorn		dinfo->opd_dinfo.addr = addr;
134187261Snwhitehorn		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
135187261Snwhitehorn		    0) {
136187261Snwhitehorn			free(dinfo, M_DEVBUF);
137187261Snwhitehorn			continue;
138187261Snwhitehorn		}
139187261Snwhitehorn		childdev = device_add_child(dev, NULL, -1);
140187261Snwhitehorn		device_set_ivars(childdev, dinfo);
141187261Snwhitehorn	}
142187261Snwhitehorn
143187261Snwhitehorn	return (bus_generic_attach(dev));
144187261Snwhitehorn}
145187261Snwhitehorn
146187261Snwhitehornstatic device_t
147187261Snwhitehornofw_iicbus_add_child(device_t dev, int order, const char *name, int unit)
148187261Snwhitehorn{
149187261Snwhitehorn	device_t child;
150187261Snwhitehorn	struct ofw_iicbus_devinfo *devi;
151187261Snwhitehorn
152187261Snwhitehorn	child = device_add_child_ordered(dev, order, name, unit);
153187261Snwhitehorn	if (child == NULL)
154187261Snwhitehorn		return (child);
155187261Snwhitehorn	devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
156187261Snwhitehorn	    M_NOWAIT | M_ZERO);
157187261Snwhitehorn	if (devi == NULL) {
158187261Snwhitehorn		device_delete_child(dev, child);
159187261Snwhitehorn		return (0);
160187261Snwhitehorn	}
161187261Snwhitehorn
162187261Snwhitehorn	/* NULL all the OFW-related parts of the ivars for non-OFW children */
163187261Snwhitehorn
164187261Snwhitehorn	devi->opd_obdinfo.obd_node = -1;
165187261Snwhitehorn	devi->opd_obdinfo.obd_name = NULL;
166187261Snwhitehorn	devi->opd_obdinfo.obd_compat = NULL;
167187261Snwhitehorn	devi->opd_obdinfo.obd_type = NULL;
168187261Snwhitehorn	devi->opd_obdinfo.obd_model = NULL;
169187261Snwhitehorn
170187261Snwhitehorn	device_set_ivars(child, devi);
171187261Snwhitehorn
172187261Snwhitehorn	return (child);
173187261Snwhitehorn}
174187261Snwhitehorn
175187261Snwhitehornstatic const struct ofw_bus_devinfo *
176187261Snwhitehornofw_iicbus_get_devinfo(device_t bus, device_t dev)
177187261Snwhitehorn{
178187261Snwhitehorn	struct ofw_iicbus_devinfo *dinfo;
179187261Snwhitehorn
180187261Snwhitehorn	dinfo = device_get_ivars(dev);
181187261Snwhitehorn	return (&dinfo->opd_obdinfo);
182187261Snwhitehorn}
183187261Snwhitehorn
184