at45.c 19 KB

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