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