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