blob: 92adb46da3ddcb29e9ab26cdf0f9f1c31848e4cb [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
22static int fdt_fixup_usb_mode_phy_type(void *blob, const char *mode,
23 const char *phy_type, int start_offset)
24{
25 const char *compat_dr = "fsl-usb2-dr";
26 const char *compat_mph = "fsl-usb2-mph";
27 const char *prop_mode = "dr_mode";
28 const char *prop_type = "phy_type";
29 const char *node_type = NULL;
30 int node_offset;
31 int err;
32
33 node_offset = fdt_node_offset_by_compatible(blob,
34 start_offset, compat_mph);
35 if (node_offset < 0) {
36 node_offset = fdt_node_offset_by_compatible(blob,
37 start_offset,
38 compat_dr);
39 if (node_offset < 0) {
40 printf("WARNING: could not find compatible node: %s",
41 fdt_strerror(node_offset));
42 return -1;
43 }
44 node_type = compat_dr;
45 } else {
46 node_type = compat_mph;
47 }
48
49 if (mode) {
50 err = fdt_setprop(blob, node_offset, prop_mode, mode,
51 strlen(mode) + 1);
52 if (err < 0)
53 printf("WARNING: could not set %s for %s: %s.\n",
54 prop_mode, node_type, fdt_strerror(err));
55 }
56
57 if (phy_type) {
58 err = fdt_setprop(blob, node_offset, prop_type, phy_type,
59 strlen(phy_type) + 1);
60 if (err < 0)
61 printf("WARNING: could not set %s for %s: %s.\n",
62 prop_type, node_type, fdt_strerror(err));
63 }
64
65 return node_offset;
66}
67
68static const char *fdt_usb_get_node_type(void *blob, int start_offset,
69 int *node_offset)
70{
71 const char *compat_dr = "fsl-usb2-dr";
72 const char *compat_mph = "fsl-usb2-mph";
73 const char *node_type = NULL;
74
75 *node_offset = fdt_node_offset_by_compatible(blob, start_offset,
76 compat_mph);
77 if (*node_offset < 0) {
78 *node_offset = fdt_node_offset_by_compatible(blob,
79 start_offset,
80 compat_dr);
81 if (*node_offset < 0) {
82 printf("ERROR: could not find compatible node: %s\n",
83 fdt_strerror(*node_offset));
84 } else {
85 node_type = compat_dr;
86 }
87 } else {
88 node_type = compat_mph;
89 }
90
91 return node_type;
92}
93
94static int fdt_fixup_usb_erratum(void *blob, const char *prop_erratum,
95 int start_offset)
96{
97 int node_offset, err;
98 const char *node_type = NULL;
99
100 node_type = fdt_usb_get_node_type(blob, start_offset, &node_offset);
101 if (!node_type)
102 return -1;
103
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
113void fdt_fixup_dr_usb(void *blob, bd_t *bd)
114{
115 static const char * const modes[] = { "host", "peripheral", "otg" };
116 static const char * const phys[] = { "ulpi", "utmi", "utmi_dual" };
117 int usb_erratum_a006261_off = -1;
118 int usb_erratum_a007075_off = -1;
119 int usb_erratum_a007792_off = -1;
120 int usb_erratum_a005697_off = -1;
121 int usb_mode_off = -1;
122 int usb_phy_off = -1;
123 char str[5];
124 int i, j;
125
126 for (i = 1; i <= CONFIG_USB_MAX_CONTROLLER_COUNT; i++) {
127 const char *dr_mode_type = NULL;
128 const char *dr_phy_type = NULL;
129 int mode_idx = -1, phy_idx = -1;
130
131 snprintf(str, 5, "%s%d", "usb", i);
132 if (hwconfig(str)) {
133 for (j = 0; j < ARRAY_SIZE(modes); j++) {
134 if (hwconfig_subarg_cmp(str, "dr_mode",
135 modes[j])) {
136 mode_idx = j;
137 break;
138 }
139 }
140
141 for (j = 0; j < ARRAY_SIZE(phys); j++) {
142 if (hwconfig_subarg_cmp(str, "phy_type",
143 phys[j])) {
144 phy_idx = j;
145 break;
146 }
147 }
148
149 if (mode_idx < 0 && phy_idx < 0) {
150 printf("WARNING: invalid phy or mode\n");
151 return;
152 }
153
154 if (mode_idx > -1)
155 dr_mode_type = modes[mode_idx];
156
157 if (phy_idx > -1)
158 dr_phy_type = phys[phy_idx];
159 }
160
161 if (has_dual_phy())
162 dr_phy_type = phys[2];
163
164 usb_mode_off = fdt_fixup_usb_mode_phy_type(blob,
165 dr_mode_type, NULL,
166 usb_mode_off);
167
168 if (usb_mode_off < 0)
169 return;
170
171 usb_phy_off = fdt_fixup_usb_mode_phy_type(blob,
172 NULL, dr_phy_type,
173 usb_phy_off);
174
175 if (usb_phy_off < 0)
176 return;
177
178 if (has_erratum_a006261()) {
179 usb_erratum_a006261_off = fdt_fixup_usb_erratum
180 (blob,
181 "fsl,usb-erratum-a006261",
182 usb_erratum_a006261_off);
183 if (usb_erratum_a006261_off < 0)
184 return;
185 }
186
187 if (has_erratum_a007075()) {
188 usb_erratum_a007075_off = fdt_fixup_usb_erratum
189 (blob,
190 "fsl,usb-erratum-a007075",
191 usb_erratum_a007075_off);
192 if (usb_erratum_a007075_off < 0)
193 return;
194 }
195
196 if (has_erratum_a007792()) {
197 usb_erratum_a007792_off = fdt_fixup_usb_erratum
198 (blob,
199 "fsl,usb-erratum-a007792",
200 usb_erratum_a007792_off);
201 if (usb_erratum_a007792_off < 0)
202 return;
203 }
204 if (has_erratum_a005697()) {
205 usb_erratum_a005697_off = fdt_fixup_usb_erratum
206 (blob,
207 "fsl,usb-erratum-a005697",
208 usb_erratum_a005697_off);
209 if (usb_erratum_a005697_off < 0)
210 return;
211 }
212 }
213}