1 /*- 2 * Copyright (c) 2006 M. Warner Losh <imp@FreeBSD.org> 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23 * SUCH DAMAGE. 24 * 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/malloc.h> 33 #include <sys/module.h> 34 #include <sys/kernel.h> 35 #include <sys/queue.h> 36 #include <sys/sbuf.h> 37 #include <sys/sysctl.h> 38 39 #include <sys/bus.h> 40 #include <machine/bus.h> 41 #include <sys/rman.h> 42 #include <machine/resource.h> 43 44 #include <dev/spibus/spibusvar.h> 45 #include <dev/spibus/spi.h> 46 #include "spibus_if.h" 47 48 static int 49 spibus_probe(device_t dev) 50 { 51 52 device_set_desc(dev, "SPI bus"); 53 return (BUS_PROBE_DEFAULT); 54 } 55 56 static int 57 spibus_attach(device_t dev) 58 { 59 struct spibus_softc *sc = SPIBUS_SOFTC(dev); 60 61 sc->dev = dev; 62 bus_enumerate_hinted_children(dev); 63 return (bus_generic_attach(dev)); 64 } 65 66 /* 67 * Since this is not a self-enumerating bus, and since we always add 68 * children in attach, we have to always delete children here. 69 */ 70 static int 71 spibus_detach(device_t dev) 72 { 73 int err; 74 75 if ((err = bus_generic_detach(dev)) != 0) 76 return (err); 77 device_delete_children(dev); 78 79 return (0); 80 } 81 82 static int 83 spibus_suspend(device_t dev) 84 { 85 return (bus_generic_suspend(dev)); 86 } 87 88 static 89 int 90 spibus_resume(device_t dev) 91 { 92 return (bus_generic_resume(dev)); 93 } 94 95 static int 96 spibus_print_child(device_t dev, device_t child) 97 { 98 struct spibus_ivar *devi = SPIBUS_IVAR(child); 99 int retval = 0; 100 101 retval += bus_print_child_header(dev, child); 102 retval += printf(" at cs %d", devi->cs); 103 retval += printf(" mode %d", devi->mode); 104 retval += resource_list_print_type(&devi->rl, "irq", 105 SYS_RES_IRQ, "%jd"); 106 retval += bus_print_child_footer(dev, child); 107 108 return (retval); 109 } 110 111 static void 112 spibus_probe_nomatch(device_t bus, device_t child) 113 { 114 struct spibus_ivar *devi = SPIBUS_IVAR(child); 115 116 device_printf(bus, "<unknown card> at cs %d mode %d\n", devi->cs, 117 devi->mode); 118 return; 119 } 120 121 static int 122 spibus_child_location(device_t bus, device_t child, struct sbuf *sb) 123 { 124 struct spibus_ivar *devi = SPIBUS_IVAR(child); 125 int cs; 126 127 cs = devi->cs & ~SPIBUS_CS_HIGH; /* trim 'cs high' bit */ 128 sbuf_printf(sb, "bus=%d cs=%d", device_get_unit(bus), cs); 129 return (0); 130 } 131 132 static int 133 spibus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 134 { 135 struct spibus_ivar *devi = SPIBUS_IVAR(child); 136 137 switch (which) { 138 default: 139 return (EINVAL); 140 case SPIBUS_IVAR_CS: 141 *(uint32_t *)result = devi->cs; 142 break; 143 case SPIBUS_IVAR_MODE: 144 *(uint32_t *)result = devi->mode; 145 break; 146 case SPIBUS_IVAR_CLOCK: 147 *(uint32_t *)result = devi->clock; 148 break; 149 } 150 return (0); 151 } 152 153 static int 154 spibus_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 155 { 156 struct spibus_ivar *devi = SPIBUS_IVAR(child); 157 158 if (devi == NULL || device_get_parent(child) != bus) 159 return (EDOOFUS); 160 161 switch (which) { 162 case SPIBUS_IVAR_CLOCK: 163 /* Any non-zero value is allowed for max clock frequency. */ 164 if (value == 0) 165 return (EINVAL); 166 devi->clock = (uint32_t)value; 167 break; 168 case SPIBUS_IVAR_CS: 169 /* Chip select cannot be changed. */ 170 return (EINVAL); 171 case SPIBUS_IVAR_MODE: 172 /* Valid SPI modes are 0-3. */ 173 if (value > 3) 174 return (EINVAL); 175 devi->mode = (uint32_t)value; 176 break; 177 default: 178 return (EINVAL); 179 } 180 181 return (0); 182 } 183 184 static device_t 185 spibus_add_child(device_t dev, u_int order, const char *name, int unit) 186 { 187 device_t child; 188 struct spibus_ivar *devi; 189 190 child = device_add_child_ordered(dev, order, name, unit); 191 if (child == NULL) 192 return (child); 193 devi = malloc(sizeof(struct spibus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 194 if (devi == NULL) { 195 device_delete_child(dev, child); 196 return (0); 197 } 198 resource_list_init(&devi->rl); 199 device_set_ivars(child, devi); 200 return (child); 201 } 202 203 static void 204 spibus_hinted_child(device_t bus, const char *dname, int dunit) 205 { 206 device_t child; 207 int irq; 208 struct spibus_ivar *devi; 209 210 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 211 devi = SPIBUS_IVAR(child); 212 devi->mode = SPIBUS_MODE_NONE; 213 resource_int_value(dname, dunit, "clock", &devi->clock); 214 resource_int_value(dname, dunit, "cs", &devi->cs); 215 resource_int_value(dname, dunit, "mode", &devi->mode); 216 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 217 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) 218 device_printf(bus, 219 "Warning: bus_set_resource() failed\n"); 220 } 221 } 222 223 static struct resource_list * 224 spibus_get_resource_list(device_t bus __unused, device_t child) 225 { 226 struct spibus_ivar *devi; 227 228 devi = SPIBUS_IVAR(child); 229 return (&devi->rl); 230 } 231 232 static int 233 spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd) 234 { 235 return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd)); 236 } 237 238 static device_method_t spibus_methods[] = { 239 /* Device interface */ 240 DEVMETHOD(device_probe, spibus_probe), 241 DEVMETHOD(device_attach, spibus_attach), 242 DEVMETHOD(device_detach, spibus_detach), 243 DEVMETHOD(device_shutdown, bus_generic_shutdown), 244 DEVMETHOD(device_suspend, spibus_suspend), 245 DEVMETHOD(device_resume, spibus_resume), 246 247 /* Bus interface */ 248 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 249 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 250 DEVMETHOD(bus_release_resource, bus_generic_release_resource), 251 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 252 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 253 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 254 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 255 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 256 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 257 DEVMETHOD(bus_get_resource_list, spibus_get_resource_list), 258 259 DEVMETHOD(bus_add_child, spibus_add_child), 260 DEVMETHOD(bus_print_child, spibus_print_child), 261 DEVMETHOD(bus_probe_nomatch, spibus_probe_nomatch), 262 DEVMETHOD(bus_read_ivar, spibus_read_ivar), 263 DEVMETHOD(bus_write_ivar, spibus_write_ivar), 264 DEVMETHOD(bus_child_location, spibus_child_location), 265 DEVMETHOD(bus_hinted_child, spibus_hinted_child), 266 267 /* spibus interface */ 268 DEVMETHOD(spibus_transfer, spibus_transfer_impl), 269 270 DEVMETHOD_END 271 }; 272 273 driver_t spibus_driver = { 274 "spibus", 275 spibus_methods, 276 sizeof(struct spibus_softc) 277 }; 278 279 DRIVER_MODULE(spibus, spi, spibus_driver, 0, 0); 280 MODULE_VERSION(spibus, 1); 281