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