blob: adb01b96b89ce465e50ffc5357a2c188d8114b1e [file] [log] [blame]
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +02001/*
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 Denkac7eb8a32005-09-14 23:53:32 +020025#include <common.h>
26#include <exports.h>
Peter Tyser561858e2008-11-03 09:30:59 -060027#include <timestamp.h>
Ben Warren7194ab82009-10-04 22:37:03 -070028#include <net.h>
Jean-Christophe PLAGNIOL-VILLARD2439e4b2007-11-21 21:19:24 +010029#include "../drivers/net/smc91111.h"
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020030
Ben Warren7194ab82009-10-04 22:37:03 -070031static u16 read_eeprom_reg(struct eth_device *dev, u16 reg)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020032{
33 int timeout;
34
Ben Warren7194ab82009-10-04 22:37:03 -070035 SMC_SELECT_BANK(dev, 2);
36 SMC_outw(dev, reg, PTR_REG);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020037
Ben Warren7194ab82009-10-04 22:37:03 -070038 SMC_SELECT_BANK(dev, 1);
39 SMC_outw(dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020040 CTL_REG);
41 timeout = 100;
Ben Warren7194ab82009-10-04 22:37:03 -070042 while((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --timeout)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020043 udelay(100);
44 if (timeout == 0) {
45 printf("Timeout Reading EEPROM register %02x\n", reg);
46 return 0;
47 }
48
Ben Warren7194ab82009-10-04 22:37:03 -070049 return SMC_inw (dev, GP_REG);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020050}
51
Ben Warren7194ab82009-10-04 22:37:03 -070052static int write_eeprom_reg(struct eth_device *dev, u16 value, u16 reg)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020053{
54 int timeout;
55
Ben Warren7194ab82009-10-04 22:37:03 -070056 SMC_SELECT_BANK(dev, 2);
57 SMC_outw(dev, reg, PTR_REG);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020058
Ben Warren7194ab82009-10-04 22:37:03 -070059 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 Denkac7eb8a32005-09-14 23:53:32 +020062 timeout = 100;
Ben Warren7194ab82009-10-04 22:37:03 -070063 while ((SMC_inw(dev, CTL_REG) & CTL_STORE) && --timeout)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020064 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 Warren7194ab82009-10-04 22:37:03 -070073static int write_data(struct eth_device *dev, u16 *buf, int len)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020074{
75 u16 reg = 0x23;
76
77 while (len--)
Ben Warren7194ab82009-10-04 22:37:03 -070078 write_eeprom_reg(dev, *buf++, reg++);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020079
80 return 0;
81}
82
Ben Warren7194ab82009-10-04 22:37:03 -070083static int verify_macaddr(struct eth_device *dev, char *s)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020084{
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 Warren7194ab82009-10-04 22:37:03 -070091 reg = read_eeprom_reg(dev, 0x20 + i);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020092 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 Warren7194ab82009-10-04 22:37:03 -0700100static int set_mac(struct eth_device *dev, char *s)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200101{
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 Warren7194ab82009-10-04 22:37:03 -0700112 write_eeprom_reg(dev, *(((u16 *)eaddr) + i), 0x20 + i);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200113
114 return 0;
115}
116
117static 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 Denkac7eb8a32005-09-14 23:53:32 +0200143int eeprom(int argc, char *argv[])
144{
145 int i, len, ret;
146 unsigned char buf[58], *p;
147
Ben Warren7194ab82009-10-04 22:37:03 -0700148 struct eth_device dev = {
149 .iobase = CONFIG_SMC91111_BASE
150 };
151
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200152 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 Warren7194ab82009-10-04 22:37:03 -0700160 if ((SMC_inw (&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200161 printf("SMSC91111 not found.\n");
162 return 2;
163 }
164
165 /* Called without parameters - print MAC address */
166 if (argc < 2) {
Ben Warren7194ab82009-10-04 22:37:03 -0700167 verify_macaddr(&dev, NULL);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200168 return 0;
169 }
170
171 /* Print help message */
172 if (argv[1][1] == 'h') {
Ladislav Michld798e272010-01-27 11:12:28 -0500173 printf("NetStar EEPROM writer\n");
Peter Tyser561858e2008-11-03 09:30:59 -0600174 printf("Built: %s at %s\n", U_BOOT_DATE, U_BOOT_TIME);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200175 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 Warren7194ab82009-10-04 22:37:03 -0700201 set_mac(&dev, argv[1]);
202 if (verify_macaddr(&dev, argv[1])) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200203 printf("*** MAC address does not match! ***\n");
204 return 4;
205 }
206
207 while (len--)
208 *p++ = 0;
209
Ben Warren7194ab82009-10-04 22:37:03 -0700210 write_data(&dev, (u16 *)buf, sizeof(buf) >> 1);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200211
212 return 0;
213}