blob: be010b1adb1e05b8418fa2ae5a2c30cf61d043aa [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>
8#include <usb.h>
9#include <errno.h>
Mateusz Kulikowski4073b832016-01-23 11:54:32 +010010#include <wait_bit.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000011#include <linux/compiler.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020012#include <usb/ehci-ci.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000013#include <asm/io.h>
14#include <asm/arch/imx-regs.h>
15#include <asm/arch/clock.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020016#include <asm/mach-imx/iomux-v3.h>
17#include <asm/mach-imx/sys_proto.h>
Peng Fan5c363c12016-06-17 14:19:27 +080018#include <dm.h>
Simon Glass0ffb9d62017-05-31 19:47:48 -060019#include <asm/mach-types.h>
Peng Fan13351332016-12-22 17:06:43 +080020#include <power/regulator.h>
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000021
22#include "ehci.h"
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000023
Peng Fan9e3eab32016-12-22 17:06:42 +080024DECLARE_GLOBAL_DATA_PTR;
25
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000026#define USB_OTGREGS_OFFSET 0x000
27#define USB_H1REGS_OFFSET 0x200
28#define USB_H2REGS_OFFSET 0x400
29#define USB_H3REGS_OFFSET 0x600
30#define USB_OTHERREGS_OFFSET 0x800
31
32#define USB_H1_CTRL_OFFSET 0x04
33
34#define USBPHY_CTRL 0x00000030
35#define USBPHY_CTRL_SET 0x00000034
36#define USBPHY_CTRL_CLR 0x00000038
37#define USBPHY_CTRL_TOG 0x0000003c
38
39#define USBPHY_PWD 0x00000000
40#define USBPHY_CTRL_SFTRST 0x80000000
41#define USBPHY_CTRL_CLKGATE 0x40000000
42#define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
43#define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
Troy Kiskyed72a9e2013-10-10 15:27:59 -070044#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000045
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000046#define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
47#define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
48
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000049#define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
50#define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
51#define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
52#define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
53
Adrian Alonsof31599f2015-08-06 15:43:17 -050054#define USBNC_OFFSET 0x200
Peng Fan9e3eab32016-12-22 17:06:42 +080055#define USBNC_PHY_STATUS_OFFSET 0x23C
Adrian Alonsof31599f2015-08-06 15:43:17 -050056#define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
57#define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
Stefan Agner475cf912016-07-13 00:25:37 -070058#define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000059#define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
60#define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
61
62/* USBCMD */
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000063#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
64#define UCMD_RESET (1 << 1) /* controller reset */
65
Adrian Alonsof31599f2015-08-06 15:43:17 -050066#if defined(CONFIG_MX6)
Troy Kiskyed72a9e2013-10-10 15:27:59 -070067static const unsigned phy_bases[] = {
68 USB_PHY0_BASE_ADDR,
69 USB_PHY1_BASE_ADDR,
70};
71
72static void usb_internal_phy_clock_gate(int index, int on)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000073{
Troy Kiskyed72a9e2013-10-10 15:27:59 -070074 void __iomem *phy_reg;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000075
Troy Kiskyed72a9e2013-10-10 15:27:59 -070076 if (index >= ARRAY_SIZE(phy_bases))
77 return;
78
79 phy_reg = (void __iomem *)phy_bases[index];
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000080 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
Adrian Alonsoaee79b42015-08-06 15:43:15 -050081 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000082}
83
Troy Kiskyed72a9e2013-10-10 15:27:59 -070084static void usb_power_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +000085{
Wolfgang Grandegger5d113ca2012-05-02 04:36:39 +000086 struct anatop_regs __iomem *anatop =
87 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Troy Kiskyed72a9e2013-10-10 15:27:59 -070088 void __iomem *chrg_detect;
89 void __iomem *pll_480_ctrl_clr;
90 void __iomem *pll_480_ctrl_set;
91
92 switch (index) {
93 case 0:
94 chrg_detect = &anatop->usb1_chrg_detect;
95 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
96 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
97 break;
98 case 1:
99 chrg_detect = &anatop->usb2_chrg_detect;
100 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
101 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
102 break;
103 default:
104 return;
105 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000106 /*
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700107 * Some phy and power's special controls
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000108 * 1. The external charger detector needs to be disabled
109 * or the signal at DP will be poor
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700110 * 2. The PLL's power and output to usb
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000111 * is totally controlled by IC, so the Software only needs
112 * to enable them at initializtion.
113 */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500114 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000115 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700116 chrg_detect);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000117
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500118 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700119 pll_480_ctrl_clr);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000120
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500121 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000122 ANADIG_USB2_PLL_480_CTRL_POWER |
123 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700124 pll_480_ctrl_set);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000125}
126
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700127/* Return 0 : host node, <>0 : device mode */
128static int usb_phy_enable(int index, struct usb_ehci *ehci)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000129{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700130 void __iomem *phy_reg;
131 void __iomem *phy_ctrl;
132 void __iomem *usb_cmd;
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500133 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000134
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700135 if (index >= ARRAY_SIZE(phy_bases))
136 return 0;
137
138 phy_reg = (void __iomem *)phy_bases[index];
139 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
140 usb_cmd = (void __iomem *)&ehci->usbcmd;
141
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000142 /* Stop then Reset */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500143 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100144 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500145 if (ret)
146 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000147
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500148 setbits_le32(usb_cmd, UCMD_RESET);
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100149 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false);
Adrian Alonsoc52eb1c2015-08-06 15:46:03 -0500150 if (ret)
151 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000152
153 /* Reset USBPHY module */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500154 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000155 udelay(10);
156
157 /* Remove CLKGATE and SFTRST */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500158 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000159 udelay(10);
160
161 /* Power up the PHY */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500162 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000163 /* enable FS/LS device */
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500164 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
165 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000166
Peng Fan220402e2014-11-10 08:50:39 +0800167 return 0;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000168}
169
Adrian Alonsof31599f2015-08-06 15:43:17 -0500170int usb_phy_mode(int port)
171{
172 void __iomem *phy_reg;
173 void __iomem *phy_ctrl;
174 u32 val;
175
176 phy_reg = (void __iomem *)phy_bases[port];
177 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
178
179 val = readl(phy_ctrl);
180
181 if (val & USBPHY_CTRL_OTG_ID)
182 return USB_INIT_DEVICE;
183 else
184 return USB_INIT_HOST;
185}
186
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700187/* Base address for this IP block is 0x02184800 */
188struct usbnc_regs {
189 u32 ctrl[4]; /* otg/host1-3 */
190 u32 uh2_hsic_ctrl;
191 u32 uh3_hsic_ctrl;
192 u32 otg_phy_ctrl_0;
193 u32 uh1_phy_ctrl_0;
194};
Adrian Alonsof31599f2015-08-06 15:43:17 -0500195#elif defined(CONFIG_MX7)
196struct usbnc_regs {
197 u32 ctrl1;
198 u32 ctrl2;
199 u32 reserve1[10];
200 u32 phy_cfg1;
201 u32 phy_cfg2;
Peng Fan13186cf2016-06-20 09:43:08 +0800202 u32 reserve2;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500203 u32 phy_status;
Peng Fan13186cf2016-06-20 09:43:08 +0800204 u32 reserve3[4];
Adrian Alonsof31599f2015-08-06 15:43:17 -0500205 u32 adp_cfg1;
206 u32 adp_cfg2;
207 u32 adp_status;
208};
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700209
Adrian Alonsof31599f2015-08-06 15:43:17 -0500210static void usb_power_config(int index)
211{
212 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
213 (0x10000 * index) + USBNC_OFFSET);
214 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
Stefan Agner475cf912016-07-13 00:25:37 -0700215 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
Adrian Alonsof31599f2015-08-06 15:43:17 -0500216
Peng Fan928d3d02016-06-20 09:43:09 +0800217 /*
218 * Clear the ACAENB to enable usb_otg_id detection,
219 * otherwise it is the ACA detection enabled.
220 */
221 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
Stefan Agner475cf912016-07-13 00:25:37 -0700222
223 /* Set power polarity to high active */
Stefan Agner8652ce92016-07-13 00:25:38 -0700224#ifdef CONFIG_MXC_USB_OTG_HACTIVE
Stefan Agner475cf912016-07-13 00:25:37 -0700225 setbits_le32(ctrl, UCTRL_PWR_POL);
Stefan Agner8652ce92016-07-13 00:25:38 -0700226#else
227 clrbits_le32(ctrl, UCTRL_PWR_POL);
228#endif
Adrian Alonsof31599f2015-08-06 15:43:17 -0500229}
230
231int usb_phy_mode(int port)
232{
233 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
234 (0x10000 * port) + USBNC_OFFSET);
235 void __iomem *status = (void __iomem *)(&usbnc->phy_status);
236 u32 val;
237
238 val = readl(status);
239
240 if (val & USBNC_PHYSTATUS_ID_DIG)
241 return USB_INIT_DEVICE;
242 else
243 return USB_INIT_HOST;
244}
245#endif
246
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700247static void usb_oc_config(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000248{
Adrian Alonsof31599f2015-08-06 15:43:17 -0500249#if defined(CONFIG_MX6)
Ye.Lif93453a2014-09-15 17:23:14 +0800250 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700251 USB_OTHERREGS_OFFSET);
252 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
Adrian Alonsof31599f2015-08-06 15:43:17 -0500253#elif defined(CONFIG_MX7)
254 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
255 (0x10000 * index) + USBNC_OFFSET);
256 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
257#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000258
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000259#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
260 /* mx6qarm2 seems to required a different setting*/
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500261 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000262#else
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500263 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000264#endif
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000265
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500266 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
Peng Fan220402e2014-11-10 08:50:39 +0800267}
268
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500269/**
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700270 * board_usb_phy_mode - override usb phy mode
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500271 * @port: usb host/otg port
272 *
273 * Target board specific, override usb_phy_mode.
274 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
275 * left disconnected in this case usb_phy_mode will not be able to identify
276 * the phy mode that usb port is used.
277 * Machine file overrides board_usb_phy_mode.
278 *
279 * Return: USB_INIT_DEVICE or USB_INIT_HOST
280 */
Peng Fan220402e2014-11-10 08:50:39 +0800281int __weak board_usb_phy_mode(int port)
282{
283 return usb_phy_mode(port);
284}
285
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500286/**
287 * board_ehci_hcd_init - set usb vbus voltage
288 * @port: usb otg port
289 *
290 * Target board specific, setup iomux pad to setup supply vbus voltage
291 * for usb otg port. Machine board file overrides board_ehci_hcd_init
292 *
293 * Return: 0 Success
294 */
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000295int __weak board_ehci_hcd_init(int port)
296{
297 return 0;
298}
299
Adrian Alonso14dfbbb2015-08-06 15:43:16 -0500300/**
301 * board_ehci_power - enables/disables usb vbus voltage
302 * @port: usb otg port
303 * @on: on/off vbus voltage
304 *
305 * Enables/disables supply vbus voltage for usb otg port.
306 * Machine board file overrides board_ehci_power
307 *
308 * Return: 0 Success
309 */
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700310int __weak board_ehci_power(int port, int on)
311{
312 return 0;
313}
314
Peng Fan5c363c12016-06-17 14:19:27 +0800315int ehci_mx6_common_init(struct usb_ehci *ehci, int index)
316{
317 int ret;
318
319 enable_usboh3_clk(1);
320 mdelay(1);
321
322 /* Do board specific initialization */
323 ret = board_ehci_hcd_init(index);
324 if (ret)
325 return ret;
326
327 usb_power_config(index);
328 usb_oc_config(index);
329
330#if defined(CONFIG_MX6)
331 usb_internal_phy_clock_gate(index, 1);
332 usb_phy_enable(index, ehci);
333#endif
334
335 return 0;
336}
337
338#ifndef CONFIG_DM_USB
Troy Kisky7d6bbb92013-10-10 15:27:57 -0700339int ehci_hcd_init(int index, enum usb_init_type init,
340 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000341{
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700342 enum usb_init_type type;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500343#if defined(CONFIG_MX6)
344 u32 controller_spacing = 0x200;
345#elif defined(CONFIG_MX7)
346 u32 controller_spacing = 0x10000;
347#endif
Ye.Lif93453a2014-09-15 17:23:14 +0800348 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
Adrian Alonsof31599f2015-08-06 15:43:17 -0500349 (controller_spacing * index));
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700350 int ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000351
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700352 if (index > 3)
353 return -EINVAL;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000354
Peng Fan5c363c12016-06-17 14:19:27 +0800355 ret = ehci_mx6_common_init(ehci, index);
Stefan Agner3dfd3a02016-05-05 16:59:12 -0700356 if (ret)
357 return ret;
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000358
Peng Fan220402e2014-11-10 08:50:39 +0800359 type = board_usb_phy_mode(index);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000360
Peng Fan5c363c12016-06-17 14:19:27 +0800361 if (hccr && hcor) {
362 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
363 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
364 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
365 }
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000366
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700367 if ((type == init) || (type == USB_INIT_DEVICE))
368 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
369 if (type != init)
370 return -ENODEV;
371 if (type == USB_INIT_DEVICE)
372 return 0;
Adrian Alonsof31599f2015-08-06 15:43:17 -0500373
Troy Kiskyed72a9e2013-10-10 15:27:59 -0700374 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoaee79b42015-08-06 15:43:15 -0500375 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000376 setbits_le32(&ehci->portsc, USB_EN);
377
378 mdelay(10);
379
380 return 0;
381}
382
Lucas Stach3494a4c2012-09-26 00:14:35 +0200383int ehci_hcd_stop(int index)
Wolfgang Grandegger1859b702012-02-08 22:33:25 +0000384{
Peng Fan5c363c12016-06-17 14:19:27 +0800385 return 0;
386}
387#else
388struct ehci_mx6_priv_data {
389 struct ehci_ctrl ctrl;
390 struct usb_ehci *ehci;
Peng Fan13351332016-12-22 17:06:43 +0800391 struct udevice *vbus_supply;
Peng Fan5c363c12016-06-17 14:19:27 +0800392 enum usb_init_type init_type;
393 int portnr;
394};
395
396static int mx6_init_after_reset(struct ehci_ctrl *dev)
397{
398 struct ehci_mx6_priv_data *priv = dev->priv;
399 enum usb_init_type type = priv->init_type;
400 struct usb_ehci *ehci = priv->ehci;
401 int ret;
402
403 ret = ehci_mx6_common_init(priv->ehci, priv->portnr);
404 if (ret)
405 return ret;
406
Peng Fan13351332016-12-22 17:06:43 +0800407 if (priv->vbus_supply) {
408 ret = regulator_set_enable(priv->vbus_supply,
409 (type == USB_INIT_DEVICE) ?
410 false : true);
411 if (ret) {
412 puts("Error enabling VBUS supply\n");
413 return ret;
414 }
415 }
Peng Fan5c363c12016-06-17 14:19:27 +0800416
417 if (type == USB_INIT_DEVICE)
418 return 0;
419
420 setbits_le32(&ehci->usbmode, CM_HOST);
421 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
422 setbits_le32(&ehci->portsc, USB_EN);
423
424 mdelay(10);
425
426 return 0;
427}
428
429static const struct ehci_ops mx6_ehci_ops = {
430 .init_after_reset = mx6_init_after_reset
431};
432
Peng Fan9e3eab32016-12-22 17:06:42 +0800433static int ehci_usb_phy_mode(struct udevice *dev)
434{
435 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassba1dea42017-05-17 17:18:05 -0600436 void *__iomem addr = (void *__iomem)devfdt_get_addr(dev);
Peng Fan9e3eab32016-12-22 17:06:42 +0800437 void *__iomem phy_ctrl, *__iomem phy_status;
438 const void *blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700439 int offset = dev_of_offset(dev), phy_off;
Peng Fan9e3eab32016-12-22 17:06:42 +0800440 u32 val;
441
442 /*
443 * About fsl,usbphy, Refer to
444 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt.
445 */
446 if (is_mx6()) {
447 phy_off = fdtdec_lookup_phandle(blob,
448 offset,
449 "fsl,usbphy");
450 if (phy_off < 0)
451 return -EINVAL;
452
453 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off,
454 "reg");
455 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE)
456 return -EINVAL;
457
458 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL);
459 val = readl(phy_ctrl);
460
461 if (val & USBPHY_CTRL_OTG_ID)
462 plat->init_type = USB_INIT_DEVICE;
463 else
464 plat->init_type = USB_INIT_HOST;
465 } else if (is_mx7()) {
466 phy_status = (void __iomem *)(addr +
467 USBNC_PHY_STATUS_OFFSET);
468 val = readl(phy_status);
469
470 if (val & USBNC_PHYSTATUS_ID_DIG)
471 plat->init_type = USB_INIT_DEVICE;
472 else
473 plat->init_type = USB_INIT_HOST;
474 } else {
475 return -EINVAL;
476 }
477
478 return 0;
479}
480
481static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
482{
483 struct usb_platdata *plat = dev_get_platdata(dev);
484 const char *mode;
485
Simon Glassdd79d6e2017-01-17 16:52:55 -0700486 mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "dr_mode", NULL);
Peng Fan9e3eab32016-12-22 17:06:42 +0800487 if (mode) {
488 if (strcmp(mode, "peripheral") == 0)
489 plat->init_type = USB_INIT_DEVICE;
490 else if (strcmp(mode, "host") == 0)
491 plat->init_type = USB_INIT_HOST;
492 else if (strcmp(mode, "otg") == 0)
493 return ehci_usb_phy_mode(dev);
494 else
495 return -EINVAL;
496
497 return 0;
498 }
499
500 return ehci_usb_phy_mode(dev);
501}
502
Peng Fan5c363c12016-06-17 14:19:27 +0800503static int ehci_usb_probe(struct udevice *dev)
504{
505 struct usb_platdata *plat = dev_get_platdata(dev);
Simon Glassba1dea42017-05-17 17:18:05 -0600506 struct usb_ehci *ehci = (struct usb_ehci *)devfdt_get_addr(dev);
Peng Fan5c363c12016-06-17 14:19:27 +0800507 struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
Peng Fan13351332016-12-22 17:06:43 +0800508 enum usb_init_type type = plat->init_type;
Peng Fan5c363c12016-06-17 14:19:27 +0800509 struct ehci_hccr *hccr;
510 struct ehci_hcor *hcor;
511 int ret;
512
513 priv->ehci = ehci;
514 priv->portnr = dev->seq;
Peng Fan13351332016-12-22 17:06:43 +0800515 priv->init_type = type;
516
517 ret = device_get_supply_regulator(dev, "vbus-supply",
518 &priv->vbus_supply);
519 if (ret)
520 debug("%s: No vbus supply\n", dev->name);
Peng Fan5c363c12016-06-17 14:19:27 +0800521
522 ret = ehci_mx6_common_init(ehci, priv->portnr);
523 if (ret)
524 return ret;
525
Peng Fan13351332016-12-22 17:06:43 +0800526 if (priv->vbus_supply) {
527 ret = regulator_set_enable(priv->vbus_supply,
528 (type == USB_INIT_DEVICE) ?
529 false : true);
530 if (ret) {
531 puts("Error enabling VBUS supply\n");
532 return ret;
533 }
534 }
Peng Fan5c363c12016-06-17 14:19:27 +0800535
536 if (priv->init_type == USB_INIT_HOST) {
537 setbits_le32(&ehci->usbmode, CM_HOST);
538 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
539 setbits_le32(&ehci->portsc, USB_EN);
540 }
541
542 mdelay(10);
543
544 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
545 hcor = (struct ehci_hcor *)((uint32_t)hccr +
546 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
547
548 return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
549}
550
Peng Fan5c363c12016-06-17 14:19:27 +0800551static const struct udevice_id mx6_usb_ids[] = {
552 { .compatible = "fsl,imx27-usb" },
553 { }
554};
555
556U_BOOT_DRIVER(usb_mx6) = {
557 .name = "ehci_mx6",
558 .id = UCLASS_USB,
559 .of_match = mx6_usb_ids,
Peng Fan9e3eab32016-12-22 17:06:42 +0800560 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata,
Peng Fan5c363c12016-06-17 14:19:27 +0800561 .probe = ehci_usb_probe,
Masahiro Yamadad41919b2016-09-06 22:17:34 +0900562 .remove = ehci_deregister,
Peng Fan5c363c12016-06-17 14:19:27 +0800563 .ops = &ehci_usb_ops,
564 .platdata_auto_alloc_size = sizeof(struct usb_platdata),
565 .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data),
566 .flags = DM_FLAG_ALLOC_PRIV_DMA,
567};
568#endif