Home | History | Annotate | Line # | Download | only in rockchip
      1  1.11  thorpej /*	$NetBSD: rk_spi.c,v 1.11 2025/09/10 04:17:19 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.11  thorpej __KERNEL_RCSID(0, "$NetBSD: rk_spi.c,v 1.11 2025/09/10 04:17:19 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.11  thorpej 	spibus_attach(self, &sc->sc_spi);
    255   1.1      tnn }
    256   1.1      tnn 
    257   1.1      tnn static int
    258   1.1      tnn rk_spi_configure(void *cookie, int slave, int mode, int speed)
    259   1.1      tnn {
    260   1.1      tnn 	struct rk_spi_softc * const sc = cookie;
    261   1.1      tnn 	uint32_t ctrlr0;
    262   1.1      tnn 	uint16_t divider;
    263   1.1      tnn 
    264   1.1      tnn 	divider = (sc->sc_spi_freq / speed) & ~1;
    265   1.4      tnn 	if (divider < 2) {
    266   1.4      tnn 		aprint_error_dev(sc->sc_dev,
    267   1.4      tnn 		    "spi_clk %u is too low for speed %u, using speed %u\n",
    268   1.4      tnn 		     sc->sc_spi_freq, speed, sc->sc_spi_freq / 2);
    269   1.4      tnn 		divider = 2;
    270   1.4      tnn 	}
    271   1.1      tnn 
    272   1.1      tnn 	if (slave >= sc->sc_spi.sct_nslaves)
    273   1.1      tnn 		return EINVAL;
    274   1.1      tnn 
    275   1.1      tnn 	ctrlr0 = SPI_CTRLR0_BHT | __SHIFTIN(SPI_CTRLR0_DFS_8BIT, SPI_CTRLR0_DFS);
    276   1.1      tnn 
    277   1.1      tnn 	switch (mode) {
    278   1.1      tnn 	case SPI_MODE_0:
    279   1.1      tnn 		ctrlr0 |= 0;
    280   1.1      tnn 		break;
    281   1.1      tnn 	case SPI_MODE_1:
    282   1.1      tnn 		ctrlr0 |= SPI_CTRLR0_SCPH;
    283   1.1      tnn 		break;
    284   1.1      tnn 	case SPI_MODE_2:
    285   1.1      tnn 		ctrlr0 |= SPI_CTRLR0_SCPOL;
    286   1.1      tnn 		break;
    287   1.1      tnn 	case SPI_MODE_3:
    288   1.1      tnn 		ctrlr0 |= SPI_CTRLR0_SCPH | SPI_CTRLR0_SCPOL;
    289   1.1      tnn 		break;
    290   1.1      tnn 	default:
    291   1.1      tnn 		return EINVAL;
    292   1.1      tnn 	}
    293   1.1      tnn 
    294   1.1      tnn 	SPIREG_WRITE(sc, SPI_ENR, 0);
    295   1.1      tnn 	SPIREG_WRITE(sc, SPI_SER, 0);
    296   1.1      tnn 	SPIREG_WRITE(sc, SPI_CTRLR0, ctrlr0);
    297   1.1      tnn 	SPIREG_WRITE(sc, SPI_BAUDR, divider);
    298   1.1      tnn 
    299   1.1      tnn 	SPIREG_WRITE(sc, SPI_DMACR, 0);
    300   1.1      tnn 	SPIREG_WRITE(sc, SPI_DMATDLR, 0);
    301   1.1      tnn 	SPIREG_WRITE(sc, SPI_DMARDLR, 0);
    302   1.1      tnn 
    303   1.1      tnn 	SPIREG_WRITE(sc, SPI_IPR, 0);
    304   1.1      tnn 	SPIREG_WRITE(sc, SPI_IMR, 0);
    305   1.1      tnn 	SPIREG_WRITE(sc, SPI_ICR, SPI_ICR_ALL);
    306   1.1      tnn 
    307   1.1      tnn 	SPIREG_WRITE(sc, SPI_ENR, 1);
    308   1.1      tnn 
    309   1.1      tnn 	return 0;
    310   1.1      tnn }
    311   1.1      tnn 
    312   1.1      tnn static int
    313   1.1      tnn rk_spi_transfer(void *cookie, struct spi_transfer *st)
    314   1.1      tnn {
    315   1.1      tnn 	struct rk_spi_softc * const sc = cookie;
    316   1.1      tnn 	int s;
    317   1.1      tnn 
    318   1.1      tnn 	s = splbio();
    319   1.1      tnn 	spi_transq_enqueue(&sc->sc_q, st);
    320   1.1      tnn 	if (sc->sc_running == false) {
    321   1.1      tnn 		rk_spi_start(sc);
    322   1.1      tnn 	}
    323   1.1      tnn 	splx(s);
    324   1.1      tnn 
    325   1.1      tnn 	return 0;
    326   1.1      tnn }
    327   1.1      tnn 
    328   1.1      tnn static void
    329   1.1      tnn rk_spi_txfifo_fill(struct rk_spi_softc * const sc, size_t maxlen)
    330   1.1      tnn {
    331   1.1      tnn 	struct spi_chunk *chunk = sc->sc_wchunk;
    332   1.1      tnn 	size_t len;
    333   1.1      tnn 	uint8_t b;
    334   1.1      tnn 
    335   1.1      tnn 	if (chunk == NULL)
    336   1.1      tnn 		return;
    337   1.1      tnn 
    338   1.1      tnn 	len = MIN(maxlen, chunk->chunk_wresid);
    339   1.1      tnn 	chunk->chunk_wresid -= len;
    340   1.1      tnn 	while (len--) {
    341   1.1      tnn 		if (chunk->chunk_wptr) {
    342   1.1      tnn 			b = *chunk->chunk_wptr++;
    343   1.1      tnn 		} else {
    344   1.1      tnn 			b = 0;
    345   1.1      tnn 		}
    346   1.1      tnn 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, SPI_TXDR, b);
    347   1.1      tnn 	}
    348   1.1      tnn 	if (sc->sc_wchunk->chunk_wresid == 0) {
    349   1.1      tnn 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    350   1.1      tnn 	}
    351   1.1      tnn }
    352   1.1      tnn 
    353   1.1      tnn static void
    354   1.1      tnn rk_spi_rxfifo_drain(struct rk_spi_softc * const sc, size_t maxlen)
    355   1.1      tnn {
    356   1.1      tnn 	struct spi_chunk *chunk = sc->sc_rchunk;
    357   1.1      tnn 	size_t len;
    358   1.1      tnn 	uint8_t b;
    359   1.1      tnn 
    360   1.1      tnn 	if (chunk == NULL)
    361   1.1      tnn 		return;
    362   1.1      tnn 
    363   1.1      tnn 	len = MIN(maxlen, chunk->chunk_rresid);
    364   1.1      tnn 	chunk->chunk_rresid -= len;
    365   1.1      tnn 
    366   1.1      tnn 	while (len--) {
    367   1.1      tnn 		b = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SPI_RXDR);
    368   1.1      tnn 		if (chunk->chunk_rptr) {
    369   1.1      tnn 			*chunk->chunk_rptr++ = b;
    370   1.1      tnn 		}
    371   1.1      tnn 	}
    372   1.1      tnn 	if (sc->sc_rchunk->chunk_rresid == 0) {
    373   1.1      tnn 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    374   1.1      tnn 	}
    375   1.1      tnn }
    376   1.1      tnn 
    377   1.1      tnn static void
    378   1.1      tnn rk_spi_rxtx(struct rk_spi_softc * const sc)
    379   1.1      tnn {
    380   1.1      tnn 	bool again;
    381   1.1      tnn 	uint32_t reg;
    382   1.1      tnn 	size_t avail;
    383   1.1      tnn 
    384   1.1      tnn 	/* Service both FIFOs until no more progress can be made. */
    385   1.1      tnn 	again = true;
    386   1.1      tnn 	while (again) {
    387   1.1      tnn 		again = false;
    388   1.1      tnn 		reg = SPIREG_READ(sc, SPI_RXFLR);
    389   1.1      tnn 		avail = __SHIFTOUT(reg, SPI_RXFLR_RXFLR);
    390   1.1      tnn 		if (avail > 0) {
    391   1.1      tnn 			KASSERT(sc->sc_rchunk != NULL);
    392   1.1      tnn 			rk_spi_rxfifo_drain(sc, avail);
    393   1.1      tnn 			again = true;
    394   1.1      tnn 		}
    395   1.1      tnn 		reg = SPIREG_READ(sc, SPI_TXFLR);
    396   1.1      tnn 		avail = SPI_FIFOLEN - __SHIFTOUT(reg, SPI_TXFLR_TXFLR);
    397   1.1      tnn 		if (avail > 0 && sc->sc_wchunk != NULL) {
    398   1.1      tnn 			rk_spi_txfifo_fill(sc, avail);
    399   1.1      tnn 			again = true;
    400   1.1      tnn 		}
    401   1.1      tnn 	}
    402   1.1      tnn }
    403   1.1      tnn 
    404   1.1      tnn static void
    405   1.1      tnn rk_spi_set_interrupt_mask(struct rk_spi_softc * const sc)
    406   1.1      tnn {
    407   1.1      tnn 	uint32_t imr = SPI_IMR_RFOIM | SPI_IMR_RFUIM | SPI_IMR_TFOIM;
    408   1.1      tnn 	int len;
    409   1.1      tnn 
    410   1.1      tnn 	/*
    411   1.1      tnn 	 * Delay rx interrupts until the FIFO has the # of bytes we'd
    412   1.1      tnn 	 * ideally like to receive, or FIFO is half full.
    413   1.1      tnn 	 */
    414   1.1      tnn 	len = sc->sc_rchunk != NULL
    415   1.1      tnn 	    ? MIN(sc->sc_rchunk->chunk_rresid, SPI_FIFOLEN / 2) : 0;
    416   1.1      tnn 	if (len > 0) {
    417   1.1      tnn 		SPIREG_WRITE(sc, SPI_RXFTLR, len - 1);
    418   1.1      tnn 		imr |= SPI_IMR_RFFIM;
    419   1.1      tnn 	}
    420   1.1      tnn 
    421   1.1      tnn 	/*
    422   1.1      tnn 	 * Delay tx interrupts until the FIFO can accept the # of bytes we'd
    423   1.1      tnn 	 * ideally like to transmit, or the FIFO is half empty.
    424   1.1      tnn 	 */
    425   1.1      tnn 	len = sc->sc_wchunk != NULL
    426   1.1      tnn 	    ? MIN(sc->sc_wchunk->chunk_wresid, SPI_FIFOLEN / 2) : 0;
    427   1.1      tnn 	if (len > 0) {
    428   1.1      tnn 		SPIREG_WRITE(sc, SPI_TXFTLR, SPI_FIFOLEN - len);
    429   1.1      tnn 		imr |= SPI_IMR_TFEIM;
    430   1.1      tnn 	}
    431   1.1      tnn 
    432   1.1      tnn 	/* If xfer is done, then interrupt as soon as the tx fifo is empty. */
    433   1.1      tnn 	if (!ISSET(imr, (SPI_IMR_RFFIM | SPI_IMR_TFEIM))) {
    434   1.1      tnn 		SPIREG_WRITE(sc, SPI_TXFTLR, 0);
    435   1.1      tnn 		imr |= SPI_IMR_TFEIM;
    436   1.1      tnn 	}
    437   1.1      tnn 
    438   1.1      tnn 	SPIREG_WRITE(sc, SPI_IMR, imr);
    439   1.1      tnn }
    440   1.1      tnn 
    441   1.1      tnn static void
    442   1.1      tnn rk_spi_start(struct rk_spi_softc * const sc)
    443   1.1      tnn {
    444   1.1      tnn 	struct spi_transfer *st;
    445   1.1      tnn 
    446   1.1      tnn 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    447   1.1      tnn 		spi_transq_dequeue(&sc->sc_q);
    448   1.1      tnn 		KASSERT(sc->sc_transfer == NULL);
    449   1.1      tnn 		sc->sc_transfer = st;
    450   1.1      tnn 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    451   1.1      tnn 		sc->sc_running = true;
    452   1.1      tnn 
    453   1.1      tnn 		KASSERT(st->st_slave < sc->sc_spi.sct_nslaves);
    454   1.1      tnn 		SPIREG_WRITE(sc, SPI_SER, 1 << st->st_slave);
    455   1.1      tnn 
    456   1.1      tnn 		rk_spi_rxtx(sc);
    457   1.1      tnn 		rk_spi_set_interrupt_mask(sc);
    458   1.1      tnn 
    459   1.1      tnn 		if (!cold)
    460   1.1      tnn 			return;
    461   1.1      tnn 
    462   1.1      tnn 		for (;;) {
    463   1.1      tnn 			(void) rk_spi_intr(sc);
    464   1.1      tnn 			if (ISSET(st->st_flags, SPI_F_DONE))
    465   1.1      tnn 				break;
    466   1.1      tnn 		}
    467   1.1      tnn 	}
    468   1.1      tnn 	sc->sc_running = false;
    469   1.1      tnn }
    470   1.1      tnn 
    471   1.1      tnn static int
    472   1.1      tnn rk_spi_intr(void *cookie)
    473   1.1      tnn {
    474   1.1      tnn 	struct rk_spi_softc * const sc = cookie;
    475   1.1      tnn 	struct spi_transfer *st;
    476   1.1      tnn 	uint32_t isr;
    477   1.1      tnn 	uint32_t sr;
    478   1.1      tnn 	uint32_t icr = SPI_ICR_CCI;
    479   1.1      tnn 
    480   1.1      tnn 	isr = SPIREG_READ(sc, SPI_ISR);
    481   1.1      tnn 	if (!isr)
    482   1.1      tnn 		return 0;
    483   1.1      tnn 
    484   1.1      tnn 	if (ISSET(isr, SPI_ISR_RFOIS)) {
    485   1.1      tnn 		device_printf(sc->sc_dev, "RXFIFO overflow\n");
    486   1.1      tnn 		icr |= SPI_ICR_CRFOI;
    487   1.1      tnn 	}
    488   1.1      tnn 	if (ISSET(isr, SPI_ISR_RFUIS)) {
    489   1.1      tnn 		device_printf(sc->sc_dev, "RXFIFO underflow\n");
    490   1.1      tnn 		icr |= SPI_ICR_CRFUI;
    491   1.1      tnn 	}
    492   1.1      tnn 	if (ISSET(isr, SPI_ISR_TFOIS)) {
    493   1.1      tnn 		device_printf(sc->sc_dev, "TXFIFO overflow\n");
    494   1.1      tnn 		icr |= SPI_ICR_CTFOI;
    495   1.1      tnn 	}
    496   1.1      tnn 
    497   1.1      tnn 	rk_spi_rxtx(sc);
    498   1.1      tnn 
    499   1.1      tnn 	if (sc->sc_rchunk == NULL && sc->sc_wchunk == NULL) {
    500   1.1      tnn 		do {
    501   1.1      tnn 			sr = SPIREG_READ(sc, SPI_SR);
    502   1.1      tnn 		} while (ISSET(sr, SPI_SR_BSF));
    503   1.1      tnn 		SPIREG_WRITE(sc, SPI_IMR, 0);
    504   1.1      tnn 		SPIREG_WRITE(sc, SPI_SER, 0);
    505   1.1      tnn 		st = sc->sc_transfer;
    506   1.1      tnn 		sc->sc_transfer = NULL;
    507   1.1      tnn 		KASSERT(st != NULL);
    508   1.1      tnn 		spi_done(st, 0);
    509   1.1      tnn 		sc->sc_running = false;
    510   1.1      tnn 	} else {
    511   1.1      tnn 		rk_spi_set_interrupt_mask(sc);
    512   1.1      tnn 	}
    513   1.1      tnn 
    514   1.1      tnn 	SPIREG_WRITE(sc, SPI_ICR, icr);
    515   1.1      tnn 
    516   1.1      tnn 	return 1;
    517   1.1      tnn }
    518