blob: d8f521befe14e03ed3cc173de71ee2c4ea8b18f8 [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
Simon Glass0f2af882020-05-10 11:40:05 -06007#include <log.h>
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +01008#include <usb.h>
9#include <errno.h>
10#include <linux/compiler.h>
Simon Glassdbd79542020-05-10 11:40:11 -060011#include <linux/delay.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020012#include <usb/ehci-ci.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010014#include <asm/io.h>
15#include <asm/arch/imx-regs.h>
16#include <asm/arch/clock.h>
Lukasz Majewskidbc70f82019-04-04 12:26:52 +020017#include <dm.h>
18#include <power/regulator.h>
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010019
20#include "ehci.h"
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010021
22#define MX5_USBOTHER_REGS_OFFSET 0x800
23
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000024#define MXC_OTG_OFFSET 0
25#define MXC_H1_OFFSET 0x200
26#define MXC_H2_OFFSET 0x400
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000027#define MXC_H3_OFFSET 0x600
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010028
29#define MXC_USBCTRL_OFFSET 0
30#define MXC_USB_PHY_CTR_FUNC_OFFSET 0x8
31#define MXC_USB_PHY_CTR_FUNC2_OFFSET 0xc
32#define MXC_USB_CTRL_1_OFFSET 0x10
33#define MXC_USBH2CTRL_OFFSET 0x14
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000034#define MXC_USBH3CTRL_OFFSET 0x18
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010035
36/* USB_CTRL */
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000037/* OTG wakeup intr enable */
38#define MXC_OTG_UCTRL_OWIE_BIT (1 << 27)
39/* OTG power mask */
40#define MXC_OTG_UCTRL_OPM_BIT (1 << 24)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000041/* OTG power pin polarity */
42#define MXC_OTG_UCTRL_O_PWR_POL_BIT (1 << 24)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000043/* Host1 ULPI interrupt enable */
44#define MXC_H1_UCTRL_H1UIE_BIT (1 << 12)
45/* HOST1 wakeup intr enable */
46#define MXC_H1_UCTRL_H1WIE_BIT (1 << 11)
47/* HOST1 power mask */
48#define MXC_H1_UCTRL_H1PM_BIT (1 << 8)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000049/* HOST1 power pin polarity */
50#define MXC_H1_UCTRL_H1_PWR_POL_BIT (1 << 8)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010051
52/* USB_PHY_CTRL_FUNC */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000053/* OTG Polarity of Overcurrent */
54#define MXC_OTG_PHYCTRL_OC_POL_BIT (1 << 9)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000055/* OTG Disable Overcurrent Event */
56#define MXC_OTG_PHYCTRL_OC_DIS_BIT (1 << 8)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000057/* UH1 Polarity of Overcurrent */
58#define MXC_H1_OC_POL_BIT (1 << 6)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000059/* UH1 Disable Overcurrent Event */
60#define MXC_H1_OC_DIS_BIT (1 << 5)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000061/* OTG Power Pin Polarity */
62#define MXC_OTG_PHYCTRL_PWR_POL_BIT (1 << 3)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010063
64/* USBH2CTRL */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000065#define MXC_H2_UCTRL_H2_OC_POL_BIT (1 << 31)
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000066#define MXC_H2_UCTRL_H2_OC_DIS_BIT (1 << 30)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000067#define MXC_H2_UCTRL_H2UIE_BIT (1 << 8)
68#define MXC_H2_UCTRL_H2WIE_BIT (1 << 7)
69#define MXC_H2_UCTRL_H2PM_BIT (1 << 4)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000070#define MXC_H2_UCTRL_H2_PWR_POL_BIT (1 << 4)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010071
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000072/* USBH3CTRL */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000073#define MXC_H3_UCTRL_H3_OC_POL_BIT (1 << 31)
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000074#define MXC_H3_UCTRL_H3_OC_DIS_BIT (1 << 30)
75#define MXC_H3_UCTRL_H3UIE_BIT (1 << 8)
76#define MXC_H3_UCTRL_H3WIE_BIT (1 << 7)
Benoît Thébaudeau17971a92012-11-13 09:57:27 +000077#define MXC_H3_UCTRL_H3_PWR_POL_BIT (1 << 4)
Benoît Thébaudeau284a4592012-11-13 09:57:14 +000078
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010079/* USB_CTRL_1 */
Benoît Thébaudeaucb109722012-11-13 09:56:15 +000080#define MXC_USB_CTRL_UH1_EXT_CLK_EN (1 << 25)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010081
Fabio Estevam49873af2024-08-28 10:25:27 -030082#ifndef CFG_MXC_USB_PORTSC
83#define CFG_MXC_USB_PORTSC (PORT_PTS_UTMI | PORT_PTS_PTW)
84#endif
85
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +010086int mxc_set_usbcontrol(int port, unsigned int flags)
87{
88 unsigned int v;
89 void __iomem *usb_base = (void __iomem *)OTG_BASE_ADDR;
90 void __iomem *usbother_base;
91 int ret = 0;
92
93 usbother_base = usb_base + MX5_USBOTHER_REGS_OFFSET;
94
95 switch (port) {
96 case 0: /* OTG port */
97 if (flags & MXC_EHCI_INTERNAL_PHY) {
98 v = __raw_readl(usbother_base +
99 MXC_USB_PHY_CTR_FUNC_OFFSET);
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000100 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
101 v |= MXC_OTG_PHYCTRL_OC_POL_BIT;
102 else
103 v &= ~MXC_OTG_PHYCTRL_OC_POL_BIT;
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100104 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100105 /* OC/USBPWR is used */
106 v &= ~MXC_OTG_PHYCTRL_OC_DIS_BIT;
Benoît Thébaudeau2b2a4152012-11-13 09:56:30 +0000107 else
108 /* OC/USBPWR is not used */
109 v |= MXC_OTG_PHYCTRL_OC_DIS_BIT;
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000110#ifdef CONFIG_MX51
111 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
112 v |= MXC_OTG_PHYCTRL_PWR_POL_BIT;
113 else
114 v &= ~MXC_OTG_PHYCTRL_PWR_POL_BIT;
115#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100116 __raw_writel(v, usbother_base +
117 MXC_USB_PHY_CTR_FUNC_OFFSET);
118
119 v = __raw_readl(usbother_base + MXC_USBCTRL_OFFSET);
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000120#ifdef CONFIG_MX51
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100121 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100122 v &= ~MXC_OTG_UCTRL_OPM_BIT;
Benoît Thébaudeaub7647f32012-11-13 09:56:44 +0000123 else
124 v |= MXC_OTG_UCTRL_OPM_BIT;
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000125#endif
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000126#ifdef CONFIG_MX53
127 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
128 v |= MXC_OTG_UCTRL_O_PWR_POL_BIT;
129 else
130 v &= ~MXC_OTG_UCTRL_O_PWR_POL_BIT;
131#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100132 __raw_writel(v, usbother_base + MXC_USBCTRL_OFFSET);
133 }
134 break;
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000135 case 1: /* Host 1 ULPI */
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100136#ifdef CONFIG_MX51
137 /* The clock for the USBH1 ULPI port will come externally
138 from the PHY. */
139 v = __raw_readl(usbother_base + MXC_USB_CTRL_1_OFFSET);
140 __raw_writel(v | MXC_USB_CTRL_UH1_EXT_CLK_EN, usbother_base +
141 MXC_USB_CTRL_1_OFFSET);
142#endif
143
144 v = __raw_readl(usbother_base + MXC_USBCTRL_OFFSET);
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000145#ifdef CONFIG_MX51
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100146 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000147 v &= ~MXC_H1_UCTRL_H1PM_BIT; /* H1 power mask unused */
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100148 else
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000149 v |= MXC_H1_UCTRL_H1PM_BIT; /* H1 power mask used */
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000150#endif
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000151#ifdef CONFIG_MX53
152 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
153 v |= MXC_H1_UCTRL_H1_PWR_POL_BIT;
154 else
155 v &= ~MXC_H1_UCTRL_H1_PWR_POL_BIT;
156#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100157 __raw_writel(v, usbother_base + MXC_USBCTRL_OFFSET);
158
159 v = __raw_readl(usbother_base + MXC_USB_PHY_CTR_FUNC_OFFSET);
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000160 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
161 v |= MXC_H1_OC_POL_BIT;
162 else
163 v &= ~MXC_H1_OC_POL_BIT;
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100164 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
165 v &= ~MXC_H1_OC_DIS_BIT; /* OC is used */
166 else
167 v |= MXC_H1_OC_DIS_BIT; /* OC is not used */
168 __raw_writel(v, usbother_base + MXC_USB_PHY_CTR_FUNC_OFFSET);
169
170 break;
171 case 2: /* Host 2 ULPI */
172 v = __raw_readl(usbother_base + MXC_USBH2CTRL_OFFSET);
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000173#ifdef CONFIG_MX51
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100174 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000175 v &= ~MXC_H2_UCTRL_H2PM_BIT; /* H2 power mask unused */
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100176 else
Benoît Thébaudeaucb109722012-11-13 09:56:15 +0000177 v |= MXC_H2_UCTRL_H2PM_BIT; /* H2 power mask used */
Benoît Thébaudeau46a53ab2012-11-13 09:56:59 +0000178#endif
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000179#ifdef CONFIG_MX53
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000180 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
181 v |= MXC_H2_UCTRL_H2_OC_POL_BIT;
182 else
183 v &= ~MXC_H2_UCTRL_H2_OC_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000184 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
185 v &= ~MXC_H2_UCTRL_H2_OC_DIS_BIT; /* OC is used */
186 else
187 v |= MXC_H2_UCTRL_H2_OC_DIS_BIT; /* OC is not used */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000188 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
189 v |= MXC_H2_UCTRL_H2_PWR_POL_BIT;
190 else
191 v &= ~MXC_H2_UCTRL_H2_PWR_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000192#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100193 __raw_writel(v, usbother_base + MXC_USBH2CTRL_OFFSET);
194 break;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000195#ifdef CONFIG_MX53
196 case 3: /* Host 3 ULPI */
197 v = __raw_readl(usbother_base + MXC_USBH3CTRL_OFFSET);
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000198 if (flags & MXC_EHCI_OC_PIN_ACTIVE_LOW)
199 v |= MXC_H3_UCTRL_H3_OC_POL_BIT;
200 else
201 v &= ~MXC_H3_UCTRL_H3_OC_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000202 if (flags & MXC_EHCI_POWER_PINS_ENABLED)
203 v &= ~MXC_H3_UCTRL_H3_OC_DIS_BIT; /* OC is used */
204 else
205 v |= MXC_H3_UCTRL_H3_OC_DIS_BIT; /* OC is not used */
Benoît Thébaudeau17971a92012-11-13 09:57:27 +0000206 if (flags & MXC_EHCI_PWR_PIN_ACTIVE_HIGH)
207 v |= MXC_H3_UCTRL_H3_PWR_POL_BIT;
208 else
209 v &= ~MXC_H3_UCTRL_H3_PWR_POL_BIT;
Benoît Thébaudeau284a4592012-11-13 09:57:14 +0000210 __raw_writel(v, usbother_base + MXC_USBH3CTRL_OFFSET);
211 break;
212#endif
Wolfgang Grandeggerd17f2d22011-11-11 14:03:36 +0100213 }
214
215 return ret;
216}
217
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000218int __weak board_ehci_hcd_init(int port)
Marek Vasutde09b792011-11-24 05:14:00 +0100219{
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000220 return 0;
Marek Vasutde09b792011-11-24 05:14:00 +0100221}
222
Benoît Thébaudeau98023c12012-11-13 09:58:35 +0000223void __weak board_ehci_hcd_postinit(struct usb_ehci *ehci, int port)
224{
225}
Marek Vasutde09b792011-11-24 05:14:00 +0100226
Simon Glassdc9f3ed2015-03-25 12:22:27 -0600227__weak void mx5_ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg,
228 uint32_t *reg)
229{
230 mdelay(50);
231}
232
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200233struct ehci_mx5_priv_data {
234 struct ehci_ctrl ctrl;
235 struct usb_ehci *ehci;
236 struct udevice *vbus_supply;
237 enum usb_init_type init_type;
238 int portnr;
239};
240
241static const struct ehci_ops mx5_ehci_ops = {
242 .powerup_fixup = mx5_ehci_powerup_fixup,
243};
244
Simon Glassaad29ae2020-12-03 16:55:21 -0700245static int ehci_usb_of_to_plat(struct udevice *dev)
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200246{
Simon Glassb75b15b2020-12-03 16:55:23 -0700247 struct usb_plat *plat = dev_get_plat(dev);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200248 const char *mode;
249
250 mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "dr_mode", NULL);
251 if (mode) {
252 if (strcmp(mode, "peripheral") == 0)
253 plat->init_type = USB_INIT_DEVICE;
254 else if (strcmp(mode, "host") == 0)
255 plat->init_type = USB_INIT_HOST;
256 else
257 return -EINVAL;
258 }
259
260 return 0;
261}
262
263static int ehci_usb_probe(struct udevice *dev)
264{
Simon Glassb75b15b2020-12-03 16:55:23 -0700265 struct usb_plat *plat = dev_get_plat(dev);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900266 struct usb_ehci *ehci = dev_read_addr_ptr(dev);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200267 struct ehci_mx5_priv_data *priv = dev_get_priv(dev);
268 enum usb_init_type type = plat->init_type;
269 struct ehci_hccr *hccr;
270 struct ehci_hcor *hcor;
271 int ret;
272
273 set_usboh3_clk();
274 enable_usboh3_clk(true);
275 set_usb_phy_clk();
276 enable_usb_phy1_clk(true);
277 enable_usb_phy2_clk(true);
278 mdelay(1);
279
280 priv->ehci = ehci;
Simon Glass75e534b2020-12-16 21:20:07 -0700281 priv->portnr = dev_seq(dev);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200282 priv->init_type = type;
283
284 ret = device_get_supply_regulator(dev, "vbus-supply",
285 &priv->vbus_supply);
286 if (ret)
287 debug("%s: No vbus supply\n", dev->name);
288
289 if (!ret && priv->vbus_supply) {
290 ret = regulator_set_enable(priv->vbus_supply,
291 (type == USB_INIT_DEVICE) ?
292 false : true);
293 if (ret) {
294 puts("Error enabling VBUS supply\n");
295 return ret;
296 }
297 }
298
299 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
300 hcor = (struct ehci_hcor *)((uint32_t)hccr +
301 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
302 setbits_le32(&ehci->usbmode, CM_HOST);
303
Tom Rinib9796e82022-12-04 10:04:56 -0500304 __raw_writel(CFG_MXC_USB_PORTSC, &ehci->portsc);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200305 setbits_le32(&ehci->portsc, USB_EN);
306
Tom Rinib9796e82022-12-04 10:04:56 -0500307 mxc_set_usbcontrol(priv->portnr, CFG_MXC_USB_FLAGS);
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200308 mdelay(10);
309
310 return ehci_register(dev, hccr, hcor, &mx5_ehci_ops, 0,
311 priv->init_type);
312}
313
314static const struct udevice_id mx5_usb_ids[] = {
315 { .compatible = "fsl,imx53-usb" },
316 { }
317};
318
319U_BOOT_DRIVER(usb_mx5) = {
320 .name = "ehci_mx5",
321 .id = UCLASS_USB,
322 .of_match = mx5_usb_ids,
Simon Glassaad29ae2020-12-03 16:55:21 -0700323 .of_to_plat = ehci_usb_of_to_plat,
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200324 .probe = ehci_usb_probe,
325 .remove = ehci_deregister,
326 .ops = &ehci_usb_ops,
Simon Glassb75b15b2020-12-03 16:55:23 -0700327 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700328 .priv_auto = sizeof(struct ehci_mx5_priv_data),
Lukasz Majewskidbc70f82019-04-04 12:26:52 +0200329 .flags = DM_FLAG_ALLOC_PRIV_DMA,
330};