ofw_iicbus.c revision 256966
10Sduke/*- 2553Sohair * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org> 30Sduke * All rights reserved. 40Sduke * 50Sduke * Redistribution and use in source and binary forms, with or without 60Sduke * modification, are permitted provided that the following conditions 70Sduke * are met: 80Sduke * 1. Redistributions of source code must retain the above copyright 90Sduke * notice unmodified, this list of conditions, and the following 100Sduke * disclaimer. 110Sduke * 2. Redistributions in binary form must reproduce the above copyright 120Sduke * notice, this list of conditions and the following disclaimer in the 130Sduke * documentation and/or other materials provided with the distribution. 140Sduke * 150Sduke * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 160Sduke * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 170Sduke * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 180Sduke * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19553Sohair * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20553Sohair * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21553Sohair * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 220Sduke * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 230Sduke * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 240Sduke * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 250Sduke */ 260Sduke 270Sduke#include <sys/cdefs.h> 280Sduke__FBSDID("$FreeBSD: head/sys/dev/ofw/ofw_iicbus.c 256966 2013-10-23 13:55:41Z nwhitehorn $"); 290Sduke 300Sduke#include <sys/param.h> 310Sduke#include <sys/bus.h> 320Sduke#include <sys/kernel.h> 330Sduke#include <sys/libkern.h> 340Sduke#include <sys/lock.h> 350Sduke#include <sys/module.h> 360Sduke#include <sys/mutex.h> 370Sduke 380Sduke#include <dev/iicbus/iicbus.h> 390Sduke#include <dev/iicbus/iiconf.h> 400Sduke#include <dev/ofw/ofw_bus.h> 410Sduke#include <dev/ofw/ofw_bus_subr.h> 420Sduke#include <dev/ofw/openfirm.h> 430Sduke 440Sduke#include "iicbus_if.h" 450Sduke 460Sduke/* Methods */ 470Sdukestatic device_probe_t ofw_iicbus_probe; 480Sdukestatic device_attach_t ofw_iicbus_attach; 490Sdukestatic device_t ofw_iicbus_add_child(device_t dev, u_int order, 500Sduke const char *name, int unit); 510Sdukestatic const struct ofw_bus_devinfo *ofw_iicbus_get_devinfo(device_t bus, 520Sduke device_t dev); 530Sduke 540Sdukestatic device_method_t ofw_iicbus_methods[] = { 550Sduke /* Device interface */ 560Sduke DEVMETHOD(device_probe, ofw_iicbus_probe), 570Sduke DEVMETHOD(device_attach, ofw_iicbus_attach), 580Sduke 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; 76 struct ofw_bus_devinfo opd_obdinfo; 77}; 78 79static devclass_t ofwiicbus_devclass; 80 81DEFINE_CLASS_1(iicbus, ofw_iicbus_driver, ofw_iicbus_methods, 82 sizeof(struct iicbus_softc), iicbus_driver); 83DRIVER_MODULE(ofw_iicbus, iichb, ofw_iicbus_driver, ofwiicbus_devclass, 0, 0); 84MODULE_VERSION(ofw_iicbus, 1); 85MODULE_DEPEND(ofw_iicbus, iicbus, 1, 1, 1); 86 87static int 88ofw_iicbus_probe(device_t dev) 89{ 90 91 if (ofw_bus_get_node(dev) == -1) 92 return (ENXIO); 93 device_set_desc(dev, "OFW I2C bus"); 94 95 return (0); 96} 97 98static int 99ofw_iicbus_attach(device_t dev) 100{ 101 struct iicbus_softc *sc = IICBUS_SOFTC(dev); 102 struct ofw_iicbus_devinfo *dinfo; 103 phandle_t child; 104 pcell_t paddr; 105 device_t childdev; 106 107 sc->dev = dev; 108 mtx_init(&sc->lock, "iicbus", NULL, MTX_DEF); 109 iicbus_reset(dev, IIC_FASTEST, 0, NULL); 110 111 bus_generic_probe(dev); 112 bus_enumerate_hinted_children(dev); 113 114 /* 115 * Attach those children represented in the device tree. 116 */ 117 for (child = OF_child(ofw_bus_get_node(dev)); child != 0; 118 child = OF_peer(child)) { 119 /* 120 * Try to get the I2C address first from the i2c-address 121 * property, then try the reg property. It moves around 122 * on different systems. 123 */ 124 if (OF_getencprop(child, "i2c-address", &paddr, 125 sizeof(paddr)) == -1) 126 if (OF_getencprop(child, "reg", &paddr, 127 sizeof(paddr)) == -1) 128 continue; 129 130 /* 131 * Now set up the I2C and OFW bus layer devinfo and add it 132 * to the bus. 133 */ 134 dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF, 135 M_NOWAIT | M_ZERO); 136 if (dinfo == NULL) 137 continue; 138 dinfo->opd_dinfo.addr = paddr; 139 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) != 140 0) { 141 free(dinfo, M_DEVBUF); 142 continue; 143 } 144 childdev = device_add_child(dev, NULL, -1); 145 device_set_ivars(childdev, dinfo); 146 } 147 148 return (bus_generic_attach(dev)); 149} 150 151static device_t 152ofw_iicbus_add_child(device_t dev, u_int order, const char *name, int unit) 153{ 154 device_t child; 155 struct ofw_iicbus_devinfo *devi; 156 157 child = device_add_child_ordered(dev, order, name, unit); 158 if (child == NULL) 159 return (child); 160 devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF, 161 M_NOWAIT | M_ZERO); 162 if (devi == NULL) { 163 device_delete_child(dev, child); 164 return (0); 165 } 166 167 /* 168 * NULL all the OFW-related parts of the ivars for non-OFW 169 * children. 170 */ 171 devi->opd_obdinfo.obd_node = -1; 172 devi->opd_obdinfo.obd_name = NULL; 173 devi->opd_obdinfo.obd_compat = NULL; 174 devi->opd_obdinfo.obd_type = NULL; 175 devi->opd_obdinfo.obd_model = NULL; 176 177 device_set_ivars(child, devi); 178 179 return (child); 180} 181 182static const struct ofw_bus_devinfo * 183ofw_iicbus_get_devinfo(device_t bus, device_t dev) 184{ 185 struct ofw_iicbus_devinfo *dinfo; 186 187 dinfo = device_get_ivars(dev); 188 return (&dinfo->opd_obdinfo); 189} 190