]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - board/netstar/eeprom.c
Merge branch 'master' of git://git.denx.de/u-boot-arm
[karo-tx-uboot.git] / board / netstar / eeprom.c
1 /*
2  * (C) Copyright 2005
3  * Ladislav Michl, 2N Telekomunikace, michl@2n.cz
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20  * MA 02111-1307 USA
21  *
22  * Some code shamelessly stolen back from Robin Getz.
23  */
24
25 #include <common.h>
26 #include <exports.h>
27 #include <timestamp.h>
28 #include <net.h>
29 #include "../drivers/net/smc91111.h"
30
31 static u16 read_eeprom_reg(struct eth_device *dev, u16 reg)
32 {
33         int timeout;
34
35         SMC_SELECT_BANK(dev, 2);
36         SMC_outw(dev, reg, PTR_REG);
37
38         SMC_SELECT_BANK(dev, 1);
39         SMC_outw(dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
40                  CTL_REG);
41         timeout = 100;
42         while((SMC_inw (dev, CTL_REG) & CTL_RELOAD) && --timeout)
43                 udelay(100);
44         if (timeout == 0) {
45                 printf("Timeout Reading EEPROM register %02x\n", reg);
46                 return 0;
47         }
48
49         return SMC_inw (dev, GP_REG);
50 }
51
52 static int write_eeprom_reg(struct eth_device *dev, u16 value, u16 reg)
53 {
54         int timeout;
55
56         SMC_SELECT_BANK(dev, 2);
57         SMC_outw(dev, reg, PTR_REG);
58
59         SMC_SELECT_BANK(dev, 1);
60         SMC_outw(dev, value, GP_REG);
61         SMC_outw(dev, SMC_inw (dev, CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
62         timeout = 100;
63         while ((SMC_inw(dev, CTL_REG) & CTL_STORE) && --timeout)
64                 udelay (100);
65         if (timeout == 0) {
66                 printf("Timeout Writing EEPROM register %02x\n", reg);
67                 return 0;
68         }
69
70         return 1;
71 }
72
73 static int write_data(struct eth_device *dev, u16 *buf, int len)
74 {
75         u16 reg = 0x23;
76
77         while (len--)
78                 write_eeprom_reg(dev, *buf++, reg++);
79
80         return 0;
81 }
82
83 static int verify_macaddr(struct eth_device *dev, char *s)
84 {
85         u16 reg;
86         int i, err = 0;
87
88         printf("MAC Address: ");
89         err = i = 0;
90         for (i = 0; i < 3; i++) {
91                 reg = read_eeprom_reg(dev, 0x20 + i);
92                 printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
93                 if (s)
94                         err |= reg != ((u16 *)s)[i];
95         }
96
97         return err ? 0 : 1;
98 }
99
100 static int set_mac(struct eth_device *dev, char *s)
101 {
102         int i;
103         char *e, eaddr[6];
104
105         /* turn string into mac value */
106         for (i = 0; i < 6; i++) {
107                 eaddr[i] = simple_strtoul(s, &e, 16);
108                 s = (*e) ? e+1 : e;
109         }
110
111         for (i = 0; i < 3; i++)
112                 write_eeprom_reg(dev, *(((u16 *)eaddr) + i), 0x20 + i);
113
114         return 0;
115 }
116
117 static int parse_element(char *s, unsigned char *buf, int len)
118 {
119         int cnt;
120         char *p, num[3];
121         unsigned char id;
122
123         id = simple_strtoul(s, &p, 16);
124         if (*p++ != ':')
125                 return -1;
126         cnt = 2;
127         num[2] = 0;
128         for (; *p; p += 2) {
129                 if (p[1] == 0)
130                         return -2;
131                 if (cnt + 3 > len)
132                         return -3;
133                 num[0] = p[0];
134                 num[1] = p[1];
135                 buf[cnt++] = simple_strtoul(num, NULL, 16);
136         }
137         buf[0] = id;
138         buf[1] = cnt - 2;
139
140         return cnt;
141 }
142
143 int eeprom(int argc, char *argv[])
144 {
145         int i, len, ret;
146         unsigned char buf[58], *p;
147
148         struct eth_device dev = {
149                 .iobase = CONFIG_SMC91111_BASE
150         };
151
152         app_startup(argv);
153         if (get_version() != XF_VERSION) {
154                 printf("Wrong XF_VERSION.\n");
155                 printf("Application expects ABI version %d\n", XF_VERSION);
156                 printf("Actual U-Boot ABI version %d\n", (int)get_version());
157                 return 1;
158         }
159
160         if ((SMC_inw (&dev, BANK_SELECT) & 0xFF00) != 0x3300) {
161                 printf("SMSC91111 not found.\n");
162                 return 2;
163         }
164
165         /* Called without parameters - print MAC address */
166         if (argc < 2) {
167                 verify_macaddr(&dev, NULL);
168                 return 0;
169         }
170
171         /* Print help message */
172         if (argv[1][1] == 'h') {
173                 printf("NetStar EEPROM writer\n");
174                 printf("Built: %s at %s\n", U_BOOT_DATE, U_BOOT_TIME);
175                 printf("Usage:\n\t<mac_address> [<element_1>] [<...>]\n");
176                 return 0;
177         }
178
179         /* Try to parse information elements */
180         len = sizeof(buf);
181         p = buf;
182         for (i = 2; i < argc; i++) {
183                 ret = parse_element(argv[i], p, len);
184                 switch (ret) {
185                 case -1:
186                         printf("Element %d: malformed\n", i - 1);
187                         return 3;
188                 case -2:
189                         printf("Element %d: odd character count\n", i - 1);
190                         return 3;
191                 case -3:
192                         printf("Out of EEPROM memory\n");
193                         return 3;
194                 default:
195                         p += ret;
196                         len -= ret;
197                 }
198         }
199
200         /* First argument (MAC) is mandatory */
201         set_mac(&dev, argv[1]);
202         if (verify_macaddr(&dev, argv[1])) {
203                 printf("*** MAC address does not match! ***\n");
204                 return 4;
205         }
206
207         while (len--)
208                 *p++ = 0;
209
210         write_data(&dev, (u16 *)buf, sizeof(buf) >> 1);
211
212         return 0;
213 }