blob: 2d560cceaf53fccb6067bec719b50288853bc020 [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>
Alex Kiernan9c215492018-04-01 09:22:38 +00007#include <environment.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +01008#include <g_dnl.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +09009#include <linux/libfdt.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010010
11#include "tdx-cfg-block.h"
Simon Glassd9a766f2017-05-17 08:23:00 -060012#include <asm/setup.h>
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010013#include "tdx-common.h"
14
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +010015#define TORADEX_OUI 0x00142dUL
16
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010017#ifdef CONFIG_TDX_CFG_BLOCK
18static char tdx_serial_str[9];
19static char tdx_board_rev_str[6];
20
21#ifdef CONFIG_REVISION_TAG
22u32 get_board_rev(void)
23{
24 /* Check validity */
25 if (!tdx_hw_tag.ver_major)
26 return 0;
27
28 return ((tdx_hw_tag.ver_major & 0xff) << 8) |
29 ((tdx_hw_tag.ver_minor & 0xf) << 4) |
30 ((tdx_hw_tag.ver_assembly & 0xf) + 0xa);
31}
32#endif /* CONFIG_TDX_CFG_BLOCK */
33
34#ifdef CONFIG_SERIAL_TAG
35void get_board_serial(struct tag_serialnr *serialnr)
36{
37 int array[8];
38 unsigned int serial = tdx_serial;
39 int i;
40
41 serialnr->low = 0;
42 serialnr->high = 0;
43
44 /* Check validity */
45 if (serial) {
46 /*
47 * Convert to Linux serial number format (hexadecimal coded
48 * decimal)
49 */
50 i = 7;
51 while (serial) {
52 array[i--] = serial % 10;
53 serial /= 10;
54 }
55 while (i >= 0)
56 array[i--] = 0;
57 serial = array[0];
58 for (i = 1; i < 8; i++) {
59 serial *= 16;
60 serial += array[i];
61 }
62
63 serialnr->low = serial;
64 }
65}
66#endif /* CONFIG_SERIAL_TAG */
67
68int show_board_info(void)
69{
70 unsigned char ethaddr[6];
71
72 if (read_tdx_cfg_block()) {
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +010073 printf("MISSING TORADEX CONFIG BLOCK\n");
74 tdx_eth_addr.oui = htonl(TORADEX_OUI << 8);
75 tdx_eth_addr.nic = htonl(tdx_serial << 8);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010076 checkboard();
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +010077 } else {
78 sprintf(tdx_serial_str, "%08u", tdx_serial);
79 sprintf(tdx_board_rev_str, "V%1d.%1d%c",
80 tdx_hw_tag.ver_major,
81 tdx_hw_tag.ver_minor,
82 (char)tdx_hw_tag.ver_assembly + 'A');
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010083
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +010084 env_set("serial#", tdx_serial_str);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010085
Bhuvanchandra DV2190ade2019-03-25 17:18:28 +010086 printf("Model: Toradex %s %s, Serial# %s\n",
87 toradex_modules[tdx_hw_tag.prodid],
88 tdx_board_rev_str,
89 tdx_serial_str);
90 }
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010091
92 /*
93 * Check if environment contains a valid MAC address,
94 * set the one from config block if not
95 */
Simon Glass399a9ce2017-08-03 12:22:14 -060096 if (!eth_env_get_enetaddr("ethaddr", ethaddr))
Simon Glass8551d552017-08-03 12:22:11 -060097 eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +010098
99#ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR
Simon Glass399a9ce2017-08-03 12:22:14 -0600100 if (!eth_env_get_enetaddr("eth1addr", ethaddr)) {
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100101 /*
102 * Secondary MAC address is allocated from block
103 * 0x100000 higher then the first MAC address
104 */
105 memcpy(ethaddr, &tdx_eth_addr, 6);
106 ethaddr[3] += 0x10;
Simon Glass8551d552017-08-03 12:22:11 -0600107 eth_env_set_enetaddr("eth1addr", ethaddr);
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100108 }
109#endif
110
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100111 return 0;
112}
113
Stefan Agnera1886522016-11-30 13:41:52 -0800114#ifdef CONFIG_USB_GADGET_DOWNLOAD
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100115int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name)
116{
117 unsigned short usb_pid;
118
119 usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid;
120 put_unaligned(usb_pid, &dev->idProduct);
121
122 return 0;
123}
Stefan Agnera1886522016-11-30 13:41:52 -0800124#endif
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100125
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800126#if defined(CONFIG_OF_LIBFDT)
127int ft_common_board_setup(void *blob, bd_t *bd)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100128{
129 if (tdx_serial) {
130 fdt_setprop(blob, 0, "serial-number", tdx_serial_str,
131 strlen(tdx_serial_str) + 1);
132 }
133
134 if (tdx_hw_tag.ver_major) {
135 char prod_id[5];
136
137 sprintf(prod_id, "%04u", tdx_hw_tag.prodid);
138 fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5);
139
140 fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str,
141 strlen(tdx_board_rev_str) + 1);
142 }
143
144 return 0;
145}
146#endif
147
148#else /* CONFIG_TDX_CFG_BLOCK */
149
150#ifdef CONFIG_REVISION_TAG
151u32 get_board_rev(void)
152{
153 return 0;
154}
155#endif /* CONFIG_REVISION_TAG */
156
157#ifdef CONFIG_SERIAL_TAG
158u32 get_board_serial(void)
159{
160 return 0;
161}
162#endif /* CONFIG_SERIAL_TAG */
163
Stefan Agner98ffd0f2016-11-30 13:41:53 -0800164int ft_common_board_setup(void *blob, bd_t *bd)
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100165{
166 return 0;
167}
Marcel Ziswiler7a28dfc2016-11-16 17:49:22 +0100168
169#endif /* CONFIG_TDX_CFG_BLOCK */