blob: da697a7b0fedc41379b436208dfdb5e2f90e6ea2 [file] [log] [blame]
Eugen Hristev860d8ba2018-07-06 11:15:10 +03001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright (C) 2018 Microchip Technology, Inc.
4 * Eugen Hristev <eugen.hristev@microchip.com>
5 */
6
7#include <common.h>
8#include <debug_uart.h>
Simon Glassa7b51302019-11-14 12:57:46 -07009#include <init.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060010#include <asm/global_data.h>
Eugen Hristev860d8ba2018-07-06 11:15:10 +030011#include <asm/io.h>
12#include <asm/arch/at91_common.h>
13#include <asm/arch/atmel_pio4.h>
14#include <asm/arch/atmel_mpddrc.h>
15#include <asm/arch/atmel_sdhci.h>
16#include <asm/arch/clk.h>
17#include <asm/arch/gpio.h>
18#include <asm/arch/sama5d2.h>
19
20DECLARE_GLOBAL_DATA_PTR;
21
Mihai Sainca453022022-03-07 11:20:50 +020022static void rgb_leds_init(void)
23{
24 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 0, 0); /* LED RED */
25 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 1, 0); /* LED GREEN */
26 atmel_pio4_set_pio_output(AT91_PIO_PORTA, 31, 1); /* LED BLUE */
27}
28
Eugen Hristev860d8ba2018-07-06 11:15:10 +030029int board_late_init(void)
30{
31 return 0;
32}
33
34#ifdef CONFIG_DEBUG_UART_BOARD_INIT
35static void board_uart0_hw_init(void)
36{
37 atmel_pio4_set_c_periph(AT91_PIO_PORTB, 26, ATMEL_PIO_PUEN_MASK); /* URXD0 */
38 atmel_pio4_set_c_periph(AT91_PIO_PORTB, 27, 0); /* UTXD0 */
39
40 at91_periph_clk_enable(ATMEL_ID_UART0);
41}
42
43void board_debug_uart_init(void)
44{
45 board_uart0_hw_init();
46}
47#endif
48
49int board_early_init_f(void)
50{
51#ifdef CONFIG_DEBUG_UART
52 debug_uart_init();
53#endif
54 return 0;
55}
56
57int board_init(void)
58{
59 /* address of boot parameters */
60 gd->bd->bi_boot_params = CONFIG_SYS_SDRAM_BASE + 0x100;
61
Mihai Sainca453022022-03-07 11:20:50 +020062 rgb_leds_init();
63
Eugen Hristev860d8ba2018-07-06 11:15:10 +030064 return 0;
65}
66
67int dram_init(void)
68{
69 gd->ram_size = get_ram_size((void *)CONFIG_SYS_SDRAM_BASE,
70 CONFIG_SYS_SDRAM_SIZE);
71 return 0;
72}
73
74#define MAC24AA_MAC_OFFSET 0xfa
75
76int misc_init_r(void)
77{
78#ifdef CONFIG_I2C_EEPROM
79 at91_set_ethaddr(MAC24AA_MAC_OFFSET);
80#endif
81 return 0;
82}
83
84/* SPL */
85#ifdef CONFIG_SPL_BUILD
86
Eugen Hristev584596e2019-05-03 16:24:21 +030087/* must set PB25 low to enable the CAN transceivers */
88static void board_can_stdby_dis(void)
89{
90 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 25, 0);
91}
92
Eugen Hristevec11d892019-05-03 16:26:53 +030093static void board_leds_init(void)
94{
95 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 0, 0); /* RED */
96 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 1, 1); /* GREEN */
97 atmel_pio4_set_pio_output(AT91_PIO_PORTA, 31, 0); /* BLUE */
98}
99
Eugen Hristev50736162019-05-03 15:56:44 +0300100/* deassert reset lines for external periph in case of warm reboot */
101static void board_reset_additional_periph(void)
102{
103 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 16, 0); /* LAN9252_RST */
104 atmel_pio4_set_pio_output(AT91_PIO_PORTC, 2, 0); /* HSIC_RST */
105 atmel_pio4_set_pio_output(AT91_PIO_PORTC, 17, 0); /* USB2534_RST */
106 atmel_pio4_set_pio_output(AT91_PIO_PORTD, 4, 0); /* KSZ8563_RST */
107}
108
109static void board_start_additional_periph(void)
110{
111 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 16, 1); /* LAN9252_RST */
112 atmel_pio4_set_pio_output(AT91_PIO_PORTC, 2, 1); /* HSIC_RST */
113 atmel_pio4_set_pio_output(AT91_PIO_PORTC, 17, 1); /* USB2534_RST */
114 atmel_pio4_set_pio_output(AT91_PIO_PORTD, 4, 1); /* KSZ8563_RST */
115}
116
Eugen Hristev860d8ba2018-07-06 11:15:10 +0300117#ifdef CONFIG_SD_BOOT
118void spl_mmc_init(void)
119{
120 atmel_pio4_set_a_periph(AT91_PIO_PORTA, 1, 0); /* CMD */
121 atmel_pio4_set_a_periph(AT91_PIO_PORTA, 2, 0); /* DAT0 */
122 atmel_pio4_set_a_periph(AT91_PIO_PORTA, 3, 0); /* DAT1 */
123 atmel_pio4_set_a_periph(AT91_PIO_PORTA, 4, 0); /* DAT2 */
124 atmel_pio4_set_a_periph(AT91_PIO_PORTA, 5, 0); /* DAT3 */
125 atmel_pio4_set_a_periph(AT91_PIO_PORTA, 0, 0); /* CK */
126 atmel_pio4_set_a_periph(AT91_PIO_PORTA, 13, 0); /* CD */
127
128 at91_periph_clk_enable(ATMEL_ID_SDMMC0);
129}
130#endif
131
132void spl_board_init(void)
133{
134#ifdef CONFIG_SD_BOOT
135 spl_mmc_init();
136#endif
Eugen Hristev50736162019-05-03 15:56:44 +0300137 board_reset_additional_periph();
Eugen Hristev584596e2019-05-03 16:24:21 +0300138 board_can_stdby_dis();
Eugen Hristevec11d892019-05-03 16:26:53 +0300139 board_leds_init();
Eugen Hristev860d8ba2018-07-06 11:15:10 +0300140}
141
142void spl_display_print(void)
143{
Eugen Hristev50736162019-05-03 15:56:44 +0300144}
145
146void spl_board_prepare_for_boot(void)
147{
148 board_start_additional_periph();
Eugen Hristev860d8ba2018-07-06 11:15:10 +0300149}
150
151static void ddrc_conf(struct atmel_mpddrc_config *ddrc)
152{
153 ddrc->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR3_SDRAM);
154
155 ddrc->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
156 ATMEL_MPDDRC_CR_NR_ROW_14 |
157 ATMEL_MPDDRC_CR_CAS_DDR_CAS5 |
158 ATMEL_MPDDRC_CR_DIC_DS |
159 ATMEL_MPDDRC_CR_NB_8BANKS |
160 ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
161 ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
162
163 ddrc->rtr = 0x298;
164
165 ddrc->tpr0 = ((6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET) |
166 (3 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET) |
167 (3 << ATMEL_MPDDRC_TPR0_TWR_OFFSET) |
168 (9 << ATMEL_MPDDRC_TPR0_TRC_OFFSET) |
169 (3 << ATMEL_MPDDRC_TPR0_TRP_OFFSET) |
170 (4 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET) |
171 (4 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET) |
172 (4 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET));
173
174 ddrc->tpr1 = ((27 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET) |
175 (29 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET) |
176 (0 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET) |
177 (10 << ATMEL_MPDDRC_TPR1_TXP_OFFSET));
178
179 ddrc->tpr2 = ((0 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET) |
180 (0 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET) |
181 (0 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET) |
182 (4 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET) |
183 (7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET));
184}
185
186void mem_init(void)
187{
188 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
189 struct atmel_mpddr *mpddrc = (struct atmel_mpddr *)ATMEL_BASE_MPDDRC;
190 struct atmel_mpddrc_config ddrc_config;
191 u32 reg;
192
193 ddrc_conf(&ddrc_config);
194
195 at91_periph_clk_enable(ATMEL_ID_MPDDRC);
196 writel(AT91_PMC_DDR, &pmc->scer);
197
198 reg = readl(&mpddrc->io_calibr);
199 reg &= ~ATMEL_MPDDRC_IO_CALIBR_RDIV;
200 reg |= ATMEL_MPDDRC_IO_CALIBR_DDR3_RZQ_55;
201 reg &= ~ATMEL_MPDDRC_IO_CALIBR_TZQIO;
202 reg |= ATMEL_MPDDRC_IO_CALIBR_TZQIO_(100);
203 writel(reg, &mpddrc->io_calibr);
204
205 writel(ATMEL_MPDDRC_RD_DATA_PATH_SHIFT_ONE_CYCLE,
206 &mpddrc->rd_data_path);
207
208 ddr3_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddrc_config);
209
210 writel(0x5355, &mpddrc->cal_mr4);
211 writel(64, &mpddrc->tim_cal);
212}
213
214void at91_pmc_init(void)
215{
216 u32 tmp;
217
218 /*
219 * while coming from the ROM code, we run on PLLA @ 492 MHz / 164 MHz
220 * so we need to slow down and configure MCKR accordingly.
221 * This is why we have a special flavor of the switching function.
222 */
223 tmp = AT91_PMC_MCKR_PLLADIV_2 |
224 AT91_PMC_MCKR_MDIV_3 |
225 AT91_PMC_MCKR_CSS_MAIN;
226 at91_mck_init_down(tmp);
227
228 tmp = AT91_PMC_PLLAR_29 |
229 AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
230 AT91_PMC_PLLXR_MUL(82) |
231 AT91_PMC_PLLXR_DIV(1);
232 at91_plla_init(tmp);
233
234 tmp = AT91_PMC_MCKR_H32MXDIV |
235 AT91_PMC_MCKR_PLLADIV_2 |
236 AT91_PMC_MCKR_MDIV_3 |
237 AT91_PMC_MCKR_CSS_PLLA;
238 at91_mck_init(tmp);
239}
240#endif