blob: 46488dcdcdf54b064763e05fd090f9d0e201110c [file] [log] [blame]
Sriram Dash93eb8f32016-04-05 14:41:19 +05301/*
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 Dashb9f67862016-04-05 14:41:21 +053022static const char * const compat_usb_fsl[] = {
23 "fsl-usb2-mph",
24 "fsl-usb2-dr",
25 "snps,dwc3",
26 NULL
27};
28
Sriram Dash93eb8f32016-04-05 14:41:19 +053029static const char *fdt_usb_get_node_type(void *blob, int start_offset,
30 int *node_offset)
31{
Sriram Dash93eb8f32016-04-05 14:41:19 +053032 const char *node_type = NULL;
Sriram Dashb9f67862016-04-05 14:41:21 +053033 int i;
Sriram Dash93eb8f32016-04-05 14:41:19 +053034
Sriram Dashb9f67862016-04-05 14:41:21 +053035 for (i = 0; compat_usb_fsl[i]; i++) {
36 *node_offset = fdt_node_offset_by_compatible
37 (blob, start_offset,
38 compat_usb_fsl[i]);
39 if (*node_offset >= 0) {
40 node_type = compat_usb_fsl[i];
41 break;
Sriram Dash93eb8f32016-04-05 14:41:19 +053042 }
Sriram Dash93eb8f32016-04-05 14:41:19 +053043 }
44
45 return node_type;
46}
47
Sriram Dash469e72b2016-04-05 14:41:20 +053048static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
49 const char *phy_type, int start_offset)
50{
51 const char *prop_mode = "dr_mode";
52 const char *prop_type = "phy_type";
53 const char *node_type = NULL;
54 int node_offset;
55 int err;
56
57 node_type = fdt_usb_get_node_type(blob, start_offset, &node_offset);
58 if (!node_type)
59 return -1;
60
61 if (mode) {
62 err = fdt_setprop(blob, node_offset, prop_mode, mode,
63 strlen(mode) + 1);
64 if (err < 0)
65 printf("WARNING: could not set %s for %s: %s.\n",
66 prop_mode, node_type, fdt_strerror(err));
67 }
68
69 if (phy_type) {
70 err = fdt_setprop(blob, node_offset, prop_type, phy_type,
71 strlen(phy_type) + 1);
72 if (err < 0)
73 printf("WARNING: could not set %s for %s: %s.\n",
74 prop_type, node_type, fdt_strerror(err));
75 }
76
77 return node_offset;
78}
79
Sriram Dash93eb8f32016-04-05 14:41:19 +053080static int fdt_fixup_usb_erratum(void *blob, const char *prop_erratum,
81 int start_offset)
82{
83 int node_offset, err;
84 const char *node_type = NULL;
85
86 node_type = fdt_usb_get_node_type(blob, start_offset, &node_offset);
87 if (!node_type)
88 return -1;
89
90 err = fdt_setprop(blob, node_offset, prop_erratum, NULL, 0);
91 if (err < 0) {
92 printf("ERROR: could not set %s for %s: %s.\n",
93 prop_erratum, node_type, fdt_strerror(err));
94 }
95
96 return node_offset;
97}
98
99void fdt_fixup_dr_usb(void *blob, bd_t *bd)
100{
101 static const char * const modes[] = { "host", "peripheral", "otg" };
102 static const char * const phys[] = { "ulpi", "utmi", "utmi_dual" };
103 int usb_erratum_a006261_off = -1;
104 int usb_erratum_a007075_off = -1;
105 int usb_erratum_a007792_off = -1;
106 int usb_erratum_a005697_off = -1;
107 int usb_mode_off = -1;
108 int usb_phy_off = -1;
109 char str[5];
110 int i, j;
111
112 for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
113 const char *dr_mode_type = NULL;
114 const char *dr_phy_type = NULL;
115 int mode_idx = -1, phy_idx = -1;
116
117 snprintf(str, 5, "%s%d", "usb", i);
118 if (hwconfig(str)) {
119 for (j = 0; j < ARRAY_SIZE(modes); j++) {
120 if (hwconfig_subarg_cmp(str, "dr_mode",
121 modes[j])) {
122 mode_idx = j;
123 break;
124 }
125 }
126
127 for (j = 0; j < ARRAY_SIZE(phys); j++) {
128 if (hwconfig_subarg_cmp(str, "phy_type",
129 phys[j])) {
130 phy_idx = j;
131 break;
132 }
133 }
134
135 if (mode_idx < 0 && phy_idx < 0) {
136 printf("WARNING: invalid phy or mode\n");
137 return;
138 }
139
140 if (mode_idx > -1)
141 dr_mode_type = modes[mode_idx];
142
143 if (phy_idx > -1)
144 dr_phy_type = phys[phy_idx];
145 }
146
147 if (has_dual_phy())
148 dr_phy_type = phys[2];
149
150 usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
151 dr_mode_type, NULL,
152 usb_mode_off);
153
154 if (usb_mode_off < 0)
155 return;
156
157 usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
158 NULL, dr_phy_type,
159 usb_phy_off);
160
161 if (usb_phy_off < 0)
162 return;
163
164 if (has_erratum_a006261()) {
165 usb_erratum_a006261_off = fdt_fixup_usb_erratum
166 (blob,
167 "fsl,usb-erratum-a006261",
168 usb_erratum_a006261_off);
169 if (usb_erratum_a006261_off < 0)
170 return;
171 }
172
173 if (has_erratum_a007075()) {
174 usb_erratum_a007075_off = fdt_fixup_usb_erratum
175 (blob,
176 "fsl,usb-erratum-a007075",
177 usb_erratum_a007075_off);
178 if (usb_erratum_a007075_off < 0)
179 return;
180 }
181
182 if (has_erratum_a007792()) {
183 usb_erratum_a007792_off = fdt_fixup_usb_erratum
184 (blob,
185 "fsl,usb-erratum-a007792",
186 usb_erratum_a007792_off);
187 if (usb_erratum_a007792_off < 0)
188 return;
189 }
190 if (has_erratum_a005697()) {
191 usb_erratum_a005697_off = fdt_fixup_usb_erratum
192 (blob,
193 "fsl,usb-erratum-a005697",
194 usb_erratum_a005697_off);
195 if (usb_erratum_a005697_off < 0)
196 return;
197 }
198 }
199}