1ec2b0ccdSEmmanuel Vadot /*- 2ec2b0ccdSEmmanuel Vadot * Copyright (c) 2018 Emmanuel Vadot <manu@FreeBSD.org> 3ec2b0ccdSEmmanuel Vadot * 4ec2b0ccdSEmmanuel Vadot * Redistribution and use in source and binary forms, with or without 5ec2b0ccdSEmmanuel Vadot * modification, are permitted provided that the following conditions 6ec2b0ccdSEmmanuel Vadot * are met: 7ec2b0ccdSEmmanuel Vadot * 1. Redistributions of source code must retain the above copyright 8ec2b0ccdSEmmanuel Vadot * notice, this list of conditions and the following disclaimer. 9ec2b0ccdSEmmanuel Vadot * 2. Redistributions in binary form must reproduce the above copyright 10ec2b0ccdSEmmanuel Vadot * notice, this list of conditions and the following disclaimer in the 11ec2b0ccdSEmmanuel Vadot * documentation and/or other materials provided with the distribution. 12ec2b0ccdSEmmanuel Vadot * 13ec2b0ccdSEmmanuel Vadot * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14ec2b0ccdSEmmanuel Vadot * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15ec2b0ccdSEmmanuel Vadot * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16ec2b0ccdSEmmanuel Vadot * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 17ec2b0ccdSEmmanuel Vadot * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 18ec2b0ccdSEmmanuel Vadot * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 19ec2b0ccdSEmmanuel Vadot * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 20ec2b0ccdSEmmanuel Vadot * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21ec2b0ccdSEmmanuel Vadot * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 22ec2b0ccdSEmmanuel Vadot * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 23ec2b0ccdSEmmanuel Vadot * SUCH DAMAGE. 24ec2b0ccdSEmmanuel Vadot */ 25ec2b0ccdSEmmanuel Vadot 26ec2b0ccdSEmmanuel Vadot #include <sys/param.h> 27ec2b0ccdSEmmanuel Vadot #include <sys/systm.h> 28ec2b0ccdSEmmanuel Vadot #include <sys/bus.h> 29ec2b0ccdSEmmanuel Vadot #include <sys/kernel.h> 30ec2b0ccdSEmmanuel Vadot #include <sys/lock.h> 31ec2b0ccdSEmmanuel Vadot #include <sys/module.h> 32ec2b0ccdSEmmanuel Vadot #include <sys/mutex.h> 33ec2b0ccdSEmmanuel Vadot #include <sys/rman.h> 34ec2b0ccdSEmmanuel Vadot #include <sys/resource.h> 35ec2b0ccdSEmmanuel Vadot #include <machine/bus.h> 36ec2b0ccdSEmmanuel Vadot 37ec2b0ccdSEmmanuel Vadot #include <dev/ofw/ofw_bus.h> 38ec2b0ccdSEmmanuel Vadot #include <dev/ofw/ofw_bus_subr.h> 39ec2b0ccdSEmmanuel Vadot 40ec2b0ccdSEmmanuel Vadot #include <dev/spibus/spi.h> 41ec2b0ccdSEmmanuel Vadot #include <dev/spibus/spibusvar.h> 42ec2b0ccdSEmmanuel Vadot 43be82b3a0SEmmanuel Vadot #include <dev/clk/clk.h> 441f469a9fSEmmanuel Vadot #include <dev/hwreset/hwreset.h> 45ec2b0ccdSEmmanuel Vadot 46ec2b0ccdSEmmanuel Vadot #include "spibus_if.h" 47ec2b0ccdSEmmanuel Vadot 48ec2b0ccdSEmmanuel Vadot #define AW_SPI_GCR 0x04 /* Global Control Register */ 49ec2b0ccdSEmmanuel Vadot #define AW_SPI_GCR_EN (1 << 0) /* ENable */ 50ec2b0ccdSEmmanuel Vadot #define AW_SPI_GCR_MODE_MASTER (1 << 1) /* 1 = Master, 0 = Slave */ 51ec2b0ccdSEmmanuel Vadot #define AW_SPI_GCR_TP_EN (1 << 7) /* 1 = Stop transmit when FIFO is full */ 52ec2b0ccdSEmmanuel Vadot #define AW_SPI_GCR_SRST (1 << 31) /* Soft Reset */ 53ec2b0ccdSEmmanuel Vadot 54ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR 0x08 /* Transfer Control register */ 55ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_XCH (1 << 31) /* Initiate transfer */ 56ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SDDM (1 << 14) /* Sending Delay Data Mode */ 57ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SDM (1 << 13) /* Master Sample Data Mode */ 58ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_FBS (1 << 12) /* First Transmit Bit Select (1 == LSB) */ 59ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SDC (1 << 11) /* Master Sample Data Control */ 60ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_RPSM (1 << 10) /* Rapid Mode Select */ 61ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_DDB (1 << 9) /* Dummy Burst Type */ 62ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SSSEL_MASK 0x30 /* Chip select */ 63ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SSSEL_SHIFT 4 64ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SS_LEVEL (1 << 7) /* 1 == CS High */ 65ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SS_OWNER (1 << 6) /* 1 == Software controlled */ 66ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_SPOL (1 << 2) /* 1 == Active low */ 67ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_CPOL (1 << 1) /* 1 == Active low */ 68ec2b0ccdSEmmanuel Vadot #define AW_SPI_TCR_CPHA (1 << 0) /* 1 == Phase 1 */ 69ec2b0ccdSEmmanuel Vadot 70ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER 0x10 /* Interrupt Control Register */ 71ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_SS (1 << 13) /* Chip select went from valid to invalid */ 72ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_TC (1 << 12) /* Transfer complete */ 73ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_TF_UDR (1 << 11) /* TXFIFO underrun */ 74ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_TF_OVF (1 << 10) /* TXFIFO overrun */ 75ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_RF_UDR (1 << 9) /* RXFIFO underrun */ 76ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_RF_OVF (1 << 8) /* RXFIFO overrun */ 77ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_TF_FULL (1 << 6) /* TXFIFO Full */ 78ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_TF_EMP (1 << 5) /* TXFIFO Empty */ 79ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_TF_ERQ (1 << 4) /* TXFIFO Empty Request */ 80ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_RF_FULL (1 << 2) /* RXFIFO Full */ 81ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_RF_EMP (1 << 1) /* RXFIFO Empty */ 82ec2b0ccdSEmmanuel Vadot #define AW_SPI_IER_RF_RDY (1 << 0) /* RXFIFO Ready Request */ 83ec2b0ccdSEmmanuel Vadot 84ec2b0ccdSEmmanuel Vadot #define AW_SPI_ISR 0x14 /* Interrupt Status Register */ 85ec2b0ccdSEmmanuel Vadot 86ec2b0ccdSEmmanuel Vadot #define AW_SPI_FCR 0x18 /* FIFO Control Register */ 87ec2b0ccdSEmmanuel Vadot #define AW_SPI_FCR_TX_RST (1 << 31) /* Reset TX FIFO */ 88ec2b0ccdSEmmanuel Vadot #define AW_SPI_FCR_TX_TRIG_MASK 0xFF0000 /* TX FIFO Trigger level */ 89ec2b0ccdSEmmanuel Vadot #define AW_SPI_FCR_TX_TRIG_SHIFT 16 90ec2b0ccdSEmmanuel Vadot #define AW_SPI_FCR_RX_RST (1 << 15) /* Reset RX FIFO */ 91ec2b0ccdSEmmanuel Vadot #define AW_SPI_FCR_RX_TRIG_MASK 0xFF /* RX FIFO Trigger level */ 92ec2b0ccdSEmmanuel Vadot #define AW_SPI_FCR_RX_TRIG_SHIFT 0 93ec2b0ccdSEmmanuel Vadot 94ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR 0x1C /* FIFO Status Register */ 95ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_TB_WR (1 << 31) 96ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_TB_CNT_MASK 0x70000000 97ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_TB_CNT_SHIFT 28 98ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_TF_CNT_MASK 0xFF0000 99ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_TF_CNT_SHIFT 16 100ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_RB_WR (1 << 15) 101ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_RB_CNT_MASK 0x7000 102ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_RB_CNT_SHIFT 12 103ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_RF_CNT_MASK 0xFF 104ec2b0ccdSEmmanuel Vadot #define AW_SPI_FSR_RF_CNT_SHIFT 0 105ec2b0ccdSEmmanuel Vadot 106ec2b0ccdSEmmanuel Vadot #define AW_SPI_WCR 0x20 /* Wait Clock Counter Register */ 107ec2b0ccdSEmmanuel Vadot 108ec2b0ccdSEmmanuel Vadot #define AW_SPI_CCR 0x24 /* Clock Rate Control Register */ 109ec2b0ccdSEmmanuel Vadot #define AW_SPI_CCR_DRS (1 << 12) /* Clock divider select */ 110ec2b0ccdSEmmanuel Vadot #define AW_SPI_CCR_CDR1_MASK 0xF00 111ec2b0ccdSEmmanuel Vadot #define AW_SPI_CCR_CDR1_SHIFT 8 112ec2b0ccdSEmmanuel Vadot #define AW_SPI_CCR_CDR2_MASK 0xFF 113ec2b0ccdSEmmanuel Vadot #define AW_SPI_CCR_CDR2_SHIFT 0 114ec2b0ccdSEmmanuel Vadot 115ec2b0ccdSEmmanuel Vadot #define AW_SPI_MBC 0x30 /* Burst Counter Register */ 116ec2b0ccdSEmmanuel Vadot #define AW_SPI_MTC 0x34 /* Transmit Counter Register */ 117ec2b0ccdSEmmanuel Vadot #define AW_SPI_BCC 0x38 /* Burst Control Register */ 118ec2b0ccdSEmmanuel Vadot #define AW_SPI_MDMA_CTL 0x88 /* Normal DMA Control Register */ 119ec2b0ccdSEmmanuel Vadot #define AW_SPI_TXD 0x200 /* TX Data Register */ 120ec2b0ccdSEmmanuel Vadot #define AW_SPI_RDX 0x300 /* RX Data Register */ 121ec2b0ccdSEmmanuel Vadot 122ec2b0ccdSEmmanuel Vadot #define AW_SPI_MAX_CS 4 123ec2b0ccdSEmmanuel Vadot #define AW_SPI_FIFO_SIZE 64 124ec2b0ccdSEmmanuel Vadot 125ec2b0ccdSEmmanuel Vadot static struct ofw_compat_data compat_data[] = { 126ec2b0ccdSEmmanuel Vadot { "allwinner,sun8i-h3-spi", 1 }, 127ec2b0ccdSEmmanuel Vadot { NULL, 0 } 128ec2b0ccdSEmmanuel Vadot }; 129ec2b0ccdSEmmanuel Vadot 130ec2b0ccdSEmmanuel Vadot static struct resource_spec aw_spi_spec[] = { 131ec2b0ccdSEmmanuel Vadot { SYS_RES_MEMORY, 0, RF_ACTIVE }, 132ec2b0ccdSEmmanuel Vadot { SYS_RES_IRQ, 0, RF_ACTIVE | RF_SHAREABLE }, 133ec2b0ccdSEmmanuel Vadot { -1, 0 } 134ec2b0ccdSEmmanuel Vadot }; 135ec2b0ccdSEmmanuel Vadot 136ec2b0ccdSEmmanuel Vadot struct aw_spi_softc { 137ec2b0ccdSEmmanuel Vadot device_t dev; 138ec2b0ccdSEmmanuel Vadot device_t spibus; 139ec2b0ccdSEmmanuel Vadot struct resource *res[2]; 140ec2b0ccdSEmmanuel Vadot struct mtx mtx; 141ec2b0ccdSEmmanuel Vadot clk_t clk_ahb; 142ec2b0ccdSEmmanuel Vadot clk_t clk_mod; 143ec2b0ccdSEmmanuel Vadot uint64_t mod_freq; 144ec2b0ccdSEmmanuel Vadot hwreset_t rst_ahb; 145ec2b0ccdSEmmanuel Vadot void * intrhand; 146ec2b0ccdSEmmanuel Vadot int transfer; 147ec2b0ccdSEmmanuel Vadot 148ec2b0ccdSEmmanuel Vadot uint8_t *rxbuf; 149ec2b0ccdSEmmanuel Vadot uint32_t rxcnt; 150ec2b0ccdSEmmanuel Vadot uint8_t *txbuf; 151ec2b0ccdSEmmanuel Vadot uint32_t txcnt; 152ec2b0ccdSEmmanuel Vadot uint32_t txlen; 153ec2b0ccdSEmmanuel Vadot uint32_t rxlen; 154ec2b0ccdSEmmanuel Vadot }; 155ec2b0ccdSEmmanuel Vadot 156ec2b0ccdSEmmanuel Vadot #define AW_SPI_LOCK(sc) mtx_lock(&(sc)->mtx) 157ec2b0ccdSEmmanuel Vadot #define AW_SPI_UNLOCK(sc) mtx_unlock(&(sc)->mtx) 158ec2b0ccdSEmmanuel Vadot #define AW_SPI_ASSERT_LOCKED(sc) mtx_assert(&(sc)->mtx, MA_OWNED) 159ec2b0ccdSEmmanuel Vadot #define AW_SPI_READ_1(sc, reg) bus_read_1((sc)->res[0], (reg)) 160ec2b0ccdSEmmanuel Vadot #define AW_SPI_WRITE_1(sc, reg, val) bus_write_1((sc)->res[0], (reg), (val)) 161ec2b0ccdSEmmanuel Vadot #define AW_SPI_READ_4(sc, reg) bus_read_4((sc)->res[0], (reg)) 162ec2b0ccdSEmmanuel Vadot #define AW_SPI_WRITE_4(sc, reg, val) bus_write_4((sc)->res[0], (reg), (val)) 163ec2b0ccdSEmmanuel Vadot 164ec2b0ccdSEmmanuel Vadot static int aw_spi_probe(device_t dev); 165ec2b0ccdSEmmanuel Vadot static int aw_spi_attach(device_t dev); 166ec2b0ccdSEmmanuel Vadot static int aw_spi_detach(device_t dev); 167ec2b0ccdSEmmanuel Vadot static int aw_spi_intr(void *arg); 168ec2b0ccdSEmmanuel Vadot 169ec2b0ccdSEmmanuel Vadot static int 170ec2b0ccdSEmmanuel Vadot aw_spi_probe(device_t dev) 171ec2b0ccdSEmmanuel Vadot { 172ec2b0ccdSEmmanuel Vadot if (!ofw_bus_status_okay(dev)) 173ec2b0ccdSEmmanuel Vadot return (ENXIO); 174ec2b0ccdSEmmanuel Vadot 175ec2b0ccdSEmmanuel Vadot if (!ofw_bus_search_compatible(dev, compat_data)->ocd_data) 176ec2b0ccdSEmmanuel Vadot return (ENXIO); 177ec2b0ccdSEmmanuel Vadot 178ec2b0ccdSEmmanuel Vadot device_set_desc(dev, "Allwinner SPI"); 179ec2b0ccdSEmmanuel Vadot return (BUS_PROBE_DEFAULT); 180ec2b0ccdSEmmanuel Vadot } 181ec2b0ccdSEmmanuel Vadot 182ec2b0ccdSEmmanuel Vadot static int 183ec2b0ccdSEmmanuel Vadot aw_spi_attach(device_t dev) 184ec2b0ccdSEmmanuel Vadot { 185ec2b0ccdSEmmanuel Vadot struct aw_spi_softc *sc; 186ec2b0ccdSEmmanuel Vadot int error; 187ec2b0ccdSEmmanuel Vadot 188ec2b0ccdSEmmanuel Vadot sc = device_get_softc(dev); 189ec2b0ccdSEmmanuel Vadot sc->dev = dev; 190ec2b0ccdSEmmanuel Vadot 191ec2b0ccdSEmmanuel Vadot mtx_init(&sc->mtx, device_get_nameunit(dev), NULL, MTX_DEF); 192ec2b0ccdSEmmanuel Vadot 193ec2b0ccdSEmmanuel Vadot if (bus_alloc_resources(dev, aw_spi_spec, sc->res) != 0) { 194ec2b0ccdSEmmanuel Vadot device_printf(dev, "cannot allocate resources for device\n"); 195ec2b0ccdSEmmanuel Vadot error = ENXIO; 196ec2b0ccdSEmmanuel Vadot goto fail; 197ec2b0ccdSEmmanuel Vadot } 198ec2b0ccdSEmmanuel Vadot 199ec2b0ccdSEmmanuel Vadot if (bus_setup_intr(dev, sc->res[1], 200ec2b0ccdSEmmanuel Vadot INTR_TYPE_MISC | INTR_MPSAFE, aw_spi_intr, NULL, sc, 201ec2b0ccdSEmmanuel Vadot &sc->intrhand)) { 202ec2b0ccdSEmmanuel Vadot bus_release_resources(dev, aw_spi_spec, sc->res); 203ec2b0ccdSEmmanuel Vadot device_printf(dev, "cannot setup interrupt handler\n"); 204ec2b0ccdSEmmanuel Vadot return (ENXIO); 205ec2b0ccdSEmmanuel Vadot } 206ec2b0ccdSEmmanuel Vadot 207ec2b0ccdSEmmanuel Vadot /* De-assert reset */ 208ec2b0ccdSEmmanuel Vadot if (hwreset_get_by_ofw_idx(dev, 0, 0, &sc->rst_ahb) == 0) { 209ec2b0ccdSEmmanuel Vadot error = hwreset_deassert(sc->rst_ahb); 210ec2b0ccdSEmmanuel Vadot if (error != 0) { 211ec2b0ccdSEmmanuel Vadot device_printf(dev, "cannot de-assert reset\n"); 212ec2b0ccdSEmmanuel Vadot goto fail; 213ec2b0ccdSEmmanuel Vadot } 214ec2b0ccdSEmmanuel Vadot } 215ec2b0ccdSEmmanuel Vadot 216ec2b0ccdSEmmanuel Vadot /* Activate the module clock. */ 217ec2b0ccdSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "ahb", &sc->clk_ahb); 218ec2b0ccdSEmmanuel Vadot if (error != 0) { 219ec2b0ccdSEmmanuel Vadot device_printf(dev, "cannot get ahb clock\n"); 220ec2b0ccdSEmmanuel Vadot goto fail; 221ec2b0ccdSEmmanuel Vadot } 222ec2b0ccdSEmmanuel Vadot error = clk_get_by_ofw_name(dev, 0, "mod", &sc->clk_mod); 223ec2b0ccdSEmmanuel Vadot if (error != 0) { 224ec2b0ccdSEmmanuel Vadot device_printf(dev, "cannot get mod clock\n"); 225ec2b0ccdSEmmanuel Vadot goto fail; 226ec2b0ccdSEmmanuel Vadot } 227ec2b0ccdSEmmanuel Vadot error = clk_enable(sc->clk_ahb); 228ec2b0ccdSEmmanuel Vadot if (error != 0) { 229ec2b0ccdSEmmanuel Vadot device_printf(dev, "cannot enable ahb clock\n"); 230ec2b0ccdSEmmanuel Vadot goto fail; 231ec2b0ccdSEmmanuel Vadot } 232ec2b0ccdSEmmanuel Vadot error = clk_enable(sc->clk_mod); 233ec2b0ccdSEmmanuel Vadot if (error != 0) { 234ec2b0ccdSEmmanuel Vadot device_printf(dev, "cannot enable mod clock\n"); 235ec2b0ccdSEmmanuel Vadot goto fail; 236ec2b0ccdSEmmanuel Vadot } 237ec2b0ccdSEmmanuel Vadot 2385b56413dSWarner Losh sc->spibus = device_add_child(dev, "spibus", DEVICE_UNIT_ANY); 239ec2b0ccdSEmmanuel Vadot 240*18250ec6SJohn Baldwin bus_attach_children(dev); 241*18250ec6SJohn Baldwin return (0); 242ec2b0ccdSEmmanuel Vadot 243ec2b0ccdSEmmanuel Vadot fail: 244ec2b0ccdSEmmanuel Vadot aw_spi_detach(dev); 245ec2b0ccdSEmmanuel Vadot return (error); 246ec2b0ccdSEmmanuel Vadot } 247ec2b0ccdSEmmanuel Vadot 248ec2b0ccdSEmmanuel Vadot static int 249ec2b0ccdSEmmanuel Vadot aw_spi_detach(device_t dev) 250ec2b0ccdSEmmanuel Vadot { 251ec2b0ccdSEmmanuel Vadot struct aw_spi_softc *sc; 252ec2b0ccdSEmmanuel Vadot 253ec2b0ccdSEmmanuel Vadot sc = device_get_softc(dev); 254ec2b0ccdSEmmanuel Vadot 255ec2b0ccdSEmmanuel Vadot bus_generic_detach(sc->dev); 256ec2b0ccdSEmmanuel Vadot if (sc->spibus != NULL) 257ec2b0ccdSEmmanuel Vadot device_delete_child(dev, sc->spibus); 258ec2b0ccdSEmmanuel Vadot 259ec2b0ccdSEmmanuel Vadot if (sc->clk_mod != NULL) 260ec2b0ccdSEmmanuel Vadot clk_release(sc->clk_mod); 261ec2b0ccdSEmmanuel Vadot if (sc->clk_ahb) 262ec2b0ccdSEmmanuel Vadot clk_release(sc->clk_ahb); 263ec2b0ccdSEmmanuel Vadot if (sc->rst_ahb) 264ec2b0ccdSEmmanuel Vadot hwreset_assert(sc->rst_ahb); 265ec2b0ccdSEmmanuel Vadot 266ec2b0ccdSEmmanuel Vadot if (sc->intrhand != NULL) 267ec2b0ccdSEmmanuel Vadot bus_teardown_intr(sc->dev, sc->res[1], sc->intrhand); 268ec2b0ccdSEmmanuel Vadot 269ec2b0ccdSEmmanuel Vadot bus_release_resources(dev, aw_spi_spec, sc->res); 270ec2b0ccdSEmmanuel Vadot mtx_destroy(&sc->mtx); 271ec2b0ccdSEmmanuel Vadot 272ec2b0ccdSEmmanuel Vadot return (0); 273ec2b0ccdSEmmanuel Vadot } 274ec2b0ccdSEmmanuel Vadot 275ec2b0ccdSEmmanuel Vadot static phandle_t 276ec2b0ccdSEmmanuel Vadot aw_spi_get_node(device_t bus, device_t dev) 277ec2b0ccdSEmmanuel Vadot { 278ec2b0ccdSEmmanuel Vadot 279ec2b0ccdSEmmanuel Vadot return ofw_bus_get_node(bus); 280ec2b0ccdSEmmanuel Vadot } 281ec2b0ccdSEmmanuel Vadot 282ec2b0ccdSEmmanuel Vadot static void 283ec2b0ccdSEmmanuel Vadot aw_spi_setup_mode(struct aw_spi_softc *sc, uint32_t mode) 284ec2b0ccdSEmmanuel Vadot { 285ec2b0ccdSEmmanuel Vadot uint32_t reg; 286ec2b0ccdSEmmanuel Vadot 287ec2b0ccdSEmmanuel Vadot /* We only support master mode */ 288ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_GCR); 289ec2b0ccdSEmmanuel Vadot reg |= AW_SPI_GCR_MODE_MASTER; 290ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg); 291ec2b0ccdSEmmanuel Vadot 292ec2b0ccdSEmmanuel Vadot /* Setup the modes */ 293ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_TCR); 294ec2b0ccdSEmmanuel Vadot if (mode & SPIBUS_MODE_CPHA) 295ec2b0ccdSEmmanuel Vadot reg |= AW_SPI_TCR_CPHA; 296ec2b0ccdSEmmanuel Vadot if (mode & SPIBUS_MODE_CPOL) 297ec2b0ccdSEmmanuel Vadot reg |= AW_SPI_TCR_CPOL; 298ec2b0ccdSEmmanuel Vadot 299ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg); 300ec2b0ccdSEmmanuel Vadot } 301ec2b0ccdSEmmanuel Vadot 302ec2b0ccdSEmmanuel Vadot static void 303ec2b0ccdSEmmanuel Vadot aw_spi_setup_cs(struct aw_spi_softc *sc, uint32_t cs, bool low) 304ec2b0ccdSEmmanuel Vadot { 305ec2b0ccdSEmmanuel Vadot uint32_t reg; 306ec2b0ccdSEmmanuel Vadot 307ec2b0ccdSEmmanuel Vadot /* Setup CS */ 308ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_TCR); 309ec2b0ccdSEmmanuel Vadot reg &= ~(AW_SPI_TCR_SSSEL_MASK); 310ec2b0ccdSEmmanuel Vadot reg |= cs << AW_SPI_TCR_SSSEL_SHIFT; 311ec2b0ccdSEmmanuel Vadot reg |= AW_SPI_TCR_SS_OWNER; 312ec2b0ccdSEmmanuel Vadot if (low) 313ec2b0ccdSEmmanuel Vadot reg &= ~(AW_SPI_TCR_SS_LEVEL); 314ec2b0ccdSEmmanuel Vadot else 315ec2b0ccdSEmmanuel Vadot reg |= AW_SPI_TCR_SS_LEVEL; 316ec2b0ccdSEmmanuel Vadot 317ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg); 318ec2b0ccdSEmmanuel Vadot } 319ec2b0ccdSEmmanuel Vadot 320ec2b0ccdSEmmanuel Vadot static uint64_t 321ec2b0ccdSEmmanuel Vadot aw_spi_clock_test_cdr1(struct aw_spi_softc *sc, uint64_t clock, uint32_t *ccr) 322ec2b0ccdSEmmanuel Vadot { 323ec2b0ccdSEmmanuel Vadot uint64_t cur, best = 0; 324ec2b0ccdSEmmanuel Vadot int i, max, best_div; 325ec2b0ccdSEmmanuel Vadot 326ec2b0ccdSEmmanuel Vadot max = AW_SPI_CCR_CDR1_MASK >> AW_SPI_CCR_CDR1_SHIFT; 327ec2b0ccdSEmmanuel Vadot for (i = 0; i < max; i++) { 328ec2b0ccdSEmmanuel Vadot cur = sc->mod_freq / (1 << i); 329ec2b0ccdSEmmanuel Vadot if ((clock - cur) < (clock - best)) { 330ec2b0ccdSEmmanuel Vadot best = cur; 331ec2b0ccdSEmmanuel Vadot best_div = i; 332ec2b0ccdSEmmanuel Vadot } 333ec2b0ccdSEmmanuel Vadot } 334ec2b0ccdSEmmanuel Vadot 335ec2b0ccdSEmmanuel Vadot *ccr = (best_div << AW_SPI_CCR_CDR1_SHIFT); 336ec2b0ccdSEmmanuel Vadot return (best); 337ec2b0ccdSEmmanuel Vadot } 338ec2b0ccdSEmmanuel Vadot 339ec2b0ccdSEmmanuel Vadot static uint64_t 340ec2b0ccdSEmmanuel Vadot aw_spi_clock_test_cdr2(struct aw_spi_softc *sc, uint64_t clock, uint32_t *ccr) 341ec2b0ccdSEmmanuel Vadot { 342ec2b0ccdSEmmanuel Vadot uint64_t cur, best = 0; 343ec2b0ccdSEmmanuel Vadot int i, max, best_div; 344ec2b0ccdSEmmanuel Vadot 345ec2b0ccdSEmmanuel Vadot max = ((AW_SPI_CCR_CDR2_MASK) >> AW_SPI_CCR_CDR2_SHIFT); 346ec2b0ccdSEmmanuel Vadot for (i = 0; i < max; i++) { 347ec2b0ccdSEmmanuel Vadot cur = sc->mod_freq / (2 * i + 1); 348ec2b0ccdSEmmanuel Vadot if ((clock - cur) < (clock - best)) { 349ec2b0ccdSEmmanuel Vadot best = cur; 350ec2b0ccdSEmmanuel Vadot best_div = i; 351ec2b0ccdSEmmanuel Vadot } 352ec2b0ccdSEmmanuel Vadot } 353ec2b0ccdSEmmanuel Vadot 354ec2b0ccdSEmmanuel Vadot *ccr = AW_SPI_CCR_DRS | (best_div << AW_SPI_CCR_CDR2_SHIFT); 355ec2b0ccdSEmmanuel Vadot return (best); 356ec2b0ccdSEmmanuel Vadot } 357ec2b0ccdSEmmanuel Vadot 358ec2b0ccdSEmmanuel Vadot static void 359ec2b0ccdSEmmanuel Vadot aw_spi_setup_clock(struct aw_spi_softc *sc, uint64_t clock) 360ec2b0ccdSEmmanuel Vadot { 361ec2b0ccdSEmmanuel Vadot uint64_t best_ccr1, best_ccr2; 362ec2b0ccdSEmmanuel Vadot uint32_t ccr, ccr1, ccr2; 363ec2b0ccdSEmmanuel Vadot 364ec2b0ccdSEmmanuel Vadot best_ccr1 = aw_spi_clock_test_cdr1(sc, clock, &ccr1); 365ec2b0ccdSEmmanuel Vadot best_ccr2 = aw_spi_clock_test_cdr2(sc, clock, &ccr2); 366ec2b0ccdSEmmanuel Vadot 367ec2b0ccdSEmmanuel Vadot if (best_ccr1 == clock) { 368ec2b0ccdSEmmanuel Vadot ccr = ccr1; 369ec2b0ccdSEmmanuel Vadot } else if (best_ccr2 == clock) { 370ec2b0ccdSEmmanuel Vadot ccr = ccr2; 371ec2b0ccdSEmmanuel Vadot } else { 372ec2b0ccdSEmmanuel Vadot if ((clock - best_ccr1) < (clock - best_ccr2)) 373ec2b0ccdSEmmanuel Vadot ccr = ccr1; 374ec2b0ccdSEmmanuel Vadot else 375ec2b0ccdSEmmanuel Vadot ccr = ccr2; 376ec2b0ccdSEmmanuel Vadot } 377ec2b0ccdSEmmanuel Vadot 378ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_CCR, ccr); 379ec2b0ccdSEmmanuel Vadot } 380ec2b0ccdSEmmanuel Vadot 381ec2b0ccdSEmmanuel Vadot static inline void 382ec2b0ccdSEmmanuel Vadot aw_spi_fill_txfifo(struct aw_spi_softc *sc) 383ec2b0ccdSEmmanuel Vadot { 384ec2b0ccdSEmmanuel Vadot uint32_t reg, txcnt; 385ec2b0ccdSEmmanuel Vadot int i; 386ec2b0ccdSEmmanuel Vadot 387ec2b0ccdSEmmanuel Vadot if (sc->txcnt == sc->txlen) 388ec2b0ccdSEmmanuel Vadot return; 389ec2b0ccdSEmmanuel Vadot 390ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_FSR); 391ec2b0ccdSEmmanuel Vadot reg &= AW_SPI_FSR_TF_CNT_MASK; 392ec2b0ccdSEmmanuel Vadot txcnt = reg >> AW_SPI_FSR_TF_CNT_SHIFT; 393ec2b0ccdSEmmanuel Vadot 394ec2b0ccdSEmmanuel Vadot for (i = 0; i < (AW_SPI_FIFO_SIZE - txcnt); i++) { 395ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_1(sc, AW_SPI_TXD, sc->txbuf[sc->txcnt++]); 396ec2b0ccdSEmmanuel Vadot if (sc->txcnt == sc->txlen) 397ec2b0ccdSEmmanuel Vadot break; 398ec2b0ccdSEmmanuel Vadot } 399ec2b0ccdSEmmanuel Vadot 400ec2b0ccdSEmmanuel Vadot return; 401ec2b0ccdSEmmanuel Vadot } 402ec2b0ccdSEmmanuel Vadot 403ec2b0ccdSEmmanuel Vadot static inline void 404ec2b0ccdSEmmanuel Vadot aw_spi_read_rxfifo(struct aw_spi_softc *sc) 405ec2b0ccdSEmmanuel Vadot { 406ec2b0ccdSEmmanuel Vadot uint32_t reg; 407ec2b0ccdSEmmanuel Vadot uint8_t val; 408ec2b0ccdSEmmanuel Vadot int i; 409ec2b0ccdSEmmanuel Vadot 410ec2b0ccdSEmmanuel Vadot if (sc->rxcnt == sc->rxlen) 411ec2b0ccdSEmmanuel Vadot return; 412ec2b0ccdSEmmanuel Vadot 413ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_FSR); 414ec2b0ccdSEmmanuel Vadot reg = (reg & AW_SPI_FSR_RF_CNT_MASK) >> AW_SPI_FSR_RF_CNT_SHIFT; 415ec2b0ccdSEmmanuel Vadot 416ec2b0ccdSEmmanuel Vadot for (i = 0; i < reg; i++) { 417ec2b0ccdSEmmanuel Vadot val = AW_SPI_READ_1(sc, AW_SPI_RDX); 418ec2b0ccdSEmmanuel Vadot if (sc->rxcnt < sc->rxlen) 419ec2b0ccdSEmmanuel Vadot sc->rxbuf[sc->rxcnt++] = val; 420ec2b0ccdSEmmanuel Vadot } 421ec2b0ccdSEmmanuel Vadot } 422ec2b0ccdSEmmanuel Vadot 423ec2b0ccdSEmmanuel Vadot static int 424ec2b0ccdSEmmanuel Vadot aw_spi_intr(void *arg) 425ec2b0ccdSEmmanuel Vadot { 426ec2b0ccdSEmmanuel Vadot struct aw_spi_softc *sc; 427ec2b0ccdSEmmanuel Vadot uint32_t intr; 428ec2b0ccdSEmmanuel Vadot 429ec2b0ccdSEmmanuel Vadot sc = (struct aw_spi_softc *)arg; 430ec2b0ccdSEmmanuel Vadot 431ec2b0ccdSEmmanuel Vadot intr = AW_SPI_READ_4(sc, AW_SPI_ISR); 432ec2b0ccdSEmmanuel Vadot 433ec2b0ccdSEmmanuel Vadot if (intr & AW_SPI_IER_RF_RDY) 434ec2b0ccdSEmmanuel Vadot aw_spi_read_rxfifo(sc); 435ec2b0ccdSEmmanuel Vadot 436ec2b0ccdSEmmanuel Vadot if (intr & AW_SPI_IER_TF_ERQ) { 437ec2b0ccdSEmmanuel Vadot aw_spi_fill_txfifo(sc); 438ec2b0ccdSEmmanuel Vadot 439ec2b0ccdSEmmanuel Vadot /* 440ec2b0ccdSEmmanuel Vadot * If we don't have anything else to write 441ec2b0ccdSEmmanuel Vadot * disable TXFifo interrupts 442ec2b0ccdSEmmanuel Vadot */ 443ec2b0ccdSEmmanuel Vadot if (sc->txcnt == sc->txlen) 444ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_IER, AW_SPI_IER_TC | 445ec2b0ccdSEmmanuel Vadot AW_SPI_IER_RF_RDY); 446ec2b0ccdSEmmanuel Vadot } 447ec2b0ccdSEmmanuel Vadot 448ec2b0ccdSEmmanuel Vadot if (intr & AW_SPI_IER_TC) { 449ec2b0ccdSEmmanuel Vadot /* read the rest of the data from the fifo */ 450ec2b0ccdSEmmanuel Vadot aw_spi_read_rxfifo(sc); 451ec2b0ccdSEmmanuel Vadot 452ec2b0ccdSEmmanuel Vadot /* Disable the interrupts */ 453ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_IER, 0); 454ec2b0ccdSEmmanuel Vadot sc->transfer = 0; 455ec2b0ccdSEmmanuel Vadot wakeup(sc); 456ec2b0ccdSEmmanuel Vadot } 457ec2b0ccdSEmmanuel Vadot 458ec2b0ccdSEmmanuel Vadot /* Clear Interrupts */ 459ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_ISR, intr); 460ec2b0ccdSEmmanuel Vadot return (intr != 0 ? FILTER_HANDLED : FILTER_STRAY); 461ec2b0ccdSEmmanuel Vadot } 462ec2b0ccdSEmmanuel Vadot 463ec2b0ccdSEmmanuel Vadot static int 464ec2b0ccdSEmmanuel Vadot aw_spi_xfer(struct aw_spi_softc *sc, void *rxbuf, void *txbuf, uint32_t txlen, uint32_t rxlen) 465ec2b0ccdSEmmanuel Vadot { 466ec2b0ccdSEmmanuel Vadot uint32_t reg; 467ec2b0ccdSEmmanuel Vadot int error = 0, timeout; 468ec2b0ccdSEmmanuel Vadot 469ec2b0ccdSEmmanuel Vadot sc->rxbuf = rxbuf; 470ec2b0ccdSEmmanuel Vadot sc->rxcnt = 0; 471ec2b0ccdSEmmanuel Vadot sc->txbuf = txbuf; 472ec2b0ccdSEmmanuel Vadot sc->txcnt = 0; 473ec2b0ccdSEmmanuel Vadot sc->txlen = txlen; 474ec2b0ccdSEmmanuel Vadot sc->rxlen = rxlen; 475ec2b0ccdSEmmanuel Vadot 476ec2b0ccdSEmmanuel Vadot /* Reset the FIFOs */ 477ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_FCR, AW_SPI_FCR_TX_RST | AW_SPI_FCR_RX_RST); 478ec2b0ccdSEmmanuel Vadot 479ec2b0ccdSEmmanuel Vadot for (timeout = 1000; timeout > 0; timeout--) { 480ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_FCR); 481ec2b0ccdSEmmanuel Vadot if (reg == 0) 482ec2b0ccdSEmmanuel Vadot break; 483ec2b0ccdSEmmanuel Vadot } 484ec2b0ccdSEmmanuel Vadot if (timeout == 0) { 485ec2b0ccdSEmmanuel Vadot device_printf(sc->dev, "Cannot reset the FIFOs\n"); 486ec2b0ccdSEmmanuel Vadot return (EIO); 487ec2b0ccdSEmmanuel Vadot } 488ec2b0ccdSEmmanuel Vadot 489ec2b0ccdSEmmanuel Vadot /* 490ec2b0ccdSEmmanuel Vadot * Set the TX FIFO threshold to 3/4-th the size and 491ec2b0ccdSEmmanuel Vadot * the RX FIFO one to 1/4-th. 492ec2b0ccdSEmmanuel Vadot */ 493ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_FCR, 494ec2b0ccdSEmmanuel Vadot ((3 * AW_SPI_FIFO_SIZE / 4) << AW_SPI_FCR_TX_TRIG_SHIFT) | 495ec2b0ccdSEmmanuel Vadot ((AW_SPI_FIFO_SIZE / 4) << AW_SPI_FCR_RX_TRIG_SHIFT)); 496ec2b0ccdSEmmanuel Vadot 497ec2b0ccdSEmmanuel Vadot /* Write the counters */ 498ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_MBC, txlen); 499ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_MTC, txlen); 500ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_BCC, txlen); 501ec2b0ccdSEmmanuel Vadot 502ec2b0ccdSEmmanuel Vadot /* First fill */ 503ec2b0ccdSEmmanuel Vadot aw_spi_fill_txfifo(sc); 504ec2b0ccdSEmmanuel Vadot 505ec2b0ccdSEmmanuel Vadot /* Start transmit */ 506ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_TCR); 507ec2b0ccdSEmmanuel Vadot reg |= AW_SPI_TCR_XCH; 508ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_TCR, reg); 509ec2b0ccdSEmmanuel Vadot 510ec2b0ccdSEmmanuel Vadot /* 511ec2b0ccdSEmmanuel Vadot * Enable interrupts for : 512ec2b0ccdSEmmanuel Vadot * Transmit complete 513ec2b0ccdSEmmanuel Vadot * TX Fifo is below its trigger threshold 514ec2b0ccdSEmmanuel Vadot * RX Fifo is above its trigger threshold 515ec2b0ccdSEmmanuel Vadot */ 516ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_IER, AW_SPI_IER_TC | 517ec2b0ccdSEmmanuel Vadot AW_SPI_IER_TF_ERQ | AW_SPI_IER_RF_RDY); 518ec2b0ccdSEmmanuel Vadot 519ec2b0ccdSEmmanuel Vadot sc->transfer = 1; 520ec2b0ccdSEmmanuel Vadot 521ec2b0ccdSEmmanuel Vadot while (error == 0 && sc->transfer != 0) 522ec2b0ccdSEmmanuel Vadot error = msleep(sc, &sc->mtx, 0, "aw_spi", 10 * hz); 523ec2b0ccdSEmmanuel Vadot 524ec2b0ccdSEmmanuel Vadot return (0); 525ec2b0ccdSEmmanuel Vadot } 526ec2b0ccdSEmmanuel Vadot 527ec2b0ccdSEmmanuel Vadot static int 528ec2b0ccdSEmmanuel Vadot aw_spi_transfer(device_t dev, device_t child, struct spi_command *cmd) 529ec2b0ccdSEmmanuel Vadot { 530ec2b0ccdSEmmanuel Vadot struct aw_spi_softc *sc; 531ec2b0ccdSEmmanuel Vadot uint32_t cs, mode, clock, reg; 532ec2b0ccdSEmmanuel Vadot int err = 0; 533ec2b0ccdSEmmanuel Vadot 534ec2b0ccdSEmmanuel Vadot sc = device_get_softc(dev); 535ec2b0ccdSEmmanuel Vadot 536ec2b0ccdSEmmanuel Vadot spibus_get_cs(child, &cs); 537ec2b0ccdSEmmanuel Vadot spibus_get_clock(child, &clock); 538ec2b0ccdSEmmanuel Vadot spibus_get_mode(child, &mode); 539ec2b0ccdSEmmanuel Vadot 540ec2b0ccdSEmmanuel Vadot /* The minimum divider is 2 so set the clock at twice the needed speed */ 541ec2b0ccdSEmmanuel Vadot clk_set_freq(sc->clk_mod, 2 * clock, CLK_SET_ROUND_DOWN); 542ec2b0ccdSEmmanuel Vadot clk_get_freq(sc->clk_mod, &sc->mod_freq); 543ec2b0ccdSEmmanuel Vadot if (cs >= AW_SPI_MAX_CS) { 544ec2b0ccdSEmmanuel Vadot device_printf(dev, "Invalid cs %d\n", cs); 545ec2b0ccdSEmmanuel Vadot return (EINVAL); 546ec2b0ccdSEmmanuel Vadot } 547ec2b0ccdSEmmanuel Vadot 548ec2b0ccdSEmmanuel Vadot mtx_lock(&sc->mtx); 549ec2b0ccdSEmmanuel Vadot 550ec2b0ccdSEmmanuel Vadot /* Enable and reset the module */ 551ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_GCR); 552ec2b0ccdSEmmanuel Vadot reg |= AW_SPI_GCR_EN | AW_SPI_GCR_SRST; 553ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg); 554ec2b0ccdSEmmanuel Vadot 555ec2b0ccdSEmmanuel Vadot /* Setup clock, CS and mode */ 556ec2b0ccdSEmmanuel Vadot aw_spi_setup_clock(sc, clock); 557ec2b0ccdSEmmanuel Vadot aw_spi_setup_mode(sc, mode); 558ec2b0ccdSEmmanuel Vadot if (cs & SPIBUS_CS_HIGH) 559ec2b0ccdSEmmanuel Vadot aw_spi_setup_cs(sc, cs, false); 560ec2b0ccdSEmmanuel Vadot else 561ec2b0ccdSEmmanuel Vadot aw_spi_setup_cs(sc, cs, true); 562ec2b0ccdSEmmanuel Vadot 563ec2b0ccdSEmmanuel Vadot /* xfer */ 564ec2b0ccdSEmmanuel Vadot err = 0; 565ec2b0ccdSEmmanuel Vadot if (cmd->tx_cmd_sz > 0) 566ec2b0ccdSEmmanuel Vadot err = aw_spi_xfer(sc, cmd->rx_cmd, cmd->tx_cmd, 567ec2b0ccdSEmmanuel Vadot cmd->tx_cmd_sz, cmd->rx_cmd_sz); 568ec2b0ccdSEmmanuel Vadot if (cmd->tx_data_sz > 0 && err == 0) 569ec2b0ccdSEmmanuel Vadot err = aw_spi_xfer(sc, cmd->rx_data, cmd->tx_data, 570ec2b0ccdSEmmanuel Vadot cmd->tx_data_sz, cmd->rx_data_sz); 571ec2b0ccdSEmmanuel Vadot 572ec2b0ccdSEmmanuel Vadot if (cs & SPIBUS_CS_HIGH) 573ec2b0ccdSEmmanuel Vadot aw_spi_setup_cs(sc, cs, true); 574ec2b0ccdSEmmanuel Vadot else 575ec2b0ccdSEmmanuel Vadot aw_spi_setup_cs(sc, cs, false); 576ec2b0ccdSEmmanuel Vadot 577ec2b0ccdSEmmanuel Vadot /* Disable the module */ 578ec2b0ccdSEmmanuel Vadot reg = AW_SPI_READ_4(sc, AW_SPI_GCR); 579ec2b0ccdSEmmanuel Vadot reg &= ~AW_SPI_GCR_EN; 580ec2b0ccdSEmmanuel Vadot AW_SPI_WRITE_4(sc, AW_SPI_GCR, reg); 581ec2b0ccdSEmmanuel Vadot 582ec2b0ccdSEmmanuel Vadot mtx_unlock(&sc->mtx); 583ec2b0ccdSEmmanuel Vadot 584ec2b0ccdSEmmanuel Vadot return (err); 585ec2b0ccdSEmmanuel Vadot } 586ec2b0ccdSEmmanuel Vadot 587ec2b0ccdSEmmanuel Vadot static device_method_t aw_spi_methods[] = { 588ec2b0ccdSEmmanuel Vadot /* Device interface */ 589ec2b0ccdSEmmanuel Vadot DEVMETHOD(device_probe, aw_spi_probe), 590ec2b0ccdSEmmanuel Vadot DEVMETHOD(device_attach, aw_spi_attach), 591ec2b0ccdSEmmanuel Vadot DEVMETHOD(device_detach, aw_spi_detach), 592ec2b0ccdSEmmanuel Vadot 593ec2b0ccdSEmmanuel Vadot /* spibus_if */ 594ec2b0ccdSEmmanuel Vadot DEVMETHOD(spibus_transfer, aw_spi_transfer), 595ec2b0ccdSEmmanuel Vadot 596ec2b0ccdSEmmanuel Vadot /* ofw_bus_if */ 597ec2b0ccdSEmmanuel Vadot DEVMETHOD(ofw_bus_get_node, aw_spi_get_node), 598ec2b0ccdSEmmanuel Vadot 599ec2b0ccdSEmmanuel Vadot DEVMETHOD_END 600ec2b0ccdSEmmanuel Vadot }; 601ec2b0ccdSEmmanuel Vadot 602ec2b0ccdSEmmanuel Vadot static driver_t aw_spi_driver = { 603ec2b0ccdSEmmanuel Vadot "aw_spi", 604ec2b0ccdSEmmanuel Vadot aw_spi_methods, 605ec2b0ccdSEmmanuel Vadot sizeof(struct aw_spi_softc), 606ec2b0ccdSEmmanuel Vadot }; 607ec2b0ccdSEmmanuel Vadot 608ec2b0ccdSEmmanuel Vadot DRIVER_MODULE(aw_spi, simplebus, aw_spi_driver, 0, 0); 609ec2b0ccdSEmmanuel Vadot DRIVER_MODULE(ofw_spibus, aw_spi, ofw_spibus_driver, 0, 0); 610ec2b0ccdSEmmanuel Vadot MODULE_DEPEND(aw_spi, ofw_spibus, 1, 1, 1); 611ec2b0ccdSEmmanuel Vadot SIMPLEBUS_PNP_INFO(compat_data); 612