blob: c3e4170513ec06fed7acbcff1417198c0d123146 [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>
Marek Vasut7f2c10e2021-03-31 12:28:03 +02008#include <clk.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000010#include <usb.h>
11#include <errno.h>
Mateusz Kulikowski8c25c252016-01-23 11:54:32 +010012#include <wait_bit.h>
Simon Glass401d1c42020-10-30 21:38:53 -060013#include <asm/global_data.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000014#include <linux/compiler.h>
Simon Glassc05ed002020-05-10 11:40:11 -060015#include <linux/delay.h>
Mateusz Kulikowskie162c6b2016-03-31 23:12:23 +020016#include <usb/ehci-ci.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000017#include <asm/io.h>
18#include <asm/arch/imx-regs.h>
19#include <asm/arch/clock.h>
Stefano Babic552a8482017-06-29 10:16:06 +020020#include <asm/mach-imx/iomux-v3.h>
21#include <asm/mach-imx/sys_proto.h>
Peng Fanbb42fb42016-06-17 14:19:27 +080022#include <dm.h>
Simon Glassc62db352017-05-31 19:47:48 -060023#include <asm/mach-types.h>
Peng Fanfcf9f9f2016-12-22 17:06:43 +080024#include <power/regulator.h>
Adam Ford69535b32019-04-03 08:41:56 -050025#include <linux/usb/otg.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000026
27#include "ehci.h"
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000028
Peng Fancccbddc2016-12-22 17:06:42 +080029DECLARE_GLOBAL_DATA_PTR;
30
Wolfgang Grandegger3f467522012-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 Kiskyd1a52862013-10-10 15:27:59 -070049#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000050
Wolfgang Grandegger3f467522012-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 Grandegger3f467522012-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 Alonso35554fc2015-08-06 15:43:17 -050059#define USBNC_OFFSET 0x200
Peng Fancccbddc2016-12-22 17:06:42 +080060#define USBNC_PHY_STATUS_OFFSET 0x23C
Adrian Alonso35554fc2015-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 Agner9a881802016-07-13 00:25:37 -070063#define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
Wolfgang Grandegger3f467522012-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 Grandegger3f467522012-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 Vasut1aae8a32021-04-10 16:03:04 +020071/* If this is not defined, assume MX6/MX7/MX8M SoC default */
72#ifndef CONFIG_MXC_USB_PORTSC
73#define CONFIG_MXC_USB_PORTSC (PORT_PTS_UTMI | PORT_PTS_PTW)
74#endif
75
Marek Vasut598fa7e2021-03-31 22:19:00 +020076/* Base address for this IP block is 0x02184800 */
77struct usbnc_regs {
78 u32 ctrl[4]; /* otg/host1-3 */
79 u32 uh2_hsic_ctrl;
80 u32 uh3_hsic_ctrl;
81 u32 otg_phy_ctrl_0;
82 u32 uh1_phy_ctrl_0;
83 u32 reserve1[4];
84 u32 phy_cfg1;
85 u32 phy_cfg2;
86 u32 reserve2;
87 u32 phy_status;
88 u32 reserve3[4];
89 u32 adp_cfg1;
90 u32 adp_cfg2;
91 u32 adp_status;
92};
93
Marek Vasut849763b2021-03-31 23:00:23 +020094#if defined(CONFIG_MX6) && !defined(CONFIG_PHY)
95static void usb_power_config_mx6(struct anatop_regs __iomem *anatop,
96 int anatop_bits_index)
Troy Kiskyd1a52862013-10-10 15:27:59 -070097{
Troy Kiskyd1a52862013-10-10 15:27:59 -070098 void __iomem *chrg_detect;
99 void __iomem *pll_480_ctrl_clr;
100 void __iomem *pll_480_ctrl_set;
101
Marek Vasut849763b2021-03-31 23:00:23 +0200102 if (!is_mx6())
103 return;
104
105 switch (anatop_bits_index) {
Troy Kiskyd1a52862013-10-10 15:27:59 -0700106 case 0:
107 chrg_detect = &anatop->usb1_chrg_detect;
108 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
109 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
110 break;
111 case 1:
112 chrg_detect = &anatop->usb2_chrg_detect;
113 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
114 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
115 break;
116 default:
117 return;
118 }
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000119 /*
Troy Kiskyd1a52862013-10-10 15:27:59 -0700120 * Some phy and power's special controls
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000121 * 1. The external charger detector needs to be disabled
122 * or the signal at DP will be poor
Troy Kiskyd1a52862013-10-10 15:27:59 -0700123 * 2. The PLL's power and output to usb
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000124 * is totally controlled by IC, so the Software only needs
125 * to enable them at initializtion.
126 */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500127 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000128 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700129 chrg_detect);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000130
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500131 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700132 pll_480_ctrl_clr);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000133
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500134 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000135 ANADIG_USB2_PLL_480_CTRL_POWER |
136 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700137 pll_480_ctrl_set);
Marek Vasut849763b2021-03-31 23:00:23 +0200138}
139#else
140static void __maybe_unused
141usb_power_config_mx6(void *anatop, int anatop_bits_index) { }
Ye Li235f5e12019-10-24 10:29:32 -0300142#endif
Marek Vasut849763b2021-03-31 23:00:23 +0200143
144#if defined(CONFIG_MX7) && !defined(CONFIG_PHY)
145static void usb_power_config_mx7(struct usbnc_regs *usbnc)
146{
147 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
148
149 if (!is_mx7())
150 return;
151
152 /*
153 * Clear the ACAENB to enable usb_otg_id detection,
154 * otherwise it is the ACA detection enabled.
155 */
156 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
157}
158#else
159static void __maybe_unused
160usb_power_config_mx7(void *usbnc) { }
161#endif
162
163#if defined(CONFIG_MX7ULP) && !defined(CONFIG_PHY)
164static void usb_power_config_mx7ulp(struct usbphy_regs __iomem *usbphy)
165{
166 if (!is_mx7ulp())
167 return;
168
169 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
170 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
171 &usbphy->usb1_chrg_detect);
172
173 scg_enable_usb_pll(true);
174}
175#else
176static void __maybe_unused
177usb_power_config_mx7ulp(void *usbphy) { }
178#endif
179
Giulio Benettie7e81e82021-05-20 16:10:15 +0200180#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) || defined(CONFIG_IMXRT)
Marek Vasut849763b2021-03-31 23:00:23 +0200181static const unsigned phy_bases[] = {
182 USB_PHY0_BASE_ADDR,
183#if defined(USB_PHY1_BASE_ADDR)
184 USB_PHY1_BASE_ADDR,
185#endif
186};
187
188#if !defined(CONFIG_PHY)
189static void usb_internal_phy_clock_gate(void __iomem *phy_reg, int on)
190{
191 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
192 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000193}
194
Troy Kiskyd1a52862013-10-10 15:27:59 -0700195/* Return 0 : host node, <>0 : device mode */
Marek Vasuteb64f592021-03-31 22:10:35 +0200196static int usb_phy_enable(struct usb_ehci *ehci, void __iomem *phy_reg)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000197{
Troy Kiskyd1a52862013-10-10 15:27:59 -0700198 void __iomem *phy_ctrl;
199 void __iomem *usb_cmd;
Adrian Alonsof0c89d52015-08-06 15:46:03 -0500200 int ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000201
Troy Kiskyd1a52862013-10-10 15:27:59 -0700202 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
203 usb_cmd = (void __iomem *)&ehci->usbcmd;
204
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000205 /* Stop then Reset */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500206 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100207 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
Adrian Alonsof0c89d52015-08-06 15:46:03 -0500208 if (ret)
209 return ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000210
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500211 setbits_le32(usb_cmd, UCMD_RESET);
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100212 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
Adrian Alonsof0c89d52015-08-06 15:46:03 -0500213 if (ret)
214 return ret;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000215
216 /* Reset USBPHY module */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500217 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000218 udelay(10);
219
220 /* Remove CLKGATE and SFTRST */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500221 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000222 udelay(10);
223
224 /* Power up the PHY */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500225 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000226 /* enable FS/LS device */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500227 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
228 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000229
Peng Fan229dbba2014-11-10 08:50:39 +0800230 return 0;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000231}
Marek Vasut849763b2021-03-31 23:00:23 +0200232#endif
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000233
Peng Fan229dbba2014-11-10 08:50:39 +0800234int usb_phy_mode(int port)
235{
236 void __iomem *phy_reg;
237 void __iomem *phy_ctrl;
238 u32 val;
239
240 phy_reg = (void __iomem *)phy_bases[port];
241 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
242
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500243 val = readl(phy_ctrl);
Peng Fan229dbba2014-11-10 08:50:39 +0800244
245 if (val & USBPHY_CTRL_OTG_ID)
246 return USB_INIT_DEVICE;
247 else
248 return USB_INIT_HOST;
249}
250
Adrian Alonso35554fc2015-08-06 15:43:17 -0500251#elif defined(CONFIG_MX7)
Adrian Alonso35554fc2015-08-06 15:43:17 -0500252int 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
Marek Vasut53396d62021-04-22 21:06:40 +0200268#if !defined(CONFIG_PHY)
269/* Should be done in the MXS PHY driver */
Marek Vasut66864692021-03-31 23:24:41 +0200270static void usb_oc_config(struct usbnc_regs *usbnc, int index)
Adrian Alonso35554fc2015-08-06 15:43:17 -0500271{
Marek Vasut598fa7e2021-03-31 22:19:00 +0200272 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
Adrian Alonso35554fc2015-08-06 15:43:17 -0500273
274#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
275 /* mx6qarm2 seems to required a different setting*/
276 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
277#else
278 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
279#endif
280
Adrian Alonso35554fc2015-08-06 15:43:17 -0500281 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
Ye Li235f5e12019-10-24 10:29:32 -0300282
283 /* Set power polarity to high active */
284#ifdef CONFIG_MXC_USB_OTG_HACTIVE
285 setbits_le32(ctrl, UCTRL_PWR_POL);
286#else
287 clrbits_le32(ctrl, UCTRL_PWR_POL);
288#endif
Adrian Alonso35554fc2015-08-06 15:43:17 -0500289}
Marek Vasut53396d62021-04-22 21:06:40 +0200290#endif
Adrian Alonso35554fc2015-08-06 15:43:17 -0500291
Marek Vasutef464e42021-03-31 21:40:24 +0200292#if !CONFIG_IS_ENABLED(DM_USB)
Adrian Alonso74f06102015-08-06 15:43:16 -0500293/**
Stefan Agner79d867c2016-05-05 16:59:12 -0700294 * board_usb_phy_mode - override usb phy mode
Adrian Alonso74f06102015-08-06 15:43:16 -0500295 * @port: usb host/otg port
296 *
297 * Target board specific, override usb_phy_mode.
298 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
299 * left disconnected in this case usb_phy_mode will not be able to identify
300 * the phy mode that usb port is used.
301 * Machine file overrides board_usb_phy_mode.
302 *
303 * Return: USB_INIT_DEVICE or USB_INIT_HOST
304 */
Peng Fan229dbba2014-11-10 08:50:39 +0800305int __weak board_usb_phy_mode(int port)
306{
307 return usb_phy_mode(port);
308}
309
Adrian Alonso74f06102015-08-06 15:43:16 -0500310/**
311 * board_ehci_hcd_init - set usb vbus voltage
312 * @port: usb otg port
313 *
314 * Target board specific, setup iomux pad to setup supply vbus voltage
315 * for usb otg port. Machine board file overrides board_ehci_hcd_init
316 *
317 * Return: 0 Success
318 */
Benoît Thébaudeauf22e4fa2012-11-13 09:58:35 +0000319int __weak board_ehci_hcd_init(int port)
320{
321 return 0;
322}
323
Adrian Alonso74f06102015-08-06 15:43:16 -0500324/**
325 * board_ehci_power - enables/disables usb vbus voltage
326 * @port: usb otg port
327 * @on: on/off vbus voltage
328 *
329 * Enables/disables supply vbus voltage for usb otg port.
330 * Machine board file overrides board_ehci_power
331 *
332 * Return: 0 Success
333 */
Troy Kiskyd1a52862013-10-10 15:27:59 -0700334int __weak board_ehci_power(int port, int on)
335{
336 return 0;
337}
338
Peng Fanbb42fb42016-06-17 14:19:27 +0800339int ehci_hcd_init(int index, enum usb_init_type init,
340 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
341{
342 enum usb_init_type type;
Giulio Benettie7e81e82021-05-20 16:10:15 +0200343#if defined(CONFIG_MX6) || defined(CONFIG_IMXRT)
Peng Fanbb42fb42016-06-17 14:19:27 +0800344 u32 controller_spacing = 0x200;
Marek Vasut849763b2021-03-31 23:00:23 +0200345 struct anatop_regs __iomem *anatop =
346 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Marek Vasut66864692021-03-31 23:24:41 +0200347 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
348 USB_OTHERREGS_OFFSET);
Marek Vasut849763b2021-03-31 23:00:23 +0200349#elif defined(CONFIG_MX7)
Peng Fanbb42fb42016-06-17 14:19:27 +0800350 u32 controller_spacing = 0x10000;
Marek Vasut849763b2021-03-31 23:00:23 +0200351 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
352 (0x10000 * index) + USBNC_OFFSET);
353#elif defined(CONFIG_MX7ULP)
354 u32 controller_spacing = 0x10000;
355 struct usbphy_regs __iomem *usbphy =
356 (struct usbphy_regs __iomem *)USB_PHY0_BASE_ADDR;
Marek Vasut66864692021-03-31 23:24:41 +0200357 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
358 (0x10000 * index) + USBNC_OFFSET);
Peng Fanbb42fb42016-06-17 14:19:27 +0800359#endif
360 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
361 (controller_spacing * index));
362 int ret;
363
364 if (index > 3)
365 return -EINVAL;
366
Peng Fan0bd3d912020-05-01 22:08:36 +0800367 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
368 if (usb_fused((ulong)ehci)) {
369 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
370 (ulong)ehci);
371 return -ENODEV;
372 }
373 }
374
Marek Vasut7f2c10e2021-03-31 12:28:03 +0200375 enable_usboh3_clk(1);
376 mdelay(1);
377
Marek Vasutef464e42021-03-31 21:40:24 +0200378 /* Do board specific initialization */
379 ret = board_ehci_hcd_init(index);
380 if (ret) {
381 enable_usboh3_clk(0);
Peng Fanbb42fb42016-06-17 14:19:27 +0800382 return ret;
Marek Vasutef464e42021-03-31 21:40:24 +0200383 }
384
Giulio Benettie7e81e82021-05-20 16:10:15 +0200385#if defined(CONFIG_MX6) || defined(CONFIG_IMXRT)
Marek Vasut849763b2021-03-31 23:00:23 +0200386 usb_power_config_mx6(anatop, index);
387#elif defined (CONFIG_MX7)
388 usb_power_config_mx7(usbnc);
389#elif defined (CONFIG_MX7ULP)
390 usb_power_config_mx7ulp(usbphy);
391#endif
392
Marek Vasut66864692021-03-31 23:24:41 +0200393 usb_oc_config(usbnc, index);
Marek Vasutef464e42021-03-31 21:40:24 +0200394
Giulio Benettie7e81e82021-05-20 16:10:15 +0200395#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) || defined(CONFIG_IMXRT)
Marek Vasuteb64f592021-03-31 22:10:35 +0200396 if (index < ARRAY_SIZE(phy_bases)) {
397 usb_internal_phy_clock_gate((void __iomem *)phy_bases[index], 1);
398 usb_phy_enable(ehci, (void __iomem *)phy_bases[index]);
399 }
Marek Vasutef464e42021-03-31 21:40:24 +0200400#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800401
Peng Fan229dbba2014-11-10 08:50:39 +0800402 type = board_usb_phy_mode(index);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000403
Peng Fanbb42fb42016-06-17 14:19:27 +0800404 if (hccr && hcor) {
Marek Vasutf444f892021-04-06 20:37:16 +0200405 *hccr = (struct ehci_hccr *)((uintptr_t)&ehci->caplength);
406 *hcor = (struct ehci_hcor *)((uintptr_t)*hccr +
Peng Fanbb42fb42016-06-17 14:19:27 +0800407 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
408 }
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000409
Troy Kiskyd1a52862013-10-10 15:27:59 -0700410 if ((type == init) || (type == USB_INIT_DEVICE))
411 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
412 if (type != init)
413 return -ENODEV;
414 if (type == USB_INIT_DEVICE)
415 return 0;
Adrian Alonso35554fc2015-08-06 15:43:17 -0500416
Troy Kiskyd1a52862013-10-10 15:27:59 -0700417 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500418 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000419 setbits_le32(&ehci->portsc, USB_EN);
420
421 mdelay(10);
422
423 return 0;
424}
425
Lucas Stach676ae062012-09-26 00:14:35 +0200426int ehci_hcd_stop(int index)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000427{
428 return 0;
429}
Peng Fanbb42fb42016-06-17 14:19:27 +0800430#else
431struct ehci_mx6_priv_data {
432 struct ehci_ctrl ctrl;
433 struct usb_ehci *ehci;
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800434 struct udevice *vbus_supply;
Marek Vasut7f2c10e2021-03-31 12:28:03 +0200435 struct clk clk;
Marek Vasut50d01462021-04-02 13:07:49 +0200436 struct phy phy;
Peng Fanbb42fb42016-06-17 14:19:27 +0800437 enum usb_init_type init_type;
Marek Vasut53396d62021-04-22 21:06:40 +0200438#if !defined(CONFIG_PHY)
Peng Fanbb42fb42016-06-17 14:19:27 +0800439 int portnr;
Marek Vasut4dcfa3b2021-03-31 23:06:07 +0200440 void __iomem *phy_addr;
441 void __iomem *misc_addr;
442 void __iomem *anatop_addr;
Marek Vasut53396d62021-04-22 21:06:40 +0200443#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800444};
445
446static int mx6_init_after_reset(struct ehci_ctrl *dev)
447{
448 struct ehci_mx6_priv_data *priv = dev->priv;
449 enum usb_init_type type = priv->init_type;
450 struct usb_ehci *ehci = priv->ehci;
Peng Fanbb42fb42016-06-17 14:19:27 +0800451
Marek Vasut849763b2021-03-31 23:00:23 +0200452#if !defined(CONFIG_PHY)
453 usb_power_config_mx6(priv->anatop_addr, priv->portnr);
454 usb_power_config_mx7(priv->misc_addr);
455 usb_power_config_mx7ulp(priv->phy_addr);
Marek Vasut849763b2021-03-31 23:00:23 +0200456
Marek Vasut66864692021-03-31 23:24:41 +0200457 usb_oc_config(priv->misc_addr, priv->portnr);
Marek Vasutef464e42021-03-31 21:40:24 +0200458
Marek Vasut53396d62021-04-22 21:06:40 +0200459#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP)
Marek Vasuteb64f592021-03-31 22:10:35 +0200460 usb_internal_phy_clock_gate(priv->phy_addr, 1);
461 usb_phy_enable(ehci, priv->phy_addr);
Marek Vasutef464e42021-03-31 21:40:24 +0200462#endif
Marek Vasut53396d62021-04-22 21:06:40 +0200463#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800464
Abel Vesa921208e2019-02-01 16:40:08 +0000465#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800466 if (priv->vbus_supply) {
Marek Vasutef464e42021-03-31 21:40:24 +0200467 int ret;
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800468 ret = regulator_set_enable(priv->vbus_supply,
469 (type == USB_INIT_DEVICE) ?
470 false : true);
Marek Vasut10bcafb2020-05-21 23:32:23 +0200471 if (ret && ret != -ENOSYS) {
Marek Vasut73021d12020-05-21 23:34:06 +0200472 printf("Error enabling VBUS supply (ret=%i)\n", ret);
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800473 return ret;
474 }
475 }
Abel Vesa921208e2019-02-01 16:40:08 +0000476#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800477
478 if (type == USB_INIT_DEVICE)
479 return 0;
480
481 setbits_le32(&ehci->usbmode, CM_HOST);
482 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
483 setbits_le32(&ehci->portsc, USB_EN);
484
485 mdelay(10);
486
487 return 0;
488}
489
490static const struct ehci_ops mx6_ehci_ops = {
491 .init_after_reset = mx6_init_after_reset
492};
493
Peng Fancccbddc2016-12-22 17:06:42 +0800494static int ehci_usb_phy_mode(struct udevice *dev)
495{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700496 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900497 void *__iomem addr = dev_read_addr_ptr(dev);
Peng Fancccbddc2016-12-22 17:06:42 +0800498 void *__iomem phy_ctrl, *__iomem phy_status;
499 const void *blob = gd->fdt_blob;
Simon Glasse160f7d2017-01-17 16:52:55 -0700500 int offset = dev_of_offset(dev), phy_off;
Peng Fancccbddc2016-12-22 17:06:42 +0800501 u32 val;
502
503 /*
504 * About fsl,usbphy, Refer to
505 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
506 */
Giulio Benettie7e81e82021-05-20 16:10:15 +0200507 if (is_mx6() || is_mx7ulp() || is_imxrt()) {
Peng Fancccbddc2016-12-22 17:06:42 +0800508 phy_off = fdtdec_lookup_phandle(blob,
509 offset,
510 "fsl,usbphy");
511 if (phy_off < 0)
512 return -EINVAL;
513
514 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
515 "reg");
516 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
517 return -EINVAL;
518
519 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
520 val = readl(phy_ctrl);
521
522 if (val & USBPHY_CTRL_OTG_ID)
523 plat->init_type = USB_INIT_DEVICE;
524 else
525 plat->init_type = USB_INIT_HOST;
526 } else if (is_mx7()) {
527 phy_status = (void __iomem *)(addr +
528 USBNC_PHY_STATUS_OFFSET);
529 val = readl(phy_status);
530
531 if (val & USBNC_PHYSTATUS_ID_DIG)
532 plat->init_type = USB_INIT_DEVICE;
533 else
534 plat->init_type = USB_INIT_HOST;
535 } else {
536 return -EINVAL;
537 }
538
539 return 0;
540}
541
Simon Glassd1998a92020-12-03 16:55:21 -0700542static int ehci_usb_of_to_plat(struct udevice *dev)
Peng Fancccbddc2016-12-22 17:06:42 +0800543{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700544 struct usb_plat *plat = dev_get_plat(dev);
Adam Ford69535b32019-04-03 08:41:56 -0500545 enum usb_dr_mode dr_mode;
Peng Fancccbddc2016-12-22 17:06:42 +0800546
Simon Glassf10643c2020-12-19 10:40:14 -0700547 dr_mode = usb_get_dr_mode(dev_ofnode(dev));
Peng Fancccbddc2016-12-22 17:06:42 +0800548
Adam Ford69535b32019-04-03 08:41:56 -0500549 switch (dr_mode) {
550 case USB_DR_MODE_HOST:
551 plat->init_type = USB_INIT_HOST;
552 break;
553 case USB_DR_MODE_PERIPHERAL:
554 plat->init_type = USB_INIT_DEVICE;
555 break;
556 case USB_DR_MODE_OTG:
557 case USB_DR_MODE_UNKNOWN:
558 return ehci_usb_phy_mode(dev);
559 };
Peng Fancccbddc2016-12-22 17:06:42 +0800560
Adam Ford69535b32019-04-03 08:41:56 -0500561 return 0;
Peng Fancccbddc2016-12-22 17:06:42 +0800562}
563
Marek Vasut4dcfa3b2021-03-31 23:06:07 +0200564static int mx6_parse_dt_addrs(struct udevice *dev)
565{
Marek Vasut53396d62021-04-22 21:06:40 +0200566#if !defined(CONFIG_PHY)
Marek Vasut4dcfa3b2021-03-31 23:06:07 +0200567 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
568 int phy_off, misc_off;
569 const void *blob = gd->fdt_blob;
570 int offset = dev_of_offset(dev);
571 void *__iomem addr;
572
573 phy_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbphy");
574 if (phy_off < 0) {
575 phy_off = fdtdec_lookup_phandle(blob, offset, "phys");
576 if (phy_off < 0)
577 return -EINVAL;
578 }
579
580 misc_off = fdtdec_lookup_phandle(blob, offset, "fsl,usbmisc");
581 if (misc_off < 0)
582 return -EINVAL;
583
584 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, "reg");
585 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
Fabio Estevam48221142021-06-20 12:00:52 -0300586 addr = NULL;
Marek Vasut4dcfa3b2021-03-31 23:06:07 +0200587
588 priv->phy_addr = addr;
589
590 addr = (void __iomem *)fdtdec_get_addr(blob, misc_off, "reg");
591 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
592 return -EINVAL;
593
594 priv->misc_addr = addr;
595
Marek Vasut53396d62021-04-22 21:06:40 +0200596#if defined(CONFIG_MX6)
Fabio Estevamec326b92021-06-20 12:00:51 -0300597 int anatop_off, ret, devnump;
598
599 ret = fdtdec_get_alias_seq(blob, dev->uclass->uc_drv->name,
600 phy_off, &devnump);
601 if (ret < 0)
602 return ret;
603 priv->portnr = devnump;
Marek Vasut4dcfa3b2021-03-31 23:06:07 +0200604
605 /* Resolve ANATOP offset through USB PHY node */
606 anatop_off = fdtdec_lookup_phandle(blob, phy_off, "fsl,anatop");
607 if (anatop_off < 0)
608 return -EINVAL;
609
610 addr = (void __iomem *)fdtdec_get_addr(blob, anatop_off, "reg");
611 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
612 return -EINVAL;
613
614 priv->anatop_addr = addr;
615#endif
Marek Vasut53396d62021-04-22 21:06:40 +0200616#endif
Marek Vasut4dcfa3b2021-03-31 23:06:07 +0200617 return 0;
618}
619
Peng Fanbb42fb42016-06-17 14:19:27 +0800620static int ehci_usb_probe(struct udevice *dev)
621{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700622 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900623 struct usb_ehci *ehci = dev_read_addr_ptr(dev);
Peng Fanbb42fb42016-06-17 14:19:27 +0800624 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800625 enum usb_init_type type = plat->init_type;
Peng Fanbb42fb42016-06-17 14:19:27 +0800626 struct ehci_hccr *hccr;
627 struct ehci_hcor *hcor;
628 int ret;
629
Peng Fan0bd3d912020-05-01 22:08:36 +0800630 if (CONFIG_IS_ENABLED(IMX_MODULE_FUSE)) {
631 if (usb_fused((ulong)ehci)) {
632 printf("SoC fuse indicates USB@0x%lx is unavailable.\n",
633 (ulong)ehci);
634 return -ENODEV;
635 }
636 }
637
Marek Vasut4dcfa3b2021-03-31 23:06:07 +0200638 ret = mx6_parse_dt_addrs(dev);
639 if (ret)
640 return ret;
641
Peng Fanbb42fb42016-06-17 14:19:27 +0800642 priv->ehci = ehci;
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800643 priv->init_type = type;
644
Marek Vasut7f2c10e2021-03-31 12:28:03 +0200645#if CONFIG_IS_ENABLED(CLK)
646 ret = clk_get_by_index(dev, 0, &priv->clk);
647 if (ret < 0)
648 return ret;
649
650 ret = clk_enable(&priv->clk);
651 if (ret)
652 return ret;
653#else
654 /* Compatibility with DM_USB and !CLK */
655 enable_usboh3_clk(1);
656 mdelay(1);
657#endif
658
Abel Vesa921208e2019-02-01 16:40:08 +0000659#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800660 ret = device_get_supply_regulator(dev, "vbus-supply",
661 &priv->vbus_supply);
662 if (ret)
663 debug("%s: No vbus supply\n", dev->name);
Abel Vesa921208e2019-02-01 16:40:08 +0000664#endif
Marek Vasutef464e42021-03-31 21:40:24 +0200665
Marek Vasut849763b2021-03-31 23:00:23 +0200666#if !defined(CONFIG_PHY)
667 usb_power_config_mx6(priv->anatop_addr, priv->portnr);
668 usb_power_config_mx7(priv->misc_addr);
669 usb_power_config_mx7ulp(priv->phy_addr);
Marek Vasut849763b2021-03-31 23:00:23 +0200670
Marek Vasut66864692021-03-31 23:24:41 +0200671 usb_oc_config(priv->misc_addr, priv->portnr);
Marek Vasutef464e42021-03-31 21:40:24 +0200672
Giulio Benettie7e81e82021-05-20 16:10:15 +0200673#if defined(CONFIG_MX6) || defined(CONFIG_MX7ULP) || defined(CONFIG_IMXRT)
Marek Vasuteb64f592021-03-31 22:10:35 +0200674 usb_internal_phy_clock_gate(priv->phy_addr, 1);
675 usb_phy_enable(ehci, priv->phy_addr);
Marek Vasutef464e42021-03-31 21:40:24 +0200676#endif
Marek Vasut53396d62021-04-22 21:06:40 +0200677#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800678
Abel Vesa921208e2019-02-01 16:40:08 +0000679#if CONFIG_IS_ENABLED(DM_REGULATOR)
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800680 if (priv->vbus_supply) {
681 ret = regulator_set_enable(priv->vbus_supply,
682 (type == USB_INIT_DEVICE) ?
683 false : true);
Marek Vasut10bcafb2020-05-21 23:32:23 +0200684 if (ret && ret != -ENOSYS) {
Marek Vasut73021d12020-05-21 23:34:06 +0200685 printf("Error enabling VBUS supply (ret=%i)\n", ret);
Marek Vasut7f2c10e2021-03-31 12:28:03 +0200686 goto err_clk;
Peng Fanfcf9f9f2016-12-22 17:06:43 +0800687 }
688 }
Abel Vesa921208e2019-02-01 16:40:08 +0000689#endif
Peng Fanbb42fb42016-06-17 14:19:27 +0800690
691 if (priv->init_type == USB_INIT_HOST) {
692 setbits_le32(&ehci->usbmode, CM_HOST);
693 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
694 setbits_le32(&ehci->portsc, USB_EN);
695 }
696
697 mdelay(10);
698
Marek Vasut50d01462021-04-02 13:07:49 +0200699#if defined(CONFIG_PHY)
700 ret = ehci_setup_phy(dev, &priv->phy, 0);
701 if (ret)
702 goto err_regulator;
703#endif
704
Marek Vasutf444f892021-04-06 20:37:16 +0200705 hccr = (struct ehci_hccr *)((uintptr_t)&ehci->caplength);
706 hcor = (struct ehci_hcor *)((uintptr_t)hccr +
Peng Fanbb42fb42016-06-17 14:19:27 +0800707 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
708
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200709 ret = ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
710 if (ret)
Marek Vasut50d01462021-04-02 13:07:49 +0200711 goto err_phy;
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200712
713 return ret;
714
Marek Vasut50d01462021-04-02 13:07:49 +0200715err_phy:
716#if defined(CONFIG_PHY)
717 ehci_shutdown_phy(dev, &priv->phy);
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200718err_regulator:
Marek Vasut50d01462021-04-02 13:07:49 +0200719#endif
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200720#if CONFIG_IS_ENABLED(DM_REGULATOR)
721 if (priv->vbus_supply)
722 regulator_set_enable(priv->vbus_supply, false);
Marek Vasut7f2c10e2021-03-31 12:28:03 +0200723err_clk:
Marek Vasut50d01462021-04-02 13:07:49 +0200724#endif
Marek Vasut7f2c10e2021-03-31 12:28:03 +0200725#if CONFIG_IS_ENABLED(CLK)
726 clk_disable(&priv->clk);
727#else
728 /* Compatibility with DM_USB and !CLK */
729 enable_usboh3_clk(0);
730#endif
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200731 return ret;
732}
733
734int ehci_usb_remove(struct udevice *dev)
735{
736 struct ehci_mx6_priv_data *priv __maybe_unused = dev_get_priv(dev);
737
738 ehci_deregister(dev);
739
Marek Vasut50d01462021-04-02 13:07:49 +0200740#if defined(CONFIG_PHY)
741 ehci_shutdown_phy(dev, &priv->phy);
742#endif
743
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200744#if CONFIG_IS_ENABLED(DM_REGULATOR)
745 if (priv->vbus_supply)
746 regulator_set_enable(priv->vbus_supply, false);
747#endif
748
Marek Vasut7f2c10e2021-03-31 12:28:03 +0200749#if CONFIG_IS_ENABLED(CLK)
750 clk_disable(&priv->clk);
751#endif
752
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200753 return 0;
Peng Fanbb42fb42016-06-17 14:19:27 +0800754}
755
Peng Fanbb42fb42016-06-17 14:19:27 +0800756static const struct udevice_id mx6_usb_ids[] = {
757 { .compatible = "fsl,imx27-usb" },
Marek Vasute87015f2021-04-02 13:07:59 +0200758 { .compatible = "fsl,imx7d-usb" },
Giulio Benettie7e81e82021-05-20 16:10:15 +0200759 { .compatible = "fsl,imxrt-usb" },
Peng Fanbb42fb42016-06-17 14:19:27 +0800760 { }
761};
762
763U_BOOT_DRIVER(usb_mx6) = {
764 .name = "ehci_mx6",
765 .id = UCLASS_USB,
766 .of_match = mx6_usb_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700767 .of_to_plat = ehci_usb_of_to_plat,
Peng Fanbb42fb42016-06-17 14:19:27 +0800768 .probe = ehci_usb_probe,
Marek Vasut7e1f1e12021-03-31 12:19:27 +0200769 .remove = ehci_usb_remove,
Peng Fanbb42fb42016-06-17 14:19:27 +0800770 .ops = &ehci_usb_ops,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700771 .plat_auto = sizeof(struct usb_plat),
Simon Glass41575d82020-12-03 16:55:17 -0700772 .priv_auto = sizeof(struct ehci_mx6_priv_data),
Peng Fanbb42fb42016-06-17 14:19:27 +0800773 .flags = DM_FLAG_ALLOC_PRIV_DMA,
774};
775#endif