blob: 9145763e30ce3319e47573d00c6df42e7ce518a2 [file] [log] [blame]
wdenkc86cdb92005-01-12 00:38:03 +00001/*
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>
Jean-Christophe PLAGNIOL-VILLARDed0ea1f2007-11-21 21:19:24 +010032#include "../drivers/net/smc91111.h"
wdenkc86cdb92005-01-12 00:38:03 +000033
Mike Frysinger034bcce2009-03-22 22:18:01 -040034#ifndef SMC91111_EEPROM_INIT
35# define SMC91111_EEPROM_INIT()
Mike Frysingerecb196d2008-02-04 19:26:56 -050036#endif
37
wdenkc86cdb92005-01-12 00:38:03 +000038#define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
Mike Frysinger034bcce2009-03-22 22:18:01 -040039#define EEPROM 0x1
40#define MAC 0x2
41#define UNKNOWN 0x4
wdenkc86cdb92005-01-12 00:38:03 +000042
Ben Warren0fd6aae2009-10-04 22:37:03 -070043void dump_reg (struct eth_device *dev);
44void dump_eeprom (struct eth_device *dev);
45int write_eeprom_reg (struct eth_device *dev, int value, int reg);
46void copy_from_eeprom (struct eth_device *dev);
47void print_MAC (struct eth_device *dev);
48int read_eeprom_reg (struct eth_device *dev, int reg);
49void print_macaddr (struct eth_device *dev);
wdenkc86cdb92005-01-12 00:38:03 +000050
51int smc91111_eeprom (int argc, char *argv[])
52{
53 int c, i, j, done, line, reg, value, start, what;
54 char input[50];
55
Ben Warren0fd6aae2009-10-04 22:37:03 -070056 struct eth_device dev = {
57 .iobase = CONFIG_SMC91111_BASE
58 };
59
wdenkc86cdb92005-01-12 00:38:03 +000060 /* Print the ABI version */
61 app_startup (argv);
62 if (XF_VERSION != (int) get_version ()) {
63 printf ("Expects ABI version %d\n", XF_VERSION);
64 printf ("Actual U-Boot ABI version %d\n",
65 (int) get_version ());
66 printf ("Can't run\n\n");
67 return (0);
68 }
69
Mike Frysinger034bcce2009-03-22 22:18:01 -040070 SMC91111_EEPROM_INIT();
wdenkc86cdb92005-01-12 00:38:03 +000071
Ben Warren0fd6aae2009-10-04 22:37:03 -070072 if ((SMC_inw (&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
wdenkc86cdb92005-01-12 00:38:03 +000073 printf ("Can't find SMSC91111\n");
74 return (0);
75 }
76
77 done = 0;
78 what = UNKNOWN;
79 printf ("\n");
80 while (!done) {
81 /* print the prompt */
82 printf ("SMC91111> ");
83 line = 0;
84 i = 0;
85 start = 1;
86 while (!line) {
87 /* Wait for a keystroke */
88 while (!tstc ());
89
90 c = getc ();
91 /* Make Uppercase */
92 if (c >= 'Z')
93 c -= ('a' - 'A');
94 /* printf(" |%02x| ",c); */
95
96 switch (c) {
97 case '\r': /* Enter */
98 case '\n':
99 input[i] = 0;
100 puts ("\r\n");
101 line = 1;
102 break;
103 case '\0': /* nul */
104 continue;
105
106 case 0x03: /* ^C - break */
107 input[0] = 0;
108 i = 0;
109 line = 1;
110 done = 1;
111 break;
112
113 case 0x5F:
114 case 0x08: /* ^H - backspace */
115 case 0x7F: /* DEL - backspace */
116 if (i > 0) {
117 puts ("\b \b");
118 i--;
119 }
120 break;
121 default:
122 if (start) {
123 if ((c == 'W') || (c == 'D')
124 || (c == 'M') || (c == 'C')
125 || (c == 'P')) {
126 putc (c);
127 input[i] = c;
128 if (i <= 45)
129 i++;
130 start = 0;
131 }
132 } else {
133 if ((c >= '0' && c <= '9')
134 || (c >= 'A' && c <= 'F')
135 || (c == 'E') || (c == 'M')
136 || (c == ' ')) {
137 putc (c);
138 input[i] = c;
139 if (i <= 45)
140 i++;
141 break;
142 }
143 }
144 break;
145 }
146 }
147
148 for (; i < 49; i++)
149 input[i] = 0;
150
151 switch (input[0]) {
152 case ('W'):
153 /* Line should be w reg value */
154 i = 0;
155 reg = 0;
156 value = 0;
157 /* Skip to the next space or end) */
158 while ((input[i] != ' ') && (input[i] != 0))
159 i++;
160
161 if (input[i] != 0)
162 i++;
163
164 /* Are we writing to EEPROM or MAC */
165 switch (input[i]) {
166 case ('E'):
167 what = EEPROM;
168 break;
169 case ('M'):
170 what = MAC;
171 break;
172 default:
173 what = UNKNOWN;
174 break;
175 }
176
177 /* skip to the next space or end */
178 while ((input[i] != ' ') && (input[i] != 0))
179 i++;
180 if (input[i] != 0)
181 i++;
182
183 /* Find register to write into */
184 j = 0;
185 while ((input[i] != ' ') && (input[i] != 0)) {
186 j = input[i] - 0x30;
187 if (j >= 0xA) {
188 j -= 0x07;
189 }
190 reg = (reg * 0x10) + j;
191 i++;
192 }
193
194 while ((input[i] != ' ') && (input[i] != 0))
195 i++;
196
197 if (input[i] != 0)
198 i++;
199 else
200 what = UNKNOWN;
201
202 /* Get the value to write */
203 j = 0;
204 while ((input[i] != ' ') && (input[i] != 0)) {
205 j = input[i] - 0x30;
206 if (j >= 0xA) {
207 j -= 0x07;
208 }
209 value = (value * 0x10) + j;
210 i++;
211 }
212
213 switch (what) {
214 case 1:
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100215 printf ("Writing EEPROM register %02x with %04x\n", reg, value);
Ben Warren0fd6aae2009-10-04 22:37:03 -0700216 write_eeprom_reg (&dev, value, reg);
wdenkc86cdb92005-01-12 00:38:03 +0000217 break;
218 case 2:
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100219 printf ("Writing MAC register bank %i, reg %02x with %04x\n", reg >> 4, reg & 0xE, value);
Ben Warren0fd6aae2009-10-04 22:37:03 -0700220 SMC_SELECT_BANK (&dev, reg >> 4);
221 SMC_outw (&dev, value, reg & 0xE);
wdenkc86cdb92005-01-12 00:38:03 +0000222 break;
223 default:
224 printf ("Wrong\n");
225 break;
226 }
227 break;
228 case ('D'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700229 dump_eeprom (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000230 break;
231 case ('M'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700232 dump_reg (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000233 break;
234 case ('C'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700235 copy_from_eeprom (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000236 break;
237 case ('P'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700238 print_macaddr (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000239 break;
240 default:
241 break;
242 }
243
244 }
245
246 return (0);
247}
248
Ben Warren0fd6aae2009-10-04 22:37:03 -0700249void copy_from_eeprom (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000250{
251 int i;
252
Ben Warren0fd6aae2009-10-04 22:37:03 -0700253 SMC_SELECT_BANK (dev, 1);
254 SMC_outw (dev, (SMC_inw (dev, CTL_REG) & !CTL_EEPROM_SELECT) |
255 CTL_RELOAD, CTL_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000256 i = 100;
Ben Warren0fd6aae2009-10-04 22:37:03 -0700257 while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --i)
wdenkc86cdb92005-01-12 00:38:03 +0000258 udelay (100);
259 if (i == 0) {
260 printf ("Timeout Refreshing EEPROM registers\n");
261 } else {
262 printf ("EEPROM contents copied to MAC\n");
263 }
264
265}
266
Ben Warren0fd6aae2009-10-04 22:37:03 -0700267void print_macaddr (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000268{
269 int i, j, k, mac[6];
270
271 printf ("Current MAC Address in SMSC91111 ");
Ben Warren0fd6aae2009-10-04 22:37:03 -0700272 SMC_SELECT_BANK (dev, 1);
wdenkc86cdb92005-01-12 00:38:03 +0000273 for (i = 0; i < 5; i++) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700274 printf ("%02x:", SMC_inb (dev, ADDR0_REG + i));
wdenkc86cdb92005-01-12 00:38:03 +0000275 }
276
Ben Warren0fd6aae2009-10-04 22:37:03 -0700277 printf ("%02x\n", SMC_inb (dev, ADDR0_REG + 5));
wdenkc86cdb92005-01-12 00:38:03 +0000278
279 i = 0;
280 for (j = 0x20; j < 0x23; j++) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700281 k = read_eeprom_reg (dev, j);
wdenkc86cdb92005-01-12 00:38:03 +0000282 mac[i] = k & 0xFF;
283 i++;
284 mac[i] = k >> 8;
285 i++;
286 }
287
288 printf ("Current MAC Address in EEPROM ");
289 for (i = 0; i < 5; i++)
290 printf ("%02x:", mac[i]);
291 printf ("%02x\n", mac[5]);
292
293}
Ben Warren0fd6aae2009-10-04 22:37:03 -0700294void dump_eeprom (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000295{
296 int j, k;
297
298 printf ("IOS2-0 ");
299 for (j = 0; j < 8; j++) {
300 printf ("%03x ", j);
301 }
302 printf ("\n");
303
304 for (k = 0; k < 4; k++) {
305 if (k == 0)
306 printf ("CONFIG ");
307 if (k == 1)
308 printf ("BASE ");
309 if ((k == 2) || (k == 3))
310 printf (" ");
311 for (j = 0; j < 0x20; j += 4) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700312 printf ("%02x:%04x ", j + k,
313 read_eeprom_reg (dev, j + k));
wdenkc86cdb92005-01-12 00:38:03 +0000314 }
315 printf ("\n");
316 }
317
318 for (j = 0x20; j < 0x40; j++) {
319 if ((j & 0x07) == 0)
320 printf ("\n");
Ben Warren0fd6aae2009-10-04 22:37:03 -0700321 printf ("%02x:%04x ", j, read_eeprom_reg (dev, j));
wdenkc86cdb92005-01-12 00:38:03 +0000322 }
323 printf ("\n");
324
325}
326
Ben Warren0fd6aae2009-10-04 22:37:03 -0700327int read_eeprom_reg (struct eth_device *dev, int reg)
wdenkc86cdb92005-01-12 00:38:03 +0000328{
329 int timeout;
330
Ben Warren0fd6aae2009-10-04 22:37:03 -0700331 SMC_SELECT_BANK (dev, 2);
332 SMC_outw (dev, reg, PTR_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000333
Ben Warren0fd6aae2009-10-04 22:37:03 -0700334 SMC_SELECT_BANK (dev, 1);
335 SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
336 CTL_RELOAD, CTL_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000337 timeout = 100;
Ben Warren0fd6aae2009-10-04 22:37:03 -0700338 while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --timeout)
wdenkc86cdb92005-01-12 00:38:03 +0000339 udelay (100);
340 if (timeout == 0) {
341 printf ("Timeout Reading EEPROM register %02x\n", reg);
342 return 0;
343 }
344
Ben Warren0fd6aae2009-10-04 22:37:03 -0700345 return SMC_inw (dev, GP_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000346
347}
348
Ben Warren0fd6aae2009-10-04 22:37:03 -0700349int write_eeprom_reg (struct eth_device *dev, int value, int reg)
wdenkc86cdb92005-01-12 00:38:03 +0000350{
351 int timeout;
352
Ben Warren0fd6aae2009-10-04 22:37:03 -0700353 SMC_SELECT_BANK (dev, 2);
354 SMC_outw (dev, reg, PTR_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000355
Ben Warren0fd6aae2009-10-04 22:37:03 -0700356 SMC_SELECT_BANK (dev, 1);
357 SMC_outw (dev, value, GP_REG);
358 SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
359 CTL_STORE, CTL_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000360 timeout = 100;
Ben Warren0fd6aae2009-10-04 22:37:03 -0700361 while ((SMC_inw (dev, CTL_REG) & CTL_STORE) && --timeout)
wdenkc86cdb92005-01-12 00:38:03 +0000362 udelay (100);
363 if (timeout == 0) {
364 printf ("Timeout Writing EEPROM register %02x\n", reg);
365 return 0;
366 }
367
368 return 1;
369
370}
371
Ben Warren0fd6aae2009-10-04 22:37:03 -0700372void dump_reg (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000373{
374 int i, j;
375
376 printf (" ");
377 for (j = 0; j < 4; j++) {
378 printf ("Bank%i ", j);
379 }
380 printf ("\n");
381 for (i = 0; i < 0xF; i += 2) {
382 printf ("%02x ", i);
383 for (j = 0; j < 4; j++) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700384 SMC_SELECT_BANK (dev, j);
385 printf ("%04x ", SMC_inw (dev, i));
wdenkc86cdb92005-01-12 00:38:03 +0000386 }
387 printf ("\n");
388 }
389}