PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/standalone/smc91111_eeprom.c

https://github.com/Vodalys/u-boot-fslc
C | 372 lines | 298 code | 51 blank | 23 comment | 99 complexity | d4aeeb06526e4ad1651524315ff9bb78 MD5 | raw file
Possible License(s): LGPL-2.1, BSD-2-Clause, GPL-2.0, LGPL-2.0
  1. /*
  2. * (C) Copyright 2004
  3. * Robin Getz rgetz@blacfin.uclinux.org
  4. *
  5. * SPDX-License-Identifier: GPL-2.0+
  6. *
  7. * Heavily borrowed from the following peoples GPL'ed software:
  8. * - Wolfgang Denk, DENX Software Engineering, wd@denx.de
  9. * Das U-boot
  10. * - Ladislav Michl ladis@linux-mips.org
  11. * A rejected patch on the U-Boot mailing list
  12. */
  13. #include <common.h>
  14. #include <exports.h>
  15. #include "../drivers/net/smc91111.h"
  16. #ifndef SMC91111_EEPROM_INIT
  17. # define SMC91111_EEPROM_INIT()
  18. #endif
  19. #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
  20. #define EEPROM 0x1
  21. #define MAC 0x2
  22. #define UNKNOWN 0x4
  23. void dump_reg (struct eth_device *dev);
  24. void dump_eeprom (struct eth_device *dev);
  25. int write_eeprom_reg (struct eth_device *dev, int value, int reg);
  26. void copy_from_eeprom (struct eth_device *dev);
  27. void print_MAC (struct eth_device *dev);
  28. int read_eeprom_reg (struct eth_device *dev, int reg);
  29. void print_macaddr (struct eth_device *dev);
  30. int smc91111_eeprom (int argc, char * const argv[])
  31. {
  32. int c, i, j, done, line, reg, value, start, what;
  33. char input[50];
  34. struct eth_device dev;
  35. dev.iobase = CONFIG_SMC91111_BASE;
  36. /* Print the ABI version */
  37. app_startup (argv);
  38. if (XF_VERSION != (int) get_version ()) {
  39. printf ("Expects ABI version %d\n", XF_VERSION);
  40. printf ("Actual U-Boot ABI version %d\n",
  41. (int) get_version ());
  42. printf ("Can't run\n\n");
  43. return (0);
  44. }
  45. SMC91111_EEPROM_INIT();
  46. if ((SMC_inw (&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
  47. printf ("Can't find SMSC91111\n");
  48. return (0);
  49. }
  50. done = 0;
  51. what = UNKNOWN;
  52. printf ("\n");
  53. while (!done) {
  54. /* print the prompt */
  55. printf ("SMC91111> ");
  56. line = 0;
  57. i = 0;
  58. start = 1;
  59. while (!line) {
  60. /* Wait for a keystroke */
  61. while (!tstc ());
  62. c = getc ();
  63. /* Make Uppercase */
  64. if (c >= 'Z')
  65. c -= ('a' - 'A');
  66. /* printf(" |%02x| ",c); */
  67. switch (c) {
  68. case '\r': /* Enter */
  69. case '\n':
  70. input[i] = 0;
  71. puts ("\r\n");
  72. line = 1;
  73. break;
  74. case '\0': /* nul */
  75. continue;
  76. case 0x03: /* ^C - break */
  77. input[0] = 0;
  78. i = 0;
  79. line = 1;
  80. done = 1;
  81. break;
  82. case 0x5F:
  83. case 0x08: /* ^H - backspace */
  84. case 0x7F: /* DEL - backspace */
  85. if (i > 0) {
  86. puts ("\b \b");
  87. i--;
  88. }
  89. break;
  90. default:
  91. if (start) {
  92. if ((c == 'W') || (c == 'D')
  93. || (c == 'M') || (c == 'C')
  94. || (c == 'P')) {
  95. putc (c);
  96. input[i] = c;
  97. if (i <= 45)
  98. i++;
  99. start = 0;
  100. }
  101. } else {
  102. if ((c >= '0' && c <= '9')
  103. || (c >= 'A' && c <= 'F')
  104. || (c == 'E') || (c == 'M')
  105. || (c == ' ')) {
  106. putc (c);
  107. input[i] = c;
  108. if (i <= 45)
  109. i++;
  110. break;
  111. }
  112. }
  113. break;
  114. }
  115. }
  116. for (; i < 49; i++)
  117. input[i] = 0;
  118. switch (input[0]) {
  119. case ('W'):
  120. /* Line should be w reg value */
  121. i = 0;
  122. reg = 0;
  123. value = 0;
  124. /* Skip to the next space or end) */
  125. while ((input[i] != ' ') && (input[i] != 0))
  126. i++;
  127. if (input[i] != 0)
  128. i++;
  129. /* Are we writing to EEPROM or MAC */
  130. switch (input[i]) {
  131. case ('E'):
  132. what = EEPROM;
  133. break;
  134. case ('M'):
  135. what = MAC;
  136. break;
  137. default:
  138. what = UNKNOWN;
  139. break;
  140. }
  141. /* skip to the next space or end */
  142. while ((input[i] != ' ') && (input[i] != 0))
  143. i++;
  144. if (input[i] != 0)
  145. i++;
  146. /* Find register to write into */
  147. j = 0;
  148. while ((input[i] != ' ') && (input[i] != 0)) {
  149. j = input[i] - 0x30;
  150. if (j >= 0xA) {
  151. j -= 0x07;
  152. }
  153. reg = (reg * 0x10) + j;
  154. i++;
  155. }
  156. while ((input[i] != ' ') && (input[i] != 0))
  157. i++;
  158. if (input[i] != 0)
  159. i++;
  160. else
  161. what = UNKNOWN;
  162. /* Get the value to write */
  163. j = 0;
  164. while ((input[i] != ' ') && (input[i] != 0)) {
  165. j = input[i] - 0x30;
  166. if (j >= 0xA) {
  167. j -= 0x07;
  168. }
  169. value = (value * 0x10) + j;
  170. i++;
  171. }
  172. switch (what) {
  173. case 1:
  174. printf ("Writing EEPROM register %02x with %04x\n", reg, value);
  175. write_eeprom_reg (&dev, value, reg);
  176. break;
  177. case 2:
  178. printf ("Writing MAC register bank %i, reg %02x with %04x\n", reg >> 4, reg & 0xE, value);
  179. SMC_SELECT_BANK (&dev, reg >> 4);
  180. SMC_outw (&dev, value, reg & 0xE);
  181. break;
  182. default:
  183. printf ("Wrong\n");
  184. break;
  185. }
  186. break;
  187. case ('D'):
  188. dump_eeprom (&dev);
  189. break;
  190. case ('M'):
  191. dump_reg (&dev);
  192. break;
  193. case ('C'):
  194. copy_from_eeprom (&dev);
  195. break;
  196. case ('P'):
  197. print_macaddr (&dev);
  198. break;
  199. default:
  200. break;
  201. }
  202. }
  203. return (0);
  204. }
  205. void copy_from_eeprom (struct eth_device *dev)
  206. {
  207. int i;
  208. SMC_SELECT_BANK (dev, 1);
  209. SMC_outw (dev, (SMC_inw (dev, CTL_REG) & !CTL_EEPROM_SELECT) |
  210. CTL_RELOAD, CTL_REG);
  211. i = 100;
  212. while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --i)
  213. udelay (100);
  214. if (i == 0) {
  215. printf ("Timeout Refreshing EEPROM registers\n");
  216. } else {
  217. printf ("EEPROM contents copied to MAC\n");
  218. }
  219. }
  220. void print_macaddr (struct eth_device *dev)
  221. {
  222. int i, j, k, mac[6];
  223. printf ("Current MAC Address in SMSC91111 ");
  224. SMC_SELECT_BANK (dev, 1);
  225. for (i = 0; i < 5; i++) {
  226. printf ("%02x:", SMC_inb (dev, ADDR0_REG + i));
  227. }
  228. printf ("%02x\n", SMC_inb (dev, ADDR0_REG + 5));
  229. i = 0;
  230. for (j = 0x20; j < 0x23; j++) {
  231. k = read_eeprom_reg (dev, j);
  232. mac[i] = k & 0xFF;
  233. i++;
  234. mac[i] = k >> 8;
  235. i++;
  236. }
  237. printf ("Current MAC Address in EEPROM ");
  238. for (i = 0; i < 5; i++)
  239. printf ("%02x:", mac[i]);
  240. printf ("%02x\n", mac[5]);
  241. }
  242. void dump_eeprom (struct eth_device *dev)
  243. {
  244. int j, k;
  245. printf ("IOS2-0 ");
  246. for (j = 0; j < 8; j++) {
  247. printf ("%03x ", j);
  248. }
  249. printf ("\n");
  250. for (k = 0; k < 4; k++) {
  251. if (k == 0)
  252. printf ("CONFIG ");
  253. if (k == 1)
  254. printf ("BASE ");
  255. if ((k == 2) || (k == 3))
  256. printf (" ");
  257. for (j = 0; j < 0x20; j += 4) {
  258. printf ("%02x:%04x ", j + k,
  259. read_eeprom_reg (dev, j + k));
  260. }
  261. printf ("\n");
  262. }
  263. for (j = 0x20; j < 0x40; j++) {
  264. if ((j & 0x07) == 0)
  265. printf ("\n");
  266. printf ("%02x:%04x ", j, read_eeprom_reg (dev, j));
  267. }
  268. printf ("\n");
  269. }
  270. int read_eeprom_reg (struct eth_device *dev, int reg)
  271. {
  272. int timeout;
  273. SMC_SELECT_BANK (dev, 2);
  274. SMC_outw (dev, reg, PTR_REG);
  275. SMC_SELECT_BANK (dev, 1);
  276. SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
  277. CTL_RELOAD, CTL_REG);
  278. timeout = 100;
  279. while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --timeout)
  280. udelay (100);
  281. if (timeout == 0) {
  282. printf ("Timeout Reading EEPROM register %02x\n", reg);
  283. return 0;
  284. }
  285. return SMC_inw (dev, GP_REG);
  286. }
  287. int write_eeprom_reg (struct eth_device *dev, int value, int reg)
  288. {
  289. int timeout;
  290. SMC_SELECT_BANK (dev, 2);
  291. SMC_outw (dev, reg, PTR_REG);
  292. SMC_SELECT_BANK (dev, 1);
  293. SMC_outw (dev, value, GP_REG);
  294. SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
  295. CTL_STORE, CTL_REG);
  296. timeout = 100;
  297. while ((SMC_inw (dev, CTL_REG) & CTL_STORE) && --timeout)
  298. udelay (100);
  299. if (timeout == 0) {
  300. printf ("Timeout Writing EEPROM register %02x\n", reg);
  301. return 0;
  302. }
  303. return 1;
  304. }
  305. void dump_reg (struct eth_device *dev)
  306. {
  307. int i, j;
  308. printf (" ");
  309. for (j = 0; j < 4; j++) {
  310. printf ("Bank%i ", j);
  311. }
  312. printf ("\n");
  313. for (i = 0; i < 0xF; i += 2) {
  314. printf ("%02x ", i);
  315. for (j = 0; j < 4; j++) {
  316. SMC_SELECT_BANK (dev, j);
  317. printf ("%04x ", SMC_inw (dev, i));
  318. }
  319. printf ("\n");
  320. }
  321. }