3aa46b1774a3c8352602d0a54925d57b1ae0fbf5
[oweals/u-boot.git] / board / atmel / sama5d3xek / sama5d3xek.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2012 - 2013 Atmel Corporation
4  * Bo Shen <voice.shen@atmel.com>
5  */
6
7 #include <common.h>
8 #include <init.h>
9 #include <asm/io.h>
10 #include <asm/arch/sama5d3_smc.h>
11 #include <asm/arch/at91_common.h>
12 #include <asm/arch/at91_rstc.h>
13 #include <asm/arch/gpio.h>
14 #include <asm/arch/clk.h>
15 #include <debug_uart.h>
16 #include <env.h>
17 #include <linux/ctype.h>
18 #include <phy.h>
19 #include <micrel.h>
20 #include <spl.h>
21 #include <asm/arch/atmel_mpddrc.h>
22 #include <asm/arch/at91_wdt.h>
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 /* ------------------------------------------------------------------------- */
27 /*
28  * Miscelaneous platform dependent initialisations
29  */
30
31 #ifdef CONFIG_NAND_ATMEL
32 void sama5d3xek_nand_hw_init(void)
33 {
34         struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
35
36         at91_periph_clk_enable(ATMEL_ID_SMC);
37
38         /* Configure SMC CS3 for NAND/SmartMedia */
39         writel(AT91_SMC_SETUP_NWE(2) | AT91_SMC_SETUP_NCS_WR(1) |
40                AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(1),
41                &smc->cs[3].setup);
42         writel(AT91_SMC_PULSE_NWE(3) | AT91_SMC_PULSE_NCS_WR(5) |
43                AT91_SMC_PULSE_NRD(3) | AT91_SMC_PULSE_NCS_RD(5),
44                &smc->cs[3].pulse);
45         writel(AT91_SMC_CYCLE_NWE(8) | AT91_SMC_CYCLE_NRD(8),
46                &smc->cs[3].cycle);
47         writel(AT91_SMC_TIMINGS_TCLR(3) | AT91_SMC_TIMINGS_TADL(10) |
48                AT91_SMC_TIMINGS_TAR(3)  | AT91_SMC_TIMINGS_TRR(4)   |
49                AT91_SMC_TIMINGS_TWB(5)  | AT91_SMC_TIMINGS_RBNSEL(3)|
50                AT91_SMC_TIMINGS_NFSEL(1), &smc->cs[3].timings);
51         writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
52                AT91_SMC_MODE_EXNW_DISABLE |
53 #ifdef CONFIG_SYS_NAND_DBW_16
54                AT91_SMC_MODE_DBW_16 |
55 #else /* CONFIG_SYS_NAND_DBW_8 */
56                AT91_SMC_MODE_DBW_8 |
57 #endif
58                AT91_SMC_MODE_TDF_CYCLE(3),
59                &smc->cs[3].mode);
60 }
61 #endif
62
63 #ifdef CONFIG_MTD_NOR_FLASH
64 static void sama5d3xek_nor_hw_init(void)
65 {
66         struct at91_smc *smc = (struct at91_smc *)ATMEL_BASE_SMC;
67
68         at91_periph_clk_enable(ATMEL_ID_SMC);
69
70         /* Configure SMC CS0 for NOR flash */
71         writel(AT91_SMC_SETUP_NWE(1) | AT91_SMC_SETUP_NCS_WR(0) |
72                AT91_SMC_SETUP_NRD(2) | AT91_SMC_SETUP_NCS_RD(0),
73                &smc->cs[0].setup);
74         writel(AT91_SMC_PULSE_NWE(10) | AT91_SMC_PULSE_NCS_WR(11) |
75                AT91_SMC_PULSE_NRD(10) | AT91_SMC_PULSE_NCS_RD(11),
76                &smc->cs[0].pulse);
77         writel(AT91_SMC_CYCLE_NWE(11) | AT91_SMC_CYCLE_NRD(14),
78                &smc->cs[0].cycle);
79         writel(AT91_SMC_TIMINGS_TCLR(0) | AT91_SMC_TIMINGS_TADL(0)  |
80                AT91_SMC_TIMINGS_TAR(0)  | AT91_SMC_TIMINGS_TRR(0)   |
81                AT91_SMC_TIMINGS_TWB(0)  | AT91_SMC_TIMINGS_RBNSEL(0)|
82                AT91_SMC_TIMINGS_NFSEL(0), &smc->cs[0].timings);
83         writel(AT91_SMC_MODE_RM_NRD | AT91_SMC_MODE_WM_NWE |
84                AT91_SMC_MODE_EXNW_DISABLE |
85                AT91_SMC_MODE_DBW_16 |
86                AT91_SMC_MODE_TDF_CYCLE(1),
87                &smc->cs[0].mode);
88
89         /* Address pin (A1 ~ A23) configuration */
90         at91_pio3_set_a_periph(AT91_PIO_PORTE, 1, 0);
91         at91_pio3_set_a_periph(AT91_PIO_PORTE, 2, 0);
92         at91_pio3_set_a_periph(AT91_PIO_PORTE, 3, 0);
93         at91_pio3_set_a_periph(AT91_PIO_PORTE, 4, 0);
94         at91_pio3_set_a_periph(AT91_PIO_PORTE, 5, 0);
95         at91_pio3_set_a_periph(AT91_PIO_PORTE, 6, 0);
96         at91_pio3_set_a_periph(AT91_PIO_PORTE, 7, 0);
97         at91_pio3_set_a_periph(AT91_PIO_PORTE, 8, 0);
98         at91_pio3_set_a_periph(AT91_PIO_PORTE, 9, 0);
99         at91_pio3_set_a_periph(AT91_PIO_PORTE, 10, 0);
100         at91_pio3_set_a_periph(AT91_PIO_PORTE, 11, 0);
101         at91_pio3_set_a_periph(AT91_PIO_PORTE, 12, 0);
102         at91_pio3_set_a_periph(AT91_PIO_PORTE, 13, 0);
103         at91_pio3_set_a_periph(AT91_PIO_PORTE, 14, 0);
104         at91_pio3_set_a_periph(AT91_PIO_PORTE, 15, 0);
105         at91_pio3_set_a_periph(AT91_PIO_PORTE, 16, 0);
106         at91_pio3_set_a_periph(AT91_PIO_PORTE, 17, 0);
107         at91_pio3_set_a_periph(AT91_PIO_PORTE, 18, 0);
108         at91_pio3_set_a_periph(AT91_PIO_PORTE, 19, 0);
109         at91_pio3_set_a_periph(AT91_PIO_PORTE, 20, 0);
110         at91_pio3_set_a_periph(AT91_PIO_PORTE, 21, 0);
111         at91_pio3_set_a_periph(AT91_PIO_PORTE, 22, 0);
112         at91_pio3_set_a_periph(AT91_PIO_PORTE, 23, 0);
113         /* CS0 pin configuration */
114         at91_pio3_set_a_periph(AT91_PIO_PORTE, 26, 0);
115 }
116 #endif
117
118 #ifdef CONFIG_CMD_USB
119 static void sama5d3xek_usb_hw_init(void)
120 {
121         at91_set_pio_output(AT91_PIO_PORTD, 25, 0);
122         at91_set_pio_output(AT91_PIO_PORTD, 26, 0);
123         at91_set_pio_output(AT91_PIO_PORTD, 27, 0);
124 }
125 #endif
126
127 #ifdef CONFIG_GENERIC_ATMEL_MCI
128 static void sama5d3xek_mci_hw_init(void)
129 {
130         at91_set_pio_output(AT91_PIO_PORTB, 10, 0);     /* MCI0 Power */
131 }
132 #endif
133
134 #ifdef CONFIG_DEBUG_UART_BOARD_INIT
135 void board_debug_uart_init(void)
136 {
137         at91_seriald_hw_init();
138 }
139 #endif
140
141 #ifdef CONFIG_BOARD_EARLY_INIT_F
142 int board_early_init_f(void)
143 {
144 #ifdef CONFIG_DEBUG_UART
145         debug_uart_init();
146 #endif
147         return 0;
148 }
149 #endif
150
151 int board_init(void)
152 {
153         /* adress of boot parameters */
154         gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
155
156 #ifdef CONFIG_NAND_ATMEL
157         sama5d3xek_nand_hw_init();
158 #endif
159 #ifdef CONFIG_MTD_NOR_FLASH
160         sama5d3xek_nor_hw_init();
161 #endif
162 #ifdef CONFIG_CMD_USB
163         sama5d3xek_usb_hw_init();
164 #endif
165 #ifdef CONFIG_GENERIC_ATMEL_MCI
166         sama5d3xek_mci_hw_init();
167 #endif
168         return 0;
169 }
170
171 int dram_init(void)
172 {
173         gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
174                                     CONFIG_SYS_SDRAM_SIZE);
175         return 0;
176 }
177
178 #ifdef CONFIG_BOARD_LATE_INIT
179 int board_late_init(void)
180 {
181 #ifdef CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG
182         const int MAX_STR_LEN = 32;
183         char name[MAX_STR_LEN], *p;
184         int i;
185
186         strncpy(name, get_cpu_name(), MAX_STR_LEN);
187         for (i = 0, p = name; (*p) && (i < MAX_STR_LEN); p++, i++)
188                 *p = tolower(*p);
189
190         strcat(name, "ek.dtb");
191         env_set("dtb_name", name);
192 #endif
193 #ifdef CONFIG_DM_VIDEO
194         at91_video_show_board_info();
195 #endif
196         return 0;
197 }
198 #endif
199
200 /* SPL */
201 #ifdef CONFIG_SPL_BUILD
202 void spl_board_init(void)
203 {
204 #if CONFIG_NAND_BOOT
205         sama5d3xek_nand_hw_init();
206 #endif
207 }
208
209 static void ddr2_conf(struct atmel_mpddrc_config *ddr2)
210 {
211         ddr2->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR2_SDRAM);
212
213         ddr2->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
214                     ATMEL_MPDDRC_CR_NR_ROW_14 |
215                     ATMEL_MPDDRC_CR_CAS_DDR_CAS3 |
216                     ATMEL_MPDDRC_CR_ENRDM_ON |
217                     ATMEL_MPDDRC_CR_NB_8BANKS |
218                     ATMEL_MPDDRC_CR_NDQS_DISABLED |
219                     ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
220                     ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
221         /*
222          * As the DDR2-SDRAm device requires a refresh time is 7.8125us
223          * when DDR run at 133MHz, so it needs (7.8125us * 133MHz / 10^9) clocks
224          */
225         ddr2->rtr = 0x411;
226
227         ddr2->tpr0 = (6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET |
228                       2 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET |
229                       2 << ATMEL_MPDDRC_TPR0_TWR_OFFSET |
230                       8 << ATMEL_MPDDRC_TPR0_TRC_OFFSET |
231                       2 << ATMEL_MPDDRC_TPR0_TRP_OFFSET |
232                       2 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET |
233                       2 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET |
234                       2 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET);
235
236         ddr2->tpr1 = (2 << ATMEL_MPDDRC_TPR1_TXP_OFFSET |
237                       200 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET |
238                       28 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET |
239                       26 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET);
240
241         ddr2->tpr2 = (7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET |
242                       2 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET |
243                       2 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET |
244                       7 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET |
245                       8 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET);
246 }
247
248 void mem_init(void)
249 {
250         struct atmel_mpddrc_config ddr2;
251
252         ddr2_conf(&ddr2);
253
254         /* Enable MPDDR clock */
255         at91_periph_clk_enable(ATMEL_ID_MPDDRC);
256         at91_system_clk_enable(AT91_PMC_DDR);
257
258         /* DDRAM2 Controller initialize */
259         ddr2_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddr2);
260 }
261
262 void at91_pmc_init(void)
263 {
264         u32 tmp;
265
266         tmp = AT91_PMC_PLLAR_29 |
267               AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
268               AT91_PMC_PLLXR_MUL(43) |
269               AT91_PMC_PLLXR_DIV(1);
270         at91_plla_init(tmp);
271
272         at91_pllicpr_init(AT91_PMC_IPLL_PLLA(0x3));
273
274         tmp = AT91_PMC_MCKR_MDIV_4 |
275               AT91_PMC_MCKR_CSS_PLLA;
276         at91_mck_init(tmp);
277 }
278 #endif