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
20 /* the smc911x.h gets base addr through eth_device' iobase */
26 #include "../drivers/net/smc911x.h"
29 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
31 static int smsc_ctrlc(void)
33 return (tstc() && getc() == 0x03);
37 * usage - dump usage information
39 static void usage(void)
42 "MAC/EEPROM Commands:\n"
43 " P : Print the MAC addresses\n"
44 " D : Dump the EEPROM contents\n"
45 " M : Dump the MAC contents\n"
46 " C : Copy the MAC address from the EEPROM to the MAC\n"
47 " W : Write a register in the EEPROM or in the MAC\n"
50 "Some commands take arguments:\n"
51 " W <E|M> <register> <value>\n"
57 * dump_regs - dump the MAC registers
59 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
60 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
62 static void dump_regs(struct eth_device *dev)
65 for (i = 0x50; i < 0xB8; i += sizeof(u32))
66 printf("%02x: 0x%08x %c", i,
67 smc911x_reg_read(dev, i),
68 (j++ % 2 ? '\n' : ' '));
72 * do_eeprom_cmd - handle eeprom communication
74 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
76 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
77 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
78 smc911x_reg_read(dev, E2P_CMD));
82 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
84 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
86 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
87 smc911x_reg_read(dev, E2P_CMD));
95 * read_eeprom_reg - read specified register in EEPROM
97 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
99 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
100 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
104 * write_eeprom_reg - write specified value into specified register in EEPROM
106 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
110 /* enable erasing/writing */
111 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
115 /* erase the eeprom reg */
116 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
120 /* write the eeprom reg */
121 smc911x_reg_write(dev, E2P_DATA, value);
122 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
126 /* disable erasing/writing */
127 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
134 * skip_space - find first non-whitespace in given pointer
136 static char *skip_space(char *buf)
138 while (buf[0] == ' ' || buf[0] == '\t')
144 * write_stuff - handle writing of MAC registers / eeprom
146 static void write_stuff(struct eth_device *dev, char *line)
153 /* Skip over the "W " part of the command */
154 line = skip_space(line + 1);
156 /* Figure out destination */
164 printf("ERROR: Invalid write usage\n");
169 /* Get the register to write */
170 line = skip_space(line + 1);
171 reg = simple_strtoul(line, &endp, 16);
175 /* Get the value to write */
176 line = skip_space(endp);
177 value = simple_strtoul(line, &endp, 16);
181 /* Check for trailing cruft */
182 line = skip_space(endp);
186 /* Finally, execute the command */
188 printf("Writing EEPROM register %02x with %02x\n", reg, value);
189 write_eeprom_reg(dev, value, reg);
191 printf("Writing MAC register %02x with %08x\n", reg, value);
192 smc911x_reg_write(dev, reg, value);
197 * copy_from_eeprom - copy MAC address in eeprom to address registers
199 static void copy_from_eeprom(struct eth_device *dev)
202 read_eeprom_reg(dev, 0x01) |
203 read_eeprom_reg(dev, 0x02) << 8 |
204 read_eeprom_reg(dev, 0x03) << 16 |
205 read_eeprom_reg(dev, 0x04) << 24;
207 read_eeprom_reg(dev, 0x05) |
208 read_eeprom_reg(dev, 0x06) << 8;
209 smc911x_set_mac_csr(dev, ADDRL, addrl);
210 smc911x_set_mac_csr(dev, ADDRH, addrh);
211 puts("EEPROM contents copied to MAC\n");
215 * print_macaddr - print MAC address registers and MAC address in eeprom
217 static void print_macaddr(struct eth_device *dev)
219 puts("Current MAC Address in MAC: ");
220 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
221 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
222 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
223 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
224 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
226 puts("Current MAC Address in EEPROM: ");
228 for (i = 1; i < 6; ++i)
229 printf("%02x:", read_eeprom_reg(dev, i));
230 printf("%02x\n", read_eeprom_reg(dev, i));
234 * dump_eeprom - dump the whole content of the EEPROM
236 static void dump_eeprom(struct eth_device *dev)
240 for (i = 0; i < 7; ++i)
241 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
245 * smc911x_init - get the MAC/EEPROM up and ready for use
247 static int smc911x_init(struct eth_device *dev)
249 /* See if there is anything there */
250 if (!smc911x_detect_chip(dev))
255 /* Make sure we set EEDIO/EECLK to the EEPROM */
256 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
257 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
259 printf("init: timeout (E2P_CMD = 0x%08x)\n",
260 smc911x_reg_read(dev, E2P_CMD));
263 smc911x_reg_write(dev, GPIO_CFG,
264 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
271 * getline - consume a line of input and handle some escape sequences
273 static char *getline(void)
275 static char buffer[100];
286 /* Convert to uppercase */
287 if (c >= 'a' && c <= 'z')
291 case '\r': /* Enter/Return key */
296 case 0x03: /* ^C - break */
300 case 0x08: /* ^H - backspace */
301 case 0x7F: /* DEL - backspace */
309 /* Ignore control characters */
312 /* Queue up all other characters */
321 * smc911x_eeprom - our application's main() function
323 int smc911x_eeprom(int argc, char *argv[])
325 /* Avoid initializing on stack as gcc likes to call memset() */
326 struct eth_device dev;
328 dev.iobase = CONFIG_SMC911X_BASE;
330 /* Print the ABI version */
332 if (XF_VERSION != get_version()) {
333 printf("Expects ABI version %d\n", XF_VERSION);
334 printf("Actual U-Boot ABI version %lu\n", get_version());
335 printf("Can't run\n\n");
339 /* Initialize the MAC/EEPROM somewhat */
341 if (smc911x_init(&dev))
344 /* Dump helpful usage information */
352 /* Send the prompt and wait for a line */
360 /* Eat leading space */
361 line = skip_space(line);
363 /* Empty line, try again */
367 /* Only accept 1 letter commands */
368 if (line[0] && line[1] && line[1] != ' ' && line[1] != '\t')
371 /* Now parse the command */
373 case 'W': write_stuff(&dev, line); break;
374 case 'D': dump_eeprom(&dev); break;
375 case 'M': dump_regs(&dev); break;
376 case 'C': copy_from_eeprom(&dev); break;
377 case 'P': print_macaddr(&dev); break;
379 default: puts("ERROR: Unknown command!\n\n");
381 case 'H': usage(); break;