ohci-at91.c 16.0 KB
Newer Older
1 2 3 4 5 6
/*
 * OHCI HCD (Host Controller Driver) for USB.
 *
 *  Copyright (C) 2004 SAN People (Pty) Ltd.
 *  Copyright (C) 2005 Thibaut VARENE <varenet@parisc-linux.org>
 *
7
 * AT91 Bus Glue
8 9 10 11 12 13 14 15 16
 *
 * Based on fragments of 2.4 driver by Rick Bronson.
 * Based on ohci-omap.c
 *
 * This file is licenced under the GPL.
 */

#include <linux/clk.h>
#include <linux/platform_device.h>
17 18
#include <linux/of_platform.h>
#include <linux/of_gpio.h>
19

20
#include <mach/hardware.h>
21 22
#include <asm/gpio.h>

23 24
#include <mach/board.h>
#include <mach/cpu.h>
25

26 27
#ifndef CONFIG_ARCH_AT91
#error "CONFIG_ARCH_AT91 must be defined."
28 29
#endif

30 31 32 33
#define valid_port(index)	((index) >= 0 && (index) < AT91_MAX_USBH_PORTS)
#define at91_for_each_port(index)	\
		for ((index) = 0; (index) < AT91_MAX_USBH_PORTS; (index)++)

34 35
/* interface and function clocks; sometimes also an AHB clock */
static struct clk *iclk, *fclk, *hclk;
36
static int clocked;
37 38 39 40 41

extern int usb_disabled(void);

/*-------------------------------------------------------------------------*/

42 43
static void at91_start_clock(void)
{
44
	clk_enable(hclk);
45 46 47 48 49 50 51 52 53
	clk_enable(iclk);
	clk_enable(fclk);
	clocked = 1;
}

static void at91_stop_clock(void)
{
	clk_disable(fclk);
	clk_disable(iclk);
54
	clk_disable(hclk);
55 56 57
	clocked = 0;
}

58 59 60 61 62
static void at91_start_hc(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct ohci_regs __iomem *regs = hcd->regs;

63
	dev_dbg(&pdev->dev, "start\n");
64 65 66 67

	/*
	 * Start the USB clocks.
	 */
68
	at91_start_clock();
69 70 71 72 73 74 75 76 77 78 79 80

	/*
	 * The USB host controller must remain in reset.
	 */
	writel(0, &regs->control);
}

static void at91_stop_hc(struct platform_device *pdev)
{
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
	struct ohci_regs __iomem *regs = hcd->regs;

81
	dev_dbg(&pdev->dev, "stop\n");
82 83 84 85 86 87 88 89 90

	/*
	 * Put the USB host controller into reset.
	 */
	writel(0, &regs->control);

	/*
	 * Stop the USB clocks.
	 */
91
	at91_stop_clock();
92 93 94 95 96
}


/*-------------------------------------------------------------------------*/

97
static void __devexit usb_hcd_at91_remove (struct usb_hcd *, struct platform_device *);
98 99 100 101 102 103

/* configure so an HC device and id are always provided */
/* always called with process context; sleeping is OK */


/**
104
 * usb_hcd_at91_probe - initialize AT91-based HCDs
105 106 107 108 109 110
 * Context: !in_interrupt()
 *
 * Allocates basic resources for this USB host controller, and
 * then invokes the start() method for the HCD associated with it
 * through the hotplug entry's driver_data.
 */
111
static int __devinit usb_hcd_at91_probe(const struct hc_driver *driver,
112
			struct platform_device *pdev)
