at45.c 19 KB

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