2 * driver/usb/gadget/fsl_qe_udc.c
4 * Copyright (c) 2006-2008 Freescale Semiconductor, Inc. All rights reserved.
6 * Xie Xiaobo <X.Xie@freescale.com>
7 * Li Yang <leoli@freescale.com>
8 * Based on bareboard code from Shlomi Gridish.
11 * Freescle QE/CPM USB Pheripheral Controller Driver
12 * The controller can be found on MPC8360, MPC8272, and etc.
13 * MPC8360 Rev 1.1 may need QE mircocode update
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
23 #include <linux/module.h>
24 #include <linux/kernel.h>
25 #include <linux/init.h>
26 #include <linux/ioport.h>
27 #include <linux/types.h>
28 #include <linux/errno.h>
29 #include <linux/err.h>
30 #include <linux/slab.h>
31 #include <linux/list.h>
32 #include <linux/interrupt.h>
34 #include <linux/moduleparam.h>
35 #include <linux/of_platform.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/usb/ch9.h>
38 #include <linux/usb/gadget.h>
39 #include <linux/usb/otg.h>
44 #include "fsl_qe_udc.h"
46 #define DRIVER_DESC "Freescale QE/CPM USB Device Controller driver"
47 #define DRIVER_AUTHOR "Xie XiaoBo"
48 #define DRIVER_VERSION "1.0"
50 #define DMA_ADDR_INVALID (~(dma_addr_t)0)
52 static const char driver_name
[] = "fsl_qe_udc";
53 static const char driver_desc
[] = DRIVER_DESC
;
55 /*ep name is important in gadget, it should obey the convention of ep_match()*/
56 static const char *const ep_name
[] = {
57 "ep0-control", /* everyone has ep0 */
58 /* 3 configurable endpoints */
64 static struct usb_endpoint_descriptor qe_ep0_desc
= {
65 .bLength
= USB_DT_ENDPOINT_SIZE
,
66 .bDescriptorType
= USB_DT_ENDPOINT
,
68 .bEndpointAddress
= 0,
69 .bmAttributes
= USB_ENDPOINT_XFER_CONTROL
,
70 .wMaxPacketSize
= USB_MAX_CTRL_PAYLOAD
,
73 /* it is initialized in probe() */
74 static struct qe_udc
*udc_controller
;
76 /********************************************************************
77 * Internal Used Function Start
78 ********************************************************************/
79 /*-----------------------------------------------------------------
80 * done() - retire a request; caller blocked irqs
81 *--------------------------------------------------------------*/
82 static void done(struct qe_ep
*ep
, struct qe_req
*req
, int status
)
84 struct qe_udc
*udc
= ep
->udc
;
85 unsigned char stopped
= ep
->stopped
;
87 /* the req->queue pointer is used by ep_queue() func, in which
88 * the request will be added into a udc_ep->queue 'd tail
89 * so here the req will be dropped from the ep->queue
91 list_del_init(&req
->queue
);
93 /* req.status should be set as -EINPROGRESS in ep_queue() */
94 if (req
->req
.status
== -EINPROGRESS
)
95 req
->req
.status
= status
;
97 status
= req
->req
.status
;
100 dma_unmap_single(udc
->gadget
.dev
.parent
,
101 req
->req
.dma
, req
->req
.length
,
105 req
->req
.dma
= DMA_ADDR_INVALID
;
108 dma_sync_single_for_cpu(udc
->gadget
.dev
.parent
,
109 req
->req
.dma
, req
->req
.length
,
114 if (status
&& (status
!= -ESHUTDOWN
))
115 dev_vdbg(udc
->dev
, "complete %s req %p stat %d len %u/%u\n",
116 ep
->ep
.name
, &req
->req
, status
,
117 req
->req
.actual
, req
->req
.length
);
119 /* don't modify queue heads during completion callback */
121 spin_unlock(&udc
->lock
);
123 /* this complete() should a func implemented by gadget layer,
124 * eg fsg->bulk_in_complete() */
125 if (req
->req
.complete
)
126 req
->req
.complete(&ep
->ep
, &req
->req
);
128 spin_lock(&udc
->lock
);
130 ep
->stopped
= stopped
;
133 /*-----------------------------------------------------------------
134 * nuke(): delete all requests related to this ep
135 *--------------------------------------------------------------*/
136 static void nuke(struct qe_ep
*ep
, int status
)
138 /* Whether this eq has request linked */
139 while (!list_empty(&ep
->queue
)) {
140 struct qe_req
*req
= NULL
;
141 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
143 done(ep
, req
, status
);
147 /*---------------------------------------------------------------------------*
148 * USB and Endpoint manipulate process, include parameter and register *
149 *---------------------------------------------------------------------------*/
150 /* @value: 1--set stall 0--clean stall */
151 static int qe_eprx_stall_change(struct qe_ep
*ep
, int value
)
154 u8 epnum
= ep
->epnum
;
155 struct qe_udc
*udc
= ep
->udc
;
157 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
158 tem_usep
= tem_usep
& ~USB_RHS_MASK
;
160 tem_usep
|= USB_RHS_STALL
;
161 else if (ep
->dir
== USB_DIR_IN
)
162 tem_usep
|= USB_RHS_IGNORE_OUT
;
164 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
168 static int qe_eptx_stall_change(struct qe_ep
*ep
, int value
)
171 u8 epnum
= ep
->epnum
;
172 struct qe_udc
*udc
= ep
->udc
;
174 tem_usep
= in_be16(&udc
->usb_regs
->usb_usep
[epnum
]);
175 tem_usep
= tem_usep
& ~USB_THS_MASK
;
177 tem_usep
|= USB_THS_STALL
;
178 else if (ep
->dir
== USB_DIR_OUT
)
179 tem_usep
|= USB_THS_IGNORE_IN
;
181 out_be16(&udc
->usb_regs
->usb_usep
[epnum
], tem_usep
);
186 static int qe_ep0_stall(struct qe_udc
*udc
)
188 qe_eptx_stall_change(&udc
->eps
[0], 1);
189 qe_eprx_stall_change(&udc
->eps
[0], 1);
190 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
191 udc_controller
->ep0_dir
= 0;
195 static int qe_eprx_nack(struct qe_ep
*ep
)
197 u8 epnum
= ep
->epnum
;
198 struct qe_udc
*udc
= ep
->udc
;
200 if (ep
->state
== EP_STATE_IDLE
) {
201 /* Set the ep's nack */
202 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[epnum
],
203 USB_RHS_MASK
, USB_RHS_NACK
);
205 /* Mask Rx and Busy interrupts */
206 clrbits16(&udc
->usb_regs
->usb_usbmr
,
207 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
209 ep
->state
= EP_STATE_NACK
;
214 static int qe_eprx_normal(struct qe_ep
*ep
)
216 struct qe_udc
*udc
= ep
->udc
;
218 if (ep
->state
== EP_STATE_NACK
) {
219 clrsetbits_be16(&udc
->usb_regs
->usb_usep
[ep
->epnum
],
220 USB_RTHS_MASK
, USB_THS_IGNORE_IN
);
222 /* Unmask RX interrupts */
223 out_be16(&udc
->usb_regs
->usb_usber
,
224 USB_E_BSY_MASK
| USB_E_RXB_MASK
);
225 setbits16(&udc
->usb_regs
->usb_usbmr
,
226 (USB_E_RXB_MASK
| USB_E_BSY_MASK
));
228 ep
->state
= EP_STATE_IDLE
;
235 static int qe_ep_cmd_stoptx(struct qe_ep
*ep
)
237 if (ep
->udc
->soc_type
== PORT_CPM
)
238 cpm_command(CPM_USB_STOP_TX
| (ep
->epnum
<< CPM_USB_EP_SHIFT
),
239 CPM_USB_STOP_TX_OPCODE
);
241 qe_issue_cmd(QE_USB_STOP_TX
, QE_CR_SUBBLOCK_USB
,
247 static int qe_ep_cmd_restarttx(struct qe_ep
*ep
)
249 if (ep
->udc
->soc_type
== PORT_CPM
)
250 cpm_command(CPM_USB_RESTART_TX
| (ep
->epnum
<<
251 CPM_USB_EP_SHIFT
), CPM_USB_RESTART_TX_OPCODE
);
253 qe_issue_cmd(QE_USB_RESTART_TX
, QE_CR_SUBBLOCK_USB
,
259 static int qe_ep_flushtxfifo(struct qe_ep
*ep
)
261 struct qe_udc
*udc
= ep
->udc
;
266 qe_ep_cmd_stoptx(ep
);
267 out_8(&udc
->usb_regs
->usb_uscom
,
268 USB_CMD_FLUSH_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
269 out_be16(&udc
->ep_param
[i
]->tbptr
, in_be16(&udc
->ep_param
[i
]->tbase
));
270 out_be32(&udc
->ep_param
[i
]->tstate
, 0);
271 out_be16(&udc
->ep_param
[i
]->tbcnt
, 0);
273 ep
->c_txbd
= ep
->txbase
;
274 ep
->n_txbd
= ep
->txbase
;
275 qe_ep_cmd_restarttx(ep
);
279 static int qe_ep_filltxfifo(struct qe_ep
*ep
)
281 struct qe_udc
*udc
= ep
->udc
;
283 out_8(&udc
->usb_regs
->usb_uscom
,
284 USB_CMD_STR_FIFO
| (USB_CMD_EP_MASK
& (ep
->epnum
)));
288 static int qe_epbds_reset(struct qe_udc
*udc
, int pipe_num
)
292 struct qe_bd __iomem
*bd
;
295 ep
= &udc
->eps
[pipe_num
];
297 if (ep
->dir
== USB_DIR_OUT
)
298 bdring_len
= USB_BDRING_LEN_RX
;
300 bdring_len
= USB_BDRING_LEN
;
303 for (i
= 0; i
< (bdring_len
- 1); i
++) {
304 out_be32((u32 __iomem
*)bd
, R_E
| R_I
);
307 out_be32((u32 __iomem
*)bd
, R_E
| R_I
| R_W
);
310 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
311 out_be32(&bd
->buf
, 0);
312 out_be32((u32 __iomem
*)bd
, 0);
315 out_be32((u32 __iomem
*)bd
, T_W
);
320 static int qe_ep_reset(struct qe_udc
*udc
, int pipe_num
)
325 ep
= &udc
->eps
[pipe_num
];
326 tmpusep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe_num
]);
327 tmpusep
&= ~USB_RTHS_MASK
;
331 qe_ep_flushtxfifo(ep
);
334 tmpusep
|= USB_THS_IGNORE_IN
;
337 qe_ep_flushtxfifo(ep
);
338 tmpusep
|= USB_RHS_IGNORE_OUT
;
343 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], tmpusep
);
345 qe_epbds_reset(udc
, pipe_num
);
350 static int qe_ep_toggledata01(struct qe_ep
*ep
)
356 static int qe_ep_bd_init(struct qe_udc
*udc
, unsigned char pipe_num
)
358 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
359 unsigned long tmp_addr
= 0;
360 struct usb_ep_para __iomem
*epparam
;
362 struct qe_bd __iomem
*bd
;
365 if (ep
->dir
== USB_DIR_OUT
)
366 bdring_len
= USB_BDRING_LEN_RX
;
368 bdring_len
= USB_BDRING_LEN
;
370 epparam
= udc
->ep_param
[pipe_num
];
371 /* alloc multi-ram for BD rings and set the ep parameters */
372 tmp_addr
= cpm_muram_alloc(sizeof(struct qe_bd
) * (bdring_len
+
373 USB_BDRING_LEN_TX
), QE_ALIGNMENT_OF_BD
);
374 if (IS_ERR_VALUE(tmp_addr
))
377 out_be16(&epparam
->rbase
, (u16
)tmp_addr
);
378 out_be16(&epparam
->tbase
, (u16
)(tmp_addr
+
379 (sizeof(struct qe_bd
) * bdring_len
)));
381 out_be16(&epparam
->rbptr
, in_be16(&epparam
->rbase
));
382 out_be16(&epparam
->tbptr
, in_be16(&epparam
->tbase
));
384 ep
->rxbase
= cpm_muram_addr(tmp_addr
);
385 ep
->txbase
= cpm_muram_addr(tmp_addr
+ (sizeof(struct qe_bd
)
387 ep
->n_rxbd
= ep
->rxbase
;
388 ep
->e_rxbd
= ep
->rxbase
;
389 ep
->n_txbd
= ep
->txbase
;
390 ep
->c_txbd
= ep
->txbase
;
391 ep
->data01
= 0; /* data0 */
393 /* Init TX and RX bds */
395 for (i
= 0; i
< bdring_len
- 1; i
++) {
396 out_be32(&bd
->buf
, 0);
397 out_be32((u32 __iomem
*)bd
, 0);
400 out_be32(&bd
->buf
, 0);
401 out_be32((u32 __iomem
*)bd
, R_W
);
404 for (i
= 0; i
< USB_BDRING_LEN_TX
- 1; i
++) {
405 out_be32(&bd
->buf
, 0);
406 out_be32((u32 __iomem
*)bd
, 0);
409 out_be32(&bd
->buf
, 0);
410 out_be32((u32 __iomem
*)bd
, T_W
);
415 static int qe_ep_rxbd_update(struct qe_ep
*ep
)
420 struct qe_bd __iomem
*bd
;
421 unsigned int bdring_len
;
423 if (ep
->rxbase
== NULL
)
428 ep
->rxframe
= kmalloc(sizeof(*ep
->rxframe
), GFP_ATOMIC
);
429 if (ep
->rxframe
== NULL
) {
430 dev_err(ep
->udc
->dev
, "malloc rxframe failed\n");
434 qe_frame_init(ep
->rxframe
);
436 if (ep
->dir
== USB_DIR_OUT
)
437 bdring_len
= USB_BDRING_LEN_RX
;
439 bdring_len
= USB_BDRING_LEN
;
441 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (bdring_len
+ 1);
442 ep
->rxbuffer
= kzalloc(size
, GFP_ATOMIC
);
443 if (ep
->rxbuffer
== NULL
) {
444 dev_err(ep
->udc
->dev
, "malloc rxbuffer failed,size=%d\n",
450 ep
->rxbuf_d
= virt_to_phys((void *)ep
->rxbuffer
);
451 if (ep
->rxbuf_d
== DMA_ADDR_INVALID
) {
452 ep
->rxbuf_d
= dma_map_single(udc_controller
->gadget
.dev
.parent
,
458 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
464 size
= ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2;
466 tmp
= (u32
)(((tmp
>> 2) << 2) + 4);
468 for (i
= 0; i
< bdring_len
- 1; i
++) {
469 out_be32(&bd
->buf
, tmp
);
470 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
));
474 out_be32(&bd
->buf
, tmp
);
475 out_be32((u32 __iomem
*)bd
, (R_E
| R_I
| R_W
));
480 static int qe_ep_register_init(struct qe_udc
*udc
, unsigned char pipe_num
)
482 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
483 struct usb_ep_para __iomem
*epparam
;
488 epparam
= udc
->ep_param
[pipe_num
];
491 logepnum
= (ep
->desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
);
492 usep
|= (logepnum
<< USB_EPNUM_SHIFT
);
494 switch (ep
->desc
->bmAttributes
& 0x03) {
495 case USB_ENDPOINT_XFER_BULK
:
496 usep
|= USB_TRANS_BULK
;
498 case USB_ENDPOINT_XFER_ISOC
:
499 usep
|= USB_TRANS_ISO
;
501 case USB_ENDPOINT_XFER_INT
:
502 usep
|= USB_TRANS_INT
;
505 usep
|= USB_TRANS_CTR
;
511 usep
|= USB_THS_IGNORE_IN
;
514 usep
|= USB_RHS_IGNORE_OUT
;
519 out_be16(&udc
->usb_regs
->usb_usep
[pipe_num
], usep
);
522 out_8(&epparam
->rbmr
, rtfcr
);
523 out_8(&epparam
->tbmr
, rtfcr
);
525 tmp
= (u16
)(ep
->ep
.maxpacket
+ USB_CRC_SIZE
);
526 /* MRBLR must be divisble by 4 */
527 tmp
= (u16
)(((tmp
>> 2) << 2) + 4);
528 out_be16(&epparam
->mrblr
, tmp
);
533 static int qe_ep_init(struct qe_udc
*udc
,
534 unsigned char pipe_num
,
535 const struct usb_endpoint_descriptor
*desc
)
537 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
542 max
= le16_to_cpu(desc
->wMaxPacketSize
);
544 /* check the max package size validate for this endpoint */
545 /* Refer to USB2.0 spec table 9-13,
548 switch (desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
) {
549 case USB_ENDPOINT_XFER_BULK
:
550 if (strstr(ep
->ep
.name
, "-iso")
551 || strstr(ep
->ep
.name
, "-int"))
553 switch (udc
->gadget
.speed
) {
555 if ((max
== 128) || (max
== 256) || (max
== 512))
571 case USB_ENDPOINT_XFER_INT
:
572 if (strstr(ep
->ep
.name
, "-iso")) /* bulk is ok */
574 switch (udc
->gadget
.speed
) {
587 case USB_ENDPOINT_XFER_ISOC
:
588 if (strstr(ep
->ep
.name
, "-bulk")
589 || strstr(ep
->ep
.name
, "-int"))
591 switch (udc
->gadget
.speed
) {
602 case USB_ENDPOINT_XFER_CONTROL
:
603 if (strstr(ep
->ep
.name
, "-iso")
604 || strstr(ep
->ep
.name
, "-int"))
606 switch (udc
->gadget
.speed
) {
641 spin_lock_irqsave(&udc
->lock
, flags
);
643 /* initialize ep structure */
644 ep
->ep
.maxpacket
= max
;
645 ep
->tm
= (u8
)(desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
);
651 ep
->dir
= USB_DIR_BOTH
;
652 udc
->ep0_dir
= USB_DIR_OUT
;
653 udc
->ep0_state
= WAIT_FOR_SETUP
;
655 switch (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) {
657 ep
->dir
= USB_DIR_OUT
;
660 ep
->dir
= USB_DIR_IN
;
666 /* hardware special operation */
667 qe_ep_bd_init(udc
, pipe_num
);
668 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_OUT
)) {
669 reval
= qe_ep_rxbd_update(ep
);
674 if ((ep
->tm
== USBP_TM_CTL
) || (ep
->dir
== USB_DIR_IN
)) {
675 ep
->txframe
= kmalloc(sizeof(*ep
->txframe
), GFP_ATOMIC
);
676 if (ep
->txframe
== NULL
) {
677 dev_err(udc
->dev
, "malloc txframe failed\n");
680 qe_frame_init(ep
->txframe
);
683 qe_ep_register_init(udc
, pipe_num
);
685 /* Now HW will be NAKing transfers to that EP,
686 * until a buffer is queued to it. */
687 spin_unlock_irqrestore(&udc
->lock
, flags
);
694 spin_unlock_irqrestore(&udc
->lock
, flags
);
696 dev_err(udc
->dev
, "failed to initialize %s\n", ep
->ep
.name
);
700 static inline void qe_usb_enable(void)
702 setbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
705 static inline void qe_usb_disable(void)
707 clrbits8(&udc_controller
->usb_regs
->usb_usmod
, USB_MODE_EN
);
710 /*----------------------------------------------------------------------------*
711 * USB and EP basic manipulate function end *
712 *----------------------------------------------------------------------------*/
715 /******************************************************************************
716 UDC transmit and receive process
717 ******************************************************************************/
718 static void recycle_one_rxbd(struct qe_ep
*ep
)
722 bdstatus
= in_be32((u32 __iomem
*)ep
->e_rxbd
);
723 bdstatus
= R_I
| R_E
| (bdstatus
& R_W
);
724 out_be32((u32 __iomem
*)ep
->e_rxbd
, bdstatus
);
727 ep
->e_rxbd
= ep
->rxbase
;
732 static void recycle_rxbds(struct qe_ep
*ep
, unsigned char stopatnext
)
735 struct qe_bd __iomem
*bd
, *nextbd
;
736 unsigned char stop
= 0;
740 bdstatus
= in_be32((u32 __iomem
*)bd
);
742 while (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
) && !stop
) {
743 bdstatus
= R_E
| R_I
| (bdstatus
& R_W
);
744 out_be32((u32 __iomem
*)bd
, bdstatus
);
751 bdstatus
= in_be32((u32 __iomem
*)bd
);
752 if (stopatnext
&& (bd
== nextbd
))
759 static void ep_recycle_rxbds(struct qe_ep
*ep
)
761 struct qe_bd __iomem
*bd
= ep
->n_rxbd
;
763 u8 epnum
= ep
->epnum
;
764 struct qe_udc
*udc
= ep
->udc
;
766 bdstatus
= in_be32((u32 __iomem
*)bd
);
767 if (!(bdstatus
& R_E
) && !(bdstatus
& BD_LENGTH_MASK
)) {
769 ((in_be16(&udc
->ep_param
[epnum
]->rbptr
) -
770 in_be16(&udc
->ep_param
[epnum
]->rbase
))
772 bdstatus
= in_be32((u32 __iomem
*)bd
);
780 recycle_rxbds(ep
, 0);
781 ep
->e_rxbd
= ep
->n_rxbd
;
783 recycle_rxbds(ep
, 1);
785 if (in_be16(&udc
->usb_regs
->usb_usber
) & USB_E_BSY_MASK
)
786 out_be16(&udc
->usb_regs
->usb_usber
, USB_E_BSY_MASK
);
788 if (ep
->has_data
<= 0 && (!list_empty(&ep
->queue
)))
794 static void setup_received_handle(struct qe_udc
*udc
,
795 struct usb_ctrlrequest
*setup
);
796 static int qe_ep_rxframe_handle(struct qe_ep
*ep
);
797 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
);
798 /* when BD PID is setup, handle the packet */
799 static int ep0_setup_handle(struct qe_udc
*udc
)
801 struct qe_ep
*ep
= &udc
->eps
[0];
802 struct qe_frame
*pframe
;
806 pframe
= ep
->rxframe
;
807 if ((frame_get_info(pframe
) & PID_SETUP
)
808 && (udc
->ep0_state
== WAIT_FOR_SETUP
)) {
809 fsize
= frame_get_length(pframe
);
810 if (unlikely(fsize
!= 8))
812 cp
= (u8
*)&udc
->local_setup_buff
;
813 memcpy(cp
, pframe
->data
, fsize
);
816 /* handle the usb command base on the usb_ctrlrequest */
817 setup_received_handle(udc
, &udc
->local_setup_buff
);
823 static int qe_ep0_rx(struct qe_udc
*udc
)
825 struct qe_ep
*ep
= &udc
->eps
[0];
826 struct qe_frame
*pframe
;
827 struct qe_bd __iomem
*bd
;
828 u32 bdstatus
, length
;
831 pframe
= ep
->rxframe
;
833 if (ep
->dir
== USB_DIR_IN
) {
834 dev_err(udc
->dev
, "ep0 not a control endpoint\n");
839 bdstatus
= in_be32((u32 __iomem
*)bd
);
840 length
= bdstatus
& BD_LENGTH_MASK
;
842 while (!(bdstatus
& R_E
) && length
) {
843 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
844 && !(bdstatus
& R_ERROR
)) {
845 if (length
== USB_CRC_SIZE
) {
846 udc
->ep0_state
= WAIT_FOR_SETUP
;
848 "receive a ZLP in status phase\n");
850 qe_frame_clean(pframe
);
851 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
852 frame_set_data(pframe
, (u8
*)vaddr
);
853 frame_set_length(pframe
,
854 (length
- USB_CRC_SIZE
));
855 frame_set_status(pframe
, FRAME_OK
);
856 switch (bdstatus
& R_PID
) {
858 frame_set_info(pframe
, PID_SETUP
);
861 frame_set_info(pframe
, PID_DATA1
);
864 frame_set_info(pframe
, PID_DATA0
);
868 if ((bdstatus
& R_PID
) == R_PID_SETUP
)
869 ep0_setup_handle(udc
);
871 qe_ep_rxframe_handle(ep
);
874 dev_err(udc
->dev
, "The receive frame with error!\n");
877 /* note: don't clear the rxbd's buffer address */
878 recycle_one_rxbd(ep
);
886 bdstatus
= in_be32((u32 __iomem
*)bd
);
887 length
= bdstatus
& BD_LENGTH_MASK
;
896 static int qe_ep_rxframe_handle(struct qe_ep
*ep
)
898 struct qe_frame
*pframe
;
904 pframe
= ep
->rxframe
;
906 if (frame_get_info(pframe
) & PID_DATA1
)
909 if (framepid
!= ep
->data01
) {
910 dev_err(ep
->udc
->dev
, "the data01 error!\n");
914 fsize
= frame_get_length(pframe
);
915 if (list_empty(&ep
->queue
)) {
916 dev_err(ep
->udc
->dev
, "the %s have no requeue!\n", ep
->name
);
918 req
= list_entry(ep
->queue
.next
, struct qe_req
, queue
);
920 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
922 memcpy(cp
, pframe
->data
, fsize
);
923 req
->req
.actual
+= fsize
;
924 if ((fsize
< ep
->ep
.maxpacket
) ||
925 (req
->req
.actual
>= req
->req
.length
)) {
927 ep0_req_complete(ep
->udc
, req
);
930 if (list_empty(&ep
->queue
) && ep
->epnum
!= 0)
936 qe_ep_toggledata01(ep
);
941 static void ep_rx_tasklet(unsigned long data
)
943 struct qe_udc
*udc
= (struct qe_udc
*)data
;
945 struct qe_frame
*pframe
;
946 struct qe_bd __iomem
*bd
;
948 u32 bdstatus
, length
;
951 spin_lock_irqsave(&udc
->lock
, flags
);
953 for (i
= 1; i
< USB_MAX_ENDPOINTS
; i
++) {
956 if (ep
->dir
== USB_DIR_IN
|| ep
->enable_tasklet
== 0) {
958 "This is a transmit ep or disable tasklet!\n");
962 pframe
= ep
->rxframe
;
964 bdstatus
= in_be32((u32 __iomem
*)bd
);
965 length
= bdstatus
& BD_LENGTH_MASK
;
967 while (!(bdstatus
& R_E
) && length
) {
968 if (list_empty(&ep
->queue
)) {
971 "The rxep have noreq %d\n",
976 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
977 && !(bdstatus
& R_ERROR
)) {
978 qe_frame_clean(pframe
);
979 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
980 frame_set_data(pframe
, (u8
*)vaddr
);
981 frame_set_length(pframe
,
982 (length
- USB_CRC_SIZE
));
983 frame_set_status(pframe
, FRAME_OK
);
984 switch (bdstatus
& R_PID
) {
986 frame_set_info(pframe
, PID_DATA1
);
989 frame_set_info(pframe
, PID_SETUP
);
992 frame_set_info(pframe
, PID_DATA0
);
995 /* handle the rx frame */
996 qe_ep_rxframe_handle(ep
);
999 "error in received frame\n");
1001 /* note: don't clear the rxbd's buffer address */
1002 /*clear the length */
1003 out_be32((u32 __iomem
*)bd
, bdstatus
& BD_STATUS_MASK
);
1005 if (!(ep
->localnack
))
1006 recycle_one_rxbd(ep
);
1014 bdstatus
= in_be32((u32 __iomem
*)bd
);
1015 length
= bdstatus
& BD_LENGTH_MASK
;
1021 ep_recycle_rxbds(ep
);
1023 ep
->enable_tasklet
= 0;
1026 spin_unlock_irqrestore(&udc
->lock
, flags
);
1029 static int qe_ep_rx(struct qe_ep
*ep
)
1032 struct qe_frame
*pframe
;
1033 struct qe_bd __iomem
*bd
;
1034 u16 swoffs
, ucoffs
, emptybds
;
1037 pframe
= ep
->rxframe
;
1039 if (ep
->dir
== USB_DIR_IN
) {
1040 dev_err(udc
->dev
, "transmit ep in rx function\n");
1046 swoffs
= (u16
)(bd
- ep
->rxbase
);
1047 ucoffs
= (u16
)((in_be16(&udc
->ep_param
[ep
->epnum
]->rbptr
) -
1048 in_be16(&udc
->ep_param
[ep
->epnum
]->rbase
)) >> 3);
1049 if (swoffs
< ucoffs
)
1050 emptybds
= USB_BDRING_LEN_RX
- ucoffs
+ swoffs
;
1052 emptybds
= swoffs
- ucoffs
;
1054 if (emptybds
< MIN_EMPTY_BDS
) {
1057 dev_vdbg(udc
->dev
, "%d empty bds, send NACK\n", emptybds
);
1059 ep
->has_data
= USB_BDRING_LEN_RX
- emptybds
;
1061 if (list_empty(&ep
->queue
)) {
1063 dev_vdbg(udc
->dev
, "The rxep have no req queued with %d BDs\n",
1068 tasklet_schedule(&udc
->rx_tasklet
);
1069 ep
->enable_tasklet
= 1;
1074 /* send data from a frame, no matter what tx_req */
1075 static int qe_ep_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1077 struct qe_udc
*udc
= ep
->udc
;
1078 struct qe_bd __iomem
*bd
;
1080 u32 bdstatus
, pidmask
;
1083 if (ep
->dir
== USB_DIR_OUT
) {
1084 dev_err(udc
->dev
, "receive ep passed to tx function\n");
1088 /* Disable the Tx interrupt */
1089 saveusbmr
= in_be16(&udc
->usb_regs
->usb_usbmr
);
1090 out_be16(&udc
->usb_regs
->usb_usbmr
,
1091 saveusbmr
& ~(USB_E_TXB_MASK
| USB_E_TXE_MASK
));
1094 bdstatus
= in_be32((u32 __iomem
*)bd
);
1096 if (!(bdstatus
& (T_R
| BD_LENGTH_MASK
))) {
1097 if (frame_get_length(frame
) == 0) {
1098 frame_set_data(frame
, udc
->nullbuf
);
1099 frame_set_length(frame
, 2);
1100 frame
->info
|= (ZLP
| NO_CRC
);
1101 dev_vdbg(udc
->dev
, "the frame size = 0\n");
1103 paddr
= virt_to_phys((void *)frame
->data
);
1104 out_be32(&bd
->buf
, paddr
);
1105 bdstatus
= (bdstatus
&T_W
);
1106 if (!(frame_get_info(frame
) & NO_CRC
))
1107 bdstatus
|= T_R
| T_I
| T_L
| T_TC
1108 | frame_get_length(frame
);
1110 bdstatus
|= T_R
| T_I
| T_L
| frame_get_length(frame
);
1112 /* if the packet is a ZLP in status phase */
1113 if ((ep
->epnum
== 0) && (udc
->ep0_state
== DATA_STATE_NEED_ZLP
))
1117 pidmask
= T_PID_DATA1
;
1118 frame
->info
|= PID_DATA1
;
1120 pidmask
= T_PID_DATA0
;
1121 frame
->info
|= PID_DATA0
;
1124 bdstatus
|= pidmask
;
1125 out_be32((u32 __iomem
*)bd
, bdstatus
);
1126 qe_ep_filltxfifo(ep
);
1128 /* enable the TX interrupt */
1129 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1131 qe_ep_toggledata01(ep
);
1133 ep
->n_txbd
= ep
->txbase
;
1139 out_be16(&udc
->usb_regs
->usb_usbmr
, saveusbmr
);
1140 dev_vdbg(udc
->dev
, "The tx bd is not ready!\n");
1145 /* when a bd was transmitted, the function can
1146 * handle the tx_req, not include ep0 */
1147 static int txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1149 if (ep
->tx_req
!= NULL
) {
1151 int asent
= ep
->last
;
1158 /* a request already were transmitted completely */
1159 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1160 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1161 done(ep
, ep
->tx_req
, 0);
1168 /* we should gain a new tx_req fot this endpoint */
1169 if (ep
->tx_req
== NULL
) {
1170 if (!list_empty(&ep
->queue
)) {
1171 ep
->tx_req
= list_entry(ep
->queue
.next
, struct qe_req
,
1181 /* give a frame and a tx_req, send some data */
1182 static int qe_usb_senddata(struct qe_ep
*ep
, struct qe_frame
*frame
)
1187 qe_frame_clean(frame
);
1188 size
= min_t(u32
, (ep
->tx_req
->req
.length
- ep
->sent
),
1190 buf
= (u8
*)ep
->tx_req
->req
.buf
+ ep
->sent
;
1193 frame_set_data(frame
, buf
);
1194 frame_set_length(frame
, size
);
1195 frame_set_status(frame
, FRAME_OK
);
1196 frame_set_info(frame
, 0);
1197 return qe_ep_tx(ep
, frame
);
1202 /* give a frame struct,send a ZLP */
1203 static int sendnulldata(struct qe_ep
*ep
, struct qe_frame
*frame
, uint infor
)
1205 struct qe_udc
*udc
= ep
->udc
;
1210 qe_frame_clean(frame
);
1211 frame_set_data(frame
, (u8
*)udc
->nullbuf
);
1212 frame_set_length(frame
, 2);
1213 frame_set_status(frame
, FRAME_OK
);
1214 frame_set_info(frame
, (ZLP
| NO_CRC
| infor
));
1216 return qe_ep_tx(ep
, frame
);
1219 static int frame_create_tx(struct qe_ep
*ep
, struct qe_frame
*frame
)
1221 struct qe_req
*req
= ep
->tx_req
;
1227 if ((req
->req
.length
- ep
->sent
) > 0)
1228 reval
= qe_usb_senddata(ep
, frame
);
1230 reval
= sendnulldata(ep
, frame
, 0);
1235 /* if direction is DIR_IN, the status is Device->Host
1236 * if direction is DIR_OUT, the status transaction is Device<-Host
1237 * in status phase, udc create a request and gain status */
1238 static int ep0_prime_status(struct qe_udc
*udc
, int direction
)
1241 struct qe_ep
*ep
= &udc
->eps
[0];
1243 if (direction
== USB_DIR_IN
) {
1244 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
1245 udc
->ep0_dir
= USB_DIR_IN
;
1246 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1248 udc
->ep0_dir
= USB_DIR_OUT
;
1249 udc
->ep0_state
= WAIT_FOR_OUT_STATUS
;
1255 /* a request complete in ep0, whether gadget request or udc request */
1256 static void ep0_req_complete(struct qe_udc
*udc
, struct qe_req
*req
)
1258 struct qe_ep
*ep
= &udc
->eps
[0];
1259 /* because usb and ep's status already been set in ch9setaddress() */
1261 switch (udc
->ep0_state
) {
1262 case DATA_STATE_XMIT
:
1264 /* receive status phase */
1265 if (ep0_prime_status(udc
, USB_DIR_OUT
))
1269 case DATA_STATE_NEED_ZLP
:
1271 udc
->ep0_state
= WAIT_FOR_SETUP
;
1274 case DATA_STATE_RECV
:
1276 /* send status phase */
1277 if (ep0_prime_status(udc
, USB_DIR_IN
))
1281 case WAIT_FOR_OUT_STATUS
:
1283 udc
->ep0_state
= WAIT_FOR_SETUP
;
1286 case WAIT_FOR_SETUP
:
1287 dev_vdbg(udc
->dev
, "Unexpected interrupt\n");
1296 static int ep0_txcomplete(struct qe_ep
*ep
, unsigned char restart
)
1298 struct qe_req
*tx_req
= NULL
;
1299 struct qe_frame
*frame
= ep
->txframe
;
1301 if ((frame_get_info(frame
) & (ZLP
| NO_REQ
)) == (ZLP
| NO_REQ
)) {
1303 ep
->udc
->ep0_state
= WAIT_FOR_SETUP
;
1305 sendnulldata(ep
, ep
->txframe
, SETUP_STATUS
| NO_REQ
);
1309 tx_req
= ep
->tx_req
;
1310 if (tx_req
!= NULL
) {
1312 int asent
= ep
->last
;
1319 /* a request already were transmitted completely */
1320 if ((ep
->tx_req
->req
.length
- ep
->sent
) <= 0) {
1321 ep
->tx_req
->req
.actual
= (unsigned int)ep
->sent
;
1322 ep0_req_complete(ep
->udc
, ep
->tx_req
);
1328 dev_vdbg(ep
->udc
->dev
, "the ep0_controller have no req\n");
1334 static int ep0_txframe_handle(struct qe_ep
*ep
)
1336 /* if have error, transmit again */
1337 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1338 qe_ep_flushtxfifo(ep
);
1339 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1340 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1345 ep0_txcomplete(ep
, 1);
1347 ep0_txcomplete(ep
, 0);
1349 frame_create_tx(ep
, ep
->txframe
);
1353 static int qe_ep0_txconf(struct qe_ep
*ep
)
1355 struct qe_bd __iomem
*bd
;
1356 struct qe_frame
*pframe
;
1360 bdstatus
= in_be32((u32 __iomem
*)bd
);
1361 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1362 pframe
= ep
->txframe
;
1364 /* clear and recycle the BD */
1365 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1366 out_be32(&bd
->buf
, 0);
1368 ep
->c_txbd
= ep
->txbase
;
1372 if (ep
->c_txbd
== ep
->n_txbd
) {
1373 if (bdstatus
& DEVICE_T_ERROR
) {
1374 frame_set_status(pframe
, FRAME_ERROR
);
1375 if (bdstatus
& T_TO
)
1376 pframe
->status
|= TX_ER_TIMEOUT
;
1377 if (bdstatus
& T_UN
)
1378 pframe
->status
|= TX_ER_UNDERUN
;
1380 ep0_txframe_handle(ep
);
1384 bdstatus
= in_be32((u32 __iomem
*)bd
);
1390 static int ep_txframe_handle(struct qe_ep
*ep
)
1392 if (frame_get_status(ep
->txframe
) & FRAME_ERROR
) {
1393 qe_ep_flushtxfifo(ep
);
1394 dev_vdbg(ep
->udc
->dev
, "The EP0 transmit data have error!\n");
1395 if (frame_get_info(ep
->txframe
) & PID_DATA0
)
1404 frame_create_tx(ep
, ep
->txframe
); /* send the data */
1408 /* confirm the already trainsmited bd */
1409 static int qe_ep_txconf(struct qe_ep
*ep
)
1411 struct qe_bd __iomem
*bd
;
1412 struct qe_frame
*pframe
= NULL
;
1414 unsigned char breakonrxinterrupt
= 0;
1417 bdstatus
= in_be32((u32 __iomem
*)bd
);
1418 while (!(bdstatus
& T_R
) && (bdstatus
& ~T_W
)) {
1419 pframe
= ep
->txframe
;
1420 if (bdstatus
& DEVICE_T_ERROR
) {
1421 frame_set_status(pframe
, FRAME_ERROR
);
1422 if (bdstatus
& T_TO
)
1423 pframe
->status
|= TX_ER_TIMEOUT
;
1424 if (bdstatus
& T_UN
)
1425 pframe
->status
|= TX_ER_UNDERUN
;
1428 /* clear and recycle the BD */
1429 out_be32((u32 __iomem
*)bd
, bdstatus
& T_W
);
1430 out_be32(&bd
->buf
, 0);
1432 ep
->c_txbd
= ep
->txbase
;
1436 /* handle the tx frame */
1437 ep_txframe_handle(ep
);
1439 bdstatus
= in_be32((u32 __iomem
*)bd
);
1441 if (breakonrxinterrupt
)
1447 /* Add a request in queue, and try to transmit a packet */
1448 static int ep_req_send(struct qe_ep
*ep
, struct qe_req
*req
)
1452 if (ep
->tx_req
== NULL
) {
1455 txcomplete(ep
, 0); /* can gain a new tx_req */
1456 reval
= frame_create_tx(ep
, ep
->txframe
);
1461 /* Maybe this is a good ideal */
1462 static int ep_req_rx(struct qe_ep
*ep
, struct qe_req
*req
)
1464 struct qe_udc
*udc
= ep
->udc
;
1465 struct qe_frame
*pframe
= NULL
;
1466 struct qe_bd __iomem
*bd
;
1467 u32 bdstatus
, length
;
1473 if (list_empty(&ep
->queue
)) {
1474 dev_vdbg(udc
->dev
, "the req already finish!\n");
1477 pframe
= ep
->rxframe
;
1480 bdstatus
= in_be32((u32 __iomem
*)bd
);
1481 length
= bdstatus
& BD_LENGTH_MASK
;
1483 while (!(bdstatus
& R_E
) && length
) {
1486 if ((bdstatus
& R_F
) && (bdstatus
& R_L
)
1487 && !(bdstatus
& R_ERROR
)) {
1488 qe_frame_clean(pframe
);
1489 vaddr
= (u32
)phys_to_virt(in_be32(&bd
->buf
));
1490 frame_set_data(pframe
, (u8
*)vaddr
);
1491 frame_set_length(pframe
, (length
- USB_CRC_SIZE
));
1492 frame_set_status(pframe
, FRAME_OK
);
1493 switch (bdstatus
& R_PID
) {
1495 frame_set_info(pframe
, PID_DATA1
); break;
1497 frame_set_info(pframe
, PID_DATA0
); break;
1499 /* handle the rx frame */
1501 if (frame_get_info(pframe
) & PID_DATA1
)
1506 if (framepid
!= ep
->data01
) {
1507 dev_vdbg(udc
->dev
, "the data01 error!\n");
1509 fsize
= frame_get_length(pframe
);
1511 cp
= (u8
*)(req
->req
.buf
) + req
->req
.actual
;
1513 memcpy(cp
, pframe
->data
, fsize
);
1514 req
->req
.actual
+= fsize
;
1515 if ((fsize
< ep
->ep
.maxpacket
)
1516 || (req
->req
.actual
>=
1520 if (list_empty(&ep
->queue
))
1524 qe_ep_toggledata01(ep
);
1527 dev_err(udc
->dev
, "The receive frame with error!\n");
1530 /* note: don't clear the rxbd's buffer address *
1531 * only Clear the length */
1532 out_be32((u32 __iomem
*)bd
, (bdstatus
& BD_STATUS_MASK
));
1541 bdstatus
= in_be32((u32 __iomem
*)bd
);
1542 length
= bdstatus
& BD_LENGTH_MASK
;
1546 ep_recycle_rxbds(ep
);
1551 /* only add the request in queue */
1552 static int ep_req_receive(struct qe_ep
*ep
, struct qe_req
*req
)
1554 if (ep
->state
== EP_STATE_NACK
) {
1555 if (ep
->has_data
<= 0) {
1556 /* Enable rx and unmask rx interrupt */
1559 /* Copy the exist BD data */
1567 /********************************************************************
1568 Internal Used Function End
1569 ********************************************************************/
1571 /*-----------------------------------------------------------------------
1572 Endpoint Management Functions For Gadget
1573 -----------------------------------------------------------------------*/
1574 static int qe_ep_enable(struct usb_ep
*_ep
,
1575 const struct usb_endpoint_descriptor
*desc
)
1580 unsigned char epnum
;
1582 ep
= container_of(_ep
, struct qe_ep
, ep
);
1584 /* catch various bogus parameters */
1585 if (!_ep
|| !desc
|| ep
->desc
|| _ep
->name
== ep_name
[0] ||
1586 (desc
->bDescriptorType
!= USB_DT_ENDPOINT
))
1590 if (!udc
->driver
|| (udc
->gadget
.speed
== USB_SPEED_UNKNOWN
))
1593 epnum
= (u8
)desc
->bEndpointAddress
& 0xF;
1595 retval
= qe_ep_init(udc
, epnum
, desc
);
1597 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1598 dev_dbg(udc
->dev
, "enable ep%d failed\n", ep
->epnum
);
1601 dev_dbg(udc
->dev
, "enable ep%d successful\n", ep
->epnum
);
1605 static int qe_ep_disable(struct usb_ep
*_ep
)
1609 unsigned long flags
;
1612 ep
= container_of(_ep
, struct qe_ep
, ep
);
1615 if (!_ep
|| !ep
->desc
) {
1616 dev_dbg(udc
->dev
, "%s not enabled\n", _ep
? ep
->ep
.name
: NULL
);
1620 spin_lock_irqsave(&udc
->lock
, flags
);
1621 /* Nuke all pending requests (does flush) */
1622 nuke(ep
, -ESHUTDOWN
);
1626 qe_ep_reset(udc
, ep
->epnum
);
1627 spin_unlock_irqrestore(&udc
->lock
, flags
);
1629 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
1631 if (ep
->dir
== USB_DIR_OUT
)
1632 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1633 (USB_BDRING_LEN_RX
+ 1);
1635 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) *
1636 (USB_BDRING_LEN
+ 1);
1638 if (ep
->dir
!= USB_DIR_IN
) {
1641 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
1644 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
1646 dma_sync_single_for_cpu(
1647 udc_controller
->gadget
.dev
.parent
,
1651 kfree(ep
->rxbuffer
);
1654 if (ep
->dir
!= USB_DIR_OUT
)
1657 dev_dbg(udc
->dev
, "disabled %s OK\n", _ep
->name
);
1661 static struct usb_request
*qe_alloc_request(struct usb_ep
*_ep
, gfp_t gfp_flags
)
1665 req
= kzalloc(sizeof(*req
), gfp_flags
);
1669 req
->req
.dma
= DMA_ADDR_INVALID
;
1671 INIT_LIST_HEAD(&req
->queue
);
1676 static void qe_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
1680 req
= container_of(_req
, struct qe_req
, req
);
1686 static int __qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1688 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1689 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1694 /* catch various bogus parameters */
1695 if (!_req
|| !req
->req
.complete
|| !req
->req
.buf
1696 || !list_empty(&req
->queue
)) {
1697 dev_dbg(udc
->dev
, "bad params\n");
1700 if (!_ep
|| (!ep
->desc
&& ep_index(ep
))) {
1701 dev_dbg(udc
->dev
, "bad ep\n");
1705 if (!udc
->driver
|| udc
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1710 /* map virtual address to hardware */
1711 if (req
->req
.dma
== DMA_ADDR_INVALID
) {
1712 req
->req
.dma
= dma_map_single(ep
->udc
->gadget
.dev
.parent
,
1720 dma_sync_single_for_device(ep
->udc
->gadget
.dev
.parent
,
1721 req
->req
.dma
, req
->req
.length
,
1728 req
->req
.status
= -EINPROGRESS
;
1729 req
->req
.actual
= 0;
1731 list_add_tail(&req
->queue
, &ep
->queue
);
1732 dev_vdbg(udc
->dev
, "gadget have request in %s! %d\n",
1733 ep
->name
, req
->req
.length
);
1735 /* push the request to device */
1737 reval
= ep_req_send(ep
, req
);
1740 if (ep_index(ep
) == 0 && req
->req
.length
> 0) {
1742 udc
->ep0_state
= DATA_STATE_XMIT
;
1744 udc
->ep0_state
= DATA_STATE_RECV
;
1747 if (ep
->dir
== USB_DIR_OUT
)
1748 reval
= ep_req_receive(ep
, req
);
1753 /* queues (submits) an I/O request to an endpoint */
1754 static int qe_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
1757 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1758 struct qe_udc
*udc
= ep
->udc
;
1759 unsigned long flags
;
1762 spin_lock_irqsave(&udc
->lock
, flags
);
1763 ret
= __qe_ep_queue(_ep
, _req
);
1764 spin_unlock_irqrestore(&udc
->lock
, flags
);
1768 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1769 static int qe_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
1771 struct qe_ep
*ep
= container_of(_ep
, struct qe_ep
, ep
);
1773 unsigned long flags
;
1778 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1780 /* make sure it's actually queued on this endpoint */
1781 list_for_each_entry(req
, &ep
->queue
, queue
) {
1782 if (&req
->req
== _req
)
1786 if (&req
->req
!= _req
) {
1787 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1791 done(ep
, req
, -ECONNRESET
);
1793 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1797 /*-----------------------------------------------------------------
1798 * modify the endpoint halt feature
1799 * @ep: the non-isochronous endpoint being stalled
1800 * @value: 1--set halt 0--clear halt
1801 * Returns zero, or a negative error code.
1802 *----------------------------------------------------------------*/
1803 static int qe_ep_set_halt(struct usb_ep
*_ep
, int value
)
1806 unsigned long flags
;
1807 int status
= -EOPNOTSUPP
;
1810 ep
= container_of(_ep
, struct qe_ep
, ep
);
1811 if (!_ep
|| !ep
->desc
) {
1817 /* Attempt to halt IN ep will fail if any transfer requests
1818 * are still queue */
1819 if (value
&& ep_is_in(ep
) && !list_empty(&ep
->queue
)) {
1825 spin_lock_irqsave(&ep
->udc
->lock
, flags
);
1826 qe_eptx_stall_change(ep
, value
);
1827 qe_eprx_stall_change(ep
, value
);
1828 spin_unlock_irqrestore(&ep
->udc
->lock
, flags
);
1830 if (ep
->epnum
== 0) {
1831 udc
->ep0_state
= WAIT_FOR_SETUP
;
1835 /* set data toggle to DATA0 on clear halt */
1839 dev_vdbg(udc
->dev
, "%s %s halt stat %d\n", ep
->ep
.name
,
1840 value
? "set" : "clear", status
);
1845 static struct usb_ep_ops qe_ep_ops
= {
1846 .enable
= qe_ep_enable
,
1847 .disable
= qe_ep_disable
,
1849 .alloc_request
= qe_alloc_request
,
1850 .free_request
= qe_free_request
,
1852 .queue
= qe_ep_queue
,
1853 .dequeue
= qe_ep_dequeue
,
1855 .set_halt
= qe_ep_set_halt
,
1858 /*------------------------------------------------------------------------
1859 Gadget Driver Layer Operations
1860 ------------------------------------------------------------------------*/
1862 /* Get the current frame number */
1863 static int qe_get_frame(struct usb_gadget
*gadget
)
1867 tmp
= in_be16(&udc_controller
->usb_param
->frame_n
);
1876 /* Tries to wake up the host connected to this gadget
1878 * Return : 0-success
1879 * Negative-this feature not enabled by host or not supported by device hw
1881 static int qe_wakeup(struct usb_gadget
*gadget
)
1886 /* Notify controller that VBUS is powered, Called by whatever
1887 detects VBUS sessions */
1888 static int qe_vbus_session(struct usb_gadget
*gadget
, int is_active
)
1893 /* constrain controller's VBUS power usage
1894 * This call is used by gadget drivers during SET_CONFIGURATION calls,
1895 * reporting how much power the device may consume. For example, this
1896 * could affect how quickly batteries are recharged.
1898 * Returns zero on success, else negative errno.
1900 static int qe_vbus_draw(struct usb_gadget
*gadget
, unsigned mA
)
1905 /* Change Data+ pullup status
1906 * this func is used by usb_gadget_connect/disconnect
1908 static int qe_pullup(struct usb_gadget
*gadget
, int is_on
)
1913 /* defined in usb_gadget.h */
1914 static struct usb_gadget_ops qe_gadget_ops
= {
1915 .get_frame
= qe_get_frame
,
1916 .wakeup
= qe_wakeup
,
1917 /* .set_selfpowered = qe_set_selfpowered,*/ /* always selfpowered */
1918 .vbus_session
= qe_vbus_session
,
1919 .vbus_draw
= qe_vbus_draw
,
1920 .pullup
= qe_pullup
,
1923 /*-------------------------------------------------------------------------
1924 USB ep0 Setup process in BUS Enumeration
1925 -------------------------------------------------------------------------*/
1926 static int udc_reset_ep_queue(struct qe_udc
*udc
, u8 pipe
)
1928 struct qe_ep
*ep
= &udc
->eps
[pipe
];
1930 nuke(ep
, -ECONNRESET
);
1935 static int reset_queues(struct qe_udc
*udc
)
1939 for (pipe
= 0; pipe
< USB_MAX_ENDPOINTS
; pipe
++)
1940 udc_reset_ep_queue(udc
, pipe
);
1942 /* report disconnect; the driver is already quiesced */
1943 spin_unlock(&udc
->lock
);
1944 udc
->driver
->disconnect(&udc
->gadget
);
1945 spin_lock(&udc
->lock
);
1950 static void ch9setaddress(struct qe_udc
*udc
, u16 value
, u16 index
,
1953 /* Save the new address to device struct */
1954 udc
->device_address
= (u8
) value
;
1955 /* Update usb state */
1956 udc
->usb_state
= USB_STATE_ADDRESS
;
1958 /* Status phase , send a ZLP */
1959 if (ep0_prime_status(udc
, USB_DIR_IN
))
1963 static void ownercomplete(struct usb_ep
*_ep
, struct usb_request
*_req
)
1965 struct qe_req
*req
= container_of(_req
, struct qe_req
, req
);
1967 req
->req
.buf
= NULL
;
1971 static void ch9getstatus(struct qe_udc
*udc
, u8 request_type
, u16 value
,
1972 u16 index
, u16 length
)
1980 if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_DEVICE
) {
1981 /* Get device status */
1982 usb_status
= 1 << USB_DEVICE_SELF_POWERED
;
1983 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_INTERFACE
) {
1984 /* Get interface status */
1985 /* We don't have interface information in udc driver */
1987 } else if ((request_type
& USB_RECIP_MASK
) == USB_RECIP_ENDPOINT
) {
1988 /* Get endpoint status */
1989 int pipe
= index
& USB_ENDPOINT_NUMBER_MASK
;
1990 struct qe_ep
*target_ep
= &udc
->eps
[pipe
];
1993 /* stall if endpoint doesn't exist */
1994 if (!target_ep
->desc
)
1997 usep
= in_be16(&udc
->usb_regs
->usb_usep
[pipe
]);
1998 if (index
& USB_DIR_IN
) {
1999 if (target_ep
->dir
!= USB_DIR_IN
)
2001 if ((usep
& USB_THS_MASK
) == USB_THS_STALL
)
2002 usb_status
= 1 << USB_ENDPOINT_HALT
;
2004 if (target_ep
->dir
!= USB_DIR_OUT
)
2006 if ((usep
& USB_RHS_MASK
) == USB_RHS_STALL
)
2007 usb_status
= 1 << USB_ENDPOINT_HALT
;
2011 req
= container_of(qe_alloc_request(&ep
->ep
, GFP_KERNEL
),
2012 struct qe_req
, req
);
2013 req
->req
.length
= 2;
2014 req
->req
.buf
= udc
->statusbuf
;
2015 *(u16
*)req
->req
.buf
= cpu_to_le16(usb_status
);
2016 req
->req
.status
= -EINPROGRESS
;
2017 req
->req
.actual
= 0;
2018 req
->req
.complete
= ownercomplete
;
2020 udc
->ep0_dir
= USB_DIR_IN
;
2023 status
= __qe_ep_queue(&ep
->ep
, &req
->req
);
2028 dev_err(udc
->dev
, "Can't respond to getstatus request \n");
2032 /* only handle the setup request, suppose the device in normal status */
2033 static void setup_received_handle(struct qe_udc
*udc
,
2034 struct usb_ctrlrequest
*setup
)
2036 /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2037 u16 wValue
= le16_to_cpu(setup
->wValue
);
2038 u16 wIndex
= le16_to_cpu(setup
->wIndex
);
2039 u16 wLength
= le16_to_cpu(setup
->wLength
);
2041 /* clear the previous request in the ep0 */
2042 udc_reset_ep_queue(udc
, 0);
2044 if (setup
->bRequestType
& USB_DIR_IN
)
2045 udc
->ep0_dir
= USB_DIR_IN
;
2047 udc
->ep0_dir
= USB_DIR_OUT
;
2049 switch (setup
->bRequest
) {
2050 case USB_REQ_GET_STATUS
:
2051 /* Data+Status phase form udc */
2052 if ((setup
->bRequestType
& (USB_DIR_IN
| USB_TYPE_MASK
))
2053 != (USB_DIR_IN
| USB_TYPE_STANDARD
))
2055 ch9getstatus(udc
, setup
->bRequestType
, wValue
, wIndex
,
2059 case USB_REQ_SET_ADDRESS
:
2060 /* Status phase from udc */
2061 if (setup
->bRequestType
!= (USB_DIR_OUT
| USB_TYPE_STANDARD
|
2064 ch9setaddress(udc
, wValue
, wIndex
, wLength
);
2067 case USB_REQ_CLEAR_FEATURE
:
2068 case USB_REQ_SET_FEATURE
:
2069 /* Requests with no data phase, status phase from udc */
2070 if ((setup
->bRequestType
& USB_TYPE_MASK
)
2071 != USB_TYPE_STANDARD
)
2074 if ((setup
->bRequestType
& USB_RECIP_MASK
)
2075 == USB_RECIP_ENDPOINT
) {
2076 int pipe
= wIndex
& USB_ENDPOINT_NUMBER_MASK
;
2079 if (wValue
!= 0 || wLength
!= 0
2080 || pipe
> USB_MAX_ENDPOINTS
)
2082 ep
= &udc
->eps
[pipe
];
2084 spin_unlock(&udc
->lock
);
2085 qe_ep_set_halt(&ep
->ep
,
2086 (setup
->bRequest
== USB_REQ_SET_FEATURE
)
2088 spin_lock(&udc
->lock
);
2091 ep0_prime_status(udc
, USB_DIR_IN
);
2100 /* Data phase from gadget, status phase from udc */
2101 if (setup
->bRequestType
& USB_DIR_IN
) {
2102 udc
->ep0_state
= DATA_STATE_XMIT
;
2103 udc
->ep0_dir
= USB_DIR_IN
;
2105 udc
->ep0_state
= DATA_STATE_RECV
;
2106 udc
->ep0_dir
= USB_DIR_OUT
;
2108 spin_unlock(&udc
->lock
);
2109 if (udc
->driver
->setup(&udc
->gadget
,
2110 &udc
->local_setup_buff
) < 0)
2112 spin_lock(&udc
->lock
);
2114 /* No data phase, IN status from gadget */
2115 udc
->ep0_dir
= USB_DIR_IN
;
2116 spin_unlock(&udc
->lock
);
2117 if (udc
->driver
->setup(&udc
->gadget
,
2118 &udc
->local_setup_buff
) < 0)
2120 spin_lock(&udc
->lock
);
2121 udc
->ep0_state
= DATA_STATE_NEED_ZLP
;
2125 /*-------------------------------------------------------------------------
2126 USB Interrupt handlers
2127 -------------------------------------------------------------------------*/
2128 static void suspend_irq(struct qe_udc
*udc
)
2130 udc
->resume_state
= udc
->usb_state
;
2131 udc
->usb_state
= USB_STATE_SUSPENDED
;
2133 /* report suspend to the driver ,serial.c not support this*/
2134 if (udc
->driver
->suspend
)
2135 udc
->driver
->suspend(&udc
->gadget
);
2138 static void resume_irq(struct qe_udc
*udc
)
2140 udc
->usb_state
= udc
->resume_state
;
2141 udc
->resume_state
= 0;
2143 /* report resume to the driver , serial.c not support this*/
2144 if (udc
->driver
->resume
)
2145 udc
->driver
->resume(&udc
->gadget
);
2148 static void idle_irq(struct qe_udc
*udc
)
2152 usbs
= in_8(&udc
->usb_regs
->usb_usbs
);
2153 if (usbs
& USB_IDLE_STATUS_MASK
) {
2154 if ((udc
->usb_state
) != USB_STATE_SUSPENDED
)
2157 if (udc
->usb_state
== USB_STATE_SUSPENDED
)
2162 static int reset_irq(struct qe_udc
*udc
)
2166 if (udc
->usb_state
== USB_STATE_DEFAULT
)
2170 out_8(&udc
->usb_regs
->usb_usadr
, 0);
2172 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2173 if (udc
->eps
[i
].init
)
2174 qe_ep_reset(udc
, i
);
2178 udc
->usb_state
= USB_STATE_DEFAULT
;
2179 udc
->ep0_state
= WAIT_FOR_SETUP
;
2180 udc
->ep0_dir
= USB_DIR_OUT
;
2185 static int bsy_irq(struct qe_udc
*udc
)
2190 static int txe_irq(struct qe_udc
*udc
)
2195 /* ep0 tx interrupt also in here */
2196 static int tx_irq(struct qe_udc
*udc
)
2199 struct qe_bd __iomem
*bd
;
2202 if ((udc
->usb_state
== USB_STATE_ADDRESS
)
2203 && (in_8(&udc
->usb_regs
->usb_usadr
) == 0))
2204 out_8(&udc
->usb_regs
->usb_usadr
, udc
->device_address
);
2206 for (i
= (USB_MAX_ENDPOINTS
-1); ((i
>= 0) && (res
== 0)); i
--) {
2208 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_OUT
)) {
2210 if (!(in_be32((u32 __iomem
*)bd
) & T_R
)
2211 && (in_be32(&bd
->buf
))) {
2212 /* confirm the transmitted bd */
2214 res
= qe_ep0_txconf(ep
);
2216 res
= qe_ep_txconf(ep
);
2224 /* setup packect's rx is handle in the function too */
2225 static void rx_irq(struct qe_udc
*udc
)
2228 struct qe_bd __iomem
*bd
;
2231 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2233 if (ep
&& ep
->init
&& (ep
->dir
!= USB_DIR_IN
)) {
2235 if (!(in_be32((u32 __iomem
*)bd
) & R_E
)
2236 && (in_be32(&bd
->buf
))) {
2237 if (ep
->epnum
== 0) {
2240 /*non-setup package receive*/
2248 static irqreturn_t
qe_udc_irq(int irq
, void *_udc
)
2250 struct qe_udc
*udc
= (struct qe_udc
*)_udc
;
2252 irqreturn_t status
= IRQ_NONE
;
2253 unsigned long flags
;
2255 spin_lock_irqsave(&udc
->lock
, flags
);
2257 irq_src
= in_be16(&udc
->usb_regs
->usb_usber
) &
2258 in_be16(&udc
->usb_regs
->usb_usbmr
);
2259 /* Clear notification bits */
2260 out_be16(&udc
->usb_regs
->usb_usber
, irq_src
);
2262 if (irq_src
& USB_E_IDLE_MASK
) {
2264 irq_src
&= ~USB_E_IDLE_MASK
;
2265 status
= IRQ_HANDLED
;
2268 if (irq_src
& USB_E_TXB_MASK
) {
2270 irq_src
&= ~USB_E_TXB_MASK
;
2271 status
= IRQ_HANDLED
;
2274 if (irq_src
& USB_E_RXB_MASK
) {
2276 irq_src
&= ~USB_E_RXB_MASK
;
2277 status
= IRQ_HANDLED
;
2280 if (irq_src
& USB_E_RESET_MASK
) {
2282 irq_src
&= ~USB_E_RESET_MASK
;
2283 status
= IRQ_HANDLED
;
2286 if (irq_src
& USB_E_BSY_MASK
) {
2288 irq_src
&= ~USB_E_BSY_MASK
;
2289 status
= IRQ_HANDLED
;
2292 if (irq_src
& USB_E_TXE_MASK
) {
2294 irq_src
&= ~USB_E_TXE_MASK
;
2295 status
= IRQ_HANDLED
;
2298 spin_unlock_irqrestore(&udc
->lock
, flags
);
2303 /*-------------------------------------------------------------------------
2304 Gadget driver register and unregister.
2305 --------------------------------------------------------------------------*/
2306 int usb_gadget_register_driver(struct usb_gadget_driver
*driver
)
2309 unsigned long flags
= 0;
2311 /* standard operations */
2312 if (!udc_controller
)
2315 if (!driver
|| (driver
->speed
!= USB_SPEED_FULL
2316 && driver
->speed
!= USB_SPEED_HIGH
)
2317 || !driver
->bind
|| !driver
->disconnect
2321 if (udc_controller
->driver
)
2324 /* lock is needed but whether should use this lock or another */
2325 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2327 driver
->driver
.bus
= NULL
;
2328 /* hook up the driver */
2329 udc_controller
->driver
= driver
;
2330 udc_controller
->gadget
.dev
.driver
= &driver
->driver
;
2331 udc_controller
->gadget
.speed
= (enum usb_device_speed
)(driver
->speed
);
2332 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2334 retval
= driver
->bind(&udc_controller
->gadget
);
2336 dev_err(udc_controller
->dev
, "bind to %s --> %d",
2337 driver
->driver
.name
, retval
);
2338 udc_controller
->gadget
.dev
.driver
= NULL
;
2339 udc_controller
->driver
= NULL
;
2343 /* Enable IRQ reg and Set usbcmd reg EN bit */
2346 out_be16(&udc_controller
->usb_regs
->usb_usber
, 0xffff);
2347 out_be16(&udc_controller
->usb_regs
->usb_usbmr
, USB_E_DEFAULT_DEVICE
);
2348 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2349 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2350 udc_controller
->ep0_dir
= USB_DIR_OUT
;
2351 dev_info(udc_controller
->dev
, "%s bind to driver %s \n",
2352 udc_controller
->gadget
.name
, driver
->driver
.name
);
2355 EXPORT_SYMBOL(usb_gadget_register_driver
);
2357 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
2359 struct qe_ep
*loop_ep
;
2360 unsigned long flags
;
2362 if (!udc_controller
)
2365 if (!driver
|| driver
!= udc_controller
->driver
)
2368 /* stop usb controller, disable intr */
2371 /* in fact, no needed */
2372 udc_controller
->usb_state
= USB_STATE_ATTACHED
;
2373 udc_controller
->ep0_state
= WAIT_FOR_SETUP
;
2374 udc_controller
->ep0_dir
= 0;
2376 /* stand operation */
2377 spin_lock_irqsave(&udc_controller
->lock
, flags
);
2378 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2379 nuke(&udc_controller
->eps
[0], -ESHUTDOWN
);
2380 list_for_each_entry(loop_ep
, &udc_controller
->gadget
.ep_list
,
2382 nuke(loop_ep
, -ESHUTDOWN
);
2383 spin_unlock_irqrestore(&udc_controller
->lock
, flags
);
2385 /* report disconnect; the controller is already quiesced */
2386 driver
->disconnect(&udc_controller
->gadget
);
2388 /* unbind gadget and unhook driver. */
2389 driver
->unbind(&udc_controller
->gadget
);
2390 udc_controller
->gadget
.dev
.driver
= NULL
;
2391 udc_controller
->driver
= NULL
;
2393 dev_info(udc_controller
->dev
, "unregistered gadget driver '%s'\r\n",
2394 driver
->driver
.name
);
2397 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
2399 /* udc structure's alloc and setup, include ep-param alloc */
2400 static struct qe_udc __devinit
*qe_udc_config(struct of_device
*ofdev
)
2403 struct device_node
*np
= ofdev
->node
;
2404 unsigned int tmp_addr
= 0;
2405 struct usb_device_para __iomem
*usbpram
;
2410 udc
= kzalloc(sizeof(*udc
), GFP_KERNEL
);
2412 dev_err(&ofdev
->dev
, "malloc udc failed\n");
2416 udc
->dev
= &ofdev
->dev
;
2418 /* get default address of usb parameter in MURAM from device tree */
2419 offset
= *of_get_address(np
, 1, &size
, NULL
);
2420 udc
->usb_param
= cpm_muram_addr(offset
);
2421 memset_io(udc
->usb_param
, 0, size
);
2423 usbpram
= udc
->usb_param
;
2424 out_be16(&usbpram
->frame_n
, 0);
2425 out_be32(&usbpram
->rstate
, 0);
2427 tmp_addr
= cpm_muram_alloc((USB_MAX_ENDPOINTS
*
2428 sizeof(struct usb_ep_para
)),
2429 USB_EP_PARA_ALIGNMENT
);
2430 if (IS_ERR_VALUE(tmp_addr
))
2433 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2434 out_be16(&usbpram
->epptr
[i
], (u16
)tmp_addr
);
2435 udc
->ep_param
[i
] = cpm_muram_addr(tmp_addr
);
2439 memset_io(udc
->ep_param
[0], 0,
2440 USB_MAX_ENDPOINTS
* sizeof(struct usb_ep_para
));
2442 udc
->resume_state
= USB_STATE_NOTATTACHED
;
2443 udc
->usb_state
= USB_STATE_POWERED
;
2446 spin_lock_init(&udc
->lock
);
2454 /* USB Controller register init */
2455 static int __devinit
qe_udc_reg_init(struct qe_udc
*udc
)
2457 struct usb_ctlr __iomem
*qe_usbregs
;
2458 qe_usbregs
= udc
->usb_regs
;
2460 /* Spec says that we must enable the USB controller to change mode. */
2461 out_8(&qe_usbregs
->usb_usmod
, 0x01);
2462 /* Mode changed, now disable it, since muram isn't initialized yet. */
2463 out_8(&qe_usbregs
->usb_usmod
, 0x00);
2465 /* Initialize the rest. */
2466 out_be16(&qe_usbregs
->usb_usbmr
, 0);
2467 out_8(&qe_usbregs
->usb_uscom
, 0);
2468 out_be16(&qe_usbregs
->usb_usber
, USBER_ALL_CLEAR
);
2473 static int __devinit
qe_ep_config(struct qe_udc
*udc
, unsigned char pipe_num
)
2475 struct qe_ep
*ep
= &udc
->eps
[pipe_num
];
2478 strcpy(ep
->name
, ep_name
[pipe_num
]);
2479 ep
->ep
.name
= ep_name
[pipe_num
];
2481 ep
->ep
.ops
= &qe_ep_ops
;
2483 ep
->ep
.maxpacket
= (unsigned short) ~0;
2486 ep
->epnum
= (u8
)pipe_num
;
2493 ep
->state
= EP_STATE_IDLE
;
2496 /* the queue lists any req for this ep */
2497 INIT_LIST_HEAD(&ep
->queue
);
2499 /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2501 list_add_tail(&ep
->ep
.ep_list
, &udc
->gadget
.ep_list
);
2503 ep
->gadget
= &udc
->gadget
;
2508 /*-----------------------------------------------------------------------
2509 * UDC device Driver operation functions *
2510 *----------------------------------------------------------------------*/
2511 static void qe_udc_release(struct device
*dev
)
2515 complete(udc_controller
->done
);
2516 cpm_muram_free(cpm_muram_offset(udc_controller
->ep_param
[0]));
2517 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++)
2518 udc_controller
->ep_param
[i
] = NULL
;
2520 kfree(udc_controller
);
2521 udc_controller
= NULL
;
2524 /* Driver probe functions */
2525 static int __devinit
qe_udc_probe(struct of_device
*ofdev
,
2526 const struct of_device_id
*match
)
2528 struct device_node
*np
= ofdev
->node
;
2530 unsigned int ret
= 0;
2534 prop
= of_get_property(np
, "mode", NULL
);
2535 if (!prop
|| strcmp(prop
, "peripheral"))
2538 /* Initialize the udc structure including QH member and other member */
2539 udc_controller
= qe_udc_config(ofdev
);
2540 if (!udc_controller
) {
2541 dev_err(&ofdev
->dev
, "failed to initialize\n");
2545 udc_controller
->soc_type
= (unsigned long)match
->data
;
2546 udc_controller
->usb_regs
= of_iomap(np
, 0);
2547 if (!udc_controller
->usb_regs
) {
2552 /* initialize usb hw reg except for regs for EP,
2553 * leave usbintr reg untouched*/
2554 qe_udc_reg_init(udc_controller
);
2556 /* here comes the stand operations for probe
2557 * set the qe_udc->gadget.xxx */
2558 udc_controller
->gadget
.ops
= &qe_gadget_ops
;
2560 /* gadget.ep0 is a pointer */
2561 udc_controller
->gadget
.ep0
= &udc_controller
->eps
[0].ep
;
2563 INIT_LIST_HEAD(&udc_controller
->gadget
.ep_list
);
2565 /* modify in register gadget process */
2566 udc_controller
->gadget
.speed
= USB_SPEED_UNKNOWN
;
2568 /* name: Identifies the controller hardware type. */
2569 udc_controller
->gadget
.name
= driver_name
;
2571 device_initialize(&udc_controller
->gadget
.dev
);
2573 dev_set_name(&udc_controller
->gadget
.dev
, "gadget");
2575 udc_controller
->gadget
.dev
.release
= qe_udc_release
;
2576 udc_controller
->gadget
.dev
.parent
= &ofdev
->dev
;
2578 /* initialize qe_ep struct */
2579 for (i
= 0; i
< USB_MAX_ENDPOINTS
; i
++) {
2580 /* because the ep type isn't decide here so
2581 * qe_ep_init() should be called in ep_enable() */
2583 /* setup the qe_ep struct and link ep.ep.list
2584 * into gadget.ep_list */
2585 qe_ep_config(udc_controller
, (unsigned char)i
);
2588 /* ep0 initialization in here */
2589 ret
= qe_ep_init(udc_controller
, 0, &qe_ep0_desc
);
2593 /* create a buf for ZLP send, need to remain zeroed */
2594 udc_controller
->nullbuf
= kzalloc(256, GFP_KERNEL
);
2595 if (udc_controller
->nullbuf
== NULL
) {
2596 dev_err(udc_controller
->dev
, "cannot alloc nullbuf\n");
2601 /* buffer for data of get_status request */
2602 udc_controller
->statusbuf
= kzalloc(2, GFP_KERNEL
);
2603 if (udc_controller
->statusbuf
== NULL
) {
2608 udc_controller
->nullp
= virt_to_phys((void *)udc_controller
->nullbuf
);
2609 if (udc_controller
->nullp
== DMA_ADDR_INVALID
) {
2610 udc_controller
->nullp
= dma_map_single(
2611 udc_controller
->gadget
.dev
.parent
,
2612 udc_controller
->nullbuf
,
2615 udc_controller
->nullmap
= 1;
2617 dma_sync_single_for_device(udc_controller
->gadget
.dev
.parent
,
2618 udc_controller
->nullp
, 256,
2622 tasklet_init(&udc_controller
->rx_tasklet
, ep_rx_tasklet
,
2623 (unsigned long)udc_controller
);
2624 /* request irq and disable DR */
2625 udc_controller
->usb_irq
= irq_of_parse_and_map(np
, 0);
2626 if (!udc_controller
->usb_irq
) {
2631 ret
= request_irq(udc_controller
->usb_irq
, qe_udc_irq
, 0,
2632 driver_name
, udc_controller
);
2634 dev_err(udc_controller
->dev
, "cannot request irq %d err %d \n",
2635 udc_controller
->usb_irq
, ret
);
2639 ret
= device_add(&udc_controller
->gadget
.dev
);
2643 dev_info(udc_controller
->dev
,
2644 "%s USB controller initialized as device\n",
2645 (udc_controller
->soc_type
== PORT_QE
) ? "QE" : "CPM");
2649 free_irq(udc_controller
->usb_irq
, udc_controller
);
2651 irq_dispose_mapping(udc_controller
->usb_irq
);
2653 if (udc_controller
->nullmap
) {
2654 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2655 udc_controller
->nullp
, 256,
2657 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2659 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2660 udc_controller
->nullp
, 256,
2663 kfree(udc_controller
->statusbuf
);
2665 kfree(udc_controller
->nullbuf
);
2667 ep
= &udc_controller
->eps
[0];
2668 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2670 kfree(ep
->rxbuffer
);
2673 iounmap(udc_controller
->usb_regs
);
2675 kfree(udc_controller
);
2676 udc_controller
= NULL
;
2681 static int qe_udc_suspend(struct of_device
*dev
, pm_message_t state
)
2686 static int qe_udc_resume(struct of_device
*dev
)
2692 static int __devexit
qe_udc_remove(struct of_device
*ofdev
)
2697 DECLARE_COMPLETION(done
);
2699 if (!udc_controller
)
2702 udc_controller
->done
= &done
;
2703 tasklet_disable(&udc_controller
->rx_tasklet
);
2705 if (udc_controller
->nullmap
) {
2706 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2707 udc_controller
->nullp
, 256,
2709 udc_controller
->nullp
= DMA_ADDR_INVALID
;
2711 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2712 udc_controller
->nullp
, 256,
2715 kfree(udc_controller
->statusbuf
);
2716 kfree(udc_controller
->nullbuf
);
2718 ep
= &udc_controller
->eps
[0];
2719 cpm_muram_free(cpm_muram_offset(ep
->rxbase
));
2720 size
= (ep
->ep
.maxpacket
+ USB_CRC_SIZE
+ 2) * (USB_BDRING_LEN
+ 1);
2724 dma_unmap_single(udc_controller
->gadget
.dev
.parent
,
2727 ep
->rxbuf_d
= DMA_ADDR_INVALID
;
2729 dma_sync_single_for_cpu(udc_controller
->gadget
.dev
.parent
,
2734 kfree(ep
->rxbuffer
);
2737 free_irq(udc_controller
->usb_irq
, udc_controller
);
2738 irq_dispose_mapping(udc_controller
->usb_irq
);
2740 tasklet_kill(&udc_controller
->rx_tasklet
);
2742 iounmap(udc_controller
->usb_regs
);
2744 device_unregister(&udc_controller
->gadget
.dev
);
2745 /* wait for release() of gadget.dev to free udc */
2746 wait_for_completion(&done
);
2751 /*-------------------------------------------------------------------------*/
2752 static struct of_device_id __devinitdata qe_udc_match
[] = {
2754 .compatible
= "fsl,mpc8360-qe-usb",
2755 .data
= (void *)PORT_QE
,
2758 .compatible
= "fsl,mpc8272-cpm-usb",
2759 .data
= (void *)PORT_CPM
,
2764 MODULE_DEVICE_TABLE(of
, qe_udc_match
);
2766 static struct of_platform_driver udc_driver
= {
2767 .name
= (char *)driver_name
,
2768 .match_table
= qe_udc_match
,
2769 .probe
= qe_udc_probe
,
2770 .remove
= __devexit_p(qe_udc_remove
),
2772 .suspend
= qe_udc_suspend
,
2773 .resume
= qe_udc_resume
,
2777 static int __init
qe_udc_init(void)
2779 printk(KERN_INFO
"%s: %s, %s\n", driver_name
, driver_desc
,
2781 return of_register_platform_driver(&udc_driver
);
2784 static void __exit
qe_udc_exit(void)
2786 of_unregister_platform_driver(&udc_driver
);
2789 module_init(qe_udc_init
);
2790 module_exit(qe_udc_exit
);
2792 MODULE_DESCRIPTION(DRIVER_DESC
);
2793 MODULE_AUTHOR(DRIVER_AUTHOR
);
2794 MODULE_LICENSE("GPL");