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 | |
| 22 | #include <config.h> |
| 23 | #include <common.h> |
| 24 | #include <asm/hardware.h> |
| 25 | |
| 26 | #ifdef CONFIG_HAS_DATAFLASH |
| 27 | #include <dataflash.h> |
| 28 | |
| 29 | /* Max Value = 10MHz to be compliant to the Continuous Array Read function */ |
| 30 | #define AT91C_SPI_CLK 10000000 |
| 31 | |
| 32 | /* AC Characteristics: DLYBS = tCSS = 250ns min and DLYBCT = tCSH = 250ns */ |
| 33 | #define DATAFLASH_TCSS (0xFA << 16) |
| 34 | #define DATAFLASH_TCHS (0x8 << 24) |
| 35 | |
| 36 | #define AT91C_TIMEOUT_WRDY 200000 |
| 37 | #define AT91C_SPI_PCS0_DATAFLASH_CARD 0xE /* Chip Select 0: NPCS0%1110 */ |
| 38 | #define AT91C_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3: NPCS3%0111 */ |
| 39 | |
| 40 | void AT91F_SpiInit(void) |
| 41 | { |
| 42 | /* Reset the SPI */ |
| 43 | AT91C_BASE_SPI0->SPI_CR = AT91C_SPI_SWRST; |
| 44 | |
| 45 | /* Configure SPI in Master Mode with No CS selected !!! */ |
| 46 | AT91C_BASE_SPI0->SPI_MR = |
| 47 | AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | AT91C_SPI_PCS; |
| 48 | |
| 49 | /* Configure CS0 */ |
| 50 | AT91C_BASE_SPI0->SPI_CSR[0] = |
| 51 | AT91C_SPI_CPOL | |
| 52 | (AT91C_SPI_DLYBS & DATAFLASH_TCSS) | |
| 53 | (AT91C_SPI_DLYBCT & DATAFLASH_TCHS) | |
| 54 | ((AT91C_MASTER_CLOCK / (2*AT91C_SPI_CLK)) << 8); |
| 55 | } |
| 56 | |
| 57 | void AT91F_SpiEnable(int cs) |
| 58 | { |
| 59 | switch (cs) { |
| 60 | case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */ |
| 61 | AT91C_BASE_SPI0->SPI_MR &= 0xFFF0FFFF; |
| 62 | AT91C_BASE_SPI0->SPI_MR |= |
| 63 | ((AT91C_SPI_PCS0_DATAFLASH_CARD<<16) & AT91C_SPI_PCS); |
| 64 | break; |
| 65 | case 3: |
| 66 | AT91C_BASE_SPI0->SPI_MR &= 0xFFF0FFFF; |
| 67 | AT91C_BASE_SPI0->SPI_MR |= |
| 68 | ((AT91C_SPI_PCS3_DATAFLASH_CARD<<16) & AT91C_SPI_PCS); |
| 69 | break; |
| 70 | } |
| 71 | |
| 72 | /* SPI_Enable */ |
| 73 | AT91C_BASE_SPI0->SPI_CR = AT91C_SPI_SPIEN; |
| 74 | } |
| 75 | |
| 76 | unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc) |
| 77 | { |
| 78 | unsigned int timeout; |
| 79 | |
| 80 | pDesc->state = BUSY; |
| 81 | |
| 82 | AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS; |
| 83 | |
| 84 | /* Initialize the Transmit and Receive Pointer */ |
| 85 | AT91C_BASE_SPI0->SPI_RPR = (unsigned int)pDesc->rx_cmd_pt; |
| 86 | AT91C_BASE_SPI0->SPI_TPR = (unsigned int)pDesc->tx_cmd_pt; |
| 87 | |
| 88 | /* Intialize the Transmit and Receive Counters */ |
| 89 | AT91C_BASE_SPI0->SPI_RCR = pDesc->rx_cmd_size; |
| 90 | AT91C_BASE_SPI0->SPI_TCR = pDesc->tx_cmd_size; |
| 91 | |
| 92 | if (pDesc->tx_data_size != 0) { |
| 93 | /* Initialize the Next Transmit and Next Receive Pointer */ |
| 94 | AT91C_BASE_SPI0->SPI_RNPR = (unsigned int)pDesc->rx_data_pt; |
| 95 | AT91C_BASE_SPI0->SPI_TNPR = (unsigned int)pDesc->tx_data_pt; |
| 96 | |
| 97 | /* Intialize the Next Transmit and Next Receive Counters */ |
| 98 | AT91C_BASE_SPI0->SPI_RNCR = pDesc->rx_data_size; |
| 99 | AT91C_BASE_SPI0->SPI_TNCR = pDesc->tx_data_size; |
| 100 | } |
| 101 | |
| 102 | /* arm simple, non interrupt dependent timer */ |
| 103 | reset_timer_masked(); |
| 104 | timeout = 0; |
| 105 | |
| 106 | AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTEN + AT91C_PDC_RXTEN; |
| 107 | while (!(AT91C_BASE_SPI0->SPI_SR & AT91C_SPI_RXBUFF) && |
| 108 | ((timeout = get_timer_masked()) < CFG_SPI_WRITE_TOUT)); |
| 109 | AT91C_BASE_SPI0->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS; |
| 110 | pDesc->state = IDLE; |
| 111 | |
| 112 | if (timeout >= CFG_SPI_WRITE_TOUT) { |
| 113 | printf("Error Timeout\n\r"); |
| 114 | return DATAFLASH_ERROR; |
| 115 | } |
| 116 | |
| 117 | return DATAFLASH_OK; |
| 118 | } |
| 119 | #endif |