blob: 346b2b6491a65994f8983724dfa5d2c584269c41 [file] [log] [blame]
Tom Rinidec7ea02024-05-20 13:35:03 -06001// 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 Egorov4baf2a62024-06-10 15:33:42 +02008#include <fdt_support.h>
Wadim Egorov94d92522024-10-30 17:48:15 +01009#include <dm/ofnode.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060010#include <spl.h>
Wadim Egorov94d92522024-10-30 17:48:15 +010011#include <malloc.h>
Tom Rinidec7ea02024-05-20 13:35:03 -060012#include <asm/arch/hardware.h>
13
Daniel Schultz80677cb2024-05-21 23:18:26 -070014#include "../am6_som_detection.h"
15
Tom Rinidec7ea02024-05-20 13:35:03 -060016#if IS_ENABLED(CONFIG_ENV_IS_IN_FAT) || IS_ENABLED(CONFIG_ENV_IS_IN_MMC)
17int 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
32enum 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)
55int 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 Schultz80677cb2024-05-21 23:18:26 -070076 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 Rinidec7ea02024-05-20 13:35:03 -060097 return 0;
98}
99#endif
Wadim Egorov4baf2a62024-06-10 15:33:42 +0200100
101#if IS_ENABLED(CONFIG_OF_LIBFDT) && IS_ENABLED(CONFIG_OF_BOARD_SETUP)
Wadim Egorov94d92522024-10-30 17:48:15 +0100102static 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
119static 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
163cleanup:
164 free(fdt_copy);
165 return;
166
167fixup_error:
168 pr_err("Failed to apply SoM overlays\n");
169 goto cleanup;
170}
171
Wadim Egorov4baf2a62024-06-10 15:33:42 +0200172int ft_board_setup(void *blob, struct bd_info *bd)
173{
Wadim Egorov94d92522024-10-30 17:48:15 +0100174 fdt_apply_som_overlays(blob);
Wadim Egorov4baf2a62024-06-10 15:33:42 +0200175 fdt_copy_fixed_partitions(blob);
176
177 return 0;
178}
179#endif