113 114 115 116 117 118 119 120 121
{
	int retval;
	struct usb_hcd *hcd = NULL;

	if (pdev->num_resources != 2) {
		pr_debug("hcd probe: invalid num_resources");
		return -ENODEV;
	}

122 123
	if ((pdev->resource[0].flags != IORESOURCE_MEM)
			|| (pdev->resource[1].flags != IORESOURCE_IRQ)) {
124 125 126 127
		pr_debug("hcd probe: invalid resource type\n");
		return -ENODEV;
	}

128
	hcd = usb_create_hcd(driver, &pdev->dev, "at91");
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	if (!hcd)
		return -ENOMEM;
	hcd->rsrc_start = pdev->resource[0].start;
	hcd->rsrc_len = pdev->resource[0].end - pdev->resource[0].start + 1;

	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
		pr_debug("request_mem_region failed\n");
		retval = -EBUSY;
		goto err1;
	}

	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
	if (!hcd->regs) {
		pr_debug("ioremap failed\n");
		retval = -EIO;
		goto err2;
	}

	iclk = clk_get(&pdev->dev, "ohci_clk");
148 149 150 151 152
	if (IS_ERR(iclk)) {
		dev_err(&pdev->dev, "failed to get ohci_clk\n");
		retval = PTR_ERR(iclk);
		goto err3;
	}
153
	fclk = clk_get(&pdev->dev, "uhpck");
154 155 156 157 158
	if (IS_ERR(fclk)) {
		dev_err(&pdev->dev, "failed to get uhpck\n");
		retval = PTR_ERR(fclk);
		goto err4;
	}
159
	hclk = clk_get(&pdev->dev, "hclk");
160 161 162 163 164
	if (IS_ERR(hclk)) {
		dev_err(&pdev->dev, "failed to get hclk\n");
		retval = PTR_ERR(hclk);
		goto err5;
	}
165 166 167 168

	at91_start_hc(pdev);
	ohci_hcd_init(hcd_to_ohci(hcd));

169
	retval = usb_add_hcd(hcd, pdev->resource[1].start, IRQF_SHARED);
170 171 172 173 174 175
	if (retval == 0)
		return retval;

	/* Error handling */
	at91_stop_hc(pdev);

176
	clk_put(hclk);
177
 err5:
178
	clk_put(fclk);
179
 err4:
180 181
	clk_put(iclk);

182
 err3:
183 184 185 186 187 188 189 190 191 192 193 194 195 196
	iounmap(hcd->regs);

 err2:
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);

 err1:
	usb_put_hcd(hcd);
	return retval;
}


/* may be called with controller, bus, and devices active */

/**
197
 * usb_hcd_at91_remove - shutdown processing for AT91-based HCDs
198 199 200 201 202
 * @dev: USB Host Controller being removed
 * Context: !in_interrupt()
 *
 * Reverses the effect of usb_hcd_at91_probe(), first invoking
 * the HCD's stop() method.  It is always called from a thread
203
 * context, "rmmod" or something similar.
204 205
 *
 */
206
static void __devexit usb_hcd_at91_remove(struct usb_hcd *hcd,
207
				struct platform_device *pdev)
208 209 210 211
{
	usb_remove_hcd(hcd);
	at91_stop_hc(pdev);
	iounmap(hcd->regs);
212
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
213
	usb_put_hcd(hcd);
214

215
	clk_put(hclk);
216 217
	clk_put(fclk);
	clk_put(iclk);
218
	fclk = iclk = hclk = NULL;
219 220 221 222 223 224 225 226 227

	dev_set_drvdata(&pdev->dev, NULL);
}

/*-------------------------------------------------------------------------*/

static int __devinit
ohci_at91_start (struct usb_hcd *hcd)
{
228
	struct at91_usbh_data	*board = hcd->self.controller->platform_data;
229 230 231 232 233 234
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);
	int			ret;

	if ((ret = ohci_init(ohci)) < 0)
		return ret;

D
David Brownell 已提交
235
	ohci->num_ports = board->ports;
236

237 238 239 240 241 242 243 244
	if ((ret = ohci_run(ohci)) < 0) {
		err("can't start %s", hcd->self.bus_name);
		ohci_stop(hcd);
		return ret;
	}
	return 0;
}

