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