blob: 1f596fb7cf71c72292ca951258a4c1165d888221 [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.
Venu Byravarasubbdabdb2013-01-17 20:15:37 +00005 * Copyright (C) 2009 - 2013 NVIDIA Corporation
Benoit Goby79ad3b52011-03-09 16:28:56 -08006 *
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>
Venu Byravarasubbdabdb2013-01-17 20:15:37 +000029#include <linux/usb/ehci_def.h>
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
Venu Byravarasubbdabdb2013-01-17 20:15:37 +000036/* PORTSC registers */
37#define TEGRA_USB_PORTSC1 0x184
38#define TEGRA_USB_PORTSC1_PTS(x) (((x) & 0x3) << 30)
39#define TEGRA_USB_PORTSC1_PHCD (1 << 23)
40
Robert Morellfbf98652011-03-09 16:28:57 -080041#define TEGRA_USB_DMA_ALIGN 32
42
Benoit Goby79ad3b52011-03-09 16:28:56 -080043struct tegra_ehci_hcd {
44 struct ehci_hcd *ehci;
45 struct tegra_usb_phy *phy;
46 struct clk *clk;
47 struct clk *emc_clk;
Heikki Krogerus86753812012-02-13 13:24:02 +020048 struct usb_phy *transceiver;
Benoit Goby79ad3b52011-03-09 16:28:56 -080049 int host_resumed;
Benoit Goby79ad3b52011-03-09 16:28:56 -080050 int port_resuming;
Venu Byravarasu585355c2012-12-13 20:59:08 +000051 bool needs_double_reset;
Benoit Goby79ad3b52011-03-09 16:28:56 -080052 enum tegra_usb_phy_port_speed port_speed;
53};
54
55static void tegra_ehci_power_up(struct usb_hcd *hcd)
56{
57 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
58
Prashant Gaikwad20de12c2012-06-05 09:59:38 +053059 clk_prepare_enable(tegra->emc_clk);
60 clk_prepare_enable(tegra->clk);
Venu Byravarasu1ba82162012-09-05 18:50:23 +053061 usb_phy_set_suspend(&tegra->phy->u_phy, 0);
Benoit Goby79ad3b52011-03-09 16:28:56 -080062 tegra->host_resumed = 1;
63}
64
65static void tegra_ehci_power_down(struct usb_hcd *hcd)
66{
67 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
68
69 tegra->host_resumed = 0;
Venu Byravarasu1ba82162012-09-05 18:50:23 +053070 usb_phy_set_suspend(&tegra->phy->u_phy, 1);
Prashant Gaikwad20de12c2012-06-05 09:59:38 +053071 clk_disable_unprepare(tegra->clk);
72 clk_disable_unprepare(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -080073}
74
Jim Lin1f594b62011-04-17 11:58:25 +030075static int tegra_ehci_internal_port_reset(
76 struct ehci_hcd *ehci,
77 u32 __iomem *portsc_reg
78)
79{
80 u32 temp;
81 unsigned long flags;
82 int retval = 0;
83 int i, tries;
84 u32 saved_usbintr;
85
86 spin_lock_irqsave(&ehci->lock, flags);
87 saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
88 /* disable USB interrupt */
89 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
90 spin_unlock_irqrestore(&ehci->lock, flags);
91
92 /*
93 * Here we have to do Port Reset at most twice for
94 * Port Enable bit to be set.
95 */
96 for (i = 0; i < 2; i++) {
97 temp = ehci_readl(ehci, portsc_reg);
98 temp |= PORT_RESET;
99 ehci_writel(ehci, temp, portsc_reg);
100 mdelay(10);
101 temp &= ~PORT_RESET;
102 ehci_writel(ehci, temp, portsc_reg);
103 mdelay(1);
104 tries = 100;
105 do {
106 mdelay(1);
107 /*
108 * Up to this point, Port Enable bit is
109 * expected to be set after 2 ms waiting.
110 * USB1 usually takes extra 45 ms, for safety,
111 * we take 100 ms as timeout.
112 */
113 temp = ehci_readl(ehci, portsc_reg);
114 } while (!(temp & PORT_PE) && tries--);
115 if (temp & PORT_PE)
116 break;
117 }
118 if (i == 2)
119 retval = -ETIMEDOUT;
120
121 /*
122 * Clear Connect Status Change bit if it's set.
123 * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
124 */
125 if (temp & PORT_CSC)
126 ehci_writel(ehci, PORT_CSC, portsc_reg);
127
128 /*
129 * Write to clear any interrupt status bits that might be set
130 * during port reset.
131 */
132 temp = ehci_readl(ehci, &ehci->regs->status);
133 ehci_writel(ehci, temp, &ehci->regs->status);
134
135 /* restore original interrupt enable bits */
136 ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
137 return retval;
138}
139
Benoit Goby79ad3b52011-03-09 16:28:56 -0800140static int tegra_ehci_hub_control(
141 struct usb_hcd *hcd,
142 u16 typeReq,
143 u16 wValue,
144 u16 wIndex,
145 char *buf,
146 u16 wLength
147)
148{
149 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
150 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
151 u32 __iomem *status_reg;
152 u32 temp;
153 unsigned long flags;
154 int retval = 0;
155
156 status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
157
158 spin_lock_irqsave(&ehci->lock, flags);
159
Stephen Warren6d5f89c2012-04-18 15:32:46 -0600160 if (typeReq == GetPortStatus) {
Benoit Goby79ad3b52011-03-09 16:28:56 -0800161 temp = ehci_readl(ehci, status_reg);
162 if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
163 /* Resume completed, re-enable disconnect detection */
164 tegra->port_resuming = 0;
165 tegra_usb_phy_postresume(tegra->phy);
166 }
167 }
168
169 else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
170 temp = ehci_readl(ehci, status_reg);
171 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
172 retval = -EPIPE;
173 goto done;
174 }
175
Stephen Warrenb0876572012-04-25 12:31:10 -0600176 temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800177 temp |= PORT_WKDISC_E | PORT_WKOC_E;
178 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
179
180 /*
181 * If a transaction is in progress, there may be a delay in
182 * suspending the port. Poll until the port is suspended.
183 */
184 if (handshake(ehci, status_reg, PORT_SUSPEND,
185 PORT_SUSPEND, 5000))
186 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
187
188 set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
189 goto done;
190 }
191
Jim Lin1f594b62011-04-17 11:58:25 +0300192 /* For USB1 port we need to issue Port Reset twice internally */
Venu Byravarasu585355c2012-12-13 20:59:08 +0000193 if (tegra->needs_double_reset &&
Jim Lin1f594b62011-04-17 11:58:25 +0300194 (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
195 spin_unlock_irqrestore(&ehci->lock, flags);
196 return tegra_ehci_internal_port_reset(ehci, status_reg);
197 }
198
Benoit Goby79ad3b52011-03-09 16:28:56 -0800199 /*
200 * Tegra host controller will time the resume operation to clear the bit
201 * when the port control state switches to HS or FS Idle. This behavior
202 * is different from EHCI where the host controller driver is required
203 * to set this bit to a zero after the resume duration is timed in the
204 * driver.
205 */
206 else if (typeReq == ClearPortFeature &&
207 wValue == USB_PORT_FEAT_SUSPEND) {
208 temp = ehci_readl(ehci, status_reg);
209 if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
210 retval = -EPIPE;
211 goto done;
212 }
213
214 if (!(temp & PORT_SUSPEND))
215 goto done;
216
217 /* Disable disconnect detection during port resume */
218 tegra_usb_phy_preresume(tegra->phy);
219
220 ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
221
222 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
223 /* start resume signalling */
224 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
Alan Sterna448e4d2012-04-03 15:24:30 -0400225 set_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800226
227 spin_unlock_irqrestore(&ehci->lock, flags);
228 msleep(20);
229 spin_lock_irqsave(&ehci->lock, flags);
230
231 /* Poll until the controller clears RESUME and SUSPEND */
232 if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
233 pr_err("%s: timeout waiting for RESUME\n", __func__);
234 if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
235 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
236
237 ehci->reset_done[wIndex-1] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400238 clear_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800239
240 tegra->port_resuming = 1;
241 goto done;
242 }
243
244 spin_unlock_irqrestore(&ehci->lock, flags);
245
246 /* Handle the hub control events here */
247 return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
248done:
249 spin_unlock_irqrestore(&ehci->lock, flags);
250 return retval;
251}
252
253static void tegra_ehci_restart(struct usb_hcd *hcd)
254{
255 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
256
257 ehci_reset(ehci);
258
259 /* setup the frame list and Async q heads */
260 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
261 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
262 /* setup the command register and set the controller in RUN mode */
263 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
264 ehci->command |= CMD_RUN;
265 ehci_writel(ehci, ehci->command, &ehci->regs->command);
266
267 down_write(&ehci_cf_port_reset_rwsem);
268 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
269 /* flush posted writes */
270 ehci_readl(ehci, &ehci->regs->command);
271 up_write(&ehci_cf_port_reset_rwsem);
272}
273
Benoit Goby79ad3b52011-03-09 16:28:56 -0800274static void tegra_ehci_shutdown(struct usb_hcd *hcd)
275{
276 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
277
278 /* ehci_shutdown touches the USB controller registers, make sure
279 * controller has clocks to it */
280 if (!tegra->host_resumed)
281 tegra_ehci_power_up(hcd);
282
283 ehci_shutdown(hcd);
284}
285
286static int tegra_ehci_setup(struct usb_hcd *hcd)
287{
288 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800289
290 /* EHCI registers start at offset 0x100 */
291 ehci->caps = hcd->regs + 0x100;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800292
293 /* switch to host mode */
294 hcd->has_tt = 1;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800295
Alan Sternc73cee72012-10-31 13:21:06 -0400296 return ehci_setup(hcd);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800297}
298
Venu Byravarasufe375772012-04-05 11:25:30 +0530299struct dma_aligned_buffer {
Robert Morellfbf98652011-03-09 16:28:57 -0800300 void *kmalloc_ptr;
301 void *old_xfer_buffer;
302 u8 data[0];
303};
304
Venu Byravarasufe375772012-04-05 11:25:30 +0530305static void free_dma_aligned_buffer(struct urb *urb)
Robert Morellfbf98652011-03-09 16:28:57 -0800306{
Venu Byravarasufe375772012-04-05 11:25:30 +0530307 struct dma_aligned_buffer *temp;
Robert Morellfbf98652011-03-09 16:28:57 -0800308
309 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
310 return;
311
Venu Byravarasufe375772012-04-05 11:25:30 +0530312 temp = container_of(urb->transfer_buffer,
313 struct dma_aligned_buffer, data);
Robert Morellfbf98652011-03-09 16:28:57 -0800314
Venu Byravarasufe375772012-04-05 11:25:30 +0530315 if (usb_urb_dir_in(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800316 memcpy(temp->old_xfer_buffer, temp->data,
317 urb->transfer_buffer_length);
318 urb->transfer_buffer = temp->old_xfer_buffer;
319 kfree(temp->kmalloc_ptr);
320
321 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
322}
323
Venu Byravarasufe375772012-04-05 11:25:30 +0530324static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
Robert Morellfbf98652011-03-09 16:28:57 -0800325{
Venu Byravarasufe375772012-04-05 11:25:30 +0530326 struct dma_aligned_buffer *temp, *kmalloc_ptr;
Robert Morellfbf98652011-03-09 16:28:57 -0800327 size_t kmalloc_size;
328
329 if (urb->num_sgs || urb->sg ||
330 urb->transfer_buffer_length == 0 ||
331 !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
332 return 0;
333
Robert Morellfbf98652011-03-09 16:28:57 -0800334 /* Allocate a buffer with enough padding for alignment */
335 kmalloc_size = urb->transfer_buffer_length +
Venu Byravarasufe375772012-04-05 11:25:30 +0530336 sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800337
338 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
339 if (!kmalloc_ptr)
340 return -ENOMEM;
341
Venu Byravarasufe375772012-04-05 11:25:30 +0530342 /* Position our struct dma_aligned_buffer such that data is aligned */
Robert Morellfbf98652011-03-09 16:28:57 -0800343 temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800344 temp->kmalloc_ptr = kmalloc_ptr;
345 temp->old_xfer_buffer = urb->transfer_buffer;
Venu Byravarasufe375772012-04-05 11:25:30 +0530346 if (usb_urb_dir_out(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800347 memcpy(temp->data, urb->transfer_buffer,
348 urb->transfer_buffer_length);
349 urb->transfer_buffer = temp->data;
350
351 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
352
353 return 0;
354}
355
356static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
357 gfp_t mem_flags)
358{
359 int ret;
360
Venu Byravarasufe375772012-04-05 11:25:30 +0530361 ret = alloc_dma_aligned_buffer(urb, mem_flags);
Robert Morellfbf98652011-03-09 16:28:57 -0800362 if (ret)
363 return ret;
364
365 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
366 if (ret)
Venu Byravarasufe375772012-04-05 11:25:30 +0530367 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800368
369 return ret;
370}
371
372static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
373{
374 usb_hcd_unmap_urb_for_dma(hcd, urb);
Venu Byravarasufe375772012-04-05 11:25:30 +0530375 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800376}
377
Benoit Goby79ad3b52011-03-09 16:28:56 -0800378static const struct hc_driver tegra_ehci_hc_driver = {
379 .description = hcd_name,
380 .product_desc = "Tegra EHCI Host Controller",
381 .hcd_priv_size = sizeof(struct ehci_hcd),
Benoit Goby79ad3b52011-03-09 16:28:56 -0800382 .flags = HCD_USB2 | HCD_MEMORY,
383
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530384 /* standard ehci functions */
Benoit Goby79ad3b52011-03-09 16:28:56 -0800385 .irq = ehci_irq,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800386 .start = ehci_run,
387 .stop = ehci_stop,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800388 .urb_enqueue = ehci_urb_enqueue,
389 .urb_dequeue = ehci_urb_dequeue,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800390 .endpoint_disable = ehci_endpoint_disable,
391 .endpoint_reset = ehci_endpoint_reset,
392 .get_frame_number = ehci_get_frame,
393 .hub_status_data = ehci_hub_status_data,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800394 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530395 .relinquish_port = ehci_relinquish_port,
396 .port_handed_over = ehci_port_handed_over,
397
398 /* modified ehci functions for tegra */
399 .reset = tegra_ehci_setup,
400 .shutdown = tegra_ehci_shutdown,
401 .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
402 .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
403 .hub_control = tegra_ehci_hub_control,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800404#ifdef CONFIG_PM
Alan Sternebf20de2012-05-01 11:28:49 -0400405 .bus_suspend = ehci_bus_suspend,
406 .bus_resume = ehci_bus_resume,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800407#endif
Benoit Goby79ad3b52011-03-09 16:28:56 -0800408};
409
Stephen Warren434103a2012-03-16 16:06:07 -0600410static int setup_vbus_gpio(struct platform_device *pdev,
411 struct tegra_ehci_platform_data *pdata)
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000412{
413 int err = 0;
414 int gpio;
415
Stephen Warren434103a2012-03-16 16:06:07 -0600416 gpio = pdata->vbus_gpio;
417 if (!gpio_is_valid(gpio))
418 gpio = of_get_named_gpio(pdev->dev.of_node,
419 "nvidia,vbus-gpio", 0);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000420 if (!gpio_is_valid(gpio))
421 return 0;
422
423 err = gpio_request(gpio, "vbus_gpio");
424 if (err) {
425 dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
426 return err;
427 }
428 err = gpio_direction_output(gpio, 1);
429 if (err) {
430 dev_err(&pdev->dev, "can't enable vbus\n");
431 return err;
432 }
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000433
434 return err;
435}
436
Alan Sternebf20de2012-05-01 11:28:49 -0400437#ifdef CONFIG_PM
438
439static int controller_suspend(struct device *dev)
440{
441 struct tegra_ehci_hcd *tegra =
442 platform_get_drvdata(to_platform_device(dev));
443 struct ehci_hcd *ehci = tegra->ehci;
444 struct usb_hcd *hcd = ehci_to_hcd(ehci);
445 struct ehci_regs __iomem *hw = ehci->regs;
446 unsigned long flags;
447
448 if (time_before(jiffies, ehci->next_statechange))
449 msleep(10);
450
Alan Sternebf20de2012-05-01 11:28:49 -0400451 ehci_halt(ehci);
Alan Sternebf20de2012-05-01 11:28:49 -0400452
Alan Sternc4f34762012-07-11 11:23:10 -0400453 spin_lock_irqsave(&ehci->lock, flags);
454 tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
455 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Alan Sternebf20de2012-05-01 11:28:49 -0400456 spin_unlock_irqrestore(&ehci->lock, flags);
457
458 tegra_ehci_power_down(hcd);
459 return 0;
460}
461
462static int controller_resume(struct device *dev)
463{
464 struct tegra_ehci_hcd *tegra =
465 platform_get_drvdata(to_platform_device(dev));
466 struct ehci_hcd *ehci = tegra->ehci;
467 struct usb_hcd *hcd = ehci_to_hcd(ehci);
468 struct ehci_regs __iomem *hw = ehci->regs;
469 unsigned long val;
470
471 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
472 tegra_ehci_power_up(hcd);
473
474 if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
475 /* Wait for the phy to detect new devices
476 * before we restart the controller */
477 msleep(10);
478 goto restart;
479 }
480
481 /* Force the phy to keep data lines in suspend state */
482 tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
483
484 /* Enable host mode */
485 tdi_reset(ehci);
486
487 /* Enable Port Power */
488 val = readl(&hw->port_status[0]);
489 val |= PORT_POWER;
490 writel(val, &hw->port_status[0]);
491 udelay(10);
492
493 /* Check if the phy resume from LP0. When the phy resume from LP0
494 * USB register will be reset. */
495 if (!readl(&hw->async_next)) {
496 /* Program the field PTC based on the saved speed mode */
497 val = readl(&hw->port_status[0]);
498 val &= ~PORT_TEST(~0);
499 if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
500 val |= PORT_TEST_FORCE;
501 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
502 val |= PORT_TEST(6);
503 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
504 val |= PORT_TEST(7);
505 writel(val, &hw->port_status[0]);
506 udelay(10);
507
508 /* Disable test mode by setting PTC field to NORMAL_OP */
509 val = readl(&hw->port_status[0]);
510 val &= ~PORT_TEST(~0);
511 writel(val, &hw->port_status[0]);
512 udelay(10);
513 }
514
515 /* Poll until CCS is enabled */
516 if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
517 PORT_CONNECT, 2000)) {
518 pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
519 goto restart;
520 }
521
522 /* Poll until PE is enabled */
523 if (handshake(ehci, &hw->port_status[0], PORT_PE,
524 PORT_PE, 2000)) {
525 pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
526 goto restart;
527 }
528
529 /* Clear the PCI status, to avoid an interrupt taken upon resume */
530 val = readl(&hw->status);
531 val |= STS_PCD;
532 writel(val, &hw->status);
533
534 /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
535 val = readl(&hw->port_status[0]);
536 if ((val & PORT_POWER) && (val & PORT_PE)) {
537 val |= PORT_SUSPEND;
538 writel(val, &hw->port_status[0]);
539
540 /* Wait until port suspend completes */
541 if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
542 PORT_SUSPEND, 1000)) {
543 pr_err("%s: timeout waiting for PORT_SUSPEND\n",
544 __func__);
545 goto restart;
546 }
547 }
548
549 tegra_ehci_phy_restore_end(tegra->phy);
550 goto done;
551
552 restart:
553 if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
554 tegra_ehci_phy_restore_end(tegra->phy);
555
556 tegra_ehci_restart(hcd);
557
558 done:
559 tegra_usb_phy_preresume(tegra->phy);
560 tegra->port_resuming = 1;
561 return 0;
562}
563
564static int tegra_ehci_suspend(struct device *dev)
565{
566 struct tegra_ehci_hcd *tegra =
567 platform_get_drvdata(to_platform_device(dev));
568 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
569 int rc = 0;
570
571 /*
572 * When system sleep is supported and USB controller wakeup is
573 * implemented: If the controller is runtime-suspended and the
574 * wakeup setting needs to be changed, call pm_runtime_resume().
575 */
576 if (HCD_HW_ACCESSIBLE(hcd))
577 rc = controller_suspend(dev);
578 return rc;
579}
580
581static int tegra_ehci_resume(struct device *dev)
582{
583 int rc;
584
585 rc = controller_resume(dev);
586 if (rc == 0) {
587 pm_runtime_disable(dev);
588 pm_runtime_set_active(dev);
589 pm_runtime_enable(dev);
590 }
591 return rc;
592}
593
594static int tegra_ehci_runtime_suspend(struct device *dev)
595{
596 return controller_suspend(dev);
597}
598
599static int tegra_ehci_runtime_resume(struct device *dev)
600{
601 return controller_resume(dev);
602}
603
604static const struct dev_pm_ops tegra_ehci_pm_ops = {
605 .suspend = tegra_ehci_suspend,
606 .resume = tegra_ehci_resume,
607 .runtime_suspend = tegra_ehci_runtime_suspend,
608 .runtime_resume = tegra_ehci_runtime_resume,
609};
610
611#endif
612
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000613/* Bits of PORTSC1, which will get cleared by writing 1 into them */
614#define TEGRA_PORTSC1_RWC_BITS (PORT_CSC | PORT_PEC | PORT_OCC)
615
616void tegra_ehci_set_pts(struct usb_phy *x, u8 pts_val)
617{
618 unsigned long val;
619 struct usb_hcd *hcd = bus_to_hcd(x->otg->host);
620 void __iomem *base = hcd->regs;
621
622 val = readl(base + TEGRA_USB_PORTSC1) & ~TEGRA_PORTSC1_RWC_BITS;
623 val &= ~TEGRA_USB_PORTSC1_PTS(3);
624 val |= TEGRA_USB_PORTSC1_PTS(pts_val & 3);
625 writel(val, base + TEGRA_USB_PORTSC1);
626}
627EXPORT_SYMBOL_GPL(tegra_ehci_set_pts);
628
629void tegra_ehci_set_phcd(struct usb_phy *x, bool enable)
630{
631 unsigned long val;
632 struct usb_hcd *hcd = bus_to_hcd(x->otg->host);
633 void __iomem *base = hcd->regs;
634
635 val = readl(base + TEGRA_USB_PORTSC1) & ~TEGRA_PORTSC1_RWC_BITS;
636 if (enable)
637 val |= TEGRA_USB_PORTSC1_PHCD;
638 else
639 val &= ~TEGRA_USB_PORTSC1_PHCD;
640 writel(val, base + TEGRA_USB_PORTSC1);
641}
642EXPORT_SYMBOL_GPL(tegra_ehci_set_phcd);
643
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000644static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
645
Benoit Goby79ad3b52011-03-09 16:28:56 -0800646static int tegra_ehci_probe(struct platform_device *pdev)
647{
648 struct resource *res;
649 struct usb_hcd *hcd;
650 struct tegra_ehci_hcd *tegra;
651 struct tegra_ehci_platform_data *pdata;
652 int err = 0;
653 int irq;
654 int instance = pdev->id;
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000655 struct usb_phy *u_phy;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800656
657 pdata = pdev->dev.platform_data;
658 if (!pdata) {
659 dev_err(&pdev->dev, "Platform data missing\n");
660 return -EINVAL;
661 }
662
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000663 /* Right now device-tree probed devices don't get dma_mask set.
664 * Since shared usb code relies on it, set it here for now.
665 * Once we have dma capability bindings this can go away.
666 */
667 if (!pdev->dev.dma_mask)
668 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
669
Stephen Warren434103a2012-03-16 16:06:07 -0600670 setup_vbus_gpio(pdev, pdata);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000671
Julia Lawallbc2ff982012-07-30 16:43:41 +0200672 tegra = devm_kzalloc(&pdev->dev, sizeof(struct tegra_ehci_hcd),
673 GFP_KERNEL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800674 if (!tegra)
675 return -ENOMEM;
676
677 hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
678 dev_name(&pdev->dev));
679 if (!hcd) {
680 dev_err(&pdev->dev, "Unable to create HCD\n");
Julia Lawallbc2ff982012-07-30 16:43:41 +0200681 return -ENOMEM;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800682 }
683
684 platform_set_drvdata(pdev, tegra);
685
Julia Lawallbc2ff982012-07-30 16:43:41 +0200686 tegra->clk = devm_clk_get(&pdev->dev, NULL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800687 if (IS_ERR(tegra->clk)) {
688 dev_err(&pdev->dev, "Can't get ehci clock\n");
689 err = PTR_ERR(tegra->clk);
690 goto fail_clk;
691 }
692
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530693 err = clk_prepare_enable(tegra->clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800694 if (err)
Julia Lawallbc2ff982012-07-30 16:43:41 +0200695 goto fail_clk;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800696
Julia Lawallbc2ff982012-07-30 16:43:41 +0200697 tegra->emc_clk = devm_clk_get(&pdev->dev, "emc");
Benoit Goby79ad3b52011-03-09 16:28:56 -0800698 if (IS_ERR(tegra->emc_clk)) {
699 dev_err(&pdev->dev, "Can't get emc clock\n");
700 err = PTR_ERR(tegra->emc_clk);
701 goto fail_emc_clk;
702 }
703
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530704 clk_prepare_enable(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800705 clk_set_rate(tegra->emc_clk, 400000000);
706
Venu Byravarasu585355c2012-12-13 20:59:08 +0000707 tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
708 "nvidia,needs-double-reset");
709
Benoit Goby79ad3b52011-03-09 16:28:56 -0800710 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
711 if (!res) {
712 dev_err(&pdev->dev, "Failed to get I/O memory\n");
713 err = -ENXIO;
714 goto fail_io;
715 }
716 hcd->rsrc_start = res->start;
717 hcd->rsrc_len = resource_size(res);
Julia Lawallbc2ff982012-07-30 16:43:41 +0200718 hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
Benoit Goby79ad3b52011-03-09 16:28:56 -0800719 if (!hcd->regs) {
720 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
721 err = -ENOMEM;
722 goto fail_io;
723 }
724
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000725 /* This is pretty ugly and needs to be fixed when we do only
726 * device-tree probing. Old code relies on the platform_device
727 * numbering that we lack for device-tree-instantiated devices.
728 */
729 if (instance < 0) {
730 switch (res->start) {
731 case TEGRA_USB_BASE:
732 instance = 0;
733 break;
734 case TEGRA_USB2_BASE:
735 instance = 1;
736 break;
737 case TEGRA_USB3_BASE:
738 instance = 2;
739 break;
740 default:
741 err = -ENODEV;
742 dev_err(&pdev->dev, "unknown usb instance\n");
Julia Lawallbc2ff982012-07-30 16:43:41 +0200743 goto fail_io;
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000744 }
745 }
746
Stephen Warrenaa607eb2012-04-12 15:46:49 -0600747 tegra->phy = tegra_usb_phy_open(&pdev->dev, instance, hcd->regs,
748 pdata->phy_config,
749 TEGRA_USB_PHY_MODE_HOST);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800750 if (IS_ERR(tegra->phy)) {
751 dev_err(&pdev->dev, "Failed to open USB phy\n");
752 err = -ENXIO;
Julia Lawallbc2ff982012-07-30 16:43:41 +0200753 goto fail_io;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800754 }
755
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530756 usb_phy_init(&tegra->phy->u_phy);
757
Venu Byravarasubbdabdb2013-01-17 20:15:37 +0000758 hcd->phy = u_phy = &tegra->phy->u_phy;
759 u_phy->otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
760 GFP_KERNEL);
761 if (!u_phy->otg) {
762 dev_err(&pdev->dev, "Failed to alloc memory for otg\n");
763 err = -ENOMEM;
764 goto fail_io;
765 }
766 u_phy->otg->host = hcd_to_bus(hcd);
767
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530768 err = usb_phy_set_suspend(&tegra->phy->u_phy, 0);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800769 if (err) {
770 dev_err(&pdev->dev, "Failed to power on the phy\n");
771 goto fail;
772 }
773
774 tegra->host_resumed = 1;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800775 tegra->ehci = hcd_to_ehci(hcd);
776
777 irq = platform_get_irq(pdev, 0);
778 if (!irq) {
779 dev_err(&pdev->dev, "Failed to get IRQ\n");
780 err = -ENODEV;
781 goto fail;
782 }
Benoit Goby79ad3b52011-03-09 16:28:56 -0800783
784#ifdef CONFIG_USB_OTG_UTILS
785 if (pdata->operating_mode == TEGRA_USB_OTG) {
Julia Lawallbc2ff982012-07-30 16:43:41 +0200786 tegra->transceiver =
787 devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
Kishon Vijay Abraham Ided017e2012-06-26 17:40:32 +0530788 if (!IS_ERR_OR_NULL(tegra->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200789 otg_set_host(tegra->transceiver->otg, &hcd->self);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800790 }
791#endif
792
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800793 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800794 if (err) {
795 dev_err(&pdev->dev, "Failed to add USB HCD\n");
796 goto fail;
797 }
798
Alan Sternebf20de2012-05-01 11:28:49 -0400799 pm_runtime_set_active(&pdev->dev);
800 pm_runtime_get_noresume(&pdev->dev);
801
802 /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
803 /* if (!pdata->power_down_on_bus_suspend) */
804 pm_runtime_forbid(&pdev->dev);
805 pm_runtime_enable(&pdev->dev);
806 pm_runtime_put_sync(&pdev->dev);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800807 return err;
808
809fail:
810#ifdef CONFIG_USB_OTG_UTILS
Julia Lawallbc2ff982012-07-30 16:43:41 +0200811 if (!IS_ERR_OR_NULL(tegra->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200812 otg_set_host(tegra->transceiver->otg, NULL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800813#endif
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530814 usb_phy_shutdown(&tegra->phy->u_phy);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800815fail_io:
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530816 clk_disable_unprepare(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800817fail_emc_clk:
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530818 clk_disable_unprepare(tegra->clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800819fail_clk:
820 usb_put_hcd(hcd);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800821 return err;
822}
823
Benoit Goby79ad3b52011-03-09 16:28:56 -0800824static int tegra_ehci_remove(struct platform_device *pdev)
825{
826 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
827 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
828
Alan Sternebf20de2012-05-01 11:28:49 -0400829 pm_runtime_get_sync(&pdev->dev);
830 pm_runtime_disable(&pdev->dev);
831 pm_runtime_put_noidle(&pdev->dev);
832
Benoit Goby79ad3b52011-03-09 16:28:56 -0800833#ifdef CONFIG_USB_OTG_UTILS
Julia Lawallbc2ff982012-07-30 16:43:41 +0200834 if (!IS_ERR_OR_NULL(tegra->transceiver))
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200835 otg_set_host(tegra->transceiver->otg, NULL);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800836#endif
837
838 usb_remove_hcd(hcd);
Venu Byravarasuecc8a0c2012-08-10 11:42:43 +0530839 usb_put_hcd(hcd);
840
Venu Byravarasu1ba82162012-09-05 18:50:23 +0530841 usb_phy_shutdown(&tegra->phy->u_phy);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800842
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530843 clk_disable_unprepare(tegra->clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800844
Prashant Gaikwad20de12c2012-06-05 09:59:38 +0530845 clk_disable_unprepare(tegra->emc_clk);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800846
Benoit Goby79ad3b52011-03-09 16:28:56 -0800847 return 0;
848}
849
850static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
851{
852 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
853 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
854
855 if (hcd->driver->shutdown)
856 hcd->driver->shutdown(hcd);
857}
858
Bill Pembertond3608b62012-11-19 13:24:34 -0500859static struct of_device_id tegra_ehci_of_match[] = {
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000860 { .compatible = "nvidia,tegra20-ehci", },
861 { },
862};
863
Benoit Goby79ad3b52011-03-09 16:28:56 -0800864static struct platform_driver tegra_ehci_driver = {
865 .probe = tegra_ehci_probe,
866 .remove = tegra_ehci_remove,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800867 .shutdown = tegra_ehci_hcd_shutdown,
868 .driver = {
869 .name = "tegra-ehci",
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000870 .of_match_table = tegra_ehci_of_match,
Alan Sternebf20de2012-05-01 11:28:49 -0400871#ifdef CONFIG_PM
872 .pm = &tegra_ehci_pm_ops,
873#endif
Benoit Goby79ad3b52011-03-09 16:28:56 -0800874 }
875};