1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
4 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
7 #include <linux/kernel.h>
8 #include <linux/module.h>
10 #include <linux/platform_device.h>
11 #include <linux/clk.h>
12 #include <linux/delay.h>
13 #include <linux/usb/otg.h>
14 #include <linux/usb/ulpi.h>
15 #include <linux/slab.h>
16 #include <linux/usb.h>
17 #include <linux/usb/hcd.h>
18 #include <linux/platform_data/usb-ehci-mxc.h>
21 #define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
23 static const char hcd_name
[] = "ehci-mxc";
25 #define ULPI_VIEWPORT_OFFSET 0x170
27 struct ehci_mxc_priv
{
28 struct clk
*usbclk
, *ahbclk
, *phyclk
;
31 static struct hc_driver __read_mostly ehci_mxc_hc_driver
;
33 static const struct ehci_driver_overrides ehci_mxc_overrides __initconst
= {
34 .extra_priv_size
= sizeof(struct ehci_mxc_priv
),
37 static int ehci_mxc_drv_probe(struct platform_device
*pdev
)
39 struct mxc_usbh_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
43 struct ehci_mxc_priv
*priv
;
44 struct device
*dev
= &pdev
->dev
;
45 struct ehci_hcd
*ehci
;
48 dev_err(dev
, "No platform data given, bailing out.\n");
52 irq
= platform_get_irq(pdev
, 0);
54 hcd
= usb_create_hcd(&ehci_mxc_hc_driver
, dev
, dev_name(dev
));
58 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
59 hcd
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
60 if (IS_ERR(hcd
->regs
)) {
61 ret
= PTR_ERR(hcd
->regs
);
64 hcd
->rsrc_start
= res
->start
;
65 hcd
->rsrc_len
= resource_size(res
);
68 ehci
= hcd_to_ehci(hcd
);
69 priv
= (struct ehci_mxc_priv
*) ehci
->priv
;
72 priv
->usbclk
= devm_clk_get(&pdev
->dev
, "ipg");
73 if (IS_ERR(priv
->usbclk
)) {
74 ret
= PTR_ERR(priv
->usbclk
);
77 clk_prepare_enable(priv
->usbclk
);
79 priv
->ahbclk
= devm_clk_get(&pdev
->dev
, "ahb");
80 if (IS_ERR(priv
->ahbclk
)) {
81 ret
= PTR_ERR(priv
->ahbclk
);
84 clk_prepare_enable(priv
->ahbclk
);
86 /* "dr" device has its own clock on i.MX51 */
87 priv
->phyclk
= devm_clk_get(&pdev
->dev
, "phy");
88 if (IS_ERR(priv
->phyclk
))
91 clk_prepare_enable(priv
->phyclk
);
94 /* call platform specific init function */
96 ret
= pdata
->init(pdev
);
98 dev_err(dev
, "platform init failed\n");
101 /* platforms need some time to settle changed IO settings */
105 /* EHCI registers start at offset 0x100 */
106 ehci
->caps
= hcd
->regs
+ 0x100;
107 ehci
->regs
= hcd
->regs
+ 0x100 +
108 HC_LENGTH(ehci
, ehci_readl(ehci
, &ehci
->caps
->hc_capbase
));
110 /* set up the PORTSCx register */
111 ehci_writel(ehci
, pdata
->portsc
, &ehci
->regs
->port_status
[0]);
113 /* is this really needed? */
116 /* Initialize the transceiver */
118 pdata
->otg
->io_priv
= hcd
->regs
+ ULPI_VIEWPORT_OFFSET
;
119 ret
= usb_phy_init(pdata
->otg
);
121 dev_err(dev
, "unable to init transceiver, probably missing\n");
125 ret
= otg_set_vbus(pdata
->otg
->otg
, 1);
127 dev_err(dev
, "unable to enable vbus on transceiver\n");
132 platform_set_drvdata(pdev
, hcd
);
134 ret
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
138 device_wakeup_enable(hcd
->self
.controller
);
142 if (pdata
&& pdata
->exit
)
146 clk_disable_unprepare(priv
->phyclk
);
148 clk_disable_unprepare(priv
->ahbclk
);
150 clk_disable_unprepare(priv
->usbclk
);
156 static int ehci_mxc_drv_remove(struct platform_device
*pdev
)
158 struct mxc_usbh_platform_data
*pdata
= dev_get_platdata(&pdev
->dev
);
159 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
160 struct ehci_hcd
*ehci
= hcd_to_ehci(hcd
);
161 struct ehci_mxc_priv
*priv
= (struct ehci_mxc_priv
*) ehci
->priv
;
165 if (pdata
&& pdata
->exit
)
168 if (pdata
&& pdata
->otg
)
169 usb_phy_shutdown(pdata
->otg
);
171 clk_disable_unprepare(priv
->usbclk
);
172 clk_disable_unprepare(priv
->ahbclk
);
175 clk_disable_unprepare(priv
->phyclk
);
181 MODULE_ALIAS("platform:mxc-ehci");
183 static struct platform_driver ehci_mxc_driver
= {
184 .probe
= ehci_mxc_drv_probe
,
185 .remove
= ehci_mxc_drv_remove
,
186 .shutdown
= usb_hcd_platform_shutdown
,
192 static int __init
ehci_mxc_init(void)
197 pr_info("%s: " DRIVER_DESC
"\n", hcd_name
);
199 ehci_init_driver(&ehci_mxc_hc_driver
, &ehci_mxc_overrides
);
200 return platform_driver_register(&ehci_mxc_driver
);
202 module_init(ehci_mxc_init
);
204 static void __exit
ehci_mxc_cleanup(void)
206 platform_driver_unregister(&ehci_mxc_driver
);
208 module_exit(ehci_mxc_cleanup
);
210 MODULE_DESCRIPTION(DRIVER_DESC
);
211 MODULE_AUTHOR("Sascha Hauer");
212 MODULE_LICENSE("GPL");