blob: 0d5ac36fdf47aab8dec5cf96e082ac48ad1e0f65 [file] [log] [blame]
Felipe Balbi54ab2b02009-10-14 11:44:14 +03001/*
Anand Gadiyar4792a152010-11-21 23:23:42 +05302 * ehci-omap.c - driver for USBHOST on OMAP3/4 processors
Felipe Balbi54ab2b02009-10-14 11:44:14 +03003 *
Anand Gadiyar4792a152010-11-21 23:23:42 +05304 * Bus Glue for the EHCI controllers in OMAP3/4
5 * Tested on several OMAP3 boards, and OMAP4 Pandaboard
Felipe Balbi54ab2b02009-10-14 11:44:14 +03006 *
Keshava Munegowda19403162011-03-01 20:08:21 +05307 * Copyright (C) 2007-2011 Texas Instruments, Inc.
Felipe Balbi54ab2b02009-10-14 11:44:14 +03008 * Author: Vikram Pandita <vikram.pandita@ti.com>
Anand Gadiyar4792a152010-11-21 23:23:42 +05309 * Author: Anand Gadiyar <gadiyar@ti.com>
Keshava Munegowda19403162011-03-01 20:08:21 +053010 * Author: Keshava Munegowda <keshava_mgowda@ti.com>
Felipe Balbi54ab2b02009-10-14 11:44:14 +030011 *
12 * Copyright (C) 2009 Nokia Corporation
13 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
14 *
15 * Based on "ehci-fsl.c" and "ehci-au1xxx.c" ehci glue layers
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 *
Keshava Munegowda19403162011-03-01 20:08:21 +053031 * TODO (last updated Feb 27, 2010):
Felipe Balbi54ab2b02009-10-14 11:44:14 +030032 * - add kernel-doc
33 * - enable AUTOIDLE
Felipe Balbi54ab2b02009-10-14 11:44:14 +030034 * - add suspend/resume
Anand Gadiyar4792a152010-11-21 23:23:42 +053035 * - add HSIC and TLL support
36 * - convert to use hwmod and runtime PM
Felipe Balbi54ab2b02009-10-14 11:44:14 +030037 */
38
39#include <linux/platform_device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090040#include <linux/slab.h>
Ajay Kumar Gupta5aa4af22010-07-08 14:03:02 +053041#include <linux/usb/ulpi.h>
Keshava Munegowda6e3d4be2011-04-21 19:52:43 +053042#include <linux/regulator/consumer.h>
Keshava Munegowda6c984b02011-10-11 13:22:11 +053043#include <linux/pm_runtime.h>
Keshava Munegowda1fcb57d2012-03-19 12:12:47 +053044#include <linux/gpio.h>
Anand Gadiyar354ab852012-06-05 15:34:27 +030045#include <linux/clk.h>
Felipe Balbi54ab2b02009-10-14 11:44:14 +030046
Felipe Balbie8c4a7a2012-10-24 14:26:19 -070047#include <linux/platform_data/usb-omap.h>
48
Felipe Balbi54ab2b02009-10-14 11:44:14 +030049/* EHCI Register Set */
Anand Gadiyar572538d2010-05-06 20:09:48 +053050#define EHCI_INSNREG04 (0xA0)
51#define EHCI_INSNREG04_DISABLE_UNSUSPEND (1 << 5)
Felipe Balbi54ab2b02009-10-14 11:44:14 +030052#define EHCI_INSNREG05_ULPI (0xA4)
53#define EHCI_INSNREG05_ULPI_CONTROL_SHIFT 31
54#define EHCI_INSNREG05_ULPI_PORTSEL_SHIFT 24
55#define EHCI_INSNREG05_ULPI_OPSEL_SHIFT 22
56#define EHCI_INSNREG05_ULPI_REGADD_SHIFT 16
57#define EHCI_INSNREG05_ULPI_EXTREGADD_SHIFT 8
58#define EHCI_INSNREG05_ULPI_WRDATA_SHIFT 0
59
60/*-------------------------------------------------------------------------*/
61
Keshava Munegowda19403162011-03-01 20:08:21 +053062static const struct hc_driver ehci_omap_hc_driver;
63
64
65static inline void ehci_write(void __iomem *base, u32 reg, u32 val)
Felipe Balbi54ab2b02009-10-14 11:44:14 +030066{
67 __raw_writel(val, base + reg);
68}
69
Keshava Munegowda19403162011-03-01 20:08:21 +053070static inline u32 ehci_read(void __iomem *base, u32 reg)
Felipe Balbi54ab2b02009-10-14 11:44:14 +030071{
72 return __raw_readl(base + reg);
73}
74
Anand Gadiyar354ab852012-06-05 15:34:27 +030075
Ming Leiba5952e2012-07-13 17:25:24 +080076static void omap_ehci_soft_phy_reset(struct usb_hcd *hcd, u8 port)
Felipe Balbi54ab2b02009-10-14 11:44:14 +030077{
Ajay Kumar Gupta5aa4af22010-07-08 14:03:02 +053078 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
79 unsigned reg = 0;
80
81 reg = ULPI_FUNC_CTRL_RESET
82 /* FUNCTION_CTRL_SET register */
83 | (ULPI_SET(ULPI_FUNC_CTRL) << EHCI_INSNREG05_ULPI_REGADD_SHIFT)
84 /* Write */
85 | (2 << EHCI_INSNREG05_ULPI_OPSEL_SHIFT)
86 /* PORTn */
87 | ((port + 1) << EHCI_INSNREG05_ULPI_PORTSEL_SHIFT)
88 /* start ULPI access*/
89 | (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT);
90
Keshava Munegowda19403162011-03-01 20:08:21 +053091 ehci_write(hcd->regs, EHCI_INSNREG05_ULPI, reg);
Ajay Kumar Gupta5aa4af22010-07-08 14:03:02 +053092
93 /* Wait for ULPI access completion */
Keshava Munegowda19403162011-03-01 20:08:21 +053094 while ((ehci_read(hcd->regs, EHCI_INSNREG05_ULPI)
Ajay Kumar Gupta5aa4af22010-07-08 14:03:02 +053095 & (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT))) {
96 cpu_relax();
97
98 if (time_after(jiffies, timeout)) {
Ming Leiba5952e2012-07-13 17:25:24 +080099 dev_dbg(hcd->self.controller,
100 "phy reset operation timed out\n");
Ajay Kumar Gupta5aa4af22010-07-08 14:03:02 +0530101 break;
102 }
103 }
104}
105
Alan Stern1a49e2a2012-07-09 15:55:14 -0400106static int omap_ehci_init(struct usb_hcd *hcd)
107{
108 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
109 int rc;
110 struct ehci_hcd_omap_platform_data *pdata;
111
112 pdata = hcd->self.controller->platform_data;
Linus Torvalds3e701cd2012-07-30 12:41:17 -0700113
114 /* Hold PHYs in reset while initializing EHCI controller */
Alan Stern1a49e2a2012-07-09 15:55:14 -0400115 if (pdata->phy_reset) {
116 if (gpio_is_valid(pdata->reset_gpio_port[0]))
Linus Torvalds3e701cd2012-07-30 12:41:17 -0700117 gpio_set_value_cansleep(pdata->reset_gpio_port[0], 0);
Alan Stern1a49e2a2012-07-09 15:55:14 -0400118
119 if (gpio_is_valid(pdata->reset_gpio_port[1]))
Linus Torvalds3e701cd2012-07-30 12:41:17 -0700120 gpio_set_value_cansleep(pdata->reset_gpio_port[1], 0);
Alan Stern1a49e2a2012-07-09 15:55:14 -0400121
122 /* Hold the PHY in RESET for enough time till DIR is high */
123 udelay(10);
124 }
125
126 /* Soft reset the PHY using PHY reset command over ULPI */
127 if (pdata->port_mode[0] == OMAP_EHCI_PORT_MODE_PHY)
Ming Leiba5952e2012-07-13 17:25:24 +0800128 omap_ehci_soft_phy_reset(hcd, 0);
Alan Stern1a49e2a2012-07-09 15:55:14 -0400129 if (pdata->port_mode[1] == OMAP_EHCI_PORT_MODE_PHY)
Ming Leiba5952e2012-07-13 17:25:24 +0800130 omap_ehci_soft_phy_reset(hcd, 1);
Alan Stern1a49e2a2012-07-09 15:55:14 -0400131
132 /* we know this is the memory we want, no need to ioremap again */
133 ehci->caps = hcd->regs;
134
135 rc = ehci_setup(hcd);
136
137 if (pdata->phy_reset) {
138 /* Hold the PHY in RESET for enough time till
139 * PHY is settled and ready
140 */
141 udelay(10);
142
143 if (gpio_is_valid(pdata->reset_gpio_port[0]))
144 gpio_set_value_cansleep(pdata->reset_gpio_port[0], 1);
145
146 if (gpio_is_valid(pdata->reset_gpio_port[1]))
147 gpio_set_value_cansleep(pdata->reset_gpio_port[1], 1);
148 }
149
150 /* root ports should always stay powered */
151 ehci_port_power(ehci, 1);
152
153 return rc;
154}
155
Ming Leid4aefec2011-07-28 22:59:53 +0800156static void disable_put_regulator(
157 struct ehci_hcd_omap_platform_data *pdata)
158{
159 int i;
160
161 for (i = 0 ; i < OMAP3_HS_USB_PORTS ; i++) {
162 if (pdata->regulator[i]) {
163 regulator_disable(pdata->regulator[i]);
164 regulator_put(pdata->regulator[i]);
165 }
166 }
167}
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300168
169/* configure so an HC device and id are always provided */
170/* always called with process context; sleeping is OK */
171
172/**
173 * ehci_hcd_omap_probe - initialize TI-based HCDs
174 *
175 * Allocates basic resources for this USB host controller, and
176 * then invokes the start() method for the HCD associated with it
177 * through the hotplug entry's driver_data.
178 */
179static int ehci_hcd_omap_probe(struct platform_device *pdev)
180{
Keshava Munegowda19403162011-03-01 20:08:21 +0530181 struct device *dev = &pdev->dev;
182 struct ehci_hcd_omap_platform_data *pdata = dev->platform_data;
183 struct resource *res;
184 struct usb_hcd *hcd;
185 void __iomem *regs;
Keshava Munegowda19403162011-03-01 20:08:21 +0530186 int ret = -ENODEV;
187 int irq;
Keshava Munegowda6e3d4be2011-04-21 19:52:43 +0530188 int i;
189 char supply[7];
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300190
191 if (usb_disabled())
Keshava Munegowda19403162011-03-01 20:08:21 +0530192 return -ENODEV;
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300193
Keshava Munegowda19403162011-03-01 20:08:21 +0530194 if (!dev->parent) {
195 dev_err(dev, "Missing parent device\n");
196 return -ENODEV;
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300197 }
198
Keshava Munegowda19403162011-03-01 20:08:21 +0530199 irq = platform_get_irq_byname(pdev, "ehci-irq");
200 if (irq < 0) {
201 dev_err(dev, "EHCI irq failed\n");
202 return -ENODEV;
203 }
204
205 res = platform_get_resource_byname(pdev,
206 IORESOURCE_MEM, "ehci");
207 if (!res) {
208 dev_err(dev, "UHH EHCI get resource failed\n");
209 return -ENODEV;
210 }
211
212 regs = ioremap(res->start, resource_size(res));
213 if (!regs) {
214 dev_err(dev, "UHH EHCI ioremap failed\n");
215 return -ENOMEM;
216 }
217
218 hcd = usb_create_hcd(&ehci_omap_hc_driver, dev,
219 dev_name(dev));
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300220 if (!hcd) {
Keshava Munegowda19403162011-03-01 20:08:21 +0530221 dev_err(dev, "failed to create hcd with err %d\n", ret);
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300222 ret = -ENOMEM;
Keshava Munegowda19403162011-03-01 20:08:21 +0530223 goto err_io;
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300224 }
225
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300226 hcd->rsrc_start = res->start;
227 hcd->rsrc_len = resource_size(res);
Keshava Munegowda19403162011-03-01 20:08:21 +0530228 hcd->regs = regs;
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300229
Keshava Munegowda6e3d4be2011-04-21 19:52:43 +0530230 /* get ehci regulator and enable */
231 for (i = 0 ; i < OMAP3_HS_USB_PORTS ; i++) {
232 if (pdata->port_mode[i] != OMAP_EHCI_PORT_MODE_PHY) {
233 pdata->regulator[i] = NULL;
234 continue;
235 }
236 snprintf(supply, sizeof(supply), "hsusb%d", i);
237 pdata->regulator[i] = regulator_get(dev, supply);
238 if (IS_ERR(pdata->regulator[i])) {
239 pdata->regulator[i] = NULL;
240 dev_dbg(dev,
241 "failed to get ehci port%d regulator\n", i);
242 } else {
243 regulator_enable(pdata->regulator[i]);
244 }
245 }
246
Keshava Munegowda6c984b02011-10-11 13:22:11 +0530247 pm_runtime_enable(dev);
248 pm_runtime_get_sync(dev);
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300249
Keshava Munegowda19403162011-03-01 20:08:21 +0530250 /*
251 * An undocumented "feature" in the OMAP3 EHCI controller,
252 * causes suspended ports to be taken out of suspend when
253 * the USBCMD.Run/Stop bit is cleared (for example when
254 * we do ehci_bus_suspend).
255 * This breaks suspend-resume if the root-hub is allowed
256 * to suspend. Writing 1 to this undocumented register bit
257 * disables this feature and restores normal behavior.
258 */
259 ehci_write(regs, EHCI_INSNREG04,
260 EHCI_INSNREG04_DISABLE_UNSUSPEND);
261
Russ Dill3aa2ae72012-05-04 04:24:47 -0700262 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
263 if (ret) {
264 dev_err(dev, "failed to add hcd with err %d\n", ret);
Russ Dill984e9742012-06-21 03:44:31 -0700265 goto err_pm_runtime;
Russ Dill3aa2ae72012-05-04 04:24:47 -0700266 }
267
Anand Gadiyar354ab852012-06-05 15:34:27 +0300268
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300269 return 0;
270
Russ Dill984e9742012-06-21 03:44:31 -0700271err_pm_runtime:
Ming Leid4aefec2011-07-28 22:59:53 +0800272 disable_put_regulator(pdata);
Keshava Munegowda6c984b02011-10-11 13:22:11 +0530273 pm_runtime_put_sync(dev);
Russ Dill984e9742012-06-21 03:44:31 -0700274 usb_put_hcd(hcd);
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300275
Keshava Munegowda19403162011-03-01 20:08:21 +0530276err_io:
Ming Leid4aefec2011-07-28 22:59:53 +0800277 iounmap(regs);
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300278 return ret;
279}
280
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300281
282/**
283 * ehci_hcd_omap_remove - shutdown processing for EHCI HCDs
284 * @pdev: USB Host Controller being removed
285 *
286 * Reverses the effect of usb_ehci_hcd_omap_probe(), first invoking
287 * the HCD's stop() method. It is always called from a thread
288 * context, normally "rmmod", "apmd", or something similar.
289 */
290static int ehci_hcd_omap_remove(struct platform_device *pdev)
291{
Keshava Munegowda1fcb57d2012-03-19 12:12:47 +0530292 struct device *dev = &pdev->dev;
293 struct usb_hcd *hcd = dev_get_drvdata(dev);
294 struct ehci_hcd_omap_platform_data *pdata = dev->platform_data;
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300295
296 usb_remove_hcd(hcd);
Ming Leid4aefec2011-07-28 22:59:53 +0800297 disable_put_regulator(dev->platform_data);
298 iounmap(hcd->regs);
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300299 usb_put_hcd(hcd);
Anand Gadiyar354ab852012-06-05 15:34:27 +0300300
Keshava Munegowda6c984b02011-10-11 13:22:11 +0530301 pm_runtime_put_sync(dev);
302 pm_runtime_disable(dev);
303
Keshava Munegowda1fcb57d2012-03-19 12:12:47 +0530304 if (pdata->phy_reset) {
305 if (gpio_is_valid(pdata->reset_gpio_port[0]))
306 gpio_free(pdata->reset_gpio_port[0]);
307
308 if (gpio_is_valid(pdata->reset_gpio_port[1]))
309 gpio_free(pdata->reset_gpio_port[1]);
310 }
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300311 return 0;
312}
313
314static void ehci_hcd_omap_shutdown(struct platform_device *pdev)
315{
Keshava Munegowda19403162011-03-01 20:08:21 +0530316 struct usb_hcd *hcd = dev_get_drvdata(&pdev->dev);
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300317
318 if (hcd->driver->shutdown)
319 hcd->driver->shutdown(hcd);
320}
321
322static struct platform_driver ehci_hcd_omap_driver = {
323 .probe = ehci_hcd_omap_probe,
324 .remove = ehci_hcd_omap_remove,
325 .shutdown = ehci_hcd_omap_shutdown,
326 /*.suspend = ehci_hcd_omap_suspend, */
327 /*.resume = ehci_hcd_omap_resume, */
328 .driver = {
329 .name = "ehci-omap",
330 }
331};
332
333/*-------------------------------------------------------------------------*/
334
335static const struct hc_driver ehci_omap_hc_driver = {
336 .description = hcd_name,
337 .product_desc = "OMAP-EHCI Host Controller",
338 .hcd_priv_size = sizeof(struct ehci_hcd),
339
340 /*
341 * generic hardware linkage
342 */
343 .irq = ehci_irq,
344 .flags = HCD_MEMORY | HCD_USB2,
345
346 /*
347 * basic lifecycle operations
348 */
Alan Stern1a49e2a2012-07-09 15:55:14 -0400349 .reset = omap_ehci_init,
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300350 .start = ehci_run,
351 .stop = ehci_stop,
352 .shutdown = ehci_shutdown,
353
354 /*
355 * managing i/o requests and associated device resources
356 */
357 .urb_enqueue = ehci_urb_enqueue,
358 .urb_dequeue = ehci_urb_dequeue,
359 .endpoint_disable = ehci_endpoint_disable,
360 .endpoint_reset = ehci_endpoint_reset,
361
362 /*
363 * scheduling support
364 */
365 .get_frame_number = ehci_get_frame,
366
367 /*
368 * root hub support
369 */
370 .hub_status_data = ehci_hub_status_data,
Keshava Munegowda872c4952012-07-20 15:13:35 +0530371 .hub_control = ehci_hub_control,
Felipe Balbi54ab2b02009-10-14 11:44:14 +0300372 .bus_suspend = ehci_bus_suspend,
373 .bus_resume = ehci_bus_resume,
374
375 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
376};
377
378MODULE_ALIAS("platform:omap-ehci");
379MODULE_AUTHOR("Texas Instruments, Inc.");
380MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
381