blob: b78cafdd9cb6f4edb4d8431b97f417ad406026cd [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>
Simon Glassf11478f2019-12-28 10:45:07 -070012#include <hang.h>
Simon Glass97589732020-05-10 11:40:02 -060013#include <init.h>
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010014#include <asm/io.h>
15#include <asm/arch/at91_common.h>
16#include <asm/arch/at91sam9_matrix.h>
17#include <asm/arch/at91_pit.h>
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010018#include <asm/arch/at91_rstc.h>
19#include <asm/arch/at91_wdt.h>
20#include <asm/arch/clk.h>
21#include <spl.h>
22
23DECLARE_GLOBAL_DATA_PTR;
24
25static void enable_ext_reset(void)
26{
27 struct at91_rstc *rstc = (struct at91_rstc *)ATMEL_BASE_RSTC;
28
29 writel(AT91_RSTC_KEY | AT91_RSTC_MR_URSTEN, &rstc->mr);
30}
31
32void lowlevel_clock_init(void)
33{
34 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
35
36 if (!(readl(&pmc->sr) & AT91_PMC_MOSCS)) {
37 /* Enable Main Oscillator */
38 writel(AT91_PMC_MOSCS | (0x40 << 8), &pmc->mor);
39
40 /* Wait until Main Oscillator is stable */
41 while (!(readl(&pmc->sr) & AT91_PMC_MOSCS))
42 ;
43 }
44
45 /* After stabilization, switch to Main Oscillator */
46 if ((readl(&pmc->mckr) & AT91_PMC_CSS) == AT91_PMC_CSS_SLOW) {
47 unsigned long tmp;
48
49 tmp = readl(&pmc->mckr);
50 tmp &= ~AT91_PMC_CSS;
51 tmp |= AT91_PMC_CSS_MAIN;
52 writel(tmp, &pmc->mckr);
53 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
54 ;
55
56 tmp &= ~AT91_PMC_PRES;
57 tmp |= AT91_PMC_PRES_1;
58 writel(tmp, &pmc->mckr);
59 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
60 ;
61 }
62
63 return;
64}
65
66void __weak matrix_init(void)
67{
68}
69
70void __weak at91_spl_board_init(void)
71{
72}
73
Bo Shenc56e9f42015-03-27 14:23:34 +080074void __weak spl_board_init(void)
75{
76}
77
78void board_init_f(ulong dummy)
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010079{
Stefan Roese7937d032019-04-02 10:57:16 +020080#if CONFIG_IS_ENABLED(OF_CONTROL)
81 int ret;
82
83 ret = spl_early_init();
84 if (ret) {
85 debug("spl_early_init() failed: %d\n", ret);
86 hang();
87 }
88#endif
89
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010090 lowlevel_clock_init();
Prasanthi Chellakumar0509c4e2018-10-09 11:46:40 -070091#if !defined(CONFIG_WDT_AT91)
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010092 at91_disable_wdt();
Tom Rini4a2b61b2018-05-10 07:15:52 -040093#endif
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +010094
95 /*
96 * At this stage the main oscillator is supposed to be enabled
97 * PCK = MCK = MOSC
98 */
Wenyou Yang747e9db2016-02-02 12:46:13 +080099 at91_pllicpr_init(0x00);
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100100
101 /* Configure PLLA = MOSC * (PLL_MULA + 1) / PLL_DIVA */
102 at91_plla_init(CONFIG_SYS_AT91_PLLA);
103
104 /* PCK = PLLA = 2 * MCK */
105 at91_mck_init(CONFIG_SYS_MCKR);
106
107 /* Switch MCK on PLLA output */
108 at91_mck_init(CONFIG_SYS_MCKR_CSS);
109
110#if defined(CONFIG_SYS_AT91_PLLB)
111 /* Configure PLLB */
112 at91_pllb_init(CONFIG_SYS_AT91_PLLB);
113#endif
114
115 /* Enable External Reset */
116 enable_ext_reset();
117
118 /* Initialize matrix */
119 matrix_init();
120
121 gd->arch.mck_rate_hz = CONFIG_SYS_MASTER_CLOCK;
122 /*
123 * init timer long enough for using in spl.
124 */
125 timer_init();
126
127 /* enable clocks for all PIOs */
Bo Shen9c709392015-03-27 14:23:36 +0800128#if defined(CONFIG_AT91SAM9X5) || defined(CONFIG_AT91SAM9N12)
Bo Shen9a3b1fe2015-03-27 14:23:35 +0800129 at91_periph_clk_enable(ATMEL_ID_PIOAB);
130 at91_periph_clk_enable(ATMEL_ID_PIOCD);
131#else
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100132 at91_periph_clk_enable(ATMEL_ID_PIOA);
133 at91_periph_clk_enable(ATMEL_ID_PIOB);
134 at91_periph_clk_enable(ATMEL_ID_PIOC);
Bo Shen9a3b1fe2015-03-27 14:23:35 +0800135#endif
Heiko Schocher62cb1562015-06-29 09:10:46 +0200136
137#if defined(CONFIG_SPL_SERIAL_SUPPORT)
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100138 /* init console */
139 at91_seriald_hw_init();
140 preloader_console_init();
Heiko Schocher62cb1562015-06-29 09:10:46 +0200141#endif
Heiko Schocherf1e3a8c2014-10-31 08:31:04 +0100142
143 mem_init();
144
145 at91_spl_board_init();
146}