Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2015 Atmel Corporation |
| 4 | * Wenyou.Yang <wenyou.yang@atmel.com> |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Wenyou Yang | 113e1d1 | 2016-10-17 09:55:26 +0800 | [diff] [blame] | 8 | #include <debug_uart.h> |
Simon Glass | a7b5130 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 9 | #include <init.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 11 | #include <asm/io.h> |
| 12 | #include <asm/arch/at91_common.h> |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 13 | #include <asm/arch/atmel_pio4.h> |
Wenyou Yang | 3acd9cc | 2016-02-01 18:18:21 +0800 | [diff] [blame] | 14 | #include <asm/arch/atmel_mpddrc.h> |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 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 | |
Eugen Hristev | 0c9ffe3 | 2018-09-18 10:35:43 +0300 | [diff] [blame] | 20 | extern void at91_pda_detect(void); |
| 21 | |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 22 | DECLARE_GLOBAL_DATA_PTR; |
| 23 | |
Mihai Sain | ca45302 | 2022-03-07 11:20:50 +0200 | [diff] [blame] | 24 | static 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 Lusticky | a0f2af3 | 2020-04-17 09:32:25 +0200 | [diff] [blame] | 31 | #ifdef CONFIG_CMD_USB |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 32 | static void board_usb_hw_init(void) |
| 33 | { |
| 34 | atmel_pio4_set_pio_output(AT91_PIO_PORTB, 10, 1); |
| 35 | } |
Josef Lusticky | a0f2af3 | 2020-04-17 09:32:25 +0200 | [diff] [blame] | 36 | #endif |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 37 | |
Wenyou Yang | 3ec18a6 | 2017-09-18 15:25:57 +0800 | [diff] [blame] | 38 | #ifdef CONFIG_BOARD_LATE_INIT |
| 39 | int board_late_init(void) |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 40 | { |
Wenyou Yang | 3ec18a6 | 2017-09-18 15:25:57 +0800 | [diff] [blame] | 41 | #ifdef CONFIG_DM_VIDEO |
| 42 | at91_video_show_board_info(); |
| 43 | #endif |
Eugen Hristev | 0c9ffe3 | 2018-09-18 10:35:43 +0300 | [diff] [blame] | 44 | at91_pda_detect(); |
Wenyou Yang | 3ec18a6 | 2017-09-18 15:25:57 +0800 | [diff] [blame] | 45 | return 0; |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 46 | } |
Wenyou Yang | 3ec18a6 | 2017-09-18 15:25:57 +0800 | [diff] [blame] | 47 | #endif |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 48 | |
Wenyou Yang | 4b1fa80 | 2017-03-23 14:26:26 +0800 | [diff] [blame] | 49 | #ifdef CONFIG_DEBUG_UART_BOARD_INIT |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 50 | static void board_uart1_hw_init(void) |
| 51 | { |
Ludovic Desroches | 8650491 | 2018-04-24 10:16:01 +0300 | [diff] [blame] | 52 | atmel_pio4_set_a_periph(AT91_PIO_PORTD, 2, ATMEL_PIO_PUEN_MASK); /* URXD1 */ |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 53 | atmel_pio4_set_a_periph(AT91_PIO_PORTD, 3, 0); /* UTXD1 */ |
| 54 | |
| 55 | at91_periph_clk_enable(ATMEL_ID_UART1); |
| 56 | } |
| 57 | |
Wenyou Yang | 113e1d1 | 2016-10-17 09:55:26 +0800 | [diff] [blame] | 58 | void board_debug_uart_init(void) |
| 59 | { |
| 60 | board_uart1_hw_init(); |
| 61 | } |
| 62 | #endif |
| 63 | |
| 64 | #ifdef CONFIG_BOARD_EARLY_INIT_F |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 65 | int board_early_init_f(void) |
| 66 | { |
Wenyou Yang | 113e1d1 | 2016-10-17 09:55:26 +0800 | [diff] [blame] | 67 | #ifdef CONFIG_DEBUG_UART |
| 68 | debug_uart_init(); |
Wenyou Yang | 113e1d1 | 2016-10-17 09:55:26 +0800 | [diff] [blame] | 69 | #endif |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 70 | |
| 71 | return 0; |
| 72 | } |
Wenyou Yang | 113e1d1 | 2016-10-17 09:55:26 +0800 | [diff] [blame] | 73 | #endif |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 74 | |
| 75 | int board_init(void) |
| 76 | { |
| 77 | /* address of boot parameters */ |
Clément Léger | d884264 | 2021-08-16 14:25:42 +0200 | [diff] [blame] | 78 | gd->bd->bi_boot_params = gd->bd->bi_dram[0].start + 0x100; |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 79 | |
Mihai Sain | ca45302 | 2022-03-07 11:20:50 +0200 | [diff] [blame] | 80 | rgb_leds_init(); |
| 81 | |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 82 | #ifdef CONFIG_CMD_USB |
| 83 | board_usb_hw_init(); |
| 84 | #endif |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 85 | |
| 86 | return 0; |
| 87 | } |
| 88 | |
Clément Léger | d884264 | 2021-08-16 14:25:42 +0200 | [diff] [blame] | 89 | int dram_init_banksize(void) |
| 90 | { |
| 91 | return fdtdec_setup_memory_banksize(); |
| 92 | } |
| 93 | |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 94 | int dram_init(void) |
| 95 | { |
Clément Léger | d884264 | 2021-08-16 14:25:42 +0200 | [diff] [blame] | 96 | return fdtdec_setup_mem_size_base(); |
Wenyou Yang | c64a75a | 2015-10-30 09:55:52 +0800 | [diff] [blame] | 97 | } |
| 98 | |
Wenyou Yang | a1e24ec | 2017-09-01 16:26:17 +0800 | [diff] [blame] | 99 | #define AT24MAC_MAC_OFFSET 0x9a |
Wenyou Yang | 3ce80fa | 2016-10-17 09:55:25 +0800 | [diff] [blame] | 100 | |
| 101 | #ifdef CONFIG_MISC_INIT_R |
| 102 | int misc_init_r(void) |
| 103 | { |
Wenyou Yang | a1e24ec | 2017-09-01 16:26:17 +0800 | [diff] [blame] | 104 | #ifdef CONFIG_I2C_EEPROM |
| 105 | at91_set_ethaddr(AT24MAC_MAC_OFFSET); |
| 106 | #endif |
Wenyou Yang | 3ce80fa | 2016-10-17 09:55:25 +0800 | [diff] [blame] | 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | #endif |
| 111 | |
Wenyou Yang | 3acd9cc | 2016-02-01 18:18:21 +0800 | [diff] [blame] | 112 | /* SPL */ |
| 113 | #ifdef CONFIG_SPL_BUILD |
| 114 | void spl_board_init(void) |
| 115 | { |
Wenyou Yang | 3acd9cc | 2016-02-01 18:18:21 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static void ddrc_conf(struct atmel_mpddrc_config *ddrc) |
| 119 | { |
| 120 | ddrc->md = (ATMEL_MPDDRC_MD_DBW_32_BITS | ATMEL_MPDDRC_MD_DDR3_SDRAM); |
| 121 | |
| 122 | ddrc->cr = (ATMEL_MPDDRC_CR_NC_COL_10 | |
| 123 | ATMEL_MPDDRC_CR_NR_ROW_14 | |
| 124 | ATMEL_MPDDRC_CR_CAS_DDR_CAS5 | |
| 125 | ATMEL_MPDDRC_CR_DIC_DS | |
| 126 | ATMEL_MPDDRC_CR_DIS_DLL | |
| 127 | ATMEL_MPDDRC_CR_NB_8BANKS | |
| 128 | ATMEL_MPDDRC_CR_DECOD_INTERLEAVED | |
| 129 | ATMEL_MPDDRC_CR_UNAL_SUPPORTED); |
| 130 | |
| 131 | ddrc->rtr = 0x511; |
| 132 | |
| 133 | ddrc->tpr0 = (6 << ATMEL_MPDDRC_TPR0_TRAS_OFFSET | |
| 134 | 3 << ATMEL_MPDDRC_TPR0_TRCD_OFFSET | |
| 135 | 4 << ATMEL_MPDDRC_TPR0_TWR_OFFSET | |
| 136 | 9 << ATMEL_MPDDRC_TPR0_TRC_OFFSET | |
| 137 | 3 << ATMEL_MPDDRC_TPR0_TRP_OFFSET | |
| 138 | 4 << ATMEL_MPDDRC_TPR0_TRRD_OFFSET | |
| 139 | 4 << ATMEL_MPDDRC_TPR0_TWTR_OFFSET | |
| 140 | 4 << ATMEL_MPDDRC_TPR0_TMRD_OFFSET); |
| 141 | |
| 142 | ddrc->tpr1 = (27 << ATMEL_MPDDRC_TPR1_TRFC_OFFSET | |
| 143 | 29 << ATMEL_MPDDRC_TPR1_TXSNR_OFFSET | |
| 144 | 0 << ATMEL_MPDDRC_TPR1_TXSRD_OFFSET | |
| 145 | 3 << ATMEL_MPDDRC_TPR1_TXP_OFFSET); |
| 146 | |
| 147 | ddrc->tpr2 = (0 << ATMEL_MPDDRC_TPR2_TXARD_OFFSET | |
| 148 | 0 << ATMEL_MPDDRC_TPR2_TXARDS_OFFSET | |
| 149 | 0 << ATMEL_MPDDRC_TPR2_TRPA_OFFSET | |
| 150 | 4 << ATMEL_MPDDRC_TPR2_TRTP_OFFSET | |
| 151 | 7 << ATMEL_MPDDRC_TPR2_TFAW_OFFSET); |
| 152 | } |
| 153 | |
| 154 | void mem_init(void) |
| 155 | { |
| 156 | struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; |
| 157 | struct atmel_mpddr *mpddrc = (struct atmel_mpddr *)ATMEL_BASE_MPDDRC; |
| 158 | struct atmel_mpddrc_config ddrc_config; |
| 159 | u32 reg; |
| 160 | |
| 161 | ddrc_conf(&ddrc_config); |
| 162 | |
| 163 | at91_periph_clk_enable(ATMEL_ID_MPDDRC); |
| 164 | writel(AT91_PMC_DDR, &pmc->scer); |
| 165 | |
| 166 | reg = readl(&mpddrc->io_calibr); |
| 167 | reg &= ~ATMEL_MPDDRC_IO_CALIBR_RDIV; |
| 168 | reg |= ATMEL_MPDDRC_IO_CALIBR_DDR3_RZQ_55; |
| 169 | reg &= ~ATMEL_MPDDRC_IO_CALIBR_TZQIO; |
| 170 | reg |= ATMEL_MPDDRC_IO_CALIBR_TZQIO_(100); |
| 171 | writel(reg, &mpddrc->io_calibr); |
| 172 | |
| 173 | writel(ATMEL_MPDDRC_RD_DATA_PATH_SHIFT_TWO_CYCLE, |
| 174 | &mpddrc->rd_data_path); |
| 175 | |
| 176 | ddr3_init(ATMEL_BASE_MPDDRC, ATMEL_BASE_DDRCS, &ddrc_config); |
| 177 | |
| 178 | writel(0x3, &mpddrc->cal_mr4); |
| 179 | writel(64, &mpddrc->tim_cal); |
| 180 | } |
| 181 | |
| 182 | void at91_pmc_init(void) |
| 183 | { |
| 184 | struct at91_pmc *pmc = (struct at91_pmc *)ATMEL_BASE_PMC; |
| 185 | u32 tmp; |
| 186 | |
Wenyou Yang | 8344ebd | 2017-09-13 14:58:50 +0800 | [diff] [blame] | 187 | /* |
| 188 | * while coming from the ROM code, we run on PLLA @ 492 MHz / 164 MHz |
| 189 | * so we need to slow down and configure MCKR accordingly. |
| 190 | * This is why we have a special flavor of the switching function. |
| 191 | */ |
| 192 | tmp = AT91_PMC_MCKR_PLLADIV_2 | |
| 193 | AT91_PMC_MCKR_MDIV_3 | |
| 194 | AT91_PMC_MCKR_CSS_MAIN; |
| 195 | at91_mck_init_down(tmp); |
| 196 | |
Wenyou Yang | 3acd9cc | 2016-02-01 18:18:21 +0800 | [diff] [blame] | 197 | tmp = AT91_PMC_PLLAR_29 | |
| 198 | AT91_PMC_PLLXR_PLLCOUNT(0x3f) | |
| 199 | AT91_PMC_PLLXR_MUL(82) | |
| 200 | AT91_PMC_PLLXR_DIV(1); |
| 201 | at91_plla_init(tmp); |
| 202 | |
| 203 | writel(0x0 << 8, &pmc->pllicpr); |
| 204 | |
| 205 | tmp = AT91_PMC_MCKR_H32MXDIV | |
| 206 | AT91_PMC_MCKR_PLLADIV_2 | |
| 207 | AT91_PMC_MCKR_MDIV_3 | |
| 208 | AT91_PMC_MCKR_CSS_PLLA; |
| 209 | at91_mck_init(tmp); |
| 210 | } |
| 211 | #endif |