245 246
static void ohci_at91_usb_set_power(struct at91_usbh_data *pdata, int port, int enable)
{
247
	if (!valid_port(port))
248 249
		return;

250
	if (!gpio_is_valid(pdata->vbus_pin[port]))
251 252
		return;

253
	gpio_set_value(pdata->vbus_pin[port],
254
		       pdata->vbus_pin_active_low[port] ^ enable);
255 256 257 258
}

static int ohci_at91_usb_get_power(struct at91_usbh_data *pdata, int port)
{
259
	if (!valid_port(port))
260 261
		return -EINVAL;

262
	if (!gpio_is_valid(pdata->vbus_pin[port]))
263 264
		return -EINVAL;

265
	return gpio_get_value(pdata->vbus_pin[port]) ^
266
		pdata->vbus_pin_active_low[port];
267 268 269 270 271 272 273 274 275 276 277
}

/*
 * Update the status data from the hub with the over-current indicator change.
 */
static int ohci_at91_hub_status_data(struct usb_hcd *hcd, char *buf)
{
	struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
	int length = ohci_hub_status_data(hcd, buf);
	int port;

278
	at91_for_each_port(port) {
279
		if (pdata->overcurrent_changed[port]) {
280
			if (!length)
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
				length = 1;
			buf[0] |= 1 << (port + 1);
		}
	}

	return length;
}

/*
 * Look at the control requests to the root hub and see if we need to override.
 */
static int ohci_at91_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
				 u16 wIndex, char *buf, u16 wLength)
{
	struct at91_usbh_data *pdata = hcd->self.controller->platform_data;
	struct usb_hub_descriptor *desc;
	int ret = -EINVAL;
	u32 *data = (u32 *)buf;

	dev_dbg(hcd->self.controller,
		"ohci_at91_hub_control(%p,0x%04x,0x%04x,0x%04x,%p,%04x)\n",
		hcd, typeReq, wValue, wIndex, buf, wLength);

304 305
	wIndex--;

306 307 308 309
	switch (typeReq) {
	case SetPortFeature:
		if (wValue == USB_PORT_FEAT_POWER) {
			dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
310 311 312 313 314
			if (valid_port(wIndex)) {
				ohci_at91_usb_set_power(pdata, wIndex, 1);
				ret = 0;
			}

315 316 317 318 319 320 321 322 323 324
			goto out;
		}
		break;

	case ClearPortFeature:
		switch (wValue) {
		case USB_PORT_FEAT_C_OVER_CURRENT:
			dev_dbg(hcd->self.controller,
				"ClearPortFeature: C_OVER_CURRENT\n");

325 326 327
			if (valid_port(wIndex)) {
				pdata->overcurrent_changed[wIndex] = 0;
				pdata->overcurrent_status[wIndex] = 0;
328 329 330 331 332 333 334 335
			}

			goto out;

		case USB_PORT_FEAT_OVER_CURRENT:
			dev_dbg(hcd->self.controller,
				"ClearPortFeature: OVER_CURRENT\n");

336 337
			if (valid_port(wIndex))
				pdata->overcurrent_status[wIndex] = 0;
338 339 340 341 342 343 344

			goto out;

		case USB_PORT_FEAT_POWER:
			dev_dbg(hcd->self.controller,
				"ClearPortFeature: POWER\n");

345 346
			if (valid_port(wIndex)) {
				ohci_at91_usb_set_power(pdata, wIndex, 0);
347 348 349 350 351 352
				return 0;
			}
		}
		break;
	}

353
	ret = ohci_hub_control(hcd, typeReq, wValue, wIndex + 1, buf, wLength);
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
	if (ret)
		goto out;

	switch (typeReq) {
	case GetHubDescriptor:

		/* update the hub's descriptor */

		desc = (struct usb_hub_descriptor *)buf;

		dev_dbg(hcd->self.controller, "wHubCharacteristics 0x%04x\n",
			desc->wHubCharacteristics);

		/* remove the old configurations for power-switching, and
		 * over-current protection, and insert our new configuration
		 */

		desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_LPSM);
		desc->wHubCharacteristics |= cpu_to_le16(0x0001);

		if (pdata->overcurrent_supported) {
			desc->wHubCharacteristics &= ~cpu_to_le16(HUB_CHAR_OCPM);
			desc->wHubCharacteristics |=  cpu_to_le16(0x0008|0x0001);
		}

		dev_dbg(hcd->self.controller, "wHubCharacteristics after 0x%04x\n",
			desc->wHubCharacteristics);

		return ret;

	case GetPortStatus:
		/* check port status */

		dev_dbg(hcd->self.controller, "GetPortStatus(%d)\n", wIndex);

389 390
		if (valid_port(wIndex)) {
			if (!ohci_at91_usb_get_power(pdata, wIndex))
391 392
				*data &= ~cpu_to_le32(RH_PS_PPS);

393
			if (pdata->overcurrent_changed[wIndex])
394 395
				*data |= cpu_to_le32(RH_PS_OCIC);

396
			if (pdata->overcurrent_status[wIndex])
397 398 399 400 401 402 403 404
				*data |= cpu_to_le32(RH_PS_POCI);
		}
	}

 out:
	return ret;
}

