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: stable/11/sys/dev/iicbus/ofw_iicbus.c 331501 2018-03-24 22:39:38Z ian $");
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
79331501Siandevclass_t ofw_iicbus_devclass;
80187261Snwhitehorn
81187472SnwhitehornDEFINE_CLASS_1(iicbus, ofw_iicbus_driver, ofw_iicbus_methods,
82187472Snwhitehorn    sizeof(struct iicbus_softc), iicbus_driver);
83331501SianEARLY_DRIVER_MODULE(ofw_iicbus, iicbb, ofw_iicbus_driver, ofw_iicbus_devclass,
84296901Smmel    0, 0, BUS_PASS_BUS);
85331501SianEARLY_DRIVER_MODULE(ofw_iicbus, iichb, ofw_iicbus_driver, ofw_iicbus_devclass,
86296901Smmel    0, 0, BUS_PASS_BUS);
87331501SianEARLY_DRIVER_MODULE(ofw_iicbus, twsi, ofw_iicbus_driver, ofw_iicbus_devclass,
88331501Sian    0, 0, BUS_PASS_BUS);
89187261SnwhitehornMODULE_VERSION(ofw_iicbus, 1);
90187261SnwhitehornMODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1);
91187261Snwhitehorn
92187261Snwhitehornstatic int
93187261Snwhitehornofw_iicbus_probe(device_t dev)
94187261Snwhitehorn{
95187261Snwhitehorn
96233018Snwhitehorn	if (ofw_bus_get_node(dev) == -1)
97187261Snwhitehorn		return (ENXIO);
98187261Snwhitehorn	device_set_desc(dev, "OFW I2C bus");
99187261Snwhitehorn
100187261Snwhitehorn	return (0);
101187261Snwhitehorn}
102187261Snwhitehorn
103187261Snwhitehornstatic int
104187261Snwhitehornofw_iicbus_attach(device_t dev)
105187261Snwhitehorn{
106187261Snwhitehorn	struct iicbus_softc *sc = IICBUS_SOFTC(dev);
107187261Snwhitehorn	struct ofw_iicbus_devinfo *dinfo;
108290557Sandreast	phandle_t child, node, root;
109274641Sian	pcell_t freq, paddr;
110187261Snwhitehorn	device_t childdev;
111290557Sandreast	ssize_t compatlen;
112290557Sandreast	char compat[255];
113290557Sandreast	char *curstr;
114290557Sandreast	u_int iic_addr_8bit = 0;
115187261Snwhitehorn
116187261Snwhitehorn	sc->dev = dev;
117187261Snwhitehorn	mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF);
118274641Sian
119274641Sian	/*
120274641Sian	 * If there is a clock-frequency property for the device node, use it as
121274641Sian	 * the starting value for the bus frequency.  Then call the common
122274641Sian	 * routine that handles the tunable/sysctl which allows the FDT value to
123274641Sian	 * be overridden by the user.
124274641Sian	 */
125274641Sian	node = ofw_bus_get_node(dev);
126274641Sian	freq = 0;
127274641Sian	OF_getencprop(node, "clock-frequency", &freq, sizeof(freq));
128274641Sian	iicbus_init_frequency(dev, freq);
129274641Sian
130187261Snwhitehorn	iicbus_reset(dev, IIC_FASTEST, 0, NULL);
131187261Snwhitehorn
132187261Snwhitehorn	bus_generic_probe(dev);
133187261Snwhitehorn	bus_enumerate_hinted_children(dev);
134187261Snwhitehorn
135187261Snwhitehorn	/*
136290557Sandreast	 * Check if we're running on a PowerMac, needed for the I2C
137290557Sandreast	 * address below.
138290557Sandreast	 */
139290557Sandreast	root = OF_peer(0);
140290557Sandreast	compatlen = OF_getprop(root, "compatible", compat,
141290557Sandreast				sizeof(compat));
142290557Sandreast	if (compatlen != -1) {
143290557Sandreast	    for (curstr = compat; curstr < compat + compatlen;
144290557Sandreast		curstr += strlen(curstr) + 1) {
145290557Sandreast		if (strncmp(curstr, "MacRISC", 7) == 0)
146290557Sandreast		    iic_addr_8bit = 1;
147290557Sandreast	    }
148290557Sandreast	}
149290557Sandreast
150290557Sandreast	/*
151187261Snwhitehorn	 * Attach those children represented in the device tree.
152187261Snwhitehorn	 */
153274641Sian	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
154189280Snwhitehorn		/*
155189280Snwhitehorn		 * Try to get the I2C address first from the i2c-address
156194138Smarius		 * property, then try the reg property.  It moves around
157189280Snwhitehorn		 * on different systems.
158189280Snwhitehorn		 */
159256966Snwhitehorn		if (OF_getencprop(child, "i2c-address", &paddr,
160256966Snwhitehorn		    sizeof(paddr)) == -1)
161256966Snwhitehorn			if (OF_getencprop(child, "reg", &paddr,
162256966Snwhitehorn			    sizeof(paddr)) == -1)
163189280Snwhitehorn				continue;
164189280Snwhitehorn
165187261Snwhitehorn		/*
166187261Snwhitehorn		 * Now set up the I2C and OFW bus layer devinfo and add it
167187261Snwhitehorn		 * to the bus.
168187261Snwhitehorn		 */
169187261Snwhitehorn		dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
170187261Snwhitehorn		    M_NOWAIT | M_ZERO);
171187261Snwhitehorn		if (dinfo == NULL)
172187261Snwhitehorn			continue;
173289704Sian		/*
174290373Sandreast		 * FreeBSD drivers expect I2C addresses to be expressed as
175290373Sandreast		 * 8-bit values.  Apple OFW data contains 8-bit values, but
176290373Sandreast		 * Linux FDT data contains 7-bit values, so shift them up to
177290373Sandreast		 * 8-bit format.
178289704Sian		 */
179290557Sandreast		if (iic_addr_8bit)
180290557Sandreast		    dinfo->opd_dinfo.addr = paddr;
181290557Sandreast		else
182290557Sandreast		    dinfo->opd_dinfo.addr = paddr << 1;
183290557Sandreast
184187261Snwhitehorn		if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) !=
185187261Snwhitehorn		    0) {
186187261Snwhitehorn			free(dinfo, M_DEVBUF);
187187261Snwhitehorn			continue;
188187261Snwhitehorn		}
189282674Sloos
190187261Snwhitehorn		childdev = device_add_child(dev, NULL, -1);
191282674Sloos		resource_list_init(&dinfo->opd_dinfo.rl);
192282972Sbr		ofw_bus_intr_to_rl(childdev, child,
193282972Sbr					&dinfo->opd_dinfo.rl, NULL);
194187261Snwhitehorn		device_set_ivars(childdev, dinfo);
195187261Snwhitehorn	}
196187261Snwhitehorn
197292157Smmel	/* Register bus */
198292157Smmel	OF_device_register_xref(OF_xref_from_node(node), dev);
199187261Snwhitehorn	return (bus_generic_attach(dev));
200187261Snwhitehorn}
201187261Snwhitehorn
202187261Snwhitehornstatic device_t
203212413Savgofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit)
204187261Snwhitehorn{
205187261Snwhitehorn	device_t child;
206187261Snwhitehorn	struct ofw_iicbus_devinfo *devi;
207187261Snwhitehorn
208187261Snwhitehorn	child = device_add_child_ordered(dev, order, name, unit);
209187261Snwhitehorn	if (child == NULL)
210187261Snwhitehorn		return (child);
211194138Smarius	devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF,
212187261Snwhitehorn	    M_NOWAIT | M_ZERO);
213187261Snwhitehorn	if (devi == NULL) {
214187261Snwhitehorn		device_delete_child(dev, child);
215187261Snwhitehorn		return (0);
216187261Snwhitehorn	}
217187261Snwhitehorn
218194138Smarius	/*
219194138Smarius	 * NULL all the OFW-related parts of the ivars for non-OFW
220194138Smarius	 * children.
221194138Smarius	 */
222187261Snwhitehorn	devi->opd_obdinfo.obd_node = -1;
223187261Snwhitehorn	devi->opd_obdinfo.obd_name = NULL;
224187261Snwhitehorn	devi->opd_obdinfo.obd_compat = NULL;
225187261Snwhitehorn	devi->opd_obdinfo.obd_type = NULL;
226187261Snwhitehorn	devi->opd_obdinfo.obd_model = NULL;
227187261Snwhitehorn
228187261Snwhitehorn	device_set_ivars(child, devi);
229194138Smarius
230187261Snwhitehorn	return (child);
231187261Snwhitehorn}
232187261Snwhitehorn
233187261Snwhitehornstatic const struct ofw_bus_devinfo *
234187261Snwhitehornofw_iicbus_get_devinfo(device_t bus, device_t dev)
235187261Snwhitehorn{
236187261Snwhitehorn	struct ofw_iicbus_devinfo *dinfo;
237187261Snwhitehorn
238187261Snwhitehorn	dinfo = device_get_ivars(dev);
239187261Snwhitehorn	return (&dinfo->opd_obdinfo);
240187261Snwhitehorn}
241