blackfin.c 15.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/*
 * MUSB OTG controller driver for Blackfin Processors
 *
 * Copyright 2006-2008 Analog Devices Inc.
 *
 * Enter bugs at http://blackfin.uclinux.org/
 *
 * Licensed under the GPL-2 or later.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/sched.h>
#include <linux/list.h>
#include <linux/gpio.h>
#include <linux/io.h>
17
#include <linux/err.h>
18 19
#include <linux/platform_device.h>
#include <linux/dma-mapping.h>
20
#include <linux/prefetch.h>
21
#include <linux/usb/usb_phy_generic.h>
22 23 24 25

#include <asm/cacheflush.h>

#include "musb_core.h"
26
#include "musbhsdma.h"
27 28
#include "blackfin.h"

29 30 31
struct bfin_glue {
	struct device		*dev;
	struct platform_device	*musb;
32
	struct platform_device	*phy;
33
};
34
#define glue_to_musb(g)		platform_get_drvdata(g->musb)
35

36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
static u32 bfin_fifo_offset(u8 epnum)
{
	return USB_OFFSET(USB_EP0_FIFO) + (epnum * 8);
}

static u8 bfin_readb(const void __iomem *addr, unsigned offset)
{
	return (u8)(bfin_read16(addr + offset));
}

static u16 bfin_readw(const void __iomem *addr, unsigned offset)
{
	return bfin_read16(addr + offset);
}

static u32 bfin_readl(const void __iomem *addr, unsigned offset)
{
	return (u32)(bfin_read16(addr + offset));
}

static void bfin_writeb(void __iomem *addr, unsigned offset, u8 data)
{
	bfin_write16(addr + offset, (u16)data);
}

static void bfin_writew(void __iomem *addr, unsigned offset, u16 data)
{
	bfin_write16(addr + offset, data);
}

66
static void bfin_writel(void __iomem *addr, unsigned offset, u32 data)
67 68 69 70
{
	bfin_write16(addr + offset, (u16)data);
}

71 72 73
/*
 * Load an endpoint's FIFO
 */
74
static void bfin_write_fifo(struct musb_hw_ep *hw_ep, u16 len, const u8 *src)
75
{
F
Felipe Balbi 已提交
76
	struct musb *musb = hw_ep->musb;
77 78
	void __iomem *fifo = hw_ep->fifo;
	void __iomem *epio = hw_ep->regs;
79
	u8 epnum = hw_ep->epnum;
80 81 82 83 84

	prefetch((u8 *)src);

	musb_writew(epio, MUSB_TXCOUNT, len);

85
	dev_dbg(musb->controller, "TX ep%d fifo %p count %d buf %p, epio %p\n",
86 87 88 89
			hw_ep->epnum, fifo, len, src, epio);

	dump_fifo_data(src, len);

90
	if (!ANOMALY_05000380 && epnum != 0) {
91 92 93 94
		u16 dma_reg;

		flush_dcache_range((unsigned long)src,
			(unsigned long)(src + len));
95 96

		/* Setup DMA address register */
97
		dma_reg = (u32)src;
98 99 100
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
		SSYNC();

101
		dma_reg = (u32)src >> 16;
102 103 104 105 106 107 108 109 110 111 112 113 114
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
		SSYNC();

		/* Setup DMA count register */
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
		SSYNC();

		/* Enable the DMA */
		dma_reg = (epnum << 4) | DMA_ENA | INT_ENA | DIRECTION;
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
		SSYNC();

115
		/* Wait for complete */
116 117 118 119 120 121 122 123 124 125 126 127 128 129
		while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
			cpu_relax();

		/* acknowledge dma interrupt */
		bfin_write_USB_DMA_INTERRUPT(1 << epnum);
		SSYNC();

		/* Reset DMA */
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
		SSYNC();
	} else {
		SSYNC();

		if (unlikely((unsigned long)src & 0x01))
130
			outsw_8((unsigned long)fifo, src, (len + 1) >> 1);
131
		else
132
			outsw((unsigned long)fifo, src, (len + 1) >> 1);
133
	}
134 135 136 137
}
/*
 * Unload an endpoint's FIFO
 */
