ehci-tegra.c 14.3 KB
Newer Older
1 2 3 4
/*
 * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
 *
 * Copyright (C) 2010 Google, Inc.
5
 * Copyright (C) 2009 - 2013 NVIDIA Corporation
6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
 * more details.
 *
 */

#include <linux/clk.h>
20
#include <linux/err.h>
21 22 23 24
#include <linux/platform_device.h>
#include <linux/platform_data/tegra_usb.h>
#include <linux/irq.h>
#include <linux/usb/otg.h>
25 26 27
#include <linux/gpio.h>
#include <linux/of.h>
#include <linux/of_gpio.h>
28
#include <linux/pm_runtime.h>
29
#include <linux/usb/ehci_def.h>
30
#include <linux/usb/tegra_usb_phy.h>
31
#include <linux/clk/tegra.h>
32 33 34 35

#define TEGRA_USB_BASE			0xC5000000
#define TEGRA_USB2_BASE			0xC5004000
#define TEGRA_USB3_BASE			0xC5008000
36

37 38
#define TEGRA_USB_DMA_ALIGN 32

39 40 41 42
struct tegra_ehci_hcd {
	struct ehci_hcd *ehci;
	struct tegra_usb_phy *phy;
	struct clk *clk;
43
	struct usb_phy *transceiver;
44
	int port_resuming;
45
	bool needs_double_reset;
46 47 48
	enum tegra_usb_phy_port_speed port_speed;
};

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
static int tegra_ehci_internal_port_reset(
	struct ehci_hcd	*ehci,
	u32 __iomem	*portsc_reg
)
{
	u32		temp;
	unsigned long	flags;
	int		retval = 0;
	int		i, tries;
	u32		saved_usbintr;

	spin_lock_irqsave(&ehci->lock, flags);
	saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
	/* disable USB interrupt */
	ehci_writel(ehci, 0, &ehci->regs->intr_enable);
	spin_unlock_irqrestore(&ehci->lock, flags);

	/*
	 * Here we have to do Port Reset at most twice for
	 * Port Enable bit to be set.
	 */
	for (i = 0; i < 2; i++) {
		temp = ehci_readl(ehci, portsc_reg);
		temp |= PORT_RESET;
		ehci_writel(ehci, temp, portsc_reg);
		mdelay(10);
		temp &= ~PORT_RESET;
		ehci_writel(ehci, temp, portsc_reg);
		mdelay(1);
		tries = 100;
		do {
			mdelay(1);
			/*
			 * Up to this point, Port Enable bit is
			 * expected to be set after 2 ms waiting.
			 * USB1 usually takes extra 45 ms, for safety,
			 * we take 100 ms as timeout.
			 */
			temp = ehci_readl(ehci, portsc_reg);
		} while (!(temp & PORT_PE) && tries--);
		if (temp & PORT_PE)
			break;
	}
	if (i == 2)
		retval = -ETIMEDOUT;

	/*
	 * Clear Connect Status Change bit if it's set.
	 * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
	 */
	if (temp & PORT_CSC)
		ehci_writel(ehci, PORT_CSC, portsc_reg);

	/*
	 * Write to clear any interrupt status bits that might be set
	 * during port reset.
	 */
	temp = ehci_readl(ehci, &ehci->regs->status);
	ehci_writel(ehci, temp, &ehci->regs->status);

	/* restore original interrupt enable bits */
	ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
	return retval;
}

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
static int tegra_ehci_hub_control(
	struct usb_hcd	*hcd,
	u16		typeReq,
	u16		wValue,
	u16		wIndex,
	char		*buf,
	u16		wLength
)
{
	struct ehci_hcd	*ehci = hcd_to_ehci(hcd);
	struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
	u32 __iomem	*status_reg;
	u32		temp;
	unsigned long	flags;
	int		retval = 0;

	status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];

	spin_lock_irqsave(&ehci->lock, flags);

134
	if (typeReq == GetPortStatus) {
135 136 137 138
		temp = ehci_readl(ehci, status_reg);
		if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
			/* Resume completed, re-enable disconnect detection */
			tegra->port_resuming = 0;
139
			tegra_usb_phy_postresume(hcd->phy);
140 141 142 143 144 145 146 147 148 149
		}
	}

	else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
		temp = ehci_readl(ehci, status_reg);
		if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
			retval = -EPIPE;
			goto done;
		}

150
		temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
151 152 153 154 155 156 157
		temp |= PORT_WKDISC_E | PORT_WKOC_E;
		ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);

		/*
		 * If a transaction is in progress, there may be a delay in
		 * suspending the port. Poll until the port is suspended.
		 */
