]> git.sur5r.net Git - u-boot/blob - examples/smc91111_eeprom.c
Coding Style cleanyp; update CHANGELOG
[u-boot] / examples / smc91111_eeprom.c
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
30 #include <common.h>
31 #include <exports.h>
32 #include "../drivers/net/smc91111.h"
33
34 #ifdef CONFIG_DRIVER_SMC91111
35
36 #ifdef pFIO0_DIR
37 # define pFIO_DIR    pFIO0_DIR
38 # define pFIO_FLAG_S pFIO0_FLAG_S
39 #endif
40
41 #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
42 #define EEPROM          0x1;
43 #define MAC             0x2;
44 #define UNKNOWN         0x4;
45
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);
53
54 int smc91111_eeprom (int argc, char *argv[])
55 {
56         int c, i, j, done, line, reg, value, start, what;
57         char input[50];
58
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);
67         }
68
69         *pFIO_DIR = 0x01;
70         *pFIO_FLAG_S = 0x01;
71         SSYNC();
72
73         if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) {
74                 printf ("Can't find SMSC91111\n");
75                 return (0);
76         }
77
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 ());
90
91                         c = getc ();
92                         /* Make Uppercase */
93                         if (c >= 'Z')
94                                 c -= ('a' - 'A');
95                         /* printf(" |%02x| ",c); */
96
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;
106
107                         case 0x03:      /* ^C - break           */
108                                 input[0] = 0;
109                                 i = 0;
110                                 line = 1;
111                                 done = 1;
112                                 break;
113
114                         case 0x5F:
115                         case 0x08:      /* ^H  - backspace      */
116                         case 0x7F:      /* DEL - backspace      */
117                                 if (i > 0) {
118                                         puts ("\b \b");
119                                         i--;
120                                 }
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;
132                                         }
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;
143                                         }
144                                 }
145                                 break;
146                         }
147                 }
148
149                 for (; i < 49; i++)
150                         input[i] = 0;
151
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++;
161
162                         if (input[i] != 0)
163                                 i++;
164
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;
176                         }
177
178                         /* skip to the next space or end */
179                         while ((input[i] != ' ') && (input[i] != 0))
180                                 i++;
181                         if (input[i] != 0)
182                                 i++;
183
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;
190                                 }
191                                 reg = (reg * 0x10) + j;
192                                 i++;
193                         }
194
195                         while ((input[i] != ' ') && (input[i] != 0))
196                                 i++;
197
198                         if (input[i] != 0)
199                                 i++;
200                         else
201                                 what = UNKNOWN;
202
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;
209                                 }
210                                 value = (value * 0x10) + j;
211                                 i++;
212                         }
213
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;
227                         }
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;
243                 }
244
245         }
246
247         return (0);
248 }
249
250 void copy_from_eeprom (void)
251 {
252         int i;
253
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");
264         }
265
266 }
267
268 void print_macaddr (void)
269 {
270         int i, j, k, mac[6];
271
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));
276         }
277
278         printf ("%02x\n", SMC_inb (ADDR0_REG + 5));
279
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++;
287         }
288
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]);
293
294 }
295 void dump_eeprom (void)
296 {
297         int j, k;
298
299         printf ("IOS2-0    ");
300         for (j = 0; j < 8; j++) {
301                 printf ("%03x     ", j);
302         }
303         printf ("\n");
304
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));
314                 }
315                 printf ("\n");
316         }
317
318         for (j = 0x20; j < 0x40; j++) {
319                 if ((j & 0x07) == 0)
320                         printf ("\n");
321                 printf ("%02x:%04x ", j, read_eeprom_reg (j));
322         }
323         printf ("\n");
324
325 }
326
327 int read_eeprom_reg (int reg)
328 {
329         int timeout;
330
331         SMC_SELECT_BANK (2);
332         SMC_outw (reg, PTR_REG);
333
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;
343         }
344
345         return SMC_inw (GP_REG);
346
347 }
348
349 int write_eeprom_reg (int value, int reg)
350 {
351         int timeout;
352
353         SMC_SELECT_BANK (2);
354         SMC_outw (reg, PTR_REG);
355
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;
365         }
366
367         return 1;
368
369 }
370
371 void dump_reg (void)
372 {
373         int i, j;
374
375         printf ("    ");
376         for (j = 0; j < 4; j++) {
377                 printf ("Bank%i ", j);
378         }
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));
385                 }
386                 printf ("\n");
387         }
388 }
389
390 #else
391
392 int smc91111_eeprom (int argc, char *argv[])
393 {
394         printf("Not supported for this board\n");
395         return 1;
396 }
397
398 #endif