Initial checkin
[pandora-x-loader.git] / board / omap2430sdp / omap2430sdp.c
1 /*
2  * (C) Copyright 2004-2005
3  * Texas Instruments, <www.ti.com>
4  * Jian Zhang <jzhang@ti.com>
5  * Richard Woodruff <r-woodruff2@ti.com>
6  *
7  * See file CREDITS for list of people who contributed to this
8  * project.
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License as
12  * published by the Free Software Foundation; either version 2 of
13  * the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
23  * MA 02111-1307 USA
24  */
25 #include <common.h>
26 #include <asm/arch/omap2430.h>
27 #include <asm/arch/bits.h>
28 #include <asm/arch/mem.h>
29 #include <asm/arch/sys_info.h>
30 #include <asm/arch/clocks.h>
31
32 static void wait_for_command_complete(unsigned int wd_base);
33 static void watchdog_init(void);
34 static void peripheral_enable(void);
35 static void muxSetupAll(void);
36 static u32  get_cpu_rev(void);
37 static u32  get_device_type(void);
38 static void prcm_init(void);
39
40
41 /*******************************************************
42  * Routine: delay
43  * Description: spinning delay to use before udelay works
44  ******************************************************/
45 static inline void delay (unsigned long loops)
46 {
47         __asm__ volatile ("1:\n"
48                                           "subs %0, %1, #1\n"
49                                           "bne 1b":"=r" (loops):"0" (loops));
50 }
51
52 /*****************************************
53  * Routine: board_init
54  * Description: Early hardware init.
55  *****************************************/
56 int board_init (void)
57 {
58         return 0;
59 }
60
61 /******************************************
62  * get_cpu_rev(void) - extract version info
63  ******************************************/
64 u32 get_cpu_rev(void)
65 {
66         u32 v;
67         v = __raw_readl(TAP_IDCODE_REG);
68         v = v >> 28;
69         return(v+1);  /* currently 2422 and 2420 match up */
70 }
71
72 /*************************************************************
73  *  get_device_type(): tell if GP/HS/EMU/TST
74  *************************************************************/
75 u32 get_device_type(void)
76 {
77         int mode;
78         mode = __raw_readl(CONTROL_STATUS) & (DEVICE_MASK);
79         return(mode >>= 8);
80 }
81
82 /*************************************************************
83  *  Helper function to wait for the status of a register
84  *************************************************************/
85 u32 wait_on_value(u32 read_bit_mask, u32 match_value, u32 read_addr, u32 bound)
86 {
87         u32 i = 0, val;
88         do {
89                 ++i;
90                 val = __raw_readl(read_addr) & read_bit_mask;
91                 if (val == match_value)
92                         return(1);
93                 if (i==bound)
94                         return(0);
95         } while (1);
96 }
97
98 /*************************************************************
99  *  Support for multiple type of memory types
100  *************************************************************/
101 #ifdef CFG_SDRAM_DDR
102 void
103 config_sdram_ddr(u32 rev)
104 {
105         /* ball D11, mode 0 */
106         __raw_writeb(0x08, 0x48000032);
107
108         /* SDRC_CS0 Configuration */
109         __raw_writel(H4_2420_SDRC_MDCFG_0_DDR, SDRC_MCFG_0);
110         __raw_writel(H4_2420_SDRC_SHARING, SDRC_SHARING);
111
112         __raw_writel(H4_242x_SDRC_RFR_CTRL_ES1, SDRC_RFR_CTRL);
113         __raw_writel(H4_242x_SDRC_ACTIM_CTRLA_0_ES1, SDRC_ACTIM_CTRLA_0);
114         __raw_writel(H4_242x_SDRC_ACTIM_CTRLB_0_ES1, SDRC_ACTIM_CTRLB_0);
115
116         /* Manual Command sequence */
117         __raw_writel(CMD_NOP, SDRC_MANUAL_0);
118         __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_0);
119         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
120         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
121
122
123         /* 
124          * CS0 SDRC Mode Register
125          * Burst length = 4 - DDR memory
126          * Serial mode
127          * CAS latency = 3 
128          */
129         __raw_writel(0x00000032, SDRC_MR_0);
130
131         /* SDRC DLLA control register */
132         /* Delay is 90 degrees */
133         /* Enable DLL, Load counter with 115 (middle of range) */ 
134         __raw_writel(0x00000008, SDRC_DLLA_CTRL);       // ES2.x
135         /* Enable DLL, Load counter with 128 (middle of range) */ 
136         __raw_writel(0x00000008, SDRC_DLLB_CTRL);       // ES2.x
137
138 }
139 #endif // CFG_SDRAM_DDR
140
141 #ifdef CFG_SDRAM_COMBO 
142 void
143 config_sdram_combo(u32 rev)
144 {
145
146         u32 dllctrl=0;
147
148         /* ball C12, mode 0 */
149         __raw_writeb(0x00, 0x480000a1);
150         /* ball D11, mode 0 */
151         __raw_writeb(0x00, 0x48000032);
152         /* ball B13, mode 0 - for CKE1 (not needed rkw for combo) */
153         __raw_writeb(0x00, 0x480000a3);
154
155         /*configure sdrc 32 bit for COMBO ddr sdram. Issue soft reset */
156         __raw_writel(0x00000012, SDRC_SYSCONFIG);
157         wait_on_value(BIT0, BIT0, SDRC_STATUS, 12000000); /* wait till reset done set */
158         __raw_writel(0x00000000, SDRC_SYSCONFIG);
159
160         /* SDRCTriState: no Tris */
161         /* CS0MuxCfg: 000 (32-bit SDRAM on D31..0) */
162         if (rev == CPU_2420_2422_ES1)
163                 __raw_writel(H4_2422_SDRC_SHARING, SDRC_SHARING);
164         else
165                 __raw_writel(H4_2420_SDRC_SHARING, SDRC_SHARING);
166
167
168         /* CS0 SDRC Memory Configuration, */
169         /* DDR-SDRAM, External SDRAM is x32bit, */
170         /* Configure to MUX9: 1x8Mbx32  */
171         __raw_writel(H4_2420_COMBO_MDCFG_0_DDR, SDRC_MCFG_0);
172         __raw_writel(H4_2420_SDRC_ACTIM_CTRLA_0, SDRC_ACTIM_CTRLA_0);
173         __raw_writel(H4_2420_SDRC_ACTIM_CTRLB_0, SDRC_ACTIM_CTRLB_0);
174         
175         /* This is reqd only for ES1 */
176         if (rev == CPU_242X_ES1)
177                 __raw_writel(H4_242x_SDRC_RFR_CTRL_ES1, SDRC_RFR_CTRL);
178
179         /* Manual Command sequence */
180         __raw_writel(CMD_NOP, SDRC_MANUAL_0);
181         delay(5000);
182         __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_0);
183         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
184         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
185
186         /* CS0 SDRC Mode Register */
187         /* Burst length = 4 - DDR memory */
188         /* Serial mode */
189         /* CAS latency = 3  */
190         __raw_writel(H4_2422_SDRC_MR_0_DDR, SDRC_MR_0);
191
192         /* CS1 SDRC Memory Configuration, */
193         /* DDR-SDRAM, External SDRAM is x32bit, */
194         /* Configure to MUX9: 1x8Mbx32 */
195         __raw_writel(H4_2420_COMBO_MDCFG_0_DDR, SDRC_MCFG_1);
196         __raw_writel(H4_242X_SDRC_ACTIM_CTRLA_0_100MHz, SDRC_ACTIM_CTRLA_1);
197         __raw_writel(H4_2420_SDRC_ACTIM_CTRLB_0, SDRC_ACTIM_CTRLB_1);
198         /* This is reqd only for ES1 */
199         if (rev == CPU_242X_ES1)
200                 __raw_writel(H4_242x_SDRC_RFR_CTRL_ES1, 0x680090d4);
201
202         /* Manual Command sequence */
203         __raw_writel(CMD_NOP, 0x680090d8);
204         __raw_writel(CMD_PRECHARGE, 0x680090d8);
205         __raw_writel(CMD_AUTOREFRESH, 0x680090d8);
206         __raw_writel(CMD_AUTOREFRESH, 0x680090d8);
207
208         /* CS1 SDRC Mode Register */
209         /* Burst length = 4 - DDR memory */
210         /* Serial mode */
211         /* CAS latency = 3 */
212         __raw_writel(H4_2422_SDRC_MR_0_DDR, 0x680090b4);
213
214         /* SDRC DLLA control register */
215         /* Delay is 90 degrees */
216
217         if (rev == CPU_242X_ES1)
218                 dllctrl = (BIT0|BIT3);
219         else
220                 dllctrl = BIT0;         
221
222         if (rev == CPU_2420_2422_ES1) {
223                 /* Enable DLL, Load counter with 115 (middle of range) */
224                 __raw_writel(0x00007306, SDRC_DLLA_CTRL);
225                 __raw_writel(0x00007302, SDRC_DLLA_CTRL);
226                 /* Enable DLL, Load counter with 128 (middle of range)  */
227                 __raw_writel(0x00007306, SDRC_DLLB_CTRL); /* load ctr value */
228                 __raw_writel(0x00007302, SDRC_DLLB_CTRL); /* lock and go */
229         }
230         else {
231                 /* Enable DLL, Load counter with 115 (middle of range) */
232                 __raw_writel(H4_2420_SDRC_DLLAB_CTRL, SDRC_DLLA_CTRL);   // ES2.x
233                 __raw_writel(H4_2420_SDRC_DLLAB_CTRL & ~(LOADDLL|dllctrl), SDRC_DLLA_CTRL);   // ES2.x
234                // __raw_writel(0x00009808, SDRC_DLLA_CTRL);   // ES2.x
235                 /* Enable DLL, Load counter with 128 (middle of range) */
236                 __raw_writel(H4_2420_SDRC_DLLAB_CTRL, SDRC_DLLB_CTRL);   // ES2.x ?
237                 __raw_writel(H4_2420_SDRC_DLLAB_CTRL & ~(LOADDLL|dllctrl), SDRC_DLLB_CTRL);   // ES2.x
238                 //__raw_writel(0x00009808, SDRC_DLLB_CTRL);   // ES2.x
239         }
240 }
241
242 #endif // CFG_SDRAM_COMBO
243
244 #ifdef CFG_2430SDRAM_DDR
245 void
246 config_2430sdram_ddr(u32 rev)
247 {
248         u32 dllstat, dllctrl;
249
250         __raw_writel(0x00000012, SDRC_SYSCONFIG);
251         wait_on_value(BIT0, BIT0, SDRC_STATUS, 12000000); /* wait till reset done set */
252         __raw_writel(0x00000000, SDRC_SYSCONFIG);
253
254         /* Chip-level shared interface management */
255         /* SDRCTriState: no Tris */
256         /* CS0MuxCfg: 000 (32-bit SDRAM on D31..0) */
257         /* CS1MuxCfg: 000 (32-bit SDRAM on D31..0) */
258         __raw_writel(H4_2420_SDRC_SHARING, SDRC_SHARING);
259
260         /* CS0 SDRC Memory Configuration, */
261         /* DDR-SDRAM, External SDRAM is x32bit, */
262         /* Configure to MUX14: 32Mbx32 */
263         __raw_writel(SDP_2430_SDRC_MDCFG_0_DDR, SDRC_MCFG_0);
264         __raw_writel(SDP_2430_SDRC_ACTIM_CTRLA_0, SDRC_ACTIM_CTRLA_0);
265         __raw_writel(H4_2420_SDRC_ACTIM_CTRLB_0, SDRC_ACTIM_CTRLB_0);
266
267         __raw_writel(H4_2420_SDRC_RFR_CTRL, SDRC_RFR_CTRL);
268
269         /* Manual Command sequence */
270         __raw_writel(CMD_NOP, SDRC_MANUAL_0);
271         delay(5000);
272         __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_0);
273         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
274         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
275  
276         /* CS0 SDRC Mode Register */
277         /* Burst length = 2 - SDR memory */
278         /* Serial mode */
279         /* CAS latency = 3  */
280         __raw_writel(H4_2420_SDRC_MR_0_DDR, SDRC_MR_0);
281
282         /* Set up SDRC DLL values for 2430 DDR */
283         dllctrl = (SDP_2430_SDRC_DLLAB_CTRL & ~BIT2); /* set target ctrl val */
284         __raw_writel(dllctrl, SDRC_DLLA_CTRL);  /* set lock mode */
285         __raw_writel(dllctrl, SDRC_DLLB_CTRL);  /* set lock mode */
286         delay(0x1000); /* time to track to center */    
287         dllstat = __raw_readl(SDRC_DLLA_STATUS) & 0xFF00; /* get status */
288         dllctrl = (dllctrl & 0x00FF) | dllstat | BIT2; /* build unlock value */
289         __raw_writel(dllctrl, SDRC_DLLA_CTRL);  /* set unlock mode */
290         __raw_writel(dllctrl, SDRC_DLLB_CTRL);  /* set unlock mode */
291 }
292 #endif // CFG_2430SDRAM_DDR
293
294 #ifdef CFG_SDRAM_STACKED 
295 void
296 config_sdram_stacked(u32 rev)
297 {
298
299         /* Pin Muxing for SDRC */
300         __raw_writeb(0x00, 0x480000a1); /* mux mode 0 (CS1) */
301         __raw_writeb(0x00, 0x480000a3); /* mux mode 0 (CKE1) */
302         __raw_writeb(0x00, 0x48000032); /* connect sdrc_a12 */
303         __raw_writeb(0x00, 0x48000031); /* connect sdrc_a13 */
304
305         /* configure sdrc 32 bit for COMBO ddr sdram */
306         __raw_writel(0x00000010, SDRC_SYSCONFIG);       /* no idle ack and RESET enable */
307         delay(200000);
308         __raw_writel(0x00000010, SDRC_SYSCONFIG);       /* smart idle mode */
309
310         /* SDRC_SHARING */
311         /* U-boot is writing 0x00000100 though (H4_2420_SDRC_SHARING ) */
312         //__raw_writel(H4_2420_SDRC_SHARING, SDRC_SHARING);
313
314         __raw_writel(0x00004900, SDRC_SHARING);
315
316         /* SDRC_CS0 Configuration */
317         /* None for ES2.1 */
318
319         /*  SDRC_CS1 Configuration */
320         __raw_writel(0x00000000, SDRC_CS_CFG);  /* Remap CS1 to 0x80000000 */
321
322         /* Disable power down of CKE */
323         __raw_writel(0x00000085, SDRC_POWER);
324
325         __raw_writel(0x01A02019, SDRC_MCFG_1);  /* SDRC_MCFG1 */
326         __raw_writel(0x0003DD03, SDRC_RFR_CTRL1);       /* SDRC_RFR_CTRL1 */
327         __raw_writel(0x92DDC485, SDRC_ACTIM_CTRLA_1);   /* SDRC_ACTIM_CTRLA0 */
328         __raw_writel(0x00000014, SDRC_ACTIM_CTRLB_1);   /* SDRC_ACTIM_CTRLB0 */
329
330         /*Manual Command sequence */
331         __raw_writel(0x00000000, 0x680090D8);
332         __raw_writel(0x00000001, 0x680090D8);
333         __raw_writel(0x00000002, 0x680090D8);
334         __raw_writel(0x00000002, 0x680090D8);
335
336         /* CS0 SDRC Mode Register */
337         /* Burst length = 4 - DDR memory */
338         /* Serial mode */
339         /* CAS latency = 3  */
340         __raw_writel(0x00000032, 0x680090B4);
341         __raw_writel(0x00000020, 0x680090BC);   /* weak-strength driver */
342
343         /* SDRC DLLA control register */
344         /* Delay is 90 degrees */
345         if (rev == CPU_2420_2422_ES1) {
346                 /* Enable DLL, Load counter with 115 (middle of range) */ 
347                 __raw_writel(0x00007302, SDRC_DLLA_CTRL);
348                 /* Enable DLL, Load counter with 128 (middle of range) */ 
349                 __raw_writel(0x00007302, SDRC_DLLB_CTRL);
350         }
351         else {
352                 /* Enable DLL, Load counter with 115 (middle of range) */ 
353                 __raw_writel(0x00003108, SDRC_DLLA_CTRL);       // ES2.x
354                 /* Enable DLL, Load counter with 128 (middle of range) */ 
355                 __raw_writel(0x00003108, SDRC_DLLB_CTRL);       // ES2.x
356         }
357 }
358 #endif // CFG_SDRAM_STACKED
359
360 /*************************************************************
361  * get_sys_clk_speed - determine reference oscillator speed
362  *  based on known 32kHz clock and gptimer.
363  *************************************************************/
364 u32 get_osc_clk_speed(u32 *shift)
365 {
366 #define GPT_EN  ((0<<2)|BIT1|BIT0)      /* enable sys_clk NO-prescale /1 */
367 #define GPT_CTR OMAP24XX_GPT2+TCRR      /* read counter address */
368         u32 start, cstart, cend, cdiff, val;
369         unsigned int v, if_clks=0, func_clks=0 ;
370
371
372
373         if(__raw_readl(PRCM_CLKSRC_CTRL) & BIT7){    /* if currently /2 */
374                 *shift = 1;
375         }else{
376                 *shift = 0;
377         }
378
379         /* enable timer2 */
380         val = __raw_readl(CM_CLKSEL2_CORE) | 0x4;      /* mask for sys_clk use */
381         __raw_writel(val, CM_CLKSEL2_CORE);            /* timer2 source to sys_clk */
382         __raw_writel(BIT4, CM_ICLKEN1_CORE);           /* timer2 interface clock on */
383         __raw_writel(BIT4, CM_FCLKEN1_CORE);           /* timer2 function clock on */
384         /* Enable GP2 timer.*/
385         if_clks |= BIT4;
386         func_clks |= BIT4;
387         v = __raw_readl(CM_ICLKEN1_CORE) | if_clks;     /* Interface clocks on */
388         __raw_writel(v,CM_ICLKEN1_CORE );
389         v = __raw_readl(CM_FCLKEN1_CORE) | func_clks; /* Functional Clocks on */
390         __raw_writel(v, CM_FCLKEN1_CORE);
391         __raw_writel(0, OMAP24XX_GPT2+TLDR);           /* start counting at 0 */
392         __raw_writel(GPT_EN, OMAP24XX_GPT2+TCLR);      /* enable clock */
393         /* enable 32kHz source */                      /* enabled out of reset */
394         /* determine sys_clk via gauging */
395         start = 20 + __raw_readl(S32K_CR);             /* start time in 20 cycles*/
396         while(__raw_readl(S32K_CR) < start);           /* dead loop till start time */
397         cstart = __raw_readl(GPT_CTR);                 /* get start sys_clk count */
398         while(__raw_readl(S32K_CR) < (start+20));      /* wait for 40 cycles */
399         cend = __raw_readl(GPT_CTR);                   /* get end sys_clk count */
400         cdiff = cend - cstart;                         /* get elapsed ticks */
401         /* based on number of ticks assign speed */
402         if(cdiff > (19000 >> *shift))
403                 return(S38_4M);
404         else if (cdiff > (15200 >> *shift))
405                 return(S26M);
406         else if (cdiff > (13000 >> *shift))
407                 return(S24M);
408         else if (cdiff > (9000 >> *shift))
409                 return(S19_2M);
410         else if (cdiff > (7600 >> *shift))
411                 return(S13M);
412         else
413                 return(S12M);
414 }
415
416 /*********************************************************************************
417  * prcm_init() - inits clocks for PRCM as defined in clocks.h (config II default).
418  *   -- called from SRAM
419  *********************************************************************************/
420 void
421 prcm_init()
422 {
423         u32 div, speed, val, div_by_2;
424
425         val = __raw_readl(PRCM_CLKSRC_CTRL) & ~(BIT1 | BIT0);
426 #if defined(OMAP2430_SQUARE_CLOCK_INPUT)
427         __raw_writel(val, PRCM_CLKSRC_CTRL);
428 #else
429         __raw_writel((val | BIT0), PRCM_CLKSRC_CTRL);
430 #endif
431         speed = get_osc_clk_speed(&div_by_2);
432         if((speed > S19_2M) && (!div_by_2)){ /* if fast && /2 off, enable it */
433                 val = ~(BIT6|BIT7) & __raw_readl(PRCM_CLKSRC_CTRL);
434                 val |= (0x2 << 6); /* divide by 2 if (24,26,38.4) -> (12/13/19.2)  */
435                 __raw_writel(val, PRCM_CLKSRC_CTRL);
436         }
437
438         __raw_writel(0, CM_FCLKEN1_CORE);       /* stop all clocks to reduce ringing */
439         __raw_writel(0, CM_FCLKEN2_CORE);       /* may not be necessary */
440         __raw_writel(0, CM_ICLKEN1_CORE);
441         __raw_writel(0, CM_ICLKEN2_CORE);
442
443         /*DPLL into low power bypass (others off) */
444         __raw_writel(0x00000001, CM_CLKEN_PLL);
445
446         __raw_writel(DPLL_OUT, CM_CLKSEL2_PLL); /* set DPLL out */
447         __raw_writel(MPU_DIV, CM_CLKSEL_MPU);   /* set MPU divider */
448         __raw_writel(DSP_DIV, CM_CLKSEL_DSP);   /* set dsp and iva dividers */
449         __raw_writel(GFX_DIV, CM_CLKSEL_GFX);   /* set gfx dividers */
450         __raw_writel(MDM_DIV, CM_CLKSEL_MDM);   /* set mdm dividers */
451
452         div = BUS_DIV;
453         __raw_writel(div, CM_CLKSEL1_CORE);/* set L3/L4/USB/Display/SSi dividers */
454         delay(1000);
455
456         /*13MHz apll src, PRCM 'x' DPLL rate */
457         __raw_writel(DPLL_VAL, CM_CLKSEL1_PLL);
458
459         /*Valid the configuration */
460         __raw_writel(0x00000001, PRCM_CLKCFG_CTRL);
461         delay(1000);
462
463         /* set up APLLS_CLKIN per crystal */
464         if (speed > S19_2M)
465                 speed >>= 1;    /* if fast shift to /2 range */
466         val = (0x2 << 23);      /* default to 13Mhz for 2430c */
467         if (speed == S12M)
468                 val = (0x3 << 23);
469         else if (speed == S19_2M)
470                 val = (0x0 << 23);
471         val |= (~(BIT23|BIT24|BIT25) & __raw_readl(CM_CLKSEL1_PLL));
472         __raw_writel(val, CM_CLKSEL1_PLL);
473
474         __raw_writel(DPLL_LOCK|APLL_LOCK, CM_CLKEN_PLL);   /* enable apll */
475         wait_on_value(BIT8, BIT8, CM_IDLEST_CKGEN, LDELAY);/* wait for apll lock */
476
477         delay(200000);
478 }
479
480 void SEC_generic(void)
481 {
482 /* Permission values for registers -Full fledged permissions to all */
483 #define UNLOCK_1 0xFFFFFFFF
484 #define UNLOCK_2 0x00000000
485 #define UNLOCK_3 0x0000FFFF
486         /* Protection Module Register Target APE (PM_RT)*/
487         __raw_writel(UNLOCK_1, PM_RT_APE_BASE_ADDR_ARM + 0x68); /* REQ_INFO_PERMISSION_1 L*/
488         __raw_writel(UNLOCK_1, PM_RT_APE_BASE_ADDR_ARM + 0x50);  /* READ_PERMISSION_0 L*/
489         __raw_writel(UNLOCK_1, PM_RT_APE_BASE_ADDR_ARM + 0x58);  /* WRITE_PERMISSION_0 L*/
490         __raw_writel(UNLOCK_2, PM_RT_APE_BASE_ADDR_ARM + 0x60); /* ADDR_MATCH_1 L*/
491
492
493         __raw_writel(UNLOCK_3, PM_GPMC_BASE_ADDR_ARM + 0x48); /* REQ_INFO_PERMISSION_0 L*/
494         __raw_writel(UNLOCK_3, PM_GPMC_BASE_ADDR_ARM + 0x50); /* READ_PERMISSION_0 L*/
495         __raw_writel(UNLOCK_3, PM_GPMC_BASE_ADDR_ARM + 0x58); /* WRITE_PERMISSION_0 L*/
496
497         __raw_writel(UNLOCK_3, PM_OCM_RAM_BASE_ADDR_ARM + 0x48); /* REQ_INFO_PERMISSION_0 L*/
498         __raw_writel(UNLOCK_3, PM_OCM_RAM_BASE_ADDR_ARM + 0x50); /* READ_PERMISSION_0 L*/
499         __raw_writel(UNLOCK_3, PM_OCM_RAM_BASE_ADDR_ARM + 0x58); /* WRITE_PERMISSION_0 L*/
500         __raw_writel(UNLOCK_2, PM_OCM_RAM_BASE_ADDR_ARM + 0x80);  /* ADDR_MATCH_2 L*/
501
502         /* IVA Changes */
503         __raw_writel(UNLOCK_3, PM_IVA2_BASE_ADDR_ARM + 0x48); /* REQ_INFO_PERMISSION_0 L*/
504         __raw_writel(UNLOCK_3, PM_IVA2_BASE_ADDR_ARM + 0x50); /* READ_PERMISSION_0 L*/
505         __raw_writel(UNLOCK_3, PM_IVA2_BASE_ADDR_ARM + 0x58); /* WRITE_PERMISSION_0 L*/
506 }
507
508 /**********************************************************
509  * Routine: try_unlock_sram()
510  * Description: If chip is GP type, unlock the SRAM for general use.
511  ***********************************************************/
512 void try_unlock_sram(void)
513 {
514         int mode;
515
516         /* if GP device unlock device SRAM for general use */
517         mode = get_device_type();
518
519         if ((mode == GP_DEVICE) || (mode == HS_DEVICE) || (mode == EMU_DEVICE)
520             || (mode == TST_DEVICE)) {
521                 /* Secure or Emulation device - HS/E/T */
522                 SEC_generic();
523         }
524         return;
525 }
526
527 /**********************************************************
528  * Routine: s_init
529  * Description: Does early system init of muxing and clocks.
530  * - Called at time when only stack is available.
531  **********************************************************/
532
533 int s_init(int skip)
534 {
535         u32   rev;
536
537         rev = get_cpu_rev();
538
539         watchdog_init();
540         try_unlock_sram();   
541         muxSetupAll();
542         delay(100);
543         prcm_init();
544
545 #ifdef CFG_SDRAM_DDR
546         config_sdram_ddr(rev);
547 #elif defined(CFG_SDRAM_COMBO)
548         config_sdram_combo(rev);
549 #elif defined(CFG_2430SDRAM_DDR)
550         config_2430sdram_ddr(rev);
551 #elif defined(CFG_SDRAM_STACKED)
552         config_sdram_stacked(rev);
553 #else
554 #error SDRAM type not supported
555 #endif
556
557         delay(20000);
558         peripheral_enable();
559         return(0);
560 }
561
562 /*******************************************************
563  * Routine: misc_init_r
564  * Description: Init ethernet (done here so udelay works)
565  ********************************************************/
566 int misc_init_r (void)
567 {
568         return(0);
569 }
570
571 /****************************************
572  * Routine: watchdog_init
573  * Description: Shut down watch dogs
574  *****************************************/
575 static void watchdog_init(void)
576 {
577 #define GP (BIT8|BIT9)
578
579         /* There are 4 watch dogs.  1 secure, and 3 general purpose.
580          * I would expect that the ROM takes care of the secure one,
581          * but we will try also.  Of the 3 GP ones, 1 can reset us
582          * directly, the other 2 only generate MPU interrupts.
583          */
584         __raw_writel(WD_UNLOCK1 ,WD2_BASE+WSPR);
585         wait_for_command_complete(WD2_BASE);
586         __raw_writel(WD_UNLOCK2 ,WD2_BASE+WSPR);
587
588 }
589
590 /******************************************************
591  * Routine: wait_for_command_complete
592  * Description: Wait for posting to finish on watchdog
593  ******************************************************/
594 static void wait_for_command_complete(unsigned int wd_base)
595 {
596         int pending = 1;
597         do {
598                 pending = __raw_readl(wd_base+WWPS);
599         } while (pending);
600 }
601
602
603 /**********************************************
604  * Routine: dram_init
605  * Description: sets uboots idea of sdram size
606  **********************************************/
607 int dram_init (void)
608 {
609         return 0;
610 }
611  
612 /*****************************************************************
613  * Routine: peripheral_enable
614  * Description: Enable the clks & power for perifs (GPT2, UART1,...)
615  ******************************************************************/
616 static void peripheral_enable(void)
617 {
618         unsigned int v, if_clks=0, if_clks2 = 0, func_clks=0, func_clks2 = 0;
619
620         /* Enable GP2 timer.*/
621         if_clks |= BIT4;
622         func_clks |= BIT4;
623         v = __raw_readl(CM_CLKSEL2_CORE) | 0x4; /* Sys_clk input OMAP24XX_GPT2 */
624         __raw_writel(v, CM_CLKSEL2_CORE);
625         __raw_writel(0x1, CM_CLKSEL_WKUP);
626
627 #ifdef CFG_NS16550
628         /* Enable UART1 clock */
629         func_clks |= BIT21;
630         if_clks |= BIT21;
631 #endif
632         v = __raw_readl(CM_ICLKEN1_CORE) | if_clks;     /* Interface clocks on */
633         __raw_writel(v,CM_ICLKEN1_CORE );
634         v = __raw_readl(CM_ICLKEN2_CORE) | if_clks2;    /* Interface clocks on */
635         __raw_writel(v, CM_ICLKEN2_CORE);
636         v = __raw_readl(CM_FCLKEN1_CORE) | func_clks; /* Functional Clocks on */
637         __raw_writel(v, CM_FCLKEN1_CORE);
638         v = __raw_readl(CM_FCLKEN2_CORE) | func_clks2;  /* Functional Clocks on */
639         __raw_writel(v, CM_FCLKEN2_CORE);
640         delay(1000);
641
642 }
643
644 /* Do pin muxing for all the devices used in X-Loader */
645 #define MUX_VAL(OFFSET,VALUE)\
646                 __raw_writeb(VALUE, OMAP24XX_CTRL_BASE + OFFSET);
647 static void muxSetupAll(void)
648 {
649         /* UART 1*/
650         MUX_VAL(0x00B1, 0x1B)        /* uart1_cts- EN, HI, 3, ->gpio_32 */
651         MUX_VAL(0x00B2, 0x1B)        /* uart1_rts- EN, HI, 3, ->gpio_8 */
652         MUX_VAL(0x00B3, 0x1B)        /* uart1_tx- EN, HI, 3, ->gpio_9 */
653         MUX_VAL(0x00B4, 0x1B)        /* uart1_rx- EN, HI, 3, ->gpio_10 */
654         MUX_VAL(0x0107, 0x01)        /* ssi1_dat_tx- Dis, 1, ->uart1_tx */
655         MUX_VAL(0x0108, 0x01)        /* ssi1_flag_tx- Dis, 1, ->uart1_rts */
656         MUX_VAL(0x0109, 0x01)        /* ssi1_rdy_tx- Dis, 1, ->uart1_cts */
657         MUX_VAL(0x010A, 0x01)        /* ssi1_dat_rx- Dis, 1, ->uart1_rx */
658
659         /* Mux settings for SDRC */
660         MUX_VAL(0x0054, 0x1B)        /* sdrc_a14 - EN, HI, 3, ->gpio_0 */
661         MUX_VAL(0x0055, 0x1B)        /* sdrc_a13 - EN, HI, 3, ->gpio_1 */
662         MUX_VAL(0x0056, 0x00)        /* sdrc_a12 - Dis, 0 */
663         MUX_VAL(0x0046, 0x00)        /* sdrc_ncs1 - Dis, 0 */
664         MUX_VAL(0x0048, 0x00)        /* sdrc_cke1 - Dis, 0 */
665         /* GPMC */
666         MUX_VAL(0x0030, 0x00)        /* gpmc_clk - Dis, 0 */
667         MUX_VAL(0x0032, 0x00)        /* gpmc_ncs1- Dis, 0 */
668         MUX_VAL(0x0033, 0x00)        /* gpmc_ncs2- Dis, 0 */
669         MUX_VAL(0x0034, 0x03)        /* gpmc_ncs3- Dis, 3, ->gpio_24 */
670         MUX_VAL(0x0035, 0x03)        /* gpmc_ncs4- Dis, 3, ->gpio_25 */
671         MUX_VAL(0x0036, 0x00)        /* gpmc_ncs5- Dis, 0 */
672         MUX_VAL(0x0037, 0x03)        /* gpmc_ncs6- Dis, 3, ->gpio_27 */
673         MUX_VAL(0x0038, 0x00)        /* gpmc_ncs7- Dis, 0 */
674         MUX_VAL(0x0040, 0x18)        /* gpmc_wait1- Dis, 0 */
675         MUX_VAL(0x0041, 0x18)        /* gpmc_wait2- Dis, 0 */
676         MUX_VAL(0x0042, 0x1B)        /* gpmc_wait3- EN, HI, 3, ->gpio_35 */
677         MUX_VAL(0x0085, 0x1B)        /* gpmc_a10- EN, HI, 3, ->gpio_3 */
678 }
679
680 int nand_init(void)
681 {
682         u32     rev;
683
684         /* GPMC Configuration */
685         rev  = get_cpu_rev();
686
687         /* global settings */
688         __raw_writel(0x10, GPMC_SYSCONFIG);     /* smart idle */
689         __raw_writel(0x0, GPMC_IRQENABLE);      /* isr's sources masked */
690         __raw_writel(0, GPMC_TIMEOUT_CONTROL);/* timeout disable */
691 #ifdef CFG_NAND
692         __raw_writel(0x001, GPMC_CONFIG);       /* set nWP, disable limited addr */
693 #endif
694
695         /* Set the GPMC Vals . For NAND boot on 2430SDP, NAND is mapped at CS0
696          *  , NOR at CS1 and MPDB at CS5. And oneNAND boot, we map oneNAND at CS0.
697          *  We configure only GPMC CS0 with required values. Configiring other devices 
698          *  at other CS in done in u-boot anyway. So we don't have to bother doing it here.
699          */
700         __raw_writel(0, GPMC_CONFIG7_0);
701         sdelay(1000);
702
703 #ifdef CFG_NAND
704         __raw_writel( SMNAND_GPMC_CONFIG1, GPMC_CONFIG1_0);
705         __raw_writel( SMNAND_GPMC_CONFIG2, GPMC_CONFIG2_0);
706         __raw_writel( SMNAND_GPMC_CONFIG3, GPMC_CONFIG3_0);
707         __raw_writel( SMNAND_GPMC_CONFIG4, GPMC_CONFIG4_0);
708         __raw_writel( SMNAND_GPMC_CONFIG5, GPMC_CONFIG5_0);
709         __raw_writel( SMNAND_GPMC_CONFIG6, GPMC_CONFIG6_0);
710
711 #else /* CFG_ONENAND */
712         __raw_writel( ONENAND_GPMC_CONFIG1, GPMC_CONFIG1_0);
713         __raw_writel( ONENAND_GPMC_CONFIG2, GPMC_CONFIG2_0);
714         __raw_writel( ONENAND_GPMC_CONFIG3, GPMC_CONFIG3_0);
715         __raw_writel( ONENAND_GPMC_CONFIG4, GPMC_CONFIG4_0);
716         __raw_writel( ONENAND_GPMC_CONFIG5, GPMC_CONFIG5_0);
717         __raw_writel( ONENAND_GPMC_CONFIG6, GPMC_CONFIG6_0);
718 #endif
719
720         /* Enable the GPMC Mapping */
721         __raw_writel(( ((OMAP24XX_GPMC_CS0_SIZE & 0xF)<<8) |
722                      ((OMAP24XX_GPMC_CS0_MAP>>24) & 0x3F) |
723                      (1<<6) ), GPMC_CONFIG7_0);
724         sdelay(2000);
725 #ifdef CFG_NAND
726         if (nand_chip()){
727 #ifdef CFG_PRINTF
728                 printf("Unsupported Chip!\n");
729 #endif
730                 return 1;
731         }
732 #else
733         if (onenand_chip()){
734 #ifdef CFG_PRINTF
735                 printf("OneNAND Unsupported !\n");
736 #endif
737                 return 1;
738         }
739
740 #endif
741         return 0;
742 }
743
744 /* optionally do something like blinking LED */
745 void board_hang (void)
746 { while (0) {};}