Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 1 | /* |
| 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 Johansson | 4a53f4e | 2011-11-04 09:12:40 +0000 | [diff] [blame] | 24 | #include <linux/gpio.h> |
| 25 | #include <linux/of.h> |
| 26 | #include <linux/of_gpio.h> |
| 27 | |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 28 | #include <mach/usb_phy.h> |
Olof Johansson | 4a53f4e | 2011-11-04 09:12:40 +0000 | [diff] [blame] | 29 | #include <mach/iomap.h> |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 30 | |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 31 | #define TEGRA_USB_DMA_ALIGN 32 |
| 32 | |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 33 | struct tegra_ehci_hcd { |
| 34 | struct ehci_hcd *ehci; |
| 35 | struct tegra_usb_phy *phy; |
| 36 | struct clk *clk; |
| 37 | struct clk *emc_clk; |
Heikki Krogerus | 8675381 | 2012-02-13 13:24:02 +0200 | [diff] [blame] | 38 | struct usb_phy *transceiver; |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 39 | 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 | |
| 46 | static 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 | |
| 56 | static 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 Lin | 1f594b6 | 2011-04-17 11:58:25 +0300 | [diff] [blame] | 66 | static 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 Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 131 | static 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 Warren | 6d5f89c | 2012-04-18 15:32:46 -0600 | [diff] [blame] | 151 | if (typeReq == GetPortStatus) { |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 152 | temp = ehci_readl(ehci, status_reg); |
| 153 | if (tegra->port_resuming && !(temp & PORT_SUSPEND)) { |
| 154 | /* Resume completed, re-enable disconnect detection */ |
| 155 | tegra->port_resuming = 0; |
| 156 | tegra_usb_phy_postresume(tegra->phy); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) { |
| 161 | temp = ehci_readl(ehci, status_reg); |
| 162 | if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) { |
| 163 | retval = -EPIPE; |
| 164 | goto done; |
| 165 | } |
| 166 | |
Stephen Warren | b087657 | 2012-04-25 12:31:10 -0600 | [diff] [blame^] | 167 | temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 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 Lin | 1f594b6 | 2011-04-17 11:58:25 +0300 | [diff] [blame] | 183 | /* 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 Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 190 | /* |
| 191 | * Tegra host controller will time the resume operation to clear the bit |
| 192 | * when the port control state switches to HS or FS Idle. This behavior |
| 193 | * is different from EHCI where the host controller driver is required |
| 194 | * to set this bit to a zero after the resume duration is timed in the |
| 195 | * driver. |
| 196 | */ |
| 197 | else if (typeReq == ClearPortFeature && |
| 198 | wValue == USB_PORT_FEAT_SUSPEND) { |
| 199 | temp = ehci_readl(ehci, status_reg); |
| 200 | if ((temp & PORT_RESET) || !(temp & PORT_PE)) { |
| 201 | retval = -EPIPE; |
| 202 | goto done; |
| 203 | } |
| 204 | |
| 205 | if (!(temp & PORT_SUSPEND)) |
| 206 | goto done; |
| 207 | |
| 208 | /* Disable disconnect detection during port resume */ |
| 209 | tegra_usb_phy_preresume(tegra->phy); |
| 210 | |
| 211 | ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25); |
| 212 | |
| 213 | temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS); |
| 214 | /* start resume signalling */ |
| 215 | ehci_writel(ehci, temp | PORT_RESUME, status_reg); |
Alan Stern | a448e4d | 2012-04-03 15:24:30 -0400 | [diff] [blame] | 216 | set_bit(wIndex-1, &ehci->resuming_ports); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 217 | |
| 218 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 219 | msleep(20); |
| 220 | spin_lock_irqsave(&ehci->lock, flags); |
| 221 | |
| 222 | /* Poll until the controller clears RESUME and SUSPEND */ |
| 223 | if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000)) |
| 224 | pr_err("%s: timeout waiting for RESUME\n", __func__); |
| 225 | if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000)) |
| 226 | pr_err("%s: timeout waiting for SUSPEND\n", __func__); |
| 227 | |
| 228 | ehci->reset_done[wIndex-1] = 0; |
Alan Stern | a448e4d | 2012-04-03 15:24:30 -0400 | [diff] [blame] | 229 | clear_bit(wIndex-1, &ehci->resuming_ports); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 230 | |
| 231 | tegra->port_resuming = 1; |
| 232 | goto done; |
| 233 | } |
| 234 | |
| 235 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 236 | |
| 237 | /* Handle the hub control events here */ |
| 238 | return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength); |
| 239 | done: |
| 240 | spin_unlock_irqrestore(&ehci->lock, flags); |
| 241 | return retval; |
| 242 | } |
| 243 | |
| 244 | static void tegra_ehci_restart(struct usb_hcd *hcd) |
| 245 | { |
| 246 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 247 | |
| 248 | ehci_reset(ehci); |
| 249 | |
| 250 | /* setup the frame list and Async q heads */ |
| 251 | ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list); |
| 252 | ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next); |
| 253 | /* setup the command register and set the controller in RUN mode */ |
| 254 | ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET); |
| 255 | ehci->command |= CMD_RUN; |
| 256 | ehci_writel(ehci, ehci->command, &ehci->regs->command); |
| 257 | |
| 258 | down_write(&ehci_cf_port_reset_rwsem); |
| 259 | ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag); |
| 260 | /* flush posted writes */ |
| 261 | ehci_readl(ehci, &ehci->regs->command); |
| 262 | up_write(&ehci_cf_port_reset_rwsem); |
| 263 | } |
| 264 | |
| 265 | static int tegra_usb_suspend(struct usb_hcd *hcd) |
| 266 | { |
| 267 | struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller); |
| 268 | struct ehci_regs __iomem *hw = tegra->ehci->regs; |
| 269 | unsigned long flags; |
| 270 | |
| 271 | spin_lock_irqsave(&tegra->ehci->lock, flags); |
| 272 | |
| 273 | tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3; |
| 274 | ehci_halt(tegra->ehci); |
| 275 | clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
| 276 | |
| 277 | spin_unlock_irqrestore(&tegra->ehci->lock, flags); |
| 278 | |
| 279 | tegra_ehci_power_down(hcd); |
| 280 | return 0; |
| 281 | } |
| 282 | |
| 283 | static int tegra_usb_resume(struct usb_hcd *hcd) |
| 284 | { |
| 285 | struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller); |
| 286 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 287 | struct ehci_regs __iomem *hw = ehci->regs; |
| 288 | unsigned long val; |
| 289 | |
| 290 | set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags); |
| 291 | tegra_ehci_power_up(hcd); |
| 292 | |
| 293 | if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) { |
| 294 | /* Wait for the phy to detect new devices |
| 295 | * before we restart the controller */ |
| 296 | msleep(10); |
| 297 | goto restart; |
| 298 | } |
| 299 | |
| 300 | /* Force the phy to keep data lines in suspend state */ |
| 301 | tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed); |
| 302 | |
| 303 | /* Enable host mode */ |
| 304 | tdi_reset(ehci); |
| 305 | |
| 306 | /* Enable Port Power */ |
| 307 | val = readl(&hw->port_status[0]); |
| 308 | val |= PORT_POWER; |
| 309 | writel(val, &hw->port_status[0]); |
| 310 | udelay(10); |
| 311 | |
| 312 | /* Check if the phy resume from LP0. When the phy resume from LP0 |
| 313 | * USB register will be reset. */ |
| 314 | if (!readl(&hw->async_next)) { |
| 315 | /* Program the field PTC based on the saved speed mode */ |
| 316 | val = readl(&hw->port_status[0]); |
| 317 | val &= ~PORT_TEST(~0); |
| 318 | if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH) |
| 319 | val |= PORT_TEST_FORCE; |
| 320 | else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL) |
| 321 | val |= PORT_TEST(6); |
| 322 | else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW) |
| 323 | val |= PORT_TEST(7); |
| 324 | writel(val, &hw->port_status[0]); |
| 325 | udelay(10); |
| 326 | |
| 327 | /* Disable test mode by setting PTC field to NORMAL_OP */ |
| 328 | val = readl(&hw->port_status[0]); |
| 329 | val &= ~PORT_TEST(~0); |
| 330 | writel(val, &hw->port_status[0]); |
| 331 | udelay(10); |
| 332 | } |
| 333 | |
| 334 | /* Poll until CCS is enabled */ |
| 335 | if (handshake(ehci, &hw->port_status[0], PORT_CONNECT, |
| 336 | PORT_CONNECT, 2000)) { |
| 337 | pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__); |
| 338 | goto restart; |
| 339 | } |
| 340 | |
| 341 | /* Poll until PE is enabled */ |
| 342 | if (handshake(ehci, &hw->port_status[0], PORT_PE, |
| 343 | PORT_PE, 2000)) { |
| 344 | pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__); |
| 345 | goto restart; |
| 346 | } |
| 347 | |
| 348 | /* Clear the PCI status, to avoid an interrupt taken upon resume */ |
| 349 | val = readl(&hw->status); |
| 350 | val |= STS_PCD; |
| 351 | writel(val, &hw->status); |
| 352 | |
| 353 | /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */ |
| 354 | val = readl(&hw->port_status[0]); |
| 355 | if ((val & PORT_POWER) && (val & PORT_PE)) { |
| 356 | val |= PORT_SUSPEND; |
| 357 | writel(val, &hw->port_status[0]); |
| 358 | |
| 359 | /* Wait until port suspend completes */ |
| 360 | if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND, |
| 361 | PORT_SUSPEND, 1000)) { |
| 362 | pr_err("%s: timeout waiting for PORT_SUSPEND\n", |
| 363 | __func__); |
| 364 | goto restart; |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | tegra_ehci_phy_restore_end(tegra->phy); |
| 369 | return 0; |
| 370 | |
| 371 | restart: |
| 372 | if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH) |
| 373 | tegra_ehci_phy_restore_end(tegra->phy); |
| 374 | |
| 375 | tegra_ehci_restart(hcd); |
| 376 | return 0; |
| 377 | } |
| 378 | |
| 379 | static void tegra_ehci_shutdown(struct usb_hcd *hcd) |
| 380 | { |
| 381 | struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller); |
| 382 | |
| 383 | /* ehci_shutdown touches the USB controller registers, make sure |
| 384 | * controller has clocks to it */ |
| 385 | if (!tegra->host_resumed) |
| 386 | tegra_ehci_power_up(hcd); |
| 387 | |
| 388 | ehci_shutdown(hcd); |
| 389 | } |
| 390 | |
| 391 | static int tegra_ehci_setup(struct usb_hcd *hcd) |
| 392 | { |
| 393 | struct ehci_hcd *ehci = hcd_to_ehci(hcd); |
| 394 | int retval; |
| 395 | |
| 396 | /* EHCI registers start at offset 0x100 */ |
| 397 | ehci->caps = hcd->regs + 0x100; |
| 398 | ehci->regs = hcd->regs + 0x100 + |
Jan Andersson | c430131 | 2011-05-03 20:11:57 +0200 | [diff] [blame] | 399 | HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase)); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 400 | |
| 401 | dbg_hcs_params(ehci, "reset"); |
| 402 | dbg_hcc_params(ehci, "reset"); |
| 403 | |
| 404 | /* cache this readonly data; minimize chip reads */ |
| 405 | ehci->hcs_params = readl(&ehci->caps->hcs_params); |
| 406 | |
| 407 | /* switch to host mode */ |
| 408 | hcd->has_tt = 1; |
| 409 | ehci_reset(ehci); |
| 410 | |
| 411 | retval = ehci_halt(ehci); |
| 412 | if (retval) |
| 413 | return retval; |
| 414 | |
| 415 | /* data structure init */ |
| 416 | retval = ehci_init(hcd); |
| 417 | if (retval) |
| 418 | return retval; |
| 419 | |
| 420 | ehci->sbrn = 0x20; |
| 421 | |
| 422 | ehci_port_power(ehci, 1); |
| 423 | return retval; |
| 424 | } |
| 425 | |
| 426 | #ifdef CONFIG_PM |
| 427 | static int tegra_ehci_bus_suspend(struct usb_hcd *hcd) |
| 428 | { |
| 429 | struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller); |
| 430 | int error_status = 0; |
| 431 | |
| 432 | error_status = ehci_bus_suspend(hcd); |
| 433 | if (!error_status && tegra->power_down_on_bus_suspend) { |
| 434 | tegra_usb_suspend(hcd); |
| 435 | tegra->bus_suspended = 1; |
| 436 | } |
| 437 | |
| 438 | return error_status; |
| 439 | } |
| 440 | |
| 441 | static int tegra_ehci_bus_resume(struct usb_hcd *hcd) |
| 442 | { |
| 443 | struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller); |
| 444 | |
| 445 | if (tegra->bus_suspended && tegra->power_down_on_bus_suspend) { |
| 446 | tegra_usb_resume(hcd); |
| 447 | tegra->bus_suspended = 0; |
| 448 | } |
| 449 | |
| 450 | tegra_usb_phy_preresume(tegra->phy); |
| 451 | tegra->port_resuming = 1; |
| 452 | return ehci_bus_resume(hcd); |
| 453 | } |
| 454 | #endif |
| 455 | |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 456 | struct dma_aligned_buffer { |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 457 | void *kmalloc_ptr; |
| 458 | void *old_xfer_buffer; |
| 459 | u8 data[0]; |
| 460 | }; |
| 461 | |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 462 | static void free_dma_aligned_buffer(struct urb *urb) |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 463 | { |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 464 | struct dma_aligned_buffer *temp; |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 465 | |
| 466 | if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER)) |
| 467 | return; |
| 468 | |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 469 | temp = container_of(urb->transfer_buffer, |
| 470 | struct dma_aligned_buffer, data); |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 471 | |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 472 | if (usb_urb_dir_in(urb)) |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 473 | memcpy(temp->old_xfer_buffer, temp->data, |
| 474 | urb->transfer_buffer_length); |
| 475 | urb->transfer_buffer = temp->old_xfer_buffer; |
| 476 | kfree(temp->kmalloc_ptr); |
| 477 | |
| 478 | urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER; |
| 479 | } |
| 480 | |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 481 | static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags) |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 482 | { |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 483 | struct dma_aligned_buffer *temp, *kmalloc_ptr; |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 484 | size_t kmalloc_size; |
| 485 | |
| 486 | if (urb->num_sgs || urb->sg || |
| 487 | urb->transfer_buffer_length == 0 || |
| 488 | !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1))) |
| 489 | return 0; |
| 490 | |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 491 | /* Allocate a buffer with enough padding for alignment */ |
| 492 | kmalloc_size = urb->transfer_buffer_length + |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 493 | sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1; |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 494 | |
| 495 | kmalloc_ptr = kmalloc(kmalloc_size, mem_flags); |
| 496 | if (!kmalloc_ptr) |
| 497 | return -ENOMEM; |
| 498 | |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 499 | /* Position our struct dma_aligned_buffer such that data is aligned */ |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 500 | temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1; |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 501 | temp->kmalloc_ptr = kmalloc_ptr; |
| 502 | temp->old_xfer_buffer = urb->transfer_buffer; |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 503 | if (usb_urb_dir_out(urb)) |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 504 | memcpy(temp->data, urb->transfer_buffer, |
| 505 | urb->transfer_buffer_length); |
| 506 | urb->transfer_buffer = temp->data; |
| 507 | |
| 508 | urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER; |
| 509 | |
| 510 | return 0; |
| 511 | } |
| 512 | |
| 513 | static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb, |
| 514 | gfp_t mem_flags) |
| 515 | { |
| 516 | int ret; |
| 517 | |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 518 | ret = alloc_dma_aligned_buffer(urb, mem_flags); |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 519 | if (ret) |
| 520 | return ret; |
| 521 | |
| 522 | ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags); |
| 523 | if (ret) |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 524 | free_dma_aligned_buffer(urb); |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 525 | |
| 526 | return ret; |
| 527 | } |
| 528 | |
| 529 | static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb) |
| 530 | { |
| 531 | usb_hcd_unmap_urb_for_dma(hcd, urb); |
Venu Byravarasu | fe37577 | 2012-04-05 11:25:30 +0530 | [diff] [blame] | 532 | free_dma_aligned_buffer(urb); |
Robert Morell | fbf9865 | 2011-03-09 16:28:57 -0800 | [diff] [blame] | 533 | } |
| 534 | |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 535 | static const struct hc_driver tegra_ehci_hc_driver = { |
| 536 | .description = hcd_name, |
| 537 | .product_desc = "Tegra EHCI Host Controller", |
| 538 | .hcd_priv_size = sizeof(struct ehci_hcd), |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 539 | .flags = HCD_USB2 | HCD_MEMORY, |
| 540 | |
Venu Byravarasu | c6fa0b4 | 2012-04-06 09:40:18 +0530 | [diff] [blame] | 541 | /* standard ehci functions */ |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 542 | .irq = ehci_irq, |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 543 | .start = ehci_run, |
| 544 | .stop = ehci_stop, |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 545 | .urb_enqueue = ehci_urb_enqueue, |
| 546 | .urb_dequeue = ehci_urb_dequeue, |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 547 | .endpoint_disable = ehci_endpoint_disable, |
| 548 | .endpoint_reset = ehci_endpoint_reset, |
| 549 | .get_frame_number = ehci_get_frame, |
| 550 | .hub_status_data = ehci_hub_status_data, |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 551 | .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete, |
Venu Byravarasu | c6fa0b4 | 2012-04-06 09:40:18 +0530 | [diff] [blame] | 552 | .relinquish_port = ehci_relinquish_port, |
| 553 | .port_handed_over = ehci_port_handed_over, |
| 554 | |
| 555 | /* modified ehci functions for tegra */ |
| 556 | .reset = tegra_ehci_setup, |
| 557 | .shutdown = tegra_ehci_shutdown, |
| 558 | .map_urb_for_dma = tegra_ehci_map_urb_for_dma, |
| 559 | .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma, |
| 560 | .hub_control = tegra_ehci_hub_control, |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 561 | #ifdef CONFIG_PM |
| 562 | .bus_suspend = tegra_ehci_bus_suspend, |
| 563 | .bus_resume = tegra_ehci_bus_resume, |
| 564 | #endif |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 565 | }; |
| 566 | |
Olof Johansson | 4a53f4e | 2011-11-04 09:12:40 +0000 | [diff] [blame] | 567 | static int setup_vbus_gpio(struct platform_device *pdev) |
| 568 | { |
| 569 | int err = 0; |
| 570 | int gpio; |
| 571 | |
| 572 | if (!pdev->dev.of_node) |
| 573 | return 0; |
| 574 | |
| 575 | gpio = of_get_named_gpio(pdev->dev.of_node, "nvidia,vbus-gpio", 0); |
| 576 | if (!gpio_is_valid(gpio)) |
| 577 | return 0; |
| 578 | |
| 579 | err = gpio_request(gpio, "vbus_gpio"); |
| 580 | if (err) { |
| 581 | dev_err(&pdev->dev, "can't request vbus gpio %d", gpio); |
| 582 | return err; |
| 583 | } |
| 584 | err = gpio_direction_output(gpio, 1); |
| 585 | if (err) { |
| 586 | dev_err(&pdev->dev, "can't enable vbus\n"); |
| 587 | return err; |
| 588 | } |
| 589 | gpio_set_value(gpio, 1); |
| 590 | |
| 591 | return err; |
| 592 | } |
| 593 | |
| 594 | static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32); |
| 595 | |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 596 | static int tegra_ehci_probe(struct platform_device *pdev) |
| 597 | { |
| 598 | struct resource *res; |
| 599 | struct usb_hcd *hcd; |
| 600 | struct tegra_ehci_hcd *tegra; |
| 601 | struct tegra_ehci_platform_data *pdata; |
| 602 | int err = 0; |
| 603 | int irq; |
| 604 | int instance = pdev->id; |
| 605 | |
| 606 | pdata = pdev->dev.platform_data; |
| 607 | if (!pdata) { |
| 608 | dev_err(&pdev->dev, "Platform data missing\n"); |
| 609 | return -EINVAL; |
| 610 | } |
| 611 | |
Olof Johansson | 4a53f4e | 2011-11-04 09:12:40 +0000 | [diff] [blame] | 612 | /* Right now device-tree probed devices don't get dma_mask set. |
| 613 | * Since shared usb code relies on it, set it here for now. |
| 614 | * Once we have dma capability bindings this can go away. |
| 615 | */ |
| 616 | if (!pdev->dev.dma_mask) |
| 617 | pdev->dev.dma_mask = &tegra_ehci_dma_mask; |
| 618 | |
| 619 | setup_vbus_gpio(pdev); |
| 620 | |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 621 | tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL); |
| 622 | if (!tegra) |
| 623 | return -ENOMEM; |
| 624 | |
| 625 | hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev, |
| 626 | dev_name(&pdev->dev)); |
| 627 | if (!hcd) { |
| 628 | dev_err(&pdev->dev, "Unable to create HCD\n"); |
| 629 | err = -ENOMEM; |
| 630 | goto fail_hcd; |
| 631 | } |
| 632 | |
| 633 | platform_set_drvdata(pdev, tegra); |
| 634 | |
| 635 | tegra->clk = clk_get(&pdev->dev, NULL); |
| 636 | if (IS_ERR(tegra->clk)) { |
| 637 | dev_err(&pdev->dev, "Can't get ehci clock\n"); |
| 638 | err = PTR_ERR(tegra->clk); |
| 639 | goto fail_clk; |
| 640 | } |
| 641 | |
| 642 | err = clk_enable(tegra->clk); |
| 643 | if (err) |
| 644 | goto fail_clken; |
| 645 | |
| 646 | tegra->emc_clk = clk_get(&pdev->dev, "emc"); |
| 647 | if (IS_ERR(tegra->emc_clk)) { |
| 648 | dev_err(&pdev->dev, "Can't get emc clock\n"); |
| 649 | err = PTR_ERR(tegra->emc_clk); |
| 650 | goto fail_emc_clk; |
| 651 | } |
| 652 | |
| 653 | clk_enable(tegra->emc_clk); |
| 654 | clk_set_rate(tegra->emc_clk, 400000000); |
| 655 | |
| 656 | res = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 657 | if (!res) { |
| 658 | dev_err(&pdev->dev, "Failed to get I/O memory\n"); |
| 659 | err = -ENXIO; |
| 660 | goto fail_io; |
| 661 | } |
| 662 | hcd->rsrc_start = res->start; |
| 663 | hcd->rsrc_len = resource_size(res); |
| 664 | hcd->regs = ioremap(res->start, resource_size(res)); |
| 665 | if (!hcd->regs) { |
| 666 | dev_err(&pdev->dev, "Failed to remap I/O memory\n"); |
| 667 | err = -ENOMEM; |
| 668 | goto fail_io; |
| 669 | } |
| 670 | |
Olof Johansson | 4a53f4e | 2011-11-04 09:12:40 +0000 | [diff] [blame] | 671 | /* This is pretty ugly and needs to be fixed when we do only |
| 672 | * device-tree probing. Old code relies on the platform_device |
| 673 | * numbering that we lack for device-tree-instantiated devices. |
| 674 | */ |
| 675 | if (instance < 0) { |
| 676 | switch (res->start) { |
| 677 | case TEGRA_USB_BASE: |
| 678 | instance = 0; |
| 679 | break; |
| 680 | case TEGRA_USB2_BASE: |
| 681 | instance = 1; |
| 682 | break; |
| 683 | case TEGRA_USB3_BASE: |
| 684 | instance = 2; |
| 685 | break; |
| 686 | default: |
| 687 | err = -ENODEV; |
| 688 | dev_err(&pdev->dev, "unknown usb instance\n"); |
| 689 | goto fail_phy; |
| 690 | } |
| 691 | } |
| 692 | |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 693 | tegra->phy = tegra_usb_phy_open(instance, hcd->regs, pdata->phy_config, |
| 694 | TEGRA_USB_PHY_MODE_HOST); |
| 695 | if (IS_ERR(tegra->phy)) { |
| 696 | dev_err(&pdev->dev, "Failed to open USB phy\n"); |
| 697 | err = -ENXIO; |
| 698 | goto fail_phy; |
| 699 | } |
| 700 | |
| 701 | err = tegra_usb_phy_power_on(tegra->phy); |
| 702 | if (err) { |
| 703 | dev_err(&pdev->dev, "Failed to power on the phy\n"); |
| 704 | goto fail; |
| 705 | } |
| 706 | |
| 707 | tegra->host_resumed = 1; |
| 708 | tegra->power_down_on_bus_suspend = pdata->power_down_on_bus_suspend; |
| 709 | tegra->ehci = hcd_to_ehci(hcd); |
| 710 | |
| 711 | irq = platform_get_irq(pdev, 0); |
| 712 | if (!irq) { |
| 713 | dev_err(&pdev->dev, "Failed to get IRQ\n"); |
| 714 | err = -ENODEV; |
| 715 | goto fail; |
| 716 | } |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 717 | |
| 718 | #ifdef CONFIG_USB_OTG_UTILS |
| 719 | if (pdata->operating_mode == TEGRA_USB_OTG) { |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 720 | tegra->transceiver = usb_get_transceiver(); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 721 | if (tegra->transceiver) |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 722 | otg_set_host(tegra->transceiver->otg, &hcd->self); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 723 | } |
| 724 | #endif |
| 725 | |
Yong Zhang | b5dd18d | 2011-09-07 16:10:52 +0800 | [diff] [blame] | 726 | err = usb_add_hcd(hcd, irq, IRQF_SHARED); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 727 | if (err) { |
| 728 | dev_err(&pdev->dev, "Failed to add USB HCD\n"); |
| 729 | goto fail; |
| 730 | } |
| 731 | |
| 732 | return err; |
| 733 | |
| 734 | fail: |
| 735 | #ifdef CONFIG_USB_OTG_UTILS |
| 736 | if (tegra->transceiver) { |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 737 | otg_set_host(tegra->transceiver->otg, NULL); |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 738 | usb_put_transceiver(tegra->transceiver); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 739 | } |
| 740 | #endif |
| 741 | tegra_usb_phy_close(tegra->phy); |
| 742 | fail_phy: |
| 743 | iounmap(hcd->regs); |
| 744 | fail_io: |
| 745 | clk_disable(tegra->emc_clk); |
| 746 | clk_put(tegra->emc_clk); |
| 747 | fail_emc_clk: |
| 748 | clk_disable(tegra->clk); |
| 749 | fail_clken: |
| 750 | clk_put(tegra->clk); |
| 751 | fail_clk: |
| 752 | usb_put_hcd(hcd); |
| 753 | fail_hcd: |
| 754 | kfree(tegra); |
| 755 | return err; |
| 756 | } |
| 757 | |
| 758 | #ifdef CONFIG_PM |
| 759 | static int tegra_ehci_resume(struct platform_device *pdev) |
| 760 | { |
| 761 | struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev); |
| 762 | struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci); |
| 763 | |
| 764 | if (tegra->bus_suspended) |
| 765 | return 0; |
| 766 | |
| 767 | return tegra_usb_resume(hcd); |
| 768 | } |
| 769 | |
| 770 | static int tegra_ehci_suspend(struct platform_device *pdev, pm_message_t state) |
| 771 | { |
| 772 | struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev); |
| 773 | struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci); |
| 774 | |
| 775 | if (tegra->bus_suspended) |
| 776 | return 0; |
| 777 | |
| 778 | if (time_before(jiffies, tegra->ehci->next_statechange)) |
| 779 | msleep(10); |
| 780 | |
| 781 | return tegra_usb_suspend(hcd); |
| 782 | } |
| 783 | #endif |
| 784 | |
| 785 | static int tegra_ehci_remove(struct platform_device *pdev) |
| 786 | { |
| 787 | struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev); |
| 788 | struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci); |
| 789 | |
| 790 | if (tegra == NULL || hcd == NULL) |
| 791 | return -EINVAL; |
| 792 | |
| 793 | #ifdef CONFIG_USB_OTG_UTILS |
| 794 | if (tegra->transceiver) { |
Heikki Krogerus | 6e13c65 | 2012-02-13 13:24:20 +0200 | [diff] [blame] | 795 | otg_set_host(tegra->transceiver->otg, NULL); |
Heikki Krogerus | b96d3b0 | 2012-02-13 13:24:18 +0200 | [diff] [blame] | 796 | usb_put_transceiver(tegra->transceiver); |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 797 | } |
| 798 | #endif |
| 799 | |
| 800 | usb_remove_hcd(hcd); |
| 801 | usb_put_hcd(hcd); |
| 802 | |
| 803 | tegra_usb_phy_close(tegra->phy); |
| 804 | iounmap(hcd->regs); |
| 805 | |
| 806 | clk_disable(tegra->clk); |
| 807 | clk_put(tegra->clk); |
| 808 | |
| 809 | clk_disable(tegra->emc_clk); |
| 810 | clk_put(tegra->emc_clk); |
| 811 | |
| 812 | kfree(tegra); |
| 813 | return 0; |
| 814 | } |
| 815 | |
| 816 | static void tegra_ehci_hcd_shutdown(struct platform_device *pdev) |
| 817 | { |
| 818 | struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev); |
| 819 | struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci); |
| 820 | |
| 821 | if (hcd->driver->shutdown) |
| 822 | hcd->driver->shutdown(hcd); |
| 823 | } |
| 824 | |
Olof Johansson | 4a53f4e | 2011-11-04 09:12:40 +0000 | [diff] [blame] | 825 | static struct of_device_id tegra_ehci_of_match[] __devinitdata = { |
| 826 | { .compatible = "nvidia,tegra20-ehci", }, |
| 827 | { }, |
| 828 | }; |
| 829 | |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 830 | static struct platform_driver tegra_ehci_driver = { |
| 831 | .probe = tegra_ehci_probe, |
| 832 | .remove = tegra_ehci_remove, |
| 833 | #ifdef CONFIG_PM |
| 834 | .suspend = tegra_ehci_suspend, |
| 835 | .resume = tegra_ehci_resume, |
| 836 | #endif |
| 837 | .shutdown = tegra_ehci_hcd_shutdown, |
| 838 | .driver = { |
| 839 | .name = "tegra-ehci", |
Olof Johansson | 4a53f4e | 2011-11-04 09:12:40 +0000 | [diff] [blame] | 840 | .of_match_table = tegra_ehci_of_match, |
Benoit Goby | 79ad3b5 | 2011-03-09 16:28:56 -0800 | [diff] [blame] | 841 | } |
| 842 | }; |