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