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