at45.c 23 KB

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