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