2 * driver/usb/gadget/fsl_qe_udc.c
4 * Copyright (c) 2006-2008 Freescale Semiconductor, Inc. All rights reserved.
6 * Xie Xiaobo <X.Xie@freescale.com>
7 * Li Yang <leoli@freescale.com>
8 * Based on bareboard code from Shlomi Gridish.
11 * Freescle QE/CPM USB Pheripheral Controller Driver
12 * The controller can be found on MPC8360, MPC8272, and etc.
13 * MPC8360 Rev 1.1 may need QE mircocode update
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/ioport.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/err.h>
30 #include <linux/slab.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
34 #include <linux/moduleparam.h>
35 #include <linux/of_address.h>
36 #include <linux/of_platform.h>
37 #include <linux/dma-mapping.h>
38 #include <linux/usb/ch9.h>
39 #include <linux/usb/gadget.h>
40 #include <linux/usb/otg.h>
45 #include "fsl_qe_udc.h"
47 #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver"
48 #define DRIVER_AUTHOR "Xie XiaoBo"
49 #define DRIVER_VERSION "1.0"
51 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
53 static const char driver_name
[] = "fsl_qe_udc";
54 static const char driver_desc
[] = DRIVER_DESC
;
56 /*ep name is important in gadget, it should obey the convention of ep_match()*/
57 static const char *const ep_name
[] = {
58 "ep0-control", /* everyone has ep0 */
59 /* 3 configurable endpoints */
65 static struct usb_endpoint_descriptor qe_ep0_desc
= {
66 .bLength
= USB_DT_ENDPOINT_SIZE
,
67 .bDescriptorType
= USB_DT_ENDPOINT
,
69 .bEndpointAddress
= 0,
70 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
71 .wMaxPacketSize
= USB_MAX_CTRL_PAYLOAD
,
74 /* it is initialized in probe() */
75 static struct qe_udc
*udc_controller
;
77 /********************************************************************
78 * Internal Used Function Start
79 ********************************************************************/
80 /*-----------------------------------------------------------------
81 * done() - retire a request; caller blocked irqs
82 *--------------------------------------------------------------*/
83 static void done(struct qe_ep
*ep
, struct qe_req
*req
, int status
)
85 struct qe_udc
*udc
= ep
->udc
;
86 unsigned char stopped
= ep
->stopped
;
88 /* the req->queue pointer is used by ep_queue() func, in which
89 * the request will be added into a udc_ep->queue 'd tail
90 * so here the req will be dropped from the ep->queue
92 list_del_init(&req
->queue
);
94 /* req.status should be set as -EINPROGRESS in ep_queue() */
95 if (req
->req
.status
== -EINPROGRESS
)
96 req
->req
.status
= status
;
98 status
= req
->req
.status
;
101 dma_unmap_single(udc
->gadget
.dev
.parent
,
102 req
->req
.dma
, req
->req
.length
,
106 req
->req
.dma
= DMA_ADDR_INVALID
;
109 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
110 req
->req
.dma
, req
->req
.length
,
115 if (status
&& (status
!= -ESHUTDOWN
))
116 dev_vdbg(udc
->dev
, "complete %s req %p stat %d len %u/%u\n",
117 ep
->ep
.name
, &req
->req
, status
,
118 req
->req
.actual
, req
->req
.length
);
120 /* don't modify queue heads during completion callback */
122 spin_unlock(&udc
->lock
);
124 /* this complete() should a func implemented by gadget layer,
125 * eg fsg->bulk_in_complete() */
126 if (req
->req
.complete
)
127 req
->req
.complete(&ep
->ep
, &req
->req
);
129 spin_lock(&udc
->lock
);
131 ep
->stopped
= stopped
;
134 /*-----------------------------------------------------------------
135 * nuke(): delete all requests related to this ep
136 *--------------------------------------------------------------*/
137 static void nuke(struct qe_ep
*ep
, int status
)
139 /* Whether this eq has request linked */
140 while (!list_empty(&ep
->queue
)) {
141 struct qe_req
*req
= NULL
;
142 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
144 done(ep
, req
, status
);
148 /*---------------------------------------------------------------------------*
149 * USB and Endpoint manipulate process, include parameter and register *
150 *---------------------------------------------------------------------------*/
151 /* @value: 1--set stall 0--clean stall */
152 static int qe_eprx_stall_change(struct qe_ep
*ep
, int value
)
155 u8 epnum
= ep
->epnum
;
156 struct qe_udc
*udc
= ep
->udc
;
158 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
159 tem_usep
= tem_usep
& ~USB_RHS_MASK
;
161 tem_usep
|= USB_RHS_STALL
;
162 else if (ep
->dir
== USB_DIR_IN
)
163 tem_usep
|= USB_RHS_IGNORE_OUT
;
165 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
169 static int qe_eptx_stall_change(struct qe_ep
*ep
, int value
)
172 u8 epnum
= ep
->epnum
;
173 struct qe_udc
*udc
= ep
->udc
;
175 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
176 tem_usep
= tem_usep
& ~USB_THS_MASK
;
178 tem_usep
|= USB_THS_STALL
;
179 else if (ep
->dir
== USB_DIR_OUT
)
180 tem_usep
|= USB_THS_IGNORE_IN
;
182 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
187 static int qe_ep0_stall(struct qe_udc
*udc
)
189 qe_eptx_stall_change(&udc
->eps
[0], 1);
190 qe_eprx_stall_change(&udc
->eps
[0], 1);
191 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
192 udc_controller
->ep0_dir
= 0;
196 static int qe_eprx_nack(struct qe_ep
*ep
)
198 u8 epnum
= ep
->epnum
;
199 struct qe_udc
*udc
= ep
->udc
;
201 if (ep
->state
== EP_STATE_IDLE
) {
202 /* Set the ep's nack */
203 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[epnum
],
204 USB_RHS_MASK
, USB_RHS_NACK
);
206 /* Mask Rx and Busy interrupts */
207 clrbits16(&udc
->usb_regs
->usb_usbmr
,
208 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
210 ep
->state
= EP_STATE_NACK
;
215 static int qe_eprx_normal(struct qe_ep
*ep
)
217 struct qe_udc
*udc
= ep
->udc
;
219 if (ep
->state
== EP_STATE_NACK
) {
220 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[ep
->epnum
],
221 USB_RTHS_MASK
, USB_THS_IGNORE_IN
);
223 /* Unmask RX interrupts */
224 out_be16(&udc
->usb_regs
->usb_usber
,
225 USB_E_BSY_MASK
| USB_E_RXB_MASK
);
226 setbits16(&udc
->usb_regs
->usb_usbmr
,
227 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
229 ep
->state
= EP_STATE_IDLE
;
236 static int qe_ep_cmd_stoptx(struct qe_ep
*ep
)
238 if (ep
->udc
->soc_type
== PORT_CPM
)
239 cpm_command(CPM_USB_STOP_TX
| (ep
->epnum
<< CPM_USB_EP_SHIFT
),
240 CPM_USB_STOP_TX_OPCODE
);
242 qe_issue_cmd(QE_USB_STOP_TX
, QE_CR_SUBBLOCK_USB
,
248 static int qe_ep_cmd_restarttx(struct qe_ep
*ep
)
250 if (ep
->udc
->soc_type
== PORT_CPM
)
251 cpm_command(CPM_USB_RESTART_TX
| (ep
->epnum
<<
252 CPM_USB_EP_SHIFT
), CPM_USB_RESTART_TX_OPCODE
);
254 qe_issue_cmd(QE_USB_RESTART_TX
, QE_CR_SUBBLOCK_USB
,
260 static int qe_ep_flushtxfifo(struct qe_ep
*ep
)
262 struct qe_udc
*udc
= ep
->udc
;
267 qe_ep_cmd_stoptx(ep
);
268 out_8(&udc
->usb_regs
->usb_uscom
,
269 USB_CMD_FLUSH_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
270 out_be16(&udc
->ep_param
[i
]->tbptr
, in_be16(&udc
->ep_param
[i
]->tbase
));
271 out_be32(&udc
->ep_param
[i
]->tstate
, 0);
272 out_be16(&udc
->ep_param
[i
]->tbcnt
, 0);
274 ep
->c_txbd
= ep
->txbase
;
275 ep
->n_txbd
= ep
->txbase
;
276 qe_ep_cmd_restarttx(ep
);
280 static int qe_ep_filltxfifo(struct qe_ep
*ep
)
282 struct qe_udc
*udc
= ep
->udc
;
284 out_8(&udc
->usb_regs
->usb_uscom
,
285 USB_CMD_STR_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
289 static int qe_epbds_reset(struct qe_udc
*udc
, int pipe_num
)
293 struct qe_bd __iomem
*bd
;
296 ep
= &udc
->eps
[pipe_num
];
298 if (ep
->dir
== USB_DIR_OUT
)
299 bdring_len
= USB_BDRING_LEN_RX
;
301 bdring_len
= USB_BDRING_LEN
;
304 for (i
= 0; i
< (bdring_len
- 1); i
++) {
305 out_be32((u32 __iomem
*)bd
, R_E
| R_I
);
308 out_be32((u32 __iomem
*)bd
, R_E
| R_I
| R_W
);
311 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
312 out_be32(&bd
->buf
, 0);
313 out_be32((u32 __iomem
*)bd
, 0);
316 out_be32((u32 __iomem
*)bd
, T_W
);
321 static int qe_ep_reset(struct qe_udc
*udc
, int pipe_num
)
326 ep
= &udc
->eps
[pipe_num
];
327 tmpusep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe_num
]);
328 tmpusep
&= ~USB_RTHS_MASK
;
332 qe_ep_flushtxfifo(ep
);
335 tmpusep
|= USB_THS_IGNORE_IN
;
338 qe_ep_flushtxfifo(ep
);
339 tmpusep
|= USB_RHS_IGNORE_OUT
;
344 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], tmpusep
);
346 qe_epbds_reset(udc
, pipe_num
);
351 static int qe_ep_toggledata01(struct qe_ep
*ep
)
357 static int qe_ep_bd_init(struct qe_udc
*udc
, unsigned char pipe_num
)
359 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
360 unsigned long tmp_addr
= 0;
361 struct usb_ep_para __iomem
*epparam
;
363 struct qe_bd __iomem
*bd
;
366 if (ep
->dir
== USB_DIR_OUT
)
367 bdring_len
= USB_BDRING_LEN_RX
;
369 bdring_len
= USB_BDRING_LEN
;
371 epparam
= udc
->ep_param
[pipe_num
];
372 /* alloc multi-ram for BD rings and set the ep parameters */
373 tmp_addr
= cpm_muram_alloc(sizeof(struct qe_bd
) * (bdring_len
+
374 USB_BDRING_LEN_TX
), QE_ALIGNMENT_OF_BD
);
375 if (IS_ERR_VALUE(tmp_addr
))
378 out_be16(&epparam
->rbase
, (u16
)tmp_addr
);
379 out_be16(&epparam
->tbase
, (u16
)(tmp_addr
+
380 (sizeof(struct qe_bd
) * bdring_len
)));
382 out_be16(&epparam
->rbptr
, in_be16(&epparam
->rbase
));
383 out_be16(&epparam
->tbptr
, in_be16(&epparam
->tbase
));
385 ep
->rxbase
= cpm_muram_addr(tmp_addr
);
386 ep
->txbase
= cpm_muram_addr(tmp_addr
+ (sizeof(struct qe_bd
)
388 ep
->n_rxbd
= ep
->rxbase
;
389 ep
->e_rxbd
= ep
->rxbase
;
390 ep
->n_txbd
= ep
->txbase
;
391 ep
->c_txbd
= ep
->txbase
;
392 ep
->data01
= 0; /* data0 */
394 /* Init TX and RX bds */
396 for (i
= 0; i
< bdring_len
- 1; i
++) {
397 out_be32(&bd
->buf
, 0);
398 out_be32((u32 __iomem
*)bd
, 0);
401 out_be32(&bd
->buf
, 0);
402 out_be32((u32 __iomem
*)bd
, R_W
);
405 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
406 out_be32(&bd
->buf
, 0);
407 out_be32((u32 __iomem
*)bd
, 0);
410 out_be32(&bd
->buf
, 0);
411 out_be32((u32 __iomem
*)bd
, T_W
);
416 static int qe_ep_rxbd_update(struct qe_ep
*ep
)
421 struct qe_bd __iomem
*bd
;
422 unsigned int bdring_len
;
424 if (ep
->rxbase
== NULL
)
429 ep
->rxframe
= kmalloc(sizeof(*ep
->rxframe
), GFP_ATOMIC
);
430 if (ep
->rxframe
== NULL
) {
431 dev_err(ep
->udc
->dev
, "malloc rxframe failed\n");
435 qe_frame_init(ep
->rxframe
);
437 if (ep
->dir
== USB_DIR_OUT
)
438 bdring_len
= USB_BDRING_LEN_RX
;
440 bdring_len
= USB_BDRING_LEN
;
442 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (bdring_len
+ 1);
443 ep
->rxbuffer
= kzalloc(size
, GFP_ATOMIC
);
444 if (ep
->rxbuffer
== NULL
) {
445 dev_err(ep
->udc
->dev
, "malloc rxbuffer failed,size=%d\n",
451 ep
->rxbuf_d
= virt_to_phys((void *)ep
->rxbuffer
);
452 if (ep
->rxbuf_d
== DMA_ADDR_INVALID
) {
453 ep
->rxbuf_d
= dma_map_single(udc_controller
->gadget
.dev
.parent
,
459 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
465 size
= ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2;
467 tmp
= (u32
)(((tmp
>> 2) << 2) + 4);
469 for (i
= 0; i
< bdring_len
- 1; i
++) {
470 out_be32(&bd
->buf
, tmp
);
471 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
));
475 out_be32(&bd
->buf
, tmp
);
476 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
| R_W
));
481 static int qe_ep_register_init(struct qe_udc
*udc
, unsigned char pipe_num
)
483 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
484 struct usb_ep_para __iomem
*epparam
;
489 epparam
= udc
->ep_param
[pipe_num
];
492 logepnum
= (ep
->desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
493 usep
|= (logepnum
<< USB_EPNUM_SHIFT
);
495 switch (ep
->desc
->bmAttributes
& 0x03) {
496 case USB_ENDPOINT_XFER_BULK
:
497 usep
|= USB_TRANS_BULK
;
499 case USB_ENDPOINT_XFER_ISOC
:
500 usep
|= USB_TRANS_ISO
;
502 case USB_ENDPOINT_XFER_INT
:
503 usep
|= USB_TRANS_INT
;
506 usep
|= USB_TRANS_CTR
;
512 usep
|= USB_THS_IGNORE_IN
;
515 usep
|= USB_RHS_IGNORE_OUT
;
520 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], usep
);
523 out_8(&epparam
->rbmr
, rtfcr
);
524 out_8(&epparam
->tbmr
, rtfcr
);
526 tmp
= (u16
)(ep
->ep
.maxpacket
+ USB_CRC_SIZE
);
527 /* MRBLR must be divisble by 4 */
528 tmp
= (u16
)(((tmp
>> 2) << 2) + 4);
529 out_be16(&epparam
->mrblr
, tmp
);
534 static int qe_ep_init(struct qe_udc
*udc
,
535 unsigned char pipe_num
,
536 const struct usb_endpoint_descriptor
*desc
)
538 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
543 max
= le16_to_cpu(desc
->wMaxPacketSize
);
545 /* check the max package size validate for this endpoint */
546 /* Refer to USB2.0 spec table 9-13,
549 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
550 case USB_ENDPOINT_XFER_BULK
:
551 if (strstr(ep
->ep
.name
, "-iso")
552 || strstr(ep
->ep
.name
, "-int"))
554 switch (udc
->gadget
.speed
) {
556 if ((max
== 128) || (max
== 256) || (max
== 512))
572 case USB_ENDPOINT_XFER_INT
:
573 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
575 switch (udc
->gadget
.speed
) {
588 case USB_ENDPOINT_XFER_ISOC
:
589 if (strstr(ep
->ep
.name
, "-bulk")
590 || strstr(ep
->ep
.name
, "-int"))
592 switch (udc
->gadget
.speed
) {
603 case USB_ENDPOINT_XFER_CONTROL
:
604 if (strstr(ep
->ep
.name
, "-iso")
605 || strstr(ep
->ep
.name
, "-int"))
607 switch (udc
->gadget
.speed
) {
642 spin_lock_irqsave(&udc
->lock
, flags
);
644 /* initialize ep structure */
645 ep
->ep
.maxpacket
= max
;
646 ep
->tm
= (u8
)(desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
652 ep
->dir
= USB_DIR_BOTH
;
653 udc
->ep0_dir
= USB_DIR_OUT
;
654 udc
->ep0_state
= WAIT_FOR_SETUP
;
656 switch (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) {
658 ep
->dir
= USB_DIR_OUT
;
661 ep
->dir
= USB_DIR_IN
;
667 /* hardware special operation */
668 qe_ep_bd_init(udc
, pipe_num
);
669 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_OUT
)) {
670 reval
= qe_ep_rxbd_update(ep
);
675 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_IN
)) {
676 ep
->txframe
= kmalloc(sizeof(*ep
->txframe
), GFP_ATOMIC
);
677 if (ep
->txframe
== NULL
) {
678 dev_err(udc
->dev
, "malloc txframe failed\n");
681 qe_frame_init(ep
->txframe
);
684 qe_ep_register_init(udc
, pipe_num
);
686 /* Now HW will be NAKing transfers to that EP,
687 * until a buffer is queued to it. */
688 spin_unlock_irqrestore(&udc
->lock
, flags
);
695 spin_unlock_irqrestore(&udc
->lock
, flags
);
697 dev_err(udc
->dev
, "failed to initialize %s\n", ep
->ep
.name
);
701 static inline void qe_usb_enable(void)
703 setbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
706 static inline void qe_usb_disable(void)
708 clrbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
711 /*----------------------------------------------------------------------------*
712 * USB and EP basic manipulate function end *
713 *----------------------------------------------------------------------------*/
716 /******************************************************************************
717 UDC transmit and receive process
718 ******************************************************************************/
719 static void recycle_one_rxbd(struct qe_ep
*ep
)
723 bdstatus
= in_be32((u32 __iomem
*)ep
->e_rxbd
);
724 bdstatus
= R_I
| R_E
| (bdstatus
& R_W
);
725 out_be32((u32 __iomem
*)ep
->e_rxbd
, bdstatus
);
728 ep
->e_rxbd
= ep
->rxbase
;
733 static void recycle_rxbds(struct qe_ep
*ep
, unsigned char stopatnext
)
736 struct qe_bd __iomem
*bd
, *nextbd
;
737 unsigned char stop
= 0;
741 bdstatus
= in_be32((u32 __iomem
*)bd
);
743 while (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
) && !stop
) {
744 bdstatus
= R_E
| R_I
| (bdstatus
& R_W
);
745 out_be32((u32 __iomem
*)bd
, bdstatus
);
752 bdstatus
= in_be32((u32 __iomem
*)bd
);
753 if (stopatnext
&& (bd
== nextbd
))
760 static void ep_recycle_rxbds(struct qe_ep
*ep
)
762 struct qe_bd __iomem
*bd
= ep
->n_rxbd
;
764 u8 epnum
= ep
->epnum
;
765 struct qe_udc
*udc
= ep
->udc
;
767 bdstatus
= in_be32((u32 __iomem
*)bd
);
768 if (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
)) {
770 ((in_be16(&udc
->ep_param
[epnum
]->rbptr
) -
771 in_be16(&udc
->ep_param
[epnum
]->rbase
))
773 bdstatus
= in_be32((u32 __iomem
*)bd
);
781 recycle_rxbds(ep
, 0);
782 ep
->e_rxbd
= ep
->n_rxbd
;
784 recycle_rxbds(ep
, 1);
786 if (in_be16(&udc
->usb_regs
->usb_usber
) & USB_E_BSY_MASK
)
787 out_be16(&udc
->usb_regs
->usb_usber
, USB_E_BSY_MASK
);
789 if (ep
->has_data
<= 0 && (!list_empty(&ep
->queue
)))
795 static void setup_received_handle(struct qe_udc
*udc
,
796 struct usb_ctrlrequest
*setup
);
797 static int qe_ep_rxframe_handle(struct qe_ep
*ep
);
798 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
);
799 /* when BD PID is setup, handle the packet */
800 static int ep0_setup_handle(struct qe_udc
*udc
)
802 struct qe_ep
*ep
= &udc
->eps
[0];
803 struct qe_frame
*pframe
;
807 pframe
= ep
->rxframe
;
808 if ((frame_get_info(pframe
) & PID_SETUP
)
809 && (udc
->ep0_state
== WAIT_FOR_SETUP
)) {
810 fsize
= frame_get_length(pframe
);
811 if (unlikely(fsize
!= 8))
813 cp
= (u8
*)&udc
->local_setup_buff
;
814 memcpy(cp
, pframe
->data
, fsize
);
817 /* handle the usb command base on the usb_ctrlrequest */
818 setup_received_handle(udc
, &udc
->local_setup_buff
);
824 static int qe_ep0_rx(struct qe_udc
*udc
)
826 struct qe_ep
*ep
= &udc
->eps
[0];
827 struct qe_frame
*pframe
;
828 struct qe_bd __iomem
*bd
;
829 u32 bdstatus
, length
;
832 pframe
= ep
->rxframe
;
834 if (ep
->dir
== USB_DIR_IN
) {
835 dev_err(udc
->dev
, "ep0 not a control endpoint\n");
840 bdstatus
= in_be32((u32 __iomem
*)bd
);
841 length
= bdstatus
& BD_LENGTH_MASK
;
843 while (!(bdstatus
& R_E
) && length
) {
844 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
845 && !(bdstatus
& R_ERROR
)) {
846 if (length
== USB_CRC_SIZE
) {
847 udc
->ep0_state
= WAIT_FOR_SETUP
;
849 "receive a ZLP in status phase\n");
851 qe_frame_clean(pframe
);
852 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
853 frame_set_data(pframe
, (u8
*)vaddr
);
854 frame_set_length(pframe
,
855 (length
- USB_CRC_SIZE
));
856 frame_set_status(pframe
, FRAME_OK
);
857 switch (bdstatus
& R_PID
) {
859 frame_set_info(pframe
, PID_SETUP
);
862 frame_set_info(pframe
, PID_DATA1
);
865 frame_set_info(pframe
, PID_DATA0
);
869 if ((bdstatus
& R_PID
) == R_PID_SETUP
)
870 ep0_setup_handle(udc
);
872 qe_ep_rxframe_handle(ep
);
875 dev_err(udc
->dev
, "The receive frame with error!\n");
878 /* note: don't clear the rxbd's buffer address */
879 recycle_one_rxbd(ep
);
887 bdstatus
= in_be32((u32 __iomem
*)bd
);
888 length
= bdstatus
& BD_LENGTH_MASK
;
897 static int qe_ep_rxframe_handle(struct qe_ep
*ep
)
899 struct qe_frame
*pframe
;
905 pframe
= ep
->rxframe
;
907 if (frame_get_info(pframe
) & PID_DATA1
)
910 if (framepid
!= ep
->data01
) {
911 dev_err(ep
->udc
->dev
, "the data01 error!\n");
915 fsize
= frame_get_length(pframe
);
916 if (list_empty(&ep
->queue
)) {
917 dev_err(ep
->udc
->dev
, "the %s have no requeue!\n", ep
->name
);
919 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
921 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
923 memcpy(cp
, pframe
->data
, fsize
);
924 req
->req
.actual
+= fsize
;
925 if ((fsize
< ep
->ep
.maxpacket
) ||
926 (req
->req
.actual
>= req
->req
.length
)) {
928 ep0_req_complete(ep
->udc
, req
);
931 if (list_empty(&ep
->queue
) && ep
->epnum
!= 0)
937 qe_ep_toggledata01(ep
);
942 static void ep_rx_tasklet(unsigned long data
)
944 struct qe_udc
*udc
= (struct qe_udc
*)data
;
946 struct qe_frame
*pframe
;
947 struct qe_bd __iomem
*bd
;
949 u32 bdstatus
, length
;
952 spin_lock_irqsave(&udc
->lock
, flags
);
954 for (i
= 1; i
< USB_MAX_ENDPOINTS
; i
++) {
957 if (ep
->dir
== USB_DIR_IN
|| ep
->enable_tasklet
== 0) {
959 "This is a transmit ep or disable tasklet!\n");
963 pframe
= ep
->rxframe
;
965 bdstatus
= in_be32((u32 __iomem
*)bd
);
966 length
= bdstatus
& BD_LENGTH_MASK
;
968 while (!(bdstatus
& R_E
) && length
) {
969 if (list_empty(&ep
->queue
)) {
972 "The rxep have noreq %d\n",
977 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
978 && !(bdstatus
& R_ERROR
)) {
979 qe_frame_clean(pframe
);
980 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
981 frame_set_data(pframe
, (u8
*)vaddr
);
982 frame_set_length(pframe
,
983 (length
- USB_CRC_SIZE
));
984 frame_set_status(pframe
, FRAME_OK
);
985 switch (bdstatus
& R_PID
) {
987 frame_set_info(pframe
, PID_DATA1
);
990 frame_set_info(pframe
, PID_SETUP
);
993 frame_set_info(pframe
, PID_DATA0
);
996 /* handle the rx frame */
997 qe_ep_rxframe_handle(ep
);
1000 "error in received frame\n");
1002 /* note: don't clear the rxbd's buffer address */
1003 /*clear the length */
1004 out_be32((u32 __iomem
*)bd
, bdstatus
& BD_STATUS_MASK
);
1006 if (!(ep
->localnack
))
1007 recycle_one_rxbd(ep
);
1015 bdstatus
= in_be32((u32 __iomem
*)bd
);
1016 length
= bdstatus
& BD_LENGTH_MASK
;
1022 ep_recycle_rxbds(ep
);
1024 ep
->enable_tasklet
= 0;
1027 spin_unlock_irqrestore(&udc
->lock
, flags
);
1030 static int qe_ep_rx(struct qe_ep
*ep
)
1033 struct qe_frame
*pframe
;
1034 struct qe_bd __iomem
*bd
;
1035 u16 swoffs
, ucoffs
, emptybds
;
1038 pframe
= ep
->rxframe
;
1040 if (ep
->dir
== USB_DIR_IN
) {
1041 dev_err(udc
->dev
, "transmit ep in rx function\n");
1047 swoffs
= (u16
)(bd
- ep
->rxbase
);
1048 ucoffs
= (u16
)((in_be16(&udc
->ep_param
[ep
->epnum
]->rbptr
) -
1049 in_be16(&udc
->ep_param
[ep
->epnum
]->rbase
)) >> 3);
1050 if (swoffs
< ucoffs
)
1051 emptybds
= USB_BDRING_LEN_RX
- ucoffs
+ swoffs
;
1053 emptybds
= swoffs
- ucoffs
;
1055 if (emptybds
< MIN_EMPTY_BDS
) {
1058 dev_vdbg(udc
->dev
, "%d empty bds, send NACK\n", emptybds
);
1060 ep
->has_data
= USB_BDRING_LEN_RX
- emptybds
;
1062 if (list_empty(&ep
->queue
)) {
1064 dev_vdbg(udc
->dev
, "The rxep have no req queued with %d BDs\n",
1069 tasklet_schedule(&udc
->rx_tasklet
);
1070 ep
->enable_tasklet
= 1;
1075 /* send data from a frame, no matter what tx_req */
1076 static int qe_ep_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1078 struct qe_udc
*udc
= ep
->udc
;
1079 struct qe_bd __iomem
*bd
;
1081 u32 bdstatus
, pidmask
;
1084 if (ep
->dir
== USB_DIR_OUT
) {
1085 dev_err(udc
->dev
, "receive ep passed to tx function\n");
1089 /* Disable the Tx interrupt */
1090 saveusbmr
= in_be16(&udc
->usb_regs
->usb_usbmr
);
1091 out_be16(&udc
->usb_regs
->usb_usbmr
,
1092 saveusbmr
& ~(USB_E_TXB_MASK
| USB_E_TXE_MASK
));
1095 bdstatus
= in_be32((u32 __iomem
*)bd
);
1097 if (!(bdstatus
& (T_R
| BD_LENGTH_MASK
))) {
1098 if (frame_get_length(frame
) == 0) {
1099 frame_set_data(frame
, udc
->nullbuf
);
1100 frame_set_length(frame
, 2);
1101 frame
->info
|= (ZLP
| NO_CRC
);
1102 dev_vdbg(udc
->dev
, "the frame size = 0\n");
1104 paddr
= virt_to_phys((void *)frame
->data
);
1105 out_be32(&bd
->buf
, paddr
);
1106 bdstatus
= (bdstatus
&T_W
);
1107 if (!(frame_get_info(frame
) & NO_CRC
))
1108 bdstatus
|= T_R
| T_I
| T_L
| T_TC
1109 | frame_get_length(frame
);
1111 bdstatus
|= T_R
| T_I
| T_L
| frame_get_length(frame
);
1113 /* if the packet is a ZLP in status phase */
1114 if ((ep
->epnum
== 0) && (udc
->ep0_state
== DATA_STATE_NEED_ZLP
))
1118 pidmask
= T_PID_DATA1
;
1119 frame
->info
|= PID_DATA1
;
1121 pidmask
= T_PID_DATA0
;
1122 frame
->info
|= PID_DATA0
;
1125 bdstatus
|= pidmask
;
1126 out_be32((u32 __iomem
*)bd
, bdstatus
);
1127 qe_ep_filltxfifo(ep
);
1129 /* enable the TX interrupt */
1130 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1132 qe_ep_toggledata01(ep
);
1134 ep
->n_txbd
= ep
->txbase
;
1140 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1141 dev_vdbg(udc
->dev
, "The tx bd is not ready!\n");
1146 /* when a bd was transmitted, the function can
1147 * handle the tx_req, not include ep0 */
1148 static int txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1150 if (ep
->tx_req
!= NULL
) {
1152 int asent
= ep
->last
;
1159 /* a request already were transmitted completely */
1160 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1161 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1162 done(ep
, ep
->tx_req
, 0);
1169 /* we should gain a new tx_req fot this endpoint */
1170 if (ep
->tx_req
== NULL
) {
1171 if (!list_empty(&ep
->queue
)) {
1172 ep
->tx_req
= list_entry(ep
->queue
.next
, struct qe_req
,
1182 /* give a frame and a tx_req, send some data */
1183 static int qe_usb_senddata(struct qe_ep
*ep
, struct qe_frame
*frame
)
1188 qe_frame_clean(frame
);
1189 size
= min_t(u32
, (ep
->tx_req
->req
.length
- ep
->sent
),
1191 buf
= (u8
*)ep
->tx_req
->req
.buf
+ ep
->sent
;
1194 frame_set_data(frame
, buf
);
1195 frame_set_length(frame
, size
);
1196 frame_set_status(frame
, FRAME_OK
);
1197 frame_set_info(frame
, 0);
1198 return qe_ep_tx(ep
, frame
);
1203 /* give a frame struct,send a ZLP */
1204 static int sendnulldata(struct qe_ep
*ep
, struct qe_frame
*frame
, uint infor
)
1206 struct qe_udc
*udc
= ep
->udc
;
1211 qe_frame_clean(frame
);
1212 frame_set_data(frame
, (u8
*)udc
->nullbuf
);
1213 frame_set_length(frame
, 2);
1214 frame_set_status(frame
, FRAME_OK
);
1215 frame_set_info(frame
, (ZLP
| NO_CRC
| infor
));
1217 return qe_ep_tx(ep
, frame
);
1220 static int frame_create_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1222 struct qe_req
*req
= ep
->tx_req
;
1228 if ((req
->req
.length
- ep
->sent
) > 0)
1229 reval
= qe_usb_senddata(ep
, frame
);
1231 reval
= sendnulldata(ep
, frame
, 0);
1236 /* if direction is DIR_IN, the status is Device->Host
1237 * if direction is DIR_OUT, the status transaction is Device<-Host
1238 * in status phase, udc create a request and gain status */
1239 static int ep0_prime_status(struct qe_udc
*udc
, int direction
)
1242 struct qe_ep
*ep
= &udc
->eps
[0];
1244 if (direction
== USB_DIR_IN
) {
1245 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
1246 udc
->ep0_dir
= USB_DIR_IN
;
1247 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1249 udc
->ep0_dir
= USB_DIR_OUT
;
1250 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1256 /* a request complete in ep0, whether gadget request or udc request */
1257 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
)
1259 struct qe_ep
*ep
= &udc
->eps
[0];
1260 /* because usb and ep's status already been set in ch9setaddress() */
1262 switch (udc
->ep0_state
) {
1263 case DATA_STATE_XMIT
:
1265 /* receive status phase */
1266 if (ep0_prime_status(udc
, USB_DIR_OUT
))
1270 case DATA_STATE_NEED_ZLP
:
1272 udc
->ep0_state
= WAIT_FOR_SETUP
;
1275 case DATA_STATE_RECV
:
1277 /* send status phase */
1278 if (ep0_prime_status(udc
, USB_DIR_IN
))
1282 case WAIT_FOR_OUT_STATUS
:
1284 udc
->ep0_state
= WAIT_FOR_SETUP
;
1287 case WAIT_FOR_SETUP
:
1288 dev_vdbg(udc
->dev
, "Unexpected interrupt\n");
1297 static int ep0_txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1299 struct qe_req
*tx_req
= NULL
;
1300 struct qe_frame
*frame
= ep
->txframe
;
1302 if ((frame_get_info(frame
) & (ZLP
| NO_REQ
)) == (ZLP
| NO_REQ
)) {
1304 ep
->udc
->ep0_state
= WAIT_FOR_SETUP
;
1306 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1310 tx_req
= ep
->tx_req
;
1311 if (tx_req
!= NULL
) {
1313 int asent
= ep
->last
;
1320 /* a request already were transmitted completely */
1321 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1322 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1323 ep0_req_complete(ep
->udc
, ep
->tx_req
);
1329 dev_vdbg(ep
->udc
->dev
, "the ep0_controller have no req\n");
1335 static int ep0_txframe_handle(struct qe_ep
*ep
)
1337 /* if have error, transmit again */
1338 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1339 qe_ep_flushtxfifo(ep
);
1340 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1341 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1346 ep0_txcomplete(ep
, 1);
1348 ep0_txcomplete(ep
, 0);
1350 frame_create_tx(ep
, ep
->txframe
);
1354 static int qe_ep0_txconf(struct qe_ep
*ep
)
1356 struct qe_bd __iomem
*bd
;
1357 struct qe_frame
*pframe
;
1361 bdstatus
= in_be32((u32 __iomem
*)bd
);
1362 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1363 pframe
= ep
->txframe
;
1365 /* clear and recycle the BD */
1366 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1367 out_be32(&bd
->buf
, 0);
1369 ep
->c_txbd
= ep
->txbase
;
1373 if (ep
->c_txbd
== ep
->n_txbd
) {
1374 if (bdstatus
& DEVICE_T_ERROR
) {
1375 frame_set_status(pframe
, FRAME_ERROR
);
1376 if (bdstatus
& T_TO
)
1377 pframe
->status
|= TX_ER_TIMEOUT
;
1378 if (bdstatus
& T_UN
)
1379 pframe
->status
|= TX_ER_UNDERUN
;
1381 ep0_txframe_handle(ep
);
1385 bdstatus
= in_be32((u32 __iomem
*)bd
);
1391 static int ep_txframe_handle(struct qe_ep
*ep
)
1393 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1394 qe_ep_flushtxfifo(ep
);
1395 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1396 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1405 frame_create_tx(ep
, ep
->txframe
); /* send the data */
1409 /* confirm the already trainsmited bd */
1410 static int qe_ep_txconf(struct qe_ep
*ep
)
1412 struct qe_bd __iomem
*bd
;
1413 struct qe_frame
*pframe
= NULL
;
1415 unsigned char breakonrxinterrupt
= 0;
1418 bdstatus
= in_be32((u32 __iomem
*)bd
);
1419 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1420 pframe
= ep
->txframe
;
1421 if (bdstatus
& DEVICE_T_ERROR
) {
1422 frame_set_status(pframe
, FRAME_ERROR
);
1423 if (bdstatus
& T_TO
)
1424 pframe
->status
|= TX_ER_TIMEOUT
;
1425 if (bdstatus
& T_UN
)
1426 pframe
->status
|= TX_ER_UNDERUN
;
1429 /* clear and recycle the BD */
1430 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1431 out_be32(&bd
->buf
, 0);
1433 ep
->c_txbd
= ep
->txbase
;
1437 /* handle the tx frame */
1438 ep_txframe_handle(ep
);
1440 bdstatus
= in_be32((u32 __iomem
*)bd
);
1442 if (breakonrxinterrupt
)
1448 /* Add a request in queue, and try to transmit a packet */
1449 static int ep_req_send(struct qe_ep
*ep
, struct qe_req
*req
)
1453 if (ep
->tx_req
== NULL
) {
1456 txcomplete(ep
, 0); /* can gain a new tx_req */
1457 reval
= frame_create_tx(ep
, ep
->txframe
);
1462 /* Maybe this is a good ideal */
1463 static int ep_req_rx(struct qe_ep
*ep
, struct qe_req
*req
)
1465 struct qe_udc
*udc
= ep
->udc
;
1466 struct qe_frame
*pframe
= NULL
;
1467 struct qe_bd __iomem
*bd
;
1468 u32 bdstatus
, length
;
1474 if (list_empty(&ep
->queue
)) {
1475 dev_vdbg(udc
->dev
, "the req already finish!\n");
1478 pframe
= ep
->rxframe
;
1481 bdstatus
= in_be32((u32 __iomem
*)bd
);
1482 length
= bdstatus
& BD_LENGTH_MASK
;
1484 while (!(bdstatus
& R_E
) && length
) {
1487 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
1488 && !(bdstatus
& R_ERROR
)) {
1489 qe_frame_clean(pframe
);
1490 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
1491 frame_set_data(pframe
, (u8
*)vaddr
);
1492 frame_set_length(pframe
, (length
- USB_CRC_SIZE
));
1493 frame_set_status(pframe
, FRAME_OK
);
1494 switch (bdstatus
& R_PID
) {
1496 frame_set_info(pframe
, PID_DATA1
); break;
1498 frame_set_info(pframe
, PID_DATA0
); break;
1500 /* handle the rx frame */
1502 if (frame_get_info(pframe
) & PID_DATA1
)
1507 if (framepid
!= ep
->data01
) {
1508 dev_vdbg(udc
->dev
, "the data01 error!\n");
1510 fsize
= frame_get_length(pframe
);
1512 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
1514 memcpy(cp
, pframe
->data
, fsize
);
1515 req
->req
.actual
+= fsize
;
1516 if ((fsize
< ep
->ep
.maxpacket
)
1517 || (req
->req
.actual
>=
1521 if (list_empty(&ep
->queue
))
1525 qe_ep_toggledata01(ep
);
1528 dev_err(udc
->dev
, "The receive frame with error!\n");
1531 /* note: don't clear the rxbd's buffer address *
1532 * only Clear the length */
1533 out_be32((u32 __iomem
*)bd
, (bdstatus
& BD_STATUS_MASK
));
1542 bdstatus
= in_be32((u32 __iomem
*)bd
);
1543 length
= bdstatus
& BD_LENGTH_MASK
;
1547 ep_recycle_rxbds(ep
);
1552 /* only add the request in queue */
1553 static int ep_req_receive(struct qe_ep
*ep
, struct qe_req
*req
)
1555 if (ep
->state
== EP_STATE_NACK
) {
1556 if (ep
->has_data
<= 0) {
1557 /* Enable rx and unmask rx interrupt */
1560 /* Copy the exist BD data */
1568 /********************************************************************
1569 Internal Used Function End
1570 ********************************************************************/
1572 /*-----------------------------------------------------------------------
1573 Endpoint Management Functions For Gadget
1574 -----------------------------------------------------------------------*/
1575 static int qe_ep_enable(struct usb_ep
*_ep
,
1576 const struct usb_endpoint_descriptor
*desc
)
1581 unsigned char epnum
;
1583 ep
= container_of(_ep
, struct qe_ep
, ep
);
1585 /* catch various bogus parameters */
1586 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep_name
[0] ||
1587 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
))
1591 if (!udc
->driver
|| (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
))
1594 epnum
= (u8
)desc
->bEndpointAddress
& 0xF;
1596 retval
= qe_ep_init(udc
, epnum
, desc
);
1598 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1599 dev_dbg(udc
->dev
, "enable ep%d failed\n", ep
->epnum
);
1602 dev_dbg(udc
->dev
, "enable ep%d successful\n", ep
->epnum
);
1606 static int qe_ep_disable(struct usb_ep
*_ep
)
1610 unsigned long flags
;
1613 ep
= container_of(_ep
, struct qe_ep
, ep
);
1616 if (!_ep
|| !ep
->desc
) {
1617 dev_dbg(udc
->dev
, "%s not enabled\n", _ep
? ep
->ep
.name
: NULL
);
1621 spin_lock_irqsave(&udc
->lock
, flags
);
1622 /* Nuke all pending requests (does flush) */
1623 nuke(ep
, -ESHUTDOWN
);
1627 qe_ep_reset(udc
, ep
->epnum
);
1628 spin_unlock_irqrestore(&udc
->lock
, flags
);
1630 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1632 if (ep
->dir
== USB_DIR_OUT
)
1633 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1634 (USB_BDRING_LEN_RX
+ 1);
1636 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1637 (USB_BDRING_LEN
+ 1);
1639 if (ep
->dir
!= USB_DIR_IN
) {
1642 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
1645 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
1647 dma_sync_single_for_cpu(
1648 udc_controller
->gadget
.dev
.parent
,
1652 kfree(ep
->rxbuffer
);
1655 if (ep
->dir
!= USB_DIR_OUT
)
1658 dev_dbg(udc
->dev
, "disabled %s OK\n", _ep
->name
);
1662 static struct usb_request
*qe_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
1666 req
= kzalloc(sizeof(*req
), gfp_flags
);
1670 req
->req
.dma
= DMA_ADDR_INVALID
;
1672 INIT_LIST_HEAD(&req
->queue
);
1677 static void qe_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1681 req
= container_of(_req
, struct qe_req
, req
);
1687 static int __qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1689 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1690 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
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
);
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
);
1754 /* queues (submits) an I/O request to an endpoint */
1755 static int qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1758 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1759 struct qe_udc
*udc
= ep
->udc
;
1760 unsigned long flags
;
1763 spin_lock_irqsave(&udc
->lock
, flags
);
1764 ret
= __qe_ep_queue(_ep
, _req
);
1765 spin_unlock_irqrestore(&udc
->lock
, flags
);
1769 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1770 static int qe_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1772 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1774 unsigned long flags
;
1779 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1781 /* make sure it's actually queued on this endpoint */
1782 list_for_each_entry(req
, &ep
->queue
, queue
) {
1783 if (&req
->req
== _req
)
1787 if (&req
->req
!= _req
) {
1788 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1792 done(ep
, req
, -ECONNRESET
);
1794 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1798 /*-----------------------------------------------------------------
1799 * modify the endpoint halt feature
1800 * @ep: the non-isochronous endpoint being stalled
1801 * @value: 1--set halt 0--clear halt
1802 * Returns zero, or a negative error code.
1803 *----------------------------------------------------------------*/
1804 static int qe_ep_set_halt(struct usb_ep
*_ep
, int value
)
1807 unsigned long flags
;
1808 int status
= -EOPNOTSUPP
;
1811 ep
= container_of(_ep
, struct qe_ep
, ep
);
1812 if (!_ep
|| !ep
->desc
) {
1818 /* Attempt to halt IN ep will fail if any transfer requests
1819 * are still queue */
1820 if (value
&& ep_is_in(ep
) && !list_empty(&ep
->queue
)) {
1826 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1827 qe_eptx_stall_change(ep
, value
);
1828 qe_eprx_stall_change(ep
, value
);
1829 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1831 if (ep
->epnum
== 0) {
1832 udc
->ep0_state
= WAIT_FOR_SETUP
;
1836 /* set data toggle to DATA0 on clear halt */
1840 dev_vdbg(udc
->dev
, "%s %s halt stat %d\n", ep
->ep
.name
,
1841 value
? "set" : "clear", status
);
1846 static struct usb_ep_ops qe_ep_ops
= {
1847 .enable
= qe_ep_enable
,
1848 .disable
= qe_ep_disable
,
1850 .alloc_request
= qe_alloc_request
,
1851 .free_request
= qe_free_request
,
1853 .queue
= qe_ep_queue
,
1854 .dequeue
= qe_ep_dequeue
,
1856 .set_halt
= qe_ep_set_halt
,
1859 /*------------------------------------------------------------------------
1860 Gadget Driver Layer Operations
1861 ------------------------------------------------------------------------*/
1863 /* Get the current frame number */
1864 static int qe_get_frame(struct usb_gadget
*gadget
)
1868 tmp
= in_be16(&udc_controller
->usb_param
->frame_n
);
1877 /* Tries to wake up the host connected to this gadget
1879 * Return : 0-success
1880 * Negative-this feature not enabled by host or not supported by device hw
1882 static int qe_wakeup(struct usb_gadget
*gadget
)
1887 /* Notify controller that VBUS is powered, Called by whatever
1888 detects VBUS sessions */
1889 static int qe_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1894 /* constrain controller's VBUS power usage
1895 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1896 * reporting how much power the device may consume. For example, this
1897 * could affect how quickly batteries are recharged.
1899 * Returns zero on success, else negative errno.
1901 static int qe_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1906 /* Change Data+ pullup status
1907 * this func is used by usb_gadget_connect/disconnect
1909 static int qe_pullup(struct usb_gadget
*gadget
, int is_on
)
1914 /* defined in usb_gadget.h */
1915 static struct usb_gadget_ops qe_gadget_ops
= {
1916 .get_frame
= qe_get_frame
,
1917 .wakeup
= qe_wakeup
,
1918 /* .set_selfpowered = qe_set_selfpowered,*/ /* always selfpowered */
1919 .vbus_session
= qe_vbus_session
,
1920 .vbus_draw
= qe_vbus_draw
,
1921 .pullup
= qe_pullup
,
1924 /*-------------------------------------------------------------------------
1925 USB ep0 Setup process in BUS Enumeration
1926 -------------------------------------------------------------------------*/
1927 static int udc_reset_ep_queue(struct qe_udc
*udc
, u8 pipe
)
1929 struct qe_ep
*ep
= &udc
->eps
[pipe
];
1931 nuke(ep
, -ECONNRESET
);
1936 static int reset_queues(struct qe_udc
*udc
)
1940 for (pipe
= 0; pipe
< USB_MAX_ENDPOINTS
; pipe
++)
1941 udc_reset_ep_queue(udc
, pipe
);
1943 /* report disconnect; the driver is already quiesced */
1944 spin_unlock(&udc
->lock
);
1945 udc
->driver
->disconnect(&udc
->gadget
);
1946 spin_lock(&udc
->lock
);
1951 static void ch9setaddress(struct qe_udc
*udc
, u16 value
, u16 index
,
1954 /* Save the new address to device struct */
1955 udc
->device_address
= (u8
) value
;
1956 /* Update usb state */
1957 udc
->usb_state
= USB_STATE_ADDRESS
;
1959 /* Status phase , send a ZLP */
1960 if (ep0_prime_status(udc
, USB_DIR_IN
))
1964 static void ownercomplete(struct usb_ep
*_ep
, struct usb_request
*_req
)
1966 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1968 req
->req
.buf
= NULL
;
1972 static void ch9getstatus(struct qe_udc
*udc
, u8 request_type
, u16 value
,
1973 u16 index
, u16 length
)
1981 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1982 /* Get device status */
1983 usb_status
= 1 << USB_DEVICE_SELF_POWERED
;
1984 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
1985 /* Get interface status */
1986 /* We don't have interface information in udc driver */
1988 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
1989 /* Get endpoint status */
1990 int pipe
= index
& USB_ENDPOINT_NUMBER_MASK
;
1991 struct qe_ep
*target_ep
= &udc
->eps
[pipe
];
1994 /* stall if endpoint doesn't exist */
1995 if (!target_ep
->desc
)
1998 usep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe
]);
1999 if (index
& USB_DIR_IN
) {
2000 if (target_ep
->dir
!= USB_DIR_IN
)
2002 if ((usep
& USB_THS_MASK
) == USB_THS_STALL
)
2003 usb_status
= 1 << USB_ENDPOINT_HALT
;
2005 if (target_ep
->dir
!= USB_DIR_OUT
)
2007 if ((usep
& USB_RHS_MASK
) == USB_RHS_STALL
)
2008 usb_status
= 1 << USB_ENDPOINT_HALT
;
2012 req
= container_of(qe_alloc_request(&ep
->ep
, GFP_KERNEL
),
2013 struct qe_req
, req
);
2014 req
->req
.length
= 2;
2015 req
->req
.buf
= udc
->statusbuf
;
2016 *(u16
*)req
->req
.buf
= cpu_to_le16(usb_status
);
2017 req
->req
.status
= -EINPROGRESS
;
2018 req
->req
.actual
= 0;
2019 req
->req
.complete
= ownercomplete
;
2021 udc
->ep0_dir
= USB_DIR_IN
;
2024 status
= __qe_ep_queue(&ep
->ep
, &req
->req
);
2029 dev_err(udc
->dev
, "Can't respond to getstatus request \n");
2033 /* only handle the setup request, suppose the device in normal status */
2034 static void setup_received_handle(struct qe_udc
*udc
,
2035 struct usb_ctrlrequest
*setup
)
2037 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2038 u16 wValue
= le16_to_cpu(setup
->wValue
);
2039 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
2040 u16 wLength
= le16_to_cpu(setup
->wLength
);
2042 /* clear the previous request in the ep0 */
2043 udc_reset_ep_queue(udc
, 0);
2045 if (setup
->bRequestType
& USB_DIR_IN
)
2046 udc
->ep0_dir
= USB_DIR_IN
;
2048 udc
->ep0_dir
= USB_DIR_OUT
;
2050 switch (setup
->bRequest
) {
2051 case USB_REQ_GET_STATUS
:
2052 /* Data+Status phase form udc */
2053 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
2054 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
2056 ch9getstatus(udc
, setup
->bRequestType
, wValue
, wIndex
,
2060 case USB_REQ_SET_ADDRESS
:
2061 /* Status phase from udc */
2062 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
2065 ch9setaddress(udc
, wValue
, wIndex
, wLength
);
2068 case USB_REQ_CLEAR_FEATURE
:
2069 case USB_REQ_SET_FEATURE
:
2070 /* Requests with no data phase, status phase from udc */
2071 if ((setup
->bRequestType
& USB_TYPE_MASK
)
2072 != USB_TYPE_STANDARD
)
2075 if ((setup
->bRequestType
& USB_RECIP_MASK
)
2076 == USB_RECIP_ENDPOINT
) {
2077 int pipe
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2080 if (wValue
!= 0 || wLength
!= 0
2081 || pipe
> USB_MAX_ENDPOINTS
)
2083 ep
= &udc
->eps
[pipe
];
2085 spin_unlock(&udc
->lock
);
2086 qe_ep_set_halt(&ep
->ep
,
2087 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2089 spin_lock(&udc
->lock
);
2092 ep0_prime_status(udc
, USB_DIR_IN
);
2101 /* Data phase from gadget, status phase from udc */
2102 if (setup
->bRequestType
& USB_DIR_IN
) {
2103 udc
->ep0_state
= DATA_STATE_XMIT
;
2104 udc
->ep0_dir
= USB_DIR_IN
;
2106 udc
->ep0_state
= DATA_STATE_RECV
;
2107 udc
->ep0_dir
= USB_DIR_OUT
;
2109 spin_unlock(&udc
->lock
);
2110 if (udc
->driver
->setup(&udc
->gadget
,
2111 &udc
->local_setup_buff
) < 0)
2113 spin_lock(&udc
->lock
);
2115 /* No data phase, IN status from gadget */
2116 udc
->ep0_dir
= USB_DIR_IN
;
2117 spin_unlock(&udc
->lock
);
2118 if (udc
->driver
->setup(&udc
->gadget
,
2119 &udc
->local_setup_buff
) < 0)
2121 spin_lock(&udc
->lock
);
2122 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
2126 /*-------------------------------------------------------------------------
2127 USB Interrupt handlers
2128 -------------------------------------------------------------------------*/
2129 static void suspend_irq(struct qe_udc
*udc
)
2131 udc
->resume_state
= udc
->usb_state
;
2132 udc
->usb_state
= USB_STATE_SUSPENDED
;
2134 /* report suspend to the driver ,serial.c not support this*/
2135 if (udc
->driver
->suspend
)
2136 udc
->driver
->suspend(&udc
->gadget
);
2139 static void resume_irq(struct qe_udc
*udc
)
2141 udc
->usb_state
= udc
->resume_state
;
2142 udc
->resume_state
= 0;
2144 /* report resume to the driver , serial.c not support this*/
2145 if (udc
->driver
->resume
)
2146 udc
->driver
->resume(&udc
->gadget
);
2149 static void idle_irq(struct qe_udc
*udc
)
2153 usbs
= in_8(&udc
->usb_regs
->usb_usbs
);
2154 if (usbs
& USB_IDLE_STATUS_MASK
) {
2155 if ((udc
->usb_state
) != USB_STATE_SUSPENDED
)
2158 if (udc
->usb_state
== USB_STATE_SUSPENDED
)
2163 static int reset_irq(struct qe_udc
*udc
)
2167 if (udc
->usb_state
== USB_STATE_DEFAULT
)
2171 out_8(&udc
->usb_regs
->usb_usadr
, 0);
2173 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2174 if (udc
->eps
[i
].init
)
2175 qe_ep_reset(udc
, i
);
2179 udc
->usb_state
= USB_STATE_DEFAULT
;
2180 udc
->ep0_state
= WAIT_FOR_SETUP
;
2181 udc
->ep0_dir
= USB_DIR_OUT
;
2186 static int bsy_irq(struct qe_udc
*udc
)
2191 static int txe_irq(struct qe_udc
*udc
)
2196 /* ep0 tx interrupt also in here */
2197 static int tx_irq(struct qe_udc
*udc
)
2200 struct qe_bd __iomem
*bd
;
2203 if ((udc
->usb_state
== USB_STATE_ADDRESS
)
2204 && (in_8(&udc
->usb_regs
->usb_usadr
) == 0))
2205 out_8(&udc
->usb_regs
->usb_usadr
, udc
->device_address
);
2207 for (i
= (USB_MAX_ENDPOINTS
-1); ((i
>= 0) && (res
== 0)); i
--) {
2209 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_OUT
)) {
2211 if (!(in_be32((u32 __iomem
*)bd
) & T_R
)
2212 && (in_be32(&bd
->buf
))) {
2213 /* confirm the transmitted bd */
2215 res
= qe_ep0_txconf(ep
);
2217 res
= qe_ep_txconf(ep
);
2225 /* setup packect's rx is handle in the function too */
2226 static void rx_irq(struct qe_udc
*udc
)
2229 struct qe_bd __iomem
*bd
;
2232 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2234 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_IN
)) {
2236 if (!(in_be32((u32 __iomem
*)bd
) & R_E
)
2237 && (in_be32(&bd
->buf
))) {
2238 if (ep
->epnum
== 0) {
2241 /*non-setup package receive*/
2249 static irqreturn_t
qe_udc_irq(int irq
, void *_udc
)
2251 struct qe_udc
*udc
= (struct qe_udc
*)_udc
;
2253 irqreturn_t status
= IRQ_NONE
;
2254 unsigned long flags
;
2256 spin_lock_irqsave(&udc
->lock
, flags
);
2258 irq_src
= in_be16(&udc
->usb_regs
->usb_usber
) &
2259 in_be16(&udc
->usb_regs
->usb_usbmr
);
2260 /* Clear notification bits */
2261 out_be16(&udc
->usb_regs
->usb_usber
, irq_src
);
2263 if (irq_src
& USB_E_IDLE_MASK
) {
2265 irq_src
&= ~USB_E_IDLE_MASK
;
2266 status
= IRQ_HANDLED
;
2269 if (irq_src
& USB_E_TXB_MASK
) {
2271 irq_src
&= ~USB_E_TXB_MASK
;
2272 status
= IRQ_HANDLED
;
2275 if (irq_src
& USB_E_RXB_MASK
) {
2277 irq_src
&= ~USB_E_RXB_MASK
;
2278 status
= IRQ_HANDLED
;
2281 if (irq_src
& USB_E_RESET_MASK
) {
2283 irq_src
&= ~USB_E_RESET_MASK
;
2284 status
= IRQ_HANDLED
;
2287 if (irq_src
& USB_E_BSY_MASK
) {
2289 irq_src
&= ~USB_E_BSY_MASK
;
2290 status
= IRQ_HANDLED
;
2293 if (irq_src
& USB_E_TXE_MASK
) {
2295 irq_src
&= ~USB_E_TXE_MASK
;
2296 status
= IRQ_HANDLED
;
2299 spin_unlock_irqrestore(&udc
->lock
, flags
);
2304 /*-------------------------------------------------------------------------
2305 Gadget driver probe and unregister.
2306 --------------------------------------------------------------------------*/
2307 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
,
2308 int (*bind
)(struct usb_gadget
*))
2311 unsigned long flags
= 0;
2313 /* standard operations */
2314 if (!udc_controller
)
2317 if (!driver
|| (driver
->speed
!= USB_SPEED_FULL
2318 && driver
->speed
!= USB_SPEED_HIGH
)
2319 || !bind
|| !driver
->disconnect
|| !driver
->setup
)
2322 if (udc_controller
->driver
)
2325 /* lock is needed but whether should use this lock or another */
2326 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2328 driver
->driver
.bus
= NULL
;
2329 /* hook up the driver */
2330 udc_controller
->driver
= driver
;
2331 udc_controller
->gadget
.dev
.driver
= &driver
->driver
;
2332 udc_controller
->gadget
.speed
= (enum usb_device_speed
)(driver
->speed
);
2333 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2335 retval
= bind(&udc_controller
->gadget
);
2337 dev_err(udc_controller
->dev
, "bind to %s --> %d",
2338 driver
->driver
.name
, retval
);
2339 udc_controller
->gadget
.dev
.driver
= NULL
;
2340 udc_controller
->driver
= NULL
;
2344 /* Enable IRQ reg and Set usbcmd reg EN bit */
2347 out_be16(&udc_controller
->usb_regs
->usb_usber
, 0xffff);
2348 out_be16(&udc_controller
->usb_regs
->usb_usbmr
, USB_E_DEFAULT_DEVICE
);
2349 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2350 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2351 udc_controller
->ep0_dir
= USB_DIR_OUT
;
2352 dev_info(udc_controller
->dev
, "%s bind to driver %s \n",
2353 udc_controller
->gadget
.name
, driver
->driver
.name
);
2356 EXPORT_SYMBOL(usb_gadget_probe_driver
);
2358 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
2360 struct qe_ep
*loop_ep
;
2361 unsigned long flags
;
2363 if (!udc_controller
)
2366 if (!driver
|| driver
!= udc_controller
->driver
)
2369 /* stop usb controller, disable intr */
2372 /* in fact, no needed */
2373 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2374 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2375 udc_controller
->ep0_dir
= 0;
2377 /* stand operation */
2378 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2379 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2380 nuke(&udc_controller
->eps
[0], -ESHUTDOWN
);
2381 list_for_each_entry(loop_ep
, &udc_controller
->gadget
.ep_list
,
2383 nuke(loop_ep
, -ESHUTDOWN
);
2384 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2386 /* report disconnect; the controller is already quiesced */
2387 driver
->disconnect(&udc_controller
->gadget
);
2389 /* unbind gadget and unhook driver. */
2390 driver
->unbind(&udc_controller
->gadget
);
2391 udc_controller
->gadget
.dev
.driver
= NULL
;
2392 udc_controller
->driver
= NULL
;
2394 dev_info(udc_controller
->dev
, "unregistered gadget driver '%s'\r\n",
2395 driver
->driver
.name
);
2398 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
2400 /* udc structure's alloc and setup, include ep-param alloc */
2401 static struct qe_udc __devinit
*qe_udc_config(struct platform_device
*ofdev
)
2404 struct device_node
*np
= ofdev
->dev
.of_node
;
2405 unsigned int tmp_addr
= 0;
2406 struct usb_device_para __iomem
*usbpram
;
2411 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
2413 dev_err(&ofdev
->dev
, "malloc udc failed\n");
2417 udc
->dev
= &ofdev
->dev
;
2419 /* get default address of usb parameter in MURAM from device tree */
2420 offset
= *of_get_address(np
, 1, &size
, NULL
);
2421 udc
->usb_param
= cpm_muram_addr(offset
);
2422 memset_io(udc
->usb_param
, 0, size
);
2424 usbpram
= udc
->usb_param
;
2425 out_be16(&usbpram
->frame_n
, 0);
2426 out_be32(&usbpram
->rstate
, 0);
2428 tmp_addr
= cpm_muram_alloc((USB_MAX_ENDPOINTS
*
2429 sizeof(struct usb_ep_para
)),
2430 USB_EP_PARA_ALIGNMENT
);
2431 if (IS_ERR_VALUE(tmp_addr
))
2434 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2435 out_be16(&usbpram
->epptr
[i
], (u16
)tmp_addr
);
2436 udc
->ep_param
[i
] = cpm_muram_addr(tmp_addr
);
2440 memset_io(udc
->ep_param
[0], 0,
2441 USB_MAX_ENDPOINTS
* sizeof(struct usb_ep_para
));
2443 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2444 udc
->usb_state
= USB_STATE_POWERED
;
2447 spin_lock_init(&udc
->lock
);
2455 /* USB Controller register init */
2456 static int __devinit
qe_udc_reg_init(struct qe_udc
*udc
)
2458 struct usb_ctlr __iomem
*qe_usbregs
;
2459 qe_usbregs
= udc
->usb_regs
;
2461 /* Spec says that we must enable the USB controller to change mode. */
2462 out_8(&qe_usbregs
->usb_usmod
, 0x01);
2463 /* Mode changed, now disable it, since muram isn't initialized yet. */
2464 out_8(&qe_usbregs
->usb_usmod
, 0x00);
2466 /* Initialize the rest. */
2467 out_be16(&qe_usbregs
->usb_usbmr
, 0);
2468 out_8(&qe_usbregs
->usb_uscom
, 0);
2469 out_be16(&qe_usbregs
->usb_usber
, USBER_ALL_CLEAR
);
2474 static int __devinit
qe_ep_config(struct qe_udc
*udc
, unsigned char pipe_num
)
2476 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
2479 strcpy(ep
->name
, ep_name
[pipe_num
]);
2480 ep
->ep
.name
= ep_name
[pipe_num
];
2482 ep
->ep
.ops
= &qe_ep_ops
;
2484 ep
->ep
.maxpacket
= (unsigned short) ~0;
2487 ep
->epnum
= (u8
)pipe_num
;
2494 ep
->state
= EP_STATE_IDLE
;
2497 /* the queue lists any req for this ep */
2498 INIT_LIST_HEAD(&ep
->queue
);
2500 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2502 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2504 ep
->gadget
= &udc
->gadget
;
2509 /*-----------------------------------------------------------------------
2510 * UDC device Driver operation functions *
2511 *----------------------------------------------------------------------*/
2512 static void qe_udc_release(struct device
*dev
)
2516 complete(udc_controller
->done
);
2517 cpm_muram_free(cpm_muram_offset(udc_controller
->ep_param
[0]));
2518 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
2519 udc_controller
->ep_param
[i
] = NULL
;
2521 kfree(udc_controller
);
2522 udc_controller
= NULL
;
2525 /* Driver probe functions */
2526 static int __devinit
qe_udc_probe(struct platform_device
*ofdev
,
2527 const struct of_device_id
*match
)
2529 struct device_node
*np
= ofdev
->dev
.of_node
;
2531 unsigned int ret
= 0;
2535 prop
= of_get_property(np
, "mode", NULL
);
2536 if (!prop
|| strcmp(prop
, "peripheral"))
2539 /* Initialize the udc structure including QH member and other member */
2540 udc_controller
= qe_udc_config(ofdev
);
2541 if (!udc_controller
) {
2542 dev_err(&ofdev
->dev
, "failed to initialize\n");
2546 udc_controller
->soc_type
= (unsigned long)match
->data
;
2547 udc_controller
->usb_regs
= of_iomap(np
, 0);
2548 if (!udc_controller
->usb_regs
) {
2553 /* initialize usb hw reg except for regs for EP,
2554 * leave usbintr reg untouched*/
2555 qe_udc_reg_init(udc_controller
);
2557 /* here comes the stand operations for probe
2558 * set the qe_udc->gadget.xxx */
2559 udc_controller
->gadget
.ops
= &qe_gadget_ops
;
2561 /* gadget.ep0 is a pointer */
2562 udc_controller
->gadget
.ep0
= &udc_controller
->eps
[0].ep
;
2564 INIT_LIST_HEAD(&udc_controller
->gadget
.ep_list
);
2566 /* modify in register gadget process */
2567 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2569 /* name: Identifies the controller hardware type. */
2570 udc_controller
->gadget
.name
= driver_name
;
2572 device_initialize(&udc_controller
->gadget
.dev
);
2574 dev_set_name(&udc_controller
->gadget
.dev
, "gadget");
2576 udc_controller
->gadget
.dev
.release
= qe_udc_release
;
2577 udc_controller
->gadget
.dev
.parent
= &ofdev
->dev
;
2579 /* initialize qe_ep struct */
2580 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2581 /* because the ep type isn't decide here so
2582 * qe_ep_init() should be called in ep_enable() */
2584 /* setup the qe_ep struct and link ep.ep.list
2585 * into gadget.ep_list */
2586 qe_ep_config(udc_controller
, (unsigned char)i
);
2589 /* ep0 initialization in here */
2590 ret
= qe_ep_init(udc_controller
, 0, &qe_ep0_desc
);
2594 /* create a buf for ZLP send, need to remain zeroed */
2595 udc_controller
->nullbuf
= kzalloc(256, GFP_KERNEL
);
2596 if (udc_controller
->nullbuf
== NULL
) {
2597 dev_err(udc_controller
->dev
, "cannot alloc nullbuf\n");
2602 /* buffer for data of get_status request */
2603 udc_controller
->statusbuf
= kzalloc(2, GFP_KERNEL
);
2604 if (udc_controller
->statusbuf
== NULL
) {
2609 udc_controller
->nullp
= virt_to_phys((void *)udc_controller
->nullbuf
);
2610 if (udc_controller
->nullp
== DMA_ADDR_INVALID
) {
2611 udc_controller
->nullp
= dma_map_single(
2612 udc_controller
->gadget
.dev
.parent
,
2613 udc_controller
->nullbuf
,
2616 udc_controller
->nullmap
= 1;
2618 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
2619 udc_controller
->nullp
, 256,
2623 tasklet_init(&udc_controller
->rx_tasklet
, ep_rx_tasklet
,
2624 (unsigned long)udc_controller
);
2625 /* request irq and disable DR */
2626 udc_controller
->usb_irq
= irq_of_parse_and_map(np
, 0);
2627 if (!udc_controller
->usb_irq
) {
2632 ret
= request_irq(udc_controller
->usb_irq
, qe_udc_irq
, 0,
2633 driver_name
, udc_controller
);
2635 dev_err(udc_controller
->dev
, "cannot request irq %d err %d \n",
2636 udc_controller
->usb_irq
, ret
);
2640 ret
= device_add(&udc_controller
->gadget
.dev
);
2644 dev_info(udc_controller
->dev
,
2645 "%s USB controller initialized as device\n",
2646 (udc_controller
->soc_type
== PORT_QE
) ? "QE" : "CPM");
2650 free_irq(udc_controller
->usb_irq
, udc_controller
);
2652 irq_dispose_mapping(udc_controller
->usb_irq
);
2654 if (udc_controller
->nullmap
) {
2655 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2656 udc_controller
->nullp
, 256,
2658 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2660 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2661 udc_controller
->nullp
, 256,
2664 kfree(udc_controller
->statusbuf
);
2666 kfree(udc_controller
->nullbuf
);
2668 ep
= &udc_controller
->eps
[0];
2669 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2671 kfree(ep
->rxbuffer
);
2674 iounmap(udc_controller
->usb_regs
);
2676 kfree(udc_controller
);
2677 udc_controller
= NULL
;
2682 static int qe_udc_suspend(struct platform_device
*dev
, pm_message_t state
)
2687 static int qe_udc_resume(struct platform_device
*dev
)
2693 static int __devexit
qe_udc_remove(struct platform_device
*ofdev
)
2698 DECLARE_COMPLETION(done
);
2700 if (!udc_controller
)
2703 udc_controller
->done
= &done
;
2704 tasklet_disable(&udc_controller
->rx_tasklet
);
2706 if (udc_controller
->nullmap
) {
2707 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2708 udc_controller
->nullp
, 256,
2710 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2712 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2713 udc_controller
->nullp
, 256,
2716 kfree(udc_controller
->statusbuf
);
2717 kfree(udc_controller
->nullbuf
);
2719 ep
= &udc_controller
->eps
[0];
2720 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2721 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (USB_BDRING_LEN
+ 1);
2725 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2728 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
2730 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2735 kfree(ep
->rxbuffer
);
2738 free_irq(udc_controller
->usb_irq
, udc_controller
);
2739 irq_dispose_mapping(udc_controller
->usb_irq
);
2741 tasklet_kill(&udc_controller
->rx_tasklet
);
2743 iounmap(udc_controller
->usb_regs
);
2745 device_unregister(&udc_controller
->gadget
.dev
);
2746 /* wait for release() of gadget.dev to free udc */
2747 wait_for_completion(&done
);
2752 /*-------------------------------------------------------------------------*/
2753 static const struct of_device_id qe_udc_match
[] __devinitconst
= {
2755 .compatible
= "fsl,mpc8323-qe-usb",
2756 .data
= (void *)PORT_QE
,
2759 .compatible
= "fsl,mpc8360-qe-usb",
2760 .data
= (void *)PORT_QE
,
2763 .compatible
= "fsl,mpc8272-cpm-usb",
2764 .data
= (void *)PORT_CPM
,
2769 MODULE_DEVICE_TABLE(of
, qe_udc_match
);
2771 static struct of_platform_driver udc_driver
= {
2773 .name
= (char *)driver_name
,
2774 .owner
= THIS_MODULE
,
2775 .of_match_table
= qe_udc_match
,
2777 .probe
= qe_udc_probe
,
2778 .remove
= __devexit_p(qe_udc_remove
),
2780 .suspend
= qe_udc_suspend
,
2781 .resume
= qe_udc_resume
,
2785 static int __init
qe_udc_init(void)
2787 printk(KERN_INFO
"%s: %s, %s\n", driver_name
, driver_desc
,
2789 return of_register_platform_driver(&udc_driver
);
2792 static void __exit
qe_udc_exit(void)
2794 of_unregister_platform_driver(&udc_driver
);
2797 module_init(qe_udc_init
);
2798 module_exit(qe_udc_exit
);
2800 MODULE_DESCRIPTION(DRIVER_DESC
);
2801 MODULE_AUTHOR(DRIVER_AUTHOR
);
2802 MODULE_LICENSE("GPL");