m66592-udc.c 42.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * M66592 UDC (USB gadget)
 *
 * Copyright (C) 2006-2007 Renesas Solutions Corp.
 *
 * Author : Yoshihiro Shimoda <shimoda.yoshihiro@renesas.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#include <linux/module.h>
#include <linux/interrupt.h>
25 26
#include <linux/delay.h>
#include <linux/io.h>
27
#include <linux/platform_device.h>
28
#include <linux/slab.h>
29
#include <linux/err.h>
30
#include <linux/usb/ch9.h>
31
#include <linux/usb/gadget.h>
32 33 34

#include "m66592-udc.h"

35
MODULE_DESCRIPTION("M66592 USB gadget driver");
36 37
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Yoshihiro Shimoda");
38
MODULE_ALIAS("platform:m66592_udc");
39

40
#define DRIVER_VERSION	"21 July 2009"
41 42 43 44 45 46 47 48 49 50 51 52 53

static const char udc_name[] = "m66592_udc";
static const char *m66592_ep_name[] = {
	"ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7"
};

static void disable_controller(struct m66592 *m66592);
static void irq_ep0_write(struct m66592_ep *ep, struct m66592_request *req);
static void irq_packet_write(struct m66592_ep *ep, struct m66592_request *req);
static int m66592_queue(struct usb_ep *_ep, struct usb_request *_req,
			gfp_t gfp_flags);

static void transfer_complete(struct m66592_ep *ep,
54 55
		struct m66592_request *req, int status);

56 57 58 59 60 61 62
/*-------------------------------------------------------------------------*/
static inline u16 get_usb_speed(struct m66592 *m66592)
{
	return (m66592_read(m66592, M66592_DVSTCTR) & M66592_RHST);
}

static void enable_pipe_irq(struct m66592 *m66592, u16 pipenum,
63
		unsigned long reg)
64 65 66 67 68
{
	u16 tmp;

	tmp = m66592_read(m66592, M66592_INTENB0);
	m66592_bclr(m66592, M66592_BEMPE | M66592_NRDYE | M66592_BRDYE,
69
			M66592_INTENB0);
70 71 72 73 74
	m66592_bset(m66592, (1 << pipenum), reg);
	m66592_write(m66592, tmp, M66592_INTENB0);
}

static void disable_pipe_irq(struct m66592 *m66592, u16 pipenum,
75
		unsigned long reg)
76 77 78 79 80
{
	u16 tmp;

	tmp = m66592_read(m66592, M66592_INTENB0);
	m66592_bclr(m66592, M66592_BEMPE | M66592_NRDYE | M66592_BRDYE,
81
			M66592_INTENB0);
82 83 84 85 86 87 88 89
	m66592_bclr(m66592, (1 << pipenum), reg);
	m66592_write(m66592, tmp, M66592_INTENB0);
}

static void m66592_usb_connect(struct m66592 *m66592)
{
	m66592_bset(m66592, M66592_CTRE, M66592_INTENB0);
	m66592_bset(m66592, M66592_WDST | M66592_RDST | M66592_CMPL,
90
			M66592_INTENB0);
91 92 93 94 95 96
	m66592_bset(m66592, M66592_BEMPE | M66592_BRDYE, M66592_INTENB0);

	m66592_bset(m66592, M66592_DPRPU, M66592_SYSCFG);
}

static void m66592_usb_disconnect(struct m66592 *m66592)
97 98
__releases(m66592->lock)
__acquires(m66592->lock)
99 100 101
{
	m66592_bclr(m66592, M66592_CTRE, M66592_INTENB0);
	m66592_bclr(m66592, M66592_WDST | M66592_RDST | M66592_CMPL,
102
			M66592_INTENB0);
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
	m66592_bclr(m66592, M66592_BEMPE | M66592_BRDYE, M66592_INTENB0);
	m66592_bclr(m66592, M66592_DPRPU, M66592_SYSCFG);

	m66592->gadget.speed = USB_SPEED_UNKNOWN;
	spin_unlock(&m66592->lock);
	m66592->driver->disconnect(&m66592->gadget);
	spin_lock(&m66592->lock);

	disable_controller(m66592);
	INIT_LIST_HEAD(&m66592->ep[0].queue);
}

static inline u16 control_reg_get_pid(struct m66592 *m66592, u16 pipenum)
{
	u16 pid = 0;
	unsigned long offset;

	if (pipenum == 0)
		pid = m66592_read(m66592, M66592_DCPCTR) & M66592_PID;
	else if (pipenum < M66592_MAX_NUM_PIPE) {
		offset = get_pipectr_addr(pipenum);
		pid = m66592_read(m66592, offset) & M66592_PID;
	} else
126
		pr_err("unexpect pipe num (%d)\n", pipenum);
127 128 129 130 131

	return pid;
}

static inline void control_reg_set_pid(struct m66592 *m66592, u16 pipenum,
132
		u16 pid)
133 134 135 136 137 138 139 140 141
{
	unsigned long offset;

	if (pipenum == 0)
		m66592_mdfy(m66592, pid, M66592_PID, M66592_DCPCTR);
	else if (pipenum < M66592_MAX_NUM_PIPE) {
		offset = get_pipectr_addr(pipenum);
		m66592_mdfy(m66592, pid, M66592_PID, offset);
	} else
142
		pr_err("unexpect pipe num (%d)\n", pipenum);
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170
}

static inline void pipe_start(struct m66592 *m66592, u16 pipenum)
{
	control_reg_set_pid(m66592, pipenum, M66592_PID_BUF);
}

static inline void pipe_stop(struct m66592 *m66592, u16 pipenum)
{
	control_reg_set_pid(m66592, pipenum, M66592_PID_NAK);
}

static inline void pipe_stall(struct m66592 *m66592, u16 pipenum)
{
	control_reg_set_pid(m66592, pipenum, M66592_PID_STALL);
}

static inline u16 control_reg_get(struct m66592 *m66592, u16 pipenum)
{
	u16 ret = 0;
	unsigned long offset;

	if (pipenum == 0)
		ret = m66592_read(m66592, M66592_DCPCTR);
	else if (pipenum < M66592_MAX_NUM_PIPE) {
		offset = get_pipectr_addr(pipenum);
		ret = m66592_read(m66592, offset);
	} else
171
		pr_err("unexpect pipe num (%d)\n", pipenum);
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187

	return ret;
}

static inline void control_reg_sqclr(struct m66592 *m66592, u16 pipenum)
{
	unsigned long offset;

	pipe_stop(m66592, pipenum);

	if (pipenum == 0)
		m66592_bset(m66592, M66592_SQCLR, M66592_DCPCTR);
	else if (pipenum < M66592_MAX_NUM_PIPE) {
		offset = get_pipectr_addr(pipenum);
		m66592_bset(m66592, M66592_SQCLR, offset);
	} else
188
		pr_err("unexpect pipe num(%d)\n", pipenum);
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
}

static inline int get_buffer_size(struct m66592 *m66592, u16 pipenum)
{
	u16 tmp;
	int size;

	if (pipenum == 0) {
		tmp = m66592_read(m66592, M66592_DCPCFG);
		if ((tmp & M66592_CNTMD) != 0)
			size = 256;
		else {
			tmp = m66592_read(m66592, M66592_DCPMAXP);
			size = tmp & M66592_MAXP;
		}
	} else {
		m66592_write(m66592, pipenum, M66592_PIPESEL);
		tmp = m66592_read(m66592, M66592_PIPECFG);
		if ((tmp & M66592_CNTMD) != 0) {
			tmp = m66592_read(m66592, M66592_PIPEBUF);
			size = ((tmp >> 10) + 1) * 64;
		} else {
			tmp = m66592_read(m66592, M66592_PIPEMAXP);
			size = tmp & M66592_MXPS;
		}
	}

	return size;
}

