1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Provides code common for host and device side USB.
4  *
5  * (C) Copyright 2016
6  *     Texas Instruments Incorporated, <www.ti.com>
7  */
8 
9 #include <common.h>
10 #include <linux/libfdt.h>
11 #include <linux/usb/otg.h>
12 #include <linux/usb/ch9.h>
13 
14 DECLARE_GLOBAL_DATA_PTR;
15 
16 static const char *const usb_dr_modes[] = {
17 	[USB_DR_MODE_UNKNOWN]		= "",
18 	[USB_DR_MODE_HOST]		= "host",
19 	[USB_DR_MODE_PERIPHERAL]	= "peripheral",
20 	[USB_DR_MODE_OTG]		= "otg",
21 };
22 
usb_get_dr_mode(int node)23 enum usb_dr_mode usb_get_dr_mode(int node)
24 {
25 	const void *fdt = gd->fdt_blob;
26 	const char *dr_mode;
27 	int i;
28 
29 	dr_mode = fdt_getprop(fdt, node, "dr_mode", NULL);
30 	if (!dr_mode) {
31 		pr_err("usb dr_mode not found\n");
32 		return USB_DR_MODE_UNKNOWN;
33 	}
34 
35 	for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++)
36 		if (!strcmp(dr_mode, usb_dr_modes[i]))
37 			return i;
38 
39 	return USB_DR_MODE_UNKNOWN;
40 }
41 
42 static const char *const speed_names[] = {
43 	[USB_SPEED_UNKNOWN] = "UNKNOWN",
44 	[USB_SPEED_LOW] = "low-speed",
45 	[USB_SPEED_FULL] = "full-speed",
46 	[USB_SPEED_HIGH] = "high-speed",
47 	[USB_SPEED_WIRELESS] = "wireless",
48 	[USB_SPEED_SUPER] = "super-speed",
49 };
50 
usb_get_maximum_speed(int node)51 enum usb_device_speed usb_get_maximum_speed(int node)
52 {
53 	const void *fdt = gd->fdt_blob;
54 	const char *max_speed;
55 	int i;
56 
57 	max_speed = fdt_getprop(fdt, node, "maximum-speed", NULL);
58 	if (!max_speed) {
59 		pr_err("usb maximum-speed not found\n");
60 		return USB_SPEED_UNKNOWN;
61 	}
62 
63 	for (i = 0; i < ARRAY_SIZE(speed_names); i++)
64 		if (!strcmp(max_speed, speed_names[i]))
65 			return i;
66 
67 	return USB_SPEED_UNKNOWN;
68 }
69