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 <linux/ctype.h>
20 #include "../drivers/net/smc911x.h"
23 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
25 static int smsc_ctrlc(void)
27 return (tstc() && getc() == 0x03);
31 * usage - dump usage information
33 static void usage(void)
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"
44 "Some commands take arguments:\n"
45 " W <E|M> <register> <value>\n"
51 * dump_regs - dump the MAC registers
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.
56 static void dump_regs(struct eth_device *dev)
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' : ' '));
66 * do_eeprom_cmd - handle eeprom communication
68 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
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));
76 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
78 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
80 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
81 smc911x_reg_read(dev, E2P_CMD));
89 * read_eeprom_reg - read specified register in EEPROM
91 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
93 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
94 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
98 * write_eeprom_reg - write specified value into specified register in EEPROM
100 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
104 /* enable erasing/writing */
105 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
109 /* erase the eeprom reg */
110 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
114 /* write the eeprom reg */
115 smc911x_reg_write(dev, E2P_DATA, value);
116 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
120 /* disable erasing/writing */
121 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
128 * skip_space - find first non-whitespace in given pointer
130 static char *skip_space(char *buf)
132 while (isblank(buf[0]))
138 * write_stuff - handle writing of MAC registers / eeprom
140 static void write_stuff(struct eth_device *dev, char *line)
147 /* Skip over the "W " part of the command */
148 line = skip_space(line + 1);
150 /* Figure out destination */
158 printf("ERROR: Invalid write usage\n");
163 /* Get the register to write */
164 line = skip_space(line + 1);
165 reg = simple_strtoul(line, &endp, 16);
169 /* Get the value to write */
170 line = skip_space(endp);
171 value = simple_strtoul(line, &endp, 16);
175 /* Check for trailing cruft */
176 line = skip_space(endp);
180 /* Finally, execute the command */
182 printf("Writing EEPROM register %02x with %02x\n", reg, value);
183 write_eeprom_reg(dev, value, reg);
185 printf("Writing MAC register %02x with %08x\n", reg, value);
186 smc911x_reg_write(dev, reg, value);
191 * copy_from_eeprom - copy MAC address in eeprom to address registers
193 static void copy_from_eeprom(struct eth_device *dev)
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;
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");
209 * print_macaddr - print MAC address registers and MAC address in eeprom
211 static void print_macaddr(struct eth_device *dev)
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));
220 puts("Current MAC Address in EEPROM: ");
222 for (i = 1; i < 6; ++i)
223 printf("%02x:", read_eeprom_reg(dev, i));
224 printf("%02x\n", read_eeprom_reg(dev, i));
228 * dump_eeprom - dump the whole content of the EEPROM
230 static void dump_eeprom(struct eth_device *dev)
234 for (i = 0; i < 7; ++i)
235 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
239 * smc911x_init - get the MAC/EEPROM up and ready for use
241 static int smc911x_init(struct eth_device *dev)
243 /* See if there is anything there */
244 if (smc911x_detect_chip(dev))
249 /* Make sure we set EEDIO/EECLK to the EEPROM */
250 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
251 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
253 printf("init: timeout (E2P_CMD = 0x%08x)\n",
254 smc911x_reg_read(dev, E2P_CMD));
257 smc911x_reg_write(dev, GPIO_CFG,
258 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
265 * getline - consume a line of input and handle some escape sequences
267 static char *getline(void)
269 static char buffer[100];
280 /* Convert to uppercase */
281 if (c >= 'a' && c <= 'z')
285 case '\r': /* Enter/Return key */
290 case 0x03: /* ^C - break */
294 case 0x08: /* ^H - backspace */
295 case 0x7F: /* DEL - backspace */
303 /* Ignore control characters */
306 /* Queue up all other characters */
315 * smc911x_eeprom - our application's main() function
317 int smc911x_eeprom(int argc, char * const argv[])
319 /* Avoid initializing on stack as gcc likes to call memset() */
320 struct eth_device dev;
321 dev.iobase = CONFIG_SMC911X_BASE;
323 /* Print the ABI version */
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");
332 /* Initialize the MAC/EEPROM somewhat */
334 if (smc911x_init(&dev))
337 /* Dump helpful usage information */
345 /* Send the prompt and wait for a line */
353 /* Eat leading space */
354 line = skip_space(line);
356 /* Empty line, try again */
360 /* Only accept 1 letter commands */
361 if (line[0] && line[1] && !isblank(line[1]))
364 /* Now parse the command */
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;
372 default: puts("ERROR: Unknown command!\n\n");
374 case 'H': usage(); break;