ohci-s3c2410.c 12.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * OHCI HCD (Host Controller Driver) for USB.
 *
 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
 * (C) Copyright 2000-2002 David Brownell <dbrownell@users.sourceforge.net>
 * (C) Copyright 2002 Hewlett-Packard Company
 *
 * USB Bus Glue for Samsung S3C2410
 *
 * Written by Christopher Hoover <ch@hpl.hp.com>
11
 * Based on fragments of previous driver by Russell King et al.
12 13 14 15 16 17 18 19 20 21
 *
 * Modified for S3C2410 from ohci-sa1111.c, ohci-omap.c and ohci-lh7a40.c
 *	by Ben Dooks, <ben@simtec.co.uk>
 *	Copyright (C) 2004 Simtec Electronics
 *
 * Thanks to basprog@mail.ru for updates to newer kernels
 *
 * This file is licenced under the GPL.
*/

22
#include <linux/platform_device.h>
23
#include <linux/clk.h>
24
#include <plat/usb-control.h>
25 26 27 28 29 30

#define valid_port(idx) ((idx) == 1 || (idx) == 2)

/* clock device associated with the hcd */

static struct clk *clk;
31
static struct clk *usb_clk;
32 33 34 35 36 37 38

/* forward definitions */

static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc);

/* conversion functions */

39
static struct s3c2410_hcd_info *to_s3c2410_info(struct usb_hcd *hcd)
40 41 42 43 44 45 46 47 48
{
	return hcd->self.controller->platform_data;
}

static void s3c2410_start_hc(struct platform_device *dev, struct usb_hcd *hcd)
{
	struct s3c2410_hcd_info *info = dev->dev.platform_data;

	dev_dbg(&dev->dev, "s3c2410_start_hc:\n");
49 50 51 52

	clk_enable(usb_clk);
	mdelay(2);			/* let the bus clock stabilise */

53 54 55 56 57 58
	clk_enable(clk);

	if (info != NULL) {
		info->hcd	= hcd;
		info->report_oc = s3c2410_hcd_oc;

59
		if (info->enable_oc != NULL)
60 61 62 63 64 65 66 67 68 69 70 71 72 73
			(info->enable_oc)(info, 1);
	}
}

static void s3c2410_stop_hc(struct platform_device *dev)
{
	struct s3c2410_hcd_info *info = dev->dev.platform_data;

	dev_dbg(&dev->dev, "s3c2410_stop_hc:\n");

	if (info != NULL) {
		info->report_oc = NULL;
		info->hcd	= NULL;

74
		if (info->enable_oc != NULL)
75 76 77 78
			(info->enable_oc)(info, 0);
	}

	clk_disable(clk);
79
	clk_disable(usb_clk);
80 81 82 83 84 85 86 87 88
}

/* ohci_s3c2410_hub_status_data
 *
 * update the status data from the hub with anything that
 * has been detected by our system
*/

static int
89
ohci_s3c2410_hub_status_data(struct usb_hcd *hcd, char *buf)
90 91 92 93 94 95
{
	struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
	struct s3c2410_hcd_port *port;
	int orig;
	int portno;

96
	orig  = ohci_hub_status_data(hcd, buf);
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134

	if (info == NULL)
		return orig;

	port = &info->port[0];

	/* mark any changed port as changed */

	for (portno = 0; portno < 2; port++, portno++) {
		if (port->oc_changed == 1 &&
		    port->flags & S3C_HCDFLG_USED) {
			dev_dbg(hcd->self.controller,
				"oc change on port %d\n", portno);

			if (orig < 1)
				orig = 1;

			buf[0] |= 1<<(portno+1);
		}
	}

	return orig;
}

/* s3c2410_usb_set_power
 *
 * configure the power on a port, by calling the platform device
 * routine registered with the platform device
*/

static void s3c2410_usb_set_power(struct s3c2410_hcd_info *info,
				  int port, int to)
{
	if (info == NULL)
		return;

	if (info->power_control != NULL) {
		info->port[port-1].power = to;
135
		(info->power_control)(port-1, to);
136 137 138 139 140 141 142 143 144 145
	}
}

/* ohci_s3c2410_hub_control
 *
 * look at control requests to the hub, and see if we need
 * to take any action or over-ride the results from the
 * request.
*/

