183d290c5STom Rini // SPDX-License-Identifier: GPL-2.0+ 262011840SMasahiro Yamada /* 362011840SMasahiro Yamada * Copyright (C) 2013 Atmel Corporation 462011840SMasahiro Yamada * Bo Shen <voice.shen@atmel.com> 562011840SMasahiro Yamada */ 662011840SMasahiro Yamada 762011840SMasahiro Yamada #include <common.h> 862011840SMasahiro Yamada #include <asm/io.h> 962011840SMasahiro Yamada #include <asm/arch/at91_common.h> 1062011840SMasahiro Yamada #include <asm/arch/at91_pit.h> 1162011840SMasahiro Yamada #include <asm/arch/at91_pmc.h> 1262011840SMasahiro Yamada #include <asm/arch/at91_rstc.h> 1362011840SMasahiro Yamada #include <asm/arch/at91_wdt.h> 1462011840SMasahiro Yamada #include <asm/arch/clk.h> 1562011840SMasahiro Yamada #include <spl.h> 1662011840SMasahiro Yamada switch_to_main_crystal_osc(void)1762011840SMasahiro Yamadastatic void switch_to_main_crystal_osc(void) 1862011840SMasahiro Yamada { 1962011840SMasahiro Yamada struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; 2062011840SMasahiro Yamada u32 tmp; 2162011840SMasahiro Yamada 2262011840SMasahiro Yamada tmp = readl(&pmc->mor); 2362011840SMasahiro Yamada tmp &= ~AT91_PMC_MOR_OSCOUNT(0xff); 2462011840SMasahiro Yamada tmp &= ~AT91_PMC_MOR_KEY(0xff); 2562011840SMasahiro Yamada tmp |= AT91_PMC_MOR_MOSCEN; 2662011840SMasahiro Yamada tmp |= AT91_PMC_MOR_OSCOUNT(8); 2762011840SMasahiro Yamada tmp |= AT91_PMC_MOR_KEY(0x37); 2862011840SMasahiro Yamada writel(tmp, &pmc->mor); 2962011840SMasahiro Yamada while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCS)) 3062011840SMasahiro Yamada ; 3162011840SMasahiro Yamada 323ceb5118SWenyou Yang #if defined(CONFIG_SAMA5D2) 333ceb5118SWenyou Yang /* Enable a measurement of the external oscillator */ 343ceb5118SWenyou Yang tmp = readl(&pmc->mcfr); 353ceb5118SWenyou Yang tmp |= AT91_PMC_MCFR_CCSS_XTAL_OSC; 363ceb5118SWenyou Yang tmp |= AT91_PMC_MCFR_RCMEAS; 373ceb5118SWenyou Yang writel(tmp, &pmc->mcfr); 383ceb5118SWenyou Yang 393ceb5118SWenyou Yang while (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINRDY)) 403ceb5118SWenyou Yang ; 413ceb5118SWenyou Yang 423ceb5118SWenyou Yang if (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINF_MASK)) 433ceb5118SWenyou Yang hang(); 443ceb5118SWenyou Yang #endif 453ceb5118SWenyou Yang 4662011840SMasahiro Yamada tmp = readl(&pmc->mor); 4762011840SMasahiro Yamada tmp &= ~AT91_PMC_MOR_OSCBYPASS; 4862011840SMasahiro Yamada tmp &= ~AT91_PMC_MOR_KEY(0xff); 4962011840SMasahiro Yamada tmp |= AT91_PMC_MOR_KEY(0x37); 5062011840SMasahiro Yamada writel(tmp, &pmc->mor); 5162011840SMasahiro Yamada 5262011840SMasahiro Yamada tmp = readl(&pmc->mor); 5362011840SMasahiro Yamada tmp |= AT91_PMC_MOR_MOSCSEL; 5462011840SMasahiro Yamada tmp &= ~AT91_PMC_MOR_KEY(0xff); 5562011840SMasahiro Yamada tmp |= AT91_PMC_MOR_KEY(0x37); 5662011840SMasahiro Yamada writel(tmp, &pmc->mor); 5762011840SMasahiro Yamada 5862011840SMasahiro Yamada while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCSELS)) 5962011840SMasahiro Yamada ; 6062011840SMasahiro Yamada 613ceb5118SWenyou Yang #if !defined(CONFIG_SAMA5D2) 6262011840SMasahiro Yamada /* Wait until MAINRDY field is set to make sure main clock is stable */ 6362011840SMasahiro Yamada while (!(readl(&pmc->mcfr) & AT91_PMC_MAINRDY)) 6462011840SMasahiro Yamada ; 653ceb5118SWenyou Yang #endif 6662011840SMasahiro Yamada 673ceb5118SWenyou Yang #if !defined(CONFIG_SAMA5D4) && !defined(CONFIG_SAMA5D2) 6862011840SMasahiro Yamada tmp = readl(&pmc->mor); 6962011840SMasahiro Yamada tmp &= ~AT91_PMC_MOR_MOSCRCEN; 7062011840SMasahiro Yamada tmp &= ~AT91_PMC_MOR_KEY(0xff); 7162011840SMasahiro Yamada tmp |= AT91_PMC_MOR_KEY(0x37); 7262011840SMasahiro Yamada writel(tmp, &pmc->mor); 7362011840SMasahiro Yamada #endif 7462011840SMasahiro Yamada } 7562011840SMasahiro Yamada matrix_init(void)7662011840SMasahiro Yamada__weak void matrix_init(void) 7762011840SMasahiro Yamada { 7862011840SMasahiro Yamada /* This only be used for sama5d4 soc now */ 7962011840SMasahiro Yamada } 8062011840SMasahiro Yamada redirect_int_from_saic_to_aic(void)8162011840SMasahiro Yamada__weak void redirect_int_from_saic_to_aic(void) 8262011840SMasahiro Yamada { 8362011840SMasahiro Yamada /* This only be used for sama5d4 soc now */ 8462011840SMasahiro Yamada } 8562011840SMasahiro Yamada 86e755d543STom Rini /* empty stub to satisfy current lowlevel_init, can be removed any time */ s_init(void)8762011840SMasahiro Yamadavoid s_init(void) 8862011840SMasahiro Yamada { 89e755d543STom Rini } 90e755d543STom Rini board_init_f(ulong dummy)91e755d543STom Rinivoid board_init_f(ulong dummy) 92e755d543STom Rini { 93730a7b47SWenyou Yang int ret; 94730a7b47SWenyou Yang 9562011840SMasahiro Yamada switch_to_main_crystal_osc(); 9662011840SMasahiro Yamada 97f7cf291aSSamuel Mescoff #ifdef CONFIG_SAMA5D2 98f7cf291aSSamuel Mescoff configure_2nd_sram_as_l2_cache(); 99f7cf291aSSamuel Mescoff #endif 100f7cf291aSSamuel Mescoff 101*1473f6acSPrasanthi Chellakumar #if !defined(CONFIG_WDT_AT91) 10262011840SMasahiro Yamada /* disable watchdog */ 10362011840SMasahiro Yamada at91_disable_wdt(); 104f58e9460STom Rini #endif 10562011840SMasahiro Yamada 10662011840SMasahiro Yamada /* PMC configuration */ 10762011840SMasahiro Yamada at91_pmc_init(); 10862011840SMasahiro Yamada 10962011840SMasahiro Yamada at91_clock_init(CONFIG_SYS_AT91_MAIN_CLOCK); 11062011840SMasahiro Yamada 11162011840SMasahiro Yamada matrix_init(); 11262011840SMasahiro Yamada 11362011840SMasahiro Yamada redirect_int_from_saic_to_aic(); 11462011840SMasahiro Yamada 11562011840SMasahiro Yamada timer_init(); 11662011840SMasahiro Yamada 11762011840SMasahiro Yamada board_early_init_f(); 11862011840SMasahiro Yamada 1195bede73cSWenyou Yang mem_init(); 1205bede73cSWenyou Yang 121730a7b47SWenyou Yang ret = spl_init(); 122730a7b47SWenyou Yang if (ret) { 123730a7b47SWenyou Yang debug("spl_init() failed: %d\n", ret); 124730a7b47SWenyou Yang hang(); 125730a7b47SWenyou Yang } 126730a7b47SWenyou Yang 12762011840SMasahiro Yamada preloader_console_init(); 12862011840SMasahiro Yamada 12962011840SMasahiro Yamada } 130