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