]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - examples/standalone/smc911x_eeprom.c
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
21 #include <linux/ctype.h>
22 #include <linux/types.h>
23 #include "../drivers/net/smc911x.h"
25 #define DRIVERNAME "smc911x"
27 #if defined (CONFIG_SMC911X_32_BIT) && \
28 defined (CONFIG_SMC911X_16_BIT)
29 #error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
30 CONFIG_SMC911X_16_BIT shall be set"
38 static const struct chip_id chip_ids
[] = {
39 { CHIP_89218
, "LAN89218" },
40 { CHIP_9115
, "LAN9115" },
41 { CHIP_9116
, "LAN9116" },
42 { CHIP_9117
, "LAN9117" },
43 { CHIP_9118
, "LAN9118" },
44 { CHIP_9211
, "LAN9211" },
45 { CHIP_9215
, "LAN9215" },
46 { CHIP_9216
, "LAN9216" },
47 { CHIP_9217
, "LAN9217" },
48 { CHIP_9218
, "LAN9218" },
49 { CHIP_9220
, "LAN9220" },
50 { CHIP_9221
, "LAN9221" },
54 #if defined (CONFIG_SMC911X_32_BIT)
55 static u32
smc911x_reg_read(struct eth_device
*dev
, u32 offset
)
57 return *(volatile u32
*)(dev
->iobase
+ offset
);
60 static void smc911x_reg_write(struct eth_device
*dev
, u32 offset
, u32 val
)
62 *(volatile u32
*)(dev
->iobase
+ offset
) = val
;
64 #elif defined (CONFIG_SMC911X_16_BIT)
65 static u32
smc911x_reg_read(struct eth_device
*dev
, u32 offset
)
67 volatile u16
*addr_16
= (u16
*)(dev
->iobase
+ offset
);
68 return (*addr_16
& 0x0000ffff) | (*(addr_16
+ 1) << 16);
70 static void smc911x_reg_write(struct eth_device
*dev
, u32 offset
, u32 val
)
72 *(volatile u16
*)(dev
->iobase
+ offset
) = (u16
)val
;
73 *(volatile u16
*)(dev
->iobase
+ offset
+ 2) = (u16
)(val
>> 16);
76 #error "SMC911X: undefined bus width"
77 #endif /* CONFIG_SMC911X_16_BIT */
79 static u32
smc911x_get_mac_csr(struct eth_device
*dev
, u8 reg
)
81 while (smc911x_reg_read(dev
, MAC_CSR_CMD
) & MAC_CSR_CMD_CSR_BUSY
)
83 smc911x_reg_write(dev
, MAC_CSR_CMD
,
84 MAC_CSR_CMD_CSR_BUSY
| MAC_CSR_CMD_R_NOT_W
| reg
);
85 while (smc911x_reg_read(dev
, MAC_CSR_CMD
) & MAC_CSR_CMD_CSR_BUSY
)
88 return smc911x_reg_read(dev
, MAC_CSR_DATA
);
91 static void smc911x_set_mac_csr(struct eth_device
*dev
, u8 reg
, u32 data
)
93 while (smc911x_reg_read(dev
, MAC_CSR_CMD
) & MAC_CSR_CMD_CSR_BUSY
)
95 smc911x_reg_write(dev
, MAC_CSR_DATA
, data
);
96 smc911x_reg_write(dev
, MAC_CSR_CMD
, MAC_CSR_CMD_CSR_BUSY
| reg
);
97 while (smc911x_reg_read(dev
, MAC_CSR_CMD
) & MAC_CSR_CMD_CSR_BUSY
)
101 static int smc911x_detect_chip(struct eth_device
*dev
)
103 unsigned long val
, i
;
105 val
= smc911x_reg_read(dev
, BYTE_TEST
);
106 if (val
== 0xffffffff) {
107 /* Special case -- no chip present */
109 } else if (val
!= 0x87654321) {
110 printf(DRIVERNAME
": Invalid chip endian 0x%08lx\n", val
);
114 val
= smc911x_reg_read(dev
, ID_REV
) >> 16;
115 for (i
= 0; chip_ids
[i
].id
!= 0; i
++) {
116 if (chip_ids
[i
].id
== val
) break;
118 if (!chip_ids
[i
].id
) {
119 printf(DRIVERNAME
": Unknown chip ID %04lx\n", val
);
123 dev
->priv
= (void *)&chip_ids
[i
];
128 static void smc911x_reset(struct eth_device
*dev
)
133 * Take out of PM setting first
134 * Device is already wake up if PMT_CTRL_READY bit is set
136 if ((smc911x_reg_read(dev
, PMT_CTRL
) & PMT_CTRL_READY
) == 0) {
137 /* Write to the bytetest will take out of powerdown */
138 smc911x_reg_write(dev
, BYTE_TEST
, 0x0);
143 !(smc911x_reg_read(dev
, PMT_CTRL
) & PMT_CTRL_READY
))
147 ": timeout waiting for PM restore\n");
152 /* Disable interrupts */
153 smc911x_reg_write(dev
, INT_EN
, 0);
155 smc911x_reg_write(dev
, HW_CFG
, HW_CFG_SRST
);
158 while (timeout
-- && smc911x_reg_read(dev
, E2P_CMD
) & E2P_CMD_EPC_BUSY
)
162 printf(DRIVERNAME
": reset timeout\n");
166 /* Reset the FIFO level and flow control settings */
167 smc911x_set_mac_csr(dev
, FLOW
, FLOW_FCPT
| FLOW_FCEN
);
168 smc911x_reg_write(dev
, AFC_CFG
, 0x0050287F);
170 /* Set to LED outputs */
171 smc911x_reg_write(dev
, GPIO_CFG
, 0x70070000);
175 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
177 static int smsc_ctrlc(void)
179 return (tstc() && getc() == 0x03);
183 * usage - dump usage information
185 static void usage(void)
188 "MAC/EEPROM Commands:\n"
189 " P : Print the MAC addresses\n"
190 " D : Dump the EEPROM contents\n"
191 " M : Dump the MAC contents\n"
192 " C : Copy the MAC address from the EEPROM to the MAC\n"
193 " W : Write a register in the EEPROM or in the MAC\n"
196 "Some commands take arguments:\n"
197 " W <E|M> <register> <value>\n"
198 " E: EEPROM M: MAC\n"
203 * dump_regs - dump the MAC registers
205 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
206 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
208 static void dump_regs(struct eth_device
*dev
)
211 for (i
= 0x50; i
< 0xB8; i
+= sizeof(u32
))
212 printf("%02x: 0x%08x %c", i
,
213 smc911x_reg_read(dev
, i
),
214 (j
++ % 2 ? '\n' : ' '));
218 * do_eeprom_cmd - handle eeprom communication
220 static int do_eeprom_cmd(struct eth_device
*dev
, int cmd
, u8 reg
)
222 if (smc911x_reg_read(dev
, E2P_CMD
) & E2P_CMD_EPC_BUSY
) {
223 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
224 smc911x_reg_read(dev
, E2P_CMD
));
228 smc911x_reg_write(dev
, E2P_CMD
, E2P_CMD_EPC_BUSY
| cmd
| reg
);
230 while (smc911x_reg_read(dev
, E2P_CMD
) & E2P_CMD_EPC_BUSY
)
232 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
233 smc911x_reg_read(dev
, E2P_CMD
));
241 * read_eeprom_reg - read specified register in EEPROM
243 static u8
read_eeprom_reg(struct eth_device
*dev
, u8 reg
)
245 int ret
= do_eeprom_cmd(dev
, E2P_CMD_EPC_CMD_READ
, reg
);
246 return (ret
? : smc911x_reg_read(dev
, E2P_DATA
));
250 * write_eeprom_reg - write specified value into specified register in EEPROM
252 static int write_eeprom_reg(struct eth_device
*dev
, u8 value
, u8 reg
)
256 /* enable erasing/writing */
257 ret
= do_eeprom_cmd(dev
, E2P_CMD_EPC_CMD_EWEN
, reg
);
261 /* erase the eeprom reg */
262 ret
= do_eeprom_cmd(dev
, E2P_CMD_EPC_CMD_ERASE
, reg
);
266 /* write the eeprom reg */
267 smc911x_reg_write(dev
, E2P_DATA
, value
);
268 ret
= do_eeprom_cmd(dev
, E2P_CMD_EPC_CMD_WRITE
, reg
);
272 /* disable erasing/writing */
273 ret
= do_eeprom_cmd(dev
, E2P_CMD_EPC_CMD_EWDS
, reg
);
280 * skip_space - find first non-whitespace in given pointer
282 static char *skip_space(char *buf
)
284 while (isblank(buf
[0]))
290 * write_stuff - handle writing of MAC registers / eeprom
292 static void write_stuff(struct eth_device
*dev
, char *line
)
299 /* Skip over the "W " part of the command */
300 line
= skip_space(line
+ 1);
302 /* Figure out destination */
310 printf("ERROR: Invalid write usage\n");
315 /* Get the register to write */
316 line
= skip_space(line
+ 1);
317 reg
= simple_strtoul(line
, &endp
, 16);
321 /* Get the value to write */
322 line
= skip_space(endp
);
323 value
= simple_strtoul(line
, &endp
, 16);
327 /* Check for trailing cruft */
328 line
= skip_space(endp
);
332 /* Finally, execute the command */
334 printf("Writing EEPROM register %02x with %02x\n", reg
, value
);
335 write_eeprom_reg(dev
, value
, reg
);
337 printf("Writing MAC register %02x with %08x\n", reg
, value
);
338 smc911x_reg_write(dev
, reg
, value
);
343 * copy_from_eeprom - copy MAC address in eeprom to address registers
345 static void copy_from_eeprom(struct eth_device
*dev
)
348 read_eeprom_reg(dev
, 0x01) |
349 read_eeprom_reg(dev
, 0x02) << 8 |
350 read_eeprom_reg(dev
, 0x03) << 16 |
351 read_eeprom_reg(dev
, 0x04) << 24;
353 read_eeprom_reg(dev
, 0x05) |
354 read_eeprom_reg(dev
, 0x06) << 8;
355 smc911x_set_mac_csr(dev
, ADDRL
, addrl
);
356 smc911x_set_mac_csr(dev
, ADDRH
, addrh
);
357 puts("EEPROM contents copied to MAC\n");
361 * print_macaddr - print MAC address registers and MAC address in eeprom
363 static void print_macaddr(struct eth_device
*dev
)
365 puts("Current MAC Address in MAC: ");
366 ulong addrl
= smc911x_get_mac_csr(dev
, ADDRL
);
367 ulong addrh
= smc911x_get_mac_csr(dev
, ADDRH
);
368 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
369 (u8
)(addrl
), (u8
)(addrl
>> 8), (u8
)(addrl
>> 16),
370 (u8
)(addrl
>> 24), (u8
)(addrh
), (u8
)(addrh
>> 8));
372 puts("Current MAC Address in EEPROM: ");
374 for (i
= 1; i
< 6; ++i
)
375 printf("%02x:", read_eeprom_reg(dev
, i
));
376 printf("%02x\n", read_eeprom_reg(dev
, i
));
380 * dump_eeprom - dump the whole content of the EEPROM
382 static void dump_eeprom(struct eth_device
*dev
)
386 for (i
= 0; i
< 7; ++i
)
387 printf("%02x: 0x%02x\n", i
, read_eeprom_reg(dev
, i
));
391 * smc911x_init - get the MAC/EEPROM up and ready for use
393 static int smc911x_init(struct eth_device
*dev
)
395 /* See if there is anything there */
396 if (smc911x_detect_chip(dev
))
401 /* Make sure we set EEDIO/EECLK to the EEPROM */
402 if (smc911x_reg_read(dev
, GPIO_CFG
) & GPIO_CFG_EEPR_EN
) {
403 while (smc911x_reg_read(dev
, E2P_CMD
) & E2P_CMD_EPC_BUSY
)
405 printf("init: timeout (E2P_CMD = 0x%08x)\n",
406 smc911x_reg_read(dev
, E2P_CMD
));
409 smc911x_reg_write(dev
, GPIO_CFG
,
410 smc911x_reg_read(dev
, GPIO_CFG
) & ~GPIO_CFG_EEPR_EN
);
417 * getline - consume a line of input and handle some escape sequences
419 static char *getline(void)
421 static char buffer
[100];
432 /* Convert to uppercase */
433 if (c
>= 'a' && c
<= 'z')
437 case '\r': /* Enter/Return key */
442 case 0x03: /* ^C - break */
446 case 0x08: /* ^H - backspace */
447 case 0x7F: /* DEL - backspace */
455 /* Ignore control characters */
458 /* Queue up all other characters */
467 * smc911x_eeprom - our application's main() function
469 int smc911x_eeprom(int argc
, char *const argv
[])
471 /* Avoid initializing on stack as gcc likes to call memset() */
472 struct eth_device dev
;
473 dev
.iobase
= CONFIG_SMC911X_BASE
;
475 /* Print the ABI version */
477 if (XF_VERSION
!= get_version()) {
478 printf("Expects ABI version %d\n", XF_VERSION
);
479 printf("Actual U-Boot ABI version %lu\n", get_version());
480 printf("Can't run\n\n");
484 /* Initialize the MAC/EEPROM somewhat */
486 if (smc911x_init(&dev
))
489 /* Dump helpful usage information */
497 /* Send the prompt and wait for a line */
505 /* Eat leading space */
506 line
= skip_space(line
);
508 /* Empty line, try again */
512 /* Only accept 1 letter commands */
513 if (line
[0] && line
[1] && !isblank(line
[1]))
516 /* Now parse the command */
518 case 'W': write_stuff(&dev
, line
); break;
519 case 'D': dump_eeprom(&dev
); break;
520 case 'M': dump_regs(&dev
); break;
521 case 'C': copy_from_eeprom(&dev
); break;
522 case 'P': print_macaddr(&dev
); break;
524 default: puts("ERROR: Unknown command!\n\n");
526 case 'H': usage(); break;