1 // SPDX-License-Identifier: GPL-2.0+
4 * Copyright 2020 Linaro
9 #include <efi_loader.h>
11 #include <extension_board.h>
17 #include <i2c_eeprom.h>
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>
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>
32 DECLARE_GLOBAL_DATA_PTR;
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)
38 .image_type_id = IMX8MM_CL_IOT_GATE_FIT_IMAGE_GUID,
39 .fw_name = u"IMX8MM-CL-IOT-GATE-FIT",
42 #elif defined(CONFIG_TARGET_IMX8MM_CL_IOT_GATE_OPTEE)
44 .image_type_id = IMX8MM_CL_IOT_GATE_OPTEE_FIT_IMAGE_GUID,
45 .fw_name = u"IMX8MM-CL-IOT-GATE-FIT",
51 struct efi_capsule_update_info update_info = {
52 .dfu_string = "mmc 2=flash-bin raw 0x42 0x1D00 mmcpart 1",
56 u8 num_image_type_guids = ARRAY_SIZE(fw_images);
57 #endif /* EFI_HAVE_CAPSULE_SUPPORT */
59 int board_phys_sdram_size(phys_size_t *size)
61 struct lpddr4_tcm_desc *lpddr4_tcm_desc =
62 (struct lpddr4_tcm_desc *)TCM_DATA_CFG;
64 switch (lpddr4_tcm_desc->size) {
68 *size = (1L << 20) * lpddr4_tcm_desc->size;
71 printf("%s: DRAM size %uM is not supported\n",
73 lpddr4_tcm_desc->size);
81 /* IOT_GATE-iMX8 extension boards ID */
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 */
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;
100 static int setup_fec(void)
102 if (IS_ENABLED(CONFIG_FEC_MXC)) {
103 struct iomuxc_gpr_base_regs *gpr =
104 (struct iomuxc_gpr_base_regs *)IOMUXC_GPR_BASE_ADDR;
106 /* Use 125M anatop REF_CLK1 for ENET1, not from external */
107 clrsetbits_le32(&gpr->gpr[1], 0x2000, 0);
113 int board_phy_config(struct phy_device *phydev)
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);
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);
125 if (phydev->drv->config)
126 phydev->drv->config(phydev);
133 if (IS_ENABLED(CONFIG_FEC_MXC))
139 int board_mmc_get_env_dev(int devno)
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 */
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
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 };
160 * iot_gate_imx8_detect_ext() - extended board detection
161 * The detection is done according to the detected I2C devices.
163 static void iot_gate_imx8_detect_ext(void)
166 struct udevice *i2c_bus, *i2c_dev;
168 ret = uclass_get_device_by_seq(UCLASS_I2C, IOT_GATE_IMX8_EXT_I2C,
171 printf("%s: Failed getting i2c device\n", __func__);
175 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM_POE, 0,
178 iot_gate_imx8_ext_id = IOT_GATE_EXT_POE;
182 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM_POEV2, 0,
185 iot_gate_imx8_ext_id = IOT_GATE_EXT_POEV2;
189 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_EEPROM, 0,
192 iot_gate_imx8_ext_id = IOT_GATE_EXT_EMPTY;
195 /* Only the bridge extension includes the GPIO extender */
196 ret = dm_i2c_probe(i2c_bus, IOT_GATE_IMX8_EXT_I2C_ADDR_GPIO, 0,
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;
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),
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 |
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 */
228 /* Extension board POE GPIOs */
229 #define IOT_GATE_IMX8_GPIO_EXT_POE_MUX IMX_GPIO_NR(5, 1)/* USB_MUX */
232 * iot_gate_imx8_update_pinmux() - update the pinmux
233 * Update the pinmux according to the detected extended board.
235 static void iot_gate_imx8_update_pinmux(void)
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);
246 if (iot_gate_imx8_ext_id != IOT_GATE_EXT_IED)
249 imx_iomux_v3_setup_multiple_pads(iot_gate_imx8_ext_ied_pads,
250 ARRAY_SIZE(iot_gate_imx8_ext_ied_pads));
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);
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 */
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.
285 * Return 0 for success, 1 for failure.
287 static int iot_gate_imx8_update_ext_ied(void)
291 if (iot_gate_imx8_ext_id != IOT_GATE_EXT_IED)
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__);
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);
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;
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;
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;
338 int board_fix_fdt(void *rw_fdt_blob)
343 int extension_board_scan(struct list_head *extension_list)
345 struct extension *extension = NULL;
349 iot_gate_imx8_detect_ext(); /* Extended board detection */
351 switch(iot_gate_imx8_ext_id) {
352 case IOT_GATE_EXT_EMPTY:
354 case IOT_GATE_EXT_CAN:
355 extension = calloc(1, sizeof(struct extension));
356 snprintf(extension->name, sizeof(extension->name),
359 case IOT_GATE_EXT_IED:
360 extension = calloc(1, sizeof(struct extension));
361 snprintf(extension->name, sizeof(extension->name),
363 snprintf(extension->overlay, sizeof(extension->overlay),
364 "imx8mm-cl-iot-gate-ied.dtbo");
366 case IOT_GATE_EXT_POE:
367 extension = calloc(1, sizeof(struct extension));
368 snprintf(extension->name, sizeof(extension->name),
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");
377 printf("IOT_GATE-iMX8 extension board: unknown\n");
382 snprintf(extension->owner, sizeof(extension->owner),
384 list_add_tail(&extension->list, extension_list);
389 iot_gate_imx8_update_pinmux();
391 iot_gate_imx8_update_ext_ied();
392 for (i=0; i<ARRAY_SIZE(iot_gate_imx8_ext_ied_id); i++) {
394 switch (iot_gate_imx8_ext_ied_id[i]) {
395 case IOT_GATE_IMX8_CARD_ID_EMPTY:
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");
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");
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);
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);
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);
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");
434 printf("%s: invalid slot %d card ID: %d\n",
435 __func__, i, iot_gate_imx8_ext_ied_id[i]);
439 snprintf(extension->owner, sizeof(extension->owner),
441 snprintf(extension->other, sizeof(extension->other),
443 list_add_tail(&extension->list, extension_list);
451 static int setup_mac_address(void)
453 unsigned char enetaddr[6];
457 ret = eth_env_get_enetaddr("ethaddr", enetaddr);
461 off = fdt_path_offset(gd->fdt_blob, "eeprom1");
463 printf("No eeprom0 path offset found in DT\n");
467 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
469 printf("%s: Could not find EEPROM\n", __func__);
473 ret = i2c_set_chip_offset_len(dev, 1);
477 ret = i2c_eeprom_read(dev, 4, enetaddr, sizeof(enetaddr));
479 printf("%s: Could not read EEPROM\n", __func__);
483 ret = is_valid_ethaddr(enetaddr);
487 ret = eth_env_set_enetaddr("ethaddr", enetaddr);
494 static int read_serial_number(void)
496 unsigned char serialnumber[6];
497 unsigned char reversed[6];
498 char serial_string[12];
502 off = fdt_path_offset(gd->fdt_blob, "eeprom0");
504 printf("No eeprom0 path offset found in DT\n");
508 ret = uclass_get_device_by_of_offset(UCLASS_I2C_EEPROM, off, &dev);
510 printf("%s: Could not find EEPROM\n", __func__);
514 ret = i2c_set_chip_offset_len(dev, 1);
518 ret = i2c_eeprom_read(dev, 0x14, serialnumber, sizeof(serialnumber));
520 printf("%s: Could not read EEPROM\n", __func__);
524 for (i = sizeof(serialnumber) - 1; i >= 0; i--)
525 reversed[i] = serialnumber[sizeof(serialnumber) - 1 - i];
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;
532 for (i = 0; i < sizeof(serial_string); i++)
533 serial_string[i] += '0';
535 env_set("serial#", serial_string);
540 int board_late_init(void)
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");
549 ret = setup_mac_address();
551 printf("Cannot set MAC address from EEPROM\n");
553 ret = read_serial_number();
555 printf("Cannot read serial number from EEPROM\n");