smc91111_eeprom.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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. #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
  34. #define EEPROM 0x1;
  35. #define MAC 0x2;
  36. #define UNKNOWN 0x4;
  37. void dump_reg (void);
  38. void dump_eeprom (void);
  39. int write_eeprom_reg (int, int);
  40. void copy_from_eeprom (void);
  41. void print_MAC (void);
  42. int read_eeprom_reg (int);
  43. void print_macaddr (void);
  44. int smc91111_eeprom (int argc, char *argv[])
  45. {
  46. int c, i, j, done, line, reg, value, start, what;
  47. char input[50];
  48. /* Print the ABI version */
  49. app_startup (argv);
  50. if (XF_VERSION != (int) get_version ()) {
  51. printf ("Expects ABI version %d\n", XF_VERSION);
  52. printf ("Actual U-Boot ABI version %d\n",
  53. (int) get_version ());
  54. printf ("Can't run\n\n");
  55. return (0);
  56. }
  57. asm ("p2.h = 0xFFC0;");
  58. asm ("p2.l = 0x0730;");
  59. asm ("r0 = 0x01;");
  60. asm ("w[p2] = r0;");
  61. asm ("ssync;");
  62. asm ("p2.h = 0xffc0;");
  63. asm ("p2.l = 0x0708;");
  64. asm ("r0 = 0x01;");
  65. asm ("w[p2] = r0;");
  66. asm ("ssync;");
  67. if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) {
  68. printf ("Can't find SMSC91111\n");
  69. return (0);
  70. }
  71. done = 0;
  72. what = UNKNOWN;
  73. printf ("\n");
  74. while (!done) {
  75. /* print the prompt */
  76. printf ("SMC91111> ");
  77. line = 0;
  78. i = 0;
  79. start = 1;
  80. while (!line) {
  81. /* Wait for a keystroke */
  82. while (!tstc ());
  83. c = getc ();
  84. /* Make Uppercase */
  85. if (c >= 'Z')
  86. c -= ('a' - 'A');
  87. /* printf(" |%02x| ",c); */
  88. switch (c) {
  89. case '\r': /* Enter */
  90. case '\n':
  91. input[i] = 0;
  92. puts ("\r\n");
  93. line = 1;
  94. break;
  95. case '\0': /* nul */
  96. continue;
  97. case 0x03: /* ^C - break */
  98. input[0] = 0;
  99. i = 0;
  100. line = 1;
  101. done = 1;
  102. break;
  103. case 0x5F:
  104. case 0x08: /* ^H - backspace */
  105. case 0x7F: /* DEL - backspace */
  106. if (i > 0) {
  107. puts ("\b \b");
  108. i--;
  109. }
  110. break;
  111. default:
  112. if (start) {
  113. if ((c == 'W') || (c == 'D')
  114. || (c == 'M') || (c == 'C')
  115. || (c == 'P')) {
  116. putc (c);
  117. input[i] = c;
  118. if (i <= 45)
  119. i++;
  120. start = 0;
  121. }
  122. } else {
  123. if ((c >= '0' && c <= '9')
  124. || (c >= 'A' && c <= 'F')
  125. || (c == 'E') || (c == 'M')
  126. || (c == ' ')) {
  127. putc (c);
  128. input[i] = c;
  129. if (i <= 45)
  130. i++;
  131. break;
  132. }
  133. }
  134. break;
  135. }
  136. }
  137. for (; i < 49; i++)
  138. input[i] = 0;
  139. switch (input[0]) {
  140. case ('W'):
  141. /* Line should be w reg value */
  142. i = 0;
  143. reg = 0;
  144. value = 0;
  145. /* Skip to the next space or end) */
  146. while ((input[i] != ' ') && (input[i] != 0))
  147. i++;
  148. if (input[i] != 0)
  149. i++;
  150. /* Are we writing to EEPROM or MAC */
  151. switch (input[i]) {
  152. case ('E'):
  153. what = EEPROM;
  154. break;
  155. case ('M'):
  156. what = MAC;
  157. break;
  158. default:
  159. what = UNKNOWN;
  160. break;
  161. }
  162. /* skip to the next space or end */
  163. while ((input[i] != ' ') && (input[i] != 0))
  164. i++;
  165. if (input[i] != 0)
  166. i++;
  167. /* Find register to write into */
  168. j = 0;
  169. while ((input[i] != ' ') && (input[i] != 0)) {
  170. j = input[i] - 0x30;
  171. if (j >= 0xA) {
  172. j -= 0x07;
  173. }
  174. reg = (reg * 0x10) + j;
  175. i++;
  176. }
  177. while ((input[i] != ' ') && (input[i] != 0))
  178. i++;
  179. if (input[i] != 0)
  180. i++;
  181. else
  182. what = UNKNOWN;
  183. /* Get the value to write */
  184. j = 0;
  185. while ((input[i] != ' ') && (input[i] != 0)) {
  186. j = input[i] - 0x30;
  187. if (j >= 0xA) {
  188. j -= 0x07;
  189. }
  190. value = (value * 0x10) + j;
  191. i++;
  192. }
  193. switch (what) {
  194. case 1:
  195. printf ("Writing EEPROM register %02x with %04x\n", reg, value);
  196. write_eeprom_reg (value, reg);
  197. break;
  198. case 2:
  199. printf ("Writing MAC register bank %i, reg %02x with %04x\n", reg >> 4, reg & 0xE, value);
  200. SMC_SELECT_BANK (reg >> 4);
  201. SMC_outw (value, reg & 0xE);
  202. break;
  203. default:
  204. printf ("Wrong\n");
  205. break;
  206. }
  207. break;
  208. case ('D'):
  209. dump_eeprom ();
  210. break;
  211. case ('M'):
  212. dump_reg ();
  213. break;
  214. case ('C'):
  215. copy_from_eeprom ();
  216. break;
  217. case ('P'):
  218. print_macaddr ();
  219. break;
  220. default:
  221. break;
  222. }
  223. }
  224. return (0);
  225. }
  226. void copy_from_eeprom (void)
  227. {
  228. int i;
  229. SMC_SELECT_BANK (1);
  230. SMC_outw ((SMC_inw (CTL_REG) & !CTL_EEPROM_SELECT) | CTL_RELOAD,
  231. CTL_REG);
  232. i = 100;
  233. while ((SMC_inw (CTL_REG) & CTL_RELOAD) && --i)
  234. udelay (100);
  235. if (i == 0) {
  236. printf ("Timeout Refreshing EEPROM registers\n");
  237. } else {
  238. printf ("EEPROM contents copied to MAC\n");
  239. }
  240. }
  241. void print_macaddr (void)
  242. {
  243. int i, j, k, mac[6];
  244. printf ("Current MAC Address in SMSC91111 ");
  245. SMC_SELECT_BANK (1);
  246. for (i = 0; i < 5; i++) {
  247. printf ("%02x:", SMC_inb (ADDR0_REG + i));
  248. }
  249. printf ("%02x\n", SMC_inb (ADDR0_REG + 5));
  250. i = 0;
  251. for (j = 0x20; j < 0x23; j++) {
  252. k = read_eeprom_reg (j);
  253. mac[i] = k & 0xFF;
  254. i++;
  255. mac[i] = k >> 8;
  256. i++;
  257. }
  258. printf ("Current MAC Address in EEPROM ");
  259. for (i = 0; i < 5; i++)
  260. printf ("%02x:", mac[i]);
  261. printf ("%02x\n", mac[5]);
  262. }
  263. void dump_eeprom (void)
  264. {
  265. int j, k;
  266. printf ("IOS2-0 ");
  267. for (j = 0; j < 8; j++) {
  268. printf ("%03x ", j);
  269. }
  270. printf ("\n");
  271. for (k = 0; k < 4; k++) {
  272. if (k == 0)
  273. printf ("CONFIG ");
  274. if (k == 1)
  275. printf ("BASE ");
  276. if ((k == 2) || (k == 3))
  277. printf (" ");
  278. for (j = 0; j < 0x20; j += 4) {
  279. printf ("%02x:%04x ", j + k, read_eeprom_reg (j + k));
  280. }
  281. printf ("\n");
  282. }
  283. for (j = 0x20; j < 0x40; j++) {
  284. if ((j & 0x07) == 0)
  285. printf ("\n");
  286. printf ("%02x:%04x ", j, read_eeprom_reg (j));
  287. }
  288. printf ("\n");
  289. }
  290. int read_eeprom_reg (int reg)
  291. {
  292. int timeout;
  293. SMC_SELECT_BANK (2);
  294. SMC_outw (reg, PTR_REG);
  295. SMC_SELECT_BANK (1);
  296. SMC_outw (SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
  297. CTL_REG);
  298. timeout = 100;
  299. while ((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout)
  300. udelay (100);
  301. if (timeout == 0) {
  302. printf ("Timeout Reading EEPROM register %02x\n", reg);
  303. return 0;
  304. }
  305. return SMC_inw (GP_REG);
  306. }
  307. int write_eeprom_reg (int value, int reg)
  308. {
  309. int timeout;
  310. SMC_SELECT_BANK (2);
  311. SMC_outw (reg, PTR_REG);
  312. SMC_SELECT_BANK (1);
  313. SMC_outw (value, GP_REG);
  314. SMC_outw (SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
  315. timeout = 100;
  316. while ((SMC_inw (CTL_REG) & CTL_STORE) && --timeout)
  317. udelay (100);
  318. if (timeout == 0) {
  319. printf ("Timeout Writing EEPROM register %02x\n", reg);
  320. return 0;
  321. }
  322. return 1;
  323. }
  324. void dump_reg (void)
  325. {
  326. int i, j;
  327. printf (" ");
  328. for (j = 0; j < 4; j++) {
  329. printf ("Bank%i ", j);
  330. }
  331. printf ("\n");
  332. for (i = 0; i < 0xF; i += 2) {
  333. printf ("%02x ", i);
  334. for (j = 0; j < 4; j++) {
  335. SMC_SELECT_BANK (j);
  336. printf ("%04x ", SMC_inw (i));
  337. }
  338. printf ("\n");
  339. }
  340. }
  341. #else
  342. int smc91111_eeprom (int argc, char *argv[])
  343. {
  344. printf("Not supported for this board\n");
  345. return 1;
  346. }
  347. #endif