138
static void bfin_read_fifo(struct musb_hw_ep *hw_ep, u16 len, u8 *dst)
139
{
F
Felipe Balbi 已提交
140
	struct musb *musb = hw_ep->musb;
141 142 143
	void __iomem *fifo = hw_ep->fifo;
	u8 epnum = hw_ep->epnum;

144
	if (ANOMALY_05000467 && epnum != 0) {
145
		u16 dma_reg;
146

147 148
		invalidate_dcache_range((unsigned long)dst,
			(unsigned long)(dst + len));
149 150

		/* Setup DMA address register */
151
		dma_reg = (u32)dst;
152 153 154
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_LOW), dma_reg);
		SSYNC();

155
		dma_reg = (u32)dst >> 16;
156 157 158 159 160 161 162 163 164 165 166 167 168
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_ADDR_HIGH), dma_reg);
		SSYNC();

		/* Setup DMA count register */
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_LOW), len);
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_COUNT_HIGH), 0);
		SSYNC();

		/* Enable the DMA */
		dma_reg = (epnum << 4) | DMA_ENA | INT_ENA;
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), dma_reg);
		SSYNC();

169
		/* Wait for complete */
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
		while (!(bfin_read_USB_DMA_INTERRUPT() & (1 << epnum)))
			cpu_relax();

		/* acknowledge dma interrupt */
		bfin_write_USB_DMA_INTERRUPT(1 << epnum);
		SSYNC();

		/* Reset DMA */
		bfin_write16(USB_DMA_REG(epnum, USB_DMAx_CTRL), 0);
		SSYNC();
	} else {
		SSYNC();
		/* Read the last byte of packet with odd size from address fifo + 4
		 * to trigger 1 byte access to EP0 FIFO.
		 */
		if (len == 1)
			*dst = (u8)inw((unsigned long)fifo + 4);
		else {
			if (unlikely((unsigned long)dst & 0x01))
				insw_8((unsigned long)fifo, dst, len >> 1);
			else
				insw((unsigned long)fifo, dst, len >> 1);

			if (len & 0x01)
				*(dst + len - 1) = (u8)inw((unsigned long)fifo + 4);
		}
	}
197
	dev_dbg(musb->controller, "%cX ep%d fifo %p count %d buf %p\n",
198 199
			'R', hw_ep->epnum, fifo, len, dst);

200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
	dump_fifo_data(dst, len);
}

static irqreturn_t blackfin_interrupt(int irq, void *__hci)
{
	unsigned long	flags;
	irqreturn_t	retval = IRQ_NONE;
	struct musb	*musb = __hci;

	spin_lock_irqsave(&musb->lock, flags);

	musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
	musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
	musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);

	if (musb->int_usb || musb->int_tx || musb->int_rx) {
		musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
		musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
		musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
		retval = musb_interrupt(musb);
	}

222
	/* Start sampling ID pin, when plug is removed from MUSB */
223 224
	if ((musb->xceiv->otg->state == OTG_STATE_B_IDLE
		|| musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON) ||
225
		(musb->int_usb & MUSB_INTR_DISCONNECT && is_host_active(musb))) {
226 227 228 229
		mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
		musb->a_wait_bcon = TIMER_DELAY;
	}

230 231
	spin_unlock_irqrestore(&musb->lock, flags);

232
	return retval;
233 234 235 236 237 238 239
}