405 406 407 408
/*-------------------------------------------------------------------------*/

static const struct hc_driver ohci_at91_hc_driver = {
	.description =		hcd_name,
409
	.product_desc =		"AT91 OHCI",
410 411 412 413 414 415 416 417 418 419 420 421 422
	.hcd_priv_size =	sizeof(struct ohci_hcd),

	/*
	 * generic hardware linkage
	 */
	.irq =			ohci_irq,
	.flags =		HCD_USB11 | HCD_MEMORY,

	/*
	 * basic lifecycle operations
	 */
	.start =		ohci_at91_start,
	.stop =			ohci_stop,
423
	.shutdown =		ohci_shutdown,
424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439

	/*
	 * managing i/o requests and associated device resources
	 */
	.urb_enqueue =		ohci_urb_enqueue,
	.urb_dequeue =		ohci_urb_dequeue,
	.endpoint_disable =	ohci_endpoint_disable,

	/*
	 * scheduling support
	 */
	.get_frame_number =	ohci_get_frame,

	/*
	 * root hub support
	 */
440 441
	.hub_status_data =	ohci_at91_hub_status_data,
	.hub_control =		ohci_at91_hub_control,
442
#ifdef CONFIG_PM
443 444
	.bus_suspend =		ohci_bus_suspend,
	.bus_resume =		ohci_bus_resume,
445 446 447 448 449 450
#endif
	.start_port_reset =	ohci_start_port_reset,
};

/*-------------------------------------------------------------------------*/

451 452 453 454 455 456 457 458
static irqreturn_t ohci_hcd_at91_overcurrent_irq(int irq, void *data)
{
	struct platform_device *pdev = data;
	struct at91_usbh_data *pdata = pdev->dev.platform_data;
	int val, gpio, port;

	/* From the GPIO notifying the over-current situation, find
	 * out the corresponding port */
459
	at91_for_each_port(port) {
460 461
		if (gpio_to_irq(pdata->overcurrent_pin[port]) == irq) {
			gpio = pdata->overcurrent_pin[port];
462
			break;
463
		}
464 465
	}

466
	if (port == AT91_MAX_USBH_PORTS) {
467 468 469 470 471 472 473 474 475
		dev_err(& pdev->dev, "overcurrent interrupt from unknown GPIO\n");
		return IRQ_HANDLED;
	}

	val = gpio_get_value(gpio);

	/* When notified of an over-current situation, disable power
	   on the corresponding port, and mark this port in
	   over-current. */
476
	if (!val) {
477 478 479 480 481 482 483 484 485 486 487
		ohci_at91_usb_set_power(pdata, port, 0);
		pdata->overcurrent_status[port]  = 1;
		pdata->overcurrent_changed[port] = 1;
	}

	dev_dbg(& pdev->dev, "overcurrent situation %s\n",
		val ? "exited" : "notified");

	return IRQ_HANDLED;
}

