Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2019 Rockchip Electronics Co., Ltd. |
Quentin Schulz | ca0fa40 | 2024-03-11 13:01:52 +0100 | [diff] [blame] | 4 | * |
| 5 | * Copyright (C) 2019 Collabora Inc - https://www.collabora.com/ |
| 6 | * Rohan Garg <rohan.garg@collabora.com> |
| 7 | * |
| 8 | * Based on puma-rk3399.c: |
| 9 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 10 | */ |
| 11 | #include <common.h> |
| 12 | #include <clk.h> |
Simon Glass | 1d91ba7 | 2019-11-14 12:57:37 -0700 | [diff] [blame] | 13 | #include <cpu_func.h> |
Quentin Schulz | ca0fa40 | 2024-03-11 13:01:52 +0100 | [diff] [blame] | 14 | #include <env.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 15 | #include <dm.h> |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 16 | #include <efi_loader.h> |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 17 | #include <fastboot.h> |
Quentin Schulz | ca0fa40 | 2024-03-11 13:01:52 +0100 | [diff] [blame] | 18 | #include <hash.h> |
Simon Glass | a7b5130 | 2019-11-14 12:57:46 -0700 | [diff] [blame] | 19 | #include <init.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 20 | #include <log.h> |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 21 | #include <mmc.h> |
Quentin Schulz | ca0fa40 | 2024-03-11 13:01:52 +0100 | [diff] [blame] | 22 | #include <dm/uclass-internal.h> |
| 23 | #include <misc.h> |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 24 | #include <part.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 25 | #include <ram.h> |
| 26 | #include <syscon.h> |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 27 | #include <uuid.h> |
Quentin Schulz | ca0fa40 | 2024-03-11 13:01:52 +0100 | [diff] [blame] | 28 | #include <u-boot/crc.h> |
| 29 | #include <u-boot/sha256.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 30 | #include <asm/cache.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 31 | #include <asm/io.h> |
| 32 | #include <asm/arch-rockchip/boot_mode.h> |
| 33 | #include <asm/arch-rockchip/clock.h> |
| 34 | #include <asm/arch-rockchip/periph.h> |
| 35 | #include <power/regulator.h> |
| 36 | |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 37 | #if defined(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) && defined(CONFIG_EFI_PARTITION) |
| 38 | |
| 39 | #define DFU_ALT_BUF_LEN SZ_1K |
| 40 | |
| 41 | static struct efi_fw_image *fw_images; |
| 42 | |
| 43 | static bool updatable_image(struct disk_partition *info) |
| 44 | { |
| 45 | int i; |
| 46 | bool ret = false; |
| 47 | efi_guid_t image_type_guid; |
| 48 | |
| 49 | uuid_str_to_bin(info->type_guid, image_type_guid.b, |
| 50 | UUID_STR_FORMAT_GUID); |
| 51 | |
Masahisa Kojima | 5d2438b | 2023-06-07 14:41:51 +0900 | [diff] [blame] | 52 | for (i = 0; i < update_info.num_images; i++) { |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 53 | if (!guidcmp(&fw_images[i].image_type_id, &image_type_guid)) { |
| 54 | ret = true; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | return ret; |
| 60 | } |
| 61 | |
| 62 | static void set_image_index(struct disk_partition *info, int index) |
| 63 | { |
| 64 | int i; |
| 65 | efi_guid_t image_type_guid; |
| 66 | |
| 67 | uuid_str_to_bin(info->type_guid, image_type_guid.b, |
| 68 | UUID_STR_FORMAT_GUID); |
| 69 | |
Masahisa Kojima | 5d2438b | 2023-06-07 14:41:51 +0900 | [diff] [blame] | 70 | for (i = 0; i < update_info.num_images; i++) { |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 71 | if (!guidcmp(&fw_images[i].image_type_id, &image_type_guid)) { |
| 72 | fw_images[i].image_index = index; |
| 73 | break; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | static int get_mmc_desc(struct blk_desc **desc) |
| 79 | { |
| 80 | int ret; |
| 81 | struct mmc *mmc; |
| 82 | struct udevice *dev; |
| 83 | |
| 84 | /* |
| 85 | * For now the firmware images are assumed to |
| 86 | * be on the SD card |
| 87 | */ |
| 88 | ret = uclass_get_device(UCLASS_MMC, 1, &dev); |
| 89 | if (ret) |
| 90 | return -1; |
| 91 | |
| 92 | mmc = mmc_get_mmc_dev(dev); |
| 93 | if (!mmc) |
| 94 | return -ENODEV; |
| 95 | |
| 96 | if ((ret = mmc_init(mmc))) |
| 97 | return ret; |
| 98 | |
| 99 | *desc = mmc_get_blk_desc(mmc); |
| 100 | if (!*desc) |
| 101 | return -1; |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | void set_dfu_alt_info(char *interface, char *devstr) |
| 107 | { |
| 108 | const char *name; |
| 109 | bool first = true; |
| 110 | int p, len, devnum, ret; |
| 111 | char buf[DFU_ALT_BUF_LEN]; |
| 112 | struct disk_partition info; |
| 113 | struct blk_desc *desc = NULL; |
| 114 | |
| 115 | ret = get_mmc_desc(&desc); |
| 116 | if (ret) { |
| 117 | log_err("Unable to get mmc desc\n"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | memset(buf, 0, sizeof(buf)); |
| 122 | name = blk_get_uclass_name(desc->uclass_id); |
| 123 | devnum = desc->devnum; |
| 124 | len = strlen(buf); |
| 125 | |
| 126 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 127 | "%s %d=", name, devnum); |
| 128 | |
| 129 | for (p = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
| 130 | if (part_get_info(desc, p, &info)) |
| 131 | continue; |
| 132 | |
| 133 | /* Add entry to dfu_alt_info only for updatable images */ |
| 134 | if (updatable_image(&info)) { |
| 135 | if (!first) |
| 136 | len += snprintf(buf + len, |
| 137 | DFU_ALT_BUF_LEN - len, ";"); |
| 138 | |
| 139 | len += snprintf(buf + len, DFU_ALT_BUF_LEN - len, |
| 140 | "%s%d_%s part %d %d", |
| 141 | name, devnum, info.name, devnum, p); |
| 142 | first = false; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | log_debug("dfu_alt_info => %s\n", buf); |
| 147 | env_set("dfu_alt_info", buf); |
| 148 | } |
| 149 | |
Quentin Schulz | a0a66b2 | 2024-03-11 13:01:53 +0100 | [diff] [blame] | 150 | __weak void rockchip_capsule_update_board_setup(void) |
| 151 | { |
| 152 | } |
| 153 | |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 154 | static void gpt_capsule_update_setup(void) |
| 155 | { |
| 156 | int p, i, ret; |
| 157 | struct disk_partition info; |
| 158 | struct blk_desc *desc = NULL; |
| 159 | |
| 160 | fw_images = update_info.images; |
| 161 | rockchip_capsule_update_board_setup(); |
| 162 | |
| 163 | ret = get_mmc_desc(&desc); |
| 164 | if (ret) { |
| 165 | log_err("Unable to get mmc desc\n"); |
| 166 | return; |
| 167 | } |
| 168 | |
| 169 | for (p = 1, i = 1; p <= MAX_SEARCH_PARTITIONS; p++) { |
| 170 | if (part_get_info(desc, p, &info)) |
| 171 | continue; |
| 172 | |
| 173 | /* |
| 174 | * Since we have a GPT partitioned device, the updatable |
| 175 | * images could be stored in any order. Populate the |
| 176 | * image_index at runtime. |
| 177 | */ |
| 178 | if (updatable_image(&info)) { |
| 179 | set_image_index(&info, i); |
| 180 | i++; |
| 181 | } |
| 182 | } |
| 183 | } |
| 184 | #endif /* CONFIG_EFI_HAVE_CAPSULE_SUPPORT && CONFIG_EFI_PARTITION */ |
| 185 | |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 186 | __weak int rk_board_late_init(void) |
| 187 | { |
Sughosh Ganu | c1b8e8b | 2022-11-10 14:49:15 +0530 | [diff] [blame] | 188 | #if defined(CONFIG_EFI_HAVE_CAPSULE_SUPPORT) && defined(CONFIG_EFI_PARTITION) |
| 189 | gpt_capsule_update_setup(); |
| 190 | #endif |
| 191 | |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 192 | return 0; |
| 193 | } |
| 194 | |
| 195 | int board_late_init(void) |
| 196 | { |
| 197 | setup_boot_mode(); |
| 198 | |
| 199 | return rk_board_late_init(); |
| 200 | } |
| 201 | |
| 202 | int board_init(void) |
| 203 | { |
| 204 | int ret; |
| 205 | |
| 206 | #ifdef CONFIG_DM_REGULATOR |
| 207 | ret = regulators_enable_boot_on(false); |
| 208 | if (ret) |
| 209 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 210 | #endif |
| 211 | |
| 212 | return 0; |
| 213 | } |
| 214 | |
| 215 | #if !defined(CONFIG_SYS_DCACHE_OFF) && !defined(CONFIG_ARM64) |
| 216 | void enable_caches(void) |
| 217 | { |
| 218 | /* Enable D-cache. I-cache is already enabled in start.S */ |
| 219 | dcache_enable(); |
| 220 | } |
| 221 | #endif |
| 222 | |
Jonas Karlman | 67f97d1 | 2024-03-10 18:50:58 +0000 | [diff] [blame] | 223 | #if IS_ENABLED(CONFIG_USB_GADGET) |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 224 | #include <usb.h> |
Jonas Karlman | c4eb656 | 2024-03-10 18:50:59 +0000 | [diff] [blame^] | 225 | |
| 226 | #if IS_ENABLED(CONFIG_USB_GADGET_DOWNLOAD) |
| 227 | #define ROCKCHIP_G_DNL_UMS_PRODUCT_NUM 0x0010 |
| 228 | |
| 229 | int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) |
| 230 | { |
| 231 | if (!strcmp(name, "usb_dnl_ums")) |
| 232 | put_unaligned(ROCKCHIP_G_DNL_UMS_PRODUCT_NUM, &dev->idProduct); |
| 233 | else |
| 234 | put_unaligned(CONFIG_USB_GADGET_PRODUCT_NUM, &dev->idProduct); |
| 235 | |
| 236 | return 0; |
| 237 | } |
| 238 | #endif /* CONFIG_USB_GADGET_DOWNLOAD */ |
| 239 | |
| 240 | #if IS_ENABLED(CONFIG_USB_GADGET_DWC2_OTG) && !IS_ENABLED(CONFIG_DM_USB_GADGET) |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 241 | #include <linux/usb/otg.h> |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 242 | #include <usb/dwc2_udc.h> |
| 243 | |
| 244 | static struct dwc2_plat_otg_data otg_data = { |
| 245 | .rx_fifo_sz = 512, |
| 246 | .np_tx_fifo_sz = 16, |
| 247 | .tx_fifo_sz = 128, |
| 248 | }; |
| 249 | |
| 250 | int board_usb_init(int index, enum usb_init_type init) |
| 251 | { |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 252 | ofnode node; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 253 | bool matched = false; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 254 | |
| 255 | /* find the usb_otg node */ |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 256 | node = ofnode_by_compatible(ofnode_null(), "snps,dwc2"); |
| 257 | while (ofnode_valid(node)) { |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 258 | switch (usb_get_dr_mode(node)) { |
| 259 | case USB_DR_MODE_OTG: |
John Keeping | 3a0269b | 2023-04-12 12:52:53 +0100 | [diff] [blame] | 260 | case USB_DR_MODE_PERIPHERAL: |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 261 | matched = true; |
| 262 | break; |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 263 | |
| 264 | default: |
| 265 | break; |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 266 | } |
| 267 | |
John Keeping | a47bf33 | 2023-04-12 12:52:52 +0100 | [diff] [blame] | 268 | if (matched) |
| 269 | break; |
| 270 | |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 271 | node = ofnode_by_compatible(node, "snps,dwc2"); |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 272 | } |
| 273 | if (!matched) { |
| 274 | debug("Not found usb_otg device\n"); |
| 275 | return -ENODEV; |
| 276 | } |
Kever Yang | 45bda03 | 2019-10-16 17:13:31 +0800 | [diff] [blame] | 277 | otg_data.regs_otg = ofnode_get_addr(node); |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 278 | |
Johan Jonker | aa65f3d | 2022-04-29 23:40:07 +0200 | [diff] [blame] | 279 | #ifdef CONFIG_ROCKCHIP_USB2_PHY |
Kever Yang | 6169a0d | 2019-10-16 17:13:32 +0800 | [diff] [blame] | 280 | int ret; |
| 281 | u32 phandle, offset; |
| 282 | ofnode phy_node; |
| 283 | |
| 284 | ret = ofnode_read_u32(node, "phys", &phandle); |
| 285 | if (ret) |
| 286 | return ret; |
| 287 | |
| 288 | node = ofnode_get_by_phandle(phandle); |
| 289 | if (!ofnode_valid(node)) { |
| 290 | debug("Not found usb phy device\n"); |
| 291 | return -ENODEV; |
| 292 | } |
| 293 | |
| 294 | phy_node = ofnode_get_parent(node); |
| 295 | if (!ofnode_valid(node)) { |
| 296 | debug("Not found usb phy device\n"); |
| 297 | return -ENODEV; |
| 298 | } |
| 299 | |
| 300 | otg_data.phy_of_node = phy_node; |
| 301 | ret = ofnode_read_u32(node, "reg", &offset); |
| 302 | if (ret) |
| 303 | return ret; |
| 304 | otg_data.regs_phy = offset + |
| 305 | (u32)syscon_get_first_range(ROCKCHIP_SYSCON_GRF); |
| 306 | #endif |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 307 | return dwc2_udc_probe(&otg_data); |
| 308 | } |
| 309 | |
| 310 | int board_usb_cleanup(int index, enum usb_init_type init) |
| 311 | { |
| 312 | return 0; |
| 313 | } |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 314 | #endif /* CONFIG_USB_GADGET_DWC2_OTG */ |
Jonas Karlman | 67f97d1 | 2024-03-10 18:50:58 +0000 | [diff] [blame] | 315 | #endif /* CONFIG_USB_GADGET */ |
Jagan Teki | 886ecb2 | 2019-11-19 13:56:22 +0530 | [diff] [blame] | 316 | |
Simon Glass | 96c0d2b | 2023-02-05 17:54:10 -0700 | [diff] [blame] | 317 | #if IS_ENABLED(CONFIG_FASTBOOT) |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 318 | int fastboot_set_reboot_flag(enum fastboot_reboot_reason reason) |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 319 | { |
Roman Kovalivskyi | 1bb1342 | 2020-07-28 23:35:32 +0300 | [diff] [blame] | 320 | if (reason != FASTBOOT_REBOOT_REASON_BOOTLOADER) |
| 321 | return -ENOTSUPP; |
| 322 | |
Kever Yang | bb33773 | 2019-07-22 20:02:01 +0800 | [diff] [blame] | 323 | printf("Setting reboot to fastboot flag ...\n"); |
| 324 | /* Set boot mode to fastboot */ |
| 325 | writel(BOOT_FASTBOOT, CONFIG_ROCKCHIP_BOOT_MODE_REG); |
| 326 | |
| 327 | return 0; |
| 328 | } |
| 329 | #endif |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 330 | |
| 331 | #ifdef CONFIG_MISC_INIT_R |
Quentin Schulz | ca0fa40 | 2024-03-11 13:01:52 +0100 | [diff] [blame] | 332 | int rockchip_setup_macaddr(void) |
| 333 | { |
| 334 | #if CONFIG_IS_ENABLED(HASH) && CONFIG_IS_ENABLED(SHA256) |
| 335 | int ret; |
| 336 | const char *cpuid = env_get("cpuid#"); |
| 337 | u8 hash[SHA256_SUM_LEN]; |
| 338 | int size = sizeof(hash); |
| 339 | u8 mac_addr[6]; |
| 340 | |
| 341 | /* Only generate a MAC address, if none is set in the environment */ |
| 342 | if (env_get("ethaddr")) |
| 343 | return 0; |
| 344 | |
| 345 | if (!cpuid) { |
| 346 | debug("%s: could not retrieve 'cpuid#'\n", __func__); |
| 347 | return -1; |
| 348 | } |
| 349 | |
| 350 | ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); |
| 351 | if (ret) { |
| 352 | debug("%s: failed to calculate SHA256\n", __func__); |
| 353 | return -1; |
| 354 | } |
| 355 | |
| 356 | /* Copy 6 bytes of the hash to base the MAC address on */ |
| 357 | memcpy(mac_addr, hash, 6); |
| 358 | |
| 359 | /* Make this a valid MAC address and set it */ |
| 360 | mac_addr[0] &= 0xfe; /* clear multicast bit */ |
| 361 | mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ |
| 362 | eth_env_set_enetaddr("ethaddr", mac_addr); |
| 363 | |
| 364 | /* Make a valid MAC address for ethernet1 */ |
| 365 | mac_addr[5] ^= 0x01; |
| 366 | eth_env_set_enetaddr("eth1addr", mac_addr); |
| 367 | #endif |
| 368 | return 0; |
| 369 | } |
| 370 | |
| 371 | int rockchip_cpuid_from_efuse(const u32 cpuid_offset, |
| 372 | const u32 cpuid_length, |
| 373 | u8 *cpuid) |
| 374 | { |
| 375 | #if IS_ENABLED(CONFIG_ROCKCHIP_EFUSE) || IS_ENABLED(CONFIG_ROCKCHIP_OTP) |
| 376 | struct udevice *dev; |
| 377 | int ret; |
| 378 | |
| 379 | /* retrieve the device */ |
| 380 | #if IS_ENABLED(CONFIG_ROCKCHIP_EFUSE) |
| 381 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 382 | DM_DRIVER_GET(rockchip_efuse), &dev); |
| 383 | #elif IS_ENABLED(CONFIG_ROCKCHIP_OTP) |
| 384 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 385 | DM_DRIVER_GET(rockchip_otp), &dev); |
| 386 | #endif |
| 387 | if (ret) { |
| 388 | debug("%s: could not find efuse device\n", __func__); |
| 389 | return -1; |
| 390 | } |
| 391 | |
| 392 | /* read the cpu_id range from the efuses */ |
| 393 | ret = misc_read(dev, cpuid_offset, cpuid, cpuid_length); |
| 394 | if (ret < 0) { |
| 395 | debug("%s: reading cpuid from the efuses failed\n", |
| 396 | __func__); |
| 397 | return -1; |
| 398 | } |
| 399 | #endif |
| 400 | return 0; |
| 401 | } |
| 402 | |
| 403 | int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length) |
| 404 | { |
| 405 | u8 low[cpuid_length / 2], high[cpuid_length / 2]; |
| 406 | char cpuid_str[cpuid_length * 2 + 1]; |
| 407 | u64 serialno; |
| 408 | char serialno_str[17]; |
| 409 | const char *oldid; |
| 410 | int i; |
| 411 | |
| 412 | memset(cpuid_str, 0, sizeof(cpuid_str)); |
| 413 | for (i = 0; i < cpuid_length; i++) |
| 414 | sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); |
| 415 | |
| 416 | debug("cpuid: %s\n", cpuid_str); |
| 417 | |
| 418 | /* |
| 419 | * Mix the cpuid bytes using the same rules as in |
| 420 | * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c |
| 421 | */ |
| 422 | for (i = 0; i < cpuid_length / 2; i++) { |
| 423 | low[i] = cpuid[1 + (i << 1)]; |
| 424 | high[i] = cpuid[i << 1]; |
| 425 | } |
| 426 | |
| 427 | serialno = crc32_no_comp(0, low, cpuid_length / 2); |
| 428 | serialno |= (u64)crc32_no_comp(serialno, high, cpuid_length / 2) << 32; |
| 429 | snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno); |
| 430 | |
| 431 | oldid = env_get("cpuid#"); |
| 432 | if (oldid && strcmp(oldid, cpuid_str) != 0) |
| 433 | printf("cpuid: value %s present in env does not match hardware %s\n", |
| 434 | oldid, cpuid_str); |
| 435 | |
| 436 | env_set("cpuid#", cpuid_str); |
| 437 | |
| 438 | /* Only generate serial# when none is set yet */ |
| 439 | if (!env_get("serial#")) |
| 440 | env_set("serial#", serialno_str); |
| 441 | |
| 442 | return 0; |
| 443 | } |
| 444 | |
Quentin Schulz | dd03ea1 | 2024-03-11 13:01:45 +0100 | [diff] [blame] | 445 | __weak int rockchip_early_misc_init_r(void) |
| 446 | { |
| 447 | return 0; |
| 448 | } |
| 449 | |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 450 | __weak int misc_init_r(void) |
| 451 | { |
Jonas Karlman | be56bb5 | 2023-02-22 22:44:41 +0000 | [diff] [blame] | 452 | const u32 cpuid_offset = CFG_CPUID_OFFSET; |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 453 | const u32 cpuid_length = 0x10; |
| 454 | u8 cpuid[cpuid_length]; |
| 455 | int ret; |
| 456 | |
Quentin Schulz | dd03ea1 | 2024-03-11 13:01:45 +0100 | [diff] [blame] | 457 | ret = rockchip_early_misc_init_r(); |
| 458 | if (ret) |
| 459 | return ret; |
| 460 | |
Rohan Garg | cfdc192 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 461 | ret = rockchip_cpuid_from_efuse(cpuid_offset, cpuid_length, cpuid); |
| 462 | if (ret) |
| 463 | return ret; |
| 464 | |
| 465 | ret = rockchip_cpuid_set(cpuid, cpuid_length); |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | |
| 469 | ret = rockchip_setup_macaddr(); |
| 470 | |
| 471 | return ret; |
| 472 | } |
| 473 | #endif |
Chris Morgan | ea8e963 | 2024-01-02 09:46:52 -0600 | [diff] [blame] | 474 | |
| 475 | #if IS_ENABLED(CONFIG_BOARD_RNG_SEED) && IS_ENABLED(CONFIG_RNG_ROCKCHIP) |
| 476 | #include <rng.h> |
| 477 | |
| 478 | /* Use hardware rng to seed Linux random. */ |
| 479 | __weak int board_rng_seed(struct abuf *buf) |
| 480 | { |
| 481 | struct udevice *dev; |
| 482 | size_t len = 0x8; |
| 483 | u64 *data; |
| 484 | |
| 485 | data = malloc(len); |
| 486 | if (!data) { |
| 487 | printf("Out of memory\n"); |
| 488 | return -ENOMEM; |
| 489 | } |
| 490 | |
| 491 | if (uclass_get_device(UCLASS_RNG, 0, &dev) || !dev) { |
| 492 | printf("No RNG device\n"); |
| 493 | return -ENODEV; |
| 494 | } |
| 495 | |
| 496 | if (dm_rng_read(dev, data, len)) { |
| 497 | printf("Reading RNG failed\n"); |
| 498 | return -EIO; |
| 499 | } |
| 500 | |
| 501 | abuf_init_set(buf, data, len); |
| 502 | |
| 503 | return 0; |
| 504 | } |
| 505 | #endif |