Merge branch 'usb-next' into musb-merge
[firefly-linux-kernel-4.4.55.git] / drivers / usb / host / ehci-mxc.c
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
20 #include <linux/platform_device.h>
21 #include <linux/clk.h>
22 #include <linux/delay.h>
23 #include <linux/usb/otg.h>
24 #include <linux/slab.h>
25
26 #include <mach/mxc_ehci.h>
27
28 #define ULPI_VIEWPORT_OFFSET    0x170
29
30 struct ehci_mxc_priv {
31         struct clk *usbclk, *ahbclk;
32         struct usb_hcd *hcd;
33 };
34
35 /* called during probe() after chip reset completes */
36 static int ehci_mxc_setup(struct usb_hcd *hcd)
37 {
38         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
39         struct device *dev = hcd->self.controller;
40         struct mxc_usbh_platform_data *pdata = dev_get_platdata(dev);
41         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
53         hcd->has_tt = 1;
54
55         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
64         ehci->sbrn = 0x20;
65
66         ehci_reset(ehci);
67
68         /* 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
74         ehci_port_power(ehci, 0);
75         return 0;
76 }
77
78 static 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         .endpoint_reset = ehci_endpoint_reset,
104
105         /*
106          * scheduling support
107          */
108         .get_frame_number = ehci_get_frame,
109
110         /*
111          * root hub support
112          */
113         .hub_status_data = ehci_hub_status_data,
114         .hub_control = ehci_hub_control,
115         .bus_suspend = ehci_bus_suspend,
116         .bus_resume = ehci_bus_resume,
117         .relinquish_port = ehci_relinquish_port,
118         .port_handed_over = ehci_port_handed_over,
119
120         .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
121 };
122
123 static int ehci_mxc_drv_probe(struct platform_device *pdev)
124 {
125         struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
126         struct usb_hcd *hcd;
127         struct resource *res;
128         int irq, ret;
129         struct ehci_mxc_priv *priv;
130         struct device *dev = &pdev->dev;
131
132         dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
133
134         if (!pdata) {
135                 dev_err(dev, "No platform data given, bailing out.\n");
136                 return -EINVAL;
137         }
138
139         irq = platform_get_irq(pdev, 0);
140
141         hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
142         if (!hcd)
143                 return -ENOMEM;
144
145         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
146         if (!priv) {
147                 ret = -ENOMEM;
148                 goto err_alloc;
149         }
150
151         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
152         if (!res) {
153                 dev_err(dev, "Found HC with no register addr. Check setup!\n");
154                 ret = -ENODEV;
155                 goto err_get_resource;
156         }
157
158         hcd->rsrc_start = res->start;
159         hcd->rsrc_len = resource_size(res);
160
161         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
162                 dev_dbg(dev, "controller already in use\n");
163                 ret = -EBUSY;
164                 goto err_request_mem;
165         }
166
167         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
168         if (!hcd->regs) {
169                 dev_err(dev, "error mapping memory\n");
170                 ret = -EFAULT;
171                 goto err_ioremap;
172         }
173
174         /* call platform specific init function */
175         if (pdata->init) {
176                 ret = pdata->init(pdev);
177                 if (ret) {
178                         dev_err(dev, "platform init failed\n");
179                         goto err_init;
180                 }
181                 /* platforms need some time to settle changed IO settings */
182                 mdelay(10);
183         }
184
185         /* enable clocks */
186         priv->usbclk = clk_get(dev, "usb");
187         if (IS_ERR(priv->usbclk)) {
188                 ret = PTR_ERR(priv->usbclk);
189                 goto err_clk;
190         }
191         clk_enable(priv->usbclk);
192
193         if (!cpu_is_mx35() && !cpu_is_mx25()) {
194                 priv->ahbclk = clk_get(dev, "usb_ahb");
195                 if (IS_ERR(priv->ahbclk)) {
196                         ret = PTR_ERR(priv->ahbclk);
197                         goto err_clk_ahb;
198                 }
199                 clk_enable(priv->ahbclk);
200         }
201
202         /* setup specific usb hw */
203         ret = mxc_initialize_usb_hw(pdev->id, pdata->flags);
204         if (ret < 0)
205                 goto err_init;
206
207         /* Initialize the transceiver */
208         if (pdata->otg) {
209                 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
210                 ret = otg_init(pdata->otg);
211                 if (ret) {
212                         dev_err(dev, "unable to init transceiver, probably missing\n");
213                         ret = -ENODEV;
214                         goto err_add;
215                 }
216                 ret = otg_set_vbus(pdata->otg, 1);
217                 if (ret) {
218                         dev_err(dev, "unable to enable vbus on transceiver\n");
219                         goto err_add;
220                 }
221         }
222
223         priv->hcd = hcd;
224         platform_set_drvdata(pdev, priv);
225
226         ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
227         if (ret)
228                 goto err_add;
229
230         return 0;
231
232 err_add:
233         if (pdata && pdata->exit)
234                 pdata->exit(pdev);
235 err_init:
236         if (priv->ahbclk) {
237                 clk_disable(priv->ahbclk);
238                 clk_put(priv->ahbclk);
239         }
240 err_clk_ahb:
241         clk_disable(priv->usbclk);
242         clk_put(priv->usbclk);
243 err_clk:
244         iounmap(hcd->regs);
245 err_ioremap:
246         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
247 err_request_mem:
248 err_get_resource:
249         kfree(priv);
250 err_alloc:
251         usb_put_hcd(hcd);
252         return ret;
253 }
254
255 static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
256 {
257         struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
258         struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
259         struct usb_hcd *hcd = priv->hcd;
260
261         if (pdata && pdata->exit)
262                 pdata->exit(pdev);
263
264         if (pdata->otg)
265                 otg_shutdown(pdata->otg);
266
267         usb_remove_hcd(hcd);
268         iounmap(hcd->regs);
269         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
270         usb_put_hcd(hcd);
271         platform_set_drvdata(pdev, NULL);
272
273         clk_disable(priv->usbclk);
274         clk_put(priv->usbclk);
275         if (priv->ahbclk) {
276                 clk_disable(priv->ahbclk);
277                 clk_put(priv->ahbclk);
278         }
279
280         kfree(priv);
281
282         return 0;
283 }
284
285 static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
286 {
287         struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
288         struct usb_hcd *hcd = priv->hcd;
289
290         if (hcd->driver->shutdown)
291                 hcd->driver->shutdown(hcd);
292 }
293
294 MODULE_ALIAS("platform:mxc-ehci");
295
296 static struct platform_driver ehci_mxc_driver = {
297         .probe = ehci_mxc_drv_probe,
298         .remove = __exit_p(ehci_mxc_drv_remove),
299         .shutdown = ehci_mxc_drv_shutdown,
300         .driver = {
301                    .name = "mxc-ehci",
302         },
303 };