1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Atmel Corporation
4 * Bo Shen <voice.shen@atmel.com>
12 #include <asm/arch/at91_common.h>
13 #include <asm/arch/at91_pit.h>
14 #include <asm/arch/at91_pmc.h>
15 #include <asm/arch/at91_rstc.h>
16 #include <asm/arch/at91_wdt.h>
17 #include <asm/arch/clk.h>
20 static void switch_to_main_crystal_osc(void)
22 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
25 tmp = readl(&pmc->mor);
26 tmp &= ~AT91_PMC_MOR_OSCOUNT(0xff);
27 tmp &= ~AT91_PMC_MOR_KEY(0xff);
28 tmp |= AT91_PMC_MOR_MOSCEN;
29 tmp |= AT91_PMC_MOR_OSCOUNT(8);
30 tmp |= AT91_PMC_MOR_KEY(0x37);
31 writel(tmp, &pmc->mor);
32 while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCS))
35 #if defined(CONFIG_SAMA5D2)
36 /* Enable a measurement of the external oscillator */
37 tmp = readl(&pmc->mcfr);
38 tmp |= AT91_PMC_MCFR_CCSS_XTAL_OSC;
39 tmp |= AT91_PMC_MCFR_RCMEAS;
40 writel(tmp, &pmc->mcfr);
42 while (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINRDY))
45 if (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINF_MASK))
49 tmp = readl(&pmc->mor);
51 * some boards have an external oscillator with driving.
52 * in this case we need to disable the internal SoC driving (bypass mode)
54 #if defined(CONFIG_SPL_AT91_MCK_BYPASS)
55 tmp |= AT91_PMC_MOR_OSCBYPASS;
57 tmp &= ~AT91_PMC_MOR_OSCBYPASS;
59 tmp &= ~AT91_PMC_MOR_KEY(0xff);
60 tmp |= AT91_PMC_MOR_KEY(0x37);
61 writel(tmp, &pmc->mor);
63 tmp = readl(&pmc->mor);
64 tmp |= AT91_PMC_MOR_MOSCSEL;
65 tmp &= ~AT91_PMC_MOR_KEY(0xff);
66 tmp |= AT91_PMC_MOR_KEY(0x37);
67 writel(tmp, &pmc->mor);
69 while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCSELS))
72 #if !defined(CONFIG_SAMA5D2)
73 /* Wait until MAINRDY field is set to make sure main clock is stable */
74 while (!(readl(&pmc->mcfr) & AT91_PMC_MAINRDY))
78 #if !defined(CONFIG_SAMA5D4) && !defined(CONFIG_SAMA5D2)
79 tmp = readl(&pmc->mor);
80 tmp &= ~AT91_PMC_MOR_MOSCRCEN;
81 tmp &= ~AT91_PMC_MOR_KEY(0xff);
82 tmp |= AT91_PMC_MOR_KEY(0x37);
83 writel(tmp, &pmc->mor);
87 __weak void matrix_init(void)
89 /* This only be used for sama5d4 soc now */
92 __weak void redirect_int_from_saic_to_aic(void)
94 /* This only be used for sama5d4 soc now */
97 /* empty stub to satisfy current lowlevel_init, can be removed any time */
102 void board_init_f(ulong dummy)
106 switch_to_main_crystal_osc();
108 #ifdef CONFIG_SAMA5D2
109 configure_2nd_sram_as_l2_cache();
112 #if !defined(CONFIG_WDT_AT91)
113 /* disable watchdog */
117 /* PMC configuration */
120 at91_clock_init(CONFIG_SYS_AT91_MAIN_CLOCK);
124 redirect_int_from_saic_to_aic();
128 board_early_init_f();
134 debug("spl_init() failed: %d\n", ret);
138 preloader_console_init();