1 // SPDX-License-Identifier: GPL-2.0
3 * FOTG210 UDC Driver supports Bulk transfer so far
5 * Copyright (C) 2013 Faraday Technology Corporation
7 * Author : Yuan-Hsin Chen <yhchen@faraday-tech.com>
10 #include <linux/dma-mapping.h>
11 #include <linux/err.h>
12 #include <linux/interrupt.h>
14 #include <linux/module.h>
15 #include <linux/platform_device.h>
16 #include <linux/usb/ch9.h>
17 #include <linux/usb/gadget.h>
18 #include <linux/clk.h>
19 #include <linux/usb/otg.h>
20 #include <linux/usb/phy.h>
23 #include "fotg210-udc.h"
25 #define DRIVER_DESC "FOTG210 USB Device Controller Driver"
26 #define DRIVER_VERSION "30-April-2013"
28 static const char udc_name[] = "fotg210_udc";
29 static const char * const fotg210_ep_name[] = {
30 "ep0", "ep1", "ep2", "ep3", "ep4"};
32 static void fotg210_disable_fifo_int(struct fotg210_ep *ep)
34 u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
37 value |= DMISGR1_MF_IN_INT(ep->epnum - 1);
39 value |= DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
40 iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
43 static void fotg210_enable_fifo_int(struct fotg210_ep *ep)
45 u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR1);
48 value &= ~DMISGR1_MF_IN_INT(ep->epnum - 1);
50 value &= ~DMISGR1_MF_OUTSPK_INT(ep->epnum - 1);
51 iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR1);
54 static void fotg210_set_cxdone(struct fotg210_udc *fotg210)
56 u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
58 value |= DCFESR_CX_DONE;
59 iowrite32(value, fotg210->reg + FOTG210_DCFESR);
62 static void fotg210_done(struct fotg210_ep *ep, struct fotg210_request *req,
65 list_del_init(&req->queue);
67 /* don't modify queue heads during completion callback */
68 if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
69 req->req.status = -ESHUTDOWN;
71 req->req.status = status;
73 spin_unlock(&ep->fotg210->lock);
74 usb_gadget_giveback_request(&ep->ep, &req->req);
75 spin_lock(&ep->fotg210->lock);
78 if (list_empty(&ep->queue))
79 fotg210_disable_fifo_int(ep);
81 fotg210_set_cxdone(ep->fotg210);
85 static void fotg210_fifo_ep_mapping(struct fotg210_ep *ep, u32 epnum,
88 struct fotg210_udc *fotg210 = ep->fotg210;
91 /* Driver should map an ep to a fifo and then map the fifo
92 * to the ep. What a brain-damaged design!
95 /* map a fifo to an ep */
96 val = ioread32(fotg210->reg + FOTG210_EPMAP);
97 val &= ~EPMAP_FIFONOMSK(epnum, dir_in);
98 val |= EPMAP_FIFONO(epnum, dir_in);
99 iowrite32(val, fotg210->reg + FOTG210_EPMAP);
101 /* map the ep to the fifo */
102 val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
103 val &= ~FIFOMAP_EPNOMSK(epnum);
104 val |= FIFOMAP_EPNO(epnum);
105 iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
108 val = ioread32(fotg210->reg + FOTG210_FIFOCF);
109 val |= FIFOCF_FIFO_EN(epnum - 1);
110 iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
113 static void fotg210_set_fifo_dir(struct fotg210_ep *ep, u32 epnum, u32 dir_in)
115 struct fotg210_udc *fotg210 = ep->fotg210;
118 val = ioread32(fotg210->reg + FOTG210_FIFOMAP);
119 val |= (dir_in ? FIFOMAP_DIRIN(epnum - 1) : FIFOMAP_DIROUT(epnum - 1));
120 iowrite32(val, fotg210->reg + FOTG210_FIFOMAP);
123 static void fotg210_set_tfrtype(struct fotg210_ep *ep, u32 epnum, u32 type)
125 struct fotg210_udc *fotg210 = ep->fotg210;
128 val = ioread32(fotg210->reg + FOTG210_FIFOCF);
129 val |= FIFOCF_TYPE(type, epnum - 1);
130 iowrite32(val, fotg210->reg + FOTG210_FIFOCF);
133 static void fotg210_set_mps(struct fotg210_ep *ep, u32 epnum, u32 mps,
136 struct fotg210_udc *fotg210 = ep->fotg210;
138 u32 offset = dir_in ? FOTG210_INEPMPSR(epnum) :
139 FOTG210_OUTEPMPSR(epnum);
141 val = ioread32(fotg210->reg + offset);
142 val |= INOUTEPMPSR_MPS(mps);
143 iowrite32(val, fotg210->reg + offset);
146 static int fotg210_config_ep(struct fotg210_ep *ep,
147 const struct usb_endpoint_descriptor *desc)
149 struct fotg210_udc *fotg210 = ep->fotg210;
151 fotg210_set_fifo_dir(ep, ep->epnum, ep->dir_in);
152 fotg210_set_tfrtype(ep, ep->epnum, ep->type);
153 fotg210_set_mps(ep, ep->epnum, ep->ep.maxpacket, ep->dir_in);
154 fotg210_fifo_ep_mapping(ep, ep->epnum, ep->dir_in);
156 fotg210->ep[ep->epnum] = ep;
161 static int fotg210_ep_enable(struct usb_ep *_ep,
162 const struct usb_endpoint_descriptor *desc)
164 struct fotg210_ep *ep;
166 ep = container_of(_ep, struct fotg210_ep, ep);
169 ep->epnum = usb_endpoint_num(desc);
170 ep->type = usb_endpoint_type(desc);
171 ep->dir_in = usb_endpoint_dir_in(desc);
172 ep->ep.maxpacket = usb_endpoint_maxp(desc);
174 return fotg210_config_ep(ep, desc);
177 static void fotg210_reset_tseq(struct fotg210_udc *fotg210, u8 epnum)
179 struct fotg210_ep *ep = fotg210->ep[epnum];
184 fotg210->reg + FOTG210_INEPMPSR(epnum) :
185 fotg210->reg + FOTG210_OUTEPMPSR(epnum);
187 /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
188 * bit. Controller wouldn't clear this bit. WTF!!!
191 value = ioread32(reg);
192 value |= INOUTEPMPSR_RESET_TSEQ;
193 iowrite32(value, reg);
195 value = ioread32(reg);
196 value &= ~INOUTEPMPSR_RESET_TSEQ;
197 iowrite32(value, reg);
200 static int fotg210_ep_release(struct fotg210_ep *ep)
208 fotg210_reset_tseq(ep->fotg210, ep->epnum);
213 static int fotg210_ep_disable(struct usb_ep *_ep)
215 struct fotg210_ep *ep;
216 struct fotg210_request *req;
221 ep = container_of(_ep, struct fotg210_ep, ep);
223 while (!list_empty(&ep->queue)) {
224 req = list_entry(ep->queue.next,
225 struct fotg210_request, queue);
226 spin_lock_irqsave(&ep->fotg210->lock, flags);
227 fotg210_done(ep, req, -ECONNRESET);
228 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
231 return fotg210_ep_release(ep);
234 static struct usb_request *fotg210_ep_alloc_request(struct usb_ep *_ep,
237 struct fotg210_request *req;
239 req = kzalloc(sizeof(struct fotg210_request), gfp_flags);
243 INIT_LIST_HEAD(&req->queue);
248 static void fotg210_ep_free_request(struct usb_ep *_ep,
249 struct usb_request *_req)
251 struct fotg210_request *req;
253 req = container_of(_req, struct fotg210_request, req);
257 static void fotg210_enable_dma(struct fotg210_ep *ep,
258 dma_addr_t d, u32 len)
261 struct fotg210_udc *fotg210 = ep->fotg210;
263 /* set transfer length and direction */
264 value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
265 value &= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
266 value |= DMACPSR1_DMA_LEN(len) | DMACPSR1_DMA_TYPE(ep->dir_in);
267 iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
269 /* set device DMA target FIFO number */
270 value = ioread32(fotg210->reg + FOTG210_DMATFNR);
272 value |= DMATFNR_ACC_FN(ep->epnum - 1);
274 value |= DMATFNR_ACC_CXF;
275 iowrite32(value, fotg210->reg + FOTG210_DMATFNR);
277 /* set DMA memory address */
278 iowrite32(d, fotg210->reg + FOTG210_DMACPSR2);
280 /* enable MDMA_EROR and MDMA_CMPLT interrupt */
281 value = ioread32(fotg210->reg + FOTG210_DMISGR2);
282 value &= ~(DMISGR2_MDMA_CMPLT | DMISGR2_MDMA_ERROR);
283 iowrite32(value, fotg210->reg + FOTG210_DMISGR2);
286 value = ioread32(fotg210->reg + FOTG210_DMACPSR1);
287 value |= DMACPSR1_DMA_START;
288 iowrite32(value, fotg210->reg + FOTG210_DMACPSR1);
291 static void fotg210_disable_dma(struct fotg210_ep *ep)
293 iowrite32(DMATFNR_DISDMA, ep->fotg210->reg + FOTG210_DMATFNR);
296 static void fotg210_wait_dma_done(struct fotg210_ep *ep)
301 value = ioread32(ep->fotg210->reg + FOTG210_DISGR2);
302 if ((value & DISGR2_USBRST_INT) ||
303 (value & DISGR2_DMA_ERROR))
305 } while (!(value & DISGR2_DMA_CMPLT));
307 value &= ~DISGR2_DMA_CMPLT;
308 iowrite32(value, ep->fotg210->reg + FOTG210_DISGR2);
312 value = ioread32(ep->fotg210->reg + FOTG210_DMACPSR1);
313 value |= DMACPSR1_DMA_ABORT;
314 iowrite32(value, ep->fotg210->reg + FOTG210_DMACPSR1);
318 value = ioread32(ep->fotg210->reg +
319 FOTG210_FIBCR(ep->epnum - 1));
320 value |= FIBCR_FFRST;
321 iowrite32(value, ep->fotg210->reg +
322 FOTG210_FIBCR(ep->epnum - 1));
324 value = ioread32(ep->fotg210->reg + FOTG210_DCFESR);
325 value |= DCFESR_CX_CLR;
326 iowrite32(value, ep->fotg210->reg + FOTG210_DCFESR);
330 static void fotg210_start_dma(struct fotg210_ep *ep,
331 struct fotg210_request *req)
333 struct device *dev = &ep->fotg210->gadget.dev;
340 buffer = req->req.buf;
341 length = req->req.length;
343 buffer = req->req.buf + req->req.actual;
344 length = ioread32(ep->fotg210->reg +
345 FOTG210_FIBCR(ep->epnum - 1)) & FIBCR_BCFX;
346 if (length > req->req.length - req->req.actual)
347 length = req->req.length - req->req.actual;
350 buffer = req->req.buf + req->req.actual;
351 if (req->req.length - req->req.actual > ep->ep.maxpacket)
352 length = ep->ep.maxpacket;
354 length = req->req.length - req->req.actual;
357 d = dma_map_single(dev, buffer, length,
358 ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
360 if (dma_mapping_error(dev, d)) {
361 pr_err("dma_mapping_error\n");
365 fotg210_enable_dma(ep, d, length);
367 /* check if dma is done */
368 fotg210_wait_dma_done(ep);
370 fotg210_disable_dma(ep);
372 /* update actual transfer length */
373 req->req.actual += length;
375 dma_unmap_single(dev, d, length, DMA_TO_DEVICE);
378 static void fotg210_ep0_queue(struct fotg210_ep *ep,
379 struct fotg210_request *req)
381 if (!req->req.length) {
382 fotg210_done(ep, req, 0);
385 if (ep->dir_in) { /* if IN */
386 fotg210_start_dma(ep, req);
387 if (req->req.length == req->req.actual)
388 fotg210_done(ep, req, 0);
390 u32 value = ioread32(ep->fotg210->reg + FOTG210_DMISGR0);
392 value &= ~DMISGR0_MCX_OUT_INT;
393 iowrite32(value, ep->fotg210->reg + FOTG210_DMISGR0);
397 static int fotg210_ep_queue(struct usb_ep *_ep, struct usb_request *_req,
400 struct fotg210_ep *ep;
401 struct fotg210_request *req;
405 ep = container_of(_ep, struct fotg210_ep, ep);
406 req = container_of(_req, struct fotg210_request, req);
408 if (ep->fotg210->gadget.speed == USB_SPEED_UNKNOWN)
411 spin_lock_irqsave(&ep->fotg210->lock, flags);
413 if (list_empty(&ep->queue))
416 list_add_tail(&req->queue, &ep->queue);
419 req->req.status = -EINPROGRESS;
421 if (!ep->epnum) /* ep0 */
422 fotg210_ep0_queue(ep, req);
423 else if (request && !ep->stall)
424 fotg210_enable_fifo_int(ep);
426 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
431 static int fotg210_ep_dequeue(struct usb_ep *_ep, struct usb_request *_req)
433 struct fotg210_ep *ep;
434 struct fotg210_request *req;
437 ep = container_of(_ep, struct fotg210_ep, ep);
438 req = container_of(_req, struct fotg210_request, req);
440 spin_lock_irqsave(&ep->fotg210->lock, flags);
441 if (!list_empty(&ep->queue))
442 fotg210_done(ep, req, -ECONNRESET);
443 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
448 static void fotg210_set_epnstall(struct fotg210_ep *ep)
450 struct fotg210_udc *fotg210 = ep->fotg210;
454 /* check if IN FIFO is empty before stall */
457 value = ioread32(fotg210->reg + FOTG210_DCFESR);
458 } while (!(value & DCFESR_FIFO_EMPTY(ep->epnum - 1)));
462 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
463 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
464 value = ioread32(reg);
465 value |= INOUTEPMPSR_STL_EP;
466 iowrite32(value, reg);
469 static void fotg210_clear_epnstall(struct fotg210_ep *ep)
471 struct fotg210_udc *fotg210 = ep->fotg210;
476 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
477 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
478 value = ioread32(reg);
479 value &= ~INOUTEPMPSR_STL_EP;
480 iowrite32(value, reg);
483 static int fotg210_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
485 struct fotg210_ep *ep;
486 struct fotg210_udc *fotg210;
489 ep = container_of(_ep, struct fotg210_ep, ep);
491 fotg210 = ep->fotg210;
493 spin_lock_irqsave(&ep->fotg210->lock, flags);
496 fotg210_set_epnstall(ep);
501 fotg210_reset_tseq(fotg210, ep->epnum);
502 fotg210_clear_epnstall(ep);
505 if (!list_empty(&ep->queue))
506 fotg210_enable_fifo_int(ep);
509 spin_unlock_irqrestore(&ep->fotg210->lock, flags);
513 static int fotg210_ep_set_halt(struct usb_ep *_ep, int value)
515 return fotg210_set_halt_and_wedge(_ep, value, 0);
518 static int fotg210_ep_set_wedge(struct usb_ep *_ep)
520 return fotg210_set_halt_and_wedge(_ep, 1, 1);
523 static void fotg210_ep_fifo_flush(struct usb_ep *_ep)
527 static const struct usb_ep_ops fotg210_ep_ops = {
528 .enable = fotg210_ep_enable,
529 .disable = fotg210_ep_disable,
531 .alloc_request = fotg210_ep_alloc_request,
532 .free_request = fotg210_ep_free_request,
534 .queue = fotg210_ep_queue,
535 .dequeue = fotg210_ep_dequeue,
537 .set_halt = fotg210_ep_set_halt,
538 .fifo_flush = fotg210_ep_fifo_flush,
539 .set_wedge = fotg210_ep_set_wedge,
542 static void fotg210_clear_tx0byte(struct fotg210_udc *fotg210)
544 u32 value = ioread32(fotg210->reg + FOTG210_TX0BYTE);
546 value &= ~(TX0BYTE_EP1 | TX0BYTE_EP2 | TX0BYTE_EP3
548 iowrite32(value, fotg210->reg + FOTG210_TX0BYTE);
551 static void fotg210_clear_rx0byte(struct fotg210_udc *fotg210)
553 u32 value = ioread32(fotg210->reg + FOTG210_RX0BYTE);
555 value &= ~(RX0BYTE_EP1 | RX0BYTE_EP2 | RX0BYTE_EP3
557 iowrite32(value, fotg210->reg + FOTG210_RX0BYTE);
560 /* read 8-byte setup packet only */
561 static void fotg210_rdsetupp(struct fotg210_udc *fotg210,
569 iowrite32(DMATFNR_ACC_CXF, fotg210->reg + FOTG210_DMATFNR);
571 for (i = (length >> 2); i > 0; i--) {
572 data = ioread32(fotg210->reg + FOTG210_CXPORT);
574 *(tmp + 1) = (data >> 8) & 0xFF;
575 *(tmp + 2) = (data >> 16) & 0xFF;
576 *(tmp + 3) = (data >> 24) & 0xFF;
580 switch (length % 4) {
582 data = ioread32(fotg210->reg + FOTG210_CXPORT);
586 data = ioread32(fotg210->reg + FOTG210_CXPORT);
588 *(tmp + 1) = (data >> 8) & 0xFF;
591 data = ioread32(fotg210->reg + FOTG210_CXPORT);
593 *(tmp + 1) = (data >> 8) & 0xFF;
594 *(tmp + 2) = (data >> 16) & 0xFF;
600 iowrite32(DMATFNR_DISDMA, fotg210->reg + FOTG210_DMATFNR);
603 static void fotg210_set_configuration(struct fotg210_udc *fotg210)
605 u32 value = ioread32(fotg210->reg + FOTG210_DAR);
607 value |= DAR_AFT_CONF;
608 iowrite32(value, fotg210->reg + FOTG210_DAR);
611 static void fotg210_set_dev_addr(struct fotg210_udc *fotg210, u32 addr)
613 u32 value = ioread32(fotg210->reg + FOTG210_DAR);
615 value |= (addr & 0x7F);
616 iowrite32(value, fotg210->reg + FOTG210_DAR);
619 static void fotg210_set_cxstall(struct fotg210_udc *fotg210)
621 u32 value = ioread32(fotg210->reg + FOTG210_DCFESR);
623 value |= DCFESR_CX_STL;
624 iowrite32(value, fotg210->reg + FOTG210_DCFESR);
627 static void fotg210_request_error(struct fotg210_udc *fotg210)
629 fotg210_set_cxstall(fotg210);
630 pr_err("request error!!\n");
633 static void fotg210_set_address(struct fotg210_udc *fotg210,
634 struct usb_ctrlrequest *ctrl)
636 if (le16_to_cpu(ctrl->wValue) >= 0x0100) {
637 fotg210_request_error(fotg210);
639 fotg210_set_dev_addr(fotg210, le16_to_cpu(ctrl->wValue));
640 fotg210_set_cxdone(fotg210);
644 static void fotg210_set_feature(struct fotg210_udc *fotg210,
645 struct usb_ctrlrequest *ctrl)
647 switch (ctrl->bRequestType & USB_RECIP_MASK) {
648 case USB_RECIP_DEVICE:
649 fotg210_set_cxdone(fotg210);
651 case USB_RECIP_INTERFACE:
652 fotg210_set_cxdone(fotg210);
654 case USB_RECIP_ENDPOINT: {
656 epnum = le16_to_cpu(ctrl->wIndex) & USB_ENDPOINT_NUMBER_MASK;
658 fotg210_set_epnstall(fotg210->ep[epnum]);
660 fotg210_set_cxstall(fotg210);
661 fotg210_set_cxdone(fotg210);
665 fotg210_request_error(fotg210);
670 static void fotg210_clear_feature(struct fotg210_udc *fotg210,
671 struct usb_ctrlrequest *ctrl)
673 struct fotg210_ep *ep =
674 fotg210->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
676 switch (ctrl->bRequestType & USB_RECIP_MASK) {
677 case USB_RECIP_DEVICE:
678 fotg210_set_cxdone(fotg210);
680 case USB_RECIP_INTERFACE:
681 fotg210_set_cxdone(fotg210);
683 case USB_RECIP_ENDPOINT:
684 if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
686 fotg210_set_cxdone(fotg210);
690 fotg210_set_halt_and_wedge(&ep->ep, 0, 0);
692 fotg210_set_cxdone(fotg210);
695 fotg210_request_error(fotg210);
700 static int fotg210_is_epnstall(struct fotg210_ep *ep)
702 struct fotg210_udc *fotg210 = ep->fotg210;
707 fotg210->reg + FOTG210_INEPMPSR(ep->epnum) :
708 fotg210->reg + FOTG210_OUTEPMPSR(ep->epnum);
709 value = ioread32(reg);
710 return value & INOUTEPMPSR_STL_EP ? 1 : 0;
713 static void fotg210_get_status(struct fotg210_udc *fotg210,
714 struct usb_ctrlrequest *ctrl)
718 switch (ctrl->bRequestType & USB_RECIP_MASK) {
719 case USB_RECIP_DEVICE:
720 fotg210->ep0_data = cpu_to_le16(1 << USB_DEVICE_SELF_POWERED);
722 case USB_RECIP_INTERFACE:
723 fotg210->ep0_data = cpu_to_le16(0);
725 case USB_RECIP_ENDPOINT:
726 epnum = ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK;
729 cpu_to_le16(fotg210_is_epnstall(fotg210->ep[epnum])
730 << USB_ENDPOINT_HALT);
732 fotg210_request_error(fotg210);
736 fotg210_request_error(fotg210);
740 fotg210->ep0_req->buf = &fotg210->ep0_data;
741 fotg210->ep0_req->length = 2;
743 spin_unlock(&fotg210->lock);
744 fotg210_ep_queue(fotg210->gadget.ep0, fotg210->ep0_req, GFP_ATOMIC);
745 spin_lock(&fotg210->lock);
748 static int fotg210_setup_packet(struct fotg210_udc *fotg210,
749 struct usb_ctrlrequest *ctrl)
754 fotg210_rdsetupp(fotg210, p);
756 fotg210->ep[0]->dir_in = ctrl->bRequestType & USB_DIR_IN;
758 if (fotg210->gadget.speed == USB_SPEED_UNKNOWN) {
759 u32 value = ioread32(fotg210->reg + FOTG210_DMCR);
760 fotg210->gadget.speed = value & DMCR_HS_EN ?
761 USB_SPEED_HIGH : USB_SPEED_FULL;
765 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
766 switch (ctrl->bRequest) {
767 case USB_REQ_GET_STATUS:
768 fotg210_get_status(fotg210, ctrl);
770 case USB_REQ_CLEAR_FEATURE:
771 fotg210_clear_feature(fotg210, ctrl);
773 case USB_REQ_SET_FEATURE:
774 fotg210_set_feature(fotg210, ctrl);
776 case USB_REQ_SET_ADDRESS:
777 fotg210_set_address(fotg210, ctrl);
779 case USB_REQ_SET_CONFIGURATION:
780 fotg210_set_configuration(fotg210);
794 static void fotg210_ep0out(struct fotg210_udc *fotg210)
796 struct fotg210_ep *ep = fotg210->ep[0];
798 if (!list_empty(&ep->queue) && !ep->dir_in) {
799 struct fotg210_request *req;
801 req = list_first_entry(&ep->queue,
802 struct fotg210_request, queue);
805 fotg210_start_dma(ep, req);
807 if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
808 fotg210_done(ep, req, 0);
810 pr_err("%s : empty queue\n", __func__);
814 static void fotg210_ep0in(struct fotg210_udc *fotg210)
816 struct fotg210_ep *ep = fotg210->ep[0];
818 if ((!list_empty(&ep->queue)) && (ep->dir_in)) {
819 struct fotg210_request *req;
821 req = list_entry(ep->queue.next,
822 struct fotg210_request, queue);
825 fotg210_start_dma(ep, req);
827 if (req->req.actual == req->req.length)
828 fotg210_done(ep, req, 0);
830 fotg210_set_cxdone(fotg210);
834 static void fotg210_clear_comabt_int(struct fotg210_udc *fotg210)
836 u32 value = ioread32(fotg210->reg + FOTG210_DISGR0);
838 value &= ~DISGR0_CX_COMABT_INT;
839 iowrite32(value, fotg210->reg + FOTG210_DISGR0);
842 static void fotg210_in_fifo_handler(struct fotg210_ep *ep)
844 struct fotg210_request *req = list_entry(ep->queue.next,
845 struct fotg210_request, queue);
848 fotg210_start_dma(ep, req);
849 fotg210_done(ep, req, 0);
852 static void fotg210_out_fifo_handler(struct fotg210_ep *ep)
854 struct fotg210_request *req = list_entry(ep->queue.next,
855 struct fotg210_request, queue);
856 int disgr1 = ioread32(ep->fotg210->reg + FOTG210_DISGR1);
858 fotg210_start_dma(ep, req);
860 /* Complete the request when it's full or a short packet arrived.
861 * Like other drivers, short_not_ok isn't handled.
864 if (req->req.length == req->req.actual ||
865 (disgr1 & DISGR1_SPK_INT(ep->epnum - 1)))
866 fotg210_done(ep, req, 0);
869 static irqreturn_t fotg210_irq(int irq, void *_fotg210)
871 struct fotg210_udc *fotg210 = _fotg210;
872 u32 int_grp = ioread32(fotg210->reg + FOTG210_DIGR);
873 u32 int_msk = ioread32(fotg210->reg + FOTG210_DMIGR);
877 spin_lock(&fotg210->lock);
879 if (int_grp & DIGR_INT_G2) {
880 void __iomem *reg = fotg210->reg + FOTG210_DISGR2;
881 u32 int_grp2 = ioread32(reg);
882 u32 int_msk2 = ioread32(fotg210->reg + FOTG210_DMISGR2);
885 int_grp2 &= ~int_msk2;
887 if (int_grp2 & DISGR2_USBRST_INT) {
888 usb_gadget_udc_reset(&fotg210->gadget,
890 value = ioread32(reg);
891 value &= ~DISGR2_USBRST_INT;
892 iowrite32(value, reg);
893 pr_info("fotg210 udc reset\n");
895 if (int_grp2 & DISGR2_SUSP_INT) {
896 value = ioread32(reg);
897 value &= ~DISGR2_SUSP_INT;
898 iowrite32(value, reg);
899 pr_info("fotg210 udc suspend\n");
901 if (int_grp2 & DISGR2_RESM_INT) {
902 value = ioread32(reg);
903 value &= ~DISGR2_RESM_INT;
904 iowrite32(value, reg);
905 pr_info("fotg210 udc resume\n");
907 if (int_grp2 & DISGR2_ISO_SEQ_ERR_INT) {
908 value = ioread32(reg);
909 value &= ~DISGR2_ISO_SEQ_ERR_INT;
910 iowrite32(value, reg);
911 pr_info("fotg210 iso sequence error\n");
913 if (int_grp2 & DISGR2_ISO_SEQ_ABORT_INT) {
914 value = ioread32(reg);
915 value &= ~DISGR2_ISO_SEQ_ABORT_INT;
916 iowrite32(value, reg);
917 pr_info("fotg210 iso sequence abort\n");
919 if (int_grp2 & DISGR2_TX0BYTE_INT) {
920 fotg210_clear_tx0byte(fotg210);
921 value = ioread32(reg);
922 value &= ~DISGR2_TX0BYTE_INT;
923 iowrite32(value, reg);
924 pr_info("fotg210 transferred 0 byte\n");
926 if (int_grp2 & DISGR2_RX0BYTE_INT) {
927 fotg210_clear_rx0byte(fotg210);
928 value = ioread32(reg);
929 value &= ~DISGR2_RX0BYTE_INT;
930 iowrite32(value, reg);
931 pr_info("fotg210 received 0 byte\n");
933 if (int_grp2 & DISGR2_DMA_ERROR) {
934 value = ioread32(reg);
935 value &= ~DISGR2_DMA_ERROR;
936 iowrite32(value, reg);
940 if (int_grp & DIGR_INT_G0) {
941 void __iomem *reg = fotg210->reg + FOTG210_DISGR0;
942 u32 int_grp0 = ioread32(reg);
943 u32 int_msk0 = ioread32(fotg210->reg + FOTG210_DMISGR0);
944 struct usb_ctrlrequest ctrl;
946 int_grp0 &= ~int_msk0;
948 /* the highest priority in this source register */
949 if (int_grp0 & DISGR0_CX_COMABT_INT) {
950 fotg210_clear_comabt_int(fotg210);
951 pr_info("fotg210 CX command abort\n");
954 if (int_grp0 & DISGR0_CX_SETUP_INT) {
955 if (fotg210_setup_packet(fotg210, &ctrl)) {
956 spin_unlock(&fotg210->lock);
957 if (fotg210->driver->setup(&fotg210->gadget,
959 fotg210_set_cxstall(fotg210);
960 spin_lock(&fotg210->lock);
963 if (int_grp0 & DISGR0_CX_COMEND_INT)
964 pr_info("fotg210 cmd end\n");
966 if (int_grp0 & DISGR0_CX_IN_INT)
967 fotg210_ep0in(fotg210);
969 if (int_grp0 & DISGR0_CX_OUT_INT)
970 fotg210_ep0out(fotg210);
972 if (int_grp0 & DISGR0_CX_COMFAIL_INT) {
973 fotg210_set_cxstall(fotg210);
974 pr_info("fotg210 ep0 fail\n");
978 if (int_grp & DIGR_INT_G1) {
979 void __iomem *reg = fotg210->reg + FOTG210_DISGR1;
980 u32 int_grp1 = ioread32(reg);
981 u32 int_msk1 = ioread32(fotg210->reg + FOTG210_DMISGR1);
984 int_grp1 &= ~int_msk1;
986 for (fifo = 0; fifo < FOTG210_MAX_FIFO_NUM; fifo++) {
987 if (int_grp1 & DISGR1_IN_INT(fifo))
988 fotg210_in_fifo_handler(fotg210->ep[fifo + 1]);
990 if ((int_grp1 & DISGR1_OUT_INT(fifo)) ||
991 (int_grp1 & DISGR1_SPK_INT(fifo)))
992 fotg210_out_fifo_handler(fotg210->ep[fifo + 1]);
996 spin_unlock(&fotg210->lock);
1001 static void fotg210_disable_unplug(struct fotg210_udc *fotg210)
1003 u32 reg = ioread32(fotg210->reg + FOTG210_PHYTMSR);
1005 reg &= ~PHYTMSR_UNPLUG;
1006 iowrite32(reg, fotg210->reg + FOTG210_PHYTMSR);
1009 static int fotg210_udc_start(struct usb_gadget *g,
1010 struct usb_gadget_driver *driver)
1012 struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1016 /* hook up the driver */
1017 driver->driver.bus = NULL;
1018 fotg210->driver = driver;
1020 if (!IS_ERR_OR_NULL(fotg210->phy)) {
1021 ret = otg_set_peripheral(fotg210->phy->otg,
1024 dev_err(fotg210->dev, "can't bind to phy\n");
1027 /* enable device global interrupt */
1028 value = ioread32(fotg210->reg + FOTG210_DMCR);
1029 value |= DMCR_GLINT_EN;
1030 iowrite32(value, fotg210->reg + FOTG210_DMCR);
1035 static void fotg210_init(struct fotg210_udc *fotg210)
1039 /* disable global interrupt and set int polarity to active high */
1040 iowrite32(GMIR_MHC_INT | GMIR_MOTG_INT | GMIR_INT_POLARITY,
1041 fotg210->reg + FOTG210_GMIR);
1043 /* disable device global interrupt */
1044 value = ioread32(fotg210->reg + FOTG210_DMCR);
1045 value &= ~DMCR_GLINT_EN;
1046 iowrite32(value, fotg210->reg + FOTG210_DMCR);
1048 /* enable only grp2 irqs we handle */
1049 iowrite32(~(DISGR2_DMA_ERROR | DISGR2_RX0BYTE_INT | DISGR2_TX0BYTE_INT
1050 | DISGR2_ISO_SEQ_ABORT_INT | DISGR2_ISO_SEQ_ERR_INT
1051 | DISGR2_RESM_INT | DISGR2_SUSP_INT | DISGR2_USBRST_INT),
1052 fotg210->reg + FOTG210_DMISGR2);
1054 /* disable all fifo interrupt */
1055 iowrite32(~(u32)0, fotg210->reg + FOTG210_DMISGR1);
1057 /* disable cmd end */
1058 value = ioread32(fotg210->reg + FOTG210_DMISGR0);
1059 value |= DMISGR0_MCX_COMEND;
1060 iowrite32(value, fotg210->reg + FOTG210_DMISGR0);
1063 static int fotg210_udc_stop(struct usb_gadget *g)
1065 struct fotg210_udc *fotg210 = gadget_to_fotg210(g);
1066 unsigned long flags;
1068 if (!IS_ERR_OR_NULL(fotg210->phy))
1069 return otg_set_peripheral(fotg210->phy->otg, NULL);
1071 spin_lock_irqsave(&fotg210->lock, flags);
1073 fotg210_init(fotg210);
1074 fotg210->driver = NULL;
1076 spin_unlock_irqrestore(&fotg210->lock, flags);
1081 static const struct usb_gadget_ops fotg210_gadget_ops = {
1082 .udc_start = fotg210_udc_start,
1083 .udc_stop = fotg210_udc_stop,
1087 * fotg210_phy_event - Called by phy upon VBus event
1088 * @nb: notifier block
1089 * @action: phy action, is vbus connect or disconnect
1090 * @data: the usb_gadget structure in fotg210
1092 * Called by the USB Phy when a cable connect or disconnect is sensed.
1094 * Returns NOTIFY_OK or NOTIFY_DONE
1096 static int fotg210_phy_event(struct notifier_block *nb, unsigned long action,
1099 struct usb_gadget *gadget = data;
1105 case USB_EVENT_VBUS:
1106 usb_gadget_vbus_connect(gadget);
1108 case USB_EVENT_NONE:
1109 usb_gadget_vbus_disconnect(gadget);
1116 static struct notifier_block fotg210_phy_notifier = {
1117 .notifier_call = fotg210_phy_event,
1120 int fotg210_udc_remove(struct platform_device *pdev)
1122 struct fotg210_udc *fotg210 = platform_get_drvdata(pdev);
1125 usb_del_gadget_udc(&fotg210->gadget);
1126 if (!IS_ERR_OR_NULL(fotg210->phy)) {
1127 usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
1128 usb_put_phy(fotg210->phy);
1130 iounmap(fotg210->reg);
1131 free_irq(platform_get_irq(pdev, 0), fotg210);
1133 fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
1134 for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1135 kfree(fotg210->ep[i]);
1137 if (!IS_ERR(fotg210->pclk))
1138 clk_disable_unprepare(fotg210->pclk);
1145 int fotg210_udc_probe(struct platform_device *pdev)
1147 struct resource *res;
1148 struct fotg210_udc *fotg210 = NULL;
1149 struct device *dev = &pdev->dev;
1154 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1156 pr_err("platform_get_resource error.\n");
1160 irq = platform_get_irq(pdev, 0);
1162 pr_err("could not get irq\n");
1168 /* initialize udc */
1169 fotg210 = kzalloc(sizeof(struct fotg210_udc), GFP_KERNEL);
1170 if (fotg210 == NULL)
1175 /* It's OK not to supply this clock */
1176 fotg210->pclk = devm_clk_get(dev, "PCLK");
1177 if (!IS_ERR(fotg210->pclk)) {
1178 ret = clk_prepare_enable(fotg210->pclk);
1180 dev_err(dev, "failed to enable PCLK\n");
1183 } else if (PTR_ERR(fotg210->pclk) == -EPROBE_DEFER) {
1185 * Percolate deferrals, for anything else,
1186 * just live without the clocking.
1188 ret = -EPROBE_DEFER;
1192 fotg210->phy = devm_usb_get_phy_by_phandle(dev, "usb-phy", 0);
1193 if (IS_ERR(fotg210->phy)) {
1194 ret = PTR_ERR(fotg210->phy);
1195 if (ret == -EPROBE_DEFER)
1197 dev_info(dev, "no PHY found\n");
1198 fotg210->phy = NULL;
1200 ret = usb_phy_init(fotg210->phy);
1203 dev_info(dev, "found and initialized PHY\n");
1206 for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1207 fotg210->ep[i] = kzalloc(sizeof(struct fotg210_ep), GFP_KERNEL);
1208 if (!fotg210->ep[i])
1212 fotg210->reg = ioremap(res->start, resource_size(res));
1213 if (fotg210->reg == NULL) {
1214 dev_err(dev, "ioremap error\n");
1218 spin_lock_init(&fotg210->lock);
1220 platform_set_drvdata(pdev, fotg210);
1222 fotg210->gadget.ops = &fotg210_gadget_ops;
1224 fotg210->gadget.max_speed = USB_SPEED_HIGH;
1225 fotg210->gadget.dev.parent = dev;
1226 fotg210->gadget.dev.dma_mask = dev->dma_mask;
1227 fotg210->gadget.name = udc_name;
1229 INIT_LIST_HEAD(&fotg210->gadget.ep_list);
1231 for (i = 0; i < FOTG210_MAX_NUM_EP; i++) {
1232 struct fotg210_ep *ep = fotg210->ep[i];
1235 INIT_LIST_HEAD(&fotg210->ep[i]->ep.ep_list);
1236 list_add_tail(&fotg210->ep[i]->ep.ep_list,
1237 &fotg210->gadget.ep_list);
1239 ep->fotg210 = fotg210;
1240 INIT_LIST_HEAD(&ep->queue);
1241 ep->ep.name = fotg210_ep_name[i];
1242 ep->ep.ops = &fotg210_ep_ops;
1243 usb_ep_set_maxpacket_limit(&ep->ep, (unsigned short) ~0);
1246 ep->ep.caps.type_control = true;
1248 ep->ep.caps.type_iso = true;
1249 ep->ep.caps.type_bulk = true;
1250 ep->ep.caps.type_int = true;
1253 ep->ep.caps.dir_in = true;
1254 ep->ep.caps.dir_out = true;
1256 usb_ep_set_maxpacket_limit(&fotg210->ep[0]->ep, 0x40);
1257 fotg210->gadget.ep0 = &fotg210->ep[0]->ep;
1258 INIT_LIST_HEAD(&fotg210->gadget.ep0->ep_list);
1260 fotg210->ep0_req = fotg210_ep_alloc_request(&fotg210->ep[0]->ep,
1262 if (fotg210->ep0_req == NULL)
1265 fotg210_init(fotg210);
1267 fotg210_disable_unplug(fotg210);
1269 ret = request_irq(irq, fotg210_irq, IRQF_SHARED,
1272 dev_err(dev, "request_irq error (%d)\n", ret);
1276 if (!IS_ERR_OR_NULL(fotg210->phy))
1277 usb_register_notifier(fotg210->phy, &fotg210_phy_notifier);
1279 ret = usb_add_gadget_udc(dev, &fotg210->gadget);
1283 dev_info(dev, "version %s\n", DRIVER_VERSION);
1288 if (!IS_ERR_OR_NULL(fotg210->phy))
1289 usb_unregister_notifier(fotg210->phy, &fotg210_phy_notifier);
1290 free_irq(irq, fotg210);
1293 fotg210_ep_free_request(&fotg210->ep[0]->ep, fotg210->ep0_req);
1296 iounmap(fotg210->reg);
1299 for (i = 0; i < FOTG210_MAX_NUM_EP; i++)
1300 kfree(fotg210->ep[i]);
1302 if (!IS_ERR(fotg210->pclk))
1303 clk_disable_unprepare(fotg210->pclk);