1 /*- 2 * Copyright (c) 2011, Aleksandr Rybalko <ray@dlink.ua> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice unmodified, this list of conditions, and the following 10 * disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 25 * SUCH DAMAGE. 26 */ 27 28 #include <sys/cdefs.h> 29 #include "opt_gpio.h" 30 31 #include <sys/param.h> 32 #include <sys/systm.h> 33 34 #include <sys/bus.h> 35 #include <sys/kernel.h> 36 #include <sys/module.h> 37 #include <sys/rman.h> 38 #include <sys/sysctl.h> 39 40 #include <sys/gpio.h> 41 #include "gpiobus_if.h" 42 43 #include <dev/gpio/gpiobusvar.h> 44 45 #include <dev/spibus/spi.h> 46 #include <dev/spibus/spibusvar.h> 47 #include "spibus_if.h" 48 49 #ifdef GPIO_SPI_DEBUG 50 #define dprintf printf 51 #else 52 #define dprintf(x, arg...) 53 #endif /* GPIO_SPI_DEBUG */ 54 55 struct gpio_spi_softc { 56 device_t sc_dev; 57 device_t sc_busdev; 58 int sc_freq; 59 uint8_t sc_sclk; 60 uint8_t sc_miso; 61 uint8_t sc_mosi; 62 uint8_t sc_cs0; 63 uint8_t sc_cs1; 64 uint8_t sc_cs2; 65 uint8_t sc_cs3; 66 }; 67 68 static void gpio_spi_chip_activate(struct gpio_spi_softc *, int); 69 static void gpio_spi_chip_deactivate(struct gpio_spi_softc *, int); 70 71 static int 72 gpio_spi_probe(device_t dev) 73 { 74 device_set_desc(dev, "GPIO SPI bit-banging driver"); 75 return (0); 76 } 77 78 static void 79 gpio_delay(struct gpio_spi_softc *sc) 80 { 81 int d; 82 83 d = sc->sc_freq / 1000000; 84 if (d == 0) 85 d = 1; 86 87 DELAY(d); 88 } 89 90 static int 91 gpio_spi_attach(device_t dev) 92 { 93 uint32_t value; 94 struct gpio_spi_softc *sc; 95 96 sc = device_get_softc(dev); 97 sc->sc_dev = dev; 98 sc->sc_busdev = device_get_parent(dev); 99 100 /* Required variables */ 101 if (resource_int_value(device_get_name(dev), 102 device_get_unit(dev), "sclk", &value)) 103 return (ENXIO); 104 sc->sc_sclk = value & 0xff; 105 106 if (resource_int_value(device_get_name(dev), 107 device_get_unit(dev), "mosi", &value)) 108 return (ENXIO); 109 sc->sc_mosi = value & 0xff; 110 111 /* Handle no miso; we just never read back from the device */ 112 if (resource_int_value(device_get_name(dev), 113 device_get_unit(dev), "miso", &value)) 114 value = 0xff; 115 sc->sc_miso = value & 0xff; 116 117 if (resource_int_value(device_get_name(dev), 118 device_get_unit(dev), "cs0", &value)) 119 return (ENXIO); 120 sc->sc_cs0 = value & 0xff; 121 122 /* Optional variables */ 123 if (resource_int_value(device_get_name(dev), 124 device_get_unit(dev), "cs1", &value)) 125 value = 0xff; 126 sc->sc_cs1 = value & 0xff; 127 128 if (resource_int_value(device_get_name(dev), 129 device_get_unit(dev), "cs2", &value)) 130 value = 0xff; 131 sc->sc_cs2 = value & 0xff; 132 133 if (resource_int_value(device_get_name(dev), 134 device_get_unit(dev), "cs3", &value)) 135 value = 0xff; 136 sc->sc_cs3 = value & 0xff; 137 138 /* Default to 100KHz */ 139 if (resource_int_value(device_get_name(dev), 140 device_get_unit(dev), "freq", &value)) { 141 value = 100000; 142 } 143 sc->sc_freq = value; 144 145 if (bootverbose) { 146 device_printf(dev, "frequency: %d Hz\n", 147 sc->sc_freq); 148 device_printf(dev, 149 "Use GPIO pins: sclk=%d, mosi=%d, miso=%d, " 150 "cs0=%d, cs1=%d, cs2=%d, cs3=%d\n", 151 sc->sc_sclk, sc->sc_mosi, sc->sc_miso, 152 sc->sc_cs0, sc->sc_cs1, sc->sc_cs2, sc->sc_cs3); 153 } 154 155 /* Set directions */ 156 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sc_sclk, 157 GPIO_PIN_OUTPUT|GPIO_PIN_PULLDOWN); 158 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sc_mosi, 159 GPIO_PIN_OUTPUT|GPIO_PIN_PULLDOWN); 160 if (sc->sc_miso != 0xff) { 161 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sc_miso, 162 GPIO_PIN_INPUT|GPIO_PIN_PULLDOWN); 163 } 164 165 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sc_cs0, 166 GPIO_PIN_OUTPUT|GPIO_PIN_PULLUP); 167 168 if (sc->sc_cs1 != 0xff) 169 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sc_cs1, 170 GPIO_PIN_OUTPUT|GPIO_PIN_PULLUP); 171 if (sc->sc_cs2 != 0xff) 172 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sc_cs2, 173 GPIO_PIN_OUTPUT|GPIO_PIN_PULLUP); 174 if (sc->sc_cs3 != 0xff) 175 GPIOBUS_PIN_SETFLAGS(sc->sc_busdev, sc->sc_dev, sc->sc_cs3, 176 GPIO_PIN_OUTPUT|GPIO_PIN_PULLUP); 177 178 gpio_spi_chip_deactivate(sc, -1); 179 180 device_add_child(dev, "spibus", DEVICE_UNIT_ANY); 181 bus_attach_children(dev); 182 return (0); 183 } 184 185 static int 186 gpio_spi_detach(device_t dev) 187 { 188 189 return (0); 190 } 191 192 static void 193 gpio_spi_chip_activate(struct gpio_spi_softc *sc, int cs) 194 { 195 196 /* called with locked gpiobus */ 197 switch (cs) { 198 case 0: 199 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 200 sc->sc_cs0, 0); 201 break; 202 case 1: 203 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 204 sc->sc_cs1, 0); 205 break; 206 case 2: 207 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 208 sc->sc_cs2, 0); 209 break; 210 case 3: 211 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 212 sc->sc_cs3, 0); 213 break; 214 default: 215 device_printf(sc->sc_dev, "don't have CS%d\n", cs); 216 } 217 218 gpio_delay(sc); 219 } 220 221 static void 222 gpio_spi_chip_deactivate(struct gpio_spi_softc *sc, int cs) 223 { 224 225 /* called wth locked gpiobus */ 226 /* 227 * Put CSx to high 228 */ 229 switch (cs) { 230 case -1: 231 /* All CS */ 232 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 233 sc->sc_cs0, 1); 234 if (sc->sc_cs1 == 0xff) break; 235 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 236 sc->sc_cs1, 1); 237 if (sc->sc_cs2 == 0xff) break; 238 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 239 sc->sc_cs2, 1); 240 if (sc->sc_cs3 == 0xff) break; 241 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 242 sc->sc_cs3, 1); 243 break; 244 case 0: 245 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 246 sc->sc_cs0, 1); 247 break; 248 case 1: 249 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 250 sc->sc_cs1, 1); 251 break; 252 case 2: 253 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 254 sc->sc_cs2, 1); 255 break; 256 case 3: 257 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 258 sc->sc_cs3, 1); 259 break; 260 default: 261 device_printf(sc->sc_dev, "don't have CS%d\n", cs); 262 } 263 } 264 265 static uint8_t 266 gpio_spi_txrx(struct gpio_spi_softc *sc, int cs, int mode, uint8_t data) 267 { 268 uint32_t mask, out = 0; 269 unsigned int bit; 270 271 272 /* called with locked gpiobus */ 273 274 for (mask = 0x80; mask > 0; mask >>= 1) { 275 if ((mode == SPIBUS_MODE_CPOL) || 276 (mode == SPIBUS_MODE_CPHA)) { 277 /* If mode 1 or 2 */ 278 279 /* first step */ 280 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 281 sc->sc_mosi, (data & mask)?1:0); 282 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 283 sc->sc_sclk, 0); 284 gpio_delay(sc); 285 /* second step */ 286 if (sc->sc_miso != 0xff) { 287 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, 288 sc->sc_miso, &bit); 289 out |= bit?mask:0; 290 } 291 /* Data captured */ 292 gpio_delay(sc); 293 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 294 sc->sc_sclk, 1); 295 gpio_delay(sc); 296 } else { 297 /* If mode 0 or 3 */ 298 299 /* first step */ 300 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 301 sc->sc_mosi, (data & mask)?1:0); 302 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 303 sc->sc_sclk, 1); 304 gpio_delay(sc); 305 /* second step */ 306 if (sc->sc_miso != 0xff) { 307 GPIOBUS_PIN_GET(sc->sc_busdev, sc->sc_dev, 308 sc->sc_miso, &bit); 309 out |= bit?mask:0; 310 } 311 /* Data captured */ 312 gpio_delay(sc); 313 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 314 sc->sc_sclk, 0); 315 gpio_delay(sc); 316 } 317 } 318 319 return (out & 0xff); 320 } 321 322 static int 323 gpio_spi_transfer(device_t dev, device_t child, struct spi_command *cmd) 324 { 325 struct gpio_spi_softc *sc; 326 uint8_t *buf_in, *buf_out; 327 struct spibus_ivar *devi = SPIBUS_IVAR(child); 328 int i; 329 330 sc = device_get_softc(dev); 331 332 KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz, 333 ("TX/RX command sizes should be equal")); 334 KASSERT(cmd->tx_data_sz == cmd->rx_data_sz, 335 ("TX/RX data sizes should be equal")); 336 337 gpio_spi_chip_activate(sc, devi->cs); 338 339 /* Preset pins */ 340 if ((devi->mode == SPIBUS_MODE_CPOL) || 341 (devi->mode == SPIBUS_MODE_CPHA)) { 342 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 343 sc->sc_sclk, 1); 344 } else { 345 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 346 sc->sc_sclk, 0); 347 } 348 349 /* 350 * Transfer command 351 */ 352 buf_out = (uint8_t *)cmd->tx_cmd; 353 buf_in = (uint8_t *)cmd->rx_cmd; 354 355 for (i = 0; i < cmd->tx_cmd_sz; i++) 356 buf_in[i] = gpio_spi_txrx(sc, devi->cs, devi->mode, buf_out[i]); 357 358 /* 359 * Receive/transmit data (depends on command) 360 */ 361 buf_out = (uint8_t *)cmd->tx_data; 362 buf_in = (uint8_t *)cmd->rx_data; 363 for (i = 0; i < cmd->tx_data_sz; i++) 364 buf_in[i] = gpio_spi_txrx(sc, devi->cs, devi->mode, buf_out[i]); 365 366 /* Return pins to mode default */ 367 if ((devi->mode == SPIBUS_MODE_CPOL) || 368 (devi->mode == SPIBUS_MODE_CPHA)) { 369 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 370 sc->sc_sclk, 1); 371 } else { 372 GPIOBUS_PIN_SET(sc->sc_busdev, sc->sc_dev, 373 sc->sc_sclk, 0); 374 } 375 376 gpio_spi_chip_deactivate(sc, devi->cs); 377 378 return (0); 379 } 380 381 static device_method_t gpio_spi_methods[] = { 382 /* Device interface */ 383 DEVMETHOD(device_probe, gpio_spi_probe), 384 DEVMETHOD(device_attach, gpio_spi_attach), 385 DEVMETHOD(device_detach, gpio_spi_detach), 386 387 DEVMETHOD(spibus_transfer, gpio_spi_transfer), 388 389 {0, 0} 390 }; 391 392 static driver_t gpio_spi_driver = { 393 "gpiospi", 394 gpio_spi_methods, 395 sizeof(struct gpio_spi_softc), 396 }; 397 398 DRIVER_MODULE(gpiospi, gpiobus, gpio_spi_driver, 0, 0); 399 DRIVER_MODULE(spibus, gpiospi, spibus_driver, 0, 0); 400 MODULE_DEPEND(gpiospi, spibus, 1, 1, 1); 401 MODULE_DEPEND(gpiospi, gpiobus, 1, 1, 1); 402