pandora.c: Set GPBR1 register so that charging starts early.
[pandora-x-loader.git] / board / pandora / pandora.c
1 /*
2  * (C) Copyright 2006
3  * Texas Instruments, <www.ti.com>
4  * Jian Zhang <jzhang@ti.com>
5  * Richard Woodruff <r-woodruff2@ti.com>
6  *
7  * Modified for the Pandora
8  * John Willis <source@distant-earth.com>
9  *
10  * See file CREDITS for list of people who contributed to this
11  * project.
12  *
13  * This program is free software; you can redistribute it and/or
14  * modify it under the terms of the GNU General Public License as
15  * published by the Free Software Foundation; either version 2 of
16  * the License, or (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful,
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21  * GNU General Public License for more details.
22  *
23  * You should have received a copy of the GNU General Public License
24  * along with this program; if not, write to the Free Software
25  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
26  * MA 02111-1307 USA
27  */
28
29 #include <common.h>
30 #include <command.h>
31 #include <part.h>
32 #include <fat.h>
33 #include <asm/arch/cpu.h>
34 #include <asm/arch/bits.h>
35 #include <asm/arch/mux.h>
36 #include <asm/arch/sys_proto.h>
37 #include <asm/arch/sys_info.h>
38 #include <asm/arch/clocks.h>
39 #include <asm/arch/mem.h>
40
41 /* Used to index into DPLL parameter tables */
42 struct dpll_param {
43         unsigned int m;
44         unsigned int n;
45         unsigned int fsel;
46         unsigned int m2;
47 };
48
49 typedef struct dpll_param dpll_param;
50
51 /* Following functions are exported from lowlevel_init.S */
52 extern dpll_param *get_mpu_dpll_param();
53 extern dpll_param *get_iva_dpll_param();
54 extern dpll_param *get_core_dpll_param();
55 extern dpll_param *get_per_dpll_param();
56
57 #define __raw_readl(a)          (*(volatile unsigned int *)(a))
58 #define __raw_writel(v, a)      (*(volatile unsigned int *)(a) = (v))
59 #define __raw_readw(a)          (*(volatile unsigned short *)(a))
60 #define __raw_writew(v, a)      (*(volatile unsigned short *)(a) = (v))
61
62 /*******************************************************
63  * Routine: delay
64  * Description: spinning delay to use before udelay works
65  ******************************************************/
66 static inline void delay(unsigned long loops)
67 {
68         __asm__ volatile ("1:\n" "subs %0, %1, #1\n"
69                           "bne 1b":"=r" (loops):"0"(loops));
70 }
71
72 void udelay (unsigned long usecs) {
73         delay(usecs);
74 }
75
76 /*****************************************
77  * Routine: board_init
78  * Description: Early hardware init.
79  *****************************************/
80 int board_init(void)
81 {
82 #if defined(CFG_PRINTF) && defined(CONFIG_DRIVER_OMAP34XX_I2C)
83         unsigned char byte;
84
85         /* do additional init to prevent trash being output on startup */
86         serial_init();
87
88         #ifdef CONFIG_PANDORA_FINAL
89                 /* restore GPBR1 for charging to work -
90                  * boot ROM or something clears it before us */
91                 byte = 0x90;
92                 i2c_write(0x49, 0x91, 1, &byte, 1);
93
94                 /*VAUX3 = 2.8V    (AV connector)*/
95                 byte = 0x20;
96                 i2c_write(0x4B, 0x7A, 1, &byte, 1);
97                 byte = 0x03;
98                 i2c_write(0x4B, 0x7D, 1, &byte, 1);
99         #else
100                 /*VAUX4 = 2.8V    (TOUCH,NUBS,RS-232 TRANSCEIVER)*/
101                 byte = 0x20;
102                 i2c_write(0x4B, 0x7E, 1, &byte, 1);
103                 byte = 0x0A;
104                 i2c_write(0x4B, 0x81, 1, &byte, 1);
105         #endif
106
107 #endif
108
109         return 0;
110 }
111
112 /*************************************************************
113  *  get_device_type(): tell if GP/HS/EMU/TST
114  *************************************************************/
115 u32 get_device_type(void)
116 {
117         int mode;
118         mode = __raw_readl(CONTROL_STATUS) & (DEVICE_MASK);
119         return (mode >>= 8);
120 }
121
122 /************************************************
123  * get_sysboot_value(void) - return SYS_BOOT[4:0]
124  ************************************************/
125 u32 get_sysboot_value(void)
126 {
127         int mode;
128         mode = __raw_readl(CONTROL_STATUS) & (SYSBOOT_MASK);
129         return mode;
130 }
131
132 /*************************************************************
133  * Routine: get_mem_type(void) - returns the kind of memory connected
134  * to GPMC that we are trying to boot form. Uses SYS BOOT settings.
135  *************************************************************/
136 u32 get_mem_type(void)
137 {
138         u32   mem_type = get_sysboot_value();
139         switch (mem_type) {
140         case 0:
141         case 2:
142         case 4:
143         case 16:
144         case 22:
145                 return GPMC_ONENAND;
146
147         case 1:
148         case 12:
149         case 15:
150         case 21:
151         case 27:
152                 return GPMC_NAND;
153
154         case 3:
155         case 6:
156                 return MMC_ONENAND;
157
158         case 8:
159         case 11:
160         case 14:
161         case 20:
162         case 26:
163                 return GPMC_MDOC;
164
165         case 17:
166         case 18:
167         case 24:
168                 return MMC_NAND;
169
170         case 7:
171         case 10:
172         case 13:
173         case 19:
174         case 25:
175         default:
176                 return GPMC_NOR;
177         }
178 }
179
180 /******************************************
181  * get_cpu_rev(void) - extract version info
182  ******************************************/
183 u32 get_cpu_rev(void)
184 {
185         u32 cpuid = 0;
186         /* On ES1.0 the IDCODE register is not exposed on L4
187          * so using CPU ID to differentiate
188          * between ES2.0 and ES1.0.
189          */
190         __asm__ __volatile__("mrc p15, 0, %0, c0, c0, 0":"=r"(cpuid));
191         if ((cpuid & 0xf) == 0x0)
192                 return CPU_3430_ES1;
193         else
194                 return CPU_3430_ES2;
195
196 }
197
198 /******************************************
199  * cpu_is_3410(void) - returns true for 3410
200  ******************************************/
201 u32 cpu_is_3410(void)
202 {
203         int status;
204         if (get_cpu_rev() < CPU_3430_ES2) {
205                 return 0;
206         } else {
207                 /* read scalability status and return 1 for 3410*/
208                 status = __raw_readl(CONTROL_SCALABLE_OMAP_STATUS);
209                 /* Check whether MPU frequency is set to 266 MHz which
210                  * is nominal for 3410. If yes return true else false
211                  */
212                 if (((status >> 8) & 0x3) == 0x2)
213                         return 1;
214                 else
215                         return 0;
216         }
217 }
218
219 /*****************************************************************
220  * sr32 - clear & set a value in a bit range for a 32 bit address
221  *****************************************************************/
222 void sr32(u32 addr, u32 start_bit, u32 num_bits, u32 value)
223 {
224         u32 tmp, msk = 0;
225         msk = 1 << num_bits;
226         --msk;
227         tmp = __raw_readl(addr) & ~(msk << start_bit);
228         tmp |= value << start_bit;
229         __raw_writel(tmp, addr);
230 }
231
232 /*********************************************************************
233  * wait_on_value() - common routine to allow waiting for changes in
234  *   volatile regs.
235  *********************************************************************/
236 u32 wait_on_value(u32 read_bit_mask, u32 match_value, u32 read_addr, u32 bound)
237 {
238         u32 i = 0, val;
239         do {
240                 ++i;
241                 val = __raw_readl(read_addr) & read_bit_mask;
242                 if (val == match_value)
243                         return (1);
244                 if (i == bound)
245                         return (0);
246         } while (1);
247 }
248
249 #ifdef CFG_3430SDRAM_DDR
250 /*********************************************************************
251  * config_3430sdram_ddr() - Init DDR on 3430SDP dev board.
252  *********************************************************************/
253 void config_3430sdram_ddr(void)
254 {
255         /* reset sdrc controller */
256         __raw_writel(SOFTRESET, SDRC_SYSCONFIG);
257         wait_on_value(BIT0, BIT0, SDRC_STATUS, 12000000);
258         __raw_writel(0, SDRC_SYSCONFIG);
259
260         /* setup sdrc to ball mux */
261         __raw_writel(SDP_SDRC_SHARING, SDRC_SHARING);
262
263         /* SDRC put in weak */
264 //        (*(unsigned int*)0x6D00008C) = 0x00000020;
265
266         /* SDRC_MCFG0 register */
267         (*(unsigned int*)0x6D000080) = 0x02584099;//from Micron
268
269         /* SDRC_ACTIM_CTRLA0 register */
270 //our value        (*(unsigned int*)0x6D00009c) = 0xa29db4c6;// for 166M
271         (*(unsigned int*)0x6D00009c) = 0xaa9db4c6;// for 166M from rkw
272
273         /* SDRC_ACTIM_CTRLB0 register */
274 //from micron   (*(unsigned int*)0x6D0000a0) = 0x12214;// for 166M
275
276 //        (*(unsigned int*)0x6D0000a0) = 0x00011417; our value
277         (*(unsigned int*)0x6D0000a0) = 0x00011517;
278
279         /* SDRC_RFR_CTRL0 register */
280 //from micron   (*(unsigned int*)0x6D0000a4) =0x54601; // for 166M
281
282         (*(unsigned int*)0x6D0000a4) =0x0004DC01;
283
284         /* Disble Power Down of CKE cuz of 1 CKE on combo part */
285         (*(unsigned int*)0x6D000070) = 0x00000081;
286
287         /* SDRC_Manual command register */
288         (*(unsigned int*)0x6D0000a8) = 0x00000000; // NOP command
289         delay(5000);
290         (*(unsigned int*)0x6D0000a8) = 0x00000001; // Precharge command
291         (*(unsigned int*)0x6D0000a8) = 0x00000002; // Auto-refresh command
292         (*(unsigned int*)0x6D0000a8) = 0x00000002; // Auto-refresh command
293
294         /* SDRC MR0 register */
295         (*(int*)0x6D000084) = 0x00000032; // Burst length =4
296         // CAS latency = 3
297         // Write Burst = Read Burst
298         // Serial Mode
299
300         /* SDRC DLLA control register */
301         (*(unsigned int*)0x6D000060) = 0x0000A;
302         delay(0x20000); // some delay
303
304 }
305 #endif /* CFG_3430SDRAM_DDR */
306
307 /*************************************************************
308  * get_sys_clk_speed - determine reference oscillator speed
309  *  based on known 32kHz clock and gptimer.
310  *************************************************************/
311 u32 get_osc_clk_speed(void)
312 {
313         u32 start, cstart, cend, cdiff, val;
314
315         val = __raw_readl(PRM_CLKSRC_CTRL);
316         /* If SYS_CLK is being divided by 2, remove for now */
317         val = (val & (~BIT7)) | BIT6;
318         __raw_writel(val, PRM_CLKSRC_CTRL);
319
320         /* enable timer2 */
321         val = __raw_readl(CM_CLKSEL_WKUP) | BIT0;
322         __raw_writel(val, CM_CLKSEL_WKUP);      /* select sys_clk for GPT1 */
323
324         /* Enable I and F Clocks for GPT1 */
325         val = __raw_readl(CM_ICLKEN_WKUP) | BIT0 | BIT2;
326         __raw_writel(val, CM_ICLKEN_WKUP);
327         val = __raw_readl(CM_FCLKEN_WKUP) | BIT0;
328         __raw_writel(val, CM_FCLKEN_WKUP);
329
330         __raw_writel(0, OMAP34XX_GPT1 + TLDR);  /* start counting at 0 */
331         __raw_writel(GPT_EN, OMAP34XX_GPT1 + TCLR);     /* enable clock */
332         /* enable 32kHz source */
333         /* enabled out of reset */
334         /* determine sys_clk via gauging */
335
336         start = 20 + __raw_readl(S32K_CR);      /* start time in 20 cycles */
337         while (__raw_readl(S32K_CR) < start) ;  /* dead loop till start time */
338         cstart = __raw_readl(OMAP34XX_GPT1 + TCRR);     /* get start sys_clk count */
339         while (__raw_readl(S32K_CR) < (start + 20)) ;   /* wait for 40 cycles */
340         cend = __raw_readl(OMAP34XX_GPT1 + TCRR);       /* get end sys_clk count */
341         cdiff = cend - cstart;  /* get elapsed ticks */
342
343         /* based on number of ticks assign speed */
344         if (cdiff > 19000)
345                 return (S38_4M);
346         else if (cdiff > 15200)
347                 return (S26M);
348         else if (cdiff > 13000)
349                 return (S24M);
350         else if (cdiff > 9000)
351                 return (S19_2M);
352         else if (cdiff > 7600)
353                 return (S13M);
354         else
355                 return (S12M);
356 }
357
358 /******************************************************************************
359  * get_sys_clkin_sel() - returns the sys_clkin_sel field value based on
360  *   -- input oscillator clock frequency.
361  *
362  *****************************************************************************/
363 void get_sys_clkin_sel(u32 osc_clk, u32 * sys_clkin_sel)
364 {
365         if (osc_clk == S38_4M)
366                 *sys_clkin_sel = 4;
367         else if (osc_clk == S26M)
368                 *sys_clkin_sel = 3;
369         else if (osc_clk == S19_2M)
370                 *sys_clkin_sel = 2;
371         else if (osc_clk == S13M)
372                 *sys_clkin_sel = 1;
373         else if (osc_clk == S12M)
374                 *sys_clkin_sel = 0;
375 }
376
377 /******************************************************************************
378  * prcm_init() - inits clocks for PRCM as defined in clocks.h
379  *   -- called from SRAM, or Flash (using temp SRAM stack).
380  *****************************************************************************/
381 void prcm_init(void)
382 {
383         u32 osc_clk = 0, sys_clkin_sel;
384         dpll_param *dpll_param_p;
385         u32 clk_index, sil_index;
386
387         /* Gauge the input clock speed and find out the sys_clkin_sel
388          * value corresponding to the input clock.
389          */
390         osc_clk = get_osc_clk_speed();
391         get_sys_clkin_sel(osc_clk, &sys_clkin_sel);
392
393         sr32(PRM_CLKSEL, 0, 3, sys_clkin_sel);  /* set input crystal speed */
394
395         /* If the input clock is greater than 19.2M always divide/2 */
396         if (sys_clkin_sel > 2) {
397                 sr32(PRM_CLKSRC_CTRL, 6, 2, 2); /* input clock divider */
398                 clk_index = sys_clkin_sel / 2;
399         } else {
400                 sr32(PRM_CLKSRC_CTRL, 6, 2, 1); /* input clock divider */
401                 clk_index = sys_clkin_sel;
402         }
403
404         sr32(PRM_CLKSRC_CTRL, 0, 2, 0);/* Bypass mode: T2 inputs a square clock */
405
406         /* The DPLL tables are defined according to sysclk value and
407          * silicon revision. The clk_index value will be used to get
408          * the values for that input sysclk from the DPLL param table
409          * and sil_index will get the values for that SysClk for the
410          * appropriate silicon rev.
411          */
412         sil_index = get_cpu_rev() - 1;
413
414         /* Unlock MPU DPLL (slows things down, and needed later) */
415         sr32(CM_CLKEN_PLL_MPU, 0, 3, PLL_LOW_POWER_BYPASS);
416         wait_on_value(BIT0, 0, CM_IDLEST_PLL_MPU, LDELAY);
417
418         /* Getting the base address of Core DPLL param table */
419         dpll_param_p = (dpll_param *) get_core_dpll_param();
420         /* Moving it to the right sysclk and ES rev base */
421         dpll_param_p = dpll_param_p + 2*clk_index + sil_index;
422         /* CORE DPLL */
423         /* sr32(CM_CLKSEL2_EMU) set override to work when asleep */
424         sr32(CM_CLKEN_PLL, 0, 3, PLL_FAST_RELOCK_BYPASS);
425         wait_on_value(BIT0, 0, CM_IDLEST_CKGEN, LDELAY);
426         sr32(CM_CLKSEL1_EMU, 16, 5, CORE_M3X2); /* m3x2 */
427         sr32(CM_CLKSEL1_PLL, 27, 2, dpll_param_p->m2);  /* Set M2 */
428         sr32(CM_CLKSEL1_PLL, 16, 11, dpll_param_p->m);  /* Set M */
429         sr32(CM_CLKSEL1_PLL, 8, 7, dpll_param_p->n);    /* Set N */
430         sr32(CM_CLKSEL1_PLL, 6, 1, 0);  /* 96M Src */
431         sr32(CM_CLKSEL_CORE, 8, 4, CORE_SSI_DIV);       /* ssi */
432         sr32(CM_CLKSEL_CORE, 4, 2, CORE_FUSB_DIV);      /* fsusb */
433         sr32(CM_CLKSEL_CORE, 2, 2, CORE_L4_DIV);        /* l4 */
434         sr32(CM_CLKSEL_CORE, 0, 2, CORE_L3_DIV);        /* l3 */
435         sr32(CM_CLKSEL_GFX, 0, 3, GFX_DIV);     /* gfx */
436         sr32(CM_CLKSEL_WKUP, 1, 2, WKUP_RSM);   /* reset mgr */
437         sr32(CM_CLKEN_PLL, 4, 4, dpll_param_p->fsel);   /* FREQSEL */
438         sr32(CM_CLKEN_PLL, 0, 3, PLL_LOCK);     /* lock mode */
439         wait_on_value(BIT0, 1, CM_IDLEST_CKGEN, LDELAY);
440
441         /* Getting the base address to PER  DPLL param table */
442         dpll_param_p = (dpll_param *) get_per_dpll_param();
443         /* Moving it to the right sysclk base */
444         dpll_param_p = dpll_param_p + clk_index;
445         /* PER DPLL */
446         sr32(CM_CLKEN_PLL, 16, 3, PLL_STOP);
447         wait_on_value(BIT1, 0, CM_IDLEST_CKGEN, LDELAY);
448         sr32(CM_CLKSEL1_EMU, 24, 5, PER_M6X2);  /* set M6 */
449         sr32(CM_CLKSEL_CAM, 0, 5, PER_M5X2);    /* set M5 */
450         sr32(CM_CLKSEL_DSS, 0, 5, PER_M4X2);    /* set M4 */
451         sr32(CM_CLKSEL_DSS, 8, 5, PER_M3X2);    /* set M3 */
452         sr32(CM_CLKSEL3_PLL, 0, 5, dpll_param_p->m2);   /* set M2 */
453         sr32(CM_CLKSEL2_PLL, 8, 11, dpll_param_p->m);   /* set m */
454         sr32(CM_CLKSEL2_PLL, 0, 7, dpll_param_p->n);    /* set n */
455         sr32(CM_CLKEN_PLL, 20, 4, dpll_param_p->fsel);  /* FREQSEL */
456         sr32(CM_CLKEN_PLL, 16, 3, PLL_LOCK);    /* lock mode */
457         wait_on_value(BIT1, 2, CM_IDLEST_CKGEN, LDELAY);
458
459         /* Getting the base address to MPU DPLL param table */
460         dpll_param_p = (dpll_param *) get_mpu_dpll_param();
461         /* Moving it to the right sysclk and ES rev base */
462         dpll_param_p = dpll_param_p + 2*clk_index + sil_index;
463         /* MPU DPLL (unlocked already) */
464         sr32(CM_CLKSEL2_PLL_MPU, 0, 5, dpll_param_p->m2);       /* Set M2 */
465         sr32(CM_CLKSEL1_PLL_MPU, 8, 11, dpll_param_p->m);       /* Set M */
466         sr32(CM_CLKSEL1_PLL_MPU, 0, 7, dpll_param_p->n);        /* Set N */
467         sr32(CM_CLKEN_PLL_MPU, 4, 4, dpll_param_p->fsel);       /* FREQSEL */
468         sr32(CM_CLKEN_PLL_MPU, 0, 3, PLL_LOCK); /* lock mode */
469         wait_on_value(BIT0, 1, CM_IDLEST_PLL_MPU, LDELAY);
470
471         /* Getting the base address to IVA DPLL param table */
472         dpll_param_p = (dpll_param *) get_iva_dpll_param();
473         /* Moving it to the right sysclk and ES rev base */
474         dpll_param_p = dpll_param_p + 2*clk_index + sil_index;
475         /* IVA DPLL (set to 12*20=240MHz) */
476         sr32(CM_CLKEN_PLL_IVA2, 0, 3, PLL_STOP);
477         wait_on_value(BIT0, 0, CM_IDLEST_PLL_IVA2, LDELAY);
478         sr32(CM_CLKSEL2_PLL_IVA2, 0, 5, dpll_param_p->m2);      /* set M2 */
479         sr32(CM_CLKSEL1_PLL_IVA2, 8, 11, dpll_param_p->m);      /* set M */
480         sr32(CM_CLKSEL1_PLL_IVA2, 0, 7, dpll_param_p->n);       /* set N */
481         sr32(CM_CLKEN_PLL_IVA2, 4, 4, dpll_param_p->fsel);      /* FREQSEL */
482         sr32(CM_CLKEN_PLL_IVA2, 0, 3, PLL_LOCK);        /* lock mode */
483         wait_on_value(BIT0, 1, CM_IDLEST_PLL_IVA2, LDELAY);
484
485         /* Set up GPTimers to sys_clk source only */
486         sr32(CM_CLKSEL_PER, 0, 8, 0xff);
487         sr32(CM_CLKSEL_WKUP, 0, 1, 1);
488
489         delay(5000);
490 }
491
492 /*****************************************
493  * Routine: secure_unlock
494  * Description: Setup security registers for access
495  * (GP Device only)
496  *****************************************/
497 void secure_unlock(void)
498 {
499         /* Permission values for registers -Full fledged permissions to all */
500 #define UNLOCK_1 0xFFFFFFFF
501 #define UNLOCK_2 0x00000000
502 #define UNLOCK_3 0x0000FFFF
503         /* Protection Module Register Target APE (PM_RT) */
504         __raw_writel(UNLOCK_1, RT_REQ_INFO_PERMISSION_1);
505         __raw_writel(UNLOCK_1, RT_READ_PERMISSION_0);
506         __raw_writel(UNLOCK_1, RT_WRITE_PERMISSION_0);
507         __raw_writel(UNLOCK_2, RT_ADDR_MATCH_1);
508
509         __raw_writel(UNLOCK_3, GPMC_REQ_INFO_PERMISSION_0);
510         __raw_writel(UNLOCK_3, GPMC_READ_PERMISSION_0);
511         __raw_writel(UNLOCK_3, GPMC_WRITE_PERMISSION_0);
512
513         __raw_writel(UNLOCK_3, OCM_REQ_INFO_PERMISSION_0);
514         __raw_writel(UNLOCK_3, OCM_READ_PERMISSION_0);
515         __raw_writel(UNLOCK_3, OCM_WRITE_PERMISSION_0);
516         __raw_writel(UNLOCK_2, OCM_ADDR_MATCH_2);
517
518         /* IVA Changes */
519         __raw_writel(UNLOCK_3, IVA2_REQ_INFO_PERMISSION_0);
520         __raw_writel(UNLOCK_3, IVA2_READ_PERMISSION_0);
521         __raw_writel(UNLOCK_3, IVA2_WRITE_PERMISSION_0);
522
523         __raw_writel(UNLOCK_1, SMS_RG_ATT0);    /* SDRC region 0 public */
524 }
525
526 /**********************************************************
527  * Routine: try_unlock_sram()
528  * Description: If chip is GP type, unlock the SRAM for
529  *  general use.
530  ***********************************************************/
531 void try_unlock_memory(void)
532 {
533         int mode;
534
535         /* if GP device unlock device SRAM for general use */
536         /* secure code breaks for Secure/Emulation device - HS/E/T */
537         mode = get_device_type();
538         if (mode == GP_DEVICE) {
539                 secure_unlock();
540         }
541         return;
542 }
543
544 /**********************************************************
545  * Routine: s_init
546  * Description: Does early system init of muxing and clocks.
547  * - Called at time when only stack is available.
548  **********************************************************/
549
550 void s_init(void)
551 {
552         watchdog_init();
553 #ifdef CONFIG_3430_AS_3410
554         /* setup the scalability control register for
555          * 3430 to work in 3410 mode
556          */
557         __raw_writel(0x5ABF, CONTROL_SCALABLE_OMAP_OCP);
558 #endif
559         try_unlock_memory();
560         set_muxconf_regs();
561         delay(100);
562         prcm_init();
563         per_clocks_enable();
564         config_3430sdram_ddr();
565 }
566
567 /*******************************************************
568  * Routine: misc_init_r
569  * Description: Init
570  ********************************************************/
571 int misc_init_r(void)
572 {
573 #ifdef CFG_PRINTF
574         printf("OpenPandora System \n");
575 #endif
576
577         return (0);
578 }
579
580 /******************************************************
581  * Routine: wait_for_command_complete
582  * Description: Wait for posting to finish on watchdog
583  ******************************************************/
584 void wait_for_command_complete(unsigned int wd_base)
585 {
586         int pending = 1;
587         do {
588                 pending = __raw_readl(wd_base + WWPS);
589         } while (pending);
590 }
591
592 /****************************************
593  * Routine: watchdog_init
594  * Description: Shut down watch dogs
595  *****************************************/
596 void watchdog_init(void)
597 {
598         /* There are 3 watch dogs WD1=Secure, WD2=MPU, WD3=IVA. WD1 is
599          * either taken care of by ROM (HS/EMU) or not accessible (GP).
600          * We need to take care of WD2-MPU or take a PRCM reset.  WD3
601          * should not be running and does not generate a PRCM reset.
602          */
603         sr32(CM_FCLKEN_WKUP, 5, 1, 1);
604         sr32(CM_ICLKEN_WKUP, 5, 1, 1);
605         wait_on_value(BIT5, 0x20, CM_IDLEST_WKUP, 5);   /* some issue here */
606
607         __raw_writel(WD_UNLOCK1, WD2_BASE + WSPR);
608         wait_for_command_complete(WD2_BASE);
609         __raw_writel(WD_UNLOCK2, WD2_BASE + WSPR);
610 }
611
612 /**********************************************
613  * Routine: dram_init
614  * Description: sets uboots idea of sdram size
615  **********************************************/
616 int dram_init(void)
617 {
618         return 0;
619 }
620
621 /*****************************************************************
622  * Routine: peripheral_enable
623  * Description: Enable the clks & power for perifs (GPT2, UART1,...)
624  ******************************************************************/
625 void per_clocks_enable(void)
626 {
627         /* Enable GP2 timer. */
628         sr32(CM_CLKSEL_PER, 0, 1, 0x1); /* GPT2 = sys clk */
629         sr32(CM_ICLKEN_PER, 3, 1, 0x1); /* ICKen GPT2 */
630         sr32(CM_FCLKEN_PER, 3, 1, 0x1); /* FCKen GPT2 */
631
632 #ifdef CFG_NS16550
633         /* Enable UART1 clocks */
634         sr32(CM_FCLKEN1_CORE, 13, 1, 0x1);
635         sr32(CM_ICLKEN1_CORE, 13, 1, 0x1);
636
637         /* UART 3 Clocks */
638         sr32(CM_FCLKEN_PER, 11, 1, 0x1);
639         sr32(CM_ICLKEN_PER, 11, 1, 0x1);
640
641 #endif
642
643 #ifdef CONFIG_DRIVER_OMAP34XX_I2C
644         /* Turn on all 3 I2C clocks */
645         sr32(CM_FCLKEN1_CORE, 15, 3, 0x7);
646         sr32(CM_ICLKEN1_CORE, 15, 3, 0x7);      /* I2C1,2,3 = on */
647 #endif
648
649         /* Enable the ICLK for 32K Sync Timer as its used in udelay */
650         sr32(CM_ICLKEN_WKUP, 2, 1, 0x1);
651
652         sr32(CM_FCLKEN_IVA2, 0, 32, FCK_IVA2_ON);
653         sr32(CM_FCLKEN1_CORE, 0, 32, FCK_CORE1_ON);
654         sr32(CM_ICLKEN1_CORE, 0, 32, ICK_CORE1_ON);
655         sr32(CM_ICLKEN2_CORE, 0, 32, ICK_CORE2_ON);
656         sr32(CM_FCLKEN_WKUP, 0, 32, FCK_WKUP_ON);
657         sr32(CM_ICLKEN_WKUP, 0, 32, ICK_WKUP_ON);
658         sr32(CM_FCLKEN_DSS, 0, 32, FCK_DSS_ON);
659         sr32(CM_ICLKEN_DSS, 0, 32, ICK_DSS_ON);
660         sr32(CM_FCLKEN_CAM, 0, 32, FCK_CAM_ON);
661         sr32(CM_ICLKEN_CAM, 0, 32, ICK_CAM_ON);
662         sr32(CM_FCLKEN_PER, 0, 32, FCK_PER_ON);
663         sr32(CM_ICLKEN_PER, 0, 32, ICK_PER_ON);
664
665         /* Enable GPIO5 clocks for blinky LEDs */
666         sr32(CM_FCLKEN_PER, 16, 1, 0x1);        /* FCKen GPIO5 */
667         sr32(CM_ICLKEN_PER, 16, 1, 0x1);        /* ICKen GPIO5 */
668
669         delay(1000);
670 }
671
672 /* Set MUX for UART, GPMC, SDRC, GPIO */
673
674 #define         MUX_VAL(OFFSET,VALUE)\
675                 __raw_writew((VALUE), OMAP34XX_CTRL_BASE + (OFFSET));
676
677 #define         CP(x)   (CONTROL_PADCONF_##x)
678 /*
679  * IEN  - Input Enable
680  * IDIS - Input Disable
681  * PTD  - Pull type Down
682  * PTU  - Pull type Up
683  * DIS  - Pull type selection is inactive
684  * EN   - Pull type selection is active
685  * M0   - Mode 0
686  * The commented string gives the final mux configuration for that pin
687  */
688 #define MUX_DEFAULT()\
689         /*SDRC*/\
690         MUX_VAL(CP(SDRC_D0),        (IEN  | PTD | DIS | M0)) /*SDRC_D0*/\
691         MUX_VAL(CP(SDRC_D1),        (IEN  | PTD | DIS | M0)) /*SDRC_D1*/\
692         MUX_VAL(CP(SDRC_D2),        (IEN  | PTD | DIS | M0)) /*SDRC_D2*/\
693         MUX_VAL(CP(SDRC_D3),        (IEN  | PTD | DIS | M0)) /*SDRC_D3*/\
694         MUX_VAL(CP(SDRC_D4),        (IEN  | PTD | DIS | M0)) /*SDRC_D4*/\
695         MUX_VAL(CP(SDRC_D5),        (IEN  | PTD | DIS | M0)) /*SDRC_D5*/\
696         MUX_VAL(CP(SDRC_D6),        (IEN  | PTD | DIS | M0)) /*SDRC_D6*/\
697         MUX_VAL(CP(SDRC_D7),        (IEN  | PTD | DIS | M0)) /*SDRC_D7*/\
698         MUX_VAL(CP(SDRC_D8),        (IEN  | PTD | DIS | M0)) /*SDRC_D8*/\
699         MUX_VAL(CP(SDRC_D9),        (IEN  | PTD | DIS | M0)) /*SDRC_D9*/\
700         MUX_VAL(CP(SDRC_D10),       (IEN  | PTD | DIS | M0)) /*SDRC_D10*/\
701         MUX_VAL(CP(SDRC_D11),       (IEN  | PTD | DIS | M0)) /*SDRC_D11*/\
702         MUX_VAL(CP(SDRC_D12),       (IEN  | PTD | DIS | M0)) /*SDRC_D12*/\
703         MUX_VAL(CP(SDRC_D13),       (IEN  | PTD | DIS | M0)) /*SDRC_D13*/\
704         MUX_VAL(CP(SDRC_D14),       (IEN  | PTD | DIS | M0)) /*SDRC_D14*/\
705         MUX_VAL(CP(SDRC_D15),       (IEN  | PTD | DIS | M0)) /*SDRC_D15*/\
706         MUX_VAL(CP(SDRC_D16),       (IEN  | PTD | DIS | M0)) /*SDRC_D16*/\
707         MUX_VAL(CP(SDRC_D17),       (IEN  | PTD | DIS | M0)) /*SDRC_D17*/\
708         MUX_VAL(CP(SDRC_D18),       (IEN  | PTD | DIS | M0)) /*SDRC_D18*/\
709         MUX_VAL(CP(SDRC_D19),       (IEN  | PTD | DIS | M0)) /*SDRC_D19*/\
710         MUX_VAL(CP(SDRC_D20),       (IEN  | PTD | DIS | M0)) /*SDRC_D20*/\
711         MUX_VAL(CP(SDRC_D21),       (IEN  | PTD | DIS | M0)) /*SDRC_D21*/\
712         MUX_VAL(CP(SDRC_D22),       (IEN  | PTD | DIS | M0)) /*SDRC_D22*/\
713         MUX_VAL(CP(SDRC_D23),       (IEN  | PTD | DIS | M0)) /*SDRC_D23*/\
714         MUX_VAL(CP(SDRC_D24),       (IEN  | PTD | DIS | M0)) /*SDRC_D24*/\
715         MUX_VAL(CP(SDRC_D25),       (IEN  | PTD | DIS | M0)) /*SDRC_D25*/\
716         MUX_VAL(CP(SDRC_D26),       (IEN  | PTD | DIS | M0)) /*SDRC_D26*/\
717         MUX_VAL(CP(SDRC_D27),       (IEN  | PTD | DIS | M0)) /*SDRC_D27*/\
718         MUX_VAL(CP(SDRC_D28),       (IEN  | PTD | DIS | M0)) /*SDRC_D28*/\
719         MUX_VAL(CP(SDRC_D29),       (IEN  | PTD | DIS | M0)) /*SDRC_D29*/\
720         MUX_VAL(CP(SDRC_D30),       (IEN  | PTD | DIS | M0)) /*SDRC_D30*/\
721         MUX_VAL(CP(SDRC_D31),       (IEN  | PTD | DIS | M0)) /*SDRC_D31*/\
722         MUX_VAL(CP(SDRC_CLK),       (IEN  | PTD | DIS | M0)) /*SDRC_CLK*/\
723         MUX_VAL(CP(SDRC_DQS0),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS0*/\
724         MUX_VAL(CP(SDRC_DQS1),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS1*/\
725         MUX_VAL(CP(SDRC_DQS2),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS2*/\
726         MUX_VAL(CP(SDRC_DQS3),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS3*/\
727         /*GPMC*/\
728         MUX_VAL(CP(GPMC_A1),        (IDIS | PTD | DIS | M0)) /*GPMC_A1*/\
729         MUX_VAL(CP(GPMC_A2),        (IDIS | PTD | DIS | M0)) /*GPMC_A2*/\
730         MUX_VAL(CP(GPMC_A3),        (IDIS | PTD | DIS | M0)) /*GPMC_A3*/\
731         MUX_VAL(CP(GPMC_A4),        (IDIS | PTD | DIS | M0)) /*GPMC_A4*/\
732         MUX_VAL(CP(GPMC_A5),        (IDIS | PTD | DIS | M0)) /*GPMC_A5*/\
733         MUX_VAL(CP(GPMC_A6),        (IDIS | PTD | DIS | M0)) /*GPMC_A6*/\
734         MUX_VAL(CP(GPMC_A7),        (IDIS | PTD | DIS | M0)) /*GPMC_A7*/\
735         MUX_VAL(CP(GPMC_A8),        (IDIS | PTD | DIS | M0)) /*GPMC_A8*/\
736         MUX_VAL(CP(GPMC_A9),        (IDIS | PTD | DIS | M0)) /*GPMC_A9*/\
737         MUX_VAL(CP(GPMC_A10),       (IDIS | PTD | DIS | M0)) /*GPMC_A10*/\
738         MUX_VAL(CP(GPMC_D0),        (IEN  | PTD | DIS | M0)) /*GPMC_D0*/\
739         MUX_VAL(CP(GPMC_D1),        (IEN  | PTD | DIS | M0)) /*GPMC_D1*/\
740         MUX_VAL(CP(GPMC_D2),        (IEN  | PTD | DIS | M0)) /*GPMC_D2*/\
741         MUX_VAL(CP(GPMC_D3),        (IEN  | PTD | DIS | M0)) /*GPMC_D3*/\
742         MUX_VAL(CP(GPMC_D4),        (IEN  | PTD | DIS | M0)) /*GPMC_D4*/\
743         MUX_VAL(CP(GPMC_D5),        (IEN  | PTD | DIS | M0)) /*GPMC_D5*/\
744         MUX_VAL(CP(GPMC_D6),        (IEN  | PTD | DIS | M0)) /*GPMC_D6*/\
745         MUX_VAL(CP(GPMC_D7),        (IEN  | PTD | DIS | M0)) /*GPMC_D7*/\
746         MUX_VAL(CP(GPMC_D8),        (IEN  | PTD | DIS | M0)) /*GPMC_D8*/\
747         MUX_VAL(CP(GPMC_D9),        (IEN  | PTD | DIS | M0)) /*GPMC_D9*/\
748         MUX_VAL(CP(GPMC_D10),       (IEN  | PTD | DIS | M0)) /*GPMC_D10*/\
749         MUX_VAL(CP(GPMC_D11),       (IEN  | PTD | DIS | M0)) /*GPMC_D11*/\
750         MUX_VAL(CP(GPMC_D12),       (IEN  | PTD | DIS | M0)) /*GPMC_D12*/\
751         MUX_VAL(CP(GPMC_D13),       (IEN  | PTD | DIS | M0)) /*GPMC_D13*/\
752         MUX_VAL(CP(GPMC_D14),       (IEN  | PTD | DIS | M0)) /*GPMC_D14*/\
753         MUX_VAL(CP(GPMC_D15),       (IEN  | PTD | DIS | M0)) /*GPMC_D15*/\
754         MUX_VAL(CP(GPMC_nCS0),      (IDIS | PTU | EN  | M0)) /*GPMC_nCS0*/\
755         MUX_VAL(CP(GPMC_nCS1),      (IDIS | PTU | EN  | M0)) /*GPMC_nCS1*/\
756         MUX_VAL(CP(GPMC_CLK),       (IDIS | PTD | DIS | M0)) /*GPMC_CLK*/\
757         MUX_VAL(CP(GPMC_nADV_ALE),  (IDIS | PTD | DIS | M0)) /*GPMC_nADV_ALE*/\
758         MUX_VAL(CP(GPMC_nOE),       (IDIS | PTD | DIS | M0)) /*GPMC_nOE*/\
759         MUX_VAL(CP(GPMC_nWE),       (IDIS | PTD | DIS | M0)) /*GPMC_nWE*/\
760         MUX_VAL(CP(GPMC_nBE0_CLE),  (IDIS | PTD | DIS | M0)) /*GPMC_nBE0_CLE*/\
761         MUX_VAL(CP(GPMC_nWP),       (IEN  | PTD | DIS | M0)) /*GPMC_nWP*/\
762         MUX_VAL(CP(GPMC_WAIT0),     (IEN  | PTU | EN  | M0)) /*GPMC_WAIT0*/\
763         MUX_VAL(CP(GPMC_WAIT1),     (IEN  | PTU | EN  | M0)) /*GPMC_WAIT1*/\
764         /*Serial Interface (Peripheral boot, Linux console)*/\
765         MUX_VAL(CP(UART3_RX_IRRX ), (IEN  | PTD | DIS | M0)) /*UART3_RX*/\
766         MUX_VAL(CP(UART3_TX_IRTX ), (IDIS | PTD | DIS | M0)) /*UART3_TX*/\
767         /*I2C Ports*/\
768         MUX_VAL(CP(I2C1_SCL),       (IEN  | PTU | EN  | M0)) /*I2C1_SCL*/\
769         MUX_VAL(CP(I2C1_SDA),       (IEN  | PTU | EN  | M0)) /*I2C1_SDA*/\
770         /*LEDs (Controlled by OMAP)*/\
771         MUX_VAL(CP(MMC1_DAT6),      (IDIS | PTD | DIS | M4)) /*GPIO_128*/\
772         MUX_VAL(CP(MMC1_DAT7),      (IDIS | PTD | DIS | M4)) /*GPIO_129*/\
773
774 /**********************************************************
775  * Routine: set_muxconf_regs
776  * Description: Setting up the configuration Mux registers
777  *              specific to the hardware. Many pins need
778  *              to be moved from protect to primary mode.
779  *********************************************************/
780 void set_muxconf_regs(void)
781 {
782         MUX_DEFAULT();
783 }
784
785 /**********************************************************
786  * Routine: nand+_init
787  * Description: Set up nand for nand and jffs2 commands
788  *********************************************************/
789 int nand_init(void)
790 {
791         /* global settings */
792         __raw_writel(0x10, GPMC_SYSCONFIG);     /* smart idle */
793         __raw_writel(0x0, GPMC_IRQENABLE);      /* isr's sources masked */
794         __raw_writel(0, GPMC_TIMEOUT_CONTROL);  /* timeout disable */
795 #ifdef CFG_NAND
796         __raw_writel(0x001, GPMC_CONFIG);       /* set nWP, disable limited addr */
797 #endif
798
799         /* Set the GPMC Vals, NAND is mapped at CS0
800          *  We configure only GPMC CS0 with required values. Configuring other devices
801          *  at other CS is done in u-boot. So we don't have to bother doing it here.
802          */
803         __raw_writel(0, GPMC_CONFIG7 + GPMC_CONFIG_CS0);
804         delay(1000);
805
806         if ((get_mem_type() == GPMC_NAND) || (get_mem_type() == MMC_NAND)) {
807                 __raw_writel(M_NAND_GPMC_CONFIG1, GPMC_CONFIG1 + GPMC_CONFIG_CS0);
808                 __raw_writel(M_NAND_GPMC_CONFIG2, GPMC_CONFIG2 + GPMC_CONFIG_CS0);
809                 __raw_writel(M_NAND_GPMC_CONFIG3, GPMC_CONFIG3 + GPMC_CONFIG_CS0);
810                 __raw_writel(M_NAND_GPMC_CONFIG4, GPMC_CONFIG4 + GPMC_CONFIG_CS0);
811                 __raw_writel(M_NAND_GPMC_CONFIG5, GPMC_CONFIG5 + GPMC_CONFIG_CS0);
812                 __raw_writel(M_NAND_GPMC_CONFIG6, GPMC_CONFIG6 + GPMC_CONFIG_CS0);
813
814                 /* Enable the GPMC Mapping */
815                 __raw_writel((((OMAP34XX_GPMC_CS0_SIZE & 0xF)<<8) |
816                              ((NAND_BASE_ADR>>24) & 0x3F) |
817                              (1<<6)),  (GPMC_CONFIG7 + GPMC_CONFIG_CS0));
818                 delay(2000);
819
820                 if (nand_chip()) {
821 #ifdef CFG_PRINTF
822                         printf("Unsupported Chip!\n");
823 #endif
824                         return 1;
825                 }
826
827         }
828
829         if ((get_mem_type() == GPMC_ONENAND) || (get_mem_type() == MMC_ONENAND)) {
830                 __raw_writel(ONENAND_GPMC_CONFIG1, GPMC_CONFIG1 + GPMC_CONFIG_CS0);
831                 __raw_writel(ONENAND_GPMC_CONFIG2, GPMC_CONFIG2 + GPMC_CONFIG_CS0);
832                 __raw_writel(ONENAND_GPMC_CONFIG3, GPMC_CONFIG3 + GPMC_CONFIG_CS0);
833                 __raw_writel(ONENAND_GPMC_CONFIG4, GPMC_CONFIG4 + GPMC_CONFIG_CS0);
834                 __raw_writel(ONENAND_GPMC_CONFIG5, GPMC_CONFIG5 + GPMC_CONFIG_CS0);
835                 __raw_writel(ONENAND_GPMC_CONFIG6, GPMC_CONFIG6 + GPMC_CONFIG_CS0);
836
837                 /* Enable the GPMC Mapping */
838                 __raw_writel((((OMAP34XX_GPMC_CS0_SIZE & 0xF)<<8) |
839                              ((ONENAND_BASE>>24) & 0x3F) |
840                              (1<<6)),  (GPMC_CONFIG7 + GPMC_CONFIG_CS0));
841                 delay(2000);
842
843                 if (onenand_chip()) {
844 #ifdef CFG_PRINTF
845                         printf("OneNAND Unsupported !\n");
846 #endif
847                         return 1;
848                 }
849         }
850         return 0;
851 }
852
853 #define DEBUG_SD1_LED                   128     /* gpio - SD Slot 1 */
854 #define DEBUG_SD2_LED                   129     /* gpio - SD Slot 2 */
855
856 void blinkLEDs()
857 {
858         void *p;
859
860         /* Alternately turn the LEDs on and off */
861         p = (unsigned long *)OMAP34XX_GPIO5_BASE;
862         while (1) {
863                 /* turn LED1 on and LED2 off */
864                 *(unsigned long *)(p + 0x94) = 1 << (DEBUG_SD1_LED % 32);
865                 *(unsigned long *)(p + 0x90) = 1 << (DEBUG_SD2_LED % 32);
866
867                 /* delay for a while */
868                 delay(1000);
869
870                 /* turn LED1 off and LED2 on */
871                 *(unsigned long *)(p + 0x90) = 1 << (DEBUG_SD1_LED % 32);
872                 *(unsigned long *)(p + 0x94) = 1 << (DEBUG_SD2_LED % 32);
873
874                 /* delay for a while */
875                 delay(1000);
876         }
877 }
878
879 /* optionally do something like blinking LED */
880 void board_hang(void)
881 {
882         while (1)
883                 blinkLEDs();
884 }
885
886 /******************************************************************************
887  * Dummy function to handle errors for EABI incompatibility
888  *****************************************************************************/
889 void raise(void)
890 {
891 }
892
893 /******************************************************************************
894  * Dummy function to handle errors for EABI incompatibility
895  *****************************************************************************/
896 void abort(void)
897 {
898 }