Home | History | Annotate | Line # | Download | only in sunxi
sun6i_spi.c revision 1.2
      1  1.2  jakllsch /*	$NetBSD: sun6i_spi.c,v 1.2 2018/02/01 14:50:36 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.2  jakllsch __KERNEL_RCSID(0, "$NetBSD: sun6i_spi.c,v 1.2 2018/02/01 14:50:36 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.2  jakllsch 	for (u_int i = 0; ; i++) {
    173  1.2  jakllsch 		if (i >= 1000000) {
    174  1.2  jakllsch 			aprint_error_dev(self, "reset timeout\n");
    175  1.2  jakllsch 			return;
    176  1.2  jakllsch 		}
    177  1.2  jakllsch 		gcr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_GCR);
    178  1.2  jakllsch 		if ((gcr & SPI_GCR_SRST) == 0)
    179  1.2  jakllsch 			break;
    180  1.2  jakllsch 		else
    181  1.2  jakllsch 			DELAY(1);
    182  1.2  jakllsch 	}
    183  1.1  jakllsch 	gcr = SPI_GCR_TP_EN | SPI_GCR_MODE | SPI_GCR_EN;
    184  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_GCR, gcr);
    185  1.1  jakllsch 
    186  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_IER, SPI_IER_DEFAULT);
    187  1.1  jakllsch 
    188  1.1  jakllsch 	sc->sc_spi.sct_cookie = sc;
    189  1.1  jakllsch 	sc->sc_spi.sct_configure = sun6ispi_configure;
    190  1.1  jakllsch 	sc->sc_spi.sct_transfer = sun6ispi_transfer;
    191  1.1  jakllsch 	sc->sc_spi.sct_nslaves = 4;
    192  1.1  jakllsch 
    193  1.1  jakllsch 	sba.sba_controller = &sc->sc_spi;
    194  1.1  jakllsch 
    195  1.1  jakllsch 	(void) config_found_ia(self, "spibus", &sba, spibus_print);
    196  1.1  jakllsch }
    197  1.1  jakllsch 
    198  1.1  jakllsch static int
    199  1.1  jakllsch sun6ispi_configure(void *cookie, int slave, int mode, int speed)
    200  1.1  jakllsch {
    201  1.1  jakllsch 	struct sun6ispi_softc * const sc = cookie;
    202  1.1  jakllsch 	uint32_t tcr, cctl;
    203  1.1  jakllsch 
    204  1.1  jakllsch #if 0
    205  1.1  jakllsch 	device_printf(sc->sc_dev, "%s slave %d mode %d speed %d\n", __func__, slave, mode, speed);
    206  1.1  jakllsch 
    207  1.1  jakllsch 	if (speed > 200000)
    208  1.1  jakllsch 		speed = 200000;
    209  1.1  jakllsch #endif
    210  1.1  jakllsch 
    211  1.1  jakllsch 	tcr = SPI_TCR_SS_LEVEL | SPI_TCR_SPOL;
    212  1.1  jakllsch 
    213  1.1  jakllsch 	if (slave > 3)
    214  1.1  jakllsch 		return EINVAL;
    215  1.1  jakllsch 
    216  1.1  jakllsch 	if (speed <= 0)
    217  1.1  jakllsch 		return EINVAL;
    218  1.1  jakllsch 
    219  1.1  jakllsch 	switch (mode) {
    220  1.1  jakllsch 	case SPI_MODE_0:
    221  1.1  jakllsch 		tcr |= 0;
    222  1.1  jakllsch 		break;
    223  1.1  jakllsch 	case SPI_MODE_1:
    224  1.1  jakllsch 		tcr |= SPI_TCR_CPHA;
    225  1.1  jakllsch 		break;
    226  1.1  jakllsch 	case SPI_MODE_2:
    227  1.1  jakllsch 		tcr |= SPI_TCR_CPOL;
    228  1.1  jakllsch 		break;
    229  1.1  jakllsch 	case SPI_MODE_3:
    230  1.1  jakllsch 		tcr |= SPI_TCR_CPHA|SPI_TCR_CPOL;
    231  1.1  jakllsch 		break;
    232  1.1  jakllsch 	default:
    233  1.1  jakllsch 		return EINVAL;
    234  1.1  jakllsch 	}
    235  1.1  jakllsch 
    236  1.1  jakllsch 	sc->sc_TCR = tcr;
    237  1.1  jakllsch 
    238  1.1  jakllsch 	if (speed < 3000 || speed > 200000000)
    239  1.1  jakllsch 		return EINVAL;
    240  1.1  jakllsch 
    241  1.1  jakllsch 	if (speed > sc->sc_modclkrate / 2 || speed < sc->sc_modclkrate / 512) {
    242  1.1  jakllsch 		for (cctl = 0; cctl <= __SHIFTOUT_MASK(SPI_CCTL_CDR1); cctl++) {
    243  1.1  jakllsch 			if ((sc->sc_modclkrate / (1<<cctl)) <= speed)
    244  1.1  jakllsch 				goto cdr1_found;
    245  1.1  jakllsch 		}
    246  1.1  jakllsch 		return EINVAL;
    247  1.1  jakllsch cdr1_found:
    248  1.1  jakllsch 		cctl = __SHIFTIN(cctl, SPI_CCTL_CDR1);
    249  1.1  jakllsch 	} else {
    250  1.1  jakllsch 		cctl = howmany(sc->sc_modclkrate, 2 * speed) - 1;
    251  1.1  jakllsch 		cctl = SPI_CCTL_DRS|__SHIFTIN(cctl, SPI_CCTL_CDR2);
    252  1.1  jakllsch 	}
    253  1.1  jakllsch 
    254  1.1  jakllsch 	device_printf(sc->sc_dev, "%s CCTL 0x%08x\n", __func__, cctl);
    255  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CCTL, cctl);
    256  1.1  jakllsch 
    257  1.1  jakllsch 	return 0;
    258  1.1  jakllsch }
    259  1.1  jakllsch 
    260  1.1  jakllsch static int
    261  1.1  jakllsch sun6ispi_transfer(void *cookie, struct spi_transfer *st)
    262  1.1  jakllsch {
    263  1.1  jakllsch 	struct sun6ispi_softc * const sc = cookie;
    264  1.1  jakllsch 	int s;
    265  1.1  jakllsch 
    266  1.1  jakllsch 	s = splbio();
    267  1.1  jakllsch 	spi_transq_enqueue(&sc->sc_q, st);
    268  1.1  jakllsch 	if (sc->sc_running == false) {
    269  1.1  jakllsch 		sun6ispi_start(sc);
    270  1.1  jakllsch 	}
    271  1.1  jakllsch 	splx(s);
    272  1.1  jakllsch 	return 0;
    273  1.1  jakllsch }
    274  1.1  jakllsch 
    275  1.1  jakllsch static size_t
    276  1.1  jakllsch chunks_total_count(struct spi_transfer * st)
    277  1.1  jakllsch {
    278  1.1  jakllsch 	struct spi_chunk *chunk = st->st_chunks;
    279  1.1  jakllsch 	size_t len = 0;
    280  1.1  jakllsch 
    281  1.1  jakllsch 	do {
    282  1.1  jakllsch 		len += chunk->chunk_count;
    283  1.1  jakllsch 	} while ((chunk = chunk->chunk_next) != NULL);
    284  1.1  jakllsch 
    285  1.1  jakllsch 	return len;
    286  1.1  jakllsch }
    287  1.1  jakllsch 
    288  1.1  jakllsch static void
    289  1.1  jakllsch sun6ispi_start(struct sun6ispi_softc * const sc)
    290  1.1  jakllsch {
    291  1.1  jakllsch 	struct spi_transfer *st;
    292  1.1  jakllsch 	uint32_t isr, tcr;
    293  1.1  jakllsch 	size_t ctc;
    294  1.1  jakllsch 
    295  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s\n", __func__);
    296  1.1  jakllsch 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    297  1.1  jakllsch 
    298  1.1  jakllsch 		spi_transq_dequeue(&sc->sc_q);
    299  1.1  jakllsch 
    300  1.1  jakllsch 		KASSERT(sc->sc_transfer == NULL);
    301  1.1  jakllsch 		sc->sc_transfer = st;
    302  1.1  jakllsch 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    303  1.1  jakllsch 		sc->sc_running = true;
    304  1.1  jakllsch 
    305  1.1  jakllsch 		isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA);
    306  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA, isr);
    307  1.1  jakllsch 
    308  1.1  jakllsch 		//printf("%s fcr 0x%08x\n", __func__, bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FCR));
    309  1.1  jakllsch 
    310  1.1  jakllsch 		ctc = chunks_total_count(st);
    311  1.1  jakllsch 		//printf("%s total count %zu\n", __func__, ctc);
    312  1.1  jakllsch 
    313  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_BC, __SHIFTIN(ctc, SPI_BC_MBC));
    314  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_TC, __SHIFTIN(ctc, SPI_TC_MWTC));
    315  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_BCC, __SHIFTIN(ctc, SPI_BCC_STC));
    316  1.1  jakllsch 
    317  1.1  jakllsch 		KASSERT(st->st_slave <= 3);
    318  1.1  jakllsch 		tcr = sc->sc_TCR | __SHIFTIN(st->st_slave, SPI_TCR_SS_SEL);
    319  1.1  jakllsch 
    320  1.1  jakllsch 		//device_printf(sc->sc_dev, "%s before TCR write\n", __func__);
    321  1.1  jakllsch 		//bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_TCR, tcr);
    322  1.1  jakllsch 
    323  1.1  jakllsch 		sun6ispi_send(sc);
    324  1.1  jakllsch 
    325  1.1  jakllsch 		const uint32_t ier = SPI_IER_DEFAULT | SPI_IER_RF_RDY_INT_EN | SPI_IER_TX_ERQ_INT_EN;
    326  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_IER, ier);
    327  1.1  jakllsch 
    328  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_TCR, tcr|SPI_TCR_XCH);
    329  1.1  jakllsch 		//device_printf(sc->sc_dev, "%s after TCR write\n", __func__);
    330  1.1  jakllsch 
    331  1.1  jakllsch 		if (!cold)
    332  1.1  jakllsch 			return;
    333  1.1  jakllsch 
    334  1.1  jakllsch 		//device_printf(sc->sc_dev, "%s cold\n", __func__);
    335  1.1  jakllsch 
    336  1.1  jakllsch 		int s = splbio();
    337  1.1  jakllsch 		for (;;) {
    338  1.1  jakllsch 			sun6ispi_intr(sc);
    339  1.1  jakllsch 			if (ISSET(st->st_flags, SPI_F_DONE))
    340  1.1  jakllsch 				break;
    341  1.1  jakllsch 		}
    342  1.1  jakllsch 		splx(s);
    343  1.1  jakllsch 	}
    344  1.1  jakllsch 
    345  1.1  jakllsch 	sc->sc_running = false;
    346  1.1  jakllsch //	device_printf(sc->sc_dev, "%s finishes\n", __func__);
    347  1.1  jakllsch }
    348  1.1  jakllsch 
    349  1.1  jakllsch static void
    350  1.1  jakllsch sun6ispi_send(struct sun6ispi_softc * const sc)
    351  1.1  jakllsch {
    352  1.1  jakllsch 	uint8_t fd;
    353  1.1  jakllsch 	uint32_t fsr;
    354  1.1  jakllsch 	struct spi_chunk *chunk;
    355  1.1  jakllsch 
    356  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s\n", __func__);
    357  1.1  jakllsch 
    358  1.1  jakllsch 	while ((chunk = sc->sc_wchunk) != NULL) {
    359  1.1  jakllsch 		while (chunk->chunk_wresid) {
    360  1.1  jakllsch 			fsr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FSR);
    361  1.1  jakllsch 			if (__SHIFTOUT(fsr, SPI_FSR_TF_CNT) >= 64) {
    362  1.1  jakllsch 				//printf("%s fsr 0x%08x\n", __func__, fsr);
    363  1.1  jakllsch 				return;
    364  1.1  jakllsch 			}
    365  1.1  jakllsch 			if (chunk->chunk_wptr) {
    366  1.1  jakllsch 				fd = *chunk->chunk_wptr++;
    367  1.1  jakllsch 			} else {
    368  1.1  jakllsch 				fd = '\0';
    369  1.1  jakllsch 			}
    370  1.1  jakllsch 			//printf("%s fd %02x\n", __func__, fd);
    371  1.1  jakllsch 			bus_space_write_1(sc->sc_iot, sc->sc_ioh, SPI_TXD, fd);
    372  1.1  jakllsch 			chunk->chunk_wresid--;
    373  1.1  jakllsch 		}
    374  1.1  jakllsch 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    375  1.1  jakllsch 	}
    376  1.1  jakllsch }
    377  1.1  jakllsch 
    378  1.1  jakllsch static void
    379  1.1  jakllsch sun6ispi_recv(struct sun6ispi_softc * const sc)
    380  1.1  jakllsch {
    381  1.1  jakllsch 	uint8_t fd;
    382  1.1  jakllsch 	uint32_t fsr;
    383  1.1  jakllsch 	struct spi_chunk *chunk;
    384  1.1  jakllsch 
    385  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s\n", __func__);
    386  1.1  jakllsch 
    387  1.1  jakllsch 	while ((chunk = sc->sc_rchunk) != NULL) {
    388  1.1  jakllsch 		while (chunk->chunk_rresid) {
    389  1.1  jakllsch 			fsr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FSR);
    390  1.1  jakllsch 			if (__SHIFTOUT(fsr, SPI_FSR_RF_CNT) == 0) {
    391  1.1  jakllsch 				//printf("%s fsr 0x%08x\n", __func__, fsr);
    392  1.1  jakllsch 				return;
    393  1.1  jakllsch 			}
    394  1.1  jakllsch 			fd = bus_space_read_1(sc->sc_iot, sc->sc_ioh, SPI_RXD);
    395  1.1  jakllsch 			//printf("%s fd %02x\n", __func__, fd);
    396  1.1  jakllsch 			if (chunk->chunk_rptr) {
    397  1.1  jakllsch 				*chunk->chunk_rptr++ = fd;
    398  1.1  jakllsch 			}
    399  1.1  jakllsch 			chunk->chunk_rresid--;
    400  1.1  jakllsch 		}
    401  1.1  jakllsch 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    402  1.1  jakllsch 	}
    403  1.1  jakllsch }
    404  1.1  jakllsch 
    405  1.1  jakllsch static int
    406  1.1  jakllsch sun6ispi_intr(void *cookie)
    407  1.1  jakllsch {
    408  1.1  jakllsch 	struct sun6ispi_softc * const sc = cookie;
    409  1.1  jakllsch 	struct spi_transfer *st;
    410  1.1  jakllsch 	uint32_t isr;
    411  1.1  jakllsch 
    412  1.1  jakllsch 	isr = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA);
    413  1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_INT_STA, isr);
    414  1.1  jakllsch 	//device_printf(sc->sc_dev, "%s ISR 0x%08x\n", __func__, isr);
    415  1.1  jakllsch 
    416  1.1  jakllsch 	if (ISSET(isr, SPI_ISR_RX_RDY)) {
    417  1.1  jakllsch 		sun6ispi_recv(sc);
    418  1.1  jakllsch 		sun6ispi_send(sc);
    419  1.1  jakllsch 	}
    420  1.1  jakllsch 
    421  1.1  jakllsch 	if (ISSET(isr, SPI_ISR_TC)) {
    422  1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_IER, SPI_IER_DEFAULT);
    423  1.1  jakllsch 
    424  1.1  jakllsch 		sc->sc_rchunk = sc->sc_wchunk = NULL;
    425  1.1  jakllsch 		st = sc->sc_transfer;
    426  1.1  jakllsch 		sc->sc_transfer = NULL;
    427  1.1  jakllsch 		KASSERT(st != NULL);
    428  1.1  jakllsch 		spi_done(st, 0);
    429  1.1  jakllsch 		sc->sc_running = false;
    430  1.1  jakllsch 	}
    431  1.1  jakllsch 
    432  1.1  jakllsch 	return isr;
    433  1.1  jakllsch }
    434