static void musb_conn_timer_handler(unsigned long _musb)
{
	struct musb *musb = (void *)_musb;
	unsigned long flags;
	u16 val;
240
	static u8 toggle;
241 242

	spin_lock_irqsave(&musb->lock, flags);
243
	switch (musb->xceiv->otg->state) {
244 245 246 247
	case OTG_STATE_A_IDLE:
	case OTG_STATE_A_WAIT_BCON:
		/* Start a new session */
		val = musb_readw(musb->mregs, MUSB_DEVCTL);
248 249
		val &= ~MUSB_DEVCTL_SESSION;
		musb_writew(musb->mregs, MUSB_DEVCTL, val);
250 251
		val |= MUSB_DEVCTL_SESSION;
		musb_writew(musb->mregs, MUSB_DEVCTL, val);
252 253 254 255 256
		/* Check if musb is host or peripheral. */
		val = musb_readw(musb->mregs, MUSB_DEVCTL);

		if (!(val & MUSB_DEVCTL_BDEVICE)) {
			gpio_set_value(musb->config->gpio_vrsel, 1);
257
			musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
258 259 260 261 262 263 264 265 266
		} else {
			gpio_set_value(musb->config->gpio_vrsel, 0);
			/* Ignore VBUSERROR and SUSPEND IRQ */
			val = musb_readb(musb->mregs, MUSB_INTRUSBE);
			val &= ~MUSB_INTR_VBUSERROR;
			musb_writeb(musb->mregs, MUSB_INTRUSBE, val);

			val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
			musb_writeb(musb->mregs, MUSB_INTRUSB, val);
267
			musb->xceiv->otg->state = OTG_STATE_B_IDLE;
268 269 270 271
		}
		mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY);
		break;
	case OTG_STATE_B_IDLE:
272 273
		/*
		 * Start a new session.  It seems that MUSB needs taking
274 275 276 277 278
		 * some time to recognize the type of the plug inserted?
		 */
		val = musb_readw(musb->mregs, MUSB_DEVCTL);
		val |= MUSB_DEVCTL_SESSION;
		musb_writew(musb->mregs, MUSB_DEVCTL, val);
279
		val = musb_readw(musb->mregs, MUSB_DEVCTL);
280

281 282
		if (!(val & MUSB_DEVCTL_BDEVICE)) {
			gpio_set_value(musb->config->gpio_vrsel, 1);
283
			musb->xceiv->otg->state = OTG_STATE_A_WAIT_BCON;
284 285 286 287 288 289 290 291 292 293 294
		} else {
			gpio_set_value(musb->config->gpio_vrsel, 0);

			/* Ignore VBUSERROR and SUSPEND IRQ */
			val = musb_readb(musb->mregs, MUSB_INTRUSBE);
			val &= ~MUSB_INTR_VBUSERROR;
			musb_writeb(musb->mregs, MUSB_INTRUSBE, val);

			val = MUSB_INTR_SUSPEND | MUSB_INTR_VBUSERROR;
			musb_writeb(musb->mregs, MUSB_INTRUSB, val);

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
			/* Toggle the Soft Conn bit, so that we can response to
			 * the inserting of either A-plug or B-plug.
			 */
			if (toggle) {
				val = musb_readb(musb->mregs, MUSB_POWER);
				val &= ~MUSB_POWER_SOFTCONN;
				musb_writeb(musb->mregs, MUSB_POWER, val);
				toggle = 0;
			} else {
				val = musb_readb(musb->mregs, MUSB_POWER);
				val |= MUSB_POWER_SOFTCONN;
				musb_writeb(musb->mregs, MUSB_POWER, val);
				toggle = 1;
			}
			/* The delay time is set to 1/4 second by default,
			 * shortening it, if accelerating A-plug detection
			 * is needed in OTG mode.
			 */
			mod_timer(&musb_conn_timer, jiffies + TIMER_DELAY / 4);
314 315 316
		}
		break;
	default:
317
		dev_dbg(musb->controller, "%s state not handled\n",
318
			usb_otg_state_string(musb->xceiv->otg->state));
319 320 321 322
		break;
	}
	spin_unlock_irqrestore(&musb->lock, flags);

323
	dev_dbg(musb->controller, "state is %s\n",
324
		usb_otg_state_string(musb->xceiv->otg->state));
325 326
}

327
static void bfin_musb_enable(struct musb *musb)
328
{
329
	/* REVISIT is this really correct ? */
330 331
}

332
static void bfin_musb_disable(struct musb *musb)
333 334 335
{
}

336
static void bfin_musb_set_vbus(struct musb *musb, int is_on)
337
{
338 339 340 341
	int value = musb->config->gpio_vrsel_active;
	if (!is_on)
		value = !value;
	gpio_set_value(musb->config->gpio_vrsel, value);
342

343
	dev_dbg(musb->controller, "VBUS %s, devctl %02x "
344
		/* otg %3x conf %08x prcm %08x */ "\n",
345
		usb_otg_state_string(musb->xceiv->otg->state),
346 347 348
		musb_readb(musb->mregs, MUSB_DEVCTL));
}

349
static int bfin_musb_set_power(struct usb_phy *x, unsigned mA)
350 351 352 353
{
	return 0;
}

354
static int bfin_musb_vbus_status(struct musb *musb)
355 356 357 358
{
	return 0;
}

359
static int bfin_musb_set_mode(struct musb *musb, u8 musb_mode)
360
{
361
	return -EIO;
362 363
}

