blob: ba15873414283b4c334bb9bec5ff03a052706696 [file] [log] [blame]
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +08001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2018 NXP
4 * Copyright 2020 Linaro
5 */
6
Sughosh Ganuccb36462022-04-15 11:29:34 +05307#include <efi.h>
8#include <efi_loader.h>
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +08009#include <env.h>
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +080010#include <extension_board.h>
Ying-Chun Liu (PaulLiu)20dc0ba2021-08-24 17:44:19 +080011#include <hang.h>
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +080012#include <i2c.h>
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080013#include <init.h>
14#include <miiphy.h>
15#include <netdev.h>
Fabio Estevamf2acfc72022-04-12 13:05:37 -030016#include <i2c_eeprom.h>
17#include <i2c.h>
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080018
19#include <asm/arch/clock.h>
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +080020#include <asm/arch/imx8mm_pins.h>
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080021#include <asm/arch/sys_proto.h>
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +080022#include <asm/global_data.h>
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080023#include <asm/io.h>
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +080024#include <asm/mach-imx/gpio.h>
25#include <asm/mach-imx/mxc_i2c.h>
26#include <asm/sections.h>
Sughosh Ganuccb36462022-04-15 11:29:34 +053027#include <linux/kernel.h>
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080028
Ying-Chun Liu (PaulLiu)20dc0ba2021-08-24 17:44:19 +080029#include "ddr/ddr.h"
30
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080031DECLARE_GLOBAL_DATA_PTR;
32
Simon Glassb8196212023-02-05 15:39:42 -070033#if IS_ENABLED(CONFIG_EFI_HAVE_CAPSULE_SUPPORT)
Sughosh Ganuccb36462022-04-15 11:29:34 +053034struct efi_fw_image fw_images[] = {
35#if defined(CONFIG_TARGET_IMX8MM_CL_IOT_GATE)
36 {
37 .image_type_id = IMX8MM_CL_IOT_GATE_FIT_IMAGE_GUID,
38 .fw_name = u"IMX8MM-CL-IOT-GATE-FIT",
39 .image_index = 1,
40 },
41#elif defined(CONFIG_TARGET_IMX8MM_CL_IOT_GATE_OPTEE)
42 {
43 .image_type_id = IMX8MM_CL_IOT_GATE_OPTEE_FIT_IMAGE_GUID,
44 .fw_name = u"IMX8MM-CL-IOT-GATE-FIT",
45 .image_index = 1,
46 },
47#endif
48};
49
50struct efi_capsule_update_info update_info = {
51 .dfu_string = "mmc 2=flash-bin raw 0x42 0x1D00 mmcpart 1",
Masahisa Kojima5d2438b2023-06-07 14:41:51 +090052 .num_images = ARRAY_SIZE(fw_images),
Sughosh Ganuccb36462022-04-15 11:29:34 +053053 .images = fw_images,
54};
55
Sughosh Ganuccb36462022-04-15 11:29:34 +053056#endif /* EFI_HAVE_CAPSULE_SUPPORT */
57
Ying-Chun Liu (PaulLiu)20dc0ba2021-08-24 17:44:19 +080058int board_phys_sdram_size(phys_size_t *size)
59{
60 struct lpddr4_tcm_desc *lpddr4_tcm_desc =
61 (struct lpddr4_tcm_desc *)TCM_DATA_CFG;
62
63 switch (lpddr4_tcm_desc->size) {
64 case 4096:
65 case 2048:
66 case 1024:
67 *size = (1L << 20) * lpddr4_tcm_desc->size;
68 break;
69 default:
70 printf("%s: DRAM size %uM is not supported\n",
71 __func__,
72 lpddr4_tcm_desc->size);
73 hang();
74 break;
75 };
76
77 return 0;
78}
79
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +080080/* IOT_GATE-iMX8 extension boards ID */
81typedef enum {
82 IOT_GATE_EXT_EMPTY, /* No extension */
83 IOT_GATE_EXT_CAN, /* CAN bus */
84 IOT_GATE_EXT_IED, /* Bridge */
85 IOT_GATE_EXT_POE, /* POE */
86 IOT_GATE_EXT_POEV2, /* POEv2 */
87} iot_gate_imx8_ext;
88
89typedef enum {
90 IOT_GATE_IMX8_CARD_ID_EMPTY = 0, /* card id - uninhabited */
91 IOT_GATE_IMX8_CARD_ID_DI4O4 = 1, /* Card ID - IED-DI4O4 */
92 IOT_GATE_IMX8_CARD_ID_RS_485 = 2, /* Card ID - IED-RS485 */
93 IOT_GATE_IMX8_CARD_ID_TPM = 3, /* Card ID - IED-TPM */
94 IOT_GATE_IMX8_CARD_ID_CAN = 4, /* Card ID - IED-CAN */
95 IOT_GATE_IMX8_CARD_ID_CL420 = 5, /* Card ID - IED-CL420 */
96 IOT_GATE_IMX8_CARD_ID_RS_232 = 6, /* Card ID - IED-RS232 */
97} iot_gate_imx8_ied_ext;
98
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +080099static int setup_fec(void)
100{
101 if (IS_ENABLED(CONFIG_FEC_MXC)) {
102 struct iomuxc_gpr_base_regs *gpr =
103 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
104
105 /* Use 125M anatop REF_CLK1 for ENET1, not from external */
106 clrsetbits_le32(&gpr->gpr[1], 0x2000, 0);
107 }
108
109 return 0;
110}
111
112int board_phy_config(struct phy_device *phydev)
113{
114 if (IS_ENABLED(CONFIG_FEC_MXC)) {
115 /* enable rgmii rxc skew and phy mode select to RGMII copper */
116 phy_write(phydev, MDIO_DEVAD_NONE, 0x1d, 0x1f);
117 phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0x8);
118
119 phy_write(phydev, MDIO_DEVAD_NONE, 0x1d, 0x00);
120 phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0x82ee);
121 phy_write(phydev, MDIO_DEVAD_NONE, 0x1d, 0x05);
122 phy_write(phydev, MDIO_DEVAD_NONE, 0x1e, 0x100);
123
124 if (phydev->drv->config)
125 phydev->drv->config(phydev);
126 }
127 return 0;
128}
129
130int board_init(void)
131{
132 if (IS_ENABLED(CONFIG_FEC_MXC))
133 setup_fec();
134
135 return 0;
136}
137
138int board_mmc_get_env_dev(int devno)
139{
140 return devno;
141}
142
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +0800143#define IOT_GATE_IMX8_EXT_I2C 3 /* I2C ID of the extension board */
144#define IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM 0x54 /* I2C address of the EEPROM */
145
146/* I2C address of the EEPROM in the POE extension */
147#define IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM_POE 0x50
148#define IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM_POEV2 0x51
149#define IOT_GATE_IMX8_EXT_I2C_ADDR_GPIO 0x22 /* I2C address of the GPIO
150 extender */
151
152static int iot_gate_imx8_ext_id = IOT_GATE_EXT_EMPTY; /* Extension board ID */
153static int iot_gate_imx8_ext_ied_id [3] = {
154 IOT_GATE_IMX8_CARD_ID_EMPTY,
155 IOT_GATE_IMX8_CARD_ID_EMPTY,
156 IOT_GATE_IMX8_CARD_ID_EMPTY };
157
158/*
159 * iot_gate_imx8_detect_ext() - extended board detection
160 * The detection is done according to the detected I2C devices.
161 */
162static void iot_gate_imx8_detect_ext(void)
163{
164 int ret;
165 struct udevice *i2c_bus, *i2c_dev;
166
167 ret = uclass_get_device_by_seq(UCLASS_I2C, IOT_GATE_IMX8_EXT_I2C,
168 &i2c_bus);
169 if (ret) {
170 printf("%s: Failed getting i2c device\n", __func__);
171 return;
172 }
173
174 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM_POE, 0,
175 &i2c_dev);
176 if (!ret) {
177 iot_gate_imx8_ext_id = IOT_GATE_EXT_POE;
178 return;
179 }
180
181 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM_POEV2, 0,
182 &i2c_dev);
183 if (!ret) {
184 iot_gate_imx8_ext_id = IOT_GATE_EXT_POEV2;
185 return;
186 }
187
188 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM, 0,
189 &i2c_dev);
190 if (ret){
191 iot_gate_imx8_ext_id = IOT_GATE_EXT_EMPTY;
192 return;
193 }
194 /* Only the bridge extension includes the GPIO extender */
195 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_GPIO, 0,
196 &i2c_dev);
197 if (ret) /* GPIO extender not detected */
198 iot_gate_imx8_ext_id = IOT_GATE_EXT_CAN;
199 else /* GPIO extender detected */
200 iot_gate_imx8_ext_id = IOT_GATE_EXT_IED;
201}
202
203static iomux_v3_cfg_t const iot_gate_imx8_ext_ied_pads[] = {
204 IMX8MM_PAD_NAND_ALE_GPIO3_IO0 | MUX_PAD_CTRL(PAD_CTL_PE),
205 IMX8MM_PAD_NAND_CE0_B_GPIO3_IO1 | MUX_PAD_CTRL(PAD_CTL_PE),
206 IMX8MM_PAD_NAND_DATA00_GPIO3_IO6 | MUX_PAD_CTRL(PAD_CTL_PE),
207 IMX8MM_PAD_NAND_DATA01_GPIO3_IO7 | MUX_PAD_CTRL(PAD_CTL_PE),
208 IMX8MM_PAD_NAND_DATA02_GPIO3_IO8 | MUX_PAD_CTRL(PAD_CTL_PE),
209 IMX8MM_PAD_NAND_DATA03_GPIO3_IO9 | MUX_PAD_CTRL(PAD_CTL_PE),
210};
211
212static iomux_v3_cfg_t const iot_gate_imx8_ext_poev2_pads[] = {
213 IMX8MM_PAD_SAI3_TXD_GPIO5_IO1 | MUX_PAD_CTRL(PAD_CTL_PE |
214 PAD_CTL_PUE),
215};
216
217/* Extension board bridge GPIOs */
218#define IOT_GATE_IMX8_GPIO_EXT_IED_I0 IMX_GPIO_NR(3, 0) /* IN 0 */
219#define IOT_GATE_IMX8_GPIO_EXT_IED_I1 IMX_GPIO_NR(3, 1) /* IN 1 */
220#define IOT_GATE_IMX8_GPIO_EXT_IED_I2 IMX_GPIO_NR(3, 6) /* IN 2 */
221#define IOT_GATE_IMX8_GPIO_EXT_IED_I3 IMX_GPIO_NR(3, 7) /* IN 3 */
222#define IOT_GATE_IMX8_GPIO_EXT_IED_O0 IMX_GPIO_NR(3, 8) /* OUT 0 */
223#define IOT_GATE_IMX8_GPIO_EXT_IED_O1 IMX_GPIO_NR(3, 9) /* OUT 1 */
224#define IOT_GATE_IMX8_GPIO_EXT_IED_O2 IMX_GPIO_NR(6, 9) /* OUT 2 */
225#define IOT_GATE_IMX8_GPIO_EXT_IED_O3 IMX_GPIO_NR(6, 10)/* OUT 3 */
226
227/* Extension board POE GPIOs */
228#define IOT_GATE_IMX8_GPIO_EXT_POE_MUX IMX_GPIO_NR(5, 1)/* USB_MUX */
229
230/*
231 * iot_gate_imx8_update_pinmux() - update the pinmux
232 * Update the pinmux according to the detected extended board.
233 */
234static void iot_gate_imx8_update_pinmux(void)
235{
236 if (iot_gate_imx8_ext_id == IOT_GATE_EXT_POEV2) {
237 imx_iomux_v3_setup_multiple_pads(iot_gate_imx8_ext_poev2_pads,
238 ARRAY_SIZE(iot_gate_imx8_ext_poev2_pads));
239 gpio_request(IOT_GATE_IMX8_GPIO_EXT_POE_MUX, "poev2_usb-mux");
240 /* Update USB MUX state */
241 gpio_direction_output(IOT_GATE_IMX8_GPIO_EXT_POE_MUX, 1);
242
243 return;
244 }
245 if (iot_gate_imx8_ext_id != IOT_GATE_EXT_IED)
246 return;
247
248 imx_iomux_v3_setup_multiple_pads(iot_gate_imx8_ext_ied_pads,
249 ARRAY_SIZE(iot_gate_imx8_ext_ied_pads));
250
251 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_I0, "ied-di4o4_i0");
252 gpio_direction_input(IOT_GATE_IMX8_GPIO_EXT_IED_I0);
253 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_I1, "ied-di4o4_i1");
254 gpio_direction_input(IOT_GATE_IMX8_GPIO_EXT_IED_I1);
255 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_I2, "ied-di4o4_i2");
256 gpio_direction_input(IOT_GATE_IMX8_GPIO_EXT_IED_I2);
257 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_I3, "ied-di4o4_i3");
258 gpio_direction_input(IOT_GATE_IMX8_GPIO_EXT_IED_I3);
259 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_O0, "ied-di4o4_o0");
260 gpio_direction_output(IOT_GATE_IMX8_GPIO_EXT_IED_O0, 0);
261 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_O1, "ied-di4o4_o1");
262 gpio_direction_output(IOT_GATE_IMX8_GPIO_EXT_IED_O1, 0);
263 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_O2, "ied-di4o4_o2");
264 gpio_direction_output(IOT_GATE_IMX8_GPIO_EXT_IED_O2, 0);
265 gpio_request(IOT_GATE_IMX8_GPIO_EXT_IED_O3, "ied-di4o4_o3");
266 gpio_direction_output(IOT_GATE_IMX8_GPIO_EXT_IED_O3, 0);
267}
268
269#define IOT_GATE_IMX8_GPIO_S0B0 IMX_GPIO_NR(6, 0) /* Slot ID slot 0 bit 0 */
270#define IOT_GATE_IMX8_GPIO_S0B1 IMX_GPIO_NR(6, 1) /* Slot ID slot 0 bit 1 */
271#define IOT_GATE_IMX8_GPIO_S0B2 IMX_GPIO_NR(6, 2) /* Slot ID slot 0 bit 2 */
272#define IOT_GATE_IMX8_GPIO_S1B0 IMX_GPIO_NR(6, 3) /* Slot ID slot 1 bit 0 */
273#define IOT_GATE_IMX8_GPIO_S1B1 IMX_GPIO_NR(6, 4) /* Slot ID slot 1 bit 1 */
274#define IOT_GATE_IMX8_GPIO_S1B2 IMX_GPIO_NR(6, 5) /* Slot ID slot 1 bit 2 */
275#define IOT_GATE_IMX8_GPIO_S2B0 IMX_GPIO_NR(6, 6) /* Slot ID slot 2 bit 0 */
276#define IOT_GATE_IMX8_GPIO_S2B1 IMX_GPIO_NR(6, 7) /* Slot ID slot 2 bit 1 */
277#define IOT_GATE_IMX8_GPIO_S2B2 IMX_GPIO_NR(6, 8) /* Slot ID slot 2 bit 2 */
278
279/*
280 * iot_gate_imx8_update_ext_ied()
281 * Update device tree of the extended board IED-BASE.
282 * The device tree is updated according to the detected sub modules.
283 *
284 * Return 0 for success, 1 for failure.
285 */
286static int iot_gate_imx8_update_ext_ied(void)
287{
288 int revision;
289
290 if (iot_gate_imx8_ext_id != IOT_GATE_EXT_IED)
291 return 0;
292
293 /* ID GPIO initializations */
294 if (gpio_request(IOT_GATE_IMX8_GPIO_S0B0, "id_s0b0") ||
295 gpio_request(IOT_GATE_IMX8_GPIO_S0B1, "id_s0b1") ||
296 gpio_request(IOT_GATE_IMX8_GPIO_S0B2, "id_s0b2") ||
297 gpio_request(IOT_GATE_IMX8_GPIO_S1B0, "id_s1b0") ||
298 gpio_request(IOT_GATE_IMX8_GPIO_S1B1, "id_s1b1") ||
299 gpio_request(IOT_GATE_IMX8_GPIO_S1B2, "id_s1b2") ||
300 gpio_request(IOT_GATE_IMX8_GPIO_S2B0, "id_s2b0") ||
301 gpio_request(IOT_GATE_IMX8_GPIO_S2B1, "id_s2b1") ||
302 gpio_request(IOT_GATE_IMX8_GPIO_S2B2, "id_s2b2")) {
303 printf("%s: ID GPIO request failure\n", __func__);
304 return 1;
305 }
306 gpio_direction_input(IOT_GATE_IMX8_GPIO_S0B0);
307 gpio_direction_input(IOT_GATE_IMX8_GPIO_S0B1);
308 gpio_direction_input(IOT_GATE_IMX8_GPIO_S0B2);
309 gpio_direction_input(IOT_GATE_IMX8_GPIO_S1B0);
310 gpio_direction_input(IOT_GATE_IMX8_GPIO_S1B1);
311 gpio_direction_input(IOT_GATE_IMX8_GPIO_S1B2);
312 gpio_direction_input(IOT_GATE_IMX8_GPIO_S2B0);
313 gpio_direction_input(IOT_GATE_IMX8_GPIO_S2B1);
314 gpio_direction_input(IOT_GATE_IMX8_GPIO_S2B2);
315
316 /* Get slot 0 card ID */
317 revision = gpio_get_value(IOT_GATE_IMX8_GPIO_S0B0) |
318 gpio_get_value(IOT_GATE_IMX8_GPIO_S0B1) << 1 |
319 gpio_get_value(IOT_GATE_IMX8_GPIO_S0B2) << 2;
320 iot_gate_imx8_ext_ied_id[0] = revision;
321
322 /* Get slot 1 card ID */
323 revision = gpio_get_value(IOT_GATE_IMX8_GPIO_S1B0) |
324 gpio_get_value(IOT_GATE_IMX8_GPIO_S1B1) << 1 |
325 gpio_get_value(IOT_GATE_IMX8_GPIO_S1B2) << 2;
326 iot_gate_imx8_ext_ied_id[1] = revision;
327
328 /* Get slot 2 card ID */
329 revision = gpio_get_value(IOT_GATE_IMX8_GPIO_S2B0) |
330 gpio_get_value(IOT_GATE_IMX8_GPIO_S2B1) << 1 |
331 gpio_get_value(IOT_GATE_IMX8_GPIO_S2B2) << 2;
332 iot_gate_imx8_ext_ied_id[2] = revision;
333
334 return 0;
335}
336
Ying-Chun Liu (PaulLiu)a96ed8d2021-11-05 17:13:25 +0800337int extension_board_scan(struct list_head *extension_list)
338{
339 struct extension *extension = NULL;
340 int i;
341 int ret = 0;
342
343 iot_gate_imx8_detect_ext(); /* Extended board detection */
344
345 switch(iot_gate_imx8_ext_id) {
346 case IOT_GATE_EXT_EMPTY:
347 break;
348 case IOT_GATE_EXT_CAN:
349 extension = calloc(1, sizeof(struct extension));
350 snprintf(extension->name, sizeof(extension->name),
351 "IOT_GATE_EXT_CAN");
352 break;
353 case IOT_GATE_EXT_IED:
354 extension = calloc(1, sizeof(struct extension));
355 snprintf(extension->name, sizeof(extension->name),
356 "IOT_GATE_EXT_IED");
357 snprintf(extension->overlay, sizeof(extension->overlay),
358 "imx8mm-cl-iot-gate-ied.dtbo");
359 break;
360 case IOT_GATE_EXT_POE:
361 extension = calloc(1, sizeof(struct extension));
362 snprintf(extension->name, sizeof(extension->name),
363 "IOT_GATE_EXT_POE");
364 break;
365 case IOT_GATE_EXT_POEV2:
366 extension = calloc(1, sizeof(struct extension));
367 snprintf(extension->name, sizeof(extension->name),
368 "IOT_GATE_EXT_POEV2");
369 break;
370 default:
371 printf("IOT_GATE-iMX8 extension board: unknown\n");
372 break;
373 }
374
375 if (extension) {
376 snprintf(extension->owner, sizeof(extension->owner),
377 "Compulab");
378 list_add_tail(&extension->list, extension_list);
379 ret = 1;
380 } else
381 return ret;
382
383 iot_gate_imx8_update_pinmux();
384
385 iot_gate_imx8_update_ext_ied();
386 for (i=0; i<ARRAY_SIZE(iot_gate_imx8_ext_ied_id); i++) {
387 extension = NULL;
388 switch (iot_gate_imx8_ext_ied_id[i]) {
389 case IOT_GATE_IMX8_CARD_ID_EMPTY:
390 break;
391 case IOT_GATE_IMX8_CARD_ID_RS_485:
392 extension = calloc(1, sizeof(struct extension));
393 snprintf(extension->name, sizeof(extension->name),
394 "IOT_GATE_IMX8_CARD_ID_RS_485");
395 break;
396 case IOT_GATE_IMX8_CARD_ID_RS_232:
397 extension = calloc(1, sizeof(struct extension));
398 snprintf(extension->name, sizeof(extension->name),
399 "IOT_GATE_IMX8_CARD_ID_RS_232");
400 break;
401 case IOT_GATE_IMX8_CARD_ID_CAN:
402 extension = calloc(1, sizeof(struct extension));
403 snprintf(extension->name, sizeof(extension->name),
404 "IOT_GATE_IMX8_CARD_ID_CAN");
405 snprintf(extension->overlay, sizeof(extension->overlay),
406 "imx8mm-cl-iot-gate-ied-can%d.dtbo", i);
407 break;
408 case IOT_GATE_IMX8_CARD_ID_TPM:
409 extension = calloc(1, sizeof(struct extension));
410 snprintf(extension->name, sizeof(extension->name),
411 "IOT_GATE_IMX8_CARD_ID_TPM");
412 snprintf(extension->overlay, sizeof(extension->overlay),
413 "imx8mm-cl-iot-gate-ied-tpm%d.dtbo", i);
414 break;
415 case IOT_GATE_IMX8_CARD_ID_CL420:
416 extension = calloc(1, sizeof(struct extension));
417 snprintf(extension->name, sizeof(extension->name),
418 "IOT_GATE_IMX8_CARD_ID_CL420");
419 snprintf(extension->overlay, sizeof(extension->overlay),
420 "imx8mm-cl-iot-gate-ied-can%d.dtbo", i);
421 break;
422 case IOT_GATE_IMX8_CARD_ID_DI4O4:
423 extension = calloc(1, sizeof(struct extension));
424 snprintf(extension->name, sizeof(extension->name),
425 "IOT_GATE_IMX8_CARD_ID_DI4O4");
426 break;
427 default:
428 printf("%s: invalid slot %d card ID: %d\n",
429 __func__, i, iot_gate_imx8_ext_ied_id[i]);
430 break;
431 }
432 if (extension) {
433 snprintf(extension->owner, sizeof(extension->owner),
434 "Compulab");
435 snprintf(extension->other, sizeof(extension->other),
436 "On slot %d", i);
437 list_add_tail(&extension->list, extension_list);
438 ret = ret + 1;
439 }
440 }
441
442 return ret;
443}
444
Fabio Estevamf2acfc72022-04-12 13:05:37 -0300445static int setup_mac_address(void)
446{
447 unsigned char enetaddr[6];
448 struct udevice *dev;
449 int ret, off;
450
451 ret = eth_env_get_enetaddr("ethaddr", enetaddr);
452 if (ret)
453 return 0;
454
455 off = fdt_path_offset(gd->fdt_blob, "eeprom1");
456 if (off < 0) {
457 printf("No eeprom0 path offset found in DT\n");
458 return off;
459 }
460
461 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
462 if (ret) {
463 printf("%s: Could not find EEPROM\n", __func__);
464 return ret;
465 }
466
467 ret = i2c_set_chip_offset_len(dev, 1);
468 if (ret)
469 return ret;
470
471 ret = i2c_eeprom_read(dev, 4, enetaddr, sizeof(enetaddr));
472 if (ret) {
473 printf("%s: Could not read EEPROM\n", __func__);
474 return ret;
475 }
476
477 ret = is_valid_ethaddr(enetaddr);
478 if (!ret)
479 return -EINVAL;
480
481 ret = eth_env_set_enetaddr("ethaddr", enetaddr);
482 if (ret)
483 return ret;
484
485 return 0;
486}
487
Fabio Estevam938f70f2022-04-12 13:05:38 -0300488static int read_serial_number(void)
489{
490 unsigned char serialnumber[6];
491 unsigned char reversed[6];
492 char serial_string[12];
493 struct udevice *dev;
494 int ret, off, i;
495
496 off = fdt_path_offset(gd->fdt_blob, "eeprom0");
497 if (off < 0) {
498 printf("No eeprom0 path offset found in DT\n");
499 return off;
500 }
501
502 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
503 if (ret) {
504 printf("%s: Could not find EEPROM\n", __func__);
505 return ret;
506 }
507
508 ret = i2c_set_chip_offset_len(dev, 1);
509 if (ret)
510 return ret;
511
512 ret = i2c_eeprom_read(dev, 0x14, serialnumber, sizeof(serialnumber));
513 if (ret) {
514 printf("%s: Could not read EEPROM\n", __func__);
515 return ret;
516 }
517
518 for (i = sizeof(serialnumber) - 1; i >= 0; i--)
519 reversed[i] = serialnumber[sizeof(serialnumber) - 1 - i];
520
521 for (i = 0; i < sizeof(reversed); i++) {
522 serial_string[i * 2] = (reversed[i] >> 4) & 0xf;
523 serial_string[i * 2 + 1] = reversed[i] & 0xf;
524 }
525
526 for (i = 0; i < sizeof(serial_string); i++)
527 serial_string[i] += '0';
528
529 env_set("serial#", serial_string);
530
531 return 0;
532}
533
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +0800534int board_late_init(void)
535{
Fabio Estevamf2acfc72022-04-12 13:05:37 -0300536 int ret;
537
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +0800538 if (IS_ENABLED(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)) {
539 env_set("board_name", "IOT-GATE-IMX8");
540 env_set("board_rev", "SBC-IOTMX8");
541 }
542
Fabio Estevamf2acfc72022-04-12 13:05:37 -0300543 ret = setup_mac_address();
544 if (ret < 0)
545 printf("Cannot set MAC address from EEPROM\n");
546
Fabio Estevam938f70f2022-04-12 13:05:38 -0300547 ret = read_serial_number();
548 if (ret < 0)
549 printf("Cannot read serial number from EEPROM\n");
550
Ying-Chun Liu (PaulLiu)a97107f2021-04-22 04:50:31 +0800551 return 0;
552}