PageRenderTime 48ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/examples/smc91111_eeprom.c

https://github.com/oSaiYa/u-boot-sh4-1.3.1_stm23_0051
C | 389 lines | 300 code | 50 blank | 39 comment | 99 complexity | c77f55184d51d814209c4457cf9b8ed1 MD5 | raw file
Possible License(s): AGPL-1.0
  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. #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", reg, value);
  195. write_eeprom_reg (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 (reg >> 4);
  200. SMC_outw (value, reg & 0xE);
  201. break;
  202. default:
  203. printf ("Wrong\n");
  204. break;
  205. }
  206. break;
  207. case ('D'):
  208. dump_eeprom ();
  209. break;
  210. case ('M'):
  211. dump_reg ();
  212. break;
  213. case ('C'):
  214. copy_from_eeprom ();
  215. break;
  216. case ('P'):
  217. print_macaddr ();
  218. break;
  219. default:
  220. break;
  221. }
  222. }
  223. return (0);
  224. }
  225. void copy_from_eeprom (void)
  226. {
  227. int i;
  228. SMC_SELECT_BANK (1);
  229. SMC_outw ((SMC_inw (CTL_REG) & !CTL_EEPROM_SELECT) | CTL_RELOAD,
  230. CTL_REG);
  231. i = 100;
  232. while ((SMC_inw (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 (void)
  241. {
  242. int i, j, k, mac[6];
  243. printf ("Current MAC Address in SMSC91111 ");
  244. SMC_SELECT_BANK (1);
  245. for (i = 0; i < 5; i++) {
  246. printf ("%02x:", SMC_inb (ADDR0_REG + i));
  247. }
  248. printf ("%02x\n", SMC_inb (ADDR0_REG + 5));
  249. i = 0;
  250. for (j = 0x20; j < 0x23; j++) {
  251. k = read_eeprom_reg (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 (void)
  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, read_eeprom_reg (j + k));
  279. }
  280. printf ("\n");
  281. }
  282. for (j = 0x20; j < 0x40; j++) {
  283. if ((j & 0x07) == 0)
  284. printf ("\n");
  285. printf ("%02x:%04x ", j, read_eeprom_reg (j));
  286. }
  287. printf ("\n");
  288. }
  289. int read_eeprom_reg (int reg)
  290. {
  291. int timeout;
  292. SMC_SELECT_BANK (2);
  293. SMC_outw (reg, PTR_REG);
  294. SMC_SELECT_BANK (1);
  295. SMC_outw (SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
  296. CTL_REG);
  297. timeout = 100;
  298. while ((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout)
  299. udelay (100);
  300. if (timeout == 0) {
  301. printf ("Timeout Reading EEPROM register %02x\n", reg);
  302. return 0;
  303. }
  304. return SMC_inw (GP_REG);
  305. }
  306. int write_eeprom_reg (int value, int reg)
  307. {
  308. int timeout;
  309. SMC_SELECT_BANK (2);
  310. SMC_outw (reg, PTR_REG);
  311. SMC_SELECT_BANK (1);
  312. SMC_outw (value, GP_REG);
  313. SMC_outw (SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
  314. timeout = 100;
  315. while ((SMC_inw (CTL_REG) & CTL_STORE) && --timeout)
  316. udelay (100);
  317. if (timeout == 0) {
  318. printf ("Timeout Writing EEPROM register %02x\n", reg);
  319. return 0;
  320. }
  321. return 1;
  322. }
  323. void dump_reg (void)
  324. {
  325. int i, j;
  326. printf (" ");
  327. for (j = 0; j < 4; j++) {
  328. printf ("Bank%i ", j);
  329. }
  330. printf ("\n");
  331. for (i = 0; i < 0xF; i += 2) {
  332. printf ("%02x ", i);
  333. for (j = 0; j < 4; j++) {
  334. SMC_SELECT_BANK (j);
  335. printf ("%04x ", SMC_inw (i));
  336. }
  337. printf ("\n");
  338. }
  339. }