158
		if (ehci_handshake(ehci, status_reg, PORT_SUSPEND,
159 160 161 162 163 164 165
						PORT_SUSPEND, 5000))
			pr_err("%s: timeout waiting for SUSPEND\n", __func__);

		set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
		goto done;
	}

166
	/* For USB1 port we need to issue Port Reset twice internally */
167
	if (tegra->needs_double_reset &&
168 169 170 171 172
	   (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
		spin_unlock_irqrestore(&ehci->lock, flags);
		return tegra_ehci_internal_port_reset(ehci, status_reg);
	}

173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
	/*
	 * Tegra host controller will time the resume operation to clear the bit
	 * when the port control state switches to HS or FS Idle. This behavior
	 * is different from EHCI where the host controller driver is required
	 * to set this bit to a zero after the resume duration is timed in the
	 * driver.
	 */
	else if (typeReq == ClearPortFeature &&
					wValue == USB_PORT_FEAT_SUSPEND) {
		temp = ehci_readl(ehci, status_reg);
		if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
			retval = -EPIPE;
			goto done;
		}

		if (!(temp & PORT_SUSPEND))
			goto done;

		/* Disable disconnect detection during port resume */
192
		tegra_usb_phy_preresume(hcd->phy);
193 194 195 196 197 198

		ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);

		temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
		/* start resume signalling */
		ehci_writel(ehci, temp | PORT_RESUME, status_reg);
199
		set_bit(wIndex-1, &ehci->resuming_ports);
200 201 202 203 204 205

		spin_unlock_irqrestore(&ehci->lock, flags);
		msleep(20);
		spin_lock_irqsave(&ehci->lock, flags);

		/* Poll until the controller clears RESUME and SUSPEND */
206
		if (ehci_handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
207
			pr_err("%s: timeout waiting for RESUME\n", __func__);
208
		if (ehci_handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
209 210 211
			pr_err("%s: timeout waiting for SUSPEND\n", __func__);

		ehci->reset_done[wIndex-1] = 0;
212
		clear_bit(wIndex-1, &ehci->resuming_ports);
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

		tegra->port_resuming = 1;
		goto done;
	}

	spin_unlock_irqrestore(&ehci->lock, flags);

	/* Handle the hub control events here */
	return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
done:
	spin_unlock_irqrestore(&ehci->lock, flags);
	return retval;
}

static int tegra_ehci_setup(struct usb_hcd *hcd)
{
	struct ehci_hcd *ehci = hcd_to_ehci(hcd);

	/* EHCI registers start at offset 0x100 */
	ehci->caps = hcd->regs + 0x100;

	/* switch to host mode */
	hcd->has_tt = 1;

237
	return ehci_setup(hcd);
238 239
}

240
struct dma_aligned_buffer {
241 242 243 244 245
	void *kmalloc_ptr;
	void *old_xfer_buffer;
	u8 data[0];
};

246
static void free_dma_aligned_buffer(struct urb *urb)
247
{
248
	struct dma_aligned_buffer *temp;
249 250 251 252

	if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
		return;

253 254
	temp = container_of(urb->transfer_buffer,
		struct dma_aligned_buffer, data);
255

256
	if (usb_urb_dir_in(urb))
257 258 259 260 261 262 263 264
		memcpy(temp->old_xfer_buffer, temp->data,
		       urb->transfer_buffer_length);
	urb->transfer_buffer = temp->old_xfer_buffer;
	kfree(temp->kmalloc_ptr);

	urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
}

265
static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
266
{
267
	struct dma_aligned_buffer *temp, *kmalloc_ptr;
268 269 270 271 272 273 274 275 276
	size_t kmalloc_size;

	if (urb->num_sgs || urb->sg ||
	    urb->transfer_buffer_length == 0 ||
	    !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
		return 0;

	/* Allocate a buffer with enough padding for alignment */
	kmalloc_size = urb->transfer_buffer_length +
277
		sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
278 279 280 281 282

	kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
	if (!kmalloc_ptr)
		return -ENOMEM;

283
	/* Position our struct dma_aligned_buffer such that data is aligned */
284 285 286
	temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
	temp->kmalloc_ptr = kmalloc_ptr;
	temp->old_xfer_buffer = urb->transfer_buffer;
287
	if (usb_urb_dir_out(urb))
288 289 290 291 292 293 294 295 296 297 298 299 300 301
		memcpy(temp->data, urb->transfer_buffer,
		       urb->transfer_buffer_length);
	urb->transfer_buffer = temp->data;

	urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;

	return 0;
}

static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
				      gfp_t mem_flags)
{
	int ret;

302
	ret = alloc_dma_aligned_buffer(urb, mem_flags);
303 304 305 306 307
	if (ret)
		return ret;

	ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
	if (ret)
308
		free_dma_aligned_buffer(urb);
309 310 311 312 313 314 315

	return ret;
}

