Home | History | Annotate | Line # | Download | only in spi
spi.c revision 1.1
      1 /* $NetBSD: spi.c,v 1.1 2006/10/02 07:18:19 gdamore Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2006 Urbana-Champaign Independent Media Center.
      5  * Copyright (c) 2006 Garrett D'Amore.
      6  * All rights reserved.
      7  *
      8  * Portions of this code were written by Garrett D'Amore for the
      9  * Champaign-Urbana Community Wireless Network Project.
     10  *
     11  * Redistribution and use in source and binary forms, with or
     12  * without modification, are permitted provided that the following
     13  * conditions are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above
     17  *    copyright notice, this list of conditions and the following
     18  *    disclaimer in the documentation and/or other materials provided
     19  *    with the distribution.
     20  * 3. All advertising materials mentioning features or use of this
     21  *    software must display the following acknowledgements:
     22  *      This product includes software developed by the Urbana-Champaign
     23  *      Independent Media Center.
     24  *	This product includes software developed by Garrett D'Amore.
     25  * 4. Urbana-Champaign Independent Media Center's name and Garrett
     26  *    D'Amore's name may not be used to endorse or promote products
     27  *    derived from this software without specific prior written permission.
     28  *
     29  * THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
     30  * MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
     31  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     32  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     33  * ARE DISCLAIMED.  IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
     34  * MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
     35  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     36  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     37  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
     38  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     40  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     41  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 __KERNEL_RCSID(0, "$NetBSD: spi.c,v 1.1 2006/10/02 07:18:19 gdamore Exp $");
     46 
     47 #include "locators.h"
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/device.h>
     52 #include <sys/malloc.h>
     53 #include <sys/proc.h>
     54 #include <sys/errno.h>
     55 
     56 #include <dev/spi/spivar.h>
     57 
     58 struct spi_softc {
     59 	struct device		sc_dev;
     60 	struct spi_controller	sc_controller;
     61 	int			sc_mode;
     62 	int			sc_speed;
     63 	int			sc_nslaves;
     64 	struct spi_handle	*sc_slaves;
     65 };
     66 
     67 /*
     68  * SPI slave device.  We have one of these per slave.
     69  */
     70 struct spi_handle {
     71 	struct spi_softc	*sh_sc;
     72 	struct spi_controller	*sh_controller;
     73 	int			sh_slave;
     74 };
     75 
     76 /*
     77  * API for bus drivers.
     78  */
     79 
     80 int
     81 spibus_print(void *aux, const char *pnp)
     82 {
     83 
     84 	if (pnp != NULL)
     85 		aprint_normal("spi at %s", pnp);
     86 
     87 	return (UNCONF);
     88 }
     89 
     90 
     91 static int
     92 spi_match(struct device *parent, struct cfdata *cf, void *aux)
     93 {
     94 
     95 	return 1;
     96 }
     97 
     98 static int
     99 spi_print(void *aux, const char *pnp)
    100 {
    101 	struct spi_attach_args *sa = aux;
    102 
    103 	if (sa->sa_handle->sh_slave != -1)
    104 		aprint_normal(" slave %d", sa->sa_handle->sh_slave);
    105 
    106 	return (UNCONF);
    107 }
    108 
    109 static int
    110 spi_search(struct device *parent, struct cfdata *cf, const int *ldesc,
    111     void *aux)
    112 {
    113 	struct spi_softc *sc = (void *)parent;
    114 	struct spi_attach_args sa;
    115 	int addr;
    116 
    117 	addr = cf->cf_loc[SPICF_SLAVE];
    118 	if ((addr < 0) || (addr >= sc->sc_controller.sct_nslaves)) {
    119 		return -1;
    120 	}
    121 
    122 	sa.sa_handle = &sc->sc_slaves[addr];
    123 
    124 	if (config_match(parent, cf, &sa) > 0)
    125 		config_attach(parent, cf, &sa, spi_print);
    126 
    127 	return 0;
    128 }
    129 
    130 /*
    131  * API for device drivers.
    132  *
    133  * We provide wrapper routines to decouple the ABI for the SPI
    134  * device drivers from the ABI for the SPI bus drivers.
    135  */
    136 static void
    137 spi_attach(struct device *parent, struct device *self, void *aux)
    138 {
    139 	struct spi_softc *sc = device_private(self);
    140 	struct spibus_attach_args *sba = aux;
    141 	int i;
    142 
    143 	aprint_naive(": SPI bus\n");
    144 	aprint_normal(": SPI bus\n");
    145 
    146 	sc->sc_controller = *sba->sba_controller;
    147 	/* allocate slave structures */
    148 	sc->sc_slaves = malloc(sizeof (struct spi_handle) * sc->sc_nslaves,
    149 	    M_DEVBUF, M_WAITOK | M_ZERO);
    150 
    151 	sc->sc_speed = 0;
    152 
    153 	/*
    154 	 * Initialize slave handles
    155 	 */
    156 	sc->sc_nslaves = sba->sba_controller->sct_nslaves;
    157 	for (i = 0; i < sc->sc_nslaves; i++) {
    158 		sc->sc_slaves[i].sh_slave = i;
    159 		sc->sc_slaves[i].sh_sc = sc;
    160 		sc->sc_slaves[i].sh_controller = &sc->sc_controller;
    161 	}
    162 
    163 	/*
    164 	 * Locate and attach child devices
    165 	 */
    166 	config_search_ia(spi_search, self, "spi", NULL);
    167 }
    168 
    169 CFATTACH_DECL(spi, sizeof(struct spi_softc),
    170     spi_match, spi_attach, NULL, NULL);
    171 
    172 /*
    173  * Configure.  This should be the first thing that the SPI driver
    174  * should do, to configure which mode (e.g. SPI_MODE_0, which is the
    175  * same as Philips Microwire mode), and speed.  If the bus driver
    176  * cannot run fast enough, then it should just configure the fastest
    177  * mode that it can support.  If the bus driver cannot run slow
    178  * enough, then the device is incompatible and an error should be
    179  * returned.
    180  */
    181 int
    182 spi_configure(struct spi_handle *sh, int mode, int speed)
    183 {
    184 	int			s, rv;
    185 	struct spi_softc	*sc = sh->sh_sc;
    186 	struct spi_controller	*tag = sh->sh_controller;
    187 
    188 	/* ensure that request is compatible with other devices on the bus */
    189 	if ((sc->sc_mode >= 0) && (sc->sc_mode != mode))
    190 		return EINVAL;
    191 
    192 	s = splserial();
    193 	/* pick lowest configured speed */
    194 	if (speed == 0)
    195 		speed = sc->sc_speed;
    196 	if (sc->sc_speed)
    197 		speed = min(sc->sc_speed, speed);
    198 
    199 	rv = (*tag->sct_configure)(tag->sct_cookie, sh->sh_slave,
    200 	    mode, speed);
    201 
    202 	if (rv == 0) {
    203 		sc->sc_mode = mode;
    204 		sc->sc_speed = speed;
    205 	}
    206 	splx(s);
    207 	return rv;
    208 }
    209 
    210 void
    211 spi_transfer_init(struct spi_transfer *st)
    212 {
    213 
    214 	simple_lock_init(&st->st_lock);
    215 	st->st_flags = 0;
    216 	st->st_errno = 0;
    217 	st->st_done = NULL;
    218 	st->st_chunks = NULL;
    219 	st->st_private = NULL;
    220 	st->st_slave = -1;
    221 }
    222 
    223 void
    224 spi_chunk_init(struct spi_chunk *chunk, int cnt, const uint8_t *wptr,
    225     uint8_t *rptr)
    226 {
    227 
    228 	chunk->chunk_write = chunk->chunk_wptr = wptr;
    229 	chunk->chunk_read = chunk->chunk_read = rptr;
    230 	chunk->chunk_rresid = chunk->chunk_wresid = chunk->chunk_count = cnt;
    231 	chunk->chunk_next = NULL;
    232 }
    233 
    234 void
    235 spi_transfer_add(struct spi_transfer *st, struct spi_chunk *chunk)
    236 {
    237 	struct spi_chunk **cpp;
    238 
    239 	/* this is an O(n) insert -- perhaps we should use a simpleq? */
    240 	for (cpp = &st->st_chunks; *cpp; cpp = &(*cpp)->chunk_next);
    241 	*cpp = chunk;
    242 }
    243 
    244 int
    245 spi_transfer(struct spi_handle *sh, struct spi_transfer *st)
    246 {
    247 	struct spi_controller	*tag = sh->sh_controller;
    248 	struct spi_chunk	*chunk;
    249 
    250 	/*
    251 	 * Initialize "resid" counters and pointers, so that callers
    252 	 * and bus drivers don't have to.
    253 	 */
    254 	for (chunk = st->st_chunks; chunk; chunk = chunk->chunk_next) {
    255 		chunk->chunk_wresid = chunk->chunk_rresid = chunk->chunk_count;
    256 		chunk->chunk_wptr = chunk->chunk_write;
    257 		chunk->chunk_rptr = chunk->chunk_read;
    258 	}
    259 
    260 	/*
    261 	 * Match slave to handle's slave.
    262 	 */
    263 	st->st_slave = sh->sh_slave;
    264 
    265 	return (*tag->sct_transfer)(tag->sct_cookie, st);
    266 }
    267 
    268 void
    269 spi_wait(struct spi_transfer *st)
    270 {
    271 	int	s;
    272 
    273 	s = splserial();
    274 	simple_lock(&st->st_lock);
    275 	while (!st->st_flags & SPI_F_DONE) {
    276 		ltsleep(st, PWAIT, "spi_wait", 0, &st->st_lock);
    277 	}
    278 	simple_unlock(&st->st_lock);
    279 	splx(s);
    280 }
    281 
    282 void
    283 spi_done(struct spi_transfer *st, int err)
    284 {
    285 	int	s;
    286 
    287 	s = splserial();
    288 
    289 	if ((st->st_errno = err) != 0) {
    290 		st->st_flags |= SPI_F_ERROR;
    291 	}
    292 	st->st_flags |= SPI_F_DONE;
    293 	if (st->st_done != NULL) {
    294 		(*st->st_done)(st);
    295 	} else {
    296 
    297 		simple_lock(&st->st_lock);
    298 		wakeup(st);
    299 		simple_unlock(&st->st_lock);
    300 	}
    301 	splx(s);
    302 }
    303 
    304 /*
    305  * Some convenience routines.  These routines block until the work
    306  * is done.
    307  *
    308  * spi_recv - receives data from the bus
    309  *
    310  * spi_send - sends data to the bus
    311  *
    312  * spi_send_recv - sends data to the bus, and then receives.  Note that this is
    313  * done synchronously, i.e. send a command and get the response.  This is
    314  * not full duplex.  If you wnat full duplex, you can't use these convenience
    315  * wrappers.
    316  */
    317 int
    318 spi_recv(struct spi_handle *sh, int cnt, uint8_t *data)
    319 {
    320 	struct spi_transfer	trans;
    321 	struct spi_chunk	chunk;
    322 
    323 	spi_transfer_init(&trans);
    324 	spi_chunk_init(&chunk, cnt, NULL, data);
    325 	spi_transfer_add(&trans, &chunk);
    326 
    327 	/* enqueue it and wait for it to complete */
    328 	spi_transfer(sh, &trans);
    329 	spi_wait(&trans);
    330 
    331 	if (trans.st_flags & SPI_F_ERROR)
    332 		return trans.st_errno;
    333 
    334 	return 0;
    335 }
    336 
    337 int
    338 spi_send(struct spi_handle *sh, int cnt, const uint8_t *data)
    339 {
    340 	struct spi_transfer	trans;
    341 	struct spi_chunk	chunk;
    342 
    343 	spi_transfer_init(&trans);
    344 	spi_chunk_init(&chunk, cnt, data, NULL);
    345 	spi_transfer_add(&trans, &chunk);
    346 
    347 	/* enqueue it and wait for it to complete */
    348 	spi_transfer(sh, &trans);
    349 	spi_wait(&trans);
    350 
    351 	if (trans.st_flags & SPI_F_ERROR)
    352 		return trans.st_errno;
    353 
    354 	return 0;
    355 }
    356 
    357 int
    358 spi_send_recv(struct spi_handle *sh, int scnt, const uint8_t *snd,
    359     int rcnt, uint8_t *rcv)
    360 {
    361 	struct spi_transfer	trans;
    362 	struct spi_chunk	chunk1, chunk2;
    363 
    364 	spi_transfer_init(&trans);
    365 	spi_chunk_init(&chunk1, scnt, snd, NULL);
    366 	spi_chunk_init(&chunk2, rcnt, NULL, rcv);
    367 	spi_transfer_add(&trans, &chunk1);
    368 	spi_transfer_add(&trans, &chunk2);
    369 
    370 	/* enqueue it and wait for it to complete */
    371 	spi_transfer(sh, &trans);
    372 	spi_wait(&trans);
    373 
    374 	if (trans.st_flags & SPI_F_ERROR)
    375 		return trans.st_errno;
    376 
    377 	return 0;
    378 }
    379