]> git.sur5r.net Git - u-boot/blob - board/spear/common/spr_misc.c
SPEAr : Support for HW mac id read/write from i2c mem
[u-boot] / board / spear / common / spr_misc.c
1 /*
2  * (C) Copyright 2009
3  * Vipin Kumar, ST Micoelectronics, vipin.kumar@st.com.
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
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  */
23
24 #include <common.h>
25 #include <command.h>
26 #include <i2c.h>
27 #include <net.h>
28 #include <asm/io.h>
29 #include <asm/arch/hardware.h>
30 #include <asm/arch/spr_xloader_table.h>
31 #include <asm/arch/spr_defs.h>
32
33 #define CPU             0
34 #define DDR             1
35 #define SRAM_REL        0xD2801000
36
37 DECLARE_GLOBAL_DATA_PTR;
38 static struct chip_data chip_data;
39
40 int dram_init(void)
41 {
42         struct xloader_table *xloader_tb =
43             (struct xloader_table *)XLOADER_TABLE_ADDRESS;
44         struct xloader_table_1_1 *table_1_1;
45         struct xloader_table_1_2 *table_1_2;
46         struct chip_data *chip = &chip_data;
47
48         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
49         gd->bd->bi_dram[0].size = get_ram_size(PHYS_SDRAM_1,
50                                                PHYS_SDRAM_1_MAXSIZE);
51
52         if (XLOADER_TABLE_VERSION_1_1 == xloader_tb->table_version) {
53                 table_1_1 = &xloader_tb->table.table_1_1;
54                 chip->dramfreq = table_1_1->ddrfreq;
55                 chip->dramtype = table_1_1->ddrtype;
56
57         } else if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
58                 table_1_2 = &xloader_tb->table.table_1_2;
59                 chip->dramfreq = table_1_2->ddrfreq;
60                 chip->dramtype = table_1_2->ddrtype;
61         } else {
62                 chip->dramfreq = -1;
63         }
64
65         return 0;
66 }
67
68 int misc_init_r(void)
69 {
70 #if defined(CONFIG_CMD_NET)
71         uchar mac_id[6];
72
73         if (!eth_getenv_enetaddr("ethaddr", mac_id) && !i2c_read_mac(mac_id))
74                 eth_setenv_enetaddr("ethaddr", mac_id);
75 #endif
76         setenv("verify", "n");
77
78 #if defined(CONFIG_SPEAR_USBTTY)
79         setenv("stdin", "usbtty");
80         setenv("stdout", "usbtty");
81         setenv("stderr", "usbtty");
82 #endif
83         return 0;
84 }
85
86 int spear_board_init(ulong mach_type)
87 {
88         struct xloader_table *xloader_tb =
89             (struct xloader_table *)XLOADER_TABLE_ADDRESS;
90         struct xloader_table_1_2 *table_1_2;
91         struct chip_data *chip = &chip_data;
92
93         gd->bd->bi_arch_number = mach_type;
94
95         /* adress of boot parameters */
96         gd->bd->bi_boot_params = CONFIG_BOOT_PARAMS_ADDR;
97
98         /* CPU is initialized to work at 333MHz in Xloader */
99         chip->cpufreq = 333;
100
101         if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
102                 table_1_2 = &xloader_tb->table.table_1_2;
103                 memcpy(chip->version, table_1_2->version,
104                        sizeof(chip->version));
105         }
106
107         return 0;
108 }
109
110 static int i2c_read_mac(uchar *buffer)
111 {
112         u8 buf[2];
113
114         i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
115
116         /* Check if mac in i2c memory is valid */
117         if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
118                 /* Valid mac address is saved in i2c eeprom */
119                 i2c_read(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, buffer, MAC_LEN);
120                 return 0;
121         }
122
123         return -1;
124 }
125
126 static int write_mac(uchar *mac)
127 {
128         u8 buf[2];
129
130         buf[0] = (u8)MAGIC_BYTE0;
131         buf[1] = (u8)MAGIC_BYTE1;
132         i2c_write(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
133
134         buf[0] = (u8)~MAGIC_BYTE0;
135         buf[1] = (u8)~MAGIC_BYTE1;
136
137         i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
138
139         /* check if valid MAC address is saved in I2C EEPROM or not? */
140         if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
141                 i2c_write(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, mac, MAC_LEN);
142                 puts("I2C EEPROM written with mac address \n");
143                 return 0;
144         }
145
146         puts("I2C EEPROM writing failed \n");
147         return -1;
148 }
149
150 int do_chip_config(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
151 {
152         void (*sram_setfreq) (unsigned int, unsigned int);
153         struct chip_data *chip = &chip_data;
154         unsigned char mac[6];
155         unsigned int reg, frequency;
156         char *s, *e;
157         char i2c_mac[20];
158
159         if ((argc > 3) || (argc < 2)) {
160                 cmd_usage(cmdtp);
161                 return 1;
162         }
163
164         if ((!strcmp(argv[1], "cpufreq")) || (!strcmp(argv[1], "ddrfreq"))) {
165
166                 frequency = simple_strtoul(argv[2], NULL, 0);
167
168                 if (frequency > 333) {
169                         printf("Frequency is limited to 333MHz\n");
170                         return 1;
171                 }
172
173                 sram_setfreq = memcpy((void *)SRAM_REL, setfreq, setfreq_sz);
174
175                 if (!strcmp(argv[1], "cpufreq")) {
176                         sram_setfreq(CPU, frequency);
177                         printf("CPU frequency changed to %u\n", frequency);
178
179                         chip->cpufreq = frequency;
180                 } else {
181                         sram_setfreq(DDR, frequency);
182                         printf("DDR frequency changed to %u\n", frequency);
183
184                         chip->dramfreq = frequency;
185                 }
186
187                 return 0;
188         } else if (!strcmp(argv[1], "ethaddr")) {
189
190                 s = argv[2];
191                 for (reg = 0; reg < 6; ++reg) {
192                         mac[reg] = s ? simple_strtoul(s, &e, 16) : 0;
193                         if (s)
194                                 s = (*e) ? e + 1 : e;
195                 }
196                 write_mac(mac);
197
198                 return 0;
199         } else if (!strcmp(argv[1], "print")) {
200
201                 if (chip->cpufreq == -1)
202                         printf("CPU Freq    = Not Known\n");
203                 else
204                         printf("CPU Freq    = %d MHz\n", chip->cpufreq);
205
206                 if (chip->dramfreq == -1)
207                         printf("DDR Freq    = Not Known\n");
208                 else
209                         printf("DDR Freq    = %d MHz\n", chip->dramfreq);
210
211                 if (chip->dramtype == DDRMOBILE)
212                         printf("DDR Type    = MOBILE\n");
213                 else if (chip->dramtype == DDR2)
214                         printf("DDR Type    = DDR2\n");
215                 else
216                         printf("DDR Type    = Not Known\n");
217
218                 if (!i2c_read_mac(mac)) {
219                         sprintf(i2c_mac, "%pM", mac);
220                         printf("Ethaddr (from i2c mem) = %s\n", i2c_mac);
221                 } else {
222                         printf("Ethaddr (from i2c mem) = Not set\n");
223                 }
224
225                 printf("Xloader Rev = %s\n", chip->version);
226
227                 return 0;
228         }
229
230         cmd_usage(cmdtp);
231         return 1;
232 }
233
234 U_BOOT_CMD(chip_config, 3, 1, do_chip_config,
235            "configure chip",
236            "chip_config cpufreq/ddrfreq frequency\n"
237            "chip_config print");