blob: 6c79c5f65a0f74236f46c1f0da26612d3705d435 [file] [log] [blame]
Mike Frysingerad2d1632008-12-22 02:56:07 -05001/*
2 * smc911x_eeprom.c - EEPROM interface to SMC911x parts.
3 * Only tested on SMSC9118 though ...
4 *
Mike Frysingerc44efcf2009-11-12 22:26:02 -05005 * Copyright 2004-2009 Analog Devices Inc.
Mike Frysingerad2d1632008-12-22 02:56:07 -05006 *
7 * Licensed under the GPL-2 or later.
8 *
9 * Based on smc91111_eeprom.c which:
10 * Heavily borrowed from the following peoples GPL'ed software:
11 * - Wolfgang Denk, DENX Software Engineering, wd@denx.de
12 * Das U-boot
13 * - Ladislav Michl ladis@linux-mips.org
14 * A rejected patch on the U-Boot mailing list
15 */
16
17#include <common.h>
18#include <exports.h>
Jason Hobbs4d91a6e2011-08-23 11:06:54 +000019#include <linux/ctype.h>
Mike Frysingerad2d1632008-12-22 02:56:07 -050020#include "../drivers/net/smc911x.h"
21
22/**
23 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
24 */
25static int smsc_ctrlc(void)
26{
27 return (tstc() && getc() == 0x03);
28}
29
30/**
31 * usage - dump usage information
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 * dump_regs - dump the MAC registers
52 *
53 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
54 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
55 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -050056static void dump_regs(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -050057{
58 u8 i, j = 0;
59 for (i = 0x50; i < 0xB8; i += sizeof(u32))
Mike Frysinger069f4362009-02-25 17:29:40 -050060 printf("%02x: 0x%08x %c", i,
Mike Frysingerc44efcf2009-11-12 22:26:02 -050061 smc911x_reg_read(dev, i),
Mike Frysingerad2d1632008-12-22 02:56:07 -050062 (j++ % 2 ? '\n' : ' '));
63}
64
65/**
66 * do_eeprom_cmd - handle eeprom communication
67 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -050068static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
Mike Frysingerad2d1632008-12-22 02:56:07 -050069{
Mike Frysingerc44efcf2009-11-12 22:26:02 -050070 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
Mike Frysingerad2d1632008-12-22 02:56:07 -050071 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
Mike Frysingerc44efcf2009-11-12 22:26:02 -050072 smc911x_reg_read(dev, E2P_CMD));
Mike Frysingerad2d1632008-12-22 02:56:07 -050073 return -1;
74 }
75
Mike Frysingerc44efcf2009-11-12 22:26:02 -050076 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -050077
Mike Frysingerc44efcf2009-11-12 22:26:02 -050078 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysingerad2d1632008-12-22 02:56:07 -050079 if (smsc_ctrlc()) {
80 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysingerc44efcf2009-11-12 22:26:02 -050081 smc911x_reg_read(dev, E2P_CMD));
Mike Frysingerad2d1632008-12-22 02:56:07 -050082 return -1;
83 }
84
85 return 0;
86}
87
88/**
89 * read_eeprom_reg - read specified register in EEPROM
90 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -050091static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
Mike Frysingerad2d1632008-12-22 02:56:07 -050092{
Mike Frysingerc44efcf2009-11-12 22:26:02 -050093 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
94 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
Mike Frysingerad2d1632008-12-22 02:56:07 -050095}
96
97/**
98 * write_eeprom_reg - write specified value into specified register in EEPROM
99 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500100static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500101{
102 int ret;
103
104 /* enable erasing/writing */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500105 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500106 if (ret)
107 goto done;
108
109 /* erase the eeprom reg */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500110 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500111 if (ret)
112 goto done;
113
114 /* write the eeprom reg */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500115 smc911x_reg_write(dev, E2P_DATA, value);
116 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500117 if (ret)
118 goto done;
119
120 /* disable erasing/writing */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500121 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500122
123 done:
124 return ret;
125}
126
127/**
128 * skip_space - find first non-whitespace in given pointer
129 */
130static char *skip_space(char *buf)
131{
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000132 while (isblank(buf[0]))
Mike Frysingerad2d1632008-12-22 02:56:07 -0500133 ++buf;
134 return buf;
135}
136
137/**
138 * write_stuff - handle writing of MAC registers / eeprom
139 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500140static void write_stuff(struct eth_device *dev, char *line)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500141{
142 char dest;
143 char *endp;
144 u8 reg;
145 u32 value;
146
147 /* Skip over the "W " part of the command */
148 line = skip_space(line + 1);
149
150 /* Figure out destination */
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 /* Get the register to write */
164 line = skip_space(line + 1);
165 reg = simple_strtoul(line, &endp, 16);
166 if (line == endp)
167 goto invalid_usage;
168
169 /* Get the value to write */
170 line = skip_space(endp);
171 value = simple_strtoul(line, &endp, 16);
172 if (line == endp)
173 goto invalid_usage;
174
175 /* Check for trailing cruft */
176 line = skip_space(endp);
177 if (line[0])
178 goto invalid_usage;
179
180 /* Finally, execute the command */
181 if (dest == 'E') {
182 printf("Writing EEPROM register %02x with %02x\n", reg, value);
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500183 write_eeprom_reg(dev, value, reg);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500184 } else {
185 printf("Writing MAC register %02x with %08x\n", reg, value);
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500186 smc911x_reg_write(dev, reg, value);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500187 }
188}
189
190/**
191 * copy_from_eeprom - copy MAC address in eeprom to address registers
192 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500193static void copy_from_eeprom(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500194{
195 ulong addrl =
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500196 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;
Mike Frysingerad2d1632008-12-22 02:56:07 -0500200 ulong addrh =
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500201 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);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500205 puts("EEPROM contents copied to MAC\n");
206}
207
208/**
209 * print_macaddr - print MAC address registers and MAC address in eeprom
210 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500211static void print_macaddr(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500212{
213 puts("Current MAC Address in MAC: ");
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500214 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
215 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500216 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)
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500223 printf("%02x:", read_eeprom_reg(dev, i));
224 printf("%02x\n", read_eeprom_reg(dev, i));
Mike Frysingerad2d1632008-12-22 02:56:07 -0500225}
226
227/**
228 * dump_eeprom - dump the whole content of the EEPROM
229 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500230static void dump_eeprom(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500231{
232 int i;
233 puts("EEPROM:\n");
234 for (i = 0; i < 7; ++i)
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500235 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
Mike Frysingerad2d1632008-12-22 02:56:07 -0500236}
237
238/**
239 * smc911x_init - get the MAC/EEPROM up and ready for use
240 */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500241static int smc911x_init(struct eth_device *dev)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500242{
243 /* See if there is anything there */
Juergen Kilbaa9fba52010-06-12 21:22:01 +0200244 if (smc911x_detect_chip(dev))
Mike Frysingerad2d1632008-12-22 02:56:07 -0500245 return 1;
246
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500247 smc911x_reset(dev);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500248
249 /* Make sure we set EEDIO/EECLK to the EEPROM */
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500250 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
251 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysingerad2d1632008-12-22 02:56:07 -0500252 if (smsc_ctrlc()) {
Mike Frysinger069f4362009-02-25 17:29:40 -0500253 printf("init: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500254 smc911x_reg_read(dev, E2P_CMD));
Mike Frysingerad2d1632008-12-22 02:56:07 -0500255 return 1;
256 }
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500257 smc911x_reg_write(dev, GPIO_CFG,
258 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
Mike Frysingerad2d1632008-12-22 02:56:07 -0500259 }
260
261 return 0;
262}
263
264/**
265 * getline - consume a line of input and handle some escape sequences
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 /* Convert to uppercase */
281 if (c >= 'a' && c <= 'z')
282 c -= ('a' - 'A');
283
284 switch (c) {
285 case '\r': /* Enter/Return key */
286 case '\n':
287 puts("\n");
288 return buffer;
289
290 case 0x03: /* ^C - break */
291 return NULL;
292
293 case 0x5F:
294 case 0x08: /* ^H - backspace */
295 case 0x7F: /* DEL - backspace */
296 if (i) {
297 puts("\b \b");
298 i--;
299 }
300 break;
301
302 default:
303 /* Ignore control characters */
304 if (c < 0x20)
305 break;
306 /* Queue up all other characters */
307 buffer[i++] = c;
308 printf("%c", c);
309 break;
310 }
311 }
312}
313
314/**
315 * smc911x_eeprom - our application's main() function
316 */
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200317int smc911x_eeprom(int argc, char * const argv[])
Mike Frysingerad2d1632008-12-22 02:56:07 -0500318{
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500319 /* Avoid initializing on stack as gcc likes to call memset() */
320 struct eth_device dev;
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500321 dev.iobase = CONFIG_SMC911X_BASE;
322
Mike Frysingerad2d1632008-12-22 02:56:07 -0500323 /* Print the ABI version */
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 /* Initialize the MAC/EEPROM somewhat */
333 puts("\n");
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500334 if (smc911x_init(&dev))
Mike Frysingerad2d1632008-12-22 02:56:07 -0500335 return 1;
336
337 /* Dump helpful usage information */
338 puts("\n");
339 usage();
340 puts("\n");
341
342 while (1) {
343 char *line;
344
345 /* Send the prompt and wait for a line */
346 puts("eeprom> ");
347 line = getline();
348
349 /* Got a ctrl+c */
350 if (!line)
351 return 0;
352
353 /* Eat leading space */
354 line = skip_space(line);
355
356 /* Empty line, try again */
357 if (!line[0])
358 continue;
359
360 /* Only accept 1 letter commands */
Jason Hobbs4d91a6e2011-08-23 11:06:54 +0000361 if (line[0] && line[1] && !isblank(line[1]))
Mike Frysingerad2d1632008-12-22 02:56:07 -0500362 goto unknown_cmd;
363
364 /* Now parse the command */
365 switch (line[0]) {
Mike Frysingerc44efcf2009-11-12 22:26:02 -0500366 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;
Mike Frysingerad2d1632008-12-22 02:56:07 -0500371 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}