]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/usb/host/ehci-mxc.c
Merge branch 'next/cleanup-samsung-3' of git://git.kernel.org/pub/scm/linux/kernel...
[mirror_ubuntu-artful-kernel.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/usb/ulpi.h>
25 #include <linux/slab.h>
26
27 #include <linux/platform_data/usb-ehci-mxc.h>
28
29 #include <asm/mach-types.h>
30
31 #define ULPI_VIEWPORT_OFFSET 0x170
32
33 struct ehci_mxc_priv {
34 struct clk *usbclk, *ahbclk, *phyclk;
35 struct usb_hcd *hcd;
36 };
37
38 /* called during probe() after chip reset completes */
39 static int ehci_mxc_setup(struct usb_hcd *hcd)
40 {
41 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
42 int retval;
43
44 hcd->has_tt = 1;
45
46 retval = ehci_setup(hcd);
47 if (retval)
48 return retval;
49
50 ehci_port_power(ehci, 0);
51 return 0;
52 }
53
54 static const struct hc_driver ehci_mxc_hc_driver = {
55 .description = hcd_name,
56 .product_desc = "Freescale On-Chip EHCI Host Controller",
57 .hcd_priv_size = sizeof(struct ehci_hcd),
58
59 /*
60 * generic hardware linkage
61 */
62 .irq = ehci_irq,
63 .flags = HCD_USB2 | HCD_MEMORY,
64
65 /*
66 * basic lifecycle operations
67 */
68 .reset = ehci_mxc_setup,
69 .start = ehci_run,
70 .stop = ehci_stop,
71 .shutdown = ehci_shutdown,
72
73 /*
74 * managing i/o requests and associated device resources
75 */
76 .urb_enqueue = ehci_urb_enqueue,
77 .urb_dequeue = ehci_urb_dequeue,
78 .endpoint_disable = ehci_endpoint_disable,
79 .endpoint_reset = ehci_endpoint_reset,
80
81 /*
82 * scheduling support
83 */
84 .get_frame_number = ehci_get_frame,
85
86 /*
87 * root hub support
88 */
89 .hub_status_data = ehci_hub_status_data,
90 .hub_control = ehci_hub_control,
91 .bus_suspend = ehci_bus_suspend,
92 .bus_resume = ehci_bus_resume,
93 .relinquish_port = ehci_relinquish_port,
94 .port_handed_over = ehci_port_handed_over,
95
96 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
97 };
98
99 static int ehci_mxc_drv_probe(struct platform_device *pdev)
100 {
101 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
102 struct usb_hcd *hcd;
103 struct resource *res;
104 int irq, ret;
105 unsigned int flags;
106 struct ehci_mxc_priv *priv;
107 struct device *dev = &pdev->dev;
108 struct ehci_hcd *ehci;
109
110 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
111
112 if (!pdata) {
113 dev_err(dev, "No platform data given, bailing out.\n");
114 return -EINVAL;
115 }
116
117 irq = platform_get_irq(pdev, 0);
118
119 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
120 if (!hcd)
121 return -ENOMEM;
122
123 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
124 if (!priv) {
125 ret = -ENOMEM;
126 goto err_alloc;
127 }
128
129 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
130 if (!res) {
131 dev_err(dev, "Found HC with no register addr. Check setup!\n");
132 ret = -ENODEV;
133 goto err_alloc;
134 }
135
136 hcd->rsrc_start = res->start;
137 hcd->rsrc_len = resource_size(res);
138
139 hcd->regs = devm_request_and_ioremap(&pdev->dev, res);
140 if (!hcd->regs) {
141 dev_err(dev, "error mapping memory\n");
142 ret = -EFAULT;
143 goto err_alloc;
144 }
145
146 /* enable clocks */
147 priv->usbclk = devm_clk_get(&pdev->dev, "ipg");
148 if (IS_ERR(priv->usbclk)) {
149 ret = PTR_ERR(priv->usbclk);
150 goto err_alloc;
151 }
152 clk_prepare_enable(priv->usbclk);
153
154 priv->ahbclk = devm_clk_get(&pdev->dev, "ahb");
155 if (IS_ERR(priv->ahbclk)) {
156 ret = PTR_ERR(priv->ahbclk);
157 goto err_clk_ahb;
158 }
159 clk_prepare_enable(priv->ahbclk);
160
161 /* "dr" device has its own clock on i.MX51 */
162 priv->phyclk = devm_clk_get(&pdev->dev, "phy");
163 if (IS_ERR(priv->phyclk))
164 priv->phyclk = NULL;
165 if (priv->phyclk)
166 clk_prepare_enable(priv->phyclk);
167
168
169 /* call platform specific init function */
170 if (pdata->init) {
171 ret = pdata->init(pdev);
172 if (ret) {
173 dev_err(dev, "platform init failed\n");
174 goto err_init;
175 }
176 /* platforms need some time to settle changed IO settings */
177 mdelay(10);
178 }
179
180 ehci = hcd_to_ehci(hcd);
181
182 /* EHCI registers start at offset 0x100 */
183 ehci->caps = hcd->regs + 0x100;
184 ehci->regs = hcd->regs + 0x100 +
185 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
186
187 /* set up the PORTSCx register */
188 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
189
190 /* is this really needed? */
191 msleep(10);
192
193 /* Initialize the transceiver */
194 if (pdata->otg) {
195 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
196 ret = usb_phy_init(pdata->otg);
197 if (ret) {
198 dev_err(dev, "unable to init transceiver, probably missing\n");
199 ret = -ENODEV;
200 goto err_add;
201 }
202 ret = otg_set_vbus(pdata->otg->otg, 1);
203 if (ret) {
204 dev_err(dev, "unable to enable vbus on transceiver\n");
205 goto err_add;
206 }
207 }
208
209 priv->hcd = hcd;
210 platform_set_drvdata(pdev, priv);
211
212 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
213 if (ret)
214 goto err_add;
215
216 if (pdata->otg) {
217 /*
218 * efikamx and efikasb have some hardware bug which is
219 * preventing usb to work unless CHRGVBUS is set.
220 * It's in violation of USB specs
221 */
222 if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
223 flags = usb_phy_io_read(pdata->otg,
224 ULPI_OTG_CTRL);
225 flags |= ULPI_OTG_CTRL_CHRGVBUS;
226 ret = usb_phy_io_write(pdata->otg, flags,
227 ULPI_OTG_CTRL);
228 if (ret) {
229 dev_err(dev, "unable to set CHRVBUS\n");
230 goto err_add;
231 }
232 }
233 }
234
235 return 0;
236
237 err_add:
238 if (pdata && pdata->exit)
239 pdata->exit(pdev);
240 err_init:
241 if (priv->phyclk)
242 clk_disable_unprepare(priv->phyclk);
243
244 clk_disable_unprepare(priv->ahbclk);
245 err_clk_ahb:
246 clk_disable_unprepare(priv->usbclk);
247 err_alloc:
248 usb_put_hcd(hcd);
249 return ret;
250 }
251
252 static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
253 {
254 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
255 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
256 struct usb_hcd *hcd = priv->hcd;
257
258 if (pdata && pdata->exit)
259 pdata->exit(pdev);
260
261 if (pdata->otg)
262 usb_phy_shutdown(pdata->otg);
263
264 usb_remove_hcd(hcd);
265 usb_put_hcd(hcd);
266 platform_set_drvdata(pdev, NULL);
267
268 clk_disable_unprepare(priv->usbclk);
269 clk_disable_unprepare(priv->ahbclk);
270
271 if (priv->phyclk)
272 clk_disable_unprepare(priv->phyclk);
273
274 return 0;
275 }
276
277 static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
278 {
279 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
280 struct usb_hcd *hcd = priv->hcd;
281
282 if (hcd->driver->shutdown)
283 hcd->driver->shutdown(hcd);
284 }
285
286 MODULE_ALIAS("platform:mxc-ehci");
287
288 static struct platform_driver ehci_mxc_driver = {
289 .probe = ehci_mxc_drv_probe,
290 .remove = __exit_p(ehci_mxc_drv_remove),
291 .shutdown = ehci_mxc_drv_shutdown,
292 .driver = {
293 .name = "mxc-ehci",
294 },
295 };