static inline void pipe_change(struct m66592 *m66592, u16 pipenum)
{
	struct m66592_ep *ep = m66592->pipenum2ep[pipenum];
222
	unsigned short mbw;
223 224 225 226 227 228 229 230

	if (ep->use_dma)
		return;

	m66592_mdfy(m66592, pipenum, M66592_CURPIPE, ep->fifosel);

	ndelay(450);

231 232 233 234 235 236
	if (m66592->pdata->on_chip)
		mbw = M66592_MBW_32;
	else
		mbw = M66592_MBW_16;

	m66592_bset(m66592, mbw, ep->fifosel);
237 238 239
}

static int pipe_buffer_setting(struct m66592 *m66592,
240
		struct m66592_pipe_info *info)
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
	u16 bufnum = 0, buf_bsize = 0;
	u16 pipecfg = 0;

	if (info->pipe == 0)
		return -EINVAL;

	m66592_write(m66592, info->pipe, M66592_PIPESEL);

	if (info->dir_in)
		pipecfg |= M66592_DIR;
	pipecfg |= info->type;
	pipecfg |= info->epnum;
	switch (info->type) {
	case M66592_INT:
		bufnum = 4 + (info->pipe - M66592_BASE_PIPENUM_INT);
		buf_bsize = 0;
		break;
	case M66592_BULK:
260 261 262 263 264 265 266
		/* isochronous pipes may be used as bulk pipes */
		if (info->pipe > M66592_BASE_PIPENUM_BULK)
			bufnum = info->pipe - M66592_BASE_PIPENUM_BULK;
		else
			bufnum = info->pipe - M66592_BASE_PIPENUM_ISOC;

		bufnum = M66592_BASE_BUFNUM + (bufnum * 16);
267 268 269 270 271 272
		buf_bsize = 7;
		pipecfg |= M66592_DBLB;
		if (!info->dir_in)
			pipecfg |= M66592_SHTNAK;
		break;
	case M66592_ISO:
273
		bufnum = M66592_BASE_BUFNUM +
274 275 276 277
			 (info->pipe - M66592_BASE_PIPENUM_ISOC) * 16;
		buf_bsize = 7;
		break;
	}
278 279 280

	if (buf_bsize && ((bufnum + 16) >= M66592_MAX_BUFNUM)) {
		pr_err("m66592 pipe memory is insufficient\n");
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
		return -ENOMEM;
	}

	m66592_write(m66592, pipecfg, M66592_PIPECFG);
	m66592_write(m66592, (buf_bsize << 10) | (bufnum), M66592_PIPEBUF);
	m66592_write(m66592, info->maxpacket, M66592_PIPEMAXP);
	if (info->interval)
		info->interval--;
	m66592_write(m66592, info->interval, M66592_PIPEPERI);

	return 0;
}

static void pipe_buffer_release(struct m66592 *m66592,
				struct m66592_pipe_info *info)
{
	if (info->pipe == 0)
		return;

	if (is_bulk_pipe(info->pipe)) {
		m66592->bulk--;
	} else if (is_interrupt_pipe(info->pipe))
		m66592->interrupt--;
	else if (is_isoc_pipe(info->pipe)) {
		m66592->isochronous--;
		if (info->type == M66592_BULK)
			m66592->bulk--;
	} else
309
		pr_err("ep_release: unexpect pipenum (%d)\n",
310
				info->pipe);
311 312 313 314 315
}

static void pipe_initialize(struct m66592_ep *ep)
{
	struct m66592 *m66592 = ep->m66592;
316
	unsigned short mbw;
317 318 319 320 321 322 323 324 325 326 327

	m66592_mdfy(m66592, 0, M66592_CURPIPE, ep->fifosel);

	m66592_write(m66592, M66592_ACLRM, ep->pipectr);
	m66592_write(m66592, 0, ep->pipectr);
	m66592_write(m66592, M66592_SQCLR, ep->pipectr);
	if (ep->use_dma) {
		m66592_mdfy(m66592, ep->pipenum, M66592_CURPIPE, ep->fifosel);

		ndelay(450);

328 329 330 331 332 333
		if (m66592->pdata->on_chip)
			mbw = M66592_MBW_32;
		else
			mbw = M66592_MBW_16;

		m66592_bset(m66592, mbw, ep->fifosel);
334 335 336 337
	}
}

static void m66592_ep_setting(struct m66592 *m66592, struct m66592_ep *ep,
338 339
		const struct usb_endpoint_descriptor *desc,
		u16 pipenum, int dma)
340 341 342 343 344 345 346 347 348
{
	if ((pipenum != 0) && dma) {
		if (m66592->num_dma == 0) {
			m66592->num_dma++;
			ep->use_dma = 1;
			ep->fifoaddr = M66592_D0FIFO;
			ep->fifosel = M66592_D0FIFOSEL;
			ep->fifoctr = M66592_D0FIFOCTR;
			ep->fifotrn = M66592_D0FIFOTRN;
349
		} else if (!m66592->pdata->on_chip && m66592->num_dma == 1) {
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
			m66592->num_dma++;
			ep->use_dma = 1;
			ep->fifoaddr = M66592_D1FIFO;
			ep->fifosel = M66592_D1FIFOSEL;
			ep->fifoctr = M66592_D1FIFOCTR;
			ep->fifotrn = M66592_D1FIFOTRN;
		} else {
			ep->use_dma = 0;
			ep->fifoaddr = M66592_CFIFO;
			ep->fifosel = M66592_CFIFOSEL;
			ep->fifoctr = M66592_CFIFOCTR;
			ep->fifotrn = 0;
		}
	} else {
		ep->use_dma = 0;
		ep->fifoaddr = M66592_CFIFO;
		ep->fifosel = M66592_CFIFOSEL;
		ep->fifoctr = M66592_CFIFOCTR;
		ep->fifotrn = 0;
	}

	ep->pipectr = get_pipectr_addr(pipenum);
	ep->pipenum = pipenum;
373
	ep->ep.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
	m66592->pipenum2ep[pipenum] = ep;
	m66592->epaddr2ep[desc->bEndpointAddress&USB_ENDPOINT_NUMBER_MASK] = ep;
	INIT_LIST_HEAD(&ep->queue);
}

static void m66592_ep_release(struct m66592_ep *ep)
{
	struct m66592 *m66592 = ep->m66592;
	u16 pipenum = ep->pipenum;

	if (pipenum == 0)
		return;

	if (ep->use_dma)
		m66592->num_dma--;
	ep->pipenum = 0;
	ep->busy = 0;
	ep->use_dma = 0;
}

static int alloc_pipe_config(struct m66592_ep *ep,
395
		const struct usb_endpoint_descriptor *desc)
396 397 398 399 400 401 402 403 404 405 406
{
	struct m66592 *m66592 = ep->m66592;
	struct m66592_pipe_info info;
	int dma = 0;
	int *counter;
	int ret;

	ep->desc = desc;

	BUG_ON(ep->pipenum);

407
	switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
408 409 410
	case USB_ENDPOINT_XFER_BULK:
		if (m66592->bulk >= M66592_MAX_NUM_BULK) {
			if (m66592->isochronous >= M66592_MAX_NUM_ISOC) {
411
				pr_err("bulk pipe is insufficient\n");
412 413
				return -ENODEV;
			} else {
414 415
				info.pipe = M66592_BASE_PIPENUM_ISOC
						+ m66592->isochronous;
416 417 418 419 420 421 422 423 424 425 426
				counter = &m66592->isochronous;
			}
		} else {
			info.pipe = M66592_BASE_PIPENUM_BULK + m66592->bulk;
			counter = &m66592->bulk;
		}
		info.type = M66592_BULK;
		dma = 1;
		break;
	case USB_ENDPOINT_XFER_INT:
		if (m66592->interrupt >= M66592_MAX_NUM_INT) {
427
			pr_err("interrupt pipe is insufficient\n");
428 429 430 431 432 433 434 435
			return -ENODEV;
		}
		info.pipe = M66592_BASE_PIPENUM_INT + m66592->interrupt;
		info.type = M66592_INT;
		counter = &m66592->interrupt;
		break;
	case USB_ENDPOINT_XFER_ISOC:
		if (m66592->isochronous >= M66592_MAX_NUM_ISOC) {
436
			pr_err("isochronous pipe is insufficient\n");
437 438 439 440 441 442 443
			return -ENODEV;
		}
		info.pipe = M66592_BASE_PIPENUM_ISOC + m66592->isochronous;
		info.type = M66592_ISO;
		counter = &m66592->isochronous;
		break;
	default:
444
		pr_err("unexpect xfer type\n");
445 446 447 448 449
		return -EINVAL;
	}
	ep->type = info.type;

	info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
