blob: 19ad9e6297c77122587a1633b9cf25aded1fae4c [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)
54static inline u32 __smc911x_reg_read(struct eth_device *dev, u32 offset)
55{
56 return *(volatile u32*)(dev->iobase + offset);
57}
58u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
59 __attribute__((weak, alias("__smc911x_reg_read")));
60
61static inline void __smc911x_reg_write(struct eth_device *dev,
62 u32 offset, u32 val)
63{
64 *(volatile u32*)(dev->iobase + offset) = val;
65}
66void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
67 __attribute__((weak, alias("__smc911x_reg_write")));
68#elif defined (CONFIG_SMC911X_16_BIT)
69static inline u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
70{
71 volatile u16 *addr_16 = (u16 *)(dev->iobase + offset);
72 return ((*addr_16 & 0x0000ffff) | (*(addr_16 + 1) << 16));
73}
74static inline void smc911x_reg_write(struct eth_device *dev,
75 u32 offset, u32 val)
76{
77 *(volatile u16 *)(dev->iobase + offset) = (u16)val;
78 *(volatile u16 *)(dev->iobase + offset + 2) = (u16)(val >> 16);
79}
80#else
81#error "SMC911X: undefined bus width"
82#endif /* CONFIG_SMC911X_16_BIT */
83
84static u32 smc911x_get_mac_csr(struct eth_device *dev, u8 reg)
85{
86 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
87 ;
88 smc911x_reg_write(dev, MAC_CSR_CMD,
89 MAC_CSR_CMD_CSR_BUSY | MAC_CSR_CMD_R_NOT_W | reg);
90 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
91 ;
92
93 return smc911x_reg_read(dev, MAC_CSR_DATA);
94}
95
96static void smc911x_set_mac_csr(struct eth_device *dev, u8 reg, u32 data)
97{
98 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
99 ;
100 smc911x_reg_write(dev, MAC_CSR_DATA, data);
101 smc911x_reg_write(dev, MAC_CSR_CMD, MAC_CSR_CMD_CSR_BUSY | reg);
102 while (smc911x_reg_read(dev, MAC_CSR_CMD) & MAC_CSR_CMD_CSR_BUSY)
103 ;
104}
105
106static int smc911x_detect_chip(struct eth_device *dev)
107{
108 unsigned long val, i;
109
110 val = smc911x_reg_read(dev, BYTE_TEST);
111 if (val == 0xffffffff) {
112 /* Special case -- no chip present */
113 return -1;
114 } else if (val != 0x87654321) {
115 printf(DRIVERNAME ": Invalid chip endian 0x%08lx\n", val);
116 return -1;
117 }
118
119 val = smc911x_reg_read(dev, ID_REV) >> 16;
120 for (i = 0; chip_ids[i].id != 0; i++) {
121 if (chip_ids[i].id == val) break;
122 }
123 if (!chip_ids[i].id) {
124 printf(DRIVERNAME ": Unknown chip ID %04lx\n", val);
125 return -1;
126 }
127
128 dev->priv = (void *)&chip_ids[i];
129
130 return 0;
131}
132
133static void smc911x_reset(struct eth_device *dev)
134{
135 int timeout;
136
137 /*
138 * Take out of PM setting first
139 * Device is already wake up if PMT_CTRL_READY bit is set
140 */
141 if ((smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY) == 0) {
142 /* Write to the bytetest will take out of powerdown */
143 smc911x_reg_write(dev, BYTE_TEST, 0x0);
144
145 timeout = 10;
146
147 while (timeout-- &&
148 !(smc911x_reg_read(dev, PMT_CTRL) & PMT_CTRL_READY))
149 udelay(10);
150 if (timeout < 0) {
151 printf(DRIVERNAME
152 ": timeout waiting for PM restore\n");
153 return;
154 }
155 }
156
157 /* Disable interrupts */
158 smc911x_reg_write(dev, INT_EN, 0);
159
160 smc911x_reg_write(dev, HW_CFG, HW_CFG_SRST);
161
162 timeout = 1000;
163 while (timeout-- && smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
164 udelay(10);
165
166 if (timeout < 0) {
167 printf(DRIVERNAME ": reset timeout\n");
168 return;
169 }
170
171 /* Reset the FIFO level and flow control settings */
172 smc911x_set_mac_csr(dev, FLOW, FLOW_FCPT | FLOW_FCEN);
173 smc911x_reg_write(dev, AFC_CFG, 0x0050287F);
174
175 /* Set to LED outputs */
176 smc911x_reg_write(dev, GPIO_CFG, 0x70070000);
177}
178
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500179/**
180 * smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
181 */
182static int smsc_ctrlc(void)
183{
184 return (tstc() && getc() == 0x03);
185}
186
187/**
188 * usage - dump usage information
189 */
190static void usage(void)
191{
192 puts(
193 "MAC/EEPROM Commands:\n"
194 " P : Print the MAC addresses\n"
195 " D : Dump the EEPROM contents\n"
196 " M : Dump the MAC contents\n"
197 " C : Copy the MAC address from the EEPROM to the MAC\n"
198 " W : Write a register in the EEPROM or in the MAC\n"
199 " Q : Quit\n"
200 "\n"
201 "Some commands take arguments:\n"
202 " W <E|M> <register> <value>\n"
203 " E: EEPROM M: MAC\n"
204 );
205}
206
207/**
208 * dump_regs - dump the MAC registers
209 *
210 * Registers 0x00 - 0x50 are FIFOs. The 0x50+ are the control registers
211 * and they're all 32bits long. 0xB8+ are reserved, so don't bother.
212 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500213static void dump_regs(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500214{
215 u8 i, j = 0;
216 for (i = 0x50; i < 0xB8; i += sizeof(u32))
Mike Frysinger78b53362009-02-25 17:29:40 -0500217 printf("%02x: 0x%08x %c", i,
Mike Frysinger17e90122009-11-12 22:26:02 -0500218 smc911x_reg_read(dev, i),
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500219 (j++ % 2 ? '\n' : ' '));
220}
221
222/**
223 * do_eeprom_cmd - handle eeprom communication
224 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500225static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500226{
Mike Frysinger17e90122009-11-12 22:26:02 -0500227 if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500228 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
Mike Frysinger17e90122009-11-12 22:26:02 -0500229 smc911x_reg_read(dev, E2P_CMD));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500230 return -1;
231 }
232
Mike Frysinger17e90122009-11-12 22:26:02 -0500233 smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500234
Mike Frysinger17e90122009-11-12 22:26:02 -0500235 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500236 if (smsc_ctrlc()) {
237 printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysinger17e90122009-11-12 22:26:02 -0500238 smc911x_reg_read(dev, E2P_CMD));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500239 return -1;
240 }
241
242 return 0;
243}
244
245/**
246 * read_eeprom_reg - read specified register in EEPROM
247 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500248static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500249{
Mike Frysinger17e90122009-11-12 22:26:02 -0500250 int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
251 return (ret ? : smc911x_reg_read(dev, E2P_DATA));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500252}
253
254/**
255 * write_eeprom_reg - write specified value into specified register in EEPROM
256 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500257static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500258{
259 int ret;
260
261 /* enable erasing/writing */
Mike Frysinger17e90122009-11-12 22:26:02 -0500262 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500263 if (ret)
264 goto done;
265
266 /* erase the eeprom reg */
Mike Frysinger17e90122009-11-12 22:26:02 -0500267 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500268 if (ret)
269 goto done;
270
271 /* write the eeprom reg */
Mike Frysinger17e90122009-11-12 22:26:02 -0500272 smc911x_reg_write(dev, E2P_DATA, value);
273 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500274 if (ret)
275 goto done;
276
277 /* disable erasing/writing */
Mike Frysinger17e90122009-11-12 22:26:02 -0500278 ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500279
280 done:
281 return ret;
282}
283
284/**
285 * skip_space - find first non-whitespace in given pointer
286 */
287static char *skip_space(char *buf)
288{
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000289 while (isblank(buf[0]))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500290 ++buf;
291 return buf;
292}
293
294/**
295 * write_stuff - handle writing of MAC registers / eeprom
296 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500297static void write_stuff(struct eth_device *dev, char *line)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500298{
299 char dest;
300 char *endp;
301 u8 reg;
302 u32 value;
303
304 /* Skip over the "W " part of the command */
305 line = skip_space(line + 1);
306
307 /* Figure out destination */
308 switch (line[0]) {
309 case 'E':
310 case 'M':
311 dest = line[0];
312 break;
313 default:
314 invalid_usage:
315 printf("ERROR: Invalid write usage\n");
316 usage();
317 return;
318 }
319
320 /* Get the register to write */
321 line = skip_space(line + 1);
322 reg = simple_strtoul(line, &endp, 16);
323 if (line == endp)
324 goto invalid_usage;
325
326 /* Get the value to write */
327 line = skip_space(endp);
328 value = simple_strtoul(line, &endp, 16);
329 if (line == endp)
330 goto invalid_usage;
331
332 /* Check for trailing cruft */
333 line = skip_space(endp);
334 if (line[0])
335 goto invalid_usage;
336
337 /* Finally, execute the command */
338 if (dest == 'E') {
339 printf("Writing EEPROM register %02x with %02x\n", reg, value);
Mike Frysinger17e90122009-11-12 22:26:02 -0500340 write_eeprom_reg(dev, value, reg);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500341 } else {
342 printf("Writing MAC register %02x with %08x\n", reg, value);
Mike Frysinger17e90122009-11-12 22:26:02 -0500343 smc911x_reg_write(dev, reg, value);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500344 }
345}
346
347/**
348 * copy_from_eeprom - copy MAC address in eeprom to address registers
349 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500350static void copy_from_eeprom(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500351{
352 ulong addrl =
Mike Frysinger17e90122009-11-12 22:26:02 -0500353 read_eeprom_reg(dev, 0x01) |
354 read_eeprom_reg(dev, 0x02) << 8 |
355 read_eeprom_reg(dev, 0x03) << 16 |
356 read_eeprom_reg(dev, 0x04) << 24;
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500357 ulong addrh =
Mike Frysinger17e90122009-11-12 22:26:02 -0500358 read_eeprom_reg(dev, 0x05) |
359 read_eeprom_reg(dev, 0x06) << 8;
360 smc911x_set_mac_csr(dev, ADDRL, addrl);
361 smc911x_set_mac_csr(dev, ADDRH, addrh);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500362 puts("EEPROM contents copied to MAC\n");
363}
364
365/**
366 * print_macaddr - print MAC address registers and MAC address in eeprom
367 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500368static void print_macaddr(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500369{
370 puts("Current MAC Address in MAC: ");
Mike Frysinger17e90122009-11-12 22:26:02 -0500371 ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
372 ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500373 printf("%02x:%02x:%02x:%02x:%02x:%02x\n",
374 (u8)(addrl), (u8)(addrl >> 8), (u8)(addrl >> 16),
375 (u8)(addrl >> 24), (u8)(addrh), (u8)(addrh >> 8));
376
377 puts("Current MAC Address in EEPROM: ");
378 int i;
379 for (i = 1; i < 6; ++i)
Mike Frysinger17e90122009-11-12 22:26:02 -0500380 printf("%02x:", read_eeprom_reg(dev, i));
381 printf("%02x\n", read_eeprom_reg(dev, i));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500382}
383
384/**
385 * dump_eeprom - dump the whole content of the EEPROM
386 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500387static void dump_eeprom(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500388{
389 int i;
390 puts("EEPROM:\n");
391 for (i = 0; i < 7; ++i)
Mike Frysinger17e90122009-11-12 22:26:02 -0500392 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500393}
394
395/**
396 * smc911x_init - get the MAC/EEPROM up and ready for use
397 */
Mike Frysinger17e90122009-11-12 22:26:02 -0500398static int smc911x_init(struct eth_device *dev)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500399{
400 /* See if there is anything there */
Juergen Kilbc9314d02010-06-12 21:22:01 +0200401 if (smc911x_detect_chip(dev))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500402 return 1;
403
Mike Frysinger17e90122009-11-12 22:26:02 -0500404 smc911x_reset(dev);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500405
406 /* Make sure we set EEDIO/EECLK to the EEPROM */
Mike Frysinger17e90122009-11-12 22:26:02 -0500407 if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
408 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500409 if (smsc_ctrlc()) {
Mike Frysinger78b53362009-02-25 17:29:40 -0500410 printf("init: timeout (E2P_CMD = 0x%08x)\n",
Mike Frysinger17e90122009-11-12 22:26:02 -0500411 smc911x_reg_read(dev, E2P_CMD));
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500412 return 1;
413 }
Mike Frysinger17e90122009-11-12 22:26:02 -0500414 smc911x_reg_write(dev, GPIO_CFG,
415 smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500416 }
417
418 return 0;
419}
420
421/**
422 * getline - consume a line of input and handle some escape sequences
423 */
424static char *getline(void)
425{
426 static char buffer[100];
427 char c;
428 size_t i;
429
430 i = 0;
431 while (1) {
432 buffer[i] = '\0';
433 while (!tstc())
434 continue;
435
436 c = getc();
437 /* Convert to uppercase */
438 if (c >= 'a' && c <= 'z')
439 c -= ('a' - 'A');
440
441 switch (c) {
442 case '\r': /* Enter/Return key */
443 case '\n':
444 puts("\n");
445 return buffer;
446
447 case 0x03: /* ^C - break */
448 return NULL;
449
450 case 0x5F:
451 case 0x08: /* ^H - backspace */
452 case 0x7F: /* DEL - backspace */
453 if (i) {
454 puts("\b \b");
455 i--;
456 }
457 break;
458
459 default:
460 /* Ignore control characters */
461 if (c < 0x20)
462 break;
463 /* Queue up all other characters */
464 buffer[i++] = c;
465 printf("%c", c);
466 break;
467 }
468 }
469}
470
471/**
472 * smc911x_eeprom - our application's main() function
473 */
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200474int smc911x_eeprom(int argc, char * const argv[])
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500475{
Mike Frysinger17e90122009-11-12 22:26:02 -0500476 /* Avoid initializing on stack as gcc likes to call memset() */
477 struct eth_device dev;
Mike Frysinger17e90122009-11-12 22:26:02 -0500478 dev.iobase = CONFIG_SMC911X_BASE;
479
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500480 /* Print the ABI version */
481 app_startup(argv);
482 if (XF_VERSION != get_version()) {
483 printf("Expects ABI version %d\n", XF_VERSION);
484 printf("Actual U-Boot ABI version %lu\n", get_version());
485 printf("Can't run\n\n");
486 return 1;
487 }
488
489 /* Initialize the MAC/EEPROM somewhat */
490 puts("\n");
Mike Frysinger17e90122009-11-12 22:26:02 -0500491 if (smc911x_init(&dev))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500492 return 1;
493
494 /* Dump helpful usage information */
495 puts("\n");
496 usage();
497 puts("\n");
498
499 while (1) {
500 char *line;
501
502 /* Send the prompt and wait for a line */
503 puts("eeprom> ");
504 line = getline();
505
506 /* Got a ctrl+c */
507 if (!line)
508 return 0;
509
510 /* Eat leading space */
511 line = skip_space(line);
512
513 /* Empty line, try again */
514 if (!line[0])
515 continue;
516
517 /* Only accept 1 letter commands */
Jason Hobbscafa1aa2011-08-23 11:06:54 +0000518 if (line[0] && line[1] && !isblank(line[1]))
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500519 goto unknown_cmd;
520
521 /* Now parse the command */
522 switch (line[0]) {
Mike Frysinger17e90122009-11-12 22:26:02 -0500523 case 'W': write_stuff(&dev, line); break;
524 case 'D': dump_eeprom(&dev); break;
525 case 'M': dump_regs(&dev); break;
526 case 'C': copy_from_eeprom(&dev); break;
527 case 'P': print_macaddr(&dev); break;
Mike Frysinger96a1ef82008-12-22 02:56:07 -0500528 unknown_cmd:
529 default: puts("ERROR: Unknown command!\n\n");
530 case '?':
531 case 'H': usage(); break;
532 case 'Q': return 0;
533 }
534 }
535}