smc91111_eeprom.c 7.9 KB

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