From 35adb6e2c4e5c9bbf3bbda92c1c8dcdd429915fa Mon Sep 17 00:00:00 2001 From: Aurelien Jarno Date: Tue, 12 May 2020 23:36:36 +0200 Subject: [PATCH] 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 --- drivers/usb/device/usb_dc_sam.c | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/drivers/usb/device/usb_dc_sam.c b/drivers/usb/device/usb_dc_sam.c index b9ea3a1c4c2..e474423989a 100644 --- a/drivers/usb/device/usb_dc_sam.c +++ b/drivers/usb/device/usb_dc_sam.c @@ -47,7 +47,9 @@ LOG_MODULE_REGISTER(usb_dc_sam); #define NUM_OF_EP_MAX DT_INST_PROP(0, num_bidir_endpoints) #if DT_INST_NODE_HAS_PROP(0, maximum_speed) -#define USB_MAXIMUM_SPEED DT_INST_PROP(0, maximum_speed) +#define USB_MAXIMUM_SPEED DT_ENUM_IDX(DT_DRV_INST(0), maximum_speed) +#else +#define USB_MAXIMUM_SPEED 2 /* Default to high-speed */ #endif struct usb_device_ep_data { @@ -310,23 +312,19 @@ int usb_dc_attach(void) /* Select the speed */ regval = USBHS_DEVCTRL_DETACH; -#ifdef USB_MAXIMUM_SPEED - if (!strncmp(USB_MAXIMUM_SPEED, "high-speed", 10)) { - regval |= USBHS_DEVCTRL_SPDCONF_NORMAL; - } else if (!strncmp(USB_MAXIMUM_SPEED, "full-speed", 10)) { - regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER; - } else if (!strncmp(USB_MAXIMUM_SPEED, "low-speed", 9)) { - regval |= USBHS_DEVCTRL_LS; - regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER; - } else { - regval |= USBHS_DEVCTRL_SPDCONF_NORMAL; - LOG_WRN("Unsupported maximum speed defined in device tree. " - "USB controller will default to its maximum HW " - "capability"); - } -#else +#if USB_MAXIMUM_SPEED == 0 + /* low-speed */ + regval |= USBHS_DEVCTRL_LS; + regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER; +#elif USB_MAXIMUM_SPEED == 1 + /* full-speed */ + regval |= USBHS_DEVCTRL_SPDCONF_LOW_POWER; +#elif USB_MAXIMUM_SPEED == 2 + /* high-speed */ regval |= USBHS_DEVCTRL_SPDCONF_NORMAL; -#endif /* USB_MAXIMUM_SPEED */ +#else +#error "Unsupported maximum speed defined in device tree." +#endif USBHS->USBHS_DEVCTRL = regval; /* Enable the USB clock */