Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 1 | /* Driver for ATMEL DataFlash support |
| 2 | * Author : Hamid Ikdoumi (Atmel) |
| 3 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 4 | * SPDX-License-Identifier: GPL-2.0+ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <config.h> |
| 8 | #include <common.h> |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 9 | #include <dataflash.h> |
| 10 | |
Peter Pearse | e54b970 | 2007-08-14 15:40:00 +0100 | [diff] [blame] | 11 | /* |
| 12 | * spi.c API |
| 13 | */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 14 | extern unsigned int AT91F_SpiWrite(AT91PS_DataflashDesc pDesc); |
| 15 | extern void AT91F_SpiEnable(int cs); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 16 | |
| 17 | #define AT91C_TIMEOUT_WRDY 200000 |
| 18 | |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 19 | /*----------------------------------------------------------------------*/ |
| 20 | /* \fn AT91F_DataFlashSendCommand */ |
| 21 | /* \brief Generic function to send a command to the dataflash */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 22 | /*----------------------------------------------------------------------*/ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 23 | AT91S_DataFlashStatus AT91F_DataFlashSendCommand(AT91PS_DataFlash pDataFlash, |
| 24 | unsigned char OpCode, |
| 25 | unsigned int CmdSize, |
| 26 | unsigned int DataflashAddress) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 27 | { |
| 28 | unsigned int adr; |
| 29 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 30 | if ((pDataFlash->pDataFlashDesc->state) != IDLE) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 31 | return DATAFLASH_BUSY; |
| 32 | |
| 33 | /* process the address to obtain page address and byte address */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 34 | adr = ((DataflashAddress / (pDataFlash->pDevice->pages_size)) << |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 35 | pDataFlash->pDevice->page_offset) + |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 36 | (DataflashAddress % (pDataFlash->pDevice->pages_size)); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 37 | |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 38 | /* fill the command buffer */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 39 | pDataFlash->pDataFlashDesc->command[0] = OpCode; |
| 40 | if (pDataFlash->pDevice->pages_number >= 16384) { |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 41 | pDataFlash->pDataFlashDesc->command[1] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 42 | (unsigned char)((adr & 0x0F000000) >> 24); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 43 | pDataFlash->pDataFlashDesc->command[2] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 44 | (unsigned char)((adr & 0x00FF0000) >> 16); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 45 | pDataFlash->pDataFlashDesc->command[3] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 46 | (unsigned char)((adr & 0x0000FF00) >> 8); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 47 | pDataFlash->pDataFlashDesc->command[4] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 48 | (unsigned char)(adr & 0x000000FF); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 49 | } else { |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 50 | pDataFlash->pDataFlashDesc->command[1] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 51 | (unsigned char)((adr & 0x00FF0000) >> 16); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 52 | pDataFlash->pDataFlashDesc->command[2] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 53 | (unsigned char)((adr & 0x0000FF00) >> 8); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 54 | pDataFlash->pDataFlashDesc->command[3] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 55 | (unsigned char)(adr & 0x000000FF); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 56 | pDataFlash->pDataFlashDesc->command[4] = 0; |
| 57 | } |
| 58 | pDataFlash->pDataFlashDesc->command[5] = 0; |
| 59 | pDataFlash->pDataFlashDesc->command[6] = 0; |
| 60 | pDataFlash->pDataFlashDesc->command[7] = 0; |
| 61 | |
| 62 | /* Initialize the SpiData structure for the spi write fuction */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 63 | pDataFlash->pDataFlashDesc->tx_cmd_pt = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 64 | pDataFlash->pDataFlashDesc->command; |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 65 | pDataFlash->pDataFlashDesc->tx_cmd_size = CmdSize; |
| 66 | pDataFlash->pDataFlashDesc->rx_cmd_pt = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 67 | pDataFlash->pDataFlashDesc->command; |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 68 | pDataFlash->pDataFlashDesc->rx_cmd_size = CmdSize; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 69 | |
| 70 | /* send the command and read the data */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 71 | return AT91F_SpiWrite(pDataFlash->pDataFlashDesc); |
| 72 | } |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 73 | |
| 74 | /*----------------------------------------------------------------------*/ |
| 75 | /* \fn AT91F_DataFlashGetStatus */ |
| 76 | /* \brief Read the status register of the dataflash */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 77 | /*----------------------------------------------------------------------*/ |
| 78 | AT91S_DataFlashStatus AT91F_DataFlashGetStatus(AT91PS_DataflashDesc pDesc) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 79 | { |
| 80 | AT91S_DataFlashStatus status; |
| 81 | |
| 82 | /* if a transfert is in progress ==> return 0 */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 83 | if ((pDesc->state) != IDLE) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 84 | return DATAFLASH_BUSY; |
| 85 | |
| 86 | /* first send the read status command (D7H) */ |
| 87 | pDesc->command[0] = DB_STATUS; |
| 88 | pDesc->command[1] = 0; |
| 89 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 90 | pDesc->DataFlash_state = GET_STATUS; |
| 91 | pDesc->tx_data_size = 0; /* Transmit the command */ |
| 92 | /* and receive response */ |
| 93 | pDesc->tx_cmd_pt = pDesc->command; |
| 94 | pDesc->rx_cmd_pt = pDesc->command; |
| 95 | pDesc->rx_cmd_size = 2; |
| 96 | pDesc->tx_cmd_size = 2; |
| 97 | status = AT91F_SpiWrite(pDesc); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 98 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 99 | pDesc->DataFlash_state = *((unsigned char *)(pDesc->rx_cmd_pt) + 1); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 100 | |
| 101 | return status; |
| 102 | } |
| 103 | |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 104 | /*----------------------------------------------------------------------*/ |
| 105 | /* \fn AT91F_DataFlashWaitReady */ |
| 106 | /* \brief wait for dataflash ready (bit7 of the status register == 1) */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 107 | /*----------------------------------------------------------------------*/ |
| 108 | AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 109 | pDataFlashDesc, |
| 110 | unsigned int timeout) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 111 | { |
| 112 | pDataFlashDesc->DataFlash_state = IDLE; |
| 113 | |
| 114 | do { |
| 115 | AT91F_DataFlashGetStatus(pDataFlashDesc); |
| 116 | timeout--; |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 117 | } while (((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) && |
| 118 | (timeout > 0)); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 119 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 120 | if ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 121 | return DATAFLASH_ERROR; |
| 122 | |
| 123 | return DATAFLASH_OK; |
| 124 | } |
| 125 | |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 126 | /*--------------------------------------------------------------------------*/ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 127 | /* Function Name : AT91F_DataFlashContinuousRead */ |
| 128 | /* Object : Continuous stream Read */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 129 | /* Input Parameters : DataFlash Service */ |
| 130 | /* : <src> = dataflash address */ |
| 131 | /* : <*dataBuffer> = data buffer pointer */ |
| 132 | /* : <sizeToRead> = data buffer size */ |
| 133 | /* Return value : State of the dataflash */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 134 | /*--------------------------------------------------------------------------*/ |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 135 | AT91S_DataFlashStatus AT91F_DataFlashContinuousRead( |
| 136 | AT91PS_DataFlash pDataFlash, |
| 137 | int src, |
| 138 | unsigned char *dataBuffer, |
| 139 | int sizeToRead) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 140 | { |
| 141 | AT91S_DataFlashStatus status; |
| 142 | /* Test the size to read in the device */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 143 | if ((src + sizeToRead) > |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 144 | (pDataFlash->pDevice->pages_size * |
| 145 | (pDataFlash->pDevice->pages_number))) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 146 | return DATAFLASH_MEMORY_OVERFLOW; |
| 147 | |
| 148 | pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer; |
| 149 | pDataFlash->pDataFlashDesc->rx_data_size = sizeToRead; |
| 150 | pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer; |
| 151 | pDataFlash->pDataFlashDesc->tx_data_size = sizeToRead; |
| 152 | |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 153 | status = AT91F_DataFlashSendCommand( |
| 154 | pDataFlash, DB_CONTINUOUS_ARRAY_READ, 8, src); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 155 | /* Send the command to the dataflash */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 156 | return (status); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 157 | } |
| 158 | |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 159 | /*---------------------------------------------------------------------------*/ |
| 160 | /* Function Name : AT91F_DataFlashPagePgmBuf */ |
| 161 | /* Object : Main memory page program thru buffer 1 or buffer 2 */ |
| 162 | /* Input Parameters : DataFlash Service */ |
| 163 | /* : <*src> = Source buffer */ |
| 164 | /* : <dest> = dataflash destination address */ |
| 165 | /* : <SizeToWrite> = data buffer size */ |
| 166 | /* Return value : State of the dataflash */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 167 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 168 | AT91S_DataFlashStatus AT91F_DataFlashPagePgmBuf(AT91PS_DataFlash pDataFlash, |
| 169 | unsigned char *src, |
| 170 | unsigned int dest, |
| 171 | unsigned int SizeToWrite) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 172 | { |
| 173 | int cmdsize; |
| 174 | pDataFlash->pDataFlashDesc->tx_data_pt = src; |
| 175 | pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite; |
| 176 | pDataFlash->pDataFlashDesc->rx_data_pt = src; |
| 177 | pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite; |
| 178 | |
| 179 | cmdsize = 4; |
| 180 | /* Send the command to the dataflash */ |
| 181 | if (pDataFlash->pDevice->pages_number >= 16384) |
| 182 | cmdsize = 5; |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 183 | return (AT91F_DataFlashSendCommand( |
| 184 | pDataFlash, DB_PAGE_PGM_BUF1, cmdsize, dest)); |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 185 | } |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 186 | |
| 187 | /*---------------------------------------------------------------------------*/ |
| 188 | /* Function Name : AT91F_MainMemoryToBufferTransfert */ |
| 189 | /* Object : Read a page in the SRAM Buffer 1 or 2 */ |
| 190 | /* Input Parameters : DataFlash Service */ |
| 191 | /* : Page concerned */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 192 | /* : */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 193 | /* Return value : State of the dataflash */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 194 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 195 | AT91S_DataFlashStatus AT91F_MainMemoryToBufferTransfert( |
| 196 | AT91PS_DataFlash |
| 197 | pDataFlash, |
| 198 | unsigned char |
| 199 | BufferCommand, |
| 200 | unsigned int page) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 201 | { |
| 202 | int cmdsize; |
| 203 | /* Test if the buffer command is legal */ |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 204 | if ((BufferCommand != DB_PAGE_2_BUF1_TRF) && |
| 205 | (BufferCommand != DB_PAGE_2_BUF2_TRF)) { |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 206 | return DATAFLASH_BAD_COMMAND; |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 207 | } |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 208 | |
| 209 | /* no data to transmit or receive */ |
| 210 | pDataFlash->pDataFlashDesc->tx_data_size = 0; |
| 211 | cmdsize = 4; |
| 212 | if (pDataFlash->pDevice->pages_number >= 16384) |
| 213 | cmdsize = 5; |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 214 | return (AT91F_DataFlashSendCommand( |
| 215 | pDataFlash, BufferCommand, cmdsize, |
| 216 | page * pDataFlash->pDevice->pages_size)); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 217 | } |
| 218 | |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 219 | /*-------------------------------------------------------------------------- */ |
| 220 | /* Function Name : AT91F_DataFlashWriteBuffer */ |
| 221 | /* Object : Write data to the internal sram buffer 1 or 2 */ |
| 222 | /* Input Parameters : DataFlash Service */ |
| 223 | /* : <BufferCommand> = command to write buffer1 or 2 */ |
| 224 | /* : <*dataBuffer> = data buffer to write */ |
| 225 | /* : <bufferAddress> = address in the internal buffer */ |
| 226 | /* : <SizeToWrite> = data buffer size */ |
| 227 | /* Return value : State of the dataflash */ |
| 228 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 229 | AT91S_DataFlashStatus AT91F_DataFlashWriteBuffer( |
| 230 | AT91PS_DataFlash pDataFlash, |
| 231 | unsigned char BufferCommand, |
| 232 | unsigned char *dataBuffer, |
| 233 | unsigned int bufferAddress, |
| 234 | int SizeToWrite) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 235 | { |
| 236 | int cmdsize; |
| 237 | /* Test if the buffer command is legal */ |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 238 | if ((BufferCommand != DB_BUF1_WRITE) && |
| 239 | (BufferCommand != DB_BUF2_WRITE)) { |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 240 | return DATAFLASH_BAD_COMMAND; |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 241 | } |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 242 | |
| 243 | /* buffer address must be lower than page size */ |
| 244 | if (bufferAddress > pDataFlash->pDevice->pages_size) |
| 245 | return DATAFLASH_BAD_ADDRESS; |
| 246 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 247 | if ((pDataFlash->pDataFlashDesc->state) != IDLE) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 248 | return DATAFLASH_BUSY; |
| 249 | |
| 250 | /* Send first Write Command */ |
| 251 | pDataFlash->pDataFlashDesc->command[0] = BufferCommand; |
| 252 | pDataFlash->pDataFlashDesc->command[1] = 0; |
| 253 | if (pDataFlash->pDevice->pages_number >= 16384) { |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 254 | pDataFlash->pDataFlashDesc->command[2] = 0; |
| 255 | pDataFlash->pDataFlashDesc->command[3] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 256 | (unsigned char)(((unsigned int)(bufferAddress & |
| 257 | pDataFlash->pDevice-> |
| 258 | byte_mask)) >> 8); |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 259 | pDataFlash->pDataFlashDesc->command[4] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 260 | (unsigned char)((unsigned int)bufferAddress & 0x00FF); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 261 | cmdsize = 5; |
| 262 | } else { |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 263 | pDataFlash->pDataFlashDesc->command[2] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 264 | (unsigned char)(((unsigned int)(bufferAddress & |
| 265 | pDataFlash->pDevice-> |
| 266 | byte_mask)) >> 8); |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 267 | pDataFlash->pDataFlashDesc->command[3] = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 268 | (unsigned char)((unsigned int)bufferAddress & 0x00FF); |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 269 | pDataFlash->pDataFlashDesc->command[4] = 0; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 270 | cmdsize = 4; |
| 271 | } |
| 272 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 273 | pDataFlash->pDataFlashDesc->tx_cmd_pt = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 274 | pDataFlash->pDataFlashDesc->command; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 275 | pDataFlash->pDataFlashDesc->tx_cmd_size = cmdsize; |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 276 | pDataFlash->pDataFlashDesc->rx_cmd_pt = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 277 | pDataFlash->pDataFlashDesc->command; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 278 | pDataFlash->pDataFlashDesc->rx_cmd_size = cmdsize; |
| 279 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 280 | pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer; |
| 281 | pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer; |
| 282 | pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite; |
| 283 | pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 284 | |
| 285 | return AT91F_SpiWrite(pDataFlash->pDataFlashDesc); |
| 286 | } |
| 287 | |
| 288 | /*---------------------------------------------------------------------------*/ |
| 289 | /* Function Name : AT91F_PageErase */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 290 | /* Object : Erase a page */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 291 | /* Input Parameters : DataFlash Service */ |
| 292 | /* : Page concerned */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 293 | /* : */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 294 | /* Return value : State of the dataflash */ |
| 295 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 296 | AT91S_DataFlashStatus AT91F_PageErase( |
| 297 | AT91PS_DataFlash pDataFlash, |
| 298 | unsigned int page) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 299 | { |
| 300 | int cmdsize; |
| 301 | /* Test if the buffer command is legal */ |
| 302 | /* no data to transmit or receive */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 303 | pDataFlash->pDataFlashDesc->tx_data_size = 0; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 304 | |
| 305 | cmdsize = 4; |
| 306 | if (pDataFlash->pDevice->pages_number >= 16384) |
| 307 | cmdsize = 5; |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 308 | return (AT91F_DataFlashSendCommand(pDataFlash, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 309 | DB_PAGE_ERASE, cmdsize, |
| 310 | page * pDataFlash->pDevice->pages_size)); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 311 | } |
| 312 | |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 313 | /*---------------------------------------------------------------------------*/ |
| 314 | /* Function Name : AT91F_BlockErase */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 315 | /* Object : Erase a Block */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 316 | /* Input Parameters : DataFlash Service */ |
| 317 | /* : Page concerned */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 318 | /* : */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 319 | /* Return value : State of the dataflash */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 320 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 321 | AT91S_DataFlashStatus AT91F_BlockErase( |
| 322 | AT91PS_DataFlash pDataFlash, |
| 323 | unsigned int block) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 324 | { |
| 325 | int cmdsize; |
| 326 | /* Test if the buffer command is legal */ |
| 327 | /* no data to transmit or receive */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 328 | pDataFlash->pDataFlashDesc->tx_data_size = 0; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 329 | cmdsize = 4; |
| 330 | if (pDataFlash->pDevice->pages_number >= 16384) |
| 331 | cmdsize = 5; |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 332 | return (AT91F_DataFlashSendCommand(pDataFlash, DB_BLOCK_ERASE, cmdsize, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 333 | block * 8 * |
| 334 | pDataFlash->pDevice->pages_size)); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 335 | } |
| 336 | |
| 337 | /*---------------------------------------------------------------------------*/ |
| 338 | /* Function Name : AT91F_WriteBufferToMain */ |
| 339 | /* Object : Write buffer to the main memory */ |
| 340 | /* Input Parameters : DataFlash Service */ |
| 341 | /* : <BufferCommand> = command to send to buffer1 or buffer2 */ |
| 342 | /* : <dest> = main memory address */ |
| 343 | /* Return value : State of the dataflash */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 344 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 345 | AT91S_DataFlashStatus AT91F_WriteBufferToMain(AT91PS_DataFlash pDataFlash, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 346 | unsigned char BufferCommand, |
| 347 | unsigned int dest) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 348 | { |
| 349 | int cmdsize; |
| 350 | /* Test if the buffer command is correct */ |
| 351 | if ((BufferCommand != DB_BUF1_PAGE_PGM) && |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 352 | (BufferCommand != DB_BUF1_PAGE_ERASE_PGM) && |
| 353 | (BufferCommand != DB_BUF2_PAGE_PGM) && |
| 354 | (BufferCommand != DB_BUF2_PAGE_ERASE_PGM)) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 355 | return DATAFLASH_BAD_COMMAND; |
| 356 | |
| 357 | /* no data to transmit or receive */ |
| 358 | pDataFlash->pDataFlashDesc->tx_data_size = 0; |
| 359 | |
| 360 | cmdsize = 4; |
| 361 | if (pDataFlash->pDevice->pages_number >= 16384) |
| 362 | cmdsize = 5; |
| 363 | /* Send the command to the dataflash */ |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 364 | return (AT91F_DataFlashSendCommand(pDataFlash, BufferCommand, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 365 | cmdsize, dest)); |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 366 | } |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 367 | |
| 368 | /*---------------------------------------------------------------------------*/ |
| 369 | /* Function Name : AT91F_PartialPageWrite */ |
| 370 | /* Object : Erase partielly a page */ |
| 371 | /* Input Parameters : <page> = page number */ |
| 372 | /* : <AdrInpage> = adr to begin the fading */ |
| 373 | /* : <length> = Number of bytes to erase */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 374 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 375 | AT91S_DataFlashStatus AT91F_PartialPageWrite(AT91PS_DataFlash pDataFlash, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 376 | unsigned char *src, |
| 377 | unsigned int dest, |
| 378 | unsigned int size) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 379 | { |
| 380 | unsigned int page; |
| 381 | unsigned int AdrInPage; |
| 382 | |
| 383 | page = dest / (pDataFlash->pDevice->pages_size); |
| 384 | AdrInPage = dest % (pDataFlash->pDevice->pages_size); |
| 385 | |
| 386 | /* Read the contents of the page in the Sram Buffer */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 387 | AT91F_MainMemoryToBufferTransfert(pDataFlash, DB_PAGE_2_BUF1_TRF, page); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 388 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 389 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 390 | /*Update the SRAM buffer */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 391 | AT91F_DataFlashWriteBuffer(pDataFlash, DB_BUF1_WRITE, src, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 392 | AdrInPage, size); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 393 | |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 394 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 395 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 396 | |
| 397 | /* Erase page if a 128 Mbits device */ |
| 398 | if (pDataFlash->pDevice->pages_number >= 16384) { |
| 399 | AT91F_PageErase(pDataFlash, page); |
| 400 | /* Rewrite the modified Sram Buffer in the main memory */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 401 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 402 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | /* Rewrite the modified Sram Buffer in the main memory */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 406 | return (AT91F_WriteBufferToMain(pDataFlash, DB_BUF1_PAGE_ERASE_PGM, |
| 407 | (page * |
| 408 | pDataFlash->pDevice->pages_size))); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 409 | } |
| 410 | |
| 411 | /*---------------------------------------------------------------------------*/ |
| 412 | /* Function Name : AT91F_DataFlashWrite */ |
| 413 | /* Object : */ |
| 414 | /* Input Parameters : <*src> = Source buffer */ |
| 415 | /* : <dest> = dataflash adress */ |
| 416 | /* : <size> = data buffer size */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 417 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 418 | AT91S_DataFlashStatus AT91F_DataFlashWrite(AT91PS_DataFlash pDataFlash, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 419 | unsigned char *src, |
| 420 | int dest, int size) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 421 | { |
| 422 | unsigned int length; |
| 423 | unsigned int page; |
| 424 | unsigned int status; |
| 425 | |
| 426 | AT91F_SpiEnable(pDataFlash->pDevice->cs); |
| 427 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 428 | if ((dest + size) > (pDataFlash->pDevice->pages_size * |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 429 | (pDataFlash->pDevice->pages_number))) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 430 | return DATAFLASH_MEMORY_OVERFLOW; |
| 431 | |
| 432 | /* If destination does not fit a page start address */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 433 | if ((dest % ((unsigned int)(pDataFlash->pDevice->pages_size))) != 0) { |
Wolfgang Denk | de74b9e | 2007-10-13 21:15:39 +0200 | [diff] [blame] | 434 | length = |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 435 | pDataFlash->pDevice->pages_size - |
| 436 | (dest % ((unsigned int)(pDataFlash->pDevice->pages_size))); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 437 | |
| 438 | if (size < length) |
| 439 | length = size; |
| 440 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 441 | if (!AT91F_PartialPageWrite(pDataFlash, src, dest, length)) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 442 | return DATAFLASH_ERROR; |
| 443 | |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 444 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 445 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 446 | |
| 447 | /* Update size, source and destination pointers */ |
| 448 | size -= length; |
| 449 | dest += length; |
| 450 | src += length; |
| 451 | } |
| 452 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 453 | while ((size - pDataFlash->pDevice->pages_size) >= 0) { |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 454 | /* program dataflash page */ |
| 455 | page = (unsigned int)dest / (pDataFlash->pDevice->pages_size); |
| 456 | |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 457 | status = AT91F_DataFlashWriteBuffer(pDataFlash, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 458 | DB_BUF1_WRITE, src, 0, |
| 459 | pDataFlash->pDevice-> |
| 460 | pages_size); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 461 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 462 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 463 | |
| 464 | status = AT91F_PageErase(pDataFlash, page); |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 465 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 466 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 467 | if (!status) |
| 468 | return DATAFLASH_ERROR; |
| 469 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 470 | status = AT91F_WriteBufferToMain(pDataFlash, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 471 | DB_BUF1_PAGE_PGM, dest); |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 472 | if (!status) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 473 | return DATAFLASH_ERROR; |
| 474 | |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 475 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 476 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 477 | |
| 478 | /* Update size, source and destination pointers */ |
| 479 | size -= pDataFlash->pDevice->pages_size; |
| 480 | dest += pDataFlash->pDevice->pages_size; |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 481 | src += pDataFlash->pDevice->pages_size; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 482 | } |
| 483 | |
| 484 | /* If still some bytes to read */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 485 | if (size > 0) { |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 486 | /* program dataflash page */ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 487 | if (!AT91F_PartialPageWrite(pDataFlash, src, dest, size)) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 488 | return DATAFLASH_ERROR; |
| 489 | |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 490 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 491 | AT91C_TIMEOUT_WRDY); |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 492 | } |
| 493 | return DATAFLASH_OK; |
| 494 | } |
| 495 | |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 496 | /*---------------------------------------------------------------------------*/ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 497 | /* Function Name : AT91F_DataFlashRead */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 498 | /* Object : Read a block in dataflash */ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 499 | /* Input Parameters : */ |
| 500 | /* Return value : */ |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 501 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 502 | int AT91F_DataFlashRead(AT91PS_DataFlash pDataFlash, |
| 503 | unsigned long addr, unsigned long size, char *buffer) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 504 | { |
| 505 | unsigned long SizeToRead; |
| 506 | |
| 507 | AT91F_SpiEnable(pDataFlash->pDevice->cs); |
| 508 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 509 | if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 510 | AT91C_TIMEOUT_WRDY) != DATAFLASH_OK) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 511 | return -1; |
| 512 | |
| 513 | while (size) { |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 514 | SizeToRead = (size < 0x8000) ? size : 0x8000; |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 515 | |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 516 | if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 517 | AT91C_TIMEOUT_WRDY) != |
| 518 | DATAFLASH_OK) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 519 | return -1; |
| 520 | |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 521 | if (AT91F_DataFlashContinuousRead(pDataFlash, addr, |
Peter Pearse | bd86220 | 2007-09-18 13:07:54 +0100 | [diff] [blame] | 522 | (uchar *) buffer, |
| 523 | SizeToRead) != DATAFLASH_OK) |
Peter Pearse | dcbfd2e | 2007-08-14 10:14:05 +0100 | [diff] [blame] | 524 | return -1; |
| 525 | |
| 526 | size -= SizeToRead; |
| 527 | addr += SizeToRead; |
| 528 | buffer += SizeToRead; |
| 529 | } |
| 530 | |
| 531 | return DATAFLASH_OK; |
| 532 | } |
| 533 | |
Peter Pearse | 0c42f36 | 2007-08-14 10:46:32 +0100 | [diff] [blame] | 534 | /*---------------------------------------------------------------------------*/ |
Wolfgang Denk | 53677ef | 2008-05-20 16:00:29 +0200 | [diff] [blame] | 535 | /* Function Name : AT91F_DataflashProbe */ |
| 536 | /* Object : */ |
| 537 | /* Input Parameters : */ |
Peter Pearse | 0c42f36 | 2007-08-14 10:46:32 +0100 | [diff] [blame] | 538 | /* Return value : Dataflash status register */ |
Wolfgang Denk | f01dbb5 | 2007-08-14 18:42:36 +0200 | [diff] [blame] | 539 | /*---------------------------------------------------------------------------*/ |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 540 | int AT91F_DataflashProbe(int cs, AT91PS_DataflashDesc pDesc) |
| 541 | { |
Peter Pearse | 0c42f36 | 2007-08-14 10:46:32 +0100 | [diff] [blame] | 542 | AT91F_SpiEnable(cs); |
| 543 | AT91F_DataFlashGetStatus(pDesc); |
Peter Pearse | 6e4bf9b | 2007-09-04 14:25:51 +0100 | [diff] [blame] | 544 | return ((pDesc->command[1] == 0xFF) ? 0 : pDesc->command[1] & 0x3C); |
Peter Pearse | 0c42f36 | 2007-08-14 10:46:32 +0100 | [diff] [blame] | 545 | } |