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>
21 #define DRIVER_DESC "FOTG210 USB Device Controller Driver"
22 #define DRIVER_VERSION "30-April-2013"
24 static const char udc_name
[] = "fotg210_udc";
25 static const char * const fotg210_ep_name
[] = {
26 "ep0", "ep1", "ep2", "ep3", "ep4"};
28 static void fotg210_disable_fifo_int(struct fotg210_ep
*ep
)
30 u32 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
33 value
|= DMISGR1_MF_IN_INT(ep
->epnum
- 1);
35 value
|= DMISGR1_MF_OUTSPK_INT(ep
->epnum
- 1);
36 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
39 static void fotg210_enable_fifo_int(struct fotg210_ep
*ep
)
41 u32 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
44 value
&= ~DMISGR1_MF_IN_INT(ep
->epnum
- 1);
46 value
&= ~DMISGR1_MF_OUTSPK_INT(ep
->epnum
- 1);
47 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
50 static void fotg210_set_cxdone(struct fotg210_udc
*fotg210
)
52 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DCFESR
);
54 value
|= DCFESR_CX_DONE
;
55 iowrite32(value
, fotg210
->reg
+ FOTG210_DCFESR
);
58 static void fotg210_done(struct fotg210_ep
*ep
, struct fotg210_request
*req
,
61 list_del_init(&req
->queue
);
63 /* don't modify queue heads during completion callback */
64 if (ep
->fotg210
->gadget
.speed
== USB_SPEED_UNKNOWN
)
65 req
->req
.status
= -ESHUTDOWN
;
67 req
->req
.status
= status
;
69 spin_unlock(&ep
->fotg210
->lock
);
70 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
71 spin_lock(&ep
->fotg210
->lock
);
74 if (list_empty(&ep
->queue
))
75 fotg210_disable_fifo_int(ep
);
77 fotg210_set_cxdone(ep
->fotg210
);
81 static void fotg210_fifo_ep_mapping(struct fotg210_ep
*ep
, u32 epnum
,
84 struct fotg210_udc
*fotg210
= ep
->fotg210
;
87 /* Driver should map an ep to a fifo and then map the fifo
88 * to the ep. What a brain-damaged design!
91 /* map a fifo to an ep */
92 val
= ioread32(fotg210
->reg
+ FOTG210_EPMAP
);
93 val
&= ~EPMAP_FIFONOMSK(epnum
, dir_in
);
94 val
|= EPMAP_FIFONO(epnum
, dir_in
);
95 iowrite32(val
, fotg210
->reg
+ FOTG210_EPMAP
);
97 /* map the ep to the fifo */
98 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOMAP
);
99 val
&= ~FIFOMAP_EPNOMSK(epnum
);
100 val
|= FIFOMAP_EPNO(epnum
);
101 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOMAP
);
104 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOCF
);
105 val
|= FIFOCF_FIFO_EN(epnum
- 1);
106 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOCF
);
109 static void fotg210_set_fifo_dir(struct fotg210_ep
*ep
, u32 epnum
, u32 dir_in
)
111 struct fotg210_udc
*fotg210
= ep
->fotg210
;
114 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOMAP
);
115 val
|= (dir_in
? FIFOMAP_DIRIN(epnum
- 1) : FIFOMAP_DIROUT(epnum
- 1));
116 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOMAP
);
119 static void fotg210_set_tfrtype(struct fotg210_ep
*ep
, u32 epnum
, u32 type
)
121 struct fotg210_udc
*fotg210
= ep
->fotg210
;
124 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOCF
);
125 val
|= FIFOCF_TYPE(type
, epnum
- 1);
126 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOCF
);
129 static void fotg210_set_mps(struct fotg210_ep
*ep
, u32 epnum
, u32 mps
,
132 struct fotg210_udc
*fotg210
= ep
->fotg210
;
134 u32 offset
= dir_in
? FOTG210_INEPMPSR(epnum
) :
135 FOTG210_OUTEPMPSR(epnum
);
137 val
= ioread32(fotg210
->reg
+ offset
);
138 val
|= INOUTEPMPSR_MPS(mps
);
139 iowrite32(val
, fotg210
->reg
+ offset
);
142 static int fotg210_config_ep(struct fotg210_ep
*ep
,
143 const struct usb_endpoint_descriptor
*desc
)
145 struct fotg210_udc
*fotg210
= ep
->fotg210
;
147 fotg210_set_fifo_dir(ep
, ep
->epnum
, ep
->dir_in
);
148 fotg210_set_tfrtype(ep
, ep
->epnum
, ep
->type
);
149 fotg210_set_mps(ep
, ep
->epnum
, ep
->ep
.maxpacket
, ep
->dir_in
);
150 fotg210_fifo_ep_mapping(ep
, ep
->epnum
, ep
->dir_in
);
152 fotg210
->ep
[ep
->epnum
] = ep
;
157 static int fotg210_ep_enable(struct usb_ep
*_ep
,
158 const struct usb_endpoint_descriptor
*desc
)
160 struct fotg210_ep
*ep
;
162 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
165 ep
->epnum
= usb_endpoint_num(desc
);
166 ep
->type
= usb_endpoint_type(desc
);
167 ep
->dir_in
= usb_endpoint_dir_in(desc
);
168 ep
->ep
.maxpacket
= usb_endpoint_maxp(desc
);
170 return fotg210_config_ep(ep
, desc
);
173 static void fotg210_reset_tseq(struct fotg210_udc
*fotg210
, u8 epnum
)
175 struct fotg210_ep
*ep
= fotg210
->ep
[epnum
];
180 fotg210
->reg
+ FOTG210_INEPMPSR(epnum
) :
181 fotg210
->reg
+ FOTG210_OUTEPMPSR(epnum
);
183 /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
184 * bit. Controller wouldn't clear this bit. WTF!!!
187 value
= ioread32(reg
);
188 value
|= INOUTEPMPSR_RESET_TSEQ
;
189 iowrite32(value
, reg
);
191 value
= ioread32(reg
);
192 value
&= ~INOUTEPMPSR_RESET_TSEQ
;
193 iowrite32(value
, reg
);
196 static int fotg210_ep_release(struct fotg210_ep
*ep
)
204 fotg210_reset_tseq(ep
->fotg210
, ep
->epnum
);
209 static int fotg210_ep_disable(struct usb_ep
*_ep
)
211 struct fotg210_ep
*ep
;
212 struct fotg210_request
*req
;
217 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
219 while (!list_empty(&ep
->queue
)) {
220 req
= list_entry(ep
->queue
.next
,
221 struct fotg210_request
, queue
);
222 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
223 fotg210_done(ep
, req
, -ECONNRESET
);
224 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
227 return fotg210_ep_release(ep
);
230 static struct usb_request
*fotg210_ep_alloc_request(struct usb_ep
*_ep
,
233 struct fotg210_request
*req
;
235 req
= kzalloc(sizeof(struct fotg210_request
), gfp_flags
);
239 INIT_LIST_HEAD(&req
->queue
);
244 static void fotg210_ep_free_request(struct usb_ep
*_ep
,
245 struct usb_request
*_req
)
247 struct fotg210_request
*req
;
249 req
= container_of(_req
, struct fotg210_request
, req
);
253 static void fotg210_enable_dma(struct fotg210_ep
*ep
,
254 dma_addr_t d
, u32 len
)
257 struct fotg210_udc
*fotg210
= ep
->fotg210
;
259 /* set transfer length and direction */
260 value
= ioread32(fotg210
->reg
+ FOTG210_DMACPSR1
);
261 value
&= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
262 value
|= DMACPSR1_DMA_LEN(len
) | DMACPSR1_DMA_TYPE(ep
->dir_in
);
263 iowrite32(value
, fotg210
->reg
+ FOTG210_DMACPSR1
);
265 /* set device DMA target FIFO number */
266 value
= ioread32(fotg210
->reg
+ FOTG210_DMATFNR
);
268 value
|= DMATFNR_ACC_FN(ep
->epnum
- 1);
270 value
|= DMATFNR_ACC_CXF
;
271 iowrite32(value
, fotg210
->reg
+ FOTG210_DMATFNR
);
273 /* set DMA memory address */
274 iowrite32(d
, fotg210
->reg
+ FOTG210_DMACPSR2
);
276 /* enable MDMA_EROR and MDMA_CMPLT interrupt */
277 value
= ioread32(fotg210
->reg
+ FOTG210_DMISGR2
);
278 value
&= ~(DMISGR2_MDMA_CMPLT
| DMISGR2_MDMA_ERROR
);
279 iowrite32(value
, fotg210
->reg
+ FOTG210_DMISGR2
);
282 value
= ioread32(fotg210
->reg
+ FOTG210_DMACPSR1
);
283 value
|= DMACPSR1_DMA_START
;
284 iowrite32(value
, fotg210
->reg
+ FOTG210_DMACPSR1
);
287 static void fotg210_disable_dma(struct fotg210_ep
*ep
)
289 iowrite32(DMATFNR_DISDMA
, ep
->fotg210
->reg
+ FOTG210_DMATFNR
);
292 static void fotg210_wait_dma_done(struct fotg210_ep
*ep
)
297 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DISGR2
);
298 if ((value
& DISGR2_USBRST_INT
) ||
299 (value
& DISGR2_DMA_ERROR
))
301 } while (!(value
& DISGR2_DMA_CMPLT
));
303 value
&= ~DISGR2_DMA_CMPLT
;
304 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DISGR2
);
308 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMACPSR1
);
309 value
|= DMACPSR1_DMA_ABORT
;
310 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMACPSR1
);
314 value
= ioread32(ep
->fotg210
->reg
+
315 FOTG210_FIBCR(ep
->epnum
- 1));
316 value
|= FIBCR_FFRST
;
317 iowrite32(value
, ep
->fotg210
->reg
+
318 FOTG210_FIBCR(ep
->epnum
- 1));
320 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DCFESR
);
321 value
|= DCFESR_CX_CLR
;
322 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DCFESR
);
326 static void fotg210_start_dma(struct fotg210_ep
*ep
,
327 struct fotg210_request
*req
)
329 struct device
*dev
= &ep
->fotg210
->gadget
.dev
;
336 buffer
= req
->req
.buf
;
337 length
= req
->req
.length
;
339 buffer
= req
->req
.buf
+ req
->req
.actual
;
340 length
= ioread32(ep
->fotg210
->reg
+
341 FOTG210_FIBCR(ep
->epnum
- 1));
342 length
&= FIBCR_BCFX
;
345 buffer
= req
->req
.buf
+ req
->req
.actual
;
346 if (req
->req
.length
- req
->req
.actual
> ep
->ep
.maxpacket
)
347 length
= ep
->ep
.maxpacket
;
349 length
= req
->req
.length
;
352 d
= dma_map_single(dev
, buffer
, length
,
353 ep
->dir_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
355 if (dma_mapping_error(dev
, d
)) {
356 pr_err("dma_mapping_error\n");
360 fotg210_enable_dma(ep
, d
, length
);
362 /* check if dma is done */
363 fotg210_wait_dma_done(ep
);
365 fotg210_disable_dma(ep
);
367 /* update actual transfer length */
368 req
->req
.actual
+= length
;
370 dma_unmap_single(dev
, d
, length
, DMA_TO_DEVICE
);
373 static void fotg210_ep0_queue(struct fotg210_ep
*ep
,
374 struct fotg210_request
*req
)
376 if (!req
->req
.length
) {
377 fotg210_done(ep
, req
, 0);
380 if (ep
->dir_in
) { /* if IN */
381 fotg210_start_dma(ep
, req
);
382 if ((req
->req
.length
== req
->req
.actual
) ||
383 (req
->req
.actual
< ep
->ep
.maxpacket
))
384 fotg210_done(ep
, req
, 0);
386 u32 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMISGR0
);
388 value
&= ~DMISGR0_MCX_OUT_INT
;
389 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMISGR0
);
393 static int fotg210_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
396 struct fotg210_ep
*ep
;
397 struct fotg210_request
*req
;
401 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
402 req
= container_of(_req
, struct fotg210_request
, req
);
404 if (ep
->fotg210
->gadget
.speed
== USB_SPEED_UNKNOWN
)
407 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
409 if (list_empty(&ep
->queue
))
412 list_add_tail(&req
->queue
, &ep
->queue
);
415 req
->req
.status
= -EINPROGRESS
;
417 if (!ep
->epnum
) /* ep0 */
418 fotg210_ep0_queue(ep
, req
);
419 else if (request
&& !ep
->stall
)
420 fotg210_enable_fifo_int(ep
);
422 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
427 static int fotg210_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
429 struct fotg210_ep
*ep
;
430 struct fotg210_request
*req
;
433 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
434 req
= container_of(_req
, struct fotg210_request
, req
);
436 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
437 if (!list_empty(&ep
->queue
))
438 fotg210_done(ep
, req
, -ECONNRESET
);
439 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
444 static void fotg210_set_epnstall(struct fotg210_ep
*ep
)
446 struct fotg210_udc
*fotg210
= ep
->fotg210
;
450 /* check if IN FIFO is empty before stall */
453 value
= ioread32(fotg210
->reg
+ FOTG210_DCFESR
);
454 } while (!(value
& DCFESR_FIFO_EMPTY(ep
->epnum
- 1)));
458 fotg210
->reg
+ FOTG210_INEPMPSR(ep
->epnum
) :
459 fotg210
->reg
+ FOTG210_OUTEPMPSR(ep
->epnum
);
460 value
= ioread32(reg
);
461 value
|= INOUTEPMPSR_STL_EP
;
462 iowrite32(value
, reg
);
465 static void fotg210_clear_epnstall(struct fotg210_ep
*ep
)
467 struct fotg210_udc
*fotg210
= ep
->fotg210
;
472 fotg210
->reg
+ FOTG210_INEPMPSR(ep
->epnum
) :
473 fotg210
->reg
+ FOTG210_OUTEPMPSR(ep
->epnum
);
474 value
= ioread32(reg
);
475 value
&= ~INOUTEPMPSR_STL_EP
;
476 iowrite32(value
, reg
);
479 static int fotg210_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedge
)
481 struct fotg210_ep
*ep
;
482 struct fotg210_udc
*fotg210
;
485 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
487 fotg210
= ep
->fotg210
;
489 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
492 fotg210_set_epnstall(ep
);
497 fotg210_reset_tseq(fotg210
, ep
->epnum
);
498 fotg210_clear_epnstall(ep
);
501 if (!list_empty(&ep
->queue
))
502 fotg210_enable_fifo_int(ep
);
505 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
509 static int fotg210_ep_set_halt(struct usb_ep
*_ep
, int value
)
511 return fotg210_set_halt_and_wedge(_ep
, value
, 0);
514 static int fotg210_ep_set_wedge(struct usb_ep
*_ep
)
516 return fotg210_set_halt_and_wedge(_ep
, 1, 1);
519 static void fotg210_ep_fifo_flush(struct usb_ep
*_ep
)
523 static const struct usb_ep_ops fotg210_ep_ops
= {
524 .enable
= fotg210_ep_enable
,
525 .disable
= fotg210_ep_disable
,
527 .alloc_request
= fotg210_ep_alloc_request
,
528 .free_request
= fotg210_ep_free_request
,
530 .queue
= fotg210_ep_queue
,
531 .dequeue
= fotg210_ep_dequeue
,
533 .set_halt
= fotg210_ep_set_halt
,
534 .fifo_flush
= fotg210_ep_fifo_flush
,
535 .set_wedge
= fotg210_ep_set_wedge
,
538 static void fotg210_clear_tx0byte(struct fotg210_udc
*fotg210
)
540 u32 value
= ioread32(fotg210
->reg
+ FOTG210_TX0BYTE
);
542 value
&= ~(TX0BYTE_EP1
| TX0BYTE_EP2
| TX0BYTE_EP3
544 iowrite32(value
, fotg210
->reg
+ FOTG210_TX0BYTE
);
547 static void fotg210_clear_rx0byte(struct fotg210_udc
*fotg210
)
549 u32 value
= ioread32(fotg210
->reg
+ FOTG210_RX0BYTE
);
551 value
&= ~(RX0BYTE_EP1
| RX0BYTE_EP2
| RX0BYTE_EP3
553 iowrite32(value
, fotg210
->reg
+ FOTG210_RX0BYTE
);
556 /* read 8-byte setup packet only */
557 static void fotg210_rdsetupp(struct fotg210_udc
*fotg210
,
565 iowrite32(DMATFNR_ACC_CXF
, fotg210
->reg
+ FOTG210_DMATFNR
);
567 for (i
= (length
>> 2); i
> 0; i
--) {
568 data
= ioread32(fotg210
->reg
+ FOTG210_CXPORT
);
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(fotg210
->reg
+ FOTG210_CXPORT
);
582 data
= ioread32(fotg210
->reg
+ FOTG210_CXPORT
);
584 *(tmp
+ 1) = (data
>> 8) & 0xFF;
587 data
= ioread32(fotg210
->reg
+ FOTG210_CXPORT
);
589 *(tmp
+ 1) = (data
>> 8) & 0xFF;
590 *(tmp
+ 2) = (data
>> 16) & 0xFF;
596 iowrite32(DMATFNR_DISDMA
, fotg210
->reg
+ FOTG210_DMATFNR
);
599 static void fotg210_set_configuration(struct fotg210_udc
*fotg210
)
601 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DAR
);
603 value
|= DAR_AFT_CONF
;
604 iowrite32(value
, fotg210
->reg
+ FOTG210_DAR
);
607 static void fotg210_set_dev_addr(struct fotg210_udc
*fotg210
, u32 addr
)
609 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DAR
);
611 value
|= (addr
& 0x7F);
612 iowrite32(value
, fotg210
->reg
+ FOTG210_DAR
);
615 static void fotg210_set_cxstall(struct fotg210_udc
*fotg210
)
617 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DCFESR
);
619 value
|= DCFESR_CX_STL
;
620 iowrite32(value
, fotg210
->reg
+ FOTG210_DCFESR
);
623 static void fotg210_request_error(struct fotg210_udc
*fotg210
)
625 fotg210_set_cxstall(fotg210
);
626 pr_err("request error!!\n");
629 static void fotg210_set_address(struct fotg210_udc
*fotg210
,
630 struct usb_ctrlrequest
*ctrl
)
632 if (ctrl
->wValue
>= 0x0100) {
633 fotg210_request_error(fotg210
);
635 fotg210_set_dev_addr(fotg210
, ctrl
->wValue
);
636 fotg210_set_cxdone(fotg210
);
640 static void fotg210_set_feature(struct fotg210_udc
*fotg210
,
641 struct usb_ctrlrequest
*ctrl
)
643 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
644 case USB_RECIP_DEVICE
:
645 fotg210_set_cxdone(fotg210
);
647 case USB_RECIP_INTERFACE
:
648 fotg210_set_cxdone(fotg210
);
650 case USB_RECIP_ENDPOINT
: {
652 epnum
= le16_to_cpu(ctrl
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
654 fotg210_set_epnstall(fotg210
->ep
[epnum
]);
656 fotg210_set_cxstall(fotg210
);
657 fotg210_set_cxdone(fotg210
);
661 fotg210_request_error(fotg210
);
666 static void fotg210_clear_feature(struct fotg210_udc
*fotg210
,
667 struct usb_ctrlrequest
*ctrl
)
669 struct fotg210_ep
*ep
=
670 fotg210
->ep
[ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
];
672 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
673 case USB_RECIP_DEVICE
:
674 fotg210_set_cxdone(fotg210
);
676 case USB_RECIP_INTERFACE
:
677 fotg210_set_cxdone(fotg210
);
679 case USB_RECIP_ENDPOINT
:
680 if (ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
) {
682 fotg210_set_cxdone(fotg210
);
686 fotg210_set_halt_and_wedge(&ep
->ep
, 0, 0);
688 fotg210_set_cxdone(fotg210
);
691 fotg210_request_error(fotg210
);
696 static int fotg210_is_epnstall(struct fotg210_ep
*ep
)
698 struct fotg210_udc
*fotg210
= ep
->fotg210
;
703 fotg210
->reg
+ FOTG210_INEPMPSR(ep
->epnum
) :
704 fotg210
->reg
+ FOTG210_OUTEPMPSR(ep
->epnum
);
705 value
= ioread32(reg
);
706 return value
& INOUTEPMPSR_STL_EP
? 1 : 0;
709 static void fotg210_get_status(struct fotg210_udc
*fotg210
,
710 struct usb_ctrlrequest
*ctrl
)
714 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
715 case USB_RECIP_DEVICE
:
716 fotg210
->ep0_data
= 1 << USB_DEVICE_SELF_POWERED
;
718 case USB_RECIP_INTERFACE
:
719 fotg210
->ep0_data
= 0;
721 case USB_RECIP_ENDPOINT
:
722 epnum
= ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
725 fotg210_is_epnstall(fotg210
->ep
[epnum
])
726 << USB_ENDPOINT_HALT
;
728 fotg210_request_error(fotg210
);
732 fotg210_request_error(fotg210
);
736 fotg210
->ep0_req
->buf
= &fotg210
->ep0_data
;
737 fotg210
->ep0_req
->length
= 2;
739 spin_unlock(&fotg210
->lock
);
740 fotg210_ep_queue(fotg210
->gadget
.ep0
, fotg210
->ep0_req
, GFP_ATOMIC
);
741 spin_lock(&fotg210
->lock
);
744 static int fotg210_setup_packet(struct fotg210_udc
*fotg210
,
745 struct usb_ctrlrequest
*ctrl
)
750 fotg210_rdsetupp(fotg210
, p
);
752 fotg210
->ep
[0]->dir_in
= ctrl
->bRequestType
& USB_DIR_IN
;
754 if (fotg210
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
755 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DMCR
);
756 fotg210
->gadget
.speed
= value
& DMCR_HS_EN
?
757 USB_SPEED_HIGH
: USB_SPEED_FULL
;
761 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
762 switch (ctrl
->bRequest
) {
763 case USB_REQ_GET_STATUS
:
764 fotg210_get_status(fotg210
, ctrl
);
766 case USB_REQ_CLEAR_FEATURE
:
767 fotg210_clear_feature(fotg210
, ctrl
);
769 case USB_REQ_SET_FEATURE
:
770 fotg210_set_feature(fotg210
, ctrl
);
772 case USB_REQ_SET_ADDRESS
:
773 fotg210_set_address(fotg210
, ctrl
);
775 case USB_REQ_SET_CONFIGURATION
:
776 fotg210_set_configuration(fotg210
);
790 static void fotg210_ep0out(struct fotg210_udc
*fotg210
)
792 struct fotg210_ep
*ep
= fotg210
->ep
[0];
794 if (!list_empty(&ep
->queue
) && !ep
->dir_in
) {
795 struct fotg210_request
*req
;
797 req
= list_first_entry(&ep
->queue
,
798 struct fotg210_request
, queue
);
801 fotg210_start_dma(ep
, req
);
803 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
)
804 fotg210_done(ep
, req
, 0);
806 pr_err("%s : empty queue\n", __func__
);
810 static void fotg210_ep0in(struct fotg210_udc
*fotg210
)
812 struct fotg210_ep
*ep
= fotg210
->ep
[0];
814 if ((!list_empty(&ep
->queue
)) && (ep
->dir_in
)) {
815 struct fotg210_request
*req
;
817 req
= list_entry(ep
->queue
.next
,
818 struct fotg210_request
, queue
);
821 fotg210_start_dma(ep
, req
);
823 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
)
824 fotg210_done(ep
, req
, 0);
826 fotg210_set_cxdone(fotg210
);
830 static void fotg210_clear_comabt_int(struct fotg210_udc
*fotg210
)
832 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DISGR0
);
834 value
&= ~DISGR0_CX_COMABT_INT
;
835 iowrite32(value
, fotg210
->reg
+ FOTG210_DISGR0
);
838 static void fotg210_in_fifo_handler(struct fotg210_ep
*ep
)
840 struct fotg210_request
*req
= list_entry(ep
->queue
.next
,
841 struct fotg210_request
, queue
);
844 fotg210_start_dma(ep
, req
);
845 fotg210_done(ep
, req
, 0);
848 static void fotg210_out_fifo_handler(struct fotg210_ep
*ep
)
850 struct fotg210_request
*req
= list_entry(ep
->queue
.next
,
851 struct fotg210_request
, queue
);
853 fotg210_start_dma(ep
, req
);
855 /* finish out transfer */
856 if (req
->req
.length
== req
->req
.actual
||
857 req
->req
.actual
< ep
->ep
.maxpacket
)
858 fotg210_done(ep
, req
, 0);
861 static irqreturn_t
fotg210_irq(int irq
, void *_fotg210
)
863 struct fotg210_udc
*fotg210
= _fotg210
;
864 u32 int_grp
= ioread32(fotg210
->reg
+ FOTG210_DIGR
);
865 u32 int_msk
= ioread32(fotg210
->reg
+ FOTG210_DMIGR
);
869 spin_lock(&fotg210
->lock
);
871 if (int_grp
& DIGR_INT_G2
) {
872 void __iomem
*reg
= fotg210
->reg
+ FOTG210_DISGR2
;
873 u32 int_grp2
= ioread32(reg
);
874 u32 int_msk2
= ioread32(fotg210
->reg
+ FOTG210_DMISGR2
);
877 int_grp2
&= ~int_msk2
;
879 if (int_grp2
& DISGR2_USBRST_INT
) {
880 value
= ioread32(reg
);
881 value
&= ~DISGR2_USBRST_INT
;
882 iowrite32(value
, reg
);
883 pr_info("fotg210 udc reset\n");
885 if (int_grp2
& DISGR2_SUSP_INT
) {
886 value
= ioread32(reg
);
887 value
&= ~DISGR2_SUSP_INT
;
888 iowrite32(value
, reg
);
889 pr_info("fotg210 udc suspend\n");
891 if (int_grp2
& DISGR2_RESM_INT
) {
892 value
= ioread32(reg
);
893 value
&= ~DISGR2_RESM_INT
;
894 iowrite32(value
, reg
);
895 pr_info("fotg210 udc resume\n");
897 if (int_grp2
& DISGR2_ISO_SEQ_ERR_INT
) {
898 value
= ioread32(reg
);
899 value
&= ~DISGR2_ISO_SEQ_ERR_INT
;
900 iowrite32(value
, reg
);
901 pr_info("fotg210 iso sequence error\n");
903 if (int_grp2
& DISGR2_ISO_SEQ_ABORT_INT
) {
904 value
= ioread32(reg
);
905 value
&= ~DISGR2_ISO_SEQ_ABORT_INT
;
906 iowrite32(value
, reg
);
907 pr_info("fotg210 iso sequence abort\n");
909 if (int_grp2
& DISGR2_TX0BYTE_INT
) {
910 fotg210_clear_tx0byte(fotg210
);
911 value
= ioread32(reg
);
912 value
&= ~DISGR2_TX0BYTE_INT
;
913 iowrite32(value
, reg
);
914 pr_info("fotg210 transferred 0 byte\n");
916 if (int_grp2
& DISGR2_RX0BYTE_INT
) {
917 fotg210_clear_rx0byte(fotg210
);
918 value
= ioread32(reg
);
919 value
&= ~DISGR2_RX0BYTE_INT
;
920 iowrite32(value
, reg
);
921 pr_info("fotg210 received 0 byte\n");
923 if (int_grp2
& DISGR2_DMA_ERROR
) {
924 value
= ioread32(reg
);
925 value
&= ~DISGR2_DMA_ERROR
;
926 iowrite32(value
, reg
);
930 if (int_grp
& DIGR_INT_G0
) {
931 void __iomem
*reg
= fotg210
->reg
+ FOTG210_DISGR0
;
932 u32 int_grp0
= ioread32(reg
);
933 u32 int_msk0
= ioread32(fotg210
->reg
+ FOTG210_DMISGR0
);
934 struct usb_ctrlrequest ctrl
;
936 int_grp0
&= ~int_msk0
;
938 /* the highest priority in this source register */
939 if (int_grp0
& DISGR0_CX_COMABT_INT
) {
940 fotg210_clear_comabt_int(fotg210
);
941 pr_info("fotg210 CX command abort\n");
944 if (int_grp0
& DISGR0_CX_SETUP_INT
) {
945 if (fotg210_setup_packet(fotg210
, &ctrl
)) {
946 spin_unlock(&fotg210
->lock
);
947 if (fotg210
->driver
->setup(&fotg210
->gadget
,
949 fotg210_set_cxstall(fotg210
);
950 spin_lock(&fotg210
->lock
);
953 if (int_grp0
& DISGR0_CX_COMEND_INT
)
954 pr_info("fotg210 cmd end\n");
956 if (int_grp0
& DISGR0_CX_IN_INT
)
957 fotg210_ep0in(fotg210
);
959 if (int_grp0
& DISGR0_CX_OUT_INT
)
960 fotg210_ep0out(fotg210
);
962 if (int_grp0
& DISGR0_CX_COMFAIL_INT
) {
963 fotg210_set_cxstall(fotg210
);
964 pr_info("fotg210 ep0 fail\n");
968 if (int_grp
& DIGR_INT_G1
) {
969 void __iomem
*reg
= fotg210
->reg
+ FOTG210_DISGR1
;
970 u32 int_grp1
= ioread32(reg
);
971 u32 int_msk1
= ioread32(fotg210
->reg
+ FOTG210_DMISGR1
);
974 int_grp1
&= ~int_msk1
;
976 for (fifo
= 0; fifo
< FOTG210_MAX_FIFO_NUM
; fifo
++) {
977 if (int_grp1
& DISGR1_IN_INT(fifo
))
978 fotg210_in_fifo_handler(fotg210
->ep
[fifo
+ 1]);
980 if ((int_grp1
& DISGR1_OUT_INT(fifo
)) ||
981 (int_grp1
& DISGR1_SPK_INT(fifo
)))
982 fotg210_out_fifo_handler(fotg210
->ep
[fifo
+ 1]);
986 spin_unlock(&fotg210
->lock
);
991 static void fotg210_disable_unplug(struct fotg210_udc
*fotg210
)
993 u32 reg
= ioread32(fotg210
->reg
+ FOTG210_PHYTMSR
);
995 reg
&= ~PHYTMSR_UNPLUG
;
996 iowrite32(reg
, fotg210
->reg
+ FOTG210_PHYTMSR
);
999 static int fotg210_udc_start(struct usb_gadget
*g
,
1000 struct usb_gadget_driver
*driver
)
1002 struct fotg210_udc
*fotg210
= gadget_to_fotg210(g
);
1005 /* hook up the driver */
1006 driver
->driver
.bus
= NULL
;
1007 fotg210
->driver
= driver
;
1009 /* enable device global interrupt */
1010 value
= ioread32(fotg210
->reg
+ FOTG210_DMCR
);
1011 value
|= DMCR_GLINT_EN
;
1012 iowrite32(value
, fotg210
->reg
+ FOTG210_DMCR
);
1017 static void fotg210_init(struct fotg210_udc
*fotg210
)
1021 /* disable global interrupt and set int polarity to active high */
1022 iowrite32(GMIR_MHC_INT
| GMIR_MOTG_INT
| GMIR_INT_POLARITY
,
1023 fotg210
->reg
+ FOTG210_GMIR
);
1025 /* disable device global interrupt */
1026 value
= ioread32(fotg210
->reg
+ FOTG210_DMCR
);
1027 value
&= ~DMCR_GLINT_EN
;
1028 iowrite32(value
, fotg210
->reg
+ FOTG210_DMCR
);
1030 /* disable all fifo interrupt */
1031 iowrite32(~(u32
)0, fotg210
->reg
+ FOTG210_DMISGR1
);
1033 /* disable cmd end */
1034 value
= ioread32(fotg210
->reg
+ FOTG210_DMISGR0
);
1035 value
|= DMISGR0_MCX_COMEND
;
1036 iowrite32(value
, fotg210
->reg
+ FOTG210_DMISGR0
);
1039 static int fotg210_udc_stop(struct usb_gadget
*g
)
1041 struct fotg210_udc
*fotg210
= gadget_to_fotg210(g
);
1042 unsigned long flags
;
1044 spin_lock_irqsave(&fotg210
->lock
, flags
);
1046 fotg210_init(fotg210
);
1047 fotg210
->driver
= NULL
;
1049 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1054 static const struct usb_gadget_ops fotg210_gadget_ops
= {
1055 .udc_start
= fotg210_udc_start
,
1056 .udc_stop
= fotg210_udc_stop
,
1059 static int fotg210_udc_remove(struct platform_device
*pdev
)
1061 struct fotg210_udc
*fotg210
= platform_get_drvdata(pdev
);
1064 usb_del_gadget_udc(&fotg210
->gadget
);
1065 iounmap(fotg210
->reg
);
1066 free_irq(platform_get_irq(pdev
, 0), fotg210
);
1068 fotg210_ep_free_request(&fotg210
->ep
[0]->ep
, fotg210
->ep0_req
);
1069 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++)
1070 kfree(fotg210
->ep
[i
]);
1076 static int fotg210_udc_probe(struct platform_device
*pdev
)
1078 struct resource
*res
, *ires
;
1079 struct fotg210_udc
*fotg210
= NULL
;
1080 struct fotg210_ep
*_ep
[FOTG210_MAX_NUM_EP
];
1084 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1086 pr_err("platform_get_resource error.\n");
1090 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1092 pr_err("platform_get_resource IORESOURCE_IRQ error.\n");
1098 /* initialize udc */
1099 fotg210
= kzalloc(sizeof(struct fotg210_udc
), GFP_KERNEL
);
1100 if (fotg210
== NULL
)
1103 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++) {
1104 _ep
[i
] = kzalloc(sizeof(struct fotg210_ep
), GFP_KERNEL
);
1107 fotg210
->ep
[i
] = _ep
[i
];
1110 fotg210
->reg
= ioremap(res
->start
, resource_size(res
));
1111 if (fotg210
->reg
== NULL
) {
1112 pr_err("ioremap error.\n");
1116 spin_lock_init(&fotg210
->lock
);
1118 platform_set_drvdata(pdev
, fotg210
);
1120 fotg210
->gadget
.ops
= &fotg210_gadget_ops
;
1122 fotg210
->gadget
.max_speed
= USB_SPEED_HIGH
;
1123 fotg210
->gadget
.dev
.parent
= &pdev
->dev
;
1124 fotg210
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1125 fotg210
->gadget
.name
= udc_name
;
1127 INIT_LIST_HEAD(&fotg210
->gadget
.ep_list
);
1129 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++) {
1130 struct fotg210_ep
*ep
= fotg210
->ep
[i
];
1133 INIT_LIST_HEAD(&fotg210
->ep
[i
]->ep
.ep_list
);
1134 list_add_tail(&fotg210
->ep
[i
]->ep
.ep_list
,
1135 &fotg210
->gadget
.ep_list
);
1137 ep
->fotg210
= fotg210
;
1138 INIT_LIST_HEAD(&ep
->queue
);
1139 ep
->ep
.name
= fotg210_ep_name
[i
];
1140 ep
->ep
.ops
= &fotg210_ep_ops
;
1141 usb_ep_set_maxpacket_limit(&ep
->ep
, (unsigned short) ~0);
1144 ep
->ep
.caps
.type_control
= true;
1146 ep
->ep
.caps
.type_iso
= true;
1147 ep
->ep
.caps
.type_bulk
= true;
1148 ep
->ep
.caps
.type_int
= true;
1151 ep
->ep
.caps
.dir_in
= true;
1152 ep
->ep
.caps
.dir_out
= true;
1154 usb_ep_set_maxpacket_limit(&fotg210
->ep
[0]->ep
, 0x40);
1155 fotg210
->gadget
.ep0
= &fotg210
->ep
[0]->ep
;
1156 INIT_LIST_HEAD(&fotg210
->gadget
.ep0
->ep_list
);
1158 fotg210
->ep0_req
= fotg210_ep_alloc_request(&fotg210
->ep
[0]->ep
,
1160 if (fotg210
->ep0_req
== NULL
)
1163 fotg210_init(fotg210
);
1165 fotg210_disable_unplug(fotg210
);
1167 ret
= request_irq(ires
->start
, fotg210_irq
, IRQF_SHARED
,
1170 pr_err("request_irq error (%d)\n", ret
);
1174 ret
= usb_add_gadget_udc(&pdev
->dev
, &fotg210
->gadget
);
1178 dev_info(&pdev
->dev
, "version %s\n", DRIVER_VERSION
);
1183 free_irq(ires
->start
, fotg210
);
1186 fotg210_ep_free_request(&fotg210
->ep
[0]->ep
, fotg210
->ep0_req
);
1189 iounmap(fotg210
->reg
);
1192 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++)
1193 kfree(fotg210
->ep
[i
]);
1200 static struct platform_driver fotg210_driver
= {
1204 .probe
= fotg210_udc_probe
,
1205 .remove
= fotg210_udc_remove
,
1208 module_platform_driver(fotg210_driver
);
1210 MODULE_AUTHOR("Yuan-Hsin Chen, Feng-Hsin Chiang <john453@faraday-tech.com>");
1211 MODULE_LICENSE("GPL");
1212 MODULE_DESCRIPTION(DRIVER_DESC
);