smc91111_eeprom.c 7.5 KB

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