Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 1 | /* |
| 2 | * smc911x_eeprom.c - EEPROM interface to SMC911x parts. |
| 3 | * Only tested on SMSC9118 though ... |
| 4 | * |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 5 | * Copyright 2004-2009 Analog Devices Inc. |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 6 | * |
| 7 | * Licensed under the GPL-2 or later. |
| 8 | * |
| 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 |
Bin Meng | 7557405 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 12 | * Das U-Boot |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 13 | * - Ladislav Michl ladis@linux-mips.org |
| 14 | * A rejected patch on the U-Boot mailing list |
| 15 | */ |
| 16 | |
| 17 | #include <common.h> |
Simon Glass | a73bda4 | 2015-11-08 23:47:45 -0700 | [diff] [blame] | 18 | #include <console.h> |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 19 | #include <exports.h> |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 20 | #include <linux/ctype.h> |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 21 | #include <linux/types.h> |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 22 | #include "../drivers/net/smc911x.h" |
| 23 | |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 24 | #define DRIVERNAME "smc911x" |
| 25 | |
| 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" |
| 30 | #endif |
| 31 | |
| 32 | struct chip_id { |
| 33 | u16 id; |
| 34 | char *name; |
| 35 | }; |
| 36 | |
| 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" }, |
| 50 | { 0, NULL }, |
| 51 | }; |
| 52 | |
| 53 | #if defined (CONFIG_SMC911X_32_BIT) |
Marek Vasut | 8577e8c | 2020-03-15 14:38:59 +0100 | [diff] [blame] | 54 | static u32 smc911x_reg_read(struct eth_device *dev, u32 offset) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 55 | { |
| 56 | return *(volatile u32*)(dev->iobase + offset); |
| 57 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 58 | |
Marek Vasut | 8577e8c | 2020-03-15 14:38:59 +0100 | [diff] [blame] | 59 | static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 60 | { |
| 61 | *(volatile u32*)(dev->iobase + offset) = val; |
| 62 | } |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 63 | #elif defined (CONFIG_SMC911X_16_BIT) |
Marek Vasut | 8577e8c | 2020-03-15 14:38:59 +0100 | [diff] [blame] | 64 | static u32 smc911x_reg_read(struct eth_device *dev, u32 offset) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 65 | { |
| 66 | volatile u16 *addr_16 = (u16 *)(dev->iobase + offset); |
Marek Vasut | 8577e8c | 2020-03-15 14:38:59 +0100 | [diff] [blame] | 67 | return (*addr_16 & 0x0000ffff) | (*(addr_16 + 1) << 16); |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 68 | } |
Marek Vasut | 8577e8c | 2020-03-15 14:38:59 +0100 | [diff] [blame] | 69 | static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val) |
Marek Vasut | 689434e | 2020-03-15 15:03:07 +0100 | [diff] [blame] | 70 | { |
| 71 | *(volatile u16 *)(dev->iobase + offset) = (u16)val; |
| 72 | *(volatile u16 *)(dev->iobase + offset + 2) = (u16)(val >> 16); |
| 73 | } |
| 74 | #else |
| 75 | #error "SMC911X: undefined bus width" |
| 76 | #endif /* CONFIG_SMC911X_16_BIT */ |
| 77 | |
| 78 | static u32 smc911x_get_mac_csr(struct eth_device *dev, u8 reg) |
| 79 | { |
| 80 | while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
| 81 | ; |
| 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) |
| 85 | ; |
| 86 | |
| 87 | return smc911x_reg_read(dev, MAC_CSR_DATA); |
| 88 | } |
| 89 | |
| 90 | static void smc911x_set_mac_csr(struct eth_device *dev, u8 reg, u32 data) |
| 91 | { |
| 92 | while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY) |
| 93 | ; |
| 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) |
| 97 | ; |
| 98 | } |
| 99 | |
| 100 | static int smc911x_detect_chip(struct eth_device *dev) |
| 101 | { |
| 102 | unsigned long val, i; |
| 103 | |
| 104 | val = smc911x_reg_read(dev, BYTE_TEST); |
| 105 | if (val == 0xffffffff) { |
| 106 | /* Special case -- no chip present */ |
| 107 | return -1; |
| 108 | } else if (val != 0x87654321) { |
| 109 | printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val); |
| 110 | return -1; |
| 111 | } |
| 112 | |
| 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; |
| 116 | } |
| 117 | if (!chip_ids[i].id) { |
| 118 | printf(DRIVERNAME ": Unknown chip ID %04lx\n", val); |
| 119 | return -1; |
| 120 | } |
| 121 | |
| 122 | dev->priv = (void *)&chip_ids[i]; |
| 123 | |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | static void smc911x_reset(struct eth_device *dev) |
| 128 | { |
| 129 | int timeout; |
| 130 | |
| 131 | /* |
| 132 | * Take out of PM setting first |
| 133 | * Device is already wake up if PMT_CTRL_READY bit is set |
| 134 | */ |
| 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); |
| 138 | |
| 139 | timeout = 10; |
| 140 | |
| 141 | while (timeout-- && |
| 142 | !(smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY)) |
| 143 | udelay(10); |
| 144 | if (timeout < 0) { |
| 145 | printf(DRIVERNAME |
| 146 | ": timeout waiting for PM restore\n"); |
| 147 | return; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | /* Disable interrupts */ |
| 152 | smc911x_reg_write(dev, INT_EN, 0); |
| 153 | |
| 154 | smc911x_reg_write(dev, HW_CFG, HW_CFG_SRST); |
| 155 | |
| 156 | timeout = 1000; |
| 157 | while (timeout-- && smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) |
| 158 | udelay(10); |
| 159 | |
| 160 | if (timeout < 0) { |
| 161 | printf(DRIVERNAME ": reset timeout\n"); |
| 162 | return; |
| 163 | } |
| 164 | |
| 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); |
| 168 | |
| 169 | /* Set to LED outputs */ |
| 170 | smc911x_reg_write(dev, GPIO_CFG, 0x70070000); |
| 171 | } |
| 172 | |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 173 | /** |
| 174 | * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?) |
| 175 | */ |
| 176 | static int smsc_ctrlc(void) |
| 177 | { |
| 178 | return (tstc() && getc() == 0x03); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * usage - dump usage information |
| 183 | */ |
| 184 | static void usage(void) |
| 185 | { |
| 186 | puts( |
| 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" |
| 193 | " Q : Quit\n" |
| 194 | "\n" |
| 195 | "Some commands take arguments:\n" |
| 196 | " W <E|M> <register> <value>\n" |
| 197 | " E: EEPROM M: MAC\n" |
| 198 | ); |
| 199 | } |
| 200 | |
| 201 | /** |
| 202 | * dump_regs - dump the MAC registers |
| 203 | * |
| 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. |
| 206 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 207 | static void dump_regs(struct eth_device *dev) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 208 | { |
| 209 | u8 i, j = 0; |
| 210 | for (i = 0x50; i < 0xB8; i += sizeof(u32)) |
Mike Frysinger | 78b5336 | 2009-02-25 17:29:40 -0500 | [diff] [blame] | 211 | printf("%02x: 0x%08x %c", i, |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 212 | smc911x_reg_read(dev, i), |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 213 | (j++ % 2 ? '\n' : ' ')); |
| 214 | } |
| 215 | |
| 216 | /** |
| 217 | * do_eeprom_cmd - handle eeprom communication |
| 218 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 219 | static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 220 | { |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 221 | if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) { |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 222 | printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n", |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 223 | smc911x_reg_read(dev, E2P_CMD)); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 227 | smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 228 | |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 229 | while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 230 | if (smsc_ctrlc()) { |
| 231 | printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n", |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 232 | smc911x_reg_read(dev, E2P_CMD)); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 233 | return -1; |
| 234 | } |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | |
| 239 | /** |
| 240 | * read_eeprom_reg - read specified register in EEPROM |
| 241 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 242 | static u8 read_eeprom_reg(struct eth_device *dev, u8 reg) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 243 | { |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 244 | int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg); |
| 245 | return (ret ? : smc911x_reg_read(dev, E2P_DATA)); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | /** |
| 249 | * write_eeprom_reg - write specified value into specified register in EEPROM |
| 250 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 251 | static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 252 | { |
| 253 | int ret; |
| 254 | |
| 255 | /* enable erasing/writing */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 256 | ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 257 | if (ret) |
| 258 | goto done; |
| 259 | |
| 260 | /* erase the eeprom reg */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 261 | ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 262 | if (ret) |
| 263 | goto done; |
| 264 | |
| 265 | /* write the eeprom reg */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 266 | smc911x_reg_write(dev, E2P_DATA, value); |
| 267 | ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 268 | if (ret) |
| 269 | goto done; |
| 270 | |
| 271 | /* disable erasing/writing */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 272 | ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 273 | |
| 274 | done: |
| 275 | return ret; |
| 276 | } |
| 277 | |
| 278 | /** |
| 279 | * skip_space - find first non-whitespace in given pointer |
| 280 | */ |
| 281 | static char *skip_space(char *buf) |
| 282 | { |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 283 | while (isblank(buf[0])) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 284 | ++buf; |
| 285 | return buf; |
| 286 | } |
| 287 | |
| 288 | /** |
| 289 | * write_stuff - handle writing of MAC registers / eeprom |
| 290 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 291 | static void write_stuff(struct eth_device *dev, char *line) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 292 | { |
| 293 | char dest; |
| 294 | char *endp; |
| 295 | u8 reg; |
| 296 | u32 value; |
| 297 | |
| 298 | /* Skip over the "W " part of the command */ |
| 299 | line = skip_space(line + 1); |
| 300 | |
| 301 | /* Figure out destination */ |
| 302 | switch (line[0]) { |
| 303 | case 'E': |
| 304 | case 'M': |
| 305 | dest = line[0]; |
| 306 | break; |
| 307 | default: |
| 308 | invalid_usage: |
| 309 | printf("ERROR: Invalid write usage\n"); |
| 310 | usage(); |
| 311 | return; |
| 312 | } |
| 313 | |
| 314 | /* Get the register to write */ |
| 315 | line = skip_space(line + 1); |
| 316 | reg = simple_strtoul(line, &endp, 16); |
| 317 | if (line == endp) |
| 318 | goto invalid_usage; |
| 319 | |
| 320 | /* Get the value to write */ |
| 321 | line = skip_space(endp); |
| 322 | value = simple_strtoul(line, &endp, 16); |
| 323 | if (line == endp) |
| 324 | goto invalid_usage; |
| 325 | |
| 326 | /* Check for trailing cruft */ |
| 327 | line = skip_space(endp); |
| 328 | if (line[0]) |
| 329 | goto invalid_usage; |
| 330 | |
| 331 | /* Finally, execute the command */ |
| 332 | if (dest == 'E') { |
| 333 | printf("Writing EEPROM register %02x with %02x\n", reg, value); |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 334 | write_eeprom_reg(dev, value, reg); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 335 | } else { |
| 336 | printf("Writing MAC register %02x with %08x\n", reg, value); |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 337 | smc911x_reg_write(dev, reg, value); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 338 | } |
| 339 | } |
| 340 | |
| 341 | /** |
| 342 | * copy_from_eeprom - copy MAC address in eeprom to address registers |
| 343 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 344 | static void copy_from_eeprom(struct eth_device *dev) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 345 | { |
| 346 | ulong addrl = |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 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; |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 351 | ulong addrh = |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 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); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 356 | puts("EEPROM contents copied to MAC\n"); |
| 357 | } |
| 358 | |
| 359 | /** |
| 360 | * print_macaddr - print MAC address registers and MAC address in eeprom |
| 361 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 362 | static void print_macaddr(struct eth_device *dev) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 363 | { |
| 364 | puts("Current MAC Address in MAC: "); |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 365 | ulong addrl = smc911x_get_mac_csr(dev, ADDRL); |
| 366 | ulong addrh = smc911x_get_mac_csr(dev, ADDRH); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 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)); |
| 370 | |
| 371 | puts("Current MAC Address in EEPROM: "); |
| 372 | int i; |
| 373 | for (i = 1; i < 6; ++i) |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 374 | printf("%02x:", read_eeprom_reg(dev, i)); |
| 375 | printf("%02x\n", read_eeprom_reg(dev, i)); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | /** |
| 379 | * dump_eeprom - dump the whole content of the EEPROM |
| 380 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 381 | static void dump_eeprom(struct eth_device *dev) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 382 | { |
| 383 | int i; |
| 384 | puts("EEPROM:\n"); |
| 385 | for (i = 0; i < 7; ++i) |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 386 | printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i)); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
| 390 | * smc911x_init - get the MAC/EEPROM up and ready for use |
| 391 | */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 392 | static int smc911x_init(struct eth_device *dev) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 393 | { |
| 394 | /* See if there is anything there */ |
Juergen Kilb | c9314d0 | 2010-06-12 21:22:01 +0200 | [diff] [blame] | 395 | if (smc911x_detect_chip(dev)) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 396 | return 1; |
| 397 | |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 398 | smc911x_reset(dev); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 399 | |
| 400 | /* Make sure we set EEDIO/EECLK to the EEPROM */ |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 401 | if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) { |
| 402 | while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 403 | if (smsc_ctrlc()) { |
Mike Frysinger | 78b5336 | 2009-02-25 17:29:40 -0500 | [diff] [blame] | 404 | printf("init: timeout (E2P_CMD = 0x%08x)\n", |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 405 | smc911x_reg_read(dev, E2P_CMD)); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 406 | return 1; |
| 407 | } |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 408 | smc911x_reg_write(dev, GPIO_CFG, |
| 409 | smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN); |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | return 0; |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | * getline - consume a line of input and handle some escape sequences |
| 417 | */ |
| 418 | static char *getline(void) |
| 419 | { |
| 420 | static char buffer[100]; |
| 421 | char c; |
| 422 | size_t i; |
| 423 | |
| 424 | i = 0; |
| 425 | while (1) { |
| 426 | buffer[i] = '\0'; |
| 427 | while (!tstc()) |
| 428 | continue; |
| 429 | |
| 430 | c = getc(); |
| 431 | /* Convert to uppercase */ |
| 432 | if (c >= 'a' && c <= 'z') |
| 433 | c -= ('a' - 'A'); |
| 434 | |
| 435 | switch (c) { |
| 436 | case '\r': /* Enter/Return key */ |
| 437 | case '\n': |
| 438 | puts("\n"); |
| 439 | return buffer; |
| 440 | |
| 441 | case 0x03: /* ^C - break */ |
| 442 | return NULL; |
| 443 | |
| 444 | case 0x5F: |
| 445 | case 0x08: /* ^H - backspace */ |
| 446 | case 0x7F: /* DEL - backspace */ |
| 447 | if (i) { |
| 448 | puts("\b \b"); |
| 449 | i--; |
| 450 | } |
| 451 | break; |
| 452 | |
| 453 | default: |
| 454 | /* Ignore control characters */ |
| 455 | if (c < 0x20) |
| 456 | break; |
| 457 | /* Queue up all other characters */ |
| 458 | buffer[i++] = c; |
| 459 | printf("%c", c); |
| 460 | break; |
| 461 | } |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | /** |
| 466 | * smc911x_eeprom - our application's main() function |
| 467 | */ |
Wolfgang Denk | 6262d021 | 2010-06-28 22:00:46 +0200 | [diff] [blame] | 468 | int smc911x_eeprom(int argc, char * const argv[]) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 469 | { |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 470 | /* Avoid initializing on stack as gcc likes to call memset() */ |
| 471 | struct eth_device dev; |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 472 | dev.iobase = CONFIG_SMC911X_BASE; |
| 473 | |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 474 | /* Print the ABI version */ |
| 475 | app_startup(argv); |
| 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"); |
| 480 | return 1; |
| 481 | } |
| 482 | |
| 483 | /* Initialize the MAC/EEPROM somewhat */ |
| 484 | puts("\n"); |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 485 | if (smc911x_init(&dev)) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 486 | return 1; |
| 487 | |
| 488 | /* Dump helpful usage information */ |
| 489 | puts("\n"); |
| 490 | usage(); |
| 491 | puts("\n"); |
| 492 | |
| 493 | while (1) { |
| 494 | char *line; |
| 495 | |
| 496 | /* Send the prompt and wait for a line */ |
| 497 | puts("eeprom> "); |
| 498 | line = getline(); |
| 499 | |
| 500 | /* Got a ctrl+c */ |
| 501 | if (!line) |
| 502 | return 0; |
| 503 | |
| 504 | /* Eat leading space */ |
| 505 | line = skip_space(line); |
| 506 | |
| 507 | /* Empty line, try again */ |
| 508 | if (!line[0]) |
| 509 | continue; |
| 510 | |
| 511 | /* Only accept 1 letter commands */ |
Jason Hobbs | cafa1aa | 2011-08-23 11:06:54 +0000 | [diff] [blame] | 512 | if (line[0] && line[1] && !isblank(line[1])) |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 513 | goto unknown_cmd; |
| 514 | |
| 515 | /* Now parse the command */ |
| 516 | switch (line[0]) { |
Mike Frysinger | 17e9012 | 2009-11-12 22:26:02 -0500 | [diff] [blame] | 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; |
Mike Frysinger | 96a1ef8 | 2008-12-22 02:56:07 -0500 | [diff] [blame] | 522 | unknown_cmd: |
| 523 | default: puts("ERROR: Unknown command!\n\n"); |
| 524 | case '?': |
| 525 | case 'H': usage(); break; |
| 526 | case 'Q': return 0; |
| 527 | } |
| 528 | } |
| 529 | } |