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