blob: a37224a4a49b88c06f9f2ddba55cec404531ab1f [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
20#include <linux/platform_device.h>
21#include <linux/clk.h>
22#include <linux/delay.h>
23#include <linux/usb/otg.h>
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010024#include <linux/usb/ulpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010026
Shawn Guo03a1d6b2011-07-21 22:24:10 +080027#include <mach/hardware.h>
Arnd Bergmann82906b12012-08-24 15:14:29 +020028#include <linux/platform_data/usb-ehci-mxc.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010029
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010030#include <asm/mach-types.h>
31
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010032#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010033
34struct ehci_mxc_priv {
Sascha Hauerc9437402012-04-25 16:39:06 +020035 struct clk *usbclk, *ahbclk, *phyclk;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010036 struct usb_hcd *hcd;
37};
38
39/* called during probe() after chip reset completes */
40static int ehci_mxc_setup(struct usb_hcd *hcd)
41{
42 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010043
Matthieu CASTET65fd4272010-09-06 18:26:56 +020044 hcd->has_tt = 1;
45
Alan Sternc73cee72012-10-31 13:21:06 -040046 return ehci_setup(hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010047}
48
49static const struct hc_driver ehci_mxc_hc_driver = {
50 .description = hcd_name,
51 .product_desc = "Freescale On-Chip EHCI Host Controller",
52 .hcd_priv_size = sizeof(struct ehci_hcd),
53
54 /*
55 * generic hardware linkage
56 */
57 .irq = ehci_irq,
58 .flags = HCD_USB2 | HCD_MEMORY,
59
60 /*
61 * basic lifecycle operations
62 */
63 .reset = ehci_mxc_setup,
64 .start = ehci_run,
65 .stop = ehci_stop,
66 .shutdown = ehci_shutdown,
67
68 /*
69 * managing i/o requests and associated device resources
70 */
71 .urb_enqueue = ehci_urb_enqueue,
72 .urb_dequeue = ehci_urb_dequeue,
73 .endpoint_disable = ehci_endpoint_disable,
Paul Mundtb48d7f52010-11-30 17:57:02 +090074 .endpoint_reset = ehci_endpoint_reset,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010075
76 /*
77 * scheduling support
78 */
79 .get_frame_number = ehci_get_frame,
80
81 /*
82 * root hub support
83 */
84 .hub_status_data = ehci_hub_status_data,
85 .hub_control = ehci_hub_control,
86 .bus_suspend = ehci_bus_suspend,
87 .bus_resume = ehci_bus_resume,
88 .relinquish_port = ehci_relinquish_port,
89 .port_handed_over = ehci_port_handed_over,
Paul Mundtb48d7f52010-11-30 17:57:02 +090090
91 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010092};
93
94static int ehci_mxc_drv_probe(struct platform_device *pdev)
95{
96 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
97 struct usb_hcd *hcd;
98 struct resource *res;
Uwe Kleine-König724c8522010-11-02 10:30:57 +010099 int irq, ret;
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100100 unsigned int flags;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100101 struct ehci_mxc_priv *priv;
102 struct device *dev = &pdev->dev;
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200103 struct ehci_hcd *ehci;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100104
105 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
106
107 if (!pdata) {
108 dev_err(dev, "No platform data given, bailing out.\n");
109 return -EINVAL;
110 }
111
112 irq = platform_get_irq(pdev, 0);
113
114 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
115 if (!hcd)
116 return -ENOMEM;
117
Julia Lawallaf09c062012-07-29 21:46:13 +0200118 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100119 if (!priv) {
120 ret = -ENOMEM;
121 goto err_alloc;
122 }
123
124 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
125 if (!res) {
126 dev_err(dev, "Found HC with no register addr. Check setup!\n");
127 ret = -ENODEV;
Julia Lawallaf09c062012-07-29 21:46:13 +0200128 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100129 }
130
131 hcd->rsrc_start = res->start;
132 hcd->rsrc_len = resource_size(res);
133
Julia Lawallaf09c062012-07-29 21:46:13 +0200134 hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100135 if (!hcd->regs) {
136 dev_err(dev, "error mapping memory\n");
137 ret = -EFAULT;
Julia Lawallaf09c062012-07-29 21:46:13 +0200138 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100139 }
140
141 /* enable clocks */
Julia Lawallaf09c062012-07-29 21:46:13 +0200142 priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100143 if (IS_ERR(priv->usbclk)) {
144 ret = PTR_ERR(priv->usbclk);
Julia Lawallaf09c062012-07-29 21:46:13 +0200145 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100146 }
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100147 clk_prepare_enable(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100148
Julia Lawallaf09c062012-07-29 21:46:13 +0200149 priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
Sascha Hauerc9437402012-04-25 16:39:06 +0200150 if (IS_ERR(priv->ahbclk)) {
151 ret = PTR_ERR(priv->ahbclk);
152 goto err_clk_ahb;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100153 }
Sascha Hauerc9437402012-04-25 16:39:06 +0200154 clk_prepare_enable(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100155
Eric Bénard3bb80292011-01-13 14:53:17 +0100156 /* "dr" device has its own clock on i.MX51 */
Julia Lawallaf09c062012-07-29 21:46:13 +0200157 priv->phyclk = devm_clk_get(&pdev->dev, "phy");
Sascha Hauerc9437402012-04-25 16:39:06 +0200158 if (IS_ERR(priv->phyclk))
159 priv->phyclk = NULL;
160 if (priv->phyclk)
161 clk_prepare_enable(priv->phyclk);
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100162
163
164 /* call platform specific init function */
165 if (pdata->init) {
166 ret = pdata->init(pdev);
167 if (ret) {
168 dev_err(dev, "platform init failed\n");
169 goto err_init;
170 }
171 /* platforms need some time to settle changed IO settings */
172 mdelay(10);
173 }
174
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200175 ehci = hcd_to_ehci(hcd);
176
177 /* EHCI registers start at offset 0x100 */
178 ehci->caps = hcd->regs + 0x100;
179 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200180 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200181
182 /* set up the PORTSCx register */
183 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
184
185 /* is this really needed? */
186 msleep(10);
187
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100188 /* Initialize the transceiver */
189 if (pdata->otg) {
190 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200191 ret = usb_phy_init(pdata->otg);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200192 if (ret) {
193 dev_err(dev, "unable to init transceiver, probably missing\n");
194 ret = -ENODEV;
195 goto err_add;
196 }
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200197 ret = otg_set_vbus(pdata->otg->otg, 1);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200198 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100199 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200200 goto err_add;
201 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100202 }
203
204 priv->hcd = hcd;
205 platform_set_drvdata(pdev, priv);
206
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800207 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100208 if (ret)
209 goto err_add;
210
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100211 if (pdata->otg) {
212 /*
213 * efikamx and efikasb have some hardware bug which is
214 * preventing usb to work unless CHRGVBUS is set.
215 * It's in violation of USB specs
216 */
217 if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200218 flags = usb_phy_io_read(pdata->otg,
219 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100220 flags |= ULPI_OTG_CTRL_CHRGVBUS;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200221 ret = usb_phy_io_write(pdata->otg, flags,
222 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100223 if (ret) {
224 dev_err(dev, "unable to set CHRVBUS\n");
225 goto err_add;
226 }
227 }
228 }
229
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100230 return 0;
231
232err_add:
233 if (pdata && pdata->exit)
234 pdata->exit(pdev);
235err_init:
Julia Lawallaf09c062012-07-29 21:46:13 +0200236 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200237 clk_disable_unprepare(priv->phyclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200238
239 clk_disable_unprepare(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100240err_clk_ahb:
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100241 clk_disable_unprepare(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100242err_alloc:
243 usb_put_hcd(hcd);
244 return ret;
245}
246
247static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
248{
249 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
250 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
251 struct usb_hcd *hcd = priv->hcd;
252
253 if (pdata && pdata->exit)
254 pdata->exit(pdev);
255
256 if (pdata->otg)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200257 usb_phy_shutdown(pdata->otg);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100258
259 usb_remove_hcd(hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100260 usb_put_hcd(hcd);
261 platform_set_drvdata(pdev, NULL);
262
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100263 clk_disable_unprepare(priv->usbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200264 clk_disable_unprepare(priv->ahbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200265
Julia Lawallaf09c062012-07-29 21:46:13 +0200266 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200267 clk_disable_unprepare(priv->phyclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100268
269 return 0;
270}
271
272static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
273{
274 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
275 struct usb_hcd *hcd = priv->hcd;
276
277 if (hcd->driver->shutdown)
278 hcd->driver->shutdown(hcd);
279}
280
281MODULE_ALIAS("platform:mxc-ehci");
282
283static struct platform_driver ehci_mxc_driver = {
284 .probe = ehci_mxc_drv_probe,
285 .remove = __exit_p(ehci_mxc_drv_remove),
286 .shutdown = ehci_mxc_drv_shutdown,
287 .driver = {
288 .name = "mxc-ehci",
289 },
290};