hwmon: applesmc: prolong status wait
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / usb / gadget / fsl_qe_udc.c
blob1fe8b44787b332c93e4b8261243c08e3816b2d27
1 /*
2 * driver/usb/gadget/fsl_qe_udc.c
4 * Copyright (c) 2006-2008 Freescale Semiconductor, Inc. All rights reserved.
6 * Xie Xiaobo <X.Xie@freescale.com>
7 * Li Yang <leoli@freescale.com>
8 * Based on bareboard code from Shlomi Gridish.
10 * Description:
11 * Freescle QE/CPM USB Pheripheral Controller Driver
12 * The controller can be found on MPC8360, MPC8272, and etc.
13 * MPC8360 Rev 1.1 may need QE mircocode update
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
21 #undef USB_TRACE
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/ioport.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/slab.h>
30 #include <linux/list.h>
31 #include <linux/interrupt.h>
32 #include <linux/io.h>
33 #include <linux/moduleparam.h>
34 #include <linux/of_platform.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/usb/ch9.h>
37 #include <linux/usb/gadget.h>
38 #include <linux/usb/otg.h>
39 #include <asm/qe.h>
40 #include <asm/cpm.h>
41 #include <asm/dma.h>
42 #include <asm/reg.h>
43 #include "fsl_qe_udc.h"
45 #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver"
46 #define DRIVER_AUTHOR "Xie XiaoBo"
47 #define DRIVER_VERSION "1.0"
49 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
51 static const char driver_name[] = "fsl_qe_udc";
52 static const char driver_desc[] = DRIVER_DESC;
54 /*ep name is important in gadget, it should obey the convention of ep_match()*/
55 static const char *const ep_name[] = {
56 "ep0-control", /* everyone has ep0 */
57 /* 3 configurable endpoints */
58 "ep1",
59 "ep2",
60 "ep3",
63 static struct usb_endpoint_descriptor qe_ep0_desc = {
64 .bLength = USB_DT_ENDPOINT_SIZE,
65 .bDescriptorType = USB_DT_ENDPOINT,
67 .bEndpointAddress = 0,
68 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
69 .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
72 /* it is initialized in probe() */
73 static struct qe_udc *udc_controller;
75 /********************************************************************
76 * Internal Used Function Start
77 ********************************************************************/
78 /*-----------------------------------------------------------------
79 * done() - retire a request; caller blocked irqs
80 *--------------------------------------------------------------*/
81 static void done(struct qe_ep *ep, struct qe_req *req, int status)
83 struct qe_udc *udc = ep->udc;
84 unsigned char stopped = ep->stopped;
86 /* the req->queue pointer is used by ep_queue() func, in which
87 * the request will be added into a udc_ep->queue 'd tail
88 * so here the req will be dropped from the ep->queue
90 list_del_init(&req->queue);
92 /* req.status should be set as -EINPROGRESS in ep_queue() */
93 if (req->req.status == -EINPROGRESS)
94 req->req.status = status;
95 else
96 status = req->req.status;
98 if (req->mapped) {
99 dma_unmap_single(udc->gadget.dev.parent,
100 req->req.dma, req->req.length,
101 ep_is_in(ep)
102 ? DMA_TO_DEVICE
103 : DMA_FROM_DEVICE);
104 req->req.dma = DMA_ADDR_INVALID;
105 req->mapped = 0;
106 } else
107 dma_sync_single_for_cpu(udc->gadget.dev.parent,
108 req->req.dma, req->req.length,
109 ep_is_in(ep)
110 ? DMA_TO_DEVICE
111 : DMA_FROM_DEVICE);
113 if (status && (status != -ESHUTDOWN))
114 dev_vdbg(udc->dev, "complete %s req %p stat %d len %u/%u\n",
115 ep->ep.name, &req->req, status,
116 req->req.actual, req->req.length);
118 /* don't modify queue heads during completion callback */
119 ep->stopped = 1;
120 spin_unlock(&udc->lock);
122 /* this complete() should a func implemented by gadget layer,
123 * eg fsg->bulk_in_complete() */
124 if (req->req.complete)
125 req->req.complete(&ep->ep, &req->req);
127 spin_lock(&udc->lock);
129 ep->stopped = stopped;
132 /*-----------------------------------------------------------------
133 * nuke(): delete all requests related to this ep
134 *--------------------------------------------------------------*/
135 static void nuke(struct qe_ep *ep, int status)
137 /* Whether this eq has request linked */
138 while (!list_empty(&ep->queue)) {
139 struct qe_req *req = NULL;
140 req = list_entry(ep->queue.next, struct qe_req, queue);
142 done(ep, req, status);
146 /*---------------------------------------------------------------------------*
147 * USB and Endpoint manipulate process, include parameter and register *
148 *---------------------------------------------------------------------------*/
149 /* @value: 1--set stall 0--clean stall */
150 static int qe_eprx_stall_change(struct qe_ep *ep, int value)
152 u16 tem_usep;
153 u8 epnum = ep->epnum;
154 struct qe_udc *udc = ep->udc;
156 tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]);
157 tem_usep = tem_usep & ~USB_RHS_MASK;
158 if (value == 1)
159 tem_usep |= USB_RHS_STALL;
160 else if (ep->dir == USB_DIR_IN)
161 tem_usep |= USB_RHS_IGNORE_OUT;
163 out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep);
164 return 0;
167 static int qe_eptx_stall_change(struct qe_ep *ep, int value)
169 u16 tem_usep;
170 u8 epnum = ep->epnum;
171 struct qe_udc *udc = ep->udc;
173 tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]);
174 tem_usep = tem_usep & ~USB_THS_MASK;
175 if (value == 1)
176 tem_usep |= USB_THS_STALL;
177 else if (ep->dir == USB_DIR_OUT)
178 tem_usep |= USB_THS_IGNORE_IN;
180 out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep);
182 return 0;
185 static int qe_ep0_stall(struct qe_udc *udc)
187 qe_eptx_stall_change(&udc->eps[0], 1);
188 qe_eprx_stall_change(&udc->eps[0], 1);
189 udc_controller->ep0_state = WAIT_FOR_SETUP;
190 udc_controller->ep0_dir = 0;
191 return 0;
194 static int qe_eprx_nack(struct qe_ep *ep)
196 u8 epnum = ep->epnum;
197 struct qe_udc *udc = ep->udc;
199 if (ep->state == EP_STATE_IDLE) {
200 /* Set the ep's nack */
201 clrsetbits_be16(&udc->usb_regs->usb_usep[epnum],
202 USB_RHS_MASK, USB_RHS_NACK);
204 /* Mask Rx and Busy interrupts */
205 clrbits16(&udc->usb_regs->usb_usbmr,
206 (USB_E_RXB_MASK | USB_E_BSY_MASK));
208 ep->state = EP_STATE_NACK;
210 return 0;
213 static int qe_eprx_normal(struct qe_ep *ep)
215 struct qe_udc *udc = ep->udc;
217 if (ep->state == EP_STATE_NACK) {
218 clrsetbits_be16(&udc->usb_regs->usb_usep[ep->epnum],
219 USB_RTHS_MASK, USB_THS_IGNORE_IN);
221 /* Unmask RX interrupts */
222 out_be16(&udc->usb_regs->usb_usber,
223 USB_E_BSY_MASK | USB_E_RXB_MASK);
224 setbits16(&udc->usb_regs->usb_usbmr,
225 (USB_E_RXB_MASK | USB_E_BSY_MASK));
227 ep->state = EP_STATE_IDLE;
228 ep->has_data = 0;
231 return 0;
234 static int qe_ep_cmd_stoptx(struct qe_ep *ep)
236 if (ep->udc->soc_type == PORT_CPM)
237 cpm_command(CPM_USB_STOP_TX | (ep->epnum << CPM_USB_EP_SHIFT),
238 CPM_USB_STOP_TX_OPCODE);
239 else
240 qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB,
241 ep->epnum, 0);
243 return 0;
246 static int qe_ep_cmd_restarttx(struct qe_ep *ep)
248 if (ep->udc->soc_type == PORT_CPM)
249 cpm_command(CPM_USB_RESTART_TX | (ep->epnum <<
250 CPM_USB_EP_SHIFT), CPM_USB_RESTART_TX_OPCODE);
251 else
252 qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB,
253 ep->epnum, 0);
255 return 0;
258 static int qe_ep_flushtxfifo(struct qe_ep *ep)
260 struct qe_udc *udc = ep->udc;
261 int i;
263 i = (int)ep->epnum;
265 qe_ep_cmd_stoptx(ep);
266 out_8(&udc->usb_regs->usb_uscom,
267 USB_CMD_FLUSH_FIFO | (USB_CMD_EP_MASK & (ep->epnum)));
268 out_be16(&udc->ep_param[i]->tbptr, in_be16(&udc->ep_param[i]->tbase));
269 out_be32(&udc->ep_param[i]->tstate, 0);
270 out_be16(&udc->ep_param[i]->tbcnt, 0);
272 ep->c_txbd = ep->txbase;
273 ep->n_txbd = ep->txbase;
274 qe_ep_cmd_restarttx(ep);
275 return 0;
278 static int qe_ep_filltxfifo(struct qe_ep *ep)
280 struct qe_udc *udc = ep->udc;
282 out_8(&udc->usb_regs->usb_uscom,
283 USB_CMD_STR_FIFO | (USB_CMD_EP_MASK & (ep->epnum)));
284 return 0;
287 static int qe_epbds_reset(struct qe_udc *udc, int pipe_num)
289 struct qe_ep *ep;
290 u32 bdring_len;
291 struct qe_bd __iomem *bd;
292 int i;
294 ep = &udc->eps[pipe_num];
296 if (ep->dir == USB_DIR_OUT)
297 bdring_len = USB_BDRING_LEN_RX;
298 else
299 bdring_len = USB_BDRING_LEN;
301 bd = ep->rxbase;
302 for (i = 0; i < (bdring_len - 1); i++) {
303 out_be32((u32 __iomem *)bd, R_E | R_I);
304 bd++;
306 out_be32((u32 __iomem *)bd, R_E | R_I | R_W);
308 bd = ep->txbase;
309 for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) {
310 out_be32(&bd->buf, 0);
311 out_be32((u32 __iomem *)bd, 0);
312 bd++;
314 out_be32((u32 __iomem *)bd, T_W);
316 return 0;
319 static int qe_ep_reset(struct qe_udc *udc, int pipe_num)
321 struct qe_ep *ep;
322 u16 tmpusep;
324 ep = &udc->eps[pipe_num];
325 tmpusep = in_be16(&udc->usb_regs->usb_usep[pipe_num]);
326 tmpusep &= ~USB_RTHS_MASK;
328 switch (ep->dir) {
329 case USB_DIR_BOTH:
330 qe_ep_flushtxfifo(ep);
331 break;
332 case USB_DIR_OUT:
333 tmpusep |= USB_THS_IGNORE_IN;
334 break;
335 case USB_DIR_IN:
336 qe_ep_flushtxfifo(ep);
337 tmpusep |= USB_RHS_IGNORE_OUT;
338 break;
339 default:
340 break;
342 out_be16(&udc->usb_regs->usb_usep[pipe_num], tmpusep);
344 qe_epbds_reset(udc, pipe_num);
346 return 0;
349 static int qe_ep_toggledata01(struct qe_ep *ep)
351 ep->data01 ^= 0x1;
352 return 0;
355 static int qe_ep_bd_init(struct qe_udc *udc, unsigned char pipe_num)
357 struct qe_ep *ep = &udc->eps[pipe_num];
358 unsigned long tmp_addr = 0;
359 struct usb_ep_para __iomem *epparam;
360 int i;
361 struct qe_bd __iomem *bd;
362 int bdring_len;
364 if (ep->dir == USB_DIR_OUT)
365 bdring_len = USB_BDRING_LEN_RX;
366 else
367 bdring_len = USB_BDRING_LEN;
369 epparam = udc->ep_param[pipe_num];
370 /* alloc multi-ram for BD rings and set the ep parameters */
371 tmp_addr = cpm_muram_alloc(sizeof(struct qe_bd) * (bdring_len +
372 USB_BDRING_LEN_TX), QE_ALIGNMENT_OF_BD);
373 out_be16(&epparam->rbase, (u16)tmp_addr);
374 out_be16(&epparam->tbase, (u16)(tmp_addr +
375 (sizeof(struct qe_bd) * bdring_len)));
377 out_be16(&epparam->rbptr, in_be16(&epparam->rbase));
378 out_be16(&epparam->tbptr, in_be16(&epparam->tbase));
380 ep->rxbase = cpm_muram_addr(tmp_addr);
381 ep->txbase = cpm_muram_addr(tmp_addr + (sizeof(struct qe_bd)
382 * bdring_len));
383 ep->n_rxbd = ep->rxbase;
384 ep->e_rxbd = ep->rxbase;
385 ep->n_txbd = ep->txbase;
386 ep->c_txbd = ep->txbase;
387 ep->data01 = 0; /* data0 */
389 /* Init TX and RX bds */
390 bd = ep->rxbase;
391 for (i = 0; i < bdring_len - 1; i++) {
392 out_be32(&bd->buf, 0);
393 out_be32((u32 __iomem *)bd, 0);
394 bd++;
396 out_be32(&bd->buf, 0);
397 out_be32((u32 __iomem *)bd, R_W);
399 bd = ep->txbase;
400 for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) {
401 out_be32(&bd->buf, 0);
402 out_be32((u32 __iomem *)bd, 0);
403 bd++;
405 out_be32(&bd->buf, 0);
406 out_be32((u32 __iomem *)bd, T_W);
408 return 0;
411 static int qe_ep_rxbd_update(struct qe_ep *ep)
413 unsigned int size;
414 int i;
415 unsigned int tmp;
416 struct qe_bd __iomem *bd;
417 unsigned int bdring_len;
419 if (ep->rxbase == NULL)
420 return -EINVAL;
422 bd = ep->rxbase;
424 ep->rxframe = kmalloc(sizeof(*ep->rxframe), GFP_ATOMIC);
425 if (ep->rxframe == NULL) {
426 dev_err(ep->udc->dev, "malloc rxframe failed\n");
427 return -ENOMEM;
430 qe_frame_init(ep->rxframe);
432 if (ep->dir == USB_DIR_OUT)
433 bdring_len = USB_BDRING_LEN_RX;
434 else
435 bdring_len = USB_BDRING_LEN;
437 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (bdring_len + 1);
438 ep->rxbuffer = kzalloc(size, GFP_ATOMIC);
439 if (ep->rxbuffer == NULL) {
440 dev_err(ep->udc->dev, "malloc rxbuffer failed,size=%d\n",
441 size);
442 kfree(ep->rxframe);
443 return -ENOMEM;
446 ep->rxbuf_d = virt_to_phys((void *)ep->rxbuffer);
447 if (ep->rxbuf_d == DMA_ADDR_INVALID) {
448 ep->rxbuf_d = dma_map_single(udc_controller->gadget.dev.parent,
449 ep->rxbuffer,
450 size,
451 DMA_FROM_DEVICE);
452 ep->rxbufmap = 1;
453 } else {
454 dma_sync_single_for_device(udc_controller->gadget.dev.parent,
455 ep->rxbuf_d, size,
456 DMA_FROM_DEVICE);
457 ep->rxbufmap = 0;
460 size = ep->ep.maxpacket + USB_CRC_SIZE + 2;
461 tmp = ep->rxbuf_d;
462 tmp = (u32)(((tmp >> 2) << 2) + 4);
464 for (i = 0; i < bdring_len - 1; i++) {
465 out_be32(&bd->buf, tmp);
466 out_be32((u32 __iomem *)bd, (R_E | R_I));
467 tmp = tmp + size;
468 bd++;
470 out_be32(&bd->buf, tmp);
471 out_be32((u32 __iomem *)bd, (R_E | R_I | R_W));
473 return 0;
476 static int qe_ep_register_init(struct qe_udc *udc, unsigned char pipe_num)
478 struct qe_ep *ep = &udc->eps[pipe_num];
479 struct usb_ep_para __iomem *epparam;
480 u16 usep, logepnum;
481 u16 tmp;
482 u8 rtfcr = 0;
484 epparam = udc->ep_param[pipe_num];
486 usep = 0;
487 logepnum = (ep->desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
488 usep |= (logepnum << USB_EPNUM_SHIFT);
490 switch (ep->desc->bmAttributes & 0x03) {
491 case USB_ENDPOINT_XFER_BULK:
492 usep |= USB_TRANS_BULK;
493 break;
494 case USB_ENDPOINT_XFER_ISOC:
495 usep |= USB_TRANS_ISO;
496 break;
497 case USB_ENDPOINT_XFER_INT:
498 usep |= USB_TRANS_INT;
499 break;
500 default:
501 usep |= USB_TRANS_CTR;
502 break;
505 switch (ep->dir) {
506 case USB_DIR_OUT:
507 usep |= USB_THS_IGNORE_IN;
508 break;
509 case USB_DIR_IN:
510 usep |= USB_RHS_IGNORE_OUT;
511 break;
512 default:
513 break;
515 out_be16(&udc->usb_regs->usb_usep[pipe_num], usep);
517 rtfcr = 0x30;
518 out_8(&epparam->rbmr, rtfcr);
519 out_8(&epparam->tbmr, rtfcr);
521 tmp = (u16)(ep->ep.maxpacket + USB_CRC_SIZE);
522 /* MRBLR must be divisble by 4 */
523 tmp = (u16)(((tmp >> 2) << 2) + 4);
524 out_be16(&epparam->mrblr, tmp);
526 return 0;
529 static int qe_ep_init(struct qe_udc *udc,
530 unsigned char pipe_num,
531 const struct usb_endpoint_descriptor *desc)
533 struct qe_ep *ep = &udc->eps[pipe_num];
534 unsigned long flags;
535 int reval = 0;
536 u16 max = 0;
538 max = le16_to_cpu(desc->wMaxPacketSize);
540 /* check the max package size validate for this endpoint */
541 /* Refer to USB2.0 spec table 9-13,
543 if (pipe_num != 0) {
544 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
545 case USB_ENDPOINT_XFER_BULK:
546 if (strstr(ep->ep.name, "-iso")
547 || strstr(ep->ep.name, "-int"))
548 goto en_done;
549 switch (udc->gadget.speed) {
550 case USB_SPEED_HIGH:
551 if ((max == 128) || (max == 256) || (max == 512))
552 break;
553 default:
554 switch (max) {
555 case 4:
556 case 8:
557 case 16:
558 case 32:
559 case 64:
560 break;
561 default:
562 case USB_SPEED_LOW:
563 goto en_done;
566 break;
567 case USB_ENDPOINT_XFER_INT:
568 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
569 goto en_done;
570 switch (udc->gadget.speed) {
571 case USB_SPEED_HIGH:
572 if (max <= 1024)
573 break;
574 case USB_SPEED_FULL:
575 if (max <= 64)
576 break;
577 default:
578 if (max <= 8)
579 break;
580 goto en_done;
582 break;
583 case USB_ENDPOINT_XFER_ISOC:
584 if (strstr(ep->ep.name, "-bulk")
585 || strstr(ep->ep.name, "-int"))
586 goto en_done;
587 switch (udc->gadget.speed) {
588 case USB_SPEED_HIGH:
589 if (max <= 1024)
590 break;
591 case USB_SPEED_FULL:
592 if (max <= 1023)
593 break;
594 default:
595 goto en_done;
597 break;
598 case USB_ENDPOINT_XFER_CONTROL:
599 if (strstr(ep->ep.name, "-iso")
600 || strstr(ep->ep.name, "-int"))
601 goto en_done;
602 switch (udc->gadget.speed) {
603 case USB_SPEED_HIGH:
604 case USB_SPEED_FULL:
605 switch (max) {
606 case 1:
607 case 2:
608 case 4:
609 case 8:
610 case 16:
611 case 32:
612 case 64:
613 break;
614 default:
615 goto en_done;
617 case USB_SPEED_LOW:
618 switch (max) {
619 case 1:
620 case 2:
621 case 4:
622 case 8:
623 break;
624 default:
625 goto en_done;
627 default:
628 goto en_done;
630 break;
632 default:
633 goto en_done;
635 } /* if ep0*/
637 spin_lock_irqsave(&udc->lock, flags);
639 /* initialize ep structure */
640 ep->ep.maxpacket = max;
641 ep->tm = (u8)(desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
642 ep->desc = desc;
643 ep->stopped = 0;
644 ep->init = 1;
646 if (pipe_num == 0) {
647 ep->dir = USB_DIR_BOTH;
648 udc->ep0_dir = USB_DIR_OUT;
649 udc->ep0_state = WAIT_FOR_SETUP;
650 } else {
651 switch (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
652 case USB_DIR_OUT:
653 ep->dir = USB_DIR_OUT;
654 break;
655 case USB_DIR_IN:
656 ep->dir = USB_DIR_IN;
657 default:
658 break;
662 /* hardware special operation */
663 qe_ep_bd_init(udc, pipe_num);
664 if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_OUT)) {
665 reval = qe_ep_rxbd_update(ep);
666 if (reval)
667 goto en_done1;
670 if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_IN)) {
671 ep->txframe = kmalloc(sizeof(*ep->txframe), GFP_ATOMIC);
672 if (ep->txframe == NULL) {
673 dev_err(udc->dev, "malloc txframe failed\n");
674 goto en_done2;
676 qe_frame_init(ep->txframe);
679 qe_ep_register_init(udc, pipe_num);
681 /* Now HW will be NAKing transfers to that EP,
682 * until a buffer is queued to it. */
683 spin_unlock_irqrestore(&udc->lock, flags);
685 return 0;
686 en_done2:
687 kfree(ep->rxbuffer);
688 kfree(ep->rxframe);
689 en_done1:
690 spin_unlock_irqrestore(&udc->lock, flags);
691 en_done:
692 dev_dbg(udc->dev, "failed to initialize %s\n", ep->ep.name);
693 return -ENODEV;
696 static inline void qe_usb_enable(void)
698 setbits8(&udc_controller->usb_regs->usb_usmod, USB_MODE_EN);
701 static inline void qe_usb_disable(void)
703 clrbits8(&udc_controller->usb_regs->usb_usmod, USB_MODE_EN);
706 /*----------------------------------------------------------------------------*
707 * USB and EP basic manipulate function end *
708 *----------------------------------------------------------------------------*/
711 /******************************************************************************
712 UDC transmit and receive process
713 ******************************************************************************/
714 static void recycle_one_rxbd(struct qe_ep *ep)
716 u32 bdstatus;
718 bdstatus = in_be32((u32 __iomem *)ep->e_rxbd);
719 bdstatus = R_I | R_E | (bdstatus & R_W);
720 out_be32((u32 __iomem *)ep->e_rxbd, bdstatus);
722 if (bdstatus & R_W)
723 ep->e_rxbd = ep->rxbase;
724 else
725 ep->e_rxbd++;
728 static void recycle_rxbds(struct qe_ep *ep, unsigned char stopatnext)
730 u32 bdstatus;
731 struct qe_bd __iomem *bd, *nextbd;
732 unsigned char stop = 0;
734 nextbd = ep->n_rxbd;
735 bd = ep->e_rxbd;
736 bdstatus = in_be32((u32 __iomem *)bd);
738 while (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK) && !stop) {
739 bdstatus = R_E | R_I | (bdstatus & R_W);
740 out_be32((u32 __iomem *)bd, bdstatus);
742 if (bdstatus & R_W)
743 bd = ep->rxbase;
744 else
745 bd++;
747 bdstatus = in_be32((u32 __iomem *)bd);
748 if (stopatnext && (bd == nextbd))
749 stop = 1;
752 ep->e_rxbd = bd;
755 static void ep_recycle_rxbds(struct qe_ep *ep)
757 struct qe_bd __iomem *bd = ep->n_rxbd;
758 u32 bdstatus;
759 u8 epnum = ep->epnum;
760 struct qe_udc *udc = ep->udc;
762 bdstatus = in_be32((u32 __iomem *)bd);
763 if (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK)) {
764 bd = ep->rxbase +
765 ((in_be16(&udc->ep_param[epnum]->rbptr) -
766 in_be16(&udc->ep_param[epnum]->rbase))
767 >> 3);
768 bdstatus = in_be32((u32 __iomem *)bd);
770 if (bdstatus & R_W)
771 bd = ep->rxbase;
772 else
773 bd++;
775 ep->e_rxbd = bd;
776 recycle_rxbds(ep, 0);
777 ep->e_rxbd = ep->n_rxbd;
778 } else
779 recycle_rxbds(ep, 1);
781 if (in_be16(&udc->usb_regs->usb_usber) & USB_E_BSY_MASK)
782 out_be16(&udc->usb_regs->usb_usber, USB_E_BSY_MASK);
784 if (ep->has_data <= 0 && (!list_empty(&ep->queue)))
785 qe_eprx_normal(ep);
787 ep->localnack = 0;
790 static void setup_received_handle(struct qe_udc *udc,
791 struct usb_ctrlrequest *setup);
792 static int qe_ep_rxframe_handle(struct qe_ep *ep);
793 static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req);
794 /* when BD PID is setup, handle the packet */
795 static int ep0_setup_handle(struct qe_udc *udc)
797 struct qe_ep *ep = &udc->eps[0];
798 struct qe_frame *pframe;
799 unsigned int fsize;
800 u8 *cp;
802 pframe = ep->rxframe;
803 if ((frame_get_info(pframe) & PID_SETUP)
804 && (udc->ep0_state == WAIT_FOR_SETUP)) {
805 fsize = frame_get_length(pframe);
806 if (unlikely(fsize != 8))
807 return -EINVAL;
808 cp = (u8 *)&udc->local_setup_buff;
809 memcpy(cp, pframe->data, fsize);
810 ep->data01 = 1;
812 /* handle the usb command base on the usb_ctrlrequest */
813 setup_received_handle(udc, &udc->local_setup_buff);
814 return 0;
816 return -EINVAL;
819 static int qe_ep0_rx(struct qe_udc *udc)
821 struct qe_ep *ep = &udc->eps[0];
822 struct qe_frame *pframe;
823 struct qe_bd __iomem *bd;
824 u32 bdstatus, length;
825 u32 vaddr;
827 pframe = ep->rxframe;
829 if (ep->dir == USB_DIR_IN) {
830 dev_err(udc->dev, "ep0 not a control endpoint\n");
831 return -EINVAL;
834 bd = ep->n_rxbd;
835 bdstatus = in_be32((u32 __iomem *)bd);
836 length = bdstatus & BD_LENGTH_MASK;
838 while (!(bdstatus & R_E) && length) {
839 if ((bdstatus & R_F) && (bdstatus & R_L)
840 && !(bdstatus & R_ERROR)) {
841 if (length == USB_CRC_SIZE) {
842 udc->ep0_state = WAIT_FOR_SETUP;
843 dev_vdbg(udc->dev,
844 "receive a ZLP in status phase\n");
845 } else {
846 qe_frame_clean(pframe);
847 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
848 frame_set_data(pframe, (u8 *)vaddr);
849 frame_set_length(pframe,
850 (length - USB_CRC_SIZE));
851 frame_set_status(pframe, FRAME_OK);
852 switch (bdstatus & R_PID) {
853 case R_PID_SETUP:
854 frame_set_info(pframe, PID_SETUP);
855 break;
856 case R_PID_DATA1:
857 frame_set_info(pframe, PID_DATA1);
858 break;
859 default:
860 frame_set_info(pframe, PID_DATA0);
861 break;
864 if ((bdstatus & R_PID) == R_PID_SETUP)
865 ep0_setup_handle(udc);
866 else
867 qe_ep_rxframe_handle(ep);
869 } else {
870 dev_err(udc->dev, "The receive frame with error!\n");
873 /* note: don't clear the rxbd's buffer address */
874 recycle_one_rxbd(ep);
876 /* Get next BD */
877 if (bdstatus & R_W)
878 bd = ep->rxbase;
879 else
880 bd++;
882 bdstatus = in_be32((u32 __iomem *)bd);
883 length = bdstatus & BD_LENGTH_MASK;
887 ep->n_rxbd = bd;
889 return 0;
892 static int qe_ep_rxframe_handle(struct qe_ep *ep)
894 struct qe_frame *pframe;
895 u8 framepid = 0;
896 unsigned int fsize;
897 u8 *cp;
898 struct qe_req *req;
900 pframe = ep->rxframe;
902 if (frame_get_info(pframe) & PID_DATA1)
903 framepid = 0x1;
905 if (framepid != ep->data01) {
906 dev_err(ep->udc->dev, "the data01 error!\n");
907 return -EIO;
910 fsize = frame_get_length(pframe);
911 if (list_empty(&ep->queue)) {
912 dev_err(ep->udc->dev, "the %s have no requeue!\n", ep->name);
913 } else {
914 req = list_entry(ep->queue.next, struct qe_req, queue);
916 cp = (u8 *)(req->req.buf) + req->req.actual;
917 if (cp) {
918 memcpy(cp, pframe->data, fsize);
919 req->req.actual += fsize;
920 if ((fsize < ep->ep.maxpacket) ||
921 (req->req.actual >= req->req.length)) {
922 if (ep->epnum == 0)
923 ep0_req_complete(ep->udc, req);
924 else
925 done(ep, req, 0);
926 if (list_empty(&ep->queue) && ep->epnum != 0)
927 qe_eprx_nack(ep);
932 qe_ep_toggledata01(ep);
934 return 0;
937 static void ep_rx_tasklet(unsigned long data)
939 struct qe_udc *udc = (struct qe_udc *)data;
940 struct qe_ep *ep;
941 struct qe_frame *pframe;
942 struct qe_bd __iomem *bd;
943 unsigned long flags;
944 u32 bdstatus, length;
945 u32 vaddr, i;
947 spin_lock_irqsave(&udc->lock, flags);
949 for (i = 1; i < USB_MAX_ENDPOINTS; i++) {
950 ep = &udc->eps[i];
952 if (ep->dir == USB_DIR_IN || ep->enable_tasklet == 0) {
953 dev_dbg(udc->dev,
954 "This is a transmit ep or disable tasklet!\n");
955 continue;
958 pframe = ep->rxframe;
959 bd = ep->n_rxbd;
960 bdstatus = in_be32((u32 __iomem *)bd);
961 length = bdstatus & BD_LENGTH_MASK;
963 while (!(bdstatus & R_E) && length) {
964 if (list_empty(&ep->queue)) {
965 qe_eprx_nack(ep);
966 dev_dbg(udc->dev,
967 "The rxep have noreq %d\n",
968 ep->has_data);
969 break;
972 if ((bdstatus & R_F) && (bdstatus & R_L)
973 && !(bdstatus & R_ERROR)) {
974 qe_frame_clean(pframe);
975 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
976 frame_set_data(pframe, (u8 *)vaddr);
977 frame_set_length(pframe,
978 (length - USB_CRC_SIZE));
979 frame_set_status(pframe, FRAME_OK);
980 switch (bdstatus & R_PID) {
981 case R_PID_DATA1:
982 frame_set_info(pframe, PID_DATA1);
983 break;
984 case R_PID_SETUP:
985 frame_set_info(pframe, PID_SETUP);
986 break;
987 default:
988 frame_set_info(pframe, PID_DATA0);
989 break;
991 /* handle the rx frame */
992 qe_ep_rxframe_handle(ep);
993 } else {
994 dev_err(udc->dev,
995 "error in received frame\n");
997 /* note: don't clear the rxbd's buffer address */
998 /*clear the length */
999 out_be32((u32 __iomem *)bd, bdstatus & BD_STATUS_MASK);
1000 ep->has_data--;
1001 if (!(ep->localnack))
1002 recycle_one_rxbd(ep);
1004 /* Get next BD */
1005 if (bdstatus & R_W)
1006 bd = ep->rxbase;
1007 else
1008 bd++;
1010 bdstatus = in_be32((u32 __iomem *)bd);
1011 length = bdstatus & BD_LENGTH_MASK;
1014 ep->n_rxbd = bd;
1016 if (ep->localnack)
1017 ep_recycle_rxbds(ep);
1019 ep->enable_tasklet = 0;
1020 } /* for i=1 */
1022 spin_unlock_irqrestore(&udc->lock, flags);
1025 static int qe_ep_rx(struct qe_ep *ep)
1027 struct qe_udc *udc;
1028 struct qe_frame *pframe;
1029 struct qe_bd __iomem *bd;
1030 u16 swoffs, ucoffs, emptybds;
1032 udc = ep->udc;
1033 pframe = ep->rxframe;
1035 if (ep->dir == USB_DIR_IN) {
1036 dev_err(udc->dev, "transmit ep in rx function\n");
1037 return -EINVAL;
1040 bd = ep->n_rxbd;
1042 swoffs = (u16)(bd - ep->rxbase);
1043 ucoffs = (u16)((in_be16(&udc->ep_param[ep->epnum]->rbptr) -
1044 in_be16(&udc->ep_param[ep->epnum]->rbase)) >> 3);
1045 if (swoffs < ucoffs)
1046 emptybds = USB_BDRING_LEN_RX - ucoffs + swoffs;
1047 else
1048 emptybds = swoffs - ucoffs;
1050 if (emptybds < MIN_EMPTY_BDS) {
1051 qe_eprx_nack(ep);
1052 ep->localnack = 1;
1053 dev_vdbg(udc->dev, "%d empty bds, send NACK\n", emptybds);
1055 ep->has_data = USB_BDRING_LEN_RX - emptybds;
1057 if (list_empty(&ep->queue)) {
1058 qe_eprx_nack(ep);
1059 dev_vdbg(udc->dev, "The rxep have no req queued with %d BDs\n",
1060 ep->has_data);
1061 return 0;
1064 tasklet_schedule(&udc->rx_tasklet);
1065 ep->enable_tasklet = 1;
1067 return 0;
1070 /* send data from a frame, no matter what tx_req */
1071 static int qe_ep_tx(struct qe_ep *ep, struct qe_frame *frame)
1073 struct qe_udc *udc = ep->udc;
1074 struct qe_bd __iomem *bd;
1075 u16 saveusbmr;
1076 u32 bdstatus, pidmask;
1077 u32 paddr;
1079 if (ep->dir == USB_DIR_OUT) {
1080 dev_err(udc->dev, "receive ep passed to tx function\n");
1081 return -EINVAL;
1084 /* Disable the Tx interrupt */
1085 saveusbmr = in_be16(&udc->usb_regs->usb_usbmr);
1086 out_be16(&udc->usb_regs->usb_usbmr,
1087 saveusbmr & ~(USB_E_TXB_MASK | USB_E_TXE_MASK));
1089 bd = ep->n_txbd;
1090 bdstatus = in_be32((u32 __iomem *)bd);
1092 if (!(bdstatus & (T_R | BD_LENGTH_MASK))) {
1093 if (frame_get_length(frame) == 0) {
1094 frame_set_data(frame, udc->nullbuf);
1095 frame_set_length(frame, 2);
1096 frame->info |= (ZLP | NO_CRC);
1097 dev_vdbg(udc->dev, "the frame size = 0\n");
1099 paddr = virt_to_phys((void *)frame->data);
1100 out_be32(&bd->buf, paddr);
1101 bdstatus = (bdstatus&T_W);
1102 if (!(frame_get_info(frame) & NO_CRC))
1103 bdstatus |= T_R | T_I | T_L | T_TC
1104 | frame_get_length(frame);
1105 else
1106 bdstatus |= T_R | T_I | T_L | frame_get_length(frame);
1108 /* if the packet is a ZLP in status phase */
1109 if ((ep->epnum == 0) && (udc->ep0_state == DATA_STATE_NEED_ZLP))
1110 ep->data01 = 0x1;
1112 if (ep->data01) {
1113 pidmask = T_PID_DATA1;
1114 frame->info |= PID_DATA1;
1115 } else {
1116 pidmask = T_PID_DATA0;
1117 frame->info |= PID_DATA0;
1119 bdstatus |= T_CNF;
1120 bdstatus |= pidmask;
1121 out_be32((u32 __iomem *)bd, bdstatus);
1122 qe_ep_filltxfifo(ep);
1124 /* enable the TX interrupt */
1125 out_be16(&udc->usb_regs->usb_usbmr, saveusbmr);
1127 qe_ep_toggledata01(ep);
1128 if (bdstatus & T_W)
1129 ep->n_txbd = ep->txbase;
1130 else
1131 ep->n_txbd++;
1133 return 0;
1134 } else {
1135 out_be16(&udc->usb_regs->usb_usbmr, saveusbmr);
1136 dev_vdbg(udc->dev, "The tx bd is not ready!\n");
1137 return -EBUSY;
1141 /* when a bd was transmitted, the function can
1142 * handle the tx_req, not include ep0 */
1143 static int txcomplete(struct qe_ep *ep, unsigned char restart)
1145 if (ep->tx_req != NULL) {
1146 if (!restart) {
1147 int asent = ep->last;
1148 ep->sent += asent;
1149 ep->last -= asent;
1150 } else {
1151 ep->last = 0;
1154 /* a request already were transmitted completely */
1155 if ((ep->tx_req->req.length - ep->sent) <= 0) {
1156 ep->tx_req->req.actual = (unsigned int)ep->sent;
1157 done(ep, ep->tx_req, 0);
1158 ep->tx_req = NULL;
1159 ep->last = 0;
1160 ep->sent = 0;
1164 /* we should gain a new tx_req fot this endpoint */
1165 if (ep->tx_req == NULL) {
1166 if (!list_empty(&ep->queue)) {
1167 ep->tx_req = list_entry(ep->queue.next, struct qe_req,
1168 queue);
1169 ep->last = 0;
1170 ep->sent = 0;
1174 return 0;
1177 /* give a frame and a tx_req, send some data */
1178 static int qe_usb_senddata(struct qe_ep *ep, struct qe_frame *frame)
1180 unsigned int size;
1181 u8 *buf;
1183 qe_frame_clean(frame);
1184 size = min_t(u32, (ep->tx_req->req.length - ep->sent),
1185 ep->ep.maxpacket);
1186 buf = (u8 *)ep->tx_req->req.buf + ep->sent;
1187 if (buf && size) {
1188 ep->last = size;
1189 frame_set_data(frame, buf);
1190 frame_set_length(frame, size);
1191 frame_set_status(frame, FRAME_OK);
1192 frame_set_info(frame, 0);
1193 return qe_ep_tx(ep, frame);
1195 return -EIO;
1198 /* give a frame struct,send a ZLP */
1199 static int sendnulldata(struct qe_ep *ep, struct qe_frame *frame, uint infor)
1201 struct qe_udc *udc = ep->udc;
1203 if (frame == NULL)
1204 return -ENODEV;
1206 qe_frame_clean(frame);
1207 frame_set_data(frame, (u8 *)udc->nullbuf);
1208 frame_set_length(frame, 2);
1209 frame_set_status(frame, FRAME_OK);
1210 frame_set_info(frame, (ZLP | NO_CRC | infor));
1212 return qe_ep_tx(ep, frame);
1215 static int frame_create_tx(struct qe_ep *ep, struct qe_frame *frame)
1217 struct qe_req *req = ep->tx_req;
1218 int reval;
1220 if (req == NULL)
1221 return -ENODEV;
1223 if ((req->req.length - ep->sent) > 0)
1224 reval = qe_usb_senddata(ep, frame);
1225 else
1226 reval = sendnulldata(ep, frame, 0);
1228 return reval;
1231 /* if direction is DIR_IN, the status is Device->Host
1232 * if direction is DIR_OUT, the status transaction is Device<-Host
1233 * in status phase, udc create a request and gain status */
1234 static int ep0_prime_status(struct qe_udc *udc, int direction)
1237 struct qe_ep *ep = &udc->eps[0];
1239 if (direction == USB_DIR_IN) {
1240 udc->ep0_state = DATA_STATE_NEED_ZLP;
1241 udc->ep0_dir = USB_DIR_IN;
1242 sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ);
1243 } else {
1244 udc->ep0_dir = USB_DIR_OUT;
1245 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1248 return 0;
1251 /* a request complete in ep0, whether gadget request or udc request */
1252 static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req)
1254 struct qe_ep *ep = &udc->eps[0];
1255 /* because usb and ep's status already been set in ch9setaddress() */
1257 switch (udc->ep0_state) {
1258 case DATA_STATE_XMIT:
1259 done(ep, req, 0);
1260 /* receive status phase */
1261 if (ep0_prime_status(udc, USB_DIR_OUT))
1262 qe_ep0_stall(udc);
1263 break;
1265 case DATA_STATE_NEED_ZLP:
1266 done(ep, req, 0);
1267 udc->ep0_state = WAIT_FOR_SETUP;
1268 break;
1270 case DATA_STATE_RECV:
1271 done(ep, req, 0);
1272 /* send status phase */
1273 if (ep0_prime_status(udc, USB_DIR_IN))
1274 qe_ep0_stall(udc);
1275 break;
1277 case WAIT_FOR_OUT_STATUS:
1278 done(ep, req, 0);
1279 udc->ep0_state = WAIT_FOR_SETUP;
1280 break;
1282 case WAIT_FOR_SETUP:
1283 dev_vdbg(udc->dev, "Unexpected interrupt\n");
1284 break;
1286 default:
1287 qe_ep0_stall(udc);
1288 break;
1292 static int ep0_txcomplete(struct qe_ep *ep, unsigned char restart)
1294 struct qe_req *tx_req = NULL;
1295 struct qe_frame *frame = ep->txframe;
1297 if ((frame_get_info(frame) & (ZLP | NO_REQ)) == (ZLP | NO_REQ)) {
1298 if (!restart)
1299 ep->udc->ep0_state = WAIT_FOR_SETUP;
1300 else
1301 sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ);
1302 return 0;
1305 tx_req = ep->tx_req;
1306 if (tx_req != NULL) {
1307 if (!restart) {
1308 int asent = ep->last;
1309 ep->sent += asent;
1310 ep->last -= asent;
1311 } else {
1312 ep->last = 0;
1315 /* a request already were transmitted completely */
1316 if ((ep->tx_req->req.length - ep->sent) <= 0) {
1317 ep->tx_req->req.actual = (unsigned int)ep->sent;
1318 ep0_req_complete(ep->udc, ep->tx_req);
1319 ep->tx_req = NULL;
1320 ep->last = 0;
1321 ep->sent = 0;
1323 } else {
1324 dev_vdbg(ep->udc->dev, "the ep0_controller have no req\n");
1327 return 0;
1330 static int ep0_txframe_handle(struct qe_ep *ep)
1332 /* if have error, transmit again */
1333 if (frame_get_status(ep->txframe) & FRAME_ERROR) {
1334 qe_ep_flushtxfifo(ep);
1335 dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n");
1336 if (frame_get_info(ep->txframe) & PID_DATA0)
1337 ep->data01 = 0;
1338 else
1339 ep->data01 = 1;
1341 ep0_txcomplete(ep, 1);
1342 } else
1343 ep0_txcomplete(ep, 0);
1345 frame_create_tx(ep, ep->txframe);
1346 return 0;
1349 static int qe_ep0_txconf(struct qe_ep *ep)
1351 struct qe_bd __iomem *bd;
1352 struct qe_frame *pframe;
1353 u32 bdstatus;
1355 bd = ep->c_txbd;
1356 bdstatus = in_be32((u32 __iomem *)bd);
1357 while (!(bdstatus & T_R) && (bdstatus & ~T_W)) {
1358 pframe = ep->txframe;
1360 /* clear and recycle the BD */
1361 out_be32((u32 __iomem *)bd, bdstatus & T_W);
1362 out_be32(&bd->buf, 0);
1363 if (bdstatus & T_W)
1364 ep->c_txbd = ep->txbase;
1365 else
1366 ep->c_txbd++;
1368 if (ep->c_txbd == ep->n_txbd) {
1369 if (bdstatus & DEVICE_T_ERROR) {
1370 frame_set_status(pframe, FRAME_ERROR);
1371 if (bdstatus & T_TO)
1372 pframe->status |= TX_ER_TIMEOUT;
1373 if (bdstatus & T_UN)
1374 pframe->status |= TX_ER_UNDERUN;
1376 ep0_txframe_handle(ep);
1379 bd = ep->c_txbd;
1380 bdstatus = in_be32((u32 __iomem *)bd);
1383 return 0;
1386 static int ep_txframe_handle(struct qe_ep *ep)
1388 if (frame_get_status(ep->txframe) & FRAME_ERROR) {
1389 qe_ep_flushtxfifo(ep);
1390 dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n");
1391 if (frame_get_info(ep->txframe) & PID_DATA0)
1392 ep->data01 = 0;
1393 else
1394 ep->data01 = 1;
1396 txcomplete(ep, 1);
1397 } else
1398 txcomplete(ep, 0);
1400 frame_create_tx(ep, ep->txframe); /* send the data */
1401 return 0;
1404 /* confirm the already trainsmited bd */
1405 static int qe_ep_txconf(struct qe_ep *ep)
1407 struct qe_bd __iomem *bd;
1408 struct qe_frame *pframe = NULL;
1409 u32 bdstatus;
1410 unsigned char breakonrxinterrupt = 0;
1412 bd = ep->c_txbd;
1413 bdstatus = in_be32((u32 __iomem *)bd);
1414 while (!(bdstatus & T_R) && (bdstatus & ~T_W)) {
1415 pframe = ep->txframe;
1416 if (bdstatus & DEVICE_T_ERROR) {
1417 frame_set_status(pframe, FRAME_ERROR);
1418 if (bdstatus & T_TO)
1419 pframe->status |= TX_ER_TIMEOUT;
1420 if (bdstatus & T_UN)
1421 pframe->status |= TX_ER_UNDERUN;
1424 /* clear and recycle the BD */
1425 out_be32((u32 __iomem *)bd, bdstatus & T_W);
1426 out_be32(&bd->buf, 0);
1427 if (bdstatus & T_W)
1428 ep->c_txbd = ep->txbase;
1429 else
1430 ep->c_txbd++;
1432 /* handle the tx frame */
1433 ep_txframe_handle(ep);
1434 bd = ep->c_txbd;
1435 bdstatus = in_be32((u32 __iomem *)bd);
1437 if (breakonrxinterrupt)
1438 return -EIO;
1439 else
1440 return 0;
1443 /* Add a request in queue, and try to transmit a packet */
1444 static int ep_req_send(struct qe_ep *ep, struct qe_req *req)
1446 int reval = 0;
1448 if (ep->tx_req == NULL) {
1449 ep->sent = 0;
1450 ep->last = 0;
1451 txcomplete(ep, 0); /* can gain a new tx_req */
1452 reval = frame_create_tx(ep, ep->txframe);
1454 return reval;
1457 /* Maybe this is a good ideal */
1458 static int ep_req_rx(struct qe_ep *ep, struct qe_req *req)
1460 struct qe_udc *udc = ep->udc;
1461 struct qe_frame *pframe = NULL;
1462 struct qe_bd __iomem *bd;
1463 u32 bdstatus, length;
1464 u32 vaddr, fsize;
1465 u8 *cp;
1466 u8 finish_req = 0;
1467 u8 framepid;
1469 if (list_empty(&ep->queue)) {
1470 dev_vdbg(udc->dev, "the req already finish!\n");
1471 return 0;
1473 pframe = ep->rxframe;
1475 bd = ep->n_rxbd;
1476 bdstatus = in_be32((u32 __iomem *)bd);
1477 length = bdstatus & BD_LENGTH_MASK;
1479 while (!(bdstatus & R_E) && length) {
1480 if (finish_req)
1481 break;
1482 if ((bdstatus & R_F) && (bdstatus & R_L)
1483 && !(bdstatus & R_ERROR)) {
1484 qe_frame_clean(pframe);
1485 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
1486 frame_set_data(pframe, (u8 *)vaddr);
1487 frame_set_length(pframe, (length - USB_CRC_SIZE));
1488 frame_set_status(pframe, FRAME_OK);
1489 switch (bdstatus & R_PID) {
1490 case R_PID_DATA1:
1491 frame_set_info(pframe, PID_DATA1); break;
1492 default:
1493 frame_set_info(pframe, PID_DATA0); break;
1495 /* handle the rx frame */
1497 if (frame_get_info(pframe) & PID_DATA1)
1498 framepid = 0x1;
1499 else
1500 framepid = 0;
1502 if (framepid != ep->data01) {
1503 dev_vdbg(udc->dev, "the data01 error!\n");
1504 } else {
1505 fsize = frame_get_length(pframe);
1507 cp = (u8 *)(req->req.buf) + req->req.actual;
1508 if (cp) {
1509 memcpy(cp, pframe->data, fsize);
1510 req->req.actual += fsize;
1511 if ((fsize < ep->ep.maxpacket)
1512 || (req->req.actual >=
1513 req->req.length)) {
1514 finish_req = 1;
1515 done(ep, req, 0);
1516 if (list_empty(&ep->queue))
1517 qe_eprx_nack(ep);
1520 qe_ep_toggledata01(ep);
1522 } else {
1523 dev_err(udc->dev, "The receive frame with error!\n");
1526 /* note: don't clear the rxbd's buffer address *
1527 * only Clear the length */
1528 out_be32((u32 __iomem *)bd, (bdstatus & BD_STATUS_MASK));
1529 ep->has_data--;
1531 /* Get next BD */
1532 if (bdstatus & R_W)
1533 bd = ep->rxbase;
1534 else
1535 bd++;
1537 bdstatus = in_be32((u32 __iomem *)bd);
1538 length = bdstatus & BD_LENGTH_MASK;
1541 ep->n_rxbd = bd;
1542 ep_recycle_rxbds(ep);
1544 return 0;
1547 /* only add the request in queue */
1548 static int ep_req_receive(struct qe_ep *ep, struct qe_req *req)
1550 if (ep->state == EP_STATE_NACK) {
1551 if (ep->has_data <= 0) {
1552 /* Enable rx and unmask rx interrupt */
1553 qe_eprx_normal(ep);
1554 } else {
1555 /* Copy the exist BD data */
1556 ep_req_rx(ep, req);
1560 return 0;
1563 /********************************************************************
1564 Internal Used Function End
1565 ********************************************************************/
1567 /*-----------------------------------------------------------------------
1568 Endpoint Management Functions For Gadget
1569 -----------------------------------------------------------------------*/
1570 static int qe_ep_enable(struct usb_ep *_ep,
1571 const struct usb_endpoint_descriptor *desc)
1573 struct qe_udc *udc;
1574 struct qe_ep *ep;
1575 int retval = 0;
1576 unsigned char epnum;
1578 ep = container_of(_ep, struct qe_ep, ep);
1580 /* catch various bogus parameters */
1581 if (!_ep || !desc || ep->desc || _ep->name == ep_name[0] ||
1582 (desc->bDescriptorType != USB_DT_ENDPOINT))
1583 return -EINVAL;
1585 udc = ep->udc;
1586 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
1587 return -ESHUTDOWN;
1589 epnum = (u8)desc->bEndpointAddress & 0xF;
1591 retval = qe_ep_init(udc, epnum, desc);
1592 if (retval != 0) {
1593 cpm_muram_free(cpm_muram_offset(ep->rxbase));
1594 dev_dbg(udc->dev, "enable ep%d failed\n", ep->epnum);
1595 return -EINVAL;
1597 dev_dbg(udc->dev, "enable ep%d successful\n", ep->epnum);
1598 return 0;
1601 static int qe_ep_disable(struct usb_ep *_ep)
1603 struct qe_udc *udc;
1604 struct qe_ep *ep;
1605 unsigned long flags;
1606 unsigned int size;
1608 ep = container_of(_ep, struct qe_ep, ep);
1609 udc = ep->udc;
1611 if (!_ep || !ep->desc) {
1612 dev_dbg(udc->dev, "%s not enabled\n", _ep ? ep->ep.name : NULL);
1613 return -EINVAL;
1616 spin_lock_irqsave(&udc->lock, flags);
1617 /* Nuke all pending requests (does flush) */
1618 nuke(ep, -ESHUTDOWN);
1619 ep->desc = NULL;
1620 ep->stopped = 1;
1621 spin_unlock_irqrestore(&udc->lock, flags);
1623 cpm_muram_free(cpm_muram_offset(ep->rxbase));
1625 if (ep->dir == USB_DIR_OUT)
1626 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) *
1627 (USB_BDRING_LEN_RX + 1);
1628 else
1629 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) *
1630 (USB_BDRING_LEN + 1);
1632 if (ep->dir != USB_DIR_IN) {
1633 kfree(ep->rxframe);
1634 if (ep->rxbufmap) {
1635 dma_unmap_single(udc_controller->gadget.dev.parent,
1636 ep->rxbuf_d, size,
1637 DMA_FROM_DEVICE);
1638 ep->rxbuf_d = DMA_ADDR_INVALID;
1639 } else {
1640 dma_sync_single_for_cpu(
1641 udc_controller->gadget.dev.parent,
1642 ep->rxbuf_d, size,
1643 DMA_FROM_DEVICE);
1645 kfree(ep->rxbuffer);
1648 if (ep->dir != USB_DIR_OUT)
1649 kfree(ep->txframe);
1651 dev_dbg(udc->dev, "disabled %s OK\n", _ep->name);
1652 return 0;
1655 static struct usb_request *qe_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
1657 struct qe_req *req;
1659 req = kzalloc(sizeof(*req), gfp_flags);
1660 if (!req)
1661 return NULL;
1663 req->req.dma = DMA_ADDR_INVALID;
1665 INIT_LIST_HEAD(&req->queue);
1667 return &req->req;
1670 static void qe_free_request(struct usb_ep *_ep, struct usb_request *_req)
1672 struct qe_req *req;
1674 req = container_of(_req, struct qe_req, req);
1676 if (_req)
1677 kfree(req);
1680 /* queues (submits) an I/O request to an endpoint */
1681 static int qe_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1682 gfp_t gfp_flags)
1684 struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1685 struct qe_req *req = container_of(_req, struct qe_req, req);
1686 struct qe_udc *udc;
1687 unsigned long flags;
1688 int reval;
1690 udc = ep->udc;
1691 /* catch various bogus parameters */
1692 if (!_req || !req->req.complete || !req->req.buf
1693 || !list_empty(&req->queue)) {
1694 dev_dbg(udc->dev, "bad params\n");
1695 return -EINVAL;
1697 if (!_ep || (!ep->desc && ep_index(ep))) {
1698 dev_dbg(udc->dev, "bad ep\n");
1699 return -EINVAL;
1702 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1703 return -ESHUTDOWN;
1705 req->ep = ep;
1707 /* map virtual address to hardware */
1708 if (req->req.dma == DMA_ADDR_INVALID) {
1709 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1710 req->req.buf,
1711 req->req.length,
1712 ep_is_in(ep)
1713 ? DMA_TO_DEVICE :
1714 DMA_FROM_DEVICE);
1715 req->mapped = 1;
1716 } else {
1717 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
1718 req->req.dma, req->req.length,
1719 ep_is_in(ep)
1720 ? DMA_TO_DEVICE :
1721 DMA_FROM_DEVICE);
1722 req->mapped = 0;
1725 req->req.status = -EINPROGRESS;
1726 req->req.actual = 0;
1728 list_add_tail(&req->queue, &ep->queue);
1729 dev_vdbg(udc->dev, "gadget have request in %s! %d\n",
1730 ep->name, req->req.length);
1731 spin_lock_irqsave(&udc->lock, flags);
1732 /* push the request to device */
1733 if (ep_is_in(ep))
1734 reval = ep_req_send(ep, req);
1736 /* EP0 */
1737 if (ep_index(ep) == 0 && req->req.length > 0) {
1738 if (ep_is_in(ep))
1739 udc->ep0_state = DATA_STATE_XMIT;
1740 else
1741 udc->ep0_state = DATA_STATE_RECV;
1744 if (ep->dir == USB_DIR_OUT)
1745 reval = ep_req_receive(ep, req);
1747 spin_unlock_irqrestore(&udc->lock, flags);
1749 return 0;
1752 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1753 static int qe_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1755 struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1756 struct qe_req *req;
1757 unsigned long flags;
1759 if (!_ep || !_req)
1760 return -EINVAL;
1762 spin_lock_irqsave(&ep->udc->lock, flags);
1764 /* make sure it's actually queued on this endpoint */
1765 list_for_each_entry(req, &ep->queue, queue) {
1766 if (&req->req == _req)
1767 break;
1770 if (&req->req != _req) {
1771 spin_unlock_irqrestore(&ep->udc->lock, flags);
1772 return -EINVAL;
1775 done(ep, req, -ECONNRESET);
1777 spin_unlock_irqrestore(&ep->udc->lock, flags);
1778 return 0;
1781 /*-----------------------------------------------------------------
1782 * modify the endpoint halt feature
1783 * @ep: the non-isochronous endpoint being stalled
1784 * @value: 1--set halt 0--clear halt
1785 * Returns zero, or a negative error code.
1786 *----------------------------------------------------------------*/
1787 static int qe_ep_set_halt(struct usb_ep *_ep, int value)
1789 struct qe_ep *ep;
1790 unsigned long flags;
1791 int status = -EOPNOTSUPP;
1792 struct qe_udc *udc;
1794 ep = container_of(_ep, struct qe_ep, ep);
1795 if (!_ep || !ep->desc) {
1796 status = -EINVAL;
1797 goto out;
1800 udc = ep->udc;
1801 /* Attempt to halt IN ep will fail if any transfer requests
1802 * are still queue */
1803 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1804 status = -EAGAIN;
1805 goto out;
1808 status = 0;
1809 spin_lock_irqsave(&ep->udc->lock, flags);
1810 qe_eptx_stall_change(ep, value);
1811 qe_eprx_stall_change(ep, value);
1812 spin_unlock_irqrestore(&ep->udc->lock, flags);
1814 if (ep->epnum == 0) {
1815 udc->ep0_state = WAIT_FOR_SETUP;
1816 udc->ep0_dir = 0;
1819 /* set data toggle to DATA0 on clear halt */
1820 if (value == 0)
1821 ep->data01 = 0;
1822 out:
1823 dev_vdbg(udc->dev, "%s %s halt stat %d\n", ep->ep.name,
1824 value ? "set" : "clear", status);
1826 return status;
1829 static struct usb_ep_ops qe_ep_ops = {
1830 .enable = qe_ep_enable,
1831 .disable = qe_ep_disable,
1833 .alloc_request = qe_alloc_request,
1834 .free_request = qe_free_request,
1836 .queue = qe_ep_queue,
1837 .dequeue = qe_ep_dequeue,
1839 .set_halt = qe_ep_set_halt,
1842 /*------------------------------------------------------------------------
1843 Gadget Driver Layer Operations
1844 ------------------------------------------------------------------------*/
1846 /* Get the current frame number */
1847 static int qe_get_frame(struct usb_gadget *gadget)
1849 u16 tmp;
1851 tmp = in_be16(&udc_controller->usb_param->frame_n);
1852 if (tmp & 0x8000)
1853 tmp = tmp & 0x07ff;
1854 else
1855 tmp = -EINVAL;
1857 return (int)tmp;
1860 /* Tries to wake up the host connected to this gadget
1862 * Return : 0-success
1863 * Negative-this feature not enabled by host or not supported by device hw
1865 static int qe_wakeup(struct usb_gadget *gadget)
1867 return -ENOTSUPP;
1870 /* Notify controller that VBUS is powered, Called by whatever
1871 detects VBUS sessions */
1872 static int qe_vbus_session(struct usb_gadget *gadget, int is_active)
1874 return -ENOTSUPP;
1877 /* constrain controller's VBUS power usage
1878 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1879 * reporting how much power the device may consume. For example, this
1880 * could affect how quickly batteries are recharged.
1882 * Returns zero on success, else negative errno.
1884 static int qe_vbus_draw(struct usb_gadget *gadget, unsigned mA)
1886 return -ENOTSUPP;
1889 /* Change Data+ pullup status
1890 * this func is used by usb_gadget_connect/disconnect
1892 static int qe_pullup(struct usb_gadget *gadget, int is_on)
1894 return -ENOTSUPP;
1897 /* defined in usb_gadget.h */
1898 static struct usb_gadget_ops qe_gadget_ops = {
1899 .get_frame = qe_get_frame,
1900 .wakeup = qe_wakeup,
1901 /* .set_selfpowered = qe_set_selfpowered,*/ /* always selfpowered */
1902 .vbus_session = qe_vbus_session,
1903 .vbus_draw = qe_vbus_draw,
1904 .pullup = qe_pullup,
1907 /*-------------------------------------------------------------------------
1908 USB ep0 Setup process in BUS Enumeration
1909 -------------------------------------------------------------------------*/
1910 static int udc_reset_ep_queue(struct qe_udc *udc, u8 pipe)
1912 struct qe_ep *ep = &udc->eps[pipe];
1914 nuke(ep, -ECONNRESET);
1915 ep->tx_req = NULL;
1916 return 0;
1919 static int reset_queues(struct qe_udc *udc)
1921 u8 pipe;
1923 for (pipe = 0; pipe < USB_MAX_ENDPOINTS; pipe++)
1924 udc_reset_ep_queue(udc, pipe);
1926 /* report disconnect; the driver is already quiesced */
1927 spin_unlock(&udc->lock);
1928 udc->driver->disconnect(&udc->gadget);
1929 spin_lock(&udc->lock);
1931 return 0;
1934 static void ch9setaddress(struct qe_udc *udc, u16 value, u16 index,
1935 u16 length)
1937 /* Save the new address to device struct */
1938 udc->device_address = (u8) value;
1939 /* Update usb state */
1940 udc->usb_state = USB_STATE_ADDRESS;
1942 /* Status phase , send a ZLP */
1943 if (ep0_prime_status(udc, USB_DIR_IN))
1944 qe_ep0_stall(udc);
1947 static void ownercomplete(struct usb_ep *_ep, struct usb_request *_req)
1949 struct qe_req *req = container_of(_req, struct qe_req, req);
1951 req->req.buf = NULL;
1952 kfree(req);
1955 static void ch9getstatus(struct qe_udc *udc, u8 request_type, u16 value,
1956 u16 index, u16 length)
1958 u16 usb_status = 0;
1959 struct qe_req *req;
1960 struct qe_ep *ep;
1961 int status = 0;
1963 ep = &udc->eps[0];
1964 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1965 /* Get device status */
1966 usb_status = 1 << USB_DEVICE_SELF_POWERED;
1967 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1968 /* Get interface status */
1969 /* We don't have interface information in udc driver */
1970 usb_status = 0;
1971 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1972 /* Get endpoint status */
1973 int pipe = index & USB_ENDPOINT_NUMBER_MASK;
1974 struct qe_ep *target_ep = &udc->eps[pipe];
1975 u16 usep;
1977 /* stall if endpoint doesn't exist */
1978 if (!target_ep->desc)
1979 goto stall;
1981 usep = in_be16(&udc->usb_regs->usb_usep[pipe]);
1982 if (index & USB_DIR_IN) {
1983 if (target_ep->dir != USB_DIR_IN)
1984 goto stall;
1985 if ((usep & USB_THS_MASK) == USB_THS_STALL)
1986 usb_status = 1 << USB_ENDPOINT_HALT;
1987 } else {
1988 if (target_ep->dir != USB_DIR_OUT)
1989 goto stall;
1990 if ((usep & USB_RHS_MASK) == USB_RHS_STALL)
1991 usb_status = 1 << USB_ENDPOINT_HALT;
1995 req = container_of(qe_alloc_request(&ep->ep, GFP_KERNEL),
1996 struct qe_req, req);
1997 req->req.length = 2;
1998 req->req.buf = udc->statusbuf;
1999 *(u16 *)req->req.buf = cpu_to_le16(usb_status);
2000 req->req.status = -EINPROGRESS;
2001 req->req.actual = 0;
2002 req->req.complete = ownercomplete;
2004 udc->ep0_dir = USB_DIR_IN;
2006 /* data phase */
2007 status = qe_ep_queue(&ep->ep, &req->req, GFP_ATOMIC);
2009 if (status == 0)
2010 return;
2011 stall:
2012 dev_err(udc->dev, "Can't respond to getstatus request \n");
2013 qe_ep0_stall(udc);
2016 /* only handle the setup request, suppose the device in normal status */
2017 static void setup_received_handle(struct qe_udc *udc,
2018 struct usb_ctrlrequest *setup)
2020 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2021 u16 wValue = le16_to_cpu(setup->wValue);
2022 u16 wIndex = le16_to_cpu(setup->wIndex);
2023 u16 wLength = le16_to_cpu(setup->wLength);
2025 /* clear the previous request in the ep0 */
2026 udc_reset_ep_queue(udc, 0);
2028 if (setup->bRequestType & USB_DIR_IN)
2029 udc->ep0_dir = USB_DIR_IN;
2030 else
2031 udc->ep0_dir = USB_DIR_OUT;
2033 switch (setup->bRequest) {
2034 case USB_REQ_GET_STATUS:
2035 /* Data+Status phase form udc */
2036 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
2037 != (USB_DIR_IN | USB_TYPE_STANDARD))
2038 break;
2039 ch9getstatus(udc, setup->bRequestType, wValue, wIndex,
2040 wLength);
2041 return;
2043 case USB_REQ_SET_ADDRESS:
2044 /* Status phase from udc */
2045 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
2046 USB_RECIP_DEVICE))
2047 break;
2048 ch9setaddress(udc, wValue, wIndex, wLength);
2049 return;
2051 case USB_REQ_CLEAR_FEATURE:
2052 case USB_REQ_SET_FEATURE:
2053 /* Requests with no data phase, status phase from udc */
2054 if ((setup->bRequestType & USB_TYPE_MASK)
2055 != USB_TYPE_STANDARD)
2056 break;
2058 if ((setup->bRequestType & USB_RECIP_MASK)
2059 == USB_RECIP_ENDPOINT) {
2060 int pipe = wIndex & USB_ENDPOINT_NUMBER_MASK;
2061 struct qe_ep *ep;
2063 if (wValue != 0 || wLength != 0
2064 || pipe > USB_MAX_ENDPOINTS)
2065 break;
2066 ep = &udc->eps[pipe];
2068 spin_unlock(&udc->lock);
2069 qe_ep_set_halt(&ep->ep,
2070 (setup->bRequest == USB_REQ_SET_FEATURE)
2071 ? 1 : 0);
2072 spin_lock(&udc->lock);
2075 ep0_prime_status(udc, USB_DIR_IN);
2077 return;
2079 default:
2080 break;
2083 if (wLength) {
2084 /* Data phase from gadget, status phase from udc */
2085 if (setup->bRequestType & USB_DIR_IN) {
2086 udc->ep0_state = DATA_STATE_XMIT;
2087 udc->ep0_dir = USB_DIR_IN;
2088 } else {
2089 udc->ep0_state = DATA_STATE_RECV;
2090 udc->ep0_dir = USB_DIR_OUT;
2092 spin_unlock(&udc->lock);
2093 if (udc->driver->setup(&udc->gadget,
2094 &udc->local_setup_buff) < 0)
2095 qe_ep0_stall(udc);
2096 spin_lock(&udc->lock);
2097 } else {
2098 /* No data phase, IN status from gadget */
2099 udc->ep0_dir = USB_DIR_IN;
2100 spin_unlock(&udc->lock);
2101 if (udc->driver->setup(&udc->gadget,
2102 &udc->local_setup_buff) < 0)
2103 qe_ep0_stall(udc);
2104 spin_lock(&udc->lock);
2105 udc->ep0_state = DATA_STATE_NEED_ZLP;
2109 /*-------------------------------------------------------------------------
2110 USB Interrupt handlers
2111 -------------------------------------------------------------------------*/
2112 static void suspend_irq(struct qe_udc *udc)
2114 udc->resume_state = udc->usb_state;
2115 udc->usb_state = USB_STATE_SUSPENDED;
2117 /* report suspend to the driver ,serial.c not support this*/
2118 if (udc->driver->suspend)
2119 udc->driver->suspend(&udc->gadget);
2122 static void resume_irq(struct qe_udc *udc)
2124 udc->usb_state = udc->resume_state;
2125 udc->resume_state = 0;
2127 /* report resume to the driver , serial.c not support this*/
2128 if (udc->driver->resume)
2129 udc->driver->resume(&udc->gadget);
2132 static void idle_irq(struct qe_udc *udc)
2134 u8 usbs;
2136 usbs = in_8(&udc->usb_regs->usb_usbs);
2137 if (usbs & USB_IDLE_STATUS_MASK) {
2138 if ((udc->usb_state) != USB_STATE_SUSPENDED)
2139 suspend_irq(udc);
2140 } else {
2141 if (udc->usb_state == USB_STATE_SUSPENDED)
2142 resume_irq(udc);
2146 static int reset_irq(struct qe_udc *udc)
2148 unsigned char i;
2150 qe_usb_disable();
2151 out_8(&udc->usb_regs->usb_usadr, 0);
2153 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2154 if (udc->eps[i].init)
2155 qe_ep_reset(udc, i);
2158 reset_queues(udc);
2159 udc->usb_state = USB_STATE_DEFAULT;
2160 udc->ep0_state = WAIT_FOR_SETUP;
2161 udc->ep0_dir = USB_DIR_OUT;
2162 qe_usb_enable();
2163 return 0;
2166 static int bsy_irq(struct qe_udc *udc)
2168 return 0;
2171 static int txe_irq(struct qe_udc *udc)
2173 return 0;
2176 /* ep0 tx interrupt also in here */
2177 static int tx_irq(struct qe_udc *udc)
2179 struct qe_ep *ep;
2180 struct qe_bd __iomem *bd;
2181 int i, res = 0;
2183 if ((udc->usb_state == USB_STATE_ADDRESS)
2184 && (in_8(&udc->usb_regs->usb_usadr) == 0))
2185 out_8(&udc->usb_regs->usb_usadr, udc->device_address);
2187 for (i = (USB_MAX_ENDPOINTS-1); ((i >= 0) && (res == 0)); i--) {
2188 ep = &udc->eps[i];
2189 if (ep && ep->init && (ep->dir != USB_DIR_OUT)) {
2190 bd = ep->c_txbd;
2191 if (!(in_be32((u32 __iomem *)bd) & T_R)
2192 && (in_be32(&bd->buf))) {
2193 /* confirm the transmitted bd */
2194 if (ep->epnum == 0)
2195 res = qe_ep0_txconf(ep);
2196 else
2197 res = qe_ep_txconf(ep);
2201 return res;
2205 /* setup packect's rx is handle in the function too */
2206 static void rx_irq(struct qe_udc *udc)
2208 struct qe_ep *ep;
2209 struct qe_bd __iomem *bd;
2210 int i;
2212 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2213 ep = &udc->eps[i];
2214 if (ep && ep->init && (ep->dir != USB_DIR_IN)) {
2215 bd = ep->n_rxbd;
2216 if (!(in_be32((u32 __iomem *)bd) & R_E)
2217 && (in_be32(&bd->buf))) {
2218 if (ep->epnum == 0) {
2219 qe_ep0_rx(udc);
2220 } else {
2221 /*non-setup package receive*/
2222 qe_ep_rx(ep);
2229 static irqreturn_t qe_udc_irq(int irq, void *_udc)
2231 struct qe_udc *udc = (struct qe_udc *)_udc;
2232 u16 irq_src;
2233 irqreturn_t status = IRQ_NONE;
2234 unsigned long flags;
2236 spin_lock_irqsave(&udc->lock, flags);
2238 irq_src = in_be16(&udc->usb_regs->usb_usber) &
2239 in_be16(&udc->usb_regs->usb_usbmr);
2240 /* Clear notification bits */
2241 out_be16(&udc->usb_regs->usb_usber, irq_src);
2242 /* USB Interrupt */
2243 if (irq_src & USB_E_IDLE_MASK) {
2244 idle_irq(udc);
2245 irq_src &= ~USB_E_IDLE_MASK;
2246 status = IRQ_HANDLED;
2249 if (irq_src & USB_E_TXB_MASK) {
2250 tx_irq(udc);
2251 irq_src &= ~USB_E_TXB_MASK;
2252 status = IRQ_HANDLED;
2255 if (irq_src & USB_E_RXB_MASK) {
2256 rx_irq(udc);
2257 irq_src &= ~USB_E_RXB_MASK;
2258 status = IRQ_HANDLED;
2261 if (irq_src & USB_E_RESET_MASK) {
2262 reset_irq(udc);
2263 irq_src &= ~USB_E_RESET_MASK;
2264 status = IRQ_HANDLED;
2267 if (irq_src & USB_E_BSY_MASK) {
2268 bsy_irq(udc);
2269 irq_src &= ~USB_E_BSY_MASK;
2270 status = IRQ_HANDLED;
2273 if (irq_src & USB_E_TXE_MASK) {
2274 txe_irq(udc);
2275 irq_src &= ~USB_E_TXE_MASK;
2276 status = IRQ_HANDLED;
2279 spin_unlock_irqrestore(&udc->lock, flags);
2281 return status;
2284 /*-------------------------------------------------------------------------
2285 Gadget driver register and unregister.
2286 --------------------------------------------------------------------------*/
2287 int usb_gadget_register_driver(struct usb_gadget_driver *driver)
2289 int retval;
2290 unsigned long flags = 0;
2292 /* standard operations */
2293 if (!udc_controller)
2294 return -ENODEV;
2296 if (!driver || (driver->speed != USB_SPEED_FULL
2297 && driver->speed != USB_SPEED_HIGH)
2298 || !driver->bind || !driver->disconnect
2299 || !driver->setup)
2300 return -EINVAL;
2302 if (udc_controller->driver)
2303 return -EBUSY;
2305 /* lock is needed but whether should use this lock or another */
2306 spin_lock_irqsave(&udc_controller->lock, flags);
2308 driver->driver.bus = NULL;
2309 /* hook up the driver */
2310 udc_controller->driver = driver;
2311 udc_controller->gadget.dev.driver = &driver->driver;
2312 udc_controller->gadget.speed = (enum usb_device_speed)(driver->speed);
2313 spin_unlock_irqrestore(&udc_controller->lock, flags);
2315 retval = driver->bind(&udc_controller->gadget);
2316 if (retval) {
2317 dev_err(udc_controller->dev, "bind to %s --> %d",
2318 driver->driver.name, retval);
2319 udc_controller->gadget.dev.driver = NULL;
2320 udc_controller->driver = NULL;
2321 return retval;
2324 /* Enable IRQ reg and Set usbcmd reg EN bit */
2325 qe_usb_enable();
2327 out_be16(&udc_controller->usb_regs->usb_usber, 0xffff);
2328 out_be16(&udc_controller->usb_regs->usb_usbmr, USB_E_DEFAULT_DEVICE);
2329 udc_controller->usb_state = USB_STATE_ATTACHED;
2330 udc_controller->ep0_state = WAIT_FOR_SETUP;
2331 udc_controller->ep0_dir = USB_DIR_OUT;
2332 dev_info(udc_controller->dev, "%s bind to driver %s \n",
2333 udc_controller->gadget.name, driver->driver.name);
2334 return 0;
2336 EXPORT_SYMBOL(usb_gadget_register_driver);
2338 int usb_gadget_unregister_driver(struct usb_gadget_driver *driver)
2340 struct qe_ep *loop_ep;
2341 unsigned long flags;
2343 if (!udc_controller)
2344 return -ENODEV;
2346 if (!driver || driver != udc_controller->driver)
2347 return -EINVAL;
2349 /* stop usb controller, disable intr */
2350 qe_usb_disable();
2352 /* in fact, no needed */
2353 udc_controller->usb_state = USB_STATE_ATTACHED;
2354 udc_controller->ep0_state = WAIT_FOR_SETUP;
2355 udc_controller->ep0_dir = 0;
2357 /* stand operation */
2358 spin_lock_irqsave(&udc_controller->lock, flags);
2359 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2360 nuke(&udc_controller->eps[0], -ESHUTDOWN);
2361 list_for_each_entry(loop_ep, &udc_controller->gadget.ep_list,
2362 ep.ep_list)
2363 nuke(loop_ep, -ESHUTDOWN);
2364 spin_unlock_irqrestore(&udc_controller->lock, flags);
2366 /* unbind gadget and unhook driver. */
2367 driver->unbind(&udc_controller->gadget);
2368 udc_controller->gadget.dev.driver = NULL;
2369 udc_controller->driver = NULL;
2371 dev_info(udc_controller->dev, "unregistered gadget driver '%s'\r\n",
2372 driver->driver.name);
2373 return 0;
2375 EXPORT_SYMBOL(usb_gadget_unregister_driver);
2377 /* udc structure's alloc and setup, include ep-param alloc */
2378 static struct qe_udc __devinit *qe_udc_config(struct of_device *ofdev)
2380 struct qe_udc *udc;
2381 struct device_node *np = ofdev->node;
2382 unsigned int tmp_addr = 0;
2383 struct usb_device_para __iomem *usbpram;
2384 unsigned int i;
2385 u64 size;
2386 u32 offset;
2388 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2389 if (udc == NULL) {
2390 dev_err(&ofdev->dev, "malloc udc failed\n");
2391 goto cleanup;
2394 udc->dev = &ofdev->dev;
2396 /* get default address of usb parameter in MURAM from device tree */
2397 offset = *of_get_address(np, 1, &size, NULL);
2398 udc->usb_param = cpm_muram_addr(offset);
2399 memset_io(udc->usb_param, 0, size);
2401 usbpram = udc->usb_param;
2402 out_be16(&usbpram->frame_n, 0);
2403 out_be32(&usbpram->rstate, 0);
2405 tmp_addr = cpm_muram_alloc((USB_MAX_ENDPOINTS *
2406 sizeof(struct usb_ep_para)),
2407 USB_EP_PARA_ALIGNMENT);
2409 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2410 out_be16(&usbpram->epptr[i], (u16)tmp_addr);
2411 udc->ep_param[i] = cpm_muram_addr(tmp_addr);
2412 tmp_addr += 32;
2415 memset_io(udc->ep_param[0], 0,
2416 USB_MAX_ENDPOINTS * sizeof(struct usb_ep_para));
2418 udc->resume_state = USB_STATE_NOTATTACHED;
2419 udc->usb_state = USB_STATE_POWERED;
2420 udc->ep0_dir = 0;
2422 spin_lock_init(&udc->lock);
2423 return udc;
2425 cleanup:
2426 kfree(udc);
2427 return NULL;
2430 /* USB Controller register init */
2431 static int __devinit qe_udc_reg_init(struct qe_udc *udc)
2433 struct usb_ctlr __iomem *qe_usbregs;
2434 qe_usbregs = udc->usb_regs;
2436 /* Init the usb register */
2437 out_8(&qe_usbregs->usb_usmod, 0x01);
2438 out_be16(&qe_usbregs->usb_usbmr, 0);
2439 out_8(&qe_usbregs->usb_uscom, 0);
2440 out_be16(&qe_usbregs->usb_usber, USBER_ALL_CLEAR);
2442 return 0;
2445 static int __devinit qe_ep_config(struct qe_udc *udc, unsigned char pipe_num)
2447 struct qe_ep *ep = &udc->eps[pipe_num];
2449 ep->udc = udc;
2450 strcpy(ep->name, ep_name[pipe_num]);
2451 ep->ep.name = ep_name[pipe_num];
2453 ep->ep.ops = &qe_ep_ops;
2454 ep->stopped = 1;
2455 ep->ep.maxpacket = (unsigned short) ~0;
2456 ep->desc = NULL;
2457 ep->dir = 0xff;
2458 ep->epnum = (u8)pipe_num;
2459 ep->sent = 0;
2460 ep->last = 0;
2461 ep->init = 0;
2462 ep->rxframe = NULL;
2463 ep->txframe = NULL;
2464 ep->tx_req = NULL;
2465 ep->state = EP_STATE_IDLE;
2466 ep->has_data = 0;
2468 /* the queue lists any req for this ep */
2469 INIT_LIST_HEAD(&ep->queue);
2471 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2472 if (pipe_num != 0)
2473 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2475 ep->gadget = &udc->gadget;
2477 return 0;
2480 /*-----------------------------------------------------------------------
2481 * UDC device Driver operation functions *
2482 *----------------------------------------------------------------------*/
2483 static void qe_udc_release(struct device *dev)
2485 int i = 0;
2487 complete(udc_controller->done);
2488 cpm_muram_free(cpm_muram_offset(udc_controller->ep_param[0]));
2489 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
2490 udc_controller->ep_param[i] = NULL;
2492 kfree(udc_controller);
2493 udc_controller = NULL;
2496 /* Driver probe functions */
2497 static int __devinit qe_udc_probe(struct of_device *ofdev,
2498 const struct of_device_id *match)
2500 struct device_node *np = ofdev->node;
2501 struct qe_ep *ep;
2502 unsigned int ret = 0;
2503 unsigned int i;
2504 const void *prop;
2506 prop = of_get_property(np, "mode", NULL);
2507 if (!prop || strcmp(prop, "peripheral"))
2508 return -ENODEV;
2510 /* Initialize the udc structure including QH member and other member */
2511 udc_controller = qe_udc_config(ofdev);
2512 if (!udc_controller) {
2513 dev_dbg(&ofdev->dev, "udc_controll is NULL\n");
2514 return -ENOMEM;
2517 udc_controller->soc_type = (unsigned long)match->data;
2518 udc_controller->usb_regs = of_iomap(np, 0);
2519 if (!udc_controller->usb_regs) {
2520 ret = -ENOMEM;
2521 goto err1;
2524 /* initialize usb hw reg except for regs for EP,
2525 * leave usbintr reg untouched*/
2526 qe_udc_reg_init(udc_controller);
2528 /* here comes the stand operations for probe
2529 * set the qe_udc->gadget.xxx */
2530 udc_controller->gadget.ops = &qe_gadget_ops;
2532 /* gadget.ep0 is a pointer */
2533 udc_controller->gadget.ep0 = &udc_controller->eps[0].ep;
2535 INIT_LIST_HEAD(&udc_controller->gadget.ep_list);
2537 /* modify in register gadget process */
2538 udc_controller->gadget.speed = USB_SPEED_UNKNOWN;
2540 /* name: Identifies the controller hardware type. */
2541 udc_controller->gadget.name = driver_name;
2543 device_initialize(&udc_controller->gadget.dev);
2545 strcpy(udc_controller->gadget.dev.bus_id, "gadget");
2547 udc_controller->gadget.dev.release = qe_udc_release;
2548 udc_controller->gadget.dev.parent = &ofdev->dev;
2550 /* initialize qe_ep struct */
2551 for (i = 0; i < USB_MAX_ENDPOINTS ; i++) {
2552 /* because the ep type isn't decide here so
2553 * qe_ep_init() should be called in ep_enable() */
2555 /* setup the qe_ep struct and link ep.ep.list
2556 * into gadget.ep_list */
2557 qe_ep_config(udc_controller, (unsigned char)i);
2560 /* ep0 initialization in here */
2561 ret = qe_ep_init(udc_controller, 0, &qe_ep0_desc);
2562 if (ret)
2563 goto err2;
2565 /* create a buf for ZLP send, need to remain zeroed */
2566 udc_controller->nullbuf = kzalloc(256, GFP_KERNEL);
2567 if (udc_controller->nullbuf == NULL) {
2568 dev_dbg(udc_controller->dev, "cannot alloc nullbuf\n");
2569 ret = -ENOMEM;
2570 goto err3;
2573 /* buffer for data of get_status request */
2574 udc_controller->statusbuf = kzalloc(2, GFP_KERNEL);
2575 if (udc_controller->statusbuf == NULL) {
2576 ret = -ENOMEM;
2577 goto err4;
2580 udc_controller->nullp = virt_to_phys((void *)udc_controller->nullbuf);
2581 if (udc_controller->nullp == DMA_ADDR_INVALID) {
2582 udc_controller->nullp = dma_map_single(
2583 udc_controller->gadget.dev.parent,
2584 udc_controller->nullbuf,
2585 256,
2586 DMA_TO_DEVICE);
2587 udc_controller->nullmap = 1;
2588 } else {
2589 dma_sync_single_for_device(udc_controller->gadget.dev.parent,
2590 udc_controller->nullp, 256,
2591 DMA_TO_DEVICE);
2594 tasklet_init(&udc_controller->rx_tasklet, ep_rx_tasklet,
2595 (unsigned long)udc_controller);
2596 /* request irq and disable DR */
2597 udc_controller->usb_irq = irq_of_parse_and_map(np, 0);
2599 ret = request_irq(udc_controller->usb_irq, qe_udc_irq, 0,
2600 driver_name, udc_controller);
2601 if (ret) {
2602 dev_err(udc_controller->dev, "cannot request irq %d err %d \n",
2603 udc_controller->usb_irq, ret);
2604 goto err5;
2607 ret = device_add(&udc_controller->gadget.dev);
2608 if (ret)
2609 goto err6;
2611 dev_info(udc_controller->dev,
2612 "%s USB controller initialized as device\n",
2613 (udc_controller->soc_type == PORT_QE) ? "QE" : "CPM");
2614 return 0;
2616 err6:
2617 free_irq(udc_controller->usb_irq, udc_controller);
2618 err5:
2619 if (udc_controller->nullmap) {
2620 dma_unmap_single(udc_controller->gadget.dev.parent,
2621 udc_controller->nullp, 256,
2622 DMA_TO_DEVICE);
2623 udc_controller->nullp = DMA_ADDR_INVALID;
2624 } else {
2625 dma_sync_single_for_cpu(udc_controller->gadget.dev.parent,
2626 udc_controller->nullp, 256,
2627 DMA_TO_DEVICE);
2629 kfree(udc_controller->statusbuf);
2630 err4:
2631 kfree(udc_controller->nullbuf);
2632 err3:
2633 ep = &udc_controller->eps[0];
2634 cpm_muram_free(cpm_muram_offset(ep->rxbase));
2635 kfree(ep->rxframe);
2636 kfree(ep->rxbuffer);
2637 kfree(ep->txframe);
2638 err2:
2639 iounmap(udc_controller->usb_regs);
2640 err1:
2641 kfree(udc_controller);
2643 return ret;
2646 #ifdef CONFIG_PM
2647 static int qe_udc_suspend(struct of_device *dev, pm_message_t state)
2649 return -ENOTSUPP;
2652 static int qe_udc_resume(struct of_device *dev)
2654 return -ENOTSUPP;
2656 #endif
2658 static int __devexit qe_udc_remove(struct of_device *ofdev)
2660 struct qe_ep *ep;
2661 unsigned int size;
2663 DECLARE_COMPLETION(done);
2665 if (!udc_controller)
2666 return -ENODEV;
2668 udc_controller->done = &done;
2669 tasklet_disable(&udc_controller->rx_tasklet);
2671 if (udc_controller->nullmap) {
2672 dma_unmap_single(udc_controller->gadget.dev.parent,
2673 udc_controller->nullp, 256,
2674 DMA_TO_DEVICE);
2675 udc_controller->nullp = DMA_ADDR_INVALID;
2676 } else {
2677 dma_sync_single_for_cpu(udc_controller->gadget.dev.parent,
2678 udc_controller->nullp, 256,
2679 DMA_TO_DEVICE);
2681 kfree(udc_controller->statusbuf);
2682 kfree(udc_controller->nullbuf);
2684 ep = &udc_controller->eps[0];
2685 cpm_muram_free(cpm_muram_offset(ep->rxbase));
2686 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (USB_BDRING_LEN + 1);
2688 kfree(ep->rxframe);
2689 if (ep->rxbufmap) {
2690 dma_unmap_single(udc_controller->gadget.dev.parent,
2691 ep->rxbuf_d, size,
2692 DMA_FROM_DEVICE);
2693 ep->rxbuf_d = DMA_ADDR_INVALID;
2694 } else {
2695 dma_sync_single_for_cpu(udc_controller->gadget.dev.parent,
2696 ep->rxbuf_d, size,
2697 DMA_FROM_DEVICE);
2700 kfree(ep->rxbuffer);
2701 kfree(ep->txframe);
2703 free_irq(udc_controller->usb_irq, udc_controller);
2705 tasklet_kill(&udc_controller->rx_tasklet);
2707 iounmap(udc_controller->usb_regs);
2709 device_unregister(&udc_controller->gadget.dev);
2710 /* wait for release() of gadget.dev to free udc */
2711 wait_for_completion(&done);
2713 return 0;
2716 /*-------------------------------------------------------------------------*/
2717 static struct of_device_id __devinitdata qe_udc_match[] = {
2719 .compatible = "fsl,mpc8360-qe-usb",
2720 .data = (void *)PORT_QE,
2723 .compatible = "fsl,mpc8272-cpm-usb",
2724 .data = (void *)PORT_CPM,
2729 MODULE_DEVICE_TABLE(of, qe_udc_match);
2731 static struct of_platform_driver udc_driver = {
2732 .name = (char *)driver_name,
2733 .match_table = qe_udc_match,
2734 .probe = qe_udc_probe,
2735 .remove = __devexit_p(qe_udc_remove),
2736 #ifdef CONFIG_PM
2737 .suspend = qe_udc_suspend,
2738 .resume = qe_udc_resume,
2739 #endif
2742 static int __init qe_udc_init(void)
2744 printk(KERN_INFO "%s: %s, %s\n", driver_name, driver_desc,
2745 DRIVER_VERSION);
2746 return of_register_platform_driver(&udc_driver);
2749 static void __exit qe_udc_exit(void)
2751 of_unregister_platform_driver(&udc_driver);
2754 module_init(qe_udc_init);
2755 module_exit(qe_udc_exit);
2757 MODULE_DESCRIPTION(DRIVER_DESC);
2758 MODULE_AUTHOR(DRIVER_AUTHOR);
2759 MODULE_LICENSE("GPL");