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. #include <asm/io.h>
  27. #define EEPROM_CAP 0x50000358
  28. #define EEPROM_DATA 0x5000035c
  29. unsigned int eepromReadLong(int offs)
  30. {
  31. unsigned int value;
  32. unsigned short ret;
  33. int count;
  34. out_be16((void *)EEPROM_CAP, offs);
  35. count = 0;
  36. for (;;)
  37. {
  38. count++;
  39. ret = in_be16((void *)EEPROM_CAP);
  40. if ((ret & 0x8000) != 0)
  41. break;
  42. }
  43. value = in_be32((void *)EEPROM_DATA);
  44. return value;
  45. }
  46. unsigned char eepromReadByte(int offs)
  47. {
  48. unsigned int valueLong;
  49. unsigned char *ptr;
  50. valueLong = eepromReadLong(offs & ~3);
  51. ptr = (unsigned char *)&valueLong;
  52. return ptr[offs & 3];
  53. }
  54. void eepromWriteLong(int offs, unsigned int value)
  55. {
  56. unsigned short ret;
  57. int count;
  58. count = 0;
  59. out_be32((void *)EEPROM_DATA, value);
  60. out_be16((void *)EEPROM_CAP, 0x8000 + offs);
  61. for (;;)
  62. {
  63. count++;
  64. ret = in_be16((void *)EEPROM_CAP);
  65. if ((ret & 0x8000) == 0)
  66. break;
  67. }
  68. }
  69. void eepromWriteByte(int offs, unsigned char valueByte)
  70. {
  71. unsigned int valueLong;
  72. unsigned char *ptr;
  73. valueLong = eepromReadLong(offs & ~3);
  74. ptr = (unsigned char *)&valueLong;
  75. ptr[offs & 3] = valueByte;
  76. eepromWriteLong(offs & ~3, valueLong);
  77. }
  78. void i2c_read (uchar *addr, int alen, uchar *buffer, int len)
  79. {
  80. int i;
  81. int len2, ptr;
  82. /* printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); /###* test-only */
  83. ptr = *(short *)addr;
  84. /*
  85. * Read till lword boundary
  86. */
  87. len2 = 4 - (*(short *)addr & 0x0003);
  88. for (i=0; i<len2; i++)
  89. {
  90. *buffer++ = eepromReadByte(ptr++);
  91. }
  92. /*
  93. * Read all lwords
  94. */
  95. len2 = (len - len2) >> 2;
  96. for (i=0; i<len2; i++)
  97. {
  98. *(unsigned int *)buffer = eepromReadLong(ptr);
  99. buffer += 4;
  100. ptr += 4;
  101. }
  102. /*
  103. * Read last bytes
  104. */
  105. len2 = (*(short *)addr + len) & 0x0003;
  106. for (i=0; i<len2; i++)
  107. {
  108. *buffer++ = eepromReadByte(ptr++);
  109. }
  110. }
  111. void i2c_write (uchar *addr, int alen, uchar *buffer, int len)
  112. {
  113. int i;
  114. int len2, ptr;
  115. /* printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); /###* test-only */
  116. ptr = *(short *)addr;
  117. /*
  118. * Write till lword boundary
  119. */
  120. len2 = 4 - (*(short *)addr & 0x0003);
  121. for (i=0; i<len2; i++)
  122. {
  123. eepromWriteByte(ptr++, *buffer++);
  124. }
  125. /*
  126. * Write all lwords
  127. */
  128. len2 = (len - len2) >> 2;
  129. for (i=0; i<len2; i++)
  130. {
  131. eepromWriteLong(ptr, *(unsigned int *)buffer);
  132. buffer += 4;
  133. ptr += 4;
  134. }
  135. /*
  136. * Write last bytes
  137. */
  138. len2 = (*(short *)addr + len) & 0x0003;
  139. for (i=0; i<len2; i++)
  140. {
  141. eepromWriteByte(ptr++, *buffer++);
  142. }
  143. }