Home | History | Annotate | Line # | Download | only in rockchip
rk_spi.c revision 1.6.4.2
      1  1.6.4.2  thorpej /*	$NetBSD: rk_spi.c,v 1.6.4.2 2021/06/17 04:46:18 thorpej 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.6.4.2  thorpej __KERNEL_RCSID(0, "$NetBSD: rk_spi.c,v 1.6.4.2 2021/06/17 04:46:18 thorpej 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.6  thorpej static const struct device_compatible_entry compat_data[] = {
    147      1.6  thorpej 	{ .compat = "rockchip,rk3066-spi" },
    148      1.6  thorpej 	{ .compat = "rockchip,rk3328-spi" },
    149      1.6  thorpej 	{ .compat = "rockchip,rk3399-spi" },
    150      1.6  thorpej 	DEVICE_COMPAT_EOL
    151      1.1      tnn };
    152      1.1      tnn 
    153      1.1      tnn struct rk_spi_softc {
    154      1.1      tnn 	device_t		sc_dev;
    155      1.1      tnn 	bus_space_tag_t		sc_bst;
    156      1.1      tnn 	bus_space_handle_t	sc_bsh;
    157      1.1      tnn 	void			*sc_ih;
    158      1.1      tnn 	u_int			sc_spi_freq;
    159      1.1      tnn 	struct spi_controller	sc_spi;
    160      1.1      tnn 	SIMPLEQ_HEAD(,spi_transfer) sc_q;
    161      1.1      tnn 	struct spi_transfer	*sc_transfer;
    162      1.1      tnn 	struct spi_chunk	*sc_rchunk, *sc_wchunk;
    163      1.1      tnn 	volatile bool		sc_running;
    164      1.1      tnn };
    165      1.1      tnn 
    166      1.1      tnn #define SPIREG_READ(sc, reg) \
    167      1.1      tnn     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
    168      1.1      tnn #define SPIREG_WRITE(sc, reg, val) \
    169      1.1      tnn     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
    170      1.1      tnn 
    171      1.1      tnn static int rk_spi_match(device_t, cfdata_t, void *);
    172      1.1      tnn static void rk_spi_attach(device_t, device_t, void *);
    173      1.1      tnn 
    174      1.1      tnn static int rk_spi_configure(void *, int, int, int);
    175      1.1      tnn static int rk_spi_transfer(void *, struct spi_transfer *);
    176      1.1      tnn 
    177      1.1      tnn static void rk_spi_txfifo_fill(struct rk_spi_softc * const, size_t);
    178      1.1      tnn static void rk_spi_rxfifo_drain(struct rk_spi_softc * const, size_t);
    179      1.1      tnn static void rk_spi_rxtx(struct rk_spi_softc * const);
    180      1.1      tnn static void rk_spi_set_interrupt_mask(struct rk_spi_softc * const);
    181      1.1      tnn static void rk_spi_start(struct rk_spi_softc * const);
    182      1.1      tnn static int rk_spi_intr(void *);
    183      1.1      tnn 
    184      1.1      tnn CFATTACH_DECL_NEW(rk_spi, sizeof(struct rk_spi_softc),
    185      1.1      tnn     rk_spi_match, rk_spi_attach, NULL, NULL);
    186      1.1      tnn 
    187      1.1      tnn static int
    188      1.1      tnn rk_spi_match(device_t parent, cfdata_t cf, void *aux)
    189      1.1      tnn {
    190      1.1      tnn 	struct fdt_attach_args * const faa = aux;
    191      1.1      tnn 
    192      1.6  thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
    193      1.1      tnn }
    194      1.1      tnn 
    195      1.1      tnn static void
    196      1.1      tnn rk_spi_attach(device_t parent, device_t self, void *aux)
    197      1.1      tnn {
    198      1.1      tnn 	struct rk_spi_softc * const sc = device_private(self);
    199      1.1      tnn 	struct fdt_attach_args * const faa = aux;
    200      1.1      tnn 	const int phandle = faa->faa_phandle;
    201      1.1      tnn 	bus_addr_t addr;
    202      1.1      tnn 	bus_size_t size;
    203      1.1      tnn 	struct clk *sclk, *pclk;
    204      1.1      tnn 	char intrstr[128];
    205      1.3      tnn 
    206      1.1      tnn 	sc->sc_dev = self;
    207      1.1      tnn 	sc->sc_bst = faa->faa_bst;
    208      1.1      tnn 	SIMPLEQ_INIT(&sc->sc_q);
    209      1.1      tnn 
    210      1.1      tnn 	if ((sclk = fdtbus_clock_get(phandle, "spiclk")) == NULL
    211      1.1      tnn 	    || clk_enable(sclk) != 0) {
    212      1.1      tnn 		aprint_error(": couldn't enable sclk\n");
    213      1.1      tnn 		return;
    214      1.1      tnn 	}
    215      1.1      tnn 
    216      1.1      tnn 	if ((pclk = fdtbus_clock_get(phandle, "apb_pclk")) == NULL
    217      1.1      tnn 	    || clk_enable(pclk) != 0) {
    218      1.1      tnn 		aprint_error(": couldn't enable pclk\n");
    219      1.1      tnn 		return;
    220      1.1      tnn 	}
    221      1.1      tnn 
    222      1.1      tnn 	sc->sc_spi_freq = clk_get_rate(sclk);
    223      1.1      tnn 
    224      1.1      tnn 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
    225      1.1      tnn 	    || bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    226      1.1      tnn 		aprint_error(": couldn't map registers\n");
    227      1.1      tnn 		return;
    228      1.1      tnn 	}
    229      1.1      tnn 
    230      1.1      tnn 	SPIREG_WRITE(sc, SPI_ENR, 0);
    231      1.1      tnn 	SPIREG_WRITE(sc, SPI_IMR, 0);
    232      1.1      tnn 
    233      1.1      tnn 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    234      1.1      tnn 		aprint_error(": failed to decode interrupt\n");
    235      1.1      tnn 		return;
    236      1.1      tnn 	}
    237      1.1      tnn 
    238      1.5      ryo 	sc->sc_ih = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
    239      1.5      ryo 	    rk_spi_intr, sc, device_xname(self));
    240      1.1      tnn 	if (sc->sc_ih == NULL) {
    241      1.1      tnn 		aprint_error(": unable to establish interrupt\n");
    242      1.1      tnn 		return;
    243      1.1      tnn 	}
    244      1.1      tnn 
    245      1.1      tnn 	aprint_naive("\n");
    246      1.1      tnn 	aprint_normal(": SPI\n");
    247      1.1      tnn 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    248      1.1      tnn 
    249      1.1      tnn 	sc->sc_spi.sct_cookie = sc;
    250      1.1      tnn 	sc->sc_spi.sct_configure = rk_spi_configure;
    251      1.1      tnn 	sc->sc_spi.sct_transfer = rk_spi_transfer;
    252      1.1      tnn 	sc->sc_spi.sct_nslaves = 2;
    253      1.1      tnn 
    254  1.6.4.1  thorpej 	fdtbus_register_spi_controller(&sc->sc_spi, phandle);
    255  1.6.4.1  thorpej 
    256  1.6.4.1  thorpej 	struct spibus_attach_args sba = {
    257  1.6.4.1  thorpej 		.sba_controller = &sc->sc_spi,
    258  1.6.4.1  thorpej 	};
    259  1.6.4.1  thorpej 	config_found(self, &sba, spibus_print,
    260  1.6.4.1  thorpej 	    CFARG_DEVHANDLE, device_handle(self),
    261  1.6.4.1  thorpej 	    CFARG_EOL);
    262      1.1      tnn }
    263      1.1      tnn 
    264      1.1      tnn static int
    265      1.1      tnn rk_spi_configure(void *cookie, int slave, int mode, int speed)
    266      1.1      tnn {
    267      1.1      tnn 	struct rk_spi_softc * const sc = cookie;
    268      1.1      tnn 	uint32_t ctrlr0;
    269      1.1      tnn 	uint16_t divider;
    270      1.1      tnn 
    271      1.1      tnn 	divider = (sc->sc_spi_freq / speed) & ~1;
    272      1.4      tnn 	if (divider < 2) {
    273      1.4      tnn 		aprint_error_dev(sc->sc_dev,
    274      1.4      tnn 		    "spi_clk %u is too low for speed %u, using speed %u\n",
    275      1.4      tnn 		     sc->sc_spi_freq, speed, sc->sc_spi_freq / 2);
    276      1.4      tnn 		divider = 2;
    277      1.4      tnn 	}
    278      1.1      tnn 
    279      1.1      tnn 	if (slave >= sc->sc_spi.sct_nslaves)
    280      1.1      tnn 		return EINVAL;
    281      1.1      tnn 
    282      1.1      tnn 	ctrlr0 = SPI_CTRLR0_BHT | __SHIFTIN(SPI_CTRLR0_DFS_8BIT, SPI_CTRLR0_DFS);
    283      1.1      tnn 
    284      1.1      tnn 	switch (mode) {
    285      1.1      tnn 	case SPI_MODE_0:
    286      1.1      tnn 		ctrlr0 |= 0;
    287      1.1      tnn 		break;
    288      1.1      tnn 	case SPI_MODE_1:
    289      1.1      tnn 		ctrlr0 |= SPI_CTRLR0_SCPH;
    290      1.1      tnn 		break;
    291      1.1      tnn 	case SPI_MODE_2:
    292      1.1      tnn 		ctrlr0 |= SPI_CTRLR0_SCPOL;
    293      1.1      tnn 		break;
    294      1.1      tnn 	case SPI_MODE_3:
    295      1.1      tnn 		ctrlr0 |= SPI_CTRLR0_SCPH | SPI_CTRLR0_SCPOL;
    296      1.1      tnn 		break;
    297      1.1      tnn 	default:
    298      1.1      tnn 		return EINVAL;
    299      1.1      tnn 	}
    300      1.1      tnn 
    301      1.1      tnn 	SPIREG_WRITE(sc, SPI_ENR, 0);
    302      1.1      tnn 	SPIREG_WRITE(sc, SPI_SER, 0);
    303      1.1      tnn 	SPIREG_WRITE(sc, SPI_CTRLR0, ctrlr0);
    304      1.1      tnn 	SPIREG_WRITE(sc, SPI_BAUDR, divider);
    305      1.1      tnn 
    306      1.1      tnn 	SPIREG_WRITE(sc, SPI_DMACR, 0);
    307      1.1      tnn 	SPIREG_WRITE(sc, SPI_DMATDLR, 0);
    308      1.1      tnn 	SPIREG_WRITE(sc, SPI_DMARDLR, 0);
    309      1.1      tnn 
    310      1.1      tnn 	SPIREG_WRITE(sc, SPI_IPR, 0);
    311      1.1      tnn 	SPIREG_WRITE(sc, SPI_IMR, 0);
    312      1.1      tnn 	SPIREG_WRITE(sc, SPI_ICR, SPI_ICR_ALL);
    313      1.1      tnn 
    314      1.1      tnn 	SPIREG_WRITE(sc, SPI_ENR, 1);
    315      1.1      tnn 
    316      1.1      tnn 	return 0;
    317      1.1      tnn }
    318      1.1      tnn 
    319      1.1      tnn static int
    320      1.1      tnn rk_spi_transfer(void *cookie, struct spi_transfer *st)
    321      1.1      tnn {
    322      1.1      tnn 	struct rk_spi_softc * const sc = cookie;
    323      1.1      tnn 	int s;
    324      1.1      tnn 
    325      1.1      tnn 	s = splbio();
    326      1.1      tnn 	spi_transq_enqueue(&sc->sc_q, st);
    327      1.1      tnn 	if (sc->sc_running == false) {
    328      1.1      tnn 		rk_spi_start(sc);
    329      1.1      tnn 	}
    330      1.1      tnn 	splx(s);
    331      1.1      tnn 
    332      1.1      tnn 	return 0;
    333      1.1      tnn }
    334      1.1      tnn 
    335      1.1      tnn static void
    336      1.1      tnn rk_spi_txfifo_fill(struct rk_spi_softc * const sc, size_t maxlen)
    337      1.1      tnn {
    338      1.1      tnn 	struct spi_chunk *chunk = sc->sc_wchunk;
    339      1.1      tnn 	size_t len;
    340      1.1      tnn 	uint8_t b;
    341      1.1      tnn 
    342      1.1      tnn 	if (chunk == NULL)
    343      1.1      tnn 		return;
    344      1.1      tnn 
    345      1.1      tnn 	len = MIN(maxlen, chunk->chunk_wresid);
    346      1.1      tnn 	chunk->chunk_wresid -= len;
    347      1.1      tnn 	while (len--) {
    348      1.1      tnn 		if (chunk->chunk_wptr) {
    349      1.1      tnn 			b = *chunk->chunk_wptr++;
    350      1.1      tnn 		} else {
    351      1.1      tnn 			b = 0;
    352      1.1      tnn 		}
    353      1.1      tnn 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, SPI_TXDR, b);
    354      1.1      tnn 	}
    355      1.1      tnn 	if (sc->sc_wchunk->chunk_wresid == 0) {
    356      1.1      tnn 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    357      1.1      tnn 	}
    358      1.1      tnn }
    359      1.1      tnn 
    360      1.1      tnn static void
    361      1.1      tnn rk_spi_rxfifo_drain(struct rk_spi_softc * const sc, size_t maxlen)
    362      1.1      tnn {
    363      1.1      tnn 	struct spi_chunk *chunk = sc->sc_rchunk;
    364      1.1      tnn 	size_t len;
    365      1.1      tnn 	uint8_t b;
    366      1.1      tnn 
    367      1.1      tnn 	if (chunk == NULL)
    368      1.1      tnn 		return;
    369      1.1      tnn 
    370      1.1      tnn 	len = MIN(maxlen, chunk->chunk_rresid);
    371      1.1      tnn 	chunk->chunk_rresid -= len;
    372      1.1      tnn 
    373      1.1      tnn 	while (len--) {
    374      1.1      tnn 		b = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SPI_RXDR);
    375      1.1      tnn 		if (chunk->chunk_rptr) {
    376      1.1      tnn 			*chunk->chunk_rptr++ = b;
    377      1.1      tnn 		}
    378      1.1      tnn 	}
    379      1.1      tnn 	if (sc->sc_rchunk->chunk_rresid == 0) {
    380      1.1      tnn 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    381      1.1      tnn 	}
    382      1.1      tnn }
    383      1.1      tnn 
    384      1.1      tnn static void
    385      1.1      tnn rk_spi_rxtx(struct rk_spi_softc * const sc)
    386      1.1      tnn {
    387      1.1      tnn 	bool again;
    388      1.1      tnn 	uint32_t reg;
    389      1.1      tnn 	size_t avail;
    390      1.1      tnn 
    391      1.1      tnn 	/* Service both FIFOs until no more progress can be made. */
    392      1.1      tnn 	again = true;
    393      1.1      tnn 	while (again) {
    394      1.1      tnn 		again = false;
    395      1.1      tnn 		reg = SPIREG_READ(sc, SPI_RXFLR);
    396      1.1      tnn 		avail = __SHIFTOUT(reg, SPI_RXFLR_RXFLR);
    397      1.1      tnn 		if (avail > 0) {
    398      1.1      tnn 			KASSERT(sc->sc_rchunk != NULL);
    399      1.1      tnn 			rk_spi_rxfifo_drain(sc, avail);
    400      1.1      tnn 			again = true;
    401      1.1      tnn 		}
    402      1.1      tnn 		reg = SPIREG_READ(sc, SPI_TXFLR);
    403      1.1      tnn 		avail = SPI_FIFOLEN - __SHIFTOUT(reg, SPI_TXFLR_TXFLR);
    404      1.1      tnn 		if (avail > 0 && sc->sc_wchunk != NULL) {
    405      1.1      tnn 			rk_spi_txfifo_fill(sc, avail);
    406      1.1      tnn 			again = true;
    407      1.1      tnn 		}
    408      1.1      tnn 	}
    409      1.1      tnn }
    410      1.1      tnn 
    411      1.1      tnn static void
    412      1.1      tnn rk_spi_set_interrupt_mask(struct rk_spi_softc * const sc)
    413      1.1      tnn {
    414      1.1      tnn 	uint32_t imr = SPI_IMR_RFOIM | SPI_IMR_RFUIM | SPI_IMR_TFOIM;
    415      1.1      tnn 	int len;
    416      1.1      tnn 
    417      1.1      tnn 	/*
    418      1.1      tnn 	 * Delay rx interrupts until the FIFO has the # of bytes we'd
    419      1.1      tnn 	 * ideally like to receive, or FIFO is half full.
    420      1.1      tnn 	 */
    421      1.1      tnn 	len = sc->sc_rchunk != NULL
    422      1.1      tnn 	    ? MIN(sc->sc_rchunk->chunk_rresid, SPI_FIFOLEN / 2) : 0;
    423      1.1      tnn 	if (len > 0) {
    424      1.1      tnn 		SPIREG_WRITE(sc, SPI_RXFTLR, len - 1);
    425      1.1      tnn 		imr |= SPI_IMR_RFFIM;
    426      1.1      tnn 	}
    427      1.1      tnn 
    428      1.1      tnn 	/*
    429      1.1      tnn 	 * Delay tx interrupts until the FIFO can accept the # of bytes we'd
    430      1.1      tnn 	 * ideally like to transmit, or the FIFO is half empty.
    431      1.1      tnn 	 */
    432      1.1      tnn 	len = sc->sc_wchunk != NULL
    433      1.1      tnn 	    ? MIN(sc->sc_wchunk->chunk_wresid, SPI_FIFOLEN / 2) : 0;
    434      1.1      tnn 	if (len > 0) {
    435      1.1      tnn 		SPIREG_WRITE(sc, SPI_TXFTLR, SPI_FIFOLEN - len);
    436      1.1      tnn 		imr |= SPI_IMR_TFEIM;
    437      1.1      tnn 	}
    438      1.1      tnn 
    439      1.1      tnn 	/* If xfer is done, then interrupt as soon as the tx fifo is empty. */
    440      1.1      tnn 	if (!ISSET(imr, (SPI_IMR_RFFIM | SPI_IMR_TFEIM))) {
    441      1.1      tnn 		SPIREG_WRITE(sc, SPI_TXFTLR, 0);
    442      1.1      tnn 		imr |= SPI_IMR_TFEIM;
    443      1.1      tnn 	}
    444      1.1      tnn 
    445      1.1      tnn 	SPIREG_WRITE(sc, SPI_IMR, imr);
    446      1.1      tnn }
    447      1.1      tnn 
    448      1.1      tnn static void
    449      1.1      tnn rk_spi_start(struct rk_spi_softc * const sc)
    450      1.1      tnn {
    451      1.1      tnn 	struct spi_transfer *st;
    452      1.1      tnn 
    453      1.1      tnn 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    454      1.1      tnn 		spi_transq_dequeue(&sc->sc_q);
    455      1.1      tnn 		KASSERT(sc->sc_transfer == NULL);
    456      1.1      tnn 		sc->sc_transfer = st;
    457      1.1      tnn 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    458      1.1      tnn 		sc->sc_running = true;
    459      1.1      tnn 
    460      1.1      tnn 		KASSERT(st->st_slave < sc->sc_spi.sct_nslaves);
    461      1.1      tnn 		SPIREG_WRITE(sc, SPI_SER, 1 << st->st_slave);
    462      1.1      tnn 
    463      1.1      tnn 		rk_spi_rxtx(sc);
    464      1.1      tnn 		rk_spi_set_interrupt_mask(sc);
    465      1.1      tnn 
    466      1.1      tnn 		if (!cold)
    467      1.1      tnn 			return;
    468      1.1      tnn 
    469      1.1      tnn 		for (;;) {
    470      1.1      tnn 			(void) rk_spi_intr(sc);
    471      1.1      tnn 			if (ISSET(st->st_flags, SPI_F_DONE))
    472      1.1      tnn 				break;
    473      1.1      tnn 		}
    474      1.1      tnn 	}
    475      1.1      tnn 	sc->sc_running = false;
    476      1.1      tnn }
    477      1.1      tnn 
    478      1.1      tnn static int
    479      1.1      tnn rk_spi_intr(void *cookie)
    480      1.1      tnn {
    481      1.1      tnn 	struct rk_spi_softc * const sc = cookie;
    482      1.1      tnn 	struct spi_transfer *st;
    483      1.1      tnn 	uint32_t isr;
    484      1.1      tnn 	uint32_t sr;
    485      1.1      tnn 	uint32_t icr = SPI_ICR_CCI;
    486      1.1      tnn 
    487      1.1      tnn 	isr = SPIREG_READ(sc, SPI_ISR);
    488      1.1      tnn 	if (!isr)
    489      1.1      tnn 		return 0;
    490      1.1      tnn 
    491      1.1      tnn 	if (ISSET(isr, SPI_ISR_RFOIS)) {
    492      1.1      tnn 		device_printf(sc->sc_dev, "RXFIFO overflow\n");
    493      1.1      tnn 		icr |= SPI_ICR_CRFOI;
    494      1.1      tnn 	}
    495      1.1      tnn 	if (ISSET(isr, SPI_ISR_RFUIS)) {
    496      1.1      tnn 		device_printf(sc->sc_dev, "RXFIFO underflow\n");
    497      1.1      tnn 		icr |= SPI_ICR_CRFUI;
    498      1.1      tnn 	}
    499      1.1      tnn 	if (ISSET(isr, SPI_ISR_TFOIS)) {
    500      1.1      tnn 		device_printf(sc->sc_dev, "TXFIFO overflow\n");
    501      1.1      tnn 		icr |= SPI_ICR_CTFOI;
    502      1.1      tnn 	}
    503      1.1      tnn 
    504      1.1      tnn 	rk_spi_rxtx(sc);
    505      1.1      tnn 
    506      1.1      tnn 	if (sc->sc_rchunk == NULL && sc->sc_wchunk == NULL) {
    507      1.1      tnn 		do {
    508      1.1      tnn 			sr = SPIREG_READ(sc, SPI_SR);
    509      1.1      tnn 		} while (ISSET(sr, SPI_SR_BSF));
    510      1.1      tnn 		SPIREG_WRITE(sc, SPI_IMR, 0);
    511      1.1      tnn 		SPIREG_WRITE(sc, SPI_SER, 0);
    512      1.1      tnn 		st = sc->sc_transfer;
    513      1.1      tnn 		sc->sc_transfer = NULL;
    514      1.1      tnn 		KASSERT(st != NULL);
    515      1.1      tnn 		spi_done(st, 0);
    516      1.1      tnn 		sc->sc_running = false;
    517      1.1      tnn 	} else {
    518      1.1      tnn 		rk_spi_set_interrupt_mask(sc);
    519      1.1      tnn 	}
    520      1.1      tnn 
    521      1.1      tnn 	SPIREG_WRITE(sc, SPI_ICR, icr);
    522      1.1      tnn 
    523      1.1      tnn 	return 1;
    524      1.1      tnn }
    525