eeprom.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * (C) Copyright 2005
  3. * Ladislav Michl, 2N Telekomunikace, michl@2n.cz
  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 modify
  9. * it under the terms of the GNU General Public License version 2 as
  10. * published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  20. * MA 02111-1307 USA
  21. *
  22. * Some code shamelessly stolen back from Robin Getz.
  23. */
  24. #define DEBUG
  25. #include <common.h>
  26. #include <exports.h>
  27. #include "../drivers/smc91111.h"
  28. #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
  29. static u16 read_eeprom_reg(u16 reg)
  30. {
  31. int timeout;
  32. SMC_SELECT_BANK(2);
  33. SMC_outw(reg, PTR_REG);
  34. SMC_SELECT_BANK(1);
  35. SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
  36. CTL_REG);
  37. timeout = 100;
  38. while((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout)
  39. udelay(100);
  40. if (timeout == 0) {
  41. printf("Timeout Reading EEPROM register %02x\n", reg);
  42. return 0;
  43. }
  44. return SMC_inw (GP_REG);
  45. }
  46. static int write_eeprom_reg(u16 value, u16 reg)
  47. {
  48. int timeout;
  49. SMC_SELECT_BANK(2);
  50. SMC_outw(reg, PTR_REG);
  51. SMC_SELECT_BANK(1);
  52. SMC_outw(value, GP_REG);
  53. SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
  54. timeout = 100;
  55. while ((SMC_inw(CTL_REG) & CTL_STORE) && --timeout)
  56. udelay (100);
  57. if (timeout == 0) {
  58. printf("Timeout Writing EEPROM register %02x\n", reg);
  59. return 0;
  60. }
  61. return 1;
  62. }
  63. static int write_data(u16 *buf, int len)
  64. {
  65. u16 reg = 0x23;
  66. while (len--)
  67. write_eeprom_reg(*buf++, reg++);
  68. return 0;
  69. }
  70. static int verify_macaddr(char *s)
  71. {
  72. u16 reg;
  73. int i, err = 0;
  74. printf("MAC Address: ");
  75. err = i = 0;
  76. for (i = 0; i < 3; i++) {
  77. reg = read_eeprom_reg(0x20 + i);
  78. printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
  79. if (s)
  80. err |= reg != ((u16 *)s)[i];
  81. }
  82. return err ? 0 : 1;
  83. }
  84. static int set_mac(char *s)
  85. {
  86. int i;
  87. char *e, eaddr[6];
  88. /* turn string into mac value */
  89. for (i = 0; i < 6; i++) {
  90. eaddr[i] = simple_strtoul(s, &e, 16);
  91. s = (*e) ? e+1 : e;
  92. }
  93. for (i = 0; i < 3; i++)
  94. write_eeprom_reg(*(((u16 *)eaddr) + i), 0x20 + i);
  95. return 0;
  96. }
  97. static int parse_element(char *s, unsigned char *buf, int len)
  98. {
  99. int cnt;
  100. char *p, num[3];
  101. unsigned char id;
  102. id = simple_strtoul(s, &p, 16);
  103. if (*p++ != ':')
  104. return -1;
  105. cnt = 2;
  106. num[2] = 0;
  107. for (; *p; p += 2) {
  108. if (p[1] == 0)
  109. return -2;
  110. if (cnt + 3 > len)
  111. return -3;
  112. num[0] = p[0];
  113. num[1] = p[1];
  114. buf[cnt++] = simple_strtoul(num, NULL, 16);
  115. }
  116. buf[0] = id;
  117. buf[1] = cnt - 2;
  118. return cnt;
  119. }
  120. extern int crcek(void);
  121. int eeprom(int argc, char *argv[])
  122. {
  123. int i, len, ret;
  124. unsigned char buf[58], *p;
  125. app_startup(argv);
  126. if (get_version() != XF_VERSION) {
  127. printf("Wrong XF_VERSION.\n");
  128. printf("Application expects ABI version %d\n", XF_VERSION);
  129. printf("Actual U-Boot ABI version %d\n", (int)get_version());
  130. return 1;
  131. }
  132. return crcek();
  133. if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) {
  134. printf("SMSC91111 not found.\n");
  135. return 2;
  136. }
  137. /* Called without parameters - print MAC address */
  138. if (argc < 2) {
  139. verify_macaddr(NULL);
  140. return 0;
  141. }
  142. /* Print help message */
  143. if (argv[1][1] == 'h') {
  144. printf("VoiceBlue EEPROM writer\n");
  145. printf("Built: %s at %s\n", __DATE__ , __TIME__ );
  146. printf("Usage:\n\t<mac_address> [<element_1>] [<...>]\n");
  147. return 0;
  148. }
  149. /* Try to parse information elements */
  150. len = sizeof(buf);
  151. p = buf;
  152. for (i = 2; i < argc; i++) {
  153. ret = parse_element(argv[i], p, len);
  154. switch (ret) {
  155. case -1:
  156. printf("Element %d: malformed\n", i - 1);
  157. return 3;
  158. case -2:
  159. printf("Element %d: odd character count\n", i - 1);
  160. return 3;
  161. case -3:
  162. printf("Out of EEPROM memory\n");
  163. return 3;
  164. default:
  165. p += ret;
  166. len -= ret;
  167. }
  168. }
  169. /* First argument (MAC) is mandatory */
  170. set_mac(argv[1]);
  171. if (verify_macaddr(argv[1])) {
  172. printf("*** MAC address does not match! ***\n");
  173. return 4;
  174. }
  175. while (len--)
  176. *p++ = 0;
  177. write_data((u16 *)buf, sizeof(buf) >> 1);
  178. return 0;
  179. }