at45.c 23 KB

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