Skip to content

Commit 35adb6e

Browse files
aurel32nashif
authored andcommitted
drivers: usb_dc_sam: get the maximum speed from DT as enum
For historical reasons, the maximum speed of the Atmel SAM USB controller is done by comparing the maximum-speed property string from the DT using strncmp at runtime. Now that the DT_ENUM_IDX macro exists, we can use it to get the maximum-speed property at build time and without string comparison. Unsupported speed can also be reported at build time. Note that the default speed in case the optional maximum-speed property isn't present in the DT is changed from full-speed to high-speed to match the property description. This is a no-op in practice as this properties is defined at the soc level. Signed-off-by: Aurelien Jarno <[email protected]>
1 parent d5c60ff commit 35adb6e

File tree

1 file changed

+15
-17
lines changed

1 file changed

+15
-17
lines changed

drivers/usb/device/usb_dc_sam.c

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,9 @@ LOG_MODULE_REGISTER(usb_dc_sam);
4747

4848
#define NUM_OF_EP_MAX DT_INST_PROP(0, num_bidir_endpoints)
4949
#if DT_INST_NODE_HAS_PROP(0, maximum_speed)
50-
#define USB_MAXIMUM_SPEED DT_INST_PROP(0, maximum_speed)
50+
#define USB_MAXIMUM_SPEED DT_ENUM_IDX(DT_DRV_INST(0), maximum_speed)
51+
#else
52+
#define USB_MAXIMUM_SPEED 2 /* Default to high-speed */
5153
#endif
5254

5355
struct usb_device_ep_data {
@@ -310,23 +312,19 @@ int usb_dc_attach(void)
310312

311313
/* Select the speed */
312314
regval = USBHS_DEVCTRL_DETACH;
313-
#ifdef USB_MAXIMUM_SPEED
314-
if (!strncmp(USB_MAXIMUM_SPEED, "high-speed", 10)) {
315-
regval |= USBHS_DEVCTRL_SPDCONF_NORMAL;
316-
} else if (!strncmp(USB_MAXIMUM_SPEED, "full-speed", 10)) {
317-
regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER;
318-
} else if (!strncmp(USB_MAXIMUM_SPEED, "low-speed", 9)) {
319-
regval |= USBHS_DEVCTRL_LS;
320-
regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER;
321-
} else {
322-
regval |= USBHS_DEVCTRL_SPDCONF_NORMAL;
323-
LOG_WRN("Unsupported maximum speed defined in device tree. "
324-
"USB controller will default to its maximum HW "
325-
"capability");
326-
}
327-
#else
315+
#if USB_MAXIMUM_SPEED == 0
316+
/* low-speed */
317+
regval |= USBHS_DEVCTRL_LS;
318+
regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER;
319+
#elif USB_MAXIMUM_SPEED == 1
320+
/* full-speed */
321+
regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER;
322+
#elif USB_MAXIMUM_SPEED == 2
323+
/* high-speed */
328324
regval |= USBHS_DEVCTRL_SPDCONF_NORMAL;
329-
#endif /* USB_MAXIMUM_SPEED */
325+
#else
326+
#error "Unsupported maximum speed defined in device tree."
327+
#endif
330328
USBHS->USBHS_DEVCTRL = regval;
331329

332330
/* Enable the USB clock */

0 commit comments

Comments
 (0)