blob: 9a614955fc16686af8b03b885746adb74b82d004 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutf7d7f942011-11-08 23:18:26 +00002/*
3 * Freescale i.MX28 USB Host driver
4 *
5 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
6 * on behalf of DENX Software Engineering GmbH
Marek Vasutf7d7f942011-11-08 23:18:26 +00007 */
8
9#include <common.h>
10#include <asm/io.h>
Marek Vasut83e330c2013-02-23 02:43:01 +000011#include <asm/arch/imx-regs.h>
Marek Vasut191a2ce2013-02-23 02:43:02 +000012#include <errno.h>
Simon Glassdbd79542020-05-10 11:40:11 -060013#include <linux/delay.h>
Lukasz Majewski3ed2b182021-12-22 10:55:09 +010014#include <dm.h>
15#include <power/regulator.h>
Marek Vasutf7d7f942011-11-08 23:18:26 +000016
Marek Vasutf7d7f942011-11-08 23:18:26 +000017#include "ehci.h"
18
Marek Vasut191a2ce2013-02-23 02:43:02 +000019/* This DIGCTL register ungates clock to USB */
20#define HW_DIGCTL_CTRL 0x8001c000
21#define HW_DIGCTL_CTRL_USB0_CLKGATE (1 << 2)
22#define HW_DIGCTL_CTRL_USB1_CLKGATE (1 << 16)
Marek Vasutf7d7f942011-11-08 23:18:26 +000023
Marek Vasut191a2ce2013-02-23 02:43:02 +000024struct ehci_mxs_port {
25 uint32_t usb_regs;
Otavio Salvador22f4ff92012-08-05 09:05:31 +000026 struct mxs_usbphy_regs *phy_regs;
Marek Vasut191a2ce2013-02-23 02:43:02 +000027
28 struct mxs_register_32 *pll;
29 uint32_t pll_en_bits;
30 uint32_t pll_dis_bits;
31 uint32_t gate_bits;
32};
33
Lukasz Majewski5fb7cb22021-12-22 10:55:07 +010034static int ehci_mxs_toggle_clock(const struct ehci_mxs_port *port, int enable)
35{
36 struct mxs_register_32 *digctl_ctrl =
37 (struct mxs_register_32 *)HW_DIGCTL_CTRL;
38 int pll_offset, dig_offset;
39
40 if (enable) {
41 pll_offset = offsetof(struct mxs_register_32, reg_set);
42 dig_offset = offsetof(struct mxs_register_32, reg_clr);
43 writel(port->gate_bits, (u32)&digctl_ctrl->reg + dig_offset);
44 writel(port->pll_en_bits, (u32)port->pll + pll_offset);
45 } else {
46 pll_offset = offsetof(struct mxs_register_32, reg_clr);
47 dig_offset = offsetof(struct mxs_register_32, reg_set);
48 writel(port->pll_dis_bits, (u32)port->pll + pll_offset);
49 writel(port->gate_bits, (u32)&digctl_ctrl->reg + dig_offset);
50 }
51
52 return 0;
53}
54
Lukasz Majewskie91edbc2021-12-22 10:55:08 +010055static int __ehci_hcd_init(struct ehci_mxs_port *port, enum usb_init_type init,
56 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
57{
58 u32 usb_base, cap_base;
59 int ret;
60
61 /* Reset the PHY block */
62 writel(USBPHY_CTRL_SFTRST, &port->phy_regs->hw_usbphy_ctrl_set);
63 udelay(10);
64 writel(USBPHY_CTRL_SFTRST | USBPHY_CTRL_CLKGATE,
65 &port->phy_regs->hw_usbphy_ctrl_clr);
66
67 /* Enable USB clock */
68 ret = ehci_mxs_toggle_clock(port, 1);
69 if (ret)
70 return ret;
71
72 /* Start USB PHY */
73 writel(0, &port->phy_regs->hw_usbphy_pwd);
74
75 /* Enable UTMI+ Level 2 and Level 3 compatibility */
76 writel(USBPHY_CTRL_ENUTMILEVEL3 | USBPHY_CTRL_ENUTMILEVEL2 | 1,
77 &port->phy_regs->hw_usbphy_ctrl_set);
78
79 usb_base = port->usb_regs + 0x100;
80 *hccr = (struct ehci_hccr *)usb_base;
81
82 cap_base = ehci_readl(&(*hccr)->cr_capbase);
83 *hcor = (struct ehci_hcor *)(usb_base + HC_LENGTH(cap_base));
84
85 return 0;
86}
87
88static int __ehci_hcd_stop(struct ehci_mxs_port *port)
89{
90 u32 usb_base, cap_base, tmp;
91 struct ehci_hccr *hccr;
92 struct ehci_hcor *hcor;
93
94 /* Stop the USB port */
95 usb_base = port->usb_regs + 0x100;
96 hccr = (struct ehci_hccr *)usb_base;
97 cap_base = ehci_readl(&hccr->cr_capbase);
98 hcor = (struct ehci_hcor *)(usb_base + HC_LENGTH(cap_base));
99
100 tmp = ehci_readl(&hcor->or_usbcmd);
101 tmp &= ~CMD_RUN;
102 ehci_writel(&hcor->or_usbcmd, tmp);
103
104 /* Disable the PHY */
105 tmp = USBPHY_PWD_RXPWDRX | USBPHY_PWD_RXPWDDIFF |
106 USBPHY_PWD_RXPWD1PT1 | USBPHY_PWD_RXPWDENV |
107 USBPHY_PWD_TXPWDV2I | USBPHY_PWD_TXPWDIBIAS |
108 USBPHY_PWD_TXPWDFS;
109 writel(tmp, &port->phy_regs->hw_usbphy_pwd);
110
111 /* Disable USB clock */
112 return ehci_mxs_toggle_clock(port, 0);
113}
114
Lukasz Majewski3ed2b182021-12-22 10:55:09 +0100115#if !CONFIG_IS_ENABLED(DM_USB)
Marek Vasut191a2ce2013-02-23 02:43:02 +0000116static const struct ehci_mxs_port mxs_port[] = {
117#ifdef CONFIG_EHCI_MXS_PORT0
118 {
119 MXS_USBCTRL0_BASE,
120 (struct mxs_usbphy_regs *)MXS_USBPHY0_BASE,
121 (struct mxs_register_32 *)(MXS_CLKCTRL_BASE +
122 offsetof(struct mxs_clkctrl_regs,
123 hw_clkctrl_pll0ctrl0_reg)),
124 CLKCTRL_PLL0CTRL0_EN_USB_CLKS | CLKCTRL_PLL0CTRL0_POWER,
125 CLKCTRL_PLL0CTRL0_EN_USB_CLKS,
126 HW_DIGCTL_CTRL_USB0_CLKGATE,
127 },
128#endif
129#ifdef CONFIG_EHCI_MXS_PORT1
130 {
131 MXS_USBCTRL1_BASE,
132 (struct mxs_usbphy_regs *)MXS_USBPHY1_BASE,
133 (struct mxs_register_32 *)(MXS_CLKCTRL_BASE +
134 offsetof(struct mxs_clkctrl_regs,
135 hw_clkctrl_pll1ctrl0_reg)),
136 CLKCTRL_PLL1CTRL0_EN_USB_CLKS | CLKCTRL_PLL1CTRL0_POWER,
137 CLKCTRL_PLL1CTRL0_EN_USB_CLKS,
138 HW_DIGCTL_CTRL_USB1_CLKGATE,
139 },
140#endif
141};
Marek Vasutf7d7f942011-11-08 23:18:26 +0000142
Marek Vasut809860b2014-04-28 03:38:39 +0200143int __weak board_ehci_hcd_init(int port)
144{
145 return 0;
146}
147
148int __weak board_ehci_hcd_exit(int port)
149{
150 return 0;
151}
152
Troy Kisky7d6bbb92013-10-10 15:27:57 -0700153int ehci_hcd_init(int index, enum usb_init_type init,
154 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
Marek Vasutf7d7f942011-11-08 23:18:26 +0000155{
156
157 int ret;
Marek Vasut191a2ce2013-02-23 02:43:02 +0000158 const struct ehci_mxs_port *port;
Marek Vasutf7d7f942011-11-08 23:18:26 +0000159
Marek Vasut191a2ce2013-02-23 02:43:02 +0000160 if ((index < 0) || (index >= ARRAY_SIZE(mxs_port))) {
161 printf("Invalid port index (index = %d)!\n", index);
162 return -EINVAL;
163 }
164
Marek Vasut809860b2014-04-28 03:38:39 +0200165 ret = board_ehci_hcd_init(index);
166 if (ret)
167 return ret;
168
Marek Vasut191a2ce2013-02-23 02:43:02 +0000169 port = &mxs_port[index];
Lukasz Majewskie91edbc2021-12-22 10:55:08 +0100170 return __ehci_hcd_init(port, init, hccr, hcor);
Marek Vasutf7d7f942011-11-08 23:18:26 +0000171}
172
Lucas Stach3494a4c2012-09-26 00:14:35 +0200173int ehci_hcd_stop(int index)
Marek Vasutf7d7f942011-11-08 23:18:26 +0000174{
175 int ret;
Marek Vasut191a2ce2013-02-23 02:43:02 +0000176 const struct ehci_mxs_port *port;
Marek Vasutf7d7f942011-11-08 23:18:26 +0000177
Marek Vasut191a2ce2013-02-23 02:43:02 +0000178 if ((index < 0) || (index >= ARRAY_SIZE(mxs_port))) {
179 printf("Invalid port index (index = %d)!\n", index);
180 return -EINVAL;
181 }
182
183 port = &mxs_port[index];
Marek Vasutf7d7f942011-11-08 23:18:26 +0000184
Lukasz Majewskie91edbc2021-12-22 10:55:08 +0100185 ret = __ehci_hcd_stop(port);
Marek Vasut809860b2014-04-28 03:38:39 +0200186 board_ehci_hcd_exit(index);
187
Marek Vasut191a2ce2013-02-23 02:43:02 +0000188 return ret;
Marek Vasutf7d7f942011-11-08 23:18:26 +0000189}
Lukasz Majewski3ed2b182021-12-22 10:55:09 +0100190#else /* CONFIG_IS_ENABLED(DM_USB) */
191struct ehci_mxs_priv_data {
192 struct ehci_ctrl ctrl;
193 struct usb_ehci *ehci;
194 struct udevice *vbus_supply;
195 struct ehci_mxs_port port;
196 enum usb_init_type init_type;
197};
198
199/*
200 * Below defines correspond to imx28 clk Linux (v5.15.y)
201 * clock driver to provide proper offset for PHY[01]
202 * devices.
203 */
204#define CLK_USB_PHY0 62
205#define CLK_USB_PHY1 63
206#define PLL0CTRL0(base) ((base) + 0x0000)
207#define PLL1CTRL0(base) ((base) + 0x0020)
208
209static int ehci_usb_ofdata_to_platdata(struct udevice *dev)
210{
211 struct ehci_mxs_priv_data *priv = dev_get_priv(dev);
212 struct usb_plat *plat = dev_get_plat(dev);
213 struct ehci_mxs_port *port = &priv->port;
214 u32 phandle, phy_reg, clk_reg, clk_id;
215 ofnode phy_node, clk_node;
216 const char *mode;
217 int ret;
218
219 mode = ofnode_read_string(dev->node_, "dr_mode");
220 if (mode) {
221 if (strcmp(mode, "peripheral") == 0)
222 plat->init_type = USB_INIT_DEVICE;
223 else if (strcmp(mode, "host") == 0)
224 plat->init_type = USB_INIT_HOST;
225 else
226 return -EINVAL;
227 }
228
229 /* Read base address of the USB IP block */
230 ret = ofnode_read_u32(dev->node_, "reg", &port->usb_regs);
231 if (ret)
232 return ret;
233
234 /* Read base address of the USB PHY IP block */
235 ret = ofnode_read_u32(dev->node_, "fsl,usbphy", &phandle);
236 if (ret)
237 return ret;
238
239 phy_node = ofnode_get_by_phandle(phandle);
240 if (!ofnode_valid(phy_node))
241 return -ENODEV;
242
243 ret = ofnode_read_u32(phy_node, "reg", &phy_reg);
244 if (ret)
245 return ret;
246
247 port->phy_regs = (struct mxs_usbphy_regs *)phy_reg;
248
249 /* Read base address of the CLK IP block and proper ID */
250 ret = ofnode_read_u32_index(phy_node, "clocks", 0, &phandle);
251 if (ret)
252 return ret;
253
254 ret = ofnode_read_u32_index(phy_node, "clocks", 1, &clk_id);
255 if (ret)
256 return ret;
257
258 clk_node = ofnode_get_by_phandle(phandle);
259 if (!ofnode_valid(clk_node))
260 return -ENODEV;
261
262 ret = ofnode_read_u32(clk_node, "reg", &clk_reg);
263 if (ret)
264 return ret;
265
266 port->pll = (struct mxs_register_32 *)clk_reg;
267
268 /* Provide proper offset for USB PHY clocks */
269 if (clk_id == CLK_USB_PHY0)
270 port->pll = PLL0CTRL0(port->pll);
271
272 if (clk_id == CLK_USB_PHY1)
273 port->pll = PLL1CTRL0(port->pll);
274
275 debug("%s: pll_reg: 0x%p clk_id: %d\n", __func__, port->pll, clk_id);
276 /*
277 * On the imx28 the values provided by CLKCTRL_PLL0* defines to are the
278 * same as ones for CLKCTRL_PLL1*. As a result the former can be used
279 * for both ports - i.e. (usb[01]).
280 */
281 port->pll_en_bits = CLKCTRL_PLL0CTRL0_EN_USB_CLKS |
282 CLKCTRL_PLL0CTRL0_POWER;
283 port->pll_dis_bits = CLKCTRL_PLL0CTRL0_EN_USB_CLKS;
284 port->gate_bits = HW_DIGCTL_CTRL_USB0_CLKGATE;
285
286 return 0;
287}
288
289static int ehci_usb_probe(struct udevice *dev)
290{
291 struct usb_plat *plat = dev_get_plat(dev);
292 struct usb_ehci *ehci = dev_read_addr_ptr(dev);
293 struct ehci_mxs_priv_data *priv = dev_get_priv(dev);
294 struct ehci_mxs_port *port = &priv->port;
295 enum usb_init_type type = plat->init_type;
296 struct ehci_hccr *hccr;
297 struct ehci_hcor *hcor;
298 int ret;
299
300 priv->ehci = ehci;
301 priv->init_type = type;
302
303 debug("%s: USB type: %s reg: 0x%x phy_reg 0x%p\n", __func__,
304 type == USB_INIT_HOST ? "HOST" : "DEVICE", port->usb_regs,
305 (uint32_t *)port->phy_regs);
306
307#if CONFIG_IS_ENABLED(DM_REGULATOR)
308 ret = device_get_supply_regulator(dev, "vbus-supply",
309 &priv->vbus_supply);
310 if (ret)
311 debug("%s: No vbus supply\n", dev->name);
312
313 if (!ret && priv->vbus_supply) {
314 ret = regulator_set_enable(priv->vbus_supply,
315 (type == USB_INIT_DEVICE) ?
316 false : true);
317 if (ret) {
318 puts("Error enabling VBUS supply\n");
319 return ret;
320 }
321 }
322#endif
323 ret = __ehci_hcd_init(port, type, &hccr, &hcor);
324 if (ret)
325 return ret;
326
327 mdelay(10);
328 return ehci_register(dev, hccr, hcor, NULL, 0, priv->init_type);
329}
330
331static int ehci_usb_remove(struct udevice *dev)
332{
333 struct ehci_mxs_priv_data *priv = dev_get_priv(dev);
334 struct ehci_mxs_port *port = &priv->port;
335 int ret;
336
337 ret = ehci_deregister(dev);
338 if (ret)
339 return ret;
340
341#if CONFIG_IS_ENABLED(DM_REGULATOR)
342 if (priv->vbus_supply) {
343 ret = regulator_set_enable(priv->vbus_supply, false);
344 if (ret) {
345 puts("Error disabling VBUS supply\n");
346 return ret;
347 }
348 }
349#endif
350 return __ehci_hcd_stop(port);
351}
352
353static const struct udevice_id mxs_usb_ids[] = {
354 { .compatible = "fsl,imx28-usb" },
355 { }
356};
357
358U_BOOT_DRIVER(usb_mxs) = {
359 .name = "ehci_mxs",
360 .id = UCLASS_USB,
361 .of_match = mxs_usb_ids,
362 .of_to_plat = ehci_usb_ofdata_to_platdata,
363 .probe = ehci_usb_probe,
364 .remove = ehci_usb_remove,
365 .ops = &ehci_usb_ops,
366 .plat_auto = sizeof(struct usb_plat),
367 .priv_auto = sizeof(struct ehci_mxs_priv_data),
368 .flags = DM_FLAG_ALLOC_PRIV_DMA,
369};
370#endif /* !CONFIG_IS_ENABLED(DM_USB) */