1 /*
2 * (C) Copyright 2017 Rockchip Electronics Co., Ltd
3 *
4 * SPDX-License-Identifier: GPL-2.0+
5 */
6
7 #include <common.h>
8 #include <dm.h>
9 #include <misc.h>
10 #include <time.h>
11 #include <asm/gpio.h>
12 #include <asm/io.h>
13 #include <asm/setup.h>
14 #include <asm/arch/uart.h>
15 #include <asm/arch/vendor.h>
16 #include <configs/gva_rk3229.h>
17
18 DECLARE_GLOBAL_DATA_PTR;
19
20 /* define serialno max length, the max length is 512 Bytes
21 * The remaining bytes are used to ensure that the first 512 bytes
22 * are valid when executing 'env_set("serial#", value)'.
23 */
24 #define VENDOR_SN_MAX 513
25 /* These values are provided by the chip documentation */
26 #define RK3229_CPUID_OFF 0x7
27 #define RK3229_CPUID_LEN 0x10
28
29 /*
30 * First obtain the serial number from vendor partition,
31 * if vendor partition is not initialized, then generate
32 * a default serial number according to CPU Id.
33 */
set_serialno(void)34 int set_serialno(void)
35 {
36 char serialno_str[VENDOR_SN_MAX];
37 struct udevice *dev;
38 u8 cpuid[RK3229_CPUID_LEN] = {0};
39 u8 low[RK3229_CPUID_LEN/2], high[RK3229_CPUID_LEN/2];
40 u64 serialno;
41 int ret, i;
42
43 /* Read serial number from vendor storage part */
44 memset(serialno_str, 0, VENDOR_SN_MAX);
45 #ifdef CONFIG_VENDOR_STORAGE_API
46 ret = vendor_storage_read(VENDOR_SN_ID, serialno_str, (VENDOR_SN_MAX-1));
47 if (ret > 0) {
48 env_set("serial#", serialno_str);
49 } else {
50 #endif
51 #ifdef CONFIG_ROCKCHIP_EFUSE
52 /* retrieve the device */
53 ret = uclass_get_device_by_driver(UCLASS_MISC,
54 DM_GET_DRIVER(rockchip_efuse), &dev);
55 if (ret) {
56 printf("%s: could not find efuse device\n", __func__);
57 return ret;
58 }
59 /* read the cpu_id range from the efuses */
60 ret = misc_read(dev, RK3229_CPUID_OFF, &cpuid, sizeof(cpuid));
61 if (ret) {
62 printf("%s: reading cpuid from the efuses failed\n", __func__);
63 return ret;
64 }
65 #endif
66 /* Generate the serial number based on CPU ID */
67 for (i = 0; i < 8; i++) {
68 low[i] = cpuid[1 + (i << 1)];
69 high[i] = cpuid[i << 1];
70 }
71 serialno = crc32_no_comp(0, low, 8);
72 serialno |= (u64)crc32_no_comp(serialno, high, 8) << 32;
73 snprintf(serialno_str, sizeof(serialno_str), "%llx", serialno);
74
75 env_set("serial#", serialno_str);
76 #ifdef CONFIG_VENDOR_STORAGE_API
77 }
78 #endif
79 return 0;
80 }
81
82 #ifdef CONFIG_MISC_INIT_R
misc_init_r(void)83 int misc_init_r(void)
84 {
85 set_serialno();
86
87 return 0;
88 }
89 #endif
90
91 #ifdef CONFIG_SERIAL_TAG
get_board_serial(struct tag_serialnr * serialnr)92 void get_board_serial(struct tag_serialnr *serialnr)
93 {
94 char *serial_string;
95 u64 serial = 0;
96
97 serial_string = env_get("serial#");
98
99 if (serial_string)
100 serial = simple_strtoull(serial_string, NULL, 16);
101
102 serialnr->high = (u32)(serial >> 32);
103 serialnr->low = (u32)(serial & 0xffffffff);
104 }
105 #endif
106
107 #define FASTBOOT_KEY_GPIO 43 /*GPIO1B3*/
108
fastboot_key_pressed(void)109 int fastboot_key_pressed(void)
110 {
111 gpio_request(FASTBOOT_KEY_GPIO, "fastboot_key");
112 gpio_direction_input(FASTBOOT_KEY_GPIO);
113 return !gpio_get_value(FASTBOOT_KEY_GPIO);
114 }
115
rk_board_late_init(void)116 int rk_board_late_init(void)
117 {
118 if (fastboot_key_pressed()) {
119 printf("enter fastboot!\n");
120 env_set("preboot", "setenv preboot; fastboot usb0");
121 }
122
123 return 0;
124 }
125