blob: 270588bcf5ea66d4bb2ba65785dbb6b6e1a37c2a [file] [log] [blame]
Mike Frysinger96a1ef82008-12-22 02:56:07 -05001/*
2 * smc911x_eeprom.c - EEPROM interface to SMC911x parts.
3 * Only tested on SMSC9118 though ...
4 *
Mike Frysinger17e90122009-11-12 22:26:02 -05005 * Copyright 2004-2009 Analog Devices Inc.
Mike Frysinger96a1ef82008-12-22 02:56:07 -05006 *
7 * Licensed under the GPL-2 or later.
8 *
9 * Based on smc91111_eeprom.c which:
10 * Heavily borrowed from the following peoples GPL'ed software:
11 * - Wolfgang Denk, DENX Software Engineering, wd@denx.de
Bin Meng75574052016-02-05 19:30:11 -080012 * Das U-Boot
Mike Frysinger96a1ef82008-12-22 02:56:07 -050013 * - Ladislav Michl ladis@linux-mips.org
14 * A rejected patch on the U-Boot mailing list
15 */
16
17#include <common.h>
Simon Glassa73bda42015-11-08 23:47:45 -070018#include <console.h>
Mike Frysinger96a1ef82008-12-22 02:56:07 -050019#include <exports.h>
Jason Hobbscafa1aa2011-08-23 11:06:54 +000020#include <linux/ctype.h>
Marek Vasut689434e2020-03-15 15:03:07 +010021#include <linux/types.h>
Mike Frysinger96a1ef82008-12-22 02:56:07 -050022#include "../drivers/net/smc911x.h"
23
Marek Vasut689434e2020-03-15 15:03:07 +010024#define DRIVERNAME "smc911x"
25
26#if defined (CONFIG_SMC911X_32_BIT) && \
27 defined (CONFIG_SMC911X_16_BIT)
28#error "SMC911X: Only one of CONFIG_SMC911X_32_BIT and \
29 CONFIG_SMC911X_16_BIT shall be set"
30#endif
31
32struct chip_id {
33 u16 id;
34 char *name;
35};
36
37static const struct chip_id chip_ids[] = {
38 { CHIP_89218, "LAN89218" },
39 { CHIP_9115, "LAN9115" },
40 { CHIP_9116, "LAN9116" },
41 { CHIP_9117, "LAN9117" },
42 { CHIP_9118, "LAN9118" },
43 { CHIP_9211, "LAN9211" },
44 { CHIP_9215, "LAN9215" },
45 { CHIP_9216, "LAN9216" },
46 { CHIP_9217, "LAN9217" },
47 { CHIP_9218, "LAN9218" },
48 { CHIP_9220, "LAN9220" },
49 { CHIP_9221, "LAN9221" },
50 { 0, NULL },
51};
52
53#if defined (CONFIG_SMC911X_32_BIT)
Marek Vasut8577e8c2020-03-15 14:38:59 +010054static u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
Marek Vasut689434e2020-03-15 15:03:07 +010055{
56 return *(volatile u32*)(dev->iobase + offset);
57}
Marek Vasut689434e2020-03-15 15:03:07 +010058
Marek Vasut8577e8c2020-03-15 14:38:59 +010059static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
Marek Vasut689434e2020-03-15 15:03:07 +010060{
61 *(volatile u32*)(dev->iobase + offset) = val;
62}
Marek Vasut689434e2020-03-15 15:03:07 +010063#elif defined (CONFIG_SMC911X_16_BIT)
Marek Vasut8577e8c2020-03-15 14:38:59 +010064static u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
Marek Vasut689434e2020-03-15 15:03:07 +010065{
66 volatile u16 *addr_16 = (u16 *)(dev->iobase + offset);
Marek Vasut8577e8c2020-03-15 14:38:59 +010067 return (*addr_16 & 0x0000ffff) | (*(addr_16 + 1) << 16);
Marek Vasut689434e2020-03-15 15:03:07 +010068}
Marek Vasut8577e8c2020-03-15 14:38:59 +010069static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
Marek Vasut689434e2020-03-15 15:03:07 +010070{
71 *(volatile u16 *)(dev->iobase + offset) = (u16)val;
72 *(volatile u16 *)(dev->iobase + offset + 2) = (u16)(val >> 16);
73}
74#else
75#error "SMC911X: undefined bus width"
76#endif /* CONFIG_SMC911X_16_BIT */
77
78static u32 smc911x_get_mac_csr(struct eth_device *dev, u8 reg)
79{
80 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
81 ;
82 smc911x_reg_write(dev, MAC_CSR_CMD,
83 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
84 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
85 ;
86
87 return smc911x_reg_read(dev, MAC_CSR_DATA);
88}
89
90static void smc911x_set_mac_csr(struct eth_device *dev, u8 reg, u32 data)
91{
92 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
93 ;
94 smc911x_reg_write(dev, MAC_CSR_DATA, data);
95 smc911x_reg_write(dev, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
96 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
97 ;
98}
99
100static int smc911x_detect_chip(struct eth_device *dev)
101{
102 unsigned long val, i;
103
104 val = smc911x_reg_read(dev, BYTE_TEST);
105 if (val == 0xffffffff) {
106 /* Special case -- no chip present */
107 return -1;
108 } else if (val != 0x87654321) {
109 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
110 return -1;
111 }
112
113 val = smc911x_reg_read(dev, ID_REV) >> 16;
114 for (i = 0; chip_ids[i].id != 0; i++) {
115 if (chip_ids[i].id == val) break;
116 }
117 if (!chip_ids[i].id) {
118 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
119 return -1;
120 }
121
122 dev->priv = (void *)&chip_ids[i];
123
124 return 0;
125}
126
127static void smc911x_reset(struct eth_device *dev)
128{
129 int timeout;
130
131 /*
132 * Take out of PM setting first
133 * Device is already wake up if PMT_CTRL_READY bit is set
134 */
135 if ((smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY) == 0) {
136 /* Write to the bytetest will take out of powerdown */
137 smc911x_reg_write(dev, BYTE_TEST, 0x0);
138
139 timeout = 10;
140
141 while (timeout-- &&
142 !(smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY))
143 udelay(10);
144 if (timeout < 0) {
145 printf(DRIVERNAME
146 ": timeout waiting for PM restore\n");
147 return;
148 }
149 }
150
151 /* Disable interrupts */
152 smc911x_reg_write(dev, INT_EN, 0);
153
154 smc911x_reg_write(dev, HW_CFG, HW_CFG_SRST);
155
156 timeout = 1000;
157 while (timeout-- && smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
158 udelay(10);
159
160 if (timeout < 0) {
161 printf(DRIVERNAME ": reset timeout\n");
162 return;
163 }
164
165 /* Reset the FIFO level and flow control settings */
166 smc911x_set_mac_csr(dev, FLOW, FLOW_FCPT | FLOW_FCEN);
167 smc911x_reg_write(dev, AFC_CFG, 0x0050287F);
168
169 /* Set to LED outputs */
170 smc911x_reg_write(dev, GPIO_CFG, 0x70070000);
171}
172
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500173/**
174 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
175 */
176static int smsc_ctrlc(void)
177{
178 return (tstc() && getc() == 0x03);
179}
180
181/**
182 * usage - dump usage information
183 */
184static void usage(void)
185{
186 puts(
187 "MAC/EEPROM Commands:\n"
188 " P : Print the MAC addresses\n"
189 " D : Dump the EEPROM contents\n"
190 " M : Dump the MAC contents\n"
191 " C : Copy the MAC address from the EEPROM to the MAC\n"
192 " W : Write a register in the EEPROM or in the MAC\n"
193 " Q : Quit\n"
194 "\n"
195 "Some commands take arguments:\n"
196 " W <E|M> <register> <value>\n"
197 " E: EEPROM M: MAC\n"
198 );
199}
200
201/**
202 * dump_regs - dump the MAC registers
203 *
204 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
205 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
206 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500207static void dump_regs(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500208{
209 u8 i, j = 0;
210 for (i = 0x50; i < 0xB8; i += sizeof(u32))
Mike Frysinger78b53362009-02-25 17:29:40 -0500211 printf("%02x: 0x%08x %c", i,
Mike Frysinger17e90122009-11-12 22:26:02 -0500212 smc911x_reg_read(dev, i),
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500213 (j++ % 2 ? '\n' : ' '));
214}
215
216/**
217 * do_eeprom_cmd - handle eeprom communication
218 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500219static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500220{
Mike Frysinger17e90122009-11-12 22:26:02 -0500221 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500222 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
Mike Frysinger17e90122009-11-12 22:26:02 -0500223 smc911x_reg_read(dev, E2P_CMD));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500224 return -1;
225 }
226
Mike Frysinger17e90122009-11-12 22:26:02 -0500227 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500228
Mike Frysinger17e90122009-11-12 22:26:02 -0500229 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500230 if (smsc_ctrlc()) {
231 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysinger17e90122009-11-12 22:26:02 -0500232 smc911x_reg_read(dev, E2P_CMD));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500233 return -1;
234 }
235
236 return 0;
237}
238
239/**
240 * read_eeprom_reg - read specified register in EEPROM
241 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500242static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500243{
Mike Frysinger17e90122009-11-12 22:26:02 -0500244 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
245 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500246}
247
248/**
249 * write_eeprom_reg - write specified value into specified register in EEPROM
250 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500251static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500252{
253 int ret;
254
255 /* enable erasing/writing */
Mike Frysinger17e90122009-11-12 22:26:02 -0500256 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500257 if (ret)
258 goto done;
259
260 /* erase the eeprom reg */
Mike Frysinger17e90122009-11-12 22:26:02 -0500261 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500262 if (ret)
263 goto done;
264
265 /* write the eeprom reg */
Mike Frysinger17e90122009-11-12 22:26:02 -0500266 smc911x_reg_write(dev, E2P_DATA, value);
267 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500268 if (ret)
269 goto done;
270
271 /* disable erasing/writing */
Mike Frysinger17e90122009-11-12 22:26:02 -0500272 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500273
274 done:
275 return ret;
276}
277
278/**
279 * skip_space - find first non-whitespace in given pointer
280 */
281static char *skip_space(char *buf)
282{
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000283 while (isblank(buf[0]))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500284 ++buf;
285 return buf;
286}
287
288/**
289 * write_stuff - handle writing of MAC registers / eeprom
290 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500291static void write_stuff(struct eth_device *dev, char *line)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500292{
293 char dest;
294 char *endp;
295 u8 reg;
296 u32 value;
297
298 /* Skip over the "W " part of the command */
299 line = skip_space(line + 1);
300
301 /* Figure out destination */
302 switch (line[0]) {
303 case 'E':
304 case 'M':
305 dest = line[0];
306 break;
307 default:
308 invalid_usage:
309 printf("ERROR: Invalid write usage\n");
310 usage();
311 return;
312 }
313
314 /* Get the register to write */
315 line = skip_space(line + 1);
316 reg = simple_strtoul(line, &endp, 16);
317 if (line == endp)
318 goto invalid_usage;
319
320 /* Get the value to write */
321 line = skip_space(endp);
322 value = simple_strtoul(line, &endp, 16);
323 if (line == endp)
324 goto invalid_usage;
325
326 /* Check for trailing cruft */
327 line = skip_space(endp);
328 if (line[0])
329 goto invalid_usage;
330
331 /* Finally, execute the command */
332 if (dest == 'E') {
333 printf("Writing EEPROM register %02x with %02x\n", reg, value);
Mike Frysinger17e90122009-11-12 22:26:02 -0500334 write_eeprom_reg(dev, value, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500335 } else {
336 printf("Writing MAC register %02x with %08x\n", reg, value);
Mike Frysinger17e90122009-11-12 22:26:02 -0500337 smc911x_reg_write(dev, reg, value);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500338 }
339}
340
341/**
342 * copy_from_eeprom - copy MAC address in eeprom to address registers
343 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500344static void copy_from_eeprom(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500345{
346 ulong addrl =
Mike Frysinger17e90122009-11-12 22:26:02 -0500347 read_eeprom_reg(dev, 0x01) |
348 read_eeprom_reg(dev, 0x02) << 8 |
349 read_eeprom_reg(dev, 0x03) << 16 |
350 read_eeprom_reg(dev, 0x04) << 24;
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500351 ulong addrh =
Mike Frysinger17e90122009-11-12 22:26:02 -0500352 read_eeprom_reg(dev, 0x05) |
353 read_eeprom_reg(dev, 0x06) << 8;
354 smc911x_set_mac_csr(dev, ADDRL, addrl);
355 smc911x_set_mac_csr(dev, ADDRH, addrh);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500356 puts("EEPROM contents copied to MAC\n");
357}
358
359/**
360 * print_macaddr - print MAC address registers and MAC address in eeprom
361 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500362static void print_macaddr(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500363{
364 puts("Current MAC Address in MAC: ");
Mike Frysinger17e90122009-11-12 22:26:02 -0500365 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
366 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500367 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
368 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
369 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
370
371 puts("Current MAC Address in EEPROM: ");
372 int i;
373 for (i = 1; i < 6; ++i)
Mike Frysinger17e90122009-11-12 22:26:02 -0500374 printf("%02x:", read_eeprom_reg(dev, i));
375 printf("%02x\n", read_eeprom_reg(dev, i));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500376}
377
378/**
379 * dump_eeprom - dump the whole content of the EEPROM
380 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500381static void dump_eeprom(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500382{
383 int i;
384 puts("EEPROM:\n");
385 for (i = 0; i < 7; ++i)
Mike Frysinger17e90122009-11-12 22:26:02 -0500386 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500387}
388
389/**
390 * smc911x_init - get the MAC/EEPROM up and ready for use
391 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500392static int smc911x_init(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500393{
394 /* See if there is anything there */
Juergen Kilbc9314d02010-06-12 21:22:01 +0200395 if (smc911x_detect_chip(dev))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500396 return 1;
397
Mike Frysinger17e90122009-11-12 22:26:02 -0500398 smc911x_reset(dev);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500399
400 /* Make sure we set EEDIO/EECLK to the EEPROM */
Mike Frysinger17e90122009-11-12 22:26:02 -0500401 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
402 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500403 if (smsc_ctrlc()) {
Mike Frysinger78b53362009-02-25 17:29:40 -0500404 printf("init: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysinger17e90122009-11-12 22:26:02 -0500405 smc911x_reg_read(dev, E2P_CMD));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500406 return 1;
407 }
Mike Frysinger17e90122009-11-12 22:26:02 -0500408 smc911x_reg_write(dev, GPIO_CFG,
409 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500410 }
411
412 return 0;
413}
414
415/**
416 * getline - consume a line of input and handle some escape sequences
417 */
418static char *getline(void)
419{
420 static char buffer[100];
421 char c;
422 size_t i;
423
424 i = 0;
425 while (1) {
426 buffer[i] = '\0';
427 while (!tstc())
428 continue;
429
430 c = getc();
431 /* Convert to uppercase */
432 if (c >= 'a' && c <= 'z')
433 c -= ('a' - 'A');
434
435 switch (c) {
436 case '\r': /* Enter/Return key */
437 case '\n':
438 puts("\n");
439 return buffer;
440
441 case 0x03: /* ^C - break */
442 return NULL;
443
444 case 0x5F:
445 case 0x08: /* ^H - backspace */
446 case 0x7F: /* DEL - backspace */
447 if (i) {
448 puts("\b \b");
449 i--;
450 }
451 break;
452
453 default:
454 /* Ignore control characters */
455 if (c < 0x20)
456 break;
457 /* Queue up all other characters */
458 buffer[i++] = c;
459 printf("%c", c);
460 break;
461 }
462 }
463}
464
465/**
466 * smc911x_eeprom - our application's main() function
467 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200468int smc911x_eeprom(int argc, char * const argv[])
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500469{
Mike Frysinger17e90122009-11-12 22:26:02 -0500470 /* Avoid initializing on stack as gcc likes to call memset() */
471 struct eth_device dev;
Mike Frysinger17e90122009-11-12 22:26:02 -0500472 dev.iobase = CONFIG_SMC911X_BASE;
473
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500474 /* Print the ABI version */
475 app_startup(argv);
476 if (XF_VERSION != get_version()) {
477 printf("Expects ABI version %d\n", XF_VERSION);
478 printf("Actual U-Boot ABI version %lu\n", get_version());
479 printf("Can't run\n\n");
480 return 1;
481 }
482
483 /* Initialize the MAC/EEPROM somewhat */
484 puts("\n");
Mike Frysinger17e90122009-11-12 22:26:02 -0500485 if (smc911x_init(&dev))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500486 return 1;
487
488 /* Dump helpful usage information */
489 puts("\n");
490 usage();
491 puts("\n");
492
493 while (1) {
494 char *line;
495
496 /* Send the prompt and wait for a line */
497 puts("eeprom> ");
498 line = getline();
499
500 /* Got a ctrl+c */
501 if (!line)
502 return 0;
503
504 /* Eat leading space */
505 line = skip_space(line);
506
507 /* Empty line, try again */
508 if (!line[0])
509 continue;
510
511 /* Only accept 1 letter commands */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000512 if (line[0] && line[1] && !isblank(line[1]))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500513 goto unknown_cmd;
514
515 /* Now parse the command */
516 switch (line[0]) {
Mike Frysinger17e90122009-11-12 22:26:02 -0500517 case 'W': write_stuff(&dev, line); break;
518 case 'D': dump_eeprom(&dev); break;
519 case 'M': dump_regs(&dev); break;
520 case 'C': copy_from_eeprom(&dev); break;
521 case 'P': print_macaddr(&dev); break;
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500522 unknown_cmd:
523 default: puts("ERROR: Unknown command!\n\n");
524 case '?':
525 case 'H': usage(); break;
526 case 'Q': return 0;
527 }
528 }
529}