Patrick Delaunay | f12b745 | 2021-06-30 17:06:19 +0200 | [diff] [blame] | 1 | /* |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 2 | * Copyright (c) 2021-2022, STMicroelectronics - All Rights Reserved |
Patrick Delaunay | f12b745 | 2021-06-30 17:06:19 +0200 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
| 7 | #include <limits.h> |
| 8 | #include <string.h> |
| 9 | |
| 10 | #include <common/debug.h> |
| 11 | #include <drivers/st/bsec.h> |
| 12 | #include <drivers/st/stm32mp1_usb.h> |
| 13 | #include <drivers/usb_device.h> |
| 14 | |
| 15 | #include <platform_def.h> |
| 16 | #include <stm32cubeprogrammer.h> |
| 17 | #include <stm32mp_common.h> |
| 18 | #include <usb_dfu.h> |
| 19 | |
| 20 | /* String size (1 byte) + type (1 byte) + 24 UTF16 characters: 2 bytes each */ |
| 21 | #define SIZ_STRING_SERIAL U(24) |
| 22 | #define USB_SIZ_STRING_SERIAL (1U + 1U + (SIZ_STRING_SERIAL * 2U)) |
| 23 | #define USBD_MAX_STR_DESC_SIZ 0x100 |
| 24 | #define USBD_VID 0x0483 |
| 25 | #define USBD_PID 0xDF11 |
| 26 | #define USBD_LANGID_STRING 0x409 |
| 27 | #define USBD_MANUFACTURER_STRING "STMicroelectronics" |
| 28 | #define USBD_CONFIGURATION_STRING "DFU Config" |
| 29 | #define USBD_INTERFACE_STRING "DFU Interface" |
| 30 | |
| 31 | #define USB_DFU_ITF_NUM 6 |
| 32 | |
| 33 | #define USB_DFU_CONFIG_DESC_SIZ USB_DFU_DESC_SIZ(USB_DFU_ITF_NUM) |
| 34 | |
| 35 | /* DFU devices */ |
| 36 | static struct usb_dfu_handle usb_dfu_handle; |
| 37 | |
| 38 | /* USB Standard Device Descriptor */ |
| 39 | static const uint8_t usb_stm32mp1_desc[USB_LEN_DEV_DESC] = { |
| 40 | USB_LEN_DEV_DESC, /* bLength */ |
| 41 | USB_DESC_TYPE_DEVICE, /* bDescriptorType */ |
| 42 | 0x00, /* bcdUSB */ |
| 43 | 0x02, /* version */ |
| 44 | 0x00, /* bDeviceClass */ |
| 45 | 0x00, /* bDeviceSubClass */ |
| 46 | 0x00, /* bDeviceProtocol */ |
| 47 | USB_MAX_EP0_SIZE, /* bMaxPacketSize */ |
| 48 | LOBYTE(USBD_VID), /* idVendor */ |
| 49 | HIBYTE(USBD_VID), /* idVendor */ |
| 50 | LOBYTE(USBD_PID), /* idVendor */ |
| 51 | HIBYTE(USBD_PID), /* idVendor */ |
| 52 | 0x00, /* bcdDevice rel. 2.00 */ |
| 53 | 0x02, |
| 54 | USBD_IDX_MFC_STR, /* Index of manufacturer string */ |
| 55 | USBD_IDX_PRODUCT_STR, /* Index of product string */ |
| 56 | USBD_IDX_SERIAL_STR, /* Index of serial number string */ |
| 57 | USBD_MAX_NUM_CONFIGURATION /* bNumConfigurations */ |
| 58 | }; /* USB_DeviceDescriptor */ |
| 59 | |
| 60 | /* USB Standard String Descriptor */ |
| 61 | static const uint8_t usb_stm32mp1_lang_id_desc[USB_LEN_LANGID_STR_DESC] = { |
| 62 | USB_LEN_LANGID_STR_DESC, |
| 63 | USB_DESC_TYPE_STRING, |
| 64 | LOBYTE(USBD_LANGID_STRING), |
| 65 | HIBYTE(USBD_LANGID_STRING), |
| 66 | }; |
| 67 | |
| 68 | /* USB Standard Device Descriptor */ |
| 69 | static const uint8_t |
| 70 | usbd_stm32mp1_qualifier_desc[USB_LEN_DEV_QUALIFIER_DESC] = { |
| 71 | USB_LEN_DEV_QUALIFIER_DESC, |
| 72 | USB_DESC_TYPE_DEVICE_QUALIFIER, |
| 73 | 0x00, |
| 74 | 0x02, |
| 75 | 0x00, |
| 76 | 0x00, |
| 77 | 0x00, |
| 78 | 0x40, |
| 79 | 0x01, |
| 80 | 0x00, |
| 81 | }; |
| 82 | |
| 83 | /* USB serial number: build dynamically */ |
| 84 | static uint8_t usb_stm32mp1_serial[USB_SIZ_STRING_SERIAL + 1]; |
| 85 | |
| 86 | /* USB DFU device Configuration Descriptor */ |
| 87 | static const uint8_t usb_stm32mp1_config_desc[USB_DFU_CONFIG_DESC_SIZ] = { |
| 88 | 0x09, /* bLength: Configuration Descriptor size */ |
| 89 | USB_DESC_TYPE_CONFIGURATION, /* bDescriptorType: Configuration */ |
| 90 | USB_DFU_CONFIG_DESC_SIZ, /* wTotalLength: Bytes returned */ |
| 91 | 0x00, |
| 92 | 0x01, /* bNumInterfaces: 1 interface */ |
| 93 | 0x01, /* bConfigurationValue: Configuration value */ |
| 94 | 0x02, /* iConfiguration: Index of string descriptor for configuration */ |
| 95 | 0xC0, /* bmAttributes: bus powered and Supprts Remote Wakeup */ |
| 96 | 0x32, /* MaxPower 100 mA: this current is used for detecting Vbus */ |
| 97 | |
| 98 | /* Descriptor of DFU interface 0 Alternate setting 0..N */ |
| 99 | USBD_DFU_IF_DESC(0), |
| 100 | USBD_DFU_IF_DESC(1), |
| 101 | USBD_DFU_IF_DESC(2), |
| 102 | USBD_DFU_IF_DESC(3), |
| 103 | USBD_DFU_IF_DESC(4), |
| 104 | USBD_DFU_IF_DESC(5), |
| 105 | |
| 106 | /* DFU Functional Descriptor */ |
| 107 | 0x09, /* blength = 9 Bytes */ |
| 108 | DFU_DESCRIPTOR_TYPE, /* DFU Functional Descriptor */ |
| 109 | DFU_BM_ATTRIBUTE, /* bmAttribute for DFU */ |
| 110 | 0xFF, /* DetachTimeOut = 255 ms */ |
| 111 | 0x00, |
| 112 | TRANSFER_SIZE_BYTES(USBD_DFU_XFER_SIZE), /* TransferSize = 1024 Byte */ |
| 113 | ((USB_DFU_VERSION >> 0) & 0xFF), /* bcdDFUVersion */ |
| 114 | ((USB_DFU_VERSION >> 8) & 0xFF) |
| 115 | }; |
| 116 | |
| 117 | /* The user strings: one by alternate as defined in USBD_DFU_IF_DESC */ |
| 118 | const char *const if_desc_string[USB_DFU_ITF_NUM] = { |
| 119 | "@Partition0 /0x00/1*256Ke", |
| 120 | "@FSBL /0x01/1*1Me", |
| 121 | "@Partition2 /0x02/1*1Me", |
| 122 | "@Partition3 /0x03/1*16Me", |
| 123 | "@Partition4 /0x04/1*16Me", |
| 124 | "@virtual /0xF1/1*512Ba" |
| 125 | }; |
| 126 | |
| 127 | /* Buffer to build the unicode string provided to USB device stack */ |
| 128 | static uint8_t usb_str_dec[USBD_MAX_STR_DESC_SIZ]; |
| 129 | |
| 130 | /* |
| 131 | * Convert Ascii string into unicode one |
| 132 | * desc : descriptor buffer |
| 133 | * unicode : Formatted string buffer (unicode) |
| 134 | * len : descriptor length |
| 135 | */ |
| 136 | static void stm32mp1_get_string(const char *desc, uint8_t *unicode, uint16_t *len) |
| 137 | { |
| 138 | uint8_t idx = 0U; |
| 139 | |
| 140 | if (desc == NULL) { |
| 141 | return; |
| 142 | } |
| 143 | |
| 144 | *len = strlen(desc) * 2U + 2U; |
| 145 | unicode[idx++] = *len; |
| 146 | unicode[idx++] = USB_DESC_TYPE_STRING; |
| 147 | |
| 148 | while (*desc != '\0') { |
| 149 | unicode[idx++] = *desc++; |
| 150 | unicode[idx++] = 0x00U; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /* |
| 155 | * Create the serial number string descriptor |
| 156 | */ |
| 157 | static void update_serial_num_string(void) |
| 158 | { |
| 159 | uint8_t i; |
Patrick Delaunay | f12b745 | 2021-06-30 17:06:19 +0200 | [diff] [blame] | 160 | char serial_string[SIZ_STRING_SERIAL + 2U]; |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 161 | /* serial number is set to 0 */ |
| 162 | uint32_t deviceserial[UID_WORD_NB] = {0U, 0U, 0U}; |
| 163 | uint32_t otp; |
| 164 | uint32_t len; |
Patrick Delaunay | f12b745 | 2021-06-30 17:06:19 +0200 | [diff] [blame] | 165 | uint16_t length; |
| 166 | |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 167 | if (stm32_get_otp_index(UID_OTP, &otp, &len) != 0) { |
| 168 | ERROR("BSEC: Get UID_OTP number Error\n"); |
| 169 | return; |
Patrick Delaunay | f12b745 | 2021-06-30 17:06:19 +0200 | [diff] [blame] | 170 | } |
Lionel Debieve | bc2d88d | 2019-11-04 14:31:38 +0100 | [diff] [blame] | 171 | |
| 172 | if ((len / __WORD_BIT) != UID_WORD_NB) { |
| 173 | ERROR("BSEC: Get UID_OTP length Error\n"); |
| 174 | return; |
| 175 | } |
| 176 | |
| 177 | for (i = 0; i < UID_WORD_NB; i++) { |
| 178 | if (bsec_shadow_read_otp(&deviceserial[i], i + otp) != |
| 179 | BSEC_OK) { |
| 180 | ERROR("BSEC: UID%d Error\n", i); |
| 181 | return; |
Patrick Delaunay | f12b745 | 2021-06-30 17:06:19 +0200 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | /* build serial number with OTP value as in ROM code */ |
| 185 | snprintf(serial_string, sizeof(serial_string), "%08X%08X%08X", |
| 186 | deviceserial[0], deviceserial[1], deviceserial[2]); |
| 187 | |
| 188 | length = USB_SIZ_STRING_SERIAL; |
| 189 | stm32mp1_get_string(serial_string, usb_stm32mp1_serial, &length); |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Return Device Qualifier descriptor |
| 194 | * length : pointer data length |
| 195 | * return : pointer to descriptor buffer |
| 196 | */ |
| 197 | static uint8_t *stm32mp1_get_qualifier_desc(uint16_t *length) |
| 198 | { |
| 199 | *length = sizeof(usbd_stm32mp1_qualifier_desc); |
| 200 | |
| 201 | return (uint8_t *)usbd_stm32mp1_qualifier_desc; |
| 202 | } |
| 203 | |
| 204 | /* |
| 205 | * Return configuration descriptor |
| 206 | * length : pointer data length |
| 207 | * return : pointer to descriptor buffer |
| 208 | */ |
| 209 | static uint8_t *stm32mp1_get_config_desc(uint16_t *length) |
| 210 | { |
| 211 | *length = sizeof(usb_stm32mp1_config_desc); |
| 212 | |
| 213 | return (uint8_t *)usb_stm32mp1_config_desc; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Returns the device descriptor. |
| 218 | * length: Pointer to data length variable |
| 219 | * return : Pointer to descriptor buffer |
| 220 | */ |
| 221 | static uint8_t *stm32mp1_device_desc(uint16_t *length) |
| 222 | { |
| 223 | *length = sizeof(usb_stm32mp1_desc); |
| 224 | |
| 225 | return (uint8_t *)usb_stm32mp1_desc; |
| 226 | } |
| 227 | |
| 228 | /* |
| 229 | * Returns the LangID string descriptor. |
| 230 | * length: Pointer to data length variable |
| 231 | * return : Pointer to descriptor buffer |
| 232 | */ |
| 233 | static uint8_t *stm32mp1_lang_id_desc(uint16_t *length) |
| 234 | { |
| 235 | *length = sizeof(usb_stm32mp1_lang_id_desc); |
| 236 | |
| 237 | return (uint8_t *)usb_stm32mp1_lang_id_desc; |
| 238 | } |
| 239 | |
| 240 | /* |
| 241 | * Returns the product string descriptor. |
| 242 | * length: Pointer to data length variable |
| 243 | * return : Pointer to descriptor buffer |
| 244 | */ |
| 245 | static uint8_t *stm32mp1_product_desc(uint16_t *length) |
| 246 | { |
| 247 | char name[STM32_SOC_NAME_SIZE]; |
| 248 | char product[128]; |
| 249 | uint32_t chip_id; |
| 250 | uint32_t chip_version; |
| 251 | |
| 252 | stm32mp_get_soc_name(name); |
| 253 | chip_id = stm32mp_get_chip_dev_id(); |
| 254 | chip_version = stm32mp_get_chip_version(); |
| 255 | |
| 256 | snprintf(product, sizeof(product), |
| 257 | "DFU @Device ID /0x%03X, @Revision ID /0x%04X, @Name /%s,", |
| 258 | chip_id, chip_version, name); |
| 259 | |
| 260 | stm32mp1_get_string(product, usb_str_dec, length); |
| 261 | |
| 262 | return usb_str_dec; |
| 263 | } |
| 264 | |
| 265 | /* |
| 266 | * Returns the manufacturer string descriptor. |
| 267 | * length: Pointer to data length variable |
| 268 | * return : Pointer to descriptor buffer |
| 269 | */ |
| 270 | static uint8_t *stm32mp1_manufacturer_desc(uint16_t *length) |
| 271 | { |
| 272 | stm32mp1_get_string(USBD_MANUFACTURER_STRING, usb_str_dec, length); |
| 273 | |
| 274 | return usb_str_dec; |
| 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Returns the serial number string descriptor. |
| 279 | * length: Pointer to data length variable |
| 280 | * return : Pointer to descriptor buffer |
| 281 | */ |
| 282 | static uint8_t *stm32mp1_serial_desc(uint16_t *length) |
| 283 | { |
| 284 | *length = USB_SIZ_STRING_SERIAL; |
| 285 | |
| 286 | return (uint8_t *)usb_stm32mp1_serial; |
| 287 | } |
| 288 | |
| 289 | /* |
| 290 | * Returns the configuration string descriptor. |
| 291 | * length: Pointer to data length variable |
| 292 | * return : Pointer to descriptor buffer |
| 293 | */ |
| 294 | static uint8_t *stm32mp1_config_desc(uint16_t *length) |
| 295 | { |
| 296 | stm32mp1_get_string(USBD_CONFIGURATION_STRING, usb_str_dec, length); |
| 297 | |
| 298 | return usb_str_dec; |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * Returns the interface string descriptor. |
| 303 | * length : Pointer to data length variable |
| 304 | * return : Pointer to descriptor buffer |
| 305 | */ |
| 306 | static uint8_t *stm32mp1_interface_desc(uint16_t *length) |
| 307 | { |
| 308 | stm32mp1_get_string(USBD_INTERFACE_STRING, usb_str_dec, length); |
| 309 | |
| 310 | return usb_str_dec; |
| 311 | } |
| 312 | |
| 313 | /* |
| 314 | * Manages the transfer of memory interfaces string descriptors. |
| 315 | * index: descriptor index |
| 316 | * length : pointer data length |
| 317 | * return : pointer to the descriptor table or NULL if the descriptor |
| 318 | * is not supported. |
| 319 | */ |
| 320 | static uint8_t *stm32mp1_get_usr_desc(uint8_t index, uint16_t *length) |
| 321 | { |
| 322 | if (index >= ARRAY_SIZE(if_desc_string)) { |
| 323 | return NULL; |
| 324 | } |
| 325 | |
| 326 | stm32mp1_get_string(if_desc_string[index], usb_str_dec, length); |
| 327 | |
| 328 | return usb_str_dec; |
| 329 | } |
| 330 | |
| 331 | static const struct usb_desc dfu_desc = { |
| 332 | .get_device_desc = stm32mp1_device_desc, |
| 333 | .get_lang_id_desc = stm32mp1_lang_id_desc, |
| 334 | .get_manufacturer_desc = stm32mp1_manufacturer_desc, |
| 335 | .get_product_desc = stm32mp1_product_desc, |
| 336 | .get_configuration_desc = stm32mp1_config_desc, |
| 337 | .get_serial_desc = stm32mp1_serial_desc, |
| 338 | .get_interface_desc = stm32mp1_interface_desc, |
| 339 | .get_usr_desc = stm32mp1_get_usr_desc, |
| 340 | .get_config_desc = stm32mp1_get_config_desc, |
| 341 | .get_device_qualifier_desc = stm32mp1_get_qualifier_desc, |
Patrick Delaunay | b43d85a | 2021-11-04 15:13:33 +0100 | [diff] [blame] | 342 | /* only HS is supported, as ROM code */ |
| 343 | .get_other_speed_config_desc = NULL, |
Patrick Delaunay | f12b745 | 2021-06-30 17:06:19 +0200 | [diff] [blame] | 344 | }; |
| 345 | |
| 346 | static struct usb_handle usb_core_handle; |
| 347 | static struct pcd_handle pcd_handle; |
| 348 | |
| 349 | struct usb_handle *usb_dfu_plat_init(void) |
| 350 | { |
| 351 | /* Prepare USB Driver */ |
| 352 | pcd_handle.in_ep[0].maxpacket = USB_MAX_EP0_SIZE; |
| 353 | pcd_handle.out_ep[0].maxpacket = USB_MAX_EP0_SIZE; |
| 354 | stm32mp1_usb_init_driver(&usb_core_handle, &pcd_handle, |
| 355 | (uint32_t *)USB_OTG_BASE); |
| 356 | |
| 357 | /* STM32MP15 = keep the configuration from ROM code */ |
| 358 | usb_core_handle.ep0_state = USBD_EP0_DATA_IN; |
| 359 | usb_core_handle.dev_state = USBD_STATE_CONFIGURED; |
| 360 | |
| 361 | /* Update the serial number string descriptor from the unique ID */ |
| 362 | update_serial_num_string(); |
| 363 | |
| 364 | /* Prepare USB DFU stack */ |
| 365 | usb_dfu_register(&usb_core_handle, &usb_dfu_handle); |
| 366 | |
| 367 | /* Register DFU descriptor in USB stack */ |
| 368 | register_platform(&usb_core_handle, &dfu_desc); |
| 369 | |
| 370 | return &usb_core_handle; |
| 371 | } |
| 372 | |
| 373 | /* Link between USB alternate and STM32CubeProgramer phase */ |
| 374 | uint8_t usb_dfu_get_phase(uint8_t alt) |
| 375 | { |
| 376 | uint8_t ret; |
| 377 | |
| 378 | switch (alt) { |
| 379 | case 3: |
| 380 | ret = PHASE_SSBL; |
| 381 | break; |
| 382 | case 5: |
| 383 | ret = PHASE_CMD; |
| 384 | break; |
| 385 | default: |
| 386 | ret = PHASE_RESET; |
| 387 | break; |
| 388 | } |
| 389 | |
| 390 | return ret; |
| 391 | } |