Michal Simek | 9755e3d | 2019-01-21 15:25:02 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 3 | * (C) Copyright 2014 - 2020 Xilinx, Inc. |
Michal Simek | 9755e3d | 2019-01-21 15:25:02 +0100 | [diff] [blame] | 4 | * Michal Simek <michal.simek@xilinx.com> |
| 5 | */ |
| 6 | |
| 7 | #include <common.h> |
Sughosh Ganu | 741ef86 | 2022-04-15 11:29:34 +0530 | [diff] [blame] | 8 | #include <efi.h> |
| 9 | #include <efi_loader.h> |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 10 | #include <env.h> |
Michal Simek | a32c3e9 | 2022-08-25 14:23:10 +0200 | [diff] [blame] | 11 | #include <image.h> |
| 12 | #include <lmb.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Michal Simek | fc274a5 | 2019-12-19 17:45:15 +0100 | [diff] [blame] | 15 | #include <asm/sections.h> |
Michal Simek | 9755e3d | 2019-01-21 15:25:02 +0100 | [diff] [blame] | 16 | #include <dm/uclass.h> |
| 17 | #include <i2c.h> |
Michal Simek | a29511e | 2020-04-08 10:51:36 +0200 | [diff] [blame] | 18 | #include <linux/sizes.h> |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 19 | #include <malloc.h> |
Michal Simek | 80fdef1 | 2020-03-31 12:39:37 +0200 | [diff] [blame] | 20 | #include "board.h" |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 21 | #include <dm.h> |
| 22 | #include <i2c_eeprom.h> |
| 23 | #include <net.h> |
Michal Simek | 05af483 | 2020-10-26 12:26:13 +0100 | [diff] [blame] | 24 | #include <generated/dt.h> |
T Karthik Reddy | 80c0d38 | 2021-08-10 06:50:20 -0600 | [diff] [blame] | 25 | #include <soc.h> |
Michal Simek | 52ff162 | 2021-08-11 14:23:54 +0200 | [diff] [blame] | 26 | #include <linux/ctype.h> |
Sughosh Ganu | 741ef86 | 2022-04-15 11:29:34 +0530 | [diff] [blame] | 27 | #include <linux/kernel.h> |
Michal Simek | ee5a4b8 | 2022-07-21 16:19:17 +0200 | [diff] [blame] | 28 | #include <uuid.h> |
Michal Simek | 9755e3d | 2019-01-21 15:25:02 +0100 | [diff] [blame] | 29 | |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 30 | #include "fru.h" |
| 31 | |
Sughosh Ganu | 741ef86 | 2022-04-15 11:29:34 +0530 | [diff] [blame] | 32 | #if CONFIG_IS_ENABLED(EFI_HAVE_CAPSULE_SUPPORT) |
| 33 | struct efi_fw_image fw_images[] = { |
| 34 | #if defined(XILINX_BOOT_IMAGE_GUID) |
| 35 | { |
| 36 | .image_type_id = XILINX_BOOT_IMAGE_GUID, |
| 37 | .fw_name = u"XILINX-BOOT", |
| 38 | .image_index = 1, |
| 39 | }, |
| 40 | #endif |
| 41 | #if defined(XILINX_UBOOT_IMAGE_GUID) |
| 42 | { |
| 43 | .image_type_id = XILINX_UBOOT_IMAGE_GUID, |
| 44 | .fw_name = u"XILINX-UBOOT", |
| 45 | .image_index = 2, |
| 46 | }, |
| 47 | #endif |
| 48 | }; |
| 49 | |
| 50 | struct efi_capsule_update_info update_info = { |
| 51 | .images = fw_images, |
| 52 | }; |
| 53 | |
| 54 | u8 num_image_type_guids = ARRAY_SIZE(fw_images); |
| 55 | #endif /* EFI_HAVE_CAPSULE_SUPPORT */ |
| 56 | |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 57 | #define EEPROM_HEADER_MAGIC 0xdaaddeed |
| 58 | #define EEPROM_HDR_MANUFACTURER_LEN 16 |
| 59 | #define EEPROM_HDR_NAME_LEN 16 |
| 60 | #define EEPROM_HDR_REV_LEN 8 |
| 61 | #define EEPROM_HDR_SERIAL_LEN 20 |
| 62 | #define EEPROM_HDR_NO_OF_MAC_ADDR 4 |
| 63 | #define EEPROM_HDR_ETH_ALEN ETH_ALEN |
Michal Simek | ee5a4b8 | 2022-07-21 16:19:17 +0200 | [diff] [blame] | 64 | #define EEPROM_HDR_UUID_LEN 16 |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 65 | |
| 66 | struct xilinx_board_description { |
| 67 | u32 header; |
| 68 | char manufacturer[EEPROM_HDR_MANUFACTURER_LEN + 1]; |
| 69 | char name[EEPROM_HDR_NAME_LEN + 1]; |
| 70 | char revision[EEPROM_HDR_REV_LEN + 1]; |
| 71 | char serial[EEPROM_HDR_SERIAL_LEN + 1]; |
| 72 | u8 mac_addr[EEPROM_HDR_NO_OF_MAC_ADDR][EEPROM_HDR_ETH_ALEN + 1]; |
Michal Simek | ee5a4b8 | 2022-07-21 16:19:17 +0200 | [diff] [blame] | 73 | char uuid[EEPROM_HDR_UUID_LEN + 1]; |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 74 | }; |
| 75 | |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 76 | static int highest_id = -1; |
Michal Simek | d9c93c9 | 2021-08-12 12:30:36 +0200 | [diff] [blame] | 77 | static struct xilinx_board_description *board_info; |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 78 | |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 79 | #define XILINX_I2C_DETECTION_BITS sizeof(struct fru_common_hdr) |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 80 | |
| 81 | /* Variable which stores pointer to array which stores eeprom content */ |
| 82 | struct xilinx_legacy_format { |
| 83 | char board_sn[18]; /* 0x0 */ |
| 84 | char unused0[14]; /* 0x12 */ |
| 85 | char eth_mac[6]; /* 0x20 */ |
| 86 | char unused1[170]; /* 0x26 */ |
| 87 | char board_name[11]; /* 0xd0 */ |
| 88 | char unused2[5]; /* 0xdc */ |
| 89 | char board_revision[3]; /* 0xe0 */ |
| 90 | char unused3[29]; /* 0xe3 */ |
| 91 | }; |
| 92 | |
| 93 | static void xilinx_eeprom_legacy_cleanup(char *eeprom, int size) |
| 94 | { |
| 95 | int i; |
Venkatesh Yadav Abbarapu | e1a193b | 2022-09-26 12:22:42 +0530 | [diff] [blame] | 96 | unsigned char byte; |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 97 | |
| 98 | for (i = 0; i < size; i++) { |
| 99 | byte = eeprom[i]; |
| 100 | |
| 101 | /* Remove all ffs and spaces */ |
| 102 | if (byte == 0xff || byte == ' ') |
| 103 | eeprom[i] = 0; |
| 104 | |
| 105 | /* Convert strings to lower case */ |
| 106 | if (byte >= 'A' && byte <= 'Z') |
| 107 | eeprom[i] = byte + 'a' - 'A'; |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | static int xilinx_read_eeprom_legacy(struct udevice *dev, char *name, |
| 112 | struct xilinx_board_description *desc) |
| 113 | { |
| 114 | int ret, size; |
| 115 | struct xilinx_legacy_format *eeprom_content; |
| 116 | bool eth_valid = false; |
| 117 | |
| 118 | size = sizeof(*eeprom_content); |
| 119 | |
| 120 | eeprom_content = calloc(1, size); |
| 121 | if (!eeprom_content) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | debug("%s: I2C EEPROM read pass data at %p\n", __func__, |
| 125 | eeprom_content); |
| 126 | |
| 127 | ret = dm_i2c_read(dev, 0, (uchar *)eeprom_content, size); |
| 128 | if (ret) { |
| 129 | debug("%s: I2C EEPROM read failed\n", __func__); |
| 130 | free(eeprom_content); |
| 131 | return ret; |
| 132 | } |
| 133 | |
| 134 | xilinx_eeprom_legacy_cleanup((char *)eeprom_content, size); |
| 135 | |
| 136 | printf("Xilinx I2C Legacy format at %s:\n", name); |
| 137 | printf(" Board name:\t%s\n", eeprom_content->board_name); |
| 138 | printf(" Board rev:\t%s\n", eeprom_content->board_revision); |
| 139 | printf(" Board SN:\t%s\n", eeprom_content->board_sn); |
| 140 | |
| 141 | eth_valid = is_valid_ethaddr((const u8 *)eeprom_content->eth_mac); |
| 142 | if (eth_valid) |
| 143 | printf(" Ethernet mac:\t%pM\n", eeprom_content->eth_mac); |
| 144 | |
| 145 | /* Terminating \0 chars ensure end of string */ |
| 146 | strcpy(desc->name, eeprom_content->board_name); |
| 147 | strcpy(desc->revision, eeprom_content->board_revision); |
| 148 | strcpy(desc->serial, eeprom_content->board_sn); |
| 149 | if (eth_valid) |
| 150 | memcpy(desc->mac_addr[0], eeprom_content->eth_mac, ETH_ALEN); |
| 151 | |
| 152 | desc->header = EEPROM_HEADER_MAGIC; |
| 153 | |
| 154 | free(eeprom_content); |
| 155 | |
| 156 | return ret; |
| 157 | } |
| 158 | |
| 159 | static bool xilinx_detect_legacy(u8 *buffer) |
| 160 | { |
| 161 | int i; |
| 162 | char c; |
| 163 | |
| 164 | for (i = 0; i < XILINX_I2C_DETECTION_BITS; i++) { |
| 165 | c = buffer[i]; |
| 166 | |
| 167 | if (c < '0' || c > '9') |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| 173 | |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 174 | static int xilinx_read_eeprom_fru(struct udevice *dev, char *name, |
| 175 | struct xilinx_board_description *desc) |
| 176 | { |
Michal Simek | 530560b | 2021-08-12 11:03:49 +0200 | [diff] [blame] | 177 | int i, ret, eeprom_size; |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 178 | u8 *fru_content; |
Ashok Reddy Soma | 7a036b6 | 2022-02-23 15:00:59 +0100 | [diff] [blame] | 179 | u8 id = 0; |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 180 | |
| 181 | /* FIXME this is shortcut - if eeprom type is wrong it will fail */ |
| 182 | eeprom_size = i2c_eeprom_size(dev); |
| 183 | |
| 184 | fru_content = calloc(1, eeprom_size); |
| 185 | if (!fru_content) |
| 186 | return -ENOMEM; |
| 187 | |
| 188 | debug("%s: I2C EEPROM read pass data at %p\n", __func__, |
| 189 | fru_content); |
| 190 | |
| 191 | ret = dm_i2c_read(dev, 0, (uchar *)fru_content, |
| 192 | eeprom_size); |
| 193 | if (ret) { |
| 194 | debug("%s: I2C EEPROM read failed\n", __func__); |
Michal Simek | b262863 | 2021-08-13 09:17:10 +0200 | [diff] [blame] | 195 | goto end; |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 196 | } |
| 197 | |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 198 | fru_capture((unsigned long)fru_content); |
Michal Simek | 52ff162 | 2021-08-11 14:23:54 +0200 | [diff] [blame] | 199 | if (gd->flags & GD_FLG_RELOC || (_DEBUG && CONFIG_IS_ENABLED(DTB_RESELECT))) { |
| 200 | printf("Xilinx I2C FRU format at %s:\n", name); |
| 201 | ret = fru_display(0); |
| 202 | if (ret) { |
| 203 | printf("FRU format decoding failed.\n"); |
| 204 | goto end; |
| 205 | } |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | if (desc->header == EEPROM_HEADER_MAGIC) { |
| 209 | debug("Information already filled\n"); |
Michal Simek | b262863 | 2021-08-13 09:17:10 +0200 | [diff] [blame] | 210 | ret = -EINVAL; |
| 211 | goto end; |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | /* It is clear that FRU was captured and structures were filled */ |
Michal Simek | 4a1bfcd | 2022-07-21 16:19:18 +0200 | [diff] [blame] | 215 | strlcpy(desc->manufacturer, (char *)fru_data.brd.manufacturer_name, |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 216 | sizeof(desc->manufacturer)); |
Michal Simek | 4a1bfcd | 2022-07-21 16:19:18 +0200 | [diff] [blame] | 217 | strlcpy(desc->uuid, (char *)fru_data.brd.uuid, |
Michal Simek | ee5a4b8 | 2022-07-21 16:19:17 +0200 | [diff] [blame] | 218 | sizeof(desc->uuid)); |
Michal Simek | 4a1bfcd | 2022-07-21 16:19:18 +0200 | [diff] [blame] | 219 | strlcpy(desc->name, (char *)fru_data.brd.product_name, |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 220 | sizeof(desc->name)); |
Michal Simek | 530560b | 2021-08-12 11:03:49 +0200 | [diff] [blame] | 221 | for (i = 0; i < sizeof(desc->name); i++) { |
| 222 | if (desc->name[i] == ' ') |
| 223 | desc->name[i] = '\0'; |
| 224 | } |
Michal Simek | 4a1bfcd | 2022-07-21 16:19:18 +0200 | [diff] [blame] | 225 | strlcpy(desc->revision, (char *)fru_data.brd.rev, |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 226 | sizeof(desc->revision)); |
Michal Simek | 9321627 | 2022-06-06 09:31:27 +0200 | [diff] [blame] | 227 | for (i = 0; i < sizeof(desc->revision); i++) { |
| 228 | if (desc->revision[i] == ' ') |
| 229 | desc->revision[i] = '\0'; |
| 230 | } |
Michal Simek | 4a1bfcd | 2022-07-21 16:19:18 +0200 | [diff] [blame] | 231 | strlcpy(desc->serial, (char *)fru_data.brd.serial_number, |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 232 | sizeof(desc->serial)); |
Ashok Reddy Soma | 7a036b6 | 2022-02-23 15:00:59 +0100 | [diff] [blame] | 233 | |
| 234 | while (id < EEPROM_HDR_NO_OF_MAC_ADDR) { |
| 235 | if (is_valid_ethaddr((const u8 *)fru_data.mac.macid[id])) |
| 236 | memcpy(&desc->mac_addr[id], |
| 237 | (char *)fru_data.mac.macid[id], ETH_ALEN); |
| 238 | id++; |
| 239 | } |
| 240 | |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 241 | desc->header = EEPROM_HEADER_MAGIC; |
| 242 | |
Michal Simek | b262863 | 2021-08-13 09:17:10 +0200 | [diff] [blame] | 243 | end: |
| 244 | free(fru_content); |
| 245 | return ret; |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 246 | } |
| 247 | |
| 248 | static bool xilinx_detect_fru(u8 *buffer) |
| 249 | { |
| 250 | u8 checksum = 0; |
| 251 | int i; |
| 252 | |
| 253 | checksum = fru_checksum((u8 *)buffer, sizeof(struct fru_common_hdr)); |
| 254 | if (checksum) { |
| 255 | debug("%s Common header CRC FAIL\n", __func__); |
| 256 | return false; |
| 257 | } |
| 258 | |
| 259 | bool all_zeros = true; |
| 260 | /* Checksum over all zeros is also zero that's why detect this case */ |
| 261 | for (i = 0; i < sizeof(struct fru_common_hdr); i++) { |
| 262 | if (buffer[i] != 0) |
| 263 | all_zeros = false; |
| 264 | } |
| 265 | |
| 266 | if (all_zeros) |
| 267 | return false; |
| 268 | |
| 269 | debug("%s Common header CRC PASS\n", __func__); |
| 270 | return true; |
| 271 | } |
| 272 | |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 273 | static int xilinx_read_eeprom_single(char *name, |
| 274 | struct xilinx_board_description *desc) |
| 275 | { |
| 276 | int ret; |
| 277 | struct udevice *dev; |
| 278 | ofnode eeprom; |
| 279 | u8 buffer[XILINX_I2C_DETECTION_BITS]; |
| 280 | |
| 281 | eeprom = ofnode_get_aliases_node(name); |
| 282 | if (!ofnode_valid(eeprom)) |
| 283 | return -ENODEV; |
| 284 | |
| 285 | ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, eeprom, &dev); |
| 286 | if (ret) |
| 287 | return ret; |
| 288 | |
| 289 | ret = dm_i2c_read(dev, 0, buffer, sizeof(buffer)); |
| 290 | if (ret) { |
| 291 | debug("%s: I2C EEPROM read failed\n", __func__); |
| 292 | return ret; |
| 293 | } |
| 294 | |
| 295 | debug("%s: i2c memory detected: %s\n", __func__, name); |
| 296 | |
Michal Simek | f149b39 | 2020-08-03 16:14:23 +0200 | [diff] [blame] | 297 | if (CONFIG_IS_ENABLED(CMD_FRU) && xilinx_detect_fru(buffer)) |
| 298 | return xilinx_read_eeprom_fru(dev, name, desc); |
| 299 | |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 300 | if (xilinx_detect_legacy(buffer)) |
| 301 | return xilinx_read_eeprom_legacy(dev, name, desc); |
| 302 | |
| 303 | return -ENODEV; |
| 304 | } |
| 305 | |
| 306 | __maybe_unused int xilinx_read_eeprom(void) |
| 307 | { |
Michal Simek | d9c93c9 | 2021-08-12 12:30:36 +0200 | [diff] [blame] | 308 | int id; |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 309 | char name_buf[8]; /* 8 bytes should be enough for nvmem+number */ |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 310 | struct xilinx_board_description *desc; |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 311 | |
| 312 | highest_id = dev_read_alias_highest_id("nvmem"); |
| 313 | /* No nvmem aliases present */ |
| 314 | if (highest_id < 0) |
| 315 | return -EINVAL; |
| 316 | |
Michal Simek | d9c93c9 | 2021-08-12 12:30:36 +0200 | [diff] [blame] | 317 | board_info = calloc(1, sizeof(*desc) * (highest_id + 1)); |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 318 | if (!board_info) |
| 319 | return -ENOMEM; |
| 320 | |
| 321 | debug("%s: Highest ID %d, board_info %p\n", __func__, |
| 322 | highest_id, board_info); |
| 323 | |
| 324 | for (id = 0; id <= highest_id; id++) { |
| 325 | snprintf(name_buf, sizeof(name_buf), "nvmem%d", id); |
| 326 | |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 327 | /* Alloc structure */ |
Michal Simek | d9c93c9 | 2021-08-12 12:30:36 +0200 | [diff] [blame] | 328 | desc = &board_info[id]; |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 329 | |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 330 | /* Ignoring return value for supporting multiple chips */ |
Michal Simek | d9c93c9 | 2021-08-12 12:30:36 +0200 | [diff] [blame] | 331 | xilinx_read_eeprom_single(name_buf, desc); |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 332 | } |
| 333 | |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 334 | /* |
| 335 | * Consider to clean board_info structure when board/cards are not |
| 336 | * detected. |
| 337 | */ |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 338 | |
| 339 | return 0; |
| 340 | } |
| 341 | |
Michal Simek | f063100 | 2022-02-17 14:28:38 +0100 | [diff] [blame] | 342 | #if defined(CONFIG_OF_BOARD) |
Ilias Apalodimas | e7fb789 | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 343 | void *board_fdt_blob_setup(int *err) |
Ibai Erkiaga | fec657b | 2019-10-02 15:57:36 +0100 | [diff] [blame] | 344 | { |
Michal Simek | e2572b5 | 2020-09-04 16:21:47 +0200 | [diff] [blame] | 345 | void *fdt_blob; |
Michal Simek | 453bb77 | 2020-03-19 10:23:56 +0100 | [diff] [blame] | 346 | |
Ilias Apalodimas | e7fb789 | 2021-10-26 09:12:33 +0300 | [diff] [blame] | 347 | *err = 0; |
Michal Simek | a672b98 | 2021-01-04 11:07:28 +0100 | [diff] [blame] | 348 | if (!IS_ENABLED(CONFIG_SPL_BUILD) && |
| 349 | !IS_ENABLED(CONFIG_VERSAL_NO_DDR) && |
Michal Simek | fc3c6fd | 2021-02-02 13:33:22 +0100 | [diff] [blame] | 350 | !IS_ENABLED(CONFIG_ZYNQMP_NO_DDR)) { |
Michal Simek | 506009f | 2021-01-04 11:03:36 +0100 | [diff] [blame] | 351 | fdt_blob = (void *)CONFIG_XILINX_OF_BOARD_DTB_ADDR; |
Ibai Erkiaga | fec657b | 2019-10-02 15:57:36 +0100 | [diff] [blame] | 352 | |
Michal Simek | 506009f | 2021-01-04 11:03:36 +0100 | [diff] [blame] | 353 | if (fdt_magic(fdt_blob) == FDT_MAGIC) |
| 354 | return fdt_blob; |
Ibai Erkiaga | fec657b | 2019-10-02 15:57:36 +0100 | [diff] [blame] | 355 | |
Michal Simek | 506009f | 2021-01-04 11:03:36 +0100 | [diff] [blame] | 356 | debug("DTB is not passed via %p\n", fdt_blob); |
| 357 | } |
Michal Simek | fc274a5 | 2019-12-19 17:45:15 +0100 | [diff] [blame] | 358 | |
Michal Simek | 506009f | 2021-01-04 11:03:36 +0100 | [diff] [blame] | 359 | if (IS_ENABLED(CONFIG_SPL_BUILD)) { |
| 360 | /* |
| 361 | * FDT is at end of BSS unless it is in a different memory |
| 362 | * region |
| 363 | */ |
| 364 | if (IS_ENABLED(CONFIG_SPL_SEPARATE_BSS)) |
| 365 | fdt_blob = (ulong *)&_image_binary_end; |
| 366 | else |
| 367 | fdt_blob = (ulong *)&__bss_end; |
| 368 | } else { |
| 369 | /* FDT is at end of image */ |
| 370 | fdt_blob = (ulong *)&_end; |
| 371 | } |
Michal Simek | fc274a5 | 2019-12-19 17:45:15 +0100 | [diff] [blame] | 372 | |
| 373 | if (fdt_magic(fdt_blob) == FDT_MAGIC) |
| 374 | return fdt_blob; |
| 375 | |
| 376 | debug("DTB is also not passed via %p\n", fdt_blob); |
| 377 | |
Michal Simek | f063100 | 2022-02-17 14:28:38 +0100 | [diff] [blame] | 378 | *err = -EINVAL; |
Michal Simek | fc274a5 | 2019-12-19 17:45:15 +0100 | [diff] [blame] | 379 | return NULL; |
Ibai Erkiaga | fec657b | 2019-10-02 15:57:36 +0100 | [diff] [blame] | 380 | } |
| 381 | #endif |
Michal Simek | 80fdef1 | 2020-03-31 12:39:37 +0200 | [diff] [blame] | 382 | |
Michal Simek | 7c553ac | 2020-10-22 11:14:20 +0200 | [diff] [blame] | 383 | #if defined(CONFIG_BOARD_LATE_INIT) |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 384 | static int env_set_by_index(const char *name, int index, char *data) |
| 385 | { |
| 386 | char var[32]; |
| 387 | |
| 388 | if (!index) |
| 389 | sprintf(var, "board_%s", name); |
| 390 | else |
| 391 | sprintf(var, "card%d_%s", index, name); |
| 392 | |
| 393 | return env_set(var, data); |
| 394 | } |
| 395 | |
Michal Simek | 80fdef1 | 2020-03-31 12:39:37 +0200 | [diff] [blame] | 396 | int board_late_init_xilinx(void) |
| 397 | { |
Michal Simek | ca0f616 | 2020-08-12 12:16:49 +0200 | [diff] [blame] | 398 | u32 ret = 0; |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 399 | int i, id, macid = 0; |
| 400 | struct xilinx_board_description *desc; |
Ricardo Salveti | e6e3b9d | 2022-01-20 16:17:30 -0300 | [diff] [blame] | 401 | phys_size_t bootm_size = gd->ram_top - gd->ram_base; |
T Karthik Reddy | d388ced | 2020-04-01 06:01:21 -0600 | [diff] [blame] | 402 | |
T Karthik Reddy | 25484d9 | 2021-04-02 01:49:17 -0600 | [diff] [blame] | 403 | if (!CONFIG_IS_ENABLED(MICROBLAZE)) { |
T Karthik Reddy | d388ced | 2020-04-01 06:01:21 -0600 | [diff] [blame] | 404 | ulong scriptaddr; |
| 405 | |
| 406 | scriptaddr = env_get_hex("scriptaddr", 0); |
T Karthik Reddy | 25484d9 | 2021-04-02 01:49:17 -0600 | [diff] [blame] | 407 | ret |= env_set_hex("scriptaddr", gd->ram_base + scriptaddr); |
T Karthik Reddy | d388ced | 2020-04-01 06:01:21 -0600 | [diff] [blame] | 408 | } |
Michal Simek | 2570cc6 | 2020-08-12 12:17:53 +0200 | [diff] [blame] | 409 | |
Michal Simek | 1230582 | 2020-10-23 07:54:18 +0200 | [diff] [blame] | 410 | if (CONFIG_IS_ENABLED(ARCH_ZYNQ) || CONFIG_IS_ENABLED(MICROBLAZE)) |
Michal Simek | 2570cc6 | 2020-08-12 12:17:53 +0200 | [diff] [blame] | 411 | bootm_size = min(bootm_size, (phys_size_t)(SZ_512M + SZ_256M)); |
Michal Simek | 80fdef1 | 2020-03-31 12:39:37 +0200 | [diff] [blame] | 412 | |
Michal Simek | ca0f616 | 2020-08-12 12:16:49 +0200 | [diff] [blame] | 413 | ret |= env_set_hex("script_offset_f", CONFIG_BOOT_SCRIPT_OFFSET); |
| 414 | |
| 415 | ret |= env_set_addr("bootm_low", (void *)gd->ram_base); |
Michal Simek | 2570cc6 | 2020-08-12 12:17:53 +0200 | [diff] [blame] | 416 | ret |= env_set_addr("bootm_size", (void *)bootm_size); |
Michal Simek | ca0f616 | 2020-08-12 12:16:49 +0200 | [diff] [blame] | 417 | |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 418 | for (id = 0; id <= highest_id; id++) { |
Michal Simek | d9c93c9 | 2021-08-12 12:30:36 +0200 | [diff] [blame] | 419 | desc = &board_info[id]; |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 420 | if (desc && desc->header == EEPROM_HEADER_MAGIC) { |
| 421 | if (desc->manufacturer[0]) |
| 422 | ret |= env_set_by_index("manufacturer", id, |
| 423 | desc->manufacturer); |
| 424 | if (desc->name[0]) |
| 425 | ret |= env_set_by_index("name", id, |
| 426 | desc->name); |
| 427 | if (desc->revision[0]) |
| 428 | ret |= env_set_by_index("rev", id, |
| 429 | desc->revision); |
| 430 | if (desc->serial[0]) |
| 431 | ret |= env_set_by_index("serial", id, |
| 432 | desc->serial); |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 433 | |
Michal Simek | ee5a4b8 | 2022-07-21 16:19:17 +0200 | [diff] [blame] | 434 | if (desc->uuid[0]) { |
Venkatesh Yadav Abbarapu | e1a193b | 2022-09-26 12:22:42 +0530 | [diff] [blame] | 435 | unsigned char uuid[UUID_STR_LEN + 1]; |
| 436 | unsigned char *t = desc->uuid; |
Michal Simek | ee5a4b8 | 2022-07-21 16:19:17 +0200 | [diff] [blame] | 437 | |
| 438 | memset(uuid, 0, UUID_STR_LEN + 1); |
| 439 | |
| 440 | sprintf(uuid, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", |
| 441 | t[0], t[1], t[2], t[3], t[4], t[5], |
| 442 | t[6], t[7], t[8], t[9], t[10], t[11], |
| 443 | t[12], t[13], t[14], t[15]); |
| 444 | ret |= env_set_by_index("uuid", id, uuid); |
| 445 | } |
| 446 | |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 447 | if (!CONFIG_IS_ENABLED(NET)) |
| 448 | continue; |
| 449 | |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 450 | for (i = 0; i < EEPROM_HDR_NO_OF_MAC_ADDR; i++) { |
Michal Simek | a03b594 | 2020-08-03 12:57:05 +0200 | [diff] [blame] | 451 | if (is_valid_ethaddr((const u8 *)desc->mac_addr[i])) |
| 452 | ret |= eth_env_set_enetaddr_by_index("eth", |
| 453 | macid++, desc->mac_addr[i]); |
| 454 | } |
Michal Simek | d61728c | 2020-08-03 13:01:45 +0200 | [diff] [blame] | 455 | } |
| 456 | } |
| 457 | |
Michal Simek | ca0f616 | 2020-08-12 12:16:49 +0200 | [diff] [blame] | 458 | if (ret) |
| 459 | printf("%s: Saving run time variables FAILED\n", __func__); |
Michal Simek | c8da651 | 2020-07-09 15:57:56 +0200 | [diff] [blame] | 460 | |
Michal Simek | 80fdef1 | 2020-03-31 12:39:37 +0200 | [diff] [blame] | 461 | return 0; |
| 462 | } |
Michal Simek | 7c553ac | 2020-10-22 11:14:20 +0200 | [diff] [blame] | 463 | #endif |
Michal Simek | 05af483 | 2020-10-26 12:26:13 +0100 | [diff] [blame] | 464 | |
Michal Simek | 57f7103 | 2021-07-23 09:55:59 +0200 | [diff] [blame] | 465 | static char *board_name = DEVICE_TREE; |
| 466 | |
Michal Simek | 05af483 | 2020-10-26 12:26:13 +0100 | [diff] [blame] | 467 | int __maybe_unused board_fit_config_name_match(const char *name) |
| 468 | { |
Michal Simek | 57f7103 | 2021-07-23 09:55:59 +0200 | [diff] [blame] | 469 | debug("%s: Check %s, default %s\n", __func__, name, board_name); |
Michal Simek | 05af483 | 2020-10-26 12:26:13 +0100 | [diff] [blame] | 470 | |
Michal Simek | 57f7103 | 2021-07-23 09:55:59 +0200 | [diff] [blame] | 471 | if (!strcmp(name, board_name)) |
Michal Simek | 05af483 | 2020-10-26 12:26:13 +0100 | [diff] [blame] | 472 | return 0; |
| 473 | |
| 474 | return -1; |
| 475 | } |
T Karthik Reddy | 80c0d38 | 2021-08-10 06:50:20 -0600 | [diff] [blame] | 476 | |
Michal Simek | 8823253 | 2021-07-23 09:59:59 +0200 | [diff] [blame] | 477 | #if CONFIG_IS_ENABLED(DTB_RESELECT) |
Michal Simek | 52ff162 | 2021-08-11 14:23:54 +0200 | [diff] [blame] | 478 | #define MAX_NAME_LENGTH 50 |
| 479 | |
Michal Simek | 8823253 | 2021-07-23 09:59:59 +0200 | [diff] [blame] | 480 | char * __maybe_unused __weak board_name_decode(void) |
| 481 | { |
Michal Simek | 52ff162 | 2021-08-11 14:23:54 +0200 | [diff] [blame] | 482 | char *board_local_name; |
| 483 | struct xilinx_board_description *desc; |
| 484 | int i, id; |
| 485 | |
| 486 | board_local_name = calloc(1, MAX_NAME_LENGTH); |
| 487 | if (!board_info) |
| 488 | return NULL; |
| 489 | |
| 490 | for (id = 0; id <= highest_id; id++) { |
| 491 | desc = &board_info[id]; |
| 492 | |
| 493 | /* No board description */ |
| 494 | if (!desc) |
| 495 | goto error; |
| 496 | |
| 497 | /* Board is not detected */ |
| 498 | if (desc->header != EEPROM_HEADER_MAGIC) |
| 499 | continue; |
| 500 | |
| 501 | /* The first string should be soc name */ |
| 502 | if (!id) |
| 503 | strcat(board_local_name, CONFIG_SYS_BOARD); |
| 504 | |
| 505 | /* |
| 506 | * For two purpose here: |
| 507 | * soc_name- eg: zynqmp- |
| 508 | * and between base board and CC eg: ..revA-sck... |
| 509 | */ |
| 510 | strcat(board_local_name, "-"); |
| 511 | |
| 512 | if (desc->name[0]) { |
| 513 | /* For DT composition name needs to be lowercase */ |
| 514 | for (i = 0; i < sizeof(desc->name); i++) |
| 515 | desc->name[i] = tolower(desc->name[i]); |
| 516 | |
| 517 | strcat(board_local_name, desc->name); |
| 518 | } |
| 519 | if (desc->revision[0]) { |
| 520 | strcat(board_local_name, "-rev"); |
| 521 | |
| 522 | /* And revision needs to be uppercase */ |
| 523 | for (i = 0; i < sizeof(desc->revision); i++) |
| 524 | desc->revision[i] = toupper(desc->revision[i]); |
| 525 | |
| 526 | strcat(board_local_name, desc->revision); |
| 527 | } |
| 528 | } |
| 529 | |
| 530 | /* |
| 531 | * Longer strings will end up with buffer overflow and potential |
| 532 | * attacks that's why check it |
| 533 | */ |
| 534 | if (strlen(board_local_name) >= MAX_NAME_LENGTH) |
| 535 | panic("Board name can't be determined\n"); |
| 536 | |
| 537 | if (strlen(board_local_name)) |
| 538 | return board_local_name; |
| 539 | |
| 540 | error: |
| 541 | free(board_local_name); |
Michal Simek | 8823253 | 2021-07-23 09:59:59 +0200 | [diff] [blame] | 542 | return NULL; |
| 543 | } |
| 544 | |
| 545 | bool __maybe_unused __weak board_detection(void) |
| 546 | { |
Michal Simek | 52ff162 | 2021-08-11 14:23:54 +0200 | [diff] [blame] | 547 | if (CONFIG_IS_ENABLED(DM_I2C) && CONFIG_IS_ENABLED(I2C_EEPROM)) { |
| 548 | int ret; |
| 549 | |
| 550 | ret = xilinx_read_eeprom(); |
| 551 | return !ret ? true : false; |
| 552 | } |
| 553 | |
Michal Simek | 8823253 | 2021-07-23 09:59:59 +0200 | [diff] [blame] | 554 | return false; |
| 555 | } |
| 556 | |
Michal Simek | 39d3c3c | 2022-09-06 12:40:41 +0200 | [diff] [blame] | 557 | bool __maybe_unused __weak soc_detection(void) |
| 558 | { |
| 559 | return false; |
| 560 | } |
| 561 | |
| 562 | char * __maybe_unused __weak soc_name_decode(void) |
| 563 | { |
| 564 | return NULL; |
| 565 | } |
| 566 | |
Michal Simek | 8823253 | 2021-07-23 09:59:59 +0200 | [diff] [blame] | 567 | int embedded_dtb_select(void) |
| 568 | { |
Michal Simek | 39d3c3c | 2022-09-06 12:40:41 +0200 | [diff] [blame] | 569 | if (soc_detection()) { |
| 570 | char *soc_local_name; |
| 571 | |
| 572 | soc_local_name = soc_name_decode(); |
| 573 | if (soc_local_name) { |
| 574 | board_name = soc_local_name; |
| 575 | printf("Detected SOC name: %s\n", board_name); |
| 576 | |
| 577 | /* Time to change DTB on fly */ |
| 578 | /* Both ways should work here */ |
| 579 | /* fdtdec_resetup(&rescan); */ |
| 580 | return fdtdec_setup(); |
| 581 | } |
| 582 | } |
| 583 | |
Michal Simek | 8823253 | 2021-07-23 09:59:59 +0200 | [diff] [blame] | 584 | if (board_detection()) { |
| 585 | char *board_local_name; |
| 586 | |
| 587 | board_local_name = board_name_decode(); |
| 588 | if (board_local_name) { |
| 589 | board_name = board_local_name; |
Michal Simek | 52ff162 | 2021-08-11 14:23:54 +0200 | [diff] [blame] | 590 | printf("Detected name: %s\n", board_name); |
Michal Simek | 8823253 | 2021-07-23 09:59:59 +0200 | [diff] [blame] | 591 | |
| 592 | /* Time to change DTB on fly */ |
| 593 | /* Both ways should work here */ |
| 594 | /* fdtdec_resetup(&rescan); */ |
| 595 | fdtdec_setup(); |
| 596 | } |
| 597 | } |
| 598 | return 0; |
| 599 | } |
| 600 | #endif |
Michal Simek | a32c3e9 | 2022-08-25 14:23:10 +0200 | [diff] [blame] | 601 | |
| 602 | #if defined(CONFIG_LMB) |
Pali Rohár | 049704f | 2022-09-09 17:32:40 +0200 | [diff] [blame] | 603 | phys_size_t board_get_usable_ram_top(phys_size_t total_size) |
Michal Simek | a32c3e9 | 2022-08-25 14:23:10 +0200 | [diff] [blame] | 604 | { |
| 605 | phys_size_t size; |
| 606 | phys_addr_t reg; |
| 607 | struct lmb lmb; |
| 608 | |
| 609 | if (!total_size) |
| 610 | return gd->ram_top; |
| 611 | |
| 612 | if (!IS_ALIGNED((ulong)gd->fdt_blob, 0x8)) |
| 613 | panic("Not 64bit aligned DT location: %p\n", gd->fdt_blob); |
| 614 | |
| 615 | /* found enough not-reserved memory to relocated U-Boot */ |
| 616 | lmb_init(&lmb); |
| 617 | lmb_add(&lmb, gd->ram_base, gd->ram_size); |
| 618 | boot_fdt_add_mem_rsv_regions(&lmb, (void *)gd->fdt_blob); |
| 619 | size = ALIGN(CONFIG_SYS_MALLOC_LEN + total_size, MMU_SECTION_SIZE); |
| 620 | reg = lmb_alloc(&lmb, size, MMU_SECTION_SIZE); |
| 621 | |
| 622 | if (!reg) |
| 623 | reg = gd->ram_top - size; |
| 624 | |
| 625 | return reg + size; |
| 626 | } |
| 627 | #endif |