Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mugunthan V N | c0c62d9 | 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 | c0c62d9 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Kever Yang | ac28e59 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame^] | 11 | #include <asm/global_data.h> |
Mugunthan V N | c0c62d9 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 12 | #include <linux/usb/otg.h> |
Mugunthan V N | 59592b9 | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 13 | #include <linux/usb/ch9.h> |
Frank Wang | 6469794 | 2020-05-26 11:34:30 +0800 | [diff] [blame] | 14 | #include <linux/usb/phy.h> |
Mugunthan V N | c0c62d9 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 15 | |
| 16 | DECLARE_GLOBAL_DATA_PTR; |
| 17 | |
| 18 | static 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 Yang | ac28e59 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 25 | enum usb_dr_mode usb_get_dr_mode(ofnode node) |
Mugunthan V N | c0c62d9 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 26 | { |
Mugunthan V N | c0c62d9 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 27 | const char *dr_mode; |
| 28 | int i; |
| 29 | |
Kever Yang | ac28e59 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 30 | dr_mode = ofnode_read_string(node, "dr_mode"); |
Mugunthan V N | c0c62d9 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 31 | if (!dr_mode) { |
Masahiro Yamada | 9b643e3 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 32 | pr_err("usb dr_mode not found\n"); |
Mugunthan V N | c0c62d9 | 2016-04-12 16:01:19 +0530 | [diff] [blame] | 33 | 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 N | 59592b9 | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 42 | |
| 43 | static 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", |
Chunfeng Yun | a4de6e3 | 2020-10-14 15:08:27 +0800 | [diff] [blame] | 50 | [USB_SPEED_SUPER_PLUS] = "super-speed-plus", |
Mugunthan V N | 59592b9 | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 51 | }; |
| 52 | |
Chunfeng Yun | d92e866 | 2020-10-14 15:08:28 +0800 | [diff] [blame] | 53 | const 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 Yang | ac28e59 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 60 | enum usb_device_speed usb_get_maximum_speed(ofnode node) |
Mugunthan V N | 59592b9 | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 61 | { |
Mugunthan V N | 59592b9 | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 62 | const char *max_speed; |
| 63 | int i; |
| 64 | |
Kever Yang | ac28e59 | 2020-03-04 08:59:50 +0800 | [diff] [blame] | 65 | max_speed = ofnode_read_string(node, "maximum-speed"); |
Mugunthan V N | 59592b9 | 2018-05-18 13:15:05 +0200 | [diff] [blame] | 66 | if (!max_speed) { |
| 67 | pr_err("usb maximum-speed not found\n"); |
| 68 | 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 Wang | 6469794 | 2020-05-26 11:34:30 +0800 | [diff] [blame] | 77 | |
| 78 | #if CONFIG_IS_ENABLED(DM_USB) |
| 79 | static const char *const usbphy_modes[] = { |
| 80 | [USBPHY_INTERFACE_MODE_UNKNOWN] = "", |
| 81 | [USBPHY_INTERFACE_MODE_UTMI] = "utmi", |
| 82 | [USBPHY_INTERFACE_MODE_UTMIW] = "utmi_wide", |
| 83 | }; |
| 84 | |
| 85 | enum usb_phy_interface usb_get_phy_mode(ofnode node) |
| 86 | { |
| 87 | const char *phy_type; |
| 88 | int i; |
| 89 | |
| 90 | phy_type = ofnode_get_property(node, "phy_type", NULL); |
| 91 | if (!phy_type) |
| 92 | return USBPHY_INTERFACE_MODE_UNKNOWN; |
| 93 | |
| 94 | for (i = 0; i < ARRAY_SIZE(usbphy_modes); i++) |
| 95 | if (!strcmp(phy_type, usbphy_modes[i])) |
| 96 | return i; |
| 97 | |
| 98 | return USBPHY_INTERFACE_MODE_UNKNOWN; |
| 99 | } |
| 100 | #endif |