Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2013 Atmel Corporation |
| 4 | * Bo Shen <voice.shen@atmel.com> |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
| 8 | #include <asm/io.h> |
| 9 | #include <asm/arch/at91_common.h> |
| 10 | #include <asm/arch/at91_pit.h> |
| 11 | #include <asm/arch/at91_pmc.h> |
| 12 | #include <asm/arch/at91_rstc.h> |
| 13 | #include <asm/arch/at91_wdt.h> |
| 14 | #include <asm/arch/clk.h> |
| 15 | #include <spl.h> |
| 16 | |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 17 | static void switch_to_main_crystal_osc(void) |
| 18 | { |
| 19 | struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; |
| 20 | u32 tmp; |
| 21 | |
| 22 | tmp = readl(&pmc->mor); |
| 23 | tmp &= ~AT91_PMC_MOR_OSCOUNT(0xff); |
| 24 | tmp &= ~AT91_PMC_MOR_KEY(0xff); |
| 25 | tmp |= AT91_PMC_MOR_MOSCEN; |
| 26 | tmp |= AT91_PMC_MOR_OSCOUNT(8); |
| 27 | tmp |= AT91_PMC_MOR_KEY(0x37); |
| 28 | writel(tmp, &pmc->mor); |
| 29 | while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCS)) |
| 30 | ; |
| 31 | |
Wenyou Yang | f5fb545 | 2017-09-13 14:58:48 +0800 | [diff] [blame] | 32 | #if defined(CONFIG_SAMA5D2) |
| 33 | /* Enable a measurement of the external oscillator */ |
| 34 | tmp = readl(&pmc->mcfr); |
| 35 | tmp |= AT91_PMC_MCFR_CCSS_XTAL_OSC; |
| 36 | tmp |= AT91_PMC_MCFR_RCMEAS; |
| 37 | writel(tmp, &pmc->mcfr); |
| 38 | |
| 39 | while (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINRDY)) |
| 40 | ; |
| 41 | |
| 42 | if (!(readl(&pmc->mcfr) & AT91_PMC_MCFR_MAINF_MASK)) |
| 43 | hang(); |
| 44 | #endif |
| 45 | |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 46 | tmp = readl(&pmc->mor); |
Eugen Hristev | fdd833e | 2019-05-24 09:38:10 +0300 | [diff] [blame^] | 47 | /* |
| 48 | * some boards have an external oscillator with driving. |
| 49 | * in this case we need to disable the internal SoC driving (bypass mode) |
| 50 | */ |
| 51 | #if defined(CONFIG_SPL_AT91_MCK_BYPASS) |
| 52 | tmp |= AT91_PMC_MOR_OSCBYPASS; |
| 53 | #else |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 54 | tmp &= ~AT91_PMC_MOR_OSCBYPASS; |
Eugen Hristev | fdd833e | 2019-05-24 09:38:10 +0300 | [diff] [blame^] | 55 | #endif |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 56 | tmp &= ~AT91_PMC_MOR_KEY(0xff); |
| 57 | tmp |= AT91_PMC_MOR_KEY(0x37); |
| 58 | writel(tmp, &pmc->mor); |
| 59 | |
| 60 | tmp = readl(&pmc->mor); |
| 61 | tmp |= AT91_PMC_MOR_MOSCSEL; |
| 62 | tmp &= ~AT91_PMC_MOR_KEY(0xff); |
| 63 | tmp |= AT91_PMC_MOR_KEY(0x37); |
| 64 | writel(tmp, &pmc->mor); |
| 65 | |
| 66 | while (!(readl(&pmc->sr) & AT91_PMC_IXR_MOSCSELS)) |
| 67 | ; |
| 68 | |
Wenyou Yang | f5fb545 | 2017-09-13 14:58:48 +0800 | [diff] [blame] | 69 | #if !defined(CONFIG_SAMA5D2) |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 70 | /* Wait until MAINRDY field is set to make sure main clock is stable */ |
| 71 | while (!(readl(&pmc->mcfr) & AT91_PMC_MAINRDY)) |
| 72 | ; |
Wenyou Yang | f5fb545 | 2017-09-13 14:58:48 +0800 | [diff] [blame] | 73 | #endif |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 74 | |
Wenyou Yang | f5fb545 | 2017-09-13 14:58:48 +0800 | [diff] [blame] | 75 | #if !defined(CONFIG_SAMA5D4) && !defined(CONFIG_SAMA5D2) |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 76 | tmp = readl(&pmc->mor); |
| 77 | tmp &= ~AT91_PMC_MOR_MOSCRCEN; |
| 78 | tmp &= ~AT91_PMC_MOR_KEY(0xff); |
| 79 | tmp |= AT91_PMC_MOR_KEY(0x37); |
| 80 | writel(tmp, &pmc->mor); |
Bo Shen | 73864b1 | 2014-12-15 13:24:32 +0800 | [diff] [blame] | 81 | #endif |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 82 | } |
| 83 | |
Bo Shen | 7dc2dbd | 2014-12-15 13:24:30 +0800 | [diff] [blame] | 84 | __weak void matrix_init(void) |
| 85 | { |
| 86 | /* This only be used for sama5d4 soc now */ |
| 87 | } |
| 88 | |
Bo Shen | 0a91028 | 2014-12-15 13:24:31 +0800 | [diff] [blame] | 89 | __weak void redirect_int_from_saic_to_aic(void) |
| 90 | { |
| 91 | /* This only be used for sama5d4 soc now */ |
| 92 | } |
| 93 | |
Tom Rini | d9eae55 | 2015-02-10 19:07:22 -0500 | [diff] [blame] | 94 | /* empty stub to satisfy current lowlevel_init, can be removed any time */ |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 95 | void s_init(void) |
| 96 | { |
Tom Rini | d9eae55 | 2015-02-10 19:07:22 -0500 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | void board_init_f(ulong dummy) |
| 100 | { |
Wenyou Yang | f13d0ff | 2017-03-24 11:34:04 +0800 | [diff] [blame] | 101 | int ret; |
| 102 | |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 103 | switch_to_main_crystal_osc(); |
| 104 | |
Samuel Mescoff | c3156fc | 2016-02-16 09:45:06 +0100 | [diff] [blame] | 105 | #ifdef CONFIG_SAMA5D2 |
| 106 | configure_2nd_sram_as_l2_cache(); |
| 107 | #endif |
| 108 | |
Prasanthi Chellakumar | 0509c4e | 2018-10-09 11:46:40 -0700 | [diff] [blame] | 109 | #if !defined(CONFIG_WDT_AT91) |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 110 | /* disable watchdog */ |
| 111 | at91_disable_wdt(); |
Tom Rini | 4a2b61b | 2018-05-10 07:15:52 -0400 | [diff] [blame] | 112 | #endif |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 113 | |
| 114 | /* PMC configuration */ |
| 115 | at91_pmc_init(); |
| 116 | |
| 117 | at91_clock_init(CONFIG_SYS_AT91_MAIN_CLOCK); |
| 118 | |
Bo Shen | 7dc2dbd | 2014-12-15 13:24:30 +0800 | [diff] [blame] | 119 | matrix_init(); |
| 120 | |
Bo Shen | 0a91028 | 2014-12-15 13:24:31 +0800 | [diff] [blame] | 121 | redirect_int_from_saic_to_aic(); |
| 122 | |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 123 | timer_init(); |
| 124 | |
| 125 | board_early_init_f(); |
| 126 | |
Wenyou Yang | ac8af4c | 2017-03-24 11:34:05 +0800 | [diff] [blame] | 127 | mem_init(); |
| 128 | |
Wenyou Yang | f13d0ff | 2017-03-24 11:34:04 +0800 | [diff] [blame] | 129 | ret = spl_init(); |
| 130 | if (ret) { |
| 131 | debug("spl_init() failed: %d\n", ret); |
| 132 | hang(); |
| 133 | } |
| 134 | |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 135 | preloader_console_init(); |
| 136 | |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 137 | } |