blob: 1065f090e0102d06686409ccf7d477524fa042af [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +01002/*
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 Schocherf1e3a8c2014-10-31 08:31:04 +01009 */
10
11#include <common.h>
12#include <asm/io.h>
13#include <asm/arch/at91_common.h>
14#include <asm/arch/at91sam9_matrix.h>
15#include <asm/arch/at91_pit.h>
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010016#include <asm/arch/at91_rstc.h>
17#include <asm/arch/at91_wdt.h>
18#include <asm/arch/clk.h>
19#include <spl.h>
20
21DECLARE_GLOBAL_DATA_PTR;
22
23static void enable_ext_reset(void)
24{
25 struct at91_rstc *rstc = (struct at91_rstc *)ATMEL_BASE_RSTC;
26
27 writel(AT91_RSTC_KEY | AT91_RSTC_MR_URSTEN, &rstc->mr);
28}
29
30void lowlevel_clock_init(void)
31{
32 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
33
34 if (!(readl(&pmc->sr) & AT91_PMC_MOSCS)) {
35 /* Enable Main Oscillator */
36 writel(AT91_PMC_MOSCS | (0x40 << 8), &pmc->mor);
37
38 /* Wait until Main Oscillator is stable */
39 while (!(readl(&pmc->sr) & AT91_PMC_MOSCS))
40 ;
41 }
42
43 /* After stabilization, switch to Main Oscillator */
44 if ((readl(&pmc->mckr) & AT91_PMC_CSS) == AT91_PMC_CSS_SLOW) {
45 unsigned long tmp;
46
47 tmp = readl(&pmc->mckr);
48 tmp &= ~AT91_PMC_CSS;
49 tmp |= AT91_PMC_CSS_MAIN;
50 writel(tmp, &pmc->mckr);
51 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
52 ;
53
54 tmp &= ~AT91_PMC_PRES;
55 tmp |= AT91_PMC_PRES_1;
56 writel(tmp, &pmc->mckr);
57 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
58 ;
59 }
60
61 return;
62}
63
64void __weak matrix_init(void)
65{
66}
67
68void __weak at91_spl_board_init(void)
69{
70}
71
Bo Shenc56e9f42015-03-27 14:23:34 +080072void __weak spl_board_init(void)
73{
74}
75
76void board_init_f(ulong dummy)
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010077{
Stefan Roese7937d032019-04-02 10:57:16 +020078#if CONFIG_IS_ENABLED(OF_CONTROL)
79 int ret;
80
81 ret = spl_early_init();
82 if (ret) {
83 debug("spl_early_init() failed: %d\n", ret);
84 hang();
85 }
86#endif
87
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010088 lowlevel_clock_init();
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070089#if !defined(CONFIG_WDT_AT91)
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010090 at91_disable_wdt();
Tom Rini4a2b61b2018-05-10 07:15:52 -040091#endif
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010092
93 /*
94 * At this stage the main oscillator is supposed to be enabled
95 * PCK = MCK = MOSC
96 */
Wenyou Yang747e9db2016-02-02 12:46:13 +080097 at91_pllicpr_init(0x00);
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010098
99 /* Configure PLLA = MOSC * (PLL_MULA + 1) / PLL_DIVA */
100 at91_plla_init(CONFIG_SYS_AT91_PLLA);
101
102 /* PCK = PLLA = 2 * MCK */
103 at91_mck_init(CONFIG_SYS_MCKR);
104
105 /* Switch MCK on PLLA output */
106 at91_mck_init(CONFIG_SYS_MCKR_CSS);
107
108#if defined(CONFIG_SYS_AT91_PLLB)
109 /* Configure PLLB */
110 at91_pllb_init(CONFIG_SYS_AT91_PLLB);
111#endif
112
113 /* Enable External Reset */
114 enable_ext_reset();
115
116 /* Initialize matrix */
117 matrix_init();
118
119 gd->arch.mck_rate_hz = CONFIG_SYS_MASTER_CLOCK;
120 /*
121 * init timer long enough for using in spl.
122 */
123 timer_init();
124
125 /* enable clocks for all PIOs */
Bo Shen9c709392015-03-27 14:23:36 +0800126#if defined(CONFIG_AT91SAM9X5) || defined(CONFIG_AT91SAM9N12)
Bo Shen9a3b1fe2015-03-27 14:23:35 +0800127 at91_periph_clk_enable(ATMEL_ID_PIOAB);
128 at91_periph_clk_enable(ATMEL_ID_PIOCD);
129#else
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100130 at91_periph_clk_enable(ATMEL_ID_PIOA);
131 at91_periph_clk_enable(ATMEL_ID_PIOB);
132 at91_periph_clk_enable(ATMEL_ID_PIOC);
Bo Shen9a3b1fe2015-03-27 14:23:35 +0800133#endif
Heiko Schocher62cb1562015-06-29 09:10:46 +0200134
135#if defined(CONFIG_SPL_SERIAL_SUPPORT)
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100136 /* init console */
137 at91_seriald_hw_init();
138 preloader_console_init();
Heiko Schocher62cb1562015-06-29 09:10:46 +0200139#endif
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100140
141 mem_init();
142
143 at91_spl_board_init();
144}