blob: aeea5399995f55d83a706590f5ec22312ca46566 [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>
Simon Glass3ba929a2020-10-30 21:38:53 -060012#include <asm/global_data.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000013#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060014#include <linux/delay.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020015#include <usb/ehci-ci.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000016#include <asm/io.h>
17#include <asm/arch/imx-regs.h>
18#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020019#include <asm/mach-imx/iomux-v3.h>
20#include <asm/mach-imx/sys_proto.h>
Peng Fan5c363c12016-06-17 14:19:27 +080021#include <dm.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060022#include <asm/mach-types.h>
Peng Fan13351332016-12-22 17:06:43 +080023#include <power/regulator.h>
Adam Ford15287f02019-04-03 08:41:56 -050024#include <linux/usb/otg.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000025
26#include "ehci.h"
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000027
Peng Fan9e3eab32016-12-22 17:06:42 +080028DECLARE_GLOBAL_DATA_PTR;
29
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000030#define USB_OTGREGS_OFFSET 0x000
31#define USB_H1REGS_OFFSET 0x200
32#define USB_H2REGS_OFFSET 0x400
33#define USB_H3REGS_OFFSET 0x600
34#define USB_OTHERREGS_OFFSET 0x800
35
36#define USB_H1_CTRL_OFFSET 0x04
37
38#define USBPHY_CTRL 0x00000030
39#define USBPHY_CTRL_SET 0x00000034
40#define USBPHY_CTRL_CLR 0x00000038
41#define USBPHY_CTRL_TOG 0x0000003c
42
43#define USBPHY_PWD 0x00000000
44#define USBPHY_CTRL_SFTRST 0x80000000
45#define USBPHY_CTRL_CLKGATE 0x40000000
46#define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
47#define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
Troy Kiskyed72a9e2013-10-10 15:27:59 -070048#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000049
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000050#define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
51#define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
52
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000053#define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
54#define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
55#define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
56#define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
57
Adrian Alonsof31599f2015-08-06 15:43:17 -050058#define USBNC_OFFSET 0x200
Peng Fan9e3eab32016-12-22 17:06:42 +080059#define USBNC_PHY_STATUS_OFFSET 0x23C
Adrian Alonsof31599f2015-08-06 15:43:17 -050060#define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
61#define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
Stefan Agner475cf912016-07-13 00:25:37 -070062#define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000063#define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
64#define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
65
66/* USBCMD */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000067#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
68#define UCMD_RESET (1 << 1) /* controller reset */
69
Ye Li9da57ea2019-10-24 10:29:32 -030070#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Troy Kiskyed72a9e2013-10-10 15:27:59 -070071static const unsigned phy_bases[] = {
72 USB_PHY0_BASE_ADDR,
Ye Li9da57ea2019-10-24 10:29:32 -030073#if defined(USB_PHY1_BASE_ADDR)
Troy Kiskyed72a9e2013-10-10 15:27:59 -070074 USB_PHY1_BASE_ADDR,
Ye Li9da57ea2019-10-24 10:29:32 -030075#endif
Troy Kiskyed72a9e2013-10-10 15:27:59 -070076};
77
78static void usb_internal_phy_clock_gate(int index, int on)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000079{
Troy Kiskyed72a9e2013-10-10 15:27:59 -070080 void __iomem *phy_reg;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000081
Troy Kiskyed72a9e2013-10-10 15:27:59 -070082 if (index >= ARRAY_SIZE(phy_bases))
83 return;
84
85 phy_reg = (void __iomem *)phy_bases[index];
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000086 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
Adrian Alonsoaee79b42015-08-06 15:43:15 -050087 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000088}
89
Troy Kiskyed72a9e2013-10-10 15:27:59 -070090static void usb_power_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000091{
Ye Li9da57ea2019-10-24 10:29:32 -030092#if defined(CONFIG_MX7ULP)
93 struct usbphy_regs __iomem *usbphy =
94 (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR;
95
96 if (index > 0)
97 return;
98
99 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
100 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
101 &usbphy->usb1_chrg_detect);
102
103 scg_enable_usb_pll(true);
104
105#else
Wolfgang Grandegger5d113ca2012-05-02 04:36:39 +0000106 struct anatop_regs __iomem *anatop =
107 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700108 void __iomem *chrg_detect;
109 void __iomem *pll_480_ctrl_clr;
110 void __iomem *pll_480_ctrl_set;
111
112 switch (index) {
113 case 0:
114 chrg_detect = &anatop->usb1_chrg_detect;
115 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
116 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
117 break;
118 case 1:
119 chrg_detect = &anatop->usb2_chrg_detect;
120 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
121 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
122 break;
123 default:
124 return;
125 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000126 /*
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700127 * Some phy and power's special controls
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000128 * 1. The external charger detector needs to be disabled
129 * or the signal at DP will be poor
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700130 * 2. The PLL's power and output to usb
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000131 * is totally controlled by IC, so the Software only needs
132 * to enable them at initializtion.
133 */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500134 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000135 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700136 chrg_detect);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000137
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500138 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700139 pll_480_ctrl_clr);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000140
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500141 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000142 ANADIG_USB2_PLL_480_CTRL_POWER |
143 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700144 pll_480_ctrl_set);
Ye Li9da57ea2019-10-24 10:29:32 -0300145
146#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000147}
148
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700149/* Return 0 : host node, <>0 : device mode */
150static int usb_phy_enable(int index, struct usb_ehci *ehci)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000151{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700152 void __iomem *phy_reg;
153 void __iomem *phy_ctrl;
154 void __iomem *usb_cmd;
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500155 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000156
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700157 if (index >= ARRAY_SIZE(phy_bases))
158 return 0;
159
160 phy_reg = (void __iomem *)phy_bases[index];
161 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
162 usb_cmd = (void __iomem *)&ehci->usbcmd;
163
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000164 /* Stop then Reset */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500165 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100166 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500167 if (ret)
168 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000169
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500170 setbits_le32(usb_cmd, UCMD_RESET);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100171 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500172 if (ret)
173 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000174
175 /* Reset USBPHY module */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500176 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000177 udelay(10);
178
179 /* Remove CLKGATE and SFTRST */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500180 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000181 udelay(10);
182
183 /* Power up the PHY */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500184 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000185 /* enable FS/LS device */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500186 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
187 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000188
Peng Fan220402e2014-11-10 08:50:39 +0800189 return 0;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000190}
191
Adrian Alonsof31599f2015-08-06 15:43:17 -0500192int usb_phy_mode(int port)
193{
194 void __iomem *phy_reg;
195 void __iomem *phy_ctrl;
196 u32 val;
197
198 phy_reg = (void __iomem *)phy_bases[port];
199 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
200
201 val = readl(phy_ctrl);
202
203 if (val & USBPHY_CTRL_OTG_ID)
204 return USB_INIT_DEVICE;
205 else
206 return USB_INIT_HOST;
207}
208
Ye Li9da57ea2019-10-24 10:29:32 -0300209#if defined(CONFIG_MX7ULP)
210struct usbnc_regs {
211 u32 ctrl1;
212 u32 ctrl2;
213 u32 reserve0[2];
214 u32 hsic_ctrl;
215};
216#else
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700217/* Base address for this IP block is 0x02184800 */
218struct usbnc_regs {
219 u32 ctrl[4]; /* otg/host1-3 */
220 u32 uh2_hsic_ctrl;
221 u32 uh3_hsic_ctrl;
222 u32 otg_phy_ctrl_0;
223 u32 uh1_phy_ctrl_0;
224};
Ye Li9da57ea2019-10-24 10:29:32 -0300225#endif
226
Adrian Alonsof31599f2015-08-06 15:43:17 -0500227#elif defined(CONFIG_MX7)
228struct usbnc_regs {
229 u32 ctrl1;
230 u32 ctrl2;
231 u32 reserve1[10];
232 u32 phy_cfg1;
233 u32 phy_cfg2;
Peng Fan13186cf2016-06-20 09:43:08 +0800234 u32 reserve2;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500235 u32 phy_status;
Peng Fan13186cf2016-06-20 09:43:08 +0800236 u32 reserve3[4];
Adrian Alonsof31599f2015-08-06 15:43:17 -0500237 u32 adp_cfg1;
238 u32 adp_cfg2;
239 u32 adp_status;
240};
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700241
Adrian Alonsof31599f2015-08-06 15:43:17 -0500242static void usb_power_config(int index)
243{
244 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
245 (0x10000 * index) + USBNC_OFFSET);
246 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
247
Peng Fan928d3d02016-06-20 09:43:09 +0800248 /*
249 * Clear the ACAENB to enable usb_otg_id detection,
250 * otherwise it is the ACA detection enabled.
251 */
252 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
Adrian Alonsof31599f2015-08-06 15:43:17 -0500253}
254
255int usb_phy_mode(int port)
256{
257 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
258 (0x10000 * port) + USBNC_OFFSET);
259 void __iomem *status = (void __iomem *)(&usbnc->phy_status);
260 u32 val;
261
262 val = readl(status);
263
264 if (val & USBNC_PHYSTATUS_ID_DIG)
265 return USB_INIT_DEVICE;
266 else
267 return USB_INIT_HOST;
268}
269#endif
270
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700271static void usb_oc_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000272{
Adrian Alonsof31599f2015-08-06 15:43:17 -0500273#if defined(CONFIG_MX6)
Ye.Lif93453a2014-09-15 17:23:14 +0800274 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700275 USB_OTHERREGS_OFFSET);
276 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
Ye Li9da57ea2019-10-24 10:29:32 -0300277#elif defined(CONFIG_MX7) || defined(CONFIG_MX7ULP)
Adrian Alonsof31599f2015-08-06 15:43:17 -0500278 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
279 (0x10000 * index) + USBNC_OFFSET);
280 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
281#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000282
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000283#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
284 /* mx6qarm2 seems to required a different setting*/
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500285 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000286#else
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500287 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000288#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000289
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500290 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
Ye Li9da57ea2019-10-24 10:29:32 -0300291
292 /* Set power polarity to high active */
293#ifdef CONFIG_MXC_USB_OTG_HACTIVE
294 setbits_le32(ctrl, UCTRL_PWR_POL);
295#else
296 clrbits_le32(ctrl, UCTRL_PWR_POL);
297#endif
Peng Fan220402e2014-11-10 08:50:39 +0800298}
299
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500300/**
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700301 * board_usb_phy_mode - override usb phy mode
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500302 * @port: usb host/otg port
303 *
304 * Target board specific, override usb_phy_mode.
305 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
306 * left disconnected in this case usb_phy_mode will not be able to identify
307 * the phy mode that usb port is used.
308 * Machine file overrides board_usb_phy_mode.
309 *
310 * Return: USB_INIT_DEVICE or USB_INIT_HOST
311 */
Peng Fan220402e2014-11-10 08:50:39 +0800312int __weak board_usb_phy_mode(int port)
313{
314 return usb_phy_mode(port);
315}
316
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500317/**
318 * board_ehci_hcd_init - set usb vbus voltage
319 * @port: usb otg port
320 *
321 * Target board specific, setup iomux pad to setup supply vbus voltage
322 * for usb otg port. Machine board file overrides board_ehci_hcd_init
323 *
324 * Return: 0 Success
325 */
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000326int __weak board_ehci_hcd_init(int port)
327{
328 return 0;
329}
330
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500331/**
332 * board_ehci_power - enables/disables usb vbus voltage
333 * @port: usb otg port
334 * @on: on/off vbus voltage
335 *
336 * Enables/disables supply vbus voltage for usb otg port.
337 * Machine board file overrides board_ehci_power
338 *
339 * Return: 0 Success
340 */
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700341int __weak board_ehci_power(int port, int on)
342{
343 return 0;
344}
345
Peng Fan5c363c12016-06-17 14:19:27 +0800346int ehci_mx6_common_init(struct usb_ehci *ehci, int index)
347{
348 int ret;
349
350 enable_usboh3_clk(1);
351 mdelay(1);
352
353 /* Do board specific initialization */
354 ret = board_ehci_hcd_init(index);
355 if (ret)
356 return ret;
357
358 usb_power_config(index);
359 usb_oc_config(index);
360
Ye Li9da57ea2019-10-24 10:29:32 -0300361#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Peng Fan5c363c12016-06-17 14:19:27 +0800362 usb_internal_phy_clock_gate(index, 1);
363 usb_phy_enable(index, ehci);
364#endif
365
366 return 0;
367}
368
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100369#if !CONFIG_IS_ENABLED(DM_USB)
Troy Kisky7d6bbb92013-10-10 15:27:57 -0700370int ehci_hcd_init(int index, enum usb_init_type init,
371 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000372{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700373 enum usb_init_type type;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500374#if defined(CONFIG_MX6)
375 u32 controller_spacing = 0x200;
Ye Li9da57ea2019-10-24 10:29:32 -0300376#elif defined(CONFIG_MX7) || defined(CONFIG_MX7ULP)
Adrian Alonsof31599f2015-08-06 15:43:17 -0500377 u32 controller_spacing = 0x10000;
378#endif
Ye.Lif93453a2014-09-15 17:23:14 +0800379 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
Adrian Alonsof31599f2015-08-06 15:43:17 -0500380 (controller_spacing * index));
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700381 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000382
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700383 if (index > 3)
384 return -EINVAL;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000385
Peng Fanf8b27192020-05-01 22:08:36 +0800386 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
387 if (usb_fused((ulong)ehci)) {
388 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
389 (ulong)ehci);
390 return -ENODEV;
391 }
392 }
393
Peng Fan5c363c12016-06-17 14:19:27 +0800394 ret = ehci_mx6_common_init(ehci, index);
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700395 if (ret)
396 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000397
Peng Fan220402e2014-11-10 08:50:39 +0800398 type = board_usb_phy_mode(index);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000399
Peng Fan5c363c12016-06-17 14:19:27 +0800400 if (hccr && hcor) {
401 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
402 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
403 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
404 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000405
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700406 if ((type == init) || (type == USB_INIT_DEVICE))
407 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
408 if (type != init)
409 return -ENODEV;
410 if (type == USB_INIT_DEVICE)
411 return 0;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500412
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700413 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500414 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000415 setbits_le32(&ehci->portsc, USB_EN);
416
417 mdelay(10);
418
419 return 0;
420}
421
Lucas Stach3494a4c2012-09-26 00:14:35 +0200422int ehci_hcd_stop(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000423{
Peng Fan5c363c12016-06-17 14:19:27 +0800424 return 0;
425}
426#else
427struct ehci_mx6_priv_data {
428 struct ehci_ctrl ctrl;
429 struct usb_ehci *ehci;
Peng Fan13351332016-12-22 17:06:43 +0800430 struct udevice *vbus_supply;
Peng Fan5c363c12016-06-17 14:19:27 +0800431 enum usb_init_type init_type;
432 int portnr;
433};
434
435static int mx6_init_after_reset(struct ehci_ctrl *dev)
436{
437 struct ehci_mx6_priv_data *priv = dev->priv;
438 enum usb_init_type type = priv->init_type;
439 struct usb_ehci *ehci = priv->ehci;
440 int ret;
441
442 ret = ehci_mx6_common_init(priv->ehci, priv->portnr);
443 if (ret)
444 return ret;
445
Abel Vesa888a9462019-02-01 16:40:08 +0000446#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800447 if (priv->vbus_supply) {
448 ret = regulator_set_enable(priv->vbus_supply,
449 (type == USB_INIT_DEVICE) ?
450 false : true);
Marek Vasut27370452020-05-21 23:32:23 +0200451 if (ret && ret != -ENOSYS) {
Marek Vasuta86d51a2020-05-21 23:34:06 +0200452 printf("Error enabling VBUS supply (ret=%i)\n", ret);
Peng Fan13351332016-12-22 17:06:43 +0800453 return ret;
454 }
455 }
Abel Vesa888a9462019-02-01 16:40:08 +0000456#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800457
458 if (type == USB_INIT_DEVICE)
459 return 0;
460
461 setbits_le32(&ehci->usbmode, CM_HOST);
462 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
463 setbits_le32(&ehci->portsc, USB_EN);
464
465 mdelay(10);
466
467 return 0;
468}
469
470static const struct ehci_ops mx6_ehci_ops = {
471 .init_after_reset = mx6_init_after_reset
472};
473
Peng Fan9e3eab32016-12-22 17:06:42 +0800474static int ehci_usb_phy_mode(struct udevice *dev)
475{
Simon Glassb75b15b2020-12-03 16:55:23 -0700476 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900477 void *__iomem addr = dev_read_addr_ptr(dev);
Peng Fan9e3eab32016-12-22 17:06:42 +0800478 void *__iomem phy_ctrl, *__iomem phy_status;
479 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700480 int offset = dev_of_offset(dev), phy_off;
Peng Fan9e3eab32016-12-22 17:06:42 +0800481 u32 val;
482
483 /*
484 * About fsl,usbphy, Refer to
485 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
486 */
Ye Li9da57ea2019-10-24 10:29:32 -0300487 if (is_mx6() || is_mx7ulp()) {
Peng Fan9e3eab32016-12-22 17:06:42 +0800488 phy_off = fdtdec_lookup_phandle(blob,
489 offset,
490 "fsl,usbphy");
491 if (phy_off < 0)
492 return -EINVAL;
493
494 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
495 "reg");
496 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
497 return -EINVAL;
498
499 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
500 val = readl(phy_ctrl);
501
502 if (val & USBPHY_CTRL_OTG_ID)
503 plat->init_type = USB_INIT_DEVICE;
504 else
505 plat->init_type = USB_INIT_HOST;
506 } else if (is_mx7()) {
507 phy_status = (void __iomem *)(addr +
508 USBNC_PHY_STATUS_OFFSET);
509 val = readl(phy_status);
510
511 if (val & USBNC_PHYSTATUS_ID_DIG)
512 plat->init_type = USB_INIT_DEVICE;
513 else
514 plat->init_type = USB_INIT_HOST;
515 } else {
516 return -EINVAL;
517 }
518
519 return 0;
520}
521
Simon Glassaad29ae2020-12-03 16:55:21 -0700522static int ehci_usb_of_to_plat(struct udevice *dev)
Peng Fan9e3eab32016-12-22 17:06:42 +0800523{
Simon Glassb75b15b2020-12-03 16:55:23 -0700524 struct usb_plat *plat = dev_get_plat(dev);
Adam Ford15287f02019-04-03 08:41:56 -0500525 enum usb_dr_mode dr_mode;
Peng Fan9e3eab32016-12-22 17:06:42 +0800526
Simon Glassa7ece582020-12-19 10:40:14 -0700527 dr_mode = usb_get_dr_mode(dev_ofnode(dev));
Peng Fan9e3eab32016-12-22 17:06:42 +0800528
Adam Ford15287f02019-04-03 08:41:56 -0500529 switch (dr_mode) {
530 case USB_DR_MODE_HOST:
531 plat->init_type = USB_INIT_HOST;
532 break;
533 case USB_DR_MODE_PERIPHERAL:
534 plat->init_type = USB_INIT_DEVICE;
535 break;
536 case USB_DR_MODE_OTG:
537 case USB_DR_MODE_UNKNOWN:
538 return ehci_usb_phy_mode(dev);
539 };
Peng Fan9e3eab32016-12-22 17:06:42 +0800540
Adam Ford15287f02019-04-03 08:41:56 -0500541 return 0;
Peng Fan9e3eab32016-12-22 17:06:42 +0800542}
543
Marek Vasutd93dda72019-06-24 19:05:47 +0200544static int ehci_usb_bind(struct udevice *dev)
545{
546 /*
547 * TODO:
548 * This driver is only partly converted to DT probing and still uses
549 * a tremendous amount of hard-coded addresses. To make things worse,
550 * the driver depends on specific sequential indexing of controllers,
551 * from which it derives offsets in the PHY and ANATOP register sets.
552 *
553 * Here we attempt to calculate these indexes from DT information as
Igor Opaniuk9c24d012019-10-10 16:09:35 +0300554 * well as we can. The USB controllers on all existing iMX6 SoCs
555 * are placed next to each other, at addresses incremented by 0x200,
556 * and iMX7 their addresses are shifted by 0x10000.
557 * Thus, the index is derived from the multiple of 0x200 (0x10000 for
558 * iMX7) offset from the first controller address.
Marek Vasutd93dda72019-06-24 19:05:47 +0200559 *
560 * However, to complete conversion of this driver to DT probing, the
561 * following has to be done:
562 * - DM clock framework support for iMX must be implemented
563 * - usb_power_config() has to be converted to clock framework
564 * -> Thus, the ad-hoc "index" variable goes away.
565 * - USB PHY handling has to be factored out into separate driver
566 * -> Thus, the ad-hoc "index" variable goes away from the PHY
567 * code, the PHY driver must parse it's address from DT. This
568 * USB driver must find the PHY driver via DT phandle.
569 * -> usb_power_config() shall be moved to PHY driver
570 * With these changes in place, the ad-hoc indexing goes away and
571 * the driver is fully converted to DT probing.
572 */
Marek Vasutd93dda72019-06-24 19:05:47 +0200573
Simon Glasscb4363a2020-12-16 21:20:20 -0700574 /*
575 * FIXME: This cannot work with the new sequence numbers.
576 * Please complete the DM conversion.
577 *
578 * u32 controller_spacing = is_mx7() ? 0x10000 : 0x200;
579 * fdt_addr_t addr = devfdt_get_addr_index(dev, 0);
580 *
581 * dev->req_seq = (addr - USB_BASE_ADDR) / controller_spacing;
582 */
Marek Vasutd93dda72019-06-24 19:05:47 +0200583
584 return 0;
585}
586
Peng Fan5c363c12016-06-17 14:19:27 +0800587static int ehci_usb_probe(struct udevice *dev)
588{
Simon Glassb75b15b2020-12-03 16:55:23 -0700589 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900590 struct usb_ehci *ehci = dev_read_addr_ptr(dev);
Peng Fan5c363c12016-06-17 14:19:27 +0800591 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
Peng Fan13351332016-12-22 17:06:43 +0800592 enum usb_init_type type = plat->init_type;
Peng Fan5c363c12016-06-17 14:19:27 +0800593 struct ehci_hccr *hccr;
594 struct ehci_hcor *hcor;
595 int ret;
596
Peng Fanf8b27192020-05-01 22:08:36 +0800597 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
598 if (usb_fused((ulong)ehci)) {
599 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
600 (ulong)ehci);
601 return -ENODEV;
602 }
603 }
604
Peng Fan5c363c12016-06-17 14:19:27 +0800605 priv->ehci = ehci;
Simon Glass75e534b2020-12-16 21:20:07 -0700606 priv->portnr = dev_seq(dev);
Peng Fan13351332016-12-22 17:06:43 +0800607 priv->init_type = type;
608
Abel Vesa888a9462019-02-01 16:40:08 +0000609#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800610 ret = device_get_supply_regulator(dev, "vbus-supply",
611 &priv->vbus_supply);
612 if (ret)
613 debug("%s: No vbus supply\n", dev->name);
Abel Vesa888a9462019-02-01 16:40:08 +0000614#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800615 ret = ehci_mx6_common_init(ehci, priv->portnr);
616 if (ret)
617 return ret;
618
Abel Vesa888a9462019-02-01 16:40:08 +0000619#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800620 if (priv->vbus_supply) {
621 ret = regulator_set_enable(priv->vbus_supply,
622 (type == USB_INIT_DEVICE) ?
623 false : true);
Marek Vasut27370452020-05-21 23:32:23 +0200624 if (ret && ret != -ENOSYS) {
Marek Vasuta86d51a2020-05-21 23:34:06 +0200625 printf("Error enabling VBUS supply (ret=%i)\n", ret);
Peng Fan13351332016-12-22 17:06:43 +0800626 return ret;
627 }
628 }
Abel Vesa888a9462019-02-01 16:40:08 +0000629#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800630
631 if (priv->init_type == USB_INIT_HOST) {
632 setbits_le32(&ehci->usbmode, CM_HOST);
633 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
634 setbits_le32(&ehci->portsc, USB_EN);
635 }
636
637 mdelay(10);
638
639 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
640 hcor = (struct ehci_hcor *)((uint32_t)hccr +
641 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
642
643 return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
644}
645
Peng Fan5c363c12016-06-17 14:19:27 +0800646static const struct udevice_id mx6_usb_ids[] = {
647 { .compatible = "fsl,imx27-usb" },
648 { }
649};
650
651U_BOOT_DRIVER(usb_mx6) = {
652 .name = "ehci_mx6",
653 .id = UCLASS_USB,
654 .of_match = mx6_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700655 .of_to_plat = ehci_usb_of_to_plat,
Marek Vasutd93dda72019-06-24 19:05:47 +0200656 .bind = ehci_usb_bind,
Peng Fan5c363c12016-06-17 14:19:27 +0800657 .probe = ehci_usb_probe,
Masahiro Yamadad41919b2016-09-06 22:17:34 +0900658 .remove = ehci_deregister,
Peng Fan5c363c12016-06-17 14:19:27 +0800659 .ops = &ehci_usb_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700660 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700661 .priv_auto = sizeof(struct ehci_mx6_priv_data),
Peng Fan5c363c12016-06-17 14:19:27 +0800662 .flags = DM_FLAG_ALLOC_PRIV_DMA,
663};
664#endif