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>
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 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
);
424 if (ep
->rxframe
== NULL
) {
425 dev_err(ep
->udc
->dev
, "malloc rxframe failed\n");
429 qe_frame_init(ep
->rxframe
);
431 if (ep
->dir
== USB_DIR_OUT
)
432 bdring_len
= USB_BDRING_LEN_RX
;
434 bdring_len
= USB_BDRING_LEN
;
436 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (bdring_len
+ 1);
437 ep
->rxbuffer
= kzalloc(size
, GFP_ATOMIC
);
438 if (ep
->rxbuffer
== NULL
) {
439 dev_err(ep
->udc
->dev
, "malloc rxbuffer failed,size=%d\n",
445 ep
->rxbuf_d
= virt_to_phys((void *)ep
->rxbuffer
);
446 if (ep
->rxbuf_d
== DMA_ADDR_INVALID
) {
447 ep
->rxbuf_d
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
453 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
459 size
= ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2;
461 tmp
= (u32
)(((tmp
>> 2) << 2) + 4);
463 for (i
= 0; i
< bdring_len
- 1; i
++) {
464 out_be32(&bd
->buf
, tmp
);
465 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
));
469 out_be32(&bd
->buf
, tmp
);
470 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
| R_W
));
475 static int qe_ep_register_init(struct qe_udc
*udc
, unsigned char pipe_num
)
477 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
478 struct usb_ep_para __iomem
*epparam
;
483 epparam
= udc
->ep_param
[pipe_num
];
486 logepnum
= (ep
->ep
.desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
487 usep
|= (logepnum
<< USB_EPNUM_SHIFT
);
489 switch (ep
->ep
.desc
->bmAttributes
& 0x03) {
490 case USB_ENDPOINT_XFER_BULK
:
491 usep
|= USB_TRANS_BULK
;
493 case USB_ENDPOINT_XFER_ISOC
:
494 usep
|= USB_TRANS_ISO
;
496 case USB_ENDPOINT_XFER_INT
:
497 usep
|= USB_TRANS_INT
;
500 usep
|= USB_TRANS_CTR
;
506 usep
|= USB_THS_IGNORE_IN
;
509 usep
|= USB_RHS_IGNORE_OUT
;
514 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], usep
);
517 out_8(&epparam
->rbmr
, rtfcr
);
518 out_8(&epparam
->tbmr
, rtfcr
);
520 tmp
= (u16
)(ep
->ep
.maxpacket
+ USB_CRC_SIZE
);
521 /* MRBLR must be divisble by 4 */
522 tmp
= (u16
)(((tmp
>> 2) << 2) + 4);
523 out_be16(&epparam
->mrblr
, tmp
);
528 static int qe_ep_init(struct qe_udc
*udc
,
529 unsigned char pipe_num
,
530 const struct usb_endpoint_descriptor
*desc
)
532 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
537 max
= usb_endpoint_maxp(desc
);
539 /* check the max package size validate for this endpoint */
540 /* Refer to USB2.0 spec table 9-13,
543 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
544 case USB_ENDPOINT_XFER_BULK
:
545 if (strstr(ep
->ep
.name
, "-iso")
546 || strstr(ep
->ep
.name
, "-int"))
548 switch (udc
->gadget
.speed
) {
550 if ((max
== 128) || (max
== 256) || (max
== 512))
566 case USB_ENDPOINT_XFER_INT
:
567 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
569 switch (udc
->gadget
.speed
) {
582 case USB_ENDPOINT_XFER_ISOC
:
583 if (strstr(ep
->ep
.name
, "-bulk")
584 || strstr(ep
->ep
.name
, "-int"))
586 switch (udc
->gadget
.speed
) {
597 case USB_ENDPOINT_XFER_CONTROL
:
598 if (strstr(ep
->ep
.name
, "-iso")
599 || strstr(ep
->ep
.name
, "-int"))
601 switch (udc
->gadget
.speed
) {
636 spin_lock_irqsave(&udc
->lock
, flags
);
638 /* initialize ep structure */
639 ep
->ep
.maxpacket
= max
;
640 ep
->tm
= (u8
)(desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
646 ep
->dir
= USB_DIR_BOTH
;
647 udc
->ep0_dir
= USB_DIR_OUT
;
648 udc
->ep0_state
= WAIT_FOR_SETUP
;
650 switch (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) {
652 ep
->dir
= USB_DIR_OUT
;
655 ep
->dir
= USB_DIR_IN
;
661 /* hardware special operation */
662 qe_ep_bd_init(udc
, pipe_num
);
663 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_OUT
)) {
664 reval
= qe_ep_rxbd_update(ep
);
669 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_IN
)) {
670 ep
->txframe
= kmalloc(sizeof(*ep
->txframe
), GFP_ATOMIC
);
671 if (ep
->txframe
== NULL
) {
672 dev_err(udc
->dev
, "malloc txframe failed\n");
675 qe_frame_init(ep
->txframe
);
678 qe_ep_register_init(udc
, pipe_num
);
680 /* Now HW will be NAKing transfers to that EP,
681 * until a buffer is queued to it. */
682 spin_unlock_irqrestore(&udc
->lock
, flags
);
689 spin_unlock_irqrestore(&udc
->lock
, flags
);
691 dev_err(udc
->dev
, "failed to initialize %s\n", ep
->ep
.name
);
695 static inline void qe_usb_enable(struct qe_udc
*udc
)
697 setbits8(&udc
->usb_regs
->usb_usmod
, USB_MODE_EN
);
700 static inline void qe_usb_disable(struct qe_udc
*udc
)
702 clrbits8(&udc
->usb_regs
->usb_usmod
, USB_MODE_EN
);
705 /*----------------------------------------------------------------------------*
706 * USB and EP basic manipulate function end *
707 *----------------------------------------------------------------------------*/
710 /******************************************************************************
711 UDC transmit and receive process
712 ******************************************************************************/
713 static void recycle_one_rxbd(struct qe_ep
*ep
)
717 bdstatus
= in_be32((u32 __iomem
*)ep
->e_rxbd
);
718 bdstatus
= R_I
| R_E
| (bdstatus
& R_W
);
719 out_be32((u32 __iomem
*)ep
->e_rxbd
, bdstatus
);
722 ep
->e_rxbd
= ep
->rxbase
;
727 static void recycle_rxbds(struct qe_ep
*ep
, unsigned char stopatnext
)
730 struct qe_bd __iomem
*bd
, *nextbd
;
731 unsigned char stop
= 0;
735 bdstatus
= in_be32((u32 __iomem
*)bd
);
737 while (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
) && !stop
) {
738 bdstatus
= R_E
| R_I
| (bdstatus
& R_W
);
739 out_be32((u32 __iomem
*)bd
, bdstatus
);
746 bdstatus
= in_be32((u32 __iomem
*)bd
);
747 if (stopatnext
&& (bd
== nextbd
))
754 static void ep_recycle_rxbds(struct qe_ep
*ep
)
756 struct qe_bd __iomem
*bd
= ep
->n_rxbd
;
758 u8 epnum
= ep
->epnum
;
759 struct qe_udc
*udc
= ep
->udc
;
761 bdstatus
= in_be32((u32 __iomem
*)bd
);
762 if (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
)) {
764 ((in_be16(&udc
->ep_param
[epnum
]->rbptr
) -
765 in_be16(&udc
->ep_param
[epnum
]->rbase
))
767 bdstatus
= in_be32((u32 __iomem
*)bd
);
775 recycle_rxbds(ep
, 0);
776 ep
->e_rxbd
= ep
->n_rxbd
;
778 recycle_rxbds(ep
, 1);
780 if (in_be16(&udc
->usb_regs
->usb_usber
) & USB_E_BSY_MASK
)
781 out_be16(&udc
->usb_regs
->usb_usber
, USB_E_BSY_MASK
);
783 if (ep
->has_data
<= 0 && (!list_empty(&ep
->queue
)))
789 static void setup_received_handle(struct qe_udc
*udc
,
790 struct usb_ctrlrequest
*setup
);
791 static int qe_ep_rxframe_handle(struct qe_ep
*ep
);
792 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
);
793 /* when BD PID is setup, handle the packet */
794 static int ep0_setup_handle(struct qe_udc
*udc
)
796 struct qe_ep
*ep
= &udc
->eps
[0];
797 struct qe_frame
*pframe
;
801 pframe
= ep
->rxframe
;
802 if ((frame_get_info(pframe
) & PID_SETUP
)
803 && (udc
->ep0_state
== WAIT_FOR_SETUP
)) {
804 fsize
= frame_get_length(pframe
);
805 if (unlikely(fsize
!= 8))
807 cp
= (u8
*)&udc
->local_setup_buff
;
808 memcpy(cp
, pframe
->data
, fsize
);
811 /* handle the usb command base on the usb_ctrlrequest */
812 setup_received_handle(udc
, &udc
->local_setup_buff
);
818 static int qe_ep0_rx(struct qe_udc
*udc
)
820 struct qe_ep
*ep
= &udc
->eps
[0];
821 struct qe_frame
*pframe
;
822 struct qe_bd __iomem
*bd
;
823 u32 bdstatus
, length
;
826 pframe
= ep
->rxframe
;
828 if (ep
->dir
== USB_DIR_IN
) {
829 dev_err(udc
->dev
, "ep0 not a control endpoint\n");
834 bdstatus
= in_be32((u32 __iomem
*)bd
);
835 length
= bdstatus
& BD_LENGTH_MASK
;
837 while (!(bdstatus
& R_E
) && length
) {
838 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
839 && !(bdstatus
& R_ERROR
)) {
840 if (length
== USB_CRC_SIZE
) {
841 udc
->ep0_state
= WAIT_FOR_SETUP
;
843 "receive a ZLP in status phase\n");
845 qe_frame_clean(pframe
);
846 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
847 frame_set_data(pframe
, (u8
*)vaddr
);
848 frame_set_length(pframe
,
849 (length
- USB_CRC_SIZE
));
850 frame_set_status(pframe
, FRAME_OK
);
851 switch (bdstatus
& R_PID
) {
853 frame_set_info(pframe
, PID_SETUP
);
856 frame_set_info(pframe
, PID_DATA1
);
859 frame_set_info(pframe
, PID_DATA0
);
863 if ((bdstatus
& R_PID
) == R_PID_SETUP
)
864 ep0_setup_handle(udc
);
866 qe_ep_rxframe_handle(ep
);
869 dev_err(udc
->dev
, "The receive frame with error!\n");
872 /* note: don't clear the rxbd's buffer address */
873 recycle_one_rxbd(ep
);
881 bdstatus
= in_be32((u32 __iomem
*)bd
);
882 length
= bdstatus
& BD_LENGTH_MASK
;
891 static int qe_ep_rxframe_handle(struct qe_ep
*ep
)
893 struct qe_frame
*pframe
;
899 pframe
= ep
->rxframe
;
901 if (frame_get_info(pframe
) & PID_DATA1
)
904 if (framepid
!= ep
->data01
) {
905 dev_err(ep
->udc
->dev
, "the data01 error!\n");
909 fsize
= frame_get_length(pframe
);
910 if (list_empty(&ep
->queue
)) {
911 dev_err(ep
->udc
->dev
, "the %s have no requeue!\n", ep
->name
);
913 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
915 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
917 memcpy(cp
, pframe
->data
, fsize
);
918 req
->req
.actual
+= fsize
;
919 if ((fsize
< ep
->ep
.maxpacket
) ||
920 (req
->req
.actual
>= req
->req
.length
)) {
922 ep0_req_complete(ep
->udc
, req
);
925 if (list_empty(&ep
->queue
) && ep
->epnum
!= 0)
931 qe_ep_toggledata01(ep
);
936 static void ep_rx_tasklet(unsigned long data
)
938 struct qe_udc
*udc
= (struct qe_udc
*)data
;
940 struct qe_frame
*pframe
;
941 struct qe_bd __iomem
*bd
;
943 u32 bdstatus
, length
;
946 spin_lock_irqsave(&udc
->lock
, flags
);
948 for (i
= 1; i
< USB_MAX_ENDPOINTS
; i
++) {
951 if (ep
->dir
== USB_DIR_IN
|| ep
->enable_tasklet
== 0) {
953 "This is a transmit ep or disable tasklet!\n");
957 pframe
= ep
->rxframe
;
959 bdstatus
= in_be32((u32 __iomem
*)bd
);
960 length
= bdstatus
& BD_LENGTH_MASK
;
962 while (!(bdstatus
& R_E
) && length
) {
963 if (list_empty(&ep
->queue
)) {
966 "The rxep have noreq %d\n",
971 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
972 && !(bdstatus
& R_ERROR
)) {
973 qe_frame_clean(pframe
);
974 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
975 frame_set_data(pframe
, (u8
*)vaddr
);
976 frame_set_length(pframe
,
977 (length
- USB_CRC_SIZE
));
978 frame_set_status(pframe
, FRAME_OK
);
979 switch (bdstatus
& R_PID
) {
981 frame_set_info(pframe
, PID_DATA1
);
984 frame_set_info(pframe
, PID_SETUP
);
987 frame_set_info(pframe
, PID_DATA0
);
990 /* handle the rx frame */
991 qe_ep_rxframe_handle(ep
);
994 "error in received frame\n");
996 /* note: don't clear the rxbd's buffer address */
997 /*clear the length */
998 out_be32((u32 __iomem
*)bd
, bdstatus
& BD_STATUS_MASK
);
1000 if (!(ep
->localnack
))
1001 recycle_one_rxbd(ep
);
1009 bdstatus
= in_be32((u32 __iomem
*)bd
);
1010 length
= bdstatus
& BD_LENGTH_MASK
;
1016 ep_recycle_rxbds(ep
);
1018 ep
->enable_tasklet
= 0;
1021 spin_unlock_irqrestore(&udc
->lock
, flags
);
1024 static int qe_ep_rx(struct qe_ep
*ep
)
1027 struct qe_frame
*pframe
;
1028 struct qe_bd __iomem
*bd
;
1029 u16 swoffs
, ucoffs
, emptybds
;
1032 pframe
= ep
->rxframe
;
1034 if (ep
->dir
== USB_DIR_IN
) {
1035 dev_err(udc
->dev
, "transmit ep in rx function\n");
1041 swoffs
= (u16
)(bd
- ep
->rxbase
);
1042 ucoffs
= (u16
)((in_be16(&udc
->ep_param
[ep
->epnum
]->rbptr
) -
1043 in_be16(&udc
->ep_param
[ep
->epnum
]->rbase
)) >> 3);
1044 if (swoffs
< ucoffs
)
1045 emptybds
= USB_BDRING_LEN_RX
- ucoffs
+ swoffs
;
1047 emptybds
= swoffs
- ucoffs
;
1049 if (emptybds
< MIN_EMPTY_BDS
) {
1052 dev_vdbg(udc
->dev
, "%d empty bds, send NACK\n", emptybds
);
1054 ep
->has_data
= USB_BDRING_LEN_RX
- emptybds
;
1056 if (list_empty(&ep
->queue
)) {
1058 dev_vdbg(udc
->dev
, "The rxep have no req queued with %d BDs\n",
1063 tasklet_schedule(&udc
->rx_tasklet
);
1064 ep
->enable_tasklet
= 1;
1069 /* send data from a frame, no matter what tx_req */
1070 static int qe_ep_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1072 struct qe_udc
*udc
= ep
->udc
;
1073 struct qe_bd __iomem
*bd
;
1075 u32 bdstatus
, pidmask
;
1078 if (ep
->dir
== USB_DIR_OUT
) {
1079 dev_err(udc
->dev
, "receive ep passed to tx function\n");
1083 /* Disable the Tx interrupt */
1084 saveusbmr
= in_be16(&udc
->usb_regs
->usb_usbmr
);
1085 out_be16(&udc
->usb_regs
->usb_usbmr
,
1086 saveusbmr
& ~(USB_E_TXB_MASK
| USB_E_TXE_MASK
));
1089 bdstatus
= in_be32((u32 __iomem
*)bd
);
1091 if (!(bdstatus
& (T_R
| BD_LENGTH_MASK
))) {
1092 if (frame_get_length(frame
) == 0) {
1093 frame_set_data(frame
, udc
->nullbuf
);
1094 frame_set_length(frame
, 2);
1095 frame
->info
|= (ZLP
| NO_CRC
);
1096 dev_vdbg(udc
->dev
, "the frame size = 0\n");
1098 paddr
= virt_to_phys((void *)frame
->data
);
1099 out_be32(&bd
->buf
, paddr
);
1100 bdstatus
= (bdstatus
&T_W
);
1101 if (!(frame_get_info(frame
) & NO_CRC
))
1102 bdstatus
|= T_R
| T_I
| T_L
| T_TC
1103 | frame_get_length(frame
);
1105 bdstatus
|= T_R
| T_I
| T_L
| frame_get_length(frame
);
1107 /* if the packet is a ZLP in status phase */
1108 if ((ep
->epnum
== 0) && (udc
->ep0_state
== DATA_STATE_NEED_ZLP
))
1112 pidmask
= T_PID_DATA1
;
1113 frame
->info
|= PID_DATA1
;
1115 pidmask
= T_PID_DATA0
;
1116 frame
->info
|= PID_DATA0
;
1119 bdstatus
|= pidmask
;
1120 out_be32((u32 __iomem
*)bd
, bdstatus
);
1121 qe_ep_filltxfifo(ep
);
1123 /* enable the TX interrupt */
1124 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1126 qe_ep_toggledata01(ep
);
1128 ep
->n_txbd
= ep
->txbase
;
1134 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1135 dev_vdbg(udc
->dev
, "The tx bd is not ready!\n");
1140 /* when a bd was transmitted, the function can
1141 * handle the tx_req, not include ep0 */
1142 static int txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1144 if (ep
->tx_req
!= NULL
) {
1145 struct qe_req
*req
= ep
->tx_req
;
1146 unsigned zlp
= 0, last_len
= 0;
1148 last_len
= min_t(unsigned, req
->req
.length
- ep
->sent
,
1152 int asent
= ep
->last
;
1159 /* zlp needed when req->re.zero is set */
1160 if (req
->req
.zero
) {
1161 if (last_len
== 0 ||
1162 (req
->req
.length
% ep
->ep
.maxpacket
) != 0)
1169 /* a request already were transmitted completely */
1170 if (((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) && !zlp
) {
1171 done(ep
, ep
->tx_req
, 0);
1178 /* we should gain a new tx_req fot this endpoint */
1179 if (ep
->tx_req
== NULL
) {
1180 if (!list_empty(&ep
->queue
)) {
1181 ep
->tx_req
= list_entry(ep
->queue
.next
, struct qe_req
,
1191 /* give a frame and a tx_req, send some data */
1192 static int qe_usb_senddata(struct qe_ep
*ep
, struct qe_frame
*frame
)
1197 qe_frame_clean(frame
);
1198 size
= min_t(u32
, (ep
->tx_req
->req
.length
- ep
->sent
),
1200 buf
= (u8
*)ep
->tx_req
->req
.buf
+ ep
->sent
;
1203 ep
->tx_req
->req
.actual
+= size
;
1204 frame_set_data(frame
, buf
);
1205 frame_set_length(frame
, size
);
1206 frame_set_status(frame
, FRAME_OK
);
1207 frame_set_info(frame
, 0);
1208 return qe_ep_tx(ep
, frame
);
1213 /* give a frame struct,send a ZLP */
1214 static int sendnulldata(struct qe_ep
*ep
, struct qe_frame
*frame
, uint infor
)
1216 struct qe_udc
*udc
= ep
->udc
;
1221 qe_frame_clean(frame
);
1222 frame_set_data(frame
, (u8
*)udc
->nullbuf
);
1223 frame_set_length(frame
, 2);
1224 frame_set_status(frame
, FRAME_OK
);
1225 frame_set_info(frame
, (ZLP
| NO_CRC
| infor
));
1227 return qe_ep_tx(ep
, frame
);
1230 static int frame_create_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1232 struct qe_req
*req
= ep
->tx_req
;
1238 if ((req
->req
.length
- ep
->sent
) > 0)
1239 reval
= qe_usb_senddata(ep
, frame
);
1241 reval
= sendnulldata(ep
, frame
, 0);
1246 /* if direction is DIR_IN, the status is Device->Host
1247 * if direction is DIR_OUT, the status transaction is Device<-Host
1248 * in status phase, udc create a request and gain status */
1249 static int ep0_prime_status(struct qe_udc
*udc
, int direction
)
1252 struct qe_ep
*ep
= &udc
->eps
[0];
1254 if (direction
== USB_DIR_IN
) {
1255 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
1256 udc
->ep0_dir
= USB_DIR_IN
;
1257 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1259 udc
->ep0_dir
= USB_DIR_OUT
;
1260 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1266 /* a request complete in ep0, whether gadget request or udc request */
1267 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
)
1269 struct qe_ep
*ep
= &udc
->eps
[0];
1270 /* because usb and ep's status already been set in ch9setaddress() */
1272 switch (udc
->ep0_state
) {
1273 case DATA_STATE_XMIT
:
1275 /* receive status phase */
1276 if (ep0_prime_status(udc
, USB_DIR_OUT
))
1280 case DATA_STATE_NEED_ZLP
:
1282 udc
->ep0_state
= WAIT_FOR_SETUP
;
1285 case DATA_STATE_RECV
:
1287 /* send status phase */
1288 if (ep0_prime_status(udc
, USB_DIR_IN
))
1292 case WAIT_FOR_OUT_STATUS
:
1294 udc
->ep0_state
= WAIT_FOR_SETUP
;
1297 case WAIT_FOR_SETUP
:
1298 dev_vdbg(udc
->dev
, "Unexpected interrupt\n");
1307 static int ep0_txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1309 struct qe_req
*tx_req
= NULL
;
1310 struct qe_frame
*frame
= ep
->txframe
;
1312 if ((frame_get_info(frame
) & (ZLP
| NO_REQ
)) == (ZLP
| NO_REQ
)) {
1314 ep
->udc
->ep0_state
= WAIT_FOR_SETUP
;
1316 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1320 tx_req
= ep
->tx_req
;
1321 if (tx_req
!= NULL
) {
1323 int asent
= ep
->last
;
1330 /* a request already were transmitted completely */
1331 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1332 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1333 ep0_req_complete(ep
->udc
, ep
->tx_req
);
1339 dev_vdbg(ep
->udc
->dev
, "the ep0_controller have no req\n");
1345 static int ep0_txframe_handle(struct qe_ep
*ep
)
1347 /* if have error, transmit again */
1348 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1349 qe_ep_flushtxfifo(ep
);
1350 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1351 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1356 ep0_txcomplete(ep
, 1);
1358 ep0_txcomplete(ep
, 0);
1360 frame_create_tx(ep
, ep
->txframe
);
1364 static int qe_ep0_txconf(struct qe_ep
*ep
)
1366 struct qe_bd __iomem
*bd
;
1367 struct qe_frame
*pframe
;
1371 bdstatus
= in_be32((u32 __iomem
*)bd
);
1372 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1373 pframe
= ep
->txframe
;
1375 /* clear and recycle the BD */
1376 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1377 out_be32(&bd
->buf
, 0);
1379 ep
->c_txbd
= ep
->txbase
;
1383 if (ep
->c_txbd
== ep
->n_txbd
) {
1384 if (bdstatus
& DEVICE_T_ERROR
) {
1385 frame_set_status(pframe
, FRAME_ERROR
);
1386 if (bdstatus
& T_TO
)
1387 pframe
->status
|= TX_ER_TIMEOUT
;
1388 if (bdstatus
& T_UN
)
1389 pframe
->status
|= TX_ER_UNDERUN
;
1391 ep0_txframe_handle(ep
);
1395 bdstatus
= in_be32((u32 __iomem
*)bd
);
1401 static int ep_txframe_handle(struct qe_ep
*ep
)
1403 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1404 qe_ep_flushtxfifo(ep
);
1405 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1406 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1415 frame_create_tx(ep
, ep
->txframe
); /* send the data */
1419 /* confirm the already trainsmited bd */
1420 static int qe_ep_txconf(struct qe_ep
*ep
)
1422 struct qe_bd __iomem
*bd
;
1423 struct qe_frame
*pframe
= NULL
;
1425 unsigned char breakonrxinterrupt
= 0;
1428 bdstatus
= in_be32((u32 __iomem
*)bd
);
1429 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1430 pframe
= ep
->txframe
;
1431 if (bdstatus
& DEVICE_T_ERROR
) {
1432 frame_set_status(pframe
, FRAME_ERROR
);
1433 if (bdstatus
& T_TO
)
1434 pframe
->status
|= TX_ER_TIMEOUT
;
1435 if (bdstatus
& T_UN
)
1436 pframe
->status
|= TX_ER_UNDERUN
;
1439 /* clear and recycle the BD */
1440 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1441 out_be32(&bd
->buf
, 0);
1443 ep
->c_txbd
= ep
->txbase
;
1447 /* handle the tx frame */
1448 ep_txframe_handle(ep
);
1450 bdstatus
= in_be32((u32 __iomem
*)bd
);
1452 if (breakonrxinterrupt
)
1458 /* Add a request in queue, and try to transmit a packet */
1459 static int ep_req_send(struct qe_ep
*ep
, struct qe_req
*req
)
1463 if (ep
->tx_req
== NULL
) {
1466 txcomplete(ep
, 0); /* can gain a new tx_req */
1467 reval
= frame_create_tx(ep
, ep
->txframe
);
1472 /* Maybe this is a good ideal */
1473 static int ep_req_rx(struct qe_ep
*ep
, struct qe_req
*req
)
1475 struct qe_udc
*udc
= ep
->udc
;
1476 struct qe_frame
*pframe
= NULL
;
1477 struct qe_bd __iomem
*bd
;
1478 u32 bdstatus
, length
;
1484 if (list_empty(&ep
->queue
)) {
1485 dev_vdbg(udc
->dev
, "the req already finish!\n");
1488 pframe
= ep
->rxframe
;
1491 bdstatus
= in_be32((u32 __iomem
*)bd
);
1492 length
= bdstatus
& BD_LENGTH_MASK
;
1494 while (!(bdstatus
& R_E
) && length
) {
1497 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
1498 && !(bdstatus
& R_ERROR
)) {
1499 qe_frame_clean(pframe
);
1500 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
1501 frame_set_data(pframe
, (u8
*)vaddr
);
1502 frame_set_length(pframe
, (length
- USB_CRC_SIZE
));
1503 frame_set_status(pframe
, FRAME_OK
);
1504 switch (bdstatus
& R_PID
) {
1506 frame_set_info(pframe
, PID_DATA1
); break;
1508 frame_set_info(pframe
, PID_DATA0
); break;
1510 /* handle the rx frame */
1512 if (frame_get_info(pframe
) & PID_DATA1
)
1517 if (framepid
!= ep
->data01
) {
1518 dev_vdbg(udc
->dev
, "the data01 error!\n");
1520 fsize
= frame_get_length(pframe
);
1522 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
1524 memcpy(cp
, pframe
->data
, fsize
);
1525 req
->req
.actual
+= fsize
;
1526 if ((fsize
< ep
->ep
.maxpacket
)
1527 || (req
->req
.actual
>=
1531 if (list_empty(&ep
->queue
))
1535 qe_ep_toggledata01(ep
);
1538 dev_err(udc
->dev
, "The receive frame with error!\n");
1541 /* note: don't clear the rxbd's buffer address *
1542 * only Clear the length */
1543 out_be32((u32 __iomem
*)bd
, (bdstatus
& BD_STATUS_MASK
));
1552 bdstatus
= in_be32((u32 __iomem
*)bd
);
1553 length
= bdstatus
& BD_LENGTH_MASK
;
1557 ep_recycle_rxbds(ep
);
1562 /* only add the request in queue */
1563 static int ep_req_receive(struct qe_ep
*ep
, struct qe_req
*req
)
1565 if (ep
->state
== EP_STATE_NACK
) {
1566 if (ep
->has_data
<= 0) {
1567 /* Enable rx and unmask rx interrupt */
1570 /* Copy the exist BD data */
1578 /********************************************************************
1579 Internal Used Function End
1580 ********************************************************************/
1582 /*-----------------------------------------------------------------------
1583 Endpoint Management Functions For Gadget
1584 -----------------------------------------------------------------------*/
1585 static int qe_ep_enable(struct usb_ep
*_ep
,
1586 const struct usb_endpoint_descriptor
*desc
)
1591 unsigned char epnum
;
1593 ep
= container_of(_ep
, struct qe_ep
, ep
);
1595 /* catch various bogus parameters */
1596 if (!_ep
|| !desc
|| _ep
->name
== ep_name
[0] ||
1597 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
))
1601 if (!udc
->driver
|| (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
))
1604 epnum
= (u8
)desc
->bEndpointAddress
& 0xF;
1606 retval
= qe_ep_init(udc
, epnum
, desc
);
1608 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1609 dev_dbg(udc
->dev
, "enable ep%d failed\n", ep
->epnum
);
1612 dev_dbg(udc
->dev
, "enable ep%d successful\n", ep
->epnum
);
1616 static int qe_ep_disable(struct usb_ep
*_ep
)
1620 unsigned long flags
;
1623 ep
= container_of(_ep
, struct qe_ep
, ep
);
1626 if (!_ep
|| !ep
->ep
.desc
) {
1627 dev_dbg(udc
->dev
, "%s not enabled\n", _ep
? ep
->ep
.name
: NULL
);
1631 spin_lock_irqsave(&udc
->lock
, flags
);
1632 /* Nuke all pending requests (does flush) */
1633 nuke(ep
, -ESHUTDOWN
);
1637 qe_ep_reset(udc
, ep
->epnum
);
1638 spin_unlock_irqrestore(&udc
->lock
, flags
);
1640 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1642 if (ep
->dir
== USB_DIR_OUT
)
1643 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1644 (USB_BDRING_LEN_RX
+ 1);
1646 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1647 (USB_BDRING_LEN
+ 1);
1649 if (ep
->dir
!= USB_DIR_IN
) {
1652 dma_unmap_single(udc
->gadget
.dev
.parent
,
1655 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
1657 dma_sync_single_for_cpu(
1658 udc
->gadget
.dev
.parent
,
1662 kfree(ep
->rxbuffer
);
1665 if (ep
->dir
!= USB_DIR_OUT
)
1668 dev_dbg(udc
->dev
, "disabled %s OK\n", _ep
->name
);
1672 static struct usb_request
*qe_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
1676 req
= kzalloc(sizeof(*req
), gfp_flags
);
1680 req
->req
.dma
= DMA_ADDR_INVALID
;
1682 INIT_LIST_HEAD(&req
->queue
);
1687 static void qe_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1691 req
= container_of(_req
, struct qe_req
, req
);
1697 static int __qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1699 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1700 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1705 /* catch various bogus parameters */
1706 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
1707 || !list_empty(&req
->queue
)) {
1708 dev_dbg(udc
->dev
, "bad params\n");
1711 if (!_ep
|| (!ep
->ep
.desc
&& ep_index(ep
))) {
1712 dev_dbg(udc
->dev
, "bad ep\n");
1716 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1721 /* map virtual address to hardware */
1722 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
1723 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
1731 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
1732 req
->req
.dma
, req
->req
.length
,
1739 req
->req
.status
= -EINPROGRESS
;
1740 req
->req
.actual
= 0;
1742 list_add_tail(&req
->queue
, &ep
->queue
);
1743 dev_vdbg(udc
->dev
, "gadget have request in %s! %d\n",
1744 ep
->name
, req
->req
.length
);
1746 /* push the request to device */
1748 reval
= ep_req_send(ep
, req
);
1751 if (ep_index(ep
) == 0 && req
->req
.length
> 0) {
1753 udc
->ep0_state
= DATA_STATE_XMIT
;
1755 udc
->ep0_state
= DATA_STATE_RECV
;
1758 if (ep
->dir
== USB_DIR_OUT
)
1759 reval
= ep_req_receive(ep
, req
);
1764 /* queues (submits) an I/O request to an endpoint */
1765 static int qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1768 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1769 struct qe_udc
*udc
= ep
->udc
;
1770 unsigned long flags
;
1773 spin_lock_irqsave(&udc
->lock
, flags
);
1774 ret
= __qe_ep_queue(_ep
, _req
);
1775 spin_unlock_irqrestore(&udc
->lock
, flags
);
1779 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1780 static int qe_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1782 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1784 unsigned long flags
;
1789 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1791 /* make sure it's actually queued on this endpoint */
1792 list_for_each_entry(req
, &ep
->queue
, queue
) {
1793 if (&req
->req
== _req
)
1797 if (&req
->req
!= _req
) {
1798 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1802 done(ep
, req
, -ECONNRESET
);
1804 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1808 /*-----------------------------------------------------------------
1809 * modify the endpoint halt feature
1810 * @ep: the non-isochronous endpoint being stalled
1811 * @value: 1--set halt 0--clear halt
1812 * Returns zero, or a negative error code.
1813 *----------------------------------------------------------------*/
1814 static int qe_ep_set_halt(struct usb_ep
*_ep
, int value
)
1817 unsigned long flags
;
1818 int status
= -EOPNOTSUPP
;
1821 ep
= container_of(_ep
, struct qe_ep
, ep
);
1822 if (!_ep
|| !ep
->ep
.desc
) {
1828 /* Attempt to halt IN ep will fail if any transfer requests
1829 * are still queue */
1830 if (value
&& ep_is_in(ep
) && !list_empty(&ep
->queue
)) {
1836 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1837 qe_eptx_stall_change(ep
, value
);
1838 qe_eprx_stall_change(ep
, value
);
1839 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1841 if (ep
->epnum
== 0) {
1842 udc
->ep0_state
= WAIT_FOR_SETUP
;
1846 /* set data toggle to DATA0 on clear halt */
1850 dev_vdbg(udc
->dev
, "%s %s halt stat %d\n", ep
->ep
.name
,
1851 value
? "set" : "clear", status
);
1856 static struct usb_ep_ops qe_ep_ops
= {
1857 .enable
= qe_ep_enable
,
1858 .disable
= qe_ep_disable
,
1860 .alloc_request
= qe_alloc_request
,
1861 .free_request
= qe_free_request
,
1863 .queue
= qe_ep_queue
,
1864 .dequeue
= qe_ep_dequeue
,
1866 .set_halt
= qe_ep_set_halt
,
1869 /*------------------------------------------------------------------------
1870 Gadget Driver Layer Operations
1871 ------------------------------------------------------------------------*/
1873 /* Get the current frame number */
1874 static int qe_get_frame(struct usb_gadget
*gadget
)
1876 struct qe_udc
*udc
= container_of(gadget
, struct qe_udc
, gadget
);
1879 tmp
= in_be16(&udc
->usb_param
->frame_n
);
1881 return tmp
& 0x07ff;
1885 static int fsl_qe_start(struct usb_gadget
*gadget
,
1886 struct usb_gadget_driver
*driver
);
1887 static int fsl_qe_stop(struct usb_gadget
*gadget
);
1889 /* defined in usb_gadget.h */
1890 static const struct usb_gadget_ops qe_gadget_ops
= {
1891 .get_frame
= qe_get_frame
,
1892 .udc_start
= fsl_qe_start
,
1893 .udc_stop
= fsl_qe_stop
,
1896 /*-------------------------------------------------------------------------
1897 USB ep0 Setup process in BUS Enumeration
1898 -------------------------------------------------------------------------*/
1899 static int udc_reset_ep_queue(struct qe_udc
*udc
, u8 pipe
)
1901 struct qe_ep
*ep
= &udc
->eps
[pipe
];
1903 nuke(ep
, -ECONNRESET
);
1908 static int reset_queues(struct qe_udc
*udc
)
1912 for (pipe
= 0; pipe
< USB_MAX_ENDPOINTS
; pipe
++)
1913 udc_reset_ep_queue(udc
, pipe
);
1915 /* report disconnect; the driver is already quiesced */
1916 spin_unlock(&udc
->lock
);
1917 usb_gadget_udc_reset(&udc
->gadget
, udc
->driver
);
1918 spin_lock(&udc
->lock
);
1923 static void ch9setaddress(struct qe_udc
*udc
, u16 value
, u16 index
,
1926 /* Save the new address to device struct */
1927 udc
->device_address
= (u8
) value
;
1928 /* Update usb state */
1929 udc
->usb_state
= USB_STATE_ADDRESS
;
1931 /* Status phase , send a ZLP */
1932 if (ep0_prime_status(udc
, USB_DIR_IN
))
1936 static void ownercomplete(struct usb_ep
*_ep
, struct usb_request
*_req
)
1938 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1940 req
->req
.buf
= NULL
;
1944 static void ch9getstatus(struct qe_udc
*udc
, u8 request_type
, u16 value
,
1945 u16 index
, u16 length
)
1953 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1954 /* Get device status */
1955 usb_status
= 1 << USB_DEVICE_SELF_POWERED
;
1956 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
1957 /* Get interface status */
1958 /* We don't have interface information in udc driver */
1960 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
1961 /* Get endpoint status */
1962 int pipe
= index
& USB_ENDPOINT_NUMBER_MASK
;
1963 struct qe_ep
*target_ep
= &udc
->eps
[pipe
];
1966 /* stall if endpoint doesn't exist */
1967 if (!target_ep
->ep
.desc
)
1970 usep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe
]);
1971 if (index
& USB_DIR_IN
) {
1972 if (target_ep
->dir
!= USB_DIR_IN
)
1974 if ((usep
& USB_THS_MASK
) == USB_THS_STALL
)
1975 usb_status
= 1 << USB_ENDPOINT_HALT
;
1977 if (target_ep
->dir
!= USB_DIR_OUT
)
1979 if ((usep
& USB_RHS_MASK
) == USB_RHS_STALL
)
1980 usb_status
= 1 << USB_ENDPOINT_HALT
;
1984 req
= container_of(qe_alloc_request(&ep
->ep
, GFP_KERNEL
),
1985 struct qe_req
, req
);
1986 req
->req
.length
= 2;
1987 req
->req
.buf
= udc
->statusbuf
;
1988 *(u16
*)req
->req
.buf
= cpu_to_le16(usb_status
);
1989 req
->req
.status
= -EINPROGRESS
;
1990 req
->req
.actual
= 0;
1991 req
->req
.complete
= ownercomplete
;
1993 udc
->ep0_dir
= USB_DIR_IN
;
1996 status
= __qe_ep_queue(&ep
->ep
, &req
->req
);
2001 dev_err(udc
->dev
, "Can't respond to getstatus request \n");
2005 /* only handle the setup request, suppose the device in normal status */
2006 static void setup_received_handle(struct qe_udc
*udc
,
2007 struct usb_ctrlrequest
*setup
)
2009 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2010 u16 wValue
= le16_to_cpu(setup
->wValue
);
2011 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
2012 u16 wLength
= le16_to_cpu(setup
->wLength
);
2014 /* clear the previous request in the ep0 */
2015 udc_reset_ep_queue(udc
, 0);
2017 if (setup
->bRequestType
& USB_DIR_IN
)
2018 udc
->ep0_dir
= USB_DIR_IN
;
2020 udc
->ep0_dir
= USB_DIR_OUT
;
2022 switch (setup
->bRequest
) {
2023 case USB_REQ_GET_STATUS
:
2024 /* Data+Status phase form udc */
2025 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
2026 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
2028 ch9getstatus(udc
, setup
->bRequestType
, wValue
, wIndex
,
2032 case USB_REQ_SET_ADDRESS
:
2033 /* Status phase from udc */
2034 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
2037 ch9setaddress(udc
, wValue
, wIndex
, wLength
);
2040 case USB_REQ_CLEAR_FEATURE
:
2041 case USB_REQ_SET_FEATURE
:
2042 /* Requests with no data phase, status phase from udc */
2043 if ((setup
->bRequestType
& USB_TYPE_MASK
)
2044 != USB_TYPE_STANDARD
)
2047 if ((setup
->bRequestType
& USB_RECIP_MASK
)
2048 == USB_RECIP_ENDPOINT
) {
2049 int pipe
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2052 if (wValue
!= 0 || wLength
!= 0
2053 || pipe
>= USB_MAX_ENDPOINTS
)
2055 ep
= &udc
->eps
[pipe
];
2057 spin_unlock(&udc
->lock
);
2058 qe_ep_set_halt(&ep
->ep
,
2059 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2061 spin_lock(&udc
->lock
);
2064 ep0_prime_status(udc
, USB_DIR_IN
);
2073 /* Data phase from gadget, status phase from udc */
2074 if (setup
->bRequestType
& USB_DIR_IN
) {
2075 udc
->ep0_state
= DATA_STATE_XMIT
;
2076 udc
->ep0_dir
= USB_DIR_IN
;
2078 udc
->ep0_state
= DATA_STATE_RECV
;
2079 udc
->ep0_dir
= USB_DIR_OUT
;
2081 spin_unlock(&udc
->lock
);
2082 if (udc
->driver
->setup(&udc
->gadget
,
2083 &udc
->local_setup_buff
) < 0)
2085 spin_lock(&udc
->lock
);
2087 /* No data phase, IN status from gadget */
2088 udc
->ep0_dir
= USB_DIR_IN
;
2089 spin_unlock(&udc
->lock
);
2090 if (udc
->driver
->setup(&udc
->gadget
,
2091 &udc
->local_setup_buff
) < 0)
2093 spin_lock(&udc
->lock
);
2094 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
2098 /*-------------------------------------------------------------------------
2099 USB Interrupt handlers
2100 -------------------------------------------------------------------------*/
2101 static void suspend_irq(struct qe_udc
*udc
)
2103 udc
->resume_state
= udc
->usb_state
;
2104 udc
->usb_state
= USB_STATE_SUSPENDED
;
2106 /* report suspend to the driver ,serial.c not support this*/
2107 if (udc
->driver
->suspend
)
2108 udc
->driver
->suspend(&udc
->gadget
);
2111 static void resume_irq(struct qe_udc
*udc
)
2113 udc
->usb_state
= udc
->resume_state
;
2114 udc
->resume_state
= 0;
2116 /* report resume to the driver , serial.c not support this*/
2117 if (udc
->driver
->resume
)
2118 udc
->driver
->resume(&udc
->gadget
);
2121 static void idle_irq(struct qe_udc
*udc
)
2125 usbs
= in_8(&udc
->usb_regs
->usb_usbs
);
2126 if (usbs
& USB_IDLE_STATUS_MASK
) {
2127 if ((udc
->usb_state
) != USB_STATE_SUSPENDED
)
2130 if (udc
->usb_state
== USB_STATE_SUSPENDED
)
2135 static int reset_irq(struct qe_udc
*udc
)
2139 if (udc
->usb_state
== USB_STATE_DEFAULT
)
2142 qe_usb_disable(udc
);
2143 out_8(&udc
->usb_regs
->usb_usadr
, 0);
2145 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2146 if (udc
->eps
[i
].init
)
2147 qe_ep_reset(udc
, i
);
2151 udc
->usb_state
= USB_STATE_DEFAULT
;
2152 udc
->ep0_state
= WAIT_FOR_SETUP
;
2153 udc
->ep0_dir
= USB_DIR_OUT
;
2158 static int bsy_irq(struct qe_udc
*udc
)
2163 static int txe_irq(struct qe_udc
*udc
)
2168 /* ep0 tx interrupt also in here */
2169 static int tx_irq(struct qe_udc
*udc
)
2172 struct qe_bd __iomem
*bd
;
2175 if ((udc
->usb_state
== USB_STATE_ADDRESS
)
2176 && (in_8(&udc
->usb_regs
->usb_usadr
) == 0))
2177 out_8(&udc
->usb_regs
->usb_usadr
, udc
->device_address
);
2179 for (i
= (USB_MAX_ENDPOINTS
-1); ((i
>= 0) && (res
== 0)); i
--) {
2181 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_OUT
)) {
2183 if (!(in_be32((u32 __iomem
*)bd
) & T_R
)
2184 && (in_be32(&bd
->buf
))) {
2185 /* confirm the transmitted bd */
2187 res
= qe_ep0_txconf(ep
);
2189 res
= qe_ep_txconf(ep
);
2197 /* setup packect's rx is handle in the function too */
2198 static void rx_irq(struct qe_udc
*udc
)
2201 struct qe_bd __iomem
*bd
;
2204 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2206 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_IN
)) {
2208 if (!(in_be32((u32 __iomem
*)bd
) & R_E
)
2209 && (in_be32(&bd
->buf
))) {
2210 if (ep
->epnum
== 0) {
2213 /*non-setup package receive*/
2221 static irqreturn_t
qe_udc_irq(int irq
, void *_udc
)
2223 struct qe_udc
*udc
= (struct qe_udc
*)_udc
;
2225 irqreturn_t status
= IRQ_NONE
;
2226 unsigned long flags
;
2228 spin_lock_irqsave(&udc
->lock
, flags
);
2230 irq_src
= in_be16(&udc
->usb_regs
->usb_usber
) &
2231 in_be16(&udc
->usb_regs
->usb_usbmr
);
2232 /* Clear notification bits */
2233 out_be16(&udc
->usb_regs
->usb_usber
, irq_src
);
2235 if (irq_src
& USB_E_IDLE_MASK
) {
2237 irq_src
&= ~USB_E_IDLE_MASK
;
2238 status
= IRQ_HANDLED
;
2241 if (irq_src
& USB_E_TXB_MASK
) {
2243 irq_src
&= ~USB_E_TXB_MASK
;
2244 status
= IRQ_HANDLED
;
2247 if (irq_src
& USB_E_RXB_MASK
) {
2249 irq_src
&= ~USB_E_RXB_MASK
;
2250 status
= IRQ_HANDLED
;
2253 if (irq_src
& USB_E_RESET_MASK
) {
2255 irq_src
&= ~USB_E_RESET_MASK
;
2256 status
= IRQ_HANDLED
;
2259 if (irq_src
& USB_E_BSY_MASK
) {
2261 irq_src
&= ~USB_E_BSY_MASK
;
2262 status
= IRQ_HANDLED
;
2265 if (irq_src
& USB_E_TXE_MASK
) {
2267 irq_src
&= ~USB_E_TXE_MASK
;
2268 status
= IRQ_HANDLED
;
2271 spin_unlock_irqrestore(&udc
->lock
, flags
);
2276 /*-------------------------------------------------------------------------
2277 Gadget driver probe and unregister.
2278 --------------------------------------------------------------------------*/
2279 static int fsl_qe_start(struct usb_gadget
*gadget
,
2280 struct usb_gadget_driver
*driver
)
2283 unsigned long flags
;
2285 udc
= container_of(gadget
, struct qe_udc
, gadget
);
2286 /* lock is needed but whether should use this lock or another */
2287 spin_lock_irqsave(&udc
->lock
, flags
);
2289 driver
->driver
.bus
= NULL
;
2290 /* hook up the driver */
2291 udc
->driver
= driver
;
2292 udc
->gadget
.speed
= driver
->max_speed
;
2294 /* Enable IRQ reg and Set usbcmd reg EN bit */
2297 out_be16(&udc
->usb_regs
->usb_usber
, 0xffff);
2298 out_be16(&udc
->usb_regs
->usb_usbmr
, USB_E_DEFAULT_DEVICE
);
2299 udc
->usb_state
= USB_STATE_ATTACHED
;
2300 udc
->ep0_state
= WAIT_FOR_SETUP
;
2301 udc
->ep0_dir
= USB_DIR_OUT
;
2302 spin_unlock_irqrestore(&udc
->lock
, flags
);
2307 static int fsl_qe_stop(struct usb_gadget
*gadget
)
2310 struct qe_ep
*loop_ep
;
2311 unsigned long flags
;
2313 udc
= container_of(gadget
, struct qe_udc
, gadget
);
2314 /* stop usb controller, disable intr */
2315 qe_usb_disable(udc
);
2317 /* in fact, no needed */
2318 udc
->usb_state
= USB_STATE_ATTACHED
;
2319 udc
->ep0_state
= WAIT_FOR_SETUP
;
2322 /* stand operation */
2323 spin_lock_irqsave(&udc
->lock
, flags
);
2324 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2325 nuke(&udc
->eps
[0], -ESHUTDOWN
);
2326 list_for_each_entry(loop_ep
, &udc
->gadget
.ep_list
, ep
.ep_list
)
2327 nuke(loop_ep
, -ESHUTDOWN
);
2328 spin_unlock_irqrestore(&udc
->lock
, flags
);
2335 /* udc structure's alloc and setup, include ep-param alloc */
2336 static struct qe_udc
*qe_udc_config(struct platform_device
*ofdev
)
2339 struct device_node
*np
= ofdev
->dev
.of_node
;
2340 unsigned int tmp_addr
= 0;
2341 struct usb_device_para __iomem
*usbpram
;
2346 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
2348 dev_err(&ofdev
->dev
, "malloc udc failed\n");
2352 udc
->dev
= &ofdev
->dev
;
2354 /* get default address of usb parameter in MURAM from device tree */
2355 offset
= *of_get_address(np
, 1, &size
, NULL
);
2356 udc
->usb_param
= cpm_muram_addr(offset
);
2357 memset_io(udc
->usb_param
, 0, size
);
2359 usbpram
= udc
->usb_param
;
2360 out_be16(&usbpram
->frame_n
, 0);
2361 out_be32(&usbpram
->rstate
, 0);
2363 tmp_addr
= cpm_muram_alloc((USB_MAX_ENDPOINTS
*
2364 sizeof(struct usb_ep_para
)),
2365 USB_EP_PARA_ALIGNMENT
);
2366 if (IS_ERR_VALUE(tmp_addr
))
2369 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2370 out_be16(&usbpram
->epptr
[i
], (u16
)tmp_addr
);
2371 udc
->ep_param
[i
] = cpm_muram_addr(tmp_addr
);
2375 memset_io(udc
->ep_param
[0], 0,
2376 USB_MAX_ENDPOINTS
* sizeof(struct usb_ep_para
));
2378 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2379 udc
->usb_state
= USB_STATE_POWERED
;
2382 spin_lock_init(&udc
->lock
);
2390 /* USB Controller register init */
2391 static int qe_udc_reg_init(struct qe_udc
*udc
)
2393 struct usb_ctlr __iomem
*qe_usbregs
;
2394 qe_usbregs
= udc
->usb_regs
;
2396 /* Spec says that we must enable the USB controller to change mode. */
2397 out_8(&qe_usbregs
->usb_usmod
, 0x01);
2398 /* Mode changed, now disable it, since muram isn't initialized yet. */
2399 out_8(&qe_usbregs
->usb_usmod
, 0x00);
2401 /* Initialize the rest. */
2402 out_be16(&qe_usbregs
->usb_usbmr
, 0);
2403 out_8(&qe_usbregs
->usb_uscom
, 0);
2404 out_be16(&qe_usbregs
->usb_usber
, USBER_ALL_CLEAR
);
2409 static int qe_ep_config(struct qe_udc
*udc
, unsigned char pipe_num
)
2411 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
2414 strcpy(ep
->name
, ep_name
[pipe_num
]);
2415 ep
->ep
.name
= ep_name
[pipe_num
];
2417 if (pipe_num
== 0) {
2418 ep
->ep
.caps
.type_control
= true;
2420 ep
->ep
.caps
.type_iso
= true;
2421 ep
->ep
.caps
.type_bulk
= true;
2422 ep
->ep
.caps
.type_int
= true;
2425 ep
->ep
.caps
.dir_in
= true;
2426 ep
->ep
.caps
.dir_out
= true;
2428 ep
->ep
.ops
= &qe_ep_ops
;
2430 usb_ep_set_maxpacket_limit(&ep
->ep
, (unsigned short) ~0);
2433 ep
->epnum
= (u8
)pipe_num
;
2440 ep
->state
= EP_STATE_IDLE
;
2443 /* the queue lists any req for this ep */
2444 INIT_LIST_HEAD(&ep
->queue
);
2446 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2448 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2450 ep
->gadget
= &udc
->gadget
;
2455 /*-----------------------------------------------------------------------
2456 * UDC device Driver operation functions *
2457 *----------------------------------------------------------------------*/
2458 static void qe_udc_release(struct device
*dev
)
2460 struct qe_udc
*udc
= container_of(dev
, struct qe_udc
, gadget
.dev
);
2463 complete(udc
->done
);
2464 cpm_muram_free(cpm_muram_offset(udc
->ep_param
[0]));
2465 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
2466 udc
->ep_param
[i
] = NULL
;
2471 /* Driver probe functions */
2472 static const struct of_device_id qe_udc_match
[];
2473 static int qe_udc_probe(struct platform_device
*ofdev
)
2476 const struct of_device_id
*match
;
2477 struct device_node
*np
= ofdev
->dev
.of_node
;
2479 unsigned int ret
= 0;
2483 match
= of_match_device(qe_udc_match
, &ofdev
->dev
);
2487 prop
= of_get_property(np
, "mode", NULL
);
2488 if (!prop
|| strcmp(prop
, "peripheral"))
2491 /* Initialize the udc structure including QH member and other member */
2492 udc
= qe_udc_config(ofdev
);
2494 dev_err(&ofdev
->dev
, "failed to initialize\n");
2498 udc
->soc_type
= (unsigned long)match
->data
;
2499 udc
->usb_regs
= of_iomap(np
, 0);
2500 if (!udc
->usb_regs
) {
2505 /* initialize usb hw reg except for regs for EP,
2506 * leave usbintr reg untouched*/
2507 qe_udc_reg_init(udc
);
2509 /* here comes the stand operations for probe
2510 * set the qe_udc->gadget.xxx */
2511 udc
->gadget
.ops
= &qe_gadget_ops
;
2513 /* gadget.ep0 is a pointer */
2514 udc
->gadget
.ep0
= &udc
->eps
[0].ep
;
2516 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
2518 /* modify in register gadget process */
2519 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2521 /* name: Identifies the controller hardware type. */
2522 udc
->gadget
.name
= driver_name
;
2523 udc
->gadget
.dev
.parent
= &ofdev
->dev
;
2525 /* initialize qe_ep struct */
2526 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2527 /* because the ep type isn't decide here so
2528 * qe_ep_init() should be called in ep_enable() */
2530 /* setup the qe_ep struct and link ep.ep.list
2531 * into gadget.ep_list */
2532 qe_ep_config(udc
, (unsigned char)i
);
2535 /* ep0 initialization in here */
2536 ret
= qe_ep_init(udc
, 0, &qe_ep0_desc
);
2540 /* create a buf for ZLP send, need to remain zeroed */
2541 udc
->nullbuf
= devm_kzalloc(&ofdev
->dev
, 256, GFP_KERNEL
);
2542 if (udc
->nullbuf
== NULL
) {
2547 /* buffer for data of get_status request */
2548 udc
->statusbuf
= devm_kzalloc(&ofdev
->dev
, 2, GFP_KERNEL
);
2549 if (udc
->statusbuf
== NULL
) {
2554 udc
->nullp
= virt_to_phys((void *)udc
->nullbuf
);
2555 if (udc
->nullp
== DMA_ADDR_INVALID
) {
2556 udc
->nullp
= dma_map_single(
2557 udc
->gadget
.dev
.parent
,
2563 dma_sync_single_for_device(udc
->gadget
.dev
.parent
,
2568 tasklet_init(&udc
->rx_tasklet
, ep_rx_tasklet
,
2569 (unsigned long)udc
);
2570 /* request irq and disable DR */
2571 udc
->usb_irq
= irq_of_parse_and_map(np
, 0);
2572 if (!udc
->usb_irq
) {
2577 ret
= request_irq(udc
->usb_irq
, qe_udc_irq
, 0,
2580 dev_err(udc
->dev
, "cannot request irq %d err %d\n",
2585 ret
= usb_add_gadget_udc_release(&ofdev
->dev
, &udc
->gadget
,
2590 platform_set_drvdata(ofdev
, udc
);
2592 "%s USB controller initialized as device\n",
2593 (udc
->soc_type
== PORT_QE
) ? "QE" : "CPM");
2597 free_irq(udc
->usb_irq
, udc
);
2599 irq_dispose_mapping(udc
->usb_irq
);
2602 dma_unmap_single(udc
->gadget
.dev
.parent
,
2605 udc
->nullp
= DMA_ADDR_INVALID
;
2607 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
2613 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2615 kfree(ep
->rxbuffer
);
2618 iounmap(udc
->usb_regs
);
2625 static int qe_udc_suspend(struct platform_device
*dev
, pm_message_t state
)
2630 static int qe_udc_resume(struct platform_device
*dev
)
2636 static int qe_udc_remove(struct platform_device
*ofdev
)
2638 struct qe_udc
*udc
= platform_get_drvdata(ofdev
);
2641 DECLARE_COMPLETION_ONSTACK(done
);
2643 usb_del_gadget_udc(&udc
->gadget
);
2646 tasklet_disable(&udc
->rx_tasklet
);
2649 dma_unmap_single(udc
->gadget
.dev
.parent
,
2652 udc
->nullp
= DMA_ADDR_INVALID
;
2654 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
2660 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2661 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (USB_BDRING_LEN
+ 1);
2665 dma_unmap_single(udc
->gadget
.dev
.parent
,
2668 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
2670 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
2675 kfree(ep
->rxbuffer
);
2678 free_irq(udc
->usb_irq
, udc
);
2679 irq_dispose_mapping(udc
->usb_irq
);
2681 tasklet_kill(&udc
->rx_tasklet
);
2683 iounmap(udc
->usb_regs
);
2685 /* wait for release() of gadget.dev to free udc */
2686 wait_for_completion(&done
);
2691 /*-------------------------------------------------------------------------*/
2692 static const struct of_device_id qe_udc_match
[] = {
2694 .compatible
= "fsl,mpc8323-qe-usb",
2695 .data
= (void *)PORT_QE
,
2698 .compatible
= "fsl,mpc8360-qe-usb",
2699 .data
= (void *)PORT_QE
,
2702 .compatible
= "fsl,mpc8272-cpm-usb",
2703 .data
= (void *)PORT_CPM
,
2708 MODULE_DEVICE_TABLE(of
, qe_udc_match
);
2710 static struct platform_driver udc_driver
= {
2712 .name
= driver_name
,
2713 .of_match_table
= qe_udc_match
,
2715 .probe
= qe_udc_probe
,
2716 .remove
= qe_udc_remove
,
2718 .suspend
= qe_udc_suspend
,
2719 .resume
= qe_udc_resume
,
2723 module_platform_driver(udc_driver
);
2725 MODULE_DESCRIPTION(DRIVER_DESC
);
2726 MODULE_AUTHOR(DRIVER_AUTHOR
);
2727 MODULE_LICENSE("GPL");