MINI2440: Updated early nand loader
[u-boot-openmoko/mini2440.git] / examples / smc91111_eeprom.c
blobf5d8c6ab42c16799335d6d87b1f3ef11eadf95eb
1 /*
2 * (C) Copyright 2004
3 * Robin Getz rgetz@blacfin.uclinux.org
5 * See file CREDITS for list of people who contributed to this
6 * project.
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.
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.
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
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
30 #include <common.h>
31 #include <exports.h>
32 #include "../drivers/net/smc91111.h"
34 #ifdef CONFIG_DRIVER_SMC91111
36 #ifdef pFIO0_DIR
37 # define pFIO_DIR pFIO0_DIR
38 # define pFIO_FLAG_S pFIO0_FLAG_S
39 #endif
41 #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
42 #define EEPROM 0x1;
43 #define MAC 0x2;
44 #define UNKNOWN 0x4;
46 void dump_reg (void);
47 void dump_eeprom (void);
48 int write_eeprom_reg (int, int);
49 void copy_from_eeprom (void);
50 void print_MAC (void);
51 int read_eeprom_reg (int);
52 void print_macaddr (void);
54 int smc91111_eeprom (int argc, char *argv[])
56 int c, i, j, done, line, reg, value, start, what;
57 char input[50];
59 /* Print the ABI version */
60 app_startup (argv);
61 if (XF_VERSION != (int) get_version ()) {
62 printf ("Expects ABI version %d\n", XF_VERSION);
63 printf ("Actual U-Boot ABI version %d\n",
64 (int) get_version ());
65 printf ("Can't run\n\n");
66 return (0);
69 *pFIO_DIR = 0x01;
70 *pFIO_FLAG_S = 0x01;
71 SSYNC();
73 if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) {
74 printf ("Can't find SMSC91111\n");
75 return (0);
78 done = 0;
79 what = UNKNOWN;
80 printf ("\n");
81 while (!done) {
82 /* print the prompt */
83 printf ("SMC91111> ");
84 line = 0;
85 i = 0;
86 start = 1;
87 while (!line) {
88 /* Wait for a keystroke */
89 while (!tstc ());
91 c = getc ();
92 /* Make Uppercase */
93 if (c >= 'Z')
94 c -= ('a' - 'A');
95 /* printf(" |%02x| ",c); */
97 switch (c) {
98 case '\r': /* Enter */
99 case '\n':
100 input[i] = 0;
101 puts ("\r\n");
102 line = 1;
103 break;
104 case '\0': /* nul */
105 continue;
107 case 0x03: /* ^C - break */
108 input[0] = 0;
109 i = 0;
110 line = 1;
111 done = 1;
112 break;
114 case 0x5F:
115 case 0x08: /* ^H - backspace */
116 case 0x7F: /* DEL - backspace */
117 if (i > 0) {
118 puts ("\b \b");
119 i--;
121 break;
122 default:
123 if (start) {
124 if ((c == 'W') || (c == 'D')
125 || (c == 'M') || (c == 'C')
126 || (c == 'P')) {
127 putc (c);
128 input[i] = c;
129 if (i <= 45)
130 i++;
131 start = 0;
133 } else {
134 if ((c >= '0' && c <= '9')
135 || (c >= 'A' && c <= 'F')
136 || (c == 'E') || (c == 'M')
137 || (c == ' ')) {
138 putc (c);
139 input[i] = c;
140 if (i <= 45)
141 i++;
142 break;
145 break;
149 for (; i < 49; i++)
150 input[i] = 0;
152 switch (input[0]) {
153 case ('W'):
154 /* Line should be w reg value */
155 i = 0;
156 reg = 0;
157 value = 0;
158 /* Skip to the next space or end) */
159 while ((input[i] != ' ') && (input[i] != 0))
160 i++;
162 if (input[i] != 0)
163 i++;
165 /* Are we writing to EEPROM or MAC */
166 switch (input[i]) {
167 case ('E'):
168 what = EEPROM;
169 break;
170 case ('M'):
171 what = MAC;
172 break;
173 default:
174 what = UNKNOWN;
175 break;
178 /* skip to the next space or end */
179 while ((input[i] != ' ') && (input[i] != 0))
180 i++;
181 if (input[i] != 0)
182 i++;
184 /* Find register to write into */
185 j = 0;
186 while ((input[i] != ' ') && (input[i] != 0)) {
187 j = input[i] - 0x30;
188 if (j >= 0xA) {
189 j -= 0x07;
191 reg = (reg * 0x10) + j;
192 i++;
195 while ((input[i] != ' ') && (input[i] != 0))
196 i++;
198 if (input[i] != 0)
199 i++;
200 else
201 what = UNKNOWN;
203 /* Get the value to write */
204 j = 0;
205 while ((input[i] != ' ') && (input[i] != 0)) {
206 j = input[i] - 0x30;
207 if (j >= 0xA) {
208 j -= 0x07;
210 value = (value * 0x10) + j;
211 i++;
214 switch (what) {
215 case 1:
216 printf ("Writing EEPROM register %02x with %04x\n", reg, value);
217 write_eeprom_reg (value, reg);
218 break;
219 case 2:
220 printf ("Writing MAC register bank %i, reg %02x with %04x\n", reg >> 4, reg & 0xE, value);
221 SMC_SELECT_BANK (reg >> 4);
222 SMC_outw (value, reg & 0xE);
223 break;
224 default:
225 printf ("Wrong\n");
226 break;
228 break;
229 case ('D'):
230 dump_eeprom ();
231 break;
232 case ('M'):
233 dump_reg ();
234 break;
235 case ('C'):
236 copy_from_eeprom ();
237 break;
238 case ('P'):
239 print_macaddr ();
240 break;
241 default:
242 break;
247 return (0);
250 void copy_from_eeprom (void)
252 int i;
254 SMC_SELECT_BANK (1);
255 SMC_outw ((SMC_inw (CTL_REG) & !CTL_EEPROM_SELECT) | CTL_RELOAD,
256 CTL_REG);
257 i = 100;
258 while ((SMC_inw (CTL_REG) & CTL_RELOAD) && --i)
259 udelay (100);
260 if (i == 0) {
261 printf ("Timeout Refreshing EEPROM registers\n");
262 } else {
263 printf ("EEPROM contents copied to MAC\n");
268 void print_macaddr (void)
270 int i, j, k, mac[6];
272 printf ("Current MAC Address in SMSC91111 ");
273 SMC_SELECT_BANK (1);
274 for (i = 0; i < 5; i++) {
275 printf ("%02x:", SMC_inb (ADDR0_REG + i));
278 printf ("%02x\n", SMC_inb (ADDR0_REG + 5));
280 i = 0;
281 for (j = 0x20; j < 0x23; j++) {
282 k = read_eeprom_reg (j);
283 mac[i] = k & 0xFF;
284 i++;
285 mac[i] = k >> 8;
286 i++;
289 printf ("Current MAC Address in EEPROM ");
290 for (i = 0; i < 5; i++)
291 printf ("%02x:", mac[i]);
292 printf ("%02x\n", mac[5]);
295 void dump_eeprom (void)
297 int j, k;
299 printf ("IOS2-0 ");
300 for (j = 0; j < 8; j++) {
301 printf ("%03x ", j);
303 printf ("\n");
305 for (k = 0; k < 4; k++) {
306 if (k == 0)
307 printf ("CONFIG ");
308 if (k == 1)
309 printf ("BASE ");
310 if ((k == 2) || (k == 3))
311 printf (" ");
312 for (j = 0; j < 0x20; j += 4) {
313 printf ("%02x:%04x ", j + k, read_eeprom_reg (j + k));
315 printf ("\n");
318 for (j = 0x20; j < 0x40; j++) {
319 if ((j & 0x07) == 0)
320 printf ("\n");
321 printf ("%02x:%04x ", j, read_eeprom_reg (j));
323 printf ("\n");
327 int read_eeprom_reg (int reg)
329 int timeout;
331 SMC_SELECT_BANK (2);
332 SMC_outw (reg, PTR_REG);
334 SMC_SELECT_BANK (1);
335 SMC_outw (SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
336 CTL_REG);
337 timeout = 100;
338 while ((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout)
339 udelay (100);
340 if (timeout == 0) {
341 printf ("Timeout Reading EEPROM register %02x\n", reg);
342 return 0;
345 return SMC_inw (GP_REG);
349 int write_eeprom_reg (int value, int reg)
351 int timeout;
353 SMC_SELECT_BANK (2);
354 SMC_outw (reg, PTR_REG);
356 SMC_SELECT_BANK (1);
357 SMC_outw (value, GP_REG);
358 SMC_outw (SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
359 timeout = 100;
360 while ((SMC_inw (CTL_REG) & CTL_STORE) && --timeout)
361 udelay (100);
362 if (timeout == 0) {
363 printf ("Timeout Writing EEPROM register %02x\n", reg);
364 return 0;
367 return 1;
371 void dump_reg (void)
373 int i, j;
375 printf (" ");
376 for (j = 0; j < 4; j++) {
377 printf ("Bank%i ", j);
379 printf ("\n");
380 for (i = 0; i < 0xF; i += 2) {
381 printf ("%02x ", i);
382 for (j = 0; j < 4; j++) {
383 SMC_SELECT_BANK (j);
384 printf ("%04x ", SMC_inw (i));
386 printf ("\n");
390 #else
392 int smc91111_eeprom (int argc, char *argv[])
394 printf("Not supported for this board\n");
395 return 1;
398 #endif