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