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/param.h> 28 #include <sys/systm.h> 29 #include <sys/malloc.h> 30 #include <sys/module.h> 31 #include <sys/kernel.h> 32 #include <sys/queue.h> 33 #include <sys/sbuf.h> 34 #include <sys/sysctl.h> 35 36 #include <sys/bus.h> 37 #include <machine/bus.h> 38 #include <sys/rman.h> 39 #include <machine/resource.h> 40 41 #include <dev/spibus/spibusvar.h> 42 #include <dev/spibus/spi.h> 43 #include "spibus_if.h" 44 45 static int 46 spibus_probe(device_t dev) 47 { 48 49 device_set_desc(dev, "SPI bus"); 50 return (BUS_PROBE_DEFAULT); 51 } 52 53 int 54 spibus_attach(device_t dev) 55 { 56 struct spibus_softc *sc = SPIBUS_SOFTC(dev); 57 58 sc->dev = dev; 59 bus_enumerate_hinted_children(dev); 60 bus_attach_children(dev); 61 return (0); 62 } 63 64 static int 65 spibus_print_child(device_t dev, device_t child) 66 { 67 struct spibus_ivar *devi = SPIBUS_IVAR(child); 68 int retval = 0; 69 70 retval += bus_print_child_header(dev, child); 71 retval += printf(" at cs %d", devi->cs); 72 retval += printf(" mode %d", devi->mode); 73 retval += resource_list_print_type(&devi->rl, "irq", 74 SYS_RES_IRQ, "%jd"); 75 retval += bus_print_child_footer(dev, child); 76 77 return (retval); 78 } 79 80 void 81 spibus_probe_nomatch(device_t bus, device_t child) 82 { 83 struct spibus_ivar *devi = SPIBUS_IVAR(child); 84 85 device_printf(bus, "<unknown card> at cs %d mode %d\n", devi->cs, 86 devi->mode); 87 return; 88 } 89 90 int 91 spibus_child_location(device_t bus, device_t child, struct sbuf *sb) 92 { 93 struct spibus_ivar *devi = SPIBUS_IVAR(child); 94 int cs; 95 96 cs = devi->cs & ~SPIBUS_CS_HIGH; /* trim 'cs high' bit */ 97 sbuf_printf(sb, "bus=%d cs=%d", device_get_unit(bus), cs); 98 return (0); 99 } 100 101 int 102 spibus_read_ivar(device_t bus, device_t child, int which, uintptr_t *result) 103 { 104 struct spibus_ivar *devi = SPIBUS_IVAR(child); 105 106 switch (which) { 107 default: 108 return (EINVAL); 109 case SPIBUS_IVAR_CS: 110 *(uint32_t *)result = devi->cs; 111 break; 112 case SPIBUS_IVAR_MODE: 113 *(uint32_t *)result = devi->mode; 114 break; 115 case SPIBUS_IVAR_CLOCK: 116 *(uint32_t *)result = devi->clock; 117 break; 118 case SPIBUS_IVAR_CS_DELAY: 119 *(uint32_t *)result = devi->cs_delay; 120 break; 121 } 122 return (0); 123 } 124 125 int 126 spibus_write_ivar(device_t bus, device_t child, int which, uintptr_t value) 127 { 128 struct spibus_ivar *devi = SPIBUS_IVAR(child); 129 130 if (devi == NULL || device_get_parent(child) != bus) 131 return (EDOOFUS); 132 133 switch (which) { 134 case SPIBUS_IVAR_CLOCK: 135 /* Any non-zero value is allowed for max clock frequency. */ 136 if (value == 0) 137 return (EINVAL); 138 devi->clock = (uint32_t)value; 139 break; 140 case SPIBUS_IVAR_CS: 141 /* Chip select cannot be changed. */ 142 return (EINVAL); 143 case SPIBUS_IVAR_MODE: 144 /* Valid SPI modes are 0-3. */ 145 if (value > 3) 146 return (EINVAL); 147 devi->mode = (uint32_t)value; 148 break; 149 case SPIBUS_IVAR_CS_DELAY: 150 devi->cs_delay = (uint32_t)value; 151 break; 152 default: 153 return (EINVAL); 154 } 155 156 return (0); 157 } 158 159 device_t 160 spibus_add_child_common(device_t dev, u_int order, const char *name, int unit, 161 size_t ivars_size) 162 { 163 device_t child; 164 struct spibus_ivar *devi; 165 166 child = device_add_child_ordered(dev, order, name, unit); 167 if (child == NULL) 168 return (child); 169 devi = malloc(ivars_size, M_DEVBUF, M_NOWAIT | M_ZERO); 170 if (devi == NULL) { 171 device_delete_child(dev, child); 172 return (0); 173 } 174 resource_list_init(&devi->rl); 175 device_set_ivars(child, devi); 176 return (child); 177 } 178 179 void 180 spibus_child_deleted(device_t dev, device_t child) 181 { 182 struct spibus_ivar *devi; 183 184 devi = device_get_ivars(child); 185 if (devi == NULL) 186 return; 187 resource_list_free(&devi->rl); 188 free(devi, M_DEVBUF); 189 } 190 191 static device_t 192 spibus_add_child(device_t dev, u_int order, const char *name, int unit) 193 { 194 return (spibus_add_child_common( 195 dev, order, name, unit, sizeof(struct spibus_ivar))); 196 } 197 198 static void 199 spibus_hinted_child(device_t bus, const char *dname, int dunit) 200 { 201 device_t child; 202 int irq; 203 struct spibus_ivar *devi; 204 205 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 206 devi = SPIBUS_IVAR(child); 207 devi->mode = SPIBUS_MODE_NONE; 208 resource_int_value(dname, dunit, "clock", &devi->clock); 209 resource_int_value(dname, dunit, "cs", &devi->cs); 210 resource_int_value(dname, dunit, "mode", &devi->mode); 211 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 212 if (bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1) != 0) 213 device_printf(bus, 214 "Warning: bus_set_resource() failed\n"); 215 } 216 } 217 218 static struct resource_list * 219 spibus_get_resource_list(device_t bus __unused, device_t child) 220 { 221 struct spibus_ivar *devi; 222 223 devi = SPIBUS_IVAR(child); 224 return (&devi->rl); 225 } 226 227 static int 228 spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd) 229 { 230 return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd)); 231 } 232 233 static device_method_t spibus_methods[] = { 234 /* Device interface */ 235 DEVMETHOD(device_probe, spibus_probe), 236 DEVMETHOD(device_attach, spibus_attach), 237 DEVMETHOD(device_detach, bus_generic_detach), 238 DEVMETHOD(device_shutdown, bus_generic_shutdown), 239 DEVMETHOD(device_suspend, bus_generic_suspend), 240 DEVMETHOD(device_resume, bus_generic_resume), 241 242 /* Bus interface */ 243 DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 244 DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 245 DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 246 DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 247 DEVMETHOD(bus_adjust_resource, bus_generic_adjust_resource), 248 DEVMETHOD(bus_alloc_resource, bus_generic_rl_alloc_resource), 249 DEVMETHOD(bus_release_resource, bus_generic_rl_release_resource), 250 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 251 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 252 DEVMETHOD(bus_get_resource_list, spibus_get_resource_list), 253 254 DEVMETHOD(bus_add_child, spibus_add_child), 255 DEVMETHOD(bus_child_deleted, spibus_child_deleted), 256 DEVMETHOD(bus_print_child, spibus_print_child), 257 DEVMETHOD(bus_probe_nomatch, spibus_probe_nomatch), 258 DEVMETHOD(bus_read_ivar, spibus_read_ivar), 259 DEVMETHOD(bus_write_ivar, spibus_write_ivar), 260 DEVMETHOD(bus_child_location, spibus_child_location), 261 DEVMETHOD(bus_hinted_child, spibus_hinted_child), 262 263 /* spibus interface */ 264 DEVMETHOD(spibus_transfer, spibus_transfer_impl), 265 266 DEVMETHOD_END 267 }; 268 269 driver_t spibus_driver = { 270 "spibus", 271 spibus_methods, 272 sizeof(struct spibus_softc) 273 }; 274 275 DRIVER_MODULE(spibus, spi, spibus_driver, 0, 0); 276 MODULE_VERSION(spibus, 1); 277