blob: ac9c4d7c44af227ff974b6f5af7636013583bcbc [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010025
26#include <mach/mxc_ehci.h>
27
28#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010029
30struct ehci_mxc_priv {
31 struct clk *usbclk, *ahbclk;
32 struct usb_hcd *hcd;
33};
34
35/* called during probe() after chip reset completes */
36static int ehci_mxc_setup(struct usb_hcd *hcd)
37{
38 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
39 int retval;
40
41 /* EHCI registers start at offset 0x100 */
42 ehci->caps = hcd->regs + 0x100;
43 ehci->regs = hcd->regs + 0x100 +
44 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
45 dbg_hcs_params(ehci, "reset");
46 dbg_hcc_params(ehci, "reset");
47
48 /* cache this readonly data; minimize chip reads */
49 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
50
Matthieu CASTET65fd4272010-09-06 18:26:56 +020051 hcd->has_tt = 1;
52
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010053 retval = ehci_halt(ehci);
54 if (retval)
55 return retval;
56
57 /* data structure init */
58 retval = ehci_init(hcd);
59 if (retval)
60 return retval;
61
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010062 ehci->sbrn = 0x20;
63
64 ehci_reset(ehci);
65
66 ehci_port_power(ehci, 0);
67 return 0;
68}
69
70static const struct hc_driver ehci_mxc_hc_driver = {
71 .description = hcd_name,
72 .product_desc = "Freescale On-Chip EHCI Host Controller",
73 .hcd_priv_size = sizeof(struct ehci_hcd),
74
75 /*
76 * generic hardware linkage
77 */
78 .irq = ehci_irq,
79 .flags = HCD_USB2 | HCD_MEMORY,
80
81 /*
82 * basic lifecycle operations
83 */
84 .reset = ehci_mxc_setup,
85 .start = ehci_run,
86 .stop = ehci_stop,
87 .shutdown = ehci_shutdown,
88
89 /*
90 * managing i/o requests and associated device resources
91 */
92 .urb_enqueue = ehci_urb_enqueue,
93 .urb_dequeue = ehci_urb_dequeue,
94 .endpoint_disable = ehci_endpoint_disable,
95
96 /*
97 * scheduling support
98 */
99 .get_frame_number = ehci_get_frame,
100
101 /*
102 * root hub support
103 */
104 .hub_status_data = ehci_hub_status_data,
105 .hub_control = ehci_hub_control,
106 .bus_suspend = ehci_bus_suspend,
107 .bus_resume = ehci_bus_resume,
108 .relinquish_port = ehci_relinquish_port,
109 .port_handed_over = ehci_port_handed_over,
110};
111
112static int ehci_mxc_drv_probe(struct platform_device *pdev)
113{
114 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
115 struct usb_hcd *hcd;
116 struct resource *res;
117 int irq, ret, temp;
118 struct ehci_mxc_priv *priv;
119 struct device *dev = &pdev->dev;
120
121 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
122
123 if (!pdata) {
124 dev_err(dev, "No platform data given, bailing out.\n");
125 return -EINVAL;
126 }
127
128 irq = platform_get_irq(pdev, 0);
129
130 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
131 if (!hcd)
132 return -ENOMEM;
133
134 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
135 if (!priv) {
136 ret = -ENOMEM;
137 goto err_alloc;
138 }
139
140 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
141 if (!res) {
142 dev_err(dev, "Found HC with no register addr. Check setup!\n");
143 ret = -ENODEV;
144 goto err_get_resource;
145 }
146
147 hcd->rsrc_start = res->start;
148 hcd->rsrc_len = resource_size(res);
149
150 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
151 dev_dbg(dev, "controller already in use\n");
152 ret = -EBUSY;
153 goto err_request_mem;
154 }
155
156 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
157 if (!hcd->regs) {
158 dev_err(dev, "error mapping memory\n");
159 ret = -EFAULT;
160 goto err_ioremap;
161 }
162
Valentin Longchamp2f0e40a2010-01-20 19:43:23 +0100163 /* call platform specific init function */
164 if (pdata->init) {
165 ret = pdata->init(pdev);
166 if (ret) {
167 dev_err(dev, "platform init failed\n");
168 goto err_init;
169 }
170 /* platforms need some time to settle changed IO settings */
171 mdelay(10);
172 }
173
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100174 /* enable clocks */
175 priv->usbclk = clk_get(dev, "usb");
176 if (IS_ERR(priv->usbclk)) {
177 ret = PTR_ERR(priv->usbclk);
178 goto err_clk;
179 }
180 clk_enable(priv->usbclk);
181
Eric BĂ©nard4ea0af02010-06-08 11:02:58 +0200182 if (!cpu_is_mx35() && !cpu_is_mx25()) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100183 priv->ahbclk = clk_get(dev, "usb_ahb");
184 if (IS_ERR(priv->ahbclk)) {
185 ret = PTR_ERR(priv->ahbclk);
186 goto err_clk_ahb;
187 }
188 clk_enable(priv->ahbclk);
189 }
190
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100191 /* set up the PORTSCx register */
Matthieu CASTET65fd4272010-09-06 18:26:56 +0200192 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100193 mdelay(10);
194
Dinh Nguyen5a25ad82010-04-30 15:48:26 -0500195 /* setup specific usb hw */
196 ret = mxc_initialize_usb_hw(pdev->id, pdata->flags);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100197 if (ret < 0)
198 goto err_init;
199
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100200 /* Initialize the transceiver */
201 if (pdata->otg) {
202 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200203 ret = otg_init(pdata->otg);
204 if (ret) {
205 dev_err(dev, "unable to init transceiver, probably missing\n");
206 ret = -ENODEV;
207 goto err_add;
208 }
209 ret = otg_set_vbus(pdata->otg, 1);
210 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100211 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200212 goto err_add;
213 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100214 }
215
216 priv->hcd = hcd;
217 platform_set_drvdata(pdev, priv);
218
219 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
220 if (ret)
221 goto err_add;
222
223 return 0;
224
225err_add:
226 if (pdata && pdata->exit)
227 pdata->exit(pdev);
228err_init:
229 if (priv->ahbclk) {
230 clk_disable(priv->ahbclk);
231 clk_put(priv->ahbclk);
232 }
233err_clk_ahb:
234 clk_disable(priv->usbclk);
235 clk_put(priv->usbclk);
236err_clk:
237 iounmap(hcd->regs);
238err_ioremap:
239 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
240err_request_mem:
241err_get_resource:
242 kfree(priv);
243err_alloc:
244 usb_put_hcd(hcd);
245 return ret;
246}
247
248static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
249{
250 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
251 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
252 struct usb_hcd *hcd = priv->hcd;
253
254 if (pdata && pdata->exit)
255 pdata->exit(pdev);
256
257 if (pdata->otg)
258 otg_shutdown(pdata->otg);
259
260 usb_remove_hcd(hcd);
261 iounmap(hcd->regs);
262 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
263 usb_put_hcd(hcd);
264 platform_set_drvdata(pdev, NULL);
265
266 clk_disable(priv->usbclk);
267 clk_put(priv->usbclk);
268 if (priv->ahbclk) {
269 clk_disable(priv->ahbclk);
270 clk_put(priv->ahbclk);
271 }
272
273 kfree(priv);
274
275 return 0;
276}
277
278static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
279{
280 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
281 struct usb_hcd *hcd = priv->hcd;
282
283 if (hcd->driver->shutdown)
284 hcd->driver->shutdown(hcd);
285}
286
287MODULE_ALIAS("platform:mxc-ehci");
288
289static struct platform_driver ehci_mxc_driver = {
290 .probe = ehci_mxc_drv_probe,
291 .remove = __exit_p(ehci_mxc_drv_remove),
292 .shutdown = ehci_mxc_drv_shutdown,
293 .driver = {
294 .name = "mxc-ehci",
295 },
296};