ohci-omap.c 13.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6
/*
 * OHCI HCD (Host Controller Driver) for USB.
 *
 * (C) Copyright 1999 Roman Weissgaerber <weissg@vienna.at>
 * (C) Copyright 2000-2005 David Brownell
 * (C) Copyright 2002 Hewlett-Packard Company
D
David Brownell 已提交
7
 *
L
Linus Torvalds 已提交
8 9 10 11 12 13 14 15 16
 * OMAP Bus Glue
 *
 * Modified for OMAP by Tony Lindgren <tony@atomide.com>
 * Based on the 2.4 OMAP OHCI driver originally done by MontaVista Software Inc.
 * and on ohci-sa1111.c by Christopher Hoover <ch@hpl.hp.com>
 *
 * This file is licenced under the GPL.
 */

Y
Yong Zhang 已提交
17
#include <linux/signal.h>
T
Tim Schmielau 已提交
18
#include <linux/jiffies.h>
19
#include <linux/platform_device.h>
20
#include <linux/clk.h>
21
#include <linux/gpio.h>
T
Tim Schmielau 已提交
22

23
#include <mach/hardware.h>
L
Linus Torvalds 已提交
24 25 26
#include <asm/io.h>
#include <asm/mach-types.h>

27
#include <plat/mux.h>
28
#include <mach/irqs.h>
29 30
#include <plat/fpga.h>
#include <plat/usb.h>
L
Linus Torvalds 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49


/* OMAP-1510 OHCI has its own MMU for DMA */
#define OMAP1510_LB_MEMSIZE	32	/* Should be same as SDRAM size */
#define OMAP1510_LB_CLOCK_DIV	0xfffec10c
#define OMAP1510_LB_MMU_CTL	0xfffec208
#define OMAP1510_LB_MMU_LCK	0xfffec224
#define OMAP1510_LB_MMU_LD_TLB	0xfffec228
#define OMAP1510_LB_MMU_CAM_H	0xfffec22c
#define OMAP1510_LB_MMU_CAM_L	0xfffec230
#define OMAP1510_LB_MMU_RAM_H	0xfffec234
#define OMAP1510_LB_MMU_RAM_L	0xfffec238


#ifndef CONFIG_ARCH_OMAP
#error "This file is OMAP bus glue.  CONFIG_OMAP must be defined."
#endif

#ifdef CONFIG_TPS65010
50
#include <linux/i2c/tps65010.h>
L
Linus Torvalds 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#else

#define LOW	0
#define HIGH	1

#define GPIO1	1

static inline int tps65010_set_gpio_out_value(unsigned gpio, unsigned value)
{
	return 0;
}

#endif

extern int usb_disabled(void);
extern int ocpi_enable(void);

static struct clk *usb_host_ck;
D
David Brownell 已提交
69 70 71
static struct clk *usb_dc_ck;
static int host_enabled;
static int host_initialized;
L
Linus Torvalds 已提交
72 73 74 75

static void omap_ohci_clock_power(int on)
{
	if (on) {
D
David Brownell 已提交
76
		clk_enable(usb_dc_ck);
L
Linus Torvalds 已提交
77 78 79 80 81
		clk_enable(usb_host_ck);
		/* guesstimate for T5 == 1x 32K clock + APLL lock time */
		udelay(100);
	} else {
		clk_disable(usb_host_ck);
D
David Brownell 已提交
82
		clk_disable(usb_dc_ck);
L
Linus Torvalds 已提交
83 84 85 86 87 88 89 90 91 92 93 94
	}
}

/*
 * Board specific gang-switched transceiver power on/off.
 * NOTE:  OSK supplies power from DC, not battery.
 */
static int omap_ohci_transceiver_power(int on)
{
	if (on) {
		if (machine_is_omap_innovator() && cpu_is_omap1510())
			fpga_write(fpga_read(INNOVATOR_FPGA_CAM_USB_CONTROL)
D
David Brownell 已提交
95
				| ((1 << 5/*usb1*/) | (1 << 3/*usb2*/)),
L
Linus Torvalds 已提交
96 97 98 99 100 101
			       INNOVATOR_FPGA_CAM_USB_CONTROL);
		else if (machine_is_omap_osk())
			tps65010_set_gpio_out_value(GPIO1, LOW);
	} else {
		if (machine_is_omap_innovator() && cpu_is_omap1510())
			fpga_write(fpga_read(INNOVATOR_FPGA_CAM_USB_CONTROL)
D
David Brownell 已提交
102
				& ~((1 << 5/*usb1*/) | (1 << 3/*usb2*/)),
L
Linus Torvalds 已提交
103 104 105 106 107 108 109 110
			       INNOVATOR_FPGA_CAM_USB_CONTROL);
		else if (machine_is_omap_osk())
			tps65010_set_gpio_out_value(GPIO1, HIGH);
	}

	return 0;
}

