1 /* 2 * Copyright (c) 2016 Toradex, Inc. 3 * 4 * SPDX-License-Identifier: GPL-2.0+ 5 */ 6 7 #include <common.h> 8 #include <environment.h> 9 #include <g_dnl.h> 10 #include <linux/libfdt.h> 11 12 #include "tdx-cfg-block.h" 13 #include <asm/setup.h> 14 #include "tdx-common.h" 15 16 #ifdef CONFIG_TDX_CFG_BLOCK 17 static char tdx_serial_str[9]; 18 static char tdx_board_rev_str[6]; 19 20 #ifdef CONFIG_REVISION_TAG 21 u32 get_board_rev(void) 22 { 23 /* Check validity */ 24 if (!tdx_hw_tag.ver_major) 25 return 0; 26 27 return ((tdx_hw_tag.ver_major & 0xff) << 8) | 28 ((tdx_hw_tag.ver_minor & 0xf) << 4) | 29 ((tdx_hw_tag.ver_assembly & 0xf) + 0xa); 30 } 31 #endif /* CONFIG_TDX_CFG_BLOCK */ 32 33 #ifdef CONFIG_SERIAL_TAG 34 void get_board_serial(struct tag_serialnr *serialnr) 35 { 36 int array[8]; 37 unsigned int serial = tdx_serial; 38 int i; 39 40 serialnr->low = 0; 41 serialnr->high = 0; 42 43 /* Check validity */ 44 if (serial) { 45 /* 46 * Convert to Linux serial number format (hexadecimal coded 47 * decimal) 48 */ 49 i = 7; 50 while (serial) { 51 array[i--] = serial % 10; 52 serial /= 10; 53 } 54 while (i >= 0) 55 array[i--] = 0; 56 serial = array[0]; 57 for (i = 1; i < 8; i++) { 58 serial *= 16; 59 serial += array[i]; 60 } 61 62 serialnr->low = serial; 63 } 64 } 65 #endif /* CONFIG_SERIAL_TAG */ 66 67 int show_board_info(void) 68 { 69 unsigned char ethaddr[6]; 70 71 if (read_tdx_cfg_block()) { 72 printf("Missing Toradex config block\n"); 73 checkboard(); 74 return 0; 75 } 76 77 /* board serial-number */ 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'); 83 84 env_set("serial#", tdx_serial_str); 85 86 /* 87 * Check if environment contains a valid MAC address, 88 * set the one from config block if not 89 */ 90 if (!eth_env_get_enetaddr("ethaddr", ethaddr)) 91 eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr); 92 93 #ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR 94 if (!eth_env_get_enetaddr("eth1addr", ethaddr)) { 95 /* 96 * Secondary MAC address is allocated from block 97 * 0x100000 higher then the first MAC address 98 */ 99 memcpy(ethaddr, &tdx_eth_addr, 6); 100 ethaddr[3] += 0x10; 101 eth_env_set_enetaddr("eth1addr", ethaddr); 102 } 103 #endif 104 105 printf("Model: Toradex %s %s, Serial# %s\n", 106 toradex_modules[tdx_hw_tag.prodid], 107 tdx_board_rev_str, 108 tdx_serial_str); 109 110 return 0; 111 } 112 113 #ifdef CONFIG_USB_GADGET_DOWNLOAD 114 int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) 115 { 116 unsigned short usb_pid; 117 118 usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid; 119 put_unaligned(usb_pid, &dev->idProduct); 120 121 return 0; 122 } 123 #endif 124 125 #if defined(CONFIG_OF_LIBFDT) 126 int ft_common_board_setup(void *blob, bd_t *bd) 127 { 128 if (tdx_serial) { 129 fdt_setprop(blob, 0, "serial-number", tdx_serial_str, 130 strlen(tdx_serial_str) + 1); 131 } 132 133 if (tdx_hw_tag.ver_major) { 134 char prod_id[5]; 135 136 sprintf(prod_id, "%04u", tdx_hw_tag.prodid); 137 fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5); 138 139 fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str, 140 strlen(tdx_board_rev_str) + 1); 141 } 142 143 return 0; 144 } 145 #endif 146 147 #else /* CONFIG_TDX_CFG_BLOCK */ 148 149 #ifdef CONFIG_REVISION_TAG 150 u32 get_board_rev(void) 151 { 152 return 0; 153 } 154 #endif /* CONFIG_REVISION_TAG */ 155 156 #ifdef CONFIG_SERIAL_TAG 157 u32 get_board_serial(void) 158 { 159 return 0; 160 } 161 #endif /* CONFIG_SERIAL_TAG */ 162 163 int ft_common_board_setup(void *blob, bd_t *bd) 164 { 165 return 0; 166 } 167 168 #endif /* CONFIG_TDX_CFG_BLOCK */ 169