blob: 9c991e8cbca123a09b033c0f4d5a9ff9287d75c7 [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>
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010023#ifndef CONFIG_AT91_LEGACY
Reinhard Meyer324914a2010-11-09 17:06:20 +010024# define CONFIG_ATMEL_LEGACY
25# warning Please update to use C structure SoC access !
Jens Scharsiga4db1ca2010-02-03 22:46:58 +010026#endif
Reinhard Meyer324914a2010-11-09 17:06:20 +010027#include <common.h>
28#include <spi.h>
29#include <malloc.h>
30
31#include <asm/io.h>
32
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020033#include <asm/arch/clk.h>
Reinhard Meyer324914a2010-11-09 17:06:20 +010034#include <asm/arch/hardware.h>
35
36#include "atmel_spi.h"
37
Stelian Popd4bfbc52008-03-26 20:52:32 +010038#include <asm/arch/gpio.h>
Stelian Popd4bfbc52008-03-26 20:52:32 +010039#include <asm/arch/at91_pio.h>
40#include <asm/arch/at91_spi.h>
Stelian Popd1aea1c2008-01-30 21:15:54 +000041
Stelian Popd1aea1c2008-01-30 21:15:54 +000042#include <dataflash.h>
43
Stelian Popd4bfbc52008-03-26 20:52:32 +010044#define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */
Remy Bohmer7b2b5d32009-10-28 22:13:37 +010045#define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 1: NPCS1%1101 */
46#define AT91_SPI_PCS2_DATAFLASH_CARD 0xB /* Chip Select 2: NPCS2%1011 */
Stelian Popd4bfbc52008-03-26 20:52:32 +010047#define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */
Stelian Popd1aea1c2008-01-30 21:15:54 +000048
49void AT91F_SpiInit(void)
50{
51 /* Reset the SPI */
Reinhard Meyer324914a2010-11-09 17:06:20 +010052 writel(AT91_SPI_SWRST, ATMEL_BASE_SPI0 + AT91_SPI_CR);
Stelian Popd1aea1c2008-01-30 21:15:54 +000053
54 /* Configure SPI in Master Mode with No CS selected !!! */
Stelian Popd4bfbc52008-03-26 20:52:32 +010055 writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS,
Reinhard Meyer324914a2010-11-09 17:06:20 +010056 ATMEL_BASE_SPI0 + AT91_SPI_MR);
Stelian Popd1aea1c2008-01-30 21:15:54 +000057
58 /* Configure CS0 */
Stelian Popd4bfbc52008-03-26 20:52:32 +010059 writel(AT91_SPI_NCPHA |
60 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
61 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020062 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
Reinhard Meyer324914a2010-11-09 17:06:20 +010063 ATMEL_BASE_SPI0 + AT91_SPI_CSR(0));
Stelian Popd4bfbc52008-03-26 20:52:32 +010064
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020065#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1
Stelian Popd4bfbc52008-03-26 20:52:32 +010066 /* Configure CS1 */
67 writel(AT91_SPI_NCPHA |
68 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
69 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020070 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
Reinhard Meyer324914a2010-11-09 17:06:20 +010071 ATMEL_BASE_SPI0 + AT91_SPI_CSR(1));
Stelian Popd4bfbc52008-03-26 20:52:32 +010072#endif
Remy Bohmer7b2b5d32009-10-28 22:13:37 +010073#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS2
74 /* Configure CS2 */
75 writel(AT91_SPI_NCPHA |
76 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
77 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
78 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
Reinhard Meyer324914a2010-11-09 17:06:20 +010079 ATMEL_BASE_SPI0 + AT91_SPI_CSR(2));
Remy Bohmer7b2b5d32009-10-28 22:13:37 +010080#endif
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020081#ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3
Stelian Popd4bfbc52008-03-26 20:52:32 +010082 /* Configure CS3 */
83 writel(AT91_SPI_NCPHA |
84 (AT91_SPI_DLYBS & DATAFLASH_TCSS) |
85 (AT91_SPI_DLYBCT & DATAFLASH_TCHS) |
Jean-Christophe PLAGNIOL-VILLARD23164f12009-04-16 21:30:44 +020086 ((get_mck_clk_rate() / AT91_SPI_CLK) << 8),
Reinhard Meyer324914a2010-11-09 17:06:20 +010087 ATMEL_BASE_SPI0 + AT91_SPI_CSR(3));
Stelian Popd4bfbc52008-03-26 20:52:32 +010088#endif
89
90 /* SPI_Enable */
Reinhard Meyer324914a2010-11-09 17:06:20 +010091 writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR);
Stelian Popd4bfbc52008-03-26 20:52:32 +010092
Reinhard Meyer324914a2010-11-09 17:06:20 +010093 while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_SPIENS))
94 ;
Stelian Popd4bfbc52008-03-26 20:52:32 +010095
96 /*
97 * Add tempo to get SPI in a safe state.
98 * Should not be needed for new silicon (Rev B)
99 */
100 udelay(500000);
Reinhard Meyer324914a2010-11-09 17:06:20 +0100101 readl(ATMEL_BASE_SPI0 + AT91_SPI_SR);
102 readl(ATMEL_BASE_SPI0 + AT91_SPI_RDR);
Stelian Popd4bfbc52008-03-26 20:52:32 +0100103
Stelian Popd1aea1c2008-01-30 21:15:54 +0000104}
105
106void AT91F_SpiEnable(int cs)
107{
Stelian Popd4bfbc52008-03-26 20:52:32 +0100108 unsigned long mode;
Jean-Christophe PLAGNIOL-VILLARD4c2b4652008-03-31 21:20:49 +0200109
Stelian Popd1aea1c2008-01-30 21:15:54 +0000110 switch (cs) {
111 case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100112 mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
Stelian Popd4bfbc52008-03-26 20:52:32 +0100113 mode &= 0xFFF0FFFF;
114 writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
Reinhard Meyer324914a2010-11-09 17:06:20 +0100115 ATMEL_BASE_SPI0 + AT91_SPI_MR);
Stelian Popd4bfbc52008-03-26 20:52:32 +0100116 break;
117 case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100118 mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
Stelian Popd4bfbc52008-03-26 20:52:32 +0100119 mode &= 0xFFF0FFFF;
120 writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
Reinhard Meyer324914a2010-11-09 17:06:20 +0100121 ATMEL_BASE_SPI0 + AT91_SPI_MR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000122 break;
Remy Bohmer7b2b5d32009-10-28 22:13:37 +0100123 case 2: /* Configure SPI CS2 for Serial DataFlash AT45DBxx */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100124 mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
Remy Bohmer7b2b5d32009-10-28 22:13:37 +0100125 mode &= 0xFFF0FFFF;
126 writel(mode | ((AT91_SPI_PCS2_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
Reinhard Meyer324914a2010-11-09 17:06:20 +0100127 ATMEL_BASE_SPI0 + AT91_SPI_MR);
Remy Bohmer7b2b5d32009-10-28 22:13:37 +0100128 break;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000129 case 3:
Reinhard Meyer324914a2010-11-09 17:06:20 +0100130 mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR);
Stelian Popd4bfbc52008-03-26 20:52:32 +0100131 mode &= 0xFFF0FFFF;
132 writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS),
Reinhard Meyer324914a2010-11-09 17:06:20 +0100133 ATMEL_BASE_SPI0 + AT91_SPI_MR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000134 break;
135 }
136
137 /* SPI_Enable */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100138 writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000139}
140
Stelian Popd4bfbc52008-03-26 20:52:32 +0100141unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc);
142
Stelian Popd1aea1c2008-01-30 21:15:54 +0000143unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc)
144{
145 unsigned int timeout;
Reinhard Meyer324914a2010-11-09 17:06:20 +0100146 unsigned int timebase;
Stelian Popd1aea1c2008-01-30 21:15:54 +0000147
148 pDesc->state = BUSY;
149
Reinhard Meyer324914a2010-11-09 17:06:20 +0100150 writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS,
151 ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
Stelian Popd4bfbc52008-03-26 20:52:32 +0100152
Stelian Popd1aea1c2008-01-30 21:15:54 +0000153 /* Initialize the Transmit and Receive Pointer */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100154 writel((unsigned int)pDesc->rx_cmd_pt,
155 ATMEL_BASE_SPI0 + AT91_SPI_RPR);
156 writel((unsigned int)pDesc->tx_cmd_pt,
157 ATMEL_BASE_SPI0 + AT91_SPI_TPR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000158
159 /* Intialize the Transmit and Receive Counters */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100160 writel(pDesc->rx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_RCR);
161 writel(pDesc->tx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_TCR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000162
163 if (pDesc->tx_data_size != 0) {
164 /* Initialize the Next Transmit and Next Receive Pointer */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100165 writel((unsigned int)pDesc->rx_data_pt,
166 ATMEL_BASE_SPI0 + AT91_SPI_RNPR);
167 writel((unsigned int)pDesc->tx_data_pt,
168 ATMEL_BASE_SPI0 + AT91_SPI_TNPR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000169
170 /* Intialize the Next Transmit and Next Receive Counters */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100171 writel(pDesc->rx_data_size,
172 ATMEL_BASE_SPI0 + AT91_SPI_RNCR);
173 writel(pDesc->tx_data_size,
174 ATMEL_BASE_SPI0 + AT91_SPI_TNCR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000175 }
176
177 /* arm simple, non interrupt dependent timer */
Reinhard Meyer324914a2010-11-09 17:06:20 +0100178 timebase = get_timer(0);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000179 timeout = 0;
180
Reinhard Meyer324914a2010-11-09 17:06:20 +0100181 writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN,
182 ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
183 while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_RXBUFF) &&
184 ((timeout = get_timer(timebase)) < CONFIG_SYS_SPI_WRITE_TOUT))
185 ;
186 writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS,
187 ATMEL_BASE_SPI0 + AT91_SPI_PTCR);
Stelian Popd1aea1c2008-01-30 21:15:54 +0000188 pDesc->state = IDLE;
189
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200190 if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) {
Stelian Popd1aea1c2008-01-30 21:15:54 +0000191 printf("Error Timeout\n\r");
192 return DATAFLASH_ERROR;
193 }
194
195 return DATAFLASH_OK;
196}