1 // SPDX-License-Identifier: GPL-2.0
3 * Fusb300 UDC (USB gadget)
5 * Copyright (C) 2010 Faraday Technology Corp.
9 #include <linux/dma-mapping.h>
10 #include <linux/err.h>
11 #include <linux/interrupt.h>
13 #include <linux/module.h>
14 #include <linux/platform_device.h>
15 #include <linux/usb/ch9.h>
16 #include <linux/usb/gadget.h>
18 #include "fusb300_udc.h"
20 MODULE_DESCRIPTION("FUSB300 USB gadget driver");
21 MODULE_LICENSE("GPL");
23 MODULE_ALIAS("platform:fusb300_udc");
25 #define DRIVER_VERSION "20 October 2010"
27 static const char udc_name[] = "fusb300_udc";
28 static const char * const fusb300_ep_name[] = {
29 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7", "ep8", "ep9",
30 "ep10", "ep11", "ep12", "ep13", "ep14", "ep15"
33 static void done(struct fusb300_ep *ep, struct fusb300_request *req,
36 static void fusb300_enable_bit(struct fusb300 *fusb300, u32 offset,
39 u32 reg = ioread32(fusb300->reg + offset);
42 iowrite32(reg, fusb300->reg + offset);
45 static void fusb300_disable_bit(struct fusb300 *fusb300, u32 offset,
48 u32 reg = ioread32(fusb300->reg + offset);
51 iowrite32(reg, fusb300->reg + offset);
55 static void fusb300_ep_setting(struct fusb300_ep *ep,
56 struct fusb300_ep_info info)
58 ep->epnum = info.epnum;
62 static int fusb300_ep_release(struct fusb300_ep *ep)
72 static void fusb300_set_fifo_entry(struct fusb300 *fusb300,
75 u32 val = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
77 val &= ~FUSB300_EPSET1_FIFOENTRY_MSK;
78 val |= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM);
79 iowrite32(val, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
82 static void fusb300_set_start_entry(struct fusb300 *fusb300,
85 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
86 u32 start_entry = fusb300->fifo_entry_num * FUSB300_FIFO_ENTRY_NUM;
88 reg &= ~FUSB300_EPSET1_START_ENTRY_MSK ;
89 reg |= FUSB300_EPSET1_START_ENTRY(start_entry);
90 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
91 if (fusb300->fifo_entry_num == FUSB300_MAX_FIFO_ENTRY) {
92 fusb300->fifo_entry_num = 0;
94 pr_err("fifo entry is over the maximum number!\n");
96 fusb300->fifo_entry_num++;
99 /* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
100 static void fusb300_set_epaddrofs(struct fusb300 *fusb300,
101 struct fusb300_ep_info info)
103 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
105 reg &= ~FUSB300_EPSET2_ADDROFS_MSK;
106 reg |= FUSB300_EPSET2_ADDROFS(fusb300->addrofs);
107 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
108 fusb300->addrofs += (info.maxpacket + 7) / 8 * FUSB300_FIFO_ENTRY_NUM;
111 static void ep_fifo_setting(struct fusb300 *fusb300,
112 struct fusb300_ep_info info)
114 fusb300_set_fifo_entry(fusb300, info.epnum);
115 fusb300_set_start_entry(fusb300, info.epnum);
116 fusb300_set_epaddrofs(fusb300, info);
119 static void fusb300_set_eptype(struct fusb300 *fusb300,
120 struct fusb300_ep_info info)
122 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
124 reg &= ~FUSB300_EPSET1_TYPE_MSK;
125 reg |= FUSB300_EPSET1_TYPE(info.type);
126 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
129 static void fusb300_set_epdir(struct fusb300 *fusb300,
130 struct fusb300_ep_info info)
136 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
137 reg &= ~FUSB300_EPSET1_DIR_MSK;
138 reg |= FUSB300_EPSET1_DIRIN;
139 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
142 static void fusb300_set_ep_active(struct fusb300 *fusb300,
145 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
147 reg |= FUSB300_EPSET1_ACTEN;
148 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(ep));
151 static void fusb300_set_epmps(struct fusb300 *fusb300,
152 struct fusb300_ep_info info)
154 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
156 reg &= ~FUSB300_EPSET2_MPS_MSK;
157 reg |= FUSB300_EPSET2_MPS(info.maxpacket);
158 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET2(info.epnum));
161 static void fusb300_set_interval(struct fusb300 *fusb300,
162 struct fusb300_ep_info info)
164 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
166 reg &= ~FUSB300_EPSET1_INTERVAL(0x7);
167 reg |= FUSB300_EPSET1_INTERVAL(info.interval);
168 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
171 static void fusb300_set_bwnum(struct fusb300 *fusb300,
172 struct fusb300_ep_info info)
174 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
176 reg &= ~FUSB300_EPSET1_BWNUM(0x3);
177 reg |= FUSB300_EPSET1_BWNUM(info.bw_num);
178 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET1(info.epnum));
181 static void set_ep_reg(struct fusb300 *fusb300,
182 struct fusb300_ep_info info)
184 fusb300_set_eptype(fusb300, info);
185 fusb300_set_epdir(fusb300, info);
186 fusb300_set_epmps(fusb300, info);
189 fusb300_set_interval(fusb300, info);
192 fusb300_set_bwnum(fusb300, info);
194 fusb300_set_ep_active(fusb300, info.epnum);
197 static int config_ep(struct fusb300_ep *ep,
198 const struct usb_endpoint_descriptor *desc)
200 struct fusb300 *fusb300 = ep->fusb300;
201 struct fusb300_ep_info info;
209 info.type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
210 info.dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
211 info.maxpacket = usb_endpoint_maxp(desc);
212 info.epnum = desc->bEndpointAddress & USB_ENDPOINT_NUMBER_MASK;
214 if ((info.type == USB_ENDPOINT_XFER_INT) ||
215 (info.type == USB_ENDPOINT_XFER_ISOC)) {
216 info.interval = desc->bInterval;
217 if (info.type == USB_ENDPOINT_XFER_ISOC)
218 info.bw_num = usb_endpoint_maxp_mult(desc);
221 ep_fifo_setting(fusb300, info);
223 set_ep_reg(fusb300, info);
225 fusb300_ep_setting(ep, info);
227 fusb300->ep[info.epnum] = ep;
232 static int fusb300_enable(struct usb_ep *_ep,
233 const struct usb_endpoint_descriptor *desc)
235 struct fusb300_ep *ep;
237 ep = container_of(_ep, struct fusb300_ep, ep);
239 if (ep->fusb300->reenum) {
240 ep->fusb300->fifo_entry_num = 0;
241 ep->fusb300->addrofs = 0;
242 ep->fusb300->reenum = 0;
245 return config_ep(ep, desc);
248 static int fusb300_disable(struct usb_ep *_ep)
250 struct fusb300_ep *ep;
251 struct fusb300_request *req;
254 ep = container_of(_ep, struct fusb300_ep, ep);
258 while (!list_empty(&ep->queue)) {
259 req = list_entry(ep->queue.next, struct fusb300_request, queue);
260 spin_lock_irqsave(&ep->fusb300->lock, flags);
261 done(ep, req, -ECONNRESET);
262 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
265 return fusb300_ep_release(ep);
268 static struct usb_request *fusb300_alloc_request(struct usb_ep *_ep,
271 struct fusb300_request *req;
273 req = kzalloc(sizeof(struct fusb300_request), gfp_flags);
276 INIT_LIST_HEAD(&req->queue);
281 static void fusb300_free_request(struct usb_ep *_ep, struct usb_request *_req)
283 struct fusb300_request *req;
285 req = container_of(_req, struct fusb300_request, req);
289 static int enable_fifo_int(struct fusb300_ep *ep)
291 struct fusb300 *fusb300 = ep->fusb300;
294 fusb300_enable_bit(fusb300, FUSB300_OFFSET_IGER0,
295 FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
297 pr_err("can't enable_fifo_int ep0\n");
304 static int disable_fifo_int(struct fusb300_ep *ep)
306 struct fusb300 *fusb300 = ep->fusb300;
309 fusb300_disable_bit(fusb300, FUSB300_OFFSET_IGER0,
310 FUSB300_IGER0_EEPn_FIFO_INT(ep->epnum));
312 pr_err("can't disable_fifo_int ep0\n");
319 static void fusb300_set_cxlen(struct fusb300 *fusb300, u32 length)
323 reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
324 reg &= ~FUSB300_CSR_LEN_MSK;
325 reg |= FUSB300_CSR_LEN(length);
326 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_CSR);
329 /* write data to cx fifo */
330 static void fusb300_wrcxf(struct fusb300_ep *ep,
331 struct fusb300_request *req)
336 struct fusb300 *fusb300 = ep->fusb300;
337 u32 length = req->req.length - req->req.actual;
339 tmp = req->req.buf + req->req.actual;
341 if (length > SS_CTL_MAX_PACKET_SIZE) {
342 fusb300_set_cxlen(fusb300, SS_CTL_MAX_PACKET_SIZE);
343 for (i = (SS_CTL_MAX_PACKET_SIZE >> 2); i > 0; i--) {
344 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
346 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
349 req->req.actual += SS_CTL_MAX_PACKET_SIZE;
350 } else { /* length is less than max packet size */
351 fusb300_set_cxlen(fusb300, length);
352 for (i = length >> 2; i > 0; i--) {
353 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16 |
355 printk(KERN_DEBUG " 0x%x\n", data);
356 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
359 switch (length % 4) {
362 printk(KERN_DEBUG " 0x%x\n", data);
363 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
366 data = *tmp | *(tmp + 1) << 8;
367 printk(KERN_DEBUG " 0x%x\n", data);
368 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
371 data = *tmp | *(tmp + 1) << 8 | *(tmp + 2) << 16;
372 printk(KERN_DEBUG " 0x%x\n", data);
373 iowrite32(data, fusb300->reg + FUSB300_OFFSET_CXPORT);
378 req->req.actual += length;
382 static void fusb300_set_epnstall(struct fusb300 *fusb300, u8 ep)
384 fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
388 static void fusb300_clear_epnstall(struct fusb300 *fusb300, u8 ep)
390 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
392 if (reg & FUSB300_EPSET0_STL) {
393 printk(KERN_DEBUG "EP%d stall... Clear!!\n", ep);
394 reg |= FUSB300_EPSET0_STL_CLR;
395 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
399 static void ep0_queue(struct fusb300_ep *ep, struct fusb300_request *req)
401 if (ep->fusb300->ep0_dir) { /* if IN */
402 if (req->req.length) {
403 fusb300_wrcxf(ep, req);
405 printk(KERN_DEBUG "%s : req->req.length = 0x%x\n",
406 __func__, req->req.length);
407 if ((req->req.length == req->req.actual) ||
408 (req->req.actual < ep->ep.maxpacket))
411 if (!req->req.length)
414 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER1,
415 FUSB300_IGER1_CX_OUT_INT);
419 static int fusb300_queue(struct usb_ep *_ep, struct usb_request *_req,
422 struct fusb300_ep *ep;
423 struct fusb300_request *req;
427 ep = container_of(_ep, struct fusb300_ep, ep);
428 req = container_of(_req, struct fusb300_request, req);
430 if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
433 spin_lock_irqsave(&ep->fusb300->lock, flags);
435 if (list_empty(&ep->queue))
438 list_add_tail(&req->queue, &ep->queue);
441 req->req.status = -EINPROGRESS;
443 if (ep->ep.desc == NULL) /* ep0 */
445 else if (request && !ep->stall)
448 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
453 static int fusb300_dequeue(struct usb_ep *_ep, struct usb_request *_req)
455 struct fusb300_ep *ep;
456 struct fusb300_request *req;
459 ep = container_of(_ep, struct fusb300_ep, ep);
460 req = container_of(_req, struct fusb300_request, req);
462 spin_lock_irqsave(&ep->fusb300->lock, flags);
463 if (!list_empty(&ep->queue))
464 done(ep, req, -ECONNRESET);
465 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
470 static int fusb300_set_halt_and_wedge(struct usb_ep *_ep, int value, int wedge)
472 struct fusb300_ep *ep;
473 struct fusb300 *fusb300;
477 ep = container_of(_ep, struct fusb300_ep, ep);
479 fusb300 = ep->fusb300;
481 spin_lock_irqsave(&ep->fusb300->lock, flags);
483 if (!list_empty(&ep->queue)) {
489 fusb300_set_epnstall(fusb300, ep->epnum);
494 fusb300_clear_epnstall(fusb300, ep->epnum);
500 spin_unlock_irqrestore(&ep->fusb300->lock, flags);
504 static int fusb300_set_halt(struct usb_ep *_ep, int value)
506 return fusb300_set_halt_and_wedge(_ep, value, 0);
509 static int fusb300_set_wedge(struct usb_ep *_ep)
511 return fusb300_set_halt_and_wedge(_ep, 1, 1);
514 static void fusb300_fifo_flush(struct usb_ep *_ep)
518 static const struct usb_ep_ops fusb300_ep_ops = {
519 .enable = fusb300_enable,
520 .disable = fusb300_disable,
522 .alloc_request = fusb300_alloc_request,
523 .free_request = fusb300_free_request,
525 .queue = fusb300_queue,
526 .dequeue = fusb300_dequeue,
528 .set_halt = fusb300_set_halt,
529 .fifo_flush = fusb300_fifo_flush,
530 .set_wedge = fusb300_set_wedge,
533 /*****************************************************************************/
534 static void fusb300_clear_int(struct fusb300 *fusb300, u32 offset,
537 iowrite32(value, fusb300->reg + offset);
540 static void fusb300_reset(void)
544 static void fusb300_set_cxstall(struct fusb300 *fusb300)
546 fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
550 static void fusb300_set_cxdone(struct fusb300 *fusb300)
552 fusb300_enable_bit(fusb300, FUSB300_OFFSET_CSR,
556 /* read data from cx fifo */
557 static void fusb300_rdcxf(struct fusb300 *fusb300,
558 u8 *buffer, u32 length)
566 for (i = (length >> 2); i > 0; i--) {
567 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
568 printk(KERN_DEBUG " 0x%x\n", data);
570 *(tmp + 1) = (data >> 8) & 0xFF;
571 *(tmp + 2) = (data >> 16) & 0xFF;
572 *(tmp + 3) = (data >> 24) & 0xFF;
576 switch (length % 4) {
578 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
579 printk(KERN_DEBUG " 0x%x\n", data);
583 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
584 printk(KERN_DEBUG " 0x%x\n", data);
586 *(tmp + 1) = (data >> 8) & 0xFF;
589 data = ioread32(fusb300->reg + FUSB300_OFFSET_CXPORT);
590 printk(KERN_DEBUG " 0x%x\n", data);
592 *(tmp + 1) = (data >> 8) & 0xFF;
593 *(tmp + 2) = (data >> 16) & 0xFF;
600 static void fusb300_rdfifo(struct fusb300_ep *ep,
601 struct fusb300_request *req,
607 struct fusb300 *fusb300 = ep->fusb300;
609 tmp = req->req.buf + req->req.actual;
610 req->req.actual += length;
612 if (req->req.actual > req->req.length)
613 printk(KERN_DEBUG "req->req.actual > req->req.length\n");
615 for (i = (length >> 2); i > 0; i--) {
616 data = ioread32(fusb300->reg +
617 FUSB300_OFFSET_EPPORT(ep->epnum));
619 *(tmp + 1) = (data >> 8) & 0xFF;
620 *(tmp + 2) = (data >> 16) & 0xFF;
621 *(tmp + 3) = (data >> 24) & 0xFF;
625 switch (length % 4) {
627 data = ioread32(fusb300->reg +
628 FUSB300_OFFSET_EPPORT(ep->epnum));
632 data = ioread32(fusb300->reg +
633 FUSB300_OFFSET_EPPORT(ep->epnum));
635 *(tmp + 1) = (data >> 8) & 0xFF;
638 data = ioread32(fusb300->reg +
639 FUSB300_OFFSET_EPPORT(ep->epnum));
641 *(tmp + 1) = (data >> 8) & 0xFF;
642 *(tmp + 2) = (data >> 16) & 0xFF;
649 reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
650 reg &= FUSB300_IGR1_SYNF0_EMPTY_INT;
652 printk(KERN_INFO "sync fifo is not empty!\n");
657 static u8 fusb300_get_epnstall(struct fusb300 *fusb300, u8 ep)
660 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPSET0(ep));
662 value = reg & FUSB300_EPSET0_STL;
667 static u8 fusb300_get_cxstall(struct fusb300 *fusb300)
670 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_CSR);
672 value = (reg & FUSB300_CSR_STL) >> 1;
677 static void request_error(struct fusb300 *fusb300)
679 fusb300_set_cxstall(fusb300);
680 printk(KERN_DEBUG "request error!!\n");
683 static void get_status(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
684 __releases(fusb300->lock)
685 __acquires(fusb300->lock)
689 u16 w_index = ctrl->wIndex;
691 switch (ctrl->bRequestType & USB_RECIP_MASK) {
692 case USB_RECIP_DEVICE:
693 status = 1 << USB_DEVICE_SELF_POWERED;
695 case USB_RECIP_INTERFACE:
698 case USB_RECIP_ENDPOINT:
699 ep = w_index & USB_ENDPOINT_NUMBER_MASK;
701 if (fusb300_get_epnstall(fusb300, ep))
702 status = 1 << USB_ENDPOINT_HALT;
704 if (fusb300_get_cxstall(fusb300))
710 request_error(fusb300);
714 fusb300->ep0_data = cpu_to_le16(status);
715 fusb300->ep0_req->buf = &fusb300->ep0_data;
716 fusb300->ep0_req->length = 2;
718 spin_unlock(&fusb300->lock);
719 fusb300_queue(fusb300->gadget.ep0, fusb300->ep0_req, GFP_KERNEL);
720 spin_lock(&fusb300->lock);
723 static void set_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
727 switch (ctrl->bRequestType & USB_RECIP_MASK) {
728 case USB_RECIP_DEVICE:
729 fusb300_set_cxdone(fusb300);
731 case USB_RECIP_INTERFACE:
732 fusb300_set_cxdone(fusb300);
734 case USB_RECIP_ENDPOINT: {
735 u16 w_index = le16_to_cpu(ctrl->wIndex);
737 ep = w_index & USB_ENDPOINT_NUMBER_MASK;
739 fusb300_set_epnstall(fusb300, ep);
741 fusb300_set_cxstall(fusb300);
742 fusb300_set_cxdone(fusb300);
746 request_error(fusb300);
751 static void fusb300_clear_seqnum(struct fusb300 *fusb300, u8 ep)
753 fusb300_enable_bit(fusb300, FUSB300_OFFSET_EPSET0(ep),
754 FUSB300_EPSET0_CLRSEQNUM);
757 static void clear_feature(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
759 struct fusb300_ep *ep =
760 fusb300->ep[ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK];
762 switch (ctrl->bRequestType & USB_RECIP_MASK) {
763 case USB_RECIP_DEVICE:
764 fusb300_set_cxdone(fusb300);
766 case USB_RECIP_INTERFACE:
767 fusb300_set_cxdone(fusb300);
769 case USB_RECIP_ENDPOINT:
770 if (ctrl->wIndex & USB_ENDPOINT_NUMBER_MASK) {
772 fusb300_set_cxdone(fusb300);
777 fusb300_clear_seqnum(fusb300, ep->epnum);
778 fusb300_clear_epnstall(fusb300, ep->epnum);
779 if (!list_empty(&ep->queue))
783 fusb300_set_cxdone(fusb300);
786 request_error(fusb300);
791 static void fusb300_set_dev_addr(struct fusb300 *fusb300, u16 addr)
793 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_DAR);
795 reg &= ~FUSB300_DAR_DRVADDR_MSK;
796 reg |= FUSB300_DAR_DRVADDR(addr);
798 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_DAR);
801 static void set_address(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
803 if (ctrl->wValue >= 0x0100)
804 request_error(fusb300);
806 fusb300_set_dev_addr(fusb300, ctrl->wValue);
807 fusb300_set_cxdone(fusb300);
811 #define UVC_COPY_DESCRIPTORS(mem, src) \
813 const struct usb_descriptor_header * const *__src; \
814 for (__src = src; *__src; ++__src) { \
815 memcpy(mem, *__src, (*__src)->bLength); \
816 mem += (*__src)->bLength; \
820 static int setup_packet(struct fusb300 *fusb300, struct usb_ctrlrequest *ctrl)
826 fusb300_rdcxf(fusb300, p, 8);
827 fusb300->ep0_dir = ctrl->bRequestType & USB_DIR_IN;
828 fusb300->ep0_length = ctrl->wLength;
831 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
832 switch (ctrl->bRequest) {
833 case USB_REQ_GET_STATUS:
834 get_status(fusb300, ctrl);
836 case USB_REQ_CLEAR_FEATURE:
837 clear_feature(fusb300, ctrl);
839 case USB_REQ_SET_FEATURE:
840 set_feature(fusb300, ctrl);
842 case USB_REQ_SET_ADDRESS:
843 set_address(fusb300, ctrl);
845 case USB_REQ_SET_CONFIGURATION:
846 fusb300_enable_bit(fusb300, FUSB300_OFFSET_DAR,
847 FUSB300_DAR_SETCONFG);
848 /* clear sequence number */
849 for (i = 1; i <= FUSB300_MAX_NUM_EP; i++)
850 fusb300_clear_seqnum(fusb300, i);
864 static void done(struct fusb300_ep *ep, struct fusb300_request *req,
867 list_del_init(&req->queue);
869 /* don't modify queue heads during completion callback */
870 if (ep->fusb300->gadget.speed == USB_SPEED_UNKNOWN)
871 req->req.status = -ESHUTDOWN;
873 req->req.status = status;
875 spin_unlock(&ep->fusb300->lock);
876 usb_gadget_giveback_request(&ep->ep, &req->req);
877 spin_lock(&ep->fusb300->lock);
880 disable_fifo_int(ep);
881 if (!list_empty(&ep->queue))
884 fusb300_set_cxdone(ep->fusb300);
887 static void fusb300_fill_idma_prdtbl(struct fusb300_ep *ep, dma_addr_t d,
895 reg = ioread32(ep->fusb300->reg +
896 FUSB300_OFFSET_EPPRD_W0(ep->epnum));
897 reg &= FUSB300_EPPRD0_H;
900 iowrite32(d, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W1(ep->epnum));
902 value = FUSB300_EPPRD0_BTC(len) | FUSB300_EPPRD0_H |
903 FUSB300_EPPRD0_F | FUSB300_EPPRD0_L | FUSB300_EPPRD0_I;
904 iowrite32(value, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W0(ep->epnum));
906 iowrite32(0x0, ep->fusb300->reg + FUSB300_OFFSET_EPPRD_W2(ep->epnum));
908 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_EPPRDRDY,
909 FUSB300_EPPRDR_EP_PRD_RDY(ep->epnum));
912 static void fusb300_wait_idma_finished(struct fusb300_ep *ep)
917 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR1);
918 if ((reg & FUSB300_IGR1_VBUS_CHG_INT) ||
919 (reg & FUSB300_IGR1_WARM_RST_INT) ||
920 (reg & FUSB300_IGR1_HOT_RST_INT) ||
921 (reg & FUSB300_IGR1_USBRST_INT)
924 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGR0);
925 reg &= FUSB300_IGR0_EPn_PRD_INT(ep->epnum);
928 fusb300_clear_int(ep->fusb300, FUSB300_OFFSET_IGR0,
929 FUSB300_IGR0_EPn_PRD_INT(ep->epnum));
933 reg = ioread32(ep->fusb300->reg + FUSB300_OFFSET_IGER0);
934 reg &= ~FUSB300_IGER0_EEPn_PRD_INT(ep->epnum);
935 iowrite32(reg, ep->fusb300->reg + FUSB300_OFFSET_IGER0);
938 static void fusb300_set_idma(struct fusb300_ep *ep,
939 struct fusb300_request *req)
943 ret = usb_gadget_map_request(&ep->fusb300->gadget,
944 &req->req, DMA_TO_DEVICE);
948 fusb300_enable_bit(ep->fusb300, FUSB300_OFFSET_IGER0,
949 FUSB300_IGER0_EEPn_PRD_INT(ep->epnum));
951 fusb300_fill_idma_prdtbl(ep, req->req.dma, req->req.length);
952 /* check idma is done */
953 fusb300_wait_idma_finished(ep);
955 usb_gadget_unmap_request(&ep->fusb300->gadget,
956 &req->req, DMA_TO_DEVICE);
959 static void in_ep_fifo_handler(struct fusb300_ep *ep)
961 struct fusb300_request *req = list_entry(ep->queue.next,
962 struct fusb300_request, queue);
965 fusb300_set_idma(ep, req);
969 static void out_ep_fifo_handler(struct fusb300_ep *ep)
971 struct fusb300 *fusb300 = ep->fusb300;
972 struct fusb300_request *req = list_entry(ep->queue.next,
973 struct fusb300_request, queue);
974 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_EPFFR(ep->epnum));
975 u32 length = reg & FUSB300_FFR_BYCNT;
977 fusb300_rdfifo(ep, req, length);
979 /* finish out transfer */
980 if ((req->req.length == req->req.actual) || (length < ep->ep.maxpacket))
984 static void check_device_mode(struct fusb300 *fusb300)
986 u32 reg = ioread32(fusb300->reg + FUSB300_OFFSET_GCR);
988 switch (reg & FUSB300_GCR_DEVEN_MSK) {
989 case FUSB300_GCR_DEVEN_SS:
990 fusb300->gadget.speed = USB_SPEED_SUPER;
992 case FUSB300_GCR_DEVEN_HS:
993 fusb300->gadget.speed = USB_SPEED_HIGH;
995 case FUSB300_GCR_DEVEN_FS:
996 fusb300->gadget.speed = USB_SPEED_FULL;
999 fusb300->gadget.speed = USB_SPEED_UNKNOWN;
1002 printk(KERN_INFO "dev_mode = %d\n", (reg & FUSB300_GCR_DEVEN_MSK));
1006 static void fusb300_ep0out(struct fusb300 *fusb300)
1008 struct fusb300_ep *ep = fusb300->ep[0];
1011 if (!list_empty(&ep->queue)) {
1012 struct fusb300_request *req;
1014 req = list_first_entry(&ep->queue,
1015 struct fusb300_request, queue);
1016 if (req->req.length)
1017 fusb300_rdcxf(ep->fusb300, req->req.buf,
1020 reg = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
1021 reg &= ~FUSB300_IGER1_CX_OUT_INT;
1022 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_IGER1);
1024 pr_err("%s : empty queue\n", __func__);
1027 static void fusb300_ep0in(struct fusb300 *fusb300)
1029 struct fusb300_request *req;
1030 struct fusb300_ep *ep = fusb300->ep[0];
1032 if ((!list_empty(&ep->queue)) && (fusb300->ep0_dir)) {
1033 req = list_entry(ep->queue.next,
1034 struct fusb300_request, queue);
1035 if (req->req.length)
1036 fusb300_wrcxf(ep, req);
1037 if ((req->req.length - req->req.actual) < ep->ep.maxpacket)
1040 fusb300_set_cxdone(fusb300);
1043 static void fusb300_grp2_handler(void)
1047 static void fusb300_grp3_handler(void)
1051 static void fusb300_grp4_handler(void)
1055 static void fusb300_grp5_handler(void)
1059 static irqreturn_t fusb300_irq(int irq, void *_fusb300)
1061 struct fusb300 *fusb300 = _fusb300;
1062 u32 int_grp1 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR1);
1063 u32 int_grp1_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER1);
1064 u32 int_grp0 = ioread32(fusb300->reg + FUSB300_OFFSET_IGR0);
1065 u32 int_grp0_en = ioread32(fusb300->reg + FUSB300_OFFSET_IGER0);
1066 struct usb_ctrlrequest ctrl;
1071 spin_lock(&fusb300->lock);
1073 int_grp1 &= int_grp1_en;
1074 int_grp0 &= int_grp0_en;
1076 if (int_grp1 & FUSB300_IGR1_WARM_RST_INT) {
1077 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1078 FUSB300_IGR1_WARM_RST_INT);
1079 printk(KERN_INFO"fusb300_warmreset\n");
1083 if (int_grp1 & FUSB300_IGR1_HOT_RST_INT) {
1084 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1085 FUSB300_IGR1_HOT_RST_INT);
1086 printk(KERN_INFO"fusb300_hotreset\n");
1090 if (int_grp1 & FUSB300_IGR1_USBRST_INT) {
1091 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1092 FUSB300_IGR1_USBRST_INT);
1095 /* COMABT_INT has a highest priority */
1097 if (int_grp1 & FUSB300_IGR1_CX_COMABT_INT) {
1098 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1099 FUSB300_IGR1_CX_COMABT_INT);
1100 printk(KERN_INFO"fusb300_ep0abt\n");
1103 if (int_grp1 & FUSB300_IGR1_VBUS_CHG_INT) {
1104 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1105 FUSB300_IGR1_VBUS_CHG_INT);
1106 printk(KERN_INFO"fusb300_vbus_change\n");
1109 if (int_grp1 & FUSB300_IGR1_U3_EXIT_FAIL_INT) {
1110 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1111 FUSB300_IGR1_U3_EXIT_FAIL_INT);
1114 if (int_grp1 & FUSB300_IGR1_U2_EXIT_FAIL_INT) {
1115 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1116 FUSB300_IGR1_U2_EXIT_FAIL_INT);
1119 if (int_grp1 & FUSB300_IGR1_U1_EXIT_FAIL_INT) {
1120 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1121 FUSB300_IGR1_U1_EXIT_FAIL_INT);
1124 if (int_grp1 & FUSB300_IGR1_U2_ENTRY_FAIL_INT) {
1125 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1126 FUSB300_IGR1_U2_ENTRY_FAIL_INT);
1129 if (int_grp1 & FUSB300_IGR1_U1_ENTRY_FAIL_INT) {
1130 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1131 FUSB300_IGR1_U1_ENTRY_FAIL_INT);
1134 if (int_grp1 & FUSB300_IGR1_U3_EXIT_INT) {
1135 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1136 FUSB300_IGR1_U3_EXIT_INT);
1137 printk(KERN_INFO "FUSB300_IGR1_U3_EXIT_INT\n");
1140 if (int_grp1 & FUSB300_IGR1_U2_EXIT_INT) {
1141 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1142 FUSB300_IGR1_U2_EXIT_INT);
1143 printk(KERN_INFO "FUSB300_IGR1_U2_EXIT_INT\n");
1146 if (int_grp1 & FUSB300_IGR1_U1_EXIT_INT) {
1147 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1148 FUSB300_IGR1_U1_EXIT_INT);
1149 printk(KERN_INFO "FUSB300_IGR1_U1_EXIT_INT\n");
1152 if (int_grp1 & FUSB300_IGR1_U3_ENTRY_INT) {
1153 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1154 FUSB300_IGR1_U3_ENTRY_INT);
1155 printk(KERN_INFO "FUSB300_IGR1_U3_ENTRY_INT\n");
1156 fusb300_enable_bit(fusb300, FUSB300_OFFSET_SSCR1,
1157 FUSB300_SSCR1_GO_U3_DONE);
1160 if (int_grp1 & FUSB300_IGR1_U2_ENTRY_INT) {
1161 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1162 FUSB300_IGR1_U2_ENTRY_INT);
1163 printk(KERN_INFO "FUSB300_IGR1_U2_ENTRY_INT\n");
1166 if (int_grp1 & FUSB300_IGR1_U1_ENTRY_INT) {
1167 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1168 FUSB300_IGR1_U1_ENTRY_INT);
1169 printk(KERN_INFO "FUSB300_IGR1_U1_ENTRY_INT\n");
1172 if (int_grp1 & FUSB300_IGR1_RESM_INT) {
1173 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1174 FUSB300_IGR1_RESM_INT);
1175 printk(KERN_INFO "fusb300_resume\n");
1178 if (int_grp1 & FUSB300_IGR1_SUSP_INT) {
1179 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1180 FUSB300_IGR1_SUSP_INT);
1181 printk(KERN_INFO "fusb300_suspend\n");
1184 if (int_grp1 & FUSB300_IGR1_HS_LPM_INT) {
1185 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1186 FUSB300_IGR1_HS_LPM_INT);
1187 printk(KERN_INFO "fusb300_HS_LPM_INT\n");
1190 if (int_grp1 & FUSB300_IGR1_DEV_MODE_CHG_INT) {
1191 fusb300_clear_int(fusb300, FUSB300_OFFSET_IGR1,
1192 FUSB300_IGR1_DEV_MODE_CHG_INT);
1193 check_device_mode(fusb300);
1196 if (int_grp1 & FUSB300_IGR1_CX_COMFAIL_INT) {
1197 fusb300_set_cxstall(fusb300);
1198 printk(KERN_INFO "fusb300_ep0fail\n");
1201 if (int_grp1 & FUSB300_IGR1_CX_SETUP_INT) {
1202 printk(KERN_INFO "fusb300_ep0setup\n");
1203 if (setup_packet(fusb300, &ctrl)) {
1204 spin_unlock(&fusb300->lock);
1205 if (fusb300->driver->setup(&fusb300->gadget, &ctrl) < 0)
1206 fusb300_set_cxstall(fusb300);
1207 spin_lock(&fusb300->lock);
1211 if (int_grp1 & FUSB300_IGR1_CX_CMDEND_INT)
1212 printk(KERN_INFO "fusb300_cmdend\n");
1215 if (int_grp1 & FUSB300_IGR1_CX_OUT_INT) {
1216 printk(KERN_INFO "fusb300_cxout\n");
1217 fusb300_ep0out(fusb300);
1220 if (int_grp1 & FUSB300_IGR1_CX_IN_INT) {
1221 printk(KERN_INFO "fusb300_cxin\n");
1222 fusb300_ep0in(fusb300);
1225 if (int_grp1 & FUSB300_IGR1_INTGRP5)
1226 fusb300_grp5_handler();
1228 if (int_grp1 & FUSB300_IGR1_INTGRP4)
1229 fusb300_grp4_handler();
1231 if (int_grp1 & FUSB300_IGR1_INTGRP3)
1232 fusb300_grp3_handler();
1234 if (int_grp1 & FUSB300_IGR1_INTGRP2)
1235 fusb300_grp2_handler();
1238 for (i = 1; i < FUSB300_MAX_NUM_EP; i++) {
1239 if (int_grp0 & FUSB300_IGR0_EPn_FIFO_INT(i)) {
1240 reg = ioread32(fusb300->reg +
1241 FUSB300_OFFSET_EPSET1(i));
1242 in = (reg & FUSB300_EPSET1_DIRIN) ? 1 : 0;
1244 in_ep_fifo_handler(fusb300->ep[i]);
1246 out_ep_fifo_handler(fusb300->ep[i]);
1251 spin_unlock(&fusb300->lock);
1256 static void fusb300_set_u2_timeout(struct fusb300 *fusb300,
1261 reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
1263 reg |= FUSB300_SSCR2_U2TIMEOUT(time);
1265 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
1268 static void fusb300_set_u1_timeout(struct fusb300 *fusb300,
1273 reg = ioread32(fusb300->reg + FUSB300_OFFSET_TT);
1274 reg &= ~(0xff << 8);
1275 reg |= FUSB300_SSCR2_U1TIMEOUT(time);
1277 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_TT);
1280 static void init_controller(struct fusb300 *fusb300)
1287 mask = val = FUSB300_AHBBCR_S0_SPLIT_ON | FUSB300_AHBBCR_S1_SPLIT_ON;
1288 reg = ioread32(fusb300->reg + FUSB300_OFFSET_AHBCR);
1291 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_AHBCR);
1293 /* enable high-speed LPM */
1294 mask = val = FUSB300_HSCR_HS_LPM_PERMIT;
1295 reg = ioread32(fusb300->reg + FUSB300_OFFSET_HSCR);
1298 iowrite32(reg, fusb300->reg + FUSB300_OFFSET_HSCR);
1300 /*set u1 u2 timmer*/
1301 fusb300_set_u2_timeout(fusb300, 0xff);
1302 fusb300_set_u1_timeout(fusb300, 0xff);
1304 /* enable all grp1 interrupt */
1305 iowrite32(0xcfffff9f, fusb300->reg + FUSB300_OFFSET_IGER1);
1307 /*------------------------------------------------------------------------*/
1308 static int fusb300_udc_start(struct usb_gadget *g,
1309 struct usb_gadget_driver *driver)
1311 struct fusb300 *fusb300 = to_fusb300(g);
1313 /* hook up the driver */
1314 fusb300->driver = driver;
1319 static int fusb300_udc_stop(struct usb_gadget *g)
1321 struct fusb300 *fusb300 = to_fusb300(g);
1323 init_controller(fusb300);
1324 fusb300->driver = NULL;
1328 /*--------------------------------------------------------------------------*/
1330 static int fusb300_udc_pullup(struct usb_gadget *_gadget, int is_active)
1335 static const struct usb_gadget_ops fusb300_gadget_ops = {
1336 .pullup = fusb300_udc_pullup,
1337 .udc_start = fusb300_udc_start,
1338 .udc_stop = fusb300_udc_stop,
1341 static int fusb300_remove(struct platform_device *pdev)
1343 struct fusb300 *fusb300 = platform_get_drvdata(pdev);
1346 usb_del_gadget_udc(&fusb300->gadget);
1347 iounmap(fusb300->reg);
1348 free_irq(platform_get_irq(pdev, 0), fusb300);
1350 fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
1351 for (i = 0; i < FUSB300_MAX_NUM_EP; i++)
1352 kfree(fusb300->ep[i]);
1358 static int fusb300_probe(struct platform_device *pdev)
1360 struct resource *res, *ires, *ires1;
1361 void __iomem *reg = NULL;
1362 struct fusb300 *fusb300 = NULL;
1363 struct fusb300_ep *_ep[FUSB300_MAX_NUM_EP];
1367 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1370 pr_err("platform_get_resource error.\n");
1374 ires = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1378 "platform_get_resource IORESOURCE_IRQ error.\n");
1382 ires1 = platform_get_resource(pdev, IORESOURCE_IRQ, 1);
1386 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1390 reg = ioremap(res->start, resource_size(res));
1393 pr_err("ioremap error.\n");
1397 /* initialize udc */
1398 fusb300 = kzalloc(sizeof(struct fusb300), GFP_KERNEL);
1399 if (fusb300 == NULL) {
1404 for (i = 0; i < FUSB300_MAX_NUM_EP; i++) {
1405 _ep[i] = kzalloc(sizeof(struct fusb300_ep), GFP_KERNEL);
1406 if (_ep[i] == NULL) {
1410 fusb300->ep[i] = _ep[i];
1413 spin_lock_init(&fusb300->lock);
1415 platform_set_drvdata(pdev, fusb300);
1417 fusb300->gadget.ops = &fusb300_gadget_ops;
1419 fusb300->gadget.max_speed = USB_SPEED_HIGH;
1420 fusb300->gadget.name = udc_name;
1423 ret = request_irq(ires->start, fusb300_irq, IRQF_SHARED,
1426 pr_err("request_irq error (%d)\n", ret);
1430 ret = request_irq(ires1->start, fusb300_irq,
1431 IRQF_SHARED, udc_name, fusb300);
1433 pr_err("request_irq1 error (%d)\n", ret);
1437 INIT_LIST_HEAD(&fusb300->gadget.ep_list);
1439 for (i = 0; i < FUSB300_MAX_NUM_EP ; i++) {
1440 struct fusb300_ep *ep = fusb300->ep[i];
1443 INIT_LIST_HEAD(&fusb300->ep[i]->ep.ep_list);
1444 list_add_tail(&fusb300->ep[i]->ep.ep_list,
1445 &fusb300->gadget.ep_list);
1447 ep->fusb300 = fusb300;
1448 INIT_LIST_HEAD(&ep->queue);
1449 ep->ep.name = fusb300_ep_name[i];
1450 ep->ep.ops = &fusb300_ep_ops;
1451 usb_ep_set_maxpacket_limit(&ep->ep, HS_BULK_MAX_PACKET_SIZE);
1454 ep->ep.caps.type_control = true;
1456 ep->ep.caps.type_iso = true;
1457 ep->ep.caps.type_bulk = true;
1458 ep->ep.caps.type_int = true;
1461 ep->ep.caps.dir_in = true;
1462 ep->ep.caps.dir_out = true;
1464 usb_ep_set_maxpacket_limit(&fusb300->ep[0]->ep, HS_CTL_MAX_PACKET_SIZE);
1465 fusb300->ep[0]->epnum = 0;
1466 fusb300->gadget.ep0 = &fusb300->ep[0]->ep;
1467 INIT_LIST_HEAD(&fusb300->gadget.ep0->ep_list);
1469 fusb300->ep0_req = fusb300_alloc_request(&fusb300->ep[0]->ep,
1471 if (fusb300->ep0_req == NULL) {
1476 init_controller(fusb300);
1477 ret = usb_add_gadget_udc(&pdev->dev, &fusb300->gadget);
1481 dev_info(&pdev->dev, "version %s\n", DRIVER_VERSION);
1486 fusb300_free_request(&fusb300->ep[0]->ep, fusb300->ep0_req);
1489 free_irq(ires->start, fusb300);
1493 if (fusb300->ep0_req)
1494 fusb300_free_request(&fusb300->ep[0]->ep,
1496 for (i = 0; i < FUSB300_MAX_NUM_EP; i++)
1497 kfree(fusb300->ep[i]);
1506 static struct platform_driver fusb300_driver = {
1507 .remove = fusb300_remove,
1513 module_platform_driver_probe(fusb300_driver, fusb300_probe);