488 489 490 491 492 493 494 495 496 497 498 499 500
#ifdef CONFIG_OF
static const struct of_device_id at91_ohci_dt_ids[] = {
	{ .compatible = "atmel,at91rm9200-ohci" },
	{ /* sentinel */ }
};

MODULE_DEVICE_TABLE(of, at91_ohci_dt_ids);

static u64 at91_ohci_dma_mask = DMA_BIT_MASK(32);

static int __devinit ohci_at91_of_init(struct platform_device *pdev)
{
	struct device_node *np = pdev->dev.of_node;
501
	int i, gpio;
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
	enum of_gpio_flags flags;
	struct at91_usbh_data	*pdata;
	u32 ports;

	if (!np)
		return 0;

	/* 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)
		pdev->dev.dma_mask = &at91_ohci_dma_mask;

	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
	if (!pdata)
		return -ENOMEM;

	if (!of_property_read_u32(np, "num-ports", &ports))
		pdata->ports = ports;

523
	at91_for_each_port(i) {
524 525 526 527 528 529 530
		gpio = of_get_named_gpio_flags(np, "atmel,vbus-gpio", i, &flags);
		pdata->vbus_pin[i] = gpio;
		if (!gpio_is_valid(gpio))
			continue;
		pdata->vbus_pin_active_low[i] = flags & OF_GPIO_ACTIVE_LOW;
	}

531
	at91_for_each_port(i)
532 533
		pdata->overcurrent_pin[i] =
			of_get_named_gpio_flags(np, "atmel,oc-gpio", i, &flags);
534 535 536 537 538 539 540 541 542 543 544 545

	pdev->dev.platform_data = pdata;

	return 0;
}
#else
static int __devinit ohci_at91_of_init(struct platform_device *pdev)
{
	return 0;
}
#endif

546 547
/*-------------------------------------------------------------------------*/

548
static int __devinit ohci_hcd_at91_drv_probe(struct platform_device *pdev)
549
{
550
	struct at91_usbh_data	*pdata;
551
	int			i;
552 553
	int			gpio;
	int			ret;
554

555 556 557
	ret = ohci_at91_of_init(pdev);
	if (ret)
		return ret;
558 559 560

	pdata = pdev->dev.platform_data;

561
	if (pdata) {
562
		at91_for_each_port(i) {
563
			if (!gpio_is_valid(pdata->vbus_pin[i]))
564
				continue;
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
			gpio = pdata->vbus_pin[i];

			ret = gpio_request(gpio, "ohci_vbus");
			if (ret) {
				dev_err(&pdev->dev,
					"can't request vbus gpio %d\n", gpio);
				continue;
			}
			ret = gpio_direction_output(gpio,
						!pdata->vbus_pin_active_low[i]);
			if (ret) {
				dev_err(&pdev->dev,
					"can't put vbus gpio %d as output %d\n",
					gpio, !pdata->vbus_pin_active_low[i]);
				gpio_free(gpio);
				continue;
			}

583 584 585
			ohci_at91_usb_set_power(pdata, i, 1);
		}

586
		at91_for_each_port(i) {
587
			if (!gpio_is_valid(pdata->overcurrent_pin[i]))
588
				continue;
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
			gpio = pdata->overcurrent_pin[i];

			ret = gpio_request(gpio, "ohci_overcurrent");
			if (ret) {
				dev_err(&pdev->dev,
					"can't request overcurrent gpio %d\n",
					gpio);
				continue;
			}

			ret = gpio_direction_input(gpio);
			if (ret) {
				dev_err(&pdev->dev,
					"can't configure overcurrent gpio %d as input\n",
					gpio);
				gpio_free(gpio);
				continue;
			}
607

608
			ret = request_irq(gpio_to_irq(gpio),
609 610 611
					  ohci_hcd_at91_overcurrent_irq,
					  IRQF_SHARED, "ohci_overcurrent", pdev);
			if (ret) {
612 613 614
				gpio_free(gpio);
				dev_err(&pdev->dev,
					"can't get gpio IRQ for overcurrent\n");
615
			}
616 617 618
		}
	}

619 620
	device_init_wakeup(&pdev->dev, 1);
	return usb_hcd_at91_probe(&ohci_at91_hc_driver, pdev);
621 622
}

623
static int __devexit ohci_hcd_at91_drv_remove(struct platform_device *pdev)
624
{
625 626 627 628
	struct at91_usbh_data	*pdata = pdev->dev.platform_data;
	int			i;

	if (pdata) {
629
		at91_for_each_port(i) {
630
			if (!gpio_is_valid(pdata->vbus_pin[i]))
631
				continue;
632
			ohci_at91_usb_set_power(pdata, i, 0);
633 634
			gpio_free(pdata->vbus_pin[i]);
		}
635

636
		at91_for_each_port(i) {
637
			if (!gpio_is_valid(pdata->overcurrent_pin[i]))
638 639 640 641
				continue;
			free_irq(gpio_to_irq(pdata->overcurrent_pin[i]), pdev);
			gpio_free(pdata->overcurrent_pin[i]);
		}
642 643
	}

644
	device_init_wakeup(&pdev->dev, 0);
645 646
	usb_hcd_at91_remove(platform_get_drvdata(pdev), pdev);
	return 0;
647 648 649 650
}

#ifdef CONFIG_PM

651
static int
652
ohci_hcd_at91_drv_suspend(struct platform_device *pdev, pm_message_t mesg)
653
{
654 655 656 657 658 659 660 661 662 663 664 665 666 667 668
	struct usb_hcd	*hcd = platform_get_drvdata(pdev);
	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);

	if (device_may_wakeup(&pdev->dev))
		enable_irq_wake(hcd->irq);

	/*
	 * The integrated transceivers seem unable to notice disconnect,
	 * reconnect, or wakeup without the 48 MHz clock active.  so for
	 * correctness, always discard connection state (using reset).
	 *
	 * REVISIT: some boards will be able to turn VBUS off...
	 */
	if (at91_suspend_entering_slow_clock()) {
		ohci_usb_reset (ohci);
669 670
		/* flush the writes */
		(void) ohci_readl (ohci, &ohci->regs->control);
671
		at91_stop_clock();
672
	}
673 674 675 676

	return 0;
}

