Home | History | Annotate | Line # | Download | only in dev
arspi.c revision 1.5.76.1
      1  1.5.76.1   cherry /* $NetBSD: arspi.c,v 1.5.76.1 2011/06/23 14:19:22 cherry 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.5.76.1   cherry __KERNEL_RCSID(0, "$NetBSD: arspi.c,v 1.5.76.1 2011/06/23 14:19:22 cherry 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/kernel.h>
     52       1.1  gdamore #include <sys/device.h>
     53       1.1  gdamore #include <sys/errno.h>
     54       1.1  gdamore #include <sys/malloc.h>
     55       1.1  gdamore #include <sys/proc.h>
     56       1.1  gdamore #include <sys/queue.h>
     57       1.1  gdamore 
     58       1.1  gdamore #include <machine/bus.h>
     59       1.1  gdamore #include <machine/cpu.h>
     60       1.1  gdamore 
     61       1.1  gdamore #include <mips/atheros/include/ar5315reg.h>
     62       1.1  gdamore #include <mips/atheros/include/ar531xvar.h>
     63       1.1  gdamore #include <mips/atheros/include/arbusvar.h>
     64       1.1  gdamore 
     65       1.1  gdamore #include <mips/atheros/dev/arspireg.h>
     66       1.1  gdamore 
     67       1.1  gdamore #include <dev/spi/spiflash.h>
     68       1.1  gdamore #include <dev/spi/spivar.h>
     69       1.1  gdamore 
     70       1.1  gdamore /*
     71       1.1  gdamore  * This device is intended only to operate with specific SPI flash
     72       1.1  gdamore  * parts, and is not a general purpose SPI host.  (Or at least if it
     73       1.1  gdamore  * is, the Linux and eCos sources do not show how to use it as such.)
     74       1.1  gdamore  * And lack of documentation on the Atheros SoCs is less than helpful.
     75       1.1  gdamore  *
     76       1.1  gdamore  * So for now we just "emulate" enough of the host bus framework to
     77       1.1  gdamore  * make the SPI flash drivers happy.
     78       1.1  gdamore  */
     79       1.1  gdamore 
     80       1.1  gdamore struct arspi_job {
     81       1.1  gdamore 	uint8_t			job_opcode;
     82       1.1  gdamore 	struct spi_chunk	*job_chunk;
     83       1.1  gdamore 	uint32_t		job_flags;
     84       1.1  gdamore 	uint32_t		job_addr;
     85       1.1  gdamore 	uint32_t		job_data;
     86       1.1  gdamore 	int			job_rxcnt;
     87       1.1  gdamore 	int			job_txcnt;
     88       1.1  gdamore 	int			job_addrcnt;
     89       1.1  gdamore 	int			job_rresid;
     90       1.1  gdamore 	int			job_wresid;
     91       1.1  gdamore };
     92       1.1  gdamore 
     93       1.1  gdamore #define	JOB_READ		0x1
     94       1.1  gdamore #define	JOB_WRITE		0x2
     95       1.1  gdamore #define	JOB_LAST		0x4
     96       1.1  gdamore #define	JOB_WAIT		0x8	/* job must wait for WIP bits */
     97       1.1  gdamore #define	JOB_WREN		0x10	/* WREN needed */
     98       1.1  gdamore 
     99       1.1  gdamore struct arspi_softc {
    100       1.1  gdamore 	struct device		sc_dev;
    101       1.1  gdamore 	struct spi_controller	sc_spi;
    102       1.1  gdamore 	void			*sc_ih;
    103       1.4  thorpej 	bool			sc_interrupts;
    104       1.1  gdamore 
    105       1.1  gdamore 	struct spi_transfer	*sc_transfer;
    106       1.1  gdamore 	struct spi_chunk	*sc_wchunk;	/* for partial writes */
    107       1.1  gdamore 	struct spi_transq	sc_transq;
    108       1.1  gdamore 	bus_space_tag_t		sc_st;
    109       1.1  gdamore 	bus_space_handle_t	sc_sh;
    110       1.1  gdamore 	bus_size_t		sc_size;
    111       1.1  gdamore };
    112       1.1  gdamore 
    113       1.1  gdamore #define	STATIC
    114       1.1  gdamore 
    115       1.1  gdamore STATIC int arspi_match(struct device *, struct cfdata *, void *);
    116       1.1  gdamore STATIC void arspi_attach(struct device *, struct device *, void *);
    117       1.1  gdamore STATIC void arspi_interrupts(struct device *);
    118       1.1  gdamore STATIC int arspi_intr(void *);
    119       1.1  gdamore /* SPI service routines */
    120       1.1  gdamore STATIC int arspi_configure(void *, int, int, int);
    121       1.1  gdamore STATIC int arspi_transfer(void *, struct spi_transfer *);
    122       1.1  gdamore /* internal support */
    123       1.1  gdamore STATIC void arspi_poll(struct arspi_softc *);
    124       1.1  gdamore STATIC void arspi_done(struct arspi_softc *, int);
    125       1.1  gdamore STATIC void arspi_sched(struct arspi_softc *);
    126       1.1  gdamore STATIC int arspi_get_byte(struct spi_chunk **, uint8_t *);
    127       1.1  gdamore STATIC int arspi_put_byte(struct spi_chunk **, uint8_t);
    128       1.1  gdamore STATIC int arspi_make_job(struct spi_transfer *);
    129       1.1  gdamore STATIC void arspi_update_job(struct spi_transfer *);
    130       1.1  gdamore STATIC void arspi_finish_job(struct spi_transfer *);
    131       1.1  gdamore 
    132       1.1  gdamore 
    133       1.1  gdamore CFATTACH_DECL(arspi, sizeof(struct arspi_softc),
    134       1.1  gdamore     arspi_match, arspi_attach, NULL, NULL);
    135       1.1  gdamore 
    136       1.1  gdamore #define	GETREG(sc, o)		bus_space_read_4(sc->sc_st, sc->sc_sh, o)
    137       1.1  gdamore #define	PUTREG(sc, o, v)	bus_space_write_4(sc->sc_st, sc->sc_sh, o, v)
    138       1.1  gdamore 
    139       1.1  gdamore int
    140       1.1  gdamore arspi_match(struct device *parent, struct cfdata *cf, void *aux)
    141       1.1  gdamore {
    142       1.1  gdamore 	struct arbus_attach_args *aa = aux;
    143       1.1  gdamore 
    144       1.1  gdamore 	if (strcmp(aa->aa_name, cf->cf_name) != 0)
    145       1.1  gdamore 		return 0;
    146       1.1  gdamore 	return 1;
    147       1.1  gdamore }
    148       1.1  gdamore 
    149       1.1  gdamore void
    150       1.1  gdamore arspi_attach(struct device *parent, struct device *self, void *aux)
    151       1.1  gdamore {
    152       1.1  gdamore 	struct arspi_softc *sc = device_private(self);
    153       1.1  gdamore 	struct spibus_attach_args sba;
    154       1.1  gdamore 	struct arbus_attach_args *aa = aux;
    155       1.1  gdamore 
    156       1.1  gdamore 	/*
    157       1.1  gdamore 	 * Map registers.
    158       1.1  gdamore 	 */
    159       1.1  gdamore 	sc->sc_st = aa->aa_bst;
    160       1.1  gdamore 	sc->sc_size = aa->aa_size;
    161       1.1  gdamore 	if (bus_space_map(sc->sc_st, aa->aa_addr, sc->sc_size, 0,
    162       1.1  gdamore 		&sc->sc_sh) != 0) {
    163       1.1  gdamore 		printf(": unable to map registers!\n");
    164       1.1  gdamore 		return;
    165       1.1  gdamore 	}
    166       1.1  gdamore 
    167       1.1  gdamore 	aprint_normal(": Atheros SPI controller\n");
    168       1.1  gdamore 
    169       1.1  gdamore 	/*
    170       1.1  gdamore 	 * Initialize SPI controller.
    171       1.1  gdamore 	 */
    172       1.1  gdamore 	sc->sc_spi.sct_cookie = sc;
    173       1.1  gdamore 	sc->sc_spi.sct_configure = arspi_configure;
    174       1.1  gdamore 	sc->sc_spi.sct_transfer = arspi_transfer;
    175       1.1  gdamore 	sc->sc_spi.sct_nslaves = 1;
    176       1.1  gdamore 
    177       1.1  gdamore 
    178       1.1  gdamore 	/*
    179       1.1  gdamore 	 * Initialize the queue.
    180       1.1  gdamore 	 */
    181       1.1  gdamore 	spi_transq_init(&sc->sc_transq);
    182       1.1  gdamore 
    183       1.1  gdamore 	/*
    184       1.1  gdamore 	 * Enable device interrupts.
    185       1.1  gdamore 	 */
    186       1.1  gdamore 	sc->sc_ih = arbus_intr_establish(aa->aa_cirq, aa->aa_mirq,
    187       1.1  gdamore 	    arspi_intr, sc);
    188       1.1  gdamore 	if (sc->sc_ih == NULL) {
    189       1.1  gdamore 		aprint_error("%s: couldn't establish interrupt\n",
    190       1.1  gdamore 		    device_xname(self));
    191       1.1  gdamore 		/* just leave it in polled mode */
    192       1.1  gdamore 	} else
    193       1.1  gdamore 		config_interrupts(self, arspi_interrupts);
    194       1.1  gdamore 
    195       1.1  gdamore 	/*
    196       1.1  gdamore 	 * Initialize and attach bus attach.
    197       1.1  gdamore 	 */
    198       1.1  gdamore 	sba.sba_controller = &sc->sc_spi;
    199       1.1  gdamore 	(void) config_found_ia(&sc->sc_dev, "spibus", &sba, spibus_print);
    200       1.1  gdamore }
    201       1.1  gdamore 
    202       1.1  gdamore void
    203       1.1  gdamore arspi_interrupts(struct device *self)
    204       1.1  gdamore {
    205       1.1  gdamore 	/*
    206       1.1  gdamore 	 * we never leave polling mode, because, apparently, we
    207       1.1  gdamore 	 * are missing some data about how to drive the SPI in interrupt
    208       1.1  gdamore 	 * mode.
    209       1.1  gdamore 	 */
    210       1.1  gdamore #if 0
    211       1.1  gdamore 	struct arspi_softc *sc = device_private(self);
    212       1.1  gdamore 	int	s;
    213       1.1  gdamore 
    214  1.5.76.1   cherry 	s = splbio();
    215       1.5  thorpej 	sc->sc_interrupts = true;
    216       1.1  gdamore 	splx(s);
    217       1.1  gdamore #endif
    218       1.1  gdamore }
    219       1.1  gdamore 
    220       1.1  gdamore int
    221       1.1  gdamore arspi_intr(void *arg)
    222       1.1  gdamore {
    223       1.1  gdamore 	struct arspi_softc *sc = arg;
    224       1.1  gdamore 
    225       1.1  gdamore 	while (GETREG(sc, ARSPI_REG_CTL) & ARSPI_CTL_BUSY);
    226       1.1  gdamore 
    227       1.1  gdamore 	arspi_done(sc, 0);
    228       1.1  gdamore 
    229       1.1  gdamore 	return 1;
    230       1.1  gdamore }
    231       1.1  gdamore 
    232       1.1  gdamore void
    233       1.1  gdamore arspi_poll(struct arspi_softc *sc)
    234       1.1  gdamore {
    235       1.1  gdamore 
    236       1.1  gdamore 	while (sc->sc_transfer) {
    237       1.1  gdamore 		arspi_intr(sc);
    238       1.1  gdamore 	}
    239       1.1  gdamore }
    240       1.1  gdamore 
    241       1.1  gdamore int
    242       1.1  gdamore arspi_configure(void *cookie, int slave, int mode, int speed)
    243       1.1  gdamore {
    244       1.1  gdamore 
    245       1.1  gdamore 	/*
    246       1.1  gdamore 	 * We don't support the full SPI protocol, and hopefully the
    247       1.1  gdamore 	 * firmware has programmed a reasonable mode already.  So
    248       1.1  gdamore 	 * just a couple of quick sanity checks, then bail.
    249       1.1  gdamore 	 */
    250       1.1  gdamore 	if ((mode != 0) || (slave != 0))
    251       1.1  gdamore 		return EINVAL;
    252       1.1  gdamore 
    253       1.1  gdamore 	return 0;
    254       1.1  gdamore }
    255       1.1  gdamore 
    256       1.1  gdamore int
    257       1.1  gdamore arspi_transfer(void *cookie, struct spi_transfer *st)
    258       1.1  gdamore {
    259       1.1  gdamore 	struct arspi_softc *sc = cookie;
    260       1.1  gdamore 	int rv;
    261       1.1  gdamore 	int s;
    262       1.1  gdamore 
    263       1.1  gdamore 	st->st_busprivate = NULL;
    264       1.1  gdamore 	if ((rv = arspi_make_job(st)) != 0) {
    265       1.1  gdamore 		if (st->st_busprivate) {
    266       1.1  gdamore 			free(st->st_busprivate, M_DEVBUF);
    267       1.1  gdamore 			st->st_busprivate = NULL;
    268       1.1  gdamore 		}
    269       1.1  gdamore 		spi_done(st, rv);
    270       1.1  gdamore 		return rv;
    271       1.1  gdamore 	}
    272       1.1  gdamore 
    273  1.5.76.1   cherry 	s = splbio();
    274       1.1  gdamore 	spi_transq_enqueue(&sc->sc_transq, st);
    275       1.1  gdamore 	if (sc->sc_transfer == NULL) {
    276       1.1  gdamore 		arspi_sched(sc);
    277       1.1  gdamore 		if (!sc->sc_interrupts)
    278       1.1  gdamore 			arspi_poll(sc);
    279       1.1  gdamore 	}
    280       1.1  gdamore 	splx(s);
    281       1.1  gdamore 	return 0;
    282       1.1  gdamore }
    283       1.1  gdamore 
    284       1.1  gdamore void
    285       1.1  gdamore arspi_sched(struct arspi_softc *sc)
    286       1.1  gdamore {
    287       1.1  gdamore 	struct spi_transfer *st;
    288       1.1  gdamore 	struct arspi_job *job;
    289       1.1  gdamore 	uint32_t ctl, cnt;
    290       1.1  gdamore 
    291       1.1  gdamore 	for (;;) {
    292       1.1  gdamore 		if ((st = sc->sc_transfer) == NULL) {
    293       1.1  gdamore 			if ((st = spi_transq_first(&sc->sc_transq)) == NULL) {
    294       1.1  gdamore 				/* no work left to do */
    295       1.1  gdamore 				break;
    296       1.1  gdamore 			}
    297       1.1  gdamore 			spi_transq_dequeue(&sc->sc_transq);
    298       1.1  gdamore 			sc->sc_transfer = st;
    299       1.1  gdamore 		}
    300       1.1  gdamore 
    301       1.1  gdamore 		arspi_update_job(st);
    302       1.1  gdamore 		job = st->st_busprivate;
    303       1.1  gdamore 
    304       1.1  gdamore 		/* there shouldn't be anything running, but ensure it */
    305       1.1  gdamore 		do {
    306       1.1  gdamore 			ctl = GETREG(sc, ARSPI_REG_CTL);
    307       1.1  gdamore 		}  while (ctl & ARSPI_CTL_BUSY);
    308       1.1  gdamore 		/* clear all of the tx and rx bits */
    309       1.1  gdamore 		ctl &= ~(ARSPI_CTL_TXCNT_MASK | ARSPI_CTL_RXCNT_MASK);
    310       1.1  gdamore 
    311       1.1  gdamore 		if (job->job_flags & JOB_WAIT) {
    312       1.1  gdamore 			PUTREG(sc, ARSPI_REG_OPCODE, SPIFLASH_CMD_RDSR);
    313       1.1  gdamore 			/* only the opcode for tx */
    314       1.1  gdamore 			ctl |= (1 << ARSPI_CTL_TXCNT_SHIFT);
    315       1.1  gdamore 			/* and one rx byte */
    316       1.1  gdamore 			ctl |= (1 << ARSPI_CTL_RXCNT_SHIFT);
    317       1.1  gdamore 		} else if (job->job_flags & JOB_WREN) {
    318       1.1  gdamore 			PUTREG(sc, ARSPI_REG_OPCODE, SPIFLASH_CMD_WREN);
    319       1.1  gdamore 			/* just the opcode */
    320       1.1  gdamore 			ctl |= (1 << ARSPI_CTL_TXCNT_SHIFT);
    321       1.1  gdamore 			/* no rx bytes */
    322       1.1  gdamore 		} else {
    323       1.1  gdamore 			/* set the data */
    324       1.1  gdamore 			PUTREG(sc, ARSPI_REG_DATA, job->job_data);
    325       1.1  gdamore 
    326       1.1  gdamore 			/* set the opcode and the address */
    327       1.1  gdamore 			PUTREG(sc, ARSPI_REG_OPCODE, job->job_opcode |
    328       1.1  gdamore 			    (job->job_addr << 8));
    329       1.1  gdamore 
    330       1.1  gdamore 			/* now set txcnt */
    331       1.1  gdamore 			cnt = 1;	/* opcode */
    332       1.1  gdamore 			cnt += job->job_addrcnt + job->job_txcnt;
    333       1.1  gdamore 			ctl |= (cnt << ARSPI_CTL_TXCNT_SHIFT);
    334       1.1  gdamore 
    335       1.1  gdamore 			/* now set rxcnt */
    336       1.1  gdamore 			cnt = job->job_rxcnt;
    337       1.1  gdamore 			ctl |= (cnt << ARSPI_CTL_RXCNT_SHIFT);
    338       1.1  gdamore 		}
    339       1.1  gdamore 
    340       1.1  gdamore 		/* set the start bit */
    341       1.1  gdamore 		ctl |= ARSPI_CTL_START;
    342       1.1  gdamore 
    343       1.1  gdamore 		PUTREG(sc, ARSPI_REG_CTL, ctl);
    344       1.1  gdamore 		break;
    345       1.1  gdamore 	}
    346       1.1  gdamore }
    347       1.1  gdamore 
    348       1.1  gdamore void
    349       1.1  gdamore arspi_done(struct arspi_softc *sc, int err)
    350       1.1  gdamore {
    351       1.1  gdamore 	struct spi_transfer *st;
    352       1.1  gdamore 	struct arspi_job *job;
    353       1.1  gdamore 
    354       1.1  gdamore 	if ((st = sc->sc_transfer) != NULL) {
    355       1.1  gdamore 		job = st->st_busprivate;
    356       1.1  gdamore 
    357       1.1  gdamore 		if (job->job_flags & JOB_WAIT) {
    358       1.1  gdamore 			if (err == 0) {
    359       1.1  gdamore 				if ((GETREG(sc, ARSPI_REG_DATA) &
    360       1.1  gdamore 				    SPIFLASH_SR_BUSY) == 0) {
    361       1.1  gdamore 					/* intermediate wait done */
    362       1.1  gdamore 					job->job_flags &= ~JOB_WAIT;
    363       1.1  gdamore 					goto done;
    364       1.1  gdamore 				}
    365       1.1  gdamore 			}
    366       1.1  gdamore 		} else if (job->job_flags & JOB_WREN) {
    367       1.1  gdamore 			if (err == 0) {
    368       1.1  gdamore 				job->job_flags &= ~JOB_WREN;
    369       1.1  gdamore 				goto done;
    370       1.1  gdamore 			}
    371       1.1  gdamore 		} else if (err == 0) {
    372       1.1  gdamore 			/*
    373       1.1  gdamore 			 * When breaking up write jobs, we have to wait until
    374       1.3      wiz 			 * the WIP bit is clear, and we have to separately
    375       1.1  gdamore 			 * send WREN for each chunk.  These flags facilitate
    376       1.1  gdamore 			 * that.
    377       1.1  gdamore 			 */
    378       1.1  gdamore 			if (job->job_flags & JOB_WRITE)
    379       1.1  gdamore 				job->job_flags |= (JOB_WAIT | JOB_WREN);
    380       1.1  gdamore 			job->job_data = GETREG(sc, ARSPI_REG_DATA);
    381       1.1  gdamore 			arspi_finish_job(st);
    382       1.1  gdamore 		}
    383       1.1  gdamore 
    384       1.1  gdamore 		if (err || (job->job_flags & JOB_LAST)) {
    385       1.1  gdamore 			sc->sc_transfer = NULL;
    386       1.1  gdamore 			st->st_busprivate = NULL;
    387       1.1  gdamore 			spi_done(st, err);
    388       1.1  gdamore 			free(job, M_DEVBUF);
    389       1.1  gdamore 		}
    390       1.1  gdamore 	}
    391       1.1  gdamore done:
    392       1.1  gdamore 	arspi_sched(sc);
    393       1.1  gdamore }
    394       1.1  gdamore 
    395       1.1  gdamore int
    396       1.1  gdamore arspi_get_byte(struct spi_chunk **chunkp, uint8_t *bytep)
    397       1.1  gdamore {
    398       1.1  gdamore 	struct spi_chunk *chunk;
    399       1.1  gdamore 
    400       1.1  gdamore 	chunk = *chunkp;
    401       1.1  gdamore 
    402       1.1  gdamore 	/* skip leading empty (or already consumed) chunks */
    403       1.1  gdamore 	while (chunk && chunk->chunk_wresid == 0)
    404       1.1  gdamore 		chunk = chunk->chunk_next;
    405       1.1  gdamore 
    406       1.1  gdamore 	if (chunk == NULL) {
    407       1.1  gdamore 		return ENODATA;
    408       1.1  gdamore 	}
    409       1.1  gdamore 
    410       1.1  gdamore 	/*
    411       1.1  gdamore 	 * chunk must be write only.  SPI flash doesn't support
    412       1.1  gdamore 	 * any full duplex operations.
    413       1.1  gdamore 	 */
    414       1.1  gdamore 	if ((chunk->chunk_rptr) || !(chunk->chunk_wptr)) {
    415       1.1  gdamore 		return EINVAL;
    416       1.1  gdamore 	}
    417       1.1  gdamore 
    418       1.1  gdamore 	*bytep = *chunk->chunk_wptr;
    419       1.1  gdamore 	chunk->chunk_wptr++;
    420       1.1  gdamore 	chunk->chunk_wresid--;
    421       1.1  gdamore 	chunk->chunk_rresid--;
    422       1.1  gdamore 	/* clearing wptr and rptr makes sanity checks later easier */
    423       1.1  gdamore 	if (chunk->chunk_wresid == 0)
    424       1.1  gdamore 		chunk->chunk_wptr = NULL;
    425       1.1  gdamore 	if (chunk->chunk_rresid == 0)
    426       1.1  gdamore 		chunk->chunk_rptr = NULL;
    427       1.1  gdamore 	while (chunk && chunk->chunk_wresid == 0)
    428       1.1  gdamore 		chunk = chunk->chunk_next;
    429       1.1  gdamore 
    430       1.1  gdamore 	*chunkp = chunk;
    431       1.1  gdamore 	return 0;
    432       1.1  gdamore }
    433       1.1  gdamore 
    434       1.1  gdamore int
    435       1.1  gdamore arspi_put_byte(struct spi_chunk **chunkp, uint8_t byte)
    436       1.1  gdamore {
    437       1.1  gdamore 	struct spi_chunk *chunk;
    438       1.1  gdamore 
    439       1.1  gdamore 	chunk = *chunkp;
    440       1.1  gdamore 
    441       1.1  gdamore 	/* skip leading empty (or already consumed) chunks */
    442       1.1  gdamore 	while (chunk && chunk->chunk_rresid == 0)
    443       1.1  gdamore 		chunk = chunk->chunk_next;
    444       1.1  gdamore 
    445       1.1  gdamore 	if (chunk == NULL) {
    446       1.1  gdamore 		return EOVERFLOW;
    447       1.1  gdamore 	}
    448       1.1  gdamore 
    449       1.1  gdamore 	/*
    450       1.1  gdamore 	 * chunk must be read only.  SPI flash doesn't support
    451       1.1  gdamore 	 * any full duplex operations.
    452       1.1  gdamore 	 */
    453       1.1  gdamore 	if ((chunk->chunk_wptr) || !(chunk->chunk_rptr)) {
    454       1.1  gdamore 		return EINVAL;
    455       1.1  gdamore 	}
    456       1.1  gdamore 
    457       1.1  gdamore 	*chunk->chunk_rptr = byte;
    458       1.1  gdamore 	chunk->chunk_rptr++;
    459       1.1  gdamore 	chunk->chunk_wresid--;	/* technically this was done at send time */
    460       1.1  gdamore 	chunk->chunk_rresid--;
    461       1.1  gdamore 	while (chunk && chunk->chunk_rresid == 0)
    462       1.1  gdamore 		chunk = chunk->chunk_next;
    463       1.1  gdamore 
    464       1.1  gdamore 	*chunkp = chunk;
    465       1.1  gdamore 	return 0;
    466       1.1  gdamore }
    467       1.1  gdamore 
    468       1.1  gdamore int
    469       1.1  gdamore arspi_make_job(struct spi_transfer *st)
    470       1.1  gdamore {
    471       1.1  gdamore 	struct arspi_job *job;
    472       1.1  gdamore 	struct spi_chunk *chunk;
    473       1.1  gdamore 	uint8_t byte;
    474       1.1  gdamore 	int i, rv;
    475       1.1  gdamore 
    476       1.1  gdamore 	job = malloc(sizeof (struct arspi_job), M_DEVBUF, M_ZERO);
    477       1.1  gdamore 	if (job == NULL) {
    478       1.1  gdamore 		return ENOMEM;
    479       1.1  gdamore 	}
    480       1.1  gdamore 
    481       1.1  gdamore 	st->st_busprivate = job;
    482       1.1  gdamore 
    483       1.1  gdamore 	/* skip any leading empty chunks (should not be any!) */
    484       1.1  gdamore 	chunk = st->st_chunks;
    485       1.1  gdamore 
    486       1.1  gdamore 	/* get transfer opcode */
    487       1.1  gdamore 	if ((rv = arspi_get_byte(&chunk, &byte)) != 0)
    488       1.1  gdamore 		return rv;
    489       1.1  gdamore 
    490       1.1  gdamore 	job->job_opcode = byte;
    491       1.1  gdamore 	switch (job->job_opcode) {
    492       1.1  gdamore 	case SPIFLASH_CMD_WREN:
    493       1.1  gdamore 	case SPIFLASH_CMD_WRDI:
    494       1.1  gdamore 	case SPIFLASH_CMD_CHIPERASE:
    495       1.1  gdamore 		break;
    496       1.1  gdamore 	case SPIFLASH_CMD_RDJI:
    497       1.1  gdamore 		job->job_rxcnt = 3;
    498       1.1  gdamore 		break;
    499       1.1  gdamore 	case SPIFLASH_CMD_RDSR:
    500       1.1  gdamore 		job->job_rxcnt = 1;
    501       1.1  gdamore 		break;
    502       1.1  gdamore 	case SPIFLASH_CMD_WRSR:
    503       1.1  gdamore 		/*
    504       1.1  gdamore 		 * is this in data, or in address?  stick it in data
    505       1.1  gdamore 		 * for now.
    506       1.1  gdamore 		 */
    507       1.1  gdamore 		job->job_txcnt = 1;
    508       1.1  gdamore 		break;
    509       1.1  gdamore 	case SPIFLASH_CMD_RDID:
    510       1.1  gdamore 		job->job_addrcnt = 3;	/* 3 dummy bytes */
    511       1.1  gdamore 		job->job_rxcnt = 1;
    512       1.1  gdamore 		break;
    513       1.1  gdamore 	case SPIFLASH_CMD_ERASE:
    514       1.1  gdamore 		job->job_addrcnt = 3;
    515       1.1  gdamore 		break;
    516       1.1  gdamore 	case SPIFLASH_CMD_READ:
    517       1.1  gdamore 		job->job_addrcnt = 3;
    518       1.1  gdamore 		job->job_flags |= JOB_READ;
    519       1.1  gdamore 		break;
    520       1.1  gdamore 	case SPIFLASH_CMD_PROGRAM:
    521       1.1  gdamore 		job->job_addrcnt = 3;
    522       1.1  gdamore 		job->job_flags |= JOB_WRITE;
    523       1.1  gdamore 		break;
    524       1.1  gdamore 	case SPIFLASH_CMD_READFAST:
    525       1.1  gdamore 		/*
    526       1.1  gdamore 		 * This is a pain in the arse to support, so we will
    527       1.1  gdamore 		 * rewrite as an ordinary read.  But later, after we
    528       1.1  gdamore 		 * obtain the address.
    529       1.1  gdamore 		 */
    530       1.1  gdamore 		job->job_addrcnt = 3;	/* 3 address */
    531       1.1  gdamore 		job->job_flags |= JOB_READ;
    532       1.1  gdamore 		break;
    533       1.1  gdamore 	default:
    534       1.1  gdamore 		return EINVAL;
    535       1.1  gdamore 	}
    536       1.1  gdamore 
    537       1.1  gdamore 	for (i = 0; i < job->job_addrcnt; i++) {
    538       1.1  gdamore 		if ((rv = arspi_get_byte(&chunk, &byte)) != 0)
    539       1.1  gdamore 			return rv;
    540       1.1  gdamore 		job->job_addr <<= 8;
    541       1.1  gdamore 		job->job_addr |= byte;
    542       1.1  gdamore 	}
    543       1.1  gdamore 
    544       1.1  gdamore 
    545       1.1  gdamore 	if (job->job_opcode == SPIFLASH_CMD_READFAST) {
    546       1.1  gdamore 		/* eat the dummy timing byte */
    547       1.1  gdamore 		if ((rv = arspi_get_byte(&chunk, &byte)) != 0)
    548       1.1  gdamore 			return rv;
    549       1.1  gdamore 		/* rewrite this as a read */
    550       1.1  gdamore 		job->job_opcode = SPIFLASH_CMD_READ;
    551       1.1  gdamore 	}
    552       1.1  gdamore 
    553       1.1  gdamore 	job->job_chunk = chunk;
    554       1.1  gdamore 
    555       1.1  gdamore 	/*
    556       1.1  gdamore 	 * Now quickly check a few other things.   Namely, we are not
    557       1.1  gdamore 	 * allowed to have both READ and WRITE.
    558       1.1  gdamore 	 */
    559       1.1  gdamore 	for (chunk = job->job_chunk; chunk; chunk = chunk->chunk_next) {
    560       1.1  gdamore 		if (chunk->chunk_wptr) {
    561       1.1  gdamore 			job->job_wresid += chunk->chunk_wresid;
    562       1.1  gdamore 		}
    563       1.1  gdamore 		if (chunk->chunk_rptr) {
    564       1.1  gdamore 			job->job_rresid += chunk->chunk_rresid;
    565       1.1  gdamore 		}
    566       1.1  gdamore 	}
    567       1.1  gdamore 
    568       1.1  gdamore 	if (job->job_rresid && job->job_wresid) {
    569       1.1  gdamore 		return EINVAL;
    570       1.1  gdamore 	}
    571       1.1  gdamore 
    572       1.1  gdamore 	return 0;
    573       1.1  gdamore }
    574       1.1  gdamore 
    575       1.2  gdamore /*
    576       1.2  gdamore  * NB: The Atheros SPI controller runs in little endian mode. So all
    577       1.2  gdamore  * data accesses must be swapped appropriately.
    578       1.2  gdamore  *
    579       1.2  gdamore  * The controller auto-swaps read accesses done through the mapped memory
    580       1.2  gdamore  * region, but when using SPI directly, we have to do the right thing to
    581       1.2  gdamore  * swap to or from little endian.
    582       1.2  gdamore  */
    583       1.2  gdamore 
    584       1.1  gdamore void
    585       1.1  gdamore arspi_update_job(struct spi_transfer *st)
    586       1.1  gdamore {
    587       1.1  gdamore 	struct arspi_job *job = st->st_busprivate;
    588       1.1  gdamore 	uint8_t byte;
    589       1.1  gdamore 	int i;
    590       1.1  gdamore 
    591       1.1  gdamore 	if (job->job_flags & (JOB_WAIT|JOB_WREN))
    592       1.1  gdamore 		return;
    593       1.1  gdamore 
    594       1.1  gdamore 	job->job_rxcnt = 0;
    595       1.1  gdamore 	job->job_txcnt = 0;
    596       1.1  gdamore 	job->job_data = 0;
    597       1.1  gdamore 
    598       1.1  gdamore 	job->job_txcnt = min(job->job_wresid, 4);
    599       1.1  gdamore 	job->job_rxcnt = min(job->job_rresid, 4);
    600       1.1  gdamore 
    601       1.1  gdamore 	job->job_wresid -= job->job_txcnt;
    602       1.1  gdamore 	job->job_rresid -= job->job_rxcnt;
    603       1.1  gdamore 
    604       1.1  gdamore 	for (i = 0; i < job->job_txcnt; i++) {
    605       1.1  gdamore 		arspi_get_byte(&job->job_chunk, &byte);
    606       1.2  gdamore 		job->job_data |= (byte << (i * 8));
    607       1.1  gdamore 	}
    608       1.1  gdamore 
    609       1.1  gdamore 	if ((!job->job_wresid) && (!job->job_rresid)) {
    610       1.1  gdamore 		job->job_flags |= JOB_LAST;
    611       1.1  gdamore 	}
    612       1.1  gdamore }
    613       1.1  gdamore 
    614       1.1  gdamore void
    615       1.1  gdamore arspi_finish_job(struct spi_transfer *st)
    616       1.1  gdamore {
    617       1.1  gdamore 	struct arspi_job *job = st->st_busprivate;
    618       1.1  gdamore 	uint8_t	byte;
    619       1.1  gdamore 	int i;
    620       1.1  gdamore 
    621       1.1  gdamore 	job->job_addr += job->job_rxcnt;
    622       1.1  gdamore 	job->job_addr += job->job_txcnt;
    623       1.1  gdamore 	for (i = 0; i < job->job_rxcnt; i++) {
    624       1.1  gdamore 		byte = job->job_data & 0xff;
    625       1.1  gdamore 		job->job_data >>= 8;
    626       1.1  gdamore 		arspi_put_byte(&job->job_chunk, byte);
    627       1.1  gdamore 	}
    628       1.1  gdamore }
    629       1.1  gdamore 
    630