D
David Brownell 已提交
111
#ifdef CONFIG_ARCH_OMAP15XX
L
Linus Torvalds 已提交
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/*
 * OMAP-1510 specific Local Bus clock on/off
 */
static int omap_1510_local_bus_power(int on)
{
	if (on) {
		omap_writel((1 << 1) | (1 << 0), OMAP1510_LB_MMU_CTL);
		udelay(200);
	} else {
		omap_writel(0, OMAP1510_LB_MMU_CTL);
	}

	return 0;
}

/*
 * OMAP-1510 specific Local Bus initialization
 * NOTE: This assumes 32MB memory size in OMAP1510LB_MEMSIZE.
D
David Brownell 已提交
130 131
 *       See also arch/mach-omap/memory.h for __virt_to_dma() and
 *       __dma_to_virt() which need to match with the physical
L
Linus Torvalds 已提交
132 133 134 135 136 137 138
 *       Local Bus address below.
 */
static int omap_1510_local_bus_init(void)
{
	unsigned int tlb;
	unsigned long lbaddr, physaddr;

D
David Brownell 已提交
139
	omap_writel((omap_readl(OMAP1510_LB_CLOCK_DIV) & 0xfffffff8) | 0x4,
L
Linus Torvalds 已提交
140 141 142 143 144 145 146
	       OMAP1510_LB_CLOCK_DIV);

	/* Configure the Local Bus MMU table */
	for (tlb = 0; tlb < OMAP1510_LB_MEMSIZE; tlb++) {
		lbaddr = tlb * 0x00100000 + OMAP1510_LB_OFFSET;
		physaddr = tlb * 0x00100000 + PHYS_OFFSET;
		omap_writel((lbaddr & 0x0fffffff) >> 22, OMAP1510_LB_MMU_CAM_H);
D
David Brownell 已提交
147
		omap_writel(((lbaddr & 0x003ffc00) >> 6) | 0xc,
L
Linus Torvalds 已提交
148 149 150 151 152 153 154 155 156 157 158 159 160
		       OMAP1510_LB_MMU_CAM_L);
		omap_writel(physaddr >> 16, OMAP1510_LB_MMU_RAM_H);
		omap_writel((physaddr & 0x0000fc00) | 0x300, OMAP1510_LB_MMU_RAM_L);
		omap_writel(tlb << 4, OMAP1510_LB_MMU_LCK);
		omap_writel(0x1, OMAP1510_LB_MMU_LD_TLB);
	}

	/* Enable the walking table */
	omap_writel(omap_readl(OMAP1510_LB_MMU_CTL) | (1 << 3), OMAP1510_LB_MMU_CTL);
	udelay(200);

	return 0;
}
D
David Brownell 已提交
161 162 163 164
#else
#define omap_1510_local_bus_power(x)	{}
#define omap_1510_local_bus_init()	{}
#endif
L
Linus Torvalds 已提交
165 166 167 168 169 170 171

#ifdef	CONFIG_USB_OTG

static void start_hnp(struct ohci_hcd *ohci)
{
	const unsigned	port = ohci_to_hcd(ohci)->self.otg_port - 1;
	unsigned long	flags;
172
	u32 l;
L
Linus Torvalds 已提交
173

174
	otg_start_hnp(ohci->transceiver->otg);
L
Linus Torvalds 已提交
175 176 177 178

	local_irq_save(flags);
	ohci->transceiver->state = OTG_STATE_A_SUSPEND;
	writel (RH_PS_PSS, &ohci->regs->roothub.portstatus [port]);
179 180 181
	l = omap_readl(OTG_CTRL);
	l &= ~OTG_A_BUSREQ;
	omap_writel(l, OTG_CTRL);
L
Linus Torvalds 已提交
182 183 184 185 186 187 188
	local_irq_restore(flags);
}

#endif

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

