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 <linux/types.h>
22 #include "../drivers/net/smc911x.h"
24 #define DRIVERNAME "smc911x"
26 #if defined (CONFIG_SMC911X_32_BIT) && \
27 defined (CONFIG_SMC911X_16_BIT)
28 #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
29 CONFIG_SMC911X_16_BIT shall be set"
37 static const struct chip_id chip_ids[] = {
38 { CHIP_89218, "LAN89218" },
39 { CHIP_9115, "LAN9115" },
40 { CHIP_9116, "LAN9116" },
41 { CHIP_9117, "LAN9117" },
42 { CHIP_9118, "LAN9118" },
43 { CHIP_9211, "LAN9211" },
44 { CHIP_9215, "LAN9215" },
45 { CHIP_9216, "LAN9216" },
46 { CHIP_9217, "LAN9217" },
47 { CHIP_9218, "LAN9218" },
48 { CHIP_9220, "LAN9220" },
49 { CHIP_9221, "LAN9221" },
53 #if defined (CONFIG_SMC911X_32_BIT)
54 static inline u32 __smc911x_reg_read(struct eth_device *dev, u32 offset)
56 return *(volatile u32*)(dev->iobase + offset);
58 u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
59 __attribute__((weak, alias("__smc911x_reg_read")));
61 static inline void __smc911x_reg_write(struct eth_device *dev,
64 *(volatile u32*)(dev->iobase + offset) = val;
66 void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
67 __attribute__((weak, alias("__smc911x_reg_write")));
68 #elif defined (CONFIG_SMC911X_16_BIT)
69 static inline u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
71 volatile u16 *addr_16 = (u16 *)(dev->iobase + offset);
72 return ((*addr_16 & 0x0000ffff) | (*(addr_16 + 1) << 16));
74 static inline void smc911x_reg_write(struct eth_device *dev,
77 *(volatile u16 *)(dev->iobase + offset) = (u16)val;
78 *(volatile u16 *)(dev->iobase + offset + 2) = (u16)(val >> 16);
81 #error "SMC911X: undefined bus width"
82 #endif /* CONFIG_SMC911X_16_BIT */
84 static u32 smc911x_get_mac_csr(struct eth_device *dev, u8 reg)
86 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
88 smc911x_reg_write(dev, MAC_CSR_CMD,
89 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
90 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
93 return smc911x_reg_read(dev, MAC_CSR_DATA);
96 static void smc911x_set_mac_csr(struct eth_device *dev, u8 reg, u32 data)
98 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
100 smc911x_reg_write(dev, MAC_CSR_DATA, data);
101 smc911x_reg_write(dev, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
102 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
106 static int smc911x_detect_chip(struct eth_device *dev)
108 unsigned long val, i;
110 val = smc911x_reg_read(dev, BYTE_TEST);
111 if (val == 0xffffffff) {
112 /* Special case -- no chip present */
114 } else if (val != 0x87654321) {
115 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
119 val = smc911x_reg_read(dev, ID_REV) >> 16;
120 for (i = 0; chip_ids[i].id != 0; i++) {
121 if (chip_ids[i].id == val) break;
123 if (!chip_ids[i].id) {
124 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
128 dev->priv = (void *)&chip_ids[i];
133 static void smc911x_reset(struct eth_device *dev)
138 * Take out of PM setting first
139 * Device is already wake up if PMT_CTRL_READY bit is set
141 if ((smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY) == 0) {
142 /* Write to the bytetest will take out of powerdown */
143 smc911x_reg_write(dev, BYTE_TEST, 0x0);
148 !(smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY))
152 ": timeout waiting for PM restore\n");
157 /* Disable interrupts */
158 smc911x_reg_write(dev, INT_EN, 0);
160 smc911x_reg_write(dev, HW_CFG, HW_CFG_SRST);
163 while (timeout-- && smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
167 printf(DRIVERNAME ": reset timeout\n");
171 /* Reset the FIFO level and flow control settings */
172 smc911x_set_mac_csr(dev, FLOW, FLOW_FCPT | FLOW_FCEN);
173 smc911x_reg_write(dev, AFC_CFG, 0x0050287F);
175 /* Set to LED outputs */
176 smc911x_reg_write(dev, GPIO_CFG, 0x70070000);
180 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
182 static int smsc_ctrlc(void)
184 return (tstc() && getc() == 0x03);
188 * usage - dump usage information
190 static void usage(void)
193 "MAC/EEPROM Commands:\n"
194 " P : Print the MAC addresses\n"
195 " D : Dump the EEPROM contents\n"
196 " M : Dump the MAC contents\n"
197 " C : Copy the MAC address from the EEPROM to the MAC\n"
198 " W : Write a register in the EEPROM or in the MAC\n"
201 "Some commands take arguments:\n"
202 " W <E|M> <register> <value>\n"
203 " E: EEPROM M: MAC\n"
208 * dump_regs - dump the MAC registers
210 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
211 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
213 static void dump_regs(struct eth_device *dev)
216 for (i = 0x50; i < 0xB8; i += sizeof(u32))
217 printf("%02x: 0x%08x %c", i,
218 smc911x_reg_read(dev, i),
219 (j++ % 2 ? '\n' : ' '));
223 * do_eeprom_cmd - handle eeprom communication
225 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
227 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
228 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
229 smc911x_reg_read(dev, E2P_CMD));
233 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
235 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
237 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
238 smc911x_reg_read(dev, E2P_CMD));
246 * read_eeprom_reg - read specified register in EEPROM
248 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
250 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
251 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
255 * write_eeprom_reg - write specified value into specified register in EEPROM
257 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
261 /* enable erasing/writing */
262 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
266 /* erase the eeprom reg */
267 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
271 /* write the eeprom reg */
272 smc911x_reg_write(dev, E2P_DATA, value);
273 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
277 /* disable erasing/writing */
278 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
285 * skip_space - find first non-whitespace in given pointer
287 static char *skip_space(char *buf)
289 while (isblank(buf[0]))
295 * write_stuff - handle writing of MAC registers / eeprom
297 static void write_stuff(struct eth_device *dev, char *line)
304 /* Skip over the "W " part of the command */
305 line = skip_space(line + 1);
307 /* Figure out destination */
315 printf("ERROR: Invalid write usage\n");
320 /* Get the register to write */
321 line = skip_space(line + 1);
322 reg = simple_strtoul(line, &endp, 16);
326 /* Get the value to write */
327 line = skip_space(endp);
328 value = simple_strtoul(line, &endp, 16);
332 /* Check for trailing cruft */
333 line = skip_space(endp);
337 /* Finally, execute the command */
339 printf("Writing EEPROM register %02x with %02x\n", reg, value);
340 write_eeprom_reg(dev, value, reg);
342 printf("Writing MAC register %02x with %08x\n", reg, value);
343 smc911x_reg_write(dev, reg, value);
348 * copy_from_eeprom - copy MAC address in eeprom to address registers
350 static void copy_from_eeprom(struct eth_device *dev)
353 read_eeprom_reg(dev, 0x01) |
354 read_eeprom_reg(dev, 0x02) << 8 |
355 read_eeprom_reg(dev, 0x03) << 16 |
356 read_eeprom_reg(dev, 0x04) << 24;
358 read_eeprom_reg(dev, 0x05) |
359 read_eeprom_reg(dev, 0x06) << 8;
360 smc911x_set_mac_csr(dev, ADDRL, addrl);
361 smc911x_set_mac_csr(dev, ADDRH, addrh);
362 puts("EEPROM contents copied to MAC\n");
366 * print_macaddr - print MAC address registers and MAC address in eeprom
368 static void print_macaddr(struct eth_device *dev)
370 puts("Current MAC Address in MAC: ");
371 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
372 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
373 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
374 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
375 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
377 puts("Current MAC Address in EEPROM: ");
379 for (i = 1; i < 6; ++i)
380 printf("%02x:", read_eeprom_reg(dev, i));
381 printf("%02x\n", read_eeprom_reg(dev, i));
385 * dump_eeprom - dump the whole content of the EEPROM
387 static void dump_eeprom(struct eth_device *dev)
391 for (i = 0; i < 7; ++i)
392 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
396 * smc911x_init - get the MAC/EEPROM up and ready for use
398 static int smc911x_init(struct eth_device *dev)
400 /* See if there is anything there */
401 if (smc911x_detect_chip(dev))
406 /* Make sure we set EEDIO/EECLK to the EEPROM */
407 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
408 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
410 printf("init: timeout (E2P_CMD = 0x%08x)\n",
411 smc911x_reg_read(dev, E2P_CMD));
414 smc911x_reg_write(dev, GPIO_CFG,
415 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
422 * getline - consume a line of input and handle some escape sequences
424 static char *getline(void)
426 static char buffer[100];
437 /* Convert to uppercase */
438 if (c >= 'a' && c <= 'z')
442 case '\r': /* Enter/Return key */
447 case 0x03: /* ^C - break */
451 case 0x08: /* ^H - backspace */
452 case 0x7F: /* DEL - backspace */
460 /* Ignore control characters */
463 /* Queue up all other characters */
472 * smc911x_eeprom - our application's main() function
474 int smc911x_eeprom(int argc, char * const argv[])
476 /* Avoid initializing on stack as gcc likes to call memset() */
477 struct eth_device dev;
478 dev.iobase = CONFIG_SMC911X_BASE;
480 /* Print the ABI version */
482 if (XF_VERSION != get_version()) {
483 printf("Expects ABI version %d\n", XF_VERSION);
484 printf("Actual U-Boot ABI version %lu\n", get_version());
485 printf("Can't run\n\n");
489 /* Initialize the MAC/EEPROM somewhat */
491 if (smc911x_init(&dev))
494 /* Dump helpful usage information */
502 /* Send the prompt and wait for a line */
510 /* Eat leading space */
511 line = skip_space(line);
513 /* Empty line, try again */
517 /* Only accept 1 letter commands */
518 if (line[0] && line[1] && !isblank(line[1]))
521 /* Now parse the command */
523 case 'W': write_stuff(&dev, line); break;
524 case 'D': dump_eeprom(&dev); break;
525 case 'M': dump_regs(&dev); break;
526 case 'C': copy_from_eeprom(&dev); break;
527 case 'P': print_macaddr(&dev); break;
529 default: puts("ERROR: Unknown command!\n\n");
531 case 'H': usage(); break;