blob: 9f817680ff04fdbb797389a4254b644c1c9190ff [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>
27
Benoit Goby79ad3b52011-03-09 16:28:56 -080028#include <mach/usb_phy.h>
Olof Johansson4a53f4e2011-11-04 09:12:40 +000029#include <mach/iomap.h>
Benoit Goby79ad3b52011-03-09 16:28:56 -080030
Robert Morellfbf98652011-03-09 16:28:57 -080031#define TEGRA_USB_DMA_ALIGN 32
32
Benoit Goby79ad3b52011-03-09 16:28:56 -080033struct tegra_ehci_hcd {
34 struct ehci_hcd *ehci;
35 struct tegra_usb_phy *phy;
36 struct clk *clk;
37 struct clk *emc_clk;
Heikki Krogerus86753812012-02-13 13:24:02 +020038 struct usb_phy *transceiver;
Benoit Goby79ad3b52011-03-09 16:28:56 -080039 int host_resumed;
40 int bus_suspended;
41 int port_resuming;
42 int power_down_on_bus_suspend;
43 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
167 temp &= ~PORT_WKCONN_E;
168 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);
216
217 spin_unlock_irqrestore(&ehci->lock, flags);
218 msleep(20);
219 spin_lock_irqsave(&ehci->lock, flags);
220
221 /* Poll until the controller clears RESUME and SUSPEND */
222 if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
223 pr_err("%s: timeout waiting for RESUME\n", __func__);
224 if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
225 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
226
227 ehci->reset_done[wIndex-1] = 0;
228
229 tegra->port_resuming = 1;
230 goto done;
231 }
232
233 spin_unlock_irqrestore(&ehci->lock, flags);
234
235 /* Handle the hub control events here */
236 return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
237done:
238 spin_unlock_irqrestore(&ehci->lock, flags);
239 return retval;
240}
241
242static void tegra_ehci_restart(struct usb_hcd *hcd)
243{
244 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
245
246 ehci_reset(ehci);
247
248 /* setup the frame list and Async q heads */
249 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
250 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
251 /* setup the command register and set the controller in RUN mode */
252 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
253 ehci->command |= CMD_RUN;
254 ehci_writel(ehci, ehci->command, &ehci->regs->command);
255
256 down_write(&ehci_cf_port_reset_rwsem);
257 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
258 /* flush posted writes */
259 ehci_readl(ehci, &ehci->regs->command);
260 up_write(&ehci_cf_port_reset_rwsem);
261}
262
263static int tegra_usb_suspend(struct usb_hcd *hcd)
264{
265 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
266 struct ehci_regs __iomem *hw = tegra->ehci->regs;
267 unsigned long flags;
268
269 spin_lock_irqsave(&tegra->ehci->lock, flags);
270
271 tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
272 ehci_halt(tegra->ehci);
273 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
274
275 spin_unlock_irqrestore(&tegra->ehci->lock, flags);
276
277 tegra_ehci_power_down(hcd);
278 return 0;
279}
280
281static int tegra_usb_resume(struct usb_hcd *hcd)
282{
283 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
284 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
285 struct ehci_regs __iomem *hw = ehci->regs;
286 unsigned long val;
287
288 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
289 tegra_ehci_power_up(hcd);
290
291 if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
292 /* Wait for the phy to detect new devices
293 * before we restart the controller */
294 msleep(10);
295 goto restart;
296 }
297
298 /* Force the phy to keep data lines in suspend state */
299 tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
300
301 /* Enable host mode */
302 tdi_reset(ehci);
303
304 /* Enable Port Power */
305 val = readl(&hw->port_status[0]);
306 val |= PORT_POWER;
307 writel(val, &hw->port_status[0]);
308 udelay(10);
309
310 /* Check if the phy resume from LP0. When the phy resume from LP0
311 * USB register will be reset. */
312 if (!readl(&hw->async_next)) {
313 /* Program the field PTC based on the saved speed mode */
314 val = readl(&hw->port_status[0]);
315 val &= ~PORT_TEST(~0);
316 if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
317 val |= PORT_TEST_FORCE;
318 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
319 val |= PORT_TEST(6);
320 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
321 val |= PORT_TEST(7);
322 writel(val, &hw->port_status[0]);
323 udelay(10);
324
325 /* Disable test mode by setting PTC field to NORMAL_OP */
326 val = readl(&hw->port_status[0]);
327 val &= ~PORT_TEST(~0);
328 writel(val, &hw->port_status[0]);
329 udelay(10);
330 }
331
332 /* Poll until CCS is enabled */
333 if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
334 PORT_CONNECT, 2000)) {
335 pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
336 goto restart;
337 }
338
339 /* Poll until PE is enabled */
340 if (handshake(ehci, &hw->port_status[0], PORT_PE,
341 PORT_PE, 2000)) {
342 pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
343 goto restart;
344 }
345
346 /* Clear the PCI status, to avoid an interrupt taken upon resume */
347 val = readl(&hw->status);
348 val |= STS_PCD;
349 writel(val, &hw->status);
350
351 /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
352 val = readl(&hw->port_status[0]);
353 if ((val & PORT_POWER) && (val & PORT_PE)) {
354 val |= PORT_SUSPEND;
355 writel(val, &hw->port_status[0]);
356
357 /* Wait until port suspend completes */
358 if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
359 PORT_SUSPEND, 1000)) {
360 pr_err("%s: timeout waiting for PORT_SUSPEND\n",
361 __func__);
362 goto restart;
363 }
364 }
365
366 tegra_ehci_phy_restore_end(tegra->phy);
367 return 0;
368
369restart:
370 if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
371 tegra_ehci_phy_restore_end(tegra->phy);
372
373 tegra_ehci_restart(hcd);
374 return 0;
375}
376
377static void tegra_ehci_shutdown(struct usb_hcd *hcd)
378{
379 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
380
381 /* ehci_shutdown touches the USB controller registers, make sure
382 * controller has clocks to it */
383 if (!tegra->host_resumed)
384 tegra_ehci_power_up(hcd);
385
386 ehci_shutdown(hcd);
387}
388
389static int tegra_ehci_setup(struct usb_hcd *hcd)
390{
391 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
392 int retval;
393
394 /* EHCI registers start at offset 0x100 */
395 ehci->caps = hcd->regs + 0x100;
396 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200397 HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase));
Benoit Goby79ad3b52011-03-09 16:28:56 -0800398
399 dbg_hcs_params(ehci, "reset");
400 dbg_hcc_params(ehci, "reset");
401
402 /* cache this readonly data; minimize chip reads */
403 ehci->hcs_params = readl(&ehci->caps->hcs_params);
404
405 /* switch to host mode */
406 hcd->has_tt = 1;
407 ehci_reset(ehci);
408
409 retval = ehci_halt(ehci);
410 if (retval)
411 return retval;
412
413 /* data structure init */
414 retval = ehci_init(hcd);
415 if (retval)
416 return retval;
417
418 ehci->sbrn = 0x20;
419
420 ehci_port_power(ehci, 1);
421 return retval;
422}
423
424#ifdef CONFIG_PM
425static int tegra_ehci_bus_suspend(struct usb_hcd *hcd)
426{
427 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
428 int error_status = 0;
429
430 error_status = ehci_bus_suspend(hcd);
431 if (!error_status && tegra->power_down_on_bus_suspend) {
432 tegra_usb_suspend(hcd);
433 tegra->bus_suspended = 1;
434 }
435
436 return error_status;
437}
438
439static int tegra_ehci_bus_resume(struct usb_hcd *hcd)
440{
441 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
442
443 if (tegra->bus_suspended && tegra->power_down_on_bus_suspend) {
444 tegra_usb_resume(hcd);
445 tegra->bus_suspended = 0;
446 }
447
448 tegra_usb_phy_preresume(tegra->phy);
449 tegra->port_resuming = 1;
450 return ehci_bus_resume(hcd);
451}
452#endif
453
Venu Byravarasufe375772012-04-05 11:25:30 +0530454struct dma_aligned_buffer {
Robert Morellfbf98652011-03-09 16:28:57 -0800455 void *kmalloc_ptr;
456 void *old_xfer_buffer;
457 u8 data[0];
458};
459
Venu Byravarasufe375772012-04-05 11:25:30 +0530460static void free_dma_aligned_buffer(struct urb *urb)
Robert Morellfbf98652011-03-09 16:28:57 -0800461{
Venu Byravarasufe375772012-04-05 11:25:30 +0530462 struct dma_aligned_buffer *temp;
Robert Morellfbf98652011-03-09 16:28:57 -0800463
464 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
465 return;
466
Venu Byravarasufe375772012-04-05 11:25:30 +0530467 temp = container_of(urb->transfer_buffer,
468 struct dma_aligned_buffer, data);
Robert Morellfbf98652011-03-09 16:28:57 -0800469
Venu Byravarasufe375772012-04-05 11:25:30 +0530470 if (usb_urb_dir_in(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800471 memcpy(temp->old_xfer_buffer, temp->data,
472 urb->transfer_buffer_length);
473 urb->transfer_buffer = temp->old_xfer_buffer;
474 kfree(temp->kmalloc_ptr);
475
476 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
477}
478
Venu Byravarasufe375772012-04-05 11:25:30 +0530479static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
Robert Morellfbf98652011-03-09 16:28:57 -0800480{
Venu Byravarasufe375772012-04-05 11:25:30 +0530481 struct dma_aligned_buffer *temp, *kmalloc_ptr;
Robert Morellfbf98652011-03-09 16:28:57 -0800482 size_t kmalloc_size;
483
484 if (urb->num_sgs || urb->sg ||
485 urb->transfer_buffer_length == 0 ||
486 !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
487 return 0;
488
Robert Morellfbf98652011-03-09 16:28:57 -0800489 /* Allocate a buffer with enough padding for alignment */
490 kmalloc_size = urb->transfer_buffer_length +
Venu Byravarasufe375772012-04-05 11:25:30 +0530491 sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800492
493 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
494 if (!kmalloc_ptr)
495 return -ENOMEM;
496
Venu Byravarasufe375772012-04-05 11:25:30 +0530497 /* Position our struct dma_aligned_buffer such that data is aligned */
Robert Morellfbf98652011-03-09 16:28:57 -0800498 temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
Robert Morellfbf98652011-03-09 16:28:57 -0800499 temp->kmalloc_ptr = kmalloc_ptr;
500 temp->old_xfer_buffer = urb->transfer_buffer;
Venu Byravarasufe375772012-04-05 11:25:30 +0530501 if (usb_urb_dir_out(urb))
Robert Morellfbf98652011-03-09 16:28:57 -0800502 memcpy(temp->data, urb->transfer_buffer,
503 urb->transfer_buffer_length);
504 urb->transfer_buffer = temp->data;
505
506 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
507
508 return 0;
509}
510
511static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
512 gfp_t mem_flags)
513{
514 int ret;
515
Venu Byravarasufe375772012-04-05 11:25:30 +0530516 ret = alloc_dma_aligned_buffer(urb, mem_flags);
Robert Morellfbf98652011-03-09 16:28:57 -0800517 if (ret)
518 return ret;
519
520 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
521 if (ret)
Venu Byravarasufe375772012-04-05 11:25:30 +0530522 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800523
524 return ret;
525}
526
527static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
528{
529 usb_hcd_unmap_urb_for_dma(hcd, urb);
Venu Byravarasufe375772012-04-05 11:25:30 +0530530 free_dma_aligned_buffer(urb);
Robert Morellfbf98652011-03-09 16:28:57 -0800531}
532
Benoit Goby79ad3b52011-03-09 16:28:56 -0800533static const struct hc_driver tegra_ehci_hc_driver = {
534 .description = hcd_name,
535 .product_desc = "Tegra EHCI Host Controller",
536 .hcd_priv_size = sizeof(struct ehci_hcd),
Benoit Goby79ad3b52011-03-09 16:28:56 -0800537 .flags = HCD_USB2 | HCD_MEMORY,
538
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530539 /* standard ehci functions */
Benoit Goby79ad3b52011-03-09 16:28:56 -0800540 .irq = ehci_irq,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800541 .start = ehci_run,
542 .stop = ehci_stop,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800543 .urb_enqueue = ehci_urb_enqueue,
544 .urb_dequeue = ehci_urb_dequeue,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800545 .endpoint_disable = ehci_endpoint_disable,
546 .endpoint_reset = ehci_endpoint_reset,
547 .get_frame_number = ehci_get_frame,
548 .hub_status_data = ehci_hub_status_data,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800549 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Venu Byravarasuc6fa0b42012-04-06 09:40:18 +0530550 .relinquish_port = ehci_relinquish_port,
551 .port_handed_over = ehci_port_handed_over,
552
553 /* modified ehci functions for tegra */
554 .reset = tegra_ehci_setup,
555 .shutdown = tegra_ehci_shutdown,
556 .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
557 .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
558 .hub_control = tegra_ehci_hub_control,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800559#ifdef CONFIG_PM
560 .bus_suspend = tegra_ehci_bus_suspend,
561 .bus_resume = tegra_ehci_bus_resume,
562#endif
Benoit Goby79ad3b52011-03-09 16:28:56 -0800563};
564
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000565static int setup_vbus_gpio(struct platform_device *pdev)
566{
567 int err = 0;
568 int gpio;
569
570 if (!pdev->dev.of_node)
571 return 0;
572
573 gpio = of_get_named_gpio(pdev->dev.of_node, "nvidia,vbus-gpio", 0);
574 if (!gpio_is_valid(gpio))
575 return 0;
576
577 err = gpio_request(gpio, "vbus_gpio");
578 if (err) {
579 dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
580 return err;
581 }
582 err = gpio_direction_output(gpio, 1);
583 if (err) {
584 dev_err(&pdev->dev, "can't enable vbus\n");
585 return err;
586 }
587 gpio_set_value(gpio, 1);
588
589 return err;
590}
591
592static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
593
Benoit Goby79ad3b52011-03-09 16:28:56 -0800594static int tegra_ehci_probe(struct platform_device *pdev)
595{
596 struct resource *res;
597 struct usb_hcd *hcd;
598 struct tegra_ehci_hcd *tegra;
599 struct tegra_ehci_platform_data *pdata;
600 int err = 0;
601 int irq;
602 int instance = pdev->id;
603
604 pdata = pdev->dev.platform_data;
605 if (!pdata) {
606 dev_err(&pdev->dev, "Platform data missing\n");
607 return -EINVAL;
608 }
609
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000610 /* Right now device-tree probed devices don't get dma_mask set.
611 * Since shared usb code relies on it, set it here for now.
612 * Once we have dma capability bindings this can go away.
613 */
614 if (!pdev->dev.dma_mask)
615 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
616
617 setup_vbus_gpio(pdev);
618
Benoit Goby79ad3b52011-03-09 16:28:56 -0800619 tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL);
620 if (!tegra)
621 return -ENOMEM;
622
623 hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
624 dev_name(&pdev->dev));
625 if (!hcd) {
626 dev_err(&pdev->dev, "Unable to create HCD\n");
627 err = -ENOMEM;
628 goto fail_hcd;
629 }
630
631 platform_set_drvdata(pdev, tegra);
632
633 tegra->clk = clk_get(&pdev->dev, NULL);
634 if (IS_ERR(tegra->clk)) {
635 dev_err(&pdev->dev, "Can't get ehci clock\n");
636 err = PTR_ERR(tegra->clk);
637 goto fail_clk;
638 }
639
640 err = clk_enable(tegra->clk);
641 if (err)
642 goto fail_clken;
643
644 tegra->emc_clk = clk_get(&pdev->dev, "emc");
645 if (IS_ERR(tegra->emc_clk)) {
646 dev_err(&pdev->dev, "Can't get emc clock\n");
647 err = PTR_ERR(tegra->emc_clk);
648 goto fail_emc_clk;
649 }
650
651 clk_enable(tegra->emc_clk);
652 clk_set_rate(tegra->emc_clk, 400000000);
653
654 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
655 if (!res) {
656 dev_err(&pdev->dev, "Failed to get I/O memory\n");
657 err = -ENXIO;
658 goto fail_io;
659 }
660 hcd->rsrc_start = res->start;
661 hcd->rsrc_len = resource_size(res);
662 hcd->regs = ioremap(res->start, resource_size(res));
663 if (!hcd->regs) {
664 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
665 err = -ENOMEM;
666 goto fail_io;
667 }
668
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000669 /* This is pretty ugly and needs to be fixed when we do only
670 * device-tree probing. Old code relies on the platform_device
671 * numbering that we lack for device-tree-instantiated devices.
672 */
673 if (instance < 0) {
674 switch (res->start) {
675 case TEGRA_USB_BASE:
676 instance = 0;
677 break;
678 case TEGRA_USB2_BASE:
679 instance = 1;
680 break;
681 case TEGRA_USB3_BASE:
682 instance = 2;
683 break;
684 default:
685 err = -ENODEV;
686 dev_err(&pdev->dev, "unknown usb instance\n");
687 goto fail_phy;
688 }
689 }
690
Benoit Goby79ad3b52011-03-09 16:28:56 -0800691 tegra->phy = tegra_usb_phy_open(instance, hcd->regs, pdata->phy_config,
692 TEGRA_USB_PHY_MODE_HOST);
693 if (IS_ERR(tegra->phy)) {
694 dev_err(&pdev->dev, "Failed to open USB phy\n");
695 err = -ENXIO;
696 goto fail_phy;
697 }
698
699 err = tegra_usb_phy_power_on(tegra->phy);
700 if (err) {
701 dev_err(&pdev->dev, "Failed to power on the phy\n");
702 goto fail;
703 }
704
705 tegra->host_resumed = 1;
706 tegra->power_down_on_bus_suspend = pdata->power_down_on_bus_suspend;
707 tegra->ehci = hcd_to_ehci(hcd);
708
709 irq = platform_get_irq(pdev, 0);
710 if (!irq) {
711 dev_err(&pdev->dev, "Failed to get IRQ\n");
712 err = -ENODEV;
713 goto fail;
714 }
715 set_irq_flags(irq, IRQF_VALID);
716
717#ifdef CONFIG_USB_OTG_UTILS
718 if (pdata->operating_mode == TEGRA_USB_OTG) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200719 tegra->transceiver = usb_get_transceiver();
Benoit Goby79ad3b52011-03-09 16:28:56 -0800720 if (tegra->transceiver)
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200721 otg_set_host(tegra->transceiver->otg, &hcd->self);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800722 }
723#endif
724
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800725 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800726 if (err) {
727 dev_err(&pdev->dev, "Failed to add USB HCD\n");
728 goto fail;
729 }
730
731 return err;
732
733fail:
734#ifdef CONFIG_USB_OTG_UTILS
735 if (tegra->transceiver) {
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200736 otg_set_host(tegra->transceiver->otg, NULL);
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200737 usb_put_transceiver(tegra->transceiver);
Benoit Goby79ad3b52011-03-09 16:28:56 -0800738 }
739#endif
740 tegra_usb_phy_close(tegra->phy);
741fail_phy:
742 iounmap(hcd->regs);
743fail_io:
744 clk_disable(tegra->emc_clk);
745 clk_put(tegra->emc_clk);
746fail_emc_clk:
747 clk_disable(tegra->clk);
748fail_clken:
749 clk_put(tegra->clk);
750fail_clk:
751 usb_put_hcd(hcd);
752fail_hcd:
753 kfree(tegra);
754 return err;
755}
756
757#ifdef CONFIG_PM
758static int tegra_ehci_resume(struct platform_device *pdev)
759{
760 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
761 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
762
763 if (tegra->bus_suspended)
764 return 0;
765
766 return tegra_usb_resume(hcd);
767}
768
769static int tegra_ehci_suspend(struct platform_device *pdev, pm_message_t state)
770{
771 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
772 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
773
774 if (tegra->bus_suspended)
775 return 0;
776
777 if (time_before(jiffies, tegra->ehci->next_statechange))
778 msleep(10);
779
780 return tegra_usb_suspend(hcd);
781}
782#endif
783
784static int tegra_ehci_remove(struct platform_device *pdev)
785{
786 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
787 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
788
789 if (tegra == NULL || hcd == NULL)
790 return -EINVAL;
791
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
799 usb_remove_hcd(hcd);
800 usb_put_hcd(hcd);
801
802 tegra_usb_phy_close(tegra->phy);
803 iounmap(hcd->regs);
804
805 clk_disable(tegra->clk);
806 clk_put(tegra->clk);
807
808 clk_disable(tegra->emc_clk);
809 clk_put(tegra->emc_clk);
810
811 kfree(tegra);
812 return 0;
813}
814
815static void tegra_ehci_hcd_shutdown(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 (hcd->driver->shutdown)
821 hcd->driver->shutdown(hcd);
822}
823
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000824static struct of_device_id tegra_ehci_of_match[] __devinitdata = {
825 { .compatible = "nvidia,tegra20-ehci", },
826 { },
827};
828
Benoit Goby79ad3b52011-03-09 16:28:56 -0800829static struct platform_driver tegra_ehci_driver = {
830 .probe = tegra_ehci_probe,
831 .remove = tegra_ehci_remove,
832#ifdef CONFIG_PM
833 .suspend = tegra_ehci_suspend,
834 .resume = tegra_ehci_resume,
835#endif
836 .shutdown = tegra_ehci_hcd_shutdown,
837 .driver = {
838 .name = "tegra-ehci",
Olof Johansson4a53f4e2011-11-04 09:12:40 +0000839 .of_match_table = tegra_ehci_of_match,
Benoit Goby79ad3b52011-03-09 16:28:56 -0800840 }
841};