Home | History | Annotate | Line # | Download | only in sunxi
sun6i_spi.c revision 1.1
      1  1.1  jakllsch /*	$NetBSD: sun6i_spi.c,v 1.1 2018/01/31 16:24:11 jakllsch Exp $	*/
      2  1.1  jakllsch 
      3  1.1  jakllsch /*
      4  1.1  jakllsch  * Copyright (c) 2018 Jonathan A. Kollasch
      5  1.1  jakllsch  * All rights reserved.
      6  1.1  jakllsch  *
      7  1.1  jakllsch  * Redistribution and use in source and binary forms, with or without
      8  1.1  jakllsch  * modification, are permitted provided that the following conditions
      9  1.1  jakllsch  * are met:
     10  1.1  jakllsch  * 1. Redistributions of source code must retain the above copyright
     11  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer.
     12  1.1  jakllsch  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1  jakllsch  *    notice, this list of conditions and the following disclaimer in the
     14  1.1  jakllsch  *    documentation and/or other materials provided with the distribution.
     15  1.1  jakllsch  *
     16  1.1  jakllsch  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     17  1.1  jakllsch  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     18  1.1  jakllsch  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     19  1.1  jakllsch  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     20  1.1  jakllsch  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
     21  1.1  jakllsch  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     22  1.1  jakllsch  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     23  1.1  jakllsch  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     24  1.1  jakllsch  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     25  1.1  jakllsch  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     26  1.1  jakllsch  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  jakllsch  */
     28  1.1  jakllsch 
     29  1.1  jakllsch #include <sys/cdefs.h>
     30  1.1  jakllsch __KERNEL_RCSID(0, "$NetBSD: sun6i_spi.c,v 1.1 2018/01/31 16:24:11 jakllsch Exp $");
     31  1.1  jakllsch 
     32  1.1  jakllsch #include <sys/param.h>
     33  1.1  jakllsch #include <sys/device.h>
     34  1.1  jakllsch #include <sys/systm.h>
     35  1.1  jakllsch #include <sys/mutex.h>
     36  1.1  jakllsch #include <sys/bus.h>
     37  1.1  jakllsch #include <sys/intr.h>
     38  1.1  jakllsch #include <sys/kernel.h>
     39  1.1  jakllsch 
     40  1.1  jakllsch #include <sys/bitops.h>
     41  1.1  jakllsch #include <dev/spi/spivar.h>
     42  1.1  jakllsch 
     43  1.1  jakllsch #include <arm/sunxi/sun6i_spireg.h>
     44  1.1  jakllsch 
     45  1.1  jakllsch #include <dev/fdt/fdtvar.h>
     46  1.1  jakllsch 
     47  1.1  jakllsch #include <arm/fdt/arm_fdtvar.h>
     48  1.1  jakllsch 
     49  1.1  jakllsch #define SPI_IER_DEFAULT (SPI_IER_TC_INT_EN | SPI_IER_TF_UDR_INT_EN | \
     50  1.1  jakllsch 	SPI_IER_TF_OVF_INT_EN | SPI_IER_RF_UDR_INT_EN | SPI_IER_RF_OVF_INT_EN)
     51  1.1  jakllsch 
     52  1.1  jakllsch struct sun6ispi_softc {
     53  1.1  jakllsch 	device_t		sc_dev;
     54  1.1  jakllsch 	bus_space_tag_t		sc_iot;
     55  1.1  jakllsch 	bus_space_handle_t	sc_ioh;
     56  1.1  jakllsch 	void			*sc_intrh;
     57  1.1  jakllsch 	struct spi_controller	sc_spi;
     58  1.1  jakllsch 	SIMPLEQ_HEAD(,spi_transfer) sc_q;
     59  1.1  jakllsch 	struct spi_transfer	*sc_transfer;
     60  1.1  jakllsch 	struct spi_chunk	*sc_wchunk;
     61  1.1  jakllsch 	struct spi_chunk	*sc_rchunk;
     62  1.1  jakllsch 	uint32_t		sc_TCR;
     63  1.1  jakllsch 	u_int			sc_modclkrate;
     64  1.1  jakllsch 	volatile bool		sc_running;
     65  1.1  jakllsch };
     66  1.1  jakllsch 
     67  1.1  jakllsch static int sun6ispi_match(device_t, cfdata_t, void *);
     68  1.1  jakllsch static void sun6ispi_attach(device_t, device_t, void *);
     69  1.1  jakllsch 
     70  1.1  jakllsch static int sun6ispi_configure(void *, int, int, int);
     71  1.1  jakllsch static int sun6ispi_transfer(void *, struct spi_transfer *);
     72  1.1  jakllsch 
     73  1.1  jakllsch static void sun6ispi_start(struct sun6ispi_softc * const);
     74  1.1  jakllsch static int sun6ispi_intr(void *);
     75  1.1  jakllsch 
     76  1.1  jakllsch static void sun6ispi_send(struct sun6ispi_softc * const);
     77  1.1  jakllsch static void sun6ispi_recv(struct sun6ispi_softc * const);
     78  1.1  jakllsch 
     79  1.1  jakllsch CFATTACH_DECL_NEW(sun6i_spi, sizeof(struct sun6ispi_softc),
     80  1.1  jakllsch     sun6ispi_match, sun6ispi_attach, NULL, NULL);
     81  1.1  jakllsch 
     82  1.1  jakllsch static int
     83  1.1  jakllsch sun6ispi_match(device_t parent, cfdata_t cf, void *aux)
     84  1.1  jakllsch {
     85  1.1  jakllsch 	const char * const compatible[] = {
     86  1.1  jakllsch 		"allwinner,sun8i-h3-spi",
     87  1.1  jakllsch 		NULL
     88  1.1  jakllsch 	};
     89  1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
     90  1.1  jakllsch 
     91  1.1  jakllsch 	return of_match_compatible(faa->faa_phandle, compatible);
     92  1.1  jakllsch }
     93  1.1  jakllsch 
     94  1.1  jakllsch static void
     95  1.1  jakllsch sun6ispi_attach(device_t parent, device_t self, void *aux)
     96  1.1  jakllsch {
     97  1.1  jakllsch 	struct sun6ispi_softc * const sc = device_private(self);
     98  1.1  jakllsch 	struct fdt_attach_args * const faa = aux;
     99  1.1  jakllsch 	struct spibus_attach_args sba;
    100  1.1  jakllsch 	struct fdtbus_reset *rst;
    101  1.1  jakllsch 	struct clk *clk, *modclk;
    102  1.1  jakllsch 	uint32_t gcr, isr;
    103  1.1  jakllsch 	char intrstr[128];
    104  1.1  jakllsch 
    105  1.1  jakllsch 	aprint_naive("\n");
    106  1.1  jakllsch 	aprint_normal(": SPI\n");
    107  1.1  jakllsch 
    108  1.1  jakllsch 	sc->sc_dev = self;
    109  1.1  jakllsch 	sc->sc_iot = faa->faa_bst;
    110  1.1  jakllsch 	SIMPLEQ_INIT(&sc->sc_q);
    111  1.1  jakllsch 
    112  1.1  jakllsch 	const int phandle = faa->faa_phandle;
    113  1.1  jakllsch 	bus_addr_t addr;
    114  1.1  jakllsch 	bus_size_t size;
    115  1.1  jakllsch 
    116  1.1  jakllsch 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    117  1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "missing 'reg' property\n");
    118  1.1  jakllsch 		return;
    119  1.1  jakllsch 	}
    120  1.1  jakllsch 
    121  1.1  jakllsch 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
    122  1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    123  1.1  jakllsch 		return;
    124  1.1  jakllsch 	}
    125  1.1  jakllsch 
    126  1.1  jakllsch 	if ((clk = fdtbus_clock_get_index(phandle, 0)) != NULL) {
    127  1.1  jakllsch 		if (clk_enable(clk) != 0) {
    128  1.1  jakllsch 			aprint_error_dev(sc->sc_dev, "couldn't enable clock\n");
    129  1.1  jakllsch 			return;
    130  1.1  jakllsch 		}
    131  1.1  jakllsch 		device_printf(self, "%s ahb @ %uHz\n", __func__, clk_get_rate(clk));
    132  1.1  jakllsch 	}
    133  1.1  jakllsch 
    134  1.1  jakllsch 	if ((modclk = fdtbus_clock_get(phandle, "mod")) != NULL) {
    135  1.1  jakllsch 		/* 200MHz max on H3,H5 */
    136  1.1  jakllsch 		if (clk_set_rate(modclk, 200000000) != 0) {
    137  1.1  jakllsch 			aprint_error_dev(sc->sc_dev, "couldn't configure module clock\n");
    138  1.1  jakllsch 			return;
    139  1.1  jakllsch 		}
    140  1.1  jakllsch 		if (clk_enable(modclk) != 0) {
    141  1.1  jakllsch 			aprint_error_dev(sc->sc_dev, "couldn't enable module clock\n");
    142  1.1  jakllsch 			return;
    143  1.1  jakllsch 		}
    144  1.1  jakllsch 		sc->sc_modclkrate = clk_get_rate(modclk);
    145  1.1  jakllsch 		device_printf(self, "%s %uHz\n", __func__, sc->sc_modclkrate);
    146  1.1  jakllsch 	}
    147  1.1  jakllsch 
    148  1.1  jakllsch 	if ((rst = fdtbus_reset_get_index(phandle, 0)) != NULL)
    149  1.1  jakllsch 		if (fdtbus_reset_deassert(rst) != 0) {
    150  1.1  jakllsch 			aprint_error(": couldn't de-assert reset\n");
    151  1.1  jakllsch 			return;
    152  1.1  jakllsch 		}
    153  1.1  jakllsch 
    154  1.1  jakllsch 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA);
    155  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA, isr);
    156  1.1  jakllsch 
    157  1.1  jakllsch 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    158  1.1  jakllsch 		aprint_error(": failed to decode interrupt\n");
    159  1.1  jakllsch 		return;
    160  1.1  jakllsch 	}
    161  1.1  jakllsch 
    162  1.1  jakllsch 	sc->sc_intrh = fdtbus_intr_establish(phandle, 0, IPL_VM, 0,
    163  1.1  jakllsch 	    sun6ispi_intr, sc);
    164  1.1  jakllsch 	if (sc->sc_intrh == NULL) {
    165  1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
    166  1.1  jakllsch 		return;
    167  1.1  jakllsch 	}
    168  1.1  jakllsch 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    169  1.1  jakllsch 
    170  1.1  jakllsch 	gcr = SPI_GCR_SRST;
    171  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_GCR, gcr);
    172  1.1  jakllsch 	do {
    173  1.1  jakllsch 	} while ((bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_GCR) & SPI_GCR_SRST) != 0);
    174  1.1  jakllsch 	gcr = SPI_GCR_TP_EN | SPI_GCR_MODE | SPI_GCR_EN;
    175  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_GCR, gcr);
    176  1.1  jakllsch 
    177  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_IER, SPI_IER_DEFAULT);
    178  1.1  jakllsch 
    179  1.1  jakllsch 	sc->sc_spi.sct_cookie = sc;
    180  1.1  jakllsch 	sc->sc_spi.sct_configure = sun6ispi_configure;
    181  1.1  jakllsch 	sc->sc_spi.sct_transfer = sun6ispi_transfer;
    182  1.1  jakllsch 	sc->sc_spi.sct_nslaves = 4;
    183  1.1  jakllsch 
    184  1.1  jakllsch 	sba.sba_controller = &sc->sc_spi;
    185  1.1  jakllsch 
    186  1.1  jakllsch 	(void) config_found_ia(self, "spibus", &sba, spibus_print);
    187  1.1  jakllsch }
    188  1.1  jakllsch 
    189  1.1  jakllsch static int
    190  1.1  jakllsch sun6ispi_configure(void *cookie, int slave, int mode, int speed)
    191  1.1  jakllsch {
    192  1.1  jakllsch 	struct sun6ispi_softc * const sc = cookie;
    193  1.1  jakllsch 	uint32_t tcr, cctl;
    194  1.1  jakllsch 
    195  1.1  jakllsch #if 0
    196  1.1  jakllsch 	device_printf(sc->sc_dev, "%s slave %d mode %d speed %d\n", __func__, slave, mode, speed);
    197  1.1  jakllsch 
    198  1.1  jakllsch 	if (speed > 200000)
    199  1.1  jakllsch 		speed = 200000;
    200  1.1  jakllsch #endif
    201  1.1  jakllsch 
    202  1.1  jakllsch 	tcr = SPI_TCR_SS_LEVEL | SPI_TCR_SPOL;
    203  1.1  jakllsch 
    204  1.1  jakllsch 	if (slave > 3)
    205  1.1  jakllsch 		return EINVAL;
    206  1.1  jakllsch 
    207  1.1  jakllsch 	if (speed <= 0)
    208  1.1  jakllsch 		return EINVAL;
    209  1.1  jakllsch 
    210  1.1  jakllsch 	switch (mode) {
    211  1.1  jakllsch 	case SPI_MODE_0:
    212  1.1  jakllsch 		tcr |= 0;
    213  1.1  jakllsch 		break;
    214  1.1  jakllsch 	case SPI_MODE_1:
    215  1.1  jakllsch 		tcr |= SPI_TCR_CPHA;
    216  1.1  jakllsch 		break;
    217  1.1  jakllsch 	case SPI_MODE_2:
    218  1.1  jakllsch 		tcr |= SPI_TCR_CPOL;
    219  1.1  jakllsch 		break;
    220  1.1  jakllsch 	case SPI_MODE_3:
    221  1.1  jakllsch 		tcr |= SPI_TCR_CPHA|SPI_TCR_CPOL;
    222  1.1  jakllsch 		break;
    223  1.1  jakllsch 	default:
    224  1.1  jakllsch 		return EINVAL;
    225  1.1  jakllsch 	}
    226  1.1  jakllsch 
    227  1.1  jakllsch 	sc->sc_TCR = tcr;
    228  1.1  jakllsch 
    229  1.1  jakllsch 	if (speed < 3000 || speed > 200000000)
    230  1.1  jakllsch 		return EINVAL;
    231  1.1  jakllsch 
    232  1.1  jakllsch 	if (speed > sc->sc_modclkrate / 2 || speed < sc->sc_modclkrate / 512) {
    233  1.1  jakllsch 		for (cctl = 0; cctl <= __SHIFTOUT_MASK(SPI_CCTL_CDR1); cctl++) {
    234  1.1  jakllsch 			if ((sc->sc_modclkrate / (1<<cctl)) <= speed)
    235  1.1  jakllsch 				goto cdr1_found;
    236  1.1  jakllsch 		}
    237  1.1  jakllsch 		return EINVAL;
    238  1.1  jakllsch cdr1_found:
    239  1.1  jakllsch 		cctl = __SHIFTIN(cctl, SPI_CCTL_CDR1);
    240  1.1  jakllsch 	} else {
    241  1.1  jakllsch 		cctl = howmany(sc->sc_modclkrate, 2 * speed) - 1;
    242  1.1  jakllsch 		cctl = SPI_CCTL_DRS|__SHIFTIN(cctl, SPI_CCTL_CDR2);
    243  1.1  jakllsch 	}
    244  1.1  jakllsch 
    245  1.1  jakllsch 	device_printf(sc->sc_dev, "%s CCTL 0x%08x\n", __func__, cctl);
    246  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CCTL, cctl);
    247  1.1  jakllsch 
    248  1.1  jakllsch 	return 0;
    249  1.1  jakllsch }
    250  1.1  jakllsch 
    251  1.1  jakllsch static int
    252  1.1  jakllsch sun6ispi_transfer(void *cookie, struct spi_transfer *st)
    253  1.1  jakllsch {
    254  1.1  jakllsch 	struct sun6ispi_softc * const sc = cookie;
    255  1.1  jakllsch 	int s;
    256  1.1  jakllsch 
    257  1.1  jakllsch 	s = splbio();
    258  1.1  jakllsch 	spi_transq_enqueue(&sc->sc_q, st);
    259  1.1  jakllsch 	if (sc->sc_running == false) {
    260  1.1  jakllsch 		sun6ispi_start(sc);
    261  1.1  jakllsch 	}
    262  1.1  jakllsch 	splx(s);
    263  1.1  jakllsch 	return 0;
    264  1.1  jakllsch }
    265  1.1  jakllsch 
    266  1.1  jakllsch static size_t
    267  1.1  jakllsch chunks_total_count(struct spi_transfer * st)
    268  1.1  jakllsch {
    269  1.1  jakllsch 	struct spi_chunk *chunk = st->st_chunks;
    270  1.1  jakllsch 	size_t len = 0;
    271  1.1  jakllsch 
    272  1.1  jakllsch 	do {
    273  1.1  jakllsch 		len += chunk->chunk_count;
    274  1.1  jakllsch 	} while ((chunk = chunk->chunk_next) != NULL);
    275  1.1  jakllsch 
    276  1.1  jakllsch 	return len;
    277  1.1  jakllsch }
    278  1.1  jakllsch 
    279  1.1  jakllsch static void
    280  1.1  jakllsch sun6ispi_start(struct sun6ispi_softc * const sc)
    281  1.1  jakllsch {
    282  1.1  jakllsch 	struct spi_transfer *st;
    283  1.1  jakllsch 	uint32_t isr, tcr;
    284  1.1  jakllsch 	size_t ctc;
    285  1.1  jakllsch 
    286  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s\n", __func__);
    287  1.1  jakllsch 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    288  1.1  jakllsch 
    289  1.1  jakllsch 		spi_transq_dequeue(&sc->sc_q);
    290  1.1  jakllsch 
    291  1.1  jakllsch 		KASSERT(sc->sc_transfer == NULL);
    292  1.1  jakllsch 		sc->sc_transfer = st;
    293  1.1  jakllsch 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    294  1.1  jakllsch 		sc->sc_running = true;
    295  1.1  jakllsch 
    296  1.1  jakllsch 		isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA);
    297  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA, isr);
    298  1.1  jakllsch 
    299  1.1  jakllsch 		//printf("%s fcr 0x%08x\n", __func__, bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FCR));
    300  1.1  jakllsch 
    301  1.1  jakllsch 		ctc = chunks_total_count(st);
    302  1.1  jakllsch 		//printf("%s total count %zu\n", __func__, ctc);
    303  1.1  jakllsch 
    304  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_BC, __SHIFTIN(ctc, SPI_BC_MBC));
    305  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_TC, __SHIFTIN(ctc, SPI_TC_MWTC));
    306  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_BCC, __SHIFTIN(ctc, SPI_BCC_STC));
    307  1.1  jakllsch 
    308  1.1  jakllsch 		KASSERT(st->st_slave <= 3);
    309  1.1  jakllsch 		tcr = sc->sc_TCR | __SHIFTIN(st->st_slave, SPI_TCR_SS_SEL);
    310  1.1  jakllsch 
    311  1.1  jakllsch 		//device_printf(sc->sc_dev, "%s before TCR write\n", __func__);
    312  1.1  jakllsch 		//bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_TCR, tcr);
    313  1.1  jakllsch 
    314  1.1  jakllsch 		sun6ispi_send(sc);
    315  1.1  jakllsch 
    316  1.1  jakllsch 		const uint32_t ier = SPI_IER_DEFAULT | SPI_IER_RF_RDY_INT_EN | SPI_IER_TX_ERQ_INT_EN;
    317  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_IER, ier);
    318  1.1  jakllsch 
    319  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_TCR, tcr|SPI_TCR_XCH);
    320  1.1  jakllsch 		//device_printf(sc->sc_dev, "%s after TCR write\n", __func__);
    321  1.1  jakllsch 
    322  1.1  jakllsch 		if (!cold)
    323  1.1  jakllsch 			return;
    324  1.1  jakllsch 
    325  1.1  jakllsch 		//device_printf(sc->sc_dev, "%s cold\n", __func__);
    326  1.1  jakllsch 
    327  1.1  jakllsch 		int s = splbio();
    328  1.1  jakllsch 		for (;;) {
    329  1.1  jakllsch 			sun6ispi_intr(sc);
    330  1.1  jakllsch 			if (ISSET(st->st_flags, SPI_F_DONE))
    331  1.1  jakllsch 				break;
    332  1.1  jakllsch 		}
    333  1.1  jakllsch 		splx(s);
    334  1.1  jakllsch 	}
    335  1.1  jakllsch 
    336  1.1  jakllsch 	sc->sc_running = false;
    337  1.1  jakllsch //	device_printf(sc->sc_dev, "%s finishes\n", __func__);
    338  1.1  jakllsch }
    339  1.1  jakllsch 
    340  1.1  jakllsch static void
    341  1.1  jakllsch sun6ispi_send(struct sun6ispi_softc * const sc)
    342  1.1  jakllsch {
    343  1.1  jakllsch 	uint8_t fd;
    344  1.1  jakllsch 	uint32_t fsr;
    345  1.1  jakllsch 	struct spi_chunk *chunk;
    346  1.1  jakllsch 
    347  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s\n", __func__);
    348  1.1  jakllsch 
    349  1.1  jakllsch 	while ((chunk = sc->sc_wchunk) != NULL) {
    350  1.1  jakllsch 		while (chunk->chunk_wresid) {
    351  1.1  jakllsch 			fsr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FSR);
    352  1.1  jakllsch 			if (__SHIFTOUT(fsr, SPI_FSR_TF_CNT) >= 64) {
    353  1.1  jakllsch 				//printf("%s fsr 0x%08x\n", __func__, fsr);
    354  1.1  jakllsch 				return;
    355  1.1  jakllsch 			}
    356  1.1  jakllsch 			if (chunk->chunk_wptr) {
    357  1.1  jakllsch 				fd = *chunk->chunk_wptr++;
    358  1.1  jakllsch 			} else {
    359  1.1  jakllsch 				fd = '\0';
    360  1.1  jakllsch 			}
    361  1.1  jakllsch 			//printf("%s fd %02x\n", __func__, fd);
    362  1.1  jakllsch 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, SPI_TXD, fd);
    363  1.1  jakllsch 			chunk->chunk_wresid--;
    364  1.1  jakllsch 		}
    365  1.1  jakllsch 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    366  1.1  jakllsch 	}
    367  1.1  jakllsch }
    368  1.1  jakllsch 
    369  1.1  jakllsch static void
    370  1.1  jakllsch sun6ispi_recv(struct sun6ispi_softc * const sc)
    371  1.1  jakllsch {
    372  1.1  jakllsch 	uint8_t fd;
    373  1.1  jakllsch 	uint32_t fsr;
    374  1.1  jakllsch 	struct spi_chunk *chunk;
    375  1.1  jakllsch 
    376  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s\n", __func__);
    377  1.1  jakllsch 
    378  1.1  jakllsch 	while ((chunk = sc->sc_rchunk) != NULL) {
    379  1.1  jakllsch 		while (chunk->chunk_rresid) {
    380  1.1  jakllsch 			fsr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FSR);
    381  1.1  jakllsch 			if (__SHIFTOUT(fsr, SPI_FSR_RF_CNT) == 0) {
    382  1.1  jakllsch 				//printf("%s fsr 0x%08x\n", __func__, fsr);
    383  1.1  jakllsch 				return;
    384  1.1  jakllsch 			}
    385  1.1  jakllsch 			fd = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SPI_RXD);
    386  1.1  jakllsch 			//printf("%s fd %02x\n", __func__, fd);
    387  1.1  jakllsch 			if (chunk->chunk_rptr) {
    388  1.1  jakllsch 				*chunk->chunk_rptr++ = fd;
    389  1.1  jakllsch 			}
    390  1.1  jakllsch 			chunk->chunk_rresid--;
    391  1.1  jakllsch 		}
    392  1.1  jakllsch 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    393  1.1  jakllsch 	}
    394  1.1  jakllsch }
    395  1.1  jakllsch 
    396  1.1  jakllsch static int
    397  1.1  jakllsch sun6ispi_intr(void *cookie)
    398  1.1  jakllsch {
    399  1.1  jakllsch 	struct sun6ispi_softc * const sc = cookie;
    400  1.1  jakllsch 	struct spi_transfer *st;
    401  1.1  jakllsch 	uint32_t isr;
    402  1.1  jakllsch 
    403  1.1  jakllsch 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA);
    404  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA, isr);
    405  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s ISR 0x%08x\n", __func__, isr);
    406  1.1  jakllsch 
    407  1.1  jakllsch 	if (ISSET(isr, SPI_ISR_RX_RDY)) {
    408  1.1  jakllsch 		sun6ispi_recv(sc);
    409  1.1  jakllsch 		sun6ispi_send(sc);
    410  1.1  jakllsch 	}
    411  1.1  jakllsch 
    412  1.1  jakllsch 	if (ISSET(isr, SPI_ISR_TC)) {
    413  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_IER, SPI_IER_DEFAULT);
    414  1.1  jakllsch 
    415  1.1  jakllsch 		sc->sc_rchunk = sc->sc_wchunk = NULL;
    416  1.1  jakllsch 		st = sc->sc_transfer;
    417  1.1  jakllsch 		sc->sc_transfer = NULL;
    418  1.1  jakllsch 		KASSERT(st != NULL);
    419  1.1  jakllsch 		spi_done(st, 0);
    420  1.1  jakllsch 		sc->sc_running = false;
    421  1.1  jakllsch 	}
    422  1.1  jakllsch 
    423  1.1  jakllsch 	return isr;
    424  1.1  jakllsch }
    425