blob: 5e5c3c3e3dc987ba653b3a8df2cece150d1bcfc5 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mugunthan V Nc0c62d92016-04-12 16:01:19 +05302/*
3 * Provides code common for host and device side USB.
4 *
5 * (C) Copyright 2016
6 * Texas Instruments Incorporated, <www.ti.com>
Mugunthan V Nc0c62d92016-04-12 16:01:19 +05307 */
8
9#include <common.h>
Kever Yangac28e592020-03-04 08:59:50 +080010#include <dm.h>
Mugunthan V Nc0c62d92016-04-12 16:01:19 +053011#include <linux/usb/otg.h>
Mugunthan V N59592b92018-05-18 13:15:05 +020012#include <linux/usb/ch9.h>
Frank Wang64697942020-05-26 11:34:30 +080013#include <linux/usb/phy.h>
Mugunthan V Nc0c62d92016-04-12 16:01:19 +053014
15DECLARE_GLOBAL_DATA_PTR;
16
17static const char *const usb_dr_modes[] = {
18 [USB_DR_MODE_UNKNOWN] = "",
19 [USB_DR_MODE_HOST] = "host",
20 [USB_DR_MODE_PERIPHERAL] = "peripheral",
21 [USB_DR_MODE_OTG] = "otg",
22};
23
Kever Yangac28e592020-03-04 08:59:50 +080024enum usb_dr_mode usb_get_dr_mode(ofnode node)
Mugunthan V Nc0c62d92016-04-12 16:01:19 +053025{
Mugunthan V Nc0c62d92016-04-12 16:01:19 +053026 const char *dr_mode;
27 int i;
28
Kever Yangac28e592020-03-04 08:59:50 +080029 dr_mode = ofnode_read_string(node, "dr_mode");
Mugunthan V Nc0c62d92016-04-12 16:01:19 +053030 if (!dr_mode) {
Masahiro Yamada9b643e32017-09-16 14:10:41 +090031 pr_err("usb dr_mode not found\n");
Mugunthan V Nc0c62d92016-04-12 16:01:19 +053032 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}
Mugunthan V N59592b92018-05-18 13:15:05 +020041
42static 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",
Chunfeng Yuna4de6e32020-10-14 15:08:27 +080049 [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
Mugunthan V N59592b92018-05-18 13:15:05 +020050};
51
Chunfeng Yund92e8662020-10-14 15:08:28 +080052const 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
Kever Yangac28e592020-03-04 08:59:50 +080059enum usb_device_speed usb_get_maximum_speed(ofnode node)
Mugunthan V N59592b92018-05-18 13:15:05 +020060{
Mugunthan V N59592b92018-05-18 13:15:05 +020061 const char *max_speed;
62 int i;
63
Kever Yangac28e592020-03-04 08:59:50 +080064 max_speed = ofnode_read_string(node, "maximum-speed");
Mugunthan V N59592b92018-05-18 13:15:05 +020065 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}
Frank Wang64697942020-05-26 11:34:30 +080076
77#if CONFIG_IS_ENABLED(DM_USB)
78static 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
84enum 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