at49bv.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. /*
  2. * Copyright (C) 2005-2006 by egnite Software GmbH. All rights reserved.
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  21. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*!
  34. * \file dev/at49bv.c
  35. * \brief Routines for Atmel AT49 flash memory chips.
  36. *
  37. * \verbatim
  38. *
  39. * $Log$
  40. * Revision 1.6 2009/01/17 11:26:46 haraldkipp
  41. * Getting rid of two remaining BSD types in favor of stdint.
  42. * Replaced 'u_int' by 'unsinged int' and 'uptr_t' by 'uintptr_t'.
  43. *
  44. * Revision 1.5 2008/08/11 06:59:41 haraldkipp
  45. * BSD types replaced by stdint types (feature request #1282721).
  46. *
  47. * Revision 1.4 2008/07/17 11:51:18 olereinhardt
  48. * Added function AT49bvReadProtectionRegister to read the 64bit factory
  49. * or user id
  50. *
  51. * Revision 1.3 2006/10/08 16:48:09 haraldkipp
  52. * Documentation fixed
  53. *
  54. * Revision 1.2 2006/05/25 09:33:35 haraldkipp
  55. * Bugfix. At49bvParamWrite() returned an error when flash sector contents
  56. * was equal to the contents to write.
  57. *
  58. * Revision 1.1 2006/04/07 13:51:36 haraldkipp
  59. * AT49BV flash memory support added. A single sector may be used to
  60. * store system configurations in case there is no EEPROM available.
  61. *
  62. *
  63. * \endverbatim
  64. */
  65. #include <cfg/os.h>
  66. #include <cfg/memory.h>
  67. #include <sys/event.h>
  68. #include <sys/timer.h>
  69. #include <stdlib.h>
  70. #include <string.h>
  71. #include <dev/at49bv.h>
  72. /*! \brief Base address of the flash memory chip.
  73. */
  74. #ifndef FLASH_CHIP_BASE
  75. #define FLASH_CHIP_BASE 0x10000000
  76. #endif
  77. /*! \brief Address offset of the configuration sector.
  78. */
  79. #ifndef FLASH_CONF_SECTOR
  80. #define FLASH_CONF_SECTOR 0x6000
  81. #endif
  82. /*! \brief Size of the configuration area.
  83. *
  84. * During write operations a buffer with this size is allocated
  85. * from heap and may cause memory problems with large sectors.
  86. * Thus, this value may be less than the size of the configuration
  87. * sector, in which case the rest of the sector is unused.
  88. */
  89. #ifndef FLASH_CONF_SIZE
  90. #define FLASH_CONF_SIZE 512
  91. #endif
  92. #ifndef FLASH_ERASE_WAIT
  93. #define FLASH_ERASE_WAIT 3000
  94. #endif
  95. #ifndef FLASH_CHIP_ERASE_WAIT
  96. #define FLASH_CHIP_ERASE_WAIT 50000
  97. #endif
  98. #ifndef FLASH_WRITE_POLLS
  99. #define FLASH_WRITE_POLLS 1000
  100. #endif
  101. #ifdef FLASH_8BIT
  102. typedef unsigned char flashdat_t;
  103. #else
  104. typedef unsigned short flashdat_t;
  105. #endif
  106. typedef unsigned long flashadr_t;
  107. typedef volatile flashdat_t *flashptr_t;
  108. static flashptr_t chip = (flashptr_t) FLASH_CHIP_BASE;
  109. #define FLASH_UNLOCK(base) { \
  110. base[0x0555] = 0xAA; \
  111. base[0x0AAA] = 0x55; \
  112. }
  113. #define FLASH_COMMAND(base, cmd) { \
  114. base[0x0555] = cmd; \
  115. }
  116. #define FLASH_CMD_ERASE 0x80
  117. #define FLASH_CMD_ERASE_CHIP 0x10
  118. #define FLASH_CMD_ERASE_SECTOR 0x30
  119. #define FLASH_CMD_ENTER_ID 0x90
  120. #define FLASH_CMD_EXIT_ID 0xF0
  121. #define FLASH_CMD_PROGRAM 0xA0
  122. /*!
  123. * \brief Wait until flash memory cycle finished.
  124. *
  125. * \return 0 on success or -1 in case of an error.
  126. */
  127. static int At49bvWaitReady(flashptr_t addr, flashdat_t data, uint32_t tmo, int poll)
  128. {
  129. while (*addr != data) {
  130. if (!poll) {
  131. NutSleep(1);
  132. }
  133. if (tmo-- == 0) {
  134. return -1;
  135. }
  136. }
  137. return 0;
  138. }
  139. /*
  140. * May be later used to retrieve the chip layout.
  141. */
  142. unsigned long At49bvInit(void)
  143. {
  144. unsigned long id;
  145. FLASH_UNLOCK(chip);
  146. FLASH_COMMAND(chip, FLASH_CMD_ENTER_ID);
  147. id = chip[0];
  148. id <<= 16;
  149. id |= chip[1];
  150. FLASH_UNLOCK(chip);
  151. FLASH_COMMAND(chip, FLASH_CMD_EXIT_ID);
  152. return id;
  153. }
  154. /*!
  155. * \brief Read user or factory id from protection register.
  156. * \param factory If true, read factory id, otherwise read user id
  157. *
  158. * \return The ID read, 64 bit long!
  159. */
  160. unsigned long long AT49bvReadProtectionRegister(int factory)
  161. {
  162. unsigned long long id;
  163. FLASH_UNLOCK(chip);
  164. FLASH_COMMAND(chip, FLASH_CMD_ENTER_ID);
  165. if (factory) {
  166. id = chip[0x81];
  167. id <<= 16;
  168. id |= chip[0x82];
  169. id <<= 16;
  170. id |= chip[0x83];
  171. id <<= 16;
  172. id |= chip[0x84];
  173. } else {
  174. id = chip[0x85];
  175. id <<= 16;
  176. id |= chip[0x86];
  177. id <<= 16;
  178. id |= chip[0x87];
  179. id <<= 16;
  180. id |= chip[0x88];
  181. }
  182. FLASH_UNLOCK(chip);
  183. FLASH_COMMAND(chip, FLASH_CMD_EXIT_ID);
  184. return id;
  185. }
  186. /*!
  187. * \brief Erase sector at the specified offset.
  188. */
  189. int At49bvSectorErase(unsigned int off)
  190. {
  191. flashptr_t ptr = (flashptr_t) (uintptr_t) (FLASH_CHIP_BASE + off);
  192. FLASH_UNLOCK(chip);
  193. FLASH_COMMAND(chip, FLASH_CMD_ERASE);
  194. FLASH_UNLOCK(chip);
  195. *ptr = FLASH_CMD_ERASE_SECTOR;
  196. return At49bvWaitReady(ptr, (flashdat_t) - 1, FLASH_ERASE_WAIT, 0);
  197. }
  198. /*!
  199. * \brief Erase entire flash memory chip.
  200. */
  201. int At49bvChipErase(void)
  202. {
  203. FLASH_UNLOCK(chip);
  204. FLASH_COMMAND(chip, FLASH_CMD_ERASE);
  205. FLASH_UNLOCK(chip);
  206. FLASH_COMMAND(chip, FLASH_CMD_ERASE_CHIP);
  207. return At49bvWaitReady(chip, (flashdat_t) - 1, FLASH_CHIP_ERASE_WAIT, 0);
  208. }
  209. /*!
  210. * \brief Read data from flash memory.
  211. *
  212. * \param off Start location within the chip, starting at 0.
  213. * \param data Points to a buffer that receives the data.
  214. * \param len Number of bytes to read.
  215. *
  216. * \return 0 on success or -1 in case of an error.
  217. */
  218. int At49bvSectorRead(unsigned int off, void *data, unsigned int len)
  219. {
  220. memcpy(data, (void *) (uintptr_t) (FLASH_CHIP_BASE + off), len);
  221. return 0;
  222. }
  223. /*!
  224. * \brief Write data into flash memory.
  225. *
  226. * The related sector must have been erased before calling this function.
  227. *
  228. * \param off Start location within the chip, starting at 0.
  229. * \param data Points to a buffer that contains the bytes to be written.
  230. * \param len Number of bytes to write.
  231. *
  232. * \return 0 on success or -1 in case of an error.
  233. */
  234. int At49bvSectorWrite(unsigned int off, const void *data, unsigned int len)
  235. {
  236. int rc = 0;
  237. flashptr_t sp = (flashptr_t) data;
  238. flashptr_t dp = (flashptr_t) (uintptr_t) (FLASH_CHIP_BASE + off);
  239. unsigned int i;
  240. for (i = 0; i < len; i += sizeof(flashdat_t)) {
  241. /* No need to save values with all bits set. */
  242. if (*sp != (flashdat_t) - 1) {
  243. /* Write to memory location. */
  244. FLASH_UNLOCK(chip);
  245. FLASH_COMMAND(chip, FLASH_CMD_PROGRAM);
  246. *dp = *sp;
  247. if ((rc = At49bvWaitReady(dp, *sp, FLASH_WRITE_POLLS, 1)) != 0) {
  248. break;
  249. }
  250. }
  251. dp++;
  252. sp++;
  253. }
  254. return rc;
  255. }
  256. /*!
  257. * \brief Load configuration parameters from flash memory.
  258. *
  259. * \param pos Start location within configuration sector.
  260. * \param data Points to a buffer that receives the contents.
  261. * \param len Number of bytes to read.
  262. *
  263. * \return Always 0.
  264. */
  265. int At49bvParamRead(unsigned int pos, void *data, unsigned int len)
  266. {
  267. return At49bvSectorRead(FLASH_CONF_SECTOR + pos, data, len);
  268. }
  269. /*!
  270. * \brief Store configuration parameters in flash memory.
  271. *
  272. * \param pos Start location within configuration sector.
  273. * \param data Points to a buffer that contains the bytes to store.
  274. * \param len Number of bytes to store.
  275. *
  276. * \return 0 on success or -1 in case of an error.
  277. */
  278. int At49bvParamWrite(unsigned int pos, const void *data, unsigned int len)
  279. {
  280. int rc = -1;
  281. uint8_t *buff;
  282. /* Load the complete configuration area. */
  283. if ((buff = malloc(FLASH_CONF_SIZE)) != 0) {
  284. rc = At49bvSectorRead(FLASH_CONF_SECTOR, buff, FLASH_CONF_SIZE);
  285. /* Compare old with new contents. */
  286. if (memcmp(buff + pos, data, len)) {
  287. /* New contents differs. Copy it into the sector buffer. */
  288. memcpy(buff + pos, data, len);
  289. /* Erase sector and write new data. */
  290. if ((rc = At49bvSectorErase(FLASH_CONF_SECTOR)) == 0) {
  291. rc = At49bvSectorWrite(FLASH_CONF_SECTOR, buff, FLASH_CONF_SIZE);
  292. }
  293. }
  294. free(buff);
  295. }
  296. return rc;
  297. }