Home | History | Annotate | Line # | Download | only in at91
at91spi.c revision 1.6.2.1
      1 /*	$Id: at91spi.c,v 1.6.2.1 2021/05/18 23:30:55 thorpej Exp $	*/
      2 /*	$NetBSD: at91spi.c,v 1.6.2.1 2021/05/18 23:30:55 thorpej Exp $	*/
      3 
      4 /*-
      5  * Copyright (c) 2007 Embedtronics Oy. All rights reserved.
      6  *
      7  * Based on arch/mips/alchemy/dev/auspi.c,
      8  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      9  * Copyright (c) 2006 Garrett D'Amore.
     10  * All rights reserved.
     11  *
     12  * Portions of this code were written by Garrett D'Amore for the
     13  * Champaign-Urbana Community Wireless Network Project.
     14  *
     15  * Redistribution and use in source and binary forms, with or
     16  * without modification, are permitted provided that the following
     17  * conditions are met:
     18  * 1. Redistributions of source code must retain the above copyright
     19  *    notice, this list of conditions and the following disclaimer.
     20  * 2. Redistributions in binary form must reproduce the above
     21  *    copyright notice, this list of conditions and the following
     22  *    disclaimer in the documentation and/or other materials provided
     23  *    with the distribution.
     24  * 3. All advertising materials mentioning features or use of this
     25  *    software must display the following acknowledgements:
     26  *      This product includes software developed by the Urbana-Champaign
     27  *      Independent Media Center.
     28  *	This product includes software developed by Garrett D'Amore.
     29  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     30  *    D'Amore's name may not be used to endorse or promote products
     31  *    derived from this software without specific prior written permission.
     32  *
     33  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     34  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     35  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     36  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     37  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     38  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     39  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     40  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     41  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     42  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     43  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     44  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     45  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: at91spi.c,v 1.6.2.1 2021/05/18 23:30:55 thorpej Exp $");
     50 
     51 #include "locators.h"
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/kernel.h>
     56 #include <sys/device.h>
     57 #include <sys/errno.h>
     58 #include <sys/proc.h>
     59 
     60 #include <sys/bus.h>
     61 #include <machine/cpu.h>
     62 #include <machine/vmparam.h>
     63 #include <sys/inttypes.h>
     64 
     65 #include <arm/at91/at91var.h>
     66 #include <arm/at91/at91reg.h>
     67 #include <arm/at91/at91spivar.h>
     68 #include <arm/at91/at91spireg.h>
     69 
     70 #define	at91spi_select(sc, slave)	\
     71 	(sc)->sc_md->select_slave((sc), (slave))
     72 
     73 #define	STATIC
     74 
     75 //#define	AT91SPI_DEBUG	4
     76 
     77 #ifdef	AT91SPI_DEBUG
     78 int at91spi_debug = AT91SPI_DEBUG;
     79 #define	DPRINTFN(n,x)	if (at91spi_debug>(n)) printf x;
     80 #else
     81 #define	DPRINTFN(n,x)
     82 #endif
     83 
     84 STATIC int at91spi_intr(void *);
     85 
     86 /* SPI service routines */
     87 STATIC int at91spi_configure(void *, int, int, int);
     88 STATIC int at91spi_transfer(void *, struct spi_transfer *);
     89 STATIC void at91spi_xfer(struct at91spi_softc *sc, int start);
     90 
     91 /* internal stuff */
     92 STATIC void at91spi_done(struct at91spi_softc *, int);
     93 STATIC void at91spi_send(struct at91spi_softc *);
     94 STATIC void at91spi_recv(struct at91spi_softc *);
     95 STATIC void at91spi_sched(struct at91spi_softc *);
     96 
     97 #define	GETREG(sc, x)					\
     98 	bus_space_read_4(sc->sc_iot, sc->sc_ioh, x)
     99 #define	PUTREG(sc, x, v)				\
    100 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, x, v)
    101 
    102 void
    103 at91spi_attach_common(device_t parent, device_t self, void *aux,
    104 		      at91spi_machdep_tag_t md)
    105 {
    106 	struct at91spi_softc *sc = device_private(self);
    107 	struct at91bus_attach_args *sa = aux;
    108 	bus_dma_segment_t segs;
    109 	int rsegs, err;
    110 
    111 	aprint_normal(": AT91 SPI Controller\n");
    112 
    113 	sc->sc_dev = self;
    114 	sc->sc_iot = sa->sa_iot;
    115 	sc->sc_pid = sa->sa_pid;
    116 	sc->sc_dmat = sa->sa_dmat;
    117 	sc->sc_md = md;
    118 
    119 	if (bus_space_map(sa->sa_iot, sa->sa_addr, sa->sa_size, 0, &sc->sc_ioh))
    120 		panic("%s: Cannot map registers", device_xname(self));
    121 
    122 	/* we want to use dma, so allocate dma memory: */
    123 	err = bus_dmamem_alloc(sc->sc_dmat, PAGE_SIZE, 0, PAGE_SIZE,
    124 			       &segs, 1, &rsegs, BUS_DMA_WAITOK);
    125 	if (err == 0) {
    126 		err = bus_dmamem_map(sc->sc_dmat, &segs, 1, PAGE_SIZE,
    127 				     &sc->sc_dmapage,
    128 				     BUS_DMA_WAITOK);
    129 	}
    130 	if (err == 0) {
    131 		err = bus_dmamap_create(sc->sc_dmat, PAGE_SIZE, 1,
    132 					 PAGE_SIZE, 0, BUS_DMA_WAITOK,
    133 					 &sc->sc_dmamap);
    134 	}
    135 	if (err == 0) {
    136 		err = bus_dmamap_load(sc->sc_dmat, sc->sc_dmamap,
    137 				      sc->sc_dmapage, PAGE_SIZE, NULL,
    138 				      BUS_DMA_WAITOK);
    139 	}
    140 	if (err != 0) {
    141 		panic("%s: Cannot get DMA memory", device_xname(sc->sc_dev));
    142 	}
    143 	sc->sc_dmaaddr = sc->sc_dmamap->dm_segs[0].ds_addr;
    144 
    145 	/*
    146 	 * Initialize SPI controller
    147 	 */
    148 	sc->sc_spi.sct_cookie = sc;
    149 	sc->sc_spi.sct_configure = at91spi_configure;
    150 	sc->sc_spi.sct_transfer = at91spi_transfer;
    151 
    152 	//sc->sc_spi.sct_nslaves must have been initialized by machdep code
    153 	if (!sc->sc_spi.sct_nslaves) {
    154 		aprint_error("%s: no slaves!\n", device_xname(sc->sc_dev));
    155 	}
    156 
    157 	/* initialize the queue */
    158 	SIMPLEQ_INIT(&sc->sc_q);
    159 
    160 	/* reset the SPI */
    161 	at91_peripheral_clock(sc->sc_pid, 1);
    162 	PUTREG(sc, SPI_CR, SPI_CR_SWRST);
    163 	delay(100);
    164 
    165 	/* be paranoid and make sure the PDC is dead */
    166 	PUTREG(sc, SPI_PDC_BASE + PDC_PTCR, PDC_PTCR_TXTDIS | PDC_PTCR_RXTDIS);
    167 	PUTREG(sc, SPI_PDC_BASE + PDC_RNCR, 0);
    168 	PUTREG(sc, SPI_PDC_BASE + PDC_RCR, 0);
    169 	PUTREG(sc, SPI_PDC_BASE + PDC_TNCR, 0);
    170 	PUTREG(sc, SPI_PDC_BASE + PDC_TCR, 0);
    171 
    172 	// configure SPI:
    173 	PUTREG(sc, SPI_IDR, -1);
    174 	PUTREG(sc, SPI_CSR(0), SPI_CSR_SCBR | SPI_CSR_BITS_8);
    175 	PUTREG(sc, SPI_CSR(1), SPI_CSR_SCBR | SPI_CSR_BITS_8);
    176 	PUTREG(sc, SPI_CSR(2), SPI_CSR_SCBR | SPI_CSR_BITS_8);
    177 	PUTREG(sc, SPI_CSR(3), SPI_CSR_SCBR | SPI_CSR_BITS_8);
    178 	PUTREG(sc, SPI_MR, SPI_MR_MODFDIS/* <- machdep? */ | SPI_MR_MSTR);
    179 
    180 	/* enable device interrupts */
    181 	sc->sc_ih = at91_intr_establish(sc->sc_pid, IPL_BIO, INTR_HIGH_LEVEL,
    182 					at91spi_intr, sc);
    183 
    184 	/* enable SPI */
    185 	PUTREG(sc, SPI_CR, SPI_CR_SPIEN);
    186 	if (GETREG(sc, SPI_SR) & SPI_SR_RDRF)
    187 		(void)GETREG(sc, SPI_RDR);
    188 
    189 	PUTREG(sc, SPI_PDC_BASE + PDC_PTCR, PDC_PTCR_TXTEN | PDC_PTCR_RXTEN);
    190 
    191 	/* attach slave devices */
    192 	struct spibus_attach_args sba = {
    193 		.sba_controller = &sc->sc_spi,
    194 	};
    195 	config_found(sc->sc_dev, &sba, spibus_print,
    196 	    CFARG_DEVHANDLE, device_handle(sc->sc_dev),
    197 	    CFARG_EOL);
    198 }
    199 
    200 int
    201 at91spi_configure(void *arg, int slave, int mode, int speed)
    202 {
    203 	struct at91spi_softc *sc = arg;
    204 	uint		scbr;
    205 	uint32_t	csr;
    206 
    207 	/* setup interrupt registers */
    208 	PUTREG(sc, SPI_IDR, -1);	/* disable interrupts for now	*/
    209 
    210 	csr = GETREG(sc, SPI_CSR(0));	/* read register		*/
    211 	csr &= SPI_CSR_RESERVED;	/* keep reserved bits		*/
    212 	csr |= SPI_CSR_BITS_8;		/* assume 8 bit transfers	*/
    213 
    214 	/*
    215 	 * Calculate clock divider
    216 	 */
    217 	scbr = speed ? ((AT91_MSTCLK + speed - 1) / speed + 1) & ~1 : -1;
    218 	if (scbr > 0xFF) {
    219 		aprint_error("%s: speed %d not supported\n",
    220 		    device_xname(sc->sc_dev), speed);
    221 		return EINVAL;
    222 	}
    223 	csr |= scbr << SPI_CSR_SCBR_SHIFT;
    224 
    225 	/*
    226 	 * I'm not entirely confident that these values are correct.
    227 	 * But at least mode 0 appears to work properly with the
    228 	 * devices I have tested.  The documentation seems to suggest
    229 	 * that I have the meaning of the clock delay bit inverted.
    230 	 */
    231 	switch (mode) {
    232 	case SPI_MODE_0:
    233 		csr |= SPI_CSR_NCPHA;		/* CPHA = 0, CPOL = 0 */
    234 		break;
    235 	case SPI_MODE_1:
    236 		csr |= 0;			/* CPHA = 1, CPOL = 0 */
    237 		break;
    238 	case SPI_MODE_2:
    239 		csr |= SPI_CSR_NCPHA		/* CPHA = 0, CPOL = 1 */
    240 		       | SPI_CSR_CPOL;
    241 		break;
    242 	case SPI_MODE_3:
    243 		csr |= SPI_CSR_CPOL;		/* CPHA = 1, CPOL = 1 */
    244 		break;
    245 	default:
    246 		return EINVAL;
    247 	}
    248 
    249 	PUTREG(sc, SPI_CSR(0), csr);
    250 
    251 	DPRINTFN(3, ("%s: slave %d mode %d speed %d, csr=0x%08"PRIX32"\n",
    252 		     __FUNCTION__, slave, mode, speed, csr));
    253 
    254 #if 0
    255 	// wait until ready!?
    256 	for (i = 1000000; i; i -= 10) {
    257 		if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DR) {
    258 			return 0;
    259 		}
    260 	}
    261 
    262 	return ETIMEDOUT;
    263 #else
    264 	return 0;
    265 #endif
    266 }
    267 
    268 #define	HALF_BUF_SIZE	(PAGE_SIZE / 2)
    269 
    270 void
    271 at91spi_xfer(struct at91spi_softc *sc, int start)
    272 {
    273 	struct spi_chunk	*chunk;
    274 	int			len;
    275 	uint32_t		sr;
    276 
    277 	DPRINTFN(3, ("%s: sc=%p start=%d\n", __FUNCTION__, sc, start));
    278 
    279 	/* so ready to transmit more / anything received? */
    280 	if (((sr = GETREG(sc, SPI_SR)) & (SPI_SR_ENDTX | SPI_SR_ENDRX)) != (SPI_SR_ENDTX | SPI_SR_ENDRX)) {
    281 		/* not ready, get out */
    282 		DPRINTFN(3, ("%s: sc=%p start=%d sr=%"PRIX32"\n", __FUNCTION__, sc, start, sr));
    283 		return;
    284 	}
    285 
    286 	DPRINTFN(3, ("%s: sr=%"PRIX32"\n", __FUNCTION__, sr));
    287 
    288 	if (!start) {
    289 		// ok, something has been transferred, synchronize..
    290 		int offs = sc->sc_dmaoffs ^ HALF_BUF_SIZE;
    291 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, offs, HALF_BUF_SIZE,
    292 				BUS_DMASYNC_POSTWRITE | BUS_DMASYNC_POSTREAD);
    293 
    294 		if ((chunk = sc->sc_rchunk) != NULL) {
    295 			if ((len = chunk->chunk_rresid) > HALF_BUF_SIZE)
    296 				len = HALF_BUF_SIZE;
    297 			if (chunk->chunk_rptr && len > 0) {
    298 				memcpy(chunk->chunk_rptr, (const uint8_t *)sc->sc_dmapage + offs, len);
    299 				chunk->chunk_rptr += len;
    300 			}
    301 			if ((chunk->chunk_rresid -= len) <= 0) {
    302 				// done with this chunk, get next
    303 				sc->sc_rchunk = chunk->chunk_next;
    304 			}
    305 		}
    306 	}
    307 
    308 	/* start transmitting next chunk: */
    309 	if ((chunk = sc->sc_wchunk) != NULL) {
    310 
    311 		/* make sure we transmit just half buffer at a time */
    312 		len = MIN(chunk->chunk_wresid, HALF_BUF_SIZE);
    313 
    314 		// setup outgoing data
    315 		if (chunk->chunk_wptr && len > 0) {
    316 			memcpy((uint8_t *)sc->sc_dmapage + sc->sc_dmaoffs, chunk->chunk_wptr, len);
    317 			chunk->chunk_wptr += len;
    318 		} else {
    319 			memset((uint8_t *)sc->sc_dmapage + sc->sc_dmaoffs, 0, len);
    320 		}
    321 
    322 		/* advance to next transfer if it's time to */
    323 		if ((chunk->chunk_wresid -= len) <= 0) {
    324 			sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    325 		}
    326 
    327 		/* determine which interrupt to get */
    328 		if (sc->sc_wchunk) {
    329 			/* just wait for next buffer to free */
    330 			PUTREG(sc, SPI_IER, SPI_SR_ENDRX);
    331 		} else {
    332 			/* must wait until transfer has completed */
    333 			PUTREG(sc, SPI_IDR, SPI_SR_ENDRX);
    334 			PUTREG(sc, SPI_IER, SPI_SR_RXBUFF);
    335 		}
    336 
    337 		DPRINTFN(3, ("%s: dmaoffs=%d len=%d wchunk=%p (%p:%d) rchunk=%p (%p:%d) mr=%"PRIX32" sr=%"PRIX32" imr=%"PRIX32" csr0=%"PRIX32"\n",
    338 			     __FUNCTION__, sc->sc_dmaoffs, len, sc->sc_wchunk,
    339 			     sc->sc_wchunk ? sc->sc_wchunk->chunk_wptr : NULL,
    340 			     sc->sc_wchunk ? sc->sc_wchunk->chunk_wresid : -1,
    341 			     sc->sc_rchunk,
    342 			     sc->sc_rchunk ? sc->sc_rchunk->chunk_rptr : NULL,
    343 			     sc->sc_rchunk ? sc->sc_rchunk->chunk_rresid : -1,
    344 			     GETREG(sc, SPI_MR), GETREG(sc, SPI_SR),
    345 			     GETREG(sc, SPI_IMR), GETREG(sc, SPI_CSR(0))));
    346 
    347 		// prepare DMA
    348 		bus_dmamap_sync(sc->sc_dmat, sc->sc_dmamap, sc->sc_dmaoffs, len,
    349 				BUS_DMASYNC_PREWRITE | BUS_DMASYNC_PREREAD);
    350 
    351 		// and start transmitting / receiving
    352 		PUTREG(sc, SPI_PDC_BASE + PDC_RNPR, sc->sc_dmaaddr + sc->sc_dmaoffs);
    353 		PUTREG(sc, SPI_PDC_BASE + PDC_RNCR, len);
    354 		PUTREG(sc, SPI_PDC_BASE + PDC_TNPR, sc->sc_dmaaddr + sc->sc_dmaoffs);
    355 		PUTREG(sc, SPI_PDC_BASE + PDC_TNCR, len);
    356 
    357 		// swap buffer
    358 		sc->sc_dmaoffs ^= HALF_BUF_SIZE;
    359 
    360 		// get out
    361 		return;
    362 	} else {
    363 		DPRINTFN(3, ("%s: nothing to write anymore\n", __FUNCTION__));
    364 		return;
    365 	}
    366 }
    367 
    368 void
    369 at91spi_sched(struct at91spi_softc *sc)
    370 {
    371 	struct spi_transfer	*st;
    372 	int			err;
    373 
    374 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    375 
    376 		DPRINTFN(2, ("%s: st=%p\n", __FUNCTION__, st));
    377 
    378 		/* remove the item */
    379 		spi_transq_dequeue(&sc->sc_q);
    380 
    381 		/* note that we are working on it */
    382 		sc->sc_transfer = st;
    383 
    384 		if ((err = at91spi_select(sc, st->st_slave)) != 0) {
    385 			spi_done(st, err);
    386 			continue;
    387 		}
    388 
    389 		/* setup chunks */
    390 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    391 
    392 		/* now kick the master start to get the chip running */
    393 		at91spi_xfer(sc, TRUE);
    394 
    395 		/* enable error interrupts too: */
    396 		PUTREG(sc, SPI_IER, SPI_SR_MODF | SPI_SR_OVRES);
    397 
    398 		sc->sc_running = TRUE;
    399 		return;
    400 	}
    401 	DPRINTFN(2, ("%s: nothing to do anymore\n", __FUNCTION__));
    402 	PUTREG(sc, SPI_IDR, -1);	/* disable interrupts */
    403 	at91spi_select(sc, -1);
    404 	sc->sc_running = FALSE;
    405 }
    406 
    407 void
    408 at91spi_done(struct at91spi_softc *sc, int err)
    409 {
    410 	struct spi_transfer	*st;
    411 
    412 	/* called from interrupt handler */
    413 	if ((st = sc->sc_transfer) != NULL) {
    414 		sc->sc_transfer = NULL;
    415 		DPRINTFN(2, ("%s: st %p finished with error code %d\n", __FUNCTION__, st, err));
    416 		spi_done(st, err);
    417 	}
    418 	/* make sure we clear these bits out */
    419 	sc->sc_wchunk = sc->sc_rchunk = NULL;
    420 	at91spi_sched(sc);
    421 }
    422 
    423 int
    424 at91spi_intr(void *arg)
    425 {
    426 	struct at91spi_softc	*sc = arg;
    427 	uint32_t		imr, sr;
    428 	int			err = 0;
    429 
    430 	if ((imr = GETREG(sc, SPI_IMR)) == 0) {
    431 		/* interrupts are not enabled, get out */
    432 		DPRINTFN(4, ("%s: interrupts are not enabled\n", __FUNCTION__));
    433 		return 0;
    434 	}
    435 
    436 	sr = GETREG(sc, SPI_SR);
    437 	if (!(sr & imr)) {
    438 		/* interrupt did not happen, get out */
    439 		DPRINTFN(3, ("%s: interrupts are not enabled, sr=%08"PRIX32" imr=%08"PRIX32"\n",
    440 			     __FUNCTION__, sr, imr));
    441 		return 0;
    442 	}
    443 
    444 	DPRINTFN(3, ("%s: sr=%08"PRIX32" imr=%08"PRIX32"\n",
    445 		     __FUNCTION__, sr, imr));
    446 
    447 	if (sr & imr & SPI_SR_MODF) {
    448 		printf("%s: mode fault!\n", device_xname(sc->sc_dev));
    449 		err = EIO;
    450 	}
    451 
    452 	if (sr & imr & SPI_SR_OVRES) {
    453 		printf("%s: overrun error!\n", device_xname(sc->sc_dev));
    454 		err = EIO;
    455 	}
    456 	if (err) {
    457 		/* clear errors */
    458 		/* complete transfer */
    459 		at91spi_done(sc, err);
    460 	} else {
    461 		/* do all data exchanges */
    462 		at91spi_xfer(sc, FALSE);
    463 
    464 		/*
    465 		 * if the master done bit is set, make sure we do the
    466 		 * right processing.
    467 		 */
    468 		if (sr & imr & SPI_SR_RXBUFF) {
    469 			if ((sc->sc_wchunk != NULL) ||
    470 			    (sc->sc_rchunk != NULL)) {
    471 				printf("%s: partial transfer?\n",
    472 				    device_xname(sc->sc_dev));
    473 				err = EIO;
    474 			}
    475 			at91spi_done(sc, err);
    476 		}
    477 
    478 	}
    479 
    480 	return 1;
    481 }
    482 
    483 int
    484 at91spi_transfer(void *arg, struct spi_transfer *st)
    485 {
    486 	struct at91spi_softc	*sc = arg;
    487 	int			s;
    488 
    489 	/* make sure we select the right chip */
    490 	s = splbio();
    491 	spi_transq_enqueue(&sc->sc_q, st);
    492 	if (sc->sc_running == 0) {
    493 		at91spi_sched(sc);
    494 	}
    495 	splx(s);
    496 	return 0;
    497 }
    498 
    499