eeprom.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 <timestamp.h>
  28. #include "../drivers/net/smc91111.h"
  29. #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
  30. static u16 read_eeprom_reg(u16 reg)
  31. {
  32. int timeout;
  33. SMC_SELECT_BANK(2);
  34. SMC_outw(reg, PTR_REG);
  35. SMC_SELECT_BANK(1);
  36. SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
  37. CTL_REG);
  38. timeout = 100;
  39. while((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout)
  40. udelay(100);
  41. if (timeout == 0) {
  42. printf("Timeout Reading EEPROM register %02x\n", reg);
  43. return 0;
  44. }
  45. return SMC_inw (GP_REG);
  46. }
  47. static int write_eeprom_reg(u16 value, u16 reg)
  48. {
  49. int timeout;
  50. SMC_SELECT_BANK(2);
  51. SMC_outw(reg, PTR_REG);
  52. SMC_SELECT_BANK(1);
  53. SMC_outw(value, GP_REG);
  54. SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
  55. timeout = 100;
  56. while ((SMC_inw(CTL_REG) & CTL_STORE) && --timeout)
  57. udelay (100);
  58. if (timeout == 0) {
  59. printf("Timeout Writing EEPROM register %02x\n", reg);
  60. return 0;
  61. }
  62. return 1;
  63. }
  64. static int write_data(u16 *buf, int len)
  65. {
  66. u16 reg = 0x23;
  67. while (len--)
  68. write_eeprom_reg(*buf++, reg++);
  69. return 0;
  70. }
  71. static int verify_macaddr(char *s)
  72. {
  73. u16 reg;
  74. int i, err = 0;
  75. printf("MAC Address: ");
  76. err = i = 0;
  77. for (i = 0; i < 3; i++) {
  78. reg = read_eeprom_reg(0x20 + i);
  79. printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
  80. if (s)
  81. err |= reg != ((u16 *)s)[i];
  82. }
  83. return err ? 0 : 1;
  84. }
  85. static int set_mac(char *s)
  86. {
  87. int i;
  88. char *e, eaddr[6];
  89. /* turn string into mac value */
  90. for (i = 0; i < 6; i++) {
  91. eaddr[i] = simple_strtoul(s, &e, 16);
  92. s = (*e) ? e+1 : e;
  93. }
  94. for (i = 0; i < 3; i++)
  95. write_eeprom_reg(*(((u16 *)eaddr) + i), 0x20 + i);
  96. return 0;
  97. }
  98. static int parse_element(char *s, unsigned char *buf, int len)
  99. {
  100. int cnt;
  101. char *p, num[3];
  102. unsigned char id;
  103. id = simple_strtoul(s, &p, 16);
  104. if (*p++ != ':')
  105. return -1;
  106. cnt = 2;
  107. num[2] = 0;
  108. for (; *p; p += 2) {
  109. if (p[1] == 0)
  110. return -2;
  111. if (cnt + 3 > len)
  112. return -3;
  113. num[0] = p[0];
  114. num[1] = p[1];
  115. buf[cnt++] = simple_strtoul(num, NULL, 16);
  116. }
  117. buf[0] = id;
  118. buf[1] = cnt - 2;
  119. return cnt;
  120. }
  121. extern int crcek(void);
  122. int eeprom(int argc, char *argv[])
  123. {
  124. int i, len, ret;
  125. unsigned char buf[58], *p;
  126. app_startup(argv);
  127. if (get_version() != XF_VERSION) {
  128. printf("Wrong XF_VERSION.\n");
  129. printf("Application expects ABI version %d\n", XF_VERSION);
  130. printf("Actual U-Boot ABI version %d\n", (int)get_version());
  131. return 1;
  132. }
  133. return crcek();
  134. if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) {
  135. printf("SMSC91111 not found.\n");
  136. return 2;
  137. }
  138. /* Called without parameters - print MAC address */
  139. if (argc < 2) {
  140. verify_macaddr(NULL);
  141. return 0;
  142. }
  143. /* Print help message */
  144. if (argv[1][1] == 'h') {
  145. printf("VoiceBlue EEPROM writer\n");
  146. printf("Built: %s at %s\n", U_BOOT_DATE, U_BOOT_TIME);
  147. printf("Usage:\n\t<mac_address> [<element_1>] [<...>]\n");
  148. return 0;
  149. }
  150. /* Try to parse information elements */
  151. len = sizeof(buf);
  152. p = buf;
  153. for (i = 2; i < argc; i++) {
  154. ret = parse_element(argv[i], p, len);
  155. switch (ret) {
  156. case -1:
  157. printf("Element %d: malformed\n", i - 1);
  158. return 3;
  159. case -2:
  160. printf("Element %d: odd character count\n", i - 1);
  161. return 3;
  162. case -3:
  163. printf("Out of EEPROM memory\n");
  164. return 3;
  165. default:
  166. p += ret;
  167. len -= ret;
  168. }
  169. }
  170. /* First argument (MAC) is mandatory */
  171. set_mac(argv[1]);
  172. if (verify_macaddr(argv[1])) {
  173. printf("*** MAC address does not match! ***\n");
  174. return 4;
  175. }
  176. while (len--)
  177. *p++ = 0;
  178. write_data((u16 *)buf, sizeof(buf) >> 1);
  179. return 0;
  180. }