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/init.h>
26 #include <linux/ioport.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/err.h>
30 #include <linux/slab.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
34 #include <linux/moduleparam.h>
35 #include <linux/of_address.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 /* this complete() should a func implemented by gadget layer,
122 * eg fsg->bulk_in_complete() */
123 if (req
->req
.complete
)
124 req
->req
.complete(&ep
->ep
, &req
->req
);
126 spin_lock(&udc
->lock
);
128 ep
->stopped
= stopped
;
131 /*-----------------------------------------------------------------
132 * nuke(): delete all requests related to this ep
133 *--------------------------------------------------------------*/
134 static void nuke(struct qe_ep
*ep
, int status
)
136 /* Whether this eq has request linked */
137 while (!list_empty(&ep
->queue
)) {
138 struct qe_req
*req
= NULL
;
139 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
141 done(ep
, req
, status
);
145 /*---------------------------------------------------------------------------*
146 * USB and Endpoint manipulate process, include parameter and register *
147 *---------------------------------------------------------------------------*/
148 /* @value: 1--set stall 0--clean stall */
149 static int qe_eprx_stall_change(struct qe_ep
*ep
, int value
)
152 u8 epnum
= ep
->epnum
;
153 struct qe_udc
*udc
= ep
->udc
;
155 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
156 tem_usep
= tem_usep
& ~USB_RHS_MASK
;
158 tem_usep
|= USB_RHS_STALL
;
159 else if (ep
->dir
== USB_DIR_IN
)
160 tem_usep
|= USB_RHS_IGNORE_OUT
;
162 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
166 static int qe_eptx_stall_change(struct qe_ep
*ep
, int value
)
169 u8 epnum
= ep
->epnum
;
170 struct qe_udc
*udc
= ep
->udc
;
172 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
173 tem_usep
= tem_usep
& ~USB_THS_MASK
;
175 tem_usep
|= USB_THS_STALL
;
176 else if (ep
->dir
== USB_DIR_OUT
)
177 tem_usep
|= USB_THS_IGNORE_IN
;
179 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
184 static int qe_ep0_stall(struct qe_udc
*udc
)
186 qe_eptx_stall_change(&udc
->eps
[0], 1);
187 qe_eprx_stall_change(&udc
->eps
[0], 1);
188 udc
->ep0_state
= WAIT_FOR_SETUP
;
193 static int qe_eprx_nack(struct qe_ep
*ep
)
195 u8 epnum
= ep
->epnum
;
196 struct qe_udc
*udc
= ep
->udc
;
198 if (ep
->state
== EP_STATE_IDLE
) {
199 /* Set the ep's nack */
200 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[epnum
],
201 USB_RHS_MASK
, USB_RHS_NACK
);
203 /* Mask Rx and Busy interrupts */
204 clrbits16(&udc
->usb_regs
->usb_usbmr
,
205 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
207 ep
->state
= EP_STATE_NACK
;
212 static int qe_eprx_normal(struct qe_ep
*ep
)
214 struct qe_udc
*udc
= ep
->udc
;
216 if (ep
->state
== EP_STATE_NACK
) {
217 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[ep
->epnum
],
218 USB_RTHS_MASK
, USB_THS_IGNORE_IN
);
220 /* Unmask RX interrupts */
221 out_be16(&udc
->usb_regs
->usb_usber
,
222 USB_E_BSY_MASK
| USB_E_RXB_MASK
);
223 setbits16(&udc
->usb_regs
->usb_usbmr
,
224 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
226 ep
->state
= EP_STATE_IDLE
;
233 static int qe_ep_cmd_stoptx(struct qe_ep
*ep
)
235 if (ep
->udc
->soc_type
== PORT_CPM
)
236 cpm_command(CPM_USB_STOP_TX
| (ep
->epnum
<< CPM_USB_EP_SHIFT
),
237 CPM_USB_STOP_TX_OPCODE
);
239 qe_issue_cmd(QE_USB_STOP_TX
, QE_CR_SUBBLOCK_USB
,
245 static int qe_ep_cmd_restarttx(struct qe_ep
*ep
)
247 if (ep
->udc
->soc_type
== PORT_CPM
)
248 cpm_command(CPM_USB_RESTART_TX
| (ep
->epnum
<<
249 CPM_USB_EP_SHIFT
), CPM_USB_RESTART_TX_OPCODE
);
251 qe_issue_cmd(QE_USB_RESTART_TX
, QE_CR_SUBBLOCK_USB
,
257 static int qe_ep_flushtxfifo(struct qe_ep
*ep
)
259 struct qe_udc
*udc
= ep
->udc
;
264 qe_ep_cmd_stoptx(ep
);
265 out_8(&udc
->usb_regs
->usb_uscom
,
266 USB_CMD_FLUSH_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
267 out_be16(&udc
->ep_param
[i
]->tbptr
, in_be16(&udc
->ep_param
[i
]->tbase
));
268 out_be32(&udc
->ep_param
[i
]->tstate
, 0);
269 out_be16(&udc
->ep_param
[i
]->tbcnt
, 0);
271 ep
->c_txbd
= ep
->txbase
;
272 ep
->n_txbd
= ep
->txbase
;
273 qe_ep_cmd_restarttx(ep
);
277 static int qe_ep_filltxfifo(struct qe_ep
*ep
)
279 struct qe_udc
*udc
= ep
->udc
;
281 out_8(&udc
->usb_regs
->usb_uscom
,
282 USB_CMD_STR_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
286 static int qe_epbds_reset(struct qe_udc
*udc
, int pipe_num
)
290 struct qe_bd __iomem
*bd
;
293 ep
= &udc
->eps
[pipe_num
];
295 if (ep
->dir
== USB_DIR_OUT
)
296 bdring_len
= USB_BDRING_LEN_RX
;
298 bdring_len
= USB_BDRING_LEN
;
301 for (i
= 0; i
< (bdring_len
- 1); i
++) {
302 out_be32((u32 __iomem
*)bd
, R_E
| R_I
);
305 out_be32((u32 __iomem
*)bd
, R_E
| R_I
| R_W
);
308 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
309 out_be32(&bd
->buf
, 0);
310 out_be32((u32 __iomem
*)bd
, 0);
313 out_be32((u32 __iomem
*)bd
, T_W
);
318 static int qe_ep_reset(struct qe_udc
*udc
, int pipe_num
)
323 ep
= &udc
->eps
[pipe_num
];
324 tmpusep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe_num
]);
325 tmpusep
&= ~USB_RTHS_MASK
;
329 qe_ep_flushtxfifo(ep
);
332 tmpusep
|= USB_THS_IGNORE_IN
;
335 qe_ep_flushtxfifo(ep
);
336 tmpusep
|= USB_RHS_IGNORE_OUT
;
341 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], tmpusep
);
343 qe_epbds_reset(udc
, pipe_num
);
348 static int qe_ep_toggledata01(struct qe_ep
*ep
)
354 static int qe_ep_bd_init(struct qe_udc
*udc
, unsigned char pipe_num
)
356 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
357 unsigned long tmp_addr
= 0;
358 struct usb_ep_para __iomem
*epparam
;
360 struct qe_bd __iomem
*bd
;
363 if (ep
->dir
== USB_DIR_OUT
)
364 bdring_len
= USB_BDRING_LEN_RX
;
366 bdring_len
= USB_BDRING_LEN
;
368 epparam
= udc
->ep_param
[pipe_num
];
369 /* alloc multi-ram for BD rings and set the ep parameters */
370 tmp_addr
= cpm_muram_alloc(sizeof(struct qe_bd
) * (bdring_len
+
371 USB_BDRING_LEN_TX
), QE_ALIGNMENT_OF_BD
);
372 if (IS_ERR_VALUE(tmp_addr
))
375 out_be16(&epparam
->rbase
, (u16
)tmp_addr
);
376 out_be16(&epparam
->tbase
, (u16
)(tmp_addr
+
377 (sizeof(struct qe_bd
) * bdring_len
)));
379 out_be16(&epparam
->rbptr
, in_be16(&epparam
->rbase
));
380 out_be16(&epparam
->tbptr
, in_be16(&epparam
->tbase
));
382 ep
->rxbase
= cpm_muram_addr(tmp_addr
);
383 ep
->txbase
= cpm_muram_addr(tmp_addr
+ (sizeof(struct qe_bd
)
385 ep
->n_rxbd
= ep
->rxbase
;
386 ep
->e_rxbd
= ep
->rxbase
;
387 ep
->n_txbd
= ep
->txbase
;
388 ep
->c_txbd
= ep
->txbase
;
389 ep
->data01
= 0; /* data0 */
391 /* Init TX and RX bds */
393 for (i
= 0; i
< bdring_len
- 1; i
++) {
394 out_be32(&bd
->buf
, 0);
395 out_be32((u32 __iomem
*)bd
, 0);
398 out_be32(&bd
->buf
, 0);
399 out_be32((u32 __iomem
*)bd
, R_W
);
402 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
403 out_be32(&bd
->buf
, 0);
404 out_be32((u32 __iomem
*)bd
, 0);
407 out_be32(&bd
->buf
, 0);
408 out_be32((u32 __iomem
*)bd
, T_W
);
413 static int qe_ep_rxbd_update(struct qe_ep
*ep
)
418 struct qe_bd __iomem
*bd
;
419 unsigned int bdring_len
;
421 if (ep
->rxbase
== NULL
)
426 ep
->rxframe
= kmalloc(sizeof(*ep
->rxframe
), GFP_ATOMIC
);
427 if (ep
->rxframe
== NULL
) {
428 dev_err(ep
->udc
->dev
, "malloc rxframe failed\n");
432 qe_frame_init(ep
->rxframe
);
434 if (ep
->dir
== USB_DIR_OUT
)
435 bdring_len
= USB_BDRING_LEN_RX
;
437 bdring_len
= USB_BDRING_LEN
;
439 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (bdring_len
+ 1);
440 ep
->rxbuffer
= kzalloc(size
, GFP_ATOMIC
);
441 if (ep
->rxbuffer
== NULL
) {
442 dev_err(ep
->udc
->dev
, "malloc rxbuffer failed,size=%d\n",
448 ep
->rxbuf_d
= virt_to_phys((void *)ep
->rxbuffer
);
449 if (ep
->rxbuf_d
== DMA_ADDR_INVALID
) {
450 ep
->rxbuf_d
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
456 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
462 size
= ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2;
464 tmp
= (u32
)(((tmp
>> 2) << 2) + 4);
466 for (i
= 0; i
< bdring_len
- 1; i
++) {
467 out_be32(&bd
->buf
, tmp
);
468 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
));
472 out_be32(&bd
->buf
, tmp
);
473 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
| R_W
));
478 static int qe_ep_register_init(struct qe_udc
*udc
, unsigned char pipe_num
)
480 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
481 struct usb_ep_para __iomem
*epparam
;
486 epparam
= udc
->ep_param
[pipe_num
];
489 logepnum
= (ep
->ep
.desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
490 usep
|= (logepnum
<< USB_EPNUM_SHIFT
);
492 switch (ep
->ep
.desc
->bmAttributes
& 0x03) {
493 case USB_ENDPOINT_XFER_BULK
:
494 usep
|= USB_TRANS_BULK
;
496 case USB_ENDPOINT_XFER_ISOC
:
497 usep
|= USB_TRANS_ISO
;
499 case USB_ENDPOINT_XFER_INT
:
500 usep
|= USB_TRANS_INT
;
503 usep
|= USB_TRANS_CTR
;
509 usep
|= USB_THS_IGNORE_IN
;
512 usep
|= USB_RHS_IGNORE_OUT
;
517 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], usep
);
520 out_8(&epparam
->rbmr
, rtfcr
);
521 out_8(&epparam
->tbmr
, rtfcr
);
523 tmp
= (u16
)(ep
->ep
.maxpacket
+ USB_CRC_SIZE
);
524 /* MRBLR must be divisble by 4 */
525 tmp
= (u16
)(((tmp
>> 2) << 2) + 4);
526 out_be16(&epparam
->mrblr
, tmp
);
531 static int qe_ep_init(struct qe_udc
*udc
,
532 unsigned char pipe_num
,
533 const struct usb_endpoint_descriptor
*desc
)
535 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
540 max
= usb_endpoint_maxp(desc
);
542 /* check the max package size validate for this endpoint */
543 /* Refer to USB2.0 spec table 9-13,
546 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
547 case USB_ENDPOINT_XFER_BULK
:
548 if (strstr(ep
->ep
.name
, "-iso")
549 || strstr(ep
->ep
.name
, "-int"))
551 switch (udc
->gadget
.speed
) {
553 if ((max
== 128) || (max
== 256) || (max
== 512))
569 case USB_ENDPOINT_XFER_INT
:
570 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
572 switch (udc
->gadget
.speed
) {
585 case USB_ENDPOINT_XFER_ISOC
:
586 if (strstr(ep
->ep
.name
, "-bulk")
587 || strstr(ep
->ep
.name
, "-int"))
589 switch (udc
->gadget
.speed
) {
600 case USB_ENDPOINT_XFER_CONTROL
:
601 if (strstr(ep
->ep
.name
, "-iso")
602 || strstr(ep
->ep
.name
, "-int"))
604 switch (udc
->gadget
.speed
) {
639 spin_lock_irqsave(&udc
->lock
, flags
);
641 /* initialize ep structure */
642 ep
->ep
.maxpacket
= max
;
643 ep
->tm
= (u8
)(desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
649 ep
->dir
= USB_DIR_BOTH
;
650 udc
->ep0_dir
= USB_DIR_OUT
;
651 udc
->ep0_state
= WAIT_FOR_SETUP
;
653 switch (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) {
655 ep
->dir
= USB_DIR_OUT
;
658 ep
->dir
= USB_DIR_IN
;
664 /* hardware special operation */
665 qe_ep_bd_init(udc
, pipe_num
);
666 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_OUT
)) {
667 reval
= qe_ep_rxbd_update(ep
);
672 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_IN
)) {
673 ep
->txframe
= kmalloc(sizeof(*ep
->txframe
), GFP_ATOMIC
);
674 if (ep
->txframe
== NULL
) {
675 dev_err(udc
->dev
, "malloc txframe failed\n");
678 qe_frame_init(ep
->txframe
);
681 qe_ep_register_init(udc
, pipe_num
);
683 /* Now HW will be NAKing transfers to that EP,
684 * until a buffer is queued to it. */
685 spin_unlock_irqrestore(&udc
->lock
, flags
);
692 spin_unlock_irqrestore(&udc
->lock
, flags
);
694 dev_err(udc
->dev
, "failed to initialize %s\n", ep
->ep
.name
);
698 static inline void qe_usb_enable(struct qe_udc
*udc
)
700 setbits8(&udc
->usb_regs
->usb_usmod
, USB_MODE_EN
);
703 static inline void qe_usb_disable(struct qe_udc
*udc
)
705 clrbits8(&udc
->usb_regs
->usb_usmod
, USB_MODE_EN
);
708 /*----------------------------------------------------------------------------*
709 * USB and EP basic manipulate function end *
710 *----------------------------------------------------------------------------*/
713 /******************************************************************************
714 UDC transmit and receive process
715 ******************************************************************************/
716 static void recycle_one_rxbd(struct qe_ep
*ep
)
720 bdstatus
= in_be32((u32 __iomem
*)ep
->e_rxbd
);
721 bdstatus
= R_I
| R_E
| (bdstatus
& R_W
);
722 out_be32((u32 __iomem
*)ep
->e_rxbd
, bdstatus
);
725 ep
->e_rxbd
= ep
->rxbase
;
730 static void recycle_rxbds(struct qe_ep
*ep
, unsigned char stopatnext
)
733 struct qe_bd __iomem
*bd
, *nextbd
;
734 unsigned char stop
= 0;
738 bdstatus
= in_be32((u32 __iomem
*)bd
);
740 while (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
) && !stop
) {
741 bdstatus
= R_E
| R_I
| (bdstatus
& R_W
);
742 out_be32((u32 __iomem
*)bd
, bdstatus
);
749 bdstatus
= in_be32((u32 __iomem
*)bd
);
750 if (stopatnext
&& (bd
== nextbd
))
757 static void ep_recycle_rxbds(struct qe_ep
*ep
)
759 struct qe_bd __iomem
*bd
= ep
->n_rxbd
;
761 u8 epnum
= ep
->epnum
;
762 struct qe_udc
*udc
= ep
->udc
;
764 bdstatus
= in_be32((u32 __iomem
*)bd
);
765 if (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
)) {
767 ((in_be16(&udc
->ep_param
[epnum
]->rbptr
) -
768 in_be16(&udc
->ep_param
[epnum
]->rbase
))
770 bdstatus
= in_be32((u32 __iomem
*)bd
);
778 recycle_rxbds(ep
, 0);
779 ep
->e_rxbd
= ep
->n_rxbd
;
781 recycle_rxbds(ep
, 1);
783 if (in_be16(&udc
->usb_regs
->usb_usber
) & USB_E_BSY_MASK
)
784 out_be16(&udc
->usb_regs
->usb_usber
, USB_E_BSY_MASK
);
786 if (ep
->has_data
<= 0 && (!list_empty(&ep
->queue
)))
792 static void setup_received_handle(struct qe_udc
*udc
,
793 struct usb_ctrlrequest
*setup
);
794 static int qe_ep_rxframe_handle(struct qe_ep
*ep
);
795 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
);
796 /* when BD PID is setup, handle the packet */
797 static int ep0_setup_handle(struct qe_udc
*udc
)
799 struct qe_ep
*ep
= &udc
->eps
[0];
800 struct qe_frame
*pframe
;
804 pframe
= ep
->rxframe
;
805 if ((frame_get_info(pframe
) & PID_SETUP
)
806 && (udc
->ep0_state
== WAIT_FOR_SETUP
)) {
807 fsize
= frame_get_length(pframe
);
808 if (unlikely(fsize
!= 8))
810 cp
= (u8
*)&udc
->local_setup_buff
;
811 memcpy(cp
, pframe
->data
, fsize
);
814 /* handle the usb command base on the usb_ctrlrequest */
815 setup_received_handle(udc
, &udc
->local_setup_buff
);
821 static int qe_ep0_rx(struct qe_udc
*udc
)
823 struct qe_ep
*ep
= &udc
->eps
[0];
824 struct qe_frame
*pframe
;
825 struct qe_bd __iomem
*bd
;
826 u32 bdstatus
, length
;
829 pframe
= ep
->rxframe
;
831 if (ep
->dir
== USB_DIR_IN
) {
832 dev_err(udc
->dev
, "ep0 not a control endpoint\n");
837 bdstatus
= in_be32((u32 __iomem
*)bd
);
838 length
= bdstatus
& BD_LENGTH_MASK
;
840 while (!(bdstatus
& R_E
) && length
) {
841 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
842 && !(bdstatus
& R_ERROR
)) {
843 if (length
== USB_CRC_SIZE
) {
844 udc
->ep0_state
= WAIT_FOR_SETUP
;
846 "receive a ZLP in status phase\n");
848 qe_frame_clean(pframe
);
849 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
850 frame_set_data(pframe
, (u8
*)vaddr
);
851 frame_set_length(pframe
,
852 (length
- USB_CRC_SIZE
));
853 frame_set_status(pframe
, FRAME_OK
);
854 switch (bdstatus
& R_PID
) {
856 frame_set_info(pframe
, PID_SETUP
);
859 frame_set_info(pframe
, PID_DATA1
);
862 frame_set_info(pframe
, PID_DATA0
);
866 if ((bdstatus
& R_PID
) == R_PID_SETUP
)
867 ep0_setup_handle(udc
);
869 qe_ep_rxframe_handle(ep
);
872 dev_err(udc
->dev
, "The receive frame with error!\n");
875 /* note: don't clear the rxbd's buffer address */
876 recycle_one_rxbd(ep
);
884 bdstatus
= in_be32((u32 __iomem
*)bd
);
885 length
= bdstatus
& BD_LENGTH_MASK
;
894 static int qe_ep_rxframe_handle(struct qe_ep
*ep
)
896 struct qe_frame
*pframe
;
902 pframe
= ep
->rxframe
;
904 if (frame_get_info(pframe
) & PID_DATA1
)
907 if (framepid
!= ep
->data01
) {
908 dev_err(ep
->udc
->dev
, "the data01 error!\n");
912 fsize
= frame_get_length(pframe
);
913 if (list_empty(&ep
->queue
)) {
914 dev_err(ep
->udc
->dev
, "the %s have no requeue!\n", ep
->name
);
916 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
918 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
920 memcpy(cp
, pframe
->data
, fsize
);
921 req
->req
.actual
+= fsize
;
922 if ((fsize
< ep
->ep
.maxpacket
) ||
923 (req
->req
.actual
>= req
->req
.length
)) {
925 ep0_req_complete(ep
->udc
, req
);
928 if (list_empty(&ep
->queue
) && ep
->epnum
!= 0)
934 qe_ep_toggledata01(ep
);
939 static void ep_rx_tasklet(unsigned long data
)
941 struct qe_udc
*udc
= (struct qe_udc
*)data
;
943 struct qe_frame
*pframe
;
944 struct qe_bd __iomem
*bd
;
946 u32 bdstatus
, length
;
949 spin_lock_irqsave(&udc
->lock
, flags
);
951 for (i
= 1; i
< USB_MAX_ENDPOINTS
; i
++) {
954 if (ep
->dir
== USB_DIR_IN
|| ep
->enable_tasklet
== 0) {
956 "This is a transmit ep or disable tasklet!\n");
960 pframe
= ep
->rxframe
;
962 bdstatus
= in_be32((u32 __iomem
*)bd
);
963 length
= bdstatus
& BD_LENGTH_MASK
;
965 while (!(bdstatus
& R_E
) && length
) {
966 if (list_empty(&ep
->queue
)) {
969 "The rxep have noreq %d\n",
974 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
975 && !(bdstatus
& R_ERROR
)) {
976 qe_frame_clean(pframe
);
977 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
978 frame_set_data(pframe
, (u8
*)vaddr
);
979 frame_set_length(pframe
,
980 (length
- USB_CRC_SIZE
));
981 frame_set_status(pframe
, FRAME_OK
);
982 switch (bdstatus
& R_PID
) {
984 frame_set_info(pframe
, PID_DATA1
);
987 frame_set_info(pframe
, PID_SETUP
);
990 frame_set_info(pframe
, PID_DATA0
);
993 /* handle the rx frame */
994 qe_ep_rxframe_handle(ep
);
997 "error in received frame\n");
999 /* note: don't clear the rxbd's buffer address */
1000 /*clear the length */
1001 out_be32((u32 __iomem
*)bd
, bdstatus
& BD_STATUS_MASK
);
1003 if (!(ep
->localnack
))
1004 recycle_one_rxbd(ep
);
1012 bdstatus
= in_be32((u32 __iomem
*)bd
);
1013 length
= bdstatus
& BD_LENGTH_MASK
;
1019 ep_recycle_rxbds(ep
);
1021 ep
->enable_tasklet
= 0;
1024 spin_unlock_irqrestore(&udc
->lock
, flags
);
1027 static int qe_ep_rx(struct qe_ep
*ep
)
1030 struct qe_frame
*pframe
;
1031 struct qe_bd __iomem
*bd
;
1032 u16 swoffs
, ucoffs
, emptybds
;
1035 pframe
= ep
->rxframe
;
1037 if (ep
->dir
== USB_DIR_IN
) {
1038 dev_err(udc
->dev
, "transmit ep in rx function\n");
1044 swoffs
= (u16
)(bd
- ep
->rxbase
);
1045 ucoffs
= (u16
)((in_be16(&udc
->ep_param
[ep
->epnum
]->rbptr
) -
1046 in_be16(&udc
->ep_param
[ep
->epnum
]->rbase
)) >> 3);
1047 if (swoffs
< ucoffs
)
1048 emptybds
= USB_BDRING_LEN_RX
- ucoffs
+ swoffs
;
1050 emptybds
= swoffs
- ucoffs
;
1052 if (emptybds
< MIN_EMPTY_BDS
) {
1055 dev_vdbg(udc
->dev
, "%d empty bds, send NACK\n", emptybds
);
1057 ep
->has_data
= USB_BDRING_LEN_RX
- emptybds
;
1059 if (list_empty(&ep
->queue
)) {
1061 dev_vdbg(udc
->dev
, "The rxep have no req queued with %d BDs\n",
1066 tasklet_schedule(&udc
->rx_tasklet
);
1067 ep
->enable_tasklet
= 1;
1072 /* send data from a frame, no matter what tx_req */
1073 static int qe_ep_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1075 struct qe_udc
*udc
= ep
->udc
;
1076 struct qe_bd __iomem
*bd
;
1078 u32 bdstatus
, pidmask
;
1081 if (ep
->dir
== USB_DIR_OUT
) {
1082 dev_err(udc
->dev
, "receive ep passed to tx function\n");
1086 /* Disable the Tx interrupt */
1087 saveusbmr
= in_be16(&udc
->usb_regs
->usb_usbmr
);
1088 out_be16(&udc
->usb_regs
->usb_usbmr
,
1089 saveusbmr
& ~(USB_E_TXB_MASK
| USB_E_TXE_MASK
));
1092 bdstatus
= in_be32((u32 __iomem
*)bd
);
1094 if (!(bdstatus
& (T_R
| BD_LENGTH_MASK
))) {
1095 if (frame_get_length(frame
) == 0) {
1096 frame_set_data(frame
, udc
->nullbuf
);
1097 frame_set_length(frame
, 2);
1098 frame
->info
|= (ZLP
| NO_CRC
);
1099 dev_vdbg(udc
->dev
, "the frame size = 0\n");
1101 paddr
= virt_to_phys((void *)frame
->data
);
1102 out_be32(&bd
->buf
, paddr
);
1103 bdstatus
= (bdstatus
&T_W
);
1104 if (!(frame_get_info(frame
) & NO_CRC
))
1105 bdstatus
|= T_R
| T_I
| T_L
| T_TC
1106 | frame_get_length(frame
);
1108 bdstatus
|= T_R
| T_I
| T_L
| frame_get_length(frame
);
1110 /* if the packet is a ZLP in status phase */
1111 if ((ep
->epnum
== 0) && (udc
->ep0_state
== DATA_STATE_NEED_ZLP
))
1115 pidmask
= T_PID_DATA1
;
1116 frame
->info
|= PID_DATA1
;
1118 pidmask
= T_PID_DATA0
;
1119 frame
->info
|= PID_DATA0
;
1122 bdstatus
|= pidmask
;
1123 out_be32((u32 __iomem
*)bd
, bdstatus
);
1124 qe_ep_filltxfifo(ep
);
1126 /* enable the TX interrupt */
1127 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1129 qe_ep_toggledata01(ep
);
1131 ep
->n_txbd
= ep
->txbase
;
1137 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1138 dev_vdbg(udc
->dev
, "The tx bd is not ready!\n");
1143 /* when a bd was transmitted, the function can
1144 * handle the tx_req, not include ep0 */
1145 static int txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1147 if (ep
->tx_req
!= NULL
) {
1148 struct qe_req
*req
= ep
->tx_req
;
1149 unsigned zlp
= 0, last_len
= 0;
1151 last_len
= min_t(unsigned, req
->req
.length
- ep
->sent
,
1155 int asent
= ep
->last
;
1162 /* zlp needed when req->re.zero is set */
1163 if (req
->req
.zero
) {
1164 if (last_len
== 0 ||
1165 (req
->req
.length
% ep
->ep
.maxpacket
) != 0)
1172 /* a request already were transmitted completely */
1173 if (((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) && !zlp
) {
1174 done(ep
, ep
->tx_req
, 0);
1181 /* we should gain a new tx_req fot this endpoint */
1182 if (ep
->tx_req
== NULL
) {
1183 if (!list_empty(&ep
->queue
)) {
1184 ep
->tx_req
= list_entry(ep
->queue
.next
, struct qe_req
,
1194 /* give a frame and a tx_req, send some data */
1195 static int qe_usb_senddata(struct qe_ep
*ep
, struct qe_frame
*frame
)
1200 qe_frame_clean(frame
);
1201 size
= min_t(u32
, (ep
->tx_req
->req
.length
- ep
->sent
),
1203 buf
= (u8
*)ep
->tx_req
->req
.buf
+ ep
->sent
;
1206 ep
->tx_req
->req
.actual
+= size
;
1207 frame_set_data(frame
, buf
);
1208 frame_set_length(frame
, size
);
1209 frame_set_status(frame
, FRAME_OK
);
1210 frame_set_info(frame
, 0);
1211 return qe_ep_tx(ep
, frame
);
1216 /* give a frame struct,send a ZLP */
1217 static int sendnulldata(struct qe_ep
*ep
, struct qe_frame
*frame
, uint infor
)
1219 struct qe_udc
*udc
= ep
->udc
;
1224 qe_frame_clean(frame
);
1225 frame_set_data(frame
, (u8
*)udc
->nullbuf
);
1226 frame_set_length(frame
, 2);
1227 frame_set_status(frame
, FRAME_OK
);
1228 frame_set_info(frame
, (ZLP
| NO_CRC
| infor
));
1230 return qe_ep_tx(ep
, frame
);
1233 static int frame_create_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1235 struct qe_req
*req
= ep
->tx_req
;
1241 if ((req
->req
.length
- ep
->sent
) > 0)
1242 reval
= qe_usb_senddata(ep
, frame
);
1244 reval
= sendnulldata(ep
, frame
, 0);
1249 /* if direction is DIR_IN, the status is Device->Host
1250 * if direction is DIR_OUT, the status transaction is Device<-Host
1251 * in status phase, udc create a request and gain status */
1252 static int ep0_prime_status(struct qe_udc
*udc
, int direction
)
1255 struct qe_ep
*ep
= &udc
->eps
[0];
1257 if (direction
== USB_DIR_IN
) {
1258 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
1259 udc
->ep0_dir
= USB_DIR_IN
;
1260 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1262 udc
->ep0_dir
= USB_DIR_OUT
;
1263 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1269 /* a request complete in ep0, whether gadget request or udc request */
1270 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
)
1272 struct qe_ep
*ep
= &udc
->eps
[0];
1273 /* because usb and ep's status already been set in ch9setaddress() */
1275 switch (udc
->ep0_state
) {
1276 case DATA_STATE_XMIT
:
1278 /* receive status phase */
1279 if (ep0_prime_status(udc
, USB_DIR_OUT
))
1283 case DATA_STATE_NEED_ZLP
:
1285 udc
->ep0_state
= WAIT_FOR_SETUP
;
1288 case DATA_STATE_RECV
:
1290 /* send status phase */
1291 if (ep0_prime_status(udc
, USB_DIR_IN
))
1295 case WAIT_FOR_OUT_STATUS
:
1297 udc
->ep0_state
= WAIT_FOR_SETUP
;
1300 case WAIT_FOR_SETUP
:
1301 dev_vdbg(udc
->dev
, "Unexpected interrupt\n");
1310 static int ep0_txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1312 struct qe_req
*tx_req
= NULL
;
1313 struct qe_frame
*frame
= ep
->txframe
;
1315 if ((frame_get_info(frame
) & (ZLP
| NO_REQ
)) == (ZLP
| NO_REQ
)) {
1317 ep
->udc
->ep0_state
= WAIT_FOR_SETUP
;
1319 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1323 tx_req
= ep
->tx_req
;
1324 if (tx_req
!= NULL
) {
1326 int asent
= ep
->last
;
1333 /* a request already were transmitted completely */
1334 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1335 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1336 ep0_req_complete(ep
->udc
, ep
->tx_req
);
1342 dev_vdbg(ep
->udc
->dev
, "the ep0_controller have no req\n");
1348 static int ep0_txframe_handle(struct qe_ep
*ep
)
1350 /* if have error, transmit again */
1351 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1352 qe_ep_flushtxfifo(ep
);
1353 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1354 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1359 ep0_txcomplete(ep
, 1);
1361 ep0_txcomplete(ep
, 0);
1363 frame_create_tx(ep
, ep
->txframe
);
1367 static int qe_ep0_txconf(struct qe_ep
*ep
)
1369 struct qe_bd __iomem
*bd
;
1370 struct qe_frame
*pframe
;
1374 bdstatus
= in_be32((u32 __iomem
*)bd
);
1375 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1376 pframe
= ep
->txframe
;
1378 /* clear and recycle the BD */
1379 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1380 out_be32(&bd
->buf
, 0);
1382 ep
->c_txbd
= ep
->txbase
;
1386 if (ep
->c_txbd
== ep
->n_txbd
) {
1387 if (bdstatus
& DEVICE_T_ERROR
) {
1388 frame_set_status(pframe
, FRAME_ERROR
);
1389 if (bdstatus
& T_TO
)
1390 pframe
->status
|= TX_ER_TIMEOUT
;
1391 if (bdstatus
& T_UN
)
1392 pframe
->status
|= TX_ER_UNDERUN
;
1394 ep0_txframe_handle(ep
);
1398 bdstatus
= in_be32((u32 __iomem
*)bd
);
1404 static int ep_txframe_handle(struct qe_ep
*ep
)
1406 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1407 qe_ep_flushtxfifo(ep
);
1408 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1409 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1418 frame_create_tx(ep
, ep
->txframe
); /* send the data */
1422 /* confirm the already trainsmited bd */
1423 static int qe_ep_txconf(struct qe_ep
*ep
)
1425 struct qe_bd __iomem
*bd
;
1426 struct qe_frame
*pframe
= NULL
;
1428 unsigned char breakonrxinterrupt
= 0;
1431 bdstatus
= in_be32((u32 __iomem
*)bd
);
1432 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1433 pframe
= ep
->txframe
;
1434 if (bdstatus
& DEVICE_T_ERROR
) {
1435 frame_set_status(pframe
, FRAME_ERROR
);
1436 if (bdstatus
& T_TO
)
1437 pframe
->status
|= TX_ER_TIMEOUT
;
1438 if (bdstatus
& T_UN
)
1439 pframe
->status
|= TX_ER_UNDERUN
;
1442 /* clear and recycle the BD */
1443 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1444 out_be32(&bd
->buf
, 0);
1446 ep
->c_txbd
= ep
->txbase
;
1450 /* handle the tx frame */
1451 ep_txframe_handle(ep
);
1453 bdstatus
= in_be32((u32 __iomem
*)bd
);
1455 if (breakonrxinterrupt
)
1461 /* Add a request in queue, and try to transmit a packet */
1462 static int ep_req_send(struct qe_ep
*ep
, struct qe_req
*req
)
1466 if (ep
->tx_req
== NULL
) {
1469 txcomplete(ep
, 0); /* can gain a new tx_req */
1470 reval
= frame_create_tx(ep
, ep
->txframe
);
1475 /* Maybe this is a good ideal */
1476 static int ep_req_rx(struct qe_ep
*ep
, struct qe_req
*req
)
1478 struct qe_udc
*udc
= ep
->udc
;
1479 struct qe_frame
*pframe
= NULL
;
1480 struct qe_bd __iomem
*bd
;
1481 u32 bdstatus
, length
;
1487 if (list_empty(&ep
->queue
)) {
1488 dev_vdbg(udc
->dev
, "the req already finish!\n");
1491 pframe
= ep
->rxframe
;
1494 bdstatus
= in_be32((u32 __iomem
*)bd
);
1495 length
= bdstatus
& BD_LENGTH_MASK
;
1497 while (!(bdstatus
& R_E
) && length
) {
1500 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
1501 && !(bdstatus
& R_ERROR
)) {
1502 qe_frame_clean(pframe
);
1503 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
1504 frame_set_data(pframe
, (u8
*)vaddr
);
1505 frame_set_length(pframe
, (length
- USB_CRC_SIZE
));
1506 frame_set_status(pframe
, FRAME_OK
);
1507 switch (bdstatus
& R_PID
) {
1509 frame_set_info(pframe
, PID_DATA1
); break;
1511 frame_set_info(pframe
, PID_DATA0
); break;
1513 /* handle the rx frame */
1515 if (frame_get_info(pframe
) & PID_DATA1
)
1520 if (framepid
!= ep
->data01
) {
1521 dev_vdbg(udc
->dev
, "the data01 error!\n");
1523 fsize
= frame_get_length(pframe
);
1525 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
1527 memcpy(cp
, pframe
->data
, fsize
);
1528 req
->req
.actual
+= fsize
;
1529 if ((fsize
< ep
->ep
.maxpacket
)
1530 || (req
->req
.actual
>=
1534 if (list_empty(&ep
->queue
))
1538 qe_ep_toggledata01(ep
);
1541 dev_err(udc
->dev
, "The receive frame with error!\n");
1544 /* note: don't clear the rxbd's buffer address *
1545 * only Clear the length */
1546 out_be32((u32 __iomem
*)bd
, (bdstatus
& BD_STATUS_MASK
));
1555 bdstatus
= in_be32((u32 __iomem
*)bd
);
1556 length
= bdstatus
& BD_LENGTH_MASK
;
1560 ep_recycle_rxbds(ep
);
1565 /* only add the request in queue */
1566 static int ep_req_receive(struct qe_ep
*ep
, struct qe_req
*req
)
1568 if (ep
->state
== EP_STATE_NACK
) {
1569 if (ep
->has_data
<= 0) {
1570 /* Enable rx and unmask rx interrupt */
1573 /* Copy the exist BD data */
1581 /********************************************************************
1582 Internal Used Function End
1583 ********************************************************************/
1585 /*-----------------------------------------------------------------------
1586 Endpoint Management Functions For Gadget
1587 -----------------------------------------------------------------------*/
1588 static int qe_ep_enable(struct usb_ep
*_ep
,
1589 const struct usb_endpoint_descriptor
*desc
)
1594 unsigned char epnum
;
1596 ep
= container_of(_ep
, struct qe_ep
, ep
);
1598 /* catch various bogus parameters */
1599 if (!_ep
|| !desc
|| _ep
->name
== ep_name
[0] ||
1600 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
))
1604 if (!udc
->driver
|| (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
))
1607 epnum
= (u8
)desc
->bEndpointAddress
& 0xF;
1609 retval
= qe_ep_init(udc
, epnum
, desc
);
1611 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1612 dev_dbg(udc
->dev
, "enable ep%d failed\n", ep
->epnum
);
1615 dev_dbg(udc
->dev
, "enable ep%d successful\n", ep
->epnum
);
1619 static int qe_ep_disable(struct usb_ep
*_ep
)
1623 unsigned long flags
;
1626 ep
= container_of(_ep
, struct qe_ep
, ep
);
1629 if (!_ep
|| !ep
->ep
.desc
) {
1630 dev_dbg(udc
->dev
, "%s not enabled\n", _ep
? ep
->ep
.name
: NULL
);
1634 spin_lock_irqsave(&udc
->lock
, flags
);
1635 /* Nuke all pending requests (does flush) */
1636 nuke(ep
, -ESHUTDOWN
);
1640 qe_ep_reset(udc
, ep
->epnum
);
1641 spin_unlock_irqrestore(&udc
->lock
, flags
);
1643 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1645 if (ep
->dir
== USB_DIR_OUT
)
1646 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1647 (USB_BDRING_LEN_RX
+ 1);
1649 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1650 (USB_BDRING_LEN
+ 1);
1652 if (ep
->dir
!= USB_DIR_IN
) {
1655 dma_unmap_single(udc
->gadget
.dev
.parent
,
1658 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
1660 dma_sync_single_for_cpu(
1661 udc
->gadget
.dev
.parent
,
1665 kfree(ep
->rxbuffer
);
1668 if (ep
->dir
!= USB_DIR_OUT
)
1671 dev_dbg(udc
->dev
, "disabled %s OK\n", _ep
->name
);
1675 static struct usb_request
*qe_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
1679 req
= kzalloc(sizeof(*req
), gfp_flags
);
1683 req
->req
.dma
= DMA_ADDR_INVALID
;
1685 INIT_LIST_HEAD(&req
->queue
);
1690 static void qe_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1694 req
= container_of(_req
, struct qe_req
, req
);
1700 static int __qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1702 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1703 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1708 /* catch various bogus parameters */
1709 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
1710 || !list_empty(&req
->queue
)) {
1711 dev_dbg(udc
->dev
, "bad params\n");
1714 if (!_ep
|| (!ep
->ep
.desc
&& ep_index(ep
))) {
1715 dev_dbg(udc
->dev
, "bad ep\n");
1719 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1724 /* map virtual address to hardware */
1725 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
1726 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
1734 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
1735 req
->req
.dma
, req
->req
.length
,
1742 req
->req
.status
= -EINPROGRESS
;
1743 req
->req
.actual
= 0;
1745 list_add_tail(&req
->queue
, &ep
->queue
);
1746 dev_vdbg(udc
->dev
, "gadget have request in %s! %d\n",
1747 ep
->name
, req
->req
.length
);
1749 /* push the request to device */
1751 reval
= ep_req_send(ep
, req
);
1754 if (ep_index(ep
) == 0 && req
->req
.length
> 0) {
1756 udc
->ep0_state
= DATA_STATE_XMIT
;
1758 udc
->ep0_state
= DATA_STATE_RECV
;
1761 if (ep
->dir
== USB_DIR_OUT
)
1762 reval
= ep_req_receive(ep
, req
);
1767 /* queues (submits) an I/O request to an endpoint */
1768 static int qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1771 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1772 struct qe_udc
*udc
= ep
->udc
;
1773 unsigned long flags
;
1776 spin_lock_irqsave(&udc
->lock
, flags
);
1777 ret
= __qe_ep_queue(_ep
, _req
);
1778 spin_unlock_irqrestore(&udc
->lock
, flags
);
1782 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1783 static int qe_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1785 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1787 unsigned long flags
;
1792 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1794 /* make sure it's actually queued on this endpoint */
1795 list_for_each_entry(req
, &ep
->queue
, queue
) {
1796 if (&req
->req
== _req
)
1800 if (&req
->req
!= _req
) {
1801 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1805 done(ep
, req
, -ECONNRESET
);
1807 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1811 /*-----------------------------------------------------------------
1812 * modify the endpoint halt feature
1813 * @ep: the non-isochronous endpoint being stalled
1814 * @value: 1--set halt 0--clear halt
1815 * Returns zero, or a negative error code.
1816 *----------------------------------------------------------------*/
1817 static int qe_ep_set_halt(struct usb_ep
*_ep
, int value
)
1820 unsigned long flags
;
1821 int status
= -EOPNOTSUPP
;
1824 ep
= container_of(_ep
, struct qe_ep
, ep
);
1825 if (!_ep
|| !ep
->ep
.desc
) {
1831 /* Attempt to halt IN ep will fail if any transfer requests
1832 * are still queue */
1833 if (value
&& ep_is_in(ep
) && !list_empty(&ep
->queue
)) {
1839 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1840 qe_eptx_stall_change(ep
, value
);
1841 qe_eprx_stall_change(ep
, value
);
1842 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1844 if (ep
->epnum
== 0) {
1845 udc
->ep0_state
= WAIT_FOR_SETUP
;
1849 /* set data toggle to DATA0 on clear halt */
1853 dev_vdbg(udc
->dev
, "%s %s halt stat %d\n", ep
->ep
.name
,
1854 value
? "set" : "clear", status
);
1859 static struct usb_ep_ops qe_ep_ops
= {
1860 .enable
= qe_ep_enable
,
1861 .disable
= qe_ep_disable
,
1863 .alloc_request
= qe_alloc_request
,
1864 .free_request
= qe_free_request
,
1866 .queue
= qe_ep_queue
,
1867 .dequeue
= qe_ep_dequeue
,
1869 .set_halt
= qe_ep_set_halt
,
1872 /*------------------------------------------------------------------------
1873 Gadget Driver Layer Operations
1874 ------------------------------------------------------------------------*/
1876 /* Get the current frame number */
1877 static int qe_get_frame(struct usb_gadget
*gadget
)
1879 struct qe_udc
*udc
= container_of(gadget
, struct qe_udc
, gadget
);
1882 tmp
= in_be16(&udc
->usb_param
->frame_n
);
1891 static int fsl_qe_start(struct usb_gadget
*gadget
,
1892 struct usb_gadget_driver
*driver
);
1893 static int fsl_qe_stop(struct usb_gadget
*gadget
,
1894 struct usb_gadget_driver
*driver
);
1896 /* defined in usb_gadget.h */
1897 static struct usb_gadget_ops qe_gadget_ops
= {
1898 .get_frame
= qe_get_frame
,
1899 .udc_start
= fsl_qe_start
,
1900 .udc_stop
= fsl_qe_stop
,
1903 /*-------------------------------------------------------------------------
1904 USB ep0 Setup process in BUS Enumeration
1905 -------------------------------------------------------------------------*/
1906 static int udc_reset_ep_queue(struct qe_udc
*udc
, u8 pipe
)
1908 struct qe_ep
*ep
= &udc
->eps
[pipe
];
1910 nuke(ep
, -ECONNRESET
);
1915 static int reset_queues(struct qe_udc
*udc
)
1919 for (pipe
= 0; pipe
< USB_MAX_ENDPOINTS
; pipe
++)
1920 udc_reset_ep_queue(udc
, pipe
);
1922 /* report disconnect; the driver is already quiesced */
1923 spin_unlock(&udc
->lock
);
1924 udc
->driver
->disconnect(&udc
->gadget
);
1925 spin_lock(&udc
->lock
);
1930 static void ch9setaddress(struct qe_udc
*udc
, u16 value
, u16 index
,
1933 /* Save the new address to device struct */
1934 udc
->device_address
= (u8
) value
;
1935 /* Update usb state */
1936 udc
->usb_state
= USB_STATE_ADDRESS
;
1938 /* Status phase , send a ZLP */
1939 if (ep0_prime_status(udc
, USB_DIR_IN
))
1943 static void ownercomplete(struct usb_ep
*_ep
, struct usb_request
*_req
)
1945 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1947 req
->req
.buf
= NULL
;
1951 static void ch9getstatus(struct qe_udc
*udc
, u8 request_type
, u16 value
,
1952 u16 index
, u16 length
)
1960 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1961 /* Get device status */
1962 usb_status
= 1 << USB_DEVICE_SELF_POWERED
;
1963 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
1964 /* Get interface status */
1965 /* We don't have interface information in udc driver */
1967 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
1968 /* Get endpoint status */
1969 int pipe
= index
& USB_ENDPOINT_NUMBER_MASK
;
1970 struct qe_ep
*target_ep
= &udc
->eps
[pipe
];
1973 /* stall if endpoint doesn't exist */
1974 if (!target_ep
->ep
.desc
)
1977 usep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe
]);
1978 if (index
& USB_DIR_IN
) {
1979 if (target_ep
->dir
!= USB_DIR_IN
)
1981 if ((usep
& USB_THS_MASK
) == USB_THS_STALL
)
1982 usb_status
= 1 << USB_ENDPOINT_HALT
;
1984 if (target_ep
->dir
!= USB_DIR_OUT
)
1986 if ((usep
& USB_RHS_MASK
) == USB_RHS_STALL
)
1987 usb_status
= 1 << USB_ENDPOINT_HALT
;
1991 req
= container_of(qe_alloc_request(&ep
->ep
, GFP_KERNEL
),
1992 struct qe_req
, req
);
1993 req
->req
.length
= 2;
1994 req
->req
.buf
= udc
->statusbuf
;
1995 *(u16
*)req
->req
.buf
= cpu_to_le16(usb_status
);
1996 req
->req
.status
= -EINPROGRESS
;
1997 req
->req
.actual
= 0;
1998 req
->req
.complete
= ownercomplete
;
2000 udc
->ep0_dir
= USB_DIR_IN
;
2003 status
= __qe_ep_queue(&ep
->ep
, &req
->req
);
2008 dev_err(udc
->dev
, "Can't respond to getstatus request \n");
2012 /* only handle the setup request, suppose the device in normal status */
2013 static void setup_received_handle(struct qe_udc
*udc
,
2014 struct usb_ctrlrequest
*setup
)
2016 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2017 u16 wValue
= le16_to_cpu(setup
->wValue
);
2018 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
2019 u16 wLength
= le16_to_cpu(setup
->wLength
);
2021 /* clear the previous request in the ep0 */
2022 udc_reset_ep_queue(udc
, 0);
2024 if (setup
->bRequestType
& USB_DIR_IN
)
2025 udc
->ep0_dir
= USB_DIR_IN
;
2027 udc
->ep0_dir
= USB_DIR_OUT
;
2029 switch (setup
->bRequest
) {
2030 case USB_REQ_GET_STATUS
:
2031 /* Data+Status phase form udc */
2032 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
2033 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
2035 ch9getstatus(udc
, setup
->bRequestType
, wValue
, wIndex
,
2039 case USB_REQ_SET_ADDRESS
:
2040 /* Status phase from udc */
2041 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
2044 ch9setaddress(udc
, wValue
, wIndex
, wLength
);
2047 case USB_REQ_CLEAR_FEATURE
:
2048 case USB_REQ_SET_FEATURE
:
2049 /* Requests with no data phase, status phase from udc */
2050 if ((setup
->bRequestType
& USB_TYPE_MASK
)
2051 != USB_TYPE_STANDARD
)
2054 if ((setup
->bRequestType
& USB_RECIP_MASK
)
2055 == USB_RECIP_ENDPOINT
) {
2056 int pipe
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2059 if (wValue
!= 0 || wLength
!= 0
2060 || pipe
> USB_MAX_ENDPOINTS
)
2062 ep
= &udc
->eps
[pipe
];
2064 spin_unlock(&udc
->lock
);
2065 qe_ep_set_halt(&ep
->ep
,
2066 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2068 spin_lock(&udc
->lock
);
2071 ep0_prime_status(udc
, USB_DIR_IN
);
2080 /* Data phase from gadget, status phase from udc */
2081 if (setup
->bRequestType
& USB_DIR_IN
) {
2082 udc
->ep0_state
= DATA_STATE_XMIT
;
2083 udc
->ep0_dir
= USB_DIR_IN
;
2085 udc
->ep0_state
= DATA_STATE_RECV
;
2086 udc
->ep0_dir
= USB_DIR_OUT
;
2088 spin_unlock(&udc
->lock
);
2089 if (udc
->driver
->setup(&udc
->gadget
,
2090 &udc
->local_setup_buff
) < 0)
2092 spin_lock(&udc
->lock
);
2094 /* No data phase, IN status from gadget */
2095 udc
->ep0_dir
= USB_DIR_IN
;
2096 spin_unlock(&udc
->lock
);
2097 if (udc
->driver
->setup(&udc
->gadget
,
2098 &udc
->local_setup_buff
) < 0)
2100 spin_lock(&udc
->lock
);
2101 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
2105 /*-------------------------------------------------------------------------
2106 USB Interrupt handlers
2107 -------------------------------------------------------------------------*/
2108 static void suspend_irq(struct qe_udc
*udc
)
2110 udc
->resume_state
= udc
->usb_state
;
2111 udc
->usb_state
= USB_STATE_SUSPENDED
;
2113 /* report suspend to the driver ,serial.c not support this*/
2114 if (udc
->driver
->suspend
)
2115 udc
->driver
->suspend(&udc
->gadget
);
2118 static void resume_irq(struct qe_udc
*udc
)
2120 udc
->usb_state
= udc
->resume_state
;
2121 udc
->resume_state
= 0;
2123 /* report resume to the driver , serial.c not support this*/
2124 if (udc
->driver
->resume
)
2125 udc
->driver
->resume(&udc
->gadget
);
2128 static void idle_irq(struct qe_udc
*udc
)
2132 usbs
= in_8(&udc
->usb_regs
->usb_usbs
);
2133 if (usbs
& USB_IDLE_STATUS_MASK
) {
2134 if ((udc
->usb_state
) != USB_STATE_SUSPENDED
)
2137 if (udc
->usb_state
== USB_STATE_SUSPENDED
)
2142 static int reset_irq(struct qe_udc
*udc
)
2146 if (udc
->usb_state
== USB_STATE_DEFAULT
)
2149 qe_usb_disable(udc
);
2150 out_8(&udc
->usb_regs
->usb_usadr
, 0);
2152 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2153 if (udc
->eps
[i
].init
)
2154 qe_ep_reset(udc
, i
);
2158 udc
->usb_state
= USB_STATE_DEFAULT
;
2159 udc
->ep0_state
= WAIT_FOR_SETUP
;
2160 udc
->ep0_dir
= USB_DIR_OUT
;
2165 static int bsy_irq(struct qe_udc
*udc
)
2170 static int txe_irq(struct qe_udc
*udc
)
2175 /* ep0 tx interrupt also in here */
2176 static int tx_irq(struct qe_udc
*udc
)
2179 struct qe_bd __iomem
*bd
;
2182 if ((udc
->usb_state
== USB_STATE_ADDRESS
)
2183 && (in_8(&udc
->usb_regs
->usb_usadr
) == 0))
2184 out_8(&udc
->usb_regs
->usb_usadr
, udc
->device_address
);
2186 for (i
= (USB_MAX_ENDPOINTS
-1); ((i
>= 0) && (res
== 0)); i
--) {
2188 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_OUT
)) {
2190 if (!(in_be32((u32 __iomem
*)bd
) & T_R
)
2191 && (in_be32(&bd
->buf
))) {
2192 /* confirm the transmitted bd */
2194 res
= qe_ep0_txconf(ep
);
2196 res
= qe_ep_txconf(ep
);
2204 /* setup packect's rx is handle in the function too */
2205 static void rx_irq(struct qe_udc
*udc
)
2208 struct qe_bd __iomem
*bd
;
2211 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2213 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_IN
)) {
2215 if (!(in_be32((u32 __iomem
*)bd
) & R_E
)
2216 && (in_be32(&bd
->buf
))) {
2217 if (ep
->epnum
== 0) {
2220 /*non-setup package receive*/
2228 static irqreturn_t
qe_udc_irq(int irq
, void *_udc
)
2230 struct qe_udc
*udc
= (struct qe_udc
*)_udc
;
2232 irqreturn_t status
= IRQ_NONE
;
2233 unsigned long flags
;
2235 spin_lock_irqsave(&udc
->lock
, flags
);
2237 irq_src
= in_be16(&udc
->usb_regs
->usb_usber
) &
2238 in_be16(&udc
->usb_regs
->usb_usbmr
);
2239 /* Clear notification bits */
2240 out_be16(&udc
->usb_regs
->usb_usber
, irq_src
);
2242 if (irq_src
& USB_E_IDLE_MASK
) {
2244 irq_src
&= ~USB_E_IDLE_MASK
;
2245 status
= IRQ_HANDLED
;
2248 if (irq_src
& USB_E_TXB_MASK
) {
2250 irq_src
&= ~USB_E_TXB_MASK
;
2251 status
= IRQ_HANDLED
;
2254 if (irq_src
& USB_E_RXB_MASK
) {
2256 irq_src
&= ~USB_E_RXB_MASK
;
2257 status
= IRQ_HANDLED
;
2260 if (irq_src
& USB_E_RESET_MASK
) {
2262 irq_src
&= ~USB_E_RESET_MASK
;
2263 status
= IRQ_HANDLED
;
2266 if (irq_src
& USB_E_BSY_MASK
) {
2268 irq_src
&= ~USB_E_BSY_MASK
;
2269 status
= IRQ_HANDLED
;
2272 if (irq_src
& USB_E_TXE_MASK
) {
2274 irq_src
&= ~USB_E_TXE_MASK
;
2275 status
= IRQ_HANDLED
;
2278 spin_unlock_irqrestore(&udc
->lock
, flags
);
2283 /*-------------------------------------------------------------------------
2284 Gadget driver probe and unregister.
2285 --------------------------------------------------------------------------*/
2286 static int fsl_qe_start(struct usb_gadget
*gadget
,
2287 struct usb_gadget_driver
*driver
)
2290 unsigned long flags
;
2292 udc
= container_of(gadget
, struct qe_udc
, gadget
);
2293 /* lock is needed but whether should use this lock or another */
2294 spin_lock_irqsave(&udc
->lock
, flags
);
2296 driver
->driver
.bus
= NULL
;
2297 /* hook up the driver */
2298 udc
->driver
= driver
;
2299 udc
->gadget
.dev
.driver
= &driver
->driver
;
2300 udc
->gadget
.speed
= driver
->max_speed
;
2302 /* Enable IRQ reg and Set usbcmd reg EN bit */
2305 out_be16(&udc
->usb_regs
->usb_usber
, 0xffff);
2306 out_be16(&udc
->usb_regs
->usb_usbmr
, USB_E_DEFAULT_DEVICE
);
2307 udc
->usb_state
= USB_STATE_ATTACHED
;
2308 udc
->ep0_state
= WAIT_FOR_SETUP
;
2309 udc
->ep0_dir
= USB_DIR_OUT
;
2310 spin_unlock_irqrestore(&udc
->lock
, flags
);
2312 dev_info(udc
->dev
, "%s bind to driver %s\n", udc
->gadget
.name
,
2313 driver
->driver
.name
);
2317 static int fsl_qe_stop(struct usb_gadget
*gadget
,
2318 struct usb_gadget_driver
*driver
)
2321 struct qe_ep
*loop_ep
;
2322 unsigned long flags
;
2324 udc
= container_of(gadget
, struct qe_udc
, gadget
);
2325 /* stop usb controller, disable intr */
2326 qe_usb_disable(udc
);
2328 /* in fact, no needed */
2329 udc
->usb_state
= USB_STATE_ATTACHED
;
2330 udc
->ep0_state
= WAIT_FOR_SETUP
;
2333 /* stand operation */
2334 spin_lock_irqsave(&udc
->lock
, flags
);
2335 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2336 nuke(&udc
->eps
[0], -ESHUTDOWN
);
2337 list_for_each_entry(loop_ep
, &udc
->gadget
.ep_list
, ep
.ep_list
)
2338 nuke(loop_ep
, -ESHUTDOWN
);
2339 spin_unlock_irqrestore(&udc
->lock
, flags
);
2341 udc
->gadget
.dev
.driver
= NULL
;
2344 dev_info(udc
->dev
, "unregistered gadget driver '%s'\r\n",
2345 driver
->driver
.name
);
2349 /* udc structure's alloc and setup, include ep-param alloc */
2350 static struct qe_udc __devinit
*qe_udc_config(struct platform_device
*ofdev
)
2353 struct device_node
*np
= ofdev
->dev
.of_node
;
2354 unsigned int tmp_addr
= 0;
2355 struct usb_device_para __iomem
*usbpram
;
2360 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
2362 dev_err(&ofdev
->dev
, "malloc udc failed\n");
2366 udc
->dev
= &ofdev
->dev
;
2368 /* get default address of usb parameter in MURAM from device tree */
2369 offset
= *of_get_address(np
, 1, &size
, NULL
);
2370 udc
->usb_param
= cpm_muram_addr(offset
);
2371 memset_io(udc
->usb_param
, 0, size
);
2373 usbpram
= udc
->usb_param
;
2374 out_be16(&usbpram
->frame_n
, 0);
2375 out_be32(&usbpram
->rstate
, 0);
2377 tmp_addr
= cpm_muram_alloc((USB_MAX_ENDPOINTS
*
2378 sizeof(struct usb_ep_para
)),
2379 USB_EP_PARA_ALIGNMENT
);
2380 if (IS_ERR_VALUE(tmp_addr
))
2383 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2384 out_be16(&usbpram
->epptr
[i
], (u16
)tmp_addr
);
2385 udc
->ep_param
[i
] = cpm_muram_addr(tmp_addr
);
2389 memset_io(udc
->ep_param
[0], 0,
2390 USB_MAX_ENDPOINTS
* sizeof(struct usb_ep_para
));
2392 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2393 udc
->usb_state
= USB_STATE_POWERED
;
2396 spin_lock_init(&udc
->lock
);
2404 /* USB Controller register init */
2405 static int __devinit
qe_udc_reg_init(struct qe_udc
*udc
)
2407 struct usb_ctlr __iomem
*qe_usbregs
;
2408 qe_usbregs
= udc
->usb_regs
;
2410 /* Spec says that we must enable the USB controller to change mode. */
2411 out_8(&qe_usbregs
->usb_usmod
, 0x01);
2412 /* Mode changed, now disable it, since muram isn't initialized yet. */
2413 out_8(&qe_usbregs
->usb_usmod
, 0x00);
2415 /* Initialize the rest. */
2416 out_be16(&qe_usbregs
->usb_usbmr
, 0);
2417 out_8(&qe_usbregs
->usb_uscom
, 0);
2418 out_be16(&qe_usbregs
->usb_usber
, USBER_ALL_CLEAR
);
2423 static int __devinit
qe_ep_config(struct qe_udc
*udc
, unsigned char pipe_num
)
2425 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
2428 strcpy(ep
->name
, ep_name
[pipe_num
]);
2429 ep
->ep
.name
= ep_name
[pipe_num
];
2431 ep
->ep
.ops
= &qe_ep_ops
;
2433 ep
->ep
.maxpacket
= (unsigned short) ~0;
2436 ep
->epnum
= (u8
)pipe_num
;
2443 ep
->state
= EP_STATE_IDLE
;
2446 /* the queue lists any req for this ep */
2447 INIT_LIST_HEAD(&ep
->queue
);
2449 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2451 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2453 ep
->gadget
= &udc
->gadget
;
2458 /*-----------------------------------------------------------------------
2459 * UDC device Driver operation functions *
2460 *----------------------------------------------------------------------*/
2461 static void qe_udc_release(struct device
*dev
)
2463 struct qe_udc
*udc
= container_of(dev
, struct qe_udc
, gadget
.dev
);
2466 complete(udc
->done
);
2467 cpm_muram_free(cpm_muram_offset(udc
->ep_param
[0]));
2468 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
2469 udc
->ep_param
[i
] = NULL
;
2474 /* Driver probe functions */
2475 static const struct of_device_id qe_udc_match
[];
2476 static int __devinit
qe_udc_probe(struct platform_device
*ofdev
)
2479 const struct of_device_id
*match
;
2480 struct device_node
*np
= ofdev
->dev
.of_node
;
2482 unsigned int ret
= 0;
2486 match
= of_match_device(qe_udc_match
, &ofdev
->dev
);
2490 prop
= of_get_property(np
, "mode", NULL
);
2491 if (!prop
|| strcmp(prop
, "peripheral"))
2494 /* Initialize the udc structure including QH member and other member */
2495 udc
= qe_udc_config(ofdev
);
2497 dev_err(&ofdev
->dev
, "failed to initialize\n");
2501 udc
->soc_type
= (unsigned long)match
->data
;
2502 udc
->usb_regs
= of_iomap(np
, 0);
2503 if (!udc
->usb_regs
) {
2508 /* initialize usb hw reg except for regs for EP,
2509 * leave usbintr reg untouched*/
2510 qe_udc_reg_init(udc
);
2512 /* here comes the stand operations for probe
2513 * set the qe_udc->gadget.xxx */
2514 udc
->gadget
.ops
= &qe_gadget_ops
;
2516 /* gadget.ep0 is a pointer */
2517 udc
->gadget
.ep0
= &udc
->eps
[0].ep
;
2519 INIT_LIST_HEAD(&udc
->gadget
.ep_list
);
2521 /* modify in register gadget process */
2522 udc
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2524 /* name: Identifies the controller hardware type. */
2525 udc
->gadget
.name
= driver_name
;
2527 device_initialize(&udc
->gadget
.dev
);
2529 dev_set_name(&udc
->gadget
.dev
, "gadget");
2531 udc
->gadget
.dev
.release
= qe_udc_release
;
2532 udc
->gadget
.dev
.parent
= &ofdev
->dev
;
2534 /* initialize qe_ep struct */
2535 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2536 /* because the ep type isn't decide here so
2537 * qe_ep_init() should be called in ep_enable() */
2539 /* setup the qe_ep struct and link ep.ep.list
2540 * into gadget.ep_list */
2541 qe_ep_config(udc
, (unsigned char)i
);
2544 /* ep0 initialization in here */
2545 ret
= qe_ep_init(udc
, 0, &qe_ep0_desc
);
2549 /* create a buf for ZLP send, need to remain zeroed */
2550 udc
->nullbuf
= kzalloc(256, GFP_KERNEL
);
2551 if (udc
->nullbuf
== NULL
) {
2552 dev_err(udc
->dev
, "cannot alloc nullbuf\n");
2557 /* buffer for data of get_status request */
2558 udc
->statusbuf
= kzalloc(2, GFP_KERNEL
);
2559 if (udc
->statusbuf
== NULL
) {
2564 udc
->nullp
= virt_to_phys((void *)udc
->nullbuf
);
2565 if (udc
->nullp
== DMA_ADDR_INVALID
) {
2566 udc
->nullp
= dma_map_single(
2567 udc
->gadget
.dev
.parent
,
2573 dma_sync_single_for_device(udc
->gadget
.dev
.parent
,
2578 tasklet_init(&udc
->rx_tasklet
, ep_rx_tasklet
,
2579 (unsigned long)udc
);
2580 /* request irq and disable DR */
2581 udc
->usb_irq
= irq_of_parse_and_map(np
, 0);
2582 if (!udc
->usb_irq
) {
2587 ret
= request_irq(udc
->usb_irq
, qe_udc_irq
, 0,
2590 dev_err(udc
->dev
, "cannot request irq %d err %d\n",
2595 ret
= device_add(&udc
->gadget
.dev
);
2599 ret
= usb_add_gadget_udc(&ofdev
->dev
, &udc
->gadget
);
2603 dev_set_drvdata(&ofdev
->dev
, udc
);
2605 "%s USB controller initialized as device\n",
2606 (udc
->soc_type
== PORT_QE
) ? "QE" : "CPM");
2610 device_unregister(&udc
->gadget
.dev
);
2612 free_irq(udc
->usb_irq
, udc
);
2614 irq_dispose_mapping(udc
->usb_irq
);
2617 dma_unmap_single(udc
->gadget
.dev
.parent
,
2620 udc
->nullp
= DMA_ADDR_INVALID
;
2622 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
2626 kfree(udc
->statusbuf
);
2628 kfree(udc
->nullbuf
);
2631 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2633 kfree(ep
->rxbuffer
);
2636 iounmap(udc
->usb_regs
);
2643 static int qe_udc_suspend(struct platform_device
*dev
, pm_message_t state
)
2648 static int qe_udc_resume(struct platform_device
*dev
)
2654 static int __devexit
qe_udc_remove(struct platform_device
*ofdev
)
2656 struct qe_udc
*udc
= dev_get_drvdata(&ofdev
->dev
);
2659 DECLARE_COMPLETION(done
);
2661 usb_del_gadget_udc(&udc
->gadget
);
2664 tasklet_disable(&udc
->rx_tasklet
);
2667 dma_unmap_single(udc
->gadget
.dev
.parent
,
2670 udc
->nullp
= DMA_ADDR_INVALID
;
2672 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
2676 kfree(udc
->statusbuf
);
2677 kfree(udc
->nullbuf
);
2680 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2681 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (USB_BDRING_LEN
+ 1);
2685 dma_unmap_single(udc
->gadget
.dev
.parent
,
2688 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
2690 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
2695 kfree(ep
->rxbuffer
);
2698 free_irq(udc
->usb_irq
, udc
);
2699 irq_dispose_mapping(udc
->usb_irq
);
2701 tasklet_kill(&udc
->rx_tasklet
);
2703 iounmap(udc
->usb_regs
);
2705 device_unregister(&udc
->gadget
.dev
);
2706 /* wait for release() of gadget.dev to free udc */
2707 wait_for_completion(&done
);
2712 /*-------------------------------------------------------------------------*/
2713 static const struct of_device_id qe_udc_match
[] __devinitconst
= {
2715 .compatible
= "fsl,mpc8323-qe-usb",
2716 .data
= (void *)PORT_QE
,
2719 .compatible
= "fsl,mpc8360-qe-usb",
2720 .data
= (void *)PORT_QE
,
2723 .compatible
= "fsl,mpc8272-cpm-usb",
2724 .data
= (void *)PORT_CPM
,
2729 MODULE_DEVICE_TABLE(of
, qe_udc_match
);
2731 static struct platform_driver udc_driver
= {
2733 .name
= (char *)driver_name
,
2734 .owner
= THIS_MODULE
,
2735 .of_match_table
= qe_udc_match
,
2737 .probe
= qe_udc_probe
,
2738 .remove
= __devexit_p(qe_udc_remove
),
2740 .suspend
= qe_udc_suspend
,
2741 .resume
= qe_udc_resume
,
2745 module_platform_driver(udc_driver
);
2747 MODULE_DESCRIPTION(DRIVER_DESC
);
2748 MODULE_AUTHOR(DRIVER_AUTHOR
);
2749 MODULE_LICENSE("GPL");