blob: 5ad5d00a0d907035a6e61de478a1eaf2b02eb3d9 [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
Igor Opaniuk897822d2020-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 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
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +010023#define TORADEX_OUI 0x00142dUL
24
Philippe Schenker1832bd32022-06-13 19:35:22 +020025#define SERIAL_STR_LEN 8
26#define MODULE_VER_STR_LEN 4 // V1.1
Philippe Schenker498f95a2022-06-13 19:35:23 +020027#define MODULE_REV_STR_LEN 3 // [A-Z] or #[26-99]
Philippe Schenker1832bd32022-06-13 19:35:22 +020028
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010029#ifdef CONFIG_TDX_CFG_BLOCK
Philippe Schenker1832bd32022-06-13 19:35:22 +020030static char tdx_serial_str[SERIAL_STR_LEN + 1];
31static char tdx_board_rev_str[MODULE_VER_STR_LEN + MODULE_REV_STR_LEN + 1];
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010032
Igor Opaniukecfe0f52020-07-15 13:30:57 +030033#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
Philippe Schenker1832bd32022-06-13 19:35:22 +020034static char tdx_car_serial_str[SERIAL_STR_LEN + 1];
35static char tdx_car_rev_str[MODULE_VER_STR_LEN + MODULE_REV_STR_LEN + 1];
Igor Opaniukecfe0f52020-07-15 13:30:57 +030036static char *tdx_carrier_board_name;
37#endif
38
Tom Rini4cc38852021-08-30 09:16:30 -040039#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
40u32 get_board_revision(void)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010041{
42 /* Check validity */
43 if (!tdx_hw_tag.ver_major)
44 return 0;
45
46 return ((tdx_hw_tag.ver_major & 0xff) << 8) |
47 ((tdx_hw_tag.ver_minor & 0xf) << 4) |
48 ((tdx_hw_tag.ver_assembly & 0xf) + 0xa);
49}
50#endif /* CONFIG_TDX_CFG_BLOCK */
51
52#ifdef CONFIG_SERIAL_TAG
53void get_board_serial(struct tag_serialnr *serialnr)
54{
55 int array[8];
56 unsigned int serial = tdx_serial;
57 int i;
58
59 serialnr->low = 0;
60 serialnr->high = 0;
61
62 /* Check validity */
63 if (serial) {
64 /*
65 * Convert to Linux serial number format (hexadecimal coded
66 * decimal)
67 */
68 i = 7;
69 while (serial) {
70 array[i--] = serial % 10;
71 serial /= 10;
72 }
73 while (i >= 0)
74 array[i--] = 0;
75 serial = array[0];
76 for (i = 1; i < 8; i++) {
77 serial *= 16;
78 serial += array[i];
79 }
80
81 serialnr->low = serial;
82 }
83}
84#endif /* CONFIG_SERIAL_TAG */
85
Philippe Schenker498f95a2022-06-13 19:35:23 +020086static const char *get_board_assembly(u16 ver_assembly)
87{
88 static char ver_name[MODULE_REV_STR_LEN + 1];
89
90 if (ver_assembly < 26) {
91 ver_name[0] = (char)ver_assembly + 'A';
92 ver_name[1] = '\0';
93 } else {
94 snprintf(ver_name, sizeof(ver_name),
95 "#%u", ver_assembly);
96 }
97
98 return ver_name;
99}
100
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100101int show_board_info(void)
102{
103 unsigned char ethaddr[6];
104
105 if (read_tdx_cfg_block()) {
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100106 printf("MISSING TORADEX CONFIG BLOCK\n");
107 tdx_eth_addr.oui = htonl(TORADEX_OUI << 8);
108 tdx_eth_addr.nic = htonl(tdx_serial << 8);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100109 checkboard();
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100110 } else {
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200111 snprintf(tdx_serial_str, sizeof(tdx_serial_str),
112 "%08u", tdx_serial);
113 snprintf(tdx_board_rev_str, sizeof(tdx_board_rev_str),
Philippe Schenker498f95a2022-06-13 19:35:23 +0200114 "V%1d.%1d%s",
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200115 tdx_hw_tag.ver_major,
116 tdx_hw_tag.ver_minor,
Philippe Schenker498f95a2022-06-13 19:35:23 +0200117 get_board_assembly(tdx_hw_tag.ver_assembly));
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100118
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100119 env_set("serial#", tdx_serial_str);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100120
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100121 printf("Model: Toradex %s %s, Serial# %s\n",
122 toradex_modules[tdx_hw_tag.prodid],
123 tdx_board_rev_str,
124 tdx_serial_str);
Igor Opaniukecfe0f52020-07-15 13:30:57 +0300125#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
126 if (read_tdx_cfg_block_carrier()) {
127 printf("MISSING TORADEX CARRIER CONFIG BLOCKS\n");
128 try_migrate_tdx_cfg_block_carrier();
129 } else {
130 tdx_carrier_board_name = (char *)
131 toradex_carrier_boards[tdx_car_hw_tag.prodid];
132
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200133 snprintf(tdx_car_serial_str, sizeof(tdx_car_serial_str),
134 "%08u", tdx_car_serial);
135 snprintf(tdx_car_rev_str, sizeof(tdx_car_rev_str),
Philippe Schenker498f95a2022-06-13 19:35:23 +0200136 "V%1d.%1d%s",
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200137 tdx_car_hw_tag.ver_major,
138 tdx_car_hw_tag.ver_minor,
Philippe Schenker498f95a2022-06-13 19:35:23 +0200139 get_board_assembly(tdx_car_hw_tag.ver_assembly));
Igor Opaniukecfe0f52020-07-15 13:30:57 +0300140
141 env_set("carrier_serial#", tdx_car_serial_str);
142 printf("Carrier: Toradex %s %s, Serial# %s\n",
143 tdx_carrier_board_name,
144 tdx_car_rev_str,
145 tdx_car_serial_str);
146 }
147#endif
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +0100148 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100149
150 /*
151 * Check if environment contains a valid MAC address,
152 * set the one from config block if not
153 */
Simon Glass399a9ce2017-08-03 12:22:14 -0600154 if (!eth_env_get_enetaddr("ethaddr", ethaddr))
Simon Glass8551d552017-08-03 12:22:11 -0600155 eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100156
157#ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR
Simon Glass399a9ce2017-08-03 12:22:14 -0600158 if (!eth_env_get_enetaddr("eth1addr", ethaddr)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100159 /*
160 * Secondary MAC address is allocated from block
161 * 0x100000 higher then the first MAC address
162 */
163 memcpy(ethaddr, &tdx_eth_addr, 6);
164 ethaddr[3] += 0x10;
Simon Glass8551d552017-08-03 12:22:11 -0600165 eth_env_set_enetaddr("eth1addr", ethaddr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100166 }
167#endif
168
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100169 return 0;
170}
171
Stefan Agner57985bd2021-07-23 09:39:46 +0300172#ifdef CONFIG_TDX_CFG_BLOCK_USB_GADGET_PID
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100173int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
174{
175 unsigned short usb_pid;
176
177 usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid;
178 put_unaligned(usb_pid, &dev->idProduct);
179
180 return 0;
181}
Stefan Agnera1886522016-11-30 13:41:52 -0800182#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100183
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800184#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900185int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100186{
187 if (tdx_serial) {
188 fdt_setprop(blob, 0, "serial-number", tdx_serial_str,
189 strlen(tdx_serial_str) + 1);
190 }
191
192 if (tdx_hw_tag.ver_major) {
193 char prod_id[5];
194
Philippe Schenker38cf79e2022-06-13 19:35:21 +0200195 snprintf(prod_id, sizeof(prod_id), "%04u", tdx_hw_tag.prodid);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100196 fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5);
197
198 fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str,
199 strlen(tdx_board_rev_str) + 1);
200 }
201
202 return 0;
203}
204#endif
205
206#else /* CONFIG_TDX_CFG_BLOCK */
207
Tom Rini4cc38852021-08-30 09:16:30 -0400208#if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG)
209u32 get_board_revision(void)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100210{
211 return 0;
212}
213#endif /* CONFIG_REVISION_TAG */
214
215#ifdef CONFIG_SERIAL_TAG
216u32 get_board_serial(void)
217{
218 return 0;
219}
220#endif /* CONFIG_SERIAL_TAG */
221
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900222int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100223{
224 return 0;
225}
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100226
227#endif /* CONFIG_TDX_CFG_BLOCK */