blob: 071961f3d93730854691b1fed24040b81f668f97 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01002/*
3 * Copyright (c) 2016 Toradex, Inc.
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01004 */
5
6#include <common.h>
Simon Glass5e6201b2019-08-01 09:46:51 -06007#include <env.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01008#include <g_dnl.h>
Simon Glass97589732020-05-10 11:40:02 -06009#include <init.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090010#include <linux/libfdt.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010011
Simon Glass52cb5042022-10-18 07:46:31 -060012#ifdef CONFIG_VIDEO
Igor Opaniuk897822d2020-07-15 13:31:01 +030013#include <bmp_logo.h>
14#include <dm.h>
15#include <splash.h>
16#include <video.h>
17#endif
18
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010019#include "tdx-cfg-block.h"
Simon Glassd9a766f2017-05-17 08:23:00 -060020#include <asm/setup.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010021#include "tdx-common.h"
22
Philippe Schenker1832bd32022-06-13 19:35:22 +020023#define SERIAL_STR_LEN 8
24#define MODULE_VER_STR_LEN 4 // V1.1
Philippe Schenker498f95a2022-06-13 19:35:23 +020025#define MODULE_REV_STR_LEN 3 // [A-Z] or #[26-99]
Philippe Schenker1832bd32022-06-13 19:35:22 +020026
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010027#ifdef CONFIG_TDX_CFG_BLOCK
Philippe Schenker1832bd32022-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 Ziswiler7a28dfc2016-11-16 17:49:22 +010030
Igor Opaniukecfe0f52020-07-15 13:30:57 +030031#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
Philippe Schenker1832bd32022-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 Opaniukecfe0f52020-07-15 13:30:57 +030034static char *tdx_carrier_board_name;
35#endif
36
Tom Rini4cc38852021-08-30 09:16:30 -040037#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
38u32 get_board_revision(void)
Marcel Ziswiler7a28dfc2016-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 Schenker498f95a2022-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 Ziswiler7a28dfc2016-11-16 17:49:22 +010099int show_board_info(void)
100{
101 unsigned char ethaddr[6];
102
103 if (read_tdx_cfg_block()) {
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100104 printf("MISSING TORADEX CONFIG BLOCK\n");
Philippe Schenkerd52a2572022-06-20 16:57:45 +0200105 get_mac_from_serial(tdx_serial, &tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100106 checkboard();
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100107 } else {
Philippe Schenker38cf79e2022-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 Schenker498f95a2022-06-13 19:35:23 +0200111 "V%1d.%1d%s",
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200112 tdx_hw_tag.ver_major,
113 tdx_hw_tag.ver_minor,
Philippe Schenker498f95a2022-06-13 19:35:23 +0200114 get_board_assembly(tdx_hw_tag.ver_assembly));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100115
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100116 env_set("serial#", tdx_serial_str);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100117
Francesco Dolcini6e9ba382022-07-21 15:17:38 +0200118 printf("Model: Toradex %04d %s %s\n",
119 tdx_hw_tag.prodid,
Francesco Dolcinifed3bc72022-07-21 15:17:34 +0200120 toradex_modules[tdx_hw_tag.prodid].name,
Francesco Dolcini6e9ba382022-07-21 15:17:38 +0200121 tdx_board_rev_str);
122 printf("Serial#: %s\n", tdx_serial_str);
Igor Opaniukecfe0f52020-07-15 13:30:57 +0300123#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
124 if (read_tdx_cfg_block_carrier()) {
125 printf("MISSING TORADEX CARRIER CONFIG BLOCKS\n");
126 try_migrate_tdx_cfg_block_carrier();
127 } else {
128 tdx_carrier_board_name = (char *)
129 toradex_carrier_boards[tdx_car_hw_tag.prodid];
130
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200131 snprintf(tdx_car_serial_str, sizeof(tdx_car_serial_str),
132 "%08u", tdx_car_serial);
133 snprintf(tdx_car_rev_str, sizeof(tdx_car_rev_str),
Philippe Schenker498f95a2022-06-13 19:35:23 +0200134 "V%1d.%1d%s",
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200135 tdx_car_hw_tag.ver_major,
136 tdx_car_hw_tag.ver_minor,
Philippe Schenker498f95a2022-06-13 19:35:23 +0200137 get_board_assembly(tdx_car_hw_tag.ver_assembly));
Igor Opaniukecfe0f52020-07-15 13:30:57 +0300138
139 env_set("carrier_serial#", tdx_car_serial_str);
140 printf("Carrier: Toradex %s %s, Serial# %s\n",
141 tdx_carrier_board_name,
142 tdx_car_rev_str,
143 tdx_car_serial_str);
144 }
145#endif
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100146 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100147
148 /*
149 * Check if environment contains a valid MAC address,
150 * set the one from config block if not
151 */
Simon Glass399a9ce2017-08-03 12:22:14 -0600152 if (!eth_env_get_enetaddr("ethaddr", ethaddr))
Simon Glass8551d552017-08-03 12:22:11 -0600153 eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100154
Francesco Dolcini14163d62022-07-21 15:17:36 +0200155 if (IS_ENABLED(CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR) &&
156 !eth_env_get_enetaddr("eth1addr", ethaddr)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100157 /*
158 * Secondary MAC address is allocated from block
159 * 0x100000 higher then the first MAC address
160 */
161 memcpy(ethaddr, &tdx_eth_addr, 6);
162 ethaddr[3] += 0x10;
Simon Glass8551d552017-08-03 12:22:11 -0600163 eth_env_set_enetaddr("eth1addr", ethaddr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100164 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100165
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100166 return 0;
167}
168
Stefan Agner57985bd2021-07-23 09:39:46 +0300169#ifdef CONFIG_TDX_CFG_BLOCK_USB_GADGET_PID
Marcel Ziswiler7a28dfc2016-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 Agnera1886522016-11-30 13:41:52 -0800179#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100180
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800181#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900182int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswiler7a28dfc2016-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 Schenker38cf79e2022-06-13 19:35:21 +0200192 snprintf(prod_id, sizeof(prod_id), "%04u", tdx_hw_tag.prodid);
Marcel Ziswiler7a28dfc2016-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 Rini4cc38852021-08-30 09:16:30 -0400205#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
206u32 get_board_revision(void)
Marcel Ziswiler7a28dfc2016-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 Yamadaf7ed78b2020-06-26 15:13:33 +0900219int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100220{
221 return 0;
222}
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100223
224#endif /* CONFIG_TDX_CFG_BLOCK */