GNU Linux-libre 4.9.292-gnu1
[releases.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/kernel.h>
21 #include <linux/module.h>
22 #include <linux/io.h>
23 #include <linux/platform_device.h>
24 #include <linux/clk.h>
25 #include <linux/delay.h>
26 #include <linux/usb/otg.h>
27 #include <linux/usb/ulpi.h>
28 #include <linux/slab.h>
29 #include <linux/usb.h>
30 #include <linux/usb/hcd.h>
31 #include <linux/platform_data/usb-ehci-mxc.h>
32 #include "ehci.h"
33
34 #define DRIVER_DESC "Freescale On-Chip EHCI Host driver"
35
36 static const char hcd_name[] = "ehci-mxc";
37
38 #define ULPI_VIEWPORT_OFFSET    0x170
39
40 struct ehci_mxc_priv {
41         struct clk *usbclk, *ahbclk, *phyclk;
42 };
43
44 static struct hc_driver __read_mostly ehci_mxc_hc_driver;
45
46 static const struct ehci_driver_overrides ehci_mxc_overrides __initconst = {
47         .extra_priv_size =      sizeof(struct ehci_mxc_priv),
48 };
49
50 static int ehci_mxc_drv_probe(struct platform_device *pdev)
51 {
52         struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
53         struct usb_hcd *hcd;
54         struct resource *res;
55         int irq, ret;
56         struct ehci_mxc_priv *priv;
57         struct device *dev = &pdev->dev;
58         struct ehci_hcd *ehci;
59
60         if (!pdata) {
61                 dev_err(dev, "No platform data given, bailing out.\n");
62                 return -EINVAL;
63         }
64
65         irq = platform_get_irq(pdev, 0);
66         if (irq < 0)
67                 return irq;
68
69         hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
70         if (!hcd)
71                 return -ENOMEM;
72
73         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
74         hcd->regs = devm_ioremap_resource(&pdev->dev, res);
75         if (IS_ERR(hcd->regs)) {
76                 ret = PTR_ERR(hcd->regs);
77                 goto err_alloc;
78         }
79         hcd->rsrc_start = res->start;
80         hcd->rsrc_len = resource_size(res);
81
82         hcd->has_tt = 1;
83         ehci = hcd_to_ehci(hcd);
84         priv = (struct ehci_mxc_priv *) ehci->priv;
85
86         /* enable clocks */
87         priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
88         if (IS_ERR(priv->usbclk)) {
89                 ret = PTR_ERR(priv->usbclk);
90                 goto err_alloc;
91         }
92         clk_prepare_enable(priv->usbclk);
93
94         priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
95         if (IS_ERR(priv->ahbclk)) {
96                 ret = PTR_ERR(priv->ahbclk);
97                 goto err_clk_ahb;
98         }
99         clk_prepare_enable(priv->ahbclk);
100
101         /* "dr" device has its own clock on i.MX51 */
102         priv->phyclk = devm_clk_get(&pdev->dev, "phy");
103         if (IS_ERR(priv->phyclk))
104                 priv->phyclk = NULL;
105         if (priv->phyclk)
106                 clk_prepare_enable(priv->phyclk);
107
108
109         /* call platform specific init function */
110         if (pdata->init) {
111                 ret = pdata->init(pdev);
112                 if (ret) {
113                         dev_err(dev, "platform init failed\n");
114                         goto err_init;
115                 }
116                 /* platforms need some time to settle changed IO settings */
117                 mdelay(10);
118         }
119
120         /* EHCI registers start at offset 0x100 */
121         ehci->caps = hcd->regs + 0x100;
122         ehci->regs = hcd->regs + 0x100 +
123                 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
124
125         /* set up the PORTSCx register */
126         ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
127
128         /* is this really needed? */
129         msleep(10);
130
131         /* Initialize the transceiver */
132         if (pdata->otg) {
133                 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
134                 ret = usb_phy_init(pdata->otg);
135                 if (ret) {
136                         dev_err(dev, "unable to init transceiver, probably missing\n");
137                         ret = -ENODEV;
138                         goto err_add;
139                 }
140                 ret = otg_set_vbus(pdata->otg->otg, 1);
141                 if (ret) {
142                         dev_err(dev, "unable to enable vbus on transceiver\n");
143                         goto err_add;
144                 }
145         }
146
147         platform_set_drvdata(pdev, hcd);
148
149         ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
150         if (ret)
151                 goto err_add;
152
153         device_wakeup_enable(hcd->self.controller);
154         return 0;
155
156 err_add:
157         if (pdata && pdata->exit)
158                 pdata->exit(pdev);
159 err_init:
160         if (priv->phyclk)
161                 clk_disable_unprepare(priv->phyclk);
162
163         clk_disable_unprepare(priv->ahbclk);
164 err_clk_ahb:
165         clk_disable_unprepare(priv->usbclk);
166 err_alloc:
167         usb_put_hcd(hcd);
168         return ret;
169 }
170
171 static int ehci_mxc_drv_remove(struct platform_device *pdev)
172 {
173         struct mxc_usbh_platform_data *pdata = dev_get_platdata(&pdev->dev);
174         struct usb_hcd *hcd = platform_get_drvdata(pdev);
175         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
176         struct ehci_mxc_priv *priv = (struct ehci_mxc_priv *) ehci->priv;
177
178         usb_remove_hcd(hcd);
179
180         if (pdata && pdata->exit)
181                 pdata->exit(pdev);
182
183         if (pdata && pdata->otg)
184                 usb_phy_shutdown(pdata->otg);
185
186         clk_disable_unprepare(priv->usbclk);
187         clk_disable_unprepare(priv->ahbclk);
188
189         if (priv->phyclk)
190                 clk_disable_unprepare(priv->phyclk);
191
192         usb_put_hcd(hcd);
193         return 0;
194 }
195
196 MODULE_ALIAS("platform:mxc-ehci");
197
198 static struct platform_driver ehci_mxc_driver = {
199         .probe = ehci_mxc_drv_probe,
200         .remove = ehci_mxc_drv_remove,
201         .shutdown = usb_hcd_platform_shutdown,
202         .driver = {
203                    .name = "mxc-ehci",
204         },
205 };
206
207 static int __init ehci_mxc_init(void)
208 {
209         if (usb_disabled())
210                 return -ENODEV;
211
212         pr_info("%s: " DRIVER_DESC "\n", hcd_name);
213
214         ehci_init_driver(&ehci_mxc_hc_driver, &ehci_mxc_overrides);
215         return platform_driver_register(&ehci_mxc_driver);
216 }
217 module_init(ehci_mxc_init);
218
219 static void __exit ehci_mxc_cleanup(void)
220 {
221         platform_driver_unregister(&ehci_mxc_driver);
222 }
223 module_exit(ehci_mxc_cleanup);
224
225 MODULE_DESCRIPTION(DRIVER_DESC);
226 MODULE_AUTHOR("Sascha Hauer");
227 MODULE_LICENSE("GPL");