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