Home | History | Annotate | Line # | Download | only in broadcom
bcm2835_spi.c revision 1.13
      1  1.13       tnn /*	$NetBSD: bcm2835_spi.c,v 1.13 2023/09/03 11:36:52 tnn 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.13       tnn __KERNEL_RCSID(0, "$NetBSD: bcm2835_spi.c,v 1.13 2023/09/03 11:36:52 tnn 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 	struct spibus_attach_args sba;
     99   1.1  jakllsch 
    100   1.1  jakllsch 	aprint_naive("\n");
    101   1.1  jakllsch 	aprint_normal(": SPI\n");
    102   1.1  jakllsch 
    103   1.1  jakllsch 	sc->sc_dev = self;
    104   1.5     skrll 	sc->sc_iot = faa->faa_bst;
    105   1.1  jakllsch 	SIMPLEQ_INIT(&sc->sc_q);
    106   1.5     skrll 
    107   1.5     skrll 	const int phandle = faa->faa_phandle;
    108   1.5     skrll 	bus_addr_t addr;
    109   1.5     skrll 	bus_size_t size;
    110   1.5     skrll 
    111   1.5     skrll 	if (fdtbus_get_reg(phandle, 0, &addr, &size) != 0) {
    112   1.5     skrll 		aprint_error(": missing 'reg' property\n");
    113   1.5     skrll 		return;
    114   1.5     skrll 	}
    115   1.5     skrll 
    116   1.5     skrll 	if (bus_space_map(sc->sc_iot, addr, size, 0, &sc->sc_ioh) != 0) {
    117   1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "unable to map device\n");
    118   1.1  jakllsch 		return;
    119   1.1  jakllsch 	}
    120   1.1  jakllsch 
    121   1.5     skrll 	char intrstr[128];
    122   1.5     skrll 	if (!fdtbus_intr_str(phandle, 0, intrstr, sizeof(intrstr))) {
    123   1.5     skrll 		aprint_error(": failed to decode interrupt\n");
    124   1.5     skrll 		return;
    125   1.5     skrll 	}
    126   1.1  jakllsch 
    127   1.9     skrll 	sc->sc_intrh = fdtbus_intr_establish_xname(phandle, 0, IPL_VM, 0,
    128   1.9     skrll 	    bcmspi_intr, sc, device_xname(self));
    129   1.1  jakllsch 	if (sc->sc_intrh == NULL) {
    130   1.1  jakllsch 		aprint_error_dev(sc->sc_dev, "unable to establish interrupt\n");
    131   1.1  jakllsch 		return;
    132   1.1  jakllsch 	}
    133   1.5     skrll 	aprint_normal_dev(self, "interrupting on %s\n", intrstr);
    134   1.1  jakllsch 
    135   1.1  jakllsch 	sc->sc_spi.sct_cookie = sc;
    136   1.7    kardel 	mutex_init(&sc->sc_mutex, MUTEX_DEFAULT, IPL_VM);
    137   1.1  jakllsch 	sc->sc_spi.sct_configure = bcmspi_configure;
    138   1.1  jakllsch 	sc->sc_spi.sct_transfer = bcmspi_transfer;
    139   1.1  jakllsch 	sc->sc_spi.sct_nslaves = 3;
    140   1.1  jakllsch 
    141   1.6       tnn 	memset(&sba, 0, sizeof(sba));
    142   1.1  jakllsch 	sba.sba_controller = &sc->sc_spi;
    143   1.1  jakllsch 
    144  1.11   thorpej 	config_found(self, &sba, spibus_print, CFARGS_NONE);
    145   1.1  jakllsch }
    146   1.1  jakllsch 
    147   1.1  jakllsch static int
    148   1.1  jakllsch bcmspi_configure(void *cookie, int slave, int mode, int speed)
    149   1.1  jakllsch {
    150   1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    151   1.1  jakllsch 	uint32_t cs, clk;
    152   1.1  jakllsch 
    153   1.1  jakllsch 	cs = SPI_CS_INTR | SPI_CS_INTD;
    154   1.1  jakllsch 
    155   1.1  jakllsch 	if (slave > 2)
    156   1.1  jakllsch 		return EINVAL;
    157   1.1  jakllsch 
    158   1.1  jakllsch 	if (speed <= 0)
    159   1.1  jakllsch 		return EINVAL;
    160   1.1  jakllsch 
    161   1.1  jakllsch 	switch (mode) {
    162   1.1  jakllsch 	case SPI_MODE_0:
    163   1.1  jakllsch 		cs |= 0;
    164   1.1  jakllsch 		break;
    165   1.1  jakllsch 	case SPI_MODE_1:
    166   1.1  jakllsch 		cs |= SPI_CS_CPHA;
    167   1.1  jakllsch 		break;
    168   1.1  jakllsch 	case SPI_MODE_2:
    169   1.1  jakllsch 		cs |= SPI_CS_CPOL;
    170   1.1  jakllsch 		break;
    171   1.1  jakllsch 	case SPI_MODE_3:
    172   1.1  jakllsch 		cs |= SPI_CS_CPHA|SPI_CS_CPOL;
    173   1.1  jakllsch 		break;
    174   1.1  jakllsch 	default:
    175   1.1  jakllsch 		return EINVAL;
    176   1.1  jakllsch 	}
    177   1.1  jakllsch 
    178   1.1  jakllsch 	sc->sc_CS = cs;
    179   1.1  jakllsch 
    180   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
    181   1.1  jakllsch 
    182   1.1  jakllsch 	clk = 2 * 250000000 / speed; /* XXX 250MHz */
    183   1.1  jakllsch 	clk = (clk / 2) + (clk & 1);
    184   1.1  jakllsch 	clk = roundup(clk, 2);
    185  1.13       tnn 	if (clk >= 0xfffe)
    186  1.13       tnn 		clk = 0xfffe;
    187   1.1  jakllsch 	clk = __SHIFTIN(clk, SPI_CLK_CDIV);
    188   1.1  jakllsch 	bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CLK, clk);
    189   1.1  jakllsch 
    190   1.1  jakllsch 	return 0;
    191   1.1  jakllsch }
    192   1.1  jakllsch 
    193   1.1  jakllsch static int
    194   1.1  jakllsch bcmspi_transfer(void *cookie, struct spi_transfer *st)
    195   1.1  jakllsch {
    196   1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    197   1.1  jakllsch 
    198   1.7    kardel 	mutex_enter(&sc->sc_mutex);
    199   1.1  jakllsch 	spi_transq_enqueue(&sc->sc_q, st);
    200   1.1  jakllsch 	if (sc->sc_running == false) {
    201   1.1  jakllsch 		bcmspi_start(sc);
    202   1.1  jakllsch 	}
    203   1.7    kardel 	mutex_exit(&sc->sc_mutex);
    204   1.1  jakllsch 	return 0;
    205   1.1  jakllsch }
    206   1.1  jakllsch 
    207   1.1  jakllsch static void
    208   1.1  jakllsch bcmspi_start(struct bcmspi_softc * const sc)
    209   1.1  jakllsch {
    210   1.1  jakllsch 	struct spi_transfer *st;
    211   1.1  jakllsch 	uint32_t cs;
    212   1.1  jakllsch 
    213   1.1  jakllsch 	while ((st = spi_transq_first(&sc->sc_q)) != NULL) {
    214   1.1  jakllsch 
    215   1.1  jakllsch 		spi_transq_dequeue(&sc->sc_q);
    216   1.1  jakllsch 
    217   1.1  jakllsch 		KASSERT(sc->sc_transfer == NULL);
    218   1.1  jakllsch 		sc->sc_transfer = st;
    219   1.1  jakllsch 		sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
    220   1.1  jakllsch 
    221   1.1  jakllsch 		cs = sc->sc_CS;
    222   1.1  jakllsch 		cs |= SPI_CS_TA;
    223   1.1  jakllsch 		cs |= SPI_CS_CLEAR_TX;
    224   1.1  jakllsch 		cs |= SPI_CS_CLEAR_RX;
    225   1.1  jakllsch 		KASSERT(st->st_slave <= 2);
    226   1.1  jakllsch 		cs |= __SHIFTIN(st->st_slave, SPI_CS_CS);
    227   1.1  jakllsch 		sc->sc_running = true;
    228   1.1  jakllsch 		bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_CS, cs);
    229   1.1  jakllsch 
    230   1.1  jakllsch 		if (!cold)
    231   1.1  jakllsch 			return;
    232   1.1  jakllsch 
    233   1.1  jakllsch 		for (;;) {
    234   1.7    kardel 		        mutex_exit(&sc->sc_mutex);
    235   1.1  jakllsch 			bcmspi_intr(sc);
    236   1.7    kardel 			mutex_enter(&sc->sc_mutex);
    237   1.1  jakllsch 			if (ISSET(st->st_flags, SPI_F_DONE))
    238   1.1  jakllsch 				break;
    239   1.1  jakllsch 		}
    240   1.1  jakllsch 	}
    241   1.1  jakllsch 
    242   1.1  jakllsch 	sc->sc_running = false;
    243   1.1  jakllsch }
    244   1.1  jakllsch 
    245   1.1  jakllsch static void
    246   1.1  jakllsch bcmspi_send(struct bcmspi_softc * const sc)
    247   1.1  jakllsch {
    248   1.1  jakllsch 	uint32_t fd;
    249   1.1  jakllsch 	uint32_t cs;
    250   1.1  jakllsch 	struct spi_chunk *chunk;
    251   1.1  jakllsch 
    252   1.1  jakllsch 	while ((chunk = sc->sc_wchunk) != NULL) {
    253   1.1  jakllsch 		while (chunk->chunk_wresid) {
    254   1.1  jakllsch 			cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    255   1.1  jakllsch 			if ((cs & SPI_CS_TXD) == 0)
    256   1.1  jakllsch 				return;
    257   1.1  jakllsch 			if (chunk->chunk_wptr) {
    258   1.1  jakllsch 				fd = *chunk->chunk_wptr++;
    259   1.1  jakllsch 			} else {
    260   1.1  jakllsch 				fd = '\0';
    261   1.1  jakllsch 			}
    262   1.1  jakllsch 			bus_space_write_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO, fd);
    263   1.1  jakllsch 			chunk->chunk_wresid--;
    264   1.1  jakllsch 		}
    265   1.1  jakllsch 		sc->sc_wchunk = sc->sc_wchunk->chunk_next;
    266   1.1  jakllsch 	}
    267   1.1  jakllsch }
    268   1.1  jakllsch 
    269   1.1  jakllsch static void
    270   1.1  jakllsch bcmspi_recv(struct bcmspi_softc * const sc)
    271   1.1  jakllsch {
    272   1.1  jakllsch 	uint32_t fd;
    273   1.1  jakllsch 	uint32_t cs;
    274   1.1  jakllsch 	struct spi_chunk *chunk;
    275   1.1  jakllsch 
    276   1.1  jakllsch 	while ((chunk = sc->sc_rchunk) != NULL) {
    277   1.1  jakllsch 		while (chunk->chunk_rresid) {
    278   1.1  jakllsch 			cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    279   1.1  jakllsch 			if ((cs & SPI_CS_RXD) == 0)
    280   1.1  jakllsch 				return;
    281   1.1  jakllsch 			fd = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_FIFO);
    282   1.1  jakllsch 			if (chunk->chunk_rptr) {
    283   1.1  jakllsch 				*chunk->chunk_rptr++ = fd & 0xff;
    284   1.1  jakllsch 			}
    285   1.1  jakllsch 			chunk->chunk_rresid--;
    286   1.1  jakllsch 		}
    287   1.1  jakllsch 		sc->sc_rchunk = sc->sc_rchunk->chunk_next;
    288   1.1  jakllsch 	}
    289   1.1  jakllsch }
    290   1.1  jakllsch 
    291   1.1  jakllsch static int
    292   1.1  jakllsch bcmspi_intr(void *cookie)
    293   1.1  jakllsch {
    294   1.1  jakllsch 	struct bcmspi_softc * const sc = cookie;
    295   1.1  jakllsch 	struct spi_transfer *st;
    296   1.1  jakllsch 	uint32_t cs;
    297   1.1  jakllsch 
    298   1.7    kardel 	mutex_enter(&sc->sc_mutex);
    299   1.1  jakllsch 	cs = bus_space_read_4(sc->sc_iot, sc->sc_ioh, SPI_CS);
    300   1.1  jakllsch 	if (ISSET(cs, SPI_CS_DONE)) {
    301   1.1  jakllsch 		if (sc->sc_wchunk != NULL) {
    302   1.1  jakllsch 			bcmspi_send(sc);
    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 		}
    314   1.1  jakllsch 	} else if (ISSET(cs, SPI_CS_RXR)) {
    315   1.1  jakllsch 		bcmspi_recv(sc);
    316   1.1  jakllsch 		bcmspi_send(sc);
    317   1.1  jakllsch 	}
    318   1.1  jakllsch 
    319   1.7    kardel 	mutex_exit(&sc->sc_mutex);
    320   1.1  jakllsch 	return ISSET(cs, SPI_CS_DONE|SPI_CS_RXR);
    321   1.1  jakllsch }
    322