D
David Brownell 已提交
189
static int ohci_omap_init(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
190
{
D
David Brownell 已提交
191 192
	struct ohci_hcd		*ohci = hcd_to_ohci(hcd);
	struct omap_usb_config	*config = hcd->self.controller->platform_data;
L
Linus Torvalds 已提交
193 194 195
	int			need_transceiver = (config->otg != 0);
	int			ret;

D
David Brownell 已提交
196
	dev_dbg(hcd->self.controller, "starting USB Controller\n");
L
Linus Torvalds 已提交
197 198 199 200

	if (config->otg) {
		ohci_to_hcd(ohci)->self.otg_port = config->otg;
		/* default/minimum OTG power budget:  8 mA */
201
		ohci_to_hcd(ohci)->power_budget = 8;
L
Linus Torvalds 已提交
202 203 204 205 206 207
	}

	/* boards can use OTG transceivers in non-OTG modes */
	need_transceiver = need_transceiver
			|| machine_is_omap_h2() || machine_is_omap_h3();

208 209 210
	/* XXX OMAP16xx only */
	if (config->ocpi_enable)
		config->ocpi_enable();
L
Linus Torvalds 已提交
211

212
#ifdef	CONFIG_USB_OTG
L
Linus Torvalds 已提交
213
	if (need_transceiver) {
214
		ohci->transceiver = usb_get_phy(USB_PHY_TYPE_USB2);
L
Linus Torvalds 已提交
215
		if (ohci->transceiver) {
216
			int	status = otg_set_host(ohci->transceiver->otg,
L
Linus Torvalds 已提交
217
						&ohci_to_hcd(ohci)->self);
D
David Brownell 已提交
218
			dev_dbg(hcd->self.controller, "init %s transceiver, status %d\n",
L
Linus Torvalds 已提交
219 220
					ohci->transceiver->label, status);
			if (status) {
221
				usb_put_phy(ohci->transceiver);
L
Linus Torvalds 已提交
222 223 224
				return status;
			}
		} else {
D
David Brownell 已提交
225
			dev_err(hcd->self.controller, "can't find transceiver\n");
L
Linus Torvalds 已提交
226 227
			return -ENODEV;
		}
228
		ohci->start_hnp = start_hnp;
L
Linus Torvalds 已提交
229 230 231 232 233
	}
#endif

	omap_ohci_clock_power(1);

234
	if (cpu_is_omap15xx()) {
L
Linus Torvalds 已提交
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
		omap_1510_local_bus_power(1);
		omap_1510_local_bus_init();
	}

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

	/* board-specific power switching and overcurrent support */
	if (machine_is_omap_osk() || machine_is_omap_innovator()) {
		u32	rh = roothub_a (ohci);

		/* power switching (ganged by default) */
		rh &= ~RH_A_NPS;

		/* TPS2045 switch for internal transceiver (port 1) */
		if (machine_is_omap_osk()) {
251
			ohci_to_hcd(ohci)->power_budget = 250;
L
Linus Torvalds 已提交
252 253 254 255 256

			rh &= ~RH_A_NOCP;

			/* gpio9 for overcurrent detction */
			omap_cfg_reg(W8_1610_GPIO9);
257
			gpio_request(9, "OHCI overcurrent");
258
			gpio_direction_input(9);
L
Linus Torvalds 已提交
259 260 261 262 263

			/* for paranoia's sake:  disable USB.PUEN */
			omap_cfg_reg(W4_USB_HIGHZ);
		}
		ohci_writel(ohci, rh, &ohci->regs->roothub.a);
264
		ohci->flags &= ~OHCI_QUIRK_HUB_POWER;
D
David Brownell 已提交
265 266 267 268
	} else if (machine_is_nokia770()) {
		/* We require a self-powered hub, which should have
		 * plenty of power. */
		ohci_to_hcd(ohci)->power_budget = 0;
L
Linus Torvalds 已提交
269 270 271 272 273 274 275 276 277 278 279 280 281
	}

	/* FIXME khubd hub requests should manage power switching */
	omap_ohci_transceiver_power(1);

	/* board init will have already handled HMC and mux setup.
	 * any external transceiver should already be initialized
	 * too, so all configured ports use the right signaling now.
	 */

	return 0;
}

D
David Brownell 已提交
282
static void ohci_omap_stop(struct usb_hcd *hcd)
L
Linus Torvalds 已提交
283
{
D
David Brownell 已提交
284
	dev_dbg(hcd->self.controller, "stopping USB Controller\n");
285
	ohci_stop(hcd);
L
Linus Torvalds 已提交
286 287 288 289 290 291 292 293 294 295 296 297 298 299
	omap_ohci_clock_power(0);
}


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

/**
 * usb_hcd_omap_probe - initialize OMAP-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.
 */
D
David Brownell 已提交
300
static int usb_hcd_omap_probe (const struct hc_driver *driver,
L
Linus Torvalds 已提交
301 302
			  struct platform_device *pdev)
{
303
	int retval, irq;
L
Linus Torvalds 已提交
304 305 306 307
	struct usb_hcd *hcd = 0;
	struct ohci_hcd *ohci;

	if (pdev->num_resources != 2) {
D
David Brownell 已提交
308
		printk(KERN_ERR "hcd probe: invalid num_resources: %i\n",
L
Linus Torvalds 已提交
309 310 311 312
		       pdev->num_resources);
		return -ENODEV;
	}

D
David Brownell 已提交
313
	if (pdev->resource[0].flags != IORESOURCE_MEM
L
Linus Torvalds 已提交
314 315 316 317 318
			|| pdev->resource[1].flags != IORESOURCE_IRQ) {
		printk(KERN_ERR "hcd probe: invalid resource type\n");
		return -ENODEV;
	}

319
	usb_host_ck = clk_get(&pdev->dev, "usb_hhc_ck");
L
Linus Torvalds 已提交
320 321 322
	if (IS_ERR(usb_host_ck))
		return PTR_ERR(usb_host_ck);

323
	if (!cpu_is_omap15xx())
324
		usb_dc_ck = clk_get(&pdev->dev, "usb_dc_ck");
D
David Brownell 已提交
325
	else
326
		usb_dc_ck = clk_get(&pdev->dev, "lb_ck");
D
David Brownell 已提交
327 328 329 330 331 332 333

	if (IS_ERR(usb_dc_ck)) {
		clk_put(usb_host_ck);
		return PTR_ERR(usb_dc_ck);
	}


334
	hcd = usb_create_hcd (driver, &pdev->dev, dev_name(&pdev->dev));
L
Linus Torvalds 已提交
335 336 337 338 339 340 341 342 343 344 345 346 347
	if (!hcd) {
		retval = -ENOMEM;
		goto err0;
	}
	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)) {
		dev_dbg(&pdev->dev, "request_mem_region failed\n");
		retval = -EBUSY;
		goto err1;
	}

