blob: 99ae5ea3f8d1d39ea2fd51ce82dbf6ccd3f3fa83 [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>
20#include <linux/platform_device.h>
21#include <linux/platform_data/tegra_usb.h>
22#include <linux/irq.h>
23#include <linux/usb/otg.h>
Olof Johansson4a53f4e2011-11-04 09:12:40 +000024#include <linux/gpio.h>
25#include <linux/of.h>
26#include <linux/of_gpio.h>
Alan Sternebf20de2012-05-01 11:28:49 -040027#include <linux/pm_runtime.h>
Olof Johansson4a53f4e2011-11-04 09:12:40 +000028
Benoit Goby79ad3b52011-03-09 16:28:56 -080029#include <mach/usb_phy.h>
Olof Johansson4a53f4e2011-11-04 09:12:40 +000030#include <mach/iomap.h>
Benoit Goby79ad3b52011-03-09 16:28:56 -080031
Robert Morellfbf98652011-03-09 16:28:57 -080032#define TEGRA_USB_DMA_ALIGN 32
33
Benoit Goby79ad3b52011-03-09 16:28:56 -080034struct tegra_ehci_hcd {
35 struct ehci_hcd *ehci;
36 struct tegra_usb_phy *phy;
37 struct clk *clk;
38 struct clk *emc_clk;
Heikki Krogerus86753812012-02-13 13:24:02 +020039 struct usb_phy *transceiver;
Benoit Goby79ad3b52011-03-09 16:28:56 -080040 int host_resumed;
Benoit Goby79ad3b52011-03-09 16:28:56 -080041 int port_resuming;
Benoit Goby79ad3b52011-03-09 16:28:56 -080042 enum tegra_usb_phy_port_speed port_speed;
43};
44
45static void tegra_ehci_power_up(struct usb_hcd *hcd)
46{
47 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
48
49 clk_enable(tegra->emc_clk);
50 clk_enable(tegra->clk);
51 tegra_usb_phy_power_on(tegra->phy);
52 tegra->host_resumed = 1;
53}
54
55static void tegra_ehci_power_down(struct usb_hcd *hcd)
56{
57 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
58
59 tegra->host_resumed = 0;
60 tegra_usb_phy_power_off(tegra->phy);
61 clk_disable(tegra->clk);
62 clk_disable(tegra->emc_clk);
63}
64
Jim Lin1f594b62011-04-17 11:58:25 +030065static int tegra_ehci_internal_port_reset(
66 struct ehci_hcd *ehci,
67 u32 __iomem *portsc_reg
68)
69{
70 u32 temp;
71 unsigned long flags;
72 int retval = 0;
73 int i, tries;
74 u32 saved_usbintr;
75
76 spin_lock_irqsave(&ehci->lock, flags);
77 saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
78 /* disable USB interrupt */
79 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
80 spin_unlock_irqrestore(&ehci->lock, flags);
81
82 /*
83 * Here we have to do Port Reset at most twice for
84 * Port Enable bit to be set.
85 */
86 for (i = 0; i < 2; i++) {
87 temp = ehci_readl(ehci, portsc_reg);
88 temp |= PORT_RESET;
89 ehci_writel(ehci, temp, portsc_reg);
90 mdelay(10);
91 temp &= ~PORT_RESET;
92 ehci_writel(ehci, temp, portsc_reg);
93 mdelay(1);
94 tries = 100;
95 do {
96 mdelay(1);
97 /*
98 * Up to this point, Port Enable bit is
99 * expected to be set after 2 ms waiting.
100 * USB1 usually takes extra 45 ms, for safety,
101 * we take 100 ms as timeout.
102 */
103 temp = ehci_readl(ehci, portsc_reg);
104 } while (!(temp & PORT_PE) && tries--);
105 if (temp & PORT_PE)
106 break;
107 }
108 if (i == 2)
109 retval = -ETIMEDOUT;
110
111 /*
112 * Clear Connect Status Change bit if it's set.
113 * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
114 */
115 if (temp & PORT_CSC)
116 ehci_writel(ehci, PORT_CSC, portsc_reg);
117
118 /*
119 * Write to clear any interrupt status bits that might be set
120 * during port reset.
121 */
122 temp = ehci_readl(ehci, &ehci->regs->status);
123 ehci_writel(ehci, temp, &ehci->regs->status);
124
125 /* restore original interrupt enable bits */
126 ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
127 return retval;
128}
129
Benoit Goby79ad3b52011-03-09 16:28:56 -0800130static int tegra_ehci_hub_control(
131 struct usb_hcd *hcd,
132 u16 typeReq,
133 u16 wValue,
134 u16 wIndex,
135 char *buf,
136 u16 wLength
137)
138{
139 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
140 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
141 u32 __iomem *status_reg;
142 u32 temp;
143 unsigned long flags;
144 int retval = 0;
145
146 status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
147
148 spin_lock_irqsave(&ehci->lock, flags);
149
150 /*
151 * In ehci_hub_control() for USB_PORT_FEAT_ENABLE clears the other bits
152 * that are write on clear, by writing back the register read value, so
153 * USB_PORT_FEAT_ENABLE is handled by masking the set on clear bits
154 */
155 if (typeReq == ClearPortFeature && wValue == USB_PORT_FEAT_ENABLE) {
156 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
157 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
158 goto done;
159 }
160
161 else if (typeReq == GetPortStatus) {
162 temp = ehci_readl(ehci, status_reg);
163 if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
164 /* Resume completed, re-enable disconnect detection */
165 tegra->port_resuming = 0;
166 tegra_usb_phy_postresume(tegra->phy);
167 }
168 }
169
170 else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
171 temp = ehci_readl(ehci, status_reg);
172 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
173 retval = -EPIPE;
174 goto done;
175 }
176
177 temp &= ~PORT_WKCONN_E;
178 temp |= PORT_WKDISC_E | PORT_WKOC_E;
179 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
180
181 /*
182 * If a transaction is in progress, there may be a delay in
183 * suspending the port. Poll until the port is suspended.
184 */
185 if (handshake(ehci, status_reg, PORT_SUSPEND,
186 PORT_SUSPEND, 5000))
187 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
188
189 set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
190 goto done;
191 }
192
Jim Lin1f594b62011-04-17 11:58:25 +0300193 /* For USB1 port we need to issue Port Reset twice internally */
194 if (tegra->phy->instance == 0 &&
195 (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
196 spin_unlock_irqrestore(&ehci->lock, flags);
197 return tegra_ehci_internal_port_reset(ehci, status_reg);
198 }
199
Benoit Goby79ad3b52011-03-09 16:28:56 -0800200 /*
201 * Tegra host controller will time the resume operation to clear the bit
202 * when the port control state switches to HS or FS Idle. This behavior
203 * is different from EHCI where the host controller driver is required
204 * to set this bit to a zero after the resume duration is timed in the
205 * driver.
206 */
207 else if (typeReq == ClearPortFeature &&
208 wValue == USB_PORT_FEAT_SUSPEND) {
209 temp = ehci_readl(ehci, status_reg);
210 if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
211 retval = -EPIPE;
212 goto done;
213 }
214
215 if (!(temp & PORT_SUSPEND))
216 goto done;
217
218 /* Disable disconnect detection during port resume */
219 tegra_usb_phy_preresume(tegra->phy);
220
221 ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
222
223 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
224 /* start resume signalling */
225 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
Alan Sterna448e4d2012-04-03 15:24:30 -0400226 set_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800227
228 spin_unlock_irqrestore(&ehci->lock, flags);
229 msleep(20);
230 spin_lock_irqsave(&ehci->lock, flags);
231
232 /* Poll until the controller clears RESUME and SUSPEND */
233 if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
234 pr_err("%s: timeout waiting for RESUME\n", __func__);
235 if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
236 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
237
238 ehci->reset_done[wIndex-1] = 0;
Alan Sterna448e4d2012-04-03 15:24:30 -0400239 clear_bit(wIndex-1, &ehci->resuming_ports);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800240
241 tegra->port_resuming = 1;
242 goto done;
243 }
244
245 spin_unlock_irqrestore(&ehci->lock, flags);
246
247 /* Handle the hub control events here */
248 return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
249done:
250 spin_unlock_irqrestore(&ehci->lock, flags);
251 return retval;
252}
253
254static void tegra_ehci_restart(struct usb_hcd *hcd)
255{
256 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
257
258 ehci_reset(ehci);
259
260 /* setup the frame list and Async q heads */
261 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
262 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
263 /* setup the command register and set the controller in RUN mode */
264 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
265 ehci->command |= CMD_RUN;
266 ehci_writel(ehci, ehci->command, &ehci->regs->command);
267
268 down_write(&ehci_cf_port_reset_rwsem);
269 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
270 /* flush posted writes */
271 ehci_readl(ehci, &ehci->regs->command);
272 up_write(&ehci_cf_port_reset_rwsem);
273}
274
Benoit Goby79ad3b52011-03-09 16:28:56 -0800275static void tegra_ehci_shutdown(struct usb_hcd *hcd)
276{
277 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
278
279 /* ehci_shutdown touches the USB controller registers, make sure
280 * controller has clocks to it */
281 if (!tegra->host_resumed)
282 tegra_ehci_power_up(hcd);
283
284 ehci_shutdown(hcd);
285}
286
287static int tegra_ehci_setup(struct usb_hcd *hcd)
288{
289 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
290 int retval;
291
292 /* EHCI registers start at offset 0x100 */
293 ehci->caps = hcd->regs + 0x100;
294 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200295 HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase));
Benoit Goby79ad3b52011-03-09 16:28:56 -0800296
297 dbg_hcs_params(ehci, "reset");
298 dbg_hcc_params(ehci, "reset");
299
300 /* cache this readonly data; minimize chip reads */
301 ehci->hcs_params = readl(&ehci->caps->hcs_params);
302
303 /* switch to host mode */
304 hcd->has_tt = 1;
305 ehci_reset(ehci);
306
307 retval = ehci_halt(ehci);
308 if (retval)
309 return retval;
310
311 /* data structure init */
312 retval = ehci_init(hcd);
313 if (retval)
314 return retval;
315
316 ehci->sbrn = 0x20;
317
318 ehci_port_power(ehci, 1);
319 return retval;
320}
321
Robert Morellfbf98652011-03-09 16:28:57 -0800322struct temp_buffer {
323 void *kmalloc_ptr;
324 void *old_xfer_buffer;
325 u8 data[0];
326};
327
328static void free_temp_buffer(struct urb *urb)
329{
330 enum dma_data_direction dir;
331 struct temp_buffer *temp;
332
333 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
334 return;
335
336 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
337
338 temp = container_of(urb->transfer_buffer, struct temp_buffer,
339 data);
340
341 if (dir == DMA_FROM_DEVICE)
342 memcpy(temp->old_xfer_buffer, temp->data,
343 urb->transfer_buffer_length);
344 urb->transfer_buffer = temp->old_xfer_buffer;
345 kfree(temp->kmalloc_ptr);
346
347 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
348}
349
350static int alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
351{
352 enum dma_data_direction dir;
353 struct temp_buffer *temp, *kmalloc_ptr;
354 size_t kmalloc_size;
355
356 if (urb->num_sgs || urb->sg ||
357 urb->transfer_buffer_length == 0 ||
358 !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
359 return 0;
360
361 dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
362
363 /* Allocate a buffer with enough padding for alignment */
364 kmalloc_size = urb->transfer_buffer_length +
365 sizeof(struct temp_buffer) + TEGRA_USB_DMA_ALIGN - 1;
366
367 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
368 if (!kmalloc_ptr)
369 return -ENOMEM;
370
371 /* Position our struct temp_buffer such that data is aligned */
372 temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
373
374 temp->kmalloc_ptr = kmalloc_ptr;
375 temp->old_xfer_buffer = urb->transfer_buffer;
376 if (dir == DMA_TO_DEVICE)
377 memcpy(temp->data, urb->transfer_buffer,
378 urb->transfer_buffer_length);
379 urb->transfer_buffer = temp->data;
380
381 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
382
383 return 0;
384}
385
386static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
387 gfp_t mem_flags)
388{
389 int ret;
390
391 ret = alloc_temp_buffer(urb, mem_flags);
392 if (ret)
393 return ret;
394
395 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
396 if (ret)
397 free_temp_buffer(urb);
398
399 return ret;
400}
401
402static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
403{
404 usb_hcd_unmap_urb_for_dma(hcd, urb);
405 free_temp_buffer(urb);
406}
407
Benoit Goby79ad3b52011-03-09 16:28:56 -0800408static const struct hc_driver tegra_ehci_hc_driver = {
409 .description = hcd_name,
410 .product_desc = "Tegra EHCI Host Controller",
411 .hcd_priv_size = sizeof(struct ehci_hcd),
412
413 .flags = HCD_USB2 | HCD_MEMORY,
414
415 .reset = tegra_ehci_setup,
416 .irq = ehci_irq,
417
418 .start = ehci_run,
419 .stop = ehci_stop,
420 .shutdown = tegra_ehci_shutdown,
421 .urb_enqueue = ehci_urb_enqueue,
422 .urb_dequeue = ehci_urb_dequeue,
Robert Morellfbf98652011-03-09 16:28:57 -0800423 .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
424 .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800425 .endpoint_disable = ehci_endpoint_disable,
426 .endpoint_reset = ehci_endpoint_reset,
427 .get_frame_number = ehci_get_frame,
428 .hub_status_data = ehci_hub_status_data,
429 .hub_control = tegra_ehci_hub_control,
430 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
431#ifdef CONFIG_PM
Alan Sternebf20de2012-05-01 11:28:49 -0400432 .bus_suspend = ehci_bus_suspend,
433 .bus_resume = ehci_bus_resume,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800434#endif
435 .relinquish_port = ehci_relinquish_port,
436 .port_handed_over = ehci_port_handed_over,
437};
438
Stephen Warren434103a2012-03-16 16:06:07 -0600439static int setup_vbus_gpio(struct platform_device *pdev,
440 struct tegra_ehci_platform_data *pdata)
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000441{
442 int err = 0;
443 int gpio;
444
Stephen Warren434103a2012-03-16 16:06:07 -0600445 gpio = pdata->vbus_gpio;
446 if (!gpio_is_valid(gpio))
447 gpio = of_get_named_gpio(pdev->dev.of_node,
448 "nvidia,vbus-gpio", 0);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000449 if (!gpio_is_valid(gpio))
450 return 0;
451
452 err = gpio_request(gpio, "vbus_gpio");
453 if (err) {
454 dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
455 return err;
456 }
457 err = gpio_direction_output(gpio, 1);
458 if (err) {
459 dev_err(&pdev->dev, "can't enable vbus\n");
460 return err;
461 }
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000462
463 return err;
464}
465
Alan Sternebf20de2012-05-01 11:28:49 -0400466#ifdef CONFIG_PM
467
468static int controller_suspend(struct device *dev)
469{
470 struct tegra_ehci_hcd *tegra =
471 platform_get_drvdata(to_platform_device(dev));
472 struct ehci_hcd *ehci = tegra->ehci;
473 struct usb_hcd *hcd = ehci_to_hcd(ehci);
474 struct ehci_regs __iomem *hw = ehci->regs;
475 unsigned long flags;
476
477 if (time_before(jiffies, ehci->next_statechange))
478 msleep(10);
479
480 spin_lock_irqsave(&ehci->lock, flags);
481
482 tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
483 ehci_halt(ehci);
484 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
485
486 spin_unlock_irqrestore(&ehci->lock, flags);
487
488 tegra_ehci_power_down(hcd);
489 return 0;
490}
491
492static int controller_resume(struct device *dev)
493{
494 struct tegra_ehci_hcd *tegra =
495 platform_get_drvdata(to_platform_device(dev));
496 struct ehci_hcd *ehci = tegra->ehci;
497 struct usb_hcd *hcd = ehci_to_hcd(ehci);
498 struct ehci_regs __iomem *hw = ehci->regs;
499 unsigned long val;
500
501 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
502 tegra_ehci_power_up(hcd);
503
504 if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
505 /* Wait for the phy to detect new devices
506 * before we restart the controller */
507 msleep(10);
508 goto restart;
509 }
510
511 /* Force the phy to keep data lines in suspend state */
512 tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
513
514 /* Enable host mode */
515 tdi_reset(ehci);
516
517 /* Enable Port Power */
518 val = readl(&hw->port_status[0]);
519 val |= PORT_POWER;
520 writel(val, &hw->port_status[0]);
521 udelay(10);
522
523 /* Check if the phy resume from LP0. When the phy resume from LP0
524 * USB register will be reset. */
525 if (!readl(&hw->async_next)) {
526 /* Program the field PTC based on the saved speed mode */
527 val = readl(&hw->port_status[0]);
528 val &= ~PORT_TEST(~0);
529 if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
530 val |= PORT_TEST_FORCE;
531 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
532 val |= PORT_TEST(6);
533 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
534 val |= PORT_TEST(7);
535 writel(val, &hw->port_status[0]);
536 udelay(10);
537
538 /* Disable test mode by setting PTC field to NORMAL_OP */
539 val = readl(&hw->port_status[0]);
540 val &= ~PORT_TEST(~0);
541 writel(val, &hw->port_status[0]);
542 udelay(10);
543 }
544
545 /* Poll until CCS is enabled */
546 if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
547 PORT_CONNECT, 2000)) {
548 pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
549 goto restart;
550 }
551
552 /* Poll until PE is enabled */
553 if (handshake(ehci, &hw->port_status[0], PORT_PE,
554 PORT_PE, 2000)) {
555 pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
556 goto restart;
557 }
558
559 /* Clear the PCI status, to avoid an interrupt taken upon resume */
560 val = readl(&hw->status);
561 val |= STS_PCD;
562 writel(val, &hw->status);
563
564 /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
565 val = readl(&hw->port_status[0]);
566 if ((val & PORT_POWER) && (val & PORT_PE)) {
567 val |= PORT_SUSPEND;
568 writel(val, &hw->port_status[0]);
569
570 /* Wait until port suspend completes */
571 if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
572 PORT_SUSPEND, 1000)) {
573 pr_err("%s: timeout waiting for PORT_SUSPEND\n",
574 __func__);
575 goto restart;
576 }
577 }
578
579 tegra_ehci_phy_restore_end(tegra->phy);
580 goto done;
581
582 restart:
583 if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
584 tegra_ehci_phy_restore_end(tegra->phy);
585
586 tegra_ehci_restart(hcd);
587
588 done:
589 tegra_usb_phy_preresume(tegra->phy);
590 tegra->port_resuming = 1;
591 return 0;
592}
593
594static int tegra_ehci_suspend(struct device *dev)
595{
596 struct tegra_ehci_hcd *tegra =
597 platform_get_drvdata(to_platform_device(dev));
598 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
599 int rc = 0;
600
601 /*
602 * When system sleep is supported and USB controller wakeup is
603 * implemented: If the controller is runtime-suspended and the
604 * wakeup setting needs to be changed, call pm_runtime_resume().
605 */
606 if (HCD_HW_ACCESSIBLE(hcd))
607 rc = controller_suspend(dev);
608 return rc;
609}
610
611static int tegra_ehci_resume(struct device *dev)
612{
613 int rc;
614
615 rc = controller_resume(dev);
616 if (rc == 0) {
617 pm_runtime_disable(dev);
618 pm_runtime_set_active(dev);
619 pm_runtime_enable(dev);
620 }
621 return rc;
622}
623
624static int tegra_ehci_runtime_suspend(struct device *dev)
625{
626 return controller_suspend(dev);
627}
628
629static int tegra_ehci_runtime_resume(struct device *dev)
630{
631 return controller_resume(dev);
632}
633
634static const struct dev_pm_ops tegra_ehci_pm_ops = {
635 .suspend = tegra_ehci_suspend,
636 .resume = tegra_ehci_resume,
637 .runtime_suspend = tegra_ehci_runtime_suspend,
638 .runtime_resume = tegra_ehci_runtime_resume,
639};
640
641#endif
642
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000643static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
644
Benoit Goby79ad3b52011-03-09 16:28:56 -0800645static int tegra_ehci_probe(struct platform_device *pdev)
646{
647 struct resource *res;
648 struct usb_hcd *hcd;
649 struct tegra_ehci_hcd *tegra;
650 struct tegra_ehci_platform_data *pdata;
651 int err = 0;
652 int irq;
653 int instance = pdev->id;
654
655 pdata = pdev->dev.platform_data;
656 if (!pdata) {
657 dev_err(&pdev->dev, "Platform data missing\n");
658 return -EINVAL;
659 }
660
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000661 /* Right now device-tree probed devices don't get dma_mask set.
662 * Since shared usb code relies on it, set it here for now.
663 * Once we have dma capability bindings this can go away.
664 */
665 if (!pdev->dev.dma_mask)
666 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
667
Stephen Warren434103a2012-03-16 16:06:07 -0600668 setup_vbus_gpio(pdev, pdata);
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000669
Benoit Goby79ad3b52011-03-09 16:28:56 -0800670 tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL);
671 if (!tegra)
672 return -ENOMEM;
673
674 hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
675 dev_name(&pdev->dev));
676 if (!hcd) {
677 dev_err(&pdev->dev, "Unable to create HCD\n");
678 err = -ENOMEM;
679 goto fail_hcd;
680 }
681
682 platform_set_drvdata(pdev, tegra);
683
684 tegra->clk = clk_get(&pdev->dev, NULL);
685 if (IS_ERR(tegra->clk)) {
686 dev_err(&pdev->dev, "Can't get ehci clock\n");
687 err = PTR_ERR(tegra->clk);
688 goto fail_clk;
689 }
690
691 err = clk_enable(tegra->clk);
692 if (err)
693 goto fail_clken;
694
695 tegra->emc_clk = clk_get(&pdev->dev, "emc");
696 if (IS_ERR(tegra->emc_clk)) {
697 dev_err(&pdev->dev, "Can't get emc clock\n");
698 err = PTR_ERR(tegra->emc_clk);
699 goto fail_emc_clk;
700 }
701
702 clk_enable(tegra->emc_clk);
703 clk_set_rate(tegra->emc_clk, 400000000);
704
705 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706 if (!res) {
707 dev_err(&pdev->dev, "Failed to get I/O memory\n");
708 err = -ENXIO;
709 goto fail_io;
710 }
711 hcd->rsrc_start = res->start;
712 hcd->rsrc_len = resource_size(res);
713 hcd->regs = ioremap(res->start, resource_size(res));
714 if (!hcd->regs) {
715 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
716 err = -ENOMEM;
717 goto fail_io;
718 }
719
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000720 /* This is pretty ugly and needs to be fixed when we do only
721 * device-tree probing. Old code relies on the platform_device
722 * numbering that we lack for device-tree-instantiated devices.
723 */
724 if (instance < 0) {
725 switch (res->start) {
726 case TEGRA_USB_BASE:
727 instance = 0;
728 break;
729 case TEGRA_USB2_BASE:
730 instance = 1;
731 break;
732 case TEGRA_USB3_BASE:
733 instance = 2;
734 break;
735 default:
736 err = -ENODEV;
737 dev_err(&pdev->dev, "unknown usb instance\n");
738 goto fail_phy;
739 }
740 }
741
Stephen Warrenaa607eb2012-04-12 15:46:49 -0600742 tegra->phy = tegra_usb_phy_open(&pdev->dev, instance, hcd->regs,
743 pdata->phy_config,
744 TEGRA_USB_PHY_MODE_HOST);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800745 if (IS_ERR(tegra->phy)) {
746 dev_err(&pdev->dev, "Failed to open USB phy\n");
747 err = -ENXIO;
748 goto fail_phy;
749 }
750
751 err = tegra_usb_phy_power_on(tegra->phy);
752 if (err) {
753 dev_err(&pdev->dev, "Failed to power on the phy\n");
754 goto fail;
755 }
756
757 tegra->host_resumed = 1;
Benoit Goby79ad3b52011-03-09 16:28:56 -0800758 tegra->ehci = hcd_to_ehci(hcd);
759
760 irq = platform_get_irq(pdev, 0);
761 if (!irq) {
762 dev_err(&pdev->dev, "Failed to get IRQ\n");
763 err = -ENODEV;
764 goto fail;
765 }
Benoit Goby79ad3b52011-03-09 16:28:56 -0800766
767#ifdef CONFIG_USB_OTG_UTILS
768 if (pdata->operating_mode == TEGRA_USB_OTG) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200769 tegra->transceiver = usb_get_transceiver();
Benoit Goby79ad3b52011-03-09 16:28:56 -0800770 if (tegra->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200771 otg_set_host(tegra->transceiver->otg, &hcd->self);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800772 }
773#endif
774
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800775 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800776 if (err) {
777 dev_err(&pdev->dev, "Failed to add USB HCD\n");
778 goto fail;
779 }
780
Alan Sternebf20de2012-05-01 11:28:49 -0400781 pm_runtime_set_active(&pdev->dev);
782 pm_runtime_get_noresume(&pdev->dev);
783
784 /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
785 /* if (!pdata->power_down_on_bus_suspend) */
786 pm_runtime_forbid(&pdev->dev);
787 pm_runtime_enable(&pdev->dev);
788 pm_runtime_put_sync(&pdev->dev);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800789 return err;
790
791fail:
792#ifdef CONFIG_USB_OTG_UTILS
793 if (tegra->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200794 otg_set_host(tegra->transceiver->otg, NULL);
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200795 usb_put_transceiver(tegra->transceiver);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800796 }
797#endif
798 tegra_usb_phy_close(tegra->phy);
799fail_phy:
800 iounmap(hcd->regs);
801fail_io:
802 clk_disable(tegra->emc_clk);
803 clk_put(tegra->emc_clk);
804fail_emc_clk:
805 clk_disable(tegra->clk);
806fail_clken:
807 clk_put(tegra->clk);
808fail_clk:
809 usb_put_hcd(hcd);
810fail_hcd:
811 kfree(tegra);
812 return err;
813}
814
Benoit Goby79ad3b52011-03-09 16:28:56 -0800815static int tegra_ehci_remove(struct platform_device *pdev)
816{
817 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
818 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
819
820 if (tegra == NULL || hcd == NULL)
821 return -EINVAL;
822
Alan Sternebf20de2012-05-01 11:28:49 -0400823 pm_runtime_get_sync(&pdev->dev);
824 pm_runtime_disable(&pdev->dev);
825 pm_runtime_put_noidle(&pdev->dev);
826
Benoit Goby79ad3b52011-03-09 16:28:56 -0800827#ifdef CONFIG_USB_OTG_UTILS
828 if (tegra->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200829 otg_set_host(tegra->transceiver->otg, NULL);
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200830 usb_put_transceiver(tegra->transceiver);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800831 }
832#endif
833
834 usb_remove_hcd(hcd);
835 usb_put_hcd(hcd);
836
837 tegra_usb_phy_close(tegra->phy);
838 iounmap(hcd->regs);
839
840 clk_disable(tegra->clk);
841 clk_put(tegra->clk);
842
843 clk_disable(tegra->emc_clk);
844 clk_put(tegra->emc_clk);
845
846 kfree(tegra);
847 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
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000859static struct of_device_id tegra_ehci_of_match[] __devinitdata = {
860 { .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};