blob: 00b8cd368b1af569e5c50e2188628fad74dfa62b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sriram Dash55f01cc2016-04-05 14:41:19 +05302/*
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 Dash55f01cc2016-04-05 14:41:19 +05308 */
9
10#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
Sriram Dash55f01cc2016-04-05 14:41:19 +053012#include <usb.h>
13#include <asm/io.h>
14#include <hwconfig.h>
Sriram Dash9e8da5c2016-06-13 09:58:34 +053015#include <fsl_errata.h>
Sriram Dash55f01cc2016-04-05 14:41:19 +053016#include <fsl_usb.h>
17#include <fdt_support.h>
18
Sriram Dasheacf09c2016-06-13 09:58:33 +053019/* 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 Dash2510ff52016-04-05 14:41:21 +053025static const char * const compat_usb_fsl[] = {
Sriram Dasheacf09c2016-06-13 09:58:33 +053026 FSL_USB2_MPH,
27 FSL_USB2_DR,
28 SNPS_DWC3,
Sriram Dash2510ff52016-04-05 14:41:21 +053029 NULL
30};
31
Sriram Dashc0a7fe22016-04-05 14:41:22 +053032static int fdt_usb_get_node_type(void *blob, int start_offset,
33 int *node_offset, const char **node_type)
Sriram Dash55f01cc2016-04-05 14:41:19 +053034{
Sriram Dash2510ff52016-04-05 14:41:21 +053035 int i;
Sriram Dashc0a7fe22016-04-05 14:41:22 +053036 int ret = -ENOENT;
Sriram Dash55f01cc2016-04-05 14:41:19 +053037
Sriram Dash2510ff52016-04-05 14:41:21 +053038 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 Dashc0a7fe22016-04-05 14:41:22 +053043 *node_type = compat_usb_fsl[i];
44 ret = 0;
Sriram Dash2510ff52016-04-05 14:41:21 +053045 break;
Sriram Dash55f01cc2016-04-05 14:41:19 +053046 }
Sriram Dash55f01cc2016-04-05 14:41:19 +053047 }
48
Sriram Dashc0a7fe22016-04-05 14:41:22 +053049 return ret;
Sriram Dash70c08152016-04-05 14:41:20 +053050}
51
52static 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 Dashc0a7fe22016-04-05 14:41:22 +053061 err = fdt_usb_get_node_type(blob, start_offset,
62 &node_offset, &node_type);
63 if (err < 0)
64 return err;
Sriram Dash70c08152016-04-05 14:41:20 +053065
Sriram Dash55f01cc2016-04-05 14:41:19 +053066 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 Dasha3c1f692016-09-16 17:12:16 +053085static int fsl_fdt_fixup_usb_erratum(void *blob, const char *prop_erratum,
86 const char *controller_type,
87 int start_offset)
Sriram Dash55f01cc2016-04-05 14:41:19 +053088{
89 int node_offset, err;
90 const char *node_type = NULL;
Sriram Dasheacf09c2016-06-13 09:58:33 +053091 const char *node_name = NULL;
Sriram Dash55f01cc2016-04-05 14:41:19 +053092
Sriram Dashc0a7fe22016-04-05 14:41:22 +053093 err = fdt_usb_get_node_type(blob, start_offset,
94 &node_offset, &node_type);
95 if (err < 0)
96 return err;
Sriram Dash55f01cc2016-04-05 14:41:19 +053097
Sriram Dasheacf09c2016-06-13 09:58:33 +053098 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 Dash55f01cc2016-04-05 14:41:19 +0530105 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 Dasha3c1f692016-09-16 17:12:16 +0530114static int fsl_fdt_fixup_erratum(int *usb_erratum_off, void *blob,
115 const char *controller_type, char *str,
116 bool (*has_erratum)(void))
Sriram Dasheacf09c2016-06-13 09:58:33 +0530117{
118 char buf[32] = {0};
119
120 snprintf(buf, sizeof(buf), "fsl,usb-erratum-%s", str);
121 if (!has_erratum())
122 return -EINVAL;
Sriram Dasha3c1f692016-09-16 17:12:16 +0530123 *usb_erratum_off = fsl_fdt_fixup_usb_erratum(blob, buf, controller_type,
124 *usb_erratum_off);
Sriram Dasheacf09c2016-06-13 09:58:33 +0530125 if (*usb_erratum_off < 0)
126 return -ENOSPC;
127 debug("Adding USB erratum %s\n", str);
128 return 0;
129}
130
Masahiro Yamadaf7ed78b2020-06-26 15:13:33 +0900131void fsl_fdt_fixup_dr_usb(void *blob, struct bd_info *bd)
Sriram Dash55f01cc2016-04-05 14:41:19 +0530132{
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 Dash01820952016-06-13 09:58:36 +0530139 int usb_erratum_a008751_off = -1;
Sriram Dash55f01cc2016-04-05 14:41:19 +0530140 int usb_mode_off = -1;
141 int usb_phy_off = -1;
142 char str[5];
143 int i, j;
Sriram Dasheacf09c2016-06-13 09:58:33 +0530144 int ret;
Sriram Dash55f01cc2016-04-05 14:41:19 +0530145
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 Dasha3c1f692016-09-16 17:12:16 +0530198 ret = fsl_fdt_fixup_erratum(&usb_erratum_a006261_off, blob,
199 CHIPIDEA_USB2, "a006261",
200 has_erratum_a006261);
Sriram Dasheacf09c2016-06-13 09:58:33 +0530201 if (ret == -ENOSPC)
202 return;
Sriram Dasha3c1f692016-09-16 17:12:16 +0530203 ret = fsl_fdt_fixup_erratum(&usb_erratum_a007075_off, blob,
204 CHIPIDEA_USB2, "a007075",
205 has_erratum_a007075);
Sriram Dasheacf09c2016-06-13 09:58:33 +0530206 if (ret == -ENOSPC)
207 return;
Sriram Dasha3c1f692016-09-16 17:12:16 +0530208 ret = fsl_fdt_fixup_erratum(&usb_erratum_a007792_off, blob,
209 CHIPIDEA_USB2, "a007792",
210 has_erratum_a007792);
Sriram Dasheacf09c2016-06-13 09:58:33 +0530211 if (ret == -ENOSPC)
212 return;
Sriram Dasha3c1f692016-09-16 17:12:16 +0530213 ret = fsl_fdt_fixup_erratum(&usb_erratum_a005697_off, blob,
214 CHIPIDEA_USB2, "a005697",
215 has_erratum_a005697);
Sriram Dasheacf09c2016-06-13 09:58:33 +0530216 if (ret == -ENOSPC)
217 return;
Sriram Dasha3c1f692016-09-16 17:12:16 +0530218 ret = fsl_fdt_fixup_erratum(&usb_erratum_a008751_off, blob,
219 SNPS_DWC3, "a008751",
220 has_erratum_a008751);
Sriram Dash01820952016-06-13 09:58:36 +0530221 if (ret == -ENOSPC)
222 return;
223
Sriram Dash55f01cc2016-04-05 14:41:19 +0530224 }
225}