ohci-da8xx.c 13.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/*
 * OHCI HCD (Host Controller Driver) for USB.
 *
 * TI DA8xx (OMAP-L1x) Bus Glue
 *
 * Derived from: ohci-omap.c and ohci-s3c2410.c
 * Copyright (C) 2008-2009 MontaVista Software, Inc. <source@mvista.com>
 *
 * This file is licensed under the terms of the GNU General Public License
 * version 2. This program is licensed "as is" without any warranty of any
 * kind, whether express or implied.
 */

14 15
#include <linux/clk.h>
#include <linux/io.h>
16 17
#include <linux/interrupt.h>
#include <linux/jiffies.h>
18 19
#include <linux/kernel.h>
#include <linux/module.h>
20
#include <linux/platform_device.h>
21
#include <linux/phy/phy.h>
22
#include <linux/platform_data/usb-davinci.h>
23
#include <linux/regulator/consumer.h>
24 25 26
#include <linux/usb.h>
#include <linux/usb/hcd.h>
#include <asm/unaligned.h>
27

28 29 30
#include "ohci.h"

#define DRIVER_DESC "DA8XX"
31
#define DRV_NAME "ohci-da8xx"
32 33 34 35 36 37

static struct hc_driver __read_mostly ohci_da8xx_hc_driver;

static int (*orig_ohci_hub_control)(struct usb_hcd  *hcd, u16 typeReq,
			u16 wValue, u16 wIndex, char *buf, u16 wLength);
static int (*orig_ohci_hub_status_data)(struct usb_hcd *hcd, char *buf);
38

39
struct da8xx_ohci_hcd {
40
	struct usb_hcd *hcd;
41 42
	struct clk *usb11_clk;
	struct phy *usb11_phy;
43 44 45
	struct regulator *vbus_reg;
	struct notifier_block nb;
	unsigned int reg_enabled;
46 47 48
};

#define to_da8xx_ohci(hcd) (struct da8xx_ohci_hcd *)(hcd_to_ohci(hcd)->priv)
49 50 51 52

/* Over-current indicator change bitmask */
static volatile u16 ocic_mask;

53
static int ohci_da8xx_enable(struct usb_hcd *hcd)
54
{
55
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
56
	int ret;
57

58
	ret = clk_prepare_enable(da8xx_ohci->usb11_clk);
59 60
	if (ret)
		return ret;
61

62
	ret = phy_init(da8xx_ohci->usb11_phy);
63 64 65
	if (ret)
		goto err_phy_init;

66
	ret = phy_power_on(da8xx_ohci->usb11_phy);
67 68 69 70 71 72
	if (ret)
		goto err_phy_power_on;

	return 0;

err_phy_power_on:
73
	phy_exit(da8xx_ohci->usb11_phy);
74
err_phy_init:
75
	clk_disable_unprepare(da8xx_ohci->usb11_clk);
76 77 78 79

	return ret;
}

80
static void ohci_da8xx_disable(struct usb_hcd *hcd)
81
{
82 83 84 85 86
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);

	phy_power_off(da8xx_ohci->usb11_phy);
	phy_exit(da8xx_ohci->usb11_phy);
	clk_disable_unprepare(da8xx_ohci->usb11_clk);
87 88
}

89 90
static int ohci_da8xx_set_power(struct usb_hcd *hcd, int on)
{
91
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
92 93
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
94
	int ret;
95 96 97 98

	if (hub && hub->set_power)
		return hub->set_power(1, on);

99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
	if (!da8xx_ohci->vbus_reg)
		return 0;

	if (on && !da8xx_ohci->reg_enabled) {
		ret = regulator_enable(da8xx_ohci->vbus_reg);
		if (ret) {
			dev_err(dev, "Failed to enable regulator: %d\n", ret);
			return ret;
		}
		da8xx_ohci->reg_enabled = 1;

	} else if (!on && da8xx_ohci->reg_enabled) {
		ret = regulator_disable(da8xx_ohci->vbus_reg);
		if (ret) {
			dev_err(dev, "Failed  to disable regulator: %d\n", ret);
			return ret;
		}
		da8xx_ohci->reg_enabled = 0;
	}

119 120 121 122 123
	return 0;
}

