19ad9e6297c77122587a1633b9cf25aded1fae4c
[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 inline u32 __smc911x_reg_read(struct eth_device *dev, u32 offset)
55 {
56         return *(volatile u32*)(dev->iobase + offset);
57 }
58 u32 smc911x_reg_read(struct eth_device *dev, u32 offset)
59         __attribute__((weak, alias("__smc911x_reg_read")));
60
61 static inline void __smc911x_reg_write(struct eth_device *dev,
62                                         u32 offset, u32 val)
63 {
64         *(volatile u32*)(dev->iobase + offset) = val;
65 }
66 void 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)
69 static 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 }
74 static 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
84 static 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
96 static 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
106 static 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
133 static 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
179 /**
180  *      smsc_ctrlc - detect press of CTRL+C (common ctrlc() isnt exported!?)
181  */
182 static int smsc_ctrlc(void)
183 {
184         return (tstc() && getc() == 0x03);
185 }
186
187 /**
188  *      usage - dump usage information
189  */
190 static 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  */
213 static void dump_regs(struct eth_device *dev)
214 {
215         u8 i, j = 0;
216         for (i = 0x50; i < 0xB8; i += sizeof(u32))
217                 printf("%02x: 0x%08x %c", i,
218                         smc911x_reg_read(dev, i),
219                         (j++ % 2 ? '\n' : ' '));
220 }
221
222 /**
223  *      do_eeprom_cmd - handle eeprom communication
224  */
225 static int do_eeprom_cmd(struct eth_device *dev, int cmd, u8 reg)
226 {
227         if (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY) {
228                 printf("eeprom_cmd: busy at start (E2P_CMD = 0x%08x)\n",
229                         smc911x_reg_read(dev, E2P_CMD));
230                 return -1;
231         }
232
233         smc911x_reg_write(dev, E2P_CMD, E2P_CMD_EPC_BUSY | cmd | reg);
234
235         while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
236                 if (smsc_ctrlc()) {
237                         printf("eeprom_cmd: timeout (E2P_CMD = 0x%08x)\n",
238                                 smc911x_reg_read(dev, E2P_CMD));
239                         return -1;
240                 }
241
242         return 0;
243 }
244
245 /**
246  *      read_eeprom_reg - read specified register in EEPROM
247  */
248 static u8 read_eeprom_reg(struct eth_device *dev, u8 reg)
249 {
250         int ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_READ, reg);
251         return (ret ? : smc911x_reg_read(dev, E2P_DATA));
252 }
253
254 /**
255  *      write_eeprom_reg - write specified value into specified register in EEPROM
256  */
257 static int write_eeprom_reg(struct eth_device *dev, u8 value, u8 reg)
258 {
259         int ret;
260
261         /* enable erasing/writing */
262         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWEN, reg);
263         if (ret)
264                 goto done;
265
266         /* erase the eeprom reg */
267         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_ERASE, reg);
268         if (ret)
269                 goto done;
270
271         /* write the eeprom reg */
272         smc911x_reg_write(dev, E2P_DATA, value);
273         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_WRITE, reg);
274         if (ret)
275                 goto done;
276
277         /* disable erasing/writing */
278         ret = do_eeprom_cmd(dev, E2P_CMD_EPC_CMD_EWDS, reg);
279
280  done:
281         return ret;
282 }
283
284 /**
285  *      skip_space - find first non-whitespace in given pointer
286  */
287 static char *skip_space(char *buf)
288 {
289         while (isblank(buf[0]))
290                 ++buf;
291         return buf;
292 }
293
294 /**
295  *      write_stuff - handle writing of MAC registers / eeprom
296  */
297 static void write_stuff(struct eth_device *dev, char *line)
298 {
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);
340                 write_eeprom_reg(dev, value, reg);
341         } else {
342                 printf("Writing MAC register %02x with %08x\n", reg, value);
343                 smc911x_reg_write(dev, reg, value);
344         }
345 }
346
347 /**
348  *      copy_from_eeprom - copy MAC address in eeprom to address registers
349  */
350 static void copy_from_eeprom(struct eth_device *dev)
351 {
352         ulong addrl =
353                 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;
357         ulong addrh =
358                 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);
362         puts("EEPROM contents copied to MAC\n");
363 }
364
365 /**
366  *      print_macaddr - print MAC address registers and MAC address in eeprom
367  */
368 static void print_macaddr(struct eth_device *dev)
369 {
370         puts("Current MAC Address in MAC:     ");
371         ulong addrl = smc911x_get_mac_csr(dev, ADDRL);
372         ulong addrh = smc911x_get_mac_csr(dev, ADDRH);
373         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)
380                 printf("%02x:", read_eeprom_reg(dev, i));
381         printf("%02x\n", read_eeprom_reg(dev, i));
382 }
383
384 /**
385  *      dump_eeprom - dump the whole content of the EEPROM
386  */
387 static void dump_eeprom(struct eth_device *dev)
388 {
389         int i;
390         puts("EEPROM:\n");
391         for (i = 0; i < 7; ++i)
392                 printf("%02x: 0x%02x\n", i, read_eeprom_reg(dev, i));
393 }
394
395 /**
396  *      smc911x_init - get the MAC/EEPROM up and ready for use
397  */
398 static int smc911x_init(struct eth_device *dev)
399 {
400         /* See if there is anything there */
401         if (smc911x_detect_chip(dev))
402                 return 1;
403
404         smc911x_reset(dev);
405
406         /* Make sure we set EEDIO/EECLK to the EEPROM */
407         if (smc911x_reg_read(dev, GPIO_CFG) & GPIO_CFG_EEPR_EN) {
408                 while (smc911x_reg_read(dev, E2P_CMD) & E2P_CMD_EPC_BUSY)
409                         if (smsc_ctrlc()) {
410                                 printf("init: timeout (E2P_CMD = 0x%08x)\n",
411                                         smc911x_reg_read(dev, E2P_CMD));
412                                 return 1;
413                         }
414                 smc911x_reg_write(dev, GPIO_CFG,
415                         smc911x_reg_read(dev, GPIO_CFG) & ~GPIO_CFG_EEPR_EN);
416         }
417
418         return 0;
419 }
420
421 /**
422  *      getline - consume a line of input and handle some escape sequences
423  */
424 static 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  */
474 int smc911x_eeprom(int argc, char * const argv[])
475 {
476         /* Avoid initializing on stack as gcc likes to call memset() */
477         struct eth_device dev;
478         dev.iobase = CONFIG_SMC911X_BASE;
479
480         /* 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");
491         if (smc911x_init(&dev))
492                 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 */
518                 if (line[0] && line[1] && !isblank(line[1]))
519                         goto unknown_cmd;
520
521                 /* Now parse the command */
522                 switch (line[0]) {
523                 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;
528                 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 }