1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2013 Atmel Corporation
10 #include <asm/arch/at91_common.h>
11 #include <asm/arch/at91_pit.h>
12 #include <asm/arch/at91_pmc.h>
13 #include <asm/arch/at91_rstc.h>
14 #include <asm/arch/at91_wdt.h>
15 #include <asm/arch/clk.h>
18 static void switch_to_main_crystal_osc(void)
20 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
23 tmp = readl(&pmc->mor);
24 tmp &= ~AT91_PMC_MOR_OSCOUNT(0xff);
25 tmp &= ~AT91_PMC_MOR_KEY(0xff);
26 tmp |= AT91_PMC_MOR_MOSCEN;
27 tmp |= AT91_PMC_MOR_OSCOUNT(8);
28 tmp |= AT91_PMC_MOR_KEY(0x37);
29 writel(tmp, &pmc->mor);
30 while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCS))
33 #if defined(CONFIG_SAMA5D2)
34 /* Enable a measurement of the external oscillator */
35 tmp = readl(&pmc->mcfr);
36 tmp |= AT91_PMC_MCFR_CCSS_XTAL_OSC;
37 tmp |= AT91_PMC_MCFR_RCMEAS;
38 writel(tmp, &pmc->mcfr);
40 while (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINRDY))
43 if (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINF_MASK))
47 tmp = readl(&pmc->mor);
49 * some boards have an external oscillator with driving.
50 * in this case we need to disable the internal SoC driving (bypass mode)
52 #if defined(CONFIG_SPL_AT91_MCK_BYPASS)
53 tmp |= AT91_PMC_MOR_OSCBYPASS;
55 tmp &= ~AT91_PMC_MOR_OSCBYPASS;
57 tmp &= ~AT91_PMC_MOR_KEY(0xff);
58 tmp |= AT91_PMC_MOR_KEY(0x37);
59 writel(tmp, &pmc->mor);
61 tmp = readl(&pmc->mor);
62 tmp |= AT91_PMC_MOR_MOSCSEL;
63 tmp &= ~AT91_PMC_MOR_KEY(0xff);
64 tmp |= AT91_PMC_MOR_KEY(0x37);
65 writel(tmp, &pmc->mor);
67 while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCSELS))
70 #if !defined(CONFIG_SAMA5D2)
71 /* Wait until MAINRDY field is set to make sure main clock is stable */
72 while (!(readl(&pmc->mcfr) & AT91_PMC_MAINRDY))
76 #if !defined(CONFIG_SAMA5D4) && !defined(CONFIG_SAMA5D2)
77 tmp = readl(&pmc->mor);
78 tmp &= ~AT91_PMC_MOR_MOSCRCEN;
79 tmp &= ~AT91_PMC_MOR_KEY(0xff);
80 tmp |= AT91_PMC_MOR_KEY(0x37);
81 writel(tmp, &pmc->mor);
85 __weak void matrix_init(void)
87 /* This only be used for sama5d4 soc now */
90 __weak void redirect_int_from_saic_to_aic(void)
92 /* This only be used for sama5d4 soc now */
95 /* empty stub to satisfy current lowlevel_init, can be removed any time */
100 void board_init_f(ulong dummy)
104 switch_to_main_crystal_osc();
106 #ifdef CONFIG_SAMA5D2
107 configure_2nd_sram_as_l2_cache();
110 #if !defined(CONFIG_WDT_AT91)
111 /* disable watchdog */
115 /* PMC configuration */
118 at91_clock_init(CONFIG_SYS_AT91_MAIN_CLOCK);
122 redirect_int_from_saic_to_aic();
126 board_early_init_f();
132 debug("spl_init() failed: %d\n", ret);
136 preloader_console_init();