2 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
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.
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
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.
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>
27 #include <mach/hardware.h>
28 #include <mach/mxc_ehci.h>
30 #include <asm/mach-types.h>
32 #define ULPI_VIEWPORT_OFFSET 0x170
34 struct ehci_mxc_priv {
35 struct clk *usbclk, *ahbclk, *phyclk;
39 /* called during probe() after chip reset completes */
40 static int ehci_mxc_setup(struct usb_hcd *hcd)
42 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
47 retval = ehci_setup(hcd);
51 ehci_port_power(ehci, 0);
55 static const struct hc_driver ehci_mxc_hc_driver = {
56 .description = hcd_name,
57 .product_desc = "Freescale On-Chip EHCI Host Controller",
58 .hcd_priv_size = sizeof(struct ehci_hcd),
61 * generic hardware linkage
64 .flags = HCD_USB2 | HCD_MEMORY,
67 * basic lifecycle operations
69 .reset = ehci_mxc_setup,
72 .shutdown = ehci_shutdown,
75 * managing i/o requests and associated device resources
77 .urb_enqueue = ehci_urb_enqueue,
78 .urb_dequeue = ehci_urb_dequeue,
79 .endpoint_disable = ehci_endpoint_disable,
80 .endpoint_reset = ehci_endpoint_reset,
85 .get_frame_number = ehci_get_frame,
90 .hub_status_data = ehci_hub_status_data,
91 .hub_control = ehci_hub_control,
92 .bus_suspend = ehci_bus_suspend,
93 .bus_resume = ehci_bus_resume,
94 .relinquish_port = ehci_relinquish_port,
95 .port_handed_over = ehci_port_handed_over,
97 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
100 static int ehci_mxc_drv_probe(struct platform_device *pdev)
102 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
104 struct resource *res;
107 struct ehci_mxc_priv *priv;
108 struct device *dev = &pdev->dev;
109 struct ehci_hcd *ehci;
111 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
114 dev_err(dev, "No platform data given, bailing out.\n");
118 irq = platform_get_irq(pdev, 0);
120 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
124 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
130 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
132 dev_err(dev, "Found HC with no register addr. Check setup!\n");
134 goto err_get_resource;
137 hcd->rsrc_start = res->start;
138 hcd->rsrc_len = resource_size(res);
140 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
141 dev_dbg(dev, "controller already in use\n");
143 goto err_request_mem;
146 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
148 dev_err(dev, "error mapping memory\n");
154 priv->usbclk = clk_get(dev, "ipg");
155 if (IS_ERR(priv->usbclk)) {
156 ret = PTR_ERR(priv->usbclk);
159 clk_prepare_enable(priv->usbclk);
161 priv->ahbclk = clk_get(dev, "ahb");
162 if (IS_ERR(priv->ahbclk)) {
163 ret = PTR_ERR(priv->ahbclk);
166 clk_prepare_enable(priv->ahbclk);
168 /* "dr" device has its own clock on i.MX51 */
169 priv->phyclk = clk_get(dev, "phy");
170 if (IS_ERR(priv->phyclk))
173 clk_prepare_enable(priv->phyclk);
176 /* call platform specific init function */
178 ret = pdata->init(pdev);
180 dev_err(dev, "platform init failed\n");
183 /* platforms need some time to settle changed IO settings */
187 ehci = hcd_to_ehci(hcd);
189 /* EHCI registers start at offset 0x100 */
190 ehci->caps = hcd->regs + 0x100;
191 ehci->regs = hcd->regs + 0x100 +
192 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
194 /* set up the PORTSCx register */
195 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
197 /* is this really needed? */
200 /* Initialize the transceiver */
202 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
203 ret = usb_phy_init(pdata->otg);
205 dev_err(dev, "unable to init transceiver, probably missing\n");
209 ret = otg_set_vbus(pdata->otg->otg, 1);
211 dev_err(dev, "unable to enable vbus on transceiver\n");
217 platform_set_drvdata(pdev, priv);
219 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
225 * efikamx and efikasb have some hardware bug which is
226 * preventing usb to work unless CHRGVBUS is set.
227 * It's in violation of USB specs
229 if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
230 flags = usb_phy_io_read(pdata->otg,
232 flags |= ULPI_OTG_CTRL_CHRGVBUS;
233 ret = usb_phy_io_write(pdata->otg, flags,
236 dev_err(dev, "unable to set CHRVBUS\n");
245 if (pdata && pdata->exit)
249 clk_disable_unprepare(priv->phyclk);
250 clk_put(priv->phyclk);
253 clk_disable_unprepare(priv->ahbclk);
254 clk_put(priv->ahbclk);
256 clk_disable_unprepare(priv->usbclk);
257 clk_put(priv->usbclk);
261 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
270 static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
272 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
273 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
274 struct usb_hcd *hcd = priv->hcd;
276 if (pdata && pdata->exit)
280 usb_phy_shutdown(pdata->otg);
284 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
286 platform_set_drvdata(pdev, NULL);
288 clk_disable_unprepare(priv->usbclk);
289 clk_put(priv->usbclk);
290 clk_disable_unprepare(priv->ahbclk);
291 clk_put(priv->ahbclk);
294 clk_disable_unprepare(priv->phyclk);
295 clk_put(priv->phyclk);
303 static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
305 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
306 struct usb_hcd *hcd = priv->hcd;
308 if (hcd->driver->shutdown)
309 hcd->driver->shutdown(hcd);
312 MODULE_ALIAS("platform:mxc-ehci");
314 static struct platform_driver ehci_mxc_driver = {
315 .probe = ehci_mxc_drv_probe,
316 .remove = __exit_p(ehci_mxc_drv_remove),
317 .shutdown = ehci_mxc_drv_shutdown,