smc91111_eeprom.c 7.6 KB

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