blob: dfba9f730c12a9a22efad578433e794bf41e4bd7 [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>
Simon Glass691d7192020-05-10 11:40:02 -060013#include <init.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060014#include <log.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Heiko Schocher5abc00d2014-10-31 08:31:04 +010016#include <asm/io.h>
17#include <asm/arch/at91_common.h>
18#include <asm/arch/at91sam9_matrix.h>
19#include <asm/arch/at91_pit.h>
Heiko Schocher5abc00d2014-10-31 08:31:04 +010020#include <asm/arch/at91_rstc.h>
21#include <asm/arch/at91_wdt.h>
22#include <asm/arch/clk.h>
23#include <spl.h>
24
25DECLARE_GLOBAL_DATA_PTR;
26
27static void enable_ext_reset(void)
28{
29 struct at91_rstc *rstc = (struct at91_rstc *)ATMEL_BASE_RSTC;
30
31 writel(AT91_RSTC_KEY | AT91_RSTC_MR_URSTEN, &rstc->mr);
32}
33
34void lowlevel_clock_init(void)
35{
36 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
37
38 if (!(readl(&pmc->sr) & AT91_PMC_MOSCS)) {
39 /* Enable Main Oscillator */
40 writel(AT91_PMC_MOSCS | (0x40 << 8), &pmc->mor);
41
42 /* Wait until Main Oscillator is stable */
43 while (!(readl(&pmc->sr) & AT91_PMC_MOSCS))
44 ;
45 }
46
47 /* After stabilization, switch to Main Oscillator */
48 if ((readl(&pmc->mckr) & AT91_PMC_CSS) == AT91_PMC_CSS_SLOW) {
49 unsigned long tmp;
50
51 tmp = readl(&pmc->mckr);
52 tmp &= ~AT91_PMC_CSS;
53 tmp |= AT91_PMC_CSS_MAIN;
54 writel(tmp, &pmc->mckr);
55 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
56 ;
57
58 tmp &= ~AT91_PMC_PRES;
59 tmp |= AT91_PMC_PRES_1;
60 writel(tmp, &pmc->mckr);
61 while (!(readl(&pmc->sr) & AT91_PMC_MCKRDY))
62 ;
63 }
64
65 return;
66}
67
68void __weak matrix_init(void)
69{
70}
71
72void __weak at91_spl_board_init(void)
73{
74}
75
Bo Shen41d41a92015-03-27 14:23:34 +080076void __weak spl_board_init(void)
77{
78}
79
80void board_init_f(ulong dummy)
Heiko Schocher5abc00d2014-10-31 08:31:04 +010081{
Stefan Roesece4d04a2019-04-02 10:57:16 +020082#if CONFIG_IS_ENABLED(OF_CONTROL)
83 int ret;
84
85 ret = spl_early_init();
86 if (ret) {
87 debug("spl_early_init() failed: %d\n", ret);
88 hang();
89 }
90#endif
91
Heiko Schocher5abc00d2014-10-31 08:31:04 +010092 lowlevel_clock_init();
Prasanthi Chellakumar1473f6a2018-10-09 11:46:40 -070093#if !defined(CONFIG_WDT_AT91)
Heiko Schocher5abc00d2014-10-31 08:31:04 +010094 at91_disable_wdt();
Tom Rinif58e9462018-05-10 07:15:52 -040095#endif
Heiko Schocher5abc00d2014-10-31 08:31:04 +010096
97 /*
98 * At this stage the main oscillator is supposed to be enabled
99 * PCK = MCK = MOSC
100 */
Wenyou Yangc43a72e2016-02-02 12:46:13 +0800101 at91_pllicpr_init(0x00);
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100102
103 /* Configure PLLA = MOSC * (PLL_MULA + 1) / PLL_DIVA */
Tom Rini65cc0e22022-11-16 13:10:41 -0500104 at91_plla_init(CFG_SYS_AT91_PLLA);
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100105
106 /* PCK = PLLA = 2 * MCK */
Tom Rini65cc0e22022-11-16 13:10:41 -0500107 at91_mck_init(CFG_SYS_MCKR);
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100108
109 /* Switch MCK on PLLA output */
Tom Rini65cc0e22022-11-16 13:10:41 -0500110 at91_mck_init(CFG_SYS_MCKR_CSS);
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100111
Tom Rini65cc0e22022-11-16 13:10:41 -0500112#if defined(CFG_SYS_AT91_PLLB)
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100113 /* Configure PLLB */
Tom Rini65cc0e22022-11-16 13:10:41 -0500114 at91_pllb_init(CFG_SYS_AT91_PLLB);
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100115#endif
116
117 /* Enable External Reset */
118 enable_ext_reset();
119
120 /* Initialize matrix */
121 matrix_init();
122
Tom Rini65cc0e22022-11-16 13:10:41 -0500123 gd->arch.mck_rate_hz = CFG_SYS_MASTER_CLOCK;
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100124 /*
125 * init timer long enough for using in spl.
126 */
127 timer_init();
128
129 /* enable clocks for all PIOs */
Bo Shenff255e82015-03-27 14:23:36 +0800130#if defined(CONFIG_AT91SAM9X5) || defined(CONFIG_AT91SAM9N12)
Bo Shend85e8912015-03-27 14:23:35 +0800131 at91_periph_clk_enable(ATMEL_ID_PIOAB);
132 at91_periph_clk_enable(ATMEL_ID_PIOCD);
133#else
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100134 at91_periph_clk_enable(ATMEL_ID_PIOA);
135 at91_periph_clk_enable(ATMEL_ID_PIOB);
136 at91_periph_clk_enable(ATMEL_ID_PIOC);
Bo Shend85e8912015-03-27 14:23:35 +0800137#endif
Heiko Schocher80402f32015-06-29 09:10:46 +0200138
Simon Glass2a736062021-08-08 12:20:12 -0600139#if defined(CONFIG_SPL_SERIAL)
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100140 /* init console */
141 at91_seriald_hw_init();
142 preloader_console_init();
Heiko Schocher80402f32015-06-29 09:10:46 +0200143#endif
Heiko Schocher5abc00d2014-10-31 08:31:04 +0100144
145 mem_init();
146
147 at91_spl_board_init();
148}