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/slab.h>
30 #include <linux/list.h>
31 #include <linux/interrupt.h>
33 #include <linux/moduleparam.h>
34 #include <linux/of_platform.h>
35 #include <linux/dma-mapping.h>
36 #include <linux/usb/ch9.h>
37 #include <linux/usb/gadget.h>
38 #include <linux/usb/otg.h>
43 #include "fsl_qe_udc.h"
45 #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver"
46 #define DRIVER_AUTHOR "Xie XiaoBo"
47 #define DRIVER_VERSION "1.0"
49 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
51 static const char driver_name
[] = "fsl_qe_udc";
52 static const char driver_desc
[] = DRIVER_DESC
;
54 /*ep name is important in gadget, it should obey the convention of ep_match()*/
55 static const char *const ep_name
[] = {
56 "ep0-control", /* everyone has ep0 */
57 /* 3 configurable endpoints */
63 static struct usb_endpoint_descriptor qe_ep0_desc
= {
64 .bLength
= USB_DT_ENDPOINT_SIZE
,
65 .bDescriptorType
= USB_DT_ENDPOINT
,
67 .bEndpointAddress
= 0,
68 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
69 .wMaxPacketSize
= USB_MAX_CTRL_PAYLOAD
,
72 /* it is initialized in probe() */
73 static struct qe_udc
*udc_controller
;
75 /********************************************************************
76 * Internal Used Function Start
77 ********************************************************************/
78 /*-----------------------------------------------------------------
79 * done() - retire a request; caller blocked irqs
80 *--------------------------------------------------------------*/
81 static void done(struct qe_ep
*ep
, struct qe_req
*req
, int status
)
83 struct qe_udc
*udc
= ep
->udc
;
84 unsigned char stopped
= ep
->stopped
;
86 /* the req->queue pointer is used by ep_queue() func, in which
87 * the request will be added into a udc_ep->queue 'd tail
88 * so here the req will be dropped from the ep->queue
90 list_del_init(&req
->queue
);
92 /* req.status should be set as -EINPROGRESS in ep_queue() */
93 if (req
->req
.status
== -EINPROGRESS
)
94 req
->req
.status
= status
;
96 status
= req
->req
.status
;
99 dma_unmap_single(udc
->gadget
.dev
.parent
,
100 req
->req
.dma
, req
->req
.length
,
104 req
->req
.dma
= DMA_ADDR_INVALID
;
107 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
108 req
->req
.dma
, req
->req
.length
,
113 if (status
&& (status
!= -ESHUTDOWN
))
114 dev_vdbg(udc
->dev
, "complete %s req %p stat %d len %u/%u\n",
115 ep
->ep
.name
, &req
->req
, status
,
116 req
->req
.actual
, req
->req
.length
);
118 /* don't modify queue heads during completion callback */
120 spin_unlock(&udc
->lock
);
122 /* this complete() should a func implemented by gadget layer,
123 * eg fsg->bulk_in_complete() */
124 if (req
->req
.complete
)
125 req
->req
.complete(&ep
->ep
, &req
->req
);
127 spin_lock(&udc
->lock
);
129 ep
->stopped
= stopped
;
132 /*-----------------------------------------------------------------
133 * nuke(): delete all requests related to this ep
134 *--------------------------------------------------------------*/
135 static void nuke(struct qe_ep
*ep
, int status
)
137 /* Whether this eq has request linked */
138 while (!list_empty(&ep
->queue
)) {
139 struct qe_req
*req
= NULL
;
140 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
142 done(ep
, req
, status
);
146 /*---------------------------------------------------------------------------*
147 * USB and Endpoint manipulate process, include parameter and register *
148 *---------------------------------------------------------------------------*/
149 /* @value: 1--set stall 0--clean stall */
150 static int qe_eprx_stall_change(struct qe_ep
*ep
, int value
)
153 u8 epnum
= ep
->epnum
;
154 struct qe_udc
*udc
= ep
->udc
;
156 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
157 tem_usep
= tem_usep
& ~USB_RHS_MASK
;
159 tem_usep
|= USB_RHS_STALL
;
160 else if (ep
->dir
== USB_DIR_IN
)
161 tem_usep
|= USB_RHS_IGNORE_OUT
;
163 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
167 static int qe_eptx_stall_change(struct qe_ep
*ep
, int value
)
170 u8 epnum
= ep
->epnum
;
171 struct qe_udc
*udc
= ep
->udc
;
173 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
174 tem_usep
= tem_usep
& ~USB_THS_MASK
;
176 tem_usep
|= USB_THS_STALL
;
177 else if (ep
->dir
== USB_DIR_OUT
)
178 tem_usep
|= USB_THS_IGNORE_IN
;
180 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
185 static int qe_ep0_stall(struct qe_udc
*udc
)
187 qe_eptx_stall_change(&udc
->eps
[0], 1);
188 qe_eprx_stall_change(&udc
->eps
[0], 1);
189 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
190 udc_controller
->ep0_dir
= 0;
194 static int qe_eprx_nack(struct qe_ep
*ep
)
196 u8 epnum
= ep
->epnum
;
197 struct qe_udc
*udc
= ep
->udc
;
199 if (ep
->state
== EP_STATE_IDLE
) {
200 /* Set the ep's nack */
201 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[epnum
],
202 USB_RHS_MASK
, USB_RHS_NACK
);
204 /* Mask Rx and Busy interrupts */
205 clrbits16(&udc
->usb_regs
->usb_usbmr
,
206 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
208 ep
->state
= EP_STATE_NACK
;
213 static int qe_eprx_normal(struct qe_ep
*ep
)
215 struct qe_udc
*udc
= ep
->udc
;
217 if (ep
->state
== EP_STATE_NACK
) {
218 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[ep
->epnum
],
219 USB_RTHS_MASK
, USB_THS_IGNORE_IN
);
221 /* Unmask RX interrupts */
222 out_be16(&udc
->usb_regs
->usb_usber
,
223 USB_E_BSY_MASK
| USB_E_RXB_MASK
);
224 setbits16(&udc
->usb_regs
->usb_usbmr
,
225 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
227 ep
->state
= EP_STATE_IDLE
;
234 static int qe_ep_cmd_stoptx(struct qe_ep
*ep
)
236 if (ep
->udc
->soc_type
== PORT_CPM
)
237 cpm_command(CPM_USB_STOP_TX
| (ep
->epnum
<< CPM_USB_EP_SHIFT
),
238 CPM_USB_STOP_TX_OPCODE
);
240 qe_issue_cmd(QE_USB_STOP_TX
, QE_CR_SUBBLOCK_USB
,
246 static int qe_ep_cmd_restarttx(struct qe_ep
*ep
)
248 if (ep
->udc
->soc_type
== PORT_CPM
)
249 cpm_command(CPM_USB_RESTART_TX
| (ep
->epnum
<<
250 CPM_USB_EP_SHIFT
), CPM_USB_RESTART_TX_OPCODE
);
252 qe_issue_cmd(QE_USB_RESTART_TX
, QE_CR_SUBBLOCK_USB
,
258 static int qe_ep_flushtxfifo(struct qe_ep
*ep
)
260 struct qe_udc
*udc
= ep
->udc
;
265 qe_ep_cmd_stoptx(ep
);
266 out_8(&udc
->usb_regs
->usb_uscom
,
267 USB_CMD_FLUSH_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
268 out_be16(&udc
->ep_param
[i
]->tbptr
, in_be16(&udc
->ep_param
[i
]->tbase
));
269 out_be32(&udc
->ep_param
[i
]->tstate
, 0);
270 out_be16(&udc
->ep_param
[i
]->tbcnt
, 0);
272 ep
->c_txbd
= ep
->txbase
;
273 ep
->n_txbd
= ep
->txbase
;
274 qe_ep_cmd_restarttx(ep
);
278 static int qe_ep_filltxfifo(struct qe_ep
*ep
)
280 struct qe_udc
*udc
= ep
->udc
;
282 out_8(&udc
->usb_regs
->usb_uscom
,
283 USB_CMD_STR_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
287 static int qe_epbds_reset(struct qe_udc
*udc
, int pipe_num
)
291 struct qe_bd __iomem
*bd
;
294 ep
= &udc
->eps
[pipe_num
];
296 if (ep
->dir
== USB_DIR_OUT
)
297 bdring_len
= USB_BDRING_LEN_RX
;
299 bdring_len
= USB_BDRING_LEN
;
302 for (i
= 0; i
< (bdring_len
- 1); i
++) {
303 out_be32((u32 __iomem
*)bd
, R_E
| R_I
);
306 out_be32((u32 __iomem
*)bd
, R_E
| R_I
| R_W
);
309 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
310 out_be32(&bd
->buf
, 0);
311 out_be32((u32 __iomem
*)bd
, 0);
314 out_be32((u32 __iomem
*)bd
, T_W
);
319 static int qe_ep_reset(struct qe_udc
*udc
, int pipe_num
)
324 ep
= &udc
->eps
[pipe_num
];
325 tmpusep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe_num
]);
326 tmpusep
&= ~USB_RTHS_MASK
;
330 qe_ep_flushtxfifo(ep
);
333 tmpusep
|= USB_THS_IGNORE_IN
;
336 qe_ep_flushtxfifo(ep
);
337 tmpusep
|= USB_RHS_IGNORE_OUT
;
342 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], tmpusep
);
344 qe_epbds_reset(udc
, pipe_num
);
349 static int qe_ep_toggledata01(struct qe_ep
*ep
)
355 static int qe_ep_bd_init(struct qe_udc
*udc
, unsigned char pipe_num
)
357 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
358 unsigned long tmp_addr
= 0;
359 struct usb_ep_para __iomem
*epparam
;
361 struct qe_bd __iomem
*bd
;
364 if (ep
->dir
== USB_DIR_OUT
)
365 bdring_len
= USB_BDRING_LEN_RX
;
367 bdring_len
= USB_BDRING_LEN
;
369 epparam
= udc
->ep_param
[pipe_num
];
370 /* alloc multi-ram for BD rings and set the ep parameters */
371 tmp_addr
= cpm_muram_alloc(sizeof(struct qe_bd
) * (bdring_len
+
372 USB_BDRING_LEN_TX
), QE_ALIGNMENT_OF_BD
);
373 out_be16(&epparam
->rbase
, (u16
)tmp_addr
);
374 out_be16(&epparam
->tbase
, (u16
)(tmp_addr
+
375 (sizeof(struct qe_bd
) * bdring_len
)));
377 out_be16(&epparam
->rbptr
, in_be16(&epparam
->rbase
));
378 out_be16(&epparam
->tbptr
, in_be16(&epparam
->tbase
));
380 ep
->rxbase
= cpm_muram_addr(tmp_addr
);
381 ep
->txbase
= cpm_muram_addr(tmp_addr
+ (sizeof(struct qe_bd
)
383 ep
->n_rxbd
= ep
->rxbase
;
384 ep
->e_rxbd
= ep
->rxbase
;
385 ep
->n_txbd
= ep
->txbase
;
386 ep
->c_txbd
= ep
->txbase
;
387 ep
->data01
= 0; /* data0 */
389 /* Init TX and RX bds */
391 for (i
= 0; i
< bdring_len
- 1; i
++) {
392 out_be32(&bd
->buf
, 0);
393 out_be32((u32 __iomem
*)bd
, 0);
396 out_be32(&bd
->buf
, 0);
397 out_be32((u32 __iomem
*)bd
, R_W
);
400 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
401 out_be32(&bd
->buf
, 0);
402 out_be32((u32 __iomem
*)bd
, 0);
405 out_be32(&bd
->buf
, 0);
406 out_be32((u32 __iomem
*)bd
, T_W
);
411 static int qe_ep_rxbd_update(struct qe_ep
*ep
)
416 struct qe_bd __iomem
*bd
;
417 unsigned int bdring_len
;
419 if (ep
->rxbase
== NULL
)
424 ep
->rxframe
= kmalloc(sizeof(*ep
->rxframe
), GFP_ATOMIC
);
425 if (ep
->rxframe
== NULL
) {
426 dev_err(ep
->udc
->dev
, "malloc rxframe failed\n");
430 qe_frame_init(ep
->rxframe
);
432 if (ep
->dir
== USB_DIR_OUT
)
433 bdring_len
= USB_BDRING_LEN_RX
;
435 bdring_len
= USB_BDRING_LEN
;
437 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (bdring_len
+ 1);
438 ep
->rxbuffer
= kzalloc(size
, GFP_ATOMIC
);
439 if (ep
->rxbuffer
== NULL
) {
440 dev_err(ep
->udc
->dev
, "malloc rxbuffer failed,size=%d\n",
446 ep
->rxbuf_d
= virt_to_phys((void *)ep
->rxbuffer
);
447 if (ep
->rxbuf_d
== DMA_ADDR_INVALID
) {
448 ep
->rxbuf_d
= dma_map_single(udc_controller
->gadget
.dev
.parent
,
454 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
460 size
= ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2;
462 tmp
= (u32
)(((tmp
>> 2) << 2) + 4);
464 for (i
= 0; i
< bdring_len
- 1; i
++) {
465 out_be32(&bd
->buf
, tmp
);
466 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
));
470 out_be32(&bd
->buf
, tmp
);
471 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
| R_W
));
476 static int qe_ep_register_init(struct qe_udc
*udc
, unsigned char pipe_num
)
478 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
479 struct usb_ep_para __iomem
*epparam
;
484 epparam
= udc
->ep_param
[pipe_num
];
487 logepnum
= (ep
->desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
488 usep
|= (logepnum
<< USB_EPNUM_SHIFT
);
490 switch (ep
->desc
->bmAttributes
& 0x03) {
491 case USB_ENDPOINT_XFER_BULK
:
492 usep
|= USB_TRANS_BULK
;
494 case USB_ENDPOINT_XFER_ISOC
:
495 usep
|= USB_TRANS_ISO
;
497 case USB_ENDPOINT_XFER_INT
:
498 usep
|= USB_TRANS_INT
;
501 usep
|= USB_TRANS_CTR
;
507 usep
|= USB_THS_IGNORE_IN
;
510 usep
|= USB_RHS_IGNORE_OUT
;
515 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], usep
);
518 out_8(&epparam
->rbmr
, rtfcr
);
519 out_8(&epparam
->tbmr
, rtfcr
);
521 tmp
= (u16
)(ep
->ep
.maxpacket
+ USB_CRC_SIZE
);
522 /* MRBLR must be divisble by 4 */
523 tmp
= (u16
)(((tmp
>> 2) << 2) + 4);
524 out_be16(&epparam
->mrblr
, tmp
);
529 static int qe_ep_init(struct qe_udc
*udc
,
530 unsigned char pipe_num
,
531 const struct usb_endpoint_descriptor
*desc
)
533 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
538 max
= le16_to_cpu(desc
->wMaxPacketSize
);
540 /* check the max package size validate for this endpoint */
541 /* Refer to USB2.0 spec table 9-13,
544 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
545 case USB_ENDPOINT_XFER_BULK
:
546 if (strstr(ep
->ep
.name
, "-iso")
547 || strstr(ep
->ep
.name
, "-int"))
549 switch (udc
->gadget
.speed
) {
551 if ((max
== 128) || (max
== 256) || (max
== 512))
567 case USB_ENDPOINT_XFER_INT
:
568 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
570 switch (udc
->gadget
.speed
) {
583 case USB_ENDPOINT_XFER_ISOC
:
584 if (strstr(ep
->ep
.name
, "-bulk")
585 || strstr(ep
->ep
.name
, "-int"))
587 switch (udc
->gadget
.speed
) {
598 case USB_ENDPOINT_XFER_CONTROL
:
599 if (strstr(ep
->ep
.name
, "-iso")
600 || strstr(ep
->ep
.name
, "-int"))
602 switch (udc
->gadget
.speed
) {
637 spin_lock_irqsave(&udc
->lock
, flags
);
639 /* initialize ep structure */
640 ep
->ep
.maxpacket
= max
;
641 ep
->tm
= (u8
)(desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
647 ep
->dir
= USB_DIR_BOTH
;
648 udc
->ep0_dir
= USB_DIR_OUT
;
649 udc
->ep0_state
= WAIT_FOR_SETUP
;
651 switch (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) {
653 ep
->dir
= USB_DIR_OUT
;
656 ep
->dir
= USB_DIR_IN
;
662 /* hardware special operation */
663 qe_ep_bd_init(udc
, pipe_num
);
664 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_OUT
)) {
665 reval
= qe_ep_rxbd_update(ep
);
670 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_IN
)) {
671 ep
->txframe
= kmalloc(sizeof(*ep
->txframe
), GFP_ATOMIC
);
672 if (ep
->txframe
== NULL
) {
673 dev_err(udc
->dev
, "malloc txframe failed\n");
676 qe_frame_init(ep
->txframe
);
679 qe_ep_register_init(udc
, pipe_num
);
681 /* Now HW will be NAKing transfers to that EP,
682 * until a buffer is queued to it. */
683 spin_unlock_irqrestore(&udc
->lock
, flags
);
690 spin_unlock_irqrestore(&udc
->lock
, flags
);
692 dev_dbg(udc
->dev
, "failed to initialize %s\n", ep
->ep
.name
);
696 static inline void qe_usb_enable(void)
698 setbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
701 static inline void qe_usb_disable(void)
703 clrbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
706 /*----------------------------------------------------------------------------*
707 * USB and EP basic manipulate function end *
708 *----------------------------------------------------------------------------*/
711 /******************************************************************************
712 UDC transmit and receive process
713 ******************************************************************************/
714 static void recycle_one_rxbd(struct qe_ep
*ep
)
718 bdstatus
= in_be32((u32 __iomem
*)ep
->e_rxbd
);
719 bdstatus
= R_I
| R_E
| (bdstatus
& R_W
);
720 out_be32((u32 __iomem
*)ep
->e_rxbd
, bdstatus
);
723 ep
->e_rxbd
= ep
->rxbase
;
728 static void recycle_rxbds(struct qe_ep
*ep
, unsigned char stopatnext
)
731 struct qe_bd __iomem
*bd
, *nextbd
;
732 unsigned char stop
= 0;
736 bdstatus
= in_be32((u32 __iomem
*)bd
);
738 while (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
) && !stop
) {
739 bdstatus
= R_E
| R_I
| (bdstatus
& R_W
);
740 out_be32((u32 __iomem
*)bd
, bdstatus
);
747 bdstatus
= in_be32((u32 __iomem
*)bd
);
748 if (stopatnext
&& (bd
== nextbd
))
755 static void ep_recycle_rxbds(struct qe_ep
*ep
)
757 struct qe_bd __iomem
*bd
= ep
->n_rxbd
;
759 u8 epnum
= ep
->epnum
;
760 struct qe_udc
*udc
= ep
->udc
;
762 bdstatus
= in_be32((u32 __iomem
*)bd
);
763 if (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
)) {
765 ((in_be16(&udc
->ep_param
[epnum
]->rbptr
) -
766 in_be16(&udc
->ep_param
[epnum
]->rbase
))
768 bdstatus
= in_be32((u32 __iomem
*)bd
);
776 recycle_rxbds(ep
, 0);
777 ep
->e_rxbd
= ep
->n_rxbd
;
779 recycle_rxbds(ep
, 1);
781 if (in_be16(&udc
->usb_regs
->usb_usber
) & USB_E_BSY_MASK
)
782 out_be16(&udc
->usb_regs
->usb_usber
, USB_E_BSY_MASK
);
784 if (ep
->has_data
<= 0 && (!list_empty(&ep
->queue
)))
790 static void setup_received_handle(struct qe_udc
*udc
,
791 struct usb_ctrlrequest
*setup
);
792 static int qe_ep_rxframe_handle(struct qe_ep
*ep
);
793 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
);
794 /* when BD PID is setup, handle the packet */
795 static int ep0_setup_handle(struct qe_udc
*udc
)
797 struct qe_ep
*ep
= &udc
->eps
[0];
798 struct qe_frame
*pframe
;
802 pframe
= ep
->rxframe
;
803 if ((frame_get_info(pframe
) & PID_SETUP
)
804 && (udc
->ep0_state
== WAIT_FOR_SETUP
)) {
805 fsize
= frame_get_length(pframe
);
806 if (unlikely(fsize
!= 8))
808 cp
= (u8
*)&udc
->local_setup_buff
;
809 memcpy(cp
, pframe
->data
, fsize
);
812 /* handle the usb command base on the usb_ctrlrequest */
813 setup_received_handle(udc
, &udc
->local_setup_buff
);
819 static int qe_ep0_rx(struct qe_udc
*udc
)
821 struct qe_ep
*ep
= &udc
->eps
[0];
822 struct qe_frame
*pframe
;
823 struct qe_bd __iomem
*bd
;
824 u32 bdstatus
, length
;
827 pframe
= ep
->rxframe
;
829 if (ep
->dir
== USB_DIR_IN
) {
830 dev_err(udc
->dev
, "ep0 not a control endpoint\n");
835 bdstatus
= in_be32((u32 __iomem
*)bd
);
836 length
= bdstatus
& BD_LENGTH_MASK
;
838 while (!(bdstatus
& R_E
) && length
) {
839 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
840 && !(bdstatus
& R_ERROR
)) {
841 if (length
== USB_CRC_SIZE
) {
842 udc
->ep0_state
= WAIT_FOR_SETUP
;
844 "receive a ZLP in status phase\n");
846 qe_frame_clean(pframe
);
847 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
848 frame_set_data(pframe
, (u8
*)vaddr
);
849 frame_set_length(pframe
,
850 (length
- USB_CRC_SIZE
));
851 frame_set_status(pframe
, FRAME_OK
);
852 switch (bdstatus
& R_PID
) {
854 frame_set_info(pframe
, PID_SETUP
);
857 frame_set_info(pframe
, PID_DATA1
);
860 frame_set_info(pframe
, PID_DATA0
);
864 if ((bdstatus
& R_PID
) == R_PID_SETUP
)
865 ep0_setup_handle(udc
);
867 qe_ep_rxframe_handle(ep
);
870 dev_err(udc
->dev
, "The receive frame with error!\n");
873 /* note: don't clear the rxbd's buffer address */
874 recycle_one_rxbd(ep
);
882 bdstatus
= in_be32((u32 __iomem
*)bd
);
883 length
= bdstatus
& BD_LENGTH_MASK
;
892 static int qe_ep_rxframe_handle(struct qe_ep
*ep
)
894 struct qe_frame
*pframe
;
900 pframe
= ep
->rxframe
;
902 if (frame_get_info(pframe
) & PID_DATA1
)
905 if (framepid
!= ep
->data01
) {
906 dev_err(ep
->udc
->dev
, "the data01 error!\n");
910 fsize
= frame_get_length(pframe
);
911 if (list_empty(&ep
->queue
)) {
912 dev_err(ep
->udc
->dev
, "the %s have no requeue!\n", ep
->name
);
914 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
916 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
918 memcpy(cp
, pframe
->data
, fsize
);
919 req
->req
.actual
+= fsize
;
920 if ((fsize
< ep
->ep
.maxpacket
) ||
921 (req
->req
.actual
>= req
->req
.length
)) {
923 ep0_req_complete(ep
->udc
, req
);
926 if (list_empty(&ep
->queue
) && ep
->epnum
!= 0)
932 qe_ep_toggledata01(ep
);
937 static void ep_rx_tasklet(unsigned long data
)
939 struct qe_udc
*udc
= (struct qe_udc
*)data
;
941 struct qe_frame
*pframe
;
942 struct qe_bd __iomem
*bd
;
944 u32 bdstatus
, length
;
947 spin_lock_irqsave(&udc
->lock
, flags
);
949 for (i
= 1; i
< USB_MAX_ENDPOINTS
; i
++) {
952 if (ep
->dir
== USB_DIR_IN
|| ep
->enable_tasklet
== 0) {
954 "This is a transmit ep or disable tasklet!\n");
958 pframe
= ep
->rxframe
;
960 bdstatus
= in_be32((u32 __iomem
*)bd
);
961 length
= bdstatus
& BD_LENGTH_MASK
;
963 while (!(bdstatus
& R_E
) && length
) {
964 if (list_empty(&ep
->queue
)) {
967 "The rxep have noreq %d\n",
972 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
973 && !(bdstatus
& R_ERROR
)) {
974 qe_frame_clean(pframe
);
975 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
976 frame_set_data(pframe
, (u8
*)vaddr
);
977 frame_set_length(pframe
,
978 (length
- USB_CRC_SIZE
));
979 frame_set_status(pframe
, FRAME_OK
);
980 switch (bdstatus
& R_PID
) {
982 frame_set_info(pframe
, PID_DATA1
);
985 frame_set_info(pframe
, PID_SETUP
);
988 frame_set_info(pframe
, PID_DATA0
);
991 /* handle the rx frame */
992 qe_ep_rxframe_handle(ep
);
995 "error in received frame\n");
997 /* note: don't clear the rxbd's buffer address */
998 /*clear the length */
999 out_be32((u32 __iomem
*)bd
, bdstatus
& BD_STATUS_MASK
);
1001 if (!(ep
->localnack
))
1002 recycle_one_rxbd(ep
);
1010 bdstatus
= in_be32((u32 __iomem
*)bd
);
1011 length
= bdstatus
& BD_LENGTH_MASK
;
1017 ep_recycle_rxbds(ep
);
1019 ep
->enable_tasklet
= 0;
1022 spin_unlock_irqrestore(&udc
->lock
, flags
);
1025 static int qe_ep_rx(struct qe_ep
*ep
)
1028 struct qe_frame
*pframe
;
1029 struct qe_bd __iomem
*bd
;
1030 u16 swoffs
, ucoffs
, emptybds
;
1033 pframe
= ep
->rxframe
;
1035 if (ep
->dir
== USB_DIR_IN
) {
1036 dev_err(udc
->dev
, "transmit ep in rx function\n");
1042 swoffs
= (u16
)(bd
- ep
->rxbase
);
1043 ucoffs
= (u16
)((in_be16(&udc
->ep_param
[ep
->epnum
]->rbptr
) -
1044 in_be16(&udc
->ep_param
[ep
->epnum
]->rbase
)) >> 3);
1045 if (swoffs
< ucoffs
)
1046 emptybds
= USB_BDRING_LEN_RX
- ucoffs
+ swoffs
;
1048 emptybds
= swoffs
- ucoffs
;
1050 if (emptybds
< MIN_EMPTY_BDS
) {
1053 dev_vdbg(udc
->dev
, "%d empty bds, send NACK\n", emptybds
);
1055 ep
->has_data
= USB_BDRING_LEN_RX
- emptybds
;
1057 if (list_empty(&ep
->queue
)) {
1059 dev_vdbg(udc
->dev
, "The rxep have no req queued with %d BDs\n",
1064 tasklet_schedule(&udc
->rx_tasklet
);
1065 ep
->enable_tasklet
= 1;
1070 /* send data from a frame, no matter what tx_req */
1071 static int qe_ep_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1073 struct qe_udc
*udc
= ep
->udc
;
1074 struct qe_bd __iomem
*bd
;
1076 u32 bdstatus
, pidmask
;
1079 if (ep
->dir
== USB_DIR_OUT
) {
1080 dev_err(udc
->dev
, "receive ep passed to tx function\n");
1084 /* Disable the Tx interrupt */
1085 saveusbmr
= in_be16(&udc
->usb_regs
->usb_usbmr
);
1086 out_be16(&udc
->usb_regs
->usb_usbmr
,
1087 saveusbmr
& ~(USB_E_TXB_MASK
| USB_E_TXE_MASK
));
1090 bdstatus
= in_be32((u32 __iomem
*)bd
);
1092 if (!(bdstatus
& (T_R
| BD_LENGTH_MASK
))) {
1093 if (frame_get_length(frame
) == 0) {
1094 frame_set_data(frame
, udc
->nullbuf
);
1095 frame_set_length(frame
, 2);
1096 frame
->info
|= (ZLP
| NO_CRC
);
1097 dev_vdbg(udc
->dev
, "the frame size = 0\n");
1099 paddr
= virt_to_phys((void *)frame
->data
);
1100 out_be32(&bd
->buf
, paddr
);
1101 bdstatus
= (bdstatus
&T_W
);
1102 if (!(frame_get_info(frame
) & NO_CRC
))
1103 bdstatus
|= T_R
| T_I
| T_L
| T_TC
1104 | frame_get_length(frame
);
1106 bdstatus
|= T_R
| T_I
| T_L
| frame_get_length(frame
);
1108 /* if the packet is a ZLP in status phase */
1109 if ((ep
->epnum
== 0) && (udc
->ep0_state
== DATA_STATE_NEED_ZLP
))
1113 pidmask
= T_PID_DATA1
;
1114 frame
->info
|= PID_DATA1
;
1116 pidmask
= T_PID_DATA0
;
1117 frame
->info
|= PID_DATA0
;
1120 bdstatus
|= pidmask
;
1121 out_be32((u32 __iomem
*)bd
, bdstatus
);
1122 qe_ep_filltxfifo(ep
);
1124 /* enable the TX interrupt */
1125 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1127 qe_ep_toggledata01(ep
);
1129 ep
->n_txbd
= ep
->txbase
;
1135 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1136 dev_vdbg(udc
->dev
, "The tx bd is not ready!\n");
1141 /* when a bd was transmitted, the function can
1142 * handle the tx_req, not include ep0 */
1143 static int txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1145 if (ep
->tx_req
!= NULL
) {
1147 int asent
= ep
->last
;
1154 /* a request already were transmitted completely */
1155 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1156 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1157 done(ep
, ep
->tx_req
, 0);
1164 /* we should gain a new tx_req fot this endpoint */
1165 if (ep
->tx_req
== NULL
) {
1166 if (!list_empty(&ep
->queue
)) {
1167 ep
->tx_req
= list_entry(ep
->queue
.next
, struct qe_req
,
1177 /* give a frame and a tx_req, send some data */
1178 static int qe_usb_senddata(struct qe_ep
*ep
, struct qe_frame
*frame
)
1183 qe_frame_clean(frame
);
1184 size
= min_t(u32
, (ep
->tx_req
->req
.length
- ep
->sent
),
1186 buf
= (u8
*)ep
->tx_req
->req
.buf
+ ep
->sent
;
1189 frame_set_data(frame
, buf
);
1190 frame_set_length(frame
, size
);
1191 frame_set_status(frame
, FRAME_OK
);
1192 frame_set_info(frame
, 0);
1193 return qe_ep_tx(ep
, frame
);
1198 /* give a frame struct,send a ZLP */
1199 static int sendnulldata(struct qe_ep
*ep
, struct qe_frame
*frame
, uint infor
)
1201 struct qe_udc
*udc
= ep
->udc
;
1206 qe_frame_clean(frame
);
1207 frame_set_data(frame
, (u8
*)udc
->nullbuf
);
1208 frame_set_length(frame
, 2);
1209 frame_set_status(frame
, FRAME_OK
);
1210 frame_set_info(frame
, (ZLP
| NO_CRC
| infor
));
1212 return qe_ep_tx(ep
, frame
);
1215 static int frame_create_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1217 struct qe_req
*req
= ep
->tx_req
;
1223 if ((req
->req
.length
- ep
->sent
) > 0)
1224 reval
= qe_usb_senddata(ep
, frame
);
1226 reval
= sendnulldata(ep
, frame
, 0);
1231 /* if direction is DIR_IN, the status is Device->Host
1232 * if direction is DIR_OUT, the status transaction is Device<-Host
1233 * in status phase, udc create a request and gain status */
1234 static int ep0_prime_status(struct qe_udc
*udc
, int direction
)
1237 struct qe_ep
*ep
= &udc
->eps
[0];
1239 if (direction
== USB_DIR_IN
) {
1240 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
1241 udc
->ep0_dir
= USB_DIR_IN
;
1242 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1244 udc
->ep0_dir
= USB_DIR_OUT
;
1245 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1251 /* a request complete in ep0, whether gadget request or udc request */
1252 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
)
1254 struct qe_ep
*ep
= &udc
->eps
[0];
1255 /* because usb and ep's status already been set in ch9setaddress() */
1257 switch (udc
->ep0_state
) {
1258 case DATA_STATE_XMIT
:
1260 /* receive status phase */
1261 if (ep0_prime_status(udc
, USB_DIR_OUT
))
1265 case DATA_STATE_NEED_ZLP
:
1267 udc
->ep0_state
= WAIT_FOR_SETUP
;
1270 case DATA_STATE_RECV
:
1272 /* send status phase */
1273 if (ep0_prime_status(udc
, USB_DIR_IN
))
1277 case WAIT_FOR_OUT_STATUS
:
1279 udc
->ep0_state
= WAIT_FOR_SETUP
;
1282 case WAIT_FOR_SETUP
:
1283 dev_vdbg(udc
->dev
, "Unexpected interrupt\n");
1292 static int ep0_txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1294 struct qe_req
*tx_req
= NULL
;
1295 struct qe_frame
*frame
= ep
->txframe
;
1297 if ((frame_get_info(frame
) & (ZLP
| NO_REQ
)) == (ZLP
| NO_REQ
)) {
1299 ep
->udc
->ep0_state
= WAIT_FOR_SETUP
;
1301 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1305 tx_req
= ep
->tx_req
;
1306 if (tx_req
!= NULL
) {
1308 int asent
= ep
->last
;
1315 /* a request already were transmitted completely */
1316 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1317 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1318 ep0_req_complete(ep
->udc
, ep
->tx_req
);
1324 dev_vdbg(ep
->udc
->dev
, "the ep0_controller have no req\n");
1330 static int ep0_txframe_handle(struct qe_ep
*ep
)
1332 /* if have error, transmit again */
1333 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1334 qe_ep_flushtxfifo(ep
);
1335 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1336 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1341 ep0_txcomplete(ep
, 1);
1343 ep0_txcomplete(ep
, 0);
1345 frame_create_tx(ep
, ep
->txframe
);
1349 static int qe_ep0_txconf(struct qe_ep
*ep
)
1351 struct qe_bd __iomem
*bd
;
1352 struct qe_frame
*pframe
;
1356 bdstatus
= in_be32((u32 __iomem
*)bd
);
1357 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1358 pframe
= ep
->txframe
;
1360 /* clear and recycle the BD */
1361 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1362 out_be32(&bd
->buf
, 0);
1364 ep
->c_txbd
= ep
->txbase
;
1368 if (ep
->c_txbd
== ep
->n_txbd
) {
1369 if (bdstatus
& DEVICE_T_ERROR
) {
1370 frame_set_status(pframe
, FRAME_ERROR
);
1371 if (bdstatus
& T_TO
)
1372 pframe
->status
|= TX_ER_TIMEOUT
;
1373 if (bdstatus
& T_UN
)
1374 pframe
->status
|= TX_ER_UNDERUN
;
1376 ep0_txframe_handle(ep
);
1380 bdstatus
= in_be32((u32 __iomem
*)bd
);
1386 static int ep_txframe_handle(struct qe_ep
*ep
)
1388 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1389 qe_ep_flushtxfifo(ep
);
1390 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1391 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1400 frame_create_tx(ep
, ep
->txframe
); /* send the data */
1404 /* confirm the already trainsmited bd */
1405 static int qe_ep_txconf(struct qe_ep
*ep
)
1407 struct qe_bd __iomem
*bd
;
1408 struct qe_frame
*pframe
= NULL
;
1410 unsigned char breakonrxinterrupt
= 0;
1413 bdstatus
= in_be32((u32 __iomem
*)bd
);
1414 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1415 pframe
= ep
->txframe
;
1416 if (bdstatus
& DEVICE_T_ERROR
) {
1417 frame_set_status(pframe
, FRAME_ERROR
);
1418 if (bdstatus
& T_TO
)
1419 pframe
->status
|= TX_ER_TIMEOUT
;
1420 if (bdstatus
& T_UN
)
1421 pframe
->status
|= TX_ER_UNDERUN
;
1424 /* clear and recycle the BD */
1425 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1426 out_be32(&bd
->buf
, 0);
1428 ep
->c_txbd
= ep
->txbase
;
1432 /* handle the tx frame */
1433 ep_txframe_handle(ep
);
1435 bdstatus
= in_be32((u32 __iomem
*)bd
);
1437 if (breakonrxinterrupt
)
1443 /* Add a request in queue, and try to transmit a packet */
1444 static int ep_req_send(struct qe_ep
*ep
, struct qe_req
*req
)
1448 if (ep
->tx_req
== NULL
) {
1451 txcomplete(ep
, 0); /* can gain a new tx_req */
1452 reval
= frame_create_tx(ep
, ep
->txframe
);
1457 /* Maybe this is a good ideal */
1458 static int ep_req_rx(struct qe_ep
*ep
, struct qe_req
*req
)
1460 struct qe_udc
*udc
= ep
->udc
;
1461 struct qe_frame
*pframe
= NULL
;
1462 struct qe_bd __iomem
*bd
;
1463 u32 bdstatus
, length
;
1469 if (list_empty(&ep
->queue
)) {
1470 dev_vdbg(udc
->dev
, "the req already finish!\n");
1473 pframe
= ep
->rxframe
;
1476 bdstatus
= in_be32((u32 __iomem
*)bd
);
1477 length
= bdstatus
& BD_LENGTH_MASK
;
1479 while (!(bdstatus
& R_E
) && length
) {
1482 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
1483 && !(bdstatus
& R_ERROR
)) {
1484 qe_frame_clean(pframe
);
1485 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
1486 frame_set_data(pframe
, (u8
*)vaddr
);
1487 frame_set_length(pframe
, (length
- USB_CRC_SIZE
));
1488 frame_set_status(pframe
, FRAME_OK
);
1489 switch (bdstatus
& R_PID
) {
1491 frame_set_info(pframe
, PID_DATA1
); break;
1493 frame_set_info(pframe
, PID_DATA0
); break;
1495 /* handle the rx frame */
1497 if (frame_get_info(pframe
) & PID_DATA1
)
1502 if (framepid
!= ep
->data01
) {
1503 dev_vdbg(udc
->dev
, "the data01 error!\n");
1505 fsize
= frame_get_length(pframe
);
1507 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
1509 memcpy(cp
, pframe
->data
, fsize
);
1510 req
->req
.actual
+= fsize
;
1511 if ((fsize
< ep
->ep
.maxpacket
)
1512 || (req
->req
.actual
>=
1516 if (list_empty(&ep
->queue
))
1520 qe_ep_toggledata01(ep
);
1523 dev_err(udc
->dev
, "The receive frame with error!\n");
1526 /* note: don't clear the rxbd's buffer address *
1527 * only Clear the length */
1528 out_be32((u32 __iomem
*)bd
, (bdstatus
& BD_STATUS_MASK
));
1537 bdstatus
= in_be32((u32 __iomem
*)bd
);
1538 length
= bdstatus
& BD_LENGTH_MASK
;
1542 ep_recycle_rxbds(ep
);
1547 /* only add the request in queue */
1548 static int ep_req_receive(struct qe_ep
*ep
, struct qe_req
*req
)
1550 if (ep
->state
== EP_STATE_NACK
) {
1551 if (ep
->has_data
<= 0) {
1552 /* Enable rx and unmask rx interrupt */
1555 /* Copy the exist BD data */
1563 /********************************************************************
1564 Internal Used Function End
1565 ********************************************************************/
1567 /*-----------------------------------------------------------------------
1568 Endpoint Management Functions For Gadget
1569 -----------------------------------------------------------------------*/
1570 static int qe_ep_enable(struct usb_ep
*_ep
,
1571 const struct usb_endpoint_descriptor
*desc
)
1576 unsigned char epnum
;
1578 ep
= container_of(_ep
, struct qe_ep
, ep
);
1580 /* catch various bogus parameters */
1581 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep_name
[0] ||
1582 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
))
1586 if (!udc
->driver
|| (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
))
1589 epnum
= (u8
)desc
->bEndpointAddress
& 0xF;
1591 retval
= qe_ep_init(udc
, epnum
, desc
);
1593 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1594 dev_dbg(udc
->dev
, "enable ep%d failed\n", ep
->epnum
);
1597 dev_dbg(udc
->dev
, "enable ep%d successful\n", ep
->epnum
);
1601 static int qe_ep_disable(struct usb_ep
*_ep
)
1605 unsigned long flags
;
1608 ep
= container_of(_ep
, struct qe_ep
, ep
);
1611 if (!_ep
|| !ep
->desc
) {
1612 dev_dbg(udc
->dev
, "%s not enabled\n", _ep
? ep
->ep
.name
: NULL
);
1616 spin_lock_irqsave(&udc
->lock
, flags
);
1617 /* Nuke all pending requests (does flush) */
1618 nuke(ep
, -ESHUTDOWN
);
1621 spin_unlock_irqrestore(&udc
->lock
, flags
);
1623 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1625 if (ep
->dir
== USB_DIR_OUT
)
1626 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1627 (USB_BDRING_LEN_RX
+ 1);
1629 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1630 (USB_BDRING_LEN
+ 1);
1632 if (ep
->dir
!= USB_DIR_IN
) {
1635 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
1638 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
1640 dma_sync_single_for_cpu(
1641 udc_controller
->gadget
.dev
.parent
,
1645 kfree(ep
->rxbuffer
);
1648 if (ep
->dir
!= USB_DIR_OUT
)
1651 dev_dbg(udc
->dev
, "disabled %s OK\n", _ep
->name
);
1655 static struct usb_request
*qe_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
1659 req
= kzalloc(sizeof(*req
), gfp_flags
);
1663 req
->req
.dma
= DMA_ADDR_INVALID
;
1665 INIT_LIST_HEAD(&req
->queue
);
1670 static void qe_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1674 req
= container_of(_req
, struct qe_req
, req
);
1680 /* queues (submits) an I/O request to an endpoint */
1681 static int qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1684 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1685 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1687 unsigned long flags
;
1691 /* catch various bogus parameters */
1692 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
1693 || !list_empty(&req
->queue
)) {
1694 dev_dbg(udc
->dev
, "bad params\n");
1697 if (!_ep
|| (!ep
->desc
&& ep_index(ep
))) {
1698 dev_dbg(udc
->dev
, "bad ep\n");
1702 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1707 /* map virtual address to hardware */
1708 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
1709 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
1717 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
1718 req
->req
.dma
, req
->req
.length
,
1725 req
->req
.status
= -EINPROGRESS
;
1726 req
->req
.actual
= 0;
1728 list_add_tail(&req
->queue
, &ep
->queue
);
1729 dev_vdbg(udc
->dev
, "gadget have request in %s! %d\n",
1730 ep
->name
, req
->req
.length
);
1731 spin_lock_irqsave(&udc
->lock
, flags
);
1732 /* push the request to device */
1734 reval
= ep_req_send(ep
, req
);
1737 if (ep_index(ep
) == 0 && req
->req
.length
> 0) {
1739 udc
->ep0_state
= DATA_STATE_XMIT
;
1741 udc
->ep0_state
= DATA_STATE_RECV
;
1744 if (ep
->dir
== USB_DIR_OUT
)
1745 reval
= ep_req_receive(ep
, req
);
1747 spin_unlock_irqrestore(&udc
->lock
, flags
);
1752 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1753 static int qe_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1755 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1757 unsigned long flags
;
1762 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1764 /* make sure it's actually queued on this endpoint */
1765 list_for_each_entry(req
, &ep
->queue
, queue
) {
1766 if (&req
->req
== _req
)
1770 if (&req
->req
!= _req
) {
1771 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1775 done(ep
, req
, -ECONNRESET
);
1777 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1781 /*-----------------------------------------------------------------
1782 * modify the endpoint halt feature
1783 * @ep: the non-isochronous endpoint being stalled
1784 * @value: 1--set halt 0--clear halt
1785 * Returns zero, or a negative error code.
1786 *----------------------------------------------------------------*/
1787 static int qe_ep_set_halt(struct usb_ep
*_ep
, int value
)
1790 unsigned long flags
;
1791 int status
= -EOPNOTSUPP
;
1794 ep
= container_of(_ep
, struct qe_ep
, ep
);
1795 if (!_ep
|| !ep
->desc
) {
1801 /* Attempt to halt IN ep will fail if any transfer requests
1802 * are still queue */
1803 if (value
&& ep_is_in(ep
) && !list_empty(&ep
->queue
)) {
1809 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1810 qe_eptx_stall_change(ep
, value
);
1811 qe_eprx_stall_change(ep
, value
);
1812 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1814 if (ep
->epnum
== 0) {
1815 udc
->ep0_state
= WAIT_FOR_SETUP
;
1819 /* set data toggle to DATA0 on clear halt */
1823 dev_vdbg(udc
->dev
, "%s %s halt stat %d\n", ep
->ep
.name
,
1824 value
? "set" : "clear", status
);
1829 static struct usb_ep_ops qe_ep_ops
= {
1830 .enable
= qe_ep_enable
,
1831 .disable
= qe_ep_disable
,
1833 .alloc_request
= qe_alloc_request
,
1834 .free_request
= qe_free_request
,
1836 .queue
= qe_ep_queue
,
1837 .dequeue
= qe_ep_dequeue
,
1839 .set_halt
= qe_ep_set_halt
,
1842 /*------------------------------------------------------------------------
1843 Gadget Driver Layer Operations
1844 ------------------------------------------------------------------------*/
1846 /* Get the current frame number */
1847 static int qe_get_frame(struct usb_gadget
*gadget
)
1851 tmp
= in_be16(&udc_controller
->usb_param
->frame_n
);
1860 /* Tries to wake up the host connected to this gadget
1862 * Return : 0-success
1863 * Negative-this feature not enabled by host or not supported by device hw
1865 static int qe_wakeup(struct usb_gadget
*gadget
)
1870 /* Notify controller that VBUS is powered, Called by whatever
1871 detects VBUS sessions */
1872 static int qe_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1877 /* constrain controller's VBUS power usage
1878 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1879 * reporting how much power the device may consume. For example, this
1880 * could affect how quickly batteries are recharged.
1882 * Returns zero on success, else negative errno.
1884 static int qe_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1889 /* Change Data+ pullup status
1890 * this func is used by usb_gadget_connect/disconnect
1892 static int qe_pullup(struct usb_gadget
*gadget
, int is_on
)
1897 /* defined in usb_gadget.h */
1898 static struct usb_gadget_ops qe_gadget_ops
= {
1899 .get_frame
= qe_get_frame
,
1900 .wakeup
= qe_wakeup
,
1901 /* .set_selfpowered = qe_set_selfpowered,*/ /* always selfpowered */
1902 .vbus_session
= qe_vbus_session
,
1903 .vbus_draw
= qe_vbus_draw
,
1904 .pullup
= qe_pullup
,
1907 /*-------------------------------------------------------------------------
1908 USB ep0 Setup process in BUS Enumeration
1909 -------------------------------------------------------------------------*/
1910 static int udc_reset_ep_queue(struct qe_udc
*udc
, u8 pipe
)
1912 struct qe_ep
*ep
= &udc
->eps
[pipe
];
1914 nuke(ep
, -ECONNRESET
);
1919 static int reset_queues(struct qe_udc
*udc
)
1923 for (pipe
= 0; pipe
< USB_MAX_ENDPOINTS
; pipe
++)
1924 udc_reset_ep_queue(udc
, pipe
);
1926 /* report disconnect; the driver is already quiesced */
1927 spin_unlock(&udc
->lock
);
1928 udc
->driver
->disconnect(&udc
->gadget
);
1929 spin_lock(&udc
->lock
);
1934 static void ch9setaddress(struct qe_udc
*udc
, u16 value
, u16 index
,
1937 /* Save the new address to device struct */
1938 udc
->device_address
= (u8
) value
;
1939 /* Update usb state */
1940 udc
->usb_state
= USB_STATE_ADDRESS
;
1942 /* Status phase , send a ZLP */
1943 if (ep0_prime_status(udc
, USB_DIR_IN
))
1947 static void ownercomplete(struct usb_ep
*_ep
, struct usb_request
*_req
)
1949 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1951 req
->req
.buf
= NULL
;
1955 static void ch9getstatus(struct qe_udc
*udc
, u8 request_type
, u16 value
,
1956 u16 index
, u16 length
)
1964 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1965 /* Get device status */
1966 usb_status
= 1 << USB_DEVICE_SELF_POWERED
;
1967 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
1968 /* Get interface status */
1969 /* We don't have interface information in udc driver */
1971 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
1972 /* Get endpoint status */
1973 int pipe
= index
& USB_ENDPOINT_NUMBER_MASK
;
1974 struct qe_ep
*target_ep
= &udc
->eps
[pipe
];
1977 /* stall if endpoint doesn't exist */
1978 if (!target_ep
->desc
)
1981 usep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe
]);
1982 if (index
& USB_DIR_IN
) {
1983 if (target_ep
->dir
!= USB_DIR_IN
)
1985 if ((usep
& USB_THS_MASK
) == USB_THS_STALL
)
1986 usb_status
= 1 << USB_ENDPOINT_HALT
;
1988 if (target_ep
->dir
!= USB_DIR_OUT
)
1990 if ((usep
& USB_RHS_MASK
) == USB_RHS_STALL
)
1991 usb_status
= 1 << USB_ENDPOINT_HALT
;
1995 req
= container_of(qe_alloc_request(&ep
->ep
, GFP_KERNEL
),
1996 struct qe_req
, req
);
1997 req
->req
.length
= 2;
1998 req
->req
.buf
= udc
->statusbuf
;
1999 *(u16
*)req
->req
.buf
= cpu_to_le16(usb_status
);
2000 req
->req
.status
= -EINPROGRESS
;
2001 req
->req
.actual
= 0;
2002 req
->req
.complete
= ownercomplete
;
2004 udc
->ep0_dir
= USB_DIR_IN
;
2007 status
= qe_ep_queue(&ep
->ep
, &req
->req
, GFP_ATOMIC
);
2012 dev_err(udc
->dev
, "Can't respond to getstatus request \n");
2016 /* only handle the setup request, suppose the device in normal status */
2017 static void setup_received_handle(struct qe_udc
*udc
,
2018 struct usb_ctrlrequest
*setup
)
2020 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2021 u16 wValue
= le16_to_cpu(setup
->wValue
);
2022 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
2023 u16 wLength
= le16_to_cpu(setup
->wLength
);
2025 /* clear the previous request in the ep0 */
2026 udc_reset_ep_queue(udc
, 0);
2028 if (setup
->bRequestType
& USB_DIR_IN
)
2029 udc
->ep0_dir
= USB_DIR_IN
;
2031 udc
->ep0_dir
= USB_DIR_OUT
;
2033 switch (setup
->bRequest
) {
2034 case USB_REQ_GET_STATUS
:
2035 /* Data+Status phase form udc */
2036 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
2037 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
2039 ch9getstatus(udc
, setup
->bRequestType
, wValue
, wIndex
,
2043 case USB_REQ_SET_ADDRESS
:
2044 /* Status phase from udc */
2045 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
2048 ch9setaddress(udc
, wValue
, wIndex
, wLength
);
2051 case USB_REQ_CLEAR_FEATURE
:
2052 case USB_REQ_SET_FEATURE
:
2053 /* Requests with no data phase, status phase from udc */
2054 if ((setup
->bRequestType
& USB_TYPE_MASK
)
2055 != USB_TYPE_STANDARD
)
2058 if ((setup
->bRequestType
& USB_RECIP_MASK
)
2059 == USB_RECIP_ENDPOINT
) {
2060 int pipe
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2063 if (wValue
!= 0 || wLength
!= 0
2064 || pipe
> USB_MAX_ENDPOINTS
)
2066 ep
= &udc
->eps
[pipe
];
2068 spin_unlock(&udc
->lock
);
2069 qe_ep_set_halt(&ep
->ep
,
2070 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2072 spin_lock(&udc
->lock
);
2075 ep0_prime_status(udc
, USB_DIR_IN
);
2084 /* Data phase from gadget, status phase from udc */
2085 if (setup
->bRequestType
& USB_DIR_IN
) {
2086 udc
->ep0_state
= DATA_STATE_XMIT
;
2087 udc
->ep0_dir
= USB_DIR_IN
;
2089 udc
->ep0_state
= DATA_STATE_RECV
;
2090 udc
->ep0_dir
= USB_DIR_OUT
;
2092 spin_unlock(&udc
->lock
);
2093 if (udc
->driver
->setup(&udc
->gadget
,
2094 &udc
->local_setup_buff
) < 0)
2096 spin_lock(&udc
->lock
);
2098 /* No data phase, IN status from gadget */
2099 udc
->ep0_dir
= USB_DIR_IN
;
2100 spin_unlock(&udc
->lock
);
2101 if (udc
->driver
->setup(&udc
->gadget
,
2102 &udc
->local_setup_buff
) < 0)
2104 spin_lock(&udc
->lock
);
2105 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
2109 /*-------------------------------------------------------------------------
2110 USB Interrupt handlers
2111 -------------------------------------------------------------------------*/
2112 static void suspend_irq(struct qe_udc
*udc
)
2114 udc
->resume_state
= udc
->usb_state
;
2115 udc
->usb_state
= USB_STATE_SUSPENDED
;
2117 /* report suspend to the driver ,serial.c not support this*/
2118 if (udc
->driver
->suspend
)
2119 udc
->driver
->suspend(&udc
->gadget
);
2122 static void resume_irq(struct qe_udc
*udc
)
2124 udc
->usb_state
= udc
->resume_state
;
2125 udc
->resume_state
= 0;
2127 /* report resume to the driver , serial.c not support this*/
2128 if (udc
->driver
->resume
)
2129 udc
->driver
->resume(&udc
->gadget
);
2132 static void idle_irq(struct qe_udc
*udc
)
2136 usbs
= in_8(&udc
->usb_regs
->usb_usbs
);
2137 if (usbs
& USB_IDLE_STATUS_MASK
) {
2138 if ((udc
->usb_state
) != USB_STATE_SUSPENDED
)
2141 if (udc
->usb_state
== USB_STATE_SUSPENDED
)
2146 static int reset_irq(struct qe_udc
*udc
)
2151 out_8(&udc
->usb_regs
->usb_usadr
, 0);
2153 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2154 if (udc
->eps
[i
].init
)
2155 qe_ep_reset(udc
, i
);
2159 udc
->usb_state
= USB_STATE_DEFAULT
;
2160 udc
->ep0_state
= WAIT_FOR_SETUP
;
2161 udc
->ep0_dir
= USB_DIR_OUT
;
2166 static int bsy_irq(struct qe_udc
*udc
)
2171 static int txe_irq(struct qe_udc
*udc
)
2176 /* ep0 tx interrupt also in here */
2177 static int tx_irq(struct qe_udc
*udc
)
2180 struct qe_bd __iomem
*bd
;
2183 if ((udc
->usb_state
== USB_STATE_ADDRESS
)
2184 && (in_8(&udc
->usb_regs
->usb_usadr
) == 0))
2185 out_8(&udc
->usb_regs
->usb_usadr
, udc
->device_address
);
2187 for (i
= (USB_MAX_ENDPOINTS
-1); ((i
>= 0) && (res
== 0)); i
--) {
2189 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_OUT
)) {
2191 if (!(in_be32((u32 __iomem
*)bd
) & T_R
)
2192 && (in_be32(&bd
->buf
))) {
2193 /* confirm the transmitted bd */
2195 res
= qe_ep0_txconf(ep
);
2197 res
= qe_ep_txconf(ep
);
2205 /* setup packect's rx is handle in the function too */
2206 static void rx_irq(struct qe_udc
*udc
)
2209 struct qe_bd __iomem
*bd
;
2212 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2214 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_IN
)) {
2216 if (!(in_be32((u32 __iomem
*)bd
) & R_E
)
2217 && (in_be32(&bd
->buf
))) {
2218 if (ep
->epnum
== 0) {
2221 /*non-setup package receive*/
2229 static irqreturn_t
qe_udc_irq(int irq
, void *_udc
)
2231 struct qe_udc
*udc
= (struct qe_udc
*)_udc
;
2233 irqreturn_t status
= IRQ_NONE
;
2234 unsigned long flags
;
2236 spin_lock_irqsave(&udc
->lock
, flags
);
2238 irq_src
= in_be16(&udc
->usb_regs
->usb_usber
) &
2239 in_be16(&udc
->usb_regs
->usb_usbmr
);
2240 /* Clear notification bits */
2241 out_be16(&udc
->usb_regs
->usb_usber
, irq_src
);
2243 if (irq_src
& USB_E_IDLE_MASK
) {
2245 irq_src
&= ~USB_E_IDLE_MASK
;
2246 status
= IRQ_HANDLED
;
2249 if (irq_src
& USB_E_TXB_MASK
) {
2251 irq_src
&= ~USB_E_TXB_MASK
;
2252 status
= IRQ_HANDLED
;
2255 if (irq_src
& USB_E_RXB_MASK
) {
2257 irq_src
&= ~USB_E_RXB_MASK
;
2258 status
= IRQ_HANDLED
;
2261 if (irq_src
& USB_E_RESET_MASK
) {
2263 irq_src
&= ~USB_E_RESET_MASK
;
2264 status
= IRQ_HANDLED
;
2267 if (irq_src
& USB_E_BSY_MASK
) {
2269 irq_src
&= ~USB_E_BSY_MASK
;
2270 status
= IRQ_HANDLED
;
2273 if (irq_src
& USB_E_TXE_MASK
) {
2275 irq_src
&= ~USB_E_TXE_MASK
;
2276 status
= IRQ_HANDLED
;
2279 spin_unlock_irqrestore(&udc
->lock
, flags
);
2284 /*-------------------------------------------------------------------------
2285 Gadget driver register and unregister.
2286 --------------------------------------------------------------------------*/
2287 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
2290 unsigned long flags
= 0;
2292 /* standard operations */
2293 if (!udc_controller
)
2296 if (!driver
|| (driver
->speed
!= USB_SPEED_FULL
2297 && driver
->speed
!= USB_SPEED_HIGH
)
2298 || !driver
->bind
|| !driver
->disconnect
2302 if (udc_controller
->driver
)
2305 /* lock is needed but whether should use this lock or another */
2306 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2308 driver
->driver
.bus
= NULL
;
2309 /* hook up the driver */
2310 udc_controller
->driver
= driver
;
2311 udc_controller
->gadget
.dev
.driver
= &driver
->driver
;
2312 udc_controller
->gadget
.speed
= (enum usb_device_speed
)(driver
->speed
);
2313 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2315 retval
= driver
->bind(&udc_controller
->gadget
);
2317 dev_err(udc_controller
->dev
, "bind to %s --> %d",
2318 driver
->driver
.name
, retval
);
2319 udc_controller
->gadget
.dev
.driver
= NULL
;
2320 udc_controller
->driver
= NULL
;
2324 /* Enable IRQ reg and Set usbcmd reg EN bit */
2327 out_be16(&udc_controller
->usb_regs
->usb_usber
, 0xffff);
2328 out_be16(&udc_controller
->usb_regs
->usb_usbmr
, USB_E_DEFAULT_DEVICE
);
2329 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2330 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2331 udc_controller
->ep0_dir
= USB_DIR_OUT
;
2332 dev_info(udc_controller
->dev
, "%s bind to driver %s \n",
2333 udc_controller
->gadget
.name
, driver
->driver
.name
);
2336 EXPORT_SYMBOL(usb_gadget_register_driver
);
2338 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
2340 struct qe_ep
*loop_ep
;
2341 unsigned long flags
;
2343 if (!udc_controller
)
2346 if (!driver
|| driver
!= udc_controller
->driver
)
2349 /* stop usb controller, disable intr */
2352 /* in fact, no needed */
2353 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2354 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2355 udc_controller
->ep0_dir
= 0;
2357 /* stand operation */
2358 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2359 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2360 nuke(&udc_controller
->eps
[0], -ESHUTDOWN
);
2361 list_for_each_entry(loop_ep
, &udc_controller
->gadget
.ep_list
,
2363 nuke(loop_ep
, -ESHUTDOWN
);
2364 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2366 /* unbind gadget and unhook driver. */
2367 driver
->unbind(&udc_controller
->gadget
);
2368 udc_controller
->gadget
.dev
.driver
= NULL
;
2369 udc_controller
->driver
= NULL
;
2371 dev_info(udc_controller
->dev
, "unregistered gadget driver '%s'\r\n",
2372 driver
->driver
.name
);
2375 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
2377 /* udc structure's alloc and setup, include ep-param alloc */
2378 static struct qe_udc __devinit
*qe_udc_config(struct of_device
*ofdev
)
2381 struct device_node
*np
= ofdev
->node
;
2382 unsigned int tmp_addr
= 0;
2383 struct usb_device_para __iomem
*usbpram
;
2388 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
2390 dev_err(&ofdev
->dev
, "malloc udc failed\n");
2394 udc
->dev
= &ofdev
->dev
;
2396 /* get default address of usb parameter in MURAM from device tree */
2397 offset
= *of_get_address(np
, 1, &size
, NULL
);
2398 udc
->usb_param
= cpm_muram_addr(offset
);
2399 memset_io(udc
->usb_param
, 0, size
);
2401 usbpram
= udc
->usb_param
;
2402 out_be16(&usbpram
->frame_n
, 0);
2403 out_be32(&usbpram
->rstate
, 0);
2405 tmp_addr
= cpm_muram_alloc((USB_MAX_ENDPOINTS
*
2406 sizeof(struct usb_ep_para
)),
2407 USB_EP_PARA_ALIGNMENT
);
2409 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2410 out_be16(&usbpram
->epptr
[i
], (u16
)tmp_addr
);
2411 udc
->ep_param
[i
] = cpm_muram_addr(tmp_addr
);
2415 memset_io(udc
->ep_param
[0], 0,
2416 USB_MAX_ENDPOINTS
* sizeof(struct usb_ep_para
));
2418 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2419 udc
->usb_state
= USB_STATE_POWERED
;
2422 spin_lock_init(&udc
->lock
);
2430 /* USB Controller register init */
2431 static int __devinit
qe_udc_reg_init(struct qe_udc
*udc
)
2433 struct usb_ctlr __iomem
*qe_usbregs
;
2434 qe_usbregs
= udc
->usb_regs
;
2436 /* Init the usb register */
2437 out_8(&qe_usbregs
->usb_usmod
, 0x01);
2438 out_be16(&qe_usbregs
->usb_usbmr
, 0);
2439 out_8(&qe_usbregs
->usb_uscom
, 0);
2440 out_be16(&qe_usbregs
->usb_usber
, USBER_ALL_CLEAR
);
2445 static int __devinit
qe_ep_config(struct qe_udc
*udc
, unsigned char pipe_num
)
2447 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
2450 strcpy(ep
->name
, ep_name
[pipe_num
]);
2451 ep
->ep
.name
= ep_name
[pipe_num
];
2453 ep
->ep
.ops
= &qe_ep_ops
;
2455 ep
->ep
.maxpacket
= (unsigned short) ~0;
2458 ep
->epnum
= (u8
)pipe_num
;
2465 ep
->state
= EP_STATE_IDLE
;
2468 /* the queue lists any req for this ep */
2469 INIT_LIST_HEAD(&ep
->queue
);
2471 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2473 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2475 ep
->gadget
= &udc
->gadget
;
2480 /*-----------------------------------------------------------------------
2481 * UDC device Driver operation functions *
2482 *----------------------------------------------------------------------*/
2483 static void qe_udc_release(struct device
*dev
)
2487 complete(udc_controller
->done
);
2488 cpm_muram_free(cpm_muram_offset(udc_controller
->ep_param
[0]));
2489 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
2490 udc_controller
->ep_param
[i
] = NULL
;
2492 kfree(udc_controller
);
2493 udc_controller
= NULL
;
2496 /* Driver probe functions */
2497 static int __devinit
qe_udc_probe(struct of_device
*ofdev
,
2498 const struct of_device_id
*match
)
2500 struct device_node
*np
= ofdev
->node
;
2502 unsigned int ret
= 0;
2506 prop
= of_get_property(np
, "mode", NULL
);
2507 if (!prop
|| strcmp(prop
, "peripheral"))
2510 /* Initialize the udc structure including QH member and other member */
2511 udc_controller
= qe_udc_config(ofdev
);
2512 if (!udc_controller
) {
2513 dev_dbg(&ofdev
->dev
, "udc_controll is NULL\n");
2517 udc_controller
->soc_type
= (unsigned long)match
->data
;
2518 udc_controller
->usb_regs
= of_iomap(np
, 0);
2519 if (!udc_controller
->usb_regs
) {
2524 /* initialize usb hw reg except for regs for EP,
2525 * leave usbintr reg untouched*/
2526 qe_udc_reg_init(udc_controller
);
2528 /* here comes the stand operations for probe
2529 * set the qe_udc->gadget.xxx */
2530 udc_controller
->gadget
.ops
= &qe_gadget_ops
;
2532 /* gadget.ep0 is a pointer */
2533 udc_controller
->gadget
.ep0
= &udc_controller
->eps
[0].ep
;
2535 INIT_LIST_HEAD(&udc_controller
->gadget
.ep_list
);
2537 /* modify in register gadget process */
2538 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2540 /* name: Identifies the controller hardware type. */
2541 udc_controller
->gadget
.name
= driver_name
;
2543 device_initialize(&udc_controller
->gadget
.dev
);
2545 strcpy(udc_controller
->gadget
.dev
.bus_id
, "gadget");
2547 udc_controller
->gadget
.dev
.release
= qe_udc_release
;
2548 udc_controller
->gadget
.dev
.parent
= &ofdev
->dev
;
2550 /* initialize qe_ep struct */
2551 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2552 /* because the ep type isn't decide here so
2553 * qe_ep_init() should be called in ep_enable() */
2555 /* setup the qe_ep struct and link ep.ep.list
2556 * into gadget.ep_list */
2557 qe_ep_config(udc_controller
, (unsigned char)i
);
2560 /* ep0 initialization in here */
2561 ret
= qe_ep_init(udc_controller
, 0, &qe_ep0_desc
);
2565 /* create a buf for ZLP send, need to remain zeroed */
2566 udc_controller
->nullbuf
= kzalloc(256, GFP_KERNEL
);
2567 if (udc_controller
->nullbuf
== NULL
) {
2568 dev_dbg(udc_controller
->dev
, "cannot alloc nullbuf\n");
2573 /* buffer for data of get_status request */
2574 udc_controller
->statusbuf
= kzalloc(2, GFP_KERNEL
);
2575 if (udc_controller
->statusbuf
== NULL
) {
2580 udc_controller
->nullp
= virt_to_phys((void *)udc_controller
->nullbuf
);
2581 if (udc_controller
->nullp
== DMA_ADDR_INVALID
) {
2582 udc_controller
->nullp
= dma_map_single(
2583 udc_controller
->gadget
.dev
.parent
,
2584 udc_controller
->nullbuf
,
2587 udc_controller
->nullmap
= 1;
2589 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
2590 udc_controller
->nullp
, 256,
2594 tasklet_init(&udc_controller
->rx_tasklet
, ep_rx_tasklet
,
2595 (unsigned long)udc_controller
);
2596 /* request irq and disable DR */
2597 udc_controller
->usb_irq
= irq_of_parse_and_map(np
, 0);
2599 ret
= request_irq(udc_controller
->usb_irq
, qe_udc_irq
, 0,
2600 driver_name
, udc_controller
);
2602 dev_err(udc_controller
->dev
, "cannot request irq %d err %d \n",
2603 udc_controller
->usb_irq
, ret
);
2607 ret
= device_add(&udc_controller
->gadget
.dev
);
2611 dev_info(udc_controller
->dev
,
2612 "%s USB controller initialized as device\n",
2613 (udc_controller
->soc_type
== PORT_QE
) ? "QE" : "CPM");
2617 free_irq(udc_controller
->usb_irq
, udc_controller
);
2619 if (udc_controller
->nullmap
) {
2620 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2621 udc_controller
->nullp
, 256,
2623 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2625 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2626 udc_controller
->nullp
, 256,
2629 kfree(udc_controller
->statusbuf
);
2631 kfree(udc_controller
->nullbuf
);
2633 ep
= &udc_controller
->eps
[0];
2634 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2636 kfree(ep
->rxbuffer
);
2639 iounmap(udc_controller
->usb_regs
);
2641 kfree(udc_controller
);
2647 static int qe_udc_suspend(struct of_device
*dev
, pm_message_t state
)
2652 static int qe_udc_resume(struct of_device
*dev
)
2658 static int __devexit
qe_udc_remove(struct of_device
*ofdev
)
2663 DECLARE_COMPLETION(done
);
2665 if (!udc_controller
)
2668 udc_controller
->done
= &done
;
2669 tasklet_disable(&udc_controller
->rx_tasklet
);
2671 if (udc_controller
->nullmap
) {
2672 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2673 udc_controller
->nullp
, 256,
2675 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2677 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2678 udc_controller
->nullp
, 256,
2681 kfree(udc_controller
->statusbuf
);
2682 kfree(udc_controller
->nullbuf
);
2684 ep
= &udc_controller
->eps
[0];
2685 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2686 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (USB_BDRING_LEN
+ 1);
2690 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2693 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
2695 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2700 kfree(ep
->rxbuffer
);
2703 free_irq(udc_controller
->usb_irq
, udc_controller
);
2705 tasklet_kill(&udc_controller
->rx_tasklet
);
2707 iounmap(udc_controller
->usb_regs
);
2709 device_unregister(&udc_controller
->gadget
.dev
);
2710 /* wait for release() of gadget.dev to free udc */
2711 wait_for_completion(&done
);
2716 /*-------------------------------------------------------------------------*/
2717 static struct of_device_id __devinitdata qe_udc_match
[] = {
2719 .compatible
= "fsl,mpc8360-qe-usb",
2720 .data
= (void *)PORT_QE
,
2723 .compatible
= "fsl,mpc8272-cpm-usb",
2724 .data
= (void *)PORT_CPM
,
2729 MODULE_DEVICE_TABLE(of
, qe_udc_match
);
2731 static struct of_platform_driver udc_driver
= {
2732 .name
= (char *)driver_name
,
2733 .match_table
= qe_udc_match
,
2734 .probe
= qe_udc_probe
,
2735 .remove
= __devexit_p(qe_udc_remove
),
2737 .suspend
= qe_udc_suspend
,
2738 .resume
= qe_udc_resume
,
2742 static int __init
qe_udc_init(void)
2744 printk(KERN_INFO
"%s: %s, %s\n", driver_name
, driver_desc
,
2746 return of_register_platform_driver(&udc_driver
);
2749 static void __exit
qe_udc_exit(void)
2751 of_unregister_platform_driver(&udc_driver
);
2754 module_init(qe_udc_init
);
2755 module_exit(qe_udc_exit
);
2757 MODULE_DESCRIPTION(DRIVER_DESC
);
2758 MODULE_AUTHOR(DRIVER_AUTHOR
);
2759 MODULE_LICENSE("GPL");