/* ----------------------------------------------------------------------------
 *         ATMEL Microcontroller Software Support 
 * ----------------------------------------------------------------------------
 * Copyright (c) 2008, Atmel Corporation
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Atmel's name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL ATMEL 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.
 *
 * Jun-2010 Ported to Linux user space by Micromint <support@micromint.com>
 * ----------------------------------------------------------------------------
 */

//------------------------------------------------------------------------------
//         Headers
//------------------------------------------------------------------------------

#include "spi.h"
#include <board.h>

#if defined(linux)
volatile AT91PS_SPI at91SPI;
volatile AT91PS_PMC at91PMC;
static int mem_fd = 0;

/* SPI MAP 0xfffc8000 - 0xfffcffff allows access to SPI0, SPI1 */
#define MAP_BASE         AT91C_BASE_SPI0
#define MAP_SIZE         32768

#define PERIPH_ADDR(A)   ((unsigned)at91SPI + (unsigned)A \
                         - (unsigned)MAP_BASE)
#define SPI_ADDR(A)      ((AT91PS_SPI)(PERIPH_ADDR(A)))

#else
#define at91PMC          AT91C_BASE_PMC
#define SPI_ADDR(A)      (A)
#endif

#if defined(linux)
static void SPI_Init()
{
    /* Open handle to mem area */
    mem_fd = open("/dev/mem", O_RDWR | O_SYNC);

    /* Map base PMC address to virtual memory */
    at91PMC = mmap(0, 4096, PROT_READ | PROT_WRITE,
                   MAP_SHARED, mem_fd, (unsigned)(AT91C_BASE_AIC) & ~(4096-1));
    if (at91PMC == MAP_FAILED) {
      printf("ERROR: Unable to mmap PMC\n");
    }
    at91PMC = (AT91PS_PMC)((unsigned)at91PMC + (unsigned)AT91C_BASE_PMC
            - (unsigned)AT91C_BASE_AIC);

    /* Map base SPI address to virtual memory */
    at91SPI = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE,
                   MAP_SHARED, mem_fd, (unsigned)(MAP_BASE) & ~(MAP_SIZE-1));
    if (at91SPI == MAP_FAILED) {
        printf("ERROR: Unable to mmap SPI\n");
    }

    /* Close handle */
    close(mem_fd);
}
#endif

//------------------------------------------------------------------------------
//         Exported functions
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Enables a SPI peripheral
/// \param spi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
void SPI_Enable(AT91S_SPI *spi)
{
    SPI_ADDR(spi)->SPI_CR = AT91C_SPI_SPIEN;
}

//------------------------------------------------------------------------------
/// Disables a SPI peripheral.
/// \param spi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
void SPI_Disable(AT91S_SPI *spi)
{
    SPI_ADDR(spi)->SPI_CR = AT91C_SPI_SPIDIS;
}

//------------------------------------------------------------------------------
/// Configures a SPI peripheral as specified. The configuration can be computed
/// using several macros (see "SPI configuration macros") and the constants
/// defined in LibV3 (AT91C_SPI_*).
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param id  Peripheral ID of the SPI.
/// \param configuration  Value of the SPI configuration register.
//------------------------------------------------------------------------------
void SPI_Configure(AT91S_SPI *spi,
                          unsigned int id,
                          unsigned int configuration)
{
#if defined(linux)
    if (at91SPI == NULL) {
        SPI_Init();
    }
    //printf("at91SPI %X\n", (unsigned int)at91SPI);
#endif
    at91PMC->PMC_PCER = 1 << id;
    SPI_ADDR(spi)->SPI_CR = AT91C_SPI_SPIDIS;
    // Execute a software reset of the SPI twice
    SPI_ADDR(spi)->SPI_CR = AT91C_SPI_SWRST;
    SPI_ADDR(spi)->SPI_CR = AT91C_SPI_SWRST;
    SPI_ADDR(spi)->SPI_MR = configuration;
}

//------------------------------------------------------------------------------
/// Configures a chip select of a SPI peripheral. The chip select configuration
/// is computed using the definition provided by the LibV3 (AT91C_SPI_*).
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param npcs  Chip select to configure (1, 2, 3 or 4).
/// \param configuration  Desired chip select configuration.
//------------------------------------------------------------------------------
void SPI_ConfigureNPCS(AT91S_SPI *spi,
                              unsigned int npcs,
                              unsigned int configuration)
{
    SPI_ADDR(spi)->SPI_CSR[npcs] = configuration;
}

