blob: a38c8c8e5b0d9fdc6d25c61f4f79322bddfdf02d [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>
Arnd Bergmann82906b12012-08-24 15:14:29 +020031#include <linux/platform_data/usb-ehci-mxc.h>
Alan Sterndba63b22013-01-23 13:26:15 -050032#include "ehci.h"
33
34#define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
35
36static const char hcd_name[] = "ehci-mxc";
37
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010038#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010039
40struct ehci_mxc_priv {
Sascha Hauerc9437402012-04-25 16:39:06 +020041 struct clk *usbclk, *ahbclk, *phyclk;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010042};
43
Alan Sterndba63b22013-01-23 13:26:15 -050044static struct hc_driver __read_mostly ehci_mxc_hc_driver;
Matthieu CASTET65fd4272010-09-06 18:26:56 +020045
Alan Sterndba63b22013-01-23 13:26:15 -050046static const struct ehci_driver_overrides ehci_mxc_overrides __initdata = {
47 .extra_priv_size = sizeof(struct ehci_mxc_priv),
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010048};
49
50static int ehci_mxc_drv_probe(struct platform_device *pdev)
51{
52 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
53 struct usb_hcd *hcd;
54 struct resource *res;
Uwe Kleine-König724c8522010-11-02 10:30:57 +010055 int irq, ret;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010056 struct ehci_mxc_priv *priv;
57 struct device *dev = &pdev->dev;
Fabio Estevam0247a7b2010-12-15 22:31:28 -020058 struct ehci_hcd *ehci;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010059
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010060 if (!pdata) {
61 dev_err(dev, "No platform data given, bailing out.\n");
62 return -EINVAL;
63 }
64
65 irq = platform_get_irq(pdev, 0);
66
67 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
68 if (!hcd)
69 return -ENOMEM;
70
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010071 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
72 if (!res) {
73 dev_err(dev, "Found HC with no register addr. Check setup!\n");
74 ret = -ENODEV;
Julia Lawallaf09c062012-07-29 21:46:13 +020075 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010076 }
77
78 hcd->rsrc_start = res->start;
79 hcd->rsrc_len = resource_size(res);
80
Thierry Reding148e1132013-01-21 11:09:22 +010081 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
82 if (IS_ERR(hcd->regs)) {
83 ret = PTR_ERR(hcd->regs);
Julia Lawallaf09c062012-07-29 21:46:13 +020084 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010085 }
86
Alan Sterndba63b22013-01-23 13:26:15 -050087 hcd->has_tt = 1;
88 ehci = hcd_to_ehci(hcd);
89 priv = (struct ehci_mxc_priv *) ehci->priv;
90
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010091 /* enable clocks */
Julia Lawallaf09c062012-07-29 21:46:13 +020092 priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010093 if (IS_ERR(priv->usbclk)) {
94 ret = PTR_ERR(priv->usbclk);
Julia Lawallaf09c062012-07-29 21:46:13 +020095 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010096 }
Sascha Hauer198ad2c2012-03-07 20:58:21 +010097 clk_prepare_enable(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010098
Julia Lawallaf09c062012-07-29 21:46:13 +020099 priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
Sascha Hauerc9437402012-04-25 16:39:06 +0200100 if (IS_ERR(priv->ahbclk)) {
101 ret = PTR_ERR(priv->ahbclk);
102 goto err_clk_ahb;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100103 }
Sascha Hauerc9437402012-04-25 16:39:06 +0200104 clk_prepare_enable(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100105
Eric Bénard3bb80292011-01-13 14:53:17 +0100106 /* "dr" device has its own clock on i.MX51 */
Julia Lawallaf09c062012-07-29 21:46:13 +0200107 priv->phyclk = devm_clk_get(&pdev->dev, "phy");
Sascha Hauerc9437402012-04-25 16:39:06 +0200108 if (IS_ERR(priv->phyclk))
109 priv->phyclk = NULL;
110 if (priv->phyclk)
111 clk_prepare_enable(priv->phyclk);
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100112
113
114 /* call platform specific init function */
115 if (pdata->init) {
116 ret = pdata->init(pdev);
117 if (ret) {
118 dev_err(dev, "platform init failed\n");
119 goto err_init;
120 }
121 /* platforms need some time to settle changed IO settings */
122 mdelay(10);
123 }
124
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200125 /* EHCI registers start at offset 0x100 */
126 ehci->caps = hcd->regs + 0x100;
127 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200128 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200129
130 /* set up the PORTSCx register */
131 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
132
133 /* is this really needed? */
134 msleep(10);
135
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100136 /* Initialize the transceiver */
137 if (pdata->otg) {
138 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200139 ret = usb_phy_init(pdata->otg);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200140 if (ret) {
141 dev_err(dev, "unable to init transceiver, probably missing\n");
142 ret = -ENODEV;
143 goto err_add;
144 }
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200145 ret = otg_set_vbus(pdata->otg->otg, 1);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200146 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100147 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200148 goto err_add;
149 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100150 }
151
Alan Sterndba63b22013-01-23 13:26:15 -0500152 platform_set_drvdata(pdev, hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100153
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800154 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100155 if (ret)
156 goto err_add;
157
158 return 0;
159
160err_add:
161 if (pdata && pdata->exit)
162 pdata->exit(pdev);
163err_init:
Julia Lawallaf09c062012-07-29 21:46:13 +0200164 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200165 clk_disable_unprepare(priv->phyclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200166
167 clk_disable_unprepare(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100168err_clk_ahb:
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100169 clk_disable_unprepare(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100170err_alloc:
171 usb_put_hcd(hcd);
172 return ret;
173}
174
Dmitry Torokhov39d35682013-02-24 00:55:07 -0800175static int ehci_mxc_drv_remove(struct platform_device *pdev)
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100176{
177 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
Alan Sterndba63b22013-01-23 13:26:15 -0500178 struct usb_hcd *hcd = platform_get_drvdata(pdev);
179 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
180 struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
181
182 usb_remove_hcd(hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100183
184 if (pdata && pdata->exit)
185 pdata->exit(pdev);
186
187 if (pdata->otg)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200188 usb_phy_shutdown(pdata->otg);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100189
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100190 clk_disable_unprepare(priv->usbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200191 clk_disable_unprepare(priv->ahbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200192
Julia Lawallaf09c062012-07-29 21:46:13 +0200193 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200194 clk_disable_unprepare(priv->phyclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100195
Alan Sterndba63b22013-01-23 13:26:15 -0500196 usb_put_hcd(hcd);
197 platform_set_drvdata(pdev, NULL);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100198 return 0;
199}
200
201static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
202{
Alan Sterndba63b22013-01-23 13:26:15 -0500203 struct usb_hcd *hcd = platform_get_drvdata(pdev);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100204
205 if (hcd->driver->shutdown)
206 hcd->driver->shutdown(hcd);
207}
208
209MODULE_ALIAS("platform:mxc-ehci");
210
211static struct platform_driver ehci_mxc_driver = {
212 .probe = ehci_mxc_drv_probe,
Alan Sterndba63b22013-01-23 13:26:15 -0500213 .remove = ehci_mxc_drv_remove,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100214 .shutdown = ehci_mxc_drv_shutdown,
215 .driver = {
216 .name = "mxc-ehci",
217 },
218};
Alan Sterndba63b22013-01-23 13:26:15 -0500219
220static int __init ehci_mxc_init(void)
221{
222 if (usb_disabled())
223 return -ENODEV;
224
225 pr_info("%s: " DRIVER_DESC "\n", hcd_name);
226
227 ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
228 return platform_driver_register(&ehci_mxc_driver);
229}
230module_init(ehci_mxc_init);
231
232static void __exit ehci_mxc_cleanup(void)
233{
234 platform_driver_unregister(&ehci_mxc_driver);
235}
236module_exit(ehci_mxc_cleanup);
237
238MODULE_DESCRIPTION(DRIVER_DESC);
239MODULE_AUTHOR("Sascha Hauer");
240MODULE_LICENSE("GPL");