348 349 350 351 352 353
	hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
	if (!hcd->regs) {
		dev_err(&pdev->dev, "can't ioremap OHCI HCD\n");
		retval = -ENOMEM;
		goto err2;
	}
L
Linus Torvalds 已提交
354 355 356 357

	ohci = hcd_to_ohci(hcd);
	ohci_hcd_init(ohci);

D
David Brownell 已提交
358 359
	host_initialized = 0;
	host_enabled = 1;
L
Linus Torvalds 已提交
360

361 362 363
	irq = platform_get_irq(pdev, 0);
	if (irq < 0) {
		retval = -ENXIO;
364
		goto err3;
365
	}
Y
Yong Zhang 已提交
366
	retval = usb_add_hcd(hcd, irq, 0);
D
David Brownell 已提交
367
	if (retval)
368
		goto err3;
D
David Brownell 已提交
369 370 371 372 373

	host_initialized = 1;

	if (!host_enabled)
		omap_ohci_clock_power(0);
L
Linus Torvalds 已提交
374

D
David Brownell 已提交
375
	return 0;
376 377
err3:
	iounmap(hcd->regs);
L
Linus Torvalds 已提交
378 379 380 381 382
err2:
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
err1:
	usb_put_hcd(hcd);
err0:
D
David Brownell 已提交
383
	clk_put(usb_dc_ck);
L
Linus Torvalds 已提交
384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
	clk_put(usb_host_ck);
	return retval;
}


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

/**
 * usb_hcd_omap_remove - shutdown processing for OMAP-based HCDs
 * @dev: USB Host Controller being removed
 * Context: !in_interrupt()
 *
 * Reverses the effect of usb_hcd_omap_probe(), first invoking
 * the HCD's stop() method.  It is always called from a thread
 * context, normally "rmmod", "apmd", or something similar.
 */
D
David Brownell 已提交
400 401
static inline void
usb_hcd_omap_remove (struct usb_hcd *hcd, struct platform_device *pdev)
L
Linus Torvalds 已提交
402
{
D
David Brownell 已提交
403 404
	struct ohci_hcd		*ohci = hcd_to_ohci (hcd);

L
Linus Torvalds 已提交
405
	usb_remove_hcd(hcd);
D
David Brownell 已提交
406
	if (ohci->transceiver) {
407
		(void) otg_set_host(ohci->transceiver->otg, 0);
408
		usb_put_phy(ohci->transceiver);
D
David Brownell 已提交
409
	}
L
Linus Torvalds 已提交
410
	if (machine_is_omap_osk())
411
		gpio_free(9);
412
	iounmap(hcd->regs);
L
Linus Torvalds 已提交
413 414
	release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
	usb_put_hcd(hcd);
D
David Brownell 已提交
415
	clk_put(usb_dc_ck);
L
Linus Torvalds 已提交
416 417 418 419 420
	clk_put(usb_host_ck);
}

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

