Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 1 | /* |
| 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 | |
Reinhard Meyer | c163d3d | 2011-08-02 15:09:55 +0000 | [diff] [blame] | 22 | /* |
| 23 | * This driver desperately needs rework: |
| 24 | * |
| 25 | * - use structure SoC access |
| 26 | * - get rid of including asm/arch/at91_spi.h |
| 27 | * - remove asm/arch/at91_spi.h |
| 28 | * - get rid of all CONFIG_ATMEL_LEGACY defines and uses |
| 29 | * |
| 30 | * 02-Aug-2010 Reinhard Meyer <uboot@emk-elektronik.de> |
| 31 | */ |
| 32 | |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 33 | #include <common.h> |
Reinhard Meyer | c163d3d | 2011-08-02 15:09:55 +0000 | [diff] [blame] | 34 | #ifndef CONFIG_ATMEL_LEGACY |
| 35 | # define CONFIG_ATMEL_LEGACY |
Jens Scharsig | a4db1ca | 2010-02-03 22:46:58 +0100 | [diff] [blame] | 36 | #endif |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 37 | #include <spi.h> |
| 38 | #include <malloc.h> |
| 39 | |
| 40 | #include <asm/io.h> |
| 41 | |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 42 | #include <asm/arch/clk.h> |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 43 | #include <asm/arch/hardware.h> |
| 44 | |
| 45 | #include "atmel_spi.h" |
| 46 | |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 47 | #include <asm/arch/gpio.h> |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 48 | #include <asm/arch/at91_pio.h> |
| 49 | #include <asm/arch/at91_spi.h> |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 50 | |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 51 | #include <dataflash.h> |
| 52 | |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 53 | #define AT91_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */ |
Remy Bohmer | 7b2b5d3 | 2009-10-28 22:13:37 +0100 | [diff] [blame] | 54 | #define AT91_SPI_PCS1_DATAFLASH_CARD 0xD /* Chip Select 1: NPCS1%1101 */ |
| 55 | #define AT91_SPI_PCS2_DATAFLASH_CARD 0xB /* Chip Select 2: NPCS2%1011 */ |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 56 | #define AT91_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */ |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 57 | |
| 58 | void AT91F_SpiInit(void) |
| 59 | { |
| 60 | /* Reset the SPI */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 61 | writel(AT91_SPI_SWRST, ATMEL_BASE_SPI0 + AT91_SPI_CR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 62 | |
| 63 | /* Configure SPI in Master Mode with No CS selected !!! */ |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 64 | writel(AT91_SPI_MSTR | AT91_SPI_MODFDIS | AT91_SPI_PCS, |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 65 | ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 66 | |
| 67 | /* Configure CS0 */ |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 68 | writel(AT91_SPI_NCPHA | |
| 69 | (AT91_SPI_DLYBS & DATAFLASH_TCSS) | |
| 70 | (AT91_SPI_DLYBCT & DATAFLASH_TCHS) | |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 71 | ((get_mck_clk_rate() / AT91_SPI_CLK) << 8), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 72 | ATMEL_BASE_SPI0 + AT91_SPI_CSR(0)); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 73 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS1 |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 75 | /* Configure CS1 */ |
| 76 | writel(AT91_SPI_NCPHA | |
| 77 | (AT91_SPI_DLYBS & DATAFLASH_TCSS) | |
| 78 | (AT91_SPI_DLYBCT & DATAFLASH_TCHS) | |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 79 | ((get_mck_clk_rate() / AT91_SPI_CLK) << 8), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 80 | ATMEL_BASE_SPI0 + AT91_SPI_CSR(1)); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 81 | #endif |
Remy Bohmer | 7b2b5d3 | 2009-10-28 22:13:37 +0100 | [diff] [blame] | 82 | #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS2 |
| 83 | /* Configure CS2 */ |
| 84 | writel(AT91_SPI_NCPHA | |
| 85 | (AT91_SPI_DLYBS & DATAFLASH_TCSS) | |
| 86 | (AT91_SPI_DLYBCT & DATAFLASH_TCHS) | |
| 87 | ((get_mck_clk_rate() / AT91_SPI_CLK) << 8), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 88 | ATMEL_BASE_SPI0 + AT91_SPI_CSR(2)); |
Remy Bohmer | 7b2b5d3 | 2009-10-28 22:13:37 +0100 | [diff] [blame] | 89 | #endif |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 90 | #ifdef CONFIG_SYS_DATAFLASH_LOGIC_ADDR_CS3 |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 91 | /* Configure CS3 */ |
| 92 | writel(AT91_SPI_NCPHA | |
| 93 | (AT91_SPI_DLYBS & DATAFLASH_TCSS) | |
| 94 | (AT91_SPI_DLYBCT & DATAFLASH_TCHS) | |
Jean-Christophe PLAGNIOL-VILLARD | 23164f1 | 2009-04-16 21:30:44 +0200 | [diff] [blame] | 95 | ((get_mck_clk_rate() / AT91_SPI_CLK) << 8), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 96 | ATMEL_BASE_SPI0 + AT91_SPI_CSR(3)); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 97 | #endif |
| 98 | |
| 99 | /* SPI_Enable */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 100 | writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 101 | |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 102 | while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_SPIENS)) |
| 103 | ; |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 104 | |
| 105 | /* |
| 106 | * Add tempo to get SPI in a safe state. |
| 107 | * Should not be needed for new silicon (Rev B) |
| 108 | */ |
| 109 | udelay(500000); |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 110 | readl(ATMEL_BASE_SPI0 + AT91_SPI_SR); |
| 111 | readl(ATMEL_BASE_SPI0 + AT91_SPI_RDR); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 112 | |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void AT91F_SpiEnable(int cs) |
| 116 | { |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 117 | unsigned long mode; |
Jean-Christophe PLAGNIOL-VILLARD | 4c2b465 | 2008-03-31 21:20:49 +0200 | [diff] [blame] | 118 | |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 119 | switch (cs) { |
| 120 | case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 121 | mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 122 | mode &= 0xFFF0FFFF; |
| 123 | writel(mode | ((AT91_SPI_PCS0_DATAFLASH_CARD<<16) & AT91_SPI_PCS), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 124 | ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 125 | break; |
| 126 | case 1: /* Configure SPI CS1 for Serial DataFlash AT45DBxx */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 127 | mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 128 | mode &= 0xFFF0FFFF; |
| 129 | writel(mode | ((AT91_SPI_PCS1_DATAFLASH_CARD<<16) & AT91_SPI_PCS), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 130 | ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 131 | break; |
Remy Bohmer | 7b2b5d3 | 2009-10-28 22:13:37 +0100 | [diff] [blame] | 132 | case 2: /* Configure SPI CS2 for Serial DataFlash AT45DBxx */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 133 | mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Remy Bohmer | 7b2b5d3 | 2009-10-28 22:13:37 +0100 | [diff] [blame] | 134 | mode &= 0xFFF0FFFF; |
| 135 | writel(mode | ((AT91_SPI_PCS2_DATAFLASH_CARD<<16) & AT91_SPI_PCS), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 136 | ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Remy Bohmer | 7b2b5d3 | 2009-10-28 22:13:37 +0100 | [diff] [blame] | 137 | break; |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 138 | case 3: |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 139 | mode = readl(ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 140 | mode &= 0xFFF0FFFF; |
| 141 | writel(mode | ((AT91_SPI_PCS3_DATAFLASH_CARD<<16) & AT91_SPI_PCS), |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 142 | ATMEL_BASE_SPI0 + AT91_SPI_MR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 143 | break; |
| 144 | } |
| 145 | |
| 146 | /* SPI_Enable */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 147 | writel(AT91_SPI_SPIEN, ATMEL_BASE_SPI0 + AT91_SPI_CR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 150 | unsigned int AT91F_SpiWrite1(AT91PS_DataflashDesc pDesc); |
| 151 | |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 152 | unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc) |
| 153 | { |
| 154 | unsigned int timeout; |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 155 | unsigned int timebase; |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 156 | |
| 157 | pDesc->state = BUSY; |
| 158 | |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 159 | writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, |
| 160 | ATMEL_BASE_SPI0 + AT91_SPI_PTCR); |
Stelian Pop | d4bfbc5 | 2008-03-26 20:52:32 +0100 | [diff] [blame] | 161 | |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 162 | /* Initialize the Transmit and Receive Pointer */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 163 | writel((unsigned int)pDesc->rx_cmd_pt, |
| 164 | ATMEL_BASE_SPI0 + AT91_SPI_RPR); |
| 165 | writel((unsigned int)pDesc->tx_cmd_pt, |
| 166 | ATMEL_BASE_SPI0 + AT91_SPI_TPR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 167 | |
| 168 | /* Intialize the Transmit and Receive Counters */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 169 | writel(pDesc->rx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_RCR); |
| 170 | writel(pDesc->tx_cmd_size, ATMEL_BASE_SPI0 + AT91_SPI_TCR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 171 | |
| 172 | if (pDesc->tx_data_size != 0) { |
| 173 | /* Initialize the Next Transmit and Next Receive Pointer */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 174 | writel((unsigned int)pDesc->rx_data_pt, |
| 175 | ATMEL_BASE_SPI0 + AT91_SPI_RNPR); |
| 176 | writel((unsigned int)pDesc->tx_data_pt, |
| 177 | ATMEL_BASE_SPI0 + AT91_SPI_TNPR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 178 | |
| 179 | /* Intialize the Next Transmit and Next Receive Counters */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 180 | writel(pDesc->rx_data_size, |
| 181 | ATMEL_BASE_SPI0 + AT91_SPI_RNCR); |
| 182 | writel(pDesc->tx_data_size, |
| 183 | ATMEL_BASE_SPI0 + AT91_SPI_TNCR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | /* arm simple, non interrupt dependent timer */ |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 187 | timebase = get_timer(0); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 188 | timeout = 0; |
| 189 | |
Reinhard Meyer | 324914a | 2010-11-09 17:06:20 +0100 | [diff] [blame] | 190 | writel(AT91_SPI_TXTEN + AT91_SPI_RXTEN, |
| 191 | ATMEL_BASE_SPI0 + AT91_SPI_PTCR); |
| 192 | while (!(readl(ATMEL_BASE_SPI0 + AT91_SPI_SR) & AT91_SPI_RXBUFF) && |
| 193 | ((timeout = get_timer(timebase)) < CONFIG_SYS_SPI_WRITE_TOUT)) |
| 194 | ; |
| 195 | writel(AT91_SPI_TXTDIS + AT91_SPI_RXTDIS, |
| 196 | ATMEL_BASE_SPI0 + AT91_SPI_PTCR); |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 197 | pDesc->state = IDLE; |
| 198 | |
Jean-Christophe PLAGNIOL-VILLARD | 0383694 | 2008-10-16 15:01:15 +0200 | [diff] [blame] | 199 | if (timeout >= CONFIG_SYS_SPI_WRITE_TOUT) { |
Stelian Pop | d1aea1c | 2008-01-30 21:15:54 +0000 | [diff] [blame] | 200 | printf("Error Timeout\n\r"); |
| 201 | return DATAFLASH_ERROR; |
| 202 | } |
| 203 | |
| 204 | return DATAFLASH_OK; |
| 205 | } |