450
	info.maxpacket = le16_to_cpu(desc->wMaxPacketSize);
451 452 453 454 455 456 457 458
	info.interval = desc->bInterval;
	if (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK)
		info.dir_in = 1;
	else
		info.dir_in = 0;

	ret = pipe_buffer_setting(m66592, &info);
	if (ret < 0) {
459
		pr_err("pipe_buffer_setting fail\n");
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
		return ret;
	}

	(*counter)++;
	if ((counter == &m66592->isochronous) && info.type == M66592_BULK)
		m66592->bulk++;

	m66592_ep_setting(m66592, ep, desc, info.pipe, dma);
	pipe_initialize(ep);

	return 0;
}

static int free_pipe_config(struct m66592_ep *ep)
{
	struct m66592 *m66592 = ep->m66592;
	struct m66592_pipe_info info;

	info.pipe = ep->pipenum;
	info.type = ep->type;
	pipe_buffer_release(m66592, &info);
	m66592_ep_release(ep);

	return 0;
}

/*-------------------------------------------------------------------------*/
static void pipe_irq_enable(struct m66592 *m66592, u16 pipenum)
{
	enable_irq_ready(m66592, pipenum);
	enable_irq_nrdy(m66592, pipenum);
}

static void pipe_irq_disable(struct m66592 *m66592, u16 pipenum)
{
	disable_irq_ready(m66592, pipenum);
	disable_irq_nrdy(m66592, pipenum);
}

/* if complete is true, gadget driver complete function is not call */
static void control_end(struct m66592 *m66592, unsigned ccpl)
{
	m66592->ep[0].internal_ccpl = ccpl;
	pipe_start(m66592, 0);
	m66592_bset(m66592, M66592_CCPL, M66592_DCPCTR);
}

static void start_ep0_write(struct m66592_ep *ep, struct m66592_request *req)
{
	struct m66592 *m66592 = ep->m66592;

	pipe_change(m66592, ep->pipenum);
	m66592_mdfy(m66592, M66592_ISEL | M66592_PIPE0,
513 514
			(M66592_ISEL | M66592_CURPIPE),
			M66592_CFIFOSEL);
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 542 543 544 545 546 547 548
	m66592_write(m66592, M66592_BCLR, ep->fifoctr);
	if (req->req.length == 0) {
		m66592_bset(m66592, M66592_BVAL, ep->fifoctr);
		pipe_start(m66592, 0);
		transfer_complete(ep, req, 0);
	} else {
		m66592_write(m66592, ~M66592_BEMP0, M66592_BEMPSTS);
		irq_ep0_write(ep, req);
	}
}

static void start_packet_write(struct m66592_ep *ep, struct m66592_request *req)
{
	struct m66592 *m66592 = ep->m66592;
	u16 tmp;

	pipe_change(m66592, ep->pipenum);
	disable_irq_empty(m66592, ep->pipenum);
	pipe_start(m66592, ep->pipenum);

	tmp = m66592_read(m66592, ep->fifoctr);
	if (unlikely((tmp & M66592_FRDY) == 0))
		pipe_irq_enable(m66592, ep->pipenum);
	else
		irq_packet_write(ep, req);
}

static void start_packet_read(struct m66592_ep *ep, struct m66592_request *req)
{
	struct m66592 *m66592 = ep->m66592;
	u16 pipenum = ep->pipenum;

	if (ep->pipenum == 0) {
		m66592_mdfy(m66592, M66592_PIPE0,
549 550
				(M66592_ISEL | M66592_CURPIPE),
				M66592_CFIFOSEL);
551 552 553 554 555 556 557 558 559
		m66592_write(m66592, M66592_BCLR, ep->fifoctr);
		pipe_start(m66592, pipenum);
		pipe_irq_enable(m66592, pipenum);
	} else {
		if (ep->use_dma) {
			m66592_bset(m66592, M66592_TRCLR, ep->fifosel);
			pipe_change(m66592, pipenum);
			m66592_bset(m66592, M66592_TRENB, ep->fifosel);
			m66592_write(m66592,
560 561 562
				(req->req.length + ep->ep.maxpacket - 1)
					/ ep->ep.maxpacket,
				ep->fifotrn);
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 588 589 590 591 592 593 594
		}
		pipe_start(m66592, pipenum);	/* trigger once */
		pipe_irq_enable(m66592, pipenum);
	}
}

static void start_packet(struct m66592_ep *ep, struct m66592_request *req)
{
	if (ep->desc->bEndpointAddress & USB_DIR_IN)
		start_packet_write(ep, req);
	else
		start_packet_read(ep, req);
}

static void start_ep0(struct m66592_ep *ep, struct m66592_request *req)
{
	u16 ctsq;

	ctsq = m66592_read(ep->m66592, M66592_INTSTS0) & M66592_CTSQ;

	switch (ctsq) {
	case M66592_CS_RDDS:
		start_ep0_write(ep, req);
		break;
	case M66592_CS_WRDS:
		start_packet_read(ep, req);
		break;

	case M66592_CS_WRND:
		control_end(ep->m66592, 0);
		break;
	default:
595
		pr_err("start_ep0: unexpect ctsq(%x)\n", ctsq);
596 597 598 599
		break;
	}
}

