eeprom.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 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 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. puts("HWaddr: ");
  79. for (i = 0; i < 3; i++) {
  80. reg = read_eeprom_reg(0x20 + i);
  81. printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
  82. if (s)
  83. err |= reg != ((u16 *)s)[i];
  84. }
  85. return err ? 0 : 1;
  86. }
  87. static int set_mac(char *s)
  88. {
  89. int i;
  90. char *e, eaddr[6];
  91. /* turn string into mac value */
  92. for (i = 0; i < 6; i++) {
  93. eaddr[i] = simple_strtoul(s, &e, 16);
  94. s = (*e) ? e+1 : e;
  95. }
  96. for (i = 0; i < 3; i++)
  97. write_eeprom_reg(*(((u16 *)eaddr) + i), 0x20 + i);
  98. return 0;
  99. }
  100. static int parse_element(char *s, unsigned char *buf, int len)
  101. {
  102. int cnt;
  103. char *p, num[3];
  104. unsigned char id;
  105. id = simple_strtoul(s, &p, 16);
  106. if (*p++ != ':')
  107. return -1;
  108. cnt = 2;
  109. num[2] = 0;
  110. for (; *p; p += 2) {
  111. if (p[1] == 0)
  112. return -2;
  113. if (cnt + 3 > len)
  114. return -3;
  115. num[0] = p[0];
  116. num[1] = p[1];
  117. buf[cnt++] = simple_strtoul(num, NULL, 16);
  118. }
  119. buf[0] = id;
  120. buf[1] = cnt - 2;
  121. return cnt;
  122. }
  123. int eeprom(int argc, char * const argv[])
  124. {
  125. int i, len, ret;
  126. unsigned char buf[58], *p;
  127. app_startup(argv);
  128. i = get_version();
  129. if (i != XF_VERSION) {
  130. printf("Using ABI version %d, but U-Boot provides %d\n",
  131. XF_VERSION, i);
  132. return 1;
  133. }
  134. if ((SMC_inw(&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
  135. puts("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. puts("NetStar EEPROM writer\n"
  146. "Built: " U_BOOT_DATE " at " U_BOOT_TIME "\n"
  147. "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. puts("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. puts("*** HWaddr 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. }