2 * Fusb300 UDC (USB gadget)
4 * Copyright (C) 2010 Faraday Technology Corp.
6 * Author : Yuan-hsin Chen <yhchen@faraday-tech.com>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; version 2 of the License.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 #include <linux/dma-mapping.h>
23 #include <linux/err.h>
24 #include <linux/interrupt.h>
26 #include <linux/platform_device.h>
27 #include <linux/usb/ch9.h>
28 #include <linux/usb/gadget.h>
30 #include "fusb300_udc.h"
32 MODULE_DESCRIPTION("FUSB300 USB gadget driver");
33 MODULE_LICENSE("GPL");
34 MODULE_AUTHOR("Yuan Hsin Chen <yhchen@faraday-tech.com>");
35 MODULE_ALIAS("platform:fusb300_udc");
37 #define DRIVER_VERSION "20 October 2010"
39 static const char udc_name
[] = "fusb300_udc";
40 static const char * const fusb300_ep_name
[] = {
41 "ep0", "ep1", "ep2", "ep3", "ep4", "ep5", "ep6", "ep7"
44 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
47 static void fusb300_enable_bit(struct fusb300
*fusb300
, u32 offset
,
50 u32 reg
= ioread32(fusb300
->reg
+ offset
);
53 iowrite32(reg
, fusb300
->reg
+ offset
);
56 static void fusb300_disable_bit(struct fusb300
*fusb300
, u32 offset
,
59 u32 reg
= ioread32(fusb300
->reg
+ offset
);
62 iowrite32(reg
, fusb300
->reg
+ offset
);
66 static void fusb300_ep_setting(struct fusb300_ep
*ep
,
67 struct fusb300_ep_info info
)
69 ep
->epnum
= info
.epnum
;
73 static int fusb300_ep_release(struct fusb300_ep
*ep
)
83 static void fusb300_set_fifo_entry(struct fusb300
*fusb300
,
86 u32 val
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
88 val
&= ~FUSB300_EPSET1_FIFOENTRY_MSK
;
89 val
|= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM
);
90 iowrite32(val
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
93 static void fusb300_set_start_entry(struct fusb300
*fusb300
,
96 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
97 u32 start_entry
= fusb300
->fifo_entry_num
* FUSB300_FIFO_ENTRY_NUM
;
99 reg
&= ~FUSB300_EPSET1_START_ENTRY_MSK
;
100 reg
|= FUSB300_EPSET1_START_ENTRY(start_entry
);
101 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
102 if (fusb300
->fifo_entry_num
== FUSB300_MAX_FIFO_ENTRY
) {
103 fusb300
->fifo_entry_num
= 0;
104 fusb300
->addrofs
= 0;
105 pr_err("fifo entry is over the maximum number!\n");
107 fusb300
->fifo_entry_num
++;
110 /* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
111 static void fusb300_set_epaddrofs(struct fusb300
*fusb300
,
112 struct fusb300_ep_info info
)
114 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
116 reg
&= ~FUSB300_EPSET2_ADDROFS_MSK
;
117 reg
|= FUSB300_EPSET2_ADDROFS(fusb300
->addrofs
);
118 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
119 fusb300
->addrofs
+= (info
.maxpacket
+ 7) / 8 * FUSB300_FIFO_ENTRY_NUM
;
122 static void ep_fifo_setting(struct fusb300
*fusb300
,
123 struct fusb300_ep_info info
)
125 fusb300_set_fifo_entry(fusb300
, info
.epnum
);
126 fusb300_set_start_entry(fusb300
, info
.epnum
);
127 fusb300_set_epaddrofs(fusb300
, info
);
130 static void fusb300_set_eptype(struct fusb300
*fusb300
,
131 struct fusb300_ep_info info
)
133 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
135 reg
&= ~FUSB300_EPSET1_TYPE_MSK
;
136 reg
|= FUSB300_EPSET1_TYPE(info
.type
);
137 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
140 static void fusb300_set_epdir(struct fusb300
*fusb300
,
141 struct fusb300_ep_info info
)
147 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
148 reg
&= ~FUSB300_EPSET1_DIR_MSK
;
149 reg
|= FUSB300_EPSET1_DIRIN
;
150 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
153 static void fusb300_set_ep_active(struct fusb300
*fusb300
,
156 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
158 reg
|= FUSB300_EPSET1_ACTEN
;
159 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
162 static void fusb300_set_epmps(struct fusb300
*fusb300
,
163 struct fusb300_ep_info info
)
165 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
167 reg
&= ~FUSB300_EPSET2_MPS_MSK
;
168 reg
|= FUSB300_EPSET2_MPS(info
.maxpacket
);
169 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
172 static void fusb300_set_interval(struct fusb300
*fusb300
,
173 struct fusb300_ep_info info
)
175 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
177 reg
&= ~FUSB300_EPSET1_INTERVAL(0x7);
178 reg
|= FUSB300_EPSET1_INTERVAL(info
.interval
);
179 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
182 static void fusb300_set_bwnum(struct fusb300
*fusb300
,
183 struct fusb300_ep_info info
)
185 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
187 reg
&= ~FUSB300_EPSET1_BWNUM(0x3);
188 reg
|= FUSB300_EPSET1_BWNUM(info
.bw_num
);
189 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
192 static void set_ep_reg(struct fusb300
*fusb300
,
193 struct fusb300_ep_info info
)
195 fusb300_set_eptype(fusb300
, info
);
196 fusb300_set_epdir(fusb300
, info
);
197 fusb300_set_epmps(fusb300
, info
);
200 fusb300_set_interval(fusb300
, info
);
203 fusb300_set_bwnum(fusb300
, info
);
205 fusb300_set_ep_active(fusb300
, info
.epnum
);
208 static int config_ep(struct fusb300_ep
*ep
,
209 const struct usb_endpoint_descriptor
*desc
)
211 struct fusb300
*fusb300
= ep
->fusb300
;
212 struct fusb300_ep_info info
;
220 info
.type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
221 info
.dir_in
= (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) ? 1 : 0;
222 info
.maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
);
223 info
.epnum
= desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
225 if ((info
.type
== USB_ENDPOINT_XFER_INT
) ||
226 (info
.type
== USB_ENDPOINT_XFER_ISOC
)) {
227 info
.interval
= desc
->bInterval
;
228 if (info
.type
== USB_ENDPOINT_XFER_ISOC
)
229 info
.bw_num
= ((desc
->wMaxPacketSize
& 0x1800) >> 11);
232 ep_fifo_setting(fusb300
, info
);
234 set_ep_reg(fusb300
, info
);
236 fusb300_ep_setting(ep
, info
);
238 fusb300
->ep
[info
.epnum
] = ep
;
243 static int fusb300_enable(struct usb_ep
*_ep
,
244 const struct usb_endpoint_descriptor
*desc
)
246 struct fusb300_ep
*ep
;
248 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
250 if (ep
->fusb300
->reenum
) {
251 ep
->fusb300
->fifo_entry_num
= 0;
252 ep
->fusb300
->addrofs
= 0;
253 ep
->fusb300
->reenum
= 0;
256 return config_ep(ep
, desc
);
259 static int fusb300_disable(struct usb_ep
*_ep
)
261 struct fusb300_ep
*ep
;
262 struct fusb300_request
*req
;
265 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
269 while (!list_empty(&ep
->queue
)) {
270 req
= list_entry(ep
->queue
.next
, struct fusb300_request
, queue
);
271 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
272 done(ep
, req
, -ECONNRESET
);
273 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
276 return fusb300_ep_release(ep
);
279 static struct usb_request
*fusb300_alloc_request(struct usb_ep
*_ep
,
282 struct fusb300_request
*req
;
284 req
= kzalloc(sizeof(struct fusb300_request
), gfp_flags
);
287 INIT_LIST_HEAD(&req
->queue
);
292 static void fusb300_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
294 struct fusb300_request
*req
;
296 req
= container_of(_req
, struct fusb300_request
, req
);
300 static int enable_fifo_int(struct fusb300_ep
*ep
)
302 struct fusb300
*fusb300
= ep
->fusb300
;
305 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
306 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
308 pr_err("can't enable_fifo_int ep0\n");
315 static int disable_fifo_int(struct fusb300_ep
*ep
)
317 struct fusb300
*fusb300
= ep
->fusb300
;
320 fusb300_disable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
321 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
323 pr_err("can't disable_fifo_int ep0\n");
330 static void fusb300_set_cxlen(struct fusb300
*fusb300
, u32 length
)
334 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
335 reg
&= ~FUSB300_CSR_LEN_MSK
;
336 reg
|= FUSB300_CSR_LEN(length
);
337 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_CSR
);
340 /* write data to cx fifo */
341 static void fusb300_wrcxf(struct fusb300_ep
*ep
,
342 struct fusb300_request
*req
)
347 struct fusb300
*fusb300
= ep
->fusb300
;
348 u32 length
= req
->req
.length
- req
->req
.actual
;
350 tmp
= req
->req
.buf
+ req
->req
.actual
;
352 if (length
> SS_CTL_MAX_PACKET_SIZE
) {
353 fusb300_set_cxlen(fusb300
, SS_CTL_MAX_PACKET_SIZE
);
354 for (i
= (SS_CTL_MAX_PACKET_SIZE
>> 2); i
> 0; i
--) {
355 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
357 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
360 req
->req
.actual
+= SS_CTL_MAX_PACKET_SIZE
;
361 } else { /* length is less than max packet size */
362 fusb300_set_cxlen(fusb300
, length
);
363 for (i
= length
>> 2; i
> 0; i
--) {
364 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
366 printk(KERN_DEBUG
" 0x%x\n", data
);
367 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
370 switch (length
% 4) {
373 printk(KERN_DEBUG
" 0x%x\n", data
);
374 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
377 data
= *tmp
| *(tmp
+ 1) << 8;
378 printk(KERN_DEBUG
" 0x%x\n", data
);
379 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
382 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16;
383 printk(KERN_DEBUG
" 0x%x\n", data
);
384 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
389 req
->req
.actual
+= length
;
393 static void fusb300_set_epnstall(struct fusb300
*fusb300
, u8 ep
)
395 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
399 static void fusb300_clear_epnstall(struct fusb300
*fusb300
, u8 ep
)
401 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
403 if (reg
& FUSB300_EPSET0_STL
) {
404 printk(KERN_DEBUG
"EP%d stall... Clear!!\n", ep
);
405 reg
&= ~FUSB300_EPSET0_STL
;
406 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
410 static void ep0_queue(struct fusb300_ep
*ep
, struct fusb300_request
*req
)
412 if (ep
->fusb300
->ep0_dir
) { /* if IN */
413 if (req
->req
.length
) {
414 fusb300_wrcxf(ep
, req
);
416 printk(KERN_DEBUG
"%s : req->req.length = 0x%x\n",
417 __func__
, req
->req
.length
);
418 if ((req
->req
.length
== req
->req
.actual
) ||
419 (req
->req
.actual
< ep
->ep
.maxpacket
))
422 if (!req
->req
.length
)
425 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER1
,
426 FUSB300_IGER1_CX_OUT_INT
);
430 static int fusb300_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
433 struct fusb300_ep
*ep
;
434 struct fusb300_request
*req
;
438 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
439 req
= container_of(_req
, struct fusb300_request
, req
);
441 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
444 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
446 if (list_empty(&ep
->queue
))
449 list_add_tail(&req
->queue
, &ep
->queue
);
452 req
->req
.status
= -EINPROGRESS
;
454 if (ep
->desc
== NULL
) /* ep0 */
456 else if (request
&& !ep
->stall
)
459 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
464 static int fusb300_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
466 struct fusb300_ep
*ep
;
467 struct fusb300_request
*req
;
470 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
471 req
= container_of(_req
, struct fusb300_request
, req
);
473 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
474 if (!list_empty(&ep
->queue
))
475 done(ep
, req
, -ECONNRESET
);
476 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
481 static int fusb300_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedge
)
483 struct fusb300_ep
*ep
;
484 struct fusb300
*fusb300
;
488 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
490 fusb300
= ep
->fusb300
;
492 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
494 if (!list_empty(&ep
->queue
)) {
500 fusb300_set_epnstall(fusb300
, ep
->epnum
);
505 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
511 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
515 static int fusb300_set_halt(struct usb_ep
*_ep
, int value
)
517 return fusb300_set_halt_and_wedge(_ep
, value
, 0);
520 static int fusb300_set_wedge(struct usb_ep
*_ep
)
522 return fusb300_set_halt_and_wedge(_ep
, 1, 1);
525 static void fusb300_fifo_flush(struct usb_ep
*_ep
)
529 static struct usb_ep_ops fusb300_ep_ops
= {
530 .enable
= fusb300_enable
,
531 .disable
= fusb300_disable
,
533 .alloc_request
= fusb300_alloc_request
,
534 .free_request
= fusb300_free_request
,
536 .queue
= fusb300_queue
,
537 .dequeue
= fusb300_dequeue
,
539 .set_halt
= fusb300_set_halt
,
540 .fifo_flush
= fusb300_fifo_flush
,
541 .set_wedge
= fusb300_set_wedge
,
544 /*****************************************************************************/
545 static void fusb300_clear_int(struct fusb300
*fusb300
, u32 offset
,
548 iowrite32(value
, fusb300
->reg
+ offset
);
551 static void fusb300_reset(void)
555 static void fusb300_set_cxstall(struct fusb300
*fusb300
)
557 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
561 static void fusb300_set_cxdone(struct fusb300
*fusb300
)
563 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
567 /* read data from cx fifo */
568 void fusb300_rdcxf(struct fusb300
*fusb300
,
569 u8
*buffer
, u32 length
)
577 for (i
= (length
>> 2); i
> 0; i
--) {
578 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
579 printk(KERN_DEBUG
" 0x%x\n", data
);
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(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
590 printk(KERN_DEBUG
" 0x%x\n", data
);
594 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
595 printk(KERN_DEBUG
" 0x%x\n", data
);
597 *(tmp
+ 1) = (data
>> 8) & 0xFF;
600 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
601 printk(KERN_DEBUG
" 0x%x\n", data
);
603 *(tmp
+ 1) = (data
>> 8) & 0xFF;
604 *(tmp
+ 2) = (data
>> 16) & 0xFF;
612 static void fusb300_dbg_fifo(struct fusb300_ep
*ep
,
613 u8 entry
, u16 length
)
619 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
620 reg
&= ~(FUSB300_GTM_TST_EP_ENTRY(0xF) |
621 FUSB300_GTM_TST_EP_NUM(0xF) | FUSB300_GTM_TST_FIFO_DEG
);
622 reg
|= (FUSB300_GTM_TST_EP_ENTRY(entry
) |
623 FUSB300_GTM_TST_EP_NUM(ep
->epnum
) | FUSB300_GTM_TST_FIFO_DEG
);
624 iowrite32(reg
, ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
626 for (i
= 0; i
< (length
>> 2); i
++) {
629 reg
= ioread32(ep
->fusb300
->reg
+
630 FUSB300_OFFSET_BUFDBG_START
+ i
* 4);
631 printk(KERN_DEBUG
" 0x%-8x", reg
);
634 printk(KERN_DEBUG
"\n");
638 reg
= ioread32(ep
->fusb300
->reg
+
639 FUSB300_OFFSET_BUFDBG_START
+ i
* 4);
640 printk(KERN_DEBUG
" 0x%x\n", reg
);
644 printk(KERN_DEBUG
"\n");
646 fusb300_disable_bit(ep
->fusb300
, FUSB300_OFFSET_GTM
,
647 FUSB300_GTM_TST_FIFO_DEG
);
650 static void fusb300_cmp_dbg_fifo(struct fusb300_ep
*ep
,
651 u8 entry
, u16 length
, u8
*golden
)
660 printk(KERN_DEBUG
"fusb300_cmp_dbg_fifo (entry %d) : start\n", entry
);
662 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
663 reg
&= ~(FUSB300_GTM_TST_EP_ENTRY(0xF) |
664 FUSB300_GTM_TST_EP_NUM(0xF) | FUSB300_GTM_TST_FIFO_DEG
);
665 reg
|= (FUSB300_GTM_TST_EP_ENTRY(entry
) |
666 FUSB300_GTM_TST_EP_NUM(ep
->epnum
) | FUSB300_GTM_TST_FIFO_DEG
);
667 iowrite32(reg
, ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
669 for (i
= 0; i
< (length
>> 2); i
++) {
672 golden_value
= *tmp
| *(tmp
+ 1) << 8 |
673 *(tmp
+ 2) << 16 | *(tmp
+ 3) << 24;
675 reg
= ioread32(ep
->fusb300
->reg
+
676 FUSB300_OFFSET_BUFDBG_START
+ i
*4);
678 if (reg
!= golden_value
) {
679 printk(KERN_DEBUG
"0x%x : ", (u32
)(ep
->fusb300
->reg
+
680 FUSB300_OFFSET_BUFDBG_START
+ i
*4));
681 printk(KERN_DEBUG
" golden = 0x%x, reg = 0x%x\n",
687 switch (length
% 4) {
691 golden_value
= *tmp
| *(tmp
+ 1) << 8;
693 golden_value
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16;
697 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_BUFDBG_START
+ i
*4);
698 if (reg
!= golden_value
) {
699 printk(KERN_DEBUG
"0x%x:", (u32
)(ep
->fusb300
->reg
+
700 FUSB300_OFFSET_BUFDBG_START
+ i
*4));
701 printk(KERN_DEBUG
" golden = 0x%x, reg = 0x%x\n",
706 printk(KERN_DEBUG
"fusb300_cmp_dbg_fifo : end\n");
707 fusb300_disable_bit(ep
->fusb300
, FUSB300_OFFSET_GTM
,
708 FUSB300_GTM_TST_FIFO_DEG
);
712 static void fusb300_rdfifo(struct fusb300_ep
*ep
,
713 struct fusb300_request
*req
,
719 struct fusb300
*fusb300
= ep
->fusb300
;
721 tmp
= req
->req
.buf
+ req
->req
.actual
;
722 req
->req
.actual
+= length
;
724 if (req
->req
.actual
> req
->req
.length
)
725 printk(KERN_DEBUG
"req->req.actual > req->req.length\n");
727 for (i
= (length
>> 2); i
> 0; i
--) {
728 data
= ioread32(fusb300
->reg
+
729 FUSB300_OFFSET_EPPORT(ep
->epnum
));
731 *(tmp
+ 1) = (data
>> 8) & 0xFF;
732 *(tmp
+ 2) = (data
>> 16) & 0xFF;
733 *(tmp
+ 3) = (data
>> 24) & 0xFF;
737 switch (length
% 4) {
739 data
= ioread32(fusb300
->reg
+
740 FUSB300_OFFSET_EPPORT(ep
->epnum
));
744 data
= ioread32(fusb300
->reg
+
745 FUSB300_OFFSET_EPPORT(ep
->epnum
));
747 *(tmp
+ 1) = (data
>> 8) & 0xFF;
750 data
= ioread32(fusb300
->reg
+
751 FUSB300_OFFSET_EPPORT(ep
->epnum
));
753 *(tmp
+ 1) = (data
>> 8) & 0xFF;
754 *(tmp
+ 2) = (data
>> 16) & 0xFF;
761 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
762 reg
&= FUSB300_IGR1_SYNF0_EMPTY_INT
;
764 printk(KERN_INFO
"sync fifo is not empty!\n");
769 /* write data to fifo */
770 static void fusb300_wrfifo(struct fusb300_ep
*ep
,
771 struct fusb300_request
*req
)
776 struct fusb300
*fusb300
= ep
->fusb300
;
779 req
->req
.actual
= req
->req
.length
;
781 for (i
= (req
->req
.length
>> 2); i
> 0; i
--) {
782 data
= *tmp
| *(tmp
+ 1) << 8 |
783 *(tmp
+ 2) << 16 | *(tmp
+ 3) << 24;
785 iowrite32(data
, fusb300
->reg
+
786 FUSB300_OFFSET_EPPORT(ep
->epnum
));
790 switch (req
->req
.length
% 4) {
793 iowrite32(data
, fusb300
->reg
+
794 FUSB300_OFFSET_EPPORT(ep
->epnum
));
797 data
= *tmp
| *(tmp
+ 1) << 8;
798 iowrite32(data
, fusb300
->reg
+
799 FUSB300_OFFSET_EPPORT(ep
->epnum
));
802 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16;
803 iowrite32(data
, fusb300
->reg
+
804 FUSB300_OFFSET_EPPORT(ep
->epnum
));
811 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
812 reg
&= FUSB300_IGR1_SYNF0_EMPTY_INT
;
814 printk(KERN_INFO
"sync fifo is not empty!\n");
819 static u8
fusb300_get_epnstall(struct fusb300
*fusb300
, u8 ep
)
822 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
824 value
= reg
& FUSB300_EPSET0_STL
;
829 static u8
fusb300_get_cxstall(struct fusb300
*fusb300
)
832 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
834 value
= (reg
& FUSB300_CSR_STL
) >> 1;
839 static void request_error(struct fusb300
*fusb300
)
841 fusb300_set_cxstall(fusb300
);
842 printk(KERN_DEBUG
"request error!!\n");
845 static void get_status(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
846 __releases(fusb300
->lock
)
847 __acquires(fusb300
->lock
)
851 u16 w_index
= ctrl
->wIndex
;
853 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
854 case USB_RECIP_DEVICE
:
855 status
= 1 << USB_DEVICE_SELF_POWERED
;
857 case USB_RECIP_INTERFACE
:
860 case USB_RECIP_ENDPOINT
:
861 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
863 if (fusb300_get_epnstall(fusb300
, ep
))
864 status
= 1 << USB_ENDPOINT_HALT
;
866 if (fusb300_get_cxstall(fusb300
))
872 request_error(fusb300
);
876 fusb300
->ep0_data
= cpu_to_le16(status
);
877 fusb300
->ep0_req
->buf
= &fusb300
->ep0_data
;
878 fusb300
->ep0_req
->length
= 2;
880 spin_unlock(&fusb300
->lock
);
881 fusb300_queue(fusb300
->gadget
.ep0
, fusb300
->ep0_req
, GFP_KERNEL
);
882 spin_lock(&fusb300
->lock
);
885 static void set_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
889 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
890 case USB_RECIP_DEVICE
:
891 fusb300_set_cxdone(fusb300
);
893 case USB_RECIP_INTERFACE
:
894 fusb300_set_cxdone(fusb300
);
896 case USB_RECIP_ENDPOINT
: {
897 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
899 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
901 fusb300_set_epnstall(fusb300
, ep
);
903 fusb300_set_cxstall(fusb300
);
904 fusb300_set_cxdone(fusb300
);
908 request_error(fusb300
);
913 static void fusb300_clear_seqnum(struct fusb300
*fusb300
, u8 ep
)
915 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
916 FUSB300_EPSET0_CLRSEQNUM
);
919 static void clear_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
921 struct fusb300_ep
*ep
=
922 fusb300
->ep
[ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
];
924 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
925 case USB_RECIP_DEVICE
:
926 fusb300_set_cxdone(fusb300
);
928 case USB_RECIP_INTERFACE
:
929 fusb300_set_cxdone(fusb300
);
931 case USB_RECIP_ENDPOINT
:
932 if (ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
) {
934 fusb300_set_cxdone(fusb300
);
939 fusb300_clear_seqnum(fusb300
, ep
->epnum
);
940 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
941 if (!list_empty(&ep
->queue
))
945 fusb300_set_cxdone(fusb300
);
948 request_error(fusb300
);
953 static void fusb300_set_dev_addr(struct fusb300
*fusb300
, u16 addr
)
955 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_DAR
);
957 reg
&= ~FUSB300_DAR_DRVADDR_MSK
;
958 reg
|= FUSB300_DAR_DRVADDR(addr
);
960 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_DAR
);
963 static void set_address(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
965 if (ctrl
->wValue
>= 0x0100)
966 request_error(fusb300
);
968 fusb300_set_dev_addr(fusb300
, ctrl
->wValue
);
969 fusb300_set_cxdone(fusb300
);
973 #define UVC_COPY_DESCRIPTORS(mem, src) \
975 const struct usb_descriptor_header * const *__src; \
976 for (__src = src; *__src; ++__src) { \
977 memcpy(mem, *__src, (*__src)->bLength); \
978 mem += (*__src)->bLength; \
982 static void fusb300_ep0_complete(struct usb_ep
*ep
,
983 struct usb_request
*req
)
987 static int setup_packet(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
993 fusb300_rdcxf(fusb300
, p
, 8);
994 fusb300
->ep0_dir
= ctrl
->bRequestType
& USB_DIR_IN
;
995 fusb300
->ep0_length
= ctrl
->wLength
;
998 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
999 switch (ctrl
->bRequest
) {
1000 case USB_REQ_GET_STATUS
:
1001 get_status(fusb300
, ctrl
);
1003 case USB_REQ_CLEAR_FEATURE
:
1004 clear_feature(fusb300
, ctrl
);
1006 case USB_REQ_SET_FEATURE
:
1007 set_feature(fusb300
, ctrl
);
1009 case USB_REQ_SET_ADDRESS
:
1010 set_address(fusb300
, ctrl
);
1012 case USB_REQ_SET_CONFIGURATION
:
1013 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_DAR
,
1014 FUSB300_DAR_SETCONFG
);
1015 /* clear sequence number */
1016 for (i
= 1; i
<= FUSB300_MAX_NUM_EP
; i
++)
1017 fusb300_clear_seqnum(fusb300
, i
);
1018 fusb300
->reenum
= 1;
1031 static void fusb300_set_ep_bycnt(struct fusb300_ep
*ep
, u32 bycnt
)
1033 struct fusb300
*fusb300
= ep
->fusb300
;
1034 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPFFR(ep
->epnum
));
1036 reg
&= ~FUSB300_FFR_BYCNT
;
1037 reg
|= bycnt
& FUSB300_FFR_BYCNT
;
1039 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPFFR(ep
->epnum
));
1042 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
1045 list_del_init(&req
->queue
);
1047 /* don't modify queue heads during completion callback */
1048 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
1049 req
->req
.status
= -ESHUTDOWN
;
1051 req
->req
.status
= status
;
1053 spin_unlock(&ep
->fusb300
->lock
);
1054 req
->req
.complete(&ep
->ep
, &req
->req
);
1055 spin_lock(&ep
->fusb300
->lock
);
1058 disable_fifo_int(ep
);
1059 if (!list_empty(&ep
->queue
))
1060 enable_fifo_int(ep
);
1062 fusb300_set_cxdone(ep
->fusb300
);
1065 void fusb300_fill_idma_prdtbl(struct fusb300_ep
*ep
,
1066 struct fusb300_request
*req
)
1073 reg
= ioread32(ep
->fusb300
->reg
+
1074 FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
1075 reg
&= FUSB300_EPPRD0_H
;
1078 iowrite32((u32
) req
->req
.buf
, ep
->fusb300
->reg
+
1079 FUSB300_OFFSET_EPPRD_W1(ep
->epnum
));
1081 value
= FUSB300_EPPRD0_BTC(req
->req
.length
) | FUSB300_EPPRD0_H
|
1082 FUSB300_EPPRD0_F
| FUSB300_EPPRD0_L
| FUSB300_EPPRD0_I
;
1083 iowrite32(value
, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
1085 iowrite32(0x0, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W2(ep
->epnum
));
1087 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_EPPRDRDY
,
1088 FUSB300_EPPRDR_EP_PRD_RDY(ep
->epnum
));
1091 static void fusb300_wait_idma_finished(struct fusb300_ep
*ep
)
1096 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
1097 if ((reg
& FUSB300_IGR1_VBUS_CHG_INT
) ||
1098 (reg
& FUSB300_IGR1_WARM_RST_INT
) ||
1099 (reg
& FUSB300_IGR1_HOT_RST_INT
) ||
1100 (reg
& FUSB300_IGR1_USBRST_INT
)
1103 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
1104 reg
&= FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
);
1107 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGR0
,
1108 FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
));
1110 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
1111 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
1114 static void fusb300_set_idma(struct fusb300_ep
*ep
,
1115 struct fusb300_request
*req
)
1120 d
= dma_map_single(NULL
, req
->req
.buf
, req
->req
.length
, DMA_TO_DEVICE
);
1122 if (dma_mapping_error(NULL
, d
)) {
1123 kfree(req
->req
.buf
);
1124 printk(KERN_DEBUG
"dma_mapping_error\n");
1127 dma_sync_single_for_device(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
1129 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
1130 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
1133 req
->req
.buf
= (u8
*)d
;
1135 fusb300_fill_idma_prdtbl(ep
, req
);
1136 /* check idma is done */
1137 fusb300_wait_idma_finished(ep
);
1142 dma_unmap_single(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
1145 static void in_ep_fifo_handler(struct fusb300_ep
*ep
)
1147 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
1148 struct fusb300_request
, queue
);
1150 if (req
->req
.length
) {
1152 fusb300_set_ep_bycnt(ep
, req
->req
.length
);
1153 fusb300_wrfifo(ep
, req
);
1155 fusb300_set_idma(ep
, req
);
1161 static void out_ep_fifo_handler(struct fusb300_ep
*ep
)
1163 struct fusb300
*fusb300
= ep
->fusb300
;
1164 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
1165 struct fusb300_request
, queue
);
1166 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPFFR(ep
->epnum
));
1167 u32 length
= reg
& FUSB300_FFR_BYCNT
;
1169 fusb300_rdfifo(ep
, req
, length
);
1171 /* finish out transfer */
1172 if ((req
->req
.length
== req
->req
.actual
) || (length
< ep
->ep
.maxpacket
))
1176 static void check_device_mode(struct fusb300
*fusb300
)
1178 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_GCR
);
1180 switch (reg
& FUSB300_GCR_DEVEN_MSK
) {
1181 case FUSB300_GCR_DEVEN_SS
:
1182 fusb300
->gadget
.speed
= USB_SPEED_SUPER
;
1184 case FUSB300_GCR_DEVEN_HS
:
1185 fusb300
->gadget
.speed
= USB_SPEED_HIGH
;
1187 case FUSB300_GCR_DEVEN_FS
:
1188 fusb300
->gadget
.speed
= USB_SPEED_FULL
;
1191 fusb300
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1194 printk(KERN_INFO
"dev_mode = %d\n", (reg
& FUSB300_GCR_DEVEN_MSK
));
1198 static void fusb300_ep0out(struct fusb300
*fusb300
)
1200 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1203 if (!list_empty(&ep
->queue
)) {
1204 struct fusb300_request
*req
;
1206 req
= list_first_entry(&ep
->queue
,
1207 struct fusb300_request
, queue
);
1208 if (req
->req
.length
)
1209 fusb300_rdcxf(ep
->fusb300
, req
->req
.buf
,
1212 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1213 reg
&= ~FUSB300_IGER1_CX_OUT_INT
;
1214 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1216 pr_err("%s : empty queue\n", __func__
);
1219 static void fusb300_ep0in(struct fusb300
*fusb300
)
1221 struct fusb300_request
*req
;
1222 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1224 if ((!list_empty(&ep
->queue
)) && (fusb300
->ep0_dir
)) {
1225 req
= list_entry(ep
->queue
.next
,
1226 struct fusb300_request
, queue
);
1227 if (req
->req
.length
)
1228 fusb300_wrcxf(ep
, req
);
1229 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
)
1232 fusb300_set_cxdone(fusb300
);
1235 static void fusb300_grp2_handler(void)
1239 static void fusb300_grp3_handler(void)
1243 static void fusb300_grp4_handler(void)
1247 static void fusb300_grp5_handler(void)
1251 static irqreturn_t
fusb300_irq(int irq
, void *_fusb300
)
1253 struct fusb300
*fusb300
= _fusb300
;
1254 u32 int_grp1
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
1255 u32 int_grp1_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1256 u32 int_grp0
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
1257 u32 int_grp0_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER0
);
1258 struct usb_ctrlrequest ctrl
;
1263 spin_lock(&fusb300
->lock
);
1265 int_grp1
&= int_grp1_en
;
1266 int_grp0
&= int_grp0_en
;
1268 if (int_grp1
& FUSB300_IGR1_WARM_RST_INT
) {
1269 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1270 FUSB300_IGR1_WARM_RST_INT
);
1271 printk(KERN_INFO
"fusb300_warmreset\n");
1275 if (int_grp1
& FUSB300_IGR1_HOT_RST_INT
) {
1276 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1277 FUSB300_IGR1_HOT_RST_INT
);
1278 printk(KERN_INFO
"fusb300_hotreset\n");
1282 if (int_grp1
& FUSB300_IGR1_USBRST_INT
) {
1283 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1284 FUSB300_IGR1_USBRST_INT
);
1287 /* COMABT_INT has a highest priority */
1289 if (int_grp1
& FUSB300_IGR1_CX_COMABT_INT
) {
1290 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1291 FUSB300_IGR1_CX_COMABT_INT
);
1292 printk(KERN_INFO
"fusb300_ep0abt\n");
1295 if (int_grp1
& FUSB300_IGR1_VBUS_CHG_INT
) {
1296 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1297 FUSB300_IGR1_VBUS_CHG_INT
);
1298 printk(KERN_INFO
"fusb300_vbus_change\n");
1301 if (int_grp1
& FUSB300_IGR1_U3_EXIT_FAIL_INT
) {
1302 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1303 FUSB300_IGR1_U3_EXIT_FAIL_INT
);
1306 if (int_grp1
& FUSB300_IGR1_U2_EXIT_FAIL_INT
) {
1307 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1308 FUSB300_IGR1_U2_EXIT_FAIL_INT
);
1311 if (int_grp1
& FUSB300_IGR1_U1_EXIT_FAIL_INT
) {
1312 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1313 FUSB300_IGR1_U1_EXIT_FAIL_INT
);
1316 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_FAIL_INT
) {
1317 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1318 FUSB300_IGR1_U2_ENTRY_FAIL_INT
);
1321 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_FAIL_INT
) {
1322 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1323 FUSB300_IGR1_U1_ENTRY_FAIL_INT
);
1326 if (int_grp1
& FUSB300_IGR1_U3_EXIT_INT
) {
1327 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1328 FUSB300_IGR1_U3_EXIT_INT
);
1329 printk(KERN_INFO
"FUSB300_IGR1_U3_EXIT_INT\n");
1332 if (int_grp1
& FUSB300_IGR1_U2_EXIT_INT
) {
1333 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1334 FUSB300_IGR1_U2_EXIT_INT
);
1335 printk(KERN_INFO
"FUSB300_IGR1_U2_EXIT_INT\n");
1338 if (int_grp1
& FUSB300_IGR1_U1_EXIT_INT
) {
1339 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1340 FUSB300_IGR1_U1_EXIT_INT
);
1341 printk(KERN_INFO
"FUSB300_IGR1_U1_EXIT_INT\n");
1344 if (int_grp1
& FUSB300_IGR1_U3_ENTRY_INT
) {
1345 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1346 FUSB300_IGR1_U3_ENTRY_INT
);
1347 printk(KERN_INFO
"FUSB300_IGR1_U3_ENTRY_INT\n");
1348 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_SSCR1
,
1349 FUSB300_SSCR1_GO_U3_DONE
);
1352 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_INT
) {
1353 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1354 FUSB300_IGR1_U2_ENTRY_INT
);
1355 printk(KERN_INFO
"FUSB300_IGR1_U2_ENTRY_INT\n");
1358 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_INT
) {
1359 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1360 FUSB300_IGR1_U1_ENTRY_INT
);
1361 printk(KERN_INFO
"FUSB300_IGR1_U1_ENTRY_INT\n");
1364 if (int_grp1
& FUSB300_IGR1_RESM_INT
) {
1365 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1366 FUSB300_IGR1_RESM_INT
);
1367 printk(KERN_INFO
"fusb300_resume\n");
1370 if (int_grp1
& FUSB300_IGR1_SUSP_INT
) {
1371 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1372 FUSB300_IGR1_SUSP_INT
);
1373 printk(KERN_INFO
"fusb300_suspend\n");
1376 if (int_grp1
& FUSB300_IGR1_HS_LPM_INT
) {
1377 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1378 FUSB300_IGR1_HS_LPM_INT
);
1379 printk(KERN_INFO
"fusb300_HS_LPM_INT\n");
1382 if (int_grp1
& FUSB300_IGR1_DEV_MODE_CHG_INT
) {
1383 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1384 FUSB300_IGR1_DEV_MODE_CHG_INT
);
1385 check_device_mode(fusb300
);
1388 if (int_grp1
& FUSB300_IGR1_CX_COMFAIL_INT
) {
1389 fusb300_set_cxstall(fusb300
);
1390 printk(KERN_INFO
"fusb300_ep0fail\n");
1393 if (int_grp1
& FUSB300_IGR1_CX_SETUP_INT
) {
1394 printk(KERN_INFO
"fusb300_ep0setup\n");
1395 if (setup_packet(fusb300
, &ctrl
)) {
1396 spin_unlock(&fusb300
->lock
);
1397 if (fusb300
->driver
->setup(&fusb300
->gadget
, &ctrl
) < 0)
1398 fusb300_set_cxstall(fusb300
);
1399 spin_lock(&fusb300
->lock
);
1403 if (int_grp1
& FUSB300_IGR1_CX_CMDEND_INT
)
1404 printk(KERN_INFO
"fusb300_cmdend\n");
1407 if (int_grp1
& FUSB300_IGR1_CX_OUT_INT
) {
1408 printk(KERN_INFO
"fusb300_cxout\n");
1409 fusb300_ep0out(fusb300
);
1412 if (int_grp1
& FUSB300_IGR1_CX_IN_INT
) {
1413 printk(KERN_INFO
"fusb300_cxin\n");
1414 fusb300_ep0in(fusb300
);
1417 if (int_grp1
& FUSB300_IGR1_INTGRP5
)
1418 fusb300_grp5_handler();
1420 if (int_grp1
& FUSB300_IGR1_INTGRP4
)
1421 fusb300_grp4_handler();
1423 if (int_grp1
& FUSB300_IGR1_INTGRP3
)
1424 fusb300_grp3_handler();
1426 if (int_grp1
& FUSB300_IGR1_INTGRP2
)
1427 fusb300_grp2_handler();
1430 for (i
= 1; i
< FUSB300_MAX_NUM_EP
; i
++) {
1431 if (int_grp0
& FUSB300_IGR0_EPn_FIFO_INT(i
)) {
1432 reg
= ioread32(fusb300
->reg
+
1433 FUSB300_OFFSET_EPSET1(i
));
1434 in
= (reg
& FUSB300_EPSET1_DIRIN
) ? 1 : 0;
1436 in_ep_fifo_handler(fusb300
->ep
[i
]);
1438 out_ep_fifo_handler(fusb300
->ep
[i
]);
1443 spin_unlock(&fusb300
->lock
);
1448 static void fusb300_set_u2_timeout(struct fusb300
*fusb300
,
1453 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1455 reg
|= FUSB300_SSCR2_U2TIMEOUT(time
);
1457 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1460 static void fusb300_set_u1_timeout(struct fusb300
*fusb300
,
1465 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1466 reg
&= ~(0xff << 8);
1467 reg
|= FUSB300_SSCR2_U1TIMEOUT(time
);
1469 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1472 static void init_controller(struct fusb300
*fusb300
)
1479 mask
= val
= FUSB300_AHBBCR_S0_SPLIT_ON
| FUSB300_AHBBCR_S1_SPLIT_ON
;
1480 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1483 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1485 /* enable high-speed LPM */
1486 mask
= val
= FUSB300_HSCR_HS_LPM_PERMIT
;
1487 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1490 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1492 /*set u1 u2 timmer*/
1493 fusb300_set_u2_timeout(fusb300
, 0xff);
1494 fusb300_set_u1_timeout(fusb300
, 0xff);
1496 /* enable all grp1 interrupt */
1497 iowrite32(0xcfffff9f, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1499 /*------------------------------------------------------------------------*/
1500 static struct fusb300
*the_controller
;
1502 int usb_gadget_probe_driver(struct usb_gadget_driver
*driver
,
1503 int (*bind
)(struct usb_gadget
*))
1505 struct fusb300
*fusb300
= the_controller
;
1509 || driver
->speed
< USB_SPEED_FULL
1517 if (fusb300
->driver
)
1520 /* hook up the driver */
1521 driver
->driver
.bus
= NULL
;
1522 fusb300
->driver
= driver
;
1523 fusb300
->gadget
.dev
.driver
= &driver
->driver
;
1525 retval
= device_add(&fusb300
->gadget
.dev
);
1527 pr_err("device_add error (%d)\n", retval
);
1531 retval
= bind(&fusb300
->gadget
);
1533 pr_err("bind to driver error (%d)\n", retval
);
1534 device_del(&fusb300
->gadget
.dev
);
1541 fusb300
->driver
= NULL
;
1542 fusb300
->gadget
.dev
.driver
= NULL
;
1546 EXPORT_SYMBOL(usb_gadget_probe_driver
);
1548 int usb_gadget_unregister_driver(struct usb_gadget_driver
*driver
)
1550 struct fusb300
*fusb300
= the_controller
;
1552 if (driver
!= fusb300
->driver
|| !driver
->unbind
)
1555 driver
->unbind(&fusb300
->gadget
);
1556 fusb300
->gadget
.dev
.driver
= NULL
;
1558 init_controller(fusb300
);
1559 device_del(&fusb300
->gadget
.dev
);
1560 fusb300
->driver
= NULL
;
1564 EXPORT_SYMBOL(usb_gadget_unregister_driver
);
1565 /*--------------------------------------------------------------------------*/
1567 static int fusb300_udc_pullup(struct usb_gadget
*_gadget
, int is_active
)
1572 static struct usb_gadget_ops fusb300_gadget_ops
= {
1573 .pullup
= fusb300_udc_pullup
,
1576 static int __exit
fusb300_remove(struct platform_device
*pdev
)
1578 struct fusb300
*fusb300
= dev_get_drvdata(&pdev
->dev
);
1580 iounmap(fusb300
->reg
);
1581 free_irq(platform_get_irq(pdev
, 0), fusb300
);
1583 fusb300_free_request(&fusb300
->ep
[0]->ep
, fusb300
->ep0_req
);
1589 static int __init
fusb300_probe(struct platform_device
*pdev
)
1591 struct resource
*res
, *ires
, *ires1
;
1592 void __iomem
*reg
= NULL
;
1593 struct fusb300
*fusb300
= NULL
;
1594 struct fusb300_ep
*_ep
[FUSB300_MAX_NUM_EP
];
1598 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1601 pr_err("platform_get_resource error.\n");
1605 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1609 "platform_get_resource IORESOURCE_IRQ error.\n");
1613 ires1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 1);
1617 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1621 reg
= ioremap(res
->start
, resource_size(res
));
1624 pr_err("ioremap error.\n");
1628 /* initialize udc */
1629 fusb300
= kzalloc(sizeof(struct fusb300
), GFP_KERNEL
);
1630 if (fusb300
== NULL
) {
1631 pr_err("kzalloc error\n");
1635 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1636 _ep
[i
] = kzalloc(sizeof(struct fusb300_ep
), GFP_KERNEL
);
1637 if (_ep
[i
] == NULL
) {
1638 pr_err("_ep kzalloc error\n");
1641 fusb300
->ep
[i
] = _ep
[i
];
1644 spin_lock_init(&fusb300
->lock
);
1646 dev_set_drvdata(&pdev
->dev
, fusb300
);
1648 fusb300
->gadget
.ops
= &fusb300_gadget_ops
;
1650 device_initialize(&fusb300
->gadget
.dev
);
1652 dev_set_name(&fusb300
->gadget
.dev
, "gadget");
1654 fusb300
->gadget
.is_dualspeed
= 1;
1655 fusb300
->gadget
.dev
.parent
= &pdev
->dev
;
1656 fusb300
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1657 fusb300
->gadget
.dev
.release
= pdev
->dev
.release
;
1658 fusb300
->gadget
.name
= udc_name
;
1661 ret
= request_irq(ires
->start
, fusb300_irq
, IRQF_DISABLED
| IRQF_SHARED
,
1664 pr_err("request_irq error (%d)\n", ret
);
1668 ret
= request_irq(ires1
->start
, fusb300_irq
,
1669 IRQF_DISABLED
| IRQF_SHARED
, udc_name
, fusb300
);
1671 pr_err("request_irq1 error (%d)\n", ret
);
1675 INIT_LIST_HEAD(&fusb300
->gadget
.ep_list
);
1677 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1678 struct fusb300_ep
*ep
= fusb300
->ep
[i
];
1681 INIT_LIST_HEAD(&fusb300
->ep
[i
]->ep
.ep_list
);
1682 list_add_tail(&fusb300
->ep
[i
]->ep
.ep_list
,
1683 &fusb300
->gadget
.ep_list
);
1685 ep
->fusb300
= fusb300
;
1686 INIT_LIST_HEAD(&ep
->queue
);
1687 ep
->ep
.name
= fusb300_ep_name
[i
];
1688 ep
->ep
.ops
= &fusb300_ep_ops
;
1689 ep
->ep
.maxpacket
= HS_BULK_MAX_PACKET_SIZE
;
1691 fusb300
->ep
[0]->ep
.maxpacket
= HS_CTL_MAX_PACKET_SIZE
;
1692 fusb300
->ep
[0]->epnum
= 0;
1693 fusb300
->gadget
.ep0
= &fusb300
->ep
[0]->ep
;
1694 INIT_LIST_HEAD(&fusb300
->gadget
.ep0
->ep_list
);
1696 the_controller
= fusb300
;
1698 fusb300
->ep0_req
= fusb300_alloc_request(&fusb300
->ep
[0]->ep
,
1700 if (fusb300
->ep0_req
== NULL
)
1703 init_controller(fusb300
);
1704 dev_info(&pdev
->dev
, "version %s\n", DRIVER_VERSION
);
1709 free_irq(ires
->start
, fusb300
);
1713 if (fusb300
->ep0_req
)
1714 fusb300_free_request(&fusb300
->ep
[0]->ep
,
1724 static struct platform_driver fusb300_driver
= {
1725 .remove
= __exit_p(fusb300_remove
),
1727 .name
= (char *) udc_name
,
1728 .owner
= THIS_MODULE
,
1732 static int __init
fusb300_udc_init(void)
1734 return platform_driver_probe(&fusb300_driver
, fusb300_probe
);
1737 module_init(fusb300_udc_init
);
1739 static void __exit
fusb300_udc_cleanup(void)
1741 platform_driver_unregister(&fusb300_driver
);
1743 module_exit(fusb300_udc_cleanup
);