static int ohci_da8xx_get_power(struct usb_hcd *hcd)
{
124
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
125 126 127 128 129 130
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);

	if (hub && hub->get_power)
		return hub->get_power(1);

131 132 133
	if (da8xx_ohci->vbus_reg)
		return regulator_is_enabled(da8xx_ohci->vbus_reg);

134 135 136 137 138
	return 1;
}

static int ohci_da8xx_get_oci(struct usb_hcd *hcd)
{
139
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
140 141
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
142 143
	unsigned int flags;
	int ret;
144 145 146 147

	if (hub && hub->get_oci)
		return hub->get_oci(1);

148 149 150 151 152 153 154 155 156 157
	if (!da8xx_ohci->vbus_reg)
		return 0;

	ret = regulator_get_error_flags(da8xx_ohci->vbus_reg, &flags);
	if (ret)
		return ret;

	if (flags & REGULATOR_ERROR_OVER_CURRENT)
		return 1;

158 159 160 161 162
	return 0;
}

static int ohci_da8xx_has_set_power(struct usb_hcd *hcd)
{
163
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
164 165 166 167 168 169
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);

	if (hub && hub->set_power)
		return 1;

170 171 172
	if (da8xx_ohci->vbus_reg)
		return 1;

173 174 175 176 177
	return 0;
}

static int ohci_da8xx_has_oci(struct usb_hcd *hcd)
{
178
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
179 180 181 182 183 184
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);

	if (hub && hub->get_oci)
		return 1;

185 186 187
	if (da8xx_ohci->vbus_reg)
		return 1;

188 189 190 191 192 193 194 195 196 197 198 199 200 201
	return 0;
}

static int ohci_da8xx_has_potpgt(struct usb_hcd *hcd)
{
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);

	if (hub && hub->potpgt)
		return 1;

	return 0;
}

202 203 204 205 206 207 208 209 210 211 212 213 214
/*
 * Handle the port over-current indicator change.
 */
static void ohci_da8xx_ocic_handler(struct da8xx_ohci_root_hub *hub,
				    unsigned port)
{
	ocic_mask |= 1 << port;

	/* Once over-current is detected, the port needs to be powered down */
	if (hub->get_oci(port) > 0)
		hub->set_power(port, 0);
}

215 216 217 218 219 220 221 222 223 224 225 226 227 228
static int ohci_da8xx_regulator_event(struct notifier_block *nb,
				unsigned long event, void *data)
{
	struct da8xx_ohci_hcd *da8xx_ohci =
				container_of(nb, struct da8xx_ohci_hcd, nb);

	if (event & REGULATOR_EVENT_OVER_CURRENT) {
		ocic_mask |= 1 << 1;
		ohci_da8xx_set_power(da8xx_ohci->hcd, 0);
	}

	return 0;
}

229 230
static int ohci_da8xx_register_notify(struct usb_hcd *hcd)
{
231
	struct da8xx_ohci_hcd *da8xx_ohci = to_da8xx_ohci(hcd);
232 233
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
234 235 236 237 238 239 240 241 242
	int ret = 0;

	if (hub && hub->ocic_notify) {
		ret = hub->ocic_notify(ohci_da8xx_ocic_handler);
	} else if (da8xx_ohci->vbus_reg) {
		da8xx_ohci->nb.notifier_call = ohci_da8xx_regulator_event;
		ret = devm_regulator_register_notifier(da8xx_ohci->vbus_reg,
						&da8xx_ohci->nb);
	}
243

244 245
	if (ret)
		dev_err(dev, "Failed to register notifier: %d\n", ret);
246

247
	return ret;
248 249 250 251 252 253 254 255 256 257 258
}

