at45.c 19 KB

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