364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
static int bfin_musb_adjust_channel_params(struct dma_channel *channel,
				u16 packet_sz, u8 *mode,
				dma_addr_t *dma_addr, u32 *len)
{
	struct musb_dma_channel *musb_channel = channel->private_data;

	/*
	 * Anomaly 05000450 might cause data corruption when using DMA
	 * MODE 1 transmits with short packet.  So to work around this,
	 * we truncate all MODE 1 transfers down to a multiple of the
	 * max packet size, and then do the last short packet transfer
	 * (if there is any) using MODE 0.
	 */
	if (ANOMALY_05000450) {
		if (musb_channel->transmit && *mode == 1)
			*len = *len - (*len % packet_sz);
	}

	return 0;
}

385
static void bfin_musb_reg_init(struct musb *musb)
386
{
387 388 389 390
	if (ANOMALY_05000346) {
		bfin_write_USB_APHY_CALIB(ANOMALY_05000346_value);
		SSYNC();
	}
391

392 393 394 395
	if (ANOMALY_05000347) {
		bfin_write_USB_APHY_CNTRL(0x0);
		SSYNC();
	}
396 397

	/* Configure PLL oscillator register */
398 399
	bfin_write_USB_PLLOSC_CTRL(0x3080 |
			((480/musb->config->clkin) << 1));
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
	SSYNC();

	bfin_write_USB_SRP_CLKDIV((get_sclk()/1000) / 32 - 1);
	SSYNC();

	bfin_write_USB_EP_NI0_RXMAXP(64);
	SSYNC();

	bfin_write_USB_EP_NI0_TXMAXP(64);
	SSYNC();

	/* Route INTRUSB/INTR_RX/INTR_TX to USB_INT0*/
	bfin_write_USB_GLOBINTR(0x7);
	SSYNC();

	bfin_write_USB_GLOBAL_CTL(GLOBAL_ENA | EP1_TX_ENA | EP2_TX_ENA |
				EP3_TX_ENA | EP4_TX_ENA | EP5_TX_ENA |
				EP6_TX_ENA | EP7_TX_ENA | EP1_RX_ENA |
				EP2_RX_ENA | EP3_RX_ENA | EP4_RX_ENA |
				EP5_RX_ENA | EP6_RX_ENA | EP7_RX_ENA);
	SSYNC();
421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
}

static int bfin_musb_init(struct musb *musb)
{

	/*
	 * Rev 1.0 BF549 EZ-KITs require PE7 to be high for both DEVICE
	 * and OTG HOST modes, while rev 1.1 and greater require PE7 to
	 * be low for DEVICE mode and high for HOST mode. We set it high
	 * here because we are in host mode
	 */

	if (gpio_request(musb->config->gpio_vrsel, "USB_VRSEL")) {
		printk(KERN_ERR "Failed ro request USB_VRSEL GPIO_%d\n",
			musb->config->gpio_vrsel);
		return -ENODEV;
	}
	gpio_direction_output(musb->config->gpio_vrsel, 0);

440
	musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
441
	if (IS_ERR_OR_NULL(musb->xceiv)) {
442
		gpio_free(musb->config->gpio_vrsel);
443
		return -EPROBE_DEFER;
444 445 446
	}

	bfin_musb_reg_init(musb);
447

448 449 450 451
	setup_timer(&musb_conn_timer, musb_conn_timer_handler,
			(unsigned long) musb);

	musb->xceiv->set_power = bfin_musb_set_power;
452 453

	musb->isr = blackfin_interrupt;
454
	musb->double_buffer_not_ok = true;
455 456 457 458

	return 0;
}

459
static int bfin_musb_exit(struct musb *musb)
460 461
{
	gpio_free(musb->config->gpio_vrsel);
462
	usb_put_phy(musb->xceiv);
463

464 465
	return 0;
}
466

467
static const struct musb_platform_ops bfin_ops = {
468 469 470
	.init		= bfin_musb_init,
	.exit		= bfin_musb_exit,

471 472 473 474 475 476
	.readb		= bfin_readb,
	.writeb		= bfin_writeb,
	.readw		= bfin_readw,
	.writew		= bfin_writew,
	.readl		= bfin_readl,
	.writel		= bfin_writel,
477
	.fifo_mode	= 2,
478 479
	.read_fifo	= bfin_read_fifo,
	.write_fifo	= bfin_write_fifo,
480 481 482 483 484 485 486
	.enable		= bfin_musb_enable,
	.disable	= bfin_musb_disable,

	.set_mode	= bfin_musb_set_mode,

	.vbus_status	= bfin_musb_vbus_status,
	.set_vbus	= bfin_musb_set_vbus,
487 488

	.adjust_channel_params = bfin_musb_adjust_channel_params,
489
};
490 491 492

