blob: afe07be949a1bf82961f8deab89829bc5b604ed4 [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
12#include "tdx-cfg-block.h"
Simon Glass5d982852017-05-17 08:23:00 -060013#include <asm/setup.h>
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010014#include "tdx-common.h"
15
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +010016#define TORADEX_OUI 0x00142dUL
17
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010018#ifdef CONFIG_TDX_CFG_BLOCK
19static char tdx_serial_str[9];
20static char tdx_board_rev_str[6];
21
Igor Opaniuk717fa2c2020-07-15 13:30:57 +030022#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
23static char tdx_car_serial_str[9];
24static char tdx_car_rev_str[6];
25static char *tdx_carrier_board_name;
26#endif
27
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010028#ifdef CONFIG_REVISION_TAG
29u32 get_board_rev(void)
30{
31 /* Check validity */
32 if (!tdx_hw_tag.ver_major)
33 return 0;
34
35 return ((tdx_hw_tag.ver_major & 0xff) << 8) |
36 ((tdx_hw_tag.ver_minor & 0xf) << 4) |
37 ((tdx_hw_tag.ver_assembly & 0xf) + 0xa);
38}
39#endif /* CONFIG_TDX_CFG_BLOCK */
40
41#ifdef CONFIG_SERIAL_TAG
42void get_board_serial(struct tag_serialnr *serialnr)
43{
44 int array[8];
45 unsigned int serial = tdx_serial;
46 int i;
47
48 serialnr->low = 0;
49 serialnr->high = 0;
50
51 /* Check validity */
52 if (serial) {
53 /*
54 * Convert to Linux serial number format (hexadecimal coded
55 * decimal)
56 */
57 i = 7;
58 while (serial) {
59 array[i--] = serial % 10;
60 serial /= 10;
61 }
62 while (i >= 0)
63 array[i--] = 0;
64 serial = array[0];
65 for (i = 1; i < 8; i++) {
66 serial *= 16;
67 serial += array[i];
68 }
69
70 serialnr->low = serial;
71 }
72}
73#endif /* CONFIG_SERIAL_TAG */
74
75int show_board_info(void)
76{
77 unsigned char ethaddr[6];
78
79 if (read_tdx_cfg_block()) {
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +010080 printf("MISSING TORADEX CONFIG BLOCK\n");
81 tdx_eth_addr.oui = htonl(TORADEX_OUI << 8);
82 tdx_eth_addr.nic = htonl(tdx_serial << 8);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010083 checkboard();
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +010084 } else {
85 sprintf(tdx_serial_str, "%08u", tdx_serial);
86 sprintf(tdx_board_rev_str, "V%1d.%1d%c",
87 tdx_hw_tag.ver_major,
88 tdx_hw_tag.ver_minor,
89 (char)tdx_hw_tag.ver_assembly + 'A');
90
91 env_set("serial#", tdx_serial_str);
92
93 printf("Model: Toradex %s %s, Serial# %s\n",
94 toradex_modules[tdx_hw_tag.prodid],
95 tdx_board_rev_str,
96 tdx_serial_str);
Igor Opaniuk717fa2c2020-07-15 13:30:57 +030097#ifdef CONFIG_TDX_CFG_BLOCK_EXTRA
98 if (read_tdx_cfg_block_carrier()) {
99 printf("MISSING TORADEX CARRIER CONFIG BLOCKS\n");
100 try_migrate_tdx_cfg_block_carrier();
101 } else {
102 tdx_carrier_board_name = (char *)
103 toradex_carrier_boards[tdx_car_hw_tag.prodid];
104
105 sprintf(tdx_car_serial_str, "%08u", tdx_car_serial);
106 sprintf(tdx_car_rev_str, "V%1d.%1d%c",
107 tdx_car_hw_tag.ver_major,
108 tdx_car_hw_tag.ver_minor,
109 (char)tdx_car_hw_tag.ver_assembly +
110 'A');
111
112 env_set("carrier_serial#", tdx_car_serial_str);
113 printf("Carrier: Toradex %s %s, Serial# %s\n",
114 tdx_carrier_board_name,
115 tdx_car_rev_str,
116 tdx_car_serial_str);
117 }
118#endif
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100119 }
120
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100121 /*
122 * Check if environment contains a valid MAC address,
123 * set the one from config block if not
124 */
Simon Glass35affd72017-08-03 12:22:14 -0600125 if (!eth_env_get_enetaddr("ethaddr", ethaddr))
Simon Glassfd1e9592017-08-03 12:22:11 -0600126 eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100127
128#ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR
Simon Glass35affd72017-08-03 12:22:14 -0600129 if (!eth_env_get_enetaddr("eth1addr", ethaddr)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100130 /*
131 * Secondary MAC address is allocated from block
132 * 0x100000 higher then the first MAC address
133 */
134 memcpy(ethaddr, &tdx_eth_addr, 6);
135 ethaddr[3] += 0x10;
Simon Glassfd1e9592017-08-03 12:22:11 -0600136 eth_env_set_enetaddr("eth1addr", ethaddr);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100137 }
138#endif
139
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100140 return 0;
141}
142
Stefan Agnerbeaf4062016-11-30 13:41:52 -0800143#ifdef CONFIG_USB_GADGET_DOWNLOAD
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100144int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
145{
146 unsigned short usb_pid;
147
148 usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid;
149 put_unaligned(usb_pid, &dev->idProduct);
150
151 return 0;
152}
Stefan Agnerbeaf4062016-11-30 13:41:52 -0800153#endif
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100154
Stefan Agner37fa4122016-11-30 13:41:53 -0800155#if defined(CONFIG_OF_LIBFDT)
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900156int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100157{
158 if (tdx_serial) {
159 fdt_setprop(blob, 0, "serial-number", tdx_serial_str,
160 strlen(tdx_serial_str) + 1);
161 }
162
163 if (tdx_hw_tag.ver_major) {
164 char prod_id[5];
165
166 sprintf(prod_id, "%04u", tdx_hw_tag.prodid);
167 fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5);
168
169 fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str,
170 strlen(tdx_board_rev_str) + 1);
171 }
172
173 return 0;
174}
175#endif
176
177#else /* CONFIG_TDX_CFG_BLOCK */
178
179#ifdef CONFIG_REVISION_TAG
180u32 get_board_rev(void)
181{
182 return 0;
183}
184#endif /* CONFIG_REVISION_TAG */
185
186#ifdef CONFIG_SERIAL_TAG
187u32 get_board_serial(void)
188{
189 return 0;
190}
191#endif /* CONFIG_SERIAL_TAG */
192
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900193int ft_common_board_setup(void *blob, struct bd_info *bd)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100194{
195 return 0;
196}
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100197
198#endif /* CONFIG_TDX_CFG_BLOCK */