blob: 3643ebbb16328d76804ab0778e31a326032300db [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01002/*
3 * Copyright (c) 2016 Toradex, Inc.
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
Simon Glass9fb625c2019-08-01 09:46:51 -06007#include <env.h>
Marcel Ziswilera2777ec2016-11-16 17:49:22 +01008#include <g_dnl.h>
Simon Glass691d7192020-05-10 11:40:02 -06009#include <init.h>
Masahiro Yamadab08c8c42018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010011
Igor Opaniukd3241892020-07-15 13:31:01 +030012#ifdef CONFIG_DM_VIDEO
13#include <bmp_logo.h>
14#include <dm.h>
15#include <splash.h>
16#include <video.h>
17#endif
18
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010019#include "tdx-cfg-block.h"
Simon Glass5d982852017-05-17 08:23:00 -060020#include <asm/setup.h>
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010021#include "tdx-common.h"
22
Philippe Schenker494ef102022-06-13 19:35:22 +020023#define SERIAL_STR_LEN 8
24#define MODULE_VER_STR_LEN 4 // V1.1
Philippe Schenker7e27ce12022-06-13 19:35:23 +020025#define MODULE_REV_STR_LEN 3 // [A-Z] or #[26-99]
Philippe Schenker494ef102022-06-13 19:35:22 +020026
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010027#ifdef CONFIG_TDX_CFG_BLOCK
Philippe Schenker494ef102022-06-13 19:35:22 +020028static char tdx_serial_str[SERIAL_STR_LEN + 1];
29static char tdx_board_rev_str[MODULE_VER_STR_LEN + MODULE_REV_STR_LEN + 1];
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010030
Igor Opaniuk717fa2c2020-07-15 13:30:57 +030031#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
Philippe Schenker494ef102022-06-13 19:35:22 +020032static char tdx_car_serial_str[SERIAL_STR_LEN + 1];
33static char tdx_car_rev_str[MODULE_VER_STR_LEN + MODULE_REV_STR_LEN + 1];
Igor Opaniuk717fa2c2020-07-15 13:30:57 +030034static char *tdx_carrier_board_name;
35#endif
36
Tom Rini97744622021-08-30 09:16:30 -040037#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
38u32 get_board_revision(void)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010039{
40 /* Check validity */
41 if (!tdx_hw_tag.ver_major)
42 return 0;
43
44 return ((tdx_hw_tag.ver_major & 0xff) << 8) |
45 ((tdx_hw_tag.ver_minor & 0xf) << 4) |
46 ((tdx_hw_tag.ver_assembly & 0xf) + 0xa);
47}
48#endif /* CONFIG_TDX_CFG_BLOCK */
49
50#ifdef CONFIG_SERIAL_TAG
51void get_board_serial(struct tag_serialnr *serialnr)
52{
53 int array[8];
54 unsigned int serial = tdx_serial;
55 int i;
56
57 serialnr->low = 0;
58 serialnr->high = 0;
59
60 /* Check validity */
61 if (serial) {
62 /*
63 * Convert to Linux serial number format (hexadecimal coded
64 * decimal)
65 */
66 i = 7;
67 while (serial) {
68 array[i--] = serial % 10;
69 serial /= 10;
70 }
71 while (i >= 0)
72 array[i--] = 0;
73 serial = array[0];
74 for (i = 1; i < 8; i++) {
75 serial *= 16;
76 serial += array[i];
77 }
78
79 serialnr->low = serial;
80 }
81}
82#endif /* CONFIG_SERIAL_TAG */
83
Philippe Schenker7e27ce12022-06-13 19:35:23 +020084static const char *get_board_assembly(u16 ver_assembly)
85{
86 static char ver_name[MODULE_REV_STR_LEN + 1];
87
88 if (ver_assembly < 26) {
89 ver_name[0] = (char)ver_assembly + 'A';
90 ver_name[1] = '\0';
91 } else {
92 snprintf(ver_name, sizeof(ver_name),
93 "#%u", ver_assembly);
94 }
95
96 return ver_name;
97}
98
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010099int show_board_info(void)
100{
101 unsigned char ethaddr[6];
102
103 if (read_tdx_cfg_block()) {
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +0100104 printf("MISSING TORADEX CONFIG BLOCK\n");
Philippe Schenker1cf4e792022-06-20 16:57:45 +0200105 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100106 checkboard();
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +0100107 } else {
Philippe Schenker39ff0622022-06-13 19:35:21 +0200108 snprintf(tdx_serial_str, sizeof(tdx_serial_str),
109 "%08u", tdx_serial);
110 snprintf(tdx_board_rev_str, sizeof(tdx_board_rev_str),
Philippe Schenker7e27ce12022-06-13 19:35:23 +0200111 "V%1d.%1d%s",
Philippe Schenker39ff0622022-06-13 19:35:21 +0200112 tdx_hw_tag.ver_major,
113 tdx_hw_tag.ver_minor,
Philippe Schenker7e27ce12022-06-13 19:35:23 +0200114 get_board_assembly(tdx_hw_tag.ver_assembly));
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +0100115
116 env_set("serial#", tdx_serial_str);
117
118 printf("Model: Toradex %s %s, Serial# %s\n",
Francesco Dolcini8b6dc5d2022-07-21 15:17:34 +0200119 toradex_modules[tdx_hw_tag.prodid].name,
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +0100120 tdx_board_rev_str,
121 tdx_serial_str);
Igor Opaniuk717fa2c2020-07-15 13:30:57 +0300122#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
123 if (read_tdx_cfg_block_carrier()) {
124 printf("MISSING TORADEX CARRIER CONFIG BLOCKS\n");
125 try_migrate_tdx_cfg_block_carrier();
126 } else {
127 tdx_carrier_board_name = (char *)
128 toradex_carrier_boards[tdx_car_hw_tag.prodid];
129
Philippe Schenker39ff0622022-06-13 19:35:21 +0200130 snprintf(tdx_car_serial_str, sizeof(tdx_car_serial_str),
131 "%08u", tdx_car_serial);
132 snprintf(tdx_car_rev_str, sizeof(tdx_car_rev_str),
Philippe Schenker7e27ce12022-06-13 19:35:23 +0200133 "V%1d.%1d%s",
Philippe Schenker39ff0622022-06-13 19:35:21 +0200134 tdx_car_hw_tag.ver_major,
135 tdx_car_hw_tag.ver_minor,
Philippe Schenker7e27ce12022-06-13 19:35:23 +0200136 get_board_assembly(tdx_car_hw_tag.ver_assembly));
Igor Opaniuk717fa2c2020-07-15 13:30:57 +0300137
138 env_set("carrier_serial#", tdx_car_serial_str);
139 printf("Carrier: Toradex %s %s, Serial# %s\n",
140 tdx_carrier_board_name,
141 tdx_car_rev_str,
142 tdx_car_serial_str);
143 }
144#endif
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100145 }
146
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100147 /*
148 * Check if environment contains a valid MAC address,
149 * set the one from config block if not
150 */
Simon Glass35affd72017-08-03 12:22:14 -0600151 if (!eth_env_get_enetaddr("ethaddr", ethaddr))
Simon Glassfd1e9592017-08-03 12:22:11 -0600152 eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100153
154#ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR
Simon Glass35affd72017-08-03 12:22:14 -0600155 if (!eth_env_get_enetaddr("eth1addr", ethaddr)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100156 /*
157 * Secondary MAC address is allocated from block
158 * 0x100000 higher then the first MAC address
159 */
160 memcpy(ethaddr, &tdx_eth_addr, 6);
161 ethaddr[3] += 0x10;
Simon Glassfd1e9592017-08-03 12:22:11 -0600162 eth_env_set_enetaddr("eth1addr", ethaddr);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100163 }
164#endif
165
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100166 return 0;
167}
168
Stefan Agner421440f2021-07-23 09:39:46 +0300169#ifdef CONFIG_TDX_CFG_BLOCK_USB_GADGET_PID
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100170int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
171{
172 unsigned short usb_pid;
173
174 usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid;
175 put_unaligned(usb_pid, &dev->idProduct);
176
177 return 0;
178}
Stefan Agnerbeaf4062016-11-30 13:41:52 -0800179#endif
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100180
Stefan Agner37fa4122016-11-30 13:41:53 -0800181#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900182int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100183{
184 if (tdx_serial) {
185 fdt_setprop(blob, 0, "serial-number", tdx_serial_str,
186 strlen(tdx_serial_str) + 1);
187 }
188
189 if (tdx_hw_tag.ver_major) {
190 char prod_id[5];
191
Philippe Schenker39ff0622022-06-13 19:35:21 +0200192 snprintf(prod_id, sizeof(prod_id), "%04u", tdx_hw_tag.prodid);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100193 fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5);
194
195 fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str,
196 strlen(tdx_board_rev_str) + 1);
197 }
198
199 return 0;
200}
201#endif
202
203#else /* CONFIG_TDX_CFG_BLOCK */
204
Tom Rini97744622021-08-30 09:16:30 -0400205#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
206u32 get_board_revision(void)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100207{
208 return 0;
209}
210#endif /* CONFIG_REVISION_TAG */
211
212#ifdef CONFIG_SERIAL_TAG
213u32 get_board_serial(void)
214{
215 return 0;
216}
217#endif /* CONFIG_SERIAL_TAG */
218
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900219int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100220{
221 return 0;
222}
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100223
224#endif /* CONFIG_TDX_CFG_BLOCK */