blob: ad301eb9cf06e04ce0ce8a17026c1ba010c6c88b [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
Ladislav Michlb29ff622010-02-08 14:15:15 -050031static struct eth_device dev = {
32 .iobase = CONFIG_SMC91111_BASE
33};
34
35static u16 read_eeprom_reg(u16 reg)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020036{
37 int timeout;
38
Ladislav Michlb29ff622010-02-08 14:15:15 -050039 SMC_SELECT_BANK(&dev, 2);
40 SMC_outw(&dev, reg, PTR_REG);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020041
Ladislav Michlb29ff622010-02-08 14:15:15 -050042 SMC_SELECT_BANK(&dev, 1);
43 SMC_outw(&dev, SMC_inw(&dev, CTL_REG) | CTL_EEPROM_SELECT |
44 CTL_RELOAD, CTL_REG);
45
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020046 timeout = 100;
Ladislav Michlb29ff622010-02-08 14:15:15 -050047
48 while ((SMC_inw(&dev, CTL_REG) & CTL_RELOAD) && --timeout)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020049 udelay(100);
50 if (timeout == 0) {
51 printf("Timeout Reading EEPROM register %02x\n", reg);
52 return 0;
53 }
54
Ladislav Michlb29ff622010-02-08 14:15:15 -050055 return SMC_inw(&dev, GP_REG);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020056}
57
Ladislav Michlb29ff622010-02-08 14:15:15 -050058static int write_eeprom_reg(u16 value, u16 reg)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020059{
60 int timeout;
61
Ladislav Michlb29ff622010-02-08 14:15:15 -050062 SMC_SELECT_BANK(&dev, 2);
63 SMC_outw(&dev, reg, PTR_REG);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020064
Ladislav Michlb29ff622010-02-08 14:15:15 -050065 SMC_SELECT_BANK(&dev, 1);
66
67 SMC_outw(&dev, value, GP_REG);
68 SMC_outw(&dev, SMC_inw(&dev, CTL_REG) | CTL_EEPROM_SELECT |
69 CTL_STORE, CTL_REG);
70
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020071 timeout = 100;
Ladislav Michlb29ff622010-02-08 14:15:15 -050072
73 while ((SMC_inw(&dev, CTL_REG) & CTL_STORE) && --timeout)
74 udelay(100);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020075 if (timeout == 0) {
76 printf("Timeout Writing EEPROM register %02x\n", reg);
77 return 0;
78 }
79
80 return 1;
81}
82
Ladislav Michlb29ff622010-02-08 14:15:15 -050083static int write_data(u16 *buf, int len)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020084{
85 u16 reg = 0x23;
86
87 while (len--)
Ladislav Michlb29ff622010-02-08 14:15:15 -050088 write_eeprom_reg(*buf++, reg++);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020089
90 return 0;
91}
92
Ladislav Michlb29ff622010-02-08 14:15:15 -050093static int verify_macaddr(char *s)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +020094{
95 u16 reg;
96 int i, err = 0;
97
98 printf("MAC Address: ");
99 err = i = 0;
100 for (i = 0; i < 3; i++) {
Ladislav Michlb29ff622010-02-08 14:15:15 -0500101 reg = read_eeprom_reg(0x20 + i);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200102 printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
103 if (s)
104 err |= reg != ((u16 *)s)[i];
105 }
106
107 return err ? 0 : 1;
108}
109
Ladislav Michlb29ff622010-02-08 14:15:15 -0500110static int set_mac(char *s)
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200111{
112 int i;
113 char *e, eaddr[6];
114
115 /* turn string into mac value */
116 for (i = 0; i < 6; i++) {
117 eaddr[i] = simple_strtoul(s, &e, 16);
118 s = (*e) ? e+1 : e;
119 }
120
121 for (i = 0; i < 3; i++)
Ladislav Michlb29ff622010-02-08 14:15:15 -0500122 write_eeprom_reg(*(((u16 *)eaddr) + i), 0x20 + i);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200123
124 return 0;
125}
126
127static int parse_element(char *s, unsigned char *buf, int len)
128{
129 int cnt;
130 char *p, num[3];
131 unsigned char id;
132
133 id = simple_strtoul(s, &p, 16);
134 if (*p++ != ':')
135 return -1;
136 cnt = 2;
137 num[2] = 0;
138 for (; *p; p += 2) {
139 if (p[1] == 0)
140 return -2;
141 if (cnt + 3 > len)
142 return -3;
143 num[0] = p[0];
144 num[1] = p[1];
145 buf[cnt++] = simple_strtoul(num, NULL, 16);
146 }
147 buf[0] = id;
148 buf[1] = cnt - 2;
149
150 return cnt;
151}
152
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200153int eeprom(int argc, char *argv[])
154{
155 int i, len, ret;
156 unsigned char buf[58], *p;
157
158 app_startup(argv);
159 if (get_version() != XF_VERSION) {
160 printf("Wrong XF_VERSION.\n");
161 printf("Application expects ABI version %d\n", XF_VERSION);
162 printf("Actual U-Boot ABI version %d\n", (int)get_version());
163 return 1;
164 }
165
Ladislav Michlb29ff622010-02-08 14:15:15 -0500166 if ((SMC_inw(&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200167 printf("SMSC91111 not found.\n");
168 return 2;
169 }
170
171 /* Called without parameters - print MAC address */
172 if (argc < 2) {
Ladislav Michlb29ff622010-02-08 14:15:15 -0500173 verify_macaddr(NULL);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200174 return 0;
175 }
176
177 /* Print help message */
178 if (argv[1][1] == 'h') {
Ladislav Michld798e272010-01-27 11:12:28 -0500179 printf("NetStar EEPROM writer\n");
Peter Tyser561858e2008-11-03 09:30:59 -0600180 printf("Built: %s at %s\n", U_BOOT_DATE, U_BOOT_TIME);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200181 printf("Usage:\n\t<mac_address> [<element_1>] [<...>]\n");
182 return 0;
183 }
184
185 /* Try to parse information elements */
186 len = sizeof(buf);
187 p = buf;
188 for (i = 2; i < argc; i++) {
189 ret = parse_element(argv[i], p, len);
190 switch (ret) {
191 case -1:
192 printf("Element %d: malformed\n", i - 1);
193 return 3;
194 case -2:
195 printf("Element %d: odd character count\n", i - 1);
196 return 3;
197 case -3:
198 printf("Out of EEPROM memory\n");
199 return 3;
200 default:
201 p += ret;
202 len -= ret;
203 }
204 }
205
206 /* First argument (MAC) is mandatory */
Ladislav Michlb29ff622010-02-08 14:15:15 -0500207 set_mac(argv[1]);
208 if (verify_macaddr(argv[1])) {
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200209 printf("*** MAC address does not match! ***\n");
210 return 4;
211 }
212
213 while (len--)
214 *p++ = 0;
215
Ladislav Michlb29ff622010-02-08 14:15:15 -0500216 write_data((u16 *)buf, sizeof(buf) >> 1);
Wolfgang Denkac7eb8a32005-09-14 23:53:32 +0200217
218 return 0;
219}