at45.c 19 KB

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