3 * Ladislav Michl, 2N Telekomunikace, michl@2n.cz
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
22 * Some code shamelessly stolen back from Robin Getz.
27 #include <timestamp.h>
29 #include "../drivers/net/smc91111.h"
31 static u16 read_eeprom_reg(struct eth_device *dev, u16 reg)
35 SMC_SELECT_BANK(dev, 2);
36 SMC_outw(dev, reg, PTR_REG);
38 SMC_SELECT_BANK(dev, 1);
39 SMC_outw(dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
42 while((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --timeout)
45 printf("Timeout Reading EEPROM register %02x\n", reg);
49 return SMC_inw (dev, GP_REG);
52 static int write_eeprom_reg(struct eth_device *dev, u16 value, u16 reg)
56 SMC_SELECT_BANK(dev, 2);
57 SMC_outw(dev, reg, PTR_REG);
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);
63 while ((SMC_inw(dev, CTL_REG) & CTL_STORE) && --timeout)
66 printf("Timeout Writing EEPROM register %02x\n", reg);
73 static int write_data(struct eth_device *dev, u16 *buf, int len)
78 write_eeprom_reg(dev, *buf++, reg++);
83 static int verify_macaddr(struct eth_device *dev, char *s)
88 printf("MAC Address: ");
90 for (i = 0; i < 3; i++) {
91 reg = read_eeprom_reg(dev, 0x20 + i);
92 printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
94 err |= reg != ((u16 *)s)[i];
100 static int set_mac(struct eth_device *dev, char *s)
105 /* turn string into mac value */
106 for (i = 0; i < 6; i++) {
107 eaddr[i] = simple_strtoul(s, &e, 16);
111 for (i = 0; i < 3; i++)
112 write_eeprom_reg(dev, *(((u16 *)eaddr) + i), 0x20 + i);
117 static int parse_element(char *s, unsigned char *buf, int len)
123 id = simple_strtoul(s, &p, 16);
135 buf[cnt++] = simple_strtoul(num, NULL, 16);
143 int eeprom(int argc, char *argv[])
146 unsigned char buf[58], *p;
148 struct eth_device dev = {
149 .iobase = CONFIG_SMC91111_BASE
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());
160 if ((SMC_inw (&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
161 printf("SMSC91111 not found.\n");
165 /* Called without parameters - print MAC address */
167 verify_macaddr(&dev, NULL);
171 /* Print help message */
172 if (argv[1][1] == 'h') {
173 printf("NetStar EEPROM writer\n");
174 printf("Built: %s at %s\n", U_BOOT_DATE, U_BOOT_TIME);
175 printf("Usage:\n\t<mac_address> [<element_1>] [<...>]\n");
179 /* Try to parse information elements */
182 for (i = 2; i < argc; i++) {
183 ret = parse_element(argv[i], p, len);
186 printf("Element %d: malformed\n", i - 1);
189 printf("Element %d: odd character count\n", i - 1);
192 printf("Out of EEPROM memory\n");
200 /* First argument (MAC) is mandatory */
201 set_mac(&dev, argv[1]);
202 if (verify_macaddr(&dev, argv[1])) {
203 printf("*** MAC address does not match! ***\n");
210 write_data(&dev, (u16 *)buf, sizeof(buf) >> 1);