at45.c 19 KB

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