1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <common.h>
18#include <exports.h>
19#include <linux/ctype.h>
20#include "../drivers/net/smc911x.h"
21
22
23
24
25static int smsc_ctrlc(void)
26{
27 return (tstc() && getc() == 0x03);
28}
29
30
31
32
33static void usage(void)
34{
35 puts(
36 "MAC/EEPROM Commands:\n"
37 " P : Print the MAC addresses\n"
38 " D : Dump the EEPROM contents\n"
39 " M : Dump the MAC contents\n"
40 " C : Copy the MAC address from the EEPROM to the MAC\n"
41 " W : Write a register in the EEPROM or in the MAC\n"
42 " Q : Quit\n"
43 "\n"
44 "Some commands take arguments:\n"
45 " W <E|M> <register> <value>\n"
46 " E: EEPROM M: MAC\n"
47 );
48}
49
50
51
52
53
54
55
56static void dump_regs(struct eth_device *dev)
57{
58 u8 i, j = 0;
59 for (i = 0x50; i < 0xB8; i += sizeof(u32))
60 printf("%02x: 0x%08x %c", i,
61 smc911x_reg_read(dev, i),
62 (j++ % 2 ? '\n' : ' '));
63}
64
65
66
67
68static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
69{
70 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
71 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
72 smc911x_reg_read(dev, E2P_CMD));
73 return -1;
74 }
75
76 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
77
78 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
79 if (smsc_ctrlc()) {
80 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
81 smc911x_reg_read(dev, E2P_CMD));
82 return -1;
83 }
84
85 return 0;
86}
87
88
89
90
91static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
92{
93 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
94 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
95}
96
97
98
99
100static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
101{
102 int ret;
103
104
105 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
106 if (ret)
107 goto done;
108
109
110 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
111 if (ret)
112 goto done;
113
114
115 smc911x_reg_write(dev, E2P_DATA, value);
116 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
117 if (ret)
118 goto done;
119
120
121 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
122
123 done:
124 return ret;
125}
126
127
128
129
130static char *skip_space(char *buf)
131{
132 while (isblank(buf[0]))
133 ++buf;
134 return buf;
135}
136
137
138
139
140static void write_stuff(struct eth_device *dev, char *line)
141{
142 char dest;
143 char *endp;
144 u8 reg;
145 u32 value;
146
147
148 line = skip_space(line + 1);
149
150
151 switch (line[0]) {
152 case 'E':
153 case 'M':
154 dest = line[0];
155 break;
156 default:
157 invalid_usage:
158 printf("ERROR: Invalid write usage\n");
159 usage();
160 return;
161 }
162
163
164 line = skip_space(line + 1);
165 reg = simple_strtoul(line, &endp, 16);
166 if (line == endp)
167 goto invalid_usage;
168
169
170 line = skip_space(endp);
171 value = simple_strtoul(line, &endp, 16);
172 if (line == endp)
173 goto invalid_usage;
174
175
176 line = skip_space(endp);
177 if (line[0])
178 goto invalid_usage;
179
180
181 if (dest == 'E') {
182 printf("Writing EEPROM register %02x with %02x\n", reg, value);
183 write_eeprom_reg(dev, value, reg);
184 } else {
185 printf("Writing MAC register %02x with %08x\n", reg, value);
186 smc911x_reg_write(dev, reg, value);
187 }
188}
189
190
191
192
193static void copy_from_eeprom(struct eth_device *dev)
194{
195 ulong addrl =
196 read_eeprom_reg(dev, 0x01) |
197 read_eeprom_reg(dev, 0x02) << 8 |
198 read_eeprom_reg(dev, 0x03) << 16 |
199 read_eeprom_reg(dev, 0x04) << 24;
200 ulong addrh =
201 read_eeprom_reg(dev, 0x05) |
202 read_eeprom_reg(dev, 0x06) << 8;
203 smc911x_set_mac_csr(dev, ADDRL, addrl);
204 smc911x_set_mac_csr(dev, ADDRH, addrh);
205 puts("EEPROM contents copied to MAC\n");
206}
207
208
209
210
211static void print_macaddr(struct eth_device *dev)
212{
213 puts("Current MAC Address in MAC: ");
214 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
215 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
216 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
217 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
218 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
219
220 puts("Current MAC Address in EEPROM: ");
221 int i;
222 for (i = 1; i < 6; ++i)
223 printf("%02x:", read_eeprom_reg(dev, i));
224 printf("%02x\n", read_eeprom_reg(dev, i));
225}
226
227
228
229
230static void dump_eeprom(struct eth_device *dev)
231{
232 int i;
233 puts("EEPROM:\n");
234 for (i = 0; i < 7; ++i)
235 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
236}
237
238
239
240
241static int smc911x_init(struct eth_device *dev)
242{
243
244 if (smc911x_detect_chip(dev))
245 return 1;
246
247 smc911x_reset(dev);
248
249
250 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
251 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
252 if (smsc_ctrlc()) {
253 printf("init: timeout (E2P_CMD = 0x%08x)\n",
254 smc911x_reg_read(dev, E2P_CMD));
255 return 1;
256 }
257 smc911x_reg_write(dev, GPIO_CFG,
258 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
259 }
260
261 return 0;
262}
263
264
265
266
267static char *getline(void)
268{
269 static char buffer[100];
270 char c;
271 size_t i;
272
273 i = 0;
274 while (1) {
275 buffer[i] = '\0';
276 while (!tstc())
277 continue;
278
279 c = getc();
280
281 if (c >= 'a' && c <= 'z')
282 c -= ('a' - 'A');
283
284 switch (c) {
285 case '\r':
286 case '\n':
287 puts("\n");
288 return buffer;
289
290 case 0x03:
291 return NULL;
292
293 case 0x5F:
294 case 0x08:
295 case 0x7F:
296 if (i) {
297 puts("\b \b");
298 i--;
299 }
300 break;
301
302 default:
303
304 if (c < 0x20)
305 break;
306
307 buffer[i++] = c;
308 printf("%c", c);
309 break;
310 }
311 }
312}
313
314
315
316
317int smc911x_eeprom(int argc, char * const argv[])
318{
319
320 struct eth_device dev;
321 dev.iobase = CONFIG_SMC911X_BASE;
322
323
324 app_startup(argv);
325 if (XF_VERSION != get_version()) {
326 printf("Expects ABI version %d\n", XF_VERSION);
327 printf("Actual U-Boot ABI version %lu\n", get_version());
328 printf("Can't run\n\n");
329 return 1;
330 }
331
332
333 puts("\n");
334 if (smc911x_init(&dev))
335 return 1;
336
337
338 puts("\n");
339 usage();
340 puts("\n");
341
342 while (1) {
343 char *line;
344
345
346 puts("eeprom> ");
347 line = getline();
348
349
350 if (!line)
351 return 0;
352
353
354 line = skip_space(line);
355
356
357 if (!line[0])
358 continue;
359
360
361 if (line[0] && line[1] && !isblank(line[1]))
362 goto unknown_cmd;
363
364
365 switch (line[0]) {
366 case 'W': write_stuff(&dev, line); break;
367 case 'D': dump_eeprom(&dev); break;
368 case 'M': dump_regs(&dev); break;
369 case 'C': copy_from_eeprom(&dev); break;
370 case 'P': print_macaddr(&dev); break;
371 unknown_cmd:
372 default: puts("ERROR: Unknown command!\n\n");
373 case '?':
374 case 'H': usage(); break;
375 case 'Q': return 0;
376 }
377 }
378}
379