blob: 04eb3c006d45425bfc298770b3733aafc38f9d7c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wolfgang Grandegger3f467522012-02-08 22:33:25 +00002/*
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4 * Copyright (C) 2010 Freescale Semiconductor, Inc.
Wolfgang Grandegger3f467522012-02-08 22:33:25 +00005 */
6
7#include <common.h>
8#include <usb.h>
9#include <errno.h>
Mateusz Kulikowski8c25c252016-01-23 11:54:32 +010010#include <wait_bit.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000011#include <linux/compiler.h>
Mateusz Kulikowskie162c6b2016-03-31 23:12:23 +020012#include <usb/ehci-ci.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000013#include <asm/io.h>
14#include <asm/arch/imx-regs.h>
15#include <asm/arch/clock.h>
Stefano Babic552a8482017-06-29 10:16:06 +020016#include <asm/mach-imx/iomux-v3.h>
17#include <asm/mach-imx/sys_proto.h>
Peng Fanbb42fb42016-06-17 14:19:27 +080018#include <dm.h>
Simon Glassc62db352017-05-31 19:47:48 -060019#include <asm/mach-types.h>
Peng Fanfcf9f9f2016-12-22 17:06:43 +080020#include <power/regulator.h>
Adam Ford69535b32019-04-03 08:41:56 -050021#include <linux/usb/otg.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000022
23#include "ehci.h"
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000024
Peng Fancccbddc2016-12-22 17:06:42 +080025DECLARE_GLOBAL_DATA_PTR;
26
Wolfgang Grandegger3f467522012-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 Kiskyd1a52862013-10-10 15:27:59 -070045#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000046
Wolfgang Grandegger3f467522012-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 Grandegger3f467522012-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 Alonso35554fc2015-08-06 15:43:17 -050055#define USBNC_OFFSET 0x200
Peng Fancccbddc2016-12-22 17:06:42 +080056#define USBNC_PHY_STATUS_OFFSET 0x23C
Adrian Alonso35554fc2015-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 Agner9a881802016-07-13 00:25:37 -070059#define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
Wolfgang Grandegger3f467522012-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 Grandegger3f467522012-02-08 22:33:25 +000064#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
65#define UCMD_RESET (1 << 1) /* controller reset */
66
Ye Li235f5e12019-10-24 10:29:32 -030067#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Troy Kiskyd1a52862013-10-10 15:27:59 -070068static const unsigned phy_bases[] = {
69 USB_PHY0_BASE_ADDR,
Ye Li235f5e12019-10-24 10:29:32 -030070#if defined(USB_PHY1_BASE_ADDR)
Troy Kiskyd1a52862013-10-10 15:27:59 -070071 USB_PHY1_BASE_ADDR,
Ye Li235f5e12019-10-24 10:29:32 -030072#endif
Troy Kiskyd1a52862013-10-10 15:27:59 -070073};
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000074
Troy Kiskyd1a52862013-10-10 15:27:59 -070075static void usb_internal_phy_clock_gate(int index, int on)
76{
77 void __iomem *phy_reg;
78
79 if (index >= ARRAY_SIZE(phy_bases))
80 return;
81
82 phy_reg = (void __iomem *)phy_bases[index];
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000083 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
Adrian Alonsoe38ff302015-08-06 15:43:15 -050084 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000085}
86
Troy Kiskyd1a52862013-10-10 15:27:59 -070087static void usb_power_config(int index)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000088{
Ye Li235f5e12019-10-24 10:29:32 -030089#if defined(CONFIG_MX7ULP)
90 struct usbphy_regs __iomem *usbphy =
91 (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR;
92
93 if (index > 0)
94 return;
95
96 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
97 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
98 &usbphy->usb1_chrg_detect);
99
100 scg_enable_usb_pll(true);
101
102#else
Wolfgang Grandegger3f29d962012-05-02 04:36:39 +0000103 struct anatop_regs __iomem *anatop =
104 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Troy Kiskyd1a52862013-10-10 15:27:59 -0700105 void __iomem *chrg_detect;
106 void __iomem *pll_480_ctrl_clr;
107 void __iomem *pll_480_ctrl_set;
108
109 switch (index) {
110 case 0:
111 chrg_detect = &anatop->usb1_chrg_detect;
112 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
113 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
114 break;
115 case 1:
116 chrg_detect = &anatop->usb2_chrg_detect;
117 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
118 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
119 break;
120 default:
121 return;
122 }
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000123 /*
Troy Kiskyd1a52862013-10-10 15:27:59 -0700124 * Some phy and power's special controls
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000125 * 1. The external charger detector needs to be disabled
126 * or the signal at DP will be poor
Troy Kiskyd1a52862013-10-10 15:27:59 -0700127 * 2. The PLL's power and output to usb
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000128 * is totally controlled by IC, so the Software only needs
129 * to enable them at initializtion.
130 */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500131 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000132 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700133 chrg_detect);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000134
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500135 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700136 pll_480_ctrl_clr);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000137
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500138 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000139 ANADIG_USB2_PLL_480_CTRL_POWER |
140 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700141 pll_480_ctrl_set);
Ye Li235f5e12019-10-24 10:29:32 -0300142
143#endif
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000144}
145
Troy Kiskyd1a52862013-10-10 15:27:59 -0700146/* Return 0 : host node, <>0 : device mode */
147static int usb_phy_enable(int index, struct usb_ehci *ehci)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000148{
Troy Kiskyd1a52862013-10-10 15:27:59 -0700149 void __iomem *phy_reg;
150 void __iomem *phy_ctrl;
151 void __iomem *usb_cmd;
Adrian Alonsof0c89d52015-08-06 15:46:03 -0500152 int ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000153
Troy Kiskyd1a52862013-10-10 15:27:59 -0700154 if (index >= ARRAY_SIZE(phy_bases))
155 return 0;
156
157 phy_reg = (void __iomem *)phy_bases[index];
158 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
159 usb_cmd = (void __iomem *)&ehci->usbcmd;
160
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000161 /* Stop then Reset */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500162 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100163 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
Adrian Alonsof0c89d52015-08-06 15:46:03 -0500164 if (ret)
165 return ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000166
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500167 setbits_le32(usb_cmd, UCMD_RESET);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100168 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
Adrian Alonsof0c89d52015-08-06 15:46:03 -0500169 if (ret)
170 return ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000171
172 /* Reset USBPHY module */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500173 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000174 udelay(10);
175
176 /* Remove CLKGATE and SFTRST */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500177 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000178 udelay(10);
179
180 /* Power up the PHY */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500181 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000182 /* enable FS/LS device */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500183 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
184 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000185
Peng Fan229dbba2014-11-10 08:50:39 +0800186 return 0;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000187}
188
Peng Fan229dbba2014-11-10 08:50:39 +0800189int usb_phy_mode(int port)
190{
191 void __iomem *phy_reg;
192 void __iomem *phy_ctrl;
193 u32 val;
194
195 phy_reg = (void __iomem *)phy_bases[port];
196 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
197
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500198 val = readl(phy_ctrl);
Peng Fan229dbba2014-11-10 08:50:39 +0800199
200 if (val & USBPHY_CTRL_OTG_ID)
201 return USB_INIT_DEVICE;
202 else
203 return USB_INIT_HOST;
204}
205
Ye Li235f5e12019-10-24 10:29:32 -0300206#if defined(CONFIG_MX7ULP)
207struct usbnc_regs {
208 u32 ctrl1;
209 u32 ctrl2;
210 u32 reserve0[2];
211 u32 hsic_ctrl;
212};
213#else
Adrian Alonso35554fc2015-08-06 15:43:17 -0500214/* Base address for this IP block is 0x02184800 */
215struct usbnc_regs {
216 u32 ctrl[4]; /* otg/host1-3 */
217 u32 uh2_hsic_ctrl;
218 u32 uh3_hsic_ctrl;
219 u32 otg_phy_ctrl_0;
220 u32 uh1_phy_ctrl_0;
221};
Ye Li235f5e12019-10-24 10:29:32 -0300222#endif
223
Adrian Alonso35554fc2015-08-06 15:43:17 -0500224#elif defined(CONFIG_MX7)
225struct usbnc_regs {
226 u32 ctrl1;
227 u32 ctrl2;
228 u32 reserve1[10];
229 u32 phy_cfg1;
230 u32 phy_cfg2;
Peng Fan429ff442016-06-20 09:43:08 +0800231 u32 reserve2;
Adrian Alonso35554fc2015-08-06 15:43:17 -0500232 u32 phy_status;
Peng Fan429ff442016-06-20 09:43:08 +0800233 u32 reserve3[4];
Adrian Alonso35554fc2015-08-06 15:43:17 -0500234 u32 adp_cfg1;
235 u32 adp_cfg2;
236 u32 adp_status;
237};
238
239static void usb_power_config(int index)
240{
241 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
242 (0x10000 * index) + USBNC_OFFSET);
243 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
244
Peng Fan57de41e2016-06-20 09:43:09 +0800245 /*
246 * Clear the ACAENB to enable usb_otg_id detection,
247 * otherwise it is the ACA detection enabled.
248 */
249 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
Adrian Alonso35554fc2015-08-06 15:43:17 -0500250}
251
252int 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
268static void usb_oc_config(int index)
269{
270#if defined(CONFIG_MX6)
271 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
272 USB_OTHERREGS_OFFSET);
273 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
Ye Li235f5e12019-10-24 10:29:32 -0300274#elif defined(CONFIG_MX7) || defined(CONFIG_MX7ULP)
Adrian Alonso35554fc2015-08-06 15:43:17 -0500275 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
276 (0x10000 * index) + USBNC_OFFSET);
277 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
278#endif
279
280#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
281 /* mx6qarm2 seems to required a different setting*/
282 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
283#else
284 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
285#endif
286
Adrian Alonso35554fc2015-08-06 15:43:17 -0500287 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
Ye Li235f5e12019-10-24 10:29:32 -0300288
289 /* Set power polarity to high active */
290#ifdef CONFIG_MXC_USB_OTG_HACTIVE
291 setbits_le32(ctrl, UCTRL_PWR_POL);
292#else
293 clrbits_le32(ctrl, UCTRL_PWR_POL);
294#endif
Adrian Alonso35554fc2015-08-06 15:43:17 -0500295}
296
Adrian Alonso74f06102015-08-06 15:43:16 -0500297/**
Stefan Agner79d867c2016-05-05 16:59:12 -0700298 * board_usb_phy_mode - override usb phy mode
Adrian Alonso74f06102015-08-06 15:43:16 -0500299 * @port: usb host/otg port
300 *
301 * Target board specific, override usb_phy_mode.
302 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
303 * left disconnected in this case usb_phy_mode will not be able to identify
304 * the phy mode that usb port is used.
305 * Machine file overrides board_usb_phy_mode.
306 *
307 * Return: USB_INIT_DEVICE or USB_INIT_HOST
308 */
Peng Fan229dbba2014-11-10 08:50:39 +0800309int __weak board_usb_phy_mode(int port)
310{
311 return usb_phy_mode(port);
312}
313
Adrian Alonso74f06102015-08-06 15:43:16 -0500314/**
315 * board_ehci_hcd_init - set usb vbus voltage
316 * @port: usb otg port
317 *
318 * Target board specific, setup iomux pad to setup supply vbus voltage
319 * for usb otg port. Machine board file overrides board_ehci_hcd_init
320 *
321 * Return: 0 Success
322 */
Benoît Thébaudeauf22e4fa2012-11-13 09:58:35 +0000323int __weak board_ehci_hcd_init(int port)
324{
325 return 0;
326}
327
Adrian Alonso74f06102015-08-06 15:43:16 -0500328/**
329 * board_ehci_power - enables/disables usb vbus voltage
330 * @port: usb otg port
331 * @on: on/off vbus voltage
332 *
333 * Enables/disables supply vbus voltage for usb otg port.
334 * Machine board file overrides board_ehci_power
335 *
336 * Return: 0 Success
337 */
Troy Kiskyd1a52862013-10-10 15:27:59 -0700338int __weak board_ehci_power(int port, int on)
339{
340 return 0;
341}
342
Peng Fanbb42fb42016-06-17 14:19:27 +0800343int ehci_mx6_common_init(struct usb_ehci *ehci, int index)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000344{
Stefan Agner79d867c2016-05-05 16:59:12 -0700345 int ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000346
347 enable_usboh3_clk(1);
348 mdelay(1);
349
350 /* Do board specific initialization */
Stefan Agner79d867c2016-05-05 16:59:12 -0700351 ret = board_ehci_hcd_init(index);
352 if (ret)
353 return ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000354
Troy Kiskyd1a52862013-10-10 15:27:59 -0700355 usb_power_config(index);
356 usb_oc_config(index);
Adrian Alonso35554fc2015-08-06 15:43:17 -0500357
Ye Li235f5e12019-10-24 10:29:32 -0300358#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Troy Kiskyd1a52862013-10-10 15:27:59 -0700359 usb_internal_phy_clock_gate(index, 1);
Peng Fan229dbba2014-11-10 08:50:39 +0800360 usb_phy_enable(index, ehci);
Adrian Alonso35554fc2015-08-06 15:43:17 -0500361#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800362
363 return 0;
364}
365
Sven Schwermerfd09c202018-11-21 08:43:56 +0100366#if !CONFIG_IS_ENABLED(DM_USB)
Peng Fanbb42fb42016-06-17 14:19:27 +0800367int ehci_hcd_init(int index, enum usb_init_type init,
368 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
369{
370 enum usb_init_type type;
371#if defined(CONFIG_MX6)
372 u32 controller_spacing = 0x200;
Ye Li235f5e12019-10-24 10:29:32 -0300373#elif defined(CONFIG_MX7) || defined(CONFIG_MX7ULP)
Peng Fanbb42fb42016-06-17 14:19:27 +0800374 u32 controller_spacing = 0x10000;
375#endif
376 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
377 (controller_spacing * index));
378 int ret;
379
380 if (index > 3)
381 return -EINVAL;
382
Peng Fan0bd3d912020-05-01 22:08:36 +0800383 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
384 if (usb_fused((ulong)ehci)) {
385 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
386 (ulong)ehci);
387 return -ENODEV;
388 }
389 }
390
Peng Fanbb42fb42016-06-17 14:19:27 +0800391 ret = ehci_mx6_common_init(ehci, index);
392 if (ret)
393 return ret;
394
Peng Fan229dbba2014-11-10 08:50:39 +0800395 type = board_usb_phy_mode(index);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000396
Peng Fanbb42fb42016-06-17 14:19:27 +0800397 if (hccr && hcor) {
398 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
399 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
400 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
401 }
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000402
Troy Kiskyd1a52862013-10-10 15:27:59 -0700403 if ((type == init) || (type == USB_INIT_DEVICE))
404 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
405 if (type != init)
406 return -ENODEV;
407 if (type == USB_INIT_DEVICE)
408 return 0;
Adrian Alonso35554fc2015-08-06 15:43:17 -0500409
Troy Kiskyd1a52862013-10-10 15:27:59 -0700410 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500411 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000412 setbits_le32(&ehci->portsc, USB_EN);
413
414 mdelay(10);
415
416 return 0;
417}
418
Lucas Stach676ae062012-09-26 00:14:35 +0200419int ehci_hcd_stop(int index)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000420{
421 return 0;
422}
Peng Fanbb42fb42016-06-17 14:19:27 +0800423#else
424struct ehci_mx6_priv_data {
425 struct ehci_ctrl ctrl;
426 struct usb_ehci *ehci;
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800427 struct udevice *vbus_supply;
Peng Fanbb42fb42016-06-17 14:19:27 +0800428 enum usb_init_type init_type;
429 int portnr;
430};
431
432static int mx6_init_after_reset(struct ehci_ctrl *dev)
433{
434 struct ehci_mx6_priv_data *priv = dev->priv;
435 enum usb_init_type type = priv->init_type;
436 struct usb_ehci *ehci = priv->ehci;
437 int ret;
438
439 ret = ehci_mx6_common_init(priv->ehci, priv->portnr);
440 if (ret)
441 return ret;
442
Abel Vesa921208e2019-02-01 16:40:08 +0000443#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800444 if (priv->vbus_supply) {
445 ret = regulator_set_enable(priv->vbus_supply,
446 (type == USB_INIT_DEVICE) ?
447 false : true);
448 if (ret) {
449 puts("Error enabling VBUS supply\n");
450 return ret;
451 }
452 }
Abel Vesa921208e2019-02-01 16:40:08 +0000453#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800454
455 if (type == USB_INIT_DEVICE)
456 return 0;
457
458 setbits_le32(&ehci->usbmode, CM_HOST);
459 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
460 setbits_le32(&ehci->portsc, USB_EN);
461
462 mdelay(10);
463
464 return 0;
465}
466
467static const struct ehci_ops mx6_ehci_ops = {
468 .init_after_reset = mx6_init_after_reset
469};
470
Peng Fancccbddc2016-12-22 17:06:42 +0800471static int ehci_usb_phy_mode(struct udevice *dev)
472{
473 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassa821c4a2017-05-17 17:18:05 -0600474 void *__iomem addr = (void *__iomem)devfdt_get_addr(dev);
Peng Fancccbddc2016-12-22 17:06:42 +0800475 void *__iomem phy_ctrl, *__iomem phy_status;
476 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700477 int offset = dev_of_offset(dev), phy_off;
Peng Fancccbddc2016-12-22 17:06:42 +0800478 u32 val;
479
480 /*
481 * About fsl,usbphy, Refer to
482 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
483 */
Ye Li235f5e12019-10-24 10:29:32 -0300484 if (is_mx6() || is_mx7ulp()) {
Peng Fancccbddc2016-12-22 17:06:42 +0800485 phy_off = fdtdec_lookup_phandle(blob,
486 offset,
487 "fsl,usbphy");
488 if (phy_off < 0)
489 return -EINVAL;
490
491 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
492 "reg");
493 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
494 return -EINVAL;
495
496 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
497 val = readl(phy_ctrl);
498
499 if (val & USBPHY_CTRL_OTG_ID)
500 plat->init_type = USB_INIT_DEVICE;
501 else
502 plat->init_type = USB_INIT_HOST;
503 } else if (is_mx7()) {
504 phy_status = (void __iomem *)(addr +
505 USBNC_PHY_STATUS_OFFSET);
506 val = readl(phy_status);
507
508 if (val & USBNC_PHYSTATUS_ID_DIG)
509 plat->init_type = USB_INIT_DEVICE;
510 else
511 plat->init_type = USB_INIT_HOST;
512 } else {
513 return -EINVAL;
514 }
515
516 return 0;
517}
518
519static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
520{
521 struct usb_platdata *plat = dev_get_platdata(dev);
Adam Ford69535b32019-04-03 08:41:56 -0500522 enum usb_dr_mode dr_mode;
Peng Fancccbddc2016-12-22 17:06:42 +0800523
Kever Yangac28e592020-03-04 08:59:50 +0800524 dr_mode = usb_get_dr_mode(dev->node);
Peng Fancccbddc2016-12-22 17:06:42 +0800525
Adam Ford69535b32019-04-03 08:41:56 -0500526 switch (dr_mode) {
527 case USB_DR_MODE_HOST:
528 plat->init_type = USB_INIT_HOST;
529 break;
530 case USB_DR_MODE_PERIPHERAL:
531 plat->init_type = USB_INIT_DEVICE;
532 break;
533 case USB_DR_MODE_OTG:
534 case USB_DR_MODE_UNKNOWN:
535 return ehci_usb_phy_mode(dev);
536 };
Peng Fancccbddc2016-12-22 17:06:42 +0800537
Adam Ford69535b32019-04-03 08:41:56 -0500538 return 0;
Peng Fancccbddc2016-12-22 17:06:42 +0800539}
540
Marek Vasut501547c2019-06-24 19:05:47 +0200541static int ehci_usb_bind(struct udevice *dev)
542{
543 /*
544 * TODO:
545 * This driver is only partly converted to DT probing and still uses
546 * a tremendous amount of hard-coded addresses. To make things worse,
547 * the driver depends on specific sequential indexing of controllers,
548 * from which it derives offsets in the PHY and ANATOP register sets.
549 *
550 * Here we attempt to calculate these indexes from DT information as
Igor Opaniuk1198a102019-10-10 16:09:35 +0300551 * well as we can. The USB controllers on all existing iMX6 SoCs
552 * are placed next to each other, at addresses incremented by 0x200,
553 * and iMX7 their addresses are shifted by 0x10000.
554 * Thus, the index is derived from the multiple of 0x200 (0x10000 for
555 * iMX7) offset from the first controller address.
Marek Vasut501547c2019-06-24 19:05:47 +0200556 *
557 * However, to complete conversion of this driver to DT probing, the
558 * following has to be done:
559 * - DM clock framework support for iMX must be implemented
560 * - usb_power_config() has to be converted to clock framework
561 * -> Thus, the ad-hoc "index" variable goes away.
562 * - USB PHY handling has to be factored out into separate driver
563 * -> Thus, the ad-hoc "index" variable goes away from the PHY
564 * code, the PHY driver must parse it's address from DT. This
565 * USB driver must find the PHY driver via DT phandle.
566 * -> usb_power_config() shall be moved to PHY driver
567 * With these changes in place, the ad-hoc indexing goes away and
568 * the driver is fully converted to DT probing.
569 */
Igor Opaniuk1198a102019-10-10 16:09:35 +0300570 u32 controller_spacing = is_mx7() ? 0x10000 : 0x200;
571 fdt_addr_t addr = devfdt_get_addr_index(dev, 0);
Marek Vasut501547c2019-06-24 19:05:47 +0200572
Igor Opaniuk1198a102019-10-10 16:09:35 +0300573 dev->req_seq = (addr - USB_BASE_ADDR) / controller_spacing;
Marek Vasut501547c2019-06-24 19:05:47 +0200574
575 return 0;
576}
577
Peng Fanbb42fb42016-06-17 14:19:27 +0800578static int ehci_usb_probe(struct udevice *dev)
579{
580 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassa821c4a2017-05-17 17:18:05 -0600581 struct usb_ehci *ehci = (struct usb_ehci *)devfdt_get_addr(dev);
Peng Fanbb42fb42016-06-17 14:19:27 +0800582 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800583 enum usb_init_type type = plat->init_type;
Peng Fanbb42fb42016-06-17 14:19:27 +0800584 struct ehci_hccr *hccr;
585 struct ehci_hcor *hcor;
586 int ret;
587
Peng Fan0bd3d912020-05-01 22:08:36 +0800588 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
589 if (usb_fused((ulong)ehci)) {
590 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
591 (ulong)ehci);
592 return -ENODEV;
593 }
594 }
595
Peng Fanbb42fb42016-06-17 14:19:27 +0800596 priv->ehci = ehci;
597 priv->portnr = dev->seq;
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800598 priv->init_type = type;
599
Abel Vesa921208e2019-02-01 16:40:08 +0000600#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800601 ret = device_get_supply_regulator(dev, "vbus-supply",
602 &priv->vbus_supply);
603 if (ret)
604 debug("%s: No vbus supply\n", dev->name);
Abel Vesa921208e2019-02-01 16:40:08 +0000605#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800606 ret = ehci_mx6_common_init(ehci, priv->portnr);
607 if (ret)
608 return ret;
609
Abel Vesa921208e2019-02-01 16:40:08 +0000610#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800611 if (priv->vbus_supply) {
612 ret = regulator_set_enable(priv->vbus_supply,
613 (type == USB_INIT_DEVICE) ?
614 false : true);
615 if (ret) {
616 puts("Error enabling VBUS supply\n");
617 return ret;
618 }
619 }
Abel Vesa921208e2019-02-01 16:40:08 +0000620#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800621
622 if (priv->init_type == USB_INIT_HOST) {
623 setbits_le32(&ehci->usbmode, CM_HOST);
624 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
625 setbits_le32(&ehci->portsc, USB_EN);
626 }
627
628 mdelay(10);
629
630 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
631 hcor = (struct ehci_hcor *)((uint32_t)hccr +
632 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
633
634 return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
635}
636
Peng Fanbb42fb42016-06-17 14:19:27 +0800637static const struct udevice_id mx6_usb_ids[] = {
638 { .compatible = "fsl,imx27-usb" },
639 { }
640};
641
642U_BOOT_DRIVER(usb_mx6) = {
643 .name = "ehci_mx6",
644 .id = UCLASS_USB,
645 .of_match = mx6_usb_ids,
Peng Fancccbddc2016-12-22 17:06:42 +0800646 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
Marek Vasut501547c2019-06-24 19:05:47 +0200647 .bind = ehci_usb_bind,
Peng Fanbb42fb42016-06-17 14:19:27 +0800648 .probe = ehci_usb_probe,
Masahiro Yamada40527342016-09-06 22:17:34 +0900649 .remove = ehci_deregister,
Peng Fanbb42fb42016-06-17 14:19:27 +0800650 .ops = &ehci_usb_ops,
651 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
652 .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data),
653 .flags = DM_FLAG_ALLOC_PRIV_DMA,
654};
655#endif