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/slab.h>
26 #include <mach/mxc_ehci.h>
28 #define ULPI_VIEWPORT_OFFSET 0x170
30 struct ehci_mxc_priv {
31 struct clk *usbclk, *ahbclk;
35 /* called during probe() after chip reset completes */
36 static int ehci_mxc_setup(struct usb_hcd *hcd)
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);
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");
50 /* cache this readonly data; minimize chip reads */
51 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
55 retval = ehci_halt(ehci);
59 /* data structure init */
60 retval = ehci_init(hcd);
68 /* set up the PORTSCx register */
69 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
71 /* is this really needed? */
74 ehci_port_power(ehci, 0);
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),
84 * generic hardware linkage
87 .flags = HCD_USB2 | HCD_MEMORY,
90 * basic lifecycle operations
92 .reset = ehci_mxc_setup,
95 .shutdown = ehci_shutdown,
98 * managing i/o requests and associated device resources
100 .urb_enqueue = ehci_urb_enqueue,
101 .urb_dequeue = ehci_urb_dequeue,
102 .endpoint_disable = ehci_endpoint_disable,
107 .get_frame_number = ehci_get_frame,
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,
120 static int ehci_mxc_drv_probe(struct platform_device *pdev)
122 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
124 struct resource *res;
126 struct ehci_mxc_priv *priv;
127 struct device *dev = &pdev->dev;
129 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
132 dev_err(dev, "No platform data given, bailing out.\n");
136 irq = platform_get_irq(pdev, 0);
138 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
142 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
148 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
150 dev_err(dev, "Found HC with no register addr. Check setup!\n");
152 goto err_get_resource;
155 hcd->rsrc_start = res->start;
156 hcd->rsrc_len = resource_size(res);
158 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
159 dev_dbg(dev, "controller already in use\n");
161 goto err_request_mem;
164 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
166 dev_err(dev, "error mapping memory\n");
171 /* call platform specific init function */
173 ret = pdata->init(pdev);
175 dev_err(dev, "platform init failed\n");
178 /* platforms need some time to settle changed IO settings */
183 priv->usbclk = clk_get(dev, "usb");
184 if (IS_ERR(priv->usbclk)) {
185 ret = PTR_ERR(priv->usbclk);
188 clk_enable(priv->usbclk);
190 if (!cpu_is_mx35() && !cpu_is_mx25()) {
191 priv->ahbclk = clk_get(dev, "usb_ahb");
192 if (IS_ERR(priv->ahbclk)) {
193 ret = PTR_ERR(priv->ahbclk);
196 clk_enable(priv->ahbclk);
199 /* setup specific usb hw */
200 ret = mxc_initialize_usb_hw(pdev->id, pdata->flags);
204 /* Initialize the transceiver */
206 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
207 ret = otg_init(pdata->otg);
209 dev_err(dev, "unable to init transceiver, probably missing\n");
213 ret = otg_set_vbus(pdata->otg, 1);
215 dev_err(dev, "unable to enable vbus on transceiver\n");
221 platform_set_drvdata(pdev, priv);
223 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
230 if (pdata && pdata->exit)
234 clk_disable(priv->ahbclk);
235 clk_put(priv->ahbclk);
238 clk_disable(priv->usbclk);
239 clk_put(priv->usbclk);
243 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
252 static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
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;
258 if (pdata && pdata->exit)
262 otg_shutdown(pdata->otg);
266 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
268 platform_set_drvdata(pdev, NULL);
270 clk_disable(priv->usbclk);
271 clk_put(priv->usbclk);
273 clk_disable(priv->ahbclk);
274 clk_put(priv->ahbclk);
282 static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
284 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
285 struct usb_hcd *hcd = priv->hcd;
287 if (hcd->driver->shutdown)
288 hcd->driver->shutdown(hcd);
291 MODULE_ALIAS("platform:mxc-ehci");
293 static struct platform_driver ehci_mxc_driver = {
294 .probe = ehci_mxc_drv_probe,
295 .remove = __exit_p(ehci_mxc_drv_remove),
296 .shutdown = ehci_mxc_drv_shutdown,