at45.c 20 KB

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