blob: c11279867c7ca0d2412d9cea5105763419729df3 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +01002/*
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4 * Copyright (C) 2010 Freescale Semiconductor, Inc.
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +01005 */
6
7#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -06008#include <log.h>
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +01009#include <usb.h>
10#include <errno.h>
11#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060012#include <linux/delay.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020013#include <usb/ehci-ci.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060014#include <asm/global_data.h>
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010015#include <asm/io.h>
16#include <asm/arch/imx-regs.h>
17#include <asm/arch/clock.h>
Lukasz Majewskidbc70f82019-04-04 12:26:52 +020018#include <dm.h>
19#include <power/regulator.h>
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010020
21#include "ehci.h"
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010022
23#define MX5_USBOTHER_REGS_OFFSET 0x800
24
25
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000026#define MXC_OTG_OFFSET 0
27#define MXC_H1_OFFSET 0x200
28#define MXC_H2_OFFSET 0x400
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000029#define MXC_H3_OFFSET 0x600
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010030
31#define MXC_USBCTRL_OFFSET 0
32#define MXC_USB_PHY_CTR_FUNC_OFFSET 0x8
33#define MXC_USB_PHY_CTR_FUNC2_OFFSET 0xc
34#define MXC_USB_CTRL_1_OFFSET 0x10
35#define MXC_USBH2CTRL_OFFSET 0x14
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000036#define MXC_USBH3CTRL_OFFSET 0x18
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010037
38/* USB_CTRL */
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000039/* OTG wakeup intr enable */
40#define MXC_OTG_UCTRL_OWIE_BIT (1 << 27)
41/* OTG power mask */
42#define MXC_OTG_UCTRL_OPM_BIT (1 << 24)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000043/* OTG power pin polarity */
44#define MXC_OTG_UCTRL_O_PWR_POL_BIT (1 << 24)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000045/* Host1 ULPI interrupt enable */
46#define MXC_H1_UCTRL_H1UIE_BIT (1 << 12)
47/* HOST1 wakeup intr enable */
48#define MXC_H1_UCTRL_H1WIE_BIT (1 << 11)
49/* HOST1 power mask */
50#define MXC_H1_UCTRL_H1PM_BIT (1 << 8)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000051/* HOST1 power pin polarity */
52#define MXC_H1_UCTRL_H1_PWR_POL_BIT (1 << 8)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010053
54/* USB_PHY_CTRL_FUNC */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000055/* OTG Polarity of Overcurrent */
56#define MXC_OTG_PHYCTRL_OC_POL_BIT (1 << 9)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000057/* OTG Disable Overcurrent Event */
58#define MXC_OTG_PHYCTRL_OC_DIS_BIT (1 << 8)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000059/* UH1 Polarity of Overcurrent */
60#define MXC_H1_OC_POL_BIT (1 << 6)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000061/* UH1 Disable Overcurrent Event */
62#define MXC_H1_OC_DIS_BIT (1 << 5)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000063/* OTG Power Pin Polarity */
64#define MXC_OTG_PHYCTRL_PWR_POL_BIT (1 << 3)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010065
66/* USBH2CTRL */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000067#define MXC_H2_UCTRL_H2_OC_POL_BIT (1 << 31)
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000068#define MXC_H2_UCTRL_H2_OC_DIS_BIT (1 << 30)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000069#define MXC_H2_UCTRL_H2UIE_BIT (1 << 8)
70#define MXC_H2_UCTRL_H2WIE_BIT (1 << 7)
71#define MXC_H2_UCTRL_H2PM_BIT (1 << 4)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000072#define MXC_H2_UCTRL_H2_PWR_POL_BIT (1 << 4)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010073
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000074/* USBH3CTRL */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000075#define MXC_H3_UCTRL_H3_OC_POL_BIT (1 << 31)
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000076#define MXC_H3_UCTRL_H3_OC_DIS_BIT (1 << 30)
77#define MXC_H3_UCTRL_H3UIE_BIT (1 << 8)
78#define MXC_H3_UCTRL_H3WIE_BIT (1 << 7)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000079#define MXC_H3_UCTRL_H3_PWR_POL_BIT (1 << 4)
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000080
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010081/* USB_CTRL_1 */
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000082#define MXC_USB_CTRL_UH1_EXT_CLK_EN (1 << 25)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010083
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010084int mxc_set_usbcontrol(int port, unsigned int flags)
85{
86 unsigned int v;
87 void __iomem *usb_base = (void __iomem *)OTG_BASE_ADDR;
88 void __iomem *usbother_base;
89 int ret = 0;
90
91 usbother_base = usb_base + MX5_USBOTHER_REGS_OFFSET;
92
93 switch (port) {
94 case 0: /* OTG port */
95 if (flags & MXC_EHCI_INTERNAL_PHY) {
96 v = __raw_readl(usbother_base +
97 MXC_USB_PHY_CTR_FUNC_OFFSET);
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000098 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
99 v |= MXC_OTG_PHYCTRL_OC_POL_BIT;
100 else
101 v &= ~MXC_OTG_PHYCTRL_OC_POL_BIT;
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100102 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100103 /* OC/USBPWR is used */
104 v &= ~MXC_OTG_PHYCTRL_OC_DIS_BIT;
Benoît Thébaudeau2b2a4152012-11-13 09:56:30 +0000105 else
106 /* OC/USBPWR is not used */
107 v |= MXC_OTG_PHYCTRL_OC_DIS_BIT;
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000108#ifdef CONFIG_MX51
109 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
110 v |= MXC_OTG_PHYCTRL_PWR_POL_BIT;
111 else
112 v &= ~MXC_OTG_PHYCTRL_PWR_POL_BIT;
113#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100114 __raw_writel(v, usbother_base +
115 MXC_USB_PHY_CTR_FUNC_OFFSET);
116
117 v = __raw_readl(usbother_base + MXC_USBCTRL_OFFSET);
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000118#ifdef CONFIG_MX51
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100119 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100120 v &= ~MXC_OTG_UCTRL_OPM_BIT;
Benoît Thébaudeaub7647f32012-11-13 09:56:44 +0000121 else
122 v |= MXC_OTG_UCTRL_OPM_BIT;
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000123#endif
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000124#ifdef CONFIG_MX53
125 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
126 v |= MXC_OTG_UCTRL_O_PWR_POL_BIT;
127 else
128 v &= ~MXC_OTG_UCTRL_O_PWR_POL_BIT;
129#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100130 __raw_writel(v, usbother_base + MXC_USBCTRL_OFFSET);
131 }
132 break;
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000133 case 1: /* Host 1 ULPI */
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100134#ifdef CONFIG_MX51
135 /* The clock for the USBH1 ULPI port will come externally
136 from the PHY. */
137 v = __raw_readl(usbother_base + MXC_USB_CTRL_1_OFFSET);
138 __raw_writel(v | MXC_USB_CTRL_UH1_EXT_CLK_EN, usbother_base +
139 MXC_USB_CTRL_1_OFFSET);
140#endif
141
142 v = __raw_readl(usbother_base + MXC_USBCTRL_OFFSET);
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000143#ifdef CONFIG_MX51
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100144 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000145 v &= ~MXC_H1_UCTRL_H1PM_BIT; /* H1 power mask unused */
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100146 else
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000147 v |= MXC_H1_UCTRL_H1PM_BIT; /* H1 power mask used */
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000148#endif
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000149#ifdef CONFIG_MX53
150 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
151 v |= MXC_H1_UCTRL_H1_PWR_POL_BIT;
152 else
153 v &= ~MXC_H1_UCTRL_H1_PWR_POL_BIT;
154#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100155 __raw_writel(v, usbother_base + MXC_USBCTRL_OFFSET);
156
157 v = __raw_readl(usbother_base + MXC_USB_PHY_CTR_FUNC_OFFSET);
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000158 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
159 v |= MXC_H1_OC_POL_BIT;
160 else
161 v &= ~MXC_H1_OC_POL_BIT;
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100162 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
163 v &= ~MXC_H1_OC_DIS_BIT; /* OC is used */
164 else
165 v |= MXC_H1_OC_DIS_BIT; /* OC is not used */
166 __raw_writel(v, usbother_base + MXC_USB_PHY_CTR_FUNC_OFFSET);
167
168 break;
169 case 2: /* Host 2 ULPI */
170 v = __raw_readl(usbother_base + MXC_USBH2CTRL_OFFSET);
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000171#ifdef CONFIG_MX51
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100172 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000173 v &= ~MXC_H2_UCTRL_H2PM_BIT; /* H2 power mask unused */
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100174 else
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000175 v |= MXC_H2_UCTRL_H2PM_BIT; /* H2 power mask used */
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000176#endif
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000177#ifdef CONFIG_MX53
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000178 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
179 v |= MXC_H2_UCTRL_H2_OC_POL_BIT;
180 else
181 v &= ~MXC_H2_UCTRL_H2_OC_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000182 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
183 v &= ~MXC_H2_UCTRL_H2_OC_DIS_BIT; /* OC is used */
184 else
185 v |= MXC_H2_UCTRL_H2_OC_DIS_BIT; /* OC is not used */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000186 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
187 v |= MXC_H2_UCTRL_H2_PWR_POL_BIT;
188 else
189 v &= ~MXC_H2_UCTRL_H2_PWR_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000190#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100191 __raw_writel(v, usbother_base + MXC_USBH2CTRL_OFFSET);
192 break;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000193#ifdef CONFIG_MX53
194 case 3: /* Host 3 ULPI */
195 v = __raw_readl(usbother_base + MXC_USBH3CTRL_OFFSET);
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000196 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
197 v |= MXC_H3_UCTRL_H3_OC_POL_BIT;
198 else
199 v &= ~MXC_H3_UCTRL_H3_OC_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000200 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
201 v &= ~MXC_H3_UCTRL_H3_OC_DIS_BIT; /* OC is used */
202 else
203 v |= MXC_H3_UCTRL_H3_OC_DIS_BIT; /* OC is not used */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000204 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
205 v |= MXC_H3_UCTRL_H3_PWR_POL_BIT;
206 else
207 v &= ~MXC_H3_UCTRL_H3_PWR_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000208 __raw_writel(v, usbother_base + MXC_USBH3CTRL_OFFSET);
209 break;
210#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100211 }
212
213 return ret;
214}
215
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000216int __weak board_ehci_hcd_init(int port)
Marek Vasutde09b792011-11-24 05:14:00 +0100217{
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000218 return 0;
Marek Vasutde09b792011-11-24 05:14:00 +0100219}
220
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000221void __weak board_ehci_hcd_postinit(struct usb_ehci *ehci, int port)
222{
223}
Marek Vasutde09b792011-11-24 05:14:00 +0100224
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600225__weak void mx5_ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
226 uint32_t *reg)
227{
228 mdelay(50);
229}
230
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200231struct ehci_mx5_priv_data {
232 struct ehci_ctrl ctrl;
233 struct usb_ehci *ehci;
234 struct udevice *vbus_supply;
235 enum usb_init_type init_type;
236 int portnr;
237};
238
239static const struct ehci_ops mx5_ehci_ops = {
240 .powerup_fixup = mx5_ehci_powerup_fixup,
241};
242
Simon Glassaad29ae2020-12-03 16:55:21 -0700243static int ehci_usb_of_to_plat(struct udevice *dev)
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200244{
Simon Glassb75b15b2020-12-03 16:55:23 -0700245 struct usb_plat *plat = dev_get_plat(dev);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200246 const char *mode;
247
248 mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "dr_mode", NULL);
249 if (mode) {
250 if (strcmp(mode, "peripheral") == 0)
251 plat->init_type = USB_INIT_DEVICE;
252 else if (strcmp(mode, "host") == 0)
253 plat->init_type = USB_INIT_HOST;
254 else
255 return -EINVAL;
256 }
257
258 return 0;
259}
260
261static int ehci_usb_probe(struct udevice *dev)
262{
Simon Glassb75b15b2020-12-03 16:55:23 -0700263 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900264 struct usb_ehci *ehci = dev_read_addr_ptr(dev);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200265 struct ehci_mx5_priv_data *priv = dev_get_priv(dev);
266 enum usb_init_type type = plat->init_type;
267 struct ehci_hccr *hccr;
268 struct ehci_hcor *hcor;
269 int ret;
270
271 set_usboh3_clk();
272 enable_usboh3_clk(true);
273 set_usb_phy_clk();
274 enable_usb_phy1_clk(true);
275 enable_usb_phy2_clk(true);
276 mdelay(1);
277
278 priv->ehci = ehci;
Simon Glass75e534b2020-12-16 21:20:07 -0700279 priv->portnr = dev_seq(dev);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200280 priv->init_type = type;
281
282 ret = device_get_supply_regulator(dev, "vbus-supply",
283 &priv->vbus_supply);
284 if (ret)
285 debug("%s: No vbus supply\n", dev->name);
286
287 if (!ret && priv->vbus_supply) {
288 ret = regulator_set_enable(priv->vbus_supply,
289 (type == USB_INIT_DEVICE) ?
290 false : true);
291 if (ret) {
292 puts("Error enabling VBUS supply\n");
293 return ret;
294 }
295 }
296
297 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
298 hcor = (struct ehci_hcor *)((uint32_t)hccr +
299 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
300 setbits_le32(&ehci->usbmode, CM_HOST);
301
Tom Rinib9796e82022-12-04 10:04:56 -0500302 __raw_writel(CFG_MXC_USB_PORTSC, &ehci->portsc);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200303 setbits_le32(&ehci->portsc, USB_EN);
304
Tom Rinib9796e82022-12-04 10:04:56 -0500305 mxc_set_usbcontrol(priv->portnr, CFG_MXC_USB_FLAGS);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200306 mdelay(10);
307
308 return ehci_register(dev, hccr, hcor, &mx5_ehci_ops, 0,
309 priv->init_type);
310}
311
312static const struct udevice_id mx5_usb_ids[] = {
313 { .compatible = "fsl,imx53-usb" },
314 { }
315};
316
317U_BOOT_DRIVER(usb_mx5) = {
318 .name = "ehci_mx5",
319 .id = UCLASS_USB,
320 .of_match = mx5_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700321 .of_to_plat = ehci_usb_of_to_plat,
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200322 .probe = ehci_usb_probe,
323 .remove = ehci_deregister,
324 .ops = &ehci_usb_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700325 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700326 .priv_auto = sizeof(struct ehci_mx5_priv_data),
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200327 .flags = DM_FLAG_ALLOC_PRIV_DMA,
328};