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