Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2014 DENX Software Engineering |
| 3 | * Heiko Schocher <hs@denx.de> |
| 4 | * |
| 5 | * Based on: |
| 6 | * Copyright (C) 2013 Atmel Corporation |
| 7 | * Bo Shen <voice.shen@atmel.com> |
| 8 | * |
| 9 | * SPDX-License-Identifier: GPL-2.0+ |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <asm/io.h> |
| 14 | #include <asm/arch/at91_common.h> |
| 15 | #include <asm/arch/at91sam9_matrix.h> |
| 16 | #include <asm/arch/at91_pit.h> |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 17 | #include <asm/arch/at91_rstc.h> |
| 18 | #include <asm/arch/at91_wdt.h> |
| 19 | #include <asm/arch/clk.h> |
| 20 | #include <spl.h> |
| 21 | |
| 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
| 24 | static void enable_ext_reset(void) |
| 25 | { |
| 26 | struct at91_rstc *rstc = (struct at91_rstc *)ATMEL_BASE_RSTC; |
| 27 | |
| 28 | writel(AT91_RSTC_KEY | AT91_RSTC_MR_URSTEN, &rstc->mr); |
| 29 | } |
| 30 | |
| 31 | void lowlevel_clock_init(void) |
| 32 | { |
| 33 | struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; |
| 34 | |
| 35 | if (!(readl(&pmc->sr) & AT91_PMC_MOSCS)) { |
| 36 | /* Enable Main Oscillator */ |
| 37 | writel(AT91_PMC_MOSCS | (0x40 << 8), &pmc->mor); |
| 38 | |
| 39 | /* Wait until Main Oscillator is stable */ |
| 40 | while (!(readl(&pmc->sr) & AT91_PMC_MOSCS)) |
| 41 | ; |
| 42 | } |
| 43 | |
| 44 | /* After stabilization, switch to Main Oscillator */ |
| 45 | if ((readl(&pmc->mckr) & AT91_PMC_CSS) == AT91_PMC_CSS_SLOW) { |
| 46 | unsigned long tmp; |
| 47 | |
| 48 | tmp = readl(&pmc->mckr); |
| 49 | tmp &= ~AT91_PMC_CSS; |
| 50 | tmp |= AT91_PMC_CSS_MAIN; |
| 51 | writel(tmp, &pmc->mckr); |
| 52 | while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY)) |
| 53 | ; |
| 54 | |
| 55 | tmp &= ~AT91_PMC_PRES; |
| 56 | tmp |= AT91_PMC_PRES_1; |
| 57 | writel(tmp, &pmc->mckr); |
| 58 | while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY)) |
| 59 | ; |
| 60 | } |
| 61 | |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | void __weak matrix_init(void) |
| 66 | { |
| 67 | } |
| 68 | |
| 69 | void __weak at91_spl_board_init(void) |
| 70 | { |
| 71 | } |
| 72 | |
Bo Shen | c56e9f4 | 2015-03-27 14:23:34 +0800 | [diff] [blame] | 73 | void __weak spl_board_init(void) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | void board_init_f(ulong dummy) |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 78 | { |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 79 | lowlevel_clock_init(); |
| 80 | at91_disable_wdt(); |
| 81 | |
| 82 | /* |
| 83 | * At this stage the main oscillator is supposed to be enabled |
| 84 | * PCK = MCK = MOSC |
| 85 | */ |
Wenyou Yang | 747e9db | 2016-02-02 12:46:13 +0800 | [diff] [blame] | 86 | at91_pllicpr_init(0x00); |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 87 | |
| 88 | /* Configure PLLA = MOSC * (PLL_MULA + 1) / PLL_DIVA */ |
| 89 | at91_plla_init(CONFIG_SYS_AT91_PLLA); |
| 90 | |
| 91 | /* PCK = PLLA = 2 * MCK */ |
| 92 | at91_mck_init(CONFIG_SYS_MCKR); |
| 93 | |
| 94 | /* Switch MCK on PLLA output */ |
| 95 | at91_mck_init(CONFIG_SYS_MCKR_CSS); |
| 96 | |
| 97 | #if defined(CONFIG_SYS_AT91_PLLB) |
| 98 | /* Configure PLLB */ |
| 99 | at91_pllb_init(CONFIG_SYS_AT91_PLLB); |
| 100 | #endif |
| 101 | |
| 102 | /* Enable External Reset */ |
| 103 | enable_ext_reset(); |
| 104 | |
| 105 | /* Initialize matrix */ |
| 106 | matrix_init(); |
| 107 | |
| 108 | gd->arch.mck_rate_hz = CONFIG_SYS_MASTER_CLOCK; |
| 109 | /* |
| 110 | * init timer long enough for using in spl. |
| 111 | */ |
| 112 | timer_init(); |
| 113 | |
| 114 | /* enable clocks for all PIOs */ |
Bo Shen | 9c70939 | 2015-03-27 14:23:36 +0800 | [diff] [blame] | 115 | #if defined(CONFIG_AT91SAM9X5) || defined(CONFIG_AT91SAM9N12) |
Bo Shen | 9a3b1fe | 2015-03-27 14:23:35 +0800 | [diff] [blame] | 116 | at91_periph_clk_enable(ATMEL_ID_PIOAB); |
| 117 | at91_periph_clk_enable(ATMEL_ID_PIOCD); |
| 118 | #else |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 119 | at91_periph_clk_enable(ATMEL_ID_PIOA); |
| 120 | at91_periph_clk_enable(ATMEL_ID_PIOB); |
| 121 | at91_periph_clk_enable(ATMEL_ID_PIOC); |
Bo Shen | 9a3b1fe | 2015-03-27 14:23:35 +0800 | [diff] [blame] | 122 | #endif |
Heiko Schocher | 62cb156 | 2015-06-29 09:10:46 +0200 | [diff] [blame] | 123 | |
| 124 | #if defined(CONFIG_SPL_SERIAL_SUPPORT) |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 125 | /* init console */ |
| 126 | at91_seriald_hw_init(); |
| 127 | preloader_console_init(); |
Heiko Schocher | 62cb156 | 2015-06-29 09:10:46 +0200 | [diff] [blame] | 128 | #endif |
Heiko Schocher | f1e3a8c | 2014-10-31 08:31:04 +0100 | [diff] [blame] | 129 | |
| 130 | mem_init(); |
| 131 | |
| 132 | at91_spl_board_init(); |
| 133 | } |