nvmem.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  1. /*
  2. * Copyright (C) 2005-2007 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. */
  35. /*
  36. * $Id: nvmem.c 2935 2010-04-01 12:14:17Z haraldkipp $
  37. */
  38. #include "utils.h"
  39. #include "twbbi.h"
  40. #include "npl.h"
  41. #include "nvmem.h"
  42. #ifndef EEPROM_PAGE_SIZE
  43. #define EEPROM_PAGE_SIZE 64
  44. #endif
  45. #ifndef I2C_SLA_RTC
  46. #define I2C_SLA_RTC 0x6F
  47. #endif
  48. #ifndef I2C_SLA_EEPROM
  49. #define I2C_SLA_EEPROM 0x57
  50. #endif
  51. #ifndef I2C_SLA_PLL
  52. #define I2C_SLA_PLL 0x69
  53. #endif
  54. #define DFCMD_BUF1_FLASH 0x83
  55. #define DFCMD_BUF1_WRITE 0x84
  56. #define DFCMD_READ_STATUS 0xD7
  57. #define DFCMD_CONT_READ 0xE8
  58. #define SPI_WRITE 1
  59. #define SPI_READ 2
  60. static int at45d_avail;
  61. #ifndef NVMEM_BUFF_SIZE
  62. #ifdef AT45D_CONF
  63. #define NVMEM_BUFF_SIZE 528
  64. #else
  65. #define NVMEM_BUFF_SIZE 128
  66. #endif
  67. #endif
  68. static unsigned char page_buf[NVMEM_BUFF_SIZE];
  69. #ifdef AT45D_CONF
  70. static int NplSpiTransfer(int dir, unsigned char *ptr, int len)
  71. {
  72. int cnt = 20000;
  73. unsigned char b;
  74. while (len--) {
  75. outb(NPL_MMCDR, *ptr);
  76. while ((inb(NPL_SLR) & NPL_MMCREADY) == 0) {
  77. if (cnt-- <= 0) {
  78. return -1;
  79. }
  80. }
  81. b = inb(NPL_MMCDR);
  82. if (dir & SPI_READ) {
  83. *ptr = b;
  84. }
  85. ptr++;
  86. }
  87. return 0;
  88. }
  89. static unsigned char At45dStatus(void)
  90. {
  91. unsigned char cmd[2] = { DFCMD_READ_STATUS, 0xFF };
  92. outb(NPL_XER, inb(NPL_XER) & ~NPL_NPCS0);
  93. NplSpiTransfer(SPI_WRITE | SPI_READ, cmd, 2);
  94. outb(NPL_XER, inb(NPL_XER) | NPL_NPCS0);
  95. return cmd[1];
  96. }
  97. static int At45dCommand(unsigned char op, unsigned long parm, int oplen, unsigned char *xptr, int xlen)
  98. {
  99. int rc = -1;
  100. unsigned char cmd[8];
  101. memset_(cmd, 0, oplen);
  102. cmd[0] = op;
  103. if (parm) {
  104. cmd[1] = (unsigned char) (parm >> 16);
  105. cmd[2] = (unsigned char) (parm >> 8);
  106. cmd[3] = (unsigned char) parm;
  107. }
  108. outb(NPL_XER, inb(NPL_XER) & ~NPL_NPCS0);
  109. rc = NplSpiTransfer(SPI_WRITE, cmd, oplen);
  110. if (rc == 0) {
  111. if (xlen > 0) {
  112. rc = NplSpiTransfer(SPI_WRITE, xptr, xlen);
  113. } else if (xlen < 0) {
  114. rc = NplSpiTransfer(SPI_READ, xptr, -xlen);
  115. }
  116. }
  117. outb(NPL_XER, inb(NPL_XER) | NPL_NPCS0);
  118. return rc;
  119. }
  120. #else
  121. static int NvMemWriteEnable(int on)
  122. {
  123. int rc;
  124. unsigned char buf[3];
  125. buf[0] = 0;
  126. buf[1] = 0x3F;
  127. if (on) {
  128. buf[2] = 0x02;
  129. if ((rc = TwMasterTransact(I2C_SLA_RTC, buf, 3, 0, 0, 0)) == 0) {
  130. buf[2] = 0x06;
  131. rc = TwMasterTransact(I2C_SLA_RTC, buf, 3, 0, 0, 0);
  132. }
  133. } else {
  134. buf[2] = 0x00;
  135. rc = TwMasterTransact(I2C_SLA_RTC, buf, 3, 0, 0, 0);
  136. }
  137. return rc;
  138. }
  139. static int NvMemWaitReady(void)
  140. {
  141. unsigned char poll;
  142. int cnt = 20;
  143. /* Poll for write cycle finished. */
  144. while (--cnt && TwMasterTransact(I2C_SLA_EEPROM, 0, 0, &poll, 1, 0) == -1) {
  145. Delay(1);
  146. }
  147. return cnt ? 0 : -1;
  148. }
  149. #endif
  150. /*!
  151. * \brief Read contents from non-volatile EEPROM.
  152. *
  153. * \param addr Start location.
  154. * \param buff Points to a buffer that receives the contents.
  155. * \param len Number of bytes to read.
  156. *
  157. * \return 0 on success or -1 in case of an error.
  158. */
  159. int NvMemRead(unsigned int addr, void *buff, unsigned int len)
  160. {
  161. int rc = -1;
  162. #ifdef AT45D_CONF
  163. if (at45d_avail) {
  164. memcpy_(buff, &page_buf[addr], len);
  165. rc = 0;
  166. }
  167. #else
  168. unsigned char param[2];
  169. param[0] = (unsigned char) (addr >> 8);
  170. param[1] = (unsigned char) addr;
  171. if (TwMasterTransact(I2C_SLA_EEPROM, param, 2, buff, len, 0) == len) {
  172. rc = 0;
  173. }
  174. #endif
  175. return rc;
  176. }
  177. /*!
  178. * \brief Store buffer contents in non-volatile EEPROM.
  179. *
  180. * The EEPROM of the X122x has a capacity of 512 bytes, while the X1286 is
  181. * able to store 32 kBytes.
  182. *
  183. * \param addr Storage start location.
  184. * \param buff Points to a buffer that contains the bytes to store.
  185. * \param len Number of valid bytes in the buffer.
  186. *
  187. * \return 0 on success or -1 in case of an error.
  188. */
  189. int NvMemWrite(unsigned int addr, void *buff, unsigned int len)
  190. {
  191. int rc = 0;
  192. #ifdef AT45D_CONF
  193. if (at45d_avail) {
  194. memcpy_(&page_buf[addr], buff, len);
  195. if (At45dCommand(DFCMD_BUF1_WRITE, 0, 4, page_buf, sizeof(page_buf))) {
  196. rc = -1;
  197. }
  198. else if (At45dCommand(DFCMD_BUF1_FLASH, 8191 << 10, 4, 0, 0)) {
  199. rc = -1;
  200. }
  201. Delay(10);
  202. } else {
  203. rc = -1;
  204. }
  205. #else
  206. unsigned int wlen;
  207. unsigned char *wp = buff;
  208. /*
  209. * Loop for each page to be written to.
  210. */
  211. while (len) {
  212. /* Do not cross page boundaries. */
  213. wlen = EEPROM_PAGE_SIZE - (addr & (EEPROM_PAGE_SIZE - 1));
  214. if (wlen > len) {
  215. wlen = len;
  216. }
  217. /* Set a TWI write buffer. */
  218. page_buf[0] = (unsigned char) (addr >> 8);
  219. page_buf[1] = (unsigned char) addr;
  220. memcpy_(page_buf + 2, wp, wlen);
  221. /* Enable EEPROM write access and send the write buffer. */
  222. if ((rc = NvMemWriteEnable(1)) == 0) {
  223. rc = TwMasterTransact(I2C_SLA_EEPROM, page_buf, wlen + 2, 0, 0, 0);
  224. }
  225. /* Check the result. */
  226. if (rc) {
  227. break;
  228. }
  229. len -= wlen;
  230. addr += wlen;
  231. wp += wlen;
  232. /* Poll for write cycle finished. */
  233. if ((rc = NvMemWaitReady()) != 0) {
  234. break;
  235. }
  236. }
  237. NvMemWriteEnable(0);
  238. #endif
  239. return rc;
  240. }
  241. /*!
  242. * \brief Initialize the interface to serial EEPROM.
  243. */
  244. void NvMemInit(void)
  245. {
  246. TwInit();
  247. #ifdef AT45D_CONF
  248. {
  249. unsigned char reg[2];
  250. reg[0] = 0x09;
  251. if (TwMasterTransact(I2C_SLA_PLL, reg, 1, &reg[1], 1, 0) == 1) {
  252. reg[1] &= ~0x7F;
  253. reg[1] |= 2;
  254. TwMasterTransact(I2C_SLA_PLL, reg, 2, 0, 0, 0);
  255. }
  256. reg[0] = 0x08;
  257. if (TwMasterTransact(I2C_SLA_PLL, reg, 1, &reg[1], 1, 0) == 1) {
  258. reg[1] &= ~0x7F;
  259. reg[1] |= 2;
  260. TwMasterTransact(I2C_SLA_PLL, reg, 2, 0, 0, 0);
  261. }
  262. /* AT45DB0321B is supported only. */
  263. if ((At45dStatus() & 0x3D) == 0x34) {
  264. if (At45dCommand(DFCMD_CONT_READ, 8191 << 10, 8, page_buf, -((int)sizeof(page_buf))) == 0) {
  265. at45d_avail = 1;
  266. }
  267. }
  268. }
  269. #endif
  270. }