eeprom.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. /*
  2. * (C) Copyright 2001
  3. * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. *
  23. */
  24. #include <common.h>
  25. #include <command.h>
  26. #define EEPROM_CAP 0x50000358
  27. #define EEPROM_DATA 0x5000035c
  28. unsigned int eepromReadLong(int offs)
  29. {
  30. unsigned int value;
  31. volatile unsigned short ret;
  32. int count;
  33. *(unsigned short *)EEPROM_CAP = offs;
  34. count = 0;
  35. for (;;)
  36. {
  37. count++;
  38. ret = *(unsigned short *)EEPROM_CAP;
  39. if ((ret & 0x8000) != 0)
  40. break;
  41. }
  42. value = *(unsigned long *)EEPROM_DATA;
  43. return value;
  44. }
  45. unsigned char eepromReadByte(int offs)
  46. {
  47. unsigned int valueLong;
  48. unsigned char *ptr;
  49. valueLong = eepromReadLong(offs & ~3);
  50. ptr = (unsigned char *)&valueLong;
  51. return ptr[offs & 3];
  52. }
  53. void eepromWriteLong(int offs, unsigned int value)
  54. {
  55. volatile unsigned short ret;
  56. int count;
  57. count = 0;
  58. *(unsigned long *)EEPROM_DATA = value;
  59. *(unsigned short *)EEPROM_CAP = 0x8000 + offs;
  60. for (;;)
  61. {
  62. count++;
  63. ret = *(unsigned short *)EEPROM_CAP;
  64. if ((ret & 0x8000) == 0)
  65. break;
  66. }
  67. }
  68. void eepromWriteByte(int offs, unsigned char valueByte)
  69. {
  70. unsigned int valueLong;
  71. unsigned char *ptr;
  72. valueLong = eepromReadLong(offs & ~3);
  73. ptr = (unsigned char *)&valueLong;
  74. ptr[offs & 3] = valueByte;
  75. eepromWriteLong(offs & ~3, valueLong);
  76. }
  77. void i2c_read (uchar *addr, int alen, uchar *buffer, int len)
  78. {
  79. int i;
  80. int len2, ptr;
  81. /* printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); // test-only */
  82. ptr = *(short *)addr;
  83. /*
  84. * Read till lword boundary
  85. */
  86. len2 = 4 - (*(short *)addr & 0x0003);
  87. for (i=0; i<len2; i++)
  88. {
  89. *buffer++ = eepromReadByte(ptr++);
  90. }
  91. /*
  92. * Read all lwords
  93. */
  94. len2 = (len - len2) >> 2;
  95. for (i=0; i<len2; i++)
  96. {
  97. *(unsigned int *)buffer = eepromReadLong(ptr);
  98. buffer += 4;
  99. ptr += 4;
  100. }
  101. /*
  102. * Read last bytes
  103. */
  104. len2 = (*(short *)addr + len) & 0x0003;
  105. for (i=0; i<len2; i++)
  106. {
  107. *buffer++ = eepromReadByte(ptr++);
  108. }
  109. }
  110. void i2c_write (uchar *addr, int alen, uchar *buffer, int len)
  111. {
  112. int i;
  113. int len2, ptr;
  114. /* printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); // test-only */
  115. ptr = *(short *)addr;
  116. /*
  117. * Write till lword boundary
  118. */
  119. len2 = 4 - (*(short *)addr & 0x0003);
  120. for (i=0; i<len2; i++)
  121. {
  122. eepromWriteByte(ptr++, *buffer++);
  123. }
  124. /*
  125. * Write all lwords
  126. */
  127. len2 = (len - len2) >> 2;
  128. for (i=0; i<len2; i++)
  129. {
  130. eepromWriteLong(ptr, *(unsigned int *)buffer);
  131. buffer += 4;
  132. ptr += 4;
  133. }
  134. /*
  135. * Write last bytes
  136. */
  137. len2 = (*(short *)addr + len) & 0x0003;
  138. for (i=0; i<len2; i++)
  139. {
  140. eepromWriteByte(ptr++, *buffer++);
  141. }
  142. }