blob: ee0c064f1ffb9fbb4ad76a08105d2da3838c1619 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mugunthan V N1a547132016-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 N1a547132016-04-12 16:01:19 +05307 */
8
9#include <common.h>
Kever Yang1b807052020-03-04 08:59:50 +080010#include <dm.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060011#include <asm/global_data.h>
Mugunthan V N1a547132016-04-12 16:01:19 +053012#include <linux/usb/otg.h>
Mugunthan V N58d8fbe2018-05-18 13:15:05 +020013#include <linux/usb/ch9.h>
Frank Wanga4a29122020-05-26 11:34:30 +080014#include <linux/usb/phy.h>
Mugunthan V N1a547132016-04-12 16:01:19 +053015
16DECLARE_GLOBAL_DATA_PTR;
17
18static 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
Kever Yang1b807052020-03-04 08:59:50 +080025enum usb_dr_mode usb_get_dr_mode(ofnode node)
Mugunthan V N1a547132016-04-12 16:01:19 +053026{
Mugunthan V N1a547132016-04-12 16:01:19 +053027 const char *dr_mode;
28 int i;
29
Kever Yang1b807052020-03-04 08:59:50 +080030 dr_mode = ofnode_read_string(node, "dr_mode");
Mugunthan V N1a547132016-04-12 16:01:19 +053031 if (!dr_mode) {
Michael Wallef1826562021-10-15 15:15:20 +020032 pr_debug("usb dr_mode not found\n");
Mugunthan V N1a547132016-04-12 16:01:19 +053033 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}
Mugunthan V N58d8fbe2018-05-18 13:15:05 +020042
43static 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",
developer64bb6ce2020-10-14 15:08:27 +080050 [USB_SPEED_SUPER_PLUS] = "super-speed-plus",
Mugunthan V N58d8fbe2018-05-18 13:15:05 +020051};
52
developerb7581582020-10-14 15:08:28 +080053const char *usb_speed_string(enum usb_device_speed speed)
54{
55 if (speed < 0 || speed >= ARRAY_SIZE(speed_names))
56 speed = USB_SPEED_UNKNOWN;
57 return speed_names[speed];
58}
59
Kever Yang1b807052020-03-04 08:59:50 +080060enum usb_device_speed usb_get_maximum_speed(ofnode node)
Mugunthan V N58d8fbe2018-05-18 13:15:05 +020061{
Mugunthan V N58d8fbe2018-05-18 13:15:05 +020062 const char *max_speed;
63 int i;
64
Kever Yang1b807052020-03-04 08:59:50 +080065 max_speed = ofnode_read_string(node, "maximum-speed");
Mugunthan V N58d8fbe2018-05-18 13:15:05 +020066 if (!max_speed) {
Michael Wallef1826562021-10-15 15:15:20 +020067 pr_debug("usb maximum-speed not found\n");
Mugunthan V N58d8fbe2018-05-18 13:15:05 +020068 return USB_SPEED_UNKNOWN;
69 }
70
71 for (i = 0; i < ARRAY_SIZE(speed_names); i++)
72 if (!strcmp(max_speed, speed_names[i]))
73 return i;
74
75 return USB_SPEED_UNKNOWN;
76}
Frank Wanga4a29122020-05-26 11:34:30 +080077
78#if CONFIG_IS_ENABLED(DM_USB)
79static const char *const usbphy_modes[] = {
80 [USBPHY_INTERFACE_MODE_UNKNOWN] = "",
81 [USBPHY_INTERFACE_MODE_UTMI] = "utmi",
82 [USBPHY_INTERFACE_MODE_UTMIW] = "utmi_wide",
Matthias Schiffer382cf4e2021-09-20 15:37:22 +020083 [USBPHY_INTERFACE_MODE_ULPI] = "ulpi",
84 [USBPHY_INTERFACE_MODE_SERIAL] = "serial",
85 [USBPHY_INTERFACE_MODE_HSIC] = "hsic",
Frank Wanga4a29122020-05-26 11:34:30 +080086};
87
88enum usb_phy_interface usb_get_phy_mode(ofnode node)
89{
90 const char *phy_type;
91 int i;
92
93 phy_type = ofnode_get_property(node, "phy_type", NULL);
94 if (!phy_type)
95 return USBPHY_INTERFACE_MODE_UNKNOWN;
96
97 for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++)
98 if (!strcmp(phy_type, usbphy_modes[i]))
99 return i;
100
101 return USBPHY_INTERFACE_MODE_UNKNOWN;
102}
103#endif