static void ohci_da8xx_unregister_notify(struct usb_hcd *hcd)
{
	struct device *dev		= hcd->self.controller;
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);

	if (hub && hub->ocic_notify)
		hub->ocic_notify(NULL);
}

259
static int ohci_da8xx_reset(struct usb_hcd *hcd)
260 261
{
	struct device *dev		= hcd->self.controller;
J
Jingoo Han 已提交
262
	struct da8xx_ohci_root_hub *hub	= dev_get_platdata(dev);
263 264 265 266 267 268
	struct ohci_hcd	*ohci		= hcd_to_ohci(hcd);
	int result;
	u32 rh_a;

	dev_dbg(dev, "starting USB controller\n");

269
	result = ohci_da8xx_enable(hcd);
270 271
	if (result < 0)
		return result;
272 273 274 275 276 277 278

	/*
	 * DA8xx only have 1 port connected to the pins but the HC root hub
	 * register A reports 2 ports, thus we'll have to override it...
	 */
	ohci->num_ports = 1;

279
	result = ohci_setup(hcd);
280
	if (result < 0) {
281
		ohci_da8xx_disable(hcd);
282
		return result;
283
	}
284 285 286 287 288 289 290 291

	/*
	 * Since we're providing a board-specific root hub port power control
	 * and over-current reporting, we have to override the HC root hub A
	 * register's default value, so that ohci_hub_control() could return
	 * the correct hub descriptor...
	 */
	rh_a = ohci_readl(ohci, &ohci->regs->roothub.a);
292
	if (ohci_da8xx_has_set_power(hcd)) {
293 294 295
		rh_a &= ~RH_A_NPS;
		rh_a |=  RH_A_PSM;
	}
296
	if (ohci_da8xx_has_oci(hcd)) {
297 298 299
		rh_a &= ~RH_A_NOCP;
		rh_a |=  RH_A_OCPM;
	}
300 301 302 303
	if (ohci_da8xx_has_potpgt(hcd)) {
		rh_a &= ~RH_A_POTPGT;
		rh_a |= hub->potpgt << 24;
	}
304 305 306 307 308 309 310 311 312 313
	ohci_writel(ohci, rh_a, &ohci->regs->roothub.a);

	return result;
}

/*
 * Update the status data from the hub with the over-current indicator change.
 */
static int ohci_da8xx_hub_status_data(struct usb_hcd *hcd, char *buf)
{
314
	int length		= orig_ohci_hub_status_data(hcd, buf);
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348

	/* See if we have OCIC bit set on port 1 */
	if (ocic_mask & (1 << 1)) {
		dev_dbg(hcd->self.controller, "over-current indicator change "
			"on port 1\n");

		if (!length)
			length = 1;

		buf[0] |= 1 << 1;
	}
	return length;
}

/*
 * Look at the control requests to the root hub and see if we need to override.
 */
static int ohci_da8xx_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
				  u16 wIndex, char *buf, u16 wLength)
{
	struct device *dev		= hcd->self.controller;
	int temp;

	switch (typeReq) {
	case GetPortStatus:
		/* Check the port number */
		if (wIndex != 1)
			break;

		dev_dbg(dev, "GetPortStatus(%u)\n", wIndex);

		temp = roothub_portstatus(hcd_to_ohci(hcd), wIndex - 1);

		/* The port power status (PPS) bit defaults to 1 */
349
		if (!ohci_da8xx_get_power(hcd))
350 351 352
			temp &= ~RH_PS_PPS;

		/* The port over-current indicator (POCI) bit is always 0 */
353
		if (ohci_da8xx_get_oci(hcd) > 0)
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
			temp |=  RH_PS_POCI;

		/* The over-current indicator change (OCIC) bit is 0 too */
		if (ocic_mask & (1 << wIndex))
			temp |=  RH_PS_OCIC;

		put_unaligned(cpu_to_le32(temp), (__le32 *)buf);
		return 0;
	case SetPortFeature:
		temp = 1;
		goto check_port;
	case ClearPortFeature:
		temp = 0;

check_port:
		/* Check the port number */
		if (wIndex != 1)
			break;

		switch (wValue) {
		case USB_PORT_FEAT_POWER:
			dev_dbg(dev, "%sPortFeature(%u): %s\n",
				temp ? "Set" : "Clear", wIndex, "POWER");

378
			return ohci_da8xx_set_power(hcd, temp) ? -EPIPE : 0;
379 380 381 382 383 384 385 386 387 388 389 390 391
		case USB_PORT_FEAT_C_OVER_CURRENT:
			dev_dbg(dev, "%sPortFeature(%u): %s\n",
				temp ? "Set" : "Clear", wIndex,
				"C_OVER_CURRENT");

			if (temp)
				ocic_mask |= 1 << wIndex;
			else
				ocic_mask &= ~(1 << wIndex);
			return 0;
		}
	}

392 393
	return orig_ohci_hub_control(hcd, typeReq, wValue,
			wIndex, buf, wLength);
394 395 396 397
}

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

