]> git.karo-electronics.de Git - karo-tx-uboot.git/blob - board/voiceblue/eeprom.c
6383a0203b5be729a51aa652f8c33f04d044ed84
[karo-tx-uboot.git] / board / voiceblue / 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 #define DEBUG
26
27 #include <common.h>
28 #include <exports.h>
29 #include "../drivers/smc91111.h"
30
31 #define SMC_BASE_ADDRESS CONFIG_SMC91111_BASE
32
33 static int verify_macaddr(char *);
34 static int set_mac(char *);
35
36 int eeprom(int argc, char *argv[])
37 {
38         app_startup(argv);
39         if (get_version() != XF_VERSION) {
40                 printf("Wrong XF_VERSION.\n");
41                 printf("Application expects ABI version %d\n", XF_VERSION);
42                 printf("Actual U-Boot ABI version %d\n", (int)get_version());
43                 return 1;
44         }
45
46         if ((SMC_inw (BANK_SELECT) & 0xFF00) != 0x3300) {
47                 printf("SMSC91111 not found.\n");
48                 return 2;
49         }
50
51         if (argc != 2) {
52                 printf("VoiceBlue EEPROM writer\n");
53                 printf("Built: %s at %s\n", __DATE__ , __TIME__ );
54                 printf("Usage:\n\t<mac_address>");
55                 return 3;
56         }
57
58         set_mac(argv[1]);
59         if (verify_macaddr(argv[1])) {
60                 printf("*** ERROR ***\n");
61                 return 4;
62         }
63
64         return 0;
65 }
66
67 static u16 read_eeprom_reg(u16 reg)
68 {
69         int timeout;
70
71         SMC_SELECT_BANK(2);
72         SMC_outw(reg, PTR_REG);
73
74         SMC_SELECT_BANK(1);
75         SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_RELOAD,
76                  CTL_REG);
77         timeout = 100;
78         while((SMC_inw (CTL_REG) & CTL_RELOAD) && --timeout)
79                 udelay(100);
80         if (timeout == 0) {
81                 printf("Timeout Reading EEPROM register %02x\n", reg);
82                 return 0;
83         }
84
85         return SMC_inw (GP_REG);
86 }
87
88 static int write_eeprom_reg(u16 value, u16 reg)
89 {
90         int timeout;
91
92         SMC_SELECT_BANK(2);
93         SMC_outw(reg, PTR_REG);
94
95         SMC_SELECT_BANK(1);
96         SMC_outw(value, GP_REG);
97         SMC_outw(SMC_inw (CTL_REG) | CTL_EEPROM_SELECT | CTL_STORE, CTL_REG);
98         timeout = 100;
99         while ((SMC_inw(CTL_REG) & CTL_STORE) && --timeout)
100                 udelay (100);
101         if (timeout == 0) {
102                 printf("Timeout Writing EEPROM register %02x\n", reg);
103                 return 0;
104         }
105
106         return 1;
107 }
108
109 static int verify_macaddr(char *s)
110 {
111         u16 reg;
112         int i, err = 0;
113
114         printf("Verifying MAC Address: ");
115         err = i = 0;
116         for (i = 0; i < 3; i++) {
117                 reg = read_eeprom_reg(0x20 + i);
118                 printf("%02x:%02x%c", reg & 0xff, reg >> 8, i != 2 ? ':' : '\n');
119                 err |= reg != ((u16 *)s)[i];
120         }
121
122         return err ? 0 : 1;
123 }
124
125 static int set_mac(char *s)
126 {
127         int i;
128         char *e, eaddr[6];
129
130         /* turn string into mac value */
131         for (i = 0; i < 6; i++) {
132                 eaddr[i] = simple_strtoul(s, &e, 16);
133                 s = (*e) ? e+1 : e;
134         }
135
136         for (i = 0; i < 3; i++)
137                 write_eeprom_reg(*(((u16 *)eaddr) + i), 0x20 + i);
138
139         return 0;
140 }