Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2016 Toradex, Inc. |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 4 | */ |
| 5 | |
| 6 | #include <common.h> |
Alex Kiernan | 9c21549 | 2018-04-01 09:22:38 +0000 | [diff] [blame] | 7 | #include <environment.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 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 | |
Bhuvanchandra DV | 2190ade | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 15 | #define TORADEX_OUI 0x00142dUL |
| 16 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 17 | #ifdef CONFIG_TDX_CFG_BLOCK |
| 18 | static char tdx_serial_str[9]; |
| 19 | static char tdx_board_rev_str[6]; |
| 20 | |
| 21 | #ifdef CONFIG_REVISION_TAG |
| 22 | u32 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 |
| 35 | void 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 | |
| 68 | int show_board_info(void) |
| 69 | { |
| 70 | unsigned char ethaddr[6]; |
| 71 | |
| 72 | if (read_tdx_cfg_block()) { |
Bhuvanchandra DV | 2190ade | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 73 | 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 Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 76 | checkboard(); |
Bhuvanchandra DV | 2190ade | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 77 | } 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 Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 83 | |
Bhuvanchandra DV | 2190ade | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 84 | env_set("serial#", tdx_serial_str); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 85 | |
Bhuvanchandra DV | 2190ade | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 86 | 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 Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * Check if environment contains a valid MAC address, |
| 94 | * set the one from config block if not |
| 95 | */ |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 96 | if (!eth_env_get_enetaddr("ethaddr", ethaddr)) |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 97 | eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 98 | |
| 99 | #ifdef CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 100 | if (!eth_env_get_enetaddr("eth1addr", ethaddr)) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 101 | /* |
| 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 Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 107 | eth_env_set_enetaddr("eth1addr", ethaddr); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 108 | } |
| 109 | #endif |
| 110 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 111 | return 0; |
| 112 | } |
| 113 | |
Stefan Agner | a188652 | 2016-11-30 13:41:52 -0800 | [diff] [blame] | 114 | #ifdef CONFIG_USB_GADGET_DOWNLOAD |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 115 | int 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 Agner | a188652 | 2016-11-30 13:41:52 -0800 | [diff] [blame] | 124 | #endif |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 125 | |
Stefan Agner | 98ffd0f | 2016-11-30 13:41:53 -0800 | [diff] [blame] | 126 | #if defined(CONFIG_OF_LIBFDT) |
| 127 | int ft_common_board_setup(void *blob, bd_t *bd) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 128 | { |
| 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 |
| 151 | u32 get_board_rev(void) |
| 152 | { |
| 153 | return 0; |
| 154 | } |
| 155 | #endif /* CONFIG_REVISION_TAG */ |
| 156 | |
| 157 | #ifdef CONFIG_SERIAL_TAG |
| 158 | u32 get_board_serial(void) |
| 159 | { |
| 160 | return 0; |
| 161 | } |
| 162 | #endif /* CONFIG_SERIAL_TAG */ |
| 163 | |
Stefan Agner | 98ffd0f | 2016-11-30 13:41:53 -0800 | [diff] [blame] | 164 | int ft_common_board_setup(void *blob, bd_t *bd) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 165 | { |
| 166 | return 0; |
| 167 | } |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 168 | |
| 169 | #endif /* CONFIG_TDX_CFG_BLOCK */ |