Home | History | Annotate | Line # | Download | only in sunxi
sun4i_spi.c revision 1.6
      1  1.6  jmcneill /*	$NetBSD: sun4i_spi.c,v 1.6 2021/01/15 22:47:32 jmcneill Exp $	*/
      2  1.1       tnn 
      3  1.1       tnn /*
      4  1.1       tnn  * Copyright (c) 2019 Tobias Nygren
      5  1.1       tnn  * Copyright (c) 2018 Jonathan A. Kollasch
      6  1.1       tnn  * All rights reserved.
      7  1.1       tnn  *
      8  1.1       tnn  * Redistribution and use in source and binary forms, with or without
      9  1.1       tnn  * modification, are permitted provided that the following conditions
     10  1.1       tnn  * are met:
     11  1.1       tnn  * 1. Redistributions of source code must retain the above copyright
     12  1.1       tnn  *    notice, this list of conditions and the following disclaimer.
     13  1.1       tnn  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1       tnn  *    notice, this list of conditions and the following disclaimer in the
     15  1.1       tnn  *    documentation and/or other materials provided with the distribution.
     16  1.1       tnn  *
     17  1.1       tnn  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     18  1.1       tnn  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     19  1.1       tnn  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     20  1.1       tnn  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     21  1.1       tnn  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     22  1.1       tnn  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     23  1.1       tnn  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     24  1.1       tnn  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     25  1.1       tnn  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     26  1.1       tnn  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     27  1.1       tnn  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28  1.1       tnn  */
     29  1.1       tnn 
     30  1.1       tnn #include <sys/cdefs.h>
     31  1.6  jmcneill __KERNEL_RCSID(0, "$NetBSD: sun4i_spi.c,v 1.6 2021/01/15 22:47:32 jmcneill Exp $");
     32  1.1       tnn 
     33  1.1       tnn #include <sys/param.h>
     34  1.1       tnn #include <sys/device.h>
     35  1.1       tnn #include <sys/systm.h>
     36  1.1       tnn #include <sys/bus.h>
     37  1.1       tnn #include <sys/intr.h>
     38  1.1       tnn #include <sys/kernel.h>
     39  1.1       tnn #include <sys/bitops.h>
     40  1.1       tnn #include <dev/spi/spivar.h>
     41  1.1       tnn #include <arm/sunxi/sun4i_spireg.h>
     42  1.1       tnn #include <dev/fdt/fdtvar.h>
     43  1.1       tnn 
     44  1.1       tnn static const char * const compatible[] = {
     45  1.1       tnn 	"allwinner,sun4i-a10-spi",
     46  1.1       tnn 	NULL
     47  1.1       tnn };
     48  1.1       tnn 
     49  1.1       tnn struct sun4ispi_softc {
     50  1.1       tnn 	device_t		sc_dev;
     51  1.1       tnn 	bus_space_tag_t		sc_bst;
     52  1.1       tnn 	bus_space_handle_t	sc_bsh;
     53  1.1       tnn 	void			*sc_intrh;
     54  1.1       tnn 	struct spi_controller	sc_spi;
     55  1.1       tnn 	SIMPLEQ_HEAD(,spi_transfer) sc_q;
     56  1.1       tnn 	struct spi_transfer	*sc_transfer;
     57  1.1       tnn 	struct spi_chunk	*sc_rchunk, *sc_wchunk;
     58  1.1       tnn 	uint32_t		sc_CTL;
     59  1.1       tnn 	u_int			sc_modclkrate;
     60  1.1       tnn 	volatile bool		sc_running;
     61  1.1       tnn };
     62  1.1       tnn 
     63  1.1       tnn #define SPIREG_READ(sc, reg) \
     64  1.1       tnn     bus_space_read_4((sc)->sc_bst, (sc)->sc_bsh, (reg))
     65  1.1       tnn #define SPIREG_WRITE(sc, reg, val) \
     66  1.1       tnn     bus_space_write_4((sc)->sc_bst, (sc)->sc_bsh, (reg), (val))
     67  1.1       tnn 
     68  1.5    bouyer static struct spi_controller * sun4i_spi_get_controller(device_t);
     69  1.1       tnn static int sun4ispi_match(device_t, cfdata_t, void *);
     70  1.1       tnn static void sun4ispi_attach(device_t, device_t, void *);
     71  1.1       tnn 
     72  1.1       tnn static int sun4ispi_configure(void *, int, int, int);
     73  1.1       tnn static int sun4ispi_transfer(void *, struct spi_transfer *);
     74  1.1       tnn 
     75  1.1       tnn static void sun4ispi_txfifo_fill(struct sun4ispi_softc * const, size_t);
     76  1.1       tnn static void sun4ispi_rxfifo_drain(struct sun4ispi_softc * const, size_t);
     77  1.2       tnn static void sun4ispi_rxtx(struct sun4ispi_softc * const);
     78  1.1       tnn static void sun4ispi_set_interrupt_mask(struct sun4ispi_softc * const);
     79  1.1       tnn static void sun4ispi_start(struct sun4ispi_softc * const);
     80  1.1       tnn static int sun4ispi_intr(void *);
     81  1.1       tnn 
     82  1.1       tnn CFATTACH_DECL_NEW(sun4i_spi, sizeof(struct sun4ispi_softc),
     83  1.1       tnn     sun4ispi_match, sun4ispi_attach, NULL, NULL);
     84  1.1       tnn 
     85  1.5    bouyer static const struct fdtbus_spi_controller_func sun4i_spi_funcs = {
     86  1.5    bouyer 	.get_controller = sun4i_spi_get_controller
     87  1.5    bouyer };
     88  1.5    bouyer 
     89  1.5    bouyer static struct spi_controller *
     90  1.5    bouyer sun4i_spi_get_controller(device_t dev)
     91  1.5    bouyer {
     92  1.5    bouyer 	struct sun4ispi_softc * const sc = device_private(dev);
     93  1.5    bouyer 
     94  1.5    bouyer 	return &sc->sc_spi;
     95  1.5    bouyer }
     96  1.5    bouyer 
     97  1.1       tnn static int
     98  1.1       tnn sun4ispi_match(device_t parent, cfdata_t cf, void *aux)
     99  1.1       tnn {
    100  1.1       tnn 	struct fdt_attach_args * const faa = aux;
    101  1.1       tnn 
    102  1.1       tnn 	return of_match_compatible(faa->faa_phandle, compatible);
    103  1.1       tnn }
    104  1.1       tnn 
    105  1.1       tnn static void
    106  1.1       tnn sun4ispi_attach(device_t parent, device_t self, void *aux)
    107  1.1       tnn {
    108  1.1       tnn 	struct sun4ispi_softc * const sc = device_private(self);
    109  1.1       tnn 	struct fdt_attach_args * const faa = aux;
    110  1.1       tnn 	const int phandle = faa->faa_phandle;
    111  1.1       tnn 	bus_addr_t addr;
    112  1.1       tnn 	bus_size_t size;
    113  1.1       tnn 	struct clk *clk, *modclk;
    114  1.1       tnn 	char intrstr[128];
    115  1.1       tnn 
    116  1.1       tnn 	sc->sc_dev = self;
    117  1.1       tnn 	sc->sc_bst = faa->faa_bst;
    118  1.1       tnn 	SIMPLEQ_INIT(&sc->sc_q);
    119  1.1       tnn 
    120  1.1       tnn 	if ((clk = fdtbus_clock_get_index(phandle, 0)) == NULL
    121  1.1       tnn 	    || clk_enable(clk) != 0) {
    122  1.1       tnn 		aprint_error(": couldn't enable clock\n");
    123  1.1       tnn 		return;
    124  1.1       tnn 	}
    125  1.1       tnn 
    126  1.1       tnn 	if ((modclk = fdtbus_clock_get(phandle, "mod")) == NULL
    127  1.1       tnn 	    || clk_set_rate(modclk, clk_get_rate(clk)) != 0
    128  1.1       tnn 	    || clk_enable(modclk) != 0) {
    129  1.1       tnn 		aprint_error(": couldn't enable module clock\n");
    130  1.1       tnn 		return;
    131  1.1       tnn 	}
    132  1.1       tnn 	sc->sc_modclkrate = clk_get_rate(modclk);
    133  1.1       tnn 
    134  1.1       tnn 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0
    135  1.1       tnn 	    || bus_space_map(sc->sc_bst, addr, size, 0, &sc->sc_bsh) != 0) {
    136  1.1       tnn 		aprint_error(": couldn't map registers\n");
    137  1.1       tnn 		return;
    138  1.1       tnn 	}
    139  1.1       tnn 
    140  1.1       tnn 	SPIREG_WRITE(sc, SPI_CTL, SPI_CTL_SSPOL | SPI_CTL_RF_RST
    141  1.1       tnn 	    | SPI_CTL_TF_RST | SPI_CTL_MODE);
    142  1.1       tnn 	SPIREG_WRITE(sc, SPI_DMACTL, 0);
    143  1.1       tnn 	SPIREG_WRITE(sc, SPI_WAIT, 0);
    144  1.1       tnn 	SPIREG_WRITE(sc, SPI_INTCTL, 0);
    145  1.1       tnn 	SPIREG_WRITE(sc, SPI_INT_STA, ~0);
    146  1.1       tnn 
    147  1.1       tnn 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    148  1.1       tnn 		aprint_error(": failed to decode interrupt\n");
    149  1.1       tnn 		return;
    150  1.1       tnn 	}
    151  1.1       tnn 
    152  1.6  jmcneill 	sc->sc_intrh = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
    153  1.6  jmcneill 	    sun4ispi_intr, sc, device_xname(self));
    154  1.1       tnn 	if (sc->sc_intrh == NULL) {
    155  1.2       tnn 		aprint_error(": unable to establish interrupt\n");
    156  1.1       tnn 		return;
    157  1.1       tnn 	}
    158  1.1       tnn 
    159  1.1       tnn 	aprint_naive("\n");
    160  1.1       tnn 	aprint_normal(": SPI\n");
    161  1.1       tnn 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    162  1.1       tnn 
    163  1.1       tnn 	sc->sc_spi.sct_cookie = sc;
    164  1.1       tnn 	sc->sc_spi.sct_configure = sun4ispi_configure;
    165  1.1       tnn 	sc->sc_spi.sct_transfer = sun4ispi_transfer;
    166  1.1       tnn 	(void) of_getprop_uint32(phandle, "num-cs", &sc->sc_spi.sct_nslaves);
    167  1.5    bouyer 	fdtbus_register_spi_controller(self, phandle, &sun4i_spi_funcs);
    168  1.5    bouyer 	(void) fdtbus_attach_spibus(self, phandle, spibus_print);
    169  1.1       tnn }
    170  1.1       tnn 
    171  1.1       tnn static int
    172  1.1       tnn sun4ispi_configure(void *cookie, int slave, int mode, int speed)
    173  1.1       tnn {
    174  1.1       tnn 	struct sun4ispi_softc * const sc = cookie;
    175  1.1       tnn 	uint32_t ctl, cctl;
    176  1.1       tnn 	uint32_t minfreq, maxfreq;
    177  1.1       tnn 
    178  1.1       tnn 	minfreq = sc->sc_modclkrate >> 16;
    179  1.1       tnn 	maxfreq = sc->sc_modclkrate >> 1;
    180  1.1       tnn 
    181  1.1       tnn 	if (speed <= 0 || speed < minfreq || speed > maxfreq)
    182  1.1       tnn 		return EINVAL;
    183  1.1       tnn 
    184  1.1       tnn 	if (slave >= sc->sc_spi.sct_nslaves)
    185  1.1       tnn 		return EINVAL;
    186  1.1       tnn 
    187  1.1       tnn 	ctl = SPI_CTL_SDM | SPI_CTL_TP_EN | SPI_CTL_SSPOL | SPI_CTL_MODE | SPI_CTL_EN;
    188  1.1       tnn 
    189  1.1       tnn 	switch (mode) {
    190  1.1       tnn 	case SPI_MODE_0:
    191  1.1       tnn 		ctl |= 0;
    192  1.1       tnn 		break;
    193  1.1       tnn 	case SPI_MODE_1:
    194  1.1       tnn 		ctl |= SPI_CTL_PHA;
    195  1.1       tnn 		break;
    196  1.1       tnn 	case SPI_MODE_2:
    197  1.1       tnn 		ctl |= SPI_CTL_POL;
    198  1.1       tnn 		break;
    199  1.1       tnn 	case SPI_MODE_3:
    200  1.1       tnn 		ctl |= SPI_CTL_PHA | SPI_CTL_POL;
    201  1.1       tnn 		break;
    202  1.1       tnn 	default:
    203  1.1       tnn 		return EINVAL;
    204  1.1       tnn 	}
    205  1.1       tnn 
    206  1.1       tnn 	if (speed < sc->sc_modclkrate / 512) {
    207  1.1       tnn 		for (cctl = 0; cctl <= __SHIFTOUT_MASK(SPI_CCTL_CDR1); cctl++) {
    208  1.1       tnn 			if ((sc->sc_modclkrate / (1 << cctl)) <= speed)
    209  1.1       tnn 				goto cdr1_found;
    210  1.1       tnn 		}
    211  1.1       tnn 		return EINVAL;
    212  1.1       tnn cdr1_found:
    213  1.1       tnn 		cctl = __SHIFTIN(cctl, SPI_CCTL_CDR1);
    214  1.1       tnn 	} else {
    215  1.1       tnn 		cctl = howmany(sc->sc_modclkrate, 2 * speed) - 1;
    216  1.1       tnn 		cctl = SPI_CCTL_DRS|__SHIFTIN(cctl, SPI_CCTL_CDR2);
    217  1.1       tnn 	}
    218  1.1       tnn 
    219  1.1       tnn 	device_printf(sc->sc_dev, "ctl 0x%x, cctl 0x%x, CLK %uHz, SCLK %uHz\n",
    220  1.1       tnn 	    ctl, cctl, sc->sc_modclkrate,
    221  1.1       tnn 	    (cctl & SPI_CCTL_DRS)
    222  1.1       tnn 	    ? (sc->sc_modclkrate / (u_int)(2 * (__SHIFTOUT(cctl, SPI_CCTL_CDR2) + 1)))
    223  1.1       tnn 	    : (sc->sc_modclkrate >> (__SHIFTOUT(cctl, SPI_CCTL_CDR1) + 1))
    224  1.1       tnn 	);
    225  1.1       tnn 
    226  1.1       tnn 	sc->sc_CTL = ctl;
    227  1.1       tnn 	SPIREG_WRITE(sc, SPI_CTL, (ctl | SPI_CTL_RF_RST | SPI_CTL_TF_RST) & ~SPI_CTL_EN);
    228  1.1       tnn 	SPIREG_WRITE(sc, SPI_CCTL, cctl);
    229  1.1       tnn 	SPIREG_WRITE(sc, SPI_CTL, ctl);
    230  1.1       tnn 
    231  1.1       tnn 	return 0;
    232  1.1       tnn }
    233  1.1       tnn 
    234  1.1       tnn static int
    235  1.1       tnn sun4ispi_transfer(void *cookie, struct spi_transfer *st)
    236  1.1       tnn {
    237  1.1       tnn 	struct sun4ispi_softc * const sc = cookie;
    238  1.1       tnn 	int s;
    239  1.1       tnn 
    240  1.1       tnn 	s = splbio();
    241  1.1       tnn 	spi_transq_enqueue(&sc->sc_q, st);
    242  1.1       tnn 	if (sc->sc_running == false) {
    243  1.1       tnn 		sun4ispi_start(sc);
    244  1.1       tnn 	}
    245  1.1       tnn 	splx(s);
    246  1.1       tnn 
    247  1.1       tnn 	return 0;
    248  1.1       tnn }
    249  1.1       tnn 
    250  1.1       tnn static void
    251  1.1       tnn sun4ispi_txfifo_fill(struct sun4ispi_softc * const sc, size_t maxlen)
    252  1.1       tnn {
    253  1.1       tnn 	struct spi_chunk *chunk = sc->sc_wchunk;
    254  1.1       tnn 	size_t len;
    255  1.1       tnn 	uint8_t b;
    256  1.1       tnn 
    257  1.1       tnn 	if (chunk == NULL)
    258  1.1       tnn 		return;
    259  1.1       tnn 
    260  1.1       tnn 	len = MIN(maxlen, chunk->chunk_wresid);
    261  1.1       tnn 	chunk->chunk_wresid -= len;
    262  1.1       tnn 	while (len--) {
    263  1.1       tnn 		if (chunk->chunk_wptr) {
    264  1.1       tnn 			b = *chunk->chunk_wptr++;
    265  1.1       tnn 		} else {
    266  1.1       tnn 			b = 0;
    267  1.1       tnn 		}
    268  1.1       tnn 		bus_space_write_1(sc->sc_bst, sc->sc_bsh, SPI_TXDATA, b);
    269  1.1       tnn 	}
    270  1.1       tnn 	if (sc->sc_wchunk->chunk_wresid == 0) {
    271  1.1       tnn 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    272  1.1       tnn 	}
    273  1.1       tnn }
    274  1.1       tnn 
    275  1.1       tnn static void
    276  1.1       tnn sun4ispi_rxfifo_drain(struct sun4ispi_softc * const sc, size_t maxlen)
    277  1.1       tnn {
    278  1.1       tnn 	struct spi_chunk *chunk = sc->sc_rchunk;
    279  1.1       tnn 	size_t len;
    280  1.1       tnn 	uint8_t b;
    281  1.1       tnn 
    282  1.1       tnn 	if (chunk == NULL)
    283  1.1       tnn 		return;
    284  1.1       tnn 
    285  1.1       tnn 	len = MIN(maxlen, chunk->chunk_rresid);
    286  1.1       tnn 	chunk->chunk_rresid -= len;
    287  1.1       tnn 
    288  1.1       tnn 	while (len--) {
    289  1.1       tnn 		b = bus_space_read_1(sc->sc_bst, sc->sc_bsh, SPI_RXDATA);
    290  1.1       tnn 		if (chunk->chunk_rptr) {
    291  1.1       tnn 			*chunk->chunk_rptr++ = b;
    292  1.1       tnn 		}
    293  1.1       tnn 	}
    294  1.1       tnn 	if (sc->sc_rchunk->chunk_rresid == 0) {
    295  1.1       tnn 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    296  1.1       tnn 	}
    297  1.1       tnn }
    298  1.1       tnn 
    299  1.1       tnn static void
    300  1.1       tnn sun4ispi_rxtx(struct sun4ispi_softc * const sc)
    301  1.1       tnn {
    302  1.1       tnn 	bool again;
    303  1.1       tnn 	size_t rxavail, txavail;
    304  1.1       tnn 	uint32_t fsr;
    305  1.1       tnn 
    306  1.1       tnn 	/* service both FIFOs until no more progress can be made */
    307  1.1       tnn 	again = true;
    308  1.1       tnn 	while (again) {
    309  1.1       tnn 		again = false;
    310  1.1       tnn 		fsr = SPIREG_READ(sc, SPI_FIFO_STA);
    311  1.1       tnn 		rxavail = __SHIFTOUT(fsr, SPI_FIFO_STA_RF_CNT);
    312  1.1       tnn 		txavail = 64 - __SHIFTOUT(fsr, SPI_FIFO_STA_TF_CNT);
    313  1.1       tnn 		if (rxavail > 0) {
    314  1.1       tnn 			KASSERT(sc->sc_rchunk != NULL);
    315  1.1       tnn 			sun4ispi_rxfifo_drain(sc, rxavail);
    316  1.1       tnn 			again = true;
    317  1.1       tnn 		}
    318  1.1       tnn 		if (txavail > 0 && sc->sc_wchunk != NULL) {
    319  1.1       tnn 			sun4ispi_txfifo_fill(sc, txavail);
    320  1.1       tnn 			again = true;
    321  1.1       tnn 		}
    322  1.1       tnn 	}
    323  1.1       tnn }
    324  1.1       tnn 
    325  1.1       tnn static void
    326  1.1       tnn sun4ispi_set_interrupt_mask(struct sun4ispi_softc * const sc)
    327  1.1       tnn {
    328  1.1       tnn 	uint32_t intctl;
    329  1.1       tnn 
    330  1.1       tnn 	intctl = SPI_INTCTL_TX_INT_EN;
    331  1.1       tnn 	intctl |= SPI_INTCTL_RF_OF_INT_EN;
    332  1.1       tnn 	intctl |= SPI_INTCTL_TF_UR_INT_EN;
    333  1.1       tnn 
    334  1.1       tnn 	if (sc->sc_rchunk) {
    335  1.1       tnn 		if (sc->sc_rchunk->chunk_rresid >= 32) {
    336  1.1       tnn 			intctl |= SPI_INTCTL_RF_HALF_FU_INT_EN;
    337  1.1       tnn 		} else {
    338  1.1       tnn 			intctl |= SPI_INTCTL_RF_RDY_INT_EN;
    339  1.1       tnn 		}
    340  1.1       tnn 	}
    341  1.1       tnn 	if (sc->sc_wchunk) {
    342  1.1       tnn 		intctl |= SPI_INTCTL_TF_HALF_EMP_INT_EN;
    343  1.1       tnn 	}
    344  1.1       tnn 	SPIREG_WRITE(sc, SPI_INTCTL, intctl);
    345  1.1       tnn }
    346  1.1       tnn 
    347  1.1       tnn static void
    348  1.1       tnn sun4ispi_start(struct sun4ispi_softc * const sc)
    349  1.1       tnn {
    350  1.1       tnn 	struct spi_transfer *st;
    351  1.1       tnn 	uint32_t ctl;
    352  1.1       tnn 	struct spi_chunk *chunk;
    353  1.1       tnn 	size_t burstcount;
    354  1.1       tnn 
    355  1.1       tnn 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    356  1.1       tnn 
    357  1.1       tnn 		spi_transq_dequeue(&sc->sc_q);
    358  1.1       tnn 
    359  1.1       tnn 		KASSERT(sc->sc_transfer == NULL);
    360  1.1       tnn 		sc->sc_transfer = st;
    361  1.1       tnn 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    362  1.1       tnn 		sc->sc_running = true;
    363  1.1       tnn 
    364  1.1       tnn 		burstcount = 0;
    365  1.1       tnn 		for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
    366  1.1       tnn 			burstcount += chunk->chunk_count;
    367  1.1       tnn 		}
    368  1.1       tnn 		KASSERT(burstcount <= SPI_BC_BC);
    369  1.1       tnn 		SPIREG_WRITE(sc, SPI_BC, __SHIFTIN(burstcount, SPI_BC_BC));
    370  1.1       tnn 		SPIREG_WRITE(sc, SPI_TC, __SHIFTIN(burstcount, SPI_TC_WTC));
    371  1.1       tnn 
    372  1.1       tnn 		sun4ispi_rxtx(sc);
    373  1.1       tnn 		sun4ispi_set_interrupt_mask(sc);
    374  1.1       tnn 
    375  1.1       tnn 		KASSERT(st->st_slave < sc->sc_spi.sct_nslaves);
    376  1.1       tnn 		ctl = sc->sc_CTL | __SHIFTIN(st->st_slave, SPI_CTL_SS) | SPI_CTL_XCH;
    377  1.1       tnn 		SPIREG_WRITE(sc, SPI_CTL, ctl);
    378  1.1       tnn 
    379  1.1       tnn 		if (!cold)
    380  1.1       tnn 			return;
    381  1.1       tnn 
    382  1.1       tnn 		for (;;) {
    383  1.1       tnn 			(void) sun4ispi_intr(sc);
    384  1.1       tnn 			if (ISSET(st->st_flags, SPI_F_DONE))
    385  1.1       tnn 				break;
    386  1.1       tnn 		}
    387  1.1       tnn 	}
    388  1.1       tnn 	sc->sc_running = false;
    389  1.1       tnn }
    390  1.1       tnn 
    391  1.1       tnn static int
    392  1.1       tnn sun4ispi_intr(void *cookie)
    393  1.1       tnn {
    394  1.1       tnn 	struct sun4ispi_softc * const sc = cookie;
    395  1.1       tnn 	struct spi_transfer *st;
    396  1.1       tnn 	uint32_t isr;
    397  1.1       tnn 
    398  1.1       tnn 	isr = SPIREG_READ(sc, SPI_INT_STA);
    399  1.1       tnn 	if (!isr)
    400  1.1       tnn 		return 0;
    401  1.1       tnn 
    402  1.1       tnn 	if (ISSET(isr, SPI_INT_STA_RO)) {
    403  1.1       tnn 		device_printf(sc->sc_dev, "RXFIFO overflow\n");
    404  1.1       tnn 	}
    405  1.1       tnn 	if (ISSET(isr, SPI_INT_STA_TU)) {
    406  1.1       tnn 		device_printf(sc->sc_dev, "TXFIFO underrun\n");
    407  1.1       tnn 	}
    408  1.1       tnn 
    409  1.1       tnn 	sun4ispi_rxtx(sc);
    410  1.1       tnn 
    411  1.1       tnn 	if (ISSET(isr, SPI_INT_STA_TC)) {
    412  1.1       tnn 		SPIREG_WRITE(sc, SPI_INTCTL, 0);
    413  1.1       tnn 		KASSERT(sc->sc_rchunk == NULL);
    414  1.1       tnn 		KASSERT(sc->sc_wchunk == NULL);
    415  1.1       tnn 		st = sc->sc_transfer;
    416  1.1       tnn 		sc->sc_transfer = NULL;
    417  1.1       tnn 		KASSERT(st != NULL);
    418  1.1       tnn 		spi_done(st, 0);
    419  1.1       tnn 		sc->sc_running = false;
    420  1.1       tnn 	} else {
    421  1.1       tnn 		sun4ispi_set_interrupt_mask(sc);
    422  1.1       tnn 	}
    423  1.1       tnn 	SPIREG_WRITE(sc, SPI_INT_STA, isr);
    424  1.1       tnn 
    425  1.1       tnn 	return 1;
    426  1.1       tnn }
    427