blob: 051d4353295cf05aacf2aeebd2c1a301e3731a80 [file] [log] [blame]
Patrick Delaunayf12b7452021-06-30 17:06:19 +02001/*
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 */
36static struct usb_dfu_handle usb_dfu_handle;
37
38/* USB Standard Device Descriptor */
39static 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 */
61static 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 */
69static const uint8_t
70usbd_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 */
84static uint8_t usb_stm32mp1_serial[USB_SIZ_STRING_SERIAL + 1];
85
86/* USB DFU device Configuration Descriptor */
87static 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 */
118const 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 */
128static 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 */
136static 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 */
157static 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 */
196static 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 */
208static 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 */
220static 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 */
232static 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 */
244static 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 */
269static 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 */
281static 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 */
293static 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 */
305static 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 */
319static 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
330static 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};
342
343static struct usb_handle usb_core_handle;
344static struct pcd_handle pcd_handle;
345
346struct usb_handle *usb_dfu_plat_init(void)
347{
348 /* Prepare USB Driver */
349 pcd_handle.in_ep[0].maxpacket = USB_MAX_EP0_SIZE;
350 pcd_handle.out_ep[0].maxpacket = USB_MAX_EP0_SIZE;
351 stm32mp1_usb_init_driver(&usb_core_handle, &pcd_handle,
352 (uint32_t *)USB_OTG_BASE);
353
354 /* STM32MP15 = keep the configuration from ROM code */
355 usb_core_handle.ep0_state = USBD_EP0_DATA_IN;
356 usb_core_handle.dev_state = USBD_STATE_CONFIGURED;
357
358 /* Update the serial number string descriptor from the unique ID */
359 update_serial_num_string();
360
361 /* Prepare USB DFU stack */
362 usb_dfu_register(&usb_core_handle, &usb_dfu_handle);
363
364 /* Register DFU descriptor in USB stack */
365 register_platform(&usb_core_handle, &dfu_desc);
366
367 return &usb_core_handle;
368}
369
370/* Link between USB alternate and STM32CubeProgramer phase */
371uint8_t usb_dfu_get_phase(uint8_t alt)
372{
373 uint8_t ret;
374
375 switch (alt) {
376 case 3:
377 ret = PHASE_SSBL;
378 break;
379 case 5:
380 ret = PHASE_CMD;
381 break;
382 default:
383 ret = PHASE_RESET;
384 break;
385 }
386
387 return ret;
388}