398
static int ohci_da8xx_probe(struct platform_device *pdev)
399
{
400
	struct da8xx_ohci_hcd *da8xx_ohci;
401 402 403
	struct usb_hcd	*hcd;
	struct resource *mem;
	int error, irq;
404 405 406 407 408 409
	hcd = usb_create_hcd(&ohci_da8xx_hc_driver, &pdev->dev,
				dev_name(&pdev->dev));
	if (!hcd)
		return -ENOMEM;

	da8xx_ohci = to_da8xx_ohci(hcd);
410
	da8xx_ohci->hcd = hcd;
411 412 413 414 415

	da8xx_ohci->usb11_clk = devm_clk_get(&pdev->dev, "usb11");
	if (IS_ERR(da8xx_ohci->usb11_clk)) {
		error = PTR_ERR(da8xx_ohci->usb11_clk);
		if (error != -EPROBE_DEFER)
416
			dev_err(&pdev->dev, "Failed to get clock.\n");
417
		goto err;
418
	}
419

420 421 422 423
	da8xx_ohci->usb11_phy = devm_phy_get(&pdev->dev, "usb-phy");
	if (IS_ERR(da8xx_ohci->usb11_phy)) {
		error = PTR_ERR(da8xx_ohci->usb11_phy);
		if (error != -EPROBE_DEFER)
424
			dev_err(&pdev->dev, "Failed to get phy.\n");
425
		goto err;
426
	}
427

428 429 430 431 432 433 434 435 436 437 438 439 440
	da8xx_ohci->vbus_reg = devm_regulator_get_optional(&pdev->dev, "vbus");
	if (IS_ERR(da8xx_ohci->vbus_reg)) {
		error = PTR_ERR(da8xx_ohci->vbus_reg);
		if (error == -ENODEV) {
			da8xx_ohci->vbus_reg = NULL;
		} else if (error == -EPROBE_DEFER) {
			goto err;
		} else {
			dev_err(&pdev->dev, "Failed to get regulator\n");
			goto err;
		}
	}

441
	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
442 443 444 445
	hcd->regs = devm_ioremap_resource(&pdev->dev, mem);
	if (IS_ERR(hcd->regs)) {
		error = PTR_ERR(hcd->regs);
		goto err;
446
	}
447 448
	hcd->rsrc_start = mem->start;
	hcd->rsrc_len = resource_size(mem);
449 450 451 452

	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		error = -ENODEV;
453
		goto err;
454
	}
455

Y
Yong Zhang 已提交
456
	error = usb_add_hcd(hcd, irq, 0);
457
	if (error)
458
		goto err;
459

460 461
	device_wakeup_enable(hcd->self.controller);

462 463 464 465 466
	error = ohci_da8xx_register_notify(hcd);
	if (error)
		goto err_remove_hcd;

	return 0;
467

468
err_remove_hcd:
469
	usb_remove_hcd(hcd);
470
err:
471 472 473 474
	usb_put_hcd(hcd);
	return error;
}

