Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Provides code common for host and device side USB. |
| 4 | * |
| 5 | * (C) Copyright 2016 |
| 6 | * Texas Instruments Incorporated, <www.ti.com> |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 11 | #include <asm/global_data.h> |
Simon Glass | bdd5f81 | 2023-09-14 18:21:46 -0600 | [diff] [blame] | 12 | #include <linux/printk.h> |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 13 | #include <linux/usb/otg.h> |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 14 | #include <linux/usb/ch9.h> |
Frank Wang | a4a2912 | 2020-05-26 11:34:30 +0800 | [diff] [blame] | 15 | #include <linux/usb/phy.h> |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 16 | |
| 17 | DECLARE_GLOBAL_DATA_PTR; |
| 18 | |
| 19 | static const char *const usb_dr_modes[] = { |
| 20 | [USB_DR_MODE_UNKNOWN] = "", |
| 21 | [USB_DR_MODE_HOST] = "host", |
| 22 | [USB_DR_MODE_PERIPHERAL] = "peripheral", |
| 23 | [USB_DR_MODE_OTG] = "otg", |
| 24 | }; |
| 25 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 26 | enum usb_dr_mode usb_get_dr_mode(ofnode node) |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 27 | { |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 28 | const char *dr_mode; |
| 29 | int i; |
| 30 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 31 | dr_mode = ofnode_read_string(node, "dr_mode"); |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 32 | if (!dr_mode) { |
Michael Walle | f182656 | 2021-10-15 15:15:20 +0200 | [diff] [blame] | 33 | pr_debug("usb dr_mode not found\n"); |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 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 | } |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 43 | |
Mark Kettenis | 5732eae | 2022-04-19 21:06:33 +0200 | [diff] [blame] | 44 | enum usb_dr_mode usb_get_role_switch_default_mode(ofnode node) |
| 45 | { |
| 46 | const char *dr_mode; |
| 47 | int i; |
| 48 | |
| 49 | dr_mode = ofnode_read_string(node, "role-switch-default-mode"); |
| 50 | if (!dr_mode) |
| 51 | return USB_DR_MODE_UNKNOWN; |
| 52 | |
| 53 | for (i = 0; i < ARRAY_SIZE(usb_dr_modes); i++) |
| 54 | if (!strcmp(dr_mode, usb_dr_modes[i])) |
| 55 | return i; |
| 56 | |
| 57 | return USB_DR_MODE_UNKNOWN; |
| 58 | } |
| 59 | |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 60 | static const char *const speed_names[] = { |
| 61 | [USB_SPEED_UNKNOWN] = "UNKNOWN", |
| 62 | [USB_SPEED_LOW] = "low-speed", |
| 63 | [USB_SPEED_FULL] = "full-speed", |
| 64 | [USB_SPEED_HIGH] = "high-speed", |
| 65 | [USB_SPEED_WIRELESS] = "wireless", |
| 66 | [USB_SPEED_SUPER] = "super-speed", |
developer | 64bb6ce | 2020-10-14 15:08:27 +0800 | [diff] [blame] | 67 | [USB_SPEED_SUPER_PLUS] = "super-speed-plus", |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 68 | }; |
| 69 | |
developer | b758158 | 2020-10-14 15:08:28 +0800 | [diff] [blame] | 70 | const char *usb_speed_string(enum usb_device_speed speed) |
| 71 | { |
| 72 | if (speed < 0 || speed >= ARRAY_SIZE(speed_names)) |
| 73 | speed = USB_SPEED_UNKNOWN; |
| 74 | return speed_names[speed]; |
| 75 | } |
| 76 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 77 | enum usb_device_speed usb_get_maximum_speed(ofnode node) |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 78 | { |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 79 | const char *max_speed; |
| 80 | int i; |
| 81 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 82 | max_speed = ofnode_read_string(node, "maximum-speed"); |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 83 | if (!max_speed) { |
Michael Walle | f182656 | 2021-10-15 15:15:20 +0200 | [diff] [blame] | 84 | pr_debug("usb maximum-speed not found\n"); |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 85 | return USB_SPEED_UNKNOWN; |
| 86 | } |
| 87 | |
| 88 | for (i = 0; i < ARRAY_SIZE(speed_names); i++) |
| 89 | if (!strcmp(max_speed, speed_names[i])) |
| 90 | return i; |
| 91 | |
| 92 | return USB_SPEED_UNKNOWN; |
| 93 | } |
Frank Wang | a4a2912 | 2020-05-26 11:34:30 +0800 | [diff] [blame] | 94 | |
| 95 | #if CONFIG_IS_ENABLED(DM_USB) |
| 96 | static const char *const usbphy_modes[] = { |
| 97 | [USBPHY_INTERFACE_MODE_UNKNOWN] = "", |
| 98 | [USBPHY_INTERFACE_MODE_UTMI] = "utmi", |
| 99 | [USBPHY_INTERFACE_MODE_UTMIW] = "utmi_wide", |
Matthias Schiffer | 382cf4e | 2021-09-20 15:37:22 +0200 | [diff] [blame] | 100 | [USBPHY_INTERFACE_MODE_ULPI] = "ulpi", |
| 101 | [USBPHY_INTERFACE_MODE_SERIAL] = "serial", |
| 102 | [USBPHY_INTERFACE_MODE_HSIC] = "hsic", |
Frank Wang | a4a2912 | 2020-05-26 11:34:30 +0800 | [diff] [blame] | 103 | }; |
| 104 | |
| 105 | enum usb_phy_interface usb_get_phy_mode(ofnode node) |
| 106 | { |
| 107 | const char *phy_type; |
| 108 | int i; |
| 109 | |
| 110 | phy_type = ofnode_get_property(node, "phy_type", NULL); |
| 111 | if (!phy_type) |
| 112 | return USBPHY_INTERFACE_MODE_UNKNOWN; |
| 113 | |
| 114 | for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++) |
| 115 | if (!strcmp(phy_type, usbphy_modes[i])) |
| 116 | return i; |
| 117 | |
| 118 | return USBPHY_INTERFACE_MODE_UNKNOWN; |
| 119 | } |
| 120 | #endif |