2008-01-30 21:15:54 +00:00
|
|
|
/*
|
|
|
|
* Driver for ATMEL DataFlash support
|
|
|
|
* Author : Hamid Ikdoumi (Atmel)
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
|
|
|
|
* MA 02111-1307 USA
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2008-03-26 19:52:32 +00:00
|
|
|
#include <asm/arch/hardware.h>
|
2009-04-16 19:30:44 +00:00
|
|
|
#include <asm/arch/clk.h>
|
2008-03-26 19:52:32 +00:00
|
|
|
#include <asm/arch/gpio.h>
|
|
|
|
#include <asm/arch/io.h>
|
|
|
|
#include <asm/arch/at91_pio.h>
|
|
|
|
#include <asm/arch/at91_spi.h>
|
2008-01-30 21:15:54 +00:00
|
|
|
|
|
|
|
#include <dataflash.h>
|
|
|
|
|
2008-03-26 19:52:32 +00:00
|
|
|
#define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
|
|
|
|
#define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 0: NPCS0%1101 */
|
|
|
|
#define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
|
2008-01-30 21:15:54 +00:00
|
|
|
|
|
|
|
void AT91F_SpiInit(void)
|
|
|
|
{
|
|
|
|
/* Reset the SPI */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(AT91_SPI_SWRST, AT91_BASE_SPI + AT91_SPI_CR);
|
2008-01-30 21:15:54 +00:00
|
|
|
|
|
|
|
/* Configure SPI in Master Mode with No CS selected !!! */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS,
|
|
|
|
AT91_BASE_SPI + AT91_SPI_MR);
|
2008-01-30 21:15:54 +00:00
|
|
|
|
|
|
|
/* Configure CS0 */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(AT91_SPI_NCPHA |
|
|
|
|
(AT91_SPI_DLYBS & DATAFLASH_TCSS) |
|
|
|
|
(AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
|
2009-04-16 19:30:44 +00:00
|
|
|
((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
|
2008-03-26 19:52:32 +00:00
|
|
|
AT91_BASE_SPI + AT91_SPI_CSR(0));
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1
|
2008-03-26 19:52:32 +00:00
|
|
|
/* Configure CS1 */
|
|
|
|
writel(AT91_SPI_NCPHA |
|
|
|
|
(AT91_SPI_DLYBS & DATAFLASH_TCSS) |
|
|
|
|
(AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
|
2009-04-16 19:30:44 +00:00
|
|
|
((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
|
2008-03-26 19:52:32 +00:00
|
|
|
AT91_BASE_SPI + AT91_SPI_CSR(1));
|
|
|
|
#endif
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3
|
2008-03-26 19:52:32 +00:00
|
|
|
/* Configure CS3 */
|
|
|
|
writel(AT91_SPI_NCPHA |
|
|
|
|
(AT91_SPI_DLYBS & DATAFLASH_TCSS) |
|
|
|
|
(AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
|
2009-04-16 19:30:44 +00:00
|
|
|
((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
|
2008-03-26 19:52:32 +00:00
|
|
|
AT91_BASE_SPI + AT91_SPI_CSR(3));
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* SPI_Enable */
|
|
|
|
writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
|
|
|
|
|
|
|
|
while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_SPIENS));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Add tempo to get SPI in a safe state.
|
|
|
|
* Should not be needed for new silicon (Rev B)
|
|
|
|
*/
|
|
|
|
udelay(500000);
|
|
|
|
readl(AT91_BASE_SPI + AT91_SPI_SR);
|
|
|
|
readl(AT91_BASE_SPI + AT91_SPI_RDR);
|
|
|
|
|
2008-01-30 21:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void AT91F_SpiEnable(int cs)
|
|
|
|
{
|
2008-03-26 19:52:32 +00:00
|
|
|
unsigned long mode;
|
2008-03-31 19:20:49 +00:00
|
|
|
|
2008-01-30 21:15:54 +00:00
|
|
|
switch (cs) {
|
|
|
|
case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
|
2008-03-26 19:52:32 +00:00
|
|
|
mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
|
|
|
|
mode &= 0xFFF0FFFF;
|
|
|
|
writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
|
|
|
|
AT91_BASE_SPI + AT91_SPI_MR);
|
|
|
|
break;
|
|
|
|
case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */
|
|
|
|
mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
|
|
|
|
mode &= 0xFFF0FFFF;
|
|
|
|
writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
|
|
|
|
AT91_BASE_SPI + AT91_SPI_MR);
|
2008-01-30 21:15:54 +00:00
|
|
|
break;
|
|
|
|
case 3:
|
2008-03-26 19:52:32 +00:00
|
|
|
mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
|
|
|
|
mode &= 0xFFF0FFFF;
|
|
|
|
writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
|
|
|
|
AT91_BASE_SPI + AT91_SPI_MR);
|
2008-01-30 21:15:54 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* SPI_Enable */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
|
2008-01-30 21:15:54 +00:00
|
|
|
}
|
|
|
|
|
2008-03-26 19:52:32 +00:00
|
|
|
unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc);
|
|
|
|
|
2008-01-30 21:15:54 +00:00
|
|
|
unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
|
|
|
|
{
|
|
|
|
unsigned int timeout;
|
|
|
|
|
|
|
|
pDesc->state = BUSY;
|
|
|
|
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
|
|
|
|
|
2008-01-30 21:15:54 +00:00
|
|
|
/* Initialize the Transmit and Receive Pointer */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel((unsigned int)pDesc->rx_cmd_pt, AT91_BASE_SPI + AT91_SPI_RPR);
|
|
|
|
writel((unsigned int)pDesc->tx_cmd_pt, AT91_BASE_SPI + AT91_SPI_TPR);
|
2008-01-30 21:15:54 +00:00
|
|
|
|
|
|
|
/* Intialize the Transmit and Receive Counters */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(pDesc->rx_cmd_size, AT91_BASE_SPI + AT91_SPI_RCR);
|
|
|
|
writel(pDesc->tx_cmd_size, AT91_BASE_SPI + AT91_SPI_TCR);
|
2008-01-30 21:15:54 +00:00
|
|
|
|
|
|
|
if (pDesc->tx_data_size != 0) {
|
|
|
|
/* Initialize the Next Transmit and Next Receive Pointer */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel((unsigned int)pDesc->rx_data_pt, AT91_BASE_SPI + AT91_SPI_RNPR);
|
|
|
|
writel((unsigned int)pDesc->tx_data_pt, AT91_BASE_SPI + AT91_SPI_TNPR);
|
2008-01-30 21:15:54 +00:00
|
|
|
|
|
|
|
/* Intialize the Next Transmit and Next Receive Counters */
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(pDesc->rx_data_size, AT91_BASE_SPI + AT91_SPI_RNCR);
|
|
|
|
writel(pDesc->tx_data_size, AT91_BASE_SPI + AT91_SPI_TNCR);
|
2008-01-30 21:15:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* arm simple, non interrupt dependent timer */
|
|
|
|
reset_timer_masked();
|
|
|
|
timeout = 0;
|
|
|
|
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN, AT91_BASE_SPI + AT91_SPI_PTCR);
|
|
|
|
while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_RXBUFF) &&
|
2008-10-16 13:01:15 +00:00
|
|
|
((timeout = get_timer_masked()) < CONFIG_SYS_SPI_WRITE_TOUT));
|
2008-03-26 19:52:32 +00:00
|
|
|
writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
|
2008-01-30 21:15:54 +00:00
|
|
|
pDesc->state = IDLE;
|
|
|
|
|
2008-10-16 13:01:15 +00:00
|
|
|
if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) {
|
2008-01-30 21:15:54 +00:00
|
|
|
printf("Error Timeout\n\r");
|
|
|
|
return DATAFLASH_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return DATAFLASH_OK;
|
|
|
|
}
|