Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/linville/wirel...
[linux-2.6-block.git] / drivers / usb / host / ehci-mxc.c
CommitLineData
7e8d5cd9
DM
1/*
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
dba63b2f
AS
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/io.h>
7e8d5cd9
DM
23#include <linux/platform_device.h>
24#include <linux/clk.h>
25#include <linux/delay.h>
26#include <linux/usb/otg.h>
a464dc4d 27#include <linux/usb/ulpi.h>
5a0e3ad6 28#include <linux/slab.h>
dba63b2f
AS
29#include <linux/usb.h>
30#include <linux/usb/hcd.h>
7e8d5cd9 31
82906b13 32#include <linux/platform_data/usb-ehci-mxc.h>
7e8d5cd9 33
a464dc4d
APR
34#include <asm/mach-types.h>
35
dba63b2f
AS
36#include "ehci.h"
37
38#define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
39
40static const char hcd_name[] = "ehci-mxc";
41
7e8d5cd9 42#define ULPI_VIEWPORT_OFFSET 0x170
7e8d5cd9
DM
43
44struct ehci_mxc_priv {
c943740c 45 struct clk *usbclk, *ahbclk, *phyclk;
7e8d5cd9
DM
46};
47
dba63b2f 48static struct hc_driver __read_mostly ehci_mxc_hc_driver;
7e8d5cd9 49
dba63b2f
AS
50static const struct ehci_driver_overrides ehci_mxc_overrides __initdata = {
51 .extra_priv_size = sizeof(struct ehci_mxc_priv),
7e8d5cd9
DM
52};
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;
724c8525 59 int irq, ret;
a464dc4d 60 unsigned int flags;
7e8d5cd9
DM
61 struct ehci_mxc_priv *priv;
62 struct device *dev = &pdev->dev;
0247a7bc 63 struct ehci_hcd *ehci;
7e8d5cd9
DM
64
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
7e8d5cd9
DM
78 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;
af09c060 82 goto err_alloc;
7e8d5cd9
DM
83 }
84
85 hcd->rsrc_start = res->start;
86 hcd->rsrc_len = resource_size(res);
87
af09c060 88 hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
7e8d5cd9
DM
89 if (!hcd->regs) {
90 dev_err(dev, "error mapping memory\n");
91 ret = -EFAULT;
af09c060 92 goto err_alloc;
7e8d5cd9
DM
93 }
94
dba63b2f
AS
95 hcd->has_tt = 1;
96 ehci = hcd_to_ehci(hcd);
97 priv = (struct ehci_mxc_priv *) ehci->priv;
98
7e8d5cd9 99 /* enable clocks */
af09c060 100 priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
7e8d5cd9
DM
101 if (IS_ERR(priv->usbclk)) {
102 ret = PTR_ERR(priv->usbclk);
af09c060 103 goto err_alloc;
7e8d5cd9 104 }
198ad2ce 105 clk_prepare_enable(priv->usbclk);
7e8d5cd9 106
af09c060 107 priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
c943740c
SH
108 if (IS_ERR(priv->ahbclk)) {
109 ret = PTR_ERR(priv->ahbclk);
110 goto err_clk_ahb;
7e8d5cd9 111 }
c943740c 112 clk_prepare_enable(priv->ahbclk);
7e8d5cd9 113
3bb8029a 114 /* "dr" device has its own clock on i.MX51 */
af09c060 115 priv->phyclk = devm_clk_get(&pdev->dev, "phy");
c943740c
SH
116 if (IS_ERR(priv->phyclk))
117 priv->phyclk = NULL;
118 if (priv->phyclk)
119 clk_prepare_enable(priv->phyclk);
711669e5
APR
120
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
0247a7bc
FE
133 /* EHCI registers start at offset 0x100 */
134 ehci->caps = hcd->regs + 0x100;
135 ehci->regs = hcd->regs + 0x100 +
c430131a 136 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
0247a7bc
FE
137
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
7e8d5cd9
DM
144 /* Initialize the transceiver */
145 if (pdata->otg) {
146 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
b96d3b08 147 ret = usb_phy_init(pdata->otg);
4c9715de
WS
148 if (ret) {
149 dev_err(dev, "unable to init transceiver, probably missing\n");
150 ret = -ENODEV;
151 goto err_add;
152 }
6e13c650 153 ret = otg_set_vbus(pdata->otg->otg, 1);
4c9715de 154 if (ret) {
7e8d5cd9 155 dev_err(dev, "unable to enable vbus on transceiver\n");
4c9715de
WS
156 goto err_add;
157 }
7e8d5cd9
DM
158 }
159
dba63b2f 160 platform_set_drvdata(pdev, hcd);
7e8d5cd9 161
b5dd18d8 162 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
7e8d5cd9
DM
163 if (ret)
164 goto err_add;
165
a464dc4d
APR
166 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()) {
b96d3b08
HK
173 flags = usb_phy_io_read(pdata->otg,
174 ULPI_OTG_CTRL);
a464dc4d 175 flags |= ULPI_OTG_CTRL_CHRGVBUS;
b96d3b08
HK
176 ret = usb_phy_io_write(pdata->otg, flags,
177 ULPI_OTG_CTRL);
a464dc4d
APR
178 if (ret) {
179 dev_err(dev, "unable to set CHRVBUS\n");
180 goto err_add;
181 }
182 }
183 }
184
7e8d5cd9
DM
185 return 0;
186
187err_add:
188 if (pdata && pdata->exit)
189 pdata->exit(pdev);
190err_init:
af09c060 191 if (priv->phyclk)
c943740c 192 clk_disable_unprepare(priv->phyclk);
c943740c
SH
193
194 clk_disable_unprepare(priv->ahbclk);
7e8d5cd9 195err_clk_ahb:
198ad2ce 196 clk_disable_unprepare(priv->usbclk);
7e8d5cd9
DM
197err_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;
dba63b2f
AS
205 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);
7e8d5cd9
DM
210
211 if (pdata && pdata->exit)
212 pdata->exit(pdev);
213
214 if (pdata->otg)
b96d3b08 215 usb_phy_shutdown(pdata->otg);
7e8d5cd9 216
198ad2ce 217 clk_disable_unprepare(priv->usbclk);
c943740c 218 clk_disable_unprepare(priv->ahbclk);
c943740c 219
af09c060 220 if (priv->phyclk)
c943740c 221 clk_disable_unprepare(priv->phyclk);
7e8d5cd9 222
dba63b2f
AS
223 usb_put_hcd(hcd);
224 platform_set_drvdata(pdev, NULL);
7e8d5cd9
DM
225 return 0;
226}
227
228static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
229{
dba63b2f 230 struct usb_hcd *hcd = platform_get_drvdata(pdev);
7e8d5cd9
DM
231
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,
dba63b2f 240 .remove = ehci_mxc_drv_remove,
7e8d5cd9
DM
241 .shutdown = ehci_mxc_drv_shutdown,
242 .driver = {
243 .name = "mxc-ehci",
244 },
245};
dba63b2f
AS
246
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");