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.
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.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/ioport.h>
26 #include <linux/types.h>
27 #include <linux/errno.h>
28 #include <linux/err.h>
29 #include <linux/slab.h>
30 #include <linux/list.h>
31 #include <linux/interrupt.h>
33 #include <linux/moduleparam.h>
34 #include <linux/of_address.h>
35 #include <linux/of_irq.h>
36 #include <linux/of_platform.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/usb/ch9.h>
39 #include <linux/usb/gadget.h>
40 #include <linux/usb/otg.h>
41 #include <soc/fsl/qe/qe.h>
45 #include "fsl_qe_udc.h"
47 #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver"
48 #define DRIVER_AUTHOR "Xie XiaoBo"
49 #define DRIVER_VERSION "1.0"
51 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
53 static const char driver_name[] = "fsl_qe_udc";
54 static const char driver_desc[] = DRIVER_DESC;
56 /*ep name is important in gadget, it should obey the convention of ep_match()*/
57 static const char *const ep_name[] = {
58 "ep0-control", /* everyone has ep0 */
59 /* 3 configurable endpoints */
65 static const struct usb_endpoint_descriptor qe_ep0_desc = {
66 .bLength = USB_DT_ENDPOINT_SIZE,
67 .bDescriptorType = USB_DT_ENDPOINT,
69 .bEndpointAddress = 0,
70 .bmAttributes = USB_ENDPOINT_XFER_CONTROL,
71 .wMaxPacketSize = USB_MAX_CTRL_PAYLOAD,
74 /********************************************************************
75 * Internal Used Function Start
76 ********************************************************************/
77 /*-----------------------------------------------------------------
78 * done() - retire a request; caller blocked irqs
79 *--------------------------------------------------------------*/
80 static void done(struct qe_ep *ep, struct qe_req *req, int status)
82 struct qe_udc *udc = ep->udc;
83 unsigned char stopped = ep->stopped;
85 /* the req->queue pointer is used by ep_queue() func, in which
86 * the request will be added into a udc_ep->queue 'd tail
87 * so here the req will be dropped from the ep->queue
89 list_del_init(&req->queue);
91 /* req.status should be set as -EINPROGRESS in ep_queue() */
92 if (req->req.status == -EINPROGRESS)
93 req->req.status = status;
95 status = req->req.status;
98 dma_unmap_single(udc->gadget.dev.parent,
99 req->req.dma, req->req.length,
103 req->req.dma = DMA_ADDR_INVALID;
106 dma_sync_single_for_cpu(udc->gadget.dev.parent,
107 req->req.dma, req->req.length,
112 if (status && (status != -ESHUTDOWN))
113 dev_vdbg(udc->dev, "complete %s req %p stat %d len %u/%u\n",
114 ep->ep.name, &req->req, status,
115 req->req.actual, req->req.length);
117 /* don't modify queue heads during completion callback */
119 spin_unlock(&udc->lock);
121 usb_gadget_giveback_request(&ep->ep, &req->req);
123 spin_lock(&udc->lock);
125 ep->stopped = stopped;
128 /*-----------------------------------------------------------------
129 * nuke(): delete all requests related to this ep
130 *--------------------------------------------------------------*/
131 static void nuke(struct qe_ep *ep, int status)
133 /* Whether this eq has request linked */
134 while (!list_empty(&ep->queue)) {
135 struct qe_req *req = NULL;
136 req = list_entry(ep->queue.next, struct qe_req, queue);
138 done(ep, req, status);
142 /*---------------------------------------------------------------------------*
143 * USB and Endpoint manipulate process, include parameter and register *
144 *---------------------------------------------------------------------------*/
145 /* @value: 1--set stall 0--clean stall */
146 static int qe_eprx_stall_change(struct qe_ep *ep, int value)
149 u8 epnum = ep->epnum;
150 struct qe_udc *udc = ep->udc;
152 tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]);
153 tem_usep = tem_usep & ~USB_RHS_MASK;
155 tem_usep |= USB_RHS_STALL;
156 else if (ep->dir == USB_DIR_IN)
157 tem_usep |= USB_RHS_IGNORE_OUT;
159 out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep);
163 static int qe_eptx_stall_change(struct qe_ep *ep, int value)
166 u8 epnum = ep->epnum;
167 struct qe_udc *udc = ep->udc;
169 tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]);
170 tem_usep = tem_usep & ~USB_THS_MASK;
172 tem_usep |= USB_THS_STALL;
173 else if (ep->dir == USB_DIR_OUT)
174 tem_usep |= USB_THS_IGNORE_IN;
176 out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep);
181 static int qe_ep0_stall(struct qe_udc *udc)
183 qe_eptx_stall_change(&udc->eps[0], 1);
184 qe_eprx_stall_change(&udc->eps[0], 1);
185 udc->ep0_state = WAIT_FOR_SETUP;
190 static int qe_eprx_nack(struct qe_ep *ep)
192 u8 epnum = ep->epnum;
193 struct qe_udc *udc = ep->udc;
195 if (ep->state == EP_STATE_IDLE) {
196 /* Set the ep's nack */
197 clrsetbits_be16(&udc->usb_regs->usb_usep[epnum],
198 USB_RHS_MASK, USB_RHS_NACK);
200 /* Mask Rx and Busy interrupts */
201 clrbits16(&udc->usb_regs->usb_usbmr,
202 (USB_E_RXB_MASK | USB_E_BSY_MASK));
204 ep->state = EP_STATE_NACK;
209 static int qe_eprx_normal(struct qe_ep *ep)
211 struct qe_udc *udc = ep->udc;
213 if (ep->state == EP_STATE_NACK) {
214 clrsetbits_be16(&udc->usb_regs->usb_usep[ep->epnum],
215 USB_RTHS_MASK, USB_THS_IGNORE_IN);
217 /* Unmask RX interrupts */
218 out_be16(&udc->usb_regs->usb_usber,
219 USB_E_BSY_MASK | USB_E_RXB_MASK);
220 setbits16(&udc->usb_regs->usb_usbmr,
221 (USB_E_RXB_MASK | USB_E_BSY_MASK));
223 ep->state = EP_STATE_IDLE;
230 static int qe_ep_cmd_stoptx(struct qe_ep *ep)
232 if (ep->udc->soc_type == PORT_CPM)
233 cpm_command(CPM_USB_STOP_TX | (ep->epnum << CPM_USB_EP_SHIFT),
234 CPM_USB_STOP_TX_OPCODE);
236 qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB,
242 static int qe_ep_cmd_restarttx(struct qe_ep *ep)
244 if (ep->udc->soc_type == PORT_CPM)
245 cpm_command(CPM_USB_RESTART_TX | (ep->epnum <<
246 CPM_USB_EP_SHIFT), CPM_USB_RESTART_TX_OPCODE);
248 qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB,
254 static int qe_ep_flushtxfifo(struct qe_ep *ep)
256 struct qe_udc *udc = ep->udc;
261 qe_ep_cmd_stoptx(ep);
262 out_8(&udc->usb_regs->usb_uscom,
263 USB_CMD_FLUSH_FIFO | (USB_CMD_EP_MASK & (ep->epnum)));
264 out_be16(&udc->ep_param[i]->tbptr, in_be16(&udc->ep_param[i]->tbase));
265 out_be32(&udc->ep_param[i]->tstate, 0);
266 out_be16(&udc->ep_param[i]->tbcnt, 0);
268 ep->c_txbd = ep->txbase;
269 ep->n_txbd = ep->txbase;
270 qe_ep_cmd_restarttx(ep);
274 static int qe_ep_filltxfifo(struct qe_ep *ep)
276 struct qe_udc *udc = ep->udc;
278 out_8(&udc->usb_regs->usb_uscom,
279 USB_CMD_STR_FIFO | (USB_CMD_EP_MASK & (ep->epnum)));
283 static int qe_epbds_reset(struct qe_udc *udc, int pipe_num)
287 struct qe_bd __iomem *bd;
290 ep = &udc->eps[pipe_num];
292 if (ep->dir == USB_DIR_OUT)
293 bdring_len = USB_BDRING_LEN_RX;
295 bdring_len = USB_BDRING_LEN;
298 for (i = 0; i < (bdring_len - 1); i++) {
299 out_be32((u32 __iomem *)bd, R_E | R_I);
302 out_be32((u32 __iomem *)bd, R_E | R_I | R_W);
305 for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) {
306 out_be32(&bd->buf, 0);
307 out_be32((u32 __iomem *)bd, 0);
310 out_be32((u32 __iomem *)bd, T_W);
315 static int qe_ep_reset(struct qe_udc *udc, int pipe_num)
320 ep = &udc->eps[pipe_num];
321 tmpusep = in_be16(&udc->usb_regs->usb_usep[pipe_num]);
322 tmpusep &= ~USB_RTHS_MASK;
326 qe_ep_flushtxfifo(ep);
329 tmpusep |= USB_THS_IGNORE_IN;
332 qe_ep_flushtxfifo(ep);
333 tmpusep |= USB_RHS_IGNORE_OUT;
338 out_be16(&udc->usb_regs->usb_usep[pipe_num], tmpusep);
340 qe_epbds_reset(udc, pipe_num);
345 static int qe_ep_toggledata01(struct qe_ep *ep)
351 static int qe_ep_bd_init(struct qe_udc *udc, unsigned char pipe_num)
353 struct qe_ep *ep = &udc->eps[pipe_num];
354 unsigned long tmp_addr = 0;
355 struct usb_ep_para __iomem *epparam;
357 struct qe_bd __iomem *bd;
360 if (ep->dir == USB_DIR_OUT)
361 bdring_len = USB_BDRING_LEN_RX;
363 bdring_len = USB_BDRING_LEN;
365 epparam = udc->ep_param[pipe_num];
366 /* alloc multi-ram for BD rings and set the ep parameters */
367 tmp_addr = cpm_muram_alloc(sizeof(struct qe_bd) * (bdring_len +
368 USB_BDRING_LEN_TX), QE_ALIGNMENT_OF_BD);
369 if (IS_ERR_VALUE(tmp_addr))
372 out_be16(&epparam->rbase, (u16)tmp_addr);
373 out_be16(&epparam->tbase, (u16)(tmp_addr +
374 (sizeof(struct qe_bd) * bdring_len)));
376 out_be16(&epparam->rbptr, in_be16(&epparam->rbase));
377 out_be16(&epparam->tbptr, in_be16(&epparam->tbase));
379 ep->rxbase = cpm_muram_addr(tmp_addr);
380 ep->txbase = cpm_muram_addr(tmp_addr + (sizeof(struct qe_bd)
382 ep->n_rxbd = ep->rxbase;
383 ep->e_rxbd = ep->rxbase;
384 ep->n_txbd = ep->txbase;
385 ep->c_txbd = ep->txbase;
386 ep->data01 = 0; /* data0 */
388 /* Init TX and RX bds */
390 for (i = 0; i < bdring_len - 1; i++) {
391 out_be32(&bd->buf, 0);
392 out_be32((u32 __iomem *)bd, 0);
395 out_be32(&bd->buf, 0);
396 out_be32((u32 __iomem *)bd, R_W);
399 for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) {
400 out_be32(&bd->buf, 0);
401 out_be32((u32 __iomem *)bd, 0);
404 out_be32(&bd->buf, 0);
405 out_be32((u32 __iomem *)bd, T_W);
410 static int qe_ep_rxbd_update(struct qe_ep *ep)
415 struct qe_bd __iomem *bd;
416 unsigned int bdring_len;
418 if (ep->rxbase == NULL)
423 ep->rxframe = kmalloc(sizeof(*ep->rxframe), GFP_ATOMIC);
427 qe_frame_init(ep->rxframe);
429 if (ep->dir == USB_DIR_OUT)
430 bdring_len = USB_BDRING_LEN_RX;
432 bdring_len = USB_BDRING_LEN;
434 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (bdring_len + 1);
435 ep->rxbuffer = kzalloc(size, GFP_ATOMIC);
441 ep->rxbuf_d = virt_to_phys((void *)ep->rxbuffer);
442 if (ep->rxbuf_d == DMA_ADDR_INVALID) {
443 ep->rxbuf_d = dma_map_single(ep->udc->gadget.dev.parent,
449 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
455 size = ep->ep.maxpacket + USB_CRC_SIZE + 2;
457 tmp = (u32)(((tmp >> 2) << 2) + 4);
459 for (i = 0; i < bdring_len - 1; i++) {
460 out_be32(&bd->buf, tmp);
461 out_be32((u32 __iomem *)bd, (R_E | R_I));
465 out_be32(&bd->buf, tmp);
466 out_be32((u32 __iomem *)bd, (R_E | R_I | R_W));
471 static int qe_ep_register_init(struct qe_udc *udc, unsigned char pipe_num)
473 struct qe_ep *ep = &udc->eps[pipe_num];
474 struct usb_ep_para __iomem *epparam;
479 epparam = udc->ep_param[pipe_num];
482 logepnum = (ep->ep.desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
483 usep |= (logepnum << USB_EPNUM_SHIFT);
485 switch (ep->ep.desc->bmAttributes & 0x03) {
486 case USB_ENDPOINT_XFER_BULK:
487 usep |= USB_TRANS_BULK;
489 case USB_ENDPOINT_XFER_ISOC:
490 usep |= USB_TRANS_ISO;
492 case USB_ENDPOINT_XFER_INT:
493 usep |= USB_TRANS_INT;
496 usep |= USB_TRANS_CTR;
502 usep |= USB_THS_IGNORE_IN;
505 usep |= USB_RHS_IGNORE_OUT;
510 out_be16(&udc->usb_regs->usb_usep[pipe_num], usep);
513 out_8(&epparam->rbmr, rtfcr);
514 out_8(&epparam->tbmr, rtfcr);
516 tmp = (u16)(ep->ep.maxpacket + USB_CRC_SIZE);
517 /* MRBLR must be divisble by 4 */
518 tmp = (u16)(((tmp >> 2) << 2) + 4);
519 out_be16(&epparam->mrblr, tmp);
524 static int qe_ep_init(struct qe_udc *udc,
525 unsigned char pipe_num,
526 const struct usb_endpoint_descriptor *desc)
528 struct qe_ep *ep = &udc->eps[pipe_num];
533 max = usb_endpoint_maxp(desc);
535 /* check the max package size validate for this endpoint */
536 /* Refer to USB2.0 spec table 9-13,
539 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
540 case USB_ENDPOINT_XFER_BULK:
541 if (strstr(ep->ep.name, "-iso")
542 || strstr(ep->ep.name, "-int"))
544 switch (udc->gadget.speed) {
546 if ((max == 128) || (max == 256) || (max == 512))
562 case USB_ENDPOINT_XFER_INT:
563 if (strstr(ep->ep.name, "-iso")) /* bulk is ok */
565 switch (udc->gadget.speed) {
578 case USB_ENDPOINT_XFER_ISOC:
579 if (strstr(ep->ep.name, "-bulk")
580 || strstr(ep->ep.name, "-int"))
582 switch (udc->gadget.speed) {
593 case USB_ENDPOINT_XFER_CONTROL:
594 if (strstr(ep->ep.name, "-iso")
595 || strstr(ep->ep.name, "-int"))
597 switch (udc->gadget.speed) {
632 spin_lock_irqsave(&udc->lock, flags);
634 /* initialize ep structure */
635 ep->ep.maxpacket = max;
636 ep->tm = (u8)(desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
642 ep->dir = USB_DIR_BOTH;
643 udc->ep0_dir = USB_DIR_OUT;
644 udc->ep0_state = WAIT_FOR_SETUP;
646 switch (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
648 ep->dir = USB_DIR_OUT;
651 ep->dir = USB_DIR_IN;
657 /* hardware special operation */
658 qe_ep_bd_init(udc, pipe_num);
659 if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_OUT)) {
660 reval = qe_ep_rxbd_update(ep);
665 if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_IN)) {
666 ep->txframe = kmalloc(sizeof(*ep->txframe), GFP_ATOMIC);
669 qe_frame_init(ep->txframe);
672 qe_ep_register_init(udc, pipe_num);
674 /* Now HW will be NAKing transfers to that EP,
675 * until a buffer is queued to it. */
676 spin_unlock_irqrestore(&udc->lock, flags);
683 spin_unlock_irqrestore(&udc->lock, flags);
685 dev_err(udc->dev, "failed to initialize %s\n", ep->ep.name);
689 static inline void qe_usb_enable(struct qe_udc *udc)
691 setbits8(&udc->usb_regs->usb_usmod, USB_MODE_EN);
694 static inline void qe_usb_disable(struct qe_udc *udc)
696 clrbits8(&udc->usb_regs->usb_usmod, USB_MODE_EN);
699 /*----------------------------------------------------------------------------*
700 * USB and EP basic manipulate function end *
701 *----------------------------------------------------------------------------*/
704 /******************************************************************************
705 UDC transmit and receive process
706 ******************************************************************************/
707 static void recycle_one_rxbd(struct qe_ep *ep)
711 bdstatus = in_be32((u32 __iomem *)ep->e_rxbd);
712 bdstatus = R_I | R_E | (bdstatus & R_W);
713 out_be32((u32 __iomem *)ep->e_rxbd, bdstatus);
716 ep->e_rxbd = ep->rxbase;
721 static void recycle_rxbds(struct qe_ep *ep, unsigned char stopatnext)
724 struct qe_bd __iomem *bd, *nextbd;
725 unsigned char stop = 0;
729 bdstatus = in_be32((u32 __iomem *)bd);
731 while (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK) && !stop) {
732 bdstatus = R_E | R_I | (bdstatus & R_W);
733 out_be32((u32 __iomem *)bd, bdstatus);
740 bdstatus = in_be32((u32 __iomem *)bd);
741 if (stopatnext && (bd == nextbd))
748 static void ep_recycle_rxbds(struct qe_ep *ep)
750 struct qe_bd __iomem *bd = ep->n_rxbd;
752 u8 epnum = ep->epnum;
753 struct qe_udc *udc = ep->udc;
755 bdstatus = in_be32((u32 __iomem *)bd);
756 if (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK)) {
758 ((in_be16(&udc->ep_param[epnum]->rbptr) -
759 in_be16(&udc->ep_param[epnum]->rbase))
761 bdstatus = in_be32((u32 __iomem *)bd);
769 recycle_rxbds(ep, 0);
770 ep->e_rxbd = ep->n_rxbd;
772 recycle_rxbds(ep, 1);
774 if (in_be16(&udc->usb_regs->usb_usber) & USB_E_BSY_MASK)
775 out_be16(&udc->usb_regs->usb_usber, USB_E_BSY_MASK);
777 if (ep->has_data <= 0 && (!list_empty(&ep->queue)))
783 static void setup_received_handle(struct qe_udc *udc,
784 struct usb_ctrlrequest *setup);
785 static int qe_ep_rxframe_handle(struct qe_ep *ep);
786 static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req);
787 /* when BD PID is setup, handle the packet */
788 static int ep0_setup_handle(struct qe_udc *udc)
790 struct qe_ep *ep = &udc->eps[0];
791 struct qe_frame *pframe;
795 pframe = ep->rxframe;
796 if ((frame_get_info(pframe) & PID_SETUP)
797 && (udc->ep0_state == WAIT_FOR_SETUP)) {
798 fsize = frame_get_length(pframe);
799 if (unlikely(fsize != 8))
801 cp = (u8 *)&udc->local_setup_buff;
802 memcpy(cp, pframe->data, fsize);
805 /* handle the usb command base on the usb_ctrlrequest */
806 setup_received_handle(udc, &udc->local_setup_buff);
812 static int qe_ep0_rx(struct qe_udc *udc)
814 struct qe_ep *ep = &udc->eps[0];
815 struct qe_frame *pframe;
816 struct qe_bd __iomem *bd;
817 u32 bdstatus, length;
820 pframe = ep->rxframe;
822 if (ep->dir == USB_DIR_IN) {
823 dev_err(udc->dev, "ep0 not a control endpoint\n");
828 bdstatus = in_be32((u32 __iomem *)bd);
829 length = bdstatus & BD_LENGTH_MASK;
831 while (!(bdstatus & R_E) && length) {
832 if ((bdstatus & R_F) && (bdstatus & R_L)
833 && !(bdstatus & R_ERROR)) {
834 if (length == USB_CRC_SIZE) {
835 udc->ep0_state = WAIT_FOR_SETUP;
837 "receive a ZLP in status phase\n");
839 qe_frame_clean(pframe);
840 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
841 frame_set_data(pframe, (u8 *)vaddr);
842 frame_set_length(pframe,
843 (length - USB_CRC_SIZE));
844 frame_set_status(pframe, FRAME_OK);
845 switch (bdstatus & R_PID) {
847 frame_set_info(pframe, PID_SETUP);
850 frame_set_info(pframe, PID_DATA1);
853 frame_set_info(pframe, PID_DATA0);
857 if ((bdstatus & R_PID) == R_PID_SETUP)
858 ep0_setup_handle(udc);
860 qe_ep_rxframe_handle(ep);
863 dev_err(udc->dev, "The receive frame with error!\n");
866 /* note: don't clear the rxbd's buffer address */
867 recycle_one_rxbd(ep);
875 bdstatus = in_be32((u32 __iomem *)bd);
876 length = bdstatus & BD_LENGTH_MASK;
885 static int qe_ep_rxframe_handle(struct qe_ep *ep)
887 struct qe_frame *pframe;
893 pframe = ep->rxframe;
895 if (frame_get_info(pframe) & PID_DATA1)
898 if (framepid != ep->data01) {
899 dev_err(ep->udc->dev, "the data01 error!\n");
903 fsize = frame_get_length(pframe);
904 if (list_empty(&ep->queue)) {
905 dev_err(ep->udc->dev, "the %s have no requeue!\n", ep->name);
907 req = list_entry(ep->queue.next, struct qe_req, queue);
909 cp = (u8 *)(req->req.buf) + req->req.actual;
911 memcpy(cp, pframe->data, fsize);
912 req->req.actual += fsize;
913 if ((fsize < ep->ep.maxpacket) ||
914 (req->req.actual >= req->req.length)) {
916 ep0_req_complete(ep->udc, req);
919 if (list_empty(&ep->queue) && ep->epnum != 0)
925 qe_ep_toggledata01(ep);
930 static void ep_rx_tasklet(unsigned long data)
932 struct qe_udc *udc = (struct qe_udc *)data;
934 struct qe_frame *pframe;
935 struct qe_bd __iomem *bd;
937 u32 bdstatus, length;
940 spin_lock_irqsave(&udc->lock, flags);
942 for (i = 1; i < USB_MAX_ENDPOINTS; i++) {
945 if (ep->dir == USB_DIR_IN || ep->enable_tasklet == 0) {
947 "This is a transmit ep or disable tasklet!\n");
951 pframe = ep->rxframe;
953 bdstatus = in_be32((u32 __iomem *)bd);
954 length = bdstatus & BD_LENGTH_MASK;
956 while (!(bdstatus & R_E) && length) {
957 if (list_empty(&ep->queue)) {
960 "The rxep have noreq %d\n",
965 if ((bdstatus & R_F) && (bdstatus & R_L)
966 && !(bdstatus & R_ERROR)) {
967 qe_frame_clean(pframe);
968 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
969 frame_set_data(pframe, (u8 *)vaddr);
970 frame_set_length(pframe,
971 (length - USB_CRC_SIZE));
972 frame_set_status(pframe, FRAME_OK);
973 switch (bdstatus & R_PID) {
975 frame_set_info(pframe, PID_DATA1);
978 frame_set_info(pframe, PID_SETUP);
981 frame_set_info(pframe, PID_DATA0);
984 /* handle the rx frame */
985 qe_ep_rxframe_handle(ep);
988 "error in received frame\n");
990 /* note: don't clear the rxbd's buffer address */
991 /*clear the length */
992 out_be32((u32 __iomem *)bd, bdstatus & BD_STATUS_MASK);
994 if (!(ep->localnack))
995 recycle_one_rxbd(ep);
1003 bdstatus = in_be32((u32 __iomem *)bd);
1004 length = bdstatus & BD_LENGTH_MASK;
1010 ep_recycle_rxbds(ep);
1012 ep->enable_tasklet = 0;
1015 spin_unlock_irqrestore(&udc->lock, flags);
1018 static int qe_ep_rx(struct qe_ep *ep)
1021 struct qe_frame *pframe;
1022 struct qe_bd __iomem *bd;
1023 u16 swoffs, ucoffs, emptybds;
1026 pframe = ep->rxframe;
1028 if (ep->dir == USB_DIR_IN) {
1029 dev_err(udc->dev, "transmit ep in rx function\n");
1035 swoffs = (u16)(bd - ep->rxbase);
1036 ucoffs = (u16)((in_be16(&udc->ep_param[ep->epnum]->rbptr) -
1037 in_be16(&udc->ep_param[ep->epnum]->rbase)) >> 3);
1038 if (swoffs < ucoffs)
1039 emptybds = USB_BDRING_LEN_RX - ucoffs + swoffs;
1041 emptybds = swoffs - ucoffs;
1043 if (emptybds < MIN_EMPTY_BDS) {
1046 dev_vdbg(udc->dev, "%d empty bds, send NACK\n", emptybds);
1048 ep->has_data = USB_BDRING_LEN_RX - emptybds;
1050 if (list_empty(&ep->queue)) {
1052 dev_vdbg(udc->dev, "The rxep have no req queued with %d BDs\n",
1057 tasklet_schedule(&udc->rx_tasklet);
1058 ep->enable_tasklet = 1;
1063 /* send data from a frame, no matter what tx_req */
1064 static int qe_ep_tx(struct qe_ep *ep, struct qe_frame *frame)
1066 struct qe_udc *udc = ep->udc;
1067 struct qe_bd __iomem *bd;
1069 u32 bdstatus, pidmask;
1072 if (ep->dir == USB_DIR_OUT) {
1073 dev_err(udc->dev, "receive ep passed to tx function\n");
1077 /* Disable the Tx interrupt */
1078 saveusbmr = in_be16(&udc->usb_regs->usb_usbmr);
1079 out_be16(&udc->usb_regs->usb_usbmr,
1080 saveusbmr & ~(USB_E_TXB_MASK | USB_E_TXE_MASK));
1083 bdstatus = in_be32((u32 __iomem *)bd);
1085 if (!(bdstatus & (T_R | BD_LENGTH_MASK))) {
1086 if (frame_get_length(frame) == 0) {
1087 frame_set_data(frame, udc->nullbuf);
1088 frame_set_length(frame, 2);
1089 frame->info |= (ZLP | NO_CRC);
1090 dev_vdbg(udc->dev, "the frame size = 0\n");
1092 paddr = virt_to_phys((void *)frame->data);
1093 out_be32(&bd->buf, paddr);
1094 bdstatus = (bdstatus&T_W);
1095 if (!(frame_get_info(frame) & NO_CRC))
1096 bdstatus |= T_R | T_I | T_L | T_TC
1097 | frame_get_length(frame);
1099 bdstatus |= T_R | T_I | T_L | frame_get_length(frame);
1101 /* if the packet is a ZLP in status phase */
1102 if ((ep->epnum == 0) && (udc->ep0_state == DATA_STATE_NEED_ZLP))
1106 pidmask = T_PID_DATA1;
1107 frame->info |= PID_DATA1;
1109 pidmask = T_PID_DATA0;
1110 frame->info |= PID_DATA0;
1113 bdstatus |= pidmask;
1114 out_be32((u32 __iomem *)bd, bdstatus);
1115 qe_ep_filltxfifo(ep);
1117 /* enable the TX interrupt */
1118 out_be16(&udc->usb_regs->usb_usbmr, saveusbmr);
1120 qe_ep_toggledata01(ep);
1122 ep->n_txbd = ep->txbase;
1128 out_be16(&udc->usb_regs->usb_usbmr, saveusbmr);
1129 dev_vdbg(udc->dev, "The tx bd is not ready!\n");
1134 /* when a bd was transmitted, the function can
1135 * handle the tx_req, not include ep0 */
1136 static int txcomplete(struct qe_ep *ep, unsigned char restart)
1138 if (ep->tx_req != NULL) {
1139 struct qe_req *req = ep->tx_req;
1140 unsigned zlp = 0, last_len = 0;
1142 last_len = min_t(unsigned, req->req.length - ep->sent,
1146 int asent = ep->last;
1153 /* zlp needed when req->re.zero is set */
1154 if (req->req.zero) {
1155 if (last_len == 0 ||
1156 (req->req.length % ep->ep.maxpacket) != 0)
1163 /* a request already were transmitted completely */
1164 if (((ep->tx_req->req.length - ep->sent) <= 0) && !zlp) {
1165 done(ep, ep->tx_req, 0);
1172 /* we should gain a new tx_req fot this endpoint */
1173 if (ep->tx_req == NULL) {
1174 if (!list_empty(&ep->queue)) {
1175 ep->tx_req = list_entry(ep->queue.next, struct qe_req,
1185 /* give a frame and a tx_req, send some data */
1186 static int qe_usb_senddata(struct qe_ep *ep, struct qe_frame *frame)
1191 qe_frame_clean(frame);
1192 size = min_t(u32, (ep->tx_req->req.length - ep->sent),
1194 buf = (u8 *)ep->tx_req->req.buf + ep->sent;
1197 ep->tx_req->req.actual += size;
1198 frame_set_data(frame, buf);
1199 frame_set_length(frame, size);
1200 frame_set_status(frame, FRAME_OK);
1201 frame_set_info(frame, 0);
1202 return qe_ep_tx(ep, frame);
1207 /* give a frame struct,send a ZLP */
1208 static int sendnulldata(struct qe_ep *ep, struct qe_frame *frame, uint infor)
1210 struct qe_udc *udc = ep->udc;
1215 qe_frame_clean(frame);
1216 frame_set_data(frame, (u8 *)udc->nullbuf);
1217 frame_set_length(frame, 2);
1218 frame_set_status(frame, FRAME_OK);
1219 frame_set_info(frame, (ZLP | NO_CRC | infor));
1221 return qe_ep_tx(ep, frame);
1224 static int frame_create_tx(struct qe_ep *ep, struct qe_frame *frame)
1226 struct qe_req *req = ep->tx_req;
1232 if ((req->req.length - ep->sent) > 0)
1233 reval = qe_usb_senddata(ep, frame);
1235 reval = sendnulldata(ep, frame, 0);
1240 /* if direction is DIR_IN, the status is Device->Host
1241 * if direction is DIR_OUT, the status transaction is Device<-Host
1242 * in status phase, udc create a request and gain status */
1243 static int ep0_prime_status(struct qe_udc *udc, int direction)
1246 struct qe_ep *ep = &udc->eps[0];
1248 if (direction == USB_DIR_IN) {
1249 udc->ep0_state = DATA_STATE_NEED_ZLP;
1250 udc->ep0_dir = USB_DIR_IN;
1251 sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ);
1253 udc->ep0_dir = USB_DIR_OUT;
1254 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1260 /* a request complete in ep0, whether gadget request or udc request */
1261 static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req)
1263 struct qe_ep *ep = &udc->eps[0];
1264 /* because usb and ep's status already been set in ch9setaddress() */
1266 switch (udc->ep0_state) {
1267 case DATA_STATE_XMIT:
1269 /* receive status phase */
1270 if (ep0_prime_status(udc, USB_DIR_OUT))
1274 case DATA_STATE_NEED_ZLP:
1276 udc->ep0_state = WAIT_FOR_SETUP;
1279 case DATA_STATE_RECV:
1281 /* send status phase */
1282 if (ep0_prime_status(udc, USB_DIR_IN))
1286 case WAIT_FOR_OUT_STATUS:
1288 udc->ep0_state = WAIT_FOR_SETUP;
1291 case WAIT_FOR_SETUP:
1292 dev_vdbg(udc->dev, "Unexpected interrupt\n");
1301 static int ep0_txcomplete(struct qe_ep *ep, unsigned char restart)
1303 struct qe_req *tx_req = NULL;
1304 struct qe_frame *frame = ep->txframe;
1306 if ((frame_get_info(frame) & (ZLP | NO_REQ)) == (ZLP | NO_REQ)) {
1308 ep->udc->ep0_state = WAIT_FOR_SETUP;
1310 sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ);
1314 tx_req = ep->tx_req;
1315 if (tx_req != NULL) {
1317 int asent = ep->last;
1324 /* a request already were transmitted completely */
1325 if ((ep->tx_req->req.length - ep->sent) <= 0) {
1326 ep->tx_req->req.actual = (unsigned int)ep->sent;
1327 ep0_req_complete(ep->udc, ep->tx_req);
1333 dev_vdbg(ep->udc->dev, "the ep0_controller have no req\n");
1339 static int ep0_txframe_handle(struct qe_ep *ep)
1341 /* if have error, transmit again */
1342 if (frame_get_status(ep->txframe) & FRAME_ERROR) {
1343 qe_ep_flushtxfifo(ep);
1344 dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n");
1345 if (frame_get_info(ep->txframe) & PID_DATA0)
1350 ep0_txcomplete(ep, 1);
1352 ep0_txcomplete(ep, 0);
1354 frame_create_tx(ep, ep->txframe);
1358 static int qe_ep0_txconf(struct qe_ep *ep)
1360 struct qe_bd __iomem *bd;
1361 struct qe_frame *pframe;
1365 bdstatus = in_be32((u32 __iomem *)bd);
1366 while (!(bdstatus & T_R) && (bdstatus & ~T_W)) {
1367 pframe = ep->txframe;
1369 /* clear and recycle the BD */
1370 out_be32((u32 __iomem *)bd, bdstatus & T_W);
1371 out_be32(&bd->buf, 0);
1373 ep->c_txbd = ep->txbase;
1377 if (ep->c_txbd == ep->n_txbd) {
1378 if (bdstatus & DEVICE_T_ERROR) {
1379 frame_set_status(pframe, FRAME_ERROR);
1380 if (bdstatus & T_TO)
1381 pframe->status |= TX_ER_TIMEOUT;
1382 if (bdstatus & T_UN)
1383 pframe->status |= TX_ER_UNDERUN;
1385 ep0_txframe_handle(ep);
1389 bdstatus = in_be32((u32 __iomem *)bd);
1395 static int ep_txframe_handle(struct qe_ep *ep)
1397 if (frame_get_status(ep->txframe) & FRAME_ERROR) {
1398 qe_ep_flushtxfifo(ep);
1399 dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n");
1400 if (frame_get_info(ep->txframe) & PID_DATA0)
1409 frame_create_tx(ep, ep->txframe); /* send the data */
1413 /* confirm the already trainsmited bd */
1414 static int qe_ep_txconf(struct qe_ep *ep)
1416 struct qe_bd __iomem *bd;
1417 struct qe_frame *pframe = NULL;
1419 unsigned char breakonrxinterrupt = 0;
1422 bdstatus = in_be32((u32 __iomem *)bd);
1423 while (!(bdstatus & T_R) && (bdstatus & ~T_W)) {
1424 pframe = ep->txframe;
1425 if (bdstatus & DEVICE_T_ERROR) {
1426 frame_set_status(pframe, FRAME_ERROR);
1427 if (bdstatus & T_TO)
1428 pframe->status |= TX_ER_TIMEOUT;
1429 if (bdstatus & T_UN)
1430 pframe->status |= TX_ER_UNDERUN;
1433 /* clear and recycle the BD */
1434 out_be32((u32 __iomem *)bd, bdstatus & T_W);
1435 out_be32(&bd->buf, 0);
1437 ep->c_txbd = ep->txbase;
1441 /* handle the tx frame */
1442 ep_txframe_handle(ep);
1444 bdstatus = in_be32((u32 __iomem *)bd);
1446 if (breakonrxinterrupt)
1452 /* Add a request in queue, and try to transmit a packet */
1453 static int ep_req_send(struct qe_ep *ep, struct qe_req *req)
1457 if (ep->tx_req == NULL) {
1460 txcomplete(ep, 0); /* can gain a new tx_req */
1461 reval = frame_create_tx(ep, ep->txframe);
1466 /* Maybe this is a good ideal */
1467 static int ep_req_rx(struct qe_ep *ep, struct qe_req *req)
1469 struct qe_udc *udc = ep->udc;
1470 struct qe_frame *pframe = NULL;
1471 struct qe_bd __iomem *bd;
1472 u32 bdstatus, length;
1478 if (list_empty(&ep->queue)) {
1479 dev_vdbg(udc->dev, "the req already finish!\n");
1482 pframe = ep->rxframe;
1485 bdstatus = in_be32((u32 __iomem *)bd);
1486 length = bdstatus & BD_LENGTH_MASK;
1488 while (!(bdstatus & R_E) && length) {
1491 if ((bdstatus & R_F) && (bdstatus & R_L)
1492 && !(bdstatus & R_ERROR)) {
1493 qe_frame_clean(pframe);
1494 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
1495 frame_set_data(pframe, (u8 *)vaddr);
1496 frame_set_length(pframe, (length - USB_CRC_SIZE));
1497 frame_set_status(pframe, FRAME_OK);
1498 switch (bdstatus & R_PID) {
1500 frame_set_info(pframe, PID_DATA1); break;
1502 frame_set_info(pframe, PID_DATA0); break;
1504 /* handle the rx frame */
1506 if (frame_get_info(pframe) & PID_DATA1)
1511 if (framepid != ep->data01) {
1512 dev_vdbg(udc->dev, "the data01 error!\n");
1514 fsize = frame_get_length(pframe);
1516 cp = (u8 *)(req->req.buf) + req->req.actual;
1518 memcpy(cp, pframe->data, fsize);
1519 req->req.actual += fsize;
1520 if ((fsize < ep->ep.maxpacket)
1521 || (req->req.actual >=
1525 if (list_empty(&ep->queue))
1529 qe_ep_toggledata01(ep);
1532 dev_err(udc->dev, "The receive frame with error!\n");
1535 /* note: don't clear the rxbd's buffer address *
1536 * only Clear the length */
1537 out_be32((u32 __iomem *)bd, (bdstatus & BD_STATUS_MASK));
1546 bdstatus = in_be32((u32 __iomem *)bd);
1547 length = bdstatus & BD_LENGTH_MASK;
1551 ep_recycle_rxbds(ep);
1556 /* only add the request in queue */
1557 static int ep_req_receive(struct qe_ep *ep, struct qe_req *req)
1559 if (ep->state == EP_STATE_NACK) {
1560 if (ep->has_data <= 0) {
1561 /* Enable rx and unmask rx interrupt */
1564 /* Copy the exist BD data */
1572 /********************************************************************
1573 Internal Used Function End
1574 ********************************************************************/
1576 /*-----------------------------------------------------------------------
1577 Endpoint Management Functions For Gadget
1578 -----------------------------------------------------------------------*/
1579 static int qe_ep_enable(struct usb_ep *_ep,
1580 const struct usb_endpoint_descriptor *desc)
1585 unsigned char epnum;
1587 ep = container_of(_ep, struct qe_ep, ep);
1589 /* catch various bogus parameters */
1590 if (!_ep || !desc || _ep->name == ep_name[0] ||
1591 (desc->bDescriptorType != USB_DT_ENDPOINT))
1595 if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
1598 epnum = (u8)desc->bEndpointAddress & 0xF;
1600 retval = qe_ep_init(udc, epnum, desc);
1602 cpm_muram_free(cpm_muram_offset(ep->rxbase));
1603 dev_dbg(udc->dev, "enable ep%d failed\n", ep->epnum);
1606 dev_dbg(udc->dev, "enable ep%d successful\n", ep->epnum);
1610 static int qe_ep_disable(struct usb_ep *_ep)
1614 unsigned long flags;
1617 ep = container_of(_ep, struct qe_ep, ep);
1620 if (!_ep || !ep->ep.desc) {
1621 dev_dbg(udc->dev, "%s not enabled\n", _ep ? ep->ep.name : NULL);
1625 spin_lock_irqsave(&udc->lock, flags);
1626 /* Nuke all pending requests (does flush) */
1627 nuke(ep, -ESHUTDOWN);
1631 qe_ep_reset(udc, ep->epnum);
1632 spin_unlock_irqrestore(&udc->lock, flags);
1634 cpm_muram_free(cpm_muram_offset(ep->rxbase));
1636 if (ep->dir == USB_DIR_OUT)
1637 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) *
1638 (USB_BDRING_LEN_RX + 1);
1640 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) *
1641 (USB_BDRING_LEN + 1);
1643 if (ep->dir != USB_DIR_IN) {
1646 dma_unmap_single(udc->gadget.dev.parent,
1649 ep->rxbuf_d = DMA_ADDR_INVALID;
1651 dma_sync_single_for_cpu(
1652 udc->gadget.dev.parent,
1656 kfree(ep->rxbuffer);
1659 if (ep->dir != USB_DIR_OUT)
1662 dev_dbg(udc->dev, "disabled %s OK\n", _ep->name);
1666 static struct usb_request *qe_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
1670 req = kzalloc(sizeof(*req), gfp_flags);
1674 req->req.dma = DMA_ADDR_INVALID;
1676 INIT_LIST_HEAD(&req->queue);
1681 static void qe_free_request(struct usb_ep *_ep, struct usb_request *_req)
1685 req = container_of(_req, struct qe_req, req);
1691 static int __qe_ep_queue(struct usb_ep *_ep, struct usb_request *_req)
1693 struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1694 struct qe_req *req = container_of(_req, struct qe_req, req);
1699 /* catch various bogus parameters */
1700 if (!_req || !req->req.complete || !req->req.buf
1701 || !list_empty(&req->queue)) {
1702 dev_dbg(udc->dev, "bad params\n");
1705 if (!_ep || (!ep->ep.desc && ep_index(ep))) {
1706 dev_dbg(udc->dev, "bad ep\n");
1710 if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1715 /* map virtual address to hardware */
1716 if (req->req.dma == DMA_ADDR_INVALID) {
1717 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1725 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
1726 req->req.dma, req->req.length,
1733 req->req.status = -EINPROGRESS;
1734 req->req.actual = 0;
1736 list_add_tail(&req->queue, &ep->queue);
1737 dev_vdbg(udc->dev, "gadget have request in %s! %d\n",
1738 ep->name, req->req.length);
1740 /* push the request to device */
1742 reval = ep_req_send(ep, req);
1745 if (ep_index(ep) == 0 && req->req.length > 0) {
1747 udc->ep0_state = DATA_STATE_XMIT;
1749 udc->ep0_state = DATA_STATE_RECV;
1752 if (ep->dir == USB_DIR_OUT)
1753 reval = ep_req_receive(ep, req);
1758 /* queues (submits) an I/O request to an endpoint */
1759 static int qe_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1762 struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1763 struct qe_udc *udc = ep->udc;
1764 unsigned long flags;
1767 spin_lock_irqsave(&udc->lock, flags);
1768 ret = __qe_ep_queue(_ep, _req);
1769 spin_unlock_irqrestore(&udc->lock, flags);
1773 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1774 static int qe_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1776 struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1778 unsigned long flags;
1783 spin_lock_irqsave(&ep->udc->lock, flags);
1785 /* make sure it's actually queued on this endpoint */
1786 list_for_each_entry(req, &ep->queue, queue) {
1787 if (&req->req == _req)
1791 if (&req->req != _req) {
1792 spin_unlock_irqrestore(&ep->udc->lock, flags);
1796 done(ep, req, -ECONNRESET);
1798 spin_unlock_irqrestore(&ep->udc->lock, flags);
1802 /*-----------------------------------------------------------------
1803 * modify the endpoint halt feature
1804 * @ep: the non-isochronous endpoint being stalled
1805 * @value: 1--set halt 0--clear halt
1806 * Returns zero, or a negative error code.
1807 *----------------------------------------------------------------*/
1808 static int qe_ep_set_halt(struct usb_ep *_ep, int value)
1811 unsigned long flags;
1812 int status = -EOPNOTSUPP;
1815 ep = container_of(_ep, struct qe_ep, ep);
1816 if (!_ep || !ep->ep.desc) {
1822 /* Attempt to halt IN ep will fail if any transfer requests
1823 * are still queue */
1824 if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1830 spin_lock_irqsave(&ep->udc->lock, flags);
1831 qe_eptx_stall_change(ep, value);
1832 qe_eprx_stall_change(ep, value);
1833 spin_unlock_irqrestore(&ep->udc->lock, flags);
1835 if (ep->epnum == 0) {
1836 udc->ep0_state = WAIT_FOR_SETUP;
1840 /* set data toggle to DATA0 on clear halt */
1844 dev_vdbg(udc->dev, "%s %s halt stat %d\n", ep->ep.name,
1845 value ? "set" : "clear", status);
1850 static const struct usb_ep_ops qe_ep_ops = {
1851 .enable = qe_ep_enable,
1852 .disable = qe_ep_disable,
1854 .alloc_request = qe_alloc_request,
1855 .free_request = qe_free_request,
1857 .queue = qe_ep_queue,
1858 .dequeue = qe_ep_dequeue,
1860 .set_halt = qe_ep_set_halt,
1863 /*------------------------------------------------------------------------
1864 Gadget Driver Layer Operations
1865 ------------------------------------------------------------------------*/
1867 /* Get the current frame number */
1868 static int qe_get_frame(struct usb_gadget *gadget)
1870 struct qe_udc *udc = container_of(gadget, struct qe_udc, gadget);
1873 tmp = in_be16(&udc->usb_param->frame_n);
1875 return tmp & 0x07ff;
1879 static int fsl_qe_start(struct usb_gadget *gadget,
1880 struct usb_gadget_driver *driver);
1881 static int fsl_qe_stop(struct usb_gadget *gadget);
1883 /* defined in usb_gadget.h */
1884 static const struct usb_gadget_ops qe_gadget_ops = {
1885 .get_frame = qe_get_frame,
1886 .udc_start = fsl_qe_start,
1887 .udc_stop = fsl_qe_stop,
1890 /*-------------------------------------------------------------------------
1891 USB ep0 Setup process in BUS Enumeration
1892 -------------------------------------------------------------------------*/
1893 static int udc_reset_ep_queue(struct qe_udc *udc, u8 pipe)
1895 struct qe_ep *ep = &udc->eps[pipe];
1897 nuke(ep, -ECONNRESET);
1902 static int reset_queues(struct qe_udc *udc)
1906 for (pipe = 0; pipe < USB_MAX_ENDPOINTS; pipe++)
1907 udc_reset_ep_queue(udc, pipe);
1909 /* report disconnect; the driver is already quiesced */
1910 spin_unlock(&udc->lock);
1911 usb_gadget_udc_reset(&udc->gadget, udc->driver);
1912 spin_lock(&udc->lock);
1917 static void ch9setaddress(struct qe_udc *udc, u16 value, u16 index,
1920 /* Save the new address to device struct */
1921 udc->device_address = (u8) value;
1922 /* Update usb state */
1923 udc->usb_state = USB_STATE_ADDRESS;
1925 /* Status phase , send a ZLP */
1926 if (ep0_prime_status(udc, USB_DIR_IN))
1930 static void ownercomplete(struct usb_ep *_ep, struct usb_request *_req)
1932 struct qe_req *req = container_of(_req, struct qe_req, req);
1934 req->req.buf = NULL;
1938 static void ch9getstatus(struct qe_udc *udc, u8 request_type, u16 value,
1939 u16 index, u16 length)
1947 if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1948 /* Get device status */
1949 usb_status = 1 << USB_DEVICE_SELF_POWERED;
1950 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1951 /* Get interface status */
1952 /* We don't have interface information in udc driver */
1954 } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1955 /* Get endpoint status */
1956 int pipe = index & USB_ENDPOINT_NUMBER_MASK;
1957 struct qe_ep *target_ep;
1960 if (pipe >= USB_MAX_ENDPOINTS)
1962 target_ep = &udc->eps[pipe];
1964 /* stall if endpoint doesn't exist */
1965 if (!target_ep->ep.desc)
1968 usep = in_be16(&udc->usb_regs->usb_usep[pipe]);
1969 if (index & USB_DIR_IN) {
1970 if (target_ep->dir != USB_DIR_IN)
1972 if ((usep & USB_THS_MASK) == USB_THS_STALL)
1973 usb_status = 1 << USB_ENDPOINT_HALT;
1975 if (target_ep->dir != USB_DIR_OUT)
1977 if ((usep & USB_RHS_MASK) == USB_RHS_STALL)
1978 usb_status = 1 << USB_ENDPOINT_HALT;
1982 req = container_of(qe_alloc_request(&ep->ep, GFP_KERNEL),
1983 struct qe_req, req);
1984 req->req.length = 2;
1985 req->req.buf = udc->statusbuf;
1986 *(u16 *)req->req.buf = cpu_to_le16(usb_status);
1987 req->req.status = -EINPROGRESS;
1988 req->req.actual = 0;
1989 req->req.complete = ownercomplete;
1991 udc->ep0_dir = USB_DIR_IN;
1994 status = __qe_ep_queue(&ep->ep, &req->req);
1999 dev_err(udc->dev, "Can't respond to getstatus request \n");
2003 /* only handle the setup request, suppose the device in normal status */
2004 static void setup_received_handle(struct qe_udc *udc,
2005 struct usb_ctrlrequest *setup)
2007 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2008 u16 wValue = le16_to_cpu(setup->wValue);
2009 u16 wIndex = le16_to_cpu(setup->wIndex);
2010 u16 wLength = le16_to_cpu(setup->wLength);
2012 /* clear the previous request in the ep0 */
2013 udc_reset_ep_queue(udc, 0);
2015 if (setup->bRequestType & USB_DIR_IN)
2016 udc->ep0_dir = USB_DIR_IN;
2018 udc->ep0_dir = USB_DIR_OUT;
2020 switch (setup->bRequest) {
2021 case USB_REQ_GET_STATUS:
2022 /* Data+Status phase form udc */
2023 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
2024 != (USB_DIR_IN | USB_TYPE_STANDARD))
2026 ch9getstatus(udc, setup->bRequestType, wValue, wIndex,
2030 case USB_REQ_SET_ADDRESS:
2031 /* Status phase from udc */
2032 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
2035 ch9setaddress(udc, wValue, wIndex, wLength);
2038 case USB_REQ_CLEAR_FEATURE:
2039 case USB_REQ_SET_FEATURE:
2040 /* Requests with no data phase, status phase from udc */
2041 if ((setup->bRequestType & USB_TYPE_MASK)
2042 != USB_TYPE_STANDARD)
2045 if ((setup->bRequestType & USB_RECIP_MASK)
2046 == USB_RECIP_ENDPOINT) {
2047 int pipe = wIndex & USB_ENDPOINT_NUMBER_MASK;
2050 if (wValue != 0 || wLength != 0
2051 || pipe >= USB_MAX_ENDPOINTS)
2053 ep = &udc->eps[pipe];
2055 spin_unlock(&udc->lock);
2056 qe_ep_set_halt(&ep->ep,
2057 (setup->bRequest == USB_REQ_SET_FEATURE)
2059 spin_lock(&udc->lock);
2062 ep0_prime_status(udc, USB_DIR_IN);
2071 /* Data phase from gadget, status phase from udc */
2072 if (setup->bRequestType & USB_DIR_IN) {
2073 udc->ep0_state = DATA_STATE_XMIT;
2074 udc->ep0_dir = USB_DIR_IN;
2076 udc->ep0_state = DATA_STATE_RECV;
2077 udc->ep0_dir = USB_DIR_OUT;
2079 spin_unlock(&udc->lock);
2080 if (udc->driver->setup(&udc->gadget,
2081 &udc->local_setup_buff) < 0)
2083 spin_lock(&udc->lock);
2085 /* No data phase, IN status from gadget */
2086 udc->ep0_dir = USB_DIR_IN;
2087 spin_unlock(&udc->lock);
2088 if (udc->driver->setup(&udc->gadget,
2089 &udc->local_setup_buff) < 0)
2091 spin_lock(&udc->lock);
2092 udc->ep0_state = DATA_STATE_NEED_ZLP;
2096 /*-------------------------------------------------------------------------
2097 USB Interrupt handlers
2098 -------------------------------------------------------------------------*/
2099 static void suspend_irq(struct qe_udc *udc)
2101 udc->resume_state = udc->usb_state;
2102 udc->usb_state = USB_STATE_SUSPENDED;
2104 /* report suspend to the driver ,serial.c not support this*/
2105 if (udc->driver->suspend)
2106 udc->driver->suspend(&udc->gadget);
2109 static void resume_irq(struct qe_udc *udc)
2111 udc->usb_state = udc->resume_state;
2112 udc->resume_state = 0;
2114 /* report resume to the driver , serial.c not support this*/
2115 if (udc->driver->resume)
2116 udc->driver->resume(&udc->gadget);
2119 static void idle_irq(struct qe_udc *udc)
2123 usbs = in_8(&udc->usb_regs->usb_usbs);
2124 if (usbs & USB_IDLE_STATUS_MASK) {
2125 if ((udc->usb_state) != USB_STATE_SUSPENDED)
2128 if (udc->usb_state == USB_STATE_SUSPENDED)
2133 static int reset_irq(struct qe_udc *udc)
2137 if (udc->usb_state == USB_STATE_DEFAULT)
2140 qe_usb_disable(udc);
2141 out_8(&udc->usb_regs->usb_usadr, 0);
2143 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2144 if (udc->eps[i].init)
2145 qe_ep_reset(udc, i);
2149 udc->usb_state = USB_STATE_DEFAULT;
2150 udc->ep0_state = WAIT_FOR_SETUP;
2151 udc->ep0_dir = USB_DIR_OUT;
2156 static int bsy_irq(struct qe_udc *udc)
2161 static int txe_irq(struct qe_udc *udc)
2166 /* ep0 tx interrupt also in here */
2167 static int tx_irq(struct qe_udc *udc)
2170 struct qe_bd __iomem *bd;
2173 if ((udc->usb_state == USB_STATE_ADDRESS)
2174 && (in_8(&udc->usb_regs->usb_usadr) == 0))
2175 out_8(&udc->usb_regs->usb_usadr, udc->device_address);
2177 for (i = (USB_MAX_ENDPOINTS-1); ((i >= 0) && (res == 0)); i--) {
2179 if (ep && ep->init && (ep->dir != USB_DIR_OUT)) {
2181 if (!(in_be32((u32 __iomem *)bd) & T_R)
2182 && (in_be32(&bd->buf))) {
2183 /* confirm the transmitted bd */
2185 res = qe_ep0_txconf(ep);
2187 res = qe_ep_txconf(ep);
2195 /* setup packect's rx is handle in the function too */
2196 static void rx_irq(struct qe_udc *udc)
2199 struct qe_bd __iomem *bd;
2202 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2204 if (ep && ep->init && (ep->dir != USB_DIR_IN)) {
2206 if (!(in_be32((u32 __iomem *)bd) & R_E)
2207 && (in_be32(&bd->buf))) {
2208 if (ep->epnum == 0) {
2211 /*non-setup package receive*/
2219 static irqreturn_t qe_udc_irq(int irq, void *_udc)
2221 struct qe_udc *udc = (struct qe_udc *)_udc;
2223 irqreturn_t status = IRQ_NONE;
2224 unsigned long flags;
2226 spin_lock_irqsave(&udc->lock, flags);
2228 irq_src = in_be16(&udc->usb_regs->usb_usber) &
2229 in_be16(&udc->usb_regs->usb_usbmr);
2230 /* Clear notification bits */
2231 out_be16(&udc->usb_regs->usb_usber, irq_src);
2233 if (irq_src & USB_E_IDLE_MASK) {
2235 irq_src &= ~USB_E_IDLE_MASK;
2236 status = IRQ_HANDLED;
2239 if (irq_src & USB_E_TXB_MASK) {
2241 irq_src &= ~USB_E_TXB_MASK;
2242 status = IRQ_HANDLED;
2245 if (irq_src & USB_E_RXB_MASK) {
2247 irq_src &= ~USB_E_RXB_MASK;
2248 status = IRQ_HANDLED;
2251 if (irq_src & USB_E_RESET_MASK) {
2253 irq_src &= ~USB_E_RESET_MASK;
2254 status = IRQ_HANDLED;
2257 if (irq_src & USB_E_BSY_MASK) {
2259 irq_src &= ~USB_E_BSY_MASK;
2260 status = IRQ_HANDLED;
2263 if (irq_src & USB_E_TXE_MASK) {
2265 irq_src &= ~USB_E_TXE_MASK;
2266 status = IRQ_HANDLED;
2269 spin_unlock_irqrestore(&udc->lock, flags);
2274 /*-------------------------------------------------------------------------
2275 Gadget driver probe and unregister.
2276 --------------------------------------------------------------------------*/
2277 static int fsl_qe_start(struct usb_gadget *gadget,
2278 struct usb_gadget_driver *driver)
2281 unsigned long flags;
2283 udc = container_of(gadget, struct qe_udc, gadget);
2284 /* lock is needed but whether should use this lock or another */
2285 spin_lock_irqsave(&udc->lock, flags);
2287 driver->driver.bus = NULL;
2288 /* hook up the driver */
2289 udc->driver = driver;
2290 udc->gadget.speed = driver->max_speed;
2292 /* Enable IRQ reg and Set usbcmd reg EN bit */
2295 out_be16(&udc->usb_regs->usb_usber, 0xffff);
2296 out_be16(&udc->usb_regs->usb_usbmr, USB_E_DEFAULT_DEVICE);
2297 udc->usb_state = USB_STATE_ATTACHED;
2298 udc->ep0_state = WAIT_FOR_SETUP;
2299 udc->ep0_dir = USB_DIR_OUT;
2300 spin_unlock_irqrestore(&udc->lock, flags);
2305 static int fsl_qe_stop(struct usb_gadget *gadget)
2308 struct qe_ep *loop_ep;
2309 unsigned long flags;
2311 udc = container_of(gadget, struct qe_udc, gadget);
2312 /* stop usb controller, disable intr */
2313 qe_usb_disable(udc);
2315 /* in fact, no needed */
2316 udc->usb_state = USB_STATE_ATTACHED;
2317 udc->ep0_state = WAIT_FOR_SETUP;
2320 /* stand operation */
2321 spin_lock_irqsave(&udc->lock, flags);
2322 udc->gadget.speed = USB_SPEED_UNKNOWN;
2323 nuke(&udc->eps[0], -ESHUTDOWN);
2324 list_for_each_entry(loop_ep, &udc->gadget.ep_list, ep.ep_list)
2325 nuke(loop_ep, -ESHUTDOWN);
2326 spin_unlock_irqrestore(&udc->lock, flags);
2333 /* udc structure's alloc and setup, include ep-param alloc */
2334 static struct qe_udc *qe_udc_config(struct platform_device *ofdev)
2337 struct device_node *np = ofdev->dev.of_node;
2338 unsigned long tmp_addr = 0;
2339 struct usb_device_para __iomem *usbpram;
2344 udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2348 udc->dev = &ofdev->dev;
2350 /* get default address of usb parameter in MURAM from device tree */
2351 offset = *of_get_address(np, 1, &size, NULL);
2352 udc->usb_param = cpm_muram_addr(offset);
2353 memset_io(udc->usb_param, 0, size);
2355 usbpram = udc->usb_param;
2356 out_be16(&usbpram->frame_n, 0);
2357 out_be32(&usbpram->rstate, 0);
2359 tmp_addr = cpm_muram_alloc((USB_MAX_ENDPOINTS *
2360 sizeof(struct usb_ep_para)),
2361 USB_EP_PARA_ALIGNMENT);
2362 if (IS_ERR_VALUE(tmp_addr))
2365 for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2366 out_be16(&usbpram->epptr[i], (u16)tmp_addr);
2367 udc->ep_param[i] = cpm_muram_addr(tmp_addr);
2371 memset_io(udc->ep_param[0], 0,
2372 USB_MAX_ENDPOINTS * sizeof(struct usb_ep_para));
2374 udc->resume_state = USB_STATE_NOTATTACHED;
2375 udc->usb_state = USB_STATE_POWERED;
2378 spin_lock_init(&udc->lock);
2386 /* USB Controller register init */
2387 static int qe_udc_reg_init(struct qe_udc *udc)
2389 struct usb_ctlr __iomem *qe_usbregs;
2390 qe_usbregs = udc->usb_regs;
2392 /* Spec says that we must enable the USB controller to change mode. */
2393 out_8(&qe_usbregs->usb_usmod, 0x01);
2394 /* Mode changed, now disable it, since muram isn't initialized yet. */
2395 out_8(&qe_usbregs->usb_usmod, 0x00);
2397 /* Initialize the rest. */
2398 out_be16(&qe_usbregs->usb_usbmr, 0);
2399 out_8(&qe_usbregs->usb_uscom, 0);
2400 out_be16(&qe_usbregs->usb_usber, USBER_ALL_CLEAR);
2405 static int qe_ep_config(struct qe_udc *udc, unsigned char pipe_num)
2407 struct qe_ep *ep = &udc->eps[pipe_num];
2410 strcpy(ep->name, ep_name[pipe_num]);
2411 ep->ep.name = ep_name[pipe_num];
2413 if (pipe_num == 0) {
2414 ep->ep.caps.type_control = true;
2416 ep->ep.caps.type_iso = true;
2417 ep->ep.caps.type_bulk = true;
2418 ep->ep.caps.type_int = true;
2421 ep->ep.caps.dir_in = true;
2422 ep->ep.caps.dir_out = true;
2424 ep->ep.ops = &qe_ep_ops;
2426 usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
2429 ep->epnum = (u8)pipe_num;
2436 ep->state = EP_STATE_IDLE;
2439 /* the queue lists any req for this ep */
2440 INIT_LIST_HEAD(&ep->queue);
2442 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2444 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2446 ep->gadget = &udc->gadget;
2451 /*-----------------------------------------------------------------------
2452 * UDC device Driver operation functions *
2453 *----------------------------------------------------------------------*/
2454 static void qe_udc_release(struct device *dev)
2456 struct qe_udc *udc = container_of(dev, struct qe_udc, gadget.dev);
2459 complete(udc->done);
2460 cpm_muram_free(cpm_muram_offset(udc->ep_param[0]));
2461 for (i = 0; i < USB_MAX_ENDPOINTS; i++)
2462 udc->ep_param[i] = NULL;
2467 /* Driver probe functions */
2468 static const struct of_device_id qe_udc_match[];
2469 static int qe_udc_probe(struct platform_device *ofdev)
2472 const struct of_device_id *match;
2473 struct device_node *np = ofdev->dev.of_node;
2475 unsigned int ret = 0;
2479 match = of_match_device(qe_udc_match, &ofdev->dev);
2483 prop = of_get_property(np, "mode", NULL);
2484 if (!prop || strcmp(prop, "peripheral"))
2487 /* Initialize the udc structure including QH member and other member */
2488 udc = qe_udc_config(ofdev);
2490 dev_err(&ofdev->dev, "failed to initialize\n");
2494 udc->soc_type = (unsigned long)match->data;
2495 udc->usb_regs = of_iomap(np, 0);
2496 if (!udc->usb_regs) {
2501 /* initialize usb hw reg except for regs for EP,
2502 * leave usbintr reg untouched*/
2503 qe_udc_reg_init(udc);
2505 /* here comes the stand operations for probe
2506 * set the qe_udc->gadget.xxx */
2507 udc->gadget.ops = &qe_gadget_ops;
2509 /* gadget.ep0 is a pointer */
2510 udc->gadget.ep0 = &udc->eps[0].ep;
2512 INIT_LIST_HEAD(&udc->gadget.ep_list);
2514 /* modify in register gadget process */
2515 udc->gadget.speed = USB_SPEED_UNKNOWN;
2517 /* name: Identifies the controller hardware type. */
2518 udc->gadget.name = driver_name;
2519 udc->gadget.dev.parent = &ofdev->dev;
2521 /* initialize qe_ep struct */
2522 for (i = 0; i < USB_MAX_ENDPOINTS ; i++) {
2523 /* because the ep type isn't decide here so
2524 * qe_ep_init() should be called in ep_enable() */
2526 /* setup the qe_ep struct and link ep.ep.list
2527 * into gadget.ep_list */
2528 qe_ep_config(udc, (unsigned char)i);
2531 /* ep0 initialization in here */
2532 ret = qe_ep_init(udc, 0, &qe_ep0_desc);
2536 /* create a buf for ZLP send, need to remain zeroed */
2537 udc->nullbuf = devm_kzalloc(&ofdev->dev, 256, GFP_KERNEL);
2538 if (udc->nullbuf == NULL) {
2543 /* buffer for data of get_status request */
2544 udc->statusbuf = devm_kzalloc(&ofdev->dev, 2, GFP_KERNEL);
2545 if (udc->statusbuf == NULL) {
2550 udc->nullp = virt_to_phys((void *)udc->nullbuf);
2551 if (udc->nullp == DMA_ADDR_INVALID) {
2552 udc->nullp = dma_map_single(
2553 udc->gadget.dev.parent,
2559 dma_sync_single_for_device(udc->gadget.dev.parent,
2564 tasklet_init(&udc->rx_tasklet, ep_rx_tasklet,
2565 (unsigned long)udc);
2566 /* request irq and disable DR */
2567 udc->usb_irq = irq_of_parse_and_map(np, 0);
2568 if (!udc->usb_irq) {
2573 ret = request_irq(udc->usb_irq, qe_udc_irq, 0,
2576 dev_err(udc->dev, "cannot request irq %d err %d\n",
2581 ret = usb_add_gadget_udc_release(&ofdev->dev, &udc->gadget,
2586 platform_set_drvdata(ofdev, udc);
2588 "%s USB controller initialized as device\n",
2589 (udc->soc_type == PORT_QE) ? "QE" : "CPM");
2593 free_irq(udc->usb_irq, udc);
2595 irq_dispose_mapping(udc->usb_irq);
2598 dma_unmap_single(udc->gadget.dev.parent,
2601 udc->nullp = DMA_ADDR_INVALID;
2603 dma_sync_single_for_cpu(udc->gadget.dev.parent,
2609 cpm_muram_free(cpm_muram_offset(ep->rxbase));
2611 kfree(ep->rxbuffer);
2614 iounmap(udc->usb_regs);
2621 static int qe_udc_suspend(struct platform_device *dev, pm_message_t state)
2626 static int qe_udc_resume(struct platform_device *dev)
2632 static int qe_udc_remove(struct platform_device *ofdev)
2634 struct qe_udc *udc = platform_get_drvdata(ofdev);
2637 DECLARE_COMPLETION_ONSTACK(done);
2639 usb_del_gadget_udc(&udc->gadget);
2642 tasklet_disable(&udc->rx_tasklet);
2645 dma_unmap_single(udc->gadget.dev.parent,
2648 udc->nullp = DMA_ADDR_INVALID;
2650 dma_sync_single_for_cpu(udc->gadget.dev.parent,
2656 cpm_muram_free(cpm_muram_offset(ep->rxbase));
2657 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (USB_BDRING_LEN + 1);
2661 dma_unmap_single(udc->gadget.dev.parent,
2664 ep->rxbuf_d = DMA_ADDR_INVALID;
2666 dma_sync_single_for_cpu(udc->gadget.dev.parent,
2671 kfree(ep->rxbuffer);
2674 free_irq(udc->usb_irq, udc);
2675 irq_dispose_mapping(udc->usb_irq);
2677 tasklet_kill(&udc->rx_tasklet);
2679 iounmap(udc->usb_regs);
2681 /* wait for release() of gadget.dev to free udc */
2682 wait_for_completion(&done);
2687 /*-------------------------------------------------------------------------*/
2688 static const struct of_device_id qe_udc_match[] = {
2690 .compatible = "fsl,mpc8323-qe-usb",
2691 .data = (void *)PORT_QE,
2694 .compatible = "fsl,mpc8360-qe-usb",
2695 .data = (void *)PORT_QE,
2698 .compatible = "fsl,mpc8272-cpm-usb",
2699 .data = (void *)PORT_CPM,
2704 MODULE_DEVICE_TABLE(of, qe_udc_match);
2706 static struct platform_driver udc_driver = {
2708 .name = driver_name,
2709 .of_match_table = qe_udc_match,
2711 .probe = qe_udc_probe,
2712 .remove = qe_udc_remove,
2714 .suspend = qe_udc_suspend,
2715 .resume = qe_udc_resume,
2719 module_platform_driver(udc_driver);
2721 MODULE_DESCRIPTION(DRIVER_DESC);
2722 MODULE_AUTHOR(DRIVER_AUTHOR);
2723 MODULE_LICENSE("GPL");