smc91111_eeprom.c 7.9 KB

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