eeprom.c 4.4 KB

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