stm32mp1_usb_dfu.c 11 KB

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