1 /* 2 * Copyright (c) 2021, STMicroelectronics - All Rights Reserved 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; 160 uint32_t result; 161 char serial_string[SIZ_STRING_SERIAL + 2U]; 162 uint32_t deviceserial[UID_WORD_NB]; 163 uint16_t length; 164 165 for (i = 0U; i < UID_WORD_NB; i++) { 166 result = bsec_shadow_register(i + UID0_OTP); 167 if (result != BSEC_OK) { 168 ERROR("BSEC: UID%d Shadowing Error\n", i); 169 break; 170 } 171 result = bsec_read_otp(&deviceserial[i], i + UID0_OTP); 172 if (result != BSEC_OK) { 173 ERROR("BSEC: UID%d Read Error\n", i); 174 break; 175 } 176 } 177 /* On bsec error: serial number is set to 0 */ 178 if (result != BSEC_OK) { 179 for (i = 0; i < UID_WORD_NB; i++) { 180 deviceserial[i] = 0U; 181 } 182 } 183 /* build serial number with OTP value as in ROM code */ 184 snprintf(serial_string, sizeof(serial_string), "%08X%08X%08X", 185 deviceserial[0], deviceserial[1], deviceserial[2]); 186 187 length = USB_SIZ_STRING_SERIAL; 188 stm32mp1_get_string(serial_string, usb_stm32mp1_serial, &length); 189 } 190 191 /* 192 * Return Device Qualifier descriptor 193 * length : pointer data length 194 * return : pointer to descriptor buffer 195 */ 196 static uint8_t *stm32mp1_get_qualifier_desc(uint16_t *length) 197 { 198 *length = sizeof(usbd_stm32mp1_qualifier_desc); 199 200 return (uint8_t *)usbd_stm32mp1_qualifier_desc; 201 } 202 203 /* 204 * Return configuration descriptor 205 * length : pointer data length 206 * return : pointer to descriptor buffer 207 */ 208 static uint8_t *stm32mp1_get_config_desc(uint16_t *length) 209 { 210 *length = sizeof(usb_stm32mp1_config_desc); 211 212 return (uint8_t *)usb_stm32mp1_config_desc; 213 } 214 215 /* 216 * Returns the device descriptor. 217 * length: Pointer to data length variable 218 * return : Pointer to descriptor buffer 219 */ 220 static uint8_t *stm32mp1_device_desc(uint16_t *length) 221 { 222 *length = sizeof(usb_stm32mp1_desc); 223 224 return (uint8_t *)usb_stm32mp1_desc; 225 } 226 227 /* 228 * Returns the LangID string descriptor. 229 * length: Pointer to data length variable 230 * return : Pointer to descriptor buffer 231 */ 232 static uint8_t *stm32mp1_lang_id_desc(uint16_t *length) 233 { 234 *length = sizeof(usb_stm32mp1_lang_id_desc); 235 236 return (uint8_t *)usb_stm32mp1_lang_id_desc; 237 } 238 239 /* 240 * Returns the product string descriptor. 241 * length: Pointer to data length variable 242 * return : Pointer to descriptor buffer 243 */ 244 static uint8_t *stm32mp1_product_desc(uint16_t *length) 245 { 246 char name[STM32_SOC_NAME_SIZE]; 247 char product[128]; 248 uint32_t chip_id; 249 uint32_t chip_version; 250 251 stm32mp_get_soc_name(name); 252 chip_id = stm32mp_get_chip_dev_id(); 253 chip_version = stm32mp_get_chip_version(); 254 255 snprintf(product, sizeof(product), 256 "DFU @Device ID /0x%03X, @Revision ID /0x%04X, @Name /%s,", 257 chip_id, chip_version, name); 258 259 stm32mp1_get_string(product, usb_str_dec, length); 260 261 return usb_str_dec; 262 } 263 264 /* 265 * Returns the manufacturer string descriptor. 266 * length: Pointer to data length variable 267 * return : Pointer to descriptor buffer 268 */ 269 static uint8_t *stm32mp1_manufacturer_desc(uint16_t *length) 270 { 271 stm32mp1_get_string(USBD_MANUFACTURER_STRING, usb_str_dec, length); 272 273 return usb_str_dec; 274 } 275 276 /* 277 * Returns the serial number string descriptor. 278 * length: Pointer to data length variable 279 * return : Pointer to descriptor buffer 280 */ 281 static uint8_t *stm32mp1_serial_desc(uint16_t *length) 282 { 283 *length = USB_SIZ_STRING_SERIAL; 284 285 return (uint8_t *)usb_stm32mp1_serial; 286 } 287 288 /* 289 * Returns the configuration string descriptor. 290 * length: Pointer to data length variable 291 * return : Pointer to descriptor buffer 292 */ 293 static uint8_t *stm32mp1_config_desc(uint16_t *length) 294 { 295 stm32mp1_get_string(USBD_CONFIGURATION_STRING, usb_str_dec, length); 296 297 return usb_str_dec; 298 } 299 300 /* 301 * Returns the interface string descriptor. 302 * length : Pointer to data length variable 303 * return : Pointer to descriptor buffer 304 */ 305 static uint8_t *stm32mp1_interface_desc(uint16_t *length) 306 { 307 stm32mp1_get_string(USBD_INTERFACE_STRING, usb_str_dec, length); 308 309 return usb_str_dec; 310 } 311 312 /* 313 * Manages the transfer of memory interfaces string descriptors. 314 * index: descriptor index 315 * length : pointer data length 316 * return : pointer to the descriptor table or NULL if the descriptor 317 * is not supported. 318 */ 319 static uint8_t *stm32mp1_get_usr_desc(uint8_t index, uint16_t *length) 320 { 321 if (index >= ARRAY_SIZE(if_desc_string)) { 322 return NULL; 323 } 324 325 stm32mp1_get_string(if_desc_string[index], usb_str_dec, length); 326 327 return usb_str_dec; 328 } 329 330 static const struct usb_desc dfu_desc = { 331 .get_device_desc = stm32mp1_device_desc, 332 .get_lang_id_desc = stm32mp1_lang_id_desc, 333 .get_manufacturer_desc = stm32mp1_manufacturer_desc, 334 .get_product_desc = stm32mp1_product_desc, 335 .get_configuration_desc = stm32mp1_config_desc, 336 .get_serial_desc = stm32mp1_serial_desc, 337 .get_interface_desc = stm32mp1_interface_desc, 338 .get_usr_desc = stm32mp1_get_usr_desc, 339 .get_config_desc = stm32mp1_get_config_desc, 340 .get_device_qualifier_desc = stm32mp1_get_qualifier_desc, 341 /* only HS is supported, as ROM code */ 342 .get_other_speed_config_desc = NULL, 343 }; 344 345 static struct usb_handle usb_core_handle; 346 static struct pcd_handle pcd_handle; 347 348 struct usb_handle *usb_dfu_plat_init(void) 349 { 350 /* Prepare USB Driver */ 351 pcd_handle.in_ep[0].maxpacket = USB_MAX_EP0_SIZE; 352 pcd_handle.out_ep[0].maxpacket = USB_MAX_EP0_SIZE; 353 stm32mp1_usb_init_driver(&usb_core_handle, &pcd_handle, 354 (uint32_t *)USB_OTG_BASE); 355 356 /* STM32MP15 = keep the configuration from ROM code */ 357 usb_core_handle.ep0_state = USBD_EP0_DATA_IN; 358 usb_core_handle.dev_state = USBD_STATE_CONFIGURED; 359 360 /* Update the serial number string descriptor from the unique ID */ 361 update_serial_num_string(); 362 363 /* Prepare USB DFU stack */ 364 usb_dfu_register(&usb_core_handle, &usb_dfu_handle); 365 366 /* Register DFU descriptor in USB stack */ 367 register_platform(&usb_core_handle, &dfu_desc); 368 369 return &usb_core_handle; 370 } 371 372 /* Link between USB alternate and STM32CubeProgramer phase */ 373 uint8_t usb_dfu_get_phase(uint8_t alt) 374 { 375 uint8_t ret; 376 377 switch (alt) { 378 case 3: 379 ret = PHASE_SSBL; 380 break; 381 case 5: 382 ret = PHASE_CMD; 383 break; 384 default: 385 ret = PHASE_RESET; 386 break; 387 } 388 389 return ret; 390 } 391