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