1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause 3 * 4 * Copyright (c) 2009, Nathan Whitehorn <nwhitehorn@FreeBSD.org> 5 * Copyright (c) 2013 The FreeBSD Foundation 6 * All rights reserved. 7 * 8 * Portions of this software were developed by Oleksandr Rybalko 9 * under sponsorship from the FreeBSD Foundation. 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice unmodified, this list of conditions, and the following 16 * disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 22 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 23 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 24 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 25 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 26 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 30 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/param.h> 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/libkern.h> 37 #include <sys/lock.h> 38 #include <sys/module.h> 39 #include <sys/mutex.h> 40 41 #include <dev/fdt/fdt_common.h> 42 #include <dev/spibus/spi.h> 43 #include <dev/spibus/spibusvar.h> 44 #include <dev/ofw/ofw_bus.h> 45 #include <dev/ofw/ofw_bus_subr.h> 46 #include <dev/ofw/openfirm.h> 47 48 #include "spibus_if.h" 49 50 struct ofw_spibus_devinfo { 51 struct spibus_ivar opd_dinfo; 52 struct ofw_bus_devinfo opd_obdinfo; 53 }; 54 55 /* Methods */ 56 static device_probe_t ofw_spibus_probe; 57 static device_attach_t ofw_spibus_attach; 58 static device_t ofw_spibus_add_child(device_t dev, u_int order, 59 const char *name, int unit); 60 static const struct ofw_bus_devinfo *ofw_spibus_get_devinfo(device_t bus, 61 device_t dev); 62 63 static int 64 ofw_spibus_probe(device_t dev) 65 { 66 67 if (ofw_bus_get_node(dev) == -1) 68 return (ENXIO); 69 device_set_desc(dev, "OFW SPI bus"); 70 71 return (BUS_PROBE_DEFAULT + 1); 72 } 73 74 static int 75 ofw_spibus_attach(device_t dev) 76 { 77 struct spibus_softc *sc = device_get_softc(dev); 78 struct ofw_spibus_devinfo *dinfo; 79 phandle_t child; 80 pcell_t clock, paddr; 81 device_t childdev; 82 uint32_t mode = SPIBUS_MODE_NONE; 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 * Try to get the cpol/cpha mode 107 */ 108 if (OF_hasprop(child, "spi-cpol")) 109 mode = SPIBUS_MODE_CPOL; 110 if (OF_hasprop(child, "spi-cpha")) { 111 if (mode == SPIBUS_MODE_CPOL) 112 mode = SPIBUS_MODE_CPOL_CPHA; 113 else 114 mode = SPIBUS_MODE_CPHA; 115 } 116 117 /* 118 * Try to get the CS polarity 119 */ 120 if (OF_hasprop(child, "spi-cs-high")) 121 paddr |= SPIBUS_CS_HIGH; 122 123 /* 124 * Get the maximum clock frequency for device, zero means 125 * use the default bus speed. 126 * 127 * XXX Note that the current (2018-04-07) dts bindings say that 128 * spi-max-frequency is a required property (but says nothing of 129 * how to interpret a value of zero). 130 */ 131 if (OF_getencprop(child, "spi-max-frequency", &clock, 132 sizeof(clock)) == -1) 133 clock = 0; 134 135 /* 136 * Now set up the SPI and OFW bus layer devinfo and add it 137 * to the bus. 138 */ 139 dinfo = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF, 140 M_NOWAIT | M_ZERO); 141 if (dinfo == NULL) 142 continue; 143 dinfo->opd_dinfo.cs = paddr; 144 dinfo->opd_dinfo.clock = clock; 145 dinfo->opd_dinfo.mode = mode; 146 if (ofw_bus_gen_setup_devinfo(&dinfo->opd_obdinfo, child) != 147 0) { 148 free(dinfo, M_DEVBUF); 149 continue; 150 } 151 childdev = device_add_child(dev, NULL, -1); 152 153 resource_list_init(&dinfo->opd_dinfo.rl); 154 ofw_bus_intr_to_rl(childdev, child, 155 &dinfo->opd_dinfo.rl, NULL); 156 device_set_ivars(childdev, dinfo); 157 } 158 159 return (bus_generic_attach(dev)); 160 } 161 162 static device_t 163 ofw_spibus_add_child(device_t dev, u_int order, const char *name, int unit) 164 { 165 device_t child; 166 struct ofw_spibus_devinfo *devi; 167 168 child = device_add_child_ordered(dev, order, name, unit); 169 if (child == NULL) 170 return (child); 171 devi = malloc(sizeof(struct ofw_spibus_devinfo), M_DEVBUF, 172 M_NOWAIT | M_ZERO); 173 if (devi == NULL) { 174 device_delete_child(dev, child); 175 return (0); 176 } 177 178 /* 179 * NULL all the OFW-related parts of the ivars for non-OFW 180 * children. 181 */ 182 devi->opd_obdinfo.obd_node = -1; 183 devi->opd_obdinfo.obd_name = NULL; 184 devi->opd_obdinfo.obd_compat = NULL; 185 devi->opd_obdinfo.obd_type = NULL; 186 devi->opd_obdinfo.obd_model = NULL; 187 188 device_set_ivars(child, devi); 189 190 return (child); 191 } 192 193 static const struct ofw_bus_devinfo * 194 ofw_spibus_get_devinfo(device_t bus, device_t dev) 195 { 196 struct ofw_spibus_devinfo *dinfo; 197 198 dinfo = device_get_ivars(dev); 199 return (&dinfo->opd_obdinfo); 200 } 201 202 static struct resource_list * 203 ofw_spibus_get_resource_list(device_t bus __unused, device_t child) 204 { 205 struct spibus_ivar *devi; 206 207 devi = SPIBUS_IVAR(child); 208 return (&devi->rl); 209 } 210 211 static device_method_t ofw_spibus_methods[] = { 212 /* Device interface */ 213 DEVMETHOD(device_probe, ofw_spibus_probe), 214 DEVMETHOD(device_attach, ofw_spibus_attach), 215 216 /* Bus interface */ 217 DEVMETHOD(bus_child_pnpinfo, ofw_bus_gen_child_pnpinfo), 218 DEVMETHOD(bus_add_child, ofw_spibus_add_child), 219 DEVMETHOD(bus_get_resource_list, ofw_spibus_get_resource_list), 220 221 /* ofw_bus interface */ 222 DEVMETHOD(ofw_bus_get_devinfo, ofw_spibus_get_devinfo), 223 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 224 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 225 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 226 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 227 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 228 229 DEVMETHOD_END 230 }; 231 232 DEFINE_CLASS_1(spibus, ofw_spibus_driver, ofw_spibus_methods, 233 sizeof(struct spibus_softc), spibus_driver); 234 DRIVER_MODULE(ofw_spibus, spi, ofw_spibus_driver, 0, 0); 235 MODULE_VERSION(ofw_spibus, 1); 236 MODULE_DEPEND(ofw_spibus, spibus, 1, 1, 1); 237