blob: dedb80bb8d40a5c37132fbdb0d0c569534aeb9c8 [file] [log] [blame]
Daniel Mack7e8d5cd2009-10-28 01:14:59 +01001/*
2 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
Alan Sterndba63b22013-01-23 13:26:15 -050020#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/io.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010023#include <linux/platform_device.h>
24#include <linux/clk.h>
25#include <linux/delay.h>
26#include <linux/usb/otg.h>
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010027#include <linux/usb/ulpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Alan Sterndba63b22013-01-23 13:26:15 -050029#include <linux/usb.h>
30#include <linux/usb/hcd.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010031
Arnd Bergmann82906b12012-08-24 15:14:29 +020032#include <linux/platform_data/usb-ehci-mxc.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010033
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010034#include <asm/mach-types.h>
35
Alan Sterndba63b22013-01-23 13:26:15 -050036#include "ehci.h"
37
38#define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
39
40static const char hcd_name[] = "ehci-mxc";
41
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010042#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010043
44struct ehci_mxc_priv {
Sascha Hauerc9437402012-04-25 16:39:06 +020045 struct clk *usbclk, *ahbclk, *phyclk;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010046};
47
Alan Sterndba63b22013-01-23 13:26:15 -050048static struct hc_driver __read_mostly ehci_mxc_hc_driver;
Matthieu CASTET65fd4272010-09-06 18:26:56 +020049
Alan Sterndba63b22013-01-23 13:26:15 -050050static const struct ehci_driver_overrides ehci_mxc_overrides __initdata = {
51 .extra_priv_size = sizeof(struct ehci_mxc_priv),
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010052};
53
54static int ehci_mxc_drv_probe(struct platform_device *pdev)
55{
56 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
57 struct usb_hcd *hcd;
58 struct resource *res;
Uwe Kleine-König724c8522010-11-02 10:30:57 +010059 int irq, ret;
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010060 unsigned int flags;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010061 struct ehci_mxc_priv *priv;
62 struct device *dev = &pdev->dev;
Fabio Estevam0247a7b2010-12-15 22:31:28 -020063 struct ehci_hcd *ehci;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010064
65 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
66
67 if (!pdata) {
68 dev_err(dev, "No platform data given, bailing out.\n");
69 return -EINVAL;
70 }
71
72 irq = platform_get_irq(pdev, 0);
73
74 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
75 if (!hcd)
76 return -ENOMEM;
77
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010078 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
79 if (!res) {
80 dev_err(dev, "Found HC with no register addr. Check setup!\n");
81 ret = -ENODEV;
Julia Lawallaf09c062012-07-29 21:46:13 +020082 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010083 }
84
85 hcd->rsrc_start = res->start;
86 hcd->rsrc_len = resource_size(res);
87
Julia Lawallaf09c062012-07-29 21:46:13 +020088 hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010089 if (!hcd->regs) {
90 dev_err(dev, "error mapping memory\n");
91 ret = -EFAULT;
Julia Lawallaf09c062012-07-29 21:46:13 +020092 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010093 }
94
Alan Sterndba63b22013-01-23 13:26:15 -050095 hcd->has_tt = 1;
96 ehci = hcd_to_ehci(hcd);
97 priv = (struct ehci_mxc_priv *) ehci->priv;
98
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010099 /* enable clocks */
Julia Lawallaf09c062012-07-29 21:46:13 +0200100 priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100101 if (IS_ERR(priv->usbclk)) {
102 ret = PTR_ERR(priv->usbclk);
Julia Lawallaf09c062012-07-29 21:46:13 +0200103 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100104 }
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100105 clk_prepare_enable(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100106
Julia Lawallaf09c062012-07-29 21:46:13 +0200107 priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
Sascha Hauerc9437402012-04-25 16:39:06 +0200108 if (IS_ERR(priv->ahbclk)) {
109 ret = PTR_ERR(priv->ahbclk);
110 goto err_clk_ahb;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100111 }
Sascha Hauerc9437402012-04-25 16:39:06 +0200112 clk_prepare_enable(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100113
Eric Bénard3bb80292011-01-13 14:53:17 +0100114 /* "dr" device has its own clock on i.MX51 */
Julia Lawallaf09c062012-07-29 21:46:13 +0200115 priv->phyclk = devm_clk_get(&pdev->dev, "phy");
Sascha Hauerc9437402012-04-25 16:39:06 +0200116 if (IS_ERR(priv->phyclk))
117 priv->phyclk = NULL;
118 if (priv->phyclk)
119 clk_prepare_enable(priv->phyclk);
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100120
121
122 /* call platform specific init function */
123 if (pdata->init) {
124 ret = pdata->init(pdev);
125 if (ret) {
126 dev_err(dev, "platform init failed\n");
127 goto err_init;
128 }
129 /* platforms need some time to settle changed IO settings */
130 mdelay(10);
131 }
132
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200133 /* EHCI registers start at offset 0x100 */
134 ehci->caps = hcd->regs + 0x100;
135 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200136 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200137
138 /* set up the PORTSCx register */
139 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
140
141 /* is this really needed? */
142 msleep(10);
143
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100144 /* Initialize the transceiver */
145 if (pdata->otg) {
146 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200147 ret = usb_phy_init(pdata->otg);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200148 if (ret) {
149 dev_err(dev, "unable to init transceiver, probably missing\n");
150 ret = -ENODEV;
151 goto err_add;
152 }
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200153 ret = otg_set_vbus(pdata->otg->otg, 1);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200154 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100155 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200156 goto err_add;
157 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100158 }
159
Alan Sterndba63b22013-01-23 13:26:15 -0500160 platform_set_drvdata(pdev, hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100161
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800162 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100163 if (ret)
164 goto err_add;
165
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100166 if (pdata->otg) {
167 /*
168 * efikamx and efikasb have some hardware bug which is
169 * preventing usb to work unless CHRGVBUS is set.
170 * It's in violation of USB specs
171 */
172 if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200173 flags = usb_phy_io_read(pdata->otg,
174 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100175 flags |= ULPI_OTG_CTRL_CHRGVBUS;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200176 ret = usb_phy_io_write(pdata->otg, flags,
177 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100178 if (ret) {
179 dev_err(dev, "unable to set CHRVBUS\n");
180 goto err_add;
181 }
182 }
183 }
184
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100185 return 0;
186
187err_add:
188 if (pdata && pdata->exit)
189 pdata->exit(pdev);
190err_init:
Julia Lawallaf09c062012-07-29 21:46:13 +0200191 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200192 clk_disable_unprepare(priv->phyclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200193
194 clk_disable_unprepare(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100195err_clk_ahb:
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100196 clk_disable_unprepare(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100197err_alloc:
198 usb_put_hcd(hcd);
199 return ret;
200}
201
202static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
203{
204 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
Alan Sterndba63b22013-01-23 13:26:15 -0500205 struct usb_hcd *hcd = platform_get_drvdata(pdev);
206 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
207 struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
208
209 usb_remove_hcd(hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100210
211 if (pdata && pdata->exit)
212 pdata->exit(pdev);
213
214 if (pdata->otg)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200215 usb_phy_shutdown(pdata->otg);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100216
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100217 clk_disable_unprepare(priv->usbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200218 clk_disable_unprepare(priv->ahbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200219
Julia Lawallaf09c062012-07-29 21:46:13 +0200220 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200221 clk_disable_unprepare(priv->phyclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100222
Alan Sterndba63b22013-01-23 13:26:15 -0500223 usb_put_hcd(hcd);
224 platform_set_drvdata(pdev, NULL);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100225 return 0;
226}
227
228static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
229{
Alan Sterndba63b22013-01-23 13:26:15 -0500230 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100231
232 if (hcd->driver->shutdown)
233 hcd->driver->shutdown(hcd);
234}
235
236MODULE_ALIAS("platform:mxc-ehci");
237
238static struct platform_driver ehci_mxc_driver = {
239 .probe = ehci_mxc_drv_probe,
Alan Sterndba63b22013-01-23 13:26:15 -0500240 .remove = ehci_mxc_drv_remove,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100241 .shutdown = ehci_mxc_drv_shutdown,
242 .driver = {
243 .name = "mxc-ehci",
244 },
245};
Alan Sterndba63b22013-01-23 13:26:15 -0500246
247static int __init ehci_mxc_init(void)
248{
249 if (usb_disabled())
250 return -ENODEV;
251
252 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
253
254 ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
255 return platform_driver_register(&ehci_mxc_driver);
256}
257module_init(ehci_mxc_init);
258
259static void __exit ehci_mxc_cleanup(void)
260{
261 platform_driver_unregister(&ehci_mxc_driver);
262}
263module_exit(ehci_mxc_cleanup);
264
265MODULE_DESCRIPTION(DRIVER_DESC);
266MODULE_AUTHOR("Sascha Hauer");
267MODULE_LICENSE("GPL");