smc911x_eeprom.c 8.2 KB

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