Home | History | Annotate | Line # | Download | only in rockchip
rk_spi.c revision 1.3
      1  1.3  tnn /*	$NetBSD: rk_spi.c,v 1.3 2019/08/13 17:15:55 tnn Exp $	*/
      2  1.1  tnn 
      3  1.1  tnn /*
      4  1.1  tnn  * Copyright (c) 2019 The NetBSD Foundation, Inc.
      5  1.1  tnn  * All rights reserved.
      6  1.1  tnn  *
      7  1.1  tnn  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  tnn  * by Tobias Nygren.
      9  1.1  tnn  *
     10  1.1  tnn  * Redistribution and use in source and binary forms, with or without
     11  1.1  tnn  * modification, are permitted provided that the following conditions
     12  1.1  tnn  * are met:
     13  1.1  tnn  * 1. Redistributions of source code must retain the above copyright
     14  1.1  tnn  *    notice, this list of conditions and the following disclaimer.
     15  1.1  tnn  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1  tnn  *    notice, this list of conditions and the following disclaimer in the
     17  1.1  tnn  *    documentation and/or other materials provided with the distribution.
     18  1.1  tnn  *
     19  1.1  tnn  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  1.1  tnn  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  1.1  tnn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  1.1  tnn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  1.1  tnn  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  1.1  tnn  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  1.1  tnn  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  1.1  tnn  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  1.1  tnn  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  1.1  tnn  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  1.1  tnn  * POSSIBILITY OF SUCH DAMAGE.
     30  1.1  tnn  */
     31  1.1  tnn 
     32  1.1  tnn #include <sys/cdefs.h>
     33  1.3  tnn __KERNEL_RCSID(0, "$NetBSD: rk_spi.c,v 1.3 2019/08/13 17:15:55 tnn Exp $");
     34  1.1  tnn 
     35  1.1  tnn #include <sys/param.h>
     36  1.1  tnn #include <sys/device.h>
     37  1.1  tnn #include <sys/systm.h>
     38  1.1  tnn #include <sys/bus.h>
     39  1.1  tnn #include <sys/intr.h>
     40  1.1  tnn #include <sys/kernel.h>
     41  1.1  tnn #include <sys/bitops.h>
     42  1.1  tnn #include <dev/spi/spivar.h>
     43  1.1  tnn #include <dev/fdt/fdtvar.h>
     44  1.1  tnn #include <arm/fdt/arm_fdtvar.h>
     45  1.1  tnn 
     46  1.1  tnn #define SPI_CTRLR0		0x00
     47  1.1  tnn #define SPI_CTRLR0_MTM		__BIT(21)
     48  1.1  tnn #define SPI_CTRLR0_OPM		__BIT(20)
     49  1.1  tnn #define SPI_CTRLR0_XFM		__BITS(19, 18)
     50  1.1  tnn #define SPI_CTRLR0_FRF		__BITS(17, 16)
     51  1.1  tnn #define SPI_CTRLR0_RSD		__BITS(15, 14)
     52  1.1  tnn #define SPI_CTRLR0_BHT		__BIT(13)
     53  1.1  tnn #define SPI_CTRLR0_FBM		__BIT(12)
     54  1.1  tnn #define SPI_CTRLR0_EM		__BIT(11)
     55  1.1  tnn #define SPI_CTRLR0_RW		__BIT(10)
     56  1.1  tnn #define SPI_CTRLR0_CSM		__BITS(9, 8)
     57  1.1  tnn #define SPI_CTRLR0_SCPOL	__BIT(7)
     58  1.1  tnn #define SPI_CTRLR0_SCPH		__BIT(6)
     59  1.1  tnn #define SPI_CTRLR0_CFS		__BITS(5, 2)
     60  1.1  tnn #define SPI_CTRLR0_DFS		__BITS(1, 0)
     61  1.1  tnn #define SPI_CTRLR0_DFS_4BIT	0x0
     62  1.1  tnn #define SPI_CTRLR0_DFS_8BIT	0x1
     63  1.1  tnn #define SPI_CTRLR0_DFS_16BIT	0x2
     64  1.1  tnn 
     65  1.1  tnn #define SPI_CTRLR1		0x04
     66  1.1  tnn #define SPI_CTRLR1_NDM		__BITS(15, 0)
     67  1.1  tnn 
     68  1.1  tnn #define SPI_ENR			0x08
     69  1.1  tnn #define SPI_ENR_ENR		__BIT(0)
     70  1.1  tnn 
     71  1.1  tnn #define SPI_SER			0x0c
     72  1.1  tnn #define SPI_SER_SER1		__BIT(1)
     73  1.1  tnn #define SPI_SER_SER0		__BIT(0)
     74  1.1  tnn 
     75  1.1  tnn #define SPI_BAUDR		0x10
     76  1.1  tnn #define SPI_BAUDR_BAUDR		__BITS(15, 0)
     77  1.1  tnn 
     78  1.1  tnn #define SPI_TXFTLR		0x14
     79  1.1  tnn #define SPI_TXFTLR_TXFLTR	__BITS(4, 0)
     80  1.1  tnn 
     81  1.1  tnn #define SPI_RXFTLR		0x18
     82  1.1  tnn #define SPI_RXFLTR_RXFLTR	__BITS(4, 0)
     83  1.1  tnn 
     84  1.1  tnn #define SPI_TXFLR		0x1c
     85  1.1  tnn #define SPI_TXFLR_TXFLR		__BITS(5, 0)
     86  1.1  tnn 
     87  1.1  tnn #define SPI_RXFLR		0x20
     88  1.1  tnn #define SPI_RXFLR_RXFLR		__BITS(5, 0)
     89  1.1  tnn 
     90  1.1  tnn #define SPI_SR			0x24
     91  1.1  tnn #define SPI_SR_RFF		__BIT(4)
     92  1.1  tnn #define SPI_SR_RFE		__BIT(3)
     93  1.1  tnn #define SPI_SR_TFE		__BIT(2)
     94  1.1  tnn #define SPI_SR_TFF		__BIT(1)
     95  1.1  tnn #define SPI_SR_BSF		__BIT(0)
     96  1.1  tnn 
     97  1.1  tnn #define SPI_IPR			0x28
     98  1.1  tnn #define SPI_IPR_IPR		__BIT(0)
     99  1.1  tnn 
    100  1.1  tnn #define SPI_IMR			0x2c
    101  1.1  tnn #define SPI_IMR_RFFIM		__BIT(4)
    102  1.1  tnn #define SPI_IMR_RFOIM		__BIT(3)
    103  1.1  tnn #define SPI_IMR_RFUIM		__BIT(2)
    104  1.1  tnn #define SPI_IMR_TFOIM		__BIT(1)
    105  1.1  tnn #define SPI_IMR_TFEIM		__BIT(0)
    106  1.1  tnn 
    107  1.1  tnn #define SPI_ISR			0x30
    108  1.1  tnn #define SPI_ISR_RFFIS		__BIT(4)
    109  1.1  tnn #define SPI_ISR_RFOIS		__BIT(3)
    110  1.1  tnn #define SPI_ISR_RFUIS		__BIT(2)
    111  1.1  tnn #define SPI_ISR_TFOIS		__BIT(1)
    112  1.1  tnn #define SPI_ISR_TFEIS		__BIT(0)
    113  1.1  tnn 
    114  1.1  tnn #define SPI_RISR		0x34
    115  1.1  tnn #define SPI_RISR_RFFRIS		__BIT(4)
    116  1.1  tnn #define SPI_RISR_RFORIS		__BIT(3)
    117  1.1  tnn #define SPI_RISR_RFURIS		__BIT(2)
    118  1.1  tnn #define SPI_RISR_TFORIS		__BIT(1)
    119  1.1  tnn #define SPI_RISR_TFERIS		__BIT(0)
    120  1.1  tnn 
    121  1.1  tnn #define SPI_ICR			0x38
    122  1.1  tnn #define SPI_ICR_CTFOI		__BIT(3)
    123  1.1  tnn #define SPI_ICR_CRFOI		__BIT(2)
    124  1.1  tnn #define SPI_ICR_CRFUI		__BIT(1)
    125  1.1  tnn #define SPI_ICR_CCI		__BIT(0)
    126  1.1  tnn #define SPI_ICR_ALL		__BITS(3, 0)
    127  1.1  tnn 
    128  1.1  tnn #define SPI_DMACR		0x3c
    129  1.1  tnn #define SPI_DMACR_TDE		__BIT(1)
    130  1.1  tnn #define SPI_DMACR_RDE		__BIT(0)
    131  1.1  tnn 
    132  1.1  tnn #define SPI_DMATDLR		0x40
    133  1.1  tnn #define SPI_DMATDLR_TDL		__BITS(4, 0)
    134  1.1  tnn 
    135  1.1  tnn #define SPI_DMARDLR		0x44
    136  1.1  tnn #define SPI_DMARDLR_RDL		__BITS(4, 0)
    137  1.1  tnn 
    138  1.1  tnn #define SPI_TXDR		0x400
    139  1.1  tnn #define SPI_TXDR_TXDR		__BITS(15, 0)
    140  1.1  tnn 
    141  1.1  tnn #define SPI_RXDR		0x800
    142  1.1  tnn #define SPI_RXDR_RXDR		__BITS(15, 0)
    143  1.1  tnn 
    144  1.1  tnn #define SPI_FIFOLEN		32
    145  1.1  tnn 
    146  1.1  tnn static const char * const compatible[] = {
    147  1.1  tnn #if 0 /* should work on RK3328 but untested */
    148  1.1  tnn 	"rockchip,rk3066-spi",
    149  1.1  tnn 	"rockchip,rk3328-spi",
    150  1.1  tnn #endif
    151  1.1  tnn 	"rockchip,rk3399-spi",
    152  1.1  tnn 	NULL
    153  1.1  tnn };
    154  1.1  tnn 
    155  1.1  tnn struct rk_spi_softc {
    156  1.1  tnn 	device_t		sc_dev;
    157  1.1  tnn 	bus_space_tag_t		sc_bst;
    158  1.1  tnn 	bus_space_handle_t	sc_bsh;
    159  1.1  tnn 	void			*sc_ih;
    160  1.1  tnn 	u_int			sc_spi_freq;
    161  1.1  tnn 	struct spi_controller	sc_spi;
    162  1.1  tnn 	SIMPLEQ_HEAD(,spi_transfer) sc_q;
    163  1.1  tnn 	struct spi_transfer	*sc_transfer;
    164  1.1  tnn 	struct spi_chunk	*sc_rchunk, *sc_wchunk;
    165  1.1  tnn 	volatile bool		sc_running;
    166  1.1  tnn };
    167  1.1  tnn 
    168  1.1  tnn #define SPIREG_READ(sc, reg) \
    169  1.1  tnn     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    170  1.1  tnn #define SPIREG_WRITE(sc, reg, val) \
    171  1.1  tnn     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    172  1.1  tnn 
    173  1.3  tnn static struct spi_controller *rk_spi_get_controller(device_t);
    174  1.1  tnn static int rk_spi_match(device_t, cfdata_t, void *);
    175  1.1  tnn static void rk_spi_attach(device_t, device_t, void *);
    176  1.1  tnn 
    177  1.1  tnn static int rk_spi_configure(void *, int, int, int);
    178  1.1  tnn static int rk_spi_transfer(void *, struct spi_transfer *);
    179  1.1  tnn 
    180  1.1  tnn static void rk_spi_txfifo_fill(struct rk_spi_softc * const, size_t);
    181  1.1  tnn static void rk_spi_rxfifo_drain(struct rk_spi_softc * const, size_t);
    182  1.1  tnn static void rk_spi_rxtx(struct rk_spi_softc * const);
    183  1.1  tnn static void rk_spi_set_interrupt_mask(struct rk_spi_softc * const);
    184  1.1  tnn static void rk_spi_start(struct rk_spi_softc * const);
    185  1.1  tnn static int rk_spi_intr(void *);
    186  1.1  tnn 
    187  1.1  tnn CFATTACH_DECL_NEW(rk_spi, sizeof(struct rk_spi_softc),
    188  1.1  tnn     rk_spi_match, rk_spi_attach, NULL, NULL);
    189  1.1  tnn 
    190  1.3  tnn static const struct fdtbus_spi_controller_func rk_spi_funcs = {
    191  1.3  tnn 	.get_controller = rk_spi_get_controller
    192  1.3  tnn };
    193  1.3  tnn 
    194  1.3  tnn static struct spi_controller *
    195  1.3  tnn rk_spi_get_controller(device_t dev)
    196  1.3  tnn {
    197  1.3  tnn 	struct rk_spi_softc * const sc = device_private(dev);
    198  1.3  tnn 
    199  1.3  tnn 	return &sc->sc_spi;
    200  1.3  tnn }
    201  1.3  tnn 
    202  1.1  tnn static int
    203  1.1  tnn rk_spi_match(device_t parent, cfdata_t cf, void *aux)
    204  1.1  tnn {
    205  1.1  tnn 	struct fdt_attach_args * const faa = aux;
    206  1.1  tnn 
    207  1.1  tnn 	return of_match_compatible(faa->faa_phandle, compatible);
    208  1.1  tnn }
    209  1.1  tnn 
    210  1.1  tnn static void
    211  1.1  tnn rk_spi_attach(device_t parent, device_t self, void *aux)
    212  1.1  tnn {
    213  1.1  tnn 	struct rk_spi_softc * const sc = device_private(self);
    214  1.1  tnn 	struct fdt_attach_args * const faa = aux;
    215  1.1  tnn 	const int phandle = faa->faa_phandle;
    216  1.1  tnn 	bus_addr_t addr;
    217  1.1  tnn 	bus_size_t size;
    218  1.1  tnn 	struct clk *sclk, *pclk;
    219  1.1  tnn 	char intrstr[128];
    220  1.3  tnn 
    221  1.1  tnn 	sc->sc_dev = self;
    222  1.1  tnn 	sc->sc_bst = faa->faa_bst;
    223  1.1  tnn 	SIMPLEQ_INIT(&sc->sc_q);
    224  1.1  tnn 
    225  1.1  tnn 	if ((sclk = fdtbus_clock_get(phandle, "spiclk")) == NULL
    226  1.1  tnn 	    || clk_enable(sclk) != 0) {
    227  1.1  tnn 		aprint_error(": couldn't enable sclk\n");
    228  1.1  tnn 		return;
    229  1.1  tnn 	}
    230  1.1  tnn 
    231  1.1  tnn 	if ((pclk = fdtbus_clock_get(phandle, "apb_pclk")) == NULL
    232  1.1  tnn 	    || clk_enable(pclk) != 0) {
    233  1.1  tnn 		aprint_error(": couldn't enable pclk\n");
    234  1.1  tnn 		return;
    235  1.1  tnn 	}
    236  1.1  tnn 
    237  1.1  tnn 	sc->sc_spi_freq = clk_get_rate(sclk);
    238  1.1  tnn 
    239  1.1  tnn 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
    240  1.1  tnn 	    || bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    241  1.1  tnn 		aprint_error(": couldn't map registers\n");
    242  1.1  tnn 		return;
    243  1.1  tnn 	}
    244  1.1  tnn 
    245  1.1  tnn 	SPIREG_WRITE(sc, SPI_ENR, 0);
    246  1.1  tnn 	SPIREG_WRITE(sc, SPI_IMR, 0);
    247  1.1  tnn 
    248  1.1  tnn 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    249  1.1  tnn 		aprint_error(": failed to decode interrupt\n");
    250  1.1  tnn 		return;
    251  1.1  tnn 	}
    252  1.1  tnn 
    253  1.1  tnn 	sc->sc_ih = fdtbus_intr_establish(phandle, 0, IPL_VM, 0, rk_spi_intr, sc);
    254  1.1  tnn 	if (sc->sc_ih == NULL) {
    255  1.1  tnn 		aprint_error(": unable to establish interrupt\n");
    256  1.1  tnn 		return;
    257  1.1  tnn 	}
    258  1.1  tnn 
    259  1.1  tnn 	aprint_naive("\n");
    260  1.1  tnn 	aprint_normal(": SPI\n");
    261  1.1  tnn 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    262  1.1  tnn 
    263  1.1  tnn 	sc->sc_spi.sct_cookie = sc;
    264  1.1  tnn 	sc->sc_spi.sct_configure = rk_spi_configure;
    265  1.1  tnn 	sc->sc_spi.sct_transfer = rk_spi_transfer;
    266  1.1  tnn 	sc->sc_spi.sct_nslaves = 2;
    267  1.1  tnn 
    268  1.3  tnn 	fdtbus_register_spi_controller(self, phandle, &rk_spi_funcs);
    269  1.3  tnn 	(void) fdtbus_attach_spibus(self, phandle, spibus_print);
    270  1.1  tnn }
    271  1.1  tnn 
    272  1.1  tnn static int
    273  1.1  tnn rk_spi_configure(void *cookie, int slave, int mode, int speed)
    274  1.1  tnn {
    275  1.1  tnn 	struct rk_spi_softc * const sc = cookie;
    276  1.1  tnn 	uint32_t ctrlr0;
    277  1.1  tnn 	uint16_t divider;
    278  1.1  tnn 
    279  1.1  tnn 	divider = (sc->sc_spi_freq / speed) & ~1;
    280  1.1  tnn 	if (divider < 2)
    281  1.1  tnn 		return EINVAL;
    282  1.1  tnn 
    283  1.1  tnn 	if (slave >= sc->sc_spi.sct_nslaves)
    284  1.1  tnn 		return EINVAL;
    285  1.1  tnn 
    286  1.1  tnn 	ctrlr0 = SPI_CTRLR0_BHT | __SHIFTIN(SPI_CTRLR0_DFS_8BIT, SPI_CTRLR0_DFS);
    287  1.1  tnn 
    288  1.1  tnn 	switch (mode) {
    289  1.1  tnn 	case SPI_MODE_0:
    290  1.1  tnn 		ctrlr0 |= 0;
    291  1.1  tnn 		break;
    292  1.1  tnn 	case SPI_MODE_1:
    293  1.1  tnn 		ctrlr0 |= SPI_CTRLR0_SCPH;
    294  1.1  tnn 		break;
    295  1.1  tnn 	case SPI_MODE_2:
    296  1.1  tnn 		ctrlr0 |= SPI_CTRLR0_SCPOL;
    297  1.1  tnn 		break;
    298  1.1  tnn 	case SPI_MODE_3:
    299  1.1  tnn 		ctrlr0 |= SPI_CTRLR0_SCPH | SPI_CTRLR0_SCPOL;
    300  1.1  tnn 		break;
    301  1.1  tnn 	default:
    302  1.1  tnn 		return EINVAL;
    303  1.1  tnn 	}
    304  1.1  tnn 
    305  1.1  tnn 	SPIREG_WRITE(sc, SPI_ENR, 0);
    306  1.1  tnn 	SPIREG_WRITE(sc, SPI_SER, 0);
    307  1.1  tnn 	SPIREG_WRITE(sc, SPI_CTRLR0, ctrlr0);
    308  1.1  tnn 	SPIREG_WRITE(sc, SPI_BAUDR, divider);
    309  1.1  tnn 
    310  1.1  tnn 	SPIREG_WRITE(sc, SPI_DMACR, 0);
    311  1.1  tnn 	SPIREG_WRITE(sc, SPI_DMATDLR, 0);
    312  1.1  tnn 	SPIREG_WRITE(sc, SPI_DMARDLR, 0);
    313  1.1  tnn 
    314  1.1  tnn 	SPIREG_WRITE(sc, SPI_IPR, 0);
    315  1.1  tnn 	SPIREG_WRITE(sc, SPI_IMR, 0);
    316  1.1  tnn 	SPIREG_WRITE(sc, SPI_ICR, SPI_ICR_ALL);
    317  1.1  tnn 
    318  1.1  tnn 	SPIREG_WRITE(sc, SPI_ENR, 1);
    319  1.1  tnn 
    320  1.1  tnn 	return 0;
    321  1.1  tnn }
    322  1.1  tnn 
    323  1.1  tnn static int
    324  1.1  tnn rk_spi_transfer(void *cookie, struct spi_transfer *st)
    325  1.1  tnn {
    326  1.1  tnn 	struct rk_spi_softc * const sc = cookie;
    327  1.1  tnn 	int s;
    328  1.1  tnn 
    329  1.1  tnn 	s = splbio();
    330  1.1  tnn 	spi_transq_enqueue(&sc->sc_q, st);
    331  1.1  tnn 	if (sc->sc_running == false) {
    332  1.1  tnn 		rk_spi_start(sc);
    333  1.1  tnn 	}
    334  1.1  tnn 	splx(s);
    335  1.1  tnn 
    336  1.1  tnn 	return 0;
    337  1.1  tnn }
    338  1.1  tnn 
    339  1.1  tnn static void
    340  1.1  tnn rk_spi_txfifo_fill(struct rk_spi_softc * const sc, size_t maxlen)
    341  1.1  tnn {
    342  1.1  tnn 	struct spi_chunk *chunk = sc->sc_wchunk;
    343  1.1  tnn 	size_t len;
    344  1.1  tnn 	uint8_t b;
    345  1.1  tnn 
    346  1.1  tnn 	if (chunk == NULL)
    347  1.1  tnn 		return;
    348  1.1  tnn 
    349  1.1  tnn 	len = MIN(maxlen, chunk->chunk_wresid);
    350  1.1  tnn 	chunk->chunk_wresid -= len;
    351  1.1  tnn 	while (len--) {
    352  1.1  tnn 		if (chunk->chunk_wptr) {
    353  1.1  tnn 			b = *chunk->chunk_wptr++;
    354  1.1  tnn 		} else {
    355  1.1  tnn 			b = 0;
    356  1.1  tnn 		}
    357  1.1  tnn 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, SPI_TXDR, b);
    358  1.1  tnn 	}
    359  1.1  tnn 	if (sc->sc_wchunk->chunk_wresid == 0) {
    360  1.1  tnn 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    361  1.1  tnn 	}
    362  1.1  tnn }
    363  1.1  tnn 
    364  1.1  tnn static void
    365  1.1  tnn rk_spi_rxfifo_drain(struct rk_spi_softc * const sc, size_t maxlen)
    366  1.1  tnn {
    367  1.1  tnn 	struct spi_chunk *chunk = sc->sc_rchunk;
    368  1.1  tnn 	size_t len;
    369  1.1  tnn 	uint8_t b;
    370  1.1  tnn 
    371  1.1  tnn 	if (chunk == NULL)
    372  1.1  tnn 		return;
    373  1.1  tnn 
    374  1.1  tnn 	len = MIN(maxlen, chunk->chunk_rresid);
    375  1.1  tnn 	chunk->chunk_rresid -= len;
    376  1.1  tnn 
    377  1.1  tnn 	while (len--) {
    378  1.1  tnn 		b = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SPI_RXDR);
    379  1.1  tnn 		if (chunk->chunk_rptr) {
    380  1.1  tnn 			*chunk->chunk_rptr++ = b;
    381  1.1  tnn 		}
    382  1.1  tnn 	}
    383  1.1  tnn 	if (sc->sc_rchunk->chunk_rresid == 0) {
    384  1.1  tnn 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    385  1.1  tnn 	}
    386  1.1  tnn }
    387  1.1  tnn 
    388  1.1  tnn static void
    389  1.1  tnn rk_spi_rxtx(struct rk_spi_softc * const sc)
    390  1.1  tnn {
    391  1.1  tnn 	bool again;
    392  1.1  tnn 	uint32_t reg;
    393  1.1  tnn 	size_t avail;
    394  1.1  tnn 
    395  1.1  tnn 	/* Service both FIFOs until no more progress can be made. */
    396  1.1  tnn 	again = true;
    397  1.1  tnn 	while (again) {
    398  1.1  tnn 		again = false;
    399  1.1  tnn 		reg = SPIREG_READ(sc, SPI_RXFLR);
    400  1.1  tnn 		avail = __SHIFTOUT(reg, SPI_RXFLR_RXFLR);
    401  1.1  tnn 		if (avail > 0) {
    402  1.1  tnn 			KASSERT(sc->sc_rchunk != NULL);
    403  1.1  tnn 			rk_spi_rxfifo_drain(sc, avail);
    404  1.1  tnn 			again = true;
    405  1.1  tnn 		}
    406  1.1  tnn 		reg = SPIREG_READ(sc, SPI_TXFLR);
    407  1.1  tnn 		avail = SPI_FIFOLEN - __SHIFTOUT(reg, SPI_TXFLR_TXFLR);
    408  1.1  tnn 		if (avail > 0 && sc->sc_wchunk != NULL) {
    409  1.1  tnn 			rk_spi_txfifo_fill(sc, avail);
    410  1.1  tnn 			again = true;
    411  1.1  tnn 		}
    412  1.1  tnn 	}
    413  1.1  tnn }
    414  1.1  tnn 
    415  1.1  tnn static void
    416  1.1  tnn rk_spi_set_interrupt_mask(struct rk_spi_softc * const sc)
    417  1.1  tnn {
    418  1.1  tnn 	uint32_t imr = SPI_IMR_RFOIM | SPI_IMR_RFUIM | SPI_IMR_TFOIM;
    419  1.1  tnn 	int len;
    420  1.1  tnn 
    421  1.1  tnn 	/*
    422  1.1  tnn 	 * Delay rx interrupts until the FIFO has the # of bytes we'd
    423  1.1  tnn 	 * ideally like to receive, or FIFO is half full.
    424  1.1  tnn 	 */
    425  1.1  tnn 	len = sc->sc_rchunk != NULL
    426  1.1  tnn 	    ? MIN(sc->sc_rchunk->chunk_rresid, SPI_FIFOLEN / 2) : 0;
    427  1.1  tnn 	if (len > 0) {
    428  1.1  tnn 		SPIREG_WRITE(sc, SPI_RXFTLR, len - 1);
    429  1.1  tnn 		imr |= SPI_IMR_RFFIM;
    430  1.1  tnn 	}
    431  1.1  tnn 
    432  1.1  tnn 	/*
    433  1.1  tnn 	 * Delay tx interrupts until the FIFO can accept the # of bytes we'd
    434  1.1  tnn 	 * ideally like to transmit, or the FIFO is half empty.
    435  1.1  tnn 	 */
    436  1.1  tnn 	len = sc->sc_wchunk != NULL
    437  1.1  tnn 	    ? MIN(sc->sc_wchunk->chunk_wresid, SPI_FIFOLEN / 2) : 0;
    438  1.1  tnn 	if (len > 0) {
    439  1.1  tnn 		SPIREG_WRITE(sc, SPI_TXFTLR, SPI_FIFOLEN - len);
    440  1.1  tnn 		imr |= SPI_IMR_TFEIM;
    441  1.1  tnn 	}
    442  1.1  tnn 
    443  1.1  tnn 	/* If xfer is done, then interrupt as soon as the tx fifo is empty. */
    444  1.1  tnn 	if (!ISSET(imr, (SPI_IMR_RFFIM | SPI_IMR_TFEIM))) {
    445  1.1  tnn 		SPIREG_WRITE(sc, SPI_TXFTLR, 0);
    446  1.1  tnn 		imr |= SPI_IMR_TFEIM;
    447  1.1  tnn 	}
    448  1.1  tnn 
    449  1.1  tnn 	SPIREG_WRITE(sc, SPI_IMR, imr);
    450  1.1  tnn }
    451  1.1  tnn 
    452  1.1  tnn static void
    453  1.1  tnn rk_spi_start(struct rk_spi_softc * const sc)
    454  1.1  tnn {
    455  1.1  tnn 	struct spi_transfer *st;
    456  1.1  tnn 
    457  1.1  tnn 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    458  1.1  tnn 		spi_transq_dequeue(&sc->sc_q);
    459  1.1  tnn 		KASSERT(sc->sc_transfer == NULL);
    460  1.1  tnn 		sc->sc_transfer = st;
    461  1.1  tnn 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    462  1.1  tnn 		sc->sc_running = true;
    463  1.1  tnn 
    464  1.1  tnn 		KASSERT(st->st_slave < sc->sc_spi.sct_nslaves);
    465  1.1  tnn 		SPIREG_WRITE(sc, SPI_SER, 1 << st->st_slave);
    466  1.1  tnn 
    467  1.1  tnn 		rk_spi_rxtx(sc);
    468  1.1  tnn 		rk_spi_set_interrupt_mask(sc);
    469  1.1  tnn 
    470  1.1  tnn 		if (!cold)
    471  1.1  tnn 			return;
    472  1.1  tnn 
    473  1.1  tnn 		for (;;) {
    474  1.1  tnn 			(void) rk_spi_intr(sc);
    475  1.1  tnn 			if (ISSET(st->st_flags, SPI_F_DONE))
    476  1.1  tnn 				break;
    477  1.1  tnn 		}
    478  1.1  tnn 	}
    479  1.1  tnn 	sc->sc_running = false;
    480  1.1  tnn }
    481  1.1  tnn 
    482  1.1  tnn static int
    483  1.1  tnn rk_spi_intr(void *cookie)
    484  1.1  tnn {
    485  1.1  tnn 	struct rk_spi_softc * const sc = cookie;
    486  1.1  tnn 	struct spi_transfer *st;
    487  1.1  tnn 	uint32_t isr;
    488  1.1  tnn 	uint32_t sr;
    489  1.1  tnn 	uint32_t icr = SPI_ICR_CCI;
    490  1.1  tnn 
    491  1.1  tnn 	isr = SPIREG_READ(sc, SPI_ISR);
    492  1.1  tnn 	if (!isr)
    493  1.1  tnn 		return 0;
    494  1.1  tnn 
    495  1.1  tnn 	if (ISSET(isr, SPI_ISR_RFOIS)) {
    496  1.1  tnn 		device_printf(sc->sc_dev, "RXFIFO overflow\n");
    497  1.1  tnn 		icr |= SPI_ICR_CRFOI;
    498  1.1  tnn 	}
    499  1.1  tnn 	if (ISSET(isr, SPI_ISR_RFUIS)) {
    500  1.1  tnn 		device_printf(sc->sc_dev, "RXFIFO underflow\n");
    501  1.1  tnn 		icr |= SPI_ICR_CRFUI;
    502  1.1  tnn 	}
    503  1.1  tnn 	if (ISSET(isr, SPI_ISR_TFOIS)) {
    504  1.1  tnn 		device_printf(sc->sc_dev, "TXFIFO overflow\n");
    505  1.1  tnn 		icr |= SPI_ICR_CTFOI;
    506  1.1  tnn 	}
    507  1.1  tnn 
    508  1.1  tnn 	rk_spi_rxtx(sc);
    509  1.1  tnn 
    510  1.1  tnn 	if (sc->sc_rchunk == NULL && sc->sc_wchunk == NULL) {
    511  1.1  tnn 		do {
    512  1.1  tnn 			sr = SPIREG_READ(sc, SPI_SR);
    513  1.1  tnn 		} while (ISSET(sr, SPI_SR_BSF));
    514  1.1  tnn 		SPIREG_WRITE(sc, SPI_IMR, 0);
    515  1.1  tnn 		SPIREG_WRITE(sc, SPI_SER, 0);
    516  1.1  tnn 		st = sc->sc_transfer;
    517  1.1  tnn 		sc->sc_transfer = NULL;
    518  1.1  tnn 		KASSERT(st != NULL);
    519  1.1  tnn 		spi_done(st, 0);
    520  1.1  tnn 		sc->sc_running = false;
    521  1.1  tnn 	} else {
    522  1.1  tnn 		rk_spi_set_interrupt_mask(sc);
    523  1.1  tnn 	}
    524  1.1  tnn 
    525  1.1  tnn 	SPIREG_WRITE(sc, SPI_ICR, icr);
    526  1.1  tnn 
    527  1.1  tnn 	return 1;
    528  1.1  tnn }
    529