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 #include <linux/ctype.h>
21 #include "../drivers/net/smc911x.h"
24 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
26 static int smsc_ctrlc(void)
28 return (tstc() && getc() == 0x03);
32 * usage - dump usage information
34 static void usage(void)
37 "MAC/EEPROM Commands:\n"
38 " P : Print the MAC addresses\n"
39 " D : Dump the EEPROM contents\n"
40 " M : Dump the MAC contents\n"
41 " C : Copy the MAC address from the EEPROM to the MAC\n"
42 " W : Write a register in the EEPROM or in the MAC\n"
45 "Some commands take arguments:\n"
46 " W <E|M> <register> <value>\n"
52 * dump_regs - dump the MAC registers
54 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
55 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
57 static void dump_regs(struct eth_device *dev)
60 for (i = 0x50; i < 0xB8; i += sizeof(u32))
61 printf("%02x: 0x%08x %c", i,
62 smc911x_reg_read(dev, i),
63 (j++ % 2 ? '\n' : ' '));
67 * do_eeprom_cmd - handle eeprom communication
69 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
71 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
72 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
73 smc911x_reg_read(dev, E2P_CMD));
77 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
79 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
81 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
82 smc911x_reg_read(dev, E2P_CMD));
90 * read_eeprom_reg - read specified register in EEPROM
92 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
94 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
95 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
99 * write_eeprom_reg - write specified value into specified register in EEPROM
101 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
105 /* enable erasing/writing */
106 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
110 /* erase the eeprom reg */
111 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
115 /* write the eeprom reg */
116 smc911x_reg_write(dev, E2P_DATA, value);
117 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
121 /* disable erasing/writing */
122 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
129 * skip_space - find first non-whitespace in given pointer
131 static char *skip_space(char *buf)
133 while (isblank(buf[0]))
139 * write_stuff - handle writing of MAC registers / eeprom
141 static void write_stuff(struct eth_device *dev, char *line)
148 /* Skip over the "W " part of the command */
149 line = skip_space(line + 1);
151 /* Figure out destination */
159 printf("ERROR: Invalid write usage\n");
164 /* Get the register to write */
165 line = skip_space(line + 1);
166 reg = simple_strtoul(line, &endp, 16);
170 /* Get the value to write */
171 line = skip_space(endp);
172 value = simple_strtoul(line, &endp, 16);
176 /* Check for trailing cruft */
177 line = skip_space(endp);
181 /* Finally, execute the command */
183 printf("Writing EEPROM register %02x with %02x\n", reg, value);
184 write_eeprom_reg(dev, value, reg);
186 printf("Writing MAC register %02x with %08x\n", reg, value);
187 smc911x_reg_write(dev, reg, value);
192 * copy_from_eeprom - copy MAC address in eeprom to address registers
194 static void copy_from_eeprom(struct eth_device *dev)
197 read_eeprom_reg(dev, 0x01) |
198 read_eeprom_reg(dev, 0x02) << 8 |
199 read_eeprom_reg(dev, 0x03) << 16 |
200 read_eeprom_reg(dev, 0x04) << 24;
202 read_eeprom_reg(dev, 0x05) |
203 read_eeprom_reg(dev, 0x06) << 8;
204 smc911x_set_mac_csr(dev, ADDRL, addrl);
205 smc911x_set_mac_csr(dev, ADDRH, addrh);
206 puts("EEPROM contents copied to MAC\n");
210 * print_macaddr - print MAC address registers and MAC address in eeprom
212 static void print_macaddr(struct eth_device *dev)
214 puts("Current MAC Address in MAC: ");
215 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
216 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
217 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
218 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
219 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
221 puts("Current MAC Address in EEPROM: ");
223 for (i = 1; i < 6; ++i)
224 printf("%02x:", read_eeprom_reg(dev, i));
225 printf("%02x\n", read_eeprom_reg(dev, i));
229 * dump_eeprom - dump the whole content of the EEPROM
231 static void dump_eeprom(struct eth_device *dev)
235 for (i = 0; i < 7; ++i)
236 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
240 * smc911x_init - get the MAC/EEPROM up and ready for use
242 static int smc911x_init(struct eth_device *dev)
244 /* See if there is anything there */
245 if (smc911x_detect_chip(dev))
250 /* Make sure we set EEDIO/EECLK to the EEPROM */
251 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
252 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
254 printf("init: timeout (E2P_CMD = 0x%08x)\n",
255 smc911x_reg_read(dev, E2P_CMD));
258 smc911x_reg_write(dev, GPIO_CFG,
259 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
266 * getline - consume a line of input and handle some escape sequences
268 static char *getline(void)
270 static char buffer[100];
281 /* Convert to uppercase */
282 if (c >= 'a' && c <= 'z')
286 case '\r': /* Enter/Return key */
291 case 0x03: /* ^C - break */
295 case 0x08: /* ^H - backspace */
296 case 0x7F: /* DEL - backspace */
304 /* Ignore control characters */
307 /* Queue up all other characters */
316 * smc911x_eeprom - our application's main() function
318 int smc911x_eeprom(int argc, char * const argv[])
320 /* Avoid initializing on stack as gcc likes to call memset() */
321 struct eth_device dev;
322 dev.iobase = CONFIG_SMC911X_BASE;
324 /* Print the ABI version */
326 if (XF_VERSION != get_version()) {
327 printf("Expects ABI version %d\n", XF_VERSION);
328 printf("Actual U-Boot ABI version %lu\n", get_version());
329 printf("Can't run\n\n");
333 /* Initialize the MAC/EEPROM somewhat */
335 if (smc911x_init(&dev))
338 /* Dump helpful usage information */
346 /* Send the prompt and wait for a line */
354 /* Eat leading space */
355 line = skip_space(line);
357 /* Empty line, try again */
361 /* Only accept 1 letter commands */
362 if (line[0] && line[1] && !isblank(line[1]))
365 /* Now parse the command */
367 case 'W': write_stuff(&dev, line); break;
368 case 'D': dump_eeprom(&dev); break;
369 case 'M': dump_regs(&dev); break;
370 case 'C': copy_from_eeprom(&dev); break;
371 case 'P': print_macaddr(&dev); break;
373 default: puts("ERROR: Unknown command!\n\n");
375 case 'H': usage(); break;