Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_spi.c revision 1.10.2.1
      1  1.10.2.1   thorpej /*	$NetBSD: bcm2835_spi.c,v 1.10.2.1 2021/05/18 23:30:55 thorpej Exp $	*/
      2       1.1  jakllsch 
      3       1.1  jakllsch /*
      4       1.1  jakllsch  * Copyright (c) 2012 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.10.2.1   thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_spi.c,v 1.10.2.1 2021/05/18 23:30:55 thorpej 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/broadcom/bcm2835reg.h>
     44       1.1  jakllsch #include <arm/broadcom/bcm2835_spireg.h>
     45       1.5     skrll 
     46       1.5     skrll #include <dev/fdt/fdtvar.h>
     47       1.5     skrll 
     48       1.5     skrll #include <arm/fdt/arm_fdtvar.h>
     49       1.1  jakllsch 
     50       1.1  jakllsch struct bcmspi_softc {
     51       1.1  jakllsch 	device_t		sc_dev;
     52       1.1  jakllsch 	bus_space_tag_t		sc_iot;
     53       1.1  jakllsch 	bus_space_handle_t	sc_ioh;
     54       1.1  jakllsch 	void			*sc_intrh;
     55       1.1  jakllsch 	struct spi_controller	sc_spi;
     56       1.7    kardel 	kmutex_t                sc_mutex;
     57       1.1  jakllsch 	SIMPLEQ_HEAD(,spi_transfer) sc_q;
     58       1.1  jakllsch 	struct spi_transfer	*sc_transfer;
     59       1.1  jakllsch 	struct spi_chunk	*sc_wchunk;
     60       1.1  jakllsch 	struct spi_chunk	*sc_rchunk;
     61       1.1  jakllsch 	uint32_t		sc_CS;
     62       1.1  jakllsch 	volatile bool		sc_running;
     63       1.1  jakllsch };
     64       1.1  jakllsch 
     65       1.1  jakllsch static int bcmspi_match(device_t, cfdata_t, void *);
     66       1.1  jakllsch static void bcmspi_attach(device_t, device_t, void *);
     67       1.1  jakllsch 
     68       1.1  jakllsch static int bcmspi_configure(void *, int, int, int);
     69       1.1  jakllsch static int bcmspi_transfer(void *, struct spi_transfer *);
     70       1.1  jakllsch 
     71       1.1  jakllsch static void bcmspi_start(struct bcmspi_softc * const);
     72       1.1  jakllsch static int bcmspi_intr(void *);
     73       1.1  jakllsch 
     74       1.1  jakllsch static void bcmspi_send(struct bcmspi_softc * const);
     75       1.1  jakllsch static void bcmspi_recv(struct bcmspi_softc * const);
     76       1.1  jakllsch 
     77       1.1  jakllsch CFATTACH_DECL_NEW(bcmspi, sizeof(struct bcmspi_softc),
     78       1.1  jakllsch     bcmspi_match, bcmspi_attach, NULL, NULL);
     79       1.1  jakllsch 
     80       1.8   thorpej static const struct device_compatible_entry compat_data[] = {
     81       1.8   thorpej 	{ .compat = "brcm,bcm2835-spi" },
     82       1.8   thorpej 	DEVICE_COMPAT_EOL
     83       1.8   thorpej };
     84       1.8   thorpej 
     85       1.1  jakllsch static int
     86       1.1  jakllsch bcmspi_match(device_t parent, cfdata_t cf, void *aux)
     87       1.1  jakllsch {
     88       1.5     skrll 	struct fdt_attach_args * const faa = aux;
     89       1.1  jakllsch 
     90       1.8   thorpej 	return of_compatible_match(faa->faa_phandle, compat_data);
     91       1.1  jakllsch }
     92       1.1  jakllsch 
     93       1.1  jakllsch static void
     94       1.1  jakllsch bcmspi_attach(device_t parent, device_t self, void *aux)
     95       1.1  jakllsch {
     96       1.1  jakllsch 	struct bcmspi_softc * const sc = device_private(self);
     97       1.5     skrll 	struct fdt_attach_args * const faa = aux;
     98       1.1  jakllsch 
     99       1.1  jakllsch 	aprint_naive("\n");
    100       1.1  jakllsch 	aprint_normal(": SPI\n");
    101       1.1  jakllsch 
    102       1.1  jakllsch 	sc->sc_dev = self;
    103       1.5     skrll 	sc->sc_iot = faa->faa_bst;
    104       1.1  jakllsch 	SIMPLEQ_INIT(&sc->sc_q);
    105       1.5     skrll 
    106       1.5     skrll 	const int phandle = faa->faa_phandle;
    107       1.5     skrll 	bus_addr_t addr;
    108       1.5     skrll 	bus_size_t size;
    109       1.5     skrll 
    110       1.5     skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    111       1.5     skrll 		aprint_error(": missing 'reg' property\n");
    112       1.5     skrll 		return;
    113       1.5     skrll 	}
    114       1.5     skrll 
    115       1.5     skrll 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
    116       1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    117       1.1  jakllsch 		return;
    118       1.1  jakllsch 	}
    119       1.1  jakllsch 
    120       1.5     skrll 	char intrstr[128];
    121       1.5     skrll 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    122       1.5     skrll 		aprint_error(": failed to decode interrupt\n");
    123       1.5     skrll 		return;
    124       1.5     skrll 	}
    125       1.1  jakllsch 
    126       1.9     skrll 	sc->sc_intrh = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
    127       1.9     skrll 	    bcmspi_intr, sc, device_xname(self));
    128       1.1  jakllsch 	if (sc->sc_intrh == NULL) {
    129       1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
    130       1.1  jakllsch 		return;
    131       1.1  jakllsch 	}
    132       1.5     skrll 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    133       1.1  jakllsch 
    134       1.1  jakllsch 	sc->sc_spi.sct_cookie = sc;
    135       1.7    kardel 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
    136       1.1  jakllsch 	sc->sc_spi.sct_configure = bcmspi_configure;
    137       1.1  jakllsch 	sc->sc_spi.sct_transfer = bcmspi_transfer;
    138       1.1  jakllsch 	sc->sc_spi.sct_nslaves = 3;
    139       1.1  jakllsch 
    140  1.10.2.1   thorpej 	struct spibus_attach_args sba = {
    141  1.10.2.1   thorpej 		.sba_controller = &sc->sc_spi,
    142  1.10.2.1   thorpej 	};
    143  1.10.2.1   thorpej 	config_found(self, &sba, spibus_print,
    144  1.10.2.1   thorpej 	    CFARG_DEVHANDLE, device_handle(self),
    145  1.10.2.1   thorpej 	    CFARG_EOL);
    146       1.1  jakllsch }
    147       1.1  jakllsch 
    148       1.1  jakllsch static int
    149       1.1  jakllsch bcmspi_configure(void *cookie, int slave, int mode, int speed)
    150       1.1  jakllsch {
    151       1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    152       1.1  jakllsch 	uint32_t cs, clk;
    153       1.1  jakllsch 
    154       1.1  jakllsch 	cs = SPI_CS_INTR | SPI_CS_INTD;
    155       1.1  jakllsch 
    156       1.1  jakllsch 	if (slave > 2)
    157       1.1  jakllsch 		return EINVAL;
    158       1.1  jakllsch 
    159       1.1  jakllsch 	if (speed <= 0)
    160       1.1  jakllsch 		return EINVAL;
    161       1.1  jakllsch 
    162       1.1  jakllsch 	switch (mode) {
    163       1.1  jakllsch 	case SPI_MODE_0:
    164       1.1  jakllsch 		cs |= 0;
    165       1.1  jakllsch 		break;
    166       1.1  jakllsch 	case SPI_MODE_1:
    167       1.1  jakllsch 		cs |= SPI_CS_CPHA;
    168       1.1  jakllsch 		break;
    169       1.1  jakllsch 	case SPI_MODE_2:
    170       1.1  jakllsch 		cs |= SPI_CS_CPOL;
    171       1.1  jakllsch 		break;
    172       1.1  jakllsch 	case SPI_MODE_3:
    173       1.1  jakllsch 		cs |= SPI_CS_CPHA|SPI_CS_CPOL;
    174       1.1  jakllsch 		break;
    175       1.1  jakllsch 	default:
    176       1.1  jakllsch 		return EINVAL;
    177       1.1  jakllsch 	}
    178       1.1  jakllsch 
    179       1.1  jakllsch 	sc->sc_CS = cs;
    180       1.1  jakllsch 
    181       1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
    182       1.1  jakllsch 
    183       1.1  jakllsch 	clk = 2 * 250000000 / speed; /* XXX 250MHz */
    184       1.1  jakllsch 	clk = (clk / 2) + (clk & 1);
    185       1.1  jakllsch 	clk = roundup(clk, 2);
    186       1.1  jakllsch 	clk = __SHIFTIN(clk, SPI_CLK_CDIV);
    187       1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CLK, clk);
    188       1.1  jakllsch 
    189       1.1  jakllsch 	return 0;
    190       1.1  jakllsch }
    191       1.1  jakllsch 
    192       1.1  jakllsch static int
    193       1.1  jakllsch bcmspi_transfer(void *cookie, struct spi_transfer *st)
    194       1.1  jakllsch {
    195       1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    196       1.1  jakllsch 
    197       1.7    kardel 	mutex_enter(&sc->sc_mutex);
    198       1.1  jakllsch 	spi_transq_enqueue(&sc->sc_q, st);
    199       1.1  jakllsch 	if (sc->sc_running == false) {
    200       1.1  jakllsch 		bcmspi_start(sc);
    201       1.1  jakllsch 	}
    202       1.7    kardel 	mutex_exit(&sc->sc_mutex);
    203       1.1  jakllsch 	return 0;
    204       1.1  jakllsch }
    205       1.1  jakllsch 
    206       1.1  jakllsch static void
    207       1.1  jakllsch bcmspi_start(struct bcmspi_softc * const sc)
    208       1.1  jakllsch {
    209       1.1  jakllsch 	struct spi_transfer *st;
    210       1.1  jakllsch 	uint32_t cs;
    211       1.1  jakllsch 
    212       1.1  jakllsch 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    213       1.1  jakllsch 
    214       1.1  jakllsch 		spi_transq_dequeue(&sc->sc_q);
    215       1.1  jakllsch 
    216       1.1  jakllsch 		KASSERT(sc->sc_transfer == NULL);
    217       1.1  jakllsch 		sc->sc_transfer = st;
    218       1.1  jakllsch 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    219       1.1  jakllsch 
    220       1.1  jakllsch 		cs = sc->sc_CS;
    221       1.1  jakllsch 		cs |= SPI_CS_TA;
    222       1.1  jakllsch 		cs |= SPI_CS_CLEAR_TX;
    223       1.1  jakllsch 		cs |= SPI_CS_CLEAR_RX;
    224       1.1  jakllsch 		KASSERT(st->st_slave <= 2);
    225       1.1  jakllsch 		cs |= __SHIFTIN(st->st_slave, SPI_CS_CS);
    226       1.1  jakllsch 		sc->sc_running = true;
    227       1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
    228       1.1  jakllsch 
    229       1.1  jakllsch 		if (!cold)
    230       1.1  jakllsch 			return;
    231       1.1  jakllsch 
    232       1.1  jakllsch 		for (;;) {
    233       1.7    kardel 		        mutex_exit(&sc->sc_mutex);
    234       1.1  jakllsch 			bcmspi_intr(sc);
    235       1.7    kardel 			mutex_enter(&sc->sc_mutex);
    236       1.1  jakllsch 			if (ISSET(st->st_flags, SPI_F_DONE))
    237       1.1  jakllsch 				break;
    238       1.1  jakllsch 		}
    239       1.1  jakllsch 	}
    240       1.1  jakllsch 
    241       1.1  jakllsch 	sc->sc_running = false;
    242       1.1  jakllsch }
    243       1.1  jakllsch 
    244       1.1  jakllsch static void
    245       1.1  jakllsch bcmspi_send(struct bcmspi_softc * const sc)
    246       1.1  jakllsch {
    247       1.1  jakllsch 	uint32_t fd;
    248       1.1  jakllsch 	uint32_t cs;
    249       1.1  jakllsch 	struct spi_chunk *chunk;
    250       1.1  jakllsch 
    251       1.1  jakllsch 	while ((chunk = sc->sc_wchunk) != NULL) {
    252       1.1  jakllsch 		while (chunk->chunk_wresid) {
    253       1.1  jakllsch 			cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    254       1.1  jakllsch 			if ((cs & SPI_CS_TXD) == 0)
    255       1.1  jakllsch 				return;
    256       1.1  jakllsch 			if (chunk->chunk_wptr) {
    257       1.1  jakllsch 				fd = *chunk->chunk_wptr++;
    258       1.1  jakllsch 			} else {
    259       1.1  jakllsch 				fd = '\0';
    260       1.1  jakllsch 			}
    261       1.1  jakllsch 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO, fd);
    262       1.1  jakllsch 			chunk->chunk_wresid--;
    263       1.1  jakllsch 		}
    264       1.1  jakllsch 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    265       1.1  jakllsch 	}
    266       1.1  jakllsch }
    267       1.1  jakllsch 
    268       1.1  jakllsch static void
    269       1.1  jakllsch bcmspi_recv(struct bcmspi_softc * const sc)
    270       1.1  jakllsch {
    271       1.1  jakllsch 	uint32_t fd;
    272       1.1  jakllsch 	uint32_t cs;
    273       1.1  jakllsch 	struct spi_chunk *chunk;
    274       1.1  jakllsch 
    275       1.1  jakllsch 	while ((chunk = sc->sc_rchunk) != NULL) {
    276       1.1  jakllsch 		while (chunk->chunk_rresid) {
    277       1.1  jakllsch 			cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    278       1.1  jakllsch 			if ((cs & SPI_CS_RXD) == 0)
    279       1.1  jakllsch 				return;
    280       1.1  jakllsch 			fd = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO);
    281       1.1  jakllsch 			if (chunk->chunk_rptr) {
    282       1.1  jakllsch 				*chunk->chunk_rptr++ = fd & 0xff;
    283       1.1  jakllsch 			}
    284       1.1  jakllsch 			chunk->chunk_rresid--;
    285       1.1  jakllsch 		}
    286       1.1  jakllsch 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    287       1.1  jakllsch 	}
    288       1.1  jakllsch }
    289       1.1  jakllsch 
    290       1.1  jakllsch static int
    291       1.1  jakllsch bcmspi_intr(void *cookie)
    292       1.1  jakllsch {
    293       1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    294       1.1  jakllsch 	struct spi_transfer *st;
    295       1.1  jakllsch 	uint32_t cs;
    296       1.1  jakllsch 
    297       1.7    kardel 	mutex_enter(&sc->sc_mutex);
    298       1.1  jakllsch 	cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    299       1.1  jakllsch 	if (ISSET(cs, SPI_CS_DONE)) {
    300       1.1  jakllsch 		if (sc->sc_wchunk != NULL) {
    301       1.1  jakllsch 			bcmspi_send(sc);
    302       1.1  jakllsch 			goto end;
    303       1.1  jakllsch 		} else {
    304       1.1  jakllsch 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS,
    305       1.1  jakllsch 			    sc->sc_CS);
    306       1.1  jakllsch 			bcmspi_recv(sc);
    307       1.1  jakllsch 			sc->sc_rchunk = sc->sc_wchunk = NULL;
    308       1.1  jakllsch 			st = sc->sc_transfer;
    309       1.1  jakllsch 			sc->sc_transfer = NULL;
    310       1.1  jakllsch 			KASSERT(st != NULL);
    311       1.1  jakllsch 			spi_done(st, 0);
    312       1.1  jakllsch 			sc->sc_running = false;
    313       1.1  jakllsch 			goto end;
    314       1.1  jakllsch 		}
    315       1.1  jakllsch 	} else if (ISSET(cs, SPI_CS_RXR)) {
    316       1.1  jakllsch 		bcmspi_recv(sc);
    317       1.1  jakllsch 		bcmspi_send(sc);
    318       1.1  jakllsch 	}
    319       1.1  jakllsch 
    320       1.1  jakllsch end:
    321       1.7    kardel 	mutex_exit(&sc->sc_mutex);
    322       1.1  jakllsch 	return ISSET(cs, SPI_CS_DONE|SPI_CS_RXR);
    323       1.1  jakllsch }
    324