eeprom.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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 int verify_macaddr(char *);
  30. static int set_mac(char *);
  31. int eeprom(int argc, char *argv[])
  32. {
  33. app_startup(argv);
  34. if (get_version() != XF_VERSION) {
  35. printf("Wrong XF_VERSION.\n");
  36. printf("Application expects ABI version %d\n", XF_VERSION);
  37. printf("Actual U-Boot ABI version %d\n", (int)get_version());
  38. return 1;
  39. }
  40. if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) {
  41. printf("SMSC91111 not found.\n");
  42. return 2;
  43. }
  44. if (argc != 2) {
  45. printf("VoiceBlue EEPROM writer\n");
  46. printf("Built: %s at %s\n", __DATE__ , __TIME__ );
  47. printf("Usage:\n\t<mac_address>");
  48. return 3;
  49. }
  50. set_mac(argv[1]);
  51. if (verify_macaddr(argv[1])) {
  52. printf("*** ERROR ***\n");
  53. return 4;
  54. }
  55. return 0;
  56. }
  57. static u16 read_eeprom_reg(u16 reg)
  58. {
  59. int timeout;
  60. SMC_SELECT_BANK(2);
  61. SMC_outw(reg, PTR_REG);
  62. SMC_SELECT_BANK(1);
  63. SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
  64. CTL_REG);
  65. timeout = 100;
  66. while((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout)
  67. udelay(100);
  68. if (timeout == 0) {
  69. printf("Timeout Reading EEPROM register %02x\n", reg);
  70. return 0;
  71. }
  72. return SMC_inw (GP_REG);
  73. }
  74. static int write_eeprom_reg(u16 value, u16 reg)
  75. {
  76. int timeout;
  77. SMC_SELECT_BANK(2);
  78. SMC_outw(reg, PTR_REG);
  79. SMC_SELECT_BANK(1);
  80. SMC_outw(value, GP_REG);
  81. SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
  82. timeout = 100;
  83. while ((SMC_inw(CTL_REG) & CTL_STORE) && --timeout)
  84. udelay (100);
  85. if (timeout == 0) {
  86. printf("Timeout Writing EEPROM register %02x\n", reg);
  87. return 0;
  88. }
  89. return 1;
  90. }
  91. static int verify_macaddr(char *s)
  92. {
  93. u16 reg;
  94. int i, err = 0;
  95. printf("Verifying MAC Address: ");
  96. err = i = 0;
  97. for (i = 0; i < 3; i++) {
  98. reg = read_eeprom_reg(0x20 + i);
  99. printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
  100. err |= reg != ((u16 *)s)[i];
  101. }
  102. return err ? 0 : 1;
  103. }
  104. static int set_mac(char *s)
  105. {
  106. int i;
  107. char *e, eaddr[6];
  108. /* turn string into mac value */
  109. for (i = 0; i < 6; i++) {
  110. eaddr[i] = simple_strtoul(s, &e, 16);
  111. s = (*e) ? e+1 : e;
  112. }
  113. for (i = 0; i < 3; i++)
  114. write_eeprom_reg(*(((u16 *)eaddr) + i), 0x20 + i);
  115. return 0;
  116. }