1 /* 2 * smc911x_eeprom.c - EEPROM interface to SMC911x parts. 3 * Only tested on SMSC9118 though ... 4 * 5 * Copyright 2004-2009 Analog Devices Inc. 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 12 * Das U-boot 13 * - Ladislav Michl ladis@linux-mips.org 14 * A rejected patch on the U-Boot mailing list 15 */ 16 17 #include <common.h> 18 #include <exports.h> 19 #include <linux/ctype.h> 20 #include "../drivers/net/smc911x.h" 21 22 /** 23 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?) 24 */ 25 static int smsc_ctrlc(void) 26 { 27 return (tstc() && getc() == 0x03); 28 } 29 30 /** 31 * usage - dump usage information 32 */ 33 static void usage(void) 34 { 35 puts( 36 "MAC/EEPROM Commands:\n" 37 " P : Print the MAC addresses\n" 38 " D : Dump the EEPROM contents\n" 39 " M : Dump the MAC contents\n" 40 " C : Copy the MAC address from the EEPROM to the MAC\n" 41 " W : Write a register in the EEPROM or in the MAC\n" 42 " Q : Quit\n" 43 "\n" 44 "Some commands take arguments:\n" 45 " W <E|M> <register> <value>\n" 46 " E: EEPROM M: MAC\n" 47 ); 48 } 49 50 /** 51 * dump_regs - dump the MAC registers 52 * 53 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers 54 * and they're all 32bits long. 0xB8+ are reserved, so don't bother. 55 */ 56 static void dump_regs(struct eth_device *dev) 57 { 58 u8 i, j = 0; 59 for (i = 0x50; i < 0xB8; i += sizeof(u32)) 60 printf("%02x: 0x%08x %c", i, 61 smc911x_reg_read(dev, i), 62 (j++ % 2 ? '\n' : ' ')); 63 } 64 65 /** 66 * do_eeprom_cmd - handle eeprom communication 67 */ 68 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg) 69 { 70 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) { 71 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n", 72 smc911x_reg_read(dev, E2P_CMD)); 73 return -1; 74 } 75 76 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg); 77 78 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) 79 if (smsc_ctrlc()) { 80 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n", 81 smc911x_reg_read(dev, E2P_CMD)); 82 return -1; 83 } 84 85 return 0; 86 } 87 88 /** 89 * read_eeprom_reg - read specified register in EEPROM 90 */ 91 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg) 92 { 93 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg); 94 return (ret ? : smc911x_reg_read(dev, E2P_DATA)); 95 } 96 97 /** 98 * write_eeprom_reg - write specified value into specified register in EEPROM 99 */ 100 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg) 101 { 102 int ret; 103 104 /* enable erasing/writing */ 105 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg); 106 if (ret) 107 goto done; 108 109 /* erase the eeprom reg */ 110 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg); 111 if (ret) 112 goto done; 113 114 /* write the eeprom reg */ 115 smc911x_reg_write(dev, E2P_DATA, value); 116 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg); 117 if (ret) 118 goto done; 119 120 /* disable erasing/writing */ 121 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg); 122 123 done: 124 return ret; 125 } 126 127 /** 128 * skip_space - find first non-whitespace in given pointer 129 */ 130 static char *skip_space(char *buf) 131 { 132 while (isblank(buf[0])) 133 ++buf; 134 return buf; 135 } 136 137 /** 138 * write_stuff - handle writing of MAC registers / eeprom 139 */ 140 static void write_stuff(struct eth_device *dev, char *line) 141 { 142 char dest; 143 char *endp; 144 u8 reg; 145 u32 value; 146 147 /* Skip over the "W " part of the command */ 148 line = skip_space(line + 1); 149 150 /* Figure out destination */ 151 switch (line[0]) { 152 case 'E': 153 case 'M': 154 dest = line[0]; 155 break; 156 default: 157 invalid_usage: 158 printf("ERROR: Invalid write usage\n"); 159 usage(); 160 return; 161 } 162 163 /* Get the register to write */ 164 line = skip_space(line + 1); 165 reg = simple_strtoul(line, &endp, 16); 166 if (line == endp) 167 goto invalid_usage; 168 169 /* Get the value to write */ 170 line = skip_space(endp); 171 value = simple_strtoul(line, &endp, 16); 172 if (line == endp) 173 goto invalid_usage; 174 175 /* Check for trailing cruft */ 176 line = skip_space(endp); 177 if (line[0]) 178 goto invalid_usage; 179 180 /* Finally, execute the command */ 181 if (dest == 'E') { 182 printf("Writing EEPROM register %02x with %02x\n", reg, value); 183 write_eeprom_reg(dev, value, reg); 184 } else { 185 printf("Writing MAC register %02x with %08x\n", reg, value); 186 smc911x_reg_write(dev, reg, value); 187 } 188 } 189 190 /** 191 * copy_from_eeprom - copy MAC address in eeprom to address registers 192 */ 193 static void copy_from_eeprom(struct eth_device *dev) 194 { 195 ulong addrl = 196 read_eeprom_reg(dev, 0x01) | 197 read_eeprom_reg(dev, 0x02) << 8 | 198 read_eeprom_reg(dev, 0x03) << 16 | 199 read_eeprom_reg(dev, 0x04) << 24; 200 ulong addrh = 201 read_eeprom_reg(dev, 0x05) | 202 read_eeprom_reg(dev, 0x06) << 8; 203 smc911x_set_mac_csr(dev, ADDRL, addrl); 204 smc911x_set_mac_csr(dev, ADDRH, addrh); 205 puts("EEPROM contents copied to MAC\n"); 206 } 207 208 /** 209 * print_macaddr - print MAC address registers and MAC address in eeprom 210 */ 211 static void print_macaddr(struct eth_device *dev) 212 { 213 puts("Current MAC Address in MAC: "); 214 ulong addrl = smc911x_get_mac_csr(dev, ADDRL); 215 ulong addrh = smc911x_get_mac_csr(dev, ADDRH); 216 printf("%02x:%02x:%02x:%02x:%02x:%02x\n", 217 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16), 218 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8)); 219 220 puts("Current MAC Address in EEPROM: "); 221 int i; 222 for (i = 1; i < 6; ++i) 223 printf("%02x:", read_eeprom_reg(dev, i)); 224 printf("%02x\n", read_eeprom_reg(dev, i)); 225 } 226 227 /** 228 * dump_eeprom - dump the whole content of the EEPROM 229 */ 230 static void dump_eeprom(struct eth_device *dev) 231 { 232 int i; 233 puts("EEPROM:\n"); 234 for (i = 0; i < 7; ++i) 235 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i)); 236 } 237 238 /** 239 * smc911x_init - get the MAC/EEPROM up and ready for use 240 */ 241 static int smc911x_init(struct eth_device *dev) 242 { 243 /* See if there is anything there */ 244 if (smc911x_detect_chip(dev)) 245 return 1; 246 247 smc911x_reset(dev); 248 249 /* Make sure we set EEDIO/EECLK to the EEPROM */ 250 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) { 251 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) 252 if (smsc_ctrlc()) { 253 printf("init: timeout (E2P_CMD = 0x%08x)\n", 254 smc911x_reg_read(dev, E2P_CMD)); 255 return 1; 256 } 257 smc911x_reg_write(dev, GPIO_CFG, 258 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN); 259 } 260 261 return 0; 262 } 263 264 /** 265 * getline - consume a line of input and handle some escape sequences 266 */ 267 static char *getline(void) 268 { 269 static char buffer[100]; 270 char c; 271 size_t i; 272 273 i = 0; 274 while (1) { 275 buffer[i] = '\0'; 276 while (!tstc()) 277 continue; 278 279 c = getc(); 280 /* Convert to uppercase */ 281 if (c >= 'a' && c <= 'z') 282 c -= ('a' - 'A'); 283 284 switch (c) { 285 case '\r': /* Enter/Return key */ 286 case '\n': 287 puts("\n"); 288 return buffer; 289 290 case 0x03: /* ^C - break */ 291 return NULL; 292 293 case 0x5F: 294 case 0x08: /* ^H - backspace */ 295 case 0x7F: /* DEL - backspace */ 296 if (i) { 297 puts("\b \b"); 298 i--; 299 } 300 break; 301 302 default: 303 /* Ignore control characters */ 304 if (c < 0x20) 305 break; 306 /* Queue up all other characters */ 307 buffer[i++] = c; 308 printf("%c", c); 309 break; 310 } 311 } 312 } 313 314 /** 315 * smc911x_eeprom - our application's main() function 316 */ 317 int smc911x_eeprom(int argc, char * const argv[]) 318 { 319 /* Avoid initializing on stack as gcc likes to call memset() */ 320 struct eth_device dev; 321 dev.iobase = CONFIG_SMC911X_BASE; 322 323 /* Print the ABI version */ 324 app_startup(argv); 325 if (XF_VERSION != get_version()) { 326 printf("Expects ABI version %d\n", XF_VERSION); 327 printf("Actual U-Boot ABI version %lu\n", get_version()); 328 printf("Can't run\n\n"); 329 return 1; 330 } 331 332 /* Initialize the MAC/EEPROM somewhat */ 333 puts("\n"); 334 if (smc911x_init(&dev)) 335 return 1; 336 337 /* Dump helpful usage information */ 338 puts("\n"); 339 usage(); 340 puts("\n"); 341 342 while (1) { 343 char *line; 344 345 /* Send the prompt and wait for a line */ 346 puts("eeprom> "); 347 line = getline(); 348 349 /* Got a ctrl+c */ 350 if (!line) 351 return 0; 352 353 /* Eat leading space */ 354 line = skip_space(line); 355 356 /* Empty line, try again */ 357 if (!line[0]) 358 continue; 359 360 /* Only accept 1 letter commands */ 361 if (line[0] && line[1] && !isblank(line[1])) 362 goto unknown_cmd; 363 364 /* Now parse the command */ 365 switch (line[0]) { 366 case 'W': write_stuff(&dev, line); break; 367 case 'D': dump_eeprom(&dev); break; 368 case 'M': dump_regs(&dev); break; 369 case 'C': copy_from_eeprom(&dev); break; 370 case 'P': print_macaddr(&dev); break; 371 unknown_cmd: 372 default: puts("ERROR: Unknown command!\n\n"); 373 case '?': 374 case 'H': usage(); break; 375 case 'Q': return 0; 376 } 377 } 378 } 379