at45db.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  1. /*
  2. * Copyright (C) 2006 by egnite Software GmbH. All rights reserved.
  3. * Copyright (C) 2008 by egnite GmbH. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the copyright holders nor the names of
  15. * contributors may be used to endorse or promote products derived
  16. * from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  21. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  22. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  23. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  24. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  25. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  26. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  27. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  28. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  29. * SUCH DAMAGE.
  30. *
  31. * For additional information see http://www.ethernut.de/
  32. *
  33. */
  34. /*!
  35. * \file dev/at45db.c
  36. * \brief Routines for Adesto/AtmelAT45 serial dataflash memory chips.
  37. *
  38. * \verbatim
  39. *
  40. * $Log$
  41. * Revision 1.91 2010/12/15 15:20:42 ve2yag
  42. * Added MCU_AT91SAM7S family.
  43. *
  44. * Revision 1.9 2009/02/06 15:53:42 haraldkipp
  45. * Corrected a bug with non-negated chip selects.
  46. *
  47. * Revision 1.8 2009/01/17 11:26:46 haraldkipp
  48. * Getting rid of two remaining BSD types in favor of stdint.
  49. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  50. *
  51. * Revision 1.7 2008/12/15 19:18:49 haraldkipp
  52. * Enable DataFlash support for EIR board.
  53. *
  54. * Revision 1.6 2008/08/11 06:59:41 haraldkipp
  55. * BSD types replaced by stdint types (feature request #1282721).
  56. *
  57. * Revision 1.5 2008/08/06 12:51:09 haraldkipp
  58. * Added support for Ethernut 5 (AT91SAM9XE reference design).
  59. *
  60. * Revision 1.4 2008/02/15 17:10:43 haraldkipp
  61. * At45dbPageErase selected the wrong bank. Fixed. Parameter pgn (page number)
  62. * of At45dbPageWrite() changed from unsigned int to unsigned long.
  63. * New routines At45dbPages() and At45dbPageSize() allow to determine the
  64. * chip's layout.
  65. *
  66. * Revision 1.3 2007/08/17 10:45:21 haraldkipp
  67. * Enhanced documentation.
  68. *
  69. * Revision 1.2 2006/10/08 16:48:09 haraldkipp
  70. * Documentation fixed
  71. *
  72. * Revision 1.1 2006/09/29 12:41:55 haraldkipp
  73. * Added support for AT45 serial DataFlash memory chips. Currently limited
  74. * to AT91 builds.
  75. *
  76. *
  77. * \endverbatim
  78. */
  79. #include <cfg/os.h>
  80. #include <cfg/memory.h>
  81. #include <sys/timer.h>
  82. #include <string.h>
  83. #include <stdlib.h>
  84. #include <dev/at91_spi.h>
  85. #include <dev/at45db.h>
  86. #ifndef MAX_AT45_DEVICES
  87. #define MAX_AT45_DEVICES 1
  88. #endif
  89. #ifndef MAX_AT45_CMDLEN
  90. #define MAX_AT45_CMDLEN 8
  91. #endif
  92. #ifndef AT45_CONF_DFSPI
  93. #define AT45_CONF_DFSPI SPI0_BASE
  94. #endif
  95. #ifndef AT45_CONF_DFPCS
  96. #define AT45_CONF_DFPCS 1
  97. #endif
  98. #ifndef AT45_ERASE_WAIT
  99. #define AT45_ERASE_WAIT 3000
  100. #endif
  101. #ifndef AT45_CHIP_ERASE_WAIT
  102. #define AT45_CHIP_ERASE_WAIT 50000
  103. #endif
  104. #ifndef AT45_WRITE_POLLS
  105. #define AT45_WRITE_POLLS 1000
  106. #endif
  107. /*!
  108. * \name AT45 DataFlash Commands
  109. */
  110. /*@{*/
  111. /*! \brief Continuos read (low frequency).
  112. *
  113. * Reads a continous stream in low speed mode. Automatically switches
  114. * to the the page and wraps to the first page after the last has been
  115. * read. Note, that the data buffers are not used for this operation.
  116. */
  117. #define DFCMD_CONT_READ_LF 0x03
  118. /*! \brief Continuos read (high frequency).
  119. *
  120. * Reads a continous stream in high speed mode. Automatically switches
  121. * to the the page and wraps to the first page after the last has been
  122. * read. Note, that the data buffers are not used for this operation.
  123. */
  124. #define DFCMD_CONT_READ_HF 0x0B
  125. /*! \brief Block erase.
  126. */
  127. #define DFCMD_BLOCK_ERASE 0x50
  128. /*! \brief Sector erase.
  129. */
  130. #define DFCMD_SECTOR_ERASE 0x7C
  131. /*! \brief Page erase.
  132. */
  133. #define DFCMD_PAGE_ERASE 0x81
  134. /*! \brief Main memory page program through buffer 1.
  135. */
  136. #define DFCMD_BUF1_PROG 0x82
  137. /*! \brief Buffer 1 flash with page erase.
  138. */
  139. #define DFCMD_BUF1_FLASH 0x83
  140. /*! \brief Buffer 1 write.
  141. */
  142. #define DFCMD_BUF1_WRITE 0x84
  143. /*! \brief Main memory page program through buffer 2.
  144. */
  145. #define DFCMD_BUF2_PROG 0x85
  146. /*! \brief Buffer 2 flash with page erase.
  147. */
  148. #define DFCMD_BUF2_FLASH 0x86
  149. /*! \brief Buffer 2 write.
  150. */
  151. #define DFCMD_BUF2_WRITE 0x87
  152. /*! \brief Buffer 1 flash without page erase.
  153. */
  154. #define DFCMD_BUF1_FLASH_NE 0x88
  155. /*! \brief Buffer 2 flash without page erase.
  156. */
  157. #define DFCMD_BUF2_FLASH_NE 0x89
  158. /*! \brief Chip erase.
  159. */
  160. #define DFCMD_CHIP_ERASE 0xC7
  161. /*! \brief Buffer 1 read (low frequency).
  162. */
  163. #define DFCMD_BUF1_READ_LF 0xD1
  164. /*! \brief Read main memory page.
  165. *
  166. * Automatically wraps to the first byte of the same page after
  167. * the last byte had been read. The data buffers are left unchanged.
  168. */
  169. #define DFCMD_READ_PAGE 0xD2
  170. /*! \brief Buffer 2 read (low frequency).
  171. */
  172. #define DFCMD_BUF2_READ_LF 0xD3
  173. /*! \brief Buffer 1 read.
  174. */
  175. #define DFCMD_BUF1_READ 0xD4
  176. /*! \brief Buffer 2 read.
  177. */
  178. #define DFCMD_BUF2_READ 0xD6
  179. /*! \brief Read status register.
  180. */
  181. #define DFCMD_READ_STATUS 0xD7
  182. /*! \brief Continuos read (legacy).
  183. *
  184. * Reads a continous stream. Automatically switches to the the page
  185. * and wraps to the first page after the last has been read. Note,
  186. * that the data buffers are not used for this operation.
  187. */
  188. #define DFCMD_CONT_READ 0xE8
  189. /*@}*/
  190. #define AT45DB_AT91
  191. #if defined(AT45DB_SPI0_DEVICE)
  192. #include <dev/sppif0.h>
  193. #if defined(AT45DB_RESET_ACTIVE_HIGH)
  194. #define SpiReset(act) Sppi0ChipReset(AT45DB_SPI0_DEVICE, act)
  195. #else
  196. #define SpiReset(act) Sppi0ChipReset(AT45DB_SPI0_DEVICE, !act)
  197. #endif
  198. #define SpiSetMode() Sppi0SetMode(AT45DB_SPI0_DEVICE, AT45DB_SPI_MODE)
  199. #define SpiSetSpeed() Sppi0SetSpeed(AT45DB_SPI0_DEVICE, AT45DB_SPI_RATE)
  200. #if defined(AT45DB_SELECT_ACTIVE_HIGH)
  201. #define SpiSelect() Sppi0SelectDevice(AT45DB_SPI0_DEVICE)
  202. #define SpiDeselect() Sppi0DeselectDevice(AT45DB_SPI0_DEVICE)
  203. #else
  204. #define SpiSelect() Sppi0NegSelectDevice(AT45DB_SPI0_DEVICE)
  205. #define SpiDeselect() Sppi0NegDeselectDevice(AT45DB_SPI0_DEVICE)
  206. #endif
  207. #define SpiByte Sppi0Byte
  208. #elif defined(AT45DB_SBBI0_DEVICE)
  209. #include <dev/sbbif0.h>
  210. #if defined(AT45DB_RESET_ACTIVE_HIGH)
  211. #define SpiReset(act) Sbbi0ChipReset(AT45DB_SBBI0_DEVICE, act)
  212. #else
  213. #define SpiReset(act) Sbbi0ChipReset(AT45DB_SBBI0_DEVICE, !act)
  214. #endif
  215. #define SpiSetMode() Sbbi0SetMode(AT45DB_SBBI0_DEVICE, AT45DB_SPI_MODE)
  216. #define SpiSetSpeed() Sbbi0SetSpeed(AT45DB_SBBI0_DEVICE, AT45DB_SPI_RATE)
  217. #if defined(AT45DB_SELECT_ACTIVE_HIGH)
  218. #define SpiSelect() Sbbi0SelectDevice(AT45DB_SBBI0_DEVICE)
  219. #define SpiDeselect() Sbbi0DeselectDevice(AT45DB_SBBI0_DEVICE)
  220. #else
  221. #define SpiSelect() Sbbi0NegSelectDevice(AT45DB_SBBI0_DEVICE)
  222. #define SpiDeselect() Sbbi0NegDeselectDevice(AT45DB_SBBI0_DEVICE)
  223. #endif
  224. #define SpiByte Sbbi0Byte
  225. #endif
  226. /*!
  227. * \brief Known device type entry.
  228. */
  229. typedef struct _AT45_DEVTAB {
  230. uint32_t devt_pages;
  231. unsigned int devt_pagsiz;
  232. unsigned int devt_offs;
  233. uint8_t devt_srmsk;
  234. uint8_t devt_srval;
  235. } AT45_DEVTAB;
  236. /*!
  237. * \brief Active device entry.
  238. */
  239. typedef struct _AT45DB_DCB {
  240. AT45_DEVTAB *dcb_devt;
  241. unsigned int dcb_spibas;
  242. unsigned int dcb_spipcs;
  243. uint8_t dcb_cmdbuf[MAX_AT45_CMDLEN];
  244. } AT45DB_DCB;
  245. /*!
  246. * \brief Table of known Dataflash types.
  247. */
  248. AT45_DEVTAB at45_devt[] = {
  249. {512, 264, 9, 0x3C, 0x0C}, /* AT45DB011B - 128kB */
  250. {1025, 264, 9, 0x3C, 0x14}, /* AT45DB021B - 256kB */
  251. {2048, 264, 9, 0x3C, 0x1C}, /* AT45DB041B - 512kB */
  252. {4096, 264, 9, 0x3C, 0x24}, /* AT45DB081B - 1MB */
  253. {4096, 528, 10, 0x3C, 0x2C}, /* AT45DB0161B - 2MB */
  254. {8192, 528, 10, 0x3C, 0x34}, /* AT45DB0321B - 4MB */
  255. {8192, 1056, 11, 0x38, 0x38}, /* AT45DB0642 - 8MB */
  256. {0, 0, 0, 0, 0} /* End of table */
  257. };
  258. /*!
  259. * \brief Table of active devices.
  260. */
  261. static AT45DB_DCB dcbtab[MAX_AT45_DEVICES];
  262. /* Number of active chips. */
  263. static uint_least8_t dcbnum;
  264. /* Chip used for parameter storage. */
  265. static int dd_param = -1;
  266. /*!
  267. * \brief Send DataFlash command.
  268. *
  269. * \param dd Device descriptor.
  270. * \param op Command operation code.
  271. * \param parm Command parameter.
  272. * \param len Command length.
  273. * \param tdata Transmit data.
  274. * \param rdata Receive data buffer.
  275. * \param datalen Data length.
  276. */
  277. int At45dbSendCmd(int dd, uint8_t op, uint32_t parm, int len, const void *tdata, void *rdata, int datalen)
  278. {
  279. uint8_t *cb = dcbtab[dd].dcb_cmdbuf;
  280. if (len > MAX_AT45_CMDLEN) {
  281. return -1;
  282. }
  283. memset(cb, 0, len);
  284. cb[0] = op;
  285. if (parm) {
  286. cb[1] = (uint8_t) (parm >> 16);
  287. cb[2] = (uint8_t) (parm >> 8);
  288. cb[3] = (uint8_t) parm;
  289. }
  290. return At91SpiTransfer2(dcbtab[dd].dcb_spibas, dcbtab[dd].dcb_spipcs, cb, cb, len, tdata, rdata, datalen);
  291. }
  292. uint8_t At45dbGetStatus(int dd)
  293. {
  294. uint8_t buf[2] = { DFCMD_READ_STATUS, 0xFF };
  295. if (At91SpiTransfer2(dcbtab[dd].dcb_spibas, dcbtab[dd].dcb_spipcs, buf, buf, 2, NULL, NULL, 0)) {
  296. return (uint8_t) - 1;
  297. }
  298. return buf[1];
  299. }
  300. /*!
  301. * \brief Wait until flash memory cycle finished.
  302. *
  303. * \return 0 on success or -1 in case of an error.
  304. */
  305. int At45dbWaitReady(int dd, uint32_t tmo, int poll)
  306. {
  307. uint8_t sr;
  308. while (((sr = At45dbGetStatus(dd)) & 0x80) == 0) {
  309. if (!poll) {
  310. NutSleep(1);
  311. }
  312. if (tmo-- == 0) {
  313. return -1;
  314. }
  315. }
  316. return 0;
  317. }
  318. /*!
  319. * \brief Initialize dataflash at specified interface and chip select.
  320. *
  321. * \param spibas Interface base address. For ARM MCUs this may be the
  322. * I/O base address of the hardware SPI.
  323. * \param spipcs Device chip select.
  324. *
  325. * \return Device descriptor or -1 in case of an error.
  326. */
  327. int At45dbInit(unsigned int spibas, unsigned int spipcs)
  328. {
  329. int dd = -1;
  330. uint8_t sr;
  331. uint_fast8_t i;
  332. for (i = 0; i < dcbnum; i++) {
  333. if (dcbtab[i].dcb_spibas == spibas && dcbtab[i].dcb_spipcs == spipcs) {
  334. return i;
  335. }
  336. }
  337. if (dcbnum >= MAX_AT45_DEVICES) {
  338. return -1;
  339. }
  340. #if defined(MCU_AT91SAM7S) || defined(MCU_AT91SAM7X) || defined(MCU_AT91SAM7SE) || defined(MCU_AT91SAM9260) || defined(MCU_AT91SAM9XE)
  341. At91SpiInit(spibas);
  342. At91SpiReset(spibas);
  343. At91SpiInitChipSelects(spibas, _BV(spipcs));
  344. At91SpiSetRate(spibas, spipcs, 1000000);
  345. At91SpiSetModeFlags(spibas, spipcs, SPIMF_MASTER | SPIMF_SCKIAHI | SPIMF_CAPRISE);
  346. #elif defined(MCU_AT91R40008)
  347. #endif
  348. dcbtab[dcbnum].dcb_spibas = spibas;
  349. dcbtab[dcbnum].dcb_spipcs = spipcs;
  350. sr = At45dbGetStatus(dcbnum);
  351. for (i = 0; at45_devt[i].devt_pages; i++) {
  352. if ((sr & at45_devt[i].devt_srmsk) == at45_devt[i].devt_srval) {
  353. dcbtab[dcbnum].dcb_devt = &at45_devt[i];
  354. dd = dcbnum++;
  355. break;
  356. }
  357. }
  358. return dd;
  359. }
  360. /*!
  361. * \brief Erase sector at the specified offset.
  362. */
  363. int At45dbPageErase(int dd, uint32_t pgn)
  364. {
  365. pgn <<= dcbtab[dd].dcb_devt->devt_offs;
  366. return At45dbSendCmd(dd, DFCMD_PAGE_ERASE, pgn, 4, NULL, NULL, 0);
  367. }
  368. /*!
  369. * \brief Erase entire flash memory chip.
  370. */
  371. int At45dbChipErase(void)
  372. {
  373. return -1;
  374. }
  375. /*!
  376. * \brief Read data from flash memory.
  377. *
  378. * \param dd Device descriptor.
  379. * \param pgn Page number to read, starting at 0.
  380. * \param data Points to a buffer that receives the data.
  381. * \param len Number of bytes to read.
  382. *
  383. * \return 0 on success or -1 in case of an error.
  384. */
  385. int At45dbPageRead(int dd, uint32_t pgn, void *data, unsigned int len)
  386. {
  387. pgn <<= dcbtab[dd].dcb_devt->devt_offs;
  388. return At45dbSendCmd(dd, DFCMD_CONT_READ, pgn, 8, data, data, len);
  389. }
  390. /*!
  391. * \brief Write data into flash memory.
  392. *
  393. * The related sector must have been erased before calling this function.
  394. *
  395. * \param dd Device descriptor.
  396. * \param pgn Start location within the chip, starting at 0.
  397. * \param data Points to a buffer that contains the bytes to be written.
  398. * \param len Number of bytes to write.
  399. *
  400. * \return 0 on success or -1 in case of an error.
  401. */
  402. int At45dbPageWrite(int dd, uint32_t pgn, const void *data, unsigned int len)
  403. {
  404. int rc = -1;
  405. void *rp;
  406. if ((rp = malloc(len)) != NULL) {
  407. /* Copy data to dataflash RAM buffer. */
  408. if (At45dbSendCmd(dd, DFCMD_BUF1_WRITE, 0, 4, data, rp, len) == 0) {
  409. /* Flash RAM buffer. */
  410. pgn <<= dcbtab[dd].dcb_devt->devt_offs;
  411. if (At45dbSendCmd(dd, DFCMD_BUF1_FLASH, pgn, 4, NULL, NULL, 0) == 0) {
  412. rc = At45dbWaitReady(dd, AT45_WRITE_POLLS, 1);
  413. }
  414. }
  415. free(rp);
  416. }
  417. return rc;
  418. }
  419. uint32_t At45dbPages(int dd)
  420. {
  421. return dcbtab[dd].dcb_devt->devt_pages;
  422. }
  423. unsigned int At45dbPageSize(int dd)
  424. {
  425. return dcbtab[dd].dcb_devt->devt_pagsiz;
  426. }
  427. uint32_t At45dbParamPage(void)
  428. {
  429. #ifdef AT45_CONF_PAGE
  430. return AT45_CONF_PAGE;
  431. #else
  432. return dcbtab[dd_param].dcb_devt->devt_pages - 1;
  433. #endif
  434. }
  435. /*!
  436. * \brief Get size of configuration area.
  437. *
  438. * A part of the DataFlash may be used to store configuration parameters,
  439. * like the network interface MAC address, the local hostname etc. The
  440. * size of this area may be configured by defining AT45_CONF_SIZE.
  441. * Otherwise one full page is used.
  442. *
  443. * \return The number of bytes available for configuration data. In case of
  444. * an error, -1 is returned.
  445. */
  446. int At45dbParamSize(void)
  447. {
  448. int rc;
  449. if (dd_param == -1 && (dd_param = At45dbInit(AT45_CONF_DFSPI, AT45_CONF_DFPCS)) == -1) {
  450. return -1;
  451. }
  452. #ifdef AT45_CONF_SIZE
  453. rc = AT45_CONF_SIZE;
  454. #else
  455. rc = dcbtab[dd_param].dcb_devt->devt_pagsiz;
  456. #endif
  457. return rc;
  458. }
  459. /*!
  460. * \brief Load configuration parameters from flash memory.
  461. *
  462. * \param pos Start location within configuration sector.
  463. * \param data Points to a buffer that receives the contents.
  464. * \param len Number of bytes to read.
  465. *
  466. * \return Always 0.
  467. */
  468. int At45dbParamRead(unsigned int pos, void *data, unsigned int len)
  469. {
  470. int rc = -1;
  471. uint8_t *buff;
  472. int csize = At45dbParamSize();
  473. uint32_t cpage = At45dbParamPage();
  474. /* Load the complete configuration area. */
  475. if (csize > len && (buff = malloc(csize)) != NULL) {
  476. rc = At45dbPageRead(dd_param, cpage, buff, csize);
  477. /* Copy requested contents to caller's buffer. */
  478. memcpy(data, buff + pos, len);
  479. free(buff);
  480. }
  481. return rc;
  482. }
  483. /*!
  484. * \brief Store configuration parameters in flash memory.
  485. *
  486. * \param pos Start location within configuration sector.
  487. * \param data Points to a buffer that contains the bytes to store.
  488. * \param len Number of bytes to store.
  489. *
  490. * \return 0 on success or -1 in case of an error.
  491. */
  492. int At45dbParamWrite(unsigned int pos, const void *data, unsigned int len)
  493. {
  494. int rc = -1;
  495. uint8_t *buff;
  496. int csize = At45dbParamSize();
  497. uint32_t cpage = At45dbParamPage();
  498. /* Load the complete configuration area. */
  499. if (csize > len && (buff = malloc(csize)) != NULL) {
  500. rc = At45dbPageRead(dd_param, cpage, buff, csize);
  501. /* Compare old with new contents. */
  502. if (memcmp(buff + pos, data, len)) {
  503. /* New contents differs. Copy it into the sector buffer. */
  504. memcpy(buff + pos, data, len);
  505. /* Erase sector and write new data. */
  506. rc = At45dbPageWrite(dd_param, cpage, buff, csize);
  507. }
  508. free(buff);
  509. }
  510. return rc;
  511. }