smc911x_eeprom.c 8.2 KB

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