blob: 4a5c4aac7017cdfa43bb3092561d0290ea4ba1f2 [file] [log] [blame]
Stelian Popfefb6c12008-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 Popfefb6c12008-01-30 21:15:54 +000022#include <common.h>
Jens Scharsig0cf0b932010-02-03 22:46:58 +010023#ifndef CONFIG_AT91_LEGACY
24#define CONFIG_AT91_LEGACY
25#warning Please update to use C structur SoC access !
26#endif
Stelian Pop983c1db2008-03-26 20:52:32 +010027#include <asm/arch/hardware.h>
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020028#include <asm/arch/clk.h>
Stelian Pop983c1db2008-03-26 20:52:32 +010029#include <asm/arch/gpio.h>
30#include <asm/arch/io.h>
31#include <asm/arch/at91_pio.h>
32#include <asm/arch/at91_spi.h>
Stelian Popfefb6c12008-01-30 21:15:54 +000033
Stelian Popfefb6c12008-01-30 21:15:54 +000034#include <dataflash.h>
35
Stelian Pop983c1db2008-03-26 20:52:32 +010036#define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
Remy Bohmer41dfd8a2009-10-28 22:13:37 +010037#define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 1: NPCS1%1101 */
38#define AT91_SPI_PCS2_DATAFLASH_CARD 0xB /* Chip Select 2: NPCS2%1011 */
Stelian Pop983c1db2008-03-26 20:52:32 +010039#define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
Stelian Popfefb6c12008-01-30 21:15:54 +000040
41void AT91F_SpiInit(void)
42{
43 /* Reset the SPI */
Stelian Pop983c1db2008-03-26 20:52:32 +010044 writel(AT91_SPI_SWRST, AT91_BASE_SPI + AT91_SPI_CR);
Stelian Popfefb6c12008-01-30 21:15:54 +000045
46 /* Configure SPI in Master Mode with No CS selected !!! */
Stelian Pop983c1db2008-03-26 20:52:32 +010047 writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS,
48 AT91_BASE_SPI + AT91_SPI_MR);
Stelian Popfefb6c12008-01-30 21:15:54 +000049
50 /* Configure CS0 */
Stelian Pop983c1db2008-03-26 20:52:32 +010051 writel(AT91_SPI_NCPHA |
52 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
53 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020054 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
Stelian Pop983c1db2008-03-26 20:52:32 +010055 AT91_BASE_SPI + AT91_SPI_CSR(0));
56
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020057#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1
Stelian Pop983c1db2008-03-26 20:52:32 +010058 /* Configure CS1 */
59 writel(AT91_SPI_NCPHA |
60 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
61 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020062 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
Stelian Pop983c1db2008-03-26 20:52:32 +010063 AT91_BASE_SPI + AT91_SPI_CSR(1));
64#endif
Remy Bohmer41dfd8a2009-10-28 22:13:37 +010065#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS2
66 /* Configure CS2 */
67 writel(AT91_SPI_NCPHA |
68 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
69 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
70 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
71 AT91_BASE_SPI + AT91_SPI_CSR(2));
72#endif
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020073#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3
Stelian Pop983c1db2008-03-26 20:52:32 +010074 /* Configure CS3 */
75 writel(AT91_SPI_NCPHA |
76 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
77 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
Jean-Christophe PLAGNIOL-VILLARDdc39ae92009-04-16 21:30:44 +020078 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
Stelian Pop983c1db2008-03-26 20:52:32 +010079 AT91_BASE_SPI + AT91_SPI_CSR(3));
80#endif
81
82 /* SPI_Enable */
83 writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
84
85 while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_SPIENS));
86
87 /*
88 * Add tempo to get SPI in a safe state.
89 * Should not be needed for new silicon (Rev B)
90 */
91 udelay(500000);
92 readl(AT91_BASE_SPI + AT91_SPI_SR);
93 readl(AT91_BASE_SPI + AT91_SPI_RDR);
94
Stelian Popfefb6c12008-01-30 21:15:54 +000095}
96
97void AT91F_SpiEnable(int cs)
98{
Stelian Pop983c1db2008-03-26 20:52:32 +010099 unsigned long mode;
Jean-Christophe PLAGNIOL-VILLARD1762f132008-03-31 21:20:49 +0200100
Stelian Popfefb6c12008-01-30 21:15:54 +0000101 switch (cs) {
102 case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
Stelian Pop983c1db2008-03-26 20:52:32 +0100103 mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
104 mode &= 0xFFF0FFFF;
105 writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
106 AT91_BASE_SPI + AT91_SPI_MR);
107 break;
108 case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */
109 mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
110 mode &= 0xFFF0FFFF;
111 writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
112 AT91_BASE_SPI + AT91_SPI_MR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000113 break;
Remy Bohmer41dfd8a2009-10-28 22:13:37 +0100114 case 2: /* Configure SPI CS2 for Serial DataFlash AT45DBxx */
115 mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
116 mode &= 0xFFF0FFFF;
117 writel(mode | ((AT91_SPI_PCS2_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
118 AT91_BASE_SPI + AT91_SPI_MR);
119 break;
Stelian Popfefb6c12008-01-30 21:15:54 +0000120 case 3:
Stelian Pop983c1db2008-03-26 20:52:32 +0100121 mode = readl(AT91_BASE_SPI + AT91_SPI_MR);
122 mode &= 0xFFF0FFFF;
123 writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
124 AT91_BASE_SPI + AT91_SPI_MR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000125 break;
126 }
127
128 /* SPI_Enable */
Stelian Pop983c1db2008-03-26 20:52:32 +0100129 writel(AT91_SPI_SPIEN, AT91_BASE_SPI + AT91_SPI_CR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000130}
131
Stelian Pop983c1db2008-03-26 20:52:32 +0100132unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc);
133
Stelian Popfefb6c12008-01-30 21:15:54 +0000134unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
135{
136 unsigned int timeout;
137
138 pDesc->state = BUSY;
139
Stelian Pop983c1db2008-03-26 20:52:32 +0100140 writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
141
Stelian Popfefb6c12008-01-30 21:15:54 +0000142 /* Initialize the Transmit and Receive Pointer */
Stelian Pop983c1db2008-03-26 20:52:32 +0100143 writel((unsigned int)pDesc->rx_cmd_pt, AT91_BASE_SPI + AT91_SPI_RPR);
144 writel((unsigned int)pDesc->tx_cmd_pt, AT91_BASE_SPI + AT91_SPI_TPR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000145
146 /* Intialize the Transmit and Receive Counters */
Stelian Pop983c1db2008-03-26 20:52:32 +0100147 writel(pDesc->rx_cmd_size, AT91_BASE_SPI + AT91_SPI_RCR);
148 writel(pDesc->tx_cmd_size, AT91_BASE_SPI + AT91_SPI_TCR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000149
150 if (pDesc->tx_data_size != 0) {
151 /* Initialize the Next Transmit and Next Receive Pointer */
Stelian Pop983c1db2008-03-26 20:52:32 +0100152 writel((unsigned int)pDesc->rx_data_pt, AT91_BASE_SPI + AT91_SPI_RNPR);
153 writel((unsigned int)pDesc->tx_data_pt, AT91_BASE_SPI + AT91_SPI_TNPR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000154
155 /* Intialize the Next Transmit and Next Receive Counters */
Stelian Pop983c1db2008-03-26 20:52:32 +0100156 writel(pDesc->rx_data_size, AT91_BASE_SPI + AT91_SPI_RNCR);
157 writel(pDesc->tx_data_size, AT91_BASE_SPI + AT91_SPI_TNCR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000158 }
159
160 /* arm simple, non interrupt dependent timer */
161 reset_timer_masked();
162 timeout = 0;
163
Stelian Pop983c1db2008-03-26 20:52:32 +0100164 writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN, AT91_BASE_SPI + AT91_SPI_PTCR);
165 while (!(readl(AT91_BASE_SPI + AT91_SPI_SR) & AT91_SPI_RXBUFF) &&
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200166 ((timeout = get_timer_masked()) < CONFIG_SYS_SPI_WRITE_TOUT));
Stelian Pop983c1db2008-03-26 20:52:32 +0100167 writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, AT91_BASE_SPI + AT91_SPI_PTCR);
Stelian Popfefb6c12008-01-30 21:15:54 +0000168 pDesc->state = IDLE;
169
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200170 if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) {
Stelian Popfefb6c12008-01-30 21:15:54 +0000171 printf("Error Timeout\n\r");
172 return DATAFLASH_ERROR;
173 }
174
175 return DATAFLASH_OK;
176}