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> |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 11 | #include <linux/usb/otg.h> |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 12 | #include <linux/usb/ch9.h> |
Frank Wang | a4a2912 | 2020-05-26 11:34:30 +0800 | [diff] [blame] | 13 | #include <linux/usb/phy.h> |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 14 | |
| 15 | DECLARE_GLOBAL_DATA_PTR; |
| 16 | |
| 17 | static 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 Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 24 | enum usb_dr_mode usb_get_dr_mode(ofnode node) |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 25 | { |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 26 | const char *dr_mode; |
| 27 | int i; |
| 28 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 29 | dr_mode = ofnode_read_string(node, "dr_mode"); |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 30 | if (!dr_mode) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 31 | pr_err("usb dr_mode not found\n"); |
Mugunthan V N | 1a54713 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 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 | } |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 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 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 51 | enum usb_device_speed usb_get_maximum_speed(ofnode node) |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 52 | { |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 53 | const char *max_speed; |
| 54 | int i; |
| 55 | |
Kever Yang | 1b80705 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 56 | max_speed = ofnode_read_string(node, "maximum-speed"); |
Mugunthan V N | 58d8fbe | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 57 | if (!max_speed) { |
| 58 | pr_err("usb maximum-speed not found\n"); |
| 59 | return USB_SPEED_UNKNOWN; |
| 60 | } |
| 61 | |
| 62 | for (i = 0; i < ARRAY_SIZE(speed_names); i++) |
| 63 | if (!strcmp(max_speed, speed_names[i])) |
| 64 | return i; |
| 65 | |
| 66 | return USB_SPEED_UNKNOWN; |
| 67 | } |
Frank Wang | a4a2912 | 2020-05-26 11:34:30 +0800 | [diff] [blame] | 68 | |
| 69 | #if CONFIG_IS_ENABLED(DM_USB) |
| 70 | static const char *const usbphy_modes[] = { |
| 71 | [USBPHY_INTERFACE_MODE_UNKNOWN] = "", |
| 72 | [USBPHY_INTERFACE_MODE_UTMI] = "utmi", |
| 73 | [USBPHY_INTERFACE_MODE_UTMIW] = "utmi_wide", |
| 74 | }; |
| 75 | |
| 76 | enum usb_phy_interface usb_get_phy_mode(ofnode node) |
| 77 | { |
| 78 | const char *phy_type; |
| 79 | int i; |
| 80 | |
| 81 | phy_type = ofnode_get_property(node, "phy_type", NULL); |
| 82 | if (!phy_type) |
| 83 | return USBPHY_INTERFACE_MODE_UNKNOWN; |
| 84 | |
| 85 | for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++) |
| 86 | if (!strcmp(phy_type, usbphy_modes[i])) |
| 87 | return i; |
| 88 | |
| 89 | return USBPHY_INTERFACE_MODE_UNKNOWN; |
| 90 | } |
| 91 | #endif |