blob: 5ffefc8f36bf0b47231dc8572718f6e37b8fb448 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenkc86cdb92005-01-12 00:38:03 +00002/*
3 * (C) Copyright 2004
4 * Robin Getz rgetz@blacfin.uclinux.org
5 *
wdenkc86cdb92005-01-12 00:38:03 +00006 * Heavily borrowed from the following peoples GPL'ed software:
7 * - Wolfgang Denk, DENX Software Engineering, wd@denx.de
Bin Meng75574052016-02-05 19:30:11 -08008 * Das U-Boot
wdenkc86cdb92005-01-12 00:38:03 +00009 * - Ladislav Michl ladis@linux-mips.org
10 * A rejected patch on the U-Boot mailing list
11 */
12
13#include <common.h>
14#include <exports.h>
Jean-Christophe PLAGNIOL-VILLARDed0ea1f2007-11-21 21:19:24 +010015#include "../drivers/net/smc91111.h"
wdenkc86cdb92005-01-12 00:38:03 +000016
Mike Frysinger034bcce2009-03-22 22:18:01 -040017#ifndef SMC91111_EEPROM_INIT
18# define SMC91111_EEPROM_INIT()
Mike Frysingerecb196d2008-02-04 19:26:56 -050019#endif
20
wdenkc86cdb92005-01-12 00:38:03 +000021#define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
Mike Frysinger034bcce2009-03-22 22:18:01 -040022#define EEPROM 0x1
23#define MAC 0x2
24#define UNKNOWN 0x4
wdenkc86cdb92005-01-12 00:38:03 +000025
Ben Warren0fd6aae2009-10-04 22:37:03 -070026void dump_reg (struct eth_device *dev);
27void dump_eeprom (struct eth_device *dev);
28int write_eeprom_reg (struct eth_device *dev, int value, int reg);
29void copy_from_eeprom (struct eth_device *dev);
30void print_MAC (struct eth_device *dev);
31int read_eeprom_reg (struct eth_device *dev, int reg);
32void print_macaddr (struct eth_device *dev);
wdenkc86cdb92005-01-12 00:38:03 +000033
Wolfgang Denk6262d0212010-06-28 22:00:46 +020034int smc91111_eeprom (int argc, char * const argv[])
wdenkc86cdb92005-01-12 00:38:03 +000035{
36 int c, i, j, done, line, reg, value, start, what;
37 char input[50];
38
Mike Frysinger9c9e6032010-01-21 05:04:59 -050039 struct eth_device dev;
40 dev.iobase = CONFIG_SMC91111_BASE;
Ben Warren0fd6aae2009-10-04 22:37:03 -070041
wdenkc86cdb92005-01-12 00:38:03 +000042 /* Print the ABI version */
43 app_startup (argv);
44 if (XF_VERSION != (int) get_version ()) {
45 printf ("Expects ABI version %d\n", XF_VERSION);
46 printf ("Actual U-Boot ABI version %d\n",
47 (int) get_version ());
48 printf ("Can't run\n\n");
49 return (0);
50 }
51
Mike Frysinger034bcce2009-03-22 22:18:01 -040052 SMC91111_EEPROM_INIT();
wdenkc86cdb92005-01-12 00:38:03 +000053
Ben Warren0fd6aae2009-10-04 22:37:03 -070054 if ((SMC_inw (&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
wdenkc86cdb92005-01-12 00:38:03 +000055 printf ("Can't find SMSC91111\n");
56 return (0);
57 }
58
59 done = 0;
60 what = UNKNOWN;
61 printf ("\n");
62 while (!done) {
63 /* print the prompt */
64 printf ("SMC91111> ");
65 line = 0;
66 i = 0;
67 start = 1;
68 while (!line) {
69 /* Wait for a keystroke */
70 while (!tstc ());
71
72 c = getc ();
73 /* Make Uppercase */
74 if (c >= 'Z')
75 c -= ('a' - 'A');
76 /* printf(" |%02x| ",c); */
77
78 switch (c) {
79 case '\r': /* Enter */
80 case '\n':
81 input[i] = 0;
82 puts ("\r\n");
83 line = 1;
84 break;
85 case '\0': /* nul */
86 continue;
87
88 case 0x03: /* ^C - break */
89 input[0] = 0;
90 i = 0;
91 line = 1;
92 done = 1;
93 break;
94
95 case 0x5F:
96 case 0x08: /* ^H - backspace */
97 case 0x7F: /* DEL - backspace */
98 if (i > 0) {
99 puts ("\b \b");
100 i--;
101 }
102 break;
103 default:
104 if (start) {
105 if ((c == 'W') || (c == 'D')
106 || (c == 'M') || (c == 'C')
107 || (c == 'P')) {
108 putc (c);
109 input[i] = c;
110 if (i <= 45)
111 i++;
112 start = 0;
113 }
114 } else {
115 if ((c >= '0' && c <= '9')
116 || (c >= 'A' && c <= 'F')
117 || (c == 'E') || (c == 'M')
118 || (c == ' ')) {
119 putc (c);
120 input[i] = c;
121 if (i <= 45)
122 i++;
123 break;
124 }
125 }
126 break;
127 }
128 }
129
130 for (; i < 49; i++)
131 input[i] = 0;
132
133 switch (input[0]) {
134 case ('W'):
135 /* Line should be w reg value */
136 i = 0;
137 reg = 0;
138 value = 0;
139 /* Skip to the next space or end) */
140 while ((input[i] != ' ') && (input[i] != 0))
141 i++;
142
143 if (input[i] != 0)
144 i++;
145
146 /* Are we writing to EEPROM or MAC */
147 switch (input[i]) {
148 case ('E'):
149 what = EEPROM;
150 break;
151 case ('M'):
152 what = MAC;
153 break;
154 default:
155 what = UNKNOWN;
156 break;
157 }
158
159 /* skip to the next space or end */
160 while ((input[i] != ' ') && (input[i] != 0))
161 i++;
162 if (input[i] != 0)
163 i++;
164
165 /* Find register to write into */
166 j = 0;
167 while ((input[i] != ' ') && (input[i] != 0)) {
168 j = input[i] - 0x30;
169 if (j >= 0xA) {
170 j -= 0x07;
171 }
172 reg = (reg * 0x10) + j;
173 i++;
174 }
175
176 while ((input[i] != ' ') && (input[i] != 0))
177 i++;
178
179 if (input[i] != 0)
180 i++;
181 else
182 what = UNKNOWN;
183
184 /* Get the value to write */
185 j = 0;
186 while ((input[i] != ' ') && (input[i] != 0)) {
187 j = input[i] - 0x30;
188 if (j >= 0xA) {
189 j -= 0x07;
190 }
191 value = (value * 0x10) + j;
192 i++;
193 }
194
195 switch (what) {
196 case 1:
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100197 printf ("Writing EEPROM register %02x with %04x\n", reg, value);
Ben Warren0fd6aae2009-10-04 22:37:03 -0700198 write_eeprom_reg (&dev, value, reg);
wdenkc86cdb92005-01-12 00:38:03 +0000199 break;
200 case 2:
Wolfgang Denk46d2b522006-03-12 02:10:00 +0100201 printf ("Writing MAC register bank %i, reg %02x with %04x\n", reg >> 4, reg & 0xE, value);
Ben Warren0fd6aae2009-10-04 22:37:03 -0700202 SMC_SELECT_BANK (&dev, reg >> 4);
203 SMC_outw (&dev, value, reg & 0xE);
wdenkc86cdb92005-01-12 00:38:03 +0000204 break;
205 default:
206 printf ("Wrong\n");
207 break;
208 }
209 break;
210 case ('D'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700211 dump_eeprom (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000212 break;
213 case ('M'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700214 dump_reg (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000215 break;
216 case ('C'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700217 copy_from_eeprom (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000218 break;
219 case ('P'):
Ben Warren0fd6aae2009-10-04 22:37:03 -0700220 print_macaddr (&dev);
wdenkc86cdb92005-01-12 00:38:03 +0000221 break;
222 default:
223 break;
224 }
225
226 }
227
228 return (0);
229}
230
Ben Warren0fd6aae2009-10-04 22:37:03 -0700231void copy_from_eeprom (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000232{
233 int i;
234
Ben Warren0fd6aae2009-10-04 22:37:03 -0700235 SMC_SELECT_BANK (dev, 1);
236 SMC_outw (dev, (SMC_inw (dev, CTL_REG) & !CTL_EEPROM_SELECT) |
237 CTL_RELOAD, CTL_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000238 i = 100;
Ben Warren0fd6aae2009-10-04 22:37:03 -0700239 while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --i)
wdenkc86cdb92005-01-12 00:38:03 +0000240 udelay (100);
241 if (i == 0) {
242 printf ("Timeout Refreshing EEPROM registers\n");
243 } else {
244 printf ("EEPROM contents copied to MAC\n");
245 }
246
247}
248
Ben Warren0fd6aae2009-10-04 22:37:03 -0700249void print_macaddr (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000250{
251 int i, j, k, mac[6];
252
253 printf ("Current MAC Address in SMSC91111 ");
Ben Warren0fd6aae2009-10-04 22:37:03 -0700254 SMC_SELECT_BANK (dev, 1);
wdenkc86cdb92005-01-12 00:38:03 +0000255 for (i = 0; i < 5; i++) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700256 printf ("%02x:", SMC_inb (dev, ADDR0_REG + i));
wdenkc86cdb92005-01-12 00:38:03 +0000257 }
258
Ben Warren0fd6aae2009-10-04 22:37:03 -0700259 printf ("%02x\n", SMC_inb (dev, ADDR0_REG + 5));
wdenkc86cdb92005-01-12 00:38:03 +0000260
261 i = 0;
262 for (j = 0x20; j < 0x23; j++) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700263 k = read_eeprom_reg (dev, j);
wdenkc86cdb92005-01-12 00:38:03 +0000264 mac[i] = k & 0xFF;
265 i++;
266 mac[i] = k >> 8;
267 i++;
268 }
269
270 printf ("Current MAC Address in EEPROM ");
271 for (i = 0; i < 5; i++)
272 printf ("%02x:", mac[i]);
273 printf ("%02x\n", mac[5]);
274
275}
Ben Warren0fd6aae2009-10-04 22:37:03 -0700276void dump_eeprom (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000277{
278 int j, k;
279
280 printf ("IOS2-0 ");
281 for (j = 0; j < 8; j++) {
282 printf ("%03x ", j);
283 }
284 printf ("\n");
285
286 for (k = 0; k < 4; k++) {
287 if (k == 0)
288 printf ("CONFIG ");
289 if (k == 1)
290 printf ("BASE ");
291 if ((k == 2) || (k == 3))
292 printf (" ");
293 for (j = 0; j < 0x20; j += 4) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700294 printf ("%02x:%04x ", j + k,
295 read_eeprom_reg (dev, j + k));
wdenkc86cdb92005-01-12 00:38:03 +0000296 }
297 printf ("\n");
298 }
299
300 for (j = 0x20; j < 0x40; j++) {
301 if ((j & 0x07) == 0)
302 printf ("\n");
Ben Warren0fd6aae2009-10-04 22:37:03 -0700303 printf ("%02x:%04x ", j, read_eeprom_reg (dev, j));
wdenkc86cdb92005-01-12 00:38:03 +0000304 }
305 printf ("\n");
306
307}
308
Ben Warren0fd6aae2009-10-04 22:37:03 -0700309int read_eeprom_reg (struct eth_device *dev, int reg)
wdenkc86cdb92005-01-12 00:38:03 +0000310{
311 int timeout;
312
Ben Warren0fd6aae2009-10-04 22:37:03 -0700313 SMC_SELECT_BANK (dev, 2);
314 SMC_outw (dev, reg, PTR_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000315
Ben Warren0fd6aae2009-10-04 22:37:03 -0700316 SMC_SELECT_BANK (dev, 1);
317 SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
318 CTL_RELOAD, CTL_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000319 timeout = 100;
Ben Warren0fd6aae2009-10-04 22:37:03 -0700320 while ((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --timeout)
wdenkc86cdb92005-01-12 00:38:03 +0000321 udelay (100);
322 if (timeout == 0) {
323 printf ("Timeout Reading EEPROM register %02x\n", reg);
324 return 0;
325 }
326
Ben Warren0fd6aae2009-10-04 22:37:03 -0700327 return SMC_inw (dev, GP_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000328
329}
330
Ben Warren0fd6aae2009-10-04 22:37:03 -0700331int write_eeprom_reg (struct eth_device *dev, int value, int reg)
wdenkc86cdb92005-01-12 00:38:03 +0000332{
333 int timeout;
334
Ben Warren0fd6aae2009-10-04 22:37:03 -0700335 SMC_SELECT_BANK (dev, 2);
336 SMC_outw (dev, reg, PTR_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000337
Ben Warren0fd6aae2009-10-04 22:37:03 -0700338 SMC_SELECT_BANK (dev, 1);
339 SMC_outw (dev, value, GP_REG);
340 SMC_outw (dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT |
341 CTL_STORE, CTL_REG);
wdenkc86cdb92005-01-12 00:38:03 +0000342 timeout = 100;
Ben Warren0fd6aae2009-10-04 22:37:03 -0700343 while ((SMC_inw (dev, CTL_REG) & CTL_STORE) && --timeout)
wdenkc86cdb92005-01-12 00:38:03 +0000344 udelay (100);
345 if (timeout == 0) {
346 printf ("Timeout Writing EEPROM register %02x\n", reg);
347 return 0;
348 }
349
350 return 1;
351
352}
353
Ben Warren0fd6aae2009-10-04 22:37:03 -0700354void dump_reg (struct eth_device *dev)
wdenkc86cdb92005-01-12 00:38:03 +0000355{
356 int i, j;
357
358 printf (" ");
359 for (j = 0; j < 4; j++) {
360 printf ("Bank%i ", j);
361 }
362 printf ("\n");
363 for (i = 0; i < 0xF; i += 2) {
364 printf ("%02x ", i);
365 for (j = 0; j < 4; j++) {
Ben Warren0fd6aae2009-10-04 22:37:03 -0700366 SMC_SELECT_BANK (dev, j);
367 printf ("%04x ", SMC_inw (dev, i));
wdenkc86cdb92005-01-12 00:38:03 +0000368 }
369 printf ("\n");
370 }
371}