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", "ep8", "ep9",
42 "ep10", "ep11", "ep12", "ep13", "ep14", "ep15"
45 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
48 static void fusb300_enable_bit(struct fusb300
*fusb300
, u32 offset
,
51 u32 reg
= ioread32(fusb300
->reg
+ offset
);
54 iowrite32(reg
, fusb300
->reg
+ offset
);
57 static void fusb300_disable_bit(struct fusb300
*fusb300
, u32 offset
,
60 u32 reg
= ioread32(fusb300
->reg
+ offset
);
63 iowrite32(reg
, fusb300
->reg
+ offset
);
67 static void fusb300_ep_setting(struct fusb300_ep
*ep
,
68 struct fusb300_ep_info info
)
70 ep
->epnum
= info
.epnum
;
74 static int fusb300_ep_release(struct fusb300_ep
*ep
)
84 static void fusb300_set_fifo_entry(struct fusb300
*fusb300
,
87 u32 val
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
89 val
&= ~FUSB300_EPSET1_FIFOENTRY_MSK
;
90 val
|= FUSB300_EPSET1_FIFOENTRY(FUSB300_FIFO_ENTRY_NUM
);
91 iowrite32(val
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
94 static void fusb300_set_start_entry(struct fusb300
*fusb300
,
97 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
98 u32 start_entry
= fusb300
->fifo_entry_num
* FUSB300_FIFO_ENTRY_NUM
;
100 reg
&= ~FUSB300_EPSET1_START_ENTRY_MSK
;
101 reg
|= FUSB300_EPSET1_START_ENTRY(start_entry
);
102 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
103 if (fusb300
->fifo_entry_num
== FUSB300_MAX_FIFO_ENTRY
) {
104 fusb300
->fifo_entry_num
= 0;
105 fusb300
->addrofs
= 0;
106 pr_err("fifo entry is over the maximum number!\n");
108 fusb300
->fifo_entry_num
++;
111 /* set fusb300_set_start_entry first before fusb300_set_epaddrofs */
112 static void fusb300_set_epaddrofs(struct fusb300
*fusb300
,
113 struct fusb300_ep_info info
)
115 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
117 reg
&= ~FUSB300_EPSET2_ADDROFS_MSK
;
118 reg
|= FUSB300_EPSET2_ADDROFS(fusb300
->addrofs
);
119 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
120 fusb300
->addrofs
+= (info
.maxpacket
+ 7) / 8 * FUSB300_FIFO_ENTRY_NUM
;
123 static void ep_fifo_setting(struct fusb300
*fusb300
,
124 struct fusb300_ep_info info
)
126 fusb300_set_fifo_entry(fusb300
, info
.epnum
);
127 fusb300_set_start_entry(fusb300
, info
.epnum
);
128 fusb300_set_epaddrofs(fusb300
, info
);
131 static void fusb300_set_eptype(struct fusb300
*fusb300
,
132 struct fusb300_ep_info info
)
134 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
136 reg
&= ~FUSB300_EPSET1_TYPE_MSK
;
137 reg
|= FUSB300_EPSET1_TYPE(info
.type
);
138 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
141 static void fusb300_set_epdir(struct fusb300
*fusb300
,
142 struct fusb300_ep_info info
)
148 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
149 reg
&= ~FUSB300_EPSET1_DIR_MSK
;
150 reg
|= FUSB300_EPSET1_DIRIN
;
151 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
154 static void fusb300_set_ep_active(struct fusb300
*fusb300
,
157 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
159 reg
|= FUSB300_EPSET1_ACTEN
;
160 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(ep
));
163 static void fusb300_set_epmps(struct fusb300
*fusb300
,
164 struct fusb300_ep_info info
)
166 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
168 reg
&= ~FUSB300_EPSET2_MPS_MSK
;
169 reg
|= FUSB300_EPSET2_MPS(info
.maxpacket
);
170 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET2(info
.epnum
));
173 static void fusb300_set_interval(struct fusb300
*fusb300
,
174 struct fusb300_ep_info info
)
176 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
178 reg
&= ~FUSB300_EPSET1_INTERVAL(0x7);
179 reg
|= FUSB300_EPSET1_INTERVAL(info
.interval
);
180 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
183 static void fusb300_set_bwnum(struct fusb300
*fusb300
,
184 struct fusb300_ep_info info
)
186 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
188 reg
&= ~FUSB300_EPSET1_BWNUM(0x3);
189 reg
|= FUSB300_EPSET1_BWNUM(info
.bw_num
);
190 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET1(info
.epnum
));
193 static void set_ep_reg(struct fusb300
*fusb300
,
194 struct fusb300_ep_info info
)
196 fusb300_set_eptype(fusb300
, info
);
197 fusb300_set_epdir(fusb300
, info
);
198 fusb300_set_epmps(fusb300
, info
);
201 fusb300_set_interval(fusb300
, info
);
204 fusb300_set_bwnum(fusb300
, info
);
206 fusb300_set_ep_active(fusb300
, info
.epnum
);
209 static int config_ep(struct fusb300_ep
*ep
,
210 const struct usb_endpoint_descriptor
*desc
)
212 struct fusb300
*fusb300
= ep
->fusb300
;
213 struct fusb300_ep_info info
;
221 info
.type
= desc
->bmAttributes
& USB_ENDPOINT_XFERTYPE_MASK
;
222 info
.dir_in
= (desc
->bEndpointAddress
& USB_ENDPOINT_DIR_MASK
) ? 1 : 0;
223 info
.maxpacket
= le16_to_cpu(desc
->wMaxPacketSize
);
224 info
.epnum
= desc
->bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK
;
226 if ((info
.type
== USB_ENDPOINT_XFER_INT
) ||
227 (info
.type
== USB_ENDPOINT_XFER_ISOC
)) {
228 info
.interval
= desc
->bInterval
;
229 if (info
.type
== USB_ENDPOINT_XFER_ISOC
)
230 info
.bw_num
= ((desc
->wMaxPacketSize
& 0x1800) >> 11);
233 ep_fifo_setting(fusb300
, info
);
235 set_ep_reg(fusb300
, info
);
237 fusb300_ep_setting(ep
, info
);
239 fusb300
->ep
[info
.epnum
] = ep
;
244 static int fusb300_enable(struct usb_ep
*_ep
,
245 const struct usb_endpoint_descriptor
*desc
)
247 struct fusb300_ep
*ep
;
249 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
251 if (ep
->fusb300
->reenum
) {
252 ep
->fusb300
->fifo_entry_num
= 0;
253 ep
->fusb300
->addrofs
= 0;
254 ep
->fusb300
->reenum
= 0;
257 return config_ep(ep
, desc
);
260 static int fusb300_disable(struct usb_ep
*_ep
)
262 struct fusb300_ep
*ep
;
263 struct fusb300_request
*req
;
266 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
270 while (!list_empty(&ep
->queue
)) {
271 req
= list_entry(ep
->queue
.next
, struct fusb300_request
, queue
);
272 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
273 done(ep
, req
, -ECONNRESET
);
274 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
277 return fusb300_ep_release(ep
);
280 static struct usb_request
*fusb300_alloc_request(struct usb_ep
*_ep
,
283 struct fusb300_request
*req
;
285 req
= kzalloc(sizeof(struct fusb300_request
), gfp_flags
);
288 INIT_LIST_HEAD(&req
->queue
);
293 static void fusb300_free_request(struct usb_ep
*_ep
, struct usb_request
*_req
)
295 struct fusb300_request
*req
;
297 req
= container_of(_req
, struct fusb300_request
, req
);
301 static int enable_fifo_int(struct fusb300_ep
*ep
)
303 struct fusb300
*fusb300
= ep
->fusb300
;
306 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
307 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
309 pr_err("can't enable_fifo_int ep0\n");
316 static int disable_fifo_int(struct fusb300_ep
*ep
)
318 struct fusb300
*fusb300
= ep
->fusb300
;
321 fusb300_disable_bit(fusb300
, FUSB300_OFFSET_IGER0
,
322 FUSB300_IGER0_EEPn_FIFO_INT(ep
->epnum
));
324 pr_err("can't disable_fifo_int ep0\n");
331 static void fusb300_set_cxlen(struct fusb300
*fusb300
, u32 length
)
335 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
336 reg
&= ~FUSB300_CSR_LEN_MSK
;
337 reg
|= FUSB300_CSR_LEN(length
);
338 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_CSR
);
341 /* write data to cx fifo */
342 static void fusb300_wrcxf(struct fusb300_ep
*ep
,
343 struct fusb300_request
*req
)
348 struct fusb300
*fusb300
= ep
->fusb300
;
349 u32 length
= req
->req
.length
- req
->req
.actual
;
351 tmp
= req
->req
.buf
+ req
->req
.actual
;
353 if (length
> SS_CTL_MAX_PACKET_SIZE
) {
354 fusb300_set_cxlen(fusb300
, SS_CTL_MAX_PACKET_SIZE
);
355 for (i
= (SS_CTL_MAX_PACKET_SIZE
>> 2); i
> 0; i
--) {
356 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
358 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
361 req
->req
.actual
+= SS_CTL_MAX_PACKET_SIZE
;
362 } else { /* length is less than max packet size */
363 fusb300_set_cxlen(fusb300
, length
);
364 for (i
= length
>> 2; i
> 0; i
--) {
365 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16 |
367 printk(KERN_DEBUG
" 0x%x\n", data
);
368 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
371 switch (length
% 4) {
374 printk(KERN_DEBUG
" 0x%x\n", data
);
375 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
378 data
= *tmp
| *(tmp
+ 1) << 8;
379 printk(KERN_DEBUG
" 0x%x\n", data
);
380 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
383 data
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16;
384 printk(KERN_DEBUG
" 0x%x\n", data
);
385 iowrite32(data
, fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
390 req
->req
.actual
+= length
;
394 static void fusb300_set_epnstall(struct fusb300
*fusb300
, u8 ep
)
396 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
400 static void fusb300_clear_epnstall(struct fusb300
*fusb300
, u8 ep
)
402 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
404 if (reg
& FUSB300_EPSET0_STL
) {
405 printk(KERN_DEBUG
"EP%d stall... Clear!!\n", ep
);
406 reg
&= ~FUSB300_EPSET0_STL
;
407 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
411 static void ep0_queue(struct fusb300_ep
*ep
, struct fusb300_request
*req
)
413 if (ep
->fusb300
->ep0_dir
) { /* if IN */
414 if (req
->req
.length
) {
415 fusb300_wrcxf(ep
, req
);
417 printk(KERN_DEBUG
"%s : req->req.length = 0x%x\n",
418 __func__
, req
->req
.length
);
419 if ((req
->req
.length
== req
->req
.actual
) ||
420 (req
->req
.actual
< ep
->ep
.maxpacket
))
423 if (!req
->req
.length
)
426 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER1
,
427 FUSB300_IGER1_CX_OUT_INT
);
431 static int fusb300_queue(struct usb_ep
*_ep
, struct usb_request
*_req
,
434 struct fusb300_ep
*ep
;
435 struct fusb300_request
*req
;
439 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
440 req
= container_of(_req
, struct fusb300_request
, req
);
442 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
445 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
447 if (list_empty(&ep
->queue
))
450 list_add_tail(&req
->queue
, &ep
->queue
);
453 req
->req
.status
= -EINPROGRESS
;
455 if (ep
->desc
== NULL
) /* ep0 */
457 else if (request
&& !ep
->stall
)
460 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
465 static int fusb300_dequeue(struct usb_ep
*_ep
, struct usb_request
*_req
)
467 struct fusb300_ep
*ep
;
468 struct fusb300_request
*req
;
471 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
472 req
= container_of(_req
, struct fusb300_request
, req
);
474 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
475 if (!list_empty(&ep
->queue
))
476 done(ep
, req
, -ECONNRESET
);
477 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
482 static int fusb300_set_halt_and_wedge(struct usb_ep
*_ep
, int value
, int wedge
)
484 struct fusb300_ep
*ep
;
485 struct fusb300
*fusb300
;
489 ep
= container_of(_ep
, struct fusb300_ep
, ep
);
491 fusb300
= ep
->fusb300
;
493 spin_lock_irqsave(&ep
->fusb300
->lock
, flags
);
495 if (!list_empty(&ep
->queue
)) {
501 fusb300_set_epnstall(fusb300
, ep
->epnum
);
506 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
512 spin_unlock_irqrestore(&ep
->fusb300
->lock
, flags
);
516 static int fusb300_set_halt(struct usb_ep
*_ep
, int value
)
518 return fusb300_set_halt_and_wedge(_ep
, value
, 0);
521 static int fusb300_set_wedge(struct usb_ep
*_ep
)
523 return fusb300_set_halt_and_wedge(_ep
, 1, 1);
526 static void fusb300_fifo_flush(struct usb_ep
*_ep
)
530 static struct usb_ep_ops fusb300_ep_ops
= {
531 .enable
= fusb300_enable
,
532 .disable
= fusb300_disable
,
534 .alloc_request
= fusb300_alloc_request
,
535 .free_request
= fusb300_free_request
,
537 .queue
= fusb300_queue
,
538 .dequeue
= fusb300_dequeue
,
540 .set_halt
= fusb300_set_halt
,
541 .fifo_flush
= fusb300_fifo_flush
,
542 .set_wedge
= fusb300_set_wedge
,
545 /*****************************************************************************/
546 static void fusb300_clear_int(struct fusb300
*fusb300
, u32 offset
,
549 iowrite32(value
, fusb300
->reg
+ offset
);
552 static void fusb300_reset(void)
556 static void fusb300_set_cxstall(struct fusb300
*fusb300
)
558 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
562 static void fusb300_set_cxdone(struct fusb300
*fusb300
)
564 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_CSR
,
568 /* read data from cx fifo */
569 void fusb300_rdcxf(struct fusb300
*fusb300
,
570 u8
*buffer
, u32 length
)
578 for (i
= (length
>> 2); i
> 0; i
--) {
579 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
580 printk(KERN_DEBUG
" 0x%x\n", data
);
582 *(tmp
+ 1) = (data
>> 8) & 0xFF;
583 *(tmp
+ 2) = (data
>> 16) & 0xFF;
584 *(tmp
+ 3) = (data
>> 24) & 0xFF;
588 switch (length
% 4) {
590 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
591 printk(KERN_DEBUG
" 0x%x\n", data
);
595 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
596 printk(KERN_DEBUG
" 0x%x\n", data
);
598 *(tmp
+ 1) = (data
>> 8) & 0xFF;
601 data
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CXPORT
);
602 printk(KERN_DEBUG
" 0x%x\n", data
);
604 *(tmp
+ 1) = (data
>> 8) & 0xFF;
605 *(tmp
+ 2) = (data
>> 16) & 0xFF;
613 static void fusb300_dbg_fifo(struct fusb300_ep
*ep
,
614 u8 entry
, u16 length
)
620 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
621 reg
&= ~(FUSB300_GTM_TST_EP_ENTRY(0xF) |
622 FUSB300_GTM_TST_EP_NUM(0xF) | FUSB300_GTM_TST_FIFO_DEG
);
623 reg
|= (FUSB300_GTM_TST_EP_ENTRY(entry
) |
624 FUSB300_GTM_TST_EP_NUM(ep
->epnum
) | FUSB300_GTM_TST_FIFO_DEG
);
625 iowrite32(reg
, ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
627 for (i
= 0; i
< (length
>> 2); i
++) {
630 reg
= ioread32(ep
->fusb300
->reg
+
631 FUSB300_OFFSET_BUFDBG_START
+ i
* 4);
632 printk(KERN_DEBUG
" 0x%-8x", reg
);
635 printk(KERN_DEBUG
"\n");
639 reg
= ioread32(ep
->fusb300
->reg
+
640 FUSB300_OFFSET_BUFDBG_START
+ i
* 4);
641 printk(KERN_DEBUG
" 0x%x\n", reg
);
645 printk(KERN_DEBUG
"\n");
647 fusb300_disable_bit(ep
->fusb300
, FUSB300_OFFSET_GTM
,
648 FUSB300_GTM_TST_FIFO_DEG
);
651 static void fusb300_cmp_dbg_fifo(struct fusb300_ep
*ep
,
652 u8 entry
, u16 length
, u8
*golden
)
661 printk(KERN_DEBUG
"fusb300_cmp_dbg_fifo (entry %d) : start\n", entry
);
663 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
664 reg
&= ~(FUSB300_GTM_TST_EP_ENTRY(0xF) |
665 FUSB300_GTM_TST_EP_NUM(0xF) | FUSB300_GTM_TST_FIFO_DEG
);
666 reg
|= (FUSB300_GTM_TST_EP_ENTRY(entry
) |
667 FUSB300_GTM_TST_EP_NUM(ep
->epnum
) | FUSB300_GTM_TST_FIFO_DEG
);
668 iowrite32(reg
, ep
->fusb300
->reg
+ FUSB300_OFFSET_GTM
);
670 for (i
= 0; i
< (length
>> 2); i
++) {
673 golden_value
= *tmp
| *(tmp
+ 1) << 8 |
674 *(tmp
+ 2) << 16 | *(tmp
+ 3) << 24;
676 reg
= ioread32(ep
->fusb300
->reg
+
677 FUSB300_OFFSET_BUFDBG_START
+ i
*4);
679 if (reg
!= golden_value
) {
680 printk(KERN_DEBUG
"0x%x : ", (u32
)(ep
->fusb300
->reg
+
681 FUSB300_OFFSET_BUFDBG_START
+ i
*4));
682 printk(KERN_DEBUG
" golden = 0x%x, reg = 0x%x\n",
688 switch (length
% 4) {
692 golden_value
= *tmp
| *(tmp
+ 1) << 8;
694 golden_value
= *tmp
| *(tmp
+ 1) << 8 | *(tmp
+ 2) << 16;
698 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_BUFDBG_START
+ i
*4);
699 if (reg
!= golden_value
) {
700 printk(KERN_DEBUG
"0x%x:", (u32
)(ep
->fusb300
->reg
+
701 FUSB300_OFFSET_BUFDBG_START
+ i
*4));
702 printk(KERN_DEBUG
" golden = 0x%x, reg = 0x%x\n",
707 printk(KERN_DEBUG
"fusb300_cmp_dbg_fifo : end\n");
708 fusb300_disable_bit(ep
->fusb300
, FUSB300_OFFSET_GTM
,
709 FUSB300_GTM_TST_FIFO_DEG
);
713 static void fusb300_rdfifo(struct fusb300_ep
*ep
,
714 struct fusb300_request
*req
,
720 struct fusb300
*fusb300
= ep
->fusb300
;
722 tmp
= req
->req
.buf
+ req
->req
.actual
;
723 req
->req
.actual
+= length
;
725 if (req
->req
.actual
> req
->req
.length
)
726 printk(KERN_DEBUG
"req->req.actual > req->req.length\n");
728 for (i
= (length
>> 2); i
> 0; i
--) {
729 data
= ioread32(fusb300
->reg
+
730 FUSB300_OFFSET_EPPORT(ep
->epnum
));
732 *(tmp
+ 1) = (data
>> 8) & 0xFF;
733 *(tmp
+ 2) = (data
>> 16) & 0xFF;
734 *(tmp
+ 3) = (data
>> 24) & 0xFF;
738 switch (length
% 4) {
740 data
= ioread32(fusb300
->reg
+
741 FUSB300_OFFSET_EPPORT(ep
->epnum
));
745 data
= ioread32(fusb300
->reg
+
746 FUSB300_OFFSET_EPPORT(ep
->epnum
));
748 *(tmp
+ 1) = (data
>> 8) & 0xFF;
751 data
= ioread32(fusb300
->reg
+
752 FUSB300_OFFSET_EPPORT(ep
->epnum
));
754 *(tmp
+ 1) = (data
>> 8) & 0xFF;
755 *(tmp
+ 2) = (data
>> 16) & 0xFF;
762 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
763 reg
&= FUSB300_IGR1_SYNF0_EMPTY_INT
;
765 printk(KERN_INFO
"sync fifo is not empty!\n");
770 static u8
fusb300_get_epnstall(struct fusb300
*fusb300
, u8 ep
)
773 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPSET0(ep
));
775 value
= reg
& FUSB300_EPSET0_STL
;
780 static u8
fusb300_get_cxstall(struct fusb300
*fusb300
)
783 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_CSR
);
785 value
= (reg
& FUSB300_CSR_STL
) >> 1;
790 static void request_error(struct fusb300
*fusb300
)
792 fusb300_set_cxstall(fusb300
);
793 printk(KERN_DEBUG
"request error!!\n");
796 static void get_status(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
797 __releases(fusb300
->lock
)
798 __acquires(fusb300
->lock
)
802 u16 w_index
= ctrl
->wIndex
;
804 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
805 case USB_RECIP_DEVICE
:
806 status
= 1 << USB_DEVICE_SELF_POWERED
;
808 case USB_RECIP_INTERFACE
:
811 case USB_RECIP_ENDPOINT
:
812 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
814 if (fusb300_get_epnstall(fusb300
, ep
))
815 status
= 1 << USB_ENDPOINT_HALT
;
817 if (fusb300_get_cxstall(fusb300
))
823 request_error(fusb300
);
827 fusb300
->ep0_data
= cpu_to_le16(status
);
828 fusb300
->ep0_req
->buf
= &fusb300
->ep0_data
;
829 fusb300
->ep0_req
->length
= 2;
831 spin_unlock(&fusb300
->lock
);
832 fusb300_queue(fusb300
->gadget
.ep0
, fusb300
->ep0_req
, GFP_KERNEL
);
833 spin_lock(&fusb300
->lock
);
836 static void set_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
840 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
841 case USB_RECIP_DEVICE
:
842 fusb300_set_cxdone(fusb300
);
844 case USB_RECIP_INTERFACE
:
845 fusb300_set_cxdone(fusb300
);
847 case USB_RECIP_ENDPOINT
: {
848 u16 w_index
= le16_to_cpu(ctrl
->wIndex
);
850 ep
= w_index
& USB_ENDPOINT_NUMBER_MASK
;
852 fusb300_set_epnstall(fusb300
, ep
);
854 fusb300_set_cxstall(fusb300
);
855 fusb300_set_cxdone(fusb300
);
859 request_error(fusb300
);
864 static void fusb300_clear_seqnum(struct fusb300
*fusb300
, u8 ep
)
866 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_EPSET0(ep
),
867 FUSB300_EPSET0_CLRSEQNUM
);
870 static void clear_feature(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
872 struct fusb300_ep
*ep
=
873 fusb300
->ep
[ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
];
875 switch (ctrl
->bRequestType
& USB_RECIP_MASK
) {
876 case USB_RECIP_DEVICE
:
877 fusb300_set_cxdone(fusb300
);
879 case USB_RECIP_INTERFACE
:
880 fusb300_set_cxdone(fusb300
);
882 case USB_RECIP_ENDPOINT
:
883 if (ctrl
->wIndex
& USB_ENDPOINT_NUMBER_MASK
) {
885 fusb300_set_cxdone(fusb300
);
890 fusb300_clear_seqnum(fusb300
, ep
->epnum
);
891 fusb300_clear_epnstall(fusb300
, ep
->epnum
);
892 if (!list_empty(&ep
->queue
))
896 fusb300_set_cxdone(fusb300
);
899 request_error(fusb300
);
904 static void fusb300_set_dev_addr(struct fusb300
*fusb300
, u16 addr
)
906 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_DAR
);
908 reg
&= ~FUSB300_DAR_DRVADDR_MSK
;
909 reg
|= FUSB300_DAR_DRVADDR(addr
);
911 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_DAR
);
914 static void set_address(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
916 if (ctrl
->wValue
>= 0x0100)
917 request_error(fusb300
);
919 fusb300_set_dev_addr(fusb300
, ctrl
->wValue
);
920 fusb300_set_cxdone(fusb300
);
924 #define UVC_COPY_DESCRIPTORS(mem, src) \
926 const struct usb_descriptor_header * const *__src; \
927 for (__src = src; *__src; ++__src) { \
928 memcpy(mem, *__src, (*__src)->bLength); \
929 mem += (*__src)->bLength; \
933 static int setup_packet(struct fusb300
*fusb300
, struct usb_ctrlrequest
*ctrl
)
939 fusb300_rdcxf(fusb300
, p
, 8);
940 fusb300
->ep0_dir
= ctrl
->bRequestType
& USB_DIR_IN
;
941 fusb300
->ep0_length
= ctrl
->wLength
;
944 if ((ctrl
->bRequestType
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
945 switch (ctrl
->bRequest
) {
946 case USB_REQ_GET_STATUS
:
947 get_status(fusb300
, ctrl
);
949 case USB_REQ_CLEAR_FEATURE
:
950 clear_feature(fusb300
, ctrl
);
952 case USB_REQ_SET_FEATURE
:
953 set_feature(fusb300
, ctrl
);
955 case USB_REQ_SET_ADDRESS
:
956 set_address(fusb300
, ctrl
);
958 case USB_REQ_SET_CONFIGURATION
:
959 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_DAR
,
960 FUSB300_DAR_SETCONFG
);
961 /* clear sequence number */
962 for (i
= 1; i
<= FUSB300_MAX_NUM_EP
; i
++)
963 fusb300_clear_seqnum(fusb300
, i
);
977 static void done(struct fusb300_ep
*ep
, struct fusb300_request
*req
,
980 list_del_init(&req
->queue
);
982 /* don't modify queue heads during completion callback */
983 if (ep
->fusb300
->gadget
.speed
== USB_SPEED_UNKNOWN
)
984 req
->req
.status
= -ESHUTDOWN
;
986 req
->req
.status
= status
;
988 spin_unlock(&ep
->fusb300
->lock
);
989 req
->req
.complete(&ep
->ep
, &req
->req
);
990 spin_lock(&ep
->fusb300
->lock
);
993 disable_fifo_int(ep
);
994 if (!list_empty(&ep
->queue
))
997 fusb300_set_cxdone(ep
->fusb300
);
1000 static void fusb300_fill_idma_prdtbl(struct fusb300_ep
*ep
, dma_addr_t d
,
1008 reg
= ioread32(ep
->fusb300
->reg
+
1009 FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
1010 reg
&= FUSB300_EPPRD0_H
;
1013 iowrite32(d
, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W1(ep
->epnum
));
1015 value
= FUSB300_EPPRD0_BTC(len
) | FUSB300_EPPRD0_H
|
1016 FUSB300_EPPRD0_F
| FUSB300_EPPRD0_L
| FUSB300_EPPRD0_I
;
1017 iowrite32(value
, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W0(ep
->epnum
));
1019 iowrite32(0x0, ep
->fusb300
->reg
+ FUSB300_OFFSET_EPPRD_W2(ep
->epnum
));
1021 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_EPPRDRDY
,
1022 FUSB300_EPPRDR_EP_PRD_RDY(ep
->epnum
));
1025 static void fusb300_wait_idma_finished(struct fusb300_ep
*ep
)
1030 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
1031 if ((reg
& FUSB300_IGR1_VBUS_CHG_INT
) ||
1032 (reg
& FUSB300_IGR1_WARM_RST_INT
) ||
1033 (reg
& FUSB300_IGR1_HOT_RST_INT
) ||
1034 (reg
& FUSB300_IGR1_USBRST_INT
)
1037 reg
= ioread32(ep
->fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
1038 reg
&= FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
);
1041 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGR0
,
1042 FUSB300_IGR0_EPn_PRD_INT(ep
->epnum
));
1044 fusb300_clear_int(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
1045 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
1048 static void fusb300_set_idma(struct fusb300_ep
*ep
,
1049 struct fusb300_request
*req
)
1053 d
= dma_map_single(NULL
, req
->req
.buf
, req
->req
.length
, DMA_TO_DEVICE
);
1055 if (dma_mapping_error(NULL
, d
)) {
1056 printk(KERN_DEBUG
"dma_mapping_error\n");
1060 dma_sync_single_for_device(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
1062 fusb300_enable_bit(ep
->fusb300
, FUSB300_OFFSET_IGER0
,
1063 FUSB300_IGER0_EEPn_PRD_INT(ep
->epnum
));
1065 fusb300_fill_idma_prdtbl(ep
, d
, req
->req
.length
);
1066 /* check idma is done */
1067 fusb300_wait_idma_finished(ep
);
1069 dma_unmap_single(NULL
, d
, req
->req
.length
, DMA_TO_DEVICE
);
1072 static void in_ep_fifo_handler(struct fusb300_ep
*ep
)
1074 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
1075 struct fusb300_request
, queue
);
1077 if (req
->req
.length
)
1078 fusb300_set_idma(ep
, req
);
1082 static void out_ep_fifo_handler(struct fusb300_ep
*ep
)
1084 struct fusb300
*fusb300
= ep
->fusb300
;
1085 struct fusb300_request
*req
= list_entry(ep
->queue
.next
,
1086 struct fusb300_request
, queue
);
1087 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_EPFFR(ep
->epnum
));
1088 u32 length
= reg
& FUSB300_FFR_BYCNT
;
1090 fusb300_rdfifo(ep
, req
, length
);
1092 /* finish out transfer */
1093 if ((req
->req
.length
== req
->req
.actual
) || (length
< ep
->ep
.maxpacket
))
1097 static void check_device_mode(struct fusb300
*fusb300
)
1099 u32 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_GCR
);
1101 switch (reg
& FUSB300_GCR_DEVEN_MSK
) {
1102 case FUSB300_GCR_DEVEN_SS
:
1103 fusb300
->gadget
.speed
= USB_SPEED_SUPER
;
1105 case FUSB300_GCR_DEVEN_HS
:
1106 fusb300
->gadget
.speed
= USB_SPEED_HIGH
;
1108 case FUSB300_GCR_DEVEN_FS
:
1109 fusb300
->gadget
.speed
= USB_SPEED_FULL
;
1112 fusb300
->gadget
.speed
= USB_SPEED_UNKNOWN
;
1115 printk(KERN_INFO
"dev_mode = %d\n", (reg
& FUSB300_GCR_DEVEN_MSK
));
1119 static void fusb300_ep0out(struct fusb300
*fusb300
)
1121 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1124 if (!list_empty(&ep
->queue
)) {
1125 struct fusb300_request
*req
;
1127 req
= list_first_entry(&ep
->queue
,
1128 struct fusb300_request
, queue
);
1129 if (req
->req
.length
)
1130 fusb300_rdcxf(ep
->fusb300
, req
->req
.buf
,
1133 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1134 reg
&= ~FUSB300_IGER1_CX_OUT_INT
;
1135 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1137 pr_err("%s : empty queue\n", __func__
);
1140 static void fusb300_ep0in(struct fusb300
*fusb300
)
1142 struct fusb300_request
*req
;
1143 struct fusb300_ep
*ep
= fusb300
->ep
[0];
1145 if ((!list_empty(&ep
->queue
)) && (fusb300
->ep0_dir
)) {
1146 req
= list_entry(ep
->queue
.next
,
1147 struct fusb300_request
, queue
);
1148 if (req
->req
.length
)
1149 fusb300_wrcxf(ep
, req
);
1150 if ((req
->req
.length
- req
->req
.actual
) < ep
->ep
.maxpacket
)
1153 fusb300_set_cxdone(fusb300
);
1156 static void fusb300_grp2_handler(void)
1160 static void fusb300_grp3_handler(void)
1164 static void fusb300_grp4_handler(void)
1168 static void fusb300_grp5_handler(void)
1172 static irqreturn_t
fusb300_irq(int irq
, void *_fusb300
)
1174 struct fusb300
*fusb300
= _fusb300
;
1175 u32 int_grp1
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR1
);
1176 u32 int_grp1_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1177 u32 int_grp0
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGR0
);
1178 u32 int_grp0_en
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_IGER0
);
1179 struct usb_ctrlrequest ctrl
;
1184 spin_lock(&fusb300
->lock
);
1186 int_grp1
&= int_grp1_en
;
1187 int_grp0
&= int_grp0_en
;
1189 if (int_grp1
& FUSB300_IGR1_WARM_RST_INT
) {
1190 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1191 FUSB300_IGR1_WARM_RST_INT
);
1192 printk(KERN_INFO
"fusb300_warmreset\n");
1196 if (int_grp1
& FUSB300_IGR1_HOT_RST_INT
) {
1197 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1198 FUSB300_IGR1_HOT_RST_INT
);
1199 printk(KERN_INFO
"fusb300_hotreset\n");
1203 if (int_grp1
& FUSB300_IGR1_USBRST_INT
) {
1204 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1205 FUSB300_IGR1_USBRST_INT
);
1208 /* COMABT_INT has a highest priority */
1210 if (int_grp1
& FUSB300_IGR1_CX_COMABT_INT
) {
1211 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1212 FUSB300_IGR1_CX_COMABT_INT
);
1213 printk(KERN_INFO
"fusb300_ep0abt\n");
1216 if (int_grp1
& FUSB300_IGR1_VBUS_CHG_INT
) {
1217 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1218 FUSB300_IGR1_VBUS_CHG_INT
);
1219 printk(KERN_INFO
"fusb300_vbus_change\n");
1222 if (int_grp1
& FUSB300_IGR1_U3_EXIT_FAIL_INT
) {
1223 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1224 FUSB300_IGR1_U3_EXIT_FAIL_INT
);
1227 if (int_grp1
& FUSB300_IGR1_U2_EXIT_FAIL_INT
) {
1228 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1229 FUSB300_IGR1_U2_EXIT_FAIL_INT
);
1232 if (int_grp1
& FUSB300_IGR1_U1_EXIT_FAIL_INT
) {
1233 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1234 FUSB300_IGR1_U1_EXIT_FAIL_INT
);
1237 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_FAIL_INT
) {
1238 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1239 FUSB300_IGR1_U2_ENTRY_FAIL_INT
);
1242 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_FAIL_INT
) {
1243 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1244 FUSB300_IGR1_U1_ENTRY_FAIL_INT
);
1247 if (int_grp1
& FUSB300_IGR1_U3_EXIT_INT
) {
1248 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1249 FUSB300_IGR1_U3_EXIT_INT
);
1250 printk(KERN_INFO
"FUSB300_IGR1_U3_EXIT_INT\n");
1253 if (int_grp1
& FUSB300_IGR1_U2_EXIT_INT
) {
1254 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1255 FUSB300_IGR1_U2_EXIT_INT
);
1256 printk(KERN_INFO
"FUSB300_IGR1_U2_EXIT_INT\n");
1259 if (int_grp1
& FUSB300_IGR1_U1_EXIT_INT
) {
1260 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1261 FUSB300_IGR1_U1_EXIT_INT
);
1262 printk(KERN_INFO
"FUSB300_IGR1_U1_EXIT_INT\n");
1265 if (int_grp1
& FUSB300_IGR1_U3_ENTRY_INT
) {
1266 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1267 FUSB300_IGR1_U3_ENTRY_INT
);
1268 printk(KERN_INFO
"FUSB300_IGR1_U3_ENTRY_INT\n");
1269 fusb300_enable_bit(fusb300
, FUSB300_OFFSET_SSCR1
,
1270 FUSB300_SSCR1_GO_U3_DONE
);
1273 if (int_grp1
& FUSB300_IGR1_U2_ENTRY_INT
) {
1274 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1275 FUSB300_IGR1_U2_ENTRY_INT
);
1276 printk(KERN_INFO
"FUSB300_IGR1_U2_ENTRY_INT\n");
1279 if (int_grp1
& FUSB300_IGR1_U1_ENTRY_INT
) {
1280 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1281 FUSB300_IGR1_U1_ENTRY_INT
);
1282 printk(KERN_INFO
"FUSB300_IGR1_U1_ENTRY_INT\n");
1285 if (int_grp1
& FUSB300_IGR1_RESM_INT
) {
1286 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1287 FUSB300_IGR1_RESM_INT
);
1288 printk(KERN_INFO
"fusb300_resume\n");
1291 if (int_grp1
& FUSB300_IGR1_SUSP_INT
) {
1292 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1293 FUSB300_IGR1_SUSP_INT
);
1294 printk(KERN_INFO
"fusb300_suspend\n");
1297 if (int_grp1
& FUSB300_IGR1_HS_LPM_INT
) {
1298 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1299 FUSB300_IGR1_HS_LPM_INT
);
1300 printk(KERN_INFO
"fusb300_HS_LPM_INT\n");
1303 if (int_grp1
& FUSB300_IGR1_DEV_MODE_CHG_INT
) {
1304 fusb300_clear_int(fusb300
, FUSB300_OFFSET_IGR1
,
1305 FUSB300_IGR1_DEV_MODE_CHG_INT
);
1306 check_device_mode(fusb300
);
1309 if (int_grp1
& FUSB300_IGR1_CX_COMFAIL_INT
) {
1310 fusb300_set_cxstall(fusb300
);
1311 printk(KERN_INFO
"fusb300_ep0fail\n");
1314 if (int_grp1
& FUSB300_IGR1_CX_SETUP_INT
) {
1315 printk(KERN_INFO
"fusb300_ep0setup\n");
1316 if (setup_packet(fusb300
, &ctrl
)) {
1317 spin_unlock(&fusb300
->lock
);
1318 if (fusb300
->driver
->setup(&fusb300
->gadget
, &ctrl
) < 0)
1319 fusb300_set_cxstall(fusb300
);
1320 spin_lock(&fusb300
->lock
);
1324 if (int_grp1
& FUSB300_IGR1_CX_CMDEND_INT
)
1325 printk(KERN_INFO
"fusb300_cmdend\n");
1328 if (int_grp1
& FUSB300_IGR1_CX_OUT_INT
) {
1329 printk(KERN_INFO
"fusb300_cxout\n");
1330 fusb300_ep0out(fusb300
);
1333 if (int_grp1
& FUSB300_IGR1_CX_IN_INT
) {
1334 printk(KERN_INFO
"fusb300_cxin\n");
1335 fusb300_ep0in(fusb300
);
1338 if (int_grp1
& FUSB300_IGR1_INTGRP5
)
1339 fusb300_grp5_handler();
1341 if (int_grp1
& FUSB300_IGR1_INTGRP4
)
1342 fusb300_grp4_handler();
1344 if (int_grp1
& FUSB300_IGR1_INTGRP3
)
1345 fusb300_grp3_handler();
1347 if (int_grp1
& FUSB300_IGR1_INTGRP2
)
1348 fusb300_grp2_handler();
1351 for (i
= 1; i
< FUSB300_MAX_NUM_EP
; i
++) {
1352 if (int_grp0
& FUSB300_IGR0_EPn_FIFO_INT(i
)) {
1353 reg
= ioread32(fusb300
->reg
+
1354 FUSB300_OFFSET_EPSET1(i
));
1355 in
= (reg
& FUSB300_EPSET1_DIRIN
) ? 1 : 0;
1357 in_ep_fifo_handler(fusb300
->ep
[i
]);
1359 out_ep_fifo_handler(fusb300
->ep
[i
]);
1364 spin_unlock(&fusb300
->lock
);
1369 static void fusb300_set_u2_timeout(struct fusb300
*fusb300
,
1374 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1376 reg
|= FUSB300_SSCR2_U2TIMEOUT(time
);
1378 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1381 static void fusb300_set_u1_timeout(struct fusb300
*fusb300
,
1386 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_TT
);
1387 reg
&= ~(0xff << 8);
1388 reg
|= FUSB300_SSCR2_U1TIMEOUT(time
);
1390 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_TT
);
1393 static void init_controller(struct fusb300
*fusb300
)
1400 mask
= val
= FUSB300_AHBBCR_S0_SPLIT_ON
| FUSB300_AHBBCR_S1_SPLIT_ON
;
1401 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1404 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_AHBCR
);
1406 /* enable high-speed LPM */
1407 mask
= val
= FUSB300_HSCR_HS_LPM_PERMIT
;
1408 reg
= ioread32(fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1411 iowrite32(reg
, fusb300
->reg
+ FUSB300_OFFSET_HSCR
);
1413 /*set u1 u2 timmer*/
1414 fusb300_set_u2_timeout(fusb300
, 0xff);
1415 fusb300_set_u1_timeout(fusb300
, 0xff);
1417 /* enable all grp1 interrupt */
1418 iowrite32(0xcfffff9f, fusb300
->reg
+ FUSB300_OFFSET_IGER1
);
1420 /*------------------------------------------------------------------------*/
1421 static struct fusb300
*the_controller
;
1423 static int fusb300_udc_start(struct usb_gadget_driver
*driver
,
1424 int (*bind
)(struct usb_gadget
*))
1426 struct fusb300
*fusb300
= the_controller
;
1430 || driver
->speed
< USB_SPEED_FULL
1438 if (fusb300
->driver
)
1441 /* hook up the driver */
1442 driver
->driver
.bus
= NULL
;
1443 fusb300
->driver
= driver
;
1444 fusb300
->gadget
.dev
.driver
= &driver
->driver
;
1446 retval
= device_add(&fusb300
->gadget
.dev
);
1448 pr_err("device_add error (%d)\n", retval
);
1452 retval
= bind(&fusb300
->gadget
);
1454 pr_err("bind to driver error (%d)\n", retval
);
1455 device_del(&fusb300
->gadget
.dev
);
1462 fusb300
->driver
= NULL
;
1463 fusb300
->gadget
.dev
.driver
= NULL
;
1468 static int fusb300_udc_stop(struct usb_gadget_driver
*driver
)
1470 struct fusb300
*fusb300
= the_controller
;
1472 if (driver
!= fusb300
->driver
|| !driver
->unbind
)
1475 driver
->unbind(&fusb300
->gadget
);
1476 fusb300
->gadget
.dev
.driver
= NULL
;
1478 init_controller(fusb300
);
1479 device_del(&fusb300
->gadget
.dev
);
1480 fusb300
->driver
= NULL
;
1484 /*--------------------------------------------------------------------------*/
1486 static int fusb300_udc_pullup(struct usb_gadget
*_gadget
, int is_active
)
1491 static struct usb_gadget_ops fusb300_gadget_ops
= {
1492 .pullup
= fusb300_udc_pullup
,
1493 .start
= fusb300_udc_start
,
1494 .stop
= fusb300_udc_stop
,
1497 static int __exit
fusb300_remove(struct platform_device
*pdev
)
1499 struct fusb300
*fusb300
= dev_get_drvdata(&pdev
->dev
);
1501 usb_del_gadget_udc(&fusb300
->gadget
);
1502 iounmap(fusb300
->reg
);
1503 free_irq(platform_get_irq(pdev
, 0), fusb300
);
1505 fusb300_free_request(&fusb300
->ep
[0]->ep
, fusb300
->ep0_req
);
1511 static int __init
fusb300_probe(struct platform_device
*pdev
)
1513 struct resource
*res
, *ires
, *ires1
;
1514 void __iomem
*reg
= NULL
;
1515 struct fusb300
*fusb300
= NULL
;
1516 struct fusb300_ep
*_ep
[FUSB300_MAX_NUM_EP
];
1520 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
1523 pr_err("platform_get_resource error.\n");
1527 ires
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 0);
1531 "platform_get_resource IORESOURCE_IRQ error.\n");
1535 ires1
= platform_get_resource(pdev
, IORESOURCE_IRQ
, 1);
1539 "platform_get_resource IORESOURCE_IRQ 1 error.\n");
1543 reg
= ioremap(res
->start
, resource_size(res
));
1546 pr_err("ioremap error.\n");
1550 /* initialize udc */
1551 fusb300
= kzalloc(sizeof(struct fusb300
), GFP_KERNEL
);
1552 if (fusb300
== NULL
) {
1553 pr_err("kzalloc error\n");
1557 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1558 _ep
[i
] = kzalloc(sizeof(struct fusb300_ep
), GFP_KERNEL
);
1559 if (_ep
[i
] == NULL
) {
1560 pr_err("_ep kzalloc error\n");
1563 fusb300
->ep
[i
] = _ep
[i
];
1566 spin_lock_init(&fusb300
->lock
);
1568 dev_set_drvdata(&pdev
->dev
, fusb300
);
1570 fusb300
->gadget
.ops
= &fusb300_gadget_ops
;
1572 device_initialize(&fusb300
->gadget
.dev
);
1574 dev_set_name(&fusb300
->gadget
.dev
, "gadget");
1576 fusb300
->gadget
.is_dualspeed
= 1;
1577 fusb300
->gadget
.dev
.parent
= &pdev
->dev
;
1578 fusb300
->gadget
.dev
.dma_mask
= pdev
->dev
.dma_mask
;
1579 fusb300
->gadget
.dev
.release
= pdev
->dev
.release
;
1580 fusb300
->gadget
.name
= udc_name
;
1583 ret
= request_irq(ires
->start
, fusb300_irq
, IRQF_DISABLED
| IRQF_SHARED
,
1586 pr_err("request_irq error (%d)\n", ret
);
1590 ret
= request_irq(ires1
->start
, fusb300_irq
,
1591 IRQF_DISABLED
| IRQF_SHARED
, udc_name
, fusb300
);
1593 pr_err("request_irq1 error (%d)\n", ret
);
1597 INIT_LIST_HEAD(&fusb300
->gadget
.ep_list
);
1599 for (i
= 0; i
< FUSB300_MAX_NUM_EP
; i
++) {
1600 struct fusb300_ep
*ep
= fusb300
->ep
[i
];
1603 INIT_LIST_HEAD(&fusb300
->ep
[i
]->ep
.ep_list
);
1604 list_add_tail(&fusb300
->ep
[i
]->ep
.ep_list
,
1605 &fusb300
->gadget
.ep_list
);
1607 ep
->fusb300
= fusb300
;
1608 INIT_LIST_HEAD(&ep
->queue
);
1609 ep
->ep
.name
= fusb300_ep_name
[i
];
1610 ep
->ep
.ops
= &fusb300_ep_ops
;
1611 ep
->ep
.maxpacket
= HS_BULK_MAX_PACKET_SIZE
;
1613 fusb300
->ep
[0]->ep
.maxpacket
= HS_CTL_MAX_PACKET_SIZE
;
1614 fusb300
->ep
[0]->epnum
= 0;
1615 fusb300
->gadget
.ep0
= &fusb300
->ep
[0]->ep
;
1616 INIT_LIST_HEAD(&fusb300
->gadget
.ep0
->ep_list
);
1618 the_controller
= fusb300
;
1620 fusb300
->ep0_req
= fusb300_alloc_request(&fusb300
->ep
[0]->ep
,
1622 if (fusb300
->ep0_req
== NULL
)
1625 init_controller(fusb300
);
1626 ret
= usb_add_gadget_udc(&pdev
->dev
, &fusb300
->gadget
);
1630 dev_info(&pdev
->dev
, "version %s\n", DRIVER_VERSION
);
1634 fusb300_free_request(&fusb300
->ep
[0]->ep
, fusb300
->ep0_req
);
1637 free_irq(ires
->start
, fusb300
);
1641 if (fusb300
->ep0_req
)
1642 fusb300_free_request(&fusb300
->ep
[0]->ep
,
1652 static struct platform_driver fusb300_driver
= {
1653 .remove
= __exit_p(fusb300_remove
),
1655 .name
= (char *) udc_name
,
1656 .owner
= THIS_MODULE
,
1660 static int __init
fusb300_udc_init(void)
1662 return platform_driver_probe(&fusb300_driver
, fusb300_probe
);
1665 module_init(fusb300_udc_init
);
1667 static void __exit
fusb300_udc_cleanup(void)
1669 platform_driver_unregister(&fusb300_driver
);
1671 module_exit(fusb300_udc_cleanup
);