clk: x86: Rename clk-lpt to more specific clk-lpss-atom
[platform/kernel/linux-rpi.git] / drivers / usb / gadget / udc / fsl_qe_udc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * driver/usb/gadget/fsl_qe_udc.c
4  *
5  * Copyright (c) 2006-2008 Freescale Semiconductor, Inc. All rights reserved.
6  *
7  *      Xie Xiaobo <X.Xie@freescale.com>
8  *      Li Yang <leoli@freescale.com>
9  *      Based on bareboard code from Shlomi Gridish.
10  *
11  * Description:
12  * Freescle QE/CPM USB Pheripheral Controller Driver
13  * The controller can be found on MPC8360, MPC8272, and etc.
14  * MPC8360 Rev 1.1 may need QE mircocode update
15  */
16
17 #undef USB_TRACE
18
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/ioport.h>
22 #include <linux/types.h>
23 #include <linux/errno.h>
24 #include <linux/err.h>
25 #include <linux/slab.h>
26 #include <linux/list.h>
27 #include <linux/interrupt.h>
28 #include <linux/io.h>
29 #include <linux/moduleparam.h>
30 #include <linux/of_address.h>
31 #include <linux/of_irq.h>
32 #include <linux/of_platform.h>
33 #include <linux/dma-mapping.h>
34 #include <linux/usb/ch9.h>
35 #include <linux/usb/gadget.h>
36 #include <linux/usb/otg.h>
37 #include <soc/fsl/qe/qe.h>
38 #include <asm/cpm.h>
39 #include <asm/dma.h>
40 #include <asm/reg.h>
41 #include "fsl_qe_udc.h"
42
43 #define DRIVER_DESC     "Freescale QE/CPM USB Device Controller driver"
44 #define DRIVER_AUTHOR   "Xie XiaoBo"
45 #define DRIVER_VERSION  "1.0"
46
47 #define DMA_ADDR_INVALID        (~(dma_addr_t)0)
48
49 static const char driver_name[] = "fsl_qe_udc";
50 static const char driver_desc[] = DRIVER_DESC;
51
52 /*ep name is important in gadget, it should obey the convention of ep_match()*/
53 static const char *const ep_name[] = {
54         "ep0-control", /* everyone has ep0 */
55         /* 3 configurable endpoints */
56         "ep1",
57         "ep2",
58         "ep3",
59 };
60
61 static const struct usb_endpoint_descriptor qe_ep0_desc = {
62         .bLength =              USB_DT_ENDPOINT_SIZE,
63         .bDescriptorType =      USB_DT_ENDPOINT,
64
65         .bEndpointAddress =     0,
66         .bmAttributes =         USB_ENDPOINT_XFER_CONTROL,
67         .wMaxPacketSize =       USB_MAX_CTRL_PAYLOAD,
68 };
69
70 /********************************************************************
71  *      Internal Used Function Start
72 ********************************************************************/
73 /*-----------------------------------------------------------------
74  * done() - retire a request; caller blocked irqs
75  *--------------------------------------------------------------*/
76 static void done(struct qe_ep *ep, struct qe_req *req, int status)
77 {
78         struct qe_udc *udc = ep->udc;
79         unsigned char stopped = ep->stopped;
80
81         /* the req->queue pointer is used by ep_queue() func, in which
82          * the request will be added into a udc_ep->queue 'd tail
83          * so here the req will be dropped from the ep->queue
84          */
85         list_del_init(&req->queue);
86
87         /* req.status should be set as -EINPROGRESS in ep_queue() */
88         if (req->req.status == -EINPROGRESS)
89                 req->req.status = status;
90         else
91                 status = req->req.status;
92
93         if (req->mapped) {
94                 dma_unmap_single(udc->gadget.dev.parent,
95                         req->req.dma, req->req.length,
96                         ep_is_in(ep)
97                                 ? DMA_TO_DEVICE
98                                 : DMA_FROM_DEVICE);
99                 req->req.dma = DMA_ADDR_INVALID;
100                 req->mapped = 0;
101         } else
102                 dma_sync_single_for_cpu(udc->gadget.dev.parent,
103                         req->req.dma, req->req.length,
104                         ep_is_in(ep)
105                                 ? DMA_TO_DEVICE
106                                 : DMA_FROM_DEVICE);
107
108         if (status && (status != -ESHUTDOWN))
109                 dev_vdbg(udc->dev, "complete %s req %p stat %d len %u/%u\n",
110                         ep->ep.name, &req->req, status,
111                         req->req.actual, req->req.length);
112
113         /* don't modify queue heads during completion callback */
114         ep->stopped = 1;
115         spin_unlock(&udc->lock);
116
117         usb_gadget_giveback_request(&ep->ep, &req->req);
118
119         spin_lock(&udc->lock);
120
121         ep->stopped = stopped;
122 }
123
124 /*-----------------------------------------------------------------
125  * nuke(): delete all requests related to this ep
126  *--------------------------------------------------------------*/
127 static void nuke(struct qe_ep *ep, int status)
128 {
129         /* Whether this eq has request linked */
130         while (!list_empty(&ep->queue)) {
131                 struct qe_req *req = NULL;
132                 req = list_entry(ep->queue.next, struct qe_req, queue);
133
134                 done(ep, req, status);
135         }
136 }
137
138 /*---------------------------------------------------------------------------*
139  * USB and Endpoint manipulate process, include parameter and register       *
140  *---------------------------------------------------------------------------*/
141 /* @value: 1--set stall 0--clean stall */
142 static int qe_eprx_stall_change(struct qe_ep *ep, int value)
143 {
144         u16 tem_usep;
145         u8 epnum = ep->epnum;
146         struct qe_udc *udc = ep->udc;
147
148         tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]);
149         tem_usep = tem_usep & ~USB_RHS_MASK;
150         if (value == 1)
151                 tem_usep |= USB_RHS_STALL;
152         else if (ep->dir == USB_DIR_IN)
153                 tem_usep |= USB_RHS_IGNORE_OUT;
154
155         out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep);
156         return 0;
157 }
158
159 static int qe_eptx_stall_change(struct qe_ep *ep, int value)
160 {
161         u16 tem_usep;
162         u8 epnum = ep->epnum;
163         struct qe_udc *udc = ep->udc;
164
165         tem_usep = in_be16(&udc->usb_regs->usb_usep[epnum]);
166         tem_usep = tem_usep & ~USB_THS_MASK;
167         if (value == 1)
168                 tem_usep |= USB_THS_STALL;
169         else if (ep->dir == USB_DIR_OUT)
170                 tem_usep |= USB_THS_IGNORE_IN;
171
172         out_be16(&udc->usb_regs->usb_usep[epnum], tem_usep);
173
174         return 0;
175 }
176
177 static int qe_ep0_stall(struct qe_udc *udc)
178 {
179         qe_eptx_stall_change(&udc->eps[0], 1);
180         qe_eprx_stall_change(&udc->eps[0], 1);
181         udc->ep0_state = WAIT_FOR_SETUP;
182         udc->ep0_dir = 0;
183         return 0;
184 }
185
186 static int qe_eprx_nack(struct qe_ep *ep)
187 {
188         u8 epnum = ep->epnum;
189         struct qe_udc *udc = ep->udc;
190
191         if (ep->state == EP_STATE_IDLE) {
192                 /* Set the ep's nack */
193                 clrsetbits_be16(&udc->usb_regs->usb_usep[epnum],
194                                 USB_RHS_MASK, USB_RHS_NACK);
195
196                 /* Mask Rx and Busy interrupts */
197                 clrbits16(&udc->usb_regs->usb_usbmr,
198                                 (USB_E_RXB_MASK | USB_E_BSY_MASK));
199
200                 ep->state = EP_STATE_NACK;
201         }
202         return 0;
203 }
204
205 static int qe_eprx_normal(struct qe_ep *ep)
206 {
207         struct qe_udc *udc = ep->udc;
208
209         if (ep->state == EP_STATE_NACK) {
210                 clrsetbits_be16(&udc->usb_regs->usb_usep[ep->epnum],
211                                 USB_RTHS_MASK, USB_THS_IGNORE_IN);
212
213                 /* Unmask RX interrupts */
214                 out_be16(&udc->usb_regs->usb_usber,
215                                 USB_E_BSY_MASK | USB_E_RXB_MASK);
216                 setbits16(&udc->usb_regs->usb_usbmr,
217                                 (USB_E_RXB_MASK | USB_E_BSY_MASK));
218
219                 ep->state = EP_STATE_IDLE;
220                 ep->has_data = 0;
221         }
222
223         return 0;
224 }
225
226 static int qe_ep_cmd_stoptx(struct qe_ep *ep)
227 {
228         if (ep->udc->soc_type == PORT_CPM)
229                 cpm_command(CPM_USB_STOP_TX | (ep->epnum << CPM_USB_EP_SHIFT),
230                                 CPM_USB_STOP_TX_OPCODE);
231         else
232                 qe_issue_cmd(QE_USB_STOP_TX, QE_CR_SUBBLOCK_USB,
233                                 ep->epnum, 0);
234
235         return 0;
236 }
237
238 static int qe_ep_cmd_restarttx(struct qe_ep *ep)
239 {
240         if (ep->udc->soc_type == PORT_CPM)
241                 cpm_command(CPM_USB_RESTART_TX | (ep->epnum <<
242                                 CPM_USB_EP_SHIFT), CPM_USB_RESTART_TX_OPCODE);
243         else
244                 qe_issue_cmd(QE_USB_RESTART_TX, QE_CR_SUBBLOCK_USB,
245                                 ep->epnum, 0);
246
247         return 0;
248 }
249
250 static int qe_ep_flushtxfifo(struct qe_ep *ep)
251 {
252         struct qe_udc *udc = ep->udc;
253         int i;
254
255         i = (int)ep->epnum;
256
257         qe_ep_cmd_stoptx(ep);
258         out_8(&udc->usb_regs->usb_uscom,
259                 USB_CMD_FLUSH_FIFO | (USB_CMD_EP_MASK & (ep->epnum)));
260         out_be16(&udc->ep_param[i]->tbptr, in_be16(&udc->ep_param[i]->tbase));
261         out_be32(&udc->ep_param[i]->tstate, 0);
262         out_be16(&udc->ep_param[i]->tbcnt, 0);
263
264         ep->c_txbd = ep->txbase;
265         ep->n_txbd = ep->txbase;
266         qe_ep_cmd_restarttx(ep);
267         return 0;
268 }
269
270 static int qe_ep_filltxfifo(struct qe_ep *ep)
271 {
272         struct qe_udc *udc = ep->udc;
273
274         out_8(&udc->usb_regs->usb_uscom,
275                         USB_CMD_STR_FIFO | (USB_CMD_EP_MASK & (ep->epnum)));
276         return 0;
277 }
278
279 static int qe_epbds_reset(struct qe_udc *udc, int pipe_num)
280 {
281         struct qe_ep *ep;
282         u32 bdring_len;
283         struct qe_bd __iomem *bd;
284         int i;
285
286         ep = &udc->eps[pipe_num];
287
288         if (ep->dir == USB_DIR_OUT)
289                 bdring_len = USB_BDRING_LEN_RX;
290         else
291                 bdring_len = USB_BDRING_LEN;
292
293         bd = ep->rxbase;
294         for (i = 0; i < (bdring_len - 1); i++) {
295                 out_be32((u32 __iomem *)bd, R_E | R_I);
296                 bd++;
297         }
298         out_be32((u32 __iomem *)bd, R_E | R_I | R_W);
299
300         bd = ep->txbase;
301         for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) {
302                 out_be32(&bd->buf, 0);
303                 out_be32((u32 __iomem *)bd, 0);
304                 bd++;
305         }
306         out_be32((u32 __iomem *)bd, T_W);
307
308         return 0;
309 }
310
311 static int qe_ep_reset(struct qe_udc *udc, int pipe_num)
312 {
313         struct qe_ep *ep;
314         u16 tmpusep;
315
316         ep = &udc->eps[pipe_num];
317         tmpusep = in_be16(&udc->usb_regs->usb_usep[pipe_num]);
318         tmpusep &= ~USB_RTHS_MASK;
319
320         switch (ep->dir) {
321         case USB_DIR_BOTH:
322                 qe_ep_flushtxfifo(ep);
323                 break;
324         case USB_DIR_OUT:
325                 tmpusep |= USB_THS_IGNORE_IN;
326                 break;
327         case USB_DIR_IN:
328                 qe_ep_flushtxfifo(ep);
329                 tmpusep |= USB_RHS_IGNORE_OUT;
330                 break;
331         default:
332                 break;
333         }
334         out_be16(&udc->usb_regs->usb_usep[pipe_num], tmpusep);
335
336         qe_epbds_reset(udc, pipe_num);
337
338         return 0;
339 }
340
341 static int qe_ep_toggledata01(struct qe_ep *ep)
342 {
343         ep->data01 ^= 0x1;
344         return 0;
345 }
346
347 static int qe_ep_bd_init(struct qe_udc *udc, unsigned char pipe_num)
348 {
349         struct qe_ep *ep = &udc->eps[pipe_num];
350         unsigned long tmp_addr = 0;
351         struct usb_ep_para __iomem *epparam;
352         int i;
353         struct qe_bd __iomem *bd;
354         int bdring_len;
355
356         if (ep->dir == USB_DIR_OUT)
357                 bdring_len = USB_BDRING_LEN_RX;
358         else
359                 bdring_len = USB_BDRING_LEN;
360
361         epparam = udc->ep_param[pipe_num];
362         /* alloc multi-ram for BD rings and set the ep parameters */
363         tmp_addr = cpm_muram_alloc(sizeof(struct qe_bd) * (bdring_len +
364                                 USB_BDRING_LEN_TX), QE_ALIGNMENT_OF_BD);
365         if (IS_ERR_VALUE(tmp_addr))
366                 return -ENOMEM;
367
368         out_be16(&epparam->rbase, (u16)tmp_addr);
369         out_be16(&epparam->tbase, (u16)(tmp_addr +
370                                 (sizeof(struct qe_bd) * bdring_len)));
371
372         out_be16(&epparam->rbptr, in_be16(&epparam->rbase));
373         out_be16(&epparam->tbptr, in_be16(&epparam->tbase));
374
375         ep->rxbase = cpm_muram_addr(tmp_addr);
376         ep->txbase = cpm_muram_addr(tmp_addr + (sizeof(struct qe_bd)
377                                 * bdring_len));
378         ep->n_rxbd = ep->rxbase;
379         ep->e_rxbd = ep->rxbase;
380         ep->n_txbd = ep->txbase;
381         ep->c_txbd = ep->txbase;
382         ep->data01 = 0; /* data0 */
383
384         /* Init TX and RX bds */
385         bd = ep->rxbase;
386         for (i = 0; i < bdring_len - 1; i++) {
387                 out_be32(&bd->buf, 0);
388                 out_be32((u32 __iomem *)bd, 0);
389                 bd++;
390         }
391         out_be32(&bd->buf, 0);
392         out_be32((u32 __iomem *)bd, R_W);
393
394         bd = ep->txbase;
395         for (i = 0; i < USB_BDRING_LEN_TX - 1; i++) {
396                 out_be32(&bd->buf, 0);
397                 out_be32((u32 __iomem *)bd, 0);
398                 bd++;
399         }
400         out_be32(&bd->buf, 0);
401         out_be32((u32 __iomem *)bd, T_W);
402
403         return 0;
404 }
405
406 static int qe_ep_rxbd_update(struct qe_ep *ep)
407 {
408         unsigned int size;
409         int i;
410         unsigned int tmp;
411         struct qe_bd __iomem *bd;
412         unsigned int bdring_len;
413
414         if (ep->rxbase == NULL)
415                 return -EINVAL;
416
417         bd = ep->rxbase;
418
419         ep->rxframe = kmalloc(sizeof(*ep->rxframe), GFP_ATOMIC);
420         if (!ep->rxframe)
421                 return -ENOMEM;
422
423         qe_frame_init(ep->rxframe);
424
425         if (ep->dir == USB_DIR_OUT)
426                 bdring_len = USB_BDRING_LEN_RX;
427         else
428                 bdring_len = USB_BDRING_LEN;
429
430         size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (bdring_len + 1);
431         ep->rxbuffer = kzalloc(size, GFP_ATOMIC);
432         if (!ep->rxbuffer) {
433                 kfree(ep->rxframe);
434                 return -ENOMEM;
435         }
436
437         ep->rxbuf_d = virt_to_phys((void *)ep->rxbuffer);
438         if (ep->rxbuf_d == DMA_ADDR_INVALID) {
439                 ep->rxbuf_d = dma_map_single(ep->udc->gadget.dev.parent,
440                                         ep->rxbuffer,
441                                         size,
442                                         DMA_FROM_DEVICE);
443                 ep->rxbufmap = 1;
444         } else {
445                 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
446                                         ep->rxbuf_d, size,
447                                         DMA_FROM_DEVICE);
448                 ep->rxbufmap = 0;
449         }
450
451         size = ep->ep.maxpacket + USB_CRC_SIZE + 2;
452         tmp = ep->rxbuf_d;
453         tmp = (u32)(((tmp >> 2) << 2) + 4);
454
455         for (i = 0; i < bdring_len - 1; i++) {
456                 out_be32(&bd->buf, tmp);
457                 out_be32((u32 __iomem *)bd, (R_E | R_I));
458                 tmp = tmp + size;
459                 bd++;
460         }
461         out_be32(&bd->buf, tmp);
462         out_be32((u32 __iomem *)bd, (R_E | R_I | R_W));
463
464         return 0;
465 }
466
467 static int qe_ep_register_init(struct qe_udc *udc, unsigned char pipe_num)
468 {
469         struct qe_ep *ep = &udc->eps[pipe_num];
470         struct usb_ep_para __iomem *epparam;
471         u16 usep, logepnum;
472         u16 tmp;
473         u8 rtfcr = 0;
474
475         epparam = udc->ep_param[pipe_num];
476
477         usep = 0;
478         logepnum = (ep->ep.desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
479         usep |= (logepnum << USB_EPNUM_SHIFT);
480
481         switch (ep->ep.desc->bmAttributes & 0x03) {
482         case USB_ENDPOINT_XFER_BULK:
483                 usep |= USB_TRANS_BULK;
484                 break;
485         case USB_ENDPOINT_XFER_ISOC:
486                 usep |=  USB_TRANS_ISO;
487                 break;
488         case USB_ENDPOINT_XFER_INT:
489                 usep |= USB_TRANS_INT;
490                 break;
491         default:
492                 usep |= USB_TRANS_CTR;
493                 break;
494         }
495
496         switch (ep->dir) {
497         case USB_DIR_OUT:
498                 usep |= USB_THS_IGNORE_IN;
499                 break;
500         case USB_DIR_IN:
501                 usep |= USB_RHS_IGNORE_OUT;
502                 break;
503         default:
504                 break;
505         }
506         out_be16(&udc->usb_regs->usb_usep[pipe_num], usep);
507
508         rtfcr = 0x30;
509         out_8(&epparam->rbmr, rtfcr);
510         out_8(&epparam->tbmr, rtfcr);
511
512         tmp = (u16)(ep->ep.maxpacket + USB_CRC_SIZE);
513         /* MRBLR must be divisble by 4 */
514         tmp = (u16)(((tmp >> 2) << 2) + 4);
515         out_be16(&epparam->mrblr, tmp);
516
517         return 0;
518 }
519
520 static int qe_ep_init(struct qe_udc *udc,
521                       unsigned char pipe_num,
522                       const struct usb_endpoint_descriptor *desc)
523 {
524         struct qe_ep *ep = &udc->eps[pipe_num];
525         unsigned long flags;
526         int reval = 0;
527         u16 max = 0;
528
529         max = usb_endpoint_maxp(desc);
530
531         /* check the max package size validate for this endpoint */
532         /* Refer to USB2.0 spec table 9-13,
533         */
534         if (pipe_num != 0) {
535                 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
536                 case USB_ENDPOINT_XFER_BULK:
537                         if (strstr(ep->ep.name, "-iso")
538                                         || strstr(ep->ep.name, "-int"))
539                                 goto en_done;
540                         switch (udc->gadget.speed) {
541                         case USB_SPEED_HIGH:
542                         if ((max == 128) || (max == 256) || (max == 512))
543                                 break;
544                         fallthrough;
545                         default:
546                                 switch (max) {
547                                 case 4:
548                                 case 8:
549                                 case 16:
550                                 case 32:
551                                 case 64:
552                                         break;
553                                 default:
554                                 case USB_SPEED_LOW:
555                                         goto en_done;
556                                 }
557                         }
558                         break;
559                 case USB_ENDPOINT_XFER_INT:
560                         if (strstr(ep->ep.name, "-iso"))        /* bulk is ok */
561                                 goto en_done;
562                         switch (udc->gadget.speed) {
563                         case USB_SPEED_HIGH:
564                                 if (max <= 1024)
565                                         break;
566                                 fallthrough;
567                         case USB_SPEED_FULL:
568                                 if (max <= 64)
569                                         break;
570                                 fallthrough;
571                         default:
572                                 if (max <= 8)
573                                         break;
574                                 goto en_done;
575                         }
576                         break;
577                 case USB_ENDPOINT_XFER_ISOC:
578                         if (strstr(ep->ep.name, "-bulk")
579                                 || strstr(ep->ep.name, "-int"))
580                                 goto en_done;
581                         switch (udc->gadget.speed) {
582                         case USB_SPEED_HIGH:
583                                 if (max <= 1024)
584                                         break;
585                                 fallthrough;
586                         case USB_SPEED_FULL:
587                                 if (max <= 1023)
588                                         break;
589                         default:
590                                 goto en_done;
591                         }
592                         break;
593                 case USB_ENDPOINT_XFER_CONTROL:
594                         if (strstr(ep->ep.name, "-iso")
595                                 || strstr(ep->ep.name, "-int"))
596                                 goto en_done;
597                         switch (udc->gadget.speed) {
598                         case USB_SPEED_HIGH:
599                         case USB_SPEED_FULL:
600                                 switch (max) {
601                                 case 1:
602                                 case 2:
603                                 case 4:
604                                 case 8:
605                                 case 16:
606                                 case 32:
607                                 case 64:
608                                         break;
609                                 default:
610                                         goto en_done;
611                                 }
612                                 fallthrough;
613                         case USB_SPEED_LOW:
614                                 switch (max) {
615                                 case 1:
616                                 case 2:
617                                 case 4:
618                                 case 8:
619                                         break;
620                                 default:
621                                         goto en_done;
622                                 }
623                         default:
624                                 goto en_done;
625                         }
626                         break;
627
628                 default:
629                         goto en_done;
630                 }
631         } /* if ep0*/
632
633         spin_lock_irqsave(&udc->lock, flags);
634
635         /* initialize ep structure */
636         ep->ep.maxpacket = max;
637         ep->tm = (u8)(desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK);
638         ep->ep.desc = desc;
639         ep->stopped = 0;
640         ep->init = 1;
641
642         if (pipe_num == 0) {
643                 ep->dir = USB_DIR_BOTH;
644                 udc->ep0_dir = USB_DIR_OUT;
645                 udc->ep0_state = WAIT_FOR_SETUP;
646         } else  {
647                 switch (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) {
648                 case USB_DIR_OUT:
649                         ep->dir = USB_DIR_OUT;
650                         break;
651                 case USB_DIR_IN:
652                         ep->dir = USB_DIR_IN;
653                 default:
654                         break;
655                 }
656         }
657
658         /* hardware special operation */
659         qe_ep_bd_init(udc, pipe_num);
660         if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_OUT)) {
661                 reval = qe_ep_rxbd_update(ep);
662                 if (reval)
663                         goto en_done1;
664         }
665
666         if ((ep->tm == USBP_TM_CTL) || (ep->dir == USB_DIR_IN)) {
667                 ep->txframe = kmalloc(sizeof(*ep->txframe), GFP_ATOMIC);
668                 if (!ep->txframe)
669                         goto en_done2;
670                 qe_frame_init(ep->txframe);
671         }
672
673         qe_ep_register_init(udc, pipe_num);
674
675         /* Now HW will be NAKing transfers to that EP,
676          * until a buffer is queued to it. */
677         spin_unlock_irqrestore(&udc->lock, flags);
678
679         return 0;
680 en_done2:
681         kfree(ep->rxbuffer);
682         kfree(ep->rxframe);
683 en_done1:
684         spin_unlock_irqrestore(&udc->lock, flags);
685 en_done:
686         dev_err(udc->dev, "failed to initialize %s\n", ep->ep.name);
687         return -ENODEV;
688 }
689
690 static inline void qe_usb_enable(struct qe_udc *udc)
691 {
692         setbits8(&udc->usb_regs->usb_usmod, USB_MODE_EN);
693 }
694
695 static inline void qe_usb_disable(struct qe_udc *udc)
696 {
697         clrbits8(&udc->usb_regs->usb_usmod, USB_MODE_EN);
698 }
699
700 /*----------------------------------------------------------------------------*
701  *              USB and EP basic manipulate function end                      *
702  *----------------------------------------------------------------------------*/
703
704
705 /******************************************************************************
706                 UDC transmit and receive process
707  ******************************************************************************/
708 static void recycle_one_rxbd(struct qe_ep *ep)
709 {
710         u32 bdstatus;
711
712         bdstatus = in_be32((u32 __iomem *)ep->e_rxbd);
713         bdstatus = R_I | R_E | (bdstatus & R_W);
714         out_be32((u32 __iomem *)ep->e_rxbd, bdstatus);
715
716         if (bdstatus & R_W)
717                 ep->e_rxbd = ep->rxbase;
718         else
719                 ep->e_rxbd++;
720 }
721
722 static void recycle_rxbds(struct qe_ep *ep, unsigned char stopatnext)
723 {
724         u32 bdstatus;
725         struct qe_bd __iomem *bd, *nextbd;
726         unsigned char stop = 0;
727
728         nextbd = ep->n_rxbd;
729         bd = ep->e_rxbd;
730         bdstatus = in_be32((u32 __iomem *)bd);
731
732         while (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK) && !stop) {
733                 bdstatus = R_E | R_I | (bdstatus & R_W);
734                 out_be32((u32 __iomem *)bd, bdstatus);
735
736                 if (bdstatus & R_W)
737                         bd = ep->rxbase;
738                 else
739                         bd++;
740
741                 bdstatus = in_be32((u32 __iomem *)bd);
742                 if (stopatnext && (bd == nextbd))
743                         stop = 1;
744         }
745
746         ep->e_rxbd = bd;
747 }
748
749 static void ep_recycle_rxbds(struct qe_ep *ep)
750 {
751         struct qe_bd __iomem *bd = ep->n_rxbd;
752         u32 bdstatus;
753         u8 epnum = ep->epnum;
754         struct qe_udc *udc = ep->udc;
755
756         bdstatus = in_be32((u32 __iomem *)bd);
757         if (!(bdstatus & R_E) && !(bdstatus & BD_LENGTH_MASK)) {
758                 bd = ep->rxbase +
759                                 ((in_be16(&udc->ep_param[epnum]->rbptr) -
760                                   in_be16(&udc->ep_param[epnum]->rbase))
761                                  >> 3);
762                 bdstatus = in_be32((u32 __iomem *)bd);
763
764                 if (bdstatus & R_W)
765                         bd = ep->rxbase;
766                 else
767                         bd++;
768
769                 ep->e_rxbd = bd;
770                 recycle_rxbds(ep, 0);
771                 ep->e_rxbd = ep->n_rxbd;
772         } else
773                 recycle_rxbds(ep, 1);
774
775         if (in_be16(&udc->usb_regs->usb_usber) & USB_E_BSY_MASK)
776                 out_be16(&udc->usb_regs->usb_usber, USB_E_BSY_MASK);
777
778         if (ep->has_data <= 0 && (!list_empty(&ep->queue)))
779                 qe_eprx_normal(ep);
780
781         ep->localnack = 0;
782 }
783
784 static void setup_received_handle(struct qe_udc *udc,
785                                         struct usb_ctrlrequest *setup);
786 static int qe_ep_rxframe_handle(struct qe_ep *ep);
787 static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req);
788 /* when BD PID is setup, handle the packet */
789 static int ep0_setup_handle(struct qe_udc *udc)
790 {
791         struct qe_ep *ep = &udc->eps[0];
792         struct qe_frame *pframe;
793         unsigned int fsize;
794         u8 *cp;
795
796         pframe = ep->rxframe;
797         if ((frame_get_info(pframe) & PID_SETUP)
798                         && (udc->ep0_state == WAIT_FOR_SETUP)) {
799                 fsize = frame_get_length(pframe);
800                 if (unlikely(fsize != 8))
801                         return -EINVAL;
802                 cp = (u8 *)&udc->local_setup_buff;
803                 memcpy(cp, pframe->data, fsize);
804                 ep->data01 = 1;
805
806                 /* handle the usb command base on the usb_ctrlrequest */
807                 setup_received_handle(udc, &udc->local_setup_buff);
808                 return 0;
809         }
810         return -EINVAL;
811 }
812
813 static int qe_ep0_rx(struct qe_udc *udc)
814 {
815         struct qe_ep *ep = &udc->eps[0];
816         struct qe_frame *pframe;
817         struct qe_bd __iomem *bd;
818         u32 bdstatus, length;
819         u32 vaddr;
820
821         pframe = ep->rxframe;
822
823         if (ep->dir == USB_DIR_IN) {
824                 dev_err(udc->dev, "ep0 not a control endpoint\n");
825                 return -EINVAL;
826         }
827
828         bd = ep->n_rxbd;
829         bdstatus = in_be32((u32 __iomem *)bd);
830         length = bdstatus & BD_LENGTH_MASK;
831
832         while (!(bdstatus & R_E) && length) {
833                 if ((bdstatus & R_F) && (bdstatus & R_L)
834                         && !(bdstatus & R_ERROR)) {
835                         if (length == USB_CRC_SIZE) {
836                                 udc->ep0_state = WAIT_FOR_SETUP;
837                                 dev_vdbg(udc->dev,
838                                         "receive a ZLP in status phase\n");
839                         } else {
840                                 qe_frame_clean(pframe);
841                                 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
842                                 frame_set_data(pframe, (u8 *)vaddr);
843                                 frame_set_length(pframe,
844                                                 (length - USB_CRC_SIZE));
845                                 frame_set_status(pframe, FRAME_OK);
846                                 switch (bdstatus & R_PID) {
847                                 case R_PID_SETUP:
848                                         frame_set_info(pframe, PID_SETUP);
849                                         break;
850                                 case R_PID_DATA1:
851                                         frame_set_info(pframe, PID_DATA1);
852                                         break;
853                                 default:
854                                         frame_set_info(pframe, PID_DATA0);
855                                         break;
856                                 }
857
858                                 if ((bdstatus & R_PID) == R_PID_SETUP)
859                                         ep0_setup_handle(udc);
860                                 else
861                                         qe_ep_rxframe_handle(ep);
862                         }
863                 } else {
864                         dev_err(udc->dev, "The receive frame with error!\n");
865                 }
866
867                 /* note: don't clear the rxbd's buffer address */
868                 recycle_one_rxbd(ep);
869
870                 /* Get next BD */
871                 if (bdstatus & R_W)
872                         bd = ep->rxbase;
873                 else
874                         bd++;
875
876                 bdstatus = in_be32((u32 __iomem *)bd);
877                 length = bdstatus & BD_LENGTH_MASK;
878
879         }
880
881         ep->n_rxbd = bd;
882
883         return 0;
884 }
885
886 static int qe_ep_rxframe_handle(struct qe_ep *ep)
887 {
888         struct qe_frame *pframe;
889         u8 framepid = 0;
890         unsigned int fsize;
891         u8 *cp;
892         struct qe_req *req;
893
894         pframe = ep->rxframe;
895
896         if (frame_get_info(pframe) & PID_DATA1)
897                 framepid = 0x1;
898
899         if (framepid != ep->data01) {
900                 dev_err(ep->udc->dev, "the data01 error!\n");
901                 return -EIO;
902         }
903
904         fsize = frame_get_length(pframe);
905         if (list_empty(&ep->queue)) {
906                 dev_err(ep->udc->dev, "the %s have no requeue!\n", ep->name);
907         } else {
908                 req = list_entry(ep->queue.next, struct qe_req, queue);
909
910                 cp = (u8 *)(req->req.buf) + req->req.actual;
911                 if (cp) {
912                         memcpy(cp, pframe->data, fsize);
913                         req->req.actual += fsize;
914                         if ((fsize < ep->ep.maxpacket) ||
915                                         (req->req.actual >= req->req.length)) {
916                                 if (ep->epnum == 0)
917                                         ep0_req_complete(ep->udc, req);
918                                 else
919                                         done(ep, req, 0);
920                                 if (list_empty(&ep->queue) && ep->epnum != 0)
921                                         qe_eprx_nack(ep);
922                         }
923                 }
924         }
925
926         qe_ep_toggledata01(ep);
927
928         return 0;
929 }
930
931 static void ep_rx_tasklet(struct tasklet_struct *t)
932 {
933         struct qe_udc *udc = from_tasklet(udc, t, rx_tasklet);
934         struct qe_ep *ep;
935         struct qe_frame *pframe;
936         struct qe_bd __iomem *bd;
937         unsigned long flags;
938         u32 bdstatus, length;
939         u32 vaddr, i;
940
941         spin_lock_irqsave(&udc->lock, flags);
942
943         for (i = 1; i < USB_MAX_ENDPOINTS; i++) {
944                 ep = &udc->eps[i];
945
946                 if (ep->dir == USB_DIR_IN || ep->enable_tasklet == 0) {
947                         dev_dbg(udc->dev,
948                                 "This is a transmit ep or disable tasklet!\n");
949                         continue;
950                 }
951
952                 pframe = ep->rxframe;
953                 bd = ep->n_rxbd;
954                 bdstatus = in_be32((u32 __iomem *)bd);
955                 length = bdstatus & BD_LENGTH_MASK;
956
957                 while (!(bdstatus & R_E) && length) {
958                         if (list_empty(&ep->queue)) {
959                                 qe_eprx_nack(ep);
960                                 dev_dbg(udc->dev,
961                                         "The rxep have noreq %d\n",
962                                         ep->has_data);
963                                 break;
964                         }
965
966                         if ((bdstatus & R_F) && (bdstatus & R_L)
967                                 && !(bdstatus & R_ERROR)) {
968                                 qe_frame_clean(pframe);
969                                 vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
970                                 frame_set_data(pframe, (u8 *)vaddr);
971                                 frame_set_length(pframe,
972                                                 (length - USB_CRC_SIZE));
973                                 frame_set_status(pframe, FRAME_OK);
974                                 switch (bdstatus & R_PID) {
975                                 case R_PID_DATA1:
976                                         frame_set_info(pframe, PID_DATA1);
977                                         break;
978                                 case R_PID_SETUP:
979                                         frame_set_info(pframe, PID_SETUP);
980                                         break;
981                                 default:
982                                         frame_set_info(pframe, PID_DATA0);
983                                         break;
984                                 }
985                                 /* handle the rx frame */
986                                 qe_ep_rxframe_handle(ep);
987                         } else {
988                                 dev_err(udc->dev,
989                                         "error in received frame\n");
990                         }
991                         /* note: don't clear the rxbd's buffer address */
992                         /*clear the length */
993                         out_be32((u32 __iomem *)bd, bdstatus & BD_STATUS_MASK);
994                         ep->has_data--;
995                         if (!(ep->localnack))
996                                 recycle_one_rxbd(ep);
997
998                         /* Get next BD */
999                         if (bdstatus & R_W)
1000                                 bd = ep->rxbase;
1001                         else
1002                                 bd++;
1003
1004                         bdstatus = in_be32((u32 __iomem *)bd);
1005                         length = bdstatus & BD_LENGTH_MASK;
1006                 }
1007
1008                 ep->n_rxbd = bd;
1009
1010                 if (ep->localnack)
1011                         ep_recycle_rxbds(ep);
1012
1013                 ep->enable_tasklet = 0;
1014         } /* for i=1 */
1015
1016         spin_unlock_irqrestore(&udc->lock, flags);
1017 }
1018
1019 static int qe_ep_rx(struct qe_ep *ep)
1020 {
1021         struct qe_udc *udc;
1022         struct qe_frame *pframe;
1023         struct qe_bd __iomem *bd;
1024         u16 swoffs, ucoffs, emptybds;
1025
1026         udc = ep->udc;
1027         pframe = ep->rxframe;
1028
1029         if (ep->dir == USB_DIR_IN) {
1030                 dev_err(udc->dev, "transmit ep in rx function\n");
1031                 return -EINVAL;
1032         }
1033
1034         bd = ep->n_rxbd;
1035
1036         swoffs = (u16)(bd - ep->rxbase);
1037         ucoffs = (u16)((in_be16(&udc->ep_param[ep->epnum]->rbptr) -
1038                         in_be16(&udc->ep_param[ep->epnum]->rbase)) >> 3);
1039         if (swoffs < ucoffs)
1040                 emptybds = USB_BDRING_LEN_RX - ucoffs + swoffs;
1041         else
1042                 emptybds = swoffs - ucoffs;
1043
1044         if (emptybds < MIN_EMPTY_BDS) {
1045                 qe_eprx_nack(ep);
1046                 ep->localnack = 1;
1047                 dev_vdbg(udc->dev, "%d empty bds, send NACK\n", emptybds);
1048         }
1049         ep->has_data = USB_BDRING_LEN_RX - emptybds;
1050
1051         if (list_empty(&ep->queue)) {
1052                 qe_eprx_nack(ep);
1053                 dev_vdbg(udc->dev, "The rxep have no req queued with %d BDs\n",
1054                                 ep->has_data);
1055                 return 0;
1056         }
1057
1058         tasklet_schedule(&udc->rx_tasklet);
1059         ep->enable_tasklet = 1;
1060
1061         return 0;
1062 }
1063
1064 /* send data from a frame, no matter what tx_req */
1065 static int qe_ep_tx(struct qe_ep *ep, struct qe_frame *frame)
1066 {
1067         struct qe_udc *udc = ep->udc;
1068         struct qe_bd __iomem *bd;
1069         u16 saveusbmr;
1070         u32 bdstatus, pidmask;
1071         u32 paddr;
1072
1073         if (ep->dir == USB_DIR_OUT) {
1074                 dev_err(udc->dev, "receive ep passed to tx function\n");
1075                 return -EINVAL;
1076         }
1077
1078         /* Disable the Tx interrupt */
1079         saveusbmr = in_be16(&udc->usb_regs->usb_usbmr);
1080         out_be16(&udc->usb_regs->usb_usbmr,
1081                         saveusbmr & ~(USB_E_TXB_MASK | USB_E_TXE_MASK));
1082
1083         bd = ep->n_txbd;
1084         bdstatus = in_be32((u32 __iomem *)bd);
1085
1086         if (!(bdstatus & (T_R | BD_LENGTH_MASK))) {
1087                 if (frame_get_length(frame) == 0) {
1088                         frame_set_data(frame, udc->nullbuf);
1089                         frame_set_length(frame, 2);
1090                         frame->info |= (ZLP | NO_CRC);
1091                         dev_vdbg(udc->dev, "the frame size = 0\n");
1092                 }
1093                 paddr = virt_to_phys((void *)frame->data);
1094                 out_be32(&bd->buf, paddr);
1095                 bdstatus = (bdstatus&T_W);
1096                 if (!(frame_get_info(frame) & NO_CRC))
1097                         bdstatus |= T_R | T_I | T_L | T_TC
1098                                         | frame_get_length(frame);
1099                 else
1100                         bdstatus |= T_R | T_I | T_L | frame_get_length(frame);
1101
1102                 /* if the packet is a ZLP in status phase */
1103                 if ((ep->epnum == 0) && (udc->ep0_state == DATA_STATE_NEED_ZLP))
1104                         ep->data01 = 0x1;
1105
1106                 if (ep->data01) {
1107                         pidmask = T_PID_DATA1;
1108                         frame->info |= PID_DATA1;
1109                 } else {
1110                         pidmask = T_PID_DATA0;
1111                         frame->info |= PID_DATA0;
1112                 }
1113                 bdstatus |= T_CNF;
1114                 bdstatus |= pidmask;
1115                 out_be32((u32 __iomem *)bd, bdstatus);
1116                 qe_ep_filltxfifo(ep);
1117
1118                 /* enable the TX interrupt */
1119                 out_be16(&udc->usb_regs->usb_usbmr, saveusbmr);
1120
1121                 qe_ep_toggledata01(ep);
1122                 if (bdstatus & T_W)
1123                         ep->n_txbd = ep->txbase;
1124                 else
1125                         ep->n_txbd++;
1126
1127                 return 0;
1128         } else {
1129                 out_be16(&udc->usb_regs->usb_usbmr, saveusbmr);
1130                 dev_vdbg(udc->dev, "The tx bd is not ready!\n");
1131                 return -EBUSY;
1132         }
1133 }
1134
1135 /* when a bd was transmitted, the function can
1136  * handle the tx_req, not include ep0           */
1137 static int txcomplete(struct qe_ep *ep, unsigned char restart)
1138 {
1139         if (ep->tx_req != NULL) {
1140                 struct qe_req *req = ep->tx_req;
1141                 unsigned zlp = 0, last_len = 0;
1142
1143                 last_len = min_t(unsigned, req->req.length - ep->sent,
1144                                 ep->ep.maxpacket);
1145
1146                 if (!restart) {
1147                         int asent = ep->last;
1148                         ep->sent += asent;
1149                         ep->last -= asent;
1150                 } else {
1151                         ep->last = 0;
1152                 }
1153
1154                 /* zlp needed when req->re.zero is set */
1155                 if (req->req.zero) {
1156                         if (last_len == 0 ||
1157                                 (req->req.length % ep->ep.maxpacket) != 0)
1158                                 zlp = 0;
1159                         else
1160                                 zlp = 1;
1161                 } else
1162                         zlp = 0;
1163
1164                 /* a request already were transmitted completely */
1165                 if (((ep->tx_req->req.length - ep->sent) <= 0) && !zlp) {
1166                         done(ep, ep->tx_req, 0);
1167                         ep->tx_req = NULL;
1168                         ep->last = 0;
1169                         ep->sent = 0;
1170                 }
1171         }
1172
1173         /* we should gain a new tx_req fot this endpoint */
1174         if (ep->tx_req == NULL) {
1175                 if (!list_empty(&ep->queue)) {
1176                         ep->tx_req = list_entry(ep->queue.next, struct qe_req,
1177                                                         queue);
1178                         ep->last = 0;
1179                         ep->sent = 0;
1180                 }
1181         }
1182
1183         return 0;
1184 }
1185
1186 /* give a frame and a tx_req, send some data */
1187 static int qe_usb_senddata(struct qe_ep *ep, struct qe_frame *frame)
1188 {
1189         unsigned int size;
1190         u8 *buf;
1191
1192         qe_frame_clean(frame);
1193         size = min_t(u32, (ep->tx_req->req.length - ep->sent),
1194                                 ep->ep.maxpacket);
1195         buf = (u8 *)ep->tx_req->req.buf + ep->sent;
1196         if (buf && size) {
1197                 ep->last = size;
1198                 ep->tx_req->req.actual += size;
1199                 frame_set_data(frame, buf);
1200                 frame_set_length(frame, size);
1201                 frame_set_status(frame, FRAME_OK);
1202                 frame_set_info(frame, 0);
1203                 return qe_ep_tx(ep, frame);
1204         }
1205         return -EIO;
1206 }
1207
1208 /* give a frame struct,send a ZLP */
1209 static int sendnulldata(struct qe_ep *ep, struct qe_frame *frame, uint infor)
1210 {
1211         struct qe_udc *udc = ep->udc;
1212
1213         if (frame == NULL)
1214                 return -ENODEV;
1215
1216         qe_frame_clean(frame);
1217         frame_set_data(frame, (u8 *)udc->nullbuf);
1218         frame_set_length(frame, 2);
1219         frame_set_status(frame, FRAME_OK);
1220         frame_set_info(frame, (ZLP | NO_CRC | infor));
1221
1222         return qe_ep_tx(ep, frame);
1223 }
1224
1225 static int frame_create_tx(struct qe_ep *ep, struct qe_frame *frame)
1226 {
1227         struct qe_req *req = ep->tx_req;
1228         int reval;
1229
1230         if (req == NULL)
1231                 return -ENODEV;
1232
1233         if ((req->req.length - ep->sent) > 0)
1234                 reval = qe_usb_senddata(ep, frame);
1235         else
1236                 reval = sendnulldata(ep, frame, 0);
1237
1238         return reval;
1239 }
1240
1241 /* if direction is DIR_IN, the status is Device->Host
1242  * if direction is DIR_OUT, the status transaction is Device<-Host
1243  * in status phase, udc create a request and gain status */
1244 static int ep0_prime_status(struct qe_udc *udc, int direction)
1245 {
1246
1247         struct qe_ep *ep = &udc->eps[0];
1248
1249         if (direction == USB_DIR_IN) {
1250                 udc->ep0_state = DATA_STATE_NEED_ZLP;
1251                 udc->ep0_dir = USB_DIR_IN;
1252                 sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ);
1253         } else {
1254                 udc->ep0_dir = USB_DIR_OUT;
1255                 udc->ep0_state = WAIT_FOR_OUT_STATUS;
1256         }
1257
1258         return 0;
1259 }
1260
1261 /* a request complete in ep0, whether gadget request or udc request */
1262 static void ep0_req_complete(struct qe_udc *udc, struct qe_req *req)
1263 {
1264         struct qe_ep *ep = &udc->eps[0];
1265         /* because usb and ep's status already been set in ch9setaddress() */
1266
1267         switch (udc->ep0_state) {
1268         case DATA_STATE_XMIT:
1269                 done(ep, req, 0);
1270                 /* receive status phase */
1271                 if (ep0_prime_status(udc, USB_DIR_OUT))
1272                         qe_ep0_stall(udc);
1273                 break;
1274
1275         case DATA_STATE_NEED_ZLP:
1276                 done(ep, req, 0);
1277                 udc->ep0_state = WAIT_FOR_SETUP;
1278                 break;
1279
1280         case DATA_STATE_RECV:
1281                 done(ep, req, 0);
1282                 /* send status phase */
1283                 if (ep0_prime_status(udc, USB_DIR_IN))
1284                         qe_ep0_stall(udc);
1285                 break;
1286
1287         case WAIT_FOR_OUT_STATUS:
1288                 done(ep, req, 0);
1289                 udc->ep0_state = WAIT_FOR_SETUP;
1290                 break;
1291
1292         case WAIT_FOR_SETUP:
1293                 dev_vdbg(udc->dev, "Unexpected interrupt\n");
1294                 break;
1295
1296         default:
1297                 qe_ep0_stall(udc);
1298                 break;
1299         }
1300 }
1301
1302 static int ep0_txcomplete(struct qe_ep *ep, unsigned char restart)
1303 {
1304         struct qe_req *tx_req = NULL;
1305         struct qe_frame *frame = ep->txframe;
1306
1307         if ((frame_get_info(frame) & (ZLP | NO_REQ)) == (ZLP | NO_REQ)) {
1308                 if (!restart)
1309                         ep->udc->ep0_state = WAIT_FOR_SETUP;
1310                 else
1311                         sendnulldata(ep, ep->txframe, SETUP_STATUS | NO_REQ);
1312                 return 0;
1313         }
1314
1315         tx_req = ep->tx_req;
1316         if (tx_req != NULL) {
1317                 if (!restart) {
1318                         int asent = ep->last;
1319                         ep->sent += asent;
1320                         ep->last -= asent;
1321                 } else {
1322                         ep->last = 0;
1323                 }
1324
1325                 /* a request already were transmitted completely */
1326                 if ((ep->tx_req->req.length - ep->sent) <= 0) {
1327                         ep->tx_req->req.actual = (unsigned int)ep->sent;
1328                         ep0_req_complete(ep->udc, ep->tx_req);
1329                         ep->tx_req = NULL;
1330                         ep->last = 0;
1331                         ep->sent = 0;
1332                 }
1333         } else {
1334                 dev_vdbg(ep->udc->dev, "the ep0_controller have no req\n");
1335         }
1336
1337         return 0;
1338 }
1339
1340 static int ep0_txframe_handle(struct qe_ep *ep)
1341 {
1342         /* if have error, transmit again */
1343         if (frame_get_status(ep->txframe) & FRAME_ERROR) {
1344                 qe_ep_flushtxfifo(ep);
1345                 dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n");
1346                 if (frame_get_info(ep->txframe) & PID_DATA0)
1347                         ep->data01 = 0;
1348                 else
1349                         ep->data01 = 1;
1350
1351                 ep0_txcomplete(ep, 1);
1352         } else
1353                 ep0_txcomplete(ep, 0);
1354
1355         frame_create_tx(ep, ep->txframe);
1356         return 0;
1357 }
1358
1359 static int qe_ep0_txconf(struct qe_ep *ep)
1360 {
1361         struct qe_bd __iomem *bd;
1362         struct qe_frame *pframe;
1363         u32 bdstatus;
1364
1365         bd = ep->c_txbd;
1366         bdstatus = in_be32((u32 __iomem *)bd);
1367         while (!(bdstatus & T_R) && (bdstatus & ~T_W)) {
1368                 pframe = ep->txframe;
1369
1370                 /* clear and recycle the BD */
1371                 out_be32((u32 __iomem *)bd, bdstatus & T_W);
1372                 out_be32(&bd->buf, 0);
1373                 if (bdstatus & T_W)
1374                         ep->c_txbd = ep->txbase;
1375                 else
1376                         ep->c_txbd++;
1377
1378                 if (ep->c_txbd == ep->n_txbd) {
1379                         if (bdstatus & DEVICE_T_ERROR) {
1380                                 frame_set_status(pframe, FRAME_ERROR);
1381                                 if (bdstatus & T_TO)
1382                                         pframe->status |= TX_ER_TIMEOUT;
1383                                 if (bdstatus & T_UN)
1384                                         pframe->status |= TX_ER_UNDERUN;
1385                         }
1386                         ep0_txframe_handle(ep);
1387                 }
1388
1389                 bd = ep->c_txbd;
1390                 bdstatus = in_be32((u32 __iomem *)bd);
1391         }
1392
1393         return 0;
1394 }
1395
1396 static int ep_txframe_handle(struct qe_ep *ep)
1397 {
1398         if (frame_get_status(ep->txframe) & FRAME_ERROR) {
1399                 qe_ep_flushtxfifo(ep);
1400                 dev_vdbg(ep->udc->dev, "The EP0 transmit data have error!\n");
1401                 if (frame_get_info(ep->txframe) & PID_DATA0)
1402                         ep->data01 = 0;
1403                 else
1404                         ep->data01 = 1;
1405
1406                 txcomplete(ep, 1);
1407         } else
1408                 txcomplete(ep, 0);
1409
1410         frame_create_tx(ep, ep->txframe); /* send the data */
1411         return 0;
1412 }
1413
1414 /* confirm the already trainsmited bd */
1415 static int qe_ep_txconf(struct qe_ep *ep)
1416 {
1417         struct qe_bd __iomem *bd;
1418         struct qe_frame *pframe = NULL;
1419         u32 bdstatus;
1420         unsigned char breakonrxinterrupt = 0;
1421
1422         bd = ep->c_txbd;
1423         bdstatus = in_be32((u32 __iomem *)bd);
1424         while (!(bdstatus & T_R) && (bdstatus & ~T_W)) {
1425                 pframe = ep->txframe;
1426                 if (bdstatus & DEVICE_T_ERROR) {
1427                         frame_set_status(pframe, FRAME_ERROR);
1428                         if (bdstatus & T_TO)
1429                                 pframe->status |= TX_ER_TIMEOUT;
1430                         if (bdstatus & T_UN)
1431                                 pframe->status |= TX_ER_UNDERUN;
1432                 }
1433
1434                 /* clear and recycle the BD */
1435                 out_be32((u32 __iomem *)bd, bdstatus & T_W);
1436                 out_be32(&bd->buf, 0);
1437                 if (bdstatus & T_W)
1438                         ep->c_txbd = ep->txbase;
1439                 else
1440                         ep->c_txbd++;
1441
1442                 /* handle the tx frame */
1443                 ep_txframe_handle(ep);
1444                 bd = ep->c_txbd;
1445                 bdstatus = in_be32((u32 __iomem *)bd);
1446         }
1447         if (breakonrxinterrupt)
1448                 return -EIO;
1449         else
1450                 return 0;
1451 }
1452
1453 /* Add a request in queue, and try to transmit a packet */
1454 static int ep_req_send(struct qe_ep *ep, struct qe_req *req)
1455 {
1456         int reval = 0;
1457
1458         if (ep->tx_req == NULL) {
1459                 ep->sent = 0;
1460                 ep->last = 0;
1461                 txcomplete(ep, 0); /* can gain a new tx_req */
1462                 reval = frame_create_tx(ep, ep->txframe);
1463         }
1464         return reval;
1465 }
1466
1467 /* Maybe this is a good ideal */
1468 static int ep_req_rx(struct qe_ep *ep, struct qe_req *req)
1469 {
1470         struct qe_udc *udc = ep->udc;
1471         struct qe_frame *pframe = NULL;
1472         struct qe_bd __iomem *bd;
1473         u32 bdstatus, length;
1474         u32 vaddr, fsize;
1475         u8 *cp;
1476         u8 finish_req = 0;
1477         u8 framepid;
1478
1479         if (list_empty(&ep->queue)) {
1480                 dev_vdbg(udc->dev, "the req already finish!\n");
1481                 return 0;
1482         }
1483         pframe = ep->rxframe;
1484
1485         bd = ep->n_rxbd;
1486         bdstatus = in_be32((u32 __iomem *)bd);
1487         length = bdstatus & BD_LENGTH_MASK;
1488
1489         while (!(bdstatus & R_E) && length) {
1490                 if (finish_req)
1491                         break;
1492                 if ((bdstatus & R_F) && (bdstatus & R_L)
1493                                         && !(bdstatus & R_ERROR)) {
1494                         qe_frame_clean(pframe);
1495                         vaddr = (u32)phys_to_virt(in_be32(&bd->buf));
1496                         frame_set_data(pframe, (u8 *)vaddr);
1497                         frame_set_length(pframe, (length - USB_CRC_SIZE));
1498                         frame_set_status(pframe, FRAME_OK);
1499                         switch (bdstatus & R_PID) {
1500                         case R_PID_DATA1:
1501                                 frame_set_info(pframe, PID_DATA1); break;
1502                         default:
1503                                 frame_set_info(pframe, PID_DATA0); break;
1504                         }
1505                         /* handle the rx frame */
1506
1507                         if (frame_get_info(pframe) & PID_DATA1)
1508                                 framepid = 0x1;
1509                         else
1510                                 framepid = 0;
1511
1512                         if (framepid != ep->data01) {
1513                                 dev_vdbg(udc->dev, "the data01 error!\n");
1514                         } else {
1515                                 fsize = frame_get_length(pframe);
1516
1517                                 cp = (u8 *)(req->req.buf) + req->req.actual;
1518                                 if (cp) {
1519                                         memcpy(cp, pframe->data, fsize);
1520                                         req->req.actual += fsize;
1521                                         if ((fsize < ep->ep.maxpacket)
1522                                                 || (req->req.actual >=
1523                                                         req->req.length)) {
1524                                                 finish_req = 1;
1525                                                 done(ep, req, 0);
1526                                                 if (list_empty(&ep->queue))
1527                                                         qe_eprx_nack(ep);
1528                                         }
1529                                 }
1530                                 qe_ep_toggledata01(ep);
1531                         }
1532                 } else {
1533                         dev_err(udc->dev, "The receive frame with error!\n");
1534                 }
1535
1536                 /* note: don't clear the rxbd's buffer address *
1537                  * only Clear the length */
1538                 out_be32((u32 __iomem *)bd, (bdstatus & BD_STATUS_MASK));
1539                 ep->has_data--;
1540
1541                 /* Get next BD */
1542                 if (bdstatus & R_W)
1543                         bd = ep->rxbase;
1544                 else
1545                         bd++;
1546
1547                 bdstatus = in_be32((u32 __iomem *)bd);
1548                 length = bdstatus & BD_LENGTH_MASK;
1549         }
1550
1551         ep->n_rxbd = bd;
1552         ep_recycle_rxbds(ep);
1553
1554         return 0;
1555 }
1556
1557 /* only add the request in queue */
1558 static int ep_req_receive(struct qe_ep *ep, struct qe_req *req)
1559 {
1560         if (ep->state == EP_STATE_NACK) {
1561                 if (ep->has_data <= 0) {
1562                         /* Enable rx and unmask rx interrupt */
1563                         qe_eprx_normal(ep);
1564                 } else {
1565                         /* Copy the exist BD data */
1566                         ep_req_rx(ep, req);
1567                 }
1568         }
1569
1570         return 0;
1571 }
1572
1573 /********************************************************************
1574         Internal Used Function End
1575 ********************************************************************/
1576
1577 /*-----------------------------------------------------------------------
1578         Endpoint Management Functions For Gadget
1579  -----------------------------------------------------------------------*/
1580 static int qe_ep_enable(struct usb_ep *_ep,
1581                          const struct usb_endpoint_descriptor *desc)
1582 {
1583         struct qe_udc *udc;
1584         struct qe_ep *ep;
1585         int retval = 0;
1586         unsigned char epnum;
1587
1588         ep = container_of(_ep, struct qe_ep, ep);
1589
1590         /* catch various bogus parameters */
1591         if (!_ep || !desc || _ep->name == ep_name[0] ||
1592                         (desc->bDescriptorType != USB_DT_ENDPOINT))
1593                 return -EINVAL;
1594
1595         udc = ep->udc;
1596         if (!udc->driver || (udc->gadget.speed == USB_SPEED_UNKNOWN))
1597                 return -ESHUTDOWN;
1598
1599         epnum = (u8)desc->bEndpointAddress & 0xF;
1600
1601         retval = qe_ep_init(udc, epnum, desc);
1602         if (retval != 0) {
1603                 cpm_muram_free(cpm_muram_offset(ep->rxbase));
1604                 dev_dbg(udc->dev, "enable ep%d failed\n", ep->epnum);
1605                 return -EINVAL;
1606         }
1607         dev_dbg(udc->dev, "enable ep%d successful\n", ep->epnum);
1608         return 0;
1609 }
1610
1611 static int qe_ep_disable(struct usb_ep *_ep)
1612 {
1613         struct qe_udc *udc;
1614         struct qe_ep *ep;
1615         unsigned long flags;
1616         unsigned int size;
1617
1618         ep = container_of(_ep, struct qe_ep, ep);
1619         udc = ep->udc;
1620
1621         if (!_ep || !ep->ep.desc) {
1622                 dev_dbg(udc->dev, "%s not enabled\n", _ep ? ep->ep.name : NULL);
1623                 return -EINVAL;
1624         }
1625
1626         spin_lock_irqsave(&udc->lock, flags);
1627         /* Nuke all pending requests (does flush) */
1628         nuke(ep, -ESHUTDOWN);
1629         ep->ep.desc = NULL;
1630         ep->stopped = 1;
1631         ep->tx_req = NULL;
1632         qe_ep_reset(udc, ep->epnum);
1633         spin_unlock_irqrestore(&udc->lock, flags);
1634
1635         cpm_muram_free(cpm_muram_offset(ep->rxbase));
1636
1637         if (ep->dir == USB_DIR_OUT)
1638                 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) *
1639                                 (USB_BDRING_LEN_RX + 1);
1640         else
1641                 size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) *
1642                                 (USB_BDRING_LEN + 1);
1643
1644         if (ep->dir != USB_DIR_IN) {
1645                 kfree(ep->rxframe);
1646                 if (ep->rxbufmap) {
1647                         dma_unmap_single(udc->gadget.dev.parent,
1648                                         ep->rxbuf_d, size,
1649                                         DMA_FROM_DEVICE);
1650                         ep->rxbuf_d = DMA_ADDR_INVALID;
1651                 } else {
1652                         dma_sync_single_for_cpu(
1653                                         udc->gadget.dev.parent,
1654                                         ep->rxbuf_d, size,
1655                                         DMA_FROM_DEVICE);
1656                 }
1657                 kfree(ep->rxbuffer);
1658         }
1659
1660         if (ep->dir != USB_DIR_OUT)
1661                 kfree(ep->txframe);
1662
1663         dev_dbg(udc->dev, "disabled %s OK\n", _ep->name);
1664         return 0;
1665 }
1666
1667 static struct usb_request *qe_alloc_request(struct usb_ep *_ep, gfp_t gfp_flags)
1668 {
1669         struct qe_req *req;
1670
1671         req = kzalloc(sizeof(*req), gfp_flags);
1672         if (!req)
1673                 return NULL;
1674
1675         req->req.dma = DMA_ADDR_INVALID;
1676
1677         INIT_LIST_HEAD(&req->queue);
1678
1679         return &req->req;
1680 }
1681
1682 static void qe_free_request(struct usb_ep *_ep, struct usb_request *_req)
1683 {
1684         struct qe_req *req;
1685
1686         req = container_of(_req, struct qe_req, req);
1687
1688         if (_req)
1689                 kfree(req);
1690 }
1691
1692 static int __qe_ep_queue(struct usb_ep *_ep, struct usb_request *_req)
1693 {
1694         struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1695         struct qe_req *req = container_of(_req, struct qe_req, req);
1696         struct qe_udc *udc;
1697         int reval;
1698
1699         udc = ep->udc;
1700         /* catch various bogus parameters */
1701         if (!_req || !req->req.complete || !req->req.buf
1702                         || !list_empty(&req->queue)) {
1703                 dev_dbg(udc->dev, "bad params\n");
1704                 return -EINVAL;
1705         }
1706         if (!_ep || (!ep->ep.desc && ep_index(ep))) {
1707                 dev_dbg(udc->dev, "bad ep\n");
1708                 return -EINVAL;
1709         }
1710
1711         if (!udc->driver || udc->gadget.speed == USB_SPEED_UNKNOWN)
1712                 return -ESHUTDOWN;
1713
1714         req->ep = ep;
1715
1716         /* map virtual address to hardware */
1717         if (req->req.dma == DMA_ADDR_INVALID) {
1718                 req->req.dma = dma_map_single(ep->udc->gadget.dev.parent,
1719                                         req->req.buf,
1720                                         req->req.length,
1721                                         ep_is_in(ep)
1722                                         ? DMA_TO_DEVICE :
1723                                         DMA_FROM_DEVICE);
1724                 req->mapped = 1;
1725         } else {
1726                 dma_sync_single_for_device(ep->udc->gadget.dev.parent,
1727                                         req->req.dma, req->req.length,
1728                                         ep_is_in(ep)
1729                                         ? DMA_TO_DEVICE :
1730                                         DMA_FROM_DEVICE);
1731                 req->mapped = 0;
1732         }
1733
1734         req->req.status = -EINPROGRESS;
1735         req->req.actual = 0;
1736
1737         list_add_tail(&req->queue, &ep->queue);
1738         dev_vdbg(udc->dev, "gadget have request in %s! %d\n",
1739                         ep->name, req->req.length);
1740
1741         /* push the request to device */
1742         if (ep_is_in(ep))
1743                 reval = ep_req_send(ep, req);
1744
1745         /* EP0 */
1746         if (ep_index(ep) == 0 && req->req.length > 0) {
1747                 if (ep_is_in(ep))
1748                         udc->ep0_state = DATA_STATE_XMIT;
1749                 else
1750                         udc->ep0_state = DATA_STATE_RECV;
1751         }
1752
1753         if (ep->dir == USB_DIR_OUT)
1754                 reval = ep_req_receive(ep, req);
1755
1756         return 0;
1757 }
1758
1759 /* queues (submits) an I/O request to an endpoint */
1760 static int qe_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
1761                        gfp_t gfp_flags)
1762 {
1763         struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1764         struct qe_udc *udc = ep->udc;
1765         unsigned long flags;
1766         int ret;
1767
1768         spin_lock_irqsave(&udc->lock, flags);
1769         ret = __qe_ep_queue(_ep, _req);
1770         spin_unlock_irqrestore(&udc->lock, flags);
1771         return ret;
1772 }
1773
1774 /* dequeues (cancels, unlinks) an I/O request from an endpoint */
1775 static int qe_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
1776 {
1777         struct qe_ep *ep = container_of(_ep, struct qe_ep, ep);
1778         struct qe_req *req;
1779         unsigned long flags;
1780
1781         if (!_ep || !_req)
1782                 return -EINVAL;
1783
1784         spin_lock_irqsave(&ep->udc->lock, flags);
1785
1786         /* make sure it's actually queued on this endpoint */
1787         list_for_each_entry(req, &ep->queue, queue) {
1788                 if (&req->req == _req)
1789                         break;
1790         }
1791
1792         if (&req->req != _req) {
1793                 spin_unlock_irqrestore(&ep->udc->lock, flags);
1794                 return -EINVAL;
1795         }
1796
1797         done(ep, req, -ECONNRESET);
1798
1799         spin_unlock_irqrestore(&ep->udc->lock, flags);
1800         return 0;
1801 }
1802
1803 /*-----------------------------------------------------------------
1804  * modify the endpoint halt feature
1805  * @ep: the non-isochronous endpoint being stalled
1806  * @value: 1--set halt  0--clear halt
1807  * Returns zero, or a negative error code.
1808 *----------------------------------------------------------------*/
1809 static int qe_ep_set_halt(struct usb_ep *_ep, int value)
1810 {
1811         struct qe_ep *ep;
1812         unsigned long flags;
1813         int status = -EOPNOTSUPP;
1814         struct qe_udc *udc;
1815
1816         ep = container_of(_ep, struct qe_ep, ep);
1817         if (!_ep || !ep->ep.desc) {
1818                 status = -EINVAL;
1819                 goto out;
1820         }
1821
1822         udc = ep->udc;
1823         /* Attempt to halt IN ep will fail if any transfer requests
1824          * are still queue */
1825         if (value && ep_is_in(ep) && !list_empty(&ep->queue)) {
1826                 status = -EAGAIN;
1827                 goto out;
1828         }
1829
1830         status = 0;
1831         spin_lock_irqsave(&ep->udc->lock, flags);
1832         qe_eptx_stall_change(ep, value);
1833         qe_eprx_stall_change(ep, value);
1834         spin_unlock_irqrestore(&ep->udc->lock, flags);
1835
1836         if (ep->epnum == 0) {
1837                 udc->ep0_state = WAIT_FOR_SETUP;
1838                 udc->ep0_dir = 0;
1839         }
1840
1841         /* set data toggle to DATA0 on clear halt */
1842         if (value == 0)
1843                 ep->data01 = 0;
1844 out:
1845         dev_vdbg(udc->dev, "%s %s halt stat %d\n", ep->ep.name,
1846                         value ?  "set" : "clear", status);
1847
1848         return status;
1849 }
1850
1851 static const struct usb_ep_ops qe_ep_ops = {
1852         .enable = qe_ep_enable,
1853         .disable = qe_ep_disable,
1854
1855         .alloc_request = qe_alloc_request,
1856         .free_request = qe_free_request,
1857
1858         .queue = qe_ep_queue,
1859         .dequeue = qe_ep_dequeue,
1860
1861         .set_halt = qe_ep_set_halt,
1862 };
1863
1864 /*------------------------------------------------------------------------
1865         Gadget Driver Layer Operations
1866  ------------------------------------------------------------------------*/
1867
1868 /* Get the current frame number */
1869 static int qe_get_frame(struct usb_gadget *gadget)
1870 {
1871         struct qe_udc *udc = container_of(gadget, struct qe_udc, gadget);
1872         u16 tmp;
1873
1874         tmp = in_be16(&udc->usb_param->frame_n);
1875         if (tmp & 0x8000)
1876                 return tmp & 0x07ff;
1877         return -EINVAL;
1878 }
1879
1880 static int fsl_qe_start(struct usb_gadget *gadget,
1881                 struct usb_gadget_driver *driver);
1882 static int fsl_qe_stop(struct usb_gadget *gadget);
1883
1884 /* defined in usb_gadget.h */
1885 static const struct usb_gadget_ops qe_gadget_ops = {
1886         .get_frame = qe_get_frame,
1887         .udc_start = fsl_qe_start,
1888         .udc_stop = fsl_qe_stop,
1889 };
1890
1891 /*-------------------------------------------------------------------------
1892         USB ep0 Setup process in BUS Enumeration
1893  -------------------------------------------------------------------------*/
1894 static int udc_reset_ep_queue(struct qe_udc *udc, u8 pipe)
1895 {
1896         struct qe_ep *ep = &udc->eps[pipe];
1897
1898         nuke(ep, -ECONNRESET);
1899         ep->tx_req = NULL;
1900         return 0;
1901 }
1902
1903 static int reset_queues(struct qe_udc *udc)
1904 {
1905         u8 pipe;
1906
1907         for (pipe = 0; pipe < USB_MAX_ENDPOINTS; pipe++)
1908                 udc_reset_ep_queue(udc, pipe);
1909
1910         /* report disconnect; the driver is already quiesced */
1911         spin_unlock(&udc->lock);
1912         usb_gadget_udc_reset(&udc->gadget, udc->driver);
1913         spin_lock(&udc->lock);
1914
1915         return 0;
1916 }
1917
1918 static void ch9setaddress(struct qe_udc *udc, u16 value, u16 index,
1919                         u16 length)
1920 {
1921         /* Save the new address to device struct */
1922         udc->device_address = (u8) value;
1923         /* Update usb state */
1924         udc->usb_state = USB_STATE_ADDRESS;
1925
1926         /* Status phase , send a ZLP */
1927         if (ep0_prime_status(udc, USB_DIR_IN))
1928                 qe_ep0_stall(udc);
1929 }
1930
1931 static void ownercomplete(struct usb_ep *_ep, struct usb_request *_req)
1932 {
1933         struct qe_req *req = container_of(_req, struct qe_req, req);
1934
1935         req->req.buf = NULL;
1936         kfree(req);
1937 }
1938
1939 static void ch9getstatus(struct qe_udc *udc, u8 request_type, u16 value,
1940                         u16 index, u16 length)
1941 {
1942         u16 usb_status = 0;
1943         struct qe_req *req;
1944         struct qe_ep *ep;
1945         int status = 0;
1946
1947         ep = &udc->eps[0];
1948         if ((request_type & USB_RECIP_MASK) == USB_RECIP_DEVICE) {
1949                 /* Get device status */
1950                 usb_status = 1 << USB_DEVICE_SELF_POWERED;
1951         } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_INTERFACE) {
1952                 /* Get interface status */
1953                 /* We don't have interface information in udc driver */
1954                 usb_status = 0;
1955         } else if ((request_type & USB_RECIP_MASK) == USB_RECIP_ENDPOINT) {
1956                 /* Get endpoint status */
1957                 int pipe = index & USB_ENDPOINT_NUMBER_MASK;
1958                 struct qe_ep *target_ep = &udc->eps[pipe];
1959                 u16 usep;
1960
1961                 /* stall if endpoint doesn't exist */
1962                 if (!target_ep->ep.desc)
1963                         goto stall;
1964
1965                 usep = in_be16(&udc->usb_regs->usb_usep[pipe]);
1966                 if (index & USB_DIR_IN) {
1967                         if (target_ep->dir != USB_DIR_IN)
1968                                 goto stall;
1969                         if ((usep & USB_THS_MASK) == USB_THS_STALL)
1970                                 usb_status = 1 << USB_ENDPOINT_HALT;
1971                 } else {
1972                         if (target_ep->dir != USB_DIR_OUT)
1973                                 goto stall;
1974                         if ((usep & USB_RHS_MASK) == USB_RHS_STALL)
1975                                 usb_status = 1 << USB_ENDPOINT_HALT;
1976                 }
1977         }
1978
1979         req = container_of(qe_alloc_request(&ep->ep, GFP_KERNEL),
1980                                         struct qe_req, req);
1981         req->req.length = 2;
1982         req->req.buf = udc->statusbuf;
1983         *(u16 *)req->req.buf = cpu_to_le16(usb_status);
1984         req->req.status = -EINPROGRESS;
1985         req->req.actual = 0;
1986         req->req.complete = ownercomplete;
1987
1988         udc->ep0_dir = USB_DIR_IN;
1989
1990         /* data phase */
1991         status = __qe_ep_queue(&ep->ep, &req->req);
1992
1993         if (status == 0)
1994                 return;
1995 stall:
1996         dev_err(udc->dev, "Can't respond to getstatus request \n");
1997         qe_ep0_stall(udc);
1998 }
1999
2000 /* only handle the setup request, suppose the device in normal status */
2001 static void setup_received_handle(struct qe_udc *udc,
2002                                 struct usb_ctrlrequest *setup)
2003 {
2004         /* Fix Endian (udc->local_setup_buff is cpu Endian now)*/
2005         u16 wValue = le16_to_cpu(setup->wValue);
2006         u16 wIndex = le16_to_cpu(setup->wIndex);
2007         u16 wLength = le16_to_cpu(setup->wLength);
2008
2009         /* clear the previous request in the ep0 */
2010         udc_reset_ep_queue(udc, 0);
2011
2012         if (setup->bRequestType & USB_DIR_IN)
2013                 udc->ep0_dir = USB_DIR_IN;
2014         else
2015                 udc->ep0_dir = USB_DIR_OUT;
2016
2017         switch (setup->bRequest) {
2018         case USB_REQ_GET_STATUS:
2019                 /* Data+Status phase form udc */
2020                 if ((setup->bRequestType & (USB_DIR_IN | USB_TYPE_MASK))
2021                                         != (USB_DIR_IN | USB_TYPE_STANDARD))
2022                         break;
2023                 ch9getstatus(udc, setup->bRequestType, wValue, wIndex,
2024                                         wLength);
2025                 return;
2026
2027         case USB_REQ_SET_ADDRESS:
2028                 /* Status phase from udc */
2029                 if (setup->bRequestType != (USB_DIR_OUT | USB_TYPE_STANDARD |
2030                                                 USB_RECIP_DEVICE))
2031                         break;
2032                 ch9setaddress(udc, wValue, wIndex, wLength);
2033                 return;
2034
2035         case USB_REQ_CLEAR_FEATURE:
2036         case USB_REQ_SET_FEATURE:
2037                 /* Requests with no data phase, status phase from udc */
2038                 if ((setup->bRequestType & USB_TYPE_MASK)
2039                                         != USB_TYPE_STANDARD)
2040                         break;
2041
2042                 if ((setup->bRequestType & USB_RECIP_MASK)
2043                                 == USB_RECIP_ENDPOINT) {
2044                         int pipe = wIndex & USB_ENDPOINT_NUMBER_MASK;
2045                         struct qe_ep *ep;
2046
2047                         if (wValue != 0 || wLength != 0
2048                                 || pipe >= USB_MAX_ENDPOINTS)
2049                                 break;
2050                         ep = &udc->eps[pipe];
2051
2052                         spin_unlock(&udc->lock);
2053                         qe_ep_set_halt(&ep->ep,
2054                                         (setup->bRequest == USB_REQ_SET_FEATURE)
2055                                                 ? 1 : 0);
2056                         spin_lock(&udc->lock);
2057                 }
2058
2059                 ep0_prime_status(udc, USB_DIR_IN);
2060
2061                 return;
2062
2063         default:
2064                 break;
2065         }
2066
2067         if (wLength) {
2068                 /* Data phase from gadget, status phase from udc */
2069                 if (setup->bRequestType & USB_DIR_IN) {
2070                         udc->ep0_state = DATA_STATE_XMIT;
2071                         udc->ep0_dir = USB_DIR_IN;
2072                 } else {
2073                         udc->ep0_state = DATA_STATE_RECV;
2074                         udc->ep0_dir = USB_DIR_OUT;
2075                 }
2076                 spin_unlock(&udc->lock);
2077                 if (udc->driver->setup(&udc->gadget,
2078                                         &udc->local_setup_buff) < 0)
2079                         qe_ep0_stall(udc);
2080                 spin_lock(&udc->lock);
2081         } else {
2082                 /* No data phase, IN status from gadget */
2083                 udc->ep0_dir = USB_DIR_IN;
2084                 spin_unlock(&udc->lock);
2085                 if (udc->driver->setup(&udc->gadget,
2086                                         &udc->local_setup_buff) < 0)
2087                         qe_ep0_stall(udc);
2088                 spin_lock(&udc->lock);
2089                 udc->ep0_state = DATA_STATE_NEED_ZLP;
2090         }
2091 }
2092
2093 /*-------------------------------------------------------------------------
2094         USB Interrupt handlers
2095  -------------------------------------------------------------------------*/
2096 static void suspend_irq(struct qe_udc *udc)
2097 {
2098         udc->resume_state = udc->usb_state;
2099         udc->usb_state = USB_STATE_SUSPENDED;
2100
2101         /* report suspend to the driver ,serial.c not support this*/
2102         if (udc->driver->suspend)
2103                 udc->driver->suspend(&udc->gadget);
2104 }
2105
2106 static void resume_irq(struct qe_udc *udc)
2107 {
2108         udc->usb_state = udc->resume_state;
2109         udc->resume_state = 0;
2110
2111         /* report resume to the driver , serial.c not support this*/
2112         if (udc->driver->resume)
2113                 udc->driver->resume(&udc->gadget);
2114 }
2115
2116 static void idle_irq(struct qe_udc *udc)
2117 {
2118         u8 usbs;
2119
2120         usbs = in_8(&udc->usb_regs->usb_usbs);
2121         if (usbs & USB_IDLE_STATUS_MASK) {
2122                 if ((udc->usb_state) != USB_STATE_SUSPENDED)
2123                         suspend_irq(udc);
2124         } else {
2125                 if (udc->usb_state == USB_STATE_SUSPENDED)
2126                         resume_irq(udc);
2127         }
2128 }
2129
2130 static int reset_irq(struct qe_udc *udc)
2131 {
2132         unsigned char i;
2133
2134         if (udc->usb_state == USB_STATE_DEFAULT)
2135                 return 0;
2136
2137         qe_usb_disable(udc);
2138         out_8(&udc->usb_regs->usb_usadr, 0);
2139
2140         for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2141                 if (udc->eps[i].init)
2142                         qe_ep_reset(udc, i);
2143         }
2144
2145         reset_queues(udc);
2146         udc->usb_state = USB_STATE_DEFAULT;
2147         udc->ep0_state = WAIT_FOR_SETUP;
2148         udc->ep0_dir = USB_DIR_OUT;
2149         qe_usb_enable(udc);
2150         return 0;
2151 }
2152
2153 static int bsy_irq(struct qe_udc *udc)
2154 {
2155         return 0;
2156 }
2157
2158 static int txe_irq(struct qe_udc *udc)
2159 {
2160         return 0;
2161 }
2162
2163 /* ep0 tx interrupt also in here */
2164 static int tx_irq(struct qe_udc *udc)
2165 {
2166         struct qe_ep *ep;
2167         struct qe_bd __iomem *bd;
2168         int i, res = 0;
2169
2170         if ((udc->usb_state == USB_STATE_ADDRESS)
2171                 && (in_8(&udc->usb_regs->usb_usadr) == 0))
2172                 out_8(&udc->usb_regs->usb_usadr, udc->device_address);
2173
2174         for (i = (USB_MAX_ENDPOINTS-1); ((i >= 0) && (res == 0)); i--) {
2175                 ep = &udc->eps[i];
2176                 if (ep && ep->init && (ep->dir != USB_DIR_OUT)) {
2177                         bd = ep->c_txbd;
2178                         if (!(in_be32((u32 __iomem *)bd) & T_R)
2179                                                 && (in_be32(&bd->buf))) {
2180                                 /* confirm the transmitted bd */
2181                                 if (ep->epnum == 0)
2182                                         res = qe_ep0_txconf(ep);
2183                                 else
2184                                         res = qe_ep_txconf(ep);
2185                         }
2186                 }
2187         }
2188         return res;
2189 }
2190
2191
2192 /* setup packect's rx is handle in the function too */
2193 static void rx_irq(struct qe_udc *udc)
2194 {
2195         struct qe_ep *ep;
2196         struct qe_bd __iomem *bd;
2197         int i;
2198
2199         for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2200                 ep = &udc->eps[i];
2201                 if (ep && ep->init && (ep->dir != USB_DIR_IN)) {
2202                         bd = ep->n_rxbd;
2203                         if (!(in_be32((u32 __iomem *)bd) & R_E)
2204                                                 && (in_be32(&bd->buf))) {
2205                                 if (ep->epnum == 0) {
2206                                         qe_ep0_rx(udc);
2207                                 } else {
2208                                         /*non-setup package receive*/
2209                                         qe_ep_rx(ep);
2210                                 }
2211                         }
2212                 }
2213         }
2214 }
2215
2216 static irqreturn_t qe_udc_irq(int irq, void *_udc)
2217 {
2218         struct qe_udc *udc = (struct qe_udc *)_udc;
2219         u16 irq_src;
2220         irqreturn_t status = IRQ_NONE;
2221         unsigned long flags;
2222
2223         spin_lock_irqsave(&udc->lock, flags);
2224
2225         irq_src = in_be16(&udc->usb_regs->usb_usber) &
2226                 in_be16(&udc->usb_regs->usb_usbmr);
2227         /* Clear notification bits */
2228         out_be16(&udc->usb_regs->usb_usber, irq_src);
2229         /* USB Interrupt */
2230         if (irq_src & USB_E_IDLE_MASK) {
2231                 idle_irq(udc);
2232                 irq_src &= ~USB_E_IDLE_MASK;
2233                 status = IRQ_HANDLED;
2234         }
2235
2236         if (irq_src & USB_E_TXB_MASK) {
2237                 tx_irq(udc);
2238                 irq_src &= ~USB_E_TXB_MASK;
2239                 status = IRQ_HANDLED;
2240         }
2241
2242         if (irq_src & USB_E_RXB_MASK) {
2243                 rx_irq(udc);
2244                 irq_src &= ~USB_E_RXB_MASK;
2245                 status = IRQ_HANDLED;
2246         }
2247
2248         if (irq_src & USB_E_RESET_MASK) {
2249                 reset_irq(udc);
2250                 irq_src &= ~USB_E_RESET_MASK;
2251                 status = IRQ_HANDLED;
2252         }
2253
2254         if (irq_src & USB_E_BSY_MASK) {
2255                 bsy_irq(udc);
2256                 irq_src &= ~USB_E_BSY_MASK;
2257                 status = IRQ_HANDLED;
2258         }
2259
2260         if (irq_src & USB_E_TXE_MASK) {
2261                 txe_irq(udc);
2262                 irq_src &= ~USB_E_TXE_MASK;
2263                 status = IRQ_HANDLED;
2264         }
2265
2266         spin_unlock_irqrestore(&udc->lock, flags);
2267
2268         return status;
2269 }
2270
2271 /*-------------------------------------------------------------------------
2272         Gadget driver probe and unregister.
2273  --------------------------------------------------------------------------*/
2274 static int fsl_qe_start(struct usb_gadget *gadget,
2275                 struct usb_gadget_driver *driver)
2276 {
2277         struct qe_udc *udc;
2278         unsigned long flags;
2279
2280         udc = container_of(gadget, struct qe_udc, gadget);
2281         /* lock is needed but whether should use this lock or another */
2282         spin_lock_irqsave(&udc->lock, flags);
2283
2284         driver->driver.bus = NULL;
2285         /* hook up the driver */
2286         udc->driver = driver;
2287         udc->gadget.speed = driver->max_speed;
2288
2289         /* Enable IRQ reg and Set usbcmd reg EN bit */
2290         qe_usb_enable(udc);
2291
2292         out_be16(&udc->usb_regs->usb_usber, 0xffff);
2293         out_be16(&udc->usb_regs->usb_usbmr, USB_E_DEFAULT_DEVICE);
2294         udc->usb_state = USB_STATE_ATTACHED;
2295         udc->ep0_state = WAIT_FOR_SETUP;
2296         udc->ep0_dir = USB_DIR_OUT;
2297         spin_unlock_irqrestore(&udc->lock, flags);
2298
2299         return 0;
2300 }
2301
2302 static int fsl_qe_stop(struct usb_gadget *gadget)
2303 {
2304         struct qe_udc *udc;
2305         struct qe_ep *loop_ep;
2306         unsigned long flags;
2307
2308         udc = container_of(gadget, struct qe_udc, gadget);
2309         /* stop usb controller, disable intr */
2310         qe_usb_disable(udc);
2311
2312         /* in fact, no needed */
2313         udc->usb_state = USB_STATE_ATTACHED;
2314         udc->ep0_state = WAIT_FOR_SETUP;
2315         udc->ep0_dir = 0;
2316
2317         /* stand operation */
2318         spin_lock_irqsave(&udc->lock, flags);
2319         udc->gadget.speed = USB_SPEED_UNKNOWN;
2320         nuke(&udc->eps[0], -ESHUTDOWN);
2321         list_for_each_entry(loop_ep, &udc->gadget.ep_list, ep.ep_list)
2322                 nuke(loop_ep, -ESHUTDOWN);
2323         spin_unlock_irqrestore(&udc->lock, flags);
2324
2325         udc->driver = NULL;
2326
2327         return 0;
2328 }
2329
2330 /* udc structure's alloc and setup, include ep-param alloc */
2331 static struct qe_udc *qe_udc_config(struct platform_device *ofdev)
2332 {
2333         struct qe_udc *udc;
2334         struct device_node *np = ofdev->dev.of_node;
2335         unsigned long tmp_addr = 0;
2336         struct usb_device_para __iomem *usbpram;
2337         unsigned int i;
2338         u64 size;
2339         u32 offset;
2340
2341         udc = kzalloc(sizeof(*udc), GFP_KERNEL);
2342         if (!udc)
2343                 goto cleanup;
2344
2345         udc->dev = &ofdev->dev;
2346
2347         /* get default address of usb parameter in MURAM from device tree */
2348         offset = *of_get_address(np, 1, &size, NULL);
2349         udc->usb_param = cpm_muram_addr(offset);
2350         memset_io(udc->usb_param, 0, size);
2351
2352         usbpram = udc->usb_param;
2353         out_be16(&usbpram->frame_n, 0);
2354         out_be32(&usbpram->rstate, 0);
2355
2356         tmp_addr = cpm_muram_alloc((USB_MAX_ENDPOINTS *
2357                                         sizeof(struct usb_ep_para)),
2358                                            USB_EP_PARA_ALIGNMENT);
2359         if (IS_ERR_VALUE(tmp_addr))
2360                 goto cleanup;
2361
2362         for (i = 0; i < USB_MAX_ENDPOINTS; i++) {
2363                 out_be16(&usbpram->epptr[i], (u16)tmp_addr);
2364                 udc->ep_param[i] = cpm_muram_addr(tmp_addr);
2365                 tmp_addr += 32;
2366         }
2367
2368         memset_io(udc->ep_param[0], 0,
2369                         USB_MAX_ENDPOINTS * sizeof(struct usb_ep_para));
2370
2371         udc->resume_state = USB_STATE_NOTATTACHED;
2372         udc->usb_state = USB_STATE_POWERED;
2373         udc->ep0_dir = 0;
2374
2375         spin_lock_init(&udc->lock);
2376         return udc;
2377
2378 cleanup:
2379         kfree(udc);
2380         return NULL;
2381 }
2382
2383 /* USB Controller register init */
2384 static int qe_udc_reg_init(struct qe_udc *udc)
2385 {
2386         struct usb_ctlr __iomem *qe_usbregs;
2387         qe_usbregs = udc->usb_regs;
2388
2389         /* Spec says that we must enable the USB controller to change mode. */
2390         out_8(&qe_usbregs->usb_usmod, 0x01);
2391         /* Mode changed, now disable it, since muram isn't initialized yet. */
2392         out_8(&qe_usbregs->usb_usmod, 0x00);
2393
2394         /* Initialize the rest. */
2395         out_be16(&qe_usbregs->usb_usbmr, 0);
2396         out_8(&qe_usbregs->usb_uscom, 0);
2397         out_be16(&qe_usbregs->usb_usber, USBER_ALL_CLEAR);
2398
2399         return 0;
2400 }
2401
2402 static int qe_ep_config(struct qe_udc *udc, unsigned char pipe_num)
2403 {
2404         struct qe_ep *ep = &udc->eps[pipe_num];
2405
2406         ep->udc = udc;
2407         strcpy(ep->name, ep_name[pipe_num]);
2408         ep->ep.name = ep_name[pipe_num];
2409
2410         if (pipe_num == 0) {
2411                 ep->ep.caps.type_control = true;
2412         } else {
2413                 ep->ep.caps.type_iso = true;
2414                 ep->ep.caps.type_bulk = true;
2415                 ep->ep.caps.type_int = true;
2416         }
2417
2418         ep->ep.caps.dir_in = true;
2419         ep->ep.caps.dir_out = true;
2420
2421         ep->ep.ops = &qe_ep_ops;
2422         ep->stopped = 1;
2423         usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
2424         ep->ep.desc = NULL;
2425         ep->dir = 0xff;
2426         ep->epnum = (u8)pipe_num;
2427         ep->sent = 0;
2428         ep->last = 0;
2429         ep->init = 0;
2430         ep->rxframe = NULL;
2431         ep->txframe = NULL;
2432         ep->tx_req = NULL;
2433         ep->state = EP_STATE_IDLE;
2434         ep->has_data = 0;
2435
2436         /* the queue lists any req for this ep */
2437         INIT_LIST_HEAD(&ep->queue);
2438
2439         /* gagdet.ep_list used for ep_autoconfig so no ep0*/
2440         if (pipe_num != 0)
2441                 list_add_tail(&ep->ep.ep_list, &udc->gadget.ep_list);
2442
2443         ep->gadget = &udc->gadget;
2444
2445         return 0;
2446 }
2447
2448 /*-----------------------------------------------------------------------
2449  *      UDC device Driver operation functions                           *
2450  *----------------------------------------------------------------------*/
2451 static void qe_udc_release(struct device *dev)
2452 {
2453         struct qe_udc *udc = container_of(dev, struct qe_udc, gadget.dev);
2454         int i;
2455
2456         complete(udc->done);
2457         cpm_muram_free(cpm_muram_offset(udc->ep_param[0]));
2458         for (i = 0; i < USB_MAX_ENDPOINTS; i++)
2459                 udc->ep_param[i] = NULL;
2460
2461         kfree(udc);
2462 }
2463
2464 /* Driver probe functions */
2465 static const struct of_device_id qe_udc_match[];
2466 static int qe_udc_probe(struct platform_device *ofdev)
2467 {
2468         struct qe_udc *udc;
2469         const struct of_device_id *match;
2470         struct device_node *np = ofdev->dev.of_node;
2471         struct qe_ep *ep;
2472         unsigned int ret = 0;
2473         unsigned int i;
2474         const void *prop;
2475
2476         match = of_match_device(qe_udc_match, &ofdev->dev);
2477         if (!match)
2478                 return -EINVAL;
2479
2480         prop = of_get_property(np, "mode", NULL);
2481         if (!prop || strcmp(prop, "peripheral"))
2482                 return -ENODEV;
2483
2484         /* Initialize the udc structure including QH member and other member */
2485         udc = qe_udc_config(ofdev);
2486         if (!udc) {
2487                 dev_err(&ofdev->dev, "failed to initialize\n");
2488                 return -ENOMEM;
2489         }
2490
2491         udc->soc_type = (unsigned long)match->data;
2492         udc->usb_regs = of_iomap(np, 0);
2493         if (!udc->usb_regs) {
2494                 ret = -ENOMEM;
2495                 goto err1;
2496         }
2497
2498         /* initialize usb hw reg except for regs for EP,
2499          * leave usbintr reg untouched*/
2500         qe_udc_reg_init(udc);
2501
2502         /* here comes the stand operations for probe
2503          * set the qe_udc->gadget.xxx */
2504         udc->gadget.ops = &qe_gadget_ops;
2505
2506         /* gadget.ep0 is a pointer */
2507         udc->gadget.ep0 = &udc->eps[0].ep;
2508
2509         INIT_LIST_HEAD(&udc->gadget.ep_list);
2510
2511         /* modify in register gadget process */
2512         udc->gadget.speed = USB_SPEED_UNKNOWN;
2513
2514         /* name: Identifies the controller hardware type. */
2515         udc->gadget.name = driver_name;
2516         udc->gadget.dev.parent = &ofdev->dev;
2517
2518         /* initialize qe_ep struct */
2519         for (i = 0; i < USB_MAX_ENDPOINTS ; i++) {
2520                 /* because the ep type isn't decide here so
2521                  * qe_ep_init() should be called in ep_enable() */
2522
2523                 /* setup the qe_ep struct and link ep.ep.list
2524                  * into gadget.ep_list */
2525                 qe_ep_config(udc, (unsigned char)i);
2526         }
2527
2528         /* ep0 initialization in here */
2529         ret = qe_ep_init(udc, 0, &qe_ep0_desc);
2530         if (ret)
2531                 goto err2;
2532
2533         /* create a buf for ZLP send, need to remain zeroed */
2534         udc->nullbuf = devm_kzalloc(&ofdev->dev, 256, GFP_KERNEL);
2535         if (udc->nullbuf == NULL) {
2536                 ret = -ENOMEM;
2537                 goto err3;
2538         }
2539
2540         /* buffer for data of get_status request */
2541         udc->statusbuf = devm_kzalloc(&ofdev->dev, 2, GFP_KERNEL);
2542         if (udc->statusbuf == NULL) {
2543                 ret = -ENOMEM;
2544                 goto err3;
2545         }
2546
2547         udc->nullp = virt_to_phys((void *)udc->nullbuf);
2548         if (udc->nullp == DMA_ADDR_INVALID) {
2549                 udc->nullp = dma_map_single(
2550                                         udc->gadget.dev.parent,
2551                                         udc->nullbuf,
2552                                         256,
2553                                         DMA_TO_DEVICE);
2554                 udc->nullmap = 1;
2555         } else {
2556                 dma_sync_single_for_device(udc->gadget.dev.parent,
2557                                         udc->nullp, 256,
2558                                         DMA_TO_DEVICE);
2559         }
2560
2561         tasklet_setup(&udc->rx_tasklet, ep_rx_tasklet);
2562         /* request irq and disable DR  */
2563         udc->usb_irq = irq_of_parse_and_map(np, 0);
2564         if (!udc->usb_irq) {
2565                 ret = -EINVAL;
2566                 goto err_noirq;
2567         }
2568
2569         ret = request_irq(udc->usb_irq, qe_udc_irq, 0,
2570                                 driver_name, udc);
2571         if (ret) {
2572                 dev_err(udc->dev, "cannot request irq %d err %d\n",
2573                                 udc->usb_irq, ret);
2574                 goto err4;
2575         }
2576
2577         ret = usb_add_gadget_udc_release(&ofdev->dev, &udc->gadget,
2578                         qe_udc_release);
2579         if (ret)
2580                 goto err5;
2581
2582         platform_set_drvdata(ofdev, udc);
2583         dev_info(udc->dev,
2584                         "%s USB controller initialized as device\n",
2585                         (udc->soc_type == PORT_QE) ? "QE" : "CPM");
2586         return 0;
2587
2588 err5:
2589         free_irq(udc->usb_irq, udc);
2590 err4:
2591         irq_dispose_mapping(udc->usb_irq);
2592 err_noirq:
2593         if (udc->nullmap) {
2594                 dma_unmap_single(udc->gadget.dev.parent,
2595                         udc->nullp, 256,
2596                                 DMA_TO_DEVICE);
2597                         udc->nullp = DMA_ADDR_INVALID;
2598         } else {
2599                 dma_sync_single_for_cpu(udc->gadget.dev.parent,
2600                         udc->nullp, 256,
2601                                 DMA_TO_DEVICE);
2602         }
2603 err3:
2604         ep = &udc->eps[0];
2605         cpm_muram_free(cpm_muram_offset(ep->rxbase));
2606         kfree(ep->rxframe);
2607         kfree(ep->rxbuffer);
2608         kfree(ep->txframe);
2609 err2:
2610         iounmap(udc->usb_regs);
2611 err1:
2612         kfree(udc);
2613         return ret;
2614 }
2615
2616 #ifdef CONFIG_PM
2617 static int qe_udc_suspend(struct platform_device *dev, pm_message_t state)
2618 {
2619         return -ENOTSUPP;
2620 }
2621
2622 static int qe_udc_resume(struct platform_device *dev)
2623 {
2624         return -ENOTSUPP;
2625 }
2626 #endif
2627
2628 static int qe_udc_remove(struct platform_device *ofdev)
2629 {
2630         struct qe_udc *udc = platform_get_drvdata(ofdev);
2631         struct qe_ep *ep;
2632         unsigned int size;
2633         DECLARE_COMPLETION_ONSTACK(done);
2634
2635         usb_del_gadget_udc(&udc->gadget);
2636
2637         udc->done = &done;
2638         tasklet_disable(&udc->rx_tasklet);
2639
2640         if (udc->nullmap) {
2641                 dma_unmap_single(udc->gadget.dev.parent,
2642                         udc->nullp, 256,
2643                                 DMA_TO_DEVICE);
2644                         udc->nullp = DMA_ADDR_INVALID;
2645         } else {
2646                 dma_sync_single_for_cpu(udc->gadget.dev.parent,
2647                         udc->nullp, 256,
2648                                 DMA_TO_DEVICE);
2649         }
2650
2651         ep = &udc->eps[0];
2652         cpm_muram_free(cpm_muram_offset(ep->rxbase));
2653         size = (ep->ep.maxpacket + USB_CRC_SIZE + 2) * (USB_BDRING_LEN + 1);
2654
2655         kfree(ep->rxframe);
2656         if (ep->rxbufmap) {
2657                 dma_unmap_single(udc->gadget.dev.parent,
2658                                 ep->rxbuf_d, size,
2659                                 DMA_FROM_DEVICE);
2660                 ep->rxbuf_d = DMA_ADDR_INVALID;
2661         } else {
2662                 dma_sync_single_for_cpu(udc->gadget.dev.parent,
2663                                 ep->rxbuf_d, size,
2664                                 DMA_FROM_DEVICE);
2665         }
2666
2667         kfree(ep->rxbuffer);
2668         kfree(ep->txframe);
2669
2670         free_irq(udc->usb_irq, udc);
2671         irq_dispose_mapping(udc->usb_irq);
2672
2673         tasklet_kill(&udc->rx_tasklet);
2674
2675         iounmap(udc->usb_regs);
2676
2677         /* wait for release() of gadget.dev to free udc */
2678         wait_for_completion(&done);
2679
2680         return 0;
2681 }
2682
2683 /*-------------------------------------------------------------------------*/
2684 static const struct of_device_id qe_udc_match[] = {
2685         {
2686                 .compatible = "fsl,mpc8323-qe-usb",
2687                 .data = (void *)PORT_QE,
2688         },
2689         {
2690                 .compatible = "fsl,mpc8360-qe-usb",
2691                 .data = (void *)PORT_QE,
2692         },
2693         {
2694                 .compatible = "fsl,mpc8272-cpm-usb",
2695                 .data = (void *)PORT_CPM,
2696         },
2697         {},
2698 };
2699
2700 MODULE_DEVICE_TABLE(of, qe_udc_match);
2701
2702 static struct platform_driver udc_driver = {
2703         .driver = {
2704                 .name = driver_name,
2705                 .of_match_table = qe_udc_match,
2706         },
2707         .probe          = qe_udc_probe,
2708         .remove         = qe_udc_remove,
2709 #ifdef CONFIG_PM
2710         .suspend        = qe_udc_suspend,
2711         .resume         = qe_udc_resume,
2712 #endif
2713 };
2714
2715 module_platform_driver(udc_driver);
2716
2717 MODULE_DESCRIPTION(DRIVER_DESC);
2718 MODULE_AUTHOR(DRIVER_AUTHOR);
2719 MODULE_LICENSE("GPL");