board: gateworks: venice: add imx8mp-venice-gw740x support
[platform/kernel/u-boot.git] / board / gateworks / venice / venice.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright 2021 Gateworks Corporation
4  */
5
6 #include <init.h>
7 #include <led.h>
8 #include <miiphy.h>
9 #include <asm/arch/clock.h>
10 #include <asm/arch/sys_proto.h>
11
12 #include "eeprom.h"
13
14 int board_phys_sdram_size(phys_size_t *size)
15 {
16         if (!size)
17                 return -EINVAL;
18
19         *size = get_ram_size((void *)PHYS_SDRAM, PHYS_SDRAM_SIZE);
20
21         return 0;
22 }
23
24 int board_fit_config_name_match(const char *name)
25 {
26         int i  = 0;
27         const char *dtb;
28         static char init;
29         char buf[32];
30
31         do {
32                 dtb = eeprom_get_dtb_name(i++, buf, sizeof(buf));
33                 if (!strcmp(dtb, name)) {
34                         if (!init++)
35                                 printf("DTB     : %s\n", name);
36                         return 0;
37                 }
38         } while (dtb);
39
40         return -1;
41 }
42
43 #if (IS_ENABLED(CONFIG_NET))
44 static int setup_fec(void)
45 {
46         struct iomuxc_gpr_base_regs *gpr =
47                 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
48
49 #ifndef CONFIG_IMX8MP
50         /* Use 125M anatop REF_CLK1 for ENET1, not from external */
51         clrsetbits_le32(&gpr->gpr[1], 0x2000, 0);
52 #else
53         /* Enable RGMII TX clk output */
54         setbits_le32(&gpr->gpr[1], BIT(22));
55 #endif
56
57         return 0;
58 }
59
60 static int setup_eqos(void)
61 {
62         struct iomuxc_gpr_base_regs *gpr =
63                 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
64
65         /* set INTF as RGMII, enable RGMII TXC clock */
66         clrsetbits_le32(&gpr->gpr[1],
67                         IOMUXC_GPR_GPR1_GPR_ENET_QOS_INTF_SEL_MASK, BIT(16));
68         setbits_le32(&gpr->gpr[1], BIT(19) | BIT(21));
69
70         return set_clk_eqos(ENET_125MHZ);
71 }
72
73 int board_phy_config(struct phy_device *phydev)
74 {
75         unsigned short val;
76         ofnode node;
77
78         switch (phydev->phy_id) {
79         case 0x2000a231: /* TI DP83867 GbE PHY */
80                 puts("DP83867 ");
81                 /* LED configuration */
82                 val = 0;
83                 val |= 0x5 << 4; /* LED1(Amber;Speed)   : 1000BT link */
84                 val |= 0xb << 8; /* LED2(Green;Link/Act): blink for TX/RX act */
85                 phy_write(phydev, MDIO_DEVAD_NONE, 24, val);
86                 break;
87         case 0xd565a401: /* MaxLinear GPY111 */
88                 puts("GPY111 ");
89                 node = phy_get_ofnode(phydev);
90                 if (ofnode_valid(node)) {
91                         u32 rx_delay, tx_delay;
92
93                         rx_delay = ofnode_read_u32_default(node, "rx-internal-delay-ps", 2000);
94                         tx_delay = ofnode_read_u32_default(node, "tx-internal-delay-ps", 2000);
95                         val = phy_read(phydev, MDIO_DEVAD_NONE, 0x17);
96                         val &= ~((0x7 << 12) | (0x7 << 8));
97                         val |= (rx_delay / 500) << 12;
98                         val |= (tx_delay / 500) << 8;
99                         phy_write(phydev, MDIO_DEVAD_NONE, 0x17, val);
100                 }
101                 break;
102         }
103
104         if (phydev->drv->config)
105                 phydev->drv->config(phydev);
106
107         return 0;
108 }
109 #endif // IS_ENABLED(CONFIG_NET)
110
111 int board_init(void)
112 {
113         eeprom_init(1);
114
115         if (IS_ENABLED(CONFIG_FEC_MXC))
116                 setup_fec();
117         if (IS_ENABLED(CONFIG_DWC_ETH_QOS))
118                 setup_eqos();
119
120         return 0;
121 }
122
123 int board_late_init(void)
124 {
125         const char *str;
126         char env[32];
127         int ret, i;
128         u8 enetaddr[6];
129         char fdt[64];
130
131         led_default_state();
132
133         /* Set board serial/model */
134         if (!env_get("serial#"))
135                 env_set_ulong("serial#", eeprom_get_serial());
136         env_set("model", eeprom_get_model());
137
138         /* Set fdt_file vars */
139         i = 0;
140         do {
141                 str = eeprom_get_dtb_name(i, fdt, sizeof(fdt));
142                 if (str) {
143                         sprintf(env, "fdt_file%d", i + 1);
144                         strcat(fdt, ".dtb");
145                         env_set(env, fdt);
146                 }
147                 i++;
148         } while (str);
149
150         /* Set mac addrs */
151         i = 0;
152         do {
153                 if (i)
154                         sprintf(env, "eth%daddr", i);
155                 else
156                         sprintf(env, "ethaddr");
157                 str = env_get(env);
158                 if (!str) {
159                         ret = eeprom_getmac(i, enetaddr);
160                         if (!ret)
161                                 eth_env_set_enetaddr(env, enetaddr);
162                 }
163                 i++;
164         } while (!ret);
165
166         return 0;
167 }
168
169 int board_mmc_get_env_dev(int devno)
170 {
171         return devno;
172 }
173
174 int ft_board_setup(void *blob, struct bd_info *bd)
175 {
176         int off;
177
178         /* set board model dt prop */
179         fdt_setprop_string(blob, 0, "board", eeprom_get_model());
180
181         /* update temp thresholds */
182         off = fdt_path_offset(blob, "/thermal-zones/cpu-thermal/trips");
183         if (off >= 0) {
184                 int minc, maxc, prop;
185
186                 get_cpu_temp_grade(&minc, &maxc);
187                 fdt_for_each_subnode(prop, blob, off) {
188                         const char *type = fdt_getprop(blob, prop, "type", NULL);
189
190                         if (type && (!strcmp("critical", type)))
191                                 fdt_setprop_u32(blob, prop, "temperature", maxc * 1000);
192                         else if (type && (!strcmp("passive", type)))
193                                 fdt_setprop_u32(blob, prop, "temperature", (maxc - 10) * 1000);
194                 }
195         }
196
197         return 0;
198 }