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_platform.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/usb/ch9.h>
38 #include <linux/usb/gadget.h>
39 #include <linux/usb/otg.h>
44 #include "fsl_qe_udc.h"
46 #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver"
47 #define DRIVER_AUTHOR "Xie XiaoBo"
48 #define DRIVER_VERSION "1.0"
50 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
52 static const char driver_name
[] = "fsl_qe_udc";
53 static const char driver_desc
[] = DRIVER_DESC
;
55 /*ep name is important in gadget, it should obey the convention of ep_match()*/
56 static const char *const ep_name
[] = {
57 "ep0-control", /* everyone has ep0 */
58 /* 3 configurable endpoints */
64 static struct usb_endpoint_descriptor qe_ep0_desc
= {
65 .bLength
= USB_DT_ENDPOINT_SIZE
,
66 .bDescriptorType
= USB_DT_ENDPOINT
,
68 .bEndpointAddress
= 0,
69 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
70 .wMaxPacketSize
= USB_MAX_CTRL_PAYLOAD
,
73 /* it is initialized in probe() */
74 static struct qe_udc
*udc_controller
;
76 /********************************************************************
77 * Internal Used Function Start
78 ********************************************************************/
79 /*-----------------------------------------------------------------
80 * done() - retire a request; caller blocked irqs
81 *--------------------------------------------------------------*/
82 static void done(struct qe_ep
*ep
, struct qe_req
*req
, int status
)
84 struct qe_udc
*udc
= ep
->udc
;
85 unsigned char stopped
= ep
->stopped
;
87 /* the req->queue pointer is used by ep_queue() func, in which
88 * the request will be added into a udc_ep->queue 'd tail
89 * so here the req will be dropped from the ep->queue
91 list_del_init(&req
->queue
);
93 /* req.status should be set as -EINPROGRESS in ep_queue() */
94 if (req
->req
.status
== -EINPROGRESS
)
95 req
->req
.status
= status
;
97 status
= req
->req
.status
;
100 dma_unmap_single(udc
->gadget
.dev
.parent
,
101 req
->req
.dma
, req
->req
.length
,
105 req
->req
.dma
= DMA_ADDR_INVALID
;
108 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
109 req
->req
.dma
, req
->req
.length
,
114 if (status
&& (status
!= -ESHUTDOWN
))
115 dev_vdbg(udc
->dev
, "complete %s req %p stat %d len %u/%u\n",
116 ep
->ep
.name
, &req
->req
, status
,
117 req
->req
.actual
, req
->req
.length
);
119 /* don't modify queue heads during completion callback */
121 spin_unlock(&udc
->lock
);
123 /* this complete() should a func implemented by gadget layer,
124 * eg fsg->bulk_in_complete() */
125 if (req
->req
.complete
)
126 req
->req
.complete(&ep
->ep
, &req
->req
);
128 spin_lock(&udc
->lock
);
130 ep
->stopped
= stopped
;
133 /*-----------------------------------------------------------------
134 * nuke(): delete all requests related to this ep
135 *--------------------------------------------------------------*/
136 static void nuke(struct qe_ep
*ep
, int status
)
138 /* Whether this eq has request linked */
139 while (!list_empty(&ep
->queue
)) {
140 struct qe_req
*req
= NULL
;
141 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
143 done(ep
, req
, status
);
147 /*---------------------------------------------------------------------------*
148 * USB and Endpoint manipulate process, include parameter and register *
149 *---------------------------------------------------------------------------*/
150 /* @value: 1--set stall 0--clean stall */
151 static int qe_eprx_stall_change(struct qe_ep
*ep
, int value
)
154 u8 epnum
= ep
->epnum
;
155 struct qe_udc
*udc
= ep
->udc
;
157 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
158 tem_usep
= tem_usep
& ~USB_RHS_MASK
;
160 tem_usep
|= USB_RHS_STALL
;
161 else if (ep
->dir
== USB_DIR_IN
)
162 tem_usep
|= USB_RHS_IGNORE_OUT
;
164 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
168 static int qe_eptx_stall_change(struct qe_ep
*ep
, int value
)
171 u8 epnum
= ep
->epnum
;
172 struct qe_udc
*udc
= ep
->udc
;
174 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
175 tem_usep
= tem_usep
& ~USB_THS_MASK
;
177 tem_usep
|= USB_THS_STALL
;
178 else if (ep
->dir
== USB_DIR_OUT
)
179 tem_usep
|= USB_THS_IGNORE_IN
;
181 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
186 static int qe_ep0_stall(struct qe_udc
*udc
)
188 qe_eptx_stall_change(&udc
->eps
[0], 1);
189 qe_eprx_stall_change(&udc
->eps
[0], 1);
190 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
191 udc_controller
->ep0_dir
= 0;
195 static int qe_eprx_nack(struct qe_ep
*ep
)
197 u8 epnum
= ep
->epnum
;
198 struct qe_udc
*udc
= ep
->udc
;
200 if (ep
->state
== EP_STATE_IDLE
) {
201 /* Set the ep's nack */
202 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[epnum
],
203 USB_RHS_MASK
, USB_RHS_NACK
);
205 /* Mask Rx and Busy interrupts */
206 clrbits16(&udc
->usb_regs
->usb_usbmr
,
207 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
209 ep
->state
= EP_STATE_NACK
;
214 static int qe_eprx_normal(struct qe_ep
*ep
)
216 struct qe_udc
*udc
= ep
->udc
;
218 if (ep
->state
== EP_STATE_NACK
) {
219 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[ep
->epnum
],
220 USB_RTHS_MASK
, USB_THS_IGNORE_IN
);
222 /* Unmask RX interrupts */
223 out_be16(&udc
->usb_regs
->usb_usber
,
224 USB_E_BSY_MASK
| USB_E_RXB_MASK
);
225 setbits16(&udc
->usb_regs
->usb_usbmr
,
226 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
228 ep
->state
= EP_STATE_IDLE
;
235 static int qe_ep_cmd_stoptx(struct qe_ep
*ep
)
237 if (ep
->udc
->soc_type
== PORT_CPM
)
238 cpm_command(CPM_USB_STOP_TX
| (ep
->epnum
<< CPM_USB_EP_SHIFT
),
239 CPM_USB_STOP_TX_OPCODE
);
241 qe_issue_cmd(QE_USB_STOP_TX
, QE_CR_SUBBLOCK_USB
,
247 static int qe_ep_cmd_restarttx(struct qe_ep
*ep
)
249 if (ep
->udc
->soc_type
== PORT_CPM
)
250 cpm_command(CPM_USB_RESTART_TX
| (ep
->epnum
<<
251 CPM_USB_EP_SHIFT
), CPM_USB_RESTART_TX_OPCODE
);
253 qe_issue_cmd(QE_USB_RESTART_TX
, QE_CR_SUBBLOCK_USB
,
259 static int qe_ep_flushtxfifo(struct qe_ep
*ep
)
261 struct qe_udc
*udc
= ep
->udc
;
266 qe_ep_cmd_stoptx(ep
);
267 out_8(&udc
->usb_regs
->usb_uscom
,
268 USB_CMD_FLUSH_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
269 out_be16(&udc
->ep_param
[i
]->tbptr
, in_be16(&udc
->ep_param
[i
]->tbase
));
270 out_be32(&udc
->ep_param
[i
]->tstate
, 0);
271 out_be16(&udc
->ep_param
[i
]->tbcnt
, 0);
273 ep
->c_txbd
= ep
->txbase
;
274 ep
->n_txbd
= ep
->txbase
;
275 qe_ep_cmd_restarttx(ep
);
279 static int qe_ep_filltxfifo(struct qe_ep
*ep
)
281 struct qe_udc
*udc
= ep
->udc
;
283 out_8(&udc
->usb_regs
->usb_uscom
,
284 USB_CMD_STR_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
288 static int qe_epbds_reset(struct qe_udc
*udc
, int pipe_num
)
292 struct qe_bd __iomem
*bd
;
295 ep
= &udc
->eps
[pipe_num
];
297 if (ep
->dir
== USB_DIR_OUT
)
298 bdring_len
= USB_BDRING_LEN_RX
;
300 bdring_len
= USB_BDRING_LEN
;
303 for (i
= 0; i
< (bdring_len
- 1); i
++) {
304 out_be32((u32 __iomem
*)bd
, R_E
| R_I
);
307 out_be32((u32 __iomem
*)bd
, R_E
| R_I
| R_W
);
310 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
311 out_be32(&bd
->buf
, 0);
312 out_be32((u32 __iomem
*)bd
, 0);
315 out_be32((u32 __iomem
*)bd
, T_W
);
320 static int qe_ep_reset(struct qe_udc
*udc
, int pipe_num
)
325 ep
= &udc
->eps
[pipe_num
];
326 tmpusep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe_num
]);
327 tmpusep
&= ~USB_RTHS_MASK
;
331 qe_ep_flushtxfifo(ep
);
334 tmpusep
|= USB_THS_IGNORE_IN
;
337 qe_ep_flushtxfifo(ep
);
338 tmpusep
|= USB_RHS_IGNORE_OUT
;
343 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], tmpusep
);
345 qe_epbds_reset(udc
, pipe_num
);
350 static int qe_ep_toggledata01(struct qe_ep
*ep
)
356 static int qe_ep_bd_init(struct qe_udc
*udc
, unsigned char pipe_num
)
358 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
359 unsigned long tmp_addr
= 0;
360 struct usb_ep_para __iomem
*epparam
;
362 struct qe_bd __iomem
*bd
;
365 if (ep
->dir
== USB_DIR_OUT
)
366 bdring_len
= USB_BDRING_LEN_RX
;
368 bdring_len
= USB_BDRING_LEN
;
370 epparam
= udc
->ep_param
[pipe_num
];
371 /* alloc multi-ram for BD rings and set the ep parameters */
372 tmp_addr
= cpm_muram_alloc(sizeof(struct qe_bd
) * (bdring_len
+
373 USB_BDRING_LEN_TX
), QE_ALIGNMENT_OF_BD
);
374 if (IS_ERR_VALUE(tmp_addr
))
377 out_be16(&epparam
->rbase
, (u16
)tmp_addr
);
378 out_be16(&epparam
->tbase
, (u16
)(tmp_addr
+
379 (sizeof(struct qe_bd
) * bdring_len
)));
381 out_be16(&epparam
->rbptr
, in_be16(&epparam
->rbase
));
382 out_be16(&epparam
->tbptr
, in_be16(&epparam
->tbase
));
384 ep
->rxbase
= cpm_muram_addr(tmp_addr
);
385 ep
->txbase
= cpm_muram_addr(tmp_addr
+ (sizeof(struct qe_bd
)
387 ep
->n_rxbd
= ep
->rxbase
;
388 ep
->e_rxbd
= ep
->rxbase
;
389 ep
->n_txbd
= ep
->txbase
;
390 ep
->c_txbd
= ep
->txbase
;
391 ep
->data01
= 0; /* data0 */
393 /* Init TX and RX bds */
395 for (i
= 0; i
< bdring_len
- 1; i
++) {
396 out_be32(&bd
->buf
, 0);
397 out_be32((u32 __iomem
*)bd
, 0);
400 out_be32(&bd
->buf
, 0);
401 out_be32((u32 __iomem
*)bd
, R_W
);
404 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
405 out_be32(&bd
->buf
, 0);
406 out_be32((u32 __iomem
*)bd
, 0);
409 out_be32(&bd
->buf
, 0);
410 out_be32((u32 __iomem
*)bd
, T_W
);
415 static int qe_ep_rxbd_update(struct qe_ep
*ep
)
420 struct qe_bd __iomem
*bd
;
421 unsigned int bdring_len
;
423 if (ep
->rxbase
== NULL
)
428 ep
->rxframe
= kmalloc(sizeof(*ep
->rxframe
), GFP_ATOMIC
);
429 if (ep
->rxframe
== NULL
) {
430 dev_err(ep
->udc
->dev
, "malloc rxframe failed\n");
434 qe_frame_init(ep
->rxframe
);
436 if (ep
->dir
== USB_DIR_OUT
)
437 bdring_len
= USB_BDRING_LEN_RX
;
439 bdring_len
= USB_BDRING_LEN
;
441 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (bdring_len
+ 1);
442 ep
->rxbuffer
= kzalloc(size
, GFP_ATOMIC
);
443 if (ep
->rxbuffer
== NULL
) {
444 dev_err(ep
->udc
->dev
, "malloc rxbuffer failed,size=%d\n",
450 ep
->rxbuf_d
= virt_to_phys((void *)ep
->rxbuffer
);
451 if (ep
->rxbuf_d
== DMA_ADDR_INVALID
) {
452 ep
->rxbuf_d
= dma_map_single(udc_controller
->gadget
.dev
.parent
,
458 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
464 size
= ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2;
466 tmp
= (u32
)(((tmp
>> 2) << 2) + 4);
468 for (i
= 0; i
< bdring_len
- 1; i
++) {
469 out_be32(&bd
->buf
, tmp
);
470 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
));
474 out_be32(&bd
->buf
, tmp
);
475 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
| R_W
));
480 static int qe_ep_register_init(struct qe_udc
*udc
, unsigned char pipe_num
)
482 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
483 struct usb_ep_para __iomem
*epparam
;
488 epparam
= udc
->ep_param
[pipe_num
];
491 logepnum
= (ep
->desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
492 usep
|= (logepnum
<< USB_EPNUM_SHIFT
);
494 switch (ep
->desc
->bmAttributes
& 0x03) {
495 case USB_ENDPOINT_XFER_BULK
:
496 usep
|= USB_TRANS_BULK
;
498 case USB_ENDPOINT_XFER_ISOC
:
499 usep
|= USB_TRANS_ISO
;
501 case USB_ENDPOINT_XFER_INT
:
502 usep
|= USB_TRANS_INT
;
505 usep
|= USB_TRANS_CTR
;
511 usep
|= USB_THS_IGNORE_IN
;
514 usep
|= USB_RHS_IGNORE_OUT
;
519 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], usep
);
522 out_8(&epparam
->rbmr
, rtfcr
);
523 out_8(&epparam
->tbmr
, rtfcr
);
525 tmp
= (u16
)(ep
->ep
.maxpacket
+ USB_CRC_SIZE
);
526 /* MRBLR must be divisble by 4 */
527 tmp
= (u16
)(((tmp
>> 2) << 2) + 4);
528 out_be16(&epparam
->mrblr
, tmp
);
533 static int qe_ep_init(struct qe_udc
*udc
,
534 unsigned char pipe_num
,
535 const struct usb_endpoint_descriptor
*desc
)
537 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
542 max
= le16_to_cpu(desc
->wMaxPacketSize
);
544 /* check the max package size validate for this endpoint */
545 /* Refer to USB2.0 spec table 9-13,
548 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
549 case USB_ENDPOINT_XFER_BULK
:
550 if (strstr(ep
->ep
.name
, "-iso")
551 || strstr(ep
->ep
.name
, "-int"))
553 switch (udc
->gadget
.speed
) {
555 if ((max
== 128) || (max
== 256) || (max
== 512))
571 case USB_ENDPOINT_XFER_INT
:
572 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
574 switch (udc
->gadget
.speed
) {
587 case USB_ENDPOINT_XFER_ISOC
:
588 if (strstr(ep
->ep
.name
, "-bulk")
589 || strstr(ep
->ep
.name
, "-int"))
591 switch (udc
->gadget
.speed
) {
602 case USB_ENDPOINT_XFER_CONTROL
:
603 if (strstr(ep
->ep
.name
, "-iso")
604 || strstr(ep
->ep
.name
, "-int"))
606 switch (udc
->gadget
.speed
) {
641 spin_lock_irqsave(&udc
->lock
, flags
);
643 /* initialize ep structure */
644 ep
->ep
.maxpacket
= max
;
645 ep
->tm
= (u8
)(desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
651 ep
->dir
= USB_DIR_BOTH
;
652 udc
->ep0_dir
= USB_DIR_OUT
;
653 udc
->ep0_state
= WAIT_FOR_SETUP
;
655 switch (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) {
657 ep
->dir
= USB_DIR_OUT
;
660 ep
->dir
= USB_DIR_IN
;
666 /* hardware special operation */
667 qe_ep_bd_init(udc
, pipe_num
);
668 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_OUT
)) {
669 reval
= qe_ep_rxbd_update(ep
);
674 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_IN
)) {
675 ep
->txframe
= kmalloc(sizeof(*ep
->txframe
), GFP_ATOMIC
);
676 if (ep
->txframe
== NULL
) {
677 dev_err(udc
->dev
, "malloc txframe failed\n");
680 qe_frame_init(ep
->txframe
);
683 qe_ep_register_init(udc
, pipe_num
);
685 /* Now HW will be NAKing transfers to that EP,
686 * until a buffer is queued to it. */
687 spin_unlock_irqrestore(&udc
->lock
, flags
);
694 spin_unlock_irqrestore(&udc
->lock
, flags
);
696 dev_err(udc
->dev
, "failed to initialize %s\n", ep
->ep
.name
);
700 static inline void qe_usb_enable(void)
702 setbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
705 static inline void qe_usb_disable(void)
707 clrbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
710 /*----------------------------------------------------------------------------*
711 * USB and EP basic manipulate function end *
712 *----------------------------------------------------------------------------*/
715 /******************************************************************************
716 UDC transmit and receive process
717 ******************************************************************************/
718 static void recycle_one_rxbd(struct qe_ep
*ep
)
722 bdstatus
= in_be32((u32 __iomem
*)ep
->e_rxbd
);
723 bdstatus
= R_I
| R_E
| (bdstatus
& R_W
);
724 out_be32((u32 __iomem
*)ep
->e_rxbd
, bdstatus
);
727 ep
->e_rxbd
= ep
->rxbase
;
732 static void recycle_rxbds(struct qe_ep
*ep
, unsigned char stopatnext
)
735 struct qe_bd __iomem
*bd
, *nextbd
;
736 unsigned char stop
= 0;
740 bdstatus
= in_be32((u32 __iomem
*)bd
);
742 while (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
) && !stop
) {
743 bdstatus
= R_E
| R_I
| (bdstatus
& R_W
);
744 out_be32((u32 __iomem
*)bd
, bdstatus
);
751 bdstatus
= in_be32((u32 __iomem
*)bd
);
752 if (stopatnext
&& (bd
== nextbd
))
759 static void ep_recycle_rxbds(struct qe_ep
*ep
)
761 struct qe_bd __iomem
*bd
= ep
->n_rxbd
;
763 u8 epnum
= ep
->epnum
;
764 struct qe_udc
*udc
= ep
->udc
;
766 bdstatus
= in_be32((u32 __iomem
*)bd
);
767 if (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
)) {
769 ((in_be16(&udc
->ep_param
[epnum
]->rbptr
) -
770 in_be16(&udc
->ep_param
[epnum
]->rbase
))
772 bdstatus
= in_be32((u32 __iomem
*)bd
);
780 recycle_rxbds(ep
, 0);
781 ep
->e_rxbd
= ep
->n_rxbd
;
783 recycle_rxbds(ep
, 1);
785 if (in_be16(&udc
->usb_regs
->usb_usber
) & USB_E_BSY_MASK
)
786 out_be16(&udc
->usb_regs
->usb_usber
, USB_E_BSY_MASK
);
788 if (ep
->has_data
<= 0 && (!list_empty(&ep
->queue
)))
794 static void setup_received_handle(struct qe_udc
*udc
,
795 struct usb_ctrlrequest
*setup
);
796 static int qe_ep_rxframe_handle(struct qe_ep
*ep
);
797 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
);
798 /* when BD PID is setup, handle the packet */
799 static int ep0_setup_handle(struct qe_udc
*udc
)
801 struct qe_ep
*ep
= &udc
->eps
[0];
802 struct qe_frame
*pframe
;
806 pframe
= ep
->rxframe
;
807 if ((frame_get_info(pframe
) & PID_SETUP
)
808 && (udc
->ep0_state
== WAIT_FOR_SETUP
)) {
809 fsize
= frame_get_length(pframe
);
810 if (unlikely(fsize
!= 8))
812 cp
= (u8
*)&udc
->local_setup_buff
;
813 memcpy(cp
, pframe
->data
, fsize
);
816 /* handle the usb command base on the usb_ctrlrequest */
817 setup_received_handle(udc
, &udc
->local_setup_buff
);
823 static int qe_ep0_rx(struct qe_udc
*udc
)
825 struct qe_ep
*ep
= &udc
->eps
[0];
826 struct qe_frame
*pframe
;
827 struct qe_bd __iomem
*bd
;
828 u32 bdstatus
, length
;
831 pframe
= ep
->rxframe
;
833 if (ep
->dir
== USB_DIR_IN
) {
834 dev_err(udc
->dev
, "ep0 not a control endpoint\n");
839 bdstatus
= in_be32((u32 __iomem
*)bd
);
840 length
= bdstatus
& BD_LENGTH_MASK
;
842 while (!(bdstatus
& R_E
) && length
) {
843 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
844 && !(bdstatus
& R_ERROR
)) {
845 if (length
== USB_CRC_SIZE
) {
846 udc
->ep0_state
= WAIT_FOR_SETUP
;
848 "receive a ZLP in status phase\n");
850 qe_frame_clean(pframe
);
851 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
852 frame_set_data(pframe
, (u8
*)vaddr
);
853 frame_set_length(pframe
,
854 (length
- USB_CRC_SIZE
));
855 frame_set_status(pframe
, FRAME_OK
);
856 switch (bdstatus
& R_PID
) {
858 frame_set_info(pframe
, PID_SETUP
);
861 frame_set_info(pframe
, PID_DATA1
);
864 frame_set_info(pframe
, PID_DATA0
);
868 if ((bdstatus
& R_PID
) == R_PID_SETUP
)
869 ep0_setup_handle(udc
);
871 qe_ep_rxframe_handle(ep
);
874 dev_err(udc
->dev
, "The receive frame with error!\n");
877 /* note: don't clear the rxbd's buffer address */
878 recycle_one_rxbd(ep
);
886 bdstatus
= in_be32((u32 __iomem
*)bd
);
887 length
= bdstatus
& BD_LENGTH_MASK
;
896 static int qe_ep_rxframe_handle(struct qe_ep
*ep
)
898 struct qe_frame
*pframe
;
904 pframe
= ep
->rxframe
;
906 if (frame_get_info(pframe
) & PID_DATA1
)
909 if (framepid
!= ep
->data01
) {
910 dev_err(ep
->udc
->dev
, "the data01 error!\n");
914 fsize
= frame_get_length(pframe
);
915 if (list_empty(&ep
->queue
)) {
916 dev_err(ep
->udc
->dev
, "the %s have no requeue!\n", ep
->name
);
918 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
920 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
922 memcpy(cp
, pframe
->data
, fsize
);
923 req
->req
.actual
+= fsize
;
924 if ((fsize
< ep
->ep
.maxpacket
) ||
925 (req
->req
.actual
>= req
->req
.length
)) {
927 ep0_req_complete(ep
->udc
, req
);
930 if (list_empty(&ep
->queue
) && ep
->epnum
!= 0)
936 qe_ep_toggledata01(ep
);
941 static void ep_rx_tasklet(unsigned long data
)
943 struct qe_udc
*udc
= (struct qe_udc
*)data
;
945 struct qe_frame
*pframe
;
946 struct qe_bd __iomem
*bd
;
948 u32 bdstatus
, length
;
951 spin_lock_irqsave(&udc
->lock
, flags
);
953 for (i
= 1; i
< USB_MAX_ENDPOINTS
; i
++) {
956 if (ep
->dir
== USB_DIR_IN
|| ep
->enable_tasklet
== 0) {
958 "This is a transmit ep or disable tasklet!\n");
962 pframe
= ep
->rxframe
;
964 bdstatus
= in_be32((u32 __iomem
*)bd
);
965 length
= bdstatus
& BD_LENGTH_MASK
;
967 while (!(bdstatus
& R_E
) && length
) {
968 if (list_empty(&ep
->queue
)) {
971 "The rxep have noreq %d\n",
976 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
977 && !(bdstatus
& R_ERROR
)) {
978 qe_frame_clean(pframe
);
979 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
980 frame_set_data(pframe
, (u8
*)vaddr
);
981 frame_set_length(pframe
,
982 (length
- USB_CRC_SIZE
));
983 frame_set_status(pframe
, FRAME_OK
);
984 switch (bdstatus
& R_PID
) {
986 frame_set_info(pframe
, PID_DATA1
);
989 frame_set_info(pframe
, PID_SETUP
);
992 frame_set_info(pframe
, PID_DATA0
);
995 /* handle the rx frame */
996 qe_ep_rxframe_handle(ep
);
999 "error in received frame\n");
1001 /* note: don't clear the rxbd's buffer address */
1002 /*clear the length */
1003 out_be32((u32 __iomem
*)bd
, bdstatus
& BD_STATUS_MASK
);
1005 if (!(ep
->localnack
))
1006 recycle_one_rxbd(ep
);
1014 bdstatus
= in_be32((u32 __iomem
*)bd
);
1015 length
= bdstatus
& BD_LENGTH_MASK
;
1021 ep_recycle_rxbds(ep
);
1023 ep
->enable_tasklet
= 0;
1026 spin_unlock_irqrestore(&udc
->lock
, flags
);
1029 static int qe_ep_rx(struct qe_ep
*ep
)
1032 struct qe_frame
*pframe
;
1033 struct qe_bd __iomem
*bd
;
1034 u16 swoffs
, ucoffs
, emptybds
;
1037 pframe
= ep
->rxframe
;
1039 if (ep
->dir
== USB_DIR_IN
) {
1040 dev_err(udc
->dev
, "transmit ep in rx function\n");
1046 swoffs
= (u16
)(bd
- ep
->rxbase
);
1047 ucoffs
= (u16
)((in_be16(&udc
->ep_param
[ep
->epnum
]->rbptr
) -
1048 in_be16(&udc
->ep_param
[ep
->epnum
]->rbase
)) >> 3);
1049 if (swoffs
< ucoffs
)
1050 emptybds
= USB_BDRING_LEN_RX
- ucoffs
+ swoffs
;
1052 emptybds
= swoffs
- ucoffs
;
1054 if (emptybds
< MIN_EMPTY_BDS
) {
1057 dev_vdbg(udc
->dev
, "%d empty bds, send NACK\n", emptybds
);
1059 ep
->has_data
= USB_BDRING_LEN_RX
- emptybds
;
1061 if (list_empty(&ep
->queue
)) {
1063 dev_vdbg(udc
->dev
, "The rxep have no req queued with %d BDs\n",
1068 tasklet_schedule(&udc
->rx_tasklet
);
1069 ep
->enable_tasklet
= 1;
1074 /* send data from a frame, no matter what tx_req */
1075 static int qe_ep_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1077 struct qe_udc
*udc
= ep
->udc
;
1078 struct qe_bd __iomem
*bd
;
1080 u32 bdstatus
, pidmask
;
1083 if (ep
->dir
== USB_DIR_OUT
) {
1084 dev_err(udc
->dev
, "receive ep passed to tx function\n");
1088 /* Disable the Tx interrupt */
1089 saveusbmr
= in_be16(&udc
->usb_regs
->usb_usbmr
);
1090 out_be16(&udc
->usb_regs
->usb_usbmr
,
1091 saveusbmr
& ~(USB_E_TXB_MASK
| USB_E_TXE_MASK
));
1094 bdstatus
= in_be32((u32 __iomem
*)bd
);
1096 if (!(bdstatus
& (T_R
| BD_LENGTH_MASK
))) {
1097 if (frame_get_length(frame
) == 0) {
1098 frame_set_data(frame
, udc
->nullbuf
);
1099 frame_set_length(frame
, 2);
1100 frame
->info
|= (ZLP
| NO_CRC
);
1101 dev_vdbg(udc
->dev
, "the frame size = 0\n");
1103 paddr
= virt_to_phys((void *)frame
->data
);
1104 out_be32(&bd
->buf
, paddr
);
1105 bdstatus
= (bdstatus
&T_W
);
1106 if (!(frame_get_info(frame
) & NO_CRC
))
1107 bdstatus
|= T_R
| T_I
| T_L
| T_TC
1108 | frame_get_length(frame
);
1110 bdstatus
|= T_R
| T_I
| T_L
| frame_get_length(frame
);
1112 /* if the packet is a ZLP in status phase */
1113 if ((ep
->epnum
== 0) && (udc
->ep0_state
== DATA_STATE_NEED_ZLP
))
1117 pidmask
= T_PID_DATA1
;
1118 frame
->info
|= PID_DATA1
;
1120 pidmask
= T_PID_DATA0
;
1121 frame
->info
|= PID_DATA0
;
1124 bdstatus
|= pidmask
;
1125 out_be32((u32 __iomem
*)bd
, bdstatus
);
1126 qe_ep_filltxfifo(ep
);
1128 /* enable the TX interrupt */
1129 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1131 qe_ep_toggledata01(ep
);
1133 ep
->n_txbd
= ep
->txbase
;
1139 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1140 dev_vdbg(udc
->dev
, "The tx bd is not ready!\n");
1145 /* when a bd was transmitted, the function can
1146 * handle the tx_req, not include ep0 */
1147 static int txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1149 if (ep
->tx_req
!= NULL
) {
1151 int asent
= ep
->last
;
1158 /* a request already were transmitted completely */
1159 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1160 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1161 done(ep
, ep
->tx_req
, 0);
1168 /* we should gain a new tx_req fot this endpoint */
1169 if (ep
->tx_req
== NULL
) {
1170 if (!list_empty(&ep
->queue
)) {
1171 ep
->tx_req
= list_entry(ep
->queue
.next
, struct qe_req
,
1181 /* give a frame and a tx_req, send some data */
1182 static int qe_usb_senddata(struct qe_ep
*ep
, struct qe_frame
*frame
)
1187 qe_frame_clean(frame
);
1188 size
= min_t(u32
, (ep
->tx_req
->req
.length
- ep
->sent
),
1190 buf
= (u8
*)ep
->tx_req
->req
.buf
+ ep
->sent
;
1193 frame_set_data(frame
, buf
);
1194 frame_set_length(frame
, size
);
1195 frame_set_status(frame
, FRAME_OK
);
1196 frame_set_info(frame
, 0);
1197 return qe_ep_tx(ep
, frame
);
1202 /* give a frame struct,send a ZLP */
1203 static int sendnulldata(struct qe_ep
*ep
, struct qe_frame
*frame
, uint infor
)
1205 struct qe_udc
*udc
= ep
->udc
;
1210 qe_frame_clean(frame
);
1211 frame_set_data(frame
, (u8
*)udc
->nullbuf
);
1212 frame_set_length(frame
, 2);
1213 frame_set_status(frame
, FRAME_OK
);
1214 frame_set_info(frame
, (ZLP
| NO_CRC
| infor
));
1216 return qe_ep_tx(ep
, frame
);
1219 static int frame_create_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1221 struct qe_req
*req
= ep
->tx_req
;
1227 if ((req
->req
.length
- ep
->sent
) > 0)
1228 reval
= qe_usb_senddata(ep
, frame
);
1230 reval
= sendnulldata(ep
, frame
, 0);
1235 /* if direction is DIR_IN, the status is Device->Host
1236 * if direction is DIR_OUT, the status transaction is Device<-Host
1237 * in status phase, udc create a request and gain status */
1238 static int ep0_prime_status(struct qe_udc
*udc
, int direction
)
1241 struct qe_ep
*ep
= &udc
->eps
[0];
1243 if (direction
== USB_DIR_IN
) {
1244 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
1245 udc
->ep0_dir
= USB_DIR_IN
;
1246 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1248 udc
->ep0_dir
= USB_DIR_OUT
;
1249 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1255 /* a request complete in ep0, whether gadget request or udc request */
1256 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
)
1258 struct qe_ep
*ep
= &udc
->eps
[0];
1259 /* because usb and ep's status already been set in ch9setaddress() */
1261 switch (udc
->ep0_state
) {
1262 case DATA_STATE_XMIT
:
1264 /* receive status phase */
1265 if (ep0_prime_status(udc
, USB_DIR_OUT
))
1269 case DATA_STATE_NEED_ZLP
:
1271 udc
->ep0_state
= WAIT_FOR_SETUP
;
1274 case DATA_STATE_RECV
:
1276 /* send status phase */
1277 if (ep0_prime_status(udc
, USB_DIR_IN
))
1281 case WAIT_FOR_OUT_STATUS
:
1283 udc
->ep0_state
= WAIT_FOR_SETUP
;
1286 case WAIT_FOR_SETUP
:
1287 dev_vdbg(udc
->dev
, "Unexpected interrupt\n");
1296 static int ep0_txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1298 struct qe_req
*tx_req
= NULL
;
1299 struct qe_frame
*frame
= ep
->txframe
;
1301 if ((frame_get_info(frame
) & (ZLP
| NO_REQ
)) == (ZLP
| NO_REQ
)) {
1303 ep
->udc
->ep0_state
= WAIT_FOR_SETUP
;
1305 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1309 tx_req
= ep
->tx_req
;
1310 if (tx_req
!= NULL
) {
1312 int asent
= ep
->last
;
1319 /* a request already were transmitted completely */
1320 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1321 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1322 ep0_req_complete(ep
->udc
, ep
->tx_req
);
1328 dev_vdbg(ep
->udc
->dev
, "the ep0_controller have no req\n");
1334 static int ep0_txframe_handle(struct qe_ep
*ep
)
1336 /* if have error, transmit again */
1337 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1338 qe_ep_flushtxfifo(ep
);
1339 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1340 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1345 ep0_txcomplete(ep
, 1);
1347 ep0_txcomplete(ep
, 0);
1349 frame_create_tx(ep
, ep
->txframe
);
1353 static int qe_ep0_txconf(struct qe_ep
*ep
)
1355 struct qe_bd __iomem
*bd
;
1356 struct qe_frame
*pframe
;
1360 bdstatus
= in_be32((u32 __iomem
*)bd
);
1361 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1362 pframe
= ep
->txframe
;
1364 /* clear and recycle the BD */
1365 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1366 out_be32(&bd
->buf
, 0);
1368 ep
->c_txbd
= ep
->txbase
;
1372 if (ep
->c_txbd
== ep
->n_txbd
) {
1373 if (bdstatus
& DEVICE_T_ERROR
) {
1374 frame_set_status(pframe
, FRAME_ERROR
);
1375 if (bdstatus
& T_TO
)
1376 pframe
->status
|= TX_ER_TIMEOUT
;
1377 if (bdstatus
& T_UN
)
1378 pframe
->status
|= TX_ER_UNDERUN
;
1380 ep0_txframe_handle(ep
);
1384 bdstatus
= in_be32((u32 __iomem
*)bd
);
1390 static int ep_txframe_handle(struct qe_ep
*ep
)
1392 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1393 qe_ep_flushtxfifo(ep
);
1394 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1395 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1404 frame_create_tx(ep
, ep
->txframe
); /* send the data */
1408 /* confirm the already trainsmited bd */
1409 static int qe_ep_txconf(struct qe_ep
*ep
)
1411 struct qe_bd __iomem
*bd
;
1412 struct qe_frame
*pframe
= NULL
;
1414 unsigned char breakonrxinterrupt
= 0;
1417 bdstatus
= in_be32((u32 __iomem
*)bd
);
1418 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1419 pframe
= ep
->txframe
;
1420 if (bdstatus
& DEVICE_T_ERROR
) {
1421 frame_set_status(pframe
, FRAME_ERROR
);
1422 if (bdstatus
& T_TO
)
1423 pframe
->status
|= TX_ER_TIMEOUT
;
1424 if (bdstatus
& T_UN
)
1425 pframe
->status
|= TX_ER_UNDERUN
;
1428 /* clear and recycle the BD */
1429 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1430 out_be32(&bd
->buf
, 0);
1432 ep
->c_txbd
= ep
->txbase
;
1436 /* handle the tx frame */
1437 ep_txframe_handle(ep
);
1439 bdstatus
= in_be32((u32 __iomem
*)bd
);
1441 if (breakonrxinterrupt
)
1447 /* Add a request in queue, and try to transmit a packet */
1448 static int ep_req_send(struct qe_ep
*ep
, struct qe_req
*req
)
1452 if (ep
->tx_req
== NULL
) {
1455 txcomplete(ep
, 0); /* can gain a new tx_req */
1456 reval
= frame_create_tx(ep
, ep
->txframe
);
1461 /* Maybe this is a good ideal */
1462 static int ep_req_rx(struct qe_ep
*ep
, struct qe_req
*req
)
1464 struct qe_udc
*udc
= ep
->udc
;
1465 struct qe_frame
*pframe
= NULL
;
1466 struct qe_bd __iomem
*bd
;
1467 u32 bdstatus
, length
;
1473 if (list_empty(&ep
->queue
)) {
1474 dev_vdbg(udc
->dev
, "the req already finish!\n");
1477 pframe
= ep
->rxframe
;
1480 bdstatus
= in_be32((u32 __iomem
*)bd
);
1481 length
= bdstatus
& BD_LENGTH_MASK
;
1483 while (!(bdstatus
& R_E
) && length
) {
1486 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
1487 && !(bdstatus
& R_ERROR
)) {
1488 qe_frame_clean(pframe
);
1489 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
1490 frame_set_data(pframe
, (u8
*)vaddr
);
1491 frame_set_length(pframe
, (length
- USB_CRC_SIZE
));
1492 frame_set_status(pframe
, FRAME_OK
);
1493 switch (bdstatus
& R_PID
) {
1495 frame_set_info(pframe
, PID_DATA1
); break;
1497 frame_set_info(pframe
, PID_DATA0
); break;
1499 /* handle the rx frame */
1501 if (frame_get_info(pframe
) & PID_DATA1
)
1506 if (framepid
!= ep
->data01
) {
1507 dev_vdbg(udc
->dev
, "the data01 error!\n");
1509 fsize
= frame_get_length(pframe
);
1511 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
1513 memcpy(cp
, pframe
->data
, fsize
);
1514 req
->req
.actual
+= fsize
;
1515 if ((fsize
< ep
->ep
.maxpacket
)
1516 || (req
->req
.actual
>=
1520 if (list_empty(&ep
->queue
))
1524 qe_ep_toggledata01(ep
);
1527 dev_err(udc
->dev
, "The receive frame with error!\n");
1530 /* note: don't clear the rxbd's buffer address *
1531 * only Clear the length */
1532 out_be32((u32 __iomem
*)bd
, (bdstatus
& BD_STATUS_MASK
));
1541 bdstatus
= in_be32((u32 __iomem
*)bd
);
1542 length
= bdstatus
& BD_LENGTH_MASK
;
1546 ep_recycle_rxbds(ep
);
1551 /* only add the request in queue */
1552 static int ep_req_receive(struct qe_ep
*ep
, struct qe_req
*req
)
1554 if (ep
->state
== EP_STATE_NACK
) {
1555 if (ep
->has_data
<= 0) {
1556 /* Enable rx and unmask rx interrupt */
1559 /* Copy the exist BD data */
1567 /********************************************************************
1568 Internal Used Function End
1569 ********************************************************************/
1571 /*-----------------------------------------------------------------------
1572 Endpoint Management Functions For Gadget
1573 -----------------------------------------------------------------------*/
1574 static int qe_ep_enable(struct usb_ep
*_ep
,
1575 const struct usb_endpoint_descriptor
*desc
)
1580 unsigned char epnum
;
1582 ep
= container_of(_ep
, struct qe_ep
, ep
);
1584 /* catch various bogus parameters */
1585 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep_name
[0] ||
1586 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
))
1590 if (!udc
->driver
|| (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
))
1593 epnum
= (u8
)desc
->bEndpointAddress
& 0xF;
1595 retval
= qe_ep_init(udc
, epnum
, desc
);
1597 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1598 dev_dbg(udc
->dev
, "enable ep%d failed\n", ep
->epnum
);
1601 dev_dbg(udc
->dev
, "enable ep%d successful\n", ep
->epnum
);
1605 static int qe_ep_disable(struct usb_ep
*_ep
)
1609 unsigned long flags
;
1612 ep
= container_of(_ep
, struct qe_ep
, ep
);
1615 if (!_ep
|| !ep
->desc
) {
1616 dev_dbg(udc
->dev
, "%s not enabled\n", _ep
? ep
->ep
.name
: NULL
);
1620 spin_lock_irqsave(&udc
->lock
, flags
);
1621 /* Nuke all pending requests (does flush) */
1622 nuke(ep
, -ESHUTDOWN
);
1625 spin_unlock_irqrestore(&udc
->lock
, flags
);
1627 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1629 if (ep
->dir
== USB_DIR_OUT
)
1630 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1631 (USB_BDRING_LEN_RX
+ 1);
1633 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1634 (USB_BDRING_LEN
+ 1);
1636 if (ep
->dir
!= USB_DIR_IN
) {
1639 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
1642 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
1644 dma_sync_single_for_cpu(
1645 udc_controller
->gadget
.dev
.parent
,
1649 kfree(ep
->rxbuffer
);
1652 if (ep
->dir
!= USB_DIR_OUT
)
1655 dev_dbg(udc
->dev
, "disabled %s OK\n", _ep
->name
);
1659 static struct usb_request
*qe_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
1663 req
= kzalloc(sizeof(*req
), gfp_flags
);
1667 req
->req
.dma
= DMA_ADDR_INVALID
;
1669 INIT_LIST_HEAD(&req
->queue
);
1674 static void qe_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1678 req
= container_of(_req
, struct qe_req
, req
);
1684 /* queues (submits) an I/O request to an endpoint */
1685 static int qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1688 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1689 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1691 unsigned long flags
;
1695 /* catch various bogus parameters */
1696 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
1697 || !list_empty(&req
->queue
)) {
1698 dev_dbg(udc
->dev
, "bad params\n");
1701 if (!_ep
|| (!ep
->desc
&& ep_index(ep
))) {
1702 dev_dbg(udc
->dev
, "bad ep\n");
1706 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1711 /* map virtual address to hardware */
1712 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
1713 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
1721 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
1722 req
->req
.dma
, req
->req
.length
,
1729 req
->req
.status
= -EINPROGRESS
;
1730 req
->req
.actual
= 0;
1732 list_add_tail(&req
->queue
, &ep
->queue
);
1733 dev_vdbg(udc
->dev
, "gadget have request in %s! %d\n",
1734 ep
->name
, req
->req
.length
);
1735 spin_lock_irqsave(&udc
->lock
, flags
);
1736 /* push the request to device */
1738 reval
= ep_req_send(ep
, req
);
1741 if (ep_index(ep
) == 0 && req
->req
.length
> 0) {
1743 udc
->ep0_state
= DATA_STATE_XMIT
;
1745 udc
->ep0_state
= DATA_STATE_RECV
;
1748 if (ep
->dir
== USB_DIR_OUT
)
1749 reval
= ep_req_receive(ep
, req
);
1751 spin_unlock_irqrestore(&udc
->lock
, flags
);
1756 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1757 static int qe_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1759 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1761 unsigned long flags
;
1766 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1768 /* make sure it's actually queued on this endpoint */
1769 list_for_each_entry(req
, &ep
->queue
, queue
) {
1770 if (&req
->req
== _req
)
1774 if (&req
->req
!= _req
) {
1775 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1779 done(ep
, req
, -ECONNRESET
);
1781 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1785 /*-----------------------------------------------------------------
1786 * modify the endpoint halt feature
1787 * @ep: the non-isochronous endpoint being stalled
1788 * @value: 1--set halt 0--clear halt
1789 * Returns zero, or a negative error code.
1790 *----------------------------------------------------------------*/
1791 static int qe_ep_set_halt(struct usb_ep
*_ep
, int value
)
1794 unsigned long flags
;
1795 int status
= -EOPNOTSUPP
;
1798 ep
= container_of(_ep
, struct qe_ep
, ep
);
1799 if (!_ep
|| !ep
->desc
) {
1805 /* Attempt to halt IN ep will fail if any transfer requests
1806 * are still queue */
1807 if (value
&& ep_is_in(ep
) && !list_empty(&ep
->queue
)) {
1813 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1814 qe_eptx_stall_change(ep
, value
);
1815 qe_eprx_stall_change(ep
, value
);
1816 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1818 if (ep
->epnum
== 0) {
1819 udc
->ep0_state
= WAIT_FOR_SETUP
;
1823 /* set data toggle to DATA0 on clear halt */
1827 dev_vdbg(udc
->dev
, "%s %s halt stat %d\n", ep
->ep
.name
,
1828 value
? "set" : "clear", status
);
1833 static struct usb_ep_ops qe_ep_ops
= {
1834 .enable
= qe_ep_enable
,
1835 .disable
= qe_ep_disable
,
1837 .alloc_request
= qe_alloc_request
,
1838 .free_request
= qe_free_request
,
1840 .queue
= qe_ep_queue
,
1841 .dequeue
= qe_ep_dequeue
,
1843 .set_halt
= qe_ep_set_halt
,
1846 /*------------------------------------------------------------------------
1847 Gadget Driver Layer Operations
1848 ------------------------------------------------------------------------*/
1850 /* Get the current frame number */
1851 static int qe_get_frame(struct usb_gadget
*gadget
)
1855 tmp
= in_be16(&udc_controller
->usb_param
->frame_n
);
1864 /* Tries to wake up the host connected to this gadget
1866 * Return : 0-success
1867 * Negative-this feature not enabled by host or not supported by device hw
1869 static int qe_wakeup(struct usb_gadget
*gadget
)
1874 /* Notify controller that VBUS is powered, Called by whatever
1875 detects VBUS sessions */
1876 static int qe_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1881 /* constrain controller's VBUS power usage
1882 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1883 * reporting how much power the device may consume. For example, this
1884 * could affect how quickly batteries are recharged.
1886 * Returns zero on success, else negative errno.
1888 static int qe_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1893 /* Change Data+ pullup status
1894 * this func is used by usb_gadget_connect/disconnect
1896 static int qe_pullup(struct usb_gadget
*gadget
, int is_on
)
1901 /* defined in usb_gadget.h */
1902 static struct usb_gadget_ops qe_gadget_ops
= {
1903 .get_frame
= qe_get_frame
,
1904 .wakeup
= qe_wakeup
,
1905 /* .set_selfpowered = qe_set_selfpowered,*/ /* always selfpowered */
1906 .vbus_session
= qe_vbus_session
,
1907 .vbus_draw
= qe_vbus_draw
,
1908 .pullup
= qe_pullup
,
1911 /*-------------------------------------------------------------------------
1912 USB ep0 Setup process in BUS Enumeration
1913 -------------------------------------------------------------------------*/
1914 static int udc_reset_ep_queue(struct qe_udc
*udc
, u8 pipe
)
1916 struct qe_ep
*ep
= &udc
->eps
[pipe
];
1918 nuke(ep
, -ECONNRESET
);
1923 static int reset_queues(struct qe_udc
*udc
)
1927 for (pipe
= 0; pipe
< USB_MAX_ENDPOINTS
; pipe
++)
1928 udc_reset_ep_queue(udc
, pipe
);
1930 /* report disconnect; the driver is already quiesced */
1931 spin_unlock(&udc
->lock
);
1932 udc
->driver
->disconnect(&udc
->gadget
);
1933 spin_lock(&udc
->lock
);
1938 static void ch9setaddress(struct qe_udc
*udc
, u16 value
, u16 index
,
1941 /* Save the new address to device struct */
1942 udc
->device_address
= (u8
) value
;
1943 /* Update usb state */
1944 udc
->usb_state
= USB_STATE_ADDRESS
;
1946 /* Status phase , send a ZLP */
1947 if (ep0_prime_status(udc
, USB_DIR_IN
))
1951 static void ownercomplete(struct usb_ep
*_ep
, struct usb_request
*_req
)
1953 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1955 req
->req
.buf
= NULL
;
1959 static void ch9getstatus(struct qe_udc
*udc
, u8 request_type
, u16 value
,
1960 u16 index
, u16 length
)
1968 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1969 /* Get device status */
1970 usb_status
= 1 << USB_DEVICE_SELF_POWERED
;
1971 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
1972 /* Get interface status */
1973 /* We don't have interface information in udc driver */
1975 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
1976 /* Get endpoint status */
1977 int pipe
= index
& USB_ENDPOINT_NUMBER_MASK
;
1978 struct qe_ep
*target_ep
= &udc
->eps
[pipe
];
1981 /* stall if endpoint doesn't exist */
1982 if (!target_ep
->desc
)
1985 usep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe
]);
1986 if (index
& USB_DIR_IN
) {
1987 if (target_ep
->dir
!= USB_DIR_IN
)
1989 if ((usep
& USB_THS_MASK
) == USB_THS_STALL
)
1990 usb_status
= 1 << USB_ENDPOINT_HALT
;
1992 if (target_ep
->dir
!= USB_DIR_OUT
)
1994 if ((usep
& USB_RHS_MASK
) == USB_RHS_STALL
)
1995 usb_status
= 1 << USB_ENDPOINT_HALT
;
1999 req
= container_of(qe_alloc_request(&ep
->ep
, GFP_KERNEL
),
2000 struct qe_req
, req
);
2001 req
->req
.length
= 2;
2002 req
->req
.buf
= udc
->statusbuf
;
2003 *(u16
*)req
->req
.buf
= cpu_to_le16(usb_status
);
2004 req
->req
.status
= -EINPROGRESS
;
2005 req
->req
.actual
= 0;
2006 req
->req
.complete
= ownercomplete
;
2008 udc
->ep0_dir
= USB_DIR_IN
;
2011 status
= qe_ep_queue(&ep
->ep
, &req
->req
, GFP_ATOMIC
);
2016 dev_err(udc
->dev
, "Can't respond to getstatus request \n");
2020 /* only handle the setup request, suppose the device in normal status */
2021 static void setup_received_handle(struct qe_udc
*udc
,
2022 struct usb_ctrlrequest
*setup
)
2024 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2025 u16 wValue
= le16_to_cpu(setup
->wValue
);
2026 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
2027 u16 wLength
= le16_to_cpu(setup
->wLength
);
2029 /* clear the previous request in the ep0 */
2030 udc_reset_ep_queue(udc
, 0);
2032 if (setup
->bRequestType
& USB_DIR_IN
)
2033 udc
->ep0_dir
= USB_DIR_IN
;
2035 udc
->ep0_dir
= USB_DIR_OUT
;
2037 switch (setup
->bRequest
) {
2038 case USB_REQ_GET_STATUS
:
2039 /* Data+Status phase form udc */
2040 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
2041 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
2043 ch9getstatus(udc
, setup
->bRequestType
, wValue
, wIndex
,
2047 case USB_REQ_SET_ADDRESS
:
2048 /* Status phase from udc */
2049 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
2052 ch9setaddress(udc
, wValue
, wIndex
, wLength
);
2055 case USB_REQ_CLEAR_FEATURE
:
2056 case USB_REQ_SET_FEATURE
:
2057 /* Requests with no data phase, status phase from udc */
2058 if ((setup
->bRequestType
& USB_TYPE_MASK
)
2059 != USB_TYPE_STANDARD
)
2062 if ((setup
->bRequestType
& USB_RECIP_MASK
)
2063 == USB_RECIP_ENDPOINT
) {
2064 int pipe
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2067 if (wValue
!= 0 || wLength
!= 0
2068 || pipe
> USB_MAX_ENDPOINTS
)
2070 ep
= &udc
->eps
[pipe
];
2072 spin_unlock(&udc
->lock
);
2073 qe_ep_set_halt(&ep
->ep
,
2074 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2076 spin_lock(&udc
->lock
);
2079 ep0_prime_status(udc
, USB_DIR_IN
);
2088 /* Data phase from gadget, status phase from udc */
2089 if (setup
->bRequestType
& USB_DIR_IN
) {
2090 udc
->ep0_state
= DATA_STATE_XMIT
;
2091 udc
->ep0_dir
= USB_DIR_IN
;
2093 udc
->ep0_state
= DATA_STATE_RECV
;
2094 udc
->ep0_dir
= USB_DIR_OUT
;
2096 spin_unlock(&udc
->lock
);
2097 if (udc
->driver
->setup(&udc
->gadget
,
2098 &udc
->local_setup_buff
) < 0)
2100 spin_lock(&udc
->lock
);
2102 /* No data phase, IN status from gadget */
2103 udc
->ep0_dir
= USB_DIR_IN
;
2104 spin_unlock(&udc
->lock
);
2105 if (udc
->driver
->setup(&udc
->gadget
,
2106 &udc
->local_setup_buff
) < 0)
2108 spin_lock(&udc
->lock
);
2109 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
2113 /*-------------------------------------------------------------------------
2114 USB Interrupt handlers
2115 -------------------------------------------------------------------------*/
2116 static void suspend_irq(struct qe_udc
*udc
)
2118 udc
->resume_state
= udc
->usb_state
;
2119 udc
->usb_state
= USB_STATE_SUSPENDED
;
2121 /* report suspend to the driver ,serial.c not support this*/
2122 if (udc
->driver
->suspend
)
2123 udc
->driver
->suspend(&udc
->gadget
);
2126 static void resume_irq(struct qe_udc
*udc
)
2128 udc
->usb_state
= udc
->resume_state
;
2129 udc
->resume_state
= 0;
2131 /* report resume to the driver , serial.c not support this*/
2132 if (udc
->driver
->resume
)
2133 udc
->driver
->resume(&udc
->gadget
);
2136 static void idle_irq(struct qe_udc
*udc
)
2140 usbs
= in_8(&udc
->usb_regs
->usb_usbs
);
2141 if (usbs
& USB_IDLE_STATUS_MASK
) {
2142 if ((udc
->usb_state
) != USB_STATE_SUSPENDED
)
2145 if (udc
->usb_state
== USB_STATE_SUSPENDED
)
2150 static int reset_irq(struct qe_udc
*udc
)
2155 out_8(&udc
->usb_regs
->usb_usadr
, 0);
2157 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2158 if (udc
->eps
[i
].init
)
2159 qe_ep_reset(udc
, i
);
2163 udc
->usb_state
= USB_STATE_DEFAULT
;
2164 udc
->ep0_state
= WAIT_FOR_SETUP
;
2165 udc
->ep0_dir
= USB_DIR_OUT
;
2170 static int bsy_irq(struct qe_udc
*udc
)
2175 static int txe_irq(struct qe_udc
*udc
)
2180 /* ep0 tx interrupt also in here */
2181 static int tx_irq(struct qe_udc
*udc
)
2184 struct qe_bd __iomem
*bd
;
2187 if ((udc
->usb_state
== USB_STATE_ADDRESS
)
2188 && (in_8(&udc
->usb_regs
->usb_usadr
) == 0))
2189 out_8(&udc
->usb_regs
->usb_usadr
, udc
->device_address
);
2191 for (i
= (USB_MAX_ENDPOINTS
-1); ((i
>= 0) && (res
== 0)); i
--) {
2193 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_OUT
)) {
2195 if (!(in_be32((u32 __iomem
*)bd
) & T_R
)
2196 && (in_be32(&bd
->buf
))) {
2197 /* confirm the transmitted bd */
2199 res
= qe_ep0_txconf(ep
);
2201 res
= qe_ep_txconf(ep
);
2209 /* setup packect's rx is handle in the function too */
2210 static void rx_irq(struct qe_udc
*udc
)
2213 struct qe_bd __iomem
*bd
;
2216 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2218 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_IN
)) {
2220 if (!(in_be32((u32 __iomem
*)bd
) & R_E
)
2221 && (in_be32(&bd
->buf
))) {
2222 if (ep
->epnum
== 0) {
2225 /*non-setup package receive*/
2233 static irqreturn_t
qe_udc_irq(int irq
, void *_udc
)
2235 struct qe_udc
*udc
= (struct qe_udc
*)_udc
;
2237 irqreturn_t status
= IRQ_NONE
;
2238 unsigned long flags
;
2240 spin_lock_irqsave(&udc
->lock
, flags
);
2242 irq_src
= in_be16(&udc
->usb_regs
->usb_usber
) &
2243 in_be16(&udc
->usb_regs
->usb_usbmr
);
2244 /* Clear notification bits */
2245 out_be16(&udc
->usb_regs
->usb_usber
, irq_src
);
2247 if (irq_src
& USB_E_IDLE_MASK
) {
2249 irq_src
&= ~USB_E_IDLE_MASK
;
2250 status
= IRQ_HANDLED
;
2253 if (irq_src
& USB_E_TXB_MASK
) {
2255 irq_src
&= ~USB_E_TXB_MASK
;
2256 status
= IRQ_HANDLED
;
2259 if (irq_src
& USB_E_RXB_MASK
) {
2261 irq_src
&= ~USB_E_RXB_MASK
;
2262 status
= IRQ_HANDLED
;
2265 if (irq_src
& USB_E_RESET_MASK
) {
2267 irq_src
&= ~USB_E_RESET_MASK
;
2268 status
= IRQ_HANDLED
;
2271 if (irq_src
& USB_E_BSY_MASK
) {
2273 irq_src
&= ~USB_E_BSY_MASK
;
2274 status
= IRQ_HANDLED
;
2277 if (irq_src
& USB_E_TXE_MASK
) {
2279 irq_src
&= ~USB_E_TXE_MASK
;
2280 status
= IRQ_HANDLED
;
2283 spin_unlock_irqrestore(&udc
->lock
, flags
);
2288 /*-------------------------------------------------------------------------
2289 Gadget driver register and unregister.
2290 --------------------------------------------------------------------------*/
2291 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
2294 unsigned long flags
= 0;
2296 /* standard operations */
2297 if (!udc_controller
)
2300 if (!driver
|| (driver
->speed
!= USB_SPEED_FULL
2301 && driver
->speed
!= USB_SPEED_HIGH
)
2302 || !driver
->bind
|| !driver
->disconnect
2306 if (udc_controller
->driver
)
2309 /* lock is needed but whether should use this lock or another */
2310 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2312 driver
->driver
.bus
= NULL
;
2313 /* hook up the driver */
2314 udc_controller
->driver
= driver
;
2315 udc_controller
->gadget
.dev
.driver
= &driver
->driver
;
2316 udc_controller
->gadget
.speed
= (enum usb_device_speed
)(driver
->speed
);
2317 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2319 retval
= driver
->bind(&udc_controller
->gadget
);
2321 dev_err(udc_controller
->dev
, "bind to %s --> %d",
2322 driver
->driver
.name
, retval
);
2323 udc_controller
->gadget
.dev
.driver
= NULL
;
2324 udc_controller
->driver
= NULL
;
2328 /* Enable IRQ reg and Set usbcmd reg EN bit */
2331 out_be16(&udc_controller
->usb_regs
->usb_usber
, 0xffff);
2332 out_be16(&udc_controller
->usb_regs
->usb_usbmr
, USB_E_DEFAULT_DEVICE
);
2333 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2334 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2335 udc_controller
->ep0_dir
= USB_DIR_OUT
;
2336 dev_info(udc_controller
->dev
, "%s bind to driver %s \n",
2337 udc_controller
->gadget
.name
, driver
->driver
.name
);
2340 EXPORT_SYMBOL(usb_gadget_register_driver
);
2342 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
2344 struct qe_ep
*loop_ep
;
2345 unsigned long flags
;
2347 if (!udc_controller
)
2350 if (!driver
|| driver
!= udc_controller
->driver
)
2353 /* stop usb controller, disable intr */
2356 /* in fact, no needed */
2357 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2358 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2359 udc_controller
->ep0_dir
= 0;
2361 /* stand operation */
2362 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2363 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2364 nuke(&udc_controller
->eps
[0], -ESHUTDOWN
);
2365 list_for_each_entry(loop_ep
, &udc_controller
->gadget
.ep_list
,
2367 nuke(loop_ep
, -ESHUTDOWN
);
2368 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2370 /* report disconnect; the controller is already quiesced */
2371 driver
->disconnect(&udc_controller
->gadget
);
2373 /* unbind gadget and unhook driver. */
2374 driver
->unbind(&udc_controller
->gadget
);
2375 udc_controller
->gadget
.dev
.driver
= NULL
;
2376 udc_controller
->driver
= NULL
;
2378 dev_info(udc_controller
->dev
, "unregistered gadget driver '%s'\r\n",
2379 driver
->driver
.name
);
2382 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
2384 /* udc structure's alloc and setup, include ep-param alloc */
2385 static struct qe_udc __devinit
*qe_udc_config(struct of_device
*ofdev
)
2388 struct device_node
*np
= ofdev
->node
;
2389 unsigned int tmp_addr
= 0;
2390 struct usb_device_para __iomem
*usbpram
;
2395 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
2397 dev_err(&ofdev
->dev
, "malloc udc failed\n");
2401 udc
->dev
= &ofdev
->dev
;
2403 /* get default address of usb parameter in MURAM from device tree */
2404 offset
= *of_get_address(np
, 1, &size
, NULL
);
2405 udc
->usb_param
= cpm_muram_addr(offset
);
2406 memset_io(udc
->usb_param
, 0, size
);
2408 usbpram
= udc
->usb_param
;
2409 out_be16(&usbpram
->frame_n
, 0);
2410 out_be32(&usbpram
->rstate
, 0);
2412 tmp_addr
= cpm_muram_alloc((USB_MAX_ENDPOINTS
*
2413 sizeof(struct usb_ep_para
)),
2414 USB_EP_PARA_ALIGNMENT
);
2415 if (IS_ERR_VALUE(tmp_addr
))
2418 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2419 out_be16(&usbpram
->epptr
[i
], (u16
)tmp_addr
);
2420 udc
->ep_param
[i
] = cpm_muram_addr(tmp_addr
);
2424 memset_io(udc
->ep_param
[0], 0,
2425 USB_MAX_ENDPOINTS
* sizeof(struct usb_ep_para
));
2427 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2428 udc
->usb_state
= USB_STATE_POWERED
;
2431 spin_lock_init(&udc
->lock
);
2439 /* USB Controller register init */
2440 static int __devinit
qe_udc_reg_init(struct qe_udc
*udc
)
2442 struct usb_ctlr __iomem
*qe_usbregs
;
2443 qe_usbregs
= udc
->usb_regs
;
2445 /* Init the usb register */
2446 out_8(&qe_usbregs
->usb_usmod
, 0x01);
2447 out_be16(&qe_usbregs
->usb_usbmr
, 0);
2448 out_8(&qe_usbregs
->usb_uscom
, 0);
2449 out_be16(&qe_usbregs
->usb_usber
, USBER_ALL_CLEAR
);
2454 static int __devinit
qe_ep_config(struct qe_udc
*udc
, unsigned char pipe_num
)
2456 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
2459 strcpy(ep
->name
, ep_name
[pipe_num
]);
2460 ep
->ep
.name
= ep_name
[pipe_num
];
2462 ep
->ep
.ops
= &qe_ep_ops
;
2464 ep
->ep
.maxpacket
= (unsigned short) ~0;
2467 ep
->epnum
= (u8
)pipe_num
;
2474 ep
->state
= EP_STATE_IDLE
;
2477 /* the queue lists any req for this ep */
2478 INIT_LIST_HEAD(&ep
->queue
);
2480 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2482 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2484 ep
->gadget
= &udc
->gadget
;
2489 /*-----------------------------------------------------------------------
2490 * UDC device Driver operation functions *
2491 *----------------------------------------------------------------------*/
2492 static void qe_udc_release(struct device
*dev
)
2496 complete(udc_controller
->done
);
2497 cpm_muram_free(cpm_muram_offset(udc_controller
->ep_param
[0]));
2498 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
2499 udc_controller
->ep_param
[i
] = NULL
;
2501 kfree(udc_controller
);
2502 udc_controller
= NULL
;
2505 /* Driver probe functions */
2506 static int __devinit
qe_udc_probe(struct of_device
*ofdev
,
2507 const struct of_device_id
*match
)
2509 struct device_node
*np
= ofdev
->node
;
2511 unsigned int ret
= 0;
2515 prop
= of_get_property(np
, "mode", NULL
);
2516 if (!prop
|| strcmp(prop
, "peripheral"))
2519 /* Initialize the udc structure including QH member and other member */
2520 udc_controller
= qe_udc_config(ofdev
);
2521 if (!udc_controller
) {
2522 dev_err(&ofdev
->dev
, "failed to initialize\n");
2526 udc_controller
->soc_type
= (unsigned long)match
->data
;
2527 udc_controller
->usb_regs
= of_iomap(np
, 0);
2528 if (!udc_controller
->usb_regs
) {
2533 /* initialize usb hw reg except for regs for EP,
2534 * leave usbintr reg untouched*/
2535 qe_udc_reg_init(udc_controller
);
2537 /* here comes the stand operations for probe
2538 * set the qe_udc->gadget.xxx */
2539 udc_controller
->gadget
.ops
= &qe_gadget_ops
;
2541 /* gadget.ep0 is a pointer */
2542 udc_controller
->gadget
.ep0
= &udc_controller
->eps
[0].ep
;
2544 INIT_LIST_HEAD(&udc_controller
->gadget
.ep_list
);
2546 /* modify in register gadget process */
2547 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2549 /* name: Identifies the controller hardware type. */
2550 udc_controller
->gadget
.name
= driver_name
;
2552 device_initialize(&udc_controller
->gadget
.dev
);
2554 dev_set_name(&udc_controller
->gadget
.dev
, "gadget");
2556 udc_controller
->gadget
.dev
.release
= qe_udc_release
;
2557 udc_controller
->gadget
.dev
.parent
= &ofdev
->dev
;
2559 /* initialize qe_ep struct */
2560 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2561 /* because the ep type isn't decide here so
2562 * qe_ep_init() should be called in ep_enable() */
2564 /* setup the qe_ep struct and link ep.ep.list
2565 * into gadget.ep_list */
2566 qe_ep_config(udc_controller
, (unsigned char)i
);
2569 /* ep0 initialization in here */
2570 ret
= qe_ep_init(udc_controller
, 0, &qe_ep0_desc
);
2574 /* create a buf for ZLP send, need to remain zeroed */
2575 udc_controller
->nullbuf
= kzalloc(256, GFP_KERNEL
);
2576 if (udc_controller
->nullbuf
== NULL
) {
2577 dev_err(udc_controller
->dev
, "cannot alloc nullbuf\n");
2582 /* buffer for data of get_status request */
2583 udc_controller
->statusbuf
= kzalloc(2, GFP_KERNEL
);
2584 if (udc_controller
->statusbuf
== NULL
) {
2589 udc_controller
->nullp
= virt_to_phys((void *)udc_controller
->nullbuf
);
2590 if (udc_controller
->nullp
== DMA_ADDR_INVALID
) {
2591 udc_controller
->nullp
= dma_map_single(
2592 udc_controller
->gadget
.dev
.parent
,
2593 udc_controller
->nullbuf
,
2596 udc_controller
->nullmap
= 1;
2598 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
2599 udc_controller
->nullp
, 256,
2603 tasklet_init(&udc_controller
->rx_tasklet
, ep_rx_tasklet
,
2604 (unsigned long)udc_controller
);
2605 /* request irq and disable DR */
2606 udc_controller
->usb_irq
= irq_of_parse_and_map(np
, 0);
2607 if (!udc_controller
->usb_irq
) {
2612 ret
= request_irq(udc_controller
->usb_irq
, qe_udc_irq
, 0,
2613 driver_name
, udc_controller
);
2615 dev_err(udc_controller
->dev
, "cannot request irq %d err %d \n",
2616 udc_controller
->usb_irq
, ret
);
2620 ret
= device_add(&udc_controller
->gadget
.dev
);
2624 dev_info(udc_controller
->dev
,
2625 "%s USB controller initialized as device\n",
2626 (udc_controller
->soc_type
== PORT_QE
) ? "QE" : "CPM");
2630 free_irq(udc_controller
->usb_irq
, udc_controller
);
2632 irq_dispose_mapping(udc_controller
->usb_irq
);
2634 if (udc_controller
->nullmap
) {
2635 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2636 udc_controller
->nullp
, 256,
2638 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2640 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2641 udc_controller
->nullp
, 256,
2644 kfree(udc_controller
->statusbuf
);
2646 kfree(udc_controller
->nullbuf
);
2648 ep
= &udc_controller
->eps
[0];
2649 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2651 kfree(ep
->rxbuffer
);
2654 iounmap(udc_controller
->usb_regs
);
2656 kfree(udc_controller
);
2657 udc_controller
= NULL
;
2662 static int qe_udc_suspend(struct of_device
*dev
, pm_message_t state
)
2667 static int qe_udc_resume(struct of_device
*dev
)
2673 static int __devexit
qe_udc_remove(struct of_device
*ofdev
)
2678 DECLARE_COMPLETION(done
);
2680 if (!udc_controller
)
2683 udc_controller
->done
= &done
;
2684 tasklet_disable(&udc_controller
->rx_tasklet
);
2686 if (udc_controller
->nullmap
) {
2687 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2688 udc_controller
->nullp
, 256,
2690 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2692 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2693 udc_controller
->nullp
, 256,
2696 kfree(udc_controller
->statusbuf
);
2697 kfree(udc_controller
->nullbuf
);
2699 ep
= &udc_controller
->eps
[0];
2700 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2701 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (USB_BDRING_LEN
+ 1);
2705 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2708 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
2710 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2715 kfree(ep
->rxbuffer
);
2718 free_irq(udc_controller
->usb_irq
, udc_controller
);
2719 irq_dispose_mapping(udc_controller
->usb_irq
);
2721 tasklet_kill(&udc_controller
->rx_tasklet
);
2723 iounmap(udc_controller
->usb_regs
);
2725 device_unregister(&udc_controller
->gadget
.dev
);
2726 /* wait for release() of gadget.dev to free udc */
2727 wait_for_completion(&done
);
2732 /*-------------------------------------------------------------------------*/
2733 static struct of_device_id __devinitdata qe_udc_match
[] = {
2735 .compatible
= "fsl,mpc8360-qe-usb",
2736 .data
= (void *)PORT_QE
,
2739 .compatible
= "fsl,mpc8272-cpm-usb",
2740 .data
= (void *)PORT_CPM
,
2745 MODULE_DEVICE_TABLE(of
, qe_udc_match
);
2747 static struct of_platform_driver udc_driver
= {
2748 .name
= (char *)driver_name
,
2749 .match_table
= qe_udc_match
,
2750 .probe
= qe_udc_probe
,
2751 .remove
= __devexit_p(qe_udc_remove
),
2753 .suspend
= qe_udc_suspend
,
2754 .resume
= qe_udc_resume
,
2758 static int __init
qe_udc_init(void)
2760 printk(KERN_INFO
"%s: %s, %s\n", driver_name
, driver_desc
,
2762 return of_register_platform_driver(&udc_driver
);
2765 static void __exit
qe_udc_exit(void)
2767 of_unregister_platform_driver(&udc_driver
);
2770 module_init(qe_udc_init
);
2771 module_exit(qe_udc_exit
);
2773 MODULE_DESCRIPTION(DRIVER_DESC
);
2774 MODULE_AUTHOR(DRIVER_AUTHOR
);
2775 MODULE_LICENSE("GPL");