1 /*
2 * Provides code common for host and device side USB.
3 *
4 * (C) Copyright 2016
5 * Texas Instruments Incorporated, <www.ti.com>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10 #include <common.h>
11 #include <dm.h>
12 #include <linux/usb/otg.h>
13 #include <linux/usb/ch9.h>
14 #include <linux/usb/phy.h>
15
16 DECLARE_GLOBAL_DATA_PTR;
17
18 static const char *const usb_dr_modes[] = {
19 [USB_DR_MODE_UNKNOWN] = "",
20 [USB_DR_MODE_HOST] = "host",
21 [USB_DR_MODE_PERIPHERAL] = "peripheral",
22 [USB_DR_MODE_OTG] = "otg",
23 };
24
usb_get_dr_mode(ofnode node)25 enum usb_dr_mode usb_get_dr_mode(ofnode node)
26 {
27 const char *dr_mode;
28 int i;
29
30 dr_mode = ofnode_read_string(node, "dr_mode");
31 if (!dr_mode) {
32 pr_err("usb dr_mode not found\n");
33 return USB_DR_MODE_UNKNOWN;
34 }
35
36 for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
37 if (!strcmp(dr_mode, usb_dr_modes[i]))
38 return i;
39
40 return USB_DR_MODE_UNKNOWN;
41 }
42
43 static const char *const speed_names[] = {
44 [USB_SPEED_UNKNOWN] = "UNKNOWN",
45 [USB_SPEED_LOW] = "low-speed",
46 [USB_SPEED_FULL] = "full-speed",
47 [USB_SPEED_HIGH] = "high-speed",
48 [USB_SPEED_WIRELESS] = "wireless",
49 [USB_SPEED_SUPER] = "super-speed",
50 };
51
usb_speed_string(enum usb_device_speed speed)52 const char *usb_speed_string(enum usb_device_speed speed)
53 {
54 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
55 speed = USB_SPEED_UNKNOWN;
56 return speed_names[speed];
57 }
58
usb_get_maximum_speed(ofnode node)59 enum usb_device_speed usb_get_maximum_speed(ofnode node)
60 {
61 const char *max_speed;
62 int i;
63
64 max_speed = ofnode_read_string(node, "maximum-speed");
65 if (!max_speed) {
66 pr_err("usb maximum-speed not found\n");
67 return USB_SPEED_UNKNOWN;
68 }
69
70 for (i = 0; i < ARRAY_SIZE(speed_names); i++)
71 if (!strcmp(max_speed, speed_names[i]))
72 return i;
73
74 return USB_SPEED_UNKNOWN;
75 }
76
77 #if CONFIG_IS_ENABLED(OF_LIVE) && CONFIG_IS_ENABLED(DM_USB)
78 static const char *const usbphy_modes[] = {
79 [USBPHY_INTERFACE_MODE_UNKNOWN] = "",
80 [USBPHY_INTERFACE_MODE_UTMI] = "utmi",
81 [USBPHY_INTERFACE_MODE_UTMIW] = "utmi_wide",
82 };
83
usb_get_phy_mode(ofnode node)84 enum usb_phy_interface usb_get_phy_mode(ofnode node)
85 {
86 const char *phy_type;
87 int i;
88
89 phy_type = ofnode_get_property(node, "phy_type", NULL);
90 if (!phy_type)
91 return USBPHY_INTERFACE_MODE_UNKNOWN;
92
93 for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++)
94 if (!strcmp(phy_type, usbphy_modes[i]))
95 return i;
96
97 return USBPHY_INTERFACE_MODE_UNKNOWN;
98 }
99 #endif
100