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