Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2017 Theobroma Systems Design und Consulting GmbH |
| 3 | * |
| 4 | * SPDX-License-Identifier: GPL-2.0+ |
| 5 | */ |
Philipp Tomsich | 3df6626 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 6 | |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 7 | #include <common.h> |
| 8 | #include <dm.h> |
Philipp Tomsich | 7674d16 | 2017-04-28 17:31:44 +0200 | [diff] [blame] | 9 | #include <misc.h> |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 10 | #include <dm/pinctrl.h> |
| 11 | #include <dm/uclass-internal.h> |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 12 | #include <asm/setup.h> |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 13 | #include <asm/arch/periph.h> |
| 14 | #include <power/regulator.h> |
Philipp Tomsich | 3df6626 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 15 | #include <spl.h> |
Philipp Tomsich | 7674d16 | 2017-04-28 17:31:44 +0200 | [diff] [blame] | 16 | #include <u-boot/sha256.h> |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 17 | |
| 18 | DECLARE_GLOBAL_DATA_PTR; |
| 19 | |
| 20 | int board_init(void) |
| 21 | { |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 22 | int ret; |
| 23 | |
| 24 | /* |
Philipp Tomsich | 96351ff | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 25 | * We need to call into regulators_enable_boot_on() again, as the call |
| 26 | * during SPL may have not included all regulators. |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 27 | */ |
Philipp Tomsich | 96351ff | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 28 | ret = regulators_enable_boot_on(false); |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 29 | if (ret) |
Philipp Tomsich | 96351ff | 2017-09-29 19:28:00 +0200 | [diff] [blame] | 30 | debug("%s: Cannot enable boot on regulator\n", __func__); |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 31 | |
Klaus Goger | 8103993 | 2017-04-07 19:13:38 +0200 | [diff] [blame] | 32 | return 0; |
| 33 | } |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 34 | |
Philipp Tomsich | 3df6626 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 35 | void spl_board_init(void) |
| 36 | { |
Philipp Tomsich | 75fc208 | 2017-09-29 19:28:01 +0200 | [diff] [blame] | 37 | int ret; |
| 38 | |
| 39 | /* |
| 40 | * Turning the eMMC and SPI back on (if disabled via the Qseven |
| 41 | * BIOS_ENABLE) signal is done through a always-on regulator). |
| 42 | */ |
| 43 | ret = regulators_enable_boot_on(false); |
| 44 | if (ret) |
| 45 | debug("%s: Cannot enable boot on regulator\n", __func__); |
| 46 | |
Philipp Tomsich | 3df6626 | 2017-09-29 19:27:54 +0200 | [diff] [blame] | 47 | preloader_console_init(); |
| 48 | } |
| 49 | |
Klaus Goger | 6065aae | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 50 | static void setup_macaddr(void) |
| 51 | { |
| 52 | #if CONFIG_IS_ENABLED(CMD_NET) |
| 53 | int ret; |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 54 | const char *cpuid = env_get("cpuid#"); |
Klaus Goger | 6065aae | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 55 | u8 hash[SHA256_SUM_LEN]; |
| 56 | int size = sizeof(hash); |
| 57 | u8 mac_addr[6]; |
| 58 | |
| 59 | /* Only generate a MAC address, if none is set in the environment */ |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 60 | if (env_get("ethaddr")) |
Klaus Goger | 6065aae | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 61 | return; |
| 62 | |
| 63 | if (!cpuid) { |
| 64 | debug("%s: could not retrieve 'cpuid#'\n", __func__); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | ret = hash_block("sha256", (void *)cpuid, strlen(cpuid), hash, &size); |
| 69 | if (ret) { |
| 70 | debug("%s: failed to calculate SHA256\n", __func__); |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | /* Copy 6 bytes of the hash to base the MAC address on */ |
| 75 | memcpy(mac_addr, hash, 6); |
| 76 | |
| 77 | /* Make this a valid MAC address and set it */ |
| 78 | mac_addr[0] &= 0xfe; /* clear multicast bit */ |
| 79 | mac_addr[0] |= 0x02; /* set local assignment bit (IEEE802) */ |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 80 | eth_env_set_enetaddr("ethaddr", mac_addr); |
Klaus Goger | 6065aae | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 81 | #endif |
Klaus Goger | 6065aae | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 82 | } |
| 83 | |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 84 | static void setup_serial(void) |
| 85 | { |
| 86 | #if CONFIG_IS_ENABLED(ROCKCHIP_EFUSE) |
Kever Yang | 627562b | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 87 | const u32 cpuid_offset = 0x7; |
| 88 | const u32 cpuid_length = 0x10; |
| 89 | |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 90 | struct udevice *dev; |
| 91 | int ret, i; |
Kever Yang | 627562b | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 92 | u8 cpuid[cpuid_length]; |
| 93 | u8 low[cpuid_length/2], high[cpuid_length/2]; |
| 94 | char cpuid_str[cpuid_length * 2 + 1]; |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 95 | u64 serialno; |
Klaus Goger | 749b987 | 2017-09-15 14:46:04 +0200 | [diff] [blame] | 96 | char serialno_str[17]; |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 97 | |
Klaus Goger | 6065aae | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 98 | /* retrieve the device */ |
| 99 | ret = uclass_get_device_by_driver(UCLASS_MISC, |
| 100 | DM_GET_DRIVER(rockchip_efuse), &dev); |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 101 | if (ret) { |
| 102 | debug("%s: could not find efuse device\n", __func__); |
| 103 | return; |
| 104 | } |
| 105 | |
| 106 | /* read the cpu_id range from the efuses */ |
Kever Yang | 627562b | 2017-07-27 19:59:03 +0800 | [diff] [blame] | 107 | ret = misc_read(dev, cpuid_offset, &cpuid, sizeof(cpuid)); |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 108 | if (ret) { |
| 109 | debug("%s: reading cpuid from the efuses failed\n", |
| 110 | __func__); |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | memset(cpuid_str, 0, sizeof(cpuid_str)); |
| 115 | for (i = 0; i < 16; i++) |
| 116 | sprintf(&cpuid_str[i * 2], "%02x", cpuid[i]); |
| 117 | |
| 118 | debug("cpuid: %s\n", cpuid_str); |
| 119 | |
| 120 | /* |
| 121 | * Mix the cpuid bytes using the same rules as in |
| 122 | * ${linux}/drivers/soc/rockchip/rockchip-cpuinfo.c |
| 123 | */ |
| 124 | for (i = 0; i < 8; i++) { |
| 125 | low[i] = cpuid[1 + (i << 1)]; |
| 126 | high[i] = cpuid[i << 1]; |
| 127 | } |
| 128 | |
| 129 | serialno = crc32_no_comp(0, low, 8); |
| 130 | serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32; |
| 131 | snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno); |
| 132 | |
Simon Glass | 6a38e41 | 2017-08-03 12:22:09 -0600 | [diff] [blame] | 133 | env_set("cpuid#", cpuid_str); |
| 134 | env_set("serial#", serialno_str); |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 135 | #endif |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | int misc_init_r(void) |
| 139 | { |
| 140 | setup_serial(); |
Klaus Goger | 6065aae | 2017-05-05 19:21:40 +0200 | [diff] [blame] | 141 | setup_macaddr(); |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 142 | |
| 143 | return 0; |
| 144 | } |
| 145 | |
| 146 | #ifdef CONFIG_SERIAL_TAG |
| 147 | void get_board_serial(struct tag_serialnr *serialnr) |
| 148 | { |
| 149 | char *serial_string; |
| 150 | u64 serial = 0; |
| 151 | |
Simon Glass | 64b723f | 2017-08-03 12:22:12 -0600 | [diff] [blame] | 152 | serial_string = env_get("serial#"); |
Philipp Tomsich | 2ebe289 | 2017-05-05 19:21:39 +0200 | [diff] [blame] | 153 | |
| 154 | if (serial_string) |
| 155 | serial = simple_strtoull(serial_string, NULL, 16); |
| 156 | |
| 157 | serialnr->high = (u32)(serial >> 32); |
| 158 | serialnr->low = (u32)(serial & 0xffffffff); |
| 159 | } |
| 160 | #endif |