Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * RK3399: Architecture common definitions |
| 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 |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <env.h> |
| 14 | #include <dm.h> |
Simon Glass | 4d72caa | 2020-05-10 11:40:01 -0600 | [diff] [blame] | 15 | #include <hash.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 16 | #include <log.h> |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 17 | #include <dm/uclass-internal.h> |
| 18 | #include <misc.h> |
Simon Glass | 3db7110 | 2019-11-14 12:57:16 -0700 | [diff] [blame] | 19 | #include <u-boot/crc.h> |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 20 | #include <u-boot/sha256.h> |
| 21 | |
| 22 | #include <asm/arch-rockchip/misc.h> |
| 23 | |
| 24 | int rockchip_setup_macaddr(void) |
| 25 | { |
Jonas Karlman | 628fb06 | 2023-02-22 22:44:40 +0000 | [diff] [blame] | 26 | #if CONFIG_IS_ENABLED(HASH) && CONFIG_IS_ENABLED(SHA256) |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 27 | int ret; |
| 28 | const char *cpuid = env_get("cpuid#"); |
| 29 | u8 hash[SHA256_SUM_LEN]; |
| 30 | int size = sizeof(hash); |
| 31 | u8 mac_addr[6]; |
| 32 | |
| 33 | /* Only generate a MAC address, if none is set in the environment */ |
| 34 | if (env_get("ethaddr")) |
Heiko Stuebner | d490fad | 2019-11-29 16:40:42 +0100 | [diff] [blame] | 35 | return 0; |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 36 | |
| 37 | if (!cpuid) { |
| 38 | debug("%s: could not retrieve 'cpuid#'\n", __func__); |
| 39 | return -1; |
| 40 | } |
| 41 | |
| 42 | ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); |
| 43 | if (ret) { |
| 44 | debug("%s: failed to calculate SHA256\n", __func__); |
| 45 | return -1; |
| 46 | } |
| 47 | |
| 48 | /* Copy 6 bytes of the hash to base the MAC address on */ |
| 49 | memcpy(mac_addr, hash, 6); |
| 50 | |
| 51 | /* Make this a valid MAC address and set it */ |
| 52 | mac_addr[0] &= 0xfe; /* clear multicast bit */ |
| 53 | mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ |
| 54 | eth_env_set_enetaddr("ethaddr", mac_addr); |
Jonas Karlman | 628fb06 | 2023-02-22 22:44:40 +0000 | [diff] [blame] | 55 | |
| 56 | /* Make a valid MAC address for ethernet1 */ |
| 57 | mac_addr[5] ^= 0x01; |
| 58 | eth_env_set_enetaddr("eth1addr", mac_addr); |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 59 | #endif |
| 60 | return 0; |
| 61 | } |
| 62 | |
| 63 | int rockchip_cpuid_from_efuse(const u32 cpuid_offset, |
| 64 | const u32 cpuid_length, |
| 65 | u8 *cpuid) |
| 66 | { |
Simon Glass | ce119a2 | 2023-02-05 17:54:59 -0700 | [diff] [blame] | 67 | #if IS_ENABLED(CONFIG_ROCKCHIP_EFUSE) || IS_ENABLED(CONFIG_ROCKCHIP_OTP) |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 68 | struct udevice *dev; |
| 69 | int ret; |
| 70 | |
| 71 | /* retrieve the device */ |
Simon Glass | 3c9adeb | 2023-02-05 15:40:40 -0700 | [diff] [blame] | 72 | #if IS_ENABLED(CONFIG_ROCKCHIP_EFUSE) |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 73 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 74 | DM_DRIVER_GET(rockchip_efuse), &dev); |
Simon Glass | ce119a2 | 2023-02-05 17:54:59 -0700 | [diff] [blame] | 75 | #elif IS_ENABLED(CONFIG_ROCKCHIP_OTP) |
Heiko Stuebner | e61350a | 2019-09-25 20:21:21 +0200 | [diff] [blame] | 76 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
Simon Glass | 65e25be | 2020-12-28 20:34:56 -0700 | [diff] [blame] | 77 | DM_DRIVER_GET(rockchip_otp), &dev); |
Heiko Stuebner | e61350a | 2019-09-25 20:21:21 +0200 | [diff] [blame] | 78 | #endif |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 79 | if (ret) { |
| 80 | debug("%s: could not find efuse device\n", __func__); |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | /* read the cpu_id range from the efuses */ |
Heiko Stuebner | 03f98b7 | 2019-09-25 20:40:56 +0200 | [diff] [blame] | 85 | ret = misc_read(dev, cpuid_offset, cpuid, cpuid_length); |
John Keeping | 999680c | 2023-03-27 12:01:08 +0100 | [diff] [blame] | 86 | if (ret < 0) { |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 87 | debug("%s: reading cpuid from the efuses failed\n", |
| 88 | __func__); |
| 89 | return -1; |
| 90 | } |
| 91 | #endif |
| 92 | return 0; |
| 93 | } |
| 94 | |
| 95 | int rockchip_cpuid_set(const u8 *cpuid, const u32 cpuid_length) |
| 96 | { |
| 97 | u8 low[cpuid_length / 2], high[cpuid_length / 2]; |
| 98 | char cpuid_str[cpuid_length * 2 + 1]; |
| 99 | u64 serialno; |
| 100 | char serialno_str[17]; |
Heiko Stuebner | fd3a7ae | 2019-11-29 16:40:43 +0100 | [diff] [blame] | 101 | const char *oldid; |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 102 | int i; |
| 103 | |
| 104 | memset(cpuid_str, 0, sizeof(cpuid_str)); |
| 105 | for (i = 0; i < 16; i++) |
| 106 | sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); |
| 107 | |
| 108 | debug("cpuid: %s\n", cpuid_str); |
| 109 | |
| 110 | /* |
| 111 | * Mix the cpuid bytes using the same rules as in |
| 112 | * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c |
| 113 | */ |
| 114 | for (i = 0; i < 8; i++) { |
| 115 | low[i] = cpuid[1 + (i << 1)]; |
| 116 | high[i] = cpuid[i << 1]; |
| 117 | } |
| 118 | |
| 119 | serialno = crc32_no_comp(0, low, 8); |
| 120 | serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; |
| 121 | snprintf(serialno_str, sizeof(serialno_str), "%016llx", serialno); |
| 122 | |
Heiko Stuebner | fd3a7ae | 2019-11-29 16:40:43 +0100 | [diff] [blame] | 123 | oldid = env_get("cpuid#"); |
| 124 | if (oldid && strcmp(oldid, cpuid_str) != 0) |
| 125 | printf("cpuid: value %s present in env does not match hardware %s\n", |
| 126 | oldid, cpuid_str); |
| 127 | |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 128 | env_set("cpuid#", cpuid_str); |
Heiko Stuebner | fd3a7ae | 2019-11-29 16:40:43 +0100 | [diff] [blame] | 129 | |
| 130 | /* Only generate serial# when none is set yet */ |
| 131 | if (!env_get("serial#")) |
| 132 | env_set("serial#", serialno_str); |
Rohan Garg | 0482538 | 2019-08-12 17:04:34 +0200 | [diff] [blame] | 133 | |
| 134 | return 0; |
| 135 | } |