Merge commit '7b2fac7654f7420c2787f74ec3b1540fa3b343e9'
[oweals/u-boot.git] / 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_emi.h>
31 #include <asm/arch/spr_xloader_table.h>
32 #include <asm/arch/spr_defs.h>
33
34 #define CPU             0
35 #define DDR             1
36 #define SRAM_REL        0xD2801000
37
38 DECLARE_GLOBAL_DATA_PTR;
39 static struct chip_data chip_data;
40
41 int dram_init(void)
42 {
43         struct xloader_table *xloader_tb =
44             (struct xloader_table *)XLOADER_TABLE_ADDRESS;
45         struct xloader_table_1_1 *table_1_1;
46         struct xloader_table_1_2 *table_1_2;
47         struct chip_data *chip = &chip_data;
48
49         gd->ram_size = get_ram_size(PHYS_SDRAM_1, PHYS_SDRAM_1_MAXSIZE);
50
51         if (XLOADER_TABLE_VERSION_1_1 == xloader_tb->table_version) {
52                 table_1_1 = &xloader_tb->table.table_1_1;
53                 chip->dramfreq = table_1_1->ddrfreq;
54                 chip->dramtype = table_1_1->ddrtype;
55
56         } else if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
57                 table_1_2 = &xloader_tb->table.table_1_2;
58                 chip->dramfreq = table_1_2->ddrfreq;
59                 chip->dramtype = table_1_2->ddrtype;
60         } else {
61                 chip->dramfreq = -1;
62         }
63
64         return 0;
65 }
66
67 void dram_init_banksize(void)
68 {
69         gd->bd->bi_dram[0].start = PHYS_SDRAM_1;
70         gd->bd->bi_dram[0].size = gd->ram_size;
71 }
72
73 int misc_init_r(void)
74 {
75 #if defined(CONFIG_CMD_NET)
76         uchar mac_id[6];
77
78         if (!eth_getenv_enetaddr("ethaddr", mac_id) && !i2c_read_mac(mac_id))
79                 eth_setenv_enetaddr("ethaddr", mac_id);
80 #endif
81         setenv("verify", "n");
82
83 #if defined(CONFIG_SPEAR_USBTTY)
84         setenv("stdin", "usbtty");
85         setenv("stdout", "usbtty");
86         setenv("stderr", "usbtty");
87 #endif
88         return 0;
89 }
90
91 #ifdef CONFIG_SPEAR_EMI
92 struct cust_emi_para {
93         unsigned int tap;
94         unsigned int tsdp;
95         unsigned int tdpw;
96         unsigned int tdpr;
97         unsigned int tdcs;
98 };
99
100 /* EMI timing setting of m28w640hc of linux kernel */
101 const struct cust_emi_para emi_timing_m28w640hc = {
102         .tap = 0x10,
103         .tsdp = 0x05,
104         .tdpw = 0x0a,
105         .tdpr = 0x0a,
106         .tdcs = 0x05,
107 };
108
109 /* EMI timing setting of bootrom */
110 const struct cust_emi_para emi_timing_bootrom = {
111         .tap = 0xf,
112         .tsdp = 0x0,
113         .tdpw = 0xff,
114         .tdpr = 0x111,
115         .tdcs = 0x02,
116 };
117
118 void spear_emi_init(void)
119 {
120         const struct cust_emi_para *p = &emi_timing_m28w640hc;
121         struct emi_regs *emi_regs_p = (struct emi_regs *)CONFIG_SPEAR_EMIBASE;
122         unsigned int cs;
123         unsigned int val, tmp;
124
125         val = readl(CONFIG_SPEAR_RASBASE);
126
127         if (val & EMI_ACKMSK)
128                 tmp = 0x3f;
129         else
130                 tmp = 0x0;
131
132         writel(tmp, &emi_regs_p->ack);
133
134         for (cs = 0; cs < CONFIG_SYS_MAX_FLASH_BANKS; cs++) {
135                 writel(p->tap, &emi_regs_p->bank_regs[cs].tap);
136                 writel(p->tsdp, &emi_regs_p->bank_regs[cs].tsdp);
137                 writel(p->tdpw, &emi_regs_p->bank_regs[cs].tdpw);
138                 writel(p->tdpr, &emi_regs_p->bank_regs[cs].tdpr);
139                 writel(p->tdcs, &emi_regs_p->bank_regs[cs].tdcs);
140                 writel(EMI_CNTL_ENBBYTERW | ((val & 0x18) >> 3),
141                        &emi_regs_p->bank_regs[cs].control);
142         }
143 }
144 #endif
145
146 int spear_board_init(ulong mach_type)
147 {
148         struct xloader_table *xloader_tb =
149             (struct xloader_table *)XLOADER_TABLE_ADDRESS;
150         struct xloader_table_1_2 *table_1_2;
151         struct chip_data *chip = &chip_data;
152
153         gd->bd->bi_arch_number = mach_type;
154
155         /* adress of boot parameters */
156         gd->bd->bi_boot_params = CONFIG_BOOT_PARAMS_ADDR;
157
158         /* CPU is initialized to work at 333MHz in Xloader */
159         chip->cpufreq = 333;
160
161         if (XLOADER_TABLE_VERSION_1_2 == xloader_tb->table_version) {
162                 table_1_2 = &xloader_tb->table.table_1_2;
163                 memcpy(chip->version, table_1_2->version,
164                        sizeof(chip->version));
165         }
166
167 #ifdef CONFIG_SPEAR_EMI
168         spear_emi_init();
169 #endif
170         return 0;
171 }
172
173 static int i2c_read_mac(uchar *buffer)
174 {
175         u8 buf[2];
176
177         i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
178
179         /* Check if mac in i2c memory is valid */
180         if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
181                 /* Valid mac address is saved in i2c eeprom */
182                 i2c_read(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, buffer, MAC_LEN);
183                 return 0;
184         }
185
186         return -1;
187 }
188
189 static int write_mac(uchar *mac)
190 {
191         u8 buf[2];
192
193         buf[0] = (u8)MAGIC_BYTE0;
194         buf[1] = (u8)MAGIC_BYTE1;
195         i2c_write(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
196
197         buf[0] = (u8)~MAGIC_BYTE0;
198         buf[1] = (u8)~MAGIC_BYTE1;
199
200         i2c_read(CONFIG_I2C_CHIPADDRESS, MAGIC_OFF, 1, buf, MAGIC_LEN);
201
202         /* check if valid MAC address is saved in I2C EEPROM or not? */
203         if ((buf[0] == MAGIC_BYTE0) && (buf[1] == MAGIC_BYTE1)) {
204                 i2c_write(CONFIG_I2C_CHIPADDRESS, MAC_OFF, 1, mac, MAC_LEN);
205                 puts("I2C EEPROM written with mac address \n");
206                 return 0;
207         }
208
209         puts("I2C EEPROM writing failed \n");
210         return -1;
211 }
212
213 int do_chip_config(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
214 {
215         void (*sram_setfreq) (unsigned int, unsigned int);
216         struct chip_data *chip = &chip_data;
217         unsigned char mac[6];
218         unsigned int reg, frequency;
219         char *s, *e;
220         char i2c_mac[20];
221
222         if ((argc > 3) || (argc < 2))
223                 return cmd_usage(cmdtp);
224
225         if ((!strcmp(argv[1], "cpufreq")) || (!strcmp(argv[1], "ddrfreq"))) {
226
227                 frequency = simple_strtoul(argv[2], NULL, 0);
228
229                 if (frequency > 333) {
230                         printf("Frequency is limited to 333MHz\n");
231                         return 1;
232                 }
233
234                 sram_setfreq = memcpy((void *)SRAM_REL, setfreq, setfreq_sz);
235
236                 if (!strcmp(argv[1], "cpufreq")) {
237                         sram_setfreq(CPU, frequency);
238                         printf("CPU frequency changed to %u\n", frequency);
239
240                         chip->cpufreq = frequency;
241                 } else {
242                         sram_setfreq(DDR, frequency);
243                         printf("DDR frequency changed to %u\n", frequency);
244
245                         chip->dramfreq = frequency;
246                 }
247
248                 return 0;
249         } else if (!strcmp(argv[1], "ethaddr")) {
250
251                 s = argv[2];
252                 for (reg = 0; reg < 6; ++reg) {
253                         mac[reg] = s ? simple_strtoul(s, &e, 16) : 0;
254                         if (s)
255                                 s = (*e) ? e + 1 : e;
256                 }
257                 write_mac(mac);
258
259                 return 0;
260         } else if (!strcmp(argv[1], "print")) {
261
262                 if (chip->cpufreq == -1)
263                         printf("CPU Freq    = Not Known\n");
264                 else
265                         printf("CPU Freq    = %d MHz\n", chip->cpufreq);
266
267                 if (chip->dramfreq == -1)
268                         printf("DDR Freq    = Not Known\n");
269                 else
270                         printf("DDR Freq    = %d MHz\n", chip->dramfreq);
271
272                 if (chip->dramtype == DDRMOBILE)
273                         printf("DDR Type    = MOBILE\n");
274                 else if (chip->dramtype == DDR2)
275                         printf("DDR Type    = DDR2\n");
276                 else
277                         printf("DDR Type    = Not Known\n");
278
279                 if (!i2c_read_mac(mac)) {
280                         sprintf(i2c_mac, "%pM", mac);
281                         printf("Ethaddr (from i2c mem) = %s\n", i2c_mac);
282                 } else {
283                         printf("Ethaddr (from i2c mem) = Not set\n");
284                 }
285
286                 printf("Xloader Rev = %s\n", chip->version);
287
288                 return 0;
289         }
290
291         return cmd_usage(cmdtp);
292 }
293
294 U_BOOT_CMD(chip_config, 3, 1, do_chip_config,
295            "configure chip",
296            "chip_config cpufreq/ddrfreq frequency\n"
297            "chip_config print");