146
static int ohci_s3c2410_hub_control(
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	struct usb_hcd	*hcd,
	u16		typeReq,
	u16		wValue,
	u16		wIndex,
	char		*buf,
	u16		wLength)
{
	struct s3c2410_hcd_info *info = to_s3c2410_info(hcd);
	struct usb_hub_descriptor *desc;
	int ret = -EINVAL;
	u32 *data = (u32 *)buf;

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

A
Alexey Dobriyan 已提交
163
	/* if we are only an humble host without any special capabilities
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
	 * process the request straight away and exit */

	if (info == NULL) {
		ret = ohci_hub_control(hcd, typeReq, wValue,
				       wIndex, buf, wLength);
		goto out;
	}

	/* check the request to see if it needs handling */

	switch (typeReq) {
	case SetPortFeature:
		if (wValue == USB_PORT_FEAT_POWER) {
			dev_dbg(hcd->self.controller, "SetPortFeat: POWER\n");
			s3c2410_usb_set_power(info, wIndex, 1);
			goto out;
		}
		break;

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

			if (valid_port(wIndex)) {
				info->port[wIndex-1].oc_changed = 0;
				info->port[wIndex-1].oc_status = 0;
			}

			goto out;

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

200
			if (valid_port(wIndex))
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
				info->port[wIndex-1].oc_status = 0;

			goto out;

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

			if (valid_port(wIndex)) {
				s3c2410_usb_set_power(info, wIndex, 0);
				return 0;
			}
		}
		break;
	}

	ret = ohci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
	if (ret)
		goto out;

	switch (typeReq) {
	case GetHubDescriptor:

		/* update the hub's descriptor */

		desc = (struct usb_hub_descriptor *)buf;

		if (info->power_control == NULL)
			return ret;

		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 (info->enable_oc) {
242 243 244 245 246
			desc->wHubCharacteristics &= ~cpu_to_le16(
				HUB_CHAR_OCPM);
			desc->wHubCharacteristics |=  cpu_to_le16(
				0x0008 |
				0x0001);
247 248 249 250 251 252 253 254 255 256 257 258 259
		}

		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);

		if (valid_port(wIndex)) {
260
			if (info->port[wIndex-1].oc_changed)
261 262
				*data |= cpu_to_le32(RH_PS_OCIC);

263
			if (info->port[wIndex-1].oc_status)
264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
				*data |= cpu_to_le32(RH_PS_POCI);
		}
	}

 out:
	return ret;
}

/* s3c2410_hcd_oc
 *
 * handle an over-current report
*/

static void s3c2410_hcd_oc(struct s3c2410_hcd_info *info, int port_oc)
{
	struct s3c2410_hcd_port *port;
	struct usb_hcd *hcd;
	unsigned long flags;
	int portno;

	if (info == NULL)
		return;

	port = &info->port[0];
	hcd = info->hcd;

	local_irq_save(flags);

	for (portno = 0; portno < 2; port++, portno++) {
		if (port_oc & (1<<portno) &&
		    port->flags & S3C_HCDFLG_USED) {
			port->oc_status = 1;
			port->oc_changed = 1;

			/* ok, once over-current is detected,
			   the port needs to be powered down */
			s3c2410_usb_set_power(info, portno+1, 0);
		}
	}

	local_irq_restore(flags);
}

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

/*
 * usb_hcd_s3c2410_remove - shutdown processing for HCD
 * @dev: USB Host Controller being removed
 * Context: !in_interrupt()
 *
 * Reverses the effect of usb_hcd_3c2410_probe(), first invoking
 * the HCD's stop() method.  It is always called from a thread
 * context, normally "rmmod", "apmd", or something similar.
 *
*/

321
static void
322
usb_hcd_s3c2410_remove(struct usb_hcd *hcd, struct platform_device *dev)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339
{
	usb_remove_hcd(hcd);
	s3c2410_stop_hc(dev);
	iounmap(hcd->regs);
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
	usb_put_hcd(hcd);
}

/**
 * usb_hcd_s3c2410_probe - initialize S3C2410-based HCDs
 * 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.
 *
 */
340
static int usb_hcd_s3c2410_probe(const struct hc_driver *driver,
341
				  struct platform_device *dev)
342 343 344 345 346
{
	struct usb_hcd *hcd = NULL;
	int retval;

	s3c2410_usb_set_power(dev->dev.platform_data, 1, 1);
347
	s3c2410_usb_set_power(dev->dev.platform_data, 2, 1);
348 349 350 351 352 353

	hcd = usb_create_hcd(driver, &dev->dev, "s3c24xx");
	if (hcd == NULL)
		return -ENOMEM;

	hcd->rsrc_start = dev->resource[0].start;
354
	hcd->rsrc_len	= resource_size(&dev->resource[0]);
355 356

	if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
357
		dev_err(&dev->dev, "request_mem_region failed\n");
358
		retval = -EBUSY;
359
		goto err_put;
360 361
	}

362
	clk = clk_get(&dev->dev, "usb-host");
363 364
	if (IS_ERR(clk)) {
		dev_err(&dev->dev, "cannot get usb-host clock\n");
365
		retval = PTR_ERR(clk);
366 367 368
		goto err_mem;
	}

369
	usb_clk = clk_get(&dev->dev, "usb-bus-host");
370
	if (IS_ERR(usb_clk)) {
371
		dev_err(&dev->dev, "cannot get usb-bus-host clock\n");
372
		retval = PTR_ERR(usb_clk);
373
		goto err_clk;
374 375 376 377 378 379 380 381
	}

	s3c2410_start_hc(dev, hcd);

	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
	if (!hcd->regs) {
		dev_err(&dev->dev, "ioremap failed\n");
		retval = -ENOMEM;
382
		goto err_ioremap;
383 384 385 386
	}

	ohci_hcd_init(hcd_to_ohci(hcd));

