Path: blob/main/sys/arm/freescale/vybrid/vf_spi.c
109304 views
/*-1* Copyright (c) 2014 Ruslan Bukin <[email protected]>2* All rights reserved.3*4* Redistribution and use in source and binary forms, with or without5* modification, are permitted provided that the following conditions6* are met:7* 1. Redistributions of source code must retain the above copyright8* notice, this list of conditions and the following disclaimer.9* 2. Redistributions in binary form must reproduce the above copyright10* notice, this list of conditions and the following disclaimer in the11* documentation and/or other materials provided with the distribution.12*13* THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND14* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE15* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE16* ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE17* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL18* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS19* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)20* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT21* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY22* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF23* SUCH DAMAGE.24*/2526/*27* Vybrid Family Serial Peripheral Interface (SPI)28* Chapter 47, Vybrid Reference Manual, Rev. 5, 07/201329*/3031#include <sys/param.h>32#include <sys/systm.h>33#include <sys/bus.h>34#include <sys/kernel.h>35#include <sys/module.h>36#include <sys/malloc.h>37#include <sys/rman.h>38#include <sys/timeet.h>39#include <sys/timetc.h>40#include <sys/watchdog.h>4142#include <dev/spibus/spi.h>43#include <dev/spibus/spibusvar.h>4445#include "spibus_if.h"4647#include <dev/ofw/openfirm.h>48#include <dev/ofw/ofw_bus.h>49#include <dev/ofw/ofw_bus_subr.h>5051#include <machine/bus.h>52#include <machine/cpu.h>53#include <machine/intr.h>5455#include <arm/freescale/vybrid/vf_common.h>5657#define SPI_FIFO_SIZE 45859#define SPI_MCR 0x00 /* Module Configuration */60#define MCR_MSTR (1 << 31) /* Master/Slave Mode Select */61#define MCR_CONT_SCKE (1 << 30) /* Continuous SCK Enable */62#define MCR_FRZ (1 << 27) /* Freeze */63#define MCR_PCSIS_S 16 /* Peripheral Chip Select */64#define MCR_PCSIS_M 0x3f65#define MCR_MDIS (1 << 14) /* Module Disable */66#define MCR_CLR_TXF (1 << 11) /* Clear TX FIFO */67#define MCR_CLR_RXF (1 << 10) /* Clear RX FIFO */68#define MCR_HALT (1 << 0) /* Starts and stops SPI transfers */69#define SPI_TCR 0x08 /* Transfer Count */70#define SPI_CTAR0 0x0C /* Clock and Transfer Attributes */71#define SPI_CTAR0_SLAVE 0x0C /* Clock and Transfer Attributes */72#define SPI_CTAR1 0x10 /* Clock and Transfer Attributes */73#define SPI_CTAR2 0x14 /* Clock and Transfer Attributes */74#define SPI_CTAR3 0x18 /* Clock and Transfer Attributes */75#define CTAR_FMSZ_M 0xf76#define CTAR_FMSZ_S 27 /* Frame Size */77#define CTAR_FMSZ_8 0x7 /* 8 bits */78#define CTAR_CPOL (1 << 26) /* Clock Polarity */79#define CTAR_CPHA (1 << 25) /* Clock Phase */80#define CTAR_LSBFE (1 << 24) /* Less significant bit first */81#define CTAR_PCSSCK_M 0x382#define CTAR_PCSSCK_S 22 /* PCS to SCK Delay Prescaler */83#define CTAR_PBR_M 0x384#define CTAR_PBR_S 16 /* Baud Rate Prescaler */85#define CTAR_PBR_7 0x3 /* Divide by 7 */86#define CTAR_CSSCK_M 0xf87#define CTAR_CSSCK_S 12 /* PCS to SCK Delay Scaler */88#define CTAR_BR_M 0xf89#define CTAR_BR_S 0 /* Baud Rate Scaler */90#define SPI_SR 0x2C /* Status Register */91#define SR_TCF (1 << 31) /* Transfer Complete Flag */92#define SR_EOQF (1 << 28) /* End of Queue Flag */93#define SR_TFFF (1 << 25) /* Transmit FIFO Fill Flag */94#define SR_RFDF (1 << 17) /* Receive FIFO Drain Flag */95#define SPI_RSER 0x30 /* DMA/Interrupt Select */96#define RSER_EOQF_RE (1 << 28) /* Finished Request Enable */97#define SPI_PUSHR 0x34 /* PUSH TX FIFO In Master Mode */98#define PUSHR_CONT (1 << 31) /* Continuous Peripheral CS */99#define PUSHR_EOQ (1 << 27) /* End Of Queue */100#define PUSHR_CTCNT (1 << 26) /* Clear Transfer Counter */101#define PUSHR_PCS_M 0x3f102#define PUSHR_PCS_S 16 /* Select PCS signals */103104#define SPI_PUSHR_SLAVE 0x34 /* PUSH TX FIFO Register In Slave Mode */105#define SPI_POPR 0x38 /* POP RX FIFO Register */106#define SPI_TXFR0 0x3C /* Transmit FIFO Registers */107#define SPI_TXFR1 0x40108#define SPI_TXFR2 0x44109#define SPI_TXFR3 0x48110#define SPI_RXFR0 0x7C /* Receive FIFO Registers */111#define SPI_RXFR1 0x80112#define SPI_RXFR2 0x84113#define SPI_RXFR3 0x88114115struct spi_softc {116struct resource *res[2];117bus_space_tag_t bst;118bus_space_handle_t bsh;119void *ih;120};121122static struct resource_spec spi_spec[] = {123{ SYS_RES_MEMORY, 0, RF_ACTIVE },124{ SYS_RES_IRQ, 0, RF_ACTIVE },125{ -1, 0 }126};127128static int129spi_probe(device_t dev)130{131132if (!ofw_bus_status_okay(dev))133return (ENXIO);134135if (!ofw_bus_is_compatible(dev, "fsl,mvf600-spi"))136return (ENXIO);137138device_set_desc(dev, "Vybrid Family Serial Peripheral Interface");139return (BUS_PROBE_DEFAULT);140}141142static int143spi_attach(device_t dev)144{145struct spi_softc *sc;146uint32_t reg;147148sc = device_get_softc(dev);149150if (bus_alloc_resources(dev, spi_spec, sc->res)) {151device_printf(dev, "could not allocate resources\n");152return (ENXIO);153}154155/* Memory interface */156sc->bst = rman_get_bustag(sc->res[0]);157sc->bsh = rman_get_bushandle(sc->res[0]);158159reg = READ4(sc, SPI_MCR);160reg |= MCR_MSTR;161reg &= ~(MCR_CONT_SCKE | MCR_MDIS | MCR_FRZ);162reg &= ~(MCR_PCSIS_M << MCR_PCSIS_S);163reg |= (MCR_PCSIS_M << MCR_PCSIS_S); /* PCS Active low */164reg |= (MCR_CLR_TXF | MCR_CLR_RXF);165WRITE4(sc, SPI_MCR, reg);166167reg = READ4(sc, SPI_RSER);168reg |= RSER_EOQF_RE;169WRITE4(sc, SPI_RSER, reg);170171reg = READ4(sc, SPI_MCR);172reg &= ~MCR_HALT;173WRITE4(sc, SPI_MCR, reg);174175reg = READ4(sc, SPI_CTAR0);176reg &= ~(CTAR_FMSZ_M << CTAR_FMSZ_S);177reg |= (CTAR_FMSZ_8 << CTAR_FMSZ_S);178/*179* TODO: calculate BR180* SCK baud rate = ( fsys / PBR ) * (1 + DBR) / BR181*182* reg &= ~(CTAR_BR_M << CTAR_BR_S);183*/184reg &= ~CTAR_CPOL; /* Polarity */185reg |= CTAR_CPHA;186/*187* Set LSB (Less significant bit first)188* must be used for some applications, e.g. some LCDs189*/190reg |= CTAR_LSBFE;191WRITE4(sc, SPI_CTAR0, reg);192193reg = READ4(sc, SPI_CTAR0);194reg &= ~(CTAR_PBR_M << CTAR_PBR_S);195reg |= (CTAR_PBR_7 << CTAR_PBR_S);196WRITE4(sc, SPI_CTAR0, reg);197198device_add_child(dev, "spibus", 0);199bus_attach_children(dev);200return (0);201}202203static int204spi_txrx(struct spi_softc *sc, uint8_t *out_buf,205uint8_t *in_buf, int bufsz, int cs)206{207uint32_t reg, wreg;208uint32_t txcnt;209uint32_t i;210211txcnt = 0;212213for (i = 0; i < bufsz; i++) {214txcnt++;215wreg = out_buf[i];216wreg |= PUSHR_CONT;217wreg |= (cs << PUSHR_PCS_S);218if (i == 0)219wreg |= PUSHR_CTCNT;220if (i == (bufsz - 1) || txcnt == SPI_FIFO_SIZE)221wreg |= PUSHR_EOQ;222WRITE4(sc, SPI_PUSHR, wreg);223224if (i == (bufsz - 1) || txcnt == SPI_FIFO_SIZE) {225txcnt = 0;226227/* Wait last entry in a queue to be transmitted */228while((READ4(sc, SPI_SR) & SR_EOQF) == 0)229continue;230231reg = READ4(sc, SPI_SR);232reg |= (SR_TCF | SR_EOQF);233WRITE4(sc, SPI_SR, reg);234}235236/* Wait until RX FIFO is empty */237while((READ4(sc, SPI_SR) & SR_RFDF) == 0)238continue;239240in_buf[i] = READ1(sc, SPI_POPR);241}242243return (0);244}245246static int247spi_transfer(device_t dev, device_t child, struct spi_command *cmd)248{249struct spi_softc *sc;250uint32_t cs;251252sc = device_get_softc(dev);253254KASSERT(cmd->tx_cmd_sz == cmd->rx_cmd_sz,255("%s: TX/RX command sizes should be equal", __func__));256KASSERT(cmd->tx_data_sz == cmd->rx_data_sz,257("%s: TX/RX data sizes should be equal", __func__));258259/* get the proper chip select */260spibus_get_cs(child, &cs);261262cs &= ~SPIBUS_CS_HIGH;263264/* Command */265spi_txrx(sc, cmd->tx_cmd, cmd->rx_cmd, cmd->tx_cmd_sz, cs);266267/* Data */268spi_txrx(sc, cmd->tx_data, cmd->rx_data, cmd->tx_data_sz, cs);269270return (0);271}272273static device_method_t spi_methods[] = {274/* Device interface */275DEVMETHOD(device_probe, spi_probe),276DEVMETHOD(device_attach, spi_attach),277/* SPI interface */278DEVMETHOD(spibus_transfer, spi_transfer),279{ 0, 0 }280};281282static driver_t spi_driver = {283"spi",284spi_methods,285sizeof(struct spi_softc),286};287288DRIVER_MODULE(spi, simplebus, spi_driver, 0, 0);289290291