2007-09-21 15:51:53 +00:00
|
|
|
/***************************************************************************
|
|
|
|
* __________ __ ___.
|
|
|
|
* Open \______ \ ____ ____ | | _\_ |__ _______ ___
|
|
|
|
* Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
|
|
|
|
* Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
|
|
|
|
* Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
|
|
|
|
* \/ \/ \/ \/ \/
|
|
|
|
* $Id$
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Will Robertson
|
|
|
|
*
|
2008-06-28 18:10:04 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2007-09-21 15:51:53 +00:00
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
****************************************************************************/
|
2008-04-27 21:32:10 +00:00
|
|
|
#include "config.h"
|
|
|
|
#include "system.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "spi-imx31.h"
|
2008-04-11 08:51:27 +00:00
|
|
|
#include "avic-imx31.h"
|
2009-03-22 02:13:27 +00:00
|
|
|
#include "ccm-imx31.h"
|
2007-09-21 15:51:53 +00:00
|
|
|
#include "debug.h"
|
|
|
|
#include "kernel.h"
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
/* Forward interrupt handler declarations */
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI1_MODULE)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void CSPI1_HANDLER(void);
|
|
|
|
#endif
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI2_MODULE)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void CSPI2_HANDLER(void);
|
|
|
|
#endif
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI3_MODULE)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void CSPI3_HANDLER(void);
|
|
|
|
#endif
|
|
|
|
|
2010-05-07 02:29:18 +00:00
|
|
|
#define RXDATA (0x000 / sizeof (unsigned long)) /* 000h */
|
|
|
|
#define TXDATA (0x004 / sizeof (unsigned long)) /* 004h */
|
|
|
|
#define CONREG (0x008 / sizeof (unsigned long)) /* 008h */
|
|
|
|
#define INTREG (0x00c / sizeof (unsigned long)) /* 00Ch */
|
|
|
|
#define DMAREG (0x010 / sizeof (unsigned long)) /* 010h */
|
|
|
|
#define STATREG (0x014 / sizeof (unsigned long)) /* 014h */
|
|
|
|
#define PERIODREG (0x01c / sizeof (unsigned long)) /* 018h */
|
|
|
|
#define TESTREG (0x1c0 / sizeof (unsigned long)) /* 1C0h */
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
/* State data associatated with each CSPI module */
|
2010-05-04 10:07:53 +00:00
|
|
|
static struct spi_module_desc
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-07 02:29:18 +00:00
|
|
|
volatile unsigned long * const base; /* CSPI module address */
|
|
|
|
struct spi_transfer_desc *head; /* Running job */
|
|
|
|
struct spi_transfer_desc *tail; /* Most recent job added */
|
|
|
|
const struct spi_node *last_node; /* Last node used for module */
|
|
|
|
void (* const handler)(void); /* Interrupt handler */
|
|
|
|
int rxcount; /* Independent copy of txcount */
|
|
|
|
int8_t enab; /* Enable count */
|
|
|
|
int8_t byte_size; /* Size of transfers in bytes */
|
|
|
|
const int8_t cg; /* Clock-gating value */
|
|
|
|
const int8_t ints; /* AVIC vector number */
|
2008-04-11 08:51:27 +00:00
|
|
|
} spi_descs[SPI_NUM_CSPI] =
|
|
|
|
/* Init non-zero members */
|
|
|
|
{
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI1_MODULE)
|
|
|
|
{
|
2010-05-07 02:29:18 +00:00
|
|
|
.base = (unsigned long *)CSPI1_BASE_ADDR,
|
2008-04-11 08:51:27 +00:00
|
|
|
.cg = CG_CSPI1,
|
2009-03-22 01:50:48 +00:00
|
|
|
.ints = INT_CSPI1,
|
2008-04-11 08:51:27 +00:00
|
|
|
.handler = CSPI1_HANDLER,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI2_MODULE)
|
|
|
|
{
|
2010-05-07 02:29:18 +00:00
|
|
|
.base = (unsigned long *)CSPI2_BASE_ADDR,
|
2008-04-11 08:51:27 +00:00
|
|
|
.cg = CG_CSPI2,
|
2009-03-22 01:50:48 +00:00
|
|
|
.ints = INT_CSPI2,
|
2008-04-11 08:51:27 +00:00
|
|
|
.handler = CSPI2_HANDLER,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI3_MODULE)
|
|
|
|
{
|
2010-05-07 02:29:18 +00:00
|
|
|
.base = (unsigned long *)CSPI3_BASE_ADDR,
|
2008-04-11 08:51:27 +00:00
|
|
|
.cg = CG_CSPI3,
|
2009-03-22 01:50:48 +00:00
|
|
|
.ints = INT_CSPI3,
|
2008-04-11 08:51:27 +00:00
|
|
|
.handler = CSPI3_HANDLER,
|
|
|
|
},
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Reset the module */
|
|
|
|
static void spi_reset(struct spi_module_desc * const desc)
|
|
|
|
{
|
|
|
|
/* Reset by leaving it disabled */
|
2010-05-07 02:29:18 +00:00
|
|
|
desc->base[CONREG] &= ~CSPI_CONREG_EN;
|
2010-05-04 10:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the context for the node and remember it to avoid unneeded reconfigure */
|
|
|
|
static bool spi_set_context(struct spi_module_desc *desc,
|
|
|
|
struct spi_transfer_desc *xfer)
|
|
|
|
{
|
|
|
|
const struct spi_node * const node = xfer->node;
|
2010-05-07 02:29:18 +00:00
|
|
|
volatile unsigned long * const base = desc->base;
|
2010-05-04 10:07:53 +00:00
|
|
|
|
|
|
|
if (desc->enab == 0)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (node == desc->last_node)
|
|
|
|
return true;
|
|
|
|
|
|
|
|
/* Errata says CSPI should be disabled when writing PERIODREG. */
|
2010-05-07 02:29:18 +00:00
|
|
|
base[CONREG] &= ~CSPI_CONREG_EN;
|
2010-05-04 10:07:53 +00:00
|
|
|
|
|
|
|
/* Switch the module's node */
|
|
|
|
desc->last_node = node;
|
|
|
|
desc->byte_size = (((node->conreg >> 8) & 0x1f) + 1 + 7) / 8 - 1;
|
|
|
|
|
|
|
|
/* Set the wait-states */
|
2010-05-07 02:29:18 +00:00
|
|
|
base[PERIODREG] = node->periodreg & 0xffff;
|
2010-05-04 10:07:53 +00:00
|
|
|
|
|
|
|
/* Keep reserved and start bits cleared. Keep enabled bit. */
|
2010-05-07 02:29:18 +00:00
|
|
|
base[CONREG] =
|
2010-05-04 10:07:53 +00:00
|
|
|
(node->conreg & ~(0xfcc8e000 | CSPI_CONREG_XCH | CSPI_CONREG_SMC));
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Fill the TX fifo. Returns the number of remaining words. */
|
|
|
|
static int tx_fill_fifo(struct spi_module_desc * const desc,
|
2010-05-07 02:29:18 +00:00
|
|
|
volatile unsigned long * const base,
|
2010-05-04 10:07:53 +00:00
|
|
|
struct spi_transfer_desc * const xfer)
|
|
|
|
{
|
|
|
|
int count = xfer->count;
|
|
|
|
int size = desc->byte_size;
|
|
|
|
|
2010-05-07 02:29:18 +00:00
|
|
|
while ((base[STATREG] & CSPI_STATREG_TF) == 0)
|
2010-05-04 10:07:53 +00:00
|
|
|
{
|
|
|
|
uint32_t word = 0;
|
|
|
|
|
|
|
|
switch (size & 3)
|
|
|
|
{
|
|
|
|
case 3:
|
|
|
|
word = *(unsigned char *)(xfer->txbuf + 3) << 24;
|
|
|
|
case 2:
|
|
|
|
word |= *(unsigned char *)(xfer->txbuf + 2) << 16;
|
|
|
|
case 1:
|
|
|
|
word |= *(unsigned char *)(xfer->txbuf + 1) << 8;
|
|
|
|
case 0:
|
|
|
|
word |= *(unsigned char *)(xfer->txbuf + 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
xfer->txbuf += size + 1; /* Increment buffer */
|
|
|
|
|
2010-05-07 02:29:18 +00:00
|
|
|
base[TXDATA] = word; /* Write to FIFO */
|
2010-05-04 10:07:53 +00:00
|
|
|
|
|
|
|
if (--count == 0)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
xfer->count = count;
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Start a transfer on the SPI */
|
|
|
|
static bool start_transfer(struct spi_module_desc * const desc,
|
|
|
|
struct spi_transfer_desc * const xfer)
|
|
|
|
{
|
2010-05-07 02:29:18 +00:00
|
|
|
volatile unsigned long * const base = desc->base;
|
2010-05-04 10:07:53 +00:00
|
|
|
unsigned long intreg;
|
|
|
|
|
|
|
|
if (!spi_set_context(desc, xfer))
|
|
|
|
return false;
|
|
|
|
|
2010-05-07 02:29:18 +00:00
|
|
|
base[CONREG] |= CSPI_CONREG_EN; /* Enable module */
|
2010-05-04 10:07:53 +00:00
|
|
|
|
|
|
|
desc->rxcount = xfer->count;
|
|
|
|
|
|
|
|
intreg = (xfer->count < 8) ?
|
|
|
|
CSPI_INTREG_TCEN : /* Trans. complete: TX will run out in prefill */
|
|
|
|
CSPI_INTREG_THEN; /* INT when TX half-empty */
|
|
|
|
|
|
|
|
intreg |= (xfer->count < 4) ?
|
|
|
|
CSPI_INTREG_RREN : /* Must grab data on every word */
|
|
|
|
CSPI_INTREG_RHEN; /* Enough data to wait for half-full */
|
|
|
|
|
|
|
|
tx_fill_fifo(desc, base, xfer);
|
|
|
|
|
2010-05-07 02:29:18 +00:00
|
|
|
base[STATREG] = CSPI_STATREG_TC; /* Ack 'complete' */
|
|
|
|
base[INTREG] = intreg; /* Enable interrupts */
|
|
|
|
base[CONREG] |= CSPI_CONREG_XCH; /* Begin transfer */
|
2010-05-04 10:07:53 +00:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
/* Common code for interrupt handlers */
|
|
|
|
static void spi_interrupt(enum spi_module_number spi)
|
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
struct spi_module_desc *desc = &spi_descs[spi];
|
2010-05-07 02:29:18 +00:00
|
|
|
volatile unsigned long * const base = desc->base;
|
|
|
|
unsigned long intreg = base[INTREG];
|
2010-05-04 10:07:53 +00:00
|
|
|
struct spi_transfer_desc *xfer = desc->head;
|
2008-04-11 08:51:27 +00:00
|
|
|
int inc = desc->byte_size + 1;
|
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Data received - empty out RXFIFO */
|
2010-05-07 02:29:18 +00:00
|
|
|
while ((base[STATREG] & CSPI_STATREG_RR) != 0)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-07 02:29:18 +00:00
|
|
|
uint32_t word = base[RXDATA];
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (desc->rxcount <= 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (xfer->rxbuf != NULL)
|
|
|
|
{
|
|
|
|
/* There is a receive buffer */
|
2008-04-11 08:51:27 +00:00
|
|
|
switch (desc->byte_size & 3)
|
|
|
|
{
|
|
|
|
case 3:
|
2010-05-04 10:07:53 +00:00
|
|
|
*(unsigned char *)(xfer->rxbuf + 3) = word >> 24;
|
2008-04-11 08:51:27 +00:00
|
|
|
case 2:
|
2010-05-04 10:07:53 +00:00
|
|
|
*(unsigned char *)(xfer->rxbuf + 2) = word >> 16;
|
2008-04-11 08:51:27 +00:00
|
|
|
case 1:
|
2010-05-04 10:07:53 +00:00
|
|
|
*(unsigned char *)(xfer->rxbuf + 1) = word >> 8;
|
2008-04-11 08:51:27 +00:00
|
|
|
case 0:
|
2010-05-04 10:07:53 +00:00
|
|
|
*(unsigned char *)(xfer->rxbuf + 0) = word;
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
xfer->rxbuf += inc;
|
|
|
|
}
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (--desc->rxcount < 4)
|
|
|
|
{
|
|
|
|
if (desc->rxcount == 0)
|
|
|
|
{
|
|
|
|
/* No more to receive - stop RX interrupts */
|
|
|
|
intreg &= ~(CSPI_INTREG_RHEN | CSPI_INTREG_RREN);
|
2010-05-07 02:29:18 +00:00
|
|
|
base[INTREG] = intreg;
|
2010-05-04 10:07:53 +00:00
|
|
|
}
|
|
|
|
else if (intreg & CSPI_INTREG_RHEN)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
/* < 4 words expected - switch to RX ready */
|
|
|
|
intreg &= ~CSPI_INTREG_RHEN;
|
|
|
|
intreg |= CSPI_INTREG_RREN;
|
2010-05-07 02:29:18 +00:00
|
|
|
base[INTREG] = intreg;
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (xfer->count > 0)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Data to transmit - fill TXFIFO or write until exhausted. */
|
|
|
|
if (tx_fill_fifo(desc, base, xfer) != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Out of data - stop TX interrupts, enable TC interrupt. */
|
|
|
|
intreg &= ~CSPI_INTREG_THEN;
|
|
|
|
intreg |= CSPI_INTREG_TCEN;
|
2010-05-07 02:29:18 +00:00
|
|
|
base[INTREG] = intreg;
|
2010-05-04 10:07:53 +00:00
|
|
|
}
|
|
|
|
|
2010-05-07 02:29:18 +00:00
|
|
|
if ((intreg & CSPI_INTREG_TCEN) && (base[STATREG] & CSPI_STATREG_TC))
|
2010-05-04 10:07:53 +00:00
|
|
|
{
|
|
|
|
/* Outbound transfer is complete. */
|
|
|
|
intreg &= ~CSPI_INTREG_TCEN;
|
2010-05-07 02:29:18 +00:00
|
|
|
base[INTREG] = intreg;
|
|
|
|
base[STATREG] = CSPI_STATREG_TC; /* Ack 'complete' */
|
2010-05-04 10:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (intreg != 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* All interrupts are masked; we're done with current transfer. */
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
struct spi_transfer_desc *next = xfer->next;
|
|
|
|
spi_transfer_cb_fn_type callback = xfer->callback;
|
|
|
|
xfer->next = NULL;
|
|
|
|
|
2010-05-07 02:29:18 +00:00
|
|
|
base[CONREG] &= ~CSPI_CONREG_EN; /* Disable module */
|
2010-05-04 10:07:53 +00:00
|
|
|
|
|
|
|
if (next == xfer)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Last job on queue */
|
|
|
|
desc->head = NULL;
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (callback != NULL)
|
|
|
|
callback(xfer);
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Callback may have restarted transfers. */
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Queue next job. */
|
|
|
|
desc->head = next;
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (callback != NULL)
|
|
|
|
callback(xfer);
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (!start_transfer(desc, next))
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
xfer = next;
|
|
|
|
xfer->count = -1;
|
|
|
|
continue; /* Failed: try next */
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
break;
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Interrupt handlers for each CSPI module */
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI1_MODULE)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void CSPI1_HANDLER(void)
|
|
|
|
{
|
|
|
|
spi_interrupt(CSPI1_NUM);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI2_MODULE)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void CSPI2_HANDLER(void)
|
|
|
|
{
|
|
|
|
spi_interrupt(CSPI2_NUM);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if (SPI_MODULE_MASK & USE_CSPI3_MODULE)
|
|
|
|
static __attribute__((interrupt("IRQ"))) void CSPI3_HANDLER(void)
|
|
|
|
{
|
|
|
|
spi_interrupt(CSPI3_NUM);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Initialize the SPI driver */
|
2008-04-11 08:51:27 +00:00
|
|
|
void spi_init(void)
|
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
unsigned i;
|
2008-04-11 08:51:27 +00:00
|
|
|
for (i = 0; i < SPI_NUM_CSPI; i++)
|
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
struct spi_module_desc * const desc = &spi_descs[i];
|
|
|
|
ccm_module_clock_gating(desc->cg, CGM_ON_RUN_WAIT);
|
|
|
|
spi_reset(desc);
|
|
|
|
ccm_module_clock_gating(desc->cg, CGM_OFF);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable the specified module for the node */
|
2010-05-04 10:07:53 +00:00
|
|
|
void spi_enable_module(const struct spi_node *node)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
struct spi_module_desc * const desc = &spi_descs[node->num];
|
2008-04-11 08:51:27 +00:00
|
|
|
|
|
|
|
if (++desc->enab == 1)
|
|
|
|
{
|
|
|
|
/* Enable clock-gating register */
|
2009-03-22 01:50:48 +00:00
|
|
|
ccm_module_clock_gating(desc->cg, CGM_ON_RUN_WAIT);
|
2008-04-11 08:51:27 +00:00
|
|
|
/* Reset */
|
2010-05-04 10:07:53 +00:00
|
|
|
spi_reset(desc);
|
|
|
|
desc->last_node = NULL;
|
2008-04-11 08:51:27 +00:00
|
|
|
/* Enable interrupt at controller level */
|
2009-03-22 01:50:48 +00:00
|
|
|
avic_enable_int(desc->ints, INT_TYPE_IRQ, INT_PRIO_DEFAULT,
|
|
|
|
desc->handler);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Disable the specified module for the node */
|
|
|
|
void spi_disable_module(const struct spi_node *node)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
struct spi_module_desc * const desc = &spi_descs[node->num];
|
2008-04-11 08:51:27 +00:00
|
|
|
|
|
|
|
if (desc->enab > 0 && --desc->enab == 0)
|
|
|
|
{
|
|
|
|
/* Last enable for this module */
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Wait for outstanding transactions */
|
|
|
|
while (*(void ** volatile)&desc->head != NULL);
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
/* Disable interrupt at controller level */
|
|
|
|
avic_disable_int(desc->ints);
|
|
|
|
|
|
|
|
/* Disable interface */
|
2010-05-07 02:29:18 +00:00
|
|
|
desc->base[CONREG] &= ~CSPI_CONREG_EN;
|
2008-04-11 08:51:27 +00:00
|
|
|
|
|
|
|
/* Disable interface clock */
|
2009-03-22 01:50:48 +00:00
|
|
|
ccm_module_clock_gating(desc->cg, CGM_OFF);
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-04-11 08:51:27 +00:00
|
|
|
/* Send and/or receive data on the specified node */
|
2010-05-04 10:07:53 +00:00
|
|
|
bool spi_transfer(struct spi_transfer_desc *xfer)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
bool retval;
|
|
|
|
struct spi_module_desc * desc;
|
|
|
|
int oldlevel;
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (xfer->count == 0)
|
|
|
|
return true; /* No data? No problem. */
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (xfer->count < 0 || xfer->next != NULL || xfer->node == NULL)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Can't pass a busy descriptor, requires a node and negative size
|
|
|
|
* is invalid to pass. */
|
|
|
|
return false;
|
|
|
|
}
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
oldlevel = disable_irq_save();
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
desc = &spi_descs[xfer->node->num];
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (desc->head == NULL)
|
|
|
|
{
|
|
|
|
/* No transfers in progress; start interface. */
|
|
|
|
retval = start_transfer(desc, xfer);
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
if (retval)
|
2008-04-11 08:51:27 +00:00
|
|
|
{
|
2010-05-04 10:07:53 +00:00
|
|
|
/* Start ok: actually put it in the queue. */
|
|
|
|
desc->head = xfer;
|
|
|
|
desc->tail = xfer;
|
|
|
|
xfer->next = xfer; /* First, self-reference terminate */
|
2008-04-11 08:51:27 +00:00
|
|
|
}
|
2010-05-04 10:07:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
xfer->count = -1; /* Signal error */
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Already running: simply add to end and the final INT on the
|
|
|
|
* running transfer will pick it up. */
|
|
|
|
desc->tail->next = xfer; /* Add to tail */
|
|
|
|
desc->tail = xfer; /* New tail */
|
|
|
|
xfer->next = xfer; /* Self-reference terminate */
|
|
|
|
retval = true;
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
2008-04-11 08:51:27 +00:00
|
|
|
|
2010-05-04 10:07:53 +00:00
|
|
|
restore_irq(oldlevel);
|
2008-04-11 08:51:27 +00:00
|
|
|
|
|
|
|
return retval;
|
2007-09-21 15:51:53 +00:00
|
|
|
}
|
2010-05-18 23:50:00 +00:00
|
|
|
|
|
|
|
/* Returns 'true' if the descriptor is not busy */
|
|
|
|
bool spi_transfer_complete(const struct spi_transfer_desc *xfer)
|
|
|
|
{
|
|
|
|
return xfer->next == NULL;
|
|
|
|
}
|