//------------------------------------------------------------------------------
/// Sends data through a SPI peripheral. If the SPI is configured to use a fixed
/// peripheral select, the npcs value is meaningless. Otherwise, it identifies
/// the component which shall be addressed.
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param npcs  Chip select of the component to address (1, 2, 3 or 4).
/// \param data  Word of data to send.
//------------------------------------------------------------------------------
void SPI_Write(AT91S_SPI *spi, unsigned int npcs, unsigned short data)
{
    // Discard contents of RDR register
    //volatile unsigned int discard = SPI_ADDR(spi)->SPI_RDR;

    // Send data
    while ((SPI_ADDR(spi)->SPI_SR & AT91C_SPI_TXEMPTY) == 0);
    SPI_ADDR(spi)->SPI_TDR = data | SPI_PCS(npcs);
    while ((SPI_ADDR(spi)->SPI_SR & AT91C_SPI_TDRE) == 0);
}

//------------------------------------------------------------------------------
/// Sends the contents of buffer through a SPI peripheral, using the PDC to
/// take care of the transfer.
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param buffer  Data buffer to send.
/// \param length  Length of the data buffer.
//------------------------------------------------------------------------------
unsigned char SPI_WriteBuffer(AT91S_SPI *spi,
                                     void *buffer,
                                     unsigned int length)
{
    // Check if first bank is free
    if (SPI_ADDR(spi)->SPI_TCR == 0) {

        SPI_ADDR(spi)->SPI_TPR = (unsigned int) buffer;
        SPI_ADDR(spi)->SPI_TCR = length;
        SPI_ADDR(spi)->SPI_PTCR = AT91C_PDC_TXTEN;
        return 1;
    }
    // Check if second bank is free
    else if (SPI_ADDR(spi)->SPI_TNCR == 0) {

        SPI_ADDR(spi)->SPI_TNPR = (unsigned int) buffer;
        SPI_ADDR(spi)->SPI_TNCR = length;
        return 1;
    }
      
    // No free banks
    return 0;
}

//------------------------------------------------------------------------------
/// Returns 1 if there is no pending write operation on the SPI; otherwise
/// returns 0.
/// \param pSpi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
unsigned char SPI_IsFinished(AT91S_SPI *spi)
{
    return ((SPI_ADDR(spi)->SPI_SR & AT91C_SPI_TXEMPTY) != 0);
}

//------------------------------------------------------------------------------
/// Reads and returns the last word of data received by a SPI peripheral. This
/// method must be called after a successful SPI_Write call.
/// \param spi  Pointer to an AT91S_SPI instance.
//------------------------------------------------------------------------------
unsigned short SPI_Read(AT91S_SPI *spi)
{
    while ((SPI_ADDR(spi)->SPI_SR & AT91C_SPI_RDRF) == 0);
    return SPI_ADDR(spi)->SPI_RDR & 0xFFFF;
}

//------------------------------------------------------------------------------
/// Reads data from a SPI peripheral until the provided buffer is filled. This
/// method does NOT need to be called after SPI_Write or SPI_WriteBuffer.
/// \param spi  Pointer to an AT91S_SPI instance.
/// \param buffer  Data buffer to store incoming bytes.
/// \param length  Length in bytes of the data buffer.
//------------------------------------------------------------------------------
unsigned char SPI_ReadBuffer(AT91S_SPI *spi,
                                    void *buffer,
                                    unsigned int length)
{
    // Check if the first bank is free
    if (SPI_ADDR(spi)->SPI_RCR == 0) {

        SPI_ADDR(spi)->SPI_RPR = (unsigned int) buffer;
        SPI_ADDR(spi)->SPI_RCR = length;
        SPI_ADDR(spi)->SPI_PTCR = AT91C_PDC_RXTEN;
        return 1;
    }
    // Check if second bank is free
    else if (SPI_ADDR(spi)->SPI_RNCR == 0) {

        SPI_ADDR(spi)->SPI_RNPR = (unsigned int) buffer;
        SPI_ADDR(spi)->SPI_RNCR = length;
        return 1;
    }

    // No free bank
    return 0;
}

