smc91111_eeprom.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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. /* the smc91111.h gets base addr through eth_device' iobase */
  32. struct eth_device { unsigned long iobase; };
  33. #include "../drivers/net/smc91111.h"
  34. #ifdef CONFIG_SMC91111
  35. #ifndef SMC91111_EEPROM_INIT
  36. # define SMC91111_EEPROM_INIT()
  37. #endif
  38. #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
  39. #define EEPROM 0x1
  40. #define MAC 0x2
  41. #define UNKNOWN 0x4
  42. void dump_reg (struct eth_device *dev);
  43. void dump_eeprom (struct eth_device *dev);
  44. int write_eeprom_reg (struct eth_device *dev, int value, int reg);
  45. void copy_from_eeprom (struct eth_device *dev);
  46. void print_MAC (struct eth_device *dev);
  47. int read_eeprom_reg (struct eth_device *dev, int reg);
  48. void print_macaddr (struct eth_device *dev);
  49. int smc91111_eeprom (int argc, char *argv[])
  50. {
  51. int c, i, j, done, line, reg, value, start, what;
  52. char input[50];
  53. struct eth_device dev = {
  54. .iobase = CONFIG_SMC91111_BASE
  55. };
  56. /* Print the ABI version */
  57. app_startup (argv);
  58. if (XF_VERSION != (int) get_version ()) {
  59. printf ("Expects ABI version %d\n", XF_VERSION);
  60. printf ("Actual U-Boot ABI version %d\n",
  61. (int) get_version ());
  62. printf ("Can't run\n\n");
  63. return (0);
  64. }
  65. SMC91111_EEPROM_INIT();
  66. if ((SMC_inw (&dev, 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", reg, value);
  195. write_eeprom_reg (&dev, value, reg);
  196. break;
  197. case 2:
  198. printf ("Writing MAC register bank %i, reg %02x with %04x\n", reg >> 4, reg & 0xE, value);
  199. SMC_SELECT_BANK (&dev, reg >> 4);
  200. SMC_outw (&dev, value, reg & 0xE);
  201. break;
  202. default:
  203. printf ("Wrong\n");
  204. break;
  205. }
  206. break;
  207. case ('D'):
  208. dump_eeprom (&dev);
  209. break;
  210. case ('M'):
  211. dump_reg (&dev);
  212. break;
  213. case ('C'):
  214. copy_from_eeprom (&dev);
  215. break;
  216. case ('P'):
  217. print_macaddr (&dev);
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. return (0);
  224. }
  225. void copy_from_eeprom (struct eth_device *dev)
  226. {
  227. int i;
  228. SMC_SELECT_BANK (dev, 1);
  229. SMC_outw (dev, (SMC_inw (dev, CTL_REG) & !CTL_EEPROM_SELECT) |
  230. CTL_RELOAD, CTL_REG);
  231. i = 100;
  232. while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --i)
  233. udelay (100);
  234. if (i == 0) {
  235. printf ("Timeout Refreshing EEPROM registers\n");
  236. } else {
  237. printf ("EEPROM contents copied to MAC\n");
  238. }
  239. }
  240. void print_macaddr (struct eth_device *dev)
  241. {
  242. int i, j, k, mac[6];
  243. printf ("Current MAC Address in SMSC91111 ");
  244. SMC_SELECT_BANK (dev, 1);
  245. for (i = 0; i < 5; i++) {
  246. printf ("%02x:", SMC_inb (dev, ADDR0_REG + i));
  247. }
  248. printf ("%02x\n", SMC_inb (dev, ADDR0_REG + 5));
  249. i = 0;
  250. for (j = 0x20; j < 0x23; j++) {
  251. k = read_eeprom_reg (dev, j);
  252. mac[i] = k & 0xFF;
  253. i++;
  254. mac[i] = k >> 8;
  255. i++;
  256. }
  257. printf ("Current MAC Address in EEPROM ");
  258. for (i = 0; i < 5; i++)
  259. printf ("%02x:", mac[i]);
  260. printf ("%02x\n", mac[5]);
  261. }
  262. void dump_eeprom (struct eth_device *dev)
  263. {
  264. int j, k;
  265. printf ("IOS2-0 ");
  266. for (j = 0; j < 8; j++) {
  267. printf ("%03x ", j);
  268. }
  269. printf ("\n");
  270. for (k = 0; k < 4; k++) {
  271. if (k == 0)
  272. printf ("CONFIG ");
  273. if (k == 1)
  274. printf ("BASE ");
  275. if ((k == 2) || (k == 3))
  276. printf (" ");
  277. for (j = 0; j < 0x20; j += 4) {
  278. printf ("%02x:%04x ", j + k,
  279. read_eeprom_reg (dev, 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 (dev, j));
  287. }
  288. printf ("\n");
  289. }
  290. int read_eeprom_reg (struct eth_device *dev, int reg)
  291. {
  292. int timeout;
  293. SMC_SELECT_BANK (dev, 2);
  294. SMC_outw (dev, reg, PTR_REG);
  295. SMC_SELECT_BANK (dev, 1);
  296. SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
  297. CTL_RELOAD, CTL_REG);
  298. timeout = 100;
  299. while ((SMC_inw (dev, 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 (dev, GP_REG);
  306. }
  307. int write_eeprom_reg (struct eth_device *dev, int value, int reg)
  308. {
  309. int timeout;
  310. SMC_SELECT_BANK (dev, 2);
  311. SMC_outw (dev, reg, PTR_REG);
  312. SMC_SELECT_BANK (dev, 1);
  313. SMC_outw (dev, value, GP_REG);
  314. SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
  315. CTL_STORE, CTL_REG);
  316. timeout = 100;
  317. while ((SMC_inw (dev, 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 (struct eth_device *dev)
  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 (dev, j);
  337. printf ("%04x ", SMC_inw (dev, i));
  338. }
  339. printf ("\n");
  340. }
  341. }
  342. #else
  343. int smc91111_eeprom (int argc, char *argv[])
  344. {
  345. printf("Not supported for this board\n");
  346. return 1;
  347. }
  348. #endif