ofw_iicbus.c revision 261844
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 261844 2014-02-13 18:22:49Z loos $"); 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 { 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); 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; 104194138Smarius phandle_t child; 105239273Sgonzo pcell_t paddr; 106187261Snwhitehorn device_t childdev; 107187261Snwhitehorn 108187261Snwhitehorn sc->dev = dev; 109187261Snwhitehorn mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF); 110187261Snwhitehorn iicbus_reset(dev, IIC_FASTEST, 0, NULL); 111187261Snwhitehorn 112187261Snwhitehorn bus_generic_probe(dev); 113187261Snwhitehorn bus_enumerate_hinted_children(dev); 114187261Snwhitehorn 115187261Snwhitehorn /* 116187261Snwhitehorn * Attach those children represented in the device tree. 117187261Snwhitehorn */ 118194138Smarius for (child = OF_child(ofw_bus_get_node(dev)); child != 0; 119194138Smarius child = OF_peer(child)) { 120189280Snwhitehorn /* 121189280Snwhitehorn * Try to get the I2C address first from the i2c-address 122194138Smarius * property, then try the reg property. It moves around 123189280Snwhitehorn * on different systems. 124189280Snwhitehorn */ 125256966Snwhitehorn if (OF_getencprop(child, "i2c-address", &paddr, 126256966Snwhitehorn sizeof(paddr)) == -1) 127256966Snwhitehorn if (OF_getencprop(child, "reg", &paddr, 128256966Snwhitehorn sizeof(paddr)) == -1) 129189280Snwhitehorn continue; 130189280Snwhitehorn 131187261Snwhitehorn /* 132187261Snwhitehorn * Now set up the I2C and OFW bus layer devinfo and add it 133187261Snwhitehorn * to the bus. 134187261Snwhitehorn */ 135187261Snwhitehorn dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF, 136187261Snwhitehorn M_NOWAIT | M_ZERO); 137187261Snwhitehorn if (dinfo == NULL) 138187261Snwhitehorn continue; 139256966Snwhitehorn dinfo->opd_dinfo.addr = paddr; 140187261Snwhitehorn if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) != 141187261Snwhitehorn 0) { 142187261Snwhitehorn free(dinfo, M_DEVBUF); 143187261Snwhitehorn continue; 144187261Snwhitehorn } 145187261Snwhitehorn childdev = device_add_child(dev, NULL, -1); 146187261Snwhitehorn device_set_ivars(childdev, dinfo); 147187261Snwhitehorn } 148187261Snwhitehorn 149187261Snwhitehorn return (bus_generic_attach(dev)); 150187261Snwhitehorn} 151187261Snwhitehorn 152187261Snwhitehornstatic device_t 153212413Savgofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit) 154187261Snwhitehorn{ 155187261Snwhitehorn device_t child; 156187261Snwhitehorn struct ofw_iicbus_devinfo *devi; 157187261Snwhitehorn 158187261Snwhitehorn child = device_add_child_ordered(dev, order, name, unit); 159187261Snwhitehorn if (child == NULL) 160187261Snwhitehorn return (child); 161194138Smarius devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF, 162187261Snwhitehorn M_NOWAIT | M_ZERO); 163187261Snwhitehorn if (devi == NULL) { 164187261Snwhitehorn device_delete_child(dev, child); 165187261Snwhitehorn return (0); 166187261Snwhitehorn } 167187261Snwhitehorn 168194138Smarius /* 169194138Smarius * NULL all the OFW-related parts of the ivars for non-OFW 170194138Smarius * children. 171194138Smarius */ 172187261Snwhitehorn devi->opd_obdinfo.obd_node = -1; 173187261Snwhitehorn devi->opd_obdinfo.obd_name = NULL; 174187261Snwhitehorn devi->opd_obdinfo.obd_compat = NULL; 175187261Snwhitehorn devi->opd_obdinfo.obd_type = NULL; 176187261Snwhitehorn devi->opd_obdinfo.obd_model = NULL; 177187261Snwhitehorn 178187261Snwhitehorn device_set_ivars(child, devi); 179194138Smarius 180187261Snwhitehorn return (child); 181187261Snwhitehorn} 182187261Snwhitehorn 183187261Snwhitehornstatic const struct ofw_bus_devinfo * 184187261Snwhitehornofw_iicbus_get_devinfo(device_t bus, device_t dev) 185187261Snwhitehorn{ 186187261Snwhitehorn struct ofw_iicbus_devinfo *dinfo; 187187261Snwhitehorn 188187261Snwhitehorn dinfo = device_get_ivars(dev); 189187261Snwhitehorn return (&dinfo->opd_obdinfo); 190187261Snwhitehorn} 191