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