wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 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 | |
| 25 | #define DEBUG |
| 26 | |
| 27 | #include <common.h> |
| 28 | #include <exports.h> |
Peter Tyser | 6294850 | 2008-11-03 09:30:59 -0600 | [diff] [blame] | 29 | #include <timestamp.h> |
Jean-Christophe PLAGNIOL-VILLARD | ed0ea1f | 2007-11-21 21:19:24 +0100 | [diff] [blame] | 30 | #include "../drivers/net/smc91111.h" |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 31 | |
| 32 | #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE |
| 33 | |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 34 | static u16 read_eeprom_reg(u16 reg) |
| 35 | { |
| 36 | int timeout; |
| 37 | |
| 38 | SMC_SELECT_BANK(2); |
| 39 | SMC_outw(reg, PTR_REG); |
| 40 | |
| 41 | SMC_SELECT_BANK(1); |
| 42 | SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD, |
| 43 | CTL_REG); |
| 44 | timeout = 100; |
| 45 | while((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout) |
| 46 | udelay(100); |
| 47 | if (timeout == 0) { |
| 48 | printf("Timeout Reading EEPROM register %02x\n", reg); |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | return SMC_inw (GP_REG); |
| 53 | } |
| 54 | |
| 55 | static int write_eeprom_reg(u16 value, u16 reg) |
| 56 | { |
| 57 | int timeout; |
| 58 | |
| 59 | SMC_SELECT_BANK(2); |
| 60 | SMC_outw(reg, PTR_REG); |
| 61 | |
| 62 | SMC_SELECT_BANK(1); |
| 63 | SMC_outw(value, GP_REG); |
| 64 | SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG); |
| 65 | timeout = 100; |
| 66 | while ((SMC_inw(CTL_REG) & CTL_STORE) && --timeout) |
| 67 | udelay (100); |
| 68 | if (timeout == 0) { |
| 69 | printf("Timeout Writing EEPROM register %02x\n", reg); |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | return 1; |
| 74 | } |
| 75 | |
Wolfgang Denk | dbb564b | 2005-08-12 22:56:51 +0200 | [diff] [blame] | 76 | static int write_data(u16 *buf, int len) |
| 77 | { |
| 78 | u16 reg = 0x23; |
| 79 | |
| 80 | while (len--) |
| 81 | write_eeprom_reg(*buf++, reg++); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 86 | static int verify_macaddr(char *s) |
| 87 | { |
| 88 | u16 reg; |
| 89 | int i, err = 0; |
| 90 | |
Wolfgang Denk | dbb564b | 2005-08-12 22:56:51 +0200 | [diff] [blame] | 91 | printf("MAC Address: "); |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 92 | err = i = 0; |
| 93 | for (i = 0; i < 3; i++) { |
| 94 | reg = read_eeprom_reg(0x20 + i); |
| 95 | printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n'); |
Wolfgang Denk | dbb564b | 2005-08-12 22:56:51 +0200 | [diff] [blame] | 96 | if (s) |
| 97 | err |= reg != ((u16 *)s)[i]; |
wdenk | e085e5b | 2005-04-05 23:32:21 +0000 | [diff] [blame] | 98 | } |
| 99 | |
| 100 | return err ? 0 : 1; |
| 101 | } |
| 102 | |
| 103 | static int set_mac(char *s) |
| 104 | { |
| 105 | int i; |
| 106 | char *e, eaddr[6]; |
| 107 | |
| 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 | |
| 114 | for (i = 0; i < 3; i++) |
| 115 | write_eeprom_reg(*(((u16 *)eaddr) + i), 0x20 + i); |
| 116 | |
| 117 | return 0; |
| 118 | } |
Wolfgang Denk | dbb564b | 2005-08-12 22:56:51 +0200 | [diff] [blame] | 119 | |
| 120 | static int parse_element(char *s, unsigned char *buf, int len) |
| 121 | { |
| 122 | int cnt; |
| 123 | char *p, num[3]; |
| 124 | unsigned char id; |
| 125 | |
| 126 | id = simple_strtoul(s, &p, 16); |
| 127 | if (*p++ != ':') |
| 128 | return -1; |
| 129 | cnt = 2; |
| 130 | num[2] = 0; |
| 131 | for (; *p; p += 2) { |
| 132 | if (p[1] == 0) |
| 133 | return -2; |
| 134 | if (cnt + 3 > len) |
| 135 | return -3; |
| 136 | num[0] = p[0]; |
| 137 | num[1] = p[1]; |
| 138 | buf[cnt++] = simple_strtoul(num, NULL, 16); |
| 139 | } |
| 140 | buf[0] = id; |
| 141 | buf[1] = cnt - 2; |
| 142 | |
| 143 | return cnt; |
| 144 | } |
| 145 | |
| 146 | int eeprom(int argc, char *argv[]) |
| 147 | { |
| 148 | int i, len, ret; |
| 149 | unsigned char buf[58], *p; |
Wolfgang Denk | 30b8732 | 2005-08-12 23:43:12 +0200 | [diff] [blame] | 150 | |
Wolfgang Denk | dbb564b | 2005-08-12 22:56:51 +0200 | [diff] [blame] | 151 | app_startup(argv); |
| 152 | if (get_version() != XF_VERSION) { |
| 153 | printf("Wrong XF_VERSION.\n"); |
| 154 | printf("Application expects ABI version %d\n", XF_VERSION); |
| 155 | printf("Actual U-Boot ABI version %d\n", (int)get_version()); |
| 156 | return 1; |
| 157 | } |
| 158 | |
| 159 | if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) { |
| 160 | printf("SMSC91111 not found.\n"); |
| 161 | return 2; |
| 162 | } |
| 163 | |
| 164 | /* Called without parameters - print MAC address */ |
| 165 | if (argc < 2) { |
| 166 | verify_macaddr(NULL); |
| 167 | return 0; |
| 168 | } |
| 169 | |
| 170 | /* Print help message */ |
| 171 | if (argv[1][1] == 'h') { |
| 172 | printf("VoiceBlue EEPROM writer\n"); |
Peter Tyser | 6294850 | 2008-11-03 09:30:59 -0600 | [diff] [blame] | 173 | printf("Built: %s at %s\n", U_BOOT_DATE, U_BOOT_TIME); |
Wolfgang Denk | dbb564b | 2005-08-12 22:56:51 +0200 | [diff] [blame] | 174 | printf("Usage:\n\t<mac_address> [<element_1>] [<...>]\n"); |
| 175 | return 0; |
| 176 | } |
| 177 | |
| 178 | /* Try to parse information elements */ |
| 179 | len = sizeof(buf); |
| 180 | p = buf; |
| 181 | for (i = 2; i < argc; i++) { |
| 182 | ret = parse_element(argv[i], p, len); |
| 183 | switch (ret) { |
| 184 | case -1: |
| 185 | printf("Element %d: malformed\n", i - 1); |
| 186 | return 3; |
| 187 | case -2: |
| 188 | printf("Element %d: odd character count\n", i - 1); |
| 189 | return 3; |
| 190 | case -3: |
| 191 | printf("Out of EEPROM memory\n"); |
| 192 | return 3; |
| 193 | default: |
| 194 | p += ret; |
| 195 | len -= ret; |
| 196 | } |
| 197 | } |
| 198 | |
| 199 | /* First argument (MAC) is mandatory */ |
| 200 | set_mac(argv[1]); |
| 201 | if (verify_macaddr(argv[1])) { |
| 202 | printf("*** MAC address does not match! ***\n"); |
| 203 | return 4; |
| 204 | } |
| 205 | |
| 206 | while (len--) |
| 207 | *p++ = 0; |
| 208 | |
| 209 | write_data((u16 *)buf, sizeof(buf) >> 1); |
| 210 | |
| 211 | return 0; |
| 212 | } |