]> git.sur5r.net Git - u-boot/blob - drivers/usb/common/fsl-dt-fixup.c
eb13f12c23380e9a89e6d7e1107cd362195b311f
[u-boot] / drivers / usb / common / fsl-dt-fixup.c
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
22 static const char *fdt_usb_get_node_type(void *blob, int start_offset,
23                                          int *node_offset)
24 {
25         const char *compat_dr = "fsl-usb2-dr";
26         const char *compat_mph = "fsl-usb2-mph";
27         const char *node_type = NULL;
28
29         *node_offset = fdt_node_offset_by_compatible(blob, start_offset,
30                                                      compat_mph);
31         if (*node_offset < 0) {
32                 *node_offset = fdt_node_offset_by_compatible(blob,
33                                                              start_offset,
34                                                              compat_dr);
35                 if (*node_offset < 0) {
36                         printf("ERROR: could not find compatible node: %s\n",
37                                fdt_strerror(*node_offset));
38                 } else {
39                         node_type = compat_dr;
40                 }
41         } else {
42                 node_type = compat_mph;
43         }
44
45         return node_type;
46 }
47
48 static 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
80 static 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
99 void 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 }