static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
{
	usb_hcd_unmap_urb_for_dma(hcd, urb);
316
	free_dma_aligned_buffer(urb);
317 318
}

319 320 321 322 323 324
static const struct hc_driver tegra_ehci_hc_driver = {
	.description		= hcd_name,
	.product_desc		= "Tegra EHCI Host Controller",
	.hcd_priv_size		= sizeof(struct ehci_hcd),
	.flags			= HCD_USB2 | HCD_MEMORY,

325
	/* standard ehci functions */
326 327 328 329 330 331 332 333 334 335
	.irq			= ehci_irq,
	.start			= ehci_run,
	.stop			= ehci_stop,
	.urb_enqueue		= ehci_urb_enqueue,
	.urb_dequeue		= ehci_urb_dequeue,
	.endpoint_disable	= ehci_endpoint_disable,
	.endpoint_reset		= ehci_endpoint_reset,
	.get_frame_number	= ehci_get_frame,
	.hub_status_data	= ehci_hub_status_data,
	.clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
336 337 338 339 340
	.relinquish_port	= ehci_relinquish_port,
	.port_handed_over	= ehci_port_handed_over,

	/* modified ehci functions for tegra */
	.reset			= tegra_ehci_setup,
341
	.shutdown		= ehci_shutdown,
342 343 344
	.map_urb_for_dma	= tegra_ehci_map_urb_for_dma,
	.unmap_urb_for_dma	= tegra_ehci_unmap_urb_for_dma,
	.hub_control		= tegra_ehci_hub_control,
345
#ifdef CONFIG_PM
346 347
	.bus_suspend		= ehci_bus_suspend,
	.bus_resume		= ehci_bus_resume,
348 349 350
#endif
};

351 352
static int setup_vbus_gpio(struct platform_device *pdev,
			   struct tegra_ehci_platform_data *pdata)
353 354 355 356
{
	int err = 0;
	int gpio;

357 358 359 360
	gpio = pdata->vbus_gpio;
	if (!gpio_is_valid(gpio))
		gpio = of_get_named_gpio(pdev->dev.of_node,
					 "nvidia,vbus-gpio", 0);
361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
	if (!gpio_is_valid(gpio))
		return 0;

	err = gpio_request(gpio, "vbus_gpio");
	if (err) {
		dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
		return err;
	}
	err = gpio_direction_output(gpio, 1);
	if (err) {
		dev_err(&pdev->dev, "can't enable vbus\n");
		return err;
	}

	return err;
}

378 379 380 381 382 383 384 385
static int tegra_ehci_probe(struct platform_device *pdev)
{
	struct resource *res;
	struct usb_hcd *hcd;
	struct tegra_ehci_hcd *tegra;
	struct tegra_ehci_platform_data *pdata;
	int err = 0;
	int irq;
386
	struct device_node *np_phy;
387
	struct usb_phy *u_phy;
388 389 390 391 392 393 394

	pdata = pdev->dev.platform_data;
	if (!pdata) {
		dev_err(&pdev->dev, "Platform data missing\n");
		return -EINVAL;
	}

395 396 397 398 399
	/* Right now device-tree probed devices don't get dma_mask set.
	 * Since shared usb code relies on it, set it here for now.
	 * Once we have dma capability bindings this can go away.
	 */
	if (!pdev->dev.dma_mask)
400 401 402
		pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
	if (!pdev->dev.coherent_dma_mask)
		pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
403

404
	setup_vbus_gpio(pdev, pdata);
405

406 407
	tegra = devm_kzalloc(&pdev->dev, sizeof(struct tegra_ehci_hcd),
			     GFP_KERNEL);
408 409 410
	if (!tegra)
		return -ENOMEM;

411
	tegra->clk = devm_clk_get(&pdev->dev, NULL);
412 413
	if (IS_ERR(tegra->clk)) {
		dev_err(&pdev->dev, "Can't get ehci clock\n");
414
		return PTR_ERR(tegra->clk);
415 416
	}

417
	err = clk_prepare_enable(tegra->clk);
418
	if (err)
419
		return err;
420

421 422 423 424
	tegra_periph_reset_assert(tegra->clk);
	udelay(1);
	tegra_periph_reset_deassert(tegra->clk);

425 426 427 428 429 430 431 432 433 434 435 436
	np_phy = of_parse_phandle(pdev->dev.of_node, "nvidia,phy", 0);
	if (!np_phy) {
		err = -ENODEV;
		goto cleanup_clk;
	}

	u_phy = tegra_usb_get_phy(np_phy);
	if (IS_ERR(u_phy)) {
		err = PTR_ERR(u_phy);
		goto cleanup_clk;
	}

437 438 439
	tegra->needs_double_reset = of_property_read_bool(pdev->dev.of_node,
		"nvidia,needs-double-reset");

440 441 442 443 444 445 446 447 448
	hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
					dev_name(&pdev->dev));
	if (!hcd) {
		dev_err(&pdev->dev, "Unable to create HCD\n");
		err = -ENOMEM;
		goto cleanup_clk;
	}
	hcd->phy = u_phy;

