blob: 822f877de717a00633d8afb945561080452243b0 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Heiko Schocher5abc00d2014-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 Schocher5abc00d2014-10-31 08:31:04 +01009 */
10
11#include <common.h>
Simon Glassdb41d652019-12-28 10:45:07 -070012#include <hang.h>
Heiko Schocher5abc00d2014-10-31 08:31:04 +010013#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 Schocher5abc00d2014-10-31 08:31:04 +010017#include <asm/arch/at91_rstc.h>
18#include <asm/arch/at91_wdt.h>
19#include <asm/arch/clk.h>
20#include <spl.h>
21
22DECLARE_GLOBAL_DATA_PTR;
23
24static 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
31void 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
65void __weak matrix_init(void)
66{
67}
68
69void __weak at91_spl_board_init(void)
70{
71}
72
Bo Shen41d41a92015-03-27 14:23:34 +080073void __weak spl_board_init(void)
74{
75}
76
77void board_init_f(ulong dummy)
Heiko Schocher5abc00d2014-10-31 08:31:04 +010078{
Stefan Roesece4d04a2019-04-02 10:57:16 +020079#if CONFIG_IS_ENABLED(OF_CONTROL)
80 int ret;
81
82 ret = spl_early_init();
83 if (ret) {
84 debug("spl_early_init() failed: %d\n", ret);
85 hang();
86 }
87#endif
88
Heiko Schocher5abc00d2014-10-31 08:31:04 +010089 lowlevel_clock_init();
Prasanthi Chellakumar1473f6a2018-10-09 11:46:40 -070090#if !defined(CONFIG_WDT_AT91)
Heiko Schocher5abc00d2014-10-31 08:31:04 +010091 at91_disable_wdt();
Tom Rinif58e9462018-05-10 07:15:52 -040092#endif
Heiko Schocher5abc00d2014-10-31 08:31:04 +010093
94 /*
95 * At this stage the main oscillator is supposed to be enabled
96 * PCK = MCK = MOSC
97 */
Wenyou Yangc43a72e2016-02-02 12:46:13 +080098 at91_pllicpr_init(0x00);
Heiko Schocher5abc00d2014-10-31 08:31:04 +010099
100 /* Configure PLLA = MOSC * (PLL_MULA + 1) / PLL_DIVA */
101 at91_plla_init(CONFIG_SYS_AT91_PLLA);
102
103 /* PCK = PLLA = 2 * MCK */
104 at91_mck_init(CONFIG_SYS_MCKR);
105
106 /* Switch MCK on PLLA output */
107 at91_mck_init(CONFIG_SYS_MCKR_CSS);
108
109#if defined(CONFIG_SYS_AT91_PLLB)
110 /* Configure PLLB */
111 at91_pllb_init(CONFIG_SYS_AT91_PLLB);
112#endif
113
114 /* Enable External Reset */
115 enable_ext_reset();
116
117 /* Initialize matrix */
118 matrix_init();
119
120 gd->arch.mck_rate_hz = CONFIG_SYS_MASTER_CLOCK;
121 /*
122 * init timer long enough for using in spl.
123 */
124 timer_init();
125
126 /* enable clocks for all PIOs */
Bo Shenff255e82015-03-27 14:23:36 +0800127#if defined(CONFIG_AT91SAM9X5) || defined(CONFIG_AT91SAM9N12)
Bo Shend85e8912015-03-27 14:23:35 +0800128 at91_periph_clk_enable(ATMEL_ID_PIOAB);
129 at91_periph_clk_enable(ATMEL_ID_PIOCD);
130#else
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100131 at91_periph_clk_enable(ATMEL_ID_PIOA);
132 at91_periph_clk_enable(ATMEL_ID_PIOB);
133 at91_periph_clk_enable(ATMEL_ID_PIOC);
Bo Shend85e8912015-03-27 14:23:35 +0800134#endif
Heiko Schocher80402f32015-06-29 09:10:46 +0200135
136#if defined(CONFIG_SPL_SERIAL_SUPPORT)
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100137 /* init console */
138 at91_seriald_hw_init();
139 preloader_console_init();
Heiko Schocher80402f32015-06-29 09:10:46 +0200140#endif
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100141
142 mem_init();
143
144 at91_spl_board_init();
145}