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