blob: 2c8fc3c4b150acc89244565dffecd726ce7abcc2 [file] [log] [blame]
Wolfgang Grandegger1859b702012-02-08 22:33:25 +00001/*
2 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
3 * Copyright (C) 2010 Freescale Semiconductor, Inc.
4 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Grandegger1859b702012-02-08 22:33:25 +00006 */
7
8#include <common.h>
9#include <usb.h>
10#include <errno.h>
Mateusz Kulikowski4073b832016-01-23 11:54:32 +010011#include <wait_bit.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000012#include <linux/compiler.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020013#include <usb/ehci-ci.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000014#include <asm/io.h>
15#include <asm/arch/imx-regs.h>
16#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020017#include <asm/mach-imx/iomux-v3.h>
18#include <asm/mach-imx/sys_proto.h>
Peng Fan5c363c12016-06-17 14:19:27 +080019#include <dm.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060020#include <asm/mach-types.h>
Peng Fan13351332016-12-22 17:06:43 +080021#include <power/regulator.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000022
23#include "ehci.h"
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000024
Peng Fan9e3eab32016-12-22 17:06:42 +080025DECLARE_GLOBAL_DATA_PTR;
26
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000027#define USB_OTGREGS_OFFSET 0x000
28#define USB_H1REGS_OFFSET 0x200
29#define USB_H2REGS_OFFSET 0x400
30#define USB_H3REGS_OFFSET 0x600
31#define USB_OTHERREGS_OFFSET 0x800
32
33#define USB_H1_CTRL_OFFSET 0x04
34
35#define USBPHY_CTRL 0x00000030
36#define USBPHY_CTRL_SET 0x00000034
37#define USBPHY_CTRL_CLR 0x00000038
38#define USBPHY_CTRL_TOG 0x0000003c
39
40#define USBPHY_PWD 0x00000000
41#define USBPHY_CTRL_SFTRST 0x80000000
42#define USBPHY_CTRL_CLKGATE 0x40000000
43#define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
44#define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
Troy Kiskyed72a9e2013-10-10 15:27:59 -070045#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000046
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000047#define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
48#define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
49
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000050#define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
51#define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
52#define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
53#define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
54
Adrian Alonsof31599f2015-08-06 15:43:17 -050055#define USBNC_OFFSET 0x200
Peng Fan9e3eab32016-12-22 17:06:42 +080056#define USBNC_PHY_STATUS_OFFSET 0x23C
Adrian Alonsof31599f2015-08-06 15:43:17 -050057#define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
58#define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
Stefan Agner475cf912016-07-13 00:25:37 -070059#define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000060#define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
61#define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
62
63/* USBCMD */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000064#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
65#define UCMD_RESET (1 << 1) /* controller reset */
66
Adrian Alonsof31599f2015-08-06 15:43:17 -050067#if defined(CONFIG_MX6)
Troy Kiskyed72a9e2013-10-10 15:27:59 -070068static const unsigned phy_bases[] = {
69 USB_PHY0_BASE_ADDR,
70 USB_PHY1_BASE_ADDR,
71};
72
73static void usb_internal_phy_clock_gate(int index, int on)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000074{
Troy Kiskyed72a9e2013-10-10 15:27:59 -070075 void __iomem *phy_reg;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000076
Troy Kiskyed72a9e2013-10-10 15:27:59 -070077 if (index >= ARRAY_SIZE(phy_bases))
78 return;
79
80 phy_reg = (void __iomem *)phy_bases[index];
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000081 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
Adrian Alonsoaee79b42015-08-06 15:43:15 -050082 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000083}
84
Troy Kiskyed72a9e2013-10-10 15:27:59 -070085static void usb_power_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000086{
Wolfgang Grandegger5d113ca2012-05-02 04:36:39 +000087 struct anatop_regs __iomem *anatop =
88 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Troy Kiskyed72a9e2013-10-10 15:27:59 -070089 void __iomem *chrg_detect;
90 void __iomem *pll_480_ctrl_clr;
91 void __iomem *pll_480_ctrl_set;
92
93 switch (index) {
94 case 0:
95 chrg_detect = &anatop->usb1_chrg_detect;
96 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
97 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
98 break;
99 case 1:
100 chrg_detect = &anatop->usb2_chrg_detect;
101 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
102 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
103 break;
104 default:
105 return;
106 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000107 /*
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700108 * Some phy and power's special controls
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000109 * 1. The external charger detector needs to be disabled
110 * or the signal at DP will be poor
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700111 * 2. The PLL's power and output to usb
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000112 * is totally controlled by IC, so the Software only needs
113 * to enable them at initializtion.
114 */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500115 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000116 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700117 chrg_detect);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000118
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500119 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700120 pll_480_ctrl_clr);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000121
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500122 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000123 ANADIG_USB2_PLL_480_CTRL_POWER |
124 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700125 pll_480_ctrl_set);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000126}
127
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700128/* Return 0 : host node, <>0 : device mode */
129static int usb_phy_enable(int index, struct usb_ehci *ehci)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000130{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700131 void __iomem *phy_reg;
132 void __iomem *phy_ctrl;
133 void __iomem *usb_cmd;
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500134 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000135
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700136 if (index >= ARRAY_SIZE(phy_bases))
137 return 0;
138
139 phy_reg = (void __iomem *)phy_bases[index];
140 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
141 usb_cmd = (void __iomem *)&ehci->usbcmd;
142
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000143 /* Stop then Reset */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500144 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100145 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500146 if (ret)
147 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000148
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500149 setbits_le32(usb_cmd, UCMD_RESET);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100150 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500151 if (ret)
152 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000153
154 /* Reset USBPHY module */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500155 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000156 udelay(10);
157
158 /* Remove CLKGATE and SFTRST */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500159 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000160 udelay(10);
161
162 /* Power up the PHY */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500163 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000164 /* enable FS/LS device */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500165 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
166 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000167
Peng Fan220402e2014-11-10 08:50:39 +0800168 return 0;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000169}
170
Adrian Alonsof31599f2015-08-06 15:43:17 -0500171int usb_phy_mode(int port)
172{
173 void __iomem *phy_reg;
174 void __iomem *phy_ctrl;
175 u32 val;
176
177 phy_reg = (void __iomem *)phy_bases[port];
178 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
179
180 val = readl(phy_ctrl);
181
182 if (val & USBPHY_CTRL_OTG_ID)
183 return USB_INIT_DEVICE;
184 else
185 return USB_INIT_HOST;
186}
187
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700188/* Base address for this IP block is 0x02184800 */
189struct usbnc_regs {
190 u32 ctrl[4]; /* otg/host1-3 */
191 u32 uh2_hsic_ctrl;
192 u32 uh3_hsic_ctrl;
193 u32 otg_phy_ctrl_0;
194 u32 uh1_phy_ctrl_0;
195};
Adrian Alonsof31599f2015-08-06 15:43:17 -0500196#elif defined(CONFIG_MX7)
197struct usbnc_regs {
198 u32 ctrl1;
199 u32 ctrl2;
200 u32 reserve1[10];
201 u32 phy_cfg1;
202 u32 phy_cfg2;
Peng Fan13186cf2016-06-20 09:43:08 +0800203 u32 reserve2;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500204 u32 phy_status;
Peng Fan13186cf2016-06-20 09:43:08 +0800205 u32 reserve3[4];
Adrian Alonsof31599f2015-08-06 15:43:17 -0500206 u32 adp_cfg1;
207 u32 adp_cfg2;
208 u32 adp_status;
209};
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700210
Adrian Alonsof31599f2015-08-06 15:43:17 -0500211static void usb_power_config(int index)
212{
213 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
214 (0x10000 * index) + USBNC_OFFSET);
215 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
Stefan Agner475cf912016-07-13 00:25:37 -0700216 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
Adrian Alonsof31599f2015-08-06 15:43:17 -0500217
Peng Fan928d3d02016-06-20 09:43:09 +0800218 /*
219 * Clear the ACAENB to enable usb_otg_id detection,
220 * otherwise it is the ACA detection enabled.
221 */
222 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
Stefan Agner475cf912016-07-13 00:25:37 -0700223
224 /* Set power polarity to high active */
Stefan Agner8652ce92016-07-13 00:25:38 -0700225#ifdef CONFIG_MXC_USB_OTG_HACTIVE
Stefan Agner475cf912016-07-13 00:25:37 -0700226 setbits_le32(ctrl, UCTRL_PWR_POL);
Stefan Agner8652ce92016-07-13 00:25:38 -0700227#else
228 clrbits_le32(ctrl, UCTRL_PWR_POL);
229#endif
Adrian Alonsof31599f2015-08-06 15:43:17 -0500230}
231
232int usb_phy_mode(int port)
233{
234 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
235 (0x10000 * port) + USBNC_OFFSET);
236 void __iomem *status = (void __iomem *)(&usbnc->phy_status);
237 u32 val;
238
239 val = readl(status);
240
241 if (val & USBNC_PHYSTATUS_ID_DIG)
242 return USB_INIT_DEVICE;
243 else
244 return USB_INIT_HOST;
245}
246#endif
247
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700248static void usb_oc_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000249{
Adrian Alonsof31599f2015-08-06 15:43:17 -0500250#if defined(CONFIG_MX6)
Ye.Lif93453a2014-09-15 17:23:14 +0800251 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700252 USB_OTHERREGS_OFFSET);
253 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
Adrian Alonsof31599f2015-08-06 15:43:17 -0500254#elif defined(CONFIG_MX7)
255 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
256 (0x10000 * index) + USBNC_OFFSET);
257 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
258#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000259
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000260#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
261 /* mx6qarm2 seems to required a different setting*/
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500262 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000263#else
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500264 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000265#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000266
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500267 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
Peng Fan220402e2014-11-10 08:50:39 +0800268}
269
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500270/**
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700271 * board_usb_phy_mode - override usb phy mode
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500272 * @port: usb host/otg port
273 *
274 * Target board specific, override usb_phy_mode.
275 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
276 * left disconnected in this case usb_phy_mode will not be able to identify
277 * the phy mode that usb port is used.
278 * Machine file overrides board_usb_phy_mode.
279 *
280 * Return: USB_INIT_DEVICE or USB_INIT_HOST
281 */
Peng Fan220402e2014-11-10 08:50:39 +0800282int __weak board_usb_phy_mode(int port)
283{
284 return usb_phy_mode(port);
285}
286
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500287/**
288 * board_ehci_hcd_init - set usb vbus voltage
289 * @port: usb otg port
290 *
291 * Target board specific, setup iomux pad to setup supply vbus voltage
292 * for usb otg port. Machine board file overrides board_ehci_hcd_init
293 *
294 * Return: 0 Success
295 */
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000296int __weak board_ehci_hcd_init(int port)
297{
298 return 0;
299}
300
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500301/**
302 * board_ehci_power - enables/disables usb vbus voltage
303 * @port: usb otg port
304 * @on: on/off vbus voltage
305 *
306 * Enables/disables supply vbus voltage for usb otg port.
307 * Machine board file overrides board_ehci_power
308 *
309 * Return: 0 Success
310 */
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700311int __weak board_ehci_power(int port, int on)
312{
313 return 0;
314}
315
Peng Fan5c363c12016-06-17 14:19:27 +0800316int ehci_mx6_common_init(struct usb_ehci *ehci, int index)
317{
318 int ret;
319
320 enable_usboh3_clk(1);
321 mdelay(1);
322
323 /* Do board specific initialization */
324 ret = board_ehci_hcd_init(index);
325 if (ret)
326 return ret;
327
328 usb_power_config(index);
329 usb_oc_config(index);
330
331#if defined(CONFIG_MX6)
332 usb_internal_phy_clock_gate(index, 1);
333 usb_phy_enable(index, ehci);
334#endif
335
336 return 0;
337}
338
339#ifndef CONFIG_DM_USB
Troy Kisky7d6bbb92013-10-10 15:27:57 -0700340int ehci_hcd_init(int index, enum usb_init_type init,
341 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000342{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700343 enum usb_init_type type;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500344#if defined(CONFIG_MX6)
345 u32 controller_spacing = 0x200;
346#elif defined(CONFIG_MX7)
347 u32 controller_spacing = 0x10000;
348#endif
Ye.Lif93453a2014-09-15 17:23:14 +0800349 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
Adrian Alonsof31599f2015-08-06 15:43:17 -0500350 (controller_spacing * index));
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700351 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000352
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700353 if (index > 3)
354 return -EINVAL;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000355
Peng Fan5c363c12016-06-17 14:19:27 +0800356 ret = ehci_mx6_common_init(ehci, index);
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700357 if (ret)
358 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000359
Peng Fan220402e2014-11-10 08:50:39 +0800360 type = board_usb_phy_mode(index);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000361
Peng Fan5c363c12016-06-17 14:19:27 +0800362 if (hccr && hcor) {
363 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
364 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
365 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
366 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000367
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700368 if ((type == init) || (type == USB_INIT_DEVICE))
369 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
370 if (type != init)
371 return -ENODEV;
372 if (type == USB_INIT_DEVICE)
373 return 0;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500374
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700375 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500376 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000377 setbits_le32(&ehci->portsc, USB_EN);
378
379 mdelay(10);
380
381 return 0;
382}
383
Lucas Stach3494a4c2012-09-26 00:14:35 +0200384int ehci_hcd_stop(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000385{
Peng Fan5c363c12016-06-17 14:19:27 +0800386 return 0;
387}
388#else
389struct ehci_mx6_priv_data {
390 struct ehci_ctrl ctrl;
391 struct usb_ehci *ehci;
Peng Fan13351332016-12-22 17:06:43 +0800392 struct udevice *vbus_supply;
Peng Fan5c363c12016-06-17 14:19:27 +0800393 enum usb_init_type init_type;
394 int portnr;
395};
396
397static int mx6_init_after_reset(struct ehci_ctrl *dev)
398{
399 struct ehci_mx6_priv_data *priv = dev->priv;
400 enum usb_init_type type = priv->init_type;
401 struct usb_ehci *ehci = priv->ehci;
402 int ret;
403
404 ret = ehci_mx6_common_init(priv->ehci, priv->portnr);
405 if (ret)
406 return ret;
407
Peng Fan13351332016-12-22 17:06:43 +0800408 if (priv->vbus_supply) {
409 ret = regulator_set_enable(priv->vbus_supply,
410 (type == USB_INIT_DEVICE) ?
411 false : true);
412 if (ret) {
413 puts("Error enabling VBUS supply\n");
414 return ret;
415 }
416 }
Peng Fan5c363c12016-06-17 14:19:27 +0800417
418 if (type == USB_INIT_DEVICE)
419 return 0;
420
421 setbits_le32(&ehci->usbmode, CM_HOST);
422 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
423 setbits_le32(&ehci->portsc, USB_EN);
424
425 mdelay(10);
426
427 return 0;
428}
429
430static const struct ehci_ops mx6_ehci_ops = {
431 .init_after_reset = mx6_init_after_reset
432};
433
Peng Fan9e3eab32016-12-22 17:06:42 +0800434static int ehci_usb_phy_mode(struct udevice *dev)
435{
436 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassba1dea42017-05-17 17:18:05 -0600437 void *__iomem addr = (void *__iomem)devfdt_get_addr(dev);
Peng Fan9e3eab32016-12-22 17:06:42 +0800438 void *__iomem phy_ctrl, *__iomem phy_status;
439 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700440 int offset = dev_of_offset(dev), phy_off;
Peng Fan9e3eab32016-12-22 17:06:42 +0800441 u32 val;
442
443 /*
444 * About fsl,usbphy, Refer to
445 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
446 */
447 if (is_mx6()) {
448 phy_off = fdtdec_lookup_phandle(blob,
449 offset,
450 "fsl,usbphy");
451 if (phy_off < 0)
452 return -EINVAL;
453
454 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
455 "reg");
456 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
457 return -EINVAL;
458
459 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
460 val = readl(phy_ctrl);
461
462 if (val & USBPHY_CTRL_OTG_ID)
463 plat->init_type = USB_INIT_DEVICE;
464 else
465 plat->init_type = USB_INIT_HOST;
466 } else if (is_mx7()) {
467 phy_status = (void __iomem *)(addr +
468 USBNC_PHY_STATUS_OFFSET);
469 val = readl(phy_status);
470
471 if (val & USBNC_PHYSTATUS_ID_DIG)
472 plat->init_type = USB_INIT_DEVICE;
473 else
474 plat->init_type = USB_INIT_HOST;
475 } else {
476 return -EINVAL;
477 }
478
479 return 0;
480}
481
482static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
483{
484 struct usb_platdata *plat = dev_get_platdata(dev);
485 const char *mode;
486
Simon Glassdd79d6e2017-01-17 16:52:55 -0700487 mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "dr_mode", NULL);
Peng Fan9e3eab32016-12-22 17:06:42 +0800488 if (mode) {
489 if (strcmp(mode, "peripheral") == 0)
490 plat->init_type = USB_INIT_DEVICE;
491 else if (strcmp(mode, "host") == 0)
492 plat->init_type = USB_INIT_HOST;
493 else if (strcmp(mode, "otg") == 0)
494 return ehci_usb_phy_mode(dev);
495 else
496 return -EINVAL;
497
498 return 0;
499 }
500
501 return ehci_usb_phy_mode(dev);
502}
503
Peng Fan5c363c12016-06-17 14:19:27 +0800504static int ehci_usb_probe(struct udevice *dev)
505{
506 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassba1dea42017-05-17 17:18:05 -0600507 struct usb_ehci *ehci = (struct usb_ehci *)devfdt_get_addr(dev);
Peng Fan5c363c12016-06-17 14:19:27 +0800508 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
Peng Fan13351332016-12-22 17:06:43 +0800509 enum usb_init_type type = plat->init_type;
Peng Fan5c363c12016-06-17 14:19:27 +0800510 struct ehci_hccr *hccr;
511 struct ehci_hcor *hcor;
512 int ret;
513
514 priv->ehci = ehci;
515 priv->portnr = dev->seq;
Peng Fan13351332016-12-22 17:06:43 +0800516 priv->init_type = type;
517
518 ret = device_get_supply_regulator(dev, "vbus-supply",
519 &priv->vbus_supply);
520 if (ret)
521 debug("%s: No vbus supply\n", dev->name);
Peng Fan5c363c12016-06-17 14:19:27 +0800522
523 ret = ehci_mx6_common_init(ehci, priv->portnr);
524 if (ret)
525 return ret;
526
Peng Fan13351332016-12-22 17:06:43 +0800527 if (priv->vbus_supply) {
528 ret = regulator_set_enable(priv->vbus_supply,
529 (type == USB_INIT_DEVICE) ?
530 false : true);
531 if (ret) {
532 puts("Error enabling VBUS supply\n");
533 return ret;
534 }
535 }
Peng Fan5c363c12016-06-17 14:19:27 +0800536
537 if (priv->init_type == USB_INIT_HOST) {
538 setbits_le32(&ehci->usbmode, CM_HOST);
539 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
540 setbits_le32(&ehci->portsc, USB_EN);
541 }
542
543 mdelay(10);
544
545 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
546 hcor = (struct ehci_hcor *)((uint32_t)hccr +
547 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
548
549 return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
550}
551
Peng Fan5c363c12016-06-17 14:19:27 +0800552static const struct udevice_id mx6_usb_ids[] = {
553 { .compatible = "fsl,imx27-usb" },
554 { }
555};
556
557U_BOOT_DRIVER(usb_mx6) = {
558 .name = "ehci_mx6",
559 .id = UCLASS_USB,
560 .of_match = mx6_usb_ids,
Peng Fan9e3eab32016-12-22 17:06:42 +0800561 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
Peng Fan5c363c12016-06-17 14:19:27 +0800562 .probe = ehci_usb_probe,
Masahiro Yamadad41919b2016-09-06 22:17:34 +0900563 .remove = ehci_deregister,
Peng Fan5c363c12016-06-17 14:19:27 +0800564 .ops = &ehci_usb_ops,
565 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
566 .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data),
567 .flags = DM_FLAG_ALLOC_PRIV_DMA,
568};
569#endif