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/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/err.h>
13 #include <linux/interrupt.h>
15 #include <linux/module.h>
16 #include <linux/platform_device.h>
17 #include <linux/usb/ch9.h>
18 #include <linux/usb/gadget.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_ack_int(struct fotg210_udc
*fotg210
, u32 offset
, u32 mask
)
34 u32 value
= ioread32(fotg210
->reg
+ offset
);
37 iowrite32(value
, fotg210
->reg
+ offset
);
40 static void fotg210_disable_fifo_int(struct fotg210_ep
*ep
)
42 u32 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
45 value
|= DMISGR1_MF_IN_INT(ep
->epnum
- 1);
47 value
|= DMISGR1_MF_OUTSPK_INT(ep
->epnum
- 1);
48 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
51 static void fotg210_enable_fifo_int(struct fotg210_ep
*ep
)
53 u32 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
56 value
&= ~DMISGR1_MF_IN_INT(ep
->epnum
- 1);
58 value
&= ~DMISGR1_MF_OUTSPK_INT(ep
->epnum
- 1);
59 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMISGR1
);
62 static void fotg210_set_cxdone(struct fotg210_udc
*fotg210
)
64 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DCFESR
);
66 value
|= DCFESR_CX_DONE
;
67 iowrite32(value
, fotg210
->reg
+ FOTG210_DCFESR
);
70 static void fotg210_done(struct fotg210_ep
*ep
, struct fotg210_request
*req
,
73 list_del_init(&req
->queue
);
75 /* don't modify queue heads during completion callback */
76 if (ep
->fotg210
->gadget
.speed
== USB_SPEED_UNKNOWN
)
77 req
->req
.status
= -ESHUTDOWN
;
79 req
->req
.status
= status
;
81 spin_unlock(&ep
->fotg210
->lock
);
82 usb_gadget_giveback_request(&ep
->ep
, &req
->req
);
83 spin_lock(&ep
->fotg210
->lock
);
86 if (list_empty(&ep
->queue
))
87 fotg210_disable_fifo_int(ep
);
89 fotg210_set_cxdone(ep
->fotg210
);
93 static void fotg210_fifo_ep_mapping(struct fotg210_ep
*ep
, u32 epnum
,
96 struct fotg210_udc
*fotg210
= ep
->fotg210
;
99 /* Driver should map an ep to a fifo and then map the fifo
100 * to the ep. What a brain-damaged design!
103 /* map a fifo to an ep */
104 val
= ioread32(fotg210
->reg
+ FOTG210_EPMAP
);
105 val
&= ~EPMAP_FIFONOMSK(epnum
, dir_in
);
106 val
|= EPMAP_FIFONO(epnum
, dir_in
);
107 iowrite32(val
, fotg210
->reg
+ FOTG210_EPMAP
);
109 /* map the ep to the fifo */
110 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOMAP
);
111 val
&= ~FIFOMAP_EPNOMSK(epnum
);
112 val
|= FIFOMAP_EPNO(epnum
);
113 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOMAP
);
116 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOCF
);
117 val
|= FIFOCF_FIFO_EN(epnum
- 1);
118 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOCF
);
121 static void fotg210_set_fifo_dir(struct fotg210_ep
*ep
, u32 epnum
, u32 dir_in
)
123 struct fotg210_udc
*fotg210
= ep
->fotg210
;
126 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOMAP
);
127 val
|= (dir_in
? FIFOMAP_DIRIN(epnum
- 1) : FIFOMAP_DIROUT(epnum
- 1));
128 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOMAP
);
131 static void fotg210_set_tfrtype(struct fotg210_ep
*ep
, u32 epnum
, u32 type
)
133 struct fotg210_udc
*fotg210
= ep
->fotg210
;
136 val
= ioread32(fotg210
->reg
+ FOTG210_FIFOCF
);
137 val
|= FIFOCF_TYPE(type
, epnum
- 1);
138 iowrite32(val
, fotg210
->reg
+ FOTG210_FIFOCF
);
141 static void fotg210_set_mps(struct fotg210_ep
*ep
, u32 epnum
, u32 mps
,
144 struct fotg210_udc
*fotg210
= ep
->fotg210
;
146 u32 offset
= dir_in
? FOTG210_INEPMPSR(epnum
) :
147 FOTG210_OUTEPMPSR(epnum
);
149 val
= ioread32(fotg210
->reg
+ offset
);
150 val
|= INOUTEPMPSR_MPS(mps
);
151 iowrite32(val
, fotg210
->reg
+ offset
);
154 static int fotg210_config_ep(struct fotg210_ep
*ep
,
155 const struct usb_endpoint_descriptor
*desc
)
157 struct fotg210_udc
*fotg210
= ep
->fotg210
;
159 fotg210_set_fifo_dir(ep
, ep
->epnum
, ep
->dir_in
);
160 fotg210_set_tfrtype(ep
, ep
->epnum
, ep
->type
);
161 fotg210_set_mps(ep
, ep
->epnum
, ep
->ep
.maxpacket
, ep
->dir_in
);
162 fotg210_fifo_ep_mapping(ep
, ep
->epnum
, ep
->dir_in
);
164 fotg210
->ep
[ep
->epnum
] = ep
;
169 static int fotg210_ep_enable(struct usb_ep
*_ep
,
170 const struct usb_endpoint_descriptor
*desc
)
172 struct fotg210_ep
*ep
;
174 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
177 ep
->epnum
= usb_endpoint_num(desc
);
178 ep
->type
= usb_endpoint_type(desc
);
179 ep
->dir_in
= usb_endpoint_dir_in(desc
);
180 ep
->ep
.maxpacket
= usb_endpoint_maxp(desc
);
182 return fotg210_config_ep(ep
, desc
);
185 static void fotg210_reset_tseq(struct fotg210_udc
*fotg210
, u8 epnum
)
187 struct fotg210_ep
*ep
= fotg210
->ep
[epnum
];
192 fotg210
->reg
+ FOTG210_INEPMPSR(epnum
) :
193 fotg210
->reg
+ FOTG210_OUTEPMPSR(epnum
);
195 /* Note: Driver needs to set and clear INOUTEPMPSR_RESET_TSEQ
196 * bit. Controller wouldn't clear this bit. WTF!!!
199 value
= ioread32(reg
);
200 value
|= INOUTEPMPSR_RESET_TSEQ
;
201 iowrite32(value
, reg
);
203 value
= ioread32(reg
);
204 value
&= ~INOUTEPMPSR_RESET_TSEQ
;
205 iowrite32(value
, reg
);
208 static int fotg210_ep_release(struct fotg210_ep
*ep
)
216 fotg210_reset_tseq(ep
->fotg210
, ep
->epnum
);
221 static int fotg210_ep_disable(struct usb_ep
*_ep
)
223 struct fotg210_ep
*ep
;
224 struct fotg210_request
*req
;
229 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
231 while (!list_empty(&ep
->queue
)) {
232 req
= list_entry(ep
->queue
.next
,
233 struct fotg210_request
, queue
);
234 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
235 fotg210_done(ep
, req
, -ECONNRESET
);
236 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
239 return fotg210_ep_release(ep
);
242 static struct usb_request
*fotg210_ep_alloc_request(struct usb_ep
*_ep
,
245 struct fotg210_request
*req
;
247 req
= kzalloc(sizeof(struct fotg210_request
), gfp_flags
);
251 INIT_LIST_HEAD(&req
->queue
);
256 static void fotg210_ep_free_request(struct usb_ep
*_ep
,
257 struct usb_request
*_req
)
259 struct fotg210_request
*req
;
261 req
= container_of(_req
, struct fotg210_request
, req
);
265 static void fotg210_enable_dma(struct fotg210_ep
*ep
,
266 dma_addr_t d
, u32 len
)
269 struct fotg210_udc
*fotg210
= ep
->fotg210
;
271 /* set transfer length and direction */
272 value
= ioread32(fotg210
->reg
+ FOTG210_DMACPSR1
);
273 value
&= ~(DMACPSR1_DMA_LEN(0xFFFF) | DMACPSR1_DMA_TYPE(1));
274 value
|= DMACPSR1_DMA_LEN(len
) | DMACPSR1_DMA_TYPE(ep
->dir_in
);
275 iowrite32(value
, fotg210
->reg
+ FOTG210_DMACPSR1
);
277 /* set device DMA target FIFO number */
278 value
= ioread32(fotg210
->reg
+ FOTG210_DMATFNR
);
280 value
|= DMATFNR_ACC_FN(ep
->epnum
- 1);
282 value
|= DMATFNR_ACC_CXF
;
283 iowrite32(value
, fotg210
->reg
+ FOTG210_DMATFNR
);
285 /* set DMA memory address */
286 iowrite32(d
, fotg210
->reg
+ FOTG210_DMACPSR2
);
288 /* enable MDMA_EROR and MDMA_CMPLT interrupt */
289 value
= ioread32(fotg210
->reg
+ FOTG210_DMISGR2
);
290 value
&= ~(DMISGR2_MDMA_CMPLT
| DMISGR2_MDMA_ERROR
);
291 iowrite32(value
, fotg210
->reg
+ FOTG210_DMISGR2
);
294 value
= ioread32(fotg210
->reg
+ FOTG210_DMACPSR1
);
295 value
|= DMACPSR1_DMA_START
;
296 iowrite32(value
, fotg210
->reg
+ FOTG210_DMACPSR1
);
299 static void fotg210_disable_dma(struct fotg210_ep
*ep
)
301 iowrite32(DMATFNR_DISDMA
, ep
->fotg210
->reg
+ FOTG210_DMATFNR
);
304 static void fotg210_wait_dma_done(struct fotg210_ep
*ep
)
309 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DISGR2
);
310 if ((value
& DISGR2_USBRST_INT
) ||
311 (value
& DISGR2_DMA_ERROR
))
313 } while (!(value
& DISGR2_DMA_CMPLT
));
315 fotg210_ack_int(ep
->fotg210
, FOTG210_DISGR2
, DISGR2_DMA_CMPLT
);
319 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMACPSR1
);
320 value
|= DMACPSR1_DMA_ABORT
;
321 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMACPSR1
);
325 value
= ioread32(ep
->fotg210
->reg
+
326 FOTG210_FIBCR(ep
->epnum
- 1));
327 value
|= FIBCR_FFRST
;
328 iowrite32(value
, ep
->fotg210
->reg
+
329 FOTG210_FIBCR(ep
->epnum
- 1));
331 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DCFESR
);
332 value
|= DCFESR_CX_CLR
;
333 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DCFESR
);
337 static void fotg210_start_dma(struct fotg210_ep
*ep
,
338 struct fotg210_request
*req
)
340 struct device
*dev
= &ep
->fotg210
->gadget
.dev
;
347 buffer
= req
->req
.buf
;
348 length
= req
->req
.length
;
350 buffer
= req
->req
.buf
+ req
->req
.actual
;
351 length
= ioread32(ep
->fotg210
->reg
+
352 FOTG210_FIBCR(ep
->epnum
- 1)) & FIBCR_BCFX
;
353 if (length
> req
->req
.length
- req
->req
.actual
)
354 length
= req
->req
.length
- req
->req
.actual
;
357 buffer
= req
->req
.buf
+ req
->req
.actual
;
358 if (req
->req
.length
- req
->req
.actual
> ep
->ep
.maxpacket
)
359 length
= ep
->ep
.maxpacket
;
361 length
= req
->req
.length
- req
->req
.actual
;
364 d
= dma_map_single(dev
, buffer
, length
,
365 ep
->dir_in
? DMA_TO_DEVICE
: DMA_FROM_DEVICE
);
367 if (dma_mapping_error(dev
, d
)) {
368 pr_err("dma_mapping_error\n");
372 fotg210_enable_dma(ep
, d
, length
);
374 /* check if dma is done */
375 fotg210_wait_dma_done(ep
);
377 fotg210_disable_dma(ep
);
379 /* update actual transfer length */
380 req
->req
.actual
+= length
;
382 dma_unmap_single(dev
, d
, length
, DMA_TO_DEVICE
);
385 static void fotg210_ep0_queue(struct fotg210_ep
*ep
,
386 struct fotg210_request
*req
)
388 if (!req
->req
.length
) {
389 fotg210_done(ep
, req
, 0);
392 if (ep
->dir_in
) { /* if IN */
393 fotg210_start_dma(ep
, req
);
394 if (req
->req
.length
== req
->req
.actual
)
395 fotg210_done(ep
, req
, 0);
397 u32 value
= ioread32(ep
->fotg210
->reg
+ FOTG210_DMISGR0
);
399 value
&= ~DMISGR0_MCX_OUT_INT
;
400 iowrite32(value
, ep
->fotg210
->reg
+ FOTG210_DMISGR0
);
404 static int fotg210_ep_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
407 struct fotg210_ep
*ep
;
408 struct fotg210_request
*req
;
412 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
413 req
= container_of(_req
, struct fotg210_request
, req
);
415 if (ep
->fotg210
->gadget
.speed
== USB_SPEED_UNKNOWN
)
418 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
420 if (list_empty(&ep
->queue
))
423 list_add_tail(&req
->queue
, &ep
->queue
);
426 req
->req
.status
= -EINPROGRESS
;
428 if (!ep
->epnum
) /* ep0 */
429 fotg210_ep0_queue(ep
, req
);
430 else if (request
&& !ep
->stall
)
431 fotg210_enable_fifo_int(ep
);
433 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
438 static int fotg210_ep_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
440 struct fotg210_ep
*ep
;
441 struct fotg210_request
*req
;
444 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
445 req
= container_of(_req
, struct fotg210_request
, req
);
447 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
448 if (!list_empty(&ep
->queue
))
449 fotg210_done(ep
, req
, -ECONNRESET
);
450 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
455 static void fotg210_set_epnstall(struct fotg210_ep
*ep
)
457 struct fotg210_udc
*fotg210
= ep
->fotg210
;
461 /* check if IN FIFO is empty before stall */
464 value
= ioread32(fotg210
->reg
+ FOTG210_DCFESR
);
465 } while (!(value
& DCFESR_FIFO_EMPTY(ep
->epnum
- 1)));
469 fotg210
->reg
+ FOTG210_INEPMPSR(ep
->epnum
) :
470 fotg210
->reg
+ FOTG210_OUTEPMPSR(ep
->epnum
);
471 value
= ioread32(reg
);
472 value
|= INOUTEPMPSR_STL_EP
;
473 iowrite32(value
, reg
);
476 static void fotg210_clear_epnstall(struct fotg210_ep
*ep
)
478 struct fotg210_udc
*fotg210
= ep
->fotg210
;
483 fotg210
->reg
+ FOTG210_INEPMPSR(ep
->epnum
) :
484 fotg210
->reg
+ FOTG210_OUTEPMPSR(ep
->epnum
);
485 value
= ioread32(reg
);
486 value
&= ~INOUTEPMPSR_STL_EP
;
487 iowrite32(value
, reg
);
490 static int fotg210_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedge
)
492 struct fotg210_ep
*ep
;
493 struct fotg210_udc
*fotg210
;
496 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
498 fotg210
= ep
->fotg210
;
500 spin_lock_irqsave(&ep
->fotg210
->lock
, flags
);
503 fotg210_set_epnstall(ep
);
508 fotg210_reset_tseq(fotg210
, ep
->epnum
);
509 fotg210_clear_epnstall(ep
);
512 if (!list_empty(&ep
->queue
))
513 fotg210_enable_fifo_int(ep
);
516 spin_unlock_irqrestore(&ep
->fotg210
->lock
, flags
);
520 static int fotg210_ep_set_halt(struct usb_ep
*_ep
, int value
)
522 return fotg210_set_halt_and_wedge(_ep
, value
, 0);
525 static int fotg210_ep_set_wedge(struct usb_ep
*_ep
)
527 return fotg210_set_halt_and_wedge(_ep
, 1, 1);
530 static void fotg210_ep_fifo_flush(struct usb_ep
*_ep
)
534 static const struct usb_ep_ops fotg210_ep_ops
= {
535 .enable
= fotg210_ep_enable
,
536 .disable
= fotg210_ep_disable
,
538 .alloc_request
= fotg210_ep_alloc_request
,
539 .free_request
= fotg210_ep_free_request
,
541 .queue
= fotg210_ep_queue
,
542 .dequeue
= fotg210_ep_dequeue
,
544 .set_halt
= fotg210_ep_set_halt
,
545 .fifo_flush
= fotg210_ep_fifo_flush
,
546 .set_wedge
= fotg210_ep_set_wedge
,
549 static void fotg210_clear_tx0byte(struct fotg210_udc
*fotg210
)
551 u32 value
= ioread32(fotg210
->reg
+ FOTG210_TX0BYTE
);
553 value
&= ~(TX0BYTE_EP1
| TX0BYTE_EP2
| TX0BYTE_EP3
555 iowrite32(value
, fotg210
->reg
+ FOTG210_TX0BYTE
);
558 static void fotg210_clear_rx0byte(struct fotg210_udc
*fotg210
)
560 u32 value
= ioread32(fotg210
->reg
+ FOTG210_RX0BYTE
);
562 value
&= ~(RX0BYTE_EP1
| RX0BYTE_EP2
| RX0BYTE_EP3
564 iowrite32(value
, fotg210
->reg
+ FOTG210_RX0BYTE
);
567 /* read 8-byte setup packet only */
568 static void fotg210_rdsetupp(struct fotg210_udc
*fotg210
,
576 iowrite32(DMATFNR_ACC_CXF
, fotg210
->reg
+ FOTG210_DMATFNR
);
578 for (i
= (length
>> 2); i
> 0; i
--) {
579 data
= ioread32(fotg210
->reg
+ FOTG210_CXPORT
);
581 *(tmp
+ 1) = (data
>> 8) & 0xFF;
582 *(tmp
+ 2) = (data
>> 16) & 0xFF;
583 *(tmp
+ 3) = (data
>> 24) & 0xFF;
587 switch (length
% 4) {
589 data
= ioread32(fotg210
->reg
+ FOTG210_CXPORT
);
593 data
= ioread32(fotg210
->reg
+ FOTG210_CXPORT
);
595 *(tmp
+ 1) = (data
>> 8) & 0xFF;
598 data
= ioread32(fotg210
->reg
+ FOTG210_CXPORT
);
600 *(tmp
+ 1) = (data
>> 8) & 0xFF;
601 *(tmp
+ 2) = (data
>> 16) & 0xFF;
607 iowrite32(DMATFNR_DISDMA
, fotg210
->reg
+ FOTG210_DMATFNR
);
610 static void fotg210_set_configuration(struct fotg210_udc
*fotg210
)
612 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DAR
);
614 value
|= DAR_AFT_CONF
;
615 iowrite32(value
, fotg210
->reg
+ FOTG210_DAR
);
618 static void fotg210_set_dev_addr(struct fotg210_udc
*fotg210
, u32 addr
)
620 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DAR
);
622 value
|= (addr
& 0x7F);
623 iowrite32(value
, fotg210
->reg
+ FOTG210_DAR
);
626 static void fotg210_set_cxstall(struct fotg210_udc
*fotg210
)
628 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DCFESR
);
630 value
|= DCFESR_CX_STL
;
631 iowrite32(value
, fotg210
->reg
+ FOTG210_DCFESR
);
634 static void fotg210_request_error(struct fotg210_udc
*fotg210
)
636 fotg210_set_cxstall(fotg210
);
637 pr_err("request error!!\n");
640 static void fotg210_set_address(struct fotg210_udc
*fotg210
,
641 struct usb_ctrlrequest
*ctrl
)
643 if (le16_to_cpu(ctrl
->wValue
) >= 0x0100) {
644 fotg210_request_error(fotg210
);
646 fotg210_set_dev_addr(fotg210
, le16_to_cpu(ctrl
->wValue
));
647 fotg210_set_cxdone(fotg210
);
651 static void fotg210_set_feature(struct fotg210_udc
*fotg210
,
652 struct usb_ctrlrequest
*ctrl
)
654 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
655 case USB_RECIP_DEVICE
:
656 fotg210_set_cxdone(fotg210
);
658 case USB_RECIP_INTERFACE
:
659 fotg210_set_cxdone(fotg210
);
661 case USB_RECIP_ENDPOINT
: {
663 epnum
= le16_to_cpu(ctrl
->wIndex
) & USB_ENDPOINT_NUMBER_MASK
;
665 fotg210_set_epnstall(fotg210
->ep
[epnum
]);
667 fotg210_set_cxstall(fotg210
);
668 fotg210_set_cxdone(fotg210
);
672 fotg210_request_error(fotg210
);
677 static void fotg210_clear_feature(struct fotg210_udc
*fotg210
,
678 struct usb_ctrlrequest
*ctrl
)
680 struct fotg210_ep
*ep
=
681 fotg210
->ep
[ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
];
683 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
684 case USB_RECIP_DEVICE
:
685 fotg210_set_cxdone(fotg210
);
687 case USB_RECIP_INTERFACE
:
688 fotg210_set_cxdone(fotg210
);
690 case USB_RECIP_ENDPOINT
:
691 if (ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
) {
693 fotg210_set_cxdone(fotg210
);
697 fotg210_set_halt_and_wedge(&ep
->ep
, 0, 0);
699 fotg210_set_cxdone(fotg210
);
702 fotg210_request_error(fotg210
);
707 static int fotg210_is_epnstall(struct fotg210_ep
*ep
)
709 struct fotg210_udc
*fotg210
= ep
->fotg210
;
714 fotg210
->reg
+ FOTG210_INEPMPSR(ep
->epnum
) :
715 fotg210
->reg
+ FOTG210_OUTEPMPSR(ep
->epnum
);
716 value
= ioread32(reg
);
717 return value
& INOUTEPMPSR_STL_EP
? 1 : 0;
720 /* For EP0 requests triggered by this driver (currently GET_STATUS response) */
721 static void fotg210_ep0_complete(struct usb_ep
*_ep
, struct usb_request
*req
)
723 struct fotg210_ep
*ep
;
724 struct fotg210_udc
*fotg210
;
726 ep
= container_of(_ep
, struct fotg210_ep
, ep
);
727 fotg210
= ep
->fotg210
;
729 if (req
->status
|| req
->actual
!= req
->length
) {
730 dev_warn(&fotg210
->gadget
.dev
, "EP0 request failed: %d\n", req
->status
);
734 static void fotg210_get_status(struct fotg210_udc
*fotg210
,
735 struct usb_ctrlrequest
*ctrl
)
739 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
740 case USB_RECIP_DEVICE
:
741 fotg210
->ep0_data
= cpu_to_le16(1 << USB_DEVICE_SELF_POWERED
);
743 case USB_RECIP_INTERFACE
:
744 fotg210
->ep0_data
= cpu_to_le16(0);
746 case USB_RECIP_ENDPOINT
:
747 epnum
= ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
;
750 cpu_to_le16(fotg210_is_epnstall(fotg210
->ep
[epnum
])
751 << USB_ENDPOINT_HALT
);
753 fotg210_request_error(fotg210
);
757 fotg210_request_error(fotg210
);
761 fotg210
->ep0_req
->buf
= &fotg210
->ep0_data
;
762 fotg210
->ep0_req
->length
= 2;
764 spin_unlock(&fotg210
->lock
);
765 fotg210_ep_queue(fotg210
->gadget
.ep0
, fotg210
->ep0_req
, GFP_ATOMIC
);
766 spin_lock(&fotg210
->lock
);
769 static int fotg210_setup_packet(struct fotg210_udc
*fotg210
,
770 struct usb_ctrlrequest
*ctrl
)
775 fotg210_rdsetupp(fotg210
, p
);
777 fotg210
->ep
[0]->dir_in
= ctrl
->bRequestType
& USB_DIR_IN
;
779 if (fotg210
->gadget
.speed
== USB_SPEED_UNKNOWN
) {
780 u32 value
= ioread32(fotg210
->reg
+ FOTG210_DMCR
);
781 fotg210
->gadget
.speed
= value
& DMCR_HS_EN
?
782 USB_SPEED_HIGH
: USB_SPEED_FULL
;
786 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
787 switch (ctrl
->bRequest
) {
788 case USB_REQ_GET_STATUS
:
789 fotg210_get_status(fotg210
, ctrl
);
791 case USB_REQ_CLEAR_FEATURE
:
792 fotg210_clear_feature(fotg210
, ctrl
);
794 case USB_REQ_SET_FEATURE
:
795 fotg210_set_feature(fotg210
, ctrl
);
797 case USB_REQ_SET_ADDRESS
:
798 fotg210_set_address(fotg210
, ctrl
);
800 case USB_REQ_SET_CONFIGURATION
:
801 fotg210_set_configuration(fotg210
);
815 static void fotg210_ep0out(struct fotg210_udc
*fotg210
)
817 struct fotg210_ep
*ep
= fotg210
->ep
[0];
819 if (!list_empty(&ep
->queue
) && !ep
->dir_in
) {
820 struct fotg210_request
*req
;
822 req
= list_first_entry(&ep
->queue
,
823 struct fotg210_request
, queue
);
826 fotg210_start_dma(ep
, req
);
828 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
)
829 fotg210_done(ep
, req
, 0);
831 pr_err("%s : empty queue\n", __func__
);
835 static void fotg210_ep0in(struct fotg210_udc
*fotg210
)
837 struct fotg210_ep
*ep
= fotg210
->ep
[0];
839 if ((!list_empty(&ep
->queue
)) && (ep
->dir_in
)) {
840 struct fotg210_request
*req
;
842 req
= list_entry(ep
->queue
.next
,
843 struct fotg210_request
, queue
);
846 fotg210_start_dma(ep
, req
);
848 if (req
->req
.actual
== req
->req
.length
)
849 fotg210_done(ep
, req
, 0);
851 fotg210_set_cxdone(fotg210
);
855 static void fotg210_in_fifo_handler(struct fotg210_ep
*ep
)
857 struct fotg210_request
*req
= list_entry(ep
->queue
.next
,
858 struct fotg210_request
, queue
);
861 fotg210_start_dma(ep
, req
);
862 fotg210_done(ep
, req
, 0);
865 static void fotg210_out_fifo_handler(struct fotg210_ep
*ep
)
867 struct fotg210_request
*req
= list_entry(ep
->queue
.next
,
868 struct fotg210_request
, queue
);
869 int disgr1
= ioread32(ep
->fotg210
->reg
+ FOTG210_DISGR1
);
871 fotg210_start_dma(ep
, req
);
873 /* Complete the request when it's full or a short packet arrived.
874 * Like other drivers, short_not_ok isn't handled.
877 if (req
->req
.length
== req
->req
.actual
||
878 (disgr1
& DISGR1_SPK_INT(ep
->epnum
- 1)))
879 fotg210_done(ep
, req
, 0);
882 static irqreturn_t
fotg210_irq(int irq
, void *_fotg210
)
884 struct fotg210_udc
*fotg210
= _fotg210
;
885 u32 int_grp
= ioread32(fotg210
->reg
+ FOTG210_DIGR
);
886 u32 int_msk
= ioread32(fotg210
->reg
+ FOTG210_DMIGR
);
890 spin_lock(&fotg210
->lock
);
892 if (int_grp
& DIGR_INT_G2
) {
893 void __iomem
*reg
= fotg210
->reg
+ FOTG210_DISGR2
;
894 u32 int_grp2
= ioread32(reg
);
895 u32 int_msk2
= ioread32(fotg210
->reg
+ FOTG210_DMISGR2
);
897 int_grp2
&= ~int_msk2
;
899 if (int_grp2
& DISGR2_USBRST_INT
) {
900 usb_gadget_udc_reset(&fotg210
->gadget
,
902 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_USBRST_INT
);
903 pr_info("fotg210 udc reset\n");
905 if (int_grp2
& DISGR2_SUSP_INT
) {
906 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_SUSP_INT
);
907 pr_info("fotg210 udc suspend\n");
909 if (int_grp2
& DISGR2_RESM_INT
) {
910 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_RESM_INT
);
911 pr_info("fotg210 udc resume\n");
913 if (int_grp2
& DISGR2_ISO_SEQ_ERR_INT
) {
914 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_ISO_SEQ_ERR_INT
);
915 pr_info("fotg210 iso sequence error\n");
917 if (int_grp2
& DISGR2_ISO_SEQ_ABORT_INT
) {
918 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_ISO_SEQ_ABORT_INT
);
919 pr_info("fotg210 iso sequence abort\n");
921 if (int_grp2
& DISGR2_TX0BYTE_INT
) {
922 fotg210_clear_tx0byte(fotg210
);
923 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_TX0BYTE_INT
);
924 pr_info("fotg210 transferred 0 byte\n");
926 if (int_grp2
& DISGR2_RX0BYTE_INT
) {
927 fotg210_clear_rx0byte(fotg210
);
928 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_RX0BYTE_INT
);
929 pr_info("fotg210 received 0 byte\n");
931 if (int_grp2
& DISGR2_DMA_ERROR
) {
932 fotg210_ack_int(fotg210
, FOTG210_DISGR2
, DISGR2_DMA_ERROR
);
936 if (int_grp
& DIGR_INT_G0
) {
937 void __iomem
*reg
= fotg210
->reg
+ FOTG210_DISGR0
;
938 u32 int_grp0
= ioread32(reg
);
939 u32 int_msk0
= ioread32(fotg210
->reg
+ FOTG210_DMISGR0
);
940 struct usb_ctrlrequest ctrl
;
942 int_grp0
&= ~int_msk0
;
944 /* the highest priority in this source register */
945 if (int_grp0
& DISGR0_CX_COMABT_INT
) {
946 fotg210_ack_int(fotg210
, FOTG210_DISGR0
, DISGR0_CX_COMABT_INT
);
947 pr_info("fotg210 CX command abort\n");
950 if (int_grp0
& DISGR0_CX_SETUP_INT
) {
951 if (fotg210_setup_packet(fotg210
, &ctrl
)) {
952 spin_unlock(&fotg210
->lock
);
953 if (fotg210
->driver
->setup(&fotg210
->gadget
,
955 fotg210_set_cxstall(fotg210
);
956 spin_lock(&fotg210
->lock
);
959 if (int_grp0
& DISGR0_CX_COMEND_INT
)
960 pr_info("fotg210 cmd end\n");
962 if (int_grp0
& DISGR0_CX_IN_INT
)
963 fotg210_ep0in(fotg210
);
965 if (int_grp0
& DISGR0_CX_OUT_INT
)
966 fotg210_ep0out(fotg210
);
968 if (int_grp0
& DISGR0_CX_COMFAIL_INT
) {
969 fotg210_set_cxstall(fotg210
);
970 pr_info("fotg210 ep0 fail\n");
974 if (int_grp
& DIGR_INT_G1
) {
975 void __iomem
*reg
= fotg210
->reg
+ FOTG210_DISGR1
;
976 u32 int_grp1
= ioread32(reg
);
977 u32 int_msk1
= ioread32(fotg210
->reg
+ FOTG210_DMISGR1
);
980 int_grp1
&= ~int_msk1
;
982 for (fifo
= 0; fifo
< FOTG210_MAX_FIFO_NUM
; fifo
++) {
983 if (int_grp1
& DISGR1_IN_INT(fifo
))
984 fotg210_in_fifo_handler(fotg210
->ep
[fifo
+ 1]);
986 if ((int_grp1
& DISGR1_OUT_INT(fifo
)) ||
987 (int_grp1
& DISGR1_SPK_INT(fifo
)))
988 fotg210_out_fifo_handler(fotg210
->ep
[fifo
+ 1]);
992 spin_unlock(&fotg210
->lock
);
997 static void fotg210_disable_unplug(struct fotg210_udc
*fotg210
)
999 u32 reg
= ioread32(fotg210
->reg
+ FOTG210_PHYTMSR
);
1001 reg
&= ~PHYTMSR_UNPLUG
;
1002 iowrite32(reg
, fotg210
->reg
+ FOTG210_PHYTMSR
);
1005 static int fotg210_udc_start(struct usb_gadget
*g
,
1006 struct usb_gadget_driver
*driver
)
1008 struct fotg210_udc
*fotg210
= gadget_to_fotg210(g
);
1012 /* hook up the driver */
1013 fotg210
->driver
= driver
;
1014 fotg210
->gadget
.dev
.of_node
= fotg210
->dev
->of_node
;
1015 fotg210
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1017 dev_info(fotg210
->dev
, "bound driver %s\n", driver
->driver
.name
);
1019 if (!IS_ERR_OR_NULL(fotg210
->phy
)) {
1020 ret
= otg_set_peripheral(fotg210
->phy
->otg
,
1023 dev_err(fotg210
->dev
, "can't bind to phy\n");
1027 value
= ioread32(fotg210
->reg
+ FOTG210_DMCR
);
1028 value
|= DMCR_CHIP_EN
;
1029 iowrite32(value
, fotg210
->reg
+ FOTG210_DMCR
);
1031 /* enable device global interrupt */
1032 value
= ioread32(fotg210
->reg
+ FOTG210_DMCR
);
1033 value
|= DMCR_GLINT_EN
;
1034 iowrite32(value
, fotg210
->reg
+ FOTG210_DMCR
);
1039 static void fotg210_init(struct fotg210_udc
*fotg210
)
1043 /* disable global interrupt and set int polarity to active high */
1044 iowrite32(GMIR_MHC_INT
| GMIR_MOTG_INT
| GMIR_INT_POLARITY
,
1045 fotg210
->reg
+ FOTG210_GMIR
);
1047 /* mask interrupts for groups other than 0-2 */
1048 iowrite32(~(DMIGR_MINT_G0
| DMIGR_MINT_G1
| DMIGR_MINT_G2
),
1049 fotg210
->reg
+ FOTG210_DMIGR
);
1051 /* udc software reset */
1052 iowrite32(DMCR_SFRST
, fotg210
->reg
+ FOTG210_DMCR
);
1053 /* Better wait a bit, but without a datasheet, no idea how long. */
1054 usleep_range(100, 200);
1056 /* disable device global interrupt */
1057 value
= ioread32(fotg210
->reg
+ FOTG210_DMCR
);
1058 value
&= ~DMCR_GLINT_EN
;
1059 iowrite32(value
, fotg210
->reg
+ FOTG210_DMCR
);
1061 /* enable only grp2 irqs we handle */
1062 iowrite32(~(DISGR2_DMA_ERROR
| DISGR2_RX0BYTE_INT
| DISGR2_TX0BYTE_INT
1063 | DISGR2_ISO_SEQ_ABORT_INT
| DISGR2_ISO_SEQ_ERR_INT
1064 | DISGR2_RESM_INT
| DISGR2_SUSP_INT
| DISGR2_USBRST_INT
),
1065 fotg210
->reg
+ FOTG210_DMISGR2
);
1067 /* disable all fifo interrupt */
1068 iowrite32(~(u32
)0, fotg210
->reg
+ FOTG210_DMISGR1
);
1070 /* disable cmd end */
1071 value
= ioread32(fotg210
->reg
+ FOTG210_DMISGR0
);
1072 value
|= DMISGR0_MCX_COMEND
;
1073 iowrite32(value
, fotg210
->reg
+ FOTG210_DMISGR0
);
1076 static int fotg210_udc_stop(struct usb_gadget
*g
)
1078 struct fotg210_udc
*fotg210
= gadget_to_fotg210(g
);
1079 unsigned long flags
;
1081 if (!IS_ERR_OR_NULL(fotg210
->phy
))
1082 return otg_set_peripheral(fotg210
->phy
->otg
, NULL
);
1084 spin_lock_irqsave(&fotg210
->lock
, flags
);
1086 fotg210_init(fotg210
);
1087 fotg210
->driver
= NULL
;
1088 fotg210
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1090 spin_unlock_irqrestore(&fotg210
->lock
, flags
);
1096 * fotg210_vbus_session - Called by external transceiver to enable/disable udc
1098 * @is_active: 0 if should disable UDC VBUS, 1 if should enable
1102 static int fotg210_vbus_session(struct usb_gadget
*g
, int is_active
)
1104 struct fotg210_udc
*fotg210
= gadget_to_fotg210(g
);
1106 /* Call down to core integration layer to drive or disable VBUS */
1107 fotg210_vbus(fotg210
->fotg
, is_active
);
1111 static const struct usb_gadget_ops fotg210_gadget_ops
= {
1112 .udc_start
= fotg210_udc_start
,
1113 .udc_stop
= fotg210_udc_stop
,
1114 .vbus_session
= fotg210_vbus_session
,
1118 * fotg210_phy_event - Called by phy upon VBus event
1119 * @nb: notifier block
1120 * @action: phy action, is vbus connect or disconnect
1121 * @data: the usb_gadget structure in fotg210
1123 * Called by the USB Phy when a cable connect or disconnect is sensed.
1125 * Returns: NOTIFY_OK or NOTIFY_DONE
1127 static int fotg210_phy_event(struct notifier_block
*nb
, unsigned long action
,
1130 struct usb_gadget
*gadget
= data
;
1136 case USB_EVENT_VBUS
:
1137 usb_gadget_vbus_connect(gadget
);
1139 case USB_EVENT_NONE
:
1140 usb_gadget_vbus_disconnect(gadget
);
1147 static struct notifier_block fotg210_phy_notifier
= {
1148 .notifier_call
= fotg210_phy_event
,
1151 int fotg210_udc_remove(struct platform_device
*pdev
)
1153 struct fotg210_udc
*fotg210
= platform_get_drvdata(pdev
);
1156 usb_del_gadget_udc(&fotg210
->gadget
);
1157 if (!IS_ERR_OR_NULL(fotg210
->phy
)) {
1158 usb_unregister_notifier(fotg210
->phy
, &fotg210_phy_notifier
);
1159 usb_put_phy(fotg210
->phy
);
1161 iounmap(fotg210
->reg
);
1162 free_irq(platform_get_irq(pdev
, 0), fotg210
);
1164 fotg210_ep_free_request(&fotg210
->ep
[0]->ep
, fotg210
->ep0_req
);
1165 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++)
1166 kfree(fotg210
->ep
[i
]);
1173 int fotg210_udc_probe(struct platform_device
*pdev
, struct fotg210
*fotg
)
1175 struct fotg210_udc
*fotg210
= NULL
;
1176 struct device
*dev
= &pdev
->dev
;
1181 irq
= platform_get_irq(pdev
, 0);
1185 /* initialize udc */
1186 fotg210
= kzalloc(sizeof(struct fotg210_udc
), GFP_KERNEL
);
1187 if (fotg210
== NULL
)
1191 fotg210
->fotg
= fotg
;
1193 fotg210
->phy
= devm_usb_get_phy_by_phandle(dev
, "usb-phy", 0);
1194 if (IS_ERR(fotg210
->phy
)) {
1195 ret
= PTR_ERR(fotg210
->phy
);
1196 if (ret
== -EPROBE_DEFER
)
1198 dev_info(dev
, "no PHY found\n");
1199 fotg210
->phy
= NULL
;
1201 ret
= usb_phy_init(fotg210
->phy
);
1204 dev_info(dev
, "found and initialized PHY\n");
1209 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++) {
1210 fotg210
->ep
[i
] = kzalloc(sizeof(struct fotg210_ep
), GFP_KERNEL
);
1211 if (!fotg210
->ep
[i
])
1215 fotg210
->reg
= fotg
->base
;
1217 spin_lock_init(&fotg210
->lock
);
1219 platform_set_drvdata(pdev
, fotg210
);
1221 fotg210
->gadget
.ops
= &fotg210_gadget_ops
;
1223 fotg210
->gadget
.max_speed
= USB_SPEED_HIGH
;
1224 fotg210
->gadget
.dev
.parent
= dev
;
1225 fotg210
->gadget
.dev
.dma_mask
= dev
->dma_mask
;
1226 fotg210
->gadget
.name
= udc_name
;
1228 INIT_LIST_HEAD(&fotg210
->gadget
.ep_list
);
1230 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++) {
1231 struct fotg210_ep
*ep
= fotg210
->ep
[i
];
1234 INIT_LIST_HEAD(&fotg210
->ep
[i
]->ep
.ep_list
);
1235 list_add_tail(&fotg210
->ep
[i
]->ep
.ep_list
,
1236 &fotg210
->gadget
.ep_list
);
1238 ep
->fotg210
= fotg210
;
1239 INIT_LIST_HEAD(&ep
->queue
);
1240 ep
->ep
.name
= fotg210_ep_name
[i
];
1241 ep
->ep
.ops
= &fotg210_ep_ops
;
1242 usb_ep_set_maxpacket_limit(&ep
->ep
, (unsigned short) ~0);
1245 ep
->ep
.caps
.type_control
= true;
1247 ep
->ep
.caps
.type_iso
= true;
1248 ep
->ep
.caps
.type_bulk
= true;
1249 ep
->ep
.caps
.type_int
= true;
1252 ep
->ep
.caps
.dir_in
= true;
1253 ep
->ep
.caps
.dir_out
= true;
1255 usb_ep_set_maxpacket_limit(&fotg210
->ep
[0]->ep
, 0x40);
1256 fotg210
->gadget
.ep0
= &fotg210
->ep
[0]->ep
;
1257 INIT_LIST_HEAD(&fotg210
->gadget
.ep0
->ep_list
);
1259 fotg210
->ep0_req
= fotg210_ep_alloc_request(&fotg210
->ep
[0]->ep
,
1261 if (fotg210
->ep0_req
== NULL
)
1264 fotg210
->ep0_req
->complete
= fotg210_ep0_complete
;
1266 fotg210_init(fotg210
);
1268 fotg210_disable_unplug(fotg210
);
1270 ret
= request_irq(irq
, fotg210_irq
, IRQF_SHARED
,
1273 dev_err_probe(dev
, ret
, "request_irq error\n");
1277 if (!IS_ERR_OR_NULL(fotg210
->phy
))
1278 usb_register_notifier(fotg210
->phy
, &fotg210_phy_notifier
);
1280 ret
= usb_add_gadget_udc(dev
, &fotg210
->gadget
);
1284 dev_info(dev
, "version %s\n", DRIVER_VERSION
);
1289 if (!IS_ERR_OR_NULL(fotg210
->phy
))
1290 usb_unregister_notifier(fotg210
->phy
, &fotg210_phy_notifier
);
1291 free_irq(irq
, fotg210
);
1294 fotg210_ep_free_request(&fotg210
->ep
[0]->ep
, fotg210
->ep0_req
);
1297 iounmap(fotg210
->reg
);
1300 for (i
= 0; i
< FOTG210_MAX_NUM_EP
; i
++)
1301 kfree(fotg210
->ep
[i
]);