blob: d0c732539248c066254e9ef30fc58b6284fdeaaf [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 */
104 at91_plla_init(CONFIG_SYS_AT91_PLLA);
105
106 /* PCK = PLLA = 2 * MCK */
107 at91_mck_init(CONFIG_SYS_MCKR);
108
109 /* Switch MCK on PLLA output */
110 at91_mck_init(CONFIG_SYS_MCKR_CSS);
111
112#if defined(CONFIG_SYS_AT91_PLLB)
113 /* Configure PLLB */
114 at91_pllb_init(CONFIG_SYS_AT91_PLLB);
115#endif
116
117 /* Enable External Reset */
118 enable_ext_reset();
119
120 /* Initialize matrix */
121 matrix_init();
122
123 gd->arch.mck_rate_hz = CONFIG_SYS_MASTER_CLOCK;
124 /*
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
139#if defined(CONFIG_SPL_SERIAL_SUPPORT)
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}