1d559130eSLey Foon Tan // SPDX-License-Identifier: GPL-2.0
2d559130eSLey Foon Tan /*
3d559130eSLey Foon Tan * Copyright (C) 2016-2018 Intel Corporation <www.intel.com>
4d559130eSLey Foon Tan *
5d559130eSLey Foon Tan */
6d559130eSLey Foon Tan
7d559130eSLey Foon Tan #include <altera.h>
8d559130eSLey Foon Tan #include <common.h>
9d559130eSLey Foon Tan #include <errno.h>
10d559130eSLey Foon Tan #include <fdtdec.h>
11d559130eSLey Foon Tan #include <miiphy.h>
12d559130eSLey Foon Tan #include <netdev.h>
13d559130eSLey Foon Tan #include <asm/io.h>
14d559130eSLey Foon Tan #include <asm/arch/reset_manager.h>
15d559130eSLey Foon Tan #include <asm/arch/system_manager.h>
16d559130eSLey Foon Tan #include <asm/arch/misc.h>
17d559130eSLey Foon Tan #include <asm/pl310.h>
18d559130eSLey Foon Tan #include <linux/libfdt.h>
19d559130eSLey Foon Tan
20d559130eSLey Foon Tan #include <dt-bindings/reset/altr,rst-mgr-s10.h>
21d559130eSLey Foon Tan
22d559130eSLey Foon Tan DECLARE_GLOBAL_DATA_PTR;
23d559130eSLey Foon Tan
24d559130eSLey Foon Tan static struct socfpga_system_manager *sysmgr_regs =
25d559130eSLey Foon Tan (struct socfpga_system_manager *)SOCFPGA_SYSMGR_ADDRESS;
26d559130eSLey Foon Tan
27d559130eSLey Foon Tan /*
28*877ec6ebSAng, Chee Hong * FPGA programming support for SoC FPGA Stratix 10
29*877ec6ebSAng, Chee Hong */
30*877ec6ebSAng, Chee Hong static Altera_desc altera_fpga[] = {
31*877ec6ebSAng, Chee Hong {
32*877ec6ebSAng, Chee Hong /* Family */
33*877ec6ebSAng, Chee Hong Intel_FPGA_Stratix10,
34*877ec6ebSAng, Chee Hong /* Interface type */
35*877ec6ebSAng, Chee Hong secure_device_manager_mailbox,
36*877ec6ebSAng, Chee Hong /* No limitation as additional data will be ignored */
37*877ec6ebSAng, Chee Hong -1,
38*877ec6ebSAng, Chee Hong /* No device function table */
39*877ec6ebSAng, Chee Hong NULL,
40*877ec6ebSAng, Chee Hong /* Base interface address specified in driver */
41*877ec6ebSAng, Chee Hong NULL,
42*877ec6ebSAng, Chee Hong /* No cookie implementation */
43*877ec6ebSAng, Chee Hong 0
44*877ec6ebSAng, Chee Hong },
45*877ec6ebSAng, Chee Hong };
46*877ec6ebSAng, Chee Hong
47*877ec6ebSAng, Chee Hong /*
48d559130eSLey Foon Tan * DesignWare Ethernet initialization
49d559130eSLey Foon Tan */
50d559130eSLey Foon Tan #ifdef CONFIG_ETH_DESIGNWARE
51d559130eSLey Foon Tan
socfpga_phymode_setup(u32 gmac_index,const char * phymode)52d559130eSLey Foon Tan static u32 socfpga_phymode_setup(u32 gmac_index, const char *phymode)
53d559130eSLey Foon Tan {
54d559130eSLey Foon Tan u32 modereg;
55d559130eSLey Foon Tan
56d559130eSLey Foon Tan if (!phymode)
57d559130eSLey Foon Tan return -EINVAL;
58d559130eSLey Foon Tan
598be11fb3SOoi, Joyce if (!strcmp(phymode, "mii") || !strcmp(phymode, "gmii") ||
608be11fb3SOoi, Joyce !strcmp(phymode, "sgmii"))
61d559130eSLey Foon Tan modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_GMII_MII;
62d559130eSLey Foon Tan else if (!strcmp(phymode, "rgmii"))
63d559130eSLey Foon Tan modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RGMII;
64d559130eSLey Foon Tan else if (!strcmp(phymode, "rmii"))
65d559130eSLey Foon Tan modereg = SYSMGR_EMACGRP_CTRL_PHYSEL_ENUM_RMII;
66d559130eSLey Foon Tan else
67d559130eSLey Foon Tan return -EINVAL;
68d559130eSLey Foon Tan
69d559130eSLey Foon Tan clrsetbits_le32(&sysmgr_regs->emac0 + gmac_index,
70d559130eSLey Foon Tan SYSMGR_EMACGRP_CTRL_PHYSEL_MASK,
71d559130eSLey Foon Tan modereg);
72d559130eSLey Foon Tan
73d559130eSLey Foon Tan return 0;
74d559130eSLey Foon Tan }
75d559130eSLey Foon Tan
socfpga_set_phymode(void)76d559130eSLey Foon Tan static int socfpga_set_phymode(void)
77d559130eSLey Foon Tan {
78d559130eSLey Foon Tan const void *fdt = gd->fdt_blob;
79d559130eSLey Foon Tan struct fdtdec_phandle_args args;
80d559130eSLey Foon Tan const char *phy_mode;
81d559130eSLey Foon Tan u32 gmac_index;
828be11fb3SOoi, Joyce int nodes[3]; /* Max. 3 GMACs */
83d559130eSLey Foon Tan int ret, count;
84d559130eSLey Foon Tan int i, node;
85d559130eSLey Foon Tan
86d559130eSLey Foon Tan count = fdtdec_find_aliases_for_id(fdt, "ethernet",
87d559130eSLey Foon Tan COMPAT_ALTERA_SOCFPGA_DWMAC,
88d559130eSLey Foon Tan nodes, ARRAY_SIZE(nodes));
89d559130eSLey Foon Tan for (i = 0; i < count; i++) {
90d559130eSLey Foon Tan node = nodes[i];
91d559130eSLey Foon Tan if (node <= 0)
92d559130eSLey Foon Tan continue;
93d559130eSLey Foon Tan
94d559130eSLey Foon Tan ret = fdtdec_parse_phandle_with_args(fdt, node, "resets",
95d559130eSLey Foon Tan "#reset-cells", 1, 0,
96d559130eSLey Foon Tan &args);
97d559130eSLey Foon Tan if (ret || args.args_count != 1) {
98d559130eSLey Foon Tan debug("GMAC%i: Failed to parse DT 'resets'!\n", i);
99d559130eSLey Foon Tan continue;
100d559130eSLey Foon Tan }
101d559130eSLey Foon Tan
102d559130eSLey Foon Tan gmac_index = args.args[0] - EMAC0_RESET;
103d559130eSLey Foon Tan
104d559130eSLey Foon Tan phy_mode = fdt_getprop(fdt, node, "phy-mode", NULL);
105d559130eSLey Foon Tan ret = socfpga_phymode_setup(gmac_index, phy_mode);
106d559130eSLey Foon Tan if (ret) {
107d559130eSLey Foon Tan debug("GMAC%i: Failed to parse DT 'phy-mode'!\n", i);
108d559130eSLey Foon Tan continue;
109d559130eSLey Foon Tan }
110d559130eSLey Foon Tan }
111d559130eSLey Foon Tan
112d559130eSLey Foon Tan return 0;
113d559130eSLey Foon Tan }
114d559130eSLey Foon Tan #else
socfpga_set_phymode(void)115d559130eSLey Foon Tan static int socfpga_set_phymode(void)
116d559130eSLey Foon Tan {
117d559130eSLey Foon Tan return 0;
118d559130eSLey Foon Tan };
119d559130eSLey Foon Tan #endif
120d559130eSLey Foon Tan
121d559130eSLey Foon Tan /*
122d559130eSLey Foon Tan * Print CPU information
123d559130eSLey Foon Tan */
124d559130eSLey Foon Tan #if defined(CONFIG_DISPLAY_CPUINFO)
print_cpuinfo(void)125d559130eSLey Foon Tan int print_cpuinfo(void)
126d559130eSLey Foon Tan {
127d559130eSLey Foon Tan puts("CPU: Intel FPGA SoCFPGA Platform (ARMv8 64bit Cortex-A53)\n");
128d559130eSLey Foon Tan
129d559130eSLey Foon Tan return 0;
130d559130eSLey Foon Tan }
131d559130eSLey Foon Tan #endif
132d559130eSLey Foon Tan
133d559130eSLey Foon Tan #ifdef CONFIG_ARCH_MISC_INIT
arch_misc_init(void)134d559130eSLey Foon Tan int arch_misc_init(void)
135d559130eSLey Foon Tan {
136d559130eSLey Foon Tan char qspi_string[13];
137d559130eSLey Foon Tan
138d559130eSLey Foon Tan sprintf(qspi_string, "<0x%08x>", cm_get_qspi_controller_clk_hz());
139d559130eSLey Foon Tan env_set("qspi_clock", qspi_string);
140d559130eSLey Foon Tan
141d559130eSLey Foon Tan socfpga_set_phymode();
142d559130eSLey Foon Tan return 0;
143d559130eSLey Foon Tan }
144d559130eSLey Foon Tan #endif
145d559130eSLey Foon Tan
arch_early_init_r(void)146d559130eSLey Foon Tan int arch_early_init_r(void)
147d559130eSLey Foon Tan {
148*877ec6ebSAng, Chee Hong socfpga_fpga_add(&altera_fpga[0]);
149*877ec6ebSAng, Chee Hong
150d559130eSLey Foon Tan return 0;
151d559130eSLey Foon Tan }
152d559130eSLey Foon Tan
do_bridge_reset(int enable)153d559130eSLey Foon Tan void do_bridge_reset(int enable)
154d559130eSLey Foon Tan {
155d559130eSLey Foon Tan socfpga_bridges_reset(enable);
156d559130eSLey Foon Tan }
157