blob: a22d2df769a9b41a9a51bfd9e46f252572586d3b [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 {
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +010031 struct clk *usbclk, *ahbclk, *phy1clk;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010032 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);
Uwe Kleine-König724c8522010-11-02 10:30:57 +010039 struct device *dev = hcd->self.controller;
40 struct mxc_usbh_platform_data *pdata = dev_get_platdata(dev);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010041 int retval;
42
43 /* EHCI registers start at offset 0x100 */
44 ehci->caps = hcd->regs + 0x100;
45 ehci->regs = hcd->regs + 0x100 +
46 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
47 dbg_hcs_params(ehci, "reset");
48 dbg_hcc_params(ehci, "reset");
49
50 /* cache this readonly data; minimize chip reads */
51 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
52
Matthieu CASTET65fd4272010-09-06 18:26:56 +020053 hcd->has_tt = 1;
54
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010055 retval = ehci_halt(ehci);
56 if (retval)
57 return retval;
58
59 /* data structure init */
60 retval = ehci_init(hcd);
61 if (retval)
62 return retval;
63
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010064 ehci->sbrn = 0x20;
65
66 ehci_reset(ehci);
67
Uwe Kleine-König724c8522010-11-02 10:30:57 +010068 /* set up the PORTSCx register */
69 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
70
71 /* is this really needed? */
72 msleep(10);
73
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010074 ehci_port_power(ehci, 0);
75 return 0;
76}
77
78static const struct hc_driver ehci_mxc_hc_driver = {
79 .description = hcd_name,
80 .product_desc = "Freescale On-Chip EHCI Host Controller",
81 .hcd_priv_size = sizeof(struct ehci_hcd),
82
83 /*
84 * generic hardware linkage
85 */
86 .irq = ehci_irq,
87 .flags = HCD_USB2 | HCD_MEMORY,
88
89 /*
90 * basic lifecycle operations
91 */
92 .reset = ehci_mxc_setup,
93 .start = ehci_run,
94 .stop = ehci_stop,
95 .shutdown = ehci_shutdown,
96
97 /*
98 * managing i/o requests and associated device resources
99 */
100 .urb_enqueue = ehci_urb_enqueue,
101 .urb_dequeue = ehci_urb_dequeue,
102 .endpoint_disable = ehci_endpoint_disable,
103
104 /*
105 * scheduling support
106 */
107 .get_frame_number = ehci_get_frame,
108
109 /*
110 * root hub support
111 */
112 .hub_status_data = ehci_hub_status_data,
113 .hub_control = ehci_hub_control,
114 .bus_suspend = ehci_bus_suspend,
115 .bus_resume = ehci_bus_resume,
116 .relinquish_port = ehci_relinquish_port,
117 .port_handed_over = ehci_port_handed_over,
118};
119
120static int ehci_mxc_drv_probe(struct platform_device *pdev)
121{
122 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
123 struct usb_hcd *hcd;
124 struct resource *res;
Uwe Kleine-König724c8522010-11-02 10:30:57 +0100125 int irq, ret;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100126 struct ehci_mxc_priv *priv;
127 struct device *dev = &pdev->dev;
128
129 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
130
131 if (!pdata) {
132 dev_err(dev, "No platform data given, bailing out.\n");
133 return -EINVAL;
134 }
135
136 irq = platform_get_irq(pdev, 0);
137
138 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
139 if (!hcd)
140 return -ENOMEM;
141
142 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
143 if (!priv) {
144 ret = -ENOMEM;
145 goto err_alloc;
146 }
147
148 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 if (!res) {
150 dev_err(dev, "Found HC with no register addr. Check setup!\n");
151 ret = -ENODEV;
152 goto err_get_resource;
153 }
154
155 hcd->rsrc_start = res->start;
156 hcd->rsrc_len = resource_size(res);
157
158 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
159 dev_dbg(dev, "controller already in use\n");
160 ret = -EBUSY;
161 goto err_request_mem;
162 }
163
164 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
165 if (!hcd->regs) {
166 dev_err(dev, "error mapping memory\n");
167 ret = -EFAULT;
168 goto err_ioremap;
169 }
170
171 /* enable clocks */
172 priv->usbclk = clk_get(dev, "usb");
173 if (IS_ERR(priv->usbclk)) {
174 ret = PTR_ERR(priv->usbclk);
175 goto err_clk;
176 }
177 clk_enable(priv->usbclk);
178
Eric Bénard4ea0af02010-06-08 11:02:58 +0200179 if (!cpu_is_mx35() && !cpu_is_mx25()) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100180 priv->ahbclk = clk_get(dev, "usb_ahb");
181 if (IS_ERR(priv->ahbclk)) {
182 ret = PTR_ERR(priv->ahbclk);
183 goto err_clk_ahb;
184 }
185 clk_enable(priv->ahbclk);
186 }
187
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100188 /* "dr" device has its own clock */
189 if (pdev->id == 0) {
190 priv->phy1clk = clk_get(dev, "usb_phy1");
191 if (IS_ERR(priv->phy1clk)) {
192 ret = PTR_ERR(priv->phy1clk);
193 goto err_clk_phy;
194 }
195 clk_enable(priv->phy1clk);
196 }
197
198
199 /* call platform specific init function */
200 if (pdata->init) {
201 ret = pdata->init(pdev);
202 if (ret) {
203 dev_err(dev, "platform init failed\n");
204 goto err_init;
205 }
206 /* platforms need some time to settle changed IO settings */
207 mdelay(10);
208 }
209
Dinh Nguyen5a25ad82010-04-30 15:48:26 -0500210 /* setup specific usb hw */
211 ret = mxc_initialize_usb_hw(pdev->id, pdata->flags);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100212 if (ret < 0)
213 goto err_init;
214
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100215 /* Initialize the transceiver */
216 if (pdata->otg) {
217 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200218 ret = otg_init(pdata->otg);
219 if (ret) {
220 dev_err(dev, "unable to init transceiver, probably missing\n");
221 ret = -ENODEV;
222 goto err_add;
223 }
224 ret = otg_set_vbus(pdata->otg, 1);
225 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100226 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200227 goto err_add;
228 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100229 }
230
231 priv->hcd = hcd;
232 platform_set_drvdata(pdev, priv);
233
234 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
235 if (ret)
236 goto err_add;
237
238 return 0;
239
240err_add:
241 if (pdata && pdata->exit)
242 pdata->exit(pdev);
243err_init:
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100244 if (priv->phy1clk) {
245 clk_disable(priv->phy1clk);
246 clk_put(priv->phy1clk);
247 }
248err_clk_phy:
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100249 if (priv->ahbclk) {
250 clk_disable(priv->ahbclk);
251 clk_put(priv->ahbclk);
252 }
253err_clk_ahb:
254 clk_disable(priv->usbclk);
255 clk_put(priv->usbclk);
256err_clk:
257 iounmap(hcd->regs);
258err_ioremap:
259 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
260err_request_mem:
261err_get_resource:
262 kfree(priv);
263err_alloc:
264 usb_put_hcd(hcd);
265 return ret;
266}
267
268static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
269{
270 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
271 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
272 struct usb_hcd *hcd = priv->hcd;
273
274 if (pdata && pdata->exit)
275 pdata->exit(pdev);
276
277 if (pdata->otg)
278 otg_shutdown(pdata->otg);
279
280 usb_remove_hcd(hcd);
281 iounmap(hcd->regs);
282 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
283 usb_put_hcd(hcd);
284 platform_set_drvdata(pdev, NULL);
285
286 clk_disable(priv->usbclk);
287 clk_put(priv->usbclk);
288 if (priv->ahbclk) {
289 clk_disable(priv->ahbclk);
290 clk_put(priv->ahbclk);
291 }
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100292 if (priv->phy1clk) {
293 clk_disable(priv->phy1clk);
294 clk_put(priv->phy1clk);
295 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100296
297 kfree(priv);
298
299 return 0;
300}
301
302static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
303{
304 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
305 struct usb_hcd *hcd = priv->hcd;
306
307 if (hcd->driver->shutdown)
308 hcd->driver->shutdown(hcd);
309}
310
311MODULE_ALIAS("platform:mxc-ehci");
312
313static struct platform_driver ehci_mxc_driver = {
314 .probe = ehci_mxc_drv_probe,
315 .remove = __exit_p(ehci_mxc_drv_remove),
316 .shutdown = ehci_mxc_drv_shutdown,
317 .driver = {
318 .name = "mxc-ehci",
319 },
320};