Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_spi.c revision 1.14
      1  1.14   thorpej /*	$NetBSD: bcm2835_spi.c,v 1.14 2025/09/10 01:55:06 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.14   thorpej __KERNEL_RCSID(0, "$NetBSD: bcm2835_spi.c,v 1.14 2025/09/10 01:55:06 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.14   thorpej 	spibus_attach(self, &sc->sc_spi);
    141   1.1  jakllsch }
    142   1.1  jakllsch 
    143   1.1  jakllsch static int
    144   1.1  jakllsch bcmspi_configure(void *cookie, int slave, int mode, int speed)
    145   1.1  jakllsch {
    146   1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    147   1.1  jakllsch 	uint32_t cs, clk;
    148   1.1  jakllsch 
    149   1.1  jakllsch 	cs = SPI_CS_INTR | SPI_CS_INTD;
    150   1.1  jakllsch 
    151   1.1  jakllsch 	if (slave > 2)
    152   1.1  jakllsch 		return EINVAL;
    153   1.1  jakllsch 
    154   1.1  jakllsch 	if (speed <= 0)
    155   1.1  jakllsch 		return EINVAL;
    156   1.1  jakllsch 
    157   1.1  jakllsch 	switch (mode) {
    158   1.1  jakllsch 	case SPI_MODE_0:
    159   1.1  jakllsch 		cs |= 0;
    160   1.1  jakllsch 		break;
    161   1.1  jakllsch 	case SPI_MODE_1:
    162   1.1  jakllsch 		cs |= SPI_CS_CPHA;
    163   1.1  jakllsch 		break;
    164   1.1  jakllsch 	case SPI_MODE_2:
    165   1.1  jakllsch 		cs |= SPI_CS_CPOL;
    166   1.1  jakllsch 		break;
    167   1.1  jakllsch 	case SPI_MODE_3:
    168   1.1  jakllsch 		cs |= SPI_CS_CPHA|SPI_CS_CPOL;
    169   1.1  jakllsch 		break;
    170   1.1  jakllsch 	default:
    171   1.1  jakllsch 		return EINVAL;
    172   1.1  jakllsch 	}
    173   1.1  jakllsch 
    174   1.1  jakllsch 	sc->sc_CS = cs;
    175   1.1  jakllsch 
    176   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
    177   1.1  jakllsch 
    178   1.1  jakllsch 	clk = 2 * 250000000 / speed; /* XXX 250MHz */
    179   1.1  jakllsch 	clk = (clk / 2) + (clk & 1);
    180   1.1  jakllsch 	clk = roundup(clk, 2);
    181  1.13       tnn 	if (clk >= 0xfffe)
    182  1.13       tnn 		clk = 0xfffe;
    183   1.1  jakllsch 	clk = __SHIFTIN(clk, SPI_CLK_CDIV);
    184   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CLK, clk);
    185   1.1  jakllsch 
    186   1.1  jakllsch 	return 0;
    187   1.1  jakllsch }
    188   1.1  jakllsch 
    189   1.1  jakllsch static int
    190   1.1  jakllsch bcmspi_transfer(void *cookie, struct spi_transfer *st)
    191   1.1  jakllsch {
    192   1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    193   1.1  jakllsch 
    194   1.7    kardel 	mutex_enter(&sc->sc_mutex);
    195   1.1  jakllsch 	spi_transq_enqueue(&sc->sc_q, st);
    196   1.1  jakllsch 	if (sc->sc_running == false) {
    197   1.1  jakllsch 		bcmspi_start(sc);
    198   1.1  jakllsch 	}
    199   1.7    kardel 	mutex_exit(&sc->sc_mutex);
    200   1.1  jakllsch 	return 0;
    201   1.1  jakllsch }
    202   1.1  jakllsch 
    203   1.1  jakllsch static void
    204   1.1  jakllsch bcmspi_start(struct bcmspi_softc * const sc)
    205   1.1  jakllsch {
    206   1.1  jakllsch 	struct spi_transfer *st;
    207   1.1  jakllsch 	uint32_t cs;
    208   1.1  jakllsch 
    209   1.1  jakllsch 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    210   1.1  jakllsch 
    211   1.1  jakllsch 		spi_transq_dequeue(&sc->sc_q);
    212   1.1  jakllsch 
    213   1.1  jakllsch 		KASSERT(sc->sc_transfer == NULL);
    214   1.1  jakllsch 		sc->sc_transfer = st;
    215   1.1  jakllsch 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    216   1.1  jakllsch 
    217   1.1  jakllsch 		cs = sc->sc_CS;
    218   1.1  jakllsch 		cs |= SPI_CS_TA;
    219   1.1  jakllsch 		cs |= SPI_CS_CLEAR_TX;
    220   1.1  jakllsch 		cs |= SPI_CS_CLEAR_RX;
    221   1.1  jakllsch 		KASSERT(st->st_slave <= 2);
    222   1.1  jakllsch 		cs |= __SHIFTIN(st->st_slave, SPI_CS_CS);
    223   1.1  jakllsch 		sc->sc_running = true;
    224   1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
    225   1.1  jakllsch 
    226   1.1  jakllsch 		if (!cold)
    227   1.1  jakllsch 			return;
    228   1.1  jakllsch 
    229   1.1  jakllsch 		for (;;) {
    230   1.7    kardel 		        mutex_exit(&sc->sc_mutex);
    231   1.1  jakllsch 			bcmspi_intr(sc);
    232   1.7    kardel 			mutex_enter(&sc->sc_mutex);
    233   1.1  jakllsch 			if (ISSET(st->st_flags, SPI_F_DONE))
    234   1.1  jakllsch 				break;
    235   1.1  jakllsch 		}
    236   1.1  jakllsch 	}
    237   1.1  jakllsch 
    238   1.1  jakllsch 	sc->sc_running = false;
    239   1.1  jakllsch }
    240   1.1  jakllsch 
    241   1.1  jakllsch static void
    242   1.1  jakllsch bcmspi_send(struct bcmspi_softc * const sc)
    243   1.1  jakllsch {
    244   1.1  jakllsch 	uint32_t fd;
    245   1.1  jakllsch 	uint32_t cs;
    246   1.1  jakllsch 	struct spi_chunk *chunk;
    247   1.1  jakllsch 
    248   1.1  jakllsch 	while ((chunk = sc->sc_wchunk) != NULL) {
    249   1.1  jakllsch 		while (chunk->chunk_wresid) {
    250   1.1  jakllsch 			cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    251   1.1  jakllsch 			if ((cs & SPI_CS_TXD) == 0)
    252   1.1  jakllsch 				return;
    253   1.1  jakllsch 			if (chunk->chunk_wptr) {
    254   1.1  jakllsch 				fd = *chunk->chunk_wptr++;
    255   1.1  jakllsch 			} else {
    256   1.1  jakllsch 				fd = '\0';
    257   1.1  jakllsch 			}
    258   1.1  jakllsch 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO, fd);
    259   1.1  jakllsch 			chunk->chunk_wresid--;
    260   1.1  jakllsch 		}
    261   1.1  jakllsch 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    262   1.1  jakllsch 	}
    263   1.1  jakllsch }
    264   1.1  jakllsch 
    265   1.1  jakllsch static void
    266   1.1  jakllsch bcmspi_recv(struct bcmspi_softc * const sc)
    267   1.1  jakllsch {
    268   1.1  jakllsch 	uint32_t fd;
    269   1.1  jakllsch 	uint32_t cs;
    270   1.1  jakllsch 	struct spi_chunk *chunk;
    271   1.1  jakllsch 
    272   1.1  jakllsch 	while ((chunk = sc->sc_rchunk) != NULL) {
    273   1.1  jakllsch 		while (chunk->chunk_rresid) {
    274   1.1  jakllsch 			cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    275   1.1  jakllsch 			if ((cs & SPI_CS_RXD) == 0)
    276   1.1  jakllsch 				return;
    277   1.1  jakllsch 			fd = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO);
    278   1.1  jakllsch 			if (chunk->chunk_rptr) {
    279   1.1  jakllsch 				*chunk->chunk_rptr++ = fd & 0xff;
    280   1.1  jakllsch 			}
    281   1.1  jakllsch 			chunk->chunk_rresid--;
    282   1.1  jakllsch 		}
    283   1.1  jakllsch 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    284   1.1  jakllsch 	}
    285   1.1  jakllsch }
    286   1.1  jakllsch 
    287   1.1  jakllsch static int
    288   1.1  jakllsch bcmspi_intr(void *cookie)
    289   1.1  jakllsch {
    290   1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    291   1.1  jakllsch 	struct spi_transfer *st;
    292   1.1  jakllsch 	uint32_t cs;
    293   1.1  jakllsch 
    294   1.7    kardel 	mutex_enter(&sc->sc_mutex);
    295   1.1  jakllsch 	cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    296   1.1  jakllsch 	if (ISSET(cs, SPI_CS_DONE)) {
    297   1.1  jakllsch 		if (sc->sc_wchunk != NULL) {
    298   1.1  jakllsch 			bcmspi_send(sc);
    299   1.1  jakllsch 		} else {
    300   1.1  jakllsch 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS,
    301   1.1  jakllsch 			    sc->sc_CS);
    302   1.1  jakllsch 			bcmspi_recv(sc);
    303   1.1  jakllsch 			sc->sc_rchunk = sc->sc_wchunk = NULL;
    304   1.1  jakllsch 			st = sc->sc_transfer;
    305   1.1  jakllsch 			sc->sc_transfer = NULL;
    306   1.1  jakllsch 			KASSERT(st != NULL);
    307   1.1  jakllsch 			spi_done(st, 0);
    308   1.1  jakllsch 			sc->sc_running = false;
    309   1.1  jakllsch 		}
    310   1.1  jakllsch 	} else if (ISSET(cs, SPI_CS_RXR)) {
    311   1.1  jakllsch 		bcmspi_recv(sc);
    312   1.1  jakllsch 		bcmspi_send(sc);
    313   1.1  jakllsch 	}
    314   1.1  jakllsch 
    315   1.7    kardel 	mutex_exit(&sc->sc_mutex);
    316   1.1  jakllsch 	return ISSET(cs, SPI_CS_DONE|SPI_CS_RXR);
    317   1.1  jakllsch }
    318