Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Toradex, Inc. |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Simon Glass | 9fb625c | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 7 | #include <env.h> |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 8 | #include <g_dnl.h> |
Simon Glass | 691d719 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 9 | #include <init.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 10 | #include <linux/libfdt.h> |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 11 | |
| 12 | #include "tdx-cfg-block.h" |
Simon Glass | 5d98285 | 2017-05-17 08:23:00 -0600 | [diff] [blame] | 13 | #include <asm/setup.h> |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 14 | #include "tdx-common.h" |
| 15 | |
Bhuvanchandra DV | 43bfc49 | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 16 | #define TORADEX_OUI 0x00142dUL |
| 17 | |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 18 | #ifdef CONFIG_TDX_CFG_BLOCK |
| 19 | static char tdx_serial_str[9]; |
| 20 | static char tdx_board_rev_str[6]; |
| 21 | |
| 22 | #ifdef CONFIG_REVISION_TAG |
| 23 | u32 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 |
| 36 | void 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 | |
| 69 | int show_board_info(void) |
| 70 | { |
| 71 | unsigned char ethaddr[6]; |
| 72 | |
| 73 | if (read_tdx_cfg_block()) { |
Bhuvanchandra DV | 43bfc49 | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 74 | 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 Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 77 | checkboard(); |
Bhuvanchandra DV | 43bfc49 | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 78 | } 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 Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 91 | } |
| 92 | |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 93 | /* |
| 94 | * Check if environment contains a valid MAC address, |
| 95 | * set the one from config block if not |
| 96 | */ |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 97 | if (!eth_env_get_enetaddr("ethaddr", ethaddr)) |
Simon Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 98 | eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr); |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 99 | |
| 100 | #ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR |
Simon Glass | 35affd7 | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 101 | if (!eth_env_get_enetaddr("eth1addr", ethaddr)) { |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 102 | /* |
| 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 Glass | fd1e959 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 108 | eth_env_set_enetaddr("eth1addr", ethaddr); |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 109 | } |
| 110 | #endif |
| 111 | |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 112 | return 0; |
| 113 | } |
| 114 | |
Stefan Agner | beaf406 | 2016-11-30 13:41:52 -0800 | [diff] [blame] | 115 | #ifdef CONFIG_USB_GADGET_DOWNLOAD |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 116 | int 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 Agner | beaf406 | 2016-11-30 13:41:52 -0800 | [diff] [blame] | 125 | #endif |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 126 | |
Stefan Agner | 37fa412 | 2016-11-30 13:41:53 -0800 | [diff] [blame] | 127 | #if defined(CONFIG_OF_LIBFDT) |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame^] | 128 | int ft_common_board_setup(void *blob, struct bd_info *bd) |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 129 | { |
| 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 |
| 152 | u32 get_board_rev(void) |
| 153 | { |
| 154 | return 0; |
| 155 | } |
| 156 | #endif /* CONFIG_REVISION_TAG */ |
| 157 | |
| 158 | #ifdef CONFIG_SERIAL_TAG |
| 159 | u32 get_board_serial(void) |
| 160 | { |
| 161 | return 0; |
| 162 | } |
| 163 | #endif /* CONFIG_SERIAL_TAG */ |
| 164 | |
Masahiro Yamada | b75d8dc | 2020-06-26 15:13:33 +0900 | [diff] [blame^] | 165 | int ft_common_board_setup(void *blob, struct bd_info *bd) |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 166 | { |
| 167 | return 0; |
| 168 | } |
Marcel Ziswiler | a2777ec | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 169 | |
| 170 | #endif /* CONFIG_TDX_CFG_BLOCK */ |