Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 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> |
Arnaud Patard (Rtp) | a464dc4 | 2011-01-18 00:04:12 +0100 | [diff] [blame] | 24 | #include <linux/usb/ulpi.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 25 | #include <linux/slab.h> |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 26 | |
Shawn Guo | 03a1d6b | 2011-07-21 22:24:10 +0800 | [diff] [blame] | 27 | #include <mach/hardware.h> |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 28 | #include <mach/mxc_ehci.h> |
| 29 | |
Arnaud Patard (Rtp) | a464dc4 | 2011-01-18 00:04:12 +0100 | [diff] [blame] | 30 | #include <asm/mach-types.h> |
| 31 | |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 32 | #define ULPI_VIEWPORT_OFFSET 0x170 |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 33 | |
| 34 | struct ehci_mxc_priv { |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 35 | struct clk *usbclk, *ahbclk, *phyclk; |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 36 | struct usb_hcd *hcd; |
| 37 | }; |
| 38 | |
| 39 | /* called during probe() after chip reset completes */ |
| 40 | static int ehci_mxc_setup(struct usb_hcd *hcd) |
| 41 | { |
| 42 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 43 | int retval; |
| 44 | |
Matthieu CASTET | 65fd427 | 2010-09-06 18:26:56 +0200 | [diff] [blame] | 45 | hcd->has_tt = 1; |
| 46 | |
Alan Stern | 1a49e2a | 2012-07-09 15:55:14 -0400 | [diff] [blame] | 47 | retval = ehci_setup(hcd); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 48 | if (retval) |
| 49 | return retval; |
| 50 | |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 51 | ehci_port_power(ehci, 0); |
| 52 | return 0; |
| 53 | } |
| 54 | |
| 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), |
| 59 | |
| 60 | /* |
| 61 | * generic hardware linkage |
| 62 | */ |
| 63 | .irq = ehci_irq, |
| 64 | .flags = HCD_USB2 | HCD_MEMORY, |
| 65 | |
| 66 | /* |
| 67 | * basic lifecycle operations |
| 68 | */ |
| 69 | .reset = ehci_mxc_setup, |
| 70 | .start = ehci_run, |
| 71 | .stop = ehci_stop, |
| 72 | .shutdown = ehci_shutdown, |
| 73 | |
| 74 | /* |
| 75 | * managing i/o requests and associated device resources |
| 76 | */ |
| 77 | .urb_enqueue = ehci_urb_enqueue, |
| 78 | .urb_dequeue = ehci_urb_dequeue, |
| 79 | .endpoint_disable = ehci_endpoint_disable, |
Paul Mundt | b48d7f5 | 2010-11-30 17:57:02 +0900 | [diff] [blame] | 80 | .endpoint_reset = ehci_endpoint_reset, |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 81 | |
| 82 | /* |
| 83 | * scheduling support |
| 84 | */ |
| 85 | .get_frame_number = ehci_get_frame, |
| 86 | |
| 87 | /* |
| 88 | * root hub support |
| 89 | */ |
| 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, |
Paul Mundt | b48d7f5 | 2010-11-30 17:57:02 +0900 | [diff] [blame] | 96 | |
| 97 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 98 | }; |
| 99 | |
| 100 | static int ehci_mxc_drv_probe(struct platform_device *pdev) |
| 101 | { |
| 102 | struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data; |
| 103 | struct usb_hcd *hcd; |
| 104 | struct resource *res; |
Uwe Kleine-König | 724c852 | 2010-11-02 10:30:57 +0100 | [diff] [blame] | 105 | int irq, ret; |
Arnaud Patard (Rtp) | a464dc4 | 2011-01-18 00:04:12 +0100 | [diff] [blame] | 106 | unsigned int flags; |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 107 | struct ehci_mxc_priv *priv; |
| 108 | struct device *dev = &pdev->dev; |
Fabio Estevam | 0247a7b | 2010-12-15 22:31:28 -0200 | [diff] [blame] | 109 | struct ehci_hcd *ehci; |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 110 | |
| 111 | dev_info(&pdev->dev, "initializing i.MX USB Controller\n"); |
| 112 | |
| 113 | if (!pdata) { |
| 114 | dev_err(dev, "No platform data given, bailing out.\n"); |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | |
| 118 | irq = platform_get_irq(pdev, 0); |
| 119 | |
| 120 | hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev)); |
| 121 | if (!hcd) |
| 122 | return -ENOMEM; |
| 123 | |
| 124 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 125 | if (!priv) { |
| 126 | ret = -ENOMEM; |
| 127 | goto err_alloc; |
| 128 | } |
| 129 | |
| 130 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 131 | if (!res) { |
| 132 | dev_err(dev, "Found HC with no register addr. Check setup!\n"); |
| 133 | ret = -ENODEV; |
| 134 | goto err_get_resource; |
| 135 | } |
| 136 | |
| 137 | hcd->rsrc_start = res->start; |
| 138 | hcd->rsrc_len = resource_size(res); |
| 139 | |
| 140 | if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) { |
| 141 | dev_dbg(dev, "controller already in use\n"); |
| 142 | ret = -EBUSY; |
| 143 | goto err_request_mem; |
| 144 | } |
| 145 | |
| 146 | hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len); |
| 147 | if (!hcd->regs) { |
| 148 | dev_err(dev, "error mapping memory\n"); |
| 149 | ret = -EFAULT; |
| 150 | goto err_ioremap; |
| 151 | } |
| 152 | |
| 153 | /* enable clocks */ |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 154 | priv->usbclk = clk_get(dev, "ipg"); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 155 | if (IS_ERR(priv->usbclk)) { |
| 156 | ret = PTR_ERR(priv->usbclk); |
| 157 | goto err_clk; |
| 158 | } |
Sascha Hauer | 198ad2c | 2012-03-07 20:58:21 +0100 | [diff] [blame] | 159 | clk_prepare_enable(priv->usbclk); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 160 | |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 161 | priv->ahbclk = clk_get(dev, "ahb"); |
| 162 | if (IS_ERR(priv->ahbclk)) { |
| 163 | ret = PTR_ERR(priv->ahbclk); |
| 164 | goto err_clk_ahb; |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 165 | } |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 166 | clk_prepare_enable(priv->ahbclk); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 167 | |
Eric Bénard | 3bb8029 | 2011-01-13 14:53:17 +0100 | [diff] [blame] | 168 | /* "dr" device has its own clock on i.MX51 */ |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 169 | priv->phyclk = clk_get(dev, "phy"); |
| 170 | if (IS_ERR(priv->phyclk)) |
| 171 | priv->phyclk = NULL; |
| 172 | if (priv->phyclk) |
| 173 | clk_prepare_enable(priv->phyclk); |
Arnaud Patard (Rtp) | 711669e | 2010-12-20 16:48:58 +0100 | [diff] [blame] | 174 | |
| 175 | |
| 176 | /* call platform specific init function */ |
| 177 | if (pdata->init) { |
| 178 | ret = pdata->init(pdev); |
| 179 | if (ret) { |
| 180 | dev_err(dev, "platform init failed\n"); |
| 181 | goto err_init; |
| 182 | } |
| 183 | /* platforms need some time to settle changed IO settings */ |
| 184 | mdelay(10); |
| 185 | } |
| 186 | |
Fabio Estevam | 0247a7b | 2010-12-15 22:31:28 -0200 | [diff] [blame] | 187 | ehci = hcd_to_ehci(hcd); |
| 188 | |
| 189 | /* EHCI registers start at offset 0x100 */ |
| 190 | ehci->caps = hcd->regs + 0x100; |
| 191 | ehci->regs = hcd->regs + 0x100 + |
Jan Andersson | c430131 | 2011-05-03 20:11:57 +0200 | [diff] [blame] | 192 | HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase)); |
Fabio Estevam | 0247a7b | 2010-12-15 22:31:28 -0200 | [diff] [blame] | 193 | |
| 194 | /* set up the PORTSCx register */ |
| 195 | ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]); |
| 196 | |
| 197 | /* is this really needed? */ |
| 198 | msleep(10); |
| 199 | |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 200 | /* Initialize the transceiver */ |
| 201 | if (pdata->otg) { |
| 202 | pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET; |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 203 | ret = usb_phy_init(pdata->otg); |
Wolfram Sang | 4c9715d | 2010-06-15 12:34:23 +0200 | [diff] [blame] | 204 | if (ret) { |
| 205 | dev_err(dev, "unable to init transceiver, probably missing\n"); |
| 206 | ret = -ENODEV; |
| 207 | goto err_add; |
| 208 | } |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 209 | ret = otg_set_vbus(pdata->otg->otg, 1); |
Wolfram Sang | 4c9715d | 2010-06-15 12:34:23 +0200 | [diff] [blame] | 210 | if (ret) { |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 211 | dev_err(dev, "unable to enable vbus on transceiver\n"); |
Wolfram Sang | 4c9715d | 2010-06-15 12:34:23 +0200 | [diff] [blame] | 212 | goto err_add; |
| 213 | } |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | priv->hcd = hcd; |
| 217 | platform_set_drvdata(pdev, priv); |
| 218 | |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 219 | ret = usb_add_hcd(hcd, irq, IRQF_SHARED); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 220 | if (ret) |
| 221 | goto err_add; |
| 222 | |
Arnaud Patard (Rtp) | a464dc4 | 2011-01-18 00:04:12 +0100 | [diff] [blame] | 223 | if (pdata->otg) { |
| 224 | /* |
| 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 |
| 228 | */ |
| 229 | if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) { |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 230 | flags = usb_phy_io_read(pdata->otg, |
| 231 | ULPI_OTG_CTRL); |
Arnaud Patard (Rtp) | a464dc4 | 2011-01-18 00:04:12 +0100 | [diff] [blame] | 232 | flags |= ULPI_OTG_CTRL_CHRGVBUS; |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 233 | ret = usb_phy_io_write(pdata->otg, flags, |
| 234 | ULPI_OTG_CTRL); |
Arnaud Patard (Rtp) | a464dc4 | 2011-01-18 00:04:12 +0100 | [diff] [blame] | 235 | if (ret) { |
| 236 | dev_err(dev, "unable to set CHRVBUS\n"); |
| 237 | goto err_add; |
| 238 | } |
| 239 | } |
| 240 | } |
| 241 | |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 242 | return 0; |
| 243 | |
| 244 | err_add: |
| 245 | if (pdata && pdata->exit) |
| 246 | pdata->exit(pdev); |
| 247 | err_init: |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 248 | if (priv->phyclk) { |
| 249 | clk_disable_unprepare(priv->phyclk); |
| 250 | clk_put(priv->phyclk); |
Arnaud Patard (Rtp) | 711669e | 2010-12-20 16:48:58 +0100 | [diff] [blame] | 251 | } |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 252 | |
| 253 | clk_disable_unprepare(priv->ahbclk); |
| 254 | clk_put(priv->ahbclk); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 255 | err_clk_ahb: |
Sascha Hauer | 198ad2c | 2012-03-07 20:58:21 +0100 | [diff] [blame] | 256 | clk_disable_unprepare(priv->usbclk); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 257 | clk_put(priv->usbclk); |
| 258 | err_clk: |
| 259 | iounmap(hcd->regs); |
| 260 | err_ioremap: |
| 261 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 262 | err_request_mem: |
| 263 | err_get_resource: |
| 264 | kfree(priv); |
| 265 | err_alloc: |
| 266 | usb_put_hcd(hcd); |
| 267 | return ret; |
| 268 | } |
| 269 | |
| 270 | static int __exit ehci_mxc_drv_remove(struct platform_device *pdev) |
| 271 | { |
| 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; |
| 275 | |
| 276 | if (pdata && pdata->exit) |
| 277 | pdata->exit(pdev); |
| 278 | |
| 279 | if (pdata->otg) |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 280 | usb_phy_shutdown(pdata->otg); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 281 | |
| 282 | usb_remove_hcd(hcd); |
| 283 | iounmap(hcd->regs); |
| 284 | release_mem_region(hcd->rsrc_start, hcd->rsrc_len); |
| 285 | usb_put_hcd(hcd); |
| 286 | platform_set_drvdata(pdev, NULL); |
| 287 | |
Sascha Hauer | 198ad2c | 2012-03-07 20:58:21 +0100 | [diff] [blame] | 288 | clk_disable_unprepare(priv->usbclk); |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 289 | clk_put(priv->usbclk); |
Sascha Hauer | c943740 | 2012-04-25 16:39:06 +0200 | [diff] [blame] | 290 | clk_disable_unprepare(priv->ahbclk); |
| 291 | clk_put(priv->ahbclk); |
| 292 | |
| 293 | if (priv->phyclk) { |
| 294 | clk_disable_unprepare(priv->phyclk); |
| 295 | clk_put(priv->phyclk); |
Arnaud Patard (Rtp) | 711669e | 2010-12-20 16:48:58 +0100 | [diff] [blame] | 296 | } |
Daniel Mack | 7e8d5cd | 2009-10-28 01:14:59 +0100 | [diff] [blame] | 297 | |
| 298 | kfree(priv); |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
| 303 | static void ehci_mxc_drv_shutdown(struct platform_device *pdev) |
| 304 | { |
| 305 | struct ehci_mxc_priv *priv = platform_get_drvdata(pdev); |
| 306 | struct usb_hcd *hcd = priv->hcd; |
| 307 | |
| 308 | if (hcd->driver->shutdown) |
| 309 | hcd->driver->shutdown(hcd); |
| 310 | } |
| 311 | |
| 312 | MODULE_ALIAS("platform:mxc-ehci"); |
| 313 | |
| 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, |
| 318 | .driver = { |
| 319 | .name = "mxc-ehci", |
| 320 | }, |
| 321 | }; |