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