Y
Yong Zhang 已提交
387
	retval = usb_add_hcd(hcd, dev->resource[1].start, 0);
388
	if (retval != 0)
389
		goto err_ioremap;
390 391 392

	return 0;

393
 err_ioremap:
394 395
	s3c2410_stop_hc(dev);
	iounmap(hcd->regs);
396 397 398
	clk_put(usb_clk);

 err_clk:
399 400
	clk_put(clk);

401
 err_mem:
402 403
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);

404
 err_put:
405 406 407 408 409 410 411
	usb_put_hcd(hcd);
	return retval;
}

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

static int
412
ohci_s3c2410_start(struct usb_hcd *hcd)
413
{
414
	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);
415 416
	int ret;

417 418
	ret = ohci_init(ohci);
	if (ret < 0)
419 420
		return ret;

421 422
	ret = ohci_run(ohci);
	if (ret < 0) {
423 424
		dev_err(hcd->self.controller, "can't start %s\n",
			hcd->self.bus_name);
425
		ohci_stop(hcd);
426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
		return ret;
	}

	return 0;
}


static const struct hc_driver ohci_s3c2410_hc_driver = {
	.description =		hcd_name,
	.product_desc =		"S3C24XX OHCI",
	.hcd_priv_size =	sizeof(struct ohci_hcd),

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

	/*
	 * basic lifecycle operations
	 */
	.start =		ohci_s3c2410_start,
	.stop =			ohci_stop,
449
	.shutdown =		ohci_shutdown,
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467

	/*
	 * 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
	 */
	.hub_status_data =	ohci_s3c2410_hub_status_data,
	.hub_control =		ohci_s3c2410_hub_control,
468
#ifdef	CONFIG_PM
469 470
	.bus_suspend =		ohci_bus_suspend,
	.bus_resume =		ohci_bus_resume,
471
#endif
472
	.start_port_reset =	ohci_start_port_reset,
473 474 475 476
};

/* device driver */

477
static int __devinit ohci_hcd_s3c2410_drv_probe(struct platform_device *pdev)
478 479 480 481
{
	return usb_hcd_s3c2410_probe(&ohci_s3c2410_hc_driver, pdev);
}

482
static int __devexit ohci_hcd_s3c2410_drv_remove(struct platform_device *pdev)
483
{
484
	struct usb_hcd *hcd = platform_get_drvdata(pdev);
485 486 487 488 489

	usb_hcd_s3c2410_remove(hcd, pdev);
	return 0;
}

J
Jingoo Han 已提交
490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541
#ifdef CONFIG_PM
static int ohci_hcd_s3c2410_drv_suspend(struct device *dev)
{
	struct usb_hcd *hcd = dev_get_drvdata(dev);
	struct ohci_hcd *ohci = hcd_to_ohci(hcd);
	struct platform_device *pdev = to_platform_device(dev);
	unsigned long flags;
	int rc = 0;

	/*
	 * Root hub was already suspended. Disable irq emission and
	 * mark HW unaccessible, bail out if RH has been resumed. Use
	 * the spinlock to properly synchronize with possible pending
	 * RH suspend or resume activity.
	 */
	spin_lock_irqsave(&ohci->lock, flags);
	if (ohci->rh_state != OHCI_RH_SUSPENDED) {
		rc = -EINVAL;
		goto bail;
	}

	clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);

	s3c2410_stop_hc(pdev);
bail:
	spin_unlock_irqrestore(&ohci->lock, flags);

	return rc;
}

static int ohci_hcd_s3c2410_drv_resume(struct device *dev)
{
	struct usb_hcd *hcd = dev_get_drvdata(dev);
	struct platform_device *pdev = to_platform_device(dev);

	s3c2410_start_hc(pdev, hcd);

	set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
	ohci_finish_controller_resume(hcd);

	return 0;
}
#else
#define ohci_hcd_s3c2410_drv_suspend	NULL
#define ohci_hcd_s3c2410_drv_resume	NULL
#endif

static const struct dev_pm_ops ohci_hcd_s3c2410_pm_ops = {
	.suspend	= ohci_hcd_s3c2410_drv_suspend,
	.resume		= ohci_hcd_s3c2410_drv_resume,
};

542
static struct platform_driver ohci_hcd_s3c2410_driver = {
543
	.probe		= ohci_hcd_s3c2410_drv_probe,
544
	.remove		= __devexit_p(ohci_hcd_s3c2410_drv_remove),
545
	.shutdown	= usb_hcd_platform_shutdown,
546 547 548
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "s3c2410-ohci",
J
Jingoo Han 已提交
549
		.pm	= &ohci_hcd_s3c2410_pm_ops,
550
	},
551 552
};

553
MODULE_ALIAS("platform:s3c2410-ohci");