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