1 /*
2  * Copyright (c) 2016 Toradex, Inc.
3  *
4  * SPDX-License-Identifier:	GPL-2.0+
5  */
6 
7 #include <common.h>
8 #include <g_dnl.h>
9 #include <libfdt.h>
10 
11 #include "tdx-cfg-block.h"
12 #include <asm/setup.h>
13 #include "tdx-common.h"
14 
15 #ifdef CONFIG_TDX_CFG_BLOCK
16 static char tdx_serial_str[9];
17 static char tdx_board_rev_str[6];
18 
19 #ifdef CONFIG_REVISION_TAG
20 u32 get_board_rev(void)
21 {
22 	/* Check validity */
23 	if (!tdx_hw_tag.ver_major)
24 		return 0;
25 
26 	return ((tdx_hw_tag.ver_major & 0xff) << 8) |
27 		((tdx_hw_tag.ver_minor & 0xf) << 4) |
28 		((tdx_hw_tag.ver_assembly & 0xf) + 0xa);
29 }
30 #endif /* CONFIG_TDX_CFG_BLOCK */
31 
32 #ifdef CONFIG_SERIAL_TAG
33 void get_board_serial(struct tag_serialnr *serialnr)
34 {
35 	int array[8];
36 	unsigned int serial = tdx_serial;
37 	int i;
38 
39 	serialnr->low = 0;
40 	serialnr->high = 0;
41 
42 	/* Check validity */
43 	if (serial) {
44 		/*
45 		 * Convert to Linux serial number format (hexadecimal coded
46 		 * decimal)
47 		 */
48 		i = 7;
49 		while (serial) {
50 			array[i--] = serial % 10;
51 			serial /= 10;
52 		}
53 		while (i >= 0)
54 			array[i--] = 0;
55 		serial = array[0];
56 		for (i = 1; i < 8; i++) {
57 			serial *= 16;
58 			serial += array[i];
59 		}
60 
61 		serialnr->low = serial;
62 	}
63 }
64 #endif /* CONFIG_SERIAL_TAG */
65 
66 int show_board_info(void)
67 {
68 	unsigned char ethaddr[6];
69 
70 	if (read_tdx_cfg_block()) {
71 		printf("Missing Toradex config block\n");
72 		checkboard();
73 		return 0;
74 	}
75 
76 	/* board serial-number */
77 	sprintf(tdx_serial_str, "%08u", tdx_serial);
78 	sprintf(tdx_board_rev_str, "V%1d.%1d%c",
79 		tdx_hw_tag.ver_major,
80 		tdx_hw_tag.ver_minor,
81 		(char)tdx_hw_tag.ver_assembly + 'A');
82 
83 	setenv("serial#", tdx_serial_str);
84 
85 	/*
86 	 * Check if environment contains a valid MAC address,
87 	 * set the one from config block if not
88 	 */
89 	if (!eth_getenv_enetaddr("ethaddr", ethaddr))
90 		eth_setenv_enetaddr("ethaddr", (u8 *)&tdx_eth_addr);
91 
92 #ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR
93 	if (!eth_getenv_enetaddr("eth1addr", ethaddr)) {
94 		/*
95 		 * Secondary MAC address is allocated from block
96 		 * 0x100000 higher then the first MAC address
97 		 */
98 		memcpy(ethaddr, &tdx_eth_addr, 6);
99 		ethaddr[3] += 0x10;
100 		eth_setenv_enetaddr("eth1addr", ethaddr);
101 	}
102 #endif
103 
104 	printf("Model: Toradex %s %s, Serial# %s\n",
105 	       toradex_modules[tdx_hw_tag.prodid],
106 	       tdx_board_rev_str,
107 	       tdx_serial_str);
108 
109 	return 0;
110 }
111 
112 #ifdef CONFIG_USB_GADGET_DOWNLOAD
113 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
114 {
115 	unsigned short usb_pid;
116 
117 	usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid;
118 	put_unaligned(usb_pid, &dev->idProduct);
119 
120 	return 0;
121 }
122 #endif
123 
124 #if defined(CONFIG_OF_LIBFDT)
125 int ft_common_board_setup(void *blob, bd_t *bd)
126 {
127 	if (tdx_serial) {
128 		fdt_setprop(blob, 0, "serial-number", tdx_serial_str,
129 			    strlen(tdx_serial_str) + 1);
130 	}
131 
132 	if (tdx_hw_tag.ver_major) {
133 		char prod_id[5];
134 
135 		sprintf(prod_id, "%04u", tdx_hw_tag.prodid);
136 		fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5);
137 
138 		fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str,
139 			    strlen(tdx_board_rev_str) + 1);
140 	}
141 
142 	return 0;
143 }
144 #endif
145 
146 #else /* CONFIG_TDX_CFG_BLOCK */
147 
148 #ifdef CONFIG_REVISION_TAG
149 u32 get_board_rev(void)
150 {
151 	return 0;
152 }
153 #endif /* CONFIG_REVISION_TAG */
154 
155 #ifdef CONFIG_SERIAL_TAG
156 u32 get_board_serial(void)
157 {
158 	return 0;
159 }
160 #endif /* CONFIG_SERIAL_TAG */
161 
162 int ft_common_board_setup(void *blob, bd_t *bd)
163 {
164 	return 0;
165 }
166 
167 #endif /* CONFIG_TDX_CFG_BLOCK */
168