449 450 451 452
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
	if (!res) {
		dev_err(&pdev->dev, "Failed to get I/O memory\n");
		err = -ENXIO;
453
		goto cleanup_hcd_create;
454 455 456
	}
	hcd->rsrc_start = res->start;
	hcd->rsrc_len = resource_size(res);
457
	hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
458 459 460
	if (!hcd->regs) {
		dev_err(&pdev->dev, "Failed to remap I/O memory\n");
		err = -ENOMEM;
461
		goto cleanup_hcd_create;
462 463
	}

464 465 466 467
	err = usb_phy_init(hcd->phy);
	if (err) {
		dev_err(&pdev->dev, "Failed to initialize phy\n");
		goto cleanup_hcd_create;
468 469
	}

470 471 472 473 474
	u_phy->otg = devm_kzalloc(&pdev->dev, sizeof(struct usb_otg),
			     GFP_KERNEL);
	if (!u_phy->otg) {
		dev_err(&pdev->dev, "Failed to alloc memory for otg\n");
		err = -ENOMEM;
475
		goto cleanup_phy;
476 477 478
	}
	u_phy->otg->host = hcd_to_bus(hcd);

479
	err = usb_phy_set_suspend(hcd->phy, 0);
480 481
	if (err) {
		dev_err(&pdev->dev, "Failed to power on the phy\n");
482
		goto cleanup_phy;
483 484 485 486 487 488 489 490
	}

	tegra->ehci = hcd_to_ehci(hcd);

	irq = platform_get_irq(pdev, 0);
	if (!irq) {
		dev_err(&pdev->dev, "Failed to get IRQ\n");
		err = -ENODEV;
491
		goto cleanup_phy;
492 493 494
	}

	if (pdata->operating_mode == TEGRA_USB_OTG) {
495 496
		tegra->transceiver =
			devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
497
		if (!IS_ERR(tegra->transceiver))
498
			otg_set_host(tegra->transceiver->otg, &hcd->self);
499 500
	} else {
		tegra->transceiver = ERR_PTR(-ENODEV);
501 502
	}

503 504
	platform_set_drvdata(pdev, tegra);

Yong Zhang's avatar
Yong Zhang committed
505
	err = usb_add_hcd(hcd, irq, IRQF_SHARED);
506 507
	if (err) {
		dev_err(&pdev->dev, "Failed to add USB HCD\n");
508
		goto cleanup_phy;
509 510 511 512
	}

	return err;

513
cleanup_phy:
514
	if (!IS_ERR(tegra->transceiver))
515
		otg_set_host(tegra->transceiver->otg, NULL);
516

517
	usb_phy_shutdown(hcd->phy);
518
cleanup_hcd_create:
519
	usb_put_hcd(hcd);
520 521
cleanup_clk:
	clk_disable_unprepare(tegra->clk);
522 523 524 525 526 527 528 529
	return err;
}

static int tegra_ehci_remove(struct platform_device *pdev)
{
	struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
	struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);

530
	if (!IS_ERR(tegra->transceiver))
531
		otg_set_host(tegra->transceiver->otg, NULL);
532

533
	usb_phy_shutdown(hcd->phy);
534
	usb_remove_hcd(hcd);
535 536
	usb_put_hcd(hcd);

537
	clk_disable_unprepare(tegra->clk);
538 539 540 541 542 543 544 545 546 547 548 549 550

	return 0;
}

static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
{
	struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
	struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);

	if (hcd->driver->shutdown)
		hcd->driver->shutdown(hcd);
}

551
static struct of_device_id tegra_ehci_of_match[] = {
552 553 554 555
	{ .compatible = "nvidia,tegra20-ehci", },
	{ },
};

556 557 558 559 560 561
static struct platform_driver tegra_ehci_driver = {
	.probe		= tegra_ehci_probe,
	.remove		= tegra_ehci_remove,
	.shutdown	= tegra_ehci_hcd_shutdown,
	.driver		= {
		.name	= "tegra-ehci",
562
		.of_match_table = tegra_ehci_of_match,
563 564
	}
};