2 * smc911x_eeprom.c - EEPROM interface to SMC911x parts.
3 * Only tested on SMSC9118 though ...
5 * Copyright 2004-2009 Analog Devices Inc.
7 * Licensed under the GPL-2 or later.
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
13 * - Ladislav Michl ladis@linux-mips.org
14 * A rejected patch on the U-Boot mailing list
19 #include "../drivers/net/smc911x.h"
22 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
24 static int smsc_ctrlc(void)
26 return (tstc() && getc() == 0x03);
30 * usage - dump usage information
32 static void usage(void)
35 "MAC/EEPROM Commands:\n"
36 " P : Print the MAC addresses\n"
37 " D : Dump the EEPROM contents\n"
38 " M : Dump the MAC contents\n"
39 " C : Copy the MAC address from the EEPROM to the MAC\n"
40 " W : Write a register in the EEPROM or in the MAC\n"
43 "Some commands take arguments:\n"
44 " W <E|M> <register> <value>\n"
50 * dump_regs - dump the MAC registers
52 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
53 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
55 static void dump_regs(struct eth_device *dev)
58 for (i = 0x50; i < 0xB8; i += sizeof(u32))
59 printf("%02x: 0x%08x %c", i,
60 smc911x_reg_read(dev, i),
61 (j++ % 2 ? '\n' : ' '));
65 * do_eeprom_cmd - handle eeprom communication
67 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
69 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
70 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
71 smc911x_reg_read(dev, E2P_CMD));
75 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
77 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
79 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
80 smc911x_reg_read(dev, E2P_CMD));
88 * read_eeprom_reg - read specified register in EEPROM
90 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
92 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
93 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
97 * write_eeprom_reg - write specified value into specified register in EEPROM
99 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
103 /* enable erasing/writing */
104 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
108 /* erase the eeprom reg */
109 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
113 /* write the eeprom reg */
114 smc911x_reg_write(dev, E2P_DATA, value);
115 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
119 /* disable erasing/writing */
120 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
127 * skip_space - find first non-whitespace in given pointer
129 static char *skip_space(char *buf)
131 while (buf[0] == ' ' || buf[0] == '\t')
137 * write_stuff - handle writing of MAC registers / eeprom
139 static void write_stuff(struct eth_device *dev, char *line)
146 /* Skip over the "W " part of the command */
147 line = skip_space(line + 1);
149 /* Figure out destination */
157 printf("ERROR: Invalid write usage\n");
162 /* Get the register to write */
163 line = skip_space(line + 1);
164 reg = simple_strtoul(line, &endp, 16);
168 /* Get the value to write */
169 line = skip_space(endp);
170 value = simple_strtoul(line, &endp, 16);
174 /* Check for trailing cruft */
175 line = skip_space(endp);
179 /* Finally, execute the command */
181 printf("Writing EEPROM register %02x with %02x\n", reg, value);
182 write_eeprom_reg(dev, value, reg);
184 printf("Writing MAC register %02x with %08x\n", reg, value);
185 smc911x_reg_write(dev, reg, value);
190 * copy_from_eeprom - copy MAC address in eeprom to address registers
192 static void copy_from_eeprom(struct eth_device *dev)
195 read_eeprom_reg(dev, 0x01) |
196 read_eeprom_reg(dev, 0x02) << 8 |
197 read_eeprom_reg(dev, 0x03) << 16 |
198 read_eeprom_reg(dev, 0x04) << 24;
200 read_eeprom_reg(dev, 0x05) |
201 read_eeprom_reg(dev, 0x06) << 8;
202 smc911x_set_mac_csr(dev, ADDRL, addrl);
203 smc911x_set_mac_csr(dev, ADDRH, addrh);
204 puts("EEPROM contents copied to MAC\n");
208 * print_macaddr - print MAC address registers and MAC address in eeprom
210 static void print_macaddr(struct eth_device *dev)
212 puts("Current MAC Address in MAC: ");
213 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
214 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
215 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
216 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
217 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
219 puts("Current MAC Address in EEPROM: ");
221 for (i = 1; i < 6; ++i)
222 printf("%02x:", read_eeprom_reg(dev, i));
223 printf("%02x\n", read_eeprom_reg(dev, i));
227 * dump_eeprom - dump the whole content of the EEPROM
229 static void dump_eeprom(struct eth_device *dev)
233 for (i = 0; i < 7; ++i)
234 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
238 * smc911x_init - get the MAC/EEPROM up and ready for use
240 static int smc911x_init(struct eth_device *dev)
242 /* See if there is anything there */
243 if (smc911x_detect_chip(dev))
248 /* Make sure we set EEDIO/EECLK to the EEPROM */
249 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
250 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
252 printf("init: timeout (E2P_CMD = 0x%08x)\n",
253 smc911x_reg_read(dev, E2P_CMD));
256 smc911x_reg_write(dev, GPIO_CFG,
257 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
264 * getline - consume a line of input and handle some escape sequences
266 static char *getline(void)
268 static char buffer[100];
279 /* Convert to uppercase */
280 if (c >= 'a' && c <= 'z')
284 case '\r': /* Enter/Return key */
289 case 0x03: /* ^C - break */
293 case 0x08: /* ^H - backspace */
294 case 0x7F: /* DEL - backspace */
302 /* Ignore control characters */
305 /* Queue up all other characters */
314 * smc911x_eeprom - our application's main() function
316 int smc911x_eeprom(int argc, char * const argv[])
318 /* Avoid initializing on stack as gcc likes to call memset() */
319 struct eth_device dev;
320 dev.iobase = CONFIG_SMC911X_BASE;
322 /* Print the ABI version */
324 if (XF_VERSION != get_version()) {
325 printf("Expects ABI version %d\n", XF_VERSION);
326 printf("Actual U-Boot ABI version %lu\n", get_version());
327 printf("Can't run\n\n");
331 /* Initialize the MAC/EEPROM somewhat */
333 if (smc911x_init(&dev))
336 /* Dump helpful usage information */
344 /* Send the prompt and wait for a line */
352 /* Eat leading space */
353 line = skip_space(line);
355 /* Empty line, try again */
359 /* Only accept 1 letter commands */
360 if (line[0] && line[1] && line[1] != ' ' && line[1] != '\t')
363 /* Now parse the command */
365 case 'W': write_stuff(&dev, line); break;
366 case 'D': dump_eeprom(&dev); break;
367 case 'M': dump_regs(&dev); break;
368 case 'C': copy_from_eeprom(&dev); break;
369 case 'P': print_macaddr(&dev); break;
371 default: puts("ERROR: Unknown command!\n\n");
373 case 'H': usage(); break;