smc911x_eeprom.c 8.4 KB

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