Home | History | Annotate | Line # | Download | only in spi
spi.c revision 1.18
      1  1.18   mlelstv /* $NetBSD: spi.c,v 1.18 2021/05/16 08:48:20 mlelstv 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.1   gdamore #include <sys/cdefs.h>
     45  1.18   mlelstv __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.18 2021/05/16 08:48:20 mlelstv Exp $");
     46   1.1   gdamore 
     47   1.1   gdamore #include "locators.h"
     48   1.1   gdamore 
     49   1.1   gdamore #include <sys/param.h>
     50   1.1   gdamore #include <sys/systm.h>
     51   1.1   gdamore #include <sys/device.h>
     52  1.10   mlelstv #include <sys/conf.h>
     53   1.1   gdamore #include <sys/malloc.h>
     54   1.5     rmind #include <sys/mutex.h>
     55   1.5     rmind #include <sys/condvar.h>
     56   1.1   gdamore #include <sys/errno.h>
     57   1.1   gdamore 
     58   1.1   gdamore #include <dev/spi/spivar.h>
     59  1.10   mlelstv #include <dev/spi/spi_io.h>
     60  1.10   mlelstv 
     61  1.10   mlelstv #include "ioconf.h"
     62  1.10   mlelstv #include "locators.h"
     63   1.1   gdamore 
     64   1.1   gdamore struct spi_softc {
     65   1.1   gdamore 	struct spi_controller	sc_controller;
     66   1.1   gdamore 	int			sc_mode;
     67   1.1   gdamore 	int			sc_speed;
     68  1.10   mlelstv 	int			sc_slave;
     69   1.1   gdamore 	int			sc_nslaves;
     70   1.1   gdamore 	struct spi_handle	*sc_slaves;
     71  1.10   mlelstv 	kmutex_t		sc_lock;
     72  1.10   mlelstv 	kcondvar_t		sc_cv;
     73  1.18   mlelstv 	kmutex_t		sc_dev_lock;
     74  1.10   mlelstv 	int			sc_flags;
     75  1.10   mlelstv #define SPIC_BUSY		1
     76  1.10   mlelstv };
     77  1.10   mlelstv 
     78  1.10   mlelstv static dev_type_open(spi_open);
     79  1.10   mlelstv static dev_type_close(spi_close);
     80  1.10   mlelstv static dev_type_ioctl(spi_ioctl);
     81  1.10   mlelstv 
     82  1.10   mlelstv const struct cdevsw spi_cdevsw = {
     83  1.10   mlelstv 	.d_open = spi_open,
     84  1.10   mlelstv 	.d_close = spi_close,
     85  1.10   mlelstv 	.d_read = noread,
     86  1.10   mlelstv 	.d_write = nowrite,
     87  1.10   mlelstv 	.d_ioctl = spi_ioctl,
     88  1.10   mlelstv 	.d_stop = nostop,
     89  1.10   mlelstv 	.d_tty = notty,
     90  1.10   mlelstv 	.d_poll = nopoll,
     91  1.10   mlelstv 	.d_mmap = nommap,
     92  1.10   mlelstv 	.d_kqfilter = nokqfilter,
     93  1.10   mlelstv 	.d_discard = nodiscard,
     94  1.18   mlelstv 	.d_flag = D_OTHER | D_MPSAFE
     95   1.1   gdamore };
     96   1.1   gdamore 
     97   1.1   gdamore /*
     98   1.1   gdamore  * SPI slave device.  We have one of these per slave.
     99   1.1   gdamore  */
    100   1.1   gdamore struct spi_handle {
    101   1.1   gdamore 	struct spi_softc	*sh_sc;
    102   1.1   gdamore 	struct spi_controller	*sh_controller;
    103   1.1   gdamore 	int			sh_slave;
    104  1.10   mlelstv 	int			sh_mode;
    105  1.10   mlelstv 	int			sh_speed;
    106  1.12       tnn 	int			sh_flags;
    107  1.12       tnn #define SPIH_ATTACHED		1
    108   1.1   gdamore };
    109   1.1   gdamore 
    110  1.10   mlelstv #define SPI_MAXDATA 4096
    111  1.10   mlelstv 
    112   1.1   gdamore /*
    113   1.1   gdamore  * API for bus drivers.
    114   1.1   gdamore  */
    115   1.1   gdamore 
    116   1.1   gdamore int
    117   1.1   gdamore spibus_print(void *aux, const char *pnp)
    118   1.1   gdamore {
    119   1.1   gdamore 
    120   1.1   gdamore 	if (pnp != NULL)
    121   1.1   gdamore 		aprint_normal("spi at %s", pnp);
    122   1.1   gdamore 
    123   1.1   gdamore 	return (UNCONF);
    124   1.1   gdamore }
    125   1.1   gdamore 
    126   1.1   gdamore 
    127   1.1   gdamore static int
    128   1.3   xtraeme spi_match(device_t parent, cfdata_t cf, void *aux)
    129   1.1   gdamore {
    130   1.5     rmind 
    131   1.1   gdamore 	return 1;
    132   1.1   gdamore }
    133   1.1   gdamore 
    134   1.1   gdamore static int
    135   1.1   gdamore spi_print(void *aux, const char *pnp)
    136   1.1   gdamore {
    137   1.1   gdamore 	struct spi_attach_args *sa = aux;
    138   1.1   gdamore 
    139   1.1   gdamore 	if (sa->sa_handle->sh_slave != -1)
    140   1.1   gdamore 		aprint_normal(" slave %d", sa->sa_handle->sh_slave);
    141   1.1   gdamore 
    142   1.1   gdamore 	return (UNCONF);
    143   1.1   gdamore }
    144   1.1   gdamore 
    145   1.1   gdamore static int
    146   1.3   xtraeme spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    147   1.1   gdamore {
    148   1.3   xtraeme 	struct spi_softc *sc = device_private(parent);
    149   1.1   gdamore 	struct spi_attach_args sa;
    150   1.1   gdamore 	int addr;
    151   1.1   gdamore 
    152   1.1   gdamore 	addr = cf->cf_loc[SPICF_SLAVE];
    153   1.1   gdamore 	if ((addr < 0) || (addr >= sc->sc_controller.sct_nslaves)) {
    154   1.1   gdamore 		return -1;
    155   1.1   gdamore 	}
    156   1.1   gdamore 
    157  1.12       tnn 	memset(&sa, 0, sizeof sa);
    158   1.1   gdamore 	sa.sa_handle = &sc->sc_slaves[addr];
    159  1.12       tnn 	if (ISSET(sa.sa_handle->sh_flags, SPIH_ATTACHED))
    160  1.12       tnn 		return -1;
    161   1.1   gdamore 
    162  1.17   thorpej 	if (config_probe(parent, cf, &sa)) {
    163  1.12       tnn 		SET(sa.sa_handle->sh_flags, SPIH_ATTACHED);
    164  1.17   thorpej 		config_attach(parent, cf, &sa, spi_print, CFARG_EOL);
    165  1.12       tnn 	}
    166   1.1   gdamore 
    167   1.1   gdamore 	return 0;
    168   1.1   gdamore }
    169   1.1   gdamore 
    170   1.1   gdamore /*
    171  1.12       tnn  * XXX this is the same as i2c_fill_compat. It could be refactored into a
    172  1.12       tnn  * common fill_compat function with pointers to compat & ncompat instead
    173  1.12       tnn  * of attach_args as the first parameter.
    174  1.12       tnn  */
    175  1.12       tnn static void
    176  1.12       tnn spi_fill_compat(struct spi_attach_args *sa, const char *compat, size_t len,
    177  1.12       tnn 	char **buffer)
    178  1.12       tnn {
    179  1.12       tnn 	int count, i;
    180  1.12       tnn 	const char *c, *start, **ptr;
    181  1.12       tnn 
    182  1.12       tnn 	*buffer = NULL;
    183  1.12       tnn 	for (i = count = 0, c = compat; i < len; i++, c++)
    184  1.12       tnn 		if (*c == 0)
    185  1.12       tnn 			count++;
    186  1.12       tnn 	count += 2;
    187  1.12       tnn 	ptr = malloc(sizeof(char*)*count, M_TEMP, M_WAITOK);
    188  1.12       tnn 	if (!ptr)
    189  1.12       tnn 		return;
    190  1.12       tnn 
    191  1.12       tnn 	for (i = count = 0, start = c = compat; i < len; i++, c++) {
    192  1.12       tnn 		if (*c == 0) {
    193  1.12       tnn 			ptr[count++] = start;
    194  1.12       tnn 			start = c + 1;
    195  1.12       tnn 		}
    196  1.12       tnn 	}
    197  1.12       tnn 	if (start < compat + len) {
    198  1.12       tnn 		/* last string not 0 terminated */
    199  1.12       tnn 		size_t l = c - start;
    200  1.12       tnn 		*buffer = malloc(l + 1, M_TEMP, M_WAITOK);
    201  1.12       tnn 		memcpy(*buffer, start, l);
    202  1.12       tnn 		(*buffer)[l] = 0;
    203  1.12       tnn 		ptr[count++] = *buffer;
    204  1.12       tnn 	}
    205  1.12       tnn 	ptr[count] = NULL;
    206  1.12       tnn 
    207  1.12       tnn 	sa->sa_compat = ptr;
    208  1.12       tnn 	sa->sa_ncompat = count;
    209  1.12       tnn }
    210  1.12       tnn 
    211  1.12       tnn static void
    212  1.12       tnn spi_direct_attach_child_devices(device_t parent, struct spi_softc *sc,
    213  1.12       tnn     prop_array_t child_devices)
    214  1.12       tnn {
    215  1.12       tnn 	unsigned int count;
    216  1.12       tnn 	prop_dictionary_t child;
    217  1.12       tnn 	prop_data_t cdata;
    218  1.12       tnn 	uint32_t slave;
    219  1.12       tnn 	uint64_t cookie;
    220  1.12       tnn 	struct spi_attach_args sa;
    221  1.12       tnn 	int loc[SPICF_NLOCS];
    222  1.12       tnn 	char *buf;
    223  1.12       tnn 	int i;
    224  1.12       tnn 
    225  1.12       tnn 	memset(loc, 0, sizeof loc);
    226  1.12       tnn 	count = prop_array_count(child_devices);
    227  1.12       tnn 	for (i = 0; i < count; i++) {
    228  1.12       tnn 		child = prop_array_get(child_devices, i);
    229  1.12       tnn 		if (!child)
    230  1.12       tnn 			continue;
    231  1.12       tnn 		if (!prop_dictionary_get_uint32(child, "slave", &slave))
    232  1.12       tnn 			continue;
    233  1.12       tnn 		if(slave >= sc->sc_controller.sct_nslaves)
    234  1.12       tnn 			continue;
    235  1.12       tnn 		if (!prop_dictionary_get_uint64(child, "cookie", &cookie))
    236  1.12       tnn 			continue;
    237  1.12       tnn 		if (!(cdata = prop_dictionary_get(child, "compatible")))
    238  1.12       tnn 			continue;
    239  1.12       tnn 		loc[SPICF_SLAVE] = slave;
    240  1.12       tnn 
    241  1.12       tnn 		memset(&sa, 0, sizeof sa);
    242  1.12       tnn 		sa.sa_handle = &sc->sc_slaves[i];
    243  1.13   hkenken 		sa.sa_prop = child;
    244  1.13   hkenken 		sa.sa_cookie = cookie;
    245  1.12       tnn 		if (ISSET(sa.sa_handle->sh_flags, SPIH_ATTACHED))
    246  1.12       tnn 			continue;
    247  1.12       tnn 		SET(sa.sa_handle->sh_flags, SPIH_ATTACHED);
    248  1.12       tnn 
    249  1.12       tnn 		buf = NULL;
    250  1.12       tnn 		spi_fill_compat(&sa,
    251  1.14   thorpej 				prop_data_value(cdata),
    252  1.12       tnn 				prop_data_size(cdata), &buf);
    253  1.17   thorpej 		config_found(parent, &sa, spi_print,
    254  1.17   thorpej 		    CFARG_LOCATORS, loc,
    255  1.17   thorpej 		    CFARG_EOL);
    256  1.12       tnn 
    257  1.12       tnn 		if (sa.sa_compat)
    258  1.12       tnn 			free(sa.sa_compat, M_TEMP);
    259  1.12       tnn 		if (buf)
    260  1.12       tnn 			free(buf, M_TEMP);
    261  1.12       tnn 	}
    262  1.12       tnn }
    263  1.12       tnn 
    264  1.12       tnn int
    265  1.12       tnn spi_compatible_match(const struct spi_attach_args *sa, const cfdata_t cf,
    266  1.12       tnn 		     const struct device_compatible_entry *compats)
    267  1.12       tnn {
    268  1.12       tnn 	if (sa->sa_ncompat > 0)
    269  1.12       tnn 		return device_compatible_match(sa->sa_compat, sa->sa_ncompat,
    270  1.16   thorpej 					       compats);
    271  1.12       tnn 
    272  1.12       tnn 	return 1;
    273  1.12       tnn }
    274  1.12       tnn 
    275  1.12       tnn /*
    276   1.1   gdamore  * API for device drivers.
    277   1.1   gdamore  *
    278   1.1   gdamore  * We provide wrapper routines to decouple the ABI for the SPI
    279   1.1   gdamore  * device drivers from the ABI for the SPI bus drivers.
    280   1.1   gdamore  */
    281   1.1   gdamore static void
    282   1.3   xtraeme spi_attach(device_t parent, device_t self, void *aux)
    283   1.1   gdamore {
    284   1.1   gdamore 	struct spi_softc *sc = device_private(self);
    285   1.1   gdamore 	struct spibus_attach_args *sba = aux;
    286   1.1   gdamore 	int i;
    287   1.1   gdamore 
    288   1.1   gdamore 	aprint_naive(": SPI bus\n");
    289   1.1   gdamore 	aprint_normal(": SPI bus\n");
    290   1.1   gdamore 
    291  1.18   mlelstv 	mutex_init(&sc->sc_dev_lock, MUTEX_DEFAULT, IPL_NONE);
    292  1.15    kardel 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_VM);
    293  1.10   mlelstv 	cv_init(&sc->sc_cv, "spictl");
    294  1.10   mlelstv 
    295   1.1   gdamore 	sc->sc_controller = *sba->sba_controller;
    296   1.8   rkujawa 	sc->sc_nslaves = sba->sba_controller->sct_nslaves;
    297   1.1   gdamore 	/* allocate slave structures */
    298   1.1   gdamore 	sc->sc_slaves = malloc(sizeof (struct spi_handle) * sc->sc_nslaves,
    299   1.1   gdamore 	    M_DEVBUF, M_WAITOK | M_ZERO);
    300   1.1   gdamore 
    301   1.1   gdamore 	sc->sc_speed = 0;
    302   1.2   gdamore 	sc->sc_mode = -1;
    303  1.10   mlelstv 	sc->sc_slave = -1;
    304   1.1   gdamore 
    305   1.1   gdamore 	/*
    306   1.1   gdamore 	 * Initialize slave handles
    307   1.1   gdamore 	 */
    308   1.1   gdamore 	for (i = 0; i < sc->sc_nslaves; i++) {
    309   1.1   gdamore 		sc->sc_slaves[i].sh_slave = i;
    310   1.1   gdamore 		sc->sc_slaves[i].sh_sc = sc;
    311   1.1   gdamore 		sc->sc_slaves[i].sh_controller = &sc->sc_controller;
    312   1.1   gdamore 	}
    313   1.1   gdamore 
    314  1.12       tnn 	/* First attach devices known to be present via fdt */
    315  1.12       tnn 	if (sba->sba_child_devices) {
    316  1.12       tnn 		spi_direct_attach_child_devices(self, sc, sba->sba_child_devices);
    317  1.12       tnn 	}
    318  1.12       tnn 	/* Then do any other devices the user may have manually wired */
    319  1.17   thorpej 	config_search(self, NULL,
    320  1.17   thorpej 	    CFARG_SEARCH, spi_search,
    321  1.17   thorpej 	    CFARG_EOL);
    322   1.1   gdamore }
    323   1.1   gdamore 
    324  1.10   mlelstv static int
    325  1.10   mlelstv spi_open(dev_t dev, int flag, int fmt, lwp_t *l)
    326  1.10   mlelstv {
    327  1.10   mlelstv 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    328  1.10   mlelstv 
    329  1.10   mlelstv 	if (sc == NULL)
    330  1.10   mlelstv 		return ENXIO;
    331  1.10   mlelstv 
    332  1.10   mlelstv 	return 0;
    333  1.10   mlelstv }
    334  1.10   mlelstv 
    335  1.10   mlelstv static int
    336  1.10   mlelstv spi_close(dev_t dev, int flag, int fmt, lwp_t *l)
    337  1.10   mlelstv {
    338  1.10   mlelstv 
    339  1.10   mlelstv 	return 0;
    340  1.10   mlelstv }
    341  1.10   mlelstv 
    342  1.10   mlelstv static int
    343  1.10   mlelstv spi_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    344  1.10   mlelstv {
    345  1.10   mlelstv 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    346  1.10   mlelstv 	struct spi_handle *sh;
    347  1.10   mlelstv 	spi_ioctl_configure_t *sic;
    348  1.10   mlelstv 	spi_ioctl_transfer_t *sit;
    349  1.10   mlelstv 	uint8_t *sbuf, *rbuf;
    350  1.10   mlelstv 	int error;
    351  1.10   mlelstv 
    352  1.10   mlelstv 	if (sc == NULL)
    353  1.10   mlelstv 		return ENXIO;
    354  1.10   mlelstv 
    355  1.18   mlelstv 	mutex_enter(&sc->sc_dev_lock);
    356  1.18   mlelstv 
    357  1.10   mlelstv 	switch (cmd) {
    358  1.10   mlelstv 	case SPI_IOCTL_CONFIGURE:
    359  1.10   mlelstv 		sic = (spi_ioctl_configure_t *)data;
    360  1.10   mlelstv 		if (sic->sic_addr < 0 || sic->sic_addr >= sc->sc_nslaves) {
    361  1.10   mlelstv 			error = EINVAL;
    362  1.10   mlelstv 			break;
    363  1.10   mlelstv 		}
    364  1.10   mlelstv 		sh = &sc->sc_slaves[sic->sic_addr];
    365  1.10   mlelstv 		error = spi_configure(sh, sic->sic_mode, sic->sic_speed);
    366  1.10   mlelstv 		break;
    367  1.10   mlelstv 	case SPI_IOCTL_TRANSFER:
    368  1.10   mlelstv 		sit = (spi_ioctl_transfer_t *)data;
    369  1.10   mlelstv 		if (sit->sit_addr < 0 || sit->sit_addr >= sc->sc_nslaves) {
    370  1.10   mlelstv 			error = EINVAL;
    371  1.10   mlelstv 			break;
    372  1.10   mlelstv 		}
    373  1.11   mlelstv 		if ((sit->sit_send && sit->sit_sendlen == 0)
    374  1.11   mlelstv 		    || (sit->sit_recv && sit->sit_recv == 0)) {
    375  1.11   mlelstv 			error = EINVAL;
    376  1.11   mlelstv 			break;
    377  1.11   mlelstv 		}
    378  1.10   mlelstv 		sh = &sc->sc_slaves[sit->sit_addr];
    379  1.10   mlelstv 		sbuf = rbuf = NULL;
    380  1.10   mlelstv 		error = 0;
    381  1.11   mlelstv 		if (sit->sit_send && sit->sit_sendlen <= SPI_MAXDATA) {
    382  1.10   mlelstv 			sbuf = malloc(sit->sit_sendlen, M_DEVBUF, M_WAITOK);
    383  1.10   mlelstv 			error = copyin(sit->sit_send, sbuf, sit->sit_sendlen);
    384  1.10   mlelstv 		}
    385  1.11   mlelstv 		if (sit->sit_recv && sit->sit_recvlen <= SPI_MAXDATA) {
    386  1.10   mlelstv 			rbuf = malloc(sit->sit_recvlen, M_DEVBUF, M_WAITOK);
    387  1.10   mlelstv 		}
    388  1.10   mlelstv 		if (error == 0) {
    389  1.10   mlelstv 			if (sbuf && rbuf)
    390  1.10   mlelstv 				error = spi_send_recv(sh,
    391  1.10   mlelstv 					sit->sit_sendlen, sbuf,
    392  1.10   mlelstv 					sit->sit_recvlen, rbuf);
    393  1.10   mlelstv 			else if (sbuf)
    394  1.10   mlelstv 				error = spi_send(sh,
    395  1.10   mlelstv 					sit->sit_sendlen, sbuf);
    396  1.10   mlelstv 			else if (rbuf)
    397  1.10   mlelstv 				error = spi_recv(sh,
    398  1.10   mlelstv 					sit->sit_recvlen, rbuf);
    399  1.10   mlelstv 		}
    400  1.10   mlelstv 		if (rbuf) {
    401  1.10   mlelstv 			if (error == 0)
    402  1.10   mlelstv 				error = copyout(rbuf, sit->sit_recv,
    403  1.10   mlelstv 						sit->sit_recvlen);
    404  1.10   mlelstv 			free(rbuf, M_DEVBUF);
    405  1.10   mlelstv 		}
    406  1.10   mlelstv 		if (sbuf) {
    407  1.10   mlelstv 			free(sbuf, M_DEVBUF);
    408  1.10   mlelstv 		}
    409  1.10   mlelstv 		break;
    410  1.10   mlelstv 	default:
    411  1.10   mlelstv 		error = ENODEV;
    412  1.10   mlelstv 		break;
    413  1.10   mlelstv 	}
    414  1.10   mlelstv 
    415  1.18   mlelstv 	mutex_exit(&sc->sc_dev_lock);
    416  1.18   mlelstv 
    417  1.10   mlelstv 	return error;
    418  1.10   mlelstv }
    419  1.10   mlelstv 
    420   1.3   xtraeme CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
    421   1.1   gdamore     spi_match, spi_attach, NULL, NULL);
    422   1.1   gdamore 
    423   1.1   gdamore /*
    424   1.1   gdamore  * Configure.  This should be the first thing that the SPI driver
    425   1.1   gdamore  * should do, to configure which mode (e.g. SPI_MODE_0, which is the
    426   1.1   gdamore  * same as Philips Microwire mode), and speed.  If the bus driver
    427   1.1   gdamore  * cannot run fast enough, then it should just configure the fastest
    428   1.1   gdamore  * mode that it can support.  If the bus driver cannot run slow
    429   1.1   gdamore  * enough, then the device is incompatible and an error should be
    430   1.1   gdamore  * returned.
    431   1.1   gdamore  */
    432   1.1   gdamore int
    433   1.1   gdamore spi_configure(struct spi_handle *sh, int mode, int speed)
    434   1.1   gdamore {
    435   1.1   gdamore 
    436  1.10   mlelstv 	sh->sh_mode = mode;
    437  1.10   mlelstv 	sh->sh_speed = speed;
    438  1.10   mlelstv 	return 0;
    439  1.10   mlelstv }
    440  1.10   mlelstv 
    441  1.10   mlelstv /*
    442  1.10   mlelstv  * Acquire controller
    443  1.10   mlelstv  */
    444  1.10   mlelstv static void
    445  1.10   mlelstv spi_acquire(struct spi_handle *sh)
    446  1.10   mlelstv {
    447  1.10   mlelstv 	struct spi_softc *sc = sh->sh_sc;
    448  1.10   mlelstv 
    449  1.10   mlelstv 	mutex_enter(&sc->sc_lock);
    450  1.10   mlelstv 	while ((sc->sc_flags & SPIC_BUSY) != 0)
    451  1.10   mlelstv 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    452  1.10   mlelstv 	sc->sc_flags |= SPIC_BUSY;
    453  1.10   mlelstv 	mutex_exit(&sc->sc_lock);
    454  1.10   mlelstv }
    455  1.10   mlelstv 
    456  1.10   mlelstv /*
    457  1.10   mlelstv  * Release controller
    458  1.10   mlelstv  */
    459  1.10   mlelstv static void
    460  1.10   mlelstv spi_release(struct spi_handle *sh)
    461  1.10   mlelstv {
    462  1.10   mlelstv 	struct spi_softc *sc = sh->sh_sc;
    463  1.10   mlelstv 
    464  1.10   mlelstv 	mutex_enter(&sc->sc_lock);
    465  1.10   mlelstv 	sc->sc_flags &= ~SPIC_BUSY;
    466  1.10   mlelstv 	cv_broadcast(&sc->sc_cv);
    467  1.10   mlelstv 	mutex_exit(&sc->sc_lock);
    468   1.1   gdamore }
    469   1.1   gdamore 
    470   1.1   gdamore void
    471   1.1   gdamore spi_transfer_init(struct spi_transfer *st)
    472   1.1   gdamore {
    473   1.1   gdamore 
    474  1.15    kardel 	mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_VM);
    475  1.10   mlelstv 	cv_init(&st->st_cv, "spixfr");
    476   1.5     rmind 
    477   1.1   gdamore 	st->st_flags = 0;
    478   1.1   gdamore 	st->st_errno = 0;
    479   1.1   gdamore 	st->st_done = NULL;
    480   1.1   gdamore 	st->st_chunks = NULL;
    481   1.1   gdamore 	st->st_private = NULL;
    482   1.1   gdamore 	st->st_slave = -1;
    483   1.1   gdamore }
    484   1.1   gdamore 
    485   1.1   gdamore void
    486   1.1   gdamore spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
    487   1.1   gdamore     uint8_t *rptr)
    488   1.1   gdamore {
    489   1.1   gdamore 
    490   1.1   gdamore 	chunk->chunk_write = chunk->chunk_wptr = wptr;
    491   1.6       mrg 	chunk->chunk_read = chunk->chunk_rptr = rptr;
    492   1.1   gdamore 	chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
    493   1.1   gdamore 	chunk->chunk_next = NULL;
    494   1.1   gdamore }
    495   1.1   gdamore 
    496   1.1   gdamore void
    497   1.1   gdamore spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
    498   1.1   gdamore {
    499   1.1   gdamore 	struct spi_chunk **cpp;
    500   1.1   gdamore 
    501   1.1   gdamore 	/* this is an O(n) insert -- perhaps we should use a simpleq? */
    502   1.1   gdamore 	for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
    503   1.1   gdamore 	*cpp = chunk;
    504   1.1   gdamore }
    505   1.1   gdamore 
    506   1.1   gdamore int
    507   1.1   gdamore spi_transfer(struct spi_handle *sh, struct spi_transfer *st)
    508   1.1   gdamore {
    509  1.10   mlelstv 	struct spi_softc	*sc = sh->sh_sc;
    510   1.1   gdamore 	struct spi_controller	*tag = sh->sh_controller;
    511   1.1   gdamore 	struct spi_chunk	*chunk;
    512  1.10   mlelstv 	int error;
    513   1.1   gdamore 
    514   1.1   gdamore 	/*
    515   1.1   gdamore 	 * Initialize "resid" counters and pointers, so that callers
    516   1.1   gdamore 	 * and bus drivers don't have to.
    517   1.1   gdamore 	 */
    518   1.1   gdamore 	for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
    519   1.1   gdamore 		chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
    520   1.1   gdamore 		chunk->chunk_wptr = chunk->chunk_write;
    521   1.1   gdamore 		chunk->chunk_rptr = chunk->chunk_read;
    522   1.1   gdamore 	}
    523   1.1   gdamore 
    524   1.1   gdamore 	/*
    525  1.10   mlelstv 	 * Match slave and parameters to handle
    526   1.1   gdamore 	 */
    527   1.1   gdamore 	st->st_slave = sh->sh_slave;
    528   1.1   gdamore 
    529  1.10   mlelstv 	/*
    530  1.10   mlelstv 	 * Reserve controller during transaction
    531  1.10   mlelstv  	 */
    532  1.10   mlelstv 	spi_acquire(sh);
    533  1.10   mlelstv 
    534  1.10   mlelstv 	st->st_spiprivate = (void *)sh;
    535  1.10   mlelstv 
    536  1.10   mlelstv 	/*
    537  1.10   mlelstv 	 * Reconfigure controller
    538  1.10   mlelstv 	 *
    539  1.10   mlelstv 	 * XXX backends don't configure per-slave parameters
    540  1.10   mlelstv 	 * Whenever we switch slaves or change mode or speed, we
    541  1.10   mlelstv 	 * need to tell the backend.
    542  1.10   mlelstv 	 */
    543  1.10   mlelstv 	if (sc->sc_slave != sh->sh_slave
    544  1.10   mlelstv 	    || sc->sc_mode != sh->sh_mode
    545  1.10   mlelstv 	    || sc->sc_speed != sh->sh_speed) {
    546  1.10   mlelstv 		error = (*tag->sct_configure)(tag->sct_cookie,
    547  1.10   mlelstv 				sh->sh_slave, sh->sh_mode, sh->sh_speed);
    548  1.10   mlelstv 		if (error)
    549  1.10   mlelstv 			return error;
    550  1.10   mlelstv 	}
    551  1.10   mlelstv 	sc->sc_mode = sh->sh_mode;
    552  1.10   mlelstv 	sc->sc_speed = sh->sh_speed;
    553  1.10   mlelstv 	sc->sc_slave = sh->sh_slave;
    554  1.10   mlelstv 
    555  1.10   mlelstv 	error = (*tag->sct_transfer)(tag->sct_cookie, st);
    556  1.10   mlelstv 
    557  1.10   mlelstv 	return error;
    558   1.1   gdamore }
    559   1.1   gdamore 
    560   1.1   gdamore void
    561   1.1   gdamore spi_wait(struct spi_transfer *st)
    562   1.1   gdamore {
    563  1.10   mlelstv 	struct spi_handle *sh = st->st_spiprivate;
    564   1.1   gdamore 
    565   1.5     rmind 	mutex_enter(&st->st_lock);
    566   1.4       jym 	while (!(st->st_flags & SPI_F_DONE)) {
    567   1.5     rmind 		cv_wait(&st->st_cv, &st->st_lock);
    568   1.1   gdamore 	}
    569   1.5     rmind 	mutex_exit(&st->st_lock);
    570   1.7  jakllsch 	cv_destroy(&st->st_cv);
    571   1.7  jakllsch 	mutex_destroy(&st->st_lock);
    572  1.10   mlelstv 
    573  1.10   mlelstv 	/*
    574  1.10   mlelstv 	 * End transaction
    575  1.10   mlelstv 	 */
    576  1.10   mlelstv 	spi_release(sh);
    577   1.1   gdamore }
    578   1.1   gdamore 
    579   1.1   gdamore void
    580   1.1   gdamore spi_done(struct spi_transfer *st, int err)
    581   1.1   gdamore {
    582   1.1   gdamore 
    583   1.5     rmind 	mutex_enter(&st->st_lock);
    584   1.1   gdamore 	if ((st->st_errno = err) != 0) {
    585   1.1   gdamore 		st->st_flags |= SPI_F_ERROR;
    586   1.1   gdamore 	}
    587   1.1   gdamore 	st->st_flags |= SPI_F_DONE;
    588   1.1   gdamore 	if (st->st_done != NULL) {
    589   1.1   gdamore 		(*st->st_done)(st);
    590   1.1   gdamore 	} else {
    591   1.5     rmind 		cv_broadcast(&st->st_cv);
    592   1.1   gdamore 	}
    593   1.5     rmind 	mutex_exit(&st->st_lock);
    594   1.1   gdamore }
    595   1.1   gdamore 
    596   1.1   gdamore /*
    597   1.1   gdamore  * Some convenience routines.  These routines block until the work
    598   1.1   gdamore  * is done.
    599   1.1   gdamore  *
    600   1.1   gdamore  * spi_recv - receives data from the bus
    601   1.1   gdamore  *
    602   1.1   gdamore  * spi_send - sends data to the bus
    603   1.1   gdamore  *
    604   1.1   gdamore  * spi_send_recv - sends data to the bus, and then receives.  Note that this is
    605   1.1   gdamore  * done synchronously, i.e. send a command and get the response.  This is
    606   1.1   gdamore  * not full duplex.  If you wnat full duplex, you can't use these convenience
    607   1.1   gdamore  * wrappers.
    608   1.1   gdamore  */
    609   1.1   gdamore int
    610   1.1   gdamore spi_recv(struct spi_handle *sh, int cnt, uint8_t *data)
    611   1.1   gdamore {
    612   1.1   gdamore 	struct spi_transfer	trans;
    613   1.1   gdamore 	struct spi_chunk	chunk;
    614   1.1   gdamore 
    615   1.1   gdamore 	spi_transfer_init(&trans);
    616   1.1   gdamore 	spi_chunk_init(&chunk, cnt, NULL, data);
    617   1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    618   1.1   gdamore 
    619   1.1   gdamore 	/* enqueue it and wait for it to complete */
    620   1.1   gdamore 	spi_transfer(sh, &trans);
    621   1.1   gdamore 	spi_wait(&trans);
    622   1.1   gdamore 
    623   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    624   1.1   gdamore 		return trans.st_errno;
    625   1.1   gdamore 
    626   1.1   gdamore 	return 0;
    627   1.1   gdamore }
    628   1.1   gdamore 
    629   1.1   gdamore int
    630   1.1   gdamore spi_send(struct spi_handle *sh, int cnt, const uint8_t *data)
    631   1.1   gdamore {
    632   1.1   gdamore 	struct spi_transfer	trans;
    633   1.1   gdamore 	struct spi_chunk	chunk;
    634   1.1   gdamore 
    635   1.1   gdamore 	spi_transfer_init(&trans);
    636   1.1   gdamore 	spi_chunk_init(&chunk, cnt, data, NULL);
    637   1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    638   1.1   gdamore 
    639   1.1   gdamore 	/* enqueue it and wait for it to complete */
    640   1.1   gdamore 	spi_transfer(sh, &trans);
    641   1.1   gdamore 	spi_wait(&trans);
    642   1.1   gdamore 
    643   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    644   1.1   gdamore 		return trans.st_errno;
    645   1.1   gdamore 
    646   1.1   gdamore 	return 0;
    647   1.1   gdamore }
    648   1.1   gdamore 
    649   1.1   gdamore int
    650   1.1   gdamore spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
    651   1.1   gdamore     int rcnt, uint8_t *rcv)
    652   1.1   gdamore {
    653   1.1   gdamore 	struct spi_transfer	trans;
    654   1.1   gdamore 	struct spi_chunk	chunk1, chunk2;
    655   1.1   gdamore 
    656   1.1   gdamore 	spi_transfer_init(&trans);
    657   1.1   gdamore 	spi_chunk_init(&chunk1, scnt, snd, NULL);
    658   1.1   gdamore 	spi_chunk_init(&chunk2, rcnt, NULL, rcv);
    659   1.1   gdamore 	spi_transfer_add(&trans, &chunk1);
    660   1.1   gdamore 	spi_transfer_add(&trans, &chunk2);
    661   1.1   gdamore 
    662   1.1   gdamore 	/* enqueue it and wait for it to complete */
    663   1.1   gdamore 	spi_transfer(sh, &trans);
    664   1.1   gdamore 	spi_wait(&trans);
    665   1.1   gdamore 
    666   1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    667   1.1   gdamore 		return trans.st_errno;
    668   1.1   gdamore 
    669   1.1   gdamore 	return 0;
    670   1.1   gdamore }
    671  1.10   mlelstv 
    672