blob: 8b5cd533d04694122b44aeeb631b6a3eff4ba8b5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wenyou Yangc64a75a2015-10-30 09:55:52 +08002/*
3 * Copyright (C) 2015 Atmel Corporation
4 * Wenyou.Yang <wenyou.yang@atmel.com>
Wenyou Yangc64a75a2015-10-30 09:55:52 +08005 */
6
7#include <common.h>
Wenyou Yang113e1d12016-10-17 09:55:26 +08008#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>
Wenyou Yangc64a75a2015-10-30 09:55:52 +080011#include <asm/io.h>
12#include <asm/arch/at91_common.h>
Wenyou Yangc64a75a2015-10-30 09:55:52 +080013#include <asm/arch/atmel_pio4.h>
Wenyou Yang3acd9cc2016-02-01 18:18:21 +080014#include <asm/arch/atmel_mpddrc.h>
Wenyou Yangc64a75a2015-10-30 09:55:52 +080015#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
Eugen Hristev0c9ffe32018-09-18 10:35:43 +030020extern void at91_pda_detect(void);
21
Wenyou Yangc64a75a2015-10-30 09:55:52 +080022DECLARE_GLOBAL_DATA_PTR;
23
Josef Lustickya0f2af32020-04-17 09:32:25 +020024#ifdef CONFIG_CMD_USB
Wenyou Yangc64a75a2015-10-30 09:55:52 +080025static void board_usb_hw_init(void)
26{
27 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 10, 1);
28}
Josef Lustickya0f2af32020-04-17 09:32:25 +020029#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080030
Wenyou Yang3ec18a62017-09-18 15:25:57 +080031#ifdef CONFIG_BOARD_LATE_INIT
32int board_late_init(void)
Wenyou Yangc64a75a2015-10-30 09:55:52 +080033{
Wenyou Yang3ec18a62017-09-18 15:25:57 +080034#ifdef CONFIG_DM_VIDEO
35 at91_video_show_board_info();
36#endif
Eugen Hristev0c9ffe32018-09-18 10:35:43 +030037 at91_pda_detect();
Wenyou Yang3ec18a62017-09-18 15:25:57 +080038 return 0;
Wenyou Yangc64a75a2015-10-30 09:55:52 +080039}
Wenyou Yang3ec18a62017-09-18 15:25:57 +080040#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080041
Wenyou Yang4b1fa802017-03-23 14:26:26 +080042#ifdef CONFIG_DEBUG_UART_BOARD_INIT
Wenyou Yangc64a75a2015-10-30 09:55:52 +080043static void board_uart1_hw_init(void)
44{
Ludovic Desroches86504912018-04-24 10:16:01 +030045 atmel_pio4_set_a_periph(AT91_PIO_PORTD, 2, ATMEL_PIO_PUEN_MASK); /* URXD1 */
Wenyou Yangc64a75a2015-10-30 09:55:52 +080046 atmel_pio4_set_a_periph(AT91_PIO_PORTD, 3, 0); /* UTXD1 */
47
48 at91_periph_clk_enable(ATMEL_ID_UART1);
49}
50
Wenyou Yang113e1d12016-10-17 09:55:26 +080051void board_debug_uart_init(void)
52{
53 board_uart1_hw_init();
54}
55#endif
56
57#ifdef CONFIG_BOARD_EARLY_INIT_F
Wenyou Yangc64a75a2015-10-30 09:55:52 +080058int board_early_init_f(void)
59{
Wenyou Yang113e1d12016-10-17 09:55:26 +080060#ifdef CONFIG_DEBUG_UART
61 debug_uart_init();
Wenyou Yang113e1d12016-10-17 09:55:26 +080062#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080063
64 return 0;
65}
Wenyou Yang113e1d12016-10-17 09:55:26 +080066#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080067
68int board_init(void)
69{
70 /* address of boot parameters */
Clément Légerd8842642021-08-16 14:25:42 +020071 gd->bd->bi_boot_params = gd->bd->bi_dram[0].start + 0x100;
Wenyou Yangc64a75a2015-10-30 09:55:52 +080072
Wenyou Yangc64a75a2015-10-30 09:55:52 +080073#ifdef CONFIG_CMD_USB
74 board_usb_hw_init();
75#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080076
77 return 0;
78}
79
Clément Légerd8842642021-08-16 14:25:42 +020080int dram_init_banksize(void)
81{
82 return fdtdec_setup_memory_banksize();
83}
84
Wenyou Yangc64a75a2015-10-30 09:55:52 +080085int dram_init(void)
86{
Clément Légerd8842642021-08-16 14:25:42 +020087 return fdtdec_setup_mem_size_base();
Wenyou Yangc64a75a2015-10-30 09:55:52 +080088}
89
Wenyou Yanga1e24ec2017-09-01 16:26:17 +080090#define AT24MAC_MAC_OFFSET 0x9a
Wenyou Yang3ce80fa2016-10-17 09:55:25 +080091
92#ifdef CONFIG_MISC_INIT_R
93int misc_init_r(void)
94{
Wenyou Yanga1e24ec2017-09-01 16:26:17 +080095#ifdef CONFIG_I2C_EEPROM
96 at91_set_ethaddr(AT24MAC_MAC_OFFSET);
97#endif
Wenyou Yang3ce80fa2016-10-17 09:55:25 +080098
99 return 0;
100}
101#endif
102
Wenyou Yang3acd9cc2016-02-01 18:18:21 +0800103/* SPL */
104#ifdef CONFIG_SPL_BUILD
105void spl_board_init(void)
106{
Wenyou Yang3acd9cc2016-02-01 18:18:21 +0800107}
108
109static void ddrc_conf(struct atmel_mpddrc_config *ddrc)
110{
111 ddrc->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR3_SDRAM);
112
113 ddrc->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
114 ATMEL_MPDDRC_CR_NR_ROW_14 |
115 ATMEL_MPDDRC_CR_CAS_DDR_CAS5 |
116 ATMEL_MPDDRC_CR_DIC_DS |
117 ATMEL_MPDDRC_CR_DIS_DLL |
118 ATMEL_MPDDRC_CR_NB_8BANKS |
119 ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
120 ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
121
122 ddrc->rtr = 0x511;
123
124 ddrc->tpr0 = (6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET |
125 3 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET |
126 4 << ATMEL_MPDDRC_TPR0_TWR_OFFSET |
127 9 << ATMEL_MPDDRC_TPR0_TRC_OFFSET |
128 3 << ATMEL_MPDDRC_TPR0_TRP_OFFSET |
129 4 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET |
130 4 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET |
131 4 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET);
132
133 ddrc->tpr1 = (27 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET |
134 29 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET |
135 0 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET |
136 3 << ATMEL_MPDDRC_TPR1_TXP_OFFSET);
137
138 ddrc->tpr2 = (0 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET |
139 0 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET |
140 0 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET |
141 4 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET |
142 7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET);
143}
144
145void mem_init(void)
146{
147 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
148 struct atmel_mpddr *mpddrc = (struct atmel_mpddr *)ATMEL_BASE_MPDDRC;
149 struct atmel_mpddrc_config ddrc_config;
150 u32 reg;
151
152 ddrc_conf(&ddrc_config);
153
154 at91_periph_clk_enable(ATMEL_ID_MPDDRC);
155 writel(AT91_PMC_DDR, &pmc->scer);
156
157 reg = readl(&mpddrc->io_calibr);
158 reg &= ~ATMEL_MPDDRC_IO_CALIBR_RDIV;
159 reg |= ATMEL_MPDDRC_IO_CALIBR_DDR3_RZQ_55;
160 reg &= ~ATMEL_MPDDRC_IO_CALIBR_TZQIO;
161 reg |= ATMEL_MPDDRC_IO_CALIBR_TZQIO_(100);
162 writel(reg, &mpddrc->io_calibr);
163
164 writel(ATMEL_MPDDRC_RD_DATA_PATH_SHIFT_TWO_CYCLE,
165 &mpddrc->rd_data_path);
166
167 ddr3_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddrc_config);
168
169 writel(0x3, &mpddrc->cal_mr4);
170 writel(64, &mpddrc->tim_cal);
171}
172
173void at91_pmc_init(void)
174{
175 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
176 u32 tmp;
177
Wenyou Yang8344ebd2017-09-13 14:58:50 +0800178 /*
179 * while coming from the ROM code, we run on PLLA @ 492 MHz / 164 MHz
180 * so we need to slow down and configure MCKR accordingly.
181 * This is why we have a special flavor of the switching function.
182 */
183 tmp = AT91_PMC_MCKR_PLLADIV_2 |
184 AT91_PMC_MCKR_MDIV_3 |
185 AT91_PMC_MCKR_CSS_MAIN;
186 at91_mck_init_down(tmp);
187
Wenyou Yang3acd9cc2016-02-01 18:18:21 +0800188 tmp = AT91_PMC_PLLAR_29 |
189 AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
190 AT91_PMC_PLLXR_MUL(82) |
191 AT91_PMC_PLLXR_DIV(1);
192 at91_plla_init(tmp);
193
194 writel(0x0 << 8, &pmc->pllicpr);
195
196 tmp = AT91_PMC_MCKR_H32MXDIV |
197 AT91_PMC_MCKR_PLLADIV_2 |
198 AT91_PMC_MCKR_MDIV_3 |
199 AT91_PMC_MCKR_CSS_PLLA;
200 at91_mck_init(tmp);
201}
202#endif