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