blob: e8de923f3e1193cbac8681be541ab3e466bb0db3 [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
22#ifdef CONFIG_REVISION_TAG
23u32 get_board_rev(void)
24{
25 /* Check validity */
26 if (!tdx_hw_tag.ver_major)
27 return 0;
28
29 return ((tdx_hw_tag.ver_major & 0xff) << 8) |
30 ((tdx_hw_tag.ver_minor & 0xf) << 4) |
31 ((tdx_hw_tag.ver_assembly & 0xf) + 0xa);
32}
33#endif /* CONFIG_TDX_CFG_BLOCK */
34
35#ifdef CONFIG_SERIAL_TAG
36void get_board_serial(struct tag_serialnr *serialnr)
37{
38 int array[8];
39 unsigned int serial = tdx_serial;
40 int i;
41
42 serialnr->low = 0;
43 serialnr->high = 0;
44
45 /* Check validity */
46 if (serial) {
47 /*
48 * Convert to Linux serial number format (hexadecimal coded
49 * decimal)
50 */
51 i = 7;
52 while (serial) {
53 array[i--] = serial % 10;
54 serial /= 10;
55 }
56 while (i >= 0)
57 array[i--] = 0;
58 serial = array[0];
59 for (i = 1; i < 8; i++) {
60 serial *= 16;
61 serial += array[i];
62 }
63
64 serialnr->low = serial;
65 }
66}
67#endif /* CONFIG_SERIAL_TAG */
68
69int show_board_info(void)
70{
71 unsigned char ethaddr[6];
72
73 if (read_tdx_cfg_block()) {
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +010074 printf("MISSING TORADEX CONFIG BLOCK\n");
75 tdx_eth_addr.oui = htonl(TORADEX_OUI << 8);
76 tdx_eth_addr.nic = htonl(tdx_serial << 8);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010077 checkboard();
Bhuvanchandra DV43bfc492019-03-25 17:18:28 +010078 } else {
79 sprintf(tdx_serial_str, "%08u", tdx_serial);
80 sprintf(tdx_board_rev_str, "V%1d.%1d%c",
81 tdx_hw_tag.ver_major,
82 tdx_hw_tag.ver_minor,
83 (char)tdx_hw_tag.ver_assembly + 'A');
84
85 env_set("serial#", tdx_serial_str);
86
87 printf("Model: Toradex %s %s, Serial# %s\n",
88 toradex_modules[tdx_hw_tag.prodid],
89 tdx_board_rev_str,
90 tdx_serial_str);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010091 }
92
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010093 /*
94 * Check if environment contains a valid MAC address,
95 * set the one from config block if not
96 */
Simon Glass35affd72017-08-03 12:22:14 -060097 if (!eth_env_get_enetaddr("ethaddr", ethaddr))
Simon Glassfd1e9592017-08-03 12:22:11 -060098 eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +010099
100#ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR
Simon Glass35affd72017-08-03 12:22:14 -0600101 if (!eth_env_get_enetaddr("eth1addr", ethaddr)) {
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100102 /*
103 * Secondary MAC address is allocated from block
104 * 0x100000 higher then the first MAC address
105 */
106 memcpy(ethaddr, &tdx_eth_addr, 6);
107 ethaddr[3] += 0x10;
Simon Glassfd1e9592017-08-03 12:22:11 -0600108 eth_env_set_enetaddr("eth1addr", ethaddr);
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100109 }
110#endif
111
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100112 return 0;
113}
114
Stefan Agnerbeaf4062016-11-30 13:41:52 -0800115#ifdef CONFIG_USB_GADGET_DOWNLOAD
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100116int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
117{
118 unsigned short usb_pid;
119
120 usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid;
121 put_unaligned(usb_pid, &dev->idProduct);
122
123 return 0;
124}
Stefan Agnerbeaf4062016-11-30 13:41:52 -0800125#endif
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100126
Stefan Agner37fa4122016-11-30 13:41:53 -0800127#if defined(CONFIG_OF_LIBFDT)
128int ft_common_board_setup(void *blob, bd_t *bd)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100129{
130 if (tdx_serial) {
131 fdt_setprop(blob, 0, "serial-number", tdx_serial_str,
132 strlen(tdx_serial_str) + 1);
133 }
134
135 if (tdx_hw_tag.ver_major) {
136 char prod_id[5];
137
138 sprintf(prod_id, "%04u", tdx_hw_tag.prodid);
139 fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5);
140
141 fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str,
142 strlen(tdx_board_rev_str) + 1);
143 }
144
145 return 0;
146}
147#endif
148
149#else /* CONFIG_TDX_CFG_BLOCK */
150
151#ifdef CONFIG_REVISION_TAG
152u32 get_board_rev(void)
153{
154 return 0;
155}
156#endif /* CONFIG_REVISION_TAG */
157
158#ifdef CONFIG_SERIAL_TAG
159u32 get_board_serial(void)
160{
161 return 0;
162}
163#endif /* CONFIG_SERIAL_TAG */
164
Stefan Agner37fa4122016-11-30 13:41:53 -0800165int ft_common_board_setup(void *blob, bd_t *bd)
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100166{
167 return 0;
168}
Marcel Ziswilera2777ec2016-11-16 17:49:22 +0100169
170#endif /* CONFIG_TDX_CFG_BLOCK */