Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2009, 2011 Freescale Semiconductor, Inc. |
| 4 | * |
| 5 | * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB |
| 6 | * |
| 7 | * Author: Tor Krill tor@excito.com |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 8 | */ |
| 9 | |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 10 | #include <log.h> |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 11 | #include <usb.h> |
| 12 | #include <asm/io.h> |
| 13 | #include <hwconfig.h> |
Sriram Dash | 9e8da5c | 2016-06-13 09:58:34 +0530 | [diff] [blame] | 14 | #include <fsl_errata.h> |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 15 | #include <fsl_usb.h> |
| 16 | #include <fdt_support.h> |
| 17 | |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 18 | /* USB Controllers */ |
| 19 | #define FSL_USB2_MPH "fsl-usb2-mph" |
| 20 | #define FSL_USB2_DR "fsl-usb2-dr" |
| 21 | #define CHIPIDEA_USB2 "chipidea,usb2" |
| 22 | #define SNPS_DWC3 "snps,dwc3" |
| 23 | |
Sriram Dash | 2510ff5 | 2016-04-05 14:41:21 +0530 | [diff] [blame] | 24 | static const char * const compat_usb_fsl[] = { |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 25 | FSL_USB2_MPH, |
| 26 | FSL_USB2_DR, |
| 27 | SNPS_DWC3, |
Sriram Dash | 2510ff5 | 2016-04-05 14:41:21 +0530 | [diff] [blame] | 28 | NULL |
| 29 | }; |
| 30 | |
Sriram Dash | c0a7fe2 | 2016-04-05 14:41:22 +0530 | [diff] [blame] | 31 | static int fdt_usb_get_node_type(void *blob, int start_offset, |
| 32 | int *node_offset, const char **node_type) |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 33 | { |
Sriram Dash | 2510ff5 | 2016-04-05 14:41:21 +0530 | [diff] [blame] | 34 | int i; |
Sriram Dash | c0a7fe2 | 2016-04-05 14:41:22 +0530 | [diff] [blame] | 35 | int ret = -ENOENT; |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 36 | |
Sriram Dash | 2510ff5 | 2016-04-05 14:41:21 +0530 | [diff] [blame] | 37 | for (i = 0; compat_usb_fsl[i]; i++) { |
| 38 | *node_offset = fdt_node_offset_by_compatible |
| 39 | (blob, start_offset, |
| 40 | compat_usb_fsl[i]); |
| 41 | if (*node_offset >= 0) { |
Sriram Dash | c0a7fe2 | 2016-04-05 14:41:22 +0530 | [diff] [blame] | 42 | *node_type = compat_usb_fsl[i]; |
| 43 | ret = 0; |
Sriram Dash | 2510ff5 | 2016-04-05 14:41:21 +0530 | [diff] [blame] | 44 | break; |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 45 | } |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 46 | } |
| 47 | |
Sriram Dash | c0a7fe2 | 2016-04-05 14:41:22 +0530 | [diff] [blame] | 48 | return ret; |
Sriram Dash | 70c0815 | 2016-04-05 14:41:20 +0530 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode, |
| 52 | const char *phy_type, int start_offset) |
| 53 | { |
| 54 | const char *prop_mode = "dr_mode"; |
| 55 | const char *prop_type = "phy_type"; |
| 56 | const char *node_type = NULL; |
| 57 | int node_offset; |
| 58 | int err; |
| 59 | |
Sriram Dash | c0a7fe2 | 2016-04-05 14:41:22 +0530 | [diff] [blame] | 60 | err = fdt_usb_get_node_type(blob, start_offset, |
| 61 | &node_offset, &node_type); |
| 62 | if (err < 0) |
| 63 | return err; |
Sriram Dash | 70c0815 | 2016-04-05 14:41:20 +0530 | [diff] [blame] | 64 | |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 65 | if (mode) { |
| 66 | err = fdt_setprop(blob, node_offset, prop_mode, mode, |
| 67 | strlen(mode) + 1); |
| 68 | if (err < 0) |
| 69 | printf("WARNING: could not set %s for %s: %s.\n", |
| 70 | prop_mode, node_type, fdt_strerror(err)); |
| 71 | } |
| 72 | |
| 73 | if (phy_type) { |
| 74 | err = fdt_setprop(blob, node_offset, prop_type, phy_type, |
| 75 | strlen(phy_type) + 1); |
| 76 | if (err < 0) |
| 77 | printf("WARNING: could not set %s for %s: %s.\n", |
| 78 | prop_type, node_type, fdt_strerror(err)); |
| 79 | } |
| 80 | |
| 81 | return node_offset; |
| 82 | } |
| 83 | |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 84 | static int fsl_fdt_fixup_usb_erratum(void *blob, const char *prop_erratum, |
| 85 | const char *controller_type, |
| 86 | int start_offset) |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 87 | { |
| 88 | int node_offset, err; |
| 89 | const char *node_type = NULL; |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 90 | const char *node_name = NULL; |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 91 | |
Sriram Dash | c0a7fe2 | 2016-04-05 14:41:22 +0530 | [diff] [blame] | 92 | err = fdt_usb_get_node_type(blob, start_offset, |
| 93 | &node_offset, &node_type); |
| 94 | if (err < 0) |
| 95 | return err; |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 96 | |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 97 | if (!strcmp(node_type, FSL_USB2_MPH) || !strcmp(node_type, FSL_USB2_DR)) |
| 98 | node_name = CHIPIDEA_USB2; |
| 99 | else |
| 100 | node_name = node_type; |
| 101 | if (strcmp(node_name, controller_type)) |
| 102 | return err; |
| 103 | |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 104 | err = fdt_setprop(blob, node_offset, prop_erratum, NULL, 0); |
| 105 | if (err < 0) { |
| 106 | printf("ERROR: could not set %s for %s: %s.\n", |
| 107 | prop_erratum, node_type, fdt_strerror(err)); |
| 108 | } |
| 109 | |
| 110 | return node_offset; |
| 111 | } |
| 112 | |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 113 | static int fsl_fdt_fixup_erratum(int *usb_erratum_off, void *blob, |
| 114 | const char *controller_type, char *str, |
| 115 | bool (*has_erratum)(void)) |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 116 | { |
| 117 | char buf[32] = {0}; |
| 118 | |
| 119 | snprintf(buf, sizeof(buf), "fsl,usb-erratum-%s", str); |
| 120 | if (!has_erratum()) |
| 121 | return -EINVAL; |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 122 | *usb_erratum_off = fsl_fdt_fixup_usb_erratum(blob, buf, controller_type, |
| 123 | *usb_erratum_off); |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 124 | if (*usb_erratum_off < 0) |
| 125 | return -ENOSPC; |
| 126 | debug("Adding USB erratum %s\n", str); |
| 127 | return 0; |
| 128 | } |
| 129 | |
Masahiro Yamada | f7ed78b | 2020-06-26 15:13:33 +0900 | [diff] [blame] | 130 | void fsl_fdt_fixup_dr_usb(void *blob, struct bd_info *bd) |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 131 | { |
| 132 | static const char * const modes[] = { "host", "peripheral", "otg" }; |
| 133 | static const char * const phys[] = { "ulpi", "utmi", "utmi_dual" }; |
| 134 | int usb_erratum_a006261_off = -1; |
| 135 | int usb_erratum_a007075_off = -1; |
| 136 | int usb_erratum_a007792_off = -1; |
| 137 | int usb_erratum_a005697_off = -1; |
Sriram Dash | 0182095 | 2016-06-13 09:58:36 +0530 | [diff] [blame] | 138 | int usb_erratum_a008751_off = -1; |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 139 | int usb_mode_off = -1; |
| 140 | int usb_phy_off = -1; |
| 141 | char str[5]; |
| 142 | int i, j; |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 143 | int ret; |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 144 | |
| 145 | for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) { |
| 146 | const char *dr_mode_type = NULL; |
| 147 | const char *dr_phy_type = NULL; |
| 148 | int mode_idx = -1, phy_idx = -1; |
| 149 | |
| 150 | snprintf(str, 5, "%s%d", "usb", i); |
| 151 | if (hwconfig(str)) { |
| 152 | for (j = 0; j < ARRAY_SIZE(modes); j++) { |
| 153 | if (hwconfig_subarg_cmp(str, "dr_mode", |
| 154 | modes[j])) { |
| 155 | mode_idx = j; |
| 156 | break; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | for (j = 0; j < ARRAY_SIZE(phys); j++) { |
| 161 | if (hwconfig_subarg_cmp(str, "phy_type", |
| 162 | phys[j])) { |
| 163 | phy_idx = j; |
| 164 | break; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | if (mode_idx < 0 && phy_idx < 0) { |
| 169 | printf("WARNING: invalid phy or mode\n"); |
| 170 | return; |
| 171 | } |
| 172 | |
| 173 | if (mode_idx > -1) |
| 174 | dr_mode_type = modes[mode_idx]; |
| 175 | |
| 176 | if (phy_idx > -1) |
| 177 | dr_phy_type = phys[phy_idx]; |
| 178 | } |
| 179 | |
| 180 | if (has_dual_phy()) |
| 181 | dr_phy_type = phys[2]; |
| 182 | |
| 183 | usb_mode_off = fdt_fixup_usb_mode_phy_type(blob, |
| 184 | dr_mode_type, NULL, |
| 185 | usb_mode_off); |
| 186 | |
| 187 | if (usb_mode_off < 0) |
| 188 | return; |
| 189 | |
| 190 | usb_phy_off = fdt_fixup_usb_mode_phy_type(blob, |
| 191 | NULL, dr_phy_type, |
| 192 | usb_phy_off); |
| 193 | |
| 194 | if (usb_phy_off < 0) |
| 195 | return; |
| 196 | |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 197 | ret = fsl_fdt_fixup_erratum(&usb_erratum_a006261_off, blob, |
| 198 | CHIPIDEA_USB2, "a006261", |
| 199 | has_erratum_a006261); |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 200 | if (ret == -ENOSPC) |
| 201 | return; |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 202 | ret = fsl_fdt_fixup_erratum(&usb_erratum_a007075_off, blob, |
| 203 | CHIPIDEA_USB2, "a007075", |
| 204 | has_erratum_a007075); |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 205 | if (ret == -ENOSPC) |
| 206 | return; |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 207 | ret = fsl_fdt_fixup_erratum(&usb_erratum_a007792_off, blob, |
| 208 | CHIPIDEA_USB2, "a007792", |
| 209 | has_erratum_a007792); |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 210 | if (ret == -ENOSPC) |
| 211 | return; |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 212 | ret = fsl_fdt_fixup_erratum(&usb_erratum_a005697_off, blob, |
| 213 | CHIPIDEA_USB2, "a005697", |
| 214 | has_erratum_a005697); |
Sriram Dash | eacf09c | 2016-06-13 09:58:33 +0530 | [diff] [blame] | 215 | if (ret == -ENOSPC) |
| 216 | return; |
Sriram Dash | a3c1f69 | 2016-09-16 17:12:16 +0530 | [diff] [blame] | 217 | ret = fsl_fdt_fixup_erratum(&usb_erratum_a008751_off, blob, |
| 218 | SNPS_DWC3, "a008751", |
| 219 | has_erratum_a008751); |
Sriram Dash | 0182095 | 2016-06-13 09:58:36 +0530 | [diff] [blame] | 220 | if (ret == -ENOSPC) |
| 221 | return; |
| 222 | |
Sriram Dash | 55f01cc | 2016-04-05 14:41:19 +0530 | [diff] [blame] | 223 | } |
| 224 | } |