D
David Brownell 已提交
421
static int
L
Linus Torvalds 已提交
422 423 424 425 426 427
ohci_omap_start (struct usb_hcd *hcd)
{
	struct omap_usb_config *config;
	struct ohci_hcd	*ohci = hcd_to_ohci (hcd);
	int		ret;

D
David Brownell 已提交
428 429
	if (!host_enabled)
		return 0;
L
Linus Torvalds 已提交
430
	config = hcd->self.controller->platform_data;
431 432
	if (config->otg || config->rwc) {
		ohci->hc_control = OHCI_CTRL_RWC;
L
Linus Torvalds 已提交
433
		writel(OHCI_CTRL_RWC, &ohci->regs->control);
434
	}
L
Linus Torvalds 已提交
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459

	if ((ret = ohci_run (ohci)) < 0) {
		dev_err(hcd->self.controller, "can't start\n");
		ohci_stop (hcd);
		return ret;
	}
	return 0;
}

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

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

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

	/*
	 * basic lifecycle operations
	 */
D
David Brownell 已提交
460
	.reset =		ohci_omap_init,
L
Linus Torvalds 已提交
461
	.start =		ohci_omap_start,
D
David Brownell 已提交
462
	.stop =			ohci_omap_stop,
463
	.shutdown =		ohci_shutdown,
L
Linus Torvalds 已提交
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481

	/*
	 * 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_hub_status_data,
	.hub_control =		ohci_hub_control,
482
#ifdef	CONFIG_PM
483 484
	.bus_suspend =		ohci_bus_suspend,
	.bus_resume =		ohci_bus_resume,
L
Linus Torvalds 已提交
485 486 487 488 489 490
#endif
	.start_port_reset =	ohci_start_port_reset,
};

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

491
static int ohci_hcd_omap_drv_probe(struct platform_device *dev)
L
Linus Torvalds 已提交
492
{
493
	return usb_hcd_omap_probe(&ohci_omap_hc_driver, dev);
L
Linus Torvalds 已提交
494 495
}

496
static int ohci_hcd_omap_drv_remove(struct platform_device *dev)
L
Linus Torvalds 已提交
497
{
498
	struct usb_hcd		*hcd = platform_get_drvdata(dev);
L
Linus Torvalds 已提交
499

500 501
	usb_hcd_omap_remove(hcd, dev);
	platform_set_drvdata(dev, NULL);
L
Linus Torvalds 已提交
502 503 504 505 506 507 508 509

	return 0;
}

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

#ifdef	CONFIG_PM

510
static int ohci_omap_suspend(struct platform_device *dev, pm_message_t message)
L
Linus Torvalds 已提交
511
{
512
	struct ohci_hcd	*ohci = hcd_to_ohci(platform_get_drvdata(dev));
D
David Brownell 已提交
513 514 515 516 517 518 519

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

	omap_ohci_clock_power(0);
	return 0;
L
Linus Torvalds 已提交
520 521
}

522
static int ohci_omap_resume(struct platform_device *dev)
L
Linus Torvalds 已提交
523
{
524 525
	struct usb_hcd	*hcd = platform_get_drvdata(dev);
	struct ohci_hcd	*ohci = hcd_to_ohci(hcd);
L
Linus Torvalds 已提交
526

527 528 529
	if (time_before(jiffies, ohci->next_statechange))
		msleep(5);
	ohci->next_statechange = jiffies;
D
David Brownell 已提交
530

531
	omap_ohci_clock_power(1);
532
	ohci_finish_controller_resume(hcd);
D
David Brownell 已提交
533
	return 0;
L
Linus Torvalds 已提交
534 535 536 537 538 539 540 541 542
}

#endif

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

/*
 * Driver definition to register with the OMAP bus
 */
543
static struct platform_driver ohci_hcd_omap_driver = {
L
Linus Torvalds 已提交
544 545
	.probe		= ohci_hcd_omap_drv_probe,
	.remove		= ohci_hcd_omap_drv_remove,
546
	.shutdown	= usb_hcd_platform_shutdown,
L
Linus Torvalds 已提交
547 548 549 550
#ifdef	CONFIG_PM
	.suspend	= ohci_omap_suspend,
	.resume		= ohci_omap_resume,
#endif
551 552 553 554
	.driver		= {
		.owner	= THIS_MODULE,
		.name	= "ohci",
	},
L
Linus Torvalds 已提交
555 556
};

557
MODULE_ALIAS("platform:ohci");