Add support for pandora board
[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  * GraÅžvydas Ignotas <notasas@gmail.com>
10  *
11  * See file CREDITS for list of people who contributed to this
12  * project.
13  *
14  * This program is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU General Public License as
16  * published by the Free Software Foundation; either version 2 of
17  * the License, or (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * You should have received a copy of the GNU General Public License
25  * along with this program; if not, write to the Free Software
26  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27  * MA 02111-1307 USA
28  */
29
30 #include <common.h>
31 #include <command.h>
32 #include <asm/arch/cpu.h>
33 #include <asm/arch/bits.h>
34 #include <asm/arch/mux.h>
35 #include <asm/arch/gpio.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 #include <part.h>
41 #include <fat.h>
42 #include <i2c.h>
43
44 /* params for 37XX */
45 #define CORE_DPLL_PARAM_M2      0x09
46 #define CORE_DPLL_PARAM_M       0x360
47 #define CORE_DPLL_PARAM_N       0xC
48
49 /* Used to index into DPLL parameter tables */
50 struct dpll_param {
51         unsigned int m;
52         unsigned int n;
53         unsigned int fsel;
54         unsigned int m2;
55 };
56
57 typedef struct dpll_param dpll_param;
58
59 /* Following functions are exported from lowlevel_init.S */
60 extern dpll_param *get_mpu_dpll_param(void);
61 extern dpll_param *get_iva_dpll_param(void);
62 extern dpll_param *get_core_dpll_param(void);
63 extern dpll_param *get_per_dpll_param(void);
64
65 #define __raw_readl(a)          (*(volatile unsigned int *)(a))
66 #define __raw_writel(v, a)      (*(volatile unsigned int *)(a) = (v))
67 #define __raw_readw(a)          (*(volatile unsigned short *)(a))
68 #define __raw_writew(v, a)      (*(volatile unsigned short *)(a) = (v))
69
70 /* slight RAM timing differencer,
71  * tCKE differs from beagle's Micron part */
72 #define MICRON_V_ACTIMB_200_2 (((MICRON_V_ACTIMB_200) & ~(7 << 12)) | (1 << 12))
73
74 /*******************************************************
75  * Routine: delay
76  * Description: spinning delay to use before udelay works
77  ******************************************************/
78 static inline void delay(unsigned long loops)
79 {
80         __asm__ volatile ("1:\n" "subs %0, %1, #1\n"
81                           "bne 1b":"=r" (loops):"0"(loops));
82 }
83
84 void udelay (unsigned long usecs) {
85         delay(usecs);
86 }
87
88 /*****************************************
89  * Routine: board_init
90  * Description: Early hardware init.
91  *****************************************/
92 int board_init(void)
93 {
94 #if defined(CFG_PRINTF) && defined(CONFIG_DRIVER_OMAP34XX_I2C)
95         unsigned char byte;
96
97         /* do additional init to prevent trash being output on startup */
98         serial_init();
99
100         /* restore GPBR1 for charging to work -
101          * boot ROM or something clears it before us */
102         byte = 0x90;
103         i2c_write(0x49, 0x91, 1, &byte, 1);
104
105         /* VAUX3 = 2.8V (AV connector) */
106         byte = 0x20;
107         i2c_write(0x4B, 0x7A, 1, &byte, 1);
108         byte = 0x03;
109         i2c_write(0x4B, 0x7D, 1, &byte, 1);
110 #endif
111
112         return 0;
113 }
114
115 /*************************************************************
116  * Routine: get_mem_type(void) - returns the kind of memory connected
117  * to GPMC that we are trying to boot form. Uses SYS BOOT settings.
118  *************************************************************/
119 u32 get_mem_type(void)
120 {
121         u32 mem_type = get_sysboot_value();
122         switch (mem_type) {
123         case 0:
124         case 2:
125         case 4:
126         case 16:
127         case 22:
128                 return GPMC_ONENAND;
129
130         case 1:
131         case 12:
132         case 15:
133         case 21:
134         case 27:
135                 return GPMC_NAND;
136
137         case 3:
138         case 6:
139                 return MMC_ONENAND;
140
141         case 8:
142         case 11:
143         case 14:
144         case 20:
145         case 26:
146                 return GPMC_MDOC;
147
148         case 17:
149         case 18:
150         case 24:
151                 return MMC_NAND;
152
153         case 7:
154         case 10:
155         case 13:
156         case 19:
157         case 25:
158         default:
159                 return GPMC_NOR;
160         }
161 }
162
163 static void early_nand_init(void)
164 {
165         /* global settings */
166         __raw_writel(0x10, GPMC_SYSCONFIG);     /* smart idle */
167         __raw_writel(0x0, GPMC_IRQENABLE);      /* isr's sources masked */
168         __raw_writel(0, GPMC_TIMEOUT_CONTROL);  /* timeout disable */
169
170         /* Set the GPMC Vals, NAND is mapped at CS0, oneNAND at CS0.
171          *  We configure only GPMC CS0 with required values. Configiring other devices
172          *  at other CS is done in u-boot. So we don't have to bother doing it here.
173          */
174         __raw_writel(0 , GPMC_CONFIG7 + GPMC_CONFIG_CS0);
175         delay(1000);
176
177         __raw_writel(M_NAND_GPMC_CONFIG1, GPMC_CONFIG1 + GPMC_CONFIG_CS0);
178         __raw_writel(M_NAND_GPMC_CONFIG2, GPMC_CONFIG2 + GPMC_CONFIG_CS0);
179         __raw_writel(M_NAND_GPMC_CONFIG3, GPMC_CONFIG3 + GPMC_CONFIG_CS0);
180         __raw_writel(M_NAND_GPMC_CONFIG4, GPMC_CONFIG4 + GPMC_CONFIG_CS0);
181         __raw_writel(M_NAND_GPMC_CONFIG5, GPMC_CONFIG5 + GPMC_CONFIG_CS0);
182         __raw_writel(M_NAND_GPMC_CONFIG6, GPMC_CONFIG6 + GPMC_CONFIG_CS0);
183
184         /* Enable the GPMC Mapping */
185         __raw_writel((((OMAP34XX_GPMC_CS0_SIZE & 0xF)<<8) |
186                                 ((NAND_BASE_ADR>>24) & 0x3F) |
187                                 (1<<6)),  (GPMC_CONFIG7 + GPMC_CONFIG_CS0));
188         delay(2000);
189 }
190
191 #ifdef CFG_3430SDRAM_DDR
192 static int strncmp(const char *s1, const char *s2, int n)
193 {
194         int i;
195         for (i = 0; i < n; i++)
196                 if (s1[i] != s2[i])
197                         return s1[i] - s2[i];
198         return 0;
199 }
200
201 #define MICRON_128MB_166MHZ 0
202 #define MICRON_256MB_200MHZ 1
203
204 /*********************************************************************
205  * config_sdram_ddr() - Init DDR SDRAM
206  *********************************************************************/
207 static void config_sdram_ddr(void)
208 {
209         unsigned char buf[64];
210         int ram_type = MICRON_128MB_166MHZ;
211         int ret;
212
213         /* reset sdrc controller */
214         __raw_writel(SOFTRESET, SDRC_SYSCONFIG);
215         wait_on_value(BIT0, BIT0, SDRC_STATUS, 12000000);
216         __raw_writel(0, SDRC_SYSCONFIG);
217
218         /* setup sdrc to ball mux */
219         __raw_writel(SDP_SDRC_SHARING, SDRC_SHARING);
220
221         /* read NAND id to guess what RAM is attached */
222         early_nand_init();
223         ret = nand_read_param_page(buf, sizeof(buf));
224         if (ret == 0) {
225                 if (strncmp((char *)buf + 44, "MT29F4G16ABBDA", 14) == 0)
226                         ram_type = MICRON_256MB_200MHZ;
227         }
228
229         if (ram_type == MICRON_256MB_200MHZ) {
230                 __raw_writel(0x2, SDRC_CS_CFG); /* 256MB/bank */
231                 __raw_writel(SDP_SDRC_MDCFG_0_DDR_MICRON_XM, SDRC_MCFG_0);
232                 __raw_writel(SDP_SDRC_MDCFG_0_DDR_MICRON_XM, SDRC_MCFG_1);
233         } else {
234                 __raw_writel(0x1, SDRC_CS_CFG); /* 128MB/bank */
235                 __raw_writel(SDP_SDRC_MDCFG_0_DDR, SDRC_MCFG_0);
236                 __raw_writel(SDP_SDRC_MDCFG_0_DDR, SDRC_MCFG_1);
237         }
238
239         if (get_cpu_family() == CPU_OMAP36XX && ram_type == MICRON_256MB_200MHZ) {
240                 __raw_writel(MICRON_V_ACTIMA_200, SDRC_ACTIM_CTRLA_0);
241                 __raw_writel(MICRON_V_ACTIMB_200_2, SDRC_ACTIM_CTRLB_0);
242                 __raw_writel(MICRON_V_ACTIMA_200, SDRC_ACTIM_CTRLA_1);
243                 __raw_writel(MICRON_V_ACTIMB_200_2, SDRC_ACTIM_CTRLB_1);
244                 __raw_writel(SDP_3430_SDRC_RFR_CTRL_200MHz, SDRC_RFR_CTRL_0);
245                 __raw_writel(SDP_3430_SDRC_RFR_CTRL_200MHz, SDRC_RFR_CTRL_1);
246         } else {
247                 __raw_writel(MICRON_V_ACTIMA_165, SDRC_ACTIM_CTRLA_0);
248                 __raw_writel(MICRON_V_ACTIMB_165, SDRC_ACTIM_CTRLB_0);
249                 __raw_writel(MICRON_V_ACTIMA_165, SDRC_ACTIM_CTRLA_1);
250                 __raw_writel(MICRON_V_ACTIMB_165, SDRC_ACTIM_CTRLB_1);
251                 __raw_writel(SDP_3430_SDRC_RFR_CTRL_165MHz, SDRC_RFR_CTRL_0);
252                 __raw_writel(SDP_3430_SDRC_RFR_CTRL_165MHz, SDRC_RFR_CTRL_1);
253         }
254
255         __raw_writel(SDP_SDRC_POWER_POP, SDRC_POWER);
256
257         /* init sequence for mDDR/mSDR using manual commands (DDR is different) */
258         __raw_writel(CMD_NOP, SDRC_MANUAL_0);
259         __raw_writel(CMD_NOP, SDRC_MANUAL_1);
260
261         delay(5000);
262
263         __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_0);
264         __raw_writel(CMD_PRECHARGE, SDRC_MANUAL_1);
265
266         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
267         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_1);
268
269         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_0);
270         __raw_writel(CMD_AUTOREFRESH, SDRC_MANUAL_1);
271
272         /* set mr0 */
273         __raw_writel(SDP_SDRC_MR_0_DDR, SDRC_MR_0);
274         __raw_writel(SDP_SDRC_MR_0_DDR, SDRC_MR_1);
275
276         /* set up dll */
277         __raw_writel(SDP_SDRC_DLLAB_CTRL, SDRC_DLLA_CTRL);
278         delay(0x2000);  /* give time to lock */
279 }
280 #endif /* CFG_3430SDRAM_DDR */
281
282 /*************************************************************
283  * get_sys_clk_speed - determine reference oscillator speed
284  *  based on known 32kHz clock and gptimer.
285  *************************************************************/
286 u32 get_osc_clk_speed(void)
287 {
288         u32 start, cstart, cend, cdiff, cdiv, val;
289
290         val = __raw_readl(PRM_CLKSRC_CTRL);
291
292         if (val & SYSCLKDIV_2)
293                 cdiv = 2;
294         else
295                 cdiv = 1;
296
297         /* enable timer2 */
298         val = __raw_readl(CM_CLKSEL_WKUP) | BIT0;
299         __raw_writel(val, CM_CLKSEL_WKUP);      /* select sys_clk for GPT1 */
300
301         /* Enable I and F Clocks for GPT1 */
302         val = __raw_readl(CM_ICLKEN_WKUP) | BIT0 | BIT2;
303         __raw_writel(val, CM_ICLKEN_WKUP);
304         val = __raw_readl(CM_FCLKEN_WKUP) | BIT0;
305         __raw_writel(val, CM_FCLKEN_WKUP);
306
307         __raw_writel(0, OMAP34XX_GPT1 + TLDR);          /* start counting at 0 */
308         __raw_writel(GPT_EN, OMAP34XX_GPT1 + TCLR);     /* enable clock */
309         /* enable 32kHz source */
310         /* enabled out of reset */
311         /* determine sys_clk via gauging */
312
313         start = 20 + __raw_readl(S32K_CR);      /* start time in 20 cycles */
314         while (__raw_readl(S32K_CR) < start) ;  /* dead loop till start time */
315         cstart = __raw_readl(OMAP34XX_GPT1 + TCRR);     /* get start sys_clk count */
316         while (__raw_readl(S32K_CR) < (start + 20)) ;   /* wait for 40 cycles */
317         cend = __raw_readl(OMAP34XX_GPT1 + TCRR);       /* get end sys_clk count */
318         cdiff = cend - cstart;  /* get elapsed ticks */
319         cdiff *= cdiv;
320
321         /* based on number of ticks assign speed */
322         if (cdiff > 19000)
323                 return S38_4M;
324         else if (cdiff > 15200)
325                 return S26M;
326         else if (cdiff > 13000)
327                 return S24M;
328         else if (cdiff > 9000)
329                 return S19_2M;
330         else if (cdiff > 7600)
331                 return S13M;
332         else
333                 return S12M;
334 }
335
336 /******************************************************************************
337  * prcm_init() - inits clocks for PRCM as defined in clocks.h
338  *   -- called from SRAM, or Flash (using temp SRAM stack).
339  *****************************************************************************/
340 void prcm_init(void)
341 {
342         u32 osc_clk = 0, sys_clkin_sel;
343         dpll_param *dpll_param_p;
344         u32 clk_index, sil_index;
345
346         /* Gauge the input clock speed and find out the sys_clkin_sel
347          * value corresponding to the input clock.
348          */
349         osc_clk = get_osc_clk_speed();
350         get_sys_clkin_sel(osc_clk, &sys_clkin_sel);
351
352         sr32(PRM_CLKSEL, 0, 3, sys_clkin_sel);  /* set input crystal speed */
353
354         /* If the input clock is greater than 19.2M always divide/2 */
355         if (sys_clkin_sel > 2) {
356                 sr32(PRM_CLKSRC_CTRL, 6, 2, 2); /* input clock divider */
357                 clk_index = sys_clkin_sel / 2;
358         } else {
359                 sr32(PRM_CLKSRC_CTRL, 6, 2, 1); /* input clock divider */
360                 clk_index = sys_clkin_sel;
361         }
362
363         sr32(PRM_CLKSRC_CTRL, 0, 2, 0);/* Bypass mode: T2 inputs a square clock */
364
365         /* The DPLL tables are defined according to sysclk value and
366          * silicon revision. The clk_index value will be used to get
367          * the values for that input sysclk from the DPLL param table
368          * and sil_index will get the values for that SysClk for the
369          * appropriate silicon rev.
370          */
371         sil_index = !(get_cpu_rev() == CPU_3XX_ES10);
372
373         /* Unlock MPU DPLL (slows things down, and needed later) */
374         sr32(CM_CLKEN_PLL_MPU, 0, 3, PLL_LOW_POWER_BYPASS);
375         wait_on_value(BIT0, 0, CM_IDLEST_PLL_MPU, LDELAY);
376
377         /* Getting the base address of Core DPLL param table */
378         dpll_param_p = (dpll_param *) get_core_dpll_param();
379         /* Moving it to the right sysclk and ES rev base */
380         dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
381         /* CORE DPLL */
382         /* sr32(CM_CLKSEL2_EMU) set override to work when asleep */
383         sr32(CM_CLKEN_PLL, 0, 3, PLL_FAST_RELOCK_BYPASS);
384         wait_on_value(BIT0, 0, CM_IDLEST_CKGEN, LDELAY);
385
386          /* For 3430 ES1.0 Errata 1.50, default value directly doesnt
387         work. write another value and then default value. */
388         sr32(CM_CLKSEL1_EMU, 16, 5, CORE_M3X2 + 1);     /* m3x2 */
389         sr32(CM_CLKSEL1_EMU, 16, 5, CORE_M3X2); /* m3x2 */
390         sr32(CM_CLKSEL1_PLL, 27, 2, dpll_param_p->m2);  /* Set M2 */
391         sr32(CM_CLKSEL1_PLL, 16, 11, dpll_param_p->m);  /* Set M */
392         sr32(CM_CLKSEL1_PLL, 8, 7, dpll_param_p->n);    /* Set N */
393         sr32(CM_CLKSEL1_PLL, 6, 1, 0);  /* 96M Src */
394         sr32(CM_CLKSEL_CORE, 8, 4, CORE_SSI_DIV);       /* ssi */
395         sr32(CM_CLKSEL_CORE, 4, 2, CORE_FUSB_DIV);      /* fsusb */
396         sr32(CM_CLKSEL_CORE, 2, 2, CORE_L4_DIV);        /* l4 */
397         sr32(CM_CLKSEL_CORE, 0, 2, CORE_L3_DIV);        /* l3 */
398         sr32(CM_CLKSEL_GFX, 0, 3, GFX_DIV);     /* gfx */
399         sr32(CM_CLKSEL_WKUP, 1, 2, WKUP_RSM);   /* reset mgr */
400         sr32(CM_CLKEN_PLL, 4, 4, dpll_param_p->fsel);   /* FREQSEL */
401         sr32(CM_CLKEN_PLL, 0, 3, PLL_LOCK);     /* lock mode */
402         wait_on_value(BIT0, 1, CM_IDLEST_CKGEN, LDELAY);
403
404         /* Getting the base address to PER  DPLL param table */
405         dpll_param_p = (dpll_param *) get_per_dpll_param();
406         /* Moving it to the right sysclk base */
407         dpll_param_p = dpll_param_p + clk_index;
408         /* PER DPLL */
409         sr32(CM_CLKEN_PLL, 16, 3, PLL_STOP);
410         wait_on_value(BIT1, 0, CM_IDLEST_CKGEN, LDELAY);
411         sr32(CM_CLKSEL1_EMU, 24, 5, PER_M6X2);  /* set M6 */
412         sr32(CM_CLKSEL_CAM, 0, 5, PER_M5X2);    /* set M5 */
413         sr32(CM_CLKSEL_DSS, 0, 5, PER_M4X2);    /* set M4 */
414         sr32(CM_CLKSEL_DSS, 8, 5, PER_M3X2);    /* set M3 */
415
416         if (get_cpu_family() == CPU_OMAP36XX) {
417                 sr32(CM_CLKSEL3_PLL, 0, 5, CORE_DPLL_PARAM_M2);   /* set M2 */
418                 sr32(CM_CLKSEL2_PLL, 8, 11, CORE_DPLL_PARAM_M);   /* set m */
419                 sr32(CM_CLKSEL2_PLL, 0, 7, CORE_DPLL_PARAM_N);    /* set n */
420         } else {
421                 sr32(CM_CLKSEL3_PLL, 0, 5, dpll_param_p->m2);   /* set M2 */
422                 sr32(CM_CLKSEL2_PLL, 8, 11, dpll_param_p->m);   /* set m */
423                 sr32(CM_CLKSEL2_PLL, 0, 7, dpll_param_p->n);    /* set n */
424         }
425
426         sr32(CM_CLKEN_PLL, 20, 4, dpll_param_p->fsel);  /* FREQSEL */
427         sr32(CM_CLKEN_PLL, 16, 3, PLL_LOCK);    /* lock mode */
428         wait_on_value(BIT1, 2, CM_IDLEST_CKGEN, LDELAY);
429
430         /* Getting the base address to MPU DPLL param table */
431         dpll_param_p = (dpll_param *) get_mpu_dpll_param();
432
433         /* Moving it to the right sysclk and ES rev base */
434         dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
435
436         /* MPU DPLL (unlocked already) */
437         sr32(CM_CLKSEL2_PLL_MPU, 0, 5, dpll_param_p->m2);       /* Set M2 */
438         sr32(CM_CLKSEL1_PLL_MPU, 8, 11, dpll_param_p->m);       /* Set M */
439         sr32(CM_CLKSEL1_PLL_MPU, 0, 7, dpll_param_p->n);        /* Set N */
440         sr32(CM_CLKEN_PLL_MPU, 4, 4, dpll_param_p->fsel);       /* FREQSEL */
441         sr32(CM_CLKEN_PLL_MPU, 0, 3, PLL_LOCK); /* lock mode */
442         wait_on_value(BIT0, 1, CM_IDLEST_PLL_MPU, LDELAY);
443
444         /* Getting the base address to IVA DPLL param table */
445         dpll_param_p = (dpll_param *) get_iva_dpll_param();
446         /* Moving it to the right sysclk and ES rev base */
447         dpll_param_p = dpll_param_p + 3 * clk_index + sil_index;
448         /* IVA DPLL (set to 12*20=240MHz) */
449         sr32(CM_CLKEN_PLL_IVA2, 0, 3, PLL_STOP);
450         wait_on_value(BIT0, 0, CM_IDLEST_PLL_IVA2, LDELAY);
451         sr32(CM_CLKSEL2_PLL_IVA2, 0, 5, dpll_param_p->m2);      /* set M2 */
452         sr32(CM_CLKSEL1_PLL_IVA2, 8, 11, dpll_param_p->m);      /* set M */
453         sr32(CM_CLKSEL1_PLL_IVA2, 0, 7, dpll_param_p->n);       /* set N */
454         sr32(CM_CLKEN_PLL_IVA2, 4, 4, dpll_param_p->fsel);      /* FREQSEL */
455         sr32(CM_CLKEN_PLL_IVA2, 0, 3, PLL_LOCK);        /* lock mode */
456         wait_on_value(BIT0, 1, CM_IDLEST_PLL_IVA2, LDELAY);
457
458         /* Set up GPTimers to sys_clk source only */
459         sr32(CM_CLKSEL_PER, 0, 8, 0xff);
460         sr32(CM_CLKSEL_WKUP, 0, 1, 1);
461
462         delay(5000);
463 }
464
465 /**********************************************************
466  * Routine: s_init
467  * Description: Does early system init of muxing and clocks.
468  * - Called at time when only stack is available.
469  **********************************************************/
470
471 void s_init(void)
472 {
473         watchdog_init();
474         try_unlock_memory();
475         set_muxconf_regs();
476         delay(100);
477         per_clocks_enable();
478         prcm_init();
479         config_sdram_ddr();
480 }
481
482 /*******************************************************
483  * Routine: misc_init_r
484  ********************************************************/
485 int misc_init_r(void)
486 {
487 #ifdef CFG_PRINTF
488         unsigned char buf[64];
489         char nand_model[20];
490         int i, ret;
491
492         printf("OpenPandora System\n");
493         print_cpuinfo();
494
495         /* log NAND model */
496         ret = nand_read_param_page(buf, sizeof(buf));
497         if (ret == 0) {
498                 for (i = 0; i < sizeof(nand_model) - 1; i++) {
499                         if (buf[i + 44] == ' ')
500                                 break;
501                         nand_model[i] = buf[i + 44];
502                 }
503                 nand_model[i] = 0;
504                 printf("NAND: %s\n", nand_model);
505         }
506 #endif
507
508         return 0;
509 }
510
511 /******************************************************
512  * Routine: wait_for_command_complete
513  * Description: Wait for posting to finish on watchdog
514  ******************************************************/
515 void wait_for_command_complete(unsigned int wd_base)
516 {
517         int pending = 1;
518         do {
519                 pending = __raw_readl(wd_base + WWPS);
520         } while (pending);
521 }
522
523 /****************************************
524  * Routine: watchdog_init
525  * Description: Shut down watch dogs
526  *****************************************/
527 void watchdog_init(void)
528 {
529         /* There are 3 watch dogs WD1=Secure, WD2=MPU, WD3=IVA. WD1 is
530          * either taken care of by ROM (HS/EMU) or not accessible (GP).
531          * We need to take care of WD2-MPU or take a PRCM reset.  WD3
532          * should not be running and does not generate a PRCM reset.
533          */
534         sr32(CM_FCLKEN_WKUP, 5, 1, 1);
535         sr32(CM_ICLKEN_WKUP, 5, 1, 1);
536         wait_on_value(BIT5, 0x20, CM_IDLEST_WKUP, 5);   /* some issue here */
537
538         __raw_writel(WD_UNLOCK1, WD2_BASE + WSPR);
539         wait_for_command_complete(WD2_BASE);
540         __raw_writel(WD_UNLOCK2, WD2_BASE + WSPR);
541 }
542
543 /**********************************************
544  * Routine: dram_init
545  * Description: sets uboots idea of sdram size
546  **********************************************/
547 int dram_init(void)
548 {
549         return 0;
550 }
551
552 /*****************************************************************
553  * Routine: peripheral_enable
554  * Description: Enable the clks & power for perifs (GPT2, UART1,...)
555  ******************************************************************/
556 void per_clocks_enable(void)
557 {
558         /* Enable GP2 timer. */
559         sr32(CM_CLKSEL_PER, 0, 1, 0x1); /* GPT2 = sys clk */
560         sr32(CM_ICLKEN_PER, 3, 1, 0x1); /* ICKen GPT2 */
561         sr32(CM_FCLKEN_PER, 3, 1, 0x1); /* FCKen GPT2 */
562
563 #ifdef CFG_NS16550
564         /* UART1 clocks */
565         sr32(CM_FCLKEN1_CORE, 13, 1, 0x1);
566         sr32(CM_ICLKEN1_CORE, 13, 1, 0x1);
567
568         /* UART 3 Clocks */
569         sr32(CM_FCLKEN_PER, 11, 1, 0x1);
570         sr32(CM_ICLKEN_PER, 11, 1, 0x1);
571 #endif
572
573 #ifdef CONFIG_DRIVER_OMAP34XX_I2C
574         /* Turn on all 3 I2C clocks */
575         sr32(CM_FCLKEN1_CORE, 15, 3, 0x7);
576         sr32(CM_ICLKEN1_CORE, 15, 3, 0x7);      /* I2C1,2,3 = on */
577 #endif
578
579         /* Enable the ICLK for 32K Sync Timer as its used in udelay */
580         sr32(CM_ICLKEN_WKUP, 2, 1, 0x1);
581
582         sr32(CM_FCLKEN_IVA2, 0, 32, FCK_IVA2_ON);
583         sr32(CM_FCLKEN1_CORE, 0, 32, FCK_CORE1_ON);
584         sr32(CM_ICLKEN1_CORE, 0, 32, ICK_CORE1_ON);
585         sr32(CM_ICLKEN2_CORE, 0, 32, ICK_CORE2_ON);
586         sr32(CM_FCLKEN_WKUP, 0, 32, FCK_WKUP_ON);
587         sr32(CM_ICLKEN_WKUP, 0, 32, ICK_WKUP_ON);
588         sr32(CM_FCLKEN_DSS, 0, 32, FCK_DSS_ON);
589         sr32(CM_ICLKEN_DSS, 0, 32, ICK_DSS_ON);
590         sr32(CM_FCLKEN_CAM, 0, 32, FCK_CAM_ON);
591         sr32(CM_ICLKEN_CAM, 0, 32, ICK_CAM_ON);
592         sr32(CM_FCLKEN_PER, 0, 32, FCK_PER_ON);
593         sr32(CM_ICLKEN_PER, 0, 32, ICK_PER_ON);
594
595         /* Enable GPIO5 clocks for blinky LEDs */
596         sr32(CM_FCLKEN_PER, 16, 1, 0x1);        /* FCKen GPIO5 */
597         sr32(CM_ICLKEN_PER, 16, 1, 0x1);        /* ICKen GPIO5 */
598
599         delay(1000);
600 }
601
602 /* Set MUX for UART, GPMC, SDRC, GPIO */
603
604 #define         MUX_VAL(OFFSET,VALUE)\
605                 __raw_writew((VALUE), OMAP34XX_CTRL_BASE + (OFFSET));
606
607 #define         CP(x)   (CONTROL_PADCONF_##x)
608 /*
609  * IEN  - Input Enable
610  * IDIS - Input Disable
611  * PTD  - Pull type Down
612  * PTU  - Pull type Up
613  * DIS  - Pull type selection is inactive
614  * EN   - Pull type selection is active
615  * M0   - Mode 0
616  * The commented string gives the final mux configuration for that pin
617  */
618 #define MUX_DEFAULT()\
619         /*SDRC*/\
620         MUX_VAL(CP(SDRC_D0),        (IEN  | PTD | DIS | M0)) /*SDRC_D0*/\
621         MUX_VAL(CP(SDRC_D1),        (IEN  | PTD | DIS | M0)) /*SDRC_D1*/\
622         MUX_VAL(CP(SDRC_D2),        (IEN  | PTD | DIS | M0)) /*SDRC_D2*/\
623         MUX_VAL(CP(SDRC_D3),        (IEN  | PTD | DIS | M0)) /*SDRC_D3*/\
624         MUX_VAL(CP(SDRC_D4),        (IEN  | PTD | DIS | M0)) /*SDRC_D4*/\
625         MUX_VAL(CP(SDRC_D5),        (IEN  | PTD | DIS | M0)) /*SDRC_D5*/\
626         MUX_VAL(CP(SDRC_D6),        (IEN  | PTD | DIS | M0)) /*SDRC_D6*/\
627         MUX_VAL(CP(SDRC_D7),        (IEN  | PTD | DIS | M0)) /*SDRC_D7*/\
628         MUX_VAL(CP(SDRC_D8),        (IEN  | PTD | DIS | M0)) /*SDRC_D8*/\
629         MUX_VAL(CP(SDRC_D9),        (IEN  | PTD | DIS | M0)) /*SDRC_D9*/\
630         MUX_VAL(CP(SDRC_D10),       (IEN  | PTD | DIS | M0)) /*SDRC_D10*/\
631         MUX_VAL(CP(SDRC_D11),       (IEN  | PTD | DIS | M0)) /*SDRC_D11*/\
632         MUX_VAL(CP(SDRC_D12),       (IEN  | PTD | DIS | M0)) /*SDRC_D12*/\
633         MUX_VAL(CP(SDRC_D13),       (IEN  | PTD | DIS | M0)) /*SDRC_D13*/\
634         MUX_VAL(CP(SDRC_D14),       (IEN  | PTD | DIS | M0)) /*SDRC_D14*/\
635         MUX_VAL(CP(SDRC_D15),       (IEN  | PTD | DIS | M0)) /*SDRC_D15*/\
636         MUX_VAL(CP(SDRC_D16),       (IEN  | PTD | DIS | M0)) /*SDRC_D16*/\
637         MUX_VAL(CP(SDRC_D17),       (IEN  | PTD | DIS | M0)) /*SDRC_D17*/\
638         MUX_VAL(CP(SDRC_D18),       (IEN  | PTD | DIS | M0)) /*SDRC_D18*/\
639         MUX_VAL(CP(SDRC_D19),       (IEN  | PTD | DIS | M0)) /*SDRC_D19*/\
640         MUX_VAL(CP(SDRC_D20),       (IEN  | PTD | DIS | M0)) /*SDRC_D20*/\
641         MUX_VAL(CP(SDRC_D21),       (IEN  | PTD | DIS | M0)) /*SDRC_D21*/\
642         MUX_VAL(CP(SDRC_D22),       (IEN  | PTD | DIS | M0)) /*SDRC_D22*/\
643         MUX_VAL(CP(SDRC_D23),       (IEN  | PTD | DIS | M0)) /*SDRC_D23*/\
644         MUX_VAL(CP(SDRC_D24),       (IEN  | PTD | DIS | M0)) /*SDRC_D24*/\
645         MUX_VAL(CP(SDRC_D25),       (IEN  | PTD | DIS | M0)) /*SDRC_D25*/\
646         MUX_VAL(CP(SDRC_D26),       (IEN  | PTD | DIS | M0)) /*SDRC_D26*/\
647         MUX_VAL(CP(SDRC_D27),       (IEN  | PTD | DIS | M0)) /*SDRC_D27*/\
648         MUX_VAL(CP(SDRC_D28),       (IEN  | PTD | DIS | M0)) /*SDRC_D28*/\
649         MUX_VAL(CP(SDRC_D29),       (IEN  | PTD | DIS | M0)) /*SDRC_D29*/\
650         MUX_VAL(CP(SDRC_D30),       (IEN  | PTD | DIS | M0)) /*SDRC_D30*/\
651         MUX_VAL(CP(SDRC_D31),       (IEN  | PTD | DIS | M0)) /*SDRC_D31*/\
652         MUX_VAL(CP(SDRC_CLK),       (IEN  | PTD | DIS | M0)) /*SDRC_CLK*/\
653         MUX_VAL(CP(SDRC_DQS0),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS0*/\
654         MUX_VAL(CP(SDRC_DQS1),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS1*/\
655         MUX_VAL(CP(SDRC_DQS2),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS2*/\
656         MUX_VAL(CP(SDRC_DQS3),      (IEN  | PTD | DIS | M0)) /*SDRC_DQS3*/\
657         /*GPMC*/\
658         MUX_VAL(CP(GPMC_A1),        (IDIS | PTD | DIS | M0)) /*GPMC_A1*/\
659         MUX_VAL(CP(GPMC_A2),        (IDIS | PTD | DIS | M0)) /*GPMC_A2*/\
660         MUX_VAL(CP(GPMC_A3),        (IDIS | PTD | DIS | M0)) /*GPMC_A3*/\
661         MUX_VAL(CP(GPMC_A4),        (IDIS | PTD | DIS | M0)) /*GPMC_A4*/\
662         MUX_VAL(CP(GPMC_A5),        (IDIS | PTD | DIS | M0)) /*GPMC_A5*/\
663         MUX_VAL(CP(GPMC_A6),        (IDIS | PTD | DIS | M0)) /*GPMC_A6*/\
664         MUX_VAL(CP(GPMC_A7),        (IDIS | PTD | DIS | M0)) /*GPMC_A7*/\
665         MUX_VAL(CP(GPMC_A8),        (IDIS | PTD | DIS | M0)) /*GPMC_A8*/\
666         MUX_VAL(CP(GPMC_A9),        (IDIS | PTD | DIS | M0)) /*GPMC_A9*/\
667         MUX_VAL(CP(GPMC_A10),       (IDIS | PTD | DIS | M0)) /*GPMC_A10*/\
668         MUX_VAL(CP(GPMC_D0),        (IEN  | PTD | DIS | M0)) /*GPMC_D0*/\
669         MUX_VAL(CP(GPMC_D1),        (IEN  | PTD | DIS | M0)) /*GPMC_D1*/\
670         MUX_VAL(CP(GPMC_D2),        (IEN  | PTD | DIS | M0)) /*GPMC_D2*/\
671         MUX_VAL(CP(GPMC_D3),        (IEN  | PTD | DIS | M0)) /*GPMC_D3*/\
672         MUX_VAL(CP(GPMC_D4),        (IEN  | PTD | DIS | M0)) /*GPMC_D4*/\
673         MUX_VAL(CP(GPMC_D5),        (IEN  | PTD | DIS | M0)) /*GPMC_D5*/\
674         MUX_VAL(CP(GPMC_D6),        (IEN  | PTD | DIS | M0)) /*GPMC_D6*/\
675         MUX_VAL(CP(GPMC_D7),        (IEN  | PTD | DIS | M0)) /*GPMC_D7*/\
676         MUX_VAL(CP(GPMC_D8),        (IEN  | PTD | DIS | M0)) /*GPMC_D8*/\
677         MUX_VAL(CP(GPMC_D9),        (IEN  | PTD | DIS | M0)) /*GPMC_D9*/\
678         MUX_VAL(CP(GPMC_D10),       (IEN  | PTD | DIS | M0)) /*GPMC_D10*/\
679         MUX_VAL(CP(GPMC_D11),       (IEN  | PTD | DIS | M0)) /*GPMC_D11*/\
680         MUX_VAL(CP(GPMC_D12),       (IEN  | PTD | DIS | M0)) /*GPMC_D12*/\
681         MUX_VAL(CP(GPMC_D13),       (IEN  | PTD | DIS | M0)) /*GPMC_D13*/\
682         MUX_VAL(CP(GPMC_D14),       (IEN  | PTD | DIS | M0)) /*GPMC_D14*/\
683         MUX_VAL(CP(GPMC_D15),       (IEN  | PTD | DIS | M0)) /*GPMC_D15*/\
684         MUX_VAL(CP(GPMC_nCS0),      (IDIS | PTU | EN  | M0)) /*GPMC_nCS0*/\
685         MUX_VAL(CP(GPMC_nCS1),      (IDIS | PTU | EN  | M0)) /*GPMC_nCS1*/\
686         MUX_VAL(CP(GPMC_CLK),       (IDIS | PTD | DIS | M0)) /*GPMC_CLK*/\
687         MUX_VAL(CP(GPMC_nADV_ALE),  (IDIS | PTD | DIS | M0)) /*GPMC_nADV_ALE*/\
688         MUX_VAL(CP(GPMC_nOE),       (IDIS | PTD | DIS | M0)) /*GPMC_nOE*/\
689         MUX_VAL(CP(GPMC_nWE),       (IDIS | PTD | DIS | M0)) /*GPMC_nWE*/\
690         MUX_VAL(CP(GPMC_nBE0_CLE),  (IDIS | PTD | DIS | M0)) /*GPMC_nBE0_CLE*/\
691         MUX_VAL(CP(GPMC_nWP),       (IEN  | PTD | DIS | M0)) /*GPMC_nWP*/\
692         MUX_VAL(CP(GPMC_WAIT0),     (IEN  | PTU | EN  | M0)) /*GPMC_WAIT0*/\
693         MUX_VAL(CP(GPMC_WAIT1),     (IEN  | PTU | EN  | M0)) /*GPMC_WAIT1*/\
694         /*Serial Interface (Peripheral boot, Linux console)*/\
695         MUX_VAL(CP(UART3_RX_IRRX ), (IEN  | PTD | DIS | M0)) /*UART3_RX*/\
696         MUX_VAL(CP(UART3_TX_IRTX ), (IDIS | PTD | DIS | M0)) /*UART3_TX*/\
697         /*I2C Ports*/\
698         MUX_VAL(CP(I2C1_SCL),       (IEN  | PTU | EN  | M0)) /*I2C1_SCL*/\
699         MUX_VAL(CP(I2C1_SDA),       (IEN  | PTU | EN  | M0)) /*I2C1_SDA*/\
700         /*LEDs (Controlled by OMAP)*/\
701         MUX_VAL(CP(MMC1_DAT6),      (IDIS | PTD | DIS | M4)) /*GPIO_128*/\
702         MUX_VAL(CP(MMC1_DAT7),      (IDIS | PTD | DIS | M4)) /*GPIO_129*/\
703
704 /**********************************************************
705  * Routine: set_muxconf_regs
706  * Description: Setting up the configuration Mux registers
707  *              specific to the hardware. Many pins need
708  *              to be moved from protect to primary mode.
709  *********************************************************/
710 void set_muxconf_regs(void)
711 {
712         MUX_DEFAULT();
713 }
714
715 /**********************************************************
716  * Routine: nand+_init
717  * Description: Set up nand for nand and jffs2 commands
718  *********************************************************/
719 int nand_init(void)
720 {
721         /* init already done in early_nand_init */
722
723         if (get_mem_type() != GPMC_NAND && get_mem_type() != MMC_NAND) {
724 #ifdef CFG_PRINTF
725                 printf("Unsupported flash chip!\n");
726 #endif
727                 return 1;
728         }
729
730         if (nand_chip()) {
731 #ifdef CFG_PRINTF
732                 printf("Unsupported NAND Chip!\n");
733 #endif
734                 return 1;
735         }
736
737         return 0;
738 }
739
740 #define DEBUG_LED1                      128     /* gpio - SD Slot 1 */
741 #define DEBUG_LED2                      129     /* gpio - SD Slot 2 */
742
743 static void blinkLEDs(void)
744 {
745         void *p;
746
747         /* Alternately turn the LEDs on and off */
748         p = (unsigned long *)OMAP34XX_GPIO5_BASE;
749         while (1) {
750                 /* turn LED1 on and LED2 off */
751                 *(unsigned long *)(p + 0x94) = 1 << (DEBUG_LED1 % 32);
752                 *(unsigned long *)(p + 0x90) = 1 << (DEBUG_LED2 % 32);
753
754                 /* delay for a while */
755                 delay(1000);
756
757                 /* turn LED1 off and LED2 on */
758                 *(unsigned long *)(p + 0x90) = 1 << (DEBUG_LED1 % 32);
759                 *(unsigned long *)(p + 0x94) = 1 << (DEBUG_LED2 % 32);
760
761                 /* delay for a while */
762                 delay(1000);
763         }
764 }
765
766 /* optionally do something like blinking LED */
767 void board_hang(void)
768 {
769         while (1)
770                 blinkLEDs();
771 }
772
773 /******************************************************************************
774  * Dummy function to handle errors for EABI incompatibility
775  *****************************************************************************/
776 void raise(void)
777 {
778 }
779
780 /******************************************************************************
781  * Dummy function to handle errors for EABI incompatibility
782  *****************************************************************************/
783 void abort(void)
784 {
785 }