1 #include <sys/cdefs.h> 2 __FBSDID("$FreeBSD$"); 3 4 #include <sys/param.h> 5 #include <sys/systm.h> 6 #include <sys/malloc.h> 7 #include <sys/module.h> 8 #include <sys/kernel.h> 9 #include <sys/queue.h> 10 #include <sys/sysctl.h> 11 #include <sys/types.h> 12 13 #include <sys/bus.h> 14 #include <machine/bus.h> 15 #include <sys/rman.h> 16 #include <machine/resource.h> 17 18 #include <dev/spibus/spibusvar.h> 19 #include <dev/spibus/spi.h> 20 #include "spibus_if.h" 21 22 static int 23 spibus_probe(device_t dev) 24 { 25 device_set_desc(dev, "spibus bus"); 26 return (0); 27 } 28 29 static int 30 spibus_attach(device_t dev) 31 { 32 struct spibus_softc *sc = SPIBUS_SOFTC(dev); 33 34 sc->dev = dev; 35 bus_enumerate_hinted_children(dev); 36 return (bus_generic_attach(dev)); 37 } 38 39 /* 40 * Since this is not a self-enumerating bus, and since we always add 41 * children in attach, we have to always delete children here. 42 */ 43 static int 44 spibus_detach(device_t dev) 45 { 46 int err, ndevs, i; 47 device_t *devlist; 48 49 if ((err = bus_generic_detach(dev)) != 0) 50 return (err); 51 if ((err = device_get_children(dev, &devlist, &ndevs)) != 0) 52 return (err); 53 for (i = 0; i < ndevs; i++) 54 device_delete_child(dev, devlist[i]); 55 free(devlist, M_TEMP); 56 57 return (0); 58 } 59 60 static int 61 spibus_suspend(device_t dev) 62 { 63 return (bus_generic_suspend(dev)); 64 } 65 66 static 67 int 68 spibus_resume(device_t dev) 69 { 70 return (bus_generic_resume(dev)); 71 } 72 73 static int 74 spibus_print_child(device_t dev, device_t child) 75 { 76 struct spibus_ivar *devi = SPIBUS_IVAR(child); 77 int retval = 0; 78 79 retval += bus_print_child_header(dev, child); 80 retval += printf(" at cs %d", devi->cs); 81 retval += bus_print_child_footer(dev, child); 82 83 return (retval); 84 } 85 86 static void 87 spibus_probe_nomatch(device_t bus, device_t child) 88 { 89 struct spibus_ivar *devi = SPIBUS_IVAR(child); 90 91 device_printf(bus, "<unknown card>"); 92 printf(" at cs %d\n", devi->cs); 93 return; 94 } 95 96 static int 97 spibus_child_location_str(device_t bus, device_t child, char *buf, 98 size_t buflen) 99 { 100 struct spibus_ivar *devi = SPIBUS_IVAR(child); 101 102 snprintf(buf, buflen, "cs=%d", devi->cs); 103 return (0); 104 } 105 106 static int 107 spibus_child_pnpinfo_str(device_t bus, device_t child, char *buf, 108 size_t buflen) 109 { 110 *buf = '\0'; 111 return (0); 112 } 113 114 static int 115 spibus_read_ivar(device_t bus, device_t child, int which, u_int *result) 116 { 117 struct spibus_ivar *devi = SPIBUS_IVAR(child); 118 119 switch (which) { 120 default: 121 return (EINVAL); 122 case SPIBUS_IVAR_CS: 123 *(uint32_t *)result = devi->cs; 124 break; 125 } 126 return (0); 127 } 128 129 static device_t 130 spibus_add_child(device_t dev, u_int order, const char *name, int unit) 131 { 132 device_t child; 133 struct spibus_ivar *devi; 134 135 child = device_add_child_ordered(dev, order, name, unit); 136 if (child == NULL) 137 return (child); 138 devi = malloc(sizeof(struct spibus_ivar), M_DEVBUF, M_NOWAIT | M_ZERO); 139 if (devi == NULL) { 140 device_delete_child(dev, child); 141 return (0); 142 } 143 device_set_ivars(child, devi); 144 return (child); 145 } 146 147 static void 148 spibus_hinted_child(device_t bus, const char *dname, int dunit) 149 { 150 device_t child; 151 struct spibus_ivar *devi; 152 153 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 154 devi = SPIBUS_IVAR(child); 155 resource_int_value(dname, dunit, "cs", &devi->cs); 156 } 157 158 static int 159 spibus_transfer_impl(device_t dev, device_t child, struct spi_command *cmd) 160 { 161 /* Maybe set flags too? spi mode? */ 162 spibus_get_cs(dev, &cmd->cs); 163 164 return (SPIBUS_TRANSFER(device_get_parent(dev), child, cmd)); 165 } 166 167 static device_method_t spibus_methods[] = { 168 /* Device interface */ 169 DEVMETHOD(device_probe, spibus_probe), 170 DEVMETHOD(device_attach, spibus_attach), 171 DEVMETHOD(device_detach, spibus_detach), 172 DEVMETHOD(device_shutdown, bus_generic_shutdown), 173 DEVMETHOD(device_suspend, spibus_suspend), 174 DEVMETHOD(device_resume, spibus_resume), 175 176 /* Bus interface */ 177 DEVMETHOD(bus_add_child, spibus_add_child), 178 DEVMETHOD(bus_print_child, spibus_print_child), 179 DEVMETHOD(bus_probe_nomatch, spibus_probe_nomatch), 180 DEVMETHOD(bus_read_ivar, spibus_read_ivar), 181 DEVMETHOD(bus_child_pnpinfo_str, spibus_child_pnpinfo_str), 182 DEVMETHOD(bus_child_location_str, spibus_child_location_str), 183 DEVMETHOD(bus_hinted_child, spibus_hinted_child), 184 185 /* spibus interface */ 186 DEVMETHOD(spibus_transfer, spibus_transfer_impl), 187 188 DEVMETHOD_END 189 }; 190 191 static driver_t spibus_driver = { 192 "spibus", 193 spibus_methods, 194 sizeof(struct spibus_softc) 195 }; 196 197 devclass_t spibus_devclass; 198 199 DRIVER_MODULE(spibus, spi, spibus_driver, spibus_devclass, 0, 0); 200 MODULE_VERSION(spibus, 1); 201