Home | History | Annotate | Line # | Download | only in spi
spi.c revision 1.8.6.2
      1  1.8.6.2   khorben /* $NetBSD: spi.c,v 1.8.6.2 2013/05/15 13:52:19 khorben 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.6.2   khorben __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.8.6.2 2013/05/15 13:52:19 khorben 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.1   gdamore #include <sys/malloc.h>
     53      1.5     rmind #include <sys/mutex.h>
     54      1.5     rmind #include <sys/condvar.h>
     55      1.1   gdamore #include <sys/errno.h>
     56      1.1   gdamore 
     57      1.1   gdamore #include <dev/spi/spivar.h>
     58      1.1   gdamore 
     59      1.1   gdamore struct spi_softc {
     60      1.1   gdamore 	struct spi_controller	sc_controller;
     61      1.1   gdamore 	int			sc_mode;
     62      1.1   gdamore 	int			sc_speed;
     63      1.1   gdamore 	int			sc_nslaves;
     64      1.1   gdamore 	struct spi_handle	*sc_slaves;
     65      1.1   gdamore };
     66      1.1   gdamore 
     67      1.1   gdamore /*
     68      1.1   gdamore  * SPI slave device.  We have one of these per slave.
     69      1.1   gdamore  */
     70      1.1   gdamore struct spi_handle {
     71      1.1   gdamore 	struct spi_softc	*sh_sc;
     72      1.1   gdamore 	struct spi_controller	*sh_controller;
     73      1.1   gdamore 	int			sh_slave;
     74      1.1   gdamore };
     75      1.1   gdamore 
     76      1.1   gdamore /*
     77      1.1   gdamore  * API for bus drivers.
     78      1.1   gdamore  */
     79      1.1   gdamore 
     80      1.1   gdamore int
     81      1.1   gdamore spibus_print(void *aux, const char *pnp)
     82      1.1   gdamore {
     83      1.1   gdamore 
     84      1.1   gdamore 	if (pnp != NULL)
     85      1.1   gdamore 		aprint_normal("spi at %s", pnp);
     86      1.1   gdamore 
     87      1.1   gdamore 	return (UNCONF);
     88      1.1   gdamore }
     89      1.1   gdamore 
     90      1.1   gdamore 
     91      1.1   gdamore static int
     92      1.3   xtraeme spi_match(device_t parent, cfdata_t cf, void *aux)
     93      1.1   gdamore {
     94      1.5     rmind 
     95      1.1   gdamore 	return 1;
     96      1.1   gdamore }
     97      1.1   gdamore 
     98      1.1   gdamore static int
     99      1.1   gdamore spi_print(void *aux, const char *pnp)
    100      1.1   gdamore {
    101      1.1   gdamore 	struct spi_attach_args *sa = aux;
    102      1.1   gdamore 
    103      1.1   gdamore 	if (sa->sa_handle->sh_slave != -1)
    104      1.1   gdamore 		aprint_normal(" slave %d", sa->sa_handle->sh_slave);
    105      1.1   gdamore 
    106      1.1   gdamore 	return (UNCONF);
    107      1.1   gdamore }
    108      1.1   gdamore 
    109      1.1   gdamore static int
    110      1.3   xtraeme spi_search(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
    111      1.1   gdamore {
    112      1.3   xtraeme 	struct spi_softc *sc = device_private(parent);
    113      1.1   gdamore 	struct spi_attach_args sa;
    114      1.1   gdamore 	int addr;
    115      1.1   gdamore 
    116      1.1   gdamore 	addr = cf->cf_loc[SPICF_SLAVE];
    117      1.1   gdamore 	if ((addr < 0) || (addr >= sc->sc_controller.sct_nslaves)) {
    118      1.1   gdamore 		return -1;
    119      1.1   gdamore 	}
    120      1.1   gdamore 
    121      1.1   gdamore 	sa.sa_handle = &sc->sc_slaves[addr];
    122  1.8.6.1   khorben 	sa.sa_intr = cf->cf_loc[SPICF_INTR];
    123  1.8.6.2   khorben 	sa.sa_speed = cf->cf_loc[SPICF_SPEED];
    124      1.1   gdamore 
    125      1.1   gdamore 	if (config_match(parent, cf, &sa) > 0)
    126      1.1   gdamore 		config_attach(parent, cf, &sa, spi_print);
    127      1.1   gdamore 
    128      1.1   gdamore 	return 0;
    129      1.1   gdamore }
    130      1.1   gdamore 
    131      1.1   gdamore /*
    132      1.1   gdamore  * API for device drivers.
    133      1.1   gdamore  *
    134      1.1   gdamore  * We provide wrapper routines to decouple the ABI for the SPI
    135      1.1   gdamore  * device drivers from the ABI for the SPI bus drivers.
    136      1.1   gdamore  */
    137      1.1   gdamore static void
    138      1.3   xtraeme spi_attach(device_t parent, device_t self, void *aux)
    139      1.1   gdamore {
    140      1.1   gdamore 	struct spi_softc *sc = device_private(self);
    141      1.1   gdamore 	struct spibus_attach_args *sba = aux;
    142      1.1   gdamore 	int i;
    143      1.1   gdamore 
    144      1.1   gdamore 	aprint_naive(": SPI bus\n");
    145      1.1   gdamore 	aprint_normal(": SPI bus\n");
    146      1.1   gdamore 
    147      1.1   gdamore 	sc->sc_controller = *sba->sba_controller;
    148      1.8   rkujawa 	sc->sc_nslaves = sba->sba_controller->sct_nslaves;
    149      1.1   gdamore 	/* allocate slave structures */
    150      1.1   gdamore 	sc->sc_slaves = malloc(sizeof (struct spi_handle) * sc->sc_nslaves,
    151      1.1   gdamore 	    M_DEVBUF, M_WAITOK | M_ZERO);
    152      1.1   gdamore 
    153      1.1   gdamore 	sc->sc_speed = 0;
    154      1.2   gdamore 	sc->sc_mode = -1;
    155      1.1   gdamore 
    156      1.1   gdamore 	/*
    157      1.1   gdamore 	 * Initialize slave handles
    158      1.1   gdamore 	 */
    159      1.1   gdamore 	for (i = 0; i < sc->sc_nslaves; i++) {
    160      1.1   gdamore 		sc->sc_slaves[i].sh_slave = i;
    161      1.1   gdamore 		sc->sc_slaves[i].sh_sc = sc;
    162      1.1   gdamore 		sc->sc_slaves[i].sh_controller = &sc->sc_controller;
    163      1.1   gdamore 	}
    164      1.1   gdamore 
    165      1.1   gdamore 	/*
    166      1.1   gdamore 	 * Locate and attach child devices
    167      1.1   gdamore 	 */
    168      1.1   gdamore 	config_search_ia(spi_search, self, "spi", NULL);
    169      1.1   gdamore }
    170      1.1   gdamore 
    171      1.3   xtraeme CFATTACH_DECL_NEW(spi, sizeof(struct spi_softc),
    172      1.1   gdamore     spi_match, spi_attach, NULL, NULL);
    173      1.1   gdamore 
    174      1.1   gdamore /*
    175      1.1   gdamore  * Configure.  This should be the first thing that the SPI driver
    176      1.1   gdamore  * should do, to configure which mode (e.g. SPI_MODE_0, which is the
    177      1.1   gdamore  * same as Philips Microwire mode), and speed.  If the bus driver
    178      1.1   gdamore  * cannot run fast enough, then it should just configure the fastest
    179      1.1   gdamore  * mode that it can support.  If the bus driver cannot run slow
    180      1.1   gdamore  * enough, then the device is incompatible and an error should be
    181      1.1   gdamore  * returned.
    182      1.1   gdamore  */
    183      1.1   gdamore int
    184      1.1   gdamore spi_configure(struct spi_handle *sh, int mode, int speed)
    185      1.1   gdamore {
    186      1.1   gdamore 	int			s, rv;
    187      1.1   gdamore 	struct spi_softc	*sc = sh->sh_sc;
    188      1.1   gdamore 	struct spi_controller	*tag = sh->sh_controller;
    189      1.1   gdamore 
    190      1.1   gdamore 	/* ensure that request is compatible with other devices on the bus */
    191      1.1   gdamore 	if ((sc->sc_mode >= 0) && (sc->sc_mode != mode))
    192      1.1   gdamore 		return EINVAL;
    193      1.1   gdamore 
    194      1.5     rmind 	s = splbio();
    195      1.1   gdamore 	/* pick lowest configured speed */
    196      1.1   gdamore 	if (speed == 0)
    197      1.1   gdamore 		speed = sc->sc_speed;
    198      1.1   gdamore 	if (sc->sc_speed)
    199      1.1   gdamore 		speed = min(sc->sc_speed, speed);
    200      1.1   gdamore 
    201  1.8.6.2   khorben 	rv = (*tag->sct_configure)(tag->sct_cookie, sh->sh_slave, mode, speed);
    202      1.1   gdamore 
    203      1.1   gdamore 	if (rv == 0) {
    204      1.1   gdamore 		sc->sc_mode = mode;
    205      1.1   gdamore 		sc->sc_speed = speed;
    206      1.1   gdamore 	}
    207      1.1   gdamore 	splx(s);
    208      1.1   gdamore 	return rv;
    209      1.1   gdamore }
    210      1.1   gdamore 
    211      1.1   gdamore void
    212      1.1   gdamore spi_transfer_init(struct spi_transfer *st)
    213      1.1   gdamore {
    214      1.5     rmind 	mutex_init(&st->st_lock, MUTEX_DEFAULT, IPL_BIO);
    215      1.5     rmind 	cv_init(&st->st_cv, "spicv");
    216      1.5     rmind 
    217      1.1   gdamore 	st->st_flags = 0;
    218      1.1   gdamore 	st->st_errno = 0;
    219      1.1   gdamore 	st->st_done = NULL;
    220      1.1   gdamore 	st->st_chunks = NULL;
    221      1.1   gdamore 	st->st_private = NULL;
    222      1.1   gdamore 	st->st_slave = -1;
    223      1.1   gdamore }
    224      1.1   gdamore 
    225      1.1   gdamore void
    226      1.1   gdamore spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
    227      1.1   gdamore     uint8_t *rptr)
    228      1.1   gdamore {
    229      1.1   gdamore 
    230      1.1   gdamore 	chunk->chunk_write = chunk->chunk_wptr = wptr;
    231      1.6       mrg 	chunk->chunk_read = chunk->chunk_rptr = rptr;
    232      1.1   gdamore 	chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
    233      1.1   gdamore 	chunk->chunk_next = NULL;
    234      1.1   gdamore }
    235      1.1   gdamore 
    236      1.1   gdamore void
    237      1.1   gdamore spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
    238      1.1   gdamore {
    239      1.1   gdamore 	struct spi_chunk **cpp;
    240      1.1   gdamore 
    241      1.1   gdamore 	/* this is an O(n) insert -- perhaps we should use a simpleq? */
    242      1.1   gdamore 	for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
    243      1.1   gdamore 	*cpp = chunk;
    244      1.1   gdamore }
    245      1.1   gdamore 
    246      1.1   gdamore int
    247      1.1   gdamore spi_transfer(struct spi_handle *sh, struct spi_transfer *st)
    248      1.1   gdamore {
    249      1.1   gdamore 	struct spi_controller	*tag = sh->sh_controller;
    250      1.1   gdamore 	struct spi_chunk	*chunk;
    251      1.1   gdamore 
    252      1.1   gdamore 	/*
    253      1.1   gdamore 	 * Initialize "resid" counters and pointers, so that callers
    254      1.1   gdamore 	 * and bus drivers don't have to.
    255      1.1   gdamore 	 */
    256      1.1   gdamore 	for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
    257      1.1   gdamore 		chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
    258      1.1   gdamore 		chunk->chunk_wptr = chunk->chunk_write;
    259      1.1   gdamore 		chunk->chunk_rptr = chunk->chunk_read;
    260      1.1   gdamore 	}
    261      1.1   gdamore 
    262      1.1   gdamore 	/*
    263      1.1   gdamore 	 * Match slave to handle's slave.
    264      1.1   gdamore 	 */
    265      1.1   gdamore 	st->st_slave = sh->sh_slave;
    266      1.1   gdamore 
    267      1.1   gdamore 	return (*tag->sct_transfer)(tag->sct_cookie, st);
    268      1.1   gdamore }
    269      1.1   gdamore 
    270      1.1   gdamore void
    271      1.1   gdamore spi_wait(struct spi_transfer *st)
    272      1.1   gdamore {
    273      1.1   gdamore 
    274      1.5     rmind 	mutex_enter(&st->st_lock);
    275      1.4       jym 	while (!(st->st_flags & SPI_F_DONE)) {
    276      1.5     rmind 		cv_wait(&st->st_cv, &st->st_lock);
    277      1.1   gdamore 	}
    278      1.5     rmind 	mutex_exit(&st->st_lock);
    279      1.7  jakllsch 	cv_destroy(&st->st_cv);
    280      1.7  jakllsch 	mutex_destroy(&st->st_lock);
    281      1.1   gdamore }
    282      1.1   gdamore 
    283      1.1   gdamore void
    284      1.1   gdamore spi_done(struct spi_transfer *st, int err)
    285      1.1   gdamore {
    286      1.1   gdamore 
    287      1.5     rmind 	mutex_enter(&st->st_lock);
    288      1.1   gdamore 	if ((st->st_errno = err) != 0) {
    289      1.1   gdamore 		st->st_flags |= SPI_F_ERROR;
    290      1.1   gdamore 	}
    291      1.1   gdamore 	st->st_flags |= SPI_F_DONE;
    292      1.1   gdamore 	if (st->st_done != NULL) {
    293      1.1   gdamore 		(*st->st_done)(st);
    294      1.1   gdamore 	} else {
    295      1.5     rmind 		cv_broadcast(&st->st_cv);
    296      1.1   gdamore 	}
    297      1.5     rmind 	mutex_exit(&st->st_lock);
    298      1.1   gdamore }
    299      1.1   gdamore 
    300      1.1   gdamore /*
    301      1.1   gdamore  * Some convenience routines.  These routines block until the work
    302      1.1   gdamore  * is done.
    303      1.1   gdamore  *
    304      1.1   gdamore  * spi_recv - receives data from the bus
    305      1.1   gdamore  *
    306      1.1   gdamore  * spi_send - sends data to the bus
    307      1.1   gdamore  *
    308      1.1   gdamore  * spi_send_recv - sends data to the bus, and then receives.  Note that this is
    309      1.1   gdamore  * done synchronously, i.e. send a command and get the response.  This is
    310      1.1   gdamore  * not full duplex.  If you wnat full duplex, you can't use these convenience
    311      1.1   gdamore  * wrappers.
    312      1.1   gdamore  */
    313      1.1   gdamore int
    314      1.1   gdamore spi_recv(struct spi_handle *sh, int cnt, uint8_t *data)
    315      1.1   gdamore {
    316      1.1   gdamore 	struct spi_transfer	trans;
    317      1.1   gdamore 	struct spi_chunk	chunk;
    318      1.1   gdamore 
    319      1.1   gdamore 	spi_transfer_init(&trans);
    320      1.1   gdamore 	spi_chunk_init(&chunk, cnt, NULL, data);
    321      1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    322      1.1   gdamore 
    323      1.1   gdamore 	/* enqueue it and wait for it to complete */
    324      1.1   gdamore 	spi_transfer(sh, &trans);
    325      1.1   gdamore 	spi_wait(&trans);
    326      1.1   gdamore 
    327      1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    328      1.1   gdamore 		return trans.st_errno;
    329      1.1   gdamore 
    330      1.1   gdamore 	return 0;
    331      1.1   gdamore }
    332      1.1   gdamore 
    333      1.1   gdamore int
    334      1.1   gdamore spi_send(struct spi_handle *sh, int cnt, const uint8_t *data)
    335      1.1   gdamore {
    336      1.1   gdamore 	struct spi_transfer	trans;
    337      1.1   gdamore 	struct spi_chunk	chunk;
    338      1.1   gdamore 
    339      1.1   gdamore 	spi_transfer_init(&trans);
    340      1.1   gdamore 	spi_chunk_init(&chunk, cnt, data, NULL);
    341      1.1   gdamore 	spi_transfer_add(&trans, &chunk);
    342      1.1   gdamore 
    343      1.1   gdamore 	/* enqueue it and wait for it to complete */
    344      1.1   gdamore 	spi_transfer(sh, &trans);
    345      1.1   gdamore 	spi_wait(&trans);
    346      1.1   gdamore 
    347      1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    348      1.1   gdamore 		return trans.st_errno;
    349      1.1   gdamore 
    350      1.1   gdamore 	return 0;
    351      1.1   gdamore }
    352      1.1   gdamore 
    353      1.1   gdamore int
    354      1.1   gdamore spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
    355      1.1   gdamore     int rcnt, uint8_t *rcv)
    356      1.1   gdamore {
    357      1.1   gdamore 	struct spi_transfer	trans;
    358      1.1   gdamore 	struct spi_chunk	chunk1, chunk2;
    359      1.1   gdamore 
    360      1.1   gdamore 	spi_transfer_init(&trans);
    361      1.1   gdamore 	spi_chunk_init(&chunk1, scnt, snd, NULL);
    362      1.1   gdamore 	spi_chunk_init(&chunk2, rcnt, NULL, rcv);
    363      1.1   gdamore 	spi_transfer_add(&trans, &chunk1);
    364      1.1   gdamore 	spi_transfer_add(&trans, &chunk2);
    365      1.1   gdamore 
    366      1.1   gdamore 	/* enqueue it and wait for it to complete */
    367      1.1   gdamore 	spi_transfer(sh, &trans);
    368      1.1   gdamore 	spi_wait(&trans);
    369      1.1   gdamore 
    370      1.1   gdamore 	if (trans.st_flags & SPI_F_ERROR)
    371      1.1   gdamore 		return trans.st_errno;
    372      1.1   gdamore 
    373      1.1   gdamore 	return 0;
    374      1.1   gdamore }
    375