blob: 55a9cdee2949f70cc5bbf8f74d594fbf2cb4d2f9 [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
Venu Byravarasu1ba82162012-09-05 18:50:23 +053030#include <linux/usb/tegra_usb_phy.h>
Stephen Warren54388b22012-10-02 16:49:25 -060031
32#define TEGRA_USB_BASE 0xC5000000
33#define TEGRA_USB2_BASE 0xC5004000
34#define TEGRA_USB3_BASE 0xC5008000
Benoit Goby79ad3b52011-03-09 16:28:56 -080035
Robert Morellfbf98652011-03-09 16:28:57 -080036#define TEGRA_USB_DMA_ALIGN 32
37
Benoit Goby79ad3b52011-03-09 16:28:56 -080038struct tegra_ehci_hcd {
39 struct ehci_hcd *ehci;
40 struct tegra_usb_phy *phy;
41 struct clk *clk;
42 struct clk *emc_clk;
Heikki Krogerus86753812012-02-13 13:24:02 +020043 struct usb_phy *transceiver;
Benoit Goby79ad3b52011-03-09 16:28:56 -080044 int host_resumed;
Benoit Goby79ad3b52011-03-09 16:28:56 -080045 int port_resuming;
Venu Byravarasu585355c2012-12-13 20:59:08 +000046 bool needs_double_reset;
Benoit Goby79ad3b52011-03-09 16:28:56 -080047 enum tegra_usb_phy_port_speed port_speed;
48};
49
50static void tegra_ehci_power_up(struct usb_hcd *hcd)
51{
52 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
53
Prashant Gaikwad20de12c2012-06-05 09:59:38 +053054 clk_prepare_enable(tegra->emc_clk);
55 clk_prepare_enable(tegra->clk);
Venu Byravarasu1ba82162012-09-05 18:50:23 +053056 usb_phy_set_suspend(&tegra->phy->u_phy, 0);
Benoit Goby79ad3b52011-03-09 16:28:56 -080057 tegra->host_resumed = 1;
58}
59
60static void tegra_ehci_power_down(struct usb_hcd *hcd)
61{
62 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
63
64 tegra->host_resumed = 0;
Venu Byravarasu1ba82162012-09-05 18:50:23 +053065 usb_phy_set_suspend(&tegra->phy->u_phy, 1);
Prashant Gaikwad20de12c2012-06-05 09:59:38 +053066 clk_disable_unprepare(tegra->clk);
67 clk_disable_unprepare(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -080068}
69
Jim Lin1f594b62011-04-17 11:58:25 +030070static int tegra_ehci_internal_port_reset(
71 struct ehci_hcd *ehci,
72 u32 __iomem *portsc_reg
73)
74{
75 u32 temp;
76 unsigned long flags;
77 int retval = 0;
78 int i, tries;
79 u32 saved_usbintr;
80
81 spin_lock_irqsave(&ehci->lock, flags);
82 saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
83 /* disable USB interrupt */
84 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
85 spin_unlock_irqrestore(&ehci->lock, flags);
86
87 /*
88 * Here we have to do Port Reset at most twice for
89 * Port Enable bit to be set.
90 */
91 for (i = 0; i < 2; i++) {
92 temp = ehci_readl(ehci, portsc_reg);
93 temp |= PORT_RESET;
94 ehci_writel(ehci, temp, portsc_reg);
95 mdelay(10);
96 temp &= ~PORT_RESET;
97 ehci_writel(ehci, temp, portsc_reg);
98 mdelay(1);
99 tries = 100;
100 do {
101 mdelay(1);
102 /*
103 * Up to this point, Port Enable bit is
104 * expected to be set after 2 ms waiting.
105 * USB1 usually takes extra 45 ms, for safety,
106 * we take 100 ms as timeout.
107 */
108 temp = ehci_readl(ehci, portsc_reg);
109 } while (!(temp & PORT_PE) && tries--);
110 if (temp & PORT_PE)
111 break;
112 }
113 if (i == 2)
114 retval = -ETIMEDOUT;
115
116 /*
117 * Clear Connect Status Change bit if it's set.
118 * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
119 */
120 if (temp & PORT_CSC)
121 ehci_writel(ehci, PORT_CSC, portsc_reg);
122
123 /*
124 * Write to clear any interrupt status bits that might be set
125 * during port reset.
126 */
127 temp = ehci_readl(ehci, &ehci->regs->status);
128 ehci_writel(ehci, temp, &ehci->regs->status);
129
130 /* restore original interrupt enable bits */
131 ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
132 return retval;
133}
134
Benoit Goby79ad3b52011-03-09 16:28:56 -0800135static int tegra_ehci_hub_control(
136 struct usb_hcd *hcd,
137 u16 typeReq,
138 u16 wValue,
139 u16 wIndex,
140 char *buf,
141 u16 wLength
142)
143{
144 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
145 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
146 u32 __iomem *status_reg;
147 u32 temp;
148 unsigned long flags;
149 int retval = 0;
150
151 status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
152
153 spin_lock_irqsave(&ehci->lock, flags);
154
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600155 if (typeReq == GetPortStatus) {
Benoit Goby79ad3b52011-03-09 16:28:56 -0800156 temp = ehci_readl(ehci, status_reg);
157 if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
158 /* Resume completed, re-enable disconnect detection */
159 tegra->port_resuming = 0;
160 tegra_usb_phy_postresume(tegra->phy);
161 }
162 }
163
164 else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
165 temp = ehci_readl(ehci, status_reg);
166 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
167 retval = -EPIPE;
168 goto done;
169 }
170
Stephen Warrenb0876572012-04-25 12:31:10 -0600171 temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800172 temp |= PORT_WKDISC_E | PORT_WKOC_E;
173 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
174
175 /*
176 * If a transaction is in progress, there may be a delay in
177 * suspending the port. Poll until the port is suspended.
178 */
179 if (handshake(ehci, status_reg, PORT_SUSPEND,
180 PORT_SUSPEND, 5000))
181 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
182
183 set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
184 goto done;
185 }
186
Jim Lin1f594b62011-04-17 11:58:25 +0300187 /* For USB1 port we need to issue Port Reset twice internally */
Venu Byravarasu585355c2012-12-13 20:59:08 +0000188 if (tegra->needs_double_reset &&
Jim Lin1f594b62011-04-17 11:58:25 +0300189 (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
190 spin_unlock_irqrestore(&ehci->lock, flags);
191 return tegra_ehci_internal_port_reset(ehci, status_reg);
192 }
193
Benoit Goby79ad3b52011-03-09 16:28:56 -0800194 /*
195 * Tegra host controller will time the resume operation to clear the bit
196 * when the port control state switches to HS or FS Idle. This behavior
197 * is different from EHCI where the host controller driver is required
198 * to set this bit to a zero after the resume duration is timed in the
199 * driver.
200 */
201 else if (typeReq == ClearPortFeature &&
202 wValue == USB_PORT_FEAT_SUSPEND) {
203 temp = ehci_readl(ehci, status_reg);
204 if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
205 retval = -EPIPE;
206 goto done;
207 }
208
209 if (!(temp & PORT_SUSPEND))
210 goto done;
211
212 /* Disable disconnect detection during port resume */
213 tegra_usb_phy_preresume(tegra->phy);
214
215 ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
216
217 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
218 /* start resume signalling */
219 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
Alan Sterna448e4d2012-04-03 15:24:30 -0400220 set_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800221
222 spin_unlock_irqrestore(&ehci->lock, flags);
223 msleep(20);
224 spin_lock_irqsave(&ehci->lock, flags);
225
226 /* Poll until the controller clears RESUME and SUSPEND */
227 if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
228 pr_err("%s: timeout waiting for RESUME\n", __func__);
229 if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
230 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
231
232 ehci->reset_done[wIndex-1] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400233 clear_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800234
235 tegra->port_resuming = 1;
236 goto done;
237 }
238
239 spin_unlock_irqrestore(&ehci->lock, flags);
240
241 /* Handle the hub control events here */
242 return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
243done:
244 spin_unlock_irqrestore(&ehci->lock, flags);
245 return retval;
246}
247
248static void tegra_ehci_restart(struct usb_hcd *hcd)
249{
250 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
251
252 ehci_reset(ehci);
253
254 /* setup the frame list and Async q heads */
255 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
256 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
257 /* setup the command register and set the controller in RUN mode */
258 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
259 ehci->command |= CMD_RUN;
260 ehci_writel(ehci, ehci->command, &ehci->regs->command);
261
262 down_write(&ehci_cf_port_reset_rwsem);
263 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
264 /* flush posted writes */
265 ehci_readl(ehci, &ehci->regs->command);
266 up_write(&ehci_cf_port_reset_rwsem);
267}
268
Benoit Goby79ad3b52011-03-09 16:28:56 -0800269static void tegra_ehci_shutdown(struct usb_hcd *hcd)
270{
271 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
272
273 /* ehci_shutdown touches the USB controller registers, make sure
274 * controller has clocks to it */
275 if (!tegra->host_resumed)
276 tegra_ehci_power_up(hcd);
277
278 ehci_shutdown(hcd);
279}
280
281static int tegra_ehci_setup(struct usb_hcd *hcd)
282{
283 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800284
285 /* EHCI registers start at offset 0x100 */
286 ehci->caps = hcd->regs + 0x100;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800287
288 /* switch to host mode */
289 hcd->has_tt = 1;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800290
Alan Sternc73cee72012-10-31 13:21:06 -0400291 return ehci_setup(hcd);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800292}
293
Venu Byravarasufe375772012-04-05 11:25:30 +0530294struct dma_aligned_buffer {
Robert Morellfbf98652011-03-09 16:28:57 -0800295 void *kmalloc_ptr;
296 void *old_xfer_buffer;
297 u8 data[0];
298};
299
Venu Byravarasufe375772012-04-05 11:25:30 +0530300static void free_dma_aligned_buffer(struct urb *urb)
Robert Morellfbf98652011-03-09 16:28:57 -0800301{
Venu Byravarasufe375772012-04-05 11:25:30 +0530302 struct dma_aligned_buffer *temp;
Robert Morellfbf98652011-03-09 16:28:57 -0800303
304 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
305 return;
306
Venu Byravarasufe375772012-04-05 11:25:30 +0530307 temp = container_of(urb->transfer_buffer,
308 struct dma_aligned_buffer, data);
Robert Morellfbf98652011-03-09 16:28:57 -0800309
Venu Byravarasufe375772012-04-05 11:25:30 +0530310 if (usb_urb_dir_in(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800311 memcpy(temp->old_xfer_buffer, temp->data,
312 urb->transfer_buffer_length);
313 urb->transfer_buffer = temp->old_xfer_buffer;
314 kfree(temp->kmalloc_ptr);
315
316 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
317}
318
Venu Byravarasufe375772012-04-05 11:25:30 +0530319static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
Robert Morellfbf98652011-03-09 16:28:57 -0800320{
Venu Byravarasufe375772012-04-05 11:25:30 +0530321 struct dma_aligned_buffer *temp, *kmalloc_ptr;
Robert Morellfbf98652011-03-09 16:28:57 -0800322 size_t kmalloc_size;
323
324 if (urb->num_sgs || urb->sg ||
325 urb->transfer_buffer_length == 0 ||
326 !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
327 return 0;
328
Robert Morellfbf98652011-03-09 16:28:57 -0800329 /* Allocate a buffer with enough padding for alignment */
330 kmalloc_size = urb->transfer_buffer_length +
Venu Byravarasufe375772012-04-05 11:25:30 +0530331 sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800332
333 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
334 if (!kmalloc_ptr)
335 return -ENOMEM;
336
Venu Byravarasufe375772012-04-05 11:25:30 +0530337 /* Position our struct dma_aligned_buffer such that data is aligned */
Robert Morellfbf98652011-03-09 16:28:57 -0800338 temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800339 temp->kmalloc_ptr = kmalloc_ptr;
340 temp->old_xfer_buffer = urb->transfer_buffer;
Venu Byravarasufe375772012-04-05 11:25:30 +0530341 if (usb_urb_dir_out(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800342 memcpy(temp->data, urb->transfer_buffer,
343 urb->transfer_buffer_length);
344 urb->transfer_buffer = temp->data;
345
346 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
347
348 return 0;
349}
350
351static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
352 gfp_t mem_flags)
353{
354 int ret;
355
Venu Byravarasufe375772012-04-05 11:25:30 +0530356 ret = alloc_dma_aligned_buffer(urb, mem_flags);
Robert Morellfbf98652011-03-09 16:28:57 -0800357 if (ret)
358 return ret;
359
360 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
361 if (ret)
Venu Byravarasufe375772012-04-05 11:25:30 +0530362 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800363
364 return ret;
365}
366
367static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
368{
369 usb_hcd_unmap_urb_for_dma(hcd, urb);
Venu Byravarasufe375772012-04-05 11:25:30 +0530370 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800371}
372
Benoit Goby79ad3b52011-03-09 16:28:56 -0800373static const struct hc_driver tegra_ehci_hc_driver = {
374 .description = hcd_name,
375 .product_desc = "Tegra EHCI Host Controller",
376 .hcd_priv_size = sizeof(struct ehci_hcd),
Benoit Goby79ad3b52011-03-09 16:28:56 -0800377 .flags = HCD_USB2 | HCD_MEMORY,
378
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530379 /* standard ehci functions */
Benoit Goby79ad3b52011-03-09 16:28:56 -0800380 .irq = ehci_irq,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800381 .start = ehci_run,
382 .stop = ehci_stop,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800383 .urb_enqueue = ehci_urb_enqueue,
384 .urb_dequeue = ehci_urb_dequeue,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800385 .endpoint_disable = ehci_endpoint_disable,
386 .endpoint_reset = ehci_endpoint_reset,
387 .get_frame_number = ehci_get_frame,
388 .hub_status_data = ehci_hub_status_data,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800389 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530390 .relinquish_port = ehci_relinquish_port,
391 .port_handed_over = ehci_port_handed_over,
392
393 /* modified ehci functions for tegra */
394 .reset = tegra_ehci_setup,
395 .shutdown = tegra_ehci_shutdown,
396 .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
397 .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
398 .hub_control = tegra_ehci_hub_control,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800399#ifdef CONFIG_PM
Alan Sternebf20de2012-05-01 11:28:49 -0400400 .bus_suspend = ehci_bus_suspend,
401 .bus_resume = ehci_bus_resume,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800402#endif
Benoit Goby79ad3b52011-03-09 16:28:56 -0800403};
404
Stephen Warren434103a2012-03-16 16:06:07 -0600405static int setup_vbus_gpio(struct platform_device *pdev,
406 struct tegra_ehci_platform_data *pdata)
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000407{
408 int err = 0;
409 int gpio;
410
Stephen Warren434103a2012-03-16 16:06:07 -0600411 gpio = pdata->vbus_gpio;
412 if (!gpio_is_valid(gpio))
413 gpio = of_get_named_gpio(pdev->dev.of_node,
414 "nvidia,vbus-gpio", 0);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000415 if (!gpio_is_valid(gpio))
416 return 0;
417
418 err = gpio_request(gpio, "vbus_gpio");
419 if (err) {
420 dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
421 return err;
422 }
423 err = gpio_direction_output(gpio, 1);
424 if (err) {
425 dev_err(&pdev->dev, "can't enable vbus\n");
426 return err;
427 }
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000428
429 return err;
430}
431
Alan Sternebf20de2012-05-01 11:28:49 -0400432#ifdef CONFIG_PM
433
434static int controller_suspend(struct device *dev)
435{
436 struct tegra_ehci_hcd *tegra =
437 platform_get_drvdata(to_platform_device(dev));
438 struct ehci_hcd *ehci = tegra->ehci;
439 struct usb_hcd *hcd = ehci_to_hcd(ehci);
440 struct ehci_regs __iomem *hw = ehci->regs;
441 unsigned long flags;
442
443 if (time_before(jiffies, ehci->next_statechange))
444 msleep(10);
445
Alan Sternebf20de2012-05-01 11:28:49 -0400446 ehci_halt(ehci);
Alan Sternebf20de2012-05-01 11:28:49 -0400447
Alan Sternc4f34762012-07-11 11:23:10 -0400448 spin_lock_irqsave(&ehci->lock, flags);
449 tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
450 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Sternebf20de2012-05-01 11:28:49 -0400451 spin_unlock_irqrestore(&ehci->lock, flags);
452
453 tegra_ehci_power_down(hcd);
454 return 0;
455}
456
457static int controller_resume(struct device *dev)
458{
459 struct tegra_ehci_hcd *tegra =
460 platform_get_drvdata(to_platform_device(dev));
461 struct ehci_hcd *ehci = tegra->ehci;
462 struct usb_hcd *hcd = ehci_to_hcd(ehci);
463 struct ehci_regs __iomem *hw = ehci->regs;
464 unsigned long val;
465
466 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
467 tegra_ehci_power_up(hcd);
468
469 if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
470 /* Wait for the phy to detect new devices
471 * before we restart the controller */
472 msleep(10);
473 goto restart;
474 }
475
476 /* Force the phy to keep data lines in suspend state */
477 tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
478
479 /* Enable host mode */
480 tdi_reset(ehci);
481
482 /* Enable Port Power */
483 val = readl(&hw->port_status[0]);
484 val |= PORT_POWER;
485 writel(val, &hw->port_status[0]);
486 udelay(10);
487
488 /* Check if the phy resume from LP0. When the phy resume from LP0
489 * USB register will be reset. */
490 if (!readl(&hw->async_next)) {
491 /* Program the field PTC based on the saved speed mode */
492 val = readl(&hw->port_status[0]);
493 val &= ~PORT_TEST(~0);
494 if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
495 val |= PORT_TEST_FORCE;
496 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
497 val |= PORT_TEST(6);
498 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
499 val |= PORT_TEST(7);
500 writel(val, &hw->port_status[0]);
501 udelay(10);
502
503 /* Disable test mode by setting PTC field to NORMAL_OP */
504 val = readl(&hw->port_status[0]);
505 val &= ~PORT_TEST(~0);
506 writel(val, &hw->port_status[0]);
507 udelay(10);
508 }
509
510 /* Poll until CCS is enabled */
511 if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
512 PORT_CONNECT, 2000)) {
513 pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
514 goto restart;
515 }
516
517 /* Poll until PE is enabled */
518 if (handshake(ehci, &hw->port_status[0], PORT_PE,
519 PORT_PE, 2000)) {
520 pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
521 goto restart;
522 }
523
524 /* Clear the PCI status, to avoid an interrupt taken upon resume */
525 val = readl(&hw->status);
526 val |= STS_PCD;
527 writel(val, &hw->status);
528
529 /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
530 val = readl(&hw->port_status[0]);
531 if ((val & PORT_POWER) && (val & PORT_PE)) {
532 val |= PORT_SUSPEND;
533 writel(val, &hw->port_status[0]);
534
535 /* Wait until port suspend completes */
536 if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
537 PORT_SUSPEND, 1000)) {
538 pr_err("%s: timeout waiting for PORT_SUSPEND\n",
539 __func__);
540 goto restart;
541 }
542 }
543
544 tegra_ehci_phy_restore_end(tegra->phy);
545 goto done;
546
547 restart:
548 if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
549 tegra_ehci_phy_restore_end(tegra->phy);
550
551 tegra_ehci_restart(hcd);
552
553 done:
554 tegra_usb_phy_preresume(tegra->phy);
555 tegra->port_resuming = 1;
556 return 0;
557}
558
559static int tegra_ehci_suspend(struct device *dev)
560{
561 struct tegra_ehci_hcd *tegra =
562 platform_get_drvdata(to_platform_device(dev));
563 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
564 int rc = 0;
565
566 /*
567 * When system sleep is supported and USB controller wakeup is
568 * implemented: If the controller is runtime-suspended and the
569 * wakeup setting needs to be changed, call pm_runtime_resume().
570 */
571 if (HCD_HW_ACCESSIBLE(hcd))
572 rc = controller_suspend(dev);
573 return rc;
574}
575
576static int tegra_ehci_resume(struct device *dev)
577{
578 int rc;
579
580 rc = controller_resume(dev);
581 if (rc == 0) {
582 pm_runtime_disable(dev);
583 pm_runtime_set_active(dev);
584 pm_runtime_enable(dev);
585 }
586 return rc;
587}
588
589static int tegra_ehci_runtime_suspend(struct device *dev)
590{
591 return controller_suspend(dev);
592}
593
594static int tegra_ehci_runtime_resume(struct device *dev)
595{
596 return controller_resume(dev);
597}
598
599static const struct dev_pm_ops tegra_ehci_pm_ops = {
600 .suspend = tegra_ehci_suspend,
601 .resume = tegra_ehci_resume,
602 .runtime_suspend = tegra_ehci_runtime_suspend,
603 .runtime_resume = tegra_ehci_runtime_resume,
604};
605
606#endif
607
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000608static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
609
Benoit Goby79ad3b52011-03-09 16:28:56 -0800610static int tegra_ehci_probe(struct platform_device *pdev)
611{
612 struct resource *res;
613 struct usb_hcd *hcd;
614 struct tegra_ehci_hcd *tegra;
615 struct tegra_ehci_platform_data *pdata;
616 int err = 0;
617 int irq;
618 int instance = pdev->id;
619
620 pdata = pdev->dev.platform_data;
621 if (!pdata) {
622 dev_err(&pdev->dev, "Platform data missing\n");
623 return -EINVAL;
624 }
625
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000626 /* Right now device-tree probed devices don't get dma_mask set.
627 * Since shared usb code relies on it, set it here for now.
628 * Once we have dma capability bindings this can go away.
629 */
630 if (!pdev->dev.dma_mask)
631 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
632
Stephen Warren434103a2012-03-16 16:06:07 -0600633 setup_vbus_gpio(pdev, pdata);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000634
Julia Lawallbc2ff982012-07-30 16:43:41 +0200635 tegra = devm_kzalloc(&pdev->dev, sizeof(struct tegra_ehci_hcd),
636 GFP_KERNEL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800637 if (!tegra)
638 return -ENOMEM;
639
640 hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
641 dev_name(&pdev->dev));
642 if (!hcd) {
643 dev_err(&pdev->dev, "Unable to create HCD\n");
Julia Lawallbc2ff982012-07-30 16:43:41 +0200644 return -ENOMEM;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800645 }
646
647 platform_set_drvdata(pdev, tegra);
648
Julia Lawallbc2ff982012-07-30 16:43:41 +0200649 tegra->clk = devm_clk_get(&pdev->dev, NULL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800650 if (IS_ERR(tegra->clk)) {
651 dev_err(&pdev->dev, "Can't get ehci clock\n");
652 err = PTR_ERR(tegra->clk);
653 goto fail_clk;
654 }
655
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530656 err = clk_prepare_enable(tegra->clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800657 if (err)
Julia Lawallbc2ff982012-07-30 16:43:41 +0200658 goto fail_clk;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800659
Julia Lawallbc2ff982012-07-30 16:43:41 +0200660 tegra->emc_clk = devm_clk_get(&pdev->dev, "emc");
Benoit Goby79ad3b52011-03-09 16:28:56 -0800661 if (IS_ERR(tegra->emc_clk)) {
662 dev_err(&pdev->dev, "Can't get emc clock\n");
663 err = PTR_ERR(tegra->emc_clk);
664 goto fail_emc_clk;
665 }
666
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530667 clk_prepare_enable(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800668 clk_set_rate(tegra->emc_clk, 400000000);
669
Venu Byravarasu585355c2012-12-13 20:59:08 +0000670 tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
671 "nvidia,needs-double-reset");
672
Benoit Goby79ad3b52011-03-09 16:28:56 -0800673 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
674 if (!res) {
675 dev_err(&pdev->dev, "Failed to get I/O memory\n");
676 err = -ENXIO;
677 goto fail_io;
678 }
679 hcd->rsrc_start = res->start;
680 hcd->rsrc_len = resource_size(res);
Julia Lawallbc2ff982012-07-30 16:43:41 +0200681 hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Benoit Goby79ad3b52011-03-09 16:28:56 -0800682 if (!hcd->regs) {
683 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
684 err = -ENOMEM;
685 goto fail_io;
686 }
687
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000688 /* This is pretty ugly and needs to be fixed when we do only
689 * device-tree probing. Old code relies on the platform_device
690 * numbering that we lack for device-tree-instantiated devices.
691 */
692 if (instance < 0) {
693 switch (res->start) {
694 case TEGRA_USB_BASE:
695 instance = 0;
696 break;
697 case TEGRA_USB2_BASE:
698 instance = 1;
699 break;
700 case TEGRA_USB3_BASE:
701 instance = 2;
702 break;
703 default:
704 err = -ENODEV;
705 dev_err(&pdev->dev, "unknown usb instance\n");
Julia Lawallbc2ff982012-07-30 16:43:41 +0200706 goto fail_io;
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000707 }
708 }
709
Stephen Warrenaa607eb2012-04-12 15:46:49 -0600710 tegra->phy = tegra_usb_phy_open(&pdev->dev, instance, hcd->regs,
711 pdata->phy_config,
712 TEGRA_USB_PHY_MODE_HOST);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800713 if (IS_ERR(tegra->phy)) {
714 dev_err(&pdev->dev, "Failed to open USB phy\n");
715 err = -ENXIO;
Julia Lawallbc2ff982012-07-30 16:43:41 +0200716 goto fail_io;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800717 }
718
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530719 usb_phy_init(&tegra->phy->u_phy);
720
721 err = usb_phy_set_suspend(&tegra->phy->u_phy, 0);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800722 if (err) {
723 dev_err(&pdev->dev, "Failed to power on the phy\n");
724 goto fail;
725 }
726
727 tegra->host_resumed = 1;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800728 tegra->ehci = hcd_to_ehci(hcd);
729
730 irq = platform_get_irq(pdev, 0);
731 if (!irq) {
732 dev_err(&pdev->dev, "Failed to get IRQ\n");
733 err = -ENODEV;
734 goto fail;
735 }
Benoit Goby79ad3b52011-03-09 16:28:56 -0800736
737#ifdef CONFIG_USB_OTG_UTILS
738 if (pdata->operating_mode == TEGRA_USB_OTG) {
Julia Lawallbc2ff982012-07-30 16:43:41 +0200739 tegra->transceiver =
740 devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530741 if (!IS_ERR_OR_NULL(tegra->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200742 otg_set_host(tegra->transceiver->otg, &hcd->self);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800743 }
744#endif
745
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800746 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800747 if (err) {
748 dev_err(&pdev->dev, "Failed to add USB HCD\n");
749 goto fail;
750 }
751
Alan Sternebf20de2012-05-01 11:28:49 -0400752 pm_runtime_set_active(&pdev->dev);
753 pm_runtime_get_noresume(&pdev->dev);
754
755 /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
756 /* if (!pdata->power_down_on_bus_suspend) */
757 pm_runtime_forbid(&pdev->dev);
758 pm_runtime_enable(&pdev->dev);
759 pm_runtime_put_sync(&pdev->dev);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800760 return err;
761
762fail:
763#ifdef CONFIG_USB_OTG_UTILS
Julia Lawallbc2ff982012-07-30 16:43:41 +0200764 if (!IS_ERR_OR_NULL(tegra->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200765 otg_set_host(tegra->transceiver->otg, NULL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800766#endif
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530767 usb_phy_shutdown(&tegra->phy->u_phy);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800768fail_io:
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530769 clk_disable_unprepare(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800770fail_emc_clk:
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530771 clk_disable_unprepare(tegra->clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800772fail_clk:
773 usb_put_hcd(hcd);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800774 return err;
775}
776
Benoit Goby79ad3b52011-03-09 16:28:56 -0800777static int tegra_ehci_remove(struct platform_device *pdev)
778{
779 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
780 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
781
Alan Sternebf20de2012-05-01 11:28:49 -0400782 pm_runtime_get_sync(&pdev->dev);
783 pm_runtime_disable(&pdev->dev);
784 pm_runtime_put_noidle(&pdev->dev);
785
Benoit Goby79ad3b52011-03-09 16:28:56 -0800786#ifdef CONFIG_USB_OTG_UTILS
Julia Lawallbc2ff982012-07-30 16:43:41 +0200787 if (!IS_ERR_OR_NULL(tegra->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200788 otg_set_host(tegra->transceiver->otg, NULL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800789#endif
790
791 usb_remove_hcd(hcd);
Venu Byravarasuecc8a0c2012-08-10 11:42:43 +0530792 usb_put_hcd(hcd);
793
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530794 usb_phy_shutdown(&tegra->phy->u_phy);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800795
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530796 clk_disable_unprepare(tegra->clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800797
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530798 clk_disable_unprepare(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800799
Benoit Goby79ad3b52011-03-09 16:28:56 -0800800 return 0;
801}
802
803static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
804{
805 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
806 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
807
808 if (hcd->driver->shutdown)
809 hcd->driver->shutdown(hcd);
810}
811
Bill Pembertond3608b62012-11-19 13:24:34 -0500812static struct of_device_id tegra_ehci_of_match[] = {
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000813 { .compatible = "nvidia,tegra20-ehci", },
814 { },
815};
816
Benoit Goby79ad3b52011-03-09 16:28:56 -0800817static struct platform_driver tegra_ehci_driver = {
818 .probe = tegra_ehci_probe,
819 .remove = tegra_ehci_remove,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800820 .shutdown = tegra_ehci_hcd_shutdown,
821 .driver = {
822 .name = "tegra-ehci",
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000823 .of_match_table = tegra_ehci_of_match,
Alan Sternebf20de2012-05-01 11:28:49 -0400824#ifdef CONFIG_PM
825 .pm = &tegra_ehci_pm_ops,
826#endif
Benoit Goby79ad3b52011-03-09 16:28:56 -0800827 }
828};