blob: 68f6ecc87223d0a073c489c8737bda59dac5b80b [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>
Marek Vasutf22fde72021-03-31 12:28:03 +02008#include <clk.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000010#include <usb.h>
11#include <errno.h>
Mateusz Kulikowski4073b832016-01-23 11:54:32 +010012#include <wait_bit.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000014#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060015#include <linux/delay.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020016#include <usb/ehci-ci.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000017#include <asm/io.h>
18#include <asm/arch/imx-regs.h>
19#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020020#include <asm/mach-imx/iomux-v3.h>
21#include <asm/mach-imx/sys_proto.h>
Peng Fan5c363c12016-06-17 14:19:27 +080022#include <dm.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060023#include <asm/mach-types.h>
Peng Fan13351332016-12-22 17:06:43 +080024#include <power/regulator.h>
Adam Ford15287f02019-04-03 08:41:56 -050025#include <linux/usb/otg.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000026
27#include "ehci.h"
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000028
Peng Fan9e3eab32016-12-22 17:06:42 +080029DECLARE_GLOBAL_DATA_PTR;
30
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000031#define USB_OTGREGS_OFFSET 0x000
32#define USB_H1REGS_OFFSET 0x200
33#define USB_H2REGS_OFFSET 0x400
34#define USB_H3REGS_OFFSET 0x600
35#define USB_OTHERREGS_OFFSET 0x800
36
37#define USB_H1_CTRL_OFFSET 0x04
38
39#define USBPHY_CTRL 0x00000030
40#define USBPHY_CTRL_SET 0x00000034
41#define USBPHY_CTRL_CLR 0x00000038
42#define USBPHY_CTRL_TOG 0x0000003c
43
44#define USBPHY_PWD 0x00000000
45#define USBPHY_CTRL_SFTRST 0x80000000
46#define USBPHY_CTRL_CLKGATE 0x40000000
47#define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
48#define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
Troy Kiskyed72a9e2013-10-10 15:27:59 -070049#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000050
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000051#define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
52#define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
53
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000054#define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
55#define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
56#define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
57#define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
58
Adrian Alonsof31599f2015-08-06 15:43:17 -050059#define USBNC_OFFSET 0x200
Peng Fan9e3eab32016-12-22 17:06:42 +080060#define USBNC_PHY_STATUS_OFFSET 0x23C
Adrian Alonsof31599f2015-08-06 15:43:17 -050061#define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
62#define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
Stefan Agner475cf912016-07-13 00:25:37 -070063#define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000064#define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
65#define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
66
67/* USBCMD */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000068#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
69#define UCMD_RESET (1 << 1) /* controller reset */
70
Marek Vasut793e5752021-03-31 22:19:00 +020071/* Base address for this IP block is 0x02184800 */
72struct usbnc_regs {
73 u32 ctrl[4]; /* otg/host1-3 */
74 u32 uh2_hsic_ctrl;
75 u32 uh3_hsic_ctrl;
76 u32 otg_phy_ctrl_0;
77 u32 uh1_phy_ctrl_0;
78 u32 reserve1[4];
79 u32 phy_cfg1;
80 u32 phy_cfg2;
81 u32 reserve2;
82 u32 phy_status;
83 u32 reserve3[4];
84 u32 adp_cfg1;
85 u32 adp_cfg2;
86 u32 adp_status;
87};
88
Marek Vasut41e81392021-03-31 23:00:23 +020089#if defined(CONFIG_MX6) && !defined(CONFIG_PHY)
90static void usb_power_config_mx6(struct anatop_regs __iomem *anatop,
91 int anatop_bits_index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000092{
Troy Kiskyed72a9e2013-10-10 15:27:59 -070093 void __iomem *chrg_detect;
94 void __iomem *pll_480_ctrl_clr;
95 void __iomem *pll_480_ctrl_set;
96
Marek Vasut41e81392021-03-31 23:00:23 +020097 if (!is_mx6())
98 return;
99
100 switch (anatop_bits_index) {
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700101 case 0:
102 chrg_detect = &anatop->usb1_chrg_detect;
103 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
104 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
105 break;
106 case 1:
107 chrg_detect = &anatop->usb2_chrg_detect;
108 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
109 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
110 break;
111 default:
112 return;
113 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000114 /*
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700115 * Some phy and power's special controls
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000116 * 1. The external charger detector needs to be disabled
117 * or the signal at DP will be poor
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700118 * 2. The PLL's power and output to usb
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000119 * is totally controlled by IC, so the Software only needs
120 * to enable them at initializtion.
121 */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500122 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000123 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700124 chrg_detect);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000125
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500126 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700127 pll_480_ctrl_clr);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000128
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500129 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000130 ANADIG_USB2_PLL_480_CTRL_POWER |
131 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700132 pll_480_ctrl_set);
Marek Vasut41e81392021-03-31 23:00:23 +0200133}
134#else
135static void __maybe_unused
136usb_power_config_mx6(void *anatop, int anatop_bits_index) { }
137#endif
Ye Li9da57ea2019-10-24 10:29:32 -0300138
Marek Vasut41e81392021-03-31 23:00:23 +0200139#if defined(CONFIG_MX7) && !defined(CONFIG_PHY)
140static void usb_power_config_mx7(struct usbnc_regs *usbnc)
141{
142 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
143
144 if (!is_mx7())
145 return;
146
147 /*
148 * Clear the ACAENB to enable usb_otg_id detection,
149 * otherwise it is the ACA detection enabled.
150 */
151 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
152}
153#else
154static void __maybe_unused
155usb_power_config_mx7(void *usbnc) { }
Ye Li9da57ea2019-10-24 10:29:32 -0300156#endif
Marek Vasut41e81392021-03-31 23:00:23 +0200157
158#if defined(CONFIG_MX7ULP) && !defined(CONFIG_PHY)
159static void usb_power_config_mx7ulp(struct usbphy_regs __iomem *usbphy)
160{
161 if (!is_mx7ulp())
162 return;
163
164 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
165 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
166 &usbphy->usb1_chrg_detect);
167
168 scg_enable_usb_pll(true);
169}
170#else
171static void __maybe_unused
172usb_power_config_mx7ulp(void *usbphy) { }
173#endif
174
175#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
176static const unsigned phy_bases[] = {
177 USB_PHY0_BASE_ADDR,
178#if defined(USB_PHY1_BASE_ADDR)
179 USB_PHY1_BASE_ADDR,
180#endif
181};
182
183#if !defined(CONFIG_PHY)
184static void usb_internal_phy_clock_gate(void __iomem *phy_reg, int on)
185{
186 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
187 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000188}
189
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700190/* Return 0 : host node, <>0 : device mode */
Marek Vasut6b6df642021-03-31 22:10:35 +0200191static int usb_phy_enable(struct usb_ehci *ehci, void __iomem *phy_reg)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000192{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700193 void __iomem *phy_ctrl;
194 void __iomem *usb_cmd;
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500195 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000196
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700197 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
198 usb_cmd = (void __iomem *)&ehci->usbcmd;
199
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000200 /* Stop then Reset */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500201 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100202 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500203 if (ret)
204 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000205
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500206 setbits_le32(usb_cmd, UCMD_RESET);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100207 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500208 if (ret)
209 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000210
211 /* Reset USBPHY module */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500212 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000213 udelay(10);
214
215 /* Remove CLKGATE and SFTRST */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500216 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000217 udelay(10);
218
219 /* Power up the PHY */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500220 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000221 /* enable FS/LS device */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500222 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
223 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000224
Peng Fan220402e2014-11-10 08:50:39 +0800225 return 0;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000226}
Marek Vasut41e81392021-03-31 23:00:23 +0200227#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000228
Adrian Alonsof31599f2015-08-06 15:43:17 -0500229int usb_phy_mode(int port)
230{
231 void __iomem *phy_reg;
232 void __iomem *phy_ctrl;
233 u32 val;
234
235 phy_reg = (void __iomem *)phy_bases[port];
236 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
237
238 val = readl(phy_ctrl);
239
240 if (val & USBPHY_CTRL_OTG_ID)
241 return USB_INIT_DEVICE;
242 else
243 return USB_INIT_HOST;
244}
245
Adrian Alonsof31599f2015-08-06 15:43:17 -0500246#elif defined(CONFIG_MX7)
Adrian Alonsof31599f2015-08-06 15:43:17 -0500247int usb_phy_mode(int port)
248{
249 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
250 (0x10000 * port) + USBNC_OFFSET);
251 void __iomem *status = (void __iomem *)(&usbnc->phy_status);
252 u32 val;
253
254 val = readl(status);
255
256 if (val & USBNC_PHYSTATUS_ID_DIG)
257 return USB_INIT_DEVICE;
258 else
259 return USB_INIT_HOST;
260}
261#endif
262
Marek Vasut1fa42432021-03-31 23:24:41 +0200263static void usb_oc_config(struct usbnc_regs *usbnc, int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000264{
Marek Vasut793e5752021-03-31 22:19:00 +0200265 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000266
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000267#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
268 /* mx6qarm2 seems to required a different setting*/
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500269 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000270#else
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500271 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000272#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000273
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500274 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
Ye Li9da57ea2019-10-24 10:29:32 -0300275
276 /* Set power polarity to high active */
277#ifdef CONFIG_MXC_USB_OTG_HACTIVE
278 setbits_le32(ctrl, UCTRL_PWR_POL);
279#else
280 clrbits_le32(ctrl, UCTRL_PWR_POL);
281#endif
Peng Fan220402e2014-11-10 08:50:39 +0800282}
283
Marek Vasut09dc0702021-03-31 21:40:24 +0200284#if !CONFIG_IS_ENABLED(DM_USB)
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500285/**
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700286 * board_usb_phy_mode - override usb phy mode
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500287 * @port: usb host/otg port
288 *
289 * Target board specific, override usb_phy_mode.
290 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
291 * left disconnected in this case usb_phy_mode will not be able to identify
292 * the phy mode that usb port is used.
293 * Machine file overrides board_usb_phy_mode.
294 *
295 * Return: USB_INIT_DEVICE or USB_INIT_HOST
296 */
Peng Fan220402e2014-11-10 08:50:39 +0800297int __weak board_usb_phy_mode(int port)
298{
299 return usb_phy_mode(port);
300}
301
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500302/**
303 * board_ehci_hcd_init - set usb vbus voltage
304 * @port: usb otg port
305 *
306 * Target board specific, setup iomux pad to setup supply vbus voltage
307 * for usb otg port. Machine board file overrides board_ehci_hcd_init
308 *
309 * Return: 0 Success
310 */
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000311int __weak board_ehci_hcd_init(int port)
312{
313 return 0;
314}
315
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500316/**
317 * board_ehci_power - enables/disables usb vbus voltage
318 * @port: usb otg port
319 * @on: on/off vbus voltage
320 *
321 * Enables/disables supply vbus voltage for usb otg port.
322 * Machine board file overrides board_ehci_power
323 *
324 * Return: 0 Success
325 */
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700326int __weak board_ehci_power(int port, int on)
327{
Peng Fan5c363c12016-06-17 14:19:27 +0800328 return 0;
329}
330
Troy Kisky7d6bbb92013-10-10 15:27:57 -0700331int ehci_hcd_init(int index, enum usb_init_type init,
332 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000333{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700334 enum usb_init_type type;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500335#if defined(CONFIG_MX6)
336 u32 controller_spacing = 0x200;
Marek Vasut41e81392021-03-31 23:00:23 +0200337 struct anatop_regs __iomem *anatop =
338 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Marek Vasut1fa42432021-03-31 23:24:41 +0200339 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
340 USB_OTHERREGS_OFFSET);
Marek Vasut41e81392021-03-31 23:00:23 +0200341#elif defined(CONFIG_MX7)
342 u32 controller_spacing = 0x10000;
343 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
344 (0x10000 * index) + USBNC_OFFSET);
345#elif defined(CONFIG_MX7ULP)
Adrian Alonsof31599f2015-08-06 15:43:17 -0500346 u32 controller_spacing = 0x10000;
Marek Vasut41e81392021-03-31 23:00:23 +0200347 struct usbphy_regs __iomem *usbphy =
348 (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR;
Marek Vasut1fa42432021-03-31 23:24:41 +0200349 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
350 (0x10000 * index) + USBNC_OFFSET);
Adrian Alonsof31599f2015-08-06 15:43:17 -0500351#endif
Ye.Lif93453a2014-09-15 17:23:14 +0800352 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
Adrian Alonsof31599f2015-08-06 15:43:17 -0500353 (controller_spacing * index));
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700354 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000355
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700356 if (index > 3)
357 return -EINVAL;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000358
Peng Fanf8b27192020-05-01 22:08:36 +0800359 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
360 if (usb_fused((ulong)ehci)) {
361 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
362 (ulong)ehci);
363 return -ENODEV;
364 }
365 }
366
Marek Vasutf22fde72021-03-31 12:28:03 +0200367 enable_usboh3_clk(1);
368 mdelay(1);
369
Marek Vasut09dc0702021-03-31 21:40:24 +0200370 /* Do board specific initialization */
371 ret = board_ehci_hcd_init(index);
372 if (ret) {
373 enable_usboh3_clk(0);
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700374 return ret;
Marek Vasut09dc0702021-03-31 21:40:24 +0200375 }
376
Marek Vasut41e81392021-03-31 23:00:23 +0200377#if defined(CONFIG_MX6)
378 usb_power_config_mx6(anatop, index);
379#elif defined (CONFIG_MX7)
380 usb_power_config_mx7(usbnc);
381#elif defined (CONFIG_MX7ULP)
382 usb_power_config_mx7ulp(usbphy);
383#endif
384
Marek Vasut1fa42432021-03-31 23:24:41 +0200385 usb_oc_config(usbnc, index);
Marek Vasut09dc0702021-03-31 21:40:24 +0200386
387#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Marek Vasut6b6df642021-03-31 22:10:35 +0200388 if (index < ARRAY_SIZE(phy_bases)) {
389 usb_internal_phy_clock_gate((void __iomem *)phy_bases[index], 1);
390 usb_phy_enable(ehci, (void __iomem *)phy_bases[index]);
391 }
Marek Vasut09dc0702021-03-31 21:40:24 +0200392#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000393
Peng Fan220402e2014-11-10 08:50:39 +0800394 type = board_usb_phy_mode(index);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000395
Peng Fan5c363c12016-06-17 14:19:27 +0800396 if (hccr && hcor) {
397 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
398 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
399 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
400 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000401
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700402 if ((type == init) || (type == USB_INIT_DEVICE))
403 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
404 if (type != init)
405 return -ENODEV;
406 if (type == USB_INIT_DEVICE)
407 return 0;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500408
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700409 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500410 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000411 setbits_le32(&ehci->portsc, USB_EN);
412
413 mdelay(10);
414
415 return 0;
416}
417
Lucas Stach3494a4c2012-09-26 00:14:35 +0200418int ehci_hcd_stop(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000419{
Peng Fan5c363c12016-06-17 14:19:27 +0800420 return 0;
421}
422#else
423struct ehci_mx6_priv_data {
424 struct ehci_ctrl ctrl;
425 struct usb_ehci *ehci;
Peng Fan13351332016-12-22 17:06:43 +0800426 struct udevice *vbus_supply;
Marek Vasutf22fde72021-03-31 12:28:03 +0200427 struct clk clk;
Peng Fan5c363c12016-06-17 14:19:27 +0800428 enum usb_init_type init_type;
429 int portnr;
Marek Vasutebaf3ae2021-03-31 23:06:07 +0200430 void __iomem *phy_addr;
431 void __iomem *misc_addr;
432 void __iomem *anatop_addr;
Peng Fan5c363c12016-06-17 14:19:27 +0800433};
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;
Peng Fan5c363c12016-06-17 14:19:27 +0800440
Marek Vasut41e81392021-03-31 23:00:23 +0200441#if !defined(CONFIG_PHY)
442 usb_power_config_mx6(priv->anatop_addr, priv->portnr);
443 usb_power_config_mx7(priv->misc_addr);
444 usb_power_config_mx7ulp(priv->phy_addr);
445#endif
446
Marek Vasut1fa42432021-03-31 23:24:41 +0200447 usb_oc_config(priv->misc_addr, priv->portnr);
Marek Vasut09dc0702021-03-31 21:40:24 +0200448
Marek Vasut41e81392021-03-31 23:00:23 +0200449#if !defined(CONFIG_PHY) && (defined(CONFIG_MX6) || defined(CONFIG_MX7ULP))
Marek Vasut6b6df642021-03-31 22:10:35 +0200450 usb_internal_phy_clock_gate(priv->phy_addr, 1);
451 usb_phy_enable(ehci, priv->phy_addr);
Marek Vasut09dc0702021-03-31 21:40:24 +0200452#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800453
Abel Vesa888a9462019-02-01 16:40:08 +0000454#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800455 if (priv->vbus_supply) {
Marek Vasut09dc0702021-03-31 21:40:24 +0200456 int ret;
Peng Fan13351332016-12-22 17:06:43 +0800457 ret = regulator_set_enable(priv->vbus_supply,
458 (type == USB_INIT_DEVICE) ?
459 false : true);
Marek Vasut27370452020-05-21 23:32:23 +0200460 if (ret && ret != -ENOSYS) {
Marek Vasuta86d51a2020-05-21 23:34:06 +0200461 printf("Error enabling VBUS supply (ret=%i)\n", ret);
Peng Fan13351332016-12-22 17:06:43 +0800462 return ret;
463 }
464 }
Abel Vesa888a9462019-02-01 16:40:08 +0000465#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800466
467 if (type == USB_INIT_DEVICE)
468 return 0;
469
470 setbits_le32(&ehci->usbmode, CM_HOST);
471 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
472 setbits_le32(&ehci->portsc, USB_EN);
473
474 mdelay(10);
475
476 return 0;
477}
478
479static const struct ehci_ops mx6_ehci_ops = {
480 .init_after_reset = mx6_init_after_reset
481};
482
Peng Fan9e3eab32016-12-22 17:06:42 +0800483static int ehci_usb_phy_mode(struct udevice *dev)
484{
Simon Glassb75b15b2020-12-03 16:55:23 -0700485 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900486 void *__iomem addr = dev_read_addr_ptr(dev);
Peng Fan9e3eab32016-12-22 17:06:42 +0800487 void *__iomem phy_ctrl, *__iomem phy_status;
488 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700489 int offset = dev_of_offset(dev), phy_off;
Peng Fan9e3eab32016-12-22 17:06:42 +0800490 u32 val;
491
492 /*
493 * About fsl,usbphy, Refer to
494 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
495 */
Ye Li9da57ea2019-10-24 10:29:32 -0300496 if (is_mx6() || is_mx7ulp()) {
Peng Fan9e3eab32016-12-22 17:06:42 +0800497 phy_off = fdtdec_lookup_phandle(blob,
498 offset,
499 "fsl,usbphy");
500 if (phy_off < 0)
501 return -EINVAL;
502
503 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
504 "reg");
505 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
506 return -EINVAL;
507
508 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
509 val = readl(phy_ctrl);
510
511 if (val & USBPHY_CTRL_OTG_ID)
512 plat->init_type = USB_INIT_DEVICE;
513 else
514 plat->init_type = USB_INIT_HOST;
515 } else if (is_mx7()) {
516 phy_status = (void __iomem *)(addr +
517 USBNC_PHY_STATUS_OFFSET);
518 val = readl(phy_status);
519
520 if (val & USBNC_PHYSTATUS_ID_DIG)
521 plat->init_type = USB_INIT_DEVICE;
522 else
523 plat->init_type = USB_INIT_HOST;
524 } else {
525 return -EINVAL;
526 }
527
528 return 0;
529}
530
Simon Glassaad29ae2020-12-03 16:55:21 -0700531static int ehci_usb_of_to_plat(struct udevice *dev)
Peng Fan9e3eab32016-12-22 17:06:42 +0800532{
Simon Glassb75b15b2020-12-03 16:55:23 -0700533 struct usb_plat *plat = dev_get_plat(dev);
Adam Ford15287f02019-04-03 08:41:56 -0500534 enum usb_dr_mode dr_mode;
Peng Fan9e3eab32016-12-22 17:06:42 +0800535
Simon Glassa7ece582020-12-19 10:40:14 -0700536 dr_mode = usb_get_dr_mode(dev_ofnode(dev));
Peng Fan9e3eab32016-12-22 17:06:42 +0800537
Adam Ford15287f02019-04-03 08:41:56 -0500538 switch (dr_mode) {
539 case USB_DR_MODE_HOST:
540 plat->init_type = USB_INIT_HOST;
541 break;
542 case USB_DR_MODE_PERIPHERAL:
543 plat->init_type = USB_INIT_DEVICE;
544 break;
545 case USB_DR_MODE_OTG:
546 case USB_DR_MODE_UNKNOWN:
547 return ehci_usb_phy_mode(dev);
548 };
Peng Fan9e3eab32016-12-22 17:06:42 +0800549
Adam Ford15287f02019-04-03 08:41:56 -0500550 return 0;
Peng Fan9e3eab32016-12-22 17:06:42 +0800551}
552
Marek Vasutebaf3ae2021-03-31 23:06:07 +0200553static int mx6_parse_dt_addrs(struct udevice *dev)
554{
555 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
556 int phy_off, misc_off;
557 const void *blob = gd->fdt_blob;
558 int offset = dev_of_offset(dev);
559 void *__iomem addr;
Marek Vasut5becd182021-04-02 13:56:28 +0200560 int ret, devnump;
Marek Vasutebaf3ae2021-03-31 23:06:07 +0200561
562 phy_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbphy");
563 if (phy_off < 0) {
564 phy_off = fdtdec_lookup_phandle(blob, offset, "phys");
565 if (phy_off < 0)
566 return -EINVAL;
567 }
568
Marek Vasut5becd182021-04-02 13:56:28 +0200569 ret = fdtdec_get_alias_seq(blob, dev->uclass->uc_drv->name,
570 phy_off, &devnump);
571 if (ret < 0)
572 return ret;
573
Marek Vasutebaf3ae2021-03-31 23:06:07 +0200574 misc_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbmisc");
575 if (misc_off < 0)
576 return -EINVAL;
577
578 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, "reg");
579 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
580 return -EINVAL;
581
582 priv->phy_addr = addr;
Marek Vasut5becd182021-04-02 13:56:28 +0200583 priv->portnr = devnump;
Marek Vasutebaf3ae2021-03-31 23:06:07 +0200584
585 addr = (void __iomem *)fdtdec_get_addr(blob, misc_off, "reg");
586 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
587 return -EINVAL;
588
589 priv->misc_addr = addr;
590
591#if !defined(CONFIG_PHY) && defined(CONFIG_MX6)
592 int anatop_off;
593
594 /* Resolve ANATOP offset through USB PHY node */
595 anatop_off = fdtdec_lookup_phandle(blob, phy_off, "fsl,anatop");
596 if (anatop_off < 0)
597 return -EINVAL;
598
599 addr = (void __iomem *)fdtdec_get_addr(blob, anatop_off, "reg");
600 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
601 return -EINVAL;
602
603 priv->anatop_addr = addr;
604#endif
605 return 0;
606}
607
Peng Fan5c363c12016-06-17 14:19:27 +0800608static int ehci_usb_probe(struct udevice *dev)
609{
Simon Glassb75b15b2020-12-03 16:55:23 -0700610 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900611 struct usb_ehci *ehci = dev_read_addr_ptr(dev);
Peng Fan5c363c12016-06-17 14:19:27 +0800612 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
Peng Fan13351332016-12-22 17:06:43 +0800613 enum usb_init_type type = plat->init_type;
Peng Fan5c363c12016-06-17 14:19:27 +0800614 struct ehci_hccr *hccr;
615 struct ehci_hcor *hcor;
616 int ret;
617
Peng Fanf8b27192020-05-01 22:08:36 +0800618 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
619 if (usb_fused((ulong)ehci)) {
620 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
621 (ulong)ehci);
622 return -ENODEV;
623 }
624 }
625
Marek Vasutebaf3ae2021-03-31 23:06:07 +0200626 ret = mx6_parse_dt_addrs(dev);
627 if (ret)
628 return ret;
629
Peng Fan5c363c12016-06-17 14:19:27 +0800630 priv->ehci = ehci;
Peng Fan13351332016-12-22 17:06:43 +0800631 priv->init_type = type;
632
Marek Vasutf22fde72021-03-31 12:28:03 +0200633#if CONFIG_IS_ENABLED(CLK)
634 ret = clk_get_by_index(dev, 0, &priv->clk);
635 if (ret < 0)
636 return ret;
637
638 ret = clk_enable(&priv->clk);
639 if (ret)
640 return ret;
641#else
642 /* Compatibility with DM_USB and !CLK */
643 enable_usboh3_clk(1);
644 mdelay(1);
645#endif
646
Abel Vesa888a9462019-02-01 16:40:08 +0000647#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800648 ret = device_get_supply_regulator(dev, "vbus-supply",
649 &priv->vbus_supply);
650 if (ret)
651 debug("%s: No vbus supply\n", dev->name);
Abel Vesa888a9462019-02-01 16:40:08 +0000652#endif
Marek Vasut09dc0702021-03-31 21:40:24 +0200653
Marek Vasut41e81392021-03-31 23:00:23 +0200654#if !defined(CONFIG_PHY)
655 usb_power_config_mx6(priv->anatop_addr, priv->portnr);
656 usb_power_config_mx7(priv->misc_addr);
657 usb_power_config_mx7ulp(priv->phy_addr);
658#endif
659
Marek Vasut1fa42432021-03-31 23:24:41 +0200660 usb_oc_config(priv->misc_addr, priv->portnr);
Marek Vasut09dc0702021-03-31 21:40:24 +0200661
Marek Vasut41e81392021-03-31 23:00:23 +0200662#if !defined(CONFIG_PHY) && (defined(CONFIG_MX6) || defined(CONFIG_MX7ULP))
Marek Vasut6b6df642021-03-31 22:10:35 +0200663 usb_internal_phy_clock_gate(priv->phy_addr, 1);
664 usb_phy_enable(ehci, priv->phy_addr);
Marek Vasut09dc0702021-03-31 21:40:24 +0200665#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800666
Abel Vesa888a9462019-02-01 16:40:08 +0000667#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fan13351332016-12-22 17:06:43 +0800668 if (priv->vbus_supply) {
669 ret = regulator_set_enable(priv->vbus_supply,
670 (type == USB_INIT_DEVICE) ?
671 false : true);
Marek Vasut27370452020-05-21 23:32:23 +0200672 if (ret && ret != -ENOSYS) {
Marek Vasuta86d51a2020-05-21 23:34:06 +0200673 printf("Error enabling VBUS supply (ret=%i)\n", ret);
Marek Vasutf22fde72021-03-31 12:28:03 +0200674 goto err_clk;
Peng Fan13351332016-12-22 17:06:43 +0800675 }
676 }
Abel Vesa888a9462019-02-01 16:40:08 +0000677#endif
Peng Fan5c363c12016-06-17 14:19:27 +0800678
679 if (priv->init_type == USB_INIT_HOST) {
680 setbits_le32(&ehci->usbmode, CM_HOST);
681 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
682 setbits_le32(&ehci->portsc, USB_EN);
683 }
684
685 mdelay(10);
686
687 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
688 hcor = (struct ehci_hcor *)((uint32_t)hccr +
689 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
690
Marek Vasutf36841e2021-03-31 12:19:27 +0200691 ret = ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
692 if (ret)
693 goto err_regulator;
694
695 return ret;
696
697err_regulator:
698#if CONFIG_IS_ENABLED(DM_REGULATOR)
699 if (priv->vbus_supply)
700 regulator_set_enable(priv->vbus_supply, false);
701#endif
Marek Vasutf22fde72021-03-31 12:28:03 +0200702err_clk:
703#if CONFIG_IS_ENABLED(CLK)
704 clk_disable(&priv->clk);
705#else
706 /* Compatibility with DM_USB and !CLK */
707 enable_usboh3_clk(0);
708#endif
Marek Vasutf36841e2021-03-31 12:19:27 +0200709 return ret;
710}
711
712int ehci_usb_remove(struct udevice *dev)
713{
714 struct ehci_mx6_priv_data *priv __maybe_unused = dev_get_priv(dev);
715
716 ehci_deregister(dev);
717
718#if CONFIG_IS_ENABLED(DM_REGULATOR)
719 if (priv->vbus_supply)
720 regulator_set_enable(priv->vbus_supply, false);
721#endif
722
Marek Vasutf22fde72021-03-31 12:28:03 +0200723#if CONFIG_IS_ENABLED(CLK)
724 clk_disable(&priv->clk);
725#endif
726
Marek Vasutf36841e2021-03-31 12:19:27 +0200727 return 0;
Peng Fan5c363c12016-06-17 14:19:27 +0800728}
729
Peng Fan5c363c12016-06-17 14:19:27 +0800730static const struct udevice_id mx6_usb_ids[] = {
731 { .compatible = "fsl,imx27-usb" },
732 { }
733};
734
735U_BOOT_DRIVER(usb_mx6) = {
736 .name = "ehci_mx6",
737 .id = UCLASS_USB,
738 .of_match = mx6_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700739 .of_to_plat = ehci_usb_of_to_plat,
Peng Fan5c363c12016-06-17 14:19:27 +0800740 .probe = ehci_usb_probe,
Marek Vasutf36841e2021-03-31 12:19:27 +0200741 .remove = ehci_usb_remove,
Peng Fan5c363c12016-06-17 14:19:27 +0800742 .ops = &ehci_usb_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700743 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700744 .priv_auto = sizeof(struct ehci_mx6_priv_data),
Peng Fan5c363c12016-06-17 14:19:27 +0800745 .flags = DM_FLAG_ALLOC_PRIV_DMA,
746};
747#endif