600 601
static void init_controller(struct m66592 *m66592)
{
602
	unsigned int endian;
603

604 605 606 607 608
	if (m66592->pdata->on_chip) {
		if (m66592->pdata->endian)
			endian = 0; /* big endian */
		else
			endian = M66592_LITTLE; /* little endian */
609

610 611 612 613
		m66592_bset(m66592, M66592_HSE, M66592_SYSCFG);	/* High spd */
		m66592_bclr(m66592, M66592_USBE, M66592_SYSCFG);
		m66592_bclr(m66592, M66592_DPRPU, M66592_SYSCFG);
		m66592_bset(m66592, M66592_USBE, M66592_SYSCFG);
614

615 616 617 618
		/* This is a workaound for SH7722 2nd cut */
		m66592_bset(m66592, 0x8000, M66592_DVSTCTR);
		m66592_bset(m66592, 0x1000, M66592_TESTMODE);
		m66592_bclr(m66592, 0x8000, M66592_DVSTCTR);
619

620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652
		m66592_bset(m66592, M66592_INTL, M66592_INTENB1);

		m66592_write(m66592, 0, M66592_CFBCFG);
		m66592_write(m66592, 0, M66592_D0FBCFG);
		m66592_bset(m66592, endian, M66592_CFBCFG);
		m66592_bset(m66592, endian, M66592_D0FBCFG);
	} else {
		unsigned int clock, vif, irq_sense;

		if (m66592->pdata->endian)
			endian = M66592_BIGEND; /* big endian */
		else
			endian = 0; /* little endian */

		if (m66592->pdata->vif)
			vif = M66592_LDRV; /* 3.3v */
		else
			vif = 0; /* 1.5v */

		switch (m66592->pdata->xtal) {
		case M66592_PLATDATA_XTAL_12MHZ:
			clock = M66592_XTAL12;
			break;
		case M66592_PLATDATA_XTAL_24MHZ:
			clock = M66592_XTAL24;
			break;
		case M66592_PLATDATA_XTAL_48MHZ:
			clock = M66592_XTAL48;
			break;
		default:
			pr_warning("m66592-udc: xtal configuration error\n");
			clock = 0;
		}
653

654 655 656 657 658 659 660 661 662 663 664
		switch (m66592->irq_trigger) {
		case IRQF_TRIGGER_LOW:
			irq_sense = M66592_INTL;
			break;
		case IRQF_TRIGGER_FALLING:
			irq_sense = 0;
			break;
		default:
			pr_warning("m66592-udc: irq trigger config error\n");
			irq_sense = 0;
		}
665

666 667 668 669 670 671 672 673 674
		m66592_bset(m66592,
			    (vif & M66592_LDRV) | (endian & M66592_BIGEND),
			    M66592_PINCFG);
		m66592_bset(m66592, M66592_HSE, M66592_SYSCFG);	/* High spd */
		m66592_mdfy(m66592, clock & M66592_XTAL, M66592_XTAL,
			    M66592_SYSCFG);
		m66592_bclr(m66592, M66592_USBE, M66592_SYSCFG);
		m66592_bclr(m66592, M66592_DPRPU, M66592_SYSCFG);
		m66592_bset(m66592, M66592_USBE, M66592_SYSCFG);
675

676 677 678
		m66592_bset(m66592, M66592_XCKE, M66592_SYSCFG);

		msleep(3);
679

680
		m66592_bset(m66592, M66592_RCKE | M66592_PLLC, M66592_SYSCFG);
681

682
		msleep(1);
683

684 685 686 687 688 689
		m66592_bset(m66592, M66592_SCKE, M66592_SYSCFG);

		m66592_bset(m66592, irq_sense & M66592_INTL, M66592_INTENB1);
		m66592_write(m66592, M66592_BURST | M66592_CPU_ADR_RD_WR,
			     M66592_DMA0CFG);
	}
690 691 692 693
}

static void disable_controller(struct m66592 *m66592)
{
694 695 696 697 698 699 700 701 702
	if (!m66592->pdata->on_chip) {
		m66592_bclr(m66592, M66592_SCKE, M66592_SYSCFG);
		udelay(1);
		m66592_bclr(m66592, M66592_PLLC, M66592_SYSCFG);
		udelay(1);
		m66592_bclr(m66592, M66592_RCKE, M66592_SYSCFG);
		udelay(1);
		m66592_bclr(m66592, M66592_XCKE, M66592_SYSCFG);
	}
703 704 705 706 707 708
}

static void m66592_start_xclock(struct m66592 *m66592)
{
	u16 tmp;

709 710 711 712 713
	if (!m66592->pdata->on_chip) {
		tmp = m66592_read(m66592, M66592_SYSCFG);
		if (!(tmp & M66592_XCKE))
			m66592_bset(m66592, M66592_XCKE, M66592_SYSCFG);
	}
714 715 716 717
}

/*-------------------------------------------------------------------------*/
static void transfer_complete(struct m66592_ep *ep,
718 719 720
		struct m66592_request *req, int status)
__releases(m66592->lock)
__acquires(m66592->lock)
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739
{
	int restart = 0;

	if (unlikely(ep->pipenum == 0)) {
		if (ep->internal_ccpl) {
			ep->internal_ccpl = 0;
			return;
		}
	}

	list_del_init(&req->queue);
	if (ep->m66592->gadget.speed == USB_SPEED_UNKNOWN)
		req->req.status = -ESHUTDOWN;
	else
		req->req.status = status;

	if (!list_empty(&ep->queue))
		restart = 1;

740 741 742
	spin_unlock(&ep->m66592->lock);
	req->req.complete(&ep->ep, &req->req);
	spin_lock(&ep->m66592->lock);
743 744 745 746 747 748 749 750 751 752 753

	if (restart) {
		req = list_entry(ep->queue.next, struct m66592_request, queue);
		if (ep->desc)
			start_packet(ep, req);
	}
}

static void irq_ep0_write(struct m66592_ep *ep, struct m66592_request *req)
{
	int i;
754
	u16 tmp;
755 756 757 758 759 760 761 762 763 764 765 766 767
	unsigned bufsize;
	size_t size;
	void *buf;
	u16 pipenum = ep->pipenum;
	struct m66592 *m66592 = ep->m66592;

	pipe_change(m66592, pipenum);
	m66592_bset(m66592, M66592_ISEL, ep->fifosel);

	i = 0;
	do {
		tmp = m66592_read(m66592, ep->fifoctr);
		if (i++ > 100000) {
768
			pr_err("pipe0 is busy. maybe cpu i/o bus "
769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791
				"conflict. please power off this controller.");
			return;
		}
		ndelay(1);
	} while ((tmp & M66592_FRDY) == 0);

	/* prepare parameters */
	bufsize = get_buffer_size(m66592, pipenum);
	buf = req->req.buf + req->req.actual;
	size = min(bufsize, req->req.length - req->req.actual);

	/* write fifo */
	if (req->req.buf) {
		if (size > 0)
			m66592_write_fifo(m66592, ep->fifoaddr, buf, size);
		if ((size == 0) || ((size % ep->ep.maxpacket) != 0))
			m66592_bset(m66592, M66592_BVAL, ep->fifoctr);
	}

	/* update parameters */
	req->req.actual += size;

	/* check transfer finish */
792 793 794
	if ((!req->req.zero && (req->req.actual == req->req.length))
			|| (size % ep->ep.maxpacket)
			|| (size == 0)) {
795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
		disable_irq_ready(m66592, pipenum);
		disable_irq_empty(m66592, pipenum);
	} else {
		disable_irq_ready(m66592, pipenum);
		enable_irq_empty(m66592, pipenum);
	}
	pipe_start(m66592, pipenum);
}

static void irq_packet_write(struct m66592_ep *ep, struct m66592_request *req)
{
	u16 tmp;
	unsigned bufsize;
	size_t size;
	void *buf;
	u16 pipenum = ep->pipenum;
	struct m66592 *m66592 = ep->m66592;

	pipe_change(m66592, pipenum);
	tmp = m66592_read(m66592, ep->fifoctr);
	if (unlikely((tmp & M66592_FRDY) == 0)) {
		pipe_stop(m66592, pipenum);
		pipe_irq_disable(m66592, pipenum);
818
		pr_err("write fifo not ready. pipnum=%d\n", pipenum);
819 820 821 822 823 824 825 826 827 828 829
		return;
	}

	/* prepare parameters */
	bufsize = get_buffer_size(m66592, pipenum);
	buf = req->req.buf + req->req.actual;
	size = min(bufsize, req->req.length - req->req.actual);

	/* write fifo */
	if (req->req.buf) {
		m66592_write_fifo(m66592, ep->fifoaddr, buf, size);
830 831 832 833
		if ((size == 0)
				|| ((size % ep->ep.maxpacket) != 0)
				|| ((bufsize != ep->ep.maxpacket)
					&& (bufsize > size)))
834 835 836 837 838 839
			m66592_bset(m66592, M66592_BVAL, ep->fifoctr);
	}

	/* update parameters */
	req->req.actual += size;
	/* check transfer finish */
840 841 842
	if ((!req->req.zero && (req->req.actual == req->req.length))
			|| (size % ep->ep.maxpacket)
			|| (size == 0)) {
843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
		disable_irq_ready(m66592, pipenum);
		enable_irq_empty(m66592, pipenum);
	} else {
		disable_irq_empty(m66592, pipenum);
		pipe_irq_enable(m66592, pipenum);
	}
}

