smc911x_eeprom.c 8.2 KB

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