wdenk | 2abbe07 | 2003-06-16 23:50:08 +0000 | [diff] [blame^] | 1 | /* Driver for ATMEL DataFlash support |
| 2 | * Author : Hamid Ikdoumi (Atmel) |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public License as |
| 6 | * published by the Free Software Foundation; either version 2 of |
| 7 | * the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 17 | * MA 02111-1307 USA |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #include <config.h> |
| 22 | #include <common.h> |
| 23 | #include <asm/hardware.h> |
| 24 | |
| 25 | #ifdef CONFIG_HAS_DATAFLASH |
| 26 | #include <dataflash.h> |
| 27 | |
| 28 | #define SPI_CLK 5000000 |
| 29 | #define AT91C_SPI_PCS0_SERIAL_DATAFLASH 0xE /* Chip Select 0 : NPCS0 %1110 */ |
| 30 | #define AT91C_SPI_PCS3_DATAFLASH_CARD 0x7 /* Chip Select 3 : NPCS3 %0111 */ |
| 31 | |
| 32 | void AT91F_SpiInit(void) { |
| 33 | |
| 34 | /*-------------------------------------------------------------------*/ |
| 35 | /* SPI DataFlash Init */ |
| 36 | /*-------------------------------------------------------------------*/ |
| 37 | /* Configure PIOs */ |
| 38 | AT91C_BASE_PIOA->PIO_ASR = AT91C_PA3_NPCS0 | AT91C_PA4_NPCS1 | AT91C_PA1_MOSI | AT91C_PA5_NPCS2 | |
| 39 | AT91C_PA6_NPCS3 | AT91C_PA0_MISO | AT91C_PA2_SPCK; |
| 40 | AT91C_BASE_PIOA->PIO_PDR = AT91C_PA3_NPCS0 | AT91C_PA4_NPCS1 | AT91C_PA1_MOSI | AT91C_PA5_NPCS2 | |
| 41 | AT91C_PA6_NPCS3 | AT91C_PA0_MISO | AT91C_PA2_SPCK; |
| 42 | /* Enable CLock */ |
| 43 | AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_SPI; |
| 44 | |
| 45 | /* Reset the SPI */ |
| 46 | AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SWRST; |
| 47 | |
| 48 | /* Configure SPI in Master Mode with No CS selected !!! */ |
| 49 | AT91C_BASE_SPI->SPI_MR = AT91C_SPI_MSTR | AT91C_SPI_MODFDIS | AT91C_SPI_PCS; |
| 50 | |
| 51 | /* Configure CS0 and CS3 */ |
| 52 | *(AT91C_SPI_CSR + 0) = AT91C_SPI_CPOL | (AT91C_SPI_DLYBS & 0x100000) | ((AT91C_MASTER_CLOCK / (2*SPI_CLK)) << 8); |
| 53 | |
| 54 | *(AT91C_SPI_CSR + 3) = AT91C_SPI_CPOL | (AT91C_SPI_DLYBS & 0x100000) | ((AT91C_MASTER_CLOCK / (2*SPI_CLK)) << 8); |
| 55 | |
| 56 | } |
| 57 | |
| 58 | void AT91F_SpiEnable(int cs) { |
| 59 | switch(cs) { |
| 60 | case 0: /* Configure SPI CS0 for Serial DataFlash AT45DBxx */ |
| 61 | AT91C_BASE_SPI->SPI_MR &= 0xFFF0FFFF; |
| 62 | AT91C_BASE_SPI->SPI_MR |= ((AT91C_SPI_PCS0_SERIAL_DATAFLASH<<16) & AT91C_SPI_PCS); |
| 63 | break; |
| 64 | case 3: /* Configure SPI CS3 for Serial DataFlash Card */ |
| 65 | /* Set up PIO SDC_TYPE to switch on DataFlash Card and not MMC/SDCard */ |
| 66 | AT91C_BASE_PIOB->PIO_PER = AT91C_PIO_PB7; /* Set in PIO mode */ |
| 67 | AT91C_BASE_PIOB->PIO_OER = AT91C_PIO_PB7; /* Configure in output */ |
| 68 | /* Clear Output */ |
| 69 | AT91C_BASE_PIOB->PIO_CODR = AT91C_PIO_PB7; |
| 70 | /* Configure PCS */ |
| 71 | AT91C_BASE_SPI->SPI_MR &= 0xFFF0FFFF; |
| 72 | AT91C_BASE_SPI->SPI_MR |= ((AT91C_SPI_PCS3_DATAFLASH_CARD<<16) & AT91C_SPI_PCS); |
| 73 | break; |
| 74 | } |
| 75 | |
| 76 | /* SPI_Enable */ |
| 77 | AT91C_BASE_SPI->SPI_CR = AT91C_SPI_SPIEN; |
| 78 | } |
| 79 | |
| 80 | /*----------------------------------------------------------------------------*/ |
| 81 | /* \fn AT91F_SpiWrite */ |
| 82 | /* \brief Set the PDC registers for a transfert */ |
| 83 | /*----------------------------------------------------------------------------*/ |
| 84 | unsigned int AT91F_SpiWrite ( AT91PS_DataflashDesc pDesc ) |
| 85 | { |
| 86 | unsigned int timeout; |
| 87 | |
| 88 | pDesc->state = BUSY; |
| 89 | |
| 90 | AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS; |
| 91 | |
| 92 | /* Initialize the Transmit and Receive Pointer */ |
| 93 | AT91C_BASE_SPI->SPI_RPR = (unsigned int)pDesc->rx_cmd_pt ; |
| 94 | AT91C_BASE_SPI->SPI_TPR = (unsigned int)pDesc->tx_cmd_pt ; |
| 95 | |
| 96 | /* Intialize the Transmit and Receive Counters */ |
| 97 | AT91C_BASE_SPI->SPI_RCR = pDesc->rx_cmd_size; |
| 98 | AT91C_BASE_SPI->SPI_TCR = pDesc->tx_cmd_size; |
| 99 | |
| 100 | if ( pDesc->tx_data_size != 0 ) { |
| 101 | /* Initialize the Next Transmit and Next Receive Pointer */ |
| 102 | AT91C_BASE_SPI->SPI_RNPR = (unsigned int)pDesc->rx_data_pt ; |
| 103 | AT91C_BASE_SPI->SPI_TNPR = (unsigned int)pDesc->tx_data_pt ; |
| 104 | |
| 105 | /* Intialize the Next Transmit and Next Receive Counters */ |
| 106 | AT91C_BASE_SPI->SPI_RNCR = pDesc->rx_data_size ; |
| 107 | AT91C_BASE_SPI->SPI_TNCR = pDesc->tx_data_size ; |
| 108 | } |
| 109 | |
| 110 | /* arm simple, non interrupt dependent timer */ |
| 111 | reset_timer_masked(); |
| 112 | timeout = 0; |
| 113 | |
| 114 | AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTEN + AT91C_PDC_RXTEN; |
| 115 | while(!(AT91C_BASE_SPI->SPI_SR & AT91C_SPI_RXBUFF) && ((timeout = get_timer_masked() ) < CFG_SPI_WRITE_TOUT)); |
| 116 | AT91C_BASE_SPI->SPI_PTCR = AT91C_PDC_TXTDIS + AT91C_PDC_RXTDIS; |
| 117 | pDesc->state = IDLE; |
| 118 | |
| 119 | if (timeout >= CFG_SPI_WRITE_TOUT){ |
| 120 | printf("Error Timeout\n\r"); |
| 121 | return DATAFLASH_ERROR; |
| 122 | } |
| 123 | |
| 124 | return DATAFLASH_OK; |
| 125 | } |
| 126 | |
| 127 | |
| 128 | /*----------------------------------------------------------------------*/ |
| 129 | /* \fn AT91F_DataFlashSendCommand */ |
| 130 | /* \brief Generic function to send a command to the dataflash */ |
| 131 | /*----------------------------------------------------------------------*/ |
| 132 | AT91S_DataFlashStatus AT91F_DataFlashSendCommand( |
| 133 | AT91PS_DataFlash pDataFlash, |
| 134 | unsigned char OpCode, |
| 135 | unsigned int CmdSize, |
| 136 | unsigned int DataflashAddress) |
| 137 | { |
| 138 | unsigned int adr; |
| 139 | |
| 140 | if ( (pDataFlash->pDataFlashDesc->state) != IDLE) |
| 141 | return DATAFLASH_BUSY; |
| 142 | |
| 143 | /* process the address to obtain page address and byte address */ |
| 144 | adr = ((DataflashAddress / (pDataFlash->pDevice->pages_size)) << pDataFlash->pDevice->page_offset) + (DataflashAddress % (pDataFlash->pDevice->pages_size)); |
| 145 | |
| 146 | /* fill the command buffer */ |
| 147 | pDataFlash->pDataFlashDesc->command[0] = OpCode; |
| 148 | pDataFlash->pDataFlashDesc->command[1] = (unsigned char)((adr & 0x00FF0000) >> 16); |
| 149 | pDataFlash->pDataFlashDesc->command[2] = (unsigned char)((adr & 0x0000FF00) >> 8); |
| 150 | pDataFlash->pDataFlashDesc->command[3] = (unsigned char)(adr & 0x000000FF) ; |
| 151 | pDataFlash->pDataFlashDesc->command[4] = 0; |
| 152 | pDataFlash->pDataFlashDesc->command[5] = 0; |
| 153 | pDataFlash->pDataFlashDesc->command[6] = 0; |
| 154 | pDataFlash->pDataFlashDesc->command[7] = 0; |
| 155 | |
| 156 | /* Initialize the SpiData structure for the spi write fuction */ |
| 157 | pDataFlash->pDataFlashDesc->tx_cmd_pt = pDataFlash->pDataFlashDesc->command ; |
| 158 | pDataFlash->pDataFlashDesc->tx_cmd_size = CmdSize ; |
| 159 | pDataFlash->pDataFlashDesc->rx_cmd_pt = pDataFlash->pDataFlashDesc->command ; |
| 160 | pDataFlash->pDataFlashDesc->rx_cmd_size = CmdSize ; |
| 161 | |
| 162 | /* send the command and read the data */ |
| 163 | return AT91F_SpiWrite (pDataFlash->pDataFlashDesc); |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /*----------------------------------------------------------------------*/ |
| 168 | /* \fn AT91F_DataFlashGetStatus */ |
| 169 | /* \brief Read the status register of the dataflash */ |
| 170 | /*----------------------------------------------------------------------*/ |
| 171 | AT91S_DataFlashStatus AT91F_DataFlashGetStatus(AT91PS_DataflashDesc pDesc) |
| 172 | { |
| 173 | AT91S_DataFlashStatus status; |
| 174 | |
| 175 | /* if a transfert is in progress ==> return 0 */ |
| 176 | if( (pDesc->state) != IDLE) |
| 177 | return DATAFLASH_BUSY; |
| 178 | |
| 179 | /* first send the read status command (D7H) */ |
| 180 | pDesc->command[0] = DB_STATUS; |
| 181 | pDesc->command[1] = 0; |
| 182 | |
| 183 | pDesc->DataFlash_state = GET_STATUS; |
| 184 | pDesc->tx_data_size = 0 ; /* Transmit the command and receive response */ |
| 185 | pDesc->tx_cmd_pt = pDesc->command ; |
| 186 | pDesc->rx_cmd_pt = pDesc->command ; |
| 187 | pDesc->rx_cmd_size = 2 ; |
| 188 | pDesc->tx_cmd_size = 2 ; |
| 189 | status = AT91F_SpiWrite (pDesc); |
| 190 | |
| 191 | pDesc->DataFlash_state = *( (unsigned char *) (pDesc->rx_cmd_pt) +1); |
| 192 | |
| 193 | return status; |
| 194 | } |
| 195 | |
| 196 | |
| 197 | /*----------------------------------------------------------------------*/ |
| 198 | /* \fn AT91F_DataFlashWaitReady */ |
| 199 | /* \brief wait for dataflash ready (bit7 of the status register == 1) */ |
| 200 | /*----------------------------------------------------------------------*/ |
| 201 | AT91S_DataFlashStatus AT91F_DataFlashWaitReady(AT91PS_DataflashDesc pDataFlashDesc, unsigned int timeout) |
| 202 | { |
| 203 | pDataFlashDesc->DataFlash_state = IDLE; |
| 204 | |
| 205 | do { |
| 206 | AT91F_DataFlashGetStatus(pDataFlashDesc); |
| 207 | timeout--; |
| 208 | } |
| 209 | while( ((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) && (timeout > 0) ); |
| 210 | |
| 211 | if((pDataFlashDesc->DataFlash_state & 0x80) != 0x80) |
| 212 | return DATAFLASH_ERROR; |
| 213 | |
| 214 | return DATAFLASH_OK; |
| 215 | } |
| 216 | |
| 217 | |
| 218 | |
| 219 | /*------------------------------------------------------------------------------*/ |
| 220 | /* Function Name : AT91F_DataFlashContinuousRead */ |
| 221 | /* Object : Continuous stream Read */ |
| 222 | /* Input Parameters : DataFlash Service */ |
| 223 | /* : <src> = dataflash address */ |
| 224 | /* : <*dataBuffer> = data buffer pointer */ |
| 225 | /* : <sizeToRead> = data buffer size */ |
| 226 | /* Return value : State of the dataflash */ |
| 227 | /*------------------------------------------------------------------------------*/ |
| 228 | AT91S_DataFlashStatus AT91F_DataFlashContinuousRead ( |
| 229 | AT91PS_DataFlash pDataFlash, |
| 230 | int src, |
| 231 | unsigned char *dataBuffer, |
| 232 | int sizeToRead ) |
| 233 | { |
| 234 | /* Test the size to read in the device */ |
| 235 | if ( (src + sizeToRead) > (pDataFlash->pDevice->pages_size * (pDataFlash->pDevice->pages_number))) |
| 236 | return DATAFLASH_MEMORY_OVERFLOW; |
| 237 | |
| 238 | pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer; |
| 239 | pDataFlash->pDataFlashDesc->rx_data_size = sizeToRead; |
| 240 | pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer; |
| 241 | pDataFlash->pDataFlashDesc->tx_data_size = sizeToRead; |
| 242 | |
| 243 | /* Send the command to the dataflash */ |
| 244 | return(AT91F_DataFlashSendCommand (pDataFlash, DB_CONTINUOUS_ARRAY_READ, 8, src)); |
| 245 | } |
| 246 | |
| 247 | |
| 248 | |
| 249 | /*------------------------------------------------------------------------------*/ |
| 250 | /* Function Name : AT91F_DataFlashPagePgmBuf */ |
| 251 | /* Object : Main memory page program through buffer 1 or buffer 2 */ |
| 252 | /* Input Parameters : DataFlash Service */ |
| 253 | /* : <*src> = Source buffer */ |
| 254 | /* : <dest> = dataflash destination address */ |
| 255 | /* : <SizeToWrite> = data buffer size */ |
| 256 | /* Return value : State of the dataflash */ |
| 257 | /*------------------------------------------------------------------------------*/ |
| 258 | AT91S_DataFlashStatus AT91F_DataFlashPagePgmBuf( |
| 259 | AT91PS_DataFlash pDataFlash, |
| 260 | unsigned char *src, |
| 261 | unsigned int dest, |
| 262 | unsigned int SizeToWrite) |
| 263 | { |
| 264 | pDataFlash->pDataFlashDesc->tx_data_pt = src ; |
| 265 | pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite ; |
| 266 | pDataFlash->pDataFlashDesc->rx_data_pt = src; |
| 267 | pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite; |
| 268 | |
| 269 | /* Send the command to the dataflash */ |
| 270 | return(AT91F_DataFlashSendCommand (pDataFlash, DB_PAGE_PGM_BUF1, 4, dest)); |
| 271 | } |
| 272 | |
| 273 | |
| 274 | /*------------------------------------------------------------------------------*/ |
| 275 | /* Function Name : AT91F_MainMemoryToBufferTransfert */ |
| 276 | /* Object : Read a page in the SRAM Buffer 1 or 2 */ |
| 277 | /* Input Parameters : DataFlash Service */ |
| 278 | /* : Page concerned */ |
| 279 | /* : */ |
| 280 | /* Return value : State of the dataflash */ |
| 281 | /*------------------------------------------------------------------------------*/ |
| 282 | AT91S_DataFlashStatus AT91F_MainMemoryToBufferTransfert( |
| 283 | AT91PS_DataFlash pDataFlash, |
| 284 | unsigned char BufferCommand, |
| 285 | unsigned int page) |
| 286 | { |
| 287 | /* Test if the buffer command is legal */ |
| 288 | if ((BufferCommand != DB_PAGE_2_BUF1_TRF) && (BufferCommand != DB_PAGE_2_BUF2_TRF)) |
| 289 | return DATAFLASH_BAD_COMMAND; |
| 290 | |
| 291 | /* no data to transmit or receive */ |
| 292 | pDataFlash->pDataFlashDesc->tx_data_size = 0; |
| 293 | |
| 294 | return(AT91F_DataFlashSendCommand (pDataFlash, BufferCommand, 4, page*pDataFlash->pDevice->pages_size)); |
| 295 | } |
| 296 | |
| 297 | |
| 298 | |
| 299 | /*----------------------------------------------------------------------------- */ |
| 300 | /* Function Name : AT91F_DataFlashWriteBuffer */ |
| 301 | /* Object : Write data to the internal sram buffer 1 or 2 */ |
| 302 | /* Input Parameters : DataFlash Service */ |
| 303 | /* : <BufferCommand> = command to write buffer1 or buffer2 */ |
| 304 | /* : <*dataBuffer> = data buffer to write */ |
| 305 | /* : <bufferAddress> = address in the internal buffer */ |
| 306 | /* : <SizeToWrite> = data buffer size */ |
| 307 | /* Return value : State of the dataflash */ |
| 308 | /*------------------------------------------------------------------------------*/ |
| 309 | AT91S_DataFlashStatus AT91F_DataFlashWriteBuffer ( |
| 310 | AT91PS_DataFlash pDataFlash, |
| 311 | unsigned char BufferCommand, |
| 312 | unsigned char *dataBuffer, |
| 313 | unsigned int bufferAddress, |
| 314 | int SizeToWrite ) |
| 315 | { |
| 316 | /* Test if the buffer command is legal */ |
| 317 | if ((BufferCommand != DB_BUF1_WRITE) && (BufferCommand != DB_BUF2_WRITE)) |
| 318 | return DATAFLASH_BAD_COMMAND; |
| 319 | |
| 320 | /* buffer address must be lower than page size */ |
| 321 | if (bufferAddress > pDataFlash->pDevice->pages_size) |
| 322 | return DATAFLASH_BAD_ADDRESS; |
| 323 | |
| 324 | if ( (pDataFlash->pDataFlashDesc->state) != IDLE) |
| 325 | return DATAFLASH_BUSY; |
| 326 | |
| 327 | /* Send first Write Command */ |
| 328 | pDataFlash->pDataFlashDesc->command[0] = BufferCommand; |
| 329 | pDataFlash->pDataFlashDesc->command[1] = 0; |
| 330 | pDataFlash->pDataFlashDesc->command[2] = (unsigned char)(((unsigned int)(bufferAddress & pDataFlash->pDevice->byte_mask)) >> 8) ; |
| 331 | pDataFlash->pDataFlashDesc->command[3] = (unsigned char)((unsigned int)bufferAddress & 0x00FF) ; |
| 332 | |
| 333 | |
| 334 | pDataFlash->pDataFlashDesc->tx_cmd_pt = pDataFlash->pDataFlashDesc->command ; |
| 335 | pDataFlash->pDataFlashDesc->tx_cmd_size = 4 ; |
| 336 | pDataFlash->pDataFlashDesc->rx_cmd_pt = pDataFlash->pDataFlashDesc->command ; |
| 337 | pDataFlash->pDataFlashDesc->rx_cmd_size = 4 ; |
| 338 | |
| 339 | pDataFlash->pDataFlashDesc->rx_data_pt = dataBuffer ; |
| 340 | pDataFlash->pDataFlashDesc->tx_data_pt = dataBuffer ; |
| 341 | pDataFlash->pDataFlashDesc->rx_data_size = SizeToWrite ; |
| 342 | pDataFlash->pDataFlashDesc->tx_data_size = SizeToWrite ; |
| 343 | |
| 344 | return AT91F_SpiWrite(pDataFlash->pDataFlashDesc); |
| 345 | } |
| 346 | |
| 347 | |
| 348 | /*------------------------------------------------------------------------------*/ |
| 349 | /* Function Name : AT91F_WriteBufferToMain */ |
| 350 | /* Object : Write buffer to the main memory */ |
| 351 | /* Input Parameters : DataFlash Service */ |
| 352 | /* : <BufferCommand> = command to send to buffer1 or buffer2 */ |
| 353 | /* : <dest> = main memory address */ |
| 354 | /* Return value : State of the dataflash */ |
| 355 | /*------------------------------------------------------------------------------*/ |
| 356 | AT91S_DataFlashStatus AT91F_WriteBufferToMain ( |
| 357 | AT91PS_DataFlash pDataFlash, |
| 358 | unsigned char BufferCommand, |
| 359 | unsigned int dest ) |
| 360 | { |
| 361 | /* Test if the buffer command is correct */ |
| 362 | if ((BufferCommand != DB_BUF1_PAGE_PGM) && |
| 363 | (BufferCommand != DB_BUF1_PAGE_ERASE_PGM) && |
| 364 | (BufferCommand != DB_BUF2_PAGE_PGM) && |
| 365 | (BufferCommand != DB_BUF2_PAGE_ERASE_PGM) ) |
| 366 | return DATAFLASH_BAD_COMMAND; |
| 367 | |
| 368 | /* no data to transmit or receive */ |
| 369 | pDataFlash->pDataFlashDesc->tx_data_size = 0; |
| 370 | |
| 371 | /* Send the command to the dataflash */ |
| 372 | return(AT91F_DataFlashSendCommand (pDataFlash, BufferCommand, 4, dest)); |
| 373 | } |
| 374 | |
| 375 | |
| 376 | /*------------------------------------------------------------------------------*/ |
| 377 | /* Function Name : AT91F_PartialPageWrite */ |
| 378 | /* Object : Erase partielly a page */ |
| 379 | /* Input Parameters : <page> = page number */ |
| 380 | /* : <AdrInpage> = adr to begin the fading */ |
| 381 | /* : <length> = Number of bytes to erase */ |
| 382 | /*------------------------------------------------------------------------------*/ |
| 383 | AT91S_DataFlashStatus AT91F_PartialPageWrite ( |
| 384 | AT91PS_DataFlash pDataFlash, |
| 385 | unsigned char *src, |
| 386 | unsigned int dest, |
| 387 | unsigned int size) |
| 388 | { |
| 389 | unsigned int page; |
| 390 | unsigned int AdrInPage; |
| 391 | |
| 392 | page = dest / (pDataFlash->pDevice->pages_size); |
| 393 | AdrInPage = dest % (pDataFlash->pDevice->pages_size); |
| 394 | |
| 395 | /* Read the contents of the page in the Sram Buffer */ |
| 396 | AT91F_MainMemoryToBufferTransfert(pDataFlash, DB_PAGE_2_BUF1_TRF, page); |
| 397 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000); |
| 398 | /*Update the SRAM buffer */ |
| 399 | AT91F_DataFlashWriteBuffer(pDataFlash, DB_BUF1_WRITE, src, AdrInPage, size); |
| 400 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000); |
| 401 | |
| 402 | /* Rewrite the modified Sram Buffer in the main memory */ |
| 403 | return(AT91F_WriteBufferToMain(pDataFlash, DB_BUF1_PAGE_ERASE_PGM, (page*pDataFlash->pDevice->pages_size))); |
| 404 | } |
| 405 | |
| 406 | |
| 407 | |
| 408 | /*------------------------------------------------------------------------------*/ |
| 409 | /* Function Name : AT91F_DataFlashWrite_Overloaded */ |
| 410 | /* Object : */ |
| 411 | /* Input Parameters : <*src> = Source buffer */ |
| 412 | /* : <dest> = dataflash adress */ |
| 413 | /* : <size> = data buffer size */ |
| 414 | /*------------------------------------------------------------------------------*/ |
| 415 | AT91S_DataFlashStatus AT91F_DataFlashWrite( |
| 416 | AT91PS_DataFlash pDataFlash, |
| 417 | unsigned char *src, |
| 418 | int dest, |
| 419 | int size ) |
| 420 | { |
| 421 | unsigned int length; |
| 422 | |
| 423 | AT91F_SpiEnable(pDataFlash->pDevice->cs); |
| 424 | |
| 425 | if ( (dest + size) > (pDataFlash->pDevice->pages_size * (pDataFlash->pDevice->pages_number))) |
| 426 | return DATAFLASH_MEMORY_OVERFLOW; |
| 427 | |
| 428 | /* If destination does not fit a page start address */ |
| 429 | if ((dest % ((unsigned int)(pDataFlash->pDevice->pages_size))) != 0 ) { |
| 430 | length = pDataFlash->pDevice->pages_size - (dest % ((unsigned int)(pDataFlash->pDevice->pages_size))); |
| 431 | |
| 432 | if (size < length) |
| 433 | length = size; |
| 434 | |
| 435 | if(!AT91F_PartialPageWrite(pDataFlash,src, dest, length)) |
| 436 | return DATAFLASH_ERROR; |
| 437 | |
| 438 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000); |
| 439 | |
| 440 | /* Update size, source and destination pointers */ |
| 441 | size -= length; |
| 442 | dest += length; |
| 443 | src += length; |
| 444 | } |
| 445 | |
| 446 | while (( size - pDataFlash->pDevice->pages_size ) >= 0 ) { |
| 447 | /* program dataflash page */ |
| 448 | if(!AT91F_DataFlashPagePgmBuf(pDataFlash, src, dest, pDataFlash->pDevice->pages_size )) |
| 449 | return DATAFLASH_ERROR; |
| 450 | |
| 451 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000); |
| 452 | |
| 453 | /* Update size, source and destination pointers */ |
| 454 | size -= pDataFlash->pDevice->pages_size ; |
| 455 | dest += pDataFlash->pDevice->pages_size ; |
| 456 | src += pDataFlash->pDevice->pages_size ; |
| 457 | } |
| 458 | |
| 459 | /* If still some bytes to read */ |
| 460 | if ( size > 0 ) { |
| 461 | /* program dataflash page */ |
| 462 | if(!AT91F_PartialPageWrite(pDataFlash, src, dest, size) ) |
| 463 | return DATAFLASH_ERROR; |
| 464 | |
| 465 | AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000); |
| 466 | } |
| 467 | return DATAFLASH_OK; |
| 468 | } |
| 469 | |
| 470 | |
| 471 | /*------------------------------------------------------------------------------*/ |
| 472 | /* Function Name : AT91F_DataFlashRead */ |
| 473 | /* Object : Read a block in dataflash */ |
| 474 | /* Input Parameters : */ |
| 475 | /* Return value : */ |
| 476 | /*------------------------------------------------------------------------------*/ |
| 477 | int AT91F_DataFlashRead( |
| 478 | AT91PS_DataFlash pDataFlash, |
| 479 | unsigned long addr, |
| 480 | unsigned long size, |
| 481 | char *buffer) |
| 482 | { |
| 483 | unsigned long SizeToRead; |
| 484 | |
| 485 | AT91F_SpiEnable(pDataFlash->pDevice->cs); |
| 486 | |
| 487 | if(AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000) != DATAFLASH_OK) |
| 488 | return -1; |
| 489 | |
| 490 | while (size) { |
| 491 | SizeToRead = (size < 0x8000)? size:0x8000; |
| 492 | |
| 493 | if (AT91F_DataFlashWaitReady(pDataFlash->pDataFlashDesc, 1000) != DATAFLASH_OK) |
| 494 | return -1; |
| 495 | |
| 496 | if (AT91F_DataFlashContinuousRead (pDataFlash, addr, buffer, SizeToRead) != DATAFLASH_OK) |
| 497 | return -1; |
| 498 | |
| 499 | size -= SizeToRead; |
| 500 | addr += SizeToRead; |
| 501 | buffer += SizeToRead; |
| 502 | } |
| 503 | |
| 504 | return DATAFLASH_OK; |
| 505 | } |
| 506 | |
| 507 | |
| 508 | /*------------------------------------------------------------------------------*/ |
| 509 | /* Function Name : AT91F_DataflashProbe */ |
| 510 | /* Object : */ |
| 511 | /* Input Parameters : */ |
| 512 | /* Return value : Dataflash status register */ |
| 513 | /*------------------------------------------------------------------------------*/ |
| 514 | int AT91F_DataflashProbe(int cs, AT91PS_DataflashDesc pDesc) |
| 515 | { |
| 516 | AT91F_SpiEnable(cs); |
| 517 | AT91F_DataFlashGetStatus(pDesc); |
| 518 | return((pDesc->command[1] == 0xFF)? 0: pDesc->command[1] & 0x3C); |
| 519 | } |
| 520 | |
| 521 | #endif |