677
static int ohci_hcd_at91_drv_resume(struct platform_device *pdev)
678
{
679 680 681 682 683
	struct usb_hcd	*hcd = platform_get_drvdata(pdev);

	if (device_may_wakeup(&pdev->dev))
		disable_irq_wake(hcd->irq);

684 685
	if (!clocked)
		at91_start_clock();
686

687
	ohci_finish_controller_resume(hcd);
688 689 690 691 692 693 694
	return 0;
}
#else
#define ohci_hcd_at91_drv_suspend NULL
#define ohci_hcd_at91_drv_resume  NULL
#endif

695
MODULE_ALIAS("platform:at91_ohci");
696

697 698
static struct platform_driver ohci_hcd_at91_driver = {
	.probe		= ohci_hcd_at91_drv_probe,
699
	.remove		= __devexit_p(ohci_hcd_at91_drv_remove),
700
	.shutdown	= usb_hcd_platform_shutdown,
701 702 703
	.suspend	= ohci_hcd_at91_drv_suspend,
	.resume		= ohci_hcd_at91_drv_resume,
	.driver		= {
704
		.name	= "at91_ohci",
705
		.owner	= THIS_MODULE,
706
		.of_match_table	= of_match_ptr(at91_ohci_dt_ids),
707 708
	},
};