Home | History | Annotate | Line # | Download | only in spi
spi.c revision 1.8.38.1
      1  1.8.38.1  christos /* $NetBSD: spi.c,v 1.8.38.1 2019/06/10 22:07:32 christos 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.8.38.1  christos __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.8.38.1 2019/06/10 22:07:32 christos 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.8.38.1  christos #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.8.38.1  christos #include <dev/spi/spi_io.h>
     60  1.8.38.1  christos 
     61  1.8.38.1  christos #include "ioconf.h"
     62  1.8.38.1  christos #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.8.38.1  christos 	int			sc_slave;
     69       1.1   gdamore 	int			sc_nslaves;
     70       1.1   gdamore 	struct spi_handle	*sc_slaves;
     71  1.8.38.1  christos 	kmutex_t		sc_lock;
     72  1.8.38.1  christos 	kcondvar_t		sc_cv;
     73  1.8.38.1  christos 	int			sc_flags;
     74  1.8.38.1  christos #define SPIC_BUSY		1
     75  1.8.38.1  christos };
     76  1.8.38.1  christos 
     77  1.8.38.1  christos static dev_type_open(spi_open);
     78  1.8.38.1  christos static dev_type_close(spi_close);
     79  1.8.38.1  christos static dev_type_ioctl(spi_ioctl);
     80  1.8.38.1  christos 
     81  1.8.38.1  christos const struct cdevsw spi_cdevsw = {
     82  1.8.38.1  christos 	.d_open = spi_open,
     83  1.8.38.1  christos 	.d_close = spi_close,
     84  1.8.38.1  christos 	.d_read = noread,
     85  1.8.38.1  christos 	.d_write = nowrite,
     86  1.8.38.1  christos 	.d_ioctl = spi_ioctl,
     87  1.8.38.1  christos 	.d_stop = nostop,
     88  1.8.38.1  christos 	.d_tty = notty,
     89  1.8.38.1  christos 	.d_poll = nopoll,
     90  1.8.38.1  christos 	.d_mmap = nommap,
     91  1.8.38.1  christos 	.d_kqfilter = nokqfilter,
     92  1.8.38.1  christos 	.d_discard = nodiscard,
     93  1.8.38.1  christos 	.d_flag = D_OTHER
     94       1.1   gdamore };
     95       1.1   gdamore 
     96       1.1   gdamore /*
     97       1.1   gdamore  * SPI slave device.  We have one of these per slave.
     98       1.1   gdamore  */
     99       1.1   gdamore struct spi_handle {
    100       1.1   gdamore 	struct spi_softc	*sh_sc;
    101       1.1   gdamore 	struct spi_controller	*sh_controller;
    102       1.1   gdamore 	int			sh_slave;
    103  1.8.38.1  christos 	int			sh_mode;
    104  1.8.38.1  christos 	int			sh_speed;
    105       1.1   gdamore };
    106       1.1   gdamore 
    107  1.8.38.1  christos #define SPI_MAXDATA 4096
    108  1.8.38.1  christos 
    109       1.1   gdamore /*
    110       1.1   gdamore  * API for bus drivers.
    111       1.1   gdamore  */
    112       1.1   gdamore 
    113       1.1   gdamore int
    114       1.1   gdamore spibus_print(void *aux, const char *pnp)
    115       1.1   gdamore {
    116       1.1   gdamore 
    117       1.1   gdamore 	if (pnp != NULL)
    118       1.1   gdamore 		aprint_normal("spi at %s", pnp);
    119       1.1   gdamore 
    120       1.1   gdamore 	return (UNCONF);
    121       1.1   gdamore }
    122       1.1   gdamore 
    123       1.1   gdamore 
    124       1.1   gdamore static int
    125       1.3   xtraeme spi_match(device_t parent, cfdata_t cf, void *aux)
    126       1.1   gdamore {
    127       1.5     rmind 
    128       1.1   gdamore 	return 1;
    129       1.1   gdamore }
    130       1.1   gdamore 
    131       1.1   gdamore static int
    132       1.1   gdamore spi_print(void *aux, const char *pnp)
    133       1.1   gdamore {
    134       1.1   gdamore 	struct spi_attach_args *sa = aux;
    135       1.1   gdamore 
    136       1.1   gdamore 	if (sa->sa_handle->sh_slave != -1)
    137       1.1   gdamore 		aprint_normal(" slave %d", sa->sa_handle->sh_slave);
    138       1.1   gdamore 
    139       1.1   gdamore 	return (UNCONF);
    140       1.1   gdamore }
    141       1.1   gdamore 
    142       1.1   gdamore static int
    143       1.3   xtraeme spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    144       1.1   gdamore {
    145       1.3   xtraeme 	struct spi_softc *sc = device_private(parent);
    146       1.1   gdamore 	struct spi_attach_args sa;
    147       1.1   gdamore 	int addr;
    148       1.1   gdamore 
    149       1.1   gdamore 	addr = cf->cf_loc[SPICF_SLAVE];
    150       1.1   gdamore 	if ((addr < 0) || (addr >= sc->sc_controller.sct_nslaves)) {
    151       1.1   gdamore 		return -1;
    152       1.1   gdamore 	}
    153       1.1   gdamore 
    154       1.1   gdamore 	sa.sa_handle = &sc->sc_slaves[addr];
    155       1.1   gdamore 
    156       1.1   gdamore 	if (config_match(parent, cf, &sa) > 0)
    157       1.1   gdamore 		config_attach(parent, cf, &sa, spi_print);
    158       1.1   gdamore 
    159       1.1   gdamore 	return 0;
    160       1.1   gdamore }
    161       1.1   gdamore 
    162       1.1   gdamore /*
    163       1.1   gdamore  * API for device drivers.
    164       1.1   gdamore  *
    165       1.1   gdamore  * We provide wrapper routines to decouple the ABI for the SPI
    166       1.1   gdamore  * device drivers from the ABI for the SPI bus drivers.
    167       1.1   gdamore  */
    168       1.1   gdamore static void
    169       1.3   xtraeme spi_attach(device_t parent, device_t self, void *aux)
    170       1.1   gdamore {
    171       1.1   gdamore 	struct spi_softc *sc = device_private(self);
    172       1.1   gdamore 	struct spibus_attach_args *sba = aux;
    173       1.1   gdamore 	int i;
    174       1.1   gdamore 
    175       1.1   gdamore 	aprint_naive(": SPI bus\n");
    176       1.1   gdamore 	aprint_normal(": SPI bus\n");
    177       1.1   gdamore 
    178  1.8.38.1  christos 	mutex_init(&sc->sc_lock, MUTEX_DEFAULT, IPL_BIO);
    179  1.8.38.1  christos 	cv_init(&sc->sc_cv, "spictl");
    180  1.8.38.1  christos 
    181       1.1   gdamore 	sc->sc_controller = *sba->sba_controller;
    182       1.8   rkujawa 	sc->sc_nslaves = sba->sba_controller->sct_nslaves;
    183       1.1   gdamore 	/* allocate slave structures */
    184       1.1   gdamore 	sc->sc_slaves = malloc(sizeof (struct spi_handle) * sc->sc_nslaves,
    185       1.1   gdamore 	    M_DEVBUF, M_WAITOK | M_ZERO);
    186       1.1   gdamore 
    187       1.1   gdamore 	sc->sc_speed = 0;
    188       1.2   gdamore 	sc->sc_mode = -1;
    189  1.8.38.1  christos 	sc->sc_slave = -1;
    190       1.1   gdamore 
    191       1.1   gdamore 	/*
    192       1.1   gdamore 	 * Initialize slave handles
    193       1.1   gdamore 	 */
    194       1.1   gdamore 	for (i = 0; i < sc->sc_nslaves; i++) {
    195       1.1   gdamore 		sc->sc_slaves[i].sh_slave = i;
    196       1.1   gdamore 		sc->sc_slaves[i].sh_sc = sc;
    197       1.1   gdamore 		sc->sc_slaves[i].sh_controller = &sc->sc_controller;
    198       1.1   gdamore 	}
    199       1.1   gdamore 
    200       1.1   gdamore 	/*
    201       1.1   gdamore 	 * Locate and attach child devices
    202       1.1   gdamore 	 */
    203       1.1   gdamore 	config_search_ia(spi_search, self, "spi", NULL);
    204       1.1   gdamore }
    205       1.1   gdamore 
    206  1.8.38.1  christos static int
    207  1.8.38.1  christos spi_open(dev_t dev, int flag, int fmt, lwp_t *l)
    208  1.8.38.1  christos {
    209  1.8.38.1  christos 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    210  1.8.38.1  christos 
    211  1.8.38.1  christos 	if (sc == NULL)
    212  1.8.38.1  christos 		return ENXIO;
    213  1.8.38.1  christos 
    214  1.8.38.1  christos 	return 0;
    215  1.8.38.1  christos }
    216  1.8.38.1  christos 
    217  1.8.38.1  christos static int
    218  1.8.38.1  christos spi_close(dev_t dev, int flag, int fmt, lwp_t *l)
    219  1.8.38.1  christos {
    220  1.8.38.1  christos 
    221  1.8.38.1  christos 	return 0;
    222  1.8.38.1  christos }
    223  1.8.38.1  christos 
    224  1.8.38.1  christos static int
    225  1.8.38.1  christos spi_ioctl(dev_t dev, u_long cmd, void *data, int flag, lwp_t *l)
    226  1.8.38.1  christos {
    227  1.8.38.1  christos 	struct spi_softc *sc = device_lookup_private(&spi_cd, minor(dev));
    228  1.8.38.1  christos 	struct spi_handle *sh;
    229  1.8.38.1  christos 	spi_ioctl_configure_t *sic;
    230  1.8.38.1  christos 	spi_ioctl_transfer_t *sit;
    231  1.8.38.1  christos 	uint8_t *sbuf, *rbuf;
    232  1.8.38.1  christos 	int error;
    233  1.8.38.1  christos 
    234  1.8.38.1  christos 	if (sc == NULL)
    235  1.8.38.1  christos 		return ENXIO;
    236  1.8.38.1  christos 
    237  1.8.38.1  christos 	switch (cmd) {
    238  1.8.38.1  christos 	case SPI_IOCTL_CONFIGURE:
    239  1.8.38.1  christos 		sic = (spi_ioctl_configure_t *)data;
    240  1.8.38.1  christos 		if (sic->sic_addr < 0 || sic->sic_addr >= sc->sc_nslaves) {
    241  1.8.38.1  christos 			error = EINVAL;
    242  1.8.38.1  christos 			break;
    243  1.8.38.1  christos 		}
    244  1.8.38.1  christos 		sh = &sc->sc_slaves[sic->sic_addr];
    245  1.8.38.1  christos 		error = spi_configure(sh, sic->sic_mode, sic->sic_speed);
    246  1.8.38.1  christos 		break;
    247  1.8.38.1  christos 	case SPI_IOCTL_TRANSFER:
    248  1.8.38.1  christos 		sit = (spi_ioctl_transfer_t *)data;
    249  1.8.38.1  christos 		if (sit->sit_addr < 0 || sit->sit_addr >= sc->sc_nslaves) {
    250  1.8.38.1  christos 			error = EINVAL;
    251  1.8.38.1  christos 			break;
    252  1.8.38.1  christos 		}
    253  1.8.38.1  christos 		if ((sit->sit_send && sit->sit_sendlen == 0)
    254  1.8.38.1  christos 		    || (sit->sit_recv && sit->sit_recv == 0)) {
    255  1.8.38.1  christos 			error = EINVAL;
    256  1.8.38.1  christos 			break;
    257  1.8.38.1  christos 		}
    258  1.8.38.1  christos 		sh = &sc->sc_slaves[sit->sit_addr];
    259  1.8.38.1  christos 		sbuf = rbuf = NULL;
    260  1.8.38.1  christos 		error = 0;
    261  1.8.38.1  christos 		if (sit->sit_send && sit->sit_sendlen <= SPI_MAXDATA) {
    262  1.8.38.1  christos 			sbuf = malloc(sit->sit_sendlen, M_DEVBUF, M_WAITOK);
    263  1.8.38.1  christos 			error = copyin(sit->sit_send, sbuf, sit->sit_sendlen);
    264  1.8.38.1  christos 		}
    265  1.8.38.1  christos 		if (sit->sit_recv && sit->sit_recvlen <= SPI_MAXDATA) {
    266  1.8.38.1  christos 			rbuf = malloc(sit->sit_recvlen, M_DEVBUF, M_WAITOK);
    267  1.8.38.1  christos 		}
    268  1.8.38.1  christos 		if (error == 0) {
    269  1.8.38.1  christos 			if (sbuf && rbuf)
    270  1.8.38.1  christos 				error = spi_send_recv(sh,
    271  1.8.38.1  christos 					sit->sit_sendlen, sbuf,
    272  1.8.38.1  christos 					sit->sit_recvlen, rbuf);
    273  1.8.38.1  christos 			else if (sbuf)
    274  1.8.38.1  christos 				error = spi_send(sh,
    275  1.8.38.1  christos 					sit->sit_sendlen, sbuf);
    276  1.8.38.1  christos 			else if (rbuf)
    277  1.8.38.1  christos 				error = spi_recv(sh,
    278  1.8.38.1  christos 					sit->sit_recvlen, rbuf);
    279  1.8.38.1  christos 		}
    280  1.8.38.1  christos 		if (rbuf) {
    281  1.8.38.1  christos 			if (error == 0)
    282  1.8.38.1  christos 				error = copyout(rbuf, sit->sit_recv,
    283  1.8.38.1  christos 						sit->sit_recvlen);
    284  1.8.38.1  christos 			free(rbuf, M_DEVBUF);
    285  1.8.38.1  christos 		}
    286  1.8.38.1  christos 		if (sbuf) {
    287  1.8.38.1  christos 			free(sbuf, M_DEVBUF);
    288  1.8.38.1  christos 		}
    289  1.8.38.1  christos 		break;
    290  1.8.38.1  christos 	default:
    291  1.8.38.1  christos 		error = ENODEV;
    292  1.8.38.1  christos 		break;
    293  1.8.38.1  christos 	}
    294  1.8.38.1  christos 
    295  1.8.38.1  christos 	return error;
    296  1.8.38.1  christos }
    297  1.8.38.1  christos 
    298       1.3   xtraeme CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
    299       1.1   gdamore     spi_match, spi_attach, NULL, NULL);
    300       1.1   gdamore 
    301       1.1   gdamore /*
    302       1.1   gdamore  * Configure.  This should be the first thing that the SPI driver
    303       1.1   gdamore  * should do, to configure which mode (e.g. SPI_MODE_0, which is the
    304       1.1   gdamore  * same as Philips Microwire mode), and speed.  If the bus driver
    305       1.1   gdamore  * cannot run fast enough, then it should just configure the fastest
    306       1.1   gdamore  * mode that it can support.  If the bus driver cannot run slow
    307       1.1   gdamore  * enough, then the device is incompatible and an error should be
    308       1.1   gdamore  * returned.
    309       1.1   gdamore  */
    310       1.1   gdamore int
    311       1.1   gdamore spi_configure(struct spi_handle *sh, int mode, int speed)
    312       1.1   gdamore {
    313       1.1   gdamore 
    314  1.8.38.1  christos 	sh->sh_mode = mode;
    315  1.8.38.1  christos 	sh->sh_speed = speed;
    316  1.8.38.1  christos 	return 0;
    317  1.8.38.1  christos }
    318  1.8.38.1  christos 
    319  1.8.38.1  christos /*
    320  1.8.38.1  christos  * Acquire controller
    321  1.8.38.1  christos  */
    322  1.8.38.1  christos static void
    323  1.8.38.1  christos spi_acquire(struct spi_handle *sh)
    324  1.8.38.1  christos {
    325  1.8.38.1  christos 	struct spi_softc *sc = sh->sh_sc;
    326  1.8.38.1  christos 
    327  1.8.38.1  christos 	mutex_enter(&sc->sc_lock);
    328  1.8.38.1  christos 	while ((sc->sc_flags & SPIC_BUSY) != 0)
    329  1.8.38.1  christos 		cv_wait(&sc->sc_cv, &sc->sc_lock);
    330  1.8.38.1  christos 	sc->sc_flags |= SPIC_BUSY;
    331  1.8.38.1  christos 	mutex_exit(&sc->sc_lock);
    332  1.8.38.1  christos }
    333  1.8.38.1  christos 
    334  1.8.38.1  christos /*
    335  1.8.38.1  christos  * Release controller
    336  1.8.38.1  christos  */
    337  1.8.38.1  christos static void
    338  1.8.38.1  christos spi_release(struct spi_handle *sh)
    339  1.8.38.1  christos {
    340  1.8.38.1  christos 	struct spi_softc *sc = sh->sh_sc;
    341  1.8.38.1  christos 
    342  1.8.38.1  christos 	mutex_enter(&sc->sc_lock);
    343  1.8.38.1  christos 	sc->sc_flags &= ~SPIC_BUSY;
    344  1.8.38.1  christos 	cv_broadcast(&sc->sc_cv);
    345  1.8.38.1  christos 	mutex_exit(&sc->sc_lock);
    346       1.1   gdamore }
    347       1.1   gdamore 
    348       1.1   gdamore void
    349       1.1   gdamore spi_transfer_init(struct spi_transfer *st)
    350       1.1   gdamore {
    351       1.1   gdamore 
    352       1.5     rmind 	mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_BIO);
    353  1.8.38.1  christos 	cv_init(&st->st_cv, "spixfr");
    354       1.5     rmind 
    355       1.1   gdamore 	st->st_flags = 0;
    356       1.1   gdamore 	st->st_errno = 0;
    357       1.1   gdamore 	st->st_done = NULL;
    358       1.1   gdamore 	st->st_chunks = NULL;
    359       1.1   gdamore 	st->st_private = NULL;
    360       1.1   gdamore 	st->st_slave = -1;
    361       1.1   gdamore }
    362       1.1   gdamore 
    363       1.1   gdamore void
    364       1.1   gdamore spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
    365       1.1   gdamore     uint8_t *rptr)
    366       1.1   gdamore {
    367       1.1   gdamore 
    368       1.1   gdamore 	chunk->chunk_write = chunk->chunk_wptr = wptr;
    369       1.6       mrg 	chunk->chunk_read = chunk->chunk_rptr = rptr;
    370       1.1   gdamore 	chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
    371       1.1   gdamore 	chunk->chunk_next = NULL;
    372       1.1   gdamore }
    373       1.1   gdamore 
    374       1.1   gdamore void
    375       1.1   gdamore spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
    376       1.1   gdamore {
    377       1.1   gdamore 	struct spi_chunk **cpp;
    378       1.1   gdamore 
    379       1.1   gdamore 	/* this is an O(n) insert -- perhaps we should use a simpleq? */
    380       1.1   gdamore 	for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
    381       1.1   gdamore 	*cpp = chunk;
    382       1.1   gdamore }
    383       1.1   gdamore 
    384       1.1   gdamore int
    385       1.1   gdamore spi_transfer(struct spi_handle *sh, struct spi_transfer *st)
    386       1.1   gdamore {
    387  1.8.38.1  christos 	struct spi_softc	*sc = sh->sh_sc;
    388       1.1   gdamore 	struct spi_controller	*tag = sh->sh_controller;
    389       1.1   gdamore 	struct spi_chunk	*chunk;
    390  1.8.38.1  christos 	int error;
    391       1.1   gdamore 
    392       1.1   gdamore 	/*
    393       1.1   gdamore 	 * Initialize "resid" counters and pointers, so that callers
    394       1.1   gdamore 	 * and bus drivers don't have to.
    395       1.1   gdamore 	 */
    396       1.1   gdamore 	for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
    397       1.1   gdamore 		chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
    398       1.1   gdamore 		chunk->chunk_wptr = chunk->chunk_write;
    399       1.1   gdamore 		chunk->chunk_rptr = chunk->chunk_read;
    400       1.1   gdamore 	}
    401       1.1   gdamore 
    402       1.1   gdamore 	/*
    403  1.8.38.1  christos 	 * Match slave and parameters to handle
    404       1.1   gdamore 	 */
    405       1.1   gdamore 	st->st_slave = sh->sh_slave;
    406       1.1   gdamore 
    407  1.8.38.1  christos 	/*
    408  1.8.38.1  christos 	 * Reserve controller during transaction
    409  1.8.38.1  christos  	 */
    410  1.8.38.1  christos 	spi_acquire(sh);
    411  1.8.38.1  christos 
    412  1.8.38.1  christos 	st->st_spiprivate = (void *)sh;
    413  1.8.38.1  christos 
    414  1.8.38.1  christos 	/*
    415  1.8.38.1  christos 	 * Reconfigure controller
    416  1.8.38.1  christos 	 *
    417  1.8.38.1  christos 	 * XXX backends don't configure per-slave parameters
    418  1.8.38.1  christos 	 * Whenever we switch slaves or change mode or speed, we
    419  1.8.38.1  christos 	 * need to tell the backend.
    420  1.8.38.1  christos 	 */
    421  1.8.38.1  christos 	if (sc->sc_slave != sh->sh_slave
    422  1.8.38.1  christos 	    || sc->sc_mode != sh->sh_mode
    423  1.8.38.1  christos 	    || sc->sc_speed != sh->sh_speed) {
    424  1.8.38.1  christos 		error = (*tag->sct_configure)(tag->sct_cookie,
    425  1.8.38.1  christos 				sh->sh_slave, sh->sh_mode, sh->sh_speed);
    426  1.8.38.1  christos 		if (error)
    427  1.8.38.1  christos 			return error;
    428  1.8.38.1  christos 	}
    429  1.8.38.1  christos 	sc->sc_mode = sh->sh_mode;
    430  1.8.38.1  christos 	sc->sc_speed = sh->sh_speed;
    431  1.8.38.1  christos 	sc->sc_slave = sh->sh_slave;
    432  1.8.38.1  christos 
    433  1.8.38.1  christos 	error = (*tag->sct_transfer)(tag->sct_cookie, st);
    434  1.8.38.1  christos 
    435  1.8.38.1  christos 	return error;
    436       1.1   gdamore }
    437       1.1   gdamore 
    438       1.1   gdamore void
    439       1.1   gdamore spi_wait(struct spi_transfer *st)
    440       1.1   gdamore {
    441  1.8.38.1  christos 	struct spi_handle *sh = st->st_spiprivate;
    442       1.1   gdamore 
    443       1.5     rmind 	mutex_enter(&st->st_lock);
    444       1.4       jym 	while (!(st->st_flags & SPI_F_DONE)) {
    445       1.5     rmind 		cv_wait(&st->st_cv, &st->st_lock);
    446       1.1   gdamore 	}
    447       1.5     rmind 	mutex_exit(&st->st_lock);
    448       1.7  jakllsch 	cv_destroy(&st->st_cv);
    449       1.7  jakllsch 	mutex_destroy(&st->st_lock);
    450  1.8.38.1  christos 
    451  1.8.38.1  christos 	/*
    452  1.8.38.1  christos 	 * End transaction
    453  1.8.38.1  christos 	 */
    454  1.8.38.1  christos 	spi_release(sh);
    455       1.1   gdamore }
    456       1.1   gdamore 
    457       1.1   gdamore void
    458       1.1   gdamore spi_done(struct spi_transfer *st, int err)
    459       1.1   gdamore {
    460       1.1   gdamore 
    461       1.5     rmind 	mutex_enter(&st->st_lock);
    462       1.1   gdamore 	if ((st->st_errno = err) != 0) {
    463       1.1   gdamore 		st->st_flags |= SPI_F_ERROR;
    464       1.1   gdamore 	}
    465       1.1   gdamore 	st->st_flags |= SPI_F_DONE;
    466       1.1   gdamore 	if (st->st_done != NULL) {
    467       1.1   gdamore 		(*st->st_done)(st);
    468       1.1   gdamore 	} else {
    469       1.5     rmind 		cv_broadcast(&st->st_cv);
    470       1.1   gdamore 	}
    471       1.5     rmind 	mutex_exit(&st->st_lock);
    472       1.1   gdamore }
    473       1.1   gdamore 
    474       1.1   gdamore /*
    475       1.1   gdamore  * Some convenience routines.  These routines block until the work
    476       1.1   gdamore  * is done.
    477       1.1   gdamore  *
    478       1.1   gdamore  * spi_recv - receives data from the bus
    479       1.1   gdamore  *
    480       1.1   gdamore  * spi_send - sends data to the bus
    481       1.1   gdamore  *
    482       1.1   gdamore  * spi_send_recv - sends data to the bus, and then receives.  Note that this is
    483       1.1   gdamore  * done synchronously, i.e. send a command and get the response.  This is
    484       1.1   gdamore  * not full duplex.  If you wnat full duplex, you can't use these convenience
    485       1.1   gdamore  * wrappers.
    486       1.1   gdamore  */
    487       1.1   gdamore int
    488       1.1   gdamore spi_recv(struct spi_handle *sh, int cnt, uint8_t *data)
    489       1.1   gdamore {
    490       1.1   gdamore 	struct spi_transfer	trans;
    491       1.1   gdamore 	struct spi_chunk	chunk;
    492       1.1   gdamore 
    493       1.1   gdamore 	spi_transfer_init(&trans);
    494       1.1   gdamore 	spi_chunk_init(&chunk, cnt, NULL, data);
    495       1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    496       1.1   gdamore 
    497       1.1   gdamore 	/* enqueue it and wait for it to complete */
    498       1.1   gdamore 	spi_transfer(sh, &trans);
    499       1.1   gdamore 	spi_wait(&trans);
    500       1.1   gdamore 
    501       1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    502       1.1   gdamore 		return trans.st_errno;
    503       1.1   gdamore 
    504       1.1   gdamore 	return 0;
    505       1.1   gdamore }
    506       1.1   gdamore 
    507       1.1   gdamore int
    508       1.1   gdamore spi_send(struct spi_handle *sh, int cnt, const uint8_t *data)
    509       1.1   gdamore {
    510       1.1   gdamore 	struct spi_transfer	trans;
    511       1.1   gdamore 	struct spi_chunk	chunk;
    512       1.1   gdamore 
    513       1.1   gdamore 	spi_transfer_init(&trans);
    514       1.1   gdamore 	spi_chunk_init(&chunk, cnt, data, NULL);
    515       1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    516       1.1   gdamore 
    517       1.1   gdamore 	/* enqueue it and wait for it to complete */
    518       1.1   gdamore 	spi_transfer(sh, &trans);
    519       1.1   gdamore 	spi_wait(&trans);
    520       1.1   gdamore 
    521       1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    522       1.1   gdamore 		return trans.st_errno;
    523       1.1   gdamore 
    524       1.1   gdamore 	return 0;
    525       1.1   gdamore }
    526       1.1   gdamore 
    527       1.1   gdamore int
    528       1.1   gdamore spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
    529       1.1   gdamore     int rcnt, uint8_t *rcv)
    530       1.1   gdamore {
    531       1.1   gdamore 	struct spi_transfer	trans;
    532       1.1   gdamore 	struct spi_chunk	chunk1, chunk2;
    533       1.1   gdamore 
    534       1.1   gdamore 	spi_transfer_init(&trans);
    535       1.1   gdamore 	spi_chunk_init(&chunk1, scnt, snd, NULL);
    536       1.1   gdamore 	spi_chunk_init(&chunk2, rcnt, NULL, rcv);
    537       1.1   gdamore 	spi_transfer_add(&trans, &chunk1);
    538       1.1   gdamore 	spi_transfer_add(&trans, &chunk2);
    539       1.1   gdamore 
    540       1.1   gdamore 	/* enqueue it and wait for it to complete */
    541       1.1   gdamore 	spi_transfer(sh, &trans);
    542       1.1   gdamore 	spi_wait(&trans);
    543       1.1   gdamore 
    544       1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    545       1.1   gdamore 		return trans.st_errno;
    546       1.1   gdamore 
    547       1.1   gdamore 	return 0;
    548       1.1   gdamore }
    549  1.8.38.1  christos 
    550