1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright (C) 2016-2017 Intel Corporation
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * SPDX-License-Identifier: GPL-2.0
5*4882a593Smuzhiyun */
6*4882a593Smuzhiyun
7*4882a593Smuzhiyun #include <altera.h>
8*4882a593Smuzhiyun #include <common.h>
9*4882a593Smuzhiyun #include <errno.h>
10*4882a593Smuzhiyun #include <fdtdec.h>
11*4882a593Smuzhiyun #include <miiphy.h>
12*4882a593Smuzhiyun #include <netdev.h>
13*4882a593Smuzhiyun #include <ns16550.h>
14*4882a593Smuzhiyun #include <watchdog.h>
15*4882a593Smuzhiyun #include <asm/arch/misc.h>
16*4882a593Smuzhiyun #include <asm/arch/pinmux.h>
17*4882a593Smuzhiyun #include <asm/arch/reset_manager.h>
18*4882a593Smuzhiyun #include <asm/arch/sdram_arria10.h>
19*4882a593Smuzhiyun #include <asm/arch/system_manager.h>
20*4882a593Smuzhiyun #include <asm/arch/nic301.h>
21*4882a593Smuzhiyun #include <asm/io.h>
22*4882a593Smuzhiyun #include <asm/pl310.h>
23*4882a593Smuzhiyun
24*4882a593Smuzhiyun #define PINMUX_UART0_TX_SHARED_IO_OFFSET_Q1_3 0x08
25*4882a593Smuzhiyun #define PINMUX_UART0_TX_SHARED_IO_OFFSET_Q2_11 0x58
26*4882a593Smuzhiyun #define PINMUX_UART0_TX_SHARED_IO_OFFSET_Q3_3 0x68
27*4882a593Smuzhiyun #define PINMUX_UART1_TX_SHARED_IO_OFFSET_Q1_7 0x18
28*4882a593Smuzhiyun #define PINMUX_UART1_TX_SHARED_IO_OFFSET_Q3_7 0x78
29*4882a593Smuzhiyun #define PINMUX_UART1_TX_SHARED_IO_OFFSET_Q4_3 0x98
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun DECLARE_GLOBAL_DATA_PTR;
32*4882a593Smuzhiyun
33*4882a593Smuzhiyun #if defined(CONFIG_SPL_BUILD)
34*4882a593Smuzhiyun static struct pl310_regs *const pl310 =
35*4882a593Smuzhiyun (struct pl310_regs *)CONFIG_SYS_PL310_BASE;
36*4882a593Smuzhiyun static const struct socfpga_noc_fw_ocram *noc_fw_ocram_base =
37*4882a593Smuzhiyun (void *)SOCFPGA_SDR_FIREWALL_OCRAM_ADDRESS;
38*4882a593Smuzhiyun #endif
39*4882a593Smuzhiyun
40*4882a593Smuzhiyun static struct socfpga_system_manager *sysmgr_regs =
41*4882a593Smuzhiyun (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /*
44*4882a593Smuzhiyun * DesignWare Ethernet initialization
45*4882a593Smuzhiyun */
46*4882a593Smuzhiyun #ifdef CONFIG_ETH_DESIGNWARE
dwmac_deassert_reset(const unsigned int of_reset_id,const u32 phymode)47*4882a593Smuzhiyun void dwmac_deassert_reset(const unsigned int of_reset_id,
48*4882a593Smuzhiyun const u32 phymode)
49*4882a593Smuzhiyun {
50*4882a593Smuzhiyun u32 reset;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun if (of_reset_id == EMAC0_RESET) {
53*4882a593Smuzhiyun reset = SOCFPGA_RESET(EMAC0);
54*4882a593Smuzhiyun } else if (of_reset_id == EMAC1_RESET) {
55*4882a593Smuzhiyun reset = SOCFPGA_RESET(EMAC1);
56*4882a593Smuzhiyun } else if (of_reset_id == EMAC2_RESET) {
57*4882a593Smuzhiyun reset = SOCFPGA_RESET(EMAC2);
58*4882a593Smuzhiyun } else {
59*4882a593Smuzhiyun printf("GMAC: Invalid reset ID (%i)!\n", of_reset_id);
60*4882a593Smuzhiyun return;
61*4882a593Smuzhiyun }
62*4882a593Smuzhiyun
63*4882a593Smuzhiyun clrsetbits_le32(&sysmgr_regs->emac[of_reset_id - EMAC0_RESET],
64*4882a593Smuzhiyun SYSMGR_EMACGRP_CTRL_PHYSEL_MASK,
65*4882a593Smuzhiyun phymode);
66*4882a593Smuzhiyun
67*4882a593Smuzhiyun /* Release the EMAC controller from reset */
68*4882a593Smuzhiyun socfpga_per_reset(reset, 0);
69*4882a593Smuzhiyun }
70*4882a593Smuzhiyun #endif
71*4882a593Smuzhiyun
72*4882a593Smuzhiyun #if defined(CONFIG_SPL_BUILD)
73*4882a593Smuzhiyun /*
74*4882a593Smuzhiyun + * This function initializes security policies to be consistent across
75*4882a593Smuzhiyun + * all logic units in the Arria 10.
76*4882a593Smuzhiyun + *
77*4882a593Smuzhiyun + * The idea is to set all security policies to be normal, nonsecure
78*4882a593Smuzhiyun + * for all units.
79*4882a593Smuzhiyun + */
initialize_security_policies(void)80*4882a593Smuzhiyun static void initialize_security_policies(void)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun /* Put OCRAM in non-secure */
83*4882a593Smuzhiyun writel(0x003f0000, &noc_fw_ocram_base->region0);
84*4882a593Smuzhiyun writel(0x1, &noc_fw_ocram_base->enable);
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun
arch_early_init_r(void)87*4882a593Smuzhiyun int arch_early_init_r(void)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun initialize_security_policies();
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* Configure the L2 controller to make SDRAM start at 0 */
92*4882a593Smuzhiyun writel(0x1, &pl310->pl310_addr_filter_start);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* assert reset to all except L4WD0 and L4TIMER0 */
95*4882a593Smuzhiyun socfpga_per_reset_all();
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* configuring the clock based on handoff */
98*4882a593Smuzhiyun /* TODO: Add call to cm_basic_init() */
99*4882a593Smuzhiyun
100*4882a593Smuzhiyun /* Add device descriptor to FPGA device table */
101*4882a593Smuzhiyun socfpga_fpga_add();
102*4882a593Smuzhiyun return 0;
103*4882a593Smuzhiyun }
104*4882a593Smuzhiyun #else
arch_early_init_r(void)105*4882a593Smuzhiyun int arch_early_init_r(void)
106*4882a593Smuzhiyun {
107*4882a593Smuzhiyun return 0;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun #endif
110*4882a593Smuzhiyun
111*4882a593Smuzhiyun /*
112*4882a593Smuzhiyun * This function looking the 1st encounter UART peripheral,
113*4882a593Smuzhiyun * and then return its offset of the dedicated/shared IO pin
114*4882a593Smuzhiyun * mux. offset value (zero and above).
115*4882a593Smuzhiyun */
find_peripheral_uart(const void * blob,int child,const char * node_name)116*4882a593Smuzhiyun static int find_peripheral_uart(const void *blob,
117*4882a593Smuzhiyun int child, const char *node_name)
118*4882a593Smuzhiyun {
119*4882a593Smuzhiyun int len;
120*4882a593Smuzhiyun fdt_addr_t base_addr = 0;
121*4882a593Smuzhiyun fdt_size_t size;
122*4882a593Smuzhiyun const u32 *cell;
123*4882a593Smuzhiyun u32 value, offset = 0;
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun base_addr = fdtdec_get_addr_size(blob, child, "reg", &size);
126*4882a593Smuzhiyun if (base_addr != FDT_ADDR_T_NONE) {
127*4882a593Smuzhiyun cell = fdt_getprop(blob, child, "pinctrl-single,pins",
128*4882a593Smuzhiyun &len);
129*4882a593Smuzhiyun if (cell != NULL) {
130*4882a593Smuzhiyun for (; len > 0; len -= (2 * sizeof(u32))) {
131*4882a593Smuzhiyun offset = fdt32_to_cpu(*cell++);
132*4882a593Smuzhiyun value = fdt32_to_cpu(*cell++);
133*4882a593Smuzhiyun /* Found UART peripheral. */
134*4882a593Smuzhiyun if (value == PINMUX_UART)
135*4882a593Smuzhiyun return offset;
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun return -EINVAL;
140*4882a593Smuzhiyun }
141*4882a593Smuzhiyun
142*4882a593Smuzhiyun /*
143*4882a593Smuzhiyun * This function looks up the 1st encounter UART peripheral,
144*4882a593Smuzhiyun * and then return its offset of the dedicated/shared IO pin
145*4882a593Smuzhiyun * mux. UART peripheral is found if the offset is not in negative
146*4882a593Smuzhiyun * value.
147*4882a593Smuzhiyun */
is_peripheral_uart_true(const void * blob,int node,const char * child_name)148*4882a593Smuzhiyun static int is_peripheral_uart_true(const void *blob,
149*4882a593Smuzhiyun int node, const char *child_name)
150*4882a593Smuzhiyun {
151*4882a593Smuzhiyun int child, len;
152*4882a593Smuzhiyun const char *node_name;
153*4882a593Smuzhiyun
154*4882a593Smuzhiyun child = fdt_first_subnode(blob, node);
155*4882a593Smuzhiyun
156*4882a593Smuzhiyun if (child < 0)
157*4882a593Smuzhiyun return -EINVAL;
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun node_name = fdt_get_name(blob, child, &len);
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun while (node_name) {
162*4882a593Smuzhiyun if (!strcmp(child_name, node_name))
163*4882a593Smuzhiyun return find_peripheral_uart(blob, child, node_name);
164*4882a593Smuzhiyun
165*4882a593Smuzhiyun child = fdt_next_subnode(blob, child);
166*4882a593Smuzhiyun if (child < 0)
167*4882a593Smuzhiyun break;
168*4882a593Smuzhiyun
169*4882a593Smuzhiyun node_name = fdt_get_name(blob, child, &len);
170*4882a593Smuzhiyun }
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun return -1;
173*4882a593Smuzhiyun }
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun /*
176*4882a593Smuzhiyun * This function looking the 1st encounter UART dedicated IO peripheral,
177*4882a593Smuzhiyun * and then return based address of the 1st encounter UART dedicated
178*4882a593Smuzhiyun * IO peripheral.
179*4882a593Smuzhiyun */
dedicated_uart_com_port(const void * blob)180*4882a593Smuzhiyun unsigned int dedicated_uart_com_port(const void *blob)
181*4882a593Smuzhiyun {
182*4882a593Smuzhiyun int node;
183*4882a593Smuzhiyun
184*4882a593Smuzhiyun node = fdtdec_next_compatible(blob, 0,
185*4882a593Smuzhiyun COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE);
186*4882a593Smuzhiyun if (node < 0)
187*4882a593Smuzhiyun return 0;
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun if (is_peripheral_uart_true(blob, node, "dedicated") >= 0)
190*4882a593Smuzhiyun return SOCFPGA_UART1_ADDRESS;
191*4882a593Smuzhiyun
192*4882a593Smuzhiyun return 0;
193*4882a593Smuzhiyun }
194*4882a593Smuzhiyun
195*4882a593Smuzhiyun /*
196*4882a593Smuzhiyun * This function looking the 1st encounter UART shared IO peripheral, and then
197*4882a593Smuzhiyun * return based address of the 1st encounter UART shared IO peripheral.
198*4882a593Smuzhiyun */
shared_uart_com_port(const void * blob)199*4882a593Smuzhiyun unsigned int shared_uart_com_port(const void *blob)
200*4882a593Smuzhiyun {
201*4882a593Smuzhiyun int node, ret;
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun node = fdtdec_next_compatible(blob, 0,
204*4882a593Smuzhiyun COMPAT_ALTERA_SOCFPGA_PINCTRL_SINGLE);
205*4882a593Smuzhiyun if (node < 0)
206*4882a593Smuzhiyun return 0;
207*4882a593Smuzhiyun
208*4882a593Smuzhiyun ret = is_peripheral_uart_true(blob, node, "shared");
209*4882a593Smuzhiyun
210*4882a593Smuzhiyun if (ret == PINMUX_UART0_TX_SHARED_IO_OFFSET_Q1_3 ||
211*4882a593Smuzhiyun ret == PINMUX_UART0_TX_SHARED_IO_OFFSET_Q2_11 ||
212*4882a593Smuzhiyun ret == PINMUX_UART0_TX_SHARED_IO_OFFSET_Q3_3)
213*4882a593Smuzhiyun return SOCFPGA_UART0_ADDRESS;
214*4882a593Smuzhiyun else if (ret == PINMUX_UART1_TX_SHARED_IO_OFFSET_Q1_7 ||
215*4882a593Smuzhiyun ret == PINMUX_UART1_TX_SHARED_IO_OFFSET_Q3_7 ||
216*4882a593Smuzhiyun ret == PINMUX_UART1_TX_SHARED_IO_OFFSET_Q4_3)
217*4882a593Smuzhiyun return SOCFPGA_UART1_ADDRESS;
218*4882a593Smuzhiyun
219*4882a593Smuzhiyun return 0;
220*4882a593Smuzhiyun }
221*4882a593Smuzhiyun
222*4882a593Smuzhiyun /*
223*4882a593Smuzhiyun * This function looking the 1st encounter UART peripheral, and then return
224*4882a593Smuzhiyun * base address of the 1st encounter UART peripheral.
225*4882a593Smuzhiyun */
uart_com_port(const void * blob)226*4882a593Smuzhiyun unsigned int uart_com_port(const void *blob)
227*4882a593Smuzhiyun {
228*4882a593Smuzhiyun unsigned int ret;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun ret = dedicated_uart_com_port(blob);
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun if (ret)
233*4882a593Smuzhiyun return ret;
234*4882a593Smuzhiyun
235*4882a593Smuzhiyun return shared_uart_com_port(blob);
236*4882a593Smuzhiyun }
237*4882a593Smuzhiyun
238*4882a593Smuzhiyun /*
239*4882a593Smuzhiyun * Print CPU information
240*4882a593Smuzhiyun */
241*4882a593Smuzhiyun #if defined(CONFIG_DISPLAY_CPUINFO)
print_cpuinfo(void)242*4882a593Smuzhiyun int print_cpuinfo(void)
243*4882a593Smuzhiyun {
244*4882a593Smuzhiyun const u32 bsel =
245*4882a593Smuzhiyun SYSMGR_GET_BOOTINFO_BSEL(readl(&sysmgr_regs->bootinfo));
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun puts("CPU: Altera SoCFPGA Arria 10\n");
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun printf("BOOT: %s\n", bsel_str[bsel].name);
250*4882a593Smuzhiyun return 0;
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun #endif
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun #ifdef CONFIG_ARCH_MISC_INIT
arch_misc_init(void)255*4882a593Smuzhiyun int arch_misc_init(void)
256*4882a593Smuzhiyun {
257*4882a593Smuzhiyun return 0;
258*4882a593Smuzhiyun }
259*4882a593Smuzhiyun #endif
260