blob: 477ecfa05154e3fdecfe3bd951e8d65cf1c65da2 [file] [log] [blame]
Benoit Goby79ad3b52011-03-09 16:28:56 -08001/*
2 * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
3 *
4 * Copyright (C) 2010 Google, Inc.
5 * Copyright (C) 2009 NVIDIA Corporation
6 *
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2 of the License, or (at your
10 * option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but WITHOUT
13 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
15 * more details.
16 *
17 */
18
19#include <linux/clk.h>
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +053020#include <linux/err.h>
Benoit Goby79ad3b52011-03-09 16:28:56 -080021#include <linux/platform_device.h>
22#include <linux/platform_data/tegra_usb.h>
23#include <linux/irq.h>
24#include <linux/usb/otg.h>
Olof Johansson4a53f4e2011-11-04 09:12:40 +000025#include <linux/gpio.h>
26#include <linux/of.h>
27#include <linux/of_gpio.h>
Alan Sternebf20de2012-05-01 11:28:49 -040028#include <linux/pm_runtime.h>
Olof Johansson4a53f4e2011-11-04 09:12:40 +000029
Benoit Goby79ad3b52011-03-09 16:28:56 -080030#include <mach/usb_phy.h>
Olof Johansson4a53f4e2011-11-04 09:12:40 +000031#include <mach/iomap.h>
Benoit Goby79ad3b52011-03-09 16:28:56 -080032
Robert Morellfbf98652011-03-09 16:28:57 -080033#define TEGRA_USB_DMA_ALIGN 32
34
Benoit Goby79ad3b52011-03-09 16:28:56 -080035struct tegra_ehci_hcd {
36 struct ehci_hcd *ehci;
37 struct tegra_usb_phy *phy;
38 struct clk *clk;
39 struct clk *emc_clk;
Heikki Krogerus86753812012-02-13 13:24:02 +020040 struct usb_phy *transceiver;
Benoit Goby79ad3b52011-03-09 16:28:56 -080041 int host_resumed;
Benoit Goby79ad3b52011-03-09 16:28:56 -080042 int port_resuming;
Benoit Goby79ad3b52011-03-09 16:28:56 -080043 enum tegra_usb_phy_port_speed port_speed;
44};
45
46static void tegra_ehci_power_up(struct usb_hcd *hcd)
47{
48 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
49
50 clk_enable(tegra->emc_clk);
51 clk_enable(tegra->clk);
52 tegra_usb_phy_power_on(tegra->phy);
53 tegra->host_resumed = 1;
54}
55
56static void tegra_ehci_power_down(struct usb_hcd *hcd)
57{
58 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
59
60 tegra->host_resumed = 0;
61 tegra_usb_phy_power_off(tegra->phy);
62 clk_disable(tegra->clk);
63 clk_disable(tegra->emc_clk);
64}
65
Jim Lin1f594b62011-04-17 11:58:25 +030066static int tegra_ehci_internal_port_reset(
67 struct ehci_hcd *ehci,
68 u32 __iomem *portsc_reg
69)
70{
71 u32 temp;
72 unsigned long flags;
73 int retval = 0;
74 int i, tries;
75 u32 saved_usbintr;
76
77 spin_lock_irqsave(&ehci->lock, flags);
78 saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
79 /* disable USB interrupt */
80 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
81 spin_unlock_irqrestore(&ehci->lock, flags);
82
83 /*
84 * Here we have to do Port Reset at most twice for
85 * Port Enable bit to be set.
86 */
87 for (i = 0; i < 2; i++) {
88 temp = ehci_readl(ehci, portsc_reg);
89 temp |= PORT_RESET;
90 ehci_writel(ehci, temp, portsc_reg);
91 mdelay(10);
92 temp &= ~PORT_RESET;
93 ehci_writel(ehci, temp, portsc_reg);
94 mdelay(1);
95 tries = 100;
96 do {
97 mdelay(1);
98 /*
99 * Up to this point, Port Enable bit is
100 * expected to be set after 2 ms waiting.
101 * USB1 usually takes extra 45 ms, for safety,
102 * we take 100 ms as timeout.
103 */
104 temp = ehci_readl(ehci, portsc_reg);
105 } while (!(temp & PORT_PE) && tries--);
106 if (temp & PORT_PE)
107 break;
108 }
109 if (i == 2)
110 retval = -ETIMEDOUT;
111
112 /*
113 * Clear Connect Status Change bit if it's set.
114 * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
115 */
116 if (temp & PORT_CSC)
117 ehci_writel(ehci, PORT_CSC, portsc_reg);
118
119 /*
120 * Write to clear any interrupt status bits that might be set
121 * during port reset.
122 */
123 temp = ehci_readl(ehci, &ehci->regs->status);
124 ehci_writel(ehci, temp, &ehci->regs->status);
125
126 /* restore original interrupt enable bits */
127 ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
128 return retval;
129}
130
Benoit Goby79ad3b52011-03-09 16:28:56 -0800131static int tegra_ehci_hub_control(
132 struct usb_hcd *hcd,
133 u16 typeReq,
134 u16 wValue,
135 u16 wIndex,
136 char *buf,
137 u16 wLength
138)
139{
140 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
141 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
142 u32 __iomem *status_reg;
143 u32 temp;
144 unsigned long flags;
145 int retval = 0;
146
147 status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
148
149 spin_lock_irqsave(&ehci->lock, flags);
150
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600151 if (typeReq == GetPortStatus) {
Benoit Goby79ad3b52011-03-09 16:28:56 -0800152 temp = ehci_readl(ehci, status_reg);
153 if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
154 /* Resume completed, re-enable disconnect detection */
155 tegra->port_resuming = 0;
156 tegra_usb_phy_postresume(tegra->phy);
157 }
158 }
159
160 else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
161 temp = ehci_readl(ehci, status_reg);
162 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
163 retval = -EPIPE;
164 goto done;
165 }
166
Stephen Warrenb0876572012-04-25 12:31:10 -0600167 temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800168 temp |= PORT_WKDISC_E | PORT_WKOC_E;
169 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
170
171 /*
172 * If a transaction is in progress, there may be a delay in
173 * suspending the port. Poll until the port is suspended.
174 */
175 if (handshake(ehci, status_reg, PORT_SUSPEND,
176 PORT_SUSPEND, 5000))
177 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
178
179 set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
180 goto done;
181 }
182
Jim Lin1f594b62011-04-17 11:58:25 +0300183 /* For USB1 port we need to issue Port Reset twice internally */
184 if (tegra->phy->instance == 0 &&
185 (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
186 spin_unlock_irqrestore(&ehci->lock, flags);
187 return tegra_ehci_internal_port_reset(ehci, status_reg);
188 }
189
Benoit Goby79ad3b52011-03-09 16:28:56 -0800190 /*
191 * Tegra host controller will time the resume operation to clear the bit
192 * when the port control state switches to HS or FS Idle. This behavior
193 * is different from EHCI where the host controller driver is required
194 * to set this bit to a zero after the resume duration is timed in the
195 * driver.
196 */
197 else if (typeReq == ClearPortFeature &&
198 wValue == USB_PORT_FEAT_SUSPEND) {
199 temp = ehci_readl(ehci, status_reg);
200 if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
201 retval = -EPIPE;
202 goto done;
203 }
204
205 if (!(temp & PORT_SUSPEND))
206 goto done;
207
208 /* Disable disconnect detection during port resume */
209 tegra_usb_phy_preresume(tegra->phy);
210
211 ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
212
213 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
214 /* start resume signalling */
215 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
Alan Sterna448e4d2012-04-03 15:24:30 -0400216 set_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800217
218 spin_unlock_irqrestore(&ehci->lock, flags);
219 msleep(20);
220 spin_lock_irqsave(&ehci->lock, flags);
221
222 /* Poll until the controller clears RESUME and SUSPEND */
223 if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
224 pr_err("%s: timeout waiting for RESUME\n", __func__);
225 if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
226 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
227
228 ehci->reset_done[wIndex-1] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400229 clear_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800230
231 tegra->port_resuming = 1;
232 goto done;
233 }
234
235 spin_unlock_irqrestore(&ehci->lock, flags);
236
237 /* Handle the hub control events here */
238 return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
239done:
240 spin_unlock_irqrestore(&ehci->lock, flags);
241 return retval;
242}
243
244static void tegra_ehci_restart(struct usb_hcd *hcd)
245{
246 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
247
248 ehci_reset(ehci);
249
250 /* setup the frame list and Async q heads */
251 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
252 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
253 /* setup the command register and set the controller in RUN mode */
254 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
255 ehci->command |= CMD_RUN;
256 ehci_writel(ehci, ehci->command, &ehci->regs->command);
257
258 down_write(&ehci_cf_port_reset_rwsem);
259 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
260 /* flush posted writes */
261 ehci_readl(ehci, &ehci->regs->command);
262 up_write(&ehci_cf_port_reset_rwsem);
263}
264
Benoit Goby79ad3b52011-03-09 16:28:56 -0800265static void tegra_ehci_shutdown(struct usb_hcd *hcd)
266{
267 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
268
269 /* ehci_shutdown touches the USB controller registers, make sure
270 * controller has clocks to it */
271 if (!tegra->host_resumed)
272 tegra_ehci_power_up(hcd);
273
274 ehci_shutdown(hcd);
275}
276
277static int tegra_ehci_setup(struct usb_hcd *hcd)
278{
279 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
280 int retval;
281
282 /* EHCI registers start at offset 0x100 */
283 ehci->caps = hcd->regs + 0x100;
284 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200285 HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase));
Benoit Goby79ad3b52011-03-09 16:28:56 -0800286
287 dbg_hcs_params(ehci, "reset");
288 dbg_hcc_params(ehci, "reset");
289
290 /* cache this readonly data; minimize chip reads */
291 ehci->hcs_params = readl(&ehci->caps->hcs_params);
292
293 /* switch to host mode */
294 hcd->has_tt = 1;
295 ehci_reset(ehci);
296
297 retval = ehci_halt(ehci);
298 if (retval)
299 return retval;
300
301 /* data structure init */
302 retval = ehci_init(hcd);
303 if (retval)
304 return retval;
305
306 ehci->sbrn = 0x20;
307
308 ehci_port_power(ehci, 1);
309 return retval;
310}
311
Venu Byravarasufe375772012-04-05 11:25:30 +0530312struct dma_aligned_buffer {
Robert Morellfbf98652011-03-09 16:28:57 -0800313 void *kmalloc_ptr;
314 void *old_xfer_buffer;
315 u8 data[0];
316};
317
Venu Byravarasufe375772012-04-05 11:25:30 +0530318static void free_dma_aligned_buffer(struct urb *urb)
Robert Morellfbf98652011-03-09 16:28:57 -0800319{
Venu Byravarasufe375772012-04-05 11:25:30 +0530320 struct dma_aligned_buffer *temp;
Robert Morellfbf98652011-03-09 16:28:57 -0800321
322 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
323 return;
324
Venu Byravarasufe375772012-04-05 11:25:30 +0530325 temp = container_of(urb->transfer_buffer,
326 struct dma_aligned_buffer, data);
Robert Morellfbf98652011-03-09 16:28:57 -0800327
Venu Byravarasufe375772012-04-05 11:25:30 +0530328 if (usb_urb_dir_in(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800329 memcpy(temp->old_xfer_buffer, temp->data,
330 urb->transfer_buffer_length);
331 urb->transfer_buffer = temp->old_xfer_buffer;
332 kfree(temp->kmalloc_ptr);
333
334 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
335}
336
Venu Byravarasufe375772012-04-05 11:25:30 +0530337static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
Robert Morellfbf98652011-03-09 16:28:57 -0800338{
Venu Byravarasufe375772012-04-05 11:25:30 +0530339 struct dma_aligned_buffer *temp, *kmalloc_ptr;
Robert Morellfbf98652011-03-09 16:28:57 -0800340 size_t kmalloc_size;
341
342 if (urb->num_sgs || urb->sg ||
343 urb->transfer_buffer_length == 0 ||
344 !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
345 return 0;
346
Robert Morellfbf98652011-03-09 16:28:57 -0800347 /* Allocate a buffer with enough padding for alignment */
348 kmalloc_size = urb->transfer_buffer_length +
Venu Byravarasufe375772012-04-05 11:25:30 +0530349 sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800350
351 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
352 if (!kmalloc_ptr)
353 return -ENOMEM;
354
Venu Byravarasufe375772012-04-05 11:25:30 +0530355 /* Position our struct dma_aligned_buffer such that data is aligned */
Robert Morellfbf98652011-03-09 16:28:57 -0800356 temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800357 temp->kmalloc_ptr = kmalloc_ptr;
358 temp->old_xfer_buffer = urb->transfer_buffer;
Venu Byravarasufe375772012-04-05 11:25:30 +0530359 if (usb_urb_dir_out(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800360 memcpy(temp->data, urb->transfer_buffer,
361 urb->transfer_buffer_length);
362 urb->transfer_buffer = temp->data;
363
364 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
365
366 return 0;
367}
368
369static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
370 gfp_t mem_flags)
371{
372 int ret;
373
Venu Byravarasufe375772012-04-05 11:25:30 +0530374 ret = alloc_dma_aligned_buffer(urb, mem_flags);
Robert Morellfbf98652011-03-09 16:28:57 -0800375 if (ret)
376 return ret;
377
378 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
379 if (ret)
Venu Byravarasufe375772012-04-05 11:25:30 +0530380 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800381
382 return ret;
383}
384
385static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
386{
387 usb_hcd_unmap_urb_for_dma(hcd, urb);
Venu Byravarasufe375772012-04-05 11:25:30 +0530388 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800389}
390
Benoit Goby79ad3b52011-03-09 16:28:56 -0800391static const struct hc_driver tegra_ehci_hc_driver = {
392 .description = hcd_name,
393 .product_desc = "Tegra EHCI Host Controller",
394 .hcd_priv_size = sizeof(struct ehci_hcd),
Benoit Goby79ad3b52011-03-09 16:28:56 -0800395 .flags = HCD_USB2 | HCD_MEMORY,
396
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530397 /* standard ehci functions */
Benoit Goby79ad3b52011-03-09 16:28:56 -0800398 .irq = ehci_irq,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800399 .start = ehci_run,
400 .stop = ehci_stop,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800401 .urb_enqueue = ehci_urb_enqueue,
402 .urb_dequeue = ehci_urb_dequeue,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800403 .endpoint_disable = ehci_endpoint_disable,
404 .endpoint_reset = ehci_endpoint_reset,
405 .get_frame_number = ehci_get_frame,
406 .hub_status_data = ehci_hub_status_data,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800407 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530408 .relinquish_port = ehci_relinquish_port,
409 .port_handed_over = ehci_port_handed_over,
410
411 /* modified ehci functions for tegra */
412 .reset = tegra_ehci_setup,
413 .shutdown = tegra_ehci_shutdown,
414 .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
415 .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
416 .hub_control = tegra_ehci_hub_control,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800417#ifdef CONFIG_PM
Alan Sternebf20de2012-05-01 11:28:49 -0400418 .bus_suspend = ehci_bus_suspend,
419 .bus_resume = ehci_bus_resume,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800420#endif
Benoit Goby79ad3b52011-03-09 16:28:56 -0800421};
422
Stephen Warren434103a2012-03-16 16:06:07 -0600423static int setup_vbus_gpio(struct platform_device *pdev,
424 struct tegra_ehci_platform_data *pdata)
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000425{
426 int err = 0;
427 int gpio;
428
Stephen Warren434103a2012-03-16 16:06:07 -0600429 gpio = pdata->vbus_gpio;
430 if (!gpio_is_valid(gpio))
431 gpio = of_get_named_gpio(pdev->dev.of_node,
432 "nvidia,vbus-gpio", 0);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000433 if (!gpio_is_valid(gpio))
434 return 0;
435
436 err = gpio_request(gpio, "vbus_gpio");
437 if (err) {
438 dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
439 return err;
440 }
441 err = gpio_direction_output(gpio, 1);
442 if (err) {
443 dev_err(&pdev->dev, "can't enable vbus\n");
444 return err;
445 }
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000446
447 return err;
448}
449
Alan Sternebf20de2012-05-01 11:28:49 -0400450#ifdef CONFIG_PM
451
452static int controller_suspend(struct device *dev)
453{
454 struct tegra_ehci_hcd *tegra =
455 platform_get_drvdata(to_platform_device(dev));
456 struct ehci_hcd *ehci = tegra->ehci;
457 struct usb_hcd *hcd = ehci_to_hcd(ehci);
458 struct ehci_regs __iomem *hw = ehci->regs;
459 unsigned long flags;
460
461 if (time_before(jiffies, ehci->next_statechange))
462 msleep(10);
463
464 spin_lock_irqsave(&ehci->lock, flags);
465
466 tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
467 ehci_halt(ehci);
468 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
469
470 spin_unlock_irqrestore(&ehci->lock, flags);
471
472 tegra_ehci_power_down(hcd);
473 return 0;
474}
475
476static int controller_resume(struct device *dev)
477{
478 struct tegra_ehci_hcd *tegra =
479 platform_get_drvdata(to_platform_device(dev));
480 struct ehci_hcd *ehci = tegra->ehci;
481 struct usb_hcd *hcd = ehci_to_hcd(ehci);
482 struct ehci_regs __iomem *hw = ehci->regs;
483 unsigned long val;
484
485 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
486 tegra_ehci_power_up(hcd);
487
488 if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
489 /* Wait for the phy to detect new devices
490 * before we restart the controller */
491 msleep(10);
492 goto restart;
493 }
494
495 /* Force the phy to keep data lines in suspend state */
496 tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
497
498 /* Enable host mode */
499 tdi_reset(ehci);
500
501 /* Enable Port Power */
502 val = readl(&hw->port_status[0]);
503 val |= PORT_POWER;
504 writel(val, &hw->port_status[0]);
505 udelay(10);
506
507 /* Check if the phy resume from LP0. When the phy resume from LP0
508 * USB register will be reset. */
509 if (!readl(&hw->async_next)) {
510 /* Program the field PTC based on the saved speed mode */
511 val = readl(&hw->port_status[0]);
512 val &= ~PORT_TEST(~0);
513 if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
514 val |= PORT_TEST_FORCE;
515 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
516 val |= PORT_TEST(6);
517 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
518 val |= PORT_TEST(7);
519 writel(val, &hw->port_status[0]);
520 udelay(10);
521
522 /* Disable test mode by setting PTC field to NORMAL_OP */
523 val = readl(&hw->port_status[0]);
524 val &= ~PORT_TEST(~0);
525 writel(val, &hw->port_status[0]);
526 udelay(10);
527 }
528
529 /* Poll until CCS is enabled */
530 if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
531 PORT_CONNECT, 2000)) {
532 pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
533 goto restart;
534 }
535
536 /* Poll until PE is enabled */
537 if (handshake(ehci, &hw->port_status[0], PORT_PE,
538 PORT_PE, 2000)) {
539 pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
540 goto restart;
541 }
542
543 /* Clear the PCI status, to avoid an interrupt taken upon resume */
544 val = readl(&hw->status);
545 val |= STS_PCD;
546 writel(val, &hw->status);
547
548 /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
549 val = readl(&hw->port_status[0]);
550 if ((val & PORT_POWER) && (val & PORT_PE)) {
551 val |= PORT_SUSPEND;
552 writel(val, &hw->port_status[0]);
553
554 /* Wait until port suspend completes */
555 if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
556 PORT_SUSPEND, 1000)) {
557 pr_err("%s: timeout waiting for PORT_SUSPEND\n",
558 __func__);
559 goto restart;
560 }
561 }
562
563 tegra_ehci_phy_restore_end(tegra->phy);
564 goto done;
565
566 restart:
567 if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
568 tegra_ehci_phy_restore_end(tegra->phy);
569
570 tegra_ehci_restart(hcd);
571
572 done:
573 tegra_usb_phy_preresume(tegra->phy);
574 tegra->port_resuming = 1;
575 return 0;
576}
577
578static int tegra_ehci_suspend(struct device *dev)
579{
580 struct tegra_ehci_hcd *tegra =
581 platform_get_drvdata(to_platform_device(dev));
582 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
583 int rc = 0;
584
585 /*
586 * When system sleep is supported and USB controller wakeup is
587 * implemented: If the controller is runtime-suspended and the
588 * wakeup setting needs to be changed, call pm_runtime_resume().
589 */
590 if (HCD_HW_ACCESSIBLE(hcd))
591 rc = controller_suspend(dev);
592 return rc;
593}
594
595static int tegra_ehci_resume(struct device *dev)
596{
597 int rc;
598
599 rc = controller_resume(dev);
600 if (rc == 0) {
601 pm_runtime_disable(dev);
602 pm_runtime_set_active(dev);
603 pm_runtime_enable(dev);
604 }
605 return rc;
606}
607
608static int tegra_ehci_runtime_suspend(struct device *dev)
609{
610 return controller_suspend(dev);
611}
612
613static int tegra_ehci_runtime_resume(struct device *dev)
614{
615 return controller_resume(dev);
616}
617
618static const struct dev_pm_ops tegra_ehci_pm_ops = {
619 .suspend = tegra_ehci_suspend,
620 .resume = tegra_ehci_resume,
621 .runtime_suspend = tegra_ehci_runtime_suspend,
622 .runtime_resume = tegra_ehci_runtime_resume,
623};
624
625#endif
626
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000627static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
628
Benoit Goby79ad3b52011-03-09 16:28:56 -0800629static int tegra_ehci_probe(struct platform_device *pdev)
630{
631 struct resource *res;
632 struct usb_hcd *hcd;
633 struct tegra_ehci_hcd *tegra;
634 struct tegra_ehci_platform_data *pdata;
635 int err = 0;
636 int irq;
637 int instance = pdev->id;
638
639 pdata = pdev->dev.platform_data;
640 if (!pdata) {
641 dev_err(&pdev->dev, "Platform data missing\n");
642 return -EINVAL;
643 }
644
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000645 /* Right now device-tree probed devices don't get dma_mask set.
646 * Since shared usb code relies on it, set it here for now.
647 * Once we have dma capability bindings this can go away.
648 */
649 if (!pdev->dev.dma_mask)
650 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
651
Stephen Warren434103a2012-03-16 16:06:07 -0600652 setup_vbus_gpio(pdev, pdata);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000653
Benoit Goby79ad3b52011-03-09 16:28:56 -0800654 tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL);
655 if (!tegra)
656 return -ENOMEM;
657
658 hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
659 dev_name(&pdev->dev));
660 if (!hcd) {
661 dev_err(&pdev->dev, "Unable to create HCD\n");
662 err = -ENOMEM;
663 goto fail_hcd;
664 }
665
666 platform_set_drvdata(pdev, tegra);
667
668 tegra->clk = clk_get(&pdev->dev, NULL);
669 if (IS_ERR(tegra->clk)) {
670 dev_err(&pdev->dev, "Can't get ehci clock\n");
671 err = PTR_ERR(tegra->clk);
672 goto fail_clk;
673 }
674
675 err = clk_enable(tegra->clk);
676 if (err)
677 goto fail_clken;
678
679 tegra->emc_clk = clk_get(&pdev->dev, "emc");
680 if (IS_ERR(tegra->emc_clk)) {
681 dev_err(&pdev->dev, "Can't get emc clock\n");
682 err = PTR_ERR(tegra->emc_clk);
683 goto fail_emc_clk;
684 }
685
686 clk_enable(tegra->emc_clk);
687 clk_set_rate(tegra->emc_clk, 400000000);
688
689 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
690 if (!res) {
691 dev_err(&pdev->dev, "Failed to get I/O memory\n");
692 err = -ENXIO;
693 goto fail_io;
694 }
695 hcd->rsrc_start = res->start;
696 hcd->rsrc_len = resource_size(res);
697 hcd->regs = ioremap(res->start, resource_size(res));
698 if (!hcd->regs) {
699 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
700 err = -ENOMEM;
701 goto fail_io;
702 }
703
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000704 /* This is pretty ugly and needs to be fixed when we do only
705 * device-tree probing. Old code relies on the platform_device
706 * numbering that we lack for device-tree-instantiated devices.
707 */
708 if (instance < 0) {
709 switch (res->start) {
710 case TEGRA_USB_BASE:
711 instance = 0;
712 break;
713 case TEGRA_USB2_BASE:
714 instance = 1;
715 break;
716 case TEGRA_USB3_BASE:
717 instance = 2;
718 break;
719 default:
720 err = -ENODEV;
721 dev_err(&pdev->dev, "unknown usb instance\n");
722 goto fail_phy;
723 }
724 }
725
Stephen Warrenaa607eb2012-04-12 15:46:49 -0600726 tegra->phy = tegra_usb_phy_open(&pdev->dev, instance, hcd->regs,
727 pdata->phy_config,
728 TEGRA_USB_PHY_MODE_HOST);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800729 if (IS_ERR(tegra->phy)) {
730 dev_err(&pdev->dev, "Failed to open USB phy\n");
731 err = -ENXIO;
732 goto fail_phy;
733 }
734
735 err = tegra_usb_phy_power_on(tegra->phy);
736 if (err) {
737 dev_err(&pdev->dev, "Failed to power on the phy\n");
738 goto fail;
739 }
740
741 tegra->host_resumed = 1;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800742 tegra->ehci = hcd_to_ehci(hcd);
743
744 irq = platform_get_irq(pdev, 0);
745 if (!irq) {
746 dev_err(&pdev->dev, "Failed to get IRQ\n");
747 err = -ENODEV;
748 goto fail;
749 }
Benoit Goby79ad3b52011-03-09 16:28:56 -0800750
751#ifdef CONFIG_USB_OTG_UTILS
752 if (pdata->operating_mode == TEGRA_USB_OTG) {
Kishon Vijay Abraham I662dca52012-06-22 17:02:46 +0530753 tegra->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530754 if (!IS_ERR_OR_NULL(tegra->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200755 otg_set_host(tegra->transceiver->otg, &hcd->self);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800756 }
757#endif
758
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800759 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800760 if (err) {
761 dev_err(&pdev->dev, "Failed to add USB HCD\n");
762 goto fail;
763 }
764
Alan Sternebf20de2012-05-01 11:28:49 -0400765 pm_runtime_set_active(&pdev->dev);
766 pm_runtime_get_noresume(&pdev->dev);
767
768 /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
769 /* if (!pdata->power_down_on_bus_suspend) */
770 pm_runtime_forbid(&pdev->dev);
771 pm_runtime_enable(&pdev->dev);
772 pm_runtime_put_sync(&pdev->dev);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800773 return err;
774
775fail:
776#ifdef CONFIG_USB_OTG_UTILS
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530777 if (!IS_ERR_OR_NULL(tegra->transceiver)) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200778 otg_set_host(tegra->transceiver->otg, NULL);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530779 usb_put_phy(tegra->transceiver);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800780 }
781#endif
782 tegra_usb_phy_close(tegra->phy);
783fail_phy:
784 iounmap(hcd->regs);
785fail_io:
786 clk_disable(tegra->emc_clk);
787 clk_put(tegra->emc_clk);
788fail_emc_clk:
789 clk_disable(tegra->clk);
790fail_clken:
791 clk_put(tegra->clk);
792fail_clk:
793 usb_put_hcd(hcd);
794fail_hcd:
795 kfree(tegra);
796 return err;
797}
798
Benoit Goby79ad3b52011-03-09 16:28:56 -0800799static int tegra_ehci_remove(struct platform_device *pdev)
800{
801 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
802 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
803
804 if (tegra == NULL || hcd == NULL)
805 return -EINVAL;
806
Alan Sternebf20de2012-05-01 11:28:49 -0400807 pm_runtime_get_sync(&pdev->dev);
808 pm_runtime_disable(&pdev->dev);
809 pm_runtime_put_noidle(&pdev->dev);
810
Benoit Goby79ad3b52011-03-09 16:28:56 -0800811#ifdef CONFIG_USB_OTG_UTILS
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530812 if (!IS_ERR_OR_NULL(tegra->transceiver)) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200813 otg_set_host(tegra->transceiver->otg, NULL);
Kishon Vijay Abraham I721002e2012-06-22 17:02:45 +0530814 usb_put_phy(tegra->transceiver);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800815 }
816#endif
817
818 usb_remove_hcd(hcd);
819 usb_put_hcd(hcd);
820
821 tegra_usb_phy_close(tegra->phy);
822 iounmap(hcd->regs);
823
824 clk_disable(tegra->clk);
825 clk_put(tegra->clk);
826
827 clk_disable(tegra->emc_clk);
828 clk_put(tegra->emc_clk);
829
830 kfree(tegra);
831 return 0;
832}
833
834static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
835{
836 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
837 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
838
839 if (hcd->driver->shutdown)
840 hcd->driver->shutdown(hcd);
841}
842
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000843static struct of_device_id tegra_ehci_of_match[] __devinitdata = {
844 { .compatible = "nvidia,tegra20-ehci", },
845 { },
846};
847
Benoit Goby79ad3b52011-03-09 16:28:56 -0800848static struct platform_driver tegra_ehci_driver = {
849 .probe = tegra_ehci_probe,
850 .remove = tegra_ehci_remove,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800851 .shutdown = tegra_ehci_hcd_shutdown,
852 .driver = {
853 .name = "tegra-ehci",
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000854 .of_match_table = tegra_ehci_of_match,
Alan Sternebf20de2012-05-01 11:28:49 -0400855#ifdef CONFIG_PM
856 .pm = &tegra_ehci_pm_ops,
857#endif
Benoit Goby79ad3b52011-03-09 16:28:56 -0800858 }
859};