static u64 bfin_dmamask = DMA_BIT_MASK(32);

B
Bill Pemberton 已提交
493
static int bfin_probe(struct platform_device *pdev)
494
{
495
	struct resource musb_resources[2];
J
Jingoo Han 已提交
496
	struct musb_hdrc_platform_data	*pdata = dev_get_platdata(&pdev->dev);
497
	struct platform_device		*musb;
498
	struct bfin_glue		*glue;
499 500 501

	int				ret = -ENOMEM;

502
	glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
503
	if (!glue)
504 505
		goto err0;

506
	musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
507
	if (!musb)
508
		goto err0;
509 510 511 512 513

	musb->dev.parent		= &pdev->dev;
	musb->dev.dma_mask		= &bfin_dmamask;
	musb->dev.coherent_dma_mask	= bfin_dmamask;

514 515 516
	glue->dev			= &pdev->dev;
	glue->musb			= musb;

517 518
	pdata->platform_ops		= &bfin_ops;

519 520
	glue->phy = usb_phy_generic_register();
	if (IS_ERR(glue->phy))
521
		goto err1;
522
	platform_set_drvdata(pdev, glue);
523

524 525 526 527 528 529 530 531 532 533 534 535 536 537 538
	memset(musb_resources, 0x00, sizeof(*musb_resources) *
			ARRAY_SIZE(musb_resources));

	musb_resources[0].name = pdev->resource[0].name;
	musb_resources[0].start = pdev->resource[0].start;
	musb_resources[0].end = pdev->resource[0].end;
	musb_resources[0].flags = pdev->resource[0].flags;

	musb_resources[1].name = pdev->resource[1].name;
	musb_resources[1].start = pdev->resource[1].start;
	musb_resources[1].end = pdev->resource[1].end;
	musb_resources[1].flags = pdev->resource[1].flags;

	ret = platform_device_add_resources(musb, musb_resources,
			ARRAY_SIZE(musb_resources));
539 540
	if (ret) {
		dev_err(&pdev->dev, "failed to add resources\n");
541
		goto err2;
542 543 544 545 546
	}

	ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
	if (ret) {
		dev_err(&pdev->dev, "failed to add platform_data\n");
547
		goto err2;
548 549 550 551 552
	}

	ret = platform_device_add(musb);
	if (ret) {
		dev_err(&pdev->dev, "failed to register musb device\n");
553
		goto err2;
554 555 556 557
	}

	return 0;

558
err2:
559
	usb_phy_generic_unregister(glue->phy);
560

561
err1:
562
	platform_device_put(musb);
563

564 565 566 567
err0:
	return ret;
}

B
Bill Pemberton 已提交
568
static int bfin_remove(struct platform_device *pdev)
569
{
570
	struct bfin_glue		*glue = platform_get_drvdata(pdev);
571

572
	platform_device_unregister(glue->musb);
573
	usb_phy_generic_unregister(glue->phy);
574 575 576 577

	return 0;
}

578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606
#ifdef CONFIG_PM
static int bfin_suspend(struct device *dev)
{
	struct bfin_glue	*glue = dev_get_drvdata(dev);
	struct musb		*musb = glue_to_musb(glue);

	if (is_host_active(musb))
		/*
		 * During hibernate gpio_vrsel will change from high to low
		 * low which will generate wakeup event resume the system
		 * immediately.  Set it to 0 before hibernate to avoid this
		 * wakeup event.
		 */
		gpio_set_value(musb->config->gpio_vrsel, 0);

	return 0;
}

static int bfin_resume(struct device *dev)
{
	struct bfin_glue	*glue = dev_get_drvdata(dev);
	struct musb		*musb = glue_to_musb(glue);

	bfin_musb_reg_init(musb);

	return 0;
}
#endif

607 608
static SIMPLE_DEV_PM_OPS(bfin_pm_ops, bfin_suspend, bfin_resume);

609
static struct platform_driver bfin_driver = {
610
	.probe		= bfin_probe,
611
	.remove		= bfin_remove,
612
	.driver		= {
613
		.name	= "musb-blackfin",
614
		.pm	= &bfin_pm_ops,
615 616 617 618 619 620
	},
};

MODULE_DESCRIPTION("Blackfin MUSB Glue Layer");
MODULE_AUTHOR("Bryan Wy <cooloney@kernel.org>");
MODULE_LICENSE("GPL v2");
621
module_platform_driver(bfin_driver);