blob: 8fcb82e14c298977a4b1cdef6f521af919190a9b [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wolfgang Grandegger1859b702012-02-08 22:33:25 +00002/*
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4 * Copyright (C) 2010 Freescale Semiconductor, Inc.
Wolfgang Grandegger1859b702012-02-08 22:33:25 +00005 */
6
7#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +00009#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>
Adam Ford15287f02019-04-03 08:41:56 -050022#include <linux/usb/otg.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000023
24#include "ehci.h"
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000025
Peng Fan9e3eab32016-12-22 17:06:42 +080026DECLARE_GLOBAL_DATA_PTR;
27
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000028#define USB_OTGREGS_OFFSET 0x000
29#define USB_H1REGS_OFFSET 0x200
30#define USB_H2REGS_OFFSET 0x400
31#define USB_H3REGS_OFFSET 0x600
32#define USB_OTHERREGS_OFFSET 0x800
33
34#define USB_H1_CTRL_OFFSET 0x04
35
36#define USBPHY_CTRL 0x00000030
37#define USBPHY_CTRL_SET 0x00000034
38#define USBPHY_CTRL_CLR 0x00000038
39#define USBPHY_CTRL_TOG 0x0000003c
40
41#define USBPHY_PWD 0x00000000
42#define USBPHY_CTRL_SFTRST 0x80000000
43#define USBPHY_CTRL_CLKGATE 0x40000000
44#define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
45#define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
Troy Kiskyed72a9e2013-10-10 15:27:59 -070046#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000047
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000048#define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
49#define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
50
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000051#define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
52#define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
53#define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
54#define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
55
Adrian Alonsof31599f2015-08-06 15:43:17 -050056#define USBNC_OFFSET 0x200
Peng Fan9e3eab32016-12-22 17:06:42 +080057#define USBNC_PHY_STATUS_OFFSET 0x23C
Adrian Alonsof31599f2015-08-06 15:43:17 -050058#define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
59#define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
Stefan Agner475cf912016-07-13 00:25:37 -070060#define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000061#define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
62#define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
63
64/* USBCMD */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000065#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
66#define UCMD_RESET (1 << 1) /* controller reset */
67
Ye Li9da57ea2019-10-24 10:29:32 -030068#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Troy Kiskyed72a9e2013-10-10 15:27:59 -070069static const unsigned phy_bases[] = {
70 USB_PHY0_BASE_ADDR,
Ye Li9da57ea2019-10-24 10:29:32 -030071#if defined(USB_PHY1_BASE_ADDR)
Troy Kiskyed72a9e2013-10-10 15:27:59 -070072 USB_PHY1_BASE_ADDR,
Ye Li9da57ea2019-10-24 10:29:32 -030073#endif
Troy Kiskyed72a9e2013-10-10 15:27:59 -070074};
75
76static void usb_internal_phy_clock_gate(int index, int on)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000077{
Troy Kiskyed72a9e2013-10-10 15:27:59 -070078 void __iomem *phy_reg;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000079
Troy Kiskyed72a9e2013-10-10 15:27:59 -070080 if (index >= ARRAY_SIZE(phy_bases))
81 return;
82
83 phy_reg = (void __iomem *)phy_bases[index];
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000084 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
Adrian Alonsoaee79b42015-08-06 15:43:15 -050085 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000086}
87
Troy Kiskyed72a9e2013-10-10 15:27:59 -070088static void usb_power_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000089{
Ye Li9da57ea2019-10-24 10:29:32 -030090#if defined(CONFIG_MX7ULP)
91 struct usbphy_regs __iomem *usbphy =
92 (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR;
93
94 if (index > 0)
95 return;
96
97 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
98 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
99 &usbphy->usb1_chrg_detect);
100
101 scg_enable_usb_pll(true);
102
103#else
Wolfgang Grandegger5d113ca2012-05-02 04:36:39 +0000104 struct anatop_regs __iomem *anatop =
105 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700106 void __iomem *chrg_detect;
107 void __iomem *pll_480_ctrl_clr;
108 void __iomem *pll_480_ctrl_set;
109
110 switch (index) {
111 case 0:
112 chrg_detect = &anatop->usb1_chrg_detect;
113 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
114 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
115 break;
116 case 1:
117 chrg_detect = &anatop->usb2_chrg_detect;
118 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
119 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
120 break;
121 default:
122 return;
123 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000124 /*
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700125 * Some phy and power's special controls
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000126 * 1. The external charger detector needs to be disabled
127 * or the signal at DP will be poor
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700128 * 2. The PLL's power and output to usb
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000129 * is totally controlled by IC, so the Software only needs
130 * to enable them at initializtion.
131 */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500132 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000133 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700134 chrg_detect);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000135
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500136 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700137 pll_480_ctrl_clr);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000138
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500139 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000140 ANADIG_USB2_PLL_480_CTRL_POWER |
141 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700142 pll_480_ctrl_set);
Ye Li9da57ea2019-10-24 10:29:32 -0300143
144#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000145}
146
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700147/* Return 0 : host node, <>0 : device mode */
148static int usb_phy_enable(int index, struct usb_ehci *ehci)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000149{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700150 void __iomem *phy_reg;
151 void __iomem *phy_ctrl;
152 void __iomem *usb_cmd;
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500153 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000154
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700155 if (index >= ARRAY_SIZE(phy_bases))
156 return 0;
157
158 phy_reg = (void __iomem *)phy_bases[index];
159 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
160 usb_cmd = (void __iomem *)&ehci->usbcmd;
161
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000162 /* Stop then Reset */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500163 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100164 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500165 if (ret)
166 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000167
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500168 setbits_le32(usb_cmd, UCMD_RESET);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100169 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500170 if (ret)
171 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000172
173 /* Reset USBPHY module */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500174 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000175 udelay(10);
176
177 /* Remove CLKGATE and SFTRST */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500178 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000179 udelay(10);
180
181 /* Power up the PHY */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500182 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000183 /* enable FS/LS device */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500184 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
185 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000186
Peng Fan220402e2014-11-10 08:50:39 +0800187 return 0;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000188}
189
Adrian Alonsof31599f2015-08-06 15:43:17 -0500190int usb_phy_mode(int port)
191{
192 void __iomem *phy_reg;
193 void __iomem *phy_ctrl;
194 u32 val;
195
196 phy_reg = (void __iomem *)phy_bases[port];
197 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
198
199 val = readl(phy_ctrl);
200
201 if (val & USBPHY_CTRL_OTG_ID)
202 return USB_INIT_DEVICE;
203 else
204 return USB_INIT_HOST;
205}
206
Ye Li9da57ea2019-10-24 10:29:32 -0300207#if defined(CONFIG_MX7ULP)
208struct usbnc_regs {
209 u32 ctrl1;
210 u32 ctrl2;
211 u32 reserve0[2];
212 u32 hsic_ctrl;
213};
214#else
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700215/* Base address for this IP block is 0x02184800 */
216struct usbnc_regs {
217 u32 ctrl[4]; /* otg/host1-3 */
218 u32 uh2_hsic_ctrl;
219 u32 uh3_hsic_ctrl;
220 u32 otg_phy_ctrl_0;
221 u32 uh1_phy_ctrl_0;
222};
Ye Li9da57ea2019-10-24 10:29:32 -0300223#endif
224
Adrian Alonsof31599f2015-08-06 15:43:17 -0500225#elif defined(CONFIG_MX7)
226struct usbnc_regs {
227 u32 ctrl1;
228 u32 ctrl2;
229 u32 reserve1[10];
230 u32 phy_cfg1;
231 u32 phy_cfg2;
Peng Fan13186cf2016-06-20 09:43:08 +0800232 u32 reserve2;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500233 u32 phy_status;
Peng Fan13186cf2016-06-20 09:43:08 +0800234 u32 reserve3[4];
Adrian Alonsof31599f2015-08-06 15:43:17 -0500235 u32 adp_cfg1;
236 u32 adp_cfg2;
237 u32 adp_status;
238};
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700239
Adrian Alonsof31599f2015-08-06 15:43:17 -0500240static void usb_power_config(int index)
241{
242 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
243 (0x10000 * index) + USBNC_OFFSET);
244 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
245
Peng Fan928d3d02016-06-20 09:43:09 +0800246 /*
247 * Clear the ACAENB to enable usb_otg_id detection,
248 * otherwise it is the ACA detection enabled.
249 */
250 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
Adrian Alonsof31599f2015-08-06 15:43:17 -0500251}
252
253int usb_phy_mode(int port)
254{
255 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
256 (0x10000 * port) + USBNC_OFFSET);
257 void __iomem *status = (void __iomem *)(&usbnc->phy_status);
258 u32 val;
259
260 val = readl(status);
261
262 if (val & USBNC_PHYSTATUS_ID_DIG)
263 return USB_INIT_DEVICE;
264 else
265 return USB_INIT_HOST;
266}
267#endif
268
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700269static void usb_oc_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000270{
Adrian Alonsof31599f2015-08-06 15:43:17 -0500271#if defined(CONFIG_MX6)
Ye.Lif93453a2014-09-15 17:23:14 +0800272 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700273 USB_OTHERREGS_OFFSET);
274 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
Ye Li9da57ea2019-10-24 10:29:32 -0300275#elif defined(CONFIG_MX7) || defined(CONFIG_MX7ULP)
Adrian Alonsof31599f2015-08-06 15:43:17 -0500276 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
277 (0x10000 * index) + USBNC_OFFSET);
278 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
279#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000280
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000281#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
282 /* mx6qarm2 seems to required a different setting*/
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500283 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000284#else
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500285 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000286#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000287
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500288 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
Ye Li9da57ea2019-10-24 10:29:32 -0300289
290 /* Set power polarity to high active */
291#ifdef CONFIG_MXC_USB_OTG_HACTIVE
292 setbits_le32(ctrl, UCTRL_PWR_POL);
293#else
294 clrbits_le32(ctrl, UCTRL_PWR_POL);
295#endif
Peng Fan220402e2014-11-10 08:50:39 +0800296}
297
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500298/**
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700299 * board_usb_phy_mode - override usb phy mode
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500300 * @port: usb host/otg port
301 *
302 * Target board specific, override usb_phy_mode.
303 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
304 * left disconnected in this case usb_phy_mode will not be able to identify
305 * the phy mode that usb port is used.
306 * Machine file overrides board_usb_phy_mode.
307 *
308 * Return: USB_INIT_DEVICE or USB_INIT_HOST
309 */
Peng Fan220402e2014-11-10 08:50:39 +0800310int __weak board_usb_phy_mode(int port)
311{
312 return usb_phy_mode(port);
313}
314
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500315/**
316 * board_ehci_hcd_init - set usb vbus voltage
317 * @port: usb otg port
318 *
319 * Target board specific, setup iomux pad to setup supply vbus voltage
320 * for usb otg port. Machine board file overrides board_ehci_hcd_init
321 *
322 * Return: 0 Success
323 */
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000324int __weak board_ehci_hcd_init(int port)
325{
326 return 0;
327}
328
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500329/**
330 * board_ehci_power - enables/disables usb vbus voltage
331 * @port: usb otg port
332 * @on: on/off vbus voltage
333 *
334 * Enables/disables supply vbus voltage for usb otg port.
335 * Machine board file overrides board_ehci_power
336 *
337 * Return: 0 Success
338 */
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700339int __weak board_ehci_power(int port, int on)
340{
341 return 0;
342}
343
Peng Fan5c363c12016-06-17 14:19:27 +0800344int ehci_mx6_common_init(struct usb_ehci *ehci, int index)
345{
346 int ret;
347
348 enable_usboh3_clk(1);
349 mdelay(1);
350
351 /* Do board specific initialization */
352 ret = board_ehci_hcd_init(index);
353 if (ret)
354 return ret;
355
356 usb_power_config(index);
357 usb_oc_config(index);
358
Ye Li9da57ea2019-10-24 10:29:32 -0300359#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Peng Fan5c363c12016-06-17 14:19:27 +0800360 usb_internal_phy_clock_gate(index, 1);
361 usb_phy_enable(index, ehci);
362#endif
363
364 return 0;
365}
366
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100367#if !CONFIG_IS_ENABLED(DM_USB)
Troy Kisky7d6bbb92013-10-10 15:27:57 -0700368int ehci_hcd_init(int index, enum usb_init_type init,
369 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000370{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700371 enum usb_init_type type;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500372#if defined(CONFIG_MX6)
373 u32 controller_spacing = 0x200;
Ye Li9da57ea2019-10-24 10:29:32 -0300374#elif defined(CONFIG_MX7) || defined(CONFIG_MX7ULP)
Adrian Alonsof31599f2015-08-06 15:43:17 -0500375 u32 controller_spacing = 0x10000;
376#endif
Ye.Lif93453a2014-09-15 17:23:14 +0800377 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
Adrian Alonsof31599f2015-08-06 15:43:17 -0500378 (controller_spacing * index));
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700379 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000380
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700381 if (index > 3)
382 return -EINVAL;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000383
Peng Fanf8b27192020-05-01 22:08:36 +0800384 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
385 if (usb_fused((ulong)ehci)) {
386 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
387 (ulong)ehci);
388 return -ENODEV;
389 }
390 }
391
Peng Fan5c363c12016-06-17 14:19:27 +0800392 ret = ehci_mx6_common_init(ehci, index);
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700393 if (ret)
394 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000395
Peng Fan220402e2014-11-10 08:50:39 +0800396 type = board_usb_phy_mode(index);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000397
Peng Fan5c363c12016-06-17 14:19:27 +0800398 if (hccr && hcor) {
399 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
400 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
401 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
402 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000403
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700404 if ((type == init) || (type == USB_INIT_DEVICE))
405 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
406 if (type != init)
407 return -ENODEV;
408 if (type == USB_INIT_DEVICE)
409 return 0;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500410
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700411 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500412 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000413 setbits_le32(&ehci->portsc, USB_EN);
414
415 mdelay(10);
416
417 return 0;
418}
419
Lucas Stach3494a4c2012-09-26 00:14:35 +0200420int ehci_hcd_stop(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000421{
Peng Fan5c363c12016-06-17 14:19:27 +0800422 return 0;
423}
424#else
425struct ehci_mx6_priv_data {
426 struct ehci_ctrl ctrl;
427 struct usb_ehci *ehci;
Peng Fan13351332016-12-22 17:06:43 +0800428 struct udevice *vbus_supply;
Peng Fan5c363c12016-06-17 14:19:27 +0800429 enum usb_init_type init_type;
430 int portnr;
431};
432
433static int mx6_init_after_reset(struct ehci_ctrl *dev)
434{
435 struct ehci_mx6_priv_data *priv = dev->priv;
436 enum usb_init_type type = priv->init_type;
437 struct usb_ehci *ehci = priv->ehci;
438 int ret;
439
440 ret = ehci_mx6_common_init(priv->ehci, priv->portnr);
441 if (ret)
442 return ret;
443
Abel Vesa888a9462019-02-01 16:40:08 +0000444#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800445 if (priv->vbus_supply) {
446 ret = regulator_set_enable(priv->vbus_supply,
447 (type == USB_INIT_DEVICE) ?
448 false : true);
449 if (ret) {
450 puts("Error enabling VBUS supply\n");
451 return ret;
452 }
453 }
Abel Vesa888a9462019-02-01 16:40:08 +0000454#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800455
456 if (type == USB_INIT_DEVICE)
457 return 0;
458
459 setbits_le32(&ehci->usbmode, CM_HOST);
460 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
461 setbits_le32(&ehci->portsc, USB_EN);
462
463 mdelay(10);
464
465 return 0;
466}
467
468static const struct ehci_ops mx6_ehci_ops = {
469 .init_after_reset = mx6_init_after_reset
470};
471
Peng Fan9e3eab32016-12-22 17:06:42 +0800472static int ehci_usb_phy_mode(struct udevice *dev)
473{
474 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassba1dea42017-05-17 17:18:05 -0600475 void *__iomem addr = (void *__iomem)devfdt_get_addr(dev);
Peng Fan9e3eab32016-12-22 17:06:42 +0800476 void *__iomem phy_ctrl, *__iomem phy_status;
477 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700478 int offset = dev_of_offset(dev), phy_off;
Peng Fan9e3eab32016-12-22 17:06:42 +0800479 u32 val;
480
481 /*
482 * About fsl,usbphy, Refer to
483 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
484 */
Ye Li9da57ea2019-10-24 10:29:32 -0300485 if (is_mx6() || is_mx7ulp()) {
Peng Fan9e3eab32016-12-22 17:06:42 +0800486 phy_off = fdtdec_lookup_phandle(blob,
487 offset,
488 "fsl,usbphy");
489 if (phy_off < 0)
490 return -EINVAL;
491
492 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
493 "reg");
494 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
495 return -EINVAL;
496
497 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
498 val = readl(phy_ctrl);
499
500 if (val & USBPHY_CTRL_OTG_ID)
501 plat->init_type = USB_INIT_DEVICE;
502 else
503 plat->init_type = USB_INIT_HOST;
504 } else if (is_mx7()) {
505 phy_status = (void __iomem *)(addr +
506 USBNC_PHY_STATUS_OFFSET);
507 val = readl(phy_status);
508
509 if (val & USBNC_PHYSTATUS_ID_DIG)
510 plat->init_type = USB_INIT_DEVICE;
511 else
512 plat->init_type = USB_INIT_HOST;
513 } else {
514 return -EINVAL;
515 }
516
517 return 0;
518}
519
520static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
521{
522 struct usb_platdata *plat = dev_get_platdata(dev);
Adam Ford15287f02019-04-03 08:41:56 -0500523 enum usb_dr_mode dr_mode;
Peng Fan9e3eab32016-12-22 17:06:42 +0800524
Kever Yang1b807052020-03-04 08:59:50 +0800525 dr_mode = usb_get_dr_mode(dev->node);
Peng Fan9e3eab32016-12-22 17:06:42 +0800526
Adam Ford15287f02019-04-03 08:41:56 -0500527 switch (dr_mode) {
528 case USB_DR_MODE_HOST:
529 plat->init_type = USB_INIT_HOST;
530 break;
531 case USB_DR_MODE_PERIPHERAL:
532 plat->init_type = USB_INIT_DEVICE;
533 break;
534 case USB_DR_MODE_OTG:
535 case USB_DR_MODE_UNKNOWN:
536 return ehci_usb_phy_mode(dev);
537 };
Peng Fan9e3eab32016-12-22 17:06:42 +0800538
Adam Ford15287f02019-04-03 08:41:56 -0500539 return 0;
Peng Fan9e3eab32016-12-22 17:06:42 +0800540}
541
Marek Vasutd93dda72019-06-24 19:05:47 +0200542static int ehci_usb_bind(struct udevice *dev)
543{
544 /*
545 * TODO:
546 * This driver is only partly converted to DT probing and still uses
547 * a tremendous amount of hard-coded addresses. To make things worse,
548 * the driver depends on specific sequential indexing of controllers,
549 * from which it derives offsets in the PHY and ANATOP register sets.
550 *
551 * Here we attempt to calculate these indexes from DT information as
Igor Opaniuk9c24d012019-10-10 16:09:35 +0300552 * well as we can. The USB controllers on all existing iMX6 SoCs
553 * are placed next to each other, at addresses incremented by 0x200,
554 * and iMX7 their addresses are shifted by 0x10000.
555 * Thus, the index is derived from the multiple of 0x200 (0x10000 for
556 * iMX7) offset from the first controller address.
Marek Vasutd93dda72019-06-24 19:05:47 +0200557 *
558 * However, to complete conversion of this driver to DT probing, the
559 * following has to be done:
560 * - DM clock framework support for iMX must be implemented
561 * - usb_power_config() has to be converted to clock framework
562 * -> Thus, the ad-hoc "index" variable goes away.
563 * - USB PHY handling has to be factored out into separate driver
564 * -> Thus, the ad-hoc "index" variable goes away from the PHY
565 * code, the PHY driver must parse it's address from DT. This
566 * USB driver must find the PHY driver via DT phandle.
567 * -> usb_power_config() shall be moved to PHY driver
568 * With these changes in place, the ad-hoc indexing goes away and
569 * the driver is fully converted to DT probing.
570 */
Igor Opaniuk9c24d012019-10-10 16:09:35 +0300571 u32 controller_spacing = is_mx7() ? 0x10000 : 0x200;
572 fdt_addr_t addr = devfdt_get_addr_index(dev, 0);
Marek Vasutd93dda72019-06-24 19:05:47 +0200573
Igor Opaniuk9c24d012019-10-10 16:09:35 +0300574 dev->req_seq = (addr - USB_BASE_ADDR) / controller_spacing;
Marek Vasutd93dda72019-06-24 19:05:47 +0200575
576 return 0;
577}
578
Peng Fan5c363c12016-06-17 14:19:27 +0800579static int ehci_usb_probe(struct udevice *dev)
580{
581 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassba1dea42017-05-17 17:18:05 -0600582 struct usb_ehci *ehci = (struct usb_ehci *)devfdt_get_addr(dev);
Peng Fan5c363c12016-06-17 14:19:27 +0800583 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
Peng Fan13351332016-12-22 17:06:43 +0800584 enum usb_init_type type = plat->init_type;
Peng Fan5c363c12016-06-17 14:19:27 +0800585 struct ehci_hccr *hccr;
586 struct ehci_hcor *hcor;
587 int ret;
588
Peng Fanf8b27192020-05-01 22:08:36 +0800589 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
590 if (usb_fused((ulong)ehci)) {
591 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
592 (ulong)ehci);
593 return -ENODEV;
594 }
595 }
596
Peng Fan5c363c12016-06-17 14:19:27 +0800597 priv->ehci = ehci;
598 priv->portnr = dev->seq;
Peng Fan13351332016-12-22 17:06:43 +0800599 priv->init_type = type;
600
Abel Vesa888a9462019-02-01 16:40:08 +0000601#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800602 ret = device_get_supply_regulator(dev, "vbus-supply",
603 &priv->vbus_supply);
604 if (ret)
605 debug("%s: No vbus supply\n", dev->name);
Abel Vesa888a9462019-02-01 16:40:08 +0000606#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800607 ret = ehci_mx6_common_init(ehci, priv->portnr);
608 if (ret)
609 return ret;
610
Abel Vesa888a9462019-02-01 16:40:08 +0000611#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800612 if (priv->vbus_supply) {
613 ret = regulator_set_enable(priv->vbus_supply,
614 (type == USB_INIT_DEVICE) ?
615 false : true);
616 if (ret) {
617 puts("Error enabling VBUS supply\n");
618 return ret;
619 }
620 }
Abel Vesa888a9462019-02-01 16:40:08 +0000621#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800622
623 if (priv->init_type == USB_INIT_HOST) {
624 setbits_le32(&ehci->usbmode, CM_HOST);
625 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
626 setbits_le32(&ehci->portsc, USB_EN);
627 }
628
629 mdelay(10);
630
631 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
632 hcor = (struct ehci_hcor *)((uint32_t)hccr +
633 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
634
635 return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
636}
637
Peng Fan5c363c12016-06-17 14:19:27 +0800638static const struct udevice_id mx6_usb_ids[] = {
639 { .compatible = "fsl,imx27-usb" },
640 { }
641};
642
643U_BOOT_DRIVER(usb_mx6) = {
644 .name = "ehci_mx6",
645 .id = UCLASS_USB,
646 .of_match = mx6_usb_ids,
Peng Fan9e3eab32016-12-22 17:06:42 +0800647 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
Marek Vasutd93dda72019-06-24 19:05:47 +0200648 .bind = ehci_usb_bind,
Peng Fan5c363c12016-06-17 14:19:27 +0800649 .probe = ehci_usb_probe,
Masahiro Yamadad41919b2016-09-06 22:17:34 +0900650 .remove = ehci_deregister,
Peng Fan5c363c12016-06-17 14:19:27 +0800651 .ops = &ehci_usb_ops,
652 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
653 .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data),
654 .flags = DM_FLAG_ALLOC_PRIV_DMA,
655};
656#endif