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 | |
Emanuele Ghidoli | 26b5cba | 2024-02-23 10:11:41 +0100 | [diff] [blame^] | 6 | #include <dm.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 7 | #include <common.h> |
Simon Glass | 5e6201b | 2019-08-01 09:46:51 -0600 | [diff] [blame] | 8 | #include <env.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 9 | #include <g_dnl.h> |
Simon Glass | 9758973 | 2020-05-10 11:40:02 -0600 | [diff] [blame] | 10 | #include <init.h> |
Masahiro Yamada | 75f82d0 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 11 | #include <linux/libfdt.h> |
Emanuele Ghidoli | 26b5cba | 2024-02-23 10:11:41 +0100 | [diff] [blame^] | 12 | #include <sysinfo.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 13 | |
Simon Glass | 52cb504 | 2022-10-18 07:46:31 -0600 | [diff] [blame] | 14 | #ifdef CONFIG_VIDEO |
Igor Opaniuk | 897822d | 2020-07-15 13:31:01 +0300 | [diff] [blame] | 15 | #include <bmp_logo.h> |
Igor Opaniuk | 897822d | 2020-07-15 13:31:01 +0300 | [diff] [blame] | 16 | #include <splash.h> |
| 17 | #include <video.h> |
| 18 | #endif |
| 19 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 20 | #include "tdx-cfg-block.h" |
Simon Glass | d9a766f | 2017-05-17 08:23:00 -0600 | [diff] [blame] | 21 | #include <asm/setup.h> |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 22 | #include "tdx-common.h" |
| 23 | |
Philippe Schenker | 1832bd3 | 2022-06-13 19:35:22 +0200 | [diff] [blame] | 24 | #define SERIAL_STR_LEN 8 |
| 25 | #define MODULE_VER_STR_LEN 4 // V1.1 |
Philippe Schenker | 498f95a | 2022-06-13 19:35:23 +0200 | [diff] [blame] | 26 | #define MODULE_REV_STR_LEN 3 // [A-Z] or #[26-99] |
Philippe Schenker | 1832bd3 | 2022-06-13 19:35:22 +0200 | [diff] [blame] | 27 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 28 | #ifdef CONFIG_TDX_CFG_BLOCK |
Philippe Schenker | 1832bd3 | 2022-06-13 19:35:22 +0200 | [diff] [blame] | 29 | static char tdx_serial_str[SERIAL_STR_LEN + 1]; |
| 30 | static char tdx_board_rev_str[MODULE_VER_STR_LEN + MODULE_REV_STR_LEN + 1]; |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 31 | |
Igor Opaniuk | ecfe0f5 | 2020-07-15 13:30:57 +0300 | [diff] [blame] | 32 | #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA |
Philippe Schenker | 1832bd3 | 2022-06-13 19:35:22 +0200 | [diff] [blame] | 33 | static char tdx_car_serial_str[SERIAL_STR_LEN + 1]; |
| 34 | static char tdx_car_rev_str[MODULE_VER_STR_LEN + MODULE_REV_STR_LEN + 1]; |
Max Krummenacher | 2ded2d6 | 2023-07-18 11:07:33 +0200 | [diff] [blame] | 35 | static const char *tdx_carrier_board_name; |
Igor Opaniuk | ecfe0f5 | 2020-07-15 13:30:57 +0300 | [diff] [blame] | 36 | #endif |
| 37 | |
Tom Rini | 4cc3885 | 2021-08-30 09:16:30 -0400 | [diff] [blame] | 38 | #if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG) |
| 39 | u32 get_board_revision(void) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 40 | { |
| 41 | /* Check validity */ |
| 42 | if (!tdx_hw_tag.ver_major) |
| 43 | return 0; |
| 44 | |
| 45 | return ((tdx_hw_tag.ver_major & 0xff) << 8) | |
| 46 | ((tdx_hw_tag.ver_minor & 0xf) << 4) | |
| 47 | ((tdx_hw_tag.ver_assembly & 0xf) + 0xa); |
| 48 | } |
| 49 | #endif /* CONFIG_TDX_CFG_BLOCK */ |
| 50 | |
| 51 | #ifdef CONFIG_SERIAL_TAG |
| 52 | void get_board_serial(struct tag_serialnr *serialnr) |
| 53 | { |
| 54 | int array[8]; |
| 55 | unsigned int serial = tdx_serial; |
| 56 | int i; |
| 57 | |
| 58 | serialnr->low = 0; |
| 59 | serialnr->high = 0; |
| 60 | |
| 61 | /* Check validity */ |
| 62 | if (serial) { |
| 63 | /* |
| 64 | * Convert to Linux serial number format (hexadecimal coded |
| 65 | * decimal) |
| 66 | */ |
| 67 | i = 7; |
| 68 | while (serial) { |
| 69 | array[i--] = serial % 10; |
| 70 | serial /= 10; |
| 71 | } |
| 72 | while (i >= 0) |
| 73 | array[i--] = 0; |
| 74 | serial = array[0]; |
| 75 | for (i = 1; i < 8; i++) { |
| 76 | serial *= 16; |
| 77 | serial += array[i]; |
| 78 | } |
| 79 | |
| 80 | serialnr->low = serial; |
| 81 | } |
| 82 | } |
| 83 | #endif /* CONFIG_SERIAL_TAG */ |
| 84 | |
Philippe Schenker | 498f95a | 2022-06-13 19:35:23 +0200 | [diff] [blame] | 85 | static const char *get_board_assembly(u16 ver_assembly) |
| 86 | { |
| 87 | static char ver_name[MODULE_REV_STR_LEN + 1]; |
| 88 | |
| 89 | if (ver_assembly < 26) { |
| 90 | ver_name[0] = (char)ver_assembly + 'A'; |
| 91 | ver_name[1] = '\0'; |
| 92 | } else { |
| 93 | snprintf(ver_name, sizeof(ver_name), |
| 94 | "#%u", ver_assembly); |
| 95 | } |
| 96 | |
| 97 | return ver_name; |
| 98 | } |
| 99 | |
Emanuele Ghidoli | 1e9459e | 2024-02-23 10:11:38 +0100 | [diff] [blame] | 100 | __weak int print_bootinfo(void) |
| 101 | { |
| 102 | return 0; |
| 103 | } |
| 104 | |
| 105 | int checkboard(void) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 106 | { |
Emanuele Ghidoli | 26b5cba | 2024-02-23 10:11:41 +0100 | [diff] [blame^] | 107 | if (valid_cfgblock) |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 108 | printf("Serial#: %s\n", tdx_serial_str); |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 109 | |
| 110 | #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA |
| 111 | if (tdx_carrier_board_name) |
| 112 | printf("Carrier: Toradex %s %s, Serial# %s\n", |
| 113 | tdx_carrier_board_name, |
| 114 | tdx_car_rev_str, |
| 115 | tdx_car_serial_str); |
| 116 | #endif |
| 117 | |
| 118 | print_bootinfo(); |
| 119 | |
| 120 | return 0; |
| 121 | } |
| 122 | |
| 123 | static int settings_r(void) |
| 124 | { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 125 | unsigned char ethaddr[6]; |
| 126 | |
| 127 | if (read_tdx_cfg_block()) { |
Bhuvanchandra DV | 2190ade | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 128 | printf("MISSING TORADEX CONFIG BLOCK\n"); |
Philippe Schenker | d52a257 | 2022-06-20 16:57:45 +0200 | [diff] [blame] | 129 | get_mac_from_serial(tdx_serial, &tdx_eth_addr); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 130 | |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 131 | /* Board can run even if config block is not present */ |
| 132 | return 0; |
| 133 | } |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 134 | |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 135 | snprintf(tdx_serial_str, sizeof(tdx_serial_str), |
| 136 | "%08u", tdx_serial); |
| 137 | snprintf(tdx_board_rev_str, sizeof(tdx_board_rev_str), |
| 138 | "V%1d.%1d%s", |
| 139 | tdx_hw_tag.ver_major, |
| 140 | tdx_hw_tag.ver_minor, |
| 141 | get_board_assembly(tdx_hw_tag.ver_assembly)); |
| 142 | |
| 143 | env_set("serial#", tdx_serial_str); |
| 144 | |
Igor Opaniuk | ecfe0f5 | 2020-07-15 13:30:57 +0300 | [diff] [blame] | 145 | #ifdef CONFIG_TDX_CFG_BLOCK_EXTRA |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 146 | if (read_tdx_cfg_block_carrier()) { |
| 147 | printf("MISSING TORADEX CARRIER CONFIG BLOCKS\n"); |
| 148 | try_migrate_tdx_cfg_block_carrier(); |
| 149 | } else { |
| 150 | tdx_carrier_board_name = |
| 151 | get_toradex_carrier_boards(tdx_car_hw_tag.prodid); |
Igor Opaniuk | ecfe0f5 | 2020-07-15 13:30:57 +0300 | [diff] [blame] | 152 | |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 153 | snprintf(tdx_car_serial_str, sizeof(tdx_car_serial_str), |
| 154 | "%08u", tdx_car_serial); |
| 155 | snprintf(tdx_car_rev_str, sizeof(tdx_car_rev_str), |
| 156 | "V%1d.%1d%s", |
| 157 | tdx_car_hw_tag.ver_major, |
| 158 | tdx_car_hw_tag.ver_minor, |
| 159 | get_board_assembly(tdx_car_hw_tag.ver_assembly)); |
Igor Opaniuk | ecfe0f5 | 2020-07-15 13:30:57 +0300 | [diff] [blame] | 160 | |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 161 | env_set("carrier_serial#", tdx_car_serial_str); |
Bhuvanchandra DV | 2190ade | 2019-03-25 17:18:28 +0100 | [diff] [blame] | 162 | } |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 163 | #endif |
Emanuele Ghidoli | 1e9459e | 2024-02-23 10:11:38 +0100 | [diff] [blame] | 164 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 165 | /* |
| 166 | * Check if environment contains a valid MAC address, |
| 167 | * set the one from config block if not |
| 168 | */ |
Simon Glass | 399a9ce | 2017-08-03 12:22:14 -0600 | [diff] [blame] | 169 | if (!eth_env_get_enetaddr("ethaddr", ethaddr)) |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 170 | eth_env_set_enetaddr("ethaddr", (u8 *)&tdx_eth_addr); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 171 | |
Francesco Dolcini | 14163d6 | 2022-07-21 15:17:36 +0200 | [diff] [blame] | 172 | if (IS_ENABLED(CONFIG_TDX_CFG_BLOCK_2ND_ETHADDR) && |
| 173 | !eth_env_get_enetaddr("eth1addr", ethaddr)) { |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 174 | /* |
| 175 | * Secondary MAC address is allocated from block |
| 176 | * 0x100000 higher then the first MAC address |
| 177 | */ |
| 178 | memcpy(ethaddr, &tdx_eth_addr, 6); |
| 179 | ethaddr[3] += 0x10; |
Simon Glass | 8551d55 | 2017-08-03 12:22:11 -0600 | [diff] [blame] | 180 | eth_env_set_enetaddr("eth1addr", ethaddr); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 181 | } |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 182 | |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 183 | return 0; |
| 184 | } |
Emanuele Ghidoli | 1121f34 | 2024-02-23 10:11:39 +0100 | [diff] [blame] | 185 | EVENT_SPY_SIMPLE(EVT_SETTINGS_R, settings_r); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 186 | |
Emanuele Ghidoli | 26b5cba | 2024-02-23 10:11:41 +0100 | [diff] [blame^] | 187 | static int tdx_detect(struct udevice *dev) |
| 188 | { |
| 189 | return valid_cfgblock ? 0 : -EINVAL; |
| 190 | } |
| 191 | |
| 192 | static int tdx_get_str(struct udevice *dev, int id, size_t size, char *val) |
| 193 | { |
| 194 | int ret = -ENOTSUPP; |
| 195 | |
| 196 | switch (id) { |
| 197 | case SYSINFO_ID_BOARD_MODEL: |
| 198 | snprintf(val, size, |
| 199 | "Toradex %04d %s %s", |
| 200 | tdx_hw_tag.prodid, |
| 201 | toradex_modules[tdx_hw_tag.prodid].name, |
| 202 | tdx_board_rev_str); |
| 203 | |
| 204 | ret = 0; |
| 205 | } |
| 206 | |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | static const struct udevice_id sysinfo_tdx_ids[] = { |
| 211 | { .compatible = "toradex,sysinfo" }, |
| 212 | { /* sentinel */ } |
| 213 | }; |
| 214 | |
| 215 | static const struct sysinfo_ops sysinfo_tdx_ops = { |
| 216 | .detect = tdx_detect, |
| 217 | .get_str = tdx_get_str, |
| 218 | }; |
| 219 | |
| 220 | U_BOOT_DRIVER(sysinfo_toradex) = { |
| 221 | .name = "sysinfo_toradex", |
| 222 | .id = UCLASS_SYSINFO, |
| 223 | .of_match = sysinfo_tdx_ids, |
| 224 | .ops = &sysinfo_tdx_ops, |
| 225 | }; |
| 226 | |
Stefan Agner | 57985bd | 2021-07-23 09:39:46 +0300 | [diff] [blame] | 227 | #ifdef CONFIG_TDX_CFG_BLOCK_USB_GADGET_PID |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 228 | int g_dnl_bind_fixup(struct usb_device_descriptor *dev, const char *name) |
| 229 | { |
| 230 | unsigned short usb_pid; |
| 231 | |
| 232 | usb_pid = TORADEX_USB_PRODUCT_NUM_OFFSET + tdx_hw_tag.prodid; |
| 233 | put_unaligned(usb_pid, &dev->idProduct); |
| 234 | |
| 235 | return 0; |
| 236 | } |
Stefan Agner | a188652 | 2016-11-30 13:41:52 -0800 | [diff] [blame] | 237 | #endif |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 238 | |
Stefan Agner | 98ffd0f | 2016-11-30 13:41:53 -0800 | [diff] [blame] | 239 | #if defined(CONFIG_OF_LIBFDT) |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 240 | int ft_common_board_setup(void *blob, struct bd_info *bd) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 241 | { |
| 242 | if (tdx_serial) { |
| 243 | fdt_setprop(blob, 0, "serial-number", tdx_serial_str, |
| 244 | strlen(tdx_serial_str) + 1); |
| 245 | } |
| 246 | |
| 247 | if (tdx_hw_tag.ver_major) { |
| 248 | char prod_id[5]; |
| 249 | |
Philippe Schenker | 38cf79e | 2022-06-13 19:35:21 +0200 | [diff] [blame] | 250 | snprintf(prod_id, sizeof(prod_id), "%04u", tdx_hw_tag.prodid); |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 251 | fdt_setprop(blob, 0, "toradex,product-id", prod_id, 5); |
| 252 | |
| 253 | fdt_setprop(blob, 0, "toradex,board-rev", tdx_board_rev_str, |
| 254 | strlen(tdx_board_rev_str) + 1); |
| 255 | } |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | #endif |
| 260 | |
| 261 | #else /* CONFIG_TDX_CFG_BLOCK */ |
| 262 | |
Tom Rini | 4cc3885 | 2021-08-30 09:16:30 -0400 | [diff] [blame] | 263 | #if defined(CONFIG_ENV_VARS_UBOOT_RUNTIME_CONFIG) |
| 264 | u32 get_board_revision(void) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 265 | { |
| 266 | return 0; |
| 267 | } |
| 268 | #endif /* CONFIG_REVISION_TAG */ |
| 269 | |
| 270 | #ifdef CONFIG_SERIAL_TAG |
| 271 | u32 get_board_serial(void) |
| 272 | { |
| 273 | return 0; |
| 274 | } |
| 275 | #endif /* CONFIG_SERIAL_TAG */ |
| 276 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 277 | int ft_common_board_setup(void *blob, struct bd_info *bd) |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 278 | { |
| 279 | return 0; |
| 280 | } |
Marcel Ziswiler | 7a28dfc | 2016-11-16 17:49:22 +0100 | [diff] [blame] | 281 | |
| 282 | #endif /* CONFIG_TDX_CFG_BLOCK */ |