blob: 803582c55b99ddc13ddf23af05e30d73b6ff866f [file] [log] [blame]
Tim Harvey256dba02021-03-02 14:00:21 -08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2021 Gateworks Corporation
4 */
5
Tim Harveya6614bb2022-08-11 11:55:38 -07006#include <fdt_support.h>
Tim Harvey256dba02021-03-02 14:00:21 -08007#include <init.h>
8#include <led.h>
Tim Harveya32dd712023-05-02 17:05:55 -07009#include <mmc.h>
Tim Harvey256dba02021-03-02 14:00:21 -080010#include <miiphy.h>
Tim Harvey0de4f2c2023-05-02 17:05:54 -070011#include <mmc.h>
Tim Harvey0f5717f2022-04-13 11:31:09 -070012#include <asm/arch/clock.h>
Tim Harvey256dba02021-03-02 14:00:21 -080013#include <asm/arch/sys_proto.h>
Tim Harveya32dd712023-05-02 17:05:55 -070014#include <asm/mach-imx/boot_mode.h>
Tim Harvey256dba02021-03-02 14:00:21 -080015
Tim Harveyd4daeaa2022-04-13 08:56:40 -070016#include "eeprom.h"
Tim Harvey256dba02021-03-02 14:00:21 -080017
18int board_phys_sdram_size(phys_size_t *size)
19{
Tim Harvey56c5e312022-03-30 13:39:02 -070020 if (!size)
Tim Harvey195a1612021-07-27 15:19:37 -070021 return -EINVAL;
Tim Harvey256dba02021-03-02 14:00:21 -080022
Tim Harvey56c5e312022-03-30 13:39:02 -070023 *size = get_ram_size((void *)PHYS_SDRAM, PHYS_SDRAM_SIZE);
Tim Harvey256dba02021-03-02 14:00:21 -080024
25 return 0;
26}
27
28int board_fit_config_name_match(const char *name)
29{
30 int i = 0;
31 const char *dtb;
Tim Harvey637b8b12021-06-30 17:07:40 -070032 static char init;
Tim Harvey256dba02021-03-02 14:00:21 -080033 char buf[32];
34
35 do {
Tim Harveyd4daeaa2022-04-13 08:56:40 -070036 dtb = eeprom_get_dtb_name(i++, buf, sizeof(buf));
Tim Harvey637b8b12021-06-30 17:07:40 -070037 if (!strcmp(dtb, name)) {
38 if (!init++)
39 printf("DTB : %s\n", name);
Tim Harvey256dba02021-03-02 14:00:21 -080040 return 0;
Tim Harvey637b8b12021-06-30 17:07:40 -070041 }
Tim Harvey256dba02021-03-02 14:00:21 -080042 } while (dtb);
43
44 return -1;
45}
46
Simon Glassa2d42102023-02-22 09:34:24 -070047static int __maybe_unused setup_fec(void)
Tim Harvey256dba02021-03-02 14:00:21 -080048{
49 struct iomuxc_gpr_base_regs *gpr =
50 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
51
Tim Harvey0f5717f2022-04-13 11:31:09 -070052#ifndef CONFIG_IMX8MP
Tim Harvey256dba02021-03-02 14:00:21 -080053 /* Use 125M anatop REF_CLK1 for ENET1, not from external */
54 clrsetbits_le32(&gpr->gpr[1], 0x2000, 0);
Tim Harvey0f5717f2022-04-13 11:31:09 -070055#else
56 /* Enable RGMII TX clk output */
57 setbits_le32(&gpr->gpr[1], BIT(22));
58#endif
Tim Harvey256dba02021-03-02 14:00:21 -080059
60 return 0;
61}
62
Simon Glassa2d42102023-02-22 09:34:24 -070063#if (IS_ENABLED(CONFIG_NET))
Tim Harvey256dba02021-03-02 14:00:21 -080064int board_phy_config(struct phy_device *phydev)
65{
66 unsigned short val;
67
68 switch (phydev->phy_id) {
69 case 0x2000a231: /* TI DP83867 GbE PHY */
70 puts("DP83867 ");
71 /* LED configuration */
72 val = 0;
73 val |= 0x5 << 4; /* LED1(Amber;Speed) : 1000BT link */
74 val |= 0xb << 8; /* LED2(Green;Link/Act): blink for TX/RX act */
75 phy_write(phydev, MDIO_DEVAD_NONE, 24, val);
76 break;
77 }
78
79 if (phydev->drv->config)
80 phydev->drv->config(phydev);
81
82 return 0;
83}
Tim Harvey0f5717f2022-04-13 11:31:09 -070084#endif // IS_ENABLED(CONFIG_NET)
Tim Harvey256dba02021-03-02 14:00:21 -080085
86int board_init(void)
87{
Tim Harvey1fec1822022-08-11 12:04:01 -070088 venice_eeprom_init(1);
Tim Harvey256dba02021-03-02 14:00:21 -080089
90 if (IS_ENABLED(CONFIG_FEC_MXC))
91 setup_fec();
92
Tim Harvey256dba02021-03-02 14:00:21 -080093 return 0;
94}
95
96int board_late_init(void)
97{
Tim Harvey68901572021-06-30 17:07:41 -070098 const char *str;
Tim Harveya32dd712023-05-02 17:05:55 -070099 struct mmc *mmc = NULL;
Tim Harvey256dba02021-03-02 14:00:21 -0800100 char env[32];
101 int ret, i;
102 u8 enetaddr[6];
Tim Harvey68901572021-06-30 17:07:41 -0700103 char fdt[64];
Tim Harveya32dd712023-05-02 17:05:55 -0700104 int bootdev;
Tim Harvey256dba02021-03-02 14:00:21 -0800105
Tim Harvey78b8e072021-07-27 15:19:39 -0700106 /* Set board serial/model */
Tim Harvey62a13192021-08-18 15:24:28 -0700107 if (!env_get("serial#"))
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700108 env_set_ulong("serial#", eeprom_get_serial());
109 env_set("model", eeprom_get_model());
Tim Harvey78b8e072021-07-27 15:19:39 -0700110
Tim Harvey68901572021-06-30 17:07:41 -0700111 /* Set fdt_file vars */
112 i = 0;
113 do {
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700114 str = eeprom_get_dtb_name(i, fdt, sizeof(fdt));
Tim Harvey68901572021-06-30 17:07:41 -0700115 if (str) {
116 sprintf(env, "fdt_file%d", i + 1);
117 strcat(fdt, ".dtb");
118 env_set(env, fdt);
119 }
120 i++;
121 } while (str);
122
Tim Harvey256dba02021-03-02 14:00:21 -0800123 /* Set mac addrs */
124 i = 0;
125 do {
126 if (i)
127 sprintf(env, "eth%daddr", i);
128 else
129 sprintf(env, "ethaddr");
Tim Harvey68901572021-06-30 17:07:41 -0700130 str = env_get(env);
131 if (!str) {
Tim Harveyd4daeaa2022-04-13 08:56:40 -0700132 ret = eeprom_getmac(i, enetaddr);
Tim Harvey256dba02021-03-02 14:00:21 -0800133 if (!ret)
134 eth_env_set_enetaddr(env, enetaddr);
135 }
136 i++;
137 } while (!ret);
138
Tim Harveya32dd712023-05-02 17:05:55 -0700139 /*
140 * set bootdev/bootblk/bootpart (used in firmware_update script)
141 * dynamically depending on boot device and SoC
142 */
143 bootdev = -1;
144 switch (get_boot_device()) {
145 case SD1_BOOT:
146 case MMC1_BOOT: /* SDHC1 */
147 bootdev = 0;
148 break;
149 case SD2_BOOT:
150 case MMC2_BOOT: /* SDHC2 */
151 bootdev = 1;
152 break;
153 case SD3_BOOT:
154 case MMC3_BOOT: /* SDHC3 */
155 bootdev = 2;
156 break;
157 default:
158 break;
159 }
160 if (bootdev != -1)
161 mmc = find_mmc_device(bootdev);
162 if (mmc) {
163 int bootblk;
164
165 if (IS_ENABLED(CONFIG_IMX8MN) || IS_ENABLED(CONFIG_IMX8MP))
166 bootblk = 32 * SZ_1K / 512;
167 else
168 bootblk = 33 * SZ_1K / 512;
169 mmc_init(mmc);
170 if (!IS_SD(mmc)) {
171 int bootpart;
172
173 switch (EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config)) {
174 case 1: /* boot0 */
175 bootpart = 1;
176 break;
177 case 2: /* boot1 */
178 bootpart = 2;
179 break;
180 case 7: /* user */
181 default:
182 bootpart = 0;
183 break;
184 }
185 /* IMX8MP/IMX8MN BOOTROM v2 uses offset=0 for boot parts */
186 if ((IS_ENABLED(CONFIG_IMX8MN) || IS_ENABLED(CONFIG_IMX8MP)) &&
187 (bootpart == 1 || bootpart == 2))
188 bootblk = 0;
189 env_set_hex("bootpart", bootpart);
190 env_set_hex("bootblk", bootblk);
191 } else { /* SD */
192 env_set("bootpart", "");
193 env_set_hex("bootblk", bootblk);
194 }
195 env_set_hex("dev", bootdev);
196 }
197
198 /* override soc=imx8m to provide a more specific soc name */
199 if (IS_ENABLED(CONFIG_IMX8MN))
200 env_set("soc", "imx8mn");
201 else if (IS_ENABLED(CONFIG_IMX8MP))
202 env_set("soc", "imx8mp");
203 else if (IS_ENABLED(CONFIG_IMX8MM))
204 env_set("soc", "imx8mm");
205
Tim Harvey256dba02021-03-02 14:00:21 -0800206 return 0;
207}
208
209int board_mmc_get_env_dev(int devno)
210{
211 return devno;
212}
Tim Harvey60c1bfd2021-07-27 15:19:40 -0700213
Tim Harvey0de4f2c2023-05-02 17:05:54 -0700214uint mmc_get_env_part(struct mmc *mmc)
215{
216 if (!IS_SD(mmc)) {
217 switch (EXT_CSD_EXTRACT_BOOT_PART(mmc->part_config)) {
218 case 1:
219 return 1;
220 case 2:
221 return 2;
222 }
223 }
224
225 return 0;
226}
227
Tim Harveya6614bb2022-08-11 11:55:38 -0700228int ft_board_setup(void *fdt, struct bd_info *bd)
Tim Harvey60c1bfd2021-07-27 15:19:40 -0700229{
Tim Harveya6614bb2022-08-11 11:55:38 -0700230 const char *base_model = eeprom_get_baseboard_model();
231 char pcbrev;
Tim Harvey547f6aa2021-08-18 15:24:30 -0700232 int off;
233
Tim Harvey60c1bfd2021-07-27 15:19:40 -0700234 /* set board model dt prop */
Tim Harveya6614bb2022-08-11 11:55:38 -0700235 fdt_setprop_string(fdt, 0, "board", eeprom_get_model());
Tim Harvey60c1bfd2021-07-27 15:19:40 -0700236
Tim Harveya6614bb2022-08-11 11:55:38 -0700237 if (!strncmp(base_model, "GW73", 4)) {
238 pcbrev = get_pcb_rev(base_model);
239
240 if (pcbrev > 'B') {
241 printf("adjusting dt for %s\n", base_model);
242
243 /*
244 * revC replaced PCIe 5-port switch with 4-port
245 * which changed ethernet1 PCIe GbE
246 * from: pcie@0,0/pcie@1,0/pcie@2,4/pcie@6.0
247 * to: pcie@0,0/pcie@1,0/pcie@2,3/pcie@5.0
248 */
249 off = fdt_path_offset(fdt, "ethernet1");
250 if (off > 0) {
251 u32 reg[5];
252
253 fdt_set_name(fdt, off, "pcie@5,0");
254 off = fdt_parent_offset(fdt, off);
255 fdt_set_name(fdt, off, "pcie@2,3");
256 memset(reg, 0, sizeof(reg));
257 reg[0] = cpu_to_fdt32(PCI_DEVFN(3, 0));
258 fdt_setprop(fdt, off, "reg", reg, sizeof(reg));
259 }
Tim Harvey547f6aa2021-08-18 15:24:30 -0700260 }
261 }
262
Tim Harvey60c1bfd2021-07-27 15:19:40 -0700263 return 0;
264}