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:
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 u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
56 return *(volatile u32*)(dev->iobase + offset);
59 static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
61 *(volatile u32*)(dev->iobase + offset) = val;
63 #elif defined (CONFIG_SMC911X_16_BIT)
64 static u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
66 volatile u16 *addr_16 = (u16 *)(dev->iobase + offset);
67 return (*addr_16 & 0x0000ffff) | (*(addr_16 + 1) << 16);
69 static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
71 *(volatile u16 *)(dev->iobase + offset) = (u16)val;
72 *(volatile u16 *)(dev->iobase + offset + 2) = (u16)(val >> 16);
75 #error "SMC911X: undefined bus width"
76 #endif /* CONFIG_SMC911X_16_BIT */
78 static u32 smc911x_get_mac_csr(struct eth_device *dev, u8 reg)
80 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
82 smc911x_reg_write(dev, MAC_CSR_CMD,
83 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
84 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
87 return smc911x_reg_read(dev, MAC_CSR_DATA);
90 static void smc911x_set_mac_csr(struct eth_device *dev, u8 reg, u32 data)
92 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
94 smc911x_reg_write(dev, MAC_CSR_DATA, data);
95 smc911x_reg_write(dev, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
96 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
100 static int smc911x_detect_chip(struct eth_device *dev)
102 unsigned long val, i;
104 val = smc911x_reg_read(dev, BYTE_TEST);
105 if (val == 0xffffffff) {
106 /* Special case -- no chip present */
108 } else if (val != 0x87654321) {
109 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
113 val = smc911x_reg_read(dev, ID_REV) >> 16;
114 for (i = 0; chip_ids[i].id != 0; i++) {
115 if (chip_ids[i].id == val) break;
117 if (!chip_ids[i].id) {
118 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
122 dev->priv = (void *)&chip_ids[i];
127 static void smc911x_reset(struct eth_device *dev)
132 * Take out of PM setting first
133 * Device is already wake up if PMT_CTRL_READY bit is set
135 if ((smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY) == 0) {
136 /* Write to the bytetest will take out of powerdown */
137 smc911x_reg_write(dev, BYTE_TEST, 0x0);
142 !(smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY))
146 ": timeout waiting for PM restore\n");
151 /* Disable interrupts */
152 smc911x_reg_write(dev, INT_EN, 0);
154 smc911x_reg_write(dev, HW_CFG, HW_CFG_SRST);
157 while (timeout-- && smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
161 printf(DRIVERNAME ": reset timeout\n");
165 /* Reset the FIFO level and flow control settings */
166 smc911x_set_mac_csr(dev, FLOW, FLOW_FCPT | FLOW_FCEN);
167 smc911x_reg_write(dev, AFC_CFG, 0x0050287F);
169 /* Set to LED outputs */
170 smc911x_reg_write(dev, GPIO_CFG, 0x70070000);
174 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
176 static int smsc_ctrlc(void)
178 return (tstc() && getc() == 0x03);
182 * usage - dump usage information
184 static void usage(void)
187 "MAC/EEPROM Commands:\n"
188 " P : Print the MAC addresses\n"
189 " D : Dump the EEPROM contents\n"
190 " M : Dump the MAC contents\n"
191 " C : Copy the MAC address from the EEPROM to the MAC\n"
192 " W : Write a register in the EEPROM or in the MAC\n"
195 "Some commands take arguments:\n"
196 " W <E|M> <register> <value>\n"
197 " E: EEPROM M: MAC\n"
202 * dump_regs - dump the MAC registers
204 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
205 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
207 static void dump_regs(struct eth_device *dev)
210 for (i = 0x50; i < 0xB8; i += sizeof(u32))
211 printf("%02x: 0x%08x %c", i,
212 smc911x_reg_read(dev, i),
213 (j++ % 2 ? '\n' : ' '));
217 * do_eeprom_cmd - handle eeprom communication
219 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
221 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
222 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
223 smc911x_reg_read(dev, E2P_CMD));
227 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
229 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
231 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
232 smc911x_reg_read(dev, E2P_CMD));
240 * read_eeprom_reg - read specified register in EEPROM
242 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
244 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
245 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
249 * write_eeprom_reg - write specified value into specified register in EEPROM
251 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
255 /* enable erasing/writing */
256 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
260 /* erase the eeprom reg */
261 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
265 /* write the eeprom reg */
266 smc911x_reg_write(dev, E2P_DATA, value);
267 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
271 /* disable erasing/writing */
272 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
279 * skip_space - find first non-whitespace in given pointer
281 static char *skip_space(char *buf)
283 while (isblank(buf[0]))
289 * write_stuff - handle writing of MAC registers / eeprom
291 static void write_stuff(struct eth_device *dev, char *line)
298 /* Skip over the "W " part of the command */
299 line = skip_space(line + 1);
301 /* Figure out destination */
309 printf("ERROR: Invalid write usage\n");
314 /* Get the register to write */
315 line = skip_space(line + 1);
316 reg = simple_strtoul(line, &endp, 16);
320 /* Get the value to write */
321 line = skip_space(endp);
322 value = simple_strtoul(line, &endp, 16);
326 /* Check for trailing cruft */
327 line = skip_space(endp);
331 /* Finally, execute the command */
333 printf("Writing EEPROM register %02x with %02x\n", reg, value);
334 write_eeprom_reg(dev, value, reg);
336 printf("Writing MAC register %02x with %08x\n", reg, value);
337 smc911x_reg_write(dev, reg, value);
342 * copy_from_eeprom - copy MAC address in eeprom to address registers
344 static void copy_from_eeprom(struct eth_device *dev)
347 read_eeprom_reg(dev, 0x01) |
348 read_eeprom_reg(dev, 0x02) << 8 |
349 read_eeprom_reg(dev, 0x03) << 16 |
350 read_eeprom_reg(dev, 0x04) << 24;
352 read_eeprom_reg(dev, 0x05) |
353 read_eeprom_reg(dev, 0x06) << 8;
354 smc911x_set_mac_csr(dev, ADDRL, addrl);
355 smc911x_set_mac_csr(dev, ADDRH, addrh);
356 puts("EEPROM contents copied to MAC\n");
360 * print_macaddr - print MAC address registers and MAC address in eeprom
362 static void print_macaddr(struct eth_device *dev)
364 puts("Current MAC Address in MAC: ");
365 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
366 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
367 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
368 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
369 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
371 puts("Current MAC Address in EEPROM: ");
373 for (i = 1; i < 6; ++i)
374 printf("%02x:", read_eeprom_reg(dev, i));
375 printf("%02x\n", read_eeprom_reg(dev, i));
379 * dump_eeprom - dump the whole content of the EEPROM
381 static void dump_eeprom(struct eth_device *dev)
385 for (i = 0; i < 7; ++i)
386 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
390 * smc911x_init - get the MAC/EEPROM up and ready for use
392 static int smc911x_init(struct eth_device *dev)
394 /* See if there is anything there */
395 if (smc911x_detect_chip(dev))
400 /* Make sure we set EEDIO/EECLK to the EEPROM */
401 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
402 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
404 printf("init: timeout (E2P_CMD = 0x%08x)\n",
405 smc911x_reg_read(dev, E2P_CMD));
408 smc911x_reg_write(dev, GPIO_CFG,
409 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
416 * getline - consume a line of input and handle some escape sequences
418 static char *getline(void)
420 static char buffer[100];
431 /* Convert to uppercase */
432 if (c >= 'a' && c <= 'z')
436 case '\r': /* Enter/Return key */
441 case 0x03: /* ^C - break */
445 case 0x08: /* ^H - backspace */
446 case 0x7F: /* DEL - backspace */
454 /* Ignore control characters */
457 /* Queue up all other characters */
466 * smc911x_eeprom - our application's main() function
468 int smc911x_eeprom(int argc, char * const argv[])
470 /* Avoid initializing on stack as gcc likes to call memset() */
471 struct eth_device dev;
472 dev.iobase = CONFIG_SMC911X_BASE;
474 /* Print the ABI version */
476 if (XF_VERSION != get_version()) {
477 printf("Expects ABI version %d\n", XF_VERSION);
478 printf("Actual U-Boot ABI version %lu\n", get_version());
479 printf("Can't run\n\n");
483 /* Initialize the MAC/EEPROM somewhat */
485 if (smc911x_init(&dev))
488 /* Dump helpful usage information */
496 /* Send the prompt and wait for a line */
504 /* Eat leading space */
505 line = skip_space(line);
507 /* Empty line, try again */
511 /* Only accept 1 letter commands */
512 if (line[0] && line[1] && !isblank(line[1]))
515 /* Now parse the command */
517 case 'W': write_stuff(&dev, line); break;
518 case 'D': dump_eeprom(&dev); break;
519 case 'M': dump_regs(&dev); break;
520 case 'C': copy_from_eeprom(&dev); break;
521 case 'P': print_macaddr(&dev); break;
523 default: puts("ERROR: Unknown command!\n\n");
525 case 'H': usage(); break;