blob: aa522075691c7b07ecde3f37b60076172a6229f3 [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
Mihai Sainca453022022-03-07 11:20:50 +020024static void rgb_leds_init(void)
25{
26 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 6, 1); /* LED RED */
27 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 5, 1); /* LED GREEN */
28 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 0, 0); /* LED BLUE */
29}
30
Josef Lustickya0f2af32020-04-17 09:32:25 +020031#ifdef CONFIG_CMD_USB
Wenyou Yangc64a75a2015-10-30 09:55:52 +080032static void board_usb_hw_init(void)
33{
34 atmel_pio4_set_pio_output(AT91_PIO_PORTB, 10, 1);
35}
Josef Lustickya0f2af32020-04-17 09:32:25 +020036#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080037
Wenyou Yang3ec18a62017-09-18 15:25:57 +080038#ifdef CONFIG_BOARD_LATE_INIT
39int board_late_init(void)
Wenyou Yangc64a75a2015-10-30 09:55:52 +080040{
Simon Glass52cb5042022-10-18 07:46:31 -060041#ifdef CONFIG_VIDEO
Wenyou Yang3ec18a62017-09-18 15:25:57 +080042 at91_video_show_board_info();
43#endif
Eugen Hristev0c9ffe32018-09-18 10:35:43 +030044 at91_pda_detect();
Wenyou Yang3ec18a62017-09-18 15:25:57 +080045 return 0;
Wenyou Yangc64a75a2015-10-30 09:55:52 +080046}
Wenyou Yang3ec18a62017-09-18 15:25:57 +080047#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080048
Wenyou Yang4b1fa802017-03-23 14:26:26 +080049#ifdef CONFIG_DEBUG_UART_BOARD_INIT
Wenyou Yangc64a75a2015-10-30 09:55:52 +080050static void board_uart1_hw_init(void)
51{
Ludovic Desroches86504912018-04-24 10:16:01 +030052 atmel_pio4_set_a_periph(AT91_PIO_PORTD, 2, ATMEL_PIO_PUEN_MASK); /* URXD1 */
Wenyou Yangc64a75a2015-10-30 09:55:52 +080053 atmel_pio4_set_a_periph(AT91_PIO_PORTD, 3, 0); /* UTXD1 */
54
55 at91_periph_clk_enable(ATMEL_ID_UART1);
56}
57
Wenyou Yang113e1d12016-10-17 09:55:26 +080058void board_debug_uart_init(void)
59{
60 board_uart1_hw_init();
61}
62#endif
63
64#ifdef CONFIG_BOARD_EARLY_INIT_F
Wenyou Yangc64a75a2015-10-30 09:55:52 +080065int board_early_init_f(void)
66{
Wenyou Yangc64a75a2015-10-30 09:55:52 +080067 return 0;
68}
Wenyou Yang113e1d12016-10-17 09:55:26 +080069#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080070
71int board_init(void)
72{
73 /* address of boot parameters */
Clément Légerd8842642021-08-16 14:25:42 +020074 gd->bd->bi_boot_params = gd->bd->bi_dram[0].start + 0x100;
Wenyou Yangc64a75a2015-10-30 09:55:52 +080075
Mihai Sainca453022022-03-07 11:20:50 +020076 rgb_leds_init();
77
Wenyou Yangc64a75a2015-10-30 09:55:52 +080078#ifdef CONFIG_CMD_USB
79 board_usb_hw_init();
80#endif
Wenyou Yangc64a75a2015-10-30 09:55:52 +080081
82 return 0;
83}
84
Clément Légerd8842642021-08-16 14:25:42 +020085int dram_init_banksize(void)
86{
87 return fdtdec_setup_memory_banksize();
88}
89
Wenyou Yangc64a75a2015-10-30 09:55:52 +080090int dram_init(void)
91{
Clément Légerd8842642021-08-16 14:25:42 +020092 return fdtdec_setup_mem_size_base();
Wenyou Yangc64a75a2015-10-30 09:55:52 +080093}
94
Wenyou Yanga1e24ec2017-09-01 16:26:17 +080095#define AT24MAC_MAC_OFFSET 0x9a
Wenyou Yang3ce80fa2016-10-17 09:55:25 +080096
97#ifdef CONFIG_MISC_INIT_R
98int misc_init_r(void)
99{
Wenyou Yanga1e24ec2017-09-01 16:26:17 +0800100#ifdef CONFIG_I2C_EEPROM
101 at91_set_ethaddr(AT24MAC_MAC_OFFSET);
102#endif
Wenyou Yang3ce80fa2016-10-17 09:55:25 +0800103
104 return 0;
105}
106#endif
107
Wenyou Yang3acd9cc2016-02-01 18:18:21 +0800108/* SPL */
109#ifdef CONFIG_SPL_BUILD
110void spl_board_init(void)
111{
Wenyou Yang3acd9cc2016-02-01 18:18:21 +0800112}
113
114static void ddrc_conf(struct atmel_mpddrc_config *ddrc)
115{
116 ddrc->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR3_SDRAM);
117
118 ddrc->cr = (ATMEL_MPDDRC_CR_NC_COL_10 |
119 ATMEL_MPDDRC_CR_NR_ROW_14 |
120 ATMEL_MPDDRC_CR_CAS_DDR_CAS5 |
121 ATMEL_MPDDRC_CR_DIC_DS |
122 ATMEL_MPDDRC_CR_DIS_DLL |
123 ATMEL_MPDDRC_CR_NB_8BANKS |
124 ATMEL_MPDDRC_CR_DECOD_INTERLEAVED |
125 ATMEL_MPDDRC_CR_UNAL_SUPPORTED);
126
127 ddrc->rtr = 0x511;
128
129 ddrc->tpr0 = (6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET |
130 3 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET |
131 4 << ATMEL_MPDDRC_TPR0_TWR_OFFSET |
132 9 << ATMEL_MPDDRC_TPR0_TRC_OFFSET |
133 3 << ATMEL_MPDDRC_TPR0_TRP_OFFSET |
134 4 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET |
135 4 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET |
136 4 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET);
137
138 ddrc->tpr1 = (27 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET |
139 29 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET |
140 0 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET |
141 3 << ATMEL_MPDDRC_TPR1_TXP_OFFSET);
142
143 ddrc->tpr2 = (0 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET |
144 0 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET |
145 0 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET |
146 4 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET |
147 7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET);
148}
149
150void mem_init(void)
151{
152 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
153 struct atmel_mpddr *mpddrc = (struct atmel_mpddr *)ATMEL_BASE_MPDDRC;
154 struct atmel_mpddrc_config ddrc_config;
155 u32 reg;
156
157 ddrc_conf(&ddrc_config);
158
159 at91_periph_clk_enable(ATMEL_ID_MPDDRC);
160 writel(AT91_PMC_DDR, &pmc->scer);
161
162 reg = readl(&mpddrc->io_calibr);
163 reg &= ~ATMEL_MPDDRC_IO_CALIBR_RDIV;
164 reg |= ATMEL_MPDDRC_IO_CALIBR_DDR3_RZQ_55;
165 reg &= ~ATMEL_MPDDRC_IO_CALIBR_TZQIO;
166 reg |= ATMEL_MPDDRC_IO_CALIBR_TZQIO_(100);
167 writel(reg, &mpddrc->io_calibr);
168
169 writel(ATMEL_MPDDRC_RD_DATA_PATH_SHIFT_TWO_CYCLE,
170 &mpddrc->rd_data_path);
171
172 ddr3_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddrc_config);
173
174 writel(0x3, &mpddrc->cal_mr4);
175 writel(64, &mpddrc->tim_cal);
176}
177
178void at91_pmc_init(void)
179{
180 struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC;
181 u32 tmp;
182
Wenyou Yang8344ebd2017-09-13 14:58:50 +0800183 /*
184 * while coming from the ROM code, we run on PLLA @ 492 MHz / 164 MHz
185 * so we need to slow down and configure MCKR accordingly.
186 * This is why we have a special flavor of the switching function.
187 */
188 tmp = AT91_PMC_MCKR_PLLADIV_2 |
189 AT91_PMC_MCKR_MDIV_3 |
190 AT91_PMC_MCKR_CSS_MAIN;
191 at91_mck_init_down(tmp);
192
Wenyou Yang3acd9cc2016-02-01 18:18:21 +0800193 tmp = AT91_PMC_PLLAR_29 |
194 AT91_PMC_PLLXR_PLLCOUNT(0x3f) |
195 AT91_PMC_PLLXR_MUL(82) |
196 AT91_PMC_PLLXR_DIV(1);
197 at91_plla_init(tmp);
198
199 writel(0x0 << 8, &pmc->pllicpr);
200
201 tmp = AT91_PMC_MCKR_H32MXDIV |
202 AT91_PMC_MCKR_PLLADIV_2 |
203 AT91_PMC_MCKR_MDIV_3 |
204 AT91_PMC_MCKR_CSS_PLLA;
205 at91_mck_init(tmp);
206}
207#endif