blob: 84c561be6fc31606300d8ac0b132aac35d2bf72c [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Bo Shen06ce3f42014-02-09 15:52:39 +08002/*
3 * Copyright (C) 2014 Atmel Corporation
4 * Bo Shen <voice.shen@atmel.com>
Bo Shen06ce3f42014-02-09 15:52:39 +08005 */
6
7#include <common.h>
Simon Glassa7b51302019-11-14 12:57:46 -07008#include <init.h>
Bo Shen06ce3f42014-02-09 15:52:39 +08009#include <asm/io.h>
10#include <asm/arch/sama5d3_smc.h>
11#include <asm/arch/at91_common.h>
Bo Shen06ce3f42014-02-09 15:52:39 +080012#include <asm/arch/at91_rstc.h>
13#include <asm/arch/gpio.h>
14#include <asm/arch/clk.h>
Wenyou Yange46dd152017-04-14 08:51:47 +080015#include <debug_uart.h>
Bo Shen735ef1a2014-03-19 14:48:45 +080016#include <spl.h>
17#include <asm/arch/atmel_mpddrc.h>
18#include <asm/arch/at91_wdt.h>
Bo Shen06ce3f42014-02-09 15:52:39 +080019
20DECLARE_GLOBAL_DATA_PTR;
21
Eugen Hristevbe01f772018-09-18 10:35:44 +030022extern void at91_pda_detect(void);
23
Bo Shen06ce3f42014-02-09 15:52:39 +080024#ifdef CONFIG_NAND_ATMEL
25void sama5d3_xplained_nand_hw_init(void)
26{
27 struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
28
29 at91_periph_clk_enable(ATMEL_ID_SMC);
30
31 /* Configure SMC CS3 for NAND/SmartMedia */
32 writel(AT91_SMC_SETUP_NWE(2) | AT91_SMC_SETUP_NCS_WR(1) |
33 AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(1),
34 &smc->cs[3].setup);
35 writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(5) |
36 AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(5),
37 &smc->cs[3].pulse);
38 writel(AT91_SMC_CYCLE_NWE(8) | AT91_SMC_CYCLE_NRD(8),
39 &smc->cs[3].cycle);
40 writel(AT91_SMC_TIMINGS_TCLR(3) | AT91_SMC_TIMINGS_TADL(10) |
41 AT91_SMC_TIMINGS_TAR(3) | AT91_SMC_TIMINGS_TRR(4) |
42 AT91_SMC_TIMINGS_TWB(5) | AT91_SMC_TIMINGS_RBNSEL(3)|
43 AT91_SMC_TIMINGS_NFSEL(1), &smc->cs[3].timings);
44 writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
45 AT91_SMC_MODE_EXNW_DISABLE |
46#ifdef CONFIG_SYS_NAND_DBW_16
47 AT91_SMC_MODE_DBW_16 |
48#else /* CONFIG_SYS_NAND_DBW_8 */
49 AT91_SMC_MODE_DBW_8 |
50#endif
51 AT91_SMC_MODE_TDF_CYCLE(3),
52 &smc->cs[3].mode);
53}
54#endif
55
56#ifdef CONFIG_CMD_USB
57static void sama5d3_xplained_usb_hw_init(void)
58{
59 at91_set_pio_output(AT91_PIO_PORTE, 3, 0);
60 at91_set_pio_output(AT91_PIO_PORTE, 4, 0);
61}
62#endif
63
64#ifdef CONFIG_GENERIC_ATMEL_MCI
65static void sama5d3_xplained_mci0_hw_init(void)
66{
Bo Shen06ce3f42014-02-09 15:52:39 +080067 at91_set_pio_output(AT91_PIO_PORTE, 2, 0); /* MCI0 Power */
68}
69#endif
70
Wenyou Yange46dd152017-04-14 08:51:47 +080071#ifdef CONFIG_DEBUG_UART_BOARD_INIT
72void board_debug_uart_init(void)
Bo Shen06ce3f42014-02-09 15:52:39 +080073{
Bo Shen06ce3f42014-02-09 15:52:39 +080074 at91_seriald_hw_init();
Wenyou Yange46dd152017-04-14 08:51:47 +080075}
76#endif
Bo Shen06ce3f42014-02-09 15:52:39 +080077
Eugen Hristevbe01f772018-09-18 10:35:44 +030078#ifdef CONFIG_BOARD_LATE_INIT
79int board_late_init(void)
80{
81 at91_pda_detect();
82 return 0;
83}
84#endif
85
Wenyou Yange46dd152017-04-14 08:51:47 +080086#ifdef CONFIG_BOARD_EARLY_INIT_F
87int board_early_init_f(void)
88{
89#ifdef CONFIG_DEBUG_UART
90 debug_uart_init();
91#endif
Bo Shen06ce3f42014-02-09 15:52:39 +080092 return 0;
93}
Wenyou Yange46dd152017-04-14 08:51:47 +080094#endif
Bo Shen06ce3f42014-02-09 15:52:39 +080095
96int board_init(void)
97{
98 /* adress of boot parameters */
99 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
100
101#ifdef CONFIG_NAND_ATMEL
102 sama5d3_xplained_nand_hw_init();
103#endif
104#ifdef CONFIG_CMD_USB
105 sama5d3_xplained_usb_hw_init();
106#endif
107#ifdef CONFIG_GENERIC_ATMEL_MCI
108 sama5d3_xplained_mci0_hw_init();
109#endif
Bo Shen06ce3f42014-02-09 15:52:39 +0800110 return 0;
111}
112
113int dram_init(void)
114{
115 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
116 CONFIG_SYS_SDRAM_SIZE);
117
Bo Shen06ce3f42014-02-09 15:52:39 +0800118 return 0;
119}
120
Bo Shen735ef1a2014-03-19 14:48:45 +0800121/* SPL */
122#ifdef CONFIG_SPL_BUILD
123void spl_board_init(void)
124{
Wenyou Yange035ea72017-09-14 11:07:44 +0800125#ifdef CONFIG_SD_BOOT
Wenyou Yang9ddd6fc2017-04-14 08:51:45 +0800126#ifdef CONFIG_GENERIC_ATMEL_MCI
Bo Shen735ef1a2014-03-19 14:48:45 +0800127 sama5d3_xplained_mci0_hw_init();
Wenyou Yang9ddd6fc2017-04-14 08:51:45 +0800128#endif
Wenyou Yange035ea72017-09-14 11:07:44 +0800129#elif CONFIG_NAND_BOOT
Bo Shen735ef1a2014-03-19 14:48:45 +0800130 sama5d3_xplained_nand_hw_init();
131#endif
132}
133
Wenyou Yangaa0a58d2016-02-01 18:12:15 +0800134static void ddr2_conf(struct atmel_mpddrc_config *ddr2)
Bo Shen735ef1a2014-03-19 14:48:45 +0800135{
136 ddr2->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR2_SDRAM);
137
138 ddr2->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
139 ATMEL_MPDDRC_CR_NR_ROW_14 |
140 ATMEL_MPDDRC_CR_CAS_DDR_CAS3 |
141 ATMEL_MPDDRC_CR_ENRDM_ON |
142 ATMEL_MPDDRC_CR_NB_8BANKS |
143 ATMEL_MPDDRC_CR_NDQS_DISABLED |
144 ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
145 ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
146 /*
147 * As the DDR2-SDRAm device requires a refresh time is 7.8125us
148 * when DDR run at 133MHz, so it needs (7.8125us * 133MHz / 10^9) clocks
149 */
150 ddr2->rtr = 0x411;
151
152 ddr2->tpr0 = (6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET |
153 2 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET |
154 2 << ATMEL_MPDDRC_TPR0_TWR_OFFSET |
155 8 << ATMEL_MPDDRC_TPR0_TRC_OFFSET |
156 2 << ATMEL_MPDDRC_TPR0_TRP_OFFSET |
157 2 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET |
158 2 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET |
159 2 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET);
160
161 ddr2->tpr1 = (2 << ATMEL_MPDDRC_TPR1_TXP_OFFSET |
162 200 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET |
163 28 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET |
164 26 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET);
165
166 ddr2->tpr2 = (7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET |
167 2 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET |
168 2 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET |
169 7 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET |
170 8 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET);
171}
172
173void mem_init(void)
174{
Wenyou Yangaa0a58d2016-02-01 18:12:15 +0800175 struct atmel_mpddrc_config ddr2;
Bo Shen735ef1a2014-03-19 14:48:45 +0800176
177 ddr2_conf(&ddr2);
178
Wenyou Yang78f89762016-02-03 10:16:50 +0800179 /* Enable MPDDR clock */
Bo Shen735ef1a2014-03-19 14:48:45 +0800180 at91_periph_clk_enable(ATMEL_ID_MPDDRC);
Wenyou Yang78f89762016-02-03 10:16:50 +0800181 at91_system_clk_enable(AT91_PMC_DDR);
Bo Shen735ef1a2014-03-19 14:48:45 +0800182
183 /* DDRAM2 Controller initialize */
Erik van Luijk59d780a2015-08-13 15:43:18 +0200184 ddr2_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddr2);
Bo Shen735ef1a2014-03-19 14:48:45 +0800185}
186
187void at91_pmc_init(void)
188{
Bo Shen735ef1a2014-03-19 14:48:45 +0800189 u32 tmp;
190
191 tmp = AT91_PMC_PLLAR_29 |
192 AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
193 AT91_PMC_PLLXR_MUL(43) |
194 AT91_PMC_PLLXR_DIV(1);
195 at91_plla_init(tmp);
196
Wenyou Yang5265b1e2016-02-02 12:46:14 +0800197 at91_pllicpr_init(AT91_PMC_IPLL_PLLA(0x3));
Bo Shen735ef1a2014-03-19 14:48:45 +0800198
199 tmp = AT91_PMC_MCKR_MDIV_4 |
200 AT91_PMC_MCKR_CSS_PLLA;
201 at91_mck_init(tmp);
202}
203#endif