xref: /rk3399_rockchip-uboot/drivers/usb/common/common.c (revision b5f6b28fa3454b1189d8fefe01a26dd09f2e3f1e)
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 <linux/libfdt.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 
25 enum usb_dr_mode usb_get_dr_mode(int node)
26 {
27 	const void *fdt = gd->fdt_blob;
28 	const char *dr_mode;
29 	int i;
30 
31 	dr_mode = fdt_getprop(fdt, node, "dr_mode", NULL);
32 	if (!dr_mode) {
33 		pr_err("usb dr_mode not found\n");
34 		return USB_DR_MODE_UNKNOWN;
35 	}
36 
37 	for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
38 		if (!strcmp(dr_mode, usb_dr_modes[i]))
39 			return i;
40 
41 	return USB_DR_MODE_UNKNOWN;
42 }
43 
44 static const char *const speed_names[] = {
45 	[USB_SPEED_UNKNOWN] = "UNKNOWN",
46 	[USB_SPEED_LOW] = "low-speed",
47 	[USB_SPEED_FULL] = "full-speed",
48 	[USB_SPEED_HIGH] = "high-speed",
49 	[USB_SPEED_WIRELESS] = "wireless",
50 	[USB_SPEED_SUPER] = "super-speed",
51 };
52 
53 enum usb_device_speed usb_get_maximum_speed(int node)
54 {
55 	const void *fdt = gd->fdt_blob;
56 	const char *max_speed;
57 	int i;
58 
59 	max_speed = fdt_getprop(fdt, node, "maximum-speed", NULL);
60 	if (!max_speed) {
61 		pr_err("usb maximum-speed not found\n");
62 		return USB_SPEED_UNKNOWN;
63 	}
64 
65 	for (i = 0; i < ARRAY_SIZE(speed_names); i++)
66 		if (!strcmp(max_speed, speed_names[i]))
67 			return i;
68 
69 	return USB_SPEED_UNKNOWN;
70 }
71 
72 #if CONFIG_IS_ENABLED(OF_LIVE) && CONFIG_IS_ENABLED(DM_USB)
73 static const char *const usbphy_modes[] = {
74 	[USBPHY_INTERFACE_MODE_UNKNOWN]	= "",
75 	[USBPHY_INTERFACE_MODE_UTMI]	= "utmi",
76 	[USBPHY_INTERFACE_MODE_UTMIW]	= "utmi_wide",
77 };
78 
79 enum usb_phy_interface usb_get_phy_mode(ofnode node)
80 {
81 	const char *phy_type;
82 	int i;
83 
84 	phy_type = ofnode_get_property(node, "phy_type", NULL);
85 	if (!phy_type)
86 		return USBPHY_INTERFACE_MODE_UNKNOWN;
87 
88 	for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++)
89 		if (!strcmp(phy_type, usbphy_modes[i]))
90 			return i;
91 
92 	return USBPHY_INTERFACE_MODE_UNKNOWN;
93 }
94 #endif
95