475
static int ohci_da8xx_remove(struct platform_device *pdev)
476
{
477
	struct usb_hcd	*hcd = platform_get_drvdata(pdev);
478

479
	ohci_da8xx_unregister_notify(hcd);
480 481 482 483 484 485 486
	usb_remove_hcd(hcd);
	usb_put_hcd(hcd);

	return 0;
}

#ifdef CONFIG_PM
487 488
static int ohci_da8xx_suspend(struct platform_device *pdev,
				pm_message_t message)
489
{
490
	struct usb_hcd	*hcd	= platform_get_drvdata(pdev);
491
	struct ohci_hcd	*ohci	= hcd_to_ohci(hcd);
492 493 494
	bool		do_wakeup	= device_may_wakeup(&pdev->dev);
	int		ret;

495 496 497 498 499

	if (time_before(jiffies, ohci->next_statechange))
		msleep(5);
	ohci->next_statechange = jiffies;

500 501 502 503
	ret = ohci_suspend(hcd, do_wakeup);
	if (ret)
		return ret;

504
	ohci_da8xx_disable(hcd);
505
	hcd->state = HC_STATE_SUSPENDED;
506 507

	return ret;
508 509 510 511 512 513
}

static int ohci_da8xx_resume(struct platform_device *dev)
{
	struct usb_hcd	*hcd	= platform_get_drvdata(dev);
	struct ohci_hcd	*ohci	= hcd_to_ohci(hcd);
514
	int ret;
515 516 517 518 519

	if (time_before(jiffies, ohci->next_statechange))
		msleep(5);
	ohci->next_statechange = jiffies;

520
	ret = ohci_da8xx_enable(hcd);
521 522 523
	if (ret)
		return ret;

524 525
	dev->dev.power.power_state = PMSG_ON;
	usb_hcd_resume_root_hub(hcd);
526

527 528 529 530
	return 0;
}
#endif

531
static const struct ohci_driver_overrides da8xx_overrides __initconst = {
532 533
	.reset		 = ohci_da8xx_reset,
	.extra_priv_size = sizeof(struct da8xx_ohci_hcd),
534 535
};

536 537 538 539
/*
 * Driver definition to register with platform structure.
 */
static struct platform_driver ohci_hcd_da8xx_driver = {
540 541
	.probe		= ohci_da8xx_probe,
	.remove		= ohci_da8xx_remove,
542 543 544 545 546 547
	.shutdown 	= usb_hcd_platform_shutdown,
#ifdef	CONFIG_PM
	.suspend	= ohci_da8xx_suspend,
	.resume		= ohci_da8xx_resume,
#endif
	.driver		= {
548
		.name	= DRV_NAME,
549 550
	},
};
551

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587
static int __init ohci_da8xx_init(void)
{

	if (usb_disabled())
		return -ENODEV;

	pr_info("%s: " DRIVER_DESC "\n", DRV_NAME);
	ohci_init_driver(&ohci_da8xx_hc_driver, &da8xx_overrides);

	/*
	 * The Davinci da8xx HW has some unusual quirks, which require
	 * da8xx-specific workarounds. We override certain hc_driver
	 * functions here to achieve that. We explicitly do not enhance
	 * ohci_driver_overrides to allow this more easily, since this
	 * is an unusual case, and we don't want to encourage others to
	 * override these functions by making it too easy.
	 */

	orig_ohci_hub_control = ohci_da8xx_hc_driver.hub_control;
	orig_ohci_hub_status_data = ohci_da8xx_hc_driver.hub_status_data;

	ohci_da8xx_hc_driver.hub_status_data     = ohci_da8xx_hub_status_data;
	ohci_da8xx_hc_driver.hub_control         = ohci_da8xx_hub_control;

	return platform_driver_register(&ohci_hcd_da8xx_driver);
}
module_init(ohci_da8xx_init);

static void __exit ohci_da8xx_exit(void)
{
	platform_driver_unregister(&ohci_hcd_da8xx_driver);
}
module_exit(ohci_da8xx_exit);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
MODULE_ALIAS("platform:" DRV_NAME);