ofw_iicbus.c revision 187261
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: head/sys/dev/ofw/ofw_iicbus.c 187261 2009-01-15 02:46:43Z nwhitehorn $"); 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, int order, const char *name, 50 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 { 0, 0 } 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, 1 /* no softc */, 82 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) == 0) 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 node, child; 104 device_t childdev; 105 uint32_t addr; 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 118 node = ofw_bus_get_node(dev); 119 120 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 121 if (OF_getprop(child, "reg", &addr, sizeof(addr)) == -1) 122 continue; 123 124 /* 125 * Now set up the I2C and OFW bus layer devinfo and add it 126 * to the bus. 127 */ 128 129 dinfo = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF, 130 M_NOWAIT | M_ZERO); 131 if (dinfo == NULL) 132 continue; 133 dinfo->opd_dinfo.addr = addr; 134 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) != 135 0) { 136 free(dinfo, M_DEVBUF); 137 continue; 138 } 139 childdev = device_add_child(dev, NULL, -1); 140 device_set_ivars(childdev, dinfo); 141 } 142 143 return (bus_generic_attach(dev)); 144} 145 146static device_t 147ofw_iicbus_add_child(device_t dev, int order, const char *name, int unit) 148{ 149 device_t child; 150 struct ofw_iicbus_devinfo *devi; 151 152 child = device_add_child_ordered(dev, order, name, unit); 153 if (child == NULL) 154 return (child); 155 devi = malloc(sizeof(struct ofw_iicbus_devinfo), M_DEVBUF, 156 M_NOWAIT | M_ZERO); 157 if (devi == NULL) { 158 device_delete_child(dev, child); 159 return (0); 160 } 161 162 /* NULL all the OFW-related parts of the ivars for non-OFW children */ 163 164 devi->opd_obdinfo.obd_node = -1; 165 devi->opd_obdinfo.obd_name = NULL; 166 devi->opd_obdinfo.obd_compat = NULL; 167 devi->opd_obdinfo.obd_type = NULL; 168 devi->opd_obdinfo.obd_model = NULL; 169 170 device_set_ivars(child, devi); 171 172 return (child); 173} 174 175static const struct ofw_bus_devinfo * 176ofw_iicbus_get_devinfo(device_t bus, device_t dev) 177{ 178 struct ofw_iicbus_devinfo *dinfo; 179 180 dinfo = device_get_ivars(dev); 181 return (&dinfo->opd_obdinfo); 182} 183 184