static void irq_packet_read(struct m66592_ep *ep, struct m66592_request *req)
{
	u16 tmp;
	int rcv_len, bufsize, req_len;
	int size;
	void *buf;
	u16 pipenum = ep->pipenum;
	struct m66592 *m66592 = ep->m66592;
	int finish = 0;

	pipe_change(m66592, pipenum);
	tmp = m66592_read(m66592, ep->fifoctr);
	if (unlikely((tmp & M66592_FRDY) == 0)) {
		req->req.status = -EPIPE;
		pipe_stop(m66592, pipenum);
		pipe_irq_disable(m66592, pipenum);
867
		pr_err("read fifo not ready");
868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885
		return;
	}

	/* prepare parameters */
	rcv_len = tmp & M66592_DTLN;
	bufsize = get_buffer_size(m66592, pipenum);

	buf = req->req.buf + req->req.actual;
	req_len = req->req.length - req->req.actual;
	if (rcv_len < bufsize)
		size = min(rcv_len, req_len);
	else
		size = min(bufsize, req_len);

	/* update parameters */
	req->req.actual += size;

	/* check transfer finish */
886 887 888
	if ((!req->req.zero && (req->req.actual == req->req.length))
			|| (size % ep->ep.maxpacket)
			|| (size == 0)) {
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
		pipe_stop(m66592, pipenum);
		pipe_irq_disable(m66592, pipenum);
		finish = 1;
	}

	/* read fifo */
	if (req->req.buf) {
		if (size == 0)
			m66592_write(m66592, M66592_BCLR, ep->fifoctr);
		else
			m66592_read_fifo(m66592, ep->fifoaddr, buf, size);
	}

	if ((ep->pipenum != 0) && finish)
		transfer_complete(ep, req, 0);
}

static void irq_pipe_ready(struct m66592 *m66592, u16 status, u16 enb)
{
	u16 check;
	u16 pipenum;
	struct m66592_ep *ep;
	struct m66592_request *req;

	if ((status & M66592_BRDY0) && (enb & M66592_BRDY0)) {
		m66592_write(m66592, ~M66592_BRDY0, M66592_BRDYSTS);
		m66592_mdfy(m66592, M66592_PIPE0, M66592_CURPIPE,
916
				M66592_CFIFOSEL);
917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974

		ep = &m66592->ep[0];
		req = list_entry(ep->queue.next, struct m66592_request, queue);
		irq_packet_read(ep, req);
	} else {
		for (pipenum = 1; pipenum < M66592_MAX_NUM_PIPE; pipenum++) {
			check = 1 << pipenum;
			if ((status & check) && (enb & check)) {
				m66592_write(m66592, ~check, M66592_BRDYSTS);
				ep = m66592->pipenum2ep[pipenum];
				req = list_entry(ep->queue.next,
						 struct m66592_request, queue);
				if (ep->desc->bEndpointAddress & USB_DIR_IN)
					irq_packet_write(ep, req);
				else
					irq_packet_read(ep, req);
			}
		}
	}
}

static void irq_pipe_empty(struct m66592 *m66592, u16 status, u16 enb)
{
	u16 tmp;
	u16 check;
	u16 pipenum;
	struct m66592_ep *ep;
	struct m66592_request *req;

	if ((status & M66592_BEMP0) && (enb & M66592_BEMP0)) {
		m66592_write(m66592, ~M66592_BEMP0, M66592_BEMPSTS);

		ep = &m66592->ep[0];
		req = list_entry(ep->queue.next, struct m66592_request, queue);
		irq_ep0_write(ep, req);
	} else {
		for (pipenum = 1; pipenum < M66592_MAX_NUM_PIPE; pipenum++) {
			check = 1 << pipenum;
			if ((status & check) && (enb & check)) {
				m66592_write(m66592, ~check, M66592_BEMPSTS);
				tmp = control_reg_get(m66592, pipenum);
				if ((tmp & M66592_INBUFM) == 0) {
					disable_irq_empty(m66592, pipenum);
					pipe_irq_disable(m66592, pipenum);
					pipe_stop(m66592, pipenum);
					ep = m66592->pipenum2ep[pipenum];
					req = list_entry(ep->queue.next,
							 struct m66592_request,
							 queue);
					if (!list_empty(&ep->queue))
						transfer_complete(ep, req, 0);
				}
			}
		}
	}
}

static void get_status(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
975 976
__releases(m66592->lock)
__acquires(m66592->lock)
977 978 979 980
{
	struct m66592_ep *ep;
	u16 pid;
	u16 status = 0;
981
	u16 w_index = le16_to_cpu(ctrl->wIndex);
982 983 984

	switch (ctrl->bRequestType & USB_RECIP_MASK) {
	case USB_RECIP_DEVICE:
985
		status = 1 << USB_DEVICE_SELF_POWERED;
986 987 988 989 990
		break;
	case USB_RECIP_INTERFACE:
		status = 0;
		break;
	case USB_RECIP_ENDPOINT:
991
		ep = m66592->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
992 993
		pid = control_reg_get_pid(m66592, ep->pipenum);
		if (pid == M66592_PID_STALL)
994
			status = 1 << USB_ENDPOINT_HALT;
995 996 997 998 999 1000 1001 1002
		else
			status = 0;
		break;
	default:
		pipe_stall(m66592, 0);
		return;		/* exit */
	}

1003 1004
	m66592->ep0_data = cpu_to_le16(status);
	m66592->ep0_req->buf = &m66592->ep0_data;
1005
	m66592->ep0_req->length = 2;
1006
	/* AV: what happens if we get called again before that gets through? */
1007
	spin_unlock(&m66592->lock);
1008
	m66592_queue(m66592->gadget.ep0, m66592->ep0_req, GFP_KERNEL);
1009
	spin_lock(&m66592->lock);
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
}

static void clear_feature(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
{
	switch (ctrl->bRequestType & USB_RECIP_MASK) {
	case USB_RECIP_DEVICE:
		control_end(m66592, 1);
		break;
	case USB_RECIP_INTERFACE:
		control_end(m66592, 1);
		break;
	case USB_RECIP_ENDPOINT: {
		struct m66592_ep *ep;
		struct m66592_request *req;
1024
		u16 w_index = le16_to_cpu(ctrl->wIndex);
1025

1026
		ep = m66592->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060
		pipe_stop(m66592, ep->pipenum);
		control_reg_sqclr(m66592, ep->pipenum);

		control_end(m66592, 1);

		req = list_entry(ep->queue.next,
		struct m66592_request, queue);
		if (ep->busy) {
			ep->busy = 0;
			if (list_empty(&ep->queue))
				break;
			start_packet(ep, req);
		} else if (!list_empty(&ep->queue))
			pipe_start(m66592, ep->pipenum);
		}
		break;
	default:
		pipe_stall(m66592, 0);
		break;
	}
}

static void set_feature(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
{

	switch (ctrl->bRequestType & USB_RECIP_MASK) {
	case USB_RECIP_DEVICE:
		control_end(m66592, 1);
		break;
	case USB_RECIP_INTERFACE:
		control_end(m66592, 1);
		break;
	case USB_RECIP_ENDPOINT: {
		struct m66592_ep *ep;
1061
		u16 w_index = le16_to_cpu(ctrl->wIndex);
1062

1063
		ep = m66592->epaddr2ep[w_index & USB_ENDPOINT_NUMBER_MASK];
1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
		pipe_stall(m66592, ep->pipenum);

		control_end(m66592, 1);
		}
		break;
	default:
		pipe_stall(m66592, 0);
		break;
	}
}

/* if return value is true, call class driver's setup() */
static int setup_packet(struct m66592 *m66592, struct usb_ctrlrequest *ctrl)
{
	u16 *p = (u16 *)ctrl;
	unsigned long offset = M66592_USBREQ;
	int i, ret = 0;

	/* read fifo */
	m66592_write(m66592, ~M66592_VALID, M66592_INTSTS0);

	for (i = 0; i < 4; i++)
		p[i] = m66592_read(m66592, offset + i*2);

	/* check request */
	if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
		switch (ctrl->bRequest) {
		case USB_REQ_GET_STATUS:
			get_status(m66592, ctrl);
			break;
		case USB_REQ_CLEAR_FEATURE:
			clear_feature(m66592, ctrl);
			break;
		case USB_REQ_SET_FEATURE:
			set_feature(m66592, ctrl);
			break;
		default:
			ret = 1;
			break;
		}
	} else
		ret = 1;
	return ret;
}

static void m66592_update_usb_speed(struct m66592 *m66592)
{
	u16 speed = get_usb_speed(m66592);

	switch (speed) {
	case M66592_HSMODE:
		m66592->gadget.speed = USB_SPEED_HIGH;
		break;
	case M66592_FSMODE:
		m66592->gadget.speed = USB_SPEED_FULL;
		break;
	default:
		m66592->gadget.speed = USB_SPEED_UNKNOWN;
1122
		pr_err("USB speed unknown\n");
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138
	}
}

static void irq_device_state(struct m66592 *m66592)
{
	u16 dvsq;

	dvsq = m66592_read(m66592, M66592_INTSTS0) & M66592_DVSQ;
	m66592_write(m66592, ~M66592_DVST, M66592_INTSTS0);

	if (dvsq == M66592_DS_DFLT) {	/* bus reset */
		m66592->driver->disconnect(&m66592->gadget);
		m66592_update_usb_speed(m66592);
	}
	if (m66592->old_dvsq == M66592_DS_CNFG && dvsq != M66592_DS_CNFG)
		m66592_update_usb_speed(m66592);
1139 1140
	if ((dvsq == M66592_DS_CNFG || dvsq == M66592_DS_ADDS)
			&& m66592->gadget.speed == USB_SPEED_UNKNOWN)
1141 1142 1143 1144 1145 1146
		m66592_update_usb_speed(m66592);

	m66592->old_dvsq = dvsq;
}

static void irq_control_stage(struct m66592 *m66592)
1147 1148
__releases(m66592->lock)
__acquires(m66592->lock)
1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169
{
	struct usb_ctrlrequest ctrl;
	u16 ctsq;

	ctsq = m66592_read(m66592, M66592_INTSTS0) & M66592_CTSQ;
	m66592_write(m66592, ~M66592_CTRT, M66592_INTSTS0);

	switch (ctsq) {
	case M66592_CS_IDST: {
		struct m66592_ep *ep;
		struct m66592_request *req;
		ep = &m66592->ep[0];
		req = list_entry(ep->queue.next, struct m66592_request, queue);
		transfer_complete(ep, req, 0);
		}
		break;

	case M66592_CS_RDDS:
	case M66592_CS_WRDS:
	case M66592_CS_WRND:
		if (setup_packet(m66592, &ctrl)) {
1170
			spin_unlock(&m66592->lock);
1171 1172
			if (m66592->driver->setup(&m66592->gadget, &ctrl) < 0)
				pipe_stall(m66592, 0);
1173
			spin_lock(&m66592->lock);
1174 1175 1176 1177 1178 1179 1180
		}
		break;
	case M66592_CS_RDSS:
	case M66592_CS_WRSS:
		control_end(m66592, 0);
		break;
	default:
1181
		pr_err("ctrl_stage: unexpect ctsq(%x)\n", ctsq);
1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195
		break;
	}
}

static irqreturn_t m66592_irq(int irq, void *_m66592)
{
	struct m66592 *m66592 = _m66592;
	u16 intsts0;
	u16 intenb0;
	u16 brdysts, nrdysts, bempsts;
	u16 brdyenb, nrdyenb, bempenb;
	u16 savepipe;
	u16 mask0;

1196 1197
	spin_lock(&m66592->lock);

1198 1199 1200
	intsts0 = m66592_read(m66592, M66592_INTSTS0);
	intenb0 = m66592_read(m66592, M66592_INTENB0);

1201
	if (m66592->pdata->on_chip && !intsts0 && !intenb0) {
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211
		/*
		 * When USB clock stops, it cannot read register. Even if a
		 * clock stops, the interrupt occurs. So this driver turn on
		 * a clock by this timing and do re-reading of register.
		 */
		m66592_start_xclock(m66592);
		intsts0 = m66592_read(m66592, M66592_INTSTS0);
		intenb0 = m66592_read(m66592, M66592_INTENB0);
	}

1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223
	savepipe = m66592_read(m66592, M66592_CFIFOSEL);

	mask0 = intsts0 & intenb0;
	if (mask0) {
		brdysts = m66592_read(m66592, M66592_BRDYSTS);
		nrdysts = m66592_read(m66592, M66592_NRDYSTS);
		bempsts = m66592_read(m66592, M66592_BEMPSTS);
		brdyenb = m66592_read(m66592, M66592_BRDYENB);
		nrdyenb = m66592_read(m66592, M66592_NRDYENB);
		bempenb = m66592_read(m66592, M66592_BEMPENB);

		if (mask0 & M66592_VBINT) {
1224 1225
			m66592_write(m66592,  0xffff & ~M66592_VBINT,
					M66592_INTSTS0);
1226 1227 1228 1229
			m66592_start_xclock(m66592);

			/* start vbus sampling */
			m66592->old_vbus = m66592_read(m66592, M66592_INTSTS0)
1230
					& M66592_VBSTS;
1231 1232 1233
			m66592->scount = M66592_MAX_SAMPLING;

			mod_timer(&m66592->timer,
1234
					jiffies + msecs_to_jiffies(50));
1235 1236 1237 1238
		}
		if (intsts0 & M66592_DVSQ)
			irq_device_state(m66592);

1239 1240
		if ((intsts0 & M66592_BRDY) && (intenb0 & M66592_BRDYE)
				&& (brdysts & brdyenb)) {
1241 1242
			irq_pipe_ready(m66592, brdysts, brdyenb);
		}
1243 1244
		if ((intsts0 & M66592_BEMP) && (intenb0 & M66592_BEMPE)
				&& (bempsts & bempenb)) {
1245 1246 1247 1248 1249 1250 1251 1252 1253
			irq_pipe_empty(m66592, bempsts, bempenb);
		}

		if (intsts0 & M66592_CTRT)
			irq_control_stage(m66592);
	}

	m66592_write(m66592, savepipe, M66592_CFIFOSEL);

1254
	spin_unlock(&m66592->lock);
1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
	return IRQ_HANDLED;
}

static void m66592_timer(unsigned long _m66592)
{
	struct m66592 *m66592 = (struct m66592 *)_m66592;
	unsigned long flags;
	u16 tmp;

	spin_lock_irqsave(&m66592->lock, flags);
	tmp = m66592_read(m66592, M66592_SYSCFG);
	if (!(tmp & M66592_RCKE)) {
		m66592_bset(m66592, M66592_RCKE | M66592_PLLC, M66592_SYSCFG);
		udelay(10);
		m66592_bset(m66592, M66592_SCKE, M66592_SYSCFG);
	}
	if (m66592->scount > 0) {
		tmp = m66592_read(m66592, M66592_INTSTS0) & M66592_VBSTS;
		if (tmp == m66592->old_vbus) {
			m66592->scount--;
			if (m66592->scount == 0) {
				if (tmp == M66592_VBSTS)
					m66592_usb_connect(m66592);
				else
					m66592_usb_disconnect(m66592);
			} else {
				mod_timer(&m66592->timer,
1282
					jiffies + msecs_to_jiffies(50));
1283 1284 1285 1286 1287
			}
		} else {
			m66592->scount = M66592_MAX_SAMPLING;
			m66592->old_vbus = tmp;
			mod_timer(&m66592->timer,
1288
					jiffies + msecs_to_jiffies(50));
1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368
		}
	}
	spin_unlock_irqrestore(&m66592->lock, flags);
}

/*-------------------------------------------------------------------------*/
static int m66592_enable(struct usb_ep *_ep,
			 const struct usb_endpoint_descriptor *desc)
{
	struct m66592_ep *ep;

	ep = container_of(_ep, struct m66592_ep, ep);
	return alloc_pipe_config(ep, desc);
}

static int m66592_disable(struct usb_ep *_ep)
{
	struct m66592_ep *ep;
	struct m66592_request *req;
	unsigned long flags;

	ep = container_of(_ep, struct m66592_ep, ep);
	BUG_ON(!ep);

	while (!list_empty(&ep->queue)) {
		req = list_entry(ep->queue.next, struct m66592_request, queue);
		spin_lock_irqsave(&ep->m66592->lock, flags);
		transfer_complete(ep, req, -ECONNRESET);
		spin_unlock_irqrestore(&ep->m66592->lock, flags);
	}

	pipe_irq_disable(ep->m66592, ep->pipenum);
	return free_pipe_config(ep);
}

static struct usb_request *m66592_alloc_request(struct usb_ep *_ep,
						gfp_t gfp_flags)
{
	struct m66592_request *req;

	req = kzalloc(sizeof(struct m66592_request), gfp_flags);
	if (!req)
		return NULL;

	INIT_LIST_HEAD(&req->queue);

	return &req->req;
}

static void m66592_free_request(struct usb_ep *_ep, struct usb_request *_req)
{
	struct m66592_request *req;

	req = container_of(_req, struct m66592_request, req);
	kfree(req);
}

static int m66592_queue(struct usb_ep *_ep, struct usb_request *_req,
			gfp_t gfp_flags)
{
	struct m66592_ep *ep;
	struct m66592_request *req;
	unsigned long flags;
	int request = 0;

	ep = container_of(_ep, struct m66592_ep, ep);
	req = container_of(_req, struct m66592_request, req);

	if (ep->m66592->gadget.speed == USB_SPEED_UNKNOWN)
		return -ESHUTDOWN;

	spin_lock_irqsave(&ep->m66592->lock, flags);

	if (list_empty(&ep->queue))
		request = 1;

	list_add_tail(&req->queue, &ep->queue);
	req->req.actual = 0;
	req->req.status = -EINPROGRESS;

1369
	if (ep->desc == NULL)	/* control */
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461
		start_ep0(ep, req);
	else {
		if (request && !ep->busy)
			start_packet(ep, req);
	}

	spin_unlock_irqrestore(&ep->m66592->lock, flags);

	return 0;
}

static int m66592_dequeue(struct usb_ep *_ep, struct usb_request *_req)
{
	struct m66592_ep *ep;
	struct m66592_request *req;
	unsigned long flags;

	ep = container_of(_ep, struct m66592_ep, ep);
	req = container_of(_req, struct m66592_request, req);

	spin_lock_irqsave(&ep->m66592->lock, flags);
	if (!list_empty(&ep->queue))
		transfer_complete(ep, req, -ECONNRESET);
	spin_unlock_irqrestore(&ep->m66592->lock, flags);

	return 0;
}

static int m66592_set_halt(struct usb_ep *_ep, int value)
{
	struct m66592_ep *ep;
	struct m66592_request *req;
	unsigned long flags;
	int ret = 0;

	ep = container_of(_ep, struct m66592_ep, ep);
	req = list_entry(ep->queue.next, struct m66592_request, queue);

	spin_lock_irqsave(&ep->m66592->lock, flags);
	if (!list_empty(&ep->queue)) {
		ret = -EAGAIN;
		goto out;
	}
	if (value) {
		ep->busy = 1;
		pipe_stall(ep->m66592, ep->pipenum);
	} else {
		ep->busy = 0;
		pipe_stop(ep->m66592, ep->pipenum);
	}

out:
	spin_unlock_irqrestore(&ep->m66592->lock, flags);
	return ret;
}

static void m66592_fifo_flush(struct usb_ep *_ep)
{
	struct m66592_ep *ep;
	unsigned long flags;

	ep = container_of(_ep, struct m66592_ep, ep);
	spin_lock_irqsave(&ep->m66592->lock, flags);
	if (list_empty(&ep->queue) && !ep->busy) {
		pipe_stop(ep->m66592, ep->pipenum);
		m66592_bclr(ep->m66592, M66592_BCLR, ep->fifoctr);
	}
	spin_unlock_irqrestore(&ep->m66592->lock, flags);
}

static struct usb_ep_ops m66592_ep_ops = {
	.enable		= m66592_enable,
	.disable	= m66592_disable,

	.alloc_request	= m66592_alloc_request,
	.free_request	= m66592_free_request,

	.queue		= m66592_queue,
	.dequeue	= m66592_dequeue,

	.set_halt	= m66592_set_halt,
	.fifo_flush	= m66592_fifo_flush,
};

/*-------------------------------------------------------------------------*/
static struct m66592 *the_controller;

int usb_gadget_register_driver(struct usb_gadget_driver *driver)
{
	struct m66592 *m66592 = the_controller;
	int retval;

1462 1463 1464 1465
	if (!driver
			|| driver->speed != USB_SPEED_HIGH
			|| !driver->bind
			|| !driver->setup)
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478
		return -EINVAL;
	if (!m66592)
		return -ENODEV;
	if (m66592->driver)
		return -EBUSY;

	/* hook up the driver */
	driver->driver.bus = NULL;
	m66592->driver = driver;
	m66592->gadget.dev.driver = &driver->driver;

	retval = device_add(&m66592->gadget.dev);
	if (retval) {
1479
		pr_err("device_add error (%d)\n", retval);
1480 1481 1482 1483 1484
		goto error;
	}

	retval = driver->bind (&m66592->gadget);
	if (retval) {
1485
		pr_err("bind to driver error (%d)\n", retval);
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496
		device_del(&m66592->gadget.dev);
		goto error;
	}

	m66592_bset(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0);
	if (m66592_read(m66592, M66592_INTSTS0) & M66592_VBSTS) {
		m66592_start_xclock(m66592);
		/* start vbus sampling */
		m66592->old_vbus = m66592_read(m66592,
					 M66592_INTSTS0) & M66592_VBSTS;
		m66592->scount = M66592_MAX_SAMPLING;
1497
		mod_timer(&m66592->timer, jiffies + msecs_to_jiffies(50));
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514
	}

	return 0;

error:
	m66592->driver = NULL;
	m66592->gadget.dev.driver = NULL;

	return retval;
}
EXPORT_SYMBOL(usb_gadget_register_driver);

int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
{
	struct m66592 *m66592 = the_controller;
	unsigned long flags;

1515 1516 1517
	if (driver != m66592->driver || !driver->unbind)
		return -EINVAL;

1518 1519 1520 1521 1522 1523 1524 1525
	spin_lock_irqsave(&m66592->lock, flags);
	if (m66592->gadget.speed != USB_SPEED_UNKNOWN)
		m66592_usb_disconnect(m66592);
	spin_unlock_irqrestore(&m66592->lock, flags);

	m66592_bclr(m66592, M66592_VBSE | M66592_URST, M66592_INTENB0);

	driver->unbind(&m66592->gadget);
1526
	m66592->gadget.dev.driver = NULL;
1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547

	init_controller(m66592);
	disable_controller(m66592);

	device_del(&m66592->gadget.dev);
	m66592->driver = NULL;
	return 0;
}
EXPORT_SYMBOL(usb_gadget_unregister_driver);

/*-------------------------------------------------------------------------*/
static int m66592_get_frame(struct usb_gadget *_gadget)
{
	struct m66592 *m66592 = gadget_to_m66592(_gadget);
	return m66592_read(m66592, M66592_FRMNUM) & 0x03FF;
}

static struct usb_gadget_ops m66592_gadget_ops = {
	.get_frame		= m66592_get_frame,
};

1548
static int __exit m66592_remove(struct platform_device *pdev)
1549 1550 1551 1552 1553 1554
{
	struct m66592		*m66592 = dev_get_drvdata(&pdev->dev);

	del_timer_sync(&m66592->timer);
	iounmap(m66592->reg);
	free_irq(platform_get_irq(pdev, 0), m66592);
1555
	m66592_free_request(&m66592->ep[0].ep, m66592->ep0_req);
1556 1557 1558 1559 1560
#ifdef CONFIG_HAVE_CLK
	if (m66592->pdata->on_chip) {
		clk_disable(m66592->clk);
		clk_put(m66592->clk);
	}
1561
#endif
1562 1563 1564 1565
	kfree(m66592);
	return 0;
}

1566 1567 1568 1569
static void nop_completion(struct usb_ep *ep, struct usb_request *r)
{
}

1570 1571
static int __init m66592_probe(struct platform_device *pdev)
{
1572
	struct resource *res, *ires;
1573 1574
	void __iomem *reg = NULL;
	struct m66592 *m66592 = NULL;
1575
#ifdef CONFIG_HAVE_CLK
1576 1577
	char clk_name[8];
#endif
1578 1579 1580
	int ret = 0;
	int i;

1581
	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1582 1583
	if (!res) {
		ret = -ENODEV;
1584
		pr_err("platform_get_resource error.\n");
1585 1586 1587
		goto clean_up;
	}

1588 1589
	ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
	if (!ires) {
1590
		ret = -ENODEV;
1591 1592
		dev_err(&pdev->dev,
			"platform_get_resource IORESOURCE_IRQ error.\n");
1593 1594 1595
		goto clean_up;
	}

1596
	reg = ioremap(res->start, resource_size(res));
1597 1598
	if (reg == NULL) {
		ret = -ENOMEM;
1599
		pr_err("ioremap error.\n");
1600 1601 1602
		goto clean_up;
	}

1603 1604 1605 1606 1607 1608
	if (pdev->dev.platform_data == NULL) {
		dev_err(&pdev->dev, "no platform data\n");
		ret = -ENODEV;
		goto clean_up;
	}

1609 1610 1611
	/* initialize ucd */
	m66592 = kzalloc(sizeof(struct m66592), GFP_KERNEL);
	if (m66592 == NULL) {
1612
		pr_err("kzalloc error\n");
1613 1614 1615
		goto clean_up;
	}

1616 1617 1618
	m66592->pdata = pdev->dev.platform_data;
	m66592->irq_trigger = ires->flags & IRQF_TRIGGER_MASK;

1619 1620 1621 1622 1623
	spin_lock_init(&m66592->lock);
	dev_set_drvdata(&pdev->dev, m66592);

	m66592->gadget.ops = &m66592_gadget_ops;
	device_initialize(&m66592->gadget.dev);
1624
	dev_set_name(&m66592->gadget.dev, "gadget");
1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635
	m66592->gadget.is_dualspeed = 1;
	m66592->gadget.dev.parent = &pdev->dev;
	m66592->gadget.dev.dma_mask = pdev->dev.dma_mask;
	m66592->gadget.dev.release = pdev->dev.release;
	m66592->gadget.name = udc_name;

	init_timer(&m66592->timer);
	m66592->timer.function = m66592_timer;
	m66592->timer.data = (unsigned long)m66592;
	m66592->reg = reg;

1636
	ret = request_irq(ires->start, m66592_irq, IRQF_DISABLED | IRQF_SHARED,
1637
			udc_name, m66592);
1638
	if (ret < 0) {
1639
		pr_err("request_irq error (%d)\n", ret);
1640 1641 1642
		goto clean_up;
	}

1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653
#ifdef CONFIG_HAVE_CLK
	if (m66592->pdata->on_chip) {
		snprintf(clk_name, sizeof(clk_name), "usbf%d", pdev->id);
		m66592->clk = clk_get(&pdev->dev, clk_name);
		if (IS_ERR(m66592->clk)) {
			dev_err(&pdev->dev, "cannot get clock \"%s\"\n",
				clk_name);
			ret = PTR_ERR(m66592->clk);
			goto clean_up2;
		}
		clk_enable(m66592->clk);
1654 1655
	}
#endif
1656 1657 1658 1659 1660 1661 1662 1663 1664
	INIT_LIST_HEAD(&m66592->gadget.ep_list);
	m66592->gadget.ep0 = &m66592->ep[0].ep;
	INIT_LIST_HEAD(&m66592->gadget.ep0->ep_list);
	for (i = 0; i < M66592_MAX_NUM_PIPE; i++) {
		struct m66592_ep *ep = &m66592->ep[i];

		if (i != 0) {
			INIT_LIST_HEAD(&m66592->ep[i].ep.ep_list);
			list_add_tail(&m66592->ep[i].ep.ep_list,
1665
					&m66592->gadget.ep_list);
1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686
		}
		ep->m66592 = m66592;
		INIT_LIST_HEAD(&ep->queue);
		ep->ep.name = m66592_ep_name[i];
		ep->ep.ops = &m66592_ep_ops;
		ep->ep.maxpacket = 512;
	}
	m66592->ep[0].ep.maxpacket = 64;
	m66592->ep[0].pipenum = 0;
	m66592->ep[0].fifoaddr = M66592_CFIFO;
	m66592->ep[0].fifosel = M66592_CFIFOSEL;
	m66592->ep[0].fifoctr = M66592_CFIFOCTR;
	m66592->ep[0].fifotrn = 0;
	m66592->ep[0].pipectr = get_pipectr_addr(0);
	m66592->pipenum2ep[0] = &m66592->ep[0];
	m66592->epaddr2ep[0] = &m66592->ep[0];

	the_controller = m66592;

	m66592->ep0_req = m66592_alloc_request(&m66592->ep[0].ep, GFP_KERNEL);
	if (m66592->ep0_req == NULL)
1687
		goto clean_up3;
1688
	m66592->ep0_req->complete = nop_completion;
1689 1690 1691

	init_controller(m66592);

1692
	dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1693 1694
	return 0;

1695
clean_up3:
1696 1697 1698 1699 1700
#ifdef CONFIG_HAVE_CLK
	if (m66592->pdata->on_chip) {
		clk_disable(m66592->clk);
		clk_put(m66592->clk);
	}
1701
clean_up2:
1702
#endif
1703
	free_irq(ires->start, m66592);
1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717
clean_up:
	if (m66592) {
		if (m66592->ep0_req)
			m66592_free_request(&m66592->ep[0].ep, m66592->ep0_req);
		kfree(m66592);
	}
	if (reg)
		iounmap(reg);

	return ret;
}

/*-------------------------------------------------------------------------*/
static struct platform_driver m66592_driver = {
1718
	.remove =	__exit_p(m66592_remove),
1719 1720
	.driver		= {
		.name =	(char *) udc_name,
1721
		.owner	= THIS_MODULE,
1722 1723 1724 1725 1726
	},
};

static int __init m66592_udc_init(void)
{
1727
	return platform_driver_probe(&m66592_driver, m66592_probe);
1728 1729 1730 1731 1732 1733 1734 1735
}
module_init(m66592_udc_init);

static void __exit m66592_udc_cleanup(void)
{
	platform_driver_unregister(&m66592_driver);
}
module_exit(m66592_udc_cleanup);