/*-
* Copyright (c) 2006 Urbana-Champaign Independent Media Center.
* Copyright (c) 2006 Garrett D'Amore.
* All rights reserved.
*
* Portions of this code were written by Garrett D'Amore for the
* Champaign-Urbana Community Wireless Network Project.
*
* Redistribution and use in source and binary forms, with or
* without modification, are permitted provided that the following
* conditions are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
* 3. All advertising materials mentioning features or use of this
* software must display the following acknowledgements:
* This product includes software developed by the Urbana-Champaign
* Independent Media Center.
* This product includes software developed by Garrett D'Amore.
* 4. Urbana-Champaign Independent Media Center's name and Garrett
* D'Amore's name may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE URBANA-CHAMPAIGN INDEPENDENT
* MEDIA CENTER AND GARRETT D'AMORE ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE URBANA-CHAMPAIGN INDEPENDENT
* MEDIA CENTER OR GARRETT D'AMORE BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/*
* We assume a base clock of 48MHz has been established by the
* platform code. The clock divider reduces this to 24MHz.
* Next we have to figure out the BRG
*/
#define BASECLK 24000000
for (brg = 0; brg < 64; brg++) {
if (speed >= (BASECLK / ((brg + 1) * 2))) {
break;
}
}
/*
* Does the device want to go even slower? Our minimum speed without
* changing other assumptions, and complicating the code even further,
* is 24MHz/128, or 187.5kHz. That should be slow enough for any
* device we're likely to encounter.
*/
if (speed < (BASECLK / ((brg + 1) * 2))) {
return EINVAL;
}
reg &= ~SPICFG_BRG_MASK;
reg |= (brg << SPICFG_BRG_SHIFT);
/*
* I'm not entirely confident that these values are correct.
* But at least mode 0 appears to work properly with the
* devices I have tested. The documentation seems to suggest
* that I have the meaning of the clock delay bit inverted.
*/
switch (mode) {
case SPI_MODE_0:
reg |= 0; /* CPHA = 0, CPOL = 0 */
break;
case SPI_MODE_1:
reg |= SPICFG_CDE; /* CPHA = 1, CPOL = 0 */
break;
case SPI_MODE_2:
reg |= SPICFG_BI; /* CPHA = 0, CPOL = 1 */
break;
case SPI_MODE_3:
reg |= SPICFG_CDE | SPICFG_BI; /* CPHA = 1, CPOL = 1 */
break;
default:
return EINVAL;
}
PUTREG(sc, AUPSC_SPICFG, reg);
for (i = 1000000; i; i -= 10) {
if (GETREG(sc, AUPSC_SPISTAT) & SPISTAT_DR) {
return 0;
}
}
if (chunk->chunk_wptr) {
data = *chunk->chunk_wptr++;
} else {
data = 0;
}
chunk->chunk_wresid--;
/* if the last outbound character, mark it */
if ((chunk->chunk_wresid == 0) &&
(chunk->chunk_next == NULL)) {
data |= SPITXRX_LC;
}
PUTREG(sc, AUPSC_SPITXRX, data);
}
/* advance to next transfer */
sc->sc_wchunk = sc->sc_wchunk->chunk_next;
}
}
/* clear the fifos */
PUTREG(sc, AUPSC_SPIPCR, SPIPCR_RC | SPIPCR_TC);
/* setup chunks */
sc->sc_rchunk = sc->sc_wchunk = st->st_chunks;
auspi_send(sc);
/* now kick the master start to get the chip running */
PUTREG(sc, AUPSC_SPIPCR, SPIPCR_MS);
sc->sc_running = true;
return;
}
auspi_select(sc, -1);
sc->sc_running = false;
}
void
auspi_done(struct auspi_softc *sc, int err)
{
struct spi_transfer *st;
/* called from interrupt handler */
if ((st = sc->sc_transfer) != NULL) {
sc->sc_transfer = NULL;
spi_done(st, err);
}
/* make sure we clear these bits out */
sc->sc_wchunk = sc->sc_rchunk = NULL;
auspi_sched(sc);
}
int
auspi_intr(void *arg)
{
struct auspi_softc *sc = arg;
uint32_t ev;
int err = 0;
/* do all data exchanges */
auspi_send(sc);
auspi_recv(sc);
/*
* if the master done bit is set, make sure we do the
* right processing.
*/
if (ev & SPIMSK_MD) {
if ((sc->sc_wchunk != NULL) ||
(sc->sc_rchunk != NULL)) {
printf("%s: partial transfer?\n",
device_xname(sc->sc_dev));
err = EIO;
}
auspi_done(sc, err);
}
/* clear interrupts */
PUTREG(sc, AUPSC_SPIEVNT,
ev & (SPIMSK_TR | SPIMSK_RR | SPIMSK_MD));
}
return 1;
}
int
auspi_transfer(void *arg, struct spi_transfer *st)
{
struct auspi_softc *sc = arg;
int s;
/* make sure we select the right chip */
s = splbio();
spi_transq_enqueue(&sc->sc_q, st);
if (sc->sc_running == 0) {
auspi_sched(sc);
}
splx(s);
return 0;
}