ofw_spibus.c revision 302408
1/*- 2 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org> 3 * Copyright (c) 2013 The FreeBSD Foundation 4 * All rights reserved. 5 * 6 * Portions of this software were developed by Oleksandr Rybalko 7 * under sponsorship from the FreeBSD Foundation. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted provided that the following conditions 11 * are met: 12 * 1. Redistributions of source code must retain the above copyright 13 * notice unmodified, this list of conditions, and the following 14 * disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 20 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 21 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 22 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 23 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 24 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: stable/11/sys/dev/spibus/ofw_spibus.c 300712 2016-05-26 06:37:33Z adrian $"); 33 34#include <sys/param.h> 35#include <sys/bus.h> 36#include <sys/kernel.h> 37#include <sys/libkern.h> 38#include <sys/lock.h> 39#include <sys/module.h> 40#include <sys/mutex.h> 41 42#include <dev/fdt/fdt_common.h> 43#include <dev/spibus/spi.h> 44#include <dev/spibus/spibusvar.h> 45#include <dev/ofw/ofw_bus.h> 46#include <dev/ofw/ofw_bus_subr.h> 47#include <dev/ofw/openfirm.h> 48 49#include "spibus_if.h" 50 51struct ofw_spibus_devinfo { 52 struct spibus_ivar opd_dinfo; 53 struct ofw_bus_devinfo opd_obdinfo; 54}; 55 56/* Methods */ 57static device_probe_t ofw_spibus_probe; 58static device_attach_t ofw_spibus_attach; 59static device_t ofw_spibus_add_child(device_t dev, u_int order, 60 const char *name, int unit); 61static const struct ofw_bus_devinfo *ofw_spibus_get_devinfo(device_t bus, 62 device_t dev); 63 64static int 65ofw_spibus_probe(device_t dev) 66{ 67 68 if (ofw_bus_get_node(dev) == -1) 69 return (ENXIO); 70 device_set_desc(dev, "OFW SPI bus"); 71 72 return (0); 73} 74 75static int 76ofw_spibus_attach(device_t dev) 77{ 78 struct spibus_softc *sc = device_get_softc(dev); 79 struct ofw_spibus_devinfo *dinfo; 80 phandle_t child; 81 pcell_t clock, paddr; 82 device_t childdev; 83 84 sc->dev = dev; 85 86 bus_generic_probe(dev); 87 bus_enumerate_hinted_children(dev); 88 89 /* 90 * Attach those children represented in the device tree. 91 */ 92 for (child = OF_child(ofw_bus_get_node(dev)); child != 0; 93 child = OF_peer(child)) { 94 /* 95 * Try to get the CS number first from the spi-chipselect 96 * property, then try the reg property. 97 */ 98 if (OF_getencprop(child, "spi-chipselect", &paddr, 99 sizeof(paddr)) == -1) { 100 if (OF_getencprop(child, "reg", &paddr, 101 sizeof(paddr)) == -1) 102 continue; 103 } 104 105 /* 106 * Get the maximum clock frequency for device, zero means 107 * use the default bus speed. 108 */ 109 if (OF_getencprop(child, "spi-max-frequency", &clock, 110 sizeof(clock)) == -1) 111 clock = 0; 112 113 /* 114 * Now set up the SPI and OFW bus layer devinfo and add it 115 * to the bus. 116 */ 117 dinfo = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF, 118 M_NOWAIT | M_ZERO); 119 if (dinfo == NULL) 120 continue; 121 dinfo->opd_dinfo.cs = paddr; 122 dinfo->opd_dinfo.clock = clock; 123 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) != 124 0) { 125 free(dinfo, M_DEVBUF); 126 continue; 127 } 128 childdev = device_add_child(dev, NULL, -1); 129 device_set_ivars(childdev, dinfo); 130 } 131 132 return (bus_generic_attach(dev)); 133} 134 135static device_t 136ofw_spibus_add_child(device_t dev, u_int order, const char *name, int unit) 137{ 138 device_t child; 139 struct ofw_spibus_devinfo *devi; 140 141 child = device_add_child_ordered(dev, order, name, unit); 142 if (child == NULL) 143 return (child); 144 devi = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF, 145 M_NOWAIT | M_ZERO); 146 if (devi == NULL) { 147 device_delete_child(dev, child); 148 return (0); 149 } 150 151 /* 152 * NULL all the OFW-related parts of the ivars for non-OFW 153 * children. 154 */ 155 devi->opd_obdinfo.obd_node = -1; 156 devi->opd_obdinfo.obd_name = NULL; 157 devi->opd_obdinfo.obd_compat = NULL; 158 devi->opd_obdinfo.obd_type = NULL; 159 devi->opd_obdinfo.obd_model = NULL; 160 161 device_set_ivars(child, devi); 162 163 return (child); 164} 165 166static const struct ofw_bus_devinfo * 167ofw_spibus_get_devinfo(device_t bus, device_t dev) 168{ 169 struct ofw_spibus_devinfo *dinfo; 170 171 dinfo = device_get_ivars(dev); 172 return (&dinfo->opd_obdinfo); 173} 174 175static device_method_t ofw_spibus_methods[] = { 176 /* Device interface */ 177 DEVMETHOD(device_probe, ofw_spibus_probe), 178 DEVMETHOD(device_attach, ofw_spibus_attach), 179 180 /* Bus interface */ 181 DEVMETHOD(bus_child_pnpinfo_str, ofw_bus_gen_child_pnpinfo_str), 182 DEVMETHOD(bus_add_child, ofw_spibus_add_child), 183 184 /* ofw_bus interface */ 185 DEVMETHOD(ofw_bus_get_devinfo, ofw_spibus_get_devinfo), 186 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 187 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 188 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 189 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 190 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 191 192 DEVMETHOD_END 193}; 194 195static devclass_t ofwspibus_devclass; 196 197DEFINE_CLASS_1(spibus, ofw_spibus_driver, ofw_spibus_methods, 198 sizeof(struct spibus_softc), spibus_driver); 199DRIVER_MODULE(ofw_spibus, spi, ofw_spibus_driver, ofwspibus_devclass, 0, 0); 200MODULE_VERSION(ofw_spibus, 1); 201MODULE_DEPEND(ofw_spibus, spibus, 1, 1, 1); 202