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