blob: 4f506af4c303c6b25b34fc54620741003b4d8638 [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
Arnd Bergmann82906b12012-08-24 15:14:29 +020027#include <linux/platform_data/usb-ehci-mxc.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010028
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010029#include <asm/mach-types.h>
30
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010031#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010032
33struct ehci_mxc_priv {
Sascha Hauerc9437402012-04-25 16:39:06 +020034 struct clk *usbclk, *ahbclk, *phyclk;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010035 struct usb_hcd *hcd;
36};
37
38/* called during probe() after chip reset completes */
39static int ehci_mxc_setup(struct usb_hcd *hcd)
40{
Matthieu CASTET65fd4272010-09-06 18:26:56 +020041 hcd->has_tt = 1;
42
Alan Sternc73cee72012-10-31 13:21:06 -040043 return ehci_setup(hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010044}
45
46static const struct hc_driver ehci_mxc_hc_driver = {
47 .description = hcd_name,
48 .product_desc = "Freescale On-Chip EHCI Host Controller",
49 .hcd_priv_size = sizeof(struct ehci_hcd),
50
51 /*
52 * generic hardware linkage
53 */
54 .irq = ehci_irq,
55 .flags = HCD_USB2 | HCD_MEMORY,
56
57 /*
58 * basic lifecycle operations
59 */
60 .reset = ehci_mxc_setup,
61 .start = ehci_run,
62 .stop = ehci_stop,
63 .shutdown = ehci_shutdown,
64
65 /*
66 * managing i/o requests and associated device resources
67 */
68 .urb_enqueue = ehci_urb_enqueue,
69 .urb_dequeue = ehci_urb_dequeue,
70 .endpoint_disable = ehci_endpoint_disable,
Paul Mundtb48d7f52010-11-30 17:57:02 +090071 .endpoint_reset = ehci_endpoint_reset,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010072
73 /*
74 * scheduling support
75 */
76 .get_frame_number = ehci_get_frame,
77
78 /*
79 * root hub support
80 */
81 .hub_status_data = ehci_hub_status_data,
82 .hub_control = ehci_hub_control,
83 .bus_suspend = ehci_bus_suspend,
84 .bus_resume = ehci_bus_resume,
85 .relinquish_port = ehci_relinquish_port,
86 .port_handed_over = ehci_port_handed_over,
Paul Mundtb48d7f52010-11-30 17:57:02 +090087
88 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010089};
90
91static int ehci_mxc_drv_probe(struct platform_device *pdev)
92{
93 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
94 struct usb_hcd *hcd;
95 struct resource *res;
Uwe Kleine-König724c8522010-11-02 10:30:57 +010096 int irq, ret;
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010097 unsigned int flags;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010098 struct ehci_mxc_priv *priv;
99 struct device *dev = &pdev->dev;
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200100 struct ehci_hcd *ehci;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100101
102 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
103
104 if (!pdata) {
105 dev_err(dev, "No platform data given, bailing out.\n");
106 return -EINVAL;
107 }
108
109 irq = platform_get_irq(pdev, 0);
110
111 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
112 if (!hcd)
113 return -ENOMEM;
114
Julia Lawallaf09c062012-07-29 21:46:13 +0200115 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100116 if (!priv) {
117 ret = -ENOMEM;
118 goto err_alloc;
119 }
120
121 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
122 if (!res) {
123 dev_err(dev, "Found HC with no register addr. Check setup!\n");
124 ret = -ENODEV;
Julia Lawallaf09c062012-07-29 21:46:13 +0200125 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100126 }
127
128 hcd->rsrc_start = res->start;
129 hcd->rsrc_len = resource_size(res);
130
Thierry Reding148e1132013-01-21 11:09:22 +0100131 hcd->regs = devm_ioremap_resource(&pdev->dev, res);
132 if (IS_ERR(hcd->regs)) {
133 ret = PTR_ERR(hcd->regs);
Julia Lawallaf09c062012-07-29 21:46:13 +0200134 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100135 }
136
137 /* enable clocks */
Julia Lawallaf09c062012-07-29 21:46:13 +0200138 priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100139 if (IS_ERR(priv->usbclk)) {
140 ret = PTR_ERR(priv->usbclk);
Julia Lawallaf09c062012-07-29 21:46:13 +0200141 goto err_alloc;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100142 }
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100143 clk_prepare_enable(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100144
Julia Lawallaf09c062012-07-29 21:46:13 +0200145 priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
Sascha Hauerc9437402012-04-25 16:39:06 +0200146 if (IS_ERR(priv->ahbclk)) {
147 ret = PTR_ERR(priv->ahbclk);
148 goto err_clk_ahb;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100149 }
Sascha Hauerc9437402012-04-25 16:39:06 +0200150 clk_prepare_enable(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100151
Eric Bénard3bb80292011-01-13 14:53:17 +0100152 /* "dr" device has its own clock on i.MX51 */
Julia Lawallaf09c062012-07-29 21:46:13 +0200153 priv->phyclk = devm_clk_get(&pdev->dev, "phy");
Sascha Hauerc9437402012-04-25 16:39:06 +0200154 if (IS_ERR(priv->phyclk))
155 priv->phyclk = NULL;
156 if (priv->phyclk)
157 clk_prepare_enable(priv->phyclk);
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100158
159
160 /* call platform specific init function */
161 if (pdata->init) {
162 ret = pdata->init(pdev);
163 if (ret) {
164 dev_err(dev, "platform init failed\n");
165 goto err_init;
166 }
167 /* platforms need some time to settle changed IO settings */
168 mdelay(10);
169 }
170
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200171 ehci = hcd_to_ehci(hcd);
172
173 /* EHCI registers start at offset 0x100 */
174 ehci->caps = hcd->regs + 0x100;
175 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200176 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200177
178 /* set up the PORTSCx register */
179 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
180
181 /* is this really needed? */
182 msleep(10);
183
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100184 /* Initialize the transceiver */
185 if (pdata->otg) {
186 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200187 ret = usb_phy_init(pdata->otg);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200188 if (ret) {
189 dev_err(dev, "unable to init transceiver, probably missing\n");
190 ret = -ENODEV;
191 goto err_add;
192 }
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200193 ret = otg_set_vbus(pdata->otg->otg, 1);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200194 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100195 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200196 goto err_add;
197 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100198 }
199
200 priv->hcd = hcd;
201 platform_set_drvdata(pdev, priv);
202
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800203 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100204 if (ret)
205 goto err_add;
206
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100207 if (pdata->otg) {
208 /*
209 * efikamx and efikasb have some hardware bug which is
210 * preventing usb to work unless CHRGVBUS is set.
211 * It's in violation of USB specs
212 */
213 if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200214 flags = usb_phy_io_read(pdata->otg,
215 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100216 flags |= ULPI_OTG_CTRL_CHRGVBUS;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200217 ret = usb_phy_io_write(pdata->otg, flags,
218 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100219 if (ret) {
220 dev_err(dev, "unable to set CHRVBUS\n");
221 goto err_add;
222 }
223 }
224 }
225
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100226 return 0;
227
228err_add:
229 if (pdata && pdata->exit)
230 pdata->exit(pdev);
231err_init:
Julia Lawallaf09c062012-07-29 21:46:13 +0200232 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200233 clk_disable_unprepare(priv->phyclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200234
235 clk_disable_unprepare(priv->ahbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100236err_clk_ahb:
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100237 clk_disable_unprepare(priv->usbclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100238err_alloc:
239 usb_put_hcd(hcd);
240 return ret;
241}
242
243static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
244{
245 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
246 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
247 struct usb_hcd *hcd = priv->hcd;
248
249 if (pdata && pdata->exit)
250 pdata->exit(pdev);
251
252 if (pdata->otg)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200253 usb_phy_shutdown(pdata->otg);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100254
255 usb_remove_hcd(hcd);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100256 usb_put_hcd(hcd);
257 platform_set_drvdata(pdev, NULL);
258
Sascha Hauer198ad2c2012-03-07 20:58:21 +0100259 clk_disable_unprepare(priv->usbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200260 clk_disable_unprepare(priv->ahbclk);
Sascha Hauerc9437402012-04-25 16:39:06 +0200261
Julia Lawallaf09c062012-07-29 21:46:13 +0200262 if (priv->phyclk)
Sascha Hauerc9437402012-04-25 16:39:06 +0200263 clk_disable_unprepare(priv->phyclk);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100264
265 return 0;
266}
267
268static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
269{
270 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
271 struct usb_hcd *hcd = priv->hcd;
272
273 if (hcd->driver->shutdown)
274 hcd->driver->shutdown(hcd);
275}
276
277MODULE_ALIAS("platform:mxc-ehci");
278
279static struct platform_driver ehci_mxc_driver = {
280 .probe = ehci_mxc_drv_probe,
281 .remove = __exit_p(ehci_mxc_drv_remove),
282 .shutdown = ehci_mxc_drv_shutdown,
283 .driver = {
284 .name = "mxc-ehci",
285 },
286};