blob: 54f868420d55af335df8341a74c9b9a42942fb9e [file] [log] [blame]
Wolfgang Grandegger3f467522012-02-08 22:33:25 +00001/*
2 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
3 * Copyright (C) 2010 Freescale Semiconductor, Inc.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
Wolfgang Grandegger3f467522012-02-08 22:33:25 +00006 */
7
8#include <common.h>
9#include <usb.h>
10#include <errno.h>
11#include <linux/compiler.h>
12#include <usb/ehci-fsl.h>
13#include <asm/io.h>
14#include <asm/arch/imx-regs.h>
15#include <asm/arch/clock.h>
Troy Kiskyaf2a35f2012-07-19 08:18:22 +000016#include <asm/imx-common/iomux-v3.h>
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000017
18#include "ehci.h"
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000019
20#define USB_OTGREGS_OFFSET 0x000
21#define USB_H1REGS_OFFSET 0x200
22#define USB_H2REGS_OFFSET 0x400
23#define USB_H3REGS_OFFSET 0x600
24#define USB_OTHERREGS_OFFSET 0x800
25
26#define USB_H1_CTRL_OFFSET 0x04
27
28#define USBPHY_CTRL 0x00000030
29#define USBPHY_CTRL_SET 0x00000034
30#define USBPHY_CTRL_CLR 0x00000038
31#define USBPHY_CTRL_TOG 0x0000003c
32
33#define USBPHY_PWD 0x00000000
34#define USBPHY_CTRL_SFTRST 0x80000000
35#define USBPHY_CTRL_CLKGATE 0x40000000
36#define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
37#define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
Troy Kiskyd1a52862013-10-10 15:27:59 -070038#define USBPHY_CTRL_OTG_ID 0x08000000
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000039
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000040#define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
41#define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
42
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000043#define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
44#define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
45#define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
46#define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
47
Adrian Alonso35554fc2015-08-06 15:43:17 -050048#define USBNC_OFFSET 0x200
49#define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
50#define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
51#define UCTRL_PM (1 << 9) /* OTG Power Mask */
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000052#define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
53#define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
54
55/* USBCMD */
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000056#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
57#define UCMD_RESET (1 << 1) /* controller reset */
58
Adrian Alonso35554fc2015-08-06 15:43:17 -050059#if defined(CONFIG_MX6)
Troy Kiskyd1a52862013-10-10 15:27:59 -070060static const unsigned phy_bases[] = {
61 USB_PHY0_BASE_ADDR,
62 USB_PHY1_BASE_ADDR,
63};
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000064
Troy Kiskyd1a52862013-10-10 15:27:59 -070065static void usb_internal_phy_clock_gate(int index, int on)
66{
67 void __iomem *phy_reg;
68
69 if (index >= ARRAY_SIZE(phy_bases))
70 return;
71
72 phy_reg = (void __iomem *)phy_bases[index];
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000073 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
Adrian Alonsoe38ff302015-08-06 15:43:15 -050074 writel(USBPHY_CTRL_CLKGATE, phy_reg);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000075}
76
Troy Kiskyd1a52862013-10-10 15:27:59 -070077static void usb_power_config(int index)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000078{
Wolfgang Grandegger3f29d962012-05-02 04:36:39 +000079 struct anatop_regs __iomem *anatop =
80 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
Troy Kiskyd1a52862013-10-10 15:27:59 -070081 void __iomem *chrg_detect;
82 void __iomem *pll_480_ctrl_clr;
83 void __iomem *pll_480_ctrl_set;
84
85 switch (index) {
86 case 0:
87 chrg_detect = &anatop->usb1_chrg_detect;
88 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
89 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
90 break;
91 case 1:
92 chrg_detect = &anatop->usb2_chrg_detect;
93 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
94 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
95 break;
96 default:
97 return;
98 }
Wolfgang Grandegger3f467522012-02-08 22:33:25 +000099 /*
Troy Kiskyd1a52862013-10-10 15:27:59 -0700100 * Some phy and power's special controls
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000101 * 1. The external charger detector needs to be disabled
102 * or the signal at DP will be poor
Troy Kiskyd1a52862013-10-10 15:27:59 -0700103 * 2. The PLL's power and output to usb
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000104 * is totally controlled by IC, so the Software only needs
105 * to enable them at initializtion.
106 */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500107 writel(ANADIG_USB2_CHRG_DETECT_EN_B |
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000108 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700109 chrg_detect);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000110
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500111 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700112 pll_480_ctrl_clr);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000113
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500114 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000115 ANADIG_USB2_PLL_480_CTRL_POWER |
116 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
Troy Kiskyd1a52862013-10-10 15:27:59 -0700117 pll_480_ctrl_set);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000118}
119
Troy Kiskyd1a52862013-10-10 15:27:59 -0700120/* Return 0 : host node, <>0 : device mode */
121static int usb_phy_enable(int index, struct usb_ehci *ehci)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000122{
Troy Kiskyd1a52862013-10-10 15:27:59 -0700123 void __iomem *phy_reg;
124 void __iomem *phy_ctrl;
125 void __iomem *usb_cmd;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000126
Troy Kiskyd1a52862013-10-10 15:27:59 -0700127 if (index >= ARRAY_SIZE(phy_bases))
128 return 0;
129
130 phy_reg = (void __iomem *)phy_bases[index];
131 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
132 usb_cmd = (void __iomem *)&ehci->usbcmd;
133
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000134 /* Stop then Reset */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500135 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
136 while (readl(usb_cmd) & UCMD_RUN_STOP)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000137 ;
138
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500139 setbits_le32(usb_cmd, UCMD_RESET);
140 while (readl(usb_cmd) & UCMD_RESET)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000141 ;
142
143 /* Reset USBPHY module */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500144 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000145 udelay(10);
146
147 /* Remove CLKGATE and SFTRST */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500148 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000149 udelay(10);
150
151 /* Power up the PHY */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500152 writel(0, phy_reg + USBPHY_PWD);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000153 /* enable FS/LS device */
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500154 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
155 USBPHY_CTRL_ENUTMILEVEL3);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000156
Peng Fan229dbba2014-11-10 08:50:39 +0800157 return 0;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000158}
159
Peng Fan229dbba2014-11-10 08:50:39 +0800160int usb_phy_mode(int port)
161{
162 void __iomem *phy_reg;
163 void __iomem *phy_ctrl;
164 u32 val;
165
166 phy_reg = (void __iomem *)phy_bases[port];
167 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
168
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500169 val = readl(phy_ctrl);
Peng Fan229dbba2014-11-10 08:50:39 +0800170
171 if (val & USBPHY_CTRL_OTG_ID)
172 return USB_INIT_DEVICE;
173 else
174 return USB_INIT_HOST;
175}
176
Adrian Alonso35554fc2015-08-06 15:43:17 -0500177/* Base address for this IP block is 0x02184800 */
178struct usbnc_regs {
179 u32 ctrl[4]; /* otg/host1-3 */
180 u32 uh2_hsic_ctrl;
181 u32 uh3_hsic_ctrl;
182 u32 otg_phy_ctrl_0;
183 u32 uh1_phy_ctrl_0;
184};
185#elif defined(CONFIG_MX7)
186struct usbnc_regs {
187 u32 ctrl1;
188 u32 ctrl2;
189 u32 reserve1[10];
190 u32 phy_cfg1;
191 u32 phy_cfg2;
192 u32 phy_status;
193 u32 reserve2[4];
194 u32 adp_cfg1;
195 u32 adp_cfg2;
196 u32 adp_status;
197};
198
199static void usb_power_config(int index)
200{
201 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
202 (0x10000 * index) + USBNC_OFFSET);
203 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
204
205 /* Enable usb_otg_id detection */
206 setbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
207}
208
209int usb_phy_mode(int port)
210{
211 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
212 (0x10000 * port) + USBNC_OFFSET);
213 void __iomem *status = (void __iomem *)(&usbnc->phy_status);
214 u32 val;
215
216 val = readl(status);
217
218 if (val & USBNC_PHYSTATUS_ID_DIG)
219 return USB_INIT_DEVICE;
220 else
221 return USB_INIT_HOST;
222}
223#endif
224
225static void usb_oc_config(int index)
226{
227#if defined(CONFIG_MX6)
228 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
229 USB_OTHERREGS_OFFSET);
230 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
231#elif defined(CONFIG_MX7)
232 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
233 (0x10000 * index) + USBNC_OFFSET);
234 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
235#endif
236
237#if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
238 /* mx6qarm2 seems to required a different setting*/
239 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
240#else
241 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
242#endif
243
244#if defined(CONFIG_MX6)
245 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
246#elif defined(CONFIG_MX7)
247 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS | UCTRL_PM);
248#endif
249}
250
Adrian Alonso74f06102015-08-06 15:43:16 -0500251/**
252 * board_ehci_hcd_init - override usb phy mode
253 * @port: usb host/otg port
254 *
255 * Target board specific, override usb_phy_mode.
256 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
257 * left disconnected in this case usb_phy_mode will not be able to identify
258 * the phy mode that usb port is used.
259 * Machine file overrides board_usb_phy_mode.
260 *
261 * Return: USB_INIT_DEVICE or USB_INIT_HOST
262 */
Peng Fan229dbba2014-11-10 08:50:39 +0800263int __weak board_usb_phy_mode(int port)
264{
265 return usb_phy_mode(port);
266}
267
Adrian Alonso74f06102015-08-06 15:43:16 -0500268/**
269 * board_ehci_hcd_init - set usb vbus voltage
270 * @port: usb otg port
271 *
272 * Target board specific, setup iomux pad to setup supply vbus voltage
273 * for usb otg port. Machine board file overrides board_ehci_hcd_init
274 *
275 * Return: 0 Success
276 */
Benoît Thébaudeauf22e4fa2012-11-13 09:58:35 +0000277int __weak board_ehci_hcd_init(int port)
278{
279 return 0;
280}
281
Adrian Alonso74f06102015-08-06 15:43:16 -0500282/**
283 * board_ehci_power - enables/disables usb vbus voltage
284 * @port: usb otg port
285 * @on: on/off vbus voltage
286 *
287 * Enables/disables supply vbus voltage for usb otg port.
288 * Machine board file overrides board_ehci_power
289 *
290 * Return: 0 Success
291 */
Troy Kiskyd1a52862013-10-10 15:27:59 -0700292int __weak board_ehci_power(int port, int on)
293{
294 return 0;
295}
296
Troy Kisky127efc42013-10-10 15:27:57 -0700297int ehci_hcd_init(int index, enum usb_init_type init,
298 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000299{
Troy Kiskyd1a52862013-10-10 15:27:59 -0700300 enum usb_init_type type;
Adrian Alonso35554fc2015-08-06 15:43:17 -0500301#if defined(CONFIG_MX6)
302 u32 controller_spacing = 0x200;
303#elif defined(CONFIG_MX7)
304 u32 controller_spacing = 0x10000;
305#endif
Ye.Li5546ad02014-09-15 17:23:14 +0800306 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
Adrian Alonso35554fc2015-08-06 15:43:17 -0500307 (controller_spacing * index));
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000308
Troy Kiskyd1a52862013-10-10 15:27:59 -0700309 if (index > 3)
310 return -EINVAL;
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000311 enable_usboh3_clk(1);
312 mdelay(1);
313
314 /* Do board specific initialization */
Troy Kiskyd1a52862013-10-10 15:27:59 -0700315 board_ehci_hcd_init(index);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000316
Troy Kiskyd1a52862013-10-10 15:27:59 -0700317 usb_power_config(index);
318 usb_oc_config(index);
Adrian Alonso35554fc2015-08-06 15:43:17 -0500319
320#if defined(CONFIG_MX6)
Troy Kiskyd1a52862013-10-10 15:27:59 -0700321 usb_internal_phy_clock_gate(index, 1);
Peng Fan229dbba2014-11-10 08:50:39 +0800322 usb_phy_enable(index, ehci);
Adrian Alonso35554fc2015-08-06 15:43:17 -0500323#endif
Peng Fan229dbba2014-11-10 08:50:39 +0800324 type = board_usb_phy_mode(index);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000325
Lucas Stach676ae062012-09-26 00:14:35 +0200326 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
327 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
328 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000329
Troy Kiskyd1a52862013-10-10 15:27:59 -0700330 if ((type == init) || (type == USB_INIT_DEVICE))
331 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
332 if (type != init)
333 return -ENODEV;
334 if (type == USB_INIT_DEVICE)
335 return 0;
Adrian Alonso35554fc2015-08-06 15:43:17 -0500336
Troy Kiskyd1a52862013-10-10 15:27:59 -0700337 setbits_le32(&ehci->usbmode, CM_HOST);
Adrian Alonsoe38ff302015-08-06 15:43:15 -0500338 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000339 setbits_le32(&ehci->portsc, USB_EN);
340
341 mdelay(10);
342
343 return 0;
344}
345
Lucas Stach676ae062012-09-26 00:14:35 +0200346int ehci_hcd_stop(int index)
Wolfgang Grandegger3f467522012-02-08 22:33:25 +0000347{
348 return 0;
349}