Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | /* |
| 3 | * Copyright (C) 2024 PHYTEC Messtechnik GmbH |
| 4 | * Author: Wadim Egorov <w.egorov@phytec.de> |
| 5 | */ |
| 6 | |
| 7 | #include <env_internal.h> |
Wadim Egorov | 4baf2a6 | 2024-06-10 15:33:42 +0200 | [diff] [blame] | 8 | #include <fdt_support.h> |
Wadim Egorov | 94d9252 | 2024-10-30 17:48:15 +0100 | [diff] [blame^] | 9 | #include <dm/ofnode.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 10 | #include <spl.h> |
Wadim Egorov | 94d9252 | 2024-10-30 17:48:15 +0100 | [diff] [blame^] | 11 | #include <malloc.h> |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 12 | #include <asm/arch/hardware.h> |
| 13 | |
Daniel Schultz | 80677cb | 2024-05-21 23:18:26 -0700 | [diff] [blame] | 14 | #include "../am6_som_detection.h" |
| 15 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 16 | #if IS_ENABLED(CONFIG_ENV_IS_IN_FAT) || IS_ENABLED(CONFIG_ENV_IS_IN_MMC) |
| 17 | int mmc_get_env_dev(void) |
| 18 | { |
| 19 | u32 boot_device = get_boot_device(); |
| 20 | |
| 21 | switch (boot_device) { |
| 22 | case BOOT_DEVICE_MMC1: |
| 23 | return 0; |
| 24 | case BOOT_DEVICE_MMC2: |
| 25 | return 1; |
| 26 | }; |
| 27 | |
| 28 | return CONFIG_SYS_MMC_ENV_DEV; |
| 29 | } |
| 30 | #endif |
| 31 | |
| 32 | enum env_location env_get_location(enum env_operation op, int prio) |
| 33 | { |
| 34 | u32 boot_device = get_boot_device(); |
| 35 | |
| 36 | if (prio) |
| 37 | return ENVL_UNKNOWN; |
| 38 | |
| 39 | switch (boot_device) { |
| 40 | case BOOT_DEVICE_MMC1: |
| 41 | case BOOT_DEVICE_MMC2: |
| 42 | if (CONFIG_IS_ENABLED(ENV_IS_IN_FAT)) |
| 43 | return ENVL_FAT; |
| 44 | if (CONFIG_IS_ENABLED(ENV_IS_IN_MMC)) |
| 45 | return ENVL_MMC; |
| 46 | case BOOT_DEVICE_SPI: |
| 47 | if (CONFIG_IS_ENABLED(ENV_IS_IN_SPI_FLASH)) |
| 48 | return ENVL_SPI_FLASH; |
| 49 | default: |
| 50 | return ENVL_NOWHERE; |
| 51 | }; |
| 52 | } |
| 53 | |
| 54 | #if IS_ENABLED(CONFIG_BOARD_LATE_INIT) |
| 55 | int board_late_init(void) |
| 56 | { |
| 57 | u32 boot_device = get_boot_device(); |
| 58 | |
| 59 | switch (boot_device) { |
| 60 | case BOOT_DEVICE_MMC1: |
| 61 | env_set_ulong("mmcdev", 0); |
| 62 | env_set("boot", "mmc"); |
| 63 | break; |
| 64 | case BOOT_DEVICE_MMC2: |
| 65 | env_set_ulong("mmcdev", 1); |
| 66 | env_set("boot", "mmc"); |
| 67 | break; |
| 68 | case BOOT_DEVICE_SPI: |
| 69 | env_set("boot", "spi"); |
| 70 | break; |
| 71 | case BOOT_DEVICE_ETHERNET: |
| 72 | env_set("boot", "net"); |
| 73 | break; |
| 74 | }; |
| 75 | |
Daniel Schultz | 80677cb | 2024-05-21 23:18:26 -0700 | [diff] [blame] | 76 | if (IS_ENABLED(CONFIG_PHYTEC_SOM_DETECTION_BLOCKS)) { |
| 77 | struct phytec_api3_element *block_element; |
| 78 | struct phytec_eeprom_data data; |
| 79 | int ret; |
| 80 | |
| 81 | ret = phytec_eeprom_data_setup(&data, 0, EEPROM_ADDR); |
| 82 | if (ret || !data.valid) |
| 83 | return 0; |
| 84 | |
| 85 | PHYTEC_API3_FOREACH_BLOCK(block_element, &data) { |
| 86 | switch (block_element->block_type) { |
| 87 | case PHYTEC_API3_BLOCK_MAC: |
| 88 | phytec_blocks_add_mac_to_env(block_element); |
| 89 | break; |
| 90 | default: |
| 91 | debug("%s: Unknown block type %i\n", __func__, |
| 92 | block_element->block_type); |
| 93 | } |
| 94 | } |
| 95 | } |
| 96 | |
Tom Rini | dec7ea0 | 2024-05-20 13:35:03 -0600 | [diff] [blame] | 97 | return 0; |
| 98 | } |
| 99 | #endif |
Wadim Egorov | 4baf2a6 | 2024-06-10 15:33:42 +0200 | [diff] [blame] | 100 | |
| 101 | #if IS_ENABLED(CONFIG_OF_LIBFDT) && IS_ENABLED(CONFIG_OF_BOARD_SETUP) |
Wadim Egorov | 94d9252 | 2024-10-30 17:48:15 +0100 | [diff] [blame^] | 102 | static int fdt_apply_overlay_from_fit(const char *overlay_path, void *fdt) |
| 103 | { |
| 104 | u64 loadaddr; |
| 105 | ofnode node; |
| 106 | int ret; |
| 107 | |
| 108 | node = ofnode_path(overlay_path); |
| 109 | if (!ofnode_valid(node)) |
| 110 | return -FDT_ERR_NOTFOUND; |
| 111 | |
| 112 | ret = ofnode_read_u64(node, "load", &loadaddr); |
| 113 | if (ret) |
| 114 | return ret; |
| 115 | |
| 116 | return fdt_overlay_apply_verbose(fdt, (void *)loadaddr); |
| 117 | } |
| 118 | |
| 119 | static void fdt_apply_som_overlays(void *blob) |
| 120 | { |
| 121 | void *fdt_copy; |
| 122 | u32 fdt_size; |
| 123 | struct phytec_eeprom_data data; |
| 124 | int err; |
| 125 | |
| 126 | fdt_size = fdt_totalsize(blob); |
| 127 | fdt_copy = malloc(fdt_size); |
| 128 | if (!fdt_copy) |
| 129 | goto fixup_error; |
| 130 | |
| 131 | memcpy(fdt_copy, blob, fdt_size); |
| 132 | |
| 133 | err = phytec_eeprom_data_setup(&data, 0, EEPROM_ADDR); |
| 134 | if (err) |
| 135 | goto fixup_error; |
| 136 | |
| 137 | if (phytec_get_am6_rtc(&data) == 0) { |
| 138 | err = fdt_apply_overlay_from_fit("/fit-images/som-no-rtc", fdt_copy); |
| 139 | if (err) |
| 140 | goto fixup_error; |
| 141 | } |
| 142 | |
| 143 | if (phytec_get_am6_spi(&data) == PHYTEC_EEPROM_VALUE_X) { |
| 144 | err = fdt_apply_overlay_from_fit("/fit-images/som-no-spi", fdt_copy); |
| 145 | if (err) |
| 146 | goto fixup_error; |
| 147 | } |
| 148 | |
| 149 | if (phytec_get_am6_eth(&data) == 0) { |
| 150 | err = fdt_apply_overlay_from_fit("/fit-images/som-no-eth", fdt_copy); |
| 151 | if (err) |
| 152 | goto fixup_error; |
| 153 | } |
| 154 | |
| 155 | if (phytec_am6_is_qspi(&data)) { |
| 156 | err = fdt_apply_overlay_from_fit("/fit-images/som-qspi-nor", fdt_copy); |
| 157 | if (err) |
| 158 | goto fixup_error; |
| 159 | } |
| 160 | |
| 161 | memcpy(blob, fdt_copy, fdt_size); |
| 162 | |
| 163 | cleanup: |
| 164 | free(fdt_copy); |
| 165 | return; |
| 166 | |
| 167 | fixup_error: |
| 168 | pr_err("Failed to apply SoM overlays\n"); |
| 169 | goto cleanup; |
| 170 | } |
| 171 | |
Wadim Egorov | 4baf2a6 | 2024-06-10 15:33:42 +0200 | [diff] [blame] | 172 | int ft_board_setup(void *blob, struct bd_info *bd) |
| 173 | { |
Wadim Egorov | 94d9252 | 2024-10-30 17:48:15 +0100 | [diff] [blame^] | 174 | fdt_apply_som_overlays(blob); |
Wadim Egorov | 4baf2a6 | 2024-06-10 15:33:42 +0200 | [diff] [blame] | 175 | fdt_copy_fixed_partitions(blob); |
| 176 | |
| 177 | return 0; |
| 178 | } |
| 179 | #endif |