blob: 5afe28ea303321a15905c17778a9a6317a9064e9 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sanchayan Maity1b320bd2015-04-15 16:24:27 +05302/*
3 * Copyright (c) 2015 Sanchayan Maity <sanchayan.maity@toradex.com>
4 * Copyright (C) 2015 Toradex AG
5 *
6 * Based on ehci-mx6 driver
Sanchayan Maity1b320bd2015-04-15 16:24:27 +05307 */
8
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +05309#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -060010#include <log.h>
Sanchayan Maity1b320bd2015-04-15 16:24:27 +053011#include <usb.h>
12#include <errno.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060013#include <asm/global_data.h>
Sanchayan Maity1b320bd2015-04-15 16:24:27 +053014#include <linux/compiler.h>
15#include <asm/io.h>
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +053016#include <asm-generic/gpio.h>
Sanchayan Maity1b320bd2015-04-15 16:24:27 +053017#include <asm/arch/clock.h>
18#include <asm/arch/imx-regs.h>
19#include <asm/arch/crm_regs.h>
Stefano Babic33731bc2017-06-29 10:16:06 +020020#include <asm/mach-imx/iomux-v3.h>
21#include <asm/mach-imx/regs-usbphy.h>
Simon Glassdbd79542020-05-10 11:40:11 -060022#include <linux/delay.h>
Mateusz Kulikowski3add69e2016-03-31 23:12:23 +020023#include <usb/ehci-ci.h>
Masahiro Yamada75f82d02018-03-05 01:20:11 +090024#include <linux/libfdt.h>
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +053025#include <fdtdec.h>
Sanchayan Maity1b320bd2015-04-15 16:24:27 +053026
27#include "ehci.h"
28
29#define USB_NC_REG_OFFSET 0x00000800
30
31#define ANADIG_PLL_CTRL_EN_USB_CLKS (1 << 6)
32
33#define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
34#define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
35
36/* USBCMD */
37#define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
38#define UCMD_RESET (1 << 1) /* controller reset */
39
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +053040DECLARE_GLOBAL_DATA_PTR;
41
Sanchayan Maity1b320bd2015-04-15 16:24:27 +053042static const unsigned phy_bases[] = {
43 USB_PHY0_BASE_ADDR,
44 USB_PHY1_BASE_ADDR,
45};
46
47static const unsigned nc_reg_bases[] = {
48 USBC0_BASE_ADDR,
49 USBC1_BASE_ADDR,
50};
51
52static void usb_internal_phy_clock_gate(int index)
53{
54 void __iomem *phy_reg;
55
56 phy_reg = (void __iomem *)phy_bases[index];
57 clrbits_le32(phy_reg + USBPHY_CTRL, USBPHY_CTRL_CLKGATE);
58}
59
60static void usb_power_config(int index)
61{
62 struct anadig_reg __iomem *anadig =
63 (struct anadig_reg __iomem *)ANADIG_BASE_ADDR;
64 void __iomem *pll_ctrl;
65
66 switch (index) {
67 case 0:
68 pll_ctrl = &anadig->pll3_ctrl;
69 clrbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_BYPASS);
70 setbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_ENABLE
71 | ANADIG_PLL3_CTRL_POWERDOWN
72 | ANADIG_PLL_CTRL_EN_USB_CLKS);
73 break;
74 case 1:
75 pll_ctrl = &anadig->pll7_ctrl;
76 clrbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_BYPASS);
77 setbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_ENABLE
78 | ANADIG_PLL7_CTRL_POWERDOWN
79 | ANADIG_PLL_CTRL_EN_USB_CLKS);
80 break;
81 default:
82 return;
83 }
84}
85
86static void usb_phy_enable(int index, struct usb_ehci *ehci)
87{
88 void __iomem *phy_reg;
89 void __iomem *phy_ctrl;
90 void __iomem *usb_cmd;
91
92 phy_reg = (void __iomem *)phy_bases[index];
93 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
94 usb_cmd = (void __iomem *)&ehci->usbcmd;
95
96 /* Stop then Reset */
97 clrbits_le32(usb_cmd, UCMD_RUN_STOP);
98 while (readl(usb_cmd) & UCMD_RUN_STOP)
99 ;
100
101 setbits_le32(usb_cmd, UCMD_RESET);
102 while (readl(usb_cmd) & UCMD_RESET)
103 ;
104
105 /* Reset USBPHY module */
106 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
107 udelay(10);
108
109 /* Remove CLKGATE and SFTRST */
110 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
111 udelay(10);
112
113 /* Power up the PHY */
114 writel(0, phy_reg + USBPHY_PWD);
115
116 /* Enable FS/LS device */
117 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
118 USBPHY_CTRL_ENUTMILEVEL3);
119}
120
121static void usb_oc_config(int index)
122{
123 void __iomem *ctrl;
124
125 ctrl = (void __iomem *)(nc_reg_bases[index] + USB_NC_REG_OFFSET);
126
127 setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
128 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
129}
130
Sanchayan Maityde3737f2015-10-26 18:28:50 +0530131int __weak board_usb_phy_mode(int port)
132{
133 return 0;
134}
135
Sanchayan Maityc417f832015-06-01 18:37:24 +0530136int __weak board_ehci_hcd_init(int port)
137{
138 return 0;
139}
140
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530141int ehci_vf_common_init(struct usb_ehci *ehci, int index)
142{
143 int ret;
144
145 /* Do board specific initialisation */
146 ret = board_ehci_hcd_init(index);
147 if (ret)
148 return ret;
149
150 usb_power_config(index);
151 usb_oc_config(index);
152 usb_internal_phy_clock_gate(index);
153 usb_phy_enable(index, ehci);
154
155 return 0;
156}
157
Sven Schwermer8a3cb9f12018-11-21 08:43:56 +0100158#if !CONFIG_IS_ENABLED(DM_USB)
Sanchayan Maity1b320bd2015-04-15 16:24:27 +0530159int ehci_hcd_init(int index, enum usb_init_type init,
160 struct ehci_hccr **hccr, struct ehci_hcor **hcor)
161{
162 struct usb_ehci *ehci;
Sanchayan Maityde3737f2015-10-26 18:28:50 +0530163 enum usb_init_type type;
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530164 int ret;
Sanchayan Maity1b320bd2015-04-15 16:24:27 +0530165
166 if (index >= ARRAY_SIZE(nc_reg_bases))
167 return -EINVAL;
168
Sanchayan Maity1b320bd2015-04-15 16:24:27 +0530169 ehci = (struct usb_ehci *)nc_reg_bases[index];
170
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530171 ret = ehci_vf_common_init(index);
172 if (ret)
173 return ret;
Sanchayan Maity1b320bd2015-04-15 16:24:27 +0530174
175 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
176 *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
177 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
178
Sanchayan Maityde3737f2015-10-26 18:28:50 +0530179 type = board_usb_phy_mode(index);
180 if (type != init)
181 return -ENODEV;
182
Sanchayan Maity1b320bd2015-04-15 16:24:27 +0530183 if (init == USB_INIT_DEVICE) {
184 setbits_le32(&ehci->usbmode, CM_DEVICE);
185 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
186 setbits_le32(&ehci->portsc, USB_EN);
187 } else if (init == USB_INIT_HOST) {
188 setbits_le32(&ehci->usbmode, CM_HOST);
189 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
190 setbits_le32(&ehci->portsc, USB_EN);
191 }
192
193 return 0;
194}
195
196int ehci_hcd_stop(int index)
197{
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530198 return 0;
199}
200#else
201/* Possible port types (dual role mode) */
202enum dr_mode {
203 DR_MODE_NONE = 0,
204 DR_MODE_HOST, /* supports host operation */
205 DR_MODE_DEVICE, /* supports device operation */
206 DR_MODE_OTG, /* supports both */
207};
208
209struct ehci_vf_priv_data {
210 struct ehci_ctrl ctrl;
211 struct usb_ehci *ehci;
212 struct gpio_desc cdet_gpio;
213 enum usb_init_type init_type;
214 enum dr_mode dr_mode;
215 u32 portnr;
216};
217
Simon Glassaad29ae2020-12-03 16:55:21 -0700218static int vf_usb_of_to_plat(struct udevice *dev)
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530219{
220 struct ehci_vf_priv_data *priv = dev_get_priv(dev);
221 const void *dt_blob = gd->fdt_blob;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700222 int node = dev_of_offset(dev);
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530223 const char *mode;
224
Simon Glass75e534b2020-12-16 21:20:07 -0700225 priv->portnr = dev_seq(dev);
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530226
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900227 priv->ehci = dev_read_addr_ptr(dev);
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530228 mode = fdt_getprop(dt_blob, node, "dr_mode", NULL);
229 if (mode) {
230 if (0 == strcmp(mode, "host")) {
231 priv->dr_mode = DR_MODE_HOST;
232 priv->init_type = USB_INIT_HOST;
233 } else if (0 == strcmp(mode, "peripheral")) {
234 priv->dr_mode = DR_MODE_DEVICE;
235 priv->init_type = USB_INIT_DEVICE;
236 } else if (0 == strcmp(mode, "otg")) {
237 priv->dr_mode = DR_MODE_OTG;
238 /*
239 * We set init_type to device by default when OTG
240 * mode is requested. If a valid gpio is provided
241 * we will switch the init_type based on the state
242 * of the gpio pin.
243 */
244 priv->init_type = USB_INIT_DEVICE;
245 } else {
246 debug("%s: Cannot decode dr_mode '%s'\n",
247 __func__, mode);
248 return -EINVAL;
249 }
250 } else {
251 priv->dr_mode = DR_MODE_HOST;
252 priv->init_type = USB_INIT_HOST;
253 }
254
255 if (priv->dr_mode == DR_MODE_OTG) {
Simon Glass1d9af1f2017-05-30 21:47:09 -0600256 gpio_request_by_name_nodev(offset_to_ofnode(node),
257 "fsl,cdet-gpio", 0, &priv->cdet_gpio,
258 GPIOD_IS_IN);
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530259 if (dm_gpio_is_valid(&priv->cdet_gpio)) {
260 if (dm_gpio_get_value(&priv->cdet_gpio))
261 priv->init_type = USB_INIT_DEVICE;
262 else
263 priv->init_type = USB_INIT_HOST;
264 }
265 }
266
267 return 0;
268}
269
270static int vf_init_after_reset(struct ehci_ctrl *dev)
271{
272 struct ehci_vf_priv_data *priv = dev->priv;
273 enum usb_init_type type = priv->init_type;
274 struct usb_ehci *ehci = priv->ehci;
275 int ret;
276
277 ret = ehci_vf_common_init(priv->ehci, priv->portnr);
278 if (ret)
279 return ret;
280
281 if (type == USB_INIT_DEVICE)
282 return 0;
283
284 setbits_le32(&ehci->usbmode, CM_HOST);
285 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
286 setbits_le32(&ehci->portsc, USB_EN);
287
288 mdelay(10);
289
Sanchayan Maity1b320bd2015-04-15 16:24:27 +0530290 return 0;
291}
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530292
293static const struct ehci_ops vf_ehci_ops = {
294 .init_after_reset = vf_init_after_reset
295};
296
297static int vf_usb_bind(struct udevice *dev)
298{
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530299 /*
300 * Without this hack, if we return ENODEV for USB Controller 0, on
301 * probe for the next controller, USB Controller 1 will be given a
302 * sequence number of 0. This conflicts with our requirement of
303 * sequence numbers while initialising the peripherals.
Simon Glass579d8042020-12-16 21:20:21 -0700304 *
305 * FIXME: Check that this still works OK with the new sequence numbers
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530306 */
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530307
308 return 0;
309}
310
311static int ehci_usb_probe(struct udevice *dev)
312{
Simon Glassb75b15b2020-12-03 16:55:23 -0700313 struct usb_plat *plat = dev_get_plat(dev);
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530314 struct ehci_vf_priv_data *priv = dev_get_priv(dev);
315 struct usb_ehci *ehci = priv->ehci;
316 struct ehci_hccr *hccr;
317 struct ehci_hcor *hcor;
318 int ret;
319
320 ret = ehci_vf_common_init(ehci, priv->portnr);
321 if (ret)
322 return ret;
323
324 if (priv->init_type != plat->init_type)
325 return -ENODEV;
326
327 if (priv->init_type == USB_INIT_HOST) {
328 setbits_le32(&ehci->usbmode, CM_HOST);
329 writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
330 setbits_le32(&ehci->portsc, USB_EN);
331 }
332
333 mdelay(10);
334
335 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
336 hcor = (struct ehci_hcor *)((uint32_t)hccr +
337 HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
338
339 return ehci_register(dev, hccr, hcor, &vf_ehci_ops, 0, priv->init_type);
340}
341
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530342static const struct udevice_id vf_usb_ids[] = {
343 { .compatible = "fsl,vf610-usb" },
344 { }
345};
346
347U_BOOT_DRIVER(usb_ehci) = {
348 .name = "ehci_vf",
349 .id = UCLASS_USB,
350 .of_match = vf_usb_ids,
351 .bind = vf_usb_bind,
352 .probe = ehci_usb_probe,
Masahiro Yamada70580842016-10-14 10:29:37 +0900353 .remove = ehci_deregister,
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530354 .ops = &ehci_usb_ops,
Simon Glassaad29ae2020-12-03 16:55:21 -0700355 .of_to_plat = vf_usb_of_to_plat,
Simon Glassb75b15b2020-12-03 16:55:23 -0700356 .plat_auto = sizeof(struct usb_plat),
Simon Glass8a2b47f2020-12-03 16:55:17 -0700357 .priv_auto = sizeof(struct ehci_vf_priv_data),
Sanchayan Maityebe7d1a2016-08-09 23:44:59 +0530358 .flags = DM_FLAG_ALLOC_PRIV_DMA,
359};
360#endif