1a0fd2339SIan Lepore /*- 2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause 3a0fd2339SIan Lepore * 4a0fd2339SIan Lepore * Copyright (c) 2018 Ian Lepore <ian@freebsd.org> 5a0fd2339SIan Lepore * All rights reserved. 6a0fd2339SIan Lepore * 7a0fd2339SIan Lepore * Redistribution and use in source and binary forms, with or without 8a0fd2339SIan Lepore * modification, are permitted provided that the following conditions 9a0fd2339SIan Lepore * are met: 10a0fd2339SIan Lepore * 1. Redistributions of source code must retain the above copyright 11a0fd2339SIan Lepore * notice, this list of conditions and the following disclaimer. 12a0fd2339SIan Lepore * 2. Redistributions in binary form must reproduce the above copyright 13a0fd2339SIan Lepore * notice, this list of conditions and the following disclaimer in the 14a0fd2339SIan Lepore * documentation and/or other materials provided with the distribution. 15a0fd2339SIan Lepore * 16a0fd2339SIan Lepore * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 17a0fd2339SIan Lepore * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 18a0fd2339SIan Lepore * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 19a0fd2339SIan Lepore * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 20a0fd2339SIan Lepore * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 21a0fd2339SIan Lepore * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 22a0fd2339SIan Lepore * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 23a0fd2339SIan Lepore * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 24a0fd2339SIan Lepore * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 25a0fd2339SIan Lepore * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 26a0fd2339SIan Lepore * SUCH DAMAGE. 27a0fd2339SIan Lepore */ 28a0fd2339SIan Lepore 29a0fd2339SIan Lepore #include <sys/cdefs.h> 30a0fd2339SIan Lepore __FBSDID("$FreeBSD$"); 31a0fd2339SIan Lepore 32a0fd2339SIan Lepore /* 33a0fd2339SIan Lepore * Driver for imx Enhanced Configurable SPI; master-mode only. 34a0fd2339SIan Lepore */ 35a0fd2339SIan Lepore 36a0fd2339SIan Lepore #include <sys/param.h> 37a0fd2339SIan Lepore #include <sys/systm.h> 38a0fd2339SIan Lepore #include <sys/bus.h> 39a0fd2339SIan Lepore #include <sys/gpio.h> 40a0fd2339SIan Lepore #include <sys/kernel.h> 41a0fd2339SIan Lepore #include <sys/lock.h> 42a0fd2339SIan Lepore #include <sys/module.h> 43a0fd2339SIan Lepore #include <sys/mutex.h> 44a0fd2339SIan Lepore #include <sys/rman.h> 45a0fd2339SIan Lepore #include <sys/sysctl.h> 46a0fd2339SIan Lepore #include <machine/bus.h> 47a0fd2339SIan Lepore #include <machine/cpu.h> 48a0fd2339SIan Lepore #include <machine/intr.h> 49a0fd2339SIan Lepore 50a0fd2339SIan Lepore #include <arm/freescale/imx/imx_ccmvar.h> 51a0fd2339SIan Lepore 52a0fd2339SIan Lepore #include <dev/gpio/gpiobusvar.h> 53a0fd2339SIan Lepore #include <dev/ofw/ofw_bus.h> 54a0fd2339SIan Lepore #include <dev/ofw/ofw_bus_subr.h> 55a0fd2339SIan Lepore #include <dev/ofw/openfirm.h> 56a0fd2339SIan Lepore #include <dev/spibus/spi.h> 57a0fd2339SIan Lepore #include <dev/spibus/spibusvar.h> 58a0fd2339SIan Lepore 59a0fd2339SIan Lepore #include "spibus_if.h" 60a0fd2339SIan Lepore 61a0fd2339SIan Lepore #define ECSPI_RXDATA 0x00 62a0fd2339SIan Lepore #define ECSPI_TXDATA 0x04 63a0fd2339SIan Lepore #define ECSPI_CTLREG 0x08 64a0fd2339SIan Lepore #define CTLREG_BLEN_SHIFT 20 65a0fd2339SIan Lepore #define CTLREG_BLEN_MASK 0x0fff 66a0fd2339SIan Lepore #define CTLREG_CSEL_SHIFT 18 67a0fd2339SIan Lepore #define CTLREG_CSEL_MASK 0x03 68a0fd2339SIan Lepore #define CTLREG_DRCTL_SHIFT 16 69a0fd2339SIan Lepore #define CTLREG_DRCTL_MASK 0x03 70a0fd2339SIan Lepore #define CTLREG_PREDIV_SHIFT 12 71a0fd2339SIan Lepore #define CTLREG_PREDIV_MASK 0x0f 72a0fd2339SIan Lepore #define CTLREG_POSTDIV_SHIFT 8 73a0fd2339SIan Lepore #define CTLREG_POSTDIV_MASK 0x0f 74a0fd2339SIan Lepore #define CTLREG_CMODE_SHIFT 4 75a0fd2339SIan Lepore #define CTLREG_CMODE_MASK 0x0f 76a0fd2339SIan Lepore #define CTLREG_CMODES_MASTER (CTLREG_CMODE_MASK << CTLREG_CMODE_SHIFT) 77a0fd2339SIan Lepore #define CTLREG_SMC (1u << 3) 78a0fd2339SIan Lepore #define CTLREG_XCH (1u << 2) 79a0fd2339SIan Lepore #define CTLREG_HT (1u << 1) 80a0fd2339SIan Lepore #define CTLREG_EN (1u << 0) 81a0fd2339SIan Lepore #define ECSPI_CFGREG 0x0c 82a0fd2339SIan Lepore #define CFGREG_HTLEN_SHIFT 24 83a0fd2339SIan Lepore #define CFGREG_SCLKCTL_SHIFT 20 84a0fd2339SIan Lepore #define CFGREG_DATACTL_SHIFT 16 85a0fd2339SIan Lepore #define CFGREG_SSPOL_SHIFT 12 86a0fd2339SIan Lepore #define CFGREG_SSCTL_SHIFT 8 87a0fd2339SIan Lepore #define CFGREG_SCLKPOL_SHIFT 4 88a0fd2339SIan Lepore #define CFGREG_SCLKPHA_SHIFT 0 89a0fd2339SIan Lepore #define CFGREG_MASK 0x0f /* all CFGREG fields are 4 bits */ 90a0fd2339SIan Lepore #define ECSPI_INTREG 0x10 91a0fd2339SIan Lepore #define INTREG_TCEN (1u << 7) 92a0fd2339SIan Lepore #define INTREG_ROEN (1u << 6) 93a0fd2339SIan Lepore #define INTREG_RFEN (1u << 5) 94a0fd2339SIan Lepore #define INTREG_RDREN (1u << 4) 95a0fd2339SIan Lepore #define INTREG_RREN (1u << 3) 96a0fd2339SIan Lepore #define INTREG_TFEN (1u << 2) 97a0fd2339SIan Lepore #define INTREG_TDREN (1u << 1) 98a0fd2339SIan Lepore #define INTREG_TEEN (1u << 0) 99a0fd2339SIan Lepore #define ECSPI_DMAREG 0x14 100a0fd2339SIan Lepore #define DMA_RX_THRESH_SHIFT 16 101a0fd2339SIan Lepore #define DMA_RX_THRESH_MASK 0x3f 102a0fd2339SIan Lepore #define DMA_TX_THRESH_SHIFT 0 103a0fd2339SIan Lepore #define DMA_TX_THRESH_MASK 0x3f 104a0fd2339SIan Lepore #define ECSPI_STATREG 0x18 105a0fd2339SIan Lepore #define SREG_TC (1u << 7) 106a0fd2339SIan Lepore #define SREG_RO (1u << 6) 107a0fd2339SIan Lepore #define SREG_RF (1u << 5) 108a0fd2339SIan Lepore #define SREG_RDR (1u << 4) 109a0fd2339SIan Lepore #define SREG_RR (1u << 3) 110a0fd2339SIan Lepore #define SREG_TF (1u << 2) 111a0fd2339SIan Lepore #define SREG_TDR (1u << 1) 112a0fd2339SIan Lepore #define SREG_TE (1u << 0) 113a0fd2339SIan Lepore #define ECSPI_PERIODREG 0x1c 114a0fd2339SIan Lepore #define ECSPI_TESTREG 0x20 115a0fd2339SIan Lepore 116a0fd2339SIan Lepore #define CS_MAX 4 /* Max number of chip selects. */ 117a0fd2339SIan Lepore #define CS_MASK 0x03 /* Mask flag bits out of chipsel. */ 118a0fd2339SIan Lepore 119a0fd2339SIan Lepore #define FIFO_SIZE 64 120a0fd2339SIan Lepore #define FIFO_RXTHRESH 32 121a0fd2339SIan Lepore #define FIFO_TXTHRESH 32 122a0fd2339SIan Lepore 123a0fd2339SIan Lepore struct spi_softc { 124a0fd2339SIan Lepore device_t dev; 125a0fd2339SIan Lepore device_t spibus; 126a0fd2339SIan Lepore struct mtx mtx; 127a0fd2339SIan Lepore struct resource *memres; 128a0fd2339SIan Lepore struct resource *intres; 129a0fd2339SIan Lepore void *inthandle; 130a0fd2339SIan Lepore gpio_pin_t cspins[CS_MAX]; 131a0fd2339SIan Lepore u_int debug; 132a0fd2339SIan Lepore u_int basefreq; 133a0fd2339SIan Lepore uint32_t ctlreg; 134a0fd2339SIan Lepore uint32_t intreg; 135a0fd2339SIan Lepore uint32_t fifocnt; 136a0fd2339SIan Lepore uint8_t *rxbuf; 137a0fd2339SIan Lepore uint32_t rxidx; 138a0fd2339SIan Lepore uint32_t rxlen; 139a0fd2339SIan Lepore uint8_t *txbuf; 140a0fd2339SIan Lepore uint32_t txidx; 141a0fd2339SIan Lepore uint32_t txlen; 142a0fd2339SIan Lepore }; 143a0fd2339SIan Lepore 144a0fd2339SIan Lepore static struct ofw_compat_data compat_data[] = { 145a0fd2339SIan Lepore {"fsl,imx51-ecspi", true}, 146a0fd2339SIan Lepore {"fsl,imx53-ecspi", true}, 147a0fd2339SIan Lepore {"fsl,imx6dl-ecspi", true}, 148a0fd2339SIan Lepore {"fsl,imx6q-ecspi", true}, 149a0fd2339SIan Lepore {"fsl,imx6sx-ecspi", true}, 150a0fd2339SIan Lepore {"fsl,imx6ul-ecspi", true}, 151a0fd2339SIan Lepore {NULL, false} 152a0fd2339SIan Lepore }; 153a0fd2339SIan Lepore 154a0fd2339SIan Lepore static inline uint32_t 155a0fd2339SIan Lepore RD4(struct spi_softc *sc, bus_size_t offset) 156a0fd2339SIan Lepore { 157a0fd2339SIan Lepore 158a0fd2339SIan Lepore return (bus_read_4(sc->memres, offset)); 159a0fd2339SIan Lepore } 160a0fd2339SIan Lepore 161a0fd2339SIan Lepore static inline void 162a0fd2339SIan Lepore WR4(struct spi_softc *sc, bus_size_t offset, uint32_t value) 163a0fd2339SIan Lepore { 164a0fd2339SIan Lepore 165a0fd2339SIan Lepore bus_write_4(sc->memres, offset, value); 166a0fd2339SIan Lepore } 167a0fd2339SIan Lepore 168a0fd2339SIan Lepore static u_int 169a0fd2339SIan Lepore spi_calc_clockdiv(struct spi_softc *sc, u_int busfreq) 170a0fd2339SIan Lepore { 171a0fd2339SIan Lepore u_int post, pre; 172a0fd2339SIan Lepore 173a0fd2339SIan Lepore /* Returning 0 effectively sets both dividers to 1. */ 174a0fd2339SIan Lepore if (sc->basefreq <= busfreq) 175a0fd2339SIan Lepore return (0); 176a0fd2339SIan Lepore 177a0fd2339SIan Lepore /* 178a0fd2339SIan Lepore * Brute-force this; all real-world bus speeds are going to be found on 179a0fd2339SIan Lepore * the 1st or 2nd time through this loop. 180a0fd2339SIan Lepore */ 181a0fd2339SIan Lepore for (post = 0; post < 16; ++post) { 182a0fd2339SIan Lepore pre = ((sc->basefreq >> post) / busfreq) - 1; 183a0fd2339SIan Lepore if (pre < 16) 184a0fd2339SIan Lepore break; 185a0fd2339SIan Lepore } 186a0fd2339SIan Lepore if (post == 16) { 187a0fd2339SIan Lepore /* The lowest we can go is ~115 Hz. */ 188a0fd2339SIan Lepore pre = 15; 189a0fd2339SIan Lepore post = 15; 190a0fd2339SIan Lepore } 191a0fd2339SIan Lepore 192a0fd2339SIan Lepore if (sc->debug >= 2) { 193a0fd2339SIan Lepore device_printf(sc->dev, 194a0fd2339SIan Lepore "base %u bus %u; pre %u, post %u; actual busfreq %u\n", 195a0fd2339SIan Lepore sc->basefreq, busfreq, pre, post, 196a0fd2339SIan Lepore (sc->basefreq / (pre + 1)) / (1 << post)); 197a0fd2339SIan Lepore } 198a0fd2339SIan Lepore 199a0fd2339SIan Lepore return (pre << CTLREG_PREDIV_SHIFT) | (post << CTLREG_POSTDIV_SHIFT); 200a0fd2339SIan Lepore } 201a0fd2339SIan Lepore 202a0fd2339SIan Lepore static void 203a0fd2339SIan Lepore spi_set_chipsel(struct spi_softc *sc, u_int cs, bool active) 204a0fd2339SIan Lepore { 205a0fd2339SIan Lepore bool pinactive; 206a0fd2339SIan Lepore 207a0fd2339SIan Lepore /* 208a0fd2339SIan Lepore * This is kinda crazy... the gpio pins for chipsel are defined as 209a0fd2339SIan Lepore * active-high in the dts, but are supposed to be treated as active-low 210a0fd2339SIan Lepore * by this driver. So to turn on chipsel we have to invert the value 211a0fd2339SIan Lepore * passed to gpio_pin_set_active(). Then, to make it more fun, any 212a0fd2339SIan Lepore * slave can say its chipsel is active-high, so if that option is 213a0fd2339SIan Lepore * on, we have to invert the value again. 214a0fd2339SIan Lepore */ 215a0fd2339SIan Lepore pinactive = !active ^ (bool)(cs & SPIBUS_CS_HIGH); 216a0fd2339SIan Lepore 217a0fd2339SIan Lepore if (sc->debug >= 2) { 218a0fd2339SIan Lepore device_printf(sc->dev, "chipsel %u changed to %u\n", 219a0fd2339SIan Lepore (cs & ~SPIBUS_CS_HIGH), pinactive); 220a0fd2339SIan Lepore } 221a0fd2339SIan Lepore 222a0fd2339SIan Lepore /* 223a0fd2339SIan Lepore * Change the pin, then do a dummy read of its current state to ensure 224a0fd2339SIan Lepore * that the state change reaches the hardware before proceeding. 225a0fd2339SIan Lepore */ 226a0fd2339SIan Lepore gpio_pin_set_active(sc->cspins[cs & ~SPIBUS_CS_HIGH], pinactive); 227a0fd2339SIan Lepore gpio_pin_is_active(sc->cspins[cs & ~SPIBUS_CS_HIGH], &pinactive); 228a0fd2339SIan Lepore } 229a0fd2339SIan Lepore 230a0fd2339SIan Lepore static void 231a0fd2339SIan Lepore spi_hw_setup(struct spi_softc *sc, u_int cs, u_int mode, u_int freq) 232a0fd2339SIan Lepore { 233a0fd2339SIan Lepore uint32_t reg; 234a0fd2339SIan Lepore 235a0fd2339SIan Lepore /* 236a0fd2339SIan Lepore * Set up control register, and write it first to bring the device out 237a0fd2339SIan Lepore * of reset. 238a0fd2339SIan Lepore */ 239a0fd2339SIan Lepore sc->ctlreg = CTLREG_EN | CTLREG_CMODES_MASTER | CTLREG_SMC; 240a0fd2339SIan Lepore sc->ctlreg |= spi_calc_clockdiv(sc, freq); 241a0fd2339SIan Lepore sc->ctlreg |= 7 << CTLREG_BLEN_SHIFT; /* XXX byte at a time */ 242a0fd2339SIan Lepore WR4(sc, ECSPI_CTLREG, sc->ctlreg); 243a0fd2339SIan Lepore 244a0fd2339SIan Lepore /* 245a0fd2339SIan Lepore * Set up the config register. Note that we do all transfers with the 246a0fd2339SIan Lepore * SPI hardware's chip-select set to zero. The actual chip select is 247a0fd2339SIan Lepore * handled with a gpio pin. 248a0fd2339SIan Lepore */ 249a0fd2339SIan Lepore reg = 0; 250a0fd2339SIan Lepore if (cs & SPIBUS_CS_HIGH) 251a0fd2339SIan Lepore reg |= 1u << CFGREG_SSPOL_SHIFT; 252a0fd2339SIan Lepore if (mode & SPIBUS_MODE_CPHA) 253a0fd2339SIan Lepore reg |= 1u << CFGREG_SCLKPHA_SHIFT; 254a0fd2339SIan Lepore if (mode & SPIBUS_MODE_CPOL) { 255a0fd2339SIan Lepore reg |= 1u << CFGREG_SCLKPOL_SHIFT; 256a0fd2339SIan Lepore reg |= 1u << CFGREG_SCLKCTL_SHIFT; 257a0fd2339SIan Lepore } 258a0fd2339SIan Lepore WR4(sc, ECSPI_CFGREG, reg); 259a0fd2339SIan Lepore 260a0fd2339SIan Lepore /* 261a0fd2339SIan Lepore * Set up the rx/tx FIFO interrupt thresholds. 262a0fd2339SIan Lepore */ 263a0fd2339SIan Lepore reg = (FIFO_RXTHRESH << DMA_RX_THRESH_SHIFT); 264a0fd2339SIan Lepore reg |= (FIFO_TXTHRESH << DMA_TX_THRESH_SHIFT); 265a0fd2339SIan Lepore WR4(sc, ECSPI_DMAREG, reg); 266a0fd2339SIan Lepore 267a0fd2339SIan Lepore /* 268a0fd2339SIan Lepore * Do a dummy read, to make sure the preceding writes reach the spi 269a0fd2339SIan Lepore * hardware before we assert any gpio chip select. 270a0fd2339SIan Lepore */ 271a0fd2339SIan Lepore (void)RD4(sc, ECSPI_CFGREG); 272a0fd2339SIan Lepore } 273a0fd2339SIan Lepore 274a0fd2339SIan Lepore static void 275a0fd2339SIan Lepore spi_empty_rxfifo(struct spi_softc *sc) 276a0fd2339SIan Lepore { 277a0fd2339SIan Lepore 278a0fd2339SIan Lepore while (sc->rxidx < sc->rxlen && (RD4(sc, ECSPI_STATREG) & SREG_RR)) { 279a0fd2339SIan Lepore sc->rxbuf[sc->rxidx++] = (uint8_t)RD4(sc, ECSPI_RXDATA); 280a0fd2339SIan Lepore --sc->fifocnt; 281a0fd2339SIan Lepore } 282a0fd2339SIan Lepore } 283a0fd2339SIan Lepore 284a0fd2339SIan Lepore static void 285a0fd2339SIan Lepore spi_fill_txfifo(struct spi_softc *sc) 286a0fd2339SIan Lepore { 287a0fd2339SIan Lepore 288a0fd2339SIan Lepore while (sc->txidx < sc->txlen && sc->fifocnt < FIFO_SIZE) { 289a0fd2339SIan Lepore WR4(sc, ECSPI_TXDATA, sc->txbuf[sc->txidx++]); 290a0fd2339SIan Lepore ++sc->fifocnt; 291a0fd2339SIan Lepore } 292a0fd2339SIan Lepore 293a0fd2339SIan Lepore /* 294a0fd2339SIan Lepore * If we're out of data, disable tx data ready (threshold) interrupts, 295a0fd2339SIan Lepore * and enable tx fifo empty interrupts. 296a0fd2339SIan Lepore */ 297a0fd2339SIan Lepore if (sc->txidx == sc->txlen) 298a0fd2339SIan Lepore sc->intreg = (sc->intreg & ~INTREG_TDREN) | INTREG_TEEN; 299a0fd2339SIan Lepore } 300a0fd2339SIan Lepore 301a0fd2339SIan Lepore static void 302a0fd2339SIan Lepore spi_intr(void *arg) 303a0fd2339SIan Lepore { 304a0fd2339SIan Lepore struct spi_softc *sc = arg; 305a0fd2339SIan Lepore uint32_t intreg, status; 306a0fd2339SIan Lepore 307a0fd2339SIan Lepore mtx_lock(&sc->mtx); 308a0fd2339SIan Lepore 309a0fd2339SIan Lepore sc = arg; 310a0fd2339SIan Lepore intreg = sc->intreg; 311a0fd2339SIan Lepore status = RD4(sc, ECSPI_STATREG); 312a0fd2339SIan Lepore WR4(sc, ECSPI_STATREG, status); /* Clear w1c bits. */ 313a0fd2339SIan Lepore 314a0fd2339SIan Lepore /* 315a0fd2339SIan Lepore * If we get an overflow error, just signal that the transfer is done 316a0fd2339SIan Lepore * and wakeup the waiting thread, which will see that txidx != txlen and 317a0fd2339SIan Lepore * return an IO error to the caller. 318a0fd2339SIan Lepore */ 319a0fd2339SIan Lepore if (__predict_false(status & SREG_RO)) { 320a0fd2339SIan Lepore if (sc->debug || bootverbose) { 321a0fd2339SIan Lepore device_printf(sc->dev, "rxoverflow rxidx %u txidx %u\n", 322a0fd2339SIan Lepore sc->rxidx, sc->txidx); 323a0fd2339SIan Lepore } 324a0fd2339SIan Lepore sc->intreg = 0; 325a0fd2339SIan Lepore wakeup(sc); 326a0fd2339SIan Lepore mtx_unlock(&sc->mtx); 327a0fd2339SIan Lepore return; 328a0fd2339SIan Lepore } 329a0fd2339SIan Lepore 330a0fd2339SIan Lepore if (status & SREG_RR) 331a0fd2339SIan Lepore spi_empty_rxfifo(sc); 332a0fd2339SIan Lepore 333a0fd2339SIan Lepore if (status & SREG_TDR) 334a0fd2339SIan Lepore spi_fill_txfifo(sc); 335a0fd2339SIan Lepore 336a0fd2339SIan Lepore /* 337a0fd2339SIan Lepore * If we're out of bytes to send... 338a0fd2339SIan Lepore * - If Transfer Complete is set (shift register is empty) and we've 339a0fd2339SIan Lepore * received everything we expect, we're all done. 340a0fd2339SIan Lepore * - Else if Tx Fifo Empty is set, we need to stop waiting for that and 341a0fd2339SIan Lepore * switch to waiting for Transfer Complete (wait for shift register 342a0fd2339SIan Lepore * to empty out), and also for Receive Ready (last of incoming data). 343a0fd2339SIan Lepore */ 344a0fd2339SIan Lepore if (sc->txidx == sc->txlen) { 345a0fd2339SIan Lepore if ((status & SREG_TC) && sc->fifocnt == 0) { 346a0fd2339SIan Lepore sc->intreg = 0; 347a0fd2339SIan Lepore wakeup(sc); 348a0fd2339SIan Lepore } else if (status & SREG_TE) { 349a0fd2339SIan Lepore sc->intreg &= ~(sc->intreg & ~INTREG_TEEN); 350a0fd2339SIan Lepore sc->intreg |= INTREG_TCEN | INTREG_RREN; 351a0fd2339SIan Lepore } 352a0fd2339SIan Lepore } 353a0fd2339SIan Lepore 354a0fd2339SIan Lepore /* 355a0fd2339SIan Lepore * If interrupt flags changed, write the new flags to the hardware and 356a0fd2339SIan Lepore * do a dummy readback to ensure the changes reach the hardware before 357a0fd2339SIan Lepore * we exit the isr. 358a0fd2339SIan Lepore */ 359a0fd2339SIan Lepore if (sc->intreg != intreg) { 360a0fd2339SIan Lepore WR4(sc, ECSPI_INTREG, sc->intreg); 361a0fd2339SIan Lepore (void)RD4(sc, ECSPI_INTREG); 362a0fd2339SIan Lepore } 363a0fd2339SIan Lepore 364a0fd2339SIan Lepore if (sc->debug >= 3) { 365a0fd2339SIan Lepore device_printf(sc->dev, 366a0fd2339SIan Lepore "spi_intr, sreg 0x%08x intreg was 0x%08x now 0x%08x\n", 367a0fd2339SIan Lepore status, intreg, sc->intreg); 368a0fd2339SIan Lepore } 369a0fd2339SIan Lepore 370a0fd2339SIan Lepore mtx_unlock(&sc->mtx); 371a0fd2339SIan Lepore } 372a0fd2339SIan Lepore 373a0fd2339SIan Lepore static int 374a0fd2339SIan Lepore spi_xfer_buf(struct spi_softc *sc, void *rxbuf, void *txbuf, uint32_t len) 375a0fd2339SIan Lepore { 376a0fd2339SIan Lepore int err; 377a0fd2339SIan Lepore 378a0fd2339SIan Lepore if (sc->debug >= 1) { 379a0fd2339SIan Lepore device_printf(sc->dev, 380a0fd2339SIan Lepore "spi_xfer_buf, rxbuf %p txbuf %p len %u\n", 381a0fd2339SIan Lepore rxbuf, txbuf, len); 382a0fd2339SIan Lepore } 383a0fd2339SIan Lepore 384a0fd2339SIan Lepore if (len == 0) 385a0fd2339SIan Lepore return (0); 386a0fd2339SIan Lepore 387a0fd2339SIan Lepore sc->rxbuf = rxbuf; 388a0fd2339SIan Lepore sc->rxlen = len; 389a0fd2339SIan Lepore sc->rxidx = 0; 390a0fd2339SIan Lepore sc->txbuf = txbuf; 391a0fd2339SIan Lepore sc->txlen = len; 392a0fd2339SIan Lepore sc->txidx = 0; 393a0fd2339SIan Lepore sc->intreg = INTREG_RDREN | INTREG_TDREN; 394a0fd2339SIan Lepore spi_fill_txfifo(sc); 395a0fd2339SIan Lepore 396a0fd2339SIan Lepore /* Enable interrupts last; spi_fill_txfifo() can change sc->intreg */ 397a0fd2339SIan Lepore WR4(sc, ECSPI_INTREG, sc->intreg); 398a0fd2339SIan Lepore 399a0fd2339SIan Lepore err = 0; 400a0fd2339SIan Lepore while (err == 0 && sc->intreg != 0) 401a0fd2339SIan Lepore err = msleep(sc, &sc->mtx, 0, "imxspi", 10 * hz); 402a0fd2339SIan Lepore 403a0fd2339SIan Lepore if (sc->rxidx != sc->rxlen || sc->txidx != sc->txlen) 404a0fd2339SIan Lepore err = EIO; 405a0fd2339SIan Lepore 406a0fd2339SIan Lepore return (err); 407a0fd2339SIan Lepore } 408a0fd2339SIan Lepore 409a0fd2339SIan Lepore static int 410a0fd2339SIan Lepore spi_transfer(device_t dev, device_t child, struct spi_command *cmd) 411a0fd2339SIan Lepore { 412a0fd2339SIan Lepore struct spi_softc *sc = device_get_softc(dev); 413a0fd2339SIan Lepore uint32_t cs, mode, clock; 414a0fd2339SIan Lepore int err; 415a0fd2339SIan Lepore 416a0fd2339SIan Lepore spibus_get_cs(child, &cs); 417a0fd2339SIan Lepore spibus_get_clock(child, &clock); 418a0fd2339SIan Lepore spibus_get_mode(child, &mode); 419a0fd2339SIan Lepore 420a0fd2339SIan Lepore if (cs > CS_MAX || sc->cspins[cs] == NULL) { 421a0fd2339SIan Lepore if (sc->debug || bootverbose) 422a0fd2339SIan Lepore device_printf(sc->dev, "Invalid chip select %u\n", cs); 423a0fd2339SIan Lepore return (EINVAL); 424a0fd2339SIan Lepore } 425a0fd2339SIan Lepore 426a0fd2339SIan Lepore mtx_lock(&sc->mtx); 4276daa4a40SIan Lepore device_busy(sc->dev); 428a0fd2339SIan Lepore 429a0fd2339SIan Lepore if (sc->debug >= 1) { 430a0fd2339SIan Lepore device_printf(sc->dev, 431a0fd2339SIan Lepore "spi_transfer, cs 0x%x clock %u mode %u\n", 432a0fd2339SIan Lepore cs, clock, mode); 433a0fd2339SIan Lepore } 434a0fd2339SIan Lepore 435a0fd2339SIan Lepore /* Set up the hardware and select the device. */ 436a0fd2339SIan Lepore spi_hw_setup(sc, cs, mode, clock); 437a0fd2339SIan Lepore spi_set_chipsel(sc, cs, true); 438a0fd2339SIan Lepore 439a0fd2339SIan Lepore /* Transfer command then data bytes. */ 440a0fd2339SIan Lepore err = 0; 441a0fd2339SIan Lepore if (cmd->tx_cmd_sz > 0) 442a0fd2339SIan Lepore err = spi_xfer_buf(sc, cmd->rx_cmd, cmd->tx_cmd, 443a0fd2339SIan Lepore cmd->tx_cmd_sz); 444a0fd2339SIan Lepore if (cmd->tx_data_sz > 0 && err == 0) 445a0fd2339SIan Lepore err = spi_xfer_buf(sc, cmd->rx_data, cmd->tx_data, 446a0fd2339SIan Lepore cmd->tx_data_sz); 447a0fd2339SIan Lepore 448a0fd2339SIan Lepore /* Deselect the device, turn off (and reset) hardware. */ 449a0fd2339SIan Lepore spi_set_chipsel(sc, cs, false); 450a0fd2339SIan Lepore WR4(sc, ECSPI_CTLREG, 0); 451a0fd2339SIan Lepore 4526daa4a40SIan Lepore device_unbusy(sc->dev); 453a0fd2339SIan Lepore mtx_unlock(&sc->mtx); 454a0fd2339SIan Lepore 455a0fd2339SIan Lepore return (err); 456a0fd2339SIan Lepore } 457a0fd2339SIan Lepore 458a0fd2339SIan Lepore static phandle_t 459a0fd2339SIan Lepore spi_get_node(device_t bus, device_t dev) 460a0fd2339SIan Lepore { 461a0fd2339SIan Lepore 462a0fd2339SIan Lepore /* 463a0fd2339SIan Lepore * Share our controller node with our spibus child; it instantiates 464a0fd2339SIan Lepore * devices by walking the children contained within our node. 465a0fd2339SIan Lepore */ 466a0fd2339SIan Lepore return ofw_bus_get_node(bus); 467a0fd2339SIan Lepore } 468a0fd2339SIan Lepore 469a0fd2339SIan Lepore static int 470a0fd2339SIan Lepore spi_detach(device_t dev) 471a0fd2339SIan Lepore { 472a0fd2339SIan Lepore struct spi_softc *sc = device_get_softc(dev); 4736daa4a40SIan Lepore int error, idx; 474a0fd2339SIan Lepore 4756daa4a40SIan Lepore if ((error = bus_generic_detach(sc->dev)) != 0) 4766daa4a40SIan Lepore return (error); 477a0fd2339SIan Lepore 478a0fd2339SIan Lepore if (sc->spibus != NULL) 479a0fd2339SIan Lepore device_delete_child(dev, sc->spibus); 480a0fd2339SIan Lepore 481a0fd2339SIan Lepore for (idx = 0; idx < nitems(sc->cspins); ++idx) { 482a0fd2339SIan Lepore if (sc->cspins[idx] != NULL) 483a0fd2339SIan Lepore gpio_pin_release(sc->cspins[idx]); 484a0fd2339SIan Lepore } 485a0fd2339SIan Lepore 486a0fd2339SIan Lepore if (sc->inthandle != NULL) 487a0fd2339SIan Lepore bus_teardown_intr(sc->dev, sc->intres, sc->inthandle); 488a0fd2339SIan Lepore if (sc->intres != NULL) 489a0fd2339SIan Lepore bus_release_resource(sc->dev, SYS_RES_IRQ, 0, sc->intres); 490a0fd2339SIan Lepore if (sc->memres != NULL) 491a0fd2339SIan Lepore bus_release_resource(sc->dev, SYS_RES_MEMORY, 0, sc->memres); 492a0fd2339SIan Lepore 493a0fd2339SIan Lepore mtx_destroy(&sc->mtx); 494a0fd2339SIan Lepore 495a0fd2339SIan Lepore return (0); 496a0fd2339SIan Lepore } 497a0fd2339SIan Lepore 498a0fd2339SIan Lepore static int 499a0fd2339SIan Lepore spi_attach(device_t dev) 500a0fd2339SIan Lepore { 501a0fd2339SIan Lepore struct spi_softc *sc = device_get_softc(dev); 502a0fd2339SIan Lepore phandle_t node; 503a0fd2339SIan Lepore int err, idx, rid; 504a0fd2339SIan Lepore 505a0fd2339SIan Lepore sc->dev = dev; 506a0fd2339SIan Lepore sc->basefreq = imx_ccm_ecspi_hz(); 507a0fd2339SIan Lepore 508a0fd2339SIan Lepore mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); 509a0fd2339SIan Lepore 510a0fd2339SIan Lepore /* Set up debug-enable sysctl. */ 511a0fd2339SIan Lepore SYSCTL_ADD_INT(device_get_sysctl_ctx(sc->dev), 512a0fd2339SIan Lepore SYSCTL_CHILDREN(device_get_sysctl_tree(sc->dev)), 513a0fd2339SIan Lepore OID_AUTO, "debug", CTLFLAG_RWTUN, &sc->debug, 0, 514a0fd2339SIan Lepore "Enable debug, higher values = more info"); 515a0fd2339SIan Lepore 516a0fd2339SIan Lepore /* Allocate mmio register access resources. */ 517a0fd2339SIan Lepore rid = 0; 518a0fd2339SIan Lepore sc->memres = bus_alloc_resource_any(sc->dev, SYS_RES_MEMORY, &rid, 519a0fd2339SIan Lepore RF_ACTIVE); 520a0fd2339SIan Lepore if (sc->memres == NULL) { 521a0fd2339SIan Lepore device_printf(sc->dev, "could not allocate registers\n"); 522a0fd2339SIan Lepore spi_detach(sc->dev); 523a0fd2339SIan Lepore return (ENXIO); 524a0fd2339SIan Lepore } 525a0fd2339SIan Lepore 526a0fd2339SIan Lepore /* Allocate interrupt resources and set up handler. */ 527a0fd2339SIan Lepore rid = 0; 528a0fd2339SIan Lepore sc->intres = bus_alloc_resource_any(sc->dev, SYS_RES_IRQ, &rid, 529a0fd2339SIan Lepore RF_ACTIVE); 530a0fd2339SIan Lepore if (sc->intres == NULL) { 531a0fd2339SIan Lepore device_printf(sc->dev, "could not allocate interrupt\n"); 532a0fd2339SIan Lepore device_detach(sc->dev); 533a0fd2339SIan Lepore return (ENXIO); 534a0fd2339SIan Lepore } 535a0fd2339SIan Lepore err = bus_setup_intr(sc->dev, sc->intres, INTR_TYPE_MISC | INTR_MPSAFE, 536a0fd2339SIan Lepore NULL, spi_intr, sc, &sc->inthandle); 537a0fd2339SIan Lepore if (err != 0) { 538a0fd2339SIan Lepore device_printf(sc->dev, "could not setup interrupt handler"); 539a0fd2339SIan Lepore device_detach(sc->dev); 540a0fd2339SIan Lepore return (ENXIO); 541a0fd2339SIan Lepore } 542a0fd2339SIan Lepore 543a0fd2339SIan Lepore /* Allocate gpio pins for configured chip selects. */ 544a0fd2339SIan Lepore node = ofw_bus_get_node(sc->dev); 545a3389cb7SIan Lepore for (idx = 0; idx < nitems(sc->cspins); ++idx) { 546a0fd2339SIan Lepore err = gpio_pin_get_by_ofw_propidx(sc->dev, node, "cs-gpios", 547a0fd2339SIan Lepore idx, &sc->cspins[idx]); 548a0fd2339SIan Lepore if (err == 0) { 549a0fd2339SIan Lepore gpio_pin_setflags(sc->cspins[idx], GPIO_PIN_OUTPUT); 550a0fd2339SIan Lepore } else if (sc->debug >= 2) { 551a0fd2339SIan Lepore device_printf(sc->dev, 552a0fd2339SIan Lepore "cannot configure gpio for chip select %u\n", idx); 553a0fd2339SIan Lepore } 554a0fd2339SIan Lepore } 555a0fd2339SIan Lepore 556a0fd2339SIan Lepore /* 557a0fd2339SIan Lepore * Hardware init: put all channels into Master mode, turn off the enable 558a0fd2339SIan Lepore * bit (gates off clocks); we only enable the hardware while xfers run. 559a0fd2339SIan Lepore */ 560a0fd2339SIan Lepore WR4(sc, ECSPI_CTLREG, CTLREG_CMODES_MASTER); 561a0fd2339SIan Lepore 562b1dbbe4dSIan Lepore /* 563b1dbbe4dSIan Lepore * Add the spibus driver as a child, and setup a one-shot intrhook to 564b1dbbe4dSIan Lepore * attach it after interrupts are working. It will attach actual SPI 565b1dbbe4dSIan Lepore * devices as its children, and those devices may need to do IO during 566b1dbbe4dSIan Lepore * their attach. We can't do IO until timers and interrupts are working. 567b1dbbe4dSIan Lepore */ 568a0fd2339SIan Lepore sc->spibus = device_add_child(dev, "spibus", -1); 569b832a7e5SWarner Losh return (bus_delayed_attach_children(dev)); 570a0fd2339SIan Lepore } 571a0fd2339SIan Lepore 572a0fd2339SIan Lepore static int 573a0fd2339SIan Lepore spi_probe(device_t dev) 574a0fd2339SIan Lepore { 575a0fd2339SIan Lepore 576a0fd2339SIan Lepore if (!ofw_bus_status_okay(dev)) 577a0fd2339SIan Lepore return (ENXIO); 578a0fd2339SIan Lepore 579a0fd2339SIan Lepore if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 580a0fd2339SIan Lepore return (ENXIO); 581a0fd2339SIan Lepore 582a0fd2339SIan Lepore device_set_desc(dev, "i.MX ECSPI Master"); 583a0fd2339SIan Lepore return (BUS_PROBE_DEFAULT); 584a0fd2339SIan Lepore } 585a0fd2339SIan Lepore 586a0fd2339SIan Lepore static device_method_t spi_methods[] = { 587a0fd2339SIan Lepore DEVMETHOD(device_probe, spi_probe), 588a0fd2339SIan Lepore DEVMETHOD(device_attach, spi_attach), 589a0fd2339SIan Lepore DEVMETHOD(device_detach, spi_detach), 590a0fd2339SIan Lepore 591a0fd2339SIan Lepore /* spibus_if */ 592a0fd2339SIan Lepore DEVMETHOD(spibus_transfer, spi_transfer), 593a0fd2339SIan Lepore 594a0fd2339SIan Lepore /* ofw_bus_if */ 595a0fd2339SIan Lepore DEVMETHOD(ofw_bus_get_node, spi_get_node), 596a0fd2339SIan Lepore 597a0fd2339SIan Lepore DEVMETHOD_END 598a0fd2339SIan Lepore }; 599a0fd2339SIan Lepore 600a0fd2339SIan Lepore static driver_t spi_driver = { 601a0fd2339SIan Lepore "imx_spi", 602a0fd2339SIan Lepore spi_methods, 603a0fd2339SIan Lepore sizeof(struct spi_softc), 604a0fd2339SIan Lepore }; 605a0fd2339SIan Lepore 606ea538dabSJohn Baldwin DRIVER_MODULE(imx_spi, simplebus, spi_driver, 0, 0); 6075f31d14aSJohn Baldwin DRIVER_MODULE(ofw_spibus, imx_spi, ofw_spibus_driver, 0, 0); 608a0fd2339SIan Lepore MODULE_DEPEND(imx_spi, ofw_spibus, 1, 1, 1); 6094c2b4396SIan Lepore SIMPLEBUS_PNP_INFO(compat_data); 610