blob: 3eb252c5b5d1d5314c023117c66b115a4bf95b4a [file] [log] [blame]
Stelian Popd1aea1c2008-01-30 21:15:54 +00001/*
2 * Driver for ATMEL DataFlash support
3 * Author : Hamid Ikdoumi (Atmel)
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation; either version 2 of
8 * the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
18 * MA 02111-1307 USA
19 *
20 */
21
Stelian Popd1aea1c2008-01-30 21:15:54 +000022#include <common.h>
Stelian Popd4bfbc52008-03-26 20:52:32 +010023#include <asm/arch/hardware.h>
24#include <asm/arch/gpio.h>
25#include <asm/arch/io.h>
26#include <asm/arch/at91_pio.h>
27#include <asm/arch/at91_spi.h>
Stelian Popd1aea1c2008-01-30 21:15:54 +000028
Stelian Popd1aea1c2008-01-30 21:15:54 +000029#include <dataflash.h>
30
Stelian Popd4bfbc52008-03-26 20:52:32 +010031#define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
32#define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 0: NPCS0%1101 */
33#define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
Stelian Popd1aea1c2008-01-30 21:15:54 +000034
35void AT91F_SpiInit(void)
36{
37 /* Reset the SPI */
Stelian Popd4bfbc52008-03-26 20:52:32 +010038 writel(AT91_SPI_SWRST, AT91_BASE_SPI + AT91_SPI_CR);
Stelian Popd1aea1c2008-01-30 21:15:54 +000039
40 /* Configure SPI in Master Mode with No CS selected !!! */
Stelian Popd4bfbc52008-03-26 20:52:32 +010041 writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS,
42 AT91_BASE_SPI + AT91_SPI_MR);
Stelian Popd1aea1c2008-01-30 21:15:54 +000043
44 /* Configure CS0 */
Stelian Popd4bfbc52008-03-26 20:52:32 +010045 writel(AT91_SPI_NCPHA |
46 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
47 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
48 ((AT91_MASTER_CLOCK / AT91_SPI_CLK) << 8),
49 AT91_BASE_SPI + AT91_SPI_CSR(0));
50
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020051#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1
Stelian Popd4bfbc52008-03-26 20:52:32 +010052 /* Configure CS1 */
53 writel(AT91_SPI_NCPHA |
54 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
55 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
56 ((AT91_MASTER_CLOCK / AT91_SPI_CLK) << 8),
57 AT91_BASE_SPI + AT91_SPI_CSR(1));
58#endif
59
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020060#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3
Stelian Popd4bfbc52008-03-26 20:52:32 +010061 /* Configure CS3 */
62 writel(AT91_SPI_NCPHA |
63 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
64 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
65 ((AT91_MASTER_CLOCK / AT91_SPI_CLK) << 8),
66 AT91_BASE_SPI + AT91_SPI_CSR(3));
67#endif
68
69 /* SPI_Enable */
70 writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
71
72 while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_SPIENS));
73
74 /*
75 * Add tempo to get SPI in a safe state.
76 * Should not be needed for new silicon (Rev B)
77 */
78 udelay(500000);
79 readl(AT91_BASE_SPI + AT91_SPI_SR);
80 readl(AT91_BASE_SPI + AT91_SPI_RDR);
81
Stelian Popd1aea1c2008-01-30 21:15:54 +000082}
83
84void AT91F_SpiEnable(int cs)
85{
Stelian Popd4bfbc52008-03-26 20:52:32 +010086 unsigned long mode;
Jean-Christophe PLAGNIOL-VILLARD4c2b4652008-03-31 21:20:49 +020087
Stelian Popd1aea1c2008-01-30 21:15:54 +000088 switch (cs) {
89 case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
Stelian Popd4bfbc52008-03-26 20:52:32 +010090 mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
91 mode &= 0xFFF0FFFF;
92 writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
93 AT91_BASE_SPI + AT91_SPI_MR);
94 break;
95 case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */
96 mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
97 mode &= 0xFFF0FFFF;
98 writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
99 AT91_BASE_SPI + AT91_SPI_MR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000100 break;
101 case 3:
Stelian Popd4bfbc52008-03-26 20:52:32 +0100102 mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
103 mode &= 0xFFF0FFFF;
104 writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
105 AT91_BASE_SPI + AT91_SPI_MR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000106 break;
107 }
108
109 /* SPI_Enable */
Stelian Popd4bfbc52008-03-26 20:52:32 +0100110 writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000111}
112
Stelian Popd4bfbc52008-03-26 20:52:32 +0100113unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc);
114
Stelian Popd1aea1c2008-01-30 21:15:54 +0000115unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
116{
117 unsigned int timeout;
118
119 pDesc->state = BUSY;
120
Stelian Popd4bfbc52008-03-26 20:52:32 +0100121 writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
122
Stelian Popd1aea1c2008-01-30 21:15:54 +0000123 /* Initialize the Transmit and Receive Pointer */
Stelian Popd4bfbc52008-03-26 20:52:32 +0100124 writel((unsigned int)pDesc->rx_cmd_pt, AT91_BASE_SPI + AT91_SPI_RPR);
125 writel((unsigned int)pDesc->tx_cmd_pt, AT91_BASE_SPI + AT91_SPI_TPR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000126
127 /* Intialize the Transmit and Receive Counters */
Stelian Popd4bfbc52008-03-26 20:52:32 +0100128 writel(pDesc->rx_cmd_size, AT91_BASE_SPI + AT91_SPI_RCR);
129 writel(pDesc->tx_cmd_size, AT91_BASE_SPI + AT91_SPI_TCR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000130
131 if (pDesc->tx_data_size != 0) {
132 /* Initialize the Next Transmit and Next Receive Pointer */
Stelian Popd4bfbc52008-03-26 20:52:32 +0100133 writel((unsigned int)pDesc->rx_data_pt, AT91_BASE_SPI + AT91_SPI_RNPR);
134 writel((unsigned int)pDesc->tx_data_pt, AT91_BASE_SPI + AT91_SPI_TNPR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000135
136 /* Intialize the Next Transmit and Next Receive Counters */
Stelian Popd4bfbc52008-03-26 20:52:32 +0100137 writel(pDesc->rx_data_size, AT91_BASE_SPI + AT91_SPI_RNCR);
138 writel(pDesc->tx_data_size, AT91_BASE_SPI + AT91_SPI_TNCR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000139 }
140
141 /* arm simple, non interrupt dependent timer */
142 reset_timer_masked();
143 timeout = 0;
144
Stelian Popd4bfbc52008-03-26 20:52:32 +0100145 writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN, AT91_BASE_SPI + AT91_SPI_PTCR);
146 while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_RXBUFF) &&
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200147 ((timeout = get_timer_masked()) < CONFIG_SYS_SPI_WRITE_TOUT));
Stelian Popd4bfbc52008-03-26 20:52:32 +0100148 writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000149 pDesc->state = IDLE;
150
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200151 if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) {
Stelian Popd1aea1c2008-01-30 21:15:54 +0000152 printf("Error Timeout\n\r");
153 return DATAFLASH_ERROR;
154 }
155
156 return DATAFLASH_OK;
157}