usb: ehci-mx6: Fix aarch64 build warnings
[platform/kernel/u-boot.git] / drivers / usb / host / ehci-mx6.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4  * Copyright (C) 2010 Freescale Semiconductor, Inc.
5  */
6
7 #include <common.h>
8 #include <clk.h>
9 #include <log.h>
10 #include <usb.h>
11 #include <errno.h>
12 #include <wait_bit.h>
13 #include <asm/global_data.h>
14 #include <linux/compiler.h>
15 #include <linux/delay.h>
16 #include <usb/ehci-ci.h>
17 #include <asm/io.h>
18 #include <asm/arch/imx-regs.h>
19 #include <asm/arch/clock.h>
20 #include <asm/mach-imx/iomux-v3.h>
21 #include <asm/mach-imx/sys_proto.h>
22 #include <dm.h>
23 #include <asm/mach-types.h>
24 #include <power/regulator.h>
25 #include <linux/usb/otg.h>
26
27 #include "ehci.h"
28
29 DECLARE_GLOBAL_DATA_PTR;
30
31 #define USB_OTGREGS_OFFSET      0x000
32 #define USB_H1REGS_OFFSET       0x200
33 #define USB_H2REGS_OFFSET       0x400
34 #define USB_H3REGS_OFFSET       0x600
35 #define USB_OTHERREGS_OFFSET    0x800
36
37 #define USB_H1_CTRL_OFFSET      0x04
38
39 #define USBPHY_CTRL                             0x00000030
40 #define USBPHY_CTRL_SET                         0x00000034
41 #define USBPHY_CTRL_CLR                         0x00000038
42 #define USBPHY_CTRL_TOG                         0x0000003c
43
44 #define USBPHY_PWD                              0x00000000
45 #define USBPHY_CTRL_SFTRST                      0x80000000
46 #define USBPHY_CTRL_CLKGATE                     0x40000000
47 #define USBPHY_CTRL_ENUTMILEVEL3                0x00008000
48 #define USBPHY_CTRL_ENUTMILEVEL2                0x00004000
49 #define USBPHY_CTRL_OTG_ID                      0x08000000
50
51 #define ANADIG_USB2_CHRG_DETECT_EN_B            0x00100000
52 #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B      0x00080000
53
54 #define ANADIG_USB2_PLL_480_CTRL_BYPASS         0x00010000
55 #define ANADIG_USB2_PLL_480_CTRL_ENABLE         0x00002000
56 #define ANADIG_USB2_PLL_480_CTRL_POWER          0x00001000
57 #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS    0x00000040
58
59 #define USBNC_OFFSET            0x200
60 #define USBNC_PHY_STATUS_OFFSET 0x23C
61 #define USBNC_PHYSTATUS_ID_DIG  (1 << 4) /* otg_id status */
62 #define USBNC_PHYCFG2_ACAENB    (1 << 4) /* otg_id detection enable */
63 #define UCTRL_PWR_POL           (1 << 9) /* OTG Polarity of Power Pin */
64 #define UCTRL_OVER_CUR_POL      (1 << 8) /* OTG Polarity of Overcurrent */
65 #define UCTRL_OVER_CUR_DIS      (1 << 7) /* Disable OTG Overcurrent Detection */
66
67 /* USBCMD */
68 #define UCMD_RUN_STOP           (1 << 0) /* controller run/stop */
69 #define UCMD_RESET              (1 << 1) /* controller reset */
70
71 /* If this is not defined, assume MX6/MX7/MX8M SoC default */
72 #ifndef CONFIG_MXC_USB_PORTSC
73 #define CONFIG_MXC_USB_PORTSC   (PORT_PTS_UTMI | PORT_PTS_PTW)
74 #endif
75
76 /* Base address for this IP block is 0x02184800 */
77 struct usbnc_regs {
78         u32 ctrl[4]; /* otg/host1-3 */
79         u32 uh2_hsic_ctrl;
80         u32 uh3_hsic_ctrl;
81         u32 otg_phy_ctrl_0;
82         u32 uh1_phy_ctrl_0;
83         u32 reserve1[4];
84         u32 phy_cfg1;
85         u32 phy_cfg2;
86         u32 reserve2;
87         u32 phy_status;
88         u32 reserve3[4];
89         u32 adp_cfg1;
90         u32 adp_cfg2;
91         u32 adp_status;
92 };
93
94 #if defined(CONFIG_MX6) && !defined(CONFIG_PHY)
95 static void usb_power_config_mx6(struct anatop_regs __iomem *anatop,
96                                  int anatop_bits_index)
97 {
98         void __iomem *chrg_detect;
99         void __iomem *pll_480_ctrl_clr;
100         void __iomem *pll_480_ctrl_set;
101
102         if (!is_mx6())
103                 return;
104
105         switch (anatop_bits_index) {
106         case 0:
107                 chrg_detect = &anatop->usb1_chrg_detect;
108                 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
109                 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
110                 break;
111         case 1:
112                 chrg_detect = &anatop->usb2_chrg_detect;
113                 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
114                 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
115                 break;
116         default:
117                 return;
118         }
119         /*
120          * Some phy and power's special controls
121          * 1. The external charger detector needs to be disabled
122          * or the signal at DP will be poor
123          * 2. The PLL's power and output to usb
124          * is totally controlled by IC, so the Software only needs
125          * to enable them at initializtion.
126          */
127         writel(ANADIG_USB2_CHRG_DETECT_EN_B |
128                      ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
129                      chrg_detect);
130
131         writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
132                      pll_480_ctrl_clr);
133
134         writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
135                      ANADIG_USB2_PLL_480_CTRL_POWER |
136                      ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
137                      pll_480_ctrl_set);
138 }
139 #else
140 static void __maybe_unused
141 usb_power_config_mx6(void *anatop, int anatop_bits_index) { }
142 #endif
143
144 #if defined(CONFIG_MX7) && !defined(CONFIG_PHY)
145 static void usb_power_config_mx7(struct usbnc_regs *usbnc)
146 {
147         void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
148
149         if (!is_mx7())
150                 return;
151
152         /*
153          * Clear the ACAENB to enable usb_otg_id detection,
154          * otherwise it is the ACA detection enabled.
155          */
156         clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
157 }
158 #else
159 static void __maybe_unused
160 usb_power_config_mx7(void *usbnc) { }
161 #endif
162
163 #if defined(CONFIG_MX7ULP) && !defined(CONFIG_PHY)
164 static void usb_power_config_mx7ulp(struct usbphy_regs __iomem *usbphy)
165 {
166         if (!is_mx7ulp())
167                 return;
168
169         writel(ANADIG_USB2_CHRG_DETECT_EN_B |
170                ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
171                &usbphy->usb1_chrg_detect);
172
173         scg_enable_usb_pll(true);
174 }
175 #else
176 static void __maybe_unused
177 usb_power_config_mx7ulp(void *usbphy) { }
178 #endif
179
180 #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
181 static const unsigned phy_bases[] = {
182         USB_PHY0_BASE_ADDR,
183 #if defined(USB_PHY1_BASE_ADDR)
184         USB_PHY1_BASE_ADDR,
185 #endif
186 };
187
188 #if !defined(CONFIG_PHY)
189 static void usb_internal_phy_clock_gate(void __iomem *phy_reg, int on)
190 {
191         phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
192         writel(USBPHY_CTRL_CLKGATE, phy_reg);
193 }
194
195 /* Return 0 : host node, <>0 : device mode */
196 static int usb_phy_enable(struct usb_ehci *ehci, void __iomem *phy_reg)
197 {
198         void __iomem *phy_ctrl;
199         void __iomem *usb_cmd;
200         int ret;
201
202         phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
203         usb_cmd = (void __iomem *)&ehci->usbcmd;
204
205         /* Stop then Reset */
206         clrbits_le32(usb_cmd, UCMD_RUN_STOP);
207         ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
208         if (ret)
209                 return ret;
210
211         setbits_le32(usb_cmd, UCMD_RESET);
212         ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
213         if (ret)
214                 return ret;
215
216         /* Reset USBPHY module */
217         setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
218         udelay(10);
219
220         /* Remove CLKGATE and SFTRST */
221         clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
222         udelay(10);
223
224         /* Power up the PHY */
225         writel(0, phy_reg + USBPHY_PWD);
226         /* enable FS/LS device */
227         setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
228                         USBPHY_CTRL_ENUTMILEVEL3);
229
230         return 0;
231 }
232 #endif
233
234 int usb_phy_mode(int port)
235 {
236         void __iomem *phy_reg;
237         void __iomem *phy_ctrl;
238         u32 val;
239
240         phy_reg = (void __iomem *)phy_bases[port];
241         phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
242
243         val = readl(phy_ctrl);
244
245         if (val & USBPHY_CTRL_OTG_ID)
246                 return USB_INIT_DEVICE;
247         else
248                 return USB_INIT_HOST;
249 }
250
251 #elif defined(CONFIG_MX7)
252 int usb_phy_mode(int port)
253 {
254         struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
255                         (0x10000 * port) + USBNC_OFFSET);
256         void __iomem *status = (void __iomem *)(&usbnc->phy_status);
257         u32 val;
258
259         val = readl(status);
260
261         if (val & USBNC_PHYSTATUS_ID_DIG)
262                 return USB_INIT_DEVICE;
263         else
264                 return USB_INIT_HOST;
265 }
266 #endif
267
268 static void usb_oc_config(struct usbnc_regs *usbnc, int index)
269 {
270         void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
271
272 #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
273         /* mx6qarm2 seems to required a different setting*/
274         clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
275 #else
276         setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
277 #endif
278
279         setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
280
281         /* Set power polarity to high active */
282 #ifdef CONFIG_MXC_USB_OTG_HACTIVE
283         setbits_le32(ctrl, UCTRL_PWR_POL);
284 #else
285         clrbits_le32(ctrl, UCTRL_PWR_POL);
286 #endif
287 }
288
289 #if !CONFIG_IS_ENABLED(DM_USB)
290 /**
291  * board_usb_phy_mode - override usb phy mode
292  * @port:       usb host/otg port
293  *
294  * Target board specific, override usb_phy_mode.
295  * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
296  * left disconnected in this case usb_phy_mode will not be able to identify
297  * the phy mode that usb port is used.
298  * Machine file overrides board_usb_phy_mode.
299  *
300  * Return: USB_INIT_DEVICE or USB_INIT_HOST
301  */
302 int __weak board_usb_phy_mode(int port)
303 {
304         return usb_phy_mode(port);
305 }
306
307 /**
308  * board_ehci_hcd_init - set usb vbus voltage
309  * @port:      usb otg port
310  *
311  * Target board specific, setup iomux pad to setup supply vbus voltage
312  * for usb otg port. Machine board file overrides board_ehci_hcd_init
313  *
314  * Return: 0 Success
315  */
316 int __weak board_ehci_hcd_init(int port)
317 {
318         return 0;
319 }
320
321 /**
322  * board_ehci_power - enables/disables usb vbus voltage
323  * @port:      usb otg port
324  * @on:        on/off vbus voltage
325  *
326  * Enables/disables supply vbus voltage for usb otg port.
327  * Machine board file overrides board_ehci_power
328  *
329  * Return: 0 Success
330  */
331 int __weak board_ehci_power(int port, int on)
332 {
333         return 0;
334 }
335
336 int ehci_hcd_init(int index, enum usb_init_type init,
337                 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
338 {
339         enum usb_init_type type;
340 #if defined(CONFIG_MX6)
341         u32 controller_spacing = 0x200;
342         struct anatop_regs __iomem *anatop =
343                 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
344         struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
345                         USB_OTHERREGS_OFFSET);
346 #elif defined(CONFIG_MX7)
347         u32 controller_spacing = 0x10000;
348         struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
349                         (0x10000 * index) + USBNC_OFFSET);
350 #elif defined(CONFIG_MX7ULP)
351         u32 controller_spacing = 0x10000;
352         struct usbphy_regs __iomem *usbphy =
353                 (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR;
354         struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
355                         (0x10000 * index) + USBNC_OFFSET);
356 #endif
357         struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
358                 (controller_spacing * index));
359         int ret;
360
361         if (index > 3)
362                 return -EINVAL;
363
364         if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
365                 if (usb_fused((ulong)ehci)) {
366                         printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
367                                (ulong)ehci);
368                         return  -ENODEV;
369                 }
370         }
371
372         enable_usboh3_clk(1);
373         mdelay(1);
374
375         /* Do board specific initialization */
376         ret = board_ehci_hcd_init(index);
377         if (ret) {
378                 enable_usboh3_clk(0);
379                 return ret;
380         }
381
382 #if defined(CONFIG_MX6)
383         usb_power_config_mx6(anatop, index);
384 #elif defined (CONFIG_MX7)
385         usb_power_config_mx7(usbnc);
386 #elif defined (CONFIG_MX7ULP)
387         usb_power_config_mx7ulp(usbphy);
388 #endif
389
390         usb_oc_config(usbnc, index);
391
392 #if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
393         if (index < ARRAY_SIZE(phy_bases)) {
394                 usb_internal_phy_clock_gate((void __iomem *)phy_bases[index], 1);
395                 usb_phy_enable(ehci, (void __iomem *)phy_bases[index]);
396         }
397 #endif
398
399         type = board_usb_phy_mode(index);
400
401         if (hccr && hcor) {
402                 *hccr = (struct ehci_hccr *)((uintptr_t)&ehci->caplength);
403                 *hcor = (struct ehci_hcor *)((uintptr_t)*hccr +
404                                 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
405         }
406
407         if ((type == init) || (type == USB_INIT_DEVICE))
408                 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
409         if (type != init)
410                 return -ENODEV;
411         if (type == USB_INIT_DEVICE)
412                 return 0;
413
414         setbits_le32(&ehci->usbmode, CM_HOST);
415         writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
416         setbits_le32(&ehci->portsc, USB_EN);
417
418         mdelay(10);
419
420         return 0;
421 }
422
423 int ehci_hcd_stop(int index)
424 {
425         return 0;
426 }
427 #else
428 struct ehci_mx6_priv_data {
429         struct ehci_ctrl ctrl;
430         struct usb_ehci *ehci;
431         struct udevice *vbus_supply;
432         struct clk clk;
433         struct phy phy;
434         enum usb_init_type init_type;
435         int portnr;
436         void __iomem *phy_addr;
437         void __iomem *misc_addr;
438         void __iomem *anatop_addr;
439 };
440
441 static int mx6_init_after_reset(struct ehci_ctrl *dev)
442 {
443         struct ehci_mx6_priv_data *priv = dev->priv;
444         enum usb_init_type type = priv->init_type;
445         struct usb_ehci *ehci = priv->ehci;
446
447 #if !defined(CONFIG_PHY)
448         usb_power_config_mx6(priv->anatop_addr, priv->portnr);
449         usb_power_config_mx7(priv->misc_addr);
450         usb_power_config_mx7ulp(priv->phy_addr);
451 #endif
452
453         usb_oc_config(priv->misc_addr, priv->portnr);
454
455 #if !defined(CONFIG_PHY) && (defined(CONFIG_MX6) || defined(CONFIG_MX7ULP))
456         usb_internal_phy_clock_gate(priv->phy_addr, 1);
457         usb_phy_enable(ehci, priv->phy_addr);
458 #endif
459
460 #if CONFIG_IS_ENABLED(DM_REGULATOR)
461         if (priv->vbus_supply) {
462                 int ret;
463                 ret = regulator_set_enable(priv->vbus_supply,
464                                            (type == USB_INIT_DEVICE) ?
465                                            false : true);
466                 if (ret && ret != -ENOSYS) {
467                         printf("Error enabling VBUS supply (ret=%i)\n", ret);
468                         return ret;
469                 }
470         }
471 #endif
472
473         if (type == USB_INIT_DEVICE)
474                 return 0;
475
476         setbits_le32(&ehci->usbmode, CM_HOST);
477         writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
478         setbits_le32(&ehci->portsc, USB_EN);
479
480         mdelay(10);
481
482         return 0;
483 }
484
485 static const struct ehci_ops mx6_ehci_ops = {
486         .init_after_reset = mx6_init_after_reset
487 };
488
489 static int ehci_usb_phy_mode(struct udevice *dev)
490 {
491         struct usb_plat *plat = dev_get_plat(dev);
492         void *__iomem addr = dev_read_addr_ptr(dev);
493         void *__iomem phy_ctrl, *__iomem phy_status;
494         const void *blob = gd->fdt_blob;
495         int offset = dev_of_offset(dev), phy_off;
496         u32 val;
497
498         /*
499          * About fsl,usbphy, Refer to
500          * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
501          */
502         if (is_mx6() || is_mx7ulp()) {
503                 phy_off = fdtdec_lookup_phandle(blob,
504                                                 offset,
505                                                 "fsl,usbphy");
506                 if (phy_off < 0)
507                         return -EINVAL;
508
509                 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
510                                                        "reg");
511                 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
512                         return -EINVAL;
513
514                 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
515                 val = readl(phy_ctrl);
516
517                 if (val & USBPHY_CTRL_OTG_ID)
518                         plat->init_type = USB_INIT_DEVICE;
519                 else
520                         plat->init_type = USB_INIT_HOST;
521         } else if (is_mx7()) {
522                 phy_status = (void __iomem *)(addr +
523                                               USBNC_PHY_STATUS_OFFSET);
524                 val = readl(phy_status);
525
526                 if (val & USBNC_PHYSTATUS_ID_DIG)
527                         plat->init_type = USB_INIT_DEVICE;
528                 else
529                         plat->init_type = USB_INIT_HOST;
530         } else {
531                 return -EINVAL;
532         }
533
534         return 0;
535 }
536
537 static int ehci_usb_of_to_plat(struct udevice *dev)
538 {
539         struct usb_plat *plat = dev_get_plat(dev);
540         enum usb_dr_mode dr_mode;
541
542         dr_mode = usb_get_dr_mode(dev_ofnode(dev));
543
544         switch (dr_mode) {
545         case USB_DR_MODE_HOST:
546                 plat->init_type = USB_INIT_HOST;
547                 break;
548         case USB_DR_MODE_PERIPHERAL:
549                 plat->init_type = USB_INIT_DEVICE;
550                 break;
551         case USB_DR_MODE_OTG:
552         case USB_DR_MODE_UNKNOWN:
553                 return ehci_usb_phy_mode(dev);
554         };
555
556         return 0;
557 }
558
559 static int mx6_parse_dt_addrs(struct udevice *dev)
560 {
561         struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
562         int phy_off, misc_off;
563         const void *blob = gd->fdt_blob;
564         int offset = dev_of_offset(dev);
565         void *__iomem addr;
566         int ret, devnump;
567
568         phy_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbphy");
569         if (phy_off < 0) {
570                 phy_off = fdtdec_lookup_phandle(blob, offset, "phys");
571                 if (phy_off < 0)
572                         return -EINVAL;
573         }
574
575         ret = fdtdec_get_alias_seq(blob, dev->uclass->uc_drv->name,
576                                    phy_off, &devnump);
577         if (ret < 0)
578                 return ret;
579
580         misc_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbmisc");
581         if (misc_off < 0)
582                 return -EINVAL;
583
584         addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, "reg");
585         if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
586                 return -EINVAL;
587
588         priv->phy_addr = addr;
589         priv->portnr = devnump;
590
591         addr = (void __iomem *)fdtdec_get_addr(blob, misc_off, "reg");
592         if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
593                 return -EINVAL;
594
595         priv->misc_addr = addr;
596
597 #if !defined(CONFIG_PHY) && defined(CONFIG_MX6)
598         int anatop_off;
599
600         /* Resolve ANATOP offset through USB PHY node */
601         anatop_off = fdtdec_lookup_phandle(blob, phy_off, "fsl,anatop");
602         if (anatop_off < 0)
603                 return -EINVAL;
604
605         addr = (void __iomem *)fdtdec_get_addr(blob, anatop_off, "reg");
606         if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
607                 return -EINVAL;
608
609         priv->anatop_addr = addr;
610 #endif
611         return 0;
612 }
613
614 static int ehci_usb_probe(struct udevice *dev)
615 {
616         struct usb_plat *plat = dev_get_plat(dev);
617         struct usb_ehci *ehci = dev_read_addr_ptr(dev);
618         struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
619         enum usb_init_type type = plat->init_type;
620         struct ehci_hccr *hccr;
621         struct ehci_hcor *hcor;
622         int ret;
623
624         if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
625                 if (usb_fused((ulong)ehci)) {
626                         printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
627                                (ulong)ehci);
628                         return -ENODEV;
629                 }
630         }
631
632         ret = mx6_parse_dt_addrs(dev);
633         if (ret)
634                 return ret;
635
636         priv->ehci = ehci;
637         priv->init_type = type;
638
639 #if CONFIG_IS_ENABLED(CLK)
640         ret = clk_get_by_index(dev, 0, &priv->clk);
641         if (ret < 0)
642                 return ret;
643
644         ret = clk_enable(&priv->clk);
645         if (ret)
646                 return ret;
647 #else
648         /* Compatibility with DM_USB and !CLK */
649         enable_usboh3_clk(1);
650         mdelay(1);
651 #endif
652
653 #if CONFIG_IS_ENABLED(DM_REGULATOR)
654         ret = device_get_supply_regulator(dev, "vbus-supply",
655                                           &priv->vbus_supply);
656         if (ret)
657                 debug("%s: No vbus supply\n", dev->name);
658 #endif
659
660 #if !defined(CONFIG_PHY)
661         usb_power_config_mx6(priv->anatop_addr, priv->portnr);
662         usb_power_config_mx7(priv->misc_addr);
663         usb_power_config_mx7ulp(priv->phy_addr);
664 #endif
665
666         usb_oc_config(priv->misc_addr, priv->portnr);
667
668 #if !defined(CONFIG_PHY) && (defined(CONFIG_MX6) || defined(CONFIG_MX7ULP))
669         usb_internal_phy_clock_gate(priv->phy_addr, 1);
670         usb_phy_enable(ehci, priv->phy_addr);
671 #endif
672
673 #if CONFIG_IS_ENABLED(DM_REGULATOR)
674         if (priv->vbus_supply) {
675                 ret = regulator_set_enable(priv->vbus_supply,
676                                            (type == USB_INIT_DEVICE) ?
677                                            false : true);
678                 if (ret && ret != -ENOSYS) {
679                         printf("Error enabling VBUS supply (ret=%i)\n", ret);
680                         goto err_clk;
681                 }
682         }
683 #endif
684
685         if (priv->init_type == USB_INIT_HOST) {
686                 setbits_le32(&ehci->usbmode, CM_HOST);
687                 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
688                 setbits_le32(&ehci->portsc, USB_EN);
689         }
690
691         mdelay(10);
692
693 #if defined(CONFIG_PHY)
694         ret = ehci_setup_phy(dev, &priv->phy, 0);
695         if (ret)
696                 goto err_regulator;
697 #endif
698
699         hccr = (struct ehci_hccr *)((uintptr_t)&ehci->caplength);
700         hcor = (struct ehci_hcor *)((uintptr_t)hccr +
701                         HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
702
703         ret = ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
704         if (ret)
705                 goto err_phy;
706
707         return ret;
708
709 err_phy:
710 #if defined(CONFIG_PHY)
711         ehci_shutdown_phy(dev, &priv->phy);
712 err_regulator:
713 #endif
714 #if CONFIG_IS_ENABLED(DM_REGULATOR)
715         if (priv->vbus_supply)
716                 regulator_set_enable(priv->vbus_supply, false);
717 err_clk:
718 #endif
719 #if CONFIG_IS_ENABLED(CLK)
720         clk_disable(&priv->clk);
721 #else
722         /* Compatibility with DM_USB and !CLK */
723         enable_usboh3_clk(0);
724 #endif
725         return ret;
726 }
727
728 int ehci_usb_remove(struct udevice *dev)
729 {
730         struct ehci_mx6_priv_data *priv __maybe_unused = dev_get_priv(dev);
731
732         ehci_deregister(dev);
733
734 #if defined(CONFIG_PHY)
735         ehci_shutdown_phy(dev, &priv->phy);
736 #endif
737
738 #if CONFIG_IS_ENABLED(DM_REGULATOR)
739         if (priv->vbus_supply)
740                 regulator_set_enable(priv->vbus_supply, false);
741 #endif
742
743 #if CONFIG_IS_ENABLED(CLK)
744         clk_disable(&priv->clk);
745 #endif
746
747         return 0;
748 }
749
750 static const struct udevice_id mx6_usb_ids[] = {
751         { .compatible = "fsl,imx27-usb" },
752         { .compatible = "fsl,imx7d-usb" },
753         { }
754 };
755
756 U_BOOT_DRIVER(usb_mx6) = {
757         .name   = "ehci_mx6",
758         .id     = UCLASS_USB,
759         .of_match = mx6_usb_ids,
760         .of_to_plat = ehci_usb_of_to_plat,
761         .probe  = ehci_usb_probe,
762         .remove = ehci_usb_remove,
763         .ops    = &ehci_usb_ops,
764         .plat_auto      = sizeof(struct usb_plat),
765         .priv_auto      = sizeof(struct ehci_mx6_priv_data),
766         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
767 };
768 #endif