270588bcf5ea66d4bb2ba65785dbb6b6e1a37c2a
[oweals/u-boot.git] / examples / standalone / smc911x_eeprom.c
1 /*
2  * smc911x_eeprom.c - EEPROM interface to SMC911x parts.
3  * Only tested on SMSC9118 though ...
4  *
5  * Copyright 2004-2009 Analog Devices Inc.
6  *
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
12  *       Das U-Boot
13  *  - Ladislav Michl ladis@linux-mips.org
14  *       A rejected patch on the U-Boot mailing list
15  */
16
17 #include <common.h>
18 #include <console.h>
19 #include <exports.h>
20 #include <linux/ctype.h>
21 #include <linux/types.h>
22 #include "../drivers/net/smc911x.h"
23
24 #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
32 struct chip_id {
33         u16 id;
34         char *name;
35 };
36
37 static 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)
54 static u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
55 {
56         return *(volatile u32*)(dev->iobase + offset);
57 }
58
59 static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
60 {
61         *(volatile u32*)(dev->iobase + offset) = val;
62 }
63 #elif defined (CONFIG_SMC911X_16_BIT)
64 static u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
65 {
66         volatile u16 *addr_16 = (u16 *)(dev->iobase + offset);
67         return (*addr_16 & 0x0000ffff) | (*(addr_16 + 1) << 16);
68 }
69 static void smc911x_reg_write(struct eth_device *dev, u32 offset, u32 val)
70 {
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
78 static 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
90 static 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
100 static 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
127 static 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
173 /**
174  *      smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
175  */
176 static int smsc_ctrlc(void)
177 {
178         return (tstc() && getc() == 0x03);
179 }
180
181 /**
182  *      usage - dump usage information
183  */
184 static 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  */
207 static void dump_regs(struct eth_device *dev)
208 {
209         u8 i, j = 0;
210         for (i = 0x50; i < 0xB8; i += sizeof(u32))
211                 printf("%02x: 0x%08x %c", i,
212                         smc911x_reg_read(dev, i),
213                         (j++ % 2 ? '\n' : ' '));
214 }
215
216 /**
217  *      do_eeprom_cmd - handle eeprom communication
218  */
219 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
220 {
221         if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
222                 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
223                         smc911x_reg_read(dev, E2P_CMD));
224                 return -1;
225         }
226
227         smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
228
229         while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
230                 if (smsc_ctrlc()) {
231                         printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
232                                 smc911x_reg_read(dev, E2P_CMD));
233                         return -1;
234                 }
235
236         return 0;
237 }
238
239 /**
240  *      read_eeprom_reg - read specified register in EEPROM
241  */
242 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
243 {
244         int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
245         return (ret ? : smc911x_reg_read(dev, E2P_DATA));
246 }
247
248 /**
249  *      write_eeprom_reg - write specified value into specified register in EEPROM
250  */
251 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
252 {
253         int ret;
254
255         /* enable erasing/writing */
256         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
257         if (ret)
258                 goto done;
259
260         /* erase the eeprom reg */
261         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
262         if (ret)
263                 goto done;
264
265         /* write the eeprom reg */
266         smc911x_reg_write(dev, E2P_DATA, value);
267         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
268         if (ret)
269                 goto done;
270
271         /* disable erasing/writing */
272         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
273
274  done:
275         return ret;
276 }
277
278 /**
279  *      skip_space - find first non-whitespace in given pointer
280  */
281 static char *skip_space(char *buf)
282 {
283         while (isblank(buf[0]))
284                 ++buf;
285         return buf;
286 }
287
288 /**
289  *      write_stuff - handle writing of MAC registers / eeprom
290  */
291 static void write_stuff(struct eth_device *dev, char *line)
292 {
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);
334                 write_eeprom_reg(dev, value, reg);
335         } else {
336                 printf("Writing MAC register %02x with %08x\n", reg, value);
337                 smc911x_reg_write(dev, reg, value);
338         }
339 }
340
341 /**
342  *      copy_from_eeprom - copy MAC address in eeprom to address registers
343  */
344 static void copy_from_eeprom(struct eth_device *dev)
345 {
346         ulong addrl =
347                 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;
351         ulong addrh =
352                 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);
356         puts("EEPROM contents copied to MAC\n");
357 }
358
359 /**
360  *      print_macaddr - print MAC address registers and MAC address in eeprom
361  */
362 static void print_macaddr(struct eth_device *dev)
363 {
364         puts("Current MAC Address in MAC:     ");
365         ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
366         ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
367         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)
374                 printf("%02x:", read_eeprom_reg(dev, i));
375         printf("%02x\n", read_eeprom_reg(dev, i));
376 }
377
378 /**
379  *      dump_eeprom - dump the whole content of the EEPROM
380  */
381 static void dump_eeprom(struct eth_device *dev)
382 {
383         int i;
384         puts("EEPROM:\n");
385         for (i = 0; i < 7; ++i)
386                 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
387 }
388
389 /**
390  *      smc911x_init - get the MAC/EEPROM up and ready for use
391  */
392 static int smc911x_init(struct eth_device *dev)
393 {
394         /* See if there is anything there */
395         if (smc911x_detect_chip(dev))
396                 return 1;
397
398         smc911x_reset(dev);
399
400         /* Make sure we set EEDIO/EECLK to the EEPROM */
401         if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
402                 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
403                         if (smsc_ctrlc()) {
404                                 printf("init: timeout (E2P_CMD = 0x%08x)\n",
405                                         smc911x_reg_read(dev, E2P_CMD));
406                                 return 1;
407                         }
408                 smc911x_reg_write(dev, GPIO_CFG,
409                         smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
410         }
411
412         return 0;
413 }
414
415 /**
416  *      getline - consume a line of input and handle some escape sequences
417  */
418 static 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  */
468 int smc911x_eeprom(int argc, char * const argv[])
469 {
470         /* Avoid initializing on stack as gcc likes to call memset() */
471         struct eth_device dev;
472         dev.iobase = CONFIG_SMC911X_BASE;
473
474         /* 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");
485         if (smc911x_init(&dev))
486                 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 */
512                 if (line[0] && line[1] && !isblank(line[1]))
513                         goto unknown_cmd;
514
515                 /* Now parse the command */
516                 switch (line[0]) {
517                 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;
522                 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 }