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