Home | History | Annotate | Line # | Download | only in spi
spi.c revision 1.29
      1  1.29   thorpej /* $NetBSD: spi.c,v 1.29 2025/09/10 04:11:32 thorpej Exp $ */
      2   1.1   gdamore 
      3   1.1   gdamore /*-
      4   1.1   gdamore  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5   1.1   gdamore  * Copyright (c) 2006 Garrett D'Amore.
      6   1.1   gdamore  * All rights reserved.
      7   1.1   gdamore  *
      8   1.1   gdamore  * Portions of this code were written by Garrett D'Amore for the
      9   1.1   gdamore  * Champaign-Urbana Community Wireless Network Project.
     10   1.1   gdamore  *
     11   1.1   gdamore  * Redistribution and use in source and binary forms, with or
     12   1.1   gdamore  * without modification, are permitted provided that the following
     13   1.1   gdamore  * conditions are met:
     14   1.1   gdamore  * 1. Redistributions of source code must retain the above copyright
     15   1.1   gdamore  *    notice, this list of conditions and the following disclaimer.
     16   1.1   gdamore  * 2. Redistributions in binary form must reproduce the above
     17   1.1   gdamore  *    copyright notice, this list of conditions and the following
     18   1.1   gdamore  *    disclaimer in the documentation and/or other materials provided
     19   1.1   gdamore  *    with the distribution.
     20   1.1   gdamore  * 3. All advertising materials mentioning features or use of this
     21   1.1   gdamore  *    software must display the following acknowledgements:
     22   1.1   gdamore  *      This product includes software developed by the Urbana-Champaign
     23   1.1   gdamore  *      Independent Media Center.
     24   1.1   gdamore  *	This product includes software developed by Garrett D'Amore.
     25   1.1   gdamore  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26   1.1   gdamore  *    D'Amore's name may not be used to endorse or promote products
     27   1.1   gdamore  *    derived from this software without specific prior written permission.
     28   1.1   gdamore  *
     29   1.1   gdamore  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30   1.1   gdamore  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31   1.1   gdamore  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32   1.1   gdamore  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33   1.1   gdamore  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34   1.1   gdamore  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35   1.1   gdamore  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36   1.1   gdamore  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37   1.1   gdamore  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38   1.1   gdamore  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.1   gdamore  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40   1.1   gdamore  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41   1.1   gdamore  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42   1.1   gdamore  */
     43   1.1   gdamore 
     44  1.27   thorpej #include "opt_fdt.h"		/* XXX */
     45  1.27   thorpej 
     46   1.1   gdamore #include <sys/cdefs.h>
     47  1.29   thorpej __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.29 2025/09/10 04:11:32 thorpej Exp $");
     48   1.1   gdamore 
     49   1.1   gdamore #include "locators.h"
     50   1.1   gdamore 
     51   1.1   gdamore #include <sys/param.h>
     52   1.1   gdamore #include <sys/systm.h>
     53   1.1   gdamore #include <sys/device.h>
     54  1.10   mlelstv #include <sys/conf.h>
     55   1.1   gdamore #include <sys/malloc.h>
     56   1.5     rmind #include <sys/mutex.h>
     57   1.5     rmind #include <sys/condvar.h>
     58   1.1   gdamore #include <sys/errno.h>
     59   1.1   gdamore 
     60   1.1   gdamore #include <dev/spi/spivar.h>
     61  1.10   mlelstv #include <dev/spi/spi_io.h>
     62  1.10   mlelstv 
     63  1.27   thorpej #ifdef FDT
     64  1.27   thorpej #include <dev/fdt/fdt_spi.h>	/* XXX */
     65  1.29   thorpej #include <dev/ofw/openfirm.h>	/* XXX */
     66  1.27   thorpej #endif
     67  1.27   thorpej 
     68  1.10   mlelstv #include "ioconf.h"
     69  1.10   mlelstv #include "locators.h"
     70   1.1   gdamore 
     71   1.1   gdamore struct spi_softc {
     72  1.22   thorpej 	device_t		sc_dev;
     73  1.28   thorpej 	const struct spi_controller *sc_controller;
     74   1.1   gdamore 	int			sc_mode;
     75   1.1   gdamore 	int			sc_speed;
     76  1.10   mlelstv 	int			sc_slave;
     77   1.1   gdamore 	int			sc_nslaves;
     78   1.1   gdamore 	struct spi_handle	*sc_slaves;
     79  1.10   mlelstv 	kmutex_t		sc_lock;
     80  1.10   mlelstv 	kcondvar_t		sc_cv;
     81  1.18   mlelstv 	kmutex_t		sc_dev_lock;
     82  1.10   mlelstv 	int			sc_flags;
     83  1.10   mlelstv #define SPIC_BUSY		1
     84  1.10   mlelstv };
     85  1.10   mlelstv 
     86  1.10   mlelstv static dev_type_open(spi_open);
     87  1.10   mlelstv static dev_type_close(spi_close);
     88  1.10   mlelstv static dev_type_ioctl(spi_ioctl);
     89  1.10   mlelstv 
     90  1.10   mlelstv const struct cdevsw spi_cdevsw = {
     91  1.10   mlelstv 	.d_open = spi_open,
     92  1.10   mlelstv 	.d_close = spi_close,
     93  1.10   mlelstv 	.d_read = noread,
     94  1.10   mlelstv 	.d_write = nowrite,
     95  1.10   mlelstv 	.d_ioctl = spi_ioctl,
     96  1.10   mlelstv 	.d_stop = nostop,
     97  1.10   mlelstv 	.d_tty = notty,
     98  1.10   mlelstv 	.d_poll = nopoll,
     99  1.10   mlelstv 	.d_mmap = nommap,
    100  1.10   mlelstv 	.d_kqfilter = nokqfilter,
    101  1.10   mlelstv 	.d_discard = nodiscard,
    102  1.18   mlelstv 	.d_flag = D_OTHER | D_MPSAFE
    103   1.1   gdamore };
    104   1.1   gdamore 
    105   1.1   gdamore /*
    106   1.1   gdamore  * SPI slave device.  We have one of these per slave.
    107   1.1   gdamore  */
    108   1.1   gdamore struct spi_handle {
    109   1.1   gdamore 	struct spi_softc	*sh_sc;
    110  1.28   thorpej 	const struct spi_controller *sh_controller;
    111   1.1   gdamore 	int			sh_slave;
    112  1.10   mlelstv 	int			sh_mode;
    113  1.10   mlelstv 	int			sh_speed;
    114  1.12       tnn 	int			sh_flags;
    115  1.12       tnn #define SPIH_ATTACHED		1
    116   1.1   gdamore };
    117   1.1   gdamore 
    118  1.10   mlelstv #define SPI_MAXDATA 4096
    119  1.10   mlelstv 
    120   1.1   gdamore /*
    121   1.1   gdamore  * API for bus drivers.
    122   1.1   gdamore  */
    123   1.1   gdamore 
    124   1.1   gdamore int
    125   1.1   gdamore spibus_print(void *aux, const char *pnp)
    126   1.1   gdamore {
    127   1.1   gdamore 
    128   1.1   gdamore 	if (pnp != NULL)
    129   1.1   gdamore 		aprint_normal("spi at %s", pnp);
    130   1.1   gdamore 
    131   1.1   gdamore 	return (UNCONF);
    132   1.1   gdamore }
    133   1.1   gdamore 
    134   1.1   gdamore 
    135   1.1   gdamore static int
    136   1.3   xtraeme spi_match(device_t parent, cfdata_t cf, void *aux)
    137   1.1   gdamore {
    138   1.5     rmind 
    139   1.1   gdamore 	return 1;
    140   1.1   gdamore }
    141   1.1   gdamore 
    142   1.1   gdamore static int
    143   1.1   gdamore spi_print(void *aux, const char *pnp)
    144   1.1   gdamore {
    145   1.1   gdamore 	struct spi_attach_args *sa = aux;
    146   1.1   gdamore 
    147   1.1   gdamore 	if (sa->sa_handle->sh_slave != -1)
    148   1.1   gdamore 		aprint_normal(" slave %d", sa->sa_handle->sh_slave);
    149   1.1   gdamore 
    150   1.1   gdamore 	return (UNCONF);
    151   1.1   gdamore }
    152   1.1   gdamore 
    153   1.1   gdamore static int
    154   1.3   xtraeme spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    155   1.1   gdamore {
    156   1.3   xtraeme 	struct spi_softc *sc = device_private(parent);
    157   1.1   gdamore 	struct spi_attach_args sa;
    158   1.1   gdamore 	int addr;
    159   1.1   gdamore 
    160   1.1   gdamore 	addr = cf->cf_loc[SPICF_SLAVE];
    161  1.28   thorpej 	if ((addr < 0) || (addr >= sc->sc_controller->sct_nslaves)) {
    162   1.1   gdamore 		return -1;
    163   1.1   gdamore 	}
    164   1.1   gdamore 
    165  1.12       tnn 	memset(&sa, 0, sizeof sa);
    166   1.1   gdamore 	sa.sa_handle = &sc->sc_slaves[addr];
    167  1.12       tnn 	if (ISSET(sa.sa_handle->sh_flags, SPIH_ATTACHED))
    168  1.12       tnn 		return -1;
    169   1.1   gdamore 
    170  1.17   thorpej 	if (config_probe(parent, cf, &sa)) {
    171  1.12       tnn 		SET(sa.sa_handle->sh_flags, SPIH_ATTACHED);
    172  1.19   thorpej 		config_attach(parent, cf, &sa, spi_print, CFARGS_NONE);
    173  1.12       tnn 	}
    174   1.1   gdamore 
    175   1.1   gdamore 	return 0;
    176   1.1   gdamore }
    177   1.1   gdamore 
    178   1.1   gdamore /*
    179  1.12       tnn  * XXX this is the same as i2c_fill_compat. It could be refactored into a
    180  1.12       tnn  * common fill_compat function with pointers to compat & ncompat instead
    181  1.12       tnn  * of attach_args as the first parameter.
    182  1.12       tnn  */
    183  1.12       tnn static void
    184  1.12       tnn spi_fill_compat(struct spi_attach_args *sa, const char *compat, size_t len,
    185  1.12       tnn 	char **buffer)
    186  1.12       tnn {
    187  1.12       tnn 	int count, i;
    188  1.12       tnn 	const char *c, *start, **ptr;
    189  1.12       tnn 
    190  1.12       tnn 	*buffer = NULL;
    191  1.12       tnn 	for (i = count = 0, c = compat; i < len; i++, c++)
    192  1.12       tnn 		if (*c == 0)
    193  1.12       tnn 			count++;
    194  1.12       tnn 	count += 2;
    195  1.12       tnn 	ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
    196  1.12       tnn 	if (!ptr)
    197  1.12       tnn 		return;
    198  1.12       tnn 
    199  1.12       tnn 	for (i = count = 0, start = c = compat; i < len; i++, c++) {
    200  1.12       tnn 		if (*c == 0) {
    201  1.12       tnn 			ptr[count++] = start;
    202  1.12       tnn 			start = c + 1;
    203  1.12       tnn 		}
    204  1.12       tnn 	}
    205  1.12       tnn 	if (start < compat + len) {
    206  1.12       tnn 		/* last string not 0 terminated */
    207  1.12       tnn 		size_t l = c - start;
    208  1.12       tnn 		*buffer = malloc(l + 1, M_TEMP, M_WAITOK);
    209  1.12       tnn 		memcpy(*buffer, start, l);
    210  1.12       tnn 		(*buffer)[l] = 0;
    211  1.12       tnn 		ptr[count++] = *buffer;
    212  1.12       tnn 	}
    213  1.12       tnn 	ptr[count] = NULL;
    214  1.12       tnn 
    215  1.12       tnn 	sa->sa_compat = ptr;
    216  1.12       tnn 	sa->sa_ncompat = count;
    217  1.12       tnn }
    218  1.12       tnn 
    219  1.12       tnn static void
    220  1.29   thorpej spi_direct_attach_child_devices(struct spi_softc *sc)
    221  1.12       tnn {
    222  1.12       tnn 	unsigned int count;
    223  1.12       tnn 	prop_dictionary_t child;
    224  1.29   thorpej 	prop_array_t child_devices;
    225  1.12       tnn 	prop_data_t cdata;
    226  1.12       tnn 	uint32_t slave;
    227  1.12       tnn 	uint64_t cookie;
    228  1.12       tnn 	struct spi_attach_args sa;
    229  1.12       tnn 	int loc[SPICF_NLOCS];
    230  1.12       tnn 	char *buf;
    231  1.12       tnn 	int i;
    232  1.12       tnn 
    233  1.29   thorpej 	/* XXX A better way is coming, I promise... */
    234  1.29   thorpej 	switch (devhandle_type(device_handle(sc->sc_dev))) {
    235  1.29   thorpej #ifdef FDT
    236  1.29   thorpej 	case DEVHANDLE_TYPE_OF:
    237  1.29   thorpej 		child_devices = of_copy_spi_devs(sc->sc_dev);
    238  1.29   thorpej 		break;
    239  1.29   thorpej #endif
    240  1.29   thorpej 	default:
    241  1.29   thorpej 		child_devices = NULL;
    242  1.29   thorpej 		break;
    243  1.29   thorpej 	}
    244  1.29   thorpej 
    245  1.29   thorpej 	if (child_devices == NULL) {
    246  1.29   thorpej 		return;
    247  1.29   thorpej 	}
    248  1.29   thorpej 
    249  1.12       tnn 	memset(loc, 0, sizeof loc);
    250  1.12       tnn 	count = prop_array_count(child_devices);
    251  1.12       tnn 	for (i = 0; i < count; i++) {
    252  1.12       tnn 		child = prop_array_get(child_devices, i);
    253  1.12       tnn 		if (!child)
    254  1.12       tnn 			continue;
    255  1.12       tnn 		if (!prop_dictionary_get_uint32(child, "slave", &slave))
    256  1.12       tnn 			continue;
    257  1.29   thorpej 		if (slave >= sc->sc_controller->sct_nslaves)
    258  1.12       tnn 			continue;
    259  1.12       tnn 		if (!prop_dictionary_get_uint64(child, "cookie", &cookie))
    260  1.12       tnn 			continue;
    261  1.12       tnn 		if (!(cdata = prop_dictionary_get(child, "compatible")))
    262  1.12       tnn 			continue;
    263  1.12       tnn 		loc[SPICF_SLAVE] = slave;
    264  1.12       tnn 
    265  1.12       tnn 		memset(&sa, 0, sizeof sa);
    266  1.12       tnn 		sa.sa_handle = &sc->sc_slaves[i];
    267  1.13   hkenken 		sa.sa_prop = child;
    268  1.13   hkenken 		sa.sa_cookie = cookie;
    269  1.12       tnn 		if (ISSET(sa.sa_handle->sh_flags, SPIH_ATTACHED))
    270  1.12       tnn 			continue;
    271  1.12       tnn 		SET(sa.sa_handle->sh_flags, SPIH_ATTACHED);
    272  1.12       tnn 
    273  1.12       tnn 		buf = NULL;
    274  1.12       tnn 		spi_fill_compat(&sa,
    275  1.14   thorpej 				prop_data_value(cdata),
    276  1.12       tnn 				prop_data_size(cdata), &buf);
    277  1.29   thorpej 		config_found(sc->sc_dev, &sa, spi_print,
    278  1.19   thorpej 		    CFARGS(.locators = loc));
    279  1.12       tnn 
    280  1.12       tnn 		if (sa.sa_compat)
    281  1.12       tnn 			free(sa.sa_compat, M_TEMP);
    282  1.12       tnn 		if (buf)
    283  1.12       tnn 			free(buf, M_TEMP);
    284  1.12       tnn 	}
    285  1.29   thorpej 	prop_object_release(child_devices);
    286  1.12       tnn }
    287  1.12       tnn 
    288  1.12       tnn int
    289  1.12       tnn spi_compatible_match(const struct spi_attach_args *sa, const cfdata_t cf,
    290  1.12       tnn 		     const struct device_compatible_entry *compats)
    291  1.12       tnn {
    292  1.12       tnn 	if (sa->sa_ncompat > 0)
    293  1.12       tnn 		return device_compatible_match(sa->sa_compat, sa->sa_ncompat,
    294  1.16   thorpej 					       compats);
    295  1.12       tnn 
    296  1.12       tnn 	return 1;
    297  1.12       tnn }
    298  1.12       tnn 
    299  1.23   thorpej const struct device_compatible_entry *
    300  1.23   thorpej spi_compatible_lookup(const struct spi_attach_args *sa,
    301  1.23   thorpej     const struct device_compatible_entry *compats)
    302  1.23   thorpej {
    303  1.23   thorpej 	return device_compatible_lookup(sa->sa_compat, sa->sa_ncompat,
    304  1.23   thorpej 					compats);
    305  1.23   thorpej }
    306  1.23   thorpej 
    307  1.12       tnn /*
    308   1.1   gdamore  * API for device drivers.
    309   1.1   gdamore  *
    310   1.1   gdamore  * We provide wrapper routines to decouple the ABI for the SPI
    311   1.1   gdamore  * device drivers from the ABI for the SPI bus drivers.
    312   1.1   gdamore  */
    313   1.1   gdamore static void
    314   1.3   xtraeme spi_attach(device_t parent, device_t self, void *aux)
    315   1.1   gdamore {
    316   1.1   gdamore 	struct spi_softc *sc = device_private(self);
    317   1.1   gdamore 	struct spibus_attach_args *sba = aux;
    318   1.1   gdamore 	int i;
    319   1.1   gdamore 
    320   1.1   gdamore 	aprint_naive(": SPI bus\n");
    321   1.1   gdamore 	aprint_normal(": SPI bus\n");
    322   1.1   gdamore 
    323  1.18   mlelstv 	mutex_init(&sc->sc_dev_lock, MUTEX_DEFAULT, IPL_NONE);
    324  1.15    kardel 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    325  1.10   mlelstv 	cv_init(&sc->sc_cv, "spictl");
    326  1.10   mlelstv 
    327  1.22   thorpej 	sc->sc_dev = self;
    328  1.28   thorpej 	sc->sc_controller = sba->sba_controller;
    329   1.8   rkujawa 	sc->sc_nslaves = sba->sba_controller->sct_nslaves;
    330   1.1   gdamore 	/* allocate slave structures */
    331   1.1   gdamore 	sc->sc_slaves = malloc(sizeof (struct spi_handle) * sc->sc_nslaves,
    332   1.1   gdamore 	    M_DEVBUF, M_WAITOK | M_ZERO);
    333   1.1   gdamore 
    334   1.1   gdamore 	sc->sc_speed = 0;
    335   1.2   gdamore 	sc->sc_mode = -1;
    336  1.10   mlelstv 	sc->sc_slave = -1;
    337   1.1   gdamore 
    338   1.1   gdamore 	/*
    339   1.1   gdamore 	 * Initialize slave handles
    340   1.1   gdamore 	 */
    341   1.1   gdamore 	for (i = 0; i < sc->sc_nslaves; i++) {
    342   1.1   gdamore 		sc->sc_slaves[i].sh_slave = i;
    343   1.1   gdamore 		sc->sc_slaves[i].sh_sc = sc;
    344  1.28   thorpej 		sc->sc_slaves[i].sh_controller = sc->sc_controller;
    345   1.1   gdamore 	}
    346   1.1   gdamore 
    347  1.29   thorpej 	/* XXX Need a better way for this. */
    348  1.29   thorpej 	switch (devhandle_type(device_handle(sc->sc_dev))) {
    349  1.29   thorpej #ifdef FDT
    350  1.29   thorpej 	case DEVHANDLE_TYPE_OF:
    351  1.29   thorpej 		fdtbus_register_spi_controller(self, sc->sc_controller);
    352  1.29   thorpej 		break;
    353  1.27   thorpej #endif /* FDT */
    354  1.29   thorpej 	default:
    355  1.29   thorpej 		break;
    356  1.29   thorpej 	}
    357  1.29   thorpej 
    358  1.29   thorpej 	/* First attach devices known to be present via the device tree. */
    359  1.29   thorpej 	spi_direct_attach_child_devices(sc);
    360  1.27   thorpej 
    361  1.12       tnn 	/* Then do any other devices the user may have manually wired */
    362  1.17   thorpej 	config_search(self, NULL,
    363  1.19   thorpej 	    CFARGS(.search = spi_search));
    364   1.1   gdamore }
    365   1.1   gdamore 
    366  1.10   mlelstv static int
    367  1.10   mlelstv spi_open(dev_t dev, int flag, int fmt, lwp_t *l)
    368  1.10   mlelstv {
    369  1.10   mlelstv 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    370  1.10   mlelstv 
    371  1.10   mlelstv 	if (sc == NULL)
    372  1.10   mlelstv 		return ENXIO;
    373  1.10   mlelstv 
    374  1.10   mlelstv 	return 0;
    375  1.10   mlelstv }
    376  1.10   mlelstv 
    377  1.10   mlelstv static int
    378  1.10   mlelstv spi_close(dev_t dev, int flag, int fmt, lwp_t *l)
    379  1.10   mlelstv {
    380  1.10   mlelstv 
    381  1.10   mlelstv 	return 0;
    382  1.10   mlelstv }
    383  1.10   mlelstv 
    384  1.10   mlelstv static int
    385  1.10   mlelstv spi_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    386  1.10   mlelstv {
    387  1.10   mlelstv 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    388  1.10   mlelstv 	struct spi_handle *sh;
    389  1.10   mlelstv 	spi_ioctl_configure_t *sic;
    390  1.10   mlelstv 	spi_ioctl_transfer_t *sit;
    391  1.10   mlelstv 	uint8_t *sbuf, *rbuf;
    392  1.10   mlelstv 	int error;
    393  1.10   mlelstv 
    394  1.10   mlelstv 	if (sc == NULL)
    395  1.10   mlelstv 		return ENXIO;
    396  1.10   mlelstv 
    397  1.18   mlelstv 	mutex_enter(&sc->sc_dev_lock);
    398  1.18   mlelstv 
    399  1.10   mlelstv 	switch (cmd) {
    400  1.10   mlelstv 	case SPI_IOCTL_CONFIGURE:
    401  1.10   mlelstv 		sic = (spi_ioctl_configure_t *)data;
    402  1.10   mlelstv 		if (sic->sic_addr < 0 || sic->sic_addr >= sc->sc_nslaves) {
    403  1.10   mlelstv 			error = EINVAL;
    404  1.10   mlelstv 			break;
    405  1.10   mlelstv 		}
    406  1.10   mlelstv 		sh = &sc->sc_slaves[sic->sic_addr];
    407  1.22   thorpej 		error = spi_configure(sc->sc_dev, sh, sic->sic_mode,
    408  1.22   thorpej 		    sic->sic_speed);
    409  1.10   mlelstv 		break;
    410  1.10   mlelstv 	case SPI_IOCTL_TRANSFER:
    411  1.10   mlelstv 		sit = (spi_ioctl_transfer_t *)data;
    412  1.10   mlelstv 		if (sit->sit_addr < 0 || sit->sit_addr >= sc->sc_nslaves) {
    413  1.10   mlelstv 			error = EINVAL;
    414  1.10   mlelstv 			break;
    415  1.10   mlelstv 		}
    416  1.11   mlelstv 		if ((sit->sit_send && sit->sit_sendlen == 0)
    417  1.24   mlelstv 		    || (sit->sit_recv && sit->sit_recvlen == 0)) {
    418  1.11   mlelstv 			error = EINVAL;
    419  1.11   mlelstv 			break;
    420  1.11   mlelstv 		}
    421  1.10   mlelstv 		sh = &sc->sc_slaves[sit->sit_addr];
    422  1.10   mlelstv 		sbuf = rbuf = NULL;
    423  1.10   mlelstv 		error = 0;
    424  1.11   mlelstv 		if (sit->sit_send && sit->sit_sendlen <= SPI_MAXDATA) {
    425  1.10   mlelstv 			sbuf = malloc(sit->sit_sendlen, M_DEVBUF, M_WAITOK);
    426  1.10   mlelstv 			error = copyin(sit->sit_send, sbuf, sit->sit_sendlen);
    427  1.10   mlelstv 		}
    428  1.11   mlelstv 		if (sit->sit_recv && sit->sit_recvlen <= SPI_MAXDATA) {
    429  1.10   mlelstv 			rbuf = malloc(sit->sit_recvlen, M_DEVBUF, M_WAITOK);
    430  1.10   mlelstv 		}
    431  1.10   mlelstv 		if (error == 0) {
    432  1.10   mlelstv 			if (sbuf && rbuf)
    433  1.10   mlelstv 				error = spi_send_recv(sh,
    434  1.10   mlelstv 					sit->sit_sendlen, sbuf,
    435  1.10   mlelstv 					sit->sit_recvlen, rbuf);
    436  1.10   mlelstv 			else if (sbuf)
    437  1.10   mlelstv 				error = spi_send(sh,
    438  1.10   mlelstv 					sit->sit_sendlen, sbuf);
    439  1.10   mlelstv 			else if (rbuf)
    440  1.10   mlelstv 				error = spi_recv(sh,
    441  1.10   mlelstv 					sit->sit_recvlen, rbuf);
    442  1.10   mlelstv 		}
    443  1.10   mlelstv 		if (rbuf) {
    444  1.10   mlelstv 			if (error == 0)
    445  1.10   mlelstv 				error = copyout(rbuf, sit->sit_recv,
    446  1.10   mlelstv 						sit->sit_recvlen);
    447  1.10   mlelstv 			free(rbuf, M_DEVBUF);
    448  1.10   mlelstv 		}
    449  1.10   mlelstv 		if (sbuf) {
    450  1.10   mlelstv 			free(sbuf, M_DEVBUF);
    451  1.10   mlelstv 		}
    452  1.10   mlelstv 		break;
    453  1.10   mlelstv 	default:
    454  1.10   mlelstv 		error = ENODEV;
    455  1.10   mlelstv 		break;
    456  1.10   mlelstv 	}
    457  1.10   mlelstv 
    458  1.18   mlelstv 	mutex_exit(&sc->sc_dev_lock);
    459  1.18   mlelstv 
    460  1.10   mlelstv 	return error;
    461  1.10   mlelstv }
    462  1.10   mlelstv 
    463   1.3   xtraeme CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
    464   1.1   gdamore     spi_match, spi_attach, NULL, NULL);
    465   1.1   gdamore 
    466   1.1   gdamore /*
    467   1.1   gdamore  * Configure.  This should be the first thing that the SPI driver
    468   1.1   gdamore  * should do, to configure which mode (e.g. SPI_MODE_0, which is the
    469   1.1   gdamore  * same as Philips Microwire mode), and speed.  If the bus driver
    470   1.1   gdamore  * cannot run fast enough, then it should just configure the fastest
    471   1.1   gdamore  * mode that it can support.  If the bus driver cannot run slow
    472   1.1   gdamore  * enough, then the device is incompatible and an error should be
    473   1.1   gdamore  * returned.
    474   1.1   gdamore  */
    475   1.1   gdamore int
    476  1.20   thorpej spi_configure(device_t dev __unused, struct spi_handle *sh, int mode, int speed)
    477   1.1   gdamore {
    478   1.1   gdamore 
    479  1.10   mlelstv 	sh->sh_mode = mode;
    480  1.10   mlelstv 	sh->sh_speed = speed;
    481  1.20   thorpej 
    482  1.20   thorpej 	/* No need to report errors; no failures. */
    483  1.20   thorpej 
    484  1.10   mlelstv 	return 0;
    485  1.10   mlelstv }
    486  1.10   mlelstv 
    487  1.10   mlelstv /*
    488  1.10   mlelstv  * Acquire controller
    489  1.10   mlelstv  */
    490  1.10   mlelstv static void
    491  1.10   mlelstv spi_acquire(struct spi_handle *sh)
    492  1.10   mlelstv {
    493  1.10   mlelstv 	struct spi_softc *sc = sh->sh_sc;
    494  1.10   mlelstv 
    495  1.10   mlelstv 	mutex_enter(&sc->sc_lock);
    496  1.10   mlelstv 	while ((sc->sc_flags & SPIC_BUSY) != 0)
    497  1.10   mlelstv 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    498  1.10   mlelstv 	sc->sc_flags |= SPIC_BUSY;
    499  1.10   mlelstv 	mutex_exit(&sc->sc_lock);
    500  1.10   mlelstv }
    501  1.10   mlelstv 
    502  1.10   mlelstv /*
    503  1.10   mlelstv  * Release controller
    504  1.10   mlelstv  */
    505  1.10   mlelstv static void
    506  1.10   mlelstv spi_release(struct spi_handle *sh)
    507  1.10   mlelstv {
    508  1.10   mlelstv 	struct spi_softc *sc = sh->sh_sc;
    509  1.10   mlelstv 
    510  1.10   mlelstv 	mutex_enter(&sc->sc_lock);
    511  1.10   mlelstv 	sc->sc_flags &= ~SPIC_BUSY;
    512  1.10   mlelstv 	cv_broadcast(&sc->sc_cv);
    513  1.10   mlelstv 	mutex_exit(&sc->sc_lock);
    514   1.1   gdamore }
    515   1.1   gdamore 
    516   1.1   gdamore void
    517   1.1   gdamore spi_transfer_init(struct spi_transfer *st)
    518   1.1   gdamore {
    519   1.1   gdamore 
    520  1.15    kardel 	mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_VM);
    521  1.10   mlelstv 	cv_init(&st->st_cv, "spixfr");
    522   1.5     rmind 
    523   1.1   gdamore 	st->st_flags = 0;
    524   1.1   gdamore 	st->st_errno = 0;
    525   1.1   gdamore 	st->st_done = NULL;
    526   1.1   gdamore 	st->st_chunks = NULL;
    527   1.1   gdamore 	st->st_private = NULL;
    528   1.1   gdamore 	st->st_slave = -1;
    529   1.1   gdamore }
    530   1.1   gdamore 
    531   1.1   gdamore void
    532   1.1   gdamore spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
    533   1.1   gdamore     uint8_t *rptr)
    534   1.1   gdamore {
    535   1.1   gdamore 
    536   1.1   gdamore 	chunk->chunk_write = chunk->chunk_wptr = wptr;
    537   1.6       mrg 	chunk->chunk_read = chunk->chunk_rptr = rptr;
    538   1.1   gdamore 	chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
    539   1.1   gdamore 	chunk->chunk_next = NULL;
    540   1.1   gdamore }
    541   1.1   gdamore 
    542   1.1   gdamore void
    543   1.1   gdamore spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
    544   1.1   gdamore {
    545   1.1   gdamore 	struct spi_chunk **cpp;
    546   1.1   gdamore 
    547   1.1   gdamore 	/* this is an O(n) insert -- perhaps we should use a simpleq? */
    548   1.1   gdamore 	for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
    549   1.1   gdamore 	*cpp = chunk;
    550   1.1   gdamore }
    551   1.1   gdamore 
    552   1.1   gdamore int
    553   1.1   gdamore spi_transfer(struct spi_handle *sh, struct spi_transfer *st)
    554   1.1   gdamore {
    555  1.10   mlelstv 	struct spi_softc	*sc = sh->sh_sc;
    556  1.28   thorpej 	const struct spi_controller *tag = sh->sh_controller;
    557   1.1   gdamore 	struct spi_chunk	*chunk;
    558  1.10   mlelstv 	int error;
    559   1.1   gdamore 
    560   1.1   gdamore 	/*
    561   1.1   gdamore 	 * Initialize "resid" counters and pointers, so that callers
    562   1.1   gdamore 	 * and bus drivers don't have to.
    563   1.1   gdamore 	 */
    564   1.1   gdamore 	for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
    565   1.1   gdamore 		chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
    566   1.1   gdamore 		chunk->chunk_wptr = chunk->chunk_write;
    567   1.1   gdamore 		chunk->chunk_rptr = chunk->chunk_read;
    568   1.1   gdamore 	}
    569   1.1   gdamore 
    570   1.1   gdamore 	/*
    571  1.10   mlelstv 	 * Match slave and parameters to handle
    572   1.1   gdamore 	 */
    573   1.1   gdamore 	st->st_slave = sh->sh_slave;
    574   1.1   gdamore 
    575  1.10   mlelstv 	/*
    576  1.10   mlelstv 	 * Reserve controller during transaction
    577  1.10   mlelstv  	 */
    578  1.10   mlelstv 	spi_acquire(sh);
    579  1.10   mlelstv 
    580  1.10   mlelstv 	st->st_spiprivate = (void *)sh;
    581  1.25     skrll 
    582  1.10   mlelstv 	/*
    583  1.10   mlelstv 	 * Reconfigure controller
    584  1.10   mlelstv 	 *
    585  1.10   mlelstv 	 * XXX backends don't configure per-slave parameters
    586  1.10   mlelstv 	 * Whenever we switch slaves or change mode or speed, we
    587  1.10   mlelstv 	 * need to tell the backend.
    588  1.10   mlelstv 	 */
    589  1.10   mlelstv 	if (sc->sc_slave != sh->sh_slave
    590  1.10   mlelstv 	    || sc->sc_mode != sh->sh_mode
    591  1.10   mlelstv 	    || sc->sc_speed != sh->sh_speed) {
    592  1.10   mlelstv 		error = (*tag->sct_configure)(tag->sct_cookie,
    593  1.10   mlelstv 				sh->sh_slave, sh->sh_mode, sh->sh_speed);
    594  1.10   mlelstv 		if (error)
    595  1.10   mlelstv 			return error;
    596  1.10   mlelstv 	}
    597  1.10   mlelstv 	sc->sc_mode = sh->sh_mode;
    598  1.10   mlelstv 	sc->sc_speed = sh->sh_speed;
    599  1.10   mlelstv 	sc->sc_slave = sh->sh_slave;
    600  1.10   mlelstv 
    601  1.10   mlelstv 	error = (*tag->sct_transfer)(tag->sct_cookie, st);
    602  1.10   mlelstv 
    603  1.10   mlelstv 	return error;
    604   1.1   gdamore }
    605   1.1   gdamore 
    606   1.1   gdamore void
    607   1.1   gdamore spi_wait(struct spi_transfer *st)
    608   1.1   gdamore {
    609  1.10   mlelstv 	struct spi_handle *sh = st->st_spiprivate;
    610   1.1   gdamore 
    611   1.5     rmind 	mutex_enter(&st->st_lock);
    612   1.4       jym 	while (!(st->st_flags & SPI_F_DONE)) {
    613   1.5     rmind 		cv_wait(&st->st_cv, &st->st_lock);
    614   1.1   gdamore 	}
    615   1.5     rmind 	mutex_exit(&st->st_lock);
    616   1.7  jakllsch 	cv_destroy(&st->st_cv);
    617   1.7  jakllsch 	mutex_destroy(&st->st_lock);
    618  1.10   mlelstv 
    619  1.10   mlelstv 	/*
    620  1.10   mlelstv 	 * End transaction
    621  1.10   mlelstv 	 */
    622  1.10   mlelstv 	spi_release(sh);
    623   1.1   gdamore }
    624   1.1   gdamore 
    625   1.1   gdamore void
    626   1.1   gdamore spi_done(struct spi_transfer *st, int err)
    627   1.1   gdamore {
    628   1.1   gdamore 
    629   1.5     rmind 	mutex_enter(&st->st_lock);
    630   1.1   gdamore 	if ((st->st_errno = err) != 0) {
    631   1.1   gdamore 		st->st_flags |= SPI_F_ERROR;
    632   1.1   gdamore 	}
    633   1.1   gdamore 	st->st_flags |= SPI_F_DONE;
    634   1.1   gdamore 	if (st->st_done != NULL) {
    635   1.1   gdamore 		(*st->st_done)(st);
    636   1.1   gdamore 	} else {
    637   1.5     rmind 		cv_broadcast(&st->st_cv);
    638   1.1   gdamore 	}
    639   1.5     rmind 	mutex_exit(&st->st_lock);
    640   1.1   gdamore }
    641   1.1   gdamore 
    642   1.1   gdamore /*
    643   1.1   gdamore  * Some convenience routines.  These routines block until the work
    644   1.1   gdamore  * is done.
    645   1.1   gdamore  *
    646   1.1   gdamore  * spi_recv - receives data from the bus
    647   1.1   gdamore  *
    648   1.1   gdamore  * spi_send - sends data to the bus
    649   1.1   gdamore  *
    650   1.1   gdamore  * spi_send_recv - sends data to the bus, and then receives.  Note that this is
    651   1.1   gdamore  * done synchronously, i.e. send a command and get the response.  This is
    652  1.26    andvar  * not full duplex.  If you want full duplex, you can't use these convenience
    653   1.1   gdamore  * wrappers.
    654   1.1   gdamore  */
    655   1.1   gdamore int
    656   1.1   gdamore spi_recv(struct spi_handle *sh, int cnt, uint8_t *data)
    657   1.1   gdamore {
    658   1.1   gdamore 	struct spi_transfer	trans;
    659   1.1   gdamore 	struct spi_chunk	chunk;
    660   1.1   gdamore 
    661   1.1   gdamore 	spi_transfer_init(&trans);
    662   1.1   gdamore 	spi_chunk_init(&chunk, cnt, NULL, data);
    663   1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    664   1.1   gdamore 
    665   1.1   gdamore 	/* enqueue it and wait for it to complete */
    666   1.1   gdamore 	spi_transfer(sh, &trans);
    667   1.1   gdamore 	spi_wait(&trans);
    668   1.1   gdamore 
    669   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    670   1.1   gdamore 		return trans.st_errno;
    671   1.1   gdamore 
    672   1.1   gdamore 	return 0;
    673   1.1   gdamore }
    674   1.1   gdamore 
    675   1.1   gdamore int
    676   1.1   gdamore spi_send(struct spi_handle *sh, int cnt, const uint8_t *data)
    677   1.1   gdamore {
    678   1.1   gdamore 	struct spi_transfer	trans;
    679   1.1   gdamore 	struct spi_chunk	chunk;
    680   1.1   gdamore 
    681   1.1   gdamore 	spi_transfer_init(&trans);
    682   1.1   gdamore 	spi_chunk_init(&chunk, cnt, data, NULL);
    683   1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    684   1.1   gdamore 
    685   1.1   gdamore 	/* enqueue it and wait for it to complete */
    686   1.1   gdamore 	spi_transfer(sh, &trans);
    687   1.1   gdamore 	spi_wait(&trans);
    688   1.1   gdamore 
    689   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    690   1.1   gdamore 		return trans.st_errno;
    691   1.1   gdamore 
    692   1.1   gdamore 	return 0;
    693   1.1   gdamore }
    694   1.1   gdamore 
    695   1.1   gdamore int
    696   1.1   gdamore spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
    697   1.1   gdamore     int rcnt, uint8_t *rcv)
    698   1.1   gdamore {
    699   1.1   gdamore 	struct spi_transfer	trans;
    700   1.1   gdamore 	struct spi_chunk	chunk1, chunk2;
    701   1.1   gdamore 
    702   1.1   gdamore 	spi_transfer_init(&trans);
    703   1.1   gdamore 	spi_chunk_init(&chunk1, scnt, snd, NULL);
    704   1.1   gdamore 	spi_chunk_init(&chunk2, rcnt, NULL, rcv);
    705   1.1   gdamore 	spi_transfer_add(&trans, &chunk1);
    706   1.1   gdamore 	spi_transfer_add(&trans, &chunk2);
    707   1.1   gdamore 
    708   1.1   gdamore 	/* enqueue it and wait for it to complete */
    709   1.1   gdamore 	spi_transfer(sh, &trans);
    710   1.1   gdamore 	spi_wait(&trans);
    711   1.1   gdamore 
    712   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    713   1.1   gdamore 		return trans.st_errno;
    714   1.1   gdamore 
    715   1.1   